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, NULL,
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, have_bflag = 0;
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,
2328 error = got_pathlist_append(&wanted_refs,
2334 repo_path = realpath(optarg, NULL);
2335 if (repo_path == NULL)
2336 return got_error_from_errno2("realpath",
2338 got_path_strip_trailing_slashes(repo_path);
2346 else if (verbosity < 3)
2360 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2361 option_conflict('a', 'b');
2362 if (list_refs_only) {
2363 if (!TAILQ_EMPTY(&wanted_branches))
2364 option_conflict('l', 'b');
2365 if (fetch_all_branches)
2366 option_conflict('l', 'a');
2368 option_conflict('l', 'd');
2370 option_conflict('l', 'X');
2372 if (delete_remote) {
2373 if (fetch_all_branches)
2374 option_conflict('X', 'a');
2375 if (!TAILQ_EMPTY(&wanted_branches))
2376 option_conflict('X', 'b');
2378 option_conflict('X', 'd');
2380 option_conflict('X', 't');
2381 if (!TAILQ_EMPTY(&wanted_refs))
2382 option_conflict('X', 'R');
2387 errx(1, "-X option requires a remote name");
2388 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2389 } else if (argc == 1)
2390 remote_name = argv[0];
2394 cwd = getcwd(NULL, 0);
2396 error = got_error_from_errno("getcwd");
2400 error = got_repo_pack_fds_open(&pack_fds);
2404 if (repo_path == NULL) {
2405 error = got_worktree_open(&worktree, cwd);
2406 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2412 strdup(got_worktree_get_repo_path(worktree));
2413 if (repo_path == NULL)
2414 error = got_error_from_errno("strdup");
2418 repo_path = strdup(cwd);
2419 if (repo_path == NULL) {
2420 error = got_error_from_errno("strdup");
2426 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2430 if (delete_remote) {
2431 error = delete_refs_for_remote(repo, remote_name);
2432 goto done; /* nothing else to do */
2436 worktree_conf = got_worktree_get_gotconfig(worktree);
2437 if (worktree_conf) {
2438 got_gotconfig_get_remotes(&nremotes, &remotes,
2440 for (i = 0; i < nremotes; i++) {
2441 if (strcmp(remotes[i].name, remote_name) == 0) {
2442 remote = &remotes[i];
2448 if (remote == NULL) {
2449 repo_conf = got_repo_get_gotconfig(repo);
2451 got_gotconfig_get_remotes(&nremotes, &remotes,
2453 for (i = 0; i < nremotes; i++) {
2454 if (strcmp(remotes[i].name, remote_name) == 0) {
2455 remote = &remotes[i];
2461 if (remote == NULL) {
2462 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2463 for (i = 0; i < nremotes; i++) {
2464 if (strcmp(remotes[i].name, remote_name) == 0) {
2465 remote = &remotes[i];
2470 if (remote == NULL) {
2471 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2475 if (TAILQ_EMPTY(&wanted_branches)) {
2476 if (!fetch_all_branches)
2477 fetch_all_branches = remote->fetch_all_branches;
2478 for (i = 0; i < remote->nfetch_branches; i++) {
2479 error = got_pathlist_append(&wanted_branches,
2480 remote->fetch_branches[i], NULL);
2485 if (TAILQ_EMPTY(&wanted_refs)) {
2486 for (i = 0; i < remote->nfetch_refs; i++) {
2487 error = got_pathlist_append(&wanted_refs,
2488 remote->fetch_refs[i], NULL);
2494 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2495 &repo_name, remote->fetch_url);
2499 if (strcmp(proto, "git") == 0) {
2501 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2502 "sendfd dns inet unveil", NULL) == -1)
2505 } else if (strcmp(proto, "git+ssh") == 0 ||
2506 strcmp(proto, "ssh") == 0) {
2508 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2509 "sendfd unveil", NULL) == -1)
2512 } else if (strcmp(proto, "http") == 0 ||
2513 strcmp(proto, "git+http") == 0) {
2514 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2517 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2521 error = got_dial_apply_unveil(proto);
2525 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2529 if (verbosity >= 0) {
2530 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2531 remote->name, proto, host,
2532 port ? ":" : "", port ? port : "",
2533 *server_path == '/' ? "" : "/", server_path);
2536 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2537 server_path, verbosity);
2541 fpa.last_scaled_size[0] = '\0';
2542 fpa.last_p_indexed = -1;
2543 fpa.last_p_resolved = -1;
2544 fpa.verbosity = verbosity;
2546 fpa.create_configs = 0;
2547 fpa.configs_created = 0;
2548 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2549 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2550 remote->mirror_references, fetch_all_branches, &wanted_branches,
2551 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2552 (worktree != NULL && !have_bflag) ?
2553 got_worktree_get_head_ref_name(worktree) : NULL,
2554 fetch_progress, &fpa);
2558 if (list_refs_only) {
2559 error = list_remote_refs(&symrefs, &refs);
2563 if (pack_hash == NULL) {
2565 printf("Already up-to-date\n");
2566 } else if (verbosity >= 0) {
2567 error = got_object_id_str(&id_str, pack_hash);
2570 printf("\nFetched %s.pack\n", id_str);
2575 /* Update references provided with the pack file. */
2576 TAILQ_FOREACH(pe, &refs, entry) {
2577 const char *refname = pe->path;
2578 struct got_object_id *id = pe->data;
2579 struct got_reference *ref;
2580 char *remote_refname;
2582 if (is_wanted_ref(&wanted_refs, refname) &&
2583 !remote->mirror_references) {
2584 error = update_wanted_ref(refname, id,
2585 remote->name, verbosity, repo);
2591 if (remote->mirror_references ||
2592 strncmp("refs/tags/", refname, 10) == 0) {
2593 error = got_ref_open(&ref, repo, refname, 1);
2595 if (error->code != GOT_ERR_NOT_REF)
2597 error = create_ref(refname, id, verbosity,
2602 error = update_ref(ref, id, replace_tags,
2604 unlock_err = got_ref_unlock(ref);
2605 if (unlock_err && error == NULL)
2611 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2612 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2613 remote_name, refname + 11) == -1) {
2614 error = got_error_from_errno("asprintf");
2618 error = got_ref_open(&ref, repo, remote_refname, 1);
2620 if (error->code != GOT_ERR_NOT_REF)
2622 error = create_ref(remote_refname, id,
2627 error = update_ref(ref, id, replace_tags,
2629 unlock_err = got_ref_unlock(ref);
2630 if (unlock_err && error == NULL)
2637 /* Also create a local branch if none exists yet. */
2638 error = got_ref_open(&ref, repo, refname, 1);
2640 if (error->code != GOT_ERR_NOT_REF)
2642 error = create_ref(refname, id, verbosity,
2647 unlock_err = got_ref_unlock(ref);
2648 if (unlock_err && error == NULL)
2655 error = delete_missing_refs(&refs, &symrefs, remote,
2661 if (!remote->mirror_references) {
2662 /* Update remote HEAD reference if the server provided one. */
2663 TAILQ_FOREACH(pe, &symrefs, entry) {
2664 struct got_reference *target_ref;
2665 const char *refname = pe->path;
2666 const char *target = pe->data;
2667 char *remote_refname = NULL, *remote_target = NULL;
2669 if (strcmp(refname, GOT_REF_HEAD) != 0)
2672 if (strncmp("refs/heads/", target, 11) != 0)
2675 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2676 remote->name, refname) == -1) {
2677 error = got_error_from_errno("asprintf");
2680 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2681 remote->name, target + 11) == -1) {
2682 error = got_error_from_errno("asprintf");
2683 free(remote_refname);
2687 error = got_ref_open(&target_ref, repo, remote_target,
2690 free(remote_refname);
2691 free(remote_target);
2692 if (error->code == GOT_ERR_NOT_REF) {
2698 error = update_symref(remote_refname, target_ref,
2700 free(remote_refname);
2701 free(remote_target);
2702 got_ref_close(target_ref);
2709 if (kill(fetchpid, SIGTERM) == -1)
2710 error = got_error_from_errno("kill");
2711 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2712 error = got_error_from_errno("waitpid");
2714 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2715 error = got_error_from_errno("close");
2717 const struct got_error *close_err = got_repo_close(repo);
2722 got_worktree_close(worktree);
2724 const struct got_error *pack_err =
2725 got_repo_pack_fds_close(pack_fds);
2729 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2730 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2731 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2732 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2747 usage_checkout(void)
2749 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2750 "[-p path-prefix] repository-path [work-tree-path]\n",
2756 show_worktree_base_ref_warning(void)
2758 fprintf(stderr, "%s: warning: could not create a reference "
2759 "to the work tree's base commit; the commit could be "
2760 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2761 "repository writable and running 'got update' will prevent this\n",
2765 struct got_checkout_progress_arg {
2766 const char *worktree_path;
2767 int had_base_commit_ref_error;
2771 static const struct got_error *
2772 checkout_progress(void *arg, unsigned char status, const char *path)
2774 struct got_checkout_progress_arg *a = arg;
2776 /* Base commit bump happens silently. */
2777 if (status == GOT_STATUS_BUMP_BASE)
2780 if (status == GOT_STATUS_BASE_REF_ERR) {
2781 a->had_base_commit_ref_error = 1;
2785 while (path[0] == '/')
2788 if (a->verbosity >= 0)
2789 printf("%c %s/%s\n", status, a->worktree_path, path);
2794 static const struct got_error *
2795 check_cancelled(void *arg)
2797 if (sigint_received || sigpipe_received)
2798 return got_error(GOT_ERR_CANCELLED);
2802 static const struct got_error *
2803 check_linear_ancestry(struct got_object_id *commit_id,
2804 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2805 struct got_repository *repo)
2807 const struct got_error *err = NULL;
2808 struct got_object_id *yca_id;
2810 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2811 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2816 return got_error(GOT_ERR_ANCESTRY);
2819 * Require a straight line of history between the target commit
2820 * and the work tree's base commit.
2822 * Non-linear situations such as this require a rebase:
2824 * (commit) D F (base_commit)
2832 * 'got update' only handles linear cases:
2833 * Update forwards in time: A (base/yca) - B - C - D (commit)
2834 * Update backwards in time: D (base) - C - B - A (commit/yca)
2836 if (allow_forwards_in_time_only) {
2837 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2838 return got_error(GOT_ERR_ANCESTRY);
2839 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2840 got_object_id_cmp(base_commit_id, yca_id) != 0)
2841 return got_error(GOT_ERR_ANCESTRY);
2847 static const struct got_error *
2848 check_same_branch(struct got_object_id *commit_id,
2849 struct got_reference *head_ref, struct got_object_id *yca_id,
2850 struct got_repository *repo)
2852 const struct got_error *err = NULL;
2853 struct got_commit_graph *graph = NULL;
2854 struct got_object_id *head_commit_id = NULL;
2855 int is_same_branch = 0;
2857 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2861 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2865 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2870 err = got_commit_graph_open(&graph, "/", 1);
2874 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2875 check_cancelled, NULL);
2880 struct got_object_id id;
2882 err = got_commit_graph_iter_next(&id, graph, repo,
2883 check_cancelled, NULL);
2885 if (err->code == GOT_ERR_ITER_COMPLETED)
2890 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2892 if (got_object_id_cmp(&id, commit_id) == 0) {
2899 got_commit_graph_close(graph);
2900 free(head_commit_id);
2901 if (!err && !is_same_branch)
2902 err = got_error(GOT_ERR_ANCESTRY);
2906 static const struct got_error *
2907 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2909 static char msg[512];
2910 const char *branch_name;
2912 if (got_ref_is_symbolic(ref))
2913 branch_name = got_ref_get_symref_target(ref);
2915 branch_name = got_ref_get_name(ref);
2917 if (strncmp("refs/heads/", branch_name, 11) == 0)
2920 snprintf(msg, sizeof(msg),
2921 "target commit is not contained in branch '%s'; "
2922 "the branch to use must be specified with -b; "
2923 "if necessary a new branch can be created for "
2924 "this commit with 'got branch -c %s BRANCH_NAME'",
2925 branch_name, commit_id_str);
2927 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2930 static const struct got_error *
2931 cmd_checkout(int argc, char *argv[])
2933 const struct got_error *error = NULL;
2934 struct got_repository *repo = NULL;
2935 struct got_reference *head_ref = NULL, *ref = NULL;
2936 struct got_worktree *worktree = NULL;
2937 char *repo_path = NULL;
2938 char *worktree_path = NULL;
2939 const char *path_prefix = "";
2940 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2941 char *commit_id_str = NULL;
2942 struct got_object_id *commit_id = NULL;
2944 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2945 struct got_pathlist_head paths;
2946 struct got_checkout_progress_arg cpa;
2947 int *pack_fds = NULL;
2951 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2954 branch_name = optarg;
2957 commit_id_str = strdup(optarg);
2958 if (commit_id_str == NULL)
2959 return got_error_from_errno("strdup");
2965 path_prefix = optarg;
2980 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2981 "unveil", NULL) == -1)
2985 char *base, *dotgit;
2987 repo_path = realpath(argv[0], NULL);
2988 if (repo_path == NULL)
2989 return got_error_from_errno2("realpath", argv[0]);
2990 cwd = getcwd(NULL, 0);
2992 error = got_error_from_errno("getcwd");
2999 error = got_path_basename(&base, path);
3002 dotgit = strstr(base, ".git");
3005 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3006 error = got_error_from_errno("asprintf");
3011 } else if (argc == 2) {
3012 repo_path = realpath(argv[0], NULL);
3013 if (repo_path == NULL) {
3014 error = got_error_from_errno2("realpath", argv[0]);
3017 worktree_path = realpath(argv[1], NULL);
3018 if (worktree_path == NULL) {
3019 if (errno != ENOENT) {
3020 error = got_error_from_errno2("realpath",
3024 worktree_path = strdup(argv[1]);
3025 if (worktree_path == NULL) {
3026 error = got_error_from_errno("strdup");
3033 got_path_strip_trailing_slashes(repo_path);
3034 got_path_strip_trailing_slashes(worktree_path);
3036 error = got_repo_pack_fds_open(&pack_fds);
3040 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3044 /* Pre-create work tree path for unveil(2) */
3045 error = got_path_mkdir(worktree_path);
3047 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3048 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3050 if (!allow_nonempty &&
3051 !got_path_dir_is_empty(worktree_path)) {
3052 error = got_error_path(worktree_path,
3053 GOT_ERR_DIR_NOT_EMPTY);
3058 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3062 error = got_ref_open(&head_ref, repo, branch_name, 0);
3066 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3067 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3070 error = got_worktree_open(&worktree, worktree_path);
3074 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3078 if (!same_path_prefix) {
3079 error = got_error(GOT_ERR_PATH_PREFIX);
3083 if (commit_id_str) {
3084 struct got_reflist_head refs;
3086 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3090 error = got_repo_match_object_id(&commit_id, NULL,
3091 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3092 got_ref_list_free(&refs);
3095 error = check_linear_ancestry(commit_id,
3096 got_worktree_get_base_commit_id(worktree), 0, repo);
3097 if (error != NULL) {
3098 if (error->code == GOT_ERR_ANCESTRY) {
3099 error = checkout_ancestry_error(
3100 head_ref, commit_id_str);
3104 error = check_same_branch(commit_id, head_ref, NULL, repo);
3106 if (error->code == GOT_ERR_ANCESTRY) {
3107 error = checkout_ancestry_error(
3108 head_ref, commit_id_str);
3112 error = got_worktree_set_base_commit_id(worktree, repo,
3116 /* Expand potentially abbreviated commit ID string. */
3117 free(commit_id_str);
3118 error = got_object_id_str(&commit_id_str, commit_id);
3122 commit_id = got_object_id_dup(
3123 got_worktree_get_base_commit_id(worktree));
3124 if (commit_id == NULL) {
3125 error = got_error_from_errno("got_object_id_dup");
3128 error = got_object_id_str(&commit_id_str, commit_id);
3133 error = got_pathlist_append(&paths, "", NULL);
3136 cpa.worktree_path = worktree_path;
3137 cpa.had_base_commit_ref_error = 0;
3138 cpa.verbosity = verbosity;
3139 error = got_worktree_checkout_files(worktree, &paths, repo,
3140 checkout_progress, &cpa, check_cancelled, NULL);
3144 if (got_ref_is_symbolic(head_ref)) {
3145 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3148 refname = got_ref_get_name(ref);
3150 refname = got_ref_get_name(head_ref);
3151 printf("Checked out %s: %s\n", refname, commit_id_str);
3152 printf("Now shut up and hack\n");
3153 if (cpa.had_base_commit_ref_error)
3154 show_worktree_base_ref_warning();
3157 const struct got_error *pack_err =
3158 got_repo_pack_fds_close(pack_fds);
3163 got_ref_close(head_ref);
3166 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3167 free(commit_id_str);
3170 free(worktree_path);
3175 struct got_update_progress_arg {
3187 print_update_progress_stats(struct got_update_progress_arg *upa)
3189 if (!upa->did_something)
3192 if (upa->conflicts > 0)
3193 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3194 if (upa->obstructed > 0)
3195 printf("File paths obstructed by a non-regular file: %d\n",
3197 if (upa->not_updated > 0)
3198 printf("Files not updated because of existing merge "
3199 "conflicts: %d\n", upa->not_updated);
3203 * The meaning of some status codes differs between merge-style operations and
3204 * update operations. For example, the ! status code means "file was missing"
3205 * if changes were merged into the work tree, and "missing file was restored"
3206 * if the work tree was updated. This function should be used by any operation
3207 * which merges changes into the work tree without updating the work tree.
3210 print_merge_progress_stats(struct got_update_progress_arg *upa)
3212 if (!upa->did_something)
3215 if (upa->conflicts > 0)
3216 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3217 if (upa->obstructed > 0)
3218 printf("File paths obstructed by a non-regular file: %d\n",
3220 if (upa->missing > 0)
3221 printf("Files which had incoming changes but could not be "
3222 "found in the work tree: %d\n", upa->missing);
3223 if (upa->not_deleted > 0)
3224 printf("Files not deleted due to differences in deleted "
3225 "content: %d\n", upa->not_deleted);
3226 if (upa->unversioned > 0)
3227 printf("Files not merged because an unversioned file was "
3228 "found in the work tree: %d\n", upa->unversioned);
3234 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3235 "[path ...]\n", getprogname());
3239 static const struct got_error *
3240 update_progress(void *arg, unsigned char status, const char *path)
3242 struct got_update_progress_arg *upa = arg;
3244 if (status == GOT_STATUS_EXISTS ||
3245 status == GOT_STATUS_BASE_REF_ERR)
3248 upa->did_something = 1;
3250 /* Base commit bump happens silently. */
3251 if (status == GOT_STATUS_BUMP_BASE)
3254 if (status == GOT_STATUS_CONFLICT)
3256 if (status == GOT_STATUS_OBSTRUCTED)
3258 if (status == GOT_STATUS_CANNOT_UPDATE)
3260 if (status == GOT_STATUS_MISSING)
3262 if (status == GOT_STATUS_CANNOT_DELETE)
3264 if (status == GOT_STATUS_UNVERSIONED)
3267 while (path[0] == '/')
3269 if (upa->verbosity >= 0)
3270 printf("%c %s\n", status, path);
3275 static const struct got_error *
3276 switch_head_ref(struct got_reference *head_ref,
3277 struct got_object_id *commit_id, struct got_worktree *worktree,
3278 struct got_repository *repo)
3280 const struct got_error *err = NULL;
3282 int ref_has_moved = 0;
3284 /* Trivial case: switching between two different references. */
3285 if (strcmp(got_ref_get_name(head_ref),
3286 got_worktree_get_head_ref_name(worktree)) != 0) {
3287 printf("Switching work tree from %s to %s\n",
3288 got_worktree_get_head_ref_name(worktree),
3289 got_ref_get_name(head_ref));
3290 return got_worktree_set_head_ref(worktree, head_ref);
3293 err = check_linear_ancestry(commit_id,
3294 got_worktree_get_base_commit_id(worktree), 0, repo);
3296 if (err->code != GOT_ERR_ANCESTRY)
3303 /* Switching to a rebased branch with the same reference name. */
3304 err = got_object_id_str(&base_id_str,
3305 got_worktree_get_base_commit_id(worktree));
3308 printf("Reference %s now points at a different branch\n",
3309 got_worktree_get_head_ref_name(worktree));
3310 printf("Switching work tree from %s to %s\n", base_id_str,
3311 got_worktree_get_head_ref_name(worktree));
3315 static const struct got_error *
3316 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3318 const struct got_error *err;
3321 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3325 return got_error(GOT_ERR_REBASING);
3327 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3331 return got_error(GOT_ERR_HISTEDIT_BUSY);
3336 static const struct got_error *
3337 check_merge_in_progress(struct got_worktree *worktree,
3338 struct got_repository *repo)
3340 const struct got_error *err;
3343 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3347 return got_error(GOT_ERR_MERGE_BUSY);
3352 static const struct got_error *
3353 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3354 char *argv[], struct got_worktree *worktree)
3356 const struct got_error *err = NULL;
3358 struct got_pathlist_entry *new;
3364 return got_error_from_errno("strdup");
3365 return got_pathlist_append(paths, path, NULL);
3368 for (i = 0; i < argc; i++) {
3369 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3372 err = got_pathlist_insert(&new, paths, path, NULL);
3373 if (err || new == NULL /* duplicate */) {
3383 static const struct got_error *
3384 wrap_not_worktree_error(const struct got_error *orig_err,
3385 const char *cmdname, const char *path)
3387 const struct got_error *err;
3388 struct got_repository *repo;
3389 static char msg[512];
3390 int *pack_fds = NULL;
3392 err = got_repo_pack_fds_open(&pack_fds);
3396 err = got_repo_open(&repo, path, NULL, pack_fds);
3400 snprintf(msg, sizeof(msg),
3401 "'got %s' needs a work tree in addition to a git repository\n"
3402 "Work trees can be checked out from this Git repository with "
3404 "The got(1) manual page contains more information.", cmdname);
3405 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3406 got_repo_close(repo);
3408 const struct got_error *pack_err =
3409 got_repo_pack_fds_close(pack_fds);
3416 static const struct got_error *
3417 cmd_update(int argc, char *argv[])
3419 const struct got_error *error = NULL;
3420 struct got_repository *repo = NULL;
3421 struct got_worktree *worktree = NULL;
3422 char *worktree_path = NULL;
3423 struct got_object_id *commit_id = NULL;
3424 char *commit_id_str = NULL;
3425 const char *branch_name = NULL;
3426 struct got_reference *head_ref = NULL;
3427 struct got_pathlist_head paths;
3428 struct got_pathlist_entry *pe;
3429 int ch, verbosity = 0;
3430 struct got_update_progress_arg upa;
3431 int *pack_fds = NULL;
3435 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3438 branch_name = optarg;
3441 commit_id_str = strdup(optarg);
3442 if (commit_id_str == NULL)
3443 return got_error_from_errno("strdup");
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3459 "unveil", NULL) == -1)
3462 worktree_path = getcwd(NULL, 0);
3463 if (worktree_path == NULL) {
3464 error = got_error_from_errno("getcwd");
3468 error = got_repo_pack_fds_open(&pack_fds);
3472 error = got_worktree_open(&worktree, worktree_path);
3474 if (error->code == GOT_ERR_NOT_WORKTREE)
3475 error = wrap_not_worktree_error(error, "update",
3480 error = check_rebase_or_histedit_in_progress(worktree);
3484 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3489 error = apply_unveil(got_repo_get_path(repo), 0,
3490 got_worktree_get_root_path(worktree));
3494 error = check_merge_in_progress(worktree, repo);
3498 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3502 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3503 got_worktree_get_head_ref_name(worktree), 0);
3506 if (commit_id_str == NULL) {
3507 error = got_ref_resolve(&commit_id, repo, head_ref);
3510 error = got_object_id_str(&commit_id_str, commit_id);
3514 struct got_reflist_head refs;
3516 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3520 error = got_repo_match_object_id(&commit_id, NULL,
3521 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3522 got_ref_list_free(&refs);
3523 free(commit_id_str);
3524 commit_id_str = NULL;
3527 error = got_object_id_str(&commit_id_str, commit_id);
3533 struct got_object_id *head_commit_id;
3534 TAILQ_FOREACH(pe, &paths, entry) {
3535 if (pe->path_len == 0)
3537 error = got_error_msg(GOT_ERR_BAD_PATH,
3538 "switching between branches requires that "
3539 "the entire work tree gets updated");
3542 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3545 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3547 free(head_commit_id);
3550 error = check_same_branch(commit_id, head_ref, NULL, repo);
3553 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3557 error = check_linear_ancestry(commit_id,
3558 got_worktree_get_base_commit_id(worktree), 0, repo);
3559 if (error != NULL) {
3560 if (error->code == GOT_ERR_ANCESTRY)
3561 error = got_error(GOT_ERR_BRANCH_MOVED);
3564 error = check_same_branch(commit_id, head_ref, NULL, repo);
3569 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3571 error = got_worktree_set_base_commit_id(worktree, repo,
3577 memset(&upa, 0, sizeof(upa));
3578 upa.verbosity = verbosity;
3579 error = got_worktree_checkout_files(worktree, &paths, repo,
3580 update_progress, &upa, check_cancelled, NULL);
3584 if (upa.did_something) {
3585 printf("Updated to %s: %s\n",
3586 got_worktree_get_head_ref_name(worktree), commit_id_str);
3588 printf("Already up-to-date\n");
3590 print_update_progress_stats(&upa);
3593 const struct got_error *pack_err =
3594 got_repo_pack_fds_close(pack_fds);
3598 free(worktree_path);
3599 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3601 free(commit_id_str);
3605 static const struct got_error *
3606 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3607 const char *path, int diff_context, int ignore_whitespace,
3608 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3609 struct got_repository *repo, FILE *outfile)
3611 const struct got_error *err = NULL;
3612 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3613 FILE *f1 = NULL, *f2 = NULL;
3614 int fd1 = -1, fd2 = -1;
3616 fd1 = got_opentempfd();
3618 return got_error_from_errno("got_opentempfd");
3619 fd2 = got_opentempfd();
3621 err = got_error_from_errno("got_opentempfd");
3626 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3632 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3636 f1 = got_opentemp();
3638 err = got_error_from_errno("got_opentemp");
3641 f2 = got_opentemp();
3643 err = got_error_from_errno("got_opentemp");
3647 while (path[0] == '/')
3649 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3650 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3651 force_text_diff, dsa, outfile);
3653 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3654 err = got_error_from_errno("close");
3656 got_object_blob_close(blob1);
3657 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3658 err = got_error_from_errno("close");
3659 got_object_blob_close(blob2);
3660 if (f1 && fclose(f1) == EOF && err == NULL)
3661 err = got_error_from_errno("fclose");
3662 if (f2 && fclose(f2) == EOF && err == NULL)
3663 err = got_error_from_errno("fclose");
3667 static const struct got_error *
3668 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3669 const char *path, int diff_context, int ignore_whitespace,
3670 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3671 struct got_repository *repo, FILE *outfile)
3673 const struct got_error *err = NULL;
3674 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3675 struct got_diff_blob_output_unidiff_arg arg;
3676 FILE *f1 = NULL, *f2 = NULL;
3677 int fd1 = -1, fd2 = -1;
3680 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3683 fd1 = got_opentempfd();
3685 err = got_error_from_errno("got_opentempfd");
3690 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3694 f1 = got_opentemp();
3696 err = got_error_from_errno("got_opentemp");
3700 f2 = got_opentemp();
3702 err = got_error_from_errno("got_opentemp");
3705 fd2 = got_opentempfd();
3707 err = got_error_from_errno("got_opentempfd");
3710 arg.diff_context = diff_context;
3711 arg.ignore_whitespace = ignore_whitespace;
3712 arg.force_text_diff = force_text_diff;
3714 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3715 arg.outfile = outfile;
3718 while (path[0] == '/')
3720 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3721 got_diff_blob_output_unidiff, &arg, 1);
3724 got_object_tree_close(tree1);
3726 got_object_tree_close(tree2);
3727 if (f1 && fclose(f1) == EOF && err == NULL)
3728 err = got_error_from_errno("fclose");
3729 if (f2 && fclose(f2) == EOF && err == NULL)
3730 err = got_error_from_errno("fclose");
3731 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3732 err = got_error_from_errno("close");
3733 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3734 err = got_error_from_errno("close");
3738 static const struct got_error *
3739 get_changed_paths(struct got_pathlist_head *paths,
3740 struct got_commit_object *commit, struct got_repository *repo,
3741 struct got_diffstat_cb_arg *dsa)
3743 const struct got_error *err = NULL;
3744 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3745 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3746 struct got_object_qid *qid;
3747 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3748 FILE *f1 = NULL, *f2 = NULL;
3749 int fd1 = -1, fd2 = -1;
3752 cb = got_diff_tree_compute_diffstat;
3754 f1 = got_opentemp();
3756 err = got_error_from_errno("got_opentemp");
3759 f2 = got_opentemp();
3761 err = got_error_from_errno("got_opentemp");
3764 fd1 = got_opentempfd();
3766 err = got_error_from_errno("got_opentempfd");
3769 fd2 = got_opentempfd();
3771 err = got_error_from_errno("got_opentempfd");
3776 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3778 struct got_commit_object *pcommit;
3779 err = got_object_open_as_commit(&pcommit, repo,
3784 tree_id1 = got_object_id_dup(
3785 got_object_commit_get_tree_id(pcommit));
3786 if (tree_id1 == NULL) {
3787 got_object_commit_close(pcommit);
3788 return got_error_from_errno("got_object_id_dup");
3790 got_object_commit_close(pcommit);
3795 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3800 tree_id2 = got_object_commit_get_tree_id(commit);
3801 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3805 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3806 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3809 got_object_tree_close(tree1);
3811 got_object_tree_close(tree2);
3812 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3813 err = got_error_from_errno("close");
3814 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3815 err = got_error_from_errno("close");
3816 if (f1 && fclose(f1) == EOF && err == NULL)
3817 err = got_error_from_errno("fclose");
3818 if (f2 && fclose(f2) == EOF && err == NULL)
3819 err = got_error_from_errno("fclose");
3824 static const struct got_error *
3825 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3826 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3827 struct got_repository *repo, FILE *outfile)
3829 const struct got_error *err = NULL;
3830 struct got_commit_object *pcommit = NULL;
3831 char *id_str1 = NULL, *id_str2 = NULL;
3832 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3833 struct got_object_qid *qid;
3835 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3837 err = got_object_open_as_commit(&pcommit, repo,
3841 err = got_object_id_str(&id_str1, &qid->id);
3846 err = got_object_id_str(&id_str2, id);
3850 if (path && path[0] != '\0') {
3852 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3856 err = got_object_id_by_path(&obj_id1, repo,
3859 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3865 err = got_object_get_type(&obj_type, repo, obj_id2);
3871 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3872 fprintf(outfile, "commit - %s\n",
3873 id_str1 ? id_str1 : "/dev/null");
3874 fprintf(outfile, "commit + %s\n", id_str2);
3876 case GOT_OBJ_TYPE_BLOB:
3877 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3878 0, 0, dsa, repo, outfile);
3880 case GOT_OBJ_TYPE_TREE:
3881 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3882 0, 0, dsa, repo, outfile);
3885 err = got_error(GOT_ERR_OBJ_TYPE);
3891 obj_id2 = got_object_commit_get_tree_id(commit);
3893 obj_id1 = got_object_commit_get_tree_id(pcommit);
3895 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3896 fprintf(outfile, "commit - %s\n",
3897 id_str1 ? id_str1 : "/dev/null");
3898 fprintf(outfile, "commit + %s\n", id_str2);
3899 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3900 dsa, repo, outfile);
3906 got_object_commit_close(pcommit);
3911 get_datestr(time_t *time, char *datebuf)
3913 struct tm mytm, *tm;
3916 tm = gmtime_r(time, &mytm);
3919 s = asctime_r(tm, datebuf);
3922 p = strchr(s, '\n');
3928 static const struct got_error *
3929 match_commit(int *have_match, struct got_object_id *id,
3930 struct got_commit_object *commit, regex_t *regex)
3932 const struct got_error *err = NULL;
3933 regmatch_t regmatch;
3934 char *id_str = NULL, *logmsg = NULL;
3938 err = got_object_id_str(&id_str, id);
3942 err = got_object_commit_get_logmsg(&logmsg, commit);
3946 if (regexec(regex, got_object_commit_get_author(commit), 1,
3947 ®match, 0) == 0 ||
3948 regexec(regex, got_object_commit_get_committer(commit), 1,
3949 ®match, 0) == 0 ||
3950 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3951 regexec(regex, logmsg, 1, ®match, 0) == 0)
3960 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3963 regmatch_t regmatch;
3964 struct got_pathlist_entry *pe;
3968 TAILQ_FOREACH(pe, changed_paths, entry) {
3969 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3976 static const struct got_error *
3977 match_patch(int *have_match, struct got_commit_object *commit,
3978 struct got_object_id *id, const char *path, int diff_context,
3979 struct got_repository *repo, regex_t *regex, FILE *f)
3981 const struct got_error *err = NULL;
3983 size_t linesize = 0;
3984 regmatch_t regmatch;
3988 err = got_opentemp_truncate(f);
3992 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3996 if (fseeko(f, 0L, SEEK_SET) == -1) {
3997 err = got_error_from_errno("fseeko");
4001 while (getline(&line, &linesize, f) != -1) {
4002 if (regexec(regex, line, 1, ®match, 0) == 0) {
4012 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4014 static const struct got_error*
4015 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4016 struct got_object_id *id, struct got_repository *repo,
4019 static const struct got_error *err = NULL;
4020 struct got_reflist_entry *re;
4026 TAILQ_FOREACH(re, refs, entry) {
4027 struct got_tag_object *tag = NULL;
4028 struct got_object_id *ref_id;
4031 name = got_ref_get_name(re->ref);
4032 if (strcmp(name, GOT_REF_HEAD) == 0)
4034 if (strncmp(name, "refs/", 5) == 0)
4036 if (strncmp(name, "got/", 4) == 0)
4038 if (strncmp(name, "heads/", 6) == 0)
4040 if (strncmp(name, "remotes/", 8) == 0) {
4044 s = strstr(name, "/" GOT_REF_HEAD);
4045 if (s != NULL && s[strlen(s)] == '\0')
4048 err = got_ref_resolve(&ref_id, repo, re->ref);
4051 if (strncmp(name, "tags/", 5) == 0) {
4052 err = got_object_open_as_tag(&tag, repo, ref_id);
4054 if (err->code != GOT_ERR_OBJ_TYPE) {
4058 /* Ref points at something other than a tag. */
4063 cmp = got_object_id_cmp(tag ?
4064 got_object_tag_get_object_id(tag) : ref_id, id);
4067 got_object_tag_close(tag);
4071 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4072 s ? ", " : "", name) == -1) {
4073 err = got_error_from_errno("asprintf");
4084 static const struct got_error *
4085 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4086 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4088 const struct got_error *err = NULL;
4089 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4090 char *comma, *s, *nl;
4091 struct got_reflist_head *refs;
4092 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4094 time_t committer_time;
4096 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4098 err = build_refs_str(&ref_str, refs, id, repo, 1);
4102 /* Display the first matching ref only. */
4103 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4107 if (ref_str == NULL) {
4108 err = got_object_id_str(&id_str, id);
4113 committer_time = got_object_commit_get_committer_time(commit);
4114 if (gmtime_r(&committer_time, &tm) == NULL) {
4115 err = got_error_from_errno("gmtime_r");
4118 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4119 err = got_error(GOT_ERR_NO_SPACE);
4123 err = got_object_commit_get_logmsg(&logmsg0, commit);
4128 while (isspace((unsigned char)s[0]))
4131 nl = strchr(s, '\n');
4137 printf("%s%-7s %s\n", datebuf, ref_str, s);
4139 printf("%s%.7s %s\n", datebuf, id_str, s);
4141 if (fflush(stdout) != 0 && err == NULL)
4142 err = got_error_from_errno("fflush");
4150 static const struct got_error *
4151 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4153 struct got_pathlist_entry *pe;
4156 printf("%s\n", header);
4158 TAILQ_FOREACH(pe, dsa->paths, entry) {
4159 struct got_diff_changed_path *cp = pe->data;
4160 int pad = dsa->max_path_len - pe->path_len + 1;
4162 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4163 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4165 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4166 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4167 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4169 if (fflush(stdout) != 0)
4170 return got_error_from_errno("fflush");
4175 static const struct got_error *
4181 if (fseeko(f, 0L, SEEK_SET) == -1)
4182 return got_error_from_errno("fseek");
4185 r = fread(buf, 1, sizeof(buf), f);
4188 return got_error_from_errno("fread");
4192 if (fwrite(buf, 1, r, stdout) != r)
4193 return got_ferror(stdout, GOT_ERR_IO);
4199 static const struct got_error *
4200 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4201 struct got_repository *repo, const char *path,
4202 struct got_pathlist_head *changed_paths,
4203 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4204 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4207 const struct got_error *err = NULL;
4209 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4211 time_t committer_time;
4212 const char *author, *committer;
4213 char *refs_str = NULL;
4215 err = got_object_id_str(&id_str, id);
4219 if (custom_refs_str == NULL) {
4220 struct got_reflist_head *refs;
4221 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4223 err = build_refs_str(&refs_str, refs, id, repo, 0);
4229 printf(GOT_COMMIT_SEP_STR);
4230 if (custom_refs_str)
4231 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4234 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4235 refs_str ? " (" : "", refs_str ? refs_str : "",
4236 refs_str ? ")" : "");
4241 printf("from: %s\n", got_object_commit_get_author(commit));
4242 author = got_object_commit_get_author(commit);
4243 committer = got_object_commit_get_committer(commit);
4244 if (strcmp(author, committer) != 0)
4245 printf("via: %s\n", committer);
4246 committer_time = got_object_commit_get_committer_time(commit);
4247 datestr = get_datestr(&committer_time, datebuf);
4249 printf("date: %s UTC\n", datestr);
4250 if (got_object_commit_get_nparents(commit) > 1) {
4251 const struct got_object_id_queue *parent_ids;
4252 struct got_object_qid *qid;
4254 parent_ids = got_object_commit_get_parent_ids(commit);
4255 STAILQ_FOREACH(qid, parent_ids, entry) {
4256 err = got_object_id_str(&id_str, &qid->id);
4259 printf("parent %d: %s\n", n++, id_str);
4265 err = got_object_commit_get_logmsg(&logmsg0, commit);
4271 line = strsep(&logmsg, "\n");
4273 printf(" %s\n", line);
4277 if (changed_paths && diffstat == NULL) {
4278 struct got_pathlist_entry *pe;
4280 TAILQ_FOREACH(pe, changed_paths, entry) {
4281 struct got_diff_changed_path *cp = pe->data;
4283 printf(" %c %s\n", cp->status, pe->path);
4291 err = got_error_from_errno("got_opentemp");
4296 err = print_patch(commit, id, path, diff_context, diffstat,
4297 repo, diffstat == NULL ? stdout : f);
4302 err = print_diffstat(diffstat, NULL);
4314 if (fflush(stdout) != 0 && err == NULL)
4315 err = got_error_from_errno("fflush");
4317 if (f && fclose(f) == EOF && err == NULL)
4318 err = got_error_from_errno("fclose");
4324 static const struct got_error *
4325 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4326 struct got_repository *repo, const char *path, int show_changed_paths,
4327 int show_diffstat, int show_patch, const char *search_pattern,
4328 int diff_context, int limit, int log_branches, int reverse_display_order,
4329 struct got_reflist_object_id_map *refs_idmap, int one_line,
4332 const struct got_error *err;
4333 struct got_commit_graph *graph;
4336 struct got_object_id_queue reversed_commits;
4337 struct got_object_qid *qid;
4338 struct got_commit_object *commit;
4339 struct got_pathlist_head changed_paths;
4341 STAILQ_INIT(&reversed_commits);
4342 TAILQ_INIT(&changed_paths);
4344 if (search_pattern && regcomp(®ex, search_pattern,
4345 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4346 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4348 err = got_commit_graph_open(&graph, path, !log_branches);
4351 err = got_commit_graph_iter_start(graph, root_id, repo,
4352 check_cancelled, NULL);
4356 struct got_object_id id;
4357 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4358 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4360 if (sigint_received || sigpipe_received)
4363 err = got_commit_graph_iter_next(&id, graph, repo,
4364 check_cancelled, NULL);
4366 if (err->code == GOT_ERR_ITER_COMPLETED)
4371 err = got_object_open_as_commit(&commit, repo, &id);
4375 if ((show_changed_paths || (show_diffstat && !show_patch))
4376 && !reverse_display_order) {
4377 err = get_changed_paths(&changed_paths, commit, repo,
4378 show_diffstat ? &dsa : NULL);
4383 if (search_pattern) {
4384 err = match_commit(&have_match, &id, commit, ®ex);
4386 got_object_commit_close(commit);
4389 if (have_match == 0 && show_changed_paths)
4390 match_changed_paths(&have_match,
4391 &changed_paths, ®ex);
4392 if (have_match == 0 && show_patch) {
4393 err = match_patch(&have_match, commit, &id,
4394 path, diff_context, repo, ®ex, tmpfile);
4398 if (have_match == 0) {
4399 got_object_commit_close(commit);
4400 got_pathlist_free(&changed_paths,
4401 GOT_PATHLIST_FREE_ALL);
4406 if (reverse_display_order) {
4407 err = got_object_qid_alloc(&qid, &id);
4410 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4411 got_object_commit_close(commit);
4414 err = print_commit_oneline(commit, &id,
4417 err = print_commit(commit, &id, repo, path,
4418 (show_changed_paths || show_diffstat) ?
4419 &changed_paths : NULL,
4420 show_diffstat ? &dsa : NULL, show_patch,
4421 diff_context, refs_idmap, NULL, NULL);
4422 got_object_commit_close(commit);
4426 if ((limit && --limit == 0) ||
4427 (end_id && got_object_id_cmp(&id, end_id) == 0))
4430 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4432 if (reverse_display_order) {
4433 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4434 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4435 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4437 err = got_object_open_as_commit(&commit, repo,
4441 if (show_changed_paths ||
4442 (show_diffstat && !show_patch)) {
4443 err = get_changed_paths(&changed_paths, commit,
4444 repo, show_diffstat ? &dsa : NULL);
4449 err = print_commit_oneline(commit, &qid->id,
4452 err = print_commit(commit, &qid->id, repo, path,
4453 (show_changed_paths || show_diffstat) ?
4454 &changed_paths : NULL,
4455 show_diffstat ? &dsa : NULL, show_patch,
4456 diff_context, refs_idmap, NULL, NULL);
4457 got_object_commit_close(commit);
4460 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4464 while (!STAILQ_EMPTY(&reversed_commits)) {
4465 qid = STAILQ_FIRST(&reversed_commits);
4466 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4467 got_object_qid_free(qid);
4469 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4472 got_commit_graph_close(graph);
4479 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4480 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4481 "[path]\n", getprogname());
4486 get_default_log_limit(void)
4488 const char *got_default_log_limit;
4492 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4493 if (got_default_log_limit == NULL)
4495 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4501 static const struct got_error *
4502 cmd_log(int argc, char *argv[])
4504 const struct got_error *error;
4505 struct got_repository *repo = NULL;
4506 struct got_worktree *worktree = NULL;
4507 struct got_object_id *start_id = NULL, *end_id = NULL;
4508 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4509 const char *start_commit = NULL, *end_commit = NULL;
4510 const char *search_pattern = NULL;
4511 int diff_context = -1, ch;
4512 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4513 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4515 struct got_reflist_head refs;
4516 struct got_reflist_object_id_map *refs_idmap = NULL;
4517 FILE *tmpfile = NULL;
4518 int *pack_fds = NULL;
4523 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4529 limit = get_default_log_limit();
4531 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4537 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4540 errx(1, "number of context lines is %s: %s",
4544 start_commit = optarg;
4550 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4552 errx(1, "number of commits is %s: %s",
4556 show_changed_paths = 1;
4562 reverse_display_order = 1;
4565 repo_path = realpath(optarg, NULL);
4566 if (repo_path == NULL)
4567 return got_error_from_errno2("realpath",
4569 got_path_strip_trailing_slashes(repo_path);
4572 search_pattern = optarg;
4578 end_commit = optarg;
4589 if (diff_context == -1)
4591 else if (!show_patch)
4592 errx(1, "-C requires -p");
4594 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4595 errx(1, "cannot use -s with -d, -p or -P");
4597 cwd = getcwd(NULL, 0);
4599 error = got_error_from_errno("getcwd");
4603 error = got_repo_pack_fds_open(&pack_fds);
4607 if (repo_path == NULL) {
4608 error = got_worktree_open(&worktree, cwd);
4609 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4616 error = got_worktree_resolve_path(&path, worktree,
4621 path = strdup(argv[0]);
4623 error = got_error_from_errno("strdup");
4627 } else if (argc != 0)
4630 if (repo_path == NULL) {
4631 repo_path = worktree ?
4632 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4634 if (repo_path == NULL) {
4635 error = got_error_from_errno("strdup");
4639 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4643 error = apply_unveil(got_repo_get_path(repo), 1,
4644 worktree ? got_worktree_get_root_path(worktree) : NULL);
4648 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4652 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4656 if (start_commit == NULL) {
4657 struct got_reference *head_ref;
4658 struct got_commit_object *commit = NULL;
4659 error = got_ref_open(&head_ref, repo,
4660 worktree ? got_worktree_get_head_ref_name(worktree)
4664 error = got_ref_resolve(&start_id, repo, head_ref);
4665 got_ref_close(head_ref);
4668 error = got_object_open_as_commit(&commit, repo,
4672 got_object_commit_close(commit);
4674 error = got_repo_match_object_id(&start_id, NULL,
4675 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4679 if (end_commit != NULL) {
4680 error = got_repo_match_object_id(&end_id, NULL,
4681 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4688 * If a path was specified on the command line it was resolved
4689 * to a path in the work tree above. Prepend the work tree's
4690 * path prefix to obtain the corresponding in-repository path.
4694 prefix = got_worktree_get_path_prefix(worktree);
4695 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4696 (path[0] != '\0') ? "/" : "", path) == -1) {
4697 error = got_error_from_errno("asprintf");
4702 error = got_repo_map_path(&in_repo_path, repo,
4708 path = in_repo_path;
4712 /* Release work tree lock. */
4713 got_worktree_close(worktree);
4717 if (search_pattern && show_patch) {
4718 tmpfile = got_opentemp();
4719 if (tmpfile == NULL) {
4720 error = got_error_from_errno("got_opentemp");
4725 error = print_commits(start_id, end_id, repo, path ? path : "",
4726 show_changed_paths, show_diffstat, show_patch, search_pattern,
4727 diff_context, limit, log_branches, reverse_display_order,
4728 refs_idmap, one_line, tmpfile);
4734 got_worktree_close(worktree);
4736 const struct got_error *close_err = got_repo_close(repo);
4741 const struct got_error *pack_err =
4742 got_repo_pack_fds_close(pack_fds);
4747 got_reflist_object_id_map_free(refs_idmap);
4748 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4749 error = got_error_from_errno("fclose");
4750 got_ref_list_free(&refs);
4757 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4758 "[-r repository-path] [object1 object2 | path ...]\n",
4763 struct print_diff_arg {
4764 struct got_repository *repo;
4765 struct got_worktree *worktree;
4766 struct got_diffstat_cb_arg *diffstat;
4771 enum got_diff_algorithm diff_algo;
4772 int ignore_whitespace;
4773 int force_text_diff;
4780 * Create a file which contains the target path of a symlink so we can feed
4781 * it as content to the diff engine.
4783 static const struct got_error *
4784 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4785 const char *abspath)
4787 const struct got_error *err = NULL;
4788 char target_path[PATH_MAX];
4789 ssize_t target_len, outlen;
4794 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4795 if (target_len == -1)
4796 return got_error_from_errno2("readlinkat", abspath);
4798 target_len = readlink(abspath, target_path, PATH_MAX);
4799 if (target_len == -1)
4800 return got_error_from_errno2("readlink", abspath);
4803 *fd = got_opentempfd();
4805 return got_error_from_errno("got_opentempfd");
4807 outlen = write(*fd, target_path, target_len);
4809 err = got_error_from_errno("got_opentempfd");
4813 if (lseek(*fd, 0, SEEK_SET) == -1) {
4814 err = got_error_from_errno2("lseek", abspath);
4825 static const struct got_error *
4826 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4827 const char *path, struct got_object_id *blob_id,
4828 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4829 int dirfd, const char *de_name)
4831 struct print_diff_arg *a = arg;
4832 const struct got_error *err = NULL;
4833 struct got_blob_object *blob1 = NULL;
4834 int fd = -1, fd1 = -1, fd2 = -1;
4836 char *abspath = NULL, *label1 = NULL;
4841 memset(&sb, 0, sizeof(sb));
4843 if (a->diff_staged) {
4844 if (staged_status != GOT_STATUS_MODIFY &&
4845 staged_status != GOT_STATUS_ADD &&
4846 staged_status != GOT_STATUS_DELETE)
4849 if (staged_status == GOT_STATUS_DELETE)
4851 if (status == GOT_STATUS_NONEXISTENT)
4852 return got_error_set_errno(ENOENT, path);
4853 if (status != GOT_STATUS_MODIFY &&
4854 status != GOT_STATUS_ADD &&
4855 status != GOT_STATUS_DELETE &&
4856 status != GOT_STATUS_CONFLICT)
4860 err = got_opentemp_truncate(a->f1);
4862 return got_error_from_errno("got_opentemp_truncate");
4863 err = got_opentemp_truncate(a->f2);
4865 return got_error_from_errno("got_opentemp_truncate");
4867 if (!a->header_shown) {
4868 if (fprintf(a->outfile, "diff %s%s\n",
4869 a->diff_staged ? "-s " : "",
4870 got_worktree_get_root_path(a->worktree)) < 0) {
4871 err = got_error_from_errno("fprintf");
4874 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4875 err = got_error_from_errno("fprintf");
4878 if (fprintf(a->outfile, "path + %s%s\n",
4879 got_worktree_get_root_path(a->worktree),
4880 a->diff_staged ? " (staged changes)" : "") < 0) {
4881 err = got_error_from_errno("fprintf");
4884 a->header_shown = 1;
4887 if (a->diff_staged) {
4888 const char *label1 = NULL, *label2 = NULL;
4889 switch (staged_status) {
4890 case GOT_STATUS_MODIFY:
4894 case GOT_STATUS_ADD:
4897 case GOT_STATUS_DELETE:
4901 return got_error(GOT_ERR_FILE_STATUS);
4903 fd1 = got_opentempfd();
4905 err = got_error_from_errno("got_opentempfd");
4908 fd2 = got_opentempfd();
4910 err = got_error_from_errno("got_opentempfd");
4913 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4914 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4915 a->diff_algo, a->diff_context, a->ignore_whitespace,
4916 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4920 fd1 = got_opentempfd();
4922 err = got_error_from_errno("got_opentempfd");
4926 if (staged_status == GOT_STATUS_ADD ||
4927 staged_status == GOT_STATUS_MODIFY) {
4929 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4933 err = got_object_id_str(&id_str, staged_blob_id);
4936 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4937 err = got_error_from_errno("asprintf");
4942 } else if (status != GOT_STATUS_ADD) {
4943 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4949 if (status != GOT_STATUS_DELETE) {
4950 if (asprintf(&abspath, "%s/%s",
4951 got_worktree_get_root_path(a->worktree), path) == -1) {
4952 err = got_error_from_errno("asprintf");
4957 fd = openat(dirfd, de_name,
4958 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4960 if (!got_err_open_nofollow_on_symlink()) {
4961 err = got_error_from_errno2("openat",
4965 err = get_symlink_target_file(&fd, dirfd,
4971 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4973 if (!got_err_open_nofollow_on_symlink()) {
4974 err = got_error_from_errno2("open",
4978 err = get_symlink_target_file(&fd, dirfd,
4984 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4985 err = got_error_from_errno2("fstatat", abspath);
4988 f2 = fdopen(fd, "r");
4990 err = got_error_from_errno2("fdopen", abspath);
4998 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5004 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5005 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5006 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5008 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5009 err = got_error_from_errno("close");
5010 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5011 err = got_error_from_errno("close");
5013 got_object_blob_close(blob1);
5014 if (fd != -1 && close(fd) == -1 && err == NULL)
5015 err = got_error_from_errno("close");
5016 if (f2 && fclose(f2) == EOF && err == NULL)
5017 err = got_error_from_errno("fclose");
5022 static const struct got_error *
5023 cmd_diff(int argc, char *argv[])
5025 const struct got_error *error;
5026 struct got_repository *repo = NULL;
5027 struct got_worktree *worktree = NULL;
5028 char *cwd = NULL, *repo_path = NULL;
5029 const char *commit_args[2] = { NULL, NULL };
5030 int ncommit_args = 0;
5031 struct got_object_id *ids[2] = { NULL, NULL };
5032 char *labels[2] = { NULL, NULL };
5033 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5034 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5035 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5037 struct got_reflist_head refs;
5038 struct got_pathlist_head diffstat_paths, paths;
5039 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5040 int fd1 = -1, fd2 = -1;
5041 int *pack_fds = NULL;
5042 struct got_diffstat_cb_arg dsa;
5044 memset(&dsa, 0, sizeof(dsa));
5048 TAILQ_INIT(&diffstat_paths);
5051 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5056 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5059 force_text_diff = 1;
5062 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5065 errx(1, "number of context lines is %s: %s",
5069 if (ncommit_args >= 2)
5070 errx(1, "too many -c options used");
5071 commit_args[ncommit_args++] = optarg;
5080 repo_path = realpath(optarg, NULL);
5081 if (repo_path == NULL)
5082 return got_error_from_errno2("realpath",
5084 got_path_strip_trailing_slashes(repo_path);
5091 ignore_whitespace = 1;
5102 cwd = getcwd(NULL, 0);
5104 error = got_error_from_errno("getcwd");
5108 error = got_repo_pack_fds_open(&pack_fds);
5112 if (repo_path == NULL) {
5113 error = got_worktree_open(&worktree, cwd);
5114 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5120 strdup(got_worktree_get_repo_path(worktree));
5121 if (repo_path == NULL) {
5122 error = got_error_from_errno("strdup");
5126 repo_path = strdup(cwd);
5127 if (repo_path == NULL) {
5128 error = got_error_from_errno("strdup");
5134 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5139 if (show_diffstat) {
5140 dsa.paths = &diffstat_paths;
5141 dsa.force_text = force_text_diff;
5142 dsa.ignore_ws = ignore_whitespace;
5143 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5146 if (rflag || worktree == NULL || ncommit_args > 0) {
5148 error = got_error_msg(GOT_ERR_NOT_IMPL,
5149 "-P option can only be used when diffing "
5154 error = got_error_msg(GOT_ERR_NOT_IMPL,
5155 "-s option can only be used when diffing "
5161 error = apply_unveil(got_repo_get_path(repo), 1,
5162 worktree ? got_worktree_get_root_path(worktree) : NULL);
5166 if ((!force_path && argc == 2) || ncommit_args > 0) {
5167 int obj_type = (ncommit_args > 0 ?
5168 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5169 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5173 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5175 if (ncommit_args > 0)
5176 arg = commit_args[i];
5179 error = got_repo_match_object_id(&ids[i], &labels[i],
5180 arg, obj_type, &refs, repo);
5182 if (error->code != GOT_ERR_NOT_REF &&
5183 error->code != GOT_ERR_NO_OBJ)
5185 if (ncommit_args > 0)
5193 f1 = got_opentemp();
5195 error = got_error_from_errno("got_opentemp");
5199 f2 = got_opentemp();
5201 error = got_error_from_errno("got_opentemp");
5205 outfile = got_opentemp();
5206 if (outfile == NULL) {
5207 error = got_error_from_errno("got_opentemp");
5211 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5212 struct print_diff_arg arg;
5215 if (worktree == NULL) {
5216 if (argc == 2 && ids[0] == NULL) {
5217 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5219 } else if (argc == 2 && ids[1] == NULL) {
5220 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5222 } else if (argc > 0) {
5223 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5224 "%s", "specified paths cannot be resolved");
5227 error = got_error(GOT_ERR_NOT_WORKTREE);
5232 error = get_worktree_paths_from_argv(&paths, argc, argv,
5237 error = got_object_id_str(&id_str,
5238 got_worktree_get_base_commit_id(worktree));
5242 arg.worktree = worktree;
5243 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5244 arg.diff_context = diff_context;
5245 arg.id_str = id_str;
5246 arg.header_shown = 0;
5247 arg.diff_staged = diff_staged;
5248 arg.ignore_whitespace = ignore_whitespace;
5249 arg.force_text_diff = force_text_diff;
5250 arg.diffstat = show_diffstat ? &dsa : NULL;
5253 arg.outfile = outfile;
5255 error = got_worktree_status(worktree, &paths, repo, 0,
5256 print_diff, &arg, check_cancelled, NULL);
5261 if (show_diffstat && dsa.nfiles > 0) {
5264 if (asprintf(&header, "diffstat %s%s",
5265 diff_staged ? "-s " : "",
5266 got_worktree_get_root_path(worktree)) == -1) {
5267 error = got_error_from_errno("asprintf");
5271 error = print_diffstat(&dsa, header);
5277 error = printfile(outfile);
5281 if (ncommit_args == 1) {
5282 struct got_commit_object *commit;
5283 error = got_object_open_as_commit(&commit, repo, ids[0]);
5287 labels[1] = labels[0];
5289 if (got_object_commit_get_nparents(commit) > 0) {
5290 const struct got_object_id_queue *pids;
5291 struct got_object_qid *pid;
5292 pids = got_object_commit_get_parent_ids(commit);
5293 pid = STAILQ_FIRST(pids);
5294 ids[0] = got_object_id_dup(&pid->id);
5295 if (ids[0] == NULL) {
5296 error = got_error_from_errno(
5297 "got_object_id_dup");
5298 got_object_commit_close(commit);
5301 error = got_object_id_str(&labels[0], ids[0]);
5303 got_object_commit_close(commit);
5308 labels[0] = strdup("/dev/null");
5309 if (labels[0] == NULL) {
5310 error = got_error_from_errno("strdup");
5311 got_object_commit_close(commit);
5316 got_object_commit_close(commit);
5319 if (ncommit_args == 0 && argc > 2) {
5320 error = got_error_msg(GOT_ERR_BAD_PATH,
5321 "path arguments cannot be used when diffing two objects");
5326 error = got_object_get_type(&type1, repo, ids[0]);
5331 error = got_object_get_type(&type2, repo, ids[1]);
5334 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5335 error = got_error(GOT_ERR_OBJ_TYPE);
5338 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5339 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5340 "path arguments cannot be used when diffing blobs");
5344 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5346 struct got_pathlist_entry *new;
5350 error = got_worktree_resolve_path(&p, worktree,
5354 prefix = got_worktree_get_path_prefix(worktree);
5355 while (prefix[0] == '/')
5357 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5358 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5360 error = got_error_from_errno("asprintf");
5366 char *mapped_path, *s;
5367 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5373 in_repo_path = strdup(s);
5374 if (in_repo_path == NULL) {
5375 error = got_error_from_errno("asprintf");
5382 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5383 if (error || new == NULL /* duplicate */)
5390 /* Release work tree lock. */
5391 got_worktree_close(worktree);
5395 fd1 = got_opentempfd();
5397 error = got_error_from_errno("got_opentempfd");
5401 fd2 = got_opentempfd();
5403 error = got_error_from_errno("got_opentempfd");
5407 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5408 case GOT_OBJ_TYPE_BLOB:
5409 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5410 fd1, fd2, ids[0], ids[1], NULL, NULL,
5411 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5412 ignore_whitespace, force_text_diff,
5413 show_diffstat ? &dsa : NULL, repo, outfile);
5415 case GOT_OBJ_TYPE_TREE:
5416 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5417 ids[0], ids[1], &paths, "", "",
5418 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5419 ignore_whitespace, force_text_diff,
5420 show_diffstat ? &dsa : NULL, repo, outfile);
5422 case GOT_OBJ_TYPE_COMMIT:
5423 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5424 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5425 fd1, fd2, ids[0], ids[1], &paths,
5426 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5427 ignore_whitespace, force_text_diff,
5428 show_diffstat ? &dsa : NULL, repo, outfile);
5431 error = got_error(GOT_ERR_OBJ_TYPE);
5436 if (show_diffstat && dsa.nfiles > 0) {
5437 char *header = NULL;
5439 if (asprintf(&header, "diffstat %s %s",
5440 labels[0], labels[1]) == -1) {
5441 error = got_error_from_errno("asprintf");
5445 error = print_diffstat(&dsa, header);
5451 error = printfile(outfile);
5459 got_worktree_close(worktree);
5461 const struct got_error *close_err = got_repo_close(repo);
5466 const struct got_error *pack_err =
5467 got_repo_pack_fds_close(pack_fds);
5471 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5472 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5473 got_ref_list_free(&refs);
5474 if (outfile && fclose(outfile) == EOF && error == NULL)
5475 error = got_error_from_errno("fclose");
5476 if (f1 && fclose(f1) == EOF && error == NULL)
5477 error = got_error_from_errno("fclose");
5478 if (f2 && fclose(f2) == EOF && error == NULL)
5479 error = got_error_from_errno("fclose");
5480 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5481 error = got_error_from_errno("close");
5482 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5483 error = got_error_from_errno("close");
5491 "usage: %s blame [-c commit] [-r repository-path] path\n",
5500 char datebuf[11]; /* YYYY-MM-DD + NUL */
5503 struct blame_cb_args {
5504 struct blame_line *lines;
5508 off_t *line_offsets;
5510 struct got_repository *repo;
5513 static const struct got_error *
5514 blame_cb(void *arg, int nlines, int lineno,
5515 struct got_commit_object *commit, struct got_object_id *id)
5517 const struct got_error *err = NULL;
5518 struct blame_cb_args *a = arg;
5519 struct blame_line *bline;
5521 size_t linesize = 0;
5524 time_t committer_time;
5526 if (nlines != a->nlines ||
5527 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5528 return got_error(GOT_ERR_RANGE);
5530 if (sigint_received)
5531 return got_error(GOT_ERR_ITER_COMPLETED);
5534 return NULL; /* no change in this commit */
5536 /* Annotate this line. */
5537 bline = &a->lines[lineno - 1];
5538 if (bline->annotated)
5540 err = got_object_id_str(&bline->id_str, id);
5544 bline->committer = strdup(got_object_commit_get_committer(commit));
5545 if (bline->committer == NULL) {
5546 err = got_error_from_errno("strdup");
5550 committer_time = got_object_commit_get_committer_time(commit);
5551 if (gmtime_r(&committer_time, &tm) == NULL)
5552 return got_error_from_errno("gmtime_r");
5553 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5555 err = got_error(GOT_ERR_NO_SPACE);
5558 bline->annotated = 1;
5560 /* Print lines annotated so far. */
5561 bline = &a->lines[a->lineno_cur - 1];
5562 if (!bline->annotated)
5565 offset = a->line_offsets[a->lineno_cur - 1];
5566 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5567 err = got_error_from_errno("fseeko");
5571 while (a->lineno_cur <= a->nlines && bline->annotated) {
5572 char *smallerthan, *at, *nl, *committer;
5575 if (getline(&line, &linesize, a->f) == -1) {
5577 err = got_error_from_errno("getline");
5581 committer = bline->committer;
5582 smallerthan = strchr(committer, '<');
5583 if (smallerthan && smallerthan[1] != '\0')
5584 committer = smallerthan + 1;
5585 at = strchr(committer, '@');
5588 len = strlen(committer);
5590 committer[8] = '\0';
5592 nl = strchr(line, '\n');
5595 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5596 bline->id_str, bline->datebuf, committer, line);
5599 bline = &a->lines[a->lineno_cur - 1];
5606 static const struct got_error *
5607 cmd_blame(int argc, char *argv[])
5609 const struct got_error *error;
5610 struct got_repository *repo = NULL;
5611 struct got_worktree *worktree = NULL;
5612 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5613 char *link_target = NULL;
5614 struct got_object_id *obj_id = NULL;
5615 struct got_object_id *commit_id = NULL;
5616 struct got_commit_object *commit = NULL;
5617 struct got_blob_object *blob = NULL;
5618 char *commit_id_str = NULL;
5619 struct blame_cb_args bca;
5620 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5622 int *pack_fds = NULL;
5623 FILE *f1 = NULL, *f2 = NULL;
5625 fd1 = got_opentempfd();
5627 return got_error_from_errno("got_opentempfd");
5629 memset(&bca, 0, sizeof(bca));
5632 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5637 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5640 commit_id_str = optarg;
5643 repo_path = realpath(optarg, NULL);
5644 if (repo_path == NULL)
5645 return got_error_from_errno2("realpath",
5647 got_path_strip_trailing_slashes(repo_path);
5663 cwd = getcwd(NULL, 0);
5665 error = got_error_from_errno("getcwd");
5669 error = got_repo_pack_fds_open(&pack_fds);
5673 if (repo_path == NULL) {
5674 error = got_worktree_open(&worktree, cwd);
5675 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5681 strdup(got_worktree_get_repo_path(worktree));
5682 if (repo_path == NULL) {
5683 error = got_error_from_errno("strdup");
5688 repo_path = strdup(cwd);
5689 if (repo_path == NULL) {
5690 error = got_error_from_errno("strdup");
5696 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5701 const char *prefix = got_worktree_get_path_prefix(worktree);
5704 error = got_worktree_resolve_path(&p, worktree, path);
5707 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5708 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5710 error = got_error_from_errno("asprintf");
5715 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5717 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5720 error = got_repo_map_path(&in_repo_path, repo, path);
5725 if (commit_id_str == NULL) {
5726 struct got_reference *head_ref;
5727 error = got_ref_open(&head_ref, repo, worktree ?
5728 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5731 error = got_ref_resolve(&commit_id, repo, head_ref);
5732 got_ref_close(head_ref);
5736 struct got_reflist_head refs;
5738 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5742 error = got_repo_match_object_id(&commit_id, NULL,
5743 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5744 got_ref_list_free(&refs);
5750 /* Release work tree lock. */
5751 got_worktree_close(worktree);
5755 error = got_object_open_as_commit(&commit, repo, commit_id);
5759 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5764 error = got_object_id_by_path(&obj_id, repo, commit,
5765 link_target ? link_target : in_repo_path);
5769 error = got_object_get_type(&obj_type, repo, obj_id);
5773 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5774 error = got_error_path(link_target ? link_target : in_repo_path,
5779 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5782 bca.f = got_opentemp();
5783 if (bca.f == NULL) {
5784 error = got_error_from_errno("got_opentemp");
5787 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5788 &bca.line_offsets, bca.f, blob);
5789 if (error || bca.nlines == 0)
5792 /* Don't include \n at EOF in the blame line count. */
5793 if (bca.line_offsets[bca.nlines - 1] == filesize)
5796 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5797 if (bca.lines == NULL) {
5798 error = got_error_from_errno("calloc");
5802 bca.nlines_prec = 0;
5810 fd2 = got_opentempfd();
5812 error = got_error_from_errno("got_opentempfd");
5815 fd3 = got_opentempfd();
5817 error = got_error_from_errno("got_opentempfd");
5820 f1 = got_opentemp();
5822 error = got_error_from_errno("got_opentemp");
5825 f2 = got_opentemp();
5827 error = got_error_from_errno("got_opentemp");
5830 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5831 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5832 check_cancelled, NULL, fd2, fd3, f1, f2);
5841 got_object_commit_close(commit);
5843 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5844 error = got_error_from_errno("close");
5845 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5846 error = got_error_from_errno("close");
5847 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5848 error = got_error_from_errno("close");
5849 if (f1 && fclose(f1) == EOF && error == NULL)
5850 error = got_error_from_errno("fclose");
5851 if (f2 && fclose(f2) == EOF && error == NULL)
5852 error = got_error_from_errno("fclose");
5855 got_object_blob_close(blob);
5857 got_worktree_close(worktree);
5859 const struct got_error *close_err = got_repo_close(repo);
5864 const struct got_error *pack_err =
5865 got_repo_pack_fds_close(pack_fds);
5870 for (i = 0; i < bca.nlines; i++) {
5871 struct blame_line *bline = &bca.lines[i];
5872 free(bline->id_str);
5873 free(bline->committer);
5877 free(bca.line_offsets);
5878 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5879 error = got_error_from_errno("fclose");
5886 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5887 "[path]\n", getprogname());
5891 static const struct got_error *
5892 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5893 const char *root_path, struct got_repository *repo)
5895 const struct got_error *err = NULL;
5896 int is_root_path = (strcmp(path, root_path) == 0);
5897 const char *modestr = "";
5898 mode_t mode = got_tree_entry_get_mode(te);
5899 char *link_target = NULL;
5901 path += strlen(root_path);
5902 while (path[0] == '/')
5905 if (got_object_tree_entry_is_submodule(te))
5907 else if (S_ISLNK(mode)) {
5910 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5913 for (i = 0; i < strlen(link_target); i++) {
5914 if (!isprint((unsigned char)link_target[i]))
5915 link_target[i] = '?';
5920 else if (S_ISDIR(mode))
5922 else if (mode & S_IXUSR)
5925 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5926 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5927 link_target ? " -> ": "", link_target ? link_target : "");
5933 static const struct got_error *
5934 print_tree(const char *path, struct got_commit_object *commit,
5935 int show_ids, int recurse, const char *root_path,
5936 struct got_repository *repo)
5938 const struct got_error *err = NULL;
5939 struct got_object_id *tree_id = NULL;
5940 struct got_tree_object *tree = NULL;
5943 err = got_object_id_by_path(&tree_id, repo, commit, path);
5947 err = got_object_open_as_tree(&tree, repo, tree_id);
5950 nentries = got_object_tree_get_nentries(tree);
5951 for (i = 0; i < nentries; i++) {
5952 struct got_tree_entry *te;
5955 if (sigint_received || sigpipe_received)
5958 te = got_object_tree_get_entry(tree, i);
5961 err = got_object_id_str(&id_str,
5962 got_tree_entry_get_id(te));
5965 if (asprintf(&id, "%s ", id_str) == -1) {
5966 err = got_error_from_errno("asprintf");
5972 err = print_entry(te, id, path, root_path, repo);
5977 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5979 if (asprintf(&child_path, "%s%s%s", path,
5980 path[0] == '/' && path[1] == '\0' ? "" : "/",
5981 got_tree_entry_get_name(te)) == -1) {
5982 err = got_error_from_errno("asprintf");
5985 err = print_tree(child_path, commit, show_ids, 1,
5994 got_object_tree_close(tree);
5999 static const struct got_error *
6000 cmd_tree(int argc, char *argv[])
6002 const struct got_error *error;
6003 struct got_repository *repo = NULL;
6004 struct got_worktree *worktree = NULL;
6005 const char *path, *refname = NULL;
6006 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6007 struct got_object_id *commit_id = NULL;
6008 struct got_commit_object *commit = NULL;
6009 char *commit_id_str = NULL;
6010 int show_ids = 0, recurse = 0;
6012 int *pack_fds = NULL;
6015 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6020 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6023 commit_id_str = optarg;
6032 repo_path = realpath(optarg, NULL);
6033 if (repo_path == NULL)
6034 return got_error_from_errno2("realpath",
6036 got_path_strip_trailing_slashes(repo_path);
6054 cwd = getcwd(NULL, 0);
6056 error = got_error_from_errno("getcwd");
6060 error = got_repo_pack_fds_open(&pack_fds);
6064 if (repo_path == NULL) {
6065 error = got_worktree_open(&worktree, cwd);
6066 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6072 strdup(got_worktree_get_repo_path(worktree));
6073 if (repo_path == NULL)
6074 error = got_error_from_errno("strdup");
6078 repo_path = strdup(cwd);
6079 if (repo_path == NULL) {
6080 error = got_error_from_errno("strdup");
6086 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6091 const char *prefix = got_worktree_get_path_prefix(worktree);
6096 error = got_worktree_resolve_path(&p, worktree, path);
6099 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6100 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6102 error = got_error_from_errno("asprintf");
6107 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6111 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6116 error = got_repo_map_path(&in_repo_path, repo, path);
6121 if (commit_id_str == NULL) {
6122 struct got_reference *head_ref;
6124 refname = got_worktree_get_head_ref_name(worktree);
6126 refname = GOT_REF_HEAD;
6127 error = got_ref_open(&head_ref, repo, refname, 0);
6130 error = got_ref_resolve(&commit_id, repo, head_ref);
6131 got_ref_close(head_ref);
6135 struct got_reflist_head refs;
6137 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6141 error = got_repo_match_object_id(&commit_id, NULL,
6142 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6143 got_ref_list_free(&refs);
6149 /* Release work tree lock. */
6150 got_worktree_close(worktree);
6154 error = got_object_open_as_commit(&commit, repo, commit_id);
6158 error = print_tree(in_repo_path, commit, show_ids, recurse,
6159 in_repo_path, repo);
6166 got_object_commit_close(commit);
6168 got_worktree_close(worktree);
6170 const struct got_error *close_err = got_repo_close(repo);
6175 const struct got_error *pack_err =
6176 got_repo_pack_fds_close(pack_fds);
6186 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6187 "[-s status-codes] [path ...]\n", getprogname());
6191 struct got_status_arg {
6196 static const struct got_error *
6197 print_status(void *arg, unsigned char status, unsigned char staged_status,
6198 const char *path, struct got_object_id *blob_id,
6199 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6200 int dirfd, const char *de_name)
6202 struct got_status_arg *st = arg;
6204 if (status == staged_status && (status == GOT_STATUS_DELETE))
6205 status = GOT_STATUS_NO_CHANGE;
6206 if (st != NULL && st->status_codes) {
6207 size_t ncodes = strlen(st->status_codes);
6210 for (i = 0; i < ncodes ; i++) {
6212 if (status == st->status_codes[i] ||
6213 staged_status == st->status_codes[i]) {
6218 if (status == st->status_codes[i] ||
6219 staged_status == st->status_codes[i])
6224 if (st->suppress && j == 0)
6231 printf("%c%c %s\n", status, staged_status, path);
6235 static const struct got_error *
6236 cmd_status(int argc, char *argv[])
6238 const struct got_error *error = NULL;
6239 struct got_repository *repo = NULL;
6240 struct got_worktree *worktree = NULL;
6241 struct got_status_arg st;
6243 struct got_pathlist_head paths;
6244 int ch, i, no_ignores = 0;
6245 int *pack_fds = NULL;
6249 memset(&st, 0, sizeof(st));
6250 st.status_codes = NULL;
6253 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6259 if (st.status_codes != NULL && st.suppress == 0)
6260 option_conflict('S', 's');
6264 for (i = 0; i < strlen(optarg); i++) {
6265 switch (optarg[i]) {
6266 case GOT_STATUS_MODIFY:
6267 case GOT_STATUS_ADD:
6268 case GOT_STATUS_DELETE:
6269 case GOT_STATUS_CONFLICT:
6270 case GOT_STATUS_MISSING:
6271 case GOT_STATUS_OBSTRUCTED:
6272 case GOT_STATUS_UNVERSIONED:
6273 case GOT_STATUS_MODE_CHANGE:
6274 case GOT_STATUS_NONEXISTENT:
6277 errx(1, "invalid status code '%c'",
6281 if (ch == 's' && st.suppress)
6282 option_conflict('s', 'S');
6283 st.status_codes = optarg;
6295 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6299 cwd = getcwd(NULL, 0);
6301 error = got_error_from_errno("getcwd");
6305 error = got_repo_pack_fds_open(&pack_fds);
6309 error = got_worktree_open(&worktree, cwd);
6311 if (error->code == GOT_ERR_NOT_WORKTREE)
6312 error = wrap_not_worktree_error(error, "status", cwd);
6316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6321 error = apply_unveil(got_repo_get_path(repo), 1,
6322 got_worktree_get_root_path(worktree));
6326 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6330 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6331 print_status, &st, check_cancelled, NULL);
6334 const struct got_error *pack_err =
6335 got_repo_pack_fds_close(pack_fds);
6340 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6348 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6349 "[-s reference] [name]\n", getprogname());
6353 static const struct got_error *
6354 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6356 static const struct got_error *err = NULL;
6357 struct got_reflist_head refs;
6358 struct got_reflist_entry *re;
6361 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6362 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6367 TAILQ_FOREACH(re, &refs, entry) {
6369 refstr = got_ref_to_str(re->ref);
6370 if (refstr == NULL) {
6371 err = got_error_from_errno("got_ref_to_str");
6374 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6378 got_ref_list_free(&refs);
6382 static const struct got_error *
6383 delete_ref_by_name(struct got_repository *repo, const char *refname)
6385 const struct got_error *err;
6386 struct got_reference *ref;
6388 err = got_ref_open(&ref, repo, refname, 0);
6392 err = delete_ref(repo, ref);
6397 static const struct got_error *
6398 add_ref(struct got_repository *repo, const char *refname, const char *target)
6400 const struct got_error *err = NULL;
6401 struct got_object_id *id = NULL;
6402 struct got_reference *ref = NULL;
6403 struct got_reflist_head refs;
6406 * Don't let the user create a reference name with a leading '-'.
6407 * While technically a valid reference name, this case is usually
6408 * an unintended typo.
6410 if (refname[0] == '-')
6411 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6414 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6417 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6419 got_ref_list_free(&refs);
6423 err = got_ref_alloc(&ref, refname, id);
6427 err = got_ref_write(ref, repo);
6435 static const struct got_error *
6436 add_symref(struct got_repository *repo, const char *refname, const char *target)
6438 const struct got_error *err = NULL;
6439 struct got_reference *ref = NULL;
6440 struct got_reference *target_ref = NULL;
6443 * Don't let the user create a reference name with a leading '-'.
6444 * While technically a valid reference name, this case is usually
6445 * an unintended typo.
6447 if (refname[0] == '-')
6448 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6450 err = got_ref_open(&target_ref, repo, target, 0);
6454 err = got_ref_alloc_symref(&ref, refname, target_ref);
6458 err = got_ref_write(ref, repo);
6461 got_ref_close(target_ref);
6467 static const struct got_error *
6468 cmd_ref(int argc, char *argv[])
6470 const struct got_error *error = NULL;
6471 struct got_repository *repo = NULL;
6472 struct got_worktree *worktree = NULL;
6473 char *cwd = NULL, *repo_path = NULL;
6474 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6475 const char *obj_arg = NULL, *symref_target= NULL;
6476 char *refname = NULL;
6477 int *pack_fds = NULL;
6479 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6491 repo_path = realpath(optarg, NULL);
6492 if (repo_path == NULL)
6493 return got_error_from_errno2("realpath",
6495 got_path_strip_trailing_slashes(repo_path);
6498 symref_target = optarg;
6509 if (obj_arg && do_list)
6510 option_conflict('c', 'l');
6511 if (obj_arg && do_delete)
6512 option_conflict('c', 'd');
6513 if (obj_arg && symref_target)
6514 option_conflict('c', 's');
6515 if (symref_target && do_delete)
6516 option_conflict('s', 'd');
6517 if (symref_target && do_list)
6518 option_conflict('s', 'l');
6519 if (do_delete && do_list)
6520 option_conflict('d', 'l');
6521 if (sort_by_time && !do_list)
6522 errx(1, "-t option requires -l option");
6528 if (argc != 0 && argc != 1)
6531 refname = strdup(argv[0]);
6532 if (refname == NULL) {
6533 error = got_error_from_errno("strdup");
6540 refname = strdup(argv[0]);
6541 if (refname == NULL) {
6542 error = got_error_from_errno("strdup");
6548 got_path_strip_trailing_slashes(refname);
6551 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6552 "sendfd unveil", NULL) == -1)
6555 cwd = getcwd(NULL, 0);
6557 error = got_error_from_errno("getcwd");
6561 error = got_repo_pack_fds_open(&pack_fds);
6565 if (repo_path == NULL) {
6566 error = got_worktree_open(&worktree, cwd);
6567 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6573 strdup(got_worktree_get_repo_path(worktree));
6574 if (repo_path == NULL)
6575 error = got_error_from_errno("strdup");
6579 repo_path = strdup(cwd);
6580 if (repo_path == NULL) {
6581 error = got_error_from_errno("strdup");
6587 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6593 /* Remove "cpath" promise. */
6594 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6600 error = apply_unveil(got_repo_get_path(repo), do_list,
6601 worktree ? got_worktree_get_root_path(worktree) : NULL);
6606 error = list_refs(repo, refname, sort_by_time);
6608 error = delete_ref_by_name(repo, refname);
6609 else if (symref_target)
6610 error = add_symref(repo, refname, symref_target);
6612 if (obj_arg == NULL)
6614 error = add_ref(repo, refname, obj_arg);
6619 const struct got_error *close_err = got_repo_close(repo);
6624 got_worktree_close(worktree);
6626 const struct got_error *pack_err =
6627 got_repo_pack_fds_close(pack_fds);
6639 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6640 "[-r repository-path] [name]\n", getprogname());
6644 static const struct got_error *
6645 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6646 struct got_reference *ref)
6648 const struct got_error *err = NULL;
6649 const char *refname, *marker = " ";
6652 refname = got_ref_get_name(ref);
6653 if (worktree && strcmp(refname,
6654 got_worktree_get_head_ref_name(worktree)) == 0) {
6655 struct got_object_id *id = NULL;
6657 err = got_ref_resolve(&id, repo, ref);
6660 if (got_object_id_cmp(id,
6661 got_worktree_get_base_commit_id(worktree)) == 0)
6668 if (strncmp(refname, "refs/heads/", 11) == 0)
6670 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6672 if (strncmp(refname, "refs/remotes/", 13) == 0)
6675 refstr = got_ref_to_str(ref);
6677 return got_error_from_errno("got_ref_to_str");
6679 printf("%s%s: %s\n", marker, refname, refstr);
6684 static const struct got_error *
6685 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6687 const char *refname;
6689 if (worktree == NULL)
6690 return got_error(GOT_ERR_NOT_WORKTREE);
6692 refname = got_worktree_get_head_ref_name(worktree);
6694 if (strncmp(refname, "refs/heads/", 11) == 0)
6696 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6699 printf("%s\n", refname);
6704 static const struct got_error *
6705 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6708 static const struct got_error *err = NULL;
6709 struct got_reflist_head refs;
6710 struct got_reflist_entry *re;
6711 struct got_reference *temp_ref = NULL;
6712 int rebase_in_progress, histedit_in_progress;
6717 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6722 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6727 if (rebase_in_progress || histedit_in_progress) {
6728 err = got_ref_open(&temp_ref, repo,
6729 got_worktree_get_head_ref_name(worktree), 0);
6732 list_branch(repo, worktree, temp_ref);
6733 got_ref_close(temp_ref);
6737 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6738 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6743 TAILQ_FOREACH(re, &refs, entry)
6744 list_branch(repo, worktree, re->ref);
6746 got_ref_list_free(&refs);
6748 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6749 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6754 TAILQ_FOREACH(re, &refs, entry)
6755 list_branch(repo, worktree, re->ref);
6757 got_ref_list_free(&refs);
6762 static const struct got_error *
6763 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6764 const char *branch_name)
6766 const struct got_error *err = NULL;
6767 struct got_reference *ref = NULL;
6768 char *refname, *remote_refname = NULL;
6770 if (strncmp(branch_name, "refs/", 5) == 0)
6772 if (strncmp(branch_name, "heads/", 6) == 0)
6774 else if (strncmp(branch_name, "remotes/", 8) == 0)
6777 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6778 return got_error_from_errno("asprintf");
6780 if (asprintf(&remote_refname, "refs/remotes/%s",
6781 branch_name) == -1) {
6782 err = got_error_from_errno("asprintf");
6786 err = got_ref_open(&ref, repo, refname, 0);
6788 const struct got_error *err2;
6789 if (err->code != GOT_ERR_NOT_REF)
6792 * Keep 'err' intact such that if neither branch exists
6793 * we report "refs/heads" rather than "refs/remotes" in
6794 * our error message.
6796 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6803 strcmp(got_worktree_get_head_ref_name(worktree),
6804 got_ref_get_name(ref)) == 0) {
6805 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6806 "will not delete this work tree's current branch");
6810 err = delete_ref(repo, ref);
6815 free(remote_refname);
6819 static const struct got_error *
6820 add_branch(struct got_repository *repo, const char *branch_name,
6821 struct got_object_id *base_commit_id)
6823 const struct got_error *err = NULL;
6824 struct got_reference *ref = NULL;
6825 char *refname = NULL;
6828 * Don't let the user create a branch name with a leading '-'.
6829 * While technically a valid reference name, this case is usually
6830 * an unintended typo.
6832 if (branch_name[0] == '-')
6833 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6835 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6838 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6839 err = got_error_from_errno("asprintf");
6843 err = got_ref_open(&ref, repo, refname, 0);
6845 err = got_error(GOT_ERR_BRANCH_EXISTS);
6847 } else if (err->code != GOT_ERR_NOT_REF)
6850 err = got_ref_alloc(&ref, refname, base_commit_id);
6854 err = got_ref_write(ref, repo);
6862 static const struct got_error *
6863 cmd_branch(int argc, char *argv[])
6865 const struct got_error *error = NULL;
6866 struct got_repository *repo = NULL;
6867 struct got_worktree *worktree = NULL;
6868 char *cwd = NULL, *repo_path = NULL;
6869 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6870 const char *delref = NULL, *commit_id_arg = NULL;
6871 struct got_reference *ref = NULL;
6872 struct got_pathlist_head paths;
6873 struct got_object_id *commit_id = NULL;
6874 char *commit_id_str = NULL;
6875 int *pack_fds = NULL;
6879 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6882 commit_id_arg = optarg;
6894 repo_path = realpath(optarg, NULL);
6895 if (repo_path == NULL)
6896 return got_error_from_errno2("realpath",
6898 got_path_strip_trailing_slashes(repo_path);
6909 if (do_list && delref)
6910 option_conflict('l', 'd');
6911 if (sort_by_time && !do_list)
6912 errx(1, "-t option requires -l option");
6917 if (!do_list && !delref && argc == 0)
6920 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6921 errx(1, "-c option can only be used when creating a branch");
6923 if (do_list || delref) {
6926 } else if (!do_show && argc != 1)
6930 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6931 "sendfd unveil", NULL) == -1)
6934 cwd = getcwd(NULL, 0);
6936 error = got_error_from_errno("getcwd");
6940 error = got_repo_pack_fds_open(&pack_fds);
6944 if (repo_path == NULL) {
6945 error = got_worktree_open(&worktree, cwd);
6946 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6952 strdup(got_worktree_get_repo_path(worktree));
6953 if (repo_path == NULL)
6954 error = got_error_from_errno("strdup");
6958 repo_path = strdup(cwd);
6959 if (repo_path == NULL) {
6960 error = got_error_from_errno("strdup");
6966 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6971 if (do_list || do_show) {
6972 /* Remove "cpath" promise. */
6973 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6979 error = apply_unveil(got_repo_get_path(repo), do_list,
6980 worktree ? got_worktree_get_root_path(worktree) : NULL);
6985 error = show_current_branch(repo, worktree);
6987 error = list_branches(repo, worktree, sort_by_time);
6989 error = delete_branch(repo, worktree, delref);
6991 struct got_reflist_head refs;
6993 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6997 if (commit_id_arg == NULL)
6998 commit_id_arg = worktree ?
6999 got_worktree_get_head_ref_name(worktree) :
7001 error = got_repo_match_object_id(&commit_id, NULL,
7002 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7003 got_ref_list_free(&refs);
7006 error = add_branch(repo, argv[0], commit_id);
7009 if (worktree && do_update) {
7010 struct got_update_progress_arg upa;
7011 char *branch_refname = NULL;
7013 error = got_object_id_str(&commit_id_str, commit_id);
7016 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7020 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7022 error = got_error_from_errno("asprintf");
7025 error = got_ref_open(&ref, repo, branch_refname, 0);
7026 free(branch_refname);
7029 error = switch_head_ref(ref, commit_id, worktree,
7033 error = got_worktree_set_base_commit_id(worktree, repo,
7037 memset(&upa, 0, sizeof(upa));
7038 error = got_worktree_checkout_files(worktree, &paths,
7039 repo, update_progress, &upa, check_cancelled,
7043 if (upa.did_something) {
7044 printf("Updated to %s: %s\n",
7045 got_worktree_get_head_ref_name(worktree),
7048 print_update_progress_stats(&upa);
7055 const struct got_error *close_err = got_repo_close(repo);
7060 got_worktree_close(worktree);
7062 const struct got_error *pack_err =
7063 got_repo_pack_fds_close(pack_fds);
7070 free(commit_id_str);
7071 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7079 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7080 "[-r repository-path] [-s signer-id] name\n", getprogname());
7085 static const struct got_error *
7086 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7088 const struct got_error *err = NULL;
7089 struct got_reflist_entry *re, *se, *new;
7090 struct got_object_id *re_id, *se_id;
7091 struct got_tag_object *re_tag, *se_tag;
7092 time_t re_time, se_time;
7094 STAILQ_FOREACH(re, tags, entry) {
7095 se = STAILQ_FIRST(sorted);
7097 err = got_reflist_entry_dup(&new, re);
7100 STAILQ_INSERT_HEAD(sorted, new, entry);
7103 err = got_ref_resolve(&re_id, repo, re->ref);
7106 err = got_object_open_as_tag(&re_tag, repo, re_id);
7110 re_time = got_object_tag_get_tagger_time(re_tag);
7111 got_object_tag_close(re_tag);
7115 err = got_ref_resolve(&se_id, repo, re->ref);
7118 err = got_object_open_as_tag(&se_tag, repo, se_id);
7122 se_time = got_object_tag_get_tagger_time(se_tag);
7123 got_object_tag_close(se_tag);
7125 if (se_time > re_time) {
7126 err = got_reflist_entry_dup(&new, re);
7129 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7132 se = STAILQ_NEXT(se, entry);
7141 static const struct got_error *
7142 get_tag_refname(char **refname, const char *tag_name)
7144 const struct got_error *err;
7146 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7147 *refname = strdup(tag_name);
7148 if (*refname == NULL)
7149 return got_error_from_errno("strdup");
7150 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7151 err = got_error_from_errno("asprintf");
7159 static const struct got_error *
7160 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7161 const char *allowed_signers, const char *revoked_signers, int verbosity)
7163 static const struct got_error *err = NULL;
7164 struct got_reflist_head refs;
7165 struct got_reflist_entry *re;
7166 char *wanted_refname = NULL;
7171 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7176 struct got_reference *ref;
7177 err = get_tag_refname(&wanted_refname, tag_name);
7180 /* Wanted tag reference should exist. */
7181 err = got_ref_open(&ref, repo, wanted_refname, 0);
7187 TAILQ_FOREACH(re, &refs, entry) {
7188 const char *refname;
7189 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7191 const char *tagger, *ssh_sig = NULL;
7192 char *sig_msg = NULL;
7194 struct got_object_id *id;
7195 struct got_tag_object *tag;
7196 struct got_commit_object *commit = NULL;
7198 refname = got_ref_get_name(re->ref);
7199 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7200 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7203 refstr = got_ref_to_str(re->ref);
7204 if (refstr == NULL) {
7205 err = got_error_from_errno("got_ref_to_str");
7209 err = got_ref_resolve(&id, repo, re->ref);
7212 err = got_object_open_as_tag(&tag, repo, id);
7214 if (err->code != GOT_ERR_OBJ_TYPE) {
7218 /* "lightweight" tag */
7219 err = got_object_open_as_commit(&commit, repo, id);
7224 tagger = got_object_commit_get_committer(commit);
7226 got_object_commit_get_committer_time(commit);
7227 err = got_object_id_str(&id_str, id);
7233 tagger = got_object_tag_get_tagger(tag);
7234 tagger_time = got_object_tag_get_tagger_time(tag);
7235 err = got_object_id_str(&id_str,
7236 got_object_tag_get_object_id(tag));
7241 if (tag && verify_tags) {
7242 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7243 got_object_tag_get_message(tag));
7244 if (ssh_sig && allowed_signers == NULL) {
7245 err = got_error_msg(
7246 GOT_ERR_VERIFY_TAG_SIGNATURE,
7247 "SSH signature verification requires "
7248 "setting allowed_signers in "
7254 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7256 printf("from: %s\n", tagger);
7257 datestr = get_datestr(&tagger_time, datebuf);
7259 printf("date: %s UTC\n", datestr);
7261 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7263 switch (got_object_tag_get_object_type(tag)) {
7264 case GOT_OBJ_TYPE_BLOB:
7265 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7268 case GOT_OBJ_TYPE_TREE:
7269 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7272 case GOT_OBJ_TYPE_COMMIT:
7273 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7276 case GOT_OBJ_TYPE_TAG:
7277 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7287 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7288 allowed_signers, revoked_signers, verbosity);
7289 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7293 printf("signature: %s", sig_msg);
7299 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7302 got_object_commit_close(commit);
7304 tagmsg0 = strdup(got_object_tag_get_message(tag));
7305 got_object_tag_close(tag);
7306 if (tagmsg0 == NULL) {
7307 err = got_error_from_errno("strdup");
7314 line = strsep(&tagmsg, "\n");
7316 printf(" %s\n", line);
7321 got_ref_list_free(&refs);
7322 free(wanted_refname);
7324 if (err == NULL && bad_sigs)
7325 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7329 static const struct got_error *
7330 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7331 const char *tag_name, const char *repo_path)
7333 const struct got_error *err = NULL;
7334 char *template = NULL, *initial_content = NULL;
7335 char *editor = NULL;
7336 int initial_content_len;
7339 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7340 err = got_error_from_errno("asprintf");
7344 initial_content_len = asprintf(&initial_content,
7345 "\n# tagging commit %s as %s\n",
7346 commit_id_str, tag_name);
7347 if (initial_content_len == -1) {
7348 err = got_error_from_errno("asprintf");
7352 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7356 if (write(fd, initial_content, initial_content_len) == -1) {
7357 err = got_error_from_errno2("write", *tagmsg_path);
7361 err = get_editor(&editor);
7364 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7365 initial_content_len, 1);
7367 free(initial_content);
7371 if (fd != -1 && close(fd) == -1 && err == NULL)
7372 err = got_error_from_errno2("close", *tagmsg_path);
7381 static const struct got_error *
7382 add_tag(struct got_repository *repo, const char *tagger,
7383 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7384 const char *signer_id, int verbosity)
7386 const struct got_error *err = NULL;
7387 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7388 char *label = NULL, *commit_id_str = NULL;
7389 struct got_reference *ref = NULL;
7390 char *refname = NULL, *tagmsg = NULL;
7391 char *tagmsg_path = NULL, *tag_id_str = NULL;
7392 int preserve_tagmsg = 0;
7393 struct got_reflist_head refs;
7398 * Don't let the user create a tag name with a leading '-'.
7399 * While technically a valid reference name, this case is usually
7400 * an unintended typo.
7402 if (tag_name[0] == '-')
7403 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7405 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7409 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7410 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7414 err = got_object_id_str(&commit_id_str, commit_id);
7418 err = get_tag_refname(&refname, tag_name);
7421 if (strncmp("refs/tags/", tag_name, 10) == 0)
7424 err = got_ref_open(&ref, repo, refname, 0);
7426 err = got_error(GOT_ERR_TAG_EXISTS);
7428 } else if (err->code != GOT_ERR_NOT_REF)
7431 if (tagmsg_arg == NULL) {
7432 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7433 tag_name, got_repo_get_path(repo));
7435 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7436 tagmsg_path != NULL)
7437 preserve_tagmsg = 1;
7440 /* Editor is done; we can now apply unveil(2) */
7441 err = got_sigs_apply_unveil();
7444 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7449 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7450 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7454 preserve_tagmsg = 1;
7458 err = got_ref_alloc(&ref, refname, tag_id);
7461 preserve_tagmsg = 1;
7465 err = got_ref_write(ref, repo);
7468 preserve_tagmsg = 1;
7472 err = got_object_id_str(&tag_id_str, tag_id);
7475 preserve_tagmsg = 1;
7478 printf("Created tag %s\n", tag_id_str);
7480 if (preserve_tagmsg) {
7481 fprintf(stderr, "%s: tag message preserved in %s\n",
7482 getprogname(), tagmsg_path);
7483 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7484 err = got_error_from_errno2("unlink", tagmsg_path);
7489 free(commit_id_str);
7493 got_ref_list_free(&refs);
7497 static const struct got_error *
7498 cmd_tag(int argc, char *argv[])
7500 const struct got_error *error = NULL;
7501 struct got_repository *repo = NULL;
7502 struct got_worktree *worktree = NULL;
7503 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7504 char *gitconfig_path = NULL, *tagger = NULL;
7505 char *allowed_signers = NULL, *revoked_signers = NULL;
7506 const char *signer_id = NULL;
7507 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7508 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7509 int *pack_fds = NULL;
7511 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7514 commit_id_arg = optarg;
7523 repo_path = realpath(optarg, NULL);
7524 if (repo_path == NULL) {
7525 error = got_error_from_errno2("realpath",
7529 got_path_strip_trailing_slashes(repo_path);
7540 else if (verbosity < 3)
7552 if (do_list || verify_tags) {
7553 if (commit_id_arg != NULL)
7555 "-c option can only be used when creating a tag");
7558 option_conflict('l', 'm');
7560 option_conflict('V', 'm');
7564 option_conflict('l', 's');
7566 option_conflict('V', 's');
7570 } else if (argc != 1)
7577 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7578 "sendfd unveil", NULL) == -1)
7581 cwd = getcwd(NULL, 0);
7583 error = got_error_from_errno("getcwd");
7587 error = got_repo_pack_fds_open(&pack_fds);
7591 if (repo_path == NULL) {
7592 error = got_worktree_open(&worktree, cwd);
7593 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7599 strdup(got_worktree_get_repo_path(worktree));
7600 if (repo_path == NULL)
7601 error = got_error_from_errno("strdup");
7605 repo_path = strdup(cwd);
7606 if (repo_path == NULL) {
7607 error = got_error_from_errno("strdup");
7613 if (do_list || verify_tags) {
7614 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7617 error = get_allowed_signers(&allowed_signers, repo, worktree);
7620 error = get_revoked_signers(&revoked_signers, repo, worktree);
7624 /* Release work tree lock. */
7625 got_worktree_close(worktree);
7630 * Remove "cpath" promise unless needed for signature tmpfile
7634 got_sigs_apply_unveil();
7637 if (pledge("stdio rpath wpath flock proc exec sendfd "
7638 "unveil", NULL) == -1)
7642 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7645 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7646 revoked_signers, verbosity);
7648 error = get_gitconfig_path(&gitconfig_path);
7651 error = got_repo_open(&repo, repo_path, gitconfig_path,
7656 error = get_author(&tagger, repo, worktree);
7659 if (signer_id == NULL)
7660 signer_id = get_signer_id(repo, worktree);
7664 error = got_sigs_apply_unveil();
7668 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7673 if (commit_id_arg == NULL) {
7674 struct got_reference *head_ref;
7675 struct got_object_id *commit_id;
7676 error = got_ref_open(&head_ref, repo,
7677 worktree ? got_worktree_get_head_ref_name(worktree)
7681 error = got_ref_resolve(&commit_id, repo, head_ref);
7682 got_ref_close(head_ref);
7685 error = got_object_id_str(&commit_id_str, commit_id);
7692 /* Release work tree lock. */
7693 got_worktree_close(worktree);
7697 error = add_tag(repo, tagger, tag_name,
7698 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7699 signer_id, verbosity);
7703 const struct got_error *close_err = got_repo_close(repo);
7708 got_worktree_close(worktree);
7710 const struct got_error *pack_err =
7711 got_repo_pack_fds_close(pack_fds);
7717 free(gitconfig_path);
7718 free(commit_id_str);
7720 free(allowed_signers);
7721 free(revoked_signers);
7728 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7732 static const struct got_error *
7733 add_progress(void *arg, unsigned char status, const char *path)
7735 while (path[0] == '/')
7737 printf("%c %s\n", status, path);
7741 static const struct got_error *
7742 cmd_add(int argc, char *argv[])
7744 const struct got_error *error = NULL;
7745 struct got_repository *repo = NULL;
7746 struct got_worktree *worktree = NULL;
7748 struct got_pathlist_head paths;
7749 struct got_pathlist_entry *pe;
7750 int ch, can_recurse = 0, no_ignores = 0;
7751 int *pack_fds = NULL;
7755 while ((ch = getopt(argc, argv, "IR")) != -1) {
7773 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7780 cwd = getcwd(NULL, 0);
7782 error = got_error_from_errno("getcwd");
7786 error = got_repo_pack_fds_open(&pack_fds);
7790 error = got_worktree_open(&worktree, cwd);
7792 if (error->code == GOT_ERR_NOT_WORKTREE)
7793 error = wrap_not_worktree_error(error, "add", cwd);
7797 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7802 error = apply_unveil(got_repo_get_path(repo), 1,
7803 got_worktree_get_root_path(worktree));
7807 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7814 TAILQ_FOREACH(pe, &paths, entry) {
7815 if (asprintf(&ondisk_path, "%s/%s",
7816 got_worktree_get_root_path(worktree),
7818 error = got_error_from_errno("asprintf");
7821 if (lstat(ondisk_path, &sb) == -1) {
7822 if (errno == ENOENT) {
7826 error = got_error_from_errno2("lstat",
7832 if (S_ISDIR(sb.st_mode)) {
7833 error = got_error_msg(GOT_ERR_BAD_PATH,
7834 "adding directories requires -R option");
7840 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7841 NULL, repo, no_ignores);
7844 const struct got_error *close_err = got_repo_close(repo);
7849 got_worktree_close(worktree);
7851 const struct got_error *pack_err =
7852 got_repo_pack_fds_close(pack_fds);
7856 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7864 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7869 static const struct got_error *
7870 print_remove_status(void *arg, unsigned char status,
7871 unsigned char staged_status, const char *path)
7873 while (path[0] == '/')
7875 if (status == GOT_STATUS_NONEXISTENT)
7877 if (status == staged_status && (status == GOT_STATUS_DELETE))
7878 status = GOT_STATUS_NO_CHANGE;
7879 printf("%c%c %s\n", status, staged_status, path);
7883 static const struct got_error *
7884 cmd_remove(int argc, char *argv[])
7886 const struct got_error *error = NULL;
7887 struct got_worktree *worktree = NULL;
7888 struct got_repository *repo = NULL;
7889 const char *status_codes = NULL;
7891 struct got_pathlist_head paths;
7892 struct got_pathlist_entry *pe;
7893 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7894 int ignore_missing_paths = 0;
7895 int *pack_fds = NULL;
7899 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7902 delete_local_mods = 1;
7903 ignore_missing_paths = 1;
7912 for (i = 0; i < strlen(optarg); i++) {
7913 switch (optarg[i]) {
7914 case GOT_STATUS_MODIFY:
7915 delete_local_mods = 1;
7917 case GOT_STATUS_MISSING:
7918 ignore_missing_paths = 1;
7921 errx(1, "invalid status code '%c'",
7925 status_codes = optarg;
7937 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7944 cwd = getcwd(NULL, 0);
7946 error = got_error_from_errno("getcwd");
7950 error = got_repo_pack_fds_open(&pack_fds);
7954 error = got_worktree_open(&worktree, cwd);
7956 if (error->code == GOT_ERR_NOT_WORKTREE)
7957 error = wrap_not_worktree_error(error, "remove", cwd);
7961 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7966 error = apply_unveil(got_repo_get_path(repo), 1,
7967 got_worktree_get_root_path(worktree));
7971 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7978 TAILQ_FOREACH(pe, &paths, entry) {
7979 if (asprintf(&ondisk_path, "%s/%s",
7980 got_worktree_get_root_path(worktree),
7982 error = got_error_from_errno("asprintf");
7985 if (lstat(ondisk_path, &sb) == -1) {
7986 if (errno == ENOENT) {
7990 error = got_error_from_errno2("lstat",
7996 if (S_ISDIR(sb.st_mode)) {
7997 error = got_error_msg(GOT_ERR_BAD_PATH,
7998 "removing directories requires -R option");
8004 error = got_worktree_schedule_delete(worktree, &paths,
8005 delete_local_mods, status_codes, print_remove_status, NULL,
8006 repo, keep_on_disk, ignore_missing_paths);
8009 const struct got_error *close_err = got_repo_close(repo);
8014 got_worktree_close(worktree);
8016 const struct got_error *pack_err =
8017 got_repo_pack_fds_close(pack_fds);
8021 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8029 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8030 "[patchfile]\n", getprogname());
8034 static const struct got_error *
8035 patch_from_stdin(int *patchfd)
8037 const struct got_error *err = NULL;
8040 sig_t sighup, sigint, sigquit;
8042 *patchfd = got_opentempfd();
8044 return got_error_from_errno("got_opentempfd");
8046 sighup = signal(SIGHUP, SIG_DFL);
8047 sigint = signal(SIGINT, SIG_DFL);
8048 sigquit = signal(SIGQUIT, SIG_DFL);
8051 r = read(0, buf, sizeof(buf));
8053 err = got_error_from_errno("read");
8058 if (write(*patchfd, buf, r) == -1) {
8059 err = got_error_from_errno("write");
8064 signal(SIGHUP, sighup);
8065 signal(SIGINT, sigint);
8066 signal(SIGQUIT, sigquit);
8068 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8069 err = got_error_from_errno("lseek");
8079 static const struct got_error *
8080 patch_progress(void *arg, const char *old, const char *new,
8081 unsigned char status, const struct got_error *error, int old_from,
8082 int old_lines, int new_from, int new_lines, int offset,
8083 int ws_mangled, const struct got_error *hunk_err)
8085 const char *path = new == NULL ? old : new;
8087 while (*path == '/')
8091 printf("%c %s\n", status, path);
8094 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8096 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8097 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8098 old_lines, new_from, new_lines);
8099 if (hunk_err != NULL)
8100 printf("%s\n", hunk_err->msg);
8101 else if (offset != 0)
8102 printf("applied with offset %d\n", offset);
8104 printf("hunk contains mangled whitespace\n");
8110 static const struct got_error *
8111 cmd_patch(int argc, char *argv[])
8113 const struct got_error *error = NULL, *close_error = NULL;
8114 struct got_worktree *worktree = NULL;
8115 struct got_repository *repo = NULL;
8116 struct got_reflist_head refs;
8117 struct got_object_id *commit_id = NULL;
8118 const char *commit_id_str = NULL;
8122 int ch, nop = 0, strip = -1, reverse = 0;
8124 int *pack_fds = NULL;
8129 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8130 "unveil", NULL) == -1)
8134 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8137 commit_id_str = optarg;
8143 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8145 errx(1, "pathname strip count is %s: %s",
8161 error = patch_from_stdin(&patchfd);
8164 } else if (argc == 1) {
8165 patchfd = open(argv[0], O_RDONLY);
8166 if (patchfd == -1) {
8167 error = got_error_from_errno2("open", argv[0]);
8170 if (fstat(patchfd, &sb) == -1) {
8171 error = got_error_from_errno2("fstat", argv[0]);
8174 if (!S_ISREG(sb.st_mode)) {
8175 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8181 if ((cwd = getcwd(NULL, 0)) == NULL) {
8182 error = got_error_from_errno("getcwd");
8186 error = got_repo_pack_fds_open(&pack_fds);
8190 error = got_worktree_open(&worktree, cwd);
8194 const char *repo_path = got_worktree_get_repo_path(worktree);
8195 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8199 error = apply_unveil(got_repo_get_path(repo), 0,
8200 got_worktree_get_root_path(worktree));
8204 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8208 if (commit_id_str != NULL) {
8209 error = got_repo_match_object_id(&commit_id, NULL,
8210 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8215 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8216 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8219 got_ref_list_free(&refs);
8222 close_error = got_repo_close(repo);
8224 error = close_error;
8226 if (worktree != NULL) {
8227 close_error = got_worktree_close(worktree);
8229 error = close_error;
8232 const struct got_error *pack_err =
8233 got_repo_pack_fds_close(pack_fds);
8244 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8249 static const struct got_error *
8250 revert_progress(void *arg, unsigned char status, const char *path)
8252 if (status == GOT_STATUS_UNVERSIONED)
8255 while (path[0] == '/')
8257 printf("%c %s\n", status, path);
8261 struct choose_patch_arg {
8262 FILE *patch_script_file;
8266 static const struct got_error *
8267 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8268 int nchanges, const char *action)
8270 const struct got_error *err;
8272 size_t linesize = 0;
8276 case GOT_STATUS_ADD:
8277 printf("A %s\n%s this addition? [y/n] ", path, action);
8279 case GOT_STATUS_DELETE:
8280 printf("D %s\n%s this deletion? [y/n] ", path, action);
8282 case GOT_STATUS_MODIFY:
8283 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8284 return got_error_from_errno("fseek");
8285 printf(GOT_COMMIT_SEP_STR);
8286 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8288 if (linelen == -1 && ferror(patch_file)) {
8289 err = got_error_from_errno("getline");
8294 printf(GOT_COMMIT_SEP_STR);
8295 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8296 path, n, nchanges, action);
8299 return got_error_path(path, GOT_ERR_FILE_STATUS);
8306 static const struct got_error *
8307 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8308 FILE *patch_file, int n, int nchanges)
8310 const struct got_error *err = NULL;
8312 size_t linesize = 0;
8315 struct choose_patch_arg *a = arg;
8317 *choice = GOT_PATCH_CHOICE_NONE;
8319 if (a->patch_script_file) {
8321 err = show_change(status, path, patch_file, n, nchanges,
8325 linelen = getline(&line, &linesize, a->patch_script_file);
8326 if (linelen == -1) {
8327 if (ferror(a->patch_script_file))
8328 return got_error_from_errno("getline");
8331 nl = strchr(line, '\n');
8334 if (strcmp(line, "y") == 0) {
8335 *choice = GOT_PATCH_CHOICE_YES;
8337 } else if (strcmp(line, "n") == 0) {
8338 *choice = GOT_PATCH_CHOICE_NO;
8340 } else if (strcmp(line, "q") == 0 &&
8341 status == GOT_STATUS_MODIFY) {
8342 *choice = GOT_PATCH_CHOICE_QUIT;
8345 printf("invalid response '%s'\n", line);
8350 while (resp != 'y' && resp != 'n' && resp != 'q') {
8351 err = show_change(status, path, patch_file, n, nchanges,
8358 if (status == GOT_STATUS_MODIFY) {
8359 if (resp != 'y' && resp != 'n' && resp != 'q') {
8360 printf("invalid response '%c'\n", resp);
8363 } else if (resp != 'y' && resp != 'n') {
8364 printf("invalid response '%c'\n", resp);
8370 *choice = GOT_PATCH_CHOICE_YES;
8371 else if (resp == 'n')
8372 *choice = GOT_PATCH_CHOICE_NO;
8373 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8374 *choice = GOT_PATCH_CHOICE_QUIT;
8379 struct wt_commitable_path_arg {
8380 struct got_pathlist_head *commit_paths;
8385 * Shortcut work tree status callback to determine if the set of paths scanned
8386 * has at least one versioned path that is being modified and, if not NULL, is
8387 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8388 * soon as a path is passed with a status that satisfies this criteria.
8390 static const struct got_error *
8391 worktree_has_commitable_path(void *arg, unsigned char status,
8392 unsigned char staged_status, const char *path,
8393 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8394 struct got_object_id *commit_id, int dirfd, const char *de_name)
8396 struct wt_commitable_path_arg *a = arg;
8398 if (status == staged_status && (status == GOT_STATUS_DELETE))
8399 status = GOT_STATUS_NO_CHANGE;
8401 if (!(status == GOT_STATUS_NO_CHANGE ||
8402 status == GOT_STATUS_UNVERSIONED) ||
8403 staged_status != GOT_STATUS_NO_CHANGE) {
8404 if (a->commit_paths != NULL) {
8405 struct got_pathlist_entry *pe;
8407 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8408 if (strncmp(path, pe->path,
8409 pe->path_len) == 0) {
8410 *a->has_changes = 1;
8415 *a->has_changes = 1;
8417 if (*a->has_changes)
8418 return got_error(GOT_ERR_FILE_MODIFIED);
8425 * Check that the changeset of the commit identified by id is
8426 * comprised of at least one modified path that is being committed.
8428 static const struct got_error *
8429 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8430 struct got_object_id *id, struct got_worktree *worktree,
8431 struct got_repository *repo)
8433 const struct got_error *err;
8434 struct got_pathlist_head paths;
8435 struct got_commit_object *commit = NULL, *pcommit = NULL;
8436 struct got_tree_object *tree = NULL, *ptree = NULL;
8437 struct got_object_qid *pid;
8441 err = got_object_open_as_commit(&commit, repo, id);
8445 err = got_object_open_as_tree(&tree, repo,
8446 got_object_commit_get_tree_id(commit));
8450 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8452 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8456 err = got_object_open_as_tree(&ptree, repo,
8457 got_object_commit_get_tree_id(pcommit));
8462 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8463 got_diff_tree_collect_changed_paths, &paths, 0);
8467 err = got_worktree_status(worktree, &paths, repo, 0,
8468 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8469 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8471 * At least one changed path in the referenced commit is
8472 * modified in the work tree, that's all we need to know!
8478 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8480 got_object_commit_close(commit);
8482 got_object_commit_close(pcommit);
8484 got_object_tree_close(tree);
8486 got_object_tree_close(ptree);
8491 * Remove any "logmsg" reference comprised entirely of paths that have
8492 * been reverted in this work tree. If any path in the logmsg ref changeset
8493 * remains in a changed state in the worktree, do not remove the reference.
8495 static const struct got_error *
8496 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8498 const struct got_error *err;
8499 struct got_reflist_head refs;
8500 struct got_reflist_entry *re;
8501 struct got_commit_object *commit = NULL;
8502 struct got_object_id *commit_id = NULL;
8503 struct wt_commitable_path_arg wcpa;
8504 char *uuidstr = NULL;
8508 err = got_worktree_get_uuid(&uuidstr, worktree);
8512 err = got_ref_list(&refs, repo, "refs/got/worktree",
8513 got_ref_cmp_by_name, repo);
8517 TAILQ_FOREACH(re, &refs, entry) {
8518 const char *refname;
8519 int has_changes = 0;
8521 refname = got_ref_get_name(re->ref);
8523 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8524 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8525 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8526 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8527 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8528 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8532 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8533 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8537 err = got_repo_match_object_id(&commit_id, NULL, refname,
8538 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8542 err = got_object_open_as_commit(&commit, repo, commit_id);
8546 wcpa.commit_paths = NULL;
8547 wcpa.has_changes = &has_changes;
8549 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8555 err = got_ref_delete(re->ref, repo);
8560 got_object_commit_close(commit);
8569 got_ref_list_free(&refs);
8571 got_object_commit_close(commit);
8575 static const struct got_error *
8576 cmd_revert(int argc, char *argv[])
8578 const struct got_error *error = NULL;
8579 struct got_worktree *worktree = NULL;
8580 struct got_repository *repo = NULL;
8581 char *cwd = NULL, *path = NULL;
8582 struct got_pathlist_head paths;
8583 struct got_pathlist_entry *pe;
8584 int ch, can_recurse = 0, pflag = 0;
8585 FILE *patch_script_file = NULL;
8586 const char *patch_script_path = NULL;
8587 struct choose_patch_arg cpa;
8588 int *pack_fds = NULL;
8592 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8595 patch_script_path = optarg;
8613 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8614 "unveil", NULL) == -1)
8619 if (patch_script_path && !pflag)
8620 errx(1, "-F option can only be used together with -p option");
8622 cwd = getcwd(NULL, 0);
8624 error = got_error_from_errno("getcwd");
8628 error = got_repo_pack_fds_open(&pack_fds);
8632 error = got_worktree_open(&worktree, cwd);
8634 if (error->code == GOT_ERR_NOT_WORKTREE)
8635 error = wrap_not_worktree_error(error, "revert", cwd);
8639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8644 if (patch_script_path) {
8645 patch_script_file = fopen(patch_script_path, "re");
8646 if (patch_script_file == NULL) {
8647 error = got_error_from_errno2("fopen",
8654 * XXX "c" perm needed on repo dir to delete merge references.
8656 error = apply_unveil(got_repo_get_path(repo), 0,
8657 got_worktree_get_root_path(worktree));
8661 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8668 TAILQ_FOREACH(pe, &paths, entry) {
8669 if (asprintf(&ondisk_path, "%s/%s",
8670 got_worktree_get_root_path(worktree),
8672 error = got_error_from_errno("asprintf");
8675 if (lstat(ondisk_path, &sb) == -1) {
8676 if (errno == ENOENT) {
8680 error = got_error_from_errno2("lstat",
8686 if (S_ISDIR(sb.st_mode)) {
8687 error = got_error_msg(GOT_ERR_BAD_PATH,
8688 "reverting directories requires -R option");
8694 cpa.patch_script_file = patch_script_file;
8695 cpa.action = "revert";
8696 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8697 pflag ? choose_patch : NULL, &cpa, repo);
8699 error = rm_logmsg_ref(worktree, repo);
8701 if (patch_script_file && fclose(patch_script_file) == EOF &&
8703 error = got_error_from_errno2("fclose", patch_script_path);
8705 const struct got_error *close_err = got_repo_close(repo);
8710 got_worktree_close(worktree);
8712 const struct got_error *pack_err =
8713 got_repo_pack_fds_close(pack_fds);
8717 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8726 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8727 "[-m message] [path ...]\n", getprogname());
8731 struct collect_commit_logmsg_arg {
8732 const char *cmdline_log;
8733 const char *prepared_log;
8734 const char *merged_log;
8735 int non_interactive;
8737 const char *worktree_path;
8738 const char *branch_name;
8739 const char *repo_path;
8744 static const struct got_error *
8745 read_prepared_logmsg(char **logmsg, const char *path)
8747 const struct got_error *err = NULL;
8753 memset(&sb, 0, sizeof(sb));
8755 f = fopen(path, "re");
8757 return got_error_from_errno2("fopen", path);
8759 if (fstat(fileno(f), &sb) == -1) {
8760 err = got_error_from_errno2("fstat", path);
8763 if (sb.st_size == 0) {
8764 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8768 *logmsg = malloc(sb.st_size + 1);
8769 if (*logmsg == NULL) {
8770 err = got_error_from_errno("malloc");
8774 r = fread(*logmsg, 1, sb.st_size, f);
8775 if (r != sb.st_size) {
8777 err = got_error_from_errno2("fread", path);
8779 err = got_error(GOT_ERR_IO);
8782 (*logmsg)[sb.st_size] = '\0';
8784 if (fclose(f) == EOF && err == NULL)
8785 err = got_error_from_errno2("fclose", path);
8793 static const struct got_error *
8794 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8795 const char *diff_path, char **logmsg, void *arg)
8797 char *initial_content = NULL;
8798 struct got_pathlist_entry *pe;
8799 const struct got_error *err = NULL;
8800 char *template = NULL;
8801 char *prepared_msg = NULL, *merged_msg = NULL;
8802 struct collect_commit_logmsg_arg *a = arg;
8803 int initial_content_len;
8807 /* if a message was specified on the command line, just use it */
8808 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8809 len = strlen(a->cmdline_log) + 1;
8810 *logmsg = malloc(len + 1);
8811 if (*logmsg == NULL)
8812 return got_error_from_errno("malloc");
8813 strlcpy(*logmsg, a->cmdline_log, len);
8815 } else if (a->prepared_log != NULL && a->non_interactive)
8816 return read_prepared_logmsg(logmsg, a->prepared_log);
8818 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8819 return got_error_from_errno("asprintf");
8821 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8825 if (a->prepared_log) {
8826 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8829 } else if (a->merged_log) {
8830 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8835 initial_content_len = asprintf(&initial_content,
8836 "%s%s\n# changes to be committed on branch %s:\n",
8837 prepared_msg ? prepared_msg : "",
8838 merged_msg ? merged_msg : "", a->branch_name);
8839 if (initial_content_len == -1) {
8840 err = got_error_from_errno("asprintf");
8844 if (write(fd, initial_content, initial_content_len) == -1) {
8845 err = got_error_from_errno2("write", a->logmsg_path);
8849 TAILQ_FOREACH(pe, commitable_paths, entry) {
8850 struct got_commitable *ct = pe->data;
8851 dprintf(fd, "# %c %s\n",
8852 got_commitable_get_status(ct),
8853 got_commitable_get_path(ct));
8857 dprintf(fd, "# detailed changes can be viewed in %s\n",
8861 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8862 initial_content_len, a->prepared_log ? 0 : 1);
8864 free(initial_content);
8869 if (fd != -1 && close(fd) == -1 && err == NULL)
8870 err = got_error_from_errno2("close", a->logmsg_path);
8872 /* Editor is done; we can now apply unveil(2) */
8874 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8882 static const struct got_error *
8883 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8884 const char *type, int has_content)
8886 const struct got_error *err = NULL;
8887 char *logmsg = NULL;
8889 err = got_object_commit_get_logmsg(&logmsg, commit);
8893 if (fprintf(f, "%s# log message of %s commit %s:%s",
8894 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8895 err = got_ferror(f, GOT_ERR_IO);
8902 * Lookup "logmsg" references of backed-out and cherrypicked commits
8903 * belonging to the current work tree. If found, and the worktree has
8904 * at least one modified file that was changed in the referenced commit,
8905 * add its log message to a new temporary file at *logmsg_path.
8906 * Add all refs found to matched_refs to be scheduled for removal on
8907 * successful commit.
8909 static const struct got_error *
8910 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8911 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8912 struct got_repository *repo)
8914 const struct got_error *err;
8915 struct got_commit_object *commit = NULL;
8916 struct got_object_id *id = NULL;
8917 struct got_reflist_head refs;
8918 struct got_reflist_entry *re, *re_match;
8920 char *uuidstr = NULL;
8921 int added_logmsg = 0;
8925 *logmsg_path = NULL;
8927 err = got_worktree_get_uuid(&uuidstr, worktree);
8931 err = got_ref_list(&refs, repo, "refs/got/worktree",
8932 got_ref_cmp_by_name, repo);
8936 TAILQ_FOREACH(re, &refs, entry) {
8937 const char *refname, *type;
8938 struct wt_commitable_path_arg wcpa;
8941 refname = got_ref_get_name(re->ref);
8943 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8944 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8945 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8946 type = "cherrypicked";
8947 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8948 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8949 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8950 type = "backed-out";
8954 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8955 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8959 err = got_repo_match_object_id(&id, NULL, refname,
8960 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8964 err = got_object_open_as_commit(&commit, repo, id);
8968 wcpa.commit_paths = paths;
8969 wcpa.has_changes = &add_logmsg;
8971 err = commit_path_changed_in_worktree(&wcpa, id,
8978 err = got_opentemp_named(logmsg_path, &f,
8979 "got-commit-logmsg", "");
8983 err = cat_logmsg(f, commit, refname, type,
8990 err = got_reflist_entry_dup(&re_match, re);
8993 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
8996 got_object_commit_close(commit);
9005 got_ref_list_free(&refs);
9007 got_object_commit_close(commit);
9008 if (f && fclose(f) == EOF && err == NULL)
9009 err = got_error_from_errno("fclose");
9010 if (!added_logmsg) {
9011 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9012 err = got_error_from_errno2("unlink", *logmsg_path);
9013 *logmsg_path = NULL;
9018 static const struct got_error *
9019 cmd_commit(int argc, char *argv[])
9021 const struct got_error *error = NULL;
9022 struct got_worktree *worktree = NULL;
9023 struct got_repository *repo = NULL;
9024 char *cwd = NULL, *id_str = NULL;
9025 struct got_object_id *id = NULL;
9026 const char *logmsg = NULL;
9027 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9028 struct collect_commit_logmsg_arg cl_arg;
9029 const char *author = NULL;
9030 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9031 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9032 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9034 struct got_pathlist_head paths;
9035 struct got_reflist_head refs;
9036 struct got_reflist_entry *re;
9037 int *pack_fds = NULL;
9041 cl_arg.logmsg_path = NULL;
9043 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9047 error = valid_author(author);
9053 option_conflict('F', 'm');
9054 prepared_logmsg = realpath(optarg, NULL);
9055 if (prepared_logmsg == NULL)
9056 return got_error_from_errno2("realpath",
9060 if (prepared_logmsg)
9061 option_conflict('m', 'F');
9065 non_interactive = 1;
9071 allow_bad_symlinks = 1;
9083 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9084 "unveil", NULL) == -1)
9087 cwd = getcwd(NULL, 0);
9089 error = got_error_from_errno("getcwd");
9093 error = got_repo_pack_fds_open(&pack_fds);
9097 error = got_worktree_open(&worktree, cwd);
9099 if (error->code == GOT_ERR_NOT_WORKTREE)
9100 error = wrap_not_worktree_error(error, "commit", cwd);
9104 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9107 if (rebase_in_progress) {
9108 error = got_error(GOT_ERR_REBASING);
9112 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9117 error = get_gitconfig_path(&gitconfig_path);
9120 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9121 gitconfig_path, pack_fds);
9125 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9128 if (merge_in_progress) {
9129 error = got_error(GOT_ERR_MERGE_BUSY);
9133 error = get_author(&committer, repo, worktree);
9137 if (author != NULL && !strcmp(committer, author)) {
9138 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
9146 * unveil(2) traverses exec(2); if an editor is used we have
9147 * to apply unveil after the log message has been written.
9149 if (logmsg == NULL || strlen(logmsg) == 0)
9150 error = get_editor(&editor);
9152 error = apply_unveil(got_repo_get_path(repo), 0,
9153 got_worktree_get_root_path(worktree));
9157 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9161 if (prepared_logmsg == NULL) {
9162 error = lookup_logmsg_ref(&merged_logmsg,
9163 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9168 cl_arg.editor = editor;
9169 cl_arg.cmdline_log = logmsg;
9170 cl_arg.prepared_log = prepared_logmsg;
9171 cl_arg.merged_log = merged_logmsg;
9172 cl_arg.non_interactive = non_interactive;
9173 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9174 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9175 if (!histedit_in_progress) {
9176 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9177 error = got_error(GOT_ERR_COMMIT_BRANCH);
9180 cl_arg.branch_name += 11;
9182 cl_arg.repo_path = got_repo_get_path(repo);
9183 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9184 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9185 print_status, NULL, repo);
9187 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9188 cl_arg.logmsg_path != NULL)
9189 preserve_logmsg = 1;
9193 error = got_object_id_str(&id_str, id);
9196 printf("Created commit %s\n", id_str);
9198 TAILQ_FOREACH(re, &refs, entry) {
9199 error = got_ref_delete(re->ref, repo);
9205 if (preserve_logmsg) {
9206 fprintf(stderr, "%s: log message preserved in %s\n",
9207 getprogname(), cl_arg.logmsg_path);
9208 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9210 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9211 free(cl_arg.logmsg_path);
9212 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9213 error = got_error_from_errno2("unlink", merged_logmsg);
9214 free(merged_logmsg);
9216 const struct got_error *close_err = got_repo_close(repo);
9221 got_worktree_close(worktree);
9223 const struct got_error *pack_err =
9224 got_repo_pack_fds_close(pack_fds);
9228 got_ref_list_free(&refs);
9229 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9232 free(gitconfig_path);
9235 free(prepared_logmsg);
9242 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9243 "[-r repository-path] [-t tag] [remote-repository]\n",
9249 print_load_info(int print_colored, int print_found, int print_trees,
9250 int ncolored, int nfound, int ntrees)
9252 if (print_colored) {
9253 printf("%d commit%s colored", ncolored,
9254 ncolored == 1 ? "" : "s");
9257 printf("%s%d object%s found",
9258 ncolored > 0 ? "; " : "",
9259 nfound, nfound == 1 ? "" : "s");
9262 printf("; %d tree%s scanned", ntrees,
9263 ntrees == 1 ? "" : "s");
9267 struct got_send_progress_arg {
9268 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9275 int last_nobj_total;
9279 int printed_something;
9281 struct got_pathlist_head *delete_branches;
9284 static const struct got_error *
9285 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9286 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9287 int nobj_written, off_t bytes_sent, const char *refname,
9288 const char *errmsg, int success)
9290 struct got_send_progress_arg *a = arg;
9291 char scaled_packsize[FMT_SCALED_STRSIZE];
9292 char scaled_sent[FMT_SCALED_STRSIZE];
9293 int p_deltify = 0, p_written = 0, p_sent = 0;
9294 int print_colored = 0, print_found = 0, print_trees = 0;
9295 int print_searching = 0, print_total = 0;
9296 int print_deltify = 0, print_written = 0, print_sent = 0;
9298 if (a->verbosity < 0)
9302 const char *status = success ? "accepted" : "rejected";
9305 struct got_pathlist_entry *pe;
9306 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9307 const char *branchname = pe->path;
9308 if (got_path_cmp(branchname, refname,
9309 strlen(branchname), strlen(refname)) == 0) {
9311 a->sent_something = 1;
9317 if (a->printed_something)
9319 printf("Server has %s %s", status, refname);
9321 printf(": %s", errmsg);
9322 a->printed_something = 1;
9326 if (a->last_ncolored != ncolored) {
9328 a->last_ncolored = ncolored;
9331 if (a->last_nfound != nfound) {
9334 a->last_nfound = nfound;
9337 if (a->last_ntrees != ntrees) {
9341 a->last_ntrees = ntrees;
9344 if ((print_colored || print_found || print_trees) &&
9347 print_load_info(print_colored, print_found, print_trees,
9348 ncolored, nfound, ntrees);
9349 a->printed_something = 1;
9352 } else if (!a->loading_done) {
9354 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9356 a->loading_done = 1;
9359 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9360 return got_error_from_errno("fmt_scaled");
9361 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9362 return got_error_from_errno("fmt_scaled");
9364 if (a->last_ncommits != ncommits) {
9365 print_searching = 1;
9366 a->last_ncommits = ncommits;
9369 if (a->last_nobj_total != nobj_total) {
9370 print_searching = 1;
9372 a->last_nobj_total = nobj_total;
9375 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9376 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9377 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9378 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9379 return got_error(GOT_ERR_NO_SPACE);
9382 if (nobj_deltify > 0 || nobj_written > 0) {
9383 if (nobj_deltify > 0) {
9384 p_deltify = (nobj_deltify * 100) / nobj_total;
9385 if (p_deltify != a->last_p_deltify) {
9386 a->last_p_deltify = p_deltify;
9387 print_searching = 1;
9392 if (nobj_written > 0) {
9393 p_written = (nobj_written * 100) / nobj_total;
9394 if (p_written != a->last_p_written) {
9395 a->last_p_written = p_written;
9396 print_searching = 1;
9404 if (bytes_sent > 0) {
9405 p_sent = (bytes_sent * 100) / packfile_size;
9406 if (p_sent != a->last_p_sent) {
9407 a->last_p_sent = p_sent;
9408 print_searching = 1;
9414 a->sent_something = 1;
9417 if (print_searching || print_total || print_deltify || print_written ||
9420 if (print_searching)
9421 printf("packing %d reference%s", ncommits,
9422 ncommits == 1 ? "" : "s");
9424 printf("; %d object%s", nobj_total,
9425 nobj_total == 1 ? "" : "s");
9427 printf("; deltify: %d%%", p_deltify);
9429 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9430 scaled_packsize, p_sent);
9431 else if (print_written)
9432 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9433 scaled_packsize, p_written);
9434 if (print_searching || print_total || print_deltify ||
9435 print_written || print_sent) {
9436 a->printed_something = 1;
9442 static const struct got_error *
9443 cmd_send(int argc, char *argv[])
9445 const struct got_error *error = NULL;
9446 char *cwd = NULL, *repo_path = NULL;
9447 const char *remote_name;
9448 char *proto = NULL, *host = NULL, *port = NULL;
9449 char *repo_name = NULL, *server_path = NULL;
9450 const struct got_remote_repo *remotes, *remote = NULL;
9451 int nremotes, nbranches = 0, ndelete_branches = 0;
9452 struct got_repository *repo = NULL;
9453 struct got_worktree *worktree = NULL;
9454 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9455 struct got_pathlist_head branches;
9456 struct got_pathlist_head tags;
9457 struct got_reflist_head all_branches;
9458 struct got_reflist_head all_tags;
9459 struct got_pathlist_head delete_args;
9460 struct got_pathlist_head delete_branches;
9461 struct got_reflist_entry *re;
9462 struct got_pathlist_entry *pe;
9463 int i, ch, sendfd = -1, sendstatus;
9465 struct got_send_progress_arg spa;
9466 int verbosity = 0, overwrite_refs = 0;
9467 int send_all_branches = 0, send_all_tags = 0;
9468 struct got_reference *ref = NULL;
9469 int *pack_fds = NULL;
9471 TAILQ_INIT(&branches);
9473 TAILQ_INIT(&all_branches);
9474 TAILQ_INIT(&all_tags);
9475 TAILQ_INIT(&delete_args);
9476 TAILQ_INIT(&delete_branches);
9478 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9481 send_all_branches = 1;
9484 error = got_pathlist_append(&branches, optarg, NULL);
9490 error = got_pathlist_append(&delete_args, optarg, NULL);
9501 repo_path = realpath(optarg, NULL);
9502 if (repo_path == NULL)
9503 return got_error_from_errno2("realpath",
9505 got_path_strip_trailing_slashes(repo_path);
9511 error = got_pathlist_append(&tags, optarg, NULL);
9518 else if (verbosity < 3)
9529 if (send_all_branches && !TAILQ_EMPTY(&branches))
9530 option_conflict('a', 'b');
9531 if (send_all_tags && !TAILQ_EMPTY(&tags))
9532 option_conflict('T', 't');
9536 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9538 remote_name = argv[0];
9542 cwd = getcwd(NULL, 0);
9544 error = got_error_from_errno("getcwd");
9548 error = got_repo_pack_fds_open(&pack_fds);
9552 if (repo_path == NULL) {
9553 error = got_worktree_open(&worktree, cwd);
9554 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9560 strdup(got_worktree_get_repo_path(worktree));
9561 if (repo_path == NULL)
9562 error = got_error_from_errno("strdup");
9566 repo_path = strdup(cwd);
9567 if (repo_path == NULL) {
9568 error = got_error_from_errno("strdup");
9574 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9579 worktree_conf = got_worktree_get_gotconfig(worktree);
9580 if (worktree_conf) {
9581 got_gotconfig_get_remotes(&nremotes, &remotes,
9583 for (i = 0; i < nremotes; i++) {
9584 if (strcmp(remotes[i].name, remote_name) == 0) {
9585 remote = &remotes[i];
9591 if (remote == NULL) {
9592 repo_conf = got_repo_get_gotconfig(repo);
9594 got_gotconfig_get_remotes(&nremotes, &remotes,
9596 for (i = 0; i < nremotes; i++) {
9597 if (strcmp(remotes[i].name, remote_name) == 0) {
9598 remote = &remotes[i];
9604 if (remote == NULL) {
9605 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9606 for (i = 0; i < nremotes; i++) {
9607 if (strcmp(remotes[i].name, remote_name) == 0) {
9608 remote = &remotes[i];
9613 if (remote == NULL) {
9614 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9618 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9619 &repo_name, remote->send_url);
9623 if (strcmp(proto, "git") == 0) {
9625 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9626 "sendfd dns inet unveil", NULL) == -1)
9629 } else if (strcmp(proto, "git+ssh") == 0 ||
9630 strcmp(proto, "ssh") == 0) {
9632 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9633 "sendfd unveil", NULL) == -1)
9636 } else if (strcmp(proto, "http") == 0 ||
9637 strcmp(proto, "git+http") == 0) {
9638 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9641 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9645 error = got_dial_apply_unveil(proto);
9649 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9653 if (send_all_branches) {
9654 error = got_ref_list(&all_branches, repo, "refs/heads",
9655 got_ref_cmp_by_name, NULL);
9658 TAILQ_FOREACH(re, &all_branches, entry) {
9659 const char *branchname = got_ref_get_name(re->ref);
9660 error = got_pathlist_append(&branches,
9666 } else if (nbranches == 0) {
9667 for (i = 0; i < remote->nsend_branches; i++) {
9668 error = got_pathlist_append(&branches,
9669 remote->send_branches[i], NULL);
9675 if (send_all_tags) {
9676 error = got_ref_list(&all_tags, repo, "refs/tags",
9677 got_ref_cmp_by_name, NULL);
9680 TAILQ_FOREACH(re, &all_tags, entry) {
9681 const char *tagname = got_ref_get_name(re->ref);
9682 error = got_pathlist_append(&tags,
9690 * To prevent accidents only branches in refs/heads/ can be deleted
9691 * with 'got send -d'.
9692 * Deleting anything else requires local repository access or Git.
9694 TAILQ_FOREACH(pe, &delete_args, entry) {
9695 const char *branchname = pe->path;
9697 struct got_pathlist_entry *new;
9698 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9699 s = strdup(branchname);
9701 error = got_error_from_errno("strdup");
9705 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9706 error = got_error_from_errno("asprintf");
9710 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9711 if (error || new == NULL /* duplicate */)
9718 if (nbranches == 0 && ndelete_branches == 0) {
9719 struct got_reference *head_ref;
9721 error = got_ref_open(&head_ref, repo,
9722 got_worktree_get_head_ref_name(worktree), 0);
9724 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9727 if (got_ref_is_symbolic(head_ref)) {
9728 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9729 got_ref_close(head_ref);
9734 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9741 if (verbosity >= 0) {
9742 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9743 remote->name, proto, host,
9744 port ? ":" : "", port ? port : "",
9745 *server_path == '/' ? "" : "/", server_path);
9748 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9749 server_path, verbosity);
9753 memset(&spa, 0, sizeof(spa));
9754 spa.last_scaled_packsize[0] = '\0';
9755 spa.last_p_deltify = -1;
9756 spa.last_p_written = -1;
9757 spa.verbosity = verbosity;
9758 spa.delete_branches = &delete_branches;
9759 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9760 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9761 check_cancelled, NULL);
9762 if (spa.printed_something)
9766 if (!spa.sent_something && verbosity >= 0)
9767 printf("Already up-to-date\n");
9770 if (kill(sendpid, SIGTERM) == -1)
9771 error = got_error_from_errno("kill");
9772 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9773 error = got_error_from_errno("waitpid");
9775 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9776 error = got_error_from_errno("close");
9778 const struct got_error *close_err = got_repo_close(repo);
9783 got_worktree_close(worktree);
9785 const struct got_error *pack_err =
9786 got_repo_pack_fds_close(pack_fds);
9792 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9793 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9794 got_ref_list_free(&all_branches);
9795 got_ref_list_free(&all_tags);
9796 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9797 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9809 * Print and if delete is set delete all ref_prefix references.
9810 * If wanted_ref is not NULL, only print or delete this reference.
9812 static const struct got_error *
9813 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9814 const char *wanted_ref, int delete, struct got_worktree *worktree,
9815 struct got_repository *repo)
9817 const struct got_error *err;
9818 struct got_pathlist_head paths;
9819 struct got_reflist_head refs;
9820 struct got_reflist_entry *re;
9821 struct got_reflist_object_id_map *refs_idmap = NULL;
9822 struct got_commit_object *commit = NULL;
9823 struct got_object_id *id = NULL;
9824 const char *header_prefix;
9825 char *uuidstr = NULL;
9831 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9835 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9839 if (worktree != NULL) {
9840 err = got_worktree_get_uuid(&uuidstr, worktree);
9846 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9850 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9851 header_prefix = "backout";
9853 header_prefix = "cherrypick";
9855 TAILQ_FOREACH(re, &refs, entry) {
9856 const char *refname, *wt;
9858 refname = got_ref_get_name(re->ref);
9860 err = check_cancelled(NULL);
9864 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9865 refname += prefix_len + 1; /* skip '-' delimiter */
9871 if (worktree == NULL || strncmp(refname, uuidstr,
9872 GOT_WORKTREE_UUID_STRLEN) == 0)
9873 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9877 err = got_repo_match_object_id(&id, NULL, refname,
9878 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9882 err = got_object_open_as_commit(&commit, repo, id);
9887 found = strncmp(wanted_ref, refname,
9888 strlen(wanted_ref)) == 0;
9889 if (wanted_ref && !found) {
9890 struct got_reflist_head *ci_refs;
9892 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9896 char *refs_str = NULL;
9897 char const *r = NULL;
9899 err = build_refs_str(&refs_str, ci_refs, id,
9906 if (strncmp(r, wanted_ref,
9907 strlen(wanted_ref)) == 0) {
9919 if (wanted_ref == NULL || found) {
9921 err = got_ref_delete(re->ref, repo);
9924 printf("Deleted: ");
9925 err = print_commit_oneline(commit, id, repo,
9929 * Print paths modified by commit to help
9930 * associate commits with worktree changes.
9932 err = get_changed_paths(&paths, commit,
9937 err = print_commit(commit, id, repo, NULL,
9938 &paths, NULL, 0, 0, refs_idmap, NULL,
9940 got_pathlist_free(&paths,
9941 GOT_PATHLIST_FREE_ALL);
9943 if (worktree == NULL)
9944 printf("work tree: %.*s\n\n",
9945 GOT_WORKTREE_UUID_STRLEN, wt);
9951 got_object_commit_close(commit);
9957 if (wanted_ref != NULL && !found)
9958 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
9963 got_ref_list_free(&refs);
9964 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
9966 got_reflist_object_id_map_free(refs_idmap);
9968 got_object_commit_close(commit);
9973 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
9974 * identified by id for log messages to prepopulate the editor on commit.
9976 static const struct got_error *
9977 logmsg_ref(struct got_object_id *id, const char *prefix,
9978 struct got_worktree *worktree, struct got_repository *repo)
9980 const struct got_error *err = NULL;
9981 char *idstr, *ref = NULL, *refname = NULL;
9982 int histedit_in_progress;
9983 int rebase_in_progress, merge_in_progress;
9986 * Silenty refuse to create merge reference if any histedit, merge,
9987 * or rebase operation is in progress.
9989 err = got_worktree_histedit_in_progress(&histedit_in_progress,
9993 if (histedit_in_progress)
9996 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9999 if (rebase_in_progress)
10002 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10006 if (merge_in_progress)
10009 err = got_object_id_str(&idstr, id);
10013 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10017 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10018 err = got_error_from_errno("asprintf");
10022 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10032 usage_cherrypick(void)
10034 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10039 static const struct got_error *
10040 cmd_cherrypick(int argc, char *argv[])
10042 const struct got_error *error = NULL;
10043 struct got_worktree *worktree = NULL;
10044 struct got_repository *repo = NULL;
10045 char *cwd = NULL, *commit_id_str = NULL;
10046 struct got_object_id *commit_id = NULL;
10047 struct got_commit_object *commit = NULL;
10048 struct got_object_qid *pid;
10049 int ch, list_refs = 0, remove_refs = 0;
10050 struct got_update_progress_arg upa;
10051 int *pack_fds = NULL;
10053 while ((ch = getopt(argc, argv, "lX")) != -1) {
10062 usage_cherrypick();
10071 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10072 "unveil", NULL) == -1)
10075 if (list_refs || remove_refs) {
10076 if (argc != 0 && argc != 1)
10077 usage_cherrypick();
10078 } else if (argc != 1)
10079 usage_cherrypick();
10080 if (list_refs && remove_refs)
10081 option_conflict('l', 'X');
10083 cwd = getcwd(NULL, 0);
10085 error = got_error_from_errno("getcwd");
10089 error = got_repo_pack_fds_open(&pack_fds);
10093 error = got_worktree_open(&worktree, cwd);
10095 if (list_refs || remove_refs) {
10096 if (error->code != GOT_ERR_NOT_WORKTREE)
10099 if (error->code == GOT_ERR_NOT_WORKTREE)
10100 error = wrap_not_worktree_error(error,
10101 "cherrypick", cwd);
10106 error = got_repo_open(&repo,
10107 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10112 error = apply_unveil(got_repo_get_path(repo), 0,
10113 worktree ? got_worktree_get_root_path(worktree) : NULL);
10117 if (list_refs || remove_refs) {
10118 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10119 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10120 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10124 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10125 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10128 error = got_object_id_str(&commit_id_str, commit_id);
10132 error = got_object_open_as_commit(&commit, repo, commit_id);
10135 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10136 memset(&upa, 0, sizeof(upa));
10137 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10138 commit_id, repo, update_progress, &upa, check_cancelled,
10143 if (upa.did_something) {
10144 error = logmsg_ref(commit_id,
10145 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10148 printf("Merged commit %s\n", commit_id_str);
10150 print_merge_progress_stats(&upa);
10154 got_object_commit_close(commit);
10155 free(commit_id_str);
10157 got_worktree_close(worktree);
10159 const struct got_error *close_err = got_repo_close(repo);
10164 const struct got_error *pack_err =
10165 got_repo_pack_fds_close(pack_fds);
10174 usage_backout(void)
10176 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10180 static const struct got_error *
10181 cmd_backout(int argc, char *argv[])
10183 const struct got_error *error = NULL;
10184 struct got_worktree *worktree = NULL;
10185 struct got_repository *repo = NULL;
10186 char *cwd = NULL, *commit_id_str = NULL;
10187 struct got_object_id *commit_id = NULL;
10188 struct got_commit_object *commit = NULL;
10189 struct got_object_qid *pid;
10190 int ch, list_refs = 0, remove_refs = 0;
10191 struct got_update_progress_arg upa;
10192 int *pack_fds = NULL;
10194 while ((ch = getopt(argc, argv, "lX")) != -1) {
10212 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10213 "unveil", NULL) == -1)
10216 if (list_refs || remove_refs) {
10217 if (argc != 0 && argc != 1)
10219 } else if (argc != 1)
10221 if (list_refs && remove_refs)
10222 option_conflict('l', 'X');
10224 cwd = getcwd(NULL, 0);
10226 error = got_error_from_errno("getcwd");
10230 error = got_repo_pack_fds_open(&pack_fds);
10234 error = got_worktree_open(&worktree, cwd);
10236 if (list_refs || remove_refs) {
10237 if (error->code != GOT_ERR_NOT_WORKTREE)
10240 if (error->code == GOT_ERR_NOT_WORKTREE)
10241 error = wrap_not_worktree_error(error,
10247 error = got_repo_open(&repo,
10248 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10253 error = apply_unveil(got_repo_get_path(repo), 0,
10254 worktree ? got_worktree_get_root_path(worktree) : NULL);
10258 if (list_refs || remove_refs) {
10259 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10260 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10261 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10265 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10266 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10269 error = got_object_id_str(&commit_id_str, commit_id);
10273 error = got_object_open_as_commit(&commit, repo, commit_id);
10276 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10278 error = got_error(GOT_ERR_ROOT_COMMIT);
10282 memset(&upa, 0, sizeof(upa));
10283 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10284 repo, update_progress, &upa, check_cancelled, NULL);
10288 if (upa.did_something) {
10289 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10293 printf("Backed out commit %s\n", commit_id_str);
10295 print_merge_progress_stats(&upa);
10299 got_object_commit_close(commit);
10300 free(commit_id_str);
10302 got_worktree_close(worktree);
10304 const struct got_error *close_err = got_repo_close(repo);
10309 const struct got_error *pack_err =
10310 got_repo_pack_fds_close(pack_fds);
10320 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10325 trim_logmsg(char *logmsg, int limit)
10330 len = strlen(logmsg);
10333 logmsg[len] = '\0';
10334 nl = strchr(logmsg, '\n');
10339 static const struct got_error *
10340 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10342 const struct got_error *err;
10343 char *logmsg0 = NULL;
10346 err = got_object_commit_get_logmsg(&logmsg0, commit);
10351 while (isspace((unsigned char)s[0]))
10354 *logmsg = strdup(s);
10355 if (*logmsg == NULL) {
10356 err = got_error_from_errno("strdup");
10360 trim_logmsg(*logmsg, limit);
10366 static const struct got_error *
10367 show_rebase_merge_conflict(struct got_object_id *id,
10368 struct got_repository *repo)
10370 const struct got_error *err;
10371 struct got_commit_object *commit = NULL;
10372 char *id_str = NULL, *logmsg = NULL;
10374 err = got_object_open_as_commit(&commit, repo, id);
10378 err = got_object_id_str(&id_str, id);
10384 err = get_short_logmsg(&logmsg, 42, commit);
10388 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10391 got_object_commit_close(commit);
10396 static const struct got_error *
10397 show_rebase_progress(struct got_commit_object *commit,
10398 struct got_object_id *old_id, struct got_object_id *new_id)
10400 const struct got_error *err;
10401 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10403 err = got_object_id_str(&old_id_str, old_id);
10408 err = got_object_id_str(&new_id_str, new_id);
10413 old_id_str[12] = '\0';
10415 new_id_str[12] = '\0';
10417 err = get_short_logmsg(&logmsg, 42, commit);
10421 printf("%s -> %s: %s\n", old_id_str,
10422 new_id_str ? new_id_str : "no-op change", logmsg);
10430 static const struct got_error *
10431 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10432 struct got_reference *branch, struct got_reference *tmp_branch,
10433 struct got_repository *repo, int create_backup)
10435 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10436 return got_worktree_rebase_complete(worktree, fileindex,
10437 tmp_branch, branch, repo, create_backup);
10440 static const struct got_error *
10441 rebase_commit(struct got_pathlist_head *merged_paths,
10442 struct got_worktree *worktree, struct got_fileindex *fileindex,
10443 struct got_reference *tmp_branch, const char *committer,
10444 struct got_object_id *commit_id, struct got_repository *repo)
10446 const struct got_error *error;
10447 struct got_commit_object *commit;
10448 struct got_object_id *new_commit_id;
10450 error = got_object_open_as_commit(&commit, repo, commit_id);
10454 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10455 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10458 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10460 error = show_rebase_progress(commit, commit_id, NULL);
10462 error = show_rebase_progress(commit, commit_id, new_commit_id);
10463 free(new_commit_id);
10466 got_object_commit_close(commit);
10470 struct check_path_prefix_arg {
10471 const char *path_prefix;
10476 static const struct got_error *
10477 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10478 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10479 struct got_object_id *id1, struct got_object_id *id2,
10480 const char *path1, const char *path2,
10481 mode_t mode1, mode_t mode2, struct got_repository *repo)
10483 struct check_path_prefix_arg *a = arg;
10485 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10486 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10487 return got_error(a->errcode);
10492 static const struct got_error *
10493 check_path_prefix(struct got_object_id *parent_id,
10494 struct got_object_id *commit_id, const char *path_prefix,
10495 int errcode, struct got_repository *repo)
10497 const struct got_error *err;
10498 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10499 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10500 struct check_path_prefix_arg cpp_arg;
10502 if (got_path_is_root_dir(path_prefix))
10505 err = got_object_open_as_commit(&commit, repo, commit_id);
10509 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10513 err = got_object_open_as_tree(&tree1, repo,
10514 got_object_commit_get_tree_id(parent_commit));
10518 err = got_object_open_as_tree(&tree2, repo,
10519 got_object_commit_get_tree_id(commit));
10523 cpp_arg.path_prefix = path_prefix;
10524 while (cpp_arg.path_prefix[0] == '/')
10525 cpp_arg.path_prefix++;
10526 cpp_arg.len = strlen(cpp_arg.path_prefix);
10527 cpp_arg.errcode = errcode;
10528 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10529 check_path_prefix_in_diff, &cpp_arg, 0);
10532 got_object_tree_close(tree1);
10534 got_object_tree_close(tree2);
10536 got_object_commit_close(commit);
10538 got_object_commit_close(parent_commit);
10542 static const struct got_error *
10543 collect_commits(struct got_object_id_queue *commits,
10544 struct got_object_id *initial_commit_id,
10545 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10546 const char *path_prefix, int path_prefix_errcode,
10547 struct got_repository *repo)
10549 const struct got_error *err = NULL;
10550 struct got_commit_graph *graph = NULL;
10551 struct got_object_id parent_id, commit_id;
10552 struct got_object_qid *qid;
10554 err = got_commit_graph_open(&graph, "/", 1);
10558 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10559 check_cancelled, NULL);
10563 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10564 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10565 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10566 check_cancelled, NULL);
10568 if (err->code == GOT_ERR_ITER_COMPLETED) {
10569 err = got_error_msg(GOT_ERR_ANCESTRY,
10570 "ran out of commits to rebase before "
10571 "youngest common ancestor commit has "
10572 "been reached?!?");
10576 err = check_path_prefix(&parent_id, &commit_id,
10577 path_prefix, path_prefix_errcode, repo);
10581 err = got_object_qid_alloc(&qid, &commit_id);
10584 STAILQ_INSERT_HEAD(commits, qid, entry);
10586 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10590 got_commit_graph_close(graph);
10594 static const struct got_error *
10595 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10597 const struct got_error *err = NULL;
10598 time_t committer_time;
10600 char datebuf[11]; /* YYYY-MM-DD + NUL */
10601 char *author0 = NULL, *author, *smallerthan;
10602 char *logmsg0 = NULL, *logmsg, *newline;
10604 committer_time = got_object_commit_get_committer_time(commit);
10605 if (gmtime_r(&committer_time, &tm) == NULL)
10606 return got_error_from_errno("gmtime_r");
10607 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10608 return got_error(GOT_ERR_NO_SPACE);
10610 author0 = strdup(got_object_commit_get_author(commit));
10611 if (author0 == NULL)
10612 return got_error_from_errno("strdup");
10614 smallerthan = strchr(author, '<');
10615 if (smallerthan && smallerthan[1] != '\0')
10616 author = smallerthan + 1;
10617 author[strcspn(author, "@>")] = '\0';
10619 err = got_object_commit_get_logmsg(&logmsg0, commit);
10623 while (*logmsg == '\n')
10625 newline = strchr(logmsg, '\n');
10629 if (asprintf(brief_str, "%s %s %s",
10630 datebuf, author, logmsg) == -1)
10631 err = got_error_from_errno("asprintf");
10638 static const struct got_error *
10639 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10640 struct got_repository *repo)
10642 const struct got_error *err;
10645 err = got_object_id_str(&id_str, id);
10649 err = got_ref_delete(ref, repo);
10653 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10659 static const struct got_error *
10660 print_backup_ref(const char *branch_name, const char *new_id_str,
10661 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10662 struct got_reflist_object_id_map *refs_idmap,
10663 struct got_repository *repo)
10665 const struct got_error *err = NULL;
10666 struct got_reflist_head *refs;
10667 char *refs_str = NULL;
10668 struct got_object_id *new_commit_id = NULL;
10669 struct got_commit_object *new_commit = NULL;
10670 char *new_commit_brief_str = NULL;
10671 struct got_object_id *yca_id = NULL;
10672 struct got_commit_object *yca_commit = NULL;
10673 char *yca_id_str = NULL, *yca_brief_str = NULL;
10674 char *custom_refs_str;
10676 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10677 return got_error_from_errno("asprintf");
10679 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10680 0, 0, refs_idmap, custom_refs_str, NULL);
10684 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10688 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10690 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10695 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10699 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10703 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10704 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10708 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10709 refs_str ? " (" : "", refs_str ? refs_str : "",
10710 refs_str ? ")" : "", new_commit_brief_str);
10711 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10712 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10716 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10720 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10724 err = got_object_id_str(&yca_id_str, yca_id);
10728 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10730 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10734 printf("history forked at %s%s%s%s\n %s\n",
10736 refs_str ? " (" : "", refs_str ? refs_str : "",
10737 refs_str ? ")" : "", yca_brief_str);
10740 free(custom_refs_str);
10741 free(new_commit_id);
10745 free(yca_brief_str);
10747 got_object_commit_close(new_commit);
10749 got_object_commit_close(yca_commit);
10754 static const struct got_error *
10755 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10756 struct got_repository *repo)
10758 const struct got_error *err;
10759 struct got_reflist_head refs;
10760 struct got_reflist_entry *re;
10761 char *uuidstr = NULL;
10762 static char msg[160];
10766 err = got_worktree_get_uuid(&uuidstr, worktree);
10770 err = got_ref_list(&refs, repo, "refs/got/worktree",
10771 got_ref_cmp_by_name, repo);
10775 TAILQ_FOREACH(re, &refs, entry) {
10776 const char *cmd, *refname, *type;
10778 refname = got_ref_get_name(re->ref);
10780 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10781 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10782 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10783 cmd = "cherrypick";
10784 type = "cherrypicked";
10785 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10786 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10787 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10789 type = "backed-out";
10793 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10796 snprintf(msg, sizeof(msg),
10797 "work tree has references created by %s commits which "
10798 "must be removed with 'got %s -X' before running the %s "
10799 "command", type, cmd, caller);
10800 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10806 got_ref_list_free(&refs);
10810 static const struct got_error *
10811 process_backup_refs(const char *backup_ref_prefix,
10812 const char *wanted_branch_name,
10813 int delete, struct got_repository *repo)
10815 const struct got_error *err;
10816 struct got_reflist_head refs, backup_refs;
10817 struct got_reflist_entry *re;
10818 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10819 struct got_object_id *old_commit_id = NULL;
10820 char *branch_name = NULL;
10821 struct got_commit_object *old_commit = NULL;
10822 struct got_reflist_object_id_map *refs_idmap = NULL;
10823 int wanted_branch_found = 0;
10826 TAILQ_INIT(&backup_refs);
10828 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10832 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10836 if (wanted_branch_name) {
10837 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10838 wanted_branch_name += 11;
10841 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10842 got_ref_cmp_by_commit_timestamp_descending, repo);
10846 TAILQ_FOREACH(re, &backup_refs, entry) {
10847 const char *refname = got_ref_get_name(re->ref);
10850 err = check_cancelled(NULL);
10854 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10858 err = got_object_open_as_commit(&old_commit, repo,
10863 if (strncmp(backup_ref_prefix, refname,
10864 backup_ref_prefix_len) == 0)
10865 refname += backup_ref_prefix_len;
10867 while (refname[0] == '/')
10870 branch_name = strdup(refname);
10871 if (branch_name == NULL) {
10872 err = got_error_from_errno("strdup");
10875 slash = strrchr(branch_name, '/');
10878 refname += strlen(branch_name) + 1;
10881 if (wanted_branch_name == NULL ||
10882 strcmp(wanted_branch_name, branch_name) == 0) {
10883 wanted_branch_found = 1;
10885 err = delete_backup_ref(re->ref,
10886 old_commit_id, repo);
10888 err = print_backup_ref(branch_name, refname,
10889 old_commit_id, old_commit, refs_idmap,
10896 free(old_commit_id);
10897 old_commit_id = NULL;
10899 branch_name = NULL;
10900 got_object_commit_close(old_commit);
10904 if (wanted_branch_name && !wanted_branch_found) {
10905 err = got_error_fmt(GOT_ERR_NOT_REF,
10906 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10910 got_reflist_object_id_map_free(refs_idmap);
10911 got_ref_list_free(&refs);
10912 got_ref_list_free(&backup_refs);
10913 free(old_commit_id);
10916 got_object_commit_close(old_commit);
10920 static const struct got_error *
10921 abort_progress(void *arg, unsigned char status, const char *path)
10924 * Unversioned files should not clutter progress output when
10925 * an operation is aborted.
10927 if (status == GOT_STATUS_UNVERSIONED)
10930 return update_progress(arg, status, path);
10933 static const struct got_error *
10934 cmd_rebase(int argc, char *argv[])
10936 const struct got_error *error = NULL;
10937 struct got_worktree *worktree = NULL;
10938 struct got_repository *repo = NULL;
10939 struct got_fileindex *fileindex = NULL;
10940 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10941 struct got_reference *branch = NULL;
10942 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10943 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10944 struct got_object_id *resume_commit_id = NULL;
10945 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10946 struct got_object_id *head_commit_id = NULL;
10947 struct got_reference *head_ref = NULL;
10948 struct got_commit_object *commit = NULL;
10949 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10950 int histedit_in_progress = 0, merge_in_progress = 0;
10951 int create_backup = 1, list_backups = 0, delete_backups = 0;
10952 struct got_object_id_queue commits;
10953 struct got_pathlist_head merged_paths;
10954 const struct got_object_id_queue *parent_ids;
10955 struct got_object_qid *qid, *pid;
10956 struct got_update_progress_arg upa;
10957 int *pack_fds = NULL;
10959 STAILQ_INIT(&commits);
10960 TAILQ_INIT(&merged_paths);
10961 memset(&upa, 0, sizeof(upa));
10963 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10969 continue_rebase = 1;
10975 delete_backups = 1;
10987 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10988 "unveil", NULL) == -1)
10991 if (list_backups) {
10993 option_conflict('l', 'a');
10994 if (continue_rebase)
10995 option_conflict('l', 'c');
10996 if (delete_backups)
10997 option_conflict('l', 'X');
10998 if (argc != 0 && argc != 1)
11000 } else if (delete_backups) {
11002 option_conflict('X', 'a');
11003 if (continue_rebase)
11004 option_conflict('X', 'c');
11006 option_conflict('l', 'X');
11007 if (argc != 0 && argc != 1)
11010 if (abort_rebase && continue_rebase)
11012 else if (abort_rebase || continue_rebase) {
11015 } else if (argc != 1)
11019 cwd = getcwd(NULL, 0);
11021 error = got_error_from_errno("getcwd");
11025 error = got_repo_pack_fds_open(&pack_fds);
11029 error = got_worktree_open(&worktree, cwd);
11031 if (list_backups || delete_backups) {
11032 if (error->code != GOT_ERR_NOT_WORKTREE)
11035 if (error->code == GOT_ERR_NOT_WORKTREE)
11036 error = wrap_not_worktree_error(error,
11042 error = get_gitconfig_path(&gitconfig_path);
11045 error = got_repo_open(&repo,
11046 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11047 gitconfig_path, pack_fds);
11051 if (worktree != NULL && !list_backups && !delete_backups) {
11052 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11057 error = get_author(&committer, repo, worktree);
11058 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11061 error = apply_unveil(got_repo_get_path(repo), 0,
11062 worktree ? got_worktree_get_root_path(worktree) : NULL);
11066 if (list_backups || delete_backups) {
11067 error = process_backup_refs(
11068 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11069 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11070 goto done; /* nothing else to do */
11073 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11077 if (histedit_in_progress) {
11078 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11082 error = got_worktree_merge_in_progress(&merge_in_progress,
11086 if (merge_in_progress) {
11087 error = got_error(GOT_ERR_MERGE_BUSY);
11091 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11095 if (abort_rebase) {
11096 if (!rebase_in_progress) {
11097 error = got_error(GOT_ERR_NOT_REBASING);
11100 error = got_worktree_rebase_continue(&resume_commit_id,
11101 &new_base_branch, &tmp_branch, &branch, &fileindex,
11105 printf("Switching work tree to %s\n",
11106 got_ref_get_symref_target(new_base_branch));
11107 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11108 new_base_branch, abort_progress, &upa);
11111 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11112 print_merge_progress_stats(&upa);
11113 goto done; /* nothing else to do */
11116 if (continue_rebase) {
11117 if (!rebase_in_progress) {
11118 error = got_error(GOT_ERR_NOT_REBASING);
11121 error = got_worktree_rebase_continue(&resume_commit_id,
11122 &new_base_branch, &tmp_branch, &branch, &fileindex,
11127 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11128 committer, resume_commit_id, repo);
11132 yca_id = got_object_id_dup(resume_commit_id);
11133 if (yca_id == NULL) {
11134 error = got_error_from_errno("got_object_id_dup");
11138 error = got_ref_open(&branch, repo, argv[0], 0);
11141 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11142 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11143 "will not rebase a branch which lives outside "
11144 "the \"refs/heads/\" reference namespace");
11149 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11153 if (!continue_rebase) {
11154 struct got_object_id *base_commit_id;
11156 error = got_ref_open(&head_ref, repo,
11157 got_worktree_get_head_ref_name(worktree), 0);
11160 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11163 base_commit_id = got_worktree_get_base_commit_id(worktree);
11164 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11165 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11169 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11170 base_commit_id, branch_head_commit_id, 1, repo,
11171 check_cancelled, NULL);
11173 if (error->code == GOT_ERR_ANCESTRY) {
11174 error = got_error_msg(GOT_ERR_ANCESTRY,
11175 "specified branch shares no common "
11176 "ancestry with work tree's branch");
11181 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11183 if (error->code != GOT_ERR_ANCESTRY)
11187 struct got_pathlist_head paths;
11188 printf("%s is already based on %s\n",
11189 got_ref_get_name(branch),
11190 got_worktree_get_head_ref_name(worktree));
11191 error = switch_head_ref(branch, branch_head_commit_id,
11195 error = got_worktree_set_base_commit_id(worktree, repo,
11196 branch_head_commit_id);
11199 TAILQ_INIT(&paths);
11200 error = got_pathlist_append(&paths, "", NULL);
11203 error = got_worktree_checkout_files(worktree,
11204 &paths, repo, update_progress, &upa,
11205 check_cancelled, NULL);
11206 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11209 if (upa.did_something) {
11211 error = got_object_id_str(&id_str,
11212 branch_head_commit_id);
11215 printf("Updated to %s: %s\n",
11216 got_worktree_get_head_ref_name(worktree),
11220 printf("Already up-to-date\n");
11221 print_update_progress_stats(&upa);
11226 commit_id = branch_head_commit_id;
11227 error = got_object_open_as_commit(&commit, repo, commit_id);
11231 parent_ids = got_object_commit_get_parent_ids(commit);
11232 pid = STAILQ_FIRST(parent_ids);
11234 error = collect_commits(&commits, commit_id, &pid->id,
11235 yca_id, got_worktree_get_path_prefix(worktree),
11236 GOT_ERR_REBASE_PATH, repo);
11241 got_object_commit_close(commit);
11244 if (!continue_rebase) {
11245 error = got_worktree_rebase_prepare(&new_base_branch,
11246 &tmp_branch, &fileindex, worktree, branch, repo);
11251 if (STAILQ_EMPTY(&commits)) {
11252 if (continue_rebase) {
11253 error = rebase_complete(worktree, fileindex,
11254 branch, tmp_branch, repo, create_backup);
11257 /* Fast-forward the reference of the branch. */
11258 struct got_object_id *new_head_commit_id;
11260 error = got_ref_resolve(&new_head_commit_id, repo,
11264 error = got_object_id_str(&id_str, new_head_commit_id);
11267 printf("Forwarding %s to commit %s\n",
11268 got_ref_get_name(branch), id_str);
11270 error = got_ref_change_ref(branch,
11271 new_head_commit_id);
11274 /* No backup needed since objects did not change. */
11280 STAILQ_FOREACH(qid, &commits, entry) {
11282 commit_id = &qid->id;
11283 parent_id = pid ? &pid->id : yca_id;
11286 memset(&upa, 0, sizeof(upa));
11287 error = got_worktree_rebase_merge_files(&merged_paths,
11288 worktree, fileindex, parent_id, commit_id, repo,
11289 update_progress, &upa, check_cancelled, NULL);
11293 print_merge_progress_stats(&upa);
11294 if (upa.conflicts > 0 || upa.missing > 0 ||
11295 upa.not_deleted > 0 || upa.unversioned > 0) {
11296 if (upa.conflicts > 0) {
11297 error = show_rebase_merge_conflict(&qid->id,
11302 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11306 error = rebase_commit(&merged_paths, worktree, fileindex,
11307 tmp_branch, committer, commit_id, repo);
11308 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11313 if (upa.conflicts > 0 || upa.missing > 0 ||
11314 upa.not_deleted > 0 || upa.unversioned > 0) {
11315 error = got_worktree_rebase_postpone(worktree, fileindex);
11318 if (upa.conflicts > 0 && upa.missing == 0 &&
11319 upa.not_deleted == 0 && upa.unversioned == 0) {
11320 error = got_error_msg(GOT_ERR_CONFLICTS,
11321 "conflicts must be resolved before rebasing "
11323 } else if (upa.conflicts > 0) {
11324 error = got_error_msg(GOT_ERR_CONFLICTS,
11325 "conflicts must be resolved before rebasing "
11326 "can continue; changes destined for some "
11327 "files were not yet merged and should be "
11328 "merged manually if required before the "
11329 "rebase operation is continued");
11331 error = got_error_msg(GOT_ERR_CONFLICTS,
11332 "changes destined for some files were not "
11333 "yet merged and should be merged manually "
11334 "if required before the rebase operation "
11338 error = rebase_complete(worktree, fileindex, branch,
11339 tmp_branch, repo, create_backup);
11343 free(gitconfig_path);
11344 got_object_id_queue_free(&commits);
11345 free(branch_head_commit_id);
11346 free(resume_commit_id);
11347 free(head_commit_id);
11350 got_object_commit_close(commit);
11352 got_ref_close(branch);
11353 if (new_base_branch)
11354 got_ref_close(new_base_branch);
11356 got_ref_close(tmp_branch);
11358 got_ref_close(head_ref);
11360 got_worktree_close(worktree);
11362 const struct got_error *close_err = got_repo_close(repo);
11367 const struct got_error *pack_err =
11368 got_repo_pack_fds_close(pack_fds);
11376 usage_histedit(void)
11378 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11379 "[branch]\n", getprogname());
11383 #define GOT_HISTEDIT_PICK 'p'
11384 #define GOT_HISTEDIT_EDIT 'e'
11385 #define GOT_HISTEDIT_FOLD 'f'
11386 #define GOT_HISTEDIT_DROP 'd'
11387 #define GOT_HISTEDIT_MESG 'm'
11389 static const struct got_histedit_cmd {
11390 unsigned char code;
11393 } got_histedit_cmds[] = {
11394 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11395 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11396 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11398 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11399 { GOT_HISTEDIT_MESG, "mesg",
11400 "single-line log message for commit above (open editor if empty)" },
11403 struct got_histedit_list_entry {
11404 TAILQ_ENTRY(got_histedit_list_entry) entry;
11405 struct got_object_id *commit_id;
11406 const struct got_histedit_cmd *cmd;
11409 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11411 static const struct got_error *
11412 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11413 FILE *f, struct got_repository *repo)
11415 const struct got_error *err = NULL;
11416 char *logmsg = NULL, *id_str = NULL;
11417 struct got_commit_object *commit = NULL;
11420 err = got_object_open_as_commit(&commit, repo, commit_id);
11424 err = get_short_logmsg(&logmsg, 34, commit);
11428 err = got_object_id_str(&id_str, commit_id);
11432 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11434 err = got_ferror(f, GOT_ERR_IO);
11437 got_object_commit_close(commit);
11443 static const struct got_error *
11444 histedit_write_commit_list(struct got_object_id_queue *commits,
11445 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11446 int edit_only, struct got_repository *repo)
11448 const struct got_error *err = NULL;
11449 struct got_object_qid *qid;
11450 const char *histedit_cmd = NULL;
11452 if (STAILQ_EMPTY(commits))
11453 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11455 STAILQ_FOREACH(qid, commits, entry) {
11456 histedit_cmd = got_histedit_cmds[0].name;
11458 histedit_cmd = "drop";
11459 else if (edit_only)
11460 histedit_cmd = "edit";
11461 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11462 histedit_cmd = "fold";
11463 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11466 if (edit_logmsg_only) {
11467 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11469 err = got_ferror(f, GOT_ERR_IO);
11478 static const struct got_error *
11479 write_cmd_list(FILE *f, const char *branch_name,
11480 struct got_object_id_queue *commits)
11482 const struct got_error *err = NULL;
11486 struct got_object_qid *qid;
11488 qid = STAILQ_FIRST(commits);
11489 err = got_object_id_str(&id_str, &qid->id);
11494 "# Editing the history of branch '%s' starting at\n"
11496 "# Commits will be processed in order from top to "
11497 "bottom of this file.\n", branch_name, id_str);
11499 err = got_ferror(f, GOT_ERR_IO);
11503 n = fprintf(f, "# Available histedit commands:\n");
11505 err = got_ferror(f, GOT_ERR_IO);
11509 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11510 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11511 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11514 err = got_ferror(f, GOT_ERR_IO);
11523 static const struct got_error *
11524 histedit_syntax_error(int lineno)
11526 static char msg[42];
11529 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11531 if (ret < 0 || (size_t)ret >= sizeof(msg))
11532 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11534 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11537 static const struct got_error *
11538 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11539 char *logmsg, struct got_repository *repo)
11541 const struct got_error *err;
11542 struct got_commit_object *folded_commit = NULL;
11543 char *id_str, *folded_logmsg = NULL;
11545 err = got_object_id_str(&id_str, hle->commit_id);
11549 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11553 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11556 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11557 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11558 folded_logmsg) == -1) {
11559 err = got_error_from_errno("asprintf");
11563 got_object_commit_close(folded_commit);
11565 free(folded_logmsg);
11569 static struct got_histedit_list_entry *
11570 get_folded_commits(struct got_histedit_list_entry *hle)
11572 struct got_histedit_list_entry *prev, *folded = NULL;
11574 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11575 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11576 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11577 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11579 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11585 static const struct got_error *
11586 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11587 struct got_repository *repo)
11589 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11590 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11591 const struct got_error *err = NULL;
11592 struct got_commit_object *commit = NULL;
11595 struct got_histedit_list_entry *folded = NULL;
11597 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11601 folded = get_folded_commits(hle);
11603 while (folded != hle) {
11604 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11605 folded = TAILQ_NEXT(folded, entry);
11608 err = append_folded_commit_msg(&new_msg, folded,
11614 folded = TAILQ_NEXT(folded, entry);
11618 err = got_object_id_str(&id_str, hle->commit_id);
11621 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11624 logmsg_len = asprintf(&new_msg,
11625 "%s\n# original log message of commit %s: %s",
11626 logmsg ? logmsg : "", id_str, orig_logmsg);
11627 if (logmsg_len == -1) {
11628 err = got_error_from_errno("asprintf");
11634 err = got_object_id_str(&id_str, hle->commit_id);
11638 err = got_opentemp_named_fd(&logmsg_path, &fd,
11639 GOT_TMPDIR_STR "/got-logmsg", "");
11643 write(fd, logmsg, logmsg_len);
11646 err = get_editor(&editor);
11650 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11656 hle->logmsg = strdup(new_msg);
11657 if (hle->logmsg == NULL)
11658 err = got_error_from_errno("strdup");
11661 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11662 err = got_error_from_errno2("unlink", logmsg_path);
11668 got_object_commit_close(commit);
11672 static const struct got_error *
11673 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11674 FILE *f, struct got_repository *repo)
11676 const struct got_error *err = NULL;
11677 char *line = NULL, *p, *end;
11680 int lineno = 0, lastcmd = -1;
11681 const struct got_histedit_cmd *cmd;
11682 struct got_object_id *commit_id = NULL;
11683 struct got_histedit_list_entry *hle = NULL;
11686 len = getline(&line, &size, f);
11688 const struct got_error *getline_err;
11691 getline_err = got_error_from_errno("getline");
11692 err = got_ferror(f, getline_err->code);
11697 while (isspace((unsigned char)p[0]))
11699 if (p[0] == '#' || p[0] == '\0') {
11705 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11706 cmd = &got_histedit_cmds[i];
11707 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11708 isspace((unsigned char)p[strlen(cmd->name)])) {
11709 p += strlen(cmd->name);
11712 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11717 if (i == nitems(got_histedit_cmds)) {
11718 err = histedit_syntax_error(lineno);
11721 while (isspace((unsigned char)p[0]))
11723 if (cmd->code == GOT_HISTEDIT_MESG) {
11724 if (lastcmd != GOT_HISTEDIT_PICK &&
11725 lastcmd != GOT_HISTEDIT_EDIT) {
11726 err = got_error(GOT_ERR_HISTEDIT_CMD);
11729 if (p[0] == '\0') {
11730 err = histedit_edit_logmsg(hle, repo);
11734 hle->logmsg = strdup(p);
11735 if (hle->logmsg == NULL) {
11736 err = got_error_from_errno("strdup");
11742 lastcmd = cmd->code;
11746 while (end[0] && !isspace((unsigned char)end[0]))
11750 err = got_object_resolve_id_str(&commit_id, repo, p);
11752 /* override error code */
11753 err = histedit_syntax_error(lineno);
11757 hle = malloc(sizeof(*hle));
11759 err = got_error_from_errno("malloc");
11763 hle->commit_id = commit_id;
11764 hle->logmsg = NULL;
11768 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11769 lastcmd = cmd->code;
11777 static const struct got_error *
11778 histedit_check_script(struct got_histedit_list *histedit_cmds,
11779 struct got_object_id_queue *commits, struct got_repository *repo)
11781 const struct got_error *err = NULL;
11782 struct got_object_qid *qid;
11783 struct got_histedit_list_entry *hle;
11784 static char msg[92];
11787 if (TAILQ_EMPTY(histedit_cmds))
11788 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11789 "histedit script contains no commands");
11790 if (STAILQ_EMPTY(commits))
11791 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11793 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11794 struct got_histedit_list_entry *hle2;
11795 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11798 if (got_object_id_cmp(hle->commit_id,
11799 hle2->commit_id) != 0)
11801 err = got_object_id_str(&id_str, hle->commit_id);
11804 snprintf(msg, sizeof(msg), "commit %s is listed "
11805 "more than once in histedit script", id_str);
11807 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11811 STAILQ_FOREACH(qid, commits, entry) {
11812 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11813 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11817 err = got_object_id_str(&id_str, &qid->id);
11820 snprintf(msg, sizeof(msg),
11821 "commit %s missing from histedit script", id_str);
11823 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11827 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11828 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11829 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11830 "last commit in histedit script cannot be folded");
11835 static const struct got_error *
11836 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11837 const char *path, struct got_object_id_queue *commits,
11838 struct got_repository *repo)
11840 const struct got_error *err = NULL;
11844 err = get_editor(&editor);
11848 if (spawn_editor(editor, path) == -1) {
11849 err = got_error_from_errno("failed spawning editor");
11853 f = fopen(path, "re");
11855 err = got_error_from_errno("fopen");
11858 err = histedit_parse_list(histedit_cmds, f, repo);
11862 err = histedit_check_script(histedit_cmds, commits, repo);
11864 if (f && fclose(f) == EOF && err == NULL)
11865 err = got_error_from_errno("fclose");
11870 static const struct got_error *
11871 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11872 struct got_object_id_queue *, const char *, const char *,
11873 struct got_repository *);
11875 static const struct got_error *
11876 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11877 struct got_object_id_queue *commits, const char *branch_name,
11878 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11879 struct got_repository *repo)
11881 const struct got_error *err;
11885 err = got_opentemp_named(&path, &f, "got-histedit", "");
11889 err = write_cmd_list(f, branch_name, commits);
11893 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11894 fold_only, drop_only, edit_only, repo);
11898 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11900 err = histedit_parse_list(histedit_cmds, f, repo);
11902 if (fclose(f) == EOF) {
11903 err = got_error_from_errno("fclose");
11907 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11909 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11910 err->code != GOT_ERR_HISTEDIT_CMD)
11912 err = histedit_edit_list_retry(histedit_cmds, err,
11913 commits, path, branch_name, repo);
11917 if (f && fclose(f) == EOF && err == NULL)
11918 err = got_error_from_errno("fclose");
11919 if (path && unlink(path) != 0 && err == NULL)
11920 err = got_error_from_errno2("unlink", path);
11925 static const struct got_error *
11926 histedit_save_list(struct got_histedit_list *histedit_cmds,
11927 struct got_worktree *worktree, struct got_repository *repo)
11929 const struct got_error *err = NULL;
11932 struct got_histedit_list_entry *hle;
11933 struct got_commit_object *commit = NULL;
11935 err = got_worktree_get_histedit_script_path(&path, worktree);
11939 f = fopen(path, "we");
11941 err = got_error_from_errno2("fopen", path);
11944 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11945 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11951 int n = fprintf(f, "%c %s\n",
11952 GOT_HISTEDIT_MESG, hle->logmsg);
11954 err = got_ferror(f, GOT_ERR_IO);
11960 if (f && fclose(f) == EOF && err == NULL)
11961 err = got_error_from_errno("fclose");
11964 got_object_commit_close(commit);
11969 histedit_free_list(struct got_histedit_list *histedit_cmds)
11971 struct got_histedit_list_entry *hle;
11973 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11974 TAILQ_REMOVE(histedit_cmds, hle, entry);
11979 static const struct got_error *
11980 histedit_load_list(struct got_histedit_list *histedit_cmds,
11981 const char *path, struct got_repository *repo)
11983 const struct got_error *err = NULL;
11986 f = fopen(path, "re");
11988 err = got_error_from_errno2("fopen", path);
11992 err = histedit_parse_list(histedit_cmds, f, repo);
11994 if (f && fclose(f) == EOF && err == NULL)
11995 err = got_error_from_errno("fclose");
11999 static const struct got_error *
12000 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12001 const struct got_error *edit_err, struct got_object_id_queue *commits,
12002 const char *path, const char *branch_name, struct got_repository *repo)
12004 const struct got_error *err = NULL, *prev_err = edit_err;
12007 while (resp != 'c' && resp != 'r' && resp != 'a') {
12008 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12009 "or (a)bort: ", getprogname(), prev_err->msg);
12014 histedit_free_list(histedit_cmds);
12015 err = histedit_run_editor(histedit_cmds, path, commits,
12018 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12019 err->code != GOT_ERR_HISTEDIT_CMD)
12026 } else if (resp == 'r') {
12027 histedit_free_list(histedit_cmds);
12028 err = histedit_edit_script(histedit_cmds,
12029 commits, branch_name, 0, 0, 0, 0, repo);
12031 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12032 err->code != GOT_ERR_HISTEDIT_CMD)
12039 } else if (resp == 'a') {
12040 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12043 printf("invalid response '%c'\n", resp);
12049 static const struct got_error *
12050 histedit_complete(struct got_worktree *worktree,
12051 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12052 struct got_reference *branch, struct got_repository *repo)
12054 printf("Switching work tree to %s\n",
12055 got_ref_get_symref_target(branch));
12056 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12060 static const struct got_error *
12061 show_histedit_progress(struct got_commit_object *commit,
12062 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12064 const struct got_error *err;
12065 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12067 err = got_object_id_str(&old_id_str, hle->commit_id);
12072 err = got_object_id_str(&new_id_str, new_id);
12077 old_id_str[12] = '\0';
12079 new_id_str[12] = '\0';
12082 logmsg = strdup(hle->logmsg);
12083 if (logmsg == NULL) {
12084 err = got_error_from_errno("strdup");
12087 trim_logmsg(logmsg, 42);
12089 err = get_short_logmsg(&logmsg, 42, commit);
12094 switch (hle->cmd->code) {
12095 case GOT_HISTEDIT_PICK:
12096 case GOT_HISTEDIT_EDIT:
12097 printf("%s -> %s: %s\n", old_id_str,
12098 new_id_str ? new_id_str : "no-op change", logmsg);
12100 case GOT_HISTEDIT_DROP:
12101 case GOT_HISTEDIT_FOLD:
12102 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12114 static const struct got_error *
12115 histedit_commit(struct got_pathlist_head *merged_paths,
12116 struct got_worktree *worktree, struct got_fileindex *fileindex,
12117 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12118 const char *committer, struct got_repository *repo)
12120 const struct got_error *err;
12121 struct got_commit_object *commit;
12122 struct got_object_id *new_commit_id;
12124 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12125 && hle->logmsg == NULL) {
12126 err = histedit_edit_logmsg(hle, repo);
12131 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12135 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12136 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12137 hle->logmsg, repo);
12139 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12141 err = show_histedit_progress(commit, hle, NULL);
12143 err = show_histedit_progress(commit, hle, new_commit_id);
12144 free(new_commit_id);
12147 got_object_commit_close(commit);
12151 static const struct got_error *
12152 histedit_skip_commit(struct got_histedit_list_entry *hle,
12153 struct got_worktree *worktree, struct got_repository *repo)
12155 const struct got_error *error;
12156 struct got_commit_object *commit;
12158 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12163 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12167 error = show_histedit_progress(commit, hle, NULL);
12168 got_object_commit_close(commit);
12172 static const struct got_error *
12173 check_local_changes(void *arg, unsigned char status,
12174 unsigned char staged_status, const char *path,
12175 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12176 struct got_object_id *commit_id, int dirfd, const char *de_name)
12178 int *have_local_changes = arg;
12181 case GOT_STATUS_ADD:
12182 case GOT_STATUS_DELETE:
12183 case GOT_STATUS_MODIFY:
12184 case GOT_STATUS_CONFLICT:
12185 *have_local_changes = 1;
12186 return got_error(GOT_ERR_CANCELLED);
12191 switch (staged_status) {
12192 case GOT_STATUS_ADD:
12193 case GOT_STATUS_DELETE:
12194 case GOT_STATUS_MODIFY:
12195 *have_local_changes = 1;
12196 return got_error(GOT_ERR_CANCELLED);
12204 static const struct got_error *
12205 cmd_histedit(int argc, char *argv[])
12207 const struct got_error *error = NULL;
12208 struct got_worktree *worktree = NULL;
12209 struct got_fileindex *fileindex = NULL;
12210 struct got_repository *repo = NULL;
12211 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12212 struct got_reference *branch = NULL;
12213 struct got_reference *tmp_branch = NULL;
12214 struct got_object_id *resume_commit_id = NULL;
12215 struct got_object_id *base_commit_id = NULL;
12216 struct got_object_id *head_commit_id = NULL;
12217 struct got_commit_object *commit = NULL;
12218 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12219 struct got_update_progress_arg upa;
12220 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12221 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12222 int list_backups = 0, delete_backups = 0;
12223 const char *edit_script_path = NULL;
12224 struct got_object_id_queue commits;
12225 struct got_pathlist_head merged_paths;
12226 const struct got_object_id_queue *parent_ids;
12227 struct got_object_qid *pid;
12228 struct got_histedit_list histedit_cmds;
12229 struct got_histedit_list_entry *hle;
12230 int *pack_fds = NULL;
12232 STAILQ_INIT(&commits);
12233 TAILQ_INIT(&histedit_cmds);
12234 TAILQ_INIT(&merged_paths);
12235 memset(&upa, 0, sizeof(upa));
12237 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12252 edit_script_path = optarg;
12261 edit_logmsg_only = 1;
12264 delete_backups = 1;
12276 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12277 "unveil", NULL) == -1)
12280 if (abort_edit && continue_edit)
12281 option_conflict('a', 'c');
12282 if (edit_script_path && edit_logmsg_only)
12283 option_conflict('F', 'm');
12284 if (abort_edit && edit_logmsg_only)
12285 option_conflict('a', 'm');
12286 if (continue_edit && edit_logmsg_only)
12287 option_conflict('c', 'm');
12288 if (abort_edit && fold_only)
12289 option_conflict('a', 'f');
12290 if (continue_edit && fold_only)
12291 option_conflict('c', 'f');
12292 if (fold_only && edit_logmsg_only)
12293 option_conflict('f', 'm');
12294 if (edit_script_path && fold_only)
12295 option_conflict('F', 'f');
12296 if (abort_edit && edit_only)
12297 option_conflict('a', 'e');
12298 if (continue_edit && edit_only)
12299 option_conflict('c', 'e');
12300 if (edit_only && edit_logmsg_only)
12301 option_conflict('e', 'm');
12302 if (edit_script_path && edit_only)
12303 option_conflict('F', 'e');
12304 if (fold_only && edit_only)
12305 option_conflict('f', 'e');
12306 if (drop_only && abort_edit)
12307 option_conflict('d', 'a');
12308 if (drop_only && continue_edit)
12309 option_conflict('d', 'c');
12310 if (drop_only && edit_logmsg_only)
12311 option_conflict('d', 'm');
12312 if (drop_only && edit_only)
12313 option_conflict('d', 'e');
12314 if (drop_only && edit_script_path)
12315 option_conflict('d', 'F');
12316 if (drop_only && fold_only)
12317 option_conflict('d', 'f');
12318 if (list_backups) {
12320 option_conflict('l', 'a');
12322 option_conflict('l', 'c');
12323 if (edit_script_path)
12324 option_conflict('l', 'F');
12325 if (edit_logmsg_only)
12326 option_conflict('l', 'm');
12328 option_conflict('l', 'd');
12330 option_conflict('l', 'f');
12332 option_conflict('l', 'e');
12333 if (delete_backups)
12334 option_conflict('l', 'X');
12335 if (argc != 0 && argc != 1)
12337 } else if (delete_backups) {
12339 option_conflict('X', 'a');
12341 option_conflict('X', 'c');
12343 option_conflict('X', 'd');
12344 if (edit_script_path)
12345 option_conflict('X', 'F');
12346 if (edit_logmsg_only)
12347 option_conflict('X', 'm');
12349 option_conflict('X', 'f');
12351 option_conflict('X', 'e');
12353 option_conflict('X', 'l');
12354 if (argc != 0 && argc != 1)
12356 } else if (argc != 0)
12360 * This command cannot apply unveil(2) in all cases because the
12361 * user may choose to run an editor to edit the histedit script
12362 * and to edit individual commit log messages.
12363 * unveil(2) traverses exec(2); if an editor is used we have to
12364 * apply unveil after edit script and log messages have been written.
12365 * XXX TODO: Make use of unveil(2) where possible.
12368 cwd = getcwd(NULL, 0);
12370 error = got_error_from_errno("getcwd");
12374 error = got_repo_pack_fds_open(&pack_fds);
12378 error = got_worktree_open(&worktree, cwd);
12380 if (list_backups || delete_backups) {
12381 if (error->code != GOT_ERR_NOT_WORKTREE)
12384 if (error->code == GOT_ERR_NOT_WORKTREE)
12385 error = wrap_not_worktree_error(error,
12391 if (list_backups || delete_backups) {
12392 error = got_repo_open(&repo,
12393 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12397 error = apply_unveil(got_repo_get_path(repo), 0,
12398 worktree ? got_worktree_get_root_path(worktree) : NULL);
12401 error = process_backup_refs(
12402 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12403 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12404 goto done; /* nothing else to do */
12407 error = get_gitconfig_path(&gitconfig_path);
12410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12411 gitconfig_path, pack_fds);
12415 if (worktree != NULL && !list_backups && !delete_backups) {
12416 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12421 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12424 if (rebase_in_progress) {
12425 error = got_error(GOT_ERR_REBASING);
12429 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12433 if (merge_in_progress) {
12434 error = got_error(GOT_ERR_MERGE_BUSY);
12438 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12442 if (edit_in_progress && edit_logmsg_only) {
12443 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12444 "histedit operation is in progress in this "
12445 "work tree and must be continued or aborted "
12446 "before the -m option can be used");
12449 if (edit_in_progress && drop_only) {
12450 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12451 "histedit operation is in progress in this "
12452 "work tree and must be continued or aborted "
12453 "before the -d option can be used");
12456 if (edit_in_progress && fold_only) {
12457 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12458 "histedit operation is in progress in this "
12459 "work tree and must be continued or aborted "
12460 "before the -f option can be used");
12463 if (edit_in_progress && edit_only) {
12464 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12465 "histedit operation is in progress in this "
12466 "work tree and must be continued or aborted "
12467 "before the -e option can be used");
12471 if (edit_in_progress && abort_edit) {
12472 error = got_worktree_histedit_continue(&resume_commit_id,
12473 &tmp_branch, &branch, &base_commit_id, &fileindex,
12477 printf("Switching work tree to %s\n",
12478 got_ref_get_symref_target(branch));
12479 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12480 branch, base_commit_id, abort_progress, &upa);
12483 printf("Histedit of %s aborted\n",
12484 got_ref_get_symref_target(branch));
12485 print_merge_progress_stats(&upa);
12486 goto done; /* nothing else to do */
12487 } else if (abort_edit) {
12488 error = got_error(GOT_ERR_NOT_HISTEDIT);
12492 error = get_author(&committer, repo, worktree);
12496 if (continue_edit) {
12499 if (!edit_in_progress) {
12500 error = got_error(GOT_ERR_NOT_HISTEDIT);
12504 error = got_worktree_get_histedit_script_path(&path, worktree);
12508 error = histedit_load_list(&histedit_cmds, path, repo);
12513 error = got_worktree_histedit_continue(&resume_commit_id,
12514 &tmp_branch, &branch, &base_commit_id, &fileindex,
12519 error = got_ref_resolve(&head_commit_id, repo, branch);
12523 error = got_object_open_as_commit(&commit, repo,
12527 parent_ids = got_object_commit_get_parent_ids(commit);
12528 pid = STAILQ_FIRST(parent_ids);
12530 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12533 error = collect_commits(&commits, head_commit_id, &pid->id,
12534 base_commit_id, got_worktree_get_path_prefix(worktree),
12535 GOT_ERR_HISTEDIT_PATH, repo);
12536 got_object_commit_close(commit);
12541 if (edit_in_progress) {
12542 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12546 error = got_ref_open(&branch, repo,
12547 got_worktree_get_head_ref_name(worktree), 0);
12551 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12552 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12553 "will not edit commit history of a branch outside "
12554 "the \"refs/heads/\" reference namespace");
12558 error = got_ref_resolve(&head_commit_id, repo, branch);
12559 got_ref_close(branch);
12564 error = got_object_open_as_commit(&commit, repo,
12568 parent_ids = got_object_commit_get_parent_ids(commit);
12569 pid = STAILQ_FIRST(parent_ids);
12571 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12574 error = collect_commits(&commits, head_commit_id, &pid->id,
12575 got_worktree_get_base_commit_id(worktree),
12576 got_worktree_get_path_prefix(worktree),
12577 GOT_ERR_HISTEDIT_PATH, repo);
12578 got_object_commit_close(commit);
12583 if (STAILQ_EMPTY(&commits)) {
12584 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12588 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12589 &base_commit_id, &fileindex, worktree, repo);
12593 if (edit_script_path) {
12594 error = histedit_load_list(&histedit_cmds,
12595 edit_script_path, repo);
12597 got_worktree_histedit_abort(worktree, fileindex,
12598 repo, branch, base_commit_id,
12599 abort_progress, &upa);
12600 print_merge_progress_stats(&upa);
12604 const char *branch_name;
12605 branch_name = got_ref_get_symref_target(branch);
12606 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12608 error = histedit_edit_script(&histedit_cmds, &commits,
12609 branch_name, edit_logmsg_only, fold_only,
12610 drop_only, edit_only, repo);
12612 got_worktree_histedit_abort(worktree, fileindex,
12613 repo, branch, base_commit_id,
12614 abort_progress, &upa);
12615 print_merge_progress_stats(&upa);
12621 error = histedit_save_list(&histedit_cmds, worktree,
12624 got_worktree_histedit_abort(worktree, fileindex,
12625 repo, branch, base_commit_id,
12626 abort_progress, &upa);
12627 print_merge_progress_stats(&upa);
12633 error = histedit_check_script(&histedit_cmds, &commits, repo);
12637 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12638 if (resume_commit_id) {
12639 if (got_object_id_cmp(hle->commit_id,
12640 resume_commit_id) != 0)
12643 resume_commit_id = NULL;
12644 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12645 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12646 error = histedit_skip_commit(hle, worktree,
12651 struct got_pathlist_head paths;
12652 int have_changes = 0;
12654 TAILQ_INIT(&paths);
12655 error = got_pathlist_append(&paths, "", NULL);
12658 error = got_worktree_status(worktree, &paths,
12659 repo, 0, check_local_changes, &have_changes,
12660 check_cancelled, NULL);
12661 got_pathlist_free(&paths,
12662 GOT_PATHLIST_FREE_NONE);
12664 if (error->code != GOT_ERR_CANCELLED)
12666 if (sigint_received || sigpipe_received)
12669 if (have_changes) {
12670 error = histedit_commit(NULL, worktree,
12671 fileindex, tmp_branch, hle,
12676 error = got_object_open_as_commit(
12677 &commit, repo, hle->commit_id);
12680 error = show_histedit_progress(commit,
12682 got_object_commit_close(commit);
12691 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12692 error = histedit_skip_commit(hle, worktree, repo);
12698 error = got_object_open_as_commit(&commit, repo,
12702 parent_ids = got_object_commit_get_parent_ids(commit);
12703 pid = STAILQ_FIRST(parent_ids);
12705 error = got_worktree_histedit_merge_files(&merged_paths,
12706 worktree, fileindex, &pid->id, hle->commit_id, repo,
12707 update_progress, &upa, check_cancelled, NULL);
12710 got_object_commit_close(commit);
12713 print_merge_progress_stats(&upa);
12714 if (upa.conflicts > 0 || upa.missing > 0 ||
12715 upa.not_deleted > 0 || upa.unversioned > 0) {
12716 if (upa.conflicts > 0) {
12717 error = show_rebase_merge_conflict(
12718 hle->commit_id, repo);
12722 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12726 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12728 error = got_object_id_str(&id_str, hle->commit_id);
12731 printf("Stopping histedit for amending commit %s\n",
12734 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12735 error = got_worktree_histedit_postpone(worktree,
12740 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12741 error = histedit_skip_commit(hle, worktree, repo);
12747 error = histedit_commit(&merged_paths, worktree, fileindex,
12748 tmp_branch, hle, committer, repo);
12749 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12754 if (upa.conflicts > 0 || upa.missing > 0 ||
12755 upa.not_deleted > 0 || upa.unversioned > 0) {
12756 error = got_worktree_histedit_postpone(worktree, fileindex);
12759 if (upa.conflicts > 0 && upa.missing == 0 &&
12760 upa.not_deleted == 0 && upa.unversioned == 0) {
12761 error = got_error_msg(GOT_ERR_CONFLICTS,
12762 "conflicts must be resolved before histedit "
12764 } else if (upa.conflicts > 0) {
12765 error = got_error_msg(GOT_ERR_CONFLICTS,
12766 "conflicts must be resolved before histedit "
12767 "can continue; changes destined for some "
12768 "files were not yet merged and should be "
12769 "merged manually if required before the "
12770 "histedit operation is continued");
12772 error = got_error_msg(GOT_ERR_CONFLICTS,
12773 "changes destined for some files were not "
12774 "yet merged and should be merged manually "
12775 "if required before the histedit operation "
12779 error = histedit_complete(worktree, fileindex, tmp_branch,
12784 free(gitconfig_path);
12785 got_object_id_queue_free(&commits);
12786 histedit_free_list(&histedit_cmds);
12787 free(head_commit_id);
12788 free(base_commit_id);
12789 free(resume_commit_id);
12791 got_object_commit_close(commit);
12793 got_ref_close(branch);
12795 got_ref_close(tmp_branch);
12797 got_worktree_close(worktree);
12799 const struct got_error *close_err = got_repo_close(repo);
12804 const struct got_error *pack_err =
12805 got_repo_pack_fds_close(pack_fds);
12813 usage_integrate(void)
12815 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12819 static const struct got_error *
12820 cmd_integrate(int argc, char *argv[])
12822 const struct got_error *error = NULL;
12823 struct got_repository *repo = NULL;
12824 struct got_worktree *worktree = NULL;
12825 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12826 const char *branch_arg = NULL;
12827 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12828 struct got_fileindex *fileindex = NULL;
12829 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12831 struct got_update_progress_arg upa;
12832 int *pack_fds = NULL;
12834 while ((ch = getopt(argc, argv, "")) != -1) {
12847 branch_arg = argv[0];
12849 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12850 "unveil", NULL) == -1)
12853 cwd = getcwd(NULL, 0);
12855 error = got_error_from_errno("getcwd");
12859 error = got_repo_pack_fds_open(&pack_fds);
12863 error = got_worktree_open(&worktree, cwd);
12865 if (error->code == GOT_ERR_NOT_WORKTREE)
12866 error = wrap_not_worktree_error(error, "integrate",
12871 error = check_rebase_or_histedit_in_progress(worktree);
12875 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12880 error = apply_unveil(got_repo_get_path(repo), 0,
12881 got_worktree_get_root_path(worktree));
12885 error = check_merge_in_progress(worktree, repo);
12889 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12890 error = got_error_from_errno("asprintf");
12894 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12895 &base_branch_ref, worktree, refname, repo);
12899 refname = strdup(got_ref_get_name(branch_ref));
12900 if (refname == NULL) {
12901 error = got_error_from_errno("strdup");
12902 got_worktree_integrate_abort(worktree, fileindex, repo,
12903 branch_ref, base_branch_ref);
12906 base_refname = strdup(got_ref_get_name(base_branch_ref));
12907 if (base_refname == NULL) {
12908 error = got_error_from_errno("strdup");
12909 got_worktree_integrate_abort(worktree, fileindex, repo,
12910 branch_ref, base_branch_ref);
12913 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12914 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12915 got_worktree_integrate_abort(worktree, fileindex, repo,
12916 branch_ref, base_branch_ref);
12920 error = got_ref_resolve(&commit_id, repo, branch_ref);
12924 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12928 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12929 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12930 "specified branch has already been integrated");
12931 got_worktree_integrate_abort(worktree, fileindex, repo,
12932 branch_ref, base_branch_ref);
12936 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12938 if (error->code == GOT_ERR_ANCESTRY)
12939 error = got_error(GOT_ERR_REBASE_REQUIRED);
12940 got_worktree_integrate_abort(worktree, fileindex, repo,
12941 branch_ref, base_branch_ref);
12945 memset(&upa, 0, sizeof(upa));
12946 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12947 branch_ref, base_branch_ref, update_progress, &upa,
12948 check_cancelled, NULL);
12952 printf("Integrated %s into %s\n", refname, base_refname);
12953 print_update_progress_stats(&upa);
12956 const struct got_error *close_err = got_repo_close(repo);
12961 got_worktree_close(worktree);
12963 const struct got_error *pack_err =
12964 got_repo_pack_fds_close(pack_fds);
12969 free(base_commit_id);
12972 free(base_refname);
12979 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12983 static const struct got_error *
12984 cmd_merge(int argc, char *argv[])
12986 const struct got_error *error = NULL;
12987 struct got_worktree *worktree = NULL;
12988 struct got_repository *repo = NULL;
12989 struct got_fileindex *fileindex = NULL;
12990 char *cwd = NULL, *id_str = NULL, *author = NULL;
12991 struct got_reference *branch = NULL, *wt_branch = NULL;
12992 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12993 struct got_object_id *wt_branch_tip = NULL;
12994 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12995 int interrupt_merge = 0;
12996 struct got_update_progress_arg upa;
12997 struct got_object_id *merge_commit_id = NULL;
12998 char *branch_name = NULL;
12999 int *pack_fds = NULL;
13001 memset(&upa, 0, sizeof(upa));
13003 while ((ch = getopt(argc, argv, "acn")) != -1) {
13009 continue_merge = 1;
13012 interrupt_merge = 1;
13024 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13025 "unveil", NULL) == -1)
13029 if (abort_merge && continue_merge)
13030 option_conflict('a', 'c');
13031 if (abort_merge || continue_merge) {
13034 } else if (argc != 1)
13037 cwd = getcwd(NULL, 0);
13039 error = got_error_from_errno("getcwd");
13043 error = got_repo_pack_fds_open(&pack_fds);
13047 error = got_worktree_open(&worktree, cwd);
13049 if (error->code == GOT_ERR_NOT_WORKTREE)
13050 error = wrap_not_worktree_error(error,
13055 error = got_repo_open(&repo,
13056 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13061 if (worktree != NULL) {
13062 error = worktree_has_logmsg_ref("merge", worktree, repo);
13067 error = apply_unveil(got_repo_get_path(repo), 0,
13068 worktree ? got_worktree_get_root_path(worktree) : NULL);
13072 error = check_rebase_or_histedit_in_progress(worktree);
13076 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13082 if (!merge_in_progress) {
13083 error = got_error(GOT_ERR_NOT_MERGING);
13086 error = got_worktree_merge_continue(&branch_name,
13087 &branch_tip, &fileindex, worktree, repo);
13090 error = got_worktree_merge_abort(worktree, fileindex, repo,
13091 abort_progress, &upa);
13094 printf("Merge of %s aborted\n", branch_name);
13095 goto done; /* nothing else to do */
13098 error = get_author(&author, repo, worktree);
13102 if (continue_merge) {
13103 if (!merge_in_progress) {
13104 error = got_error(GOT_ERR_NOT_MERGING);
13107 error = got_worktree_merge_continue(&branch_name,
13108 &branch_tip, &fileindex, worktree, repo);
13112 error = got_ref_open(&branch, repo, argv[0], 0);
13115 branch_name = strdup(got_ref_get_name(branch));
13116 if (branch_name == NULL) {
13117 error = got_error_from_errno("strdup");
13120 error = got_ref_resolve(&branch_tip, repo, branch);
13125 error = got_ref_open(&wt_branch, repo,
13126 got_worktree_get_head_ref_name(worktree), 0);
13129 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13132 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13133 wt_branch_tip, branch_tip, 0, repo,
13134 check_cancelled, NULL);
13135 if (error && error->code != GOT_ERR_ANCESTRY)
13138 if (!continue_merge) {
13139 error = check_path_prefix(wt_branch_tip, branch_tip,
13140 got_worktree_get_path_prefix(worktree),
13141 GOT_ERR_MERGE_PATH, repo);
13145 error = check_same_branch(wt_branch_tip, branch,
13148 if (error->code != GOT_ERR_ANCESTRY)
13152 static char msg[512];
13153 snprintf(msg, sizeof(msg),
13154 "cannot create a merge commit because "
13155 "%s is based on %s; %s can be integrated "
13156 "with 'got integrate' instead", branch_name,
13157 got_worktree_get_head_ref_name(worktree),
13159 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13163 error = got_worktree_merge_prepare(&fileindex, worktree,
13168 error = got_worktree_merge_branch(worktree, fileindex,
13169 yca_id, branch_tip, repo, update_progress, &upa,
13170 check_cancelled, NULL);
13173 print_merge_progress_stats(&upa);
13174 if (!upa.did_something) {
13175 error = got_worktree_merge_abort(worktree, fileindex,
13176 repo, abort_progress, &upa);
13179 printf("Already up-to-date\n");
13184 if (interrupt_merge) {
13185 error = got_worktree_merge_postpone(worktree, fileindex);
13188 printf("Merge of %s interrupted on request\n", branch_name);
13189 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13190 upa.not_deleted > 0 || upa.unversioned > 0) {
13191 error = got_worktree_merge_postpone(worktree, fileindex);
13194 if (upa.conflicts > 0 && upa.missing == 0 &&
13195 upa.not_deleted == 0 && upa.unversioned == 0) {
13196 error = got_error_msg(GOT_ERR_CONFLICTS,
13197 "conflicts must be resolved before merging "
13199 } else if (upa.conflicts > 0) {
13200 error = got_error_msg(GOT_ERR_CONFLICTS,
13201 "conflicts must be resolved before merging "
13202 "can continue; changes destined for some "
13203 "files were not yet merged and "
13204 "should be merged manually if required before the "
13205 "merge operation is continued");
13207 error = got_error_msg(GOT_ERR_CONFLICTS,
13208 "changes destined for some "
13209 "files were not yet merged and should be "
13210 "merged manually if required before the "
13211 "merge operation is continued");
13215 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13216 fileindex, author, NULL, 1, branch_tip, branch_name,
13217 repo, continue_merge ? print_status : NULL, NULL);
13220 error = got_worktree_merge_complete(worktree, fileindex, repo);
13223 error = got_object_id_str(&id_str, merge_commit_id);
13226 printf("Merged %s into %s: %s\n", branch_name,
13227 got_worktree_get_head_ref_name(worktree),
13233 free(merge_commit_id);
13239 got_ref_close(branch);
13241 got_ref_close(wt_branch);
13243 got_worktree_close(worktree);
13245 const struct got_error *close_err = got_repo_close(repo);
13250 const struct got_error *pack_err =
13251 got_repo_pack_fds_close(pack_fds);
13261 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13262 "[path ...]\n", getprogname());
13266 static const struct got_error *
13267 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13268 const char *path, struct got_object_id *blob_id,
13269 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13270 int dirfd, const char *de_name)
13272 const struct got_error *err = NULL;
13273 char *id_str = NULL;
13275 if (staged_status != GOT_STATUS_ADD &&
13276 staged_status != GOT_STATUS_MODIFY &&
13277 staged_status != GOT_STATUS_DELETE)
13280 if (staged_status == GOT_STATUS_ADD ||
13281 staged_status == GOT_STATUS_MODIFY)
13282 err = got_object_id_str(&id_str, staged_blob_id);
13284 err = got_object_id_str(&id_str, blob_id);
13288 printf("%s %c %s\n", id_str, staged_status, path);
13293 static const struct got_error *
13294 cmd_stage(int argc, char *argv[])
13296 const struct got_error *error = NULL;
13297 struct got_repository *repo = NULL;
13298 struct got_worktree *worktree = NULL;
13300 struct got_pathlist_head paths;
13301 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13302 FILE *patch_script_file = NULL;
13303 const char *patch_script_path = NULL;
13304 struct choose_patch_arg cpa;
13305 int *pack_fds = NULL;
13307 TAILQ_INIT(&paths);
13309 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13312 patch_script_path = optarg;
13321 allow_bad_symlinks = 1;
13333 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13334 "unveil", NULL) == -1)
13337 if (list_stage && (pflag || patch_script_path))
13338 errx(1, "-l option cannot be used with other options");
13339 if (patch_script_path && !pflag)
13340 errx(1, "-F option can only be used together with -p option");
13342 cwd = getcwd(NULL, 0);
13344 error = got_error_from_errno("getcwd");
13348 error = got_repo_pack_fds_open(&pack_fds);
13352 error = got_worktree_open(&worktree, cwd);
13354 if (error->code == GOT_ERR_NOT_WORKTREE)
13355 error = wrap_not_worktree_error(error, "stage", cwd);
13359 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13364 if (patch_script_path) {
13365 patch_script_file = fopen(patch_script_path, "re");
13366 if (patch_script_file == NULL) {
13367 error = got_error_from_errno2("fopen",
13368 patch_script_path);
13372 error = apply_unveil(got_repo_get_path(repo), 0,
13373 got_worktree_get_root_path(worktree));
13377 error = check_merge_in_progress(worktree, repo);
13381 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13386 error = got_worktree_status(worktree, &paths, repo, 0,
13387 print_stage, NULL, check_cancelled, NULL);
13389 cpa.patch_script_file = patch_script_file;
13390 cpa.action = "stage";
13391 error = got_worktree_stage(worktree, &paths,
13392 pflag ? NULL : print_status, NULL,
13393 pflag ? choose_patch : NULL, &cpa,
13394 allow_bad_symlinks, repo);
13397 if (patch_script_file && fclose(patch_script_file) == EOF &&
13399 error = got_error_from_errno2("fclose", patch_script_path);
13401 const struct got_error *close_err = got_repo_close(repo);
13406 got_worktree_close(worktree);
13408 const struct got_error *pack_err =
13409 got_repo_pack_fds_close(pack_fds);
13413 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13419 usage_unstage(void)
13421 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13422 "[path ...]\n", getprogname());
13427 static const struct got_error *
13428 cmd_unstage(int argc, char *argv[])
13430 const struct got_error *error = NULL;
13431 struct got_repository *repo = NULL;
13432 struct got_worktree *worktree = NULL;
13434 struct got_pathlist_head paths;
13436 struct got_update_progress_arg upa;
13437 FILE *patch_script_file = NULL;
13438 const char *patch_script_path = NULL;
13439 struct choose_patch_arg cpa;
13440 int *pack_fds = NULL;
13442 TAILQ_INIT(&paths);
13444 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13447 patch_script_path = optarg;
13462 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13463 "unveil", NULL) == -1)
13466 if (patch_script_path && !pflag)
13467 errx(1, "-F option can only be used together with -p option");
13469 cwd = getcwd(NULL, 0);
13471 error = got_error_from_errno("getcwd");
13475 error = got_repo_pack_fds_open(&pack_fds);
13479 error = got_worktree_open(&worktree, cwd);
13481 if (error->code == GOT_ERR_NOT_WORKTREE)
13482 error = wrap_not_worktree_error(error, "unstage", cwd);
13486 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13491 if (patch_script_path) {
13492 patch_script_file = fopen(patch_script_path, "re");
13493 if (patch_script_file == NULL) {
13494 error = got_error_from_errno2("fopen",
13495 patch_script_path);
13500 error = apply_unveil(got_repo_get_path(repo), 0,
13501 got_worktree_get_root_path(worktree));
13505 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13509 cpa.patch_script_file = patch_script_file;
13510 cpa.action = "unstage";
13511 memset(&upa, 0, sizeof(upa));
13512 error = got_worktree_unstage(worktree, &paths, update_progress,
13513 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13515 print_merge_progress_stats(&upa);
13517 if (patch_script_file && fclose(patch_script_file) == EOF &&
13519 error = got_error_from_errno2("fclose", patch_script_path);
13521 const struct got_error *close_err = got_repo_close(repo);
13526 got_worktree_close(worktree);
13528 const struct got_error *pack_err =
13529 got_repo_pack_fds_close(pack_fds);
13533 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13541 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13542 "arg ...\n", getprogname());
13546 static const struct got_error *
13547 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13549 const struct got_error *err;
13550 struct got_blob_object *blob;
13553 fd = got_opentempfd();
13555 return got_error_from_errno("got_opentempfd");
13557 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13561 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13563 if (fd != -1 && close(fd) == -1 && err == NULL)
13564 err = got_error_from_errno("close");
13566 got_object_blob_close(blob);
13570 static const struct got_error *
13571 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13573 const struct got_error *err;
13574 struct got_tree_object *tree;
13577 err = got_object_open_as_tree(&tree, repo, id);
13581 nentries = got_object_tree_get_nentries(tree);
13582 for (i = 0; i < nentries; i++) {
13583 struct got_tree_entry *te;
13585 if (sigint_received || sigpipe_received)
13587 te = got_object_tree_get_entry(tree, i);
13588 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13591 fprintf(outfile, "%s %.7o %s\n", id_str,
13592 got_tree_entry_get_mode(te),
13593 got_tree_entry_get_name(te));
13597 got_object_tree_close(tree);
13601 static const struct got_error *
13602 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13604 const struct got_error *err;
13605 struct got_commit_object *commit;
13606 const struct got_object_id_queue *parent_ids;
13607 struct got_object_qid *pid;
13608 char *id_str = NULL;
13609 const char *logmsg = NULL;
13612 err = got_object_open_as_commit(&commit, repo, id);
13616 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13620 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13621 parent_ids = got_object_commit_get_parent_ids(commit);
13622 fprintf(outfile, "numparents %d\n",
13623 got_object_commit_get_nparents(commit));
13624 STAILQ_FOREACH(pid, parent_ids, entry) {
13626 err = got_object_id_str(&pid_str, &pid->id);
13629 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13632 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13633 got_object_commit_get_author_gmtoff(commit));
13634 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13635 got_object_commit_get_author(commit),
13636 (long long)got_object_commit_get_author_time(commit),
13639 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13640 got_object_commit_get_committer_gmtoff(commit));
13641 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13642 got_object_commit_get_committer(commit),
13643 (long long)got_object_commit_get_committer_time(commit),
13646 logmsg = got_object_commit_get_logmsg_raw(commit);
13647 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13648 fprintf(outfile, "%s", logmsg);
13651 got_object_commit_close(commit);
13655 static const struct got_error *
13656 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13658 const struct got_error *err;
13659 struct got_tag_object *tag;
13660 char *id_str = NULL;
13661 const char *tagmsg = NULL;
13664 err = got_object_open_as_tag(&tag, repo, id);
13668 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13672 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13674 switch (got_object_tag_get_object_type(tag)) {
13675 case GOT_OBJ_TYPE_BLOB:
13676 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13677 GOT_OBJ_LABEL_BLOB);
13679 case GOT_OBJ_TYPE_TREE:
13680 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13681 GOT_OBJ_LABEL_TREE);
13683 case GOT_OBJ_TYPE_COMMIT:
13684 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13685 GOT_OBJ_LABEL_COMMIT);
13687 case GOT_OBJ_TYPE_TAG:
13688 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13689 GOT_OBJ_LABEL_TAG);
13695 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13696 got_object_tag_get_name(tag));
13698 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13699 got_object_tag_get_tagger_gmtoff(tag));
13700 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13701 got_object_tag_get_tagger(tag),
13702 (long long)got_object_tag_get_tagger_time(tag),
13705 tagmsg = got_object_tag_get_message(tag);
13706 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13707 fprintf(outfile, "%s", tagmsg);
13710 got_object_tag_close(tag);
13714 static const struct got_error *
13715 cmd_cat(int argc, char *argv[])
13717 const struct got_error *error;
13718 struct got_repository *repo = NULL;
13719 struct got_worktree *worktree = NULL;
13720 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13721 const char *commit_id_str = NULL;
13722 struct got_object_id *id = NULL, *commit_id = NULL;
13723 struct got_commit_object *commit = NULL;
13724 int ch, obj_type, i, force_path = 0;
13725 struct got_reflist_head refs;
13726 int *pack_fds = NULL;
13731 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13736 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13739 commit_id_str = optarg;
13745 repo_path = realpath(optarg, NULL);
13746 if (repo_path == NULL)
13747 return got_error_from_errno2("realpath",
13749 got_path_strip_trailing_slashes(repo_path);
13760 cwd = getcwd(NULL, 0);
13762 error = got_error_from_errno("getcwd");
13766 error = got_repo_pack_fds_open(&pack_fds);
13770 if (repo_path == NULL) {
13771 error = got_worktree_open(&worktree, cwd);
13772 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13775 repo_path = strdup(
13776 got_worktree_get_repo_path(worktree));
13777 if (repo_path == NULL) {
13778 error = got_error_from_errno("strdup");
13782 /* Release work tree lock. */
13783 got_worktree_close(worktree);
13788 if (repo_path == NULL) {
13789 repo_path = strdup(cwd);
13790 if (repo_path == NULL)
13791 return got_error_from_errno("strdup");
13794 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13799 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13803 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13807 if (commit_id_str == NULL)
13808 commit_id_str = GOT_REF_HEAD;
13809 error = got_repo_match_object_id(&commit_id, NULL,
13810 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13814 error = got_object_open_as_commit(&commit, repo, commit_id);
13818 for (i = 0; i < argc; i++) {
13820 error = got_object_id_by_path(&id, repo, commit,
13825 error = got_repo_match_object_id(&id, &label, argv[i],
13826 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13829 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13830 error->code != GOT_ERR_NOT_REF)
13832 error = got_object_id_by_path(&id, repo,
13839 error = got_object_get_type(&obj_type, repo, id);
13843 switch (obj_type) {
13844 case GOT_OBJ_TYPE_BLOB:
13845 error = cat_blob(id, repo, stdout);
13847 case GOT_OBJ_TYPE_TREE:
13848 error = cat_tree(id, repo, stdout);
13850 case GOT_OBJ_TYPE_COMMIT:
13851 error = cat_commit(id, repo, stdout);
13853 case GOT_OBJ_TYPE_TAG:
13854 error = cat_tag(id, repo, stdout);
13857 error = got_error(GOT_ERR_OBJ_TYPE);
13872 got_object_commit_close(commit);
13874 got_worktree_close(worktree);
13876 const struct got_error *close_err = got_repo_close(repo);
13881 const struct got_error *pack_err =
13882 got_repo_pack_fds_close(pack_fds);
13887 got_ref_list_free(&refs);
13894 fprintf(stderr, "usage: %s info [path ...]\n",
13899 static const struct got_error *
13900 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13901 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13902 struct got_object_id *commit_id)
13904 const struct got_error *err = NULL;
13905 char *id_str = NULL;
13907 struct tm mytm, *tm;
13908 struct got_pathlist_head *paths = arg;
13909 struct got_pathlist_entry *pe;
13912 * Clear error indication from any of the path arguments which
13913 * would cause this file index entry to be displayed.
13915 TAILQ_FOREACH(pe, paths, entry) {
13916 if (got_path_cmp(path, pe->path, strlen(path),
13917 pe->path_len) == 0 ||
13918 got_path_is_child(path, pe->path, pe->path_len))
13919 pe->data = NULL; /* no error */
13922 printf(GOT_COMMIT_SEP_STR);
13924 printf("symlink: %s\n", path);
13925 else if (S_ISREG(mode)) {
13926 printf("file: %s\n", path);
13927 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13928 } else if (S_ISDIR(mode))
13929 printf("directory: %s\n", path);
13931 printf("something: %s\n", path);
13933 tm = localtime_r(&mtime, &mytm);
13936 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13937 return got_error(GOT_ERR_NO_SPACE);
13938 printf("timestamp: %s\n", datebuf);
13941 err = got_object_id_str(&id_str, blob_id);
13944 printf("based on blob: %s\n", id_str);
13948 if (staged_blob_id) {
13949 err = got_object_id_str(&id_str, staged_blob_id);
13952 printf("based on staged blob: %s\n", id_str);
13957 err = got_object_id_str(&id_str, commit_id);
13960 printf("based on commit: %s\n", id_str);
13967 static const struct got_error *
13968 cmd_info(int argc, char *argv[])
13970 const struct got_error *error = NULL;
13971 struct got_worktree *worktree = NULL;
13972 char *cwd = NULL, *id_str = NULL;
13973 struct got_pathlist_head paths;
13974 char *uuidstr = NULL;
13975 int ch, show_files = 0;
13977 TAILQ_INIT(&paths);
13979 while ((ch = getopt(argc, argv, "")) != -1) {
13991 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13995 cwd = getcwd(NULL, 0);
13997 error = got_error_from_errno("getcwd");
14001 error = got_worktree_open(&worktree, cwd);
14003 if (error->code == GOT_ERR_NOT_WORKTREE)
14004 error = wrap_not_worktree_error(error, "info", cwd);
14009 /* Remove "wpath cpath proc exec sendfd" promises. */
14010 if (pledge("stdio rpath flock unveil", NULL) == -1)
14013 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14018 error = get_worktree_paths_from_argv(&paths, argc, argv,
14025 error = got_object_id_str(&id_str,
14026 got_worktree_get_base_commit_id(worktree));
14030 error = got_worktree_get_uuid(&uuidstr, worktree);
14034 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14035 printf("work tree base commit: %s\n", id_str);
14036 printf("work tree path prefix: %s\n",
14037 got_worktree_get_path_prefix(worktree));
14038 printf("work tree branch reference: %s\n",
14039 got_worktree_get_head_ref_name(worktree));
14040 printf("work tree UUID: %s\n", uuidstr);
14041 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14044 struct got_pathlist_entry *pe;
14045 TAILQ_FOREACH(pe, &paths, entry) {
14046 if (pe->path_len == 0)
14049 * Assume this path will fail. This will be corrected
14050 * in print_path_info() in case the path does suceeed.
14052 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14054 error = got_worktree_path_info(worktree, &paths,
14055 print_path_info, &paths, check_cancelled, NULL);
14058 TAILQ_FOREACH(pe, &paths, entry) {
14059 if (pe->data != NULL) {
14060 const struct got_error *perr;
14063 error = got_error_fmt(perr->code, "%s",
14071 got_worktree_close(worktree);
14072 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);