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>
45 #include "got_version.h"
46 #include "got_error.h"
47 #include "got_object.h"
48 #include "got_reference.h"
49 #include "got_repository.h"
51 #include "got_cancel.h"
52 #include "got_worktree.h"
54 #include "got_commit_graph.h"
55 #include "got_fetch.h"
57 #include "got_blame.h"
58 #include "got_privsep.h"
59 #include "got_opentemp.h"
60 #include "got_gotconfig.h"
62 #include "got_patch.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 static volatile sig_atomic_t sigint_received;
71 static volatile sig_atomic_t sigpipe_received;
74 catch_sigint(int signo)
80 catch_sigpipe(int signo)
88 const struct got_error *(*cmd_main)(int, char *[]);
89 void (*cmd_usage)(void);
90 const char *cmd_alias;
93 __dead static void usage(int, int);
94 __dead static void usage_import(void);
95 __dead static void usage_clone(void);
96 __dead static void usage_fetch(void);
97 __dead static void usage_checkout(void);
98 __dead static void usage_update(void);
99 __dead static void usage_log(void);
100 __dead static void usage_diff(void);
101 __dead static void usage_blame(void);
102 __dead static void usage_tree(void);
103 __dead static void usage_status(void);
104 __dead static void usage_ref(void);
105 __dead static void usage_branch(void);
106 __dead static void usage_tag(void);
107 __dead static void usage_add(void);
108 __dead static void usage_remove(void);
109 __dead static void usage_patch(void);
110 __dead static void usage_revert(void);
111 __dead static void usage_commit(void);
112 __dead static void usage_send(void);
113 __dead static void usage_cherrypick(void);
114 __dead static void usage_backout(void);
115 __dead static void usage_rebase(void);
116 __dead static void usage_histedit(void);
117 __dead static void usage_integrate(void);
118 __dead static void usage_merge(void);
119 __dead static void usage_stage(void);
120 __dead static void usage_unstage(void);
121 __dead static void usage_cat(void);
122 __dead static void usage_info(void);
124 static const struct got_error* cmd_import(int, char *[]);
125 static const struct got_error* cmd_clone(int, char *[]);
126 static const struct got_error* cmd_fetch(int, char *[]);
127 static const struct got_error* cmd_checkout(int, char *[]);
128 static const struct got_error* cmd_update(int, char *[]);
129 static const struct got_error* cmd_log(int, char *[]);
130 static const struct got_error* cmd_diff(int, char *[]);
131 static const struct got_error* cmd_blame(int, char *[]);
132 static const struct got_error* cmd_tree(int, char *[]);
133 static const struct got_error* cmd_status(int, char *[]);
134 static const struct got_error* cmd_ref(int, char *[]);
135 static const struct got_error* cmd_branch(int, char *[]);
136 static const struct got_error* cmd_tag(int, char *[]);
137 static const struct got_error* cmd_add(int, char *[]);
138 static const struct got_error* cmd_remove(int, char *[]);
139 static const struct got_error* cmd_patch(int, char *[]);
140 static const struct got_error* cmd_revert(int, char *[]);
141 static const struct got_error* cmd_commit(int, char *[]);
142 static const struct got_error* cmd_send(int, char *[]);
143 static const struct got_error* cmd_cherrypick(int, char *[]);
144 static const struct got_error* cmd_backout(int, char *[]);
145 static const struct got_error* cmd_rebase(int, char *[]);
146 static const struct got_error* cmd_histedit(int, char *[]);
147 static const struct got_error* cmd_integrate(int, char *[]);
148 static const struct got_error* cmd_merge(int, char *[]);
149 static const struct got_error* cmd_stage(int, char *[]);
150 static const struct got_error* cmd_unstage(int, char *[]);
151 static const struct got_error* cmd_cat(int, char *[]);
152 static const struct got_error* cmd_info(int, char *[]);
154 static const struct got_cmd got_commands[] = {
155 { "import", cmd_import, usage_import, "im" },
156 { "clone", cmd_clone, usage_clone, "cl" },
157 { "fetch", cmd_fetch, usage_fetch, "fe" },
158 { "checkout", cmd_checkout, usage_checkout, "co" },
159 { "update", cmd_update, usage_update, "up" },
160 { "log", cmd_log, usage_log, "" },
161 { "diff", cmd_diff, usage_diff, "di" },
162 { "blame", cmd_blame, usage_blame, "bl" },
163 { "tree", cmd_tree, usage_tree, "tr" },
164 { "status", cmd_status, usage_status, "st" },
165 { "ref", cmd_ref, usage_ref, "" },
166 { "branch", cmd_branch, usage_branch, "br" },
167 { "tag", cmd_tag, usage_tag, "" },
168 { "add", cmd_add, usage_add, "" },
169 { "remove", cmd_remove, usage_remove, "rm" },
170 { "patch", cmd_patch, usage_patch, "pa" },
171 { "revert", cmd_revert, usage_revert, "rv" },
172 { "commit", cmd_commit, usage_commit, "ci" },
173 { "send", cmd_send, usage_send, "se" },
174 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
175 { "backout", cmd_backout, usage_backout, "bo" },
176 { "rebase", cmd_rebase, usage_rebase, "rb" },
177 { "histedit", cmd_histedit, usage_histedit, "he" },
178 { "integrate", cmd_integrate, usage_integrate,"ig" },
179 { "merge", cmd_merge, usage_merge, "mg" },
180 { "stage", cmd_stage, usage_stage, "sg" },
181 { "unstage", cmd_unstage, usage_unstage, "ug" },
182 { "cat", cmd_cat, usage_cat, "" },
183 { "info", cmd_info, usage_info, "" },
187 list_commands(FILE *fp)
191 fprintf(fp, "commands:");
192 for (i = 0; i < nitems(got_commands); i++) {
193 const struct got_cmd *cmd = &got_commands[i];
194 fprintf(fp, " %s", cmd->cmd_name);
200 option_conflict(char a, char b)
202 errx(1, "-%c and -%c options are mutually exclusive", a, b);
206 main(int argc, char *argv[])
208 const struct got_cmd *cmd;
211 int hflag = 0, Vflag = 0;
212 static const struct option longopts[] = {
213 { "version", no_argument, NULL, 'V' },
217 setlocale(LC_CTYPE, "");
219 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
239 got_version_print_str();
244 usage(hflag, hflag ? 0 : 1);
246 signal(SIGINT, catch_sigint);
247 signal(SIGPIPE, catch_sigpipe);
249 for (i = 0; i < nitems(got_commands); i++) {
250 const struct got_error *error;
252 cmd = &got_commands[i];
254 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
255 strcmp(cmd->cmd_alias, argv[0]) != 0)
261 error = cmd->cmd_main(argc, argv);
262 if (error && error->code != GOT_ERR_CANCELLED &&
263 error->code != GOT_ERR_PRIVSEP_EXIT &&
264 !(sigpipe_received &&
265 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
267 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
268 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
275 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
276 list_commands(stderr);
281 usage(int hflag, int status)
283 FILE *fp = (status == 0) ? stdout : stderr;
285 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
292 static const struct got_error *
293 get_editor(char **abspath)
295 const struct got_error *err = NULL;
300 editor = getenv("VISUAL");
302 editor = getenv("EDITOR");
305 err = got_path_find_prog(abspath, editor);
310 if (*abspath == NULL) {
311 *abspath = strdup("/bin/ed");
312 if (*abspath == NULL)
313 return got_error_from_errno("strdup");
319 static const struct got_error *
320 apply_unveil(const char *repo_path, int repo_read_only,
321 const char *worktree_path)
323 const struct got_error *err;
326 if (unveil("gmon.out", "rwc") != 0)
327 return got_error_from_errno2("unveil", "gmon.out");
329 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
330 return got_error_from_errno2("unveil", repo_path);
332 if (worktree_path && unveil(worktree_path, "rwc") != 0)
333 return got_error_from_errno2("unveil", worktree_path);
335 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
336 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
338 err = got_privsep_unveil_exec_helpers();
342 if (unveil(NULL, NULL) != 0)
343 return got_error_from_errno("unveil");
351 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
352 "[-r repository-path] directory\n", getprogname());
357 spawn_editor(const char *editor, const char *file)
360 sig_t sighup, sigint, sigquit;
363 sighup = signal(SIGHUP, SIG_IGN);
364 sigint = signal(SIGINT, SIG_IGN);
365 sigquit = signal(SIGQUIT, SIG_IGN);
367 switch (pid = fork()) {
371 execl(editor, editor, file, (char *)NULL);
375 while (waitpid(pid, &st, 0) == -1)
380 (void)signal(SIGHUP, sighup);
381 (void)signal(SIGINT, sigint);
382 (void)signal(SIGQUIT, sigquit);
384 if (!WIFEXITED(st)) {
389 return WEXITSTATUS(st);
392 static const struct got_error *
393 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
394 const char *initial_content, size_t initial_content_len,
395 int require_modification)
397 const struct got_error *err = NULL;
402 size_t len, logmsg_len;
403 char *initial_content_stripped = NULL, *buf = NULL, *s;
407 if (stat(logmsg_path, &st) == -1)
408 return got_error_from_errno2("stat", logmsg_path);
410 if (spawn_editor(editor, logmsg_path) == -1)
411 return got_error_from_errno("failed spawning editor");
413 if (stat(logmsg_path, &st2) == -1)
414 return got_error_from_errno("stat");
416 if (require_modification &&
417 st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
418 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
419 "no changes made to commit message, aborting");
422 * Set up a stripped version of the initial content without comments
423 * and blank lines. We need this in order to check if the message
424 * has in fact been edited.
426 initial_content_stripped = malloc(initial_content_len + 1);
427 if (initial_content_stripped == NULL)
428 return got_error_from_errno("malloc");
429 initial_content_stripped[0] = '\0';
431 buf = strdup(initial_content);
433 err = got_error_from_errno("strdup");
438 while ((line = strsep(&s, "\n")) != NULL) {
439 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
440 continue; /* remove comments and leading empty lines */
441 len = strlcat(initial_content_stripped, line,
442 initial_content_len + 1);
443 if (len >= initial_content_len + 1) {
444 err = got_error(GOT_ERR_NO_SPACE);
448 while (len > 0 && initial_content_stripped[len - 1] == '\n') {
449 initial_content_stripped[len - 1] = '\0';
453 logmsg_len = st2.st_size;
454 *logmsg = malloc(logmsg_len + 1);
456 return got_error_from_errno("malloc");
459 fp = fopen(logmsg_path, "re");
461 err = got_error_from_errno("fopen");
466 while (getline(&line, &linesize, fp) != -1) {
467 if ((line[0] == '#' || (len == 0 && line[0] == '\n')))
468 continue; /* remove comments and leading empty lines */
469 len = strlcat(*logmsg, line, logmsg_len + 1);
470 if (len >= logmsg_len + 1) {
471 err = got_error(GOT_ERR_NO_SPACE);
477 err = got_ferror(fp, GOT_ERR_IO);
480 while (len > 0 && (*logmsg)[len - 1] == '\n') {
481 (*logmsg)[len - 1] = '\0';
486 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
487 "commit message cannot be empty, aborting");
490 if (require_modification &&
491 strcmp(*logmsg, initial_content_stripped) == 0)
492 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
493 "no changes made to commit message, aborting");
495 free(initial_content_stripped);
497 if (fp && fclose(fp) == EOF && err == NULL)
498 err = got_error_from_errno("fclose");
506 static const struct got_error *
507 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
508 const char *path_dir, const char *branch_name)
510 char *initial_content = NULL;
511 const struct got_error *err = NULL;
512 int initial_content_len;
515 initial_content_len = asprintf(&initial_content,
516 "\n# %s to be imported to branch %s\n", path_dir,
518 if (initial_content_len == -1)
519 return got_error_from_errno("asprintf");
521 err = got_opentemp_named_fd(logmsg_path, &fd,
522 GOT_TMPDIR_STR "/got-importmsg", "");
526 if (write(fd, initial_content, initial_content_len) == -1) {
527 err = got_error_from_errno2("write", *logmsg_path);
531 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
532 initial_content_len, 1);
534 if (fd != -1 && close(fd) == -1 && err == NULL)
535 err = got_error_from_errno2("close", *logmsg_path);
536 free(initial_content);
544 static const struct got_error *
545 import_progress(void *arg, const char *path)
547 printf("A %s\n", path);
551 static const struct got_error *
552 valid_author(const char *author)
554 const char *email = author;
557 * Git' expects the author (or committer) to be in the form
558 * "name <email>", which are mostly free form (see the
559 * "committer" description in git-fast-import(1)). We're only
560 * doing this to avoid git's object parser breaking on commits
564 while (*author && *author != '\n' && *author != '<' && *author != '>')
566 if (author != email && *author == '<' && *(author - 1) != ' ')
567 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
568 "between author name and email required", email);
569 if (*author++ != '<')
570 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
571 while (*author && *author != '\n' && *author != '<' && *author != '>')
573 if (strcmp(author, ">") != 0)
574 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
578 static const struct got_error *
579 get_author(char **author, struct got_repository *repo,
580 struct got_worktree *worktree)
582 const struct got_error *err = NULL;
583 const char *got_author = NULL, *name, *email;
584 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
589 worktree_conf = got_worktree_get_gotconfig(worktree);
590 repo_conf = got_repo_get_gotconfig(repo);
593 * Priority of potential author information sources, from most
594 * significant to least significant:
595 * 1) work tree's .got/got.conf file
596 * 2) repository's got.conf file
597 * 3) repository's git config file
598 * 4) environment variables
599 * 5) global git config files (in user's home directory or /etc)
603 got_author = got_gotconfig_get_author(worktree_conf);
604 if (got_author == NULL)
605 got_author = got_gotconfig_get_author(repo_conf);
606 if (got_author == NULL) {
607 name = got_repo_get_gitconfig_author_name(repo);
608 email = got_repo_get_gitconfig_author_email(repo);
610 if (asprintf(author, "%s <%s>", name, email) == -1)
611 return got_error_from_errno("asprintf");
615 got_author = getenv("GOT_AUTHOR");
616 if (got_author == NULL) {
617 name = got_repo_get_global_gitconfig_author_name(repo);
618 email = got_repo_get_global_gitconfig_author_email(
621 if (asprintf(author, "%s <%s>", name, email)
623 return got_error_from_errno("asprintf");
626 /* TODO: Look up user in password database? */
627 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
631 *author = strdup(got_author);
633 return got_error_from_errno("strdup");
635 err = valid_author(*author);
643 static const struct got_error *
644 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
645 struct got_worktree *worktree)
647 const char *got_allowed_signers = NULL;
648 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
650 *allowed_signers = NULL;
653 worktree_conf = got_worktree_get_gotconfig(worktree);
654 repo_conf = got_repo_get_gotconfig(repo);
657 * Priority of potential author information sources, from most
658 * significant to least significant:
659 * 1) work tree's .got/got.conf file
660 * 2) repository's got.conf file
664 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
666 if (got_allowed_signers == NULL)
667 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
670 if (got_allowed_signers) {
671 *allowed_signers = strdup(got_allowed_signers);
672 if (*allowed_signers == NULL)
673 return got_error_from_errno("strdup");
678 static const struct got_error *
679 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
680 struct got_worktree *worktree)
682 const char *got_revoked_signers = NULL;
683 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
685 *revoked_signers = NULL;
688 worktree_conf = got_worktree_get_gotconfig(worktree);
689 repo_conf = got_repo_get_gotconfig(repo);
692 * Priority of potential author information sources, from most
693 * significant to least significant:
694 * 1) work tree's .got/got.conf file
695 * 2) repository's got.conf file
699 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
701 if (got_revoked_signers == NULL)
702 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
705 if (got_revoked_signers) {
706 *revoked_signers = strdup(got_revoked_signers);
707 if (*revoked_signers == NULL)
708 return got_error_from_errno("strdup");
714 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
716 const char *got_signer_id = NULL;
717 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
720 worktree_conf = got_worktree_get_gotconfig(worktree);
721 repo_conf = got_repo_get_gotconfig(repo);
724 * Priority of potential author information sources, from most
725 * significant to least significant:
726 * 1) work tree's .got/got.conf file
727 * 2) repository's got.conf file
731 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
732 if (got_signer_id == NULL)
733 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
735 return got_signer_id;
738 static const struct got_error *
739 get_gitconfig_path(char **gitconfig_path)
741 const char *homedir = getenv("HOME");
743 *gitconfig_path = NULL;
745 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
746 return got_error_from_errno("asprintf");
752 static const struct got_error *
753 cmd_import(int argc, char *argv[])
755 const struct got_error *error = NULL;
756 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
757 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
758 const char *branch_name = NULL;
759 char *id_str = NULL, *logmsg_path = NULL;
760 char refname[PATH_MAX] = "refs/heads/";
761 struct got_repository *repo = NULL;
762 struct got_reference *branch_ref = NULL, *head_ref = NULL;
763 struct got_object_id *new_commit_id = NULL;
765 struct got_pathlist_head ignores;
766 struct got_pathlist_entry *pe;
767 int preserve_logmsg = 0;
768 int *pack_fds = NULL;
770 TAILQ_INIT(&ignores);
772 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
775 branch_name = optarg;
778 if (optarg[0] == '\0')
780 error = got_pathlist_insert(&pe, &ignores, optarg,
786 logmsg = strdup(optarg);
787 if (logmsg == NULL) {
788 error = got_error_from_errno("strdup");
793 repo_path = realpath(optarg, NULL);
794 if (repo_path == NULL) {
795 error = got_error_from_errno2("realpath",
810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
818 if (repo_path == NULL) {
819 repo_path = getcwd(NULL, 0);
820 if (repo_path == NULL)
821 return got_error_from_errno("getcwd");
823 got_path_strip_trailing_slashes(repo_path);
824 error = get_gitconfig_path(&gitconfig_path);
827 error = got_repo_pack_fds_open(&pack_fds);
830 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
834 error = get_author(&author, repo, NULL);
839 * Don't let the user create a branch name with a leading '-'.
840 * While technically a valid reference name, this case is usually
841 * an unintended typo.
843 if (branch_name && branch_name[0] == '-')
844 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
846 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
847 if (error && error->code != GOT_ERR_NOT_REF)
851 n = strlcat(refname, branch_name, sizeof(refname));
852 else if (head_ref && got_ref_is_symbolic(head_ref))
853 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
856 n = strlcat(refname, "main", sizeof(refname));
857 if (n >= sizeof(refname)) {
858 error = got_error(GOT_ERR_NO_SPACE);
862 error = got_ref_open(&branch_ref, repo, refname, 0);
864 if (error->code != GOT_ERR_NOT_REF)
867 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
868 "import target branch already exists");
872 path_dir = realpath(argv[0], NULL);
873 if (path_dir == NULL) {
874 error = got_error_from_errno2("realpath", argv[0]);
877 got_path_strip_trailing_slashes(path_dir);
880 * unveil(2) traverses exec(2); if an editor is used we have
881 * to apply unveil after the log message has been written.
883 if (logmsg == NULL || strlen(logmsg) == 0) {
884 error = get_editor(&editor);
888 error = collect_import_msg(&logmsg, &logmsg_path, editor,
891 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
898 if (unveil(path_dir, "r") != 0) {
899 error = got_error_from_errno2("unveil", path_dir);
905 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
912 error = got_repo_import(&new_commit_id, path_dir, logmsg,
913 author, &ignores, repo, import_progress, NULL);
920 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
927 error = got_ref_write(branch_ref, repo);
934 error = got_object_id_str(&id_str, new_commit_id);
941 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
943 if (error->code != GOT_ERR_NOT_REF) {
949 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
957 error = got_ref_write(head_ref, repo);
965 printf("Created branch %s with commit %s\n",
966 got_ref_get_name(branch_ref), id_str);
969 const struct got_error *pack_err =
970 got_repo_pack_fds_close(pack_fds);
974 if (preserve_logmsg) {
975 fprintf(stderr, "%s: log message preserved in %s\n",
976 getprogname(), logmsg_path);
977 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
978 error = got_error_from_errno2("unlink", logmsg_path);
986 free(gitconfig_path);
988 got_ref_close(branch_ref);
990 got_ref_close(head_ref);
997 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
998 "repository-URL [directory]\n", getprogname());
1002 struct got_fetch_progress_arg {
1003 char last_scaled_size[FMT_SCALED_STRSIZE];
1005 int last_p_resolved;
1008 struct got_repository *repo;
1011 int configs_created;
1013 struct got_pathlist_head *symrefs;
1014 struct got_pathlist_head *wanted_branches;
1015 struct got_pathlist_head *wanted_refs;
1019 const char *remote_repo_path;
1020 const char *git_url;
1021 int fetch_all_branches;
1022 int mirror_references;
1026 /* XXX forward declaration */
1027 static const struct got_error *
1028 create_config_files(const char *proto, const char *host, const char *port,
1029 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1030 int mirror_references, struct got_pathlist_head *symrefs,
1031 struct got_pathlist_head *wanted_branches,
1032 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1034 static const struct got_error *
1035 fetch_progress(void *arg, const char *message, off_t packfile_size,
1036 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1038 const struct got_error *err = NULL;
1039 struct got_fetch_progress_arg *a = arg;
1040 char scaled_size[FMT_SCALED_STRSIZE];
1041 int p_indexed, p_resolved;
1042 int print_size = 0, print_indexed = 0, print_resolved = 0;
1045 * In order to allow a failed clone to be resumed with 'got fetch'
1046 * we try to create configuration files as soon as possible.
1047 * Once the server has sent information about its default branch
1048 * we have all required information.
1050 if (a->create_configs && !a->configs_created &&
1051 !TAILQ_EMPTY(a->config_info.symrefs)) {
1052 err = create_config_files(a->config_info.proto,
1053 a->config_info.host, a->config_info.port,
1054 a->config_info.remote_repo_path,
1055 a->config_info.git_url,
1056 a->config_info.fetch_all_branches,
1057 a->config_info.mirror_references,
1058 a->config_info.symrefs,
1059 a->config_info.wanted_branches,
1060 a->config_info.wanted_refs, a->repo);
1063 a->configs_created = 1;
1066 if (a->verbosity < 0)
1069 if (message && message[0] != '\0') {
1070 printf("\rserver: %s", message);
1075 if (packfile_size > 0 || nobj_indexed > 0) {
1076 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1077 (a->last_scaled_size[0] == '\0' ||
1078 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1080 if (strlcpy(a->last_scaled_size, scaled_size,
1081 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1082 return got_error(GOT_ERR_NO_SPACE);
1084 if (nobj_indexed > 0) {
1085 p_indexed = (nobj_indexed * 100) / nobj_total;
1086 if (p_indexed != a->last_p_indexed) {
1087 a->last_p_indexed = p_indexed;
1092 if (nobj_resolved > 0) {
1093 p_resolved = (nobj_resolved * 100) /
1094 (nobj_total - nobj_loose);
1095 if (p_resolved != a->last_p_resolved) {
1096 a->last_p_resolved = p_resolved;
1104 if (print_size || print_indexed || print_resolved)
1107 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1109 printf("; indexing %d%%", p_indexed);
1111 printf("; resolving deltas %d%%", p_resolved);
1112 if (print_size || print_indexed || print_resolved)
1118 static const struct got_error *
1119 create_symref(const char *refname, struct got_reference *target_ref,
1120 int verbosity, struct got_repository *repo)
1122 const struct got_error *err;
1123 struct got_reference *head_symref;
1125 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1129 err = got_ref_write(head_symref, repo);
1130 if (err == NULL && verbosity > 0) {
1131 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1132 got_ref_get_name(target_ref));
1134 got_ref_close(head_symref);
1138 static const struct got_error *
1139 list_remote_refs(struct got_pathlist_head *symrefs,
1140 struct got_pathlist_head *refs)
1142 const struct got_error *err;
1143 struct got_pathlist_entry *pe;
1145 TAILQ_FOREACH(pe, symrefs, entry) {
1146 const char *refname = pe->path;
1147 const char *targetref = pe->data;
1149 printf("%s: %s\n", refname, targetref);
1152 TAILQ_FOREACH(pe, refs, entry) {
1153 const char *refname = pe->path;
1154 struct got_object_id *id = pe->data;
1157 err = got_object_id_str(&id_str, id);
1160 printf("%s: %s\n", refname, id_str);
1167 static const struct got_error *
1168 create_ref(const char *refname, struct got_object_id *id,
1169 int verbosity, struct got_repository *repo)
1171 const struct got_error *err = NULL;
1172 struct got_reference *ref;
1175 err = got_object_id_str(&id_str, id);
1179 err = got_ref_alloc(&ref, refname, id);
1183 err = got_ref_write(ref, repo);
1186 if (err == NULL && verbosity >= 0)
1187 printf("Created reference %s: %s\n", refname, id_str);
1194 match_wanted_ref(const char *refname, const char *wanted_ref)
1196 if (strncmp(refname, "refs/", 5) != 0)
1201 * Prevent fetching of references that won't make any
1202 * sense outside of the remote repository's context.
1204 if (strncmp(refname, "got/", 4) == 0)
1206 if (strncmp(refname, "remotes/", 8) == 0)
1209 if (strncmp(wanted_ref, "refs/", 5) == 0)
1212 /* Allow prefix match. */
1213 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1216 /* Allow exact match. */
1217 return (strcmp(refname, wanted_ref) == 0);
1221 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1223 struct got_pathlist_entry *pe;
1225 TAILQ_FOREACH(pe, wanted_refs, entry) {
1226 if (match_wanted_ref(refname, pe->path))
1233 static const struct got_error *
1234 create_wanted_ref(const char *refname, struct got_object_id *id,
1235 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1237 const struct got_error *err;
1238 char *remote_refname;
1240 if (strncmp("refs/", refname, 5) == 0)
1243 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1244 remote_repo_name, refname) == -1)
1245 return got_error_from_errno("asprintf");
1247 err = create_ref(remote_refname, id, verbosity, repo);
1248 free(remote_refname);
1252 static const struct got_error *
1253 create_gotconfig(const char *proto, const char *host, const char *port,
1254 const char *remote_repo_path, const char *default_branch,
1255 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1256 struct got_pathlist_head *wanted_refs, int mirror_references,
1257 struct got_repository *repo)
1259 const struct got_error *err = NULL;
1260 char *gotconfig_path = NULL;
1261 char *gotconfig = NULL;
1262 FILE *gotconfig_file = NULL;
1263 const char *branchname = NULL;
1264 char *branches = NULL, *refs = NULL;
1267 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1268 struct got_pathlist_entry *pe;
1269 TAILQ_FOREACH(pe, wanted_branches, entry) {
1271 branchname = pe->path;
1272 if (strncmp(branchname, "refs/heads/", 11) == 0)
1274 if (asprintf(&s, "%s\"%s\" ",
1275 branches ? branches : "", branchname) == -1) {
1276 err = got_error_from_errno("asprintf");
1282 } else if (!fetch_all_branches && default_branch) {
1283 branchname = default_branch;
1284 if (strncmp(branchname, "refs/heads/", 11) == 0)
1286 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1287 err = got_error_from_errno("asprintf");
1291 if (!TAILQ_EMPTY(wanted_refs)) {
1292 struct got_pathlist_entry *pe;
1293 TAILQ_FOREACH(pe, wanted_refs, entry) {
1295 const char *refname = pe->path;
1296 if (strncmp(refname, "refs/", 5) == 0)
1298 if (asprintf(&s, "%s\"%s\" ",
1299 refs ? refs : "", refname) == -1) {
1300 err = got_error_from_errno("asprintf");
1308 /* Create got.conf(5). */
1309 gotconfig_path = got_repo_get_path_gotconfig(repo);
1310 if (gotconfig_path == NULL) {
1311 err = got_error_from_errno("got_repo_get_path_gotconfig");
1314 gotconfig_file = fopen(gotconfig_path, "ae");
1315 if (gotconfig_file == NULL) {
1316 err = got_error_from_errno2("fopen", gotconfig_path);
1319 if (asprintf(&gotconfig,
1324 "\trepository \"%s\"\n"
1330 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1331 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1332 remote_repo_path, branches ? "\tbranch { " : "",
1333 branches ? branches : "", branches ? "}\n" : "",
1334 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1335 mirror_references ? "\tmirror_references yes\n" : "",
1336 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1337 err = got_error_from_errno("asprintf");
1340 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1341 if (n != strlen(gotconfig)) {
1342 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1347 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1348 err = got_error_from_errno2("fclose", gotconfig_path);
1349 free(gotconfig_path);
1354 static const struct got_error *
1355 create_gitconfig(const char *git_url, const char *default_branch,
1356 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1357 struct got_pathlist_head *wanted_refs, int mirror_references,
1358 struct got_repository *repo)
1360 const struct got_error *err = NULL;
1361 char *gitconfig_path = NULL;
1362 char *gitconfig = NULL;
1363 FILE *gitconfig_file = NULL;
1364 char *branches = NULL, *refs = NULL;
1365 const char *branchname;
1368 /* Create a config file Git can understand. */
1369 gitconfig_path = got_repo_get_path_gitconfig(repo);
1370 if (gitconfig_path == NULL) {
1371 err = got_error_from_errno("got_repo_get_path_gitconfig");
1374 gitconfig_file = fopen(gitconfig_path, "ae");
1375 if (gitconfig_file == NULL) {
1376 err = got_error_from_errno2("fopen", gitconfig_path);
1379 if (fetch_all_branches) {
1380 if (mirror_references) {
1381 if (asprintf(&branches,
1382 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1383 err = got_error_from_errno("asprintf");
1386 } else if (asprintf(&branches,
1387 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1388 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1389 err = got_error_from_errno("asprintf");
1392 } else if (!TAILQ_EMPTY(wanted_branches)) {
1393 struct got_pathlist_entry *pe;
1394 TAILQ_FOREACH(pe, wanted_branches, entry) {
1396 branchname = pe->path;
1397 if (strncmp(branchname, "refs/heads/", 11) == 0)
1399 if (mirror_references) {
1401 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1402 branches ? branches : "",
1403 branchname, branchname) == -1) {
1404 err = got_error_from_errno("asprintf");
1407 } else if (asprintf(&s,
1408 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1409 branches ? branches : "",
1410 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1411 branchname) == -1) {
1412 err = got_error_from_errno("asprintf");
1420 * If the server specified a default branch, use just that one.
1421 * Otherwise fall back to fetching all branches on next fetch.
1423 if (default_branch) {
1424 branchname = default_branch;
1425 if (strncmp(branchname, "refs/heads/", 11) == 0)
1428 branchname = "*"; /* fall back to all branches */
1429 if (mirror_references) {
1430 if (asprintf(&branches,
1431 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1432 branchname, branchname) == -1) {
1433 err = got_error_from_errno("asprintf");
1436 } else if (asprintf(&branches,
1437 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1438 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1439 branchname) == -1) {
1440 err = got_error_from_errno("asprintf");
1444 if (!TAILQ_EMPTY(wanted_refs)) {
1445 struct got_pathlist_entry *pe;
1446 TAILQ_FOREACH(pe, wanted_refs, entry) {
1448 const char *refname = pe->path;
1449 if (strncmp(refname, "refs/", 5) == 0)
1451 if (mirror_references) {
1453 "%s\tfetch = refs/%s:refs/%s\n",
1454 refs ? refs : "", refname, refname) == -1) {
1455 err = got_error_from_errno("asprintf");
1458 } else if (asprintf(&s,
1459 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1461 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1463 err = got_error_from_errno("asprintf");
1471 if (asprintf(&gitconfig,
1476 "\tfetch = refs/tags/*:refs/tags/*\n",
1477 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1478 refs ? refs : "") == -1) {
1479 err = got_error_from_errno("asprintf");
1482 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1483 if (n != strlen(gitconfig)) {
1484 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1488 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1489 err = got_error_from_errno2("fclose", gitconfig_path);
1490 free(gitconfig_path);
1495 static const struct got_error *
1496 create_config_files(const char *proto, const char *host, const char *port,
1497 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1498 int mirror_references, struct got_pathlist_head *symrefs,
1499 struct got_pathlist_head *wanted_branches,
1500 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1502 const struct got_error *err = NULL;
1503 const char *default_branch = NULL;
1504 struct got_pathlist_entry *pe;
1507 * If we asked for a set of wanted branches then use the first
1510 if (!TAILQ_EMPTY(wanted_branches)) {
1511 pe = TAILQ_FIRST(wanted_branches);
1512 default_branch = pe->path;
1514 /* First HEAD ref listed by server is the default branch. */
1515 TAILQ_FOREACH(pe, symrefs, entry) {
1516 const char *refname = pe->path;
1517 const char *target = pe->data;
1519 if (strcmp(refname, GOT_REF_HEAD) != 0)
1522 default_branch = target;
1527 /* Create got.conf(5). */
1528 err = create_gotconfig(proto, host, port, remote_repo_path,
1529 default_branch, fetch_all_branches, wanted_branches,
1530 wanted_refs, mirror_references, repo);
1534 /* Create a config file Git can understand. */
1535 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1536 wanted_branches, wanted_refs, mirror_references, repo);
1539 static const struct got_error *
1540 cmd_clone(int argc, char *argv[])
1542 const struct got_error *error = NULL;
1543 const char *uri, *dirname;
1544 char *proto, *host, *port, *repo_name, *server_path;
1545 char *default_destdir = NULL, *id_str = NULL;
1546 const char *repo_path;
1547 struct got_repository *repo = NULL;
1548 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1549 struct got_pathlist_entry *pe;
1550 struct got_object_id *pack_hash = NULL;
1551 int ch, fetchfd = -1, fetchstatus;
1552 pid_t fetchpid = -1;
1553 struct got_fetch_progress_arg fpa;
1554 char *git_url = NULL;
1555 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1556 int list_refs_only = 0;
1557 int *pack_fds = NULL;
1560 TAILQ_INIT(&symrefs);
1561 TAILQ_INIT(&wanted_branches);
1562 TAILQ_INIT(&wanted_refs);
1564 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1567 fetch_all_branches = 1;
1570 error = got_pathlist_append(&wanted_branches,
1579 mirror_references = 1;
1585 error = got_pathlist_append(&wanted_refs,
1593 else if (verbosity < 3)
1604 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1605 option_conflict('a', 'b');
1606 if (list_refs_only) {
1607 if (!TAILQ_EMPTY(&wanted_branches))
1608 option_conflict('l', 'b');
1609 if (fetch_all_branches)
1610 option_conflict('l', 'a');
1611 if (mirror_references)
1612 option_conflict('l', 'm');
1613 if (!TAILQ_EMPTY(&wanted_refs))
1614 option_conflict('l', 'R');
1626 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1631 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1632 host, port ? ":" : "", port ? port : "",
1633 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1634 error = got_error_from_errno("asprintf");
1638 if (strcmp(proto, "git") == 0) {
1640 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1641 "sendfd dns inet unveil", NULL) == -1)
1644 } else if (strcmp(proto, "git+ssh") == 0 ||
1645 strcmp(proto, "ssh") == 0) {
1647 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1648 "sendfd unveil", NULL) == -1)
1651 } else if (strcmp(proto, "http") == 0 ||
1652 strcmp(proto, "git+http") == 0) {
1653 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1656 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1659 if (dirname == NULL) {
1660 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1661 error = got_error_from_errno("asprintf");
1664 repo_path = default_destdir;
1666 repo_path = dirname;
1668 if (!list_refs_only) {
1669 error = got_path_mkdir(repo_path);
1671 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1672 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1674 if (!got_path_dir_is_empty(repo_path)) {
1675 error = got_error_path(repo_path,
1676 GOT_ERR_DIR_NOT_EMPTY);
1681 error = got_dial_apply_unveil(proto);
1685 error = apply_unveil(repo_path, 0, NULL);
1690 printf("Connecting to %s\n", git_url);
1692 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1693 server_path, verbosity);
1697 if (!list_refs_only) {
1698 error = got_repo_init(repo_path, NULL);
1701 error = got_repo_pack_fds_open(&pack_fds);
1704 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1709 fpa.last_scaled_size[0] = '\0';
1710 fpa.last_p_indexed = -1;
1711 fpa.last_p_resolved = -1;
1712 fpa.verbosity = verbosity;
1713 fpa.create_configs = 1;
1714 fpa.configs_created = 0;
1716 fpa.config_info.symrefs = &symrefs;
1717 fpa.config_info.wanted_branches = &wanted_branches;
1718 fpa.config_info.wanted_refs = &wanted_refs;
1719 fpa.config_info.proto = proto;
1720 fpa.config_info.host = host;
1721 fpa.config_info.port = port;
1722 fpa.config_info.remote_repo_path = server_path;
1723 fpa.config_info.git_url = git_url;
1724 fpa.config_info.fetch_all_branches = fetch_all_branches;
1725 fpa.config_info.mirror_references = mirror_references;
1726 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1727 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1728 fetch_all_branches, &wanted_branches, &wanted_refs,
1729 list_refs_only, verbosity, fetchfd, repo,
1730 fetch_progress, &fpa);
1734 if (list_refs_only) {
1735 error = list_remote_refs(&symrefs, &refs);
1739 if (pack_hash == NULL) {
1740 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1741 "server sent an empty pack file");
1744 error = got_object_id_str(&id_str, pack_hash);
1748 printf("\nFetched %s.pack\n", id_str);
1751 /* Set up references provided with the pack file. */
1752 TAILQ_FOREACH(pe, &refs, entry) {
1753 const char *refname = pe->path;
1754 struct got_object_id *id = pe->data;
1755 char *remote_refname;
1757 if (is_wanted_ref(&wanted_refs, refname) &&
1758 !mirror_references) {
1759 error = create_wanted_ref(refname, id,
1760 GOT_FETCH_DEFAULT_REMOTE_NAME,
1761 verbosity - 1, repo);
1767 error = create_ref(refname, id, verbosity - 1, repo);
1771 if (mirror_references)
1774 if (strncmp("refs/heads/", refname, 11) != 0)
1777 if (asprintf(&remote_refname,
1778 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1779 refname + 11) == -1) {
1780 error = got_error_from_errno("asprintf");
1783 error = create_ref(remote_refname, id, verbosity - 1, repo);
1784 free(remote_refname);
1789 /* Set the HEAD reference if the server provided one. */
1790 TAILQ_FOREACH(pe, &symrefs, entry) {
1791 struct got_reference *target_ref;
1792 const char *refname = pe->path;
1793 const char *target = pe->data;
1794 char *remote_refname = NULL, *remote_target = NULL;
1796 if (strcmp(refname, GOT_REF_HEAD) != 0)
1799 error = got_ref_open(&target_ref, repo, target, 0);
1801 if (error->code == GOT_ERR_NOT_REF) {
1808 error = create_symref(refname, target_ref, verbosity, repo);
1809 got_ref_close(target_ref);
1813 if (mirror_references)
1816 if (strncmp("refs/heads/", target, 11) != 0)
1819 if (asprintf(&remote_refname,
1820 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1822 error = got_error_from_errno("asprintf");
1825 if (asprintf(&remote_target,
1826 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1827 target + 11) == -1) {
1828 error = got_error_from_errno("asprintf");
1829 free(remote_refname);
1832 error = got_ref_open(&target_ref, repo, remote_target, 0);
1834 free(remote_refname);
1835 free(remote_target);
1836 if (error->code == GOT_ERR_NOT_REF) {
1842 error = create_symref(remote_refname, target_ref,
1843 verbosity - 1, repo);
1844 free(remote_refname);
1845 free(remote_target);
1846 got_ref_close(target_ref);
1852 * We failed to set the HEAD reference. If we asked for
1853 * a set of wanted branches use the first of one of those
1854 * which could be fetched instead.
1856 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1857 const char *target = pe->path;
1858 struct got_reference *target_ref;
1860 error = got_ref_open(&target_ref, repo, target, 0);
1862 if (error->code == GOT_ERR_NOT_REF) {
1869 error = create_symref(GOT_REF_HEAD, target_ref,
1871 got_ref_close(target_ref);
1877 if (!fpa.configs_created && pe != NULL) {
1878 error = create_config_files(fpa.config_info.proto,
1879 fpa.config_info.host, fpa.config_info.port,
1880 fpa.config_info.remote_repo_path,
1881 fpa.config_info.git_url,
1882 fpa.config_info.fetch_all_branches,
1883 fpa.config_info.mirror_references,
1884 fpa.config_info.symrefs,
1885 fpa.config_info.wanted_branches,
1886 fpa.config_info.wanted_refs, fpa.repo);
1893 printf("Created %s repository '%s'\n",
1894 mirror_references ? "mirrored" : "cloned", repo_path);
1897 const struct got_error *pack_err =
1898 got_repo_pack_fds_close(pack_fds);
1903 if (kill(fetchpid, SIGTERM) == -1)
1904 error = got_error_from_errno("kill");
1905 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1906 error = got_error_from_errno("waitpid");
1908 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1909 error = got_error_from_errno("close");
1911 const struct got_error *close_err = got_repo_close(repo);
1915 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1916 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1917 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1918 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1925 free(default_destdir);
1930 static const struct got_error *
1931 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1932 int replace_tags, int verbosity, struct got_repository *repo)
1934 const struct got_error *err = NULL;
1935 char *new_id_str = NULL;
1936 struct got_object_id *old_id = NULL;
1938 err = got_object_id_str(&new_id_str, new_id);
1942 if (!replace_tags &&
1943 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1944 err = got_ref_resolve(&old_id, repo, ref);
1947 if (got_object_id_cmp(old_id, new_id) == 0)
1949 if (verbosity >= 0) {
1950 printf("Rejecting update of existing tag %s: %s\n",
1951 got_ref_get_name(ref), new_id_str);
1956 if (got_ref_is_symbolic(ref)) {
1957 if (verbosity >= 0) {
1958 printf("Replacing reference %s: %s\n",
1959 got_ref_get_name(ref),
1960 got_ref_get_symref_target(ref));
1962 err = got_ref_change_symref_to_ref(ref, new_id);
1965 err = got_ref_write(ref, repo);
1969 err = got_ref_resolve(&old_id, repo, ref);
1972 if (got_object_id_cmp(old_id, new_id) == 0)
1975 err = got_ref_change_ref(ref, new_id);
1978 err = got_ref_write(ref, repo);
1984 printf("Updated %s: %s\n", got_ref_get_name(ref),
1992 static const struct got_error *
1993 update_symref(const char *refname, struct got_reference *target_ref,
1994 int verbosity, struct got_repository *repo)
1996 const struct got_error *err = NULL, *unlock_err;
1997 struct got_reference *symref;
1998 int symref_is_locked = 0;
2000 err = got_ref_open(&symref, repo, refname, 1);
2002 if (err->code != GOT_ERR_NOT_REF)
2004 err = got_ref_alloc_symref(&symref, refname, target_ref);
2008 err = got_ref_write(symref, repo);
2013 printf("Created reference %s: %s\n",
2014 got_ref_get_name(symref),
2015 got_ref_get_symref_target(symref));
2017 symref_is_locked = 1;
2019 if (strcmp(got_ref_get_symref_target(symref),
2020 got_ref_get_name(target_ref)) == 0)
2023 err = got_ref_change_symref(symref,
2024 got_ref_get_name(target_ref));
2028 err = got_ref_write(symref, repo);
2033 printf("Updated %s: %s\n", got_ref_get_name(symref),
2034 got_ref_get_symref_target(symref));
2038 if (symref_is_locked) {
2039 unlock_err = got_ref_unlock(symref);
2040 if (unlock_err && err == NULL)
2043 got_ref_close(symref);
2050 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2051 "[-R reference] [-r repository-path] [remote-repository]\n",
2056 static const struct got_error *
2057 delete_missing_ref(struct got_reference *ref,
2058 int verbosity, struct got_repository *repo)
2060 const struct got_error *err = NULL;
2061 struct got_object_id *id = NULL;
2062 char *id_str = NULL;
2064 if (got_ref_is_symbolic(ref)) {
2065 err = got_ref_delete(ref, repo);
2068 if (verbosity >= 0) {
2069 printf("Deleted %s: %s\n",
2070 got_ref_get_name(ref),
2071 got_ref_get_symref_target(ref));
2074 err = got_ref_resolve(&id, repo, ref);
2077 err = got_object_id_str(&id_str, id);
2081 err = got_ref_delete(ref, repo);
2084 if (verbosity >= 0) {
2085 printf("Deleted %s: %s\n",
2086 got_ref_get_name(ref), id_str);
2095 static const struct got_error *
2096 delete_missing_refs(struct got_pathlist_head *their_refs,
2097 struct got_pathlist_head *their_symrefs,
2098 const struct got_remote_repo *remote,
2099 int verbosity, struct got_repository *repo)
2101 const struct got_error *err = NULL, *unlock_err;
2102 struct got_reflist_head my_refs;
2103 struct got_reflist_entry *re;
2104 struct got_pathlist_entry *pe;
2105 char *remote_namespace = NULL;
2106 char *local_refname = NULL;
2108 TAILQ_INIT(&my_refs);
2110 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2112 return got_error_from_errno("asprintf");
2114 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2118 TAILQ_FOREACH(re, &my_refs, entry) {
2119 const char *refname = got_ref_get_name(re->ref);
2120 const char *their_refname;
2122 if (remote->mirror_references) {
2123 their_refname = refname;
2125 if (strncmp(refname, remote_namespace,
2126 strlen(remote_namespace)) == 0) {
2127 if (strcmp(refname + strlen(remote_namespace),
2130 if (asprintf(&local_refname, "refs/heads/%s",
2131 refname + strlen(remote_namespace)) == -1) {
2132 err = got_error_from_errno("asprintf");
2135 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2138 their_refname = local_refname;
2141 TAILQ_FOREACH(pe, their_refs, entry) {
2142 if (strcmp(their_refname, pe->path) == 0)
2148 TAILQ_FOREACH(pe, their_symrefs, entry) {
2149 if (strcmp(their_refname, pe->path) == 0)
2155 err = delete_missing_ref(re->ref, verbosity, repo);
2159 if (local_refname) {
2160 struct got_reference *ref;
2161 err = got_ref_open(&ref, repo, local_refname, 1);
2163 if (err->code != GOT_ERR_NOT_REF)
2165 free(local_refname);
2166 local_refname = NULL;
2169 err = delete_missing_ref(ref, verbosity, repo);
2172 unlock_err = got_ref_unlock(ref);
2174 if (unlock_err && err == NULL) {
2179 free(local_refname);
2180 local_refname = NULL;
2184 got_ref_list_free(&my_refs);
2185 free(remote_namespace);
2186 free(local_refname);
2190 static const struct got_error *
2191 update_wanted_ref(const char *refname, struct got_object_id *id,
2192 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2194 const struct got_error *err, *unlock_err;
2195 char *remote_refname;
2196 struct got_reference *ref;
2198 if (strncmp("refs/", refname, 5) == 0)
2201 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2202 remote_repo_name, refname) == -1)
2203 return got_error_from_errno("asprintf");
2205 err = got_ref_open(&ref, repo, remote_refname, 1);
2207 if (err->code != GOT_ERR_NOT_REF)
2209 err = create_ref(remote_refname, id, verbosity, repo);
2211 err = update_ref(ref, id, 0, verbosity, repo);
2212 unlock_err = got_ref_unlock(ref);
2213 if (unlock_err && err == NULL)
2218 free(remote_refname);
2222 static const struct got_error *
2223 delete_ref(struct got_repository *repo, struct got_reference *ref)
2225 const struct got_error *err = NULL;
2226 struct got_object_id *id = NULL;
2227 char *id_str = NULL;
2230 if (got_ref_is_symbolic(ref)) {
2231 target = got_ref_get_symref_target(ref);
2233 err = got_ref_resolve(&id, repo, ref);
2236 err = got_object_id_str(&id_str, id);
2242 err = got_ref_delete(ref, repo);
2246 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2253 static const struct got_error *
2254 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2256 const struct got_error *err = NULL;
2257 struct got_reflist_head refs;
2258 struct got_reflist_entry *re;
2263 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2264 err = got_error_from_errno("asprintf");
2267 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2271 TAILQ_FOREACH(re, &refs, entry)
2272 delete_ref(repo, re->ref);
2274 got_ref_list_free(&refs);
2278 static const struct got_error *
2279 cmd_fetch(int argc, char *argv[])
2281 const struct got_error *error = NULL, *unlock_err;
2282 char *cwd = NULL, *repo_path = NULL;
2283 const char *remote_name;
2284 char *proto = NULL, *host = NULL, *port = NULL;
2285 char *repo_name = NULL, *server_path = NULL;
2286 const struct got_remote_repo *remotes, *remote = NULL;
2288 char *id_str = NULL;
2289 struct got_repository *repo = NULL;
2290 struct got_worktree *worktree = NULL;
2291 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2292 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2293 struct got_pathlist_entry *pe;
2294 struct got_object_id *pack_hash = NULL;
2295 int i, ch, fetchfd = -1, fetchstatus;
2296 pid_t fetchpid = -1;
2297 struct got_fetch_progress_arg fpa;
2298 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2299 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2300 int *pack_fds = NULL;
2303 TAILQ_INIT(&symrefs);
2304 TAILQ_INIT(&wanted_branches);
2305 TAILQ_INIT(&wanted_refs);
2307 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2310 fetch_all_branches = 1;
2313 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];
2447 if (TAILQ_EMPTY(&wanted_branches)) {
2448 error = got_pathlist_append(&wanted_branches,
2449 got_worktree_get_head_ref_name(worktree), NULL);
2454 if (remote == NULL) {
2455 repo_conf = got_repo_get_gotconfig(repo);
2457 got_gotconfig_get_remotes(&nremotes, &remotes,
2459 for (i = 0; i < nremotes; i++) {
2460 if (strcmp(remotes[i].name, remote_name) == 0) {
2461 remote = &remotes[i];
2467 if (remote == NULL) {
2468 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2469 for (i = 0; i < nremotes; i++) {
2470 if (strcmp(remotes[i].name, remote_name) == 0) {
2471 remote = &remotes[i];
2476 if (remote == NULL) {
2477 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2481 if (TAILQ_EMPTY(&wanted_branches)) {
2482 if (!fetch_all_branches)
2483 fetch_all_branches = remote->fetch_all_branches;
2484 for (i = 0; i < remote->nfetch_branches; i++) {
2485 error = got_pathlist_append(&wanted_branches,
2486 remote->fetch_branches[i], NULL);
2491 if (TAILQ_EMPTY(&wanted_refs)) {
2492 for (i = 0; i < remote->nfetch_refs; i++) {
2493 error = got_pathlist_append(&wanted_refs,
2494 remote->fetch_refs[i], NULL);
2500 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2501 &repo_name, remote->fetch_url);
2505 if (strcmp(proto, "git") == 0) {
2507 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2508 "sendfd dns inet unveil", NULL) == -1)
2511 } else if (strcmp(proto, "git+ssh") == 0 ||
2512 strcmp(proto, "ssh") == 0) {
2514 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2515 "sendfd unveil", NULL) == -1)
2518 } else if (strcmp(proto, "http") == 0 ||
2519 strcmp(proto, "git+http") == 0) {
2520 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2523 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2527 error = got_dial_apply_unveil(proto);
2531 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2535 if (verbosity >= 0) {
2536 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2537 remote->name, proto, host,
2538 port ? ":" : "", port ? port : "",
2539 *server_path == '/' ? "" : "/", server_path);
2542 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2543 server_path, verbosity);
2547 fpa.last_scaled_size[0] = '\0';
2548 fpa.last_p_indexed = -1;
2549 fpa.last_p_resolved = -1;
2550 fpa.verbosity = verbosity;
2552 fpa.create_configs = 0;
2553 fpa.configs_created = 0;
2554 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2555 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2556 remote->mirror_references, fetch_all_branches, &wanted_branches,
2557 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2558 fetch_progress, &fpa);
2562 if (list_refs_only) {
2563 error = list_remote_refs(&symrefs, &refs);
2567 if (pack_hash == NULL) {
2569 printf("Already up-to-date\n");
2570 } else if (verbosity >= 0) {
2571 error = got_object_id_str(&id_str, pack_hash);
2574 printf("\nFetched %s.pack\n", id_str);
2579 /* Update references provided with the pack file. */
2580 TAILQ_FOREACH(pe, &refs, entry) {
2581 const char *refname = pe->path;
2582 struct got_object_id *id = pe->data;
2583 struct got_reference *ref;
2584 char *remote_refname;
2586 if (is_wanted_ref(&wanted_refs, refname) &&
2587 !remote->mirror_references) {
2588 error = update_wanted_ref(refname, id,
2589 remote->name, verbosity, repo);
2595 if (remote->mirror_references ||
2596 strncmp("refs/tags/", refname, 10) == 0) {
2597 error = got_ref_open(&ref, repo, refname, 1);
2599 if (error->code != GOT_ERR_NOT_REF)
2601 error = create_ref(refname, id, verbosity,
2606 error = update_ref(ref, id, replace_tags,
2608 unlock_err = got_ref_unlock(ref);
2609 if (unlock_err && error == NULL)
2615 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2616 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2617 remote_name, refname + 11) == -1) {
2618 error = got_error_from_errno("asprintf");
2622 error = got_ref_open(&ref, repo, remote_refname, 1);
2624 if (error->code != GOT_ERR_NOT_REF)
2626 error = create_ref(remote_refname, id,
2631 error = update_ref(ref, id, replace_tags,
2633 unlock_err = got_ref_unlock(ref);
2634 if (unlock_err && error == NULL)
2641 /* Also create a local branch if none exists yet. */
2642 error = got_ref_open(&ref, repo, refname, 1);
2644 if (error->code != GOT_ERR_NOT_REF)
2646 error = create_ref(refname, id, verbosity,
2651 unlock_err = got_ref_unlock(ref);
2652 if (unlock_err && error == NULL)
2659 error = delete_missing_refs(&refs, &symrefs, remote,
2665 if (!remote->mirror_references) {
2666 /* Update remote HEAD reference if the server provided one. */
2667 TAILQ_FOREACH(pe, &symrefs, entry) {
2668 struct got_reference *target_ref;
2669 const char *refname = pe->path;
2670 const char *target = pe->data;
2671 char *remote_refname = NULL, *remote_target = NULL;
2673 if (strcmp(refname, GOT_REF_HEAD) != 0)
2676 if (strncmp("refs/heads/", target, 11) != 0)
2679 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2680 remote->name, refname) == -1) {
2681 error = got_error_from_errno("asprintf");
2684 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2685 remote->name, target + 11) == -1) {
2686 error = got_error_from_errno("asprintf");
2687 free(remote_refname);
2691 error = got_ref_open(&target_ref, repo, remote_target,
2694 free(remote_refname);
2695 free(remote_target);
2696 if (error->code == GOT_ERR_NOT_REF) {
2702 error = update_symref(remote_refname, target_ref,
2704 free(remote_refname);
2705 free(remote_target);
2706 got_ref_close(target_ref);
2713 if (kill(fetchpid, SIGTERM) == -1)
2714 error = got_error_from_errno("kill");
2715 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2716 error = got_error_from_errno("waitpid");
2718 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2719 error = got_error_from_errno("close");
2721 const struct got_error *close_err = got_repo_close(repo);
2726 got_worktree_close(worktree);
2728 const struct got_error *pack_err =
2729 got_repo_pack_fds_close(pack_fds);
2733 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2734 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2735 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2736 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2751 usage_checkout(void)
2753 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2754 "[-p path-prefix] repository-path [work-tree-path]\n",
2760 show_worktree_base_ref_warning(void)
2762 fprintf(stderr, "%s: warning: could not create a reference "
2763 "to the work tree's base commit; the commit could be "
2764 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2765 "repository writable and running 'got update' will prevent this\n",
2769 struct got_checkout_progress_arg {
2770 const char *worktree_path;
2771 int had_base_commit_ref_error;
2775 static const struct got_error *
2776 checkout_progress(void *arg, unsigned char status, const char *path)
2778 struct got_checkout_progress_arg *a = arg;
2780 /* Base commit bump happens silently. */
2781 if (status == GOT_STATUS_BUMP_BASE)
2784 if (status == GOT_STATUS_BASE_REF_ERR) {
2785 a->had_base_commit_ref_error = 1;
2789 while (path[0] == '/')
2792 if (a->verbosity >= 0)
2793 printf("%c %s/%s\n", status, a->worktree_path, path);
2798 static const struct got_error *
2799 check_cancelled(void *arg)
2801 if (sigint_received || sigpipe_received)
2802 return got_error(GOT_ERR_CANCELLED);
2806 static const struct got_error *
2807 check_linear_ancestry(struct got_object_id *commit_id,
2808 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2809 struct got_repository *repo)
2811 const struct got_error *err = NULL;
2812 struct got_object_id *yca_id;
2814 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2815 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2820 return got_error(GOT_ERR_ANCESTRY);
2823 * Require a straight line of history between the target commit
2824 * and the work tree's base commit.
2826 * Non-linear situations such as this require a rebase:
2828 * (commit) D F (base_commit)
2836 * 'got update' only handles linear cases:
2837 * Update forwards in time: A (base/yca) - B - C - D (commit)
2838 * Update backwards in time: D (base) - C - B - A (commit/yca)
2840 if (allow_forwards_in_time_only) {
2841 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2842 return got_error(GOT_ERR_ANCESTRY);
2843 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2844 got_object_id_cmp(base_commit_id, yca_id) != 0)
2845 return got_error(GOT_ERR_ANCESTRY);
2851 static const struct got_error *
2852 check_same_branch(struct got_object_id *commit_id,
2853 struct got_reference *head_ref, struct got_object_id *yca_id,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_commit_graph *graph = NULL;
2858 struct got_object_id *head_commit_id = NULL;
2859 int is_same_branch = 0;
2861 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2865 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2869 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2874 err = got_commit_graph_open(&graph, "/", 1);
2878 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2879 check_cancelled, NULL);
2884 struct got_object_id id;
2886 err = got_commit_graph_iter_next(&id, graph, repo,
2887 check_cancelled, NULL);
2889 if (err->code == GOT_ERR_ITER_COMPLETED)
2894 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2896 if (got_object_id_cmp(&id, commit_id) == 0) {
2903 got_commit_graph_close(graph);
2904 free(head_commit_id);
2905 if (!err && !is_same_branch)
2906 err = got_error(GOT_ERR_ANCESTRY);
2910 static const struct got_error *
2911 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2913 static char msg[512];
2914 const char *branch_name;
2916 if (got_ref_is_symbolic(ref))
2917 branch_name = got_ref_get_symref_target(ref);
2919 branch_name = got_ref_get_name(ref);
2921 if (strncmp("refs/heads/", branch_name, 11) == 0)
2924 snprintf(msg, sizeof(msg),
2925 "target commit is not contained in branch '%s'; "
2926 "the branch to use must be specified with -b; "
2927 "if necessary a new branch can be created for "
2928 "this commit with 'got branch -c %s BRANCH_NAME'",
2929 branch_name, commit_id_str);
2931 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2934 static const struct got_error *
2935 cmd_checkout(int argc, char *argv[])
2937 const struct got_error *error = NULL;
2938 struct got_repository *repo = NULL;
2939 struct got_reference *head_ref = NULL, *ref = NULL;
2940 struct got_worktree *worktree = NULL;
2941 char *repo_path = NULL;
2942 char *worktree_path = NULL;
2943 const char *path_prefix = "";
2944 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2945 char *commit_id_str = NULL;
2946 struct got_object_id *commit_id = NULL;
2948 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2949 struct got_pathlist_head paths;
2950 struct got_checkout_progress_arg cpa;
2951 int *pack_fds = NULL;
2955 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2958 branch_name = optarg;
2961 commit_id_str = strdup(optarg);
2962 if (commit_id_str == NULL)
2963 return got_error_from_errno("strdup");
2969 path_prefix = optarg;
2984 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2985 "unveil", NULL) == -1)
2989 char *base, *dotgit;
2991 repo_path = realpath(argv[0], NULL);
2992 if (repo_path == NULL)
2993 return got_error_from_errno2("realpath", argv[0]);
2994 cwd = getcwd(NULL, 0);
2996 error = got_error_from_errno("getcwd");
3003 error = got_path_basename(&base, path);
3006 dotgit = strstr(base, ".git");
3009 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3010 error = got_error_from_errno("asprintf");
3015 } else if (argc == 2) {
3016 repo_path = realpath(argv[0], NULL);
3017 if (repo_path == NULL) {
3018 error = got_error_from_errno2("realpath", argv[0]);
3021 worktree_path = realpath(argv[1], NULL);
3022 if (worktree_path == NULL) {
3023 if (errno != ENOENT) {
3024 error = got_error_from_errno2("realpath",
3028 worktree_path = strdup(argv[1]);
3029 if (worktree_path == NULL) {
3030 error = got_error_from_errno("strdup");
3037 got_path_strip_trailing_slashes(repo_path);
3038 got_path_strip_trailing_slashes(worktree_path);
3040 error = got_repo_pack_fds_open(&pack_fds);
3044 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3048 /* Pre-create work tree path for unveil(2) */
3049 error = got_path_mkdir(worktree_path);
3051 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3052 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3054 if (!allow_nonempty &&
3055 !got_path_dir_is_empty(worktree_path)) {
3056 error = got_error_path(worktree_path,
3057 GOT_ERR_DIR_NOT_EMPTY);
3062 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3066 error = got_ref_open(&head_ref, repo, branch_name, 0);
3070 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3071 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3074 error = got_worktree_open(&worktree, worktree_path);
3078 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3082 if (!same_path_prefix) {
3083 error = got_error(GOT_ERR_PATH_PREFIX);
3087 if (commit_id_str) {
3088 struct got_reflist_head refs;
3090 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3094 error = got_repo_match_object_id(&commit_id, NULL,
3095 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3096 got_ref_list_free(&refs);
3099 error = check_linear_ancestry(commit_id,
3100 got_worktree_get_base_commit_id(worktree), 0, repo);
3101 if (error != NULL) {
3102 if (error->code == GOT_ERR_ANCESTRY) {
3103 error = checkout_ancestry_error(
3104 head_ref, commit_id_str);
3108 error = check_same_branch(commit_id, head_ref, NULL, repo);
3110 if (error->code == GOT_ERR_ANCESTRY) {
3111 error = checkout_ancestry_error(
3112 head_ref, commit_id_str);
3116 error = got_worktree_set_base_commit_id(worktree, repo,
3120 /* Expand potentially abbreviated commit ID string. */
3121 free(commit_id_str);
3122 error = got_object_id_str(&commit_id_str, commit_id);
3126 commit_id = got_object_id_dup(
3127 got_worktree_get_base_commit_id(worktree));
3128 if (commit_id == NULL) {
3129 error = got_error_from_errno("got_object_id_dup");
3132 error = got_object_id_str(&commit_id_str, commit_id);
3137 error = got_pathlist_append(&paths, "", NULL);
3140 cpa.worktree_path = worktree_path;
3141 cpa.had_base_commit_ref_error = 0;
3142 cpa.verbosity = verbosity;
3143 error = got_worktree_checkout_files(worktree, &paths, repo,
3144 checkout_progress, &cpa, check_cancelled, NULL);
3148 if (got_ref_is_symbolic(head_ref)) {
3149 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3152 refname = got_ref_get_name(ref);
3154 refname = got_ref_get_name(head_ref);
3155 printf("Checked out %s: %s\n", refname, commit_id_str);
3156 printf("Now shut up and hack\n");
3157 if (cpa.had_base_commit_ref_error)
3158 show_worktree_base_ref_warning();
3161 const struct got_error *pack_err =
3162 got_repo_pack_fds_close(pack_fds);
3167 got_ref_close(head_ref);
3170 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3171 free(commit_id_str);
3174 free(worktree_path);
3179 struct got_update_progress_arg {
3191 print_update_progress_stats(struct got_update_progress_arg *upa)
3193 if (!upa->did_something)
3196 if (upa->conflicts > 0)
3197 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3198 if (upa->obstructed > 0)
3199 printf("File paths obstructed by a non-regular file: %d\n",
3201 if (upa->not_updated > 0)
3202 printf("Files not updated because of existing merge "
3203 "conflicts: %d\n", upa->not_updated);
3207 * The meaning of some status codes differs between merge-style operations and
3208 * update operations. For example, the ! status code means "file was missing"
3209 * if changes were merged into the work tree, and "missing file was restored"
3210 * if the work tree was updated. This function should be used by any operation
3211 * which merges changes into the work tree without updating the work tree.
3214 print_merge_progress_stats(struct got_update_progress_arg *upa)
3216 if (!upa->did_something)
3219 if (upa->conflicts > 0)
3220 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3221 if (upa->obstructed > 0)
3222 printf("File paths obstructed by a non-regular file: %d\n",
3224 if (upa->missing > 0)
3225 printf("Files which had incoming changes but could not be "
3226 "found in the work tree: %d\n", upa->missing);
3227 if (upa->not_deleted > 0)
3228 printf("Files not deleted due to differences in deleted "
3229 "content: %d\n", upa->not_deleted);
3230 if (upa->unversioned > 0)
3231 printf("Files not merged because an unversioned file was "
3232 "found in the work tree: %d\n", upa->unversioned);
3238 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3239 "[path ...]\n", getprogname());
3243 static const struct got_error *
3244 update_progress(void *arg, unsigned char status, const char *path)
3246 struct got_update_progress_arg *upa = arg;
3248 if (status == GOT_STATUS_EXISTS ||
3249 status == GOT_STATUS_BASE_REF_ERR)
3252 upa->did_something = 1;
3254 /* Base commit bump happens silently. */
3255 if (status == GOT_STATUS_BUMP_BASE)
3258 if (status == GOT_STATUS_CONFLICT)
3260 if (status == GOT_STATUS_OBSTRUCTED)
3262 if (status == GOT_STATUS_CANNOT_UPDATE)
3264 if (status == GOT_STATUS_MISSING)
3266 if (status == GOT_STATUS_CANNOT_DELETE)
3268 if (status == GOT_STATUS_UNVERSIONED)
3271 while (path[0] == '/')
3273 if (upa->verbosity >= 0)
3274 printf("%c %s\n", status, path);
3279 static const struct got_error *
3280 switch_head_ref(struct got_reference *head_ref,
3281 struct got_object_id *commit_id, struct got_worktree *worktree,
3282 struct got_repository *repo)
3284 const struct got_error *err = NULL;
3286 int ref_has_moved = 0;
3288 /* Trivial case: switching between two different references. */
3289 if (strcmp(got_ref_get_name(head_ref),
3290 got_worktree_get_head_ref_name(worktree)) != 0) {
3291 printf("Switching work tree from %s to %s\n",
3292 got_worktree_get_head_ref_name(worktree),
3293 got_ref_get_name(head_ref));
3294 return got_worktree_set_head_ref(worktree, head_ref);
3297 err = check_linear_ancestry(commit_id,
3298 got_worktree_get_base_commit_id(worktree), 0, repo);
3300 if (err->code != GOT_ERR_ANCESTRY)
3307 /* Switching to a rebased branch with the same reference name. */
3308 err = got_object_id_str(&base_id_str,
3309 got_worktree_get_base_commit_id(worktree));
3312 printf("Reference %s now points at a different branch\n",
3313 got_worktree_get_head_ref_name(worktree));
3314 printf("Switching work tree from %s to %s\n", base_id_str,
3315 got_worktree_get_head_ref_name(worktree));
3319 static const struct got_error *
3320 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3322 const struct got_error *err;
3325 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3329 return got_error(GOT_ERR_REBASING);
3331 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3335 return got_error(GOT_ERR_HISTEDIT_BUSY);
3340 static const struct got_error *
3341 check_merge_in_progress(struct got_worktree *worktree,
3342 struct got_repository *repo)
3344 const struct got_error *err;
3347 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3351 return got_error(GOT_ERR_MERGE_BUSY);
3356 static const struct got_error *
3357 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3358 char *argv[], struct got_worktree *worktree)
3360 const struct got_error *err = NULL;
3362 struct got_pathlist_entry *new;
3368 return got_error_from_errno("strdup");
3369 return got_pathlist_append(paths, path, NULL);
3372 for (i = 0; i < argc; i++) {
3373 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3376 err = got_pathlist_insert(&new, paths, path, NULL);
3377 if (err || new == NULL /* duplicate */) {
3387 static const struct got_error *
3388 wrap_not_worktree_error(const struct got_error *orig_err,
3389 const char *cmdname, const char *path)
3391 const struct got_error *err;
3392 struct got_repository *repo;
3393 static char msg[512];
3394 int *pack_fds = NULL;
3396 err = got_repo_pack_fds_open(&pack_fds);
3400 err = got_repo_open(&repo, path, NULL, pack_fds);
3404 snprintf(msg, sizeof(msg),
3405 "'got %s' needs a work tree in addition to a git repository\n"
3406 "Work trees can be checked out from this Git repository with "
3408 "The got(1) manual page contains more information.", cmdname);
3409 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3410 got_repo_close(repo);
3412 const struct got_error *pack_err =
3413 got_repo_pack_fds_close(pack_fds);
3420 static const struct got_error *
3421 cmd_update(int argc, char *argv[])
3423 const struct got_error *error = NULL;
3424 struct got_repository *repo = NULL;
3425 struct got_worktree *worktree = NULL;
3426 char *worktree_path = NULL;
3427 struct got_object_id *commit_id = NULL;
3428 char *commit_id_str = NULL;
3429 const char *branch_name = NULL;
3430 struct got_reference *head_ref = NULL;
3431 struct got_pathlist_head paths;
3432 struct got_pathlist_entry *pe;
3433 int ch, verbosity = 0;
3434 struct got_update_progress_arg upa;
3435 int *pack_fds = NULL;
3439 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3442 branch_name = optarg;
3445 commit_id_str = strdup(optarg);
3446 if (commit_id_str == NULL)
3447 return got_error_from_errno("strdup");
3462 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3463 "unveil", NULL) == -1)
3466 worktree_path = getcwd(NULL, 0);
3467 if (worktree_path == NULL) {
3468 error = got_error_from_errno("getcwd");
3472 error = got_repo_pack_fds_open(&pack_fds);
3476 error = got_worktree_open(&worktree, worktree_path);
3478 if (error->code == GOT_ERR_NOT_WORKTREE)
3479 error = wrap_not_worktree_error(error, "update",
3484 error = check_rebase_or_histedit_in_progress(worktree);
3488 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3493 error = apply_unveil(got_repo_get_path(repo), 0,
3494 got_worktree_get_root_path(worktree));
3498 error = check_merge_in_progress(worktree, repo);
3502 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3506 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3507 got_worktree_get_head_ref_name(worktree), 0);
3510 if (commit_id_str == NULL) {
3511 error = got_ref_resolve(&commit_id, repo, head_ref);
3514 error = got_object_id_str(&commit_id_str, commit_id);
3518 struct got_reflist_head refs;
3520 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3524 error = got_repo_match_object_id(&commit_id, NULL,
3525 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3526 got_ref_list_free(&refs);
3527 free(commit_id_str);
3528 commit_id_str = NULL;
3531 error = got_object_id_str(&commit_id_str, commit_id);
3537 struct got_object_id *head_commit_id;
3538 TAILQ_FOREACH(pe, &paths, entry) {
3539 if (pe->path_len == 0)
3541 error = got_error_msg(GOT_ERR_BAD_PATH,
3542 "switching between branches requires that "
3543 "the entire work tree gets updated");
3546 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3549 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3551 free(head_commit_id);
3554 error = check_same_branch(commit_id, head_ref, NULL, repo);
3557 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3561 error = check_linear_ancestry(commit_id,
3562 got_worktree_get_base_commit_id(worktree), 0, repo);
3563 if (error != NULL) {
3564 if (error->code == GOT_ERR_ANCESTRY)
3565 error = got_error(GOT_ERR_BRANCH_MOVED);
3568 error = check_same_branch(commit_id, head_ref, NULL, repo);
3573 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3575 error = got_worktree_set_base_commit_id(worktree, repo,
3581 memset(&upa, 0, sizeof(upa));
3582 upa.verbosity = verbosity;
3583 error = got_worktree_checkout_files(worktree, &paths, repo,
3584 update_progress, &upa, check_cancelled, NULL);
3588 if (upa.did_something) {
3589 printf("Updated to %s: %s\n",
3590 got_worktree_get_head_ref_name(worktree), commit_id_str);
3592 printf("Already up-to-date\n");
3594 print_update_progress_stats(&upa);
3597 const struct got_error *pack_err =
3598 got_repo_pack_fds_close(pack_fds);
3602 free(worktree_path);
3603 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3605 free(commit_id_str);
3609 static const struct got_error *
3610 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3611 const char *path, int diff_context, int ignore_whitespace,
3612 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3613 struct got_repository *repo, FILE *outfile)
3615 const struct got_error *err = NULL;
3616 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3617 FILE *f1 = NULL, *f2 = NULL;
3618 int fd1 = -1, fd2 = -1;
3620 fd1 = got_opentempfd();
3622 return got_error_from_errno("got_opentempfd");
3623 fd2 = got_opentempfd();
3625 err = got_error_from_errno("got_opentempfd");
3630 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3636 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3640 f1 = got_opentemp();
3642 err = got_error_from_errno("got_opentemp");
3645 f2 = got_opentemp();
3647 err = got_error_from_errno("got_opentemp");
3651 while (path[0] == '/')
3653 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3654 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3655 force_text_diff, dsa, outfile);
3657 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3658 err = got_error_from_errno("close");
3660 got_object_blob_close(blob1);
3661 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3662 err = got_error_from_errno("close");
3663 got_object_blob_close(blob2);
3664 if (f1 && fclose(f1) == EOF && err == NULL)
3665 err = got_error_from_errno("fclose");
3666 if (f2 && fclose(f2) == EOF && err == NULL)
3667 err = got_error_from_errno("fclose");
3671 static const struct got_error *
3672 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3673 const char *path, int diff_context, int ignore_whitespace,
3674 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3675 struct got_repository *repo, FILE *outfile)
3677 const struct got_error *err = NULL;
3678 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3679 struct got_diff_blob_output_unidiff_arg arg;
3680 FILE *f1 = NULL, *f2 = NULL;
3681 int fd1 = -1, fd2 = -1;
3684 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3687 fd1 = got_opentempfd();
3689 err = got_error_from_errno("got_opentempfd");
3694 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3698 f1 = got_opentemp();
3700 err = got_error_from_errno("got_opentemp");
3704 f2 = got_opentemp();
3706 err = got_error_from_errno("got_opentemp");
3709 fd2 = got_opentempfd();
3711 err = got_error_from_errno("got_opentempfd");
3714 arg.diff_context = diff_context;
3715 arg.ignore_whitespace = ignore_whitespace;
3716 arg.force_text_diff = force_text_diff;
3718 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3719 arg.outfile = outfile;
3722 while (path[0] == '/')
3724 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3725 got_diff_blob_output_unidiff, &arg, 1);
3728 got_object_tree_close(tree1);
3730 got_object_tree_close(tree2);
3731 if (f1 && fclose(f1) == EOF && err == NULL)
3732 err = got_error_from_errno("fclose");
3733 if (f2 && fclose(f2) == EOF && err == NULL)
3734 err = got_error_from_errno("fclose");
3735 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3736 err = got_error_from_errno("close");
3737 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3738 err = got_error_from_errno("close");
3742 static const struct got_error *
3743 get_changed_paths(struct got_pathlist_head *paths,
3744 struct got_commit_object *commit, struct got_repository *repo,
3745 struct got_diffstat_cb_arg *dsa)
3747 const struct got_error *err = NULL;
3748 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3749 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3750 struct got_object_qid *qid;
3751 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3752 FILE *f1 = NULL, *f2 = NULL;
3753 int fd1 = -1, fd2 = -1;
3756 cb = got_diff_tree_compute_diffstat;
3758 f1 = got_opentemp();
3760 err = got_error_from_errno("got_opentemp");
3763 f2 = got_opentemp();
3765 err = got_error_from_errno("got_opentemp");
3768 fd1 = got_opentempfd();
3770 err = got_error_from_errno("got_opentempfd");
3773 fd2 = got_opentempfd();
3775 err = got_error_from_errno("got_opentempfd");
3780 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3782 struct got_commit_object *pcommit;
3783 err = got_object_open_as_commit(&pcommit, repo,
3788 tree_id1 = got_object_id_dup(
3789 got_object_commit_get_tree_id(pcommit));
3790 if (tree_id1 == NULL) {
3791 got_object_commit_close(pcommit);
3792 return got_error_from_errno("got_object_id_dup");
3794 got_object_commit_close(pcommit);
3799 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3804 tree_id2 = got_object_commit_get_tree_id(commit);
3805 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3809 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3810 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3813 got_object_tree_close(tree1);
3815 got_object_tree_close(tree2);
3816 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3817 err = got_error_from_errno("close");
3818 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3819 err = got_error_from_errno("close");
3820 if (f1 && fclose(f1) == EOF && err == NULL)
3821 err = got_error_from_errno("fclose");
3822 if (f2 && fclose(f2) == EOF && err == NULL)
3823 err = got_error_from_errno("fclose");
3828 static const struct got_error *
3829 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3830 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3831 struct got_repository *repo, FILE *outfile)
3833 const struct got_error *err = NULL;
3834 struct got_commit_object *pcommit = NULL;
3835 char *id_str1 = NULL, *id_str2 = NULL;
3836 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3837 struct got_object_qid *qid;
3839 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3841 err = got_object_open_as_commit(&pcommit, repo,
3845 err = got_object_id_str(&id_str1, &qid->id);
3850 err = got_object_id_str(&id_str2, id);
3854 if (path && path[0] != '\0') {
3856 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3860 err = got_object_id_by_path(&obj_id1, repo,
3863 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3869 err = got_object_get_type(&obj_type, repo, obj_id2);
3875 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3876 fprintf(outfile, "commit - %s\n",
3877 id_str1 ? id_str1 : "/dev/null");
3878 fprintf(outfile, "commit + %s\n", id_str2);
3880 case GOT_OBJ_TYPE_BLOB:
3881 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3882 0, 0, dsa, repo, outfile);
3884 case GOT_OBJ_TYPE_TREE:
3885 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3886 0, 0, dsa, repo, outfile);
3889 err = got_error(GOT_ERR_OBJ_TYPE);
3895 obj_id2 = got_object_commit_get_tree_id(commit);
3897 obj_id1 = got_object_commit_get_tree_id(pcommit);
3899 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3900 fprintf(outfile, "commit - %s\n",
3901 id_str1 ? id_str1 : "/dev/null");
3902 fprintf(outfile, "commit + %s\n", id_str2);
3903 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3904 dsa, repo, outfile);
3910 got_object_commit_close(pcommit);
3915 get_datestr(time_t *time, char *datebuf)
3917 struct tm mytm, *tm;
3920 tm = gmtime_r(time, &mytm);
3923 s = asctime_r(tm, datebuf);
3926 p = strchr(s, '\n');
3932 static const struct got_error *
3933 match_commit(int *have_match, struct got_object_id *id,
3934 struct got_commit_object *commit, regex_t *regex)
3936 const struct got_error *err = NULL;
3937 regmatch_t regmatch;
3938 char *id_str = NULL, *logmsg = NULL;
3942 err = got_object_id_str(&id_str, id);
3946 err = got_object_commit_get_logmsg(&logmsg, commit);
3950 if (regexec(regex, got_object_commit_get_author(commit), 1,
3951 ®match, 0) == 0 ||
3952 regexec(regex, got_object_commit_get_committer(commit), 1,
3953 ®match, 0) == 0 ||
3954 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3955 regexec(regex, logmsg, 1, ®match, 0) == 0)
3964 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3967 regmatch_t regmatch;
3968 struct got_pathlist_entry *pe;
3972 TAILQ_FOREACH(pe, changed_paths, entry) {
3973 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3980 static const struct got_error *
3981 match_patch(int *have_match, struct got_commit_object *commit,
3982 struct got_object_id *id, const char *path, int diff_context,
3983 struct got_repository *repo, regex_t *regex, FILE *f)
3985 const struct got_error *err = NULL;
3987 size_t linesize = 0;
3988 regmatch_t regmatch;
3992 err = got_opentemp_truncate(f);
3996 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4000 if (fseeko(f, 0L, SEEK_SET) == -1) {
4001 err = got_error_from_errno("fseeko");
4005 while (getline(&line, &linesize, f) != -1) {
4006 if (regexec(regex, line, 1, ®match, 0) == 0) {
4016 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4018 static const struct got_error*
4019 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4020 struct got_object_id *id, struct got_repository *repo,
4023 static const struct got_error *err = NULL;
4024 struct got_reflist_entry *re;
4030 TAILQ_FOREACH(re, refs, entry) {
4031 struct got_tag_object *tag = NULL;
4032 struct got_object_id *ref_id;
4035 name = got_ref_get_name(re->ref);
4036 if (strcmp(name, GOT_REF_HEAD) == 0)
4038 if (strncmp(name, "refs/", 5) == 0)
4040 if (strncmp(name, "got/", 4) == 0)
4042 if (strncmp(name, "heads/", 6) == 0)
4044 if (strncmp(name, "remotes/", 8) == 0) {
4048 s = strstr(name, "/" GOT_REF_HEAD);
4049 if (s != NULL && s[strlen(s)] == '\0')
4052 err = got_ref_resolve(&ref_id, repo, re->ref);
4055 if (strncmp(name, "tags/", 5) == 0) {
4056 err = got_object_open_as_tag(&tag, repo, ref_id);
4058 if (err->code != GOT_ERR_OBJ_TYPE) {
4062 /* Ref points at something other than a tag. */
4067 cmp = got_object_id_cmp(tag ?
4068 got_object_tag_get_object_id(tag) : ref_id, id);
4071 got_object_tag_close(tag);
4075 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4076 s ? ", " : "", name) == -1) {
4077 err = got_error_from_errno("asprintf");
4088 static const struct got_error *
4089 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4090 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4092 const struct got_error *err = NULL;
4093 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4094 char *comma, *s, *nl;
4095 struct got_reflist_head *refs;
4096 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4098 time_t committer_time;
4100 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4102 err = build_refs_str(&ref_str, refs, id, repo, 1);
4106 /* Display the first matching ref only. */
4107 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4111 if (ref_str == NULL) {
4112 err = got_object_id_str(&id_str, id);
4117 committer_time = got_object_commit_get_committer_time(commit);
4118 if (gmtime_r(&committer_time, &tm) == NULL) {
4119 err = got_error_from_errno("gmtime_r");
4122 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4123 err = got_error(GOT_ERR_NO_SPACE);
4127 err = got_object_commit_get_logmsg(&logmsg0, commit);
4132 while (isspace((unsigned char)s[0]))
4135 nl = strchr(s, '\n');
4141 printf("%s%-7s %s\n", datebuf, ref_str, s);
4143 printf("%s%.7s %s\n", datebuf, id_str, s);
4145 if (fflush(stdout) != 0 && err == NULL)
4146 err = got_error_from_errno("fflush");
4154 static const struct got_error *
4155 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4157 struct got_pathlist_entry *pe;
4160 printf("%s\n", header);
4162 TAILQ_FOREACH(pe, dsa->paths, entry) {
4163 struct got_diff_changed_path *cp = pe->data;
4164 int pad = dsa->max_path_len - pe->path_len + 1;
4166 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4167 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4169 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4170 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4171 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4173 if (fflush(stdout) != 0)
4174 return got_error_from_errno("fflush");
4179 static const struct got_error *
4185 if (fseeko(f, 0L, SEEK_SET) == -1)
4186 return got_error_from_errno("fseek");
4189 r = fread(buf, 1, sizeof(buf), f);
4192 return got_error_from_errno("fread");
4196 if (fwrite(buf, 1, r, stdout) != r)
4197 return got_ferror(stdout, GOT_ERR_IO);
4203 static const struct got_error *
4204 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4205 struct got_repository *repo, const char *path,
4206 struct got_pathlist_head *changed_paths,
4207 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4208 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4211 const struct got_error *err = NULL;
4213 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4215 time_t committer_time;
4216 const char *author, *committer;
4217 char *refs_str = NULL;
4219 err = got_object_id_str(&id_str, id);
4223 if (custom_refs_str == NULL) {
4224 struct got_reflist_head *refs;
4225 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4227 err = build_refs_str(&refs_str, refs, id, repo, 0);
4233 printf(GOT_COMMIT_SEP_STR);
4234 if (custom_refs_str)
4235 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4238 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4239 refs_str ? " (" : "", refs_str ? refs_str : "",
4240 refs_str ? ")" : "");
4245 printf("from: %s\n", got_object_commit_get_author(commit));
4246 author = got_object_commit_get_author(commit);
4247 committer = got_object_commit_get_committer(commit);
4248 if (strcmp(author, committer) != 0)
4249 printf("via: %s\n", committer);
4250 committer_time = got_object_commit_get_committer_time(commit);
4251 datestr = get_datestr(&committer_time, datebuf);
4253 printf("date: %s UTC\n", datestr);
4254 if (got_object_commit_get_nparents(commit) > 1) {
4255 const struct got_object_id_queue *parent_ids;
4256 struct got_object_qid *qid;
4258 parent_ids = got_object_commit_get_parent_ids(commit);
4259 STAILQ_FOREACH(qid, parent_ids, entry) {
4260 err = got_object_id_str(&id_str, &qid->id);
4263 printf("parent %d: %s\n", n++, id_str);
4269 err = got_object_commit_get_logmsg(&logmsg0, commit);
4275 line = strsep(&logmsg, "\n");
4277 printf(" %s\n", line);
4281 if (changed_paths && diffstat == NULL) {
4282 struct got_pathlist_entry *pe;
4284 TAILQ_FOREACH(pe, changed_paths, entry) {
4285 struct got_diff_changed_path *cp = pe->data;
4287 printf(" %c %s\n", cp->status, pe->path);
4295 err = got_error_from_errno("got_opentemp");
4300 err = print_patch(commit, id, path, diff_context, diffstat,
4301 repo, diffstat == NULL ? stdout : f);
4306 err = print_diffstat(diffstat, NULL);
4318 if (fflush(stdout) != 0 && err == NULL)
4319 err = got_error_from_errno("fflush");
4321 if (f && fclose(f) == EOF && err == NULL)
4322 err = got_error_from_errno("fclose");
4328 static const struct got_error *
4329 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4330 struct got_repository *repo, const char *path, int show_changed_paths,
4331 int show_diffstat, int show_patch, const char *search_pattern,
4332 int diff_context, int limit, int log_branches, int reverse_display_order,
4333 struct got_reflist_object_id_map *refs_idmap, int one_line,
4336 const struct got_error *err;
4337 struct got_commit_graph *graph;
4340 struct got_object_id_queue reversed_commits;
4341 struct got_object_qid *qid;
4342 struct got_commit_object *commit;
4343 struct got_pathlist_head changed_paths;
4345 STAILQ_INIT(&reversed_commits);
4346 TAILQ_INIT(&changed_paths);
4348 if (search_pattern && regcomp(®ex, search_pattern,
4349 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4350 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4352 err = got_commit_graph_open(&graph, path, !log_branches);
4355 err = got_commit_graph_iter_start(graph, root_id, repo,
4356 check_cancelled, NULL);
4360 struct got_object_id id;
4361 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4362 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4364 if (sigint_received || sigpipe_received)
4367 err = got_commit_graph_iter_next(&id, graph, repo,
4368 check_cancelled, NULL);
4370 if (err->code == GOT_ERR_ITER_COMPLETED)
4375 err = got_object_open_as_commit(&commit, repo, &id);
4379 if ((show_changed_paths || (show_diffstat && !show_patch))
4380 && !reverse_display_order) {
4381 err = get_changed_paths(&changed_paths, commit, repo,
4382 show_diffstat ? &dsa : NULL);
4387 if (search_pattern) {
4388 err = match_commit(&have_match, &id, commit, ®ex);
4390 got_object_commit_close(commit);
4393 if (have_match == 0 && show_changed_paths)
4394 match_changed_paths(&have_match,
4395 &changed_paths, ®ex);
4396 if (have_match == 0 && show_patch) {
4397 err = match_patch(&have_match, commit, &id,
4398 path, diff_context, repo, ®ex, tmpfile);
4402 if (have_match == 0) {
4403 got_object_commit_close(commit);
4404 got_pathlist_free(&changed_paths,
4405 GOT_PATHLIST_FREE_ALL);
4410 if (reverse_display_order) {
4411 err = got_object_qid_alloc(&qid, &id);
4414 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4415 got_object_commit_close(commit);
4418 err = print_commit_oneline(commit, &id,
4421 err = print_commit(commit, &id, repo, path,
4422 (show_changed_paths || show_diffstat) ?
4423 &changed_paths : NULL,
4424 show_diffstat ? &dsa : NULL, show_patch,
4425 diff_context, refs_idmap, NULL, NULL);
4426 got_object_commit_close(commit);
4430 if ((limit && --limit == 0) ||
4431 (end_id && got_object_id_cmp(&id, end_id) == 0))
4434 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4436 if (reverse_display_order) {
4437 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4438 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4439 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4441 err = got_object_open_as_commit(&commit, repo,
4445 if (show_changed_paths ||
4446 (show_diffstat && !show_patch)) {
4447 err = get_changed_paths(&changed_paths, commit,
4448 repo, show_diffstat ? &dsa : NULL);
4453 err = print_commit_oneline(commit, &qid->id,
4456 err = print_commit(commit, &qid->id, repo, path,
4457 (show_changed_paths || show_diffstat) ?
4458 &changed_paths : NULL,
4459 show_diffstat ? &dsa : NULL, show_patch,
4460 diff_context, refs_idmap, NULL, NULL);
4461 got_object_commit_close(commit);
4464 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4468 while (!STAILQ_EMPTY(&reversed_commits)) {
4469 qid = STAILQ_FIRST(&reversed_commits);
4470 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4471 got_object_qid_free(qid);
4473 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4476 got_commit_graph_close(graph);
4483 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4484 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4485 "[path]\n", getprogname());
4490 get_default_log_limit(void)
4492 const char *got_default_log_limit;
4496 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4497 if (got_default_log_limit == NULL)
4499 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4505 static const struct got_error *
4506 cmd_log(int argc, char *argv[])
4508 const struct got_error *error;
4509 struct got_repository *repo = NULL;
4510 struct got_worktree *worktree = NULL;
4511 struct got_object_id *start_id = NULL, *end_id = NULL;
4512 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4513 const char *start_commit = NULL, *end_commit = NULL;
4514 const char *search_pattern = NULL;
4515 int diff_context = -1, ch;
4516 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4517 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4519 struct got_reflist_head refs;
4520 struct got_reflist_object_id_map *refs_idmap = NULL;
4521 FILE *tmpfile = NULL;
4522 int *pack_fds = NULL;
4527 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4533 limit = get_default_log_limit();
4535 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4541 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4544 errx(1, "number of context lines is %s: %s",
4548 start_commit = optarg;
4554 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4556 errx(1, "number of commits is %s: %s",
4560 show_changed_paths = 1;
4566 reverse_display_order = 1;
4569 repo_path = realpath(optarg, NULL);
4570 if (repo_path == NULL)
4571 return got_error_from_errno2("realpath",
4573 got_path_strip_trailing_slashes(repo_path);
4576 search_pattern = optarg;
4582 end_commit = optarg;
4593 if (diff_context == -1)
4595 else if (!show_patch)
4596 errx(1, "-C requires -p");
4598 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4599 errx(1, "cannot use -s with -d, -p or -P");
4601 cwd = getcwd(NULL, 0);
4603 error = got_error_from_errno("getcwd");
4607 error = got_repo_pack_fds_open(&pack_fds);
4611 if (repo_path == NULL) {
4612 error = got_worktree_open(&worktree, cwd);
4613 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4620 error = got_worktree_resolve_path(&path, worktree,
4625 path = strdup(argv[0]);
4627 error = got_error_from_errno("strdup");
4631 } else if (argc != 0)
4634 if (repo_path == NULL) {
4635 repo_path = worktree ?
4636 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4638 if (repo_path == NULL) {
4639 error = got_error_from_errno("strdup");
4643 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4647 error = apply_unveil(got_repo_get_path(repo), 1,
4648 worktree ? got_worktree_get_root_path(worktree) : NULL);
4652 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4656 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4660 if (start_commit == NULL) {
4661 struct got_reference *head_ref;
4662 struct got_commit_object *commit = NULL;
4663 error = got_ref_open(&head_ref, repo,
4664 worktree ? got_worktree_get_head_ref_name(worktree)
4668 error = got_ref_resolve(&start_id, repo, head_ref);
4669 got_ref_close(head_ref);
4672 error = got_object_open_as_commit(&commit, repo,
4676 got_object_commit_close(commit);
4678 error = got_repo_match_object_id(&start_id, NULL,
4679 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4683 if (end_commit != NULL) {
4684 error = got_repo_match_object_id(&end_id, NULL,
4685 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4692 * If a path was specified on the command line it was resolved
4693 * to a path in the work tree above. Prepend the work tree's
4694 * path prefix to obtain the corresponding in-repository path.
4698 prefix = got_worktree_get_path_prefix(worktree);
4699 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4700 (path[0] != '\0') ? "/" : "", path) == -1) {
4701 error = got_error_from_errno("asprintf");
4706 error = got_repo_map_path(&in_repo_path, repo,
4712 path = in_repo_path;
4716 /* Release work tree lock. */
4717 got_worktree_close(worktree);
4721 if (search_pattern && show_patch) {
4722 tmpfile = got_opentemp();
4723 if (tmpfile == NULL) {
4724 error = got_error_from_errno("got_opentemp");
4729 error = print_commits(start_id, end_id, repo, path ? path : "",
4730 show_changed_paths, show_diffstat, show_patch, search_pattern,
4731 diff_context, limit, log_branches, reverse_display_order,
4732 refs_idmap, one_line, tmpfile);
4738 got_worktree_close(worktree);
4740 const struct got_error *close_err = got_repo_close(repo);
4745 const struct got_error *pack_err =
4746 got_repo_pack_fds_close(pack_fds);
4751 got_reflist_object_id_map_free(refs_idmap);
4752 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4753 error = got_error_from_errno("fclose");
4754 got_ref_list_free(&refs);
4761 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4762 "[-r repository-path] [object1 object2 | path ...]\n",
4767 struct print_diff_arg {
4768 struct got_repository *repo;
4769 struct got_worktree *worktree;
4770 struct got_diffstat_cb_arg *diffstat;
4775 enum got_diff_algorithm diff_algo;
4776 int ignore_whitespace;
4777 int force_text_diff;
4784 * Create a file which contains the target path of a symlink so we can feed
4785 * it as content to the diff engine.
4787 static const struct got_error *
4788 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4789 const char *abspath)
4791 const struct got_error *err = NULL;
4792 char target_path[PATH_MAX];
4793 ssize_t target_len, outlen;
4798 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4799 if (target_len == -1)
4800 return got_error_from_errno2("readlinkat", abspath);
4802 target_len = readlink(abspath, target_path, PATH_MAX);
4803 if (target_len == -1)
4804 return got_error_from_errno2("readlink", abspath);
4807 *fd = got_opentempfd();
4809 return got_error_from_errno("got_opentempfd");
4811 outlen = write(*fd, target_path, target_len);
4813 err = got_error_from_errno("got_opentempfd");
4817 if (lseek(*fd, 0, SEEK_SET) == -1) {
4818 err = got_error_from_errno2("lseek", abspath);
4829 static const struct got_error *
4830 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4831 const char *path, struct got_object_id *blob_id,
4832 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4833 int dirfd, const char *de_name)
4835 struct print_diff_arg *a = arg;
4836 const struct got_error *err = NULL;
4837 struct got_blob_object *blob1 = NULL;
4838 int fd = -1, fd1 = -1, fd2 = -1;
4840 char *abspath = NULL, *label1 = NULL;
4845 memset(&sb, 0, sizeof(sb));
4847 if (a->diff_staged) {
4848 if (staged_status != GOT_STATUS_MODIFY &&
4849 staged_status != GOT_STATUS_ADD &&
4850 staged_status != GOT_STATUS_DELETE)
4853 if (staged_status == GOT_STATUS_DELETE)
4855 if (status == GOT_STATUS_NONEXISTENT)
4856 return got_error_set_errno(ENOENT, path);
4857 if (status != GOT_STATUS_MODIFY &&
4858 status != GOT_STATUS_ADD &&
4859 status != GOT_STATUS_DELETE &&
4860 status != GOT_STATUS_CONFLICT)
4864 err = got_opentemp_truncate(a->f1);
4866 return got_error_from_errno("got_opentemp_truncate");
4867 err = got_opentemp_truncate(a->f2);
4869 return got_error_from_errno("got_opentemp_truncate");
4871 if (!a->header_shown) {
4872 if (fprintf(a->outfile, "diff %s%s\n",
4873 a->diff_staged ? "-s " : "",
4874 got_worktree_get_root_path(a->worktree)) < 0) {
4875 err = got_error_from_errno("fprintf");
4878 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4879 err = got_error_from_errno("fprintf");
4882 if (fprintf(a->outfile, "path + %s%s\n",
4883 got_worktree_get_root_path(a->worktree),
4884 a->diff_staged ? " (staged changes)" : "") < 0) {
4885 err = got_error_from_errno("fprintf");
4888 a->header_shown = 1;
4891 if (a->diff_staged) {
4892 const char *label1 = NULL, *label2 = NULL;
4893 switch (staged_status) {
4894 case GOT_STATUS_MODIFY:
4898 case GOT_STATUS_ADD:
4901 case GOT_STATUS_DELETE:
4905 return got_error(GOT_ERR_FILE_STATUS);
4907 fd1 = got_opentempfd();
4909 err = got_error_from_errno("got_opentempfd");
4912 fd2 = got_opentempfd();
4914 err = got_error_from_errno("got_opentempfd");
4917 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4918 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4919 a->diff_algo, a->diff_context, a->ignore_whitespace,
4920 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4924 fd1 = got_opentempfd();
4926 err = got_error_from_errno("got_opentempfd");
4930 if (staged_status == GOT_STATUS_ADD ||
4931 staged_status == GOT_STATUS_MODIFY) {
4933 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4937 err = got_object_id_str(&id_str, staged_blob_id);
4940 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4941 err = got_error_from_errno("asprintf");
4946 } else if (status != GOT_STATUS_ADD) {
4947 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4953 if (status != GOT_STATUS_DELETE) {
4954 if (asprintf(&abspath, "%s/%s",
4955 got_worktree_get_root_path(a->worktree), path) == -1) {
4956 err = got_error_from_errno("asprintf");
4961 fd = openat(dirfd, de_name,
4962 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4964 if (!got_err_open_nofollow_on_symlink()) {
4965 err = got_error_from_errno2("openat",
4969 err = get_symlink_target_file(&fd, dirfd,
4975 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4977 if (!got_err_open_nofollow_on_symlink()) {
4978 err = got_error_from_errno2("open",
4982 err = get_symlink_target_file(&fd, dirfd,
4988 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4989 err = got_error_from_errno2("fstatat", abspath);
4992 f2 = fdopen(fd, "r");
4994 err = got_error_from_errno2("fdopen", abspath);
5002 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5008 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5009 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5010 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5012 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5013 err = got_error_from_errno("close");
5014 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5015 err = got_error_from_errno("close");
5017 got_object_blob_close(blob1);
5018 if (fd != -1 && close(fd) == -1 && err == NULL)
5019 err = got_error_from_errno("close");
5020 if (f2 && fclose(f2) == EOF && err == NULL)
5021 err = got_error_from_errno("fclose");
5026 static const struct got_error *
5027 cmd_diff(int argc, char *argv[])
5029 const struct got_error *error;
5030 struct got_repository *repo = NULL;
5031 struct got_worktree *worktree = NULL;
5032 char *cwd = NULL, *repo_path = NULL;
5033 const char *commit_args[2] = { NULL, NULL };
5034 int ncommit_args = 0;
5035 struct got_object_id *ids[2] = { NULL, NULL };
5036 char *labels[2] = { NULL, NULL };
5037 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5038 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5039 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5041 struct got_reflist_head refs;
5042 struct got_pathlist_head diffstat_paths, paths;
5043 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5044 int fd1 = -1, fd2 = -1;
5045 int *pack_fds = NULL;
5046 struct got_diffstat_cb_arg dsa;
5048 memset(&dsa, 0, sizeof(dsa));
5052 TAILQ_INIT(&diffstat_paths);
5055 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5060 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5063 force_text_diff = 1;
5066 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5069 errx(1, "number of context lines is %s: %s",
5073 if (ncommit_args >= 2)
5074 errx(1, "too many -c options used");
5075 commit_args[ncommit_args++] = optarg;
5084 repo_path = realpath(optarg, NULL);
5085 if (repo_path == NULL)
5086 return got_error_from_errno2("realpath",
5088 got_path_strip_trailing_slashes(repo_path);
5095 ignore_whitespace = 1;
5106 cwd = getcwd(NULL, 0);
5108 error = got_error_from_errno("getcwd");
5112 error = got_repo_pack_fds_open(&pack_fds);
5116 if (repo_path == NULL) {
5117 error = got_worktree_open(&worktree, cwd);
5118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5124 strdup(got_worktree_get_repo_path(worktree));
5125 if (repo_path == NULL) {
5126 error = got_error_from_errno("strdup");
5130 repo_path = strdup(cwd);
5131 if (repo_path == NULL) {
5132 error = got_error_from_errno("strdup");
5138 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5143 if (show_diffstat) {
5144 dsa.paths = &diffstat_paths;
5145 dsa.force_text = force_text_diff;
5146 dsa.ignore_ws = ignore_whitespace;
5147 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5150 if (rflag || worktree == NULL || ncommit_args > 0) {
5152 error = got_error_msg(GOT_ERR_NOT_IMPL,
5153 "-P option can only be used when diffing "
5158 error = got_error_msg(GOT_ERR_NOT_IMPL,
5159 "-s option can only be used when diffing "
5165 error = apply_unveil(got_repo_get_path(repo), 1,
5166 worktree ? got_worktree_get_root_path(worktree) : NULL);
5170 if ((!force_path && argc == 2) || ncommit_args > 0) {
5171 int obj_type = (ncommit_args > 0 ?
5172 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5173 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5177 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5179 if (ncommit_args > 0)
5180 arg = commit_args[i];
5183 error = got_repo_match_object_id(&ids[i], &labels[i],
5184 arg, obj_type, &refs, repo);
5186 if (error->code != GOT_ERR_NOT_REF &&
5187 error->code != GOT_ERR_NO_OBJ)
5189 if (ncommit_args > 0)
5197 f1 = got_opentemp();
5199 error = got_error_from_errno("got_opentemp");
5203 f2 = got_opentemp();
5205 error = got_error_from_errno("got_opentemp");
5209 outfile = got_opentemp();
5210 if (outfile == NULL) {
5211 error = got_error_from_errno("got_opentemp");
5215 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5216 struct print_diff_arg arg;
5219 if (worktree == NULL) {
5220 if (argc == 2 && ids[0] == NULL) {
5221 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5223 } else if (argc == 2 && ids[1] == NULL) {
5224 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5226 } else if (argc > 0) {
5227 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5228 "%s", "specified paths cannot be resolved");
5231 error = got_error(GOT_ERR_NOT_WORKTREE);
5236 error = get_worktree_paths_from_argv(&paths, argc, argv,
5241 error = got_object_id_str(&id_str,
5242 got_worktree_get_base_commit_id(worktree));
5246 arg.worktree = worktree;
5247 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5248 arg.diff_context = diff_context;
5249 arg.id_str = id_str;
5250 arg.header_shown = 0;
5251 arg.diff_staged = diff_staged;
5252 arg.ignore_whitespace = ignore_whitespace;
5253 arg.force_text_diff = force_text_diff;
5254 arg.diffstat = show_diffstat ? &dsa : NULL;
5257 arg.outfile = outfile;
5259 error = got_worktree_status(worktree, &paths, repo, 0,
5260 print_diff, &arg, check_cancelled, NULL);
5265 if (show_diffstat && dsa.nfiles > 0) {
5268 if (asprintf(&header, "diffstat %s%s",
5269 diff_staged ? "-s " : "",
5270 got_worktree_get_root_path(worktree)) == -1) {
5271 error = got_error_from_errno("asprintf");
5275 error = print_diffstat(&dsa, header);
5281 error = printfile(outfile);
5285 if (ncommit_args == 1) {
5286 struct got_commit_object *commit;
5287 error = got_object_open_as_commit(&commit, repo, ids[0]);
5291 labels[1] = labels[0];
5293 if (got_object_commit_get_nparents(commit) > 0) {
5294 const struct got_object_id_queue *pids;
5295 struct got_object_qid *pid;
5296 pids = got_object_commit_get_parent_ids(commit);
5297 pid = STAILQ_FIRST(pids);
5298 ids[0] = got_object_id_dup(&pid->id);
5299 if (ids[0] == NULL) {
5300 error = got_error_from_errno(
5301 "got_object_id_dup");
5302 got_object_commit_close(commit);
5305 error = got_object_id_str(&labels[0], ids[0]);
5307 got_object_commit_close(commit);
5312 labels[0] = strdup("/dev/null");
5313 if (labels[0] == NULL) {
5314 error = got_error_from_errno("strdup");
5315 got_object_commit_close(commit);
5320 got_object_commit_close(commit);
5323 if (ncommit_args == 0 && argc > 2) {
5324 error = got_error_msg(GOT_ERR_BAD_PATH,
5325 "path arguments cannot be used when diffing two objects");
5330 error = got_object_get_type(&type1, repo, ids[0]);
5335 error = got_object_get_type(&type2, repo, ids[1]);
5338 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5339 error = got_error(GOT_ERR_OBJ_TYPE);
5342 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5343 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5344 "path arguments cannot be used when diffing blobs");
5348 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5350 struct got_pathlist_entry *new;
5354 error = got_worktree_resolve_path(&p, worktree,
5358 prefix = got_worktree_get_path_prefix(worktree);
5359 while (prefix[0] == '/')
5361 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5362 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5364 error = got_error_from_errno("asprintf");
5370 char *mapped_path, *s;
5371 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5377 in_repo_path = strdup(s);
5378 if (in_repo_path == NULL) {
5379 error = got_error_from_errno("asprintf");
5386 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5387 if (error || new == NULL /* duplicate */)
5394 /* Release work tree lock. */
5395 got_worktree_close(worktree);
5399 fd1 = got_opentempfd();
5401 error = got_error_from_errno("got_opentempfd");
5405 fd2 = got_opentempfd();
5407 error = got_error_from_errno("got_opentempfd");
5411 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5412 case GOT_OBJ_TYPE_BLOB:
5413 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5414 fd1, fd2, ids[0], ids[1], NULL, NULL,
5415 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5416 ignore_whitespace, force_text_diff,
5417 show_diffstat ? &dsa : NULL, repo, outfile);
5419 case GOT_OBJ_TYPE_TREE:
5420 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5421 ids[0], ids[1], &paths, "", "",
5422 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5423 ignore_whitespace, force_text_diff,
5424 show_diffstat ? &dsa : NULL, repo, outfile);
5426 case GOT_OBJ_TYPE_COMMIT:
5427 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5428 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5429 fd1, fd2, ids[0], ids[1], &paths,
5430 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5431 ignore_whitespace, force_text_diff,
5432 show_diffstat ? &dsa : NULL, repo, outfile);
5435 error = got_error(GOT_ERR_OBJ_TYPE);
5440 if (show_diffstat && dsa.nfiles > 0) {
5441 char *header = NULL;
5443 if (asprintf(&header, "diffstat %s %s",
5444 labels[0], labels[1]) == -1) {
5445 error = got_error_from_errno("asprintf");
5449 error = print_diffstat(&dsa, header);
5455 error = printfile(outfile);
5463 got_worktree_close(worktree);
5465 const struct got_error *close_err = got_repo_close(repo);
5470 const struct got_error *pack_err =
5471 got_repo_pack_fds_close(pack_fds);
5475 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5476 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5477 got_ref_list_free(&refs);
5478 if (outfile && fclose(outfile) == EOF && error == NULL)
5479 error = got_error_from_errno("fclose");
5480 if (f1 && fclose(f1) == EOF && error == NULL)
5481 error = got_error_from_errno("fclose");
5482 if (f2 && fclose(f2) == EOF && error == NULL)
5483 error = got_error_from_errno("fclose");
5484 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5485 error = got_error_from_errno("close");
5486 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5487 error = got_error_from_errno("close");
5495 "usage: %s blame [-c commit] [-r repository-path] path\n",
5504 char datebuf[11]; /* YYYY-MM-DD + NUL */
5507 struct blame_cb_args {
5508 struct blame_line *lines;
5512 off_t *line_offsets;
5514 struct got_repository *repo;
5517 static const struct got_error *
5518 blame_cb(void *arg, int nlines, int lineno,
5519 struct got_commit_object *commit, struct got_object_id *id)
5521 const struct got_error *err = NULL;
5522 struct blame_cb_args *a = arg;
5523 struct blame_line *bline;
5525 size_t linesize = 0;
5528 time_t committer_time;
5530 if (nlines != a->nlines ||
5531 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5532 return got_error(GOT_ERR_RANGE);
5534 if (sigint_received)
5535 return got_error(GOT_ERR_ITER_COMPLETED);
5538 return NULL; /* no change in this commit */
5540 /* Annotate this line. */
5541 bline = &a->lines[lineno - 1];
5542 if (bline->annotated)
5544 err = got_object_id_str(&bline->id_str, id);
5548 bline->committer = strdup(got_object_commit_get_committer(commit));
5549 if (bline->committer == NULL) {
5550 err = got_error_from_errno("strdup");
5554 committer_time = got_object_commit_get_committer_time(commit);
5555 if (gmtime_r(&committer_time, &tm) == NULL)
5556 return got_error_from_errno("gmtime_r");
5557 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5559 err = got_error(GOT_ERR_NO_SPACE);
5562 bline->annotated = 1;
5564 /* Print lines annotated so far. */
5565 bline = &a->lines[a->lineno_cur - 1];
5566 if (!bline->annotated)
5569 offset = a->line_offsets[a->lineno_cur - 1];
5570 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5571 err = got_error_from_errno("fseeko");
5575 while (a->lineno_cur <= a->nlines && bline->annotated) {
5576 char *smallerthan, *at, *nl, *committer;
5579 if (getline(&line, &linesize, a->f) == -1) {
5581 err = got_error_from_errno("getline");
5585 committer = bline->committer;
5586 smallerthan = strchr(committer, '<');
5587 if (smallerthan && smallerthan[1] != '\0')
5588 committer = smallerthan + 1;
5589 at = strchr(committer, '@');
5592 len = strlen(committer);
5594 committer[8] = '\0';
5596 nl = strchr(line, '\n');
5599 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5600 bline->id_str, bline->datebuf, committer, line);
5603 bline = &a->lines[a->lineno_cur - 1];
5610 static const struct got_error *
5611 cmd_blame(int argc, char *argv[])
5613 const struct got_error *error;
5614 struct got_repository *repo = NULL;
5615 struct got_worktree *worktree = NULL;
5616 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5617 char *link_target = NULL;
5618 struct got_object_id *obj_id = NULL;
5619 struct got_object_id *commit_id = NULL;
5620 struct got_commit_object *commit = NULL;
5621 struct got_blob_object *blob = NULL;
5622 char *commit_id_str = NULL;
5623 struct blame_cb_args bca;
5624 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5626 int *pack_fds = NULL;
5627 FILE *f1 = NULL, *f2 = NULL;
5629 fd1 = got_opentempfd();
5631 return got_error_from_errno("got_opentempfd");
5633 memset(&bca, 0, sizeof(bca));
5636 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5641 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5644 commit_id_str = optarg;
5647 repo_path = realpath(optarg, NULL);
5648 if (repo_path == NULL)
5649 return got_error_from_errno2("realpath",
5651 got_path_strip_trailing_slashes(repo_path);
5667 cwd = getcwd(NULL, 0);
5669 error = got_error_from_errno("getcwd");
5673 error = got_repo_pack_fds_open(&pack_fds);
5677 if (repo_path == NULL) {
5678 error = got_worktree_open(&worktree, cwd);
5679 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5685 strdup(got_worktree_get_repo_path(worktree));
5686 if (repo_path == NULL) {
5687 error = got_error_from_errno("strdup");
5692 repo_path = strdup(cwd);
5693 if (repo_path == NULL) {
5694 error = got_error_from_errno("strdup");
5700 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5705 const char *prefix = got_worktree_get_path_prefix(worktree);
5708 error = got_worktree_resolve_path(&p, worktree, path);
5711 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5712 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5714 error = got_error_from_errno("asprintf");
5719 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5721 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5724 error = got_repo_map_path(&in_repo_path, repo, path);
5729 if (commit_id_str == NULL) {
5730 struct got_reference *head_ref;
5731 error = got_ref_open(&head_ref, repo, worktree ?
5732 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5735 error = got_ref_resolve(&commit_id, repo, head_ref);
5736 got_ref_close(head_ref);
5740 struct got_reflist_head refs;
5742 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5746 error = got_repo_match_object_id(&commit_id, NULL,
5747 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5748 got_ref_list_free(&refs);
5754 /* Release work tree lock. */
5755 got_worktree_close(worktree);
5759 error = got_object_open_as_commit(&commit, repo, commit_id);
5763 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5768 error = got_object_id_by_path(&obj_id, repo, commit,
5769 link_target ? link_target : in_repo_path);
5773 error = got_object_get_type(&obj_type, repo, obj_id);
5777 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5778 error = got_error_path(link_target ? link_target : in_repo_path,
5783 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5786 bca.f = got_opentemp();
5787 if (bca.f == NULL) {
5788 error = got_error_from_errno("got_opentemp");
5791 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5792 &bca.line_offsets, bca.f, blob);
5793 if (error || bca.nlines == 0)
5796 /* Don't include \n at EOF in the blame line count. */
5797 if (bca.line_offsets[bca.nlines - 1] == filesize)
5800 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5801 if (bca.lines == NULL) {
5802 error = got_error_from_errno("calloc");
5806 bca.nlines_prec = 0;
5814 fd2 = got_opentempfd();
5816 error = got_error_from_errno("got_opentempfd");
5819 fd3 = got_opentempfd();
5821 error = got_error_from_errno("got_opentempfd");
5824 f1 = got_opentemp();
5826 error = got_error_from_errno("got_opentemp");
5829 f2 = got_opentemp();
5831 error = got_error_from_errno("got_opentemp");
5834 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5835 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5836 check_cancelled, NULL, fd2, fd3, f1, f2);
5845 got_object_commit_close(commit);
5847 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5848 error = got_error_from_errno("close");
5849 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5850 error = got_error_from_errno("close");
5851 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5852 error = got_error_from_errno("close");
5853 if (f1 && fclose(f1) == EOF && error == NULL)
5854 error = got_error_from_errno("fclose");
5855 if (f2 && fclose(f2) == EOF && error == NULL)
5856 error = got_error_from_errno("fclose");
5859 got_object_blob_close(blob);
5861 got_worktree_close(worktree);
5863 const struct got_error *close_err = got_repo_close(repo);
5868 const struct got_error *pack_err =
5869 got_repo_pack_fds_close(pack_fds);
5874 for (i = 0; i < bca.nlines; i++) {
5875 struct blame_line *bline = &bca.lines[i];
5876 free(bline->id_str);
5877 free(bline->committer);
5881 free(bca.line_offsets);
5882 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5883 error = got_error_from_errno("fclose");
5890 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5891 "[path]\n", getprogname());
5895 static const struct got_error *
5896 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5897 const char *root_path, struct got_repository *repo)
5899 const struct got_error *err = NULL;
5900 int is_root_path = (strcmp(path, root_path) == 0);
5901 const char *modestr = "";
5902 mode_t mode = got_tree_entry_get_mode(te);
5903 char *link_target = NULL;
5905 path += strlen(root_path);
5906 while (path[0] == '/')
5909 if (got_object_tree_entry_is_submodule(te))
5911 else if (S_ISLNK(mode)) {
5914 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5917 for (i = 0; i < strlen(link_target); i++) {
5918 if (!isprint((unsigned char)link_target[i]))
5919 link_target[i] = '?';
5924 else if (S_ISDIR(mode))
5926 else if (mode & S_IXUSR)
5929 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5930 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5931 link_target ? " -> ": "", link_target ? link_target : "");
5937 static const struct got_error *
5938 print_tree(const char *path, struct got_commit_object *commit,
5939 int show_ids, int recurse, const char *root_path,
5940 struct got_repository *repo)
5942 const struct got_error *err = NULL;
5943 struct got_object_id *tree_id = NULL;
5944 struct got_tree_object *tree = NULL;
5947 err = got_object_id_by_path(&tree_id, repo, commit, path);
5951 err = got_object_open_as_tree(&tree, repo, tree_id);
5954 nentries = got_object_tree_get_nentries(tree);
5955 for (i = 0; i < nentries; i++) {
5956 struct got_tree_entry *te;
5959 if (sigint_received || sigpipe_received)
5962 te = got_object_tree_get_entry(tree, i);
5965 err = got_object_id_str(&id_str,
5966 got_tree_entry_get_id(te));
5969 if (asprintf(&id, "%s ", id_str) == -1) {
5970 err = got_error_from_errno("asprintf");
5976 err = print_entry(te, id, path, root_path, repo);
5981 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5983 if (asprintf(&child_path, "%s%s%s", path,
5984 path[0] == '/' && path[1] == '\0' ? "" : "/",
5985 got_tree_entry_get_name(te)) == -1) {
5986 err = got_error_from_errno("asprintf");
5989 err = print_tree(child_path, commit, show_ids, 1,
5998 got_object_tree_close(tree);
6003 static const struct got_error *
6004 cmd_tree(int argc, char *argv[])
6006 const struct got_error *error;
6007 struct got_repository *repo = NULL;
6008 struct got_worktree *worktree = NULL;
6009 const char *path, *refname = NULL;
6010 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6011 struct got_object_id *commit_id = NULL;
6012 struct got_commit_object *commit = NULL;
6013 char *commit_id_str = NULL;
6014 int show_ids = 0, recurse = 0;
6016 int *pack_fds = NULL;
6019 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6024 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6027 commit_id_str = optarg;
6036 repo_path = realpath(optarg, NULL);
6037 if (repo_path == NULL)
6038 return got_error_from_errno2("realpath",
6040 got_path_strip_trailing_slashes(repo_path);
6058 cwd = getcwd(NULL, 0);
6060 error = got_error_from_errno("getcwd");
6064 error = got_repo_pack_fds_open(&pack_fds);
6068 if (repo_path == NULL) {
6069 error = got_worktree_open(&worktree, cwd);
6070 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6076 strdup(got_worktree_get_repo_path(worktree));
6077 if (repo_path == NULL)
6078 error = got_error_from_errno("strdup");
6082 repo_path = strdup(cwd);
6083 if (repo_path == NULL) {
6084 error = got_error_from_errno("strdup");
6090 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6095 const char *prefix = got_worktree_get_path_prefix(worktree);
6100 error = got_worktree_resolve_path(&p, worktree, path);
6103 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6104 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6106 error = got_error_from_errno("asprintf");
6111 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6115 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6120 error = got_repo_map_path(&in_repo_path, repo, path);
6125 if (commit_id_str == NULL) {
6126 struct got_reference *head_ref;
6128 refname = got_worktree_get_head_ref_name(worktree);
6130 refname = GOT_REF_HEAD;
6131 error = got_ref_open(&head_ref, repo, refname, 0);
6134 error = got_ref_resolve(&commit_id, repo, head_ref);
6135 got_ref_close(head_ref);
6139 struct got_reflist_head refs;
6141 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6145 error = got_repo_match_object_id(&commit_id, NULL,
6146 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6147 got_ref_list_free(&refs);
6153 /* Release work tree lock. */
6154 got_worktree_close(worktree);
6158 error = got_object_open_as_commit(&commit, repo, commit_id);
6162 error = print_tree(in_repo_path, commit, show_ids, recurse,
6163 in_repo_path, repo);
6170 got_object_commit_close(commit);
6172 got_worktree_close(worktree);
6174 const struct got_error *close_err = got_repo_close(repo);
6179 const struct got_error *pack_err =
6180 got_repo_pack_fds_close(pack_fds);
6190 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6191 "[-s status-codes] [path ...]\n", getprogname());
6195 struct got_status_arg {
6200 static const struct got_error *
6201 print_status(void *arg, unsigned char status, unsigned char staged_status,
6202 const char *path, struct got_object_id *blob_id,
6203 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6204 int dirfd, const char *de_name)
6206 struct got_status_arg *st = arg;
6208 if (status == staged_status && (status == GOT_STATUS_DELETE))
6209 status = GOT_STATUS_NO_CHANGE;
6210 if (st != NULL && st->status_codes) {
6211 size_t ncodes = strlen(st->status_codes);
6214 for (i = 0; i < ncodes ; i++) {
6216 if (status == st->status_codes[i] ||
6217 staged_status == st->status_codes[i]) {
6222 if (status == st->status_codes[i] ||
6223 staged_status == st->status_codes[i])
6228 if (st->suppress && j == 0)
6235 printf("%c%c %s\n", status, staged_status, path);
6239 static const struct got_error *
6240 cmd_status(int argc, char *argv[])
6242 const struct got_error *error = NULL;
6243 struct got_repository *repo = NULL;
6244 struct got_worktree *worktree = NULL;
6245 struct got_status_arg st;
6247 struct got_pathlist_head paths;
6248 int ch, i, no_ignores = 0;
6249 int *pack_fds = NULL;
6253 memset(&st, 0, sizeof(st));
6254 st.status_codes = NULL;
6257 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6263 if (st.status_codes != NULL && st.suppress == 0)
6264 option_conflict('S', 's');
6268 for (i = 0; i < strlen(optarg); i++) {
6269 switch (optarg[i]) {
6270 case GOT_STATUS_MODIFY:
6271 case GOT_STATUS_ADD:
6272 case GOT_STATUS_DELETE:
6273 case GOT_STATUS_CONFLICT:
6274 case GOT_STATUS_MISSING:
6275 case GOT_STATUS_OBSTRUCTED:
6276 case GOT_STATUS_UNVERSIONED:
6277 case GOT_STATUS_MODE_CHANGE:
6278 case GOT_STATUS_NONEXISTENT:
6281 errx(1, "invalid status code '%c'",
6285 if (ch == 's' && st.suppress)
6286 option_conflict('s', 'S');
6287 st.status_codes = optarg;
6299 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6303 cwd = getcwd(NULL, 0);
6305 error = got_error_from_errno("getcwd");
6309 error = got_repo_pack_fds_open(&pack_fds);
6313 error = got_worktree_open(&worktree, cwd);
6315 if (error->code == GOT_ERR_NOT_WORKTREE)
6316 error = wrap_not_worktree_error(error, "status", cwd);
6320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6325 error = apply_unveil(got_repo_get_path(repo), 1,
6326 got_worktree_get_root_path(worktree));
6330 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6334 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6335 print_status, &st, check_cancelled, NULL);
6338 const struct got_error *pack_err =
6339 got_repo_pack_fds_close(pack_fds);
6344 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6352 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6353 "[-s reference] [name]\n", getprogname());
6357 static const struct got_error *
6358 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6360 static const struct got_error *err = NULL;
6361 struct got_reflist_head refs;
6362 struct got_reflist_entry *re;
6365 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6366 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6371 TAILQ_FOREACH(re, &refs, entry) {
6373 refstr = got_ref_to_str(re->ref);
6374 if (refstr == NULL) {
6375 err = got_error_from_errno("got_ref_to_str");
6378 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6382 got_ref_list_free(&refs);
6386 static const struct got_error *
6387 delete_ref_by_name(struct got_repository *repo, const char *refname)
6389 const struct got_error *err;
6390 struct got_reference *ref;
6392 err = got_ref_open(&ref, repo, refname, 0);
6396 err = delete_ref(repo, ref);
6401 static const struct got_error *
6402 add_ref(struct got_repository *repo, const char *refname, const char *target)
6404 const struct got_error *err = NULL;
6405 struct got_object_id *id = NULL;
6406 struct got_reference *ref = NULL;
6407 struct got_reflist_head refs;
6410 * Don't let the user create a reference name with a leading '-'.
6411 * While technically a valid reference name, this case is usually
6412 * an unintended typo.
6414 if (refname[0] == '-')
6415 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6418 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6421 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6423 got_ref_list_free(&refs);
6427 err = got_ref_alloc(&ref, refname, id);
6431 err = got_ref_write(ref, repo);
6439 static const struct got_error *
6440 add_symref(struct got_repository *repo, const char *refname, const char *target)
6442 const struct got_error *err = NULL;
6443 struct got_reference *ref = NULL;
6444 struct got_reference *target_ref = NULL;
6447 * Don't let the user create a reference name with a leading '-'.
6448 * While technically a valid reference name, this case is usually
6449 * an unintended typo.
6451 if (refname[0] == '-')
6452 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6454 err = got_ref_open(&target_ref, repo, target, 0);
6458 err = got_ref_alloc_symref(&ref, refname, target_ref);
6462 err = got_ref_write(ref, repo);
6465 got_ref_close(target_ref);
6471 static const struct got_error *
6472 cmd_ref(int argc, char *argv[])
6474 const struct got_error *error = NULL;
6475 struct got_repository *repo = NULL;
6476 struct got_worktree *worktree = NULL;
6477 char *cwd = NULL, *repo_path = NULL;
6478 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6479 const char *obj_arg = NULL, *symref_target= NULL;
6480 char *refname = NULL;
6481 int *pack_fds = NULL;
6483 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6495 repo_path = realpath(optarg, NULL);
6496 if (repo_path == NULL)
6497 return got_error_from_errno2("realpath",
6499 got_path_strip_trailing_slashes(repo_path);
6502 symref_target = optarg;
6513 if (obj_arg && do_list)
6514 option_conflict('c', 'l');
6515 if (obj_arg && do_delete)
6516 option_conflict('c', 'd');
6517 if (obj_arg && symref_target)
6518 option_conflict('c', 's');
6519 if (symref_target && do_delete)
6520 option_conflict('s', 'd');
6521 if (symref_target && do_list)
6522 option_conflict('s', 'l');
6523 if (do_delete && do_list)
6524 option_conflict('d', 'l');
6525 if (sort_by_time && !do_list)
6526 errx(1, "-t option requires -l option");
6532 if (argc != 0 && argc != 1)
6535 refname = strdup(argv[0]);
6536 if (refname == NULL) {
6537 error = got_error_from_errno("strdup");
6544 refname = strdup(argv[0]);
6545 if (refname == NULL) {
6546 error = got_error_from_errno("strdup");
6552 got_path_strip_trailing_slashes(refname);
6555 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6556 "sendfd unveil", NULL) == -1)
6559 cwd = getcwd(NULL, 0);
6561 error = got_error_from_errno("getcwd");
6565 error = got_repo_pack_fds_open(&pack_fds);
6569 if (repo_path == NULL) {
6570 error = got_worktree_open(&worktree, cwd);
6571 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6577 strdup(got_worktree_get_repo_path(worktree));
6578 if (repo_path == NULL)
6579 error = got_error_from_errno("strdup");
6583 repo_path = strdup(cwd);
6584 if (repo_path == NULL) {
6585 error = got_error_from_errno("strdup");
6591 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6597 /* Remove "cpath" promise. */
6598 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6604 error = apply_unveil(got_repo_get_path(repo), do_list,
6605 worktree ? got_worktree_get_root_path(worktree) : NULL);
6610 error = list_refs(repo, refname, sort_by_time);
6612 error = delete_ref_by_name(repo, refname);
6613 else if (symref_target)
6614 error = add_symref(repo, refname, symref_target);
6616 if (obj_arg == NULL)
6618 error = add_ref(repo, refname, obj_arg);
6623 const struct got_error *close_err = got_repo_close(repo);
6628 got_worktree_close(worktree);
6630 const struct got_error *pack_err =
6631 got_repo_pack_fds_close(pack_fds);
6643 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6644 "[-r repository-path] [name]\n", getprogname());
6648 static const struct got_error *
6649 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6650 struct got_reference *ref)
6652 const struct got_error *err = NULL;
6653 const char *refname, *marker = " ";
6656 refname = got_ref_get_name(ref);
6657 if (worktree && strcmp(refname,
6658 got_worktree_get_head_ref_name(worktree)) == 0) {
6659 struct got_object_id *id = NULL;
6661 err = got_ref_resolve(&id, repo, ref);
6664 if (got_object_id_cmp(id,
6665 got_worktree_get_base_commit_id(worktree)) == 0)
6672 if (strncmp(refname, "refs/heads/", 11) == 0)
6674 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6676 if (strncmp(refname, "refs/remotes/", 13) == 0)
6679 refstr = got_ref_to_str(ref);
6681 return got_error_from_errno("got_ref_to_str");
6683 printf("%s%s: %s\n", marker, refname, refstr);
6688 static const struct got_error *
6689 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6691 const char *refname;
6693 if (worktree == NULL)
6694 return got_error(GOT_ERR_NOT_WORKTREE);
6696 refname = got_worktree_get_head_ref_name(worktree);
6698 if (strncmp(refname, "refs/heads/", 11) == 0)
6700 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6703 printf("%s\n", refname);
6708 static const struct got_error *
6709 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6712 static const struct got_error *err = NULL;
6713 struct got_reflist_head refs;
6714 struct got_reflist_entry *re;
6715 struct got_reference *temp_ref = NULL;
6716 int rebase_in_progress, histedit_in_progress;
6721 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6726 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6731 if (rebase_in_progress || histedit_in_progress) {
6732 err = got_ref_open(&temp_ref, repo,
6733 got_worktree_get_head_ref_name(worktree), 0);
6736 list_branch(repo, worktree, temp_ref);
6737 got_ref_close(temp_ref);
6741 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6742 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6747 TAILQ_FOREACH(re, &refs, entry)
6748 list_branch(repo, worktree, re->ref);
6750 got_ref_list_free(&refs);
6752 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6753 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6758 TAILQ_FOREACH(re, &refs, entry)
6759 list_branch(repo, worktree, re->ref);
6761 got_ref_list_free(&refs);
6766 static const struct got_error *
6767 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6768 const char *branch_name)
6770 const struct got_error *err = NULL;
6771 struct got_reference *ref = NULL;
6772 char *refname, *remote_refname = NULL;
6774 if (strncmp(branch_name, "refs/", 5) == 0)
6776 if (strncmp(branch_name, "heads/", 6) == 0)
6778 else if (strncmp(branch_name, "remotes/", 8) == 0)
6781 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6782 return got_error_from_errno("asprintf");
6784 if (asprintf(&remote_refname, "refs/remotes/%s",
6785 branch_name) == -1) {
6786 err = got_error_from_errno("asprintf");
6790 err = got_ref_open(&ref, repo, refname, 0);
6792 const struct got_error *err2;
6793 if (err->code != GOT_ERR_NOT_REF)
6796 * Keep 'err' intact such that if neither branch exists
6797 * we report "refs/heads" rather than "refs/remotes" in
6798 * our error message.
6800 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6807 strcmp(got_worktree_get_head_ref_name(worktree),
6808 got_ref_get_name(ref)) == 0) {
6809 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6810 "will not delete this work tree's current branch");
6814 err = delete_ref(repo, ref);
6819 free(remote_refname);
6823 static const struct got_error *
6824 add_branch(struct got_repository *repo, const char *branch_name,
6825 struct got_object_id *base_commit_id)
6827 const struct got_error *err = NULL;
6828 struct got_reference *ref = NULL;
6829 char *refname = NULL;
6832 * Don't let the user create a branch name with a leading '-'.
6833 * While technically a valid reference name, this case is usually
6834 * an unintended typo.
6836 if (branch_name[0] == '-')
6837 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6839 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6842 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6843 err = got_error_from_errno("asprintf");
6847 err = got_ref_open(&ref, repo, refname, 0);
6849 err = got_error(GOT_ERR_BRANCH_EXISTS);
6851 } else if (err->code != GOT_ERR_NOT_REF)
6854 err = got_ref_alloc(&ref, refname, base_commit_id);
6858 err = got_ref_write(ref, repo);
6866 static const struct got_error *
6867 cmd_branch(int argc, char *argv[])
6869 const struct got_error *error = NULL;
6870 struct got_repository *repo = NULL;
6871 struct got_worktree *worktree = NULL;
6872 char *cwd = NULL, *repo_path = NULL;
6873 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6874 const char *delref = NULL, *commit_id_arg = NULL;
6875 struct got_reference *ref = NULL;
6876 struct got_pathlist_head paths;
6877 struct got_object_id *commit_id = NULL;
6878 char *commit_id_str = NULL;
6879 int *pack_fds = NULL;
6883 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6886 commit_id_arg = optarg;
6898 repo_path = realpath(optarg, NULL);
6899 if (repo_path == NULL)
6900 return got_error_from_errno2("realpath",
6902 got_path_strip_trailing_slashes(repo_path);
6913 if (do_list && delref)
6914 option_conflict('l', 'd');
6915 if (sort_by_time && !do_list)
6916 errx(1, "-t option requires -l option");
6921 if (!do_list && !delref && argc == 0)
6924 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6925 errx(1, "-c option can only be used when creating a branch");
6927 if (do_list || delref) {
6930 } else if (!do_show && argc != 1)
6934 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6935 "sendfd unveil", NULL) == -1)
6938 cwd = getcwd(NULL, 0);
6940 error = got_error_from_errno("getcwd");
6944 error = got_repo_pack_fds_open(&pack_fds);
6948 if (repo_path == NULL) {
6949 error = got_worktree_open(&worktree, cwd);
6950 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6956 strdup(got_worktree_get_repo_path(worktree));
6957 if (repo_path == NULL)
6958 error = got_error_from_errno("strdup");
6962 repo_path = strdup(cwd);
6963 if (repo_path == NULL) {
6964 error = got_error_from_errno("strdup");
6970 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6975 if (do_list || do_show) {
6976 /* Remove "cpath" promise. */
6977 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6983 error = apply_unveil(got_repo_get_path(repo), do_list,
6984 worktree ? got_worktree_get_root_path(worktree) : NULL);
6989 error = show_current_branch(repo, worktree);
6991 error = list_branches(repo, worktree, sort_by_time);
6993 error = delete_branch(repo, worktree, delref);
6995 struct got_reflist_head refs;
6997 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7001 if (commit_id_arg == NULL)
7002 commit_id_arg = worktree ?
7003 got_worktree_get_head_ref_name(worktree) :
7005 error = got_repo_match_object_id(&commit_id, NULL,
7006 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7007 got_ref_list_free(&refs);
7010 error = add_branch(repo, argv[0], commit_id);
7013 if (worktree && do_update) {
7014 struct got_update_progress_arg upa;
7015 char *branch_refname = NULL;
7017 error = got_object_id_str(&commit_id_str, commit_id);
7020 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7024 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7026 error = got_error_from_errno("asprintf");
7029 error = got_ref_open(&ref, repo, branch_refname, 0);
7030 free(branch_refname);
7033 error = switch_head_ref(ref, commit_id, worktree,
7037 error = got_worktree_set_base_commit_id(worktree, repo,
7041 memset(&upa, 0, sizeof(upa));
7042 error = got_worktree_checkout_files(worktree, &paths,
7043 repo, update_progress, &upa, check_cancelled,
7047 if (upa.did_something) {
7048 printf("Updated to %s: %s\n",
7049 got_worktree_get_head_ref_name(worktree),
7052 print_update_progress_stats(&upa);
7059 const struct got_error *close_err = got_repo_close(repo);
7064 got_worktree_close(worktree);
7066 const struct got_error *pack_err =
7067 got_repo_pack_fds_close(pack_fds);
7074 free(commit_id_str);
7075 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7083 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7084 "[-r repository-path] [-s signer-id] name\n", getprogname());
7089 static const struct got_error *
7090 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7092 const struct got_error *err = NULL;
7093 struct got_reflist_entry *re, *se, *new;
7094 struct got_object_id *re_id, *se_id;
7095 struct got_tag_object *re_tag, *se_tag;
7096 time_t re_time, se_time;
7098 STAILQ_FOREACH(re, tags, entry) {
7099 se = STAILQ_FIRST(sorted);
7101 err = got_reflist_entry_dup(&new, re);
7104 STAILQ_INSERT_HEAD(sorted, new, entry);
7107 err = got_ref_resolve(&re_id, repo, re->ref);
7110 err = got_object_open_as_tag(&re_tag, repo, re_id);
7114 re_time = got_object_tag_get_tagger_time(re_tag);
7115 got_object_tag_close(re_tag);
7119 err = got_ref_resolve(&se_id, repo, re->ref);
7122 err = got_object_open_as_tag(&se_tag, repo, se_id);
7126 se_time = got_object_tag_get_tagger_time(se_tag);
7127 got_object_tag_close(se_tag);
7129 if (se_time > re_time) {
7130 err = got_reflist_entry_dup(&new, re);
7133 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7136 se = STAILQ_NEXT(se, entry);
7145 static const struct got_error *
7146 get_tag_refname(char **refname, const char *tag_name)
7148 const struct got_error *err;
7150 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7151 *refname = strdup(tag_name);
7152 if (*refname == NULL)
7153 return got_error_from_errno("strdup");
7154 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7155 err = got_error_from_errno("asprintf");
7163 static const struct got_error *
7164 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7165 const char *allowed_signers, const char *revoked_signers, int verbosity)
7167 static const struct got_error *err = NULL;
7168 struct got_reflist_head refs;
7169 struct got_reflist_entry *re;
7170 char *wanted_refname = NULL;
7175 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7180 struct got_reference *ref;
7181 err = get_tag_refname(&wanted_refname, tag_name);
7184 /* Wanted tag reference should exist. */
7185 err = got_ref_open(&ref, repo, wanted_refname, 0);
7191 TAILQ_FOREACH(re, &refs, entry) {
7192 const char *refname;
7193 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7195 const char *tagger, *ssh_sig = NULL;
7196 char *sig_msg = NULL;
7198 struct got_object_id *id;
7199 struct got_tag_object *tag;
7200 struct got_commit_object *commit = NULL;
7202 refname = got_ref_get_name(re->ref);
7203 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7204 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7207 refstr = got_ref_to_str(re->ref);
7208 if (refstr == NULL) {
7209 err = got_error_from_errno("got_ref_to_str");
7213 err = got_ref_resolve(&id, repo, re->ref);
7216 err = got_object_open_as_tag(&tag, repo, id);
7218 if (err->code != GOT_ERR_OBJ_TYPE) {
7222 /* "lightweight" tag */
7223 err = got_object_open_as_commit(&commit, repo, id);
7228 tagger = got_object_commit_get_committer(commit);
7230 got_object_commit_get_committer_time(commit);
7231 err = got_object_id_str(&id_str, id);
7237 tagger = got_object_tag_get_tagger(tag);
7238 tagger_time = got_object_tag_get_tagger_time(tag);
7239 err = got_object_id_str(&id_str,
7240 got_object_tag_get_object_id(tag));
7245 if (tag && verify_tags) {
7246 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7247 got_object_tag_get_message(tag));
7248 if (ssh_sig && allowed_signers == NULL) {
7249 err = got_error_msg(
7250 GOT_ERR_VERIFY_TAG_SIGNATURE,
7251 "SSH signature verification requires "
7252 "setting allowed_signers in "
7258 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7260 printf("from: %s\n", tagger);
7261 datestr = get_datestr(&tagger_time, datebuf);
7263 printf("date: %s UTC\n", datestr);
7265 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7267 switch (got_object_tag_get_object_type(tag)) {
7268 case GOT_OBJ_TYPE_BLOB:
7269 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7272 case GOT_OBJ_TYPE_TREE:
7273 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7276 case GOT_OBJ_TYPE_COMMIT:
7277 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7280 case GOT_OBJ_TYPE_TAG:
7281 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7291 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7292 allowed_signers, revoked_signers, verbosity);
7293 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7297 printf("signature: %s", sig_msg);
7303 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7306 got_object_commit_close(commit);
7308 tagmsg0 = strdup(got_object_tag_get_message(tag));
7309 got_object_tag_close(tag);
7310 if (tagmsg0 == NULL) {
7311 err = got_error_from_errno("strdup");
7318 line = strsep(&tagmsg, "\n");
7320 printf(" %s\n", line);
7325 got_ref_list_free(&refs);
7326 free(wanted_refname);
7328 if (err == NULL && bad_sigs)
7329 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7333 static const struct got_error *
7334 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7335 const char *tag_name, const char *repo_path)
7337 const struct got_error *err = NULL;
7338 char *template = NULL, *initial_content = NULL;
7339 char *editor = NULL;
7340 int initial_content_len;
7343 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7344 err = got_error_from_errno("asprintf");
7348 initial_content_len = asprintf(&initial_content,
7349 "\n# tagging commit %s as %s\n",
7350 commit_id_str, tag_name);
7351 if (initial_content_len == -1) {
7352 err = got_error_from_errno("asprintf");
7356 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7360 if (write(fd, initial_content, initial_content_len) == -1) {
7361 err = got_error_from_errno2("write", *tagmsg_path);
7365 err = get_editor(&editor);
7368 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7369 initial_content_len, 1);
7371 free(initial_content);
7375 if (fd != -1 && close(fd) == -1 && err == NULL)
7376 err = got_error_from_errno2("close", *tagmsg_path);
7385 static const struct got_error *
7386 add_tag(struct got_repository *repo, const char *tagger,
7387 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7388 const char *signer_id, int verbosity)
7390 const struct got_error *err = NULL;
7391 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7392 char *label = NULL, *commit_id_str = NULL;
7393 struct got_reference *ref = NULL;
7394 char *refname = NULL, *tagmsg = NULL;
7395 char *tagmsg_path = NULL, *tag_id_str = NULL;
7396 int preserve_tagmsg = 0;
7397 struct got_reflist_head refs;
7402 * Don't let the user create a tag name with a leading '-'.
7403 * While technically a valid reference name, this case is usually
7404 * an unintended typo.
7406 if (tag_name[0] == '-')
7407 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7409 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7413 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7414 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7418 err = got_object_id_str(&commit_id_str, commit_id);
7422 err = get_tag_refname(&refname, tag_name);
7425 if (strncmp("refs/tags/", tag_name, 10) == 0)
7428 err = got_ref_open(&ref, repo, refname, 0);
7430 err = got_error(GOT_ERR_TAG_EXISTS);
7432 } else if (err->code != GOT_ERR_NOT_REF)
7435 if (tagmsg_arg == NULL) {
7436 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7437 tag_name, got_repo_get_path(repo));
7439 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7440 tagmsg_path != NULL)
7441 preserve_tagmsg = 1;
7444 /* Editor is done; we can now apply unveil(2) */
7445 err = got_sigs_apply_unveil();
7448 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7453 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7454 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7458 preserve_tagmsg = 1;
7462 err = got_ref_alloc(&ref, refname, tag_id);
7465 preserve_tagmsg = 1;
7469 err = got_ref_write(ref, repo);
7472 preserve_tagmsg = 1;
7476 err = got_object_id_str(&tag_id_str, tag_id);
7479 preserve_tagmsg = 1;
7482 printf("Created tag %s\n", tag_id_str);
7484 if (preserve_tagmsg) {
7485 fprintf(stderr, "%s: tag message preserved in %s\n",
7486 getprogname(), tagmsg_path);
7487 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7488 err = got_error_from_errno2("unlink", tagmsg_path);
7493 free(commit_id_str);
7497 got_ref_list_free(&refs);
7501 static const struct got_error *
7502 cmd_tag(int argc, char *argv[])
7504 const struct got_error *error = NULL;
7505 struct got_repository *repo = NULL;
7506 struct got_worktree *worktree = NULL;
7507 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7508 char *gitconfig_path = NULL, *tagger = NULL;
7509 char *allowed_signers = NULL, *revoked_signers = NULL;
7510 const char *signer_id = NULL;
7511 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7512 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7513 int *pack_fds = NULL;
7515 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7518 commit_id_arg = optarg;
7527 repo_path = realpath(optarg, NULL);
7528 if (repo_path == NULL) {
7529 error = got_error_from_errno2("realpath",
7533 got_path_strip_trailing_slashes(repo_path);
7544 else if (verbosity < 3)
7556 if (do_list || verify_tags) {
7557 if (commit_id_arg != NULL)
7559 "-c option can only be used when creating a tag");
7562 option_conflict('l', 'm');
7564 option_conflict('V', 'm');
7568 option_conflict('l', 's');
7570 option_conflict('V', 's');
7574 } else if (argc != 1)
7581 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7582 "sendfd unveil", NULL) == -1)
7585 cwd = getcwd(NULL, 0);
7587 error = got_error_from_errno("getcwd");
7591 error = got_repo_pack_fds_open(&pack_fds);
7595 if (repo_path == NULL) {
7596 error = got_worktree_open(&worktree, cwd);
7597 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7603 strdup(got_worktree_get_repo_path(worktree));
7604 if (repo_path == NULL)
7605 error = got_error_from_errno("strdup");
7609 repo_path = strdup(cwd);
7610 if (repo_path == NULL) {
7611 error = got_error_from_errno("strdup");
7617 if (do_list || verify_tags) {
7618 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7621 error = get_allowed_signers(&allowed_signers, repo, worktree);
7624 error = get_revoked_signers(&revoked_signers, repo, worktree);
7628 /* Release work tree lock. */
7629 got_worktree_close(worktree);
7634 * Remove "cpath" promise unless needed for signature tmpfile
7638 got_sigs_apply_unveil();
7641 if (pledge("stdio rpath wpath flock proc exec sendfd "
7642 "unveil", NULL) == -1)
7646 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7649 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7650 revoked_signers, verbosity);
7652 error = get_gitconfig_path(&gitconfig_path);
7655 error = got_repo_open(&repo, repo_path, gitconfig_path,
7660 error = get_author(&tagger, repo, worktree);
7663 if (signer_id == NULL)
7664 signer_id = get_signer_id(repo, worktree);
7668 error = got_sigs_apply_unveil();
7672 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7677 if (commit_id_arg == NULL) {
7678 struct got_reference *head_ref;
7679 struct got_object_id *commit_id;
7680 error = got_ref_open(&head_ref, repo,
7681 worktree ? got_worktree_get_head_ref_name(worktree)
7685 error = got_ref_resolve(&commit_id, repo, head_ref);
7686 got_ref_close(head_ref);
7689 error = got_object_id_str(&commit_id_str, commit_id);
7696 /* Release work tree lock. */
7697 got_worktree_close(worktree);
7701 error = add_tag(repo, tagger, tag_name,
7702 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7703 signer_id, verbosity);
7707 const struct got_error *close_err = got_repo_close(repo);
7712 got_worktree_close(worktree);
7714 const struct got_error *pack_err =
7715 got_repo_pack_fds_close(pack_fds);
7721 free(gitconfig_path);
7722 free(commit_id_str);
7724 free(allowed_signers);
7725 free(revoked_signers);
7732 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7736 static const struct got_error *
7737 add_progress(void *arg, unsigned char status, const char *path)
7739 while (path[0] == '/')
7741 printf("%c %s\n", status, path);
7745 static const struct got_error *
7746 cmd_add(int argc, char *argv[])
7748 const struct got_error *error = NULL;
7749 struct got_repository *repo = NULL;
7750 struct got_worktree *worktree = NULL;
7752 struct got_pathlist_head paths;
7753 struct got_pathlist_entry *pe;
7754 int ch, can_recurse = 0, no_ignores = 0;
7755 int *pack_fds = NULL;
7759 while ((ch = getopt(argc, argv, "IR")) != -1) {
7777 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7784 cwd = getcwd(NULL, 0);
7786 error = got_error_from_errno("getcwd");
7790 error = got_repo_pack_fds_open(&pack_fds);
7794 error = got_worktree_open(&worktree, cwd);
7796 if (error->code == GOT_ERR_NOT_WORKTREE)
7797 error = wrap_not_worktree_error(error, "add", cwd);
7801 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7806 error = apply_unveil(got_repo_get_path(repo), 1,
7807 got_worktree_get_root_path(worktree));
7811 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7818 TAILQ_FOREACH(pe, &paths, entry) {
7819 if (asprintf(&ondisk_path, "%s/%s",
7820 got_worktree_get_root_path(worktree),
7822 error = got_error_from_errno("asprintf");
7825 if (lstat(ondisk_path, &sb) == -1) {
7826 if (errno == ENOENT) {
7830 error = got_error_from_errno2("lstat",
7836 if (S_ISDIR(sb.st_mode)) {
7837 error = got_error_msg(GOT_ERR_BAD_PATH,
7838 "adding directories requires -R option");
7844 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7845 NULL, repo, no_ignores);
7848 const struct got_error *close_err = got_repo_close(repo);
7853 got_worktree_close(worktree);
7855 const struct got_error *pack_err =
7856 got_repo_pack_fds_close(pack_fds);
7860 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7868 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7873 static const struct got_error *
7874 print_remove_status(void *arg, unsigned char status,
7875 unsigned char staged_status, const char *path)
7877 while (path[0] == '/')
7879 if (status == GOT_STATUS_NONEXISTENT)
7881 if (status == staged_status && (status == GOT_STATUS_DELETE))
7882 status = GOT_STATUS_NO_CHANGE;
7883 printf("%c%c %s\n", status, staged_status, path);
7887 static const struct got_error *
7888 cmd_remove(int argc, char *argv[])
7890 const struct got_error *error = NULL;
7891 struct got_worktree *worktree = NULL;
7892 struct got_repository *repo = NULL;
7893 const char *status_codes = NULL;
7895 struct got_pathlist_head paths;
7896 struct got_pathlist_entry *pe;
7897 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7898 int ignore_missing_paths = 0;
7899 int *pack_fds = NULL;
7903 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7906 delete_local_mods = 1;
7907 ignore_missing_paths = 1;
7916 for (i = 0; i < strlen(optarg); i++) {
7917 switch (optarg[i]) {
7918 case GOT_STATUS_MODIFY:
7919 delete_local_mods = 1;
7921 case GOT_STATUS_MISSING:
7922 ignore_missing_paths = 1;
7925 errx(1, "invalid status code '%c'",
7929 status_codes = optarg;
7941 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7948 cwd = getcwd(NULL, 0);
7950 error = got_error_from_errno("getcwd");
7954 error = got_repo_pack_fds_open(&pack_fds);
7958 error = got_worktree_open(&worktree, cwd);
7960 if (error->code == GOT_ERR_NOT_WORKTREE)
7961 error = wrap_not_worktree_error(error, "remove", cwd);
7965 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7970 error = apply_unveil(got_repo_get_path(repo), 1,
7971 got_worktree_get_root_path(worktree));
7975 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7982 TAILQ_FOREACH(pe, &paths, entry) {
7983 if (asprintf(&ondisk_path, "%s/%s",
7984 got_worktree_get_root_path(worktree),
7986 error = got_error_from_errno("asprintf");
7989 if (lstat(ondisk_path, &sb) == -1) {
7990 if (errno == ENOENT) {
7994 error = got_error_from_errno2("lstat",
8000 if (S_ISDIR(sb.st_mode)) {
8001 error = got_error_msg(GOT_ERR_BAD_PATH,
8002 "removing directories requires -R option");
8008 error = got_worktree_schedule_delete(worktree, &paths,
8009 delete_local_mods, status_codes, print_remove_status, NULL,
8010 repo, keep_on_disk, ignore_missing_paths);
8013 const struct got_error *close_err = got_repo_close(repo);
8018 got_worktree_close(worktree);
8020 const struct got_error *pack_err =
8021 got_repo_pack_fds_close(pack_fds);
8025 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8033 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8034 "[patchfile]\n", getprogname());
8038 static const struct got_error *
8039 patch_from_stdin(int *patchfd)
8041 const struct got_error *err = NULL;
8044 sig_t sighup, sigint, sigquit;
8046 *patchfd = got_opentempfd();
8048 return got_error_from_errno("got_opentempfd");
8050 sighup = signal(SIGHUP, SIG_DFL);
8051 sigint = signal(SIGINT, SIG_DFL);
8052 sigquit = signal(SIGQUIT, SIG_DFL);
8055 r = read(0, buf, sizeof(buf));
8057 err = got_error_from_errno("read");
8062 if (write(*patchfd, buf, r) == -1) {
8063 err = got_error_from_errno("write");
8068 signal(SIGHUP, sighup);
8069 signal(SIGINT, sigint);
8070 signal(SIGQUIT, sigquit);
8072 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8073 err = got_error_from_errno("lseek");
8083 static const struct got_error *
8084 patch_progress(void *arg, const char *old, const char *new,
8085 unsigned char status, const struct got_error *error, int old_from,
8086 int old_lines, int new_from, int new_lines, int offset,
8087 int ws_mangled, const struct got_error *hunk_err)
8089 const char *path = new == NULL ? old : new;
8091 while (*path == '/')
8095 printf("%c %s\n", status, path);
8098 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8100 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8101 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8102 old_lines, new_from, new_lines);
8103 if (hunk_err != NULL)
8104 printf("%s\n", hunk_err->msg);
8105 else if (offset != 0)
8106 printf("applied with offset %d\n", offset);
8108 printf("hunk contains mangled whitespace\n");
8114 static const struct got_error *
8115 cmd_patch(int argc, char *argv[])
8117 const struct got_error *error = NULL, *close_error = NULL;
8118 struct got_worktree *worktree = NULL;
8119 struct got_repository *repo = NULL;
8120 struct got_reflist_head refs;
8121 struct got_object_id *commit_id = NULL;
8122 const char *commit_id_str = NULL;
8126 int ch, nop = 0, strip = -1, reverse = 0;
8128 int *pack_fds = NULL;
8133 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8134 "unveil", NULL) == -1)
8138 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8141 commit_id_str = optarg;
8147 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8149 errx(1, "pathname strip count is %s: %s",
8165 error = patch_from_stdin(&patchfd);
8168 } else if (argc == 1) {
8169 patchfd = open(argv[0], O_RDONLY);
8170 if (patchfd == -1) {
8171 error = got_error_from_errno2("open", argv[0]);
8174 if (fstat(patchfd, &sb) == -1) {
8175 error = got_error_from_errno2("fstat", argv[0]);
8178 if (!S_ISREG(sb.st_mode)) {
8179 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8185 if ((cwd = getcwd(NULL, 0)) == NULL) {
8186 error = got_error_from_errno("getcwd");
8190 error = got_repo_pack_fds_open(&pack_fds);
8194 error = got_worktree_open(&worktree, cwd);
8198 const char *repo_path = got_worktree_get_repo_path(worktree);
8199 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8203 error = apply_unveil(got_repo_get_path(repo), 0,
8204 got_worktree_get_root_path(worktree));
8208 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8212 if (commit_id_str != NULL) {
8213 error = got_repo_match_object_id(&commit_id, NULL,
8214 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8219 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8220 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8223 got_ref_list_free(&refs);
8226 close_error = got_repo_close(repo);
8228 error = close_error;
8230 if (worktree != NULL) {
8231 close_error = got_worktree_close(worktree);
8233 error = close_error;
8236 const struct got_error *pack_err =
8237 got_repo_pack_fds_close(pack_fds);
8248 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8253 static const struct got_error *
8254 revert_progress(void *arg, unsigned char status, const char *path)
8256 if (status == GOT_STATUS_UNVERSIONED)
8259 while (path[0] == '/')
8261 printf("%c %s\n", status, path);
8265 struct choose_patch_arg {
8266 FILE *patch_script_file;
8270 static const struct got_error *
8271 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8272 int nchanges, const char *action)
8274 const struct got_error *err;
8276 size_t linesize = 0;
8280 case GOT_STATUS_ADD:
8281 printf("A %s\n%s this addition? [y/n] ", path, action);
8283 case GOT_STATUS_DELETE:
8284 printf("D %s\n%s this deletion? [y/n] ", path, action);
8286 case GOT_STATUS_MODIFY:
8287 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8288 return got_error_from_errno("fseek");
8289 printf(GOT_COMMIT_SEP_STR);
8290 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8292 if (linelen == -1 && ferror(patch_file)) {
8293 err = got_error_from_errno("getline");
8298 printf(GOT_COMMIT_SEP_STR);
8299 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8300 path, n, nchanges, action);
8303 return got_error_path(path, GOT_ERR_FILE_STATUS);
8310 static const struct got_error *
8311 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8312 FILE *patch_file, int n, int nchanges)
8314 const struct got_error *err = NULL;
8316 size_t linesize = 0;
8319 struct choose_patch_arg *a = arg;
8321 *choice = GOT_PATCH_CHOICE_NONE;
8323 if (a->patch_script_file) {
8325 err = show_change(status, path, patch_file, n, nchanges,
8329 linelen = getline(&line, &linesize, a->patch_script_file);
8330 if (linelen == -1) {
8331 if (ferror(a->patch_script_file))
8332 return got_error_from_errno("getline");
8335 nl = strchr(line, '\n');
8338 if (strcmp(line, "y") == 0) {
8339 *choice = GOT_PATCH_CHOICE_YES;
8341 } else if (strcmp(line, "n") == 0) {
8342 *choice = GOT_PATCH_CHOICE_NO;
8344 } else if (strcmp(line, "q") == 0 &&
8345 status == GOT_STATUS_MODIFY) {
8346 *choice = GOT_PATCH_CHOICE_QUIT;
8349 printf("invalid response '%s'\n", line);
8354 while (resp != 'y' && resp != 'n' && resp != 'q') {
8355 err = show_change(status, path, patch_file, n, nchanges,
8362 if (status == GOT_STATUS_MODIFY) {
8363 if (resp != 'y' && resp != 'n' && resp != 'q') {
8364 printf("invalid response '%c'\n", resp);
8367 } else if (resp != 'y' && resp != 'n') {
8368 printf("invalid response '%c'\n", resp);
8374 *choice = GOT_PATCH_CHOICE_YES;
8375 else if (resp == 'n')
8376 *choice = GOT_PATCH_CHOICE_NO;
8377 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8378 *choice = GOT_PATCH_CHOICE_QUIT;
8383 struct wt_commitable_path_arg {
8384 struct got_pathlist_head *commit_paths;
8389 * Shortcut work tree status callback to determine if the set of paths scanned
8390 * has at least one versioned path that is being modified and, if not NULL, is
8391 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8392 * soon as a path is passed with a status that satisfies this criteria.
8394 static const struct got_error *
8395 worktree_has_commitable_path(void *arg, unsigned char status,
8396 unsigned char staged_status, const char *path,
8397 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8398 struct got_object_id *commit_id, int dirfd, const char *de_name)
8400 struct wt_commitable_path_arg *a = arg;
8402 if (status == staged_status && (status == GOT_STATUS_DELETE))
8403 status = GOT_STATUS_NO_CHANGE;
8405 if (!(status == GOT_STATUS_NO_CHANGE ||
8406 status == GOT_STATUS_UNVERSIONED) ||
8407 staged_status != GOT_STATUS_NO_CHANGE) {
8408 if (a->commit_paths != NULL) {
8409 struct got_pathlist_entry *pe;
8411 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8412 if (strncmp(path, pe->path,
8413 pe->path_len) == 0) {
8414 *a->has_changes = 1;
8419 *a->has_changes = 1;
8421 if (*a->has_changes)
8422 return got_error(GOT_ERR_FILE_MODIFIED);
8429 * Check that the changeset of the commit identified by id is
8430 * comprised of at least one modified path that is being committed.
8432 static const struct got_error *
8433 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8434 struct got_object_id *id, struct got_worktree *worktree,
8435 struct got_repository *repo)
8437 const struct got_error *err;
8438 struct got_pathlist_head paths;
8439 struct got_commit_object *commit = NULL, *pcommit = NULL;
8440 struct got_tree_object *tree = NULL, *ptree = NULL;
8441 struct got_object_qid *pid;
8445 err = got_object_open_as_commit(&commit, repo, id);
8449 err = got_object_open_as_tree(&tree, repo,
8450 got_object_commit_get_tree_id(commit));
8454 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8456 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8460 err = got_object_open_as_tree(&ptree, repo,
8461 got_object_commit_get_tree_id(pcommit));
8466 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8467 got_diff_tree_collect_changed_paths, &paths, 0);
8471 err = got_worktree_status(worktree, &paths, repo, 0,
8472 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8473 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8475 * At least one changed path in the referenced commit is
8476 * modified in the work tree, that's all we need to know!
8482 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8484 got_object_commit_close(commit);
8486 got_object_commit_close(pcommit);
8488 got_object_tree_close(tree);
8490 got_object_tree_close(ptree);
8495 * Remove any "logmsg" reference comprised entirely of paths that have
8496 * been reverted in this work tree. If any path in the logmsg ref changeset
8497 * remains in a changed state in the worktree, do not remove the reference.
8499 static const struct got_error *
8500 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8502 const struct got_error *err;
8503 struct got_reflist_head refs;
8504 struct got_reflist_entry *re;
8505 struct got_commit_object *commit = NULL;
8506 struct got_object_id *commit_id = NULL;
8507 struct wt_commitable_path_arg wcpa;
8508 char *uuidstr = NULL;
8512 err = got_worktree_get_uuid(&uuidstr, worktree);
8516 err = got_ref_list(&refs, repo, "refs/got/worktree",
8517 got_ref_cmp_by_name, repo);
8521 TAILQ_FOREACH(re, &refs, entry) {
8522 const char *refname;
8523 int has_changes = 0;
8525 refname = got_ref_get_name(re->ref);
8527 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8528 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8529 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8530 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8531 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8532 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8536 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8537 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8541 err = got_repo_match_object_id(&commit_id, NULL, refname,
8542 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8546 err = got_object_open_as_commit(&commit, repo, commit_id);
8550 wcpa.commit_paths = NULL;
8551 wcpa.has_changes = &has_changes;
8553 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8559 err = got_ref_delete(re->ref, repo);
8564 got_object_commit_close(commit);
8573 got_ref_list_free(&refs);
8575 got_object_commit_close(commit);
8579 static const struct got_error *
8580 cmd_revert(int argc, char *argv[])
8582 const struct got_error *error = NULL;
8583 struct got_worktree *worktree = NULL;
8584 struct got_repository *repo = NULL;
8585 char *cwd = NULL, *path = NULL;
8586 struct got_pathlist_head paths;
8587 struct got_pathlist_entry *pe;
8588 int ch, can_recurse = 0, pflag = 0;
8589 FILE *patch_script_file = NULL;
8590 const char *patch_script_path = NULL;
8591 struct choose_patch_arg cpa;
8592 int *pack_fds = NULL;
8596 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8599 patch_script_path = optarg;
8617 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8618 "unveil", NULL) == -1)
8623 if (patch_script_path && !pflag)
8624 errx(1, "-F option can only be used together with -p option");
8626 cwd = getcwd(NULL, 0);
8628 error = got_error_from_errno("getcwd");
8632 error = got_repo_pack_fds_open(&pack_fds);
8636 error = got_worktree_open(&worktree, cwd);
8638 if (error->code == GOT_ERR_NOT_WORKTREE)
8639 error = wrap_not_worktree_error(error, "revert", cwd);
8643 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8648 if (patch_script_path) {
8649 patch_script_file = fopen(patch_script_path, "re");
8650 if (patch_script_file == NULL) {
8651 error = got_error_from_errno2("fopen",
8658 * XXX "c" perm needed on repo dir to delete merge references.
8660 error = apply_unveil(got_repo_get_path(repo), 0,
8661 got_worktree_get_root_path(worktree));
8665 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8672 TAILQ_FOREACH(pe, &paths, entry) {
8673 if (asprintf(&ondisk_path, "%s/%s",
8674 got_worktree_get_root_path(worktree),
8676 error = got_error_from_errno("asprintf");
8679 if (lstat(ondisk_path, &sb) == -1) {
8680 if (errno == ENOENT) {
8684 error = got_error_from_errno2("lstat",
8690 if (S_ISDIR(sb.st_mode)) {
8691 error = got_error_msg(GOT_ERR_BAD_PATH,
8692 "reverting directories requires -R option");
8698 cpa.patch_script_file = patch_script_file;
8699 cpa.action = "revert";
8700 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8701 pflag ? choose_patch : NULL, &cpa, repo);
8703 error = rm_logmsg_ref(worktree, repo);
8705 if (patch_script_file && fclose(patch_script_file) == EOF &&
8707 error = got_error_from_errno2("fclose", patch_script_path);
8709 const struct got_error *close_err = got_repo_close(repo);
8714 got_worktree_close(worktree);
8716 const struct got_error *pack_err =
8717 got_repo_pack_fds_close(pack_fds);
8721 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8730 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8731 "[-m message] [path ...]\n", getprogname());
8735 struct collect_commit_logmsg_arg {
8736 const char *cmdline_log;
8737 const char *prepared_log;
8738 const char *merged_log;
8739 int non_interactive;
8741 const char *worktree_path;
8742 const char *branch_name;
8743 const char *repo_path;
8748 static const struct got_error *
8749 read_prepared_logmsg(char **logmsg, const char *path)
8751 const struct got_error *err = NULL;
8757 memset(&sb, 0, sizeof(sb));
8759 f = fopen(path, "re");
8761 return got_error_from_errno2("fopen", path);
8763 if (fstat(fileno(f), &sb) == -1) {
8764 err = got_error_from_errno2("fstat", path);
8767 if (sb.st_size == 0) {
8768 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8772 *logmsg = malloc(sb.st_size + 1);
8773 if (*logmsg == NULL) {
8774 err = got_error_from_errno("malloc");
8778 r = fread(*logmsg, 1, sb.st_size, f);
8779 if (r != sb.st_size) {
8781 err = got_error_from_errno2("fread", path);
8783 err = got_error(GOT_ERR_IO);
8786 (*logmsg)[sb.st_size] = '\0';
8788 if (fclose(f) == EOF && err == NULL)
8789 err = got_error_from_errno2("fclose", path);
8797 static const struct got_error *
8798 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8799 const char *diff_path, char **logmsg, void *arg)
8801 char *initial_content = NULL;
8802 struct got_pathlist_entry *pe;
8803 const struct got_error *err = NULL;
8804 char *template = NULL;
8805 char *prepared_msg = NULL, *merged_msg = NULL;
8806 struct collect_commit_logmsg_arg *a = arg;
8807 int initial_content_len;
8811 /* if a message was specified on the command line, just use it */
8812 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8813 len = strlen(a->cmdline_log) + 1;
8814 *logmsg = malloc(len + 1);
8815 if (*logmsg == NULL)
8816 return got_error_from_errno("malloc");
8817 strlcpy(*logmsg, a->cmdline_log, len);
8819 } else if (a->prepared_log != NULL && a->non_interactive)
8820 return read_prepared_logmsg(logmsg, a->prepared_log);
8822 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8823 return got_error_from_errno("asprintf");
8825 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8829 if (a->prepared_log) {
8830 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8833 } else if (a->merged_log) {
8834 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8839 initial_content_len = asprintf(&initial_content,
8840 "%s%s\n# changes to be committed on branch %s:\n",
8841 prepared_msg ? prepared_msg : "",
8842 merged_msg ? merged_msg : "", a->branch_name);
8843 if (initial_content_len == -1) {
8844 err = got_error_from_errno("asprintf");
8848 if (write(fd, initial_content, initial_content_len) == -1) {
8849 err = got_error_from_errno2("write", a->logmsg_path);
8853 TAILQ_FOREACH(pe, commitable_paths, entry) {
8854 struct got_commitable *ct = pe->data;
8855 dprintf(fd, "# %c %s\n",
8856 got_commitable_get_status(ct),
8857 got_commitable_get_path(ct));
8861 dprintf(fd, "# detailed changes can be viewed in %s\n",
8865 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8866 initial_content_len, a->prepared_log ? 0 : 1);
8868 free(initial_content);
8873 if (fd != -1 && close(fd) == -1 && err == NULL)
8874 err = got_error_from_errno2("close", a->logmsg_path);
8876 /* Editor is done; we can now apply unveil(2) */
8878 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8886 static const struct got_error *
8887 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8888 const char *type, int has_content)
8890 const struct got_error *err = NULL;
8891 char *logmsg = NULL;
8893 err = got_object_commit_get_logmsg(&logmsg, commit);
8897 if (fprintf(f, "%s# log message of %s commit %s:%s",
8898 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8899 err = got_ferror(f, GOT_ERR_IO);
8906 * Lookup "logmsg" references of backed-out and cherrypicked commits
8907 * belonging to the current work tree. If found, and the worktree has
8908 * at least one modified file that was changed in the referenced commit,
8909 * add its log message to a new temporary file at *logmsg_path.
8910 * Add all refs found to matched_refs to be scheduled for removal on
8911 * successful commit.
8913 static const struct got_error *
8914 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8915 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8916 struct got_repository *repo)
8918 const struct got_error *err;
8919 struct got_commit_object *commit = NULL;
8920 struct got_object_id *id = NULL;
8921 struct got_reflist_head refs;
8922 struct got_reflist_entry *re, *re_match;
8924 char *uuidstr = NULL;
8925 int added_logmsg = 0;
8929 *logmsg_path = NULL;
8931 err = got_worktree_get_uuid(&uuidstr, worktree);
8935 err = got_ref_list(&refs, repo, "refs/got/worktree",
8936 got_ref_cmp_by_name, repo);
8940 TAILQ_FOREACH(re, &refs, entry) {
8941 const char *refname, *type;
8942 struct wt_commitable_path_arg wcpa;
8945 refname = got_ref_get_name(re->ref);
8947 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8948 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8949 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8950 type = "cherrypicked";
8951 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8952 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8953 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8954 type = "backed-out";
8958 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8959 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8963 err = got_repo_match_object_id(&id, NULL, refname,
8964 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8968 err = got_object_open_as_commit(&commit, repo, id);
8972 wcpa.commit_paths = paths;
8973 wcpa.has_changes = &add_logmsg;
8975 err = commit_path_changed_in_worktree(&wcpa, id,
8982 err = got_opentemp_named(logmsg_path, &f,
8983 "got-commit-logmsg", "");
8987 err = cat_logmsg(f, commit, refname, type,
8994 err = got_reflist_entry_dup(&re_match, re);
8997 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9000 got_object_commit_close(commit);
9009 got_ref_list_free(&refs);
9011 got_object_commit_close(commit);
9012 if (f && fclose(f) == EOF && err == NULL)
9013 err = got_error_from_errno("fclose");
9014 if (!added_logmsg) {
9015 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9016 err = got_error_from_errno2("unlink", *logmsg_path);
9017 *logmsg_path = NULL;
9022 static const struct got_error *
9023 cmd_commit(int argc, char *argv[])
9025 const struct got_error *error = NULL;
9026 struct got_worktree *worktree = NULL;
9027 struct got_repository *repo = NULL;
9028 char *cwd = NULL, *id_str = NULL;
9029 struct got_object_id *id = NULL;
9030 const char *logmsg = NULL;
9031 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9032 struct collect_commit_logmsg_arg cl_arg;
9033 const char *author = NULL;
9034 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9035 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9036 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9038 struct got_pathlist_head paths;
9039 struct got_reflist_head refs;
9040 struct got_reflist_entry *re;
9041 int *pack_fds = NULL;
9045 cl_arg.logmsg_path = NULL;
9047 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9051 error = valid_author(author);
9057 option_conflict('F', 'm');
9058 prepared_logmsg = realpath(optarg, NULL);
9059 if (prepared_logmsg == NULL)
9060 return got_error_from_errno2("realpath",
9064 if (prepared_logmsg)
9065 option_conflict('m', 'F');
9069 non_interactive = 1;
9075 allow_bad_symlinks = 1;
9087 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9088 "unveil", NULL) == -1)
9091 cwd = getcwd(NULL, 0);
9093 error = got_error_from_errno("getcwd");
9097 error = got_repo_pack_fds_open(&pack_fds);
9101 error = got_worktree_open(&worktree, cwd);
9103 if (error->code == GOT_ERR_NOT_WORKTREE)
9104 error = wrap_not_worktree_error(error, "commit", cwd);
9108 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9111 if (rebase_in_progress) {
9112 error = got_error(GOT_ERR_REBASING);
9116 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9121 error = get_gitconfig_path(&gitconfig_path);
9124 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9125 gitconfig_path, pack_fds);
9129 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9132 if (merge_in_progress) {
9133 error = got_error(GOT_ERR_MERGE_BUSY);
9137 error = get_author(&committer, repo, worktree);
9141 if (author != NULL && !strcmp(committer, author)) {
9142 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
9150 * unveil(2) traverses exec(2); if an editor is used we have
9151 * to apply unveil after the log message has been written.
9153 if (logmsg == NULL || strlen(logmsg) == 0)
9154 error = get_editor(&editor);
9156 error = apply_unveil(got_repo_get_path(repo), 0,
9157 got_worktree_get_root_path(worktree));
9161 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9165 if (prepared_logmsg == NULL) {
9166 error = lookup_logmsg_ref(&merged_logmsg,
9167 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9172 cl_arg.editor = editor;
9173 cl_arg.cmdline_log = logmsg;
9174 cl_arg.prepared_log = prepared_logmsg;
9175 cl_arg.merged_log = merged_logmsg;
9176 cl_arg.non_interactive = non_interactive;
9177 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9178 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9179 if (!histedit_in_progress) {
9180 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9181 error = got_error(GOT_ERR_COMMIT_BRANCH);
9184 cl_arg.branch_name += 11;
9186 cl_arg.repo_path = got_repo_get_path(repo);
9187 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9188 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9189 print_status, NULL, repo);
9191 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9192 cl_arg.logmsg_path != NULL)
9193 preserve_logmsg = 1;
9197 error = got_object_id_str(&id_str, id);
9200 printf("Created commit %s\n", id_str);
9202 TAILQ_FOREACH(re, &refs, entry) {
9203 error = got_ref_delete(re->ref, repo);
9209 if (preserve_logmsg) {
9210 fprintf(stderr, "%s: log message preserved in %s\n",
9211 getprogname(), cl_arg.logmsg_path);
9212 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9214 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9215 free(cl_arg.logmsg_path);
9216 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9217 error = got_error_from_errno2("unlink", merged_logmsg);
9218 free(merged_logmsg);
9220 const struct got_error *close_err = got_repo_close(repo);
9225 got_worktree_close(worktree);
9227 const struct got_error *pack_err =
9228 got_repo_pack_fds_close(pack_fds);
9232 got_ref_list_free(&refs);
9233 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9236 free(gitconfig_path);
9239 free(prepared_logmsg);
9246 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9247 "[-r repository-path] [-t tag] [remote-repository]\n",
9253 print_load_info(int print_colored, int print_found, int print_trees,
9254 int ncolored, int nfound, int ntrees)
9256 if (print_colored) {
9257 printf("%d commit%s colored", ncolored,
9258 ncolored == 1 ? "" : "s");
9261 printf("%s%d object%s found",
9262 ncolored > 0 ? "; " : "",
9263 nfound, nfound == 1 ? "" : "s");
9266 printf("; %d tree%s scanned", ntrees,
9267 ntrees == 1 ? "" : "s");
9271 struct got_send_progress_arg {
9272 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9279 int last_nobj_total;
9283 int printed_something;
9285 struct got_pathlist_head *delete_branches;
9288 static const struct got_error *
9289 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9290 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9291 int nobj_written, off_t bytes_sent, const char *refname,
9292 const char *errmsg, int success)
9294 struct got_send_progress_arg *a = arg;
9295 char scaled_packsize[FMT_SCALED_STRSIZE];
9296 char scaled_sent[FMT_SCALED_STRSIZE];
9297 int p_deltify = 0, p_written = 0, p_sent = 0;
9298 int print_colored = 0, print_found = 0, print_trees = 0;
9299 int print_searching = 0, print_total = 0;
9300 int print_deltify = 0, print_written = 0, print_sent = 0;
9302 if (a->verbosity < 0)
9306 const char *status = success ? "accepted" : "rejected";
9309 struct got_pathlist_entry *pe;
9310 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9311 const char *branchname = pe->path;
9312 if (got_path_cmp(branchname, refname,
9313 strlen(branchname), strlen(refname)) == 0) {
9315 a->sent_something = 1;
9321 if (a->printed_something)
9323 printf("Server has %s %s", status, refname);
9325 printf(": %s", errmsg);
9326 a->printed_something = 1;
9330 if (a->last_ncolored != ncolored) {
9332 a->last_ncolored = ncolored;
9335 if (a->last_nfound != nfound) {
9338 a->last_nfound = nfound;
9341 if (a->last_ntrees != ntrees) {
9345 a->last_ntrees = ntrees;
9348 if ((print_colored || print_found || print_trees) &&
9351 print_load_info(print_colored, print_found, print_trees,
9352 ncolored, nfound, ntrees);
9353 a->printed_something = 1;
9356 } else if (!a->loading_done) {
9358 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9360 a->loading_done = 1;
9363 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9364 return got_error_from_errno("fmt_scaled");
9365 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9366 return got_error_from_errno("fmt_scaled");
9368 if (a->last_ncommits != ncommits) {
9369 print_searching = 1;
9370 a->last_ncommits = ncommits;
9373 if (a->last_nobj_total != nobj_total) {
9374 print_searching = 1;
9376 a->last_nobj_total = nobj_total;
9379 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9380 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9381 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9382 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9383 return got_error(GOT_ERR_NO_SPACE);
9386 if (nobj_deltify > 0 || nobj_written > 0) {
9387 if (nobj_deltify > 0) {
9388 p_deltify = (nobj_deltify * 100) / nobj_total;
9389 if (p_deltify != a->last_p_deltify) {
9390 a->last_p_deltify = p_deltify;
9391 print_searching = 1;
9396 if (nobj_written > 0) {
9397 p_written = (nobj_written * 100) / nobj_total;
9398 if (p_written != a->last_p_written) {
9399 a->last_p_written = p_written;
9400 print_searching = 1;
9408 if (bytes_sent > 0) {
9409 p_sent = (bytes_sent * 100) / packfile_size;
9410 if (p_sent != a->last_p_sent) {
9411 a->last_p_sent = p_sent;
9412 print_searching = 1;
9418 a->sent_something = 1;
9421 if (print_searching || print_total || print_deltify || print_written ||
9424 if (print_searching)
9425 printf("packing %d reference%s", ncommits,
9426 ncommits == 1 ? "" : "s");
9428 printf("; %d object%s", nobj_total,
9429 nobj_total == 1 ? "" : "s");
9431 printf("; deltify: %d%%", p_deltify);
9433 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9434 scaled_packsize, p_sent);
9435 else if (print_written)
9436 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9437 scaled_packsize, p_written);
9438 if (print_searching || print_total || print_deltify ||
9439 print_written || print_sent) {
9440 a->printed_something = 1;
9446 static const struct got_error *
9447 cmd_send(int argc, char *argv[])
9449 const struct got_error *error = NULL;
9450 char *cwd = NULL, *repo_path = NULL;
9451 const char *remote_name;
9452 char *proto = NULL, *host = NULL, *port = NULL;
9453 char *repo_name = NULL, *server_path = NULL;
9454 const struct got_remote_repo *remotes, *remote = NULL;
9455 int nremotes, nbranches = 0, ndelete_branches = 0;
9456 struct got_repository *repo = NULL;
9457 struct got_worktree *worktree = NULL;
9458 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9459 struct got_pathlist_head branches;
9460 struct got_pathlist_head tags;
9461 struct got_reflist_head all_branches;
9462 struct got_reflist_head all_tags;
9463 struct got_pathlist_head delete_args;
9464 struct got_pathlist_head delete_branches;
9465 struct got_reflist_entry *re;
9466 struct got_pathlist_entry *pe;
9467 int i, ch, sendfd = -1, sendstatus;
9469 struct got_send_progress_arg spa;
9470 int verbosity = 0, overwrite_refs = 0;
9471 int send_all_branches = 0, send_all_tags = 0;
9472 struct got_reference *ref = NULL;
9473 int *pack_fds = NULL;
9475 TAILQ_INIT(&branches);
9477 TAILQ_INIT(&all_branches);
9478 TAILQ_INIT(&all_tags);
9479 TAILQ_INIT(&delete_args);
9480 TAILQ_INIT(&delete_branches);
9482 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9485 send_all_branches = 1;
9488 error = got_pathlist_append(&branches, optarg, NULL);
9494 error = got_pathlist_append(&delete_args, optarg, NULL);
9505 repo_path = realpath(optarg, NULL);
9506 if (repo_path == NULL)
9507 return got_error_from_errno2("realpath",
9509 got_path_strip_trailing_slashes(repo_path);
9515 error = got_pathlist_append(&tags, optarg, NULL);
9522 else if (verbosity < 3)
9533 if (send_all_branches && !TAILQ_EMPTY(&branches))
9534 option_conflict('a', 'b');
9535 if (send_all_tags && !TAILQ_EMPTY(&tags))
9536 option_conflict('T', 't');
9540 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9542 remote_name = argv[0];
9546 cwd = getcwd(NULL, 0);
9548 error = got_error_from_errno("getcwd");
9552 error = got_repo_pack_fds_open(&pack_fds);
9556 if (repo_path == NULL) {
9557 error = got_worktree_open(&worktree, cwd);
9558 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9564 strdup(got_worktree_get_repo_path(worktree));
9565 if (repo_path == NULL)
9566 error = got_error_from_errno("strdup");
9570 repo_path = strdup(cwd);
9571 if (repo_path == NULL) {
9572 error = got_error_from_errno("strdup");
9578 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9583 worktree_conf = got_worktree_get_gotconfig(worktree);
9584 if (worktree_conf) {
9585 got_gotconfig_get_remotes(&nremotes, &remotes,
9587 for (i = 0; i < nremotes; i++) {
9588 if (strcmp(remotes[i].name, remote_name) == 0) {
9589 remote = &remotes[i];
9595 if (remote == NULL) {
9596 repo_conf = got_repo_get_gotconfig(repo);
9598 got_gotconfig_get_remotes(&nremotes, &remotes,
9600 for (i = 0; i < nremotes; i++) {
9601 if (strcmp(remotes[i].name, remote_name) == 0) {
9602 remote = &remotes[i];
9608 if (remote == NULL) {
9609 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9610 for (i = 0; i < nremotes; i++) {
9611 if (strcmp(remotes[i].name, remote_name) == 0) {
9612 remote = &remotes[i];
9617 if (remote == NULL) {
9618 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9622 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9623 &repo_name, remote->send_url);
9627 if (strcmp(proto, "git") == 0) {
9629 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9630 "sendfd dns inet unveil", NULL) == -1)
9633 } else if (strcmp(proto, "git+ssh") == 0 ||
9634 strcmp(proto, "ssh") == 0) {
9636 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9637 "sendfd unveil", NULL) == -1)
9640 } else if (strcmp(proto, "http") == 0 ||
9641 strcmp(proto, "git+http") == 0) {
9642 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9645 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9649 error = got_dial_apply_unveil(proto);
9653 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9657 if (send_all_branches) {
9658 error = got_ref_list(&all_branches, repo, "refs/heads",
9659 got_ref_cmp_by_name, NULL);
9662 TAILQ_FOREACH(re, &all_branches, entry) {
9663 const char *branchname = got_ref_get_name(re->ref);
9664 error = got_pathlist_append(&branches,
9670 } else if (nbranches == 0) {
9671 for (i = 0; i < remote->nsend_branches; i++) {
9672 error = got_pathlist_append(&branches,
9673 remote->send_branches[i], NULL);
9679 if (send_all_tags) {
9680 error = got_ref_list(&all_tags, repo, "refs/tags",
9681 got_ref_cmp_by_name, NULL);
9684 TAILQ_FOREACH(re, &all_tags, entry) {
9685 const char *tagname = got_ref_get_name(re->ref);
9686 error = got_pathlist_append(&tags,
9694 * To prevent accidents only branches in refs/heads/ can be deleted
9695 * with 'got send -d'.
9696 * Deleting anything else requires local repository access or Git.
9698 TAILQ_FOREACH(pe, &delete_args, entry) {
9699 const char *branchname = pe->path;
9701 struct got_pathlist_entry *new;
9702 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9703 s = strdup(branchname);
9705 error = got_error_from_errno("strdup");
9709 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9710 error = got_error_from_errno("asprintf");
9714 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9715 if (error || new == NULL /* duplicate */)
9722 if (nbranches == 0 && ndelete_branches == 0) {
9723 struct got_reference *head_ref;
9725 error = got_ref_open(&head_ref, repo,
9726 got_worktree_get_head_ref_name(worktree), 0);
9728 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9731 if (got_ref_is_symbolic(head_ref)) {
9732 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9733 got_ref_close(head_ref);
9738 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9745 if (verbosity >= 0) {
9746 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9747 remote->name, proto, host,
9748 port ? ":" : "", port ? port : "",
9749 *server_path == '/' ? "" : "/", server_path);
9752 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9753 server_path, verbosity);
9757 memset(&spa, 0, sizeof(spa));
9758 spa.last_scaled_packsize[0] = '\0';
9759 spa.last_p_deltify = -1;
9760 spa.last_p_written = -1;
9761 spa.verbosity = verbosity;
9762 spa.delete_branches = &delete_branches;
9763 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9764 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9765 check_cancelled, NULL);
9766 if (spa.printed_something)
9770 if (!spa.sent_something && verbosity >= 0)
9771 printf("Already up-to-date\n");
9774 if (kill(sendpid, SIGTERM) == -1)
9775 error = got_error_from_errno("kill");
9776 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9777 error = got_error_from_errno("waitpid");
9779 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9780 error = got_error_from_errno("close");
9782 const struct got_error *close_err = got_repo_close(repo);
9787 got_worktree_close(worktree);
9789 const struct got_error *pack_err =
9790 got_repo_pack_fds_close(pack_fds);
9796 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9797 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9798 got_ref_list_free(&all_branches);
9799 got_ref_list_free(&all_tags);
9800 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9801 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9813 * Print and if delete is set delete all ref_prefix references.
9814 * If wanted_ref is not NULL, only print or delete this reference.
9816 static const struct got_error *
9817 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9818 const char *wanted_ref, int delete, struct got_worktree *worktree,
9819 struct got_repository *repo)
9821 const struct got_error *err;
9822 struct got_pathlist_head paths;
9823 struct got_reflist_head refs;
9824 struct got_reflist_entry *re;
9825 struct got_reflist_object_id_map *refs_idmap = NULL;
9826 struct got_commit_object *commit = NULL;
9827 struct got_object_id *id = NULL;
9828 const char *header_prefix;
9829 char *uuidstr = NULL;
9835 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9839 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9843 if (worktree != NULL) {
9844 err = got_worktree_get_uuid(&uuidstr, worktree);
9850 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9854 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9855 header_prefix = "backout";
9857 header_prefix = "cherrypick";
9859 TAILQ_FOREACH(re, &refs, entry) {
9860 const char *refname, *wt;
9862 refname = got_ref_get_name(re->ref);
9864 err = check_cancelled(NULL);
9868 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9869 refname += prefix_len + 1; /* skip '-' delimiter */
9875 if (worktree == NULL || strncmp(refname, uuidstr,
9876 GOT_WORKTREE_UUID_STRLEN) == 0)
9877 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9881 err = got_repo_match_object_id(&id, NULL, refname,
9882 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9886 err = got_object_open_as_commit(&commit, repo, id);
9891 found = strncmp(wanted_ref, refname,
9892 strlen(wanted_ref)) == 0;
9893 if (wanted_ref && !found) {
9894 struct got_reflist_head *ci_refs;
9896 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9900 char *refs_str = NULL;
9901 char const *r = NULL;
9903 err = build_refs_str(&refs_str, ci_refs, id,
9910 if (strncmp(r, wanted_ref,
9911 strlen(wanted_ref)) == 0) {
9923 if (wanted_ref == NULL || found) {
9925 err = got_ref_delete(re->ref, repo);
9928 printf("Deleted: ");
9929 err = print_commit_oneline(commit, id, repo,
9933 * Print paths modified by commit to help
9934 * associate commits with worktree changes.
9936 err = get_changed_paths(&paths, commit,
9941 err = print_commit(commit, id, repo, NULL,
9942 &paths, NULL, 0, 0, refs_idmap, NULL,
9944 got_pathlist_free(&paths,
9945 GOT_PATHLIST_FREE_ALL);
9947 if (worktree == NULL)
9948 printf("work tree: %.*s\n\n",
9949 GOT_WORKTREE_UUID_STRLEN, wt);
9955 got_object_commit_close(commit);
9961 if (wanted_ref != NULL && !found)
9962 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
9967 got_ref_list_free(&refs);
9968 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
9970 got_reflist_object_id_map_free(refs_idmap);
9972 got_object_commit_close(commit);
9977 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
9978 * identified by id for log messages to prepopulate the editor on commit.
9980 static const struct got_error *
9981 logmsg_ref(struct got_object_id *id, const char *prefix,
9982 struct got_worktree *worktree, struct got_repository *repo)
9984 const struct got_error *err = NULL;
9985 char *idstr, *ref = NULL, *refname = NULL;
9986 int histedit_in_progress;
9987 int rebase_in_progress, merge_in_progress;
9990 * Silenty refuse to create merge reference if any histedit, merge,
9991 * or rebase operation is in progress.
9993 err = got_worktree_histedit_in_progress(&histedit_in_progress,
9997 if (histedit_in_progress)
10000 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10003 if (rebase_in_progress)
10006 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10010 if (merge_in_progress)
10013 err = got_object_id_str(&idstr, id);
10017 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10021 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10022 err = got_error_from_errno("asprintf");
10026 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10036 usage_cherrypick(void)
10038 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10043 static const struct got_error *
10044 cmd_cherrypick(int argc, char *argv[])
10046 const struct got_error *error = NULL;
10047 struct got_worktree *worktree = NULL;
10048 struct got_repository *repo = NULL;
10049 char *cwd = NULL, *commit_id_str = NULL;
10050 struct got_object_id *commit_id = NULL;
10051 struct got_commit_object *commit = NULL;
10052 struct got_object_qid *pid;
10053 int ch, list_refs = 0, remove_refs = 0;
10054 struct got_update_progress_arg upa;
10055 int *pack_fds = NULL;
10057 while ((ch = getopt(argc, argv, "lX")) != -1) {
10066 usage_cherrypick();
10075 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10076 "unveil", NULL) == -1)
10079 if (list_refs || remove_refs) {
10080 if (argc != 0 && argc != 1)
10081 usage_cherrypick();
10082 } else if (argc != 1)
10083 usage_cherrypick();
10084 if (list_refs && remove_refs)
10085 option_conflict('l', 'X');
10087 cwd = getcwd(NULL, 0);
10089 error = got_error_from_errno("getcwd");
10093 error = got_repo_pack_fds_open(&pack_fds);
10097 error = got_worktree_open(&worktree, cwd);
10099 if (list_refs || remove_refs) {
10100 if (error->code != GOT_ERR_NOT_WORKTREE)
10103 if (error->code == GOT_ERR_NOT_WORKTREE)
10104 error = wrap_not_worktree_error(error,
10105 "cherrypick", cwd);
10110 error = got_repo_open(&repo,
10111 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10116 error = apply_unveil(got_repo_get_path(repo), 0,
10117 worktree ? got_worktree_get_root_path(worktree) : NULL);
10121 if (list_refs || remove_refs) {
10122 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10123 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10124 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10128 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10129 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10132 error = got_object_id_str(&commit_id_str, commit_id);
10136 error = got_object_open_as_commit(&commit, repo, commit_id);
10139 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10140 memset(&upa, 0, sizeof(upa));
10141 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10142 commit_id, repo, update_progress, &upa, check_cancelled,
10147 if (upa.did_something) {
10148 error = logmsg_ref(commit_id,
10149 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10152 printf("Merged commit %s\n", commit_id_str);
10154 print_merge_progress_stats(&upa);
10158 got_object_commit_close(commit);
10159 free(commit_id_str);
10161 got_worktree_close(worktree);
10163 const struct got_error *close_err = got_repo_close(repo);
10168 const struct got_error *pack_err =
10169 got_repo_pack_fds_close(pack_fds);
10178 usage_backout(void)
10180 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10184 static const struct got_error *
10185 cmd_backout(int argc, char *argv[])
10187 const struct got_error *error = NULL;
10188 struct got_worktree *worktree = NULL;
10189 struct got_repository *repo = NULL;
10190 char *cwd = NULL, *commit_id_str = NULL;
10191 struct got_object_id *commit_id = NULL;
10192 struct got_commit_object *commit = NULL;
10193 struct got_object_qid *pid;
10194 int ch, list_refs = 0, remove_refs = 0;
10195 struct got_update_progress_arg upa;
10196 int *pack_fds = NULL;
10198 while ((ch = getopt(argc, argv, "lX")) != -1) {
10216 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10217 "unveil", NULL) == -1)
10220 if (list_refs || remove_refs) {
10221 if (argc != 0 && argc != 1)
10223 } else if (argc != 1)
10225 if (list_refs && remove_refs)
10226 option_conflict('l', 'X');
10228 cwd = getcwd(NULL, 0);
10230 error = got_error_from_errno("getcwd");
10234 error = got_repo_pack_fds_open(&pack_fds);
10238 error = got_worktree_open(&worktree, cwd);
10240 if (list_refs || remove_refs) {
10241 if (error->code != GOT_ERR_NOT_WORKTREE)
10244 if (error->code == GOT_ERR_NOT_WORKTREE)
10245 error = wrap_not_worktree_error(error,
10251 error = got_repo_open(&repo,
10252 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10257 error = apply_unveil(got_repo_get_path(repo), 0,
10258 worktree ? got_worktree_get_root_path(worktree) : NULL);
10262 if (list_refs || remove_refs) {
10263 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10264 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10265 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10269 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10270 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10273 error = got_object_id_str(&commit_id_str, commit_id);
10277 error = got_object_open_as_commit(&commit, repo, commit_id);
10280 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10282 error = got_error(GOT_ERR_ROOT_COMMIT);
10286 memset(&upa, 0, sizeof(upa));
10287 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10288 repo, update_progress, &upa, check_cancelled, NULL);
10292 if (upa.did_something) {
10293 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10297 printf("Backed out commit %s\n", commit_id_str);
10299 print_merge_progress_stats(&upa);
10303 got_object_commit_close(commit);
10304 free(commit_id_str);
10306 got_worktree_close(worktree);
10308 const struct got_error *close_err = got_repo_close(repo);
10313 const struct got_error *pack_err =
10314 got_repo_pack_fds_close(pack_fds);
10324 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10329 trim_logmsg(char *logmsg, int limit)
10334 len = strlen(logmsg);
10337 logmsg[len] = '\0';
10338 nl = strchr(logmsg, '\n');
10343 static const struct got_error *
10344 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10346 const struct got_error *err;
10347 char *logmsg0 = NULL;
10350 err = got_object_commit_get_logmsg(&logmsg0, commit);
10355 while (isspace((unsigned char)s[0]))
10358 *logmsg = strdup(s);
10359 if (*logmsg == NULL) {
10360 err = got_error_from_errno("strdup");
10364 trim_logmsg(*logmsg, limit);
10370 static const struct got_error *
10371 show_rebase_merge_conflict(struct got_object_id *id,
10372 struct got_repository *repo)
10374 const struct got_error *err;
10375 struct got_commit_object *commit = NULL;
10376 char *id_str = NULL, *logmsg = NULL;
10378 err = got_object_open_as_commit(&commit, repo, id);
10382 err = got_object_id_str(&id_str, id);
10388 err = get_short_logmsg(&logmsg, 42, commit);
10392 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10395 got_object_commit_close(commit);
10400 static const struct got_error *
10401 show_rebase_progress(struct got_commit_object *commit,
10402 struct got_object_id *old_id, struct got_object_id *new_id)
10404 const struct got_error *err;
10405 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10407 err = got_object_id_str(&old_id_str, old_id);
10412 err = got_object_id_str(&new_id_str, new_id);
10417 old_id_str[12] = '\0';
10419 new_id_str[12] = '\0';
10421 err = get_short_logmsg(&logmsg, 42, commit);
10425 printf("%s -> %s: %s\n", old_id_str,
10426 new_id_str ? new_id_str : "no-op change", logmsg);
10434 static const struct got_error *
10435 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10436 struct got_reference *branch, struct got_reference *tmp_branch,
10437 struct got_repository *repo, int create_backup)
10439 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10440 return got_worktree_rebase_complete(worktree, fileindex,
10441 tmp_branch, branch, repo, create_backup);
10444 static const struct got_error *
10445 rebase_commit(struct got_pathlist_head *merged_paths,
10446 struct got_worktree *worktree, struct got_fileindex *fileindex,
10447 struct got_reference *tmp_branch, const char *committer,
10448 struct got_object_id *commit_id, struct got_repository *repo)
10450 const struct got_error *error;
10451 struct got_commit_object *commit;
10452 struct got_object_id *new_commit_id;
10454 error = got_object_open_as_commit(&commit, repo, commit_id);
10458 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10459 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10462 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10464 error = show_rebase_progress(commit, commit_id, NULL);
10466 error = show_rebase_progress(commit, commit_id, new_commit_id);
10467 free(new_commit_id);
10470 got_object_commit_close(commit);
10474 struct check_path_prefix_arg {
10475 const char *path_prefix;
10480 static const struct got_error *
10481 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10482 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10483 struct got_object_id *id1, struct got_object_id *id2,
10484 const char *path1, const char *path2,
10485 mode_t mode1, mode_t mode2, struct got_repository *repo)
10487 struct check_path_prefix_arg *a = arg;
10489 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10490 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10491 return got_error(a->errcode);
10496 static const struct got_error *
10497 check_path_prefix(struct got_object_id *parent_id,
10498 struct got_object_id *commit_id, const char *path_prefix,
10499 int errcode, struct got_repository *repo)
10501 const struct got_error *err;
10502 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10503 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10504 struct check_path_prefix_arg cpp_arg;
10506 if (got_path_is_root_dir(path_prefix))
10509 err = got_object_open_as_commit(&commit, repo, commit_id);
10513 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10517 err = got_object_open_as_tree(&tree1, repo,
10518 got_object_commit_get_tree_id(parent_commit));
10522 err = got_object_open_as_tree(&tree2, repo,
10523 got_object_commit_get_tree_id(commit));
10527 cpp_arg.path_prefix = path_prefix;
10528 while (cpp_arg.path_prefix[0] == '/')
10529 cpp_arg.path_prefix++;
10530 cpp_arg.len = strlen(cpp_arg.path_prefix);
10531 cpp_arg.errcode = errcode;
10532 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10533 check_path_prefix_in_diff, &cpp_arg, 0);
10536 got_object_tree_close(tree1);
10538 got_object_tree_close(tree2);
10540 got_object_commit_close(commit);
10542 got_object_commit_close(parent_commit);
10546 static const struct got_error *
10547 collect_commits(struct got_object_id_queue *commits,
10548 struct got_object_id *initial_commit_id,
10549 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10550 const char *path_prefix, int path_prefix_errcode,
10551 struct got_repository *repo)
10553 const struct got_error *err = NULL;
10554 struct got_commit_graph *graph = NULL;
10555 struct got_object_id parent_id, commit_id;
10556 struct got_object_qid *qid;
10558 err = got_commit_graph_open(&graph, "/", 1);
10562 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10563 check_cancelled, NULL);
10567 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10568 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10569 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10570 check_cancelled, NULL);
10572 if (err->code == GOT_ERR_ITER_COMPLETED) {
10573 err = got_error_msg(GOT_ERR_ANCESTRY,
10574 "ran out of commits to rebase before "
10575 "youngest common ancestor commit has "
10576 "been reached?!?");
10580 err = check_path_prefix(&parent_id, &commit_id,
10581 path_prefix, path_prefix_errcode, repo);
10585 err = got_object_qid_alloc(&qid, &commit_id);
10588 STAILQ_INSERT_HEAD(commits, qid, entry);
10590 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10594 got_commit_graph_close(graph);
10598 static const struct got_error *
10599 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10601 const struct got_error *err = NULL;
10602 time_t committer_time;
10604 char datebuf[11]; /* YYYY-MM-DD + NUL */
10605 char *author0 = NULL, *author, *smallerthan;
10606 char *logmsg0 = NULL, *logmsg, *newline;
10608 committer_time = got_object_commit_get_committer_time(commit);
10609 if (gmtime_r(&committer_time, &tm) == NULL)
10610 return got_error_from_errno("gmtime_r");
10611 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10612 return got_error(GOT_ERR_NO_SPACE);
10614 author0 = strdup(got_object_commit_get_author(commit));
10615 if (author0 == NULL)
10616 return got_error_from_errno("strdup");
10618 smallerthan = strchr(author, '<');
10619 if (smallerthan && smallerthan[1] != '\0')
10620 author = smallerthan + 1;
10621 author[strcspn(author, "@>")] = '\0';
10623 err = got_object_commit_get_logmsg(&logmsg0, commit);
10627 while (*logmsg == '\n')
10629 newline = strchr(logmsg, '\n');
10633 if (asprintf(brief_str, "%s %s %s",
10634 datebuf, author, logmsg) == -1)
10635 err = got_error_from_errno("asprintf");
10642 static const struct got_error *
10643 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10644 struct got_repository *repo)
10646 const struct got_error *err;
10649 err = got_object_id_str(&id_str, id);
10653 err = got_ref_delete(ref, repo);
10657 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10663 static const struct got_error *
10664 print_backup_ref(const char *branch_name, const char *new_id_str,
10665 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10666 struct got_reflist_object_id_map *refs_idmap,
10667 struct got_repository *repo)
10669 const struct got_error *err = NULL;
10670 struct got_reflist_head *refs;
10671 char *refs_str = NULL;
10672 struct got_object_id *new_commit_id = NULL;
10673 struct got_commit_object *new_commit = NULL;
10674 char *new_commit_brief_str = NULL;
10675 struct got_object_id *yca_id = NULL;
10676 struct got_commit_object *yca_commit = NULL;
10677 char *yca_id_str = NULL, *yca_brief_str = NULL;
10678 char *custom_refs_str;
10680 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10681 return got_error_from_errno("asprintf");
10683 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10684 0, 0, refs_idmap, custom_refs_str, NULL);
10688 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10692 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10694 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10699 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10703 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10707 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10708 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10712 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10713 refs_str ? " (" : "", refs_str ? refs_str : "",
10714 refs_str ? ")" : "", new_commit_brief_str);
10715 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10716 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10720 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10724 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10728 err = got_object_id_str(&yca_id_str, yca_id);
10732 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10734 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10738 printf("history forked at %s%s%s%s\n %s\n",
10740 refs_str ? " (" : "", refs_str ? refs_str : "",
10741 refs_str ? ")" : "", yca_brief_str);
10744 free(custom_refs_str);
10745 free(new_commit_id);
10749 free(yca_brief_str);
10751 got_object_commit_close(new_commit);
10753 got_object_commit_close(yca_commit);
10758 static const struct got_error *
10759 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10760 struct got_repository *repo)
10762 const struct got_error *err;
10763 struct got_reflist_head refs;
10764 struct got_reflist_entry *re;
10765 char *uuidstr = NULL;
10766 static char msg[160];
10770 err = got_worktree_get_uuid(&uuidstr, worktree);
10774 err = got_ref_list(&refs, repo, "refs/got/worktree",
10775 got_ref_cmp_by_name, repo);
10779 TAILQ_FOREACH(re, &refs, entry) {
10780 const char *cmd, *refname, *type;
10782 refname = got_ref_get_name(re->ref);
10784 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10785 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10786 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10787 cmd = "cherrypick";
10788 type = "cherrypicked";
10789 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10790 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10791 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10793 type = "backed-out";
10797 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10800 snprintf(msg, sizeof(msg),
10801 "work tree has references created by %s commits which "
10802 "must be removed with 'got %s -X' before running the %s "
10803 "command", type, cmd, caller);
10804 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10810 got_ref_list_free(&refs);
10814 static const struct got_error *
10815 process_backup_refs(const char *backup_ref_prefix,
10816 const char *wanted_branch_name,
10817 int delete, struct got_repository *repo)
10819 const struct got_error *err;
10820 struct got_reflist_head refs, backup_refs;
10821 struct got_reflist_entry *re;
10822 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10823 struct got_object_id *old_commit_id = NULL;
10824 char *branch_name = NULL;
10825 struct got_commit_object *old_commit = NULL;
10826 struct got_reflist_object_id_map *refs_idmap = NULL;
10827 int wanted_branch_found = 0;
10830 TAILQ_INIT(&backup_refs);
10832 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10836 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10840 if (wanted_branch_name) {
10841 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10842 wanted_branch_name += 11;
10845 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10846 got_ref_cmp_by_commit_timestamp_descending, repo);
10850 TAILQ_FOREACH(re, &backup_refs, entry) {
10851 const char *refname = got_ref_get_name(re->ref);
10854 err = check_cancelled(NULL);
10858 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10862 err = got_object_open_as_commit(&old_commit, repo,
10867 if (strncmp(backup_ref_prefix, refname,
10868 backup_ref_prefix_len) == 0)
10869 refname += backup_ref_prefix_len;
10871 while (refname[0] == '/')
10874 branch_name = strdup(refname);
10875 if (branch_name == NULL) {
10876 err = got_error_from_errno("strdup");
10879 slash = strrchr(branch_name, '/');
10882 refname += strlen(branch_name) + 1;
10885 if (wanted_branch_name == NULL ||
10886 strcmp(wanted_branch_name, branch_name) == 0) {
10887 wanted_branch_found = 1;
10889 err = delete_backup_ref(re->ref,
10890 old_commit_id, repo);
10892 err = print_backup_ref(branch_name, refname,
10893 old_commit_id, old_commit, refs_idmap,
10900 free(old_commit_id);
10901 old_commit_id = NULL;
10903 branch_name = NULL;
10904 got_object_commit_close(old_commit);
10908 if (wanted_branch_name && !wanted_branch_found) {
10909 err = got_error_fmt(GOT_ERR_NOT_REF,
10910 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10914 got_reflist_object_id_map_free(refs_idmap);
10915 got_ref_list_free(&refs);
10916 got_ref_list_free(&backup_refs);
10917 free(old_commit_id);
10920 got_object_commit_close(old_commit);
10924 static const struct got_error *
10925 abort_progress(void *arg, unsigned char status, const char *path)
10928 * Unversioned files should not clutter progress output when
10929 * an operation is aborted.
10931 if (status == GOT_STATUS_UNVERSIONED)
10934 return update_progress(arg, status, path);
10937 static const struct got_error *
10938 cmd_rebase(int argc, char *argv[])
10940 const struct got_error *error = NULL;
10941 struct got_worktree *worktree = NULL;
10942 struct got_repository *repo = NULL;
10943 struct got_fileindex *fileindex = NULL;
10944 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10945 struct got_reference *branch = NULL;
10946 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10947 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10948 struct got_object_id *resume_commit_id = NULL;
10949 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10950 struct got_object_id *head_commit_id = NULL;
10951 struct got_reference *head_ref = NULL;
10952 struct got_commit_object *commit = NULL;
10953 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10954 int histedit_in_progress = 0, merge_in_progress = 0;
10955 int create_backup = 1, list_backups = 0, delete_backups = 0;
10956 struct got_object_id_queue commits;
10957 struct got_pathlist_head merged_paths;
10958 const struct got_object_id_queue *parent_ids;
10959 struct got_object_qid *qid, *pid;
10960 struct got_update_progress_arg upa;
10961 int *pack_fds = NULL;
10963 STAILQ_INIT(&commits);
10964 TAILQ_INIT(&merged_paths);
10965 memset(&upa, 0, sizeof(upa));
10967 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10973 continue_rebase = 1;
10979 delete_backups = 1;
10991 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10992 "unveil", NULL) == -1)
10995 if (list_backups) {
10997 option_conflict('l', 'a');
10998 if (continue_rebase)
10999 option_conflict('l', 'c');
11000 if (delete_backups)
11001 option_conflict('l', 'X');
11002 if (argc != 0 && argc != 1)
11004 } else if (delete_backups) {
11006 option_conflict('X', 'a');
11007 if (continue_rebase)
11008 option_conflict('X', 'c');
11010 option_conflict('l', 'X');
11011 if (argc != 0 && argc != 1)
11014 if (abort_rebase && continue_rebase)
11016 else if (abort_rebase || continue_rebase) {
11019 } else if (argc != 1)
11023 cwd = getcwd(NULL, 0);
11025 error = got_error_from_errno("getcwd");
11029 error = got_repo_pack_fds_open(&pack_fds);
11033 error = got_worktree_open(&worktree, cwd);
11035 if (list_backups || delete_backups) {
11036 if (error->code != GOT_ERR_NOT_WORKTREE)
11039 if (error->code == GOT_ERR_NOT_WORKTREE)
11040 error = wrap_not_worktree_error(error,
11046 error = get_gitconfig_path(&gitconfig_path);
11049 error = got_repo_open(&repo,
11050 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11051 gitconfig_path, pack_fds);
11055 if (worktree != NULL && !list_backups && !delete_backups) {
11056 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11061 error = get_author(&committer, repo, worktree);
11062 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11065 error = apply_unveil(got_repo_get_path(repo), 0,
11066 worktree ? got_worktree_get_root_path(worktree) : NULL);
11070 if (list_backups || delete_backups) {
11071 error = process_backup_refs(
11072 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11073 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11074 goto done; /* nothing else to do */
11077 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11081 if (histedit_in_progress) {
11082 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11086 error = got_worktree_merge_in_progress(&merge_in_progress,
11090 if (merge_in_progress) {
11091 error = got_error(GOT_ERR_MERGE_BUSY);
11095 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11099 if (abort_rebase) {
11100 if (!rebase_in_progress) {
11101 error = got_error(GOT_ERR_NOT_REBASING);
11104 error = got_worktree_rebase_continue(&resume_commit_id,
11105 &new_base_branch, &tmp_branch, &branch, &fileindex,
11109 printf("Switching work tree to %s\n",
11110 got_ref_get_symref_target(new_base_branch));
11111 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11112 new_base_branch, abort_progress, &upa);
11115 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11116 print_merge_progress_stats(&upa);
11117 goto done; /* nothing else to do */
11120 if (continue_rebase) {
11121 if (!rebase_in_progress) {
11122 error = got_error(GOT_ERR_NOT_REBASING);
11125 error = got_worktree_rebase_continue(&resume_commit_id,
11126 &new_base_branch, &tmp_branch, &branch, &fileindex,
11131 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11132 committer, resume_commit_id, repo);
11136 yca_id = got_object_id_dup(resume_commit_id);
11137 if (yca_id == NULL) {
11138 error = got_error_from_errno("got_object_id_dup");
11142 error = got_ref_open(&branch, repo, argv[0], 0);
11145 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11146 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11147 "will not rebase a branch which lives outside "
11148 "the \"refs/heads/\" reference namespace");
11153 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11157 if (!continue_rebase) {
11158 struct got_object_id *base_commit_id;
11160 error = got_ref_open(&head_ref, repo,
11161 got_worktree_get_head_ref_name(worktree), 0);
11164 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11167 base_commit_id = got_worktree_get_base_commit_id(worktree);
11168 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11169 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11173 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11174 base_commit_id, branch_head_commit_id, 1, repo,
11175 check_cancelled, NULL);
11177 if (error->code == GOT_ERR_ANCESTRY) {
11178 error = got_error_msg(GOT_ERR_ANCESTRY,
11179 "specified branch shares no common "
11180 "ancestry with work tree's branch");
11185 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11187 if (error->code != GOT_ERR_ANCESTRY)
11191 struct got_pathlist_head paths;
11192 printf("%s is already based on %s\n",
11193 got_ref_get_name(branch),
11194 got_worktree_get_head_ref_name(worktree));
11195 error = switch_head_ref(branch, branch_head_commit_id,
11199 error = got_worktree_set_base_commit_id(worktree, repo,
11200 branch_head_commit_id);
11203 TAILQ_INIT(&paths);
11204 error = got_pathlist_append(&paths, "", NULL);
11207 error = got_worktree_checkout_files(worktree,
11208 &paths, repo, update_progress, &upa,
11209 check_cancelled, NULL);
11210 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11213 if (upa.did_something) {
11215 error = got_object_id_str(&id_str,
11216 branch_head_commit_id);
11219 printf("Updated to %s: %s\n",
11220 got_worktree_get_head_ref_name(worktree),
11224 printf("Already up-to-date\n");
11225 print_update_progress_stats(&upa);
11230 commit_id = branch_head_commit_id;
11231 error = got_object_open_as_commit(&commit, repo, commit_id);
11235 parent_ids = got_object_commit_get_parent_ids(commit);
11236 pid = STAILQ_FIRST(parent_ids);
11238 error = collect_commits(&commits, commit_id, &pid->id,
11239 yca_id, got_worktree_get_path_prefix(worktree),
11240 GOT_ERR_REBASE_PATH, repo);
11245 got_object_commit_close(commit);
11248 if (!continue_rebase) {
11249 error = got_worktree_rebase_prepare(&new_base_branch,
11250 &tmp_branch, &fileindex, worktree, branch, repo);
11255 if (STAILQ_EMPTY(&commits)) {
11256 if (continue_rebase) {
11257 error = rebase_complete(worktree, fileindex,
11258 branch, tmp_branch, repo, create_backup);
11261 /* Fast-forward the reference of the branch. */
11262 struct got_object_id *new_head_commit_id;
11264 error = got_ref_resolve(&new_head_commit_id, repo,
11268 error = got_object_id_str(&id_str, new_head_commit_id);
11271 printf("Forwarding %s to commit %s\n",
11272 got_ref_get_name(branch), id_str);
11274 error = got_ref_change_ref(branch,
11275 new_head_commit_id);
11278 /* No backup needed since objects did not change. */
11284 STAILQ_FOREACH(qid, &commits, entry) {
11286 commit_id = &qid->id;
11287 parent_id = pid ? &pid->id : yca_id;
11290 memset(&upa, 0, sizeof(upa));
11291 error = got_worktree_rebase_merge_files(&merged_paths,
11292 worktree, fileindex, parent_id, commit_id, repo,
11293 update_progress, &upa, check_cancelled, NULL);
11297 print_merge_progress_stats(&upa);
11298 if (upa.conflicts > 0 || upa.missing > 0 ||
11299 upa.not_deleted > 0 || upa.unversioned > 0) {
11300 if (upa.conflicts > 0) {
11301 error = show_rebase_merge_conflict(&qid->id,
11306 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11310 error = rebase_commit(&merged_paths, worktree, fileindex,
11311 tmp_branch, committer, commit_id, repo);
11312 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11317 if (upa.conflicts > 0 || upa.missing > 0 ||
11318 upa.not_deleted > 0 || upa.unversioned > 0) {
11319 error = got_worktree_rebase_postpone(worktree, fileindex);
11322 if (upa.conflicts > 0 && upa.missing == 0 &&
11323 upa.not_deleted == 0 && upa.unversioned == 0) {
11324 error = got_error_msg(GOT_ERR_CONFLICTS,
11325 "conflicts must be resolved before rebasing "
11327 } else if (upa.conflicts > 0) {
11328 error = got_error_msg(GOT_ERR_CONFLICTS,
11329 "conflicts must be resolved before rebasing "
11330 "can continue; changes destined for some "
11331 "files were not yet merged and should be "
11332 "merged manually if required before the "
11333 "rebase operation is continued");
11335 error = got_error_msg(GOT_ERR_CONFLICTS,
11336 "changes destined for some files were not "
11337 "yet merged and should be merged manually "
11338 "if required before the rebase operation "
11342 error = rebase_complete(worktree, fileindex, branch,
11343 tmp_branch, repo, create_backup);
11347 free(gitconfig_path);
11348 got_object_id_queue_free(&commits);
11349 free(branch_head_commit_id);
11350 free(resume_commit_id);
11351 free(head_commit_id);
11354 got_object_commit_close(commit);
11356 got_ref_close(branch);
11357 if (new_base_branch)
11358 got_ref_close(new_base_branch);
11360 got_ref_close(tmp_branch);
11362 got_ref_close(head_ref);
11364 got_worktree_close(worktree);
11366 const struct got_error *close_err = got_repo_close(repo);
11371 const struct got_error *pack_err =
11372 got_repo_pack_fds_close(pack_fds);
11380 usage_histedit(void)
11382 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11383 "[branch]\n", getprogname());
11387 #define GOT_HISTEDIT_PICK 'p'
11388 #define GOT_HISTEDIT_EDIT 'e'
11389 #define GOT_HISTEDIT_FOLD 'f'
11390 #define GOT_HISTEDIT_DROP 'd'
11391 #define GOT_HISTEDIT_MESG 'm'
11393 static const struct got_histedit_cmd {
11394 unsigned char code;
11397 } got_histedit_cmds[] = {
11398 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11399 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11400 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11402 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11403 { GOT_HISTEDIT_MESG, "mesg",
11404 "single-line log message for commit above (open editor if empty)" },
11407 struct got_histedit_list_entry {
11408 TAILQ_ENTRY(got_histedit_list_entry) entry;
11409 struct got_object_id *commit_id;
11410 const struct got_histedit_cmd *cmd;
11413 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11415 static const struct got_error *
11416 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11417 FILE *f, struct got_repository *repo)
11419 const struct got_error *err = NULL;
11420 char *logmsg = NULL, *id_str = NULL;
11421 struct got_commit_object *commit = NULL;
11424 err = got_object_open_as_commit(&commit, repo, commit_id);
11428 err = get_short_logmsg(&logmsg, 34, commit);
11432 err = got_object_id_str(&id_str, commit_id);
11436 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11438 err = got_ferror(f, GOT_ERR_IO);
11441 got_object_commit_close(commit);
11447 static const struct got_error *
11448 histedit_write_commit_list(struct got_object_id_queue *commits,
11449 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11450 int edit_only, struct got_repository *repo)
11452 const struct got_error *err = NULL;
11453 struct got_object_qid *qid;
11454 const char *histedit_cmd = NULL;
11456 if (STAILQ_EMPTY(commits))
11457 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11459 STAILQ_FOREACH(qid, commits, entry) {
11460 histedit_cmd = got_histedit_cmds[0].name;
11462 histedit_cmd = "drop";
11463 else if (edit_only)
11464 histedit_cmd = "edit";
11465 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11466 histedit_cmd = "fold";
11467 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11470 if (edit_logmsg_only) {
11471 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11473 err = got_ferror(f, GOT_ERR_IO);
11482 static const struct got_error *
11483 write_cmd_list(FILE *f, const char *branch_name,
11484 struct got_object_id_queue *commits)
11486 const struct got_error *err = NULL;
11490 struct got_object_qid *qid;
11492 qid = STAILQ_FIRST(commits);
11493 err = got_object_id_str(&id_str, &qid->id);
11498 "# Editing the history of branch '%s' starting at\n"
11500 "# Commits will be processed in order from top to "
11501 "bottom of this file.\n", branch_name, id_str);
11503 err = got_ferror(f, GOT_ERR_IO);
11507 n = fprintf(f, "# Available histedit commands:\n");
11509 err = got_ferror(f, GOT_ERR_IO);
11513 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11514 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11515 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11518 err = got_ferror(f, GOT_ERR_IO);
11527 static const struct got_error *
11528 histedit_syntax_error(int lineno)
11530 static char msg[42];
11533 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11535 if (ret < 0 || (size_t)ret >= sizeof(msg))
11536 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11538 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11541 static const struct got_error *
11542 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11543 char *logmsg, struct got_repository *repo)
11545 const struct got_error *err;
11546 struct got_commit_object *folded_commit = NULL;
11547 char *id_str, *folded_logmsg = NULL;
11549 err = got_object_id_str(&id_str, hle->commit_id);
11553 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11557 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11560 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11561 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11562 folded_logmsg) == -1) {
11563 err = got_error_from_errno("asprintf");
11567 got_object_commit_close(folded_commit);
11569 free(folded_logmsg);
11573 static struct got_histedit_list_entry *
11574 get_folded_commits(struct got_histedit_list_entry *hle)
11576 struct got_histedit_list_entry *prev, *folded = NULL;
11578 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11579 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11580 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11581 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11583 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11589 static const struct got_error *
11590 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11591 struct got_repository *repo)
11593 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11594 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11595 const struct got_error *err = NULL;
11596 struct got_commit_object *commit = NULL;
11599 struct got_histedit_list_entry *folded = NULL;
11601 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11605 folded = get_folded_commits(hle);
11607 while (folded != hle) {
11608 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11609 folded = TAILQ_NEXT(folded, entry);
11612 err = append_folded_commit_msg(&new_msg, folded,
11618 folded = TAILQ_NEXT(folded, entry);
11622 err = got_object_id_str(&id_str, hle->commit_id);
11625 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11628 logmsg_len = asprintf(&new_msg,
11629 "%s\n# original log message of commit %s: %s",
11630 logmsg ? logmsg : "", id_str, orig_logmsg);
11631 if (logmsg_len == -1) {
11632 err = got_error_from_errno("asprintf");
11638 err = got_object_id_str(&id_str, hle->commit_id);
11642 err = got_opentemp_named_fd(&logmsg_path, &fd,
11643 GOT_TMPDIR_STR "/got-logmsg", "");
11647 write(fd, logmsg, logmsg_len);
11650 err = get_editor(&editor);
11654 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11657 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11660 hle->logmsg = strdup(new_msg);
11661 if (hle->logmsg == NULL)
11662 err = got_error_from_errno("strdup");
11665 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11666 err = got_error_from_errno2("unlink", logmsg_path);
11672 got_object_commit_close(commit);
11676 static const struct got_error *
11677 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11678 FILE *f, struct got_repository *repo)
11680 const struct got_error *err = NULL;
11681 char *line = NULL, *p, *end;
11684 int lineno = 0, lastcmd = -1;
11685 const struct got_histedit_cmd *cmd;
11686 struct got_object_id *commit_id = NULL;
11687 struct got_histedit_list_entry *hle = NULL;
11690 len = getline(&line, &size, f);
11692 const struct got_error *getline_err;
11695 getline_err = got_error_from_errno("getline");
11696 err = got_ferror(f, getline_err->code);
11701 while (isspace((unsigned char)p[0]))
11703 if (p[0] == '#' || p[0] == '\0') {
11709 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11710 cmd = &got_histedit_cmds[i];
11711 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11712 isspace((unsigned char)p[strlen(cmd->name)])) {
11713 p += strlen(cmd->name);
11716 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11721 if (i == nitems(got_histedit_cmds)) {
11722 err = histedit_syntax_error(lineno);
11725 while (isspace((unsigned char)p[0]))
11727 if (cmd->code == GOT_HISTEDIT_MESG) {
11728 if (lastcmd != GOT_HISTEDIT_PICK &&
11729 lastcmd != GOT_HISTEDIT_EDIT) {
11730 err = got_error(GOT_ERR_HISTEDIT_CMD);
11733 if (p[0] == '\0') {
11734 err = histedit_edit_logmsg(hle, repo);
11738 hle->logmsg = strdup(p);
11739 if (hle->logmsg == NULL) {
11740 err = got_error_from_errno("strdup");
11746 lastcmd = cmd->code;
11750 while (end[0] && !isspace((unsigned char)end[0]))
11754 err = got_object_resolve_id_str(&commit_id, repo, p);
11756 /* override error code */
11757 err = histedit_syntax_error(lineno);
11761 hle = malloc(sizeof(*hle));
11763 err = got_error_from_errno("malloc");
11767 hle->commit_id = commit_id;
11768 hle->logmsg = NULL;
11772 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11773 lastcmd = cmd->code;
11781 static const struct got_error *
11782 histedit_check_script(struct got_histedit_list *histedit_cmds,
11783 struct got_object_id_queue *commits, struct got_repository *repo)
11785 const struct got_error *err = NULL;
11786 struct got_object_qid *qid;
11787 struct got_histedit_list_entry *hle;
11788 static char msg[92];
11791 if (TAILQ_EMPTY(histedit_cmds))
11792 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11793 "histedit script contains no commands");
11794 if (STAILQ_EMPTY(commits))
11795 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11797 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11798 struct got_histedit_list_entry *hle2;
11799 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11802 if (got_object_id_cmp(hle->commit_id,
11803 hle2->commit_id) != 0)
11805 err = got_object_id_str(&id_str, hle->commit_id);
11808 snprintf(msg, sizeof(msg), "commit %s is listed "
11809 "more than once in histedit script", id_str);
11811 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11815 STAILQ_FOREACH(qid, commits, entry) {
11816 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11817 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11821 err = got_object_id_str(&id_str, &qid->id);
11824 snprintf(msg, sizeof(msg),
11825 "commit %s missing from histedit script", id_str);
11827 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11831 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11832 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11833 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11834 "last commit in histedit script cannot be folded");
11839 static const struct got_error *
11840 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11841 const char *path, struct got_object_id_queue *commits,
11842 struct got_repository *repo)
11844 const struct got_error *err = NULL;
11848 err = get_editor(&editor);
11852 if (spawn_editor(editor, path) == -1) {
11853 err = got_error_from_errno("failed spawning editor");
11857 f = fopen(path, "re");
11859 err = got_error_from_errno("fopen");
11862 err = histedit_parse_list(histedit_cmds, f, repo);
11866 err = histedit_check_script(histedit_cmds, commits, repo);
11868 if (f && fclose(f) == EOF && err == NULL)
11869 err = got_error_from_errno("fclose");
11874 static const struct got_error *
11875 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11876 struct got_object_id_queue *, const char *, const char *,
11877 struct got_repository *);
11879 static const struct got_error *
11880 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11881 struct got_object_id_queue *commits, const char *branch_name,
11882 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11883 struct got_repository *repo)
11885 const struct got_error *err;
11889 err = got_opentemp_named(&path, &f, "got-histedit", "");
11893 err = write_cmd_list(f, branch_name, commits);
11897 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11898 fold_only, drop_only, edit_only, repo);
11902 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11904 err = histedit_parse_list(histedit_cmds, f, repo);
11906 if (fclose(f) == EOF) {
11907 err = got_error_from_errno("fclose");
11911 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11913 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11914 err->code != GOT_ERR_HISTEDIT_CMD)
11916 err = histedit_edit_list_retry(histedit_cmds, err,
11917 commits, path, branch_name, repo);
11921 if (f && fclose(f) == EOF && err == NULL)
11922 err = got_error_from_errno("fclose");
11923 if (path && unlink(path) != 0 && err == NULL)
11924 err = got_error_from_errno2("unlink", path);
11929 static const struct got_error *
11930 histedit_save_list(struct got_histedit_list *histedit_cmds,
11931 struct got_worktree *worktree, struct got_repository *repo)
11933 const struct got_error *err = NULL;
11936 struct got_histedit_list_entry *hle;
11937 struct got_commit_object *commit = NULL;
11939 err = got_worktree_get_histedit_script_path(&path, worktree);
11943 f = fopen(path, "we");
11945 err = got_error_from_errno2("fopen", path);
11948 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11949 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11955 int n = fprintf(f, "%c %s\n",
11956 GOT_HISTEDIT_MESG, hle->logmsg);
11958 err = got_ferror(f, GOT_ERR_IO);
11964 if (f && fclose(f) == EOF && err == NULL)
11965 err = got_error_from_errno("fclose");
11968 got_object_commit_close(commit);
11973 histedit_free_list(struct got_histedit_list *histedit_cmds)
11975 struct got_histedit_list_entry *hle;
11977 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11978 TAILQ_REMOVE(histedit_cmds, hle, entry);
11983 static const struct got_error *
11984 histedit_load_list(struct got_histedit_list *histedit_cmds,
11985 const char *path, struct got_repository *repo)
11987 const struct got_error *err = NULL;
11990 f = fopen(path, "re");
11992 err = got_error_from_errno2("fopen", path);
11996 err = histedit_parse_list(histedit_cmds, f, repo);
11998 if (f && fclose(f) == EOF && err == NULL)
11999 err = got_error_from_errno("fclose");
12003 static const struct got_error *
12004 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12005 const struct got_error *edit_err, struct got_object_id_queue *commits,
12006 const char *path, const char *branch_name, struct got_repository *repo)
12008 const struct got_error *err = NULL, *prev_err = edit_err;
12011 while (resp != 'c' && resp != 'r' && resp != 'a') {
12012 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12013 "or (a)bort: ", getprogname(), prev_err->msg);
12018 histedit_free_list(histedit_cmds);
12019 err = histedit_run_editor(histedit_cmds, path, commits,
12022 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12023 err->code != GOT_ERR_HISTEDIT_CMD)
12030 } else if (resp == 'r') {
12031 histedit_free_list(histedit_cmds);
12032 err = histedit_edit_script(histedit_cmds,
12033 commits, branch_name, 0, 0, 0, 0, repo);
12035 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12036 err->code != GOT_ERR_HISTEDIT_CMD)
12043 } else if (resp == 'a') {
12044 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12047 printf("invalid response '%c'\n", resp);
12053 static const struct got_error *
12054 histedit_complete(struct got_worktree *worktree,
12055 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12056 struct got_reference *branch, struct got_repository *repo)
12058 printf("Switching work tree to %s\n",
12059 got_ref_get_symref_target(branch));
12060 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12064 static const struct got_error *
12065 show_histedit_progress(struct got_commit_object *commit,
12066 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12068 const struct got_error *err;
12069 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12071 err = got_object_id_str(&old_id_str, hle->commit_id);
12076 err = got_object_id_str(&new_id_str, new_id);
12081 old_id_str[12] = '\0';
12083 new_id_str[12] = '\0';
12086 logmsg = strdup(hle->logmsg);
12087 if (logmsg == NULL) {
12088 err = got_error_from_errno("strdup");
12091 trim_logmsg(logmsg, 42);
12093 err = get_short_logmsg(&logmsg, 42, commit);
12098 switch (hle->cmd->code) {
12099 case GOT_HISTEDIT_PICK:
12100 case GOT_HISTEDIT_EDIT:
12101 printf("%s -> %s: %s\n", old_id_str,
12102 new_id_str ? new_id_str : "no-op change", logmsg);
12104 case GOT_HISTEDIT_DROP:
12105 case GOT_HISTEDIT_FOLD:
12106 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12118 static const struct got_error *
12119 histedit_commit(struct got_pathlist_head *merged_paths,
12120 struct got_worktree *worktree, struct got_fileindex *fileindex,
12121 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12122 const char *committer, struct got_repository *repo)
12124 const struct got_error *err;
12125 struct got_commit_object *commit;
12126 struct got_object_id *new_commit_id;
12128 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12129 && hle->logmsg == NULL) {
12130 err = histedit_edit_logmsg(hle, repo);
12135 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12139 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12140 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12141 hle->logmsg, repo);
12143 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12145 err = show_histedit_progress(commit, hle, NULL);
12147 err = show_histedit_progress(commit, hle, new_commit_id);
12148 free(new_commit_id);
12151 got_object_commit_close(commit);
12155 static const struct got_error *
12156 histedit_skip_commit(struct got_histedit_list_entry *hle,
12157 struct got_worktree *worktree, struct got_repository *repo)
12159 const struct got_error *error;
12160 struct got_commit_object *commit;
12162 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12167 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12171 error = show_histedit_progress(commit, hle, NULL);
12172 got_object_commit_close(commit);
12176 static const struct got_error *
12177 check_local_changes(void *arg, unsigned char status,
12178 unsigned char staged_status, const char *path,
12179 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12180 struct got_object_id *commit_id, int dirfd, const char *de_name)
12182 int *have_local_changes = arg;
12185 case GOT_STATUS_ADD:
12186 case GOT_STATUS_DELETE:
12187 case GOT_STATUS_MODIFY:
12188 case GOT_STATUS_CONFLICT:
12189 *have_local_changes = 1;
12190 return got_error(GOT_ERR_CANCELLED);
12195 switch (staged_status) {
12196 case GOT_STATUS_ADD:
12197 case GOT_STATUS_DELETE:
12198 case GOT_STATUS_MODIFY:
12199 *have_local_changes = 1;
12200 return got_error(GOT_ERR_CANCELLED);
12208 static const struct got_error *
12209 cmd_histedit(int argc, char *argv[])
12211 const struct got_error *error = NULL;
12212 struct got_worktree *worktree = NULL;
12213 struct got_fileindex *fileindex = NULL;
12214 struct got_repository *repo = NULL;
12215 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12216 struct got_reference *branch = NULL;
12217 struct got_reference *tmp_branch = NULL;
12218 struct got_object_id *resume_commit_id = NULL;
12219 struct got_object_id *base_commit_id = NULL;
12220 struct got_object_id *head_commit_id = NULL;
12221 struct got_commit_object *commit = NULL;
12222 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12223 struct got_update_progress_arg upa;
12224 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12225 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12226 int list_backups = 0, delete_backups = 0;
12227 const char *edit_script_path = NULL;
12228 struct got_object_id_queue commits;
12229 struct got_pathlist_head merged_paths;
12230 const struct got_object_id_queue *parent_ids;
12231 struct got_object_qid *pid;
12232 struct got_histedit_list histedit_cmds;
12233 struct got_histedit_list_entry *hle;
12234 int *pack_fds = NULL;
12236 STAILQ_INIT(&commits);
12237 TAILQ_INIT(&histedit_cmds);
12238 TAILQ_INIT(&merged_paths);
12239 memset(&upa, 0, sizeof(upa));
12241 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12256 edit_script_path = optarg;
12265 edit_logmsg_only = 1;
12268 delete_backups = 1;
12280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12281 "unveil", NULL) == -1)
12284 if (abort_edit && continue_edit)
12285 option_conflict('a', 'c');
12286 if (edit_script_path && edit_logmsg_only)
12287 option_conflict('F', 'm');
12288 if (abort_edit && edit_logmsg_only)
12289 option_conflict('a', 'm');
12290 if (continue_edit && edit_logmsg_only)
12291 option_conflict('c', 'm');
12292 if (abort_edit && fold_only)
12293 option_conflict('a', 'f');
12294 if (continue_edit && fold_only)
12295 option_conflict('c', 'f');
12296 if (fold_only && edit_logmsg_only)
12297 option_conflict('f', 'm');
12298 if (edit_script_path && fold_only)
12299 option_conflict('F', 'f');
12300 if (abort_edit && edit_only)
12301 option_conflict('a', 'e');
12302 if (continue_edit && edit_only)
12303 option_conflict('c', 'e');
12304 if (edit_only && edit_logmsg_only)
12305 option_conflict('e', 'm');
12306 if (edit_script_path && edit_only)
12307 option_conflict('F', 'e');
12308 if (fold_only && edit_only)
12309 option_conflict('f', 'e');
12310 if (drop_only && abort_edit)
12311 option_conflict('d', 'a');
12312 if (drop_only && continue_edit)
12313 option_conflict('d', 'c');
12314 if (drop_only && edit_logmsg_only)
12315 option_conflict('d', 'm');
12316 if (drop_only && edit_only)
12317 option_conflict('d', 'e');
12318 if (drop_only && edit_script_path)
12319 option_conflict('d', 'F');
12320 if (drop_only && fold_only)
12321 option_conflict('d', 'f');
12322 if (list_backups) {
12324 option_conflict('l', 'a');
12326 option_conflict('l', 'c');
12327 if (edit_script_path)
12328 option_conflict('l', 'F');
12329 if (edit_logmsg_only)
12330 option_conflict('l', 'm');
12332 option_conflict('l', 'd');
12334 option_conflict('l', 'f');
12336 option_conflict('l', 'e');
12337 if (delete_backups)
12338 option_conflict('l', 'X');
12339 if (argc != 0 && argc != 1)
12341 } else if (delete_backups) {
12343 option_conflict('X', 'a');
12345 option_conflict('X', 'c');
12347 option_conflict('X', 'd');
12348 if (edit_script_path)
12349 option_conflict('X', 'F');
12350 if (edit_logmsg_only)
12351 option_conflict('X', 'm');
12353 option_conflict('X', 'f');
12355 option_conflict('X', 'e');
12357 option_conflict('X', 'l');
12358 if (argc != 0 && argc != 1)
12360 } else if (argc != 0)
12364 * This command cannot apply unveil(2) in all cases because the
12365 * user may choose to run an editor to edit the histedit script
12366 * and to edit individual commit log messages.
12367 * unveil(2) traverses exec(2); if an editor is used we have to
12368 * apply unveil after edit script and log messages have been written.
12369 * XXX TODO: Make use of unveil(2) where possible.
12372 cwd = getcwd(NULL, 0);
12374 error = got_error_from_errno("getcwd");
12378 error = got_repo_pack_fds_open(&pack_fds);
12382 error = got_worktree_open(&worktree, cwd);
12384 if (list_backups || delete_backups) {
12385 if (error->code != GOT_ERR_NOT_WORKTREE)
12388 if (error->code == GOT_ERR_NOT_WORKTREE)
12389 error = wrap_not_worktree_error(error,
12395 if (list_backups || delete_backups) {
12396 error = got_repo_open(&repo,
12397 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12401 error = apply_unveil(got_repo_get_path(repo), 0,
12402 worktree ? got_worktree_get_root_path(worktree) : NULL);
12405 error = process_backup_refs(
12406 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12407 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12408 goto done; /* nothing else to do */
12411 error = get_gitconfig_path(&gitconfig_path);
12414 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12415 gitconfig_path, pack_fds);
12419 if (worktree != NULL && !list_backups && !delete_backups) {
12420 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12425 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12428 if (rebase_in_progress) {
12429 error = got_error(GOT_ERR_REBASING);
12433 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12437 if (merge_in_progress) {
12438 error = got_error(GOT_ERR_MERGE_BUSY);
12442 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12446 if (edit_in_progress && edit_logmsg_only) {
12447 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12448 "histedit operation is in progress in this "
12449 "work tree and must be continued or aborted "
12450 "before the -m option can be used");
12453 if (edit_in_progress && drop_only) {
12454 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12455 "histedit operation is in progress in this "
12456 "work tree and must be continued or aborted "
12457 "before the -d option can be used");
12460 if (edit_in_progress && fold_only) {
12461 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12462 "histedit operation is in progress in this "
12463 "work tree and must be continued or aborted "
12464 "before the -f option can be used");
12467 if (edit_in_progress && edit_only) {
12468 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12469 "histedit operation is in progress in this "
12470 "work tree and must be continued or aborted "
12471 "before the -e option can be used");
12475 if (edit_in_progress && abort_edit) {
12476 error = got_worktree_histedit_continue(&resume_commit_id,
12477 &tmp_branch, &branch, &base_commit_id, &fileindex,
12481 printf("Switching work tree to %s\n",
12482 got_ref_get_symref_target(branch));
12483 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12484 branch, base_commit_id, abort_progress, &upa);
12487 printf("Histedit of %s aborted\n",
12488 got_ref_get_symref_target(branch));
12489 print_merge_progress_stats(&upa);
12490 goto done; /* nothing else to do */
12491 } else if (abort_edit) {
12492 error = got_error(GOT_ERR_NOT_HISTEDIT);
12496 error = get_author(&committer, repo, worktree);
12500 if (continue_edit) {
12503 if (!edit_in_progress) {
12504 error = got_error(GOT_ERR_NOT_HISTEDIT);
12508 error = got_worktree_get_histedit_script_path(&path, worktree);
12512 error = histedit_load_list(&histedit_cmds, path, repo);
12517 error = got_worktree_histedit_continue(&resume_commit_id,
12518 &tmp_branch, &branch, &base_commit_id, &fileindex,
12523 error = got_ref_resolve(&head_commit_id, repo, branch);
12527 error = got_object_open_as_commit(&commit, repo,
12531 parent_ids = got_object_commit_get_parent_ids(commit);
12532 pid = STAILQ_FIRST(parent_ids);
12534 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12537 error = collect_commits(&commits, head_commit_id, &pid->id,
12538 base_commit_id, got_worktree_get_path_prefix(worktree),
12539 GOT_ERR_HISTEDIT_PATH, repo);
12540 got_object_commit_close(commit);
12545 if (edit_in_progress) {
12546 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12550 error = got_ref_open(&branch, repo,
12551 got_worktree_get_head_ref_name(worktree), 0);
12555 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12556 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12557 "will not edit commit history of a branch outside "
12558 "the \"refs/heads/\" reference namespace");
12562 error = got_ref_resolve(&head_commit_id, repo, branch);
12563 got_ref_close(branch);
12568 error = got_object_open_as_commit(&commit, repo,
12572 parent_ids = got_object_commit_get_parent_ids(commit);
12573 pid = STAILQ_FIRST(parent_ids);
12575 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12578 error = collect_commits(&commits, head_commit_id, &pid->id,
12579 got_worktree_get_base_commit_id(worktree),
12580 got_worktree_get_path_prefix(worktree),
12581 GOT_ERR_HISTEDIT_PATH, repo);
12582 got_object_commit_close(commit);
12587 if (STAILQ_EMPTY(&commits)) {
12588 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12592 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12593 &base_commit_id, &fileindex, worktree, repo);
12597 if (edit_script_path) {
12598 error = histedit_load_list(&histedit_cmds,
12599 edit_script_path, repo);
12601 got_worktree_histedit_abort(worktree, fileindex,
12602 repo, branch, base_commit_id,
12603 abort_progress, &upa);
12604 print_merge_progress_stats(&upa);
12608 const char *branch_name;
12609 branch_name = got_ref_get_symref_target(branch);
12610 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12612 error = histedit_edit_script(&histedit_cmds, &commits,
12613 branch_name, edit_logmsg_only, fold_only,
12614 drop_only, edit_only, repo);
12616 got_worktree_histedit_abort(worktree, fileindex,
12617 repo, branch, base_commit_id,
12618 abort_progress, &upa);
12619 print_merge_progress_stats(&upa);
12625 error = histedit_save_list(&histedit_cmds, worktree,
12628 got_worktree_histedit_abort(worktree, fileindex,
12629 repo, branch, base_commit_id,
12630 abort_progress, &upa);
12631 print_merge_progress_stats(&upa);
12637 error = histedit_check_script(&histedit_cmds, &commits, repo);
12641 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12642 if (resume_commit_id) {
12643 if (got_object_id_cmp(hle->commit_id,
12644 resume_commit_id) != 0)
12647 resume_commit_id = NULL;
12648 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12649 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12650 error = histedit_skip_commit(hle, worktree,
12655 struct got_pathlist_head paths;
12656 int have_changes = 0;
12658 TAILQ_INIT(&paths);
12659 error = got_pathlist_append(&paths, "", NULL);
12662 error = got_worktree_status(worktree, &paths,
12663 repo, 0, check_local_changes, &have_changes,
12664 check_cancelled, NULL);
12665 got_pathlist_free(&paths,
12666 GOT_PATHLIST_FREE_NONE);
12668 if (error->code != GOT_ERR_CANCELLED)
12670 if (sigint_received || sigpipe_received)
12673 if (have_changes) {
12674 error = histedit_commit(NULL, worktree,
12675 fileindex, tmp_branch, hle,
12680 error = got_object_open_as_commit(
12681 &commit, repo, hle->commit_id);
12684 error = show_histedit_progress(commit,
12686 got_object_commit_close(commit);
12695 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12696 error = histedit_skip_commit(hle, worktree, repo);
12702 error = got_object_open_as_commit(&commit, repo,
12706 parent_ids = got_object_commit_get_parent_ids(commit);
12707 pid = STAILQ_FIRST(parent_ids);
12709 error = got_worktree_histedit_merge_files(&merged_paths,
12710 worktree, fileindex, &pid->id, hle->commit_id, repo,
12711 update_progress, &upa, check_cancelled, NULL);
12714 got_object_commit_close(commit);
12717 print_merge_progress_stats(&upa);
12718 if (upa.conflicts > 0 || upa.missing > 0 ||
12719 upa.not_deleted > 0 || upa.unversioned > 0) {
12720 if (upa.conflicts > 0) {
12721 error = show_rebase_merge_conflict(
12722 hle->commit_id, repo);
12726 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12730 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12732 error = got_object_id_str(&id_str, hle->commit_id);
12735 printf("Stopping histedit for amending commit %s\n",
12738 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12739 error = got_worktree_histedit_postpone(worktree,
12744 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12745 error = histedit_skip_commit(hle, worktree, repo);
12751 error = histedit_commit(&merged_paths, worktree, fileindex,
12752 tmp_branch, hle, committer, repo);
12753 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12758 if (upa.conflicts > 0 || upa.missing > 0 ||
12759 upa.not_deleted > 0 || upa.unversioned > 0) {
12760 error = got_worktree_histedit_postpone(worktree, fileindex);
12763 if (upa.conflicts > 0 && upa.missing == 0 &&
12764 upa.not_deleted == 0 && upa.unversioned == 0) {
12765 error = got_error_msg(GOT_ERR_CONFLICTS,
12766 "conflicts must be resolved before histedit "
12768 } else if (upa.conflicts > 0) {
12769 error = got_error_msg(GOT_ERR_CONFLICTS,
12770 "conflicts must be resolved before histedit "
12771 "can continue; changes destined for some "
12772 "files were not yet merged and should be "
12773 "merged manually if required before the "
12774 "histedit operation is continued");
12776 error = got_error_msg(GOT_ERR_CONFLICTS,
12777 "changes destined for some files were not "
12778 "yet merged and should be merged manually "
12779 "if required before the histedit operation "
12783 error = histedit_complete(worktree, fileindex, tmp_branch,
12788 free(gitconfig_path);
12789 got_object_id_queue_free(&commits);
12790 histedit_free_list(&histedit_cmds);
12791 free(head_commit_id);
12792 free(base_commit_id);
12793 free(resume_commit_id);
12795 got_object_commit_close(commit);
12797 got_ref_close(branch);
12799 got_ref_close(tmp_branch);
12801 got_worktree_close(worktree);
12803 const struct got_error *close_err = got_repo_close(repo);
12808 const struct got_error *pack_err =
12809 got_repo_pack_fds_close(pack_fds);
12817 usage_integrate(void)
12819 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12823 static const struct got_error *
12824 cmd_integrate(int argc, char *argv[])
12826 const struct got_error *error = NULL;
12827 struct got_repository *repo = NULL;
12828 struct got_worktree *worktree = NULL;
12829 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12830 const char *branch_arg = NULL;
12831 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12832 struct got_fileindex *fileindex = NULL;
12833 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12835 struct got_update_progress_arg upa;
12836 int *pack_fds = NULL;
12838 while ((ch = getopt(argc, argv, "")) != -1) {
12851 branch_arg = argv[0];
12853 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12854 "unveil", NULL) == -1)
12857 cwd = getcwd(NULL, 0);
12859 error = got_error_from_errno("getcwd");
12863 error = got_repo_pack_fds_open(&pack_fds);
12867 error = got_worktree_open(&worktree, cwd);
12869 if (error->code == GOT_ERR_NOT_WORKTREE)
12870 error = wrap_not_worktree_error(error, "integrate",
12875 error = check_rebase_or_histedit_in_progress(worktree);
12879 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12884 error = apply_unveil(got_repo_get_path(repo), 0,
12885 got_worktree_get_root_path(worktree));
12889 error = check_merge_in_progress(worktree, repo);
12893 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12894 error = got_error_from_errno("asprintf");
12898 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12899 &base_branch_ref, worktree, refname, repo);
12903 refname = strdup(got_ref_get_name(branch_ref));
12904 if (refname == NULL) {
12905 error = got_error_from_errno("strdup");
12906 got_worktree_integrate_abort(worktree, fileindex, repo,
12907 branch_ref, base_branch_ref);
12910 base_refname = strdup(got_ref_get_name(base_branch_ref));
12911 if (base_refname == NULL) {
12912 error = got_error_from_errno("strdup");
12913 got_worktree_integrate_abort(worktree, fileindex, repo,
12914 branch_ref, base_branch_ref);
12917 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12918 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12919 got_worktree_integrate_abort(worktree, fileindex, repo,
12920 branch_ref, base_branch_ref);
12924 error = got_ref_resolve(&commit_id, repo, branch_ref);
12928 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12932 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12933 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12934 "specified branch has already been integrated");
12935 got_worktree_integrate_abort(worktree, fileindex, repo,
12936 branch_ref, base_branch_ref);
12940 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12942 if (error->code == GOT_ERR_ANCESTRY)
12943 error = got_error(GOT_ERR_REBASE_REQUIRED);
12944 got_worktree_integrate_abort(worktree, fileindex, repo,
12945 branch_ref, base_branch_ref);
12949 memset(&upa, 0, sizeof(upa));
12950 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12951 branch_ref, base_branch_ref, update_progress, &upa,
12952 check_cancelled, NULL);
12956 printf("Integrated %s into %s\n", refname, base_refname);
12957 print_update_progress_stats(&upa);
12960 const struct got_error *close_err = got_repo_close(repo);
12965 got_worktree_close(worktree);
12967 const struct got_error *pack_err =
12968 got_repo_pack_fds_close(pack_fds);
12973 free(base_commit_id);
12976 free(base_refname);
12983 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12987 static const struct got_error *
12988 cmd_merge(int argc, char *argv[])
12990 const struct got_error *error = NULL;
12991 struct got_worktree *worktree = NULL;
12992 struct got_repository *repo = NULL;
12993 struct got_fileindex *fileindex = NULL;
12994 char *cwd = NULL, *id_str = NULL, *author = NULL;
12995 struct got_reference *branch = NULL, *wt_branch = NULL;
12996 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12997 struct got_object_id *wt_branch_tip = NULL;
12998 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12999 int interrupt_merge = 0;
13000 struct got_update_progress_arg upa;
13001 struct got_object_id *merge_commit_id = NULL;
13002 char *branch_name = NULL;
13003 int *pack_fds = NULL;
13005 memset(&upa, 0, sizeof(upa));
13007 while ((ch = getopt(argc, argv, "acn")) != -1) {
13013 continue_merge = 1;
13016 interrupt_merge = 1;
13028 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13029 "unveil", NULL) == -1)
13033 if (abort_merge && continue_merge)
13034 option_conflict('a', 'c');
13035 if (abort_merge || continue_merge) {
13038 } else if (argc != 1)
13041 cwd = getcwd(NULL, 0);
13043 error = got_error_from_errno("getcwd");
13047 error = got_repo_pack_fds_open(&pack_fds);
13051 error = got_worktree_open(&worktree, cwd);
13053 if (error->code == GOT_ERR_NOT_WORKTREE)
13054 error = wrap_not_worktree_error(error,
13059 error = got_repo_open(&repo,
13060 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13065 if (worktree != NULL) {
13066 error = worktree_has_logmsg_ref("merge", worktree, repo);
13071 error = apply_unveil(got_repo_get_path(repo), 0,
13072 worktree ? got_worktree_get_root_path(worktree) : NULL);
13076 error = check_rebase_or_histedit_in_progress(worktree);
13080 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13086 if (!merge_in_progress) {
13087 error = got_error(GOT_ERR_NOT_MERGING);
13090 error = got_worktree_merge_continue(&branch_name,
13091 &branch_tip, &fileindex, worktree, repo);
13094 error = got_worktree_merge_abort(worktree, fileindex, repo,
13095 abort_progress, &upa);
13098 printf("Merge of %s aborted\n", branch_name);
13099 goto done; /* nothing else to do */
13102 error = get_author(&author, repo, worktree);
13106 if (continue_merge) {
13107 if (!merge_in_progress) {
13108 error = got_error(GOT_ERR_NOT_MERGING);
13111 error = got_worktree_merge_continue(&branch_name,
13112 &branch_tip, &fileindex, worktree, repo);
13116 error = got_ref_open(&branch, repo, argv[0], 0);
13119 branch_name = strdup(got_ref_get_name(branch));
13120 if (branch_name == NULL) {
13121 error = got_error_from_errno("strdup");
13124 error = got_ref_resolve(&branch_tip, repo, branch);
13129 error = got_ref_open(&wt_branch, repo,
13130 got_worktree_get_head_ref_name(worktree), 0);
13133 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13136 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13137 wt_branch_tip, branch_tip, 0, repo,
13138 check_cancelled, NULL);
13139 if (error && error->code != GOT_ERR_ANCESTRY)
13142 if (!continue_merge) {
13143 error = check_path_prefix(wt_branch_tip, branch_tip,
13144 got_worktree_get_path_prefix(worktree),
13145 GOT_ERR_MERGE_PATH, repo);
13149 error = check_same_branch(wt_branch_tip, branch,
13152 if (error->code != GOT_ERR_ANCESTRY)
13156 static char msg[512];
13157 snprintf(msg, sizeof(msg),
13158 "cannot create a merge commit because "
13159 "%s is based on %s; %s can be integrated "
13160 "with 'got integrate' instead", branch_name,
13161 got_worktree_get_head_ref_name(worktree),
13163 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13167 error = got_worktree_merge_prepare(&fileindex, worktree,
13172 error = got_worktree_merge_branch(worktree, fileindex,
13173 yca_id, branch_tip, repo, update_progress, &upa,
13174 check_cancelled, NULL);
13177 print_merge_progress_stats(&upa);
13178 if (!upa.did_something) {
13179 error = got_worktree_merge_abort(worktree, fileindex,
13180 repo, abort_progress, &upa);
13183 printf("Already up-to-date\n");
13188 if (interrupt_merge) {
13189 error = got_worktree_merge_postpone(worktree, fileindex);
13192 printf("Merge of %s interrupted on request\n", branch_name);
13193 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13194 upa.not_deleted > 0 || upa.unversioned > 0) {
13195 error = got_worktree_merge_postpone(worktree, fileindex);
13198 if (upa.conflicts > 0 && upa.missing == 0 &&
13199 upa.not_deleted == 0 && upa.unversioned == 0) {
13200 error = got_error_msg(GOT_ERR_CONFLICTS,
13201 "conflicts must be resolved before merging "
13203 } else if (upa.conflicts > 0) {
13204 error = got_error_msg(GOT_ERR_CONFLICTS,
13205 "conflicts must be resolved before merging "
13206 "can continue; changes destined for some "
13207 "files were not yet merged and "
13208 "should be merged manually if required before the "
13209 "merge operation is continued");
13211 error = got_error_msg(GOT_ERR_CONFLICTS,
13212 "changes destined for some "
13213 "files were not yet merged and should be "
13214 "merged manually if required before the "
13215 "merge operation is continued");
13219 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13220 fileindex, author, NULL, 1, branch_tip, branch_name,
13221 repo, continue_merge ? print_status : NULL, NULL);
13224 error = got_worktree_merge_complete(worktree, fileindex, repo);
13227 error = got_object_id_str(&id_str, merge_commit_id);
13230 printf("Merged %s into %s: %s\n", branch_name,
13231 got_worktree_get_head_ref_name(worktree),
13237 free(merge_commit_id);
13243 got_ref_close(branch);
13245 got_ref_close(wt_branch);
13247 got_worktree_close(worktree);
13249 const struct got_error *close_err = got_repo_close(repo);
13254 const struct got_error *pack_err =
13255 got_repo_pack_fds_close(pack_fds);
13265 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13266 "[path ...]\n", getprogname());
13270 static const struct got_error *
13271 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13272 const char *path, struct got_object_id *blob_id,
13273 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13274 int dirfd, const char *de_name)
13276 const struct got_error *err = NULL;
13277 char *id_str = NULL;
13279 if (staged_status != GOT_STATUS_ADD &&
13280 staged_status != GOT_STATUS_MODIFY &&
13281 staged_status != GOT_STATUS_DELETE)
13284 if (staged_status == GOT_STATUS_ADD ||
13285 staged_status == GOT_STATUS_MODIFY)
13286 err = got_object_id_str(&id_str, staged_blob_id);
13288 err = got_object_id_str(&id_str, blob_id);
13292 printf("%s %c %s\n", id_str, staged_status, path);
13297 static const struct got_error *
13298 cmd_stage(int argc, char *argv[])
13300 const struct got_error *error = NULL;
13301 struct got_repository *repo = NULL;
13302 struct got_worktree *worktree = NULL;
13304 struct got_pathlist_head paths;
13305 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13306 FILE *patch_script_file = NULL;
13307 const char *patch_script_path = NULL;
13308 struct choose_patch_arg cpa;
13309 int *pack_fds = NULL;
13311 TAILQ_INIT(&paths);
13313 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13316 patch_script_path = optarg;
13325 allow_bad_symlinks = 1;
13337 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13338 "unveil", NULL) == -1)
13341 if (list_stage && (pflag || patch_script_path))
13342 errx(1, "-l option cannot be used with other options");
13343 if (patch_script_path && !pflag)
13344 errx(1, "-F option can only be used together with -p option");
13346 cwd = getcwd(NULL, 0);
13348 error = got_error_from_errno("getcwd");
13352 error = got_repo_pack_fds_open(&pack_fds);
13356 error = got_worktree_open(&worktree, cwd);
13358 if (error->code == GOT_ERR_NOT_WORKTREE)
13359 error = wrap_not_worktree_error(error, "stage", cwd);
13363 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13368 if (patch_script_path) {
13369 patch_script_file = fopen(patch_script_path, "re");
13370 if (patch_script_file == NULL) {
13371 error = got_error_from_errno2("fopen",
13372 patch_script_path);
13376 error = apply_unveil(got_repo_get_path(repo), 0,
13377 got_worktree_get_root_path(worktree));
13381 error = check_merge_in_progress(worktree, repo);
13385 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13390 error = got_worktree_status(worktree, &paths, repo, 0,
13391 print_stage, NULL, check_cancelled, NULL);
13393 cpa.patch_script_file = patch_script_file;
13394 cpa.action = "stage";
13395 error = got_worktree_stage(worktree, &paths,
13396 pflag ? NULL : print_status, NULL,
13397 pflag ? choose_patch : NULL, &cpa,
13398 allow_bad_symlinks, repo);
13401 if (patch_script_file && fclose(patch_script_file) == EOF &&
13403 error = got_error_from_errno2("fclose", patch_script_path);
13405 const struct got_error *close_err = got_repo_close(repo);
13410 got_worktree_close(worktree);
13412 const struct got_error *pack_err =
13413 got_repo_pack_fds_close(pack_fds);
13417 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13423 usage_unstage(void)
13425 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13426 "[path ...]\n", getprogname());
13431 static const struct got_error *
13432 cmd_unstage(int argc, char *argv[])
13434 const struct got_error *error = NULL;
13435 struct got_repository *repo = NULL;
13436 struct got_worktree *worktree = NULL;
13438 struct got_pathlist_head paths;
13440 struct got_update_progress_arg upa;
13441 FILE *patch_script_file = NULL;
13442 const char *patch_script_path = NULL;
13443 struct choose_patch_arg cpa;
13444 int *pack_fds = NULL;
13446 TAILQ_INIT(&paths);
13448 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13451 patch_script_path = optarg;
13466 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13467 "unveil", NULL) == -1)
13470 if (patch_script_path && !pflag)
13471 errx(1, "-F option can only be used together with -p option");
13473 cwd = getcwd(NULL, 0);
13475 error = got_error_from_errno("getcwd");
13479 error = got_repo_pack_fds_open(&pack_fds);
13483 error = got_worktree_open(&worktree, cwd);
13485 if (error->code == GOT_ERR_NOT_WORKTREE)
13486 error = wrap_not_worktree_error(error, "unstage", cwd);
13490 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13495 if (patch_script_path) {
13496 patch_script_file = fopen(patch_script_path, "re");
13497 if (patch_script_file == NULL) {
13498 error = got_error_from_errno2("fopen",
13499 patch_script_path);
13504 error = apply_unveil(got_repo_get_path(repo), 0,
13505 got_worktree_get_root_path(worktree));
13509 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13513 cpa.patch_script_file = patch_script_file;
13514 cpa.action = "unstage";
13515 memset(&upa, 0, sizeof(upa));
13516 error = got_worktree_unstage(worktree, &paths, update_progress,
13517 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13519 print_merge_progress_stats(&upa);
13521 if (patch_script_file && fclose(patch_script_file) == EOF &&
13523 error = got_error_from_errno2("fclose", patch_script_path);
13525 const struct got_error *close_err = got_repo_close(repo);
13530 got_worktree_close(worktree);
13532 const struct got_error *pack_err =
13533 got_repo_pack_fds_close(pack_fds);
13537 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13545 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13546 "arg ...\n", getprogname());
13550 static const struct got_error *
13551 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13553 const struct got_error *err;
13554 struct got_blob_object *blob;
13557 fd = got_opentempfd();
13559 return got_error_from_errno("got_opentempfd");
13561 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13565 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13567 if (fd != -1 && close(fd) == -1 && err == NULL)
13568 err = got_error_from_errno("close");
13570 got_object_blob_close(blob);
13574 static const struct got_error *
13575 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13577 const struct got_error *err;
13578 struct got_tree_object *tree;
13581 err = got_object_open_as_tree(&tree, repo, id);
13585 nentries = got_object_tree_get_nentries(tree);
13586 for (i = 0; i < nentries; i++) {
13587 struct got_tree_entry *te;
13589 if (sigint_received || sigpipe_received)
13591 te = got_object_tree_get_entry(tree, i);
13592 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13595 fprintf(outfile, "%s %.7o %s\n", id_str,
13596 got_tree_entry_get_mode(te),
13597 got_tree_entry_get_name(te));
13601 got_object_tree_close(tree);
13605 static const struct got_error *
13606 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13608 const struct got_error *err;
13609 struct got_commit_object *commit;
13610 const struct got_object_id_queue *parent_ids;
13611 struct got_object_qid *pid;
13612 char *id_str = NULL;
13613 const char *logmsg = NULL;
13616 err = got_object_open_as_commit(&commit, repo, id);
13620 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13624 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13625 parent_ids = got_object_commit_get_parent_ids(commit);
13626 fprintf(outfile, "numparents %d\n",
13627 got_object_commit_get_nparents(commit));
13628 STAILQ_FOREACH(pid, parent_ids, entry) {
13630 err = got_object_id_str(&pid_str, &pid->id);
13633 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13636 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13637 got_object_commit_get_author_gmtoff(commit));
13638 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13639 got_object_commit_get_author(commit),
13640 (long long)got_object_commit_get_author_time(commit),
13643 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13644 got_object_commit_get_committer_gmtoff(commit));
13645 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13646 got_object_commit_get_committer(commit),
13647 (long long)got_object_commit_get_committer_time(commit),
13650 logmsg = got_object_commit_get_logmsg_raw(commit);
13651 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13652 fprintf(outfile, "%s", logmsg);
13655 got_object_commit_close(commit);
13659 static const struct got_error *
13660 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13662 const struct got_error *err;
13663 struct got_tag_object *tag;
13664 char *id_str = NULL;
13665 const char *tagmsg = NULL;
13668 err = got_object_open_as_tag(&tag, repo, id);
13672 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13676 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13678 switch (got_object_tag_get_object_type(tag)) {
13679 case GOT_OBJ_TYPE_BLOB:
13680 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13681 GOT_OBJ_LABEL_BLOB);
13683 case GOT_OBJ_TYPE_TREE:
13684 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13685 GOT_OBJ_LABEL_TREE);
13687 case GOT_OBJ_TYPE_COMMIT:
13688 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13689 GOT_OBJ_LABEL_COMMIT);
13691 case GOT_OBJ_TYPE_TAG:
13692 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13693 GOT_OBJ_LABEL_TAG);
13699 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13700 got_object_tag_get_name(tag));
13702 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13703 got_object_tag_get_tagger_gmtoff(tag));
13704 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13705 got_object_tag_get_tagger(tag),
13706 (long long)got_object_tag_get_tagger_time(tag),
13709 tagmsg = got_object_tag_get_message(tag);
13710 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13711 fprintf(outfile, "%s", tagmsg);
13714 got_object_tag_close(tag);
13718 static const struct got_error *
13719 cmd_cat(int argc, char *argv[])
13721 const struct got_error *error;
13722 struct got_repository *repo = NULL;
13723 struct got_worktree *worktree = NULL;
13724 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13725 const char *commit_id_str = NULL;
13726 struct got_object_id *id = NULL, *commit_id = NULL;
13727 struct got_commit_object *commit = NULL;
13728 int ch, obj_type, i, force_path = 0;
13729 struct got_reflist_head refs;
13730 int *pack_fds = NULL;
13735 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13740 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13743 commit_id_str = optarg;
13749 repo_path = realpath(optarg, NULL);
13750 if (repo_path == NULL)
13751 return got_error_from_errno2("realpath",
13753 got_path_strip_trailing_slashes(repo_path);
13764 cwd = getcwd(NULL, 0);
13766 error = got_error_from_errno("getcwd");
13770 error = got_repo_pack_fds_open(&pack_fds);
13774 if (repo_path == NULL) {
13775 error = got_worktree_open(&worktree, cwd);
13776 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13779 repo_path = strdup(
13780 got_worktree_get_repo_path(worktree));
13781 if (repo_path == NULL) {
13782 error = got_error_from_errno("strdup");
13786 /* Release work tree lock. */
13787 got_worktree_close(worktree);
13792 if (repo_path == NULL) {
13793 repo_path = strdup(cwd);
13794 if (repo_path == NULL)
13795 return got_error_from_errno("strdup");
13798 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13803 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13807 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13811 if (commit_id_str == NULL)
13812 commit_id_str = GOT_REF_HEAD;
13813 error = got_repo_match_object_id(&commit_id, NULL,
13814 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13818 error = got_object_open_as_commit(&commit, repo, commit_id);
13822 for (i = 0; i < argc; i++) {
13824 error = got_object_id_by_path(&id, repo, commit,
13829 error = got_repo_match_object_id(&id, &label, argv[i],
13830 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13833 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13834 error->code != GOT_ERR_NOT_REF)
13836 error = got_object_id_by_path(&id, repo,
13843 error = got_object_get_type(&obj_type, repo, id);
13847 switch (obj_type) {
13848 case GOT_OBJ_TYPE_BLOB:
13849 error = cat_blob(id, repo, stdout);
13851 case GOT_OBJ_TYPE_TREE:
13852 error = cat_tree(id, repo, stdout);
13854 case GOT_OBJ_TYPE_COMMIT:
13855 error = cat_commit(id, repo, stdout);
13857 case GOT_OBJ_TYPE_TAG:
13858 error = cat_tag(id, repo, stdout);
13861 error = got_error(GOT_ERR_OBJ_TYPE);
13876 got_object_commit_close(commit);
13878 got_worktree_close(worktree);
13880 const struct got_error *close_err = got_repo_close(repo);
13885 const struct got_error *pack_err =
13886 got_repo_pack_fds_close(pack_fds);
13891 got_ref_list_free(&refs);
13898 fprintf(stderr, "usage: %s info [path ...]\n",
13903 static const struct got_error *
13904 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13905 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13906 struct got_object_id *commit_id)
13908 const struct got_error *err = NULL;
13909 char *id_str = NULL;
13911 struct tm mytm, *tm;
13912 struct got_pathlist_head *paths = arg;
13913 struct got_pathlist_entry *pe;
13916 * Clear error indication from any of the path arguments which
13917 * would cause this file index entry to be displayed.
13919 TAILQ_FOREACH(pe, paths, entry) {
13920 if (got_path_cmp(path, pe->path, strlen(path),
13921 pe->path_len) == 0 ||
13922 got_path_is_child(path, pe->path, pe->path_len))
13923 pe->data = NULL; /* no error */
13926 printf(GOT_COMMIT_SEP_STR);
13928 printf("symlink: %s\n", path);
13929 else if (S_ISREG(mode)) {
13930 printf("file: %s\n", path);
13931 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13932 } else if (S_ISDIR(mode))
13933 printf("directory: %s\n", path);
13935 printf("something: %s\n", path);
13937 tm = localtime_r(&mtime, &mytm);
13940 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13941 return got_error(GOT_ERR_NO_SPACE);
13942 printf("timestamp: %s\n", datebuf);
13945 err = got_object_id_str(&id_str, blob_id);
13948 printf("based on blob: %s\n", id_str);
13952 if (staged_blob_id) {
13953 err = got_object_id_str(&id_str, staged_blob_id);
13956 printf("based on staged blob: %s\n", id_str);
13961 err = got_object_id_str(&id_str, commit_id);
13964 printf("based on commit: %s\n", id_str);
13971 static const struct got_error *
13972 cmd_info(int argc, char *argv[])
13974 const struct got_error *error = NULL;
13975 struct got_worktree *worktree = NULL;
13976 char *cwd = NULL, *id_str = NULL;
13977 struct got_pathlist_head paths;
13978 char *uuidstr = NULL;
13979 int ch, show_files = 0;
13981 TAILQ_INIT(&paths);
13983 while ((ch = getopt(argc, argv, "")) != -1) {
13995 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13999 cwd = getcwd(NULL, 0);
14001 error = got_error_from_errno("getcwd");
14005 error = got_worktree_open(&worktree, cwd);
14007 if (error->code == GOT_ERR_NOT_WORKTREE)
14008 error = wrap_not_worktree_error(error, "info", cwd);
14013 /* Remove "wpath cpath proc exec sendfd" promises. */
14014 if (pledge("stdio rpath flock unveil", NULL) == -1)
14017 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14022 error = get_worktree_paths_from_argv(&paths, argc, argv,
14029 error = got_object_id_str(&id_str,
14030 got_worktree_get_base_commit_id(worktree));
14034 error = got_worktree_get_uuid(&uuidstr, worktree);
14038 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14039 printf("work tree base commit: %s\n", id_str);
14040 printf("work tree path prefix: %s\n",
14041 got_worktree_get_path_prefix(worktree));
14042 printf("work tree branch reference: %s\n",
14043 got_worktree_get_head_ref_name(worktree));
14044 printf("work tree UUID: %s\n", uuidstr);
14045 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14048 struct got_pathlist_entry *pe;
14049 TAILQ_FOREACH(pe, &paths, entry) {
14050 if (pe->path_len == 0)
14053 * Assume this path will fail. This will be corrected
14054 * in print_path_info() in case the path does suceeed.
14056 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14058 error = got_worktree_path_info(worktree, &paths,
14059 print_path_info, &paths, check_cancelled, NULL);
14062 TAILQ_FOREACH(pe, &paths, entry) {
14063 if (pe->data != NULL) {
14064 const struct got_error *perr;
14067 error = got_error_fmt(perr->code, "%s",
14075 got_worktree_close(worktree);
14076 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);