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 <sys/queue.h>
21 #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"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content, size_t initial_content_len,
394 int require_modification)
396 const struct got_error *err = NULL;
401 size_t len, logmsg_len;
402 char *initial_content_stripped = NULL, *buf = NULL, *s;
406 if (stat(logmsg_path, &st) == -1)
407 return got_error_from_errno2("stat", logmsg_path);
409 if (spawn_editor(editor, logmsg_path) == -1)
410 return got_error_from_errno("failed spawning editor");
412 if (stat(logmsg_path, &st2) == -1)
413 return got_error_from_errno("stat");
415 if (require_modification &&
416 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
417 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
418 "no changes made to commit message, aborting");
421 * Set up a stripped version of the initial content without comments
422 * and blank lines. We need this in order to check if the message
423 * has in fact been edited.
425 initial_content_stripped = malloc(initial_content_len + 1);
426 if (initial_content_stripped == NULL)
427 return got_error_from_errno("malloc");
428 initial_content_stripped[0] = '\0';
430 buf = strdup(initial_content);
432 err = got_error_from_errno("strdup");
437 while ((line = strsep(&s, "\n")) != NULL) {
438 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
439 continue; /* remove comments and leading empty lines */
440 len = strlcat(initial_content_stripped, line,
441 initial_content_len + 1);
442 if (len >= initial_content_len + 1) {
443 err = got_error(GOT_ERR_NO_SPACE);
447 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
448 initial_content_stripped[len - 1] = '\0';
452 logmsg_len = st2.st_size;
453 *logmsg = malloc(logmsg_len + 1);
455 return got_error_from_errno("malloc");
458 fp = fopen(logmsg_path, "re");
460 err = got_error_from_errno("fopen");
465 while (getline(&line, &linesize, fp) != -1) {
466 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
467 continue; /* remove comments and leading empty lines */
468 len = strlcat(*logmsg, line, logmsg_len + 1);
469 if (len >= logmsg_len + 1) {
470 err = got_error(GOT_ERR_NO_SPACE);
476 err = got_ferror(fp, GOT_ERR_IO);
479 while (len > 0 && (*logmsg)[len - 1] == '\n') {
480 (*logmsg)[len - 1] = '\0';
485 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
486 "commit message cannot be empty, aborting");
489 if (require_modification &&
490 strcmp(*logmsg, initial_content_stripped) == 0)
491 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
492 "no changes made to commit message, aborting");
494 free(initial_content_stripped);
496 if (fp && fclose(fp) == EOF && err == NULL)
497 err = got_error_from_errno("fclose");
505 static const struct got_error *
506 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
507 const char *path_dir, const char *branch_name)
509 char *initial_content = NULL;
510 const struct got_error *err = NULL;
511 int initial_content_len;
514 initial_content_len = asprintf(&initial_content,
515 "\n# %s to be imported to branch %s\n", path_dir,
517 if (initial_content_len == -1)
518 return got_error_from_errno("asprintf");
520 err = got_opentemp_named_fd(logmsg_path, &fd,
521 GOT_TMPDIR_STR "/got-importmsg", "");
525 if (write(fd, initial_content, initial_content_len) == -1) {
526 err = got_error_from_errno2("write", *logmsg_path);
530 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
531 initial_content_len, 1);
533 if (fd != -1 && close(fd) == -1 && err == NULL)
534 err = got_error_from_errno2("close", *logmsg_path);
535 free(initial_content);
543 static const struct got_error *
544 import_progress(void *arg, const char *path)
546 printf("A %s\n", path);
550 static const struct got_error *
551 valid_author(const char *author)
553 const char *email = author;
556 * Git' expects the author (or committer) to be in the form
557 * "name <email>", which are mostly free form (see the
558 * "committer" description in git-fast-import(1)). We're only
559 * doing this to avoid git's object parser breaking on commits
563 while (*author && *author != '\n' && *author != '<' && *author != '>')
565 if (author != email && *author == '<' && *(author - 1) != ' ')
566 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
567 "between author name and email required", email);
568 if (*author++ != '<')
569 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
570 while (*author && *author != '\n' && *author != '<' && *author != '>')
572 if (strcmp(author, ">") != 0)
573 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
577 static const struct got_error *
578 get_author(char **author, struct got_repository *repo,
579 struct got_worktree *worktree)
581 const struct got_error *err = NULL;
582 const char *got_author = NULL, *name, *email;
583 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
588 worktree_conf = got_worktree_get_gotconfig(worktree);
589 repo_conf = got_repo_get_gotconfig(repo);
592 * Priority of potential author information sources, from most
593 * significant to least significant:
594 * 1) work tree's .got/got.conf file
595 * 2) repository's got.conf file
596 * 3) repository's git config file
597 * 4) environment variables
598 * 5) global git config files (in user's home directory or /etc)
602 got_author = got_gotconfig_get_author(worktree_conf);
603 if (got_author == NULL)
604 got_author = got_gotconfig_get_author(repo_conf);
605 if (got_author == NULL) {
606 name = got_repo_get_gitconfig_author_name(repo);
607 email = got_repo_get_gitconfig_author_email(repo);
609 if (asprintf(author, "%s <%s>", name, email) == -1)
610 return got_error_from_errno("asprintf");
614 got_author = getenv("GOT_AUTHOR");
615 if (got_author == NULL) {
616 name = got_repo_get_global_gitconfig_author_name(repo);
617 email = got_repo_get_global_gitconfig_author_email(
620 if (asprintf(author, "%s <%s>", name, email)
622 return got_error_from_errno("asprintf");
625 /* TODO: Look up user in password database? */
626 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
630 *author = strdup(got_author);
632 return got_error_from_errno("strdup");
634 err = valid_author(*author);
642 static const struct got_error *
643 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
644 struct got_worktree *worktree)
646 const char *got_allowed_signers = NULL;
647 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
649 *allowed_signers = NULL;
652 worktree_conf = got_worktree_get_gotconfig(worktree);
653 repo_conf = got_repo_get_gotconfig(repo);
656 * Priority of potential author information sources, from most
657 * significant to least significant:
658 * 1) work tree's .got/got.conf file
659 * 2) repository's got.conf file
663 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
665 if (got_allowed_signers == NULL)
666 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
669 if (got_allowed_signers) {
670 *allowed_signers = strdup(got_allowed_signers);
671 if (*allowed_signers == NULL)
672 return got_error_from_errno("strdup");
677 static const struct got_error *
678 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
679 struct got_worktree *worktree)
681 const char *got_revoked_signers = NULL;
682 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
684 *revoked_signers = NULL;
687 worktree_conf = got_worktree_get_gotconfig(worktree);
688 repo_conf = got_repo_get_gotconfig(repo);
691 * Priority of potential author information sources, from most
692 * significant to least significant:
693 * 1) work tree's .got/got.conf file
694 * 2) repository's got.conf file
698 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
700 if (got_revoked_signers == NULL)
701 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
704 if (got_revoked_signers) {
705 *revoked_signers = strdup(got_revoked_signers);
706 if (*revoked_signers == NULL)
707 return got_error_from_errno("strdup");
713 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
715 const char *got_signer_id = NULL;
716 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
719 worktree_conf = got_worktree_get_gotconfig(worktree);
720 repo_conf = got_repo_get_gotconfig(repo);
723 * Priority of potential author information sources, from most
724 * significant to least significant:
725 * 1) work tree's .got/got.conf file
726 * 2) repository's got.conf file
730 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
731 if (got_signer_id == NULL)
732 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
734 return got_signer_id;
737 static const struct got_error *
738 get_gitconfig_path(char **gitconfig_path)
740 const char *homedir = getenv("HOME");
742 *gitconfig_path = NULL;
744 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
745 return got_error_from_errno("asprintf");
751 static const struct got_error *
752 cmd_import(int argc, char *argv[])
754 const struct got_error *error = NULL;
755 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
756 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
757 const char *branch_name = NULL;
758 char *id_str = NULL, *logmsg_path = NULL;
759 char refname[PATH_MAX] = "refs/heads/";
760 struct got_repository *repo = NULL;
761 struct got_reference *branch_ref = NULL, *head_ref = NULL;
762 struct got_object_id *new_commit_id = NULL;
764 struct got_pathlist_head ignores;
765 struct got_pathlist_entry *pe;
766 int preserve_logmsg = 0;
767 int *pack_fds = NULL;
769 TAILQ_INIT(&ignores);
771 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
774 branch_name = optarg;
777 if (optarg[0] == '\0')
779 error = got_pathlist_insert(&pe, &ignores, optarg,
785 logmsg = strdup(optarg);
786 if (logmsg == NULL) {
787 error = got_error_from_errno("strdup");
792 repo_path = realpath(optarg, NULL);
793 if (repo_path == NULL) {
794 error = got_error_from_errno2("realpath",
809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
817 if (repo_path == NULL) {
818 repo_path = getcwd(NULL, 0);
819 if (repo_path == NULL)
820 return got_error_from_errno("getcwd");
822 got_path_strip_trailing_slashes(repo_path);
823 error = get_gitconfig_path(&gitconfig_path);
826 error = got_repo_pack_fds_open(&pack_fds);
829 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
833 error = get_author(&author, repo, NULL);
838 * Don't let the user create a branch name with a leading '-'.
839 * While technically a valid reference name, this case is usually
840 * an unintended typo.
842 if (branch_name && branch_name[0] == '-')
843 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
845 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
846 if (error && error->code != GOT_ERR_NOT_REF)
850 n = strlcat(refname, branch_name, sizeof(refname));
851 else if (head_ref && got_ref_is_symbolic(head_ref))
852 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
855 n = strlcat(refname, "main", sizeof(refname));
856 if (n >= sizeof(refname)) {
857 error = got_error(GOT_ERR_NO_SPACE);
861 error = got_ref_open(&branch_ref, repo, refname, 0);
863 if (error->code != GOT_ERR_NOT_REF)
866 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
867 "import target branch already exists");
871 path_dir = realpath(argv[0], NULL);
872 if (path_dir == NULL) {
873 error = got_error_from_errno2("realpath", argv[0]);
876 got_path_strip_trailing_slashes(path_dir);
879 * unveil(2) traverses exec(2); if an editor is used we have
880 * to apply unveil after the log message has been written.
882 if (logmsg == NULL || strlen(logmsg) == 0) {
883 error = get_editor(&editor);
887 error = collect_import_msg(&logmsg, &logmsg_path, editor,
890 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
897 if (unveil(path_dir, "r") != 0) {
898 error = got_error_from_errno2("unveil", path_dir);
904 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
911 error = got_repo_import(&new_commit_id, path_dir, logmsg,
912 author, &ignores, repo, import_progress, NULL);
919 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
926 error = got_ref_write(branch_ref, repo);
933 error = got_object_id_str(&id_str, new_commit_id);
940 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
942 if (error->code != GOT_ERR_NOT_REF) {
948 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
956 error = got_ref_write(head_ref, repo);
964 printf("Created branch %s with commit %s\n",
965 got_ref_get_name(branch_ref), id_str);
968 const struct got_error *pack_err =
969 got_repo_pack_fds_close(pack_fds);
973 if (preserve_logmsg) {
974 fprintf(stderr, "%s: log message preserved in %s\n",
975 getprogname(), logmsg_path);
976 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
977 error = got_error_from_errno2("unlink", logmsg_path);
985 free(gitconfig_path);
987 got_ref_close(branch_ref);
989 got_ref_close(head_ref);
996 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
997 "repository-URL [directory]\n", getprogname());
1001 struct got_fetch_progress_arg {
1002 char last_scaled_size[FMT_SCALED_STRSIZE];
1004 int last_p_resolved;
1007 struct got_repository *repo;
1010 int configs_created;
1012 struct got_pathlist_head *symrefs;
1013 struct got_pathlist_head *wanted_branches;
1014 struct got_pathlist_head *wanted_refs;
1018 const char *remote_repo_path;
1019 const char *git_url;
1020 int fetch_all_branches;
1021 int mirror_references;
1025 /* XXX forward declaration */
1026 static const struct got_error *
1027 create_config_files(const char *proto, const char *host, const char *port,
1028 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1029 int mirror_references, struct got_pathlist_head *symrefs,
1030 struct got_pathlist_head *wanted_branches,
1031 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1033 static const struct got_error *
1034 fetch_progress(void *arg, const char *message, off_t packfile_size,
1035 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1037 const struct got_error *err = NULL;
1038 struct got_fetch_progress_arg *a = arg;
1039 char scaled_size[FMT_SCALED_STRSIZE];
1040 int p_indexed, p_resolved;
1041 int print_size = 0, print_indexed = 0, print_resolved = 0;
1044 * In order to allow a failed clone to be resumed with 'got fetch'
1045 * we try to create configuration files as soon as possible.
1046 * Once the server has sent information about its default branch
1047 * we have all required information.
1049 if (a->create_configs && !a->configs_created &&
1050 !TAILQ_EMPTY(a->config_info.symrefs)) {
1051 err = create_config_files(a->config_info.proto,
1052 a->config_info.host, a->config_info.port,
1053 a->config_info.remote_repo_path,
1054 a->config_info.git_url,
1055 a->config_info.fetch_all_branches,
1056 a->config_info.mirror_references,
1057 a->config_info.symrefs,
1058 a->config_info.wanted_branches,
1059 a->config_info.wanted_refs, a->repo);
1062 a->configs_created = 1;
1065 if (a->verbosity < 0)
1068 if (message && message[0] != '\0') {
1069 printf("\rserver: %s", message);
1074 if (packfile_size > 0 || nobj_indexed > 0) {
1075 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1076 (a->last_scaled_size[0] == '\0' ||
1077 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1079 if (strlcpy(a->last_scaled_size, scaled_size,
1080 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1081 return got_error(GOT_ERR_NO_SPACE);
1083 if (nobj_indexed > 0) {
1084 p_indexed = (nobj_indexed * 100) / nobj_total;
1085 if (p_indexed != a->last_p_indexed) {
1086 a->last_p_indexed = p_indexed;
1091 if (nobj_resolved > 0) {
1092 p_resolved = (nobj_resolved * 100) /
1093 (nobj_total - nobj_loose);
1094 if (p_resolved != a->last_p_resolved) {
1095 a->last_p_resolved = p_resolved;
1103 if (print_size || print_indexed || print_resolved)
1106 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1108 printf("; indexing %d%%", p_indexed);
1110 printf("; resolving deltas %d%%", p_resolved);
1111 if (print_size || print_indexed || print_resolved)
1117 static const struct got_error *
1118 create_symref(const char *refname, struct got_reference *target_ref,
1119 int verbosity, struct got_repository *repo)
1121 const struct got_error *err;
1122 struct got_reference *head_symref;
1124 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1128 err = got_ref_write(head_symref, repo);
1129 if (err == NULL && verbosity > 0) {
1130 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1131 got_ref_get_name(target_ref));
1133 got_ref_close(head_symref);
1137 static const struct got_error *
1138 list_remote_refs(struct got_pathlist_head *symrefs,
1139 struct got_pathlist_head *refs)
1141 const struct got_error *err;
1142 struct got_pathlist_entry *pe;
1144 TAILQ_FOREACH(pe, symrefs, entry) {
1145 const char *refname = pe->path;
1146 const char *targetref = pe->data;
1148 printf("%s: %s\n", refname, targetref);
1151 TAILQ_FOREACH(pe, refs, entry) {
1152 const char *refname = pe->path;
1153 struct got_object_id *id = pe->data;
1156 err = got_object_id_str(&id_str, id);
1159 printf("%s: %s\n", refname, id_str);
1166 static const struct got_error *
1167 create_ref(const char *refname, struct got_object_id *id,
1168 int verbosity, struct got_repository *repo)
1170 const struct got_error *err = NULL;
1171 struct got_reference *ref;
1174 err = got_object_id_str(&id_str, id);
1178 err = got_ref_alloc(&ref, refname, id);
1182 err = got_ref_write(ref, repo);
1185 if (err == NULL && verbosity >= 0)
1186 printf("Created reference %s: %s\n", refname, id_str);
1193 match_wanted_ref(const char *refname, const char *wanted_ref)
1195 if (strncmp(refname, "refs/", 5) != 0)
1200 * Prevent fetching of references that won't make any
1201 * sense outside of the remote repository's context.
1203 if (strncmp(refname, "got/", 4) == 0)
1205 if (strncmp(refname, "remotes/", 8) == 0)
1208 if (strncmp(wanted_ref, "refs/", 5) == 0)
1211 /* Allow prefix match. */
1212 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1215 /* Allow exact match. */
1216 return (strcmp(refname, wanted_ref) == 0);
1220 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1222 struct got_pathlist_entry *pe;
1224 TAILQ_FOREACH(pe, wanted_refs, entry) {
1225 if (match_wanted_ref(refname, pe->path))
1232 static const struct got_error *
1233 create_wanted_ref(const char *refname, struct got_object_id *id,
1234 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1236 const struct got_error *err;
1237 char *remote_refname;
1239 if (strncmp("refs/", refname, 5) == 0)
1242 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1243 remote_repo_name, refname) == -1)
1244 return got_error_from_errno("asprintf");
1246 err = create_ref(remote_refname, id, verbosity, repo);
1247 free(remote_refname);
1251 static const struct got_error *
1252 create_gotconfig(const char *proto, const char *host, const char *port,
1253 const char *remote_repo_path, const char *default_branch,
1254 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1255 struct got_pathlist_head *wanted_refs, int mirror_references,
1256 struct got_repository *repo)
1258 const struct got_error *err = NULL;
1259 char *gotconfig_path = NULL;
1260 char *gotconfig = NULL;
1261 FILE *gotconfig_file = NULL;
1262 const char *branchname = NULL;
1263 char *branches = NULL, *refs = NULL;
1266 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1267 struct got_pathlist_entry *pe;
1268 TAILQ_FOREACH(pe, wanted_branches, entry) {
1270 branchname = pe->path;
1271 if (strncmp(branchname, "refs/heads/", 11) == 0)
1273 if (asprintf(&s, "%s\"%s\" ",
1274 branches ? branches : "", branchname) == -1) {
1275 err = got_error_from_errno("asprintf");
1281 } else if (!fetch_all_branches && default_branch) {
1282 branchname = default_branch;
1283 if (strncmp(branchname, "refs/heads/", 11) == 0)
1285 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1286 err = got_error_from_errno("asprintf");
1290 if (!TAILQ_EMPTY(wanted_refs)) {
1291 struct got_pathlist_entry *pe;
1292 TAILQ_FOREACH(pe, wanted_refs, entry) {
1294 const char *refname = pe->path;
1295 if (strncmp(refname, "refs/", 5) == 0)
1297 if (asprintf(&s, "%s\"%s\" ",
1298 refs ? refs : "", refname) == -1) {
1299 err = got_error_from_errno("asprintf");
1307 /* Create got.conf(5). */
1308 gotconfig_path = got_repo_get_path_gotconfig(repo);
1309 if (gotconfig_path == NULL) {
1310 err = got_error_from_errno("got_repo_get_path_gotconfig");
1313 gotconfig_file = fopen(gotconfig_path, "ae");
1314 if (gotconfig_file == NULL) {
1315 err = got_error_from_errno2("fopen", gotconfig_path);
1318 if (asprintf(&gotconfig,
1323 "\trepository \"%s\"\n"
1329 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1330 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1331 remote_repo_path, branches ? "\tbranch { " : "",
1332 branches ? branches : "", branches ? "}\n" : "",
1333 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1334 mirror_references ? "\tmirror_references yes\n" : "",
1335 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1336 err = got_error_from_errno("asprintf");
1339 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1340 if (n != strlen(gotconfig)) {
1341 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1346 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1347 err = got_error_from_errno2("fclose", gotconfig_path);
1348 free(gotconfig_path);
1353 static const struct got_error *
1354 create_gitconfig(const char *git_url, const char *default_branch,
1355 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1356 struct got_pathlist_head *wanted_refs, int mirror_references,
1357 struct got_repository *repo)
1359 const struct got_error *err = NULL;
1360 char *gitconfig_path = NULL;
1361 char *gitconfig = NULL;
1362 FILE *gitconfig_file = NULL;
1363 char *branches = NULL, *refs = NULL;
1364 const char *branchname;
1367 /* Create a config file Git can understand. */
1368 gitconfig_path = got_repo_get_path_gitconfig(repo);
1369 if (gitconfig_path == NULL) {
1370 err = got_error_from_errno("got_repo_get_path_gitconfig");
1373 gitconfig_file = fopen(gitconfig_path, "ae");
1374 if (gitconfig_file == NULL) {
1375 err = got_error_from_errno2("fopen", gitconfig_path);
1378 if (fetch_all_branches) {
1379 if (mirror_references) {
1380 if (asprintf(&branches,
1381 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1382 err = got_error_from_errno("asprintf");
1385 } else if (asprintf(&branches,
1386 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1387 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1388 err = got_error_from_errno("asprintf");
1391 } else if (!TAILQ_EMPTY(wanted_branches)) {
1392 struct got_pathlist_entry *pe;
1393 TAILQ_FOREACH(pe, wanted_branches, entry) {
1395 branchname = pe->path;
1396 if (strncmp(branchname, "refs/heads/", 11) == 0)
1398 if (mirror_references) {
1400 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1401 branches ? branches : "",
1402 branchname, branchname) == -1) {
1403 err = got_error_from_errno("asprintf");
1406 } else if (asprintf(&s,
1407 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1408 branches ? branches : "",
1409 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1410 branchname) == -1) {
1411 err = got_error_from_errno("asprintf");
1419 * If the server specified a default branch, use just that one.
1420 * Otherwise fall back to fetching all branches on next fetch.
1422 if (default_branch) {
1423 branchname = default_branch;
1424 if (strncmp(branchname, "refs/heads/", 11) == 0)
1427 branchname = "*"; /* fall back to all branches */
1428 if (mirror_references) {
1429 if (asprintf(&branches,
1430 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1431 branchname, branchname) == -1) {
1432 err = got_error_from_errno("asprintf");
1435 } else if (asprintf(&branches,
1436 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1437 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1438 branchname) == -1) {
1439 err = got_error_from_errno("asprintf");
1443 if (!TAILQ_EMPTY(wanted_refs)) {
1444 struct got_pathlist_entry *pe;
1445 TAILQ_FOREACH(pe, wanted_refs, entry) {
1447 const char *refname = pe->path;
1448 if (strncmp(refname, "refs/", 5) == 0)
1450 if (mirror_references) {
1452 "%s\tfetch = refs/%s:refs/%s\n",
1453 refs ? refs : "", refname, refname) == -1) {
1454 err = got_error_from_errno("asprintf");
1457 } else if (asprintf(&s,
1458 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1460 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1462 err = got_error_from_errno("asprintf");
1470 if (asprintf(&gitconfig,
1475 "\tfetch = refs/tags/*:refs/tags/*\n",
1476 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1477 refs ? refs : "") == -1) {
1478 err = got_error_from_errno("asprintf");
1481 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1482 if (n != strlen(gitconfig)) {
1483 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1487 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1488 err = got_error_from_errno2("fclose", gitconfig_path);
1489 free(gitconfig_path);
1494 static const struct got_error *
1495 create_config_files(const char *proto, const char *host, const char *port,
1496 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1497 int mirror_references, struct got_pathlist_head *symrefs,
1498 struct got_pathlist_head *wanted_branches,
1499 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1501 const struct got_error *err = NULL;
1502 const char *default_branch = NULL;
1503 struct got_pathlist_entry *pe;
1506 * If we asked for a set of wanted branches then use the first
1509 if (!TAILQ_EMPTY(wanted_branches)) {
1510 pe = TAILQ_FIRST(wanted_branches);
1511 default_branch = pe->path;
1513 /* First HEAD ref listed by server is the default branch. */
1514 TAILQ_FOREACH(pe, symrefs, entry) {
1515 const char *refname = pe->path;
1516 const char *target = pe->data;
1518 if (strcmp(refname, GOT_REF_HEAD) != 0)
1521 default_branch = target;
1526 /* Create got.conf(5). */
1527 err = create_gotconfig(proto, host, port, remote_repo_path,
1528 default_branch, fetch_all_branches, wanted_branches,
1529 wanted_refs, mirror_references, repo);
1533 /* Create a config file Git can understand. */
1534 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1535 wanted_branches, wanted_refs, mirror_references, repo);
1538 static const struct got_error *
1539 cmd_clone(int argc, char *argv[])
1541 const struct got_error *error = NULL;
1542 const char *uri, *dirname;
1543 char *proto, *host, *port, *repo_name, *server_path;
1544 char *default_destdir = NULL, *id_str = NULL;
1545 const char *repo_path;
1546 struct got_repository *repo = NULL;
1547 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1548 struct got_pathlist_entry *pe;
1549 struct got_object_id *pack_hash = NULL;
1550 int ch, fetchfd = -1, fetchstatus;
1551 pid_t fetchpid = -1;
1552 struct got_fetch_progress_arg fpa;
1553 char *git_url = NULL;
1554 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1555 int list_refs_only = 0;
1556 int *pack_fds = NULL;
1559 TAILQ_INIT(&symrefs);
1560 TAILQ_INIT(&wanted_branches);
1561 TAILQ_INIT(&wanted_refs);
1563 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1566 fetch_all_branches = 1;
1569 error = got_pathlist_append(&wanted_branches,
1578 mirror_references = 1;
1584 error = got_pathlist_append(&wanted_refs,
1592 else if (verbosity < 3)
1603 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1604 option_conflict('a', 'b');
1605 if (list_refs_only) {
1606 if (!TAILQ_EMPTY(&wanted_branches))
1607 option_conflict('l', 'b');
1608 if (fetch_all_branches)
1609 option_conflict('l', 'a');
1610 if (mirror_references)
1611 option_conflict('l', 'm');
1612 if (!TAILQ_EMPTY(&wanted_refs))
1613 option_conflict('l', 'R');
1625 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1630 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1631 host, port ? ":" : "", port ? port : "",
1632 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1633 error = got_error_from_errno("asprintf");
1637 if (strcmp(proto, "git") == 0) {
1639 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1640 "sendfd dns inet unveil", NULL) == -1)
1643 } else if (strcmp(proto, "git+ssh") == 0 ||
1644 strcmp(proto, "ssh") == 0) {
1646 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1647 "sendfd unveil", NULL) == -1)
1650 } else if (strcmp(proto, "http") == 0 ||
1651 strcmp(proto, "git+http") == 0) {
1652 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1655 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1658 if (dirname == NULL) {
1659 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1660 error = got_error_from_errno("asprintf");
1663 repo_path = default_destdir;
1665 repo_path = dirname;
1667 if (!list_refs_only) {
1668 error = got_path_mkdir(repo_path);
1670 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1671 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1673 if (!got_path_dir_is_empty(repo_path)) {
1674 error = got_error_path(repo_path,
1675 GOT_ERR_DIR_NOT_EMPTY);
1680 error = got_dial_apply_unveil(proto);
1684 error = apply_unveil(repo_path, 0, NULL);
1689 printf("Connecting to %s\n", git_url);
1691 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1692 server_path, verbosity);
1696 if (!list_refs_only) {
1697 error = got_repo_init(repo_path, NULL);
1700 error = got_repo_pack_fds_open(&pack_fds);
1703 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1708 fpa.last_scaled_size[0] = '\0';
1709 fpa.last_p_indexed = -1;
1710 fpa.last_p_resolved = -1;
1711 fpa.verbosity = verbosity;
1712 fpa.create_configs = 1;
1713 fpa.configs_created = 0;
1715 fpa.config_info.symrefs = &symrefs;
1716 fpa.config_info.wanted_branches = &wanted_branches;
1717 fpa.config_info.wanted_refs = &wanted_refs;
1718 fpa.config_info.proto = proto;
1719 fpa.config_info.host = host;
1720 fpa.config_info.port = port;
1721 fpa.config_info.remote_repo_path = server_path;
1722 fpa.config_info.git_url = git_url;
1723 fpa.config_info.fetch_all_branches = fetch_all_branches;
1724 fpa.config_info.mirror_references = mirror_references;
1725 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1726 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1727 fetch_all_branches, &wanted_branches, &wanted_refs,
1728 list_refs_only, verbosity, fetchfd, repo,
1729 fetch_progress, &fpa);
1733 if (list_refs_only) {
1734 error = list_remote_refs(&symrefs, &refs);
1738 if (pack_hash == NULL) {
1739 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1740 "server sent an empty pack file");
1743 error = got_object_id_str(&id_str, pack_hash);
1747 printf("\nFetched %s.pack\n", id_str);
1750 /* Set up references provided with the pack file. */
1751 TAILQ_FOREACH(pe, &refs, entry) {
1752 const char *refname = pe->path;
1753 struct got_object_id *id = pe->data;
1754 char *remote_refname;
1756 if (is_wanted_ref(&wanted_refs, refname) &&
1757 !mirror_references) {
1758 error = create_wanted_ref(refname, id,
1759 GOT_FETCH_DEFAULT_REMOTE_NAME,
1760 verbosity - 1, repo);
1766 error = create_ref(refname, id, verbosity - 1, repo);
1770 if (mirror_references)
1773 if (strncmp("refs/heads/", refname, 11) != 0)
1776 if (asprintf(&remote_refname,
1777 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1778 refname + 11) == -1) {
1779 error = got_error_from_errno("asprintf");
1782 error = create_ref(remote_refname, id, verbosity - 1, repo);
1783 free(remote_refname);
1788 /* Set the HEAD reference if the server provided one. */
1789 TAILQ_FOREACH(pe, &symrefs, entry) {
1790 struct got_reference *target_ref;
1791 const char *refname = pe->path;
1792 const char *target = pe->data;
1793 char *remote_refname = NULL, *remote_target = NULL;
1795 if (strcmp(refname, GOT_REF_HEAD) != 0)
1798 error = got_ref_open(&target_ref, repo, target, 0);
1800 if (error->code == GOT_ERR_NOT_REF) {
1807 error = create_symref(refname, target_ref, verbosity, repo);
1808 got_ref_close(target_ref);
1812 if (mirror_references)
1815 if (strncmp("refs/heads/", target, 11) != 0)
1818 if (asprintf(&remote_refname,
1819 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1821 error = got_error_from_errno("asprintf");
1824 if (asprintf(&remote_target,
1825 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1826 target + 11) == -1) {
1827 error = got_error_from_errno("asprintf");
1828 free(remote_refname);
1831 error = got_ref_open(&target_ref, repo, remote_target, 0);
1833 free(remote_refname);
1834 free(remote_target);
1835 if (error->code == GOT_ERR_NOT_REF) {
1841 error = create_symref(remote_refname, target_ref,
1842 verbosity - 1, repo);
1843 free(remote_refname);
1844 free(remote_target);
1845 got_ref_close(target_ref);
1851 * We failed to set the HEAD reference. If we asked for
1852 * a set of wanted branches use the first of one of those
1853 * which could be fetched instead.
1855 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1856 const char *target = pe->path;
1857 struct got_reference *target_ref;
1859 error = got_ref_open(&target_ref, repo, target, 0);
1861 if (error->code == GOT_ERR_NOT_REF) {
1868 error = create_symref(GOT_REF_HEAD, target_ref,
1870 got_ref_close(target_ref);
1876 if (!fpa.configs_created && pe != NULL) {
1877 error = create_config_files(fpa.config_info.proto,
1878 fpa.config_info.host, fpa.config_info.port,
1879 fpa.config_info.remote_repo_path,
1880 fpa.config_info.git_url,
1881 fpa.config_info.fetch_all_branches,
1882 fpa.config_info.mirror_references,
1883 fpa.config_info.symrefs,
1884 fpa.config_info.wanted_branches,
1885 fpa.config_info.wanted_refs, fpa.repo);
1892 printf("Created %s repository '%s'\n",
1893 mirror_references ? "mirrored" : "cloned", repo_path);
1896 const struct got_error *pack_err =
1897 got_repo_pack_fds_close(pack_fds);
1902 if (kill(fetchpid, SIGTERM) == -1)
1903 error = got_error_from_errno("kill");
1904 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1905 error = got_error_from_errno("waitpid");
1907 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1908 error = got_error_from_errno("close");
1910 const struct got_error *close_err = got_repo_close(repo);
1914 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1915 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1916 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1917 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1924 free(default_destdir);
1929 static const struct got_error *
1930 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1931 int replace_tags, int verbosity, struct got_repository *repo)
1933 const struct got_error *err = NULL;
1934 char *new_id_str = NULL;
1935 struct got_object_id *old_id = NULL;
1937 err = got_object_id_str(&new_id_str, new_id);
1941 if (!replace_tags &&
1942 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1943 err = got_ref_resolve(&old_id, repo, ref);
1946 if (got_object_id_cmp(old_id, new_id) == 0)
1948 if (verbosity >= 0) {
1949 printf("Rejecting update of existing tag %s: %s\n",
1950 got_ref_get_name(ref), new_id_str);
1955 if (got_ref_is_symbolic(ref)) {
1956 if (verbosity >= 0) {
1957 printf("Replacing reference %s: %s\n",
1958 got_ref_get_name(ref),
1959 got_ref_get_symref_target(ref));
1961 err = got_ref_change_symref_to_ref(ref, new_id);
1964 err = got_ref_write(ref, repo);
1968 err = got_ref_resolve(&old_id, repo, ref);
1971 if (got_object_id_cmp(old_id, new_id) == 0)
1974 err = got_ref_change_ref(ref, new_id);
1977 err = got_ref_write(ref, repo);
1983 printf("Updated %s: %s\n", got_ref_get_name(ref),
1991 static const struct got_error *
1992 update_symref(const char *refname, struct got_reference *target_ref,
1993 int verbosity, struct got_repository *repo)
1995 const struct got_error *err = NULL, *unlock_err;
1996 struct got_reference *symref;
1997 int symref_is_locked = 0;
1999 err = got_ref_open(&symref, repo, refname, 1);
2001 if (err->code != GOT_ERR_NOT_REF)
2003 err = got_ref_alloc_symref(&symref, refname, target_ref);
2007 err = got_ref_write(symref, repo);
2012 printf("Created reference %s: %s\n",
2013 got_ref_get_name(symref),
2014 got_ref_get_symref_target(symref));
2016 symref_is_locked = 1;
2018 if (strcmp(got_ref_get_symref_target(symref),
2019 got_ref_get_name(target_ref)) == 0)
2022 err = got_ref_change_symref(symref,
2023 got_ref_get_name(target_ref));
2027 err = got_ref_write(symref, repo);
2032 printf("Updated %s: %s\n", got_ref_get_name(symref),
2033 got_ref_get_symref_target(symref));
2037 if (symref_is_locked) {
2038 unlock_err = got_ref_unlock(symref);
2039 if (unlock_err && err == NULL)
2042 got_ref_close(symref);
2049 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2050 "[-R reference] [-r repository-path] [remote-repository]\n",
2055 static const struct got_error *
2056 delete_missing_ref(struct got_reference *ref,
2057 int verbosity, struct got_repository *repo)
2059 const struct got_error *err = NULL;
2060 struct got_object_id *id = NULL;
2061 char *id_str = NULL;
2063 if (got_ref_is_symbolic(ref)) {
2064 err = got_ref_delete(ref, repo);
2067 if (verbosity >= 0) {
2068 printf("Deleted %s: %s\n",
2069 got_ref_get_name(ref),
2070 got_ref_get_symref_target(ref));
2073 err = got_ref_resolve(&id, repo, ref);
2076 err = got_object_id_str(&id_str, id);
2080 err = got_ref_delete(ref, repo);
2083 if (verbosity >= 0) {
2084 printf("Deleted %s: %s\n",
2085 got_ref_get_name(ref), id_str);
2094 static const struct got_error *
2095 delete_missing_refs(struct got_pathlist_head *their_refs,
2096 struct got_pathlist_head *their_symrefs,
2097 const struct got_remote_repo *remote,
2098 int verbosity, struct got_repository *repo)
2100 const struct got_error *err = NULL, *unlock_err;
2101 struct got_reflist_head my_refs;
2102 struct got_reflist_entry *re;
2103 struct got_pathlist_entry *pe;
2104 char *remote_namespace = NULL;
2105 char *local_refname = NULL;
2107 TAILQ_INIT(&my_refs);
2109 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2111 return got_error_from_errno("asprintf");
2113 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2117 TAILQ_FOREACH(re, &my_refs, entry) {
2118 const char *refname = got_ref_get_name(re->ref);
2119 const char *their_refname;
2121 if (remote->mirror_references) {
2122 their_refname = refname;
2124 if (strncmp(refname, remote_namespace,
2125 strlen(remote_namespace)) == 0) {
2126 if (strcmp(refname + strlen(remote_namespace),
2129 if (asprintf(&local_refname, "refs/heads/%s",
2130 refname + strlen(remote_namespace)) == -1) {
2131 err = got_error_from_errno("asprintf");
2134 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2137 their_refname = local_refname;
2140 TAILQ_FOREACH(pe, their_refs, entry) {
2141 if (strcmp(their_refname, pe->path) == 0)
2147 TAILQ_FOREACH(pe, their_symrefs, entry) {
2148 if (strcmp(their_refname, pe->path) == 0)
2154 err = delete_missing_ref(re->ref, verbosity, repo);
2158 if (local_refname) {
2159 struct got_reference *ref;
2160 err = got_ref_open(&ref, repo, local_refname, 1);
2162 if (err->code != GOT_ERR_NOT_REF)
2164 free(local_refname);
2165 local_refname = NULL;
2168 err = delete_missing_ref(ref, verbosity, repo);
2171 unlock_err = got_ref_unlock(ref);
2173 if (unlock_err && err == NULL) {
2178 free(local_refname);
2179 local_refname = NULL;
2183 got_ref_list_free(&my_refs);
2184 free(remote_namespace);
2185 free(local_refname);
2189 static const struct got_error *
2190 update_wanted_ref(const char *refname, struct got_object_id *id,
2191 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2193 const struct got_error *err, *unlock_err;
2194 char *remote_refname;
2195 struct got_reference *ref;
2197 if (strncmp("refs/", refname, 5) == 0)
2200 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2201 remote_repo_name, refname) == -1)
2202 return got_error_from_errno("asprintf");
2204 err = got_ref_open(&ref, repo, remote_refname, 1);
2206 if (err->code != GOT_ERR_NOT_REF)
2208 err = create_ref(remote_refname, id, verbosity, repo);
2210 err = update_ref(ref, id, 0, verbosity, repo);
2211 unlock_err = got_ref_unlock(ref);
2212 if (unlock_err && err == NULL)
2217 free(remote_refname);
2221 static const struct got_error *
2222 delete_ref(struct got_repository *repo, struct got_reference *ref)
2224 const struct got_error *err = NULL;
2225 struct got_object_id *id = NULL;
2226 char *id_str = NULL;
2229 if (got_ref_is_symbolic(ref)) {
2230 target = got_ref_get_symref_target(ref);
2232 err = got_ref_resolve(&id, repo, ref);
2235 err = got_object_id_str(&id_str, id);
2241 err = got_ref_delete(ref, repo);
2245 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2252 static const struct got_error *
2253 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2255 const struct got_error *err = NULL;
2256 struct got_reflist_head refs;
2257 struct got_reflist_entry *re;
2262 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2263 err = got_error_from_errno("asprintf");
2266 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2270 TAILQ_FOREACH(re, &refs, entry)
2271 delete_ref(repo, re->ref);
2273 got_ref_list_free(&refs);
2277 static const struct got_error *
2278 cmd_fetch(int argc, char *argv[])
2280 const struct got_error *error = NULL, *unlock_err;
2281 char *cwd = NULL, *repo_path = NULL;
2282 const char *remote_name;
2283 char *proto = NULL, *host = NULL, *port = NULL;
2284 char *repo_name = NULL, *server_path = NULL;
2285 const struct got_remote_repo *remotes, *remote = NULL;
2287 char *id_str = NULL;
2288 struct got_repository *repo = NULL;
2289 struct got_worktree *worktree = NULL;
2290 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2291 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2292 struct got_pathlist_entry *pe;
2293 struct got_object_id *pack_hash = NULL;
2294 int i, ch, fetchfd = -1, fetchstatus;
2295 pid_t fetchpid = -1;
2296 struct got_fetch_progress_arg fpa;
2297 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2298 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2299 int *pack_fds = NULL;
2302 TAILQ_INIT(&symrefs);
2303 TAILQ_INIT(&wanted_branches);
2304 TAILQ_INIT(&wanted_refs);
2306 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2309 fetch_all_branches = 1;
2312 error = got_pathlist_append(&wanted_branches,
2327 error = got_pathlist_append(&wanted_refs,
2333 repo_path = realpath(optarg, NULL);
2334 if (repo_path == NULL)
2335 return got_error_from_errno2("realpath",
2337 got_path_strip_trailing_slashes(repo_path);
2345 else if (verbosity < 3)
2359 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2360 option_conflict('a', 'b');
2361 if (list_refs_only) {
2362 if (!TAILQ_EMPTY(&wanted_branches))
2363 option_conflict('l', 'b');
2364 if (fetch_all_branches)
2365 option_conflict('l', 'a');
2367 option_conflict('l', 'd');
2369 option_conflict('l', 'X');
2371 if (delete_remote) {
2372 if (fetch_all_branches)
2373 option_conflict('X', 'a');
2374 if (!TAILQ_EMPTY(&wanted_branches))
2375 option_conflict('X', 'b');
2377 option_conflict('X', 'd');
2379 option_conflict('X', 't');
2380 if (!TAILQ_EMPTY(&wanted_refs))
2381 option_conflict('X', 'R');
2386 errx(1, "-X option requires a remote name");
2387 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2388 } else if (argc == 1)
2389 remote_name = argv[0];
2393 cwd = getcwd(NULL, 0);
2395 error = got_error_from_errno("getcwd");
2399 error = got_repo_pack_fds_open(&pack_fds);
2403 if (repo_path == NULL) {
2404 error = got_worktree_open(&worktree, cwd);
2405 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2411 strdup(got_worktree_get_repo_path(worktree));
2412 if (repo_path == NULL)
2413 error = got_error_from_errno("strdup");
2417 repo_path = strdup(cwd);
2418 if (repo_path == NULL) {
2419 error = got_error_from_errno("strdup");
2425 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2429 if (delete_remote) {
2430 error = delete_refs_for_remote(repo, remote_name);
2431 goto done; /* nothing else to do */
2435 worktree_conf = got_worktree_get_gotconfig(worktree);
2436 if (worktree_conf) {
2437 got_gotconfig_get_remotes(&nremotes, &remotes,
2439 for (i = 0; i < nremotes; i++) {
2440 if (strcmp(remotes[i].name, remote_name) == 0) {
2441 remote = &remotes[i];
2447 if (remote == NULL) {
2448 repo_conf = got_repo_get_gotconfig(repo);
2450 got_gotconfig_get_remotes(&nremotes, &remotes,
2452 for (i = 0; i < nremotes; i++) {
2453 if (strcmp(remotes[i].name, remote_name) == 0) {
2454 remote = &remotes[i];
2460 if (remote == NULL) {
2461 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2462 for (i = 0; i < nremotes; i++) {
2463 if (strcmp(remotes[i].name, remote_name) == 0) {
2464 remote = &remotes[i];
2469 if (remote == NULL) {
2470 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2474 if (TAILQ_EMPTY(&wanted_branches)) {
2475 if (!fetch_all_branches)
2476 fetch_all_branches = remote->fetch_all_branches;
2477 for (i = 0; i < remote->nfetch_branches; i++) {
2478 error = got_pathlist_append(&wanted_branches,
2479 remote->fetch_branches[i], NULL);
2484 if (TAILQ_EMPTY(&wanted_refs)) {
2485 for (i = 0; i < remote->nfetch_refs; i++) {
2486 error = got_pathlist_append(&wanted_refs,
2487 remote->fetch_refs[i], NULL);
2493 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2494 &repo_name, remote->fetch_url);
2498 if (strcmp(proto, "git") == 0) {
2500 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2501 "sendfd dns inet unveil", NULL) == -1)
2504 } else if (strcmp(proto, "git+ssh") == 0 ||
2505 strcmp(proto, "ssh") == 0) {
2507 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2508 "sendfd unveil", NULL) == -1)
2511 } else if (strcmp(proto, "http") == 0 ||
2512 strcmp(proto, "git+http") == 0) {
2513 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2516 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2520 error = got_dial_apply_unveil(proto);
2524 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2528 if (verbosity >= 0) {
2529 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2530 remote->name, proto, host,
2531 port ? ":" : "", port ? port : "",
2532 *server_path == '/' ? "" : "/", server_path);
2535 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2536 server_path, verbosity);
2540 fpa.last_scaled_size[0] = '\0';
2541 fpa.last_p_indexed = -1;
2542 fpa.last_p_resolved = -1;
2543 fpa.verbosity = verbosity;
2545 fpa.create_configs = 0;
2546 fpa.configs_created = 0;
2547 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2548 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2549 remote->mirror_references, fetch_all_branches, &wanted_branches,
2550 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2551 fetch_progress, &fpa);
2555 if (list_refs_only) {
2556 error = list_remote_refs(&symrefs, &refs);
2560 if (pack_hash == NULL) {
2562 printf("Already up-to-date\n");
2563 } else if (verbosity >= 0) {
2564 error = got_object_id_str(&id_str, pack_hash);
2567 printf("\nFetched %s.pack\n", id_str);
2572 /* Update references provided with the pack file. */
2573 TAILQ_FOREACH(pe, &refs, entry) {
2574 const char *refname = pe->path;
2575 struct got_object_id *id = pe->data;
2576 struct got_reference *ref;
2577 char *remote_refname;
2579 if (is_wanted_ref(&wanted_refs, refname) &&
2580 !remote->mirror_references) {
2581 error = update_wanted_ref(refname, id,
2582 remote->name, verbosity, repo);
2588 if (remote->mirror_references ||
2589 strncmp("refs/tags/", refname, 10) == 0) {
2590 error = got_ref_open(&ref, repo, refname, 1);
2592 if (error->code != GOT_ERR_NOT_REF)
2594 error = create_ref(refname, id, verbosity,
2599 error = update_ref(ref, id, replace_tags,
2601 unlock_err = got_ref_unlock(ref);
2602 if (unlock_err && error == NULL)
2608 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2609 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2610 remote_name, refname + 11) == -1) {
2611 error = got_error_from_errno("asprintf");
2615 error = got_ref_open(&ref, repo, remote_refname, 1);
2617 if (error->code != GOT_ERR_NOT_REF)
2619 error = create_ref(remote_refname, id,
2624 error = update_ref(ref, id, replace_tags,
2626 unlock_err = got_ref_unlock(ref);
2627 if (unlock_err && error == NULL)
2634 /* Also create a local branch if none exists yet. */
2635 error = got_ref_open(&ref, repo, refname, 1);
2637 if (error->code != GOT_ERR_NOT_REF)
2639 error = create_ref(refname, id, verbosity,
2644 unlock_err = got_ref_unlock(ref);
2645 if (unlock_err && error == NULL)
2652 error = delete_missing_refs(&refs, &symrefs, remote,
2658 if (!remote->mirror_references) {
2659 /* Update remote HEAD reference if the server provided one. */
2660 TAILQ_FOREACH(pe, &symrefs, entry) {
2661 struct got_reference *target_ref;
2662 const char *refname = pe->path;
2663 const char *target = pe->data;
2664 char *remote_refname = NULL, *remote_target = NULL;
2666 if (strcmp(refname, GOT_REF_HEAD) != 0)
2669 if (strncmp("refs/heads/", target, 11) != 0)
2672 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2673 remote->name, refname) == -1) {
2674 error = got_error_from_errno("asprintf");
2677 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2678 remote->name, target + 11) == -1) {
2679 error = got_error_from_errno("asprintf");
2680 free(remote_refname);
2684 error = got_ref_open(&target_ref, repo, remote_target,
2687 free(remote_refname);
2688 free(remote_target);
2689 if (error->code == GOT_ERR_NOT_REF) {
2695 error = update_symref(remote_refname, target_ref,
2697 free(remote_refname);
2698 free(remote_target);
2699 got_ref_close(target_ref);
2706 if (kill(fetchpid, SIGTERM) == -1)
2707 error = got_error_from_errno("kill");
2708 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2709 error = got_error_from_errno("waitpid");
2711 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2712 error = got_error_from_errno("close");
2714 const struct got_error *close_err = got_repo_close(repo);
2719 got_worktree_close(worktree);
2721 const struct got_error *pack_err =
2722 got_repo_pack_fds_close(pack_fds);
2726 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2727 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2728 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2729 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2744 usage_checkout(void)
2746 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2747 "[-p path-prefix] repository-path [work-tree-path]\n",
2753 show_worktree_base_ref_warning(void)
2755 fprintf(stderr, "%s: warning: could not create a reference "
2756 "to the work tree's base commit; the commit could be "
2757 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2758 "repository writable and running 'got update' will prevent this\n",
2762 struct got_checkout_progress_arg {
2763 const char *worktree_path;
2764 int had_base_commit_ref_error;
2768 static const struct got_error *
2769 checkout_progress(void *arg, unsigned char status, const char *path)
2771 struct got_checkout_progress_arg *a = arg;
2773 /* Base commit bump happens silently. */
2774 if (status == GOT_STATUS_BUMP_BASE)
2777 if (status == GOT_STATUS_BASE_REF_ERR) {
2778 a->had_base_commit_ref_error = 1;
2782 while (path[0] == '/')
2785 if (a->verbosity >= 0)
2786 printf("%c %s/%s\n", status, a->worktree_path, path);
2791 static const struct got_error *
2792 check_cancelled(void *arg)
2794 if (sigint_received || sigpipe_received)
2795 return got_error(GOT_ERR_CANCELLED);
2799 static const struct got_error *
2800 check_linear_ancestry(struct got_object_id *commit_id,
2801 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2802 struct got_repository *repo)
2804 const struct got_error *err = NULL;
2805 struct got_object_id *yca_id;
2807 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2808 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2813 return got_error(GOT_ERR_ANCESTRY);
2816 * Require a straight line of history between the target commit
2817 * and the work tree's base commit.
2819 * Non-linear situations such as this require a rebase:
2821 * (commit) D F (base_commit)
2829 * 'got update' only handles linear cases:
2830 * Update forwards in time: A (base/yca) - B - C - D (commit)
2831 * Update backwards in time: D (base) - C - B - A (commit/yca)
2833 if (allow_forwards_in_time_only) {
2834 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2835 return got_error(GOT_ERR_ANCESTRY);
2836 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2837 got_object_id_cmp(base_commit_id, yca_id) != 0)
2838 return got_error(GOT_ERR_ANCESTRY);
2844 static const struct got_error *
2845 check_same_branch(struct got_object_id *commit_id,
2846 struct got_reference *head_ref, struct got_object_id *yca_id,
2847 struct got_repository *repo)
2849 const struct got_error *err = NULL;
2850 struct got_commit_graph *graph = NULL;
2851 struct got_object_id *head_commit_id = NULL;
2852 int is_same_branch = 0;
2854 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2858 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2862 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2867 err = got_commit_graph_open(&graph, "/", 1);
2871 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2872 check_cancelled, NULL);
2877 struct got_object_id id;
2879 err = got_commit_graph_iter_next(&id, graph, repo,
2880 check_cancelled, NULL);
2882 if (err->code == GOT_ERR_ITER_COMPLETED)
2887 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2889 if (got_object_id_cmp(&id, commit_id) == 0) {
2896 got_commit_graph_close(graph);
2897 free(head_commit_id);
2898 if (!err && !is_same_branch)
2899 err = got_error(GOT_ERR_ANCESTRY);
2903 static const struct got_error *
2904 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2906 static char msg[512];
2907 const char *branch_name;
2909 if (got_ref_is_symbolic(ref))
2910 branch_name = got_ref_get_symref_target(ref);
2912 branch_name = got_ref_get_name(ref);
2914 if (strncmp("refs/heads/", branch_name, 11) == 0)
2917 snprintf(msg, sizeof(msg),
2918 "target commit is not contained in branch '%s'; "
2919 "the branch to use must be specified with -b; "
2920 "if necessary a new branch can be created for "
2921 "this commit with 'got branch -c %s BRANCH_NAME'",
2922 branch_name, commit_id_str);
2924 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2927 static const struct got_error *
2928 cmd_checkout(int argc, char *argv[])
2930 const struct got_error *error = NULL;
2931 struct got_repository *repo = NULL;
2932 struct got_reference *head_ref = NULL, *ref = NULL;
2933 struct got_worktree *worktree = NULL;
2934 char *repo_path = NULL;
2935 char *worktree_path = NULL;
2936 const char *path_prefix = "";
2937 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2938 char *commit_id_str = NULL;
2939 struct got_object_id *commit_id = NULL;
2941 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2942 struct got_pathlist_head paths;
2943 struct got_checkout_progress_arg cpa;
2944 int *pack_fds = NULL;
2948 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2951 branch_name = optarg;
2954 commit_id_str = strdup(optarg);
2955 if (commit_id_str == NULL)
2956 return got_error_from_errno("strdup");
2962 path_prefix = optarg;
2977 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2978 "unveil", NULL) == -1)
2982 char *base, *dotgit;
2984 repo_path = realpath(argv[0], NULL);
2985 if (repo_path == NULL)
2986 return got_error_from_errno2("realpath", argv[0]);
2987 cwd = getcwd(NULL, 0);
2989 error = got_error_from_errno("getcwd");
2996 error = got_path_basename(&base, path);
2999 dotgit = strstr(base, ".git");
3002 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3003 error = got_error_from_errno("asprintf");
3008 } else if (argc == 2) {
3009 repo_path = realpath(argv[0], NULL);
3010 if (repo_path == NULL) {
3011 error = got_error_from_errno2("realpath", argv[0]);
3014 worktree_path = realpath(argv[1], NULL);
3015 if (worktree_path == NULL) {
3016 if (errno != ENOENT) {
3017 error = got_error_from_errno2("realpath",
3021 worktree_path = strdup(argv[1]);
3022 if (worktree_path == NULL) {
3023 error = got_error_from_errno("strdup");
3030 got_path_strip_trailing_slashes(repo_path);
3031 got_path_strip_trailing_slashes(worktree_path);
3033 error = got_repo_pack_fds_open(&pack_fds);
3037 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3041 /* Pre-create work tree path for unveil(2) */
3042 error = got_path_mkdir(worktree_path);
3044 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3045 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3047 if (!allow_nonempty &&
3048 !got_path_dir_is_empty(worktree_path)) {
3049 error = got_error_path(worktree_path,
3050 GOT_ERR_DIR_NOT_EMPTY);
3055 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3059 error = got_ref_open(&head_ref, repo, branch_name, 0);
3063 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3064 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3067 error = got_worktree_open(&worktree, worktree_path);
3071 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3075 if (!same_path_prefix) {
3076 error = got_error(GOT_ERR_PATH_PREFIX);
3080 if (commit_id_str) {
3081 struct got_reflist_head refs;
3083 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3087 error = got_repo_match_object_id(&commit_id, NULL,
3088 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3089 got_ref_list_free(&refs);
3092 error = check_linear_ancestry(commit_id,
3093 got_worktree_get_base_commit_id(worktree), 0, repo);
3094 if (error != NULL) {
3095 if (error->code == GOT_ERR_ANCESTRY) {
3096 error = checkout_ancestry_error(
3097 head_ref, commit_id_str);
3101 error = check_same_branch(commit_id, head_ref, NULL, repo);
3103 if (error->code == GOT_ERR_ANCESTRY) {
3104 error = checkout_ancestry_error(
3105 head_ref, commit_id_str);
3109 error = got_worktree_set_base_commit_id(worktree, repo,
3113 /* Expand potentially abbreviated commit ID string. */
3114 free(commit_id_str);
3115 error = got_object_id_str(&commit_id_str, commit_id);
3119 commit_id = got_object_id_dup(
3120 got_worktree_get_base_commit_id(worktree));
3121 if (commit_id == NULL) {
3122 error = got_error_from_errno("got_object_id_dup");
3125 error = got_object_id_str(&commit_id_str, commit_id);
3130 error = got_pathlist_append(&paths, "", NULL);
3133 cpa.worktree_path = worktree_path;
3134 cpa.had_base_commit_ref_error = 0;
3135 cpa.verbosity = verbosity;
3136 error = got_worktree_checkout_files(worktree, &paths, repo,
3137 checkout_progress, &cpa, check_cancelled, NULL);
3141 if (got_ref_is_symbolic(head_ref)) {
3142 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3145 refname = got_ref_get_name(ref);
3147 refname = got_ref_get_name(head_ref);
3148 printf("Checked out %s: %s\n", refname, commit_id_str);
3149 printf("Now shut up and hack\n");
3150 if (cpa.had_base_commit_ref_error)
3151 show_worktree_base_ref_warning();
3154 const struct got_error *pack_err =
3155 got_repo_pack_fds_close(pack_fds);
3160 got_ref_close(head_ref);
3163 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3164 free(commit_id_str);
3167 free(worktree_path);
3172 struct got_update_progress_arg {
3184 print_update_progress_stats(struct got_update_progress_arg *upa)
3186 if (!upa->did_something)
3189 if (upa->conflicts > 0)
3190 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3191 if (upa->obstructed > 0)
3192 printf("File paths obstructed by a non-regular file: %d\n",
3194 if (upa->not_updated > 0)
3195 printf("Files not updated because of existing merge "
3196 "conflicts: %d\n", upa->not_updated);
3200 * The meaning of some status codes differs between merge-style operations and
3201 * update operations. For example, the ! status code means "file was missing"
3202 * if changes were merged into the work tree, and "missing file was restored"
3203 * if the work tree was updated. This function should be used by any operation
3204 * which merges changes into the work tree without updating the work tree.
3207 print_merge_progress_stats(struct got_update_progress_arg *upa)
3209 if (!upa->did_something)
3212 if (upa->conflicts > 0)
3213 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3214 if (upa->obstructed > 0)
3215 printf("File paths obstructed by a non-regular file: %d\n",
3217 if (upa->missing > 0)
3218 printf("Files which had incoming changes but could not be "
3219 "found in the work tree: %d\n", upa->missing);
3220 if (upa->not_deleted > 0)
3221 printf("Files not deleted due to differences in deleted "
3222 "content: %d\n", upa->not_deleted);
3223 if (upa->unversioned > 0)
3224 printf("Files not merged because an unversioned file was "
3225 "found in the work tree: %d\n", upa->unversioned);
3231 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3232 "[path ...]\n", getprogname());
3236 static const struct got_error *
3237 update_progress(void *arg, unsigned char status, const char *path)
3239 struct got_update_progress_arg *upa = arg;
3241 if (status == GOT_STATUS_EXISTS ||
3242 status == GOT_STATUS_BASE_REF_ERR)
3245 upa->did_something = 1;
3247 /* Base commit bump happens silently. */
3248 if (status == GOT_STATUS_BUMP_BASE)
3251 if (status == GOT_STATUS_CONFLICT)
3253 if (status == GOT_STATUS_OBSTRUCTED)
3255 if (status == GOT_STATUS_CANNOT_UPDATE)
3257 if (status == GOT_STATUS_MISSING)
3259 if (status == GOT_STATUS_CANNOT_DELETE)
3261 if (status == GOT_STATUS_UNVERSIONED)
3264 while (path[0] == '/')
3266 if (upa->verbosity >= 0)
3267 printf("%c %s\n", status, path);
3272 static const struct got_error *
3273 switch_head_ref(struct got_reference *head_ref,
3274 struct got_object_id *commit_id, struct got_worktree *worktree,
3275 struct got_repository *repo)
3277 const struct got_error *err = NULL;
3279 int ref_has_moved = 0;
3281 /* Trivial case: switching between two different references. */
3282 if (strcmp(got_ref_get_name(head_ref),
3283 got_worktree_get_head_ref_name(worktree)) != 0) {
3284 printf("Switching work tree from %s to %s\n",
3285 got_worktree_get_head_ref_name(worktree),
3286 got_ref_get_name(head_ref));
3287 return got_worktree_set_head_ref(worktree, head_ref);
3290 err = check_linear_ancestry(commit_id,
3291 got_worktree_get_base_commit_id(worktree), 0, repo);
3293 if (err->code != GOT_ERR_ANCESTRY)
3300 /* Switching to a rebased branch with the same reference name. */
3301 err = got_object_id_str(&base_id_str,
3302 got_worktree_get_base_commit_id(worktree));
3305 printf("Reference %s now points at a different branch\n",
3306 got_worktree_get_head_ref_name(worktree));
3307 printf("Switching work tree from %s to %s\n", base_id_str,
3308 got_worktree_get_head_ref_name(worktree));
3312 static const struct got_error *
3313 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3315 const struct got_error *err;
3318 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3322 return got_error(GOT_ERR_REBASING);
3324 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3328 return got_error(GOT_ERR_HISTEDIT_BUSY);
3333 static const struct got_error *
3334 check_merge_in_progress(struct got_worktree *worktree,
3335 struct got_repository *repo)
3337 const struct got_error *err;
3340 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3344 return got_error(GOT_ERR_MERGE_BUSY);
3349 static const struct got_error *
3350 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3351 char *argv[], struct got_worktree *worktree)
3353 const struct got_error *err = NULL;
3355 struct got_pathlist_entry *new;
3361 return got_error_from_errno("strdup");
3362 return got_pathlist_append(paths, path, NULL);
3365 for (i = 0; i < argc; i++) {
3366 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3369 err = got_pathlist_insert(&new, paths, path, NULL);
3370 if (err || new == NULL /* duplicate */) {
3380 static const struct got_error *
3381 wrap_not_worktree_error(const struct got_error *orig_err,
3382 const char *cmdname, const char *path)
3384 const struct got_error *err;
3385 struct got_repository *repo;
3386 static char msg[512];
3387 int *pack_fds = NULL;
3389 err = got_repo_pack_fds_open(&pack_fds);
3393 err = got_repo_open(&repo, path, NULL, pack_fds);
3397 snprintf(msg, sizeof(msg),
3398 "'got %s' needs a work tree in addition to a git repository\n"
3399 "Work trees can be checked out from this Git repository with "
3401 "The got(1) manual page contains more information.", cmdname);
3402 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3403 got_repo_close(repo);
3405 const struct got_error *pack_err =
3406 got_repo_pack_fds_close(pack_fds);
3413 static const struct got_error *
3414 cmd_update(int argc, char *argv[])
3416 const struct got_error *error = NULL;
3417 struct got_repository *repo = NULL;
3418 struct got_worktree *worktree = NULL;
3419 char *worktree_path = NULL;
3420 struct got_object_id *commit_id = NULL;
3421 char *commit_id_str = NULL;
3422 const char *branch_name = NULL;
3423 struct got_reference *head_ref = NULL;
3424 struct got_pathlist_head paths;
3425 struct got_pathlist_entry *pe;
3426 int ch, verbosity = 0;
3427 struct got_update_progress_arg upa;
3428 int *pack_fds = NULL;
3432 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3435 branch_name = optarg;
3438 commit_id_str = strdup(optarg);
3439 if (commit_id_str == NULL)
3440 return got_error_from_errno("strdup");
3455 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3456 "unveil", NULL) == -1)
3459 worktree_path = getcwd(NULL, 0);
3460 if (worktree_path == NULL) {
3461 error = got_error_from_errno("getcwd");
3465 error = got_repo_pack_fds_open(&pack_fds);
3469 error = got_worktree_open(&worktree, worktree_path);
3471 if (error->code == GOT_ERR_NOT_WORKTREE)
3472 error = wrap_not_worktree_error(error, "update",
3477 error = check_rebase_or_histedit_in_progress(worktree);
3481 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3486 error = apply_unveil(got_repo_get_path(repo), 0,
3487 got_worktree_get_root_path(worktree));
3491 error = check_merge_in_progress(worktree, repo);
3495 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3499 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3500 got_worktree_get_head_ref_name(worktree), 0);
3503 if (commit_id_str == NULL) {
3504 error = got_ref_resolve(&commit_id, repo, head_ref);
3507 error = got_object_id_str(&commit_id_str, commit_id);
3511 struct got_reflist_head refs;
3513 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3517 error = got_repo_match_object_id(&commit_id, NULL,
3518 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3519 got_ref_list_free(&refs);
3520 free(commit_id_str);
3521 commit_id_str = NULL;
3524 error = got_object_id_str(&commit_id_str, commit_id);
3530 struct got_object_id *head_commit_id;
3531 TAILQ_FOREACH(pe, &paths, entry) {
3532 if (pe->path_len == 0)
3534 error = got_error_msg(GOT_ERR_BAD_PATH,
3535 "switching between branches requires that "
3536 "the entire work tree gets updated");
3539 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3542 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3544 free(head_commit_id);
3547 error = check_same_branch(commit_id, head_ref, NULL, repo);
3550 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3554 error = check_linear_ancestry(commit_id,
3555 got_worktree_get_base_commit_id(worktree), 0, repo);
3556 if (error != NULL) {
3557 if (error->code == GOT_ERR_ANCESTRY)
3558 error = got_error(GOT_ERR_BRANCH_MOVED);
3561 error = check_same_branch(commit_id, head_ref, NULL, repo);
3566 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3568 error = got_worktree_set_base_commit_id(worktree, repo,
3574 memset(&upa, 0, sizeof(upa));
3575 upa.verbosity = verbosity;
3576 error = got_worktree_checkout_files(worktree, &paths, repo,
3577 update_progress, &upa, check_cancelled, NULL);
3581 if (upa.did_something) {
3582 printf("Updated to %s: %s\n",
3583 got_worktree_get_head_ref_name(worktree), commit_id_str);
3585 printf("Already up-to-date\n");
3587 print_update_progress_stats(&upa);
3590 const struct got_error *pack_err =
3591 got_repo_pack_fds_close(pack_fds);
3595 free(worktree_path);
3596 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3598 free(commit_id_str);
3602 static const struct got_error *
3603 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3604 const char *path, int diff_context, int ignore_whitespace,
3605 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3606 struct got_repository *repo, FILE *outfile)
3608 const struct got_error *err = NULL;
3609 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3610 FILE *f1 = NULL, *f2 = NULL;
3611 int fd1 = -1, fd2 = -1;
3613 fd1 = got_opentempfd();
3615 return got_error_from_errno("got_opentempfd");
3616 fd2 = got_opentempfd();
3618 err = got_error_from_errno("got_opentempfd");
3623 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3629 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3633 f1 = got_opentemp();
3635 err = got_error_from_errno("got_opentemp");
3638 f2 = got_opentemp();
3640 err = got_error_from_errno("got_opentemp");
3644 while (path[0] == '/')
3646 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3647 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3648 force_text_diff, dsa, outfile);
3650 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3651 err = got_error_from_errno("close");
3653 got_object_blob_close(blob1);
3654 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3655 err = got_error_from_errno("close");
3656 got_object_blob_close(blob2);
3657 if (f1 && fclose(f1) == EOF && err == NULL)
3658 err = got_error_from_errno("fclose");
3659 if (f2 && fclose(f2) == EOF && err == NULL)
3660 err = got_error_from_errno("fclose");
3664 static const struct got_error *
3665 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3666 const char *path, int diff_context, int ignore_whitespace,
3667 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3668 struct got_repository *repo, FILE *outfile)
3670 const struct got_error *err = NULL;
3671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3672 struct got_diff_blob_output_unidiff_arg arg;
3673 FILE *f1 = NULL, *f2 = NULL;
3674 int fd1 = -1, fd2 = -1;
3677 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3680 fd1 = got_opentempfd();
3682 err = got_error_from_errno("got_opentempfd");
3687 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3691 f1 = got_opentemp();
3693 err = got_error_from_errno("got_opentemp");
3697 f2 = got_opentemp();
3699 err = got_error_from_errno("got_opentemp");
3702 fd2 = got_opentempfd();
3704 err = got_error_from_errno("got_opentempfd");
3707 arg.diff_context = diff_context;
3708 arg.ignore_whitespace = ignore_whitespace;
3709 arg.force_text_diff = force_text_diff;
3711 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3712 arg.outfile = outfile;
3715 while (path[0] == '/')
3717 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3718 got_diff_blob_output_unidiff, &arg, 1);
3721 got_object_tree_close(tree1);
3723 got_object_tree_close(tree2);
3724 if (f1 && fclose(f1) == EOF && err == NULL)
3725 err = got_error_from_errno("fclose");
3726 if (f2 && fclose(f2) == EOF && err == NULL)
3727 err = got_error_from_errno("fclose");
3728 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3729 err = got_error_from_errno("close");
3730 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3731 err = got_error_from_errno("close");
3735 static const struct got_error *
3736 get_changed_paths(struct got_pathlist_head *paths,
3737 struct got_commit_object *commit, struct got_repository *repo,
3738 struct got_diffstat_cb_arg *dsa)
3740 const struct got_error *err = NULL;
3741 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3742 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3743 struct got_object_qid *qid;
3744 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3745 FILE *f1 = NULL, *f2 = NULL;
3746 int fd1 = -1, fd2 = -1;
3749 cb = got_diff_tree_compute_diffstat;
3751 f1 = got_opentemp();
3753 err = got_error_from_errno("got_opentemp");
3756 f2 = got_opentemp();
3758 err = got_error_from_errno("got_opentemp");
3761 fd1 = got_opentempfd();
3763 err = got_error_from_errno("got_opentempfd");
3766 fd2 = got_opentempfd();
3768 err = got_error_from_errno("got_opentempfd");
3773 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3775 struct got_commit_object *pcommit;
3776 err = got_object_open_as_commit(&pcommit, repo,
3781 tree_id1 = got_object_id_dup(
3782 got_object_commit_get_tree_id(pcommit));
3783 if (tree_id1 == NULL) {
3784 got_object_commit_close(pcommit);
3785 return got_error_from_errno("got_object_id_dup");
3787 got_object_commit_close(pcommit);
3792 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3797 tree_id2 = got_object_commit_get_tree_id(commit);
3798 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3802 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3803 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3806 got_object_tree_close(tree1);
3808 got_object_tree_close(tree2);
3809 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3810 err = got_error_from_errno("close");
3811 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3812 err = got_error_from_errno("close");
3813 if (f1 && fclose(f1) == EOF && err == NULL)
3814 err = got_error_from_errno("fclose");
3815 if (f2 && fclose(f2) == EOF && err == NULL)
3816 err = got_error_from_errno("fclose");
3821 static const struct got_error *
3822 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3823 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3824 struct got_repository *repo, FILE *outfile)
3826 const struct got_error *err = NULL;
3827 struct got_commit_object *pcommit = NULL;
3828 char *id_str1 = NULL, *id_str2 = NULL;
3829 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3830 struct got_object_qid *qid;
3832 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3834 err = got_object_open_as_commit(&pcommit, repo,
3838 err = got_object_id_str(&id_str1, &qid->id);
3843 err = got_object_id_str(&id_str2, id);
3847 if (path && path[0] != '\0') {
3849 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3853 err = got_object_id_by_path(&obj_id1, repo,
3856 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3862 err = got_object_get_type(&obj_type, repo, obj_id2);
3868 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3869 fprintf(outfile, "commit - %s\n",
3870 id_str1 ? id_str1 : "/dev/null");
3871 fprintf(outfile, "commit + %s\n", id_str2);
3873 case GOT_OBJ_TYPE_BLOB:
3874 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3875 0, 0, dsa, repo, outfile);
3877 case GOT_OBJ_TYPE_TREE:
3878 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3879 0, 0, dsa, repo, outfile);
3882 err = got_error(GOT_ERR_OBJ_TYPE);
3888 obj_id2 = got_object_commit_get_tree_id(commit);
3890 obj_id1 = got_object_commit_get_tree_id(pcommit);
3892 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3893 fprintf(outfile, "commit - %s\n",
3894 id_str1 ? id_str1 : "/dev/null");
3895 fprintf(outfile, "commit + %s\n", id_str2);
3896 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3897 dsa, repo, outfile);
3903 got_object_commit_close(pcommit);
3908 get_datestr(time_t *time, char *datebuf)
3910 struct tm mytm, *tm;
3913 tm = gmtime_r(time, &mytm);
3916 s = asctime_r(tm, datebuf);
3919 p = strchr(s, '\n');
3925 static const struct got_error *
3926 match_commit(int *have_match, struct got_object_id *id,
3927 struct got_commit_object *commit, regex_t *regex)
3929 const struct got_error *err = NULL;
3930 regmatch_t regmatch;
3931 char *id_str = NULL, *logmsg = NULL;
3935 err = got_object_id_str(&id_str, id);
3939 err = got_object_commit_get_logmsg(&logmsg, commit);
3943 if (regexec(regex, got_object_commit_get_author(commit), 1,
3944 ®match, 0) == 0 ||
3945 regexec(regex, got_object_commit_get_committer(commit), 1,
3946 ®match, 0) == 0 ||
3947 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3948 regexec(regex, logmsg, 1, ®match, 0) == 0)
3957 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3960 regmatch_t regmatch;
3961 struct got_pathlist_entry *pe;
3965 TAILQ_FOREACH(pe, changed_paths, entry) {
3966 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3973 static const struct got_error *
3974 match_patch(int *have_match, struct got_commit_object *commit,
3975 struct got_object_id *id, const char *path, int diff_context,
3976 struct got_repository *repo, regex_t *regex, FILE *f)
3978 const struct got_error *err = NULL;
3980 size_t linesize = 0;
3981 regmatch_t regmatch;
3985 err = got_opentemp_truncate(f);
3989 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3993 if (fseeko(f, 0L, SEEK_SET) == -1) {
3994 err = got_error_from_errno("fseeko");
3998 while (getline(&line, &linesize, f) != -1) {
3999 if (regexec(regex, line, 1, ®match, 0) == 0) {
4009 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4011 static const struct got_error*
4012 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4013 struct got_object_id *id, struct got_repository *repo,
4016 static const struct got_error *err = NULL;
4017 struct got_reflist_entry *re;
4023 TAILQ_FOREACH(re, refs, entry) {
4024 struct got_tag_object *tag = NULL;
4025 struct got_object_id *ref_id;
4028 name = got_ref_get_name(re->ref);
4029 if (strcmp(name, GOT_REF_HEAD) == 0)
4031 if (strncmp(name, "refs/", 5) == 0)
4033 if (strncmp(name, "got/", 4) == 0)
4035 if (strncmp(name, "heads/", 6) == 0)
4037 if (strncmp(name, "remotes/", 8) == 0) {
4041 s = strstr(name, "/" GOT_REF_HEAD);
4042 if (s != NULL && s[strlen(s)] == '\0')
4045 err = got_ref_resolve(&ref_id, repo, re->ref);
4048 if (strncmp(name, "tags/", 5) == 0) {
4049 err = got_object_open_as_tag(&tag, repo, ref_id);
4051 if (err->code != GOT_ERR_OBJ_TYPE) {
4055 /* Ref points at something other than a tag. */
4060 cmp = got_object_id_cmp(tag ?
4061 got_object_tag_get_object_id(tag) : ref_id, id);
4064 got_object_tag_close(tag);
4068 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4069 s ? ", " : "", name) == -1) {
4070 err = got_error_from_errno("asprintf");
4081 static const struct got_error *
4082 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4083 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4085 const struct got_error *err = NULL;
4086 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4087 char *comma, *s, *nl;
4088 struct got_reflist_head *refs;
4089 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4091 time_t committer_time;
4093 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4095 err = build_refs_str(&ref_str, refs, id, repo, 1);
4099 /* Display the first matching ref only. */
4100 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4104 if (ref_str == NULL) {
4105 err = got_object_id_str(&id_str, id);
4110 committer_time = got_object_commit_get_committer_time(commit);
4111 if (gmtime_r(&committer_time, &tm) == NULL) {
4112 err = got_error_from_errno("gmtime_r");
4115 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4116 err = got_error(GOT_ERR_NO_SPACE);
4120 err = got_object_commit_get_logmsg(&logmsg0, commit);
4125 while (isspace((unsigned char)s[0]))
4128 nl = strchr(s, '\n');
4134 printf("%s%-7s %s\n", datebuf, ref_str, s);
4136 printf("%s%.7s %s\n", datebuf, id_str, s);
4138 if (fflush(stdout) != 0 && err == NULL)
4139 err = got_error_from_errno("fflush");
4147 static const struct got_error *
4148 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4150 struct got_pathlist_entry *pe;
4153 printf("%s\n", header);
4155 TAILQ_FOREACH(pe, dsa->paths, entry) {
4156 struct got_diff_changed_path *cp = pe->data;
4157 int pad = dsa->max_path_len - pe->path_len + 1;
4159 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4160 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4162 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4163 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4164 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4166 if (fflush(stdout) != 0)
4167 return got_error_from_errno("fflush");
4172 static const struct got_error *
4178 if (fseeko(f, 0L, SEEK_SET) == -1)
4179 return got_error_from_errno("fseek");
4182 r = fread(buf, 1, sizeof(buf), f);
4185 return got_error_from_errno("fread");
4189 if (fwrite(buf, 1, r, stdout) != r)
4190 return got_ferror(stdout, GOT_ERR_IO);
4196 static const struct got_error *
4197 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4198 struct got_repository *repo, const char *path,
4199 struct got_pathlist_head *changed_paths,
4200 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4201 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4204 const struct got_error *err = NULL;
4206 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4208 time_t committer_time;
4209 const char *author, *committer;
4210 char *refs_str = NULL;
4212 err = got_object_id_str(&id_str, id);
4216 if (custom_refs_str == NULL) {
4217 struct got_reflist_head *refs;
4218 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4220 err = build_refs_str(&refs_str, refs, id, repo, 0);
4226 printf(GOT_COMMIT_SEP_STR);
4227 if (custom_refs_str)
4228 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4231 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4232 refs_str ? " (" : "", refs_str ? refs_str : "",
4233 refs_str ? ")" : "");
4238 printf("from: %s\n", got_object_commit_get_author(commit));
4239 author = got_object_commit_get_author(commit);
4240 committer = got_object_commit_get_committer(commit);
4241 if (strcmp(author, committer) != 0)
4242 printf("via: %s\n", committer);
4243 committer_time = got_object_commit_get_committer_time(commit);
4244 datestr = get_datestr(&committer_time, datebuf);
4246 printf("date: %s UTC\n", datestr);
4247 if (got_object_commit_get_nparents(commit) > 1) {
4248 const struct got_object_id_queue *parent_ids;
4249 struct got_object_qid *qid;
4251 parent_ids = got_object_commit_get_parent_ids(commit);
4252 STAILQ_FOREACH(qid, parent_ids, entry) {
4253 err = got_object_id_str(&id_str, &qid->id);
4256 printf("parent %d: %s\n", n++, id_str);
4262 err = got_object_commit_get_logmsg(&logmsg0, commit);
4268 line = strsep(&logmsg, "\n");
4270 printf(" %s\n", line);
4274 if (changed_paths && diffstat == NULL) {
4275 struct got_pathlist_entry *pe;
4277 TAILQ_FOREACH(pe, changed_paths, entry) {
4278 struct got_diff_changed_path *cp = pe->data;
4280 printf(" %c %s\n", cp->status, pe->path);
4288 err = got_error_from_errno("got_opentemp");
4293 err = print_patch(commit, id, path, diff_context, diffstat,
4294 repo, diffstat == NULL ? stdout : f);
4299 err = print_diffstat(diffstat, NULL);
4311 if (fflush(stdout) != 0 && err == NULL)
4312 err = got_error_from_errno("fflush");
4314 if (f && fclose(f) == EOF && err == NULL)
4315 err = got_error_from_errno("fclose");
4321 static const struct got_error *
4322 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4323 struct got_repository *repo, const char *path, int show_changed_paths,
4324 int show_diffstat, int show_patch, const char *search_pattern,
4325 int diff_context, int limit, int log_branches, int reverse_display_order,
4326 struct got_reflist_object_id_map *refs_idmap, int one_line,
4329 const struct got_error *err;
4330 struct got_commit_graph *graph;
4333 struct got_object_id_queue reversed_commits;
4334 struct got_object_qid *qid;
4335 struct got_commit_object *commit;
4336 struct got_pathlist_head changed_paths;
4338 STAILQ_INIT(&reversed_commits);
4339 TAILQ_INIT(&changed_paths);
4341 if (search_pattern && regcomp(®ex, search_pattern,
4342 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4343 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4345 err = got_commit_graph_open(&graph, path, !log_branches);
4348 err = got_commit_graph_iter_start(graph, root_id, repo,
4349 check_cancelled, NULL);
4353 struct got_object_id id;
4354 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4355 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4357 if (sigint_received || sigpipe_received)
4360 err = got_commit_graph_iter_next(&id, graph, repo,
4361 check_cancelled, NULL);
4363 if (err->code == GOT_ERR_ITER_COMPLETED)
4368 err = got_object_open_as_commit(&commit, repo, &id);
4372 if ((show_changed_paths || (show_diffstat && !show_patch))
4373 && !reverse_display_order) {
4374 err = get_changed_paths(&changed_paths, commit, repo,
4375 show_diffstat ? &dsa : NULL);
4380 if (search_pattern) {
4381 err = match_commit(&have_match, &id, commit, ®ex);
4383 got_object_commit_close(commit);
4386 if (have_match == 0 && show_changed_paths)
4387 match_changed_paths(&have_match,
4388 &changed_paths, ®ex);
4389 if (have_match == 0 && show_patch) {
4390 err = match_patch(&have_match, commit, &id,
4391 path, diff_context, repo, ®ex, tmpfile);
4395 if (have_match == 0) {
4396 got_object_commit_close(commit);
4397 got_pathlist_free(&changed_paths,
4398 GOT_PATHLIST_FREE_ALL);
4403 if (reverse_display_order) {
4404 err = got_object_qid_alloc(&qid, &id);
4407 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4408 got_object_commit_close(commit);
4411 err = print_commit_oneline(commit, &id,
4414 err = print_commit(commit, &id, repo, path,
4415 (show_changed_paths || show_diffstat) ?
4416 &changed_paths : NULL,
4417 show_diffstat ? &dsa : NULL, show_patch,
4418 diff_context, refs_idmap, NULL, NULL);
4419 got_object_commit_close(commit);
4423 if ((limit && --limit == 0) ||
4424 (end_id && got_object_id_cmp(&id, end_id) == 0))
4427 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4429 if (reverse_display_order) {
4430 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4431 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4432 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4434 err = got_object_open_as_commit(&commit, repo,
4438 if (show_changed_paths ||
4439 (show_diffstat && !show_patch)) {
4440 err = get_changed_paths(&changed_paths, commit,
4441 repo, show_diffstat ? &dsa : NULL);
4446 err = print_commit_oneline(commit, &qid->id,
4449 err = print_commit(commit, &qid->id, repo, path,
4450 (show_changed_paths || show_diffstat) ?
4451 &changed_paths : NULL,
4452 show_diffstat ? &dsa : NULL, show_patch,
4453 diff_context, refs_idmap, NULL, NULL);
4454 got_object_commit_close(commit);
4457 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4461 while (!STAILQ_EMPTY(&reversed_commits)) {
4462 qid = STAILQ_FIRST(&reversed_commits);
4463 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4464 got_object_qid_free(qid);
4466 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4469 got_commit_graph_close(graph);
4476 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4477 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4478 "[path]\n", getprogname());
4483 get_default_log_limit(void)
4485 const char *got_default_log_limit;
4489 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4490 if (got_default_log_limit == NULL)
4492 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4498 static const struct got_error *
4499 cmd_log(int argc, char *argv[])
4501 const struct got_error *error;
4502 struct got_repository *repo = NULL;
4503 struct got_worktree *worktree = NULL;
4504 struct got_object_id *start_id = NULL, *end_id = NULL;
4505 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4506 const char *start_commit = NULL, *end_commit = NULL;
4507 const char *search_pattern = NULL;
4508 int diff_context = -1, ch;
4509 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4510 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4512 struct got_reflist_head refs;
4513 struct got_reflist_object_id_map *refs_idmap = NULL;
4514 FILE *tmpfile = NULL;
4515 int *pack_fds = NULL;
4520 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4526 limit = get_default_log_limit();
4528 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4534 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4537 errx(1, "number of context lines is %s: %s",
4541 start_commit = optarg;
4547 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4549 errx(1, "number of commits is %s: %s",
4553 show_changed_paths = 1;
4559 reverse_display_order = 1;
4562 repo_path = realpath(optarg, NULL);
4563 if (repo_path == NULL)
4564 return got_error_from_errno2("realpath",
4566 got_path_strip_trailing_slashes(repo_path);
4569 search_pattern = optarg;
4575 end_commit = optarg;
4586 if (diff_context == -1)
4588 else if (!show_patch)
4589 errx(1, "-C requires -p");
4591 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4592 errx(1, "cannot use -s with -d, -p or -P");
4594 cwd = getcwd(NULL, 0);
4596 error = got_error_from_errno("getcwd");
4600 error = got_repo_pack_fds_open(&pack_fds);
4604 if (repo_path == NULL) {
4605 error = got_worktree_open(&worktree, cwd);
4606 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4613 error = got_worktree_resolve_path(&path, worktree,
4618 path = strdup(argv[0]);
4620 error = got_error_from_errno("strdup");
4624 } else if (argc != 0)
4627 if (repo_path == NULL) {
4628 repo_path = worktree ?
4629 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4631 if (repo_path == NULL) {
4632 error = got_error_from_errno("strdup");
4636 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4640 error = apply_unveil(got_repo_get_path(repo), 1,
4641 worktree ? got_worktree_get_root_path(worktree) : NULL);
4645 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4649 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4653 if (start_commit == NULL) {
4654 struct got_reference *head_ref;
4655 struct got_commit_object *commit = NULL;
4656 error = got_ref_open(&head_ref, repo,
4657 worktree ? got_worktree_get_head_ref_name(worktree)
4661 error = got_ref_resolve(&start_id, repo, head_ref);
4662 got_ref_close(head_ref);
4665 error = got_object_open_as_commit(&commit, repo,
4669 got_object_commit_close(commit);
4671 error = got_repo_match_object_id(&start_id, NULL,
4672 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4676 if (end_commit != NULL) {
4677 error = got_repo_match_object_id(&end_id, NULL,
4678 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4685 * If a path was specified on the command line it was resolved
4686 * to a path in the work tree above. Prepend the work tree's
4687 * path prefix to obtain the corresponding in-repository path.
4691 prefix = got_worktree_get_path_prefix(worktree);
4692 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4693 (path[0] != '\0') ? "/" : "", path) == -1) {
4694 error = got_error_from_errno("asprintf");
4699 error = got_repo_map_path(&in_repo_path, repo,
4705 path = in_repo_path;
4709 /* Release work tree lock. */
4710 got_worktree_close(worktree);
4714 if (search_pattern && show_patch) {
4715 tmpfile = got_opentemp();
4716 if (tmpfile == NULL) {
4717 error = got_error_from_errno("got_opentemp");
4722 error = print_commits(start_id, end_id, repo, path ? path : "",
4723 show_changed_paths, show_diffstat, show_patch, search_pattern,
4724 diff_context, limit, log_branches, reverse_display_order,
4725 refs_idmap, one_line, tmpfile);
4731 got_worktree_close(worktree);
4733 const struct got_error *close_err = got_repo_close(repo);
4738 const struct got_error *pack_err =
4739 got_repo_pack_fds_close(pack_fds);
4744 got_reflist_object_id_map_free(refs_idmap);
4745 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4746 error = got_error_from_errno("fclose");
4747 got_ref_list_free(&refs);
4754 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4755 "[-r repository-path] [object1 object2 | path ...]\n",
4760 struct print_diff_arg {
4761 struct got_repository *repo;
4762 struct got_worktree *worktree;
4763 struct got_diffstat_cb_arg *diffstat;
4768 enum got_diff_algorithm diff_algo;
4769 int ignore_whitespace;
4770 int force_text_diff;
4777 * Create a file which contains the target path of a symlink so we can feed
4778 * it as content to the diff engine.
4780 static const struct got_error *
4781 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4782 const char *abspath)
4784 const struct got_error *err = NULL;
4785 char target_path[PATH_MAX];
4786 ssize_t target_len, outlen;
4791 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4792 if (target_len == -1)
4793 return got_error_from_errno2("readlinkat", abspath);
4795 target_len = readlink(abspath, target_path, PATH_MAX);
4796 if (target_len == -1)
4797 return got_error_from_errno2("readlink", abspath);
4800 *fd = got_opentempfd();
4802 return got_error_from_errno("got_opentempfd");
4804 outlen = write(*fd, target_path, target_len);
4806 err = got_error_from_errno("got_opentempfd");
4810 if (lseek(*fd, 0, SEEK_SET) == -1) {
4811 err = got_error_from_errno2("lseek", abspath);
4822 static const struct got_error *
4823 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4824 const char *path, struct got_object_id *blob_id,
4825 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4826 int dirfd, const char *de_name)
4828 struct print_diff_arg *a = arg;
4829 const struct got_error *err = NULL;
4830 struct got_blob_object *blob1 = NULL;
4831 int fd = -1, fd1 = -1, fd2 = -1;
4833 char *abspath = NULL, *label1 = NULL;
4838 memset(&sb, 0, sizeof(sb));
4840 if (a->diff_staged) {
4841 if (staged_status != GOT_STATUS_MODIFY &&
4842 staged_status != GOT_STATUS_ADD &&
4843 staged_status != GOT_STATUS_DELETE)
4846 if (staged_status == GOT_STATUS_DELETE)
4848 if (status == GOT_STATUS_NONEXISTENT)
4849 return got_error_set_errno(ENOENT, path);
4850 if (status != GOT_STATUS_MODIFY &&
4851 status != GOT_STATUS_ADD &&
4852 status != GOT_STATUS_DELETE &&
4853 status != GOT_STATUS_CONFLICT)
4857 err = got_opentemp_truncate(a->f1);
4859 return got_error_from_errno("got_opentemp_truncate");
4860 err = got_opentemp_truncate(a->f2);
4862 return got_error_from_errno("got_opentemp_truncate");
4864 if (!a->header_shown) {
4865 if (fprintf(a->outfile, "diff %s%s\n",
4866 a->diff_staged ? "-s " : "",
4867 got_worktree_get_root_path(a->worktree)) < 0) {
4868 err = got_error_from_errno("fprintf");
4871 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4872 err = got_error_from_errno("fprintf");
4875 if (fprintf(a->outfile, "path + %s%s\n",
4876 got_worktree_get_root_path(a->worktree),
4877 a->diff_staged ? " (staged changes)" : "") < 0) {
4878 err = got_error_from_errno("fprintf");
4881 a->header_shown = 1;
4884 if (a->diff_staged) {
4885 const char *label1 = NULL, *label2 = NULL;
4886 switch (staged_status) {
4887 case GOT_STATUS_MODIFY:
4891 case GOT_STATUS_ADD:
4894 case GOT_STATUS_DELETE:
4898 return got_error(GOT_ERR_FILE_STATUS);
4900 fd1 = got_opentempfd();
4902 err = got_error_from_errno("got_opentempfd");
4905 fd2 = got_opentempfd();
4907 err = got_error_from_errno("got_opentempfd");
4910 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4911 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4912 a->diff_algo, a->diff_context, a->ignore_whitespace,
4913 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4917 fd1 = got_opentempfd();
4919 err = got_error_from_errno("got_opentempfd");
4923 if (staged_status == GOT_STATUS_ADD ||
4924 staged_status == GOT_STATUS_MODIFY) {
4926 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4930 err = got_object_id_str(&id_str, staged_blob_id);
4933 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4934 err = got_error_from_errno("asprintf");
4939 } else if (status != GOT_STATUS_ADD) {
4940 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4946 if (status != GOT_STATUS_DELETE) {
4947 if (asprintf(&abspath, "%s/%s",
4948 got_worktree_get_root_path(a->worktree), path) == -1) {
4949 err = got_error_from_errno("asprintf");
4954 fd = openat(dirfd, de_name,
4955 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4957 if (!got_err_open_nofollow_on_symlink()) {
4958 err = got_error_from_errno2("openat",
4962 err = get_symlink_target_file(&fd, dirfd,
4968 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4970 if (!got_err_open_nofollow_on_symlink()) {
4971 err = got_error_from_errno2("open",
4975 err = get_symlink_target_file(&fd, dirfd,
4981 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4982 err = got_error_from_errno2("fstatat", abspath);
4985 f2 = fdopen(fd, "r");
4987 err = got_error_from_errno2("fdopen", abspath);
4995 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5001 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5002 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5003 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5005 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5006 err = got_error_from_errno("close");
5007 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5008 err = got_error_from_errno("close");
5010 got_object_blob_close(blob1);
5011 if (fd != -1 && close(fd) == -1 && err == NULL)
5012 err = got_error_from_errno("close");
5013 if (f2 && fclose(f2) == EOF && err == NULL)
5014 err = got_error_from_errno("fclose");
5019 static const struct got_error *
5020 cmd_diff(int argc, char *argv[])
5022 const struct got_error *error;
5023 struct got_repository *repo = NULL;
5024 struct got_worktree *worktree = NULL;
5025 char *cwd = NULL, *repo_path = NULL;
5026 const char *commit_args[2] = { NULL, NULL };
5027 int ncommit_args = 0;
5028 struct got_object_id *ids[2] = { NULL, NULL };
5029 char *labels[2] = { NULL, NULL };
5030 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5031 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5032 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5034 struct got_reflist_head refs;
5035 struct got_pathlist_head diffstat_paths, paths;
5036 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5037 int fd1 = -1, fd2 = -1;
5038 int *pack_fds = NULL;
5039 struct got_diffstat_cb_arg dsa;
5041 memset(&dsa, 0, sizeof(dsa));
5045 TAILQ_INIT(&diffstat_paths);
5048 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5053 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5056 force_text_diff = 1;
5059 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5062 errx(1, "number of context lines is %s: %s",
5066 if (ncommit_args >= 2)
5067 errx(1, "too many -c options used");
5068 commit_args[ncommit_args++] = optarg;
5077 repo_path = realpath(optarg, NULL);
5078 if (repo_path == NULL)
5079 return got_error_from_errno2("realpath",
5081 got_path_strip_trailing_slashes(repo_path);
5088 ignore_whitespace = 1;
5099 cwd = getcwd(NULL, 0);
5101 error = got_error_from_errno("getcwd");
5105 error = got_repo_pack_fds_open(&pack_fds);
5109 if (repo_path == NULL) {
5110 error = got_worktree_open(&worktree, cwd);
5111 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5117 strdup(got_worktree_get_repo_path(worktree));
5118 if (repo_path == NULL) {
5119 error = got_error_from_errno("strdup");
5123 repo_path = strdup(cwd);
5124 if (repo_path == NULL) {
5125 error = got_error_from_errno("strdup");
5131 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5136 if (show_diffstat) {
5137 dsa.paths = &diffstat_paths;
5138 dsa.force_text = force_text_diff;
5139 dsa.ignore_ws = ignore_whitespace;
5140 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5143 if (rflag || worktree == NULL || ncommit_args > 0) {
5145 error = got_error_msg(GOT_ERR_NOT_IMPL,
5146 "-P option can only be used when diffing "
5151 error = got_error_msg(GOT_ERR_NOT_IMPL,
5152 "-s option can only be used when diffing "
5158 error = apply_unveil(got_repo_get_path(repo), 1,
5159 worktree ? got_worktree_get_root_path(worktree) : NULL);
5163 if ((!force_path && argc == 2) || ncommit_args > 0) {
5164 int obj_type = (ncommit_args > 0 ?
5165 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5166 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5170 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5172 if (ncommit_args > 0)
5173 arg = commit_args[i];
5176 error = got_repo_match_object_id(&ids[i], &labels[i],
5177 arg, obj_type, &refs, repo);
5179 if (error->code != GOT_ERR_NOT_REF &&
5180 error->code != GOT_ERR_NO_OBJ)
5182 if (ncommit_args > 0)
5190 f1 = got_opentemp();
5192 error = got_error_from_errno("got_opentemp");
5196 f2 = got_opentemp();
5198 error = got_error_from_errno("got_opentemp");
5202 outfile = got_opentemp();
5203 if (outfile == NULL) {
5204 error = got_error_from_errno("got_opentemp");
5208 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5209 struct print_diff_arg arg;
5212 if (worktree == NULL) {
5213 if (argc == 2 && ids[0] == NULL) {
5214 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5216 } else if (argc == 2 && ids[1] == NULL) {
5217 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5219 } else if (argc > 0) {
5220 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5221 "%s", "specified paths cannot be resolved");
5224 error = got_error(GOT_ERR_NOT_WORKTREE);
5229 error = get_worktree_paths_from_argv(&paths, argc, argv,
5234 error = got_object_id_str(&id_str,
5235 got_worktree_get_base_commit_id(worktree));
5239 arg.worktree = worktree;
5240 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5241 arg.diff_context = diff_context;
5242 arg.id_str = id_str;
5243 arg.header_shown = 0;
5244 arg.diff_staged = diff_staged;
5245 arg.ignore_whitespace = ignore_whitespace;
5246 arg.force_text_diff = force_text_diff;
5247 arg.diffstat = show_diffstat ? &dsa : NULL;
5250 arg.outfile = outfile;
5252 error = got_worktree_status(worktree, &paths, repo, 0,
5253 print_diff, &arg, check_cancelled, NULL);
5258 if (show_diffstat && dsa.nfiles > 0) {
5261 if (asprintf(&header, "diffstat %s%s",
5262 diff_staged ? "-s " : "",
5263 got_worktree_get_root_path(worktree)) == -1) {
5264 error = got_error_from_errno("asprintf");
5268 error = print_diffstat(&dsa, header);
5274 error = printfile(outfile);
5278 if (ncommit_args == 1) {
5279 struct got_commit_object *commit;
5280 error = got_object_open_as_commit(&commit, repo, ids[0]);
5284 labels[1] = labels[0];
5286 if (got_object_commit_get_nparents(commit) > 0) {
5287 const struct got_object_id_queue *pids;
5288 struct got_object_qid *pid;
5289 pids = got_object_commit_get_parent_ids(commit);
5290 pid = STAILQ_FIRST(pids);
5291 ids[0] = got_object_id_dup(&pid->id);
5292 if (ids[0] == NULL) {
5293 error = got_error_from_errno(
5294 "got_object_id_dup");
5295 got_object_commit_close(commit);
5298 error = got_object_id_str(&labels[0], ids[0]);
5300 got_object_commit_close(commit);
5305 labels[0] = strdup("/dev/null");
5306 if (labels[0] == NULL) {
5307 error = got_error_from_errno("strdup");
5308 got_object_commit_close(commit);
5313 got_object_commit_close(commit);
5316 if (ncommit_args == 0 && argc > 2) {
5317 error = got_error_msg(GOT_ERR_BAD_PATH,
5318 "path arguments cannot be used when diffing two objects");
5323 error = got_object_get_type(&type1, repo, ids[0]);
5328 error = got_object_get_type(&type2, repo, ids[1]);
5331 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5332 error = got_error(GOT_ERR_OBJ_TYPE);
5335 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5336 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5337 "path arguments cannot be used when diffing blobs");
5341 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5343 struct got_pathlist_entry *new;
5347 error = got_worktree_resolve_path(&p, worktree,
5351 prefix = got_worktree_get_path_prefix(worktree);
5352 while (prefix[0] == '/')
5354 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5355 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5357 error = got_error_from_errno("asprintf");
5363 char *mapped_path, *s;
5364 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5370 in_repo_path = strdup(s);
5371 if (in_repo_path == NULL) {
5372 error = got_error_from_errno("asprintf");
5379 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5380 if (error || new == NULL /* duplicate */)
5387 /* Release work tree lock. */
5388 got_worktree_close(worktree);
5392 fd1 = got_opentempfd();
5394 error = got_error_from_errno("got_opentempfd");
5398 fd2 = got_opentempfd();
5400 error = got_error_from_errno("got_opentempfd");
5404 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5405 case GOT_OBJ_TYPE_BLOB:
5406 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5407 fd1, fd2, ids[0], ids[1], NULL, NULL,
5408 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5409 ignore_whitespace, force_text_diff,
5410 show_diffstat ? &dsa : NULL, repo, outfile);
5412 case GOT_OBJ_TYPE_TREE:
5413 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5414 ids[0], ids[1], &paths, "", "",
5415 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5416 ignore_whitespace, force_text_diff,
5417 show_diffstat ? &dsa : NULL, repo, outfile);
5419 case GOT_OBJ_TYPE_COMMIT:
5420 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5421 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5422 fd1, fd2, ids[0], ids[1], &paths,
5423 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5424 ignore_whitespace, force_text_diff,
5425 show_diffstat ? &dsa : NULL, repo, outfile);
5428 error = got_error(GOT_ERR_OBJ_TYPE);
5433 if (show_diffstat && dsa.nfiles > 0) {
5434 char *header = NULL;
5436 if (asprintf(&header, "diffstat %s %s",
5437 labels[0], labels[1]) == -1) {
5438 error = got_error_from_errno("asprintf");
5442 error = print_diffstat(&dsa, header);
5448 error = printfile(outfile);
5456 got_worktree_close(worktree);
5458 const struct got_error *close_err = got_repo_close(repo);
5463 const struct got_error *pack_err =
5464 got_repo_pack_fds_close(pack_fds);
5468 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5469 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5470 got_ref_list_free(&refs);
5471 if (outfile && fclose(outfile) == EOF && error == NULL)
5472 error = got_error_from_errno("fclose");
5473 if (f1 && fclose(f1) == EOF && error == NULL)
5474 error = got_error_from_errno("fclose");
5475 if (f2 && fclose(f2) == EOF && error == NULL)
5476 error = got_error_from_errno("fclose");
5477 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5478 error = got_error_from_errno("close");
5479 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5480 error = got_error_from_errno("close");
5488 "usage: %s blame [-c commit] [-r repository-path] path\n",
5497 char datebuf[11]; /* YYYY-MM-DD + NUL */
5500 struct blame_cb_args {
5501 struct blame_line *lines;
5505 off_t *line_offsets;
5507 struct got_repository *repo;
5510 static const struct got_error *
5511 blame_cb(void *arg, int nlines, int lineno,
5512 struct got_commit_object *commit, struct got_object_id *id)
5514 const struct got_error *err = NULL;
5515 struct blame_cb_args *a = arg;
5516 struct blame_line *bline;
5518 size_t linesize = 0;
5521 time_t committer_time;
5523 if (nlines != a->nlines ||
5524 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5525 return got_error(GOT_ERR_RANGE);
5527 if (sigint_received)
5528 return got_error(GOT_ERR_ITER_COMPLETED);
5531 return NULL; /* no change in this commit */
5533 /* Annotate this line. */
5534 bline = &a->lines[lineno - 1];
5535 if (bline->annotated)
5537 err = got_object_id_str(&bline->id_str, id);
5541 bline->committer = strdup(got_object_commit_get_committer(commit));
5542 if (bline->committer == NULL) {
5543 err = got_error_from_errno("strdup");
5547 committer_time = got_object_commit_get_committer_time(commit);
5548 if (gmtime_r(&committer_time, &tm) == NULL)
5549 return got_error_from_errno("gmtime_r");
5550 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5552 err = got_error(GOT_ERR_NO_SPACE);
5555 bline->annotated = 1;
5557 /* Print lines annotated so far. */
5558 bline = &a->lines[a->lineno_cur - 1];
5559 if (!bline->annotated)
5562 offset = a->line_offsets[a->lineno_cur - 1];
5563 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5564 err = got_error_from_errno("fseeko");
5568 while (a->lineno_cur <= a->nlines && bline->annotated) {
5569 char *smallerthan, *at, *nl, *committer;
5572 if (getline(&line, &linesize, a->f) == -1) {
5574 err = got_error_from_errno("getline");
5578 committer = bline->committer;
5579 smallerthan = strchr(committer, '<');
5580 if (smallerthan && smallerthan[1] != '\0')
5581 committer = smallerthan + 1;
5582 at = strchr(committer, '@');
5585 len = strlen(committer);
5587 committer[8] = '\0';
5589 nl = strchr(line, '\n');
5592 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5593 bline->id_str, bline->datebuf, committer, line);
5596 bline = &a->lines[a->lineno_cur - 1];
5603 static const struct got_error *
5604 cmd_blame(int argc, char *argv[])
5606 const struct got_error *error;
5607 struct got_repository *repo = NULL;
5608 struct got_worktree *worktree = NULL;
5609 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5610 char *link_target = NULL;
5611 struct got_object_id *obj_id = NULL;
5612 struct got_object_id *commit_id = NULL;
5613 struct got_commit_object *commit = NULL;
5614 struct got_blob_object *blob = NULL;
5615 char *commit_id_str = NULL;
5616 struct blame_cb_args bca;
5617 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5619 int *pack_fds = NULL;
5620 FILE *f1 = NULL, *f2 = NULL;
5622 fd1 = got_opentempfd();
5624 return got_error_from_errno("got_opentempfd");
5626 memset(&bca, 0, sizeof(bca));
5629 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5634 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5637 commit_id_str = optarg;
5640 repo_path = realpath(optarg, NULL);
5641 if (repo_path == NULL)
5642 return got_error_from_errno2("realpath",
5644 got_path_strip_trailing_slashes(repo_path);
5660 cwd = getcwd(NULL, 0);
5662 error = got_error_from_errno("getcwd");
5666 error = got_repo_pack_fds_open(&pack_fds);
5670 if (repo_path == NULL) {
5671 error = got_worktree_open(&worktree, cwd);
5672 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5678 strdup(got_worktree_get_repo_path(worktree));
5679 if (repo_path == NULL) {
5680 error = got_error_from_errno("strdup");
5685 repo_path = strdup(cwd);
5686 if (repo_path == NULL) {
5687 error = got_error_from_errno("strdup");
5693 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5698 const char *prefix = got_worktree_get_path_prefix(worktree);
5701 error = got_worktree_resolve_path(&p, worktree, path);
5704 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5705 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5707 error = got_error_from_errno("asprintf");
5712 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5714 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5717 error = got_repo_map_path(&in_repo_path, repo, path);
5722 if (commit_id_str == NULL) {
5723 struct got_reference *head_ref;
5724 error = got_ref_open(&head_ref, repo, worktree ?
5725 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5728 error = got_ref_resolve(&commit_id, repo, head_ref);
5729 got_ref_close(head_ref);
5733 struct got_reflist_head refs;
5735 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5739 error = got_repo_match_object_id(&commit_id, NULL,
5740 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5741 got_ref_list_free(&refs);
5747 /* Release work tree lock. */
5748 got_worktree_close(worktree);
5752 error = got_object_open_as_commit(&commit, repo, commit_id);
5756 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5761 error = got_object_id_by_path(&obj_id, repo, commit,
5762 link_target ? link_target : in_repo_path);
5766 error = got_object_get_type(&obj_type, repo, obj_id);
5770 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5771 error = got_error_path(link_target ? link_target : in_repo_path,
5776 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5779 bca.f = got_opentemp();
5780 if (bca.f == NULL) {
5781 error = got_error_from_errno("got_opentemp");
5784 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5785 &bca.line_offsets, bca.f, blob);
5786 if (error || bca.nlines == 0)
5789 /* Don't include \n at EOF in the blame line count. */
5790 if (bca.line_offsets[bca.nlines - 1] == filesize)
5793 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5794 if (bca.lines == NULL) {
5795 error = got_error_from_errno("calloc");
5799 bca.nlines_prec = 0;
5807 fd2 = got_opentempfd();
5809 error = got_error_from_errno("got_opentempfd");
5812 fd3 = got_opentempfd();
5814 error = got_error_from_errno("got_opentempfd");
5817 f1 = got_opentemp();
5819 error = got_error_from_errno("got_opentemp");
5822 f2 = got_opentemp();
5824 error = got_error_from_errno("got_opentemp");
5827 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5828 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5829 check_cancelled, NULL, fd2, fd3, f1, f2);
5838 got_object_commit_close(commit);
5840 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5841 error = got_error_from_errno("close");
5842 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5843 error = got_error_from_errno("close");
5844 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5845 error = got_error_from_errno("close");
5846 if (f1 && fclose(f1) == EOF && error == NULL)
5847 error = got_error_from_errno("fclose");
5848 if (f2 && fclose(f2) == EOF && error == NULL)
5849 error = got_error_from_errno("fclose");
5852 got_object_blob_close(blob);
5854 got_worktree_close(worktree);
5856 const struct got_error *close_err = got_repo_close(repo);
5861 const struct got_error *pack_err =
5862 got_repo_pack_fds_close(pack_fds);
5867 for (i = 0; i < bca.nlines; i++) {
5868 struct blame_line *bline = &bca.lines[i];
5869 free(bline->id_str);
5870 free(bline->committer);
5874 free(bca.line_offsets);
5875 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5876 error = got_error_from_errno("fclose");
5883 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5884 "[path]\n", getprogname());
5888 static const struct got_error *
5889 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5890 const char *root_path, struct got_repository *repo)
5892 const struct got_error *err = NULL;
5893 int is_root_path = (strcmp(path, root_path) == 0);
5894 const char *modestr = "";
5895 mode_t mode = got_tree_entry_get_mode(te);
5896 char *link_target = NULL;
5898 path += strlen(root_path);
5899 while (path[0] == '/')
5902 if (got_object_tree_entry_is_submodule(te))
5904 else if (S_ISLNK(mode)) {
5907 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5910 for (i = 0; i < strlen(link_target); i++) {
5911 if (!isprint((unsigned char)link_target[i]))
5912 link_target[i] = '?';
5917 else if (S_ISDIR(mode))
5919 else if (mode & S_IXUSR)
5922 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5923 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5924 link_target ? " -> ": "", link_target ? link_target : "");
5930 static const struct got_error *
5931 print_tree(const char *path, struct got_commit_object *commit,
5932 int show_ids, int recurse, const char *root_path,
5933 struct got_repository *repo)
5935 const struct got_error *err = NULL;
5936 struct got_object_id *tree_id = NULL;
5937 struct got_tree_object *tree = NULL;
5940 err = got_object_id_by_path(&tree_id, repo, commit, path);
5944 err = got_object_open_as_tree(&tree, repo, tree_id);
5947 nentries = got_object_tree_get_nentries(tree);
5948 for (i = 0; i < nentries; i++) {
5949 struct got_tree_entry *te;
5952 if (sigint_received || sigpipe_received)
5955 te = got_object_tree_get_entry(tree, i);
5958 err = got_object_id_str(&id_str,
5959 got_tree_entry_get_id(te));
5962 if (asprintf(&id, "%s ", id_str) == -1) {
5963 err = got_error_from_errno("asprintf");
5969 err = print_entry(te, id, path, root_path, repo);
5974 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5976 if (asprintf(&child_path, "%s%s%s", path,
5977 path[0] == '/' && path[1] == '\0' ? "" : "/",
5978 got_tree_entry_get_name(te)) == -1) {
5979 err = got_error_from_errno("asprintf");
5982 err = print_tree(child_path, commit, show_ids, 1,
5991 got_object_tree_close(tree);
5996 static const struct got_error *
5997 cmd_tree(int argc, char *argv[])
5999 const struct got_error *error;
6000 struct got_repository *repo = NULL;
6001 struct got_worktree *worktree = NULL;
6002 const char *path, *refname = NULL;
6003 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6004 struct got_object_id *commit_id = NULL;
6005 struct got_commit_object *commit = NULL;
6006 char *commit_id_str = NULL;
6007 int show_ids = 0, recurse = 0;
6009 int *pack_fds = NULL;
6012 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6017 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6020 commit_id_str = optarg;
6029 repo_path = realpath(optarg, NULL);
6030 if (repo_path == NULL)
6031 return got_error_from_errno2("realpath",
6033 got_path_strip_trailing_slashes(repo_path);
6051 cwd = getcwd(NULL, 0);
6053 error = got_error_from_errno("getcwd");
6057 error = got_repo_pack_fds_open(&pack_fds);
6061 if (repo_path == NULL) {
6062 error = got_worktree_open(&worktree, cwd);
6063 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6069 strdup(got_worktree_get_repo_path(worktree));
6070 if (repo_path == NULL)
6071 error = got_error_from_errno("strdup");
6075 repo_path = strdup(cwd);
6076 if (repo_path == NULL) {
6077 error = got_error_from_errno("strdup");
6083 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6088 const char *prefix = got_worktree_get_path_prefix(worktree);
6093 error = got_worktree_resolve_path(&p, worktree, path);
6096 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6097 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6099 error = got_error_from_errno("asprintf");
6104 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6108 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6113 error = got_repo_map_path(&in_repo_path, repo, path);
6118 if (commit_id_str == NULL) {
6119 struct got_reference *head_ref;
6121 refname = got_worktree_get_head_ref_name(worktree);
6123 refname = GOT_REF_HEAD;
6124 error = got_ref_open(&head_ref, repo, refname, 0);
6127 error = got_ref_resolve(&commit_id, repo, head_ref);
6128 got_ref_close(head_ref);
6132 struct got_reflist_head refs;
6134 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6138 error = got_repo_match_object_id(&commit_id, NULL,
6139 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6140 got_ref_list_free(&refs);
6146 /* Release work tree lock. */
6147 got_worktree_close(worktree);
6151 error = got_object_open_as_commit(&commit, repo, commit_id);
6155 error = print_tree(in_repo_path, commit, show_ids, recurse,
6156 in_repo_path, repo);
6163 got_object_commit_close(commit);
6165 got_worktree_close(worktree);
6167 const struct got_error *close_err = got_repo_close(repo);
6172 const struct got_error *pack_err =
6173 got_repo_pack_fds_close(pack_fds);
6183 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6184 "[-s status-codes] [path ...]\n", getprogname());
6188 struct got_status_arg {
6193 static const struct got_error *
6194 print_status(void *arg, unsigned char status, unsigned char staged_status,
6195 const char *path, struct got_object_id *blob_id,
6196 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6197 int dirfd, const char *de_name)
6199 struct got_status_arg *st = arg;
6201 if (status == staged_status && (status == GOT_STATUS_DELETE))
6202 status = GOT_STATUS_NO_CHANGE;
6203 if (st != NULL && st->status_codes) {
6204 size_t ncodes = strlen(st->status_codes);
6207 for (i = 0; i < ncodes ; i++) {
6209 if (status == st->status_codes[i] ||
6210 staged_status == st->status_codes[i]) {
6215 if (status == st->status_codes[i] ||
6216 staged_status == st->status_codes[i])
6221 if (st->suppress && j == 0)
6228 printf("%c%c %s\n", status, staged_status, path);
6232 static const struct got_error *
6233 cmd_status(int argc, char *argv[])
6235 const struct got_error *error = NULL;
6236 struct got_repository *repo = NULL;
6237 struct got_worktree *worktree = NULL;
6238 struct got_status_arg st;
6240 struct got_pathlist_head paths;
6241 int ch, i, no_ignores = 0;
6242 int *pack_fds = NULL;
6246 memset(&st, 0, sizeof(st));
6247 st.status_codes = NULL;
6250 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6256 if (st.status_codes != NULL && st.suppress == 0)
6257 option_conflict('S', 's');
6261 for (i = 0; i < strlen(optarg); i++) {
6262 switch (optarg[i]) {
6263 case GOT_STATUS_MODIFY:
6264 case GOT_STATUS_ADD:
6265 case GOT_STATUS_DELETE:
6266 case GOT_STATUS_CONFLICT:
6267 case GOT_STATUS_MISSING:
6268 case GOT_STATUS_OBSTRUCTED:
6269 case GOT_STATUS_UNVERSIONED:
6270 case GOT_STATUS_MODE_CHANGE:
6271 case GOT_STATUS_NONEXISTENT:
6274 errx(1, "invalid status code '%c'",
6278 if (ch == 's' && st.suppress)
6279 option_conflict('s', 'S');
6280 st.status_codes = optarg;
6292 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6296 cwd = getcwd(NULL, 0);
6298 error = got_error_from_errno("getcwd");
6302 error = got_repo_pack_fds_open(&pack_fds);
6306 error = got_worktree_open(&worktree, cwd);
6308 if (error->code == GOT_ERR_NOT_WORKTREE)
6309 error = wrap_not_worktree_error(error, "status", cwd);
6313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6318 error = apply_unveil(got_repo_get_path(repo), 1,
6319 got_worktree_get_root_path(worktree));
6323 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6327 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6328 print_status, &st, check_cancelled, NULL);
6331 const struct got_error *pack_err =
6332 got_repo_pack_fds_close(pack_fds);
6337 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6345 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6346 "[-s reference] [name]\n", getprogname());
6350 static const struct got_error *
6351 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6353 static const struct got_error *err = NULL;
6354 struct got_reflist_head refs;
6355 struct got_reflist_entry *re;
6358 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6359 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6364 TAILQ_FOREACH(re, &refs, entry) {
6366 refstr = got_ref_to_str(re->ref);
6367 if (refstr == NULL) {
6368 err = got_error_from_errno("got_ref_to_str");
6371 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6375 got_ref_list_free(&refs);
6379 static const struct got_error *
6380 delete_ref_by_name(struct got_repository *repo, const char *refname)
6382 const struct got_error *err;
6383 struct got_reference *ref;
6385 err = got_ref_open(&ref, repo, refname, 0);
6389 err = delete_ref(repo, ref);
6394 static const struct got_error *
6395 add_ref(struct got_repository *repo, const char *refname, const char *target)
6397 const struct got_error *err = NULL;
6398 struct got_object_id *id = NULL;
6399 struct got_reference *ref = NULL;
6400 struct got_reflist_head refs;
6403 * Don't let the user create a reference name with a leading '-'.
6404 * While technically a valid reference name, this case is usually
6405 * an unintended typo.
6407 if (refname[0] == '-')
6408 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6411 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6414 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6416 got_ref_list_free(&refs);
6420 err = got_ref_alloc(&ref, refname, id);
6424 err = got_ref_write(ref, repo);
6432 static const struct got_error *
6433 add_symref(struct got_repository *repo, const char *refname, const char *target)
6435 const struct got_error *err = NULL;
6436 struct got_reference *ref = NULL;
6437 struct got_reference *target_ref = NULL;
6440 * Don't let the user create a reference name with a leading '-'.
6441 * While technically a valid reference name, this case is usually
6442 * an unintended typo.
6444 if (refname[0] == '-')
6445 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6447 err = got_ref_open(&target_ref, repo, target, 0);
6451 err = got_ref_alloc_symref(&ref, refname, target_ref);
6455 err = got_ref_write(ref, repo);
6458 got_ref_close(target_ref);
6464 static const struct got_error *
6465 cmd_ref(int argc, char *argv[])
6467 const struct got_error *error = NULL;
6468 struct got_repository *repo = NULL;
6469 struct got_worktree *worktree = NULL;
6470 char *cwd = NULL, *repo_path = NULL;
6471 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6472 const char *obj_arg = NULL, *symref_target= NULL;
6473 char *refname = NULL;
6474 int *pack_fds = NULL;
6476 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6488 repo_path = realpath(optarg, NULL);
6489 if (repo_path == NULL)
6490 return got_error_from_errno2("realpath",
6492 got_path_strip_trailing_slashes(repo_path);
6495 symref_target = optarg;
6506 if (obj_arg && do_list)
6507 option_conflict('c', 'l');
6508 if (obj_arg && do_delete)
6509 option_conflict('c', 'd');
6510 if (obj_arg && symref_target)
6511 option_conflict('c', 's');
6512 if (symref_target && do_delete)
6513 option_conflict('s', 'd');
6514 if (symref_target && do_list)
6515 option_conflict('s', 'l');
6516 if (do_delete && do_list)
6517 option_conflict('d', 'l');
6518 if (sort_by_time && !do_list)
6519 errx(1, "-t option requires -l option");
6525 if (argc != 0 && argc != 1)
6528 refname = strdup(argv[0]);
6529 if (refname == NULL) {
6530 error = got_error_from_errno("strdup");
6537 refname = strdup(argv[0]);
6538 if (refname == NULL) {
6539 error = got_error_from_errno("strdup");
6545 got_path_strip_trailing_slashes(refname);
6548 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6549 "sendfd unveil", NULL) == -1)
6552 cwd = getcwd(NULL, 0);
6554 error = got_error_from_errno("getcwd");
6558 error = got_repo_pack_fds_open(&pack_fds);
6562 if (repo_path == NULL) {
6563 error = got_worktree_open(&worktree, cwd);
6564 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6570 strdup(got_worktree_get_repo_path(worktree));
6571 if (repo_path == NULL)
6572 error = got_error_from_errno("strdup");
6576 repo_path = strdup(cwd);
6577 if (repo_path == NULL) {
6578 error = got_error_from_errno("strdup");
6584 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6590 /* Remove "cpath" promise. */
6591 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6597 error = apply_unveil(got_repo_get_path(repo), do_list,
6598 worktree ? got_worktree_get_root_path(worktree) : NULL);
6603 error = list_refs(repo, refname, sort_by_time);
6605 error = delete_ref_by_name(repo, refname);
6606 else if (symref_target)
6607 error = add_symref(repo, refname, symref_target);
6609 if (obj_arg == NULL)
6611 error = add_ref(repo, refname, obj_arg);
6616 const struct got_error *close_err = got_repo_close(repo);
6621 got_worktree_close(worktree);
6623 const struct got_error *pack_err =
6624 got_repo_pack_fds_close(pack_fds);
6636 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6637 "[-r repository-path] [name]\n", getprogname());
6641 static const struct got_error *
6642 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6643 struct got_reference *ref)
6645 const struct got_error *err = NULL;
6646 const char *refname, *marker = " ";
6649 refname = got_ref_get_name(ref);
6650 if (worktree && strcmp(refname,
6651 got_worktree_get_head_ref_name(worktree)) == 0) {
6652 struct got_object_id *id = NULL;
6654 err = got_ref_resolve(&id, repo, ref);
6657 if (got_object_id_cmp(id,
6658 got_worktree_get_base_commit_id(worktree)) == 0)
6665 if (strncmp(refname, "refs/heads/", 11) == 0)
6667 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6669 if (strncmp(refname, "refs/remotes/", 13) == 0)
6672 refstr = got_ref_to_str(ref);
6674 return got_error_from_errno("got_ref_to_str");
6676 printf("%s%s: %s\n", marker, refname, refstr);
6681 static const struct got_error *
6682 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6684 const char *refname;
6686 if (worktree == NULL)
6687 return got_error(GOT_ERR_NOT_WORKTREE);
6689 refname = got_worktree_get_head_ref_name(worktree);
6691 if (strncmp(refname, "refs/heads/", 11) == 0)
6693 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6696 printf("%s\n", refname);
6701 static const struct got_error *
6702 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6705 static const struct got_error *err = NULL;
6706 struct got_reflist_head refs;
6707 struct got_reflist_entry *re;
6708 struct got_reference *temp_ref = NULL;
6709 int rebase_in_progress, histedit_in_progress;
6714 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6719 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6724 if (rebase_in_progress || histedit_in_progress) {
6725 err = got_ref_open(&temp_ref, repo,
6726 got_worktree_get_head_ref_name(worktree), 0);
6729 list_branch(repo, worktree, temp_ref);
6730 got_ref_close(temp_ref);
6734 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6735 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6740 TAILQ_FOREACH(re, &refs, entry)
6741 list_branch(repo, worktree, re->ref);
6743 got_ref_list_free(&refs);
6745 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6746 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6751 TAILQ_FOREACH(re, &refs, entry)
6752 list_branch(repo, worktree, re->ref);
6754 got_ref_list_free(&refs);
6759 static const struct got_error *
6760 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6761 const char *branch_name)
6763 const struct got_error *err = NULL;
6764 struct got_reference *ref = NULL;
6765 char *refname, *remote_refname = NULL;
6767 if (strncmp(branch_name, "refs/", 5) == 0)
6769 if (strncmp(branch_name, "heads/", 6) == 0)
6771 else if (strncmp(branch_name, "remotes/", 8) == 0)
6774 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6775 return got_error_from_errno("asprintf");
6777 if (asprintf(&remote_refname, "refs/remotes/%s",
6778 branch_name) == -1) {
6779 err = got_error_from_errno("asprintf");
6783 err = got_ref_open(&ref, repo, refname, 0);
6785 const struct got_error *err2;
6786 if (err->code != GOT_ERR_NOT_REF)
6789 * Keep 'err' intact such that if neither branch exists
6790 * we report "refs/heads" rather than "refs/remotes" in
6791 * our error message.
6793 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6800 strcmp(got_worktree_get_head_ref_name(worktree),
6801 got_ref_get_name(ref)) == 0) {
6802 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6803 "will not delete this work tree's current branch");
6807 err = delete_ref(repo, ref);
6812 free(remote_refname);
6816 static const struct got_error *
6817 add_branch(struct got_repository *repo, const char *branch_name,
6818 struct got_object_id *base_commit_id)
6820 const struct got_error *err = NULL;
6821 struct got_reference *ref = NULL;
6822 char *refname = NULL;
6825 * Don't let the user create a branch name with a leading '-'.
6826 * While technically a valid reference name, this case is usually
6827 * an unintended typo.
6829 if (branch_name[0] == '-')
6830 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6832 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6835 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6836 err = got_error_from_errno("asprintf");
6840 err = got_ref_open(&ref, repo, refname, 0);
6842 err = got_error(GOT_ERR_BRANCH_EXISTS);
6844 } else if (err->code != GOT_ERR_NOT_REF)
6847 err = got_ref_alloc(&ref, refname, base_commit_id);
6851 err = got_ref_write(ref, repo);
6859 static const struct got_error *
6860 cmd_branch(int argc, char *argv[])
6862 const struct got_error *error = NULL;
6863 struct got_repository *repo = NULL;
6864 struct got_worktree *worktree = NULL;
6865 char *cwd = NULL, *repo_path = NULL;
6866 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6867 const char *delref = NULL, *commit_id_arg = NULL;
6868 struct got_reference *ref = NULL;
6869 struct got_pathlist_head paths;
6870 struct got_object_id *commit_id = NULL;
6871 char *commit_id_str = NULL;
6872 int *pack_fds = NULL;
6876 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6879 commit_id_arg = optarg;
6891 repo_path = realpath(optarg, NULL);
6892 if (repo_path == NULL)
6893 return got_error_from_errno2("realpath",
6895 got_path_strip_trailing_slashes(repo_path);
6906 if (do_list && delref)
6907 option_conflict('l', 'd');
6908 if (sort_by_time && !do_list)
6909 errx(1, "-t option requires -l option");
6914 if (!do_list && !delref && argc == 0)
6917 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6918 errx(1, "-c option can only be used when creating a branch");
6920 if (do_list || delref) {
6923 } else if (!do_show && argc != 1)
6927 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6928 "sendfd unveil", NULL) == -1)
6931 cwd = getcwd(NULL, 0);
6933 error = got_error_from_errno("getcwd");
6937 error = got_repo_pack_fds_open(&pack_fds);
6941 if (repo_path == NULL) {
6942 error = got_worktree_open(&worktree, cwd);
6943 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6949 strdup(got_worktree_get_repo_path(worktree));
6950 if (repo_path == NULL)
6951 error = got_error_from_errno("strdup");
6955 repo_path = strdup(cwd);
6956 if (repo_path == NULL) {
6957 error = got_error_from_errno("strdup");
6963 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6968 if (do_list || do_show) {
6969 /* Remove "cpath" promise. */
6970 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6976 error = apply_unveil(got_repo_get_path(repo), do_list,
6977 worktree ? got_worktree_get_root_path(worktree) : NULL);
6982 error = show_current_branch(repo, worktree);
6984 error = list_branches(repo, worktree, sort_by_time);
6986 error = delete_branch(repo, worktree, delref);
6988 struct got_reflist_head refs;
6990 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6994 if (commit_id_arg == NULL)
6995 commit_id_arg = worktree ?
6996 got_worktree_get_head_ref_name(worktree) :
6998 error = got_repo_match_object_id(&commit_id, NULL,
6999 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7000 got_ref_list_free(&refs);
7003 error = add_branch(repo, argv[0], commit_id);
7006 if (worktree && do_update) {
7007 struct got_update_progress_arg upa;
7008 char *branch_refname = NULL;
7010 error = got_object_id_str(&commit_id_str, commit_id);
7013 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7017 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7019 error = got_error_from_errno("asprintf");
7022 error = got_ref_open(&ref, repo, branch_refname, 0);
7023 free(branch_refname);
7026 error = switch_head_ref(ref, commit_id, worktree,
7030 error = got_worktree_set_base_commit_id(worktree, repo,
7034 memset(&upa, 0, sizeof(upa));
7035 error = got_worktree_checkout_files(worktree, &paths,
7036 repo, update_progress, &upa, check_cancelled,
7040 if (upa.did_something) {
7041 printf("Updated to %s: %s\n",
7042 got_worktree_get_head_ref_name(worktree),
7045 print_update_progress_stats(&upa);
7052 const struct got_error *close_err = got_repo_close(repo);
7057 got_worktree_close(worktree);
7059 const struct got_error *pack_err =
7060 got_repo_pack_fds_close(pack_fds);
7067 free(commit_id_str);
7068 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7076 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7077 "[-r repository-path] [-s signer-id] name\n", getprogname());
7082 static const struct got_error *
7083 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7085 const struct got_error *err = NULL;
7086 struct got_reflist_entry *re, *se, *new;
7087 struct got_object_id *re_id, *se_id;
7088 struct got_tag_object *re_tag, *se_tag;
7089 time_t re_time, se_time;
7091 STAILQ_FOREACH(re, tags, entry) {
7092 se = STAILQ_FIRST(sorted);
7094 err = got_reflist_entry_dup(&new, re);
7097 STAILQ_INSERT_HEAD(sorted, new, entry);
7100 err = got_ref_resolve(&re_id, repo, re->ref);
7103 err = got_object_open_as_tag(&re_tag, repo, re_id);
7107 re_time = got_object_tag_get_tagger_time(re_tag);
7108 got_object_tag_close(re_tag);
7112 err = got_ref_resolve(&se_id, repo, re->ref);
7115 err = got_object_open_as_tag(&se_tag, repo, se_id);
7119 se_time = got_object_tag_get_tagger_time(se_tag);
7120 got_object_tag_close(se_tag);
7122 if (se_time > re_time) {
7123 err = got_reflist_entry_dup(&new, re);
7126 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7129 se = STAILQ_NEXT(se, entry);
7138 static const struct got_error *
7139 get_tag_refname(char **refname, const char *tag_name)
7141 const struct got_error *err;
7143 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7144 *refname = strdup(tag_name);
7145 if (*refname == NULL)
7146 return got_error_from_errno("strdup");
7147 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7148 err = got_error_from_errno("asprintf");
7156 static const struct got_error *
7157 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7158 const char *allowed_signers, const char *revoked_signers, int verbosity)
7160 static const struct got_error *err = NULL;
7161 struct got_reflist_head refs;
7162 struct got_reflist_entry *re;
7163 char *wanted_refname = NULL;
7168 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7173 struct got_reference *ref;
7174 err = get_tag_refname(&wanted_refname, tag_name);
7177 /* Wanted tag reference should exist. */
7178 err = got_ref_open(&ref, repo, wanted_refname, 0);
7184 TAILQ_FOREACH(re, &refs, entry) {
7185 const char *refname;
7186 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7188 const char *tagger, *ssh_sig = NULL;
7189 char *sig_msg = NULL;
7191 struct got_object_id *id;
7192 struct got_tag_object *tag;
7193 struct got_commit_object *commit = NULL;
7195 refname = got_ref_get_name(re->ref);
7196 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7197 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7200 refstr = got_ref_to_str(re->ref);
7201 if (refstr == NULL) {
7202 err = got_error_from_errno("got_ref_to_str");
7206 err = got_ref_resolve(&id, repo, re->ref);
7209 err = got_object_open_as_tag(&tag, repo, id);
7211 if (err->code != GOT_ERR_OBJ_TYPE) {
7215 /* "lightweight" tag */
7216 err = got_object_open_as_commit(&commit, repo, id);
7221 tagger = got_object_commit_get_committer(commit);
7223 got_object_commit_get_committer_time(commit);
7224 err = got_object_id_str(&id_str, id);
7230 tagger = got_object_tag_get_tagger(tag);
7231 tagger_time = got_object_tag_get_tagger_time(tag);
7232 err = got_object_id_str(&id_str,
7233 got_object_tag_get_object_id(tag));
7238 if (tag && verify_tags) {
7239 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7240 got_object_tag_get_message(tag));
7241 if (ssh_sig && allowed_signers == NULL) {
7242 err = got_error_msg(
7243 GOT_ERR_VERIFY_TAG_SIGNATURE,
7244 "SSH signature verification requires "
7245 "setting allowed_signers in "
7251 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7253 printf("from: %s\n", tagger);
7254 datestr = get_datestr(&tagger_time, datebuf);
7256 printf("date: %s UTC\n", datestr);
7258 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7260 switch (got_object_tag_get_object_type(tag)) {
7261 case GOT_OBJ_TYPE_BLOB:
7262 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7265 case GOT_OBJ_TYPE_TREE:
7266 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7269 case GOT_OBJ_TYPE_COMMIT:
7270 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7273 case GOT_OBJ_TYPE_TAG:
7274 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7284 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7285 allowed_signers, revoked_signers, verbosity);
7286 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7290 printf("signature: %s", sig_msg);
7296 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7299 got_object_commit_close(commit);
7301 tagmsg0 = strdup(got_object_tag_get_message(tag));
7302 got_object_tag_close(tag);
7303 if (tagmsg0 == NULL) {
7304 err = got_error_from_errno("strdup");
7311 line = strsep(&tagmsg, "\n");
7313 printf(" %s\n", line);
7318 got_ref_list_free(&refs);
7319 free(wanted_refname);
7321 if (err == NULL && bad_sigs)
7322 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7326 static const struct got_error *
7327 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7328 const char *tag_name, const char *repo_path)
7330 const struct got_error *err = NULL;
7331 char *template = NULL, *initial_content = NULL;
7332 char *editor = NULL;
7333 int initial_content_len;
7336 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7337 err = got_error_from_errno("asprintf");
7341 initial_content_len = asprintf(&initial_content,
7342 "\n# tagging commit %s as %s\n",
7343 commit_id_str, tag_name);
7344 if (initial_content_len == -1) {
7345 err = got_error_from_errno("asprintf");
7349 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7353 if (write(fd, initial_content, initial_content_len) == -1) {
7354 err = got_error_from_errno2("write", *tagmsg_path);
7358 err = get_editor(&editor);
7361 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7362 initial_content_len, 1);
7364 free(initial_content);
7368 if (fd != -1 && close(fd) == -1 && err == NULL)
7369 err = got_error_from_errno2("close", *tagmsg_path);
7378 static const struct got_error *
7379 add_tag(struct got_repository *repo, const char *tagger,
7380 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7381 const char *signer_id, int verbosity)
7383 const struct got_error *err = NULL;
7384 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7385 char *label = NULL, *commit_id_str = NULL;
7386 struct got_reference *ref = NULL;
7387 char *refname = NULL, *tagmsg = NULL;
7388 char *tagmsg_path = NULL, *tag_id_str = NULL;
7389 int preserve_tagmsg = 0;
7390 struct got_reflist_head refs;
7395 * Don't let the user create a tag name with a leading '-'.
7396 * While technically a valid reference name, this case is usually
7397 * an unintended typo.
7399 if (tag_name[0] == '-')
7400 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7402 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7406 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7407 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7411 err = got_object_id_str(&commit_id_str, commit_id);
7415 err = get_tag_refname(&refname, tag_name);
7418 if (strncmp("refs/tags/", tag_name, 10) == 0)
7421 err = got_ref_open(&ref, repo, refname, 0);
7423 err = got_error(GOT_ERR_TAG_EXISTS);
7425 } else if (err->code != GOT_ERR_NOT_REF)
7428 if (tagmsg_arg == NULL) {
7429 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7430 tag_name, got_repo_get_path(repo));
7432 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7433 tagmsg_path != NULL)
7434 preserve_tagmsg = 1;
7437 /* Editor is done; we can now apply unveil(2) */
7438 err = got_sigs_apply_unveil();
7441 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7446 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7447 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7451 preserve_tagmsg = 1;
7455 err = got_ref_alloc(&ref, refname, tag_id);
7458 preserve_tagmsg = 1;
7462 err = got_ref_write(ref, repo);
7465 preserve_tagmsg = 1;
7469 err = got_object_id_str(&tag_id_str, tag_id);
7472 preserve_tagmsg = 1;
7475 printf("Created tag %s\n", tag_id_str);
7477 if (preserve_tagmsg) {
7478 fprintf(stderr, "%s: tag message preserved in %s\n",
7479 getprogname(), tagmsg_path);
7480 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7481 err = got_error_from_errno2("unlink", tagmsg_path);
7486 free(commit_id_str);
7490 got_ref_list_free(&refs);
7494 static const struct got_error *
7495 cmd_tag(int argc, char *argv[])
7497 const struct got_error *error = NULL;
7498 struct got_repository *repo = NULL;
7499 struct got_worktree *worktree = NULL;
7500 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7501 char *gitconfig_path = NULL, *tagger = NULL;
7502 char *allowed_signers = NULL, *revoked_signers = NULL;
7503 const char *signer_id = NULL;
7504 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7505 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7506 int *pack_fds = NULL;
7508 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7511 commit_id_arg = optarg;
7520 repo_path = realpath(optarg, NULL);
7521 if (repo_path == NULL) {
7522 error = got_error_from_errno2("realpath",
7526 got_path_strip_trailing_slashes(repo_path);
7537 else if (verbosity < 3)
7549 if (do_list || verify_tags) {
7550 if (commit_id_arg != NULL)
7552 "-c option can only be used when creating a tag");
7555 option_conflict('l', 'm');
7557 option_conflict('V', 'm');
7561 option_conflict('l', 's');
7563 option_conflict('V', 's');
7567 } else if (argc != 1)
7574 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7575 "sendfd unveil", NULL) == -1)
7578 cwd = getcwd(NULL, 0);
7580 error = got_error_from_errno("getcwd");
7584 error = got_repo_pack_fds_open(&pack_fds);
7588 if (repo_path == NULL) {
7589 error = got_worktree_open(&worktree, cwd);
7590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7596 strdup(got_worktree_get_repo_path(worktree));
7597 if (repo_path == NULL)
7598 error = got_error_from_errno("strdup");
7602 repo_path = strdup(cwd);
7603 if (repo_path == NULL) {
7604 error = got_error_from_errno("strdup");
7610 if (do_list || verify_tags) {
7611 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7614 error = get_allowed_signers(&allowed_signers, repo, worktree);
7617 error = get_revoked_signers(&revoked_signers, repo, worktree);
7621 /* Release work tree lock. */
7622 got_worktree_close(worktree);
7627 * Remove "cpath" promise unless needed for signature tmpfile
7631 got_sigs_apply_unveil();
7634 if (pledge("stdio rpath wpath flock proc exec sendfd "
7635 "unveil", NULL) == -1)
7639 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7642 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7643 revoked_signers, verbosity);
7645 error = get_gitconfig_path(&gitconfig_path);
7648 error = got_repo_open(&repo, repo_path, gitconfig_path,
7653 error = get_author(&tagger, repo, worktree);
7656 if (signer_id == NULL)
7657 signer_id = get_signer_id(repo, worktree);
7661 error = got_sigs_apply_unveil();
7665 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7670 if (commit_id_arg == NULL) {
7671 struct got_reference *head_ref;
7672 struct got_object_id *commit_id;
7673 error = got_ref_open(&head_ref, repo,
7674 worktree ? got_worktree_get_head_ref_name(worktree)
7678 error = got_ref_resolve(&commit_id, repo, head_ref);
7679 got_ref_close(head_ref);
7682 error = got_object_id_str(&commit_id_str, commit_id);
7689 /* Release work tree lock. */
7690 got_worktree_close(worktree);
7694 error = add_tag(repo, tagger, tag_name,
7695 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7696 signer_id, verbosity);
7700 const struct got_error *close_err = got_repo_close(repo);
7705 got_worktree_close(worktree);
7707 const struct got_error *pack_err =
7708 got_repo_pack_fds_close(pack_fds);
7714 free(gitconfig_path);
7715 free(commit_id_str);
7717 free(allowed_signers);
7718 free(revoked_signers);
7725 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7729 static const struct got_error *
7730 add_progress(void *arg, unsigned char status, const char *path)
7732 while (path[0] == '/')
7734 printf("%c %s\n", status, path);
7738 static const struct got_error *
7739 cmd_add(int argc, char *argv[])
7741 const struct got_error *error = NULL;
7742 struct got_repository *repo = NULL;
7743 struct got_worktree *worktree = NULL;
7745 struct got_pathlist_head paths;
7746 struct got_pathlist_entry *pe;
7747 int ch, can_recurse = 0, no_ignores = 0;
7748 int *pack_fds = NULL;
7752 while ((ch = getopt(argc, argv, "IR")) != -1) {
7770 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7777 cwd = getcwd(NULL, 0);
7779 error = got_error_from_errno("getcwd");
7783 error = got_repo_pack_fds_open(&pack_fds);
7787 error = got_worktree_open(&worktree, cwd);
7789 if (error->code == GOT_ERR_NOT_WORKTREE)
7790 error = wrap_not_worktree_error(error, "add", cwd);
7794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7799 error = apply_unveil(got_repo_get_path(repo), 1,
7800 got_worktree_get_root_path(worktree));
7804 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7811 TAILQ_FOREACH(pe, &paths, entry) {
7812 if (asprintf(&ondisk_path, "%s/%s",
7813 got_worktree_get_root_path(worktree),
7815 error = got_error_from_errno("asprintf");
7818 if (lstat(ondisk_path, &sb) == -1) {
7819 if (errno == ENOENT) {
7823 error = got_error_from_errno2("lstat",
7829 if (S_ISDIR(sb.st_mode)) {
7830 error = got_error_msg(GOT_ERR_BAD_PATH,
7831 "adding directories requires -R option");
7837 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7838 NULL, repo, no_ignores);
7841 const struct got_error *close_err = got_repo_close(repo);
7846 got_worktree_close(worktree);
7848 const struct got_error *pack_err =
7849 got_repo_pack_fds_close(pack_fds);
7853 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7861 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7866 static const struct got_error *
7867 print_remove_status(void *arg, unsigned char status,
7868 unsigned char staged_status, const char *path)
7870 while (path[0] == '/')
7872 if (status == GOT_STATUS_NONEXISTENT)
7874 if (status == staged_status && (status == GOT_STATUS_DELETE))
7875 status = GOT_STATUS_NO_CHANGE;
7876 printf("%c%c %s\n", status, staged_status, path);
7880 static const struct got_error *
7881 cmd_remove(int argc, char *argv[])
7883 const struct got_error *error = NULL;
7884 struct got_worktree *worktree = NULL;
7885 struct got_repository *repo = NULL;
7886 const char *status_codes = NULL;
7888 struct got_pathlist_head paths;
7889 struct got_pathlist_entry *pe;
7890 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7891 int ignore_missing_paths = 0;
7892 int *pack_fds = NULL;
7896 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7899 delete_local_mods = 1;
7900 ignore_missing_paths = 1;
7909 for (i = 0; i < strlen(optarg); i++) {
7910 switch (optarg[i]) {
7911 case GOT_STATUS_MODIFY:
7912 delete_local_mods = 1;
7914 case GOT_STATUS_MISSING:
7915 ignore_missing_paths = 1;
7918 errx(1, "invalid status code '%c'",
7922 status_codes = optarg;
7934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7941 cwd = getcwd(NULL, 0);
7943 error = got_error_from_errno("getcwd");
7947 error = got_repo_pack_fds_open(&pack_fds);
7951 error = got_worktree_open(&worktree, cwd);
7953 if (error->code == GOT_ERR_NOT_WORKTREE)
7954 error = wrap_not_worktree_error(error, "remove", cwd);
7958 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7963 error = apply_unveil(got_repo_get_path(repo), 1,
7964 got_worktree_get_root_path(worktree));
7968 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7975 TAILQ_FOREACH(pe, &paths, entry) {
7976 if (asprintf(&ondisk_path, "%s/%s",
7977 got_worktree_get_root_path(worktree),
7979 error = got_error_from_errno("asprintf");
7982 if (lstat(ondisk_path, &sb) == -1) {
7983 if (errno == ENOENT) {
7987 error = got_error_from_errno2("lstat",
7993 if (S_ISDIR(sb.st_mode)) {
7994 error = got_error_msg(GOT_ERR_BAD_PATH,
7995 "removing directories requires -R option");
8001 error = got_worktree_schedule_delete(worktree, &paths,
8002 delete_local_mods, status_codes, print_remove_status, NULL,
8003 repo, keep_on_disk, ignore_missing_paths);
8006 const struct got_error *close_err = got_repo_close(repo);
8011 got_worktree_close(worktree);
8013 const struct got_error *pack_err =
8014 got_repo_pack_fds_close(pack_fds);
8018 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8026 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8027 "[patchfile]\n", getprogname());
8031 static const struct got_error *
8032 patch_from_stdin(int *patchfd)
8034 const struct got_error *err = NULL;
8037 sig_t sighup, sigint, sigquit;
8039 *patchfd = got_opentempfd();
8041 return got_error_from_errno("got_opentempfd");
8043 sighup = signal(SIGHUP, SIG_DFL);
8044 sigint = signal(SIGINT, SIG_DFL);
8045 sigquit = signal(SIGQUIT, SIG_DFL);
8048 r = read(0, buf, sizeof(buf));
8050 err = got_error_from_errno("read");
8055 if (write(*patchfd, buf, r) == -1) {
8056 err = got_error_from_errno("write");
8061 signal(SIGHUP, sighup);
8062 signal(SIGINT, sigint);
8063 signal(SIGQUIT, sigquit);
8065 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8066 err = got_error_from_errno("lseek");
8076 static const struct got_error *
8077 patch_progress(void *arg, const char *old, const char *new,
8078 unsigned char status, const struct got_error *error, int old_from,
8079 int old_lines, int new_from, int new_lines, int offset,
8080 int ws_mangled, const struct got_error *hunk_err)
8082 const char *path = new == NULL ? old : new;
8084 while (*path == '/')
8088 printf("%c %s\n", status, path);
8091 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8093 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8094 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8095 old_lines, new_from, new_lines);
8096 if (hunk_err != NULL)
8097 printf("%s\n", hunk_err->msg);
8098 else if (offset != 0)
8099 printf("applied with offset %d\n", offset);
8101 printf("hunk contains mangled whitespace\n");
8107 static const struct got_error *
8108 cmd_patch(int argc, char *argv[])
8110 const struct got_error *error = NULL, *close_error = NULL;
8111 struct got_worktree *worktree = NULL;
8112 struct got_repository *repo = NULL;
8113 struct got_reflist_head refs;
8114 struct got_object_id *commit_id = NULL;
8115 const char *commit_id_str = NULL;
8119 int ch, nop = 0, strip = -1, reverse = 0;
8121 int *pack_fds = NULL;
8126 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8127 "unveil", NULL) == -1)
8131 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8134 commit_id_str = optarg;
8140 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8142 errx(1, "pathname strip count is %s: %s",
8158 error = patch_from_stdin(&patchfd);
8161 } else if (argc == 1) {
8162 patchfd = open(argv[0], O_RDONLY);
8163 if (patchfd == -1) {
8164 error = got_error_from_errno2("open", argv[0]);
8167 if (fstat(patchfd, &sb) == -1) {
8168 error = got_error_from_errno2("fstat", argv[0]);
8171 if (!S_ISREG(sb.st_mode)) {
8172 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8178 if ((cwd = getcwd(NULL, 0)) == NULL) {
8179 error = got_error_from_errno("getcwd");
8183 error = got_repo_pack_fds_open(&pack_fds);
8187 error = got_worktree_open(&worktree, cwd);
8191 const char *repo_path = got_worktree_get_repo_path(worktree);
8192 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8196 error = apply_unveil(got_repo_get_path(repo), 0,
8197 got_worktree_get_root_path(worktree));
8201 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8205 if (commit_id_str != NULL) {
8206 error = got_repo_match_object_id(&commit_id, NULL,
8207 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8212 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8213 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8216 got_ref_list_free(&refs);
8219 close_error = got_repo_close(repo);
8221 error = close_error;
8223 if (worktree != NULL) {
8224 close_error = got_worktree_close(worktree);
8226 error = close_error;
8229 const struct got_error *pack_err =
8230 got_repo_pack_fds_close(pack_fds);
8241 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8246 static const struct got_error *
8247 revert_progress(void *arg, unsigned char status, const char *path)
8249 if (status == GOT_STATUS_UNVERSIONED)
8252 while (path[0] == '/')
8254 printf("%c %s\n", status, path);
8258 struct choose_patch_arg {
8259 FILE *patch_script_file;
8263 static const struct got_error *
8264 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8265 int nchanges, const char *action)
8267 const struct got_error *err;
8269 size_t linesize = 0;
8273 case GOT_STATUS_ADD:
8274 printf("A %s\n%s this addition? [y/n] ", path, action);
8276 case GOT_STATUS_DELETE:
8277 printf("D %s\n%s this deletion? [y/n] ", path, action);
8279 case GOT_STATUS_MODIFY:
8280 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8281 return got_error_from_errno("fseek");
8282 printf(GOT_COMMIT_SEP_STR);
8283 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8285 if (linelen == -1 && ferror(patch_file)) {
8286 err = got_error_from_errno("getline");
8291 printf(GOT_COMMIT_SEP_STR);
8292 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8293 path, n, nchanges, action);
8296 return got_error_path(path, GOT_ERR_FILE_STATUS);
8303 static const struct got_error *
8304 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8305 FILE *patch_file, int n, int nchanges)
8307 const struct got_error *err = NULL;
8309 size_t linesize = 0;
8312 struct choose_patch_arg *a = arg;
8314 *choice = GOT_PATCH_CHOICE_NONE;
8316 if (a->patch_script_file) {
8318 err = show_change(status, path, patch_file, n, nchanges,
8322 linelen = getline(&line, &linesize, a->patch_script_file);
8323 if (linelen == -1) {
8324 if (ferror(a->patch_script_file))
8325 return got_error_from_errno("getline");
8328 nl = strchr(line, '\n');
8331 if (strcmp(line, "y") == 0) {
8332 *choice = GOT_PATCH_CHOICE_YES;
8334 } else if (strcmp(line, "n") == 0) {
8335 *choice = GOT_PATCH_CHOICE_NO;
8337 } else if (strcmp(line, "q") == 0 &&
8338 status == GOT_STATUS_MODIFY) {
8339 *choice = GOT_PATCH_CHOICE_QUIT;
8342 printf("invalid response '%s'\n", line);
8347 while (resp != 'y' && resp != 'n' && resp != 'q') {
8348 err = show_change(status, path, patch_file, n, nchanges,
8355 if (status == GOT_STATUS_MODIFY) {
8356 if (resp != 'y' && resp != 'n' && resp != 'q') {
8357 printf("invalid response '%c'\n", resp);
8360 } else if (resp != 'y' && resp != 'n') {
8361 printf("invalid response '%c'\n", resp);
8367 *choice = GOT_PATCH_CHOICE_YES;
8368 else if (resp == 'n')
8369 *choice = GOT_PATCH_CHOICE_NO;
8370 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8371 *choice = GOT_PATCH_CHOICE_QUIT;
8376 struct wt_commitable_path_arg {
8377 struct got_pathlist_head *commit_paths;
8382 * Shortcut work tree status callback to determine if the set of paths scanned
8383 * has at least one versioned path that is being modified and, if not NULL, is
8384 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8385 * soon as a path is passed with a status that satisfies this criteria.
8387 static const struct got_error *
8388 worktree_has_commitable_path(void *arg, unsigned char status,
8389 unsigned char staged_status, const char *path,
8390 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8391 struct got_object_id *commit_id, int dirfd, const char *de_name)
8393 struct wt_commitable_path_arg *a = arg;
8395 if (status == staged_status && (status == GOT_STATUS_DELETE))
8396 status = GOT_STATUS_NO_CHANGE;
8398 if (!(status == GOT_STATUS_NO_CHANGE ||
8399 status == GOT_STATUS_UNVERSIONED) ||
8400 staged_status != GOT_STATUS_NO_CHANGE) {
8401 if (a->commit_paths != NULL) {
8402 struct got_pathlist_entry *pe;
8404 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8405 if (strncmp(path, pe->path,
8406 pe->path_len) == 0) {
8407 *a->has_changes = 1;
8412 *a->has_changes = 1;
8414 if (*a->has_changes)
8415 return got_error(GOT_ERR_FILE_MODIFIED);
8422 * Check that the changeset of the commit identified by id is
8423 * comprised of at least one modified path that is being committed.
8425 static const struct got_error *
8426 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8427 struct got_object_id *id, struct got_worktree *worktree,
8428 struct got_repository *repo)
8430 const struct got_error *err;
8431 struct got_pathlist_head paths;
8432 struct got_commit_object *commit = NULL, *pcommit = NULL;
8433 struct got_tree_object *tree = NULL, *ptree = NULL;
8434 struct got_object_qid *pid;
8438 err = got_object_open_as_commit(&commit, repo, id);
8442 err = got_object_open_as_tree(&tree, repo,
8443 got_object_commit_get_tree_id(commit));
8447 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8449 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8453 err = got_object_open_as_tree(&ptree, repo,
8454 got_object_commit_get_tree_id(pcommit));
8459 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8460 got_diff_tree_collect_changed_paths, &paths, 0);
8464 err = got_worktree_status(worktree, &paths, repo, 0,
8465 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8466 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8468 * At least one changed path in the referenced commit is
8469 * modified in the work tree, that's all we need to know!
8475 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8477 got_object_commit_close(commit);
8479 got_object_commit_close(pcommit);
8481 got_object_tree_close(tree);
8483 got_object_tree_close(ptree);
8488 * Remove any "logmsg" reference comprised entirely of paths that have
8489 * been reverted in this work tree. If any path in the logmsg ref changeset
8490 * remains in a changed state in the worktree, do not remove the reference.
8492 static const struct got_error *
8493 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8495 const struct got_error *err;
8496 struct got_reflist_head refs;
8497 struct got_reflist_entry *re;
8498 struct got_commit_object *commit = NULL;
8499 struct got_object_id *commit_id = NULL;
8500 struct wt_commitable_path_arg wcpa;
8501 char *uuidstr = NULL;
8505 err = got_worktree_get_uuid(&uuidstr, worktree);
8509 err = got_ref_list(&refs, repo, "refs/got/worktree",
8510 got_ref_cmp_by_name, repo);
8514 TAILQ_FOREACH(re, &refs, entry) {
8515 const char *refname;
8516 int has_changes = 0;
8518 refname = got_ref_get_name(re->ref);
8520 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8521 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8522 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8523 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8524 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8525 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8529 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8530 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8534 err = got_repo_match_object_id(&commit_id, NULL, refname,
8535 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8539 err = got_object_open_as_commit(&commit, repo, commit_id);
8543 wcpa.commit_paths = NULL;
8544 wcpa.has_changes = &has_changes;
8546 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8552 err = got_ref_delete(re->ref, repo);
8557 got_object_commit_close(commit);
8566 got_ref_list_free(&refs);
8568 got_object_commit_close(commit);
8572 static const struct got_error *
8573 cmd_revert(int argc, char *argv[])
8575 const struct got_error *error = NULL;
8576 struct got_worktree *worktree = NULL;
8577 struct got_repository *repo = NULL;
8578 char *cwd = NULL, *path = NULL;
8579 struct got_pathlist_head paths;
8580 struct got_pathlist_entry *pe;
8581 int ch, can_recurse = 0, pflag = 0;
8582 FILE *patch_script_file = NULL;
8583 const char *patch_script_path = NULL;
8584 struct choose_patch_arg cpa;
8585 int *pack_fds = NULL;
8589 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8592 patch_script_path = optarg;
8610 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8611 "unveil", NULL) == -1)
8616 if (patch_script_path && !pflag)
8617 errx(1, "-F option can only be used together with -p option");
8619 cwd = getcwd(NULL, 0);
8621 error = got_error_from_errno("getcwd");
8625 error = got_repo_pack_fds_open(&pack_fds);
8629 error = got_worktree_open(&worktree, cwd);
8631 if (error->code == GOT_ERR_NOT_WORKTREE)
8632 error = wrap_not_worktree_error(error, "revert", cwd);
8636 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8641 if (patch_script_path) {
8642 patch_script_file = fopen(patch_script_path, "re");
8643 if (patch_script_file == NULL) {
8644 error = got_error_from_errno2("fopen",
8651 * XXX "c" perm needed on repo dir to delete merge references.
8653 error = apply_unveil(got_repo_get_path(repo), 0,
8654 got_worktree_get_root_path(worktree));
8658 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8665 TAILQ_FOREACH(pe, &paths, entry) {
8666 if (asprintf(&ondisk_path, "%s/%s",
8667 got_worktree_get_root_path(worktree),
8669 error = got_error_from_errno("asprintf");
8672 if (lstat(ondisk_path, &sb) == -1) {
8673 if (errno == ENOENT) {
8677 error = got_error_from_errno2("lstat",
8683 if (S_ISDIR(sb.st_mode)) {
8684 error = got_error_msg(GOT_ERR_BAD_PATH,
8685 "reverting directories requires -R option");
8691 cpa.patch_script_file = patch_script_file;
8692 cpa.action = "revert";
8693 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8694 pflag ? choose_patch : NULL, &cpa, repo);
8696 error = rm_logmsg_ref(worktree, repo);
8698 if (patch_script_file && fclose(patch_script_file) == EOF &&
8700 error = got_error_from_errno2("fclose", patch_script_path);
8702 const struct got_error *close_err = got_repo_close(repo);
8707 got_worktree_close(worktree);
8709 const struct got_error *pack_err =
8710 got_repo_pack_fds_close(pack_fds);
8714 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8723 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8724 "[-m message] [path ...]\n", getprogname());
8728 struct collect_commit_logmsg_arg {
8729 const char *cmdline_log;
8730 const char *prepared_log;
8731 const char *merged_log;
8732 int non_interactive;
8734 const char *worktree_path;
8735 const char *branch_name;
8736 const char *repo_path;
8741 static const struct got_error *
8742 read_prepared_logmsg(char **logmsg, const char *path)
8744 const struct got_error *err = NULL;
8750 memset(&sb, 0, sizeof(sb));
8752 f = fopen(path, "re");
8754 return got_error_from_errno2("fopen", path);
8756 if (fstat(fileno(f), &sb) == -1) {
8757 err = got_error_from_errno2("fstat", path);
8760 if (sb.st_size == 0) {
8761 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8765 *logmsg = malloc(sb.st_size + 1);
8766 if (*logmsg == NULL) {
8767 err = got_error_from_errno("malloc");
8771 r = fread(*logmsg, 1, sb.st_size, f);
8772 if (r != sb.st_size) {
8774 err = got_error_from_errno2("fread", path);
8776 err = got_error(GOT_ERR_IO);
8779 (*logmsg)[sb.st_size] = '\0';
8781 if (fclose(f) == EOF && err == NULL)
8782 err = got_error_from_errno2("fclose", path);
8790 static const struct got_error *
8791 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8792 const char *diff_path, char **logmsg, void *arg)
8794 char *initial_content = NULL;
8795 struct got_pathlist_entry *pe;
8796 const struct got_error *err = NULL;
8797 char *template = NULL;
8798 char *prepared_msg = NULL, *merged_msg = NULL;
8799 struct collect_commit_logmsg_arg *a = arg;
8800 int initial_content_len;
8804 /* if a message was specified on the command line, just use it */
8805 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8806 len = strlen(a->cmdline_log) + 1;
8807 *logmsg = malloc(len + 1);
8808 if (*logmsg == NULL)
8809 return got_error_from_errno("malloc");
8810 strlcpy(*logmsg, a->cmdline_log, len);
8812 } else if (a->prepared_log != NULL && a->non_interactive)
8813 return read_prepared_logmsg(logmsg, a->prepared_log);
8815 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8816 return got_error_from_errno("asprintf");
8818 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8822 if (a->prepared_log) {
8823 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8826 } else if (a->merged_log) {
8827 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8832 initial_content_len = asprintf(&initial_content,
8833 "%s%s\n# changes to be committed on branch %s:\n",
8834 prepared_msg ? prepared_msg : "",
8835 merged_msg ? merged_msg : "", a->branch_name);
8836 if (initial_content_len == -1) {
8837 err = got_error_from_errno("asprintf");
8841 if (write(fd, initial_content, initial_content_len) == -1) {
8842 err = got_error_from_errno2("write", a->logmsg_path);
8846 TAILQ_FOREACH(pe, commitable_paths, entry) {
8847 struct got_commitable *ct = pe->data;
8848 dprintf(fd, "# %c %s\n",
8849 got_commitable_get_status(ct),
8850 got_commitable_get_path(ct));
8854 dprintf(fd, "# detailed changes can be viewed in %s\n",
8858 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8859 initial_content_len, a->prepared_log ? 0 : 1);
8861 free(initial_content);
8866 if (fd != -1 && close(fd) == -1 && err == NULL)
8867 err = got_error_from_errno2("close", a->logmsg_path);
8869 /* Editor is done; we can now apply unveil(2) */
8871 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8879 static const struct got_error *
8880 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8881 const char *type, int has_content)
8883 const struct got_error *err = NULL;
8884 char *logmsg = NULL;
8886 err = got_object_commit_get_logmsg(&logmsg, commit);
8890 if (fprintf(f, "%s# log message of %s commit %s:%s",
8891 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8892 err = got_ferror(f, GOT_ERR_IO);
8899 * Lookup "logmsg" references of backed-out and cherrypicked commits
8900 * belonging to the current work tree. If found, and the worktree has
8901 * at least one modified file that was changed in the referenced commit,
8902 * add its log message to a new temporary file at *logmsg_path.
8903 * Add all refs found to matched_refs to be scheduled for removal on
8904 * successful commit.
8906 static const struct got_error *
8907 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8908 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8909 struct got_repository *repo)
8911 const struct got_error *err;
8912 struct got_commit_object *commit = NULL;
8913 struct got_object_id *id = NULL;
8914 struct got_reflist_head refs;
8915 struct got_reflist_entry *re, *re_match;
8917 char *uuidstr = NULL;
8918 int added_logmsg = 0;
8922 *logmsg_path = NULL;
8924 err = got_worktree_get_uuid(&uuidstr, worktree);
8928 err = got_ref_list(&refs, repo, "refs/got/worktree",
8929 got_ref_cmp_by_name, repo);
8933 TAILQ_FOREACH(re, &refs, entry) {
8934 const char *refname, *type;
8935 struct wt_commitable_path_arg wcpa;
8938 refname = got_ref_get_name(re->ref);
8940 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8941 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8942 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8943 type = "cherrypicked";
8944 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8945 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8946 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8947 type = "backed-out";
8951 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8952 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8956 err = got_repo_match_object_id(&id, NULL, refname,
8957 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8961 err = got_object_open_as_commit(&commit, repo, id);
8965 wcpa.commit_paths = paths;
8966 wcpa.has_changes = &add_logmsg;
8968 err = commit_path_changed_in_worktree(&wcpa, id,
8975 err = got_opentemp_named(logmsg_path, &f,
8976 "got-commit-logmsg", "");
8980 err = cat_logmsg(f, commit, refname, type,
8987 err = got_reflist_entry_dup(&re_match, re);
8990 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
8993 got_object_commit_close(commit);
9002 got_ref_list_free(&refs);
9004 got_object_commit_close(commit);
9005 if (f && fclose(f) == EOF && err == NULL)
9006 err = got_error_from_errno("fclose");
9007 if (!added_logmsg) {
9008 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9009 err = got_error_from_errno2("unlink", *logmsg_path);
9010 *logmsg_path = NULL;
9015 static const struct got_error *
9016 cmd_commit(int argc, char *argv[])
9018 const struct got_error *error = NULL;
9019 struct got_worktree *worktree = NULL;
9020 struct got_repository *repo = NULL;
9021 char *cwd = NULL, *id_str = NULL;
9022 struct got_object_id *id = NULL;
9023 const char *logmsg = NULL;
9024 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9025 struct collect_commit_logmsg_arg cl_arg;
9026 const char *author = NULL;
9027 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9028 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9029 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9031 struct got_pathlist_head paths;
9032 struct got_reflist_head refs;
9033 struct got_reflist_entry *re;
9034 int *pack_fds = NULL;
9038 cl_arg.logmsg_path = NULL;
9040 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9044 error = valid_author(author);
9050 option_conflict('F', 'm');
9051 prepared_logmsg = realpath(optarg, NULL);
9052 if (prepared_logmsg == NULL)
9053 return got_error_from_errno2("realpath",
9057 if (prepared_logmsg)
9058 option_conflict('m', 'F');
9062 non_interactive = 1;
9068 allow_bad_symlinks = 1;
9080 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9081 "unveil", NULL) == -1)
9084 cwd = getcwd(NULL, 0);
9086 error = got_error_from_errno("getcwd");
9090 error = got_repo_pack_fds_open(&pack_fds);
9094 error = got_worktree_open(&worktree, cwd);
9096 if (error->code == GOT_ERR_NOT_WORKTREE)
9097 error = wrap_not_worktree_error(error, "commit", cwd);
9101 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9104 if (rebase_in_progress) {
9105 error = got_error(GOT_ERR_REBASING);
9109 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9114 error = get_gitconfig_path(&gitconfig_path);
9117 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9118 gitconfig_path, pack_fds);
9122 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9125 if (merge_in_progress) {
9126 error = got_error(GOT_ERR_MERGE_BUSY);
9130 error = get_author(&committer, repo, worktree);
9134 if (author != NULL && !strcmp(committer, author)) {
9135 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
9143 * unveil(2) traverses exec(2); if an editor is used we have
9144 * to apply unveil after the log message has been written.
9146 if (logmsg == NULL || strlen(logmsg) == 0)
9147 error = get_editor(&editor);
9149 error = apply_unveil(got_repo_get_path(repo), 0,
9150 got_worktree_get_root_path(worktree));
9154 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9158 if (prepared_logmsg == NULL) {
9159 error = lookup_logmsg_ref(&merged_logmsg,
9160 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9165 cl_arg.editor = editor;
9166 cl_arg.cmdline_log = logmsg;
9167 cl_arg.prepared_log = prepared_logmsg;
9168 cl_arg.merged_log = merged_logmsg;
9169 cl_arg.non_interactive = non_interactive;
9170 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9171 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9172 if (!histedit_in_progress) {
9173 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9174 error = got_error(GOT_ERR_COMMIT_BRANCH);
9177 cl_arg.branch_name += 11;
9179 cl_arg.repo_path = got_repo_get_path(repo);
9180 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9181 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9182 print_status, NULL, repo);
9184 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9185 cl_arg.logmsg_path != NULL)
9186 preserve_logmsg = 1;
9190 error = got_object_id_str(&id_str, id);
9193 printf("Created commit %s\n", id_str);
9195 TAILQ_FOREACH(re, &refs, entry) {
9196 error = got_ref_delete(re->ref, repo);
9202 if (preserve_logmsg) {
9203 fprintf(stderr, "%s: log message preserved in %s\n",
9204 getprogname(), cl_arg.logmsg_path);
9205 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9207 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9208 free(cl_arg.logmsg_path);
9209 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9210 error = got_error_from_errno2("unlink", merged_logmsg);
9211 free(merged_logmsg);
9213 const struct got_error *close_err = got_repo_close(repo);
9218 got_worktree_close(worktree);
9220 const struct got_error *pack_err =
9221 got_repo_pack_fds_close(pack_fds);
9225 got_ref_list_free(&refs);
9226 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9229 free(gitconfig_path);
9232 free(prepared_logmsg);
9239 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9240 "[-r repository-path] [-t tag] [remote-repository]\n",
9246 print_load_info(int print_colored, int print_found, int print_trees,
9247 int ncolored, int nfound, int ntrees)
9249 if (print_colored) {
9250 printf("%d commit%s colored", ncolored,
9251 ncolored == 1 ? "" : "s");
9254 printf("%s%d object%s found",
9255 ncolored > 0 ? "; " : "",
9256 nfound, nfound == 1 ? "" : "s");
9259 printf("; %d tree%s scanned", ntrees,
9260 ntrees == 1 ? "" : "s");
9264 struct got_send_progress_arg {
9265 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9272 int last_nobj_total;
9276 int printed_something;
9278 struct got_pathlist_head *delete_branches;
9281 static const struct got_error *
9282 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9283 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9284 int nobj_written, off_t bytes_sent, const char *refname,
9285 const char *errmsg, int success)
9287 struct got_send_progress_arg *a = arg;
9288 char scaled_packsize[FMT_SCALED_STRSIZE];
9289 char scaled_sent[FMT_SCALED_STRSIZE];
9290 int p_deltify = 0, p_written = 0, p_sent = 0;
9291 int print_colored = 0, print_found = 0, print_trees = 0;
9292 int print_searching = 0, print_total = 0;
9293 int print_deltify = 0, print_written = 0, print_sent = 0;
9295 if (a->verbosity < 0)
9299 const char *status = success ? "accepted" : "rejected";
9302 struct got_pathlist_entry *pe;
9303 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9304 const char *branchname = pe->path;
9305 if (got_path_cmp(branchname, refname,
9306 strlen(branchname), strlen(refname)) == 0) {
9308 a->sent_something = 1;
9314 if (a->printed_something)
9316 printf("Server has %s %s", status, refname);
9318 printf(": %s", errmsg);
9319 a->printed_something = 1;
9323 if (a->last_ncolored != ncolored) {
9325 a->last_ncolored = ncolored;
9328 if (a->last_nfound != nfound) {
9331 a->last_nfound = nfound;
9334 if (a->last_ntrees != ntrees) {
9338 a->last_ntrees = ntrees;
9341 if ((print_colored || print_found || print_trees) &&
9344 print_load_info(print_colored, print_found, print_trees,
9345 ncolored, nfound, ntrees);
9346 a->printed_something = 1;
9349 } else if (!a->loading_done) {
9351 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9353 a->loading_done = 1;
9356 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9357 return got_error_from_errno("fmt_scaled");
9358 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9359 return got_error_from_errno("fmt_scaled");
9361 if (a->last_ncommits != ncommits) {
9362 print_searching = 1;
9363 a->last_ncommits = ncommits;
9366 if (a->last_nobj_total != nobj_total) {
9367 print_searching = 1;
9369 a->last_nobj_total = nobj_total;
9372 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9373 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9374 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9375 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9376 return got_error(GOT_ERR_NO_SPACE);
9379 if (nobj_deltify > 0 || nobj_written > 0) {
9380 if (nobj_deltify > 0) {
9381 p_deltify = (nobj_deltify * 100) / nobj_total;
9382 if (p_deltify != a->last_p_deltify) {
9383 a->last_p_deltify = p_deltify;
9384 print_searching = 1;
9389 if (nobj_written > 0) {
9390 p_written = (nobj_written * 100) / nobj_total;
9391 if (p_written != a->last_p_written) {
9392 a->last_p_written = p_written;
9393 print_searching = 1;
9401 if (bytes_sent > 0) {
9402 p_sent = (bytes_sent * 100) / packfile_size;
9403 if (p_sent != a->last_p_sent) {
9404 a->last_p_sent = p_sent;
9405 print_searching = 1;
9411 a->sent_something = 1;
9414 if (print_searching || print_total || print_deltify || print_written ||
9417 if (print_searching)
9418 printf("packing %d reference%s", ncommits,
9419 ncommits == 1 ? "" : "s");
9421 printf("; %d object%s", nobj_total,
9422 nobj_total == 1 ? "" : "s");
9424 printf("; deltify: %d%%", p_deltify);
9426 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9427 scaled_packsize, p_sent);
9428 else if (print_written)
9429 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9430 scaled_packsize, p_written);
9431 if (print_searching || print_total || print_deltify ||
9432 print_written || print_sent) {
9433 a->printed_something = 1;
9439 static const struct got_error *
9440 cmd_send(int argc, char *argv[])
9442 const struct got_error *error = NULL;
9443 char *cwd = NULL, *repo_path = NULL;
9444 const char *remote_name;
9445 char *proto = NULL, *host = NULL, *port = NULL;
9446 char *repo_name = NULL, *server_path = NULL;
9447 const struct got_remote_repo *remotes, *remote = NULL;
9448 int nremotes, nbranches = 0, ndelete_branches = 0;
9449 struct got_repository *repo = NULL;
9450 struct got_worktree *worktree = NULL;
9451 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9452 struct got_pathlist_head branches;
9453 struct got_pathlist_head tags;
9454 struct got_reflist_head all_branches;
9455 struct got_reflist_head all_tags;
9456 struct got_pathlist_head delete_args;
9457 struct got_pathlist_head delete_branches;
9458 struct got_reflist_entry *re;
9459 struct got_pathlist_entry *pe;
9460 int i, ch, sendfd = -1, sendstatus;
9462 struct got_send_progress_arg spa;
9463 int verbosity = 0, overwrite_refs = 0;
9464 int send_all_branches = 0, send_all_tags = 0;
9465 struct got_reference *ref = NULL;
9466 int *pack_fds = NULL;
9468 TAILQ_INIT(&branches);
9470 TAILQ_INIT(&all_branches);
9471 TAILQ_INIT(&all_tags);
9472 TAILQ_INIT(&delete_args);
9473 TAILQ_INIT(&delete_branches);
9475 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9478 send_all_branches = 1;
9481 error = got_pathlist_append(&branches, optarg, NULL);
9487 error = got_pathlist_append(&delete_args, optarg, NULL);
9498 repo_path = realpath(optarg, NULL);
9499 if (repo_path == NULL)
9500 return got_error_from_errno2("realpath",
9502 got_path_strip_trailing_slashes(repo_path);
9508 error = got_pathlist_append(&tags, optarg, NULL);
9515 else if (verbosity < 3)
9526 if (send_all_branches && !TAILQ_EMPTY(&branches))
9527 option_conflict('a', 'b');
9528 if (send_all_tags && !TAILQ_EMPTY(&tags))
9529 option_conflict('T', 't');
9533 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9535 remote_name = argv[0];
9539 cwd = getcwd(NULL, 0);
9541 error = got_error_from_errno("getcwd");
9545 error = got_repo_pack_fds_open(&pack_fds);
9549 if (repo_path == NULL) {
9550 error = got_worktree_open(&worktree, cwd);
9551 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9557 strdup(got_worktree_get_repo_path(worktree));
9558 if (repo_path == NULL)
9559 error = got_error_from_errno("strdup");
9563 repo_path = strdup(cwd);
9564 if (repo_path == NULL) {
9565 error = got_error_from_errno("strdup");
9571 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9576 worktree_conf = got_worktree_get_gotconfig(worktree);
9577 if (worktree_conf) {
9578 got_gotconfig_get_remotes(&nremotes, &remotes,
9580 for (i = 0; i < nremotes; i++) {
9581 if (strcmp(remotes[i].name, remote_name) == 0) {
9582 remote = &remotes[i];
9588 if (remote == NULL) {
9589 repo_conf = got_repo_get_gotconfig(repo);
9591 got_gotconfig_get_remotes(&nremotes, &remotes,
9593 for (i = 0; i < nremotes; i++) {
9594 if (strcmp(remotes[i].name, remote_name) == 0) {
9595 remote = &remotes[i];
9601 if (remote == NULL) {
9602 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9603 for (i = 0; i < nremotes; i++) {
9604 if (strcmp(remotes[i].name, remote_name) == 0) {
9605 remote = &remotes[i];
9610 if (remote == NULL) {
9611 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9615 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9616 &repo_name, remote->send_url);
9620 if (strcmp(proto, "git") == 0) {
9622 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9623 "sendfd dns inet unveil", NULL) == -1)
9626 } else if (strcmp(proto, "git+ssh") == 0 ||
9627 strcmp(proto, "ssh") == 0) {
9629 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9630 "sendfd unveil", NULL) == -1)
9633 } else if (strcmp(proto, "http") == 0 ||
9634 strcmp(proto, "git+http") == 0) {
9635 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9638 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9642 error = got_dial_apply_unveil(proto);
9646 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9650 if (send_all_branches) {
9651 error = got_ref_list(&all_branches, repo, "refs/heads",
9652 got_ref_cmp_by_name, NULL);
9655 TAILQ_FOREACH(re, &all_branches, entry) {
9656 const char *branchname = got_ref_get_name(re->ref);
9657 error = got_pathlist_append(&branches,
9663 } else if (nbranches == 0) {
9664 for (i = 0; i < remote->nsend_branches; i++) {
9665 error = got_pathlist_append(&branches,
9666 remote->send_branches[i], NULL);
9672 if (send_all_tags) {
9673 error = got_ref_list(&all_tags, repo, "refs/tags",
9674 got_ref_cmp_by_name, NULL);
9677 TAILQ_FOREACH(re, &all_tags, entry) {
9678 const char *tagname = got_ref_get_name(re->ref);
9679 error = got_pathlist_append(&tags,
9687 * To prevent accidents only branches in refs/heads/ can be deleted
9688 * with 'got send -d'.
9689 * Deleting anything else requires local repository access or Git.
9691 TAILQ_FOREACH(pe, &delete_args, entry) {
9692 const char *branchname = pe->path;
9694 struct got_pathlist_entry *new;
9695 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9696 s = strdup(branchname);
9698 error = got_error_from_errno("strdup");
9702 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9703 error = got_error_from_errno("asprintf");
9707 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9708 if (error || new == NULL /* duplicate */)
9715 if (nbranches == 0 && ndelete_branches == 0) {
9716 struct got_reference *head_ref;
9718 error = got_ref_open(&head_ref, repo,
9719 got_worktree_get_head_ref_name(worktree), 0);
9721 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9724 if (got_ref_is_symbolic(head_ref)) {
9725 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9726 got_ref_close(head_ref);
9731 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9738 if (verbosity >= 0) {
9739 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9740 remote->name, proto, host,
9741 port ? ":" : "", port ? port : "",
9742 *server_path == '/' ? "" : "/", server_path);
9745 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9746 server_path, verbosity);
9750 memset(&spa, 0, sizeof(spa));
9751 spa.last_scaled_packsize[0] = '\0';
9752 spa.last_p_deltify = -1;
9753 spa.last_p_written = -1;
9754 spa.verbosity = verbosity;
9755 spa.delete_branches = &delete_branches;
9756 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9757 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9758 check_cancelled, NULL);
9759 if (spa.printed_something)
9763 if (!spa.sent_something && verbosity >= 0)
9764 printf("Already up-to-date\n");
9767 if (kill(sendpid, SIGTERM) == -1)
9768 error = got_error_from_errno("kill");
9769 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9770 error = got_error_from_errno("waitpid");
9772 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9773 error = got_error_from_errno("close");
9775 const struct got_error *close_err = got_repo_close(repo);
9780 got_worktree_close(worktree);
9782 const struct got_error *pack_err =
9783 got_repo_pack_fds_close(pack_fds);
9789 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9790 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9791 got_ref_list_free(&all_branches);
9792 got_ref_list_free(&all_tags);
9793 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9794 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9806 * Print and if delete is set delete all ref_prefix references.
9807 * If wanted_ref is not NULL, only print or delete this reference.
9809 static const struct got_error *
9810 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9811 const char *wanted_ref, int delete, struct got_worktree *worktree,
9812 struct got_repository *repo)
9814 const struct got_error *err;
9815 struct got_pathlist_head paths;
9816 struct got_reflist_head refs;
9817 struct got_reflist_entry *re;
9818 struct got_reflist_object_id_map *refs_idmap = NULL;
9819 struct got_commit_object *commit = NULL;
9820 struct got_object_id *id = NULL;
9821 const char *header_prefix;
9822 char *uuidstr = NULL;
9828 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9832 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9836 if (worktree != NULL) {
9837 err = got_worktree_get_uuid(&uuidstr, worktree);
9843 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9847 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9848 header_prefix = "backout";
9850 header_prefix = "cherrypick";
9852 TAILQ_FOREACH(re, &refs, entry) {
9853 const char *refname, *wt;
9855 refname = got_ref_get_name(re->ref);
9857 err = check_cancelled(NULL);
9861 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9862 refname += prefix_len + 1; /* skip '-' delimiter */
9868 if (worktree == NULL || strncmp(refname, uuidstr,
9869 GOT_WORKTREE_UUID_STRLEN) == 0)
9870 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9874 err = got_repo_match_object_id(&id, NULL, refname,
9875 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9879 err = got_object_open_as_commit(&commit, repo, id);
9884 found = strncmp(wanted_ref, refname,
9885 strlen(wanted_ref)) == 0;
9886 if (wanted_ref && !found) {
9887 struct got_reflist_head *ci_refs;
9889 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9893 char *refs_str = NULL;
9894 char const *r = NULL;
9896 err = build_refs_str(&refs_str, ci_refs, id,
9903 if (strncmp(r, wanted_ref,
9904 strlen(wanted_ref)) == 0) {
9916 if (wanted_ref == NULL || found) {
9918 err = got_ref_delete(re->ref, repo);
9921 printf("Deleted: ");
9922 err = print_commit_oneline(commit, id, repo,
9926 * Print paths modified by commit to help
9927 * associate commits with worktree changes.
9929 err = get_changed_paths(&paths, commit,
9934 err = print_commit(commit, id, repo, NULL,
9935 &paths, NULL, 0, 0, refs_idmap, NULL,
9937 got_pathlist_free(&paths,
9938 GOT_PATHLIST_FREE_ALL);
9940 if (worktree == NULL)
9941 printf("work tree: %.*s\n\n",
9942 GOT_WORKTREE_UUID_STRLEN, wt);
9948 got_object_commit_close(commit);
9954 if (wanted_ref != NULL && !found)
9955 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
9960 got_ref_list_free(&refs);
9961 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
9963 got_reflist_object_id_map_free(refs_idmap);
9965 got_object_commit_close(commit);
9970 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
9971 * identified by id for log messages to prepopulate the editor on commit.
9973 static const struct got_error *
9974 logmsg_ref(struct got_object_id *id, const char *prefix,
9975 struct got_worktree *worktree, struct got_repository *repo)
9977 const struct got_error *err = NULL;
9978 char *idstr, *ref = NULL, *refname = NULL;
9979 int histedit_in_progress;
9980 int rebase_in_progress, merge_in_progress;
9983 * Silenty refuse to create merge reference if any histedit, merge,
9984 * or rebase operation is in progress.
9986 err = got_worktree_histedit_in_progress(&histedit_in_progress,
9990 if (histedit_in_progress)
9993 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9996 if (rebase_in_progress)
9999 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10003 if (merge_in_progress)
10006 err = got_object_id_str(&idstr, id);
10010 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10014 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10015 err = got_error_from_errno("asprintf");
10019 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10029 usage_cherrypick(void)
10031 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10036 static const struct got_error *
10037 cmd_cherrypick(int argc, char *argv[])
10039 const struct got_error *error = NULL;
10040 struct got_worktree *worktree = NULL;
10041 struct got_repository *repo = NULL;
10042 char *cwd = NULL, *commit_id_str = NULL;
10043 struct got_object_id *commit_id = NULL;
10044 struct got_commit_object *commit = NULL;
10045 struct got_object_qid *pid;
10046 int ch, list_refs = 0, remove_refs = 0;
10047 struct got_update_progress_arg upa;
10048 int *pack_fds = NULL;
10050 while ((ch = getopt(argc, argv, "lX")) != -1) {
10059 usage_cherrypick();
10068 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10069 "unveil", NULL) == -1)
10072 if (list_refs || remove_refs) {
10073 if (argc != 0 && argc != 1)
10074 usage_cherrypick();
10075 } else if (argc != 1)
10076 usage_cherrypick();
10077 if (list_refs && remove_refs)
10078 option_conflict('l', 'X');
10080 cwd = getcwd(NULL, 0);
10082 error = got_error_from_errno("getcwd");
10086 error = got_repo_pack_fds_open(&pack_fds);
10090 error = got_worktree_open(&worktree, cwd);
10092 if (list_refs || remove_refs) {
10093 if (error->code != GOT_ERR_NOT_WORKTREE)
10096 if (error->code == GOT_ERR_NOT_WORKTREE)
10097 error = wrap_not_worktree_error(error,
10098 "cherrypick", cwd);
10103 error = got_repo_open(&repo,
10104 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10109 error = apply_unveil(got_repo_get_path(repo), 0,
10110 worktree ? got_worktree_get_root_path(worktree) : NULL);
10114 if (list_refs || remove_refs) {
10115 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10116 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10117 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10121 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10122 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10125 error = got_object_id_str(&commit_id_str, commit_id);
10129 error = got_object_open_as_commit(&commit, repo, commit_id);
10132 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10133 memset(&upa, 0, sizeof(upa));
10134 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10135 commit_id, repo, update_progress, &upa, check_cancelled,
10140 if (upa.did_something) {
10141 error = logmsg_ref(commit_id,
10142 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10145 printf("Merged commit %s\n", commit_id_str);
10147 print_merge_progress_stats(&upa);
10151 got_object_commit_close(commit);
10152 free(commit_id_str);
10154 got_worktree_close(worktree);
10156 const struct got_error *close_err = got_repo_close(repo);
10161 const struct got_error *pack_err =
10162 got_repo_pack_fds_close(pack_fds);
10171 usage_backout(void)
10173 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10177 static const struct got_error *
10178 cmd_backout(int argc, char *argv[])
10180 const struct got_error *error = NULL;
10181 struct got_worktree *worktree = NULL;
10182 struct got_repository *repo = NULL;
10183 char *cwd = NULL, *commit_id_str = NULL;
10184 struct got_object_id *commit_id = NULL;
10185 struct got_commit_object *commit = NULL;
10186 struct got_object_qid *pid;
10187 int ch, list_refs = 0, remove_refs = 0;
10188 struct got_update_progress_arg upa;
10189 int *pack_fds = NULL;
10191 while ((ch = getopt(argc, argv, "lX")) != -1) {
10209 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10210 "unveil", NULL) == -1)
10213 if (list_refs || remove_refs) {
10214 if (argc != 0 && argc != 1)
10216 } else if (argc != 1)
10218 if (list_refs && remove_refs)
10219 option_conflict('l', 'X');
10221 cwd = getcwd(NULL, 0);
10223 error = got_error_from_errno("getcwd");
10227 error = got_repo_pack_fds_open(&pack_fds);
10231 error = got_worktree_open(&worktree, cwd);
10233 if (list_refs || remove_refs) {
10234 if (error->code != GOT_ERR_NOT_WORKTREE)
10237 if (error->code == GOT_ERR_NOT_WORKTREE)
10238 error = wrap_not_worktree_error(error,
10244 error = got_repo_open(&repo,
10245 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10250 error = apply_unveil(got_repo_get_path(repo), 0,
10251 worktree ? got_worktree_get_root_path(worktree) : NULL);
10255 if (list_refs || remove_refs) {
10256 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10257 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10258 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10262 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10263 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10266 error = got_object_id_str(&commit_id_str, commit_id);
10270 error = got_object_open_as_commit(&commit, repo, commit_id);
10273 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10275 error = got_error(GOT_ERR_ROOT_COMMIT);
10279 memset(&upa, 0, sizeof(upa));
10280 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10281 repo, update_progress, &upa, check_cancelled, NULL);
10285 if (upa.did_something) {
10286 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10290 printf("Backed out commit %s\n", commit_id_str);
10292 print_merge_progress_stats(&upa);
10296 got_object_commit_close(commit);
10297 free(commit_id_str);
10299 got_worktree_close(worktree);
10301 const struct got_error *close_err = got_repo_close(repo);
10306 const struct got_error *pack_err =
10307 got_repo_pack_fds_close(pack_fds);
10317 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10322 trim_logmsg(char *logmsg, int limit)
10327 len = strlen(logmsg);
10330 logmsg[len] = '\0';
10331 nl = strchr(logmsg, '\n');
10336 static const struct got_error *
10337 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10339 const struct got_error *err;
10340 char *logmsg0 = NULL;
10343 err = got_object_commit_get_logmsg(&logmsg0, commit);
10348 while (isspace((unsigned char)s[0]))
10351 *logmsg = strdup(s);
10352 if (*logmsg == NULL) {
10353 err = got_error_from_errno("strdup");
10357 trim_logmsg(*logmsg, limit);
10363 static const struct got_error *
10364 show_rebase_merge_conflict(struct got_object_id *id,
10365 struct got_repository *repo)
10367 const struct got_error *err;
10368 struct got_commit_object *commit = NULL;
10369 char *id_str = NULL, *logmsg = NULL;
10371 err = got_object_open_as_commit(&commit, repo, id);
10375 err = got_object_id_str(&id_str, id);
10381 err = get_short_logmsg(&logmsg, 42, commit);
10385 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10388 got_object_commit_close(commit);
10393 static const struct got_error *
10394 show_rebase_progress(struct got_commit_object *commit,
10395 struct got_object_id *old_id, struct got_object_id *new_id)
10397 const struct got_error *err;
10398 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10400 err = got_object_id_str(&old_id_str, old_id);
10405 err = got_object_id_str(&new_id_str, new_id);
10410 old_id_str[12] = '\0';
10412 new_id_str[12] = '\0';
10414 err = get_short_logmsg(&logmsg, 42, commit);
10418 printf("%s -> %s: %s\n", old_id_str,
10419 new_id_str ? new_id_str : "no-op change", logmsg);
10427 static const struct got_error *
10428 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10429 struct got_reference *branch, struct got_reference *tmp_branch,
10430 struct got_repository *repo, int create_backup)
10432 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10433 return got_worktree_rebase_complete(worktree, fileindex,
10434 tmp_branch, branch, repo, create_backup);
10437 static const struct got_error *
10438 rebase_commit(struct got_pathlist_head *merged_paths,
10439 struct got_worktree *worktree, struct got_fileindex *fileindex,
10440 struct got_reference *tmp_branch, const char *committer,
10441 struct got_object_id *commit_id, struct got_repository *repo)
10443 const struct got_error *error;
10444 struct got_commit_object *commit;
10445 struct got_object_id *new_commit_id;
10447 error = got_object_open_as_commit(&commit, repo, commit_id);
10451 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10452 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10455 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10457 error = show_rebase_progress(commit, commit_id, NULL);
10459 error = show_rebase_progress(commit, commit_id, new_commit_id);
10460 free(new_commit_id);
10463 got_object_commit_close(commit);
10467 struct check_path_prefix_arg {
10468 const char *path_prefix;
10473 static const struct got_error *
10474 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10475 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10476 struct got_object_id *id1, struct got_object_id *id2,
10477 const char *path1, const char *path2,
10478 mode_t mode1, mode_t mode2, struct got_repository *repo)
10480 struct check_path_prefix_arg *a = arg;
10482 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10483 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10484 return got_error(a->errcode);
10489 static const struct got_error *
10490 check_path_prefix(struct got_object_id *parent_id,
10491 struct got_object_id *commit_id, const char *path_prefix,
10492 int errcode, struct got_repository *repo)
10494 const struct got_error *err;
10495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10496 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10497 struct check_path_prefix_arg cpp_arg;
10499 if (got_path_is_root_dir(path_prefix))
10502 err = got_object_open_as_commit(&commit, repo, commit_id);
10506 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10510 err = got_object_open_as_tree(&tree1, repo,
10511 got_object_commit_get_tree_id(parent_commit));
10515 err = got_object_open_as_tree(&tree2, repo,
10516 got_object_commit_get_tree_id(commit));
10520 cpp_arg.path_prefix = path_prefix;
10521 while (cpp_arg.path_prefix[0] == '/')
10522 cpp_arg.path_prefix++;
10523 cpp_arg.len = strlen(cpp_arg.path_prefix);
10524 cpp_arg.errcode = errcode;
10525 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10526 check_path_prefix_in_diff, &cpp_arg, 0);
10529 got_object_tree_close(tree1);
10531 got_object_tree_close(tree2);
10533 got_object_commit_close(commit);
10535 got_object_commit_close(parent_commit);
10539 static const struct got_error *
10540 collect_commits(struct got_object_id_queue *commits,
10541 struct got_object_id *initial_commit_id,
10542 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10543 const char *path_prefix, int path_prefix_errcode,
10544 struct got_repository *repo)
10546 const struct got_error *err = NULL;
10547 struct got_commit_graph *graph = NULL;
10548 struct got_object_id parent_id, commit_id;
10549 struct got_object_qid *qid;
10551 err = got_commit_graph_open(&graph, "/", 1);
10555 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10556 check_cancelled, NULL);
10560 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10561 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10562 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10563 check_cancelled, NULL);
10565 if (err->code == GOT_ERR_ITER_COMPLETED) {
10566 err = got_error_msg(GOT_ERR_ANCESTRY,
10567 "ran out of commits to rebase before "
10568 "youngest common ancestor commit has "
10569 "been reached?!?");
10573 err = check_path_prefix(&parent_id, &commit_id,
10574 path_prefix, path_prefix_errcode, repo);
10578 err = got_object_qid_alloc(&qid, &commit_id);
10581 STAILQ_INSERT_HEAD(commits, qid, entry);
10583 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10587 got_commit_graph_close(graph);
10591 static const struct got_error *
10592 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10594 const struct got_error *err = NULL;
10595 time_t committer_time;
10597 char datebuf[11]; /* YYYY-MM-DD + NUL */
10598 char *author0 = NULL, *author, *smallerthan;
10599 char *logmsg0 = NULL, *logmsg, *newline;
10601 committer_time = got_object_commit_get_committer_time(commit);
10602 if (gmtime_r(&committer_time, &tm) == NULL)
10603 return got_error_from_errno("gmtime_r");
10604 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10605 return got_error(GOT_ERR_NO_SPACE);
10607 author0 = strdup(got_object_commit_get_author(commit));
10608 if (author0 == NULL)
10609 return got_error_from_errno("strdup");
10611 smallerthan = strchr(author, '<');
10612 if (smallerthan && smallerthan[1] != '\0')
10613 author = smallerthan + 1;
10614 author[strcspn(author, "@>")] = '\0';
10616 err = got_object_commit_get_logmsg(&logmsg0, commit);
10620 while (*logmsg == '\n')
10622 newline = strchr(logmsg, '\n');
10626 if (asprintf(brief_str, "%s %s %s",
10627 datebuf, author, logmsg) == -1)
10628 err = got_error_from_errno("asprintf");
10635 static const struct got_error *
10636 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10637 struct got_repository *repo)
10639 const struct got_error *err;
10642 err = got_object_id_str(&id_str, id);
10646 err = got_ref_delete(ref, repo);
10650 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10656 static const struct got_error *
10657 print_backup_ref(const char *branch_name, const char *new_id_str,
10658 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10659 struct got_reflist_object_id_map *refs_idmap,
10660 struct got_repository *repo)
10662 const struct got_error *err = NULL;
10663 struct got_reflist_head *refs;
10664 char *refs_str = NULL;
10665 struct got_object_id *new_commit_id = NULL;
10666 struct got_commit_object *new_commit = NULL;
10667 char *new_commit_brief_str = NULL;
10668 struct got_object_id *yca_id = NULL;
10669 struct got_commit_object *yca_commit = NULL;
10670 char *yca_id_str = NULL, *yca_brief_str = NULL;
10671 char *custom_refs_str;
10673 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10674 return got_error_from_errno("asprintf");
10676 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10677 0, 0, refs_idmap, custom_refs_str, NULL);
10681 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10685 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10687 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10692 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10696 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10700 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10701 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10705 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10706 refs_str ? " (" : "", refs_str ? refs_str : "",
10707 refs_str ? ")" : "", new_commit_brief_str);
10708 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10709 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10713 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10717 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10721 err = got_object_id_str(&yca_id_str, yca_id);
10725 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10727 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10731 printf("history forked at %s%s%s%s\n %s\n",
10733 refs_str ? " (" : "", refs_str ? refs_str : "",
10734 refs_str ? ")" : "", yca_brief_str);
10737 free(custom_refs_str);
10738 free(new_commit_id);
10742 free(yca_brief_str);
10744 got_object_commit_close(new_commit);
10746 got_object_commit_close(yca_commit);
10751 static const struct got_error *
10752 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10753 struct got_repository *repo)
10755 const struct got_error *err;
10756 struct got_reflist_head refs;
10757 struct got_reflist_entry *re;
10758 char *uuidstr = NULL;
10759 static char msg[160];
10763 err = got_worktree_get_uuid(&uuidstr, worktree);
10767 err = got_ref_list(&refs, repo, "refs/got/worktree",
10768 got_ref_cmp_by_name, repo);
10772 TAILQ_FOREACH(re, &refs, entry) {
10773 const char *cmd, *refname, *type;
10775 refname = got_ref_get_name(re->ref);
10777 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10778 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10779 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10780 cmd = "cherrypick";
10781 type = "cherrypicked";
10782 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10783 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10784 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10786 type = "backed-out";
10790 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10793 snprintf(msg, sizeof(msg),
10794 "work tree has references created by %s commits which "
10795 "must be removed with 'got %s -X' before running the %s "
10796 "command", type, cmd, caller);
10797 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10803 got_ref_list_free(&refs);
10807 static const struct got_error *
10808 process_backup_refs(const char *backup_ref_prefix,
10809 const char *wanted_branch_name,
10810 int delete, struct got_repository *repo)
10812 const struct got_error *err;
10813 struct got_reflist_head refs, backup_refs;
10814 struct got_reflist_entry *re;
10815 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10816 struct got_object_id *old_commit_id = NULL;
10817 char *branch_name = NULL;
10818 struct got_commit_object *old_commit = NULL;
10819 struct got_reflist_object_id_map *refs_idmap = NULL;
10820 int wanted_branch_found = 0;
10823 TAILQ_INIT(&backup_refs);
10825 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10829 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10833 if (wanted_branch_name) {
10834 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10835 wanted_branch_name += 11;
10838 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10839 got_ref_cmp_by_commit_timestamp_descending, repo);
10843 TAILQ_FOREACH(re, &backup_refs, entry) {
10844 const char *refname = got_ref_get_name(re->ref);
10847 err = check_cancelled(NULL);
10851 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10855 err = got_object_open_as_commit(&old_commit, repo,
10860 if (strncmp(backup_ref_prefix, refname,
10861 backup_ref_prefix_len) == 0)
10862 refname += backup_ref_prefix_len;
10864 while (refname[0] == '/')
10867 branch_name = strdup(refname);
10868 if (branch_name == NULL) {
10869 err = got_error_from_errno("strdup");
10872 slash = strrchr(branch_name, '/');
10875 refname += strlen(branch_name) + 1;
10878 if (wanted_branch_name == NULL ||
10879 strcmp(wanted_branch_name, branch_name) == 0) {
10880 wanted_branch_found = 1;
10882 err = delete_backup_ref(re->ref,
10883 old_commit_id, repo);
10885 err = print_backup_ref(branch_name, refname,
10886 old_commit_id, old_commit, refs_idmap,
10893 free(old_commit_id);
10894 old_commit_id = NULL;
10896 branch_name = NULL;
10897 got_object_commit_close(old_commit);
10901 if (wanted_branch_name && !wanted_branch_found) {
10902 err = got_error_fmt(GOT_ERR_NOT_REF,
10903 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10907 got_reflist_object_id_map_free(refs_idmap);
10908 got_ref_list_free(&refs);
10909 got_ref_list_free(&backup_refs);
10910 free(old_commit_id);
10913 got_object_commit_close(old_commit);
10917 static const struct got_error *
10918 abort_progress(void *arg, unsigned char status, const char *path)
10921 * Unversioned files should not clutter progress output when
10922 * an operation is aborted.
10924 if (status == GOT_STATUS_UNVERSIONED)
10927 return update_progress(arg, status, path);
10930 static const struct got_error *
10931 cmd_rebase(int argc, char *argv[])
10933 const struct got_error *error = NULL;
10934 struct got_worktree *worktree = NULL;
10935 struct got_repository *repo = NULL;
10936 struct got_fileindex *fileindex = NULL;
10937 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10938 struct got_reference *branch = NULL;
10939 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10940 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10941 struct got_object_id *resume_commit_id = NULL;
10942 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10943 struct got_object_id *head_commit_id = NULL;
10944 struct got_reference *head_ref = NULL;
10945 struct got_commit_object *commit = NULL;
10946 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10947 int histedit_in_progress = 0, merge_in_progress = 0;
10948 int create_backup = 1, list_backups = 0, delete_backups = 0;
10949 struct got_object_id_queue commits;
10950 struct got_pathlist_head merged_paths;
10951 const struct got_object_id_queue *parent_ids;
10952 struct got_object_qid *qid, *pid;
10953 struct got_update_progress_arg upa;
10954 int *pack_fds = NULL;
10956 STAILQ_INIT(&commits);
10957 TAILQ_INIT(&merged_paths);
10958 memset(&upa, 0, sizeof(upa));
10960 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10966 continue_rebase = 1;
10972 delete_backups = 1;
10984 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10985 "unveil", NULL) == -1)
10988 if (list_backups) {
10990 option_conflict('l', 'a');
10991 if (continue_rebase)
10992 option_conflict('l', 'c');
10993 if (delete_backups)
10994 option_conflict('l', 'X');
10995 if (argc != 0 && argc != 1)
10997 } else if (delete_backups) {
10999 option_conflict('X', 'a');
11000 if (continue_rebase)
11001 option_conflict('X', 'c');
11003 option_conflict('l', 'X');
11004 if (argc != 0 && argc != 1)
11007 if (abort_rebase && continue_rebase)
11009 else if (abort_rebase || continue_rebase) {
11012 } else if (argc != 1)
11016 cwd = getcwd(NULL, 0);
11018 error = got_error_from_errno("getcwd");
11022 error = got_repo_pack_fds_open(&pack_fds);
11026 error = got_worktree_open(&worktree, cwd);
11028 if (list_backups || delete_backups) {
11029 if (error->code != GOT_ERR_NOT_WORKTREE)
11032 if (error->code == GOT_ERR_NOT_WORKTREE)
11033 error = wrap_not_worktree_error(error,
11039 error = get_gitconfig_path(&gitconfig_path);
11042 error = got_repo_open(&repo,
11043 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11044 gitconfig_path, pack_fds);
11048 if (worktree != NULL && !list_backups && !delete_backups) {
11049 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11054 error = get_author(&committer, repo, worktree);
11055 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11058 error = apply_unveil(got_repo_get_path(repo), 0,
11059 worktree ? got_worktree_get_root_path(worktree) : NULL);
11063 if (list_backups || delete_backups) {
11064 error = process_backup_refs(
11065 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11066 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11067 goto done; /* nothing else to do */
11070 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11074 if (histedit_in_progress) {
11075 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11079 error = got_worktree_merge_in_progress(&merge_in_progress,
11083 if (merge_in_progress) {
11084 error = got_error(GOT_ERR_MERGE_BUSY);
11088 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11092 if (abort_rebase) {
11093 if (!rebase_in_progress) {
11094 error = got_error(GOT_ERR_NOT_REBASING);
11097 error = got_worktree_rebase_continue(&resume_commit_id,
11098 &new_base_branch, &tmp_branch, &branch, &fileindex,
11102 printf("Switching work tree to %s\n",
11103 got_ref_get_symref_target(new_base_branch));
11104 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11105 new_base_branch, abort_progress, &upa);
11108 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11109 print_merge_progress_stats(&upa);
11110 goto done; /* nothing else to do */
11113 if (continue_rebase) {
11114 if (!rebase_in_progress) {
11115 error = got_error(GOT_ERR_NOT_REBASING);
11118 error = got_worktree_rebase_continue(&resume_commit_id,
11119 &new_base_branch, &tmp_branch, &branch, &fileindex,
11124 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11125 committer, resume_commit_id, repo);
11129 yca_id = got_object_id_dup(resume_commit_id);
11130 if (yca_id == NULL) {
11131 error = got_error_from_errno("got_object_id_dup");
11135 error = got_ref_open(&branch, repo, argv[0], 0);
11138 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11139 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11140 "will not rebase a branch which lives outside "
11141 "the \"refs/heads/\" reference namespace");
11146 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11150 if (!continue_rebase) {
11151 struct got_object_id *base_commit_id;
11153 error = got_ref_open(&head_ref, repo,
11154 got_worktree_get_head_ref_name(worktree), 0);
11157 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11160 base_commit_id = got_worktree_get_base_commit_id(worktree);
11161 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11162 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11166 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11167 base_commit_id, branch_head_commit_id, 1, repo,
11168 check_cancelled, NULL);
11170 if (error->code == GOT_ERR_ANCESTRY) {
11171 error = got_error_msg(GOT_ERR_ANCESTRY,
11172 "specified branch shares no common "
11173 "ancestry with work tree's branch");
11178 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11180 if (error->code != GOT_ERR_ANCESTRY)
11184 struct got_pathlist_head paths;
11185 printf("%s is already based on %s\n",
11186 got_ref_get_name(branch),
11187 got_worktree_get_head_ref_name(worktree));
11188 error = switch_head_ref(branch, branch_head_commit_id,
11192 error = got_worktree_set_base_commit_id(worktree, repo,
11193 branch_head_commit_id);
11196 TAILQ_INIT(&paths);
11197 error = got_pathlist_append(&paths, "", NULL);
11200 error = got_worktree_checkout_files(worktree,
11201 &paths, repo, update_progress, &upa,
11202 check_cancelled, NULL);
11203 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11206 if (upa.did_something) {
11208 error = got_object_id_str(&id_str,
11209 branch_head_commit_id);
11212 printf("Updated to %s: %s\n",
11213 got_worktree_get_head_ref_name(worktree),
11217 printf("Already up-to-date\n");
11218 print_update_progress_stats(&upa);
11223 commit_id = branch_head_commit_id;
11224 error = got_object_open_as_commit(&commit, repo, commit_id);
11228 parent_ids = got_object_commit_get_parent_ids(commit);
11229 pid = STAILQ_FIRST(parent_ids);
11231 error = collect_commits(&commits, commit_id, &pid->id,
11232 yca_id, got_worktree_get_path_prefix(worktree),
11233 GOT_ERR_REBASE_PATH, repo);
11238 got_object_commit_close(commit);
11241 if (!continue_rebase) {
11242 error = got_worktree_rebase_prepare(&new_base_branch,
11243 &tmp_branch, &fileindex, worktree, branch, repo);
11248 if (STAILQ_EMPTY(&commits)) {
11249 if (continue_rebase) {
11250 error = rebase_complete(worktree, fileindex,
11251 branch, tmp_branch, repo, create_backup);
11254 /* Fast-forward the reference of the branch. */
11255 struct got_object_id *new_head_commit_id;
11257 error = got_ref_resolve(&new_head_commit_id, repo,
11261 error = got_object_id_str(&id_str, new_head_commit_id);
11264 printf("Forwarding %s to commit %s\n",
11265 got_ref_get_name(branch), id_str);
11267 error = got_ref_change_ref(branch,
11268 new_head_commit_id);
11271 /* No backup needed since objects did not change. */
11277 STAILQ_FOREACH(qid, &commits, entry) {
11279 commit_id = &qid->id;
11280 parent_id = pid ? &pid->id : yca_id;
11283 memset(&upa, 0, sizeof(upa));
11284 error = got_worktree_rebase_merge_files(&merged_paths,
11285 worktree, fileindex, parent_id, commit_id, repo,
11286 update_progress, &upa, check_cancelled, NULL);
11290 print_merge_progress_stats(&upa);
11291 if (upa.conflicts > 0 || upa.missing > 0 ||
11292 upa.not_deleted > 0 || upa.unversioned > 0) {
11293 if (upa.conflicts > 0) {
11294 error = show_rebase_merge_conflict(&qid->id,
11299 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11303 error = rebase_commit(&merged_paths, worktree, fileindex,
11304 tmp_branch, committer, commit_id, repo);
11305 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11310 if (upa.conflicts > 0 || upa.missing > 0 ||
11311 upa.not_deleted > 0 || upa.unversioned > 0) {
11312 error = got_worktree_rebase_postpone(worktree, fileindex);
11315 if (upa.conflicts > 0 && upa.missing == 0 &&
11316 upa.not_deleted == 0 && upa.unversioned == 0) {
11317 error = got_error_msg(GOT_ERR_CONFLICTS,
11318 "conflicts must be resolved before rebasing "
11320 } else if (upa.conflicts > 0) {
11321 error = got_error_msg(GOT_ERR_CONFLICTS,
11322 "conflicts must be resolved before rebasing "
11323 "can continue; changes destined for some "
11324 "files were not yet merged and should be "
11325 "merged manually if required before the "
11326 "rebase operation is continued");
11328 error = got_error_msg(GOT_ERR_CONFLICTS,
11329 "changes destined for some files were not "
11330 "yet merged and should be merged manually "
11331 "if required before the rebase operation "
11335 error = rebase_complete(worktree, fileindex, branch,
11336 tmp_branch, repo, create_backup);
11340 free(gitconfig_path);
11341 got_object_id_queue_free(&commits);
11342 free(branch_head_commit_id);
11343 free(resume_commit_id);
11344 free(head_commit_id);
11347 got_object_commit_close(commit);
11349 got_ref_close(branch);
11350 if (new_base_branch)
11351 got_ref_close(new_base_branch);
11353 got_ref_close(tmp_branch);
11355 got_ref_close(head_ref);
11357 got_worktree_close(worktree);
11359 const struct got_error *close_err = got_repo_close(repo);
11364 const struct got_error *pack_err =
11365 got_repo_pack_fds_close(pack_fds);
11373 usage_histedit(void)
11375 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11376 "[branch]\n", getprogname());
11380 #define GOT_HISTEDIT_PICK 'p'
11381 #define GOT_HISTEDIT_EDIT 'e'
11382 #define GOT_HISTEDIT_FOLD 'f'
11383 #define GOT_HISTEDIT_DROP 'd'
11384 #define GOT_HISTEDIT_MESG 'm'
11386 static const struct got_histedit_cmd {
11387 unsigned char code;
11390 } got_histedit_cmds[] = {
11391 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11392 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11393 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11395 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11396 { GOT_HISTEDIT_MESG, "mesg",
11397 "single-line log message for commit above (open editor if empty)" },
11400 struct got_histedit_list_entry {
11401 TAILQ_ENTRY(got_histedit_list_entry) entry;
11402 struct got_object_id *commit_id;
11403 const struct got_histedit_cmd *cmd;
11406 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11408 static const struct got_error *
11409 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11410 FILE *f, struct got_repository *repo)
11412 const struct got_error *err = NULL;
11413 char *logmsg = NULL, *id_str = NULL;
11414 struct got_commit_object *commit = NULL;
11417 err = got_object_open_as_commit(&commit, repo, commit_id);
11421 err = get_short_logmsg(&logmsg, 34, commit);
11425 err = got_object_id_str(&id_str, commit_id);
11429 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11431 err = got_ferror(f, GOT_ERR_IO);
11434 got_object_commit_close(commit);
11440 static const struct got_error *
11441 histedit_write_commit_list(struct got_object_id_queue *commits,
11442 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11443 int edit_only, struct got_repository *repo)
11445 const struct got_error *err = NULL;
11446 struct got_object_qid *qid;
11447 const char *histedit_cmd = NULL;
11449 if (STAILQ_EMPTY(commits))
11450 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11452 STAILQ_FOREACH(qid, commits, entry) {
11453 histedit_cmd = got_histedit_cmds[0].name;
11455 histedit_cmd = "drop";
11456 else if (edit_only)
11457 histedit_cmd = "edit";
11458 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11459 histedit_cmd = "fold";
11460 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11463 if (edit_logmsg_only) {
11464 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11466 err = got_ferror(f, GOT_ERR_IO);
11475 static const struct got_error *
11476 write_cmd_list(FILE *f, const char *branch_name,
11477 struct got_object_id_queue *commits)
11479 const struct got_error *err = NULL;
11483 struct got_object_qid *qid;
11485 qid = STAILQ_FIRST(commits);
11486 err = got_object_id_str(&id_str, &qid->id);
11491 "# Editing the history of branch '%s' starting at\n"
11493 "# Commits will be processed in order from top to "
11494 "bottom of this file.\n", branch_name, id_str);
11496 err = got_ferror(f, GOT_ERR_IO);
11500 n = fprintf(f, "# Available histedit commands:\n");
11502 err = got_ferror(f, GOT_ERR_IO);
11506 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11507 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11508 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11511 err = got_ferror(f, GOT_ERR_IO);
11520 static const struct got_error *
11521 histedit_syntax_error(int lineno)
11523 static char msg[42];
11526 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11528 if (ret < 0 || (size_t)ret >= sizeof(msg))
11529 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11531 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11534 static const struct got_error *
11535 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11536 char *logmsg, struct got_repository *repo)
11538 const struct got_error *err;
11539 struct got_commit_object *folded_commit = NULL;
11540 char *id_str, *folded_logmsg = NULL;
11542 err = got_object_id_str(&id_str, hle->commit_id);
11546 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11550 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11553 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11554 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11555 folded_logmsg) == -1) {
11556 err = got_error_from_errno("asprintf");
11560 got_object_commit_close(folded_commit);
11562 free(folded_logmsg);
11566 static struct got_histedit_list_entry *
11567 get_folded_commits(struct got_histedit_list_entry *hle)
11569 struct got_histedit_list_entry *prev, *folded = NULL;
11571 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11572 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11573 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11574 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11576 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11582 static const struct got_error *
11583 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11584 struct got_repository *repo)
11586 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11587 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11588 const struct got_error *err = NULL;
11589 struct got_commit_object *commit = NULL;
11592 struct got_histedit_list_entry *folded = NULL;
11594 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11598 folded = get_folded_commits(hle);
11600 while (folded != hle) {
11601 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11602 folded = TAILQ_NEXT(folded, entry);
11605 err = append_folded_commit_msg(&new_msg, folded,
11611 folded = TAILQ_NEXT(folded, entry);
11615 err = got_object_id_str(&id_str, hle->commit_id);
11618 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11621 logmsg_len = asprintf(&new_msg,
11622 "%s\n# original log message of commit %s: %s",
11623 logmsg ? logmsg : "", id_str, orig_logmsg);
11624 if (logmsg_len == -1) {
11625 err = got_error_from_errno("asprintf");
11631 err = got_object_id_str(&id_str, hle->commit_id);
11635 err = got_opentemp_named_fd(&logmsg_path, &fd,
11636 GOT_TMPDIR_STR "/got-logmsg", "");
11640 write(fd, logmsg, logmsg_len);
11643 err = get_editor(&editor);
11647 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11650 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11653 hle->logmsg = strdup(new_msg);
11654 if (hle->logmsg == NULL)
11655 err = got_error_from_errno("strdup");
11658 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11659 err = got_error_from_errno2("unlink", logmsg_path);
11665 got_object_commit_close(commit);
11669 static const struct got_error *
11670 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11671 FILE *f, struct got_repository *repo)
11673 const struct got_error *err = NULL;
11674 char *line = NULL, *p, *end;
11677 int lineno = 0, lastcmd = -1;
11678 const struct got_histedit_cmd *cmd;
11679 struct got_object_id *commit_id = NULL;
11680 struct got_histedit_list_entry *hle = NULL;
11683 len = getline(&line, &size, f);
11685 const struct got_error *getline_err;
11688 getline_err = got_error_from_errno("getline");
11689 err = got_ferror(f, getline_err->code);
11694 while (isspace((unsigned char)p[0]))
11696 if (p[0] == '#' || p[0] == '\0') {
11702 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11703 cmd = &got_histedit_cmds[i];
11704 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11705 isspace((unsigned char)p[strlen(cmd->name)])) {
11706 p += strlen(cmd->name);
11709 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11714 if (i == nitems(got_histedit_cmds)) {
11715 err = histedit_syntax_error(lineno);
11718 while (isspace((unsigned char)p[0]))
11720 if (cmd->code == GOT_HISTEDIT_MESG) {
11721 if (lastcmd != GOT_HISTEDIT_PICK &&
11722 lastcmd != GOT_HISTEDIT_EDIT) {
11723 err = got_error(GOT_ERR_HISTEDIT_CMD);
11726 if (p[0] == '\0') {
11727 err = histedit_edit_logmsg(hle, repo);
11731 hle->logmsg = strdup(p);
11732 if (hle->logmsg == NULL) {
11733 err = got_error_from_errno("strdup");
11739 lastcmd = cmd->code;
11743 while (end[0] && !isspace((unsigned char)end[0]))
11747 err = got_object_resolve_id_str(&commit_id, repo, p);
11749 /* override error code */
11750 err = histedit_syntax_error(lineno);
11754 hle = malloc(sizeof(*hle));
11756 err = got_error_from_errno("malloc");
11760 hle->commit_id = commit_id;
11761 hle->logmsg = NULL;
11765 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11766 lastcmd = cmd->code;
11774 static const struct got_error *
11775 histedit_check_script(struct got_histedit_list *histedit_cmds,
11776 struct got_object_id_queue *commits, struct got_repository *repo)
11778 const struct got_error *err = NULL;
11779 struct got_object_qid *qid;
11780 struct got_histedit_list_entry *hle;
11781 static char msg[92];
11784 if (TAILQ_EMPTY(histedit_cmds))
11785 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11786 "histedit script contains no commands");
11787 if (STAILQ_EMPTY(commits))
11788 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11790 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11791 struct got_histedit_list_entry *hle2;
11792 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11795 if (got_object_id_cmp(hle->commit_id,
11796 hle2->commit_id) != 0)
11798 err = got_object_id_str(&id_str, hle->commit_id);
11801 snprintf(msg, sizeof(msg), "commit %s is listed "
11802 "more than once in histedit script", id_str);
11804 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11808 STAILQ_FOREACH(qid, commits, entry) {
11809 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11810 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11814 err = got_object_id_str(&id_str, &qid->id);
11817 snprintf(msg, sizeof(msg),
11818 "commit %s missing from histedit script", id_str);
11820 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11824 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11825 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11826 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11827 "last commit in histedit script cannot be folded");
11832 static const struct got_error *
11833 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11834 const char *path, struct got_object_id_queue *commits,
11835 struct got_repository *repo)
11837 const struct got_error *err = NULL;
11841 err = get_editor(&editor);
11845 if (spawn_editor(editor, path) == -1) {
11846 err = got_error_from_errno("failed spawning editor");
11850 f = fopen(path, "re");
11852 err = got_error_from_errno("fopen");
11855 err = histedit_parse_list(histedit_cmds, f, repo);
11859 err = histedit_check_script(histedit_cmds, commits, repo);
11861 if (f && fclose(f) == EOF && err == NULL)
11862 err = got_error_from_errno("fclose");
11867 static const struct got_error *
11868 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11869 struct got_object_id_queue *, const char *, const char *,
11870 struct got_repository *);
11872 static const struct got_error *
11873 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11874 struct got_object_id_queue *commits, const char *branch_name,
11875 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11876 struct got_repository *repo)
11878 const struct got_error *err;
11882 err = got_opentemp_named(&path, &f, "got-histedit", "");
11886 err = write_cmd_list(f, branch_name, commits);
11890 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11891 fold_only, drop_only, edit_only, repo);
11895 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11897 err = histedit_parse_list(histedit_cmds, f, repo);
11899 if (fclose(f) == EOF) {
11900 err = got_error_from_errno("fclose");
11904 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11906 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11907 err->code != GOT_ERR_HISTEDIT_CMD)
11909 err = histedit_edit_list_retry(histedit_cmds, err,
11910 commits, path, branch_name, repo);
11914 if (f && fclose(f) == EOF && err == NULL)
11915 err = got_error_from_errno("fclose");
11916 if (path && unlink(path) != 0 && err == NULL)
11917 err = got_error_from_errno2("unlink", path);
11922 static const struct got_error *
11923 histedit_save_list(struct got_histedit_list *histedit_cmds,
11924 struct got_worktree *worktree, struct got_repository *repo)
11926 const struct got_error *err = NULL;
11929 struct got_histedit_list_entry *hle;
11930 struct got_commit_object *commit = NULL;
11932 err = got_worktree_get_histedit_script_path(&path, worktree);
11936 f = fopen(path, "we");
11938 err = got_error_from_errno2("fopen", path);
11941 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11942 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11948 int n = fprintf(f, "%c %s\n",
11949 GOT_HISTEDIT_MESG, hle->logmsg);
11951 err = got_ferror(f, GOT_ERR_IO);
11957 if (f && fclose(f) == EOF && err == NULL)
11958 err = got_error_from_errno("fclose");
11961 got_object_commit_close(commit);
11966 histedit_free_list(struct got_histedit_list *histedit_cmds)
11968 struct got_histedit_list_entry *hle;
11970 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11971 TAILQ_REMOVE(histedit_cmds, hle, entry);
11976 static const struct got_error *
11977 histedit_load_list(struct got_histedit_list *histedit_cmds,
11978 const char *path, struct got_repository *repo)
11980 const struct got_error *err = NULL;
11983 f = fopen(path, "re");
11985 err = got_error_from_errno2("fopen", path);
11989 err = histedit_parse_list(histedit_cmds, f, repo);
11991 if (f && fclose(f) == EOF && err == NULL)
11992 err = got_error_from_errno("fclose");
11996 static const struct got_error *
11997 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
11998 const struct got_error *edit_err, struct got_object_id_queue *commits,
11999 const char *path, const char *branch_name, struct got_repository *repo)
12001 const struct got_error *err = NULL, *prev_err = edit_err;
12004 while (resp != 'c' && resp != 'r' && resp != 'a') {
12005 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12006 "or (a)bort: ", getprogname(), prev_err->msg);
12011 histedit_free_list(histedit_cmds);
12012 err = histedit_run_editor(histedit_cmds, path, commits,
12015 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12016 err->code != GOT_ERR_HISTEDIT_CMD)
12023 } else if (resp == 'r') {
12024 histedit_free_list(histedit_cmds);
12025 err = histedit_edit_script(histedit_cmds,
12026 commits, branch_name, 0, 0, 0, 0, repo);
12028 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12029 err->code != GOT_ERR_HISTEDIT_CMD)
12036 } else if (resp == 'a') {
12037 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12040 printf("invalid response '%c'\n", resp);
12046 static const struct got_error *
12047 histedit_complete(struct got_worktree *worktree,
12048 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12049 struct got_reference *branch, struct got_repository *repo)
12051 printf("Switching work tree to %s\n",
12052 got_ref_get_symref_target(branch));
12053 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12057 static const struct got_error *
12058 show_histedit_progress(struct got_commit_object *commit,
12059 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12061 const struct got_error *err;
12062 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12064 err = got_object_id_str(&old_id_str, hle->commit_id);
12069 err = got_object_id_str(&new_id_str, new_id);
12074 old_id_str[12] = '\0';
12076 new_id_str[12] = '\0';
12079 logmsg = strdup(hle->logmsg);
12080 if (logmsg == NULL) {
12081 err = got_error_from_errno("strdup");
12084 trim_logmsg(logmsg, 42);
12086 err = get_short_logmsg(&logmsg, 42, commit);
12091 switch (hle->cmd->code) {
12092 case GOT_HISTEDIT_PICK:
12093 case GOT_HISTEDIT_EDIT:
12094 printf("%s -> %s: %s\n", old_id_str,
12095 new_id_str ? new_id_str : "no-op change", logmsg);
12097 case GOT_HISTEDIT_DROP:
12098 case GOT_HISTEDIT_FOLD:
12099 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12111 static const struct got_error *
12112 histedit_commit(struct got_pathlist_head *merged_paths,
12113 struct got_worktree *worktree, struct got_fileindex *fileindex,
12114 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12115 const char *committer, struct got_repository *repo)
12117 const struct got_error *err;
12118 struct got_commit_object *commit;
12119 struct got_object_id *new_commit_id;
12121 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12122 && hle->logmsg == NULL) {
12123 err = histedit_edit_logmsg(hle, repo);
12128 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12132 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12133 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12134 hle->logmsg, repo);
12136 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12138 err = show_histedit_progress(commit, hle, NULL);
12140 err = show_histedit_progress(commit, hle, new_commit_id);
12141 free(new_commit_id);
12144 got_object_commit_close(commit);
12148 static const struct got_error *
12149 histedit_skip_commit(struct got_histedit_list_entry *hle,
12150 struct got_worktree *worktree, struct got_repository *repo)
12152 const struct got_error *error;
12153 struct got_commit_object *commit;
12155 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12160 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12164 error = show_histedit_progress(commit, hle, NULL);
12165 got_object_commit_close(commit);
12169 static const struct got_error *
12170 check_local_changes(void *arg, unsigned char status,
12171 unsigned char staged_status, const char *path,
12172 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12173 struct got_object_id *commit_id, int dirfd, const char *de_name)
12175 int *have_local_changes = arg;
12178 case GOT_STATUS_ADD:
12179 case GOT_STATUS_DELETE:
12180 case GOT_STATUS_MODIFY:
12181 case GOT_STATUS_CONFLICT:
12182 *have_local_changes = 1;
12183 return got_error(GOT_ERR_CANCELLED);
12188 switch (staged_status) {
12189 case GOT_STATUS_ADD:
12190 case GOT_STATUS_DELETE:
12191 case GOT_STATUS_MODIFY:
12192 *have_local_changes = 1;
12193 return got_error(GOT_ERR_CANCELLED);
12201 static const struct got_error *
12202 cmd_histedit(int argc, char *argv[])
12204 const struct got_error *error = NULL;
12205 struct got_worktree *worktree = NULL;
12206 struct got_fileindex *fileindex = NULL;
12207 struct got_repository *repo = NULL;
12208 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12209 struct got_reference *branch = NULL;
12210 struct got_reference *tmp_branch = NULL;
12211 struct got_object_id *resume_commit_id = NULL;
12212 struct got_object_id *base_commit_id = NULL;
12213 struct got_object_id *head_commit_id = NULL;
12214 struct got_commit_object *commit = NULL;
12215 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12216 struct got_update_progress_arg upa;
12217 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12218 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12219 int list_backups = 0, delete_backups = 0;
12220 const char *edit_script_path = NULL;
12221 struct got_object_id_queue commits;
12222 struct got_pathlist_head merged_paths;
12223 const struct got_object_id_queue *parent_ids;
12224 struct got_object_qid *pid;
12225 struct got_histedit_list histedit_cmds;
12226 struct got_histedit_list_entry *hle;
12227 int *pack_fds = NULL;
12229 STAILQ_INIT(&commits);
12230 TAILQ_INIT(&histedit_cmds);
12231 TAILQ_INIT(&merged_paths);
12232 memset(&upa, 0, sizeof(upa));
12234 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12249 edit_script_path = optarg;
12258 edit_logmsg_only = 1;
12261 delete_backups = 1;
12273 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12274 "unveil", NULL) == -1)
12277 if (abort_edit && continue_edit)
12278 option_conflict('a', 'c');
12279 if (edit_script_path && edit_logmsg_only)
12280 option_conflict('F', 'm');
12281 if (abort_edit && edit_logmsg_only)
12282 option_conflict('a', 'm');
12283 if (continue_edit && edit_logmsg_only)
12284 option_conflict('c', 'm');
12285 if (abort_edit && fold_only)
12286 option_conflict('a', 'f');
12287 if (continue_edit && fold_only)
12288 option_conflict('c', 'f');
12289 if (fold_only && edit_logmsg_only)
12290 option_conflict('f', 'm');
12291 if (edit_script_path && fold_only)
12292 option_conflict('F', 'f');
12293 if (abort_edit && edit_only)
12294 option_conflict('a', 'e');
12295 if (continue_edit && edit_only)
12296 option_conflict('c', 'e');
12297 if (edit_only && edit_logmsg_only)
12298 option_conflict('e', 'm');
12299 if (edit_script_path && edit_only)
12300 option_conflict('F', 'e');
12301 if (fold_only && edit_only)
12302 option_conflict('f', 'e');
12303 if (drop_only && abort_edit)
12304 option_conflict('d', 'a');
12305 if (drop_only && continue_edit)
12306 option_conflict('d', 'c');
12307 if (drop_only && edit_logmsg_only)
12308 option_conflict('d', 'm');
12309 if (drop_only && edit_only)
12310 option_conflict('d', 'e');
12311 if (drop_only && edit_script_path)
12312 option_conflict('d', 'F');
12313 if (drop_only && fold_only)
12314 option_conflict('d', 'f');
12315 if (list_backups) {
12317 option_conflict('l', 'a');
12319 option_conflict('l', 'c');
12320 if (edit_script_path)
12321 option_conflict('l', 'F');
12322 if (edit_logmsg_only)
12323 option_conflict('l', 'm');
12325 option_conflict('l', 'd');
12327 option_conflict('l', 'f');
12329 option_conflict('l', 'e');
12330 if (delete_backups)
12331 option_conflict('l', 'X');
12332 if (argc != 0 && argc != 1)
12334 } else if (delete_backups) {
12336 option_conflict('X', 'a');
12338 option_conflict('X', 'c');
12340 option_conflict('X', 'd');
12341 if (edit_script_path)
12342 option_conflict('X', 'F');
12343 if (edit_logmsg_only)
12344 option_conflict('X', 'm');
12346 option_conflict('X', 'f');
12348 option_conflict('X', 'e');
12350 option_conflict('X', 'l');
12351 if (argc != 0 && argc != 1)
12353 } else if (argc != 0)
12357 * This command cannot apply unveil(2) in all cases because the
12358 * user may choose to run an editor to edit the histedit script
12359 * and to edit individual commit log messages.
12360 * unveil(2) traverses exec(2); if an editor is used we have to
12361 * apply unveil after edit script and log messages have been written.
12362 * XXX TODO: Make use of unveil(2) where possible.
12365 cwd = getcwd(NULL, 0);
12367 error = got_error_from_errno("getcwd");
12371 error = got_repo_pack_fds_open(&pack_fds);
12375 error = got_worktree_open(&worktree, cwd);
12377 if (list_backups || delete_backups) {
12378 if (error->code != GOT_ERR_NOT_WORKTREE)
12381 if (error->code == GOT_ERR_NOT_WORKTREE)
12382 error = wrap_not_worktree_error(error,
12388 if (list_backups || delete_backups) {
12389 error = got_repo_open(&repo,
12390 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12394 error = apply_unveil(got_repo_get_path(repo), 0,
12395 worktree ? got_worktree_get_root_path(worktree) : NULL);
12398 error = process_backup_refs(
12399 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12400 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12401 goto done; /* nothing else to do */
12404 error = get_gitconfig_path(&gitconfig_path);
12407 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12408 gitconfig_path, pack_fds);
12412 if (worktree != NULL && !list_backups && !delete_backups) {
12413 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12418 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12421 if (rebase_in_progress) {
12422 error = got_error(GOT_ERR_REBASING);
12426 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12430 if (merge_in_progress) {
12431 error = got_error(GOT_ERR_MERGE_BUSY);
12435 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12439 if (edit_in_progress && edit_logmsg_only) {
12440 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12441 "histedit operation is in progress in this "
12442 "work tree and must be continued or aborted "
12443 "before the -m option can be used");
12446 if (edit_in_progress && drop_only) {
12447 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12448 "histedit operation is in progress in this "
12449 "work tree and must be continued or aborted "
12450 "before the -d option can be used");
12453 if (edit_in_progress && fold_only) {
12454 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12455 "histedit operation is in progress in this "
12456 "work tree and must be continued or aborted "
12457 "before the -f option can be used");
12460 if (edit_in_progress && edit_only) {
12461 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12462 "histedit operation is in progress in this "
12463 "work tree and must be continued or aborted "
12464 "before the -e option can be used");
12468 if (edit_in_progress && abort_edit) {
12469 error = got_worktree_histedit_continue(&resume_commit_id,
12470 &tmp_branch, &branch, &base_commit_id, &fileindex,
12474 printf("Switching work tree to %s\n",
12475 got_ref_get_symref_target(branch));
12476 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12477 branch, base_commit_id, abort_progress, &upa);
12480 printf("Histedit of %s aborted\n",
12481 got_ref_get_symref_target(branch));
12482 print_merge_progress_stats(&upa);
12483 goto done; /* nothing else to do */
12484 } else if (abort_edit) {
12485 error = got_error(GOT_ERR_NOT_HISTEDIT);
12489 error = get_author(&committer, repo, worktree);
12493 if (continue_edit) {
12496 if (!edit_in_progress) {
12497 error = got_error(GOT_ERR_NOT_HISTEDIT);
12501 error = got_worktree_get_histedit_script_path(&path, worktree);
12505 error = histedit_load_list(&histedit_cmds, path, repo);
12510 error = got_worktree_histedit_continue(&resume_commit_id,
12511 &tmp_branch, &branch, &base_commit_id, &fileindex,
12516 error = got_ref_resolve(&head_commit_id, repo, branch);
12520 error = got_object_open_as_commit(&commit, repo,
12524 parent_ids = got_object_commit_get_parent_ids(commit);
12525 pid = STAILQ_FIRST(parent_ids);
12527 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12530 error = collect_commits(&commits, head_commit_id, &pid->id,
12531 base_commit_id, got_worktree_get_path_prefix(worktree),
12532 GOT_ERR_HISTEDIT_PATH, repo);
12533 got_object_commit_close(commit);
12538 if (edit_in_progress) {
12539 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12543 error = got_ref_open(&branch, repo,
12544 got_worktree_get_head_ref_name(worktree), 0);
12548 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12549 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12550 "will not edit commit history of a branch outside "
12551 "the \"refs/heads/\" reference namespace");
12555 error = got_ref_resolve(&head_commit_id, repo, branch);
12556 got_ref_close(branch);
12561 error = got_object_open_as_commit(&commit, repo,
12565 parent_ids = got_object_commit_get_parent_ids(commit);
12566 pid = STAILQ_FIRST(parent_ids);
12568 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12571 error = collect_commits(&commits, head_commit_id, &pid->id,
12572 got_worktree_get_base_commit_id(worktree),
12573 got_worktree_get_path_prefix(worktree),
12574 GOT_ERR_HISTEDIT_PATH, repo);
12575 got_object_commit_close(commit);
12580 if (STAILQ_EMPTY(&commits)) {
12581 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12585 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12586 &base_commit_id, &fileindex, worktree, repo);
12590 if (edit_script_path) {
12591 error = histedit_load_list(&histedit_cmds,
12592 edit_script_path, repo);
12594 got_worktree_histedit_abort(worktree, fileindex,
12595 repo, branch, base_commit_id,
12596 abort_progress, &upa);
12597 print_merge_progress_stats(&upa);
12601 const char *branch_name;
12602 branch_name = got_ref_get_symref_target(branch);
12603 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12605 error = histedit_edit_script(&histedit_cmds, &commits,
12606 branch_name, edit_logmsg_only, fold_only,
12607 drop_only, edit_only, repo);
12609 got_worktree_histedit_abort(worktree, fileindex,
12610 repo, branch, base_commit_id,
12611 abort_progress, &upa);
12612 print_merge_progress_stats(&upa);
12618 error = histedit_save_list(&histedit_cmds, worktree,
12621 got_worktree_histedit_abort(worktree, fileindex,
12622 repo, branch, base_commit_id,
12623 abort_progress, &upa);
12624 print_merge_progress_stats(&upa);
12630 error = histedit_check_script(&histedit_cmds, &commits, repo);
12634 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12635 if (resume_commit_id) {
12636 if (got_object_id_cmp(hle->commit_id,
12637 resume_commit_id) != 0)
12640 resume_commit_id = NULL;
12641 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12642 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12643 error = histedit_skip_commit(hle, worktree,
12648 struct got_pathlist_head paths;
12649 int have_changes = 0;
12651 TAILQ_INIT(&paths);
12652 error = got_pathlist_append(&paths, "", NULL);
12655 error = got_worktree_status(worktree, &paths,
12656 repo, 0, check_local_changes, &have_changes,
12657 check_cancelled, NULL);
12658 got_pathlist_free(&paths,
12659 GOT_PATHLIST_FREE_NONE);
12661 if (error->code != GOT_ERR_CANCELLED)
12663 if (sigint_received || sigpipe_received)
12666 if (have_changes) {
12667 error = histedit_commit(NULL, worktree,
12668 fileindex, tmp_branch, hle,
12673 error = got_object_open_as_commit(
12674 &commit, repo, hle->commit_id);
12677 error = show_histedit_progress(commit,
12679 got_object_commit_close(commit);
12688 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12689 error = histedit_skip_commit(hle, worktree, repo);
12695 error = got_object_open_as_commit(&commit, repo,
12699 parent_ids = got_object_commit_get_parent_ids(commit);
12700 pid = STAILQ_FIRST(parent_ids);
12702 error = got_worktree_histedit_merge_files(&merged_paths,
12703 worktree, fileindex, &pid->id, hle->commit_id, repo,
12704 update_progress, &upa, check_cancelled, NULL);
12707 got_object_commit_close(commit);
12710 print_merge_progress_stats(&upa);
12711 if (upa.conflicts > 0 || upa.missing > 0 ||
12712 upa.not_deleted > 0 || upa.unversioned > 0) {
12713 if (upa.conflicts > 0) {
12714 error = show_rebase_merge_conflict(
12715 hle->commit_id, repo);
12719 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12723 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12725 error = got_object_id_str(&id_str, hle->commit_id);
12728 printf("Stopping histedit for amending commit %s\n",
12731 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12732 error = got_worktree_histedit_postpone(worktree,
12737 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12738 error = histedit_skip_commit(hle, worktree, repo);
12744 error = histedit_commit(&merged_paths, worktree, fileindex,
12745 tmp_branch, hle, committer, repo);
12746 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12751 if (upa.conflicts > 0 || upa.missing > 0 ||
12752 upa.not_deleted > 0 || upa.unversioned > 0) {
12753 error = got_worktree_histedit_postpone(worktree, fileindex);
12756 if (upa.conflicts > 0 && upa.missing == 0 &&
12757 upa.not_deleted == 0 && upa.unversioned == 0) {
12758 error = got_error_msg(GOT_ERR_CONFLICTS,
12759 "conflicts must be resolved before histedit "
12761 } else if (upa.conflicts > 0) {
12762 error = got_error_msg(GOT_ERR_CONFLICTS,
12763 "conflicts must be resolved before histedit "
12764 "can continue; changes destined for some "
12765 "files were not yet merged and should be "
12766 "merged manually if required before the "
12767 "histedit operation is continued");
12769 error = got_error_msg(GOT_ERR_CONFLICTS,
12770 "changes destined for some files were not "
12771 "yet merged and should be merged manually "
12772 "if required before the histedit operation "
12776 error = histedit_complete(worktree, fileindex, tmp_branch,
12781 free(gitconfig_path);
12782 got_object_id_queue_free(&commits);
12783 histedit_free_list(&histedit_cmds);
12784 free(head_commit_id);
12785 free(base_commit_id);
12786 free(resume_commit_id);
12788 got_object_commit_close(commit);
12790 got_ref_close(branch);
12792 got_ref_close(tmp_branch);
12794 got_worktree_close(worktree);
12796 const struct got_error *close_err = got_repo_close(repo);
12801 const struct got_error *pack_err =
12802 got_repo_pack_fds_close(pack_fds);
12810 usage_integrate(void)
12812 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12816 static const struct got_error *
12817 cmd_integrate(int argc, char *argv[])
12819 const struct got_error *error = NULL;
12820 struct got_repository *repo = NULL;
12821 struct got_worktree *worktree = NULL;
12822 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12823 const char *branch_arg = NULL;
12824 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12825 struct got_fileindex *fileindex = NULL;
12826 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12828 struct got_update_progress_arg upa;
12829 int *pack_fds = NULL;
12831 while ((ch = getopt(argc, argv, "")) != -1) {
12844 branch_arg = argv[0];
12846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12847 "unveil", NULL) == -1)
12850 cwd = getcwd(NULL, 0);
12852 error = got_error_from_errno("getcwd");
12856 error = got_repo_pack_fds_open(&pack_fds);
12860 error = got_worktree_open(&worktree, cwd);
12862 if (error->code == GOT_ERR_NOT_WORKTREE)
12863 error = wrap_not_worktree_error(error, "integrate",
12868 error = check_rebase_or_histedit_in_progress(worktree);
12872 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12877 error = apply_unveil(got_repo_get_path(repo), 0,
12878 got_worktree_get_root_path(worktree));
12882 error = check_merge_in_progress(worktree, repo);
12886 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12887 error = got_error_from_errno("asprintf");
12891 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12892 &base_branch_ref, worktree, refname, repo);
12896 refname = strdup(got_ref_get_name(branch_ref));
12897 if (refname == NULL) {
12898 error = got_error_from_errno("strdup");
12899 got_worktree_integrate_abort(worktree, fileindex, repo,
12900 branch_ref, base_branch_ref);
12903 base_refname = strdup(got_ref_get_name(base_branch_ref));
12904 if (base_refname == NULL) {
12905 error = got_error_from_errno("strdup");
12906 got_worktree_integrate_abort(worktree, fileindex, repo,
12907 branch_ref, base_branch_ref);
12910 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12911 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12912 got_worktree_integrate_abort(worktree, fileindex, repo,
12913 branch_ref, base_branch_ref);
12917 error = got_ref_resolve(&commit_id, repo, branch_ref);
12921 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12925 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12926 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12927 "specified branch has already been integrated");
12928 got_worktree_integrate_abort(worktree, fileindex, repo,
12929 branch_ref, base_branch_ref);
12933 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12935 if (error->code == GOT_ERR_ANCESTRY)
12936 error = got_error(GOT_ERR_REBASE_REQUIRED);
12937 got_worktree_integrate_abort(worktree, fileindex, repo,
12938 branch_ref, base_branch_ref);
12942 memset(&upa, 0, sizeof(upa));
12943 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12944 branch_ref, base_branch_ref, update_progress, &upa,
12945 check_cancelled, NULL);
12949 printf("Integrated %s into %s\n", refname, base_refname);
12950 print_update_progress_stats(&upa);
12953 const struct got_error *close_err = got_repo_close(repo);
12958 got_worktree_close(worktree);
12960 const struct got_error *pack_err =
12961 got_repo_pack_fds_close(pack_fds);
12966 free(base_commit_id);
12969 free(base_refname);
12976 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12980 static const struct got_error *
12981 cmd_merge(int argc, char *argv[])
12983 const struct got_error *error = NULL;
12984 struct got_worktree *worktree = NULL;
12985 struct got_repository *repo = NULL;
12986 struct got_fileindex *fileindex = NULL;
12987 char *cwd = NULL, *id_str = NULL, *author = NULL;
12988 struct got_reference *branch = NULL, *wt_branch = NULL;
12989 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12990 struct got_object_id *wt_branch_tip = NULL;
12991 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12992 int interrupt_merge = 0;
12993 struct got_update_progress_arg upa;
12994 struct got_object_id *merge_commit_id = NULL;
12995 char *branch_name = NULL;
12996 int *pack_fds = NULL;
12998 memset(&upa, 0, sizeof(upa));
13000 while ((ch = getopt(argc, argv, "acn")) != -1) {
13006 continue_merge = 1;
13009 interrupt_merge = 1;
13021 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13022 "unveil", NULL) == -1)
13026 if (abort_merge && continue_merge)
13027 option_conflict('a', 'c');
13028 if (abort_merge || continue_merge) {
13031 } else if (argc != 1)
13034 cwd = getcwd(NULL, 0);
13036 error = got_error_from_errno("getcwd");
13040 error = got_repo_pack_fds_open(&pack_fds);
13044 error = got_worktree_open(&worktree, cwd);
13046 if (error->code == GOT_ERR_NOT_WORKTREE)
13047 error = wrap_not_worktree_error(error,
13052 error = got_repo_open(&repo,
13053 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13058 if (worktree != NULL) {
13059 error = worktree_has_logmsg_ref("merge", worktree, repo);
13064 error = apply_unveil(got_repo_get_path(repo), 0,
13065 worktree ? got_worktree_get_root_path(worktree) : NULL);
13069 error = check_rebase_or_histedit_in_progress(worktree);
13073 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13079 if (!merge_in_progress) {
13080 error = got_error(GOT_ERR_NOT_MERGING);
13083 error = got_worktree_merge_continue(&branch_name,
13084 &branch_tip, &fileindex, worktree, repo);
13087 error = got_worktree_merge_abort(worktree, fileindex, repo,
13088 abort_progress, &upa);
13091 printf("Merge of %s aborted\n", branch_name);
13092 goto done; /* nothing else to do */
13095 error = get_author(&author, repo, worktree);
13099 if (continue_merge) {
13100 if (!merge_in_progress) {
13101 error = got_error(GOT_ERR_NOT_MERGING);
13104 error = got_worktree_merge_continue(&branch_name,
13105 &branch_tip, &fileindex, worktree, repo);
13109 error = got_ref_open(&branch, repo, argv[0], 0);
13112 branch_name = strdup(got_ref_get_name(branch));
13113 if (branch_name == NULL) {
13114 error = got_error_from_errno("strdup");
13117 error = got_ref_resolve(&branch_tip, repo, branch);
13122 error = got_ref_open(&wt_branch, repo,
13123 got_worktree_get_head_ref_name(worktree), 0);
13126 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13129 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13130 wt_branch_tip, branch_tip, 0, repo,
13131 check_cancelled, NULL);
13132 if (error && error->code != GOT_ERR_ANCESTRY)
13135 if (!continue_merge) {
13136 error = check_path_prefix(wt_branch_tip, branch_tip,
13137 got_worktree_get_path_prefix(worktree),
13138 GOT_ERR_MERGE_PATH, repo);
13142 error = check_same_branch(wt_branch_tip, branch,
13145 if (error->code != GOT_ERR_ANCESTRY)
13149 static char msg[512];
13150 snprintf(msg, sizeof(msg),
13151 "cannot create a merge commit because "
13152 "%s is based on %s; %s can be integrated "
13153 "with 'got integrate' instead", branch_name,
13154 got_worktree_get_head_ref_name(worktree),
13156 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13160 error = got_worktree_merge_prepare(&fileindex, worktree,
13165 error = got_worktree_merge_branch(worktree, fileindex,
13166 yca_id, branch_tip, repo, update_progress, &upa,
13167 check_cancelled, NULL);
13170 print_merge_progress_stats(&upa);
13171 if (!upa.did_something) {
13172 error = got_worktree_merge_abort(worktree, fileindex,
13173 repo, abort_progress, &upa);
13176 printf("Already up-to-date\n");
13181 if (interrupt_merge) {
13182 error = got_worktree_merge_postpone(worktree, fileindex);
13185 printf("Merge of %s interrupted on request\n", branch_name);
13186 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13187 upa.not_deleted > 0 || upa.unversioned > 0) {
13188 error = got_worktree_merge_postpone(worktree, fileindex);
13191 if (upa.conflicts > 0 && upa.missing == 0 &&
13192 upa.not_deleted == 0 && upa.unversioned == 0) {
13193 error = got_error_msg(GOT_ERR_CONFLICTS,
13194 "conflicts must be resolved before merging "
13196 } else if (upa.conflicts > 0) {
13197 error = got_error_msg(GOT_ERR_CONFLICTS,
13198 "conflicts must be resolved before merging "
13199 "can continue; changes destined for some "
13200 "files were not yet merged and "
13201 "should be merged manually if required before the "
13202 "merge operation is continued");
13204 error = got_error_msg(GOT_ERR_CONFLICTS,
13205 "changes destined for some "
13206 "files were not yet merged and should be "
13207 "merged manually if required before the "
13208 "merge operation is continued");
13212 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13213 fileindex, author, NULL, 1, branch_tip, branch_name,
13214 repo, continue_merge ? print_status : NULL, NULL);
13217 error = got_worktree_merge_complete(worktree, fileindex, repo);
13220 error = got_object_id_str(&id_str, merge_commit_id);
13223 printf("Merged %s into %s: %s\n", branch_name,
13224 got_worktree_get_head_ref_name(worktree),
13230 free(merge_commit_id);
13236 got_ref_close(branch);
13238 got_ref_close(wt_branch);
13240 got_worktree_close(worktree);
13242 const struct got_error *close_err = got_repo_close(repo);
13247 const struct got_error *pack_err =
13248 got_repo_pack_fds_close(pack_fds);
13258 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13259 "[path ...]\n", getprogname());
13263 static const struct got_error *
13264 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13265 const char *path, struct got_object_id *blob_id,
13266 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13267 int dirfd, const char *de_name)
13269 const struct got_error *err = NULL;
13270 char *id_str = NULL;
13272 if (staged_status != GOT_STATUS_ADD &&
13273 staged_status != GOT_STATUS_MODIFY &&
13274 staged_status != GOT_STATUS_DELETE)
13277 if (staged_status == GOT_STATUS_ADD ||
13278 staged_status == GOT_STATUS_MODIFY)
13279 err = got_object_id_str(&id_str, staged_blob_id);
13281 err = got_object_id_str(&id_str, blob_id);
13285 printf("%s %c %s\n", id_str, staged_status, path);
13290 static const struct got_error *
13291 cmd_stage(int argc, char *argv[])
13293 const struct got_error *error = NULL;
13294 struct got_repository *repo = NULL;
13295 struct got_worktree *worktree = NULL;
13297 struct got_pathlist_head paths;
13298 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13299 FILE *patch_script_file = NULL;
13300 const char *patch_script_path = NULL;
13301 struct choose_patch_arg cpa;
13302 int *pack_fds = NULL;
13304 TAILQ_INIT(&paths);
13306 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13309 patch_script_path = optarg;
13318 allow_bad_symlinks = 1;
13330 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13331 "unveil", NULL) == -1)
13334 if (list_stage && (pflag || patch_script_path))
13335 errx(1, "-l option cannot be used with other options");
13336 if (patch_script_path && !pflag)
13337 errx(1, "-F option can only be used together with -p option");
13339 cwd = getcwd(NULL, 0);
13341 error = got_error_from_errno("getcwd");
13345 error = got_repo_pack_fds_open(&pack_fds);
13349 error = got_worktree_open(&worktree, cwd);
13351 if (error->code == GOT_ERR_NOT_WORKTREE)
13352 error = wrap_not_worktree_error(error, "stage", cwd);
13356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13361 if (patch_script_path) {
13362 patch_script_file = fopen(patch_script_path, "re");
13363 if (patch_script_file == NULL) {
13364 error = got_error_from_errno2("fopen",
13365 patch_script_path);
13369 error = apply_unveil(got_repo_get_path(repo), 0,
13370 got_worktree_get_root_path(worktree));
13374 error = check_merge_in_progress(worktree, repo);
13378 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13383 error = got_worktree_status(worktree, &paths, repo, 0,
13384 print_stage, NULL, check_cancelled, NULL);
13386 cpa.patch_script_file = patch_script_file;
13387 cpa.action = "stage";
13388 error = got_worktree_stage(worktree, &paths,
13389 pflag ? NULL : print_status, NULL,
13390 pflag ? choose_patch : NULL, &cpa,
13391 allow_bad_symlinks, repo);
13394 if (patch_script_file && fclose(patch_script_file) == EOF &&
13396 error = got_error_from_errno2("fclose", patch_script_path);
13398 const struct got_error *close_err = got_repo_close(repo);
13403 got_worktree_close(worktree);
13405 const struct got_error *pack_err =
13406 got_repo_pack_fds_close(pack_fds);
13410 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13416 usage_unstage(void)
13418 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13419 "[path ...]\n", getprogname());
13424 static const struct got_error *
13425 cmd_unstage(int argc, char *argv[])
13427 const struct got_error *error = NULL;
13428 struct got_repository *repo = NULL;
13429 struct got_worktree *worktree = NULL;
13431 struct got_pathlist_head paths;
13433 struct got_update_progress_arg upa;
13434 FILE *patch_script_file = NULL;
13435 const char *patch_script_path = NULL;
13436 struct choose_patch_arg cpa;
13437 int *pack_fds = NULL;
13439 TAILQ_INIT(&paths);
13441 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13444 patch_script_path = optarg;
13459 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13460 "unveil", NULL) == -1)
13463 if (patch_script_path && !pflag)
13464 errx(1, "-F option can only be used together with -p option");
13466 cwd = getcwd(NULL, 0);
13468 error = got_error_from_errno("getcwd");
13472 error = got_repo_pack_fds_open(&pack_fds);
13476 error = got_worktree_open(&worktree, cwd);
13478 if (error->code == GOT_ERR_NOT_WORKTREE)
13479 error = wrap_not_worktree_error(error, "unstage", cwd);
13483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13488 if (patch_script_path) {
13489 patch_script_file = fopen(patch_script_path, "re");
13490 if (patch_script_file == NULL) {
13491 error = got_error_from_errno2("fopen",
13492 patch_script_path);
13497 error = apply_unveil(got_repo_get_path(repo), 0,
13498 got_worktree_get_root_path(worktree));
13502 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13506 cpa.patch_script_file = patch_script_file;
13507 cpa.action = "unstage";
13508 memset(&upa, 0, sizeof(upa));
13509 error = got_worktree_unstage(worktree, &paths, update_progress,
13510 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13512 print_merge_progress_stats(&upa);
13514 if (patch_script_file && fclose(patch_script_file) == EOF &&
13516 error = got_error_from_errno2("fclose", patch_script_path);
13518 const struct got_error *close_err = got_repo_close(repo);
13523 got_worktree_close(worktree);
13525 const struct got_error *pack_err =
13526 got_repo_pack_fds_close(pack_fds);
13530 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13538 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13539 "arg ...\n", getprogname());
13543 static const struct got_error *
13544 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13546 const struct got_error *err;
13547 struct got_blob_object *blob;
13550 fd = got_opentempfd();
13552 return got_error_from_errno("got_opentempfd");
13554 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13558 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13560 if (fd != -1 && close(fd) == -1 && err == NULL)
13561 err = got_error_from_errno("close");
13563 got_object_blob_close(blob);
13567 static const struct got_error *
13568 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13570 const struct got_error *err;
13571 struct got_tree_object *tree;
13574 err = got_object_open_as_tree(&tree, repo, id);
13578 nentries = got_object_tree_get_nentries(tree);
13579 for (i = 0; i < nentries; i++) {
13580 struct got_tree_entry *te;
13582 if (sigint_received || sigpipe_received)
13584 te = got_object_tree_get_entry(tree, i);
13585 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13588 fprintf(outfile, "%s %.7o %s\n", id_str,
13589 got_tree_entry_get_mode(te),
13590 got_tree_entry_get_name(te));
13594 got_object_tree_close(tree);
13598 static const struct got_error *
13599 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13601 const struct got_error *err;
13602 struct got_commit_object *commit;
13603 const struct got_object_id_queue *parent_ids;
13604 struct got_object_qid *pid;
13605 char *id_str = NULL;
13606 const char *logmsg = NULL;
13609 err = got_object_open_as_commit(&commit, repo, id);
13613 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13617 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13618 parent_ids = got_object_commit_get_parent_ids(commit);
13619 fprintf(outfile, "numparents %d\n",
13620 got_object_commit_get_nparents(commit));
13621 STAILQ_FOREACH(pid, parent_ids, entry) {
13623 err = got_object_id_str(&pid_str, &pid->id);
13626 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13629 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13630 got_object_commit_get_author_gmtoff(commit));
13631 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13632 got_object_commit_get_author(commit),
13633 (long long)got_object_commit_get_author_time(commit),
13636 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13637 got_object_commit_get_committer_gmtoff(commit));
13638 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13639 got_object_commit_get_committer(commit),
13640 (long long)got_object_commit_get_committer_time(commit),
13643 logmsg = got_object_commit_get_logmsg_raw(commit);
13644 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13645 fprintf(outfile, "%s", logmsg);
13648 got_object_commit_close(commit);
13652 static const struct got_error *
13653 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13655 const struct got_error *err;
13656 struct got_tag_object *tag;
13657 char *id_str = NULL;
13658 const char *tagmsg = NULL;
13661 err = got_object_open_as_tag(&tag, repo, id);
13665 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13669 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13671 switch (got_object_tag_get_object_type(tag)) {
13672 case GOT_OBJ_TYPE_BLOB:
13673 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13674 GOT_OBJ_LABEL_BLOB);
13676 case GOT_OBJ_TYPE_TREE:
13677 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13678 GOT_OBJ_LABEL_TREE);
13680 case GOT_OBJ_TYPE_COMMIT:
13681 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13682 GOT_OBJ_LABEL_COMMIT);
13684 case GOT_OBJ_TYPE_TAG:
13685 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13686 GOT_OBJ_LABEL_TAG);
13692 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13693 got_object_tag_get_name(tag));
13695 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13696 got_object_tag_get_tagger_gmtoff(tag));
13697 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13698 got_object_tag_get_tagger(tag),
13699 (long long)got_object_tag_get_tagger_time(tag),
13702 tagmsg = got_object_tag_get_message(tag);
13703 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13704 fprintf(outfile, "%s", tagmsg);
13707 got_object_tag_close(tag);
13711 static const struct got_error *
13712 cmd_cat(int argc, char *argv[])
13714 const struct got_error *error;
13715 struct got_repository *repo = NULL;
13716 struct got_worktree *worktree = NULL;
13717 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13718 const char *commit_id_str = NULL;
13719 struct got_object_id *id = NULL, *commit_id = NULL;
13720 struct got_commit_object *commit = NULL;
13721 int ch, obj_type, i, force_path = 0;
13722 struct got_reflist_head refs;
13723 int *pack_fds = NULL;
13728 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13733 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13736 commit_id_str = optarg;
13742 repo_path = realpath(optarg, NULL);
13743 if (repo_path == NULL)
13744 return got_error_from_errno2("realpath",
13746 got_path_strip_trailing_slashes(repo_path);
13757 cwd = getcwd(NULL, 0);
13759 error = got_error_from_errno("getcwd");
13763 error = got_repo_pack_fds_open(&pack_fds);
13767 if (repo_path == NULL) {
13768 error = got_worktree_open(&worktree, cwd);
13769 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13772 repo_path = strdup(
13773 got_worktree_get_repo_path(worktree));
13774 if (repo_path == NULL) {
13775 error = got_error_from_errno("strdup");
13779 /* Release work tree lock. */
13780 got_worktree_close(worktree);
13785 if (repo_path == NULL) {
13786 repo_path = strdup(cwd);
13787 if (repo_path == NULL)
13788 return got_error_from_errno("strdup");
13791 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13796 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13800 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13804 if (commit_id_str == NULL)
13805 commit_id_str = GOT_REF_HEAD;
13806 error = got_repo_match_object_id(&commit_id, NULL,
13807 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13811 error = got_object_open_as_commit(&commit, repo, commit_id);
13815 for (i = 0; i < argc; i++) {
13817 error = got_object_id_by_path(&id, repo, commit,
13822 error = got_repo_match_object_id(&id, &label, argv[i],
13823 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13826 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13827 error->code != GOT_ERR_NOT_REF)
13829 error = got_object_id_by_path(&id, repo,
13836 error = got_object_get_type(&obj_type, repo, id);
13840 switch (obj_type) {
13841 case GOT_OBJ_TYPE_BLOB:
13842 error = cat_blob(id, repo, stdout);
13844 case GOT_OBJ_TYPE_TREE:
13845 error = cat_tree(id, repo, stdout);
13847 case GOT_OBJ_TYPE_COMMIT:
13848 error = cat_commit(id, repo, stdout);
13850 case GOT_OBJ_TYPE_TAG:
13851 error = cat_tag(id, repo, stdout);
13854 error = got_error(GOT_ERR_OBJ_TYPE);
13869 got_object_commit_close(commit);
13871 got_worktree_close(worktree);
13873 const struct got_error *close_err = got_repo_close(repo);
13878 const struct got_error *pack_err =
13879 got_repo_pack_fds_close(pack_fds);
13884 got_ref_list_free(&refs);
13891 fprintf(stderr, "usage: %s info [path ...]\n",
13896 static const struct got_error *
13897 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13898 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13899 struct got_object_id *commit_id)
13901 const struct got_error *err = NULL;
13902 char *id_str = NULL;
13904 struct tm mytm, *tm;
13905 struct got_pathlist_head *paths = arg;
13906 struct got_pathlist_entry *pe;
13909 * Clear error indication from any of the path arguments which
13910 * would cause this file index entry to be displayed.
13912 TAILQ_FOREACH(pe, paths, entry) {
13913 if (got_path_cmp(path, pe->path, strlen(path),
13914 pe->path_len) == 0 ||
13915 got_path_is_child(path, pe->path, pe->path_len))
13916 pe->data = NULL; /* no error */
13919 printf(GOT_COMMIT_SEP_STR);
13921 printf("symlink: %s\n", path);
13922 else if (S_ISREG(mode)) {
13923 printf("file: %s\n", path);
13924 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13925 } else if (S_ISDIR(mode))
13926 printf("directory: %s\n", path);
13928 printf("something: %s\n", path);
13930 tm = localtime_r(&mtime, &mytm);
13933 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13934 return got_error(GOT_ERR_NO_SPACE);
13935 printf("timestamp: %s\n", datebuf);
13938 err = got_object_id_str(&id_str, blob_id);
13941 printf("based on blob: %s\n", id_str);
13945 if (staged_blob_id) {
13946 err = got_object_id_str(&id_str, staged_blob_id);
13949 printf("based on staged blob: %s\n", id_str);
13954 err = got_object_id_str(&id_str, commit_id);
13957 printf("based on commit: %s\n", id_str);
13964 static const struct got_error *
13965 cmd_info(int argc, char *argv[])
13967 const struct got_error *error = NULL;
13968 struct got_worktree *worktree = NULL;
13969 char *cwd = NULL, *id_str = NULL;
13970 struct got_pathlist_head paths;
13971 char *uuidstr = NULL;
13972 int ch, show_files = 0;
13974 TAILQ_INIT(&paths);
13976 while ((ch = getopt(argc, argv, "")) != -1) {
13988 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13992 cwd = getcwd(NULL, 0);
13994 error = got_error_from_errno("getcwd");
13998 error = got_worktree_open(&worktree, cwd);
14000 if (error->code == GOT_ERR_NOT_WORKTREE)
14001 error = wrap_not_worktree_error(error, "info", cwd);
14006 /* Remove "wpath cpath proc exec sendfd" promises. */
14007 if (pledge("stdio rpath flock unveil", NULL) == -1)
14010 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14015 error = get_worktree_paths_from_argv(&paths, argc, argv,
14022 error = got_object_id_str(&id_str,
14023 got_worktree_get_base_commit_id(worktree));
14027 error = got_worktree_get_uuid(&uuidstr, worktree);
14031 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14032 printf("work tree base commit: %s\n", id_str);
14033 printf("work tree path prefix: %s\n",
14034 got_worktree_get_path_prefix(worktree));
14035 printf("work tree branch reference: %s\n",
14036 got_worktree_get_head_ref_name(worktree));
14037 printf("work tree UUID: %s\n", uuidstr);
14038 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14041 struct got_pathlist_entry *pe;
14042 TAILQ_FOREACH(pe, &paths, entry) {
14043 if (pe->path_len == 0)
14046 * Assume this path will fail. This will be corrected
14047 * in print_path_info() in case the path does suceeed.
14049 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14051 error = got_worktree_path_info(worktree, &paths,
14052 print_path_info, &paths, check_cancelled, NULL);
14055 TAILQ_FOREACH(pe, &paths, entry) {
14056 if (pe->data != NULL) {
14057 const struct got_error *perr;
14060 error = got_error_fmt(perr->code, "%s",
14068 got_worktree_close(worktree);
14069 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);