2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "got_compat.h"
21 #include <sys/queue.h>
23 #include <sys/types.h>
44 #include "got_version.h"
45 #include "got_error.h"
46 #include "got_object.h"
47 #include "got_reference.h"
48 #include "got_repository.h"
50 #include "got_cancel.h"
51 #include "got_worktree.h"
53 #include "got_commit_graph.h"
54 #include "got_fetch.h"
56 #include "got_blame.h"
57 #include "got_privsep.h"
58 #include "got_opentemp.h"
59 #include "got_gotconfig.h"
61 #include "got_patch.h"
64 #include "got_keyword.h"
67 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
70 #ifndef GOT_DEFAULT_EDITOR
71 #define GOT_DEFAULT_EDITOR "/usr/bin/vi"
74 static volatile sig_atomic_t sigint_received;
75 static volatile sig_atomic_t sigpipe_received;
78 catch_sigint(int signo)
84 catch_sigpipe(int signo)
92 const struct got_error *(*cmd_main)(int, char *[]);
93 void (*cmd_usage)(void);
94 const char *cmd_alias;
97 __dead static void usage(int, int);
98 __dead static void usage_import(void);
99 __dead static void usage_clone(void);
100 __dead static void usage_fetch(void);
101 __dead static void usage_checkout(void);
102 __dead static void usage_update(void);
103 __dead static void usage_log(void);
104 __dead static void usage_diff(void);
105 __dead static void usage_blame(void);
106 __dead static void usage_tree(void);
107 __dead static void usage_status(void);
108 __dead static void usage_ref(void);
109 __dead static void usage_branch(void);
110 __dead static void usage_tag(void);
111 __dead static void usage_add(void);
112 __dead static void usage_remove(void);
113 __dead static void usage_patch(void);
114 __dead static void usage_revert(void);
115 __dead static void usage_commit(void);
116 __dead static void usage_send(void);
117 __dead static void usage_cherrypick(void);
118 __dead static void usage_backout(void);
119 __dead static void usage_rebase(void);
120 __dead static void usage_histedit(void);
121 __dead static void usage_integrate(void);
122 __dead static void usage_merge(void);
123 __dead static void usage_stage(void);
124 __dead static void usage_unstage(void);
125 __dead static void usage_cat(void);
126 __dead static void usage_info(void);
128 static const struct got_error* cmd_import(int, char *[]);
129 static const struct got_error* cmd_clone(int, char *[]);
130 static const struct got_error* cmd_fetch(int, char *[]);
131 static const struct got_error* cmd_checkout(int, char *[]);
132 static const struct got_error* cmd_update(int, char *[]);
133 static const struct got_error* cmd_log(int, char *[]);
134 static const struct got_error* cmd_diff(int, char *[]);
135 static const struct got_error* cmd_blame(int, char *[]);
136 static const struct got_error* cmd_tree(int, char *[]);
137 static const struct got_error* cmd_status(int, char *[]);
138 static const struct got_error* cmd_ref(int, char *[]);
139 static const struct got_error* cmd_branch(int, char *[]);
140 static const struct got_error* cmd_tag(int, char *[]);
141 static const struct got_error* cmd_add(int, char *[]);
142 static const struct got_error* cmd_remove(int, char *[]);
143 static const struct got_error* cmd_patch(int, char *[]);
144 static const struct got_error* cmd_revert(int, char *[]);
145 static const struct got_error* cmd_commit(int, char *[]);
146 static const struct got_error* cmd_send(int, char *[]);
147 static const struct got_error* cmd_cherrypick(int, char *[]);
148 static const struct got_error* cmd_backout(int, char *[]);
149 static const struct got_error* cmd_rebase(int, char *[]);
150 static const struct got_error* cmd_histedit(int, char *[]);
151 static const struct got_error* cmd_integrate(int, char *[]);
152 static const struct got_error* cmd_merge(int, char *[]);
153 static const struct got_error* cmd_stage(int, char *[]);
154 static const struct got_error* cmd_unstage(int, char *[]);
155 static const struct got_error* cmd_cat(int, char *[]);
156 static const struct got_error* cmd_info(int, char *[]);
158 static const struct got_cmd got_commands[] = {
159 { "import", cmd_import, usage_import, "im" },
160 { "clone", cmd_clone, usage_clone, "cl" },
161 { "fetch", cmd_fetch, usage_fetch, "fe" },
162 { "checkout", cmd_checkout, usage_checkout, "co" },
163 { "update", cmd_update, usage_update, "up" },
164 { "log", cmd_log, usage_log, "" },
165 { "diff", cmd_diff, usage_diff, "di" },
166 { "blame", cmd_blame, usage_blame, "bl" },
167 { "tree", cmd_tree, usage_tree, "tr" },
168 { "status", cmd_status, usage_status, "st" },
169 { "ref", cmd_ref, usage_ref, "" },
170 { "branch", cmd_branch, usage_branch, "br" },
171 { "tag", cmd_tag, usage_tag, "" },
172 { "add", cmd_add, usage_add, "" },
173 { "remove", cmd_remove, usage_remove, "rm" },
174 { "patch", cmd_patch, usage_patch, "pa" },
175 { "revert", cmd_revert, usage_revert, "rv" },
176 { "commit", cmd_commit, usage_commit, "ci" },
177 { "send", cmd_send, usage_send, "se" },
178 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
179 { "backout", cmd_backout, usage_backout, "bo" },
180 { "rebase", cmd_rebase, usage_rebase, "rb" },
181 { "histedit", cmd_histedit, usage_histedit, "he" },
182 { "integrate", cmd_integrate, usage_integrate,"ig" },
183 { "merge", cmd_merge, usage_merge, "mg" },
184 { "stage", cmd_stage, usage_stage, "sg" },
185 { "unstage", cmd_unstage, usage_unstage, "ug" },
186 { "cat", cmd_cat, usage_cat, "" },
187 { "info", cmd_info, usage_info, "" },
191 list_commands(FILE *fp)
195 fprintf(fp, "commands:");
196 for (i = 0; i < nitems(got_commands); i++) {
197 const struct got_cmd *cmd = &got_commands[i];
198 fprintf(fp, " %s", cmd->cmd_name);
204 option_conflict(char a, char b)
206 errx(1, "-%c and -%c options are mutually exclusive", a, b);
210 main(int argc, char *argv[])
212 const struct got_cmd *cmd;
215 int hflag = 0, Vflag = 0;
216 static const struct option longopts[] = {
217 { "version", no_argument, NULL, 'V' },
221 setlocale(LC_CTYPE, "");
223 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
243 got_version_print_str();
248 usage(hflag, hflag ? 0 : 1);
250 signal(SIGINT, catch_sigint);
251 signal(SIGPIPE, catch_sigpipe);
253 for (i = 0; i < nitems(got_commands); i++) {
254 const struct got_error *error;
256 cmd = &got_commands[i];
258 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
259 strcmp(cmd->cmd_alias, argv[0]) != 0)
265 error = cmd->cmd_main(argc, argv);
266 if (error && error->code != GOT_ERR_CANCELLED &&
267 error->code != GOT_ERR_PRIVSEP_EXIT &&
268 !(sigpipe_received &&
269 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
271 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
273 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
280 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
281 list_commands(stderr);
286 usage(int hflag, int status)
288 FILE *fp = (status == 0) ? stdout : stderr;
290 fprintf(fp, "usage: %s [-hV] command [arg ...]\n",
297 static const struct got_error *
298 get_editor(char **abspath)
300 const struct got_error *err = NULL;
305 editor = getenv("VISUAL");
307 editor = getenv("EDITOR");
310 err = got_path_find_prog(abspath, editor);
315 if (*abspath == NULL) {
316 *abspath = strdup(GOT_DEFAULT_EDITOR);
317 if (*abspath == NULL)
318 return got_error_from_errno("strdup");
324 static const struct got_error *
325 apply_unveil(const char *repo_path, int repo_read_only,
326 const char *worktree_path)
328 const struct got_error *err;
331 if (unveil("gmon.out", "rwc") != 0)
332 return got_error_from_errno2("unveil", "gmon.out");
334 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
335 return got_error_from_errno2("unveil", repo_path);
337 if (worktree_path && unveil(worktree_path, "rwc") != 0)
338 return got_error_from_errno2("unveil", worktree_path);
340 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
341 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
343 err = got_privsep_unveil_exec_helpers();
347 if (unveil(NULL, NULL) != 0)
348 return got_error_from_errno("unveil");
356 fprintf(stderr, "usage: %s import [-b branch] [-I pattern] [-m message] "
357 "[-r repository-path] directory\n", getprogname());
362 spawn_editor(const char *editor, const char *file)
365 sig_t sighup, sigint, sigquit;
368 sighup = signal(SIGHUP, SIG_IGN);
369 sigint = signal(SIGINT, SIG_IGN);
370 sigquit = signal(SIGQUIT, SIG_IGN);
372 switch (pid = fork()) {
376 execl(editor, editor, file, (char *)NULL);
380 while (waitpid(pid, &st, 0) == -1)
385 (void)signal(SIGHUP, sighup);
386 (void)signal(SIGINT, sigint);
387 (void)signal(SIGQUIT, sigquit);
389 if (!WIFEXITED(st)) {
394 return WEXITSTATUS(st);
397 static const struct got_error *
398 read_logmsg(char **logmsg, size_t *len, FILE *fp, size_t filesize)
400 const struct got_error *err = NULL;
407 if (fseeko(fp, 0L, SEEK_SET) == -1)
408 return got_error_from_errno("fseeko");
410 *logmsg = malloc(filesize + 1);
412 return got_error_from_errno("malloc");
415 while (getline(&line, &linesize, fp) != -1) {
416 if (line[0] == '#' || (*len == 0 && line[0] == '\n'))
417 continue; /* remove comments and leading empty lines */
418 *len = strlcat(*logmsg, line, filesize + 1);
419 if (*len >= filesize + 1) {
420 err = got_error(GOT_ERR_NO_SPACE);
425 err = got_ferror(fp, GOT_ERR_IO);
429 while (*len > 0 && (*logmsg)[*len - 1] == '\n') {
430 (*logmsg)[*len - 1] = '\0';
443 static const struct got_error *
444 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
445 const char *initial_content, size_t initial_content_len,
446 int require_modification)
448 const struct got_error *err = NULL;
455 if (stat(logmsg_path, &st) == -1)
456 return got_error_from_errno2("stat", logmsg_path);
458 if (spawn_editor(editor, logmsg_path) == -1)
459 return got_error_from_errno("failed spawning editor");
461 if (require_modification) {
462 struct timespec timeout;
466 nanosleep(&timeout, NULL);
469 if (stat(logmsg_path, &st2) == -1)
470 return got_error_from_errno2("stat", logmsg_path);
472 if (require_modification && st.st_size == st2.st_size &&
473 timespeccmp(&st.st_mtim, &st2.st_mtim, ==))
474 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
475 "no changes made to commit message, aborting");
477 fp = fopen(logmsg_path, "re");
479 err = got_error_from_errno("fopen");
483 /* strip comments and leading/trailing newlines */
484 err = read_logmsg(logmsg, &logmsg_len, fp, st2.st_size);
487 if (logmsg_len == 0) {
488 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
489 "commit message cannot be empty, aborting");
493 if (fp && fclose(fp) == EOF && err == NULL)
494 err = got_error_from_errno("fclose");
502 static const struct got_error *
503 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
504 const char *path_dir, const char *branch_name)
506 char *initial_content = NULL;
507 const struct got_error *err = NULL;
508 int initial_content_len;
511 initial_content_len = asprintf(&initial_content,
512 "\n# %s to be imported to branch %s\n", path_dir,
514 if (initial_content_len == -1)
515 return got_error_from_errno("asprintf");
517 err = got_opentemp_named_fd(logmsg_path, &fd,
518 GOT_TMPDIR_STR "/got-importmsg", "");
522 if (write(fd, initial_content, initial_content_len) == -1) {
523 err = got_error_from_errno2("write", *logmsg_path);
526 if (close(fd) == -1) {
527 err = got_error_from_errno2("close", *logmsg_path);
532 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content,
533 initial_content_len, 1);
535 if (fd != -1 && close(fd) == -1 && err == NULL)
536 err = got_error_from_errno2("close", *logmsg_path);
537 free(initial_content);
545 static const struct got_error *
546 import_progress(void *arg, const char *path)
548 printf("A %s\n", path);
552 static const struct got_error *
553 valid_author(const char *author)
555 const char *email = author;
558 * Git' expects the author (or committer) to be in the form
559 * "name <email>", which are mostly free form (see the
560 * "committer" description in git-fast-import(1)). We're only
561 * doing this to avoid git's object parser breaking on commits
565 while (*author && *author != '\n' && *author != '<' && *author != '>')
567 if (author != email && *author == '<' && *(author - 1) != ' ')
568 return got_error_fmt(GOT_ERR_COMMIT_BAD_AUTHOR, "%s: space "
569 "between author name and email required", email);
570 if (*author++ != '<')
571 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
572 while (*author && *author != '\n' && *author != '<' && *author != '>')
574 if (strcmp(author, ">") != 0)
575 return got_error_fmt(GOT_ERR_COMMIT_NO_EMAIL, "%s", email);
579 static const struct got_error *
580 get_author(char **author, struct got_repository *repo,
581 struct got_worktree *worktree)
583 const struct got_error *err = NULL;
584 const char *got_author = NULL, *name, *email;
585 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
590 worktree_conf = got_worktree_get_gotconfig(worktree);
591 repo_conf = got_repo_get_gotconfig(repo);
594 * Priority of potential author information sources, from most
595 * significant to least significant:
596 * 1) work tree's .got/got.conf file
597 * 2) repository's got.conf file
598 * 3) repository's git config file
599 * 4) environment variables
600 * 5) global git config files (in user's home directory or /etc)
604 got_author = got_gotconfig_get_author(worktree_conf);
605 if (got_author == NULL)
606 got_author = got_gotconfig_get_author(repo_conf);
607 if (got_author == NULL) {
608 name = got_repo_get_gitconfig_author_name(repo);
609 email = got_repo_get_gitconfig_author_email(repo);
611 if (asprintf(author, "%s <%s>", name, email) == -1)
612 return got_error_from_errno("asprintf");
616 got_author = getenv("GOT_AUTHOR");
617 if (got_author == NULL) {
618 name = got_repo_get_global_gitconfig_author_name(repo);
619 email = got_repo_get_global_gitconfig_author_email(
622 if (asprintf(author, "%s <%s>", name, email)
624 return got_error_from_errno("asprintf");
627 /* TODO: Look up user in password database? */
628 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
632 *author = strdup(got_author);
634 return got_error_from_errno("strdup");
636 err = valid_author(*author);
644 static const struct got_error *
645 get_allowed_signers(char **allowed_signers, struct got_repository *repo,
646 struct got_worktree *worktree)
648 const char *got_allowed_signers = NULL;
649 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
651 *allowed_signers = NULL;
654 worktree_conf = got_worktree_get_gotconfig(worktree);
655 repo_conf = got_repo_get_gotconfig(repo);
658 * Priority of potential author information sources, from most
659 * significant to least significant:
660 * 1) work tree's .got/got.conf file
661 * 2) repository's got.conf file
665 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
667 if (got_allowed_signers == NULL)
668 got_allowed_signers = got_gotconfig_get_allowed_signers_file(
671 if (got_allowed_signers) {
672 *allowed_signers = strdup(got_allowed_signers);
673 if (*allowed_signers == NULL)
674 return got_error_from_errno("strdup");
679 static const struct got_error *
680 get_revoked_signers(char **revoked_signers, struct got_repository *repo,
681 struct got_worktree *worktree)
683 const char *got_revoked_signers = NULL;
684 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
686 *revoked_signers = NULL;
689 worktree_conf = got_worktree_get_gotconfig(worktree);
690 repo_conf = got_repo_get_gotconfig(repo);
693 * Priority of potential author information sources, from most
694 * significant to least significant:
695 * 1) work tree's .got/got.conf file
696 * 2) repository's got.conf file
700 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
702 if (got_revoked_signers == NULL)
703 got_revoked_signers = got_gotconfig_get_revoked_signers_file(
706 if (got_revoked_signers) {
707 *revoked_signers = strdup(got_revoked_signers);
708 if (*revoked_signers == NULL)
709 return got_error_from_errno("strdup");
715 get_signer_id(struct got_repository *repo, struct got_worktree *worktree)
717 const char *got_signer_id = NULL;
718 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
721 worktree_conf = got_worktree_get_gotconfig(worktree);
722 repo_conf = got_repo_get_gotconfig(repo);
725 * Priority of potential author information sources, from most
726 * significant to least significant:
727 * 1) work tree's .got/got.conf file
728 * 2) repository's got.conf file
732 got_signer_id = got_gotconfig_get_signer_id(worktree_conf);
733 if (got_signer_id == NULL)
734 got_signer_id = got_gotconfig_get_signer_id(repo_conf);
736 return got_signer_id;
739 static const struct got_error *
740 get_gitconfig_path(char **gitconfig_path)
742 const char *homedir = getenv("HOME");
744 *gitconfig_path = NULL;
746 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
747 return got_error_from_errno("asprintf");
753 static const struct got_error *
754 cmd_import(int argc, char *argv[])
756 const struct got_error *error = NULL;
757 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
758 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
759 const char *branch_name = NULL;
760 char *id_str = NULL, *logmsg_path = NULL;
761 char refname[PATH_MAX] = "refs/heads/";
762 struct got_repository *repo = NULL;
763 struct got_reference *branch_ref = NULL, *head_ref = NULL;
764 struct got_object_id *new_commit_id = NULL;
766 struct got_pathlist_head ignores;
767 struct got_pathlist_entry *pe;
768 int preserve_logmsg = 0;
769 int *pack_fds = NULL;
771 TAILQ_INIT(&ignores);
774 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
780 while ((ch = getopt(argc, argv, "b:I:m:r:")) != -1) {
783 branch_name = optarg;
786 if (optarg[0] == '\0')
788 error = got_pathlist_insert(&pe, &ignores, optarg,
794 logmsg = strdup(optarg);
795 if (logmsg == NULL) {
796 error = got_error_from_errno("strdup");
801 repo_path = realpath(optarg, NULL);
802 if (repo_path == NULL) {
803 error = got_error_from_errno2("realpath",
820 if (repo_path == NULL) {
821 repo_path = getcwd(NULL, 0);
822 if (repo_path == NULL)
823 return got_error_from_errno("getcwd");
825 got_path_strip_trailing_slashes(repo_path);
826 error = get_gitconfig_path(&gitconfig_path);
829 error = got_repo_pack_fds_open(&pack_fds);
832 error = got_repo_open(&repo, repo_path, gitconfig_path, pack_fds);
836 path_dir = realpath(argv[0], NULL);
837 if (path_dir == NULL) {
838 error = got_error_from_errno2("realpath", argv[0]);
841 got_path_strip_trailing_slashes(path_dir);
843 error = get_editor(&editor);
847 if (unveil(path_dir, "r") != 0) {
848 error = got_error_from_errno2("unveil", path_dir);
851 if (unveil(editor, "x") != 0) {
852 error = got_error_from_errno2("unveil", editor);
855 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
859 error = get_author(&author, repo, NULL);
864 * Don't let the user create a branch name with a leading '-'.
865 * While technically a valid reference name, this case is usually
866 * an unintended typo.
868 if (branch_name && branch_name[0] == '-')
869 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
871 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
872 if (error && error->code != GOT_ERR_NOT_REF)
876 n = strlcat(refname, branch_name, sizeof(refname));
877 else if (head_ref && got_ref_is_symbolic(head_ref))
878 n = strlcpy(refname, got_ref_get_symref_target(head_ref),
881 n = strlcat(refname, "main", sizeof(refname));
882 if (n >= sizeof(refname)) {
883 error = got_error(GOT_ERR_NO_SPACE);
887 error = got_ref_open(&branch_ref, repo, refname, 0);
889 if (error->code != GOT_ERR_NOT_REF)
892 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
893 "import target branch already exists");
897 if (logmsg == NULL || *logmsg == '\0') {
899 error = collect_import_msg(&logmsg, &logmsg_path, editor,
902 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
909 error = got_repo_import(&new_commit_id, path_dir, logmsg,
910 author, &ignores, repo, import_progress, NULL);
917 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
924 error = got_ref_write(branch_ref, repo);
931 error = got_object_id_str(&id_str, new_commit_id);
938 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
940 if (error->code != GOT_ERR_NOT_REF) {
946 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
954 error = got_ref_write(head_ref, repo);
962 printf("Created branch %s with commit %s\n",
963 got_ref_get_name(branch_ref), id_str);
966 const struct got_error *pack_err =
967 got_repo_pack_fds_close(pack_fds);
972 const struct got_error *close_err = got_repo_close(repo);
976 if (preserve_logmsg) {
977 fprintf(stderr, "%s: log message preserved in %s\n",
978 getprogname(), logmsg_path);
979 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
980 error = got_error_from_errno2("unlink", logmsg_path);
988 free(gitconfig_path);
990 got_ref_close(branch_ref);
992 got_ref_close(head_ref);
999 fprintf(stderr, "usage: %s clone [-almqv] [-b branch] [-R reference] "
1000 "repository-URL [directory]\n", getprogname());
1004 struct got_fetch_progress_arg {
1005 char last_scaled_size[FMT_SCALED_STRSIZE];
1007 int last_p_resolved;
1010 struct got_repository *repo;
1013 int configs_created;
1015 struct got_pathlist_head *symrefs;
1016 struct got_pathlist_head *wanted_branches;
1017 struct got_pathlist_head *wanted_refs;
1021 const char *remote_repo_path;
1022 const char *git_url;
1023 int fetch_all_branches;
1024 int mirror_references;
1028 /* XXX forward declaration */
1029 static const struct got_error *
1030 create_config_files(const char *proto, const char *host, const char *port,
1031 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1032 int mirror_references, struct got_pathlist_head *symrefs,
1033 struct got_pathlist_head *wanted_branches,
1034 struct got_pathlist_head *wanted_refs, struct got_repository *repo);
1036 static const struct got_error *
1037 fetch_progress(void *arg, const char *message, off_t packfile_size,
1038 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
1040 const struct got_error *err = NULL;
1041 struct got_fetch_progress_arg *a = arg;
1042 char scaled_size[FMT_SCALED_STRSIZE];
1043 int p_indexed, p_resolved;
1044 int print_size = 0, print_indexed = 0, print_resolved = 0;
1047 * In order to allow a failed clone to be resumed with 'got fetch'
1048 * we try to create configuration files as soon as possible.
1049 * Once the server has sent information about its default branch
1050 * we have all required information.
1052 if (a->create_configs && !a->configs_created &&
1053 !TAILQ_EMPTY(a->config_info.symrefs)) {
1054 err = create_config_files(a->config_info.proto,
1055 a->config_info.host, a->config_info.port,
1056 a->config_info.remote_repo_path,
1057 a->config_info.git_url,
1058 a->config_info.fetch_all_branches,
1059 a->config_info.mirror_references,
1060 a->config_info.symrefs,
1061 a->config_info.wanted_branches,
1062 a->config_info.wanted_refs, a->repo);
1065 a->configs_created = 1;
1068 if (a->verbosity < 0)
1071 if (message && message[0] != '\0') {
1072 printf("\rserver: %s", message);
1077 if (packfile_size > 0 || nobj_indexed > 0) {
1078 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
1079 (a->last_scaled_size[0] == '\0' ||
1080 strcmp(scaled_size, a->last_scaled_size)) != 0) {
1082 if (strlcpy(a->last_scaled_size, scaled_size,
1083 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
1084 return got_error(GOT_ERR_NO_SPACE);
1086 if (nobj_indexed > 0) {
1087 p_indexed = (nobj_indexed * 100) / nobj_total;
1088 if (p_indexed != a->last_p_indexed) {
1089 a->last_p_indexed = p_indexed;
1094 if (nobj_resolved > 0) {
1095 p_resolved = (nobj_resolved * 100) /
1096 (nobj_total - nobj_loose);
1097 if (p_resolved != a->last_p_resolved) {
1098 a->last_p_resolved = p_resolved;
1106 if (print_size || print_indexed || print_resolved)
1109 printf("%*s fetched", FMT_SCALED_STRSIZE - 2, scaled_size);
1111 printf("; indexing %d%%", p_indexed);
1113 printf("; resolving deltas %d%%", p_resolved);
1114 if (print_size || print_indexed || print_resolved)
1120 static const struct got_error *
1121 create_symref(const char *refname, struct got_reference *target_ref,
1122 int verbosity, struct got_repository *repo)
1124 const struct got_error *err;
1125 struct got_reference *head_symref;
1127 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
1131 err = got_ref_write(head_symref, repo);
1132 if (err == NULL && verbosity > 0) {
1133 printf("Created reference %s: %s\n", GOT_REF_HEAD,
1134 got_ref_get_name(target_ref));
1136 got_ref_close(head_symref);
1140 static const struct got_error *
1141 list_remote_refs(struct got_pathlist_head *symrefs,
1142 struct got_pathlist_head *refs)
1144 const struct got_error *err;
1145 struct got_pathlist_entry *pe;
1147 TAILQ_FOREACH(pe, symrefs, entry) {
1148 const char *refname = pe->path;
1149 const char *targetref = pe->data;
1151 printf("%s: %s\n", refname, targetref);
1154 TAILQ_FOREACH(pe, refs, entry) {
1155 const char *refname = pe->path;
1156 struct got_object_id *id = pe->data;
1159 err = got_object_id_str(&id_str, id);
1162 printf("%s: %s\n", refname, id_str);
1169 static const struct got_error *
1170 create_ref(const char *refname, struct got_object_id *id,
1171 int verbosity, struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 struct got_reference *ref;
1177 err = got_object_id_str(&id_str, id);
1181 err = got_ref_alloc(&ref, refname, id);
1185 err = got_ref_write(ref, repo);
1188 if (err == NULL && verbosity >= 0)
1189 printf("Created reference %s: %s\n", refname, id_str);
1196 match_wanted_ref(const char *refname, const char *wanted_ref)
1198 if (strncmp(refname, "refs/", 5) != 0)
1203 * Prevent fetching of references that won't make any
1204 * sense outside of the remote repository's context.
1206 if (strncmp(refname, "got/", 4) == 0)
1208 if (strncmp(refname, "remotes/", 8) == 0)
1211 if (strncmp(wanted_ref, "refs/", 5) == 0)
1214 /* Allow prefix match. */
1215 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1218 /* Allow exact match. */
1219 return (strcmp(refname, wanted_ref) == 0);
1223 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1225 struct got_pathlist_entry *pe;
1227 TAILQ_FOREACH(pe, wanted_refs, entry) {
1228 if (match_wanted_ref(refname, pe->path))
1235 static const struct got_error *
1236 create_wanted_ref(const char *refname, struct got_object_id *id,
1237 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1239 const struct got_error *err;
1240 char *remote_refname;
1242 if (strncmp("refs/", refname, 5) == 0)
1245 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1246 remote_repo_name, refname) == -1)
1247 return got_error_from_errno("asprintf");
1249 err = create_ref(remote_refname, id, verbosity, repo);
1250 free(remote_refname);
1254 static const struct got_error *
1255 create_gotconfig(const char *proto, const char *host, const char *port,
1256 const char *remote_repo_path, const char *default_branch,
1257 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1258 struct got_pathlist_head *wanted_refs, int mirror_references,
1259 struct got_repository *repo)
1261 const struct got_error *err = NULL;
1262 char *gotconfig_path = NULL;
1263 char *gotconfig = NULL;
1264 FILE *gotconfig_file = NULL;
1265 const char *branchname = NULL;
1266 char *branches = NULL, *refs = NULL;
1269 if (!fetch_all_branches && !TAILQ_EMPTY(wanted_branches)) {
1270 struct got_pathlist_entry *pe;
1271 TAILQ_FOREACH(pe, wanted_branches, entry) {
1273 branchname = pe->path;
1274 if (strncmp(branchname, "refs/heads/", 11) == 0)
1276 if (asprintf(&s, "%s\"%s\" ",
1277 branches ? branches : "", branchname) == -1) {
1278 err = got_error_from_errno("asprintf");
1284 } else if (!fetch_all_branches && default_branch) {
1285 branchname = default_branch;
1286 if (strncmp(branchname, "refs/heads/", 11) == 0)
1288 if (asprintf(&branches, "\"%s\" ", branchname) == -1) {
1289 err = got_error_from_errno("asprintf");
1293 if (!TAILQ_EMPTY(wanted_refs)) {
1294 struct got_pathlist_entry *pe;
1295 TAILQ_FOREACH(pe, wanted_refs, entry) {
1297 const char *refname = pe->path;
1298 if (strncmp(refname, "refs/", 5) == 0)
1300 if (asprintf(&s, "%s\"%s\" ",
1301 refs ? refs : "", refname) == -1) {
1302 err = got_error_from_errno("asprintf");
1310 /* Create got.conf(5). */
1311 gotconfig_path = got_repo_get_path_gotconfig(repo);
1312 if (gotconfig_path == NULL) {
1313 err = got_error_from_errno("got_repo_get_path_gotconfig");
1316 gotconfig_file = fopen(gotconfig_path, "ae");
1317 if (gotconfig_file == NULL) {
1318 err = got_error_from_errno2("fopen", gotconfig_path);
1321 if (asprintf(&gotconfig,
1326 "\trepository \"%s\"\n"
1332 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1333 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1334 remote_repo_path, branches ? "\tbranch { " : "",
1335 branches ? branches : "", branches ? "}\n" : "",
1336 refs ? "\treference { " : "", refs ? refs : "", refs ? "}\n" : "",
1337 mirror_references ? "\tmirror_references yes\n" : "",
1338 fetch_all_branches ? "\tfetch_all_branches yes\n" : "") == -1) {
1339 err = got_error_from_errno("asprintf");
1342 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1343 if (n != strlen(gotconfig)) {
1344 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1349 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1350 err = got_error_from_errno2("fclose", gotconfig_path);
1351 free(gotconfig_path);
1356 static const struct got_error *
1357 create_gitconfig(const char *git_url, const char *default_branch,
1358 int fetch_all_branches, struct got_pathlist_head *wanted_branches,
1359 struct got_pathlist_head *wanted_refs, int mirror_references,
1360 struct got_repository *repo)
1362 const struct got_error *err = NULL;
1363 char *gitconfig_path = NULL;
1364 char *gitconfig = NULL;
1365 FILE *gitconfig_file = NULL;
1366 char *branches = NULL, *refs = NULL;
1367 const char *branchname;
1370 /* Create a config file Git can understand. */
1371 gitconfig_path = got_repo_get_path_gitconfig(repo);
1372 if (gitconfig_path == NULL) {
1373 err = got_error_from_errno("got_repo_get_path_gitconfig");
1376 gitconfig_file = fopen(gitconfig_path, "ae");
1377 if (gitconfig_file == NULL) {
1378 err = got_error_from_errno2("fopen", gitconfig_path);
1381 if (fetch_all_branches) {
1382 if (mirror_references) {
1383 if (asprintf(&branches,
1384 "\tfetch = refs/heads/*:refs/heads/*\n") == -1) {
1385 err = got_error_from_errno("asprintf");
1388 } else if (asprintf(&branches,
1389 "\tfetch = refs/heads/*:refs/remotes/%s/*\n",
1390 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1391 err = got_error_from_errno("asprintf");
1394 } else if (!TAILQ_EMPTY(wanted_branches)) {
1395 struct got_pathlist_entry *pe;
1396 TAILQ_FOREACH(pe, wanted_branches, entry) {
1398 branchname = pe->path;
1399 if (strncmp(branchname, "refs/heads/", 11) == 0)
1401 if (mirror_references) {
1403 "%s\tfetch = refs/heads/%s:refs/heads/%s\n",
1404 branches ? branches : "",
1405 branchname, branchname) == -1) {
1406 err = got_error_from_errno("asprintf");
1409 } else if (asprintf(&s,
1410 "%s\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1411 branches ? branches : "",
1412 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1413 branchname) == -1) {
1414 err = got_error_from_errno("asprintf");
1422 * If the server specified a default branch, use just that one.
1423 * Otherwise fall back to fetching all branches on next fetch.
1425 if (default_branch) {
1426 branchname = default_branch;
1427 if (strncmp(branchname, "refs/heads/", 11) == 0)
1430 branchname = "*"; /* fall back to all branches */
1431 if (mirror_references) {
1432 if (asprintf(&branches,
1433 "\tfetch = refs/heads/%s:refs/heads/%s\n",
1434 branchname, branchname) == -1) {
1435 err = got_error_from_errno("asprintf");
1438 } else if (asprintf(&branches,
1439 "\tfetch = refs/heads/%s:refs/remotes/%s/%s\n",
1440 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1441 branchname) == -1) {
1442 err = got_error_from_errno("asprintf");
1446 if (!TAILQ_EMPTY(wanted_refs)) {
1447 struct got_pathlist_entry *pe;
1448 TAILQ_FOREACH(pe, wanted_refs, entry) {
1450 const char *refname = pe->path;
1451 if (strncmp(refname, "refs/", 5) == 0)
1453 if (mirror_references) {
1455 "%s\tfetch = refs/%s:refs/%s\n",
1456 refs ? refs : "", refname, refname) == -1) {
1457 err = got_error_from_errno("asprintf");
1460 } else if (asprintf(&s,
1461 "%s\tfetch = refs/%s:refs/remotes/%s/%s\n",
1463 refname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1465 err = got_error_from_errno("asprintf");
1473 if (asprintf(&gitconfig,
1478 "\tfetch = refs/tags/*:refs/tags/*\n",
1479 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url, branches ? branches : "",
1480 refs ? refs : "") == -1) {
1481 err = got_error_from_errno("asprintf");
1484 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1485 if (n != strlen(gitconfig)) {
1486 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1490 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1491 err = got_error_from_errno2("fclose", gitconfig_path);
1492 free(gitconfig_path);
1497 static const struct got_error *
1498 create_config_files(const char *proto, const char *host, const char *port,
1499 const char *remote_repo_path, const char *git_url, int fetch_all_branches,
1500 int mirror_references, struct got_pathlist_head *symrefs,
1501 struct got_pathlist_head *wanted_branches,
1502 struct got_pathlist_head *wanted_refs, struct got_repository *repo)
1504 const struct got_error *err = NULL;
1505 const char *default_branch = NULL;
1506 struct got_pathlist_entry *pe;
1509 * If we asked for a set of wanted branches then use the first
1512 if (!TAILQ_EMPTY(wanted_branches)) {
1513 pe = TAILQ_FIRST(wanted_branches);
1514 default_branch = pe->path;
1516 /* First HEAD ref listed by server is the default branch. */
1517 TAILQ_FOREACH(pe, symrefs, entry) {
1518 const char *refname = pe->path;
1519 const char *target = pe->data;
1521 if (strcmp(refname, GOT_REF_HEAD) != 0)
1524 default_branch = target;
1529 /* Create got.conf(5). */
1530 err = create_gotconfig(proto, host, port, remote_repo_path,
1531 default_branch, fetch_all_branches, wanted_branches,
1532 wanted_refs, mirror_references, repo);
1536 /* Create a config file Git can understand. */
1537 return create_gitconfig(git_url, default_branch, fetch_all_branches,
1538 wanted_branches, wanted_refs, mirror_references, repo);
1541 static const struct got_error *
1542 cmd_clone(int argc, char *argv[])
1544 const struct got_error *error = NULL;
1545 const char *uri, *dirname;
1546 char *proto, *host, *port, *repo_name, *server_path;
1547 char *default_destdir = NULL, *id_str = NULL;
1548 const char *repo_path;
1549 struct got_repository *repo = NULL;
1550 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1551 struct got_pathlist_entry *pe;
1552 struct got_object_id *pack_hash = NULL;
1553 int ch, fetchfd = -1, fetchstatus;
1554 pid_t fetchpid = -1;
1555 struct got_fetch_progress_arg fpa;
1556 char *git_url = NULL;
1557 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1558 int bflag = 0, list_refs_only = 0;
1559 int *pack_fds = NULL;
1562 TAILQ_INIT(&symrefs);
1563 TAILQ_INIT(&wanted_branches);
1564 TAILQ_INIT(&wanted_refs);
1566 while ((ch = getopt(argc, argv, "ab:lmqR:v")) != -1) {
1569 fetch_all_branches = 1;
1572 error = got_pathlist_append(&wanted_branches,
1582 mirror_references = 1;
1588 error = got_pathlist_append(&wanted_refs,
1596 else if (verbosity < 3)
1607 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1608 option_conflict('a', 'b');
1609 if (list_refs_only) {
1610 if (!TAILQ_EMPTY(&wanted_branches))
1611 option_conflict('l', 'b');
1612 if (fetch_all_branches)
1613 option_conflict('l', 'a');
1614 if (mirror_references)
1615 option_conflict('l', 'm');
1616 if (!TAILQ_EMPTY(&wanted_refs))
1617 option_conflict('l', 'R');
1629 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
1634 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1635 host, port ? ":" : "", port ? port : "",
1636 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1637 error = got_error_from_errno("asprintf");
1641 if (strcmp(proto, "git") == 0) {
1643 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1644 "sendfd dns inet unveil", NULL) == -1)
1647 } else if (strcmp(proto, "git+ssh") == 0 ||
1648 strcmp(proto, "ssh") == 0) {
1650 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1651 "sendfd unveil", NULL) == -1)
1654 } else if (strcmp(proto, "http") == 0 ||
1655 strcmp(proto, "git+http") == 0) {
1656 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1659 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1662 if (dirname == NULL) {
1663 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1664 error = got_error_from_errno("asprintf");
1667 repo_path = default_destdir;
1669 repo_path = dirname;
1671 if (!list_refs_only) {
1672 error = got_path_mkdir(repo_path);
1674 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1675 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1677 if (!got_path_dir_is_empty(repo_path)) {
1678 error = got_error_path(repo_path,
1679 GOT_ERR_DIR_NOT_EMPTY);
1684 error = got_dial_apply_unveil(proto);
1688 error = apply_unveil(repo_path, 0, NULL);
1693 printf("Connecting to %s\n", git_url);
1695 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1696 server_path, verbosity);
1700 if (!list_refs_only) {
1701 error = got_repo_init(repo_path, NULL);
1704 error = got_repo_pack_fds_open(&pack_fds);
1707 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
1712 fpa.last_scaled_size[0] = '\0';
1713 fpa.last_p_indexed = -1;
1714 fpa.last_p_resolved = -1;
1715 fpa.verbosity = verbosity;
1716 fpa.create_configs = 1;
1717 fpa.configs_created = 0;
1719 fpa.config_info.symrefs = &symrefs;
1720 fpa.config_info.wanted_branches = &wanted_branches;
1721 fpa.config_info.wanted_refs = &wanted_refs;
1722 fpa.config_info.proto = proto;
1723 fpa.config_info.host = host;
1724 fpa.config_info.port = port;
1725 fpa.config_info.remote_repo_path = server_path;
1726 fpa.config_info.git_url = git_url;
1727 fpa.config_info.fetch_all_branches = fetch_all_branches;
1728 fpa.config_info.mirror_references = mirror_references;
1729 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1730 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1731 fetch_all_branches, &wanted_branches, &wanted_refs,
1732 list_refs_only, verbosity, fetchfd, repo, NULL, NULL, bflag,
1733 fetch_progress, &fpa);
1737 if (list_refs_only) {
1738 error = list_remote_refs(&symrefs, &refs);
1742 if (pack_hash == NULL) {
1743 error = got_error_fmt(GOT_ERR_FETCH_FAILED, "%s",
1744 "server sent an empty pack file");
1747 error = got_object_id_str(&id_str, pack_hash);
1751 printf("\nFetched %s.pack\n", id_str);
1754 /* Set up references provided with the pack file. */
1755 TAILQ_FOREACH(pe, &refs, entry) {
1756 const char *refname = pe->path;
1757 struct got_object_id *id = pe->data;
1758 char *remote_refname;
1760 if (is_wanted_ref(&wanted_refs, refname) &&
1761 !mirror_references) {
1762 error = create_wanted_ref(refname, id,
1763 GOT_FETCH_DEFAULT_REMOTE_NAME,
1764 verbosity - 1, repo);
1770 error = create_ref(refname, id, verbosity - 1, repo);
1774 if (mirror_references)
1777 if (strncmp("refs/heads/", refname, 11) != 0)
1780 if (asprintf(&remote_refname,
1781 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1782 refname + 11) == -1) {
1783 error = got_error_from_errno("asprintf");
1786 error = create_ref(remote_refname, id, verbosity - 1, repo);
1787 free(remote_refname);
1792 /* Set the HEAD reference if the server provided one. */
1793 TAILQ_FOREACH(pe, &symrefs, entry) {
1794 struct got_reference *target_ref;
1795 const char *refname = pe->path;
1796 const char *target = pe->data;
1797 char *remote_refname = NULL, *remote_target = NULL;
1799 if (strcmp(refname, GOT_REF_HEAD) != 0)
1802 error = got_ref_open(&target_ref, repo, target, 0);
1804 if (error->code == GOT_ERR_NOT_REF) {
1811 error = create_symref(refname, target_ref, verbosity, repo);
1812 got_ref_close(target_ref);
1816 if (mirror_references)
1819 if (strncmp("refs/heads/", target, 11) != 0)
1822 if (asprintf(&remote_refname,
1823 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1825 error = got_error_from_errno("asprintf");
1828 if (asprintf(&remote_target,
1829 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1830 target + 11) == -1) {
1831 error = got_error_from_errno("asprintf");
1832 free(remote_refname);
1835 error = got_ref_open(&target_ref, repo, remote_target, 0);
1837 free(remote_refname);
1838 free(remote_target);
1839 if (error->code == GOT_ERR_NOT_REF) {
1845 error = create_symref(remote_refname, target_ref,
1846 verbosity - 1, repo);
1847 free(remote_refname);
1848 free(remote_target);
1849 got_ref_close(target_ref);
1855 * We failed to set the HEAD reference. If we asked for
1856 * a set of wanted branches use the first of one of those
1857 * which could be fetched instead.
1859 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1860 const char *target = pe->path;
1861 struct got_reference *target_ref;
1863 error = got_ref_open(&target_ref, repo, target, 0);
1865 if (error->code == GOT_ERR_NOT_REF) {
1872 error = create_symref(GOT_REF_HEAD, target_ref,
1874 got_ref_close(target_ref);
1880 if (!fpa.configs_created && pe != NULL) {
1881 error = create_config_files(fpa.config_info.proto,
1882 fpa.config_info.host, fpa.config_info.port,
1883 fpa.config_info.remote_repo_path,
1884 fpa.config_info.git_url,
1885 fpa.config_info.fetch_all_branches,
1886 fpa.config_info.mirror_references,
1887 fpa.config_info.symrefs,
1888 fpa.config_info.wanted_branches,
1889 fpa.config_info.wanted_refs, fpa.repo);
1896 printf("Created %s repository '%s'\n",
1897 mirror_references ? "mirrored" : "cloned", repo_path);
1900 const struct got_error *pack_err =
1901 got_repo_pack_fds_close(pack_fds);
1906 if (kill(fetchpid, SIGTERM) == -1)
1907 error = got_error_from_errno("kill");
1908 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1909 error = got_error_from_errno("waitpid");
1911 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1912 error = got_error_from_errno("close");
1914 const struct got_error *close_err = got_repo_close(repo);
1918 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
1919 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
1920 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
1921 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
1928 free(default_destdir);
1933 static const struct got_error *
1934 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1935 int replace_tags, int verbosity, struct got_repository *repo)
1937 const struct got_error *err = NULL;
1938 char *new_id_str = NULL;
1939 struct got_object_id *old_id = NULL;
1941 err = got_object_id_str(&new_id_str, new_id);
1945 if (!replace_tags &&
1946 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1947 err = got_ref_resolve(&old_id, repo, ref);
1950 if (got_object_id_cmp(old_id, new_id) == 0)
1952 if (verbosity >= 0) {
1953 printf("Rejecting update of existing tag %s: %s\n",
1954 got_ref_get_name(ref), new_id_str);
1959 if (got_ref_is_symbolic(ref)) {
1960 if (verbosity >= 0) {
1961 printf("Replacing reference %s: %s\n",
1962 got_ref_get_name(ref),
1963 got_ref_get_symref_target(ref));
1965 err = got_ref_change_symref_to_ref(ref, new_id);
1968 err = got_ref_write(ref, repo);
1972 err = got_ref_resolve(&old_id, repo, ref);
1975 if (got_object_id_cmp(old_id, new_id) == 0)
1978 err = got_ref_change_ref(ref, new_id);
1981 err = got_ref_write(ref, repo);
1987 printf("Updated %s: %s\n", got_ref_get_name(ref),
1995 static const struct got_error *
1996 update_symref(const char *refname, struct got_reference *target_ref,
1997 int verbosity, struct got_repository *repo)
1999 const struct got_error *err = NULL, *unlock_err;
2000 struct got_reference *symref;
2001 int symref_is_locked = 0;
2003 err = got_ref_open(&symref, repo, refname, 1);
2005 if (err->code != GOT_ERR_NOT_REF)
2007 err = got_ref_alloc_symref(&symref, refname, target_ref);
2011 err = got_ref_write(symref, repo);
2016 printf("Created reference %s: %s\n",
2017 got_ref_get_name(symref),
2018 got_ref_get_symref_target(symref));
2020 symref_is_locked = 1;
2022 if (strcmp(got_ref_get_symref_target(symref),
2023 got_ref_get_name(target_ref)) == 0)
2026 err = got_ref_change_symref(symref,
2027 got_ref_get_name(target_ref));
2031 err = got_ref_write(symref, repo);
2036 printf("Updated %s: %s\n", got_ref_get_name(symref),
2037 got_ref_get_symref_target(symref));
2041 if (symref_is_locked) {
2042 unlock_err = got_ref_unlock(symref);
2043 if (unlock_err && err == NULL)
2046 got_ref_close(symref);
2053 fprintf(stderr, "usage: %s fetch [-adlqtvX] [-b branch] "
2054 "[-R reference] [-r repository-path] [remote-repository]\n",
2059 static const struct got_error *
2060 delete_missing_ref(struct got_reference *ref,
2061 int verbosity, struct got_repository *repo)
2063 const struct got_error *err = NULL;
2064 struct got_object_id *id = NULL;
2065 char *id_str = NULL;
2067 if (got_ref_is_symbolic(ref)) {
2068 err = got_ref_delete(ref, repo);
2071 if (verbosity >= 0) {
2072 printf("Deleted %s: %s\n",
2073 got_ref_get_name(ref),
2074 got_ref_get_symref_target(ref));
2077 err = got_ref_resolve(&id, repo, ref);
2080 err = got_object_id_str(&id_str, id);
2084 err = got_ref_delete(ref, repo);
2087 if (verbosity >= 0) {
2088 printf("Deleted %s: %s\n",
2089 got_ref_get_name(ref), id_str);
2098 static const struct got_error *
2099 delete_missing_refs(struct got_pathlist_head *their_refs,
2100 struct got_pathlist_head *their_symrefs,
2101 const struct got_remote_repo *remote,
2102 int verbosity, struct got_repository *repo)
2104 const struct got_error *err = NULL, *unlock_err;
2105 struct got_reflist_head my_refs;
2106 struct got_reflist_entry *re;
2107 struct got_pathlist_entry *pe;
2108 char *remote_namespace = NULL;
2109 char *local_refname = NULL;
2111 TAILQ_INIT(&my_refs);
2113 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
2115 return got_error_from_errno("asprintf");
2117 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
2121 TAILQ_FOREACH(re, &my_refs, entry) {
2122 const char *refname = got_ref_get_name(re->ref);
2123 const char *their_refname;
2125 if (remote->mirror_references) {
2126 their_refname = refname;
2128 if (strncmp(refname, remote_namespace,
2129 strlen(remote_namespace)) == 0) {
2130 if (strcmp(refname + strlen(remote_namespace),
2133 if (asprintf(&local_refname, "refs/heads/%s",
2134 refname + strlen(remote_namespace)) == -1) {
2135 err = got_error_from_errno("asprintf");
2138 } else if (strncmp(refname, "refs/tags/", 10) != 0)
2141 their_refname = local_refname;
2144 TAILQ_FOREACH(pe, their_refs, entry) {
2145 if (strcmp(their_refname, pe->path) == 0)
2151 TAILQ_FOREACH(pe, their_symrefs, entry) {
2152 if (strcmp(their_refname, pe->path) == 0)
2158 err = delete_missing_ref(re->ref, verbosity, repo);
2162 if (local_refname) {
2163 struct got_reference *ref;
2164 err = got_ref_open(&ref, repo, local_refname, 1);
2166 if (err->code != GOT_ERR_NOT_REF)
2168 free(local_refname);
2169 local_refname = NULL;
2172 err = delete_missing_ref(ref, verbosity, repo);
2175 unlock_err = got_ref_unlock(ref);
2177 if (unlock_err && err == NULL) {
2182 free(local_refname);
2183 local_refname = NULL;
2187 got_ref_list_free(&my_refs);
2188 free(remote_namespace);
2189 free(local_refname);
2193 static const struct got_error *
2194 update_wanted_ref(const char *refname, struct got_object_id *id,
2195 const char *remote_repo_name, int verbosity, struct got_repository *repo)
2197 const struct got_error *err, *unlock_err;
2198 char *remote_refname;
2199 struct got_reference *ref;
2201 if (strncmp("refs/", refname, 5) == 0)
2204 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2205 remote_repo_name, refname) == -1)
2206 return got_error_from_errno("asprintf");
2208 err = got_ref_open(&ref, repo, remote_refname, 1);
2210 if (err->code != GOT_ERR_NOT_REF)
2212 err = create_ref(remote_refname, id, verbosity, repo);
2214 err = update_ref(ref, id, 0, verbosity, repo);
2215 unlock_err = got_ref_unlock(ref);
2216 if (unlock_err && err == NULL)
2221 free(remote_refname);
2225 static const struct got_error *
2226 delete_ref(struct got_repository *repo, struct got_reference *ref)
2228 const struct got_error *err = NULL;
2229 struct got_object_id *id = NULL;
2230 char *id_str = NULL;
2233 if (got_ref_is_symbolic(ref)) {
2234 target = got_ref_get_symref_target(ref);
2236 err = got_ref_resolve(&id, repo, ref);
2239 err = got_object_id_str(&id_str, id);
2245 err = got_ref_delete(ref, repo);
2249 printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
2256 static const struct got_error *
2257 delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
2259 const struct got_error *err = NULL;
2260 struct got_reflist_head refs;
2261 struct got_reflist_entry *re;
2266 if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
2267 err = got_error_from_errno("asprintf");
2270 err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
2274 TAILQ_FOREACH(re, &refs, entry)
2275 delete_ref(repo, re->ref);
2277 got_ref_list_free(&refs);
2281 static const struct got_error *
2282 cmd_fetch(int argc, char *argv[])
2284 const struct got_error *error = NULL, *unlock_err;
2285 char *cwd = NULL, *repo_path = NULL;
2286 const char *remote_name;
2287 char *proto = NULL, *host = NULL, *port = NULL;
2288 char *repo_name = NULL, *server_path = NULL;
2289 const struct got_remote_repo *remotes;
2290 struct got_remote_repo *remote = NULL;
2292 char *id_str = NULL;
2293 struct got_repository *repo = NULL;
2294 struct got_worktree *worktree = NULL;
2295 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
2296 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
2297 char *head_refname = NULL;
2298 struct got_pathlist_entry *pe;
2299 struct got_reflist_head remote_refs;
2300 struct got_reflist_entry *re;
2301 struct got_object_id *pack_hash = NULL;
2302 int i, ch, fetchfd = -1, fetchstatus;
2303 pid_t fetchpid = -1;
2304 struct got_fetch_progress_arg fpa;
2305 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
2306 int delete_refs = 0, replace_tags = 0, delete_remote = 0;
2307 int *pack_fds = NULL, have_bflag = 0;
2308 const char *remote_head = NULL, *worktree_branch = NULL;
2311 TAILQ_INIT(&symrefs);
2312 TAILQ_INIT(&remote_refs);
2313 TAILQ_INIT(&wanted_branches);
2314 TAILQ_INIT(&wanted_refs);
2316 while ((ch = getopt(argc, argv, "ab:dlqR:r:tvX")) != -1) {
2319 fetch_all_branches = 1;
2322 error = got_pathlist_append(&wanted_branches,
2338 error = got_pathlist_append(&wanted_refs,
2344 repo_path = realpath(optarg, NULL);
2345 if (repo_path == NULL)
2346 return got_error_from_errno2("realpath",
2348 got_path_strip_trailing_slashes(repo_path);
2356 else if (verbosity < 3)
2370 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
2371 option_conflict('a', 'b');
2372 if (list_refs_only) {
2373 if (!TAILQ_EMPTY(&wanted_branches))
2374 option_conflict('l', 'b');
2375 if (fetch_all_branches)
2376 option_conflict('l', 'a');
2378 option_conflict('l', 'd');
2380 option_conflict('l', 'X');
2382 if (delete_remote) {
2383 if (fetch_all_branches)
2384 option_conflict('X', 'a');
2385 if (!TAILQ_EMPTY(&wanted_branches))
2386 option_conflict('X', 'b');
2388 option_conflict('X', 'd');
2390 option_conflict('X', 't');
2391 if (!TAILQ_EMPTY(&wanted_refs))
2392 option_conflict('X', 'R');
2397 errx(1, "-X option requires a remote name");
2398 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
2399 } else if (argc == 1)
2400 remote_name = argv[0];
2404 cwd = getcwd(NULL, 0);
2406 error = got_error_from_errno("getcwd");
2410 error = got_repo_pack_fds_open(&pack_fds);
2414 if (repo_path == NULL) {
2415 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
2416 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2422 strdup(got_worktree_get_repo_path(worktree));
2423 if (repo_path == NULL)
2424 error = got_error_from_errno("strdup");
2428 repo_path = strdup(cwd);
2429 if (repo_path == NULL) {
2430 error = got_error_from_errno("strdup");
2436 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
2440 if (delete_remote) {
2441 error = delete_refs_for_remote(repo, remote_name);
2442 goto done; /* nothing else to do */
2446 worktree_conf = got_worktree_get_gotconfig(worktree);
2447 if (worktree_conf) {
2448 got_gotconfig_get_remotes(&nremotes, &remotes,
2450 for (i = 0; i < nremotes; i++) {
2451 if (strcmp(remotes[i].name, remote_name) == 0) {
2452 error = got_repo_remote_repo_dup(&remote,
2461 if (remote == NULL) {
2462 repo_conf = got_repo_get_gotconfig(repo);
2464 got_gotconfig_get_remotes(&nremotes, &remotes,
2466 for (i = 0; i < nremotes; i++) {
2467 if (strcmp(remotes[i].name, remote_name) == 0) {
2468 error = got_repo_remote_repo_dup(&remote,
2477 if (remote == NULL) {
2478 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2479 for (i = 0; i < nremotes; i++) {
2480 if (strcmp(remotes[i].name, remote_name) == 0) {
2481 error = got_repo_remote_repo_dup(&remote,
2489 if (remote == NULL) {
2490 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2494 if (TAILQ_EMPTY(&wanted_branches)) {
2495 if (!fetch_all_branches)
2496 fetch_all_branches = remote->fetch_all_branches;
2497 for (i = 0; i < remote->nfetch_branches; i++) {
2498 error = got_pathlist_append(&wanted_branches,
2499 remote->fetch_branches[i], NULL);
2504 if (TAILQ_EMPTY(&wanted_refs)) {
2505 for (i = 0; i < remote->nfetch_refs; i++) {
2506 error = got_pathlist_append(&wanted_refs,
2507 remote->fetch_refs[i], NULL);
2513 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
2514 &repo_name, remote->fetch_url);
2518 if (strcmp(proto, "git") == 0) {
2520 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2521 "sendfd dns inet unveil", NULL) == -1)
2524 } else if (strcmp(proto, "git+ssh") == 0 ||
2525 strcmp(proto, "ssh") == 0) {
2527 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2528 "sendfd unveil", NULL) == -1)
2531 } else if (strcmp(proto, "http") == 0 ||
2532 strcmp(proto, "git+http") == 0) {
2533 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2536 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2540 error = got_dial_apply_unveil(proto);
2544 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2549 head_refname = strdup(got_worktree_get_head_ref_name(worktree));
2550 if (head_refname == NULL) {
2551 error = got_error_from_errno("strdup");
2555 /* Release work tree lock. */
2556 got_worktree_close(worktree);
2560 if (verbosity >= 0) {
2561 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
2562 remote->name, proto, host,
2563 port ? ":" : "", port ? port : "",
2564 *server_path == '/' ? "" : "/", server_path);
2567 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2568 server_path, verbosity);
2574 * If set, get this remote's HEAD ref target so
2575 * if it has changed on the server we can fetch it.
2577 error = got_ref_list(&remote_refs, repo, "refs/remotes",
2578 got_ref_cmp_by_name, repo);
2582 TAILQ_FOREACH(re, &remote_refs, entry) {
2583 const char *remote_refname, *remote_target;
2584 size_t remote_name_len;
2586 if (!got_ref_is_symbolic(re->ref))
2589 remote_name_len = strlen(remote->name);
2590 remote_refname = got_ref_get_name(re->ref);
2592 /* we only want refs/remotes/$remote->name/HEAD */
2593 if (strncmp(remote_refname + 13, remote->name,
2594 remote_name_len) != 0)
2597 if (strcmp(remote_refname + remote_name_len + 14,
2602 * Take the name itself because we already
2603 * only match with refs/heads/ in fetch_pack().
2605 remote_target = got_ref_get_symref_target(re->ref);
2606 remote_head = remote_target + remote_name_len + 14;
2611 strncmp(head_refname, "refs/heads/", 11) == 0)
2612 worktree_branch = head_refname;
2615 fpa.last_scaled_size[0] = '\0';
2616 fpa.last_p_indexed = -1;
2617 fpa.last_p_resolved = -1;
2618 fpa.verbosity = verbosity;
2620 fpa.create_configs = 0;
2621 fpa.configs_created = 0;
2622 memset(&fpa.config_info, 0, sizeof(fpa.config_info));
2624 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2625 remote->mirror_references, fetch_all_branches, &wanted_branches,
2626 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2627 worktree_branch, remote_head, have_bflag, fetch_progress, &fpa);
2631 if (list_refs_only) {
2632 error = list_remote_refs(&symrefs, &refs);
2636 if (pack_hash == NULL) {
2638 printf("Already up-to-date\n");
2639 } else if (verbosity >= 0) {
2640 error = got_object_id_str(&id_str, pack_hash);
2643 printf("\nFetched %s.pack\n", id_str);
2648 /* Update references provided with the pack file. */
2649 TAILQ_FOREACH(pe, &refs, entry) {
2650 const char *refname = pe->path;
2651 struct got_object_id *id = pe->data;
2652 struct got_reference *ref;
2653 char *remote_refname;
2655 if (is_wanted_ref(&wanted_refs, refname) &&
2656 !remote->mirror_references) {
2657 error = update_wanted_ref(refname, id,
2658 remote->name, verbosity, repo);
2664 if (remote->mirror_references ||
2665 strncmp("refs/tags/", refname, 10) == 0) {
2666 error = got_ref_open(&ref, repo, refname, 1);
2668 if (error->code != GOT_ERR_NOT_REF)
2670 error = create_ref(refname, id, verbosity,
2675 error = update_ref(ref, id, replace_tags,
2677 unlock_err = got_ref_unlock(ref);
2678 if (unlock_err && error == NULL)
2684 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2685 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2686 remote_name, refname + 11) == -1) {
2687 error = got_error_from_errno("asprintf");
2691 error = got_ref_open(&ref, repo, remote_refname, 1);
2693 if (error->code != GOT_ERR_NOT_REF)
2695 error = create_ref(remote_refname, id,
2700 error = update_ref(ref, id, replace_tags,
2702 unlock_err = got_ref_unlock(ref);
2703 if (unlock_err && error == NULL)
2710 /* Also create a local branch if none exists yet. */
2711 error = got_ref_open(&ref, repo, refname, 1);
2713 if (error->code != GOT_ERR_NOT_REF)
2715 error = create_ref(refname, id, verbosity,
2720 unlock_err = got_ref_unlock(ref);
2721 if (unlock_err && error == NULL)
2728 error = delete_missing_refs(&refs, &symrefs, remote,
2734 if (!remote->mirror_references) {
2735 /* Update remote HEAD reference if the server provided one. */
2736 TAILQ_FOREACH(pe, &symrefs, entry) {
2737 struct got_reference *target_ref;
2738 const char *refname = pe->path;
2739 const char *target = pe->data;
2740 char *remote_refname = NULL, *remote_target = NULL;
2742 if (strcmp(refname, GOT_REF_HEAD) != 0)
2745 if (strncmp("refs/heads/", target, 11) != 0)
2748 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2749 remote->name, refname) == -1) {
2750 error = got_error_from_errno("asprintf");
2753 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2754 remote->name, target + 11) == -1) {
2755 error = got_error_from_errno("asprintf");
2756 free(remote_refname);
2760 error = got_ref_open(&target_ref, repo, remote_target,
2763 free(remote_refname);
2764 free(remote_target);
2765 if (error->code == GOT_ERR_NOT_REF) {
2771 error = update_symref(remote_refname, target_ref,
2773 free(remote_refname);
2774 free(remote_target);
2775 got_ref_close(target_ref);
2782 if (kill(fetchpid, SIGTERM) == -1)
2783 error = got_error_from_errno("kill");
2784 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2785 error = got_error_from_errno("waitpid");
2787 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2788 error = got_error_from_errno("close");
2790 const struct got_error *close_err = got_repo_close(repo);
2795 got_worktree_close(worktree);
2797 const struct got_error *pack_err =
2798 got_repo_pack_fds_close(pack_fds);
2802 got_pathlist_free(&refs, GOT_PATHLIST_FREE_ALL);
2803 got_pathlist_free(&symrefs, GOT_PATHLIST_FREE_ALL);
2804 got_pathlist_free(&wanted_branches, GOT_PATHLIST_FREE_NONE);
2805 got_pathlist_free(&wanted_refs, GOT_PATHLIST_FREE_NONE);
2806 got_ref_list_free(&remote_refs);
2807 got_repo_free_remote_repo_data(remote);
2824 usage_checkout(void)
2826 fprintf(stderr, "usage: %s checkout [-Eq] [-b branch] [-c commit] "
2827 "[-p path-prefix] repository-path [work-tree-path]\n",
2833 show_worktree_base_ref_warning(void)
2835 fprintf(stderr, "%s: warning: could not create a reference "
2836 "to the work tree's base commit; the commit could be "
2837 "garbage-collected by Git or 'gotadmin cleanup'; making the "
2838 "repository writable and running 'got update' will prevent this\n",
2842 struct got_checkout_progress_arg {
2843 const char *worktree_path;
2844 int had_base_commit_ref_error;
2848 static const struct got_error *
2849 checkout_progress(void *arg, unsigned char status, const char *path)
2851 struct got_checkout_progress_arg *a = arg;
2853 /* Base commit bump happens silently. */
2854 if (status == GOT_STATUS_BUMP_BASE)
2857 if (status == GOT_STATUS_BASE_REF_ERR) {
2858 a->had_base_commit_ref_error = 1;
2862 while (path[0] == '/')
2865 if (a->verbosity >= 0)
2866 printf("%c %s/%s\n", status, a->worktree_path, path);
2871 static const struct got_error *
2872 check_cancelled(void *arg)
2874 if (sigint_received || sigpipe_received)
2875 return got_error(GOT_ERR_CANCELLED);
2879 static const struct got_error *
2880 check_linear_ancestry(struct got_object_id *commit_id,
2881 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2882 struct got_repository *repo)
2884 const struct got_error *err = NULL;
2885 struct got_object_id *yca_id;
2887 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2888 commit_id, base_commit_id, 1, 0, repo, check_cancelled, NULL);
2893 return got_error(GOT_ERR_ANCESTRY);
2896 * Require a straight line of history between the target commit
2897 * and the work tree's base commit.
2899 * Non-linear situations such as this require a rebase:
2901 * (commit) D F (base_commit)
2909 * 'got update' only handles linear cases:
2910 * Update forwards in time: A (base/yca) - B - C - D (commit)
2911 * Update backwards in time: D (base) - C - B - A (commit/yca)
2913 if (allow_forwards_in_time_only) {
2914 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2915 return got_error(GOT_ERR_ANCESTRY);
2916 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2917 got_object_id_cmp(base_commit_id, yca_id) != 0)
2918 return got_error(GOT_ERR_ANCESTRY);
2924 static const struct got_error *
2925 check_same_branch(struct got_object_id *commit_id,
2926 struct got_reference *head_ref, struct got_repository *repo)
2928 const struct got_error *err = NULL;
2929 struct got_commit_graph *graph = NULL;
2930 struct got_object_id *head_commit_id = NULL;
2932 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2936 if (got_object_id_cmp(head_commit_id, commit_id) == 0)
2939 err = got_commit_graph_open(&graph, "/", 1);
2943 err = got_commit_graph_bfsort(graph, head_commit_id, repo,
2944 check_cancelled, NULL);
2949 struct got_object_id id;
2951 err = got_commit_graph_iter_next(&id, graph, repo,
2952 check_cancelled, NULL);
2954 if (err->code == GOT_ERR_ITER_COMPLETED)
2955 err = got_error(GOT_ERR_ANCESTRY);
2959 if (got_object_id_cmp(&id, commit_id) == 0)
2964 got_commit_graph_close(graph);
2965 free(head_commit_id);
2969 static const struct got_error *
2970 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2972 static char msg[512];
2973 const char *branch_name;
2975 if (got_ref_is_symbolic(ref))
2976 branch_name = got_ref_get_symref_target(ref);
2978 branch_name = got_ref_get_name(ref);
2980 if (strncmp("refs/heads/", branch_name, 11) == 0)
2983 snprintf(msg, sizeof(msg),
2984 "target commit is not contained in branch '%s'; "
2985 "the branch to use must be specified with -b; "
2986 "if necessary a new branch can be created for "
2987 "this commit with 'got branch -c %s BRANCH_NAME'",
2988 branch_name, commit_id_str);
2990 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2993 static const struct got_error *
2994 cmd_checkout(int argc, char *argv[])
2996 const struct got_error *close_err, *error = NULL;
2997 struct got_repository *repo = NULL;
2998 struct got_reference *head_ref = NULL, *ref = NULL;
2999 struct got_worktree *worktree = NULL;
3000 char *repo_path = NULL;
3001 char *worktree_path = NULL;
3002 const char *path_prefix = "";
3003 const char *branch_name = GOT_REF_HEAD, *refname = NULL;
3004 char *commit_id_str = NULL, *keyword_idstr = NULL;
3005 struct got_object_id *commit_id = NULL;
3007 int ch, same_path_prefix, allow_nonempty = 0, verbosity = 0;
3008 struct got_pathlist_head paths;
3009 struct got_checkout_progress_arg cpa;
3010 int *pack_fds = NULL;
3015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3016 "unveil", NULL) == -1)
3020 while ((ch = getopt(argc, argv, "b:c:Ep:q")) != -1) {
3023 branch_name = optarg;
3026 commit_id_str = strdup(optarg);
3027 if (commit_id_str == NULL)
3028 return got_error_from_errno("strdup");
3034 path_prefix = optarg;
3049 char *base, *dotgit;
3051 repo_path = realpath(argv[0], NULL);
3052 if (repo_path == NULL)
3053 return got_error_from_errno2("realpath", argv[0]);
3054 cwd = getcwd(NULL, 0);
3056 error = got_error_from_errno("getcwd");
3063 error = got_path_basename(&base, path);
3066 dotgit = strstr(base, ".git");
3069 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
3070 error = got_error_from_errno("asprintf");
3075 } else if (argc == 2) {
3076 repo_path = realpath(argv[0], NULL);
3077 if (repo_path == NULL) {
3078 error = got_error_from_errno2("realpath", argv[0]);
3081 worktree_path = realpath(argv[1], NULL);
3082 if (worktree_path == NULL) {
3083 if (errno != ENOENT) {
3084 error = got_error_from_errno2("realpath",
3088 worktree_path = strdup(argv[1]);
3089 if (worktree_path == NULL) {
3090 error = got_error_from_errno("strdup");
3097 got_path_strip_trailing_slashes(repo_path);
3098 got_path_strip_trailing_slashes(worktree_path);
3100 if (got_path_is_child(worktree_path, repo_path, strlen(repo_path)) ||
3101 got_path_is_child(repo_path, worktree_path,
3102 strlen(worktree_path))) {
3103 error = got_error_fmt(GOT_ERR_BAD_PATH,
3104 "work tree and repository paths may not overlap: %s",
3109 error = got_repo_pack_fds_open(&pack_fds);
3113 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
3117 /* Pre-create work tree path for unveil(2) */
3118 error = got_path_mkdir(worktree_path);
3120 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
3121 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3123 if (!allow_nonempty &&
3124 !got_path_dir_is_empty(worktree_path)) {
3125 error = got_error_path(worktree_path,
3126 GOT_ERR_DIR_NOT_EMPTY);
3131 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
3135 error = got_ref_open(&head_ref, repo, branch_name, 0);
3139 error = got_worktree_init(worktree_path, head_ref, path_prefix,
3140 GOT_WORKTREE_GOT_DIR, repo);
3141 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
3144 error = got_worktree_open(&worktree, worktree_path,
3145 GOT_WORKTREE_GOT_DIR);
3149 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
3153 if (!same_path_prefix) {
3154 error = got_error(GOT_ERR_PATH_PREFIX);
3158 if (commit_id_str) {
3159 struct got_reflist_head refs;
3161 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3166 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3170 if (keyword_idstr != NULL) {
3171 free(commit_id_str);
3172 commit_id_str = keyword_idstr;
3175 error = got_repo_match_object_id(&commit_id, NULL,
3176 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3177 got_ref_list_free(&refs);
3180 error = check_linear_ancestry(commit_id,
3181 got_worktree_get_base_commit_id(worktree), 0, repo);
3182 if (error != NULL) {
3183 if (error->code == GOT_ERR_ANCESTRY) {
3184 error = checkout_ancestry_error(
3185 head_ref, commit_id_str);
3189 error = check_same_branch(commit_id, head_ref, repo);
3191 if (error->code == GOT_ERR_ANCESTRY) {
3192 error = checkout_ancestry_error(
3193 head_ref, commit_id_str);
3197 error = got_worktree_set_base_commit_id(worktree, repo,
3201 /* Expand potentially abbreviated commit ID string. */
3202 free(commit_id_str);
3203 error = got_object_id_str(&commit_id_str, commit_id);
3207 commit_id = got_object_id_dup(
3208 got_worktree_get_base_commit_id(worktree));
3209 if (commit_id == NULL) {
3210 error = got_error_from_errno("got_object_id_dup");
3213 error = got_object_id_str(&commit_id_str, commit_id);
3218 error = got_pathlist_append(&paths, "", NULL);
3221 cpa.worktree_path = worktree_path;
3222 cpa.had_base_commit_ref_error = 0;
3223 cpa.verbosity = verbosity;
3224 error = got_worktree_checkout_files(worktree, &paths, repo,
3225 checkout_progress, &cpa, check_cancelled, NULL);
3229 if (got_ref_is_symbolic(head_ref)) {
3230 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
3233 refname = got_ref_get_name(ref);
3235 refname = got_ref_get_name(head_ref);
3236 printf("Checked out %s: %s\n", refname, commit_id_str);
3237 printf("Now shut up and hack\n");
3238 if (cpa.had_base_commit_ref_error)
3239 show_worktree_base_ref_warning();
3242 const struct got_error *pack_err =
3243 got_repo_pack_fds_close(pack_fds);
3248 got_ref_close(head_ref);
3252 close_err = got_repo_close(repo);
3256 if (worktree != NULL) {
3257 close_err = got_worktree_close(worktree);
3261 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
3262 free(commit_id_str);
3265 free(worktree_path);
3270 struct got_update_progress_arg {
3282 print_update_progress_stats(struct got_update_progress_arg *upa)
3284 if (!upa->did_something)
3287 if (upa->conflicts > 0)
3288 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3289 if (upa->obstructed > 0)
3290 printf("File paths obstructed by a non-regular file: %d\n",
3292 if (upa->not_updated > 0)
3293 printf("Files not updated because of existing merge "
3294 "conflicts: %d\n", upa->not_updated);
3298 * The meaning of some status codes differs between merge-style operations and
3299 * update operations. For example, the ! status code means "file was missing"
3300 * if changes were merged into the work tree, and "missing file was restored"
3301 * if the work tree was updated. This function should be used by any operation
3302 * which merges changes into the work tree without updating the work tree.
3305 print_merge_progress_stats(struct got_update_progress_arg *upa)
3307 if (!upa->did_something)
3310 if (upa->conflicts > 0)
3311 printf("Files with new merge conflicts: %d\n", upa->conflicts);
3312 if (upa->obstructed > 0)
3313 printf("File paths obstructed by a non-regular file: %d\n",
3315 if (upa->missing > 0)
3316 printf("Files which had incoming changes but could not be "
3317 "found in the work tree: %d\n", upa->missing);
3318 if (upa->not_deleted > 0)
3319 printf("Files not deleted due to differences in deleted "
3320 "content: %d\n", upa->not_deleted);
3321 if (upa->unversioned > 0)
3322 printf("Files not merged because an unversioned file was "
3323 "found in the work tree: %d\n", upa->unversioned);
3329 fprintf(stderr, "usage: %s update [-q] [-b branch] [-c commit] "
3330 "[path ...]\n", getprogname());
3334 static const struct got_error *
3335 update_progress(void *arg, unsigned char status, const char *path)
3337 struct got_update_progress_arg *upa = arg;
3339 if (status == GOT_STATUS_EXISTS ||
3340 status == GOT_STATUS_BASE_REF_ERR)
3343 upa->did_something = 1;
3345 /* Base commit bump happens silently. */
3346 if (status == GOT_STATUS_BUMP_BASE)
3349 if (status == GOT_STATUS_CONFLICT)
3351 if (status == GOT_STATUS_OBSTRUCTED)
3353 if (status == GOT_STATUS_CANNOT_UPDATE)
3355 if (status == GOT_STATUS_MISSING)
3357 if (status == GOT_STATUS_CANNOT_DELETE)
3359 if (status == GOT_STATUS_UNVERSIONED)
3362 while (path[0] == '/')
3364 if (upa->verbosity >= 0)
3365 printf("%c %s\n", status, path);
3370 static const struct got_error *
3371 switch_head_ref(struct got_reference *head_ref,
3372 struct got_object_id *commit_id, struct got_worktree *worktree,
3373 struct got_repository *repo)
3375 const struct got_error *err = NULL;
3377 int ref_has_moved = 0;
3379 /* Trivial case: switching between two different references. */
3380 if (strcmp(got_ref_get_name(head_ref),
3381 got_worktree_get_head_ref_name(worktree)) != 0) {
3382 printf("Switching work tree from %s to %s\n",
3383 got_worktree_get_head_ref_name(worktree),
3384 got_ref_get_name(head_ref));
3385 return got_worktree_set_head_ref(worktree, head_ref);
3388 err = check_linear_ancestry(commit_id,
3389 got_worktree_get_base_commit_id(worktree), 0, repo);
3391 if (err->code != GOT_ERR_ANCESTRY)
3398 /* Switching to a rebased branch with the same reference name. */
3399 err = got_object_id_str(&base_id_str,
3400 got_worktree_get_base_commit_id(worktree));
3403 printf("Reference %s now points at a different branch\n",
3404 got_worktree_get_head_ref_name(worktree));
3405 printf("Switching work tree from %s to %s\n", base_id_str,
3406 got_worktree_get_head_ref_name(worktree));
3410 static const struct got_error *
3411 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
3413 const struct got_error *err;
3416 err = got_worktree_rebase_in_progress(&in_progress, worktree);
3420 return got_error(GOT_ERR_REBASING);
3422 err = got_worktree_histedit_in_progress(&in_progress, worktree);
3426 return got_error(GOT_ERR_HISTEDIT_BUSY);
3431 static const struct got_error *
3432 check_merge_in_progress(struct got_worktree *worktree,
3433 struct got_repository *repo)
3435 const struct got_error *err;
3438 err = got_worktree_merge_in_progress(&in_progress, worktree, repo);
3442 return got_error(GOT_ERR_MERGE_BUSY);
3447 static const struct got_error *
3448 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
3449 char *argv[], struct got_worktree *worktree)
3451 const struct got_error *err = NULL;
3453 struct got_pathlist_entry *new;
3459 return got_error_from_errno("strdup");
3460 return got_pathlist_append(paths, path, NULL);
3463 for (i = 0; i < argc; i++) {
3464 err = got_worktree_resolve_path(&path, worktree, argv[i]);
3467 err = got_pathlist_insert(&new, paths, path, NULL);
3468 if (err || new == NULL /* duplicate */) {
3478 static const struct got_error *
3479 wrap_not_worktree_error(const struct got_error *orig_err,
3480 const char *cmdname, const char *path)
3482 const struct got_error *err;
3483 struct got_repository *repo;
3484 static char msg[512];
3485 int *pack_fds = NULL;
3487 err = got_repo_pack_fds_open(&pack_fds);
3491 err = got_repo_open(&repo, path, NULL, pack_fds);
3495 snprintf(msg, sizeof(msg),
3496 "'got %s' needs a work tree in addition to a git repository\n"
3497 "Work trees can be checked out from this Git repository with "
3499 "The got(1) manual page contains more information.", cmdname);
3500 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
3502 const struct got_error *close_err = got_repo_close(repo);
3507 const struct got_error *pack_err =
3508 got_repo_pack_fds_close(pack_fds);
3515 static const struct got_error *
3516 cmd_update(int argc, char *argv[])
3518 const struct got_error *close_err, *error = NULL;
3519 struct got_repository *repo = NULL;
3520 struct got_worktree *worktree = NULL;
3521 char *worktree_path = NULL;
3522 struct got_object_id *commit_id = NULL;
3523 char *commit_id_str = NULL;
3524 const char *branch_name = NULL;
3525 struct got_reference *head_ref = NULL;
3526 struct got_pathlist_head paths;
3527 struct got_pathlist_entry *pe;
3528 int ch, verbosity = 0;
3529 struct got_update_progress_arg upa;
3530 int *pack_fds = NULL;
3535 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3536 "unveil", NULL) == -1)
3540 while ((ch = getopt(argc, argv, "b:c:q")) != -1) {
3543 branch_name = optarg;
3546 commit_id_str = strdup(optarg);
3547 if (commit_id_str == NULL)
3548 return got_error_from_errno("strdup");
3562 worktree_path = getcwd(NULL, 0);
3563 if (worktree_path == NULL) {
3564 error = got_error_from_errno("getcwd");
3568 error = got_repo_pack_fds_open(&pack_fds);
3572 error = got_worktree_open(&worktree, worktree_path,
3573 GOT_WORKTREE_GOT_DIR);
3575 if (error->code == GOT_ERR_NOT_WORKTREE)
3576 error = wrap_not_worktree_error(error, "update",
3581 error = check_rebase_or_histedit_in_progress(worktree);
3585 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3590 error = apply_unveil(got_repo_get_path(repo), 0,
3591 got_worktree_get_root_path(worktree));
3595 error = check_merge_in_progress(worktree, repo);
3599 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3603 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
3604 got_worktree_get_head_ref_name(worktree), 0);
3607 if (commit_id_str == NULL) {
3608 error = got_ref_resolve(&commit_id, repo, head_ref);
3611 error = got_object_id_str(&commit_id_str, commit_id);
3615 struct got_reflist_head refs;
3616 char *keyword_idstr = NULL;
3620 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
3625 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
3629 if (keyword_idstr != NULL) {
3630 free(commit_id_str);
3631 commit_id_str = keyword_idstr;
3634 error = got_repo_match_object_id(&commit_id, NULL,
3635 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
3636 got_ref_list_free(&refs);
3637 free(commit_id_str);
3638 commit_id_str = NULL;
3641 error = got_object_id_str(&commit_id_str, commit_id);
3647 struct got_object_id *head_commit_id;
3648 TAILQ_FOREACH(pe, &paths, entry) {
3649 if (pe->path_len == 0)
3651 error = got_error_msg(GOT_ERR_BAD_PATH,
3652 "switching between branches requires that "
3653 "the entire work tree gets updated");
3656 error = got_ref_resolve(&head_commit_id, repo, head_ref);
3659 error = check_linear_ancestry(commit_id, head_commit_id, 0,
3661 free(head_commit_id);
3664 error = check_same_branch(commit_id, head_ref, repo);
3667 error = switch_head_ref(head_ref, commit_id, worktree, repo);
3671 error = check_linear_ancestry(commit_id,
3672 got_worktree_get_base_commit_id(worktree), 0, repo);
3673 if (error != NULL) {
3674 if (error->code == GOT_ERR_ANCESTRY)
3675 error = got_error(GOT_ERR_BRANCH_MOVED);
3678 error = check_same_branch(commit_id, head_ref, repo);
3683 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
3685 error = got_worktree_set_base_commit_id(worktree, repo,
3691 memset(&upa, 0, sizeof(upa));
3692 upa.verbosity = verbosity;
3693 error = got_worktree_checkout_files(worktree, &paths, repo,
3694 update_progress, &upa, check_cancelled, NULL);
3698 if (upa.did_something) {
3699 printf("Updated to %s: %s\n",
3700 got_worktree_get_head_ref_name(worktree), commit_id_str);
3702 printf("Already up-to-date\n");
3704 print_update_progress_stats(&upa);
3707 const struct got_error *pack_err =
3708 got_repo_pack_fds_close(pack_fds);
3713 close_err = got_repo_close(repo);
3717 if (worktree != NULL) {
3718 close_err = got_worktree_close(worktree);
3722 if (head_ref != NULL)
3723 got_ref_close(head_ref);
3724 free(worktree_path);
3725 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
3727 free(commit_id_str);
3731 static const struct got_error *
3732 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
3733 const char *path, int diff_context, int ignore_whitespace,
3734 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3735 struct got_repository *repo, FILE *outfile)
3737 const struct got_error *err = NULL;
3738 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
3739 FILE *f1 = NULL, *f2 = NULL;
3740 int fd1 = -1, fd2 = -1;
3742 fd1 = got_opentempfd();
3744 return got_error_from_errno("got_opentempfd");
3745 fd2 = got_opentempfd();
3747 err = got_error_from_errno("got_opentempfd");
3752 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192,
3758 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192, fd2);
3762 f1 = got_opentemp();
3764 err = got_error_from_errno("got_opentemp");
3767 f2 = got_opentemp();
3769 err = got_error_from_errno("got_opentemp");
3773 while (path[0] == '/')
3775 err = got_diff_blob(NULL, NULL, blob1, blob2, f1, f2, path, path,
3776 GOT_DIFF_ALGORITHM_PATIENCE, diff_context, ignore_whitespace,
3777 force_text_diff, dsa, outfile);
3779 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3780 err = got_error_from_errno("close");
3782 got_object_blob_close(blob1);
3783 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3784 err = got_error_from_errno("close");
3786 got_object_blob_close(blob2);
3787 if (f1 && fclose(f1) == EOF && err == NULL)
3788 err = got_error_from_errno("fclose");
3789 if (f2 && fclose(f2) == EOF && err == NULL)
3790 err = got_error_from_errno("fclose");
3794 static const struct got_error *
3795 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3796 const char *path, int diff_context, int ignore_whitespace,
3797 int force_text_diff, struct got_diffstat_cb_arg *dsa,
3798 struct got_repository *repo, FILE *outfile)
3800 const struct got_error *err = NULL;
3801 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3802 struct got_diff_blob_output_unidiff_arg arg;
3803 FILE *f1 = NULL, *f2 = NULL;
3804 int fd1 = -1, fd2 = -1;
3807 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3810 fd1 = got_opentempfd();
3812 err = got_error_from_errno("got_opentempfd");
3817 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3821 f1 = got_opentemp();
3823 err = got_error_from_errno("got_opentemp");
3827 f2 = got_opentemp();
3829 err = got_error_from_errno("got_opentemp");
3832 fd2 = got_opentempfd();
3834 err = got_error_from_errno("got_opentempfd");
3837 arg.diff_context = diff_context;
3838 arg.ignore_whitespace = ignore_whitespace;
3839 arg.force_text_diff = force_text_diff;
3841 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
3842 arg.outfile = outfile;
3845 while (path[0] == '/')
3847 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, path, path, repo,
3848 got_diff_blob_output_unidiff, &arg, 1);
3851 got_object_tree_close(tree1);
3853 got_object_tree_close(tree2);
3854 if (f1 && fclose(f1) == EOF && err == NULL)
3855 err = got_error_from_errno("fclose");
3856 if (f2 && fclose(f2) == EOF && err == NULL)
3857 err = got_error_from_errno("fclose");
3858 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3859 err = got_error_from_errno("close");
3860 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3861 err = got_error_from_errno("close");
3865 static const struct got_error *
3866 get_changed_paths(struct got_pathlist_head *paths,
3867 struct got_commit_object *commit, struct got_repository *repo,
3868 struct got_diffstat_cb_arg *dsa)
3870 const struct got_error *err = NULL;
3871 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3872 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3873 struct got_object_qid *qid;
3874 got_diff_blob_cb cb = got_diff_tree_collect_changed_paths;
3875 FILE *f1 = NULL, *f2 = NULL;
3876 int fd1 = -1, fd2 = -1;
3879 cb = got_diff_tree_compute_diffstat;
3881 f1 = got_opentemp();
3883 err = got_error_from_errno("got_opentemp");
3886 f2 = got_opentemp();
3888 err = got_error_from_errno("got_opentemp");
3891 fd1 = got_opentempfd();
3893 err = got_error_from_errno("got_opentempfd");
3896 fd2 = got_opentempfd();
3898 err = got_error_from_errno("got_opentempfd");
3903 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3905 struct got_commit_object *pcommit;
3906 err = got_object_open_as_commit(&pcommit, repo,
3911 tree_id1 = got_object_id_dup(
3912 got_object_commit_get_tree_id(pcommit));
3913 if (tree_id1 == NULL) {
3914 got_object_commit_close(pcommit);
3915 return got_error_from_errno("got_object_id_dup");
3917 got_object_commit_close(pcommit);
3922 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3927 tree_id2 = got_object_commit_get_tree_id(commit);
3928 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3932 err = got_diff_tree(tree1, tree2, f1, f2, fd1, fd2, "", "", repo,
3933 cb, dsa ? (void *)dsa : paths, dsa ? 1 : 0);
3936 got_object_tree_close(tree1);
3938 got_object_tree_close(tree2);
3939 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
3940 err = got_error_from_errno("close");
3941 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
3942 err = got_error_from_errno("close");
3943 if (f1 && fclose(f1) == EOF && err == NULL)
3944 err = got_error_from_errno("fclose");
3945 if (f2 && fclose(f2) == EOF && err == NULL)
3946 err = got_error_from_errno("fclose");
3951 static const struct got_error *
3952 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3953 const char *path, int diff_context, struct got_diffstat_cb_arg *dsa,
3954 struct got_repository *repo, FILE *outfile)
3956 const struct got_error *err = NULL;
3957 struct got_commit_object *pcommit = NULL;
3958 char *id_str1 = NULL, *id_str2 = NULL;
3959 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3960 struct got_object_qid *qid;
3962 qid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
3964 err = got_object_open_as_commit(&pcommit, repo,
3968 err = got_object_id_str(&id_str1, &qid->id);
3973 err = got_object_id_str(&id_str2, id);
3977 if (path && path[0] != '\0') {
3979 err = got_object_id_by_path(&obj_id2, repo, commit, path);
3983 err = got_object_id_by_path(&obj_id1, repo,
3986 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3992 err = got_object_get_type(&obj_type, repo, obj_id2);
3998 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3999 fprintf(outfile, "commit - %s\n",
4000 id_str1 ? id_str1 : "/dev/null");
4001 fprintf(outfile, "commit + %s\n", id_str2);
4003 case GOT_OBJ_TYPE_BLOB:
4004 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
4005 0, 0, dsa, repo, outfile);
4007 case GOT_OBJ_TYPE_TREE:
4008 err = diff_trees(obj_id1, obj_id2, path, diff_context,
4009 0, 0, dsa, repo, outfile);
4012 err = got_error(GOT_ERR_OBJ_TYPE);
4018 obj_id2 = got_object_commit_get_tree_id(commit);
4020 obj_id1 = got_object_commit_get_tree_id(pcommit);
4022 "diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
4023 fprintf(outfile, "commit - %s\n",
4024 id_str1 ? id_str1 : "/dev/null");
4025 fprintf(outfile, "commit + %s\n", id_str2);
4026 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, 0,
4027 dsa, repo, outfile);
4033 got_object_commit_close(pcommit);
4038 get_datestr(time_t *time, char *datebuf)
4040 struct tm mytm, *tm;
4043 tm = gmtime_r(time, &mytm);
4046 s = asctime_r(tm, datebuf);
4049 p = strchr(s, '\n');
4055 static const struct got_error *
4056 match_commit(int *have_match, struct got_object_id *id,
4057 struct got_commit_object *commit, regex_t *regex)
4059 const struct got_error *err = NULL;
4060 regmatch_t regmatch;
4061 char *id_str = NULL, *logmsg = NULL;
4065 err = got_object_id_str(&id_str, id);
4069 err = got_object_commit_get_logmsg(&logmsg, commit);
4073 if (regexec(regex, got_object_commit_get_author(commit), 1,
4074 ®match, 0) == 0 ||
4075 regexec(regex, got_object_commit_get_committer(commit), 1,
4076 ®match, 0) == 0 ||
4077 regexec(regex, id_str, 1, ®match, 0) == 0 ||
4078 regexec(regex, logmsg, 1, ®match, 0) == 0)
4087 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
4090 regmatch_t regmatch;
4091 struct got_pathlist_entry *pe;
4095 TAILQ_FOREACH(pe, changed_paths, entry) {
4096 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
4103 static const struct got_error *
4104 match_patch(int *have_match, struct got_commit_object *commit,
4105 struct got_object_id *id, const char *path, int diff_context,
4106 struct got_repository *repo, regex_t *regex, FILE *f)
4108 const struct got_error *err = NULL;
4110 size_t linesize = 0;
4111 regmatch_t regmatch;
4115 err = got_opentemp_truncate(f);
4119 err = print_patch(commit, id, path, diff_context, NULL, repo, f);
4123 if (fseeko(f, 0L, SEEK_SET) == -1) {
4124 err = got_error_from_errno("fseeko");
4128 while (getline(&line, &linesize, f) != -1) {
4129 if (regexec(regex, line, 1, ®match, 0) == 0) {
4139 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
4141 static const struct got_error*
4142 build_refs_str(char **refs_str, struct got_reflist_head *refs,
4143 struct got_object_id *id, struct got_repository *repo,
4146 static const struct got_error *err = NULL;
4147 struct got_reflist_entry *re;
4153 TAILQ_FOREACH(re, refs, entry) {
4154 struct got_tag_object *tag = NULL;
4155 struct got_object_id *ref_id;
4158 name = got_ref_get_name(re->ref);
4159 if (strcmp(name, GOT_REF_HEAD) == 0)
4161 if (strncmp(name, "refs/", 5) == 0)
4163 if (strncmp(name, "got/", 4) == 0)
4165 if (strncmp(name, "heads/", 6) == 0)
4167 if (strncmp(name, "remotes/", 8) == 0) {
4171 s = strstr(name, "/" GOT_REF_HEAD);
4172 if (s != NULL && strcmp(s, "/" GOT_REF_HEAD) == 0)
4175 err = got_ref_resolve(&ref_id, repo, re->ref);
4178 if (strncmp(name, "tags/", 5) == 0) {
4179 err = got_object_open_as_tag(&tag, repo, ref_id);
4181 if (err->code != GOT_ERR_OBJ_TYPE) {
4185 /* Ref points at something other than a tag. */
4190 cmp = got_object_id_cmp(tag ?
4191 got_object_tag_get_object_id(tag) : ref_id, id);
4194 got_object_tag_close(tag);
4198 if (asprintf(refs_str, "%s%s%s", s ? s : "",
4199 s ? ", " : "", name) == -1) {
4200 err = got_error_from_errno("asprintf");
4211 static const struct got_error *
4212 print_commit_oneline(struct got_commit_object *commit, struct got_object_id *id,
4213 struct got_repository *repo, struct got_reflist_object_id_map *refs_idmap)
4215 const struct got_error *err = NULL;
4216 char *ref_str = NULL, *id_str = NULL, *logmsg0 = NULL;
4217 char *comma, *s, *nl;
4218 struct got_reflist_head *refs;
4219 char datebuf[12]; /* YYYY-MM-DD + SPACE + NUL */
4221 time_t committer_time;
4223 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4225 err = build_refs_str(&ref_str, refs, id, repo, 1);
4229 /* Display the first matching ref only. */
4230 if (ref_str && (comma = strchr(ref_str, ',')) != NULL)
4234 if (ref_str == NULL) {
4235 err = got_object_id_str(&id_str, id);
4240 committer_time = got_object_commit_get_committer_time(commit);
4241 if (gmtime_r(&committer_time, &tm) == NULL) {
4242 err = got_error_from_errno("gmtime_r");
4245 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d ", &tm) == 0) {
4246 err = got_error(GOT_ERR_NO_SPACE);
4250 err = got_object_commit_get_logmsg(&logmsg0, commit);
4255 while (isspace((unsigned char)s[0]))
4258 nl = strchr(s, '\n');
4264 printf("%s%-7s %s\n", datebuf, ref_str, s);
4266 printf("%s%.7s %s\n", datebuf, id_str, s);
4268 if (fflush(stdout) != 0 && err == NULL)
4269 err = got_error_from_errno("fflush");
4277 static const struct got_error *
4278 print_diffstat(struct got_diffstat_cb_arg *dsa, const char *header)
4280 struct got_pathlist_entry *pe;
4283 printf("%s\n", header);
4285 TAILQ_FOREACH(pe, dsa->paths, entry) {
4286 struct got_diff_changed_path *cp = pe->data;
4287 int pad = dsa->max_path_len - pe->path_len + 1;
4289 printf(" %c %s%*c | %*d+ %*d-\n", cp->status, pe->path, pad,
4290 ' ', dsa->add_cols + 1, cp->add, dsa->rm_cols + 1, cp->rm);
4292 printf("\n%d file%s changed, %d insertion%s(+), %d deletion%s(-)\n\n",
4293 dsa->nfiles, dsa->nfiles > 1 ? "s" : "", dsa->ins,
4294 dsa->ins != 1 ? "s" : "", dsa->del, dsa->del != 1 ? "s" : "");
4296 if (fflush(stdout) != 0)
4297 return got_error_from_errno("fflush");
4302 static const struct got_error *
4308 if (fseeko(f, 0L, SEEK_SET) == -1)
4309 return got_error_from_errno("fseek");
4312 r = fread(buf, 1, sizeof(buf), f);
4315 return got_error_from_errno("fread");
4319 if (fwrite(buf, 1, r, stdout) != r)
4320 return got_ferror(stdout, GOT_ERR_IO);
4326 static const struct got_error *
4327 print_commit(struct got_commit_object *commit, struct got_object_id *id,
4328 struct got_repository *repo, const char *path,
4329 struct got_pathlist_head *changed_paths,
4330 struct got_diffstat_cb_arg *diffstat, int show_patch, int diff_context,
4331 struct got_reflist_object_id_map *refs_idmap, const char *custom_refs_str,
4334 const struct got_error *err = NULL;
4336 char *id_str, *datestr, *logmsg0, *logmsg, *line;
4338 time_t committer_time;
4339 const char *author, *committer;
4340 char *refs_str = NULL;
4342 err = got_object_id_str(&id_str, id);
4346 if (custom_refs_str == NULL) {
4347 struct got_reflist_head *refs;
4348 refs = got_reflist_object_id_map_lookup(refs_idmap, id);
4350 err = build_refs_str(&refs_str, refs, id, repo, 0);
4356 printf(GOT_COMMIT_SEP_STR);
4357 if (custom_refs_str)
4358 printf("%s %s (%s)\n", prefix ? prefix : "commit", id_str,
4361 printf("%s %s%s%s%s\n", prefix ? prefix : "commit", id_str,
4362 refs_str ? " (" : "", refs_str ? refs_str : "",
4363 refs_str ? ")" : "");
4368 printf("from: %s\n", got_object_commit_get_author(commit));
4369 author = got_object_commit_get_author(commit);
4370 committer = got_object_commit_get_committer(commit);
4371 if (strcmp(author, committer) != 0)
4372 printf("via: %s\n", committer);
4373 committer_time = got_object_commit_get_committer_time(commit);
4374 datestr = get_datestr(&committer_time, datebuf);
4376 printf("date: %s UTC\n", datestr);
4377 if (got_object_commit_get_nparents(commit) > 1) {
4378 const struct got_object_id_queue *parent_ids;
4379 struct got_object_qid *qid;
4381 parent_ids = got_object_commit_get_parent_ids(commit);
4382 STAILQ_FOREACH(qid, parent_ids, entry) {
4383 err = got_object_id_str(&id_str, &qid->id);
4386 printf("parent %d: %s\n", n++, id_str);
4392 err = got_object_commit_get_logmsg(&logmsg0, commit);
4398 line = strsep(&logmsg, "\n");
4400 printf(" %s\n", line);
4404 if (changed_paths && diffstat == NULL) {
4405 struct got_pathlist_entry *pe;
4407 TAILQ_FOREACH(pe, changed_paths, entry) {
4408 struct got_diff_changed_path *cp = pe->data;
4410 printf(" %c %s\n", cp->status, pe->path);
4418 err = got_error_from_errno("got_opentemp");
4423 err = print_patch(commit, id, path, diff_context, diffstat,
4424 repo, diffstat == NULL ? stdout : f);
4429 err = print_diffstat(diffstat, NULL);
4441 if (fflush(stdout) != 0 && err == NULL)
4442 err = got_error_from_errno("fflush");
4444 if (f && fclose(f) == EOF && err == NULL)
4445 err = got_error_from_errno("fclose");
4451 static const struct got_error *
4452 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
4453 struct got_repository *repo, const char *path, int show_changed_paths,
4454 int show_diffstat, int show_patch, const char *search_pattern,
4455 int diff_context, int limit, int log_branches, int reverse_display_order,
4456 struct got_reflist_object_id_map *refs_idmap, int one_line, int toposort,
4459 const struct got_error *err;
4460 struct got_commit_graph *graph;
4463 struct got_object_id_queue reversed_commits;
4464 struct got_object_qid *qid;
4465 struct got_commit_object *commit;
4466 struct got_pathlist_head changed_paths;
4468 STAILQ_INIT(&reversed_commits);
4469 TAILQ_INIT(&changed_paths);
4471 if (search_pattern && regcomp(®ex, search_pattern,
4472 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
4473 return got_error_msg(GOT_ERR_REGEX, search_pattern);
4475 err = got_commit_graph_open(&graph, path, !log_branches);
4478 if (log_branches && toposort) {
4479 err = got_commit_graph_toposort(graph, root_id, repo,
4480 check_cancelled, NULL);
4482 err = got_commit_graph_bfsort(graph, root_id, repo,
4483 check_cancelled, NULL);
4488 struct got_object_id id;
4489 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4490 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4492 if (sigint_received || sigpipe_received)
4495 err = got_commit_graph_iter_next(&id, graph, repo,
4496 check_cancelled, NULL);
4498 if (err->code == GOT_ERR_ITER_COMPLETED)
4503 err = got_object_open_as_commit(&commit, repo, &id);
4507 if (((show_changed_paths && !show_diffstat) ||
4508 (show_diffstat && !show_patch))
4509 && !reverse_display_order) {
4510 err = get_changed_paths(&changed_paths, commit, repo,
4511 show_diffstat ? &dsa : NULL);
4516 if (search_pattern) {
4517 err = match_commit(&have_match, &id, commit, ®ex);
4519 got_object_commit_close(commit);
4522 if (have_match == 0 && show_changed_paths)
4523 match_changed_paths(&have_match,
4524 &changed_paths, ®ex);
4525 if (have_match == 0 && show_patch) {
4526 err = match_patch(&have_match, commit, &id,
4527 path, diff_context, repo, ®ex, tmpfile);
4531 if (have_match == 0) {
4532 got_object_commit_close(commit);
4533 got_pathlist_free(&changed_paths,
4534 GOT_PATHLIST_FREE_ALL);
4539 if (reverse_display_order) {
4540 err = got_object_qid_alloc(&qid, &id);
4543 STAILQ_INSERT_HEAD(&reversed_commits, qid, entry);
4544 got_object_commit_close(commit);
4547 err = print_commit_oneline(commit, &id,
4550 err = print_commit(commit, &id, repo, path,
4551 (show_changed_paths || show_diffstat) ?
4552 &changed_paths : NULL,
4553 show_diffstat ? &dsa : NULL, show_patch,
4554 diff_context, refs_idmap, NULL, NULL);
4555 got_object_commit_close(commit);
4559 if ((limit && --limit == 0) ||
4560 (end_id && got_object_id_cmp(&id, end_id) == 0))
4563 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4565 if (reverse_display_order) {
4566 STAILQ_FOREACH(qid, &reversed_commits, entry) {
4567 struct got_diffstat_cb_arg dsa = { 0, 0, 0, 0, 0, 0,
4568 &changed_paths, 0, 0, GOT_DIFF_ALGORITHM_PATIENCE };
4570 err = got_object_open_as_commit(&commit, repo,
4574 if ((show_changed_paths && !show_diffstat) ||
4575 (show_diffstat && !show_patch)) {
4576 err = get_changed_paths(&changed_paths, commit,
4577 repo, show_diffstat ? &dsa : NULL);
4582 err = print_commit_oneline(commit, &qid->id,
4585 err = print_commit(commit, &qid->id, repo, path,
4586 (show_changed_paths || show_diffstat) ?
4587 &changed_paths : NULL,
4588 show_diffstat ? &dsa : NULL, show_patch,
4589 diff_context, refs_idmap, NULL, NULL);
4590 got_object_commit_close(commit);
4593 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4597 while (!STAILQ_EMPTY(&reversed_commits)) {
4598 qid = STAILQ_FIRST(&reversed_commits);
4599 STAILQ_REMOVE_HEAD(&reversed_commits, entry);
4600 got_object_qid_free(qid);
4602 got_pathlist_free(&changed_paths, GOT_PATHLIST_FREE_ALL);
4605 got_commit_graph_close(graph);
4612 fprintf(stderr, "usage: %s log [-bdPpRst] [-C number] [-c commit] "
4613 "[-l N] [-r repository-path] [-S search-pattern] [-x commit] "
4614 "[path]\n", getprogname());
4619 get_default_log_limit(void)
4621 const char *got_default_log_limit;
4625 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
4626 if (got_default_log_limit == NULL)
4628 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
4634 static const struct got_error *
4635 cmd_log(int argc, char *argv[])
4637 const struct got_error *error;
4638 struct got_repository *repo = NULL;
4639 struct got_worktree *worktree = NULL;
4640 struct got_object_id *start_id = NULL, *end_id = NULL;
4641 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
4642 char *keyword_idstr = NULL;
4643 const char *start_commit = NULL, *end_commit = NULL;
4644 const char *search_pattern = NULL;
4645 int diff_context = -1, ch;
4646 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
4647 int show_diffstat = 0, reverse_display_order = 0, one_line = 0;
4650 struct got_reflist_head refs;
4651 struct got_reflist_object_id_map *refs_idmap = NULL;
4652 FILE *tmpfile = NULL;
4653 int *pack_fds = NULL;
4658 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4664 limit = get_default_log_limit();
4666 while ((ch = getopt(argc, argv, "bC:c:dl:PpRr:S:stx:")) != -1) {
4672 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4675 errx(1, "number of context lines is %s: %s",
4679 start_commit = optarg;
4685 limit = strtonum(optarg, 0, INT_MAX, &errstr);
4687 errx(1, "number of commits is %s: %s",
4691 show_changed_paths = 1;
4697 reverse_display_order = 1;
4700 repo_path = realpath(optarg, NULL);
4701 if (repo_path == NULL)
4702 return got_error_from_errno2("realpath",
4704 got_path_strip_trailing_slashes(repo_path);
4707 search_pattern = optarg;
4716 end_commit = optarg;
4727 if (diff_context == -1)
4729 else if (!show_patch)
4730 errx(1, "-C requires -p");
4732 if (one_line && (show_patch || show_changed_paths || show_diffstat))
4733 errx(1, "cannot use -s with -d, -p or -P");
4735 cwd = getcwd(NULL, 0);
4737 error = got_error_from_errno("getcwd");
4741 error = got_repo_pack_fds_open(&pack_fds);
4745 if (repo_path == NULL) {
4746 error = got_worktree_open(&worktree, cwd,
4747 GOT_WORKTREE_GOT_DIR);
4748 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4755 error = got_worktree_resolve_path(&path, worktree,
4760 path = strdup(argv[0]);
4762 error = got_error_from_errno("strdup");
4766 } else if (argc != 0)
4769 if (repo_path == NULL) {
4770 repo_path = worktree ?
4771 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
4773 if (repo_path == NULL) {
4774 error = got_error_from_errno("strdup");
4778 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
4782 error = apply_unveil(got_repo_get_path(repo), 1,
4783 worktree ? got_worktree_get_root_path(worktree) : NULL);
4787 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4791 error = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
4795 if (start_commit == NULL) {
4796 struct got_reference *head_ref;
4797 struct got_commit_object *commit = NULL;
4798 error = got_ref_open(&head_ref, repo,
4799 worktree ? got_worktree_get_head_ref_name(worktree)
4803 error = got_ref_resolve(&start_id, repo, head_ref);
4804 got_ref_close(head_ref);
4807 error = got_object_open_as_commit(&commit, repo,
4811 got_object_commit_close(commit);
4813 error = got_keyword_to_idstr(&keyword_idstr, start_commit,
4817 if (keyword_idstr != NULL)
4818 start_commit = keyword_idstr;
4820 error = got_repo_match_object_id(&start_id, NULL,
4821 start_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4825 if (end_commit != NULL) {
4826 error = got_keyword_to_idstr(&keyword_idstr, end_commit,
4830 if (keyword_idstr != NULL)
4831 end_commit = keyword_idstr;
4833 error = got_repo_match_object_id(&end_id, NULL,
4834 end_commit, GOT_OBJ_TYPE_COMMIT, &refs, repo);
4841 * If a path was specified on the command line it was resolved
4842 * to a path in the work tree above. Prepend the work tree's
4843 * path prefix to obtain the corresponding in-repository path.
4847 prefix = got_worktree_get_path_prefix(worktree);
4848 if (asprintf(&in_repo_path, "%s%s%s", prefix,
4849 (path[0] != '\0') ? "/" : "", path) == -1) {
4850 error = got_error_from_errno("asprintf");
4855 error = got_repo_map_path(&in_repo_path, repo,
4861 path = in_repo_path;
4865 /* Release work tree lock. */
4866 got_worktree_close(worktree);
4870 if (search_pattern && show_patch) {
4871 tmpfile = got_opentemp();
4872 if (tmpfile == NULL) {
4873 error = got_error_from_errno("got_opentemp");
4878 error = print_commits(start_id, end_id, repo, path ? path : "",
4879 show_changed_paths, show_diffstat, show_patch, search_pattern,
4880 diff_context, limit, log_branches, reverse_display_order,
4881 refs_idmap, one_line, toposort, tmpfile);
4888 free(keyword_idstr);
4890 got_worktree_close(worktree);
4892 const struct got_error *close_err = got_repo_close(repo);
4897 const struct got_error *pack_err =
4898 got_repo_pack_fds_close(pack_fds);
4903 got_reflist_object_id_map_free(refs_idmap);
4904 if (tmpfile && fclose(tmpfile) == EOF && error == NULL)
4905 error = got_error_from_errno("fclose");
4906 got_ref_list_free(&refs);
4913 fprintf(stderr, "usage: %s diff [-adPsw] [-C number] [-c commit] "
4914 "[-r repository-path] [object1 object2 | path ...]\n",
4919 struct print_diff_arg {
4920 struct got_repository *repo;
4921 struct got_worktree *worktree;
4922 struct got_diffstat_cb_arg *diffstat;
4927 enum got_diff_algorithm diff_algo;
4928 int ignore_whitespace;
4929 int force_text_diff;
4936 * Create a file which contains the target path of a symlink so we can feed
4937 * it as content to the diff engine.
4939 static const struct got_error *
4940 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
4941 const char *abspath)
4943 const struct got_error *err = NULL;
4944 char target_path[PATH_MAX];
4945 ssize_t target_len, outlen;
4950 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
4951 if (target_len == -1)
4952 return got_error_from_errno2("readlinkat", abspath);
4954 target_len = readlink(abspath, target_path, PATH_MAX);
4955 if (target_len == -1)
4956 return got_error_from_errno2("readlink", abspath);
4959 *fd = got_opentempfd();
4961 return got_error_from_errno("got_opentempfd");
4963 outlen = write(*fd, target_path, target_len);
4965 err = got_error_from_errno("got_opentempfd");
4969 if (lseek(*fd, 0, SEEK_SET) == -1) {
4970 err = got_error_from_errno2("lseek", abspath);
4981 static const struct got_error *
4982 print_diff(void *arg, unsigned char status, unsigned char staged_status,
4983 const char *path, struct got_object_id *blob_id,
4984 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4985 int dirfd, const char *de_name)
4987 struct print_diff_arg *a = arg;
4988 const struct got_error *err = NULL;
4989 struct got_blob_object *blob1 = NULL;
4990 int fd = -1, fd1 = -1, fd2 = -1;
4992 char *abspath = NULL, *label1 = NULL;
4997 memset(&sb, 0, sizeof(sb));
4999 if (a->diff_staged) {
5000 if (staged_status != GOT_STATUS_MODIFY &&
5001 staged_status != GOT_STATUS_ADD &&
5002 staged_status != GOT_STATUS_DELETE)
5005 if (staged_status == GOT_STATUS_DELETE)
5007 if (status == GOT_STATUS_NONEXISTENT)
5008 return got_error_set_errno(ENOENT, path);
5009 if (status != GOT_STATUS_MODIFY &&
5010 status != GOT_STATUS_ADD &&
5011 status != GOT_STATUS_DELETE &&
5012 status != GOT_STATUS_CONFLICT)
5016 err = got_opentemp_truncate(a->f1);
5018 return got_error_from_errno("got_opentemp_truncate");
5019 err = got_opentemp_truncate(a->f2);
5021 return got_error_from_errno("got_opentemp_truncate");
5023 if (!a->header_shown) {
5024 if (fprintf(a->outfile, "diff %s%s\n",
5025 a->diff_staged ? "-s " : "",
5026 got_worktree_get_root_path(a->worktree)) < 0) {
5027 err = got_error_from_errno("fprintf");
5030 if (fprintf(a->outfile, "commit - %s\n", a->id_str) < 0) {
5031 err = got_error_from_errno("fprintf");
5034 if (fprintf(a->outfile, "path + %s%s\n",
5035 got_worktree_get_root_path(a->worktree),
5036 a->diff_staged ? " (staged changes)" : "") < 0) {
5037 err = got_error_from_errno("fprintf");
5040 a->header_shown = 1;
5043 if (a->diff_staged) {
5044 const char *label1 = NULL, *label2 = NULL;
5045 switch (staged_status) {
5046 case GOT_STATUS_MODIFY:
5050 case GOT_STATUS_ADD:
5053 case GOT_STATUS_DELETE:
5057 return got_error(GOT_ERR_FILE_STATUS);
5059 fd1 = got_opentempfd();
5061 err = got_error_from_errno("got_opentempfd");
5064 fd2 = got_opentempfd();
5066 err = got_error_from_errno("got_opentempfd");
5069 err = got_diff_objects_as_blobs(NULL, NULL, a->f1, a->f2,
5070 fd1, fd2, blob_id, staged_blob_id, label1, label2,
5071 a->diff_algo, a->diff_context, a->ignore_whitespace,
5072 a->force_text_diff, a->diffstat, a->repo, a->outfile);
5076 fd1 = got_opentempfd();
5078 err = got_error_from_errno("got_opentempfd");
5082 if (staged_status == GOT_STATUS_ADD ||
5083 staged_status == GOT_STATUS_MODIFY) {
5085 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
5089 err = got_object_id_str(&id_str, staged_blob_id);
5092 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
5093 err = got_error_from_errno("asprintf");
5098 } else if (status != GOT_STATUS_ADD) {
5099 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192,
5105 if (status != GOT_STATUS_DELETE) {
5106 if (asprintf(&abspath, "%s/%s",
5107 got_worktree_get_root_path(a->worktree), path) == -1) {
5108 err = got_error_from_errno("asprintf");
5113 fd = openat(dirfd, de_name,
5114 O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5116 if (!got_err_open_nofollow_on_symlink()) {
5117 err = got_error_from_errno2("openat",
5121 err = get_symlink_target_file(&fd, dirfd,
5127 fd = open(abspath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
5129 if (!got_err_open_nofollow_on_symlink()) {
5130 err = got_error_from_errno2("open",
5134 err = get_symlink_target_file(&fd, dirfd,
5140 if (fstatat(fd, abspath, &sb, AT_SYMLINK_NOFOLLOW) == -1) {
5141 err = got_error_from_errno2("fstatat", abspath);
5144 f2 = fdopen(fd, "r");
5146 err = got_error_from_errno2("fdopen", abspath);
5154 err = got_object_blob_dump_to_file(&size1, NULL, NULL,
5160 err = got_diff_blob_file(blob1, a->f1, size1, label1, f2 ? f2 : a->f2,
5161 f2_exists, &sb, path, GOT_DIFF_ALGORITHM_PATIENCE, a->diff_context,
5162 a->ignore_whitespace, a->force_text_diff, a->diffstat, a->outfile);
5164 if (fd1 != -1 && close(fd1) == -1 && err == NULL)
5165 err = got_error_from_errno("close");
5166 if (fd2 != -1 && close(fd2) == -1 && err == NULL)
5167 err = got_error_from_errno("close");
5169 got_object_blob_close(blob1);
5170 if (fd != -1 && close(fd) == -1 && err == NULL)
5171 err = got_error_from_errno("close");
5172 if (f2 && fclose(f2) == EOF && err == NULL)
5173 err = got_error_from_errno("fclose");
5178 static const struct got_error *
5179 cmd_diff(int argc, char *argv[])
5181 const struct got_error *error;
5182 struct got_repository *repo = NULL;
5183 struct got_worktree *worktree = NULL;
5184 char *cwd = NULL, *repo_path = NULL;
5185 const char *commit_args[2] = { NULL, NULL };
5186 int ncommit_args = 0;
5187 struct got_object_id *ids[2] = { NULL, NULL };
5188 char *labels[2] = { NULL, NULL };
5189 int type1 = GOT_OBJ_TYPE_ANY, type2 = GOT_OBJ_TYPE_ANY;
5190 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch, i;
5191 int force_text_diff = 0, force_path = 0, rflag = 0, show_diffstat = 0;
5193 struct got_reflist_head refs;
5194 struct got_pathlist_head diffstat_paths, paths;
5195 FILE *f1 = NULL, *f2 = NULL, *outfile = NULL;
5196 int fd1 = -1, fd2 = -1;
5197 int *pack_fds = NULL;
5198 struct got_diffstat_cb_arg dsa;
5200 memset(&dsa, 0, sizeof(dsa));
5204 TAILQ_INIT(&diffstat_paths);
5207 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5212 while ((ch = getopt(argc, argv, "aC:c:dPr:sw")) != -1) {
5215 force_text_diff = 1;
5218 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
5221 errx(1, "number of context lines is %s: %s",
5225 if (ncommit_args >= 2)
5226 errx(1, "too many -c options used");
5227 commit_args[ncommit_args++] = optarg;
5236 repo_path = realpath(optarg, NULL);
5237 if (repo_path == NULL)
5238 return got_error_from_errno2("realpath",
5240 got_path_strip_trailing_slashes(repo_path);
5247 ignore_whitespace = 1;
5258 cwd = getcwd(NULL, 0);
5260 error = got_error_from_errno("getcwd");
5264 error = got_repo_pack_fds_open(&pack_fds);
5268 if (repo_path == NULL) {
5269 error = got_worktree_open(&worktree, cwd,
5270 GOT_WORKTREE_GOT_DIR);
5271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5277 strdup(got_worktree_get_repo_path(worktree));
5278 if (repo_path == NULL) {
5279 error = got_error_from_errno("strdup");
5283 repo_path = strdup(cwd);
5284 if (repo_path == NULL) {
5285 error = got_error_from_errno("strdup");
5291 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5296 if (show_diffstat) {
5297 dsa.paths = &diffstat_paths;
5298 dsa.force_text = force_text_diff;
5299 dsa.ignore_ws = ignore_whitespace;
5300 dsa.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5303 if (rflag || worktree == NULL || ncommit_args > 0) {
5305 error = got_error_msg(GOT_ERR_NOT_IMPL,
5306 "-P option can only be used when diffing "
5311 error = got_error_msg(GOT_ERR_NOT_IMPL,
5312 "-s option can only be used when diffing "
5318 error = apply_unveil(got_repo_get_path(repo), 1,
5319 worktree ? got_worktree_get_root_path(worktree) : NULL);
5323 if ((!force_path && argc == 2) || ncommit_args > 0) {
5324 int obj_type = (ncommit_args > 0 ?
5325 GOT_OBJ_TYPE_COMMIT : GOT_OBJ_TYPE_ANY);
5326 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5330 for (i = 0; i < (ncommit_args > 0 ? ncommit_args : argc); i++) {
5332 char *keyword_idstr = NULL;
5334 if (ncommit_args > 0)
5335 arg = commit_args[i];
5339 error = got_keyword_to_idstr(&keyword_idstr, arg,
5343 if (keyword_idstr != NULL)
5344 arg = keyword_idstr;
5346 error = got_repo_match_object_id(&ids[i], &labels[i],
5347 arg, obj_type, &refs, repo);
5348 free(keyword_idstr);
5350 if (error->code != GOT_ERR_NOT_REF &&
5351 error->code != GOT_ERR_NO_OBJ)
5353 if (ncommit_args > 0)
5361 f1 = got_opentemp();
5363 error = got_error_from_errno("got_opentemp");
5367 f2 = got_opentemp();
5369 error = got_error_from_errno("got_opentemp");
5373 outfile = got_opentemp();
5374 if (outfile == NULL) {
5375 error = got_error_from_errno("got_opentemp");
5379 if (ncommit_args == 0 && (ids[0] == NULL || ids[1] == NULL)) {
5380 struct print_diff_arg arg;
5383 if (worktree == NULL) {
5384 if (argc == 2 && ids[0] == NULL) {
5385 error = got_error_path(argv[0], GOT_ERR_NO_OBJ);
5387 } else if (argc == 2 && ids[1] == NULL) {
5388 error = got_error_path(argv[1], GOT_ERR_NO_OBJ);
5390 } else if (argc > 0) {
5391 error = got_error_fmt(GOT_ERR_NOT_WORKTREE,
5392 "%s", "specified paths cannot be resolved");
5395 error = got_error(GOT_ERR_NOT_WORKTREE);
5400 error = get_worktree_paths_from_argv(&paths, argc, argv,
5405 error = got_object_id_str(&id_str,
5406 got_worktree_get_base_commit_id(worktree));
5410 arg.worktree = worktree;
5411 arg.diff_algo = GOT_DIFF_ALGORITHM_PATIENCE;
5412 arg.diff_context = diff_context;
5413 arg.id_str = id_str;
5414 arg.header_shown = 0;
5415 arg.diff_staged = diff_staged;
5416 arg.ignore_whitespace = ignore_whitespace;
5417 arg.force_text_diff = force_text_diff;
5418 arg.diffstat = show_diffstat ? &dsa : NULL;
5421 arg.outfile = outfile;
5423 error = got_worktree_status(worktree, &paths, repo, 0,
5424 print_diff, &arg, check_cancelled, NULL);
5429 if (show_diffstat && dsa.nfiles > 0) {
5432 if (asprintf(&header, "diffstat %s%s",
5433 diff_staged ? "-s " : "",
5434 got_worktree_get_root_path(worktree)) == -1) {
5435 error = got_error_from_errno("asprintf");
5439 error = print_diffstat(&dsa, header);
5445 error = printfile(outfile);
5449 if (ncommit_args == 1) {
5450 struct got_commit_object *commit;
5451 error = got_object_open_as_commit(&commit, repo, ids[0]);
5455 labels[1] = labels[0];
5457 if (got_object_commit_get_nparents(commit) > 0) {
5458 const struct got_object_id_queue *pids;
5459 struct got_object_qid *pid;
5460 pids = got_object_commit_get_parent_ids(commit);
5461 pid = STAILQ_FIRST(pids);
5462 ids[0] = got_object_id_dup(&pid->id);
5463 if (ids[0] == NULL) {
5464 error = got_error_from_errno(
5465 "got_object_id_dup");
5466 got_object_commit_close(commit);
5469 error = got_object_id_str(&labels[0], ids[0]);
5471 got_object_commit_close(commit);
5476 labels[0] = strdup("/dev/null");
5477 if (labels[0] == NULL) {
5478 error = got_error_from_errno("strdup");
5479 got_object_commit_close(commit);
5484 got_object_commit_close(commit);
5487 if (ncommit_args == 0 && argc > 2) {
5488 error = got_error_msg(GOT_ERR_BAD_PATH,
5489 "path arguments cannot be used when diffing two objects");
5494 error = got_object_get_type(&type1, repo, ids[0]);
5499 error = got_object_get_type(&type2, repo, ids[1]);
5502 if (type1 != GOT_OBJ_TYPE_ANY && type1 != type2) {
5503 error = got_error(GOT_ERR_OBJ_TYPE);
5506 if (type1 == GOT_OBJ_TYPE_BLOB && argc > 2) {
5507 error = got_error_msg(GOT_ERR_OBJ_TYPE,
5508 "path arguments cannot be used when diffing blobs");
5512 for (i = 0; ncommit_args > 0 && i < argc; i++) {
5514 struct got_pathlist_entry *new;
5518 error = got_worktree_resolve_path(&p, worktree,
5522 prefix = got_worktree_get_path_prefix(worktree);
5523 while (prefix[0] == '/')
5525 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5526 (p[0] != '\0' && prefix[0] != '\0') ? "/" : "",
5528 error = got_error_from_errno("asprintf");
5534 char *mapped_path, *s;
5535 error = got_repo_map_path(&mapped_path, repo, argv[i]);
5541 in_repo_path = strdup(s);
5542 if (in_repo_path == NULL) {
5543 error = got_error_from_errno("asprintf");
5550 error = got_pathlist_insert(&new, &paths, in_repo_path, NULL);
5551 if (error || new == NULL /* duplicate */)
5558 /* Release work tree lock. */
5559 got_worktree_close(worktree);
5563 fd1 = got_opentempfd();
5565 error = got_error_from_errno("got_opentempfd");
5569 fd2 = got_opentempfd();
5571 error = got_error_from_errno("got_opentempfd");
5575 switch (type1 == GOT_OBJ_TYPE_ANY ? type2 : type1) {
5576 case GOT_OBJ_TYPE_BLOB:
5577 error = got_diff_objects_as_blobs(NULL, NULL, f1, f2,
5578 fd1, fd2, ids[0], ids[1], NULL, NULL,
5579 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5580 ignore_whitespace, force_text_diff,
5581 show_diffstat ? &dsa : NULL, repo, outfile);
5583 case GOT_OBJ_TYPE_TREE:
5584 error = got_diff_objects_as_trees(NULL, NULL, f1, f2, fd1, fd2,
5585 ids[0], ids[1], &paths, "", "",
5586 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5587 ignore_whitespace, force_text_diff,
5588 show_diffstat ? &dsa : NULL, repo, outfile);
5590 case GOT_OBJ_TYPE_COMMIT:
5591 fprintf(outfile, "diff %s %s\n", labels[0], labels[1]);
5592 error = got_diff_objects_as_commits(NULL, NULL, f1, f2,
5593 fd1, fd2, ids[0], ids[1], &paths,
5594 GOT_DIFF_ALGORITHM_PATIENCE, diff_context,
5595 ignore_whitespace, force_text_diff,
5596 show_diffstat ? &dsa : NULL, repo, outfile);
5599 error = got_error(GOT_ERR_OBJ_TYPE);
5604 if (show_diffstat && dsa.nfiles > 0) {
5605 char *header = NULL;
5607 if (asprintf(&header, "diffstat %s %s",
5608 labels[0], labels[1]) == -1) {
5609 error = got_error_from_errno("asprintf");
5613 error = print_diffstat(&dsa, header);
5619 error = printfile(outfile);
5627 got_worktree_close(worktree);
5629 const struct got_error *close_err = got_repo_close(repo);
5634 const struct got_error *pack_err =
5635 got_repo_pack_fds_close(pack_fds);
5639 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
5640 got_pathlist_free(&diffstat_paths, GOT_PATHLIST_FREE_ALL);
5641 got_ref_list_free(&refs);
5642 if (outfile && fclose(outfile) == EOF && error == NULL)
5643 error = got_error_from_errno("fclose");
5644 if (f1 && fclose(f1) == EOF && error == NULL)
5645 error = got_error_from_errno("fclose");
5646 if (f2 && fclose(f2) == EOF && error == NULL)
5647 error = got_error_from_errno("fclose");
5648 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
5649 error = got_error_from_errno("close");
5650 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
5651 error = got_error_from_errno("close");
5659 "usage: %s blame [-c commit] [-r repository-path] path\n",
5668 char datebuf[11]; /* YYYY-MM-DD + NUL */
5671 struct blame_cb_args {
5672 struct blame_line *lines;
5676 off_t *line_offsets;
5678 struct got_repository *repo;
5681 static const struct got_error *
5682 blame_cb(void *arg, int nlines, int lineno,
5683 struct got_commit_object *commit, struct got_object_id *id)
5685 const struct got_error *err = NULL;
5686 struct blame_cb_args *a = arg;
5687 struct blame_line *bline;
5689 size_t linesize = 0;
5692 time_t committer_time;
5694 if (nlines != a->nlines ||
5695 (lineno != -1 && lineno < 1) || lineno > a->nlines)
5696 return got_error(GOT_ERR_RANGE);
5698 if (sigint_received)
5699 return got_error(GOT_ERR_ITER_COMPLETED);
5702 return NULL; /* no change in this commit */
5704 /* Annotate this line. */
5705 bline = &a->lines[lineno - 1];
5706 if (bline->annotated)
5708 err = got_object_id_str(&bline->id_str, id);
5712 bline->committer = strdup(got_object_commit_get_committer(commit));
5713 if (bline->committer == NULL) {
5714 err = got_error_from_errno("strdup");
5718 committer_time = got_object_commit_get_committer_time(commit);
5719 if (gmtime_r(&committer_time, &tm) == NULL)
5720 return got_error_from_errno("gmtime_r");
5721 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
5723 err = got_error(GOT_ERR_NO_SPACE);
5726 bline->annotated = 1;
5728 /* Print lines annotated so far. */
5729 bline = &a->lines[a->lineno_cur - 1];
5730 if (!bline->annotated)
5733 offset = a->line_offsets[a->lineno_cur - 1];
5734 if (fseeko(a->f, offset, SEEK_SET) == -1) {
5735 err = got_error_from_errno("fseeko");
5739 while (a->lineno_cur <= a->nlines && bline->annotated) {
5740 char *smallerthan, *at, *nl, *committer;
5743 if (getline(&line, &linesize, a->f) == -1) {
5745 err = got_error_from_errno("getline");
5749 committer = bline->committer;
5750 smallerthan = strchr(committer, '<');
5751 if (smallerthan && smallerthan[1] != '\0')
5752 committer = smallerthan + 1;
5753 at = strchr(committer, '@');
5756 len = strlen(committer);
5758 committer[8] = '\0';
5760 nl = strchr(line, '\n');
5763 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
5764 bline->id_str, bline->datebuf, committer, line);
5767 bline = &a->lines[a->lineno_cur - 1];
5774 static const struct got_error *
5775 cmd_blame(int argc, char *argv[])
5777 const struct got_error *error;
5778 struct got_repository *repo = NULL;
5779 struct got_worktree *worktree = NULL;
5780 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
5781 char *link_target = NULL;
5782 struct got_object_id *obj_id = NULL;
5783 struct got_object_id *commit_id = NULL;
5784 struct got_commit_object *commit = NULL;
5785 struct got_blob_object *blob = NULL;
5786 char *commit_id_str = NULL, *keyword_idstr = NULL;
5787 struct blame_cb_args bca;
5788 int ch, obj_type, i, fd1 = -1, fd2 = -1, fd3 = -1;
5790 int *pack_fds = NULL;
5791 FILE *f1 = NULL, *f2 = NULL;
5793 fd1 = got_opentempfd();
5795 return got_error_from_errno("got_opentempfd");
5797 memset(&bca, 0, sizeof(bca));
5800 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5805 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
5808 commit_id_str = optarg;
5811 repo_path = realpath(optarg, NULL);
5812 if (repo_path == NULL)
5813 return got_error_from_errno2("realpath",
5815 got_path_strip_trailing_slashes(repo_path);
5831 cwd = getcwd(NULL, 0);
5833 error = got_error_from_errno("getcwd");
5837 error = got_repo_pack_fds_open(&pack_fds);
5841 if (repo_path == NULL) {
5842 error = got_worktree_open(&worktree, cwd,
5843 GOT_WORKTREE_GOT_DIR);
5844 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5850 strdup(got_worktree_get_repo_path(worktree));
5851 if (repo_path == NULL) {
5852 error = got_error_from_errno("strdup");
5857 repo_path = strdup(cwd);
5858 if (repo_path == NULL) {
5859 error = got_error_from_errno("strdup");
5865 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
5870 const char *prefix = got_worktree_get_path_prefix(worktree);
5873 error = got_worktree_resolve_path(&p, worktree, path);
5876 if (asprintf(&in_repo_path, "%s%s%s", prefix,
5877 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
5879 error = got_error_from_errno("asprintf");
5884 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5886 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5889 error = got_repo_map_path(&in_repo_path, repo, path);
5894 if (commit_id_str == NULL) {
5895 struct got_reference *head_ref;
5896 error = got_ref_open(&head_ref, repo, worktree ?
5897 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
5900 error = got_ref_resolve(&commit_id, repo, head_ref);
5901 got_ref_close(head_ref);
5905 struct got_reflist_head refs;
5908 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
5913 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
5917 if (keyword_idstr != NULL)
5918 commit_id_str = keyword_idstr;
5920 error = got_repo_match_object_id(&commit_id, NULL,
5921 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
5922 got_ref_list_free(&refs);
5928 /* Release work tree lock. */
5929 got_worktree_close(worktree);
5933 error = got_object_open_as_commit(&commit, repo, commit_id);
5937 error = got_object_resolve_symlinks(&link_target, in_repo_path,
5942 error = got_object_id_by_path(&obj_id, repo, commit,
5943 link_target ? link_target : in_repo_path);
5947 error = got_object_get_type(&obj_type, repo, obj_id);
5951 if (obj_type != GOT_OBJ_TYPE_BLOB) {
5952 error = got_error_path(link_target ? link_target : in_repo_path,
5957 error = got_object_open_as_blob(&blob, repo, obj_id, 8192, fd1);
5960 bca.f = got_opentemp();
5961 if (bca.f == NULL) {
5962 error = got_error_from_errno("got_opentemp");
5965 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
5966 &bca.line_offsets, bca.f, blob);
5967 if (error || bca.nlines == 0)
5970 /* Don't include \n at EOF in the blame line count. */
5971 if (bca.line_offsets[bca.nlines - 1] == filesize)
5974 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
5975 if (bca.lines == NULL) {
5976 error = got_error_from_errno("calloc");
5980 bca.nlines_prec = 0;
5988 fd2 = got_opentempfd();
5990 error = got_error_from_errno("got_opentempfd");
5993 fd3 = got_opentempfd();
5995 error = got_error_from_errno("got_opentempfd");
5998 f1 = got_opentemp();
6000 error = got_error_from_errno("got_opentemp");
6003 f2 = got_opentemp();
6005 error = got_error_from_errno("got_opentemp");
6008 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
6009 repo, GOT_DIFF_ALGORITHM_PATIENCE, blame_cb, &bca,
6010 check_cancelled, NULL, fd2, fd3, f1, f2);
6012 free(keyword_idstr);
6020 got_object_commit_close(commit);
6022 if (fd1 != -1 && close(fd1) == -1 && error == NULL)
6023 error = got_error_from_errno("close");
6024 if (fd2 != -1 && close(fd2) == -1 && error == NULL)
6025 error = got_error_from_errno("close");
6026 if (fd3 != -1 && close(fd3) == -1 && error == NULL)
6027 error = got_error_from_errno("close");
6028 if (f1 && fclose(f1) == EOF && error == NULL)
6029 error = got_error_from_errno("fclose");
6030 if (f2 && fclose(f2) == EOF && error == NULL)
6031 error = got_error_from_errno("fclose");
6034 got_object_blob_close(blob);
6036 got_worktree_close(worktree);
6038 const struct got_error *close_err = got_repo_close(repo);
6043 const struct got_error *pack_err =
6044 got_repo_pack_fds_close(pack_fds);
6049 for (i = 0; i < bca.nlines; i++) {
6050 struct blame_line *bline = &bca.lines[i];
6051 free(bline->id_str);
6052 free(bline->committer);
6056 free(bca.line_offsets);
6057 if (bca.f && fclose(bca.f) == EOF && error == NULL)
6058 error = got_error_from_errno("fclose");
6065 fprintf(stderr, "usage: %s tree [-iR] [-c commit] [-r repository-path] "
6066 "[path]\n", getprogname());
6070 static const struct got_error *
6071 print_entry(struct got_tree_entry *te, const char *id, const char *path,
6072 const char *root_path, struct got_repository *repo)
6074 const struct got_error *err = NULL;
6075 int is_root_path = (strcmp(path, root_path) == 0);
6076 const char *modestr = "";
6077 mode_t mode = got_tree_entry_get_mode(te);
6078 char *link_target = NULL;
6080 path += strlen(root_path);
6081 while (path[0] == '/')
6084 if (got_object_tree_entry_is_submodule(te))
6086 else if (S_ISLNK(mode)) {
6089 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
6092 for (i = 0; link_target[i] != '\0'; i++) {
6093 if (!isprint((unsigned char)link_target[i]))
6094 link_target[i] = '?';
6099 else if (S_ISDIR(mode))
6101 else if (mode & S_IXUSR)
6104 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
6105 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
6106 link_target ? " -> ": "", link_target ? link_target : "");
6112 static const struct got_error *
6113 print_tree(const char *path, struct got_commit_object *commit,
6114 int show_ids, int recurse, const char *root_path,
6115 struct got_repository *repo)
6117 const struct got_error *err = NULL;
6118 struct got_object_id *tree_id = NULL;
6119 struct got_tree_object *tree = NULL;
6122 err = got_object_id_by_path(&tree_id, repo, commit, path);
6126 err = got_object_open_as_tree(&tree, repo, tree_id);
6129 nentries = got_object_tree_get_nentries(tree);
6130 for (i = 0; i < nentries; i++) {
6131 struct got_tree_entry *te;
6134 if (sigint_received || sigpipe_received)
6137 te = got_object_tree_get_entry(tree, i);
6140 err = got_object_id_str(&id_str,
6141 got_tree_entry_get_id(te));
6144 if (asprintf(&id, "%s ", id_str) == -1) {
6145 err = got_error_from_errno("asprintf");
6151 err = print_entry(te, id, path, root_path, repo);
6156 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
6158 if (asprintf(&child_path, "%s%s%s", path,
6159 path[0] == '/' && path[1] == '\0' ? "" : "/",
6160 got_tree_entry_get_name(te)) == -1) {
6161 err = got_error_from_errno("asprintf");
6164 err = print_tree(child_path, commit, show_ids, 1,
6173 got_object_tree_close(tree);
6178 static const struct got_error *
6179 cmd_tree(int argc, char *argv[])
6181 const struct got_error *error;
6182 struct got_repository *repo = NULL;
6183 struct got_worktree *worktree = NULL;
6184 const char *path, *refname = NULL;
6185 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
6186 struct got_object_id *commit_id = NULL;
6187 struct got_commit_object *commit = NULL;
6188 char *commit_id_str = NULL, *keyword_idstr = NULL;
6189 int show_ids = 0, recurse = 0;
6191 int *pack_fds = NULL;
6194 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6199 while ((ch = getopt(argc, argv, "c:iRr:")) != -1) {
6202 commit_id_str = optarg;
6211 repo_path = realpath(optarg, NULL);
6212 if (repo_path == NULL)
6213 return got_error_from_errno2("realpath",
6215 got_path_strip_trailing_slashes(repo_path);
6233 cwd = getcwd(NULL, 0);
6235 error = got_error_from_errno("getcwd");
6239 error = got_repo_pack_fds_open(&pack_fds);
6243 if (repo_path == NULL) {
6244 error = got_worktree_open(&worktree, cwd,
6245 GOT_WORKTREE_GOT_DIR);
6246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6252 strdup(got_worktree_get_repo_path(worktree));
6253 if (repo_path == NULL)
6254 error = got_error_from_errno("strdup");
6258 repo_path = strdup(cwd);
6259 if (repo_path == NULL) {
6260 error = got_error_from_errno("strdup");
6266 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6271 const char *prefix = got_worktree_get_path_prefix(worktree);
6274 if (path == NULL || got_path_is_root_dir(path))
6276 error = got_worktree_resolve_path(&p, worktree, path);
6279 if (asprintf(&in_repo_path, "%s%s%s", prefix,
6280 (p[0] != '\0' && !got_path_is_root_dir(prefix)) ? "/" : "",
6282 error = got_error_from_errno("asprintf");
6287 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6291 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6296 error = got_repo_map_path(&in_repo_path, repo, path);
6301 if (commit_id_str == NULL) {
6302 struct got_reference *head_ref;
6304 refname = got_worktree_get_head_ref_name(worktree);
6306 refname = GOT_REF_HEAD;
6307 error = got_ref_open(&head_ref, repo, refname, 0);
6310 error = got_ref_resolve(&commit_id, repo, head_ref);
6311 got_ref_close(head_ref);
6315 struct got_reflist_head refs;
6318 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
6323 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
6327 if (keyword_idstr != NULL)
6328 commit_id_str = keyword_idstr;
6330 error = got_repo_match_object_id(&commit_id, NULL,
6331 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
6332 got_ref_list_free(&refs);
6338 /* Release work tree lock. */
6339 got_worktree_close(worktree);
6343 error = got_object_open_as_commit(&commit, repo, commit_id);
6347 error = print_tree(in_repo_path, commit, show_ids, recurse,
6348 in_repo_path, repo);
6350 free(keyword_idstr);
6356 got_object_commit_close(commit);
6358 got_worktree_close(worktree);
6360 const struct got_error *close_err = got_repo_close(repo);
6365 const struct got_error *pack_err =
6366 got_repo_pack_fds_close(pack_fds);
6376 fprintf(stderr, "usage: %s status [-I] [-S status-codes] "
6377 "[-s status-codes] [path ...]\n", getprogname());
6381 struct got_status_arg {
6386 static const struct got_error *
6387 print_status(void *arg, unsigned char status, unsigned char staged_status,
6388 const char *path, struct got_object_id *blob_id,
6389 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6390 int dirfd, const char *de_name)
6392 struct got_status_arg *st = arg;
6394 if (status == staged_status && (status == GOT_STATUS_DELETE))
6395 status = GOT_STATUS_NO_CHANGE;
6396 if (st != NULL && st->status_codes) {
6397 size_t ncodes = strlen(st->status_codes);
6400 for (i = 0; i < ncodes ; i++) {
6402 if (status == st->status_codes[i] ||
6403 staged_status == st->status_codes[i]) {
6408 if (status == st->status_codes[i] ||
6409 staged_status == st->status_codes[i])
6414 if (st->suppress && j == 0)
6421 printf("%c%c %s\n", status, staged_status, path);
6425 static const struct got_error *
6426 show_operation_in_progress(struct got_worktree *worktree,
6427 struct got_repository *repo)
6429 const struct got_error *err;
6430 char *new_base_branch_name = NULL;
6431 char *branch_name = NULL;
6432 int rebase_in_progress, histedit_in_progress, merge_in_progress;
6434 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6437 if (rebase_in_progress) {
6438 err = got_worktree_rebase_info(&new_base_branch_name,
6439 &branch_name, worktree, repo);
6442 printf("Work tree is rebasing %s onto %s\n",
6443 branch_name, new_base_branch_name);
6446 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6450 if (histedit_in_progress) {
6451 err = got_worktree_histedit_info(&branch_name, worktree, repo);
6454 printf("Work tree is editing the history of %s\n", branch_name);
6457 err = got_worktree_merge_in_progress(&merge_in_progress,
6461 if (merge_in_progress) {
6462 err = got_worktree_merge_info(&branch_name, worktree,
6466 printf("Work tree is merging %s into %s\n", branch_name,
6467 got_worktree_get_head_ref_name(worktree));
6470 free(new_base_branch_name);
6475 static const struct got_error *
6476 cmd_status(int argc, char *argv[])
6478 const struct got_error *close_err, *error = NULL;
6479 struct got_repository *repo = NULL;
6480 struct got_worktree *worktree = NULL;
6481 struct got_status_arg st;
6483 struct got_pathlist_head paths;
6484 int ch, i, no_ignores = 0;
6485 int *pack_fds = NULL;
6489 memset(&st, 0, sizeof(st));
6490 st.status_codes = NULL;
6494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6499 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6505 if (st.status_codes != NULL && st.suppress == 0)
6506 option_conflict('S', 's');
6510 for (i = 0; optarg[i] != '\0'; i++) {
6511 switch (optarg[i]) {
6512 case GOT_STATUS_MODIFY:
6513 case GOT_STATUS_ADD:
6514 case GOT_STATUS_DELETE:
6515 case GOT_STATUS_CONFLICT:
6516 case GOT_STATUS_MISSING:
6517 case GOT_STATUS_OBSTRUCTED:
6518 case GOT_STATUS_UNVERSIONED:
6519 case GOT_STATUS_MODE_CHANGE:
6520 case GOT_STATUS_NONEXISTENT:
6523 errx(1, "invalid status code '%c'",
6527 if (ch == 's' && st.suppress)
6528 option_conflict('s', 'S');
6529 st.status_codes = optarg;
6540 cwd = getcwd(NULL, 0);
6542 error = got_error_from_errno("getcwd");
6546 error = got_repo_pack_fds_open(&pack_fds);
6550 error = got_worktree_open(&worktree, cwd,
6551 GOT_WORKTREE_GOT_DIR);
6553 if (error->code == GOT_ERR_NOT_WORKTREE)
6554 error = wrap_not_worktree_error(error, "status", cwd);
6558 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6563 error = apply_unveil(got_repo_get_path(repo), 1,
6564 got_worktree_get_root_path(worktree));
6568 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6572 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6573 print_status, &st, check_cancelled, NULL);
6577 error = show_operation_in_progress(worktree, repo);
6580 const struct got_error *pack_err =
6581 got_repo_pack_fds_close(pack_fds);
6586 close_err = got_repo_close(repo);
6590 if (worktree != NULL) {
6591 close_err = got_worktree_close(worktree);
6596 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6604 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6605 "[-s reference] [name]\n", getprogname());
6609 static const struct got_error *
6610 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6612 static const struct got_error *err = NULL;
6613 struct got_reflist_head refs;
6614 struct got_reflist_entry *re;
6617 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6618 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6623 TAILQ_FOREACH(re, &refs, entry) {
6625 refstr = got_ref_to_str(re->ref);
6626 if (refstr == NULL) {
6627 err = got_error_from_errno("got_ref_to_str");
6630 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6634 got_ref_list_free(&refs);
6638 static const struct got_error *
6639 delete_ref_by_name(struct got_repository *repo, const char *refname)
6641 const struct got_error *err;
6642 struct got_reference *ref;
6644 err = got_ref_open(&ref, repo, refname, 0);
6648 err = delete_ref(repo, ref);
6653 static const struct got_error *
6654 add_ref(struct got_repository *repo, const char *refname, const char *target)
6656 const struct got_error *err = NULL;
6657 struct got_object_id *id = NULL;
6658 struct got_reference *ref = NULL;
6659 struct got_reflist_head refs;
6662 * Don't let the user create a reference name with a leading '-'.
6663 * While technically a valid reference name, this case is usually
6664 * an unintended typo.
6666 if (refname[0] == '-')
6667 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6670 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6673 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6675 got_ref_list_free(&refs);
6679 err = got_ref_alloc(&ref, refname, id);
6683 err = got_ref_write(ref, repo);
6691 static const struct got_error *
6692 add_symref(struct got_repository *repo, const char *refname, const char *target)
6694 const struct got_error *err = NULL;
6695 struct got_reference *ref = NULL;
6696 struct got_reference *target_ref = NULL;
6699 * Don't let the user create a reference name with a leading '-'.
6700 * While technically a valid reference name, this case is usually
6701 * an unintended typo.
6703 if (refname[0] == '-')
6704 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6706 err = got_ref_open(&target_ref, repo, target, 0);
6710 err = got_ref_alloc_symref(&ref, refname, target_ref);
6714 err = got_ref_write(ref, repo);
6717 got_ref_close(target_ref);
6723 static const struct got_error *
6724 cmd_ref(int argc, char *argv[])
6726 const struct got_error *error = NULL;
6727 struct got_repository *repo = NULL;
6728 struct got_worktree *worktree = NULL;
6729 char *cwd = NULL, *repo_path = NULL;
6730 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6731 const char *obj_arg = NULL, *symref_target= NULL;
6732 char *refname = NULL, *keyword_idstr = NULL;
6733 int *pack_fds = NULL;
6736 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6737 "sendfd unveil", NULL) == -1)
6741 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6753 repo_path = realpath(optarg, NULL);
6754 if (repo_path == NULL)
6755 return got_error_from_errno2("realpath",
6757 got_path_strip_trailing_slashes(repo_path);
6760 symref_target = optarg;
6771 if (obj_arg && do_list)
6772 option_conflict('c', 'l');
6773 if (obj_arg && do_delete)
6774 option_conflict('c', 'd');
6775 if (obj_arg && symref_target)
6776 option_conflict('c', 's');
6777 if (symref_target && do_delete)
6778 option_conflict('s', 'd');
6779 if (symref_target && do_list)
6780 option_conflict('s', 'l');
6781 if (do_delete && do_list)
6782 option_conflict('d', 'l');
6783 if (sort_by_time && !do_list)
6784 errx(1, "-t option requires -l option");
6790 if (argc != 0 && argc != 1)
6793 refname = strdup(argv[0]);
6794 if (refname == NULL) {
6795 error = got_error_from_errno("strdup");
6802 refname = strdup(argv[0]);
6803 if (refname == NULL) {
6804 error = got_error_from_errno("strdup");
6810 got_path_strip_trailing_slashes(refname);
6812 cwd = getcwd(NULL, 0);
6814 error = got_error_from_errno("getcwd");
6818 error = got_repo_pack_fds_open(&pack_fds);
6822 if (repo_path == NULL) {
6823 error = got_worktree_open(&worktree, cwd,
6824 GOT_WORKTREE_GOT_DIR);
6825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6831 strdup(got_worktree_get_repo_path(worktree));
6832 if (repo_path == NULL)
6833 error = got_error_from_errno("strdup");
6837 repo_path = strdup(cwd);
6838 if (repo_path == NULL) {
6839 error = got_error_from_errno("strdup");
6845 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6851 /* Remove "cpath" promise. */
6852 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6858 error = apply_unveil(got_repo_get_path(repo), do_list,
6859 worktree ? got_worktree_get_root_path(worktree) : NULL);
6864 error = list_refs(repo, refname, sort_by_time);
6866 error = delete_ref_by_name(repo, refname);
6867 else if (symref_target)
6868 error = add_symref(repo, refname, symref_target);
6870 if (obj_arg == NULL)
6873 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6877 if (keyword_idstr != NULL)
6878 obj_arg = keyword_idstr;
6880 error = add_ref(repo, refname, obj_arg);
6885 const struct got_error *close_err = got_repo_close(repo);
6890 got_worktree_close(worktree);
6892 const struct got_error *pack_err =
6893 got_repo_pack_fds_close(pack_fds);
6899 free(keyword_idstr);
6906 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6907 "[-r repository-path] [name]\n", getprogname());
6911 static const struct got_error *
6912 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6913 struct got_reference *ref)
6915 const struct got_error *err = NULL;
6916 const char *refname;
6920 refname = got_ref_get_name(ref);
6921 if (worktree && strcmp(refname,
6922 got_worktree_get_head_ref_name(worktree)) == 0) {
6923 err = got_worktree_get_state(&marker, repo, worktree,
6924 check_cancelled, NULL);
6929 if (strncmp(refname, "refs/heads/", 11) == 0)
6931 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6933 if (strncmp(refname, "refs/remotes/", 13) == 0)
6936 refstr = got_ref_to_str(ref);
6938 return got_error_from_errno("got_ref_to_str");
6940 printf("%c %s: %s\n", marker, refname, refstr);
6945 static const struct got_error *
6946 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6948 const char *refname;
6950 if (worktree == NULL)
6951 return got_error(GOT_ERR_NOT_WORKTREE);
6953 refname = got_worktree_get_head_ref_name(worktree);
6955 if (strncmp(refname, "refs/heads/", 11) == 0)
6957 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6960 printf("%s\n", refname);
6965 static const struct got_error *
6966 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6969 static const struct got_error *err = NULL;
6970 struct got_reflist_head refs;
6971 struct got_reflist_entry *re;
6972 struct got_reference *temp_ref = NULL;
6973 int rebase_in_progress, histedit_in_progress;
6978 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6983 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6988 if (rebase_in_progress || histedit_in_progress) {
6989 err = got_ref_open(&temp_ref, repo,
6990 got_worktree_get_head_ref_name(worktree), 0);
6993 list_branch(repo, worktree, temp_ref);
6994 got_ref_close(temp_ref);
6998 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6999 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7004 TAILQ_FOREACH(re, &refs, entry)
7005 list_branch(repo, worktree, re->ref);
7007 got_ref_list_free(&refs);
7009 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
7010 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
7015 TAILQ_FOREACH(re, &refs, entry)
7016 list_branch(repo, worktree, re->ref);
7018 got_ref_list_free(&refs);
7023 static const struct got_error *
7024 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
7025 const char *branch_name)
7027 const struct got_error *err = NULL;
7028 struct got_reference *ref = NULL;
7029 char *refname, *remote_refname = NULL;
7031 if (strncmp(branch_name, "refs/", 5) == 0)
7033 if (strncmp(branch_name, "heads/", 6) == 0)
7035 else if (strncmp(branch_name, "remotes/", 8) == 0)
7038 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
7039 return got_error_from_errno("asprintf");
7041 if (asprintf(&remote_refname, "refs/remotes/%s",
7042 branch_name) == -1) {
7043 err = got_error_from_errno("asprintf");
7047 err = got_ref_open(&ref, repo, refname, 0);
7049 const struct got_error *err2;
7050 if (err->code != GOT_ERR_NOT_REF)
7053 * Keep 'err' intact such that if neither branch exists
7054 * we report "refs/heads" rather than "refs/remotes" in
7055 * our error message.
7057 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7064 strcmp(got_worktree_get_head_ref_name(worktree),
7065 got_ref_get_name(ref)) == 0) {
7066 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7067 "will not delete this work tree's current branch");
7071 err = delete_ref(repo, ref);
7076 free(remote_refname);
7080 static const struct got_error *
7081 add_branch(struct got_repository *repo, const char *branch_name,
7082 struct got_object_id *base_commit_id)
7084 const struct got_error *err = NULL;
7085 struct got_reference *ref = NULL;
7086 char *refname = NULL;
7089 * Don't let the user create a branch name with a leading '-'.
7090 * While technically a valid reference name, this case is usually
7091 * an unintended typo.
7093 if (branch_name[0] == '-')
7094 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7096 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7099 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7100 err = got_error_from_errno("asprintf");
7104 err = got_ref_open(&ref, repo, refname, 0);
7106 err = got_error(GOT_ERR_BRANCH_EXISTS);
7108 } else if (err->code != GOT_ERR_NOT_REF)
7111 err = got_ref_alloc(&ref, refname, base_commit_id);
7115 err = got_ref_write(ref, repo);
7123 static const struct got_error *
7124 cmd_branch(int argc, char *argv[])
7126 const struct got_error *error = NULL;
7127 struct got_repository *repo = NULL;
7128 struct got_worktree *worktree = NULL;
7129 char *cwd = NULL, *repo_path = NULL;
7130 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7131 const char *delref = NULL, *commit_id_arg = NULL;
7132 struct got_reference *ref = NULL;
7133 struct got_pathlist_head paths;
7134 struct got_object_id *commit_id = NULL;
7135 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7136 int *pack_fds = NULL;
7141 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7142 "sendfd unveil", NULL) == -1)
7146 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7149 commit_id_arg = optarg;
7161 repo_path = realpath(optarg, NULL);
7162 if (repo_path == NULL)
7163 return got_error_from_errno2("realpath",
7165 got_path_strip_trailing_slashes(repo_path);
7176 if (do_list && delref)
7177 option_conflict('l', 'd');
7178 if (sort_by_time && !do_list)
7179 errx(1, "-t option requires -l option");
7184 if (!do_list && !delref && argc == 0)
7187 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7188 errx(1, "-c option can only be used when creating a branch");
7190 if (do_list || delref) {
7193 } else if (!do_show && argc != 1)
7196 cwd = getcwd(NULL, 0);
7198 error = got_error_from_errno("getcwd");
7202 error = got_repo_pack_fds_open(&pack_fds);
7206 if (repo_path == NULL) {
7207 error = got_worktree_open(&worktree, cwd,
7208 GOT_WORKTREE_GOT_DIR);
7209 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7215 strdup(got_worktree_get_repo_path(worktree));
7216 if (repo_path == NULL)
7217 error = got_error_from_errno("strdup");
7221 repo_path = strdup(cwd);
7222 if (repo_path == NULL) {
7223 error = got_error_from_errno("strdup");
7229 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7234 if (do_list || do_show) {
7235 /* Remove "cpath" promise. */
7236 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7242 error = apply_unveil(got_repo_get_path(repo), do_list,
7243 worktree ? got_worktree_get_root_path(worktree) : NULL);
7248 error = show_current_branch(repo, worktree);
7250 error = list_branches(repo, worktree, sort_by_time);
7252 error = delete_branch(repo, worktree, delref);
7254 struct got_reflist_head refs;
7256 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7260 if (commit_id_arg == NULL)
7261 commit_id_arg = worktree ?
7262 got_worktree_get_head_ref_name(worktree) :
7265 error = got_keyword_to_idstr(&keyword_idstr,
7266 commit_id_arg, repo, worktree);
7269 if (keyword_idstr != NULL)
7270 commit_id_arg = keyword_idstr;
7272 error = got_repo_match_object_id(&commit_id, NULL,
7273 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7274 got_ref_list_free(&refs);
7277 error = add_branch(repo, argv[0], commit_id);
7280 if (worktree && do_update) {
7281 struct got_update_progress_arg upa;
7282 char *branch_refname = NULL;
7284 error = got_object_id_str(&commit_id_str, commit_id);
7287 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7291 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7293 error = got_error_from_errno("asprintf");
7296 error = got_ref_open(&ref, repo, branch_refname, 0);
7297 free(branch_refname);
7300 error = switch_head_ref(ref, commit_id, worktree,
7304 error = got_worktree_set_base_commit_id(worktree, repo,
7308 memset(&upa, 0, sizeof(upa));
7309 error = got_worktree_checkout_files(worktree, &paths,
7310 repo, update_progress, &upa, check_cancelled,
7314 if (upa.did_something) {
7315 printf("Updated to %s: %s\n",
7316 got_worktree_get_head_ref_name(worktree),
7319 print_update_progress_stats(&upa);
7323 free(keyword_idstr);
7327 const struct got_error *close_err = got_repo_close(repo);
7332 got_worktree_close(worktree);
7334 const struct got_error *pack_err =
7335 got_repo_pack_fds_close(pack_fds);
7342 free(commit_id_str);
7343 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7351 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7352 "[-r repository-path] [-s signer-id] name\n", getprogname());
7357 static const struct got_error *
7358 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7360 const struct got_error *err = NULL;
7361 struct got_reflist_entry *re, *se, *new;
7362 struct got_object_id *re_id, *se_id;
7363 struct got_tag_object *re_tag, *se_tag;
7364 time_t re_time, se_time;
7366 STAILQ_FOREACH(re, tags, entry) {
7367 se = STAILQ_FIRST(sorted);
7369 err = got_reflist_entry_dup(&new, re);
7372 STAILQ_INSERT_HEAD(sorted, new, entry);
7375 err = got_ref_resolve(&re_id, repo, re->ref);
7378 err = got_object_open_as_tag(&re_tag, repo, re_id);
7382 re_time = got_object_tag_get_tagger_time(re_tag);
7383 got_object_tag_close(re_tag);
7387 err = got_ref_resolve(&se_id, repo, re->ref);
7390 err = got_object_open_as_tag(&se_tag, repo, se_id);
7394 se_time = got_object_tag_get_tagger_time(se_tag);
7395 got_object_tag_close(se_tag);
7397 if (se_time > re_time) {
7398 err = got_reflist_entry_dup(&new, re);
7401 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7404 se = STAILQ_NEXT(se, entry);
7413 static const struct got_error *
7414 get_tag_refname(char **refname, const char *tag_name)
7416 const struct got_error *err;
7418 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7419 *refname = strdup(tag_name);
7420 if (*refname == NULL)
7421 return got_error_from_errno("strdup");
7422 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7423 err = got_error_from_errno("asprintf");
7431 static const struct got_error *
7432 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7433 const char *allowed_signers, const char *revoked_signers, int verbosity)
7435 static const struct got_error *err = NULL;
7436 struct got_reflist_head refs;
7437 struct got_reflist_entry *re;
7438 char *wanted_refname = NULL;
7443 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7448 struct got_reference *ref;
7449 err = get_tag_refname(&wanted_refname, tag_name);
7452 /* Wanted tag reference should exist. */
7453 err = got_ref_open(&ref, repo, wanted_refname, 0);
7459 TAILQ_FOREACH(re, &refs, entry) {
7460 const char *refname;
7461 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7463 const char *tagger, *ssh_sig = NULL;
7464 char *sig_msg = NULL;
7466 struct got_object_id *id;
7467 struct got_tag_object *tag;
7468 struct got_commit_object *commit = NULL;
7470 refname = got_ref_get_name(re->ref);
7471 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7472 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7475 refstr = got_ref_to_str(re->ref);
7476 if (refstr == NULL) {
7477 err = got_error_from_errno("got_ref_to_str");
7481 err = got_ref_resolve(&id, repo, re->ref);
7484 err = got_object_open_as_tag(&tag, repo, id);
7486 if (err->code != GOT_ERR_OBJ_TYPE) {
7490 /* "lightweight" tag */
7491 err = got_object_open_as_commit(&commit, repo, id);
7496 tagger = got_object_commit_get_committer(commit);
7498 got_object_commit_get_committer_time(commit);
7499 err = got_object_id_str(&id_str, id);
7505 tagger = got_object_tag_get_tagger(tag);
7506 tagger_time = got_object_tag_get_tagger_time(tag);
7507 err = got_object_id_str(&id_str,
7508 got_object_tag_get_object_id(tag));
7513 if (tag && verify_tags) {
7514 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7515 got_object_tag_get_message(tag));
7516 if (ssh_sig && allowed_signers == NULL) {
7517 err = got_error_msg(
7518 GOT_ERR_VERIFY_TAG_SIGNATURE,
7519 "SSH signature verification requires "
7520 "setting allowed_signers in "
7526 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7528 printf("from: %s\n", tagger);
7529 datestr = get_datestr(&tagger_time, datebuf);
7531 printf("date: %s UTC\n", datestr);
7533 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7535 switch (got_object_tag_get_object_type(tag)) {
7536 case GOT_OBJ_TYPE_BLOB:
7537 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7540 case GOT_OBJ_TYPE_TREE:
7541 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7544 case GOT_OBJ_TYPE_COMMIT:
7545 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7548 case GOT_OBJ_TYPE_TAG:
7549 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7559 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7560 allowed_signers, revoked_signers, verbosity);
7561 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7565 printf("signature: %s", sig_msg);
7571 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7574 got_object_commit_close(commit);
7576 tagmsg0 = strdup(got_object_tag_get_message(tag));
7577 got_object_tag_close(tag);
7578 if (tagmsg0 == NULL) {
7579 err = got_error_from_errno("strdup");
7586 line = strsep(&tagmsg, "\n");
7588 printf(" %s\n", line);
7593 got_ref_list_free(&refs);
7594 free(wanted_refname);
7596 if (err == NULL && bad_sigs)
7597 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7601 static const struct got_error *
7602 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7603 const char *tag_name, const char *editor, const char *repo_path)
7605 const struct got_error *err = NULL;
7606 char *template = NULL, *initial_content = NULL;
7607 int initial_content_len;
7610 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7611 err = got_error_from_errno("asprintf");
7615 initial_content_len = asprintf(&initial_content,
7616 "\n# tagging commit %s as %s\n",
7617 commit_id_str, tag_name);
7618 if (initial_content_len == -1) {
7619 err = got_error_from_errno("asprintf");
7623 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7627 if (write(fd, initial_content, initial_content_len) == -1) {
7628 err = got_error_from_errno2("write", *tagmsg_path);
7631 if (close(fd) == -1) {
7632 err = got_error_from_errno2("close", *tagmsg_path);
7637 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7638 initial_content_len, 1);
7640 free(initial_content);
7643 if (fd != -1 && close(fd) == -1 && err == NULL)
7644 err = got_error_from_errno2("close", *tagmsg_path);
7653 static const struct got_error *
7654 add_tag(struct got_repository *repo, const char *tagger,
7655 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7656 const char *signer_id, const char *editor, int verbosity)
7658 const struct got_error *err = NULL;
7659 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7660 char *label = NULL, *commit_id_str = NULL;
7661 struct got_reference *ref = NULL;
7662 char *refname = NULL, *tagmsg = NULL;
7663 char *tagmsg_path = NULL, *tag_id_str = NULL;
7664 int preserve_tagmsg = 0;
7665 struct got_reflist_head refs;
7670 * Don't let the user create a tag name with a leading '-'.
7671 * While technically a valid reference name, this case is usually
7672 * an unintended typo.
7674 if (tag_name[0] == '-')
7675 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7677 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7681 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7682 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7686 err = got_object_id_str(&commit_id_str, commit_id);
7690 err = get_tag_refname(&refname, tag_name);
7693 if (strncmp("refs/tags/", tag_name, 10) == 0)
7696 err = got_ref_open(&ref, repo, refname, 0);
7698 err = got_error(GOT_ERR_TAG_EXISTS);
7700 } else if (err->code != GOT_ERR_NOT_REF)
7703 if (tagmsg_arg == NULL) {
7704 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7705 tag_name, editor, got_repo_get_path(repo));
7707 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7708 tagmsg_path != NULL)
7709 preserve_tagmsg = 1;
7714 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7715 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7719 preserve_tagmsg = 1;
7723 err = got_ref_alloc(&ref, refname, tag_id);
7726 preserve_tagmsg = 1;
7730 err = got_ref_write(ref, repo);
7733 preserve_tagmsg = 1;
7737 err = got_object_id_str(&tag_id_str, tag_id);
7740 preserve_tagmsg = 1;
7743 printf("Created tag %s\n", tag_id_str);
7745 if (preserve_tagmsg) {
7746 fprintf(stderr, "%s: tag message preserved in %s\n",
7747 getprogname(), tagmsg_path);
7748 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7749 err = got_error_from_errno2("unlink", tagmsg_path);
7754 free(commit_id_str);
7758 got_ref_list_free(&refs);
7762 static const struct got_error *
7763 cmd_tag(int argc, char *argv[])
7765 const struct got_error *error = NULL;
7766 struct got_repository *repo = NULL;
7767 struct got_worktree *worktree = NULL;
7768 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7769 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7770 char *allowed_signers = NULL, *revoked_signers = NULL, *editor = NULL;
7771 const char *signer_id = NULL;
7772 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7773 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7774 int *pack_fds = NULL;
7777 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7778 "sendfd unveil", NULL) == -1)
7782 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7785 commit_id_arg = optarg;
7794 repo_path = realpath(optarg, NULL);
7795 if (repo_path == NULL) {
7796 error = got_error_from_errno2("realpath",
7800 got_path_strip_trailing_slashes(repo_path);
7811 else if (verbosity < 3)
7823 if (do_list || verify_tags) {
7824 if (commit_id_arg != NULL)
7826 "-c option can only be used when creating a tag");
7829 option_conflict('l', 'm');
7831 option_conflict('V', 'm');
7835 option_conflict('l', 's');
7837 option_conflict('V', 's');
7841 } else if (argc != 1)
7847 cwd = getcwd(NULL, 0);
7849 error = got_error_from_errno("getcwd");
7853 error = got_repo_pack_fds_open(&pack_fds);
7857 if (repo_path == NULL) {
7858 error = got_worktree_open(&worktree, cwd,
7859 GOT_WORKTREE_GOT_DIR);
7860 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7866 strdup(got_worktree_get_repo_path(worktree));
7867 if (repo_path == NULL)
7868 error = got_error_from_errno("strdup");
7872 repo_path = strdup(cwd);
7873 if (repo_path == NULL) {
7874 error = got_error_from_errno("strdup");
7880 if (do_list || verify_tags) {
7881 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7884 error = get_allowed_signers(&allowed_signers, repo, worktree);
7887 error = get_revoked_signers(&revoked_signers, repo, worktree);
7891 /* Release work tree lock. */
7892 got_worktree_close(worktree);
7897 * Remove "cpath" promise unless needed for signature tmpfile
7901 got_sigs_apply_unveil();
7904 if (pledge("stdio rpath wpath flock proc exec sendfd "
7905 "unveil", NULL) == -1)
7909 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7912 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7913 revoked_signers, verbosity);
7915 error = get_gitconfig_path(&gitconfig_path);
7918 error = got_repo_open(&repo, repo_path, gitconfig_path,
7923 error = get_author(&tagger, repo, worktree);
7926 if (signer_id == NULL)
7927 signer_id = get_signer_id(repo, worktree);
7929 if (tagmsg == NULL) {
7930 error = get_editor(&editor);
7933 if (unveil(editor, "x") != 0) {
7934 error = got_error_from_errno2("unveil", editor);
7939 error = got_sigs_apply_unveil();
7943 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7947 if (commit_id_arg == NULL) {
7948 struct got_reference *head_ref;
7949 struct got_object_id *commit_id;
7950 error = got_ref_open(&head_ref, repo,
7951 worktree ? got_worktree_get_head_ref_name(worktree)
7955 error = got_ref_resolve(&commit_id, repo, head_ref);
7956 got_ref_close(head_ref);
7959 error = got_object_id_str(&commit_id_str, commit_id);
7964 error = got_keyword_to_idstr(&keyword_idstr,
7965 commit_id_arg, repo, worktree);
7968 commit_id_str = keyword_idstr;
7972 /* Release work tree lock. */
7973 got_worktree_close(worktree);
7977 error = add_tag(repo, tagger, tag_name,
7978 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7979 signer_id, editor, verbosity);
7983 const struct got_error *close_err = got_repo_close(repo);
7988 got_worktree_close(worktree);
7990 const struct got_error *pack_err =
7991 got_repo_pack_fds_close(pack_fds);
7998 free(gitconfig_path);
7999 free(commit_id_str);
8001 free(allowed_signers);
8002 free(revoked_signers);
8009 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
8013 static const struct got_error *
8014 add_progress(void *arg, unsigned char status, const char *path)
8016 while (path[0] == '/')
8018 printf("%c %s\n", status, path);
8022 static const struct got_error *
8023 cmd_add(int argc, char *argv[])
8025 const struct got_error *error = NULL;
8026 struct got_repository *repo = NULL;
8027 struct got_worktree *worktree = NULL;
8029 struct got_pathlist_head paths;
8030 struct got_pathlist_entry *pe;
8031 int ch, can_recurse = 0, no_ignores = 0;
8032 int *pack_fds = NULL;
8037 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8042 while ((ch = getopt(argc, argv, "IR")) != -1) {
8062 cwd = getcwd(NULL, 0);
8064 error = got_error_from_errno("getcwd");
8068 error = got_repo_pack_fds_open(&pack_fds);
8072 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8074 if (error->code == GOT_ERR_NOT_WORKTREE)
8075 error = wrap_not_worktree_error(error, "add", cwd);
8079 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8084 error = apply_unveil(got_repo_get_path(repo), 1,
8085 got_worktree_get_root_path(worktree));
8089 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8096 TAILQ_FOREACH(pe, &paths, entry) {
8097 if (asprintf(&ondisk_path, "%s/%s",
8098 got_worktree_get_root_path(worktree),
8100 error = got_error_from_errno("asprintf");
8103 if (lstat(ondisk_path, &sb) == -1) {
8104 if (errno == ENOENT) {
8108 error = got_error_from_errno2("lstat",
8114 if (S_ISDIR(sb.st_mode)) {
8115 error = got_error_msg(GOT_ERR_BAD_PATH,
8116 "adding directories requires -R option");
8122 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8123 NULL, repo, no_ignores);
8126 const struct got_error *close_err = got_repo_close(repo);
8131 got_worktree_close(worktree);
8133 const struct got_error *pack_err =
8134 got_repo_pack_fds_close(pack_fds);
8138 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8146 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8151 static const struct got_error *
8152 print_remove_status(void *arg, unsigned char status,
8153 unsigned char staged_status, const char *path)
8155 while (path[0] == '/')
8157 if (status == GOT_STATUS_NONEXISTENT)
8159 if (status == staged_status && (status == GOT_STATUS_DELETE))
8160 status = GOT_STATUS_NO_CHANGE;
8161 printf("%c%c %s\n", status, staged_status, path);
8165 static const struct got_error *
8166 cmd_remove(int argc, char *argv[])
8168 const struct got_error *error = NULL;
8169 struct got_worktree *worktree = NULL;
8170 struct got_repository *repo = NULL;
8171 const char *status_codes = NULL;
8173 struct got_pathlist_head paths;
8174 struct got_pathlist_entry *pe;
8175 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8176 int ignore_missing_paths = 0;
8177 int *pack_fds = NULL;
8182 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8187 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8190 delete_local_mods = 1;
8191 ignore_missing_paths = 1;
8200 for (i = 0; optarg[i] != '\0'; i++) {
8201 switch (optarg[i]) {
8202 case GOT_STATUS_MODIFY:
8203 delete_local_mods = 1;
8205 case GOT_STATUS_MISSING:
8206 ignore_missing_paths = 1;
8209 errx(1, "invalid status code '%c'",
8213 status_codes = optarg;
8227 cwd = getcwd(NULL, 0);
8229 error = got_error_from_errno("getcwd");
8233 error = got_repo_pack_fds_open(&pack_fds);
8237 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8239 if (error->code == GOT_ERR_NOT_WORKTREE)
8240 error = wrap_not_worktree_error(error, "remove", cwd);
8244 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8249 error = apply_unveil(got_repo_get_path(repo), 1,
8250 got_worktree_get_root_path(worktree));
8254 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8261 TAILQ_FOREACH(pe, &paths, entry) {
8262 if (asprintf(&ondisk_path, "%s/%s",
8263 got_worktree_get_root_path(worktree),
8265 error = got_error_from_errno("asprintf");
8268 if (lstat(ondisk_path, &sb) == -1) {
8269 if (errno == ENOENT) {
8273 error = got_error_from_errno2("lstat",
8279 if (S_ISDIR(sb.st_mode)) {
8280 error = got_error_msg(GOT_ERR_BAD_PATH,
8281 "removing directories requires -R option");
8287 error = got_worktree_schedule_delete(worktree, &paths,
8288 delete_local_mods, status_codes, print_remove_status, NULL,
8289 repo, keep_on_disk, ignore_missing_paths);
8292 const struct got_error *close_err = got_repo_close(repo);
8297 got_worktree_close(worktree);
8299 const struct got_error *pack_err =
8300 got_repo_pack_fds_close(pack_fds);
8304 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8312 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8313 "[patchfile]\n", getprogname());
8317 static const struct got_error *
8318 patch_from_stdin(int *patchfd)
8320 const struct got_error *err = NULL;
8323 sig_t sighup, sigint, sigquit;
8325 *patchfd = got_opentempfd();
8327 return got_error_from_errno("got_opentempfd");
8329 sighup = signal(SIGHUP, SIG_DFL);
8330 sigint = signal(SIGINT, SIG_DFL);
8331 sigquit = signal(SIGQUIT, SIG_DFL);
8334 r = read(0, buf, sizeof(buf));
8336 err = got_error_from_errno("read");
8341 if (write(*patchfd, buf, r) == -1) {
8342 err = got_error_from_errno("write");
8347 signal(SIGHUP, sighup);
8348 signal(SIGINT, sigint);
8349 signal(SIGQUIT, sigquit);
8351 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8352 err = got_error_from_errno("lseek");
8362 struct got_patch_progress_arg {
8368 static const struct got_error *
8369 patch_progress(void *arg, const char *old, const char *new,
8370 unsigned char status, const struct got_error *error, int old_from,
8371 int old_lines, int new_from, int new_lines, int offset,
8372 int ws_mangled, const struct got_error *hunk_err)
8374 const char *path = new == NULL ? old : new;
8375 struct got_patch_progress_arg *a = arg;
8377 while (*path == '/')
8380 if (status != GOT_STATUS_NO_CHANGE &&
8381 status != 0 /* per-hunk progress */) {
8382 printf("%c %s\n", status, path);
8383 a->did_something = 1;
8386 if (hunk_err == NULL) {
8387 if (status == GOT_STATUS_CANNOT_UPDATE)
8389 else if (status == GOT_STATUS_CONFLICT)
8394 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8396 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8397 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8398 old_lines, new_from, new_lines);
8399 if (hunk_err != NULL)
8400 printf("%s\n", hunk_err->msg);
8401 else if (offset != 0)
8402 printf("applied with offset %d\n", offset);
8404 printf("hunk contains mangled whitespace\n");
8411 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8413 if (!ppa->did_something)
8416 if (ppa->conflicts > 0)
8417 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8419 if (ppa->rejects > 0) {
8420 printf("Files where patch failed to apply: %d\n",
8425 static const struct got_error *
8426 cmd_patch(int argc, char *argv[])
8428 const struct got_error *error = NULL, *close_error = NULL;
8429 struct got_worktree *worktree = NULL;
8430 struct got_repository *repo = NULL;
8431 struct got_reflist_head refs;
8432 struct got_object_id *commit_id = NULL;
8433 const char *commit_id_str = NULL;
8436 char *cwd = NULL, *keyword_idstr = NULL;
8437 int ch, nop = 0, strip = -1, reverse = 0;
8439 int *pack_fds = NULL;
8440 struct got_patch_progress_arg ppa;
8445 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8446 "unveil", NULL) == -1)
8450 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8453 commit_id_str = optarg;
8459 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8461 errx(1, "pathname strip count is %s: %s",
8477 error = patch_from_stdin(&patchfd);
8480 } else if (argc == 1) {
8481 patchfd = open(argv[0], O_RDONLY);
8482 if (patchfd == -1) {
8483 error = got_error_from_errno2("open", argv[0]);
8486 if (fstat(patchfd, &sb) == -1) {
8487 error = got_error_from_errno2("fstat", argv[0]);
8490 if (!S_ISREG(sb.st_mode)) {
8491 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8497 if ((cwd = getcwd(NULL, 0)) == NULL) {
8498 error = got_error_from_errno("getcwd");
8502 error = got_repo_pack_fds_open(&pack_fds);
8506 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8510 const char *repo_path = got_worktree_get_repo_path(worktree);
8511 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8515 error = apply_unveil(got_repo_get_path(repo), 0,
8516 got_worktree_get_root_path(worktree));
8520 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8524 if (commit_id_str != NULL) {
8525 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8530 error = got_repo_match_object_id(&commit_id, NULL,
8531 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8532 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8537 memset(&ppa, 0, sizeof(ppa));
8538 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8539 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8540 print_patch_progress_stats(&ppa);
8542 got_ref_list_free(&refs);
8543 free(keyword_idstr);
8546 close_error = got_repo_close(repo);
8548 error = close_error;
8550 if (worktree != NULL) {
8551 close_error = got_worktree_close(worktree);
8553 error = close_error;
8556 const struct got_error *pack_err =
8557 got_repo_pack_fds_close(pack_fds);
8568 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8573 static const struct got_error *
8574 revert_progress(void *arg, unsigned char status, const char *path)
8576 if (status == GOT_STATUS_UNVERSIONED)
8579 while (path[0] == '/')
8581 printf("%c %s\n", status, path);
8585 struct choose_patch_arg {
8586 FILE *patch_script_file;
8590 static const struct got_error *
8591 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8592 int nchanges, const char *action)
8594 const struct got_error *err;
8596 size_t linesize = 0;
8600 case GOT_STATUS_ADD:
8601 printf("A %s\n%s this addition? [y/n] ", path, action);
8603 case GOT_STATUS_DELETE:
8604 printf("D %s\n%s this deletion? [y/n] ", path, action);
8606 case GOT_STATUS_MODIFY:
8607 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8608 return got_error_from_errno("fseek");
8609 printf(GOT_COMMIT_SEP_STR);
8610 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8612 if (linelen == -1 && ferror(patch_file)) {
8613 err = got_error_from_errno("getline");
8618 printf(GOT_COMMIT_SEP_STR);
8619 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8620 path, n, nchanges, action);
8623 return got_error_path(path, GOT_ERR_FILE_STATUS);
8630 static const struct got_error *
8631 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8632 FILE *patch_file, int n, int nchanges)
8634 const struct got_error *err = NULL;
8636 size_t linesize = 0;
8639 struct choose_patch_arg *a = arg;
8641 *choice = GOT_PATCH_CHOICE_NONE;
8643 if (a->patch_script_file) {
8645 err = show_change(status, path, patch_file, n, nchanges,
8649 linelen = getline(&line, &linesize, a->patch_script_file);
8650 if (linelen == -1) {
8651 if (ferror(a->patch_script_file))
8652 return got_error_from_errno("getline");
8655 nl = strchr(line, '\n');
8658 if (strcmp(line, "y") == 0) {
8659 *choice = GOT_PATCH_CHOICE_YES;
8661 } else if (strcmp(line, "n") == 0) {
8662 *choice = GOT_PATCH_CHOICE_NO;
8664 } else if (strcmp(line, "q") == 0 &&
8665 status == GOT_STATUS_MODIFY) {
8666 *choice = GOT_PATCH_CHOICE_QUIT;
8669 printf("invalid response '%s'\n", line);
8674 while (resp != 'y' && resp != 'n' && resp != 'q') {
8675 err = show_change(status, path, patch_file, n, nchanges,
8682 if (status == GOT_STATUS_MODIFY) {
8683 if (resp != 'y' && resp != 'n' && resp != 'q') {
8684 printf("invalid response '%c'\n", resp);
8687 } else if (resp != 'y' && resp != 'n') {
8688 printf("invalid response '%c'\n", resp);
8694 *choice = GOT_PATCH_CHOICE_YES;
8695 else if (resp == 'n')
8696 *choice = GOT_PATCH_CHOICE_NO;
8697 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8698 *choice = GOT_PATCH_CHOICE_QUIT;
8703 struct wt_commitable_path_arg {
8704 struct got_pathlist_head *commit_paths;
8709 * Shortcut work tree status callback to determine if the set of paths scanned
8710 * has at least one versioned path that is being modified and, if not NULL, is
8711 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8712 * soon as a path is passed with a status that satisfies this criteria.
8714 static const struct got_error *
8715 worktree_has_commitable_path(void *arg, unsigned char status,
8716 unsigned char staged_status, const char *path,
8717 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8718 struct got_object_id *commit_id, int dirfd, const char *de_name)
8720 struct wt_commitable_path_arg *a = arg;
8722 if (status == staged_status && (status == GOT_STATUS_DELETE))
8723 status = GOT_STATUS_NO_CHANGE;
8725 if (!(status == GOT_STATUS_NO_CHANGE ||
8726 status == GOT_STATUS_UNVERSIONED) ||
8727 staged_status != GOT_STATUS_NO_CHANGE) {
8728 if (a->commit_paths != NULL) {
8729 struct got_pathlist_entry *pe;
8731 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8732 if (strncmp(path, pe->path,
8733 pe->path_len) == 0) {
8734 *a->has_changes = 1;
8739 *a->has_changes = 1;
8741 if (*a->has_changes)
8742 return got_error(GOT_ERR_FILE_MODIFIED);
8749 * Check that the changeset of the commit identified by id is
8750 * comprised of at least one modified path that is being committed.
8752 static const struct got_error *
8753 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8754 struct got_object_id *id, struct got_worktree *worktree,
8755 struct got_repository *repo)
8757 const struct got_error *err;
8758 struct got_pathlist_head paths;
8759 struct got_commit_object *commit = NULL, *pcommit = NULL;
8760 struct got_tree_object *tree = NULL, *ptree = NULL;
8761 struct got_object_qid *pid;
8765 err = got_object_open_as_commit(&commit, repo, id);
8769 err = got_object_open_as_tree(&tree, repo,
8770 got_object_commit_get_tree_id(commit));
8774 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8776 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8780 err = got_object_open_as_tree(&ptree, repo,
8781 got_object_commit_get_tree_id(pcommit));
8786 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8787 got_diff_tree_collect_changed_paths, &paths, 0);
8791 err = got_worktree_status(worktree, &paths, repo, 0,
8792 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8793 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8795 * At least one changed path in the referenced commit is
8796 * modified in the work tree, that's all we need to know!
8802 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8804 got_object_commit_close(commit);
8806 got_object_commit_close(pcommit);
8808 got_object_tree_close(tree);
8810 got_object_tree_close(ptree);
8815 * Remove any "logmsg" reference comprised entirely of paths that have
8816 * been reverted in this work tree. If any path in the logmsg ref changeset
8817 * remains in a changed state in the worktree, do not remove the reference.
8819 static const struct got_error *
8820 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8822 const struct got_error *err;
8823 struct got_reflist_head refs;
8824 struct got_reflist_entry *re;
8825 struct got_commit_object *commit = NULL;
8826 struct got_object_id *commit_id = NULL;
8827 struct wt_commitable_path_arg wcpa;
8828 char *uuidstr = NULL;
8832 err = got_worktree_get_uuid(&uuidstr, worktree);
8836 err = got_ref_list(&refs, repo, "refs/got/worktree",
8837 got_ref_cmp_by_name, repo);
8841 TAILQ_FOREACH(re, &refs, entry) {
8842 const char *refname;
8843 int has_changes = 0;
8845 refname = got_ref_get_name(re->ref);
8847 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8848 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8849 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8850 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8851 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8852 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8856 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8857 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8861 err = got_repo_match_object_id(&commit_id, NULL, refname,
8862 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8866 err = got_object_open_as_commit(&commit, repo, commit_id);
8870 wcpa.commit_paths = NULL;
8871 wcpa.has_changes = &has_changes;
8873 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8879 err = got_ref_delete(re->ref, repo);
8884 got_object_commit_close(commit);
8893 got_ref_list_free(&refs);
8895 got_object_commit_close(commit);
8899 static const struct got_error *
8900 cmd_revert(int argc, char *argv[])
8902 const struct got_error *error = NULL;
8903 struct got_worktree *worktree = NULL;
8904 struct got_repository *repo = NULL;
8905 char *cwd = NULL, *path = NULL;
8906 struct got_pathlist_head paths;
8907 struct got_pathlist_entry *pe;
8908 int ch, can_recurse = 0, pflag = 0;
8909 FILE *patch_script_file = NULL;
8910 const char *patch_script_path = NULL;
8911 struct choose_patch_arg cpa;
8912 int *pack_fds = NULL;
8917 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8918 "unveil", NULL) == -1)
8922 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8925 patch_script_path = optarg;
8944 if (patch_script_path && !pflag)
8945 errx(1, "-F option can only be used together with -p option");
8947 cwd = getcwd(NULL, 0);
8949 error = got_error_from_errno("getcwd");
8953 error = got_repo_pack_fds_open(&pack_fds);
8957 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8959 if (error->code == GOT_ERR_NOT_WORKTREE)
8960 error = wrap_not_worktree_error(error, "revert", cwd);
8964 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8969 if (patch_script_path) {
8970 patch_script_file = fopen(patch_script_path, "re");
8971 if (patch_script_file == NULL) {
8972 error = got_error_from_errno2("fopen",
8979 * XXX "c" perm needed on repo dir to delete merge references.
8981 error = apply_unveil(got_repo_get_path(repo), 0,
8982 got_worktree_get_root_path(worktree));
8986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8993 TAILQ_FOREACH(pe, &paths, entry) {
8994 if (asprintf(&ondisk_path, "%s/%s",
8995 got_worktree_get_root_path(worktree),
8997 error = got_error_from_errno("asprintf");
9000 if (lstat(ondisk_path, &sb) == -1) {
9001 if (errno == ENOENT) {
9005 error = got_error_from_errno2("lstat",
9011 if (S_ISDIR(sb.st_mode)) {
9012 error = got_error_msg(GOT_ERR_BAD_PATH,
9013 "reverting directories requires -R option");
9019 cpa.patch_script_file = patch_script_file;
9020 cpa.action = "revert";
9021 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
9022 pflag ? choose_patch : NULL, &cpa, repo);
9024 error = rm_logmsg_ref(worktree, repo);
9026 if (patch_script_file && fclose(patch_script_file) == EOF &&
9028 error = got_error_from_errno2("fclose", patch_script_path);
9030 const struct got_error *close_err = got_repo_close(repo);
9035 got_worktree_close(worktree);
9037 const struct got_error *pack_err =
9038 got_repo_pack_fds_close(pack_fds);
9042 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9051 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
9052 "[-m message] [path ...]\n", getprogname());
9056 struct collect_commit_logmsg_arg {
9057 const char *cmdline_log;
9058 const char *prepared_log;
9059 const char *merged_log;
9060 int non_interactive;
9062 const char *worktree_path;
9063 const char *branch_name;
9064 const char *repo_path;
9069 static const struct got_error *
9070 read_prepared_logmsg(char **logmsg, const char *path)
9072 const struct got_error *err = NULL;
9078 memset(&sb, 0, sizeof(sb));
9080 f = fopen(path, "re");
9082 return got_error_from_errno2("fopen", path);
9084 if (fstat(fileno(f), &sb) == -1) {
9085 err = got_error_from_errno2("fstat", path);
9088 if (sb.st_size == 0) {
9089 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
9093 *logmsg = malloc(sb.st_size + 1);
9094 if (*logmsg == NULL) {
9095 err = got_error_from_errno("malloc");
9099 r = fread(*logmsg, 1, sb.st_size, f);
9100 if (r != sb.st_size) {
9102 err = got_error_from_errno2("fread", path);
9104 err = got_error(GOT_ERR_IO);
9107 (*logmsg)[sb.st_size] = '\0';
9109 if (fclose(f) == EOF && err == NULL)
9110 err = got_error_from_errno2("fclose", path);
9118 static const struct got_error *
9119 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9120 const char *diff_path, char **logmsg, void *arg)
9122 char *initial_content = NULL;
9123 struct got_pathlist_entry *pe;
9124 const struct got_error *err = NULL;
9125 char *template = NULL;
9126 char *prepared_msg = NULL, *merged_msg = NULL;
9127 struct collect_commit_logmsg_arg *a = arg;
9128 int initial_content_len;
9132 /* if a message was specified on the command line, just use it */
9133 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9134 len = strlen(a->cmdline_log) + 1;
9135 *logmsg = malloc(len + 1);
9136 if (*logmsg == NULL)
9137 return got_error_from_errno("malloc");
9138 strlcpy(*logmsg, a->cmdline_log, len);
9140 } else if (a->prepared_log != NULL && a->non_interactive)
9141 return read_prepared_logmsg(logmsg, a->prepared_log);
9143 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9144 return got_error_from_errno("asprintf");
9146 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9150 if (a->prepared_log) {
9151 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9154 } else if (a->merged_log) {
9155 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9160 initial_content_len = asprintf(&initial_content,
9161 "%s%s\n# changes to be committed on branch %s:\n",
9162 prepared_msg ? prepared_msg : "",
9163 merged_msg ? merged_msg : "", a->branch_name);
9164 if (initial_content_len == -1) {
9165 err = got_error_from_errno("asprintf");
9169 if (write(fd, initial_content, initial_content_len) == -1) {
9170 err = got_error_from_errno2("write", a->logmsg_path);
9174 TAILQ_FOREACH(pe, commitable_paths, entry) {
9175 struct got_commitable *ct = pe->data;
9176 dprintf(fd, "# %c %s\n",
9177 got_commitable_get_status(ct),
9178 got_commitable_get_path(ct));
9182 dprintf(fd, "# detailed changes can be viewed in %s\n",
9186 if (close(fd) == -1) {
9187 err = got_error_from_errno2("close", a->logmsg_path);
9192 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9193 initial_content_len, a->prepared_log ? 0 : 1);
9195 free(initial_content);
9200 if (fd != -1 && close(fd) == -1 && err == NULL)
9201 err = got_error_from_errno2("close", a->logmsg_path);
9209 static const struct got_error *
9210 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9211 const char *type, int has_content)
9213 const struct got_error *err = NULL;
9214 char *logmsg = NULL;
9216 err = got_object_commit_get_logmsg(&logmsg, commit);
9220 if (fprintf(f, "%s# log message of %s commit %s:%s",
9221 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9222 err = got_ferror(f, GOT_ERR_IO);
9229 * Lookup "logmsg" references of backed-out and cherrypicked commits
9230 * belonging to the current work tree. If found, and the worktree has
9231 * at least one modified file that was changed in the referenced commit,
9232 * add its log message to a new temporary file at *logmsg_path.
9233 * Add all refs found to matched_refs to be scheduled for removal on
9234 * successful commit.
9236 static const struct got_error *
9237 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9238 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9239 struct got_repository *repo)
9241 const struct got_error *err;
9242 struct got_commit_object *commit = NULL;
9243 struct got_object_id *id = NULL;
9244 struct got_reflist_head refs;
9245 struct got_reflist_entry *re, *re_match;
9247 char *uuidstr = NULL;
9248 int added_logmsg = 0;
9252 *logmsg_path = NULL;
9254 err = got_worktree_get_uuid(&uuidstr, worktree);
9258 err = got_ref_list(&refs, repo, "refs/got/worktree",
9259 got_ref_cmp_by_name, repo);
9263 TAILQ_FOREACH(re, &refs, entry) {
9264 const char *refname, *type;
9265 struct wt_commitable_path_arg wcpa;
9268 refname = got_ref_get_name(re->ref);
9270 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9271 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9272 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9273 type = "cherrypicked";
9274 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9275 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9276 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9277 type = "backed-out";
9281 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9282 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9286 err = got_repo_match_object_id(&id, NULL, refname,
9287 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9291 err = got_object_open_as_commit(&commit, repo, id);
9295 wcpa.commit_paths = paths;
9296 wcpa.has_changes = &add_logmsg;
9298 err = commit_path_changed_in_worktree(&wcpa, id,
9305 err = got_opentemp_named(logmsg_path, &f,
9306 "got-commit-logmsg", "");
9310 err = cat_logmsg(f, commit, refname, type,
9317 err = got_reflist_entry_dup(&re_match, re);
9320 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9323 got_object_commit_close(commit);
9332 got_ref_list_free(&refs);
9334 got_object_commit_close(commit);
9335 if (f && fclose(f) == EOF && err == NULL)
9336 err = got_error_from_errno("fclose");
9337 if (!added_logmsg) {
9338 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9339 err = got_error_from_errno2("unlink", *logmsg_path);
9340 *logmsg_path = NULL;
9345 static const struct got_error *
9346 cmd_commit(int argc, char *argv[])
9348 const struct got_error *error = NULL;
9349 struct got_worktree *worktree = NULL;
9350 struct got_repository *repo = NULL;
9351 char *cwd = NULL, *id_str = NULL;
9352 struct got_object_id *id = NULL;
9353 const char *logmsg = NULL;
9354 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9355 struct collect_commit_logmsg_arg cl_arg;
9356 const char *author = NULL;
9357 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9358 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9359 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9360 int show_diff = 1, commit_conflicts = 0;
9361 struct got_pathlist_head paths;
9362 struct got_reflist_head refs;
9363 struct got_reflist_entry *re;
9364 int *pack_fds = NULL;
9368 cl_arg.logmsg_path = NULL;
9371 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9372 "unveil", NULL) == -1)
9376 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9380 error = valid_author(author);
9385 commit_conflicts = 1;
9389 option_conflict('F', 'm');
9390 prepared_logmsg = realpath(optarg, NULL);
9391 if (prepared_logmsg == NULL)
9392 return got_error_from_errno2("realpath",
9396 if (prepared_logmsg)
9397 option_conflict('m', 'F');
9401 non_interactive = 1;
9407 allow_bad_symlinks = 1;
9418 cwd = getcwd(NULL, 0);
9420 error = got_error_from_errno("getcwd");
9424 error = got_repo_pack_fds_open(&pack_fds);
9428 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9430 if (error->code == GOT_ERR_NOT_WORKTREE)
9431 error = wrap_not_worktree_error(error, "commit", cwd);
9435 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9438 if (rebase_in_progress) {
9439 error = got_error(GOT_ERR_REBASING);
9443 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9448 error = get_gitconfig_path(&gitconfig_path);
9451 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9452 gitconfig_path, pack_fds);
9456 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9459 if (merge_in_progress) {
9460 error = got_error(GOT_ERR_MERGE_BUSY);
9464 error = get_author(&committer, repo, worktree);
9471 if (logmsg == NULL || strlen(logmsg) == 0) {
9472 error = get_editor(&editor);
9475 if (unveil(editor, "x") != 0) {
9476 error = got_error_from_errno2("unveil", editor);
9480 if (prepared_logmsg) {
9481 if (unveil(prepared_logmsg, "r") != 0) {
9482 error = got_error_from_errno2("unveil",
9488 error = apply_unveil(got_repo_get_path(repo), 0,
9489 got_worktree_get_root_path(worktree));
9493 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9497 if (prepared_logmsg == NULL) {
9498 error = lookup_logmsg_ref(&merged_logmsg,
9499 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9504 cl_arg.editor = editor;
9505 cl_arg.cmdline_log = logmsg;
9506 cl_arg.prepared_log = prepared_logmsg;
9507 cl_arg.merged_log = merged_logmsg;
9508 cl_arg.non_interactive = non_interactive;
9509 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9510 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9511 if (!histedit_in_progress) {
9512 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9513 error = got_error(GOT_ERR_COMMIT_BRANCH);
9516 cl_arg.branch_name += 11;
9518 cl_arg.repo_path = got_repo_get_path(repo);
9519 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9520 allow_bad_symlinks, show_diff, commit_conflicts,
9521 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9523 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9524 cl_arg.logmsg_path != NULL)
9525 preserve_logmsg = 1;
9529 error = got_object_id_str(&id_str, id);
9532 printf("Created commit %s\n", id_str);
9534 TAILQ_FOREACH(re, &refs, entry) {
9535 error = got_ref_delete(re->ref, repo);
9541 if (preserve_logmsg) {
9542 fprintf(stderr, "%s: log message preserved in %s\n",
9543 getprogname(), cl_arg.logmsg_path);
9544 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9546 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9547 free(cl_arg.logmsg_path);
9548 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9549 error = got_error_from_errno2("unlink", merged_logmsg);
9550 free(merged_logmsg);
9552 const struct got_error *close_err = got_repo_close(repo);
9557 got_worktree_close(worktree);
9559 const struct got_error *pack_err =
9560 got_repo_pack_fds_close(pack_fds);
9564 got_ref_list_free(&refs);
9565 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9568 free(gitconfig_path);
9571 free(prepared_logmsg);
9578 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9579 "[-r repository-path] [-t tag] [remote-repository]\n",
9585 print_load_info(int print_colored, int print_found, int print_trees,
9586 int ncolored, int nfound, int ntrees)
9588 if (print_colored) {
9589 printf("%d commit%s colored", ncolored,
9590 ncolored == 1 ? "" : "s");
9593 printf("%s%d object%s found",
9594 ncolored > 0 ? "; " : "",
9595 nfound, nfound == 1 ? "" : "s");
9598 printf("; %d tree%s scanned", ntrees,
9599 ntrees == 1 ? "" : "s");
9603 struct got_send_progress_arg {
9604 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9611 int last_nobj_total;
9615 int printed_something;
9617 struct got_pathlist_head *delete_branches;
9620 static const struct got_error *
9621 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9622 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9623 int nobj_written, off_t bytes_sent, const char *refname,
9624 const char *errmsg, int success)
9626 struct got_send_progress_arg *a = arg;
9627 char scaled_packsize[FMT_SCALED_STRSIZE];
9628 char scaled_sent[FMT_SCALED_STRSIZE];
9629 int p_deltify = 0, p_written = 0, p_sent = 0;
9630 int print_colored = 0, print_found = 0, print_trees = 0;
9631 int print_searching = 0, print_total = 0;
9632 int print_deltify = 0, print_written = 0, print_sent = 0;
9634 if (a->verbosity < 0)
9638 const char *status = success ? "accepted" : "rejected";
9641 struct got_pathlist_entry *pe;
9642 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9643 const char *branchname = pe->path;
9644 if (got_path_cmp(branchname, refname,
9645 strlen(branchname), strlen(refname)) == 0) {
9647 a->sent_something = 1;
9653 if (a->printed_something)
9655 printf("Server has %s %s", status, refname);
9657 printf(": %s", errmsg);
9658 a->printed_something = 1;
9662 if (a->last_ncolored != ncolored) {
9664 a->last_ncolored = ncolored;
9667 if (a->last_nfound != nfound) {
9670 a->last_nfound = nfound;
9673 if (a->last_ntrees != ntrees) {
9677 a->last_ntrees = ntrees;
9680 if ((print_colored || print_found || print_trees) &&
9683 print_load_info(print_colored, print_found, print_trees,
9684 ncolored, nfound, ntrees);
9685 a->printed_something = 1;
9688 } else if (!a->loading_done) {
9690 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9692 a->loading_done = 1;
9695 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9696 return got_error_from_errno("fmt_scaled");
9697 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9698 return got_error_from_errno("fmt_scaled");
9700 if (a->last_ncommits != ncommits) {
9701 print_searching = 1;
9702 a->last_ncommits = ncommits;
9705 if (a->last_nobj_total != nobj_total) {
9706 print_searching = 1;
9708 a->last_nobj_total = nobj_total;
9711 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9712 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9713 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9714 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9715 return got_error(GOT_ERR_NO_SPACE);
9718 if (nobj_deltify > 0 || nobj_written > 0) {
9719 if (nobj_deltify > 0) {
9720 p_deltify = (nobj_deltify * 100) / nobj_total;
9721 if (p_deltify != a->last_p_deltify) {
9722 a->last_p_deltify = p_deltify;
9723 print_searching = 1;
9728 if (nobj_written > 0) {
9729 p_written = (nobj_written * 100) / nobj_total;
9730 if (p_written != a->last_p_written) {
9731 a->last_p_written = p_written;
9732 print_searching = 1;
9740 if (bytes_sent > 0) {
9741 p_sent = (bytes_sent * 100) / packfile_size;
9742 if (p_sent != a->last_p_sent) {
9743 a->last_p_sent = p_sent;
9744 print_searching = 1;
9750 a->sent_something = 1;
9753 if (print_searching || print_total || print_deltify || print_written ||
9756 if (print_searching)
9757 printf("packing %d reference%s", ncommits,
9758 ncommits == 1 ? "" : "s");
9760 printf("; %d object%s", nobj_total,
9761 nobj_total == 1 ? "" : "s");
9763 printf("; deltify: %d%%", p_deltify);
9765 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9766 scaled_packsize, p_sent);
9767 else if (print_written)
9768 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9769 scaled_packsize, p_written);
9770 if (print_searching || print_total || print_deltify ||
9771 print_written || print_sent) {
9772 a->printed_something = 1;
9778 static const struct got_error *
9779 cmd_send(int argc, char *argv[])
9781 const struct got_error *error = NULL;
9782 char *cwd = NULL, *repo_path = NULL;
9783 const char *remote_name;
9784 char *proto = NULL, *host = NULL, *port = NULL;
9785 char *repo_name = NULL, *server_path = NULL;
9786 const struct got_remote_repo *remotes;
9787 struct got_remote_repo *remote = NULL;
9788 int nremotes, nbranches = 0, ndelete_branches = 0;
9789 struct got_repository *repo = NULL;
9790 struct got_worktree *worktree = NULL;
9791 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9792 struct got_pathlist_head branches;
9793 struct got_pathlist_head tags;
9794 struct got_reflist_head all_branches;
9795 struct got_reflist_head all_tags;
9796 struct got_pathlist_head delete_args;
9797 struct got_pathlist_head delete_branches;
9798 struct got_reflist_entry *re;
9799 struct got_pathlist_entry *pe;
9800 int i, ch, sendfd = -1, sendstatus;
9802 struct got_send_progress_arg spa;
9803 int verbosity = 0, overwrite_refs = 0;
9804 int send_all_branches = 0, send_all_tags = 0;
9805 struct got_reference *ref = NULL;
9806 int *pack_fds = NULL;
9808 TAILQ_INIT(&branches);
9810 TAILQ_INIT(&all_branches);
9811 TAILQ_INIT(&all_tags);
9812 TAILQ_INIT(&delete_args);
9813 TAILQ_INIT(&delete_branches);
9815 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9818 send_all_branches = 1;
9821 error = got_pathlist_append(&branches, optarg, NULL);
9827 error = got_pathlist_append(&delete_args, optarg, NULL);
9838 repo_path = realpath(optarg, NULL);
9839 if (repo_path == NULL)
9840 return got_error_from_errno2("realpath",
9842 got_path_strip_trailing_slashes(repo_path);
9848 error = got_pathlist_append(&tags, optarg, NULL);
9855 else if (verbosity < 3)
9866 if (send_all_branches && !TAILQ_EMPTY(&branches))
9867 option_conflict('a', 'b');
9868 if (send_all_tags && !TAILQ_EMPTY(&tags))
9869 option_conflict('T', 't');
9873 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9875 remote_name = argv[0];
9879 cwd = getcwd(NULL, 0);
9881 error = got_error_from_errno("getcwd");
9885 error = got_repo_pack_fds_open(&pack_fds);
9889 if (repo_path == NULL) {
9890 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9891 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9897 strdup(got_worktree_get_repo_path(worktree));
9898 if (repo_path == NULL)
9899 error = got_error_from_errno("strdup");
9903 repo_path = strdup(cwd);
9904 if (repo_path == NULL) {
9905 error = got_error_from_errno("strdup");
9911 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9916 worktree_conf = got_worktree_get_gotconfig(worktree);
9917 if (worktree_conf) {
9918 got_gotconfig_get_remotes(&nremotes, &remotes,
9920 for (i = 0; i < nremotes; i++) {
9921 if (strcmp(remotes[i].name, remote_name) == 0) {
9922 error = got_repo_remote_repo_dup(&remote,
9931 if (remote == NULL) {
9932 repo_conf = got_repo_get_gotconfig(repo);
9934 got_gotconfig_get_remotes(&nremotes, &remotes,
9936 for (i = 0; i < nremotes; i++) {
9937 if (strcmp(remotes[i].name, remote_name) == 0) {
9938 error = got_repo_remote_repo_dup(&remote,
9947 if (remote == NULL) {
9948 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9949 for (i = 0; i < nremotes; i++) {
9950 if (strcmp(remotes[i].name, remote_name) == 0) {
9951 error = got_repo_remote_repo_dup(&remote,
9959 if (remote == NULL) {
9960 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9964 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9965 &repo_name, remote->send_url);
9969 if (strcmp(proto, "git") == 0) {
9971 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9972 "sendfd dns inet unveil", NULL) == -1)
9975 } else if (strcmp(proto, "git+ssh") == 0 ||
9976 strcmp(proto, "ssh") == 0) {
9978 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9979 "sendfd unveil", NULL) == -1)
9982 } else if (strcmp(proto, "http") == 0 ||
9983 strcmp(proto, "git+http") == 0) {
9984 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9987 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9991 error = got_dial_apply_unveil(proto);
9995 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9999 if (send_all_branches) {
10000 error = got_ref_list(&all_branches, repo, "refs/heads",
10001 got_ref_cmp_by_name, NULL);
10004 TAILQ_FOREACH(re, &all_branches, entry) {
10005 const char *branchname = got_ref_get_name(re->ref);
10006 error = got_pathlist_append(&branches,
10012 } else if (nbranches == 0) {
10013 for (i = 0; i < remote->nsend_branches; i++) {
10014 error = got_pathlist_append(&branches,
10015 remote->send_branches[i], NULL);
10021 if (send_all_tags) {
10022 error = got_ref_list(&all_tags, repo, "refs/tags",
10023 got_ref_cmp_by_name, NULL);
10026 TAILQ_FOREACH(re, &all_tags, entry) {
10027 const char *tagname = got_ref_get_name(re->ref);
10028 error = got_pathlist_append(&tags,
10036 * To prevent accidents only branches in refs/heads/ can be deleted
10037 * with 'got send -d'.
10038 * Deleting anything else requires local repository access or Git.
10040 TAILQ_FOREACH(pe, &delete_args, entry) {
10041 const char *branchname = pe->path;
10043 struct got_pathlist_entry *new;
10044 if (strncmp(branchname, "refs/heads/", 11) == 0) {
10045 s = strdup(branchname);
10047 error = got_error_from_errno("strdup");
10051 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
10052 error = got_error_from_errno("asprintf");
10056 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
10057 if (error || new == NULL /* duplicate */)
10061 ndelete_branches++;
10064 if (nbranches == 0 && ndelete_branches == 0) {
10065 struct got_reference *head_ref;
10067 error = got_ref_open(&head_ref, repo,
10068 got_worktree_get_head_ref_name(worktree), 0);
10070 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
10073 if (got_ref_is_symbolic(head_ref)) {
10074 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
10075 got_ref_close(head_ref);
10080 error = got_pathlist_append(&branches, got_ref_get_name(ref),
10088 /* Release work tree lock. */
10089 got_worktree_close(worktree);
10093 if (verbosity >= 0) {
10094 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10095 remote->name, proto, host,
10096 port ? ":" : "", port ? port : "",
10097 *server_path == '/' ? "" : "/", server_path);
10100 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
10101 server_path, verbosity);
10105 memset(&spa, 0, sizeof(spa));
10106 spa.last_scaled_packsize[0] = '\0';
10107 spa.last_p_deltify = -1;
10108 spa.last_p_written = -1;
10109 spa.verbosity = verbosity;
10110 spa.delete_branches = &delete_branches;
10111 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
10112 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
10113 check_cancelled, NULL);
10114 if (spa.printed_something)
10118 if (!spa.sent_something && verbosity >= 0)
10119 printf("Already up-to-date\n");
10122 if (kill(sendpid, SIGTERM) == -1)
10123 error = got_error_from_errno("kill");
10124 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
10125 error = got_error_from_errno("waitpid");
10127 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
10128 error = got_error_from_errno("close");
10130 const struct got_error *close_err = got_repo_close(repo);
10135 got_worktree_close(worktree);
10137 const struct got_error *pack_err =
10138 got_repo_pack_fds_close(pack_fds);
10143 got_ref_close(ref);
10144 got_repo_free_remote_repo_data(remote);
10146 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10147 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10148 got_ref_list_free(&all_branches);
10149 got_ref_list_free(&all_tags);
10150 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10151 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10163 * Print and if delete is set delete all ref_prefix references.
10164 * If wanted_ref is not NULL, only print or delete this reference.
10166 static const struct got_error *
10167 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10168 const char *wanted_ref, int delete, struct got_worktree *worktree,
10169 struct got_repository *repo)
10171 const struct got_error *err;
10172 struct got_pathlist_head paths;
10173 struct got_reflist_head refs;
10174 struct got_reflist_entry *re;
10175 struct got_reflist_object_id_map *refs_idmap = NULL;
10176 struct got_commit_object *commit = NULL;
10177 struct got_object_id *id = NULL;
10178 const char *header_prefix;
10179 char *uuidstr = NULL;
10183 TAILQ_INIT(&paths);
10185 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10189 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10193 if (worktree != NULL) {
10194 err = got_worktree_get_uuid(&uuidstr, worktree);
10200 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10204 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10205 header_prefix = "backout";
10207 header_prefix = "cherrypick";
10209 TAILQ_FOREACH(re, &refs, entry) {
10210 const char *refname, *wt;
10212 refname = got_ref_get_name(re->ref);
10214 err = check_cancelled(NULL);
10218 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10219 refname += prefix_len + 1; /* skip '-' delimiter */
10225 if (worktree == NULL || strncmp(refname, uuidstr,
10226 GOT_WORKTREE_UUID_STRLEN) == 0)
10227 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10231 err = got_repo_match_object_id(&id, NULL, refname,
10232 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10236 err = got_object_open_as_commit(&commit, repo, id);
10241 found = strncmp(wanted_ref, refname,
10242 strlen(wanted_ref)) == 0;
10243 if (wanted_ref && !found) {
10244 struct got_reflist_head *ci_refs;
10246 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10250 char *refs_str = NULL;
10251 char const *r = NULL;
10253 err = build_refs_str(&refs_str, ci_refs, id,
10260 if (strncmp(r, wanted_ref,
10261 strlen(wanted_ref)) == 0) {
10265 r = strchr(r, ' ');
10273 if (wanted_ref == NULL || found) {
10275 err = got_ref_delete(re->ref, repo);
10278 printf("Deleted: ");
10279 err = print_commit_oneline(commit, id, repo,
10283 * Print paths modified by commit to help
10284 * associate commits with worktree changes.
10286 err = get_changed_paths(&paths, commit,
10291 err = print_commit(commit, id, repo, NULL,
10292 &paths, NULL, 0, 0, refs_idmap, NULL,
10294 got_pathlist_free(&paths,
10295 GOT_PATHLIST_FREE_ALL);
10297 if (worktree == NULL)
10298 printf("work tree: %.*s\n\n",
10299 GOT_WORKTREE_UUID_STRLEN, wt);
10305 got_object_commit_close(commit);
10311 if (wanted_ref != NULL && !found)
10312 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10317 got_ref_list_free(&refs);
10318 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10320 got_reflist_object_id_map_free(refs_idmap);
10322 got_object_commit_close(commit);
10327 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10328 * identified by id for log messages to prepopulate the editor on commit.
10330 static const struct got_error *
10331 logmsg_ref(struct got_object_id *id, const char *prefix,
10332 struct got_worktree *worktree, struct got_repository *repo)
10334 const struct got_error *err = NULL;
10335 char *idstr, *ref = NULL, *refname = NULL;
10336 int histedit_in_progress;
10337 int rebase_in_progress, merge_in_progress;
10340 * Silenty refuse to create merge reference if any histedit, merge,
10341 * or rebase operation is in progress.
10343 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10347 if (histedit_in_progress)
10350 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10353 if (rebase_in_progress)
10356 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10360 if (merge_in_progress)
10363 err = got_object_id_str(&idstr, id);
10367 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10371 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10372 err = got_error_from_errno("asprintf");
10376 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10386 usage_cherrypick(void)
10388 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10393 static const struct got_error *
10394 cmd_cherrypick(int argc, char *argv[])
10396 const struct got_error *error = NULL;
10397 struct got_worktree *worktree = NULL;
10398 struct got_repository *repo = NULL;
10399 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10400 struct got_object_id *commit_id = NULL;
10401 struct got_commit_object *commit = NULL;
10402 struct got_object_qid *pid;
10403 int ch, list_refs = 0, remove_refs = 0;
10404 struct got_update_progress_arg upa;
10405 int *pack_fds = NULL;
10408 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10409 "unveil", NULL) == -1)
10413 while ((ch = getopt(argc, argv, "lX")) != -1) {
10422 usage_cherrypick();
10430 if (list_refs || remove_refs) {
10431 if (argc != 0 && argc != 1)
10432 usage_cherrypick();
10433 } else if (argc != 1)
10434 usage_cherrypick();
10435 if (list_refs && remove_refs)
10436 option_conflict('l', 'X');
10438 cwd = getcwd(NULL, 0);
10440 error = got_error_from_errno("getcwd");
10444 error = got_repo_pack_fds_open(&pack_fds);
10448 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10450 if (list_refs || remove_refs) {
10451 if (error->code != GOT_ERR_NOT_WORKTREE)
10454 if (error->code == GOT_ERR_NOT_WORKTREE)
10455 error = wrap_not_worktree_error(error,
10456 "cherrypick", cwd);
10461 error = got_repo_open(&repo,
10462 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10467 error = apply_unveil(got_repo_get_path(repo), 0,
10468 worktree ? got_worktree_get_root_path(worktree) : NULL);
10472 if (list_refs || remove_refs) {
10473 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10474 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10475 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10479 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10483 error = got_repo_match_object_id(&commit_id, NULL,
10484 keyword_idstr != NULL ? keyword_idstr : argv[0],
10485 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10488 error = got_object_id_str(&commit_id_str, commit_id);
10492 error = got_object_open_as_commit(&commit, repo, commit_id);
10495 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10496 memset(&upa, 0, sizeof(upa));
10497 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10498 commit_id, repo, update_progress, &upa, check_cancelled,
10503 if (upa.did_something) {
10504 error = logmsg_ref(commit_id,
10505 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10508 printf("Merged commit %s\n", commit_id_str);
10510 print_merge_progress_stats(&upa);
10513 free(keyword_idstr);
10515 got_object_commit_close(commit);
10516 free(commit_id_str);
10518 got_worktree_close(worktree);
10520 const struct got_error *close_err = got_repo_close(repo);
10525 const struct got_error *pack_err =
10526 got_repo_pack_fds_close(pack_fds);
10535 usage_backout(void)
10537 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10541 static const struct got_error *
10542 cmd_backout(int argc, char *argv[])
10544 const struct got_error *error = NULL;
10545 struct got_worktree *worktree = NULL;
10546 struct got_repository *repo = NULL;
10547 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10548 struct got_object_id *commit_id = NULL;
10549 struct got_commit_object *commit = NULL;
10550 struct got_object_qid *pid;
10551 int ch, list_refs = 0, remove_refs = 0;
10552 struct got_update_progress_arg upa;
10553 int *pack_fds = NULL;
10556 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10557 "unveil", NULL) == -1)
10561 while ((ch = getopt(argc, argv, "lX")) != -1) {
10578 if (list_refs || remove_refs) {
10579 if (argc != 0 && argc != 1)
10581 } else if (argc != 1)
10583 if (list_refs && remove_refs)
10584 option_conflict('l', 'X');
10586 cwd = getcwd(NULL, 0);
10588 error = got_error_from_errno("getcwd");
10592 error = got_repo_pack_fds_open(&pack_fds);
10596 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10598 if (list_refs || remove_refs) {
10599 if (error->code != GOT_ERR_NOT_WORKTREE)
10602 if (error->code == GOT_ERR_NOT_WORKTREE)
10603 error = wrap_not_worktree_error(error,
10609 error = got_repo_open(&repo,
10610 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10615 error = apply_unveil(got_repo_get_path(repo), 0,
10616 worktree ? got_worktree_get_root_path(worktree) : NULL);
10620 if (list_refs || remove_refs) {
10621 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10622 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10623 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10627 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10631 error = got_repo_match_object_id(&commit_id, NULL,
10632 keyword_idstr != NULL ? keyword_idstr : argv[0],
10633 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10636 error = got_object_id_str(&commit_id_str, commit_id);
10640 error = got_object_open_as_commit(&commit, repo, commit_id);
10643 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10645 error = got_error(GOT_ERR_ROOT_COMMIT);
10649 memset(&upa, 0, sizeof(upa));
10650 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10651 repo, update_progress, &upa, check_cancelled, NULL);
10655 if (upa.did_something) {
10656 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10660 printf("Backed out commit %s\n", commit_id_str);
10662 print_merge_progress_stats(&upa);
10665 free(keyword_idstr);
10667 got_object_commit_close(commit);
10668 free(commit_id_str);
10670 got_worktree_close(worktree);
10672 const struct got_error *close_err = got_repo_close(repo);
10677 const struct got_error *pack_err =
10678 got_repo_pack_fds_close(pack_fds);
10688 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10693 trim_logmsg(char *logmsg, int limit)
10698 len = strlen(logmsg);
10701 logmsg[len] = '\0';
10702 nl = strchr(logmsg, '\n');
10707 static const struct got_error *
10708 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10710 const struct got_error *err;
10711 char *logmsg0 = NULL;
10714 err = got_object_commit_get_logmsg(&logmsg0, commit);
10719 while (isspace((unsigned char)s[0]))
10722 *logmsg = strdup(s);
10723 if (*logmsg == NULL) {
10724 err = got_error_from_errno("strdup");
10728 trim_logmsg(*logmsg, limit);
10734 static const struct got_error *
10735 show_rebase_merge_conflict(struct got_object_id *id,
10736 struct got_repository *repo)
10738 const struct got_error *err;
10739 struct got_commit_object *commit = NULL;
10740 char *id_str = NULL, *logmsg = NULL;
10742 err = got_object_open_as_commit(&commit, repo, id);
10746 err = got_object_id_str(&id_str, id);
10752 err = get_short_logmsg(&logmsg, 42, commit);
10756 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10759 got_object_commit_close(commit);
10764 static const struct got_error *
10765 show_rebase_progress(struct got_commit_object *commit,
10766 struct got_object_id *old_id, struct got_object_id *new_id)
10768 const struct got_error *err;
10769 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10771 err = got_object_id_str(&old_id_str, old_id);
10776 err = got_object_id_str(&new_id_str, new_id);
10781 old_id_str[12] = '\0';
10783 new_id_str[12] = '\0';
10785 err = get_short_logmsg(&logmsg, 42, commit);
10789 printf("%s -> %s: %s\n", old_id_str,
10790 new_id_str ? new_id_str : "no-op change", logmsg);
10798 static const struct got_error *
10799 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10800 struct got_reference *branch, struct got_reference *tmp_branch,
10801 struct got_repository *repo, int create_backup)
10803 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10804 return got_worktree_rebase_complete(worktree, fileindex,
10805 tmp_branch, branch, repo, create_backup);
10808 static const struct got_error *
10809 rebase_commit(struct got_pathlist_head *merged_paths,
10810 struct got_worktree *worktree, struct got_fileindex *fileindex,
10811 struct got_reference *tmp_branch, const char *committer,
10812 struct got_object_id *commit_id, int allow_conflict,
10813 struct got_repository *repo)
10815 const struct got_error *error;
10816 struct got_commit_object *commit;
10817 struct got_object_id *new_commit_id;
10819 error = got_object_open_as_commit(&commit, repo, commit_id);
10823 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10824 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10825 allow_conflict, repo);
10827 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10829 error = show_rebase_progress(commit, commit_id, NULL);
10831 error = show_rebase_progress(commit, commit_id, new_commit_id);
10832 free(new_commit_id);
10835 got_object_commit_close(commit);
10839 struct check_path_prefix_arg {
10840 const char *path_prefix;
10845 static const struct got_error *
10846 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10847 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10848 struct got_object_id *id1, struct got_object_id *id2,
10849 const char *path1, const char *path2,
10850 mode_t mode1, mode_t mode2, struct got_repository *repo)
10852 struct check_path_prefix_arg *a = arg;
10854 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10855 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10856 return got_error(a->errcode);
10861 static const struct got_error *
10862 check_path_prefix(struct got_object_id *parent_id,
10863 struct got_object_id *commit_id, const char *path_prefix,
10864 int errcode, struct got_repository *repo)
10866 const struct got_error *err;
10867 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10868 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10869 struct check_path_prefix_arg cpp_arg;
10871 if (got_path_is_root_dir(path_prefix))
10874 err = got_object_open_as_commit(&commit, repo, commit_id);
10878 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10882 err = got_object_open_as_tree(&tree1, repo,
10883 got_object_commit_get_tree_id(parent_commit));
10887 err = got_object_open_as_tree(&tree2, repo,
10888 got_object_commit_get_tree_id(commit));
10892 cpp_arg.path_prefix = path_prefix;
10893 while (cpp_arg.path_prefix[0] == '/')
10894 cpp_arg.path_prefix++;
10895 cpp_arg.len = strlen(cpp_arg.path_prefix);
10896 cpp_arg.errcode = errcode;
10897 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10898 check_path_prefix_in_diff, &cpp_arg, 0);
10901 got_object_tree_close(tree1);
10903 got_object_tree_close(tree2);
10905 got_object_commit_close(commit);
10907 got_object_commit_close(parent_commit);
10911 static const struct got_error *
10912 collect_commits(struct got_object_id_queue *commits,
10913 struct got_object_id *initial_commit_id,
10914 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10915 const char *path_prefix, int path_prefix_errcode,
10916 struct got_repository *repo)
10918 const struct got_error *err = NULL;
10919 struct got_commit_graph *graph = NULL;
10920 struct got_object_id parent_id, commit_id;
10921 struct got_object_qid *qid;
10923 err = got_commit_graph_open(&graph, "/", 1);
10927 err = got_commit_graph_bfsort(graph, iter_start_id, repo,
10928 check_cancelled, NULL);
10932 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10933 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10934 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10935 check_cancelled, NULL);
10937 if (err->code == GOT_ERR_ITER_COMPLETED) {
10938 err = got_error_msg(GOT_ERR_ANCESTRY,
10939 "ran out of commits to rebase before "
10940 "youngest common ancestor commit has "
10941 "been reached?!?");
10945 err = check_path_prefix(&parent_id, &commit_id,
10946 path_prefix, path_prefix_errcode, repo);
10950 err = got_object_qid_alloc(&qid, &commit_id);
10953 STAILQ_INSERT_HEAD(commits, qid, entry);
10955 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10959 got_commit_graph_close(graph);
10963 static const struct got_error *
10964 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10966 const struct got_error *err = NULL;
10967 time_t committer_time;
10969 char datebuf[11]; /* YYYY-MM-DD + NUL */
10970 char *author0 = NULL, *author, *smallerthan;
10971 char *logmsg0 = NULL, *logmsg, *newline;
10973 committer_time = got_object_commit_get_committer_time(commit);
10974 if (gmtime_r(&committer_time, &tm) == NULL)
10975 return got_error_from_errno("gmtime_r");
10976 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10977 return got_error(GOT_ERR_NO_SPACE);
10979 author0 = strdup(got_object_commit_get_author(commit));
10980 if (author0 == NULL)
10981 return got_error_from_errno("strdup");
10983 smallerthan = strchr(author, '<');
10984 if (smallerthan && smallerthan[1] != '\0')
10985 author = smallerthan + 1;
10986 author[strcspn(author, "@>")] = '\0';
10988 err = got_object_commit_get_logmsg(&logmsg0, commit);
10992 while (*logmsg == '\n')
10994 newline = strchr(logmsg, '\n');
10998 if (asprintf(brief_str, "%s %s %s",
10999 datebuf, author, logmsg) == -1)
11000 err = got_error_from_errno("asprintf");
11007 static const struct got_error *
11008 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
11009 struct got_repository *repo)
11011 const struct got_error *err;
11014 err = got_object_id_str(&id_str, id);
11018 err = got_ref_delete(ref, repo);
11022 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
11028 static const struct got_error *
11029 print_backup_ref(const char *branch_name, const char *new_id_str,
11030 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
11031 struct got_reflist_object_id_map *refs_idmap,
11032 struct got_repository *repo)
11034 const struct got_error *err = NULL;
11035 struct got_reflist_head *refs;
11036 char *refs_str = NULL;
11037 struct got_object_id *new_commit_id = NULL;
11038 struct got_commit_object *new_commit = NULL;
11039 char *new_commit_brief_str = NULL;
11040 struct got_object_id *yca_id = NULL;
11041 struct got_commit_object *yca_commit = NULL;
11042 char *yca_id_str = NULL, *yca_brief_str = NULL;
11043 char *custom_refs_str;
11045 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
11046 return got_error_from_errno("asprintf");
11048 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
11049 0, 0, refs_idmap, custom_refs_str, NULL);
11053 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
11057 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
11059 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
11064 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
11068 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
11072 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11073 old_commit_id, new_commit_id, 1, 0, repo, check_cancelled, NULL);
11077 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
11078 refs_str ? " (" : "", refs_str ? refs_str : "",
11079 refs_str ? ")" : "", new_commit_brief_str);
11080 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
11081 got_object_id_cmp(yca_id, old_commit_id) != 0) {
11085 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
11089 err = get_commit_brief_str(&yca_brief_str, yca_commit);
11093 err = got_object_id_str(&yca_id_str, yca_id);
11097 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
11099 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
11103 printf("history forked at %s%s%s%s\n %s\n",
11105 refs_str ? " (" : "", refs_str ? refs_str : "",
11106 refs_str ? ")" : "", yca_brief_str);
11109 free(custom_refs_str);
11110 free(new_commit_id);
11114 free(yca_brief_str);
11116 got_object_commit_close(new_commit);
11118 got_object_commit_close(yca_commit);
11123 static const struct got_error *
11124 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
11125 struct got_repository *repo)
11127 const struct got_error *err;
11128 struct got_reflist_head refs;
11129 struct got_reflist_entry *re;
11130 char *uuidstr = NULL;
11131 static char msg[160];
11135 err = got_worktree_get_uuid(&uuidstr, worktree);
11139 err = got_ref_list(&refs, repo, "refs/got/worktree",
11140 got_ref_cmp_by_name, repo);
11144 TAILQ_FOREACH(re, &refs, entry) {
11145 const char *cmd, *refname, *type;
11147 refname = got_ref_get_name(re->ref);
11149 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11150 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11151 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11152 cmd = "cherrypick";
11153 type = "cherrypicked";
11154 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11155 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11156 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11158 type = "backed-out";
11162 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11165 snprintf(msg, sizeof(msg),
11166 "work tree has references created by %s commits which "
11167 "must be removed with 'got %s -X' before running the %s "
11168 "command", type, cmd, caller);
11169 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11175 got_ref_list_free(&refs);
11179 static const struct got_error *
11180 process_backup_refs(const char *backup_ref_prefix,
11181 const char *wanted_branch_name,
11182 int delete, struct got_repository *repo)
11184 const struct got_error *err;
11185 struct got_reflist_head refs, backup_refs;
11186 struct got_reflist_entry *re;
11187 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11188 struct got_object_id *old_commit_id = NULL;
11189 char *branch_name = NULL;
11190 struct got_commit_object *old_commit = NULL;
11191 struct got_reflist_object_id_map *refs_idmap = NULL;
11192 int wanted_branch_found = 0;
11195 TAILQ_INIT(&backup_refs);
11197 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11201 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11205 if (wanted_branch_name) {
11206 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11207 wanted_branch_name += 11;
11210 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11211 got_ref_cmp_by_commit_timestamp_descending, repo);
11215 TAILQ_FOREACH(re, &backup_refs, entry) {
11216 const char *refname = got_ref_get_name(re->ref);
11219 err = check_cancelled(NULL);
11223 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11227 err = got_object_open_as_commit(&old_commit, repo,
11232 if (strncmp(backup_ref_prefix, refname,
11233 backup_ref_prefix_len) == 0)
11234 refname += backup_ref_prefix_len;
11236 while (refname[0] == '/')
11239 branch_name = strdup(refname);
11240 if (branch_name == NULL) {
11241 err = got_error_from_errno("strdup");
11244 slash = strrchr(branch_name, '/');
11247 refname += strlen(branch_name) + 1;
11250 if (wanted_branch_name == NULL ||
11251 strcmp(wanted_branch_name, branch_name) == 0) {
11252 wanted_branch_found = 1;
11254 err = delete_backup_ref(re->ref,
11255 old_commit_id, repo);
11257 err = print_backup_ref(branch_name, refname,
11258 old_commit_id, old_commit, refs_idmap,
11265 free(old_commit_id);
11266 old_commit_id = NULL;
11268 branch_name = NULL;
11269 got_object_commit_close(old_commit);
11273 if (wanted_branch_name && !wanted_branch_found) {
11274 err = got_error_fmt(GOT_ERR_NOT_REF,
11275 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11279 got_reflist_object_id_map_free(refs_idmap);
11280 got_ref_list_free(&refs);
11281 got_ref_list_free(&backup_refs);
11282 free(old_commit_id);
11285 got_object_commit_close(old_commit);
11289 static const struct got_error *
11290 abort_progress(void *arg, unsigned char status, const char *path)
11293 * Unversioned files should not clutter progress output when
11294 * an operation is aborted.
11296 if (status == GOT_STATUS_UNVERSIONED)
11299 return update_progress(arg, status, path);
11302 static const struct got_error *
11303 find_merge_commit_yca(struct got_object_id **new_yca_id,
11304 struct got_object_id *branch_head_commit_id,
11305 struct got_object_id *yca_id,
11306 struct got_object_id *base_commit_id,
11307 struct got_repository *repo)
11309 const struct got_error *err = NULL;
11310 struct got_commit_graph *graph = NULL;
11311 struct got_commit_object *commit = NULL;
11313 *new_yca_id = NULL;
11315 err = got_commit_graph_open(&graph, "/", 1);
11319 err = got_commit_graph_bfsort(graph, base_commit_id,
11320 repo, check_cancelled, NULL);
11325 struct got_object_id id;
11327 err = got_commit_graph_iter_next(&id, graph, repo,
11328 check_cancelled, NULL);
11330 if (err->code == GOT_ERR_ITER_COMPLETED)
11335 err = got_object_open_as_commit(&commit, repo, &id);
11339 if (got_object_commit_get_nparents(commit) > 1) {
11340 /* Search for a better YCA using toposort. */
11341 err = got_commit_graph_find_youngest_common_ancestor(
11342 new_yca_id, base_commit_id, branch_head_commit_id,
11343 0, 1, repo, check_cancelled, NULL);
11347 if (got_object_id_cmp(&id, yca_id) == 0)
11349 got_object_commit_close(commit);
11353 got_commit_graph_close(graph);
11355 got_object_commit_close(commit);
11359 static const struct got_error *
11360 cmd_rebase(int argc, char *argv[])
11362 const struct got_error *error = NULL;
11363 struct got_worktree *worktree = NULL;
11364 struct got_repository *repo = NULL;
11365 struct got_fileindex *fileindex = NULL;
11366 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11367 struct got_reference *branch = NULL;
11368 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11369 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11370 struct got_object_id *resume_commit_id = NULL;
11371 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11372 struct got_object_id *head_commit_id = NULL;
11373 struct got_reference *head_ref = NULL;
11374 struct got_commit_object *commit = NULL;
11375 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11376 int histedit_in_progress = 0, merge_in_progress = 0;
11377 int create_backup = 1, list_backups = 0, delete_backups = 0;
11378 int allow_conflict = 0;
11379 struct got_object_id_queue commits;
11380 struct got_pathlist_head merged_paths;
11381 const struct got_object_id_queue *parent_ids;
11382 struct got_object_qid *qid, *pid;
11383 struct got_update_progress_arg upa;
11384 int *pack_fds = NULL;
11386 STAILQ_INIT(&commits);
11387 TAILQ_INIT(&merged_paths);
11388 memset(&upa, 0, sizeof(upa));
11391 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11392 "unveil", NULL) == -1)
11396 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11402 allow_conflict = 1;
11405 continue_rebase = 1;
11411 delete_backups = 1;
11422 if (list_backups) {
11424 option_conflict('l', 'a');
11425 if (allow_conflict)
11426 option_conflict('l', 'C');
11427 if (continue_rebase)
11428 option_conflict('l', 'c');
11429 if (delete_backups)
11430 option_conflict('l', 'X');
11431 if (argc != 0 && argc != 1)
11433 } else if (delete_backups) {
11435 option_conflict('X', 'a');
11436 if (allow_conflict)
11437 option_conflict('X', 'C');
11438 if (continue_rebase)
11439 option_conflict('X', 'c');
11441 option_conflict('l', 'X');
11442 if (argc != 0 && argc != 1)
11444 } else if (allow_conflict) {
11446 option_conflict('C', 'a');
11447 if (!continue_rebase)
11448 errx(1, "-C option requires -c");
11450 if (abort_rebase && continue_rebase)
11452 else if (abort_rebase || continue_rebase) {
11455 } else if (argc != 1)
11459 cwd = getcwd(NULL, 0);
11461 error = got_error_from_errno("getcwd");
11465 error = got_repo_pack_fds_open(&pack_fds);
11469 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11471 if (list_backups || delete_backups) {
11472 if (error->code != GOT_ERR_NOT_WORKTREE)
11475 if (error->code == GOT_ERR_NOT_WORKTREE)
11476 error = wrap_not_worktree_error(error,
11482 error = get_gitconfig_path(&gitconfig_path);
11485 error = got_repo_open(&repo,
11486 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11487 gitconfig_path, pack_fds);
11491 if (worktree != NULL && !list_backups && !delete_backups) {
11492 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11497 error = get_author(&committer, repo, worktree);
11498 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11501 error = apply_unveil(got_repo_get_path(repo), 0,
11502 worktree ? got_worktree_get_root_path(worktree) : NULL);
11506 if (list_backups || delete_backups) {
11507 error = process_backup_refs(
11508 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11509 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11510 goto done; /* nothing else to do */
11513 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11517 if (histedit_in_progress) {
11518 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11522 error = got_worktree_merge_in_progress(&merge_in_progress,
11526 if (merge_in_progress) {
11527 error = got_error(GOT_ERR_MERGE_BUSY);
11531 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11535 if (abort_rebase) {
11536 if (!rebase_in_progress) {
11537 error = got_error(GOT_ERR_NOT_REBASING);
11540 error = got_worktree_rebase_continue(&resume_commit_id,
11541 &new_base_branch, &tmp_branch, &branch, &fileindex,
11545 printf("Switching work tree to %s\n",
11546 got_ref_get_symref_target(new_base_branch));
11547 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11548 new_base_branch, abort_progress, &upa);
11551 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11552 print_merge_progress_stats(&upa);
11553 goto done; /* nothing else to do */
11556 if (continue_rebase) {
11557 if (!rebase_in_progress) {
11558 error = got_error(GOT_ERR_NOT_REBASING);
11561 error = got_worktree_rebase_continue(&resume_commit_id,
11562 &new_base_branch, &tmp_branch, &branch, &fileindex,
11567 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11568 committer, resume_commit_id, allow_conflict, repo);
11572 yca_id = got_object_id_dup(resume_commit_id);
11573 if (yca_id == NULL) {
11574 error = got_error_from_errno("got_object_id_dup");
11578 error = got_ref_open(&branch, repo, argv[0], 0);
11581 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11582 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11583 "will not rebase a branch which lives outside "
11584 "the \"refs/heads/\" reference namespace");
11589 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11593 if (!continue_rebase) {
11594 struct got_object_id *base_commit_id;
11596 error = got_ref_open(&head_ref, repo,
11597 got_worktree_get_head_ref_name(worktree), 0);
11600 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11603 base_commit_id = got_worktree_get_base_commit_id(worktree);
11604 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11605 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11609 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11610 base_commit_id, branch_head_commit_id, 1, 0,
11611 repo, check_cancelled, NULL);
11613 if (error->code == GOT_ERR_ANCESTRY) {
11614 error = got_error_msg(GOT_ERR_ANCESTRY,
11615 "specified branch shares no common "
11616 "ancestry with work tree's branch");
11622 * If a merge commit appears between the new base branch tip
11623 * and a YCA found via first-parent traversal then we might
11624 * find a better YCA using topologically sorted commits.
11626 if (got_object_id_cmp(base_commit_id, yca_id) != 0) {
11627 struct got_object_id *better_yca_id;
11628 error = find_merge_commit_yca(&better_yca_id,
11629 branch_head_commit_id, yca_id,
11630 base_commit_id, repo);
11633 if (better_yca_id) {
11635 yca_id = better_yca_id;
11639 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11640 struct got_pathlist_head paths;
11641 printf("%s is already based on %s\n",
11642 got_ref_get_name(branch),
11643 got_worktree_get_head_ref_name(worktree));
11644 error = switch_head_ref(branch, branch_head_commit_id,
11648 error = got_worktree_set_base_commit_id(worktree, repo,
11649 branch_head_commit_id);
11652 TAILQ_INIT(&paths);
11653 error = got_pathlist_append(&paths, "", NULL);
11656 error = got_worktree_checkout_files(worktree,
11657 &paths, repo, update_progress, &upa,
11658 check_cancelled, NULL);
11659 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11662 if (upa.did_something) {
11664 error = got_object_id_str(&id_str,
11665 branch_head_commit_id);
11668 printf("Updated to %s: %s\n",
11669 got_worktree_get_head_ref_name(worktree),
11673 printf("Already up-to-date\n");
11674 print_update_progress_stats(&upa);
11679 commit_id = branch_head_commit_id;
11680 error = got_object_open_as_commit(&commit, repo, commit_id);
11684 parent_ids = got_object_commit_get_parent_ids(commit);
11685 pid = STAILQ_FIRST(parent_ids);
11687 error = collect_commits(&commits, commit_id, &pid->id,
11688 yca_id, got_worktree_get_path_prefix(worktree),
11689 GOT_ERR_REBASE_PATH, repo);
11694 got_object_commit_close(commit);
11697 if (!continue_rebase) {
11698 error = got_worktree_rebase_prepare(&new_base_branch,
11699 &tmp_branch, &fileindex, worktree, branch, repo);
11704 if (STAILQ_EMPTY(&commits)) {
11705 if (continue_rebase) {
11706 error = rebase_complete(worktree, fileindex,
11707 branch, tmp_branch, repo, create_backup);
11710 /* Fast-forward the reference of the branch. */
11711 struct got_object_id *new_head_commit_id;
11713 error = got_ref_resolve(&new_head_commit_id, repo,
11717 error = got_object_id_str(&id_str, new_head_commit_id);
11720 printf("Forwarding %s to commit %s\n",
11721 got_ref_get_name(branch), id_str);
11723 error = got_ref_change_ref(branch,
11724 new_head_commit_id);
11727 /* No backup needed since objects did not change. */
11733 STAILQ_FOREACH(qid, &commits, entry) {
11735 commit_id = &qid->id;
11736 parent_id = pid ? &pid->id : yca_id;
11739 memset(&upa, 0, sizeof(upa));
11740 error = got_worktree_rebase_merge_files(&merged_paths,
11741 worktree, fileindex, parent_id, commit_id, repo,
11742 update_progress, &upa, check_cancelled, NULL);
11746 print_merge_progress_stats(&upa);
11747 if (upa.conflicts > 0 || upa.missing > 0 ||
11748 upa.not_deleted > 0 || upa.unversioned > 0) {
11749 if (upa.conflicts > 0) {
11750 error = show_rebase_merge_conflict(&qid->id,
11755 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11759 error = rebase_commit(&merged_paths, worktree, fileindex,
11760 tmp_branch, committer, commit_id, 0, repo);
11761 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11766 if (upa.conflicts > 0 || upa.missing > 0 ||
11767 upa.not_deleted > 0 || upa.unversioned > 0) {
11768 error = got_worktree_rebase_postpone(worktree, fileindex);
11771 if (upa.conflicts > 0 && upa.missing == 0 &&
11772 upa.not_deleted == 0 && upa.unversioned == 0) {
11773 error = got_error_msg(GOT_ERR_CONFLICTS,
11774 "conflicts must be resolved before rebasing "
11776 } else if (upa.conflicts > 0) {
11777 error = got_error_msg(GOT_ERR_CONFLICTS,
11778 "conflicts must be resolved before rebasing "
11779 "can continue; changes destined for some "
11780 "files were not yet merged and should be "
11781 "merged manually if required before the "
11782 "rebase operation is continued");
11784 error = got_error_msg(GOT_ERR_CONFLICTS,
11785 "changes destined for some files were not "
11786 "yet merged and should be merged manually "
11787 "if required before the rebase operation "
11791 error = rebase_complete(worktree, fileindex, branch,
11792 tmp_branch, repo, create_backup);
11796 free(gitconfig_path);
11797 got_object_id_queue_free(&commits);
11798 free(branch_head_commit_id);
11799 free(resume_commit_id);
11800 free(head_commit_id);
11803 got_object_commit_close(commit);
11805 got_ref_close(branch);
11806 if (new_base_branch)
11807 got_ref_close(new_base_branch);
11809 got_ref_close(tmp_branch);
11811 got_ref_close(head_ref);
11813 got_worktree_close(worktree);
11815 const struct got_error *close_err = got_repo_close(repo);
11820 const struct got_error *pack_err =
11821 got_repo_pack_fds_close(pack_fds);
11829 usage_histedit(void)
11831 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11832 "[branch]\n", getprogname());
11836 #define GOT_HISTEDIT_PICK 'p'
11837 #define GOT_HISTEDIT_EDIT 'e'
11838 #define GOT_HISTEDIT_FOLD 'f'
11839 #define GOT_HISTEDIT_DROP 'd'
11840 #define GOT_HISTEDIT_MESG 'm'
11842 static const struct got_histedit_cmd {
11843 unsigned char code;
11846 } got_histedit_cmds[] = {
11847 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11848 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11849 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11851 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11852 { GOT_HISTEDIT_MESG, "mesg", "open editor to edit the log message" },
11855 struct got_histedit_list_entry {
11856 TAILQ_ENTRY(got_histedit_list_entry) entry;
11857 struct got_object_id *commit_id;
11858 const struct got_histedit_cmd *cmd;
11861 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11863 static const struct got_error *
11864 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11865 FILE *f, struct got_repository *repo)
11867 const struct got_error *err = NULL;
11868 char *logmsg = NULL, *id_str = NULL;
11869 struct got_commit_object *commit = NULL;
11872 err = got_object_open_as_commit(&commit, repo, commit_id);
11876 err = get_short_logmsg(&logmsg, 34, commit);
11880 err = got_object_id_str(&id_str, commit_id);
11884 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11886 err = got_ferror(f, GOT_ERR_IO);
11889 got_object_commit_close(commit);
11895 static const struct got_error *
11896 histedit_write_commit_list(struct got_object_id_queue *commits,
11897 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11898 int edit_only, struct got_repository *repo)
11900 const struct got_error *err = NULL;
11901 struct got_object_qid *qid;
11902 const char *histedit_cmd = NULL;
11904 if (STAILQ_EMPTY(commits))
11905 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11907 STAILQ_FOREACH(qid, commits, entry) {
11908 histedit_cmd = got_histedit_cmds[0].name;
11910 histedit_cmd = "drop";
11911 else if (edit_only)
11912 histedit_cmd = "edit";
11913 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11914 histedit_cmd = "fold";
11915 else if (edit_logmsg_only)
11916 histedit_cmd = "mesg";
11917 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11925 static const struct got_error *
11926 write_cmd_list(FILE *f, const char *branch_name,
11927 struct got_object_id_queue *commits)
11929 const struct got_error *err = NULL;
11933 struct got_object_qid *qid;
11935 qid = STAILQ_FIRST(commits);
11936 err = got_object_id_str(&id_str, &qid->id);
11941 "# Editing the history of branch '%s' starting at\n"
11943 "# Commits will be processed in order from top to "
11944 "bottom of this file.\n", branch_name, id_str);
11946 err = got_ferror(f, GOT_ERR_IO);
11950 n = fprintf(f, "# Available histedit commands:\n");
11952 err = got_ferror(f, GOT_ERR_IO);
11956 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11957 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11958 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11961 err = got_ferror(f, GOT_ERR_IO);
11970 static const struct got_error *
11971 histedit_syntax_error(int lineno)
11973 static char msg[42];
11976 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11978 if (ret < 0 || (size_t)ret >= sizeof(msg))
11979 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11981 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11984 static const struct got_error *
11985 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11986 char *logmsg, struct got_repository *repo)
11988 const struct got_error *err;
11989 struct got_commit_object *folded_commit = NULL;
11990 char *id_str, *folded_logmsg = NULL;
11992 err = got_object_id_str(&id_str, hle->commit_id);
11996 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
12000 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
12003 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
12004 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
12005 folded_logmsg) == -1) {
12006 err = got_error_from_errno("asprintf");
12010 got_object_commit_close(folded_commit);
12012 free(folded_logmsg);
12016 static struct got_histedit_list_entry *
12017 get_folded_commits(struct got_histedit_list_entry *hle)
12019 struct got_histedit_list_entry *prev, *folded = NULL;
12021 prev = TAILQ_PREV(hle, got_histedit_list, entry);
12022 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
12023 prev->cmd->code == GOT_HISTEDIT_DROP)) {
12024 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
12026 prev = TAILQ_PREV(prev, got_histedit_list, entry);
12032 static const struct got_error *
12033 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
12034 const char *editor, struct got_repository *repo)
12036 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
12037 char *logmsg = NULL, *new_msg = NULL;
12038 const struct got_error *err = NULL;
12039 struct got_commit_object *commit = NULL;
12042 struct got_histedit_list_entry *folded = NULL;
12044 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12048 folded = get_folded_commits(hle);
12050 while (folded != hle) {
12051 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
12052 folded = TAILQ_NEXT(folded, entry);
12055 err = append_folded_commit_msg(&new_msg, folded,
12061 folded = TAILQ_NEXT(folded, entry);
12065 err = got_object_id_str(&id_str, hle->commit_id);
12068 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
12071 logmsg_len = asprintf(&new_msg,
12072 "%s\n# original log message of commit %s: %s",
12073 logmsg ? logmsg : "", id_str, orig_logmsg);
12074 if (logmsg_len == -1) {
12075 err = got_error_from_errno("asprintf");
12081 err = got_object_id_str(&id_str, hle->commit_id);
12085 err = got_opentemp_named_fd(&logmsg_path, &fd,
12086 GOT_TMPDIR_STR "/got-logmsg", "");
12090 if (write(fd, logmsg, logmsg_len) == -1) {
12091 err = got_error_from_errno2("write", logmsg_path);
12094 if (close(fd) == -1) {
12095 err = got_error_from_errno2("close", logmsg_path);
12100 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
12103 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
12106 hle->logmsg = strdup(new_msg);
12107 if (hle->logmsg == NULL)
12108 err = got_error_from_errno("strdup");
12111 if (fd != -1 && close(fd) == -1 && err == NULL)
12112 err = got_error_from_errno2("close", logmsg_path);
12113 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
12114 err = got_error_from_errno2("unlink", logmsg_path);
12119 got_object_commit_close(commit);
12123 static const struct got_error *
12124 histedit_parse_list(struct got_histedit_list *histedit_cmds,
12125 FILE *f, struct got_repository *repo)
12127 const struct got_error *err = NULL;
12128 char *line = NULL, *p, *end;
12129 size_t i, linesize = 0;
12132 const struct got_histedit_cmd *cmd;
12133 struct got_object_id *commit_id = NULL;
12134 struct got_histedit_list_entry *hle = NULL;
12137 linelen = getline(&line, &linesize, f);
12138 if (linelen == -1) {
12139 const struct got_error *getline_err;
12142 getline_err = got_error_from_errno("getline");
12143 err = got_ferror(f, getline_err->code);
12148 while (isspace((unsigned char)p[0]))
12150 if (p[0] == '#' || p[0] == '\0')
12153 for (i = 0; i < nitems(got_histedit_cmds); i++) {
12154 cmd = &got_histedit_cmds[i];
12155 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
12156 isspace((unsigned char)p[strlen(cmd->name)])) {
12157 p += strlen(cmd->name);
12160 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
12165 if (i == nitems(got_histedit_cmds)) {
12166 err = histedit_syntax_error(lineno);
12169 while (isspace((unsigned char)p[0]))
12172 while (end[0] && !isspace((unsigned char)end[0]))
12175 err = got_object_resolve_id_str(&commit_id, repo, p);
12177 /* override error code */
12178 err = histedit_syntax_error(lineno);
12181 hle = malloc(sizeof(*hle));
12183 err = got_error_from_errno("malloc");
12187 hle->commit_id = commit_id;
12188 hle->logmsg = NULL;
12190 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12198 static const struct got_error *
12199 histedit_check_script(struct got_histedit_list *histedit_cmds,
12200 struct got_object_id_queue *commits, struct got_repository *repo)
12202 const struct got_error *err = NULL;
12203 struct got_object_qid *qid;
12204 struct got_histedit_list_entry *hle;
12205 static char msg[92];
12208 if (TAILQ_EMPTY(histedit_cmds))
12209 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12210 "histedit script contains no commands");
12211 if (STAILQ_EMPTY(commits))
12212 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12214 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12215 struct got_histedit_list_entry *hle2;
12216 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12219 if (got_object_id_cmp(hle->commit_id,
12220 hle2->commit_id) != 0)
12222 err = got_object_id_str(&id_str, hle->commit_id);
12225 snprintf(msg, sizeof(msg), "commit %s is listed "
12226 "more than once in histedit script", id_str);
12228 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12232 STAILQ_FOREACH(qid, commits, entry) {
12233 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12234 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12238 err = got_object_id_str(&id_str, &qid->id);
12241 snprintf(msg, sizeof(msg),
12242 "commit %s missing from histedit script", id_str);
12244 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12248 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12249 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12250 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12251 "last commit in histedit script cannot be folded");
12256 static const struct got_error *
12257 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12258 const char *editor, const char *path,
12259 struct got_object_id_queue *commits, struct got_repository *repo)
12261 const struct got_error *err = NULL;
12262 struct stat st, st2;
12263 struct timespec timeout;
12266 if (stat(path, &st) == -1) {
12267 err = got_error_from_errno2("stat", path);
12271 if (spawn_editor(editor, path) == -1) {
12272 err = got_error_from_errno("failed spawning editor");
12276 timeout.tv_sec = 0;
12277 timeout.tv_nsec = 1;
12278 nanosleep(&timeout, NULL);
12280 if (stat(path, &st2) == -1) {
12281 err = got_error_from_errno2("stat", path);
12285 if (st.st_size == st2.st_size &&
12286 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12287 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12288 "no changes made to histedit script, aborting");
12292 f = fopen(path, "re");
12294 err = got_error_from_errno("fopen");
12297 err = histedit_parse_list(histedit_cmds, f, repo);
12301 err = histedit_check_script(histedit_cmds, commits, repo);
12303 if (f && fclose(f) == EOF && err == NULL)
12304 err = got_error_from_errno("fclose");
12308 static const struct got_error *
12309 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12310 struct got_object_id_queue *, const char *, const char *, const char *,
12311 struct got_repository *);
12313 static const struct got_error *
12314 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12315 struct got_object_id_queue *commits, const char *branch_name,
12316 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12317 const char *editor, struct got_repository *repo)
12319 const struct got_error *err;
12323 err = got_opentemp_named(&path, &f, "got-histedit", "");
12327 err = write_cmd_list(f, branch_name, commits);
12331 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12332 fold_only, drop_only, edit_only, repo);
12336 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12338 err = histedit_parse_list(histedit_cmds, f, repo);
12340 if (fclose(f) == EOF) {
12341 err = got_error_from_errno("fclose");
12345 err = histedit_run_editor(histedit_cmds, editor, path,
12348 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12349 err->code != GOT_ERR_HISTEDIT_CMD)
12351 err = histedit_edit_list_retry(histedit_cmds, err,
12352 commits, editor, path, branch_name, repo);
12356 if (f && fclose(f) == EOF && err == NULL)
12357 err = got_error_from_errno("fclose");
12358 if (path && unlink(path) != 0 && err == NULL)
12359 err = got_error_from_errno2("unlink", path);
12364 static const struct got_error *
12365 histedit_save_list(struct got_histedit_list *histedit_cmds,
12366 struct got_worktree *worktree, struct got_repository *repo)
12368 const struct got_error *err = NULL;
12371 struct got_histedit_list_entry *hle;
12373 err = got_worktree_get_histedit_script_path(&path, worktree);
12377 f = fopen(path, "we");
12379 err = got_error_from_errno2("fopen", path);
12382 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12383 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12389 if (f && fclose(f) == EOF && err == NULL)
12390 err = got_error_from_errno("fclose");
12396 histedit_free_list(struct got_histedit_list *histedit_cmds)
12398 struct got_histedit_list_entry *hle;
12400 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12401 TAILQ_REMOVE(histedit_cmds, hle, entry);
12406 static const struct got_error *
12407 histedit_load_list(struct got_histedit_list *histedit_cmds,
12408 const char *path, struct got_repository *repo)
12410 const struct got_error *err = NULL;
12413 f = fopen(path, "re");
12415 err = got_error_from_errno2("fopen", path);
12419 err = histedit_parse_list(histedit_cmds, f, repo);
12421 if (f && fclose(f) == EOF && err == NULL)
12422 err = got_error_from_errno("fclose");
12426 static const struct got_error *
12427 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12428 const struct got_error *edit_err, struct got_object_id_queue *commits,
12429 const char *editor, const char *path, const char *branch_name,
12430 struct got_repository *repo)
12432 const struct got_error *err = NULL, *prev_err = edit_err;
12435 while (resp != 'c' && resp != 'r' && resp != 'a') {
12436 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12437 "or (a)bort: ", getprogname(), prev_err->msg);
12442 histedit_free_list(histedit_cmds);
12443 err = histedit_run_editor(histedit_cmds, editor, path,
12446 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12447 err->code != GOT_ERR_HISTEDIT_CMD)
12454 } else if (resp == 'r') {
12455 histedit_free_list(histedit_cmds);
12456 err = histedit_edit_script(histedit_cmds,
12457 commits, branch_name, 0, 0, 0, 0, editor, repo);
12459 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12460 err->code != GOT_ERR_HISTEDIT_CMD)
12467 } else if (resp == 'a') {
12468 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12471 printf("invalid response '%c'\n", resp);
12477 static const struct got_error *
12478 histedit_complete(struct got_worktree *worktree,
12479 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12480 struct got_reference *branch, struct got_repository *repo)
12482 printf("Switching work tree to %s\n",
12483 got_ref_get_symref_target(branch));
12484 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12488 static const struct got_error *
12489 show_histedit_progress(struct got_commit_object *commit,
12490 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12492 const struct got_error *err;
12493 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12495 err = got_object_id_str(&old_id_str, hle->commit_id);
12500 err = got_object_id_str(&new_id_str, new_id);
12505 old_id_str[12] = '\0';
12507 new_id_str[12] = '\0';
12510 logmsg = strdup(hle->logmsg);
12511 if (logmsg == NULL) {
12512 err = got_error_from_errno("strdup");
12515 trim_logmsg(logmsg, 42);
12517 err = get_short_logmsg(&logmsg, 42, commit);
12522 switch (hle->cmd->code) {
12523 case GOT_HISTEDIT_PICK:
12524 case GOT_HISTEDIT_EDIT:
12525 case GOT_HISTEDIT_MESG:
12526 printf("%s -> %s: %s\n", old_id_str,
12527 new_id_str ? new_id_str : "no-op change", logmsg);
12529 case GOT_HISTEDIT_DROP:
12530 case GOT_HISTEDIT_FOLD:
12531 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12543 static const struct got_error *
12544 histedit_commit(struct got_pathlist_head *merged_paths,
12545 struct got_worktree *worktree, struct got_fileindex *fileindex,
12546 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12547 const char *committer, int allow_conflict, const char *editor,
12548 struct got_repository *repo)
12550 const struct got_error *err;
12551 struct got_commit_object *commit;
12552 struct got_object_id *new_commit_id;
12554 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12555 && hle->logmsg == NULL) {
12556 err = histedit_edit_logmsg(hle, editor, repo);
12561 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12565 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12566 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12567 hle->logmsg, allow_conflict, repo);
12569 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12571 err = show_histedit_progress(commit, hle, NULL);
12573 err = show_histedit_progress(commit, hle, new_commit_id);
12574 free(new_commit_id);
12577 got_object_commit_close(commit);
12581 static const struct got_error *
12582 histedit_skip_commit(struct got_histedit_list_entry *hle,
12583 struct got_worktree *worktree, struct got_repository *repo)
12585 const struct got_error *error;
12586 struct got_commit_object *commit;
12588 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12593 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12597 error = show_histedit_progress(commit, hle, NULL);
12598 got_object_commit_close(commit);
12602 static const struct got_error *
12603 check_local_changes(void *arg, unsigned char status,
12604 unsigned char staged_status, const char *path,
12605 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12606 struct got_object_id *commit_id, int dirfd, const char *de_name)
12608 int *have_local_changes = arg;
12611 case GOT_STATUS_ADD:
12612 case GOT_STATUS_DELETE:
12613 case GOT_STATUS_MODIFY:
12614 case GOT_STATUS_CONFLICT:
12615 *have_local_changes = 1;
12616 return got_error(GOT_ERR_CANCELLED);
12621 switch (staged_status) {
12622 case GOT_STATUS_ADD:
12623 case GOT_STATUS_DELETE:
12624 case GOT_STATUS_MODIFY:
12625 *have_local_changes = 1;
12626 return got_error(GOT_ERR_CANCELLED);
12634 static const struct got_error *
12635 cmd_histedit(int argc, char *argv[])
12637 const struct got_error *error = NULL;
12638 struct got_worktree *worktree = NULL;
12639 struct got_fileindex *fileindex = NULL;
12640 struct got_repository *repo = NULL;
12641 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12642 struct got_reference *branch = NULL;
12643 struct got_reference *tmp_branch = NULL;
12644 struct got_object_id *resume_commit_id = NULL;
12645 struct got_object_id *base_commit_id = NULL;
12646 struct got_object_id *head_commit_id = NULL;
12647 struct got_commit_object *commit = NULL;
12648 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12649 struct got_update_progress_arg upa;
12650 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12651 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12652 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12653 const char *edit_script_path = NULL;
12654 char *editor = NULL;
12655 struct got_object_id_queue commits;
12656 struct got_pathlist_head merged_paths;
12657 const struct got_object_id_queue *parent_ids;
12658 struct got_object_qid *pid;
12659 struct got_histedit_list histedit_cmds;
12660 struct got_histedit_list_entry *hle;
12661 int *pack_fds = NULL;
12663 STAILQ_INIT(&commits);
12664 TAILQ_INIT(&histedit_cmds);
12665 TAILQ_INIT(&merged_paths);
12666 memset(&upa, 0, sizeof(upa));
12669 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12670 "unveil", NULL) == -1)
12674 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12680 allow_conflict = 1;
12692 edit_script_path = optarg;
12701 edit_logmsg_only = 1;
12704 delete_backups = 1;
12715 if (abort_edit && allow_conflict)
12716 option_conflict('a', 'C');
12717 if (abort_edit && continue_edit)
12718 option_conflict('a', 'c');
12719 if (edit_script_path && allow_conflict)
12720 option_conflict('F', 'C');
12721 if (edit_script_path && edit_logmsg_only)
12722 option_conflict('F', 'm');
12723 if (abort_edit && edit_logmsg_only)
12724 option_conflict('a', 'm');
12725 if (edit_logmsg_only && allow_conflict)
12726 option_conflict('m', 'C');
12727 if (continue_edit && edit_logmsg_only)
12728 option_conflict('c', 'm');
12729 if (abort_edit && fold_only)
12730 option_conflict('a', 'f');
12731 if (fold_only && allow_conflict)
12732 option_conflict('f', 'C');
12733 if (continue_edit && fold_only)
12734 option_conflict('c', 'f');
12735 if (fold_only && edit_logmsg_only)
12736 option_conflict('f', 'm');
12737 if (edit_script_path && fold_only)
12738 option_conflict('F', 'f');
12739 if (abort_edit && edit_only)
12740 option_conflict('a', 'e');
12741 if (continue_edit && edit_only)
12742 option_conflict('c', 'e');
12743 if (edit_only && edit_logmsg_only)
12744 option_conflict('e', 'm');
12745 if (edit_script_path && edit_only)
12746 option_conflict('F', 'e');
12747 if (fold_only && edit_only)
12748 option_conflict('f', 'e');
12749 if (drop_only && abort_edit)
12750 option_conflict('d', 'a');
12751 if (drop_only && allow_conflict)
12752 option_conflict('d', 'C');
12753 if (drop_only && continue_edit)
12754 option_conflict('d', 'c');
12755 if (drop_only && edit_logmsg_only)
12756 option_conflict('d', 'm');
12757 if (drop_only && edit_only)
12758 option_conflict('d', 'e');
12759 if (drop_only && edit_script_path)
12760 option_conflict('d', 'F');
12761 if (drop_only && fold_only)
12762 option_conflict('d', 'f');
12763 if (list_backups) {
12765 option_conflict('l', 'a');
12766 if (allow_conflict)
12767 option_conflict('l', 'C');
12769 option_conflict('l', 'c');
12770 if (edit_script_path)
12771 option_conflict('l', 'F');
12772 if (edit_logmsg_only)
12773 option_conflict('l', 'm');
12775 option_conflict('l', 'd');
12777 option_conflict('l', 'f');
12779 option_conflict('l', 'e');
12780 if (delete_backups)
12781 option_conflict('l', 'X');
12782 if (argc != 0 && argc != 1)
12784 } else if (delete_backups) {
12786 option_conflict('X', 'a');
12787 if (allow_conflict)
12788 option_conflict('X', 'C');
12790 option_conflict('X', 'c');
12792 option_conflict('X', 'd');
12793 if (edit_script_path)
12794 option_conflict('X', 'F');
12795 if (edit_logmsg_only)
12796 option_conflict('X', 'm');
12798 option_conflict('X', 'f');
12800 option_conflict('X', 'e');
12802 option_conflict('X', 'l');
12803 if (argc != 0 && argc != 1)
12805 } else if (allow_conflict && !continue_edit)
12806 errx(1, "-C option requires -c");
12807 else if (argc != 0)
12810 cwd = getcwd(NULL, 0);
12812 error = got_error_from_errno("getcwd");
12816 error = got_repo_pack_fds_open(&pack_fds);
12820 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12822 if (list_backups || delete_backups) {
12823 if (error->code != GOT_ERR_NOT_WORKTREE)
12826 if (error->code == GOT_ERR_NOT_WORKTREE)
12827 error = wrap_not_worktree_error(error,
12833 if (list_backups || delete_backups) {
12834 error = got_repo_open(&repo,
12835 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12839 error = apply_unveil(got_repo_get_path(repo), 0,
12840 worktree ? got_worktree_get_root_path(worktree) : NULL);
12843 error = process_backup_refs(
12844 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12845 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12846 goto done; /* nothing else to do */
12848 error = get_gitconfig_path(&gitconfig_path);
12851 error = got_repo_open(&repo,
12852 got_worktree_get_repo_path(worktree), gitconfig_path,
12856 error = get_editor(&editor);
12859 if (unveil(editor, "x") != 0) {
12860 error = got_error_from_errno2("unveil", editor);
12863 if (edit_script_path) {
12864 if (unveil(edit_script_path, "r") != 0) {
12865 error = got_error_from_errno2("unveil",
12870 error = apply_unveil(got_repo_get_path(repo), 0,
12871 got_worktree_get_root_path(worktree));
12876 if (worktree != NULL && !list_backups && !delete_backups) {
12877 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12882 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12885 if (rebase_in_progress) {
12886 error = got_error(GOT_ERR_REBASING);
12890 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12894 if (merge_in_progress) {
12895 error = got_error(GOT_ERR_MERGE_BUSY);
12899 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12903 if (edit_in_progress && edit_logmsg_only) {
12904 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12905 "histedit operation is in progress in this "
12906 "work tree and must be continued or aborted "
12907 "before the -m option can be used");
12910 if (edit_in_progress && drop_only) {
12911 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12912 "histedit operation is in progress in this "
12913 "work tree and must be continued or aborted "
12914 "before the -d option can be used");
12917 if (edit_in_progress && fold_only) {
12918 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12919 "histedit operation is in progress in this "
12920 "work tree and must be continued or aborted "
12921 "before the -f option can be used");
12924 if (edit_in_progress && edit_only) {
12925 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12926 "histedit operation is in progress in this "
12927 "work tree and must be continued or aborted "
12928 "before the -e option can be used");
12932 if (edit_in_progress && abort_edit) {
12933 error = got_worktree_histedit_continue(&resume_commit_id,
12934 &tmp_branch, &branch, &base_commit_id, &fileindex,
12938 printf("Switching work tree to %s\n",
12939 got_ref_get_symref_target(branch));
12940 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12941 branch, base_commit_id, abort_progress, &upa);
12944 printf("Histedit of %s aborted\n",
12945 got_ref_get_symref_target(branch));
12946 print_merge_progress_stats(&upa);
12947 goto done; /* nothing else to do */
12948 } else if (abort_edit) {
12949 error = got_error(GOT_ERR_NOT_HISTEDIT);
12953 error = get_author(&committer, repo, worktree);
12957 if (continue_edit) {
12960 if (!edit_in_progress) {
12961 error = got_error(GOT_ERR_NOT_HISTEDIT);
12965 error = got_worktree_get_histedit_script_path(&path, worktree);
12969 error = histedit_load_list(&histedit_cmds, path, repo);
12974 error = got_worktree_histedit_continue(&resume_commit_id,
12975 &tmp_branch, &branch, &base_commit_id, &fileindex,
12980 error = got_ref_resolve(&head_commit_id, repo, branch);
12984 error = got_object_open_as_commit(&commit, repo,
12988 parent_ids = got_object_commit_get_parent_ids(commit);
12989 pid = STAILQ_FIRST(parent_ids);
12991 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12994 error = collect_commits(&commits, head_commit_id, &pid->id,
12995 base_commit_id, got_worktree_get_path_prefix(worktree),
12996 GOT_ERR_HISTEDIT_PATH, repo);
12997 got_object_commit_close(commit);
13002 if (edit_in_progress) {
13003 error = got_error(GOT_ERR_HISTEDIT_BUSY);
13007 error = got_ref_open(&branch, repo,
13008 got_worktree_get_head_ref_name(worktree), 0);
13012 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
13013 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
13014 "will not edit commit history of a branch outside "
13015 "the \"refs/heads/\" reference namespace");
13019 error = got_ref_resolve(&head_commit_id, repo, branch);
13020 got_ref_close(branch);
13025 error = got_object_open_as_commit(&commit, repo,
13029 parent_ids = got_object_commit_get_parent_ids(commit);
13030 pid = STAILQ_FIRST(parent_ids);
13032 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
13035 error = collect_commits(&commits, head_commit_id, &pid->id,
13036 got_worktree_get_base_commit_id(worktree),
13037 got_worktree_get_path_prefix(worktree),
13038 GOT_ERR_HISTEDIT_PATH, repo);
13039 got_object_commit_close(commit);
13044 if (STAILQ_EMPTY(&commits)) {
13045 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
13049 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
13050 &base_commit_id, &fileindex, worktree, repo);
13054 if (edit_script_path) {
13055 error = histedit_load_list(&histedit_cmds,
13056 edit_script_path, repo);
13058 got_worktree_histedit_abort(worktree, fileindex,
13059 repo, branch, base_commit_id,
13060 abort_progress, &upa);
13061 print_merge_progress_stats(&upa);
13065 const char *branch_name;
13066 branch_name = got_ref_get_symref_target(branch);
13067 if (strncmp(branch_name, "refs/heads/", 11) == 0)
13069 error = histedit_edit_script(&histedit_cmds, &commits,
13070 branch_name, edit_logmsg_only, fold_only,
13071 drop_only, edit_only, editor, repo);
13073 got_worktree_histedit_abort(worktree, fileindex,
13074 repo, branch, base_commit_id,
13075 abort_progress, &upa);
13076 print_merge_progress_stats(&upa);
13082 error = histedit_save_list(&histedit_cmds, worktree,
13085 got_worktree_histedit_abort(worktree, fileindex,
13086 repo, branch, base_commit_id,
13087 abort_progress, &upa);
13088 print_merge_progress_stats(&upa);
13094 error = histedit_check_script(&histedit_cmds, &commits, repo);
13098 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
13099 if (resume_commit_id) {
13100 if (got_object_id_cmp(hle->commit_id,
13101 resume_commit_id) != 0)
13104 resume_commit_id = NULL;
13105 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
13106 hle->cmd->code == GOT_HISTEDIT_FOLD) {
13107 error = histedit_skip_commit(hle, worktree,
13112 struct got_pathlist_head paths;
13113 int have_changes = 0;
13115 TAILQ_INIT(&paths);
13116 error = got_pathlist_append(&paths, "", NULL);
13119 error = got_worktree_status(worktree, &paths,
13120 repo, 0, check_local_changes, &have_changes,
13121 check_cancelled, NULL);
13122 got_pathlist_free(&paths,
13123 GOT_PATHLIST_FREE_NONE);
13125 if (error->code != GOT_ERR_CANCELLED)
13127 if (sigint_received || sigpipe_received)
13130 if (have_changes) {
13131 error = histedit_commit(NULL, worktree,
13132 fileindex, tmp_branch, hle,
13133 committer, allow_conflict, editor,
13138 error = got_object_open_as_commit(
13139 &commit, repo, hle->commit_id);
13142 error = show_histedit_progress(commit,
13144 got_object_commit_close(commit);
13153 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
13154 error = histedit_skip_commit(hle, worktree, repo);
13159 error = got_object_open_as_commit(&commit, repo,
13163 parent_ids = got_object_commit_get_parent_ids(commit);
13164 pid = STAILQ_FIRST(parent_ids);
13166 error = got_worktree_histedit_merge_files(&merged_paths,
13167 worktree, fileindex, &pid->id, hle->commit_id, repo,
13168 update_progress, &upa, check_cancelled, NULL);
13171 got_object_commit_close(commit);
13174 print_merge_progress_stats(&upa);
13175 if (upa.conflicts > 0 || upa.missing > 0 ||
13176 upa.not_deleted > 0 || upa.unversioned > 0) {
13177 if (upa.conflicts > 0) {
13178 error = show_rebase_merge_conflict(
13179 hle->commit_id, repo);
13183 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13187 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13189 error = got_object_id_str(&id_str, hle->commit_id);
13192 printf("Stopping histedit for amending commit %s\n",
13195 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13196 error = got_worktree_histedit_postpone(worktree,
13199 } else if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13200 error = histedit_skip_commit(hle, worktree, repo);
13204 } else if (hle->cmd->code == GOT_HISTEDIT_MESG) {
13205 error = histedit_edit_logmsg(hle, editor, repo);
13210 error = histedit_commit(&merged_paths, worktree, fileindex,
13211 tmp_branch, hle, committer, allow_conflict, editor, repo);
13212 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13217 if (upa.conflicts > 0 || upa.missing > 0 ||
13218 upa.not_deleted > 0 || upa.unversioned > 0) {
13219 error = got_worktree_histedit_postpone(worktree, fileindex);
13222 if (upa.conflicts > 0 && upa.missing == 0 &&
13223 upa.not_deleted == 0 && upa.unversioned == 0) {
13224 error = got_error_msg(GOT_ERR_CONFLICTS,
13225 "conflicts must be resolved before histedit "
13227 } else if (upa.conflicts > 0) {
13228 error = got_error_msg(GOT_ERR_CONFLICTS,
13229 "conflicts must be resolved before histedit "
13230 "can continue; changes destined for some "
13231 "files were not yet merged and should be "
13232 "merged manually if required before the "
13233 "histedit operation is continued");
13235 error = got_error_msg(GOT_ERR_CONFLICTS,
13236 "changes destined for some files were not "
13237 "yet merged and should be merged manually "
13238 "if required before the histedit operation "
13242 error = histedit_complete(worktree, fileindex, tmp_branch,
13248 free(gitconfig_path);
13249 got_object_id_queue_free(&commits);
13250 histedit_free_list(&histedit_cmds);
13251 free(head_commit_id);
13252 free(base_commit_id);
13253 free(resume_commit_id);
13255 got_object_commit_close(commit);
13257 got_ref_close(branch);
13259 got_ref_close(tmp_branch);
13261 got_worktree_close(worktree);
13263 const struct got_error *close_err = got_repo_close(repo);
13268 const struct got_error *pack_err =
13269 got_repo_pack_fds_close(pack_fds);
13277 usage_integrate(void)
13279 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13283 static const struct got_error *
13284 cmd_integrate(int argc, char *argv[])
13286 const struct got_error *error = NULL;
13287 struct got_repository *repo = NULL;
13288 struct got_worktree *worktree = NULL;
13289 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13290 const char *branch_arg = NULL;
13291 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13292 struct got_fileindex *fileindex = NULL;
13293 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13295 struct got_update_progress_arg upa;
13296 int *pack_fds = NULL;
13299 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13300 "unveil", NULL) == -1)
13304 while ((ch = getopt(argc, argv, "")) != -1) {
13317 branch_arg = argv[0];
13319 cwd = getcwd(NULL, 0);
13321 error = got_error_from_errno("getcwd");
13325 error = got_repo_pack_fds_open(&pack_fds);
13329 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13331 if (error->code == GOT_ERR_NOT_WORKTREE)
13332 error = wrap_not_worktree_error(error, "integrate",
13337 error = check_rebase_or_histedit_in_progress(worktree);
13341 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13346 error = apply_unveil(got_repo_get_path(repo), 0,
13347 got_worktree_get_root_path(worktree));
13351 error = check_merge_in_progress(worktree, repo);
13355 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13356 error = got_error_from_errno("asprintf");
13360 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13361 &base_branch_ref, worktree, refname, repo);
13365 refname = strdup(got_ref_get_name(branch_ref));
13366 if (refname == NULL) {
13367 error = got_error_from_errno("strdup");
13368 got_worktree_integrate_abort(worktree, fileindex, repo,
13369 branch_ref, base_branch_ref);
13372 base_refname = strdup(got_ref_get_name(base_branch_ref));
13373 if (base_refname == NULL) {
13374 error = got_error_from_errno("strdup");
13375 got_worktree_integrate_abort(worktree, fileindex, repo,
13376 branch_ref, base_branch_ref);
13379 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13380 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13381 got_worktree_integrate_abort(worktree, fileindex, repo,
13382 branch_ref, base_branch_ref);
13386 error = got_ref_resolve(&commit_id, repo, branch_ref);
13390 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13394 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13395 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13396 "specified branch has already been integrated");
13397 got_worktree_integrate_abort(worktree, fileindex, repo,
13398 branch_ref, base_branch_ref);
13402 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13404 if (error->code == GOT_ERR_ANCESTRY)
13405 error = got_error(GOT_ERR_REBASE_REQUIRED);
13406 got_worktree_integrate_abort(worktree, fileindex, repo,
13407 branch_ref, base_branch_ref);
13411 memset(&upa, 0, sizeof(upa));
13412 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13413 branch_ref, base_branch_ref, update_progress, &upa,
13414 check_cancelled, NULL);
13418 printf("Integrated %s into %s\n", refname, base_refname);
13419 print_update_progress_stats(&upa);
13422 const struct got_error *close_err = got_repo_close(repo);
13427 got_worktree_close(worktree);
13429 const struct got_error *pack_err =
13430 got_repo_pack_fds_close(pack_fds);
13435 free(base_commit_id);
13438 free(base_refname);
13445 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13449 static const struct got_error *
13450 cmd_merge(int argc, char *argv[])
13452 const struct got_error *error = NULL;
13453 struct got_worktree *worktree = NULL;
13454 struct got_repository *repo = NULL;
13455 struct got_fileindex *fileindex = NULL;
13456 char *cwd = NULL, *id_str = NULL, *author = NULL;
13457 char *gitconfig_path = NULL;
13458 struct got_reference *branch = NULL, *wt_branch = NULL;
13459 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13460 struct got_object_id *wt_branch_tip = NULL;
13461 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13462 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13463 struct got_update_progress_arg upa;
13464 struct got_object_id *merge_commit_id = NULL;
13465 char *branch_name = NULL;
13466 int *pack_fds = NULL;
13468 memset(&upa, 0, sizeof(upa));
13471 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13472 "unveil", NULL) == -1)
13476 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13482 allow_conflict = 1;
13485 continue_merge = 1;
13488 prefer_fast_forward = 0;
13491 interrupt_merge = 1;
13503 if (continue_merge)
13504 option_conflict('a', 'c');
13505 if (!prefer_fast_forward)
13506 option_conflict('a', 'M');
13507 if (interrupt_merge)
13508 option_conflict('a', 'n');
13509 } else if (continue_merge) {
13510 if (!prefer_fast_forward)
13511 option_conflict('c', 'M');
13512 if (interrupt_merge)
13513 option_conflict('c', 'n');
13515 if (allow_conflict) {
13516 if (!continue_merge)
13517 errx(1, "-C option requires -c");
13519 if (abort_merge || continue_merge) {
13522 } else if (argc != 1)
13525 cwd = getcwd(NULL, 0);
13527 error = got_error_from_errno("getcwd");
13531 error = got_repo_pack_fds_open(&pack_fds);
13535 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13537 if (error->code == GOT_ERR_NOT_WORKTREE)
13538 error = wrap_not_worktree_error(error,
13543 error = get_gitconfig_path(&gitconfig_path);
13546 error = got_repo_open(&repo,
13547 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13548 gitconfig_path, pack_fds);
13552 if (worktree != NULL) {
13553 error = worktree_has_logmsg_ref("merge", worktree, repo);
13558 error = apply_unveil(got_repo_get_path(repo), 0,
13559 worktree ? got_worktree_get_root_path(worktree) : NULL);
13563 error = check_rebase_or_histedit_in_progress(worktree);
13567 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13572 if (merge_in_progress && !(abort_merge || continue_merge)) {
13573 error = got_error(GOT_ERR_MERGE_BUSY);
13577 if (!merge_in_progress && (abort_merge || continue_merge)) {
13578 error = got_error(GOT_ERR_NOT_MERGING);
13583 error = got_worktree_merge_continue(&branch_name,
13584 &branch_tip, &fileindex, worktree, repo);
13587 error = got_worktree_merge_abort(worktree, fileindex, repo,
13588 abort_progress, &upa);
13591 printf("Merge of %s aborted\n", branch_name);
13592 goto done; /* nothing else to do */
13595 if (strncmp(got_worktree_get_head_ref_name(worktree),
13596 "refs/heads/", 11) != 0) {
13597 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13598 "work tree's current branch %s is outside the "
13599 "\"refs/heads/\" reference namespace; "
13600 "update -b required",
13601 got_worktree_get_head_ref_name(worktree));
13605 error = get_author(&author, repo, worktree);
13609 error = got_ref_open(&wt_branch, repo,
13610 got_worktree_get_head_ref_name(worktree), 0);
13613 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13617 if (continue_merge) {
13618 struct got_object_id *base_commit_id;
13619 base_commit_id = got_worktree_get_base_commit_id(worktree);
13620 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13621 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13624 error = got_worktree_merge_continue(&branch_name,
13625 &branch_tip, &fileindex, worktree, repo);
13629 error = got_ref_open(&branch, repo, argv[0], 0);
13632 branch_name = strdup(got_ref_get_name(branch));
13633 if (branch_name == NULL) {
13634 error = got_error_from_errno("strdup");
13637 error = got_ref_resolve(&branch_tip, repo, branch);
13642 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13643 wt_branch_tip, branch_tip, 0, 0, repo,
13644 check_cancelled, NULL);
13645 if (error && error->code != GOT_ERR_ANCESTRY)
13648 if (!continue_merge) {
13649 error = check_path_prefix(wt_branch_tip, branch_tip,
13650 got_worktree_get_path_prefix(worktree),
13651 GOT_ERR_MERGE_PATH, repo);
13654 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13657 if (prefer_fast_forward && yca_id &&
13658 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13659 struct got_pathlist_head paths;
13660 if (interrupt_merge) {
13661 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13662 "there are no changes to merge since %s "
13663 "is already based on %s; merge cannot be "
13664 "interrupted for amending; -n",
13665 branch_name, got_ref_get_name(wt_branch));
13668 printf("Forwarding %s to %s\n",
13669 got_ref_get_name(wt_branch), branch_name);
13670 error = got_ref_change_ref(wt_branch, branch_tip);
13673 error = got_ref_write(wt_branch, repo);
13676 error = got_worktree_set_base_commit_id(worktree, repo,
13680 TAILQ_INIT(&paths);
13681 error = got_pathlist_append(&paths, "", NULL);
13684 error = got_worktree_checkout_files(worktree,
13685 &paths, repo, update_progress, &upa,
13686 check_cancelled, NULL);
13687 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13690 if (upa.did_something) {
13692 error = got_object_id_str(&id_str, branch_tip);
13695 printf("Updated to commit %s\n", id_str);
13698 printf("Already up-to-date\n");
13699 print_update_progress_stats(&upa);
13702 error = got_worktree_merge_write_refs(worktree, branch, repo);
13706 error = got_worktree_merge_branch(worktree, fileindex,
13707 yca_id, branch_tip, repo, update_progress, &upa,
13708 check_cancelled, NULL);
13711 print_merge_progress_stats(&upa);
13712 if (!upa.did_something) {
13713 error = got_worktree_merge_abort(worktree, fileindex,
13714 repo, abort_progress, &upa);
13717 printf("Already up-to-date\n");
13722 if (interrupt_merge) {
13723 error = got_worktree_merge_postpone(worktree, fileindex);
13726 printf("Merge of %s interrupted on request\n", branch_name);
13727 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13728 upa.not_deleted > 0 || upa.unversioned > 0) {
13729 error = got_worktree_merge_postpone(worktree, fileindex);
13732 if (upa.conflicts > 0 && upa.missing == 0 &&
13733 upa.not_deleted == 0 && upa.unversioned == 0) {
13734 error = got_error_msg(GOT_ERR_CONFLICTS,
13735 "conflicts must be resolved before merging "
13737 } else if (upa.conflicts > 0) {
13738 error = got_error_msg(GOT_ERR_CONFLICTS,
13739 "conflicts must be resolved before merging "
13740 "can continue; changes destined for some "
13741 "files were not yet merged and "
13742 "should be merged manually if required before the "
13743 "merge operation is continued");
13745 error = got_error_msg(GOT_ERR_CONFLICTS,
13746 "changes destined for some "
13747 "files were not yet merged and should be "
13748 "merged manually if required before the "
13749 "merge operation is continued");
13753 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13754 fileindex, author, NULL, 1, branch_tip, branch_name,
13755 allow_conflict, repo, continue_merge ? print_status : NULL,
13759 error = got_worktree_merge_complete(worktree, fileindex, repo);
13762 error = got_object_id_str(&id_str, merge_commit_id);
13765 printf("Merged %s into %s: %s\n", branch_name,
13766 got_worktree_get_head_ref_name(worktree),
13771 free(gitconfig_path);
13773 free(merge_commit_id);
13779 got_ref_close(branch);
13781 got_ref_close(wt_branch);
13783 got_worktree_close(worktree);
13785 const struct got_error *close_err = got_repo_close(repo);
13790 const struct got_error *pack_err =
13791 got_repo_pack_fds_close(pack_fds);
13801 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13802 "[path ...]\n", getprogname());
13806 static const struct got_error *
13807 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13808 const char *path, struct got_object_id *blob_id,
13809 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13810 int dirfd, const char *de_name)
13812 const struct got_error *err = NULL;
13813 char *id_str = NULL;
13815 if (staged_status != GOT_STATUS_ADD &&
13816 staged_status != GOT_STATUS_MODIFY &&
13817 staged_status != GOT_STATUS_DELETE)
13820 if (staged_status == GOT_STATUS_ADD ||
13821 staged_status == GOT_STATUS_MODIFY)
13822 err = got_object_id_str(&id_str, staged_blob_id);
13824 err = got_object_id_str(&id_str, blob_id);
13828 printf("%s %c %s\n", id_str, staged_status, path);
13833 static const struct got_error *
13834 cmd_stage(int argc, char *argv[])
13836 const struct got_error *error = NULL;
13837 struct got_repository *repo = NULL;
13838 struct got_worktree *worktree = NULL;
13840 struct got_pathlist_head paths;
13841 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13842 FILE *patch_script_file = NULL;
13843 const char *patch_script_path = NULL;
13844 struct choose_patch_arg cpa;
13845 int *pack_fds = NULL;
13847 TAILQ_INIT(&paths);
13850 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13851 "unveil", NULL) == -1)
13855 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13858 patch_script_path = optarg;
13867 allow_bad_symlinks = 1;
13878 if (list_stage && (pflag || patch_script_path))
13879 errx(1, "-l option cannot be used with other options");
13880 if (patch_script_path && !pflag)
13881 errx(1, "-F option can only be used together with -p option");
13883 cwd = getcwd(NULL, 0);
13885 error = got_error_from_errno("getcwd");
13889 error = got_repo_pack_fds_open(&pack_fds);
13893 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13895 if (error->code == GOT_ERR_NOT_WORKTREE)
13896 error = wrap_not_worktree_error(error, "stage", cwd);
13900 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13905 if (patch_script_path) {
13906 patch_script_file = fopen(patch_script_path, "re");
13907 if (patch_script_file == NULL) {
13908 error = got_error_from_errno2("fopen",
13909 patch_script_path);
13913 error = apply_unveil(got_repo_get_path(repo), 0,
13914 got_worktree_get_root_path(worktree));
13918 error = check_merge_in_progress(worktree, repo);
13922 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13927 error = got_worktree_status(worktree, &paths, repo, 0,
13928 print_stage, NULL, check_cancelled, NULL);
13930 cpa.patch_script_file = patch_script_file;
13931 cpa.action = "stage";
13932 error = got_worktree_stage(worktree, &paths,
13933 pflag ? NULL : print_status, NULL,
13934 pflag ? choose_patch : NULL, &cpa,
13935 allow_bad_symlinks, repo);
13938 if (patch_script_file && fclose(patch_script_file) == EOF &&
13940 error = got_error_from_errno2("fclose", patch_script_path);
13942 const struct got_error *close_err = got_repo_close(repo);
13947 got_worktree_close(worktree);
13949 const struct got_error *pack_err =
13950 got_repo_pack_fds_close(pack_fds);
13954 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13960 usage_unstage(void)
13962 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13963 "[path ...]\n", getprogname());
13968 static const struct got_error *
13969 cmd_unstage(int argc, char *argv[])
13971 const struct got_error *error = NULL;
13972 struct got_repository *repo = NULL;
13973 struct got_worktree *worktree = NULL;
13975 struct got_pathlist_head paths;
13977 struct got_update_progress_arg upa;
13978 FILE *patch_script_file = NULL;
13979 const char *patch_script_path = NULL;
13980 struct choose_patch_arg cpa;
13981 int *pack_fds = NULL;
13983 TAILQ_INIT(&paths);
13986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13987 "unveil", NULL) == -1)
13991 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13994 patch_script_path = optarg;
14008 if (patch_script_path && !pflag)
14009 errx(1, "-F option can only be used together with -p option");
14011 cwd = getcwd(NULL, 0);
14013 error = got_error_from_errno("getcwd");
14017 error = got_repo_pack_fds_open(&pack_fds);
14021 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14023 if (error->code == GOT_ERR_NOT_WORKTREE)
14024 error = wrap_not_worktree_error(error, "unstage", cwd);
14028 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
14033 if (patch_script_path) {
14034 patch_script_file = fopen(patch_script_path, "re");
14035 if (patch_script_file == NULL) {
14036 error = got_error_from_errno2("fopen",
14037 patch_script_path);
14042 error = apply_unveil(got_repo_get_path(repo), 0,
14043 got_worktree_get_root_path(worktree));
14047 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
14051 cpa.patch_script_file = patch_script_file;
14052 cpa.action = "unstage";
14053 memset(&upa, 0, sizeof(upa));
14054 error = got_worktree_unstage(worktree, &paths, update_progress,
14055 &upa, pflag ? choose_patch : NULL, &cpa, repo);
14057 print_merge_progress_stats(&upa);
14059 if (patch_script_file && fclose(patch_script_file) == EOF &&
14061 error = got_error_from_errno2("fclose", patch_script_path);
14063 const struct got_error *close_err = got_repo_close(repo);
14068 got_worktree_close(worktree);
14070 const struct got_error *pack_err =
14071 got_repo_pack_fds_close(pack_fds);
14075 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14083 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14084 "arg ...\n", getprogname());
14088 static const struct got_error *
14089 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14091 const struct got_error *err;
14092 struct got_blob_object *blob;
14095 fd = got_opentempfd();
14097 return got_error_from_errno("got_opentempfd");
14099 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
14103 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
14105 if (fd != -1 && close(fd) == -1 && err == NULL)
14106 err = got_error_from_errno("close");
14108 got_object_blob_close(blob);
14112 static const struct got_error *
14113 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14115 const struct got_error *err;
14116 struct got_tree_object *tree;
14119 err = got_object_open_as_tree(&tree, repo, id);
14123 nentries = got_object_tree_get_nentries(tree);
14124 for (i = 0; i < nentries; i++) {
14125 struct got_tree_entry *te;
14127 if (sigint_received || sigpipe_received)
14129 te = got_object_tree_get_entry(tree, i);
14130 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
14133 fprintf(outfile, "%s %.7o %s\n", id_str,
14134 got_tree_entry_get_mode(te),
14135 got_tree_entry_get_name(te));
14139 got_object_tree_close(tree);
14143 static const struct got_error *
14144 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14146 const struct got_error *err;
14147 struct got_commit_object *commit;
14148 const struct got_object_id_queue *parent_ids;
14149 struct got_object_qid *pid;
14150 char *id_str = NULL;
14151 const char *logmsg = NULL;
14154 err = got_object_open_as_commit(&commit, repo, id);
14158 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
14162 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
14163 parent_ids = got_object_commit_get_parent_ids(commit);
14164 fprintf(outfile, "numparents %d\n",
14165 got_object_commit_get_nparents(commit));
14166 STAILQ_FOREACH(pid, parent_ids, entry) {
14168 err = got_object_id_str(&pid_str, &pid->id);
14171 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
14174 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14175 got_object_commit_get_author_gmtoff(commit));
14176 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14177 got_object_commit_get_author(commit),
14178 (long long)got_object_commit_get_author_time(commit),
14181 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14182 got_object_commit_get_committer_gmtoff(commit));
14183 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14184 got_object_commit_get_committer(commit),
14185 (long long)got_object_commit_get_committer_time(commit),
14188 logmsg = got_object_commit_get_logmsg_raw(commit);
14189 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14190 fprintf(outfile, "%s", logmsg);
14193 got_object_commit_close(commit);
14197 static const struct got_error *
14198 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14200 const struct got_error *err;
14201 struct got_tag_object *tag;
14202 char *id_str = NULL;
14203 const char *tagmsg = NULL;
14206 err = got_object_open_as_tag(&tag, repo, id);
14210 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14214 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14216 switch (got_object_tag_get_object_type(tag)) {
14217 case GOT_OBJ_TYPE_BLOB:
14218 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14219 GOT_OBJ_LABEL_BLOB);
14221 case GOT_OBJ_TYPE_TREE:
14222 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14223 GOT_OBJ_LABEL_TREE);
14225 case GOT_OBJ_TYPE_COMMIT:
14226 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14227 GOT_OBJ_LABEL_COMMIT);
14229 case GOT_OBJ_TYPE_TAG:
14230 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14231 GOT_OBJ_LABEL_TAG);
14237 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14238 got_object_tag_get_name(tag));
14240 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14241 got_object_tag_get_tagger_gmtoff(tag));
14242 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14243 got_object_tag_get_tagger(tag),
14244 (long long)got_object_tag_get_tagger_time(tag),
14247 tagmsg = got_object_tag_get_message(tag);
14248 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14249 fprintf(outfile, "%s", tagmsg);
14252 got_object_tag_close(tag);
14256 static const struct got_error *
14257 cmd_cat(int argc, char *argv[])
14259 const struct got_error *error;
14260 struct got_repository *repo = NULL;
14261 struct got_worktree *worktree = NULL;
14262 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14263 char *keyword_idstr = NULL;
14264 const char *commit_id_str = NULL;
14265 struct got_object_id *id = NULL, *commit_id = NULL;
14266 struct got_commit_object *commit = NULL;
14267 int ch, obj_type, i, force_path = 0;
14268 struct got_reflist_head refs;
14269 int *pack_fds = NULL;
14274 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14279 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14282 commit_id_str = optarg;
14288 repo_path = realpath(optarg, NULL);
14289 if (repo_path == NULL)
14290 return got_error_from_errno2("realpath",
14292 got_path_strip_trailing_slashes(repo_path);
14303 cwd = getcwd(NULL, 0);
14305 error = got_error_from_errno("getcwd");
14309 error = got_repo_pack_fds_open(&pack_fds);
14313 if (repo_path == NULL) {
14314 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14315 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14318 repo_path = strdup(
14319 got_worktree_get_repo_path(worktree));
14320 if (repo_path == NULL) {
14321 error = got_error_from_errno("strdup");
14325 if (commit_id_str == NULL) {
14326 /* Release work tree lock. */
14327 got_worktree_close(worktree);
14333 if (repo_path == NULL) {
14334 repo_path = strdup(cwd);
14335 if (repo_path == NULL)
14336 return got_error_from_errno("strdup");
14339 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14344 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14348 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14352 if (commit_id_str != NULL) {
14353 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14357 if (keyword_idstr != NULL)
14358 commit_id_str = keyword_idstr;
14359 if (worktree != NULL) {
14360 got_worktree_close(worktree);
14364 commit_id_str = GOT_REF_HEAD;
14365 error = got_repo_match_object_id(&commit_id, NULL,
14366 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14370 error = got_object_open_as_commit(&commit, repo, commit_id);
14374 for (i = 0; i < argc; i++) {
14376 error = got_object_id_by_path(&id, repo, commit,
14381 error = got_repo_match_object_id(&id, &label, argv[i],
14382 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14385 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14386 error->code != GOT_ERR_NOT_REF)
14388 error = got_object_id_by_path(&id, repo,
14395 error = got_object_get_type(&obj_type, repo, id);
14399 switch (obj_type) {
14400 case GOT_OBJ_TYPE_BLOB:
14401 error = cat_blob(id, repo, stdout);
14403 case GOT_OBJ_TYPE_TREE:
14404 error = cat_tree(id, repo, stdout);
14406 case GOT_OBJ_TYPE_COMMIT:
14407 error = cat_commit(id, repo, stdout);
14409 case GOT_OBJ_TYPE_TAG:
14410 error = cat_tag(id, repo, stdout);
14413 error = got_error(GOT_ERR_OBJ_TYPE);
14427 free(keyword_idstr);
14429 got_object_commit_close(commit);
14431 got_worktree_close(worktree);
14433 const struct got_error *close_err = got_repo_close(repo);
14438 const struct got_error *pack_err =
14439 got_repo_pack_fds_close(pack_fds);
14444 got_ref_list_free(&refs);
14451 fprintf(stderr, "usage: %s info [path ...]\n",
14456 static const struct got_error *
14457 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14458 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14459 struct got_object_id *commit_id)
14461 const struct got_error *err = NULL;
14462 char *id_str = NULL;
14464 struct tm mytm, *tm;
14465 struct got_pathlist_head *paths = arg;
14466 struct got_pathlist_entry *pe;
14469 * Clear error indication from any of the path arguments which
14470 * would cause this file index entry to be displayed.
14472 TAILQ_FOREACH(pe, paths, entry) {
14473 if (got_path_cmp(path, pe->path, strlen(path),
14474 pe->path_len) == 0 ||
14475 got_path_is_child(path, pe->path, pe->path_len))
14476 pe->data = NULL; /* no error */
14479 printf(GOT_COMMIT_SEP_STR);
14481 printf("symlink: %s\n", path);
14482 else if (S_ISREG(mode)) {
14483 printf("file: %s\n", path);
14484 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14485 } else if (S_ISDIR(mode))
14486 printf("directory: %s\n", path);
14488 printf("something: %s\n", path);
14490 tm = localtime_r(&mtime, &mytm);
14493 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14494 return got_error(GOT_ERR_NO_SPACE);
14495 printf("timestamp: %s\n", datebuf);
14498 err = got_object_id_str(&id_str, blob_id);
14501 printf("based on blob: %s\n", id_str);
14505 if (staged_blob_id) {
14506 err = got_object_id_str(&id_str, staged_blob_id);
14509 printf("based on staged blob: %s\n", id_str);
14514 err = got_object_id_str(&id_str, commit_id);
14517 printf("based on commit: %s\n", id_str);
14524 static const struct got_error *
14525 cmd_info(int argc, char *argv[])
14527 const struct got_error *error = NULL;
14528 struct got_worktree *worktree = NULL;
14529 char *cwd = NULL, *id_str = NULL;
14530 struct got_pathlist_head paths;
14531 char *uuidstr = NULL;
14532 int ch, show_files = 0;
14534 TAILQ_INIT(&paths);
14537 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14542 while ((ch = getopt(argc, argv, "")) != -1) {
14553 cwd = getcwd(NULL, 0);
14555 error = got_error_from_errno("getcwd");
14559 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14561 if (error->code == GOT_ERR_NOT_WORKTREE)
14562 error = wrap_not_worktree_error(error, "info", cwd);
14567 /* Remove "wpath cpath proc exec sendfd" promises. */
14568 if (pledge("stdio rpath flock unveil", NULL) == -1)
14571 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14576 error = get_worktree_paths_from_argv(&paths, argc, argv,
14583 error = got_object_id_str(&id_str,
14584 got_worktree_get_base_commit_id(worktree));
14588 error = got_worktree_get_uuid(&uuidstr, worktree);
14592 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14593 printf("work tree base commit: %s\n", id_str);
14594 printf("work tree path prefix: %s\n",
14595 got_worktree_get_path_prefix(worktree));
14596 printf("work tree branch reference: %s\n",
14597 got_worktree_get_head_ref_name(worktree));
14598 printf("work tree UUID: %s\n", uuidstr);
14599 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14602 struct got_pathlist_entry *pe;
14603 TAILQ_FOREACH(pe, &paths, entry) {
14604 if (pe->path_len == 0)
14607 * Assume this path will fail. This will be corrected
14608 * in print_path_info() in case the path does suceeed.
14610 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14612 error = got_worktree_path_info(worktree, &paths,
14613 print_path_info, &paths, check_cancelled, NULL);
14616 TAILQ_FOREACH(pe, &paths, entry) {
14617 if (pe->data != NULL) {
14618 const struct got_error *perr;
14621 error = got_error_fmt(perr->code, "%s",
14629 got_worktree_close(worktree);
14630 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);