2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "got_compat.h"
21 #include <sys/queue.h>
23 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
64 #include "got_keyword.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #ifndef GOT_DEFAULT_EDITOR
71 #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
74 static volatile sig_atomic_t sigint_received
;
75 static volatile sig_atomic_t sigpipe_received
;
78 catch_sigint(int signo
)
84 catch_sigpipe(int signo
)
92 const struct got_error
*(*cmd_main
)(int, char *[]);
93 void (*cmd_usage
)(void);
94 const char *cmd_alias
;
97 __dead
static void usage(int, int);
98 __dead
static void usage_import(void);
99 __dead
static void usage_clone(void);
100 __dead
static void usage_fetch(void);
101 __dead
static void usage_checkout(void);
102 __dead
static void usage_update(void);
103 __dead
static void usage_log(void);
104 __dead
static void usage_diff(void);
105 __dead
static void usage_blame(void);
106 __dead
static void usage_tree(void);
107 __dead
static void usage_status(void);
108 __dead
static void usage_ref(void);
109 __dead
static void usage_branch(void);
110 __dead
static void usage_tag(void);
111 __dead
static void usage_add(void);
112 __dead
static void usage_remove(void);
113 __dead
static void usage_patch(void);
114 __dead
static void usage_revert(void);
115 __dead
static void usage_commit(void);
116 __dead
static void usage_send(void);
117 __dead
static void usage_cherrypick(void);
118 __dead
static void usage_backout(void);
119 __dead
static void usage_rebase(void);
120 __dead
static void usage_histedit(void);
121 __dead
static void usage_integrate(void);
122 __dead
static void usage_merge(void);
123 __dead
static void usage_stage(void);
124 __dead
static void usage_unstage(void);
125 __dead
static void usage_cat(void);
126 __dead
static void usage_info(void);
128 static const struct got_error
* cmd_import(int, char *[]);
129 static const struct got_error
* cmd_clone(int, char *[]);
130 static const struct got_error
* cmd_fetch(int, char *[]);
131 static const struct got_error
* cmd_checkout(int, char *[]);
132 static const struct got_error
* cmd_update(int, char *[]);
133 static const struct got_error
* cmd_log(int, char *[]);
134 static const struct got_error
* cmd_diff(int, char *[]);
135 static const struct got_error
* cmd_blame(int, char *[]);
136 static const struct got_error
* cmd_tree(int, char *[]);
137 static const struct got_error
* cmd_status(int, char *[]);
138 static const struct got_error
* cmd_ref(int, char *[]);
139 static const struct got_error
* cmd_branch(int, char *[]);
140 static const struct got_error
* cmd_tag(int, char *[]);
141 static const struct got_error
* cmd_add(int, char *[]);
142 static const struct got_error
* cmd_remove(int, char *[]);
143 static const struct got_error
* cmd_patch(int, char *[]);
144 static const struct got_error
* cmd_revert(int, char *[]);
145 static const struct got_error
* cmd_commit(int, char *[]);
146 static const struct got_error
* cmd_send(int, char *[]);
147 static const struct got_error
* cmd_cherrypick(int, char *[]);
148 static const struct got_error
* cmd_backout(int, char *[]);
149 static const struct got_error
* cmd_rebase(int, char *[]);
150 static const struct got_error
* cmd_histedit(int, char *[]);
151 static const struct got_error
* cmd_integrate(int, char *[]);
152 static const struct got_error
* cmd_merge(int, char *[]);
153 static const struct got_error
* cmd_stage(int, char *[]);
154 static const struct got_error
* cmd_unstage(int, char *[]);
155 static const struct got_error
* cmd_cat(int, char *[]);
156 static const struct got_error
* cmd_info(int, char *[]);
158 static const struct got_cmd got_commands
[] = {
159 { "import", cmd_import
, usage_import
, "im" },
160 { "clone", cmd_clone
, usage_clone
, "cl" },
161 { "fetch", cmd_fetch
, usage_fetch
, "fe" },
162 { "checkout", cmd_checkout
, usage_checkout
, "co" },
163 { "update", cmd_update
, usage_update
, "up" },
164 { "log", cmd_log
, usage_log
, "" },
165 { "diff", cmd_diff
, usage_diff
, "di" },
166 { "blame", cmd_blame
, usage_blame
, "bl" },
167 { "tree", cmd_tree
, usage_tree
, "tr" },
168 { "status", cmd_status
, usage_status
, "st" },
169 { "ref", cmd_ref
, usage_ref
, "" },
170 { "branch", cmd_branch
, usage_branch
, "br" },
171 { "tag", cmd_tag
, usage_tag
, "" },
172 { "add", cmd_add
, usage_add
, "" },
173 { "remove", cmd_remove
, usage_remove
, "rm" },
174 { "patch", cmd_patch
, usage_patch
, "pa" },
175 { "revert", cmd_revert
, usage_revert
, "rv" },
176 { "commit", cmd_commit
, usage_commit
, "ci" },
177 { "send", cmd_send
, usage_send
, "se" },
178 { "cherrypick", cmd_cherrypick
, usage_cherrypick
, "cy" },
179 { "backout", cmd_backout
, usage_backout
, "bo" },
180 { "rebase", cmd_rebase
, usage_rebase
, "rb" },
181 { "histedit", cmd_histedit
, usage_histedit
, "he" },
182 { "integrate", cmd_integrate
, usage_integrate
,"ig" },
183 { "merge", cmd_merge
, usage_merge
, "mg" },
184 { "stage", cmd_stage
, usage_stage
, "sg" },
185 { "unstage", cmd_unstage
, usage_unstage
, "ug" },
186 { "cat", cmd_cat
, usage_cat
, "" },
187 { "info", cmd_info
, usage_info
, "" },
191 list_commands(FILE *fp
)
195 fprintf(fp
, "commands:");
196 for (i
= 0; i
< nitems(got_commands
); i
++) {
197 const struct got_cmd
*cmd
= &got_commands
[i
];
198 fprintf(fp
, " %s", cmd
->cmd_name
);
204 option_conflict(char a
, char b
)
206 errx(1, "-%c and -%c options are mutually exclusive", a
, b
);
210 main(int argc
, char *argv
[])
212 const struct got_cmd
*cmd
;
215 int hflag
= 0, Vflag
= 0;
216 static const struct option longopts
[] = {
217 { "version", no_argument
, NULL
, 'V' },
221 setlocale(LC_CTYPE
, "");
223 while ((ch
= getopt_long(argc
, argv
, "+hV", longopts
, NULL
)) != -1) {
243 got_version_print_str();
248 usage(hflag
, hflag
? 0 : 1);
250 signal(SIGINT
, catch_sigint
);
251 signal(SIGPIPE
, catch_sigpipe
);
253 for (i
= 0; i
< nitems(got_commands
); i
++) {
254 const struct got_error
*error
;
256 cmd
= &got_commands
[i
];
258 if (strcmp(cmd
->cmd_name
, argv
[0]) != 0 &&
259 strcmp(cmd
->cmd_alias
, argv
[0]) != 0)
265 error
= cmd
->cmd_main(argc
, argv
);
266 if (error
&& error
->code
!= GOT_ERR_CANCELLED
&&
267 error
->code
!= GOT_ERR_PRIVSEP_EXIT
&&
268 !(sigpipe_received
&&
269 error
->code
== GOT_ERR_ERRNO
&& errno
== EPIPE
) &&
271 error
->code
== GOT_ERR_ERRNO
&& errno
== EINTR
)) {
273 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
280 fprintf(stderr
, "%s: unknown command '%s'\n", getprogname(), argv
[0]);
281 list_commands(stderr
);
286 usage(int hflag
, int status
)
288 FILE *fp
= (status
== 0) ? stdout
: stderr
;
290 fprintf(fp
, "usage: %s [-hV] command [arg ...]\n",
297 static const struct got_error
*
298 get_editor(char **abspath
)
300 const struct got_error
*err
= NULL
;
305 editor
= getenv("VISUAL");
307 editor
= getenv("EDITOR");
310 err
= got_path_find_prog(abspath
, editor
);
315 if (*abspath
== NULL
) {
316 *abspath
= strdup(GOT_DEFAULT_EDITOR
);
317 if (*abspath
== NULL
)
318 return got_error_from_errno("strdup");
324 static const struct got_error
*
325 apply_unveil(const char *repo_path
, int repo_read_only
,
326 const char *worktree_path
)
328 const struct got_error
*err
;
331 if (unveil("gmon.out", "rwc") != 0)
332 return got_error_from_errno2("unveil", "gmon.out");
334 if (repo_path
&& unveil(repo_path
, repo_read_only
? "r" : "rwc") != 0)
335 return got_error_from_errno2("unveil", repo_path
);
337 if (worktree_path
&& unveil(worktree_path
, "rwc") != 0)
338 return got_error_from_errno2("unveil", worktree_path
);
340 if (unveil(GOT_TMPDIR_STR
, "rwc") != 0)
341 return got_error_from_errno2("unveil", GOT_TMPDIR_STR
);
343 err
= got_privsep_unveil_exec_helpers();
347 if (unveil(NULL
, NULL
) != 0)
348 return got_error_from_errno("unveil");
356 fprintf(stderr
, "usage: %s import [-b branch] [-I pattern] [-m message] "
357 "[-r repository-path] directory\n", getprogname());
362 spawn_editor(const char *editor
, const char *file
)
365 sig_t sighup
, sigint
, sigquit
;
368 sighup
= signal(SIGHUP
, SIG_IGN
);
369 sigint
= signal(SIGINT
, SIG_IGN
);
370 sigquit
= signal(SIGQUIT
, SIG_IGN
);
372 switch (pid
= fork()) {
376 execl(editor
, editor
, file
, (char *)NULL
);
380 while (waitpid(pid
, &st
, 0) == -1)
385 (void)signal(SIGHUP
, sighup
);
386 (void)signal(SIGINT
, sigint
);
387 (void)signal(SIGQUIT
, sigquit
);
389 if (!WIFEXITED(st
)) {
394 return WEXITSTATUS(st
);
397 static const struct got_error
*
398 read_logmsg(char **logmsg
, size_t *len
, FILE *fp
, size_t filesize
)
400 const struct got_error
*err
= NULL
;
407 if (fseeko(fp
, 0L, SEEK_SET
) == -1)
408 return got_error_from_errno("fseeko");
410 *logmsg
= malloc(filesize
+ 1);
412 return got_error_from_errno("malloc");
415 while (getline(&line
, &linesize
, fp
) != -1) {
416 if (line
[0] == '#' || (*len
== 0 && line
[0] == '\n'))
417 continue; /* remove comments and leading empty lines */
418 *len
= strlcat(*logmsg
, line
, filesize
+ 1);
419 if (*len
>= filesize
+ 1) {
420 err
= got_error(GOT_ERR_NO_SPACE
);
425 err
= got_ferror(fp
, GOT_ERR_IO
);
429 while (*len
> 0 && (*logmsg
)[*len
- 1] == '\n') {
430 (*logmsg
)[*len
- 1] = '\0';
443 static const struct got_error
*
444 edit_logmsg(char **logmsg
, const char *editor
, const char *logmsg_path
,
445 const char *initial_content
, size_t initial_content_len
,
446 int require_modification
)
448 const struct got_error
*err
= NULL
;
455 if (stat(logmsg_path
, &st
) == -1)
456 return got_error_from_errno2("stat", logmsg_path
);
458 if (spawn_editor(editor
, logmsg_path
) == -1)
459 return got_error_from_errno("failed spawning editor");
461 if (require_modification
) {
462 struct timespec timeout
;
466 nanosleep(&timeout
, NULL
);
469 if (stat(logmsg_path
, &st2
) == -1)
470 return got_error_from_errno2("stat", logmsg_path
);
472 if (require_modification
&& st
.st_size
== st2
.st_size
&&
473 timespeccmp(&st
.st_mtim
, &st2
.st_mtim
, ==))
474 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
475 "no changes made to commit message, aborting");
477 fp
= fopen(logmsg_path
, "re");
479 err
= got_error_from_errno("fopen");
483 /* strip comments and leading/trailing newlines */
484 err
= read_logmsg(logmsg
, &logmsg_len
, fp
, st2
.st_size
);
487 if (logmsg_len
== 0) {
488 err
= got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY
,
489 "commit message cannot be empty, aborting");
493 if (fp
&& fclose(fp
) == EOF
&& err
== NULL
)
494 err
= got_error_from_errno("fclose");
502 static const struct got_error
*
503 collect_import_msg(char **logmsg
, char **logmsg_path
, const char *editor
,
504 const char *path_dir
, const char *branch_name
)
506 char *initial_content
= NULL
;
507 const struct got_error
*err
= NULL
;
508 int initial_content_len
;
511 initial_content_len
= asprintf(&initial_content
,
512 "\n# %s to be imported to branch %s\n", path_dir
,
514 if (initial_content_len
== -1)
515 return got_error_from_errno("asprintf");
517 err
= got_opentemp_named_fd(logmsg_path
, &fd
,
518 GOT_TMPDIR_STR
"/got-importmsg", "");
522 if (write(fd
, initial_content
, initial_content_len
) == -1) {
523 err
= got_error_from_errno2("write", *logmsg_path
);
526 if (close(fd
) == -1) {
527 err
= got_error_from_errno2("close", *logmsg_path
);
532 err
= edit_logmsg(logmsg
, editor
, *logmsg_path
, initial_content
,
533 initial_content_len
, 1);
535 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
536 err
= got_error_from_errno2("close", *logmsg_path
);
537 free(initial_content
);
545 static const struct got_error
*
546 import_progress(void *arg
, const char *path
)
548 printf("A %s\n", path
);
552 static const struct got_error
*
553 valid_author(const char *author
)
555 const char *email
= author
;
558 * Git' expects the author (or committer) to be in the form
559 * "name <email>", which are mostly free form (see the
560 * "committer" description in git-fast-import(1)). We're only
561 * doing this to avoid git's object parser breaking on commits
565 while (*author
&& *author
!= '\n' && *author
!= '<' && *author
!= '>')
567 if (author
!= email
&& *author
== '<' && *(author
- 1) != ' ')
568 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR
, "%s: space "
569 "between author name and email required", email
);
570 if (*author
++ != '<')
571 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL
, "%s", email
);
572 while (*author
&& *author
!= '\n' && *author
!= '<' && *author
!= '>')
574 if (strcmp(author
, ">") != 0)
575 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL
, "%s", email
);
579 static const struct got_error
*
580 get_author(char **author
, struct got_repository
*repo
,
581 struct got_worktree
*worktree
)
583 const struct got_error
*err
= NULL
;
584 const char *got_author
= NULL
, *name
, *email
;
585 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
590 worktree_conf
= got_worktree_get_gotconfig(worktree
);
591 repo_conf
= got_repo_get_gotconfig(repo
);
594 * Priority of potential author information sources, from most
595 * significant to least significant:
596 * 1) work tree's .got/got.conf file
597 * 2) repository's got.conf file
598 * 3) repository's git config file
599 * 4) environment variables
600 * 5) global git config files (in user's home directory or /etc)
604 got_author
= got_gotconfig_get_author(worktree_conf
);
605 if (got_author
== NULL
)
606 got_author
= got_gotconfig_get_author(repo_conf
);
607 if (got_author
== NULL
) {
608 name
= got_repo_get_gitconfig_author_name(repo
);
609 email
= got_repo_get_gitconfig_author_email(repo
);
611 if (asprintf(author
, "%s <%s>", name
, email
) == -1)
612 return got_error_from_errno("asprintf");
616 got_author
= getenv("GOT_AUTHOR");
617 if (got_author
== NULL
) {
618 name
= got_repo_get_global_gitconfig_author_name(repo
);
619 email
= got_repo_get_global_gitconfig_author_email(
622 if (asprintf(author
, "%s <%s>", name
, email
)
624 return got_error_from_errno("asprintf");
627 /* TODO: Look up user in password database? */
628 return got_error(GOT_ERR_COMMIT_NO_AUTHOR
);
632 *author
= strdup(got_author
);
634 return got_error_from_errno("strdup");
636 err
= valid_author(*author
);
644 static const struct got_error
*
645 get_allowed_signers(char **allowed_signers
, struct got_repository
*repo
,
646 struct got_worktree
*worktree
)
648 const char *got_allowed_signers
= NULL
;
649 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
651 *allowed_signers
= NULL
;
654 worktree_conf
= got_worktree_get_gotconfig(worktree
);
655 repo_conf
= got_repo_get_gotconfig(repo
);
658 * Priority of potential author information sources, from most
659 * significant to least significant:
660 * 1) work tree's .got/got.conf file
661 * 2) repository's got.conf file
665 got_allowed_signers
= got_gotconfig_get_allowed_signers_file(
667 if (got_allowed_signers
== NULL
)
668 got_allowed_signers
= got_gotconfig_get_allowed_signers_file(
671 if (got_allowed_signers
) {
672 *allowed_signers
= strdup(got_allowed_signers
);
673 if (*allowed_signers
== NULL
)
674 return got_error_from_errno("strdup");
679 static const struct got_error
*
680 get_revoked_signers(char **revoked_signers
, struct got_repository
*repo
,
681 struct got_worktree
*worktree
)
683 const char *got_revoked_signers
= NULL
;
684 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
686 *revoked_signers
= NULL
;
689 worktree_conf
= got_worktree_get_gotconfig(worktree
);
690 repo_conf
= got_repo_get_gotconfig(repo
);
693 * Priority of potential author information sources, from most
694 * significant to least significant:
695 * 1) work tree's .got/got.conf file
696 * 2) repository's got.conf file
700 got_revoked_signers
= got_gotconfig_get_revoked_signers_file(
702 if (got_revoked_signers
== NULL
)
703 got_revoked_signers
= got_gotconfig_get_revoked_signers_file(
706 if (got_revoked_signers
) {
707 *revoked_signers
= strdup(got_revoked_signers
);
708 if (*revoked_signers
== NULL
)
709 return got_error_from_errno("strdup");
715 get_signer_id(struct got_repository
*repo
, struct got_worktree
*worktree
)
717 const char *got_signer_id
= NULL
;
718 const struct got_gotconfig
*worktree_conf
= NULL
, *repo_conf
= NULL
;
721 worktree_conf
= got_worktree_get_gotconfig(worktree
);
722 repo_conf
= got_repo_get_gotconfig(repo
);
725 * Priority of potential author information sources, from most
726 * significant to least significant:
727 * 1) work tree's .got/got.conf file
728 * 2) repository's got.conf file
732 got_signer_id
= got_gotconfig_get_signer_id(worktree_conf
);
733 if (got_signer_id
== NULL
)
734 got_signer_id
= got_gotconfig_get_signer_id(repo_conf
);
736 return got_signer_id
;
739 static const struct got_error
*
740 get_gitconfig_path(char **gitconfig_path
)
742 const char *homedir
= getenv("HOME");
744 *gitconfig_path
= NULL
;
746 if (asprintf(gitconfig_path
, "%s/.gitconfig", homedir
) == -1)
747 return got_error_from_errno("asprintf");
753 static const struct got_error
*
754 cmd_import(int argc
, char *argv
[])
756 const struct got_error
*error
= NULL
;
757 char *path_dir
= NULL
, *repo_path
= NULL
, *logmsg
= NULL
;
758 char *gitconfig_path
= NULL
, *editor
= NULL
, *author
= NULL
;
759 const char *branch_name
= NULL
;
760 char *id_str
= NULL
, *logmsg_path
= NULL
;
761 char refname
[PATH_MAX
] = "refs/heads/";
762 struct got_repository
*repo
= NULL
;
763 struct got_reference
*branch_ref
= NULL
, *head_ref
= NULL
;
764 struct got_object_id
*new_commit_id
= NULL
;
766 struct got_pathlist_head ignores
;
767 struct got_pathlist_entry
*pe
;
768 int preserve_logmsg
= 0;
769 int *pack_fds
= NULL
;
771 TAILQ_INIT(&ignores
);
774 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
780 while ((ch
= getopt(argc
, argv
, "b:I:m:r:")) != -1) {
783 branch_name
= optarg
;
786 if (optarg
[0] == '\0')
788 error
= got_pathlist_insert(&pe
, &ignores
, optarg
,
794 logmsg
= strdup(optarg
);
795 if (logmsg
== NULL
) {
796 error
= got_error_from_errno("strdup");
801 repo_path
= realpath(optarg
, NULL
);
802 if (repo_path
== NULL
) {
803 error
= got_error_from_errno2("realpath",
820 if (repo_path
== NULL
) {
821 repo_path
= getcwd(NULL
, 0);
822 if (repo_path
== NULL
)
823 return got_error_from_errno("getcwd");
825 got_path_strip_trailing_slashes(repo_path
);
826 error
= get_gitconfig_path(&gitconfig_path
);
829 error
= got_repo_pack_fds_open(&pack_fds
);
832 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
, pack_fds
);
836 path_dir
= realpath(argv
[0], NULL
);
837 if (path_dir
== NULL
) {
838 error
= got_error_from_errno2("realpath", argv
[0]);
841 got_path_strip_trailing_slashes(path_dir
);
843 error
= get_editor(&editor
);
847 if (unveil(path_dir
, "r") != 0) {
848 error
= got_error_from_errno2("unveil", path_dir
);
851 if (unveil(editor
, "x") != 0) {
852 error
= got_error_from_errno2("unveil", editor
);
855 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
859 error
= get_author(&author
, repo
, NULL
);
864 * Don't let the user create a branch name with a leading '-'.
865 * While technically a valid reference name, this case is usually
866 * an unintended typo.
868 if (branch_name
&& branch_name
[0] == '-')
869 return got_error_path(branch_name
, GOT_ERR_REF_NAME_MINUS
);
871 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
872 if (error
&& error
->code
!= GOT_ERR_NOT_REF
)
876 n
= strlcat(refname
, branch_name
, sizeof(refname
));
877 else if (head_ref
&& got_ref_is_symbolic(head_ref
))
878 n
= strlcpy(refname
, got_ref_get_symref_target(head_ref
),
881 n
= strlcat(refname
, "main", sizeof(refname
));
882 if (n
>= sizeof(refname
)) {
883 error
= got_error(GOT_ERR_NO_SPACE
);
887 error
= got_ref_open(&branch_ref
, repo
, refname
, 0);
889 if (error
->code
!= GOT_ERR_NOT_REF
)
892 error
= got_error_msg(GOT_ERR_BRANCH_EXISTS
,
893 "import target branch already exists");
897 if (logmsg
== NULL
|| *logmsg
== '\0') {
899 error
= collect_import_msg(&logmsg
, &logmsg_path
, editor
,
902 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
909 error
= got_repo_import(&new_commit_id
, path_dir
, logmsg
,
910 author
, &ignores
, repo
, import_progress
, NULL
);
917 error
= got_ref_alloc(&branch_ref
, refname
, new_commit_id
);
924 error
= got_ref_write(branch_ref
, repo
);
931 error
= got_object_id_str(&id_str
, new_commit_id
);
938 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
940 if (error
->code
!= GOT_ERR_NOT_REF
) {
946 error
= got_ref_alloc_symref(&head_ref
, GOT_REF_HEAD
,
954 error
= got_ref_write(head_ref
, repo
);
962 printf("Created branch %s with commit %s\n",
963 got_ref_get_name(branch_ref
), id_str
);
966 const struct got_error
*pack_err
=
967 got_repo_pack_fds_close(pack_fds
);
972 const struct got_error
*close_err
= got_repo_close(repo
);
976 if (preserve_logmsg
) {
977 fprintf(stderr
, "%s: log message preserved in %s\n",
978 getprogname(), logmsg_path
);
979 } else if (logmsg_path
&& unlink(logmsg_path
) == -1 && error
== NULL
)
980 error
= got_error_from_errno2("unlink", logmsg_path
);
988 free(gitconfig_path
);
990 got_ref_close(branch_ref
);
992 got_ref_close(head_ref
);
999 fprintf(stderr
, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1000 "repository-URL [directory]\n", getprogname());
1004 struct got_fetch_progress_arg
{
1005 char last_scaled_size
[FMT_SCALED_STRSIZE
];
1007 int last_p_resolved
;
1010 struct got_repository
*repo
;
1013 int configs_created
;
1015 struct got_pathlist_head
*symrefs
;
1016 struct got_pathlist_head
*wanted_branches
;
1017 struct got_pathlist_head
*wanted_refs
;
1021 const char *remote_repo_path
;
1022 const char *git_url
;
1023 int fetch_all_branches
;
1024 int mirror_references
;
1028 /* XXX forward declaration */
1029 static const struct got_error
*
1030 create_config_files(const char *proto
, const char *host
, const char *port
,
1031 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
1032 int mirror_references
, struct got_pathlist_head
*symrefs
,
1033 struct got_pathlist_head
*wanted_branches
,
1034 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
);
1036 static const struct got_error
*
1037 fetch_progress(void *arg
, const char *message
, off_t packfile_size
,
1038 int nobj_total
, int nobj_indexed
, int nobj_loose
, int nobj_resolved
)
1040 const struct got_error
*err
= NULL
;
1041 struct got_fetch_progress_arg
*a
= arg
;
1042 char scaled_size
[FMT_SCALED_STRSIZE
];
1043 int p_indexed
, p_resolved
;
1044 int print_size
= 0, print_indexed
= 0, print_resolved
= 0;
1047 * In order to allow a failed clone to be resumed with 'got fetch'
1048 * we try to create configuration files as soon as possible.
1049 * Once the server has sent information about its default branch
1050 * we have all required information.
1052 if (a
->create_configs
&& !a
->configs_created
&&
1053 !TAILQ_EMPTY(a
->config_info
.symrefs
)) {
1054 err
= create_config_files(a
->config_info
.proto
,
1055 a
->config_info
.host
, a
->config_info
.port
,
1056 a
->config_info
.remote_repo_path
,
1057 a
->config_info
.git_url
,
1058 a
->config_info
.fetch_all_branches
,
1059 a
->config_info
.mirror_references
,
1060 a
->config_info
.symrefs
,
1061 a
->config_info
.wanted_branches
,
1062 a
->config_info
.wanted_refs
, a
->repo
);
1065 a
->configs_created
= 1;
1068 if (a
->verbosity
< 0)
1071 if (message
&& message
[0] != '\0') {
1072 printf("\rserver: %s", message
);
1077 if (packfile_size
> 0 || nobj_indexed
> 0) {
1078 if (fmt_scaled(packfile_size
, scaled_size
) == 0 &&
1079 (a
->last_scaled_size
[0] == '\0' ||
1080 strcmp(scaled_size
, a
->last_scaled_size
)) != 0) {
1082 if (strlcpy(a
->last_scaled_size
, scaled_size
,
1083 FMT_SCALED_STRSIZE
) >= FMT_SCALED_STRSIZE
)
1084 return got_error(GOT_ERR_NO_SPACE
);
1086 if (nobj_indexed
> 0) {
1087 p_indexed
= (nobj_indexed
* 100) / nobj_total
;
1088 if (p_indexed
!= a
->last_p_indexed
) {
1089 a
->last_p_indexed
= p_indexed
;
1094 if (nobj_resolved
> 0) {
1095 p_resolved
= (nobj_resolved
* 100) /
1096 (nobj_total
- nobj_loose
);
1097 if (p_resolved
!= a
->last_p_resolved
) {
1098 a
->last_p_resolved
= p_resolved
;
1106 if (print_size
|| print_indexed
|| print_resolved
)
1109 printf("%*s fetched", FMT_SCALED_STRSIZE
- 2, scaled_size
);
1111 printf("; indexing %d%%", p_indexed
);
1113 printf("; resolving deltas %d%%", p_resolved
);
1114 if (print_size
|| print_indexed
|| print_resolved
)
1120 static const struct got_error
*
1121 create_symref(const char *refname
, struct got_reference
*target_ref
,
1122 int verbosity
, struct got_repository
*repo
)
1124 const struct got_error
*err
;
1125 struct got_reference
*head_symref
;
1127 err
= got_ref_alloc_symref(&head_symref
, refname
, target_ref
);
1131 err
= got_ref_write(head_symref
, repo
);
1132 if (err
== NULL
&& verbosity
> 0) {
1133 printf("Created reference %s: %s\n", GOT_REF_HEAD
,
1134 got_ref_get_name(target_ref
));
1136 got_ref_close(head_symref
);
1140 static const struct got_error
*
1141 list_remote_refs(struct got_pathlist_head
*symrefs
,
1142 struct got_pathlist_head
*refs
)
1144 const struct got_error
*err
;
1145 struct got_pathlist_entry
*pe
;
1147 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1148 const char *refname
= pe
->path
;
1149 const char *targetref
= pe
->data
;
1151 printf("%s: %s\n", refname
, targetref
);
1154 TAILQ_FOREACH(pe
, refs
, entry
) {
1155 const char *refname
= pe
->path
;
1156 struct got_object_id
*id
= pe
->data
;
1159 err
= got_object_id_str(&id_str
, id
);
1162 printf("%s: %s\n", refname
, id_str
);
1169 static const struct got_error
*
1170 create_ref(const char *refname
, struct got_object_id
*id
,
1171 int verbosity
, struct got_repository
*repo
)
1173 const struct got_error
*err
= NULL
;
1174 struct got_reference
*ref
;
1177 err
= got_object_id_str(&id_str
, id
);
1181 err
= got_ref_alloc(&ref
, refname
, id
);
1185 err
= got_ref_write(ref
, repo
);
1188 if (err
== NULL
&& verbosity
>= 0)
1189 printf("Created reference %s: %s\n", refname
, id_str
);
1196 match_wanted_ref(const char *refname
, const char *wanted_ref
)
1198 if (strncmp(refname
, "refs/", 5) != 0)
1203 * Prevent fetching of references that won't make any
1204 * sense outside of the remote repository's context.
1206 if (strncmp(refname
, "got/", 4) == 0)
1208 if (strncmp(refname
, "remotes/", 8) == 0)
1211 if (strncmp(wanted_ref
, "refs/", 5) == 0)
1214 /* Allow prefix match. */
1215 if (got_path_is_child(refname
, wanted_ref
, strlen(wanted_ref
)))
1218 /* Allow exact match. */
1219 return (strcmp(refname
, wanted_ref
) == 0);
1223 is_wanted_ref(struct got_pathlist_head
*wanted_refs
, const char *refname
)
1225 struct got_pathlist_entry
*pe
;
1227 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1228 if (match_wanted_ref(refname
, pe
->path
))
1235 static const struct got_error
*
1236 create_wanted_ref(const char *refname
, struct got_object_id
*id
,
1237 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
1239 const struct got_error
*err
;
1240 char *remote_refname
;
1242 if (strncmp("refs/", refname
, 5) == 0)
1245 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
1246 remote_repo_name
, refname
) == -1)
1247 return got_error_from_errno("asprintf");
1249 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
1250 free(remote_refname
);
1254 static const struct got_error
*
1255 create_gotconfig(const char *proto
, const char *host
, const char *port
,
1256 const char *remote_repo_path
, const char *default_branch
,
1257 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1258 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1259 struct got_repository
*repo
)
1261 const struct got_error
*err
= NULL
;
1262 char *gotconfig_path
= NULL
;
1263 char *gotconfig
= NULL
;
1264 FILE *gotconfig_file
= NULL
;
1265 const char *branchname
= NULL
;
1266 char *branches
= NULL
, *refs
= NULL
;
1269 if (!fetch_all_branches
&& !TAILQ_EMPTY(wanted_branches
)) {
1270 struct got_pathlist_entry
*pe
;
1271 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1273 branchname
= pe
->path
;
1274 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1276 if (asprintf(&s
, "%s\"%s\" ",
1277 branches
? branches
: "", branchname
) == -1) {
1278 err
= got_error_from_errno("asprintf");
1284 } else if (!fetch_all_branches
&& default_branch
) {
1285 branchname
= default_branch
;
1286 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1288 if (asprintf(&branches
, "\"%s\" ", branchname
) == -1) {
1289 err
= got_error_from_errno("asprintf");
1293 if (!TAILQ_EMPTY(wanted_refs
)) {
1294 struct got_pathlist_entry
*pe
;
1295 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1297 const char *refname
= pe
->path
;
1298 if (strncmp(refname
, "refs/", 5) == 0)
1300 if (asprintf(&s
, "%s\"%s\" ",
1301 refs
? refs
: "", refname
) == -1) {
1302 err
= got_error_from_errno("asprintf");
1310 /* Create got.conf(5). */
1311 gotconfig_path
= got_repo_get_path_gotconfig(repo
);
1312 if (gotconfig_path
== NULL
) {
1313 err
= got_error_from_errno("got_repo_get_path_gotconfig");
1316 gotconfig_file
= fopen(gotconfig_path
, "ae");
1317 if (gotconfig_file
== NULL
) {
1318 err
= got_error_from_errno2("fopen", gotconfig_path
);
1321 if (asprintf(&gotconfig
,
1326 "\trepository \"%s\"\n"
1332 GOT_FETCH_DEFAULT_REMOTE_NAME
, host
, proto
,
1333 port
? "\tport " : "", port
? port
: "", port
? "\n" : "",
1334 remote_repo_path
, branches
? "\tbranch { " : "",
1335 branches
? branches
: "", branches
? "}\n" : "",
1336 refs
? "\treference { " : "", refs
? refs
: "", refs
? "}\n" : "",
1337 mirror_references
? "\tmirror_references yes\n" : "",
1338 fetch_all_branches
? "\tfetch_all_branches yes\n" : "") == -1) {
1339 err
= got_error_from_errno("asprintf");
1342 n
= fwrite(gotconfig
, 1, strlen(gotconfig
), gotconfig_file
);
1343 if (n
!= strlen(gotconfig
)) {
1344 err
= got_ferror(gotconfig_file
, GOT_ERR_IO
);
1349 if (gotconfig_file
&& fclose(gotconfig_file
) == EOF
&& err
== NULL
)
1350 err
= got_error_from_errno2("fclose", gotconfig_path
);
1351 free(gotconfig_path
);
1356 static const struct got_error
*
1357 create_gitconfig(const char *git_url
, const char *default_branch
,
1358 int fetch_all_branches
, struct got_pathlist_head
*wanted_branches
,
1359 struct got_pathlist_head
*wanted_refs
, int mirror_references
,
1360 struct got_repository
*repo
)
1362 const struct got_error
*err
= NULL
;
1363 char *gitconfig_path
= NULL
;
1364 char *gitconfig
= NULL
;
1365 FILE *gitconfig_file
= NULL
;
1366 char *branches
= NULL
, *refs
= NULL
;
1367 const char *branchname
;
1370 /* Create a config file Git can understand. */
1371 gitconfig_path
= got_repo_get_path_gitconfig(repo
);
1372 if (gitconfig_path
== NULL
) {
1373 err
= got_error_from_errno("got_repo_get_path_gitconfig");
1376 gitconfig_file
= fopen(gitconfig_path
, "ae");
1377 if (gitconfig_file
== NULL
) {
1378 err
= got_error_from_errno2("fopen", gitconfig_path
);
1381 if (fetch_all_branches
) {
1382 if (mirror_references
) {
1383 if (asprintf(&branches
,
1384 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1385 err
= got_error_from_errno("asprintf");
1388 } else if (asprintf(&branches
,
1389 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1390 GOT_FETCH_DEFAULT_REMOTE_NAME
) == -1) {
1391 err
= got_error_from_errno("asprintf");
1394 } else if (!TAILQ_EMPTY(wanted_branches
)) {
1395 struct got_pathlist_entry
*pe
;
1396 TAILQ_FOREACH(pe
, wanted_branches
, entry
) {
1398 branchname
= pe
->path
;
1399 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1401 if (mirror_references
) {
1403 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1404 branches
? branches
: "",
1405 branchname
, branchname
) == -1) {
1406 err
= got_error_from_errno("asprintf");
1409 } else if (asprintf(&s
,
1410 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1411 branches
? branches
: "",
1412 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1413 branchname
) == -1) {
1414 err
= got_error_from_errno("asprintf");
1422 * If the server specified a default branch, use just that one.
1423 * Otherwise fall back to fetching all branches on next fetch.
1425 if (default_branch
) {
1426 branchname
= default_branch
;
1427 if (strncmp(branchname
, "refs/heads/", 11) == 0)
1430 branchname
= "*"; /* fall back to all branches */
1431 if (mirror_references
) {
1432 if (asprintf(&branches
,
1433 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1434 branchname
, branchname
) == -1) {
1435 err
= got_error_from_errno("asprintf");
1438 } else if (asprintf(&branches
,
1439 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1440 branchname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1441 branchname
) == -1) {
1442 err
= got_error_from_errno("asprintf");
1446 if (!TAILQ_EMPTY(wanted_refs
)) {
1447 struct got_pathlist_entry
*pe
;
1448 TAILQ_FOREACH(pe
, wanted_refs
, entry
) {
1450 const char *refname
= pe
->path
;
1451 if (strncmp(refname
, "refs/", 5) == 0)
1453 if (mirror_references
) {
1455 "%s\tfetch = refs/%s:refs/%s\n",
1456 refs
? refs
: "", refname
, refname
) == -1) {
1457 err
= got_error_from_errno("asprintf");
1460 } else if (asprintf(&s
,
1461 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1463 refname
, GOT_FETCH_DEFAULT_REMOTE_NAME
,
1465 err
= got_error_from_errno("asprintf");
1473 if (asprintf(&gitconfig
,
1478 "\tfetch = refs/tags/*:refs/tags/*\n",
1479 GOT_FETCH_DEFAULT_REMOTE_NAME
, git_url
, branches
? branches
: "",
1480 refs
? refs
: "") == -1) {
1481 err
= got_error_from_errno("asprintf");
1484 n
= fwrite(gitconfig
, 1, strlen(gitconfig
), gitconfig_file
);
1485 if (n
!= strlen(gitconfig
)) {
1486 err
= got_ferror(gitconfig_file
, GOT_ERR_IO
);
1490 if (gitconfig_file
&& fclose(gitconfig_file
) == EOF
&& err
== NULL
)
1491 err
= got_error_from_errno2("fclose", gitconfig_path
);
1492 free(gitconfig_path
);
1497 static const struct got_error
*
1498 create_config_files(const char *proto
, const char *host
, const char *port
,
1499 const char *remote_repo_path
, const char *git_url
, int fetch_all_branches
,
1500 int mirror_references
, struct got_pathlist_head
*symrefs
,
1501 struct got_pathlist_head
*wanted_branches
,
1502 struct got_pathlist_head
*wanted_refs
, struct got_repository
*repo
)
1504 const struct got_error
*err
= NULL
;
1505 const char *default_branch
= NULL
;
1506 struct got_pathlist_entry
*pe
;
1509 * If we asked for a set of wanted branches then use the first
1512 if (!TAILQ_EMPTY(wanted_branches
)) {
1513 pe
= TAILQ_FIRST(wanted_branches
);
1514 default_branch
= pe
->path
;
1516 /* First HEAD ref listed by server is the default branch. */
1517 TAILQ_FOREACH(pe
, symrefs
, entry
) {
1518 const char *refname
= pe
->path
;
1519 const char *target
= pe
->data
;
1521 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1524 default_branch
= target
;
1529 /* Create got.conf(5). */
1530 err
= create_gotconfig(proto
, host
, port
, remote_repo_path
,
1531 default_branch
, fetch_all_branches
, wanted_branches
,
1532 wanted_refs
, mirror_references
, repo
);
1536 /* Create a config file Git can understand. */
1537 return create_gitconfig(git_url
, default_branch
, fetch_all_branches
,
1538 wanted_branches
, wanted_refs
, mirror_references
, repo
);
1541 static const struct got_error
*
1542 cmd_clone(int argc
, char *argv
[])
1544 const struct got_error
*error
= NULL
;
1545 const char *uri
, *dirname
;
1546 char *proto
, *host
, *port
, *repo_name
, *server_path
;
1547 char *default_destdir
= NULL
, *id_str
= NULL
;
1548 const char *repo_path
;
1549 struct got_repository
*repo
= NULL
;
1550 struct got_pathlist_head refs
, symrefs
, wanted_branches
, wanted_refs
;
1551 struct got_pathlist_entry
*pe
;
1552 struct got_object_id
*pack_hash
= NULL
;
1553 int ch
, fetchfd
= -1, fetchstatus
;
1554 pid_t fetchpid
= -1;
1555 struct got_fetch_progress_arg fpa
;
1556 char *git_url
= NULL
;
1557 int verbosity
= 0, fetch_all_branches
= 0, mirror_references
= 0;
1558 int bflag
= 0, list_refs_only
= 0;
1559 int *pack_fds
= NULL
;
1562 TAILQ_INIT(&symrefs
);
1563 TAILQ_INIT(&wanted_branches
);
1564 TAILQ_INIT(&wanted_refs
);
1566 while ((ch
= getopt(argc
, argv
, "ab:lmqR:v")) != -1) {
1569 fetch_all_branches
= 1;
1572 error
= got_pathlist_append(&wanted_branches
,
1582 mirror_references
= 1;
1588 error
= got_pathlist_append(&wanted_refs
,
1596 else if (verbosity
< 3)
1607 if (fetch_all_branches
&& !TAILQ_EMPTY(&wanted_branches
))
1608 option_conflict('a', 'b');
1609 if (list_refs_only
) {
1610 if (!TAILQ_EMPTY(&wanted_branches
))
1611 option_conflict('l', 'b');
1612 if (fetch_all_branches
)
1613 option_conflict('l', 'a');
1614 if (mirror_references
)
1615 option_conflict('l', 'm');
1616 if (!TAILQ_EMPTY(&wanted_refs
))
1617 option_conflict('l', 'R');
1629 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
1634 if (asprintf(&git_url
, "%s://%s%s%s%s%s", proto
,
1635 host
, port
? ":" : "", port
? port
: "",
1636 server_path
[0] != '/' ? "/" : "", server_path
) == -1) {
1637 error
= got_error_from_errno("asprintf");
1641 if (strcmp(proto
, "git") == 0) {
1643 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1644 "sendfd dns inet unveil", NULL
) == -1)
1647 } else if (strcmp(proto
, "git+ssh") == 0 ||
1648 strcmp(proto
, "ssh") == 0 ||
1649 strcmp(proto
, "git+http") == 0 ||
1650 strcmp(proto
, "http") == 0 ||
1651 strcmp(proto
, "git+https") == 0 ||
1652 strcmp(proto
, "https") == 0) {
1654 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1655 "sendfd unveil", NULL
) == -1)
1659 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
1662 if (dirname
== NULL
) {
1663 if (asprintf(&default_destdir
, "%s.git", repo_name
) == -1) {
1664 error
= got_error_from_errno("asprintf");
1667 repo_path
= default_destdir
;
1669 repo_path
= dirname
;
1671 if (!list_refs_only
) {
1672 error
= got_path_mkdir(repo_path
);
1674 (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
1675 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
)))
1677 if (!got_path_dir_is_empty(repo_path
)) {
1678 error
= got_error_path(repo_path
,
1679 GOT_ERR_DIR_NOT_EMPTY
);
1684 error
= got_dial_apply_unveil(proto
);
1688 error
= apply_unveil(repo_path
, 0, NULL
);
1693 printf("Connecting to %s\n", git_url
);
1695 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
1696 server_path
, verbosity
);
1701 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
1705 if (!list_refs_only
) {
1706 error
= got_repo_init(repo_path
, NULL
);
1709 error
= got_repo_pack_fds_open(&pack_fds
);
1712 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
1717 fpa
.last_scaled_size
[0] = '\0';
1718 fpa
.last_p_indexed
= -1;
1719 fpa
.last_p_resolved
= -1;
1720 fpa
.verbosity
= verbosity
;
1721 fpa
.create_configs
= 1;
1722 fpa
.configs_created
= 0;
1724 fpa
.config_info
.symrefs
= &symrefs
;
1725 fpa
.config_info
.wanted_branches
= &wanted_branches
;
1726 fpa
.config_info
.wanted_refs
= &wanted_refs
;
1727 fpa
.config_info
.proto
= proto
;
1728 fpa
.config_info
.host
= host
;
1729 fpa
.config_info
.port
= port
;
1730 fpa
.config_info
.remote_repo_path
= server_path
;
1731 fpa
.config_info
.git_url
= git_url
;
1732 fpa
.config_info
.fetch_all_branches
= fetch_all_branches
;
1733 fpa
.config_info
.mirror_references
= mirror_references
;
1734 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
,
1735 GOT_FETCH_DEFAULT_REMOTE_NAME
, mirror_references
,
1736 fetch_all_branches
, &wanted_branches
, &wanted_refs
,
1737 list_refs_only
, verbosity
, fetchfd
, repo
, NULL
, NULL
, bflag
,
1738 fetch_progress
, &fpa
);
1742 if (list_refs_only
) {
1743 error
= list_remote_refs(&symrefs
, &refs
);
1747 if (pack_hash
== NULL
) {
1748 error
= got_error_fmt(GOT_ERR_FETCH_FAILED
, "%s",
1749 "server sent an empty pack file");
1752 error
= got_object_id_str(&id_str
, pack_hash
);
1756 printf("\nFetched %s.pack\n", id_str
);
1759 /* Set up references provided with the pack file. */
1760 TAILQ_FOREACH(pe
, &refs
, entry
) {
1761 const char *refname
= pe
->path
;
1762 struct got_object_id
*id
= pe
->data
;
1763 char *remote_refname
;
1765 if (is_wanted_ref(&wanted_refs
, refname
) &&
1766 !mirror_references
) {
1767 error
= create_wanted_ref(refname
, id
,
1768 GOT_FETCH_DEFAULT_REMOTE_NAME
,
1769 verbosity
- 1, repo
);
1775 error
= create_ref(refname
, id
, verbosity
- 1, repo
);
1779 if (mirror_references
)
1782 if (strncmp("refs/heads/", refname
, 11) != 0)
1785 if (asprintf(&remote_refname
,
1786 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1787 refname
+ 11) == -1) {
1788 error
= got_error_from_errno("asprintf");
1791 error
= create_ref(remote_refname
, id
, verbosity
- 1, repo
);
1792 free(remote_refname
);
1797 /* Set the HEAD reference if the server provided one. */
1798 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
1799 struct got_reference
*target_ref
;
1800 const char *refname
= pe
->path
;
1801 const char *target
= pe
->data
;
1802 char *remote_refname
= NULL
, *remote_target
= NULL
;
1804 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
1807 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1809 if (error
->code
== GOT_ERR_NOT_REF
) {
1816 error
= create_symref(refname
, target_ref
, verbosity
, repo
);
1817 got_ref_close(target_ref
);
1821 if (mirror_references
)
1824 if (strncmp("refs/heads/", target
, 11) != 0)
1827 if (asprintf(&remote_refname
,
1828 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1830 error
= got_error_from_errno("asprintf");
1833 if (asprintf(&remote_target
,
1834 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME
,
1835 target
+ 11) == -1) {
1836 error
= got_error_from_errno("asprintf");
1837 free(remote_refname
);
1840 error
= got_ref_open(&target_ref
, repo
, remote_target
, 0);
1842 free(remote_refname
);
1843 free(remote_target
);
1844 if (error
->code
== GOT_ERR_NOT_REF
) {
1850 error
= create_symref(remote_refname
, target_ref
,
1851 verbosity
- 1, repo
);
1852 free(remote_refname
);
1853 free(remote_target
);
1854 got_ref_close(target_ref
);
1860 * We failed to set the HEAD reference. If we asked for
1861 * a set of wanted branches use the first of one of those
1862 * which could be fetched instead.
1864 TAILQ_FOREACH(pe
, &wanted_branches
, entry
) {
1865 const char *target
= pe
->path
;
1866 struct got_reference
*target_ref
;
1868 error
= got_ref_open(&target_ref
, repo
, target
, 0);
1870 if (error
->code
== GOT_ERR_NOT_REF
) {
1877 error
= create_symref(GOT_REF_HEAD
, target_ref
,
1879 got_ref_close(target_ref
);
1885 if (!fpa
.configs_created
&& pe
!= NULL
) {
1886 error
= create_config_files(fpa
.config_info
.proto
,
1887 fpa
.config_info
.host
, fpa
.config_info
.port
,
1888 fpa
.config_info
.remote_repo_path
,
1889 fpa
.config_info
.git_url
,
1890 fpa
.config_info
.fetch_all_branches
,
1891 fpa
.config_info
.mirror_references
,
1892 fpa
.config_info
.symrefs
,
1893 fpa
.config_info
.wanted_branches
,
1894 fpa
.config_info
.wanted_refs
, fpa
.repo
);
1901 printf("Created %s repository '%s'\n",
1902 mirror_references
? "mirrored" : "cloned", repo_path
);
1905 const struct got_error
*pack_err
=
1906 got_repo_pack_fds_close(pack_fds
);
1911 if (kill(fetchpid
, SIGTERM
) == -1)
1912 error
= got_error_from_errno("kill");
1913 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
1914 error
= got_error_from_errno("waitpid");
1916 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
1917 error
= got_error_from_errno("close");
1919 const struct got_error
*close_err
= got_repo_close(repo
);
1923 got_pathlist_free(&refs
, GOT_PATHLIST_FREE_ALL
);
1924 got_pathlist_free(&symrefs
, GOT_PATHLIST_FREE_ALL
);
1925 got_pathlist_free(&wanted_branches
, GOT_PATHLIST_FREE_NONE
);
1926 got_pathlist_free(&wanted_refs
, GOT_PATHLIST_FREE_NONE
);
1933 free(default_destdir
);
1938 static const struct got_error
*
1939 update_ref(struct got_reference
*ref
, struct got_object_id
*new_id
,
1940 int replace_tags
, int verbosity
, struct got_repository
*repo
)
1942 const struct got_error
*err
= NULL
;
1943 char *new_id_str
= NULL
;
1944 struct got_object_id
*old_id
= NULL
;
1946 err
= got_object_id_str(&new_id_str
, new_id
);
1950 if (!replace_tags
&&
1951 strncmp(got_ref_get_name(ref
), "refs/tags/", 10) == 0) {
1952 err
= got_ref_resolve(&old_id
, repo
, ref
);
1955 if (got_object_id_cmp(old_id
, new_id
) == 0)
1957 if (verbosity
>= 0) {
1958 printf("Rejecting update of existing tag %s: %s\n",
1959 got_ref_get_name(ref
), new_id_str
);
1964 if (got_ref_is_symbolic(ref
)) {
1965 if (verbosity
>= 0) {
1966 printf("Replacing reference %s: %s\n",
1967 got_ref_get_name(ref
),
1968 got_ref_get_symref_target(ref
));
1970 err
= got_ref_change_symref_to_ref(ref
, new_id
);
1973 err
= got_ref_write(ref
, repo
);
1977 err
= got_ref_resolve(&old_id
, repo
, ref
);
1980 if (got_object_id_cmp(old_id
, new_id
) == 0)
1983 err
= got_ref_change_ref(ref
, new_id
);
1986 err
= got_ref_write(ref
, repo
);
1992 printf("Updated %s: %s\n", got_ref_get_name(ref
),
2000 static const struct got_error
*
2001 update_symref(const char *refname
, struct got_reference
*target_ref
,
2002 int verbosity
, struct got_repository
*repo
)
2004 const struct got_error
*err
= NULL
, *unlock_err
;
2005 struct got_reference
*symref
;
2006 int symref_is_locked
= 0;
2008 err
= got_ref_open(&symref
, repo
, refname
, 1);
2010 if (err
->code
!= GOT_ERR_NOT_REF
)
2012 err
= got_ref_alloc_symref(&symref
, refname
, target_ref
);
2016 err
= got_ref_write(symref
, repo
);
2021 printf("Created reference %s: %s\n",
2022 got_ref_get_name(symref
),
2023 got_ref_get_symref_target(symref
));
2025 symref_is_locked
= 1;
2027 if (strcmp(got_ref_get_symref_target(symref
),
2028 got_ref_get_name(target_ref
)) == 0)
2031 err
= got_ref_change_symref(symref
,
2032 got_ref_get_name(target_ref
));
2036 err
= got_ref_write(symref
, repo
);
2041 printf("Updated %s: %s\n", got_ref_get_name(symref
),
2042 got_ref_get_symref_target(symref
));
2046 if (symref_is_locked
) {
2047 unlock_err
= got_ref_unlock(symref
);
2048 if (unlock_err
&& err
== NULL
)
2051 got_ref_close(symref
);
2058 fprintf(stderr
, "usage: %s fetch [-adlqtvX] [-b branch] "
2059 "[-R reference] [-r repository-path] [remote-repository]\n",
2064 static const struct got_error
*
2065 delete_missing_ref(struct got_reference
*ref
,
2066 int verbosity
, struct got_repository
*repo
)
2068 const struct got_error
*err
= NULL
;
2069 struct got_object_id
*id
= NULL
;
2070 char *id_str
= NULL
;
2072 if (got_ref_is_symbolic(ref
)) {
2073 err
= got_ref_delete(ref
, repo
);
2076 if (verbosity
>= 0) {
2077 printf("Deleted %s: %s\n",
2078 got_ref_get_name(ref
),
2079 got_ref_get_symref_target(ref
));
2082 err
= got_ref_resolve(&id
, repo
, ref
);
2085 err
= got_object_id_str(&id_str
, id
);
2089 err
= got_ref_delete(ref
, repo
);
2092 if (verbosity
>= 0) {
2093 printf("Deleted %s: %s\n",
2094 got_ref_get_name(ref
), id_str
);
2103 static const struct got_error
*
2104 delete_missing_refs(struct got_pathlist_head
*their_refs
,
2105 struct got_pathlist_head
*their_symrefs
,
2106 const struct got_remote_repo
*remote
,
2107 int verbosity
, struct got_repository
*repo
)
2109 const struct got_error
*err
= NULL
, *unlock_err
;
2110 struct got_reflist_head my_refs
;
2111 struct got_reflist_entry
*re
;
2112 struct got_pathlist_entry
*pe
;
2113 char *remote_namespace
= NULL
;
2114 char *local_refname
= NULL
;
2116 TAILQ_INIT(&my_refs
);
2118 if (asprintf(&remote_namespace
, "refs/remotes/%s/", remote
->name
)
2120 return got_error_from_errno("asprintf");
2122 err
= got_ref_list(&my_refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
2126 TAILQ_FOREACH(re
, &my_refs
, entry
) {
2127 const char *refname
= got_ref_get_name(re
->ref
);
2128 const char *their_refname
;
2130 if (remote
->mirror_references
) {
2131 their_refname
= refname
;
2133 if (strncmp(refname
, remote_namespace
,
2134 strlen(remote_namespace
)) == 0) {
2135 if (strcmp(refname
+ strlen(remote_namespace
),
2138 if (asprintf(&local_refname
, "refs/heads/%s",
2139 refname
+ strlen(remote_namespace
)) == -1) {
2140 err
= got_error_from_errno("asprintf");
2143 } else if (strncmp(refname
, "refs/tags/", 10) != 0)
2146 their_refname
= local_refname
;
2149 TAILQ_FOREACH(pe
, their_refs
, entry
) {
2150 if (strcmp(their_refname
, pe
->path
) == 0)
2156 TAILQ_FOREACH(pe
, their_symrefs
, entry
) {
2157 if (strcmp(their_refname
, pe
->path
) == 0)
2163 err
= delete_missing_ref(re
->ref
, verbosity
, repo
);
2167 if (local_refname
) {
2168 struct got_reference
*ref
;
2169 err
= got_ref_open(&ref
, repo
, local_refname
, 1);
2171 if (err
->code
!= GOT_ERR_NOT_REF
)
2173 free(local_refname
);
2174 local_refname
= NULL
;
2177 err
= delete_missing_ref(ref
, verbosity
, repo
);
2180 unlock_err
= got_ref_unlock(ref
);
2182 if (unlock_err
&& err
== NULL
) {
2187 free(local_refname
);
2188 local_refname
= NULL
;
2192 got_ref_list_free(&my_refs
);
2193 free(remote_namespace
);
2194 free(local_refname
);
2198 static const struct got_error
*
2199 update_wanted_ref(const char *refname
, struct got_object_id
*id
,
2200 const char *remote_repo_name
, int verbosity
, struct got_repository
*repo
)
2202 const struct got_error
*err
, *unlock_err
;
2203 char *remote_refname
;
2204 struct got_reference
*ref
;
2206 if (strncmp("refs/", refname
, 5) == 0)
2209 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2210 remote_repo_name
, refname
) == -1)
2211 return got_error_from_errno("asprintf");
2213 err
= got_ref_open(&ref
, repo
, remote_refname
, 1);
2215 if (err
->code
!= GOT_ERR_NOT_REF
)
2217 err
= create_ref(remote_refname
, id
, verbosity
, repo
);
2219 err
= update_ref(ref
, id
, 0, verbosity
, repo
);
2220 unlock_err
= got_ref_unlock(ref
);
2221 if (unlock_err
&& err
== NULL
)
2226 free(remote_refname
);
2230 static const struct got_error
*
2231 delete_ref(struct got_repository
*repo
, struct got_reference
*ref
)
2233 const struct got_error
*err
= NULL
;
2234 struct got_object_id
*id
= NULL
;
2235 char *id_str
= NULL
;
2238 if (got_ref_is_symbolic(ref
)) {
2239 target
= got_ref_get_symref_target(ref
);
2241 err
= got_ref_resolve(&id
, repo
, ref
);
2244 err
= got_object_id_str(&id_str
, id
);
2250 err
= got_ref_delete(ref
, repo
);
2254 printf("Deleted %s: %s\n", got_ref_get_name(ref
), target
);
2261 static const struct got_error
*
2262 delete_refs_for_remote(struct got_repository
*repo
, const char *remote_name
)
2264 const struct got_error
*err
= NULL
;
2265 struct got_reflist_head refs
;
2266 struct got_reflist_entry
*re
;
2271 if (asprintf(&prefix
, "refs/remotes/%s", remote_name
) == -1) {
2272 err
= got_error_from_errno("asprintf");
2275 err
= got_ref_list(&refs
, repo
, prefix
, got_ref_cmp_by_name
, NULL
);
2279 TAILQ_FOREACH(re
, &refs
, entry
)
2280 delete_ref(repo
, re
->ref
);
2282 got_ref_list_free(&refs
);
2286 static const struct got_error
*
2287 cmd_fetch(int argc
, char *argv
[])
2289 const struct got_error
*error
= NULL
, *unlock_err
;
2290 char *cwd
= NULL
, *repo_path
= NULL
;
2291 const char *remote_name
;
2292 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
2293 char *repo_name
= NULL
, *server_path
= NULL
;
2294 const struct got_remote_repo
*remotes
;
2295 struct got_remote_repo
*remote
= NULL
;
2297 char *id_str
= NULL
;
2298 struct got_repository
*repo
= NULL
;
2299 struct got_worktree
*worktree
= NULL
;
2300 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
2301 struct got_pathlist_head refs
, symrefs
, wanted_branches
, wanted_refs
;
2302 char *head_refname
= NULL
;
2303 struct got_pathlist_entry
*pe
;
2304 struct got_reflist_head remote_refs
;
2305 struct got_reflist_entry
*re
;
2306 struct got_object_id
*pack_hash
= NULL
;
2307 int i
, ch
, fetchfd
= -1, fetchstatus
;
2308 pid_t fetchpid
= -1;
2309 struct got_fetch_progress_arg fpa
;
2310 int verbosity
= 0, fetch_all_branches
= 0, list_refs_only
= 0;
2311 int delete_refs
= 0, replace_tags
= 0, delete_remote
= 0;
2312 int *pack_fds
= NULL
, have_bflag
= 0;
2313 const char *remote_head
= NULL
, *worktree_branch
= NULL
;
2316 TAILQ_INIT(&symrefs
);
2317 TAILQ_INIT(&remote_refs
);
2318 TAILQ_INIT(&wanted_branches
);
2319 TAILQ_INIT(&wanted_refs
);
2321 while ((ch
= getopt(argc
, argv
, "ab:dlqR:r:tvX")) != -1) {
2324 fetch_all_branches
= 1;
2327 error
= got_pathlist_append(&wanted_branches
,
2343 error
= got_pathlist_append(&wanted_refs
,
2349 repo_path
= realpath(optarg
, NULL
);
2350 if (repo_path
== NULL
)
2351 return got_error_from_errno2("realpath",
2353 got_path_strip_trailing_slashes(repo_path
);
2361 else if (verbosity
< 3)
2375 if (fetch_all_branches
&& !TAILQ_EMPTY(&wanted_branches
))
2376 option_conflict('a', 'b');
2377 if (list_refs_only
) {
2378 if (!TAILQ_EMPTY(&wanted_branches
))
2379 option_conflict('l', 'b');
2380 if (fetch_all_branches
)
2381 option_conflict('l', 'a');
2383 option_conflict('l', 'd');
2385 option_conflict('l', 'X');
2387 if (delete_remote
) {
2388 if (fetch_all_branches
)
2389 option_conflict('X', 'a');
2390 if (!TAILQ_EMPTY(&wanted_branches
))
2391 option_conflict('X', 'b');
2393 option_conflict('X', 'd');
2395 option_conflict('X', 't');
2396 if (!TAILQ_EMPTY(&wanted_refs
))
2397 option_conflict('X', 'R');
2402 errx(1, "-X option requires a remote name");
2403 remote_name
= GOT_FETCH_DEFAULT_REMOTE_NAME
;
2404 } else if (argc
== 1)
2405 remote_name
= argv
[0];
2409 cwd
= getcwd(NULL
, 0);
2411 error
= got_error_from_errno("getcwd");
2415 error
= got_repo_pack_fds_open(&pack_fds
);
2419 if (repo_path
== NULL
) {
2420 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
2421 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
2427 strdup(got_worktree_get_repo_path(worktree
));
2428 if (repo_path
== NULL
)
2429 error
= got_error_from_errno("strdup");
2433 repo_path
= strdup(cwd
);
2434 if (repo_path
== NULL
) {
2435 error
= got_error_from_errno("strdup");
2441 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
2445 if (delete_remote
) {
2446 error
= delete_refs_for_remote(repo
, remote_name
);
2447 goto done
; /* nothing else to do */
2451 worktree_conf
= got_worktree_get_gotconfig(worktree
);
2452 if (worktree_conf
) {
2453 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2455 for (i
= 0; i
< nremotes
; i
++) {
2456 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2457 error
= got_repo_remote_repo_dup(&remote
,
2466 if (remote
== NULL
) {
2467 repo_conf
= got_repo_get_gotconfig(repo
);
2469 got_gotconfig_get_remotes(&nremotes
, &remotes
,
2471 for (i
= 0; i
< nremotes
; i
++) {
2472 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2473 error
= got_repo_remote_repo_dup(&remote
,
2482 if (remote
== NULL
) {
2483 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
2484 for (i
= 0; i
< nremotes
; i
++) {
2485 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
2486 error
= got_repo_remote_repo_dup(&remote
,
2494 if (remote
== NULL
) {
2495 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
2499 if (TAILQ_EMPTY(&wanted_branches
)) {
2500 if (!fetch_all_branches
)
2501 fetch_all_branches
= remote
->fetch_all_branches
;
2502 for (i
= 0; i
< remote
->nfetch_branches
; i
++) {
2503 error
= got_pathlist_append(&wanted_branches
,
2504 remote
->fetch_branches
[i
], NULL
);
2509 if (TAILQ_EMPTY(&wanted_refs
)) {
2510 for (i
= 0; i
< remote
->nfetch_refs
; i
++) {
2511 error
= got_pathlist_append(&wanted_refs
,
2512 remote
->fetch_refs
[i
], NULL
);
2518 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
2519 &repo_name
, remote
->fetch_url
);
2523 if (strcmp(proto
, "git") == 0) {
2525 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2526 "sendfd dns inet unveil", NULL
) == -1)
2529 } else if (strcmp(proto
, "git+ssh") == 0 ||
2530 strcmp(proto
, "ssh") == 0 ||
2531 strcmp(proto
, "git+http") == 0 ||
2532 strcmp(proto
, "http") == 0 ||
2533 strcmp(proto
, "git+https") == 0 ||
2534 strcmp(proto
, "https") == 0) {
2536 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2537 "sendfd unveil", NULL
) == -1)
2541 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
2545 error
= got_dial_apply_unveil(proto
);
2549 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
2554 head_refname
= strdup(got_worktree_get_head_ref_name(worktree
));
2555 if (head_refname
== NULL
) {
2556 error
= got_error_from_errno("strdup");
2560 /* Release work tree lock. */
2561 got_worktree_close(worktree
);
2565 if (verbosity
>= 0) {
2566 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2567 remote
->name
, proto
, host
,
2568 port
? ":" : "", port
? port
: "",
2569 *server_path
== '/' ? "" : "/", server_path
);
2572 error
= got_fetch_connect(&fetchpid
, &fetchfd
, proto
, host
, port
,
2573 server_path
, verbosity
);
2577 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd",
2583 * If set, get this remote's HEAD ref target so
2584 * if it has changed on the server we can fetch it.
2586 error
= got_ref_list(&remote_refs
, repo
, "refs/remotes",
2587 got_ref_cmp_by_name
, repo
);
2591 TAILQ_FOREACH(re
, &remote_refs
, entry
) {
2592 const char *remote_refname
, *remote_target
;
2593 size_t remote_name_len
;
2595 if (!got_ref_is_symbolic(re
->ref
))
2598 remote_name_len
= strlen(remote
->name
);
2599 remote_refname
= got_ref_get_name(re
->ref
);
2601 /* we only want refs/remotes/$remote->name/HEAD */
2602 if (strncmp(remote_refname
+ 13, remote
->name
,
2603 remote_name_len
) != 0)
2606 if (strcmp(remote_refname
+ remote_name_len
+ 14,
2611 * Take the name itself because we already
2612 * only match with refs/heads/ in fetch_pack().
2614 remote_target
= got_ref_get_symref_target(re
->ref
);
2615 remote_head
= remote_target
+ remote_name_len
+ 14;
2620 strncmp(head_refname
, "refs/heads/", 11) == 0)
2621 worktree_branch
= head_refname
;
2624 fpa
.last_scaled_size
[0] = '\0';
2625 fpa
.last_p_indexed
= -1;
2626 fpa
.last_p_resolved
= -1;
2627 fpa
.verbosity
= verbosity
;
2629 fpa
.create_configs
= 0;
2630 fpa
.configs_created
= 0;
2631 memset(&fpa
.config_info
, 0, sizeof(fpa
.config_info
));
2633 error
= got_fetch_pack(&pack_hash
, &refs
, &symrefs
, remote
->name
,
2634 remote
->mirror_references
, fetch_all_branches
, &wanted_branches
,
2635 &wanted_refs
, list_refs_only
, verbosity
, fetchfd
, repo
,
2636 worktree_branch
, remote_head
, have_bflag
, fetch_progress
, &fpa
);
2640 if (list_refs_only
) {
2641 error
= list_remote_refs(&symrefs
, &refs
);
2645 if (pack_hash
== NULL
) {
2647 printf("Already up-to-date\n");
2648 } else if (verbosity
>= 0) {
2649 error
= got_object_id_str(&id_str
, pack_hash
);
2652 printf("\nFetched %s.pack\n", id_str
);
2657 /* Update references provided with the pack file. */
2658 TAILQ_FOREACH(pe
, &refs
, entry
) {
2659 const char *refname
= pe
->path
;
2660 struct got_object_id
*id
= pe
->data
;
2661 struct got_reference
*ref
;
2662 char *remote_refname
;
2664 if (is_wanted_ref(&wanted_refs
, refname
) &&
2665 !remote
->mirror_references
) {
2666 error
= update_wanted_ref(refname
, id
,
2667 remote
->name
, verbosity
, repo
);
2673 if (remote
->mirror_references
||
2674 strncmp("refs/tags/", refname
, 10) == 0) {
2675 error
= got_ref_open(&ref
, repo
, refname
, 1);
2677 if (error
->code
!= GOT_ERR_NOT_REF
)
2679 error
= create_ref(refname
, id
, verbosity
,
2684 error
= update_ref(ref
, id
, replace_tags
,
2686 unlock_err
= got_ref_unlock(ref
);
2687 if (unlock_err
&& error
== NULL
)
2693 } else if (strncmp("refs/heads/", refname
, 11) == 0) {
2694 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2695 remote_name
, refname
+ 11) == -1) {
2696 error
= got_error_from_errno("asprintf");
2700 error
= got_ref_open(&ref
, repo
, remote_refname
, 1);
2702 if (error
->code
!= GOT_ERR_NOT_REF
)
2704 error
= create_ref(remote_refname
, id
,
2709 error
= update_ref(ref
, id
, replace_tags
,
2711 unlock_err
= got_ref_unlock(ref
);
2712 if (unlock_err
&& error
== NULL
)
2719 /* Also create a local branch if none exists yet. */
2720 error
= got_ref_open(&ref
, repo
, refname
, 1);
2722 if (error
->code
!= GOT_ERR_NOT_REF
)
2724 error
= create_ref(refname
, id
, verbosity
,
2729 unlock_err
= got_ref_unlock(ref
);
2730 if (unlock_err
&& error
== NULL
)
2737 error
= delete_missing_refs(&refs
, &symrefs
, remote
,
2743 if (!remote
->mirror_references
) {
2744 /* Update remote HEAD reference if the server provided one. */
2745 TAILQ_FOREACH(pe
, &symrefs
, entry
) {
2746 struct got_reference
*target_ref
;
2747 const char *refname
= pe
->path
;
2748 const char *target
= pe
->data
;
2749 char *remote_refname
= NULL
, *remote_target
= NULL
;
2751 if (strcmp(refname
, GOT_REF_HEAD
) != 0)
2754 if (strncmp("refs/heads/", target
, 11) != 0)
2757 if (asprintf(&remote_refname
, "refs/remotes/%s/%s",
2758 remote
->name
, refname
) == -1) {
2759 error
= got_error_from_errno("asprintf");
2762 if (asprintf(&remote_target
, "refs/remotes/%s/%s",
2763 remote
->name
, target
+ 11) == -1) {
2764 error
= got_error_from_errno("asprintf");
2765 free(remote_refname
);
2769 error
= got_ref_open(&target_ref
, repo
, remote_target
,
2772 free(remote_refname
);
2773 free(remote_target
);
2774 if (error
->code
== GOT_ERR_NOT_REF
) {
2780 error
= update_symref(remote_refname
, target_ref
,
2782 free(remote_refname
);
2783 free(remote_target
);
2784 got_ref_close(target_ref
);
2791 if (kill(fetchpid
, SIGTERM
) == -1)
2792 error
= got_error_from_errno("kill");
2793 if (waitpid(fetchpid
, &fetchstatus
, 0) == -1 && error
== NULL
)
2794 error
= got_error_from_errno("waitpid");
2796 if (fetchfd
!= -1 && close(fetchfd
) == -1 && error
== NULL
)
2797 error
= got_error_from_errno("close");
2799 const struct got_error
*close_err
= got_repo_close(repo
);
2804 got_worktree_close(worktree
);
2806 const struct got_error
*pack_err
=
2807 got_repo_pack_fds_close(pack_fds
);
2811 got_pathlist_free(&refs
, GOT_PATHLIST_FREE_ALL
);
2812 got_pathlist_free(&symrefs
, GOT_PATHLIST_FREE_ALL
);
2813 got_pathlist_free(&wanted_branches
, GOT_PATHLIST_FREE_NONE
);
2814 got_pathlist_free(&wanted_refs
, GOT_PATHLIST_FREE_NONE
);
2815 got_ref_list_free(&remote_refs
);
2816 got_repo_free_remote_repo_data(remote
);
2833 usage_checkout(void)
2835 fprintf(stderr
, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2836 "[-p path-prefix] repository-path [work-tree-path]\n",
2842 show_worktree_base_ref_warning(void)
2844 fprintf(stderr
, "%s: warning: could not create a reference "
2845 "to the work tree's base commit; the commit could be "
2846 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2847 "repository writable and running 'got update' will prevent this\n",
2851 struct got_checkout_progress_arg
{
2852 const char *worktree_path
;
2853 int had_base_commit_ref_error
;
2857 static const struct got_error
*
2858 checkout_progress(void *arg
, unsigned char status
, const char *path
)
2860 struct got_checkout_progress_arg
*a
= arg
;
2862 /* Base commit bump happens silently. */
2863 if (status
== GOT_STATUS_BUMP_BASE
)
2866 if (status
== GOT_STATUS_BASE_REF_ERR
) {
2867 a
->had_base_commit_ref_error
= 1;
2871 while (path
[0] == '/')
2874 if (a
->verbosity
>= 0)
2875 printf("%c %s/%s\n", status
, a
->worktree_path
, path
);
2880 static const struct got_error
*
2881 check_cancelled(void *arg
)
2883 if (sigint_received
|| sigpipe_received
)
2884 return got_error(GOT_ERR_CANCELLED
);
2888 static const struct got_error
*
2889 check_linear_ancestry(struct got_object_id
*commit_id
,
2890 struct got_object_id
*base_commit_id
, int allow_forwards_in_time_only
,
2891 struct got_repository
*repo
)
2893 const struct got_error
*err
= NULL
;
2894 struct got_object_id
*yca_id
;
2896 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
2897 commit_id
, base_commit_id
, 1, 0, repo
, check_cancelled
, NULL
);
2902 return got_error(GOT_ERR_ANCESTRY
);
2905 * Require a straight line of history between the target commit
2906 * and the work tree's base commit.
2908 * Non-linear situations such as this require a rebase:
2910 * (commit) D F (base_commit)
2918 * 'got update' only handles linear cases:
2919 * Update forwards in time: A (base/yca) - B - C - D (commit)
2920 * Update backwards in time: D (base) - C - B - A (commit/yca)
2922 if (allow_forwards_in_time_only
) {
2923 if (got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2924 return got_error(GOT_ERR_ANCESTRY
);
2925 } else if (got_object_id_cmp(commit_id
, yca_id
) != 0 &&
2926 got_object_id_cmp(base_commit_id
, yca_id
) != 0)
2927 return got_error(GOT_ERR_ANCESTRY
);
2933 static const struct got_error
*
2934 check_same_branch(struct got_object_id
*commit_id
,
2935 struct got_reference
*head_ref
, struct got_repository
*repo
)
2937 const struct got_error
*err
= NULL
;
2938 struct got_commit_graph
*graph
= NULL
;
2939 struct got_object_id
*head_commit_id
= NULL
;
2941 err
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
2945 if (got_object_id_cmp(head_commit_id
, commit_id
) == 0)
2948 err
= got_commit_graph_open(&graph
, "/", 1);
2952 err
= got_commit_graph_bfsort(graph
, head_commit_id
, repo
,
2953 check_cancelled
, NULL
);
2958 struct got_object_id id
;
2960 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
2961 check_cancelled
, NULL
);
2963 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
2964 err
= got_error(GOT_ERR_ANCESTRY
);
2968 if (got_object_id_cmp(&id
, commit_id
) == 0)
2973 got_commit_graph_close(graph
);
2974 free(head_commit_id
);
2978 static const struct got_error
*
2979 checkout_ancestry_error(struct got_reference
*ref
, const char *commit_id_str
)
2981 static char msg
[512];
2982 const char *branch_name
;
2984 if (got_ref_is_symbolic(ref
))
2985 branch_name
= got_ref_get_symref_target(ref
);
2987 branch_name
= got_ref_get_name(ref
);
2989 if (strncmp("refs/heads/", branch_name
, 11) == 0)
2992 snprintf(msg
, sizeof(msg
),
2993 "target commit is not contained in branch '%s'; "
2994 "the branch to use must be specified with -b; "
2995 "if necessary a new branch can be created for "
2996 "this commit with 'got branch -c %s BRANCH_NAME'",
2997 branch_name
, commit_id_str
);
2999 return got_error_msg(GOT_ERR_ANCESTRY
, msg
);
3002 static const struct got_error
*
3003 cmd_checkout(int argc
, char *argv
[])
3005 const struct got_error
*close_err
, *error
= NULL
;
3006 struct got_repository
*repo
= NULL
;
3007 struct got_reference
*head_ref
= NULL
, *ref
= NULL
;
3008 struct got_worktree
*worktree
= NULL
;
3009 char *repo_path
= NULL
;
3010 char *worktree_path
= NULL
;
3011 const char *path_prefix
= "";
3012 const char *branch_name
= GOT_REF_HEAD
, *refname
= NULL
;
3013 char *commit_id_str
= NULL
, *keyword_idstr
= NULL
;
3014 struct got_object_id
*commit_id
= NULL
;
3016 int ch
, same_path_prefix
, allow_nonempty
= 0, verbosity
= 0;
3017 struct got_pathlist_head paths
;
3018 struct got_checkout_progress_arg cpa
;
3019 int *pack_fds
= NULL
;
3024 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3025 "unveil", NULL
) == -1)
3029 while ((ch
= getopt(argc
, argv
, "b:c:Ep:q")) != -1) {
3032 branch_name
= optarg
;
3035 commit_id_str
= strdup(optarg
);
3036 if (commit_id_str
== NULL
)
3037 return got_error_from_errno("strdup");
3043 path_prefix
= optarg
;
3058 char *base
, *dotgit
;
3060 repo_path
= realpath(argv
[0], NULL
);
3061 if (repo_path
== NULL
)
3062 return got_error_from_errno2("realpath", argv
[0]);
3063 cwd
= getcwd(NULL
, 0);
3065 error
= got_error_from_errno("getcwd");
3072 error
= got_path_basename(&base
, path
);
3075 dotgit
= strstr(base
, ".git");
3078 if (asprintf(&worktree_path
, "%s/%s", cwd
, base
) == -1) {
3079 error
= got_error_from_errno("asprintf");
3084 } else if (argc
== 2) {
3085 repo_path
= realpath(argv
[0], NULL
);
3086 if (repo_path
== NULL
) {
3087 error
= got_error_from_errno2("realpath", argv
[0]);
3090 worktree_path
= realpath(argv
[1], NULL
);
3091 if (worktree_path
== NULL
) {
3092 if (errno
!= ENOENT
) {
3093 error
= got_error_from_errno2("realpath",
3097 worktree_path
= strdup(argv
[1]);
3098 if (worktree_path
== NULL
) {
3099 error
= got_error_from_errno("strdup");
3106 got_path_strip_trailing_slashes(repo_path
);
3107 got_path_strip_trailing_slashes(worktree_path
);
3109 if (got_path_is_child(worktree_path
, repo_path
, strlen(repo_path
)) ||
3110 got_path_is_child(repo_path
, worktree_path
,
3111 strlen(worktree_path
))) {
3112 error
= got_error_fmt(GOT_ERR_BAD_PATH
,
3113 "work tree and repository paths may not overlap: %s",
3118 error
= got_repo_pack_fds_open(&pack_fds
);
3122 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
3126 /* Pre-create work tree path for unveil(2) */
3127 error
= got_path_mkdir(worktree_path
);
3129 if (!(error
->code
== GOT_ERR_ERRNO
&& errno
== EISDIR
) &&
3130 !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
3132 if (!allow_nonempty
&&
3133 !got_path_dir_is_empty(worktree_path
)) {
3134 error
= got_error_path(worktree_path
,
3135 GOT_ERR_DIR_NOT_EMPTY
);
3140 error
= apply_unveil(got_repo_get_path(repo
), 0, worktree_path
);
3144 error
= got_ref_open(&head_ref
, repo
, branch_name
, 0);
3148 error
= got_worktree_init(worktree_path
, head_ref
, path_prefix
,
3149 GOT_WORKTREE_GOT_DIR
, repo
);
3150 if (error
!= NULL
&& !(error
->code
== GOT_ERR_ERRNO
&& errno
== EEXIST
))
3153 error
= got_worktree_open(&worktree
, worktree_path
,
3154 GOT_WORKTREE_GOT_DIR
);
3158 error
= got_worktree_match_path_prefix(&same_path_prefix
, worktree
,
3162 if (!same_path_prefix
) {
3163 error
= got_error(GOT_ERR_PATH_PREFIX
);
3167 if (commit_id_str
) {
3168 struct got_reflist_head refs
;
3170 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
3175 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
3179 if (keyword_idstr
!= NULL
) {
3180 free(commit_id_str
);
3181 commit_id_str
= keyword_idstr
;
3184 error
= got_repo_match_object_id(&commit_id
, NULL
,
3185 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
3186 got_ref_list_free(&refs
);
3189 error
= check_linear_ancestry(commit_id
,
3190 got_worktree_get_base_commit_id(worktree
), 0, repo
);
3191 if (error
!= NULL
) {
3192 if (error
->code
== GOT_ERR_ANCESTRY
) {
3193 error
= checkout_ancestry_error(
3194 head_ref
, commit_id_str
);
3198 error
= check_same_branch(commit_id
, head_ref
, repo
);
3200 if (error
->code
== GOT_ERR_ANCESTRY
) {
3201 error
= checkout_ancestry_error(
3202 head_ref
, commit_id_str
);
3206 error
= got_worktree_set_base_commit_id(worktree
, repo
,
3210 /* Expand potentially abbreviated commit ID string. */
3211 free(commit_id_str
);
3212 error
= got_object_id_str(&commit_id_str
, commit_id
);
3216 commit_id
= got_object_id_dup(
3217 got_worktree_get_base_commit_id(worktree
));
3218 if (commit_id
== NULL
) {
3219 error
= got_error_from_errno("got_object_id_dup");
3222 error
= got_object_id_str(&commit_id_str
, commit_id
);
3227 error
= got_pathlist_append(&paths
, "", NULL
);
3230 cpa
.worktree_path
= worktree_path
;
3231 cpa
.had_base_commit_ref_error
= 0;
3232 cpa
.verbosity
= verbosity
;
3233 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
3234 checkout_progress
, &cpa
, check_cancelled
, NULL
);
3238 if (got_ref_is_symbolic(head_ref
)) {
3239 error
= got_ref_resolve_symbolic(&ref
, repo
, head_ref
);
3242 refname
= got_ref_get_name(ref
);
3244 refname
= got_ref_get_name(head_ref
);
3245 printf("Checked out %s: %s\n", refname
, commit_id_str
);
3246 printf("Now shut up and hack\n");
3247 if (cpa
.had_base_commit_ref_error
)
3248 show_worktree_base_ref_warning();
3251 const struct got_error
*pack_err
=
3252 got_repo_pack_fds_close(pack_fds
);
3257 got_ref_close(head_ref
);
3261 close_err
= got_repo_close(repo
);
3265 if (worktree
!= NULL
) {
3266 close_err
= got_worktree_close(worktree
);
3270 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
3271 free(commit_id_str
);
3274 free(worktree_path
);
3279 struct got_update_progress_arg
{
3291 print_update_progress_stats(struct got_update_progress_arg
*upa
)
3293 if (!upa
->did_something
)
3296 if (upa
->conflicts
> 0)
3297 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
3298 if (upa
->obstructed
> 0)
3299 printf("File paths obstructed by a non-regular file: %d\n",
3301 if (upa
->not_updated
> 0)
3302 printf("Files not updated because of existing merge "
3303 "conflicts: %d\n", upa
->not_updated
);
3307 * The meaning of some status codes differs between merge-style operations and
3308 * update operations. For example, the ! status code means "file was missing"
3309 * if changes were merged into the work tree, and "missing file was restored"
3310 * if the work tree was updated. This function should be used by any operation
3311 * which merges changes into the work tree without updating the work tree.
3314 print_merge_progress_stats(struct got_update_progress_arg
*upa
)
3316 if (!upa
->did_something
)
3319 if (upa
->conflicts
> 0)
3320 printf("Files with new merge conflicts: %d\n", upa
->conflicts
);
3321 if (upa
->obstructed
> 0)
3322 printf("File paths obstructed by a non-regular file: %d\n",
3324 if (upa
->missing
> 0)
3325 printf("Files which had incoming changes but could not be "
3326 "found in the work tree: %d\n", upa
->missing
);
3327 if (upa
->not_deleted
> 0)
3328 printf("Files not deleted due to differences in deleted "
3329 "content: %d\n", upa
->not_deleted
);
3330 if (upa
->unversioned
> 0)
3331 printf("Files not merged because an unversioned file was "
3332 "found in the work tree: %d\n", upa
->unversioned
);
3338 fprintf(stderr
, "usage: %s update [-q] [-b branch] [-c commit] "
3339 "[path ...]\n", getprogname());
3343 static const struct got_error
*
3344 update_progress(void *arg
, unsigned char status
, const char *path
)
3346 struct got_update_progress_arg
*upa
= arg
;
3348 if (status
== GOT_STATUS_EXISTS
||
3349 status
== GOT_STATUS_BASE_REF_ERR
)
3352 upa
->did_something
= 1;
3354 /* Base commit bump happens silently. */
3355 if (status
== GOT_STATUS_BUMP_BASE
)
3358 if (status
== GOT_STATUS_CONFLICT
)
3360 if (status
== GOT_STATUS_OBSTRUCTED
)
3362 if (status
== GOT_STATUS_CANNOT_UPDATE
)
3364 if (status
== GOT_STATUS_MISSING
)
3366 if (status
== GOT_STATUS_CANNOT_DELETE
)
3368 if (status
== GOT_STATUS_UNVERSIONED
)
3371 while (path
[0] == '/')
3373 if (upa
->verbosity
>= 0)
3374 printf("%c %s\n", status
, path
);
3379 static const struct got_error
*
3380 switch_head_ref(struct got_reference
*head_ref
,
3381 struct got_object_id
*commit_id
, struct got_worktree
*worktree
,
3382 struct got_repository
*repo
)
3384 const struct got_error
*err
= NULL
;
3386 int ref_has_moved
= 0;
3388 /* Trivial case: switching between two different references. */
3389 if (strcmp(got_ref_get_name(head_ref
),
3390 got_worktree_get_head_ref_name(worktree
)) != 0) {
3391 printf("Switching work tree from %s to %s\n",
3392 got_worktree_get_head_ref_name(worktree
),
3393 got_ref_get_name(head_ref
));
3394 return got_worktree_set_head_ref(worktree
, head_ref
);
3397 err
= check_linear_ancestry(commit_id
,
3398 got_worktree_get_base_commit_id(worktree
), 0, repo
);
3400 if (err
->code
!= GOT_ERR_ANCESTRY
)
3407 /* Switching to a rebased branch with the same reference name. */
3408 err
= got_object_id_str(&base_id_str
,
3409 got_worktree_get_base_commit_id(worktree
));
3412 printf("Reference %s now points at a different branch\n",
3413 got_worktree_get_head_ref_name(worktree
));
3414 printf("Switching work tree from %s to %s\n", base_id_str
,
3415 got_worktree_get_head_ref_name(worktree
));
3419 static const struct got_error
*
3420 check_rebase_or_histedit_in_progress(struct got_worktree
*worktree
)
3422 const struct got_error
*err
;
3425 err
= got_worktree_rebase_in_progress(&in_progress
, worktree
);
3429 return got_error(GOT_ERR_REBASING
);
3431 err
= got_worktree_histedit_in_progress(&in_progress
, worktree
);
3435 return got_error(GOT_ERR_HISTEDIT_BUSY
);
3440 static const struct got_error
*
3441 check_merge_in_progress(struct got_worktree
*worktree
,
3442 struct got_repository
*repo
)
3444 const struct got_error
*err
;
3447 err
= got_worktree_merge_in_progress(&in_progress
, worktree
, repo
);
3451 return got_error(GOT_ERR_MERGE_BUSY
);
3456 static const struct got_error
*
3457 get_worktree_paths_from_argv(struct got_pathlist_head
*paths
, int argc
,
3458 char *argv
[], struct got_worktree
*worktree
)
3460 const struct got_error
*err
= NULL
;
3462 struct got_pathlist_entry
*new;
3468 return got_error_from_errno("strdup");
3469 return got_pathlist_append(paths
, path
, NULL
);
3472 for (i
= 0; i
< argc
; i
++) {
3473 err
= got_worktree_resolve_path(&path
, worktree
, argv
[i
]);
3476 err
= got_pathlist_insert(&new, paths
, path
, NULL
);
3477 if (err
|| new == NULL
/* duplicate */) {
3487 static const struct got_error
*
3488 wrap_not_worktree_error(const struct got_error
*orig_err
,
3489 const char *cmdname
, const char *path
)
3491 const struct got_error
*err
;
3492 struct got_repository
*repo
;
3493 static char msg
[512];
3494 int *pack_fds
= NULL
;
3496 err
= got_repo_pack_fds_open(&pack_fds
);
3500 err
= got_repo_open(&repo
, path
, NULL
, pack_fds
);
3504 snprintf(msg
, sizeof(msg
),
3505 "'got %s' needs a work tree in addition to a git repository\n"
3506 "Work trees can be checked out from this Git repository with "
3508 "The got(1) manual page contains more information.", cmdname
);
3509 err
= got_error_msg(GOT_ERR_NOT_WORKTREE
, msg
);
3511 const struct got_error
*close_err
= got_repo_close(repo
);
3516 const struct got_error
*pack_err
=
3517 got_repo_pack_fds_close(pack_fds
);
3524 static const struct got_error
*
3525 cmd_update(int argc
, char *argv
[])
3527 const struct got_error
*close_err
, *error
= NULL
;
3528 struct got_repository
*repo
= NULL
;
3529 struct got_worktree
*worktree
= NULL
;
3530 char *worktree_path
= NULL
;
3531 struct got_object_id
*commit_id
= NULL
;
3532 char *commit_id_str
= NULL
;
3533 const char *branch_name
= NULL
;
3534 struct got_reference
*head_ref
= NULL
;
3535 struct got_pathlist_head paths
;
3536 struct got_pathlist_entry
*pe
;
3537 int ch
, verbosity
= 0;
3538 struct got_update_progress_arg upa
;
3539 int *pack_fds
= NULL
;
3544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3545 "unveil", NULL
) == -1)
3549 while ((ch
= getopt(argc
, argv
, "b:c:q")) != -1) {
3552 branch_name
= optarg
;
3555 commit_id_str
= strdup(optarg
);
3556 if (commit_id_str
== NULL
)
3557 return got_error_from_errno("strdup");
3571 worktree_path
= getcwd(NULL
, 0);
3572 if (worktree_path
== NULL
) {
3573 error
= got_error_from_errno("getcwd");
3577 error
= got_repo_pack_fds_open(&pack_fds
);
3581 error
= got_worktree_open(&worktree
, worktree_path
,
3582 GOT_WORKTREE_GOT_DIR
);
3584 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
3585 error
= wrap_not_worktree_error(error
, "update",
3590 error
= check_rebase_or_histedit_in_progress(worktree
);
3594 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
3599 error
= apply_unveil(got_repo_get_path(repo
), 0,
3600 got_worktree_get_root_path(worktree
));
3604 error
= check_merge_in_progress(worktree
, repo
);
3608 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
3612 error
= got_ref_open(&head_ref
, repo
, branch_name
? branch_name
:
3613 got_worktree_get_head_ref_name(worktree
), 0);
3616 if (commit_id_str
== NULL
) {
3617 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
3620 error
= got_object_id_str(&commit_id_str
, commit_id
);
3624 struct got_reflist_head refs
;
3625 char *keyword_idstr
= NULL
;
3629 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
3634 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
3638 if (keyword_idstr
!= NULL
) {
3639 free(commit_id_str
);
3640 commit_id_str
= keyword_idstr
;
3643 error
= got_repo_match_object_id(&commit_id
, NULL
,
3644 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
3645 got_ref_list_free(&refs
);
3646 free(commit_id_str
);
3647 commit_id_str
= NULL
;
3650 error
= got_object_id_str(&commit_id_str
, commit_id
);
3656 struct got_object_id
*head_commit_id
;
3657 TAILQ_FOREACH(pe
, &paths
, entry
) {
3658 if (pe
->path_len
== 0)
3660 error
= got_error_msg(GOT_ERR_BAD_PATH
,
3661 "switching between branches requires that "
3662 "the entire work tree gets updated");
3665 error
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
3668 error
= check_linear_ancestry(commit_id
, head_commit_id
, 0,
3670 free(head_commit_id
);
3673 error
= check_same_branch(commit_id
, head_ref
, repo
);
3676 error
= switch_head_ref(head_ref
, commit_id
, worktree
, repo
);
3680 error
= check_linear_ancestry(commit_id
,
3681 got_worktree_get_base_commit_id(worktree
), 0, repo
);
3682 if (error
!= NULL
) {
3683 if (error
->code
== GOT_ERR_ANCESTRY
)
3684 error
= got_error(GOT_ERR_BRANCH_MOVED
);
3687 error
= check_same_branch(commit_id
, head_ref
, repo
);
3692 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree
),
3694 error
= got_worktree_set_base_commit_id(worktree
, repo
,
3700 memset(&upa
, 0, sizeof(upa
));
3701 upa
.verbosity
= verbosity
;
3702 error
= got_worktree_checkout_files(worktree
, &paths
, repo
,
3703 update_progress
, &upa
, check_cancelled
, NULL
);
3707 if (upa
.did_something
) {
3708 printf("Updated to %s: %s\n",
3709 got_worktree_get_head_ref_name(worktree
), commit_id_str
);
3711 printf("Already up-to-date\n");
3713 print_update_progress_stats(&upa
);
3716 const struct got_error
*pack_err
=
3717 got_repo_pack_fds_close(pack_fds
);
3722 close_err
= got_repo_close(repo
);
3726 if (worktree
!= NULL
) {
3727 close_err
= got_worktree_close(worktree
);
3731 if (head_ref
!= NULL
)
3732 got_ref_close(head_ref
);
3733 free(worktree_path
);
3734 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
3736 free(commit_id_str
);
3740 static const struct got_error
*
3741 diff_blobs(struct got_object_id
*blob_id1
, struct got_object_id
*blob_id2
,
3742 const char *path
, int diff_context
, int ignore_whitespace
,
3743 int force_text_diff
, struct got_diffstat_cb_arg
*dsa
,
3744 struct got_repository
*repo
, FILE *outfile
)
3746 const struct got_error
*err
= NULL
;
3747 struct got_blob_object
*blob1
= NULL
, *blob2
= NULL
;
3748 FILE *f1
= NULL
, *f2
= NULL
;
3749 int fd1
= -1, fd2
= -1;
3751 fd1
= got_opentempfd();
3753 return got_error_from_errno("got_opentempfd");
3754 fd2
= got_opentempfd();
3756 err
= got_error_from_errno("got_opentempfd");
3761 err
= got_object_open_as_blob(&blob1
, repo
, blob_id1
, 8192,
3767 err
= got_object_open_as_blob(&blob2
, repo
, blob_id2
, 8192, fd2
);
3771 f1
= got_opentemp();
3773 err
= got_error_from_errno("got_opentemp");
3776 f2
= got_opentemp();
3778 err
= got_error_from_errno("got_opentemp");
3782 while (path
[0] == '/')
3784 err
= got_diff_blob(NULL
, NULL
, blob1
, blob2
, f1
, f2
, path
, path
,
3785 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
, ignore_whitespace
,
3786 force_text_diff
, dsa
, outfile
);
3788 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3789 err
= got_error_from_errno("close");
3791 got_object_blob_close(blob1
);
3792 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3793 err
= got_error_from_errno("close");
3795 got_object_blob_close(blob2
);
3796 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3797 err
= got_error_from_errno("fclose");
3798 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3799 err
= got_error_from_errno("fclose");
3803 static const struct got_error
*
3804 diff_trees(struct got_object_id
*tree_id1
, struct got_object_id
*tree_id2
,
3805 const char *path
, int diff_context
, int ignore_whitespace
,
3806 int force_text_diff
, struct got_diffstat_cb_arg
*dsa
,
3807 struct got_repository
*repo
, FILE *outfile
)
3809 const struct got_error
*err
= NULL
;
3810 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3811 struct got_diff_blob_output_unidiff_arg arg
;
3812 FILE *f1
= NULL
, *f2
= NULL
;
3813 int fd1
= -1, fd2
= -1;
3816 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3819 fd1
= got_opentempfd();
3821 err
= got_error_from_errno("got_opentempfd");
3826 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3830 f1
= got_opentemp();
3832 err
= got_error_from_errno("got_opentemp");
3836 f2
= got_opentemp();
3838 err
= got_error_from_errno("got_opentemp");
3841 fd2
= got_opentempfd();
3843 err
= got_error_from_errno("got_opentempfd");
3846 arg
.diff_context
= diff_context
;
3847 arg
.ignore_whitespace
= ignore_whitespace
;
3848 arg
.force_text_diff
= force_text_diff
;
3850 arg
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
3851 arg
.outfile
= outfile
;
3854 while (path
[0] == '/')
3856 err
= got_diff_tree(tree1
, tree2
, f1
, f2
, fd1
, fd2
, path
, path
, repo
,
3857 got_diff_blob_output_unidiff
, &arg
, 1);
3860 got_object_tree_close(tree1
);
3862 got_object_tree_close(tree2
);
3863 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3864 err
= got_error_from_errno("fclose");
3865 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3866 err
= got_error_from_errno("fclose");
3867 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3868 err
= got_error_from_errno("close");
3869 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3870 err
= got_error_from_errno("close");
3874 static const struct got_error
*
3875 get_changed_paths(struct got_pathlist_head
*paths
,
3876 struct got_commit_object
*commit
, struct got_repository
*repo
,
3877 struct got_diffstat_cb_arg
*dsa
)
3879 const struct got_error
*err
= NULL
;
3880 struct got_object_id
*tree_id1
= NULL
, *tree_id2
= NULL
;
3881 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
3882 struct got_object_qid
*qid
;
3883 got_diff_blob_cb cb
= got_diff_tree_collect_changed_paths
;
3884 FILE *f1
= NULL
, *f2
= NULL
;
3885 int fd1
= -1, fd2
= -1;
3888 cb
= got_diff_tree_compute_diffstat
;
3890 f1
= got_opentemp();
3892 err
= got_error_from_errno("got_opentemp");
3895 f2
= got_opentemp();
3897 err
= got_error_from_errno("got_opentemp");
3900 fd1
= got_opentempfd();
3902 err
= got_error_from_errno("got_opentempfd");
3905 fd2
= got_opentempfd();
3907 err
= got_error_from_errno("got_opentempfd");
3912 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3914 struct got_commit_object
*pcommit
;
3915 err
= got_object_open_as_commit(&pcommit
, repo
,
3920 tree_id1
= got_object_id_dup(
3921 got_object_commit_get_tree_id(pcommit
));
3922 if (tree_id1
== NULL
) {
3923 got_object_commit_close(pcommit
);
3924 return got_error_from_errno("got_object_id_dup");
3926 got_object_commit_close(pcommit
);
3931 err
= got_object_open_as_tree(&tree1
, repo
, tree_id1
);
3936 tree_id2
= got_object_commit_get_tree_id(commit
);
3937 err
= got_object_open_as_tree(&tree2
, repo
, tree_id2
);
3941 err
= got_diff_tree(tree1
, tree2
, f1
, f2
, fd1
, fd2
, "", "", repo
,
3942 cb
, dsa
? (void *)dsa
: paths
, dsa
? 1 : 0);
3945 got_object_tree_close(tree1
);
3947 got_object_tree_close(tree2
);
3948 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
3949 err
= got_error_from_errno("close");
3950 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
3951 err
= got_error_from_errno("close");
3952 if (f1
&& fclose(f1
) == EOF
&& err
== NULL
)
3953 err
= got_error_from_errno("fclose");
3954 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
3955 err
= got_error_from_errno("fclose");
3960 static const struct got_error
*
3961 print_patch(struct got_commit_object
*commit
, struct got_object_id
*id
,
3962 const char *path
, int diff_context
, struct got_diffstat_cb_arg
*dsa
,
3963 struct got_repository
*repo
, FILE *outfile
)
3965 const struct got_error
*err
= NULL
;
3966 struct got_commit_object
*pcommit
= NULL
;
3967 char *id_str1
= NULL
, *id_str2
= NULL
;
3968 struct got_object_id
*obj_id1
= NULL
, *obj_id2
= NULL
;
3969 struct got_object_qid
*qid
;
3971 qid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
3973 err
= got_object_open_as_commit(&pcommit
, repo
,
3977 err
= got_object_id_str(&id_str1
, &qid
->id
);
3982 err
= got_object_id_str(&id_str2
, id
);
3986 if (path
&& path
[0] != '\0') {
3988 err
= got_object_id_by_path(&obj_id2
, repo
, commit
, path
);
3992 err
= got_object_id_by_path(&obj_id1
, repo
,
3995 if (err
->code
!= GOT_ERR_NO_TREE_ENTRY
) {
4001 err
= got_object_get_type(&obj_type
, repo
, obj_id2
);
4007 "diff %s %s\n", id_str1
? id_str1
: "/dev/null", id_str2
);
4008 fprintf(outfile
, "commit - %s\n",
4009 id_str1
? id_str1
: "/dev/null");
4010 fprintf(outfile
, "commit + %s\n", id_str2
);
4012 case GOT_OBJ_TYPE_BLOB
:
4013 err
= diff_blobs(obj_id1
, obj_id2
, path
, diff_context
,
4014 0, 0, dsa
, repo
, outfile
);
4016 case GOT_OBJ_TYPE_TREE
:
4017 err
= diff_trees(obj_id1
, obj_id2
, path
, diff_context
,
4018 0, 0, dsa
, repo
, outfile
);
4021 err
= got_error(GOT_ERR_OBJ_TYPE
);
4027 obj_id2
= got_object_commit_get_tree_id(commit
);
4029 obj_id1
= got_object_commit_get_tree_id(pcommit
);
4031 "diff %s %s\n", id_str1
? id_str1
: "/dev/null", id_str2
);
4032 fprintf(outfile
, "commit - %s\n",
4033 id_str1
? id_str1
: "/dev/null");
4034 fprintf(outfile
, "commit + %s\n", id_str2
);
4035 err
= diff_trees(obj_id1
, obj_id2
, "", diff_context
, 0, 0,
4036 dsa
, repo
, outfile
);
4042 got_object_commit_close(pcommit
);
4047 get_datestr(time_t *time
, char *datebuf
)
4049 struct tm mytm
, *tm
;
4052 tm
= gmtime_r(time
, &mytm
);
4055 s
= asctime_r(tm
, datebuf
);
4058 p
= strchr(s
, '\n');
4064 static const struct got_error
*
4065 match_commit(int *have_match
, struct got_object_id
*id
,
4066 struct got_commit_object
*commit
, regex_t
*regex
)
4068 const struct got_error
*err
= NULL
;
4069 regmatch_t regmatch
;
4070 char *id_str
= NULL
, *logmsg
= NULL
;
4074 err
= got_object_id_str(&id_str
, id
);
4078 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
4082 if (regexec(regex
, got_object_commit_get_author(commit
), 1,
4083 ®match
, 0) == 0 ||
4084 regexec(regex
, got_object_commit_get_committer(commit
), 1,
4085 ®match
, 0) == 0 ||
4086 regexec(regex
, id_str
, 1, ®match
, 0) == 0 ||
4087 regexec(regex
, logmsg
, 1, ®match
, 0) == 0)
4096 match_changed_paths(int *have_match
, struct got_pathlist_head
*changed_paths
,
4099 regmatch_t regmatch
;
4100 struct got_pathlist_entry
*pe
;
4104 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
4105 if (regexec(regex
, pe
->path
, 1, ®match
, 0) == 0) {
4112 static const struct got_error
*
4113 match_patch(int *have_match
, struct got_commit_object
*commit
,
4114 struct got_object_id
*id
, const char *path
, int diff_context
,
4115 struct got_repository
*repo
, regex_t
*regex
, FILE *f
)
4117 const struct got_error
*err
= NULL
;
4119 size_t linesize
= 0;
4120 regmatch_t regmatch
;
4124 err
= got_opentemp_truncate(f
);
4128 err
= print_patch(commit
, id
, path
, diff_context
, NULL
, repo
, f
);
4132 if (fseeko(f
, 0L, SEEK_SET
) == -1) {
4133 err
= got_error_from_errno("fseeko");
4137 while (getline(&line
, &linesize
, f
) != -1) {
4138 if (regexec(regex
, line
, 1, ®match
, 0) == 0) {
4148 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4150 static const struct got_error
*
4151 build_refs_str(char **refs_str
, struct got_reflist_head
*refs
,
4152 struct got_object_id
*id
, struct got_repository
*repo
,
4155 static const struct got_error
*err
= NULL
;
4156 struct got_reflist_entry
*re
;
4162 TAILQ_FOREACH(re
, refs
, entry
) {
4163 struct got_tag_object
*tag
= NULL
;
4164 struct got_object_id
*ref_id
;
4167 name
= got_ref_get_name(re
->ref
);
4168 if (strcmp(name
, GOT_REF_HEAD
) == 0)
4170 if (strncmp(name
, "refs/", 5) == 0)
4172 if (strncmp(name
, "got/", 4) == 0)
4174 if (strncmp(name
, "heads/", 6) == 0)
4176 if (strncmp(name
, "remotes/", 8) == 0) {
4180 s
= strstr(name
, "/" GOT_REF_HEAD
);
4181 if (s
!= NULL
&& strcmp(s
, "/" GOT_REF_HEAD
) == 0)
4184 err
= got_ref_resolve(&ref_id
, repo
, re
->ref
);
4187 if (strncmp(name
, "tags/", 5) == 0) {
4188 err
= got_object_open_as_tag(&tag
, repo
, ref_id
);
4190 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
4194 /* Ref points at something other than a tag. */
4199 cmp
= got_object_id_cmp(tag
?
4200 got_object_tag_get_object_id(tag
) : ref_id
, id
);
4203 got_object_tag_close(tag
);
4207 if (asprintf(refs_str
, "%s%s%s", s
? s
: "",
4208 s
? ", " : "", name
) == -1) {
4209 err
= got_error_from_errno("asprintf");
4220 static const struct got_error
*
4221 print_commit_oneline(struct got_commit_object
*commit
, struct got_object_id
*id
,
4222 struct got_repository
*repo
, struct got_reflist_object_id_map
*refs_idmap
)
4224 const struct got_error
*err
= NULL
;
4225 char *ref_str
= NULL
, *id_str
= NULL
, *logmsg0
= NULL
;
4226 char *comma
, *s
, *nl
;
4227 struct got_reflist_head
*refs
;
4228 char datebuf
[12]; /* YYYY-MM-DD + SPACE + NUL */
4230 time_t committer_time
;
4232 refs
= got_reflist_object_id_map_lookup(refs_idmap
, id
);
4234 err
= build_refs_str(&ref_str
, refs
, id
, repo
, 1);
4238 /* Display the first matching ref only. */
4239 if (ref_str
&& (comma
= strchr(ref_str
, ',')) != NULL
)
4243 if (ref_str
== NULL
) {
4244 err
= got_object_id_str(&id_str
, id
);
4249 committer_time
= got_object_commit_get_committer_time(commit
);
4250 if (gmtime_r(&committer_time
, &tm
) == NULL
) {
4251 err
= got_error_from_errno("gmtime_r");
4254 if (strftime(datebuf
, sizeof(datebuf
), "%F ", &tm
) == 0) {
4255 err
= got_error(GOT_ERR_NO_SPACE
);
4259 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
4264 while (isspace((unsigned char)s
[0]))
4267 nl
= strchr(s
, '\n');
4273 printf("%s%-7s %s\n", datebuf
, ref_str
, s
);
4275 printf("%s%.7s %s\n", datebuf
, id_str
, s
);
4277 if (fflush(stdout
) != 0 && err
== NULL
)
4278 err
= got_error_from_errno("fflush");
4286 static const struct got_error
*
4287 print_diffstat(struct got_diffstat_cb_arg
*dsa
, const char *header
)
4289 struct got_pathlist_entry
*pe
;
4292 printf("%s\n", header
);
4294 TAILQ_FOREACH(pe
, dsa
->paths
, entry
) {
4295 struct got_diff_changed_path
*cp
= pe
->data
;
4296 int pad
= dsa
->max_path_len
- pe
->path_len
+ 1;
4298 printf(" %c %s%*c | %*d+ %*d-\n", cp
->status
, pe
->path
, pad
,
4299 ' ', dsa
->add_cols
+ 1, cp
->add
, dsa
->rm_cols
+ 1, cp
->rm
);
4301 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4302 dsa
->nfiles
, dsa
->nfiles
> 1 ? "s" : "", dsa
->ins
,
4303 dsa
->ins
!= 1 ? "s" : "", dsa
->del
, dsa
->del
!= 1 ? "s" : "");
4305 if (fflush(stdout
) != 0)
4306 return got_error_from_errno("fflush");
4311 static const struct got_error
*
4317 if (fseeko(f
, 0L, SEEK_SET
) == -1)
4318 return got_error_from_errno("fseek");
4321 r
= fread(buf
, 1, sizeof(buf
), f
);
4324 return got_error_from_errno("fread");
4328 if (fwrite(buf
, 1, r
, stdout
) != r
)
4329 return got_ferror(stdout
, GOT_ERR_IO
);
4335 static const struct got_error
*
4336 print_commit(struct got_commit_object
*commit
, struct got_object_id
*id
,
4337 struct got_repository
*repo
, const char *path
,
4338 struct got_pathlist_head
*changed_paths
,
4339 struct got_diffstat_cb_arg
*diffstat
, int show_patch
, int diff_context
,
4340 struct got_reflist_object_id_map
*refs_idmap
, const char *custom_refs_str
,
4343 const struct got_error
*err
= NULL
;
4345 char *id_str
, *datestr
, *logmsg0
, *logmsg
, *line
;
4347 time_t committer_time
;
4348 const char *author
, *committer
;
4349 char *refs_str
= NULL
;
4351 err
= got_object_id_str(&id_str
, id
);
4355 if (custom_refs_str
== NULL
) {
4356 struct got_reflist_head
*refs
;
4357 refs
= got_reflist_object_id_map_lookup(refs_idmap
, id
);
4359 err
= build_refs_str(&refs_str
, refs
, id
, repo
, 0);
4365 printf(GOT_COMMIT_SEP_STR
);
4366 if (custom_refs_str
)
4367 printf("%s %s (%s)\n", prefix
? prefix
: "commit", id_str
,
4370 printf("%s %s%s%s%s\n", prefix
? prefix
: "commit", id_str
,
4371 refs_str
? " (" : "", refs_str
? refs_str
: "",
4372 refs_str
? ")" : "");
4377 printf("from: %s\n", got_object_commit_get_author(commit
));
4378 author
= got_object_commit_get_author(commit
);
4379 committer
= got_object_commit_get_committer(commit
);
4380 if (strcmp(author
, committer
) != 0)
4381 printf("via: %s\n", committer
);
4382 committer_time
= got_object_commit_get_committer_time(commit
);
4383 datestr
= get_datestr(&committer_time
, datebuf
);
4385 printf("date: %s UTC\n", datestr
);
4386 if (got_object_commit_get_nparents(commit
) > 1) {
4387 const struct got_object_id_queue
*parent_ids
;
4388 struct got_object_qid
*qid
;
4390 parent_ids
= got_object_commit_get_parent_ids(commit
);
4391 STAILQ_FOREACH(qid
, parent_ids
, entry
) {
4392 err
= got_object_id_str(&id_str
, &qid
->id
);
4395 printf("parent %d: %s\n", n
++, id_str
);
4401 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
4407 line
= strsep(&logmsg
, "\n");
4409 printf(" %s\n", line
);
4413 if (changed_paths
&& diffstat
== NULL
) {
4414 struct got_pathlist_entry
*pe
;
4416 TAILQ_FOREACH(pe
, changed_paths
, entry
) {
4417 struct got_diff_changed_path
*cp
= pe
->data
;
4419 printf(" %c %s\n", cp
->status
, pe
->path
);
4427 err
= got_error_from_errno("got_opentemp");
4432 err
= print_patch(commit
, id
, path
, diff_context
, diffstat
,
4433 repo
, diffstat
== NULL
? stdout
: f
);
4438 err
= print_diffstat(diffstat
, NULL
);
4450 if (fflush(stdout
) != 0 && err
== NULL
)
4451 err
= got_error_from_errno("fflush");
4453 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
4454 err
= got_error_from_errno("fclose");
4460 static const struct got_error
*
4461 print_commits(struct got_object_id
*root_id
, struct got_object_id
*end_id
,
4462 struct got_repository
*repo
, const char *path
, int show_changed_paths
,
4463 int show_diffstat
, int show_patch
, const char *search_pattern
,
4464 int diff_context
, int limit
, int log_branches
, int reverse_display_order
,
4465 struct got_reflist_object_id_map
*refs_idmap
, int one_line
, int toposort
,
4468 const struct got_error
*err
;
4469 struct got_commit_graph
*graph
;
4472 struct got_object_id_queue reversed_commits
;
4473 struct got_object_qid
*qid
;
4474 struct got_commit_object
*commit
;
4475 struct got_pathlist_head changed_paths
;
4477 STAILQ_INIT(&reversed_commits
);
4478 TAILQ_INIT(&changed_paths
);
4480 if (search_pattern
&& regcomp(®ex
, search_pattern
,
4481 REG_EXTENDED
| REG_NOSUB
| REG_NEWLINE
))
4482 return got_error_msg(GOT_ERR_REGEX
, search_pattern
);
4484 err
= got_commit_graph_open(&graph
, path
, !log_branches
);
4487 if (log_branches
&& toposort
) {
4488 err
= got_commit_graph_toposort(graph
, root_id
, repo
,
4489 check_cancelled
, NULL
);
4491 err
= got_commit_graph_bfsort(graph
, root_id
, repo
,
4492 check_cancelled
, NULL
);
4497 struct got_object_id id
;
4498 struct got_diffstat_cb_arg dsa
= { 0, 0, 0, 0, 0, 0,
4499 &changed_paths
, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE
};
4501 if (sigint_received
|| sigpipe_received
)
4504 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
4505 check_cancelled
, NULL
);
4507 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
4512 err
= got_object_open_as_commit(&commit
, repo
, &id
);
4516 if (((show_changed_paths
&& !show_diffstat
) ||
4517 (show_diffstat
&& !show_patch
))
4518 && !reverse_display_order
) {
4519 err
= get_changed_paths(&changed_paths
, commit
, repo
,
4520 show_diffstat
? &dsa
: NULL
);
4525 if (search_pattern
) {
4526 err
= match_commit(&have_match
, &id
, commit
, ®ex
);
4528 got_object_commit_close(commit
);
4531 if (have_match
== 0 && show_changed_paths
)
4532 match_changed_paths(&have_match
,
4533 &changed_paths
, ®ex
);
4534 if (have_match
== 0 && show_patch
) {
4535 err
= match_patch(&have_match
, commit
, &id
,
4536 path
, diff_context
, repo
, ®ex
, tmpfile
);
4540 if (have_match
== 0) {
4541 got_object_commit_close(commit
);
4542 got_pathlist_free(&changed_paths
,
4543 GOT_PATHLIST_FREE_ALL
);
4548 if (reverse_display_order
) {
4549 err
= got_object_qid_alloc(&qid
, &id
);
4552 STAILQ_INSERT_HEAD(&reversed_commits
, qid
, entry
);
4553 got_object_commit_close(commit
);
4556 err
= print_commit_oneline(commit
, &id
,
4559 err
= print_commit(commit
, &id
, repo
, path
,
4560 (show_changed_paths
|| show_diffstat
) ?
4561 &changed_paths
: NULL
,
4562 show_diffstat
? &dsa
: NULL
, show_patch
,
4563 diff_context
, refs_idmap
, NULL
, NULL
);
4564 got_object_commit_close(commit
);
4568 if ((limit
&& --limit
== 0) ||
4569 (end_id
&& got_object_id_cmp(&id
, end_id
) == 0))
4572 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
4574 if (reverse_display_order
) {
4575 STAILQ_FOREACH(qid
, &reversed_commits
, entry
) {
4576 struct got_diffstat_cb_arg dsa
= { 0, 0, 0, 0, 0, 0,
4577 &changed_paths
, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE
};
4579 err
= got_object_open_as_commit(&commit
, repo
,
4583 if ((show_changed_paths
&& !show_diffstat
) ||
4584 (show_diffstat
&& !show_patch
)) {
4585 err
= get_changed_paths(&changed_paths
, commit
,
4586 repo
, show_diffstat
? &dsa
: NULL
);
4591 err
= print_commit_oneline(commit
, &qid
->id
,
4594 err
= print_commit(commit
, &qid
->id
, repo
, path
,
4595 (show_changed_paths
|| show_diffstat
) ?
4596 &changed_paths
: NULL
,
4597 show_diffstat
? &dsa
: NULL
, show_patch
,
4598 diff_context
, refs_idmap
, NULL
, NULL
);
4599 got_object_commit_close(commit
);
4602 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
4606 while (!STAILQ_EMPTY(&reversed_commits
)) {
4607 qid
= STAILQ_FIRST(&reversed_commits
);
4608 STAILQ_REMOVE_HEAD(&reversed_commits
, entry
);
4609 got_object_qid_free(qid
);
4611 got_pathlist_free(&changed_paths
, GOT_PATHLIST_FREE_ALL
);
4614 got_commit_graph_close(graph
);
4621 fprintf(stderr
, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4622 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4623 "[path]\n", getprogname());
4628 get_default_log_limit(void)
4630 const char *got_default_log_limit
;
4634 got_default_log_limit
= getenv("GOT_LOG_DEFAULT_LIMIT");
4635 if (got_default_log_limit
== NULL
)
4637 n
= strtonum(got_default_log_limit
, 0, INT_MAX
, &errstr
);
4643 static const struct got_error
*
4644 cmd_log(int argc
, char *argv
[])
4646 const struct got_error
*error
;
4647 struct got_repository
*repo
= NULL
;
4648 struct got_worktree
*worktree
= NULL
;
4649 struct got_object_id
*start_id
= NULL
, *end_id
= NULL
;
4650 char *repo_path
= NULL
, *path
= NULL
, *cwd
= NULL
, *in_repo_path
= NULL
;
4651 char *keyword_idstr
= NULL
;
4652 const char *start_commit
= NULL
, *end_commit
= NULL
;
4653 const char *search_pattern
= NULL
;
4654 int diff_context
= -1, ch
;
4655 int show_changed_paths
= 0, show_patch
= 0, limit
= 0, log_branches
= 0;
4656 int show_diffstat
= 0, reverse_display_order
= 0, one_line
= 0;
4659 struct got_reflist_head refs
;
4660 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
4661 FILE *tmpfile
= NULL
;
4662 int *pack_fds
= NULL
;
4667 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4673 limit
= get_default_log_limit();
4675 while ((ch
= getopt(argc
, argv
, "bC:c:dl:PpRr:S:stx:")) != -1) {
4681 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
4684 errx(1, "number of context lines is %s: %s",
4688 start_commit
= optarg
;
4694 limit
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
4696 errx(1, "number of commits is %s: %s",
4700 show_changed_paths
= 1;
4706 reverse_display_order
= 1;
4709 repo_path
= realpath(optarg
, NULL
);
4710 if (repo_path
== NULL
)
4711 return got_error_from_errno2("realpath",
4713 got_path_strip_trailing_slashes(repo_path
);
4716 search_pattern
= optarg
;
4725 end_commit
= optarg
;
4736 if (diff_context
== -1)
4738 else if (!show_patch
)
4739 errx(1, "-C requires -p");
4741 if (one_line
&& (show_patch
|| show_changed_paths
|| show_diffstat
))
4742 errx(1, "cannot use -s with -d, -p or -P");
4744 cwd
= getcwd(NULL
, 0);
4746 error
= got_error_from_errno("getcwd");
4750 error
= got_repo_pack_fds_open(&pack_fds
);
4754 if (repo_path
== NULL
) {
4755 error
= got_worktree_open(&worktree
, cwd
,
4756 GOT_WORKTREE_GOT_DIR
);
4757 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
4764 error
= got_worktree_resolve_path(&path
, worktree
,
4769 path
= strdup(argv
[0]);
4771 error
= got_error_from_errno("strdup");
4775 } else if (argc
!= 0)
4778 if (repo_path
== NULL
) {
4779 repo_path
= worktree
?
4780 strdup(got_worktree_get_repo_path(worktree
)) : strdup(cwd
);
4782 if (repo_path
== NULL
) {
4783 error
= got_error_from_errno("strdup");
4787 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
4791 error
= apply_unveil(got_repo_get_path(repo
), 1,
4792 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
4796 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
4800 error
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
4804 if (start_commit
== NULL
) {
4805 struct got_reference
*head_ref
;
4806 struct got_commit_object
*commit
= NULL
;
4807 error
= got_ref_open(&head_ref
, repo
,
4808 worktree
? got_worktree_get_head_ref_name(worktree
)
4812 error
= got_ref_resolve(&start_id
, repo
, head_ref
);
4813 got_ref_close(head_ref
);
4816 error
= got_object_open_as_commit(&commit
, repo
,
4820 got_object_commit_close(commit
);
4822 error
= got_keyword_to_idstr(&keyword_idstr
, start_commit
,
4826 if (keyword_idstr
!= NULL
)
4827 start_commit
= keyword_idstr
;
4829 error
= got_repo_match_object_id(&start_id
, NULL
,
4830 start_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4834 if (end_commit
!= NULL
) {
4835 error
= got_keyword_to_idstr(&keyword_idstr
, end_commit
,
4839 if (keyword_idstr
!= NULL
)
4840 end_commit
= keyword_idstr
;
4842 error
= got_repo_match_object_id(&end_id
, NULL
,
4843 end_commit
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
4850 * If a path was specified on the command line it was resolved
4851 * to a path in the work tree above. Prepend the work tree's
4852 * path prefix to obtain the corresponding in-repository path.
4856 prefix
= got_worktree_get_path_prefix(worktree
);
4857 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
4858 (path
[0] != '\0') ? "/" : "", path
) == -1) {
4859 error
= got_error_from_errno("asprintf");
4864 error
= got_repo_map_path(&in_repo_path
, repo
,
4870 path
= in_repo_path
;
4874 /* Release work tree lock. */
4875 got_worktree_close(worktree
);
4879 if (search_pattern
&& show_patch
) {
4880 tmpfile
= got_opentemp();
4881 if (tmpfile
== NULL
) {
4882 error
= got_error_from_errno("got_opentemp");
4887 error
= print_commits(start_id
, end_id
, repo
, path
? path
: "",
4888 show_changed_paths
, show_diffstat
, show_patch
, search_pattern
,
4889 diff_context
, limit
, log_branches
, reverse_display_order
,
4890 refs_idmap
, one_line
, toposort
, tmpfile
);
4897 free(keyword_idstr
);
4899 got_worktree_close(worktree
);
4901 const struct got_error
*close_err
= got_repo_close(repo
);
4906 const struct got_error
*pack_err
=
4907 got_repo_pack_fds_close(pack_fds
);
4912 got_reflist_object_id_map_free(refs_idmap
);
4913 if (tmpfile
&& fclose(tmpfile
) == EOF
&& error
== NULL
)
4914 error
= got_error_from_errno("fclose");
4915 got_ref_list_free(&refs
);
4922 fprintf(stderr
, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4923 "[-r repository-path] [object1 object2 | path ...]\n",
4928 struct print_diff_arg
{
4929 struct got_repository
*repo
;
4930 struct got_worktree
*worktree
;
4931 struct got_diffstat_cb_arg
*diffstat
;
4936 enum got_diff_algorithm diff_algo
;
4937 int ignore_whitespace
;
4938 int force_text_diff
;
4945 * Create a file which contains the target path of a symlink so we can feed
4946 * it as content to the diff engine.
4948 static const struct got_error
*
4949 get_symlink_target_file(int *fd
, int dirfd
, const char *de_name
,
4950 const char *abspath
)
4952 const struct got_error
*err
= NULL
;
4953 char target_path
[PATH_MAX
];
4954 ssize_t target_len
, outlen
;
4959 target_len
= readlinkat(dirfd
, de_name
, target_path
, PATH_MAX
);
4960 if (target_len
== -1)
4961 return got_error_from_errno2("readlinkat", abspath
);
4963 target_len
= readlink(abspath
, target_path
, PATH_MAX
);
4964 if (target_len
== -1)
4965 return got_error_from_errno2("readlink", abspath
);
4968 *fd
= got_opentempfd();
4970 return got_error_from_errno("got_opentempfd");
4972 outlen
= write(*fd
, target_path
, target_len
);
4974 err
= got_error_from_errno("got_opentempfd");
4978 if (lseek(*fd
, 0, SEEK_SET
) == -1) {
4979 err
= got_error_from_errno2("lseek", abspath
);
4990 static const struct got_error
*
4991 print_diff(void *arg
, unsigned char status
, unsigned char staged_status
,
4992 const char *path
, struct got_object_id
*blob_id
,
4993 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
4994 int dirfd
, const char *de_name
)
4996 struct print_diff_arg
*a
= arg
;
4997 const struct got_error
*err
= NULL
;
4998 struct got_blob_object
*blob1
= NULL
;
4999 int fd
= -1, fd1
= -1, fd2
= -1;
5001 char *abspath
= NULL
, *label1
= NULL
;
5006 memset(&sb
, 0, sizeof(sb
));
5008 if (a
->diff_staged
) {
5009 if (staged_status
!= GOT_STATUS_MODIFY
&&
5010 staged_status
!= GOT_STATUS_ADD
&&
5011 staged_status
!= GOT_STATUS_DELETE
)
5014 if (staged_status
== GOT_STATUS_DELETE
)
5016 if (status
== GOT_STATUS_NONEXISTENT
)
5017 return got_error_set_errno(ENOENT
, path
);
5018 if (status
!= GOT_STATUS_MODIFY
&&
5019 status
!= GOT_STATUS_ADD
&&
5020 status
!= GOT_STATUS_DELETE
&&
5021 status
!= GOT_STATUS_CONFLICT
)
5025 err
= got_opentemp_truncate(a
->f1
);
5027 return got_error_from_errno("got_opentemp_truncate");
5028 err
= got_opentemp_truncate(a
->f2
);
5030 return got_error_from_errno("got_opentemp_truncate");
5032 if (!a
->header_shown
) {
5033 if (fprintf(a
->outfile
, "diff %s%s\n",
5034 a
->diff_staged
? "-s " : "",
5035 got_worktree_get_root_path(a
->worktree
)) < 0) {
5036 err
= got_error_from_errno("fprintf");
5039 if (fprintf(a
->outfile
, "commit - %s\n", a
->id_str
) < 0) {
5040 err
= got_error_from_errno("fprintf");
5043 if (fprintf(a
->outfile
, "path + %s%s\n",
5044 got_worktree_get_root_path(a
->worktree
),
5045 a
->diff_staged
? " (staged changes)" : "") < 0) {
5046 err
= got_error_from_errno("fprintf");
5049 a
->header_shown
= 1;
5052 if (a
->diff_staged
) {
5053 const char *label1
= NULL
, *label2
= NULL
;
5054 switch (staged_status
) {
5055 case GOT_STATUS_MODIFY
:
5059 case GOT_STATUS_ADD
:
5062 case GOT_STATUS_DELETE
:
5066 return got_error(GOT_ERR_FILE_STATUS
);
5068 fd1
= got_opentempfd();
5070 err
= got_error_from_errno("got_opentempfd");
5073 fd2
= got_opentempfd();
5075 err
= got_error_from_errno("got_opentempfd");
5078 err
= got_diff_objects_as_blobs(NULL
, NULL
, a
->f1
, a
->f2
,
5079 fd1
, fd2
, blob_id
, staged_blob_id
, label1
, label2
,
5080 a
->diff_algo
, a
->diff_context
, a
->ignore_whitespace
,
5081 a
->force_text_diff
, a
->diffstat
, a
->repo
, a
->outfile
);
5085 fd1
= got_opentempfd();
5087 err
= got_error_from_errno("got_opentempfd");
5091 if (staged_status
== GOT_STATUS_ADD
||
5092 staged_status
== GOT_STATUS_MODIFY
) {
5094 err
= got_object_open_as_blob(&blob1
, a
->repo
, staged_blob_id
,
5098 err
= got_object_id_str(&id_str
, staged_blob_id
);
5101 if (asprintf(&label1
, "%s (staged)", id_str
) == -1) {
5102 err
= got_error_from_errno("asprintf");
5107 } else if (status
!= GOT_STATUS_ADD
) {
5108 err
= got_object_open_as_blob(&blob1
, a
->repo
, blob_id
, 8192,
5114 if (status
!= GOT_STATUS_DELETE
) {
5115 if (asprintf(&abspath
, "%s/%s",
5116 got_worktree_get_root_path(a
->worktree
), path
) == -1) {
5117 err
= got_error_from_errno("asprintf");
5122 fd
= openat(dirfd
, de_name
,
5123 O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
5125 if (!got_err_open_nofollow_on_symlink()) {
5126 err
= got_error_from_errno2("openat",
5130 err
= get_symlink_target_file(&fd
, dirfd
,
5136 fd
= open(abspath
, O_RDONLY
| O_NOFOLLOW
| O_CLOEXEC
);
5138 if (!got_err_open_nofollow_on_symlink()) {
5139 err
= got_error_from_errno2("open",
5143 err
= get_symlink_target_file(&fd
, dirfd
,
5149 if (fstatat(fd
, abspath
, &sb
, AT_SYMLINK_NOFOLLOW
) == -1) {
5150 err
= got_error_from_errno2("fstatat", abspath
);
5153 f2
= fdopen(fd
, "r");
5155 err
= got_error_from_errno2("fdopen", abspath
);
5163 err
= got_object_blob_dump_to_file(&size1
, NULL
, NULL
,
5169 err
= got_diff_blob_file(blob1
, a
->f1
, size1
, label1
, f2
? f2
: a
->f2
,
5170 f2_exists
, &sb
, path
, GOT_DIFF_ALGORITHM_PATIENCE
, a
->diff_context
,
5171 a
->ignore_whitespace
, a
->force_text_diff
, a
->diffstat
, a
->outfile
);
5173 if (fd1
!= -1 && close(fd1
) == -1 && err
== NULL
)
5174 err
= got_error_from_errno("close");
5175 if (fd2
!= -1 && close(fd2
) == -1 && err
== NULL
)
5176 err
= got_error_from_errno("close");
5178 got_object_blob_close(blob1
);
5179 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
5180 err
= got_error_from_errno("close");
5181 if (f2
&& fclose(f2
) == EOF
&& err
== NULL
)
5182 err
= got_error_from_errno("fclose");
5187 static const struct got_error
*
5188 cmd_diff(int argc
, char *argv
[])
5190 const struct got_error
*error
;
5191 struct got_repository
*repo
= NULL
;
5192 struct got_worktree
*worktree
= NULL
;
5193 char *cwd
= NULL
, *repo_path
= NULL
;
5194 const char *commit_args
[2] = { NULL
, NULL
};
5195 int ncommit_args
= 0;
5196 struct got_object_id
*ids
[2] = { NULL
, NULL
};
5197 char *labels
[2] = { NULL
, NULL
};
5198 int type1
= GOT_OBJ_TYPE_ANY
, type2
= GOT_OBJ_TYPE_ANY
;
5199 int diff_context
= 3, diff_staged
= 0, ignore_whitespace
= 0, ch
, i
;
5200 int force_text_diff
= 0, force_path
= 0, rflag
= 0, show_diffstat
= 0;
5202 struct got_reflist_head refs
;
5203 struct got_pathlist_head diffstat_paths
, paths
;
5204 FILE *f1
= NULL
, *f2
= NULL
, *outfile
= NULL
;
5205 int fd1
= -1, fd2
= -1;
5206 int *pack_fds
= NULL
;
5207 struct got_diffstat_cb_arg dsa
;
5209 memset(&dsa
, 0, sizeof(dsa
));
5213 TAILQ_INIT(&diffstat_paths
);
5216 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5221 while ((ch
= getopt(argc
, argv
, "aC:c:dPr:sw")) != -1) {
5224 force_text_diff
= 1;
5227 diff_context
= strtonum(optarg
, 0, GOT_DIFF_MAX_CONTEXT
,
5230 errx(1, "number of context lines is %s: %s",
5234 if (ncommit_args
>= 2)
5235 errx(1, "too many -c options used");
5236 commit_args
[ncommit_args
++] = optarg
;
5245 repo_path
= realpath(optarg
, NULL
);
5246 if (repo_path
== NULL
)
5247 return got_error_from_errno2("realpath",
5249 got_path_strip_trailing_slashes(repo_path
);
5256 ignore_whitespace
= 1;
5267 cwd
= getcwd(NULL
, 0);
5269 error
= got_error_from_errno("getcwd");
5273 error
= got_repo_pack_fds_open(&pack_fds
);
5277 if (repo_path
== NULL
) {
5278 error
= got_worktree_open(&worktree
, cwd
,
5279 GOT_WORKTREE_GOT_DIR
);
5280 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5286 strdup(got_worktree_get_repo_path(worktree
));
5287 if (repo_path
== NULL
) {
5288 error
= got_error_from_errno("strdup");
5292 repo_path
= strdup(cwd
);
5293 if (repo_path
== NULL
) {
5294 error
= got_error_from_errno("strdup");
5300 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
5305 if (show_diffstat
) {
5306 dsa
.paths
= &diffstat_paths
;
5307 dsa
.force_text
= force_text_diff
;
5308 dsa
.ignore_ws
= ignore_whitespace
;
5309 dsa
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
5312 if (rflag
|| worktree
== NULL
|| ncommit_args
> 0) {
5314 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
5315 "-P option can only be used when diffing "
5320 error
= got_error_msg(GOT_ERR_NOT_IMPL
,
5321 "-s option can only be used when diffing "
5327 error
= apply_unveil(got_repo_get_path(repo
), 1,
5328 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
5332 if ((!force_path
&& argc
== 2) || ncommit_args
> 0) {
5333 int obj_type
= (ncommit_args
> 0 ?
5334 GOT_OBJ_TYPE_COMMIT
: GOT_OBJ_TYPE_ANY
);
5335 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5339 for (i
= 0; i
< (ncommit_args
> 0 ? ncommit_args
: argc
); i
++) {
5341 char *keyword_idstr
= NULL
;
5343 if (ncommit_args
> 0)
5344 arg
= commit_args
[i
];
5348 error
= got_keyword_to_idstr(&keyword_idstr
, arg
,
5352 if (keyword_idstr
!= NULL
)
5353 arg
= keyword_idstr
;
5355 error
= got_repo_match_object_id(&ids
[i
], &labels
[i
],
5356 arg
, obj_type
, &refs
, repo
);
5357 free(keyword_idstr
);
5359 if (error
->code
!= GOT_ERR_NOT_REF
&&
5360 error
->code
!= GOT_ERR_NO_OBJ
)
5362 if (ncommit_args
> 0)
5370 f1
= got_opentemp();
5372 error
= got_error_from_errno("got_opentemp");
5376 f2
= got_opentemp();
5378 error
= got_error_from_errno("got_opentemp");
5382 outfile
= got_opentemp();
5383 if (outfile
== NULL
) {
5384 error
= got_error_from_errno("got_opentemp");
5388 if (ncommit_args
== 0 && (ids
[0] == NULL
|| ids
[1] == NULL
)) {
5389 struct print_diff_arg arg
;
5392 if (worktree
== NULL
) {
5393 if (argc
== 2 && ids
[0] == NULL
) {
5394 error
= got_error_path(argv
[0], GOT_ERR_NO_OBJ
);
5396 } else if (argc
== 2 && ids
[1] == NULL
) {
5397 error
= got_error_path(argv
[1], GOT_ERR_NO_OBJ
);
5399 } else if (argc
> 0) {
5400 error
= got_error_fmt(GOT_ERR_NOT_WORKTREE
,
5401 "%s", "specified paths cannot be resolved");
5404 error
= got_error(GOT_ERR_NOT_WORKTREE
);
5409 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
5414 error
= got_object_id_str(&id_str
,
5415 got_worktree_get_base_commit_id(worktree
));
5419 arg
.worktree
= worktree
;
5420 arg
.diff_algo
= GOT_DIFF_ALGORITHM_PATIENCE
;
5421 arg
.diff_context
= diff_context
;
5422 arg
.id_str
= id_str
;
5423 arg
.header_shown
= 0;
5424 arg
.diff_staged
= diff_staged
;
5425 arg
.ignore_whitespace
= ignore_whitespace
;
5426 arg
.force_text_diff
= force_text_diff
;
5427 arg
.diffstat
= show_diffstat
? &dsa
: NULL
;
5430 arg
.outfile
= outfile
;
5432 error
= got_worktree_status(worktree
, &paths
, repo
, 0,
5433 print_diff
, &arg
, check_cancelled
, NULL
);
5438 if (show_diffstat
&& dsa
.nfiles
> 0) {
5441 if (asprintf(&header
, "diffstat %s%s",
5442 diff_staged
? "-s " : "",
5443 got_worktree_get_root_path(worktree
)) == -1) {
5444 error
= got_error_from_errno("asprintf");
5448 error
= print_diffstat(&dsa
, header
);
5454 error
= printfile(outfile
);
5458 if (ncommit_args
== 1) {
5459 struct got_commit_object
*commit
;
5460 error
= got_object_open_as_commit(&commit
, repo
, ids
[0]);
5464 labels
[1] = labels
[0];
5466 if (got_object_commit_get_nparents(commit
) > 0) {
5467 const struct got_object_id_queue
*pids
;
5468 struct got_object_qid
*pid
;
5469 pids
= got_object_commit_get_parent_ids(commit
);
5470 pid
= STAILQ_FIRST(pids
);
5471 ids
[0] = got_object_id_dup(&pid
->id
);
5472 if (ids
[0] == NULL
) {
5473 error
= got_error_from_errno(
5474 "got_object_id_dup");
5475 got_object_commit_close(commit
);
5478 error
= got_object_id_str(&labels
[0], ids
[0]);
5480 got_object_commit_close(commit
);
5485 labels
[0] = strdup("/dev/null");
5486 if (labels
[0] == NULL
) {
5487 error
= got_error_from_errno("strdup");
5488 got_object_commit_close(commit
);
5493 got_object_commit_close(commit
);
5496 if (ncommit_args
== 0 && argc
> 2) {
5497 error
= got_error_msg(GOT_ERR_BAD_PATH
,
5498 "path arguments cannot be used when diffing two objects");
5503 error
= got_object_get_type(&type1
, repo
, ids
[0]);
5508 error
= got_object_get_type(&type2
, repo
, ids
[1]);
5511 if (type1
!= GOT_OBJ_TYPE_ANY
&& type1
!= type2
) {
5512 error
= got_error(GOT_ERR_OBJ_TYPE
);
5515 if (type1
== GOT_OBJ_TYPE_BLOB
&& argc
> 2) {
5516 error
= got_error_msg(GOT_ERR_OBJ_TYPE
,
5517 "path arguments cannot be used when diffing blobs");
5521 for (i
= 0; ncommit_args
> 0 && i
< argc
; i
++) {
5523 struct got_pathlist_entry
*new;
5527 error
= got_worktree_resolve_path(&p
, worktree
,
5531 prefix
= got_worktree_get_path_prefix(worktree
);
5532 while (prefix
[0] == '/')
5534 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5535 (p
[0] != '\0' && prefix
[0] != '\0') ? "/" : "",
5537 error
= got_error_from_errno("asprintf");
5543 char *mapped_path
, *s
;
5544 error
= got_repo_map_path(&mapped_path
, repo
, argv
[i
]);
5550 in_repo_path
= strdup(s
);
5551 if (in_repo_path
== NULL
) {
5552 error
= got_error_from_errno("asprintf");
5559 error
= got_pathlist_insert(&new, &paths
, in_repo_path
, NULL
);
5560 if (error
|| new == NULL
/* duplicate */)
5567 /* Release work tree lock. */
5568 got_worktree_close(worktree
);
5572 fd1
= got_opentempfd();
5574 error
= got_error_from_errno("got_opentempfd");
5578 fd2
= got_opentempfd();
5580 error
= got_error_from_errno("got_opentempfd");
5584 switch (type1
== GOT_OBJ_TYPE_ANY
? type2
: type1
) {
5585 case GOT_OBJ_TYPE_BLOB
:
5586 error
= got_diff_objects_as_blobs(NULL
, NULL
, f1
, f2
,
5587 fd1
, fd2
, ids
[0], ids
[1], NULL
, NULL
,
5588 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
5589 ignore_whitespace
, force_text_diff
,
5590 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
5592 case GOT_OBJ_TYPE_TREE
:
5593 error
= got_diff_objects_as_trees(NULL
, NULL
, f1
, f2
, fd1
, fd2
,
5594 ids
[0], ids
[1], &paths
, "", "",
5595 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
5596 ignore_whitespace
, force_text_diff
,
5597 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
5599 case GOT_OBJ_TYPE_COMMIT
:
5600 fprintf(outfile
, "diff %s %s\n", labels
[0], labels
[1]);
5601 error
= got_diff_objects_as_commits(NULL
, NULL
, f1
, f2
,
5602 fd1
, fd2
, ids
[0], ids
[1], &paths
,
5603 GOT_DIFF_ALGORITHM_PATIENCE
, diff_context
,
5604 ignore_whitespace
, force_text_diff
,
5605 show_diffstat
? &dsa
: NULL
, repo
, outfile
);
5608 error
= got_error(GOT_ERR_OBJ_TYPE
);
5613 if (show_diffstat
&& dsa
.nfiles
> 0) {
5614 char *header
= NULL
;
5616 if (asprintf(&header
, "diffstat %s %s",
5617 labels
[0], labels
[1]) == -1) {
5618 error
= got_error_from_errno("asprintf");
5622 error
= print_diffstat(&dsa
, header
);
5628 error
= printfile(outfile
);
5636 got_worktree_close(worktree
);
5638 const struct got_error
*close_err
= got_repo_close(repo
);
5643 const struct got_error
*pack_err
=
5644 got_repo_pack_fds_close(pack_fds
);
5648 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
5649 got_pathlist_free(&diffstat_paths
, GOT_PATHLIST_FREE_ALL
);
5650 got_ref_list_free(&refs
);
5651 if (outfile
&& fclose(outfile
) == EOF
&& error
== NULL
)
5652 error
= got_error_from_errno("fclose");
5653 if (f1
&& fclose(f1
) == EOF
&& error
== NULL
)
5654 error
= got_error_from_errno("fclose");
5655 if (f2
&& fclose(f2
) == EOF
&& error
== NULL
)
5656 error
= got_error_from_errno("fclose");
5657 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
5658 error
= got_error_from_errno("close");
5659 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
5660 error
= got_error_from_errno("close");
5668 "usage: %s blame [-c commit] [-r repository-path] path\n",
5677 char datebuf
[11]; /* YYYY-MM-DD + NUL */
5680 struct blame_cb_args
{
5681 struct blame_line
*lines
;
5685 off_t
*line_offsets
;
5687 struct got_repository
*repo
;
5690 static const struct got_error
*
5691 blame_cb(void *arg
, int nlines
, int lineno
,
5692 struct got_commit_object
*commit
, struct got_object_id
*id
)
5694 const struct got_error
*err
= NULL
;
5695 struct blame_cb_args
*a
= arg
;
5696 struct blame_line
*bline
;
5698 size_t linesize
= 0;
5701 time_t committer_time
;
5703 if (nlines
!= a
->nlines
||
5704 (lineno
!= -1 && lineno
< 1) || lineno
> a
->nlines
)
5705 return got_error(GOT_ERR_RANGE
);
5707 if (sigint_received
)
5708 return got_error(GOT_ERR_ITER_COMPLETED
);
5711 return NULL
; /* no change in this commit */
5713 /* Annotate this line. */
5714 bline
= &a
->lines
[lineno
- 1];
5715 if (bline
->annotated
)
5717 err
= got_object_id_str(&bline
->id_str
, id
);
5721 bline
->committer
= strdup(got_object_commit_get_committer(commit
));
5722 if (bline
->committer
== NULL
) {
5723 err
= got_error_from_errno("strdup");
5727 committer_time
= got_object_commit_get_committer_time(commit
);
5728 if (gmtime_r(&committer_time
, &tm
) == NULL
)
5729 return got_error_from_errno("gmtime_r");
5730 if (strftime(bline
->datebuf
, sizeof(bline
->datebuf
), "%F", &tm
) == 0) {
5731 err
= got_error(GOT_ERR_NO_SPACE
);
5734 bline
->annotated
= 1;
5736 /* Print lines annotated so far. */
5737 bline
= &a
->lines
[a
->lineno_cur
- 1];
5738 if (!bline
->annotated
)
5741 offset
= a
->line_offsets
[a
->lineno_cur
- 1];
5742 if (fseeko(a
->f
, offset
, SEEK_SET
) == -1) {
5743 err
= got_error_from_errno("fseeko");
5747 while (a
->lineno_cur
<= a
->nlines
&& bline
->annotated
) {
5748 char *smallerthan
, *at
, *nl
, *committer
;
5751 if (getline(&line
, &linesize
, a
->f
) == -1) {
5753 err
= got_error_from_errno("getline");
5757 committer
= bline
->committer
;
5758 smallerthan
= strchr(committer
, '<');
5759 if (smallerthan
&& smallerthan
[1] != '\0')
5760 committer
= smallerthan
+ 1;
5761 at
= strchr(committer
, '@');
5764 len
= strlen(committer
);
5766 committer
[8] = '\0';
5768 nl
= strchr(line
, '\n');
5771 printf("%.*d) %.8s %s %-8s %s\n", a
->nlines_prec
, a
->lineno_cur
,
5772 bline
->id_str
, bline
->datebuf
, committer
, line
);
5775 bline
= &a
->lines
[a
->lineno_cur
- 1];
5782 static const struct got_error
*
5783 cmd_blame(int argc
, char *argv
[])
5785 const struct got_error
*error
;
5786 struct got_repository
*repo
= NULL
;
5787 struct got_worktree
*worktree
= NULL
;
5788 char *path
, *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
5789 char *link_target
= NULL
;
5790 struct got_object_id
*obj_id
= NULL
;
5791 struct got_object_id
*commit_id
= NULL
;
5792 struct got_commit_object
*commit
= NULL
;
5793 struct got_blob_object
*blob
= NULL
;
5794 char *commit_id_str
= NULL
, *keyword_idstr
= NULL
;
5795 struct blame_cb_args bca
;
5796 int ch
, obj_type
, i
, fd1
= -1, fd2
= -1, fd3
= -1;
5798 int *pack_fds
= NULL
;
5799 FILE *f1
= NULL
, *f2
= NULL
;
5801 fd1
= got_opentempfd();
5803 return got_error_from_errno("got_opentempfd");
5805 memset(&bca
, 0, sizeof(bca
));
5808 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5813 while ((ch
= getopt(argc
, argv
, "c:r:")) != -1) {
5816 commit_id_str
= optarg
;
5819 repo_path
= realpath(optarg
, NULL
);
5820 if (repo_path
== NULL
)
5821 return got_error_from_errno2("realpath",
5823 got_path_strip_trailing_slashes(repo_path
);
5839 cwd
= getcwd(NULL
, 0);
5841 error
= got_error_from_errno("getcwd");
5845 error
= got_repo_pack_fds_open(&pack_fds
);
5849 if (repo_path
== NULL
) {
5850 error
= got_worktree_open(&worktree
, cwd
,
5851 GOT_WORKTREE_GOT_DIR
);
5852 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
5858 strdup(got_worktree_get_repo_path(worktree
));
5859 if (repo_path
== NULL
) {
5860 error
= got_error_from_errno("strdup");
5865 repo_path
= strdup(cwd
);
5866 if (repo_path
== NULL
) {
5867 error
= got_error_from_errno("strdup");
5873 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
5878 const char *prefix
= got_worktree_get_path_prefix(worktree
);
5881 error
= got_worktree_resolve_path(&p
, worktree
, path
);
5884 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
5885 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
5887 error
= got_error_from_errno("asprintf");
5892 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5894 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
5897 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
5902 if (commit_id_str
== NULL
) {
5903 struct got_reference
*head_ref
;
5904 error
= got_ref_open(&head_ref
, repo
, worktree
?
5905 got_worktree_get_head_ref_name(worktree
) : GOT_REF_HEAD
, 0);
5908 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
5909 got_ref_close(head_ref
);
5913 struct got_reflist_head refs
;
5916 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
5921 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
5925 if (keyword_idstr
!= NULL
)
5926 commit_id_str
= keyword_idstr
;
5928 error
= got_repo_match_object_id(&commit_id
, NULL
,
5929 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
5930 got_ref_list_free(&refs
);
5936 /* Release work tree lock. */
5937 got_worktree_close(worktree
);
5941 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
5945 error
= got_object_resolve_symlinks(&link_target
, in_repo_path
,
5950 error
= got_object_id_by_path(&obj_id
, repo
, commit
,
5951 link_target
? link_target
: in_repo_path
);
5955 error
= got_object_get_type(&obj_type
, repo
, obj_id
);
5959 if (obj_type
!= GOT_OBJ_TYPE_BLOB
) {
5960 error
= got_error_path(link_target
? link_target
: in_repo_path
,
5965 error
= got_object_open_as_blob(&blob
, repo
, obj_id
, 8192, fd1
);
5968 bca
.f
= got_opentemp();
5969 if (bca
.f
== NULL
) {
5970 error
= got_error_from_errno("got_opentemp");
5973 error
= got_object_blob_dump_to_file(&filesize
, &bca
.nlines
,
5974 &bca
.line_offsets
, bca
.f
, blob
);
5975 if (error
|| bca
.nlines
== 0)
5978 /* Don't include \n at EOF in the blame line count. */
5979 if (bca
.line_offsets
[bca
.nlines
- 1] == filesize
)
5982 bca
.lines
= calloc(bca
.nlines
, sizeof(*bca
.lines
));
5983 if (bca
.lines
== NULL
) {
5984 error
= got_error_from_errno("calloc");
5988 bca
.nlines_prec
= 0;
5996 fd2
= got_opentempfd();
5998 error
= got_error_from_errno("got_opentempfd");
6001 fd3
= got_opentempfd();
6003 error
= got_error_from_errno("got_opentempfd");
6006 f1
= got_opentemp();
6008 error
= got_error_from_errno("got_opentemp");
6011 f2
= got_opentemp();
6013 error
= got_error_from_errno("got_opentemp");
6016 error
= got_blame(link_target
? link_target
: in_repo_path
, commit_id
,
6017 repo
, GOT_DIFF_ALGORITHM_PATIENCE
, blame_cb
, &bca
,
6018 check_cancelled
, NULL
, fd2
, fd3
, f1
, f2
);
6020 free(keyword_idstr
);
6028 got_object_commit_close(commit
);
6030 if (fd1
!= -1 && close(fd1
) == -1 && error
== NULL
)
6031 error
= got_error_from_errno("close");
6032 if (fd2
!= -1 && close(fd2
) == -1 && error
== NULL
)
6033 error
= got_error_from_errno("close");
6034 if (fd3
!= -1 && close(fd3
) == -1 && error
== NULL
)
6035 error
= got_error_from_errno("close");
6036 if (f1
&& fclose(f1
) == EOF
&& error
== NULL
)
6037 error
= got_error_from_errno("fclose");
6038 if (f2
&& fclose(f2
) == EOF
&& error
== NULL
)
6039 error
= got_error_from_errno("fclose");
6042 got_object_blob_close(blob
);
6044 got_worktree_close(worktree
);
6046 const struct got_error
*close_err
= got_repo_close(repo
);
6051 const struct got_error
*pack_err
=
6052 got_repo_pack_fds_close(pack_fds
);
6057 for (i
= 0; i
< bca
.nlines
; i
++) {
6058 struct blame_line
*bline
= &bca
.lines
[i
];
6059 free(bline
->id_str
);
6060 free(bline
->committer
);
6064 free(bca
.line_offsets
);
6065 if (bca
.f
&& fclose(bca
.f
) == EOF
&& error
== NULL
)
6066 error
= got_error_from_errno("fclose");
6073 fprintf(stderr
, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6074 "[path]\n", getprogname());
6078 static const struct got_error
*
6079 print_entry(struct got_tree_entry
*te
, const char *id
, const char *path
,
6080 const char *root_path
, struct got_repository
*repo
)
6082 const struct got_error
*err
= NULL
;
6083 int is_root_path
= (strcmp(path
, root_path
) == 0);
6084 const char *modestr
= "";
6085 mode_t mode
= got_tree_entry_get_mode(te
);
6086 char *link_target
= NULL
;
6088 path
+= strlen(root_path
);
6089 while (path
[0] == '/')
6092 if (got_object_tree_entry_is_submodule(te
))
6094 else if (S_ISLNK(mode
)) {
6097 err
= got_tree_entry_get_symlink_target(&link_target
, te
, repo
);
6100 for (i
= 0; link_target
[i
] != '\0'; i
++) {
6101 if (!isprint((unsigned char)link_target
[i
]))
6102 link_target
[i
] = '?';
6107 else if (S_ISDIR(mode
))
6109 else if (mode
& S_IXUSR
)
6112 printf("%s%s%s%s%s%s%s\n", id
? id
: "", path
,
6113 is_root_path
? "" : "/", got_tree_entry_get_name(te
), modestr
,
6114 link_target
? " -> ": "", link_target
? link_target
: "");
6120 static const struct got_error
*
6121 print_tree(const char *path
, struct got_commit_object
*commit
,
6122 int show_ids
, int recurse
, const char *root_path
,
6123 struct got_repository
*repo
)
6125 const struct got_error
*err
= NULL
;
6126 struct got_object_id
*tree_id
= NULL
;
6127 struct got_tree_object
*tree
= NULL
;
6130 err
= got_object_id_by_path(&tree_id
, repo
, commit
, path
);
6134 err
= got_object_open_as_tree(&tree
, repo
, tree_id
);
6137 nentries
= got_object_tree_get_nentries(tree
);
6138 for (i
= 0; i
< nentries
; i
++) {
6139 struct got_tree_entry
*te
;
6142 if (sigint_received
|| sigpipe_received
)
6145 te
= got_object_tree_get_entry(tree
, i
);
6148 err
= got_object_id_str(&id_str
,
6149 got_tree_entry_get_id(te
));
6152 if (asprintf(&id
, "%s ", id_str
) == -1) {
6153 err
= got_error_from_errno("asprintf");
6159 err
= print_entry(te
, id
, path
, root_path
, repo
);
6164 if (recurse
&& S_ISDIR(got_tree_entry_get_mode(te
))) {
6166 if (asprintf(&child_path
, "%s%s%s", path
,
6167 path
[0] == '/' && path
[1] == '\0' ? "" : "/",
6168 got_tree_entry_get_name(te
)) == -1) {
6169 err
= got_error_from_errno("asprintf");
6172 err
= print_tree(child_path
, commit
, show_ids
, 1,
6181 got_object_tree_close(tree
);
6186 static const struct got_error
*
6187 cmd_tree(int argc
, char *argv
[])
6189 const struct got_error
*error
;
6190 struct got_repository
*repo
= NULL
;
6191 struct got_worktree
*worktree
= NULL
;
6192 const char *path
, *refname
= NULL
;
6193 char *cwd
= NULL
, *repo_path
= NULL
, *in_repo_path
= NULL
;
6194 struct got_object_id
*commit_id
= NULL
;
6195 struct got_commit_object
*commit
= NULL
;
6196 char *commit_id_str
= NULL
, *keyword_idstr
= NULL
;
6197 int show_ids
= 0, recurse
= 0;
6199 int *pack_fds
= NULL
;
6202 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6207 while ((ch
= getopt(argc
, argv
, "c:iRr:")) != -1) {
6210 commit_id_str
= optarg
;
6219 repo_path
= realpath(optarg
, NULL
);
6220 if (repo_path
== NULL
)
6221 return got_error_from_errno2("realpath",
6223 got_path_strip_trailing_slashes(repo_path
);
6241 cwd
= getcwd(NULL
, 0);
6243 error
= got_error_from_errno("getcwd");
6247 error
= got_repo_pack_fds_open(&pack_fds
);
6251 if (repo_path
== NULL
) {
6252 error
= got_worktree_open(&worktree
, cwd
,
6253 GOT_WORKTREE_GOT_DIR
);
6254 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6260 strdup(got_worktree_get_repo_path(worktree
));
6261 if (repo_path
== NULL
)
6262 error
= got_error_from_errno("strdup");
6266 repo_path
= strdup(cwd
);
6267 if (repo_path
== NULL
) {
6268 error
= got_error_from_errno("strdup");
6274 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
6279 const char *prefix
= got_worktree_get_path_prefix(worktree
);
6282 if (path
== NULL
|| got_path_is_root_dir(path
))
6284 error
= got_worktree_resolve_path(&p
, worktree
, path
);
6287 if (asprintf(&in_repo_path
, "%s%s%s", prefix
,
6288 (p
[0] != '\0' && !got_path_is_root_dir(prefix
)) ? "/" : "",
6290 error
= got_error_from_errno("asprintf");
6295 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
6299 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
6304 error
= got_repo_map_path(&in_repo_path
, repo
, path
);
6309 if (commit_id_str
== NULL
) {
6310 struct got_reference
*head_ref
;
6312 refname
= got_worktree_get_head_ref_name(worktree
);
6314 refname
= GOT_REF_HEAD
;
6315 error
= got_ref_open(&head_ref
, repo
, refname
, 0);
6318 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
6319 got_ref_close(head_ref
);
6323 struct got_reflist_head refs
;
6326 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
6331 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
6335 if (keyword_idstr
!= NULL
)
6336 commit_id_str
= keyword_idstr
;
6338 error
= got_repo_match_object_id(&commit_id
, NULL
,
6339 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
6340 got_ref_list_free(&refs
);
6346 /* Release work tree lock. */
6347 got_worktree_close(worktree
);
6351 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
6355 error
= print_tree(in_repo_path
, commit
, show_ids
, recurse
,
6356 in_repo_path
, repo
);
6358 free(keyword_idstr
);
6364 got_object_commit_close(commit
);
6366 got_worktree_close(worktree
);
6368 const struct got_error
*close_err
= got_repo_close(repo
);
6373 const struct got_error
*pack_err
=
6374 got_repo_pack_fds_close(pack_fds
);
6384 fprintf(stderr
, "usage: %s status [-I] [-S status-codes] "
6385 "[-s status-codes] [path ...]\n", getprogname());
6389 struct got_status_arg
{
6394 static const struct got_error
*
6395 print_status(void *arg
, unsigned char status
, unsigned char staged_status
,
6396 const char *path
, struct got_object_id
*blob_id
,
6397 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
6398 int dirfd
, const char *de_name
)
6400 struct got_status_arg
*st
= arg
;
6402 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
6403 status
= GOT_STATUS_NO_CHANGE
;
6404 if (st
!= NULL
&& st
->status_codes
) {
6405 size_t ncodes
= strlen(st
->status_codes
);
6408 for (i
= 0; i
< ncodes
; i
++) {
6410 if (status
== st
->status_codes
[i
] ||
6411 staged_status
== st
->status_codes
[i
]) {
6416 if (status
== st
->status_codes
[i
] ||
6417 staged_status
== st
->status_codes
[i
])
6422 if (st
->suppress
&& j
== 0)
6429 printf("%c%c %s\n", status
, staged_status
, path
);
6433 static const struct got_error
*
6434 show_operation_in_progress(struct got_worktree
*worktree
,
6435 struct got_repository
*repo
)
6437 const struct got_error
*err
;
6438 char *new_base_branch_name
= NULL
;
6439 char *branch_name
= NULL
;
6440 int rebase_in_progress
, histedit_in_progress
, merge_in_progress
;
6442 err
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
6445 if (rebase_in_progress
) {
6446 err
= got_worktree_rebase_info(&new_base_branch_name
,
6447 &branch_name
, worktree
, repo
);
6450 printf("Work tree is rebasing %s onto %s\n",
6451 branch_name
, new_base_branch_name
);
6454 err
= got_worktree_histedit_in_progress(&histedit_in_progress
,
6458 if (histedit_in_progress
) {
6459 err
= got_worktree_histedit_info(&branch_name
, worktree
, repo
);
6462 printf("Work tree is editing the history of %s\n", branch_name
);
6465 err
= got_worktree_merge_in_progress(&merge_in_progress
,
6469 if (merge_in_progress
) {
6470 err
= got_worktree_merge_info(&branch_name
, worktree
,
6474 printf("Work tree is merging %s into %s\n", branch_name
,
6475 got_worktree_get_head_ref_name(worktree
));
6478 free(new_base_branch_name
);
6483 static const struct got_error
*
6484 cmd_status(int argc
, char *argv
[])
6486 const struct got_error
*close_err
, *error
= NULL
;
6487 struct got_repository
*repo
= NULL
;
6488 struct got_worktree
*worktree
= NULL
;
6489 struct got_status_arg st
;
6491 struct got_pathlist_head paths
;
6492 int ch
, i
, no_ignores
= 0;
6493 int *pack_fds
= NULL
;
6497 memset(&st
, 0, sizeof(st
));
6498 st
.status_codes
= NULL
;
6502 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6507 while ((ch
= getopt(argc
, argv
, "IS:s:")) != -1) {
6513 if (st
.status_codes
!= NULL
&& st
.suppress
== 0)
6514 option_conflict('S', 's');
6518 for (i
= 0; optarg
[i
] != '\0'; i
++) {
6519 switch (optarg
[i
]) {
6520 case GOT_STATUS_MODIFY
:
6521 case GOT_STATUS_ADD
:
6522 case GOT_STATUS_DELETE
:
6523 case GOT_STATUS_CONFLICT
:
6524 case GOT_STATUS_MISSING
:
6525 case GOT_STATUS_OBSTRUCTED
:
6526 case GOT_STATUS_UNVERSIONED
:
6527 case GOT_STATUS_MODE_CHANGE
:
6528 case GOT_STATUS_NONEXISTENT
:
6531 errx(1, "invalid status code '%c'",
6535 if (ch
== 's' && st
.suppress
)
6536 option_conflict('s', 'S');
6537 st
.status_codes
= optarg
;
6548 cwd
= getcwd(NULL
, 0);
6550 error
= got_error_from_errno("getcwd");
6554 error
= got_repo_pack_fds_open(&pack_fds
);
6558 error
= got_worktree_open(&worktree
, cwd
,
6559 GOT_WORKTREE_GOT_DIR
);
6561 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
6562 error
= wrap_not_worktree_error(error
, "status", cwd
);
6566 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
6571 error
= apply_unveil(got_repo_get_path(repo
), 1,
6572 got_worktree_get_root_path(worktree
));
6576 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
6580 error
= got_worktree_status(worktree
, &paths
, repo
, no_ignores
,
6581 print_status
, &st
, check_cancelled
, NULL
);
6585 error
= show_operation_in_progress(worktree
, repo
);
6588 const struct got_error
*pack_err
=
6589 got_repo_pack_fds_close(pack_fds
);
6594 close_err
= got_repo_close(repo
);
6598 if (worktree
!= NULL
) {
6599 close_err
= got_worktree_close(worktree
);
6604 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
6612 fprintf(stderr
, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6613 "[-s reference] [name]\n", getprogname());
6617 static const struct got_error
*
6618 list_refs(struct got_repository
*repo
, const char *refname
, int sort_by_time
)
6620 static const struct got_error
*err
= NULL
;
6621 struct got_reflist_head refs
;
6622 struct got_reflist_entry
*re
;
6625 err
= got_ref_list(&refs
, repo
, refname
, sort_by_time
?
6626 got_ref_cmp_by_commit_timestamp_descending
: got_ref_cmp_by_name
,
6631 TAILQ_FOREACH(re
, &refs
, entry
) {
6633 refstr
= got_ref_to_str(re
->ref
);
6634 if (refstr
== NULL
) {
6635 err
= got_error_from_errno("got_ref_to_str");
6638 printf("%s: %s\n", got_ref_get_name(re
->ref
), refstr
);
6642 got_ref_list_free(&refs
);
6646 static const struct got_error
*
6647 delete_ref_by_name(struct got_repository
*repo
, const char *refname
)
6649 const struct got_error
*err
;
6650 struct got_reference
*ref
;
6652 err
= got_ref_open(&ref
, repo
, refname
, 0);
6656 err
= delete_ref(repo
, ref
);
6661 static const struct got_error
*
6662 add_ref(struct got_repository
*repo
, const char *refname
, const char *target
)
6664 const struct got_error
*err
= NULL
;
6665 struct got_object_id
*id
= NULL
;
6666 struct got_reference
*ref
= NULL
;
6667 struct got_reflist_head refs
;
6670 * Don't let the user create a reference name with a leading '-'.
6671 * While technically a valid reference name, this case is usually
6672 * an unintended typo.
6674 if (refname
[0] == '-')
6675 return got_error_path(refname
, GOT_ERR_REF_NAME_MINUS
);
6678 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
6681 err
= got_repo_match_object_id(&id
, NULL
, target
, GOT_OBJ_TYPE_ANY
,
6683 got_ref_list_free(&refs
);
6687 err
= got_ref_alloc(&ref
, refname
, id
);
6691 err
= got_ref_write(ref
, repo
);
6699 static const struct got_error
*
6700 add_symref(struct got_repository
*repo
, const char *refname
, const char *target
)
6702 const struct got_error
*err
= NULL
;
6703 struct got_reference
*ref
= NULL
;
6704 struct got_reference
*target_ref
= NULL
;
6707 * Don't let the user create a reference name with a leading '-'.
6708 * While technically a valid reference name, this case is usually
6709 * an unintended typo.
6711 if (refname
[0] == '-')
6712 return got_error_path(refname
, GOT_ERR_REF_NAME_MINUS
);
6714 err
= got_ref_open(&target_ref
, repo
, target
, 0);
6718 err
= got_ref_alloc_symref(&ref
, refname
, target_ref
);
6722 err
= got_ref_write(ref
, repo
);
6725 got_ref_close(target_ref
);
6731 static const struct got_error
*
6732 cmd_ref(int argc
, char *argv
[])
6734 const struct got_error
*error
= NULL
;
6735 struct got_repository
*repo
= NULL
;
6736 struct got_worktree
*worktree
= NULL
;
6737 char *cwd
= NULL
, *repo_path
= NULL
;
6738 int ch
, do_list
= 0, do_delete
= 0, sort_by_time
= 0;
6739 const char *obj_arg
= NULL
, *symref_target
= NULL
;
6740 char *refname
= NULL
, *keyword_idstr
= NULL
;
6741 int *pack_fds
= NULL
;
6744 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6745 "sendfd unveil", NULL
) == -1)
6749 while ((ch
= getopt(argc
, argv
, "c:dlr:s:t")) != -1) {
6761 repo_path
= realpath(optarg
, NULL
);
6762 if (repo_path
== NULL
)
6763 return got_error_from_errno2("realpath",
6765 got_path_strip_trailing_slashes(repo_path
);
6768 symref_target
= optarg
;
6779 if (obj_arg
&& do_list
)
6780 option_conflict('c', 'l');
6781 if (obj_arg
&& do_delete
)
6782 option_conflict('c', 'd');
6783 if (obj_arg
&& symref_target
)
6784 option_conflict('c', 's');
6785 if (symref_target
&& do_delete
)
6786 option_conflict('s', 'd');
6787 if (symref_target
&& do_list
)
6788 option_conflict('s', 'l');
6789 if (do_delete
&& do_list
)
6790 option_conflict('d', 'l');
6791 if (sort_by_time
&& !do_list
)
6792 errx(1, "-t option requires -l option");
6798 if (argc
!= 0 && argc
!= 1)
6801 refname
= strdup(argv
[0]);
6802 if (refname
== NULL
) {
6803 error
= got_error_from_errno("strdup");
6810 refname
= strdup(argv
[0]);
6811 if (refname
== NULL
) {
6812 error
= got_error_from_errno("strdup");
6818 got_path_strip_trailing_slashes(refname
);
6820 cwd
= getcwd(NULL
, 0);
6822 error
= got_error_from_errno("getcwd");
6826 error
= got_repo_pack_fds_open(&pack_fds
);
6830 if (repo_path
== NULL
) {
6831 error
= got_worktree_open(&worktree
, cwd
,
6832 GOT_WORKTREE_GOT_DIR
);
6833 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
6839 strdup(got_worktree_get_repo_path(worktree
));
6840 if (repo_path
== NULL
)
6841 error
= got_error_from_errno("strdup");
6845 repo_path
= strdup(cwd
);
6846 if (repo_path
== NULL
) {
6847 error
= got_error_from_errno("strdup");
6853 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
6859 /* Remove "cpath" promise. */
6860 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6866 error
= apply_unveil(got_repo_get_path(repo
), do_list
,
6867 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
6872 error
= list_refs(repo
, refname
, sort_by_time
);
6874 error
= delete_ref_by_name(repo
, refname
);
6875 else if (symref_target
)
6876 error
= add_symref(repo
, refname
, symref_target
);
6878 if (obj_arg
== NULL
)
6881 error
= got_keyword_to_idstr(&keyword_idstr
, obj_arg
,
6885 if (keyword_idstr
!= NULL
)
6886 obj_arg
= keyword_idstr
;
6888 error
= add_ref(repo
, refname
, obj_arg
);
6893 const struct got_error
*close_err
= got_repo_close(repo
);
6898 got_worktree_close(worktree
);
6900 const struct got_error
*pack_err
=
6901 got_repo_pack_fds_close(pack_fds
);
6907 free(keyword_idstr
);
6914 fprintf(stderr
, "usage: %s branch [-lnt] [-c commit] [-d name] "
6915 "[-r repository-path] [name]\n", getprogname());
6919 static const struct got_error
*
6920 list_branch(struct got_repository
*repo
, struct got_worktree
*worktree
,
6921 struct got_reference
*ref
)
6923 const struct got_error
*err
= NULL
;
6924 const char *refname
;
6928 refname
= got_ref_get_name(ref
);
6929 if (worktree
&& strcmp(refname
,
6930 got_worktree_get_head_ref_name(worktree
)) == 0) {
6931 err
= got_worktree_get_state(&marker
, repo
, worktree
,
6932 check_cancelled
, NULL
);
6937 if (strncmp(refname
, "refs/heads/", 11) == 0)
6939 if (strncmp(refname
, "refs/got/worktree/", 18) == 0)
6941 if (strncmp(refname
, "refs/remotes/", 13) == 0)
6944 refstr
= got_ref_to_str(ref
);
6946 return got_error_from_errno("got_ref_to_str");
6948 printf("%c %s: %s\n", marker
, refname
, refstr
);
6953 static const struct got_error
*
6954 show_current_branch(struct got_repository
*repo
, struct got_worktree
*worktree
)
6956 const char *refname
;
6958 if (worktree
== NULL
)
6959 return got_error(GOT_ERR_NOT_WORKTREE
);
6961 refname
= got_worktree_get_head_ref_name(worktree
);
6963 if (strncmp(refname
, "refs/heads/", 11) == 0)
6965 if (strncmp(refname
, "refs/got/worktree/", 18) == 0)
6968 printf("%s\n", refname
);
6973 static const struct got_error
*
6974 list_branches(struct got_repository
*repo
, struct got_worktree
*worktree
,
6977 static const struct got_error
*err
= NULL
;
6978 struct got_reflist_head refs
;
6979 struct got_reflist_entry
*re
;
6980 struct got_reference
*temp_ref
= NULL
;
6981 int rebase_in_progress
, histedit_in_progress
;
6986 err
= got_worktree_rebase_in_progress(&rebase_in_progress
,
6991 err
= got_worktree_histedit_in_progress(&histedit_in_progress
,
6996 if (rebase_in_progress
|| histedit_in_progress
) {
6997 err
= got_ref_open(&temp_ref
, repo
,
6998 got_worktree_get_head_ref_name(worktree
), 0);
7001 list_branch(repo
, worktree
, temp_ref
);
7002 got_ref_close(temp_ref
);
7006 err
= got_ref_list(&refs
, repo
, "refs/heads", sort_by_time
?
7007 got_ref_cmp_by_commit_timestamp_descending
: got_ref_cmp_by_name
,
7012 TAILQ_FOREACH(re
, &refs
, entry
)
7013 list_branch(repo
, worktree
, re
->ref
);
7015 got_ref_list_free(&refs
);
7017 err
= got_ref_list(&refs
, repo
, "refs/remotes", sort_by_time
?
7018 got_ref_cmp_by_commit_timestamp_descending
: got_ref_cmp_by_name
,
7023 TAILQ_FOREACH(re
, &refs
, entry
)
7024 list_branch(repo
, worktree
, re
->ref
);
7026 got_ref_list_free(&refs
);
7031 static const struct got_error
*
7032 delete_branch(struct got_repository
*repo
, struct got_worktree
*worktree
,
7033 const char *branch_name
)
7035 const struct got_error
*err
= NULL
;
7036 struct got_reference
*ref
= NULL
;
7037 char *refname
, *remote_refname
= NULL
;
7039 if (strncmp(branch_name
, "refs/", 5) == 0)
7041 if (strncmp(branch_name
, "heads/", 6) == 0)
7043 else if (strncmp(branch_name
, "remotes/", 8) == 0)
7046 if (asprintf(&refname
, "refs/heads/%s", branch_name
) == -1)
7047 return got_error_from_errno("asprintf");
7049 if (asprintf(&remote_refname
, "refs/remotes/%s",
7050 branch_name
) == -1) {
7051 err
= got_error_from_errno("asprintf");
7055 err
= got_ref_open(&ref
, repo
, refname
, 0);
7057 const struct got_error
*err2
;
7058 if (err
->code
!= GOT_ERR_NOT_REF
)
7061 * Keep 'err' intact such that if neither branch exists
7062 * we report "refs/heads" rather than "refs/remotes" in
7063 * our error message.
7065 err2
= got_ref_open(&ref
, repo
, remote_refname
, 0);
7072 strcmp(got_worktree_get_head_ref_name(worktree
),
7073 got_ref_get_name(ref
)) == 0) {
7074 err
= got_error_msg(GOT_ERR_SAME_BRANCH
,
7075 "will not delete this work tree's current branch");
7079 err
= delete_ref(repo
, ref
);
7084 free(remote_refname
);
7088 static const struct got_error
*
7089 add_branch(struct got_repository
*repo
, const char *branch_name
,
7090 struct got_object_id
*base_commit_id
)
7092 const struct got_error
*err
= NULL
;
7093 struct got_reference
*ref
= NULL
;
7094 char *refname
= NULL
;
7097 * Don't let the user create a branch name with a leading '-'.
7098 * While technically a valid reference name, this case is usually
7099 * an unintended typo.
7101 if (branch_name
[0] == '-')
7102 return got_error_path(branch_name
, GOT_ERR_REF_NAME_MINUS
);
7104 if (strncmp(branch_name
, "refs/heads/", 11) == 0)
7107 if (asprintf(&refname
, "refs/heads/%s", branch_name
) == -1) {
7108 err
= got_error_from_errno("asprintf");
7112 err
= got_ref_open(&ref
, repo
, refname
, 0);
7114 err
= got_error(GOT_ERR_BRANCH_EXISTS
);
7116 } else if (err
->code
!= GOT_ERR_NOT_REF
)
7119 err
= got_ref_alloc(&ref
, refname
, base_commit_id
);
7123 err
= got_ref_write(ref
, repo
);
7131 static const struct got_error
*
7132 cmd_branch(int argc
, char *argv
[])
7134 const struct got_error
*error
= NULL
;
7135 struct got_repository
*repo
= NULL
;
7136 struct got_worktree
*worktree
= NULL
;
7137 char *cwd
= NULL
, *repo_path
= NULL
;
7138 int ch
, do_list
= 0, do_show
= 0, do_update
= 1, sort_by_time
= 0;
7139 const char *delref
= NULL
, *commit_id_arg
= NULL
;
7140 struct got_reference
*ref
= NULL
;
7141 struct got_pathlist_head paths
;
7142 struct got_object_id
*commit_id
= NULL
;
7143 char *commit_id_str
= NULL
, *keyword_idstr
= NULL
;;
7144 int *pack_fds
= NULL
;
7149 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7150 "sendfd unveil", NULL
) == -1)
7154 while ((ch
= getopt(argc
, argv
, "c:d:lnr:t")) != -1) {
7157 commit_id_arg
= optarg
;
7169 repo_path
= realpath(optarg
, NULL
);
7170 if (repo_path
== NULL
)
7171 return got_error_from_errno2("realpath",
7173 got_path_strip_trailing_slashes(repo_path
);
7184 if (do_list
&& delref
)
7185 option_conflict('l', 'd');
7186 if (sort_by_time
&& !do_list
)
7187 errx(1, "-t option requires -l option");
7192 if (!do_list
&& !delref
&& argc
== 0)
7195 if ((do_list
|| delref
|| do_show
) && commit_id_arg
!= NULL
)
7196 errx(1, "-c option can only be used when creating a branch");
7198 if (do_list
|| delref
) {
7201 } else if (!do_show
&& argc
!= 1)
7204 cwd
= getcwd(NULL
, 0);
7206 error
= got_error_from_errno("getcwd");
7210 error
= got_repo_pack_fds_open(&pack_fds
);
7214 if (repo_path
== NULL
) {
7215 error
= got_worktree_open(&worktree
, cwd
,
7216 GOT_WORKTREE_GOT_DIR
);
7217 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
7223 strdup(got_worktree_get_repo_path(worktree
));
7224 if (repo_path
== NULL
)
7225 error
= got_error_from_errno("strdup");
7229 repo_path
= strdup(cwd
);
7230 if (repo_path
== NULL
) {
7231 error
= got_error_from_errno("strdup");
7237 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
7242 if (do_list
|| do_show
) {
7243 /* Remove "cpath" promise. */
7244 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7250 error
= apply_unveil(got_repo_get_path(repo
), do_list
,
7251 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
7256 error
= show_current_branch(repo
, worktree
);
7258 error
= list_branches(repo
, worktree
, sort_by_time
);
7260 error
= delete_branch(repo
, worktree
, delref
);
7262 struct got_reflist_head refs
;
7264 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
,
7268 if (commit_id_arg
== NULL
)
7269 commit_id_arg
= worktree
?
7270 got_worktree_get_head_ref_name(worktree
) :
7273 error
= got_keyword_to_idstr(&keyword_idstr
,
7274 commit_id_arg
, repo
, worktree
);
7277 if (keyword_idstr
!= NULL
)
7278 commit_id_arg
= keyword_idstr
;
7280 error
= got_repo_match_object_id(&commit_id
, NULL
,
7281 commit_id_arg
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
7282 got_ref_list_free(&refs
);
7285 error
= add_branch(repo
, argv
[0], commit_id
);
7288 if (worktree
&& do_update
) {
7289 struct got_update_progress_arg upa
;
7290 char *branch_refname
= NULL
;
7292 error
= got_object_id_str(&commit_id_str
, commit_id
);
7295 error
= get_worktree_paths_from_argv(&paths
, 0, NULL
,
7299 if (asprintf(&branch_refname
, "refs/heads/%s", argv
[0])
7301 error
= got_error_from_errno("asprintf");
7304 error
= got_ref_open(&ref
, repo
, branch_refname
, 0);
7305 free(branch_refname
);
7308 error
= switch_head_ref(ref
, commit_id
, worktree
,
7312 error
= got_worktree_set_base_commit_id(worktree
, repo
,
7316 memset(&upa
, 0, sizeof(upa
));
7317 error
= got_worktree_checkout_files(worktree
, &paths
,
7318 repo
, update_progress
, &upa
, check_cancelled
,
7322 if (upa
.did_something
) {
7323 printf("Updated to %s: %s\n",
7324 got_worktree_get_head_ref_name(worktree
),
7327 print_update_progress_stats(&upa
);
7331 free(keyword_idstr
);
7335 const struct got_error
*close_err
= got_repo_close(repo
);
7340 got_worktree_close(worktree
);
7342 const struct got_error
*pack_err
=
7343 got_repo_pack_fds_close(pack_fds
);
7350 free(commit_id_str
);
7351 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
7359 fprintf(stderr
, "usage: %s tag [-lVv] [-c commit] [-m message] "
7360 "[-r repository-path] [-s signer-id] name\n", getprogname());
7365 static const struct got_error
*
7366 sort_tags(struct got_reflist_head
*sorted
, struct got_reflist_head
*tags
)
7368 const struct got_error
*err
= NULL
;
7369 struct got_reflist_entry
*re
, *se
, *new;
7370 struct got_object_id
*re_id
, *se_id
;
7371 struct got_tag_object
*re_tag
, *se_tag
;
7372 time_t re_time
, se_time
;
7374 STAILQ_FOREACH(re
, tags
, entry
) {
7375 se
= STAILQ_FIRST(sorted
);
7377 err
= got_reflist_entry_dup(&new, re
);
7380 STAILQ_INSERT_HEAD(sorted
, new, entry
);
7383 err
= got_ref_resolve(&re_id
, repo
, re
->ref
);
7386 err
= got_object_open_as_tag(&re_tag
, repo
, re_id
);
7390 re_time
= got_object_tag_get_tagger_time(re_tag
);
7391 got_object_tag_close(re_tag
);
7395 err
= got_ref_resolve(&se_id
, repo
, re
->ref
);
7398 err
= got_object_open_as_tag(&se_tag
, repo
, se_id
);
7402 se_time
= got_object_tag_get_tagger_time(se_tag
);
7403 got_object_tag_close(se_tag
);
7405 if (se_time
> re_time
) {
7406 err
= got_reflist_entry_dup(&new, re
);
7409 STAILQ_INSERT_AFTER(sorted
, se
, new, entry
);
7412 se
= STAILQ_NEXT(se
, entry
);
7421 static const struct got_error
*
7422 get_tag_refname(char **refname
, const char *tag_name
)
7424 const struct got_error
*err
;
7426 if (strncmp("refs/tags/", tag_name
, 10) == 0) {
7427 *refname
= strdup(tag_name
);
7428 if (*refname
== NULL
)
7429 return got_error_from_errno("strdup");
7430 } else if (asprintf(refname
, "refs/tags/%s", tag_name
) == -1) {
7431 err
= got_error_from_errno("asprintf");
7439 static const struct got_error
*
7440 list_tags(struct got_repository
*repo
, const char *tag_name
, int verify_tags
,
7441 const char *allowed_signers
, const char *revoked_signers
, int verbosity
)
7443 static const struct got_error
*err
= NULL
;
7444 struct got_reflist_head refs
;
7445 struct got_reflist_entry
*re
;
7446 char *wanted_refname
= NULL
;
7451 err
= got_ref_list(&refs
, repo
, "refs/tags", got_ref_cmp_tags
, repo
);
7456 struct got_reference
*ref
;
7457 err
= get_tag_refname(&wanted_refname
, tag_name
);
7460 /* Wanted tag reference should exist. */
7461 err
= got_ref_open(&ref
, repo
, wanted_refname
, 0);
7467 TAILQ_FOREACH(re
, &refs
, entry
) {
7468 const char *refname
;
7469 char *refstr
, *tagmsg0
, *tagmsg
, *line
, *id_str
, *datestr
;
7471 const char *tagger
, *ssh_sig
= NULL
;
7472 char *sig_msg
= NULL
;
7474 struct got_object_id
*id
;
7475 struct got_tag_object
*tag
;
7476 struct got_commit_object
*commit
= NULL
;
7478 refname
= got_ref_get_name(re
->ref
);
7479 if (strncmp(refname
, "refs/tags/", 10) != 0 ||
7480 (wanted_refname
&& strcmp(refname
, wanted_refname
) != 0))
7483 refstr
= got_ref_to_str(re
->ref
);
7484 if (refstr
== NULL
) {
7485 err
= got_error_from_errno("got_ref_to_str");
7489 err
= got_ref_resolve(&id
, repo
, re
->ref
);
7492 err
= got_object_open_as_tag(&tag
, repo
, id
);
7494 if (err
->code
!= GOT_ERR_OBJ_TYPE
) {
7498 /* "lightweight" tag */
7499 err
= got_object_open_as_commit(&commit
, repo
, id
);
7504 tagger
= got_object_commit_get_committer(commit
);
7506 got_object_commit_get_committer_time(commit
);
7507 err
= got_object_id_str(&id_str
, id
);
7513 tagger
= got_object_tag_get_tagger(tag
);
7514 tagger_time
= got_object_tag_get_tagger_time(tag
);
7515 err
= got_object_id_str(&id_str
,
7516 got_object_tag_get_object_id(tag
));
7521 if (tag
&& verify_tags
) {
7522 ssh_sig
= got_sigs_get_tagmsg_ssh_signature(
7523 got_object_tag_get_message(tag
));
7524 if (ssh_sig
&& allowed_signers
== NULL
) {
7525 err
= got_error_msg(
7526 GOT_ERR_VERIFY_TAG_SIGNATURE
,
7527 "SSH signature verification requires "
7528 "setting allowed_signers in "
7534 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR
, refname
, refstr
);
7536 printf("from: %s\n", tagger
);
7537 datestr
= get_datestr(&tagger_time
, datebuf
);
7539 printf("date: %s UTC\n", datestr
);
7541 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
, id_str
);
7543 switch (got_object_tag_get_object_type(tag
)) {
7544 case GOT_OBJ_TYPE_BLOB
:
7545 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB
,
7548 case GOT_OBJ_TYPE_TREE
:
7549 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE
,
7552 case GOT_OBJ_TYPE_COMMIT
:
7553 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT
,
7556 case GOT_OBJ_TYPE_TAG
:
7557 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG
,
7567 err
= got_sigs_verify_tag_ssh(&sig_msg
, tag
, ssh_sig
,
7568 allowed_signers
, revoked_signers
, verbosity
);
7569 if (err
&& err
->code
== GOT_ERR_BAD_TAG_SIGNATURE
)
7573 printf("signature: %s", sig_msg
);
7579 err
= got_object_commit_get_logmsg(&tagmsg0
, commit
);
7582 got_object_commit_close(commit
);
7584 tagmsg0
= strdup(got_object_tag_get_message(tag
));
7585 got_object_tag_close(tag
);
7586 if (tagmsg0
== NULL
) {
7587 err
= got_error_from_errno("strdup");
7594 line
= strsep(&tagmsg
, "\n");
7596 printf(" %s\n", line
);
7601 got_ref_list_free(&refs
);
7602 free(wanted_refname
);
7604 if (err
== NULL
&& bad_sigs
)
7605 err
= got_error(GOT_ERR_BAD_TAG_SIGNATURE
);
7609 static const struct got_error
*
7610 get_tag_message(char **tagmsg
, char **tagmsg_path
, const char *commit_id_str
,
7611 const char *tag_name
, const char *editor
, const char *repo_path
)
7613 const struct got_error
*err
= NULL
;
7614 char *template = NULL
, *initial_content
= NULL
;
7615 int initial_content_len
;
7618 if (asprintf(&template, GOT_TMPDIR_STR
"/got-tagmsg") == -1) {
7619 err
= got_error_from_errno("asprintf");
7623 initial_content_len
= asprintf(&initial_content
,
7624 "\n# tagging commit %s as %s\n",
7625 commit_id_str
, tag_name
);
7626 if (initial_content_len
== -1) {
7627 err
= got_error_from_errno("asprintf");
7631 err
= got_opentemp_named_fd(tagmsg_path
, &fd
, template, "");
7635 if (write(fd
, initial_content
, initial_content_len
) == -1) {
7636 err
= got_error_from_errno2("write", *tagmsg_path
);
7639 if (close(fd
) == -1) {
7640 err
= got_error_from_errno2("close", *tagmsg_path
);
7645 err
= edit_logmsg(tagmsg
, editor
, *tagmsg_path
, initial_content
,
7646 initial_content_len
, 1);
7648 free(initial_content
);
7651 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
7652 err
= got_error_from_errno2("close", *tagmsg_path
);
7661 static const struct got_error
*
7662 add_tag(struct got_repository
*repo
, const char *tagger
,
7663 const char *tag_name
, const char *commit_arg
, const char *tagmsg_arg
,
7664 const char *signer_id
, const char *editor
, int verbosity
)
7666 const struct got_error
*err
= NULL
;
7667 struct got_object_id
*commit_id
= NULL
, *tag_id
= NULL
;
7668 char *label
= NULL
, *commit_id_str
= NULL
;
7669 struct got_reference
*ref
= NULL
;
7670 char *refname
= NULL
, *tagmsg
= NULL
;
7671 char *tagmsg_path
= NULL
, *tag_id_str
= NULL
;
7672 int preserve_tagmsg
= 0;
7673 struct got_reflist_head refs
;
7678 * Don't let the user create a tag name with a leading '-'.
7679 * While technically a valid reference name, this case is usually
7680 * an unintended typo.
7682 if (tag_name
[0] == '-')
7683 return got_error_path(tag_name
, GOT_ERR_REF_NAME_MINUS
);
7685 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
7689 err
= got_repo_match_object_id(&commit_id
, &label
, commit_arg
,
7690 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
7694 err
= got_object_id_str(&commit_id_str
, commit_id
);
7698 err
= get_tag_refname(&refname
, tag_name
);
7701 if (strncmp("refs/tags/", tag_name
, 10) == 0)
7704 err
= got_ref_open(&ref
, repo
, refname
, 0);
7706 err
= got_error(GOT_ERR_TAG_EXISTS
);
7708 } else if (err
->code
!= GOT_ERR_NOT_REF
)
7711 if (tagmsg_arg
== NULL
) {
7712 err
= get_tag_message(&tagmsg
, &tagmsg_path
, commit_id_str
,
7713 tag_name
, editor
, got_repo_get_path(repo
));
7715 if (err
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
7716 tagmsg_path
!= NULL
)
7717 preserve_tagmsg
= 1;
7722 err
= got_object_tag_create(&tag_id
, tag_name
, commit_id
,
7723 tagger
, time(NULL
), tagmsg
? tagmsg
: tagmsg_arg
, signer_id
, repo
,
7727 preserve_tagmsg
= 1;
7731 err
= got_ref_alloc(&ref
, refname
, tag_id
);
7734 preserve_tagmsg
= 1;
7738 err
= got_ref_write(ref
, repo
);
7741 preserve_tagmsg
= 1;
7745 err
= got_object_id_str(&tag_id_str
, tag_id
);
7748 preserve_tagmsg
= 1;
7751 printf("Created tag %s\n", tag_id_str
);
7753 if (preserve_tagmsg
) {
7754 fprintf(stderr
, "%s: tag message preserved in %s\n",
7755 getprogname(), tagmsg_path
);
7756 } else if (tagmsg_path
&& unlink(tagmsg_path
) == -1 && err
== NULL
)
7757 err
= got_error_from_errno2("unlink", tagmsg_path
);
7762 free(commit_id_str
);
7766 got_ref_list_free(&refs
);
7770 static const struct got_error
*
7771 cmd_tag(int argc
, char *argv
[])
7773 const struct got_error
*error
= NULL
;
7774 struct got_repository
*repo
= NULL
;
7775 struct got_worktree
*worktree
= NULL
;
7776 char *cwd
= NULL
, *repo_path
= NULL
, *commit_id_str
= NULL
;
7777 char *gitconfig_path
= NULL
, *tagger
= NULL
, *keyword_idstr
= NULL
;
7778 char *allowed_signers
= NULL
, *revoked_signers
= NULL
, *editor
= NULL
;
7779 const char *signer_id
= NULL
;
7780 const char *tag_name
= NULL
, *commit_id_arg
= NULL
, *tagmsg
= NULL
;
7781 int ch
, do_list
= 0, verify_tags
= 0, verbosity
= 0;
7782 int *pack_fds
= NULL
;
7785 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7786 "sendfd unveil", NULL
) == -1)
7790 while ((ch
= getopt(argc
, argv
, "c:lm:r:s:Vv")) != -1) {
7793 commit_id_arg
= optarg
;
7802 repo_path
= realpath(optarg
, NULL
);
7803 if (repo_path
== NULL
) {
7804 error
= got_error_from_errno2("realpath",
7808 got_path_strip_trailing_slashes(repo_path
);
7819 else if (verbosity
< 3)
7831 if (do_list
|| verify_tags
) {
7832 if (commit_id_arg
!= NULL
)
7834 "-c option can only be used when creating a tag");
7837 option_conflict('l', 'm');
7839 option_conflict('V', 'm');
7843 option_conflict('l', 's');
7845 option_conflict('V', 's');
7849 } else if (argc
!= 1)
7855 cwd
= getcwd(NULL
, 0);
7857 error
= got_error_from_errno("getcwd");
7861 error
= got_repo_pack_fds_open(&pack_fds
);
7865 if (repo_path
== NULL
) {
7866 error
= got_worktree_open(&worktree
, cwd
,
7867 GOT_WORKTREE_GOT_DIR
);
7868 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
7874 strdup(got_worktree_get_repo_path(worktree
));
7875 if (repo_path
== NULL
)
7876 error
= got_error_from_errno("strdup");
7880 repo_path
= strdup(cwd
);
7881 if (repo_path
== NULL
) {
7882 error
= got_error_from_errno("strdup");
7888 if (do_list
|| verify_tags
) {
7889 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
7892 error
= get_allowed_signers(&allowed_signers
, repo
, worktree
);
7895 error
= get_revoked_signers(&revoked_signers
, repo
, worktree
);
7899 /* Release work tree lock. */
7900 got_worktree_close(worktree
);
7905 * Remove "cpath" promise unless needed for signature tmpfile
7909 got_sigs_apply_unveil();
7912 if (pledge("stdio rpath wpath flock proc exec sendfd "
7913 "unveil", NULL
) == -1)
7917 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
7920 error
= list_tags(repo
, tag_name
, verify_tags
, allowed_signers
,
7921 revoked_signers
, verbosity
);
7923 error
= get_gitconfig_path(&gitconfig_path
);
7926 error
= got_repo_open(&repo
, repo_path
, gitconfig_path
,
7931 error
= get_author(&tagger
, repo
, worktree
);
7934 if (signer_id
== NULL
)
7935 signer_id
= get_signer_id(repo
, worktree
);
7937 if (tagmsg
== NULL
) {
7938 error
= get_editor(&editor
);
7941 if (unveil(editor
, "x") != 0) {
7942 error
= got_error_from_errno2("unveil", editor
);
7947 error
= got_sigs_apply_unveil();
7951 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
7955 if (commit_id_arg
== NULL
) {
7956 struct got_reference
*head_ref
;
7957 struct got_object_id
*commit_id
;
7958 error
= got_ref_open(&head_ref
, repo
,
7959 worktree
? got_worktree_get_head_ref_name(worktree
)
7963 error
= got_ref_resolve(&commit_id
, repo
, head_ref
);
7964 got_ref_close(head_ref
);
7967 error
= got_object_id_str(&commit_id_str
, commit_id
);
7972 error
= got_keyword_to_idstr(&keyword_idstr
,
7973 commit_id_arg
, repo
, worktree
);
7976 commit_id_str
= keyword_idstr
;
7980 /* Release work tree lock. */
7981 got_worktree_close(worktree
);
7985 error
= add_tag(repo
, tagger
, tag_name
,
7986 commit_id_str
? commit_id_str
: commit_id_arg
, tagmsg
,
7987 signer_id
, editor
, verbosity
);
7991 const struct got_error
*close_err
= got_repo_close(repo
);
7996 got_worktree_close(worktree
);
7998 const struct got_error
*pack_err
=
7999 got_repo_pack_fds_close(pack_fds
);
8006 free(gitconfig_path
);
8007 free(commit_id_str
);
8009 free(allowed_signers
);
8010 free(revoked_signers
);
8017 fprintf(stderr
, "usage: %s add [-IR] path ...\n", getprogname());
8021 static const struct got_error
*
8022 add_progress(void *arg
, unsigned char status
, const char *path
)
8024 while (path
[0] == '/')
8026 printf("%c %s\n", status
, path
);
8030 static const struct got_error
*
8031 cmd_add(int argc
, char *argv
[])
8033 const struct got_error
*error
= NULL
;
8034 struct got_repository
*repo
= NULL
;
8035 struct got_worktree
*worktree
= NULL
;
8037 struct got_pathlist_head paths
;
8038 struct got_pathlist_entry
*pe
;
8039 int ch
, can_recurse
= 0, no_ignores
= 0;
8040 int *pack_fds
= NULL
;
8045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8050 while ((ch
= getopt(argc
, argv
, "IR")) != -1) {
8070 cwd
= getcwd(NULL
, 0);
8072 error
= got_error_from_errno("getcwd");
8076 error
= got_repo_pack_fds_open(&pack_fds
);
8080 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
8082 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8083 error
= wrap_not_worktree_error(error
, "add", cwd
);
8087 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
8092 error
= apply_unveil(got_repo_get_path(repo
), 1,
8093 got_worktree_get_root_path(worktree
));
8097 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
8104 TAILQ_FOREACH(pe
, &paths
, entry
) {
8105 if (asprintf(&ondisk_path
, "%s/%s",
8106 got_worktree_get_root_path(worktree
),
8108 error
= got_error_from_errno("asprintf");
8111 if (lstat(ondisk_path
, &sb
) == -1) {
8112 if (errno
== ENOENT
) {
8116 error
= got_error_from_errno2("lstat",
8122 if (S_ISDIR(sb
.st_mode
)) {
8123 error
= got_error_msg(GOT_ERR_BAD_PATH
,
8124 "adding directories requires -R option");
8130 error
= got_worktree_schedule_add(worktree
, &paths
, add_progress
,
8131 NULL
, repo
, no_ignores
);
8134 const struct got_error
*close_err
= got_repo_close(repo
);
8139 got_worktree_close(worktree
);
8141 const struct got_error
*pack_err
=
8142 got_repo_pack_fds_close(pack_fds
);
8146 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
8154 fprintf(stderr
, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8159 static const struct got_error
*
8160 print_remove_status(void *arg
, unsigned char status
,
8161 unsigned char staged_status
, const char *path
)
8163 while (path
[0] == '/')
8165 if (status
== GOT_STATUS_NONEXISTENT
)
8167 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
8168 status
= GOT_STATUS_NO_CHANGE
;
8169 printf("%c%c %s\n", status
, staged_status
, path
);
8173 static const struct got_error
*
8174 cmd_remove(int argc
, char *argv
[])
8176 const struct got_error
*error
= NULL
;
8177 struct got_worktree
*worktree
= NULL
;
8178 struct got_repository
*repo
= NULL
;
8179 const char *status_codes
= NULL
;
8181 struct got_pathlist_head paths
;
8182 struct got_pathlist_entry
*pe
;
8183 int ch
, delete_local_mods
= 0, can_recurse
= 0, keep_on_disk
= 0, i
;
8184 int ignore_missing_paths
= 0;
8185 int *pack_fds
= NULL
;
8190 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8195 while ((ch
= getopt(argc
, argv
, "fkRs:")) != -1) {
8198 delete_local_mods
= 1;
8199 ignore_missing_paths
= 1;
8208 for (i
= 0; optarg
[i
] != '\0'; i
++) {
8209 switch (optarg
[i
]) {
8210 case GOT_STATUS_MODIFY
:
8211 delete_local_mods
= 1;
8213 case GOT_STATUS_MISSING
:
8214 ignore_missing_paths
= 1;
8217 errx(1, "invalid status code '%c'",
8221 status_codes
= optarg
;
8235 cwd
= getcwd(NULL
, 0);
8237 error
= got_error_from_errno("getcwd");
8241 error
= got_repo_pack_fds_open(&pack_fds
);
8245 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
8247 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8248 error
= wrap_not_worktree_error(error
, "remove", cwd
);
8252 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
8257 error
= apply_unveil(got_repo_get_path(repo
), 1,
8258 got_worktree_get_root_path(worktree
));
8262 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
8269 TAILQ_FOREACH(pe
, &paths
, entry
) {
8270 if (asprintf(&ondisk_path
, "%s/%s",
8271 got_worktree_get_root_path(worktree
),
8273 error
= got_error_from_errno("asprintf");
8276 if (lstat(ondisk_path
, &sb
) == -1) {
8277 if (errno
== ENOENT
) {
8281 error
= got_error_from_errno2("lstat",
8287 if (S_ISDIR(sb
.st_mode
)) {
8288 error
= got_error_msg(GOT_ERR_BAD_PATH
,
8289 "removing directories requires -R option");
8295 error
= got_worktree_schedule_delete(worktree
, &paths
,
8296 delete_local_mods
, status_codes
, print_remove_status
, NULL
,
8297 repo
, keep_on_disk
, ignore_missing_paths
);
8300 const struct got_error
*close_err
= got_repo_close(repo
);
8305 got_worktree_close(worktree
);
8307 const struct got_error
*pack_err
=
8308 got_repo_pack_fds_close(pack_fds
);
8312 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
8320 fprintf(stderr
, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8321 "[patchfile]\n", getprogname());
8325 static const struct got_error
*
8326 patch_from_stdin(int *patchfd
)
8328 const struct got_error
*err
= NULL
;
8331 sig_t sighup
, sigint
, sigquit
;
8333 *patchfd
= got_opentempfd();
8335 return got_error_from_errno("got_opentempfd");
8337 sighup
= signal(SIGHUP
, SIG_DFL
);
8338 sigint
= signal(SIGINT
, SIG_DFL
);
8339 sigquit
= signal(SIGQUIT
, SIG_DFL
);
8342 r
= read(0, buf
, sizeof(buf
));
8344 err
= got_error_from_errno("read");
8349 if (write(*patchfd
, buf
, r
) == -1) {
8350 err
= got_error_from_errno("write");
8355 signal(SIGHUP
, sighup
);
8356 signal(SIGINT
, sigint
);
8357 signal(SIGQUIT
, sigquit
);
8359 if (err
== NULL
&& lseek(*patchfd
, 0, SEEK_SET
) == -1)
8360 err
= got_error_from_errno("lseek");
8370 struct got_patch_progress_arg
{
8376 static const struct got_error
*
8377 patch_progress(void *arg
, const char *old
, const char *new,
8378 unsigned char status
, const struct got_error
*error
, int old_from
,
8379 int old_lines
, int new_from
, int new_lines
, int offset
,
8380 int ws_mangled
, const struct got_error
*hunk_err
)
8382 const char *path
= new == NULL
? old
: new;
8383 struct got_patch_progress_arg
*a
= arg
;
8385 while (*path
== '/')
8388 if (status
!= GOT_STATUS_NO_CHANGE
&&
8389 status
!= 0 /* per-hunk progress */) {
8390 printf("%c %s\n", status
, path
);
8391 a
->did_something
= 1;
8394 if (hunk_err
== NULL
) {
8395 if (status
== GOT_STATUS_CANNOT_UPDATE
)
8397 else if (status
== GOT_STATUS_CONFLICT
)
8402 fprintf(stderr
, "%s: %s\n", getprogname(), error
->msg
);
8404 if (offset
!= 0 || hunk_err
!= NULL
|| ws_mangled
) {
8405 printf("@@ -%d,%d +%d,%d @@ ", old_from
,
8406 old_lines
, new_from
, new_lines
);
8407 if (hunk_err
!= NULL
)
8408 printf("%s\n", hunk_err
->msg
);
8409 else if (offset
!= 0)
8410 printf("applied with offset %d\n", offset
);
8412 printf("hunk contains mangled whitespace\n");
8419 print_patch_progress_stats(struct got_patch_progress_arg
*ppa
)
8421 if (!ppa
->did_something
)
8424 if (ppa
->conflicts
> 0)
8425 printf("Files with merge conflicts: %d\n", ppa
->conflicts
);
8427 if (ppa
->rejects
> 0) {
8428 printf("Files where patch failed to apply: %d\n",
8433 static const struct got_error
*
8434 cmd_patch(int argc
, char *argv
[])
8436 const struct got_error
*error
= NULL
, *close_error
= NULL
;
8437 struct got_worktree
*worktree
= NULL
;
8438 struct got_repository
*repo
= NULL
;
8439 struct got_reflist_head refs
;
8440 struct got_object_id
*commit_id
= NULL
;
8441 const char *commit_id_str
= NULL
;
8444 char *cwd
= NULL
, *keyword_idstr
= NULL
;
8445 int ch
, nop
= 0, strip
= -1, reverse
= 0;
8447 int *pack_fds
= NULL
;
8448 struct got_patch_progress_arg ppa
;
8453 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8454 "unveil", NULL
) == -1)
8458 while ((ch
= getopt(argc
, argv
, "c:np:R")) != -1) {
8461 commit_id_str
= optarg
;
8467 strip
= strtonum(optarg
, 0, INT_MAX
, &errstr
);
8469 errx(1, "pathname strip count is %s: %s",
8485 error
= patch_from_stdin(&patchfd
);
8488 } else if (argc
== 1) {
8489 patchfd
= open(argv
[0], O_RDONLY
);
8490 if (patchfd
== -1) {
8491 error
= got_error_from_errno2("open", argv
[0]);
8494 if (fstat(patchfd
, &sb
) == -1) {
8495 error
= got_error_from_errno2("fstat", argv
[0]);
8498 if (!S_ISREG(sb
.st_mode
)) {
8499 error
= got_error_path(argv
[0], GOT_ERR_BAD_FILETYPE
);
8505 if ((cwd
= getcwd(NULL
, 0)) == NULL
) {
8506 error
= got_error_from_errno("getcwd");
8510 error
= got_repo_pack_fds_open(&pack_fds
);
8514 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
8518 const char *repo_path
= got_worktree_get_repo_path(worktree
);
8519 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
8523 error
= apply_unveil(got_repo_get_path(repo
), 0,
8524 got_worktree_get_root_path(worktree
));
8528 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
8532 if (commit_id_str
!= NULL
) {
8533 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
8538 error
= got_repo_match_object_id(&commit_id
, NULL
,
8539 keyword_idstr
!= NULL
? keyword_idstr
: commit_id_str
,
8540 GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
8545 memset(&ppa
, 0, sizeof(ppa
));
8546 error
= got_patch(patchfd
, worktree
, repo
, nop
, strip
, reverse
,
8547 commit_id
, patch_progress
, &ppa
, check_cancelled
, NULL
);
8548 print_patch_progress_stats(&ppa
);
8550 got_ref_list_free(&refs
);
8551 free(keyword_idstr
);
8554 close_error
= got_repo_close(repo
);
8556 error
= close_error
;
8558 if (worktree
!= NULL
) {
8559 close_error
= got_worktree_close(worktree
);
8561 error
= close_error
;
8564 const struct got_error
*pack_err
=
8565 got_repo_pack_fds_close(pack_fds
);
8576 fprintf(stderr
, "usage: %s revert [-pR] [-F response-script] path ...\n",
8581 static const struct got_error
*
8582 revert_progress(void *arg
, unsigned char status
, const char *path
)
8584 if (status
== GOT_STATUS_UNVERSIONED
)
8587 while (path
[0] == '/')
8589 printf("%c %s\n", status
, path
);
8593 struct choose_patch_arg
{
8594 FILE *patch_script_file
;
8598 static const struct got_error
*
8599 show_change(unsigned char status
, const char *path
, FILE *patch_file
, int n
,
8600 int nchanges
, const char *action
)
8602 const struct got_error
*err
;
8604 size_t linesize
= 0;
8608 case GOT_STATUS_ADD
:
8609 printf("A %s\n%s this addition? [y/n] ", path
, action
);
8611 case GOT_STATUS_DELETE
:
8612 printf("D %s\n%s this deletion? [y/n] ", path
, action
);
8614 case GOT_STATUS_MODIFY
:
8615 if (fseek(patch_file
, 0L, SEEK_SET
) == -1)
8616 return got_error_from_errno("fseek");
8617 printf(GOT_COMMIT_SEP_STR
);
8618 while ((linelen
= getline(&line
, &linesize
, patch_file
)) != -1)
8620 if (linelen
== -1 && ferror(patch_file
)) {
8621 err
= got_error_from_errno("getline");
8626 printf(GOT_COMMIT_SEP_STR
);
8627 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8628 path
, n
, nchanges
, action
);
8631 return got_error_path(path
, GOT_ERR_FILE_STATUS
);
8638 #define CHOOSE_PATCH_VALID_RESPONSES "ynq"
8640 static const struct got_error
*
8641 choose_patch(int *choice
, void *arg
, unsigned char status
, const char *path
,
8642 FILE *patch_file
, int n
, int nchanges
)
8644 const struct got_error
*err
= NULL
;
8645 char *nl
, *line
= NULL
;
8647 size_t linesize
= 0;
8649 int interactive
= 1;
8650 struct choose_patch_arg
*a
= arg
;
8652 *choice
= GOT_PATCH_CHOICE_NONE
;
8654 if (a
->patch_script_file
) {
8656 fp
= a
->patch_script_file
;
8660 err
= show_change(status
, path
, patch_file
, n
, nchanges
,
8665 linelen
= getline(&line
, &linesize
, fp
);
8666 if (linelen
== -1) {
8669 return got_error_from_errno("getline");
8671 return got_error(GOT_ERR_EOF
);
8675 nl
= strchr(line
, '\n');
8678 if (strlen(line
) != 1 ||
8679 !strchr(CHOOSE_PATCH_VALID_RESPONSES
, line
[0])) {
8680 printf("invalid response '%s'\n", line
);
8685 /* ADD and DELETE do not accept 'q' response currently. */
8686 if (status
!= GOT_STATUS_MODIFY
&& line
[0] == 'q') {
8687 printf("invalid response '%s'\n", line
);
8696 *choice
= GOT_PATCH_CHOICE_YES
;
8699 *choice
= GOT_PATCH_CHOICE_NO
;
8702 if (status
== GOT_STATUS_MODIFY
)
8703 *choice
= GOT_PATCH_CHOICE_QUIT
;
8706 printf("invalid response '%s'\n", line
);
8712 printf("%c\n", line
[0]);
8718 struct wt_commitable_path_arg
{
8719 struct got_pathlist_head
*commit_paths
;
8724 * Shortcut work tree status callback to determine if the set of paths scanned
8725 * has at least one versioned path that is being modified and, if not NULL, is
8726 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8727 * soon as a path is passed with a status that satisfies this criteria.
8729 static const struct got_error
*
8730 worktree_has_commitable_path(void *arg
, unsigned char status
,
8731 unsigned char staged_status
, const char *path
,
8732 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
8733 struct got_object_id
*commit_id
, int dirfd
, const char *de_name
)
8735 struct wt_commitable_path_arg
*a
= arg
;
8737 if (status
== staged_status
&& (status
== GOT_STATUS_DELETE
))
8738 status
= GOT_STATUS_NO_CHANGE
;
8740 if (!(status
== GOT_STATUS_NO_CHANGE
||
8741 status
== GOT_STATUS_UNVERSIONED
) ||
8742 staged_status
!= GOT_STATUS_NO_CHANGE
) {
8743 if (a
->commit_paths
!= NULL
) {
8744 struct got_pathlist_entry
*pe
;
8746 TAILQ_FOREACH(pe
, a
->commit_paths
, entry
) {
8747 if (strncmp(path
, pe
->path
,
8748 pe
->path_len
) == 0) {
8749 *a
->has_changes
= 1;
8754 *a
->has_changes
= 1;
8756 if (*a
->has_changes
)
8757 return got_error(GOT_ERR_FILE_MODIFIED
);
8764 * Check that the changeset of the commit identified by id is
8765 * comprised of at least one modified path that is being committed.
8767 static const struct got_error
*
8768 commit_path_changed_in_worktree(struct wt_commitable_path_arg
*wcpa
,
8769 struct got_object_id
*id
, struct got_worktree
*worktree
,
8770 struct got_repository
*repo
)
8772 const struct got_error
*err
;
8773 struct got_pathlist_head paths
;
8774 struct got_commit_object
*commit
= NULL
, *pcommit
= NULL
;
8775 struct got_tree_object
*tree
= NULL
, *ptree
= NULL
;
8776 struct got_object_qid
*pid
;
8780 err
= got_object_open_as_commit(&commit
, repo
, id
);
8784 err
= got_object_open_as_tree(&tree
, repo
,
8785 got_object_commit_get_tree_id(commit
));
8789 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
8791 err
= got_object_open_as_commit(&pcommit
, repo
, &pid
->id
);
8795 err
= got_object_open_as_tree(&ptree
, repo
,
8796 got_object_commit_get_tree_id(pcommit
));
8801 err
= got_diff_tree(ptree
, tree
, NULL
, NULL
, -1, -1, "", "", repo
,
8802 got_diff_tree_collect_changed_paths
, &paths
, 0);
8806 err
= got_worktree_status(worktree
, &paths
, repo
, 0,
8807 worktree_has_commitable_path
, wcpa
, check_cancelled
, NULL
);
8808 if (err
&& err
->code
== GOT_ERR_FILE_MODIFIED
) {
8810 * At least one changed path in the referenced commit is
8811 * modified in the work tree, that's all we need to know!
8817 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_ALL
);
8819 got_object_commit_close(commit
);
8821 got_object_commit_close(pcommit
);
8823 got_object_tree_close(tree
);
8825 got_object_tree_close(ptree
);
8830 * Remove any "logmsg" reference comprised entirely of paths that have
8831 * been reverted in this work tree. If any path in the logmsg ref changeset
8832 * remains in a changed state in the worktree, do not remove the reference.
8834 static const struct got_error
*
8835 rm_logmsg_ref(struct got_worktree
*worktree
, struct got_repository
*repo
)
8837 const struct got_error
*err
;
8838 struct got_reflist_head refs
;
8839 struct got_reflist_entry
*re
;
8840 struct got_commit_object
*commit
= NULL
;
8841 struct got_object_id
*commit_id
= NULL
;
8842 struct wt_commitable_path_arg wcpa
;
8843 char *uuidstr
= NULL
;
8847 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
8851 err
= got_ref_list(&refs
, repo
, "refs/got/worktree",
8852 got_ref_cmp_by_name
, repo
);
8856 TAILQ_FOREACH(re
, &refs
, entry
) {
8857 const char *refname
;
8858 int has_changes
= 0;
8860 refname
= got_ref_get_name(re
->ref
);
8862 if (!strncmp(refname
, GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
8863 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
))
8864 refname
+= GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
+ 1;
8865 else if (!strncmp(refname
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
8866 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
))
8867 refname
+= GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
+ 1;
8871 if (strncmp(refname
, uuidstr
, GOT_WORKTREE_UUID_STRLEN
) == 0)
8872 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
8876 err
= got_repo_match_object_id(&commit_id
, NULL
, refname
,
8877 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
8881 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
8885 wcpa
.commit_paths
= NULL
;
8886 wcpa
.has_changes
= &has_changes
;
8888 err
= commit_path_changed_in_worktree(&wcpa
, commit_id
,
8894 err
= got_ref_delete(re
->ref
, repo
);
8899 got_object_commit_close(commit
);
8908 got_ref_list_free(&refs
);
8910 got_object_commit_close(commit
);
8914 static const struct got_error
*
8915 cmd_revert(int argc
, char *argv
[])
8917 const struct got_error
*error
= NULL
;
8918 struct got_worktree
*worktree
= NULL
;
8919 struct got_repository
*repo
= NULL
;
8920 char *cwd
= NULL
, *path
= NULL
;
8921 struct got_pathlist_head paths
;
8922 struct got_pathlist_entry
*pe
;
8923 int ch
, can_recurse
= 0, pflag
= 0;
8924 FILE *patch_script_file
= NULL
;
8925 const char *patch_script_path
= NULL
;
8926 struct choose_patch_arg cpa
;
8927 int *pack_fds
= NULL
;
8932 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8933 "unveil", NULL
) == -1)
8937 while ((ch
= getopt(argc
, argv
, "F:pR")) != -1) {
8940 patch_script_path
= optarg
;
8959 if (patch_script_path
&& !pflag
)
8960 errx(1, "-F option can only be used together with -p option");
8962 cwd
= getcwd(NULL
, 0);
8964 error
= got_error_from_errno("getcwd");
8968 error
= got_repo_pack_fds_open(&pack_fds
);
8972 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
8974 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
8975 error
= wrap_not_worktree_error(error
, "revert", cwd
);
8979 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
8984 if (patch_script_path
) {
8985 patch_script_file
= fopen(patch_script_path
, "re");
8986 if (patch_script_file
== NULL
) {
8987 error
= got_error_from_errno2("fopen",
8994 * XXX "c" perm needed on repo dir to delete merge references.
8996 error
= apply_unveil(got_repo_get_path(repo
), 0,
8997 got_worktree_get_root_path(worktree
));
9001 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
9008 TAILQ_FOREACH(pe
, &paths
, entry
) {
9009 if (asprintf(&ondisk_path
, "%s/%s",
9010 got_worktree_get_root_path(worktree
),
9012 error
= got_error_from_errno("asprintf");
9015 if (lstat(ondisk_path
, &sb
) == -1) {
9016 if (errno
== ENOENT
) {
9020 error
= got_error_from_errno2("lstat",
9026 if (S_ISDIR(sb
.st_mode
)) {
9027 error
= got_error_msg(GOT_ERR_BAD_PATH
,
9028 "reverting directories requires -R option");
9034 cpa
.patch_script_file
= patch_script_file
;
9035 cpa
.action
= "revert";
9036 error
= got_worktree_revert(worktree
, &paths
, revert_progress
, NULL
,
9037 pflag
? choose_patch
: NULL
, &cpa
, repo
);
9039 error
= rm_logmsg_ref(worktree
, repo
);
9041 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
9043 error
= got_error_from_errno2("fclose", patch_script_path
);
9045 const struct got_error
*close_err
= got_repo_close(repo
);
9050 got_worktree_close(worktree
);
9052 const struct got_error
*pack_err
=
9053 got_repo_pack_fds_close(pack_fds
);
9057 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
9066 fprintf(stderr
, "usage: %s commit [-CNnS] [-A author] [-F path] "
9067 "[-m message] [path ...]\n", getprogname());
9071 struct collect_commit_logmsg_arg
{
9072 const char *cmdline_log
;
9073 const char *prepared_log
;
9074 const char *merged_log
;
9075 int non_interactive
;
9077 const char *worktree_path
;
9078 const char *branch_name
;
9079 const char *repo_path
;
9084 static const struct got_error
*
9085 read_prepared_logmsg(char **logmsg
, const char *path
)
9087 const struct got_error
*err
= NULL
;
9093 memset(&sb
, 0, sizeof(sb
));
9095 f
= fopen(path
, "re");
9097 return got_error_from_errno2("fopen", path
);
9099 if (fstat(fileno(f
), &sb
) == -1) {
9100 err
= got_error_from_errno2("fstat", path
);
9103 if (sb
.st_size
== 0) {
9104 err
= got_error(GOT_ERR_COMMIT_MSG_EMPTY
);
9108 *logmsg
= malloc(sb
.st_size
+ 1);
9109 if (*logmsg
== NULL
) {
9110 err
= got_error_from_errno("malloc");
9114 r
= fread(*logmsg
, 1, sb
.st_size
, f
);
9115 if (r
!= sb
.st_size
) {
9117 err
= got_error_from_errno2("fread", path
);
9119 err
= got_error(GOT_ERR_IO
);
9122 (*logmsg
)[sb
.st_size
] = '\0';
9124 if (fclose(f
) == EOF
&& err
== NULL
)
9125 err
= got_error_from_errno2("fclose", path
);
9133 static const struct got_error
*
9134 collect_commit_logmsg(struct got_pathlist_head
*commitable_paths
,
9135 const char *diff_path
, char **logmsg
, void *arg
)
9137 char *initial_content
= NULL
;
9138 struct got_pathlist_entry
*pe
;
9139 const struct got_error
*err
= NULL
;
9140 char *template = NULL
;
9141 char *prepared_msg
= NULL
, *merged_msg
= NULL
;
9142 struct collect_commit_logmsg_arg
*a
= arg
;
9143 int initial_content_len
;
9147 /* if a message was specified on the command line, just use it */
9148 if (a
->cmdline_log
!= NULL
&& *a
->cmdline_log
!= '\0') {
9149 len
= strlen(a
->cmdline_log
) + 1;
9150 *logmsg
= malloc(len
+ 1);
9151 if (*logmsg
== NULL
)
9152 return got_error_from_errno("malloc");
9153 strlcpy(*logmsg
, a
->cmdline_log
, len
);
9155 } else if (a
->prepared_log
!= NULL
&& a
->non_interactive
)
9156 return read_prepared_logmsg(logmsg
, a
->prepared_log
);
9158 if (asprintf(&template, "%s/logmsg", a
->worktree_path
) == -1)
9159 return got_error_from_errno("asprintf");
9161 err
= got_opentemp_named_fd(&a
->logmsg_path
, &fd
, template, "");
9165 if (a
->prepared_log
) {
9166 err
= read_prepared_logmsg(&prepared_msg
, a
->prepared_log
);
9169 } else if (a
->merged_log
) {
9170 err
= read_prepared_logmsg(&merged_msg
, a
->merged_log
);
9175 initial_content_len
= asprintf(&initial_content
,
9176 "%s%s\n# changes to be committed on branch %s:\n",
9177 prepared_msg
? prepared_msg
: "",
9178 merged_msg
? merged_msg
: "", a
->branch_name
);
9179 if (initial_content_len
== -1) {
9180 err
= got_error_from_errno("asprintf");
9184 if (write(fd
, initial_content
, initial_content_len
) == -1) {
9185 err
= got_error_from_errno2("write", a
->logmsg_path
);
9189 TAILQ_FOREACH(pe
, commitable_paths
, entry
) {
9190 struct got_commitable
*ct
= pe
->data
;
9191 dprintf(fd
, "# %c %s\n",
9192 got_commitable_get_status(ct
),
9193 got_commitable_get_path(ct
));
9197 dprintf(fd
, "# detailed changes can be viewed in %s\n",
9201 if (close(fd
) == -1) {
9202 err
= got_error_from_errno2("close", a
->logmsg_path
);
9207 err
= edit_logmsg(logmsg
, a
->editor
, a
->logmsg_path
, initial_content
,
9208 initial_content_len
, a
->prepared_log
? 0 : 1);
9210 free(initial_content
);
9215 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
9216 err
= got_error_from_errno2("close", a
->logmsg_path
);
9224 static const struct got_error
*
9225 cat_logmsg(FILE *f
, struct got_commit_object
*commit
, const char *idstr
,
9226 const char *type
, int has_content
)
9228 const struct got_error
*err
= NULL
;
9229 char *logmsg
= NULL
;
9231 err
= got_object_commit_get_logmsg(&logmsg
, commit
);
9235 if (fprintf(f
, "%s# log message of %s commit %s:%s",
9236 has_content
? "\n" : "", type
, idstr
, logmsg
) < 0)
9237 err
= got_ferror(f
, GOT_ERR_IO
);
9244 * Lookup "logmsg" references of backed-out and cherrypicked commits
9245 * belonging to the current work tree. If found, and the worktree has
9246 * at least one modified file that was changed in the referenced commit,
9247 * add its log message to a new temporary file at *logmsg_path.
9248 * Add all refs found to matched_refs to be scheduled for removal on
9249 * successful commit.
9251 static const struct got_error
*
9252 lookup_logmsg_ref(char **logmsg_path
, struct got_pathlist_head
*paths
,
9253 struct got_reflist_head
*matched_refs
, struct got_worktree
*worktree
,
9254 struct got_repository
*repo
)
9256 const struct got_error
*err
;
9257 struct got_commit_object
*commit
= NULL
;
9258 struct got_object_id
*id
= NULL
;
9259 struct got_reflist_head refs
;
9260 struct got_reflist_entry
*re
, *re_match
;
9262 char *uuidstr
= NULL
;
9263 int added_logmsg
= 0;
9267 *logmsg_path
= NULL
;
9269 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
9273 err
= got_ref_list(&refs
, repo
, "refs/got/worktree",
9274 got_ref_cmp_by_name
, repo
);
9278 TAILQ_FOREACH(re
, &refs
, entry
) {
9279 const char *refname
, *type
;
9280 struct wt_commitable_path_arg wcpa
;
9283 refname
= got_ref_get_name(re
->ref
);
9285 if (strncmp(refname
, GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
9286 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
) == 0) {
9287 refname
+= GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
+ 1;
9288 type
= "cherrypicked";
9289 } else if (strncmp(refname
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
9290 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
) == 0) {
9291 refname
+= GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
+ 1;
9292 type
= "backed-out";
9296 if (strncmp(refname
, uuidstr
, GOT_WORKTREE_UUID_STRLEN
) == 0)
9297 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
9301 err
= got_repo_match_object_id(&id
, NULL
, refname
,
9302 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
9306 err
= got_object_open_as_commit(&commit
, repo
, id
);
9310 wcpa
.commit_paths
= paths
;
9311 wcpa
.has_changes
= &add_logmsg
;
9313 err
= commit_path_changed_in_worktree(&wcpa
, id
,
9320 err
= got_opentemp_named(logmsg_path
, &f
,
9321 "got-commit-logmsg", "");
9325 err
= cat_logmsg(f
, commit
, refname
, type
,
9332 err
= got_reflist_entry_dup(&re_match
, re
);
9335 TAILQ_INSERT_HEAD(matched_refs
, re_match
, entry
);
9338 got_object_commit_close(commit
);
9347 got_ref_list_free(&refs
);
9349 got_object_commit_close(commit
);
9350 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
9351 err
= got_error_from_errno("fclose");
9352 if (!added_logmsg
) {
9353 if (*logmsg_path
&& unlink(*logmsg_path
) != 0 && err
== NULL
)
9354 err
= got_error_from_errno2("unlink", *logmsg_path
);
9355 *logmsg_path
= NULL
;
9360 static const struct got_error
*
9361 cmd_commit(int argc
, char *argv
[])
9363 const struct got_error
*error
= NULL
;
9364 struct got_worktree
*worktree
= NULL
;
9365 struct got_repository
*repo
= NULL
;
9366 char *cwd
= NULL
, *id_str
= NULL
;
9367 struct got_object_id
*id
= NULL
;
9368 const char *logmsg
= NULL
;
9369 char *prepared_logmsg
= NULL
, *merged_logmsg
= NULL
;
9370 struct collect_commit_logmsg_arg cl_arg
;
9371 const char *author
= NULL
;
9372 char *gitconfig_path
= NULL
, *editor
= NULL
, *committer
= NULL
;
9373 int ch
, rebase_in_progress
, histedit_in_progress
, preserve_logmsg
= 0;
9374 int allow_bad_symlinks
= 0, non_interactive
= 0, merge_in_progress
= 0;
9375 int show_diff
= 1, commit_conflicts
= 0;
9376 struct got_pathlist_head paths
;
9377 struct got_reflist_head refs
;
9378 struct got_reflist_entry
*re
;
9379 int *pack_fds
= NULL
;
9383 cl_arg
.logmsg_path
= NULL
;
9386 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9387 "unveil", NULL
) == -1)
9391 while ((ch
= getopt(argc
, argv
, "A:CF:m:NnS")) != -1) {
9395 error
= valid_author(author
);
9400 commit_conflicts
= 1;
9404 option_conflict('F', 'm');
9405 prepared_logmsg
= realpath(optarg
, NULL
);
9406 if (prepared_logmsg
== NULL
)
9407 return got_error_from_errno2("realpath",
9411 if (prepared_logmsg
)
9412 option_conflict('m', 'F');
9416 non_interactive
= 1;
9422 allow_bad_symlinks
= 1;
9433 cwd
= getcwd(NULL
, 0);
9435 error
= got_error_from_errno("getcwd");
9439 error
= got_repo_pack_fds_open(&pack_fds
);
9443 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
9445 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
9446 error
= wrap_not_worktree_error(error
, "commit", cwd
);
9450 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
9453 if (rebase_in_progress
) {
9454 error
= got_error(GOT_ERR_REBASING
);
9458 error
= got_worktree_histedit_in_progress(&histedit_in_progress
,
9463 error
= get_gitconfig_path(&gitconfig_path
);
9466 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
9467 gitconfig_path
, pack_fds
);
9471 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
, repo
);
9474 if (merge_in_progress
) {
9475 error
= got_error(GOT_ERR_MERGE_BUSY
);
9479 error
= get_author(&committer
, repo
, worktree
);
9486 if (logmsg
== NULL
|| strlen(logmsg
) == 0) {
9487 error
= get_editor(&editor
);
9490 if (unveil(editor
, "x") != 0) {
9491 error
= got_error_from_errno2("unveil", editor
);
9495 if (prepared_logmsg
) {
9496 if (unveil(prepared_logmsg
, "r") != 0) {
9497 error
= got_error_from_errno2("unveil",
9503 error
= apply_unveil(got_repo_get_path(repo
), 0,
9504 got_worktree_get_root_path(worktree
));
9508 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
9512 if (prepared_logmsg
== NULL
) {
9513 error
= lookup_logmsg_ref(&merged_logmsg
,
9514 argc
> 0 ? &paths
: NULL
, &refs
, worktree
, repo
);
9519 cl_arg
.editor
= editor
;
9520 cl_arg
.cmdline_log
= logmsg
;
9521 cl_arg
.prepared_log
= prepared_logmsg
;
9522 cl_arg
.merged_log
= merged_logmsg
;
9523 cl_arg
.non_interactive
= non_interactive
;
9524 cl_arg
.worktree_path
= got_worktree_get_root_path(worktree
);
9525 cl_arg
.branch_name
= got_worktree_get_head_ref_name(worktree
);
9526 if (!histedit_in_progress
) {
9527 if (strncmp(cl_arg
.branch_name
, "refs/heads/", 11) != 0) {
9528 error
= got_error(GOT_ERR_COMMIT_BRANCH
);
9531 cl_arg
.branch_name
+= 11;
9533 cl_arg
.repo_path
= got_repo_get_path(repo
);
9534 error
= got_worktree_commit(&id
, worktree
, &paths
, author
, committer
,
9535 allow_bad_symlinks
, show_diff
, commit_conflicts
,
9536 collect_commit_logmsg
, &cl_arg
, print_status
, NULL
, repo
);
9538 if (error
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
&&
9539 cl_arg
.logmsg_path
!= NULL
)
9540 preserve_logmsg
= 1;
9544 error
= got_object_id_str(&id_str
, id
);
9547 printf("Created commit %s\n", id_str
);
9549 TAILQ_FOREACH(re
, &refs
, entry
) {
9550 error
= got_ref_delete(re
->ref
, repo
);
9556 if (preserve_logmsg
) {
9557 fprintf(stderr
, "%s: log message preserved in %s\n",
9558 getprogname(), cl_arg
.logmsg_path
);
9559 } else if (cl_arg
.logmsg_path
&& unlink(cl_arg
.logmsg_path
) == -1 &&
9561 error
= got_error_from_errno2("unlink", cl_arg
.logmsg_path
);
9562 free(cl_arg
.logmsg_path
);
9563 if (merged_logmsg
&& unlink(merged_logmsg
) == -1 && error
== NULL
)
9564 error
= got_error_from_errno2("unlink", merged_logmsg
);
9565 free(merged_logmsg
);
9567 const struct got_error
*close_err
= got_repo_close(repo
);
9572 got_worktree_close(worktree
);
9574 const struct got_error
*pack_err
=
9575 got_repo_pack_fds_close(pack_fds
);
9579 got_ref_list_free(&refs
);
9580 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
9583 free(gitconfig_path
);
9586 free(prepared_logmsg
);
9593 fprintf(stderr
, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9594 "[-r repository-path] [-t tag] [remote-repository]\n",
9600 print_load_info(int print_colored
, int print_found
, int print_trees
,
9601 int ncolored
, int nfound
, int ntrees
)
9603 if (print_colored
) {
9604 printf("%d commit%s colored", ncolored
,
9605 ncolored
== 1 ? "" : "s");
9608 printf("%s%d object%s found",
9609 ncolored
> 0 ? "; " : "",
9610 nfound
, nfound
== 1 ? "" : "s");
9613 printf("; %d tree%s scanned", ntrees
,
9614 ntrees
== 1 ? "" : "s");
9618 struct got_send_progress_arg
{
9619 char last_scaled_packsize
[FMT_SCALED_STRSIZE
];
9626 int last_nobj_total
;
9630 int printed_something
;
9632 struct got_pathlist_head
*delete_branches
;
9635 static const struct got_error
*
9636 send_progress(void *arg
, int ncolored
, int nfound
, int ntrees
,
9637 off_t packfile_size
, int ncommits
, int nobj_total
, int nobj_deltify
,
9638 int nobj_written
, off_t bytes_sent
, const char *refname
,
9639 const char *errmsg
, int success
)
9641 struct got_send_progress_arg
*a
= arg
;
9642 char scaled_packsize
[FMT_SCALED_STRSIZE
];
9643 char scaled_sent
[FMT_SCALED_STRSIZE
];
9644 int p_deltify
= 0, p_written
= 0, p_sent
= 0;
9645 int print_colored
= 0, print_found
= 0, print_trees
= 0;
9646 int print_searching
= 0, print_total
= 0;
9647 int print_deltify
= 0, print_written
= 0, print_sent
= 0;
9649 if (a
->verbosity
< 0)
9653 const char *status
= success
? "accepted" : "rejected";
9656 struct got_pathlist_entry
*pe
;
9657 TAILQ_FOREACH(pe
, a
->delete_branches
, entry
) {
9658 const char *branchname
= pe
->path
;
9659 if (got_path_cmp(branchname
, refname
,
9660 strlen(branchname
), strlen(refname
)) == 0) {
9662 a
->sent_something
= 1;
9668 if (a
->printed_something
)
9670 printf("Server has %s %s", status
, refname
);
9672 printf(": %s", errmsg
);
9673 a
->printed_something
= 1;
9677 if (a
->last_ncolored
!= ncolored
) {
9679 a
->last_ncolored
= ncolored
;
9682 if (a
->last_nfound
!= nfound
) {
9685 a
->last_nfound
= nfound
;
9688 if (a
->last_ntrees
!= ntrees
) {
9692 a
->last_ntrees
= ntrees
;
9695 if ((print_colored
|| print_found
|| print_trees
) &&
9698 print_load_info(print_colored
, print_found
, print_trees
,
9699 ncolored
, nfound
, ntrees
);
9700 a
->printed_something
= 1;
9703 } else if (!a
->loading_done
) {
9705 print_load_info(1, 1, 1, ncolored
, nfound
, ntrees
);
9707 a
->loading_done
= 1;
9710 if (fmt_scaled(packfile_size
, scaled_packsize
) == -1)
9711 return got_error_from_errno("fmt_scaled");
9712 if (fmt_scaled(bytes_sent
, scaled_sent
) == -1)
9713 return got_error_from_errno("fmt_scaled");
9715 if (a
->last_ncommits
!= ncommits
) {
9716 print_searching
= 1;
9717 a
->last_ncommits
= ncommits
;
9720 if (a
->last_nobj_total
!= nobj_total
) {
9721 print_searching
= 1;
9723 a
->last_nobj_total
= nobj_total
;
9726 if (packfile_size
> 0 && (a
->last_scaled_packsize
[0] == '\0' ||
9727 strcmp(scaled_packsize
, a
->last_scaled_packsize
)) != 0) {
9728 if (strlcpy(a
->last_scaled_packsize
, scaled_packsize
,
9729 FMT_SCALED_STRSIZE
) >= FMT_SCALED_STRSIZE
)
9730 return got_error(GOT_ERR_NO_SPACE
);
9733 if (nobj_deltify
> 0 || nobj_written
> 0) {
9734 if (nobj_deltify
> 0) {
9735 p_deltify
= (nobj_deltify
* 100) / nobj_total
;
9736 if (p_deltify
!= a
->last_p_deltify
) {
9737 a
->last_p_deltify
= p_deltify
;
9738 print_searching
= 1;
9743 if (nobj_written
> 0) {
9744 p_written
= (nobj_written
* 100) / nobj_total
;
9745 if (p_written
!= a
->last_p_written
) {
9746 a
->last_p_written
= p_written
;
9747 print_searching
= 1;
9755 if (bytes_sent
> 0) {
9756 p_sent
= (bytes_sent
* 100) / packfile_size
;
9757 if (p_sent
!= a
->last_p_sent
) {
9758 a
->last_p_sent
= p_sent
;
9759 print_searching
= 1;
9765 a
->sent_something
= 1;
9768 if (print_searching
|| print_total
|| print_deltify
|| print_written
||
9771 if (print_searching
)
9772 printf("packing %d reference%s", ncommits
,
9773 ncommits
== 1 ? "" : "s");
9775 printf("; %d object%s", nobj_total
,
9776 nobj_total
== 1 ? "" : "s");
9778 printf("; deltify: %d%%", p_deltify
);
9780 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE
- 2,
9781 scaled_packsize
, p_sent
);
9782 else if (print_written
)
9783 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE
- 2,
9784 scaled_packsize
, p_written
);
9785 if (print_searching
|| print_total
|| print_deltify
||
9786 print_written
|| print_sent
) {
9787 a
->printed_something
= 1;
9793 static const struct got_error
*
9794 cmd_send(int argc
, char *argv
[])
9796 const struct got_error
*error
= NULL
;
9797 char *cwd
= NULL
, *repo_path
= NULL
;
9798 const char *remote_name
;
9799 char *proto
= NULL
, *host
= NULL
, *port
= NULL
;
9800 char *repo_name
= NULL
, *server_path
= NULL
;
9801 const struct got_remote_repo
*remotes
;
9802 struct got_remote_repo
*remote
= NULL
;
9803 int nremotes
, nbranches
= 0, ndelete_branches
= 0;
9804 struct got_repository
*repo
= NULL
;
9805 struct got_worktree
*worktree
= NULL
;
9806 const struct got_gotconfig
*repo_conf
= NULL
, *worktree_conf
= NULL
;
9807 struct got_pathlist_head branches
;
9808 struct got_pathlist_head tags
;
9809 struct got_reflist_head all_branches
;
9810 struct got_reflist_head all_tags
;
9811 struct got_pathlist_head delete_args
;
9812 struct got_pathlist_head delete_branches
;
9813 struct got_reflist_entry
*re
;
9814 struct got_pathlist_entry
*pe
;
9815 int i
, ch
, sendfd
= -1, sendstatus
;
9817 struct got_send_progress_arg spa
;
9818 int verbosity
= 0, overwrite_refs
= 0;
9819 int send_all_branches
= 0, send_all_tags
= 0;
9820 struct got_reference
*ref
= NULL
;
9821 int *pack_fds
= NULL
;
9823 TAILQ_INIT(&branches
);
9825 TAILQ_INIT(&all_branches
);
9826 TAILQ_INIT(&all_tags
);
9827 TAILQ_INIT(&delete_args
);
9828 TAILQ_INIT(&delete_branches
);
9830 while ((ch
= getopt(argc
, argv
, "ab:d:fqr:Tt:v")) != -1) {
9833 send_all_branches
= 1;
9836 error
= got_pathlist_append(&branches
, optarg
, NULL
);
9842 error
= got_pathlist_append(&delete_args
, optarg
, NULL
);
9853 repo_path
= realpath(optarg
, NULL
);
9854 if (repo_path
== NULL
)
9855 return got_error_from_errno2("realpath",
9857 got_path_strip_trailing_slashes(repo_path
);
9863 error
= got_pathlist_append(&tags
, optarg
, NULL
);
9870 else if (verbosity
< 3)
9881 if (send_all_branches
&& !TAILQ_EMPTY(&branches
))
9882 option_conflict('a', 'b');
9883 if (send_all_tags
&& !TAILQ_EMPTY(&tags
))
9884 option_conflict('T', 't');
9888 remote_name
= GOT_SEND_DEFAULT_REMOTE_NAME
;
9890 remote_name
= argv
[0];
9894 cwd
= getcwd(NULL
, 0);
9896 error
= got_error_from_errno("getcwd");
9900 error
= got_repo_pack_fds_open(&pack_fds
);
9904 if (repo_path
== NULL
) {
9905 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
9906 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
9912 strdup(got_worktree_get_repo_path(worktree
));
9913 if (repo_path
== NULL
)
9914 error
= got_error_from_errno("strdup");
9918 repo_path
= strdup(cwd
);
9919 if (repo_path
== NULL
) {
9920 error
= got_error_from_errno("strdup");
9926 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
9931 worktree_conf
= got_worktree_get_gotconfig(worktree
);
9932 if (worktree_conf
) {
9933 got_gotconfig_get_remotes(&nremotes
, &remotes
,
9935 for (i
= 0; i
< nremotes
; i
++) {
9936 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
9937 error
= got_repo_remote_repo_dup(&remote
,
9946 if (remote
== NULL
) {
9947 repo_conf
= got_repo_get_gotconfig(repo
);
9949 got_gotconfig_get_remotes(&nremotes
, &remotes
,
9951 for (i
= 0; i
< nremotes
; i
++) {
9952 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
9953 error
= got_repo_remote_repo_dup(&remote
,
9962 if (remote
== NULL
) {
9963 got_repo_get_gitconfig_remotes(&nremotes
, &remotes
, repo
);
9964 for (i
= 0; i
< nremotes
; i
++) {
9965 if (strcmp(remotes
[i
].name
, remote_name
) == 0) {
9966 error
= got_repo_remote_repo_dup(&remote
,
9974 if (remote
== NULL
) {
9975 error
= got_error_path(remote_name
, GOT_ERR_NO_REMOTE
);
9979 error
= got_dial_parse_uri(&proto
, &host
, &port
, &server_path
,
9980 &repo_name
, remote
->send_url
);
9984 if (strcmp(proto
, "git") == 0) {
9986 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9987 "sendfd dns inet unveil", NULL
) == -1)
9990 } else if (strcmp(proto
, "git+ssh") == 0 ||
9991 strcmp(proto
, "ssh") == 0) {
9993 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9994 "sendfd unveil", NULL
) == -1)
9997 } else if (strcmp(proto
, "http") == 0 ||
9998 strcmp(proto
, "git+http") == 0) {
9999 error
= got_error_path(proto
, GOT_ERR_NOT_IMPL
);
10002 error
= got_error_path(proto
, GOT_ERR_BAD_PROTO
);
10006 error
= got_dial_apply_unveil(proto
);
10010 error
= apply_unveil(got_repo_get_path(repo
), 0, NULL
);
10014 if (send_all_branches
) {
10015 error
= got_ref_list(&all_branches
, repo
, "refs/heads",
10016 got_ref_cmp_by_name
, NULL
);
10019 TAILQ_FOREACH(re
, &all_branches
, entry
) {
10020 const char *branchname
= got_ref_get_name(re
->ref
);
10021 error
= got_pathlist_append(&branches
,
10027 } else if (nbranches
== 0) {
10028 for (i
= 0; i
< remote
->nsend_branches
; i
++) {
10029 error
= got_pathlist_append(&branches
,
10030 remote
->send_branches
[i
], NULL
);
10036 if (send_all_tags
) {
10037 error
= got_ref_list(&all_tags
, repo
, "refs/tags",
10038 got_ref_cmp_by_name
, NULL
);
10041 TAILQ_FOREACH(re
, &all_tags
, entry
) {
10042 const char *tagname
= got_ref_get_name(re
->ref
);
10043 error
= got_pathlist_append(&tags
,
10051 * To prevent accidents only branches in refs/heads/ can be deleted
10052 * with 'got send -d'.
10053 * Deleting anything else requires local repository access or Git.
10055 TAILQ_FOREACH(pe
, &delete_args
, entry
) {
10056 const char *branchname
= pe
->path
;
10058 struct got_pathlist_entry
*new;
10059 if (strncmp(branchname
, "refs/heads/", 11) == 0) {
10060 s
= strdup(branchname
);
10062 error
= got_error_from_errno("strdup");
10066 if (asprintf(&s
, "refs/heads/%s", branchname
) == -1) {
10067 error
= got_error_from_errno("asprintf");
10071 error
= got_pathlist_insert(&new, &delete_branches
, s
, NULL
);
10072 if (error
|| new == NULL
/* duplicate */)
10076 ndelete_branches
++;
10079 if (nbranches
== 0 && ndelete_branches
== 0) {
10080 struct got_reference
*head_ref
;
10082 error
= got_ref_open(&head_ref
, repo
,
10083 got_worktree_get_head_ref_name(worktree
), 0);
10085 error
= got_ref_open(&head_ref
, repo
, GOT_REF_HEAD
, 0);
10088 if (got_ref_is_symbolic(head_ref
)) {
10089 error
= got_ref_resolve_symbolic(&ref
, repo
, head_ref
);
10090 got_ref_close(head_ref
);
10095 error
= got_pathlist_append(&branches
, got_ref_get_name(ref
),
10103 /* Release work tree lock. */
10104 got_worktree_close(worktree
);
10108 if (verbosity
>= 0) {
10109 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10110 remote
->name
, proto
, host
,
10111 port
? ":" : "", port
? port
: "",
10112 *server_path
== '/' ? "" : "/", server_path
);
10115 error
= got_send_connect(&sendpid
, &sendfd
, proto
, host
, port
,
10116 server_path
, verbosity
);
10120 memset(&spa
, 0, sizeof(spa
));
10121 spa
.last_scaled_packsize
[0] = '\0';
10122 spa
.last_p_deltify
= -1;
10123 spa
.last_p_written
= -1;
10124 spa
.verbosity
= verbosity
;
10125 spa
.delete_branches
= &delete_branches
;
10126 error
= got_send_pack(remote_name
, &branches
, &tags
, &delete_branches
,
10127 verbosity
, overwrite_refs
, sendfd
, repo
, send_progress
, &spa
,
10128 check_cancelled
, NULL
);
10129 if (spa
.printed_something
)
10133 if (!spa
.sent_something
&& verbosity
>= 0)
10134 printf("Already up-to-date\n");
10137 if (kill(sendpid
, SIGTERM
) == -1)
10138 error
= got_error_from_errno("kill");
10139 if (waitpid(sendpid
, &sendstatus
, 0) == -1 && error
== NULL
)
10140 error
= got_error_from_errno("waitpid");
10142 if (sendfd
!= -1 && close(sendfd
) == -1 && error
== NULL
)
10143 error
= got_error_from_errno("close");
10145 const struct got_error
*close_err
= got_repo_close(repo
);
10150 got_worktree_close(worktree
);
10152 const struct got_error
*pack_err
=
10153 got_repo_pack_fds_close(pack_fds
);
10158 got_ref_close(ref
);
10159 got_repo_free_remote_repo_data(remote
);
10161 got_pathlist_free(&branches
, GOT_PATHLIST_FREE_NONE
);
10162 got_pathlist_free(&tags
, GOT_PATHLIST_FREE_NONE
);
10163 got_ref_list_free(&all_branches
);
10164 got_ref_list_free(&all_tags
);
10165 got_pathlist_free(&delete_args
, GOT_PATHLIST_FREE_NONE
);
10166 got_pathlist_free(&delete_branches
, GOT_PATHLIST_FREE_PATH
);
10178 * Print and if delete is set delete all ref_prefix references.
10179 * If wanted_ref is not NULL, only print or delete this reference.
10181 static const struct got_error
*
10182 process_logmsg_refs(const char *ref_prefix
, size_t prefix_len
,
10183 const char *wanted_ref
, int delete, struct got_worktree
*worktree
,
10184 struct got_repository
*repo
)
10186 const struct got_error
*err
;
10187 struct got_pathlist_head paths
;
10188 struct got_reflist_head refs
;
10189 struct got_reflist_entry
*re
;
10190 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
10191 struct got_commit_object
*commit
= NULL
;
10192 struct got_object_id
*id
= NULL
;
10193 const char *header_prefix
;
10194 char *uuidstr
= NULL
;
10198 TAILQ_INIT(&paths
);
10200 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, repo
);
10204 err
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
10208 if (worktree
!= NULL
) {
10209 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
10215 if (strncmp(wanted_ref
, "refs/heads/", 11) == 0)
10219 if (strcmp(ref_prefix
, GOT_WORKTREE_BACKOUT_REF_PREFIX
) == 0)
10220 header_prefix
= "backout";
10222 header_prefix
= "cherrypick";
10224 TAILQ_FOREACH(re
, &refs
, entry
) {
10225 const char *refname
, *wt
;
10227 refname
= got_ref_get_name(re
->ref
);
10229 err
= check_cancelled(NULL
);
10233 if (strncmp(refname
, ref_prefix
, prefix_len
) == 0)
10234 refname
+= prefix_len
+ 1; /* skip '-' delimiter */
10240 if (worktree
== NULL
|| strncmp(refname
, uuidstr
,
10241 GOT_WORKTREE_UUID_STRLEN
) == 0)
10242 refname
+= GOT_WORKTREE_UUID_STRLEN
+ 1; /* skip '-' */
10246 err
= got_repo_match_object_id(&id
, NULL
, refname
,
10247 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
10251 err
= got_object_open_as_commit(&commit
, repo
, id
);
10256 found
= strncmp(wanted_ref
, refname
,
10257 strlen(wanted_ref
)) == 0;
10258 if (wanted_ref
&& !found
) {
10259 struct got_reflist_head
*ci_refs
;
10261 ci_refs
= got_reflist_object_id_map_lookup(refs_idmap
,
10265 char *refs_str
= NULL
;
10266 char const *r
= NULL
;
10268 err
= build_refs_str(&refs_str
, ci_refs
, id
,
10275 if (strncmp(r
, wanted_ref
,
10276 strlen(wanted_ref
)) == 0) {
10280 r
= strchr(r
, ' ');
10288 if (wanted_ref
== NULL
|| found
) {
10290 err
= got_ref_delete(re
->ref
, repo
);
10293 printf("Deleted: ");
10294 err
= print_commit_oneline(commit
, id
, repo
,
10298 * Print paths modified by commit to help
10299 * associate commits with worktree changes.
10301 err
= get_changed_paths(&paths
, commit
,
10306 err
= print_commit(commit
, id
, repo
, NULL
,
10307 &paths
, NULL
, 0, 0, refs_idmap
, NULL
,
10309 got_pathlist_free(&paths
,
10310 GOT_PATHLIST_FREE_ALL
);
10312 if (worktree
== NULL
)
10313 printf("work tree: %.*s\n\n",
10314 GOT_WORKTREE_UUID_STRLEN
, wt
);
10320 got_object_commit_close(commit
);
10326 if (wanted_ref
!= NULL
&& !found
)
10327 err
= got_error_fmt(GOT_ERR_NOT_REF
, "%s", wanted_ref
);
10332 got_ref_list_free(&refs
);
10333 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_ALL
);
10335 got_reflist_object_id_map_free(refs_idmap
);
10337 got_object_commit_close(commit
);
10342 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10343 * identified by id for log messages to prepopulate the editor on commit.
10345 static const struct got_error
*
10346 logmsg_ref(struct got_object_id
*id
, const char *prefix
,
10347 struct got_worktree
*worktree
, struct got_repository
*repo
)
10349 const struct got_error
*err
= NULL
;
10350 char *idstr
, *ref
= NULL
, *refname
= NULL
;
10351 int histedit_in_progress
;
10352 int rebase_in_progress
, merge_in_progress
;
10355 * Silenty refuse to create merge reference if any histedit, merge,
10356 * or rebase operation is in progress.
10358 err
= got_worktree_histedit_in_progress(&histedit_in_progress
,
10362 if (histedit_in_progress
)
10365 err
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
10368 if (rebase_in_progress
)
10371 err
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
10375 if (merge_in_progress
)
10378 err
= got_object_id_str(&idstr
, id
);
10382 err
= got_worktree_get_logmsg_ref_name(&refname
, worktree
, prefix
);
10386 if (asprintf(&ref
, "%s-%s", refname
, idstr
) == -1) {
10387 err
= got_error_from_errno("asprintf");
10391 err
= create_ref(ref
, got_worktree_get_base_commit_id(worktree
),
10401 usage_cherrypick(void)
10403 fprintf(stderr
, "usage: %s cherrypick [-lX] [commit-id]\n",
10408 static const struct got_error
*
10409 cmd_cherrypick(int argc
, char *argv
[])
10411 const struct got_error
*error
= NULL
;
10412 struct got_worktree
*worktree
= NULL
;
10413 struct got_repository
*repo
= NULL
;
10414 char *cwd
= NULL
, *commit_id_str
= NULL
, *keyword_idstr
= NULL
;
10415 struct got_object_id
*commit_id
= NULL
;
10416 struct got_commit_object
*commit
= NULL
;
10417 struct got_object_qid
*pid
;
10418 int ch
, list_refs
= 0, remove_refs
= 0;
10419 struct got_update_progress_arg upa
;
10420 int *pack_fds
= NULL
;
10423 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10424 "unveil", NULL
) == -1)
10428 while ((ch
= getopt(argc
, argv
, "lX")) != -1) {
10437 usage_cherrypick();
10445 if (list_refs
|| remove_refs
) {
10446 if (argc
!= 0 && argc
!= 1)
10447 usage_cherrypick();
10448 } else if (argc
!= 1)
10449 usage_cherrypick();
10450 if (list_refs
&& remove_refs
)
10451 option_conflict('l', 'X');
10453 cwd
= getcwd(NULL
, 0);
10455 error
= got_error_from_errno("getcwd");
10459 error
= got_repo_pack_fds_open(&pack_fds
);
10463 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
10465 if (list_refs
|| remove_refs
) {
10466 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
10469 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
10470 error
= wrap_not_worktree_error(error
,
10471 "cherrypick", cwd
);
10476 error
= got_repo_open(&repo
,
10477 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
10482 error
= apply_unveil(got_repo_get_path(repo
), 0,
10483 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
10487 if (list_refs
|| remove_refs
) {
10488 error
= process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
10489 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
,
10490 argc
== 1 ? argv
[0] : NULL
, remove_refs
, worktree
, repo
);
10494 error
= got_keyword_to_idstr(&keyword_idstr
, argv
[0], repo
, worktree
);
10498 error
= got_repo_match_object_id(&commit_id
, NULL
,
10499 keyword_idstr
!= NULL
? keyword_idstr
: argv
[0],
10500 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
10503 error
= got_object_id_str(&commit_id_str
, commit_id
);
10507 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
10510 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
10511 memset(&upa
, 0, sizeof(upa
));
10512 error
= got_worktree_merge_files(worktree
, pid
? &pid
->id
: NULL
,
10513 commit_id
, repo
, update_progress
, &upa
, check_cancelled
,
10518 if (upa
.did_something
) {
10519 error
= logmsg_ref(commit_id
,
10520 GOT_WORKTREE_CHERRYPICK_REF_PREFIX
, worktree
, repo
);
10523 printf("Merged commit %s\n", commit_id_str
);
10525 print_merge_progress_stats(&upa
);
10528 free(keyword_idstr
);
10530 got_object_commit_close(commit
);
10531 free(commit_id_str
);
10533 got_worktree_close(worktree
);
10535 const struct got_error
*close_err
= got_repo_close(repo
);
10540 const struct got_error
*pack_err
=
10541 got_repo_pack_fds_close(pack_fds
);
10550 usage_backout(void)
10552 fprintf(stderr
, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10556 static const struct got_error
*
10557 cmd_backout(int argc
, char *argv
[])
10559 const struct got_error
*error
= NULL
;
10560 struct got_worktree
*worktree
= NULL
;
10561 struct got_repository
*repo
= NULL
;
10562 char *cwd
= NULL
, *commit_id_str
= NULL
, *keyword_idstr
= NULL
;
10563 struct got_object_id
*commit_id
= NULL
;
10564 struct got_commit_object
*commit
= NULL
;
10565 struct got_object_qid
*pid
;
10566 int ch
, list_refs
= 0, remove_refs
= 0;
10567 struct got_update_progress_arg upa
;
10568 int *pack_fds
= NULL
;
10571 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10572 "unveil", NULL
) == -1)
10576 while ((ch
= getopt(argc
, argv
, "lX")) != -1) {
10593 if (list_refs
|| remove_refs
) {
10594 if (argc
!= 0 && argc
!= 1)
10596 } else if (argc
!= 1)
10598 if (list_refs
&& remove_refs
)
10599 option_conflict('l', 'X');
10601 cwd
= getcwd(NULL
, 0);
10603 error
= got_error_from_errno("getcwd");
10607 error
= got_repo_pack_fds_open(&pack_fds
);
10611 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
10613 if (list_refs
|| remove_refs
) {
10614 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
10617 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
10618 error
= wrap_not_worktree_error(error
,
10624 error
= got_repo_open(&repo
,
10625 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
10630 error
= apply_unveil(got_repo_get_path(repo
), 0,
10631 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
10635 if (list_refs
|| remove_refs
) {
10636 error
= process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX
,
10637 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
,
10638 argc
== 1 ? argv
[0] : NULL
, remove_refs
, worktree
, repo
);
10642 error
= got_keyword_to_idstr(&keyword_idstr
, argv
[0], repo
, worktree
);
10646 error
= got_repo_match_object_id(&commit_id
, NULL
,
10647 keyword_idstr
!= NULL
? keyword_idstr
: argv
[0],
10648 GOT_OBJ_TYPE_COMMIT
, NULL
, repo
);
10651 error
= got_object_id_str(&commit_id_str
, commit_id
);
10655 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
10658 pid
= STAILQ_FIRST(got_object_commit_get_parent_ids(commit
));
10660 error
= got_error(GOT_ERR_ROOT_COMMIT
);
10664 memset(&upa
, 0, sizeof(upa
));
10665 error
= got_worktree_merge_files(worktree
, commit_id
, &pid
->id
,
10666 repo
, update_progress
, &upa
, check_cancelled
, NULL
);
10670 if (upa
.did_something
) {
10671 error
= logmsg_ref(commit_id
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
10675 printf("Backed out commit %s\n", commit_id_str
);
10677 print_merge_progress_stats(&upa
);
10680 free(keyword_idstr
);
10682 got_object_commit_close(commit
);
10683 free(commit_id_str
);
10685 got_worktree_close(worktree
);
10687 const struct got_error
*close_err
= got_repo_close(repo
);
10692 const struct got_error
*pack_err
=
10693 got_repo_pack_fds_close(pack_fds
);
10703 fprintf(stderr
, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10708 trim_logmsg(char *logmsg
, int limit
)
10713 len
= strlen(logmsg
);
10716 logmsg
[len
] = '\0';
10717 nl
= strchr(logmsg
, '\n');
10722 static const struct got_error
*
10723 get_short_logmsg(char **logmsg
, int limit
, struct got_commit_object
*commit
)
10725 const struct got_error
*err
;
10726 char *logmsg0
= NULL
;
10729 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
10734 while (isspace((unsigned char)s
[0]))
10737 *logmsg
= strdup(s
);
10738 if (*logmsg
== NULL
) {
10739 err
= got_error_from_errno("strdup");
10743 trim_logmsg(*logmsg
, limit
);
10749 static const struct got_error
*
10750 show_rebase_merge_conflict(struct got_object_id
*id
,
10751 struct got_repository
*repo
)
10753 const struct got_error
*err
;
10754 struct got_commit_object
*commit
= NULL
;
10755 char *id_str
= NULL
, *logmsg
= NULL
;
10757 err
= got_object_open_as_commit(&commit
, repo
, id
);
10761 err
= got_object_id_str(&id_str
, id
);
10767 err
= get_short_logmsg(&logmsg
, 42, commit
);
10771 printf("%s -> merge conflict: %s\n", id_str
, logmsg
);
10774 got_object_commit_close(commit
);
10779 static const struct got_error
*
10780 show_rebase_progress(struct got_commit_object
*commit
,
10781 struct got_object_id
*old_id
, struct got_object_id
*new_id
)
10783 const struct got_error
*err
;
10784 char *old_id_str
= NULL
, *new_id_str
= NULL
, *logmsg
= NULL
;
10786 err
= got_object_id_str(&old_id_str
, old_id
);
10791 err
= got_object_id_str(&new_id_str
, new_id
);
10796 old_id_str
[12] = '\0';
10798 new_id_str
[12] = '\0';
10800 err
= get_short_logmsg(&logmsg
, 42, commit
);
10804 printf("%s -> %s: %s\n", old_id_str
,
10805 new_id_str
? new_id_str
: "no-op change", logmsg
);
10813 static const struct got_error
*
10814 rebase_complete(struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
10815 struct got_reference
*branch
, struct got_reference
*tmp_branch
,
10816 struct got_repository
*repo
, int create_backup
)
10818 printf("Switching work tree to %s\n", got_ref_get_name(branch
));
10819 return got_worktree_rebase_complete(worktree
, fileindex
,
10820 tmp_branch
, branch
, repo
, create_backup
);
10823 static const struct got_error
*
10824 rebase_commit(struct got_pathlist_head
*merged_paths
,
10825 struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
10826 struct got_reference
*tmp_branch
, const char *committer
,
10827 struct got_object_id
*commit_id
, int allow_conflict
,
10828 struct got_repository
*repo
)
10830 const struct got_error
*error
;
10831 struct got_commit_object
*commit
;
10832 struct got_object_id
*new_commit_id
;
10834 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
10838 error
= got_worktree_rebase_commit(&new_commit_id
, merged_paths
,
10839 worktree
, fileindex
, tmp_branch
, committer
, commit
, commit_id
,
10840 allow_conflict
, repo
);
10842 if (error
->code
!= GOT_ERR_COMMIT_NO_CHANGES
)
10844 error
= show_rebase_progress(commit
, commit_id
, NULL
);
10846 error
= show_rebase_progress(commit
, commit_id
, new_commit_id
);
10847 free(new_commit_id
);
10850 got_object_commit_close(commit
);
10854 struct check_path_prefix_arg
{
10855 const char *path_prefix
;
10860 static const struct got_error
*
10861 check_path_prefix_in_diff(void *arg
, struct got_blob_object
*blob1
,
10862 struct got_blob_object
*blob2
, FILE *f1
, FILE *f2
,
10863 struct got_object_id
*id1
, struct got_object_id
*id2
,
10864 const char *path1
, const char *path2
,
10865 mode_t mode1
, mode_t mode2
, struct got_repository
*repo
)
10867 struct check_path_prefix_arg
*a
= arg
;
10869 if ((path1
&& !got_path_is_child(path1
, a
->path_prefix
, a
->len
)) ||
10870 (path2
&& !got_path_is_child(path2
, a
->path_prefix
, a
->len
)))
10871 return got_error(a
->errcode
);
10876 static const struct got_error
*
10877 check_path_prefix(struct got_object_id
*parent_id
,
10878 struct got_object_id
*commit_id
, const char *path_prefix
,
10879 int errcode
, struct got_repository
*repo
)
10881 const struct got_error
*err
;
10882 struct got_tree_object
*tree1
= NULL
, *tree2
= NULL
;
10883 struct got_commit_object
*commit
= NULL
, *parent_commit
= NULL
;
10884 struct check_path_prefix_arg cpp_arg
;
10886 if (got_path_is_root_dir(path_prefix
))
10889 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
10893 err
= got_object_open_as_commit(&parent_commit
, repo
, parent_id
);
10897 err
= got_object_open_as_tree(&tree1
, repo
,
10898 got_object_commit_get_tree_id(parent_commit
));
10902 err
= got_object_open_as_tree(&tree2
, repo
,
10903 got_object_commit_get_tree_id(commit
));
10907 cpp_arg
.path_prefix
= path_prefix
;
10908 while (cpp_arg
.path_prefix
[0] == '/')
10909 cpp_arg
.path_prefix
++;
10910 cpp_arg
.len
= strlen(cpp_arg
.path_prefix
);
10911 cpp_arg
.errcode
= errcode
;
10912 err
= got_diff_tree(tree1
, tree2
, NULL
, NULL
, -1, -1, "", "", repo
,
10913 check_path_prefix_in_diff
, &cpp_arg
, 0);
10916 got_object_tree_close(tree1
);
10918 got_object_tree_close(tree2
);
10920 got_object_commit_close(commit
);
10922 got_object_commit_close(parent_commit
);
10926 static const struct got_error
*
10927 collect_commits(struct got_object_id_queue
*commits
,
10928 struct got_object_id
*initial_commit_id
,
10929 struct got_object_id
*iter_start_id
, struct got_object_id
*iter_stop_id
,
10930 const char *path_prefix
, int path_prefix_errcode
,
10931 struct got_repository
*repo
)
10933 const struct got_error
*err
= NULL
;
10934 struct got_commit_graph
*graph
= NULL
;
10935 struct got_object_id parent_id
, commit_id
;
10936 struct got_object_qid
*qid
;
10938 err
= got_commit_graph_open(&graph
, "/", 1);
10942 err
= got_commit_graph_bfsort(graph
, iter_start_id
, repo
,
10943 check_cancelled
, NULL
);
10947 memcpy(&commit_id
, initial_commit_id
, sizeof(commit_id
));
10948 while (got_object_id_cmp(&commit_id
, iter_stop_id
) != 0) {
10949 err
= got_commit_graph_iter_next(&parent_id
, graph
, repo
,
10950 check_cancelled
, NULL
);
10952 if (err
->code
== GOT_ERR_ITER_COMPLETED
) {
10953 err
= got_error_msg(GOT_ERR_ANCESTRY
,
10954 "ran out of commits to rebase before "
10955 "youngest common ancestor commit has "
10956 "been reached?!?");
10960 err
= check_path_prefix(&parent_id
, &commit_id
,
10961 path_prefix
, path_prefix_errcode
, repo
);
10965 err
= got_object_qid_alloc(&qid
, &commit_id
);
10968 STAILQ_INSERT_HEAD(commits
, qid
, entry
);
10970 memcpy(&commit_id
, &parent_id
, sizeof(commit_id
));
10974 got_commit_graph_close(graph
);
10978 static const struct got_error
*
10979 get_commit_brief_str(char **brief_str
, struct got_commit_object
*commit
)
10981 const struct got_error
*err
= NULL
;
10982 time_t committer_time
;
10984 char datebuf
[11]; /* YYYY-MM-DD + NUL */
10985 char *author0
= NULL
, *author
, *smallerthan
;
10986 char *logmsg0
= NULL
, *logmsg
, *newline
;
10988 committer_time
= got_object_commit_get_committer_time(commit
);
10989 if (gmtime_r(&committer_time
, &tm
) == NULL
)
10990 return got_error_from_errno("gmtime_r");
10991 if (strftime(datebuf
, sizeof(datebuf
), "%F", &tm
) == 0)
10992 return got_error(GOT_ERR_NO_SPACE
);
10994 author0
= strdup(got_object_commit_get_author(commit
));
10995 if (author0
== NULL
)
10996 return got_error_from_errno("strdup");
10998 smallerthan
= strchr(author
, '<');
10999 if (smallerthan
&& smallerthan
[1] != '\0')
11000 author
= smallerthan
+ 1;
11001 author
[strcspn(author
, "@>")] = '\0';
11003 err
= got_object_commit_get_logmsg(&logmsg0
, commit
);
11007 while (*logmsg
== '\n')
11009 newline
= strchr(logmsg
, '\n');
11013 if (asprintf(brief_str
, "%s %s %s",
11014 datebuf
, author
, logmsg
) == -1)
11015 err
= got_error_from_errno("asprintf");
11022 static const struct got_error
*
11023 delete_backup_ref(struct got_reference
*ref
, struct got_object_id
*id
,
11024 struct got_repository
*repo
)
11026 const struct got_error
*err
;
11029 err
= got_object_id_str(&id_str
, id
);
11033 err
= got_ref_delete(ref
, repo
);
11037 printf("Deleted %s: %s\n", got_ref_get_name(ref
), id_str
);
11043 static const struct got_error
*
11044 print_backup_ref(const char *branch_name
, const char *new_id_str
,
11045 struct got_object_id
*old_commit_id
, struct got_commit_object
*old_commit
,
11046 struct got_reflist_object_id_map
*refs_idmap
,
11047 struct got_repository
*repo
)
11049 const struct got_error
*err
= NULL
;
11050 struct got_reflist_head
*refs
;
11051 char *refs_str
= NULL
;
11052 struct got_object_id
*new_commit_id
= NULL
;
11053 struct got_commit_object
*new_commit
= NULL
;
11054 char *new_commit_brief_str
= NULL
;
11055 struct got_object_id
*yca_id
= NULL
;
11056 struct got_commit_object
*yca_commit
= NULL
;
11057 char *yca_id_str
= NULL
, *yca_brief_str
= NULL
;
11058 char *custom_refs_str
;
11060 if (asprintf(&custom_refs_str
, "formerly %s", branch_name
) == -1)
11061 return got_error_from_errno("asprintf");
11063 err
= print_commit(old_commit
, old_commit_id
, repo
, NULL
, NULL
, NULL
,
11064 0, 0, refs_idmap
, custom_refs_str
, NULL
);
11068 err
= got_object_resolve_id_str(&new_commit_id
, repo
, new_id_str
);
11072 refs
= got_reflist_object_id_map_lookup(refs_idmap
, new_commit_id
);
11074 err
= build_refs_str(&refs_str
, refs
, new_commit_id
, repo
, 0);
11079 err
= got_object_open_as_commit(&new_commit
, repo
, new_commit_id
);
11083 err
= get_commit_brief_str(&new_commit_brief_str
, new_commit
);
11087 err
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
11088 old_commit_id
, new_commit_id
, 1, 0, repo
, check_cancelled
, NULL
);
11092 printf("has become commit %s%s%s%s\n %s\n", new_id_str
,
11093 refs_str
? " (" : "", refs_str
? refs_str
: "",
11094 refs_str
? ")" : "", new_commit_brief_str
);
11095 if (yca_id
&& got_object_id_cmp(yca_id
, new_commit_id
) != 0 &&
11096 got_object_id_cmp(yca_id
, old_commit_id
) != 0) {
11100 err
= got_object_open_as_commit(&yca_commit
, repo
, yca_id
);
11104 err
= get_commit_brief_str(&yca_brief_str
, yca_commit
);
11108 err
= got_object_id_str(&yca_id_str
, yca_id
);
11112 refs
= got_reflist_object_id_map_lookup(refs_idmap
, yca_id
);
11114 err
= build_refs_str(&refs_str
, refs
, yca_id
, repo
, 0);
11118 printf("history forked at %s%s%s%s\n %s\n",
11120 refs_str
? " (" : "", refs_str
? refs_str
: "",
11121 refs_str
? ")" : "", yca_brief_str
);
11124 free(custom_refs_str
);
11125 free(new_commit_id
);
11129 free(yca_brief_str
);
11131 got_object_commit_close(new_commit
);
11133 got_object_commit_close(yca_commit
);
11138 static const struct got_error
*
11139 worktree_has_logmsg_ref(const char *caller
, struct got_worktree
*worktree
,
11140 struct got_repository
*repo
)
11142 const struct got_error
*err
;
11143 struct got_reflist_head refs
;
11144 struct got_reflist_entry
*re
;
11145 char *uuidstr
= NULL
;
11146 static char msg
[160];
11150 err
= got_worktree_get_uuid(&uuidstr
, worktree
);
11154 err
= got_ref_list(&refs
, repo
, "refs/got/worktree",
11155 got_ref_cmp_by_name
, repo
);
11159 TAILQ_FOREACH(re
, &refs
, entry
) {
11160 const char *cmd
, *refname
, *type
;
11162 refname
= got_ref_get_name(re
->ref
);
11164 if (strncmp(refname
, GOT_WORKTREE_CHERRYPICK_REF_PREFIX
,
11165 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
) == 0) {
11166 refname
+= GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN
+ 1;
11167 cmd
= "cherrypick";
11168 type
= "cherrypicked";
11169 } else if (strncmp(refname
, GOT_WORKTREE_BACKOUT_REF_PREFIX
,
11170 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
) == 0) {
11171 refname
+= GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN
+ 1;
11173 type
= "backed-out";
11177 if (strncmp(refname
, uuidstr
, GOT_WORKTREE_UUID_STRLEN
) != 0)
11180 snprintf(msg
, sizeof(msg
),
11181 "work tree has references created by %s commits which "
11182 "must be removed with 'got %s -X' before running the %s "
11183 "command", type
, cmd
, caller
);
11184 err
= got_error_msg(GOT_ERR_WORKTREE_META
, msg
);
11190 got_ref_list_free(&refs
);
11194 static const struct got_error
*
11195 process_backup_refs(const char *backup_ref_prefix
,
11196 const char *wanted_branch_name
,
11197 int delete, struct got_repository
*repo
)
11199 const struct got_error
*err
;
11200 struct got_reflist_head refs
, backup_refs
;
11201 struct got_reflist_entry
*re
;
11202 const size_t backup_ref_prefix_len
= strlen(backup_ref_prefix
);
11203 struct got_object_id
*old_commit_id
= NULL
;
11204 char *branch_name
= NULL
;
11205 struct got_commit_object
*old_commit
= NULL
;
11206 struct got_reflist_object_id_map
*refs_idmap
= NULL
;
11207 int wanted_branch_found
= 0;
11210 TAILQ_INIT(&backup_refs
);
11212 err
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
11216 err
= got_reflist_object_id_map_create(&refs_idmap
, &refs
, repo
);
11220 if (wanted_branch_name
) {
11221 if (strncmp(wanted_branch_name
, "refs/heads/", 11) == 0)
11222 wanted_branch_name
+= 11;
11225 err
= got_ref_list(&backup_refs
, repo
, backup_ref_prefix
,
11226 got_ref_cmp_by_commit_timestamp_descending
, repo
);
11230 TAILQ_FOREACH(re
, &backup_refs
, entry
) {
11231 const char *refname
= got_ref_get_name(re
->ref
);
11234 err
= check_cancelled(NULL
);
11238 err
= got_ref_resolve(&old_commit_id
, repo
, re
->ref
);
11242 err
= got_object_open_as_commit(&old_commit
, repo
,
11247 if (strncmp(backup_ref_prefix
, refname
,
11248 backup_ref_prefix_len
) == 0)
11249 refname
+= backup_ref_prefix_len
;
11251 while (refname
[0] == '/')
11254 branch_name
= strdup(refname
);
11255 if (branch_name
== NULL
) {
11256 err
= got_error_from_errno("strdup");
11259 slash
= strrchr(branch_name
, '/');
11262 refname
+= strlen(branch_name
) + 1;
11265 if (wanted_branch_name
== NULL
||
11266 strcmp(wanted_branch_name
, branch_name
) == 0) {
11267 wanted_branch_found
= 1;
11269 err
= delete_backup_ref(re
->ref
,
11270 old_commit_id
, repo
);
11272 err
= print_backup_ref(branch_name
, refname
,
11273 old_commit_id
, old_commit
, refs_idmap
,
11280 free(old_commit_id
);
11281 old_commit_id
= NULL
;
11283 branch_name
= NULL
;
11284 got_object_commit_close(old_commit
);
11288 if (wanted_branch_name
&& !wanted_branch_found
) {
11289 err
= got_error_fmt(GOT_ERR_NOT_REF
,
11290 "%s/%s/", backup_ref_prefix
, wanted_branch_name
);
11294 got_reflist_object_id_map_free(refs_idmap
);
11295 got_ref_list_free(&refs
);
11296 got_ref_list_free(&backup_refs
);
11297 free(old_commit_id
);
11300 got_object_commit_close(old_commit
);
11304 static const struct got_error
*
11305 abort_progress(void *arg
, unsigned char status
, const char *path
)
11308 * Unversioned files should not clutter progress output when
11309 * an operation is aborted.
11311 if (status
== GOT_STATUS_UNVERSIONED
)
11314 return update_progress(arg
, status
, path
);
11317 static const struct got_error
*
11318 find_merge_commit_yca(struct got_object_id
**new_yca_id
,
11319 struct got_object_id
*branch_head_commit_id
,
11320 struct got_object_id
*yca_id
,
11321 struct got_object_id
*base_commit_id
,
11322 struct got_repository
*repo
)
11324 const struct got_error
*err
= NULL
;
11325 struct got_commit_graph
*graph
= NULL
;
11326 struct got_commit_object
*commit
= NULL
;
11328 *new_yca_id
= NULL
;
11330 err
= got_commit_graph_open(&graph
, "/", 1);
11334 err
= got_commit_graph_bfsort(graph
, base_commit_id
,
11335 repo
, check_cancelled
, NULL
);
11340 struct got_object_id id
;
11342 err
= got_commit_graph_iter_next(&id
, graph
, repo
,
11343 check_cancelled
, NULL
);
11345 if (err
->code
== GOT_ERR_ITER_COMPLETED
)
11350 err
= got_object_open_as_commit(&commit
, repo
, &id
);
11354 if (got_object_commit_get_nparents(commit
) > 1) {
11355 /* Search for a better YCA using toposort. */
11356 err
= got_commit_graph_find_youngest_common_ancestor(
11357 new_yca_id
, base_commit_id
, branch_head_commit_id
,
11358 0, 1, repo
, check_cancelled
, NULL
);
11362 if (got_object_id_cmp(&id
, yca_id
) == 0)
11364 got_object_commit_close(commit
);
11368 got_commit_graph_close(graph
);
11370 got_object_commit_close(commit
);
11374 static const struct got_error
*
11375 cmd_rebase(int argc
, char *argv
[])
11377 const struct got_error
*error
= NULL
;
11378 struct got_worktree
*worktree
= NULL
;
11379 struct got_repository
*repo
= NULL
;
11380 struct got_fileindex
*fileindex
= NULL
;
11381 char *cwd
= NULL
, *committer
= NULL
, *gitconfig_path
= NULL
;
11382 struct got_reference
*branch
= NULL
;
11383 struct got_reference
*new_base_branch
= NULL
, *tmp_branch
= NULL
;
11384 struct got_object_id
*commit_id
= NULL
, *parent_id
= NULL
;
11385 struct got_object_id
*resume_commit_id
= NULL
;
11386 struct got_object_id
*branch_head_commit_id
= NULL
, *yca_id
= NULL
;
11387 struct got_object_id
*head_commit_id
= NULL
;
11388 struct got_reference
*head_ref
= NULL
;
11389 struct got_commit_object
*commit
= NULL
;
11390 int ch
, rebase_in_progress
= 0, abort_rebase
= 0, continue_rebase
= 0;
11391 int histedit_in_progress
= 0, merge_in_progress
= 0;
11392 int create_backup
= 1, list_backups
= 0, delete_backups
= 0;
11393 int allow_conflict
= 0;
11394 struct got_object_id_queue commits
;
11395 struct got_pathlist_head merged_paths
;
11396 const struct got_object_id_queue
*parent_ids
;
11397 struct got_object_qid
*qid
, *pid
;
11398 struct got_update_progress_arg upa
;
11399 int *pack_fds
= NULL
;
11401 STAILQ_INIT(&commits
);
11402 TAILQ_INIT(&merged_paths
);
11403 memset(&upa
, 0, sizeof(upa
));
11406 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11407 "unveil", NULL
) == -1)
11411 while ((ch
= getopt(argc
, argv
, "aCclX")) != -1) {
11417 allow_conflict
= 1;
11420 continue_rebase
= 1;
11426 delete_backups
= 1;
11437 if (list_backups
) {
11439 option_conflict('l', 'a');
11440 if (allow_conflict
)
11441 option_conflict('l', 'C');
11442 if (continue_rebase
)
11443 option_conflict('l', 'c');
11444 if (delete_backups
)
11445 option_conflict('l', 'X');
11446 if (argc
!= 0 && argc
!= 1)
11448 } else if (delete_backups
) {
11450 option_conflict('X', 'a');
11451 if (allow_conflict
)
11452 option_conflict('X', 'C');
11453 if (continue_rebase
)
11454 option_conflict('X', 'c');
11456 option_conflict('l', 'X');
11457 if (argc
!= 0 && argc
!= 1)
11459 } else if (allow_conflict
) {
11461 option_conflict('C', 'a');
11462 if (!continue_rebase
)
11463 errx(1, "-C option requires -c");
11465 if (abort_rebase
&& continue_rebase
)
11467 else if (abort_rebase
|| continue_rebase
) {
11470 } else if (argc
!= 1)
11474 cwd
= getcwd(NULL
, 0);
11476 error
= got_error_from_errno("getcwd");
11480 error
= got_repo_pack_fds_open(&pack_fds
);
11484 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
11486 if (list_backups
|| delete_backups
) {
11487 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
11490 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
11491 error
= wrap_not_worktree_error(error
,
11497 error
= get_gitconfig_path(&gitconfig_path
);
11500 error
= got_repo_open(&repo
,
11501 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
11502 gitconfig_path
, pack_fds
);
11506 if (worktree
!= NULL
&& !list_backups
&& !delete_backups
) {
11507 error
= worktree_has_logmsg_ref("rebase", worktree
, repo
);
11512 error
= get_author(&committer
, repo
, worktree
);
11513 if (error
&& error
->code
!= GOT_ERR_COMMIT_NO_AUTHOR
)
11516 error
= apply_unveil(got_repo_get_path(repo
), 0,
11517 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
11521 if (list_backups
|| delete_backups
) {
11522 error
= process_backup_refs(
11523 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX
,
11524 argc
== 1 ? argv
[0] : NULL
, delete_backups
, repo
);
11525 goto done
; /* nothing else to do */
11528 error
= got_worktree_histedit_in_progress(&histedit_in_progress
,
11532 if (histedit_in_progress
) {
11533 error
= got_error(GOT_ERR_HISTEDIT_BUSY
);
11537 error
= got_worktree_merge_in_progress(&merge_in_progress
,
11541 if (merge_in_progress
) {
11542 error
= got_error(GOT_ERR_MERGE_BUSY
);
11546 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
11550 if (abort_rebase
) {
11551 if (!rebase_in_progress
) {
11552 error
= got_error(GOT_ERR_NOT_REBASING
);
11555 error
= got_worktree_rebase_continue(&resume_commit_id
,
11556 &new_base_branch
, &tmp_branch
, &branch
, &fileindex
,
11560 printf("Switching work tree to %s\n",
11561 got_ref_get_symref_target(new_base_branch
));
11562 error
= got_worktree_rebase_abort(worktree
, fileindex
, repo
,
11563 new_base_branch
, abort_progress
, &upa
);
11566 printf("Rebase of %s aborted\n", got_ref_get_name(branch
));
11567 print_merge_progress_stats(&upa
);
11568 goto done
; /* nothing else to do */
11571 if (continue_rebase
) {
11572 if (!rebase_in_progress
) {
11573 error
= got_error(GOT_ERR_NOT_REBASING
);
11576 error
= got_worktree_rebase_continue(&resume_commit_id
,
11577 &new_base_branch
, &tmp_branch
, &branch
, &fileindex
,
11582 error
= rebase_commit(NULL
, worktree
, fileindex
, tmp_branch
,
11583 committer
, resume_commit_id
, allow_conflict
, repo
);
11587 yca_id
= got_object_id_dup(resume_commit_id
);
11588 if (yca_id
== NULL
) {
11589 error
= got_error_from_errno("got_object_id_dup");
11593 error
= got_ref_open(&branch
, repo
, argv
[0], 0);
11596 if (strncmp(got_ref_get_name(branch
), "refs/heads/", 11) != 0) {
11597 error
= got_error_msg(GOT_ERR_COMMIT_BRANCH
,
11598 "will not rebase a branch which lives outside "
11599 "the \"refs/heads/\" reference namespace");
11604 error
= got_ref_resolve(&branch_head_commit_id
, repo
, branch
);
11608 if (!continue_rebase
) {
11609 struct got_object_id
*base_commit_id
;
11611 error
= got_ref_open(&head_ref
, repo
,
11612 got_worktree_get_head_ref_name(worktree
), 0);
11615 error
= got_ref_resolve(&head_commit_id
, repo
, head_ref
);
11618 base_commit_id
= got_worktree_get_base_commit_id(worktree
);
11619 if (got_object_id_cmp(base_commit_id
, head_commit_id
) != 0) {
11620 error
= got_error(GOT_ERR_REBASE_OUT_OF_DATE
);
11624 error
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
11625 base_commit_id
, branch_head_commit_id
, 1, 0,
11626 repo
, check_cancelled
, NULL
);
11628 if (error
->code
== GOT_ERR_ANCESTRY
) {
11629 error
= got_error_msg(GOT_ERR_ANCESTRY
,
11630 "specified branch shares no common "
11631 "ancestry with work tree's branch");
11637 * If a merge commit appears between the new base branch tip
11638 * and a YCA found via first-parent traversal then we might
11639 * find a better YCA using topologically sorted commits.
11641 if (got_object_id_cmp(base_commit_id
, yca_id
) != 0) {
11642 struct got_object_id
*better_yca_id
;
11643 error
= find_merge_commit_yca(&better_yca_id
,
11644 branch_head_commit_id
, yca_id
,
11645 base_commit_id
, repo
);
11648 if (better_yca_id
) {
11650 yca_id
= better_yca_id
;
11654 if (got_object_id_cmp(base_commit_id
, yca_id
) == 0) {
11655 struct got_pathlist_head paths
;
11656 const char *branch_name
= got_ref_get_name(branch
);
11658 got_worktree_get_head_ref_name(worktree
);
11660 if (strcmp(branch_name
, base
) == 0) {
11661 error
= got_error_fmt(GOT_ERR_WRONG_BRANCH
,
11662 "cannot rebase %s onto itself",
11666 printf("%s is already based on %s\n",
11667 branch_name
, base
);
11669 error
= switch_head_ref(branch
, branch_head_commit_id
,
11673 error
= got_worktree_set_base_commit_id(worktree
, repo
,
11674 branch_head_commit_id
);
11677 TAILQ_INIT(&paths
);
11678 error
= got_pathlist_append(&paths
, "", NULL
);
11681 error
= got_worktree_checkout_files(worktree
,
11682 &paths
, repo
, update_progress
, &upa
,
11683 check_cancelled
, NULL
);
11684 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
11687 if (upa
.did_something
) {
11689 error
= got_object_id_str(&id_str
,
11690 branch_head_commit_id
);
11693 printf("Updated to %s: %s\n",
11694 got_worktree_get_head_ref_name(worktree
),
11698 printf("Already up-to-date\n");
11699 print_update_progress_stats(&upa
);
11704 commit_id
= branch_head_commit_id
;
11705 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
11709 parent_ids
= got_object_commit_get_parent_ids(commit
);
11710 pid
= STAILQ_FIRST(parent_ids
);
11712 error
= collect_commits(&commits
, commit_id
, &pid
->id
,
11713 yca_id
, got_worktree_get_path_prefix(worktree
),
11714 GOT_ERR_REBASE_PATH
, repo
);
11719 got_object_commit_close(commit
);
11722 if (!continue_rebase
) {
11723 error
= got_worktree_rebase_prepare(&new_base_branch
,
11724 &tmp_branch
, &fileindex
, worktree
, branch
, repo
);
11729 if (STAILQ_EMPTY(&commits
)) {
11730 if (continue_rebase
) {
11731 error
= rebase_complete(worktree
, fileindex
,
11732 branch
, tmp_branch
, repo
, create_backup
);
11735 /* Fast-forward the reference of the branch. */
11736 struct got_object_id
*new_head_commit_id
;
11738 error
= got_ref_resolve(&new_head_commit_id
, repo
,
11742 error
= got_object_id_str(&id_str
, new_head_commit_id
);
11745 printf("Forwarding %s to commit %s\n",
11746 got_ref_get_name(branch
), id_str
);
11748 error
= got_ref_change_ref(branch
,
11749 new_head_commit_id
);
11752 /* No backup needed since objects did not change. */
11758 STAILQ_FOREACH(qid
, &commits
, entry
) {
11760 commit_id
= &qid
->id
;
11761 parent_id
= pid
? &pid
->id
: yca_id
;
11764 memset(&upa
, 0, sizeof(upa
));
11765 error
= got_worktree_rebase_merge_files(&merged_paths
,
11766 worktree
, fileindex
, parent_id
, commit_id
, repo
,
11767 update_progress
, &upa
, check_cancelled
, NULL
);
11771 print_merge_progress_stats(&upa
);
11772 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
11773 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
11774 if (upa
.conflicts
> 0) {
11775 error
= show_rebase_merge_conflict(&qid
->id
,
11780 got_pathlist_free(&merged_paths
, GOT_PATHLIST_FREE_PATH
);
11784 error
= rebase_commit(&merged_paths
, worktree
, fileindex
,
11785 tmp_branch
, committer
, commit_id
, 0, repo
);
11786 got_pathlist_free(&merged_paths
, GOT_PATHLIST_FREE_PATH
);
11791 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
11792 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
11793 error
= got_worktree_rebase_postpone(worktree
, fileindex
);
11796 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
11797 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
11798 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11799 "conflicts must be resolved before rebasing "
11801 } else if (upa
.conflicts
> 0) {
11802 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11803 "conflicts must be resolved before rebasing "
11804 "can continue; changes destined for some "
11805 "files were not yet merged and should be "
11806 "merged manually if required before the "
11807 "rebase operation is continued");
11809 error
= got_error_msg(GOT_ERR_CONFLICTS
,
11810 "changes destined for some files were not "
11811 "yet merged and should be merged manually "
11812 "if required before the rebase operation "
11816 error
= rebase_complete(worktree
, fileindex
, branch
,
11817 tmp_branch
, repo
, create_backup
);
11821 free(gitconfig_path
);
11822 got_object_id_queue_free(&commits
);
11823 free(branch_head_commit_id
);
11824 free(resume_commit_id
);
11825 free(head_commit_id
);
11828 got_object_commit_close(commit
);
11830 got_ref_close(branch
);
11831 if (new_base_branch
)
11832 got_ref_close(new_base_branch
);
11834 got_ref_close(tmp_branch
);
11836 got_ref_close(head_ref
);
11838 got_worktree_close(worktree
);
11840 const struct got_error
*close_err
= got_repo_close(repo
);
11845 const struct got_error
*pack_err
=
11846 got_repo_pack_fds_close(pack_fds
);
11854 usage_histedit(void)
11856 fprintf(stderr
, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11857 "[branch]\n", getprogname());
11861 #define GOT_HISTEDIT_PICK 'p'
11862 #define GOT_HISTEDIT_EDIT 'e'
11863 #define GOT_HISTEDIT_FOLD 'f'
11864 #define GOT_HISTEDIT_DROP 'd'
11865 #define GOT_HISTEDIT_MESG 'm'
11867 static const struct got_histedit_cmd
{
11868 unsigned char code
;
11871 } got_histedit_cmds
[] = {
11872 { GOT_HISTEDIT_PICK
, "pick", "use commit" },
11873 { GOT_HISTEDIT_EDIT
, "edit", "use commit but stop for amending" },
11874 { GOT_HISTEDIT_FOLD
, "fold", "combine with next commit that will "
11876 { GOT_HISTEDIT_DROP
, "drop", "remove commit from history" },
11877 { GOT_HISTEDIT_MESG
, "mesg", "open editor to edit the log message" },
11880 struct got_histedit_list_entry
{
11881 TAILQ_ENTRY(got_histedit_list_entry
) entry
;
11882 struct got_object_id
*commit_id
;
11883 const struct got_histedit_cmd
*cmd
;
11886 TAILQ_HEAD(got_histedit_list
, got_histedit_list_entry
);
11888 static const struct got_error
*
11889 histedit_write_commit(struct got_object_id
*commit_id
, const char *cmdname
,
11890 FILE *f
, struct got_repository
*repo
)
11892 const struct got_error
*err
= NULL
;
11893 char *logmsg
= NULL
, *id_str
= NULL
;
11894 struct got_commit_object
*commit
= NULL
;
11897 err
= got_object_open_as_commit(&commit
, repo
, commit_id
);
11901 err
= get_short_logmsg(&logmsg
, 34, commit
);
11905 err
= got_object_id_str(&id_str
, commit_id
);
11909 n
= fprintf(f
, "%s %s %s\n", cmdname
, id_str
, logmsg
);
11911 err
= got_ferror(f
, GOT_ERR_IO
);
11914 got_object_commit_close(commit
);
11920 static const struct got_error
*
11921 histedit_write_commit_list(struct got_object_id_queue
*commits
,
11922 FILE *f
, int edit_logmsg_only
, int fold_only
, int drop_only
,
11923 int edit_only
, struct got_repository
*repo
)
11925 const struct got_error
*err
= NULL
;
11926 struct got_object_qid
*qid
;
11927 const char *histedit_cmd
= NULL
;
11929 if (STAILQ_EMPTY(commits
))
11930 return got_error(GOT_ERR_EMPTY_HISTEDIT
);
11932 STAILQ_FOREACH(qid
, commits
, entry
) {
11933 histedit_cmd
= got_histedit_cmds
[0].name
;
11935 histedit_cmd
= "drop";
11936 else if (edit_only
)
11937 histedit_cmd
= "edit";
11938 else if (fold_only
&& STAILQ_NEXT(qid
, entry
) != NULL
)
11939 histedit_cmd
= "fold";
11940 else if (edit_logmsg_only
)
11941 histedit_cmd
= "mesg";
11942 err
= histedit_write_commit(&qid
->id
, histedit_cmd
, f
, repo
);
11950 static const struct got_error
*
11951 write_cmd_list(FILE *f
, const char *branch_name
,
11952 struct got_object_id_queue
*commits
)
11954 const struct got_error
*err
= NULL
;
11958 struct got_object_qid
*qid
;
11960 qid
= STAILQ_FIRST(commits
);
11961 err
= got_object_id_str(&id_str
, &qid
->id
);
11966 "# Editing the history of branch '%s' starting at\n"
11968 "# Commits will be processed in order from top to "
11969 "bottom of this file.\n", branch_name
, id_str
);
11971 err
= got_ferror(f
, GOT_ERR_IO
);
11975 n
= fprintf(f
, "# Available histedit commands:\n");
11977 err
= got_ferror(f
, GOT_ERR_IO
);
11981 for (i
= 0; i
< nitems(got_histedit_cmds
); i
++) {
11982 const struct got_histedit_cmd
*cmd
= &got_histedit_cmds
[i
];
11983 n
= fprintf(f
, "# %s (%c): %s\n", cmd
->name
, cmd
->code
,
11986 err
= got_ferror(f
, GOT_ERR_IO
);
11995 static const struct got_error
*
11996 histedit_syntax_error(int lineno
)
11998 static char msg
[42];
12001 ret
= snprintf(msg
, sizeof(msg
), "histedit syntax error on line %d",
12003 if (ret
< 0 || (size_t)ret
>= sizeof(msg
))
12004 return got_error(GOT_ERR_HISTEDIT_SYNTAX
);
12006 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX
, msg
);
12009 static const struct got_error
*
12010 append_folded_commit_msg(char **new_msg
, struct got_histedit_list_entry
*hle
,
12011 char *logmsg
, struct got_repository
*repo
)
12013 const struct got_error
*err
;
12014 struct got_commit_object
*folded_commit
= NULL
;
12015 char *id_str
, *folded_logmsg
= NULL
;
12017 err
= got_object_id_str(&id_str
, hle
->commit_id
);
12021 err
= got_object_open_as_commit(&folded_commit
, repo
, hle
->commit_id
);
12025 err
= got_object_commit_get_logmsg(&folded_logmsg
, folded_commit
);
12028 if (asprintf(new_msg
, "%s%s# log message of folded commit %s: %s",
12029 logmsg
? logmsg
: "", logmsg
? "\n" : "", id_str
,
12030 folded_logmsg
) == -1) {
12031 err
= got_error_from_errno("asprintf");
12035 got_object_commit_close(folded_commit
);
12037 free(folded_logmsg
);
12041 static struct got_histedit_list_entry
*
12042 get_folded_commits(struct got_histedit_list_entry
*hle
)
12044 struct got_histedit_list_entry
*prev
, *folded
= NULL
;
12046 prev
= TAILQ_PREV(hle
, got_histedit_list
, entry
);
12047 while (prev
&& (prev
->cmd
->code
== GOT_HISTEDIT_FOLD
||
12048 prev
->cmd
->code
== GOT_HISTEDIT_DROP
)) {
12049 if (prev
->cmd
->code
== GOT_HISTEDIT_FOLD
)
12051 prev
= TAILQ_PREV(prev
, got_histedit_list
, entry
);
12057 static const struct got_error
*
12058 histedit_edit_logmsg(struct got_histedit_list_entry
*hle
,
12059 const char *editor
, struct got_repository
*repo
)
12061 char *logmsg_path
= NULL
, *id_str
= NULL
, *orig_logmsg
= NULL
;
12062 char *logmsg
= NULL
, *new_msg
= NULL
;
12063 const struct got_error
*err
= NULL
;
12064 struct got_commit_object
*commit
= NULL
;
12067 struct got_histedit_list_entry
*folded
= NULL
;
12069 err
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
12073 folded
= get_folded_commits(hle
);
12075 while (folded
!= hle
) {
12076 if (folded
->cmd
->code
== GOT_HISTEDIT_DROP
) {
12077 folded
= TAILQ_NEXT(folded
, entry
);
12080 err
= append_folded_commit_msg(&new_msg
, folded
,
12086 folded
= TAILQ_NEXT(folded
, entry
);
12090 err
= got_object_id_str(&id_str
, hle
->commit_id
);
12093 err
= got_object_commit_get_logmsg(&orig_logmsg
, commit
);
12096 logmsg_len
= asprintf(&new_msg
,
12097 "%s\n# original log message of commit %s: %s",
12098 logmsg
? logmsg
: "", id_str
, orig_logmsg
);
12099 if (logmsg_len
== -1) {
12100 err
= got_error_from_errno("asprintf");
12106 err
= got_object_id_str(&id_str
, hle
->commit_id
);
12110 err
= got_opentemp_named_fd(&logmsg_path
, &fd
,
12111 GOT_TMPDIR_STR
"/got-logmsg", "");
12115 if (write(fd
, logmsg
, logmsg_len
) == -1) {
12116 err
= got_error_from_errno2("write", logmsg_path
);
12119 if (close(fd
) == -1) {
12120 err
= got_error_from_errno2("close", logmsg_path
);
12125 err
= edit_logmsg(&hle
->logmsg
, editor
, logmsg_path
, logmsg
,
12128 if (err
->code
!= GOT_ERR_COMMIT_MSG_EMPTY
)
12131 hle
->logmsg
= strdup(new_msg
);
12132 if (hle
->logmsg
== NULL
)
12133 err
= got_error_from_errno("strdup");
12136 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
12137 err
= got_error_from_errno2("close", logmsg_path
);
12138 if (logmsg_path
&& unlink(logmsg_path
) != 0 && err
== NULL
)
12139 err
= got_error_from_errno2("unlink", logmsg_path
);
12144 got_object_commit_close(commit
);
12148 static const struct got_error
*
12149 histedit_parse_list(struct got_histedit_list
*histedit_cmds
,
12150 FILE *f
, struct got_repository
*repo
)
12152 const struct got_error
*err
= NULL
;
12153 char *line
= NULL
, *p
, *end
;
12154 size_t i
, linesize
= 0;
12157 const struct got_histedit_cmd
*cmd
;
12158 struct got_object_id
*commit_id
= NULL
;
12159 struct got_histedit_list_entry
*hle
= NULL
;
12162 linelen
= getline(&line
, &linesize
, f
);
12163 if (linelen
== -1) {
12164 const struct got_error
*getline_err
;
12167 getline_err
= got_error_from_errno("getline");
12168 err
= got_ferror(f
, getline_err
->code
);
12173 while (isspace((unsigned char)p
[0]))
12175 if (p
[0] == '#' || p
[0] == '\0')
12178 for (i
= 0; i
< nitems(got_histedit_cmds
); i
++) {
12179 cmd
= &got_histedit_cmds
[i
];
12180 if (strncmp(cmd
->name
, p
, strlen(cmd
->name
)) == 0 &&
12181 isspace((unsigned char)p
[strlen(cmd
->name
)])) {
12182 p
+= strlen(cmd
->name
);
12185 if (p
[0] == cmd
->code
&& isspace((unsigned char)p
[1])) {
12190 if (i
== nitems(got_histedit_cmds
)) {
12191 err
= histedit_syntax_error(lineno
);
12194 while (isspace((unsigned char)p
[0]))
12197 while (end
[0] && !isspace((unsigned char)end
[0]))
12200 err
= got_object_resolve_id_str(&commit_id
, repo
, p
);
12202 /* override error code */
12203 err
= histedit_syntax_error(lineno
);
12206 hle
= malloc(sizeof(*hle
));
12208 err
= got_error_from_errno("malloc");
12212 hle
->commit_id
= commit_id
;
12213 hle
->logmsg
= NULL
;
12215 TAILQ_INSERT_TAIL(histedit_cmds
, hle
, entry
);
12223 static const struct got_error
*
12224 histedit_check_script(struct got_histedit_list
*histedit_cmds
,
12225 struct got_object_id_queue
*commits
, struct got_repository
*repo
)
12227 const struct got_error
*err
= NULL
;
12228 struct got_object_qid
*qid
;
12229 struct got_histedit_list_entry
*hle
;
12230 static char msg
[92];
12233 if (TAILQ_EMPTY(histedit_cmds
))
12234 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT
,
12235 "histedit script contains no commands");
12236 if (STAILQ_EMPTY(commits
))
12237 return got_error(GOT_ERR_EMPTY_HISTEDIT
);
12239 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
12240 struct got_histedit_list_entry
*hle2
;
12241 TAILQ_FOREACH(hle2
, histedit_cmds
, entry
) {
12244 if (got_object_id_cmp(hle
->commit_id
,
12245 hle2
->commit_id
) != 0)
12247 err
= got_object_id_str(&id_str
, hle
->commit_id
);
12250 snprintf(msg
, sizeof(msg
), "commit %s is listed "
12251 "more than once in histedit script", id_str
);
12253 return got_error_msg(GOT_ERR_HISTEDIT_CMD
, msg
);
12257 STAILQ_FOREACH(qid
, commits
, entry
) {
12258 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
12259 if (got_object_id_cmp(&qid
->id
, hle
->commit_id
) == 0)
12263 err
= got_object_id_str(&id_str
, &qid
->id
);
12266 snprintf(msg
, sizeof(msg
),
12267 "commit %s missing from histedit script", id_str
);
12269 return got_error_msg(GOT_ERR_HISTEDIT_CMD
, msg
);
12273 hle
= TAILQ_LAST(histedit_cmds
, got_histedit_list
);
12274 if (hle
&& hle
->cmd
->code
== GOT_HISTEDIT_FOLD
)
12275 return got_error_msg(GOT_ERR_HISTEDIT_CMD
,
12276 "last commit in histedit script cannot be folded");
12281 static const struct got_error
*
12282 histedit_run_editor(struct got_histedit_list
*histedit_cmds
,
12283 const char *editor
, const char *path
,
12284 struct got_object_id_queue
*commits
, struct got_repository
*repo
)
12286 const struct got_error
*err
= NULL
;
12287 struct stat st
, st2
;
12288 struct timespec timeout
;
12291 if (stat(path
, &st
) == -1) {
12292 err
= got_error_from_errno2("stat", path
);
12296 if (spawn_editor(editor
, path
) == -1) {
12297 err
= got_error_from_errno("failed spawning editor");
12301 timeout
.tv_sec
= 0;
12302 timeout
.tv_nsec
= 1;
12303 nanosleep(&timeout
, NULL
);
12305 if (stat(path
, &st2
) == -1) {
12306 err
= got_error_from_errno2("stat", path
);
12310 if (st
.st_size
== st2
.st_size
&&
12311 timespeccmp(&st
.st_mtim
, &st2
.st_mtim
, ==)) {
12312 err
= got_error_msg(GOT_ERR_EMPTY_HISTEDIT
,
12313 "no changes made to histedit script, aborting");
12317 f
= fopen(path
, "re");
12319 err
= got_error_from_errno("fopen");
12322 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
12326 err
= histedit_check_script(histedit_cmds
, commits
, repo
);
12328 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
12329 err
= got_error_from_errno("fclose");
12333 static const struct got_error
*
12334 histedit_edit_list_retry(struct got_histedit_list
*, const struct got_error
*,
12335 struct got_object_id_queue
*, const char *, const char *, const char *,
12336 struct got_repository
*);
12338 static const struct got_error
*
12339 histedit_edit_script(struct got_histedit_list
*histedit_cmds
,
12340 struct got_object_id_queue
*commits
, const char *branch_name
,
12341 int edit_logmsg_only
, int fold_only
, int drop_only
, int edit_only
,
12342 const char *editor
, struct got_repository
*repo
)
12344 const struct got_error
*err
;
12348 err
= got_opentemp_named(&path
, &f
, "got-histedit", "");
12352 err
= write_cmd_list(f
, branch_name
, commits
);
12356 err
= histedit_write_commit_list(commits
, f
, edit_logmsg_only
,
12357 fold_only
, drop_only
, edit_only
, repo
);
12361 if (drop_only
|| edit_logmsg_only
|| fold_only
|| edit_only
) {
12363 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
12365 if (fclose(f
) == EOF
) {
12366 err
= got_error_from_errno("fclose");
12370 err
= histedit_run_editor(histedit_cmds
, editor
, path
,
12373 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
12374 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
12376 err
= histedit_edit_list_retry(histedit_cmds
, err
,
12377 commits
, editor
, path
, branch_name
, repo
);
12381 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
12382 err
= got_error_from_errno("fclose");
12383 if (path
&& unlink(path
) != 0 && err
== NULL
)
12384 err
= got_error_from_errno2("unlink", path
);
12389 static const struct got_error
*
12390 histedit_save_list(struct got_histedit_list
*histedit_cmds
,
12391 struct got_worktree
*worktree
, struct got_repository
*repo
)
12393 const struct got_error
*err
= NULL
;
12396 struct got_histedit_list_entry
*hle
;
12398 err
= got_worktree_get_histedit_script_path(&path
, worktree
);
12402 f
= fopen(path
, "we");
12404 err
= got_error_from_errno2("fopen", path
);
12407 TAILQ_FOREACH(hle
, histedit_cmds
, entry
) {
12408 err
= histedit_write_commit(hle
->commit_id
, hle
->cmd
->name
, f
,
12414 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
12415 err
= got_error_from_errno("fclose");
12421 histedit_free_list(struct got_histedit_list
*histedit_cmds
)
12423 struct got_histedit_list_entry
*hle
;
12425 while ((hle
= TAILQ_FIRST(histedit_cmds
))) {
12426 TAILQ_REMOVE(histedit_cmds
, hle
, entry
);
12431 static const struct got_error
*
12432 histedit_load_list(struct got_histedit_list
*histedit_cmds
,
12433 const char *path
, struct got_repository
*repo
)
12435 const struct got_error
*err
= NULL
;
12438 f
= fopen(path
, "re");
12440 err
= got_error_from_errno2("fopen", path
);
12444 err
= histedit_parse_list(histedit_cmds
, f
, repo
);
12446 if (f
&& fclose(f
) == EOF
&& err
== NULL
)
12447 err
= got_error_from_errno("fclose");
12451 static const struct got_error
*
12452 histedit_edit_list_retry(struct got_histedit_list
*histedit_cmds
,
12453 const struct got_error
*edit_err
, struct got_object_id_queue
*commits
,
12454 const char *editor
, const char *path
, const char *branch_name
,
12455 struct got_repository
*repo
)
12457 const struct got_error
*err
= NULL
, *prev_err
= edit_err
;
12460 while (resp
!= 'c' && resp
!= 'r' && resp
!= 'a') {
12461 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12462 "or (a)bort: ", getprogname(), prev_err
->msg
);
12467 histedit_free_list(histedit_cmds
);
12468 err
= histedit_run_editor(histedit_cmds
, editor
, path
,
12471 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
12472 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
12479 } else if (resp
== 'r') {
12480 histedit_free_list(histedit_cmds
);
12481 err
= histedit_edit_script(histedit_cmds
,
12482 commits
, branch_name
, 0, 0, 0, 0, editor
, repo
);
12484 if (err
->code
!= GOT_ERR_HISTEDIT_SYNTAX
&&
12485 err
->code
!= GOT_ERR_HISTEDIT_CMD
)
12492 } else if (resp
== 'a') {
12493 err
= got_error(GOT_ERR_HISTEDIT_CANCEL
);
12496 printf("invalid response '%c'\n", resp
);
12502 static const struct got_error
*
12503 histedit_complete(struct got_worktree
*worktree
,
12504 struct got_fileindex
*fileindex
, struct got_reference
*tmp_branch
,
12505 struct got_reference
*branch
, struct got_repository
*repo
)
12507 printf("Switching work tree to %s\n",
12508 got_ref_get_symref_target(branch
));
12509 return got_worktree_histedit_complete(worktree
, fileindex
, tmp_branch
,
12513 static const struct got_error
*
12514 show_histedit_progress(struct got_commit_object
*commit
,
12515 struct got_histedit_list_entry
*hle
, struct got_object_id
*new_id
)
12517 const struct got_error
*err
;
12518 char *old_id_str
= NULL
, *new_id_str
= NULL
, *logmsg
= NULL
;
12520 err
= got_object_id_str(&old_id_str
, hle
->commit_id
);
12525 err
= got_object_id_str(&new_id_str
, new_id
);
12530 old_id_str
[12] = '\0';
12532 new_id_str
[12] = '\0';
12535 logmsg
= strdup(hle
->logmsg
);
12536 if (logmsg
== NULL
) {
12537 err
= got_error_from_errno("strdup");
12540 trim_logmsg(logmsg
, 42);
12542 err
= get_short_logmsg(&logmsg
, 42, commit
);
12547 switch (hle
->cmd
->code
) {
12548 case GOT_HISTEDIT_PICK
:
12549 case GOT_HISTEDIT_EDIT
:
12550 case GOT_HISTEDIT_MESG
:
12551 printf("%s -> %s: %s\n", old_id_str
,
12552 new_id_str
? new_id_str
: "no-op change", logmsg
);
12554 case GOT_HISTEDIT_DROP
:
12555 case GOT_HISTEDIT_FOLD
:
12556 printf("%s -> %s commit: %s\n", old_id_str
, hle
->cmd
->name
,
12568 static const struct got_error
*
12569 histedit_commit(struct got_pathlist_head
*merged_paths
,
12570 struct got_worktree
*worktree
, struct got_fileindex
*fileindex
,
12571 struct got_reference
*tmp_branch
, struct got_histedit_list_entry
*hle
,
12572 const char *committer
, int allow_conflict
, const char *editor
,
12573 struct got_repository
*repo
)
12575 const struct got_error
*err
;
12576 struct got_commit_object
*commit
;
12577 struct got_object_id
*new_commit_id
;
12579 if ((hle
->cmd
->code
== GOT_HISTEDIT_EDIT
|| get_folded_commits(hle
))
12580 && hle
->logmsg
== NULL
) {
12581 err
= histedit_edit_logmsg(hle
, editor
, repo
);
12586 err
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
12590 err
= got_worktree_histedit_commit(&new_commit_id
, merged_paths
,
12591 worktree
, fileindex
, tmp_branch
, committer
, commit
, hle
->commit_id
,
12592 hle
->logmsg
, allow_conflict
, repo
);
12594 if (err
->code
!= GOT_ERR_COMMIT_NO_CHANGES
)
12596 err
= show_histedit_progress(commit
, hle
, NULL
);
12598 err
= show_histedit_progress(commit
, hle
, new_commit_id
);
12599 free(new_commit_id
);
12602 got_object_commit_close(commit
);
12606 static const struct got_error
*
12607 histedit_skip_commit(struct got_histedit_list_entry
*hle
,
12608 struct got_worktree
*worktree
, struct got_repository
*repo
)
12610 const struct got_error
*error
;
12611 struct got_commit_object
*commit
;
12613 error
= got_worktree_histedit_skip_commit(worktree
, hle
->commit_id
,
12618 error
= got_object_open_as_commit(&commit
, repo
, hle
->commit_id
);
12622 error
= show_histedit_progress(commit
, hle
, NULL
);
12623 got_object_commit_close(commit
);
12627 static const struct got_error
*
12628 check_local_changes(void *arg
, unsigned char status
,
12629 unsigned char staged_status
, const char *path
,
12630 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
12631 struct got_object_id
*commit_id
, int dirfd
, const char *de_name
)
12633 int *have_local_changes
= arg
;
12636 case GOT_STATUS_ADD
:
12637 case GOT_STATUS_DELETE
:
12638 case GOT_STATUS_MODIFY
:
12639 case GOT_STATUS_CONFLICT
:
12640 *have_local_changes
= 1;
12641 return got_error(GOT_ERR_CANCELLED
);
12646 switch (staged_status
) {
12647 case GOT_STATUS_ADD
:
12648 case GOT_STATUS_DELETE
:
12649 case GOT_STATUS_MODIFY
:
12650 *have_local_changes
= 1;
12651 return got_error(GOT_ERR_CANCELLED
);
12659 static const struct got_error
*
12660 cmd_histedit(int argc
, char *argv
[])
12662 const struct got_error
*error
= NULL
;
12663 struct got_worktree
*worktree
= NULL
;
12664 struct got_fileindex
*fileindex
= NULL
;
12665 struct got_repository
*repo
= NULL
;
12666 char *cwd
= NULL
, *committer
= NULL
, *gitconfig_path
= NULL
;
12667 struct got_reference
*branch
= NULL
;
12668 struct got_reference
*tmp_branch
= NULL
;
12669 struct got_object_id
*resume_commit_id
= NULL
;
12670 struct got_object_id
*base_commit_id
= NULL
;
12671 struct got_object_id
*head_commit_id
= NULL
;
12672 struct got_commit_object
*commit
= NULL
;
12673 int ch
, rebase_in_progress
= 0, merge_in_progress
= 0;
12674 struct got_update_progress_arg upa
;
12675 int edit_in_progress
= 0, abort_edit
= 0, continue_edit
= 0;
12676 int drop_only
= 0, edit_logmsg_only
= 0, fold_only
= 0, edit_only
= 0;
12677 int allow_conflict
= 0, list_backups
= 0, delete_backups
= 0;
12678 const char *edit_script_path
= NULL
;
12679 char *editor
= NULL
;
12680 struct got_object_id_queue commits
;
12681 struct got_pathlist_head merged_paths
;
12682 const struct got_object_id_queue
*parent_ids
;
12683 struct got_object_qid
*pid
;
12684 struct got_histedit_list histedit_cmds
;
12685 struct got_histedit_list_entry
*hle
;
12686 int *pack_fds
= NULL
;
12688 STAILQ_INIT(&commits
);
12689 TAILQ_INIT(&histedit_cmds
);
12690 TAILQ_INIT(&merged_paths
);
12691 memset(&upa
, 0, sizeof(upa
));
12694 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12695 "unveil", NULL
) == -1)
12699 while ((ch
= getopt(argc
, argv
, "aCcdeF:flmX")) != -1) {
12705 allow_conflict
= 1;
12717 edit_script_path
= optarg
;
12726 edit_logmsg_only
= 1;
12729 delete_backups
= 1;
12740 if (abort_edit
&& allow_conflict
)
12741 option_conflict('a', 'C');
12742 if (abort_edit
&& continue_edit
)
12743 option_conflict('a', 'c');
12744 if (edit_script_path
&& allow_conflict
)
12745 option_conflict('F', 'C');
12746 if (edit_script_path
&& edit_logmsg_only
)
12747 option_conflict('F', 'm');
12748 if (abort_edit
&& edit_logmsg_only
)
12749 option_conflict('a', 'm');
12750 if (edit_logmsg_only
&& allow_conflict
)
12751 option_conflict('m', 'C');
12752 if (continue_edit
&& edit_logmsg_only
)
12753 option_conflict('c', 'm');
12754 if (abort_edit
&& fold_only
)
12755 option_conflict('a', 'f');
12756 if (fold_only
&& allow_conflict
)
12757 option_conflict('f', 'C');
12758 if (continue_edit
&& fold_only
)
12759 option_conflict('c', 'f');
12760 if (fold_only
&& edit_logmsg_only
)
12761 option_conflict('f', 'm');
12762 if (edit_script_path
&& fold_only
)
12763 option_conflict('F', 'f');
12764 if (abort_edit
&& edit_only
)
12765 option_conflict('a', 'e');
12766 if (continue_edit
&& edit_only
)
12767 option_conflict('c', 'e');
12768 if (edit_only
&& edit_logmsg_only
)
12769 option_conflict('e', 'm');
12770 if (edit_script_path
&& edit_only
)
12771 option_conflict('F', 'e');
12772 if (fold_only
&& edit_only
)
12773 option_conflict('f', 'e');
12774 if (drop_only
&& abort_edit
)
12775 option_conflict('d', 'a');
12776 if (drop_only
&& allow_conflict
)
12777 option_conflict('d', 'C');
12778 if (drop_only
&& continue_edit
)
12779 option_conflict('d', 'c');
12780 if (drop_only
&& edit_logmsg_only
)
12781 option_conflict('d', 'm');
12782 if (drop_only
&& edit_only
)
12783 option_conflict('d', 'e');
12784 if (drop_only
&& edit_script_path
)
12785 option_conflict('d', 'F');
12786 if (drop_only
&& fold_only
)
12787 option_conflict('d', 'f');
12788 if (list_backups
) {
12790 option_conflict('l', 'a');
12791 if (allow_conflict
)
12792 option_conflict('l', 'C');
12794 option_conflict('l', 'c');
12795 if (edit_script_path
)
12796 option_conflict('l', 'F');
12797 if (edit_logmsg_only
)
12798 option_conflict('l', 'm');
12800 option_conflict('l', 'd');
12802 option_conflict('l', 'f');
12804 option_conflict('l', 'e');
12805 if (delete_backups
)
12806 option_conflict('l', 'X');
12807 if (argc
!= 0 && argc
!= 1)
12809 } else if (delete_backups
) {
12811 option_conflict('X', 'a');
12812 if (allow_conflict
)
12813 option_conflict('X', 'C');
12815 option_conflict('X', 'c');
12817 option_conflict('X', 'd');
12818 if (edit_script_path
)
12819 option_conflict('X', 'F');
12820 if (edit_logmsg_only
)
12821 option_conflict('X', 'm');
12823 option_conflict('X', 'f');
12825 option_conflict('X', 'e');
12827 option_conflict('X', 'l');
12828 if (argc
!= 0 && argc
!= 1)
12830 } else if (allow_conflict
&& !continue_edit
)
12831 errx(1, "-C option requires -c");
12832 else if (argc
!= 0)
12835 cwd
= getcwd(NULL
, 0);
12837 error
= got_error_from_errno("getcwd");
12841 error
= got_repo_pack_fds_open(&pack_fds
);
12845 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
12847 if (list_backups
|| delete_backups
) {
12848 if (error
->code
!= GOT_ERR_NOT_WORKTREE
)
12851 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
12852 error
= wrap_not_worktree_error(error
,
12858 if (list_backups
|| delete_backups
) {
12859 error
= got_repo_open(&repo
,
12860 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
12864 error
= apply_unveil(got_repo_get_path(repo
), 0,
12865 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
12868 error
= process_backup_refs(
12869 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX
,
12870 argc
== 1 ? argv
[0] : NULL
, delete_backups
, repo
);
12871 goto done
; /* nothing else to do */
12873 error
= get_gitconfig_path(&gitconfig_path
);
12876 error
= got_repo_open(&repo
,
12877 got_worktree_get_repo_path(worktree
), gitconfig_path
,
12881 error
= get_editor(&editor
);
12884 if (unveil(editor
, "x") != 0) {
12885 error
= got_error_from_errno2("unveil", editor
);
12888 if (edit_script_path
) {
12889 if (unveil(edit_script_path
, "r") != 0) {
12890 error
= got_error_from_errno2("unveil",
12895 error
= apply_unveil(got_repo_get_path(repo
), 0,
12896 got_worktree_get_root_path(worktree
));
12901 if (worktree
!= NULL
&& !list_backups
&& !delete_backups
) {
12902 error
= worktree_has_logmsg_ref("histedit", worktree
, repo
);
12907 error
= got_worktree_rebase_in_progress(&rebase_in_progress
, worktree
);
12910 if (rebase_in_progress
) {
12911 error
= got_error(GOT_ERR_REBASING
);
12915 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
12919 if (merge_in_progress
) {
12920 error
= got_error(GOT_ERR_MERGE_BUSY
);
12924 error
= got_worktree_histedit_in_progress(&edit_in_progress
, worktree
);
12928 if (edit_in_progress
&& edit_logmsg_only
) {
12929 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
12930 "histedit operation is in progress in this "
12931 "work tree and must be continued or aborted "
12932 "before the -m option can be used");
12935 if (edit_in_progress
&& drop_only
) {
12936 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
12937 "histedit operation is in progress in this "
12938 "work tree and must be continued or aborted "
12939 "before the -d option can be used");
12942 if (edit_in_progress
&& fold_only
) {
12943 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
12944 "histedit operation is in progress in this "
12945 "work tree and must be continued or aborted "
12946 "before the -f option can be used");
12949 if (edit_in_progress
&& edit_only
) {
12950 error
= got_error_msg(GOT_ERR_HISTEDIT_BUSY
,
12951 "histedit operation is in progress in this "
12952 "work tree and must be continued or aborted "
12953 "before the -e option can be used");
12957 if (edit_in_progress
&& abort_edit
) {
12958 error
= got_worktree_histedit_continue(&resume_commit_id
,
12959 &tmp_branch
, &branch
, &base_commit_id
, &fileindex
,
12963 printf("Switching work tree to %s\n",
12964 got_ref_get_symref_target(branch
));
12965 error
= got_worktree_histedit_abort(worktree
, fileindex
, repo
,
12966 branch
, base_commit_id
, abort_progress
, &upa
);
12969 printf("Histedit of %s aborted\n",
12970 got_ref_get_symref_target(branch
));
12971 print_merge_progress_stats(&upa
);
12972 goto done
; /* nothing else to do */
12973 } else if (abort_edit
) {
12974 error
= got_error(GOT_ERR_NOT_HISTEDIT
);
12978 error
= get_author(&committer
, repo
, worktree
);
12982 if (continue_edit
) {
12985 if (!edit_in_progress
) {
12986 error
= got_error(GOT_ERR_NOT_HISTEDIT
);
12990 error
= got_worktree_get_histedit_script_path(&path
, worktree
);
12994 error
= histedit_load_list(&histedit_cmds
, path
, repo
);
12999 error
= got_worktree_histedit_continue(&resume_commit_id
,
13000 &tmp_branch
, &branch
, &base_commit_id
, &fileindex
,
13005 error
= got_ref_resolve(&head_commit_id
, repo
, branch
);
13009 error
= got_object_open_as_commit(&commit
, repo
,
13013 parent_ids
= got_object_commit_get_parent_ids(commit
);
13014 pid
= STAILQ_FIRST(parent_ids
);
13016 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
13019 error
= collect_commits(&commits
, head_commit_id
, &pid
->id
,
13020 base_commit_id
, got_worktree_get_path_prefix(worktree
),
13021 GOT_ERR_HISTEDIT_PATH
, repo
);
13022 got_object_commit_close(commit
);
13027 if (edit_in_progress
) {
13028 error
= got_error(GOT_ERR_HISTEDIT_BUSY
);
13032 error
= got_ref_open(&branch
, repo
,
13033 got_worktree_get_head_ref_name(worktree
), 0);
13037 if (strncmp(got_ref_get_name(branch
), "refs/heads/", 11) != 0) {
13038 error
= got_error_msg(GOT_ERR_COMMIT_BRANCH
,
13039 "will not edit commit history of a branch outside "
13040 "the \"refs/heads/\" reference namespace");
13044 error
= got_ref_resolve(&head_commit_id
, repo
, branch
);
13045 got_ref_close(branch
);
13050 error
= got_object_open_as_commit(&commit
, repo
,
13054 parent_ids
= got_object_commit_get_parent_ids(commit
);
13055 pid
= STAILQ_FIRST(parent_ids
);
13057 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
13060 error
= collect_commits(&commits
, head_commit_id
, &pid
->id
,
13061 got_worktree_get_base_commit_id(worktree
),
13062 got_worktree_get_path_prefix(worktree
),
13063 GOT_ERR_HISTEDIT_PATH
, repo
);
13064 got_object_commit_close(commit
);
13069 if (STAILQ_EMPTY(&commits
)) {
13070 error
= got_error(GOT_ERR_EMPTY_HISTEDIT
);
13074 error
= got_worktree_histedit_prepare(&tmp_branch
, &branch
,
13075 &base_commit_id
, &fileindex
, worktree
, repo
);
13079 if (edit_script_path
) {
13080 error
= histedit_load_list(&histedit_cmds
,
13081 edit_script_path
, repo
);
13083 got_worktree_histedit_abort(worktree
, fileindex
,
13084 repo
, branch
, base_commit_id
,
13085 abort_progress
, &upa
);
13086 print_merge_progress_stats(&upa
);
13090 const char *branch_name
;
13091 branch_name
= got_ref_get_symref_target(branch
);
13092 if (strncmp(branch_name
, "refs/heads/", 11) == 0)
13094 error
= histedit_edit_script(&histedit_cmds
, &commits
,
13095 branch_name
, edit_logmsg_only
, fold_only
,
13096 drop_only
, edit_only
, editor
, repo
);
13098 got_worktree_histedit_abort(worktree
, fileindex
,
13099 repo
, branch
, base_commit_id
,
13100 abort_progress
, &upa
);
13101 print_merge_progress_stats(&upa
);
13107 error
= histedit_save_list(&histedit_cmds
, worktree
,
13110 got_worktree_histedit_abort(worktree
, fileindex
,
13111 repo
, branch
, base_commit_id
,
13112 abort_progress
, &upa
);
13113 print_merge_progress_stats(&upa
);
13119 error
= histedit_check_script(&histedit_cmds
, &commits
, repo
);
13123 TAILQ_FOREACH(hle
, &histedit_cmds
, entry
) {
13124 if (resume_commit_id
) {
13125 if (got_object_id_cmp(hle
->commit_id
,
13126 resume_commit_id
) != 0)
13129 resume_commit_id
= NULL
;
13130 if (hle
->cmd
->code
== GOT_HISTEDIT_DROP
||
13131 hle
->cmd
->code
== GOT_HISTEDIT_FOLD
) {
13132 error
= histedit_skip_commit(hle
, worktree
,
13137 struct got_pathlist_head paths
;
13138 int have_changes
= 0;
13140 TAILQ_INIT(&paths
);
13141 error
= got_pathlist_append(&paths
, "", NULL
);
13144 error
= got_worktree_status(worktree
, &paths
,
13145 repo
, 0, check_local_changes
, &have_changes
,
13146 check_cancelled
, NULL
);
13147 got_pathlist_free(&paths
,
13148 GOT_PATHLIST_FREE_NONE
);
13150 if (error
->code
!= GOT_ERR_CANCELLED
)
13152 if (sigint_received
|| sigpipe_received
)
13155 if (have_changes
) {
13156 error
= histedit_commit(NULL
, worktree
,
13157 fileindex
, tmp_branch
, hle
,
13158 committer
, allow_conflict
, editor
,
13163 error
= got_object_open_as_commit(
13164 &commit
, repo
, hle
->commit_id
);
13167 error
= show_histedit_progress(commit
,
13169 got_object_commit_close(commit
);
13178 if (hle
->cmd
->code
== GOT_HISTEDIT_DROP
) {
13179 error
= histedit_skip_commit(hle
, worktree
, repo
);
13184 error
= got_object_open_as_commit(&commit
, repo
,
13188 parent_ids
= got_object_commit_get_parent_ids(commit
);
13189 pid
= STAILQ_FIRST(parent_ids
);
13191 error
= got_worktree_histedit_merge_files(&merged_paths
,
13192 worktree
, fileindex
, &pid
->id
, hle
->commit_id
, repo
,
13193 update_progress
, &upa
, check_cancelled
, NULL
);
13196 got_object_commit_close(commit
);
13199 print_merge_progress_stats(&upa
);
13200 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
13201 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
13202 if (upa
.conflicts
> 0) {
13203 error
= show_rebase_merge_conflict(
13204 hle
->commit_id
, repo
);
13208 got_pathlist_free(&merged_paths
, GOT_PATHLIST_FREE_PATH
);
13212 if (hle
->cmd
->code
== GOT_HISTEDIT_EDIT
) {
13214 error
= got_object_id_str(&id_str
, hle
->commit_id
);
13217 printf("Stopping histedit for amending commit %s\n",
13220 got_pathlist_free(&merged_paths
, GOT_PATHLIST_FREE_PATH
);
13221 error
= got_worktree_histedit_postpone(worktree
,
13224 } else if (hle
->cmd
->code
== GOT_HISTEDIT_FOLD
) {
13225 error
= histedit_skip_commit(hle
, worktree
, repo
);
13229 } else if (hle
->cmd
->code
== GOT_HISTEDIT_MESG
) {
13230 error
= histedit_edit_logmsg(hle
, editor
, repo
);
13235 error
= histedit_commit(&merged_paths
, worktree
, fileindex
,
13236 tmp_branch
, hle
, committer
, allow_conflict
, editor
, repo
);
13237 got_pathlist_free(&merged_paths
, GOT_PATHLIST_FREE_PATH
);
13242 if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
13243 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
13244 error
= got_worktree_histedit_postpone(worktree
, fileindex
);
13247 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
13248 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
13249 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13250 "conflicts must be resolved before histedit "
13252 } else if (upa
.conflicts
> 0) {
13253 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13254 "conflicts must be resolved before histedit "
13255 "can continue; changes destined for some "
13256 "files were not yet merged and should be "
13257 "merged manually if required before the "
13258 "histedit operation is continued");
13260 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13261 "changes destined for some files were not "
13262 "yet merged and should be merged manually "
13263 "if required before the histedit operation "
13267 error
= histedit_complete(worktree
, fileindex
, tmp_branch
,
13273 free(gitconfig_path
);
13274 got_object_id_queue_free(&commits
);
13275 histedit_free_list(&histedit_cmds
);
13276 free(head_commit_id
);
13277 free(base_commit_id
);
13278 free(resume_commit_id
);
13280 got_object_commit_close(commit
);
13282 got_ref_close(branch
);
13284 got_ref_close(tmp_branch
);
13286 got_worktree_close(worktree
);
13288 const struct got_error
*close_err
= got_repo_close(repo
);
13293 const struct got_error
*pack_err
=
13294 got_repo_pack_fds_close(pack_fds
);
13302 usage_integrate(void)
13304 fprintf(stderr
, "usage: %s integrate branch\n", getprogname());
13308 static const struct got_error
*
13309 cmd_integrate(int argc
, char *argv
[])
13311 const struct got_error
*error
= NULL
;
13312 struct got_repository
*repo
= NULL
;
13313 struct got_worktree
*worktree
= NULL
;
13314 char *cwd
= NULL
, *refname
= NULL
, *base_refname
= NULL
;
13315 const char *branch_arg
= NULL
;
13316 struct got_reference
*branch_ref
= NULL
, *base_branch_ref
= NULL
;
13317 struct got_fileindex
*fileindex
= NULL
;
13318 struct got_object_id
*commit_id
= NULL
, *base_commit_id
= NULL
;
13320 struct got_update_progress_arg upa
;
13321 int *pack_fds
= NULL
;
13324 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13325 "unveil", NULL
) == -1)
13329 while ((ch
= getopt(argc
, argv
, "")) != -1) {
13342 branch_arg
= argv
[0];
13344 cwd
= getcwd(NULL
, 0);
13346 error
= got_error_from_errno("getcwd");
13350 error
= got_repo_pack_fds_open(&pack_fds
);
13354 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
13356 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
13357 error
= wrap_not_worktree_error(error
, "integrate",
13362 error
= check_rebase_or_histedit_in_progress(worktree
);
13366 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
13371 error
= apply_unveil(got_repo_get_path(repo
), 0,
13372 got_worktree_get_root_path(worktree
));
13376 error
= check_merge_in_progress(worktree
, repo
);
13380 if (asprintf(&refname
, "refs/heads/%s", branch_arg
) == -1) {
13381 error
= got_error_from_errno("asprintf");
13385 error
= got_worktree_integrate_prepare(&fileindex
, &branch_ref
,
13386 &base_branch_ref
, worktree
, refname
, repo
);
13390 refname
= strdup(got_ref_get_name(branch_ref
));
13391 if (refname
== NULL
) {
13392 error
= got_error_from_errno("strdup");
13393 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
13394 branch_ref
, base_branch_ref
);
13397 base_refname
= strdup(got_ref_get_name(base_branch_ref
));
13398 if (base_refname
== NULL
) {
13399 error
= got_error_from_errno("strdup");
13400 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
13401 branch_ref
, base_branch_ref
);
13404 if (strncmp(base_refname
, "refs/heads/", 11) != 0) {
13405 error
= got_error(GOT_ERR_INTEGRATE_BRANCH
);
13406 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
13407 branch_ref
, base_branch_ref
);
13411 error
= got_ref_resolve(&commit_id
, repo
, branch_ref
);
13415 error
= got_ref_resolve(&base_commit_id
, repo
, base_branch_ref
);
13419 if (got_object_id_cmp(commit_id
, base_commit_id
) == 0) {
13420 error
= got_error_msg(GOT_ERR_SAME_BRANCH
,
13421 "specified branch has already been integrated");
13422 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
13423 branch_ref
, base_branch_ref
);
13427 error
= check_linear_ancestry(commit_id
, base_commit_id
, 1, repo
);
13429 if (error
->code
== GOT_ERR_ANCESTRY
)
13430 error
= got_error(GOT_ERR_REBASE_REQUIRED
);
13431 got_worktree_integrate_abort(worktree
, fileindex
, repo
,
13432 branch_ref
, base_branch_ref
);
13436 memset(&upa
, 0, sizeof(upa
));
13437 error
= got_worktree_integrate_continue(worktree
, fileindex
, repo
,
13438 branch_ref
, base_branch_ref
, update_progress
, &upa
,
13439 check_cancelled
, NULL
);
13443 printf("Integrated %s into %s\n", refname
, base_refname
);
13444 print_update_progress_stats(&upa
);
13447 const struct got_error
*close_err
= got_repo_close(repo
);
13452 got_worktree_close(worktree
);
13454 const struct got_error
*pack_err
=
13455 got_repo_pack_fds_close(pack_fds
);
13460 free(base_commit_id
);
13463 free(base_refname
);
13470 fprintf(stderr
, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13474 static const struct got_error
*
13475 cmd_merge(int argc
, char *argv
[])
13477 const struct got_error
*error
= NULL
;
13478 struct got_worktree
*worktree
= NULL
;
13479 struct got_repository
*repo
= NULL
;
13480 struct got_fileindex
*fileindex
= NULL
;
13481 char *cwd
= NULL
, *id_str
= NULL
, *author
= NULL
;
13482 char *gitconfig_path
= NULL
;
13483 struct got_reference
*branch
= NULL
, *wt_branch
= NULL
;
13484 struct got_object_id
*branch_tip
= NULL
, *yca_id
= NULL
;
13485 struct got_object_id
*wt_branch_tip
= NULL
;
13486 int ch
, merge_in_progress
= 0, abort_merge
= 0, continue_merge
= 0;
13487 int allow_conflict
= 0, prefer_fast_forward
= 1, interrupt_merge
= 0;
13488 struct got_update_progress_arg upa
;
13489 struct got_object_id
*merge_commit_id
= NULL
;
13490 char *branch_name
= NULL
;
13491 int *pack_fds
= NULL
;
13493 memset(&upa
, 0, sizeof(upa
));
13496 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13497 "unveil", NULL
) == -1)
13501 while ((ch
= getopt(argc
, argv
, "aCcMn")) != -1) {
13507 allow_conflict
= 1;
13510 continue_merge
= 1;
13513 prefer_fast_forward
= 0;
13516 interrupt_merge
= 1;
13528 if (continue_merge
)
13529 option_conflict('a', 'c');
13530 if (!prefer_fast_forward
)
13531 option_conflict('a', 'M');
13532 if (interrupt_merge
)
13533 option_conflict('a', 'n');
13534 } else if (continue_merge
) {
13535 if (!prefer_fast_forward
)
13536 option_conflict('c', 'M');
13537 if (interrupt_merge
)
13538 option_conflict('c', 'n');
13540 if (allow_conflict
) {
13541 if (!continue_merge
)
13542 errx(1, "-C option requires -c");
13544 if (abort_merge
|| continue_merge
) {
13547 } else if (argc
!= 1)
13550 cwd
= getcwd(NULL
, 0);
13552 error
= got_error_from_errno("getcwd");
13556 error
= got_repo_pack_fds_open(&pack_fds
);
13560 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
13562 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
13563 error
= wrap_not_worktree_error(error
,
13568 error
= get_gitconfig_path(&gitconfig_path
);
13571 error
= got_repo_open(&repo
,
13572 worktree
? got_worktree_get_repo_path(worktree
) : cwd
,
13573 gitconfig_path
, pack_fds
);
13577 if (worktree
!= NULL
) {
13578 error
= worktree_has_logmsg_ref("merge", worktree
, repo
);
13583 error
= apply_unveil(got_repo_get_path(repo
), 0,
13584 worktree
? got_worktree_get_root_path(worktree
) : NULL
);
13588 error
= check_rebase_or_histedit_in_progress(worktree
);
13592 error
= got_worktree_merge_in_progress(&merge_in_progress
, worktree
,
13597 if (merge_in_progress
&& !(abort_merge
|| continue_merge
)) {
13598 error
= got_error(GOT_ERR_MERGE_BUSY
);
13602 if (!merge_in_progress
&& (abort_merge
|| continue_merge
)) {
13603 error
= got_error(GOT_ERR_NOT_MERGING
);
13608 error
= got_worktree_merge_continue(&branch_name
,
13609 &branch_tip
, &fileindex
, worktree
, repo
);
13612 error
= got_worktree_merge_abort(worktree
, fileindex
, repo
,
13613 abort_progress
, &upa
);
13616 printf("Merge of %s aborted\n", branch_name
);
13617 goto done
; /* nothing else to do */
13620 if (strncmp(got_worktree_get_head_ref_name(worktree
),
13621 "refs/heads/", 11) != 0) {
13622 error
= got_error_fmt(GOT_ERR_COMMIT_BRANCH
,
13623 "work tree's current branch %s is outside the "
13624 "\"refs/heads/\" reference namespace; "
13625 "update -b required",
13626 got_worktree_get_head_ref_name(worktree
));
13630 error
= get_author(&author
, repo
, worktree
);
13634 error
= got_ref_open(&wt_branch
, repo
,
13635 got_worktree_get_head_ref_name(worktree
), 0);
13638 error
= got_ref_resolve(&wt_branch_tip
, repo
, wt_branch
);
13642 if (continue_merge
) {
13643 struct got_object_id
*base_commit_id
;
13644 base_commit_id
= got_worktree_get_base_commit_id(worktree
);
13645 if (got_object_id_cmp(wt_branch_tip
, base_commit_id
) != 0) {
13646 error
= got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE
);
13649 error
= got_worktree_merge_continue(&branch_name
,
13650 &branch_tip
, &fileindex
, worktree
, repo
);
13654 error
= got_ref_open(&branch
, repo
, argv
[0], 0);
13657 branch_name
= strdup(got_ref_get_name(branch
));
13658 if (branch_name
== NULL
) {
13659 error
= got_error_from_errno("strdup");
13662 error
= got_ref_resolve(&branch_tip
, repo
, branch
);
13667 error
= got_commit_graph_find_youngest_common_ancestor(&yca_id
,
13668 wt_branch_tip
, branch_tip
, 0, 0, repo
,
13669 check_cancelled
, NULL
);
13670 if (error
&& error
->code
!= GOT_ERR_ANCESTRY
)
13673 if (!continue_merge
) {
13674 error
= check_path_prefix(wt_branch_tip
, branch_tip
,
13675 got_worktree_get_path_prefix(worktree
),
13676 GOT_ERR_MERGE_PATH
, repo
);
13679 error
= got_worktree_merge_prepare(&fileindex
, worktree
, repo
);
13682 if (prefer_fast_forward
&& yca_id
&&
13683 got_object_id_cmp(wt_branch_tip
, yca_id
) == 0) {
13684 struct got_pathlist_head paths
;
13685 if (interrupt_merge
) {
13686 error
= got_error_fmt(GOT_ERR_BAD_OPTION
,
13687 "there are no changes to merge since %s "
13688 "is already based on %s; merge cannot be "
13689 "interrupted for amending; -n",
13690 branch_name
, got_ref_get_name(wt_branch
));
13693 printf("Forwarding %s to %s\n",
13694 got_ref_get_name(wt_branch
), branch_name
);
13695 error
= got_ref_change_ref(wt_branch
, branch_tip
);
13698 error
= got_ref_write(wt_branch
, repo
);
13701 error
= got_worktree_set_base_commit_id(worktree
, repo
,
13705 TAILQ_INIT(&paths
);
13706 error
= got_pathlist_append(&paths
, "", NULL
);
13709 error
= got_worktree_checkout_files(worktree
,
13710 &paths
, repo
, update_progress
, &upa
,
13711 check_cancelled
, NULL
);
13712 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_NONE
);
13715 if (upa
.did_something
) {
13717 error
= got_object_id_str(&id_str
, branch_tip
);
13720 printf("Updated to commit %s\n", id_str
);
13723 printf("Already up-to-date\n");
13724 print_update_progress_stats(&upa
);
13727 error
= got_worktree_merge_write_refs(worktree
, branch
, repo
);
13731 error
= got_worktree_merge_branch(worktree
, fileindex
,
13732 yca_id
, branch_tip
, repo
, update_progress
, &upa
,
13733 check_cancelled
, NULL
);
13736 print_merge_progress_stats(&upa
);
13737 if (!upa
.did_something
) {
13738 error
= got_worktree_merge_abort(worktree
, fileindex
,
13739 repo
, abort_progress
, &upa
);
13742 printf("Already up-to-date\n");
13747 if (interrupt_merge
) {
13748 error
= got_worktree_merge_postpone(worktree
, fileindex
);
13751 printf("Merge of %s interrupted on request\n", branch_name
);
13752 } else if (upa
.conflicts
> 0 || upa
.missing
> 0 ||
13753 upa
.not_deleted
> 0 || upa
.unversioned
> 0) {
13754 error
= got_worktree_merge_postpone(worktree
, fileindex
);
13757 if (upa
.conflicts
> 0 && upa
.missing
== 0 &&
13758 upa
.not_deleted
== 0 && upa
.unversioned
== 0) {
13759 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13760 "conflicts must be resolved before merging "
13762 } else if (upa
.conflicts
> 0) {
13763 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13764 "conflicts must be resolved before merging "
13765 "can continue; changes destined for some "
13766 "files were not yet merged and "
13767 "should be merged manually if required before the "
13768 "merge operation is continued");
13770 error
= got_error_msg(GOT_ERR_CONFLICTS
,
13771 "changes destined for some "
13772 "files were not yet merged and should be "
13773 "merged manually if required before the "
13774 "merge operation is continued");
13778 error
= got_worktree_merge_commit(&merge_commit_id
, worktree
,
13779 fileindex
, author
, NULL
, 1, branch_tip
, branch_name
,
13780 allow_conflict
, repo
, continue_merge
? print_status
: NULL
,
13784 error
= got_worktree_merge_complete(worktree
, fileindex
, repo
);
13787 error
= got_object_id_str(&id_str
, merge_commit_id
);
13790 printf("Merged %s into %s: %s\n", branch_name
,
13791 got_worktree_get_head_ref_name(worktree
),
13796 free(gitconfig_path
);
13798 free(merge_commit_id
);
13804 got_ref_close(branch
);
13806 got_ref_close(wt_branch
);
13808 got_worktree_close(worktree
);
13810 const struct got_error
*close_err
= got_repo_close(repo
);
13815 const struct got_error
*pack_err
=
13816 got_repo_pack_fds_close(pack_fds
);
13826 fprintf(stderr
, "usage: %s stage [-lpS] [-F response-script] "
13827 "[path ...]\n", getprogname());
13831 static const struct got_error
*
13832 print_stage(void *arg
, unsigned char status
, unsigned char staged_status
,
13833 const char *path
, struct got_object_id
*blob_id
,
13834 struct got_object_id
*staged_blob_id
, struct got_object_id
*commit_id
,
13835 int dirfd
, const char *de_name
)
13837 const struct got_error
*err
= NULL
;
13838 char *id_str
= NULL
;
13840 if (staged_status
!= GOT_STATUS_ADD
&&
13841 staged_status
!= GOT_STATUS_MODIFY
&&
13842 staged_status
!= GOT_STATUS_DELETE
)
13845 if (staged_status
== GOT_STATUS_ADD
||
13846 staged_status
== GOT_STATUS_MODIFY
)
13847 err
= got_object_id_str(&id_str
, staged_blob_id
);
13849 err
= got_object_id_str(&id_str
, blob_id
);
13853 printf("%s %c %s\n", id_str
, staged_status
, path
);
13858 static const struct got_error
*
13859 cmd_stage(int argc
, char *argv
[])
13861 const struct got_error
*error
= NULL
;
13862 struct got_repository
*repo
= NULL
;
13863 struct got_worktree
*worktree
= NULL
;
13865 struct got_pathlist_head paths
;
13866 int ch
, list_stage
= 0, pflag
= 0, allow_bad_symlinks
= 0;
13867 FILE *patch_script_file
= NULL
;
13868 const char *patch_script_path
= NULL
;
13869 struct choose_patch_arg cpa
;
13870 int *pack_fds
= NULL
;
13872 TAILQ_INIT(&paths
);
13875 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13876 "unveil", NULL
) == -1)
13880 while ((ch
= getopt(argc
, argv
, "F:lpS")) != -1) {
13883 patch_script_path
= optarg
;
13892 allow_bad_symlinks
= 1;
13903 if (list_stage
&& (pflag
|| patch_script_path
))
13904 errx(1, "-l option cannot be used with other options");
13905 if (patch_script_path
&& !pflag
)
13906 errx(1, "-F option can only be used together with -p option");
13908 cwd
= getcwd(NULL
, 0);
13910 error
= got_error_from_errno("getcwd");
13914 error
= got_repo_pack_fds_open(&pack_fds
);
13918 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
13920 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
13921 error
= wrap_not_worktree_error(error
, "stage", cwd
);
13925 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
13930 if (patch_script_path
) {
13931 patch_script_file
= fopen(patch_script_path
, "re");
13932 if (patch_script_file
== NULL
) {
13933 error
= got_error_from_errno2("fopen",
13934 patch_script_path
);
13938 error
= apply_unveil(got_repo_get_path(repo
), 0,
13939 got_worktree_get_root_path(worktree
));
13943 error
= check_merge_in_progress(worktree
, repo
);
13947 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
13952 error
= got_worktree_status(worktree
, &paths
, repo
, 0,
13953 print_stage
, NULL
, check_cancelled
, NULL
);
13955 cpa
.patch_script_file
= patch_script_file
;
13956 cpa
.action
= "stage";
13957 error
= got_worktree_stage(worktree
, &paths
,
13958 pflag
? NULL
: print_status
, NULL
,
13959 pflag
? choose_patch
: NULL
, &cpa
,
13960 allow_bad_symlinks
, repo
);
13963 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
13965 error
= got_error_from_errno2("fclose", patch_script_path
);
13967 const struct got_error
*close_err
= got_repo_close(repo
);
13972 got_worktree_close(worktree
);
13974 const struct got_error
*pack_err
=
13975 got_repo_pack_fds_close(pack_fds
);
13979 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
13985 usage_unstage(void)
13987 fprintf(stderr
, "usage: %s unstage [-p] [-F response-script] "
13988 "[path ...]\n", getprogname());
13993 static const struct got_error
*
13994 cmd_unstage(int argc
, char *argv
[])
13996 const struct got_error
*error
= NULL
;
13997 struct got_repository
*repo
= NULL
;
13998 struct got_worktree
*worktree
= NULL
;
14000 struct got_pathlist_head paths
;
14002 struct got_update_progress_arg upa
;
14003 FILE *patch_script_file
= NULL
;
14004 const char *patch_script_path
= NULL
;
14005 struct choose_patch_arg cpa
;
14006 int *pack_fds
= NULL
;
14008 TAILQ_INIT(&paths
);
14011 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
14012 "unveil", NULL
) == -1)
14016 while ((ch
= getopt(argc
, argv
, "F:p")) != -1) {
14019 patch_script_path
= optarg
;
14033 if (patch_script_path
&& !pflag
)
14034 errx(1, "-F option can only be used together with -p option");
14036 cwd
= getcwd(NULL
, 0);
14038 error
= got_error_from_errno("getcwd");
14042 error
= got_repo_pack_fds_open(&pack_fds
);
14046 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
14048 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
14049 error
= wrap_not_worktree_error(error
, "unstage", cwd
);
14053 error
= got_repo_open(&repo
, got_worktree_get_repo_path(worktree
),
14058 if (patch_script_path
) {
14059 patch_script_file
= fopen(patch_script_path
, "re");
14060 if (patch_script_file
== NULL
) {
14061 error
= got_error_from_errno2("fopen",
14062 patch_script_path
);
14067 error
= apply_unveil(got_repo_get_path(repo
), 0,
14068 got_worktree_get_root_path(worktree
));
14072 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
, worktree
);
14076 cpa
.patch_script_file
= patch_script_file
;
14077 cpa
.action
= "unstage";
14078 memset(&upa
, 0, sizeof(upa
));
14079 error
= got_worktree_unstage(worktree
, &paths
, update_progress
,
14080 &upa
, pflag
? choose_patch
: NULL
, &cpa
, repo
);
14082 print_merge_progress_stats(&upa
);
14084 if (patch_script_file
&& fclose(patch_script_file
) == EOF
&&
14086 error
= got_error_from_errno2("fclose", patch_script_path
);
14088 const struct got_error
*close_err
= got_repo_close(repo
);
14093 got_worktree_close(worktree
);
14095 const struct got_error
*pack_err
=
14096 got_repo_pack_fds_close(pack_fds
);
14100 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);
14108 fprintf(stderr
, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14109 "arg ...\n", getprogname());
14113 static const struct got_error
*
14114 cat_blob(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
14116 const struct got_error
*err
;
14117 struct got_blob_object
*blob
;
14120 fd
= got_opentempfd();
14122 return got_error_from_errno("got_opentempfd");
14124 err
= got_object_open_as_blob(&blob
, repo
, id
, 8192, fd
);
14128 err
= got_object_blob_dump_to_file(NULL
, NULL
, NULL
, outfile
, blob
);
14130 if (fd
!= -1 && close(fd
) == -1 && err
== NULL
)
14131 err
= got_error_from_errno("close");
14133 got_object_blob_close(blob
);
14137 static const struct got_error
*
14138 cat_tree(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
14140 const struct got_error
*err
;
14141 struct got_tree_object
*tree
;
14144 err
= got_object_open_as_tree(&tree
, repo
, id
);
14148 nentries
= got_object_tree_get_nentries(tree
);
14149 for (i
= 0; i
< nentries
; i
++) {
14150 struct got_tree_entry
*te
;
14152 if (sigint_received
|| sigpipe_received
)
14154 te
= got_object_tree_get_entry(tree
, i
);
14155 err
= got_object_id_str(&id_str
, got_tree_entry_get_id(te
));
14158 fprintf(outfile
, "%s %.7o %s\n", id_str
,
14159 got_tree_entry_get_mode(te
),
14160 got_tree_entry_get_name(te
));
14164 got_object_tree_close(tree
);
14168 static const struct got_error
*
14169 cat_commit(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
14171 const struct got_error
*err
;
14172 struct got_commit_object
*commit
;
14173 const struct got_object_id_queue
*parent_ids
;
14174 struct got_object_qid
*pid
;
14175 char *id_str
= NULL
;
14176 const char *logmsg
= NULL
;
14179 err
= got_object_open_as_commit(&commit
, repo
, id
);
14183 err
= got_object_id_str(&id_str
, got_object_commit_get_tree_id(commit
));
14187 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_TREE
, id_str
);
14188 parent_ids
= got_object_commit_get_parent_ids(commit
);
14189 fprintf(outfile
, "numparents %d\n",
14190 got_object_commit_get_nparents(commit
));
14191 STAILQ_FOREACH(pid
, parent_ids
, entry
) {
14193 err
= got_object_id_str(&pid_str
, &pid
->id
);
14196 fprintf(outfile
, "%s%s\n", GOT_COMMIT_LABEL_PARENT
, pid_str
);
14199 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
14200 got_object_commit_get_author_gmtoff(commit
));
14201 fprintf(outfile
, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR
,
14202 got_object_commit_get_author(commit
),
14203 (long long)got_object_commit_get_author_time(commit
),
14206 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
14207 got_object_commit_get_committer_gmtoff(commit
));
14208 fprintf(outfile
, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER
,
14209 got_object_commit_get_committer(commit
),
14210 (long long)got_object_commit_get_committer_time(commit
),
14213 logmsg
= got_object_commit_get_logmsg_raw(commit
);
14214 fprintf(outfile
, "messagelen %zd\n", strlen(logmsg
));
14215 fprintf(outfile
, "%s", logmsg
);
14218 got_object_commit_close(commit
);
14222 static const struct got_error
*
14223 cat_tag(struct got_object_id
*id
, struct got_repository
*repo
, FILE *outfile
)
14225 const struct got_error
*err
;
14226 struct got_tag_object
*tag
;
14227 char *id_str
= NULL
;
14228 const char *tagmsg
= NULL
;
14231 err
= got_object_open_as_tag(&tag
, repo
, id
);
14235 err
= got_object_id_str(&id_str
, got_object_tag_get_object_id(tag
));
14239 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_OBJECT
, id_str
);
14241 switch (got_object_tag_get_object_type(tag
)) {
14242 case GOT_OBJ_TYPE_BLOB
:
14243 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
14244 GOT_OBJ_LABEL_BLOB
);
14246 case GOT_OBJ_TYPE_TREE
:
14247 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
14248 GOT_OBJ_LABEL_TREE
);
14250 case GOT_OBJ_TYPE_COMMIT
:
14251 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
14252 GOT_OBJ_LABEL_COMMIT
);
14254 case GOT_OBJ_TYPE_TAG
:
14255 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TYPE
,
14256 GOT_OBJ_LABEL_TAG
);
14262 fprintf(outfile
, "%s%s\n", GOT_TAG_LABEL_TAG
,
14263 got_object_tag_get_name(tag
));
14265 got_date_format_gmtoff(gmtoff
, sizeof(gmtoff
),
14266 got_object_tag_get_tagger_gmtoff(tag
));
14267 fprintf(outfile
, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER
,
14268 got_object_tag_get_tagger(tag
),
14269 (long long)got_object_tag_get_tagger_time(tag
),
14272 tagmsg
= got_object_tag_get_message(tag
);
14273 fprintf(outfile
, "messagelen %zd\n", strlen(tagmsg
));
14274 fprintf(outfile
, "%s", tagmsg
);
14277 got_object_tag_close(tag
);
14281 static const struct got_error
*
14282 cmd_cat(int argc
, char *argv
[])
14284 const struct got_error
*error
;
14285 struct got_repository
*repo
= NULL
;
14286 struct got_worktree
*worktree
= NULL
;
14287 char *cwd
= NULL
, *repo_path
= NULL
, *label
= NULL
;
14288 char *keyword_idstr
= NULL
;
14289 const char *commit_id_str
= NULL
;
14290 struct got_object_id
*id
= NULL
, *commit_id
= NULL
;
14291 struct got_commit_object
*commit
= NULL
;
14292 int ch
, obj_type
, i
, force_path
= 0;
14293 struct got_reflist_head refs
;
14294 int *pack_fds
= NULL
;
14299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14304 while ((ch
= getopt(argc
, argv
, "c:Pr:")) != -1) {
14307 commit_id_str
= optarg
;
14313 repo_path
= realpath(optarg
, NULL
);
14314 if (repo_path
== NULL
)
14315 return got_error_from_errno2("realpath",
14317 got_path_strip_trailing_slashes(repo_path
);
14328 cwd
= getcwd(NULL
, 0);
14330 error
= got_error_from_errno("getcwd");
14334 error
= got_repo_pack_fds_open(&pack_fds
);
14338 if (repo_path
== NULL
) {
14339 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
14340 if (error
&& error
->code
!= GOT_ERR_NOT_WORKTREE
)
14343 repo_path
= strdup(
14344 got_worktree_get_repo_path(worktree
));
14345 if (repo_path
== NULL
) {
14346 error
= got_error_from_errno("strdup");
14350 if (commit_id_str
== NULL
) {
14351 /* Release work tree lock. */
14352 got_worktree_close(worktree
);
14358 if (repo_path
== NULL
) {
14359 repo_path
= strdup(cwd
);
14360 if (repo_path
== NULL
)
14361 return got_error_from_errno("strdup");
14364 error
= got_repo_open(&repo
, repo_path
, NULL
, pack_fds
);
14369 error
= apply_unveil(got_repo_get_path(repo
), 1, NULL
);
14373 error
= got_ref_list(&refs
, repo
, NULL
, got_ref_cmp_by_name
, NULL
);
14377 if (commit_id_str
!= NULL
) {
14378 error
= got_keyword_to_idstr(&keyword_idstr
, commit_id_str
,
14382 if (keyword_idstr
!= NULL
)
14383 commit_id_str
= keyword_idstr
;
14384 if (worktree
!= NULL
) {
14385 got_worktree_close(worktree
);
14389 commit_id_str
= GOT_REF_HEAD
;
14390 error
= got_repo_match_object_id(&commit_id
, NULL
,
14391 commit_id_str
, GOT_OBJ_TYPE_COMMIT
, &refs
, repo
);
14395 error
= got_object_open_as_commit(&commit
, repo
, commit_id
);
14399 for (i
= 0; i
< argc
; i
++) {
14401 error
= got_object_id_by_path(&id
, repo
, commit
,
14406 error
= got_repo_match_object_id(&id
, &label
, argv
[i
],
14407 GOT_OBJ_TYPE_ANY
, NULL
/* do not resolve tags */,
14410 if (error
->code
!= GOT_ERR_BAD_OBJ_ID_STR
&&
14411 error
->code
!= GOT_ERR_NOT_REF
)
14413 error
= got_object_id_by_path(&id
, repo
,
14420 error
= got_object_get_type(&obj_type
, repo
, id
);
14424 switch (obj_type
) {
14425 case GOT_OBJ_TYPE_BLOB
:
14426 error
= cat_blob(id
, repo
, stdout
);
14428 case GOT_OBJ_TYPE_TREE
:
14429 error
= cat_tree(id
, repo
, stdout
);
14431 case GOT_OBJ_TYPE_COMMIT
:
14432 error
= cat_commit(id
, repo
, stdout
);
14434 case GOT_OBJ_TYPE_TAG
:
14435 error
= cat_tag(id
, repo
, stdout
);
14438 error
= got_error(GOT_ERR_OBJ_TYPE
);
14452 free(keyword_idstr
);
14454 got_object_commit_close(commit
);
14456 got_worktree_close(worktree
);
14458 const struct got_error
*close_err
= got_repo_close(repo
);
14463 const struct got_error
*pack_err
=
14464 got_repo_pack_fds_close(pack_fds
);
14469 got_ref_list_free(&refs
);
14476 fprintf(stderr
, "usage: %s info [path ...]\n",
14481 static const struct got_error
*
14482 print_path_info(void *arg
, const char *path
, mode_t mode
, time_t mtime
,
14483 struct got_object_id
*blob_id
, struct got_object_id
*staged_blob_id
,
14484 struct got_object_id
*commit_id
)
14486 const struct got_error
*err
= NULL
;
14487 char *id_str
= NULL
;
14489 struct tm mytm
, *tm
;
14490 struct got_pathlist_head
*paths
= arg
;
14491 struct got_pathlist_entry
*pe
;
14494 * Clear error indication from any of the path arguments which
14495 * would cause this file index entry to be displayed.
14497 TAILQ_FOREACH(pe
, paths
, entry
) {
14498 if (got_path_cmp(path
, pe
->path
, strlen(path
),
14499 pe
->path_len
) == 0 ||
14500 got_path_is_child(path
, pe
->path
, pe
->path_len
))
14501 pe
->data
= NULL
; /* no error */
14504 printf(GOT_COMMIT_SEP_STR
);
14506 printf("symlink: %s\n", path
);
14507 else if (S_ISREG(mode
)) {
14508 printf("file: %s\n", path
);
14509 printf("mode: %o\n", mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
));
14510 } else if (S_ISDIR(mode
))
14511 printf("directory: %s\n", path
);
14513 printf("something: %s\n", path
);
14515 tm
= localtime_r(&mtime
, &mytm
);
14518 if (strftime(datebuf
, sizeof(datebuf
), "%c %Z", tm
) == 0)
14519 return got_error(GOT_ERR_NO_SPACE
);
14520 printf("timestamp: %s\n", datebuf
);
14523 err
= got_object_id_str(&id_str
, blob_id
);
14526 printf("based on blob: %s\n", id_str
);
14530 if (staged_blob_id
) {
14531 err
= got_object_id_str(&id_str
, staged_blob_id
);
14534 printf("based on staged blob: %s\n", id_str
);
14539 err
= got_object_id_str(&id_str
, commit_id
);
14542 printf("based on commit: %s\n", id_str
);
14549 static const struct got_error
*
14550 cmd_info(int argc
, char *argv
[])
14552 const struct got_error
*error
= NULL
;
14553 struct got_worktree
*worktree
= NULL
;
14554 char *cwd
= NULL
, *id_str
= NULL
;
14555 struct got_pathlist_head paths
;
14556 char *uuidstr
= NULL
;
14557 int ch
, show_files
= 0;
14559 TAILQ_INIT(&paths
);
14562 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14567 while ((ch
= getopt(argc
, argv
, "")) != -1) {
14578 cwd
= getcwd(NULL
, 0);
14580 error
= got_error_from_errno("getcwd");
14584 error
= got_worktree_open(&worktree
, cwd
, GOT_WORKTREE_GOT_DIR
);
14586 if (error
->code
== GOT_ERR_NOT_WORKTREE
)
14587 error
= wrap_not_worktree_error(error
, "info", cwd
);
14592 /* Remove "wpath cpath proc exec sendfd" promises. */
14593 if (pledge("stdio rpath flock unveil", NULL
) == -1)
14596 error
= apply_unveil(NULL
, 0, got_worktree_get_root_path(worktree
));
14601 error
= get_worktree_paths_from_argv(&paths
, argc
, argv
,
14608 error
= got_object_id_str(&id_str
,
14609 got_worktree_get_base_commit_id(worktree
));
14613 error
= got_worktree_get_uuid(&uuidstr
, worktree
);
14617 printf("work tree: %s\n", got_worktree_get_root_path(worktree
));
14618 printf("work tree base commit: %s\n", id_str
);
14619 printf("work tree path prefix: %s\n",
14620 got_worktree_get_path_prefix(worktree
));
14621 printf("work tree branch reference: %s\n",
14622 got_worktree_get_head_ref_name(worktree
));
14623 printf("work tree UUID: %s\n", uuidstr
);
14624 printf("repository: %s\n", got_worktree_get_repo_path(worktree
));
14627 struct got_pathlist_entry
*pe
;
14628 TAILQ_FOREACH(pe
, &paths
, entry
) {
14629 if (pe
->path_len
== 0)
14632 * Assume this path will fail. This will be corrected
14633 * in print_path_info() in case the path does suceeed.
14635 pe
->data
= (void *)got_error(GOT_ERR_BAD_PATH
);
14637 error
= got_worktree_path_info(worktree
, &paths
,
14638 print_path_info
, &paths
, check_cancelled
, NULL
);
14641 TAILQ_FOREACH(pe
, &paths
, entry
) {
14642 if (pe
->data
!= NULL
) {
14643 const struct got_error
*perr
;
14646 error
= got_error_fmt(perr
->code
, "%s",
14654 got_worktree_close(worktree
);
14655 got_pathlist_free(&paths
, GOT_PATHLIST_FREE_PATH
);