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>
42 #include "got_compat.h"
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 static volatile sig_atomic_t sigint_received;
70 static volatile sig_atomic_t sigpipe_received;
73 catch_sigint(int signo)
79 catch_sigpipe(int signo)
87 const struct got_error *(*cmd_main)(int, char *[]);
88 void (*cmd_usage)(void);
89 const char *cmd_alias;
92 __dead static void usage(int, int);
93 __dead static void usage_import(void);
94 __dead static void usage_clone(void);
95 __dead static void usage_fetch(void);
96 __dead static void usage_checkout(void);
97 __dead static void usage_update(void);
98 __dead static void usage_log(void);
99 __dead static void usage_diff(void);
100 __dead static void usage_blame(void);
101 __dead static void usage_tree(void);
102 __dead static void usage_status(void);
103 __dead static void usage_ref(void);
104 __dead static void usage_branch(void);
105 __dead static void usage_tag(void);
106 __dead static void usage_add(void);
107 __dead static void usage_remove(void);
108 __dead static void usage_patch(void);
109 __dead static void usage_revert(void);
110 __dead static void usage_commit(void);
111 __dead static void usage_send(void);
112 __dead static void usage_cherrypick(void);
113 __dead static void usage_backout(void);
114 __dead static void usage_rebase(void);
115 __dead static void usage_histedit(void);
116 __dead static void usage_integrate(void);
117 __dead static void usage_merge(void);
118 __dead static void usage_stage(void);
119 __dead static void usage_unstage(void);
120 __dead static void usage_cat(void);
121 __dead static void usage_info(void);
123 static const struct got_error* cmd_import(int, char *[]);
124 static const struct got_error* cmd_clone(int, char *[]);
125 static const struct got_error* cmd_fetch(int, char *[]);
126 static const struct got_error* cmd_checkout(int, char *[]);
127 static const struct got_error* cmd_update(int, char *[]);
128 static const struct got_error* cmd_log(int, char *[]);
129 static const struct got_error* cmd_diff(int, char *[]);
130 static const struct got_error* cmd_blame(int, char *[]);
131 static const struct got_error* cmd_tree(int, char *[]);
132 static const struct got_error* cmd_status(int, char *[]);
133 static const struct got_error* cmd_ref(int, char *[]);
134 static const struct got_error* cmd_branch(int, char *[]);
135 static const struct got_error* cmd_tag(int, char *[]);
136 static const struct got_error* cmd_add(int, char *[]);
137 static const struct got_error* cmd_remove(int, char *[]);
138 static const struct got_error* cmd_patch(int, char *[]);
139 static const struct got_error* cmd_revert(int, char *[]);
140 static const struct got_error* cmd_commit(int, char *[]);
141 static const struct got_error* cmd_send(int, char *[]);
142 static const struct got_error* cmd_cherrypick(int, char *[]);
143 static const struct got_error* cmd_backout(int, char *[]);
144 static const struct got_error* cmd_rebase(int, char *[]);
145 static const struct got_error* cmd_histedit(int, char *[]);
146 static const struct got_error* cmd_integrate(int, char *[]);
147 static const struct got_error* cmd_merge(int, char *[]);
148 static const struct got_error* cmd_stage(int, char *[]);
149 static const struct got_error* cmd_unstage(int, char *[]);
150 static const struct got_error* cmd_cat(int, char *[]);
151 static const struct got_error* cmd_info(int, char *[]);
153 static const struct got_cmd got_commands[] = {
154 { "import", cmd_import, usage_import, "im" },
155 { "clone", cmd_clone, usage_clone, "cl" },
156 { "fetch", cmd_fetch, usage_fetch, "fe" },
157 { "checkout", cmd_checkout, usage_checkout, "co" },
158 { "update", cmd_update, usage_update, "up" },
159 { "log", cmd_log, usage_log, "" },
160 { "diff", cmd_diff, usage_diff, "di" },
161 { "blame", cmd_blame, usage_blame, "bl" },
162 { "tree", cmd_tree, usage_tree, "tr" },
163 { "status", cmd_status, usage_status, "st" },
164 { "ref", cmd_ref, usage_ref, "" },
165 { "branch", cmd_branch, usage_branch, "br" },
166 { "tag", cmd_tag, usage_tag, "" },
167 { "add", cmd_add, usage_add, "" },
168 { "remove", cmd_remove, usage_remove, "rm" },
169 { "patch", cmd_patch, usage_patch, "pa" },
170 { "revert", cmd_revert, usage_revert, "rv" },
171 { "commit", cmd_commit, usage_commit, "ci" },
172 { "send", cmd_send, usage_send, "se" },
173 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
174 { "backout", cmd_backout, usage_backout, "bo" },
175 { "rebase", cmd_rebase, usage_rebase, "rb" },
176 { "histedit", cmd_histedit, usage_histedit, "he" },
177 { "integrate", cmd_integrate, usage_integrate,"ig" },
178 { "merge", cmd_merge, usage_merge, "mg" },
179 { "stage", cmd_stage, usage_stage, "sg" },
180 { "unstage", cmd_unstage, usage_unstage, "ug" },
181 { "cat", cmd_cat, usage_cat, "" },
182 { "info", cmd_info, usage_info, "" },
186 list_commands(FILE *fp)
190 fprintf(fp, "commands:");
191 for (i = 0; i < nitems(got_commands); i++) {
192 const struct got_cmd *cmd = &got_commands[i];
193 fprintf(fp, " %s", cmd->cmd_name);
199 option_conflict(char a, char b)
201 errx(1, "-%c and -%c options are mutually exclusive", a, b);
205 main(int argc, char *argv[])
207 const struct got_cmd *cmd;
210 int hflag = 0, Vflag = 0;
211 static const struct option longopts[] = {
212 { "version", no_argument, NULL, 'V' },
216 setlocale(LC_CTYPE, "");
218 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
238 got_version_print_str();
243 usage(hflag, hflag ? 0 : 1);
245 signal(SIGINT, catch_sigint);
246 signal(SIGPIPE, catch_sigpipe);
248 for (i = 0; i < nitems(got_commands); i++) {
249 const struct got_error *error;
251 cmd = &got_commands[i];
253 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
254 strcmp(cmd->cmd_alias, argv[0]) != 0)
260 error = cmd->cmd_main(argc, argv);
261 if (error && error->code != GOT_ERR_CANCELLED &&
262 error->code != GOT_ERR_PRIVSEP_EXIT &&
263 !(sigpipe_received &&
264 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
266 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
267 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
274 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
275 list_commands(stderr);
280 usage(int hflag, int status)
282 FILE *fp = (status == 0) ? stdout : stderr;
284 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
291 static const struct got_error *
292 get_editor(char **abspath)
294 const struct got_error *err = NULL;
299 editor = getenv("VISUAL");
301 editor = getenv("EDITOR");
304 err = got_path_find_prog(abspath, editor);
309 if (*abspath == NULL) {
310 *abspath = strdup("/bin/ed");
311 if (*abspath == NULL)
312 return got_error_from_errno("strdup");
318 static const struct got_error *
319 apply_unveil(const char *repo_path, int repo_read_only,
320 const char *worktree_path)
322 const struct got_error *err;
325 if (unveil("gmon.out", "rwc") != 0)
326 return got_error_from_errno2("unveil", "gmon.out");
328 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
329 return got_error_from_errno2("unveil", repo_path);
331 if (worktree_path && unveil(worktree_path, "rwc") != 0)
332 return got_error_from_errno2("unveil", worktree_path);
334 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
335 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
337 err = got_privsep_unveil_exec_helpers();
341 if (unveil(NULL, NULL) != 0)
342 return got_error_from_errno("unveil");
350 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
351 "[-r repository-path] directory\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
394 const struct got_error *err = NULL;
401 if (fseeko(fp, 0L, SEEK_SET) == -1)
402 return got_error_from_errno("fseeko");
404 *logmsg = malloc(filesize + 1);
406 return got_error_from_errno("malloc");
409 while (getline(&line, &linesize, fp) != -1) {
410 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
411 continue; /* remove comments and leading empty lines */
412 *len = strlcat(*logmsg, line, filesize + 1);
413 if (*len >= filesize + 1) {
414 err = got_error(GOT_ERR_NO_SPACE);
419 err = got_ferror(fp, GOT_ERR_IO);
423 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
424 (*logmsg)[*len - 1] = '\0';
437 static const struct got_error *
438 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
439 const char *initial_content, size_t initial_content_len,
440 int require_modification)
442 const struct got_error *err = NULL;
449 if (stat(logmsg_path, &st) == -1)
450 return got_error_from_errno2("stat", logmsg_path);
452 if (spawn_editor(editor, logmsg_path) == -1)
453 return got_error_from_errno("failed spawning editor");
455 if (require_modification) {
456 struct timespec timeout;
460 nanosleep(&timeout, NULL);
463 if (stat(logmsg_path, &st2) == -1)
464 return got_error_from_errno("stat");
466 if (require_modification && st.st_size == st2.st_size &&
467 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
468 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "no changes made to commit message, aborting");
471 fp = fopen(logmsg_path, "re");
473 err = got_error_from_errno("fopen");
477 /* strip comments and leading/trailing newlines */
478 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
481 if (logmsg_len == 0) {
482 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
483 "commit message cannot be empty, aborting");
487 if (fp && fclose(fp) == EOF && err == NULL)
488 err = got_error_from_errno("fclose");
496 static const struct got_error *
497 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
498 const char *path_dir, const char *branch_name)
500 char *initial_content = NULL;
501 const struct got_error *err = NULL;
502 int initial_content_len;
505 initial_content_len = asprintf(&initial_content,
506 "\n# %s to be imported to branch %s\n", path_dir,
508 if (initial_content_len == -1)
509 return got_error_from_errno("asprintf");
511 err = got_opentemp_named_fd(logmsg_path, &fd,
512 GOT_TMPDIR_STR "/got-importmsg", "");
516 if (write(fd, initial_content, initial_content_len) == -1) {
517 err = got_error_from_errno2("write", *logmsg_path);
521 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
522 initial_content_len, 1);
524 if (fd != -1 && close(fd) == -1 && err == NULL)
525 err = got_error_from_errno2("close", *logmsg_path);
526 free(initial_content);
534 static const struct got_error *
535 import_progress(void *arg, const char *path)
537 printf("A %s\n", path);
541 static const struct got_error *
542 valid_author(const char *author)
544 const char *email = author;
547 * Git' expects the author (or committer) to be in the form
548 * "name <email>", which are mostly free form (see the
549 * "committer" description in git-fast-import(1)). We're only
550 * doing this to avoid git's object parser breaking on commits
554 while (*author && *author != '\n' && *author != '<' && *author != '>')
556 if (author != email && *author == '<' && *(author - 1) != ' ')
557 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
558 "between author name and email required", email);
559 if (*author++ != '<')
560 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
561 while (*author && *author != '\n' && *author != '<' && *author != '>')
563 if (strcmp(author, ">") != 0)
564 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
568 static const struct got_error *
569 get_author(char **author, struct got_repository *repo,
570 struct got_worktree *worktree)
572 const struct got_error *err = NULL;
573 const char *got_author = NULL, *name, *email;
574 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
579 worktree_conf = got_worktree_get_gotconfig(worktree);
580 repo_conf = got_repo_get_gotconfig(repo);
583 * Priority of potential author information sources, from most
584 * significant to least significant:
585 * 1) work tree's .got/got.conf file
586 * 2) repository's got.conf file
587 * 3) repository's git config file
588 * 4) environment variables
589 * 5) global git config files (in user's home directory or /etc)
593 got_author = got_gotconfig_get_author(worktree_conf);
594 if (got_author == NULL)
595 got_author = got_gotconfig_get_author(repo_conf);
596 if (got_author == NULL) {
597 name = got_repo_get_gitconfig_author_name(repo);
598 email = got_repo_get_gitconfig_author_email(repo);
600 if (asprintf(author, "%s <%s>", name, email) == -1)
601 return got_error_from_errno("asprintf");
605 got_author = getenv("GOT_AUTHOR");
606 if (got_author == NULL) {
607 name = got_repo_get_global_gitconfig_author_name(repo);
608 email = got_repo_get_global_gitconfig_author_email(
611 if (asprintf(author, "%s <%s>", name, email)
613 return got_error_from_errno("asprintf");
616 /* TODO: Look up user in password database? */
617 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
621 *author = strdup(got_author);
623 return got_error_from_errno("strdup");
625 err = valid_author(*author);
633 static const struct got_error *
634 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
635 struct got_worktree *worktree)
637 const char *got_allowed_signers = NULL;
638 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
640 *allowed_signers = NULL;
643 worktree_conf = got_worktree_get_gotconfig(worktree);
644 repo_conf = got_repo_get_gotconfig(repo);
647 * Priority of potential author information sources, from most
648 * significant to least significant:
649 * 1) work tree's .got/got.conf file
650 * 2) repository's got.conf file
654 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
656 if (got_allowed_signers == NULL)
657 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
660 if (got_allowed_signers) {
661 *allowed_signers = strdup(got_allowed_signers);
662 if (*allowed_signers == NULL)
663 return got_error_from_errno("strdup");
668 static const struct got_error *
669 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
670 struct got_worktree *worktree)
672 const char *got_revoked_signers = NULL;
673 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
675 *revoked_signers = NULL;
678 worktree_conf = got_worktree_get_gotconfig(worktree);
679 repo_conf = got_repo_get_gotconfig(repo);
682 * Priority of potential author information sources, from most
683 * significant to least significant:
684 * 1) work tree's .got/got.conf file
685 * 2) repository's got.conf file
689 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
691 if (got_revoked_signers == NULL)
692 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
695 if (got_revoked_signers) {
696 *revoked_signers = strdup(got_revoked_signers);
697 if (*revoked_signers == NULL)
698 return got_error_from_errno("strdup");
704 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
706 const char *got_signer_id = NULL;
707 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
710 worktree_conf = got_worktree_get_gotconfig(worktree);
711 repo_conf = got_repo_get_gotconfig(repo);
714 * Priority of potential author information sources, from most
715 * significant to least significant:
716 * 1) work tree's .got/got.conf file
717 * 2) repository's got.conf file
721 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
722 if (got_signer_id == NULL)
723 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
725 return got_signer_id;
728 static const struct got_error *
729 get_gitconfig_path(char **gitconfig_path)
731 const char *homedir = getenv("HOME");
733 *gitconfig_path = NULL;
735 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
736 return got_error_from_errno("asprintf");
742 static const struct got_error *
743 cmd_import(int argc, char *argv[])
745 const struct got_error *error = NULL;
746 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
747 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
748 const char *branch_name = NULL;
749 char *id_str = NULL, *logmsg_path = NULL;
750 char refname[PATH_MAX] = "refs/heads/";
751 struct got_repository *repo = NULL;
752 struct got_reference *branch_ref = NULL, *head_ref = NULL;
753 struct got_object_id *new_commit_id = NULL;
755 struct got_pathlist_head ignores;
756 struct got_pathlist_entry *pe;
757 int preserve_logmsg = 0;
758 int *pack_fds = NULL;
760 TAILQ_INIT(&ignores);
762 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
765 branch_name = optarg;
768 if (optarg[0] == '\0')
770 error = got_pathlist_insert(&pe, &ignores, optarg,
776 logmsg = strdup(optarg);
777 if (logmsg == NULL) {
778 error = got_error_from_errno("strdup");
783 repo_path = realpath(optarg, NULL);
784 if (repo_path == NULL) {
785 error = got_error_from_errno2("realpath",
800 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
808 if (repo_path == NULL) {
809 repo_path = getcwd(NULL, 0);
810 if (repo_path == NULL)
811 return got_error_from_errno("getcwd");
813 got_path_strip_trailing_slashes(repo_path);
814 error = get_gitconfig_path(&gitconfig_path);
817 error = got_repo_pack_fds_open(&pack_fds);
820 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
824 error = get_author(&author, repo, NULL);
829 * Don't let the user create a branch name with a leading '-'.
830 * While technically a valid reference name, this case is usually
831 * an unintended typo.
833 if (branch_name && branch_name[0] == '-')
834 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
836 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
837 if (error && error->code != GOT_ERR_NOT_REF)
841 n = strlcat(refname, branch_name, sizeof(refname));
842 else if (head_ref && got_ref_is_symbolic(head_ref))
843 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
846 n = strlcat(refname, "main", sizeof(refname));
847 if (n >= sizeof(refname)) {
848 error = got_error(GOT_ERR_NO_SPACE);
852 error = got_ref_open(&branch_ref, repo, refname, 0);
854 if (error->code != GOT_ERR_NOT_REF)
857 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
858 "import target branch already exists");
862 path_dir = realpath(argv[0], NULL);
863 if (path_dir == NULL) {
864 error = got_error_from_errno2("realpath", argv[0]);
867 got_path_strip_trailing_slashes(path_dir);
870 * unveil(2) traverses exec(2); if an editor is used we have
871 * to apply unveil after the log message has been written.
873 if (logmsg == NULL || strlen(logmsg) == 0) {
874 error = get_editor(&editor);
878 error = collect_import_msg(&logmsg, &logmsg_path, editor,
881 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
888 if (unveil(path_dir, "r") != 0) {
889 error = got_error_from_errno2("unveil", path_dir);
895 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
902 error = got_repo_import(&new_commit_id, path_dir, logmsg,
903 author, &ignores, repo, import_progress, NULL);
910 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
917 error = got_ref_write(branch_ref, repo);
924 error = got_object_id_str(&id_str, new_commit_id);
931 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
933 if (error->code != GOT_ERR_NOT_REF) {
939 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
947 error = got_ref_write(head_ref, repo);
955 printf("Created branch %s with commit %s\n",
956 got_ref_get_name(branch_ref), id_str);
959 const struct got_error *pack_err =
960 got_repo_pack_fds_close(pack_fds);
964 if (preserve_logmsg) {
965 fprintf(stderr, "%s: log message preserved in %s\n",
966 getprogname(), logmsg_path);
967 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
968 error = got_error_from_errno2("unlink", logmsg_path);
976 free(gitconfig_path);
978 got_ref_close(branch_ref);
980 got_ref_close(head_ref);
987 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
988 "repository-URL [directory]\n", getprogname());
992 struct got_fetch_progress_arg {
993 char last_scaled_size[FMT_SCALED_STRSIZE];
998 struct got_repository *repo;
1001 int configs_created;
1003 struct got_pathlist_head *symrefs;
1004 struct got_pathlist_head *wanted_branches;
1005 struct got_pathlist_head *wanted_refs;
1009 const char *remote_repo_path;
1010 const char *git_url;
1011 int fetch_all_branches;
1012 int mirror_references;
1016 /* XXX forward declaration */
1017 static const struct got_error *
1018 create_config_files(const char *proto, const char *host, const char *port,
1019 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1020 int mirror_references, struct got_pathlist_head *symrefs,
1021 struct got_pathlist_head *wanted_branches,
1022 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1024 static const struct got_error *
1025 fetch_progress(void *arg, const char *message, off_t packfile_size,
1026 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1028 const struct got_error *err = NULL;
1029 struct got_fetch_progress_arg *a = arg;
1030 char scaled_size[FMT_SCALED_STRSIZE];
1031 int p_indexed, p_resolved;
1032 int print_size = 0, print_indexed = 0, print_resolved = 0;
1035 * In order to allow a failed clone to be resumed with 'got fetch'
1036 * we try to create configuration files as soon as possible.
1037 * Once the server has sent information about its default branch
1038 * we have all required information.
1040 if (a->create_configs && !a->configs_created &&
1041 !TAILQ_EMPTY(a->config_info.symrefs)) {
1042 err = create_config_files(a->config_info.proto,
1043 a->config_info.host, a->config_info.port,
1044 a->config_info.remote_repo_path,
1045 a->config_info.git_url,
1046 a->config_info.fetch_all_branches,
1047 a->config_info.mirror_references,
1048 a->config_info.symrefs,
1049 a->config_info.wanted_branches,
1050 a->config_info.wanted_refs, a->repo);
1053 a->configs_created = 1;
1056 if (a->verbosity < 0)
1059 if (message && message[0] != '\0') {
1060 printf("\rserver: %s", message);
1065 if (packfile_size > 0 || nobj_indexed > 0) {
1066 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1067 (a->last_scaled_size[0] == '\0' ||
1068 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1070 if (strlcpy(a->last_scaled_size, scaled_size,
1071 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1072 return got_error(GOT_ERR_NO_SPACE);
1074 if (nobj_indexed > 0) {
1075 p_indexed = (nobj_indexed * 100) / nobj_total;
1076 if (p_indexed != a->last_p_indexed) {
1077 a->last_p_indexed = p_indexed;
1082 if (nobj_resolved > 0) {
1083 p_resolved = (nobj_resolved * 100) /
1084 (nobj_total - nobj_loose);
1085 if (p_resolved != a->last_p_resolved) {
1086 a->last_p_resolved = p_resolved;
1094 if (print_size || print_indexed || print_resolved)
1097 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1099 printf("; indexing %d%%", p_indexed);
1101 printf("; resolving deltas %d%%", p_resolved);
1102 if (print_size || print_indexed || print_resolved)
1108 static const struct got_error *
1109 create_symref(const char *refname, struct got_reference *target_ref,
1110 int verbosity, struct got_repository *repo)
1112 const struct got_error *err;
1113 struct got_reference *head_symref;
1115 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1119 err = got_ref_write(head_symref, repo);
1120 if (err == NULL && verbosity > 0) {
1121 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1122 got_ref_get_name(target_ref));
1124 got_ref_close(head_symref);
1128 static const struct got_error *
1129 list_remote_refs(struct got_pathlist_head *symrefs,
1130 struct got_pathlist_head *refs)
1132 const struct got_error *err;
1133 struct got_pathlist_entry *pe;
1135 TAILQ_FOREACH(pe, symrefs, entry) {
1136 const char *refname = pe->path;
1137 const char *targetref = pe->data;
1139 printf("%s: %s\n", refname, targetref);
1142 TAILQ_FOREACH(pe, refs, entry) {
1143 const char *refname = pe->path;
1144 struct got_object_id *id = pe->data;
1147 err = got_object_id_str(&id_str, id);
1150 printf("%s: %s\n", refname, id_str);
1157 static const struct got_error *
1158 create_ref(const char *refname, struct got_object_id *id,
1159 int verbosity, struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_reference *ref;
1165 err = got_object_id_str(&id_str, id);
1169 err = got_ref_alloc(&ref, refname, id);
1173 err = got_ref_write(ref, repo);
1176 if (err == NULL && verbosity >= 0)
1177 printf("Created reference %s: %s\n", refname, id_str);
1184 match_wanted_ref(const char *refname, const char *wanted_ref)
1186 if (strncmp(refname, "refs/", 5) != 0)
1191 * Prevent fetching of references that won't make any
1192 * sense outside of the remote repository's context.
1194 if (strncmp(refname, "got/", 4) == 0)
1196 if (strncmp(refname, "remotes/", 8) == 0)
1199 if (strncmp(wanted_ref, "refs/", 5) == 0)
1202 /* Allow prefix match. */
1203 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1206 /* Allow exact match. */
1207 return (strcmp(refname, wanted_ref) == 0);
1211 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1213 struct got_pathlist_entry *pe;
1215 TAILQ_FOREACH(pe, wanted_refs, entry) {
1216 if (match_wanted_ref(refname, pe->path))
1223 static const struct got_error *
1224 create_wanted_ref(const char *refname, struct got_object_id *id,
1225 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1227 const struct got_error *err;
1228 char *remote_refname;
1230 if (strncmp("refs/", refname, 5) == 0)
1233 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1234 remote_repo_name, refname) == -1)
1235 return got_error_from_errno("asprintf");
1237 err = create_ref(remote_refname, id, verbosity, repo);
1238 free(remote_refname);
1242 static const struct got_error *
1243 create_gotconfig(const char *proto, const char *host, const char *port,
1244 const char *remote_repo_path, const char *default_branch,
1245 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1246 struct got_pathlist_head *wanted_refs, int mirror_references,
1247 struct got_repository *repo)
1249 const struct got_error *err = NULL;
1250 char *gotconfig_path = NULL;
1251 char *gotconfig = NULL;
1252 FILE *gotconfig_file = NULL;
1253 const char *branchname = NULL;
1254 char *branches = NULL, *refs = NULL;
1257 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1258 struct got_pathlist_entry *pe;
1259 TAILQ_FOREACH(pe, wanted_branches, entry) {
1261 branchname = pe->path;
1262 if (strncmp(branchname, "refs/heads/", 11) == 0)
1264 if (asprintf(&s, "%s\"%s\" ",
1265 branches ? branches : "", branchname) == -1) {
1266 err = got_error_from_errno("asprintf");
1272 } else if (!fetch_all_branches && default_branch) {
1273 branchname = default_branch;
1274 if (strncmp(branchname, "refs/heads/", 11) == 0)
1276 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1277 err = got_error_from_errno("asprintf");
1281 if (!TAILQ_EMPTY(wanted_refs)) {
1282 struct got_pathlist_entry *pe;
1283 TAILQ_FOREACH(pe, wanted_refs, entry) {
1285 const char *refname = pe->path;
1286 if (strncmp(refname, "refs/", 5) == 0)
1288 if (asprintf(&s, "%s\"%s\" ",
1289 refs ? refs : "", refname) == -1) {
1290 err = got_error_from_errno("asprintf");
1298 /* Create got.conf(5). */
1299 gotconfig_path = got_repo_get_path_gotconfig(repo);
1300 if (gotconfig_path == NULL) {
1301 err = got_error_from_errno("got_repo_get_path_gotconfig");
1304 gotconfig_file = fopen(gotconfig_path, "ae");
1305 if (gotconfig_file == NULL) {
1306 err = got_error_from_errno2("fopen", gotconfig_path);
1309 if (asprintf(&gotconfig,
1314 "\trepository \"%s\"\n"
1320 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1321 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1322 remote_repo_path, branches ? "\tbranch { " : "",
1323 branches ? branches : "", branches ? "}\n" : "",
1324 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1325 mirror_references ? "\tmirror_references yes\n" : "",
1326 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1327 err = got_error_from_errno("asprintf");
1330 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1331 if (n != strlen(gotconfig)) {
1332 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1337 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1338 err = got_error_from_errno2("fclose", gotconfig_path);
1339 free(gotconfig_path);
1344 static const struct got_error *
1345 create_gitconfig(const char *git_url, const char *default_branch,
1346 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1347 struct got_pathlist_head *wanted_refs, int mirror_references,
1348 struct got_repository *repo)
1350 const struct got_error *err = NULL;
1351 char *gitconfig_path = NULL;
1352 char *gitconfig = NULL;
1353 FILE *gitconfig_file = NULL;
1354 char *branches = NULL, *refs = NULL;
1355 const char *branchname;
1358 /* Create a config file Git can understand. */
1359 gitconfig_path = got_repo_get_path_gitconfig(repo);
1360 if (gitconfig_path == NULL) {
1361 err = got_error_from_errno("got_repo_get_path_gitconfig");
1364 gitconfig_file = fopen(gitconfig_path, "ae");
1365 if (gitconfig_file == NULL) {
1366 err = got_error_from_errno2("fopen", gitconfig_path);
1369 if (fetch_all_branches) {
1370 if (mirror_references) {
1371 if (asprintf(&branches,
1372 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1373 err = got_error_from_errno("asprintf");
1376 } else if (asprintf(&branches,
1377 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1378 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1379 err = got_error_from_errno("asprintf");
1382 } else if (!TAILQ_EMPTY(wanted_branches)) {
1383 struct got_pathlist_entry *pe;
1384 TAILQ_FOREACH(pe, wanted_branches, entry) {
1386 branchname = pe->path;
1387 if (strncmp(branchname, "refs/heads/", 11) == 0)
1389 if (mirror_references) {
1391 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1392 branches ? branches : "",
1393 branchname, branchname) == -1) {
1394 err = got_error_from_errno("asprintf");
1397 } else if (asprintf(&s,
1398 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1399 branches ? branches : "",
1400 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1401 branchname) == -1) {
1402 err = got_error_from_errno("asprintf");
1410 * If the server specified a default branch, use just that one.
1411 * Otherwise fall back to fetching all branches on next fetch.
1413 if (default_branch) {
1414 branchname = default_branch;
1415 if (strncmp(branchname, "refs/heads/", 11) == 0)
1418 branchname = "*"; /* fall back to all branches */
1419 if (mirror_references) {
1420 if (asprintf(&branches,
1421 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1422 branchname, branchname) == -1) {
1423 err = got_error_from_errno("asprintf");
1426 } else if (asprintf(&branches,
1427 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1428 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1429 branchname) == -1) {
1430 err = got_error_from_errno("asprintf");
1434 if (!TAILQ_EMPTY(wanted_refs)) {
1435 struct got_pathlist_entry *pe;
1436 TAILQ_FOREACH(pe, wanted_refs, entry) {
1438 const char *refname = pe->path;
1439 if (strncmp(refname, "refs/", 5) == 0)
1441 if (mirror_references) {
1443 "%s\tfetch = refs/%s:refs/%s\n",
1444 refs ? refs : "", refname, refname) == -1) {
1445 err = got_error_from_errno("asprintf");
1448 } else if (asprintf(&s,
1449 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1451 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1453 err = got_error_from_errno("asprintf");
1461 if (asprintf(&gitconfig,
1466 "\tfetch = refs/tags/*:refs/tags/*\n",
1467 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1468 refs ? refs : "") == -1) {
1469 err = got_error_from_errno("asprintf");
1472 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1473 if (n != strlen(gitconfig)) {
1474 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1478 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1479 err = got_error_from_errno2("fclose", gitconfig_path);
1480 free(gitconfig_path);
1485 static const struct got_error *
1486 create_config_files(const char *proto, const char *host, const char *port,
1487 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1488 int mirror_references, struct got_pathlist_head *symrefs,
1489 struct got_pathlist_head *wanted_branches,
1490 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 const char *default_branch = NULL;
1494 struct got_pathlist_entry *pe;
1497 * If we asked for a set of wanted branches then use the first
1500 if (!TAILQ_EMPTY(wanted_branches)) {
1501 pe = TAILQ_FIRST(wanted_branches);
1502 default_branch = pe->path;
1504 /* First HEAD ref listed by server is the default branch. */
1505 TAILQ_FOREACH(pe, symrefs, entry) {
1506 const char *refname = pe->path;
1507 const char *target = pe->data;
1509 if (strcmp(refname, GOT_REF_HEAD) != 0)
1512 default_branch = target;
1517 /* Create got.conf(5). */
1518 err = create_gotconfig(proto, host, port, remote_repo_path,
1519 default_branch, fetch_all_branches, wanted_branches,
1520 wanted_refs, mirror_references, repo);
1524 /* Create a config file Git can understand. */
1525 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1526 wanted_branches, wanted_refs, mirror_references, repo);
1529 static const struct got_error *
1530 cmd_clone(int argc, char *argv[])
1532 const struct got_error *error = NULL;
1533 const char *uri, *dirname;
1534 char *proto, *host, *port, *repo_name, *server_path;
1535 char *default_destdir = NULL, *id_str = NULL;
1536 const char *repo_path;
1537 struct got_repository *repo = NULL;
1538 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1539 struct got_pathlist_entry *pe;
1540 struct got_object_id *pack_hash = NULL;
1541 int ch, fetchfd = -1, fetchstatus;
1542 pid_t fetchpid = -1;
1543 struct got_fetch_progress_arg fpa;
1544 char *git_url = NULL;
1545 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1546 int list_refs_only = 0;
1547 int *pack_fds = NULL;
1550 TAILQ_INIT(&symrefs);
1551 TAILQ_INIT(&wanted_branches);
1552 TAILQ_INIT(&wanted_refs);
1554 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1557 fetch_all_branches = 1;
1560 error = got_pathlist_append(&wanted_branches,
1569 mirror_references = 1;
1575 error = got_pathlist_append(&wanted_refs,
1583 else if (verbosity < 3)
1594 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1595 option_conflict('a', 'b');
1596 if (list_refs_only) {
1597 if (!TAILQ_EMPTY(&wanted_branches))
1598 option_conflict('l', 'b');
1599 if (fetch_all_branches)
1600 option_conflict('l', 'a');
1601 if (mirror_references)
1602 option_conflict('l', 'm');
1603 if (!TAILQ_EMPTY(&wanted_refs))
1604 option_conflict('l', 'R');
1616 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1621 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1622 host, port ? ":" : "", port ? port : "",
1623 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1624 error = got_error_from_errno("asprintf");
1628 if (strcmp(proto, "git") == 0) {
1630 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1631 "sendfd dns inet unveil", NULL) == -1)
1634 } else if (strcmp(proto, "git+ssh") == 0 ||
1635 strcmp(proto, "ssh") == 0) {
1637 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1638 "sendfd unveil", NULL) == -1)
1641 } else if (strcmp(proto, "http") == 0 ||
1642 strcmp(proto, "git+http") == 0) {
1643 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1646 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1649 if (dirname == NULL) {
1650 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1651 error = got_error_from_errno("asprintf");
1654 repo_path = default_destdir;
1656 repo_path = dirname;
1658 if (!list_refs_only) {
1659 error = got_path_mkdir(repo_path);
1661 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1662 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1664 if (!got_path_dir_is_empty(repo_path)) {
1665 error = got_error_path(repo_path,
1666 GOT_ERR_DIR_NOT_EMPTY);
1671 error = got_dial_apply_unveil(proto);
1675 error = apply_unveil(repo_path, 0, NULL);
1680 printf("Connecting to %s\n", git_url);
1682 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1683 server_path, verbosity);
1687 if (!list_refs_only) {
1688 error = got_repo_init(repo_path, NULL);
1691 error = got_repo_pack_fds_open(&pack_fds);
1694 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1699 fpa.last_scaled_size[0] = '\0';
1700 fpa.last_p_indexed = -1;
1701 fpa.last_p_resolved = -1;
1702 fpa.verbosity = verbosity;
1703 fpa.create_configs = 1;
1704 fpa.configs_created = 0;
1706 fpa.config_info.symrefs = &symrefs;
1707 fpa.config_info.wanted_branches = &wanted_branches;
1708 fpa.config_info.wanted_refs = &wanted_refs;
1709 fpa.config_info.proto = proto;
1710 fpa.config_info.host = host;
1711 fpa.config_info.port = port;
1712 fpa.config_info.remote_repo_path = server_path;
1713 fpa.config_info.git_url = git_url;
1714 fpa.config_info.fetch_all_branches = fetch_all_branches;
1715 fpa.config_info.mirror_references = mirror_references;
1716 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1717 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1718 fetch_all_branches, &wanted_branches, &wanted_refs,
1719 list_refs_only, verbosity, fetchfd, repo, NULL, 0,
1720 fetch_progress, &fpa);
1724 if (list_refs_only) {
1725 error = list_remote_refs(&symrefs, &refs);
1729 if (pack_hash == NULL) {
1730 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1731 "server sent an empty pack file");
1734 error = got_object_id_str(&id_str, pack_hash);
1738 printf("\nFetched %s.pack\n", id_str);
1741 /* Set up references provided with the pack file. */
1742 TAILQ_FOREACH(pe, &refs, entry) {
1743 const char *refname = pe->path;
1744 struct got_object_id *id = pe->data;
1745 char *remote_refname;
1747 if (is_wanted_ref(&wanted_refs, refname) &&
1748 !mirror_references) {
1749 error = create_wanted_ref(refname, id,
1750 GOT_FETCH_DEFAULT_REMOTE_NAME,
1751 verbosity - 1, repo);
1757 error = create_ref(refname, id, verbosity - 1, repo);
1761 if (mirror_references)
1764 if (strncmp("refs/heads/", refname, 11) != 0)
1767 if (asprintf(&remote_refname,
1768 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1769 refname + 11) == -1) {
1770 error = got_error_from_errno("asprintf");
1773 error = create_ref(remote_refname, id, verbosity - 1, repo);
1774 free(remote_refname);
1779 /* Set the HEAD reference if the server provided one. */
1780 TAILQ_FOREACH(pe, &symrefs, entry) {
1781 struct got_reference *target_ref;
1782 const char *refname = pe->path;
1783 const char *target = pe->data;
1784 char *remote_refname = NULL, *remote_target = NULL;
1786 if (strcmp(refname, GOT_REF_HEAD) != 0)
1789 error = got_ref_open(&target_ref, repo, target, 0);
1791 if (error->code == GOT_ERR_NOT_REF) {
1798 error = create_symref(refname, target_ref, verbosity, repo);
1799 got_ref_close(target_ref);
1803 if (mirror_references)
1806 if (strncmp("refs/heads/", target, 11) != 0)
1809 if (asprintf(&remote_refname,
1810 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1812 error = got_error_from_errno("asprintf");
1815 if (asprintf(&remote_target,
1816 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1817 target + 11) == -1) {
1818 error = got_error_from_errno("asprintf");
1819 free(remote_refname);
1822 error = got_ref_open(&target_ref, repo, remote_target, 0);
1824 free(remote_refname);
1825 free(remote_target);
1826 if (error->code == GOT_ERR_NOT_REF) {
1832 error = create_symref(remote_refname, target_ref,
1833 verbosity - 1, repo);
1834 free(remote_refname);
1835 free(remote_target);
1836 got_ref_close(target_ref);
1842 * We failed to set the HEAD reference. If we asked for
1843 * a set of wanted branches use the first of one of those
1844 * which could be fetched instead.
1846 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1847 const char *target = pe->path;
1848 struct got_reference *target_ref;
1850 error = got_ref_open(&target_ref, repo, target, 0);
1852 if (error->code == GOT_ERR_NOT_REF) {
1859 error = create_symref(GOT_REF_HEAD, target_ref,
1861 got_ref_close(target_ref);
1867 if (!fpa.configs_created && pe != NULL) {
1868 error = create_config_files(fpa.config_info.proto,
1869 fpa.config_info.host, fpa.config_info.port,
1870 fpa.config_info.remote_repo_path,
1871 fpa.config_info.git_url,
1872 fpa.config_info.fetch_all_branches,
1873 fpa.config_info.mirror_references,
1874 fpa.config_info.symrefs,
1875 fpa.config_info.wanted_branches,
1876 fpa.config_info.wanted_refs, fpa.repo);
1883 printf("Created %s repository '%s'\n",
1884 mirror_references ? "mirrored" : "cloned", repo_path);
1887 const struct got_error *pack_err =
1888 got_repo_pack_fds_close(pack_fds);
1893 if (kill(fetchpid, SIGTERM) == -1)
1894 error = got_error_from_errno("kill");
1895 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1896 error = got_error_from_errno("waitpid");
1898 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1899 error = got_error_from_errno("close");
1901 const struct got_error *close_err = got_repo_close(repo);
1905 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1906 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1907 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1908 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1915 free(default_destdir);
1920 static const struct got_error *
1921 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1922 int replace_tags, int verbosity, struct got_repository *repo)
1924 const struct got_error *err = NULL;
1925 char *new_id_str = NULL;
1926 struct got_object_id *old_id = NULL;
1928 err = got_object_id_str(&new_id_str, new_id);
1932 if (!replace_tags &&
1933 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1934 err = got_ref_resolve(&old_id, repo, ref);
1937 if (got_object_id_cmp(old_id, new_id) == 0)
1939 if (verbosity >= 0) {
1940 printf("Rejecting update of existing tag %s: %s\n",
1941 got_ref_get_name(ref), new_id_str);
1946 if (got_ref_is_symbolic(ref)) {
1947 if (verbosity >= 0) {
1948 printf("Replacing reference %s: %s\n",
1949 got_ref_get_name(ref),
1950 got_ref_get_symref_target(ref));
1952 err = got_ref_change_symref_to_ref(ref, new_id);
1955 err = got_ref_write(ref, repo);
1959 err = got_ref_resolve(&old_id, repo, ref);
1962 if (got_object_id_cmp(old_id, new_id) == 0)
1965 err = got_ref_change_ref(ref, new_id);
1968 err = got_ref_write(ref, repo);
1974 printf("Updated %s: %s\n", got_ref_get_name(ref),
1982 static const struct got_error *
1983 update_symref(const char *refname, struct got_reference *target_ref,
1984 int verbosity, struct got_repository *repo)
1986 const struct got_error *err = NULL, *unlock_err;
1987 struct got_reference *symref;
1988 int symref_is_locked = 0;
1990 err = got_ref_open(&symref, repo, refname, 1);
1992 if (err->code != GOT_ERR_NOT_REF)
1994 err = got_ref_alloc_symref(&symref, refname, target_ref);
1998 err = got_ref_write(symref, repo);
2003 printf("Created reference %s: %s\n",
2004 got_ref_get_name(symref),
2005 got_ref_get_symref_target(symref));
2007 symref_is_locked = 1;
2009 if (strcmp(got_ref_get_symref_target(symref),
2010 got_ref_get_name(target_ref)) == 0)
2013 err = got_ref_change_symref(symref,
2014 got_ref_get_name(target_ref));
2018 err = got_ref_write(symref, repo);
2023 printf("Updated %s: %s\n", got_ref_get_name(symref),
2024 got_ref_get_symref_target(symref));
2028 if (symref_is_locked) {
2029 unlock_err = got_ref_unlock(symref);
2030 if (unlock_err && err == NULL)
2033 got_ref_close(symref);
2040 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2041 "[-R reference] [-r repository-path] [remote-repository]\n",
2046 static const struct got_error *
2047 delete_missing_ref(struct got_reference *ref,
2048 int verbosity, struct got_repository *repo)
2050 const struct got_error *err = NULL;
2051 struct got_object_id *id = NULL;
2052 char *id_str = NULL;
2054 if (got_ref_is_symbolic(ref)) {
2055 err = got_ref_delete(ref, repo);
2058 if (verbosity >= 0) {
2059 printf("Deleted %s: %s\n",
2060 got_ref_get_name(ref),
2061 got_ref_get_symref_target(ref));
2064 err = got_ref_resolve(&id, repo, ref);
2067 err = got_object_id_str(&id_str, id);
2071 err = got_ref_delete(ref, repo);
2074 if (verbosity >= 0) {
2075 printf("Deleted %s: %s\n",
2076 got_ref_get_name(ref), id_str);
2085 static const struct got_error *
2086 delete_missing_refs(struct got_pathlist_head *their_refs,
2087 struct got_pathlist_head *their_symrefs,
2088 const struct got_remote_repo *remote,
2089 int verbosity, struct got_repository *repo)
2091 const struct got_error *err = NULL, *unlock_err;
2092 struct got_reflist_head my_refs;
2093 struct got_reflist_entry *re;
2094 struct got_pathlist_entry *pe;
2095 char *remote_namespace = NULL;
2096 char *local_refname = NULL;
2098 TAILQ_INIT(&my_refs);
2100 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2102 return got_error_from_errno("asprintf");
2104 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2108 TAILQ_FOREACH(re, &my_refs, entry) {
2109 const char *refname = got_ref_get_name(re->ref);
2110 const char *their_refname;
2112 if (remote->mirror_references) {
2113 their_refname = refname;
2115 if (strncmp(refname, remote_namespace,
2116 strlen(remote_namespace)) == 0) {
2117 if (strcmp(refname + strlen(remote_namespace),
2120 if (asprintf(&local_refname, "refs/heads/%s",
2121 refname + strlen(remote_namespace)) == -1) {
2122 err = got_error_from_errno("asprintf");
2125 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2128 their_refname = local_refname;
2131 TAILQ_FOREACH(pe, their_refs, entry) {
2132 if (strcmp(their_refname, pe->path) == 0)
2138 TAILQ_FOREACH(pe, their_symrefs, entry) {
2139 if (strcmp(their_refname, pe->path) == 0)
2145 err = delete_missing_ref(re->ref, verbosity, repo);
2149 if (local_refname) {
2150 struct got_reference *ref;
2151 err = got_ref_open(&ref, repo, local_refname, 1);
2153 if (err->code != GOT_ERR_NOT_REF)
2155 free(local_refname);
2156 local_refname = NULL;
2159 err = delete_missing_ref(ref, verbosity, repo);
2162 unlock_err = got_ref_unlock(ref);
2164 if (unlock_err && err == NULL) {
2169 free(local_refname);
2170 local_refname = NULL;
2174 got_ref_list_free(&my_refs);
2175 free(remote_namespace);
2176 free(local_refname);
2180 static const struct got_error *
2181 update_wanted_ref(const char *refname, struct got_object_id *id,
2182 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2184 const struct got_error *err, *unlock_err;
2185 char *remote_refname;
2186 struct got_reference *ref;
2188 if (strncmp("refs/", refname, 5) == 0)
2191 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2192 remote_repo_name, refname) == -1)
2193 return got_error_from_errno("asprintf");
2195 err = got_ref_open(&ref, repo, remote_refname, 1);
2197 if (err->code != GOT_ERR_NOT_REF)
2199 err = create_ref(remote_refname, id, verbosity, repo);
2201 err = update_ref(ref, id, 0, verbosity, repo);
2202 unlock_err = got_ref_unlock(ref);
2203 if (unlock_err && err == NULL)
2208 free(remote_refname);
2212 static const struct got_error *
2213 delete_ref(struct got_repository *repo, struct got_reference *ref)
2215 const struct got_error *err = NULL;
2216 struct got_object_id *id = NULL;
2217 char *id_str = NULL;
2220 if (got_ref_is_symbolic(ref)) {
2221 target = got_ref_get_symref_target(ref);
2223 err = got_ref_resolve(&id, repo, ref);
2226 err = got_object_id_str(&id_str, id);
2232 err = got_ref_delete(ref, repo);
2236 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2243 static const struct got_error *
2244 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2246 const struct got_error *err = NULL;
2247 struct got_reflist_head refs;
2248 struct got_reflist_entry *re;
2253 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2254 err = got_error_from_errno("asprintf");
2257 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2261 TAILQ_FOREACH(re, &refs, entry)
2262 delete_ref(repo, re->ref);
2264 got_ref_list_free(&refs);
2268 static const struct got_error *
2269 cmd_fetch(int argc, char *argv[])
2271 const struct got_error *error = NULL, *unlock_err;
2272 char *cwd = NULL, *repo_path = NULL;
2273 const char *remote_name;
2274 char *proto = NULL, *host = NULL, *port = NULL;
2275 char *repo_name = NULL, *server_path = NULL;
2276 const struct got_remote_repo *remotes, *remote = NULL;
2278 char *id_str = NULL;
2279 struct got_repository *repo = NULL;
2280 struct got_worktree *worktree = NULL;
2281 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2282 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2283 struct got_pathlist_entry *pe;
2284 struct got_object_id *pack_hash = NULL;
2285 int i, ch, fetchfd = -1, fetchstatus;
2286 pid_t fetchpid = -1;
2287 struct got_fetch_progress_arg fpa;
2288 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2289 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2290 int *pack_fds = NULL, have_bflag = 0;
2291 const char *worktree_branch = NULL;
2294 TAILQ_INIT(&symrefs);
2295 TAILQ_INIT(&wanted_branches);
2296 TAILQ_INIT(&wanted_refs);
2298 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2301 fetch_all_branches = 1;
2304 error = got_pathlist_append(&wanted_branches,
2320 error = got_pathlist_append(&wanted_refs,
2326 repo_path = realpath(optarg, NULL);
2327 if (repo_path == NULL)
2328 return got_error_from_errno2("realpath",
2330 got_path_strip_trailing_slashes(repo_path);
2338 else if (verbosity < 3)
2352 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2353 option_conflict('a', 'b');
2354 if (list_refs_only) {
2355 if (!TAILQ_EMPTY(&wanted_branches))
2356 option_conflict('l', 'b');
2357 if (fetch_all_branches)
2358 option_conflict('l', 'a');
2360 option_conflict('l', 'd');
2362 option_conflict('l', 'X');
2364 if (delete_remote) {
2365 if (fetch_all_branches)
2366 option_conflict('X', 'a');
2367 if (!TAILQ_EMPTY(&wanted_branches))
2368 option_conflict('X', 'b');
2370 option_conflict('X', 'd');
2372 option_conflict('X', 't');
2373 if (!TAILQ_EMPTY(&wanted_refs))
2374 option_conflict('X', 'R');
2379 errx(1, "-X option requires a remote name");
2380 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2381 } else if (argc == 1)
2382 remote_name = argv[0];
2386 cwd = getcwd(NULL, 0);
2388 error = got_error_from_errno("getcwd");
2392 error = got_repo_pack_fds_open(&pack_fds);
2396 if (repo_path == NULL) {
2397 error = got_worktree_open(&worktree, cwd);
2398 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2404 strdup(got_worktree_get_repo_path(worktree));
2405 if (repo_path == NULL)
2406 error = got_error_from_errno("strdup");
2410 repo_path = strdup(cwd);
2411 if (repo_path == NULL) {
2412 error = got_error_from_errno("strdup");
2418 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2422 if (delete_remote) {
2423 error = delete_refs_for_remote(repo, remote_name);
2424 goto done; /* nothing else to do */
2428 worktree_conf = got_worktree_get_gotconfig(worktree);
2429 if (worktree_conf) {
2430 got_gotconfig_get_remotes(&nremotes, &remotes,
2432 for (i = 0; i < nremotes; i++) {
2433 if (strcmp(remotes[i].name, remote_name) == 0) {
2434 remote = &remotes[i];
2440 if (remote == NULL) {
2441 repo_conf = got_repo_get_gotconfig(repo);
2443 got_gotconfig_get_remotes(&nremotes, &remotes,
2445 for (i = 0; i < nremotes; i++) {
2446 if (strcmp(remotes[i].name, remote_name) == 0) {
2447 remote = &remotes[i];
2453 if (remote == NULL) {
2454 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2455 for (i = 0; i < nremotes; i++) {
2456 if (strcmp(remotes[i].name, remote_name) == 0) {
2457 remote = &remotes[i];
2462 if (remote == NULL) {
2463 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2467 if (TAILQ_EMPTY(&wanted_branches)) {
2468 if (!fetch_all_branches)
2469 fetch_all_branches = remote->fetch_all_branches;
2470 for (i = 0; i < remote->nfetch_branches; i++) {
2471 error = got_pathlist_append(&wanted_branches,
2472 remote->fetch_branches[i], NULL);
2477 if (TAILQ_EMPTY(&wanted_refs)) {
2478 for (i = 0; i < remote->nfetch_refs; i++) {
2479 error = got_pathlist_append(&wanted_refs,
2480 remote->fetch_refs[i], NULL);
2486 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2487 &repo_name, remote->fetch_url);
2491 if (strcmp(proto, "git") == 0) {
2493 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2494 "sendfd dns inet unveil", NULL) == -1)
2497 } else if (strcmp(proto, "git+ssh") == 0 ||
2498 strcmp(proto, "ssh") == 0) {
2500 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2501 "sendfd unveil", NULL) == -1)
2504 } else if (strcmp(proto, "http") == 0 ||
2505 strcmp(proto, "git+http") == 0) {
2506 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2509 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2513 error = got_dial_apply_unveil(proto);
2517 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2521 if (verbosity >= 0) {
2522 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2523 remote->name, proto, host,
2524 port ? ":" : "", port ? port : "",
2525 *server_path == '/' ? "" : "/", server_path);
2528 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2529 server_path, verbosity);
2533 if (worktree && !have_bflag) {
2534 const char *refname;
2536 refname = got_worktree_get_head_ref_name(worktree);
2537 if (strncmp(refname, "refs/heads/", 11) == 0)
2538 worktree_branch = refname;
2541 fpa.last_scaled_size[0] = '\0';
2542 fpa.last_p_indexed = -1;
2543 fpa.last_p_resolved = -1;
2544 fpa.verbosity = verbosity;
2546 fpa.create_configs = 0;
2547 fpa.configs_created = 0;
2548 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2550 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2551 remote->mirror_references, fetch_all_branches, &wanted_branches,
2552 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2553 worktree_branch, have_bflag, fetch_progress, &fpa);
2557 if (list_refs_only) {
2558 error = list_remote_refs(&symrefs, &refs);
2562 if (pack_hash == NULL) {
2564 printf("Already up-to-date\n");
2565 } else if (verbosity >= 0) {
2566 error = got_object_id_str(&id_str, pack_hash);
2569 printf("\nFetched %s.pack\n", id_str);
2574 /* Update references provided with the pack file. */
2575 TAILQ_FOREACH(pe, &refs, entry) {
2576 const char *refname = pe->path;
2577 struct got_object_id *id = pe->data;
2578 struct got_reference *ref;
2579 char *remote_refname;
2581 if (is_wanted_ref(&wanted_refs, refname) &&
2582 !remote->mirror_references) {
2583 error = update_wanted_ref(refname, id,
2584 remote->name, verbosity, repo);
2590 if (remote->mirror_references ||
2591 strncmp("refs/tags/", refname, 10) == 0) {
2592 error = got_ref_open(&ref, repo, refname, 1);
2594 if (error->code != GOT_ERR_NOT_REF)
2596 error = create_ref(refname, id, verbosity,
2601 error = update_ref(ref, id, replace_tags,
2603 unlock_err = got_ref_unlock(ref);
2604 if (unlock_err && error == NULL)
2610 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2611 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2612 remote_name, refname + 11) == -1) {
2613 error = got_error_from_errno("asprintf");
2617 error = got_ref_open(&ref, repo, remote_refname, 1);
2619 if (error->code != GOT_ERR_NOT_REF)
2621 error = create_ref(remote_refname, id,
2626 error = update_ref(ref, id, replace_tags,
2628 unlock_err = got_ref_unlock(ref);
2629 if (unlock_err && error == NULL)
2636 /* Also create a local branch if none exists yet. */
2637 error = got_ref_open(&ref, repo, refname, 1);
2639 if (error->code != GOT_ERR_NOT_REF)
2641 error = create_ref(refname, id, verbosity,
2646 unlock_err = got_ref_unlock(ref);
2647 if (unlock_err && error == NULL)
2654 error = delete_missing_refs(&refs, &symrefs, remote,
2660 if (!remote->mirror_references) {
2661 /* Update remote HEAD reference if the server provided one. */
2662 TAILQ_FOREACH(pe, &symrefs, entry) {
2663 struct got_reference *target_ref;
2664 const char *refname = pe->path;
2665 const char *target = pe->data;
2666 char *remote_refname = NULL, *remote_target = NULL;
2668 if (strcmp(refname, GOT_REF_HEAD) != 0)
2671 if (strncmp("refs/heads/", target, 11) != 0)
2674 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2675 remote->name, refname) == -1) {
2676 error = got_error_from_errno("asprintf");
2679 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2680 remote->name, target + 11) == -1) {
2681 error = got_error_from_errno("asprintf");
2682 free(remote_refname);
2686 error = got_ref_open(&target_ref, repo, remote_target,
2689 free(remote_refname);
2690 free(remote_target);
2691 if (error->code == GOT_ERR_NOT_REF) {
2697 error = update_symref(remote_refname, target_ref,
2699 free(remote_refname);
2700 free(remote_target);
2701 got_ref_close(target_ref);
2708 if (kill(fetchpid, SIGTERM) == -1)
2709 error = got_error_from_errno("kill");
2710 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2711 error = got_error_from_errno("waitpid");
2713 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2714 error = got_error_from_errno("close");
2716 const struct got_error *close_err = got_repo_close(repo);
2721 got_worktree_close(worktree);
2723 const struct got_error *pack_err =
2724 got_repo_pack_fds_close(pack_fds);
2728 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2729 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2730 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2731 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2746 usage_checkout(void)
2748 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2749 "[-p path-prefix] repository-path [work-tree-path]\n",
2755 show_worktree_base_ref_warning(void)
2757 fprintf(stderr, "%s: warning: could not create a reference "
2758 "to the work tree's base commit; the commit could be "
2759 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2760 "repository writable and running 'got update' will prevent this\n",
2764 struct got_checkout_progress_arg {
2765 const char *worktree_path;
2766 int had_base_commit_ref_error;
2770 static const struct got_error *
2771 checkout_progress(void *arg, unsigned char status, const char *path)
2773 struct got_checkout_progress_arg *a = arg;
2775 /* Base commit bump happens silently. */
2776 if (status == GOT_STATUS_BUMP_BASE)
2779 if (status == GOT_STATUS_BASE_REF_ERR) {
2780 a->had_base_commit_ref_error = 1;
2784 while (path[0] == '/')
2787 if (a->verbosity >= 0)
2788 printf("%c %s/%s\n", status, a->worktree_path, path);
2793 static const struct got_error *
2794 check_cancelled(void *arg)
2796 if (sigint_received || sigpipe_received)
2797 return got_error(GOT_ERR_CANCELLED);
2801 static const struct got_error *
2802 check_linear_ancestry(struct got_object_id *commit_id,
2803 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2804 struct got_repository *repo)
2806 const struct got_error *err = NULL;
2807 struct got_object_id *yca_id;
2809 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2810 commit_id, base_commit_id, 1, repo, check_cancelled, NULL);
2815 return got_error(GOT_ERR_ANCESTRY);
2818 * Require a straight line of history between the target commit
2819 * and the work tree's base commit.
2821 * Non-linear situations such as this require a rebase:
2823 * (commit) D F (base_commit)
2831 * 'got update' only handles linear cases:
2832 * Update forwards in time: A (base/yca) - B - C - D (commit)
2833 * Update backwards in time: D (base) - C - B - A (commit/yca)
2835 if (allow_forwards_in_time_only) {
2836 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2837 return got_error(GOT_ERR_ANCESTRY);
2838 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2839 got_object_id_cmp(base_commit_id, yca_id) != 0)
2840 return got_error(GOT_ERR_ANCESTRY);
2846 static const struct got_error *
2847 check_same_branch(struct got_object_id *commit_id,
2848 struct got_reference *head_ref, struct got_object_id *yca_id,
2849 struct got_repository *repo)
2851 const struct got_error *err = NULL;
2852 struct got_commit_graph *graph = NULL;
2853 struct got_object_id *head_commit_id = NULL;
2854 int is_same_branch = 0;
2856 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2860 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2864 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2869 err = got_commit_graph_open(&graph, "/", 1);
2873 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2874 check_cancelled, NULL);
2879 struct got_object_id id;
2881 err = got_commit_graph_iter_next(&id, graph, repo,
2882 check_cancelled, NULL);
2884 if (err->code == GOT_ERR_ITER_COMPLETED)
2889 if (yca_id && got_object_id_cmp(&id, yca_id) == 0)
2891 if (got_object_id_cmp(&id, commit_id) == 0) {
2898 got_commit_graph_close(graph);
2899 free(head_commit_id);
2900 if (!err && !is_same_branch)
2901 err = got_error(GOT_ERR_ANCESTRY);
2905 static const struct got_error *
2906 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2908 static char msg[512];
2909 const char *branch_name;
2911 if (got_ref_is_symbolic(ref))
2912 branch_name = got_ref_get_symref_target(ref);
2914 branch_name = got_ref_get_name(ref);
2916 if (strncmp("refs/heads/", branch_name, 11) == 0)
2919 snprintf(msg, sizeof(msg),
2920 "target commit is not contained in branch '%s'; "
2921 "the branch to use must be specified with -b; "
2922 "if necessary a new branch can be created for "
2923 "this commit with 'got branch -c %s BRANCH_NAME'",
2924 branch_name, commit_id_str);
2926 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2929 static const struct got_error *
2930 cmd_checkout(int argc, char *argv[])
2932 const struct got_error *error = NULL;
2933 struct got_repository *repo = NULL;
2934 struct got_reference *head_ref = NULL, *ref = NULL;
2935 struct got_worktree *worktree = NULL;
2936 char *repo_path = NULL;
2937 char *worktree_path = NULL;
2938 const char *path_prefix = "";
2939 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
2940 char *commit_id_str = NULL;
2941 struct got_object_id *commit_id = NULL;
2943 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
2944 struct got_pathlist_head paths;
2945 struct got_checkout_progress_arg cpa;
2946 int *pack_fds = NULL;
2950 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
2953 branch_name = optarg;
2956 commit_id_str = strdup(optarg);
2957 if (commit_id_str == NULL)
2958 return got_error_from_errno("strdup");
2964 path_prefix = optarg;
2979 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2980 "unveil", NULL) == -1)
2984 char *base, *dotgit;
2986 repo_path = realpath(argv[0], NULL);
2987 if (repo_path == NULL)
2988 return got_error_from_errno2("realpath", argv[0]);
2989 cwd = getcwd(NULL, 0);
2991 error = got_error_from_errno("getcwd");
2998 error = got_path_basename(&base, path);
3001 dotgit = strstr(base, ".git");
3004 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3005 error = got_error_from_errno("asprintf");
3010 } else if (argc == 2) {
3011 repo_path = realpath(argv[0], NULL);
3012 if (repo_path == NULL) {
3013 error = got_error_from_errno2("realpath", argv[0]);
3016 worktree_path = realpath(argv[1], NULL);
3017 if (worktree_path == NULL) {
3018 if (errno != ENOENT) {
3019 error = got_error_from_errno2("realpath",
3023 worktree_path = strdup(argv[1]);
3024 if (worktree_path == NULL) {
3025 error = got_error_from_errno("strdup");
3032 got_path_strip_trailing_slashes(repo_path);
3033 got_path_strip_trailing_slashes(worktree_path);
3035 error = got_repo_pack_fds_open(&pack_fds);
3039 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3043 /* Pre-create work tree path for unveil(2) */
3044 error = got_path_mkdir(worktree_path);
3046 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3047 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3049 if (!allow_nonempty &&
3050 !got_path_dir_is_empty(worktree_path)) {
3051 error = got_error_path(worktree_path,
3052 GOT_ERR_DIR_NOT_EMPTY);
3057 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3061 error = got_ref_open(&head_ref, repo, branch_name, 0);
3065 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
3066 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3069 error = got_worktree_open(&worktree, worktree_path);
3073 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3077 if (!same_path_prefix) {
3078 error = got_error(GOT_ERR_PATH_PREFIX);
3082 if (commit_id_str) {
3083 struct got_reflist_head refs;
3085 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3089 error = got_repo_match_object_id(&commit_id, NULL,
3090 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3091 got_ref_list_free(&refs);
3094 error = check_linear_ancestry(commit_id,
3095 got_worktree_get_base_commit_id(worktree), 0, repo);
3096 if (error != NULL) {
3097 if (error->code == GOT_ERR_ANCESTRY) {
3098 error = checkout_ancestry_error(
3099 head_ref, commit_id_str);
3103 error = check_same_branch(commit_id, head_ref, NULL, repo);
3105 if (error->code == GOT_ERR_ANCESTRY) {
3106 error = checkout_ancestry_error(
3107 head_ref, commit_id_str);
3111 error = got_worktree_set_base_commit_id(worktree, repo,
3115 /* Expand potentially abbreviated commit ID string. */
3116 free(commit_id_str);
3117 error = got_object_id_str(&commit_id_str, commit_id);
3121 commit_id = got_object_id_dup(
3122 got_worktree_get_base_commit_id(worktree));
3123 if (commit_id == NULL) {
3124 error = got_error_from_errno("got_object_id_dup");
3127 error = got_object_id_str(&commit_id_str, commit_id);
3132 error = got_pathlist_append(&paths, "", NULL);
3135 cpa.worktree_path = worktree_path;
3136 cpa.had_base_commit_ref_error = 0;
3137 cpa.verbosity = verbosity;
3138 error = got_worktree_checkout_files(worktree, &paths, repo,
3139 checkout_progress, &cpa, check_cancelled, NULL);
3143 if (got_ref_is_symbolic(head_ref)) {
3144 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3147 refname = got_ref_get_name(ref);
3149 refname = got_ref_get_name(head_ref);
3150 printf("Checked out %s: %s\n", refname, commit_id_str);
3151 printf("Now shut up and hack\n");
3152 if (cpa.had_base_commit_ref_error)
3153 show_worktree_base_ref_warning();
3156 const struct got_error *pack_err =
3157 got_repo_pack_fds_close(pack_fds);
3162 got_ref_close(head_ref);
3165 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3166 free(commit_id_str);
3169 free(worktree_path);
3174 struct got_update_progress_arg {
3186 print_update_progress_stats(struct got_update_progress_arg *upa)
3188 if (!upa->did_something)
3191 if (upa->conflicts > 0)
3192 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3193 if (upa->obstructed > 0)
3194 printf("File paths obstructed by a non-regular file: %d\n",
3196 if (upa->not_updated > 0)
3197 printf("Files not updated because of existing merge "
3198 "conflicts: %d\n", upa->not_updated);
3202 * The meaning of some status codes differs between merge-style operations and
3203 * update operations. For example, the ! status code means "file was missing"
3204 * if changes were merged into the work tree, and "missing file was restored"
3205 * if the work tree was updated. This function should be used by any operation
3206 * which merges changes into the work tree without updating the work tree.
3209 print_merge_progress_stats(struct got_update_progress_arg *upa)
3211 if (!upa->did_something)
3214 if (upa->conflicts > 0)
3215 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3216 if (upa->obstructed > 0)
3217 printf("File paths obstructed by a non-regular file: %d\n",
3219 if (upa->missing > 0)
3220 printf("Files which had incoming changes but could not be "
3221 "found in the work tree: %d\n", upa->missing);
3222 if (upa->not_deleted > 0)
3223 printf("Files not deleted due to differences in deleted "
3224 "content: %d\n", upa->not_deleted);
3225 if (upa->unversioned > 0)
3226 printf("Files not merged because an unversioned file was "
3227 "found in the work tree: %d\n", upa->unversioned);
3233 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3234 "[path ...]\n", getprogname());
3238 static const struct got_error *
3239 update_progress(void *arg, unsigned char status, const char *path)
3241 struct got_update_progress_arg *upa = arg;
3243 if (status == GOT_STATUS_EXISTS ||
3244 status == GOT_STATUS_BASE_REF_ERR)
3247 upa->did_something = 1;
3249 /* Base commit bump happens silently. */
3250 if (status == GOT_STATUS_BUMP_BASE)
3253 if (status == GOT_STATUS_CONFLICT)
3255 if (status == GOT_STATUS_OBSTRUCTED)
3257 if (status == GOT_STATUS_CANNOT_UPDATE)
3259 if (status == GOT_STATUS_MISSING)
3261 if (status == GOT_STATUS_CANNOT_DELETE)
3263 if (status == GOT_STATUS_UNVERSIONED)
3266 while (path[0] == '/')
3268 if (upa->verbosity >= 0)
3269 printf("%c %s\n", status, path);
3274 static const struct got_error *
3275 switch_head_ref(struct got_reference *head_ref,
3276 struct got_object_id *commit_id, struct got_worktree *worktree,
3277 struct got_repository *repo)
3279 const struct got_error *err = NULL;
3281 int ref_has_moved = 0;
3283 /* Trivial case: switching between two different references. */
3284 if (strcmp(got_ref_get_name(head_ref),
3285 got_worktree_get_head_ref_name(worktree)) != 0) {
3286 printf("Switching work tree from %s to %s\n",
3287 got_worktree_get_head_ref_name(worktree),
3288 got_ref_get_name(head_ref));
3289 return got_worktree_set_head_ref(worktree, head_ref);
3292 err = check_linear_ancestry(commit_id,
3293 got_worktree_get_base_commit_id(worktree), 0, repo);
3295 if (err->code != GOT_ERR_ANCESTRY)
3302 /* Switching to a rebased branch with the same reference name. */
3303 err = got_object_id_str(&base_id_str,
3304 got_worktree_get_base_commit_id(worktree));
3307 printf("Reference %s now points at a different branch\n",
3308 got_worktree_get_head_ref_name(worktree));
3309 printf("Switching work tree from %s to %s\n", base_id_str,
3310 got_worktree_get_head_ref_name(worktree));
3314 static const struct got_error *
3315 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3317 const struct got_error *err;
3320 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3324 return got_error(GOT_ERR_REBASING);
3326 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3330 return got_error(GOT_ERR_HISTEDIT_BUSY);
3335 static const struct got_error *
3336 check_merge_in_progress(struct got_worktree *worktree,
3337 struct got_repository *repo)
3339 const struct got_error *err;
3342 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3346 return got_error(GOT_ERR_MERGE_BUSY);
3351 static const struct got_error *
3352 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3353 char *argv[], struct got_worktree *worktree)
3355 const struct got_error *err = NULL;
3357 struct got_pathlist_entry *new;
3363 return got_error_from_errno("strdup");
3364 return got_pathlist_append(paths, path, NULL);
3367 for (i = 0; i < argc; i++) {
3368 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3371 err = got_pathlist_insert(&new, paths, path, NULL);
3372 if (err || new == NULL /* duplicate */) {
3382 static const struct got_error *
3383 wrap_not_worktree_error(const struct got_error *orig_err,
3384 const char *cmdname, const char *path)
3386 const struct got_error *err;
3387 struct got_repository *repo;
3388 static char msg[512];
3389 int *pack_fds = NULL;
3391 err = got_repo_pack_fds_open(&pack_fds);
3395 err = got_repo_open(&repo, path, NULL, pack_fds);
3399 snprintf(msg, sizeof(msg),
3400 "'got %s' needs a work tree in addition to a git repository\n"
3401 "Work trees can be checked out from this Git repository with "
3403 "The got(1) manual page contains more information.", cmdname);
3404 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3405 got_repo_close(repo);
3407 const struct got_error *pack_err =
3408 got_repo_pack_fds_close(pack_fds);
3415 static const struct got_error *
3416 cmd_update(int argc, char *argv[])
3418 const struct got_error *error = NULL;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 char *worktree_path = NULL;
3422 struct got_object_id *commit_id = NULL;
3423 char *commit_id_str = NULL;
3424 const char *branch_name = NULL;
3425 struct got_reference *head_ref = NULL;
3426 struct got_pathlist_head paths;
3427 struct got_pathlist_entry *pe;
3428 int ch, verbosity = 0;
3429 struct got_update_progress_arg upa;
3430 int *pack_fds = NULL;
3434 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3437 branch_name = optarg;
3440 commit_id_str = strdup(optarg);
3441 if (commit_id_str == NULL)
3442 return got_error_from_errno("strdup");
3457 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3458 "unveil", NULL) == -1)
3461 worktree_path = getcwd(NULL, 0);
3462 if (worktree_path == NULL) {
3463 error = got_error_from_errno("getcwd");
3467 error = got_repo_pack_fds_open(&pack_fds);
3471 error = got_worktree_open(&worktree, worktree_path);
3473 if (error->code == GOT_ERR_NOT_WORKTREE)
3474 error = wrap_not_worktree_error(error, "update",
3479 error = check_rebase_or_histedit_in_progress(worktree);
3483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3488 error = apply_unveil(got_repo_get_path(repo), 0,
3489 got_worktree_get_root_path(worktree));
3493 error = check_merge_in_progress(worktree, repo);
3497 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3501 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3502 got_worktree_get_head_ref_name(worktree), 0);
3505 if (commit_id_str == NULL) {
3506 error = got_ref_resolve(&commit_id, repo, head_ref);
3509 error = got_object_id_str(&commit_id_str, commit_id);
3513 struct got_reflist_head refs;
3515 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3519 error = got_repo_match_object_id(&commit_id, NULL,
3520 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3521 got_ref_list_free(&refs);
3522 free(commit_id_str);
3523 commit_id_str = NULL;
3526 error = got_object_id_str(&commit_id_str, commit_id);
3532 struct got_object_id *head_commit_id;
3533 TAILQ_FOREACH(pe, &paths, entry) {
3534 if (pe->path_len == 0)
3536 error = got_error_msg(GOT_ERR_BAD_PATH,
3537 "switching between branches requires that "
3538 "the entire work tree gets updated");
3541 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3544 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3546 free(head_commit_id);
3549 error = check_same_branch(commit_id, head_ref, NULL, repo);
3552 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3556 error = check_linear_ancestry(commit_id,
3557 got_worktree_get_base_commit_id(worktree), 0, repo);
3558 if (error != NULL) {
3559 if (error->code == GOT_ERR_ANCESTRY)
3560 error = got_error(GOT_ERR_BRANCH_MOVED);
3563 error = check_same_branch(commit_id, head_ref, NULL, repo);
3568 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3570 error = got_worktree_set_base_commit_id(worktree, repo,
3576 memset(&upa, 0, sizeof(upa));
3577 upa.verbosity = verbosity;
3578 error = got_worktree_checkout_files(worktree, &paths, repo,
3579 update_progress, &upa, check_cancelled, NULL);
3583 if (upa.did_something) {
3584 printf("Updated to %s: %s\n",
3585 got_worktree_get_head_ref_name(worktree), commit_id_str);
3587 printf("Already up-to-date\n");
3589 print_update_progress_stats(&upa);
3592 const struct got_error *pack_err =
3593 got_repo_pack_fds_close(pack_fds);
3597 free(worktree_path);
3598 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3600 free(commit_id_str);
3604 static const struct got_error *
3605 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3606 const char *path, int diff_context, int ignore_whitespace,
3607 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3608 struct got_repository *repo, FILE *outfile)
3610 const struct got_error *err = NULL;
3611 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3612 FILE *f1 = NULL, *f2 = NULL;
3613 int fd1 = -1, fd2 = -1;
3615 fd1 = got_opentempfd();
3617 return got_error_from_errno("got_opentempfd");
3618 fd2 = got_opentempfd();
3620 err = got_error_from_errno("got_opentempfd");
3625 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3631 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3635 f1 = got_opentemp();
3637 err = got_error_from_errno("got_opentemp");
3640 f2 = got_opentemp();
3642 err = got_error_from_errno("got_opentemp");
3646 while (path[0] == '/')
3648 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3649 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3650 force_text_diff, dsa, outfile);
3652 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3653 err = got_error_from_errno("close");
3655 got_object_blob_close(blob1);
3656 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3657 err = got_error_from_errno("close");
3658 got_object_blob_close(blob2);
3659 if (f1 && fclose(f1) == EOF && err == NULL)
3660 err = got_error_from_errno("fclose");
3661 if (f2 && fclose(f2) == EOF && err == NULL)
3662 err = got_error_from_errno("fclose");
3666 static const struct got_error *
3667 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3668 const char *path, int diff_context, int ignore_whitespace,
3669 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3670 struct got_repository *repo, FILE *outfile)
3672 const struct got_error *err = NULL;
3673 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3674 struct got_diff_blob_output_unidiff_arg arg;
3675 FILE *f1 = NULL, *f2 = NULL;
3676 int fd1 = -1, fd2 = -1;
3679 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3682 fd1 = got_opentempfd();
3684 err = got_error_from_errno("got_opentempfd");
3689 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3693 f1 = got_opentemp();
3695 err = got_error_from_errno("got_opentemp");
3699 f2 = got_opentemp();
3701 err = got_error_from_errno("got_opentemp");
3704 fd2 = got_opentempfd();
3706 err = got_error_from_errno("got_opentempfd");
3709 arg.diff_context = diff_context;
3710 arg.ignore_whitespace = ignore_whitespace;
3711 arg.force_text_diff = force_text_diff;
3713 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3714 arg.outfile = outfile;
3717 while (path[0] == '/')
3719 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3720 got_diff_blob_output_unidiff, &arg, 1);
3723 got_object_tree_close(tree1);
3725 got_object_tree_close(tree2);
3726 if (f1 && fclose(f1) == EOF && err == NULL)
3727 err = got_error_from_errno("fclose");
3728 if (f2 && fclose(f2) == EOF && err == NULL)
3729 err = got_error_from_errno("fclose");
3730 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3731 err = got_error_from_errno("close");
3732 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3733 err = got_error_from_errno("close");
3737 static const struct got_error *
3738 get_changed_paths(struct got_pathlist_head *paths,
3739 struct got_commit_object *commit, struct got_repository *repo,
3740 struct got_diffstat_cb_arg *dsa)
3742 const struct got_error *err = NULL;
3743 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3744 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3745 struct got_object_qid *qid;
3746 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3747 FILE *f1 = NULL, *f2 = NULL;
3748 int fd1 = -1, fd2 = -1;
3751 cb = got_diff_tree_compute_diffstat;
3753 f1 = got_opentemp();
3755 err = got_error_from_errno("got_opentemp");
3758 f2 = got_opentemp();
3760 err = got_error_from_errno("got_opentemp");
3763 fd1 = got_opentempfd();
3765 err = got_error_from_errno("got_opentempfd");
3768 fd2 = got_opentempfd();
3770 err = got_error_from_errno("got_opentempfd");
3775 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3777 struct got_commit_object *pcommit;
3778 err = got_object_open_as_commit(&pcommit, repo,
3783 tree_id1 = got_object_id_dup(
3784 got_object_commit_get_tree_id(pcommit));
3785 if (tree_id1 == NULL) {
3786 got_object_commit_close(pcommit);
3787 return got_error_from_errno("got_object_id_dup");
3789 got_object_commit_close(pcommit);
3794 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3799 tree_id2 = got_object_commit_get_tree_id(commit);
3800 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3804 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3805 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3808 got_object_tree_close(tree1);
3810 got_object_tree_close(tree2);
3811 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3812 err = got_error_from_errno("close");
3813 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3814 err = got_error_from_errno("close");
3815 if (f1 && fclose(f1) == EOF && err == NULL)
3816 err = got_error_from_errno("fclose");
3817 if (f2 && fclose(f2) == EOF && err == NULL)
3818 err = got_error_from_errno("fclose");
3823 static const struct got_error *
3824 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3825 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3826 struct got_repository *repo, FILE *outfile)
3828 const struct got_error *err = NULL;
3829 struct got_commit_object *pcommit = NULL;
3830 char *id_str1 = NULL, *id_str2 = NULL;
3831 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3832 struct got_object_qid *qid;
3834 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3836 err = got_object_open_as_commit(&pcommit, repo,
3840 err = got_object_id_str(&id_str1, &qid->id);
3845 err = got_object_id_str(&id_str2, id);
3849 if (path && path[0] != '\0') {
3851 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3855 err = got_object_id_by_path(&obj_id1, repo,
3858 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3864 err = got_object_get_type(&obj_type, repo, obj_id2);
3870 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3871 fprintf(outfile, "commit - %s\n",
3872 id_str1 ? id_str1 : "/dev/null");
3873 fprintf(outfile, "commit + %s\n", id_str2);
3875 case GOT_OBJ_TYPE_BLOB:
3876 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3877 0, 0, dsa, repo, outfile);
3879 case GOT_OBJ_TYPE_TREE:
3880 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3881 0, 0, dsa, repo, outfile);
3884 err = got_error(GOT_ERR_OBJ_TYPE);
3890 obj_id2 = got_object_commit_get_tree_id(commit);
3892 obj_id1 = got_object_commit_get_tree_id(pcommit);
3894 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3895 fprintf(outfile, "commit - %s\n",
3896 id_str1 ? id_str1 : "/dev/null");
3897 fprintf(outfile, "commit + %s\n", id_str2);
3898 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
3899 dsa, repo, outfile);
3905 got_object_commit_close(pcommit);
3910 get_datestr(time_t *time, char *datebuf)
3912 struct tm mytm, *tm;
3915 tm = gmtime_r(time, &mytm);
3918 s = asctime_r(tm, datebuf);
3921 p = strchr(s, '\n');
3927 static const struct got_error *
3928 match_commit(int *have_match, struct got_object_id *id,
3929 struct got_commit_object *commit, regex_t *regex)
3931 const struct got_error *err = NULL;
3932 regmatch_t regmatch;
3933 char *id_str = NULL, *logmsg = NULL;
3937 err = got_object_id_str(&id_str, id);
3941 err = got_object_commit_get_logmsg(&logmsg, commit);
3945 if (regexec(regex, got_object_commit_get_author(commit), 1,
3946 ®match, 0) == 0 ||
3947 regexec(regex, got_object_commit_get_committer(commit), 1,
3948 ®match, 0) == 0 ||
3949 regexec(regex, id_str, 1, ®match, 0) == 0 ||
3950 regexec(regex, logmsg, 1, ®match, 0) == 0)
3959 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3962 regmatch_t regmatch;
3963 struct got_pathlist_entry *pe;
3967 TAILQ_FOREACH(pe, changed_paths, entry) {
3968 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3975 static const struct got_error *
3976 match_patch(int *have_match, struct got_commit_object *commit,
3977 struct got_object_id *id, const char *path, int diff_context,
3978 struct got_repository *repo, regex_t *regex, FILE *f)
3980 const struct got_error *err = NULL;
3982 size_t linesize = 0;
3983 regmatch_t regmatch;
3987 err = got_opentemp_truncate(f);
3991 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
3995 if (fseeko(f, 0L, SEEK_SET) == -1) {
3996 err = got_error_from_errno("fseeko");
4000 while (getline(&line, &linesize, f) != -1) {
4001 if (regexec(regex, line, 1, ®match, 0) == 0) {
4011 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4013 static const struct got_error*
4014 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4015 struct got_object_id *id, struct got_repository *repo,
4018 static const struct got_error *err = NULL;
4019 struct got_reflist_entry *re;
4025 TAILQ_FOREACH(re, refs, entry) {
4026 struct got_tag_object *tag = NULL;
4027 struct got_object_id *ref_id;
4030 name = got_ref_get_name(re->ref);
4031 if (strcmp(name, GOT_REF_HEAD) == 0)
4033 if (strncmp(name, "refs/", 5) == 0)
4035 if (strncmp(name, "got/", 4) == 0)
4037 if (strncmp(name, "heads/", 6) == 0)
4039 if (strncmp(name, "remotes/", 8) == 0) {
4043 s = strstr(name, "/" GOT_REF_HEAD);
4044 if (s != NULL && s[strlen(s)] == '\0')
4047 err = got_ref_resolve(&ref_id, repo, re->ref);
4050 if (strncmp(name, "tags/", 5) == 0) {
4051 err = got_object_open_as_tag(&tag, repo, ref_id);
4053 if (err->code != GOT_ERR_OBJ_TYPE) {
4057 /* Ref points at something other than a tag. */
4062 cmp = got_object_id_cmp(tag ?
4063 got_object_tag_get_object_id(tag) : ref_id, id);
4066 got_object_tag_close(tag);
4070 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4071 s ? ", " : "", name) == -1) {
4072 err = got_error_from_errno("asprintf");
4083 static const struct got_error *
4084 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4085 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4087 const struct got_error *err = NULL;
4088 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4089 char *comma, *s, *nl;
4090 struct got_reflist_head *refs;
4091 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4093 time_t committer_time;
4095 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4097 err = build_refs_str(&ref_str, refs, id, repo, 1);
4101 /* Display the first matching ref only. */
4102 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4106 if (ref_str == NULL) {
4107 err = got_object_id_str(&id_str, id);
4112 committer_time = got_object_commit_get_committer_time(commit);
4113 if (gmtime_r(&committer_time, &tm) == NULL) {
4114 err = got_error_from_errno("gmtime_r");
4117 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4118 err = got_error(GOT_ERR_NO_SPACE);
4122 err = got_object_commit_get_logmsg(&logmsg0, commit);
4127 while (isspace((unsigned char)s[0]))
4130 nl = strchr(s, '\n');
4136 printf("%s%-7s %s\n", datebuf, ref_str, s);
4138 printf("%s%.7s %s\n", datebuf, id_str, s);
4140 if (fflush(stdout) != 0 && err == NULL)
4141 err = got_error_from_errno("fflush");
4149 static const struct got_error *
4150 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4152 struct got_pathlist_entry *pe;
4155 printf("%s\n", header);
4157 TAILQ_FOREACH(pe, dsa->paths, entry) {
4158 struct got_diff_changed_path *cp = pe->data;
4159 int pad = dsa->max_path_len - pe->path_len + 1;
4161 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4162 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4164 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4165 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4166 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4168 if (fflush(stdout) != 0)
4169 return got_error_from_errno("fflush");
4174 static const struct got_error *
4180 if (fseeko(f, 0L, SEEK_SET) == -1)
4181 return got_error_from_errno("fseek");
4184 r = fread(buf, 1, sizeof(buf), f);
4187 return got_error_from_errno("fread");
4191 if (fwrite(buf, 1, r, stdout) != r)
4192 return got_ferror(stdout, GOT_ERR_IO);
4198 static const struct got_error *
4199 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4200 struct got_repository *repo, const char *path,
4201 struct got_pathlist_head *changed_paths,
4202 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4203 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4206 const struct got_error *err = NULL;
4208 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4210 time_t committer_time;
4211 const char *author, *committer;
4212 char *refs_str = NULL;
4214 err = got_object_id_str(&id_str, id);
4218 if (custom_refs_str == NULL) {
4219 struct got_reflist_head *refs;
4220 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4222 err = build_refs_str(&refs_str, refs, id, repo, 0);
4228 printf(GOT_COMMIT_SEP_STR);
4229 if (custom_refs_str)
4230 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4233 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4234 refs_str ? " (" : "", refs_str ? refs_str : "",
4235 refs_str ? ")" : "");
4240 printf("from: %s\n", got_object_commit_get_author(commit));
4241 author = got_object_commit_get_author(commit);
4242 committer = got_object_commit_get_committer(commit);
4243 if (strcmp(author, committer) != 0)
4244 printf("via: %s\n", committer);
4245 committer_time = got_object_commit_get_committer_time(commit);
4246 datestr = get_datestr(&committer_time, datebuf);
4248 printf("date: %s UTC\n", datestr);
4249 if (got_object_commit_get_nparents(commit) > 1) {
4250 const struct got_object_id_queue *parent_ids;
4251 struct got_object_qid *qid;
4253 parent_ids = got_object_commit_get_parent_ids(commit);
4254 STAILQ_FOREACH(qid, parent_ids, entry) {
4255 err = got_object_id_str(&id_str, &qid->id);
4258 printf("parent %d: %s\n", n++, id_str);
4264 err = got_object_commit_get_logmsg(&logmsg0, commit);
4270 line = strsep(&logmsg, "\n");
4272 printf(" %s\n", line);
4276 if (changed_paths && diffstat == NULL) {
4277 struct got_pathlist_entry *pe;
4279 TAILQ_FOREACH(pe, changed_paths, entry) {
4280 struct got_diff_changed_path *cp = pe->data;
4282 printf(" %c %s\n", cp->status, pe->path);
4290 err = got_error_from_errno("got_opentemp");
4295 err = print_patch(commit, id, path, diff_context, diffstat,
4296 repo, diffstat == NULL ? stdout : f);
4301 err = print_diffstat(diffstat, NULL);
4313 if (fflush(stdout) != 0 && err == NULL)
4314 err = got_error_from_errno("fflush");
4316 if (f && fclose(f) == EOF && err == NULL)
4317 err = got_error_from_errno("fclose");
4323 static const struct got_error *
4324 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4325 struct got_repository *repo, const char *path, int show_changed_paths,
4326 int show_diffstat, int show_patch, const char *search_pattern,
4327 int diff_context, int limit, int log_branches, int reverse_display_order,
4328 struct got_reflist_object_id_map *refs_idmap, int one_line,
4331 const struct got_error *err;
4332 struct got_commit_graph *graph;
4335 struct got_object_id_queue reversed_commits;
4336 struct got_object_qid *qid;
4337 struct got_commit_object *commit;
4338 struct got_pathlist_head changed_paths;
4340 STAILQ_INIT(&reversed_commits);
4341 TAILQ_INIT(&changed_paths);
4343 if (search_pattern && regcomp(®ex, search_pattern,
4344 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4345 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4347 err = got_commit_graph_open(&graph, path, !log_branches);
4350 err = got_commit_graph_iter_start(graph, root_id, repo,
4351 check_cancelled, NULL);
4355 struct got_object_id id;
4356 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4357 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4359 if (sigint_received || sigpipe_received)
4362 err = got_commit_graph_iter_next(&id, graph, repo,
4363 check_cancelled, NULL);
4365 if (err->code == GOT_ERR_ITER_COMPLETED)
4370 err = got_object_open_as_commit(&commit, repo, &id);
4374 if ((show_changed_paths || (show_diffstat && !show_patch))
4375 && !reverse_display_order) {
4376 err = get_changed_paths(&changed_paths, commit, repo,
4377 show_diffstat ? &dsa : NULL);
4382 if (search_pattern) {
4383 err = match_commit(&have_match, &id, commit, ®ex);
4385 got_object_commit_close(commit);
4388 if (have_match == 0 && show_changed_paths)
4389 match_changed_paths(&have_match,
4390 &changed_paths, ®ex);
4391 if (have_match == 0 && show_patch) {
4392 err = match_patch(&have_match, commit, &id,
4393 path, diff_context, repo, ®ex, tmpfile);
4397 if (have_match == 0) {
4398 got_object_commit_close(commit);
4399 got_pathlist_free(&changed_paths,
4400 GOT_PATHLIST_FREE_ALL);
4405 if (reverse_display_order) {
4406 err = got_object_qid_alloc(&qid, &id);
4409 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4410 got_object_commit_close(commit);
4413 err = print_commit_oneline(commit, &id,
4416 err = print_commit(commit, &id, repo, path,
4417 (show_changed_paths || show_diffstat) ?
4418 &changed_paths : NULL,
4419 show_diffstat ? &dsa : NULL, show_patch,
4420 diff_context, refs_idmap, NULL, NULL);
4421 got_object_commit_close(commit);
4425 if ((limit && --limit == 0) ||
4426 (end_id && got_object_id_cmp(&id, end_id) == 0))
4429 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4431 if (reverse_display_order) {
4432 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4433 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4434 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4436 err = got_object_open_as_commit(&commit, repo,
4440 if (show_changed_paths ||
4441 (show_diffstat && !show_patch)) {
4442 err = get_changed_paths(&changed_paths, commit,
4443 repo, show_diffstat ? &dsa : NULL);
4448 err = print_commit_oneline(commit, &qid->id,
4451 err = print_commit(commit, &qid->id, repo, path,
4452 (show_changed_paths || show_diffstat) ?
4453 &changed_paths : NULL,
4454 show_diffstat ? &dsa : NULL, show_patch,
4455 diff_context, refs_idmap, NULL, NULL);
4456 got_object_commit_close(commit);
4459 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4463 while (!STAILQ_EMPTY(&reversed_commits)) {
4464 qid = STAILQ_FIRST(&reversed_commits);
4465 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4466 got_object_qid_free(qid);
4468 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4471 got_commit_graph_close(graph);
4478 fprintf(stderr, "usage: %s log [-bdPpRs] [-C number] [-c commit] "
4479 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4480 "[path]\n", getprogname());
4485 get_default_log_limit(void)
4487 const char *got_default_log_limit;
4491 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4492 if (got_default_log_limit == NULL)
4494 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4500 static const struct got_error *
4501 cmd_log(int argc, char *argv[])
4503 const struct got_error *error;
4504 struct got_repository *repo = NULL;
4505 struct got_worktree *worktree = NULL;
4506 struct got_object_id *start_id = NULL, *end_id = NULL;
4507 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4508 const char *start_commit = NULL, *end_commit = NULL;
4509 const char *search_pattern = NULL;
4510 int diff_context = -1, ch;
4511 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4512 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4514 struct got_reflist_head refs;
4515 struct got_reflist_object_id_map *refs_idmap = NULL;
4516 FILE *tmpfile = NULL;
4517 int *pack_fds = NULL;
4522 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4528 limit = get_default_log_limit();
4530 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:sx:")) != -1) {
4536 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4539 errx(1, "number of context lines is %s: %s",
4543 start_commit = optarg;
4549 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4551 errx(1, "number of commits is %s: %s",
4555 show_changed_paths = 1;
4561 reverse_display_order = 1;
4564 repo_path = realpath(optarg, NULL);
4565 if (repo_path == NULL)
4566 return got_error_from_errno2("realpath",
4568 got_path_strip_trailing_slashes(repo_path);
4571 search_pattern = optarg;
4577 end_commit = optarg;
4588 if (diff_context == -1)
4590 else if (!show_patch)
4591 errx(1, "-C requires -p");
4593 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4594 errx(1, "cannot use -s with -d, -p or -P");
4596 cwd = getcwd(NULL, 0);
4598 error = got_error_from_errno("getcwd");
4602 error = got_repo_pack_fds_open(&pack_fds);
4606 if (repo_path == NULL) {
4607 error = got_worktree_open(&worktree, cwd);
4608 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4615 error = got_worktree_resolve_path(&path, worktree,
4620 path = strdup(argv[0]);
4622 error = got_error_from_errno("strdup");
4626 } else if (argc != 0)
4629 if (repo_path == NULL) {
4630 repo_path = worktree ?
4631 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4633 if (repo_path == NULL) {
4634 error = got_error_from_errno("strdup");
4638 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4642 error = apply_unveil(got_repo_get_path(repo), 1,
4643 worktree ? got_worktree_get_root_path(worktree) : NULL);
4647 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4651 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4655 if (start_commit == NULL) {
4656 struct got_reference *head_ref;
4657 struct got_commit_object *commit = NULL;
4658 error = got_ref_open(&head_ref, repo,
4659 worktree ? got_worktree_get_head_ref_name(worktree)
4663 error = got_ref_resolve(&start_id, repo, head_ref);
4664 got_ref_close(head_ref);
4667 error = got_object_open_as_commit(&commit, repo,
4671 got_object_commit_close(commit);
4673 error = got_repo_match_object_id(&start_id, NULL,
4674 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4678 if (end_commit != NULL) {
4679 error = got_repo_match_object_id(&end_id, NULL,
4680 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4687 * If a path was specified on the command line it was resolved
4688 * to a path in the work tree above. Prepend the work tree's
4689 * path prefix to obtain the corresponding in-repository path.
4693 prefix = got_worktree_get_path_prefix(worktree);
4694 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4695 (path[0] != '\0') ? "/" : "", path) == -1) {
4696 error = got_error_from_errno("asprintf");
4701 error = got_repo_map_path(&in_repo_path, repo,
4707 path = in_repo_path;
4711 /* Release work tree lock. */
4712 got_worktree_close(worktree);
4716 if (search_pattern && show_patch) {
4717 tmpfile = got_opentemp();
4718 if (tmpfile == NULL) {
4719 error = got_error_from_errno("got_opentemp");
4724 error = print_commits(start_id, end_id, repo, path ? path : "",
4725 show_changed_paths, show_diffstat, show_patch, search_pattern,
4726 diff_context, limit, log_branches, reverse_display_order,
4727 refs_idmap, one_line, tmpfile);
4733 got_worktree_close(worktree);
4735 const struct got_error *close_err = got_repo_close(repo);
4740 const struct got_error *pack_err =
4741 got_repo_pack_fds_close(pack_fds);
4746 got_reflist_object_id_map_free(refs_idmap);
4747 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4748 error = got_error_from_errno("fclose");
4749 got_ref_list_free(&refs);
4756 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4757 "[-r repository-path] [object1 object2 | path ...]\n",
4762 struct print_diff_arg {
4763 struct got_repository *repo;
4764 struct got_worktree *worktree;
4765 struct got_diffstat_cb_arg *diffstat;
4770 enum got_diff_algorithm diff_algo;
4771 int ignore_whitespace;
4772 int force_text_diff;
4779 * Create a file which contains the target path of a symlink so we can feed
4780 * it as content to the diff engine.
4782 static const struct got_error *
4783 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4784 const char *abspath)
4786 const struct got_error *err = NULL;
4787 char target_path[PATH_MAX];
4788 ssize_t target_len, outlen;
4793 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4794 if (target_len == -1)
4795 return got_error_from_errno2("readlinkat", abspath);
4797 target_len = readlink(abspath, target_path, PATH_MAX);
4798 if (target_len == -1)
4799 return got_error_from_errno2("readlink", abspath);
4802 *fd = got_opentempfd();
4804 return got_error_from_errno("got_opentempfd");
4806 outlen = write(*fd, target_path, target_len);
4808 err = got_error_from_errno("got_opentempfd");
4812 if (lseek(*fd, 0, SEEK_SET) == -1) {
4813 err = got_error_from_errno2("lseek", abspath);
4824 static const struct got_error *
4825 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4826 const char *path, struct got_object_id *blob_id,
4827 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4828 int dirfd, const char *de_name)
4830 struct print_diff_arg *a = arg;
4831 const struct got_error *err = NULL;
4832 struct got_blob_object *blob1 = NULL;
4833 int fd = -1, fd1 = -1, fd2 = -1;
4835 char *abspath = NULL, *label1 = NULL;
4840 memset(&sb, 0, sizeof(sb));
4842 if (a->diff_staged) {
4843 if (staged_status != GOT_STATUS_MODIFY &&
4844 staged_status != GOT_STATUS_ADD &&
4845 staged_status != GOT_STATUS_DELETE)
4848 if (staged_status == GOT_STATUS_DELETE)
4850 if (status == GOT_STATUS_NONEXISTENT)
4851 return got_error_set_errno(ENOENT, path);
4852 if (status != GOT_STATUS_MODIFY &&
4853 status != GOT_STATUS_ADD &&
4854 status != GOT_STATUS_DELETE &&
4855 status != GOT_STATUS_CONFLICT)
4859 err = got_opentemp_truncate(a->f1);
4861 return got_error_from_errno("got_opentemp_truncate");
4862 err = got_opentemp_truncate(a->f2);
4864 return got_error_from_errno("got_opentemp_truncate");
4866 if (!a->header_shown) {
4867 if (fprintf(a->outfile, "diff %s%s\n",
4868 a->diff_staged ? "-s " : "",
4869 got_worktree_get_root_path(a->worktree)) < 0) {
4870 err = got_error_from_errno("fprintf");
4873 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
4874 err = got_error_from_errno("fprintf");
4877 if (fprintf(a->outfile, "path + %s%s\n",
4878 got_worktree_get_root_path(a->worktree),
4879 a->diff_staged ? " (staged changes)" : "") < 0) {
4880 err = got_error_from_errno("fprintf");
4883 a->header_shown = 1;
4886 if (a->diff_staged) {
4887 const char *label1 = NULL, *label2 = NULL;
4888 switch (staged_status) {
4889 case GOT_STATUS_MODIFY:
4893 case GOT_STATUS_ADD:
4896 case GOT_STATUS_DELETE:
4900 return got_error(GOT_ERR_FILE_STATUS);
4902 fd1 = got_opentempfd();
4904 err = got_error_from_errno("got_opentempfd");
4907 fd2 = got_opentempfd();
4909 err = got_error_from_errno("got_opentempfd");
4912 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
4913 fd1, fd2, blob_id, staged_blob_id, label1, label2,
4914 a->diff_algo, a->diff_context, a->ignore_whitespace,
4915 a->force_text_diff, a->diffstat, a->repo, a->outfile);
4919 fd1 = got_opentempfd();
4921 err = got_error_from_errno("got_opentempfd");
4925 if (staged_status == GOT_STATUS_ADD ||
4926 staged_status == GOT_STATUS_MODIFY) {
4928 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
4932 err = got_object_id_str(&id_str, staged_blob_id);
4935 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
4936 err = got_error_from_errno("asprintf");
4941 } else if (status != GOT_STATUS_ADD) {
4942 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
4948 if (status != GOT_STATUS_DELETE) {
4949 if (asprintf(&abspath, "%s/%s",
4950 got_worktree_get_root_path(a->worktree), path) == -1) {
4951 err = got_error_from_errno("asprintf");
4956 fd = openat(dirfd, de_name,
4957 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4959 if (!got_err_open_nofollow_on_symlink()) {
4960 err = got_error_from_errno2("openat",
4964 err = get_symlink_target_file(&fd, dirfd,
4970 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
4972 if (!got_err_open_nofollow_on_symlink()) {
4973 err = got_error_from_errno2("open",
4977 err = get_symlink_target_file(&fd, dirfd,
4983 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
4984 err = got_error_from_errno2("fstatat", abspath);
4987 f2 = fdopen(fd, "r");
4989 err = got_error_from_errno2("fdopen", abspath);
4997 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5003 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5004 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5005 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5007 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5008 err = got_error_from_errno("close");
5009 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5010 err = got_error_from_errno("close");
5012 got_object_blob_close(blob1);
5013 if (fd != -1 && close(fd) == -1 && err == NULL)
5014 err = got_error_from_errno("close");
5015 if (f2 && fclose(f2) == EOF && err == NULL)
5016 err = got_error_from_errno("fclose");
5021 static const struct got_error *
5022 cmd_diff(int argc, char *argv[])
5024 const struct got_error *error;
5025 struct got_repository *repo = NULL;
5026 struct got_worktree *worktree = NULL;
5027 char *cwd = NULL, *repo_path = NULL;
5028 const char *commit_args[2] = { NULL, NULL };
5029 int ncommit_args = 0;
5030 struct got_object_id *ids[2] = { NULL, NULL };
5031 char *labels[2] = { NULL, NULL };
5032 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5033 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5034 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5036 struct got_reflist_head refs;
5037 struct got_pathlist_head diffstat_paths, paths;
5038 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5039 int fd1 = -1, fd2 = -1;
5040 int *pack_fds = NULL;
5041 struct got_diffstat_cb_arg dsa;
5043 memset(&dsa, 0, sizeof(dsa));
5047 TAILQ_INIT(&diffstat_paths);
5050 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5055 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5058 force_text_diff = 1;
5061 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5064 errx(1, "number of context lines is %s: %s",
5068 if (ncommit_args >= 2)
5069 errx(1, "too many -c options used");
5070 commit_args[ncommit_args++] = optarg;
5079 repo_path = realpath(optarg, NULL);
5080 if (repo_path == NULL)
5081 return got_error_from_errno2("realpath",
5083 got_path_strip_trailing_slashes(repo_path);
5090 ignore_whitespace = 1;
5101 cwd = getcwd(NULL, 0);
5103 error = got_error_from_errno("getcwd");
5107 error = got_repo_pack_fds_open(&pack_fds);
5111 if (repo_path == NULL) {
5112 error = got_worktree_open(&worktree, cwd);
5113 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5119 strdup(got_worktree_get_repo_path(worktree));
5120 if (repo_path == NULL) {
5121 error = got_error_from_errno("strdup");
5125 repo_path = strdup(cwd);
5126 if (repo_path == NULL) {
5127 error = got_error_from_errno("strdup");
5133 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5138 if (show_diffstat) {
5139 dsa.paths = &diffstat_paths;
5140 dsa.force_text = force_text_diff;
5141 dsa.ignore_ws = ignore_whitespace;
5142 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5145 if (rflag || worktree == NULL || ncommit_args > 0) {
5147 error = got_error_msg(GOT_ERR_NOT_IMPL,
5148 "-P option can only be used when diffing "
5153 error = got_error_msg(GOT_ERR_NOT_IMPL,
5154 "-s option can only be used when diffing "
5160 error = apply_unveil(got_repo_get_path(repo), 1,
5161 worktree ? got_worktree_get_root_path(worktree) : NULL);
5165 if ((!force_path && argc == 2) || ncommit_args > 0) {
5166 int obj_type = (ncommit_args > 0 ?
5167 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5168 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5172 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5174 if (ncommit_args > 0)
5175 arg = commit_args[i];
5178 error = got_repo_match_object_id(&ids[i], &labels[i],
5179 arg, obj_type, &refs, repo);
5181 if (error->code != GOT_ERR_NOT_REF &&
5182 error->code != GOT_ERR_NO_OBJ)
5184 if (ncommit_args > 0)
5192 f1 = got_opentemp();
5194 error = got_error_from_errno("got_opentemp");
5198 f2 = got_opentemp();
5200 error = got_error_from_errno("got_opentemp");
5204 outfile = got_opentemp();
5205 if (outfile == NULL) {
5206 error = got_error_from_errno("got_opentemp");
5210 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5211 struct print_diff_arg arg;
5214 if (worktree == NULL) {
5215 if (argc == 2 && ids[0] == NULL) {
5216 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5218 } else if (argc == 2 && ids[1] == NULL) {
5219 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5221 } else if (argc > 0) {
5222 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5223 "%s", "specified paths cannot be resolved");
5226 error = got_error(GOT_ERR_NOT_WORKTREE);
5231 error = get_worktree_paths_from_argv(&paths, argc, argv,
5236 error = got_object_id_str(&id_str,
5237 got_worktree_get_base_commit_id(worktree));
5241 arg.worktree = worktree;
5242 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5243 arg.diff_context = diff_context;
5244 arg.id_str = id_str;
5245 arg.header_shown = 0;
5246 arg.diff_staged = diff_staged;
5247 arg.ignore_whitespace = ignore_whitespace;
5248 arg.force_text_diff = force_text_diff;
5249 arg.diffstat = show_diffstat ? &dsa : NULL;
5252 arg.outfile = outfile;
5254 error = got_worktree_status(worktree, &paths, repo, 0,
5255 print_diff, &arg, check_cancelled, NULL);
5260 if (show_diffstat && dsa.nfiles > 0) {
5263 if (asprintf(&header, "diffstat %s%s",
5264 diff_staged ? "-s " : "",
5265 got_worktree_get_root_path(worktree)) == -1) {
5266 error = got_error_from_errno("asprintf");
5270 error = print_diffstat(&dsa, header);
5276 error = printfile(outfile);
5280 if (ncommit_args == 1) {
5281 struct got_commit_object *commit;
5282 error = got_object_open_as_commit(&commit, repo, ids[0]);
5286 labels[1] = labels[0];
5288 if (got_object_commit_get_nparents(commit) > 0) {
5289 const struct got_object_id_queue *pids;
5290 struct got_object_qid *pid;
5291 pids = got_object_commit_get_parent_ids(commit);
5292 pid = STAILQ_FIRST(pids);
5293 ids[0] = got_object_id_dup(&pid->id);
5294 if (ids[0] == NULL) {
5295 error = got_error_from_errno(
5296 "got_object_id_dup");
5297 got_object_commit_close(commit);
5300 error = got_object_id_str(&labels[0], ids[0]);
5302 got_object_commit_close(commit);
5307 labels[0] = strdup("/dev/null");
5308 if (labels[0] == NULL) {
5309 error = got_error_from_errno("strdup");
5310 got_object_commit_close(commit);
5315 got_object_commit_close(commit);
5318 if (ncommit_args == 0 && argc > 2) {
5319 error = got_error_msg(GOT_ERR_BAD_PATH,
5320 "path arguments cannot be used when diffing two objects");
5325 error = got_object_get_type(&type1, repo, ids[0]);
5330 error = got_object_get_type(&type2, repo, ids[1]);
5333 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5334 error = got_error(GOT_ERR_OBJ_TYPE);
5337 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5338 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5339 "path arguments cannot be used when diffing blobs");
5343 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5345 struct got_pathlist_entry *new;
5349 error = got_worktree_resolve_path(&p, worktree,
5353 prefix = got_worktree_get_path_prefix(worktree);
5354 while (prefix[0] == '/')
5356 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5357 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5359 error = got_error_from_errno("asprintf");
5365 char *mapped_path, *s;
5366 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5372 in_repo_path = strdup(s);
5373 if (in_repo_path == NULL) {
5374 error = got_error_from_errno("asprintf");
5381 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5382 if (error || new == NULL /* duplicate */)
5389 /* Release work tree lock. */
5390 got_worktree_close(worktree);
5394 fd1 = got_opentempfd();
5396 error = got_error_from_errno("got_opentempfd");
5400 fd2 = got_opentempfd();
5402 error = got_error_from_errno("got_opentempfd");
5406 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5407 case GOT_OBJ_TYPE_BLOB:
5408 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5409 fd1, fd2, ids[0], ids[1], NULL, NULL,
5410 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5411 ignore_whitespace, force_text_diff,
5412 show_diffstat ? &dsa : NULL, repo, outfile);
5414 case GOT_OBJ_TYPE_TREE:
5415 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5416 ids[0], ids[1], &paths, "", "",
5417 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5418 ignore_whitespace, force_text_diff,
5419 show_diffstat ? &dsa : NULL, repo, outfile);
5421 case GOT_OBJ_TYPE_COMMIT:
5422 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5423 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5424 fd1, fd2, ids[0], ids[1], &paths,
5425 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5426 ignore_whitespace, force_text_diff,
5427 show_diffstat ? &dsa : NULL, repo, outfile);
5430 error = got_error(GOT_ERR_OBJ_TYPE);
5435 if (show_diffstat && dsa.nfiles > 0) {
5436 char *header = NULL;
5438 if (asprintf(&header, "diffstat %s %s",
5439 labels[0], labels[1]) == -1) {
5440 error = got_error_from_errno("asprintf");
5444 error = print_diffstat(&dsa, header);
5450 error = printfile(outfile);
5458 got_worktree_close(worktree);
5460 const struct got_error *close_err = got_repo_close(repo);
5465 const struct got_error *pack_err =
5466 got_repo_pack_fds_close(pack_fds);
5470 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5471 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5472 got_ref_list_free(&refs);
5473 if (outfile && fclose(outfile) == EOF && error == NULL)
5474 error = got_error_from_errno("fclose");
5475 if (f1 && fclose(f1) == EOF && error == NULL)
5476 error = got_error_from_errno("fclose");
5477 if (f2 && fclose(f2) == EOF && error == NULL)
5478 error = got_error_from_errno("fclose");
5479 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5480 error = got_error_from_errno("close");
5481 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5482 error = got_error_from_errno("close");
5490 "usage: %s blame [-c commit] [-r repository-path] path\n",
5499 char datebuf[11]; /* YYYY-MM-DD + NUL */
5502 struct blame_cb_args {
5503 struct blame_line *lines;
5507 off_t *line_offsets;
5509 struct got_repository *repo;
5512 static const struct got_error *
5513 blame_cb(void *arg, int nlines, int lineno,
5514 struct got_commit_object *commit, struct got_object_id *id)
5516 const struct got_error *err = NULL;
5517 struct blame_cb_args *a = arg;
5518 struct blame_line *bline;
5520 size_t linesize = 0;
5523 time_t committer_time;
5525 if (nlines != a->nlines ||
5526 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5527 return got_error(GOT_ERR_RANGE);
5529 if (sigint_received)
5530 return got_error(GOT_ERR_ITER_COMPLETED);
5533 return NULL; /* no change in this commit */
5535 /* Annotate this line. */
5536 bline = &a->lines[lineno - 1];
5537 if (bline->annotated)
5539 err = got_object_id_str(&bline->id_str, id);
5543 bline->committer = strdup(got_object_commit_get_committer(commit));
5544 if (bline->committer == NULL) {
5545 err = got_error_from_errno("strdup");
5549 committer_time = got_object_commit_get_committer_time(commit);
5550 if (gmtime_r(&committer_time, &tm) == NULL)
5551 return got_error_from_errno("gmtime_r");
5552 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5554 err = got_error(GOT_ERR_NO_SPACE);
5557 bline->annotated = 1;
5559 /* Print lines annotated so far. */
5560 bline = &a->lines[a->lineno_cur - 1];
5561 if (!bline->annotated)
5564 offset = a->line_offsets[a->lineno_cur - 1];
5565 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5566 err = got_error_from_errno("fseeko");
5570 while (a->lineno_cur <= a->nlines && bline->annotated) {
5571 char *smallerthan, *at, *nl, *committer;
5574 if (getline(&line, &linesize, a->f) == -1) {
5576 err = got_error_from_errno("getline");
5580 committer = bline->committer;
5581 smallerthan = strchr(committer, '<');
5582 if (smallerthan && smallerthan[1] != '\0')
5583 committer = smallerthan + 1;
5584 at = strchr(committer, '@');
5587 len = strlen(committer);
5589 committer[8] = '\0';
5591 nl = strchr(line, '\n');
5594 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5595 bline->id_str, bline->datebuf, committer, line);
5598 bline = &a->lines[a->lineno_cur - 1];
5605 static const struct got_error *
5606 cmd_blame(int argc, char *argv[])
5608 const struct got_error *error;
5609 struct got_repository *repo = NULL;
5610 struct got_worktree *worktree = NULL;
5611 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5612 char *link_target = NULL;
5613 struct got_object_id *obj_id = NULL;
5614 struct got_object_id *commit_id = NULL;
5615 struct got_commit_object *commit = NULL;
5616 struct got_blob_object *blob = NULL;
5617 char *commit_id_str = NULL;
5618 struct blame_cb_args bca;
5619 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5621 int *pack_fds = NULL;
5622 FILE *f1 = NULL, *f2 = NULL;
5624 fd1 = got_opentempfd();
5626 return got_error_from_errno("got_opentempfd");
5628 memset(&bca, 0, sizeof(bca));
5631 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5636 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5639 commit_id_str = optarg;
5642 repo_path = realpath(optarg, NULL);
5643 if (repo_path == NULL)
5644 return got_error_from_errno2("realpath",
5646 got_path_strip_trailing_slashes(repo_path);
5662 cwd = getcwd(NULL, 0);
5664 error = got_error_from_errno("getcwd");
5668 error = got_repo_pack_fds_open(&pack_fds);
5672 if (repo_path == NULL) {
5673 error = got_worktree_open(&worktree, cwd);
5674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5680 strdup(got_worktree_get_repo_path(worktree));
5681 if (repo_path == NULL) {
5682 error = got_error_from_errno("strdup");
5687 repo_path = strdup(cwd);
5688 if (repo_path == NULL) {
5689 error = got_error_from_errno("strdup");
5695 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5700 const char *prefix = got_worktree_get_path_prefix(worktree);
5703 error = got_worktree_resolve_path(&p, worktree, path);
5706 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5707 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5709 error = got_error_from_errno("asprintf");
5714 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5716 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5719 error = got_repo_map_path(&in_repo_path, repo, path);
5724 if (commit_id_str == NULL) {
5725 struct got_reference *head_ref;
5726 error = got_ref_open(&head_ref, repo, worktree ?
5727 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5730 error = got_ref_resolve(&commit_id, repo, head_ref);
5731 got_ref_close(head_ref);
5735 struct got_reflist_head refs;
5737 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5741 error = got_repo_match_object_id(&commit_id, NULL,
5742 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5743 got_ref_list_free(&refs);
5749 /* Release work tree lock. */
5750 got_worktree_close(worktree);
5754 error = got_object_open_as_commit(&commit, repo, commit_id);
5758 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5763 error = got_object_id_by_path(&obj_id, repo, commit,
5764 link_target ? link_target : in_repo_path);
5768 error = got_object_get_type(&obj_type, repo, obj_id);
5772 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5773 error = got_error_path(link_target ? link_target : in_repo_path,
5778 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5781 bca.f = got_opentemp();
5782 if (bca.f == NULL) {
5783 error = got_error_from_errno("got_opentemp");
5786 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5787 &bca.line_offsets, bca.f, blob);
5788 if (error || bca.nlines == 0)
5791 /* Don't include \n at EOF in the blame line count. */
5792 if (bca.line_offsets[bca.nlines - 1] == filesize)
5795 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5796 if (bca.lines == NULL) {
5797 error = got_error_from_errno("calloc");
5801 bca.nlines_prec = 0;
5809 fd2 = got_opentempfd();
5811 error = got_error_from_errno("got_opentempfd");
5814 fd3 = got_opentempfd();
5816 error = got_error_from_errno("got_opentempfd");
5819 f1 = got_opentemp();
5821 error = got_error_from_errno("got_opentemp");
5824 f2 = got_opentemp();
5826 error = got_error_from_errno("got_opentemp");
5829 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
5830 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
5831 check_cancelled, NULL, fd2, fd3, f1, f2);
5840 got_object_commit_close(commit);
5842 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5843 error = got_error_from_errno("close");
5844 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5845 error = got_error_from_errno("close");
5846 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
5847 error = got_error_from_errno("close");
5848 if (f1 && fclose(f1) == EOF && error == NULL)
5849 error = got_error_from_errno("fclose");
5850 if (f2 && fclose(f2) == EOF && error == NULL)
5851 error = got_error_from_errno("fclose");
5854 got_object_blob_close(blob);
5856 got_worktree_close(worktree);
5858 const struct got_error *close_err = got_repo_close(repo);
5863 const struct got_error *pack_err =
5864 got_repo_pack_fds_close(pack_fds);
5869 for (i = 0; i < bca.nlines; i++) {
5870 struct blame_line *bline = &bca.lines[i];
5871 free(bline->id_str);
5872 free(bline->committer);
5876 free(bca.line_offsets);
5877 if (bca.f && fclose(bca.f) == EOF && error == NULL)
5878 error = got_error_from_errno("fclose");
5885 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
5886 "[path]\n", getprogname());
5890 static const struct got_error *
5891 print_entry(struct got_tree_entry *te, const char *id, const char *path,
5892 const char *root_path, struct got_repository *repo)
5894 const struct got_error *err = NULL;
5895 int is_root_path = (strcmp(path, root_path) == 0);
5896 const char *modestr = "";
5897 mode_t mode = got_tree_entry_get_mode(te);
5898 char *link_target = NULL;
5900 path += strlen(root_path);
5901 while (path[0] == '/')
5904 if (got_object_tree_entry_is_submodule(te))
5906 else if (S_ISLNK(mode)) {
5909 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
5912 for (i = 0; i < strlen(link_target); i++) {
5913 if (!isprint((unsigned char)link_target[i]))
5914 link_target[i] = '?';
5919 else if (S_ISDIR(mode))
5921 else if (mode & S_IXUSR)
5924 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
5925 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
5926 link_target ? " -> ": "", link_target ? link_target : "");
5932 static const struct got_error *
5933 print_tree(const char *path, struct got_commit_object *commit,
5934 int show_ids, int recurse, const char *root_path,
5935 struct got_repository *repo)
5937 const struct got_error *err = NULL;
5938 struct got_object_id *tree_id = NULL;
5939 struct got_tree_object *tree = NULL;
5942 err = got_object_id_by_path(&tree_id, repo, commit, path);
5946 err = got_object_open_as_tree(&tree, repo, tree_id);
5949 nentries = got_object_tree_get_nentries(tree);
5950 for (i = 0; i < nentries; i++) {
5951 struct got_tree_entry *te;
5954 if (sigint_received || sigpipe_received)
5957 te = got_object_tree_get_entry(tree, i);
5960 err = got_object_id_str(&id_str,
5961 got_tree_entry_get_id(te));
5964 if (asprintf(&id, "%s ", id_str) == -1) {
5965 err = got_error_from_errno("asprintf");
5971 err = print_entry(te, id, path, root_path, repo);
5976 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
5978 if (asprintf(&child_path, "%s%s%s", path,
5979 path[0] == '/' && path[1] == '\0' ? "" : "/",
5980 got_tree_entry_get_name(te)) == -1) {
5981 err = got_error_from_errno("asprintf");
5984 err = print_tree(child_path, commit, show_ids, 1,
5993 got_object_tree_close(tree);
5998 static const struct got_error *
5999 cmd_tree(int argc, char *argv[])
6001 const struct got_error *error;
6002 struct got_repository *repo = NULL;
6003 struct got_worktree *worktree = NULL;
6004 const char *path, *refname = NULL;
6005 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6006 struct got_object_id *commit_id = NULL;
6007 struct got_commit_object *commit = NULL;
6008 char *commit_id_str = NULL;
6009 int show_ids = 0, recurse = 0;
6011 int *pack_fds = NULL;
6014 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6019 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6022 commit_id_str = optarg;
6031 repo_path = realpath(optarg, NULL);
6032 if (repo_path == NULL)
6033 return got_error_from_errno2("realpath",
6035 got_path_strip_trailing_slashes(repo_path);
6053 cwd = getcwd(NULL, 0);
6055 error = got_error_from_errno("getcwd");
6059 error = got_repo_pack_fds_open(&pack_fds);
6063 if (repo_path == NULL) {
6064 error = got_worktree_open(&worktree, cwd);
6065 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6071 strdup(got_worktree_get_repo_path(worktree));
6072 if (repo_path == NULL)
6073 error = got_error_from_errno("strdup");
6077 repo_path = strdup(cwd);
6078 if (repo_path == NULL) {
6079 error = got_error_from_errno("strdup");
6085 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6090 const char *prefix = got_worktree_get_path_prefix(worktree);
6095 error = got_worktree_resolve_path(&p, worktree, path);
6098 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6099 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6101 error = got_error_from_errno("asprintf");
6106 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6110 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6115 error = got_repo_map_path(&in_repo_path, repo, path);
6120 if (commit_id_str == NULL) {
6121 struct got_reference *head_ref;
6123 refname = got_worktree_get_head_ref_name(worktree);
6125 refname = GOT_REF_HEAD;
6126 error = got_ref_open(&head_ref, repo, refname, 0);
6129 error = got_ref_resolve(&commit_id, repo, head_ref);
6130 got_ref_close(head_ref);
6134 struct got_reflist_head refs;
6136 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6140 error = got_repo_match_object_id(&commit_id, NULL,
6141 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6142 got_ref_list_free(&refs);
6148 /* Release work tree lock. */
6149 got_worktree_close(worktree);
6153 error = got_object_open_as_commit(&commit, repo, commit_id);
6157 error = print_tree(in_repo_path, commit, show_ids, recurse,
6158 in_repo_path, repo);
6165 got_object_commit_close(commit);
6167 got_worktree_close(worktree);
6169 const struct got_error *close_err = got_repo_close(repo);
6174 const struct got_error *pack_err =
6175 got_repo_pack_fds_close(pack_fds);
6185 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6186 "[-s status-codes] [path ...]\n", getprogname());
6190 struct got_status_arg {
6195 static const struct got_error *
6196 print_status(void *arg, unsigned char status, unsigned char staged_status,
6197 const char *path, struct got_object_id *blob_id,
6198 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6199 int dirfd, const char *de_name)
6201 struct got_status_arg *st = arg;
6203 if (status == staged_status && (status == GOT_STATUS_DELETE))
6204 status = GOT_STATUS_NO_CHANGE;
6205 if (st != NULL && st->status_codes) {
6206 size_t ncodes = strlen(st->status_codes);
6209 for (i = 0; i < ncodes ; i++) {
6211 if (status == st->status_codes[i] ||
6212 staged_status == st->status_codes[i]) {
6217 if (status == st->status_codes[i] ||
6218 staged_status == st->status_codes[i])
6223 if (st->suppress && j == 0)
6230 printf("%c%c %s\n", status, staged_status, path);
6234 static const struct got_error *
6235 cmd_status(int argc, char *argv[])
6237 const struct got_error *error = NULL;
6238 struct got_repository *repo = NULL;
6239 struct got_worktree *worktree = NULL;
6240 struct got_status_arg st;
6242 struct got_pathlist_head paths;
6243 int ch, i, no_ignores = 0;
6244 int *pack_fds = NULL;
6248 memset(&st, 0, sizeof(st));
6249 st.status_codes = NULL;
6252 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6258 if (st.status_codes != NULL && st.suppress == 0)
6259 option_conflict('S', 's');
6263 for (i = 0; i < strlen(optarg); i++) {
6264 switch (optarg[i]) {
6265 case GOT_STATUS_MODIFY:
6266 case GOT_STATUS_ADD:
6267 case GOT_STATUS_DELETE:
6268 case GOT_STATUS_CONFLICT:
6269 case GOT_STATUS_MISSING:
6270 case GOT_STATUS_OBSTRUCTED:
6271 case GOT_STATUS_UNVERSIONED:
6272 case GOT_STATUS_MODE_CHANGE:
6273 case GOT_STATUS_NONEXISTENT:
6276 errx(1, "invalid status code '%c'",
6280 if (ch == 's' && st.suppress)
6281 option_conflict('s', 'S');
6282 st.status_codes = optarg;
6294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6298 cwd = getcwd(NULL, 0);
6300 error = got_error_from_errno("getcwd");
6304 error = got_repo_pack_fds_open(&pack_fds);
6308 error = got_worktree_open(&worktree, cwd);
6310 if (error->code == GOT_ERR_NOT_WORKTREE)
6311 error = wrap_not_worktree_error(error, "status", cwd);
6315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6320 error = apply_unveil(got_repo_get_path(repo), 1,
6321 got_worktree_get_root_path(worktree));
6325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6329 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6330 print_status, &st, check_cancelled, NULL);
6333 const struct got_error *pack_err =
6334 got_repo_pack_fds_close(pack_fds);
6339 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6347 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6348 "[-s reference] [name]\n", getprogname());
6352 static const struct got_error *
6353 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6355 static const struct got_error *err = NULL;
6356 struct got_reflist_head refs;
6357 struct got_reflist_entry *re;
6360 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6361 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6366 TAILQ_FOREACH(re, &refs, entry) {
6368 refstr = got_ref_to_str(re->ref);
6369 if (refstr == NULL) {
6370 err = got_error_from_errno("got_ref_to_str");
6373 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6377 got_ref_list_free(&refs);
6381 static const struct got_error *
6382 delete_ref_by_name(struct got_repository *repo, const char *refname)
6384 const struct got_error *err;
6385 struct got_reference *ref;
6387 err = got_ref_open(&ref, repo, refname, 0);
6391 err = delete_ref(repo, ref);
6396 static const struct got_error *
6397 add_ref(struct got_repository *repo, const char *refname, const char *target)
6399 const struct got_error *err = NULL;
6400 struct got_object_id *id = NULL;
6401 struct got_reference *ref = NULL;
6402 struct got_reflist_head refs;
6405 * Don't let the user create a reference name with a leading '-'.
6406 * While technically a valid reference name, this case is usually
6407 * an unintended typo.
6409 if (refname[0] == '-')
6410 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6413 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6416 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6418 got_ref_list_free(&refs);
6422 err = got_ref_alloc(&ref, refname, id);
6426 err = got_ref_write(ref, repo);
6434 static const struct got_error *
6435 add_symref(struct got_repository *repo, const char *refname, const char *target)
6437 const struct got_error *err = NULL;
6438 struct got_reference *ref = NULL;
6439 struct got_reference *target_ref = NULL;
6442 * Don't let the user create a reference name with a leading '-'.
6443 * While technically a valid reference name, this case is usually
6444 * an unintended typo.
6446 if (refname[0] == '-')
6447 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6449 err = got_ref_open(&target_ref, repo, target, 0);
6453 err = got_ref_alloc_symref(&ref, refname, target_ref);
6457 err = got_ref_write(ref, repo);
6460 got_ref_close(target_ref);
6466 static const struct got_error *
6467 cmd_ref(int argc, char *argv[])
6469 const struct got_error *error = NULL;
6470 struct got_repository *repo = NULL;
6471 struct got_worktree *worktree = NULL;
6472 char *cwd = NULL, *repo_path = NULL;
6473 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6474 const char *obj_arg = NULL, *symref_target= NULL;
6475 char *refname = NULL;
6476 int *pack_fds = NULL;
6478 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6490 repo_path = realpath(optarg, NULL);
6491 if (repo_path == NULL)
6492 return got_error_from_errno2("realpath",
6494 got_path_strip_trailing_slashes(repo_path);
6497 symref_target = optarg;
6508 if (obj_arg && do_list)
6509 option_conflict('c', 'l');
6510 if (obj_arg && do_delete)
6511 option_conflict('c', 'd');
6512 if (obj_arg && symref_target)
6513 option_conflict('c', 's');
6514 if (symref_target && do_delete)
6515 option_conflict('s', 'd');
6516 if (symref_target && do_list)
6517 option_conflict('s', 'l');
6518 if (do_delete && do_list)
6519 option_conflict('d', 'l');
6520 if (sort_by_time && !do_list)
6521 errx(1, "-t option requires -l option");
6527 if (argc != 0 && argc != 1)
6530 refname = strdup(argv[0]);
6531 if (refname == NULL) {
6532 error = got_error_from_errno("strdup");
6539 refname = strdup(argv[0]);
6540 if (refname == NULL) {
6541 error = got_error_from_errno("strdup");
6547 got_path_strip_trailing_slashes(refname);
6550 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6551 "sendfd unveil", NULL) == -1)
6554 cwd = getcwd(NULL, 0);
6556 error = got_error_from_errno("getcwd");
6560 error = got_repo_pack_fds_open(&pack_fds);
6564 if (repo_path == NULL) {
6565 error = got_worktree_open(&worktree, cwd);
6566 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6572 strdup(got_worktree_get_repo_path(worktree));
6573 if (repo_path == NULL)
6574 error = got_error_from_errno("strdup");
6578 repo_path = strdup(cwd);
6579 if (repo_path == NULL) {
6580 error = got_error_from_errno("strdup");
6586 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6592 /* Remove "cpath" promise. */
6593 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6599 error = apply_unveil(got_repo_get_path(repo), do_list,
6600 worktree ? got_worktree_get_root_path(worktree) : NULL);
6605 error = list_refs(repo, refname, sort_by_time);
6607 error = delete_ref_by_name(repo, refname);
6608 else if (symref_target)
6609 error = add_symref(repo, refname, symref_target);
6611 if (obj_arg == NULL)
6613 error = add_ref(repo, refname, obj_arg);
6618 const struct got_error *close_err = got_repo_close(repo);
6623 got_worktree_close(worktree);
6625 const struct got_error *pack_err =
6626 got_repo_pack_fds_close(pack_fds);
6638 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6639 "[-r repository-path] [name]\n", getprogname());
6643 static const struct got_error *
6644 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6645 struct got_reference *ref)
6647 const struct got_error *err = NULL;
6648 const char *refname, *marker = " ";
6651 refname = got_ref_get_name(ref);
6652 if (worktree && strcmp(refname,
6653 got_worktree_get_head_ref_name(worktree)) == 0) {
6654 struct got_object_id *id = NULL;
6656 err = got_ref_resolve(&id, repo, ref);
6659 if (got_object_id_cmp(id,
6660 got_worktree_get_base_commit_id(worktree)) == 0)
6667 if (strncmp(refname, "refs/heads/", 11) == 0)
6669 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6671 if (strncmp(refname, "refs/remotes/", 13) == 0)
6674 refstr = got_ref_to_str(ref);
6676 return got_error_from_errno("got_ref_to_str");
6678 printf("%s%s: %s\n", marker, refname, refstr);
6683 static const struct got_error *
6684 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6686 const char *refname;
6688 if (worktree == NULL)
6689 return got_error(GOT_ERR_NOT_WORKTREE);
6691 refname = got_worktree_get_head_ref_name(worktree);
6693 if (strncmp(refname, "refs/heads/", 11) == 0)
6695 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6698 printf("%s\n", refname);
6703 static const struct got_error *
6704 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6707 static const struct got_error *err = NULL;
6708 struct got_reflist_head refs;
6709 struct got_reflist_entry *re;
6710 struct got_reference *temp_ref = NULL;
6711 int rebase_in_progress, histedit_in_progress;
6716 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6721 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6726 if (rebase_in_progress || histedit_in_progress) {
6727 err = got_ref_open(&temp_ref, repo,
6728 got_worktree_get_head_ref_name(worktree), 0);
6731 list_branch(repo, worktree, temp_ref);
6732 got_ref_close(temp_ref);
6736 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6737 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6742 TAILQ_FOREACH(re, &refs, entry)
6743 list_branch(repo, worktree, re->ref);
6745 got_ref_list_free(&refs);
6747 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6748 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6753 TAILQ_FOREACH(re, &refs, entry)
6754 list_branch(repo, worktree, re->ref);
6756 got_ref_list_free(&refs);
6761 static const struct got_error *
6762 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6763 const char *branch_name)
6765 const struct got_error *err = NULL;
6766 struct got_reference *ref = NULL;
6767 char *refname, *remote_refname = NULL;
6769 if (strncmp(branch_name, "refs/", 5) == 0)
6771 if (strncmp(branch_name, "heads/", 6) == 0)
6773 else if (strncmp(branch_name, "remotes/", 8) == 0)
6776 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6777 return got_error_from_errno("asprintf");
6779 if (asprintf(&remote_refname, "refs/remotes/%s",
6780 branch_name) == -1) {
6781 err = got_error_from_errno("asprintf");
6785 err = got_ref_open(&ref, repo, refname, 0);
6787 const struct got_error *err2;
6788 if (err->code != GOT_ERR_NOT_REF)
6791 * Keep 'err' intact such that if neither branch exists
6792 * we report "refs/heads" rather than "refs/remotes" in
6793 * our error message.
6795 err2 = got_ref_open(&ref, repo, remote_refname, 0);
6802 strcmp(got_worktree_get_head_ref_name(worktree),
6803 got_ref_get_name(ref)) == 0) {
6804 err = got_error_msg(GOT_ERR_SAME_BRANCH,
6805 "will not delete this work tree's current branch");
6809 err = delete_ref(repo, ref);
6814 free(remote_refname);
6818 static const struct got_error *
6819 add_branch(struct got_repository *repo, const char *branch_name,
6820 struct got_object_id *base_commit_id)
6822 const struct got_error *err = NULL;
6823 struct got_reference *ref = NULL;
6824 char *refname = NULL;
6827 * Don't let the user create a branch name with a leading '-'.
6828 * While technically a valid reference name, this case is usually
6829 * an unintended typo.
6831 if (branch_name[0] == '-')
6832 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
6834 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6837 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
6838 err = got_error_from_errno("asprintf");
6842 err = got_ref_open(&ref, repo, refname, 0);
6844 err = got_error(GOT_ERR_BRANCH_EXISTS);
6846 } else if (err->code != GOT_ERR_NOT_REF)
6849 err = got_ref_alloc(&ref, refname, base_commit_id);
6853 err = got_ref_write(ref, repo);
6861 static const struct got_error *
6862 cmd_branch(int argc, char *argv[])
6864 const struct got_error *error = NULL;
6865 struct got_repository *repo = NULL;
6866 struct got_worktree *worktree = NULL;
6867 char *cwd = NULL, *repo_path = NULL;
6868 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
6869 const char *delref = NULL, *commit_id_arg = NULL;
6870 struct got_reference *ref = NULL;
6871 struct got_pathlist_head paths;
6872 struct got_object_id *commit_id = NULL;
6873 char *commit_id_str = NULL;
6874 int *pack_fds = NULL;
6878 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
6881 commit_id_arg = optarg;
6893 repo_path = realpath(optarg, NULL);
6894 if (repo_path == NULL)
6895 return got_error_from_errno2("realpath",
6897 got_path_strip_trailing_slashes(repo_path);
6908 if (do_list && delref)
6909 option_conflict('l', 'd');
6910 if (sort_by_time && !do_list)
6911 errx(1, "-t option requires -l option");
6916 if (!do_list && !delref && argc == 0)
6919 if ((do_list || delref || do_show) && commit_id_arg != NULL)
6920 errx(1, "-c option can only be used when creating a branch");
6922 if (do_list || delref) {
6925 } else if (!do_show && argc != 1)
6929 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6930 "sendfd unveil", NULL) == -1)
6933 cwd = getcwd(NULL, 0);
6935 error = got_error_from_errno("getcwd");
6939 error = got_repo_pack_fds_open(&pack_fds);
6943 if (repo_path == NULL) {
6944 error = got_worktree_open(&worktree, cwd);
6945 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6951 strdup(got_worktree_get_repo_path(worktree));
6952 if (repo_path == NULL)
6953 error = got_error_from_errno("strdup");
6957 repo_path = strdup(cwd);
6958 if (repo_path == NULL) {
6959 error = got_error_from_errno("strdup");
6965 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6970 if (do_list || do_show) {
6971 /* Remove "cpath" promise. */
6972 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6978 error = apply_unveil(got_repo_get_path(repo), do_list,
6979 worktree ? got_worktree_get_root_path(worktree) : NULL);
6984 error = show_current_branch(repo, worktree);
6986 error = list_branches(repo, worktree, sort_by_time);
6988 error = delete_branch(repo, worktree, delref);
6990 struct got_reflist_head refs;
6992 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6996 if (commit_id_arg == NULL)
6997 commit_id_arg = worktree ?
6998 got_worktree_get_head_ref_name(worktree) :
7000 error = got_repo_match_object_id(&commit_id, NULL,
7001 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7002 got_ref_list_free(&refs);
7005 error = add_branch(repo, argv[0], commit_id);
7008 if (worktree && do_update) {
7009 struct got_update_progress_arg upa;
7010 char *branch_refname = NULL;
7012 error = got_object_id_str(&commit_id_str, commit_id);
7015 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7019 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7021 error = got_error_from_errno("asprintf");
7024 error = got_ref_open(&ref, repo, branch_refname, 0);
7025 free(branch_refname);
7028 error = switch_head_ref(ref, commit_id, worktree,
7032 error = got_worktree_set_base_commit_id(worktree, repo,
7036 memset(&upa, 0, sizeof(upa));
7037 error = got_worktree_checkout_files(worktree, &paths,
7038 repo, update_progress, &upa, check_cancelled,
7042 if (upa.did_something) {
7043 printf("Updated to %s: %s\n",
7044 got_worktree_get_head_ref_name(worktree),
7047 print_update_progress_stats(&upa);
7054 const struct got_error *close_err = got_repo_close(repo);
7059 got_worktree_close(worktree);
7061 const struct got_error *pack_err =
7062 got_repo_pack_fds_close(pack_fds);
7069 free(commit_id_str);
7070 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7078 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7079 "[-r repository-path] [-s signer-id] name\n", getprogname());
7084 static const struct got_error *
7085 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7087 const struct got_error *err = NULL;
7088 struct got_reflist_entry *re, *se, *new;
7089 struct got_object_id *re_id, *se_id;
7090 struct got_tag_object *re_tag, *se_tag;
7091 time_t re_time, se_time;
7093 STAILQ_FOREACH(re, tags, entry) {
7094 se = STAILQ_FIRST(sorted);
7096 err = got_reflist_entry_dup(&new, re);
7099 STAILQ_INSERT_HEAD(sorted, new, entry);
7102 err = got_ref_resolve(&re_id, repo, re->ref);
7105 err = got_object_open_as_tag(&re_tag, repo, re_id);
7109 re_time = got_object_tag_get_tagger_time(re_tag);
7110 got_object_tag_close(re_tag);
7114 err = got_ref_resolve(&se_id, repo, re->ref);
7117 err = got_object_open_as_tag(&se_tag, repo, se_id);
7121 se_time = got_object_tag_get_tagger_time(se_tag);
7122 got_object_tag_close(se_tag);
7124 if (se_time > re_time) {
7125 err = got_reflist_entry_dup(&new, re);
7128 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7131 se = STAILQ_NEXT(se, entry);
7140 static const struct got_error *
7141 get_tag_refname(char **refname, const char *tag_name)
7143 const struct got_error *err;
7145 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7146 *refname = strdup(tag_name);
7147 if (*refname == NULL)
7148 return got_error_from_errno("strdup");
7149 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7150 err = got_error_from_errno("asprintf");
7158 static const struct got_error *
7159 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7160 const char *allowed_signers, const char *revoked_signers, int verbosity)
7162 static const struct got_error *err = NULL;
7163 struct got_reflist_head refs;
7164 struct got_reflist_entry *re;
7165 char *wanted_refname = NULL;
7170 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7175 struct got_reference *ref;
7176 err = get_tag_refname(&wanted_refname, tag_name);
7179 /* Wanted tag reference should exist. */
7180 err = got_ref_open(&ref, repo, wanted_refname, 0);
7186 TAILQ_FOREACH(re, &refs, entry) {
7187 const char *refname;
7188 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7190 const char *tagger, *ssh_sig = NULL;
7191 char *sig_msg = NULL;
7193 struct got_object_id *id;
7194 struct got_tag_object *tag;
7195 struct got_commit_object *commit = NULL;
7197 refname = got_ref_get_name(re->ref);
7198 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7199 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7202 refstr = got_ref_to_str(re->ref);
7203 if (refstr == NULL) {
7204 err = got_error_from_errno("got_ref_to_str");
7208 err = got_ref_resolve(&id, repo, re->ref);
7211 err = got_object_open_as_tag(&tag, repo, id);
7213 if (err->code != GOT_ERR_OBJ_TYPE) {
7217 /* "lightweight" tag */
7218 err = got_object_open_as_commit(&commit, repo, id);
7223 tagger = got_object_commit_get_committer(commit);
7225 got_object_commit_get_committer_time(commit);
7226 err = got_object_id_str(&id_str, id);
7232 tagger = got_object_tag_get_tagger(tag);
7233 tagger_time = got_object_tag_get_tagger_time(tag);
7234 err = got_object_id_str(&id_str,
7235 got_object_tag_get_object_id(tag));
7240 if (tag && verify_tags) {
7241 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7242 got_object_tag_get_message(tag));
7243 if (ssh_sig && allowed_signers == NULL) {
7244 err = got_error_msg(
7245 GOT_ERR_VERIFY_TAG_SIGNATURE,
7246 "SSH signature verification requires "
7247 "setting allowed_signers in "
7253 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7255 printf("from: %s\n", tagger);
7256 datestr = get_datestr(&tagger_time, datebuf);
7258 printf("date: %s UTC\n", datestr);
7260 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7262 switch (got_object_tag_get_object_type(tag)) {
7263 case GOT_OBJ_TYPE_BLOB:
7264 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7267 case GOT_OBJ_TYPE_TREE:
7268 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7271 case GOT_OBJ_TYPE_COMMIT:
7272 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7275 case GOT_OBJ_TYPE_TAG:
7276 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7286 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7287 allowed_signers, revoked_signers, verbosity);
7288 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7292 printf("signature: %s", sig_msg);
7298 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7301 got_object_commit_close(commit);
7303 tagmsg0 = strdup(got_object_tag_get_message(tag));
7304 got_object_tag_close(tag);
7305 if (tagmsg0 == NULL) {
7306 err = got_error_from_errno("strdup");
7313 line = strsep(&tagmsg, "\n");
7315 printf(" %s\n", line);
7320 got_ref_list_free(&refs);
7321 free(wanted_refname);
7323 if (err == NULL && bad_sigs)
7324 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7328 static const struct got_error *
7329 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7330 const char *tag_name, const char *repo_path)
7332 const struct got_error *err = NULL;
7333 char *template = NULL, *initial_content = NULL;
7334 char *editor = NULL;
7335 int initial_content_len;
7338 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7339 err = got_error_from_errno("asprintf");
7343 initial_content_len = asprintf(&initial_content,
7344 "\n# tagging commit %s as %s\n",
7345 commit_id_str, tag_name);
7346 if (initial_content_len == -1) {
7347 err = got_error_from_errno("asprintf");
7351 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7355 if (write(fd, initial_content, initial_content_len) == -1) {
7356 err = got_error_from_errno2("write", *tagmsg_path);
7360 err = get_editor(&editor);
7363 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7364 initial_content_len, 1);
7366 free(initial_content);
7370 if (fd != -1 && close(fd) == -1 && err == NULL)
7371 err = got_error_from_errno2("close", *tagmsg_path);
7380 static const struct got_error *
7381 add_tag(struct got_repository *repo, const char *tagger,
7382 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7383 const char *signer_id, int verbosity)
7385 const struct got_error *err = NULL;
7386 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7387 char *label = NULL, *commit_id_str = NULL;
7388 struct got_reference *ref = NULL;
7389 char *refname = NULL, *tagmsg = NULL;
7390 char *tagmsg_path = NULL, *tag_id_str = NULL;
7391 int preserve_tagmsg = 0;
7392 struct got_reflist_head refs;
7397 * Don't let the user create a tag name with a leading '-'.
7398 * While technically a valid reference name, this case is usually
7399 * an unintended typo.
7401 if (tag_name[0] == '-')
7402 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7404 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7408 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7409 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7413 err = got_object_id_str(&commit_id_str, commit_id);
7417 err = get_tag_refname(&refname, tag_name);
7420 if (strncmp("refs/tags/", tag_name, 10) == 0)
7423 err = got_ref_open(&ref, repo, refname, 0);
7425 err = got_error(GOT_ERR_TAG_EXISTS);
7427 } else if (err->code != GOT_ERR_NOT_REF)
7430 if (tagmsg_arg == NULL) {
7431 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7432 tag_name, got_repo_get_path(repo));
7434 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7435 tagmsg_path != NULL)
7436 preserve_tagmsg = 1;
7439 /* Editor is done; we can now apply unveil(2) */
7440 err = got_sigs_apply_unveil();
7443 err = apply_unveil(got_repo_get_path(repo), 0, NULL);
7448 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7449 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7453 preserve_tagmsg = 1;
7457 err = got_ref_alloc(&ref, refname, tag_id);
7460 preserve_tagmsg = 1;
7464 err = got_ref_write(ref, repo);
7467 preserve_tagmsg = 1;
7471 err = got_object_id_str(&tag_id_str, tag_id);
7474 preserve_tagmsg = 1;
7477 printf("Created tag %s\n", tag_id_str);
7479 if (preserve_tagmsg) {
7480 fprintf(stderr, "%s: tag message preserved in %s\n",
7481 getprogname(), tagmsg_path);
7482 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7483 err = got_error_from_errno2("unlink", tagmsg_path);
7488 free(commit_id_str);
7492 got_ref_list_free(&refs);
7496 static const struct got_error *
7497 cmd_tag(int argc, char *argv[])
7499 const struct got_error *error = NULL;
7500 struct got_repository *repo = NULL;
7501 struct got_worktree *worktree = NULL;
7502 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7503 char *gitconfig_path = NULL, *tagger = NULL;
7504 char *allowed_signers = NULL, *revoked_signers = NULL;
7505 const char *signer_id = NULL;
7506 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7507 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7508 int *pack_fds = NULL;
7510 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7513 commit_id_arg = optarg;
7522 repo_path = realpath(optarg, NULL);
7523 if (repo_path == NULL) {
7524 error = got_error_from_errno2("realpath",
7528 got_path_strip_trailing_slashes(repo_path);
7539 else if (verbosity < 3)
7551 if (do_list || verify_tags) {
7552 if (commit_id_arg != NULL)
7554 "-c option can only be used when creating a tag");
7557 option_conflict('l', 'm');
7559 option_conflict('V', 'm');
7563 option_conflict('l', 's');
7565 option_conflict('V', 's');
7569 } else if (argc != 1)
7576 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7577 "sendfd unveil", NULL) == -1)
7580 cwd = getcwd(NULL, 0);
7582 error = got_error_from_errno("getcwd");
7586 error = got_repo_pack_fds_open(&pack_fds);
7590 if (repo_path == NULL) {
7591 error = got_worktree_open(&worktree, cwd);
7592 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7598 strdup(got_worktree_get_repo_path(worktree));
7599 if (repo_path == NULL)
7600 error = got_error_from_errno("strdup");
7604 repo_path = strdup(cwd);
7605 if (repo_path == NULL) {
7606 error = got_error_from_errno("strdup");
7612 if (do_list || verify_tags) {
7613 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7616 error = get_allowed_signers(&allowed_signers, repo, worktree);
7619 error = get_revoked_signers(&revoked_signers, repo, worktree);
7623 /* Release work tree lock. */
7624 got_worktree_close(worktree);
7629 * Remove "cpath" promise unless needed for signature tmpfile
7633 got_sigs_apply_unveil();
7636 if (pledge("stdio rpath wpath flock proc exec sendfd "
7637 "unveil", NULL) == -1)
7641 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7644 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7645 revoked_signers, verbosity);
7647 error = get_gitconfig_path(&gitconfig_path);
7650 error = got_repo_open(&repo, repo_path, gitconfig_path,
7655 error = get_author(&tagger, repo, worktree);
7658 if (signer_id == NULL)
7659 signer_id = get_signer_id(repo, worktree);
7663 error = got_sigs_apply_unveil();
7667 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7672 if (commit_id_arg == NULL) {
7673 struct got_reference *head_ref;
7674 struct got_object_id *commit_id;
7675 error = got_ref_open(&head_ref, repo,
7676 worktree ? got_worktree_get_head_ref_name(worktree)
7680 error = got_ref_resolve(&commit_id, repo, head_ref);
7681 got_ref_close(head_ref);
7684 error = got_object_id_str(&commit_id_str, commit_id);
7691 /* Release work tree lock. */
7692 got_worktree_close(worktree);
7696 error = add_tag(repo, tagger, tag_name,
7697 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7698 signer_id, verbosity);
7702 const struct got_error *close_err = got_repo_close(repo);
7707 got_worktree_close(worktree);
7709 const struct got_error *pack_err =
7710 got_repo_pack_fds_close(pack_fds);
7716 free(gitconfig_path);
7717 free(commit_id_str);
7719 free(allowed_signers);
7720 free(revoked_signers);
7727 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7731 static const struct got_error *
7732 add_progress(void *arg, unsigned char status, const char *path)
7734 while (path[0] == '/')
7736 printf("%c %s\n", status, path);
7740 static const struct got_error *
7741 cmd_add(int argc, char *argv[])
7743 const struct got_error *error = NULL;
7744 struct got_repository *repo = NULL;
7745 struct got_worktree *worktree = NULL;
7747 struct got_pathlist_head paths;
7748 struct got_pathlist_entry *pe;
7749 int ch, can_recurse = 0, no_ignores = 0;
7750 int *pack_fds = NULL;
7754 while ((ch = getopt(argc, argv, "IR")) != -1) {
7772 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7779 cwd = getcwd(NULL, 0);
7781 error = got_error_from_errno("getcwd");
7785 error = got_repo_pack_fds_open(&pack_fds);
7789 error = got_worktree_open(&worktree, cwd);
7791 if (error->code == GOT_ERR_NOT_WORKTREE)
7792 error = wrap_not_worktree_error(error, "add", cwd);
7796 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7801 error = apply_unveil(got_repo_get_path(repo), 1,
7802 got_worktree_get_root_path(worktree));
7806 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7813 TAILQ_FOREACH(pe, &paths, entry) {
7814 if (asprintf(&ondisk_path, "%s/%s",
7815 got_worktree_get_root_path(worktree),
7817 error = got_error_from_errno("asprintf");
7820 if (lstat(ondisk_path, &sb) == -1) {
7821 if (errno == ENOENT) {
7825 error = got_error_from_errno2("lstat",
7831 if (S_ISDIR(sb.st_mode)) {
7832 error = got_error_msg(GOT_ERR_BAD_PATH,
7833 "adding directories requires -R option");
7839 error = got_worktree_schedule_add(worktree, &paths, add_progress,
7840 NULL, repo, no_ignores);
7843 const struct got_error *close_err = got_repo_close(repo);
7848 got_worktree_close(worktree);
7850 const struct got_error *pack_err =
7851 got_repo_pack_fds_close(pack_fds);
7855 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7863 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
7868 static const struct got_error *
7869 print_remove_status(void *arg, unsigned char status,
7870 unsigned char staged_status, const char *path)
7872 while (path[0] == '/')
7874 if (status == GOT_STATUS_NONEXISTENT)
7876 if (status == staged_status && (status == GOT_STATUS_DELETE))
7877 status = GOT_STATUS_NO_CHANGE;
7878 printf("%c%c %s\n", status, staged_status, path);
7882 static const struct got_error *
7883 cmd_remove(int argc, char *argv[])
7885 const struct got_error *error = NULL;
7886 struct got_worktree *worktree = NULL;
7887 struct got_repository *repo = NULL;
7888 const char *status_codes = NULL;
7890 struct got_pathlist_head paths;
7891 struct got_pathlist_entry *pe;
7892 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
7893 int ignore_missing_paths = 0;
7894 int *pack_fds = NULL;
7898 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
7901 delete_local_mods = 1;
7902 ignore_missing_paths = 1;
7911 for (i = 0; i < strlen(optarg); i++) {
7912 switch (optarg[i]) {
7913 case GOT_STATUS_MODIFY:
7914 delete_local_mods = 1;
7916 case GOT_STATUS_MISSING:
7917 ignore_missing_paths = 1;
7920 errx(1, "invalid status code '%c'",
7924 status_codes = optarg;
7936 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7943 cwd = getcwd(NULL, 0);
7945 error = got_error_from_errno("getcwd");
7949 error = got_repo_pack_fds_open(&pack_fds);
7953 error = got_worktree_open(&worktree, cwd);
7955 if (error->code == GOT_ERR_NOT_WORKTREE)
7956 error = wrap_not_worktree_error(error, "remove", cwd);
7960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7965 error = apply_unveil(got_repo_get_path(repo), 1,
7966 got_worktree_get_root_path(worktree));
7970 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7977 TAILQ_FOREACH(pe, &paths, entry) {
7978 if (asprintf(&ondisk_path, "%s/%s",
7979 got_worktree_get_root_path(worktree),
7981 error = got_error_from_errno("asprintf");
7984 if (lstat(ondisk_path, &sb) == -1) {
7985 if (errno == ENOENT) {
7989 error = got_error_from_errno2("lstat",
7995 if (S_ISDIR(sb.st_mode)) {
7996 error = got_error_msg(GOT_ERR_BAD_PATH,
7997 "removing directories requires -R option");
8003 error = got_worktree_schedule_delete(worktree, &paths,
8004 delete_local_mods, status_codes, print_remove_status, NULL,
8005 repo, keep_on_disk, ignore_missing_paths);
8008 const struct got_error *close_err = got_repo_close(repo);
8013 got_worktree_close(worktree);
8015 const struct got_error *pack_err =
8016 got_repo_pack_fds_close(pack_fds);
8020 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8028 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8029 "[patchfile]\n", getprogname());
8033 static const struct got_error *
8034 patch_from_stdin(int *patchfd)
8036 const struct got_error *err = NULL;
8039 sig_t sighup, sigint, sigquit;
8041 *patchfd = got_opentempfd();
8043 return got_error_from_errno("got_opentempfd");
8045 sighup = signal(SIGHUP, SIG_DFL);
8046 sigint = signal(SIGINT, SIG_DFL);
8047 sigquit = signal(SIGQUIT, SIG_DFL);
8050 r = read(0, buf, sizeof(buf));
8052 err = got_error_from_errno("read");
8057 if (write(*patchfd, buf, r) == -1) {
8058 err = got_error_from_errno("write");
8063 signal(SIGHUP, sighup);
8064 signal(SIGINT, sigint);
8065 signal(SIGQUIT, sigquit);
8067 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8068 err = got_error_from_errno("lseek");
8078 static const struct got_error *
8079 patch_progress(void *arg, const char *old, const char *new,
8080 unsigned char status, const struct got_error *error, int old_from,
8081 int old_lines, int new_from, int new_lines, int offset,
8082 int ws_mangled, const struct got_error *hunk_err)
8084 const char *path = new == NULL ? old : new;
8086 while (*path == '/')
8090 printf("%c %s\n", status, path);
8093 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8095 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8096 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8097 old_lines, new_from, new_lines);
8098 if (hunk_err != NULL)
8099 printf("%s\n", hunk_err->msg);
8100 else if (offset != 0)
8101 printf("applied with offset %d\n", offset);
8103 printf("hunk contains mangled whitespace\n");
8109 static const struct got_error *
8110 cmd_patch(int argc, char *argv[])
8112 const struct got_error *error = NULL, *close_error = NULL;
8113 struct got_worktree *worktree = NULL;
8114 struct got_repository *repo = NULL;
8115 struct got_reflist_head refs;
8116 struct got_object_id *commit_id = NULL;
8117 const char *commit_id_str = NULL;
8121 int ch, nop = 0, strip = -1, reverse = 0;
8123 int *pack_fds = NULL;
8128 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8129 "unveil", NULL) == -1)
8133 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8136 commit_id_str = optarg;
8142 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8144 errx(1, "pathname strip count is %s: %s",
8160 error = patch_from_stdin(&patchfd);
8163 } else if (argc == 1) {
8164 patchfd = open(argv[0], O_RDONLY);
8165 if (patchfd == -1) {
8166 error = got_error_from_errno2("open", argv[0]);
8169 if (fstat(patchfd, &sb) == -1) {
8170 error = got_error_from_errno2("fstat", argv[0]);
8173 if (!S_ISREG(sb.st_mode)) {
8174 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8180 if ((cwd = getcwd(NULL, 0)) == NULL) {
8181 error = got_error_from_errno("getcwd");
8185 error = got_repo_pack_fds_open(&pack_fds);
8189 error = got_worktree_open(&worktree, cwd);
8193 const char *repo_path = got_worktree_get_repo_path(worktree);
8194 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8198 error = apply_unveil(got_repo_get_path(repo), 0,
8199 got_worktree_get_root_path(worktree));
8203 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8207 if (commit_id_str != NULL) {
8208 error = got_repo_match_object_id(&commit_id, NULL,
8209 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
8214 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8215 commit_id, &patch_progress, NULL, check_cancelled, NULL);
8218 got_ref_list_free(&refs);
8221 close_error = got_repo_close(repo);
8223 error = close_error;
8225 if (worktree != NULL) {
8226 close_error = got_worktree_close(worktree);
8228 error = close_error;
8231 const struct got_error *pack_err =
8232 got_repo_pack_fds_close(pack_fds);
8243 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8248 static const struct got_error *
8249 revert_progress(void *arg, unsigned char status, const char *path)
8251 if (status == GOT_STATUS_UNVERSIONED)
8254 while (path[0] == '/')
8256 printf("%c %s\n", status, path);
8260 struct choose_patch_arg {
8261 FILE *patch_script_file;
8265 static const struct got_error *
8266 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8267 int nchanges, const char *action)
8269 const struct got_error *err;
8271 size_t linesize = 0;
8275 case GOT_STATUS_ADD:
8276 printf("A %s\n%s this addition? [y/n] ", path, action);
8278 case GOT_STATUS_DELETE:
8279 printf("D %s\n%s this deletion? [y/n] ", path, action);
8281 case GOT_STATUS_MODIFY:
8282 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8283 return got_error_from_errno("fseek");
8284 printf(GOT_COMMIT_SEP_STR);
8285 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8287 if (linelen == -1 && ferror(patch_file)) {
8288 err = got_error_from_errno("getline");
8293 printf(GOT_COMMIT_SEP_STR);
8294 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8295 path, n, nchanges, action);
8298 return got_error_path(path, GOT_ERR_FILE_STATUS);
8305 static const struct got_error *
8306 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8307 FILE *patch_file, int n, int nchanges)
8309 const struct got_error *err = NULL;
8311 size_t linesize = 0;
8314 struct choose_patch_arg *a = arg;
8316 *choice = GOT_PATCH_CHOICE_NONE;
8318 if (a->patch_script_file) {
8320 err = show_change(status, path, patch_file, n, nchanges,
8324 linelen = getline(&line, &linesize, a->patch_script_file);
8325 if (linelen == -1) {
8326 if (ferror(a->patch_script_file))
8327 return got_error_from_errno("getline");
8330 nl = strchr(line, '\n');
8333 if (strcmp(line, "y") == 0) {
8334 *choice = GOT_PATCH_CHOICE_YES;
8336 } else if (strcmp(line, "n") == 0) {
8337 *choice = GOT_PATCH_CHOICE_NO;
8339 } else if (strcmp(line, "q") == 0 &&
8340 status == GOT_STATUS_MODIFY) {
8341 *choice = GOT_PATCH_CHOICE_QUIT;
8344 printf("invalid response '%s'\n", line);
8349 while (resp != 'y' && resp != 'n' && resp != 'q') {
8350 err = show_change(status, path, patch_file, n, nchanges,
8357 if (status == GOT_STATUS_MODIFY) {
8358 if (resp != 'y' && resp != 'n' && resp != 'q') {
8359 printf("invalid response '%c'\n", resp);
8362 } else if (resp != 'y' && resp != 'n') {
8363 printf("invalid response '%c'\n", resp);
8369 *choice = GOT_PATCH_CHOICE_YES;
8370 else if (resp == 'n')
8371 *choice = GOT_PATCH_CHOICE_NO;
8372 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8373 *choice = GOT_PATCH_CHOICE_QUIT;
8378 struct wt_commitable_path_arg {
8379 struct got_pathlist_head *commit_paths;
8384 * Shortcut work tree status callback to determine if the set of paths scanned
8385 * has at least one versioned path that is being modified and, if not NULL, is
8386 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8387 * soon as a path is passed with a status that satisfies this criteria.
8389 static const struct got_error *
8390 worktree_has_commitable_path(void *arg, unsigned char status,
8391 unsigned char staged_status, const char *path,
8392 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8393 struct got_object_id *commit_id, int dirfd, const char *de_name)
8395 struct wt_commitable_path_arg *a = arg;
8397 if (status == staged_status && (status == GOT_STATUS_DELETE))
8398 status = GOT_STATUS_NO_CHANGE;
8400 if (!(status == GOT_STATUS_NO_CHANGE ||
8401 status == GOT_STATUS_UNVERSIONED) ||
8402 staged_status != GOT_STATUS_NO_CHANGE) {
8403 if (a->commit_paths != NULL) {
8404 struct got_pathlist_entry *pe;
8406 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8407 if (strncmp(path, pe->path,
8408 pe->path_len) == 0) {
8409 *a->has_changes = 1;
8414 *a->has_changes = 1;
8416 if (*a->has_changes)
8417 return got_error(GOT_ERR_FILE_MODIFIED);
8424 * Check that the changeset of the commit identified by id is
8425 * comprised of at least one modified path that is being committed.
8427 static const struct got_error *
8428 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8429 struct got_object_id *id, struct got_worktree *worktree,
8430 struct got_repository *repo)
8432 const struct got_error *err;
8433 struct got_pathlist_head paths;
8434 struct got_commit_object *commit = NULL, *pcommit = NULL;
8435 struct got_tree_object *tree = NULL, *ptree = NULL;
8436 struct got_object_qid *pid;
8440 err = got_object_open_as_commit(&commit, repo, id);
8444 err = got_object_open_as_tree(&tree, repo,
8445 got_object_commit_get_tree_id(commit));
8449 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8451 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8455 err = got_object_open_as_tree(&ptree, repo,
8456 got_object_commit_get_tree_id(pcommit));
8461 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8462 got_diff_tree_collect_changed_paths, &paths, 0);
8466 err = got_worktree_status(worktree, &paths, repo, 0,
8467 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8468 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8470 * At least one changed path in the referenced commit is
8471 * modified in the work tree, that's all we need to know!
8477 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8479 got_object_commit_close(commit);
8481 got_object_commit_close(pcommit);
8483 got_object_tree_close(tree);
8485 got_object_tree_close(ptree);
8490 * Remove any "logmsg" reference comprised entirely of paths that have
8491 * been reverted in this work tree. If any path in the logmsg ref changeset
8492 * remains in a changed state in the worktree, do not remove the reference.
8494 static const struct got_error *
8495 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8497 const struct got_error *err;
8498 struct got_reflist_head refs;
8499 struct got_reflist_entry *re;
8500 struct got_commit_object *commit = NULL;
8501 struct got_object_id *commit_id = NULL;
8502 struct wt_commitable_path_arg wcpa;
8503 char *uuidstr = NULL;
8507 err = got_worktree_get_uuid(&uuidstr, worktree);
8511 err = got_ref_list(&refs, repo, "refs/got/worktree",
8512 got_ref_cmp_by_name, repo);
8516 TAILQ_FOREACH(re, &refs, entry) {
8517 const char *refname;
8518 int has_changes = 0;
8520 refname = got_ref_get_name(re->ref);
8522 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8523 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8524 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8525 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8526 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8527 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8531 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8532 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8536 err = got_repo_match_object_id(&commit_id, NULL, refname,
8537 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8541 err = got_object_open_as_commit(&commit, repo, commit_id);
8545 wcpa.commit_paths = NULL;
8546 wcpa.has_changes = &has_changes;
8548 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8554 err = got_ref_delete(re->ref, repo);
8559 got_object_commit_close(commit);
8568 got_ref_list_free(&refs);
8570 got_object_commit_close(commit);
8574 static const struct got_error *
8575 cmd_revert(int argc, char *argv[])
8577 const struct got_error *error = NULL;
8578 struct got_worktree *worktree = NULL;
8579 struct got_repository *repo = NULL;
8580 char *cwd = NULL, *path = NULL;
8581 struct got_pathlist_head paths;
8582 struct got_pathlist_entry *pe;
8583 int ch, can_recurse = 0, pflag = 0;
8584 FILE *patch_script_file = NULL;
8585 const char *patch_script_path = NULL;
8586 struct choose_patch_arg cpa;
8587 int *pack_fds = NULL;
8591 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8594 patch_script_path = optarg;
8612 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8613 "unveil", NULL) == -1)
8618 if (patch_script_path && !pflag)
8619 errx(1, "-F option can only be used together with -p option");
8621 cwd = getcwd(NULL, 0);
8623 error = got_error_from_errno("getcwd");
8627 error = got_repo_pack_fds_open(&pack_fds);
8631 error = got_worktree_open(&worktree, cwd);
8633 if (error->code == GOT_ERR_NOT_WORKTREE)
8634 error = wrap_not_worktree_error(error, "revert", cwd);
8638 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8643 if (patch_script_path) {
8644 patch_script_file = fopen(patch_script_path, "re");
8645 if (patch_script_file == NULL) {
8646 error = got_error_from_errno2("fopen",
8653 * XXX "c" perm needed on repo dir to delete merge references.
8655 error = apply_unveil(got_repo_get_path(repo), 0,
8656 got_worktree_get_root_path(worktree));
8660 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8667 TAILQ_FOREACH(pe, &paths, entry) {
8668 if (asprintf(&ondisk_path, "%s/%s",
8669 got_worktree_get_root_path(worktree),
8671 error = got_error_from_errno("asprintf");
8674 if (lstat(ondisk_path, &sb) == -1) {
8675 if (errno == ENOENT) {
8679 error = got_error_from_errno2("lstat",
8685 if (S_ISDIR(sb.st_mode)) {
8686 error = got_error_msg(GOT_ERR_BAD_PATH,
8687 "reverting directories requires -R option");
8693 cpa.patch_script_file = patch_script_file;
8694 cpa.action = "revert";
8695 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8696 pflag ? choose_patch : NULL, &cpa, repo);
8698 error = rm_logmsg_ref(worktree, repo);
8700 if (patch_script_file && fclose(patch_script_file) == EOF &&
8702 error = got_error_from_errno2("fclose", patch_script_path);
8704 const struct got_error *close_err = got_repo_close(repo);
8709 got_worktree_close(worktree);
8711 const struct got_error *pack_err =
8712 got_repo_pack_fds_close(pack_fds);
8716 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8725 fprintf(stderr, "usage: %s commit [-NS] [-A author] [-F path] "
8726 "[-m message] [path ...]\n", getprogname());
8730 struct collect_commit_logmsg_arg {
8731 const char *cmdline_log;
8732 const char *prepared_log;
8733 const char *merged_log;
8734 int non_interactive;
8736 const char *worktree_path;
8737 const char *branch_name;
8738 const char *repo_path;
8743 static const struct got_error *
8744 read_prepared_logmsg(char **logmsg, const char *path)
8746 const struct got_error *err = NULL;
8752 memset(&sb, 0, sizeof(sb));
8754 f = fopen(path, "re");
8756 return got_error_from_errno2("fopen", path);
8758 if (fstat(fileno(f), &sb) == -1) {
8759 err = got_error_from_errno2("fstat", path);
8762 if (sb.st_size == 0) {
8763 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
8767 *logmsg = malloc(sb.st_size + 1);
8768 if (*logmsg == NULL) {
8769 err = got_error_from_errno("malloc");
8773 r = fread(*logmsg, 1, sb.st_size, f);
8774 if (r != sb.st_size) {
8776 err = got_error_from_errno2("fread", path);
8778 err = got_error(GOT_ERR_IO);
8781 (*logmsg)[sb.st_size] = '\0';
8783 if (fclose(f) == EOF && err == NULL)
8784 err = got_error_from_errno2("fclose", path);
8792 static const struct got_error *
8793 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
8794 const char *diff_path, char **logmsg, void *arg)
8796 char *initial_content = NULL;
8797 struct got_pathlist_entry *pe;
8798 const struct got_error *err = NULL;
8799 char *template = NULL;
8800 char *prepared_msg = NULL, *merged_msg = NULL;
8801 struct collect_commit_logmsg_arg *a = arg;
8802 int initial_content_len;
8806 /* if a message was specified on the command line, just use it */
8807 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
8808 len = strlen(a->cmdline_log) + 1;
8809 *logmsg = malloc(len + 1);
8810 if (*logmsg == NULL)
8811 return got_error_from_errno("malloc");
8812 strlcpy(*logmsg, a->cmdline_log, len);
8814 } else if (a->prepared_log != NULL && a->non_interactive)
8815 return read_prepared_logmsg(logmsg, a->prepared_log);
8817 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
8818 return got_error_from_errno("asprintf");
8820 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
8824 if (a->prepared_log) {
8825 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
8828 } else if (a->merged_log) {
8829 err = read_prepared_logmsg(&merged_msg, a->merged_log);
8834 initial_content_len = asprintf(&initial_content,
8835 "%s%s\n# changes to be committed on branch %s:\n",
8836 prepared_msg ? prepared_msg : "",
8837 merged_msg ? merged_msg : "", a->branch_name);
8838 if (initial_content_len == -1) {
8839 err = got_error_from_errno("asprintf");
8843 if (write(fd, initial_content, initial_content_len) == -1) {
8844 err = got_error_from_errno2("write", a->logmsg_path);
8848 TAILQ_FOREACH(pe, commitable_paths, entry) {
8849 struct got_commitable *ct = pe->data;
8850 dprintf(fd, "# %c %s\n",
8851 got_commitable_get_status(ct),
8852 got_commitable_get_path(ct));
8856 dprintf(fd, "# detailed changes can be viewed in %s\n",
8860 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
8861 initial_content_len, a->prepared_log ? 0 : 1);
8863 free(initial_content);
8868 if (fd != -1 && close(fd) == -1 && err == NULL)
8869 err = got_error_from_errno2("close", a->logmsg_path);
8871 /* Editor is done; we can now apply unveil(2) */
8873 err = apply_unveil(a->repo_path, 0, a->worktree_path);
8881 static const struct got_error *
8882 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
8883 const char *type, int has_content)
8885 const struct got_error *err = NULL;
8886 char *logmsg = NULL;
8888 err = got_object_commit_get_logmsg(&logmsg, commit);
8892 if (fprintf(f, "%s# log message of %s commit %s:%s",
8893 has_content ? "\n" : "", type, idstr, logmsg) < 0)
8894 err = got_ferror(f, GOT_ERR_IO);
8901 * Lookup "logmsg" references of backed-out and cherrypicked commits
8902 * belonging to the current work tree. If found, and the worktree has
8903 * at least one modified file that was changed in the referenced commit,
8904 * add its log message to a new temporary file at *logmsg_path.
8905 * Add all refs found to matched_refs to be scheduled for removal on
8906 * successful commit.
8908 static const struct got_error *
8909 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
8910 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
8911 struct got_repository *repo)
8913 const struct got_error *err;
8914 struct got_commit_object *commit = NULL;
8915 struct got_object_id *id = NULL;
8916 struct got_reflist_head refs;
8917 struct got_reflist_entry *re, *re_match;
8919 char *uuidstr = NULL;
8920 int added_logmsg = 0;
8924 *logmsg_path = NULL;
8926 err = got_worktree_get_uuid(&uuidstr, worktree);
8930 err = got_ref_list(&refs, repo, "refs/got/worktree",
8931 got_ref_cmp_by_name, repo);
8935 TAILQ_FOREACH(re, &refs, entry) {
8936 const char *refname, *type;
8937 struct wt_commitable_path_arg wcpa;
8940 refname = got_ref_get_name(re->ref);
8942 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8943 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
8944 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8945 type = "cherrypicked";
8946 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8947 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
8948 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8949 type = "backed-out";
8953 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8954 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8958 err = got_repo_match_object_id(&id, NULL, refname,
8959 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8963 err = got_object_open_as_commit(&commit, repo, id);
8967 wcpa.commit_paths = paths;
8968 wcpa.has_changes = &add_logmsg;
8970 err = commit_path_changed_in_worktree(&wcpa, id,
8977 err = got_opentemp_named(logmsg_path, &f,
8978 "got-commit-logmsg", "");
8982 err = cat_logmsg(f, commit, refname, type,
8989 err = got_reflist_entry_dup(&re_match, re);
8992 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
8995 got_object_commit_close(commit);
9004 got_ref_list_free(&refs);
9006 got_object_commit_close(commit);
9007 if (f && fclose(f) == EOF && err == NULL)
9008 err = got_error_from_errno("fclose");
9009 if (!added_logmsg) {
9010 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9011 err = got_error_from_errno2("unlink", *logmsg_path);
9012 *logmsg_path = NULL;
9017 static const struct got_error *
9018 cmd_commit(int argc, char *argv[])
9020 const struct got_error *error = NULL;
9021 struct got_worktree *worktree = NULL;
9022 struct got_repository *repo = NULL;
9023 char *cwd = NULL, *id_str = NULL;
9024 struct got_object_id *id = NULL;
9025 const char *logmsg = NULL;
9026 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9027 struct collect_commit_logmsg_arg cl_arg;
9028 const char *author = NULL;
9029 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9030 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9031 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9033 struct got_pathlist_head paths;
9034 struct got_reflist_head refs;
9035 struct got_reflist_entry *re;
9036 int *pack_fds = NULL;
9040 cl_arg.logmsg_path = NULL;
9042 while ((ch = getopt(argc, argv, "A:F:m:NnS")) != -1) {
9046 error = valid_author(author);
9052 option_conflict('F', 'm');
9053 prepared_logmsg = realpath(optarg, NULL);
9054 if (prepared_logmsg == NULL)
9055 return got_error_from_errno2("realpath",
9059 if (prepared_logmsg)
9060 option_conflict('m', 'F');
9064 non_interactive = 1;
9070 allow_bad_symlinks = 1;
9082 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9083 "unveil", NULL) == -1)
9086 cwd = getcwd(NULL, 0);
9088 error = got_error_from_errno("getcwd");
9092 error = got_repo_pack_fds_open(&pack_fds);
9096 error = got_worktree_open(&worktree, cwd);
9098 if (error->code == GOT_ERR_NOT_WORKTREE)
9099 error = wrap_not_worktree_error(error, "commit", cwd);
9103 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9106 if (rebase_in_progress) {
9107 error = got_error(GOT_ERR_REBASING);
9111 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9116 error = get_gitconfig_path(&gitconfig_path);
9119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9120 gitconfig_path, pack_fds);
9124 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9127 if (merge_in_progress) {
9128 error = got_error(GOT_ERR_MERGE_BUSY);
9132 error = get_author(&committer, repo, worktree);
9136 if (author != NULL && !strcmp(committer, author)) {
9137 error = got_error(GOT_ERR_COMMIT_REDUNDANT_AUTHOR);
9145 * unveil(2) traverses exec(2); if an editor is used we have
9146 * to apply unveil after the log message has been written.
9148 if (logmsg == NULL || strlen(logmsg) == 0)
9149 error = get_editor(&editor);
9151 error = apply_unveil(got_repo_get_path(repo), 0,
9152 got_worktree_get_root_path(worktree));
9156 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9160 if (prepared_logmsg == NULL) {
9161 error = lookup_logmsg_ref(&merged_logmsg,
9162 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9167 cl_arg.editor = editor;
9168 cl_arg.cmdline_log = logmsg;
9169 cl_arg.prepared_log = prepared_logmsg;
9170 cl_arg.merged_log = merged_logmsg;
9171 cl_arg.non_interactive = non_interactive;
9172 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9173 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9174 if (!histedit_in_progress) {
9175 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9176 error = got_error(GOT_ERR_COMMIT_BRANCH);
9179 cl_arg.branch_name += 11;
9181 cl_arg.repo_path = got_repo_get_path(repo);
9182 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9183 allow_bad_symlinks, show_diff, collect_commit_logmsg, &cl_arg,
9184 print_status, NULL, repo);
9186 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9187 cl_arg.logmsg_path != NULL)
9188 preserve_logmsg = 1;
9192 error = got_object_id_str(&id_str, id);
9195 printf("Created commit %s\n", id_str);
9197 TAILQ_FOREACH(re, &refs, entry) {
9198 error = got_ref_delete(re->ref, repo);
9204 if (preserve_logmsg) {
9205 fprintf(stderr, "%s: log message preserved in %s\n",
9206 getprogname(), cl_arg.logmsg_path);
9207 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9209 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9210 free(cl_arg.logmsg_path);
9211 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9212 error = got_error_from_errno2("unlink", merged_logmsg);
9213 free(merged_logmsg);
9215 const struct got_error *close_err = got_repo_close(repo);
9220 got_worktree_close(worktree);
9222 const struct got_error *pack_err =
9223 got_repo_pack_fds_close(pack_fds);
9227 got_ref_list_free(&refs);
9228 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9231 free(gitconfig_path);
9234 free(prepared_logmsg);
9241 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9242 "[-r repository-path] [-t tag] [remote-repository]\n",
9248 print_load_info(int print_colored, int print_found, int print_trees,
9249 int ncolored, int nfound, int ntrees)
9251 if (print_colored) {
9252 printf("%d commit%s colored", ncolored,
9253 ncolored == 1 ? "" : "s");
9256 printf("%s%d object%s found",
9257 ncolored > 0 ? "; " : "",
9258 nfound, nfound == 1 ? "" : "s");
9261 printf("; %d tree%s scanned", ntrees,
9262 ntrees == 1 ? "" : "s");
9266 struct got_send_progress_arg {
9267 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9274 int last_nobj_total;
9278 int printed_something;
9280 struct got_pathlist_head *delete_branches;
9283 static const struct got_error *
9284 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9285 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9286 int nobj_written, off_t bytes_sent, const char *refname,
9287 const char *errmsg, int success)
9289 struct got_send_progress_arg *a = arg;
9290 char scaled_packsize[FMT_SCALED_STRSIZE];
9291 char scaled_sent[FMT_SCALED_STRSIZE];
9292 int p_deltify = 0, p_written = 0, p_sent = 0;
9293 int print_colored = 0, print_found = 0, print_trees = 0;
9294 int print_searching = 0, print_total = 0;
9295 int print_deltify = 0, print_written = 0, print_sent = 0;
9297 if (a->verbosity < 0)
9301 const char *status = success ? "accepted" : "rejected";
9304 struct got_pathlist_entry *pe;
9305 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9306 const char *branchname = pe->path;
9307 if (got_path_cmp(branchname, refname,
9308 strlen(branchname), strlen(refname)) == 0) {
9310 a->sent_something = 1;
9316 if (a->printed_something)
9318 printf("Server has %s %s", status, refname);
9320 printf(": %s", errmsg);
9321 a->printed_something = 1;
9325 if (a->last_ncolored != ncolored) {
9327 a->last_ncolored = ncolored;
9330 if (a->last_nfound != nfound) {
9333 a->last_nfound = nfound;
9336 if (a->last_ntrees != ntrees) {
9340 a->last_ntrees = ntrees;
9343 if ((print_colored || print_found || print_trees) &&
9346 print_load_info(print_colored, print_found, print_trees,
9347 ncolored, nfound, ntrees);
9348 a->printed_something = 1;
9351 } else if (!a->loading_done) {
9353 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9355 a->loading_done = 1;
9358 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9359 return got_error_from_errno("fmt_scaled");
9360 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9361 return got_error_from_errno("fmt_scaled");
9363 if (a->last_ncommits != ncommits) {
9364 print_searching = 1;
9365 a->last_ncommits = ncommits;
9368 if (a->last_nobj_total != nobj_total) {
9369 print_searching = 1;
9371 a->last_nobj_total = nobj_total;
9374 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9375 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9376 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9377 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9378 return got_error(GOT_ERR_NO_SPACE);
9381 if (nobj_deltify > 0 || nobj_written > 0) {
9382 if (nobj_deltify > 0) {
9383 p_deltify = (nobj_deltify * 100) / nobj_total;
9384 if (p_deltify != a->last_p_deltify) {
9385 a->last_p_deltify = p_deltify;
9386 print_searching = 1;
9391 if (nobj_written > 0) {
9392 p_written = (nobj_written * 100) / nobj_total;
9393 if (p_written != a->last_p_written) {
9394 a->last_p_written = p_written;
9395 print_searching = 1;
9403 if (bytes_sent > 0) {
9404 p_sent = (bytes_sent * 100) / packfile_size;
9405 if (p_sent != a->last_p_sent) {
9406 a->last_p_sent = p_sent;
9407 print_searching = 1;
9413 a->sent_something = 1;
9416 if (print_searching || print_total || print_deltify || print_written ||
9419 if (print_searching)
9420 printf("packing %d reference%s", ncommits,
9421 ncommits == 1 ? "" : "s");
9423 printf("; %d object%s", nobj_total,
9424 nobj_total == 1 ? "" : "s");
9426 printf("; deltify: %d%%", p_deltify);
9428 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9429 scaled_packsize, p_sent);
9430 else if (print_written)
9431 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9432 scaled_packsize, p_written);
9433 if (print_searching || print_total || print_deltify ||
9434 print_written || print_sent) {
9435 a->printed_something = 1;
9441 static const struct got_error *
9442 cmd_send(int argc, char *argv[])
9444 const struct got_error *error = NULL;
9445 char *cwd = NULL, *repo_path = NULL;
9446 const char *remote_name;
9447 char *proto = NULL, *host = NULL, *port = NULL;
9448 char *repo_name = NULL, *server_path = NULL;
9449 const struct got_remote_repo *remotes, *remote = NULL;
9450 int nremotes, nbranches = 0, ndelete_branches = 0;
9451 struct got_repository *repo = NULL;
9452 struct got_worktree *worktree = NULL;
9453 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9454 struct got_pathlist_head branches;
9455 struct got_pathlist_head tags;
9456 struct got_reflist_head all_branches;
9457 struct got_reflist_head all_tags;
9458 struct got_pathlist_head delete_args;
9459 struct got_pathlist_head delete_branches;
9460 struct got_reflist_entry *re;
9461 struct got_pathlist_entry *pe;
9462 int i, ch, sendfd = -1, sendstatus;
9464 struct got_send_progress_arg spa;
9465 int verbosity = 0, overwrite_refs = 0;
9466 int send_all_branches = 0, send_all_tags = 0;
9467 struct got_reference *ref = NULL;
9468 int *pack_fds = NULL;
9470 TAILQ_INIT(&branches);
9472 TAILQ_INIT(&all_branches);
9473 TAILQ_INIT(&all_tags);
9474 TAILQ_INIT(&delete_args);
9475 TAILQ_INIT(&delete_branches);
9477 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9480 send_all_branches = 1;
9483 error = got_pathlist_append(&branches, optarg, NULL);
9489 error = got_pathlist_append(&delete_args, optarg, NULL);
9500 repo_path = realpath(optarg, NULL);
9501 if (repo_path == NULL)
9502 return got_error_from_errno2("realpath",
9504 got_path_strip_trailing_slashes(repo_path);
9510 error = got_pathlist_append(&tags, optarg, NULL);
9517 else if (verbosity < 3)
9528 if (send_all_branches && !TAILQ_EMPTY(&branches))
9529 option_conflict('a', 'b');
9530 if (send_all_tags && !TAILQ_EMPTY(&tags))
9531 option_conflict('T', 't');
9535 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9537 remote_name = argv[0];
9541 cwd = getcwd(NULL, 0);
9543 error = got_error_from_errno("getcwd");
9547 error = got_repo_pack_fds_open(&pack_fds);
9551 if (repo_path == NULL) {
9552 error = got_worktree_open(&worktree, cwd);
9553 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9559 strdup(got_worktree_get_repo_path(worktree));
9560 if (repo_path == NULL)
9561 error = got_error_from_errno("strdup");
9565 repo_path = strdup(cwd);
9566 if (repo_path == NULL) {
9567 error = got_error_from_errno("strdup");
9573 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9578 worktree_conf = got_worktree_get_gotconfig(worktree);
9579 if (worktree_conf) {
9580 got_gotconfig_get_remotes(&nremotes, &remotes,
9582 for (i = 0; i < nremotes; i++) {
9583 if (strcmp(remotes[i].name, remote_name) == 0) {
9584 remote = &remotes[i];
9590 if (remote == NULL) {
9591 repo_conf = got_repo_get_gotconfig(repo);
9593 got_gotconfig_get_remotes(&nremotes, &remotes,
9595 for (i = 0; i < nremotes; i++) {
9596 if (strcmp(remotes[i].name, remote_name) == 0) {
9597 remote = &remotes[i];
9603 if (remote == NULL) {
9604 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9605 for (i = 0; i < nremotes; i++) {
9606 if (strcmp(remotes[i].name, remote_name) == 0) {
9607 remote = &remotes[i];
9612 if (remote == NULL) {
9613 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9617 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9618 &repo_name, remote->send_url);
9622 if (strcmp(proto, "git") == 0) {
9624 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9625 "sendfd dns inet unveil", NULL) == -1)
9628 } else if (strcmp(proto, "git+ssh") == 0 ||
9629 strcmp(proto, "ssh") == 0) {
9631 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9632 "sendfd unveil", NULL) == -1)
9635 } else if (strcmp(proto, "http") == 0 ||
9636 strcmp(proto, "git+http") == 0) {
9637 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9640 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9644 error = got_dial_apply_unveil(proto);
9648 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9652 if (send_all_branches) {
9653 error = got_ref_list(&all_branches, repo, "refs/heads",
9654 got_ref_cmp_by_name, NULL);
9657 TAILQ_FOREACH(re, &all_branches, entry) {
9658 const char *branchname = got_ref_get_name(re->ref);
9659 error = got_pathlist_append(&branches,
9665 } else if (nbranches == 0) {
9666 for (i = 0; i < remote->nsend_branches; i++) {
9667 error = got_pathlist_append(&branches,
9668 remote->send_branches[i], NULL);
9674 if (send_all_tags) {
9675 error = got_ref_list(&all_tags, repo, "refs/tags",
9676 got_ref_cmp_by_name, NULL);
9679 TAILQ_FOREACH(re, &all_tags, entry) {
9680 const char *tagname = got_ref_get_name(re->ref);
9681 error = got_pathlist_append(&tags,
9689 * To prevent accidents only branches in refs/heads/ can be deleted
9690 * with 'got send -d'.
9691 * Deleting anything else requires local repository access or Git.
9693 TAILQ_FOREACH(pe, &delete_args, entry) {
9694 const char *branchname = pe->path;
9696 struct got_pathlist_entry *new;
9697 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9698 s = strdup(branchname);
9700 error = got_error_from_errno("strdup");
9704 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9705 error = got_error_from_errno("asprintf");
9709 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9710 if (error || new == NULL /* duplicate */)
9717 if (nbranches == 0 && ndelete_branches == 0) {
9718 struct got_reference *head_ref;
9720 error = got_ref_open(&head_ref, repo,
9721 got_worktree_get_head_ref_name(worktree), 0);
9723 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
9726 if (got_ref_is_symbolic(head_ref)) {
9727 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
9728 got_ref_close(head_ref);
9733 error = got_pathlist_append(&branches, got_ref_get_name(ref),
9740 if (verbosity >= 0) {
9741 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
9742 remote->name, proto, host,
9743 port ? ":" : "", port ? port : "",
9744 *server_path == '/' ? "" : "/", server_path);
9747 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
9748 server_path, verbosity);
9752 memset(&spa, 0, sizeof(spa));
9753 spa.last_scaled_packsize[0] = '\0';
9754 spa.last_p_deltify = -1;
9755 spa.last_p_written = -1;
9756 spa.verbosity = verbosity;
9757 spa.delete_branches = &delete_branches;
9758 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
9759 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
9760 check_cancelled, NULL);
9761 if (spa.printed_something)
9765 if (!spa.sent_something && verbosity >= 0)
9766 printf("Already up-to-date\n");
9769 if (kill(sendpid, SIGTERM) == -1)
9770 error = got_error_from_errno("kill");
9771 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
9772 error = got_error_from_errno("waitpid");
9774 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
9775 error = got_error_from_errno("close");
9777 const struct got_error *close_err = got_repo_close(repo);
9782 got_worktree_close(worktree);
9784 const struct got_error *pack_err =
9785 got_repo_pack_fds_close(pack_fds);
9791 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
9792 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
9793 got_ref_list_free(&all_branches);
9794 got_ref_list_free(&all_tags);
9795 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
9796 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
9808 * Print and if delete is set delete all ref_prefix references.
9809 * If wanted_ref is not NULL, only print or delete this reference.
9811 static const struct got_error *
9812 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
9813 const char *wanted_ref, int delete, struct got_worktree *worktree,
9814 struct got_repository *repo)
9816 const struct got_error *err;
9817 struct got_pathlist_head paths;
9818 struct got_reflist_head refs;
9819 struct got_reflist_entry *re;
9820 struct got_reflist_object_id_map *refs_idmap = NULL;
9821 struct got_commit_object *commit = NULL;
9822 struct got_object_id *id = NULL;
9823 const char *header_prefix;
9824 char *uuidstr = NULL;
9830 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
9834 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
9838 if (worktree != NULL) {
9839 err = got_worktree_get_uuid(&uuidstr, worktree);
9845 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
9849 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
9850 header_prefix = "backout";
9852 header_prefix = "cherrypick";
9854 TAILQ_FOREACH(re, &refs, entry) {
9855 const char *refname, *wt;
9857 refname = got_ref_get_name(re->ref);
9859 err = check_cancelled(NULL);
9863 if (strncmp(refname, ref_prefix, prefix_len) == 0)
9864 refname += prefix_len + 1; /* skip '-' delimiter */
9870 if (worktree == NULL || strncmp(refname, uuidstr,
9871 GOT_WORKTREE_UUID_STRLEN) == 0)
9872 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9876 err = got_repo_match_object_id(&id, NULL, refname,
9877 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9881 err = got_object_open_as_commit(&commit, repo, id);
9886 found = strncmp(wanted_ref, refname,
9887 strlen(wanted_ref)) == 0;
9888 if (wanted_ref && !found) {
9889 struct got_reflist_head *ci_refs;
9891 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
9895 char *refs_str = NULL;
9896 char const *r = NULL;
9898 err = build_refs_str(&refs_str, ci_refs, id,
9905 if (strncmp(r, wanted_ref,
9906 strlen(wanted_ref)) == 0) {
9918 if (wanted_ref == NULL || found) {
9920 err = got_ref_delete(re->ref, repo);
9923 printf("Deleted: ");
9924 err = print_commit_oneline(commit, id, repo,
9928 * Print paths modified by commit to help
9929 * associate commits with worktree changes.
9931 err = get_changed_paths(&paths, commit,
9936 err = print_commit(commit, id, repo, NULL,
9937 &paths, NULL, 0, 0, refs_idmap, NULL,
9939 got_pathlist_free(&paths,
9940 GOT_PATHLIST_FREE_ALL);
9942 if (worktree == NULL)
9943 printf("work tree: %.*s\n\n",
9944 GOT_WORKTREE_UUID_STRLEN, wt);
9950 got_object_commit_close(commit);
9956 if (wanted_ref != NULL && !found)
9957 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
9962 got_ref_list_free(&refs);
9963 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
9965 got_reflist_object_id_map_free(refs_idmap);
9967 got_object_commit_close(commit);
9972 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
9973 * identified by id for log messages to prepopulate the editor on commit.
9975 static const struct got_error *
9976 logmsg_ref(struct got_object_id *id, const char *prefix,
9977 struct got_worktree *worktree, struct got_repository *repo)
9979 const struct got_error *err = NULL;
9980 char *idstr, *ref = NULL, *refname = NULL;
9981 int histedit_in_progress;
9982 int rebase_in_progress, merge_in_progress;
9985 * Silenty refuse to create merge reference if any histedit, merge,
9986 * or rebase operation is in progress.
9988 err = got_worktree_histedit_in_progress(&histedit_in_progress,
9992 if (histedit_in_progress)
9995 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9998 if (rebase_in_progress)
10001 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10005 if (merge_in_progress)
10008 err = got_object_id_str(&idstr, id);
10012 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10016 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10017 err = got_error_from_errno("asprintf");
10021 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10031 usage_cherrypick(void)
10033 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10038 static const struct got_error *
10039 cmd_cherrypick(int argc, char *argv[])
10041 const struct got_error *error = NULL;
10042 struct got_worktree *worktree = NULL;
10043 struct got_repository *repo = NULL;
10044 char *cwd = NULL, *commit_id_str = NULL;
10045 struct got_object_id *commit_id = NULL;
10046 struct got_commit_object *commit = NULL;
10047 struct got_object_qid *pid;
10048 int ch, list_refs = 0, remove_refs = 0;
10049 struct got_update_progress_arg upa;
10050 int *pack_fds = NULL;
10052 while ((ch = getopt(argc, argv, "lX")) != -1) {
10061 usage_cherrypick();
10070 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10071 "unveil", NULL) == -1)
10074 if (list_refs || remove_refs) {
10075 if (argc != 0 && argc != 1)
10076 usage_cherrypick();
10077 } else if (argc != 1)
10078 usage_cherrypick();
10079 if (list_refs && remove_refs)
10080 option_conflict('l', 'X');
10082 cwd = getcwd(NULL, 0);
10084 error = got_error_from_errno("getcwd");
10088 error = got_repo_pack_fds_open(&pack_fds);
10092 error = got_worktree_open(&worktree, cwd);
10094 if (list_refs || remove_refs) {
10095 if (error->code != GOT_ERR_NOT_WORKTREE)
10098 if (error->code == GOT_ERR_NOT_WORKTREE)
10099 error = wrap_not_worktree_error(error,
10100 "cherrypick", cwd);
10105 error = got_repo_open(&repo,
10106 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10111 error = apply_unveil(got_repo_get_path(repo), 0,
10112 worktree ? got_worktree_get_root_path(worktree) : NULL);
10116 if (list_refs || remove_refs) {
10117 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10118 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10119 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10123 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10124 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10127 error = got_object_id_str(&commit_id_str, commit_id);
10131 error = got_object_open_as_commit(&commit, repo, commit_id);
10134 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10135 memset(&upa, 0, sizeof(upa));
10136 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10137 commit_id, repo, update_progress, &upa, check_cancelled,
10142 if (upa.did_something) {
10143 error = logmsg_ref(commit_id,
10144 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10147 printf("Merged commit %s\n", commit_id_str);
10149 print_merge_progress_stats(&upa);
10153 got_object_commit_close(commit);
10154 free(commit_id_str);
10156 got_worktree_close(worktree);
10158 const struct got_error *close_err = got_repo_close(repo);
10163 const struct got_error *pack_err =
10164 got_repo_pack_fds_close(pack_fds);
10173 usage_backout(void)
10175 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10179 static const struct got_error *
10180 cmd_backout(int argc, char *argv[])
10182 const struct got_error *error = NULL;
10183 struct got_worktree *worktree = NULL;
10184 struct got_repository *repo = NULL;
10185 char *cwd = NULL, *commit_id_str = NULL;
10186 struct got_object_id *commit_id = NULL;
10187 struct got_commit_object *commit = NULL;
10188 struct got_object_qid *pid;
10189 int ch, list_refs = 0, remove_refs = 0;
10190 struct got_update_progress_arg upa;
10191 int *pack_fds = NULL;
10193 while ((ch = getopt(argc, argv, "lX")) != -1) {
10211 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10212 "unveil", NULL) == -1)
10215 if (list_refs || remove_refs) {
10216 if (argc != 0 && argc != 1)
10218 } else if (argc != 1)
10220 if (list_refs && remove_refs)
10221 option_conflict('l', 'X');
10223 cwd = getcwd(NULL, 0);
10225 error = got_error_from_errno("getcwd");
10229 error = got_repo_pack_fds_open(&pack_fds);
10233 error = got_worktree_open(&worktree, cwd);
10235 if (list_refs || remove_refs) {
10236 if (error->code != GOT_ERR_NOT_WORKTREE)
10239 if (error->code == GOT_ERR_NOT_WORKTREE)
10240 error = wrap_not_worktree_error(error,
10246 error = got_repo_open(&repo,
10247 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10252 error = apply_unveil(got_repo_get_path(repo), 0,
10253 worktree ? got_worktree_get_root_path(worktree) : NULL);
10257 if (list_refs || remove_refs) {
10258 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10259 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10260 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10264 error = got_repo_match_object_id(&commit_id, NULL, argv[0],
10265 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10268 error = got_object_id_str(&commit_id_str, commit_id);
10272 error = got_object_open_as_commit(&commit, repo, commit_id);
10275 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10277 error = got_error(GOT_ERR_ROOT_COMMIT);
10281 memset(&upa, 0, sizeof(upa));
10282 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10283 repo, update_progress, &upa, check_cancelled, NULL);
10287 if (upa.did_something) {
10288 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10292 printf("Backed out commit %s\n", commit_id_str);
10294 print_merge_progress_stats(&upa);
10298 got_object_commit_close(commit);
10299 free(commit_id_str);
10301 got_worktree_close(worktree);
10303 const struct got_error *close_err = got_repo_close(repo);
10308 const struct got_error *pack_err =
10309 got_repo_pack_fds_close(pack_fds);
10319 fprintf(stderr, "usage: %s rebase [-aclX] [branch]\n", getprogname());
10324 trim_logmsg(char *logmsg, int limit)
10329 len = strlen(logmsg);
10332 logmsg[len] = '\0';
10333 nl = strchr(logmsg, '\n');
10338 static const struct got_error *
10339 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10341 const struct got_error *err;
10342 char *logmsg0 = NULL;
10345 err = got_object_commit_get_logmsg(&logmsg0, commit);
10350 while (isspace((unsigned char)s[0]))
10353 *logmsg = strdup(s);
10354 if (*logmsg == NULL) {
10355 err = got_error_from_errno("strdup");
10359 trim_logmsg(*logmsg, limit);
10365 static const struct got_error *
10366 show_rebase_merge_conflict(struct got_object_id *id,
10367 struct got_repository *repo)
10369 const struct got_error *err;
10370 struct got_commit_object *commit = NULL;
10371 char *id_str = NULL, *logmsg = NULL;
10373 err = got_object_open_as_commit(&commit, repo, id);
10377 err = got_object_id_str(&id_str, id);
10383 err = get_short_logmsg(&logmsg, 42, commit);
10387 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10390 got_object_commit_close(commit);
10395 static const struct got_error *
10396 show_rebase_progress(struct got_commit_object *commit,
10397 struct got_object_id *old_id, struct got_object_id *new_id)
10399 const struct got_error *err;
10400 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10402 err = got_object_id_str(&old_id_str, old_id);
10407 err = got_object_id_str(&new_id_str, new_id);
10412 old_id_str[12] = '\0';
10414 new_id_str[12] = '\0';
10416 err = get_short_logmsg(&logmsg, 42, commit);
10420 printf("%s -> %s: %s\n", old_id_str,
10421 new_id_str ? new_id_str : "no-op change", logmsg);
10429 static const struct got_error *
10430 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10431 struct got_reference *branch, struct got_reference *tmp_branch,
10432 struct got_repository *repo, int create_backup)
10434 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10435 return got_worktree_rebase_complete(worktree, fileindex,
10436 tmp_branch, branch, repo, create_backup);
10439 static const struct got_error *
10440 rebase_commit(struct got_pathlist_head *merged_paths,
10441 struct got_worktree *worktree, struct got_fileindex *fileindex,
10442 struct got_reference *tmp_branch, const char *committer,
10443 struct got_object_id *commit_id, struct got_repository *repo)
10445 const struct got_error *error;
10446 struct got_commit_object *commit;
10447 struct got_object_id *new_commit_id;
10449 error = got_object_open_as_commit(&commit, repo, commit_id);
10453 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10454 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10457 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10459 error = show_rebase_progress(commit, commit_id, NULL);
10461 error = show_rebase_progress(commit, commit_id, new_commit_id);
10462 free(new_commit_id);
10465 got_object_commit_close(commit);
10469 struct check_path_prefix_arg {
10470 const char *path_prefix;
10475 static const struct got_error *
10476 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10477 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10478 struct got_object_id *id1, struct got_object_id *id2,
10479 const char *path1, const char *path2,
10480 mode_t mode1, mode_t mode2, struct got_repository *repo)
10482 struct check_path_prefix_arg *a = arg;
10484 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10485 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10486 return got_error(a->errcode);
10491 static const struct got_error *
10492 check_path_prefix(struct got_object_id *parent_id,
10493 struct got_object_id *commit_id, const char *path_prefix,
10494 int errcode, struct got_repository *repo)
10496 const struct got_error *err;
10497 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10498 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10499 struct check_path_prefix_arg cpp_arg;
10501 if (got_path_is_root_dir(path_prefix))
10504 err = got_object_open_as_commit(&commit, repo, commit_id);
10508 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10512 err = got_object_open_as_tree(&tree1, repo,
10513 got_object_commit_get_tree_id(parent_commit));
10517 err = got_object_open_as_tree(&tree2, repo,
10518 got_object_commit_get_tree_id(commit));
10522 cpp_arg.path_prefix = path_prefix;
10523 while (cpp_arg.path_prefix[0] == '/')
10524 cpp_arg.path_prefix++;
10525 cpp_arg.len = strlen(cpp_arg.path_prefix);
10526 cpp_arg.errcode = errcode;
10527 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10528 check_path_prefix_in_diff, &cpp_arg, 0);
10531 got_object_tree_close(tree1);
10533 got_object_tree_close(tree2);
10535 got_object_commit_close(commit);
10537 got_object_commit_close(parent_commit);
10541 static const struct got_error *
10542 collect_commits(struct got_object_id_queue *commits,
10543 struct got_object_id *initial_commit_id,
10544 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10545 const char *path_prefix, int path_prefix_errcode,
10546 struct got_repository *repo)
10548 const struct got_error *err = NULL;
10549 struct got_commit_graph *graph = NULL;
10550 struct got_object_id parent_id, commit_id;
10551 struct got_object_qid *qid;
10553 err = got_commit_graph_open(&graph, "/", 1);
10557 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
10558 check_cancelled, NULL);
10562 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10563 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10564 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10565 check_cancelled, NULL);
10567 if (err->code == GOT_ERR_ITER_COMPLETED) {
10568 err = got_error_msg(GOT_ERR_ANCESTRY,
10569 "ran out of commits to rebase before "
10570 "youngest common ancestor commit has "
10571 "been reached?!?");
10575 err = check_path_prefix(&parent_id, &commit_id,
10576 path_prefix, path_prefix_errcode, repo);
10580 err = got_object_qid_alloc(&qid, &commit_id);
10583 STAILQ_INSERT_HEAD(commits, qid, entry);
10585 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10589 got_commit_graph_close(graph);
10593 static const struct got_error *
10594 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10596 const struct got_error *err = NULL;
10597 time_t committer_time;
10599 char datebuf[11]; /* YYYY-MM-DD + NUL */
10600 char *author0 = NULL, *author, *smallerthan;
10601 char *logmsg0 = NULL, *logmsg, *newline;
10603 committer_time = got_object_commit_get_committer_time(commit);
10604 if (gmtime_r(&committer_time, &tm) == NULL)
10605 return got_error_from_errno("gmtime_r");
10606 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10607 return got_error(GOT_ERR_NO_SPACE);
10609 author0 = strdup(got_object_commit_get_author(commit));
10610 if (author0 == NULL)
10611 return got_error_from_errno("strdup");
10613 smallerthan = strchr(author, '<');
10614 if (smallerthan && smallerthan[1] != '\0')
10615 author = smallerthan + 1;
10616 author[strcspn(author, "@>")] = '\0';
10618 err = got_object_commit_get_logmsg(&logmsg0, commit);
10622 while (*logmsg == '\n')
10624 newline = strchr(logmsg, '\n');
10628 if (asprintf(brief_str, "%s %s %s",
10629 datebuf, author, logmsg) == -1)
10630 err = got_error_from_errno("asprintf");
10637 static const struct got_error *
10638 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10639 struct got_repository *repo)
10641 const struct got_error *err;
10644 err = got_object_id_str(&id_str, id);
10648 err = got_ref_delete(ref, repo);
10652 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10658 static const struct got_error *
10659 print_backup_ref(const char *branch_name, const char *new_id_str,
10660 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10661 struct got_reflist_object_id_map *refs_idmap,
10662 struct got_repository *repo)
10664 const struct got_error *err = NULL;
10665 struct got_reflist_head *refs;
10666 char *refs_str = NULL;
10667 struct got_object_id *new_commit_id = NULL;
10668 struct got_commit_object *new_commit = NULL;
10669 char *new_commit_brief_str = NULL;
10670 struct got_object_id *yca_id = NULL;
10671 struct got_commit_object *yca_commit = NULL;
10672 char *yca_id_str = NULL, *yca_brief_str = NULL;
10673 char *custom_refs_str;
10675 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10676 return got_error_from_errno("asprintf");
10678 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10679 0, 0, refs_idmap, custom_refs_str, NULL);
10683 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10687 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10689 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
10694 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
10698 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
10702 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
10703 old_commit_id, new_commit_id, 1, repo, check_cancelled, NULL);
10707 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
10708 refs_str ? " (" : "", refs_str ? refs_str : "",
10709 refs_str ? ")" : "", new_commit_brief_str);
10710 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
10711 got_object_id_cmp(yca_id, old_commit_id) != 0) {
10715 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
10719 err = get_commit_brief_str(&yca_brief_str, yca_commit);
10723 err = got_object_id_str(&yca_id_str, yca_id);
10727 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
10729 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
10733 printf("history forked at %s%s%s%s\n %s\n",
10735 refs_str ? " (" : "", refs_str ? refs_str : "",
10736 refs_str ? ")" : "", yca_brief_str);
10739 free(custom_refs_str);
10740 free(new_commit_id);
10744 free(yca_brief_str);
10746 got_object_commit_close(new_commit);
10748 got_object_commit_close(yca_commit);
10753 static const struct got_error *
10754 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
10755 struct got_repository *repo)
10757 const struct got_error *err;
10758 struct got_reflist_head refs;
10759 struct got_reflist_entry *re;
10760 char *uuidstr = NULL;
10761 static char msg[160];
10765 err = got_worktree_get_uuid(&uuidstr, worktree);
10769 err = got_ref_list(&refs, repo, "refs/got/worktree",
10770 got_ref_cmp_by_name, repo);
10774 TAILQ_FOREACH(re, &refs, entry) {
10775 const char *cmd, *refname, *type;
10777 refname = got_ref_get_name(re->ref);
10779 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10780 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
10781 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
10782 cmd = "cherrypick";
10783 type = "cherrypicked";
10784 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10785 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
10786 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
10788 type = "backed-out";
10792 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
10795 snprintf(msg, sizeof(msg),
10796 "work tree has references created by %s commits which "
10797 "must be removed with 'got %s -X' before running the %s "
10798 "command", type, cmd, caller);
10799 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
10805 got_ref_list_free(&refs);
10809 static const struct got_error *
10810 process_backup_refs(const char *backup_ref_prefix,
10811 const char *wanted_branch_name,
10812 int delete, struct got_repository *repo)
10814 const struct got_error *err;
10815 struct got_reflist_head refs, backup_refs;
10816 struct got_reflist_entry *re;
10817 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
10818 struct got_object_id *old_commit_id = NULL;
10819 char *branch_name = NULL;
10820 struct got_commit_object *old_commit = NULL;
10821 struct got_reflist_object_id_map *refs_idmap = NULL;
10822 int wanted_branch_found = 0;
10825 TAILQ_INIT(&backup_refs);
10827 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
10831 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10835 if (wanted_branch_name) {
10836 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
10837 wanted_branch_name += 11;
10840 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
10841 got_ref_cmp_by_commit_timestamp_descending, repo);
10845 TAILQ_FOREACH(re, &backup_refs, entry) {
10846 const char *refname = got_ref_get_name(re->ref);
10849 err = check_cancelled(NULL);
10853 err = got_ref_resolve(&old_commit_id, repo, re->ref);
10857 err = got_object_open_as_commit(&old_commit, repo,
10862 if (strncmp(backup_ref_prefix, refname,
10863 backup_ref_prefix_len) == 0)
10864 refname += backup_ref_prefix_len;
10866 while (refname[0] == '/')
10869 branch_name = strdup(refname);
10870 if (branch_name == NULL) {
10871 err = got_error_from_errno("strdup");
10874 slash = strrchr(branch_name, '/');
10877 refname += strlen(branch_name) + 1;
10880 if (wanted_branch_name == NULL ||
10881 strcmp(wanted_branch_name, branch_name) == 0) {
10882 wanted_branch_found = 1;
10884 err = delete_backup_ref(re->ref,
10885 old_commit_id, repo);
10887 err = print_backup_ref(branch_name, refname,
10888 old_commit_id, old_commit, refs_idmap,
10895 free(old_commit_id);
10896 old_commit_id = NULL;
10898 branch_name = NULL;
10899 got_object_commit_close(old_commit);
10903 if (wanted_branch_name && !wanted_branch_found) {
10904 err = got_error_fmt(GOT_ERR_NOT_REF,
10905 "%s/%s/", backup_ref_prefix, wanted_branch_name);
10909 got_reflist_object_id_map_free(refs_idmap);
10910 got_ref_list_free(&refs);
10911 got_ref_list_free(&backup_refs);
10912 free(old_commit_id);
10915 got_object_commit_close(old_commit);
10919 static const struct got_error *
10920 abort_progress(void *arg, unsigned char status, const char *path)
10923 * Unversioned files should not clutter progress output when
10924 * an operation is aborted.
10926 if (status == GOT_STATUS_UNVERSIONED)
10929 return update_progress(arg, status, path);
10932 static const struct got_error *
10933 cmd_rebase(int argc, char *argv[])
10935 const struct got_error *error = NULL;
10936 struct got_worktree *worktree = NULL;
10937 struct got_repository *repo = NULL;
10938 struct got_fileindex *fileindex = NULL;
10939 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
10940 struct got_reference *branch = NULL;
10941 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
10942 struct got_object_id *commit_id = NULL, *parent_id = NULL;
10943 struct got_object_id *resume_commit_id = NULL;
10944 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
10945 struct got_object_id *head_commit_id = NULL;
10946 struct got_reference *head_ref = NULL;
10947 struct got_commit_object *commit = NULL;
10948 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
10949 int histedit_in_progress = 0, merge_in_progress = 0;
10950 int create_backup = 1, list_backups = 0, delete_backups = 0;
10951 struct got_object_id_queue commits;
10952 struct got_pathlist_head merged_paths;
10953 const struct got_object_id_queue *parent_ids;
10954 struct got_object_qid *qid, *pid;
10955 struct got_update_progress_arg upa;
10956 int *pack_fds = NULL;
10958 STAILQ_INIT(&commits);
10959 TAILQ_INIT(&merged_paths);
10960 memset(&upa, 0, sizeof(upa));
10962 while ((ch = getopt(argc, argv, "aclX")) != -1) {
10968 continue_rebase = 1;
10974 delete_backups = 1;
10986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10987 "unveil", NULL) == -1)
10990 if (list_backups) {
10992 option_conflict('l', 'a');
10993 if (continue_rebase)
10994 option_conflict('l', 'c');
10995 if (delete_backups)
10996 option_conflict('l', 'X');
10997 if (argc != 0 && argc != 1)
10999 } else if (delete_backups) {
11001 option_conflict('X', 'a');
11002 if (continue_rebase)
11003 option_conflict('X', 'c');
11005 option_conflict('l', 'X');
11006 if (argc != 0 && argc != 1)
11009 if (abort_rebase && continue_rebase)
11011 else if (abort_rebase || continue_rebase) {
11014 } else if (argc != 1)
11018 cwd = getcwd(NULL, 0);
11020 error = got_error_from_errno("getcwd");
11024 error = got_repo_pack_fds_open(&pack_fds);
11028 error = got_worktree_open(&worktree, cwd);
11030 if (list_backups || delete_backups) {
11031 if (error->code != GOT_ERR_NOT_WORKTREE)
11034 if (error->code == GOT_ERR_NOT_WORKTREE)
11035 error = wrap_not_worktree_error(error,
11041 error = get_gitconfig_path(&gitconfig_path);
11044 error = got_repo_open(&repo,
11045 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11046 gitconfig_path, pack_fds);
11050 if (worktree != NULL && !list_backups && !delete_backups) {
11051 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11056 error = get_author(&committer, repo, worktree);
11057 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11060 error = apply_unveil(got_repo_get_path(repo), 0,
11061 worktree ? got_worktree_get_root_path(worktree) : NULL);
11065 if (list_backups || delete_backups) {
11066 error = process_backup_refs(
11067 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11068 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11069 goto done; /* nothing else to do */
11072 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11076 if (histedit_in_progress) {
11077 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11081 error = got_worktree_merge_in_progress(&merge_in_progress,
11085 if (merge_in_progress) {
11086 error = got_error(GOT_ERR_MERGE_BUSY);
11090 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11094 if (abort_rebase) {
11095 if (!rebase_in_progress) {
11096 error = got_error(GOT_ERR_NOT_REBASING);
11099 error = got_worktree_rebase_continue(&resume_commit_id,
11100 &new_base_branch, &tmp_branch, &branch, &fileindex,
11104 printf("Switching work tree to %s\n",
11105 got_ref_get_symref_target(new_base_branch));
11106 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11107 new_base_branch, abort_progress, &upa);
11110 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11111 print_merge_progress_stats(&upa);
11112 goto done; /* nothing else to do */
11115 if (continue_rebase) {
11116 if (!rebase_in_progress) {
11117 error = got_error(GOT_ERR_NOT_REBASING);
11120 error = got_worktree_rebase_continue(&resume_commit_id,
11121 &new_base_branch, &tmp_branch, &branch, &fileindex,
11126 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11127 committer, resume_commit_id, repo);
11131 yca_id = got_object_id_dup(resume_commit_id);
11132 if (yca_id == NULL) {
11133 error = got_error_from_errno("got_object_id_dup");
11137 error = got_ref_open(&branch, repo, argv[0], 0);
11140 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11141 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11142 "will not rebase a branch which lives outside "
11143 "the \"refs/heads/\" reference namespace");
11148 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11152 if (!continue_rebase) {
11153 struct got_object_id *base_commit_id;
11155 error = got_ref_open(&head_ref, repo,
11156 got_worktree_get_head_ref_name(worktree), 0);
11159 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11162 base_commit_id = got_worktree_get_base_commit_id(worktree);
11163 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11164 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11168 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11169 base_commit_id, branch_head_commit_id, 1, repo,
11170 check_cancelled, NULL);
11172 if (error->code == GOT_ERR_ANCESTRY) {
11173 error = got_error_msg(GOT_ERR_ANCESTRY,
11174 "specified branch shares no common "
11175 "ancestry with work tree's branch");
11180 error = check_same_branch(base_commit_id, branch, yca_id, repo);
11182 if (error->code != GOT_ERR_ANCESTRY)
11186 struct got_pathlist_head paths;
11187 printf("%s is already based on %s\n",
11188 got_ref_get_name(branch),
11189 got_worktree_get_head_ref_name(worktree));
11190 error = switch_head_ref(branch, branch_head_commit_id,
11194 error = got_worktree_set_base_commit_id(worktree, repo,
11195 branch_head_commit_id);
11198 TAILQ_INIT(&paths);
11199 error = got_pathlist_append(&paths, "", NULL);
11202 error = got_worktree_checkout_files(worktree,
11203 &paths, repo, update_progress, &upa,
11204 check_cancelled, NULL);
11205 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11208 if (upa.did_something) {
11210 error = got_object_id_str(&id_str,
11211 branch_head_commit_id);
11214 printf("Updated to %s: %s\n",
11215 got_worktree_get_head_ref_name(worktree),
11219 printf("Already up-to-date\n");
11220 print_update_progress_stats(&upa);
11225 commit_id = branch_head_commit_id;
11226 error = got_object_open_as_commit(&commit, repo, commit_id);
11230 parent_ids = got_object_commit_get_parent_ids(commit);
11231 pid = STAILQ_FIRST(parent_ids);
11233 error = collect_commits(&commits, commit_id, &pid->id,
11234 yca_id, got_worktree_get_path_prefix(worktree),
11235 GOT_ERR_REBASE_PATH, repo);
11240 got_object_commit_close(commit);
11243 if (!continue_rebase) {
11244 error = got_worktree_rebase_prepare(&new_base_branch,
11245 &tmp_branch, &fileindex, worktree, branch, repo);
11250 if (STAILQ_EMPTY(&commits)) {
11251 if (continue_rebase) {
11252 error = rebase_complete(worktree, fileindex,
11253 branch, tmp_branch, repo, create_backup);
11256 /* Fast-forward the reference of the branch. */
11257 struct got_object_id *new_head_commit_id;
11259 error = got_ref_resolve(&new_head_commit_id, repo,
11263 error = got_object_id_str(&id_str, new_head_commit_id);
11266 printf("Forwarding %s to commit %s\n",
11267 got_ref_get_name(branch), id_str);
11269 error = got_ref_change_ref(branch,
11270 new_head_commit_id);
11273 /* No backup needed since objects did not change. */
11279 STAILQ_FOREACH(qid, &commits, entry) {
11281 commit_id = &qid->id;
11282 parent_id = pid ? &pid->id : yca_id;
11285 memset(&upa, 0, sizeof(upa));
11286 error = got_worktree_rebase_merge_files(&merged_paths,
11287 worktree, fileindex, parent_id, commit_id, repo,
11288 update_progress, &upa, check_cancelled, NULL);
11292 print_merge_progress_stats(&upa);
11293 if (upa.conflicts > 0 || upa.missing > 0 ||
11294 upa.not_deleted > 0 || upa.unversioned > 0) {
11295 if (upa.conflicts > 0) {
11296 error = show_rebase_merge_conflict(&qid->id,
11301 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11305 error = rebase_commit(&merged_paths, worktree, fileindex,
11306 tmp_branch, committer, commit_id, repo);
11307 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11312 if (upa.conflicts > 0 || upa.missing > 0 ||
11313 upa.not_deleted > 0 || upa.unversioned > 0) {
11314 error = got_worktree_rebase_postpone(worktree, fileindex);
11317 if (upa.conflicts > 0 && upa.missing == 0 &&
11318 upa.not_deleted == 0 && upa.unversioned == 0) {
11319 error = got_error_msg(GOT_ERR_CONFLICTS,
11320 "conflicts must be resolved before rebasing "
11322 } else if (upa.conflicts > 0) {
11323 error = got_error_msg(GOT_ERR_CONFLICTS,
11324 "conflicts must be resolved before rebasing "
11325 "can continue; changes destined for some "
11326 "files were not yet merged and should be "
11327 "merged manually if required before the "
11328 "rebase operation is continued");
11330 error = got_error_msg(GOT_ERR_CONFLICTS,
11331 "changes destined for some files were not "
11332 "yet merged and should be merged manually "
11333 "if required before the rebase operation "
11337 error = rebase_complete(worktree, fileindex, branch,
11338 tmp_branch, repo, create_backup);
11342 free(gitconfig_path);
11343 got_object_id_queue_free(&commits);
11344 free(branch_head_commit_id);
11345 free(resume_commit_id);
11346 free(head_commit_id);
11349 got_object_commit_close(commit);
11351 got_ref_close(branch);
11352 if (new_base_branch)
11353 got_ref_close(new_base_branch);
11355 got_ref_close(tmp_branch);
11357 got_ref_close(head_ref);
11359 got_worktree_close(worktree);
11361 const struct got_error *close_err = got_repo_close(repo);
11366 const struct got_error *pack_err =
11367 got_repo_pack_fds_close(pack_fds);
11375 usage_histedit(void)
11377 fprintf(stderr, "usage: %s histedit [-acdeflmX] [-F histedit-script] "
11378 "[branch]\n", getprogname());
11382 #define GOT_HISTEDIT_PICK 'p'
11383 #define GOT_HISTEDIT_EDIT 'e'
11384 #define GOT_HISTEDIT_FOLD 'f'
11385 #define GOT_HISTEDIT_DROP 'd'
11386 #define GOT_HISTEDIT_MESG 'm'
11388 static const struct got_histedit_cmd {
11389 unsigned char code;
11392 } got_histedit_cmds[] = {
11393 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11394 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11395 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11397 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11398 { GOT_HISTEDIT_MESG, "mesg",
11399 "single-line log message for commit above (open editor if empty)" },
11402 struct got_histedit_list_entry {
11403 TAILQ_ENTRY(got_histedit_list_entry) entry;
11404 struct got_object_id *commit_id;
11405 const struct got_histedit_cmd *cmd;
11408 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11410 static const struct got_error *
11411 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11412 FILE *f, struct got_repository *repo)
11414 const struct got_error *err = NULL;
11415 char *logmsg = NULL, *id_str = NULL;
11416 struct got_commit_object *commit = NULL;
11419 err = got_object_open_as_commit(&commit, repo, commit_id);
11423 err = get_short_logmsg(&logmsg, 34, commit);
11427 err = got_object_id_str(&id_str, commit_id);
11431 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11433 err = got_ferror(f, GOT_ERR_IO);
11436 got_object_commit_close(commit);
11442 static const struct got_error *
11443 histedit_write_commit_list(struct got_object_id_queue *commits,
11444 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11445 int edit_only, struct got_repository *repo)
11447 const struct got_error *err = NULL;
11448 struct got_object_qid *qid;
11449 const char *histedit_cmd = NULL;
11451 if (STAILQ_EMPTY(commits))
11452 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11454 STAILQ_FOREACH(qid, commits, entry) {
11455 histedit_cmd = got_histedit_cmds[0].name;
11457 histedit_cmd = "drop";
11458 else if (edit_only)
11459 histedit_cmd = "edit";
11460 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11461 histedit_cmd = "fold";
11462 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11465 if (edit_logmsg_only) {
11466 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
11468 err = got_ferror(f, GOT_ERR_IO);
11477 static const struct got_error *
11478 write_cmd_list(FILE *f, const char *branch_name,
11479 struct got_object_id_queue *commits)
11481 const struct got_error *err = NULL;
11485 struct got_object_qid *qid;
11487 qid = STAILQ_FIRST(commits);
11488 err = got_object_id_str(&id_str, &qid->id);
11493 "# Editing the history of branch '%s' starting at\n"
11495 "# Commits will be processed in order from top to "
11496 "bottom of this file.\n", branch_name, id_str);
11498 err = got_ferror(f, GOT_ERR_IO);
11502 n = fprintf(f, "# Available histedit commands:\n");
11504 err = got_ferror(f, GOT_ERR_IO);
11508 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11509 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11510 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11513 err = got_ferror(f, GOT_ERR_IO);
11522 static const struct got_error *
11523 histedit_syntax_error(int lineno)
11525 static char msg[42];
11528 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11530 if (ret < 0 || (size_t)ret >= sizeof(msg))
11531 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11533 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11536 static const struct got_error *
11537 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11538 char *logmsg, struct got_repository *repo)
11540 const struct got_error *err;
11541 struct got_commit_object *folded_commit = NULL;
11542 char *id_str, *folded_logmsg = NULL;
11544 err = got_object_id_str(&id_str, hle->commit_id);
11548 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11552 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11555 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11556 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11557 folded_logmsg) == -1) {
11558 err = got_error_from_errno("asprintf");
11562 got_object_commit_close(folded_commit);
11564 free(folded_logmsg);
11568 static struct got_histedit_list_entry *
11569 get_folded_commits(struct got_histedit_list_entry *hle)
11571 struct got_histedit_list_entry *prev, *folded = NULL;
11573 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11574 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11575 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11576 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11578 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11584 static const struct got_error *
11585 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11586 struct got_repository *repo)
11588 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11589 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
11590 const struct got_error *err = NULL;
11591 struct got_commit_object *commit = NULL;
11594 struct got_histedit_list_entry *folded = NULL;
11596 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11600 folded = get_folded_commits(hle);
11602 while (folded != hle) {
11603 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11604 folded = TAILQ_NEXT(folded, entry);
11607 err = append_folded_commit_msg(&new_msg, folded,
11613 folded = TAILQ_NEXT(folded, entry);
11617 err = got_object_id_str(&id_str, hle->commit_id);
11620 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
11623 logmsg_len = asprintf(&new_msg,
11624 "%s\n# original log message of commit %s: %s",
11625 logmsg ? logmsg : "", id_str, orig_logmsg);
11626 if (logmsg_len == -1) {
11627 err = got_error_from_errno("asprintf");
11633 err = got_object_id_str(&id_str, hle->commit_id);
11637 err = got_opentemp_named_fd(&logmsg_path, &fd,
11638 GOT_TMPDIR_STR "/got-logmsg", "");
11642 write(fd, logmsg, logmsg_len);
11645 err = get_editor(&editor);
11649 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
11652 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
11655 hle->logmsg = strdup(new_msg);
11656 if (hle->logmsg == NULL)
11657 err = got_error_from_errno("strdup");
11660 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
11661 err = got_error_from_errno2("unlink", logmsg_path);
11667 got_object_commit_close(commit);
11671 static const struct got_error *
11672 histedit_parse_list(struct got_histedit_list *histedit_cmds,
11673 FILE *f, struct got_repository *repo)
11675 const struct got_error *err = NULL;
11676 char *line = NULL, *p, *end;
11679 int lineno = 0, lastcmd = -1;
11680 const struct got_histedit_cmd *cmd;
11681 struct got_object_id *commit_id = NULL;
11682 struct got_histedit_list_entry *hle = NULL;
11685 len = getline(&line, &size, f);
11687 const struct got_error *getline_err;
11690 getline_err = got_error_from_errno("getline");
11691 err = got_ferror(f, getline_err->code);
11696 while (isspace((unsigned char)p[0]))
11698 if (p[0] == '#' || p[0] == '\0') {
11704 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11705 cmd = &got_histedit_cmds[i];
11706 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
11707 isspace((unsigned char)p[strlen(cmd->name)])) {
11708 p += strlen(cmd->name);
11711 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
11716 if (i == nitems(got_histedit_cmds)) {
11717 err = histedit_syntax_error(lineno);
11720 while (isspace((unsigned char)p[0]))
11722 if (cmd->code == GOT_HISTEDIT_MESG) {
11723 if (lastcmd != GOT_HISTEDIT_PICK &&
11724 lastcmd != GOT_HISTEDIT_EDIT) {
11725 err = got_error(GOT_ERR_HISTEDIT_CMD);
11728 if (p[0] == '\0') {
11729 err = histedit_edit_logmsg(hle, repo);
11733 hle->logmsg = strdup(p);
11734 if (hle->logmsg == NULL) {
11735 err = got_error_from_errno("strdup");
11741 lastcmd = cmd->code;
11745 while (end[0] && !isspace((unsigned char)end[0]))
11749 err = got_object_resolve_id_str(&commit_id, repo, p);
11751 /* override error code */
11752 err = histedit_syntax_error(lineno);
11756 hle = malloc(sizeof(*hle));
11758 err = got_error_from_errno("malloc");
11762 hle->commit_id = commit_id;
11763 hle->logmsg = NULL;
11767 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
11768 lastcmd = cmd->code;
11776 static const struct got_error *
11777 histedit_check_script(struct got_histedit_list *histedit_cmds,
11778 struct got_object_id_queue *commits, struct got_repository *repo)
11780 const struct got_error *err = NULL;
11781 struct got_object_qid *qid;
11782 struct got_histedit_list_entry *hle;
11783 static char msg[92];
11786 if (TAILQ_EMPTY(histedit_cmds))
11787 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
11788 "histedit script contains no commands");
11789 if (STAILQ_EMPTY(commits))
11790 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11792 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11793 struct got_histedit_list_entry *hle2;
11794 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
11797 if (got_object_id_cmp(hle->commit_id,
11798 hle2->commit_id) != 0)
11800 err = got_object_id_str(&id_str, hle->commit_id);
11803 snprintf(msg, sizeof(msg), "commit %s is listed "
11804 "more than once in histedit script", id_str);
11806 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11810 STAILQ_FOREACH(qid, commits, entry) {
11811 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11812 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
11816 err = got_object_id_str(&id_str, &qid->id);
11819 snprintf(msg, sizeof(msg),
11820 "commit %s missing from histedit script", id_str);
11822 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
11826 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
11827 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
11828 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
11829 "last commit in histedit script cannot be folded");
11834 static const struct got_error *
11835 histedit_run_editor(struct got_histedit_list *histedit_cmds,
11836 const char *path, struct got_object_id_queue *commits,
11837 struct got_repository *repo)
11839 const struct got_error *err = NULL;
11843 err = get_editor(&editor);
11847 if (spawn_editor(editor, path) == -1) {
11848 err = got_error_from_errno("failed spawning editor");
11852 f = fopen(path, "re");
11854 err = got_error_from_errno("fopen");
11857 err = histedit_parse_list(histedit_cmds, f, repo);
11861 err = histedit_check_script(histedit_cmds, commits, repo);
11863 if (f && fclose(f) == EOF && err == NULL)
11864 err = got_error_from_errno("fclose");
11869 static const struct got_error *
11870 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
11871 struct got_object_id_queue *, const char *, const char *,
11872 struct got_repository *);
11874 static const struct got_error *
11875 histedit_edit_script(struct got_histedit_list *histedit_cmds,
11876 struct got_object_id_queue *commits, const char *branch_name,
11877 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
11878 struct got_repository *repo)
11880 const struct got_error *err;
11884 err = got_opentemp_named(&path, &f, "got-histedit", "");
11888 err = write_cmd_list(f, branch_name, commits);
11892 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
11893 fold_only, drop_only, edit_only, repo);
11897 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
11899 err = histedit_parse_list(histedit_cmds, f, repo);
11901 if (fclose(f) == EOF) {
11902 err = got_error_from_errno("fclose");
11906 err = histedit_run_editor(histedit_cmds, path, commits, repo);
11908 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
11909 err->code != GOT_ERR_HISTEDIT_CMD)
11911 err = histedit_edit_list_retry(histedit_cmds, err,
11912 commits, path, branch_name, repo);
11916 if (f && fclose(f) == EOF && err == NULL)
11917 err = got_error_from_errno("fclose");
11918 if (path && unlink(path) != 0 && err == NULL)
11919 err = got_error_from_errno2("unlink", path);
11924 static const struct got_error *
11925 histedit_save_list(struct got_histedit_list *histedit_cmds,
11926 struct got_worktree *worktree, struct got_repository *repo)
11928 const struct got_error *err = NULL;
11931 struct got_histedit_list_entry *hle;
11932 struct got_commit_object *commit = NULL;
11934 err = got_worktree_get_histedit_script_path(&path, worktree);
11938 f = fopen(path, "we");
11940 err = got_error_from_errno2("fopen", path);
11943 TAILQ_FOREACH(hle, histedit_cmds, entry) {
11944 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
11950 int n = fprintf(f, "%c %s\n",
11951 GOT_HISTEDIT_MESG, hle->logmsg);
11953 err = got_ferror(f, GOT_ERR_IO);
11959 if (f && fclose(f) == EOF && err == NULL)
11960 err = got_error_from_errno("fclose");
11963 got_object_commit_close(commit);
11968 histedit_free_list(struct got_histedit_list *histedit_cmds)
11970 struct got_histedit_list_entry *hle;
11972 while ((hle = TAILQ_FIRST(histedit_cmds))) {
11973 TAILQ_REMOVE(histedit_cmds, hle, entry);
11978 static const struct got_error *
11979 histedit_load_list(struct got_histedit_list *histedit_cmds,
11980 const char *path, struct got_repository *repo)
11982 const struct got_error *err = NULL;
11985 f = fopen(path, "re");
11987 err = got_error_from_errno2("fopen", path);
11991 err = histedit_parse_list(histedit_cmds, f, repo);
11993 if (f && fclose(f) == EOF && err == NULL)
11994 err = got_error_from_errno("fclose");
11998 static const struct got_error *
11999 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12000 const struct got_error *edit_err, struct got_object_id_queue *commits,
12001 const char *path, const char *branch_name, struct got_repository *repo)
12003 const struct got_error *err = NULL, *prev_err = edit_err;
12006 while (resp != 'c' && resp != 'r' && resp != 'a') {
12007 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12008 "or (a)bort: ", getprogname(), prev_err->msg);
12013 histedit_free_list(histedit_cmds);
12014 err = histedit_run_editor(histedit_cmds, path, commits,
12017 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12018 err->code != GOT_ERR_HISTEDIT_CMD)
12025 } else if (resp == 'r') {
12026 histedit_free_list(histedit_cmds);
12027 err = histedit_edit_script(histedit_cmds,
12028 commits, branch_name, 0, 0, 0, 0, repo);
12030 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12031 err->code != GOT_ERR_HISTEDIT_CMD)
12038 } else if (resp == 'a') {
12039 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12042 printf("invalid response '%c'\n", resp);
12048 static const struct got_error *
12049 histedit_complete(struct got_worktree *worktree,
12050 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12051 struct got_reference *branch, struct got_repository *repo)
12053 printf("Switching work tree to %s\n",
12054 got_ref_get_symref_target(branch));
12055 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12059 static const struct got_error *
12060 show_histedit_progress(struct got_commit_object *commit,
12061 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12063 const struct got_error *err;
12064 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12066 err = got_object_id_str(&old_id_str, hle->commit_id);
12071 err = got_object_id_str(&new_id_str, new_id);
12076 old_id_str[12] = '\0';
12078 new_id_str[12] = '\0';
12081 logmsg = strdup(hle->logmsg);
12082 if (logmsg == NULL) {
12083 err = got_error_from_errno("strdup");
12086 trim_logmsg(logmsg, 42);
12088 err = get_short_logmsg(&logmsg, 42, commit);
12093 switch (hle->cmd->code) {
12094 case GOT_HISTEDIT_PICK:
12095 case GOT_HISTEDIT_EDIT:
12096 printf("%s -> %s: %s\n", old_id_str,
12097 new_id_str ? new_id_str : "no-op change", logmsg);
12099 case GOT_HISTEDIT_DROP:
12100 case GOT_HISTEDIT_FOLD:
12101 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12113 static const struct got_error *
12114 histedit_commit(struct got_pathlist_head *merged_paths,
12115 struct got_worktree *worktree, struct got_fileindex *fileindex,
12116 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12117 const char *committer, struct got_repository *repo)
12119 const struct got_error *err;
12120 struct got_commit_object *commit;
12121 struct got_object_id *new_commit_id;
12123 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12124 && hle->logmsg == NULL) {
12125 err = histedit_edit_logmsg(hle, repo);
12130 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12134 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12135 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12136 hle->logmsg, repo);
12138 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12140 err = show_histedit_progress(commit, hle, NULL);
12142 err = show_histedit_progress(commit, hle, new_commit_id);
12143 free(new_commit_id);
12146 got_object_commit_close(commit);
12150 static const struct got_error *
12151 histedit_skip_commit(struct got_histedit_list_entry *hle,
12152 struct got_worktree *worktree, struct got_repository *repo)
12154 const struct got_error *error;
12155 struct got_commit_object *commit;
12157 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12162 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12166 error = show_histedit_progress(commit, hle, NULL);
12167 got_object_commit_close(commit);
12171 static const struct got_error *
12172 check_local_changes(void *arg, unsigned char status,
12173 unsigned char staged_status, const char *path,
12174 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12175 struct got_object_id *commit_id, int dirfd, const char *de_name)
12177 int *have_local_changes = arg;
12180 case GOT_STATUS_ADD:
12181 case GOT_STATUS_DELETE:
12182 case GOT_STATUS_MODIFY:
12183 case GOT_STATUS_CONFLICT:
12184 *have_local_changes = 1;
12185 return got_error(GOT_ERR_CANCELLED);
12190 switch (staged_status) {
12191 case GOT_STATUS_ADD:
12192 case GOT_STATUS_DELETE:
12193 case GOT_STATUS_MODIFY:
12194 *have_local_changes = 1;
12195 return got_error(GOT_ERR_CANCELLED);
12203 static const struct got_error *
12204 cmd_histedit(int argc, char *argv[])
12206 const struct got_error *error = NULL;
12207 struct got_worktree *worktree = NULL;
12208 struct got_fileindex *fileindex = NULL;
12209 struct got_repository *repo = NULL;
12210 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12211 struct got_reference *branch = NULL;
12212 struct got_reference *tmp_branch = NULL;
12213 struct got_object_id *resume_commit_id = NULL;
12214 struct got_object_id *base_commit_id = NULL;
12215 struct got_object_id *head_commit_id = NULL;
12216 struct got_commit_object *commit = NULL;
12217 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12218 struct got_update_progress_arg upa;
12219 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12220 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12221 int list_backups = 0, delete_backups = 0;
12222 const char *edit_script_path = NULL;
12223 struct got_object_id_queue commits;
12224 struct got_pathlist_head merged_paths;
12225 const struct got_object_id_queue *parent_ids;
12226 struct got_object_qid *pid;
12227 struct got_histedit_list histedit_cmds;
12228 struct got_histedit_list_entry *hle;
12229 int *pack_fds = NULL;
12231 STAILQ_INIT(&commits);
12232 TAILQ_INIT(&histedit_cmds);
12233 TAILQ_INIT(&merged_paths);
12234 memset(&upa, 0, sizeof(upa));
12236 while ((ch = getopt(argc, argv, "acdeF:flmX")) != -1) {
12251 edit_script_path = optarg;
12260 edit_logmsg_only = 1;
12263 delete_backups = 1;
12275 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12276 "unveil", NULL) == -1)
12279 if (abort_edit && continue_edit)
12280 option_conflict('a', 'c');
12281 if (edit_script_path && edit_logmsg_only)
12282 option_conflict('F', 'm');
12283 if (abort_edit && edit_logmsg_only)
12284 option_conflict('a', 'm');
12285 if (continue_edit && edit_logmsg_only)
12286 option_conflict('c', 'm');
12287 if (abort_edit && fold_only)
12288 option_conflict('a', 'f');
12289 if (continue_edit && fold_only)
12290 option_conflict('c', 'f');
12291 if (fold_only && edit_logmsg_only)
12292 option_conflict('f', 'm');
12293 if (edit_script_path && fold_only)
12294 option_conflict('F', 'f');
12295 if (abort_edit && edit_only)
12296 option_conflict('a', 'e');
12297 if (continue_edit && edit_only)
12298 option_conflict('c', 'e');
12299 if (edit_only && edit_logmsg_only)
12300 option_conflict('e', 'm');
12301 if (edit_script_path && edit_only)
12302 option_conflict('F', 'e');
12303 if (fold_only && edit_only)
12304 option_conflict('f', 'e');
12305 if (drop_only && abort_edit)
12306 option_conflict('d', 'a');
12307 if (drop_only && continue_edit)
12308 option_conflict('d', 'c');
12309 if (drop_only && edit_logmsg_only)
12310 option_conflict('d', 'm');
12311 if (drop_only && edit_only)
12312 option_conflict('d', 'e');
12313 if (drop_only && edit_script_path)
12314 option_conflict('d', 'F');
12315 if (drop_only && fold_only)
12316 option_conflict('d', 'f');
12317 if (list_backups) {
12319 option_conflict('l', 'a');
12321 option_conflict('l', 'c');
12322 if (edit_script_path)
12323 option_conflict('l', 'F');
12324 if (edit_logmsg_only)
12325 option_conflict('l', 'm');
12327 option_conflict('l', 'd');
12329 option_conflict('l', 'f');
12331 option_conflict('l', 'e');
12332 if (delete_backups)
12333 option_conflict('l', 'X');
12334 if (argc != 0 && argc != 1)
12336 } else if (delete_backups) {
12338 option_conflict('X', 'a');
12340 option_conflict('X', 'c');
12342 option_conflict('X', 'd');
12343 if (edit_script_path)
12344 option_conflict('X', 'F');
12345 if (edit_logmsg_only)
12346 option_conflict('X', 'm');
12348 option_conflict('X', 'f');
12350 option_conflict('X', 'e');
12352 option_conflict('X', 'l');
12353 if (argc != 0 && argc != 1)
12355 } else if (argc != 0)
12359 * This command cannot apply unveil(2) in all cases because the
12360 * user may choose to run an editor to edit the histedit script
12361 * and to edit individual commit log messages.
12362 * unveil(2) traverses exec(2); if an editor is used we have to
12363 * apply unveil after edit script and log messages have been written.
12364 * XXX TODO: Make use of unveil(2) where possible.
12367 cwd = getcwd(NULL, 0);
12369 error = got_error_from_errno("getcwd");
12373 error = got_repo_pack_fds_open(&pack_fds);
12377 error = got_worktree_open(&worktree, cwd);
12379 if (list_backups || delete_backups) {
12380 if (error->code != GOT_ERR_NOT_WORKTREE)
12383 if (error->code == GOT_ERR_NOT_WORKTREE)
12384 error = wrap_not_worktree_error(error,
12390 if (list_backups || delete_backups) {
12391 error = got_repo_open(&repo,
12392 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12396 error = apply_unveil(got_repo_get_path(repo), 0,
12397 worktree ? got_worktree_get_root_path(worktree) : NULL);
12400 error = process_backup_refs(
12401 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12402 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12403 goto done; /* nothing else to do */
12406 error = get_gitconfig_path(&gitconfig_path);
12409 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12410 gitconfig_path, pack_fds);
12414 if (worktree != NULL && !list_backups && !delete_backups) {
12415 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12420 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12423 if (rebase_in_progress) {
12424 error = got_error(GOT_ERR_REBASING);
12428 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12432 if (merge_in_progress) {
12433 error = got_error(GOT_ERR_MERGE_BUSY);
12437 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12441 if (edit_in_progress && edit_logmsg_only) {
12442 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12443 "histedit operation is in progress in this "
12444 "work tree and must be continued or aborted "
12445 "before the -m option can be used");
12448 if (edit_in_progress && drop_only) {
12449 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12450 "histedit operation is in progress in this "
12451 "work tree and must be continued or aborted "
12452 "before the -d option can be used");
12455 if (edit_in_progress && fold_only) {
12456 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12457 "histedit operation is in progress in this "
12458 "work tree and must be continued or aborted "
12459 "before the -f option can be used");
12462 if (edit_in_progress && edit_only) {
12463 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12464 "histedit operation is in progress in this "
12465 "work tree and must be continued or aborted "
12466 "before the -e option can be used");
12470 if (edit_in_progress && abort_edit) {
12471 error = got_worktree_histedit_continue(&resume_commit_id,
12472 &tmp_branch, &branch, &base_commit_id, &fileindex,
12476 printf("Switching work tree to %s\n",
12477 got_ref_get_symref_target(branch));
12478 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12479 branch, base_commit_id, abort_progress, &upa);
12482 printf("Histedit of %s aborted\n",
12483 got_ref_get_symref_target(branch));
12484 print_merge_progress_stats(&upa);
12485 goto done; /* nothing else to do */
12486 } else if (abort_edit) {
12487 error = got_error(GOT_ERR_NOT_HISTEDIT);
12491 error = get_author(&committer, repo, worktree);
12495 if (continue_edit) {
12498 if (!edit_in_progress) {
12499 error = got_error(GOT_ERR_NOT_HISTEDIT);
12503 error = got_worktree_get_histedit_script_path(&path, worktree);
12507 error = histedit_load_list(&histedit_cmds, path, repo);
12512 error = got_worktree_histedit_continue(&resume_commit_id,
12513 &tmp_branch, &branch, &base_commit_id, &fileindex,
12518 error = got_ref_resolve(&head_commit_id, repo, branch);
12522 error = got_object_open_as_commit(&commit, repo,
12526 parent_ids = got_object_commit_get_parent_ids(commit);
12527 pid = STAILQ_FIRST(parent_ids);
12529 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12532 error = collect_commits(&commits, head_commit_id, &pid->id,
12533 base_commit_id, got_worktree_get_path_prefix(worktree),
12534 GOT_ERR_HISTEDIT_PATH, repo);
12535 got_object_commit_close(commit);
12540 if (edit_in_progress) {
12541 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12545 error = got_ref_open(&branch, repo,
12546 got_worktree_get_head_ref_name(worktree), 0);
12550 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12551 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12552 "will not edit commit history of a branch outside "
12553 "the \"refs/heads/\" reference namespace");
12557 error = got_ref_resolve(&head_commit_id, repo, branch);
12558 got_ref_close(branch);
12563 error = got_object_open_as_commit(&commit, repo,
12567 parent_ids = got_object_commit_get_parent_ids(commit);
12568 pid = STAILQ_FIRST(parent_ids);
12570 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12573 error = collect_commits(&commits, head_commit_id, &pid->id,
12574 got_worktree_get_base_commit_id(worktree),
12575 got_worktree_get_path_prefix(worktree),
12576 GOT_ERR_HISTEDIT_PATH, repo);
12577 got_object_commit_close(commit);
12582 if (STAILQ_EMPTY(&commits)) {
12583 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12587 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12588 &base_commit_id, &fileindex, worktree, repo);
12592 if (edit_script_path) {
12593 error = histedit_load_list(&histedit_cmds,
12594 edit_script_path, repo);
12596 got_worktree_histedit_abort(worktree, fileindex,
12597 repo, branch, base_commit_id,
12598 abort_progress, &upa);
12599 print_merge_progress_stats(&upa);
12603 const char *branch_name;
12604 branch_name = got_ref_get_symref_target(branch);
12605 if (strncmp(branch_name, "refs/heads/", 11) == 0)
12607 error = histedit_edit_script(&histedit_cmds, &commits,
12608 branch_name, edit_logmsg_only, fold_only,
12609 drop_only, edit_only, repo);
12611 got_worktree_histedit_abort(worktree, fileindex,
12612 repo, branch, base_commit_id,
12613 abort_progress, &upa);
12614 print_merge_progress_stats(&upa);
12620 error = histedit_save_list(&histedit_cmds, worktree,
12623 got_worktree_histedit_abort(worktree, fileindex,
12624 repo, branch, base_commit_id,
12625 abort_progress, &upa);
12626 print_merge_progress_stats(&upa);
12632 error = histedit_check_script(&histedit_cmds, &commits, repo);
12636 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
12637 if (resume_commit_id) {
12638 if (got_object_id_cmp(hle->commit_id,
12639 resume_commit_id) != 0)
12642 resume_commit_id = NULL;
12643 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
12644 hle->cmd->code == GOT_HISTEDIT_FOLD) {
12645 error = histedit_skip_commit(hle, worktree,
12650 struct got_pathlist_head paths;
12651 int have_changes = 0;
12653 TAILQ_INIT(&paths);
12654 error = got_pathlist_append(&paths, "", NULL);
12657 error = got_worktree_status(worktree, &paths,
12658 repo, 0, check_local_changes, &have_changes,
12659 check_cancelled, NULL);
12660 got_pathlist_free(&paths,
12661 GOT_PATHLIST_FREE_NONE);
12663 if (error->code != GOT_ERR_CANCELLED)
12665 if (sigint_received || sigpipe_received)
12668 if (have_changes) {
12669 error = histedit_commit(NULL, worktree,
12670 fileindex, tmp_branch, hle,
12675 error = got_object_open_as_commit(
12676 &commit, repo, hle->commit_id);
12679 error = show_histedit_progress(commit,
12681 got_object_commit_close(commit);
12690 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
12691 error = histedit_skip_commit(hle, worktree, repo);
12697 error = got_object_open_as_commit(&commit, repo,
12701 parent_ids = got_object_commit_get_parent_ids(commit);
12702 pid = STAILQ_FIRST(parent_ids);
12704 error = got_worktree_histedit_merge_files(&merged_paths,
12705 worktree, fileindex, &pid->id, hle->commit_id, repo,
12706 update_progress, &upa, check_cancelled, NULL);
12709 got_object_commit_close(commit);
12712 print_merge_progress_stats(&upa);
12713 if (upa.conflicts > 0 || upa.missing > 0 ||
12714 upa.not_deleted > 0 || upa.unversioned > 0) {
12715 if (upa.conflicts > 0) {
12716 error = show_rebase_merge_conflict(
12717 hle->commit_id, repo);
12721 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12725 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
12727 error = got_object_id_str(&id_str, hle->commit_id);
12730 printf("Stopping histedit for amending commit %s\n",
12733 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12734 error = got_worktree_histedit_postpone(worktree,
12739 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
12740 error = histedit_skip_commit(hle, worktree, repo);
12746 error = histedit_commit(&merged_paths, worktree, fileindex,
12747 tmp_branch, hle, committer, repo);
12748 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
12753 if (upa.conflicts > 0 || upa.missing > 0 ||
12754 upa.not_deleted > 0 || upa.unversioned > 0) {
12755 error = got_worktree_histedit_postpone(worktree, fileindex);
12758 if (upa.conflicts > 0 && upa.missing == 0 &&
12759 upa.not_deleted == 0 && upa.unversioned == 0) {
12760 error = got_error_msg(GOT_ERR_CONFLICTS,
12761 "conflicts must be resolved before histedit "
12763 } else if (upa.conflicts > 0) {
12764 error = got_error_msg(GOT_ERR_CONFLICTS,
12765 "conflicts must be resolved before histedit "
12766 "can continue; changes destined for some "
12767 "files were not yet merged and should be "
12768 "merged manually if required before the "
12769 "histedit operation is continued");
12771 error = got_error_msg(GOT_ERR_CONFLICTS,
12772 "changes destined for some files were not "
12773 "yet merged and should be merged manually "
12774 "if required before the histedit operation "
12778 error = histedit_complete(worktree, fileindex, tmp_branch,
12783 free(gitconfig_path);
12784 got_object_id_queue_free(&commits);
12785 histedit_free_list(&histedit_cmds);
12786 free(head_commit_id);
12787 free(base_commit_id);
12788 free(resume_commit_id);
12790 got_object_commit_close(commit);
12792 got_ref_close(branch);
12794 got_ref_close(tmp_branch);
12796 got_worktree_close(worktree);
12798 const struct got_error *close_err = got_repo_close(repo);
12803 const struct got_error *pack_err =
12804 got_repo_pack_fds_close(pack_fds);
12812 usage_integrate(void)
12814 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
12818 static const struct got_error *
12819 cmd_integrate(int argc, char *argv[])
12821 const struct got_error *error = NULL;
12822 struct got_repository *repo = NULL;
12823 struct got_worktree *worktree = NULL;
12824 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
12825 const char *branch_arg = NULL;
12826 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
12827 struct got_fileindex *fileindex = NULL;
12828 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
12830 struct got_update_progress_arg upa;
12831 int *pack_fds = NULL;
12833 while ((ch = getopt(argc, argv, "")) != -1) {
12846 branch_arg = argv[0];
12848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12849 "unveil", NULL) == -1)
12852 cwd = getcwd(NULL, 0);
12854 error = got_error_from_errno("getcwd");
12858 error = got_repo_pack_fds_open(&pack_fds);
12862 error = got_worktree_open(&worktree, cwd);
12864 if (error->code == GOT_ERR_NOT_WORKTREE)
12865 error = wrap_not_worktree_error(error, "integrate",
12870 error = check_rebase_or_histedit_in_progress(worktree);
12874 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
12879 error = apply_unveil(got_repo_get_path(repo), 0,
12880 got_worktree_get_root_path(worktree));
12884 error = check_merge_in_progress(worktree, repo);
12888 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
12889 error = got_error_from_errno("asprintf");
12893 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
12894 &base_branch_ref, worktree, refname, repo);
12898 refname = strdup(got_ref_get_name(branch_ref));
12899 if (refname == NULL) {
12900 error = got_error_from_errno("strdup");
12901 got_worktree_integrate_abort(worktree, fileindex, repo,
12902 branch_ref, base_branch_ref);
12905 base_refname = strdup(got_ref_get_name(base_branch_ref));
12906 if (base_refname == NULL) {
12907 error = got_error_from_errno("strdup");
12908 got_worktree_integrate_abort(worktree, fileindex, repo,
12909 branch_ref, base_branch_ref);
12912 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
12913 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
12914 got_worktree_integrate_abort(worktree, fileindex, repo,
12915 branch_ref, base_branch_ref);
12919 error = got_ref_resolve(&commit_id, repo, branch_ref);
12923 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
12927 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
12928 error = got_error_msg(GOT_ERR_SAME_BRANCH,
12929 "specified branch has already been integrated");
12930 got_worktree_integrate_abort(worktree, fileindex, repo,
12931 branch_ref, base_branch_ref);
12935 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
12937 if (error->code == GOT_ERR_ANCESTRY)
12938 error = got_error(GOT_ERR_REBASE_REQUIRED);
12939 got_worktree_integrate_abort(worktree, fileindex, repo,
12940 branch_ref, base_branch_ref);
12944 memset(&upa, 0, sizeof(upa));
12945 error = got_worktree_integrate_continue(worktree, fileindex, repo,
12946 branch_ref, base_branch_ref, update_progress, &upa,
12947 check_cancelled, NULL);
12951 printf("Integrated %s into %s\n", refname, base_refname);
12952 print_update_progress_stats(&upa);
12955 const struct got_error *close_err = got_repo_close(repo);
12960 got_worktree_close(worktree);
12962 const struct got_error *pack_err =
12963 got_repo_pack_fds_close(pack_fds);
12968 free(base_commit_id);
12971 free(base_refname);
12978 fprintf(stderr, "usage: %s merge [-acn] [branch]\n", getprogname());
12982 static const struct got_error *
12983 cmd_merge(int argc, char *argv[])
12985 const struct got_error *error = NULL;
12986 struct got_worktree *worktree = NULL;
12987 struct got_repository *repo = NULL;
12988 struct got_fileindex *fileindex = NULL;
12989 char *cwd = NULL, *id_str = NULL, *author = NULL;
12990 struct got_reference *branch = NULL, *wt_branch = NULL;
12991 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
12992 struct got_object_id *wt_branch_tip = NULL;
12993 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
12994 int interrupt_merge = 0;
12995 struct got_update_progress_arg upa;
12996 struct got_object_id *merge_commit_id = NULL;
12997 char *branch_name = NULL;
12998 int *pack_fds = NULL;
13000 memset(&upa, 0, sizeof(upa));
13002 while ((ch = getopt(argc, argv, "acn")) != -1) {
13008 continue_merge = 1;
13011 interrupt_merge = 1;
13023 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13024 "unveil", NULL) == -1)
13028 if (abort_merge && continue_merge)
13029 option_conflict('a', 'c');
13030 if (abort_merge || continue_merge) {
13033 } else if (argc != 1)
13036 cwd = getcwd(NULL, 0);
13038 error = got_error_from_errno("getcwd");
13042 error = got_repo_pack_fds_open(&pack_fds);
13046 error = got_worktree_open(&worktree, cwd);
13048 if (error->code == GOT_ERR_NOT_WORKTREE)
13049 error = wrap_not_worktree_error(error,
13054 error = got_repo_open(&repo,
13055 worktree ? got_worktree_get_repo_path(worktree) : cwd, NULL,
13060 if (worktree != NULL) {
13061 error = worktree_has_logmsg_ref("merge", worktree, repo);
13066 error = apply_unveil(got_repo_get_path(repo), 0,
13067 worktree ? got_worktree_get_root_path(worktree) : NULL);
13071 error = check_rebase_or_histedit_in_progress(worktree);
13075 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13081 if (!merge_in_progress) {
13082 error = got_error(GOT_ERR_NOT_MERGING);
13085 error = got_worktree_merge_continue(&branch_name,
13086 &branch_tip, &fileindex, worktree, repo);
13089 error = got_worktree_merge_abort(worktree, fileindex, repo,
13090 abort_progress, &upa);
13093 printf("Merge of %s aborted\n", branch_name);
13094 goto done; /* nothing else to do */
13097 error = get_author(&author, repo, worktree);
13101 if (continue_merge) {
13102 if (!merge_in_progress) {
13103 error = got_error(GOT_ERR_NOT_MERGING);
13106 error = got_worktree_merge_continue(&branch_name,
13107 &branch_tip, &fileindex, worktree, repo);
13111 error = got_ref_open(&branch, repo, argv[0], 0);
13114 branch_name = strdup(got_ref_get_name(branch));
13115 if (branch_name == NULL) {
13116 error = got_error_from_errno("strdup");
13119 error = got_ref_resolve(&branch_tip, repo, branch);
13124 error = got_ref_open(&wt_branch, repo,
13125 got_worktree_get_head_ref_name(worktree), 0);
13128 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13131 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13132 wt_branch_tip, branch_tip, 0, repo,
13133 check_cancelled, NULL);
13134 if (error && error->code != GOT_ERR_ANCESTRY)
13137 if (!continue_merge) {
13138 error = check_path_prefix(wt_branch_tip, branch_tip,
13139 got_worktree_get_path_prefix(worktree),
13140 GOT_ERR_MERGE_PATH, repo);
13144 error = check_same_branch(wt_branch_tip, branch,
13147 if (error->code != GOT_ERR_ANCESTRY)
13151 static char msg[512];
13152 snprintf(msg, sizeof(msg),
13153 "cannot create a merge commit because "
13154 "%s is based on %s; %s can be integrated "
13155 "with 'got integrate' instead", branch_name,
13156 got_worktree_get_head_ref_name(worktree),
13158 error = got_error_msg(GOT_ERR_SAME_BRANCH, msg);
13162 error = got_worktree_merge_prepare(&fileindex, worktree,
13167 error = got_worktree_merge_branch(worktree, fileindex,
13168 yca_id, branch_tip, repo, update_progress, &upa,
13169 check_cancelled, NULL);
13172 print_merge_progress_stats(&upa);
13173 if (!upa.did_something) {
13174 error = got_worktree_merge_abort(worktree, fileindex,
13175 repo, abort_progress, &upa);
13178 printf("Already up-to-date\n");
13183 if (interrupt_merge) {
13184 error = got_worktree_merge_postpone(worktree, fileindex);
13187 printf("Merge of %s interrupted on request\n", branch_name);
13188 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13189 upa.not_deleted > 0 || upa.unversioned > 0) {
13190 error = got_worktree_merge_postpone(worktree, fileindex);
13193 if (upa.conflicts > 0 && upa.missing == 0 &&
13194 upa.not_deleted == 0 && upa.unversioned == 0) {
13195 error = got_error_msg(GOT_ERR_CONFLICTS,
13196 "conflicts must be resolved before merging "
13198 } else if (upa.conflicts > 0) {
13199 error = got_error_msg(GOT_ERR_CONFLICTS,
13200 "conflicts must be resolved before merging "
13201 "can continue; changes destined for some "
13202 "files were not yet merged and "
13203 "should be merged manually if required before the "
13204 "merge operation is continued");
13206 error = got_error_msg(GOT_ERR_CONFLICTS,
13207 "changes destined for some "
13208 "files were not yet merged and should be "
13209 "merged manually if required before the "
13210 "merge operation is continued");
13214 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13215 fileindex, author, NULL, 1, branch_tip, branch_name,
13216 repo, continue_merge ? print_status : NULL, NULL);
13219 error = got_worktree_merge_complete(worktree, fileindex, repo);
13222 error = got_object_id_str(&id_str, merge_commit_id);
13225 printf("Merged %s into %s: %s\n", branch_name,
13226 got_worktree_get_head_ref_name(worktree),
13232 free(merge_commit_id);
13238 got_ref_close(branch);
13240 got_ref_close(wt_branch);
13242 got_worktree_close(worktree);
13244 const struct got_error *close_err = got_repo_close(repo);
13249 const struct got_error *pack_err =
13250 got_repo_pack_fds_close(pack_fds);
13260 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13261 "[path ...]\n", getprogname());
13265 static const struct got_error *
13266 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13267 const char *path, struct got_object_id *blob_id,
13268 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13269 int dirfd, const char *de_name)
13271 const struct got_error *err = NULL;
13272 char *id_str = NULL;
13274 if (staged_status != GOT_STATUS_ADD &&
13275 staged_status != GOT_STATUS_MODIFY &&
13276 staged_status != GOT_STATUS_DELETE)
13279 if (staged_status == GOT_STATUS_ADD ||
13280 staged_status == GOT_STATUS_MODIFY)
13281 err = got_object_id_str(&id_str, staged_blob_id);
13283 err = got_object_id_str(&id_str, blob_id);
13287 printf("%s %c %s\n", id_str, staged_status, path);
13292 static const struct got_error *
13293 cmd_stage(int argc, char *argv[])
13295 const struct got_error *error = NULL;
13296 struct got_repository *repo = NULL;
13297 struct got_worktree *worktree = NULL;
13299 struct got_pathlist_head paths;
13300 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13301 FILE *patch_script_file = NULL;
13302 const char *patch_script_path = NULL;
13303 struct choose_patch_arg cpa;
13304 int *pack_fds = NULL;
13306 TAILQ_INIT(&paths);
13308 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13311 patch_script_path = optarg;
13320 allow_bad_symlinks = 1;
13332 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13333 "unveil", NULL) == -1)
13336 if (list_stage && (pflag || patch_script_path))
13337 errx(1, "-l option cannot be used with other options");
13338 if (patch_script_path && !pflag)
13339 errx(1, "-F option can only be used together with -p option");
13341 cwd = getcwd(NULL, 0);
13343 error = got_error_from_errno("getcwd");
13347 error = got_repo_pack_fds_open(&pack_fds);
13351 error = got_worktree_open(&worktree, cwd);
13353 if (error->code == GOT_ERR_NOT_WORKTREE)
13354 error = wrap_not_worktree_error(error, "stage", cwd);
13358 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13363 if (patch_script_path) {
13364 patch_script_file = fopen(patch_script_path, "re");
13365 if (patch_script_file == NULL) {
13366 error = got_error_from_errno2("fopen",
13367 patch_script_path);
13371 error = apply_unveil(got_repo_get_path(repo), 0,
13372 got_worktree_get_root_path(worktree));
13376 error = check_merge_in_progress(worktree, repo);
13380 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13385 error = got_worktree_status(worktree, &paths, repo, 0,
13386 print_stage, NULL, check_cancelled, NULL);
13388 cpa.patch_script_file = patch_script_file;
13389 cpa.action = "stage";
13390 error = got_worktree_stage(worktree, &paths,
13391 pflag ? NULL : print_status, NULL,
13392 pflag ? choose_patch : NULL, &cpa,
13393 allow_bad_symlinks, repo);
13396 if (patch_script_file && fclose(patch_script_file) == EOF &&
13398 error = got_error_from_errno2("fclose", patch_script_path);
13400 const struct got_error *close_err = got_repo_close(repo);
13405 got_worktree_close(worktree);
13407 const struct got_error *pack_err =
13408 got_repo_pack_fds_close(pack_fds);
13412 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13418 usage_unstage(void)
13420 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13421 "[path ...]\n", getprogname());
13426 static const struct got_error *
13427 cmd_unstage(int argc, char *argv[])
13429 const struct got_error *error = NULL;
13430 struct got_repository *repo = NULL;
13431 struct got_worktree *worktree = NULL;
13433 struct got_pathlist_head paths;
13435 struct got_update_progress_arg upa;
13436 FILE *patch_script_file = NULL;
13437 const char *patch_script_path = NULL;
13438 struct choose_patch_arg cpa;
13439 int *pack_fds = NULL;
13441 TAILQ_INIT(&paths);
13443 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13446 patch_script_path = optarg;
13461 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13462 "unveil", NULL) == -1)
13465 if (patch_script_path && !pflag)
13466 errx(1, "-F option can only be used together with -p option");
13468 cwd = getcwd(NULL, 0);
13470 error = got_error_from_errno("getcwd");
13474 error = got_repo_pack_fds_open(&pack_fds);
13478 error = got_worktree_open(&worktree, cwd);
13480 if (error->code == GOT_ERR_NOT_WORKTREE)
13481 error = wrap_not_worktree_error(error, "unstage", cwd);
13485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13490 if (patch_script_path) {
13491 patch_script_file = fopen(patch_script_path, "re");
13492 if (patch_script_file == NULL) {
13493 error = got_error_from_errno2("fopen",
13494 patch_script_path);
13499 error = apply_unveil(got_repo_get_path(repo), 0,
13500 got_worktree_get_root_path(worktree));
13504 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13508 cpa.patch_script_file = patch_script_file;
13509 cpa.action = "unstage";
13510 memset(&upa, 0, sizeof(upa));
13511 error = got_worktree_unstage(worktree, &paths, update_progress,
13512 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13514 print_merge_progress_stats(&upa);
13516 if (patch_script_file && fclose(patch_script_file) == EOF &&
13518 error = got_error_from_errno2("fclose", patch_script_path);
13520 const struct got_error *close_err = got_repo_close(repo);
13525 got_worktree_close(worktree);
13527 const struct got_error *pack_err =
13528 got_repo_pack_fds_close(pack_fds);
13532 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13540 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
13541 "arg ...\n", getprogname());
13545 static const struct got_error *
13546 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13548 const struct got_error *err;
13549 struct got_blob_object *blob;
13552 fd = got_opentempfd();
13554 return got_error_from_errno("got_opentempfd");
13556 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
13560 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
13562 if (fd != -1 && close(fd) == -1 && err == NULL)
13563 err = got_error_from_errno("close");
13565 got_object_blob_close(blob);
13569 static const struct got_error *
13570 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13572 const struct got_error *err;
13573 struct got_tree_object *tree;
13576 err = got_object_open_as_tree(&tree, repo, id);
13580 nentries = got_object_tree_get_nentries(tree);
13581 for (i = 0; i < nentries; i++) {
13582 struct got_tree_entry *te;
13584 if (sigint_received || sigpipe_received)
13586 te = got_object_tree_get_entry(tree, i);
13587 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
13590 fprintf(outfile, "%s %.7o %s\n", id_str,
13591 got_tree_entry_get_mode(te),
13592 got_tree_entry_get_name(te));
13596 got_object_tree_close(tree);
13600 static const struct got_error *
13601 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13603 const struct got_error *err;
13604 struct got_commit_object *commit;
13605 const struct got_object_id_queue *parent_ids;
13606 struct got_object_qid *pid;
13607 char *id_str = NULL;
13608 const char *logmsg = NULL;
13611 err = got_object_open_as_commit(&commit, repo, id);
13615 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
13619 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
13620 parent_ids = got_object_commit_get_parent_ids(commit);
13621 fprintf(outfile, "numparents %d\n",
13622 got_object_commit_get_nparents(commit));
13623 STAILQ_FOREACH(pid, parent_ids, entry) {
13625 err = got_object_id_str(&pid_str, &pid->id);
13628 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
13631 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13632 got_object_commit_get_author_gmtoff(commit));
13633 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
13634 got_object_commit_get_author(commit),
13635 (long long)got_object_commit_get_author_time(commit),
13638 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13639 got_object_commit_get_committer_gmtoff(commit));
13640 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
13641 got_object_commit_get_committer(commit),
13642 (long long)got_object_commit_get_committer_time(commit),
13645 logmsg = got_object_commit_get_logmsg_raw(commit);
13646 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
13647 fprintf(outfile, "%s", logmsg);
13650 got_object_commit_close(commit);
13654 static const struct got_error *
13655 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
13657 const struct got_error *err;
13658 struct got_tag_object *tag;
13659 char *id_str = NULL;
13660 const char *tagmsg = NULL;
13663 err = got_object_open_as_tag(&tag, repo, id);
13667 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
13671 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
13673 switch (got_object_tag_get_object_type(tag)) {
13674 case GOT_OBJ_TYPE_BLOB:
13675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13676 GOT_OBJ_LABEL_BLOB);
13678 case GOT_OBJ_TYPE_TREE:
13679 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13680 GOT_OBJ_LABEL_TREE);
13682 case GOT_OBJ_TYPE_COMMIT:
13683 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13684 GOT_OBJ_LABEL_COMMIT);
13686 case GOT_OBJ_TYPE_TAG:
13687 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
13688 GOT_OBJ_LABEL_TAG);
13694 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
13695 got_object_tag_get_name(tag));
13697 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
13698 got_object_tag_get_tagger_gmtoff(tag));
13699 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
13700 got_object_tag_get_tagger(tag),
13701 (long long)got_object_tag_get_tagger_time(tag),
13704 tagmsg = got_object_tag_get_message(tag);
13705 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
13706 fprintf(outfile, "%s", tagmsg);
13709 got_object_tag_close(tag);
13713 static const struct got_error *
13714 cmd_cat(int argc, char *argv[])
13716 const struct got_error *error;
13717 struct got_repository *repo = NULL;
13718 struct got_worktree *worktree = NULL;
13719 char *cwd = NULL, *repo_path = NULL, *label = NULL;
13720 const char *commit_id_str = NULL;
13721 struct got_object_id *id = NULL, *commit_id = NULL;
13722 struct got_commit_object *commit = NULL;
13723 int ch, obj_type, i, force_path = 0;
13724 struct got_reflist_head refs;
13725 int *pack_fds = NULL;
13730 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13735 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
13738 commit_id_str = optarg;
13744 repo_path = realpath(optarg, NULL);
13745 if (repo_path == NULL)
13746 return got_error_from_errno2("realpath",
13748 got_path_strip_trailing_slashes(repo_path);
13759 cwd = getcwd(NULL, 0);
13761 error = got_error_from_errno("getcwd");
13765 error = got_repo_pack_fds_open(&pack_fds);
13769 if (repo_path == NULL) {
13770 error = got_worktree_open(&worktree, cwd);
13771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
13774 repo_path = strdup(
13775 got_worktree_get_repo_path(worktree));
13776 if (repo_path == NULL) {
13777 error = got_error_from_errno("strdup");
13781 /* Release work tree lock. */
13782 got_worktree_close(worktree);
13787 if (repo_path == NULL) {
13788 repo_path = strdup(cwd);
13789 if (repo_path == NULL)
13790 return got_error_from_errno("strdup");
13793 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
13798 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
13802 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
13806 if (commit_id_str == NULL)
13807 commit_id_str = GOT_REF_HEAD;
13808 error = got_repo_match_object_id(&commit_id, NULL,
13809 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
13813 error = got_object_open_as_commit(&commit, repo, commit_id);
13817 for (i = 0; i < argc; i++) {
13819 error = got_object_id_by_path(&id, repo, commit,
13824 error = got_repo_match_object_id(&id, &label, argv[i],
13825 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
13828 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
13829 error->code != GOT_ERR_NOT_REF)
13831 error = got_object_id_by_path(&id, repo,
13838 error = got_object_get_type(&obj_type, repo, id);
13842 switch (obj_type) {
13843 case GOT_OBJ_TYPE_BLOB:
13844 error = cat_blob(id, repo, stdout);
13846 case GOT_OBJ_TYPE_TREE:
13847 error = cat_tree(id, repo, stdout);
13849 case GOT_OBJ_TYPE_COMMIT:
13850 error = cat_commit(id, repo, stdout);
13852 case GOT_OBJ_TYPE_TAG:
13853 error = cat_tag(id, repo, stdout);
13856 error = got_error(GOT_ERR_OBJ_TYPE);
13871 got_object_commit_close(commit);
13873 got_worktree_close(worktree);
13875 const struct got_error *close_err = got_repo_close(repo);
13880 const struct got_error *pack_err =
13881 got_repo_pack_fds_close(pack_fds);
13886 got_ref_list_free(&refs);
13893 fprintf(stderr, "usage: %s info [path ...]\n",
13898 static const struct got_error *
13899 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
13900 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
13901 struct got_object_id *commit_id)
13903 const struct got_error *err = NULL;
13904 char *id_str = NULL;
13906 struct tm mytm, *tm;
13907 struct got_pathlist_head *paths = arg;
13908 struct got_pathlist_entry *pe;
13911 * Clear error indication from any of the path arguments which
13912 * would cause this file index entry to be displayed.
13914 TAILQ_FOREACH(pe, paths, entry) {
13915 if (got_path_cmp(path, pe->path, strlen(path),
13916 pe->path_len) == 0 ||
13917 got_path_is_child(path, pe->path, pe->path_len))
13918 pe->data = NULL; /* no error */
13921 printf(GOT_COMMIT_SEP_STR);
13923 printf("symlink: %s\n", path);
13924 else if (S_ISREG(mode)) {
13925 printf("file: %s\n", path);
13926 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
13927 } else if (S_ISDIR(mode))
13928 printf("directory: %s\n", path);
13930 printf("something: %s\n", path);
13932 tm = localtime_r(&mtime, &mytm);
13935 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
13936 return got_error(GOT_ERR_NO_SPACE);
13937 printf("timestamp: %s\n", datebuf);
13940 err = got_object_id_str(&id_str, blob_id);
13943 printf("based on blob: %s\n", id_str);
13947 if (staged_blob_id) {
13948 err = got_object_id_str(&id_str, staged_blob_id);
13951 printf("based on staged blob: %s\n", id_str);
13956 err = got_object_id_str(&id_str, commit_id);
13959 printf("based on commit: %s\n", id_str);
13966 static const struct got_error *
13967 cmd_info(int argc, char *argv[])
13969 const struct got_error *error = NULL;
13970 struct got_worktree *worktree = NULL;
13971 char *cwd = NULL, *id_str = NULL;
13972 struct got_pathlist_head paths;
13973 char *uuidstr = NULL;
13974 int ch, show_files = 0;
13976 TAILQ_INIT(&paths);
13978 while ((ch = getopt(argc, argv, "")) != -1) {
13990 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
13994 cwd = getcwd(NULL, 0);
13996 error = got_error_from_errno("getcwd");
14000 error = got_worktree_open(&worktree, cwd);
14002 if (error->code == GOT_ERR_NOT_WORKTREE)
14003 error = wrap_not_worktree_error(error, "info", cwd);
14008 /* Remove "wpath cpath proc exec sendfd" promises. */
14009 if (pledge("stdio rpath flock unveil", NULL) == -1)
14012 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14017 error = get_worktree_paths_from_argv(&paths, argc, argv,
14024 error = got_object_id_str(&id_str,
14025 got_worktree_get_base_commit_id(worktree));
14029 error = got_worktree_get_uuid(&uuidstr, worktree);
14033 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14034 printf("work tree base commit: %s\n", id_str);
14035 printf("work tree path prefix: %s\n",
14036 got_worktree_get_path_prefix(worktree));
14037 printf("work tree branch reference: %s\n",
14038 got_worktree_get_head_ref_name(worktree));
14039 printf("work tree UUID: %s\n", uuidstr);
14040 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14043 struct got_pathlist_entry *pe;
14044 TAILQ_FOREACH(pe, &paths, entry) {
14045 if (pe->path_len == 0)
14048 * Assume this path will fail. This will be corrected
14049 * in print_path_info() in case the path does suceeed.
14051 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14053 error = got_worktree_path_info(worktree, &paths,
14054 print_path_info, &paths, check_cancelled, NULL);
14057 TAILQ_FOREACH(pe, &paths, entry) {
14058 if (pe->data != NULL) {
14059 const struct got_error *perr;
14062 error = got_error_fmt(perr->code, "%s",
14070 got_worktree_close(worktree);
14071 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);