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 cmd_status(int argc, char *argv[])
6428 const struct got_error *close_err, *error = NULL;
6429 struct got_repository *repo = NULL;
6430 struct got_worktree *worktree = NULL;
6431 struct got_status_arg st;
6433 struct got_pathlist_head paths;
6434 int ch, i, no_ignores = 0;
6435 int *pack_fds = NULL;
6439 memset(&st, 0, sizeof(st));
6440 st.status_codes = NULL;
6444 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6449 while ((ch = getopt(argc, argv, "IS:s:")) != -1) {
6455 if (st.status_codes != NULL && st.suppress == 0)
6456 option_conflict('S', 's');
6460 for (i = 0; optarg[i] != '\0'; i++) {
6461 switch (optarg[i]) {
6462 case GOT_STATUS_MODIFY:
6463 case GOT_STATUS_ADD:
6464 case GOT_STATUS_DELETE:
6465 case GOT_STATUS_CONFLICT:
6466 case GOT_STATUS_MISSING:
6467 case GOT_STATUS_OBSTRUCTED:
6468 case GOT_STATUS_UNVERSIONED:
6469 case GOT_STATUS_MODE_CHANGE:
6470 case GOT_STATUS_NONEXISTENT:
6473 errx(1, "invalid status code '%c'",
6477 if (ch == 's' && st.suppress)
6478 option_conflict('s', 'S');
6479 st.status_codes = optarg;
6490 cwd = getcwd(NULL, 0);
6492 error = got_error_from_errno("getcwd");
6496 error = got_repo_pack_fds_open(&pack_fds);
6500 error = got_worktree_open(&worktree, cwd,
6501 GOT_WORKTREE_GOT_DIR);
6503 if (error->code == GOT_ERR_NOT_WORKTREE)
6504 error = wrap_not_worktree_error(error, "status", cwd);
6508 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6513 error = apply_unveil(got_repo_get_path(repo), 1,
6514 got_worktree_get_root_path(worktree));
6518 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6522 error = got_worktree_status(worktree, &paths, repo, no_ignores,
6523 print_status, &st, check_cancelled, NULL);
6526 const struct got_error *pack_err =
6527 got_repo_pack_fds_close(pack_fds);
6532 close_err = got_repo_close(repo);
6536 if (worktree != NULL) {
6537 close_err = got_worktree_close(worktree);
6542 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
6550 fprintf(stderr, "usage: %s ref [-dlt] [-c object] [-r repository-path] "
6551 "[-s reference] [name]\n", getprogname());
6555 static const struct got_error *
6556 list_refs(struct got_repository *repo, const char *refname, int sort_by_time)
6558 static const struct got_error *err = NULL;
6559 struct got_reflist_head refs;
6560 struct got_reflist_entry *re;
6563 err = got_ref_list(&refs, repo, refname, sort_by_time ?
6564 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6569 TAILQ_FOREACH(re, &refs, entry) {
6571 refstr = got_ref_to_str(re->ref);
6572 if (refstr == NULL) {
6573 err = got_error_from_errno("got_ref_to_str");
6576 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
6580 got_ref_list_free(&refs);
6584 static const struct got_error *
6585 delete_ref_by_name(struct got_repository *repo, const char *refname)
6587 const struct got_error *err;
6588 struct got_reference *ref;
6590 err = got_ref_open(&ref, repo, refname, 0);
6594 err = delete_ref(repo, ref);
6599 static const struct got_error *
6600 add_ref(struct got_repository *repo, const char *refname, const char *target)
6602 const struct got_error *err = NULL;
6603 struct got_object_id *id = NULL;
6604 struct got_reference *ref = NULL;
6605 struct got_reflist_head refs;
6608 * Don't let the user create a reference name with a leading '-'.
6609 * While technically a valid reference name, this case is usually
6610 * an unintended typo.
6612 if (refname[0] == '-')
6613 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6616 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
6619 err = got_repo_match_object_id(&id, NULL, target, GOT_OBJ_TYPE_ANY,
6621 got_ref_list_free(&refs);
6625 err = got_ref_alloc(&ref, refname, id);
6629 err = got_ref_write(ref, repo);
6637 static const struct got_error *
6638 add_symref(struct got_repository *repo, const char *refname, const char *target)
6640 const struct got_error *err = NULL;
6641 struct got_reference *ref = NULL;
6642 struct got_reference *target_ref = NULL;
6645 * Don't let the user create a reference name with a leading '-'.
6646 * While technically a valid reference name, this case is usually
6647 * an unintended typo.
6649 if (refname[0] == '-')
6650 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
6652 err = got_ref_open(&target_ref, repo, target, 0);
6656 err = got_ref_alloc_symref(&ref, refname, target_ref);
6660 err = got_ref_write(ref, repo);
6663 got_ref_close(target_ref);
6669 static const struct got_error *
6670 cmd_ref(int argc, char *argv[])
6672 const struct got_error *error = NULL;
6673 struct got_repository *repo = NULL;
6674 struct got_worktree *worktree = NULL;
6675 char *cwd = NULL, *repo_path = NULL;
6676 int ch, do_list = 0, do_delete = 0, sort_by_time = 0;
6677 const char *obj_arg = NULL, *symref_target= NULL;
6678 char *refname = NULL, *keyword_idstr = NULL;
6679 int *pack_fds = NULL;
6682 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
6683 "sendfd unveil", NULL) == -1)
6687 while ((ch = getopt(argc, argv, "c:dlr:s:t")) != -1) {
6699 repo_path = realpath(optarg, NULL);
6700 if (repo_path == NULL)
6701 return got_error_from_errno2("realpath",
6703 got_path_strip_trailing_slashes(repo_path);
6706 symref_target = optarg;
6717 if (obj_arg && do_list)
6718 option_conflict('c', 'l');
6719 if (obj_arg && do_delete)
6720 option_conflict('c', 'd');
6721 if (obj_arg && symref_target)
6722 option_conflict('c', 's');
6723 if (symref_target && do_delete)
6724 option_conflict('s', 'd');
6725 if (symref_target && do_list)
6726 option_conflict('s', 'l');
6727 if (do_delete && do_list)
6728 option_conflict('d', 'l');
6729 if (sort_by_time && !do_list)
6730 errx(1, "-t option requires -l option");
6736 if (argc != 0 && argc != 1)
6739 refname = strdup(argv[0]);
6740 if (refname == NULL) {
6741 error = got_error_from_errno("strdup");
6748 refname = strdup(argv[0]);
6749 if (refname == NULL) {
6750 error = got_error_from_errno("strdup");
6756 got_path_strip_trailing_slashes(refname);
6758 cwd = getcwd(NULL, 0);
6760 error = got_error_from_errno("getcwd");
6764 error = got_repo_pack_fds_open(&pack_fds);
6768 if (repo_path == NULL) {
6769 error = got_worktree_open(&worktree, cwd,
6770 GOT_WORKTREE_GOT_DIR);
6771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
6777 strdup(got_worktree_get_repo_path(worktree));
6778 if (repo_path == NULL)
6779 error = got_error_from_errno("strdup");
6783 repo_path = strdup(cwd);
6784 if (repo_path == NULL) {
6785 error = got_error_from_errno("strdup");
6791 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
6797 /* Remove "cpath" promise. */
6798 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
6804 error = apply_unveil(got_repo_get_path(repo), do_list,
6805 worktree ? got_worktree_get_root_path(worktree) : NULL);
6810 error = list_refs(repo, refname, sort_by_time);
6812 error = delete_ref_by_name(repo, refname);
6813 else if (symref_target)
6814 error = add_symref(repo, refname, symref_target);
6816 if (obj_arg == NULL)
6819 error = got_keyword_to_idstr(&keyword_idstr, obj_arg,
6823 if (keyword_idstr != NULL)
6824 obj_arg = keyword_idstr;
6826 error = add_ref(repo, refname, obj_arg);
6831 const struct got_error *close_err = got_repo_close(repo);
6836 got_worktree_close(worktree);
6838 const struct got_error *pack_err =
6839 got_repo_pack_fds_close(pack_fds);
6845 free(keyword_idstr);
6852 fprintf(stderr, "usage: %s branch [-lnt] [-c commit] [-d name] "
6853 "[-r repository-path] [name]\n", getprogname());
6857 static const struct got_error *
6858 list_branch(struct got_repository *repo, struct got_worktree *worktree,
6859 struct got_reference *ref)
6861 const struct got_error *err = NULL;
6862 const char *refname;
6866 refname = got_ref_get_name(ref);
6867 if (worktree && strcmp(refname,
6868 got_worktree_get_head_ref_name(worktree)) == 0) {
6869 err = got_worktree_get_state(&marker, repo, worktree,
6870 check_cancelled, NULL);
6875 if (strncmp(refname, "refs/heads/", 11) == 0)
6877 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6879 if (strncmp(refname, "refs/remotes/", 13) == 0)
6882 refstr = got_ref_to_str(ref);
6884 return got_error_from_errno("got_ref_to_str");
6886 printf("%c %s: %s\n", marker, refname, refstr);
6891 static const struct got_error *
6892 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
6894 const char *refname;
6896 if (worktree == NULL)
6897 return got_error(GOT_ERR_NOT_WORKTREE);
6899 refname = got_worktree_get_head_ref_name(worktree);
6901 if (strncmp(refname, "refs/heads/", 11) == 0)
6903 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
6906 printf("%s\n", refname);
6911 static const struct got_error *
6912 list_branches(struct got_repository *repo, struct got_worktree *worktree,
6915 static const struct got_error *err = NULL;
6916 struct got_reflist_head refs;
6917 struct got_reflist_entry *re;
6918 struct got_reference *temp_ref = NULL;
6919 int rebase_in_progress, histedit_in_progress;
6924 err = got_worktree_rebase_in_progress(&rebase_in_progress,
6929 err = got_worktree_histedit_in_progress(&histedit_in_progress,
6934 if (rebase_in_progress || histedit_in_progress) {
6935 err = got_ref_open(&temp_ref, repo,
6936 got_worktree_get_head_ref_name(worktree), 0);
6939 list_branch(repo, worktree, temp_ref);
6940 got_ref_close(temp_ref);
6944 err = got_ref_list(&refs, repo, "refs/heads", sort_by_time ?
6945 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6950 TAILQ_FOREACH(re, &refs, entry)
6951 list_branch(repo, worktree, re->ref);
6953 got_ref_list_free(&refs);
6955 err = got_ref_list(&refs, repo, "refs/remotes", sort_by_time ?
6956 got_ref_cmp_by_commit_timestamp_descending : got_ref_cmp_by_name,
6961 TAILQ_FOREACH(re, &refs, entry)
6962 list_branch(repo, worktree, re->ref);
6964 got_ref_list_free(&refs);
6969 static const struct got_error *
6970 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
6971 const char *branch_name)
6973 const struct got_error *err = NULL;
6974 struct got_reference *ref = NULL;
6975 char *refname, *remote_refname = NULL;
6977 if (strncmp(branch_name, "refs/", 5) == 0)
6979 if (strncmp(branch_name, "heads/", 6) == 0)
6981 else if (strncmp(branch_name, "remotes/", 8) == 0)
6984 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
6985 return got_error_from_errno("asprintf");
6987 if (asprintf(&remote_refname, "refs/remotes/%s",
6988 branch_name) == -1) {
6989 err = got_error_from_errno("asprintf");
6993 err = got_ref_open(&ref, repo, refname, 0);
6995 const struct got_error *err2;
6996 if (err->code != GOT_ERR_NOT_REF)
6999 * Keep 'err' intact such that if neither branch exists
7000 * we report "refs/heads" rather than "refs/remotes" in
7001 * our error message.
7003 err2 = got_ref_open(&ref, repo, remote_refname, 0);
7010 strcmp(got_worktree_get_head_ref_name(worktree),
7011 got_ref_get_name(ref)) == 0) {
7012 err = got_error_msg(GOT_ERR_SAME_BRANCH,
7013 "will not delete this work tree's current branch");
7017 err = delete_ref(repo, ref);
7022 free(remote_refname);
7026 static const struct got_error *
7027 add_branch(struct got_repository *repo, const char *branch_name,
7028 struct got_object_id *base_commit_id)
7030 const struct got_error *err = NULL;
7031 struct got_reference *ref = NULL;
7032 char *refname = NULL;
7035 * Don't let the user create a branch name with a leading '-'.
7036 * While technically a valid reference name, this case is usually
7037 * an unintended typo.
7039 if (branch_name[0] == '-')
7040 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
7042 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7045 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
7046 err = got_error_from_errno("asprintf");
7050 err = got_ref_open(&ref, repo, refname, 0);
7052 err = got_error(GOT_ERR_BRANCH_EXISTS);
7054 } else if (err->code != GOT_ERR_NOT_REF)
7057 err = got_ref_alloc(&ref, refname, base_commit_id);
7061 err = got_ref_write(ref, repo);
7069 static const struct got_error *
7070 cmd_branch(int argc, char *argv[])
7072 const struct got_error *error = NULL;
7073 struct got_repository *repo = NULL;
7074 struct got_worktree *worktree = NULL;
7075 char *cwd = NULL, *repo_path = NULL;
7076 int ch, do_list = 0, do_show = 0, do_update = 1, sort_by_time = 0;
7077 const char *delref = NULL, *commit_id_arg = NULL;
7078 struct got_reference *ref = NULL;
7079 struct got_pathlist_head paths;
7080 struct got_object_id *commit_id = NULL;
7081 char *commit_id_str = NULL, *keyword_idstr = NULL;;
7082 int *pack_fds = NULL;
7087 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7088 "sendfd unveil", NULL) == -1)
7092 while ((ch = getopt(argc, argv, "c:d:lnr:t")) != -1) {
7095 commit_id_arg = optarg;
7107 repo_path = realpath(optarg, NULL);
7108 if (repo_path == NULL)
7109 return got_error_from_errno2("realpath",
7111 got_path_strip_trailing_slashes(repo_path);
7122 if (do_list && delref)
7123 option_conflict('l', 'd');
7124 if (sort_by_time && !do_list)
7125 errx(1, "-t option requires -l option");
7130 if (!do_list && !delref && argc == 0)
7133 if ((do_list || delref || do_show) && commit_id_arg != NULL)
7134 errx(1, "-c option can only be used when creating a branch");
7136 if (do_list || delref) {
7139 } else if (!do_show && argc != 1)
7142 cwd = getcwd(NULL, 0);
7144 error = got_error_from_errno("getcwd");
7148 error = got_repo_pack_fds_open(&pack_fds);
7152 if (repo_path == NULL) {
7153 error = got_worktree_open(&worktree, cwd,
7154 GOT_WORKTREE_GOT_DIR);
7155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7161 strdup(got_worktree_get_repo_path(worktree));
7162 if (repo_path == NULL)
7163 error = got_error_from_errno("strdup");
7167 repo_path = strdup(cwd);
7168 if (repo_path == NULL) {
7169 error = got_error_from_errno("strdup");
7175 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7180 if (do_list || do_show) {
7181 /* Remove "cpath" promise. */
7182 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
7188 error = apply_unveil(got_repo_get_path(repo), do_list,
7189 worktree ? got_worktree_get_root_path(worktree) : NULL);
7194 error = show_current_branch(repo, worktree);
7196 error = list_branches(repo, worktree, sort_by_time);
7198 error = delete_branch(repo, worktree, delref);
7200 struct got_reflist_head refs;
7202 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name,
7206 if (commit_id_arg == NULL)
7207 commit_id_arg = worktree ?
7208 got_worktree_get_head_ref_name(worktree) :
7211 error = got_keyword_to_idstr(&keyword_idstr,
7212 commit_id_arg, repo, worktree);
7215 if (keyword_idstr != NULL)
7216 commit_id_arg = keyword_idstr;
7218 error = got_repo_match_object_id(&commit_id, NULL,
7219 commit_id_arg, GOT_OBJ_TYPE_COMMIT, &refs, repo);
7220 got_ref_list_free(&refs);
7223 error = add_branch(repo, argv[0], commit_id);
7226 if (worktree && do_update) {
7227 struct got_update_progress_arg upa;
7228 char *branch_refname = NULL;
7230 error = got_object_id_str(&commit_id_str, commit_id);
7233 error = get_worktree_paths_from_argv(&paths, 0, NULL,
7237 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
7239 error = got_error_from_errno("asprintf");
7242 error = got_ref_open(&ref, repo, branch_refname, 0);
7243 free(branch_refname);
7246 error = switch_head_ref(ref, commit_id, worktree,
7250 error = got_worktree_set_base_commit_id(worktree, repo,
7254 memset(&upa, 0, sizeof(upa));
7255 error = got_worktree_checkout_files(worktree, &paths,
7256 repo, update_progress, &upa, check_cancelled,
7260 if (upa.did_something) {
7261 printf("Updated to %s: %s\n",
7262 got_worktree_get_head_ref_name(worktree),
7265 print_update_progress_stats(&upa);
7269 free(keyword_idstr);
7273 const struct got_error *close_err = got_repo_close(repo);
7278 got_worktree_close(worktree);
7280 const struct got_error *pack_err =
7281 got_repo_pack_fds_close(pack_fds);
7288 free(commit_id_str);
7289 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
7297 fprintf(stderr, "usage: %s tag [-lVv] [-c commit] [-m message] "
7298 "[-r repository-path] [-s signer-id] name\n", getprogname());
7303 static const struct got_error *
7304 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
7306 const struct got_error *err = NULL;
7307 struct got_reflist_entry *re, *se, *new;
7308 struct got_object_id *re_id, *se_id;
7309 struct got_tag_object *re_tag, *se_tag;
7310 time_t re_time, se_time;
7312 STAILQ_FOREACH(re, tags, entry) {
7313 se = STAILQ_FIRST(sorted);
7315 err = got_reflist_entry_dup(&new, re);
7318 STAILQ_INSERT_HEAD(sorted, new, entry);
7321 err = got_ref_resolve(&re_id, repo, re->ref);
7324 err = got_object_open_as_tag(&re_tag, repo, re_id);
7328 re_time = got_object_tag_get_tagger_time(re_tag);
7329 got_object_tag_close(re_tag);
7333 err = got_ref_resolve(&se_id, repo, re->ref);
7336 err = got_object_open_as_tag(&se_tag, repo, se_id);
7340 se_time = got_object_tag_get_tagger_time(se_tag);
7341 got_object_tag_close(se_tag);
7343 if (se_time > re_time) {
7344 err = got_reflist_entry_dup(&new, re);
7347 STAILQ_INSERT_AFTER(sorted, se, new, entry);
7350 se = STAILQ_NEXT(se, entry);
7359 static const struct got_error *
7360 get_tag_refname(char **refname, const char *tag_name)
7362 const struct got_error *err;
7364 if (strncmp("refs/tags/", tag_name, 10) == 0) {
7365 *refname = strdup(tag_name);
7366 if (*refname == NULL)
7367 return got_error_from_errno("strdup");
7368 } else if (asprintf(refname, "refs/tags/%s", tag_name) == -1) {
7369 err = got_error_from_errno("asprintf");
7377 static const struct got_error *
7378 list_tags(struct got_repository *repo, const char *tag_name, int verify_tags,
7379 const char *allowed_signers, const char *revoked_signers, int verbosity)
7381 static const struct got_error *err = NULL;
7382 struct got_reflist_head refs;
7383 struct got_reflist_entry *re;
7384 char *wanted_refname = NULL;
7389 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
7394 struct got_reference *ref;
7395 err = get_tag_refname(&wanted_refname, tag_name);
7398 /* Wanted tag reference should exist. */
7399 err = got_ref_open(&ref, repo, wanted_refname, 0);
7405 TAILQ_FOREACH(re, &refs, entry) {
7406 const char *refname;
7407 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
7409 const char *tagger, *ssh_sig = NULL;
7410 char *sig_msg = NULL;
7412 struct got_object_id *id;
7413 struct got_tag_object *tag;
7414 struct got_commit_object *commit = NULL;
7416 refname = got_ref_get_name(re->ref);
7417 if (strncmp(refname, "refs/tags/", 10) != 0 ||
7418 (wanted_refname && strcmp(refname, wanted_refname) != 0))
7421 refstr = got_ref_to_str(re->ref);
7422 if (refstr == NULL) {
7423 err = got_error_from_errno("got_ref_to_str");
7427 err = got_ref_resolve(&id, repo, re->ref);
7430 err = got_object_open_as_tag(&tag, repo, id);
7432 if (err->code != GOT_ERR_OBJ_TYPE) {
7436 /* "lightweight" tag */
7437 err = got_object_open_as_commit(&commit, repo, id);
7442 tagger = got_object_commit_get_committer(commit);
7444 got_object_commit_get_committer_time(commit);
7445 err = got_object_id_str(&id_str, id);
7451 tagger = got_object_tag_get_tagger(tag);
7452 tagger_time = got_object_tag_get_tagger_time(tag);
7453 err = got_object_id_str(&id_str,
7454 got_object_tag_get_object_id(tag));
7459 if (tag && verify_tags) {
7460 ssh_sig = got_sigs_get_tagmsg_ssh_signature(
7461 got_object_tag_get_message(tag));
7462 if (ssh_sig && allowed_signers == NULL) {
7463 err = got_error_msg(
7464 GOT_ERR_VERIFY_TAG_SIGNATURE,
7465 "SSH signature verification requires "
7466 "setting allowed_signers in "
7472 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
7474 printf("from: %s\n", tagger);
7475 datestr = get_datestr(&tagger_time, datebuf);
7477 printf("date: %s UTC\n", datestr);
7479 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
7481 switch (got_object_tag_get_object_type(tag)) {
7482 case GOT_OBJ_TYPE_BLOB:
7483 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
7486 case GOT_OBJ_TYPE_TREE:
7487 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
7490 case GOT_OBJ_TYPE_COMMIT:
7491 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
7494 case GOT_OBJ_TYPE_TAG:
7495 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
7505 err = got_sigs_verify_tag_ssh(&sig_msg, tag, ssh_sig,
7506 allowed_signers, revoked_signers, verbosity);
7507 if (err && err->code == GOT_ERR_BAD_TAG_SIGNATURE)
7511 printf("signature: %s", sig_msg);
7517 err = got_object_commit_get_logmsg(&tagmsg0, commit);
7520 got_object_commit_close(commit);
7522 tagmsg0 = strdup(got_object_tag_get_message(tag));
7523 got_object_tag_close(tag);
7524 if (tagmsg0 == NULL) {
7525 err = got_error_from_errno("strdup");
7532 line = strsep(&tagmsg, "\n");
7534 printf(" %s\n", line);
7539 got_ref_list_free(&refs);
7540 free(wanted_refname);
7542 if (err == NULL && bad_sigs)
7543 err = got_error(GOT_ERR_BAD_TAG_SIGNATURE);
7547 static const struct got_error *
7548 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
7549 const char *tag_name, const char *editor, const char *repo_path)
7551 const struct got_error *err = NULL;
7552 char *template = NULL, *initial_content = NULL;
7553 int initial_content_len;
7556 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
7557 err = got_error_from_errno("asprintf");
7561 initial_content_len = asprintf(&initial_content,
7562 "\n# tagging commit %s as %s\n",
7563 commit_id_str, tag_name);
7564 if (initial_content_len == -1) {
7565 err = got_error_from_errno("asprintf");
7569 err = got_opentemp_named_fd(tagmsg_path, &fd, template, "");
7573 if (write(fd, initial_content, initial_content_len) == -1) {
7574 err = got_error_from_errno2("write", *tagmsg_path);
7577 if (close(fd) == -1) {
7578 err = got_error_from_errno2("close", *tagmsg_path);
7583 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content,
7584 initial_content_len, 1);
7586 free(initial_content);
7589 if (fd != -1 && close(fd) == -1 && err == NULL)
7590 err = got_error_from_errno2("close", *tagmsg_path);
7599 static const struct got_error *
7600 add_tag(struct got_repository *repo, const char *tagger,
7601 const char *tag_name, const char *commit_arg, const char *tagmsg_arg,
7602 const char *signer_id, const char *editor, int verbosity)
7604 const struct got_error *err = NULL;
7605 struct got_object_id *commit_id = NULL, *tag_id = NULL;
7606 char *label = NULL, *commit_id_str = NULL;
7607 struct got_reference *ref = NULL;
7608 char *refname = NULL, *tagmsg = NULL;
7609 char *tagmsg_path = NULL, *tag_id_str = NULL;
7610 int preserve_tagmsg = 0;
7611 struct got_reflist_head refs;
7616 * Don't let the user create a tag name with a leading '-'.
7617 * While technically a valid reference name, this case is usually
7618 * an unintended typo.
7620 if (tag_name[0] == '-')
7621 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
7623 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
7627 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
7628 GOT_OBJ_TYPE_COMMIT, &refs, repo);
7632 err = got_object_id_str(&commit_id_str, commit_id);
7636 err = get_tag_refname(&refname, tag_name);
7639 if (strncmp("refs/tags/", tag_name, 10) == 0)
7642 err = got_ref_open(&ref, repo, refname, 0);
7644 err = got_error(GOT_ERR_TAG_EXISTS);
7646 } else if (err->code != GOT_ERR_NOT_REF)
7649 if (tagmsg_arg == NULL) {
7650 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
7651 tag_name, editor, got_repo_get_path(repo));
7653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
7654 tagmsg_path != NULL)
7655 preserve_tagmsg = 1;
7660 err = got_object_tag_create(&tag_id, tag_name, commit_id,
7661 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, signer_id, repo,
7665 preserve_tagmsg = 1;
7669 err = got_ref_alloc(&ref, refname, tag_id);
7672 preserve_tagmsg = 1;
7676 err = got_ref_write(ref, repo);
7679 preserve_tagmsg = 1;
7683 err = got_object_id_str(&tag_id_str, tag_id);
7686 preserve_tagmsg = 1;
7689 printf("Created tag %s\n", tag_id_str);
7691 if (preserve_tagmsg) {
7692 fprintf(stderr, "%s: tag message preserved in %s\n",
7693 getprogname(), tagmsg_path);
7694 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
7695 err = got_error_from_errno2("unlink", tagmsg_path);
7700 free(commit_id_str);
7704 got_ref_list_free(&refs);
7708 static const struct got_error *
7709 cmd_tag(int argc, char *argv[])
7711 const struct got_error *error = NULL;
7712 struct got_repository *repo = NULL;
7713 struct got_worktree *worktree = NULL;
7714 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
7715 char *gitconfig_path = NULL, *tagger = NULL, *keyword_idstr = NULL;
7716 char *allowed_signers = NULL, *revoked_signers = NULL, *editor = NULL;
7717 const char *signer_id = NULL;
7718 const char *tag_name = NULL, *commit_id_arg = NULL, *tagmsg = NULL;
7719 int ch, do_list = 0, verify_tags = 0, verbosity = 0;
7720 int *pack_fds = NULL;
7723 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
7724 "sendfd unveil", NULL) == -1)
7728 while ((ch = getopt(argc, argv, "c:lm:r:s:Vv")) != -1) {
7731 commit_id_arg = optarg;
7740 repo_path = realpath(optarg, NULL);
7741 if (repo_path == NULL) {
7742 error = got_error_from_errno2("realpath",
7746 got_path_strip_trailing_slashes(repo_path);
7757 else if (verbosity < 3)
7769 if (do_list || verify_tags) {
7770 if (commit_id_arg != NULL)
7772 "-c option can only be used when creating a tag");
7775 option_conflict('l', 'm');
7777 option_conflict('V', 'm');
7781 option_conflict('l', 's');
7783 option_conflict('V', 's');
7787 } else if (argc != 1)
7793 cwd = getcwd(NULL, 0);
7795 error = got_error_from_errno("getcwd");
7799 error = got_repo_pack_fds_open(&pack_fds);
7803 if (repo_path == NULL) {
7804 error = got_worktree_open(&worktree, cwd,
7805 GOT_WORKTREE_GOT_DIR);
7806 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7812 strdup(got_worktree_get_repo_path(worktree));
7813 if (repo_path == NULL)
7814 error = got_error_from_errno("strdup");
7818 repo_path = strdup(cwd);
7819 if (repo_path == NULL) {
7820 error = got_error_from_errno("strdup");
7826 if (do_list || verify_tags) {
7827 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
7830 error = get_allowed_signers(&allowed_signers, repo, worktree);
7833 error = get_revoked_signers(&revoked_signers, repo, worktree);
7837 /* Release work tree lock. */
7838 got_worktree_close(worktree);
7843 * Remove "cpath" promise unless needed for signature tmpfile
7847 got_sigs_apply_unveil();
7850 if (pledge("stdio rpath wpath flock proc exec sendfd "
7851 "unveil", NULL) == -1)
7855 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7858 error = list_tags(repo, tag_name, verify_tags, allowed_signers,
7859 revoked_signers, verbosity);
7861 error = get_gitconfig_path(&gitconfig_path);
7864 error = got_repo_open(&repo, repo_path, gitconfig_path,
7869 error = get_author(&tagger, repo, worktree);
7872 if (signer_id == NULL)
7873 signer_id = get_signer_id(repo, worktree);
7875 if (tagmsg == NULL) {
7876 error = get_editor(&editor);
7879 if (unveil(editor, "x") != 0) {
7880 error = got_error_from_errno2("unveil", editor);
7885 error = got_sigs_apply_unveil();
7889 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
7893 if (commit_id_arg == NULL) {
7894 struct got_reference *head_ref;
7895 struct got_object_id *commit_id;
7896 error = got_ref_open(&head_ref, repo,
7897 worktree ? got_worktree_get_head_ref_name(worktree)
7901 error = got_ref_resolve(&commit_id, repo, head_ref);
7902 got_ref_close(head_ref);
7905 error = got_object_id_str(&commit_id_str, commit_id);
7910 error = got_keyword_to_idstr(&keyword_idstr,
7911 commit_id_arg, repo, worktree);
7914 commit_id_str = keyword_idstr;
7918 /* Release work tree lock. */
7919 got_worktree_close(worktree);
7923 error = add_tag(repo, tagger, tag_name,
7924 commit_id_str ? commit_id_str : commit_id_arg, tagmsg,
7925 signer_id, editor, verbosity);
7929 const struct got_error *close_err = got_repo_close(repo);
7934 got_worktree_close(worktree);
7936 const struct got_error *pack_err =
7937 got_repo_pack_fds_close(pack_fds);
7944 free(gitconfig_path);
7945 free(commit_id_str);
7947 free(allowed_signers);
7948 free(revoked_signers);
7955 fprintf(stderr, "usage: %s add [-IR] path ...\n", getprogname());
7959 static const struct got_error *
7960 add_progress(void *arg, unsigned char status, const char *path)
7962 while (path[0] == '/')
7964 printf("%c %s\n", status, path);
7968 static const struct got_error *
7969 cmd_add(int argc, char *argv[])
7971 const struct got_error *error = NULL;
7972 struct got_repository *repo = NULL;
7973 struct got_worktree *worktree = NULL;
7975 struct got_pathlist_head paths;
7976 struct got_pathlist_entry *pe;
7977 int ch, can_recurse = 0, no_ignores = 0;
7978 int *pack_fds = NULL;
7983 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7988 while ((ch = getopt(argc, argv, "IR")) != -1) {
8008 cwd = getcwd(NULL, 0);
8010 error = got_error_from_errno("getcwd");
8014 error = got_repo_pack_fds_open(&pack_fds);
8018 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8020 if (error->code == GOT_ERR_NOT_WORKTREE)
8021 error = wrap_not_worktree_error(error, "add", cwd);
8025 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8030 error = apply_unveil(got_repo_get_path(repo), 1,
8031 got_worktree_get_root_path(worktree));
8035 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8042 TAILQ_FOREACH(pe, &paths, entry) {
8043 if (asprintf(&ondisk_path, "%s/%s",
8044 got_worktree_get_root_path(worktree),
8046 error = got_error_from_errno("asprintf");
8049 if (lstat(ondisk_path, &sb) == -1) {
8050 if (errno == ENOENT) {
8054 error = got_error_from_errno2("lstat",
8060 if (S_ISDIR(sb.st_mode)) {
8061 error = got_error_msg(GOT_ERR_BAD_PATH,
8062 "adding directories requires -R option");
8068 error = got_worktree_schedule_add(worktree, &paths, add_progress,
8069 NULL, repo, no_ignores);
8072 const struct got_error *close_err = got_repo_close(repo);
8077 got_worktree_close(worktree);
8079 const struct got_error *pack_err =
8080 got_repo_pack_fds_close(pack_fds);
8084 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8092 fprintf(stderr, "usage: %s remove [-fkR] [-s status-codes] path ...\n",
8097 static const struct got_error *
8098 print_remove_status(void *arg, unsigned char status,
8099 unsigned char staged_status, const char *path)
8101 while (path[0] == '/')
8103 if (status == GOT_STATUS_NONEXISTENT)
8105 if (status == staged_status && (status == GOT_STATUS_DELETE))
8106 status = GOT_STATUS_NO_CHANGE;
8107 printf("%c%c %s\n", status, staged_status, path);
8111 static const struct got_error *
8112 cmd_remove(int argc, char *argv[])
8114 const struct got_error *error = NULL;
8115 struct got_worktree *worktree = NULL;
8116 struct got_repository *repo = NULL;
8117 const char *status_codes = NULL;
8119 struct got_pathlist_head paths;
8120 struct got_pathlist_entry *pe;
8121 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
8122 int ignore_missing_paths = 0;
8123 int *pack_fds = NULL;
8128 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8133 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
8136 delete_local_mods = 1;
8137 ignore_missing_paths = 1;
8146 for (i = 0; optarg[i] != '\0'; i++) {
8147 switch (optarg[i]) {
8148 case GOT_STATUS_MODIFY:
8149 delete_local_mods = 1;
8151 case GOT_STATUS_MISSING:
8152 ignore_missing_paths = 1;
8155 errx(1, "invalid status code '%c'",
8159 status_codes = optarg;
8173 cwd = getcwd(NULL, 0);
8175 error = got_error_from_errno("getcwd");
8179 error = got_repo_pack_fds_open(&pack_fds);
8183 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8185 if (error->code == GOT_ERR_NOT_WORKTREE)
8186 error = wrap_not_worktree_error(error, "remove", cwd);
8190 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8195 error = apply_unveil(got_repo_get_path(repo), 1,
8196 got_worktree_get_root_path(worktree));
8200 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8207 TAILQ_FOREACH(pe, &paths, entry) {
8208 if (asprintf(&ondisk_path, "%s/%s",
8209 got_worktree_get_root_path(worktree),
8211 error = got_error_from_errno("asprintf");
8214 if (lstat(ondisk_path, &sb) == -1) {
8215 if (errno == ENOENT) {
8219 error = got_error_from_errno2("lstat",
8225 if (S_ISDIR(sb.st_mode)) {
8226 error = got_error_msg(GOT_ERR_BAD_PATH,
8227 "removing directories requires -R option");
8233 error = got_worktree_schedule_delete(worktree, &paths,
8234 delete_local_mods, status_codes, print_remove_status, NULL,
8235 repo, keep_on_disk, ignore_missing_paths);
8238 const struct got_error *close_err = got_repo_close(repo);
8243 got_worktree_close(worktree);
8245 const struct got_error *pack_err =
8246 got_repo_pack_fds_close(pack_fds);
8250 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8258 fprintf(stderr, "usage: %s patch [-nR] [-c commit] [-p strip-count] "
8259 "[patchfile]\n", getprogname());
8263 static const struct got_error *
8264 patch_from_stdin(int *patchfd)
8266 const struct got_error *err = NULL;
8269 sig_t sighup, sigint, sigquit;
8271 *patchfd = got_opentempfd();
8273 return got_error_from_errno("got_opentempfd");
8275 sighup = signal(SIGHUP, SIG_DFL);
8276 sigint = signal(SIGINT, SIG_DFL);
8277 sigquit = signal(SIGQUIT, SIG_DFL);
8280 r = read(0, buf, sizeof(buf));
8282 err = got_error_from_errno("read");
8287 if (write(*patchfd, buf, r) == -1) {
8288 err = got_error_from_errno("write");
8293 signal(SIGHUP, sighup);
8294 signal(SIGINT, sigint);
8295 signal(SIGQUIT, sigquit);
8297 if (err == NULL && lseek(*patchfd, 0, SEEK_SET) == -1)
8298 err = got_error_from_errno("lseek");
8308 struct got_patch_progress_arg {
8314 static const struct got_error *
8315 patch_progress(void *arg, const char *old, const char *new,
8316 unsigned char status, const struct got_error *error, int old_from,
8317 int old_lines, int new_from, int new_lines, int offset,
8318 int ws_mangled, const struct got_error *hunk_err)
8320 const char *path = new == NULL ? old : new;
8321 struct got_patch_progress_arg *a = arg;
8323 while (*path == '/')
8326 if (status != GOT_STATUS_NO_CHANGE &&
8327 status != 0 /* per-hunk progress */) {
8328 printf("%c %s\n", status, path);
8329 a->did_something = 1;
8332 if (hunk_err == NULL) {
8333 if (status == GOT_STATUS_CANNOT_UPDATE)
8335 else if (status == GOT_STATUS_CONFLICT)
8340 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
8342 if (offset != 0 || hunk_err != NULL || ws_mangled) {
8343 printf("@@ -%d,%d +%d,%d @@ ", old_from,
8344 old_lines, new_from, new_lines);
8345 if (hunk_err != NULL)
8346 printf("%s\n", hunk_err->msg);
8347 else if (offset != 0)
8348 printf("applied with offset %d\n", offset);
8350 printf("hunk contains mangled whitespace\n");
8357 print_patch_progress_stats(struct got_patch_progress_arg *ppa)
8359 if (!ppa->did_something)
8362 if (ppa->conflicts > 0)
8363 printf("Files with merge conflicts: %d\n", ppa->conflicts);
8365 if (ppa->rejects > 0) {
8366 printf("Files where patch failed to apply: %d\n",
8371 static const struct got_error *
8372 cmd_patch(int argc, char *argv[])
8374 const struct got_error *error = NULL, *close_error = NULL;
8375 struct got_worktree *worktree = NULL;
8376 struct got_repository *repo = NULL;
8377 struct got_reflist_head refs;
8378 struct got_object_id *commit_id = NULL;
8379 const char *commit_id_str = NULL;
8382 char *cwd = NULL, *keyword_idstr = NULL;
8383 int ch, nop = 0, strip = -1, reverse = 0;
8385 int *pack_fds = NULL;
8386 struct got_patch_progress_arg ppa;
8391 if (pledge("stdio rpath wpath cpath fattr proc exec sendfd flock "
8392 "unveil", NULL) == -1)
8396 while ((ch = getopt(argc, argv, "c:np:R")) != -1) {
8399 commit_id_str = optarg;
8405 strip = strtonum(optarg, 0, INT_MAX, &errstr);
8407 errx(1, "pathname strip count is %s: %s",
8423 error = patch_from_stdin(&patchfd);
8426 } else if (argc == 1) {
8427 patchfd = open(argv[0], O_RDONLY);
8428 if (patchfd == -1) {
8429 error = got_error_from_errno2("open", argv[0]);
8432 if (fstat(patchfd, &sb) == -1) {
8433 error = got_error_from_errno2("fstat", argv[0]);
8436 if (!S_ISREG(sb.st_mode)) {
8437 error = got_error_path(argv[0], GOT_ERR_BAD_FILETYPE);
8443 if ((cwd = getcwd(NULL, 0)) == NULL) {
8444 error = got_error_from_errno("getcwd");
8448 error = got_repo_pack_fds_open(&pack_fds);
8452 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8456 const char *repo_path = got_worktree_get_repo_path(worktree);
8457 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
8461 error = apply_unveil(got_repo_get_path(repo), 0,
8462 got_worktree_get_root_path(worktree));
8466 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
8470 if (commit_id_str != NULL) {
8471 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
8476 error = got_repo_match_object_id(&commit_id, NULL,
8477 keyword_idstr != NULL ? keyword_idstr : commit_id_str,
8478 GOT_OBJ_TYPE_COMMIT, &refs, repo);
8483 memset(&ppa, 0, sizeof(ppa));
8484 error = got_patch(patchfd, worktree, repo, nop, strip, reverse,
8485 commit_id, patch_progress, &ppa, check_cancelled, NULL);
8486 print_patch_progress_stats(&ppa);
8488 got_ref_list_free(&refs);
8489 free(keyword_idstr);
8492 close_error = got_repo_close(repo);
8494 error = close_error;
8496 if (worktree != NULL) {
8497 close_error = got_worktree_close(worktree);
8499 error = close_error;
8502 const struct got_error *pack_err =
8503 got_repo_pack_fds_close(pack_fds);
8514 fprintf(stderr, "usage: %s revert [-pR] [-F response-script] path ...\n",
8519 static const struct got_error *
8520 revert_progress(void *arg, unsigned char status, const char *path)
8522 if (status == GOT_STATUS_UNVERSIONED)
8525 while (path[0] == '/')
8527 printf("%c %s\n", status, path);
8531 struct choose_patch_arg {
8532 FILE *patch_script_file;
8536 static const struct got_error *
8537 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
8538 int nchanges, const char *action)
8540 const struct got_error *err;
8542 size_t linesize = 0;
8546 case GOT_STATUS_ADD:
8547 printf("A %s\n%s this addition? [y/n] ", path, action);
8549 case GOT_STATUS_DELETE:
8550 printf("D %s\n%s this deletion? [y/n] ", path, action);
8552 case GOT_STATUS_MODIFY:
8553 if (fseek(patch_file, 0L, SEEK_SET) == -1)
8554 return got_error_from_errno("fseek");
8555 printf(GOT_COMMIT_SEP_STR);
8556 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
8558 if (linelen == -1 && ferror(patch_file)) {
8559 err = got_error_from_errno("getline");
8564 printf(GOT_COMMIT_SEP_STR);
8565 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
8566 path, n, nchanges, action);
8569 return got_error_path(path, GOT_ERR_FILE_STATUS);
8576 static const struct got_error *
8577 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
8578 FILE *patch_file, int n, int nchanges)
8580 const struct got_error *err = NULL;
8582 size_t linesize = 0;
8585 struct choose_patch_arg *a = arg;
8587 *choice = GOT_PATCH_CHOICE_NONE;
8589 if (a->patch_script_file) {
8591 err = show_change(status, path, patch_file, n, nchanges,
8595 linelen = getline(&line, &linesize, a->patch_script_file);
8596 if (linelen == -1) {
8597 if (ferror(a->patch_script_file))
8598 return got_error_from_errno("getline");
8601 nl = strchr(line, '\n');
8604 if (strcmp(line, "y") == 0) {
8605 *choice = GOT_PATCH_CHOICE_YES;
8607 } else if (strcmp(line, "n") == 0) {
8608 *choice = GOT_PATCH_CHOICE_NO;
8610 } else if (strcmp(line, "q") == 0 &&
8611 status == GOT_STATUS_MODIFY) {
8612 *choice = GOT_PATCH_CHOICE_QUIT;
8615 printf("invalid response '%s'\n", line);
8620 while (resp != 'y' && resp != 'n' && resp != 'q') {
8621 err = show_change(status, path, patch_file, n, nchanges,
8628 if (status == GOT_STATUS_MODIFY) {
8629 if (resp != 'y' && resp != 'n' && resp != 'q') {
8630 printf("invalid response '%c'\n", resp);
8633 } else if (resp != 'y' && resp != 'n') {
8634 printf("invalid response '%c'\n", resp);
8640 *choice = GOT_PATCH_CHOICE_YES;
8641 else if (resp == 'n')
8642 *choice = GOT_PATCH_CHOICE_NO;
8643 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
8644 *choice = GOT_PATCH_CHOICE_QUIT;
8649 struct wt_commitable_path_arg {
8650 struct got_pathlist_head *commit_paths;
8655 * Shortcut work tree status callback to determine if the set of paths scanned
8656 * has at least one versioned path that is being modified and, if not NULL, is
8657 * in the arg->commit_paths list. Set arg and return GOT_ERR_FILE_MODIFIED as
8658 * soon as a path is passed with a status that satisfies this criteria.
8660 static const struct got_error *
8661 worktree_has_commitable_path(void *arg, unsigned char status,
8662 unsigned char staged_status, const char *path,
8663 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8664 struct got_object_id *commit_id, int dirfd, const char *de_name)
8666 struct wt_commitable_path_arg *a = arg;
8668 if (status == staged_status && (status == GOT_STATUS_DELETE))
8669 status = GOT_STATUS_NO_CHANGE;
8671 if (!(status == GOT_STATUS_NO_CHANGE ||
8672 status == GOT_STATUS_UNVERSIONED) ||
8673 staged_status != GOT_STATUS_NO_CHANGE) {
8674 if (a->commit_paths != NULL) {
8675 struct got_pathlist_entry *pe;
8677 TAILQ_FOREACH(pe, a->commit_paths, entry) {
8678 if (strncmp(path, pe->path,
8679 pe->path_len) == 0) {
8680 *a->has_changes = 1;
8685 *a->has_changes = 1;
8687 if (*a->has_changes)
8688 return got_error(GOT_ERR_FILE_MODIFIED);
8695 * Check that the changeset of the commit identified by id is
8696 * comprised of at least one modified path that is being committed.
8698 static const struct got_error *
8699 commit_path_changed_in_worktree(struct wt_commitable_path_arg *wcpa,
8700 struct got_object_id *id, struct got_worktree *worktree,
8701 struct got_repository *repo)
8703 const struct got_error *err;
8704 struct got_pathlist_head paths;
8705 struct got_commit_object *commit = NULL, *pcommit = NULL;
8706 struct got_tree_object *tree = NULL, *ptree = NULL;
8707 struct got_object_qid *pid;
8711 err = got_object_open_as_commit(&commit, repo, id);
8715 err = got_object_open_as_tree(&tree, repo,
8716 got_object_commit_get_tree_id(commit));
8720 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
8722 err = got_object_open_as_commit(&pcommit, repo, &pid->id);
8726 err = got_object_open_as_tree(&ptree, repo,
8727 got_object_commit_get_tree_id(pcommit));
8732 err = got_diff_tree(ptree, tree, NULL, NULL, -1, -1, "", "", repo,
8733 got_diff_tree_collect_changed_paths, &paths, 0);
8737 err = got_worktree_status(worktree, &paths, repo, 0,
8738 worktree_has_commitable_path, wcpa, check_cancelled, NULL);
8739 if (err && err->code == GOT_ERR_FILE_MODIFIED) {
8741 * At least one changed path in the referenced commit is
8742 * modified in the work tree, that's all we need to know!
8748 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
8750 got_object_commit_close(commit);
8752 got_object_commit_close(pcommit);
8754 got_object_tree_close(tree);
8756 got_object_tree_close(ptree);
8761 * Remove any "logmsg" reference comprised entirely of paths that have
8762 * been reverted in this work tree. If any path in the logmsg ref changeset
8763 * remains in a changed state in the worktree, do not remove the reference.
8765 static const struct got_error *
8766 rm_logmsg_ref(struct got_worktree *worktree, struct got_repository *repo)
8768 const struct got_error *err;
8769 struct got_reflist_head refs;
8770 struct got_reflist_entry *re;
8771 struct got_commit_object *commit = NULL;
8772 struct got_object_id *commit_id = NULL;
8773 struct wt_commitable_path_arg wcpa;
8774 char *uuidstr = NULL;
8778 err = got_worktree_get_uuid(&uuidstr, worktree);
8782 err = got_ref_list(&refs, repo, "refs/got/worktree",
8783 got_ref_cmp_by_name, repo);
8787 TAILQ_FOREACH(re, &refs, entry) {
8788 const char *refname;
8789 int has_changes = 0;
8791 refname = got_ref_get_name(re->ref);
8793 if (!strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
8794 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN))
8795 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
8796 else if (!strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
8797 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN))
8798 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
8802 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
8803 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
8807 err = got_repo_match_object_id(&commit_id, NULL, refname,
8808 GOT_OBJ_TYPE_COMMIT, NULL, repo);
8812 err = got_object_open_as_commit(&commit, repo, commit_id);
8816 wcpa.commit_paths = NULL;
8817 wcpa.has_changes = &has_changes;
8819 err = commit_path_changed_in_worktree(&wcpa, commit_id,
8825 err = got_ref_delete(re->ref, repo);
8830 got_object_commit_close(commit);
8839 got_ref_list_free(&refs);
8841 got_object_commit_close(commit);
8845 static const struct got_error *
8846 cmd_revert(int argc, char *argv[])
8848 const struct got_error *error = NULL;
8849 struct got_worktree *worktree = NULL;
8850 struct got_repository *repo = NULL;
8851 char *cwd = NULL, *path = NULL;
8852 struct got_pathlist_head paths;
8853 struct got_pathlist_entry *pe;
8854 int ch, can_recurse = 0, pflag = 0;
8855 FILE *patch_script_file = NULL;
8856 const char *patch_script_path = NULL;
8857 struct choose_patch_arg cpa;
8858 int *pack_fds = NULL;
8863 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8864 "unveil", NULL) == -1)
8868 while ((ch = getopt(argc, argv, "F:pR")) != -1) {
8871 patch_script_path = optarg;
8890 if (patch_script_path && !pflag)
8891 errx(1, "-F option can only be used together with -p option");
8893 cwd = getcwd(NULL, 0);
8895 error = got_error_from_errno("getcwd");
8899 error = got_repo_pack_fds_open(&pack_fds);
8903 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
8905 if (error->code == GOT_ERR_NOT_WORKTREE)
8906 error = wrap_not_worktree_error(error, "revert", cwd);
8910 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8915 if (patch_script_path) {
8916 patch_script_file = fopen(patch_script_path, "re");
8917 if (patch_script_file == NULL) {
8918 error = got_error_from_errno2("fopen",
8925 * XXX "c" perm needed on repo dir to delete merge references.
8927 error = apply_unveil(got_repo_get_path(repo), 0,
8928 got_worktree_get_root_path(worktree));
8932 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8939 TAILQ_FOREACH(pe, &paths, entry) {
8940 if (asprintf(&ondisk_path, "%s/%s",
8941 got_worktree_get_root_path(worktree),
8943 error = got_error_from_errno("asprintf");
8946 if (lstat(ondisk_path, &sb) == -1) {
8947 if (errno == ENOENT) {
8951 error = got_error_from_errno2("lstat",
8957 if (S_ISDIR(sb.st_mode)) {
8958 error = got_error_msg(GOT_ERR_BAD_PATH,
8959 "reverting directories requires -R option");
8965 cpa.patch_script_file = patch_script_file;
8966 cpa.action = "revert";
8967 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
8968 pflag ? choose_patch : NULL, &cpa, repo);
8970 error = rm_logmsg_ref(worktree, repo);
8972 if (patch_script_file && fclose(patch_script_file) == EOF &&
8974 error = got_error_from_errno2("fclose", patch_script_path);
8976 const struct got_error *close_err = got_repo_close(repo);
8981 got_worktree_close(worktree);
8983 const struct got_error *pack_err =
8984 got_repo_pack_fds_close(pack_fds);
8988 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
8997 fprintf(stderr, "usage: %s commit [-CNnS] [-A author] [-F path] "
8998 "[-m message] [path ...]\n", getprogname());
9002 struct collect_commit_logmsg_arg {
9003 const char *cmdline_log;
9004 const char *prepared_log;
9005 const char *merged_log;
9006 int non_interactive;
9008 const char *worktree_path;
9009 const char *branch_name;
9010 const char *repo_path;
9015 static const struct got_error *
9016 read_prepared_logmsg(char **logmsg, const char *path)
9018 const struct got_error *err = NULL;
9024 memset(&sb, 0, sizeof(sb));
9026 f = fopen(path, "re");
9028 return got_error_from_errno2("fopen", path);
9030 if (fstat(fileno(f), &sb) == -1) {
9031 err = got_error_from_errno2("fstat", path);
9034 if (sb.st_size == 0) {
9035 err = got_error(GOT_ERR_COMMIT_MSG_EMPTY);
9039 *logmsg = malloc(sb.st_size + 1);
9040 if (*logmsg == NULL) {
9041 err = got_error_from_errno("malloc");
9045 r = fread(*logmsg, 1, sb.st_size, f);
9046 if (r != sb.st_size) {
9048 err = got_error_from_errno2("fread", path);
9050 err = got_error(GOT_ERR_IO);
9053 (*logmsg)[sb.st_size] = '\0';
9055 if (fclose(f) == EOF && err == NULL)
9056 err = got_error_from_errno2("fclose", path);
9064 static const struct got_error *
9065 collect_commit_logmsg(struct got_pathlist_head *commitable_paths,
9066 const char *diff_path, char **logmsg, void *arg)
9068 char *initial_content = NULL;
9069 struct got_pathlist_entry *pe;
9070 const struct got_error *err = NULL;
9071 char *template = NULL;
9072 char *prepared_msg = NULL, *merged_msg = NULL;
9073 struct collect_commit_logmsg_arg *a = arg;
9074 int initial_content_len;
9078 /* if a message was specified on the command line, just use it */
9079 if (a->cmdline_log != NULL && *a->cmdline_log != '\0') {
9080 len = strlen(a->cmdline_log) + 1;
9081 *logmsg = malloc(len + 1);
9082 if (*logmsg == NULL)
9083 return got_error_from_errno("malloc");
9084 strlcpy(*logmsg, a->cmdline_log, len);
9086 } else if (a->prepared_log != NULL && a->non_interactive)
9087 return read_prepared_logmsg(logmsg, a->prepared_log);
9089 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
9090 return got_error_from_errno("asprintf");
9092 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template, "");
9096 if (a->prepared_log) {
9097 err = read_prepared_logmsg(&prepared_msg, a->prepared_log);
9100 } else if (a->merged_log) {
9101 err = read_prepared_logmsg(&merged_msg, a->merged_log);
9106 initial_content_len = asprintf(&initial_content,
9107 "%s%s\n# changes to be committed on branch %s:\n",
9108 prepared_msg ? prepared_msg : "",
9109 merged_msg ? merged_msg : "", a->branch_name);
9110 if (initial_content_len == -1) {
9111 err = got_error_from_errno("asprintf");
9115 if (write(fd, initial_content, initial_content_len) == -1) {
9116 err = got_error_from_errno2("write", a->logmsg_path);
9120 TAILQ_FOREACH(pe, commitable_paths, entry) {
9121 struct got_commitable *ct = pe->data;
9122 dprintf(fd, "# %c %s\n",
9123 got_commitable_get_status(ct),
9124 got_commitable_get_path(ct));
9128 dprintf(fd, "# detailed changes can be viewed in %s\n",
9132 if (close(fd) == -1) {
9133 err = got_error_from_errno2("close", a->logmsg_path);
9138 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content,
9139 initial_content_len, a->prepared_log ? 0 : 1);
9141 free(initial_content);
9146 if (fd != -1 && close(fd) == -1 && err == NULL)
9147 err = got_error_from_errno2("close", a->logmsg_path);
9155 static const struct got_error *
9156 cat_logmsg(FILE *f, struct got_commit_object *commit, const char *idstr,
9157 const char *type, int has_content)
9159 const struct got_error *err = NULL;
9160 char *logmsg = NULL;
9162 err = got_object_commit_get_logmsg(&logmsg, commit);
9166 if (fprintf(f, "%s# log message of %s commit %s:%s",
9167 has_content ? "\n" : "", type, idstr, logmsg) < 0)
9168 err = got_ferror(f, GOT_ERR_IO);
9175 * Lookup "logmsg" references of backed-out and cherrypicked commits
9176 * belonging to the current work tree. If found, and the worktree has
9177 * at least one modified file that was changed in the referenced commit,
9178 * add its log message to a new temporary file at *logmsg_path.
9179 * Add all refs found to matched_refs to be scheduled for removal on
9180 * successful commit.
9182 static const struct got_error *
9183 lookup_logmsg_ref(char **logmsg_path, struct got_pathlist_head *paths,
9184 struct got_reflist_head *matched_refs, struct got_worktree *worktree,
9185 struct got_repository *repo)
9187 const struct got_error *err;
9188 struct got_commit_object *commit = NULL;
9189 struct got_object_id *id = NULL;
9190 struct got_reflist_head refs;
9191 struct got_reflist_entry *re, *re_match;
9193 char *uuidstr = NULL;
9194 int added_logmsg = 0;
9198 *logmsg_path = NULL;
9200 err = got_worktree_get_uuid(&uuidstr, worktree);
9204 err = got_ref_list(&refs, repo, "refs/got/worktree",
9205 got_ref_cmp_by_name, repo);
9209 TAILQ_FOREACH(re, &refs, entry) {
9210 const char *refname, *type;
9211 struct wt_commitable_path_arg wcpa;
9214 refname = got_ref_get_name(re->ref);
9216 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
9217 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
9218 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
9219 type = "cherrypicked";
9220 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
9221 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
9222 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
9223 type = "backed-out";
9227 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) == 0)
9228 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
9232 err = got_repo_match_object_id(&id, NULL, refname,
9233 GOT_OBJ_TYPE_COMMIT, NULL, repo);
9237 err = got_object_open_as_commit(&commit, repo, id);
9241 wcpa.commit_paths = paths;
9242 wcpa.has_changes = &add_logmsg;
9244 err = commit_path_changed_in_worktree(&wcpa, id,
9251 err = got_opentemp_named(logmsg_path, &f,
9252 "got-commit-logmsg", "");
9256 err = cat_logmsg(f, commit, refname, type,
9263 err = got_reflist_entry_dup(&re_match, re);
9266 TAILQ_INSERT_HEAD(matched_refs, re_match, entry);
9269 got_object_commit_close(commit);
9278 got_ref_list_free(&refs);
9280 got_object_commit_close(commit);
9281 if (f && fclose(f) == EOF && err == NULL)
9282 err = got_error_from_errno("fclose");
9283 if (!added_logmsg) {
9284 if (*logmsg_path && unlink(*logmsg_path) != 0 && err == NULL)
9285 err = got_error_from_errno2("unlink", *logmsg_path);
9286 *logmsg_path = NULL;
9291 static const struct got_error *
9292 cmd_commit(int argc, char *argv[])
9294 const struct got_error *error = NULL;
9295 struct got_worktree *worktree = NULL;
9296 struct got_repository *repo = NULL;
9297 char *cwd = NULL, *id_str = NULL;
9298 struct got_object_id *id = NULL;
9299 const char *logmsg = NULL;
9300 char *prepared_logmsg = NULL, *merged_logmsg = NULL;
9301 struct collect_commit_logmsg_arg cl_arg;
9302 const char *author = NULL;
9303 char *gitconfig_path = NULL, *editor = NULL, *committer = NULL;
9304 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
9305 int allow_bad_symlinks = 0, non_interactive = 0, merge_in_progress = 0;
9306 int show_diff = 1, commit_conflicts = 0;
9307 struct got_pathlist_head paths;
9308 struct got_reflist_head refs;
9309 struct got_reflist_entry *re;
9310 int *pack_fds = NULL;
9314 cl_arg.logmsg_path = NULL;
9317 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9318 "unveil", NULL) == -1)
9322 while ((ch = getopt(argc, argv, "A:CF:m:NnS")) != -1) {
9326 error = valid_author(author);
9331 commit_conflicts = 1;
9335 option_conflict('F', 'm');
9336 prepared_logmsg = realpath(optarg, NULL);
9337 if (prepared_logmsg == NULL)
9338 return got_error_from_errno2("realpath",
9342 if (prepared_logmsg)
9343 option_conflict('m', 'F');
9347 non_interactive = 1;
9353 allow_bad_symlinks = 1;
9364 cwd = getcwd(NULL, 0);
9366 error = got_error_from_errno("getcwd");
9370 error = got_repo_pack_fds_open(&pack_fds);
9374 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9376 if (error->code == GOT_ERR_NOT_WORKTREE)
9377 error = wrap_not_worktree_error(error, "commit", cwd);
9381 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
9384 if (rebase_in_progress) {
9385 error = got_error(GOT_ERR_REBASING);
9389 error = got_worktree_histedit_in_progress(&histedit_in_progress,
9394 error = get_gitconfig_path(&gitconfig_path);
9397 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9398 gitconfig_path, pack_fds);
9402 error = got_worktree_merge_in_progress(&merge_in_progress, worktree, repo);
9405 if (merge_in_progress) {
9406 error = got_error(GOT_ERR_MERGE_BUSY);
9410 error = get_author(&committer, repo, worktree);
9417 if (logmsg == NULL || strlen(logmsg) == 0) {
9418 error = get_editor(&editor);
9421 if (unveil(editor, "x") != 0) {
9422 error = got_error_from_errno2("unveil", editor);
9427 error = apply_unveil(got_repo_get_path(repo), 0,
9428 got_worktree_get_root_path(worktree));
9432 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9436 if (prepared_logmsg == NULL) {
9437 error = lookup_logmsg_ref(&merged_logmsg,
9438 argc > 0 ? &paths : NULL, &refs, worktree, repo);
9443 cl_arg.editor = editor;
9444 cl_arg.cmdline_log = logmsg;
9445 cl_arg.prepared_log = prepared_logmsg;
9446 cl_arg.merged_log = merged_logmsg;
9447 cl_arg.non_interactive = non_interactive;
9448 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
9449 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
9450 if (!histedit_in_progress) {
9451 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
9452 error = got_error(GOT_ERR_COMMIT_BRANCH);
9455 cl_arg.branch_name += 11;
9457 cl_arg.repo_path = got_repo_get_path(repo);
9458 error = got_worktree_commit(&id, worktree, &paths, author, committer,
9459 allow_bad_symlinks, show_diff, commit_conflicts,
9460 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
9462 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
9463 cl_arg.logmsg_path != NULL)
9464 preserve_logmsg = 1;
9468 error = got_object_id_str(&id_str, id);
9471 printf("Created commit %s\n", id_str);
9473 TAILQ_FOREACH(re, &refs, entry) {
9474 error = got_ref_delete(re->ref, repo);
9480 if (preserve_logmsg) {
9481 fprintf(stderr, "%s: log message preserved in %s\n",
9482 getprogname(), cl_arg.logmsg_path);
9483 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
9485 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
9486 free(cl_arg.logmsg_path);
9487 if (merged_logmsg && unlink(merged_logmsg) == -1 && error == NULL)
9488 error = got_error_from_errno2("unlink", merged_logmsg);
9489 free(merged_logmsg);
9491 const struct got_error *close_err = got_repo_close(repo);
9496 got_worktree_close(worktree);
9498 const struct got_error *pack_err =
9499 got_repo_pack_fds_close(pack_fds);
9503 got_ref_list_free(&refs);
9504 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
9507 free(gitconfig_path);
9510 free(prepared_logmsg);
9517 fprintf(stderr, "usage: %s send [-afqTv] [-b branch] [-d branch] "
9518 "[-r repository-path] [-t tag] [remote-repository]\n",
9524 print_load_info(int print_colored, int print_found, int print_trees,
9525 int ncolored, int nfound, int ntrees)
9527 if (print_colored) {
9528 printf("%d commit%s colored", ncolored,
9529 ncolored == 1 ? "" : "s");
9532 printf("%s%d object%s found",
9533 ncolored > 0 ? "; " : "",
9534 nfound, nfound == 1 ? "" : "s");
9537 printf("; %d tree%s scanned", ntrees,
9538 ntrees == 1 ? "" : "s");
9542 struct got_send_progress_arg {
9543 char last_scaled_packsize[FMT_SCALED_STRSIZE];
9550 int last_nobj_total;
9554 int printed_something;
9556 struct got_pathlist_head *delete_branches;
9559 static const struct got_error *
9560 send_progress(void *arg, int ncolored, int nfound, int ntrees,
9561 off_t packfile_size, int ncommits, int nobj_total, int nobj_deltify,
9562 int nobj_written, off_t bytes_sent, const char *refname,
9563 const char *errmsg, int success)
9565 struct got_send_progress_arg *a = arg;
9566 char scaled_packsize[FMT_SCALED_STRSIZE];
9567 char scaled_sent[FMT_SCALED_STRSIZE];
9568 int p_deltify = 0, p_written = 0, p_sent = 0;
9569 int print_colored = 0, print_found = 0, print_trees = 0;
9570 int print_searching = 0, print_total = 0;
9571 int print_deltify = 0, print_written = 0, print_sent = 0;
9573 if (a->verbosity < 0)
9577 const char *status = success ? "accepted" : "rejected";
9580 struct got_pathlist_entry *pe;
9581 TAILQ_FOREACH(pe, a->delete_branches, entry) {
9582 const char *branchname = pe->path;
9583 if (got_path_cmp(branchname, refname,
9584 strlen(branchname), strlen(refname)) == 0) {
9586 a->sent_something = 1;
9592 if (a->printed_something)
9594 printf("Server has %s %s", status, refname);
9596 printf(": %s", errmsg);
9597 a->printed_something = 1;
9601 if (a->last_ncolored != ncolored) {
9603 a->last_ncolored = ncolored;
9606 if (a->last_nfound != nfound) {
9609 a->last_nfound = nfound;
9612 if (a->last_ntrees != ntrees) {
9616 a->last_ntrees = ntrees;
9619 if ((print_colored || print_found || print_trees) &&
9622 print_load_info(print_colored, print_found, print_trees,
9623 ncolored, nfound, ntrees);
9624 a->printed_something = 1;
9627 } else if (!a->loading_done) {
9629 print_load_info(1, 1, 1, ncolored, nfound, ntrees);
9631 a->loading_done = 1;
9634 if (fmt_scaled(packfile_size, scaled_packsize) == -1)
9635 return got_error_from_errno("fmt_scaled");
9636 if (fmt_scaled(bytes_sent, scaled_sent) == -1)
9637 return got_error_from_errno("fmt_scaled");
9639 if (a->last_ncommits != ncommits) {
9640 print_searching = 1;
9641 a->last_ncommits = ncommits;
9644 if (a->last_nobj_total != nobj_total) {
9645 print_searching = 1;
9647 a->last_nobj_total = nobj_total;
9650 if (packfile_size > 0 && (a->last_scaled_packsize[0] == '\0' ||
9651 strcmp(scaled_packsize, a->last_scaled_packsize)) != 0) {
9652 if (strlcpy(a->last_scaled_packsize, scaled_packsize,
9653 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
9654 return got_error(GOT_ERR_NO_SPACE);
9657 if (nobj_deltify > 0 || nobj_written > 0) {
9658 if (nobj_deltify > 0) {
9659 p_deltify = (nobj_deltify * 100) / nobj_total;
9660 if (p_deltify != a->last_p_deltify) {
9661 a->last_p_deltify = p_deltify;
9662 print_searching = 1;
9667 if (nobj_written > 0) {
9668 p_written = (nobj_written * 100) / nobj_total;
9669 if (p_written != a->last_p_written) {
9670 a->last_p_written = p_written;
9671 print_searching = 1;
9679 if (bytes_sent > 0) {
9680 p_sent = (bytes_sent * 100) / packfile_size;
9681 if (p_sent != a->last_p_sent) {
9682 a->last_p_sent = p_sent;
9683 print_searching = 1;
9689 a->sent_something = 1;
9692 if (print_searching || print_total || print_deltify || print_written ||
9695 if (print_searching)
9696 printf("packing %d reference%s", ncommits,
9697 ncommits == 1 ? "" : "s");
9699 printf("; %d object%s", nobj_total,
9700 nobj_total == 1 ? "" : "s");
9702 printf("; deltify: %d%%", p_deltify);
9704 printf("; uploading pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9705 scaled_packsize, p_sent);
9706 else if (print_written)
9707 printf("; writing pack: %*s %d%%", FMT_SCALED_STRSIZE - 2,
9708 scaled_packsize, p_written);
9709 if (print_searching || print_total || print_deltify ||
9710 print_written || print_sent) {
9711 a->printed_something = 1;
9717 static const struct got_error *
9718 cmd_send(int argc, char *argv[])
9720 const struct got_error *error = NULL;
9721 char *cwd = NULL, *repo_path = NULL;
9722 const char *remote_name;
9723 char *proto = NULL, *host = NULL, *port = NULL;
9724 char *repo_name = NULL, *server_path = NULL;
9725 const struct got_remote_repo *remotes;
9726 struct got_remote_repo *remote = NULL;
9727 int nremotes, nbranches = 0, ndelete_branches = 0;
9728 struct got_repository *repo = NULL;
9729 struct got_worktree *worktree = NULL;
9730 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
9731 struct got_pathlist_head branches;
9732 struct got_pathlist_head tags;
9733 struct got_reflist_head all_branches;
9734 struct got_reflist_head all_tags;
9735 struct got_pathlist_head delete_args;
9736 struct got_pathlist_head delete_branches;
9737 struct got_reflist_entry *re;
9738 struct got_pathlist_entry *pe;
9739 int i, ch, sendfd = -1, sendstatus;
9741 struct got_send_progress_arg spa;
9742 int verbosity = 0, overwrite_refs = 0;
9743 int send_all_branches = 0, send_all_tags = 0;
9744 struct got_reference *ref = NULL;
9745 int *pack_fds = NULL;
9747 TAILQ_INIT(&branches);
9749 TAILQ_INIT(&all_branches);
9750 TAILQ_INIT(&all_tags);
9751 TAILQ_INIT(&delete_args);
9752 TAILQ_INIT(&delete_branches);
9754 while ((ch = getopt(argc, argv, "ab:d:fqr:Tt:v")) != -1) {
9757 send_all_branches = 1;
9760 error = got_pathlist_append(&branches, optarg, NULL);
9766 error = got_pathlist_append(&delete_args, optarg, NULL);
9777 repo_path = realpath(optarg, NULL);
9778 if (repo_path == NULL)
9779 return got_error_from_errno2("realpath",
9781 got_path_strip_trailing_slashes(repo_path);
9787 error = got_pathlist_append(&tags, optarg, NULL);
9794 else if (verbosity < 3)
9805 if (send_all_branches && !TAILQ_EMPTY(&branches))
9806 option_conflict('a', 'b');
9807 if (send_all_tags && !TAILQ_EMPTY(&tags))
9808 option_conflict('T', 't');
9812 remote_name = GOT_SEND_DEFAULT_REMOTE_NAME;
9814 remote_name = argv[0];
9818 cwd = getcwd(NULL, 0);
9820 error = got_error_from_errno("getcwd");
9824 error = got_repo_pack_fds_open(&pack_fds);
9828 if (repo_path == NULL) {
9829 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
9830 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9836 strdup(got_worktree_get_repo_path(worktree));
9837 if (repo_path == NULL)
9838 error = got_error_from_errno("strdup");
9842 repo_path = strdup(cwd);
9843 if (repo_path == NULL) {
9844 error = got_error_from_errno("strdup");
9850 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
9855 worktree_conf = got_worktree_get_gotconfig(worktree);
9856 if (worktree_conf) {
9857 got_gotconfig_get_remotes(&nremotes, &remotes,
9859 for (i = 0; i < nremotes; i++) {
9860 if (strcmp(remotes[i].name, remote_name) == 0) {
9861 error = got_repo_remote_repo_dup(&remote,
9870 if (remote == NULL) {
9871 repo_conf = got_repo_get_gotconfig(repo);
9873 got_gotconfig_get_remotes(&nremotes, &remotes,
9875 for (i = 0; i < nremotes; i++) {
9876 if (strcmp(remotes[i].name, remote_name) == 0) {
9877 error = got_repo_remote_repo_dup(&remote,
9886 if (remote == NULL) {
9887 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
9888 for (i = 0; i < nremotes; i++) {
9889 if (strcmp(remotes[i].name, remote_name) == 0) {
9890 error = got_repo_remote_repo_dup(&remote,
9898 if (remote == NULL) {
9899 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
9903 error = got_dial_parse_uri(&proto, &host, &port, &server_path,
9904 &repo_name, remote->send_url);
9908 if (strcmp(proto, "git") == 0) {
9910 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9911 "sendfd dns inet unveil", NULL) == -1)
9914 } else if (strcmp(proto, "git+ssh") == 0 ||
9915 strcmp(proto, "ssh") == 0) {
9917 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
9918 "sendfd unveil", NULL) == -1)
9921 } else if (strcmp(proto, "http") == 0 ||
9922 strcmp(proto, "git+http") == 0) {
9923 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
9926 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
9930 error = got_dial_apply_unveil(proto);
9934 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
9938 if (send_all_branches) {
9939 error = got_ref_list(&all_branches, repo, "refs/heads",
9940 got_ref_cmp_by_name, NULL);
9943 TAILQ_FOREACH(re, &all_branches, entry) {
9944 const char *branchname = got_ref_get_name(re->ref);
9945 error = got_pathlist_append(&branches,
9951 } else if (nbranches == 0) {
9952 for (i = 0; i < remote->nsend_branches; i++) {
9953 error = got_pathlist_append(&branches,
9954 remote->send_branches[i], NULL);
9960 if (send_all_tags) {
9961 error = got_ref_list(&all_tags, repo, "refs/tags",
9962 got_ref_cmp_by_name, NULL);
9965 TAILQ_FOREACH(re, &all_tags, entry) {
9966 const char *tagname = got_ref_get_name(re->ref);
9967 error = got_pathlist_append(&tags,
9975 * To prevent accidents only branches in refs/heads/ can be deleted
9976 * with 'got send -d'.
9977 * Deleting anything else requires local repository access or Git.
9979 TAILQ_FOREACH(pe, &delete_args, entry) {
9980 const char *branchname = pe->path;
9982 struct got_pathlist_entry *new;
9983 if (strncmp(branchname, "refs/heads/", 11) == 0) {
9984 s = strdup(branchname);
9986 error = got_error_from_errno("strdup");
9990 if (asprintf(&s, "refs/heads/%s", branchname) == -1) {
9991 error = got_error_from_errno("asprintf");
9995 error = got_pathlist_insert(&new, &delete_branches, s, NULL);
9996 if (error || new == NULL /* duplicate */)
10000 ndelete_branches++;
10003 if (nbranches == 0 && ndelete_branches == 0) {
10004 struct got_reference *head_ref;
10006 error = got_ref_open(&head_ref, repo,
10007 got_worktree_get_head_ref_name(worktree), 0);
10009 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
10012 if (got_ref_is_symbolic(head_ref)) {
10013 error = got_ref_resolve_symbolic(&ref, repo, head_ref);
10014 got_ref_close(head_ref);
10019 error = got_pathlist_append(&branches, got_ref_get_name(ref),
10027 /* Release work tree lock. */
10028 got_worktree_close(worktree);
10032 if (verbosity >= 0) {
10033 printf("Connecting to \"%s\" %s://%s%s%s%s%s\n",
10034 remote->name, proto, host,
10035 port ? ":" : "", port ? port : "",
10036 *server_path == '/' ? "" : "/", server_path);
10039 error = got_send_connect(&sendpid, &sendfd, proto, host, port,
10040 server_path, verbosity);
10044 memset(&spa, 0, sizeof(spa));
10045 spa.last_scaled_packsize[0] = '\0';
10046 spa.last_p_deltify = -1;
10047 spa.last_p_written = -1;
10048 spa.verbosity = verbosity;
10049 spa.delete_branches = &delete_branches;
10050 error = got_send_pack(remote_name, &branches, &tags, &delete_branches,
10051 verbosity, overwrite_refs, sendfd, repo, send_progress, &spa,
10052 check_cancelled, NULL);
10053 if (spa.printed_something)
10057 if (!spa.sent_something && verbosity >= 0)
10058 printf("Already up-to-date\n");
10061 if (kill(sendpid, SIGTERM) == -1)
10062 error = got_error_from_errno("kill");
10063 if (waitpid(sendpid, &sendstatus, 0) == -1 && error == NULL)
10064 error = got_error_from_errno("waitpid");
10066 if (sendfd != -1 && close(sendfd) == -1 && error == NULL)
10067 error = got_error_from_errno("close");
10069 const struct got_error *close_err = got_repo_close(repo);
10074 got_worktree_close(worktree);
10076 const struct got_error *pack_err =
10077 got_repo_pack_fds_close(pack_fds);
10082 got_ref_close(ref);
10083 got_repo_free_remote_repo_data(remote);
10085 got_pathlist_free(&branches, GOT_PATHLIST_FREE_NONE);
10086 got_pathlist_free(&tags, GOT_PATHLIST_FREE_NONE);
10087 got_ref_list_free(&all_branches);
10088 got_ref_list_free(&all_tags);
10089 got_pathlist_free(&delete_args, GOT_PATHLIST_FREE_NONE);
10090 got_pathlist_free(&delete_branches, GOT_PATHLIST_FREE_PATH);
10102 * Print and if delete is set delete all ref_prefix references.
10103 * If wanted_ref is not NULL, only print or delete this reference.
10105 static const struct got_error *
10106 process_logmsg_refs(const char *ref_prefix, size_t prefix_len,
10107 const char *wanted_ref, int delete, struct got_worktree *worktree,
10108 struct got_repository *repo)
10110 const struct got_error *err;
10111 struct got_pathlist_head paths;
10112 struct got_reflist_head refs;
10113 struct got_reflist_entry *re;
10114 struct got_reflist_object_id_map *refs_idmap = NULL;
10115 struct got_commit_object *commit = NULL;
10116 struct got_object_id *id = NULL;
10117 const char *header_prefix;
10118 char *uuidstr = NULL;
10122 TAILQ_INIT(&paths);
10124 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, repo);
10128 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
10132 if (worktree != NULL) {
10133 err = got_worktree_get_uuid(&uuidstr, worktree);
10139 if (strncmp(wanted_ref, "refs/heads/", 11) == 0)
10143 if (strcmp(ref_prefix, GOT_WORKTREE_BACKOUT_REF_PREFIX) == 0)
10144 header_prefix = "backout";
10146 header_prefix = "cherrypick";
10148 TAILQ_FOREACH(re, &refs, entry) {
10149 const char *refname, *wt;
10151 refname = got_ref_get_name(re->ref);
10153 err = check_cancelled(NULL);
10157 if (strncmp(refname, ref_prefix, prefix_len) == 0)
10158 refname += prefix_len + 1; /* skip '-' delimiter */
10164 if (worktree == NULL || strncmp(refname, uuidstr,
10165 GOT_WORKTREE_UUID_STRLEN) == 0)
10166 refname += GOT_WORKTREE_UUID_STRLEN + 1; /* skip '-' */
10170 err = got_repo_match_object_id(&id, NULL, refname,
10171 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10175 err = got_object_open_as_commit(&commit, repo, id);
10180 found = strncmp(wanted_ref, refname,
10181 strlen(wanted_ref)) == 0;
10182 if (wanted_ref && !found) {
10183 struct got_reflist_head *ci_refs;
10185 ci_refs = got_reflist_object_id_map_lookup(refs_idmap,
10189 char *refs_str = NULL;
10190 char const *r = NULL;
10192 err = build_refs_str(&refs_str, ci_refs, id,
10199 if (strncmp(r, wanted_ref,
10200 strlen(wanted_ref)) == 0) {
10204 r = strchr(r, ' ');
10212 if (wanted_ref == NULL || found) {
10214 err = got_ref_delete(re->ref, repo);
10217 printf("Deleted: ");
10218 err = print_commit_oneline(commit, id, repo,
10222 * Print paths modified by commit to help
10223 * associate commits with worktree changes.
10225 err = get_changed_paths(&paths, commit,
10230 err = print_commit(commit, id, repo, NULL,
10231 &paths, NULL, 0, 0, refs_idmap, NULL,
10233 got_pathlist_free(&paths,
10234 GOT_PATHLIST_FREE_ALL);
10236 if (worktree == NULL)
10237 printf("work tree: %.*s\n\n",
10238 GOT_WORKTREE_UUID_STRLEN, wt);
10244 got_object_commit_close(commit);
10250 if (wanted_ref != NULL && !found)
10251 err = got_error_fmt(GOT_ERR_NOT_REF, "%s", wanted_ref);
10256 got_ref_list_free(&refs);
10257 got_pathlist_free(&paths, GOT_PATHLIST_FREE_ALL);
10259 got_reflist_object_id_map_free(refs_idmap);
10261 got_object_commit_close(commit);
10266 * Create new temp "logmsg" ref of the backed-out or cherrypicked commit
10267 * identified by id for log messages to prepopulate the editor on commit.
10269 static const struct got_error *
10270 logmsg_ref(struct got_object_id *id, const char *prefix,
10271 struct got_worktree *worktree, struct got_repository *repo)
10273 const struct got_error *err = NULL;
10274 char *idstr, *ref = NULL, *refname = NULL;
10275 int histedit_in_progress;
10276 int rebase_in_progress, merge_in_progress;
10279 * Silenty refuse to create merge reference if any histedit, merge,
10280 * or rebase operation is in progress.
10282 err = got_worktree_histedit_in_progress(&histedit_in_progress,
10286 if (histedit_in_progress)
10289 err = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
10292 if (rebase_in_progress)
10295 err = got_worktree_merge_in_progress(&merge_in_progress, worktree,
10299 if (merge_in_progress)
10302 err = got_object_id_str(&idstr, id);
10306 err = got_worktree_get_logmsg_ref_name(&refname, worktree, prefix);
10310 if (asprintf(&ref, "%s-%s", refname, idstr) == -1) {
10311 err = got_error_from_errno("asprintf");
10315 err = create_ref(ref, got_worktree_get_base_commit_id(worktree),
10325 usage_cherrypick(void)
10327 fprintf(stderr, "usage: %s cherrypick [-lX] [commit-id]\n",
10332 static const struct got_error *
10333 cmd_cherrypick(int argc, char *argv[])
10335 const struct got_error *error = NULL;
10336 struct got_worktree *worktree = NULL;
10337 struct got_repository *repo = NULL;
10338 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10339 struct got_object_id *commit_id = NULL;
10340 struct got_commit_object *commit = NULL;
10341 struct got_object_qid *pid;
10342 int ch, list_refs = 0, remove_refs = 0;
10343 struct got_update_progress_arg upa;
10344 int *pack_fds = NULL;
10347 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10348 "unveil", NULL) == -1)
10352 while ((ch = getopt(argc, argv, "lX")) != -1) {
10361 usage_cherrypick();
10369 if (list_refs || remove_refs) {
10370 if (argc != 0 && argc != 1)
10371 usage_cherrypick();
10372 } else if (argc != 1)
10373 usage_cherrypick();
10374 if (list_refs && remove_refs)
10375 option_conflict('l', 'X');
10377 cwd = getcwd(NULL, 0);
10379 error = got_error_from_errno("getcwd");
10383 error = got_repo_pack_fds_open(&pack_fds);
10387 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10389 if (list_refs || remove_refs) {
10390 if (error->code != GOT_ERR_NOT_WORKTREE)
10393 if (error->code == GOT_ERR_NOT_WORKTREE)
10394 error = wrap_not_worktree_error(error,
10395 "cherrypick", cwd);
10400 error = got_repo_open(&repo,
10401 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10406 error = apply_unveil(got_repo_get_path(repo), 0,
10407 worktree ? got_worktree_get_root_path(worktree) : NULL);
10411 if (list_refs || remove_refs) {
10412 error = process_logmsg_refs(GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
10413 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN,
10414 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10418 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10422 error = got_repo_match_object_id(&commit_id, NULL,
10423 keyword_idstr != NULL ? keyword_idstr : argv[0],
10424 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10427 error = got_object_id_str(&commit_id_str, commit_id);
10431 error = got_object_open_as_commit(&commit, repo, commit_id);
10434 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10435 memset(&upa, 0, sizeof(upa));
10436 error = got_worktree_merge_files(worktree, pid ? &pid->id : NULL,
10437 commit_id, repo, update_progress, &upa, check_cancelled,
10442 if (upa.did_something) {
10443 error = logmsg_ref(commit_id,
10444 GOT_WORKTREE_CHERRYPICK_REF_PREFIX, worktree, repo);
10447 printf("Merged commit %s\n", commit_id_str);
10449 print_merge_progress_stats(&upa);
10452 free(keyword_idstr);
10454 got_object_commit_close(commit);
10455 free(commit_id_str);
10457 got_worktree_close(worktree);
10459 const struct got_error *close_err = got_repo_close(repo);
10464 const struct got_error *pack_err =
10465 got_repo_pack_fds_close(pack_fds);
10474 usage_backout(void)
10476 fprintf(stderr, "usage: %s backout [-lX] [commit-id]\n", getprogname());
10480 static const struct got_error *
10481 cmd_backout(int argc, char *argv[])
10483 const struct got_error *error = NULL;
10484 struct got_worktree *worktree = NULL;
10485 struct got_repository *repo = NULL;
10486 char *cwd = NULL, *commit_id_str = NULL, *keyword_idstr = NULL;
10487 struct got_object_id *commit_id = NULL;
10488 struct got_commit_object *commit = NULL;
10489 struct got_object_qid *pid;
10490 int ch, list_refs = 0, remove_refs = 0;
10491 struct got_update_progress_arg upa;
10492 int *pack_fds = NULL;
10495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
10496 "unveil", NULL) == -1)
10500 while ((ch = getopt(argc, argv, "lX")) != -1) {
10517 if (list_refs || remove_refs) {
10518 if (argc != 0 && argc != 1)
10520 } else if (argc != 1)
10522 if (list_refs && remove_refs)
10523 option_conflict('l', 'X');
10525 cwd = getcwd(NULL, 0);
10527 error = got_error_from_errno("getcwd");
10531 error = got_repo_pack_fds_open(&pack_fds);
10535 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
10537 if (list_refs || remove_refs) {
10538 if (error->code != GOT_ERR_NOT_WORKTREE)
10541 if (error->code == GOT_ERR_NOT_WORKTREE)
10542 error = wrap_not_worktree_error(error,
10548 error = got_repo_open(&repo,
10549 worktree ? got_worktree_get_repo_path(worktree) : cwd,
10554 error = apply_unveil(got_repo_get_path(repo), 0,
10555 worktree ? got_worktree_get_root_path(worktree) : NULL);
10559 if (list_refs || remove_refs) {
10560 error = process_logmsg_refs(GOT_WORKTREE_BACKOUT_REF_PREFIX,
10561 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN,
10562 argc == 1 ? argv[0] : NULL, remove_refs, worktree, repo);
10566 error = got_keyword_to_idstr(&keyword_idstr, argv[0], repo, worktree);
10570 error = got_repo_match_object_id(&commit_id, NULL,
10571 keyword_idstr != NULL ? keyword_idstr : argv[0],
10572 GOT_OBJ_TYPE_COMMIT, NULL, repo);
10575 error = got_object_id_str(&commit_id_str, commit_id);
10579 error = got_object_open_as_commit(&commit, repo, commit_id);
10582 pid = STAILQ_FIRST(got_object_commit_get_parent_ids(commit));
10584 error = got_error(GOT_ERR_ROOT_COMMIT);
10588 memset(&upa, 0, sizeof(upa));
10589 error = got_worktree_merge_files(worktree, commit_id, &pid->id,
10590 repo, update_progress, &upa, check_cancelled, NULL);
10594 if (upa.did_something) {
10595 error = logmsg_ref(commit_id, GOT_WORKTREE_BACKOUT_REF_PREFIX,
10599 printf("Backed out commit %s\n", commit_id_str);
10601 print_merge_progress_stats(&upa);
10604 free(keyword_idstr);
10606 got_object_commit_close(commit);
10607 free(commit_id_str);
10609 got_worktree_close(worktree);
10611 const struct got_error *close_err = got_repo_close(repo);
10616 const struct got_error *pack_err =
10617 got_repo_pack_fds_close(pack_fds);
10627 fprintf(stderr, "usage: %s rebase [-aCclX] [branch]\n", getprogname());
10632 trim_logmsg(char *logmsg, int limit)
10637 len = strlen(logmsg);
10640 logmsg[len] = '\0';
10641 nl = strchr(logmsg, '\n');
10646 static const struct got_error *
10647 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
10649 const struct got_error *err;
10650 char *logmsg0 = NULL;
10653 err = got_object_commit_get_logmsg(&logmsg0, commit);
10658 while (isspace((unsigned char)s[0]))
10661 *logmsg = strdup(s);
10662 if (*logmsg == NULL) {
10663 err = got_error_from_errno("strdup");
10667 trim_logmsg(*logmsg, limit);
10673 static const struct got_error *
10674 show_rebase_merge_conflict(struct got_object_id *id,
10675 struct got_repository *repo)
10677 const struct got_error *err;
10678 struct got_commit_object *commit = NULL;
10679 char *id_str = NULL, *logmsg = NULL;
10681 err = got_object_open_as_commit(&commit, repo, id);
10685 err = got_object_id_str(&id_str, id);
10691 err = get_short_logmsg(&logmsg, 42, commit);
10695 printf("%s -> merge conflict: %s\n", id_str, logmsg);
10698 got_object_commit_close(commit);
10703 static const struct got_error *
10704 show_rebase_progress(struct got_commit_object *commit,
10705 struct got_object_id *old_id, struct got_object_id *new_id)
10707 const struct got_error *err;
10708 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
10710 err = got_object_id_str(&old_id_str, old_id);
10715 err = got_object_id_str(&new_id_str, new_id);
10720 old_id_str[12] = '\0';
10722 new_id_str[12] = '\0';
10724 err = get_short_logmsg(&logmsg, 42, commit);
10728 printf("%s -> %s: %s\n", old_id_str,
10729 new_id_str ? new_id_str : "no-op change", logmsg);
10737 static const struct got_error *
10738 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
10739 struct got_reference *branch, struct got_reference *tmp_branch,
10740 struct got_repository *repo, int create_backup)
10742 printf("Switching work tree to %s\n", got_ref_get_name(branch));
10743 return got_worktree_rebase_complete(worktree, fileindex,
10744 tmp_branch, branch, repo, create_backup);
10747 static const struct got_error *
10748 rebase_commit(struct got_pathlist_head *merged_paths,
10749 struct got_worktree *worktree, struct got_fileindex *fileindex,
10750 struct got_reference *tmp_branch, const char *committer,
10751 struct got_object_id *commit_id, int allow_conflict,
10752 struct got_repository *repo)
10754 const struct got_error *error;
10755 struct got_commit_object *commit;
10756 struct got_object_id *new_commit_id;
10758 error = got_object_open_as_commit(&commit, repo, commit_id);
10762 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
10763 worktree, fileindex, tmp_branch, committer, commit, commit_id,
10764 allow_conflict, repo);
10766 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
10768 error = show_rebase_progress(commit, commit_id, NULL);
10770 error = show_rebase_progress(commit, commit_id, new_commit_id);
10771 free(new_commit_id);
10774 got_object_commit_close(commit);
10778 struct check_path_prefix_arg {
10779 const char *path_prefix;
10784 static const struct got_error *
10785 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
10786 struct got_blob_object *blob2, FILE *f1, FILE *f2,
10787 struct got_object_id *id1, struct got_object_id *id2,
10788 const char *path1, const char *path2,
10789 mode_t mode1, mode_t mode2, struct got_repository *repo)
10791 struct check_path_prefix_arg *a = arg;
10793 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
10794 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
10795 return got_error(a->errcode);
10800 static const struct got_error *
10801 check_path_prefix(struct got_object_id *parent_id,
10802 struct got_object_id *commit_id, const char *path_prefix,
10803 int errcode, struct got_repository *repo)
10805 const struct got_error *err;
10806 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
10807 struct got_commit_object *commit = NULL, *parent_commit = NULL;
10808 struct check_path_prefix_arg cpp_arg;
10810 if (got_path_is_root_dir(path_prefix))
10813 err = got_object_open_as_commit(&commit, repo, commit_id);
10817 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
10821 err = got_object_open_as_tree(&tree1, repo,
10822 got_object_commit_get_tree_id(parent_commit));
10826 err = got_object_open_as_tree(&tree2, repo,
10827 got_object_commit_get_tree_id(commit));
10831 cpp_arg.path_prefix = path_prefix;
10832 while (cpp_arg.path_prefix[0] == '/')
10833 cpp_arg.path_prefix++;
10834 cpp_arg.len = strlen(cpp_arg.path_prefix);
10835 cpp_arg.errcode = errcode;
10836 err = got_diff_tree(tree1, tree2, NULL, NULL, -1, -1, "", "", repo,
10837 check_path_prefix_in_diff, &cpp_arg, 0);
10840 got_object_tree_close(tree1);
10842 got_object_tree_close(tree2);
10844 got_object_commit_close(commit);
10846 got_object_commit_close(parent_commit);
10850 static const struct got_error *
10851 collect_commits(struct got_object_id_queue *commits,
10852 struct got_object_id *initial_commit_id,
10853 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
10854 const char *path_prefix, int path_prefix_errcode,
10855 struct got_repository *repo)
10857 const struct got_error *err = NULL;
10858 struct got_commit_graph *graph = NULL;
10859 struct got_object_id parent_id, commit_id;
10860 struct got_object_qid *qid;
10862 err = got_commit_graph_open(&graph, "/", 1);
10866 err = got_commit_graph_bfsort(graph, iter_start_id, repo,
10867 check_cancelled, NULL);
10871 memcpy(&commit_id, initial_commit_id, sizeof(commit_id));
10872 while (got_object_id_cmp(&commit_id, iter_stop_id) != 0) {
10873 err = got_commit_graph_iter_next(&parent_id, graph, repo,
10874 check_cancelled, NULL);
10876 if (err->code == GOT_ERR_ITER_COMPLETED) {
10877 err = got_error_msg(GOT_ERR_ANCESTRY,
10878 "ran out of commits to rebase before "
10879 "youngest common ancestor commit has "
10880 "been reached?!?");
10884 err = check_path_prefix(&parent_id, &commit_id,
10885 path_prefix, path_prefix_errcode, repo);
10889 err = got_object_qid_alloc(&qid, &commit_id);
10892 STAILQ_INSERT_HEAD(commits, qid, entry);
10894 memcpy(&commit_id, &parent_id, sizeof(commit_id));
10898 got_commit_graph_close(graph);
10902 static const struct got_error *
10903 get_commit_brief_str(char **brief_str, struct got_commit_object *commit)
10905 const struct got_error *err = NULL;
10906 time_t committer_time;
10908 char datebuf[11]; /* YYYY-MM-DD + NUL */
10909 char *author0 = NULL, *author, *smallerthan;
10910 char *logmsg0 = NULL, *logmsg, *newline;
10912 committer_time = got_object_commit_get_committer_time(commit);
10913 if (gmtime_r(&committer_time, &tm) == NULL)
10914 return got_error_from_errno("gmtime_r");
10915 if (strftime(datebuf, sizeof(datebuf), "%G-%m-%d", &tm) == 0)
10916 return got_error(GOT_ERR_NO_SPACE);
10918 author0 = strdup(got_object_commit_get_author(commit));
10919 if (author0 == NULL)
10920 return got_error_from_errno("strdup");
10922 smallerthan = strchr(author, '<');
10923 if (smallerthan && smallerthan[1] != '\0')
10924 author = smallerthan + 1;
10925 author[strcspn(author, "@>")] = '\0';
10927 err = got_object_commit_get_logmsg(&logmsg0, commit);
10931 while (*logmsg == '\n')
10933 newline = strchr(logmsg, '\n');
10937 if (asprintf(brief_str, "%s %s %s",
10938 datebuf, author, logmsg) == -1)
10939 err = got_error_from_errno("asprintf");
10946 static const struct got_error *
10947 delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
10948 struct got_repository *repo)
10950 const struct got_error *err;
10953 err = got_object_id_str(&id_str, id);
10957 err = got_ref_delete(ref, repo);
10961 printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
10967 static const struct got_error *
10968 print_backup_ref(const char *branch_name, const char *new_id_str,
10969 struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
10970 struct got_reflist_object_id_map *refs_idmap,
10971 struct got_repository *repo)
10973 const struct got_error *err = NULL;
10974 struct got_reflist_head *refs;
10975 char *refs_str = NULL;
10976 struct got_object_id *new_commit_id = NULL;
10977 struct got_commit_object *new_commit = NULL;
10978 char *new_commit_brief_str = NULL;
10979 struct got_object_id *yca_id = NULL;
10980 struct got_commit_object *yca_commit = NULL;
10981 char *yca_id_str = NULL, *yca_brief_str = NULL;
10982 char *custom_refs_str;
10984 if (asprintf(&custom_refs_str, "formerly %s", branch_name) == -1)
10985 return got_error_from_errno("asprintf");
10987 err = print_commit(old_commit, old_commit_id, repo, NULL, NULL, NULL,
10988 0, 0, refs_idmap, custom_refs_str, NULL);
10992 err = got_object_resolve_id_str(&new_commit_id, repo, new_id_str);
10996 refs = got_reflist_object_id_map_lookup(refs_idmap, new_commit_id);
10998 err = build_refs_str(&refs_str, refs, new_commit_id, repo, 0);
11003 err = got_object_open_as_commit(&new_commit, repo, new_commit_id);
11007 err = get_commit_brief_str(&new_commit_brief_str, new_commit);
11011 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11012 old_commit_id, new_commit_id, 1, 0, repo, check_cancelled, NULL);
11016 printf("has become commit %s%s%s%s\n %s\n", new_id_str,
11017 refs_str ? " (" : "", refs_str ? refs_str : "",
11018 refs_str ? ")" : "", new_commit_brief_str);
11019 if (yca_id && got_object_id_cmp(yca_id, new_commit_id) != 0 &&
11020 got_object_id_cmp(yca_id, old_commit_id) != 0) {
11024 err = got_object_open_as_commit(&yca_commit, repo, yca_id);
11028 err = get_commit_brief_str(&yca_brief_str, yca_commit);
11032 err = got_object_id_str(&yca_id_str, yca_id);
11036 refs = got_reflist_object_id_map_lookup(refs_idmap, yca_id);
11038 err = build_refs_str(&refs_str, refs, yca_id, repo, 0);
11042 printf("history forked at %s%s%s%s\n %s\n",
11044 refs_str ? " (" : "", refs_str ? refs_str : "",
11045 refs_str ? ")" : "", yca_brief_str);
11048 free(custom_refs_str);
11049 free(new_commit_id);
11053 free(yca_brief_str);
11055 got_object_commit_close(new_commit);
11057 got_object_commit_close(yca_commit);
11062 static const struct got_error *
11063 worktree_has_logmsg_ref(const char *caller, struct got_worktree *worktree,
11064 struct got_repository *repo)
11066 const struct got_error *err;
11067 struct got_reflist_head refs;
11068 struct got_reflist_entry *re;
11069 char *uuidstr = NULL;
11070 static char msg[160];
11074 err = got_worktree_get_uuid(&uuidstr, worktree);
11078 err = got_ref_list(&refs, repo, "refs/got/worktree",
11079 got_ref_cmp_by_name, repo);
11083 TAILQ_FOREACH(re, &refs, entry) {
11084 const char *cmd, *refname, *type;
11086 refname = got_ref_get_name(re->ref);
11088 if (strncmp(refname, GOT_WORKTREE_CHERRYPICK_REF_PREFIX,
11089 GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN) == 0) {
11090 refname += GOT_WORKTREE_CHERRYPICK_REF_PREFIX_LEN + 1;
11091 cmd = "cherrypick";
11092 type = "cherrypicked";
11093 } else if (strncmp(refname, GOT_WORKTREE_BACKOUT_REF_PREFIX,
11094 GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN) == 0) {
11095 refname += GOT_WORKTREE_BACKOUT_REF_PREFIX_LEN + 1;
11097 type = "backed-out";
11101 if (strncmp(refname, uuidstr, GOT_WORKTREE_UUID_STRLEN) != 0)
11104 snprintf(msg, sizeof(msg),
11105 "work tree has references created by %s commits which "
11106 "must be removed with 'got %s -X' before running the %s "
11107 "command", type, cmd, caller);
11108 err = got_error_msg(GOT_ERR_WORKTREE_META, msg);
11114 got_ref_list_free(&refs);
11118 static const struct got_error *
11119 process_backup_refs(const char *backup_ref_prefix,
11120 const char *wanted_branch_name,
11121 int delete, struct got_repository *repo)
11123 const struct got_error *err;
11124 struct got_reflist_head refs, backup_refs;
11125 struct got_reflist_entry *re;
11126 const size_t backup_ref_prefix_len = strlen(backup_ref_prefix);
11127 struct got_object_id *old_commit_id = NULL;
11128 char *branch_name = NULL;
11129 struct got_commit_object *old_commit = NULL;
11130 struct got_reflist_object_id_map *refs_idmap = NULL;
11131 int wanted_branch_found = 0;
11134 TAILQ_INIT(&backup_refs);
11136 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
11140 err = got_reflist_object_id_map_create(&refs_idmap, &refs, repo);
11144 if (wanted_branch_name) {
11145 if (strncmp(wanted_branch_name, "refs/heads/", 11) == 0)
11146 wanted_branch_name += 11;
11149 err = got_ref_list(&backup_refs, repo, backup_ref_prefix,
11150 got_ref_cmp_by_commit_timestamp_descending, repo);
11154 TAILQ_FOREACH(re, &backup_refs, entry) {
11155 const char *refname = got_ref_get_name(re->ref);
11158 err = check_cancelled(NULL);
11162 err = got_ref_resolve(&old_commit_id, repo, re->ref);
11166 err = got_object_open_as_commit(&old_commit, repo,
11171 if (strncmp(backup_ref_prefix, refname,
11172 backup_ref_prefix_len) == 0)
11173 refname += backup_ref_prefix_len;
11175 while (refname[0] == '/')
11178 branch_name = strdup(refname);
11179 if (branch_name == NULL) {
11180 err = got_error_from_errno("strdup");
11183 slash = strrchr(branch_name, '/');
11186 refname += strlen(branch_name) + 1;
11189 if (wanted_branch_name == NULL ||
11190 strcmp(wanted_branch_name, branch_name) == 0) {
11191 wanted_branch_found = 1;
11193 err = delete_backup_ref(re->ref,
11194 old_commit_id, repo);
11196 err = print_backup_ref(branch_name, refname,
11197 old_commit_id, old_commit, refs_idmap,
11204 free(old_commit_id);
11205 old_commit_id = NULL;
11207 branch_name = NULL;
11208 got_object_commit_close(old_commit);
11212 if (wanted_branch_name && !wanted_branch_found) {
11213 err = got_error_fmt(GOT_ERR_NOT_REF,
11214 "%s/%s/", backup_ref_prefix, wanted_branch_name);
11218 got_reflist_object_id_map_free(refs_idmap);
11219 got_ref_list_free(&refs);
11220 got_ref_list_free(&backup_refs);
11221 free(old_commit_id);
11224 got_object_commit_close(old_commit);
11228 static const struct got_error *
11229 abort_progress(void *arg, unsigned char status, const char *path)
11232 * Unversioned files should not clutter progress output when
11233 * an operation is aborted.
11235 if (status == GOT_STATUS_UNVERSIONED)
11238 return update_progress(arg, status, path);
11241 static const struct got_error *
11242 find_merge_commit_yca(struct got_object_id **new_yca_id,
11243 struct got_object_id *branch_head_commit_id,
11244 struct got_object_id *yca_id,
11245 struct got_object_id *base_commit_id,
11246 struct got_repository *repo)
11248 const struct got_error *err = NULL;
11249 struct got_commit_graph *graph = NULL;
11250 struct got_commit_object *commit = NULL;
11252 *new_yca_id = NULL;
11254 err = got_commit_graph_open(&graph, "/", 1);
11258 err = got_commit_graph_bfsort(graph, base_commit_id,
11259 repo, check_cancelled, NULL);
11264 struct got_object_id id;
11266 err = got_commit_graph_iter_next(&id, graph, repo,
11267 check_cancelled, NULL);
11269 if (err->code == GOT_ERR_ITER_COMPLETED)
11274 err = got_object_open_as_commit(&commit, repo, &id);
11278 if (got_object_commit_get_nparents(commit) > 1) {
11279 /* Search for a better YCA using toposort. */
11280 err = got_commit_graph_find_youngest_common_ancestor(
11281 new_yca_id, base_commit_id, branch_head_commit_id,
11282 0, 1, repo, check_cancelled, NULL);
11286 if (got_object_id_cmp(&id, yca_id) == 0)
11288 got_object_commit_close(commit);
11292 got_commit_graph_close(graph);
11294 got_object_commit_close(commit);
11298 static const struct got_error *
11299 cmd_rebase(int argc, char *argv[])
11301 const struct got_error *error = NULL;
11302 struct got_worktree *worktree = NULL;
11303 struct got_repository *repo = NULL;
11304 struct got_fileindex *fileindex = NULL;
11305 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
11306 struct got_reference *branch = NULL;
11307 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
11308 struct got_object_id *commit_id = NULL, *parent_id = NULL;
11309 struct got_object_id *resume_commit_id = NULL;
11310 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
11311 struct got_object_id *head_commit_id = NULL;
11312 struct got_reference *head_ref = NULL;
11313 struct got_commit_object *commit = NULL;
11314 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
11315 int histedit_in_progress = 0, merge_in_progress = 0;
11316 int create_backup = 1, list_backups = 0, delete_backups = 0;
11317 int allow_conflict = 0;
11318 struct got_object_id_queue commits;
11319 struct got_pathlist_head merged_paths;
11320 const struct got_object_id_queue *parent_ids;
11321 struct got_object_qid *qid, *pid;
11322 struct got_update_progress_arg upa;
11323 int *pack_fds = NULL;
11325 STAILQ_INIT(&commits);
11326 TAILQ_INIT(&merged_paths);
11327 memset(&upa, 0, sizeof(upa));
11330 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
11331 "unveil", NULL) == -1)
11335 while ((ch = getopt(argc, argv, "aCclX")) != -1) {
11341 allow_conflict = 1;
11344 continue_rebase = 1;
11350 delete_backups = 1;
11361 if (list_backups) {
11363 option_conflict('l', 'a');
11364 if (allow_conflict)
11365 option_conflict('l', 'C');
11366 if (continue_rebase)
11367 option_conflict('l', 'c');
11368 if (delete_backups)
11369 option_conflict('l', 'X');
11370 if (argc != 0 && argc != 1)
11372 } else if (delete_backups) {
11374 option_conflict('X', 'a');
11375 if (allow_conflict)
11376 option_conflict('X', 'C');
11377 if (continue_rebase)
11378 option_conflict('X', 'c');
11380 option_conflict('l', 'X');
11381 if (argc != 0 && argc != 1)
11383 } else if (allow_conflict) {
11385 option_conflict('C', 'a');
11386 if (!continue_rebase)
11387 errx(1, "-C option requires -c");
11389 if (abort_rebase && continue_rebase)
11391 else if (abort_rebase || continue_rebase) {
11394 } else if (argc != 1)
11398 cwd = getcwd(NULL, 0);
11400 error = got_error_from_errno("getcwd");
11404 error = got_repo_pack_fds_open(&pack_fds);
11408 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
11410 if (list_backups || delete_backups) {
11411 if (error->code != GOT_ERR_NOT_WORKTREE)
11414 if (error->code == GOT_ERR_NOT_WORKTREE)
11415 error = wrap_not_worktree_error(error,
11421 error = get_gitconfig_path(&gitconfig_path);
11424 error = got_repo_open(&repo,
11425 worktree ? got_worktree_get_repo_path(worktree) : cwd,
11426 gitconfig_path, pack_fds);
11430 if (worktree != NULL && !list_backups && !delete_backups) {
11431 error = worktree_has_logmsg_ref("rebase", worktree, repo);
11436 error = get_author(&committer, repo, worktree);
11437 if (error && error->code != GOT_ERR_COMMIT_NO_AUTHOR)
11440 error = apply_unveil(got_repo_get_path(repo), 0,
11441 worktree ? got_worktree_get_root_path(worktree) : NULL);
11445 if (list_backups || delete_backups) {
11446 error = process_backup_refs(
11447 GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
11448 argc == 1 ? argv[0] : NULL, delete_backups, repo);
11449 goto done; /* nothing else to do */
11452 error = got_worktree_histedit_in_progress(&histedit_in_progress,
11456 if (histedit_in_progress) {
11457 error = got_error(GOT_ERR_HISTEDIT_BUSY);
11461 error = got_worktree_merge_in_progress(&merge_in_progress,
11465 if (merge_in_progress) {
11466 error = got_error(GOT_ERR_MERGE_BUSY);
11470 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
11474 if (abort_rebase) {
11475 if (!rebase_in_progress) {
11476 error = got_error(GOT_ERR_NOT_REBASING);
11479 error = got_worktree_rebase_continue(&resume_commit_id,
11480 &new_base_branch, &tmp_branch, &branch, &fileindex,
11484 printf("Switching work tree to %s\n",
11485 got_ref_get_symref_target(new_base_branch));
11486 error = got_worktree_rebase_abort(worktree, fileindex, repo,
11487 new_base_branch, abort_progress, &upa);
11490 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
11491 print_merge_progress_stats(&upa);
11492 goto done; /* nothing else to do */
11495 if (continue_rebase) {
11496 if (!rebase_in_progress) {
11497 error = got_error(GOT_ERR_NOT_REBASING);
11500 error = got_worktree_rebase_continue(&resume_commit_id,
11501 &new_base_branch, &tmp_branch, &branch, &fileindex,
11506 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
11507 committer, resume_commit_id, allow_conflict, repo);
11511 yca_id = got_object_id_dup(resume_commit_id);
11512 if (yca_id == NULL) {
11513 error = got_error_from_errno("got_object_id_dup");
11517 error = got_ref_open(&branch, repo, argv[0], 0);
11520 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
11521 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
11522 "will not rebase a branch which lives outside "
11523 "the \"refs/heads/\" reference namespace");
11528 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
11532 if (!continue_rebase) {
11533 struct got_object_id *base_commit_id;
11535 error = got_ref_open(&head_ref, repo,
11536 got_worktree_get_head_ref_name(worktree), 0);
11539 error = got_ref_resolve(&head_commit_id, repo, head_ref);
11542 base_commit_id = got_worktree_get_base_commit_id(worktree);
11543 if (got_object_id_cmp(base_commit_id, head_commit_id) != 0) {
11544 error = got_error(GOT_ERR_REBASE_OUT_OF_DATE);
11548 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
11549 base_commit_id, branch_head_commit_id, 1, 0,
11550 repo, check_cancelled, NULL);
11552 if (error->code == GOT_ERR_ANCESTRY) {
11553 error = got_error_msg(GOT_ERR_ANCESTRY,
11554 "specified branch shares no common "
11555 "ancestry with work tree's branch");
11561 * If a merge commit appears between the new base branch tip
11562 * and a YCA found via first-parent traversal then we might
11563 * find a better YCA using topologically sorted commits.
11565 if (got_object_id_cmp(base_commit_id, yca_id) != 0) {
11566 struct got_object_id *better_yca_id;
11567 error = find_merge_commit_yca(&better_yca_id,
11568 branch_head_commit_id, yca_id,
11569 base_commit_id, repo);
11572 if (better_yca_id) {
11574 yca_id = better_yca_id;
11578 if (got_object_id_cmp(base_commit_id, yca_id) == 0) {
11579 struct got_pathlist_head paths;
11580 printf("%s is already based on %s\n",
11581 got_ref_get_name(branch),
11582 got_worktree_get_head_ref_name(worktree));
11583 error = switch_head_ref(branch, branch_head_commit_id,
11587 error = got_worktree_set_base_commit_id(worktree, repo,
11588 branch_head_commit_id);
11591 TAILQ_INIT(&paths);
11592 error = got_pathlist_append(&paths, "", NULL);
11595 error = got_worktree_checkout_files(worktree,
11596 &paths, repo, update_progress, &upa,
11597 check_cancelled, NULL);
11598 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
11601 if (upa.did_something) {
11603 error = got_object_id_str(&id_str,
11604 branch_head_commit_id);
11607 printf("Updated to %s: %s\n",
11608 got_worktree_get_head_ref_name(worktree),
11612 printf("Already up-to-date\n");
11613 print_update_progress_stats(&upa);
11618 commit_id = branch_head_commit_id;
11619 error = got_object_open_as_commit(&commit, repo, commit_id);
11623 parent_ids = got_object_commit_get_parent_ids(commit);
11624 pid = STAILQ_FIRST(parent_ids);
11626 error = collect_commits(&commits, commit_id, &pid->id,
11627 yca_id, got_worktree_get_path_prefix(worktree),
11628 GOT_ERR_REBASE_PATH, repo);
11633 got_object_commit_close(commit);
11636 if (!continue_rebase) {
11637 error = got_worktree_rebase_prepare(&new_base_branch,
11638 &tmp_branch, &fileindex, worktree, branch, repo);
11643 if (STAILQ_EMPTY(&commits)) {
11644 if (continue_rebase) {
11645 error = rebase_complete(worktree, fileindex,
11646 branch, tmp_branch, repo, create_backup);
11649 /* Fast-forward the reference of the branch. */
11650 struct got_object_id *new_head_commit_id;
11652 error = got_ref_resolve(&new_head_commit_id, repo,
11656 error = got_object_id_str(&id_str, new_head_commit_id);
11659 printf("Forwarding %s to commit %s\n",
11660 got_ref_get_name(branch), id_str);
11662 error = got_ref_change_ref(branch,
11663 new_head_commit_id);
11666 /* No backup needed since objects did not change. */
11672 STAILQ_FOREACH(qid, &commits, entry) {
11674 commit_id = &qid->id;
11675 parent_id = pid ? &pid->id : yca_id;
11678 memset(&upa, 0, sizeof(upa));
11679 error = got_worktree_rebase_merge_files(&merged_paths,
11680 worktree, fileindex, parent_id, commit_id, repo,
11681 update_progress, &upa, check_cancelled, NULL);
11685 print_merge_progress_stats(&upa);
11686 if (upa.conflicts > 0 || upa.missing > 0 ||
11687 upa.not_deleted > 0 || upa.unversioned > 0) {
11688 if (upa.conflicts > 0) {
11689 error = show_rebase_merge_conflict(&qid->id,
11694 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11698 error = rebase_commit(&merged_paths, worktree, fileindex,
11699 tmp_branch, committer, commit_id, 0, repo);
11700 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
11705 if (upa.conflicts > 0 || upa.missing > 0 ||
11706 upa.not_deleted > 0 || upa.unversioned > 0) {
11707 error = got_worktree_rebase_postpone(worktree, fileindex);
11710 if (upa.conflicts > 0 && upa.missing == 0 &&
11711 upa.not_deleted == 0 && upa.unversioned == 0) {
11712 error = got_error_msg(GOT_ERR_CONFLICTS,
11713 "conflicts must be resolved before rebasing "
11715 } else if (upa.conflicts > 0) {
11716 error = got_error_msg(GOT_ERR_CONFLICTS,
11717 "conflicts must be resolved before rebasing "
11718 "can continue; changes destined for some "
11719 "files were not yet merged and should be "
11720 "merged manually if required before the "
11721 "rebase operation is continued");
11723 error = got_error_msg(GOT_ERR_CONFLICTS,
11724 "changes destined for some files were not "
11725 "yet merged and should be merged manually "
11726 "if required before the rebase operation "
11730 error = rebase_complete(worktree, fileindex, branch,
11731 tmp_branch, repo, create_backup);
11735 free(gitconfig_path);
11736 got_object_id_queue_free(&commits);
11737 free(branch_head_commit_id);
11738 free(resume_commit_id);
11739 free(head_commit_id);
11742 got_object_commit_close(commit);
11744 got_ref_close(branch);
11745 if (new_base_branch)
11746 got_ref_close(new_base_branch);
11748 got_ref_close(tmp_branch);
11750 got_ref_close(head_ref);
11752 got_worktree_close(worktree);
11754 const struct got_error *close_err = got_repo_close(repo);
11759 const struct got_error *pack_err =
11760 got_repo_pack_fds_close(pack_fds);
11768 usage_histedit(void)
11770 fprintf(stderr, "usage: %s histedit [-aCcdeflmX] [-F histedit-script] "
11771 "[branch]\n", getprogname());
11775 #define GOT_HISTEDIT_PICK 'p'
11776 #define GOT_HISTEDIT_EDIT 'e'
11777 #define GOT_HISTEDIT_FOLD 'f'
11778 #define GOT_HISTEDIT_DROP 'd'
11779 #define GOT_HISTEDIT_MESG 'm'
11781 static const struct got_histedit_cmd {
11782 unsigned char code;
11785 } got_histedit_cmds[] = {
11786 { GOT_HISTEDIT_PICK, "pick", "use commit" },
11787 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
11788 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
11790 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
11791 { GOT_HISTEDIT_MESG, "mesg", "open editor to edit the log message" },
11794 struct got_histedit_list_entry {
11795 TAILQ_ENTRY(got_histedit_list_entry) entry;
11796 struct got_object_id *commit_id;
11797 const struct got_histedit_cmd *cmd;
11800 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
11802 static const struct got_error *
11803 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
11804 FILE *f, struct got_repository *repo)
11806 const struct got_error *err = NULL;
11807 char *logmsg = NULL, *id_str = NULL;
11808 struct got_commit_object *commit = NULL;
11811 err = got_object_open_as_commit(&commit, repo, commit_id);
11815 err = get_short_logmsg(&logmsg, 34, commit);
11819 err = got_object_id_str(&id_str, commit_id);
11823 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
11825 err = got_ferror(f, GOT_ERR_IO);
11828 got_object_commit_close(commit);
11834 static const struct got_error *
11835 histedit_write_commit_list(struct got_object_id_queue *commits,
11836 FILE *f, int edit_logmsg_only, int fold_only, int drop_only,
11837 int edit_only, struct got_repository *repo)
11839 const struct got_error *err = NULL;
11840 struct got_object_qid *qid;
11841 const char *histedit_cmd = NULL;
11843 if (STAILQ_EMPTY(commits))
11844 return got_error(GOT_ERR_EMPTY_HISTEDIT);
11846 STAILQ_FOREACH(qid, commits, entry) {
11847 histedit_cmd = got_histedit_cmds[0].name;
11849 histedit_cmd = "drop";
11850 else if (edit_only)
11851 histedit_cmd = "edit";
11852 else if (fold_only && STAILQ_NEXT(qid, entry) != NULL)
11853 histedit_cmd = "fold";
11854 else if (edit_logmsg_only)
11855 histedit_cmd = "mesg";
11856 err = histedit_write_commit(&qid->id, histedit_cmd, f, repo);
11864 static const struct got_error *
11865 write_cmd_list(FILE *f, const char *branch_name,
11866 struct got_object_id_queue *commits)
11868 const struct got_error *err = NULL;
11872 struct got_object_qid *qid;
11874 qid = STAILQ_FIRST(commits);
11875 err = got_object_id_str(&id_str, &qid->id);
11880 "# Editing the history of branch '%s' starting at\n"
11882 "# Commits will be processed in order from top to "
11883 "bottom of this file.\n", branch_name, id_str);
11885 err = got_ferror(f, GOT_ERR_IO);
11889 n = fprintf(f, "# Available histedit commands:\n");
11891 err = got_ferror(f, GOT_ERR_IO);
11895 for (i = 0; i < nitems(got_histedit_cmds); i++) {
11896 const struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
11897 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
11900 err = got_ferror(f, GOT_ERR_IO);
11909 static const struct got_error *
11910 histedit_syntax_error(int lineno)
11912 static char msg[42];
11915 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
11917 if (ret < 0 || (size_t)ret >= sizeof(msg))
11918 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
11920 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
11923 static const struct got_error *
11924 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
11925 char *logmsg, struct got_repository *repo)
11927 const struct got_error *err;
11928 struct got_commit_object *folded_commit = NULL;
11929 char *id_str, *folded_logmsg = NULL;
11931 err = got_object_id_str(&id_str, hle->commit_id);
11935 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
11939 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
11942 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
11943 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
11944 folded_logmsg) == -1) {
11945 err = got_error_from_errno("asprintf");
11949 got_object_commit_close(folded_commit);
11951 free(folded_logmsg);
11955 static struct got_histedit_list_entry *
11956 get_folded_commits(struct got_histedit_list_entry *hle)
11958 struct got_histedit_list_entry *prev, *folded = NULL;
11960 prev = TAILQ_PREV(hle, got_histedit_list, entry);
11961 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
11962 prev->cmd->code == GOT_HISTEDIT_DROP)) {
11963 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
11965 prev = TAILQ_PREV(prev, got_histedit_list, entry);
11971 static const struct got_error *
11972 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
11973 const char *editor, struct got_repository *repo)
11975 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
11976 char *logmsg = NULL, *new_msg = NULL;
11977 const struct got_error *err = NULL;
11978 struct got_commit_object *commit = NULL;
11981 struct got_histedit_list_entry *folded = NULL;
11983 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
11987 folded = get_folded_commits(hle);
11989 while (folded != hle) {
11990 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
11991 folded = TAILQ_NEXT(folded, entry);
11994 err = append_folded_commit_msg(&new_msg, folded,
12000 folded = TAILQ_NEXT(folded, entry);
12004 err = got_object_id_str(&id_str, hle->commit_id);
12007 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
12010 logmsg_len = asprintf(&new_msg,
12011 "%s\n# original log message of commit %s: %s",
12012 logmsg ? logmsg : "", id_str, orig_logmsg);
12013 if (logmsg_len == -1) {
12014 err = got_error_from_errno("asprintf");
12020 err = got_object_id_str(&id_str, hle->commit_id);
12024 err = got_opentemp_named_fd(&logmsg_path, &fd,
12025 GOT_TMPDIR_STR "/got-logmsg", "");
12029 if (write(fd, logmsg, logmsg_len) == -1) {
12030 err = got_error_from_errno2("write", logmsg_path);
12033 if (close(fd) == -1) {
12034 err = got_error_from_errno2("close", logmsg_path);
12039 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg,
12042 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
12045 hle->logmsg = strdup(new_msg);
12046 if (hle->logmsg == NULL)
12047 err = got_error_from_errno("strdup");
12050 if (fd != -1 && close(fd) == -1 && err == NULL)
12051 err = got_error_from_errno2("close", logmsg_path);
12052 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
12053 err = got_error_from_errno2("unlink", logmsg_path);
12058 got_object_commit_close(commit);
12062 static const struct got_error *
12063 histedit_parse_list(struct got_histedit_list *histedit_cmds,
12064 FILE *f, struct got_repository *repo)
12066 const struct got_error *err = NULL;
12067 char *line = NULL, *p, *end;
12068 size_t i, linesize = 0;
12071 const struct got_histedit_cmd *cmd;
12072 struct got_object_id *commit_id = NULL;
12073 struct got_histedit_list_entry *hle = NULL;
12076 linelen = getline(&line, &linesize, f);
12077 if (linelen == -1) {
12078 const struct got_error *getline_err;
12081 getline_err = got_error_from_errno("getline");
12082 err = got_ferror(f, getline_err->code);
12087 while (isspace((unsigned char)p[0]))
12089 if (p[0] == '#' || p[0] == '\0')
12092 for (i = 0; i < nitems(got_histedit_cmds); i++) {
12093 cmd = &got_histedit_cmds[i];
12094 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
12095 isspace((unsigned char)p[strlen(cmd->name)])) {
12096 p += strlen(cmd->name);
12099 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
12104 if (i == nitems(got_histedit_cmds)) {
12105 err = histedit_syntax_error(lineno);
12108 while (isspace((unsigned char)p[0]))
12111 while (end[0] && !isspace((unsigned char)end[0]))
12114 err = got_object_resolve_id_str(&commit_id, repo, p);
12116 /* override error code */
12117 err = histedit_syntax_error(lineno);
12120 hle = malloc(sizeof(*hle));
12122 err = got_error_from_errno("malloc");
12126 hle->commit_id = commit_id;
12127 hle->logmsg = NULL;
12129 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
12137 static const struct got_error *
12138 histedit_check_script(struct got_histedit_list *histedit_cmds,
12139 struct got_object_id_queue *commits, struct got_repository *repo)
12141 const struct got_error *err = NULL;
12142 struct got_object_qid *qid;
12143 struct got_histedit_list_entry *hle;
12144 static char msg[92];
12147 if (TAILQ_EMPTY(histedit_cmds))
12148 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12149 "histedit script contains no commands");
12150 if (STAILQ_EMPTY(commits))
12151 return got_error(GOT_ERR_EMPTY_HISTEDIT);
12153 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12154 struct got_histedit_list_entry *hle2;
12155 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
12158 if (got_object_id_cmp(hle->commit_id,
12159 hle2->commit_id) != 0)
12161 err = got_object_id_str(&id_str, hle->commit_id);
12164 snprintf(msg, sizeof(msg), "commit %s is listed "
12165 "more than once in histedit script", id_str);
12167 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12171 STAILQ_FOREACH(qid, commits, entry) {
12172 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12173 if (got_object_id_cmp(&qid->id, hle->commit_id) == 0)
12177 err = got_object_id_str(&id_str, &qid->id);
12180 snprintf(msg, sizeof(msg),
12181 "commit %s missing from histedit script", id_str);
12183 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
12187 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
12188 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
12189 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
12190 "last commit in histedit script cannot be folded");
12195 static const struct got_error *
12196 histedit_run_editor(struct got_histedit_list *histedit_cmds,
12197 const char *editor, const char *path,
12198 struct got_object_id_queue *commits, struct got_repository *repo)
12200 const struct got_error *err = NULL;
12201 struct stat st, st2;
12202 struct timespec timeout;
12205 if (stat(path, &st) == -1) {
12206 err = got_error_from_errno2("stat", path);
12210 if (spawn_editor(editor, path) == -1) {
12211 err = got_error_from_errno("failed spawning editor");
12215 timeout.tv_sec = 0;
12216 timeout.tv_nsec = 1;
12217 nanosleep(&timeout, NULL);
12219 if (stat(path, &st2) == -1) {
12220 err = got_error_from_errno2("stat", path);
12224 if (st.st_size == st2.st_size &&
12225 timespeccmp(&st.st_mtim, &st2.st_mtim, ==)) {
12226 err = got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
12227 "no changes made to histedit script, aborting");
12231 f = fopen(path, "re");
12233 err = got_error_from_errno("fopen");
12236 err = histedit_parse_list(histedit_cmds, f, repo);
12240 err = histedit_check_script(histedit_cmds, commits, repo);
12242 if (f && fclose(f) == EOF && err == NULL)
12243 err = got_error_from_errno("fclose");
12247 static const struct got_error *
12248 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
12249 struct got_object_id_queue *, const char *, const char *, const char *,
12250 struct got_repository *);
12252 static const struct got_error *
12253 histedit_edit_script(struct got_histedit_list *histedit_cmds,
12254 struct got_object_id_queue *commits, const char *branch_name,
12255 int edit_logmsg_only, int fold_only, int drop_only, int edit_only,
12256 const char *editor, struct got_repository *repo)
12258 const struct got_error *err;
12262 err = got_opentemp_named(&path, &f, "got-histedit", "");
12266 err = write_cmd_list(f, branch_name, commits);
12270 err = histedit_write_commit_list(commits, f, edit_logmsg_only,
12271 fold_only, drop_only, edit_only, repo);
12275 if (drop_only || edit_logmsg_only || fold_only || edit_only) {
12277 err = histedit_parse_list(histedit_cmds, f, repo);
12279 if (fclose(f) == EOF) {
12280 err = got_error_from_errno("fclose");
12284 err = histedit_run_editor(histedit_cmds, editor, path,
12287 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12288 err->code != GOT_ERR_HISTEDIT_CMD)
12290 err = histedit_edit_list_retry(histedit_cmds, err,
12291 commits, editor, path, branch_name, repo);
12295 if (f && fclose(f) == EOF && err == NULL)
12296 err = got_error_from_errno("fclose");
12297 if (path && unlink(path) != 0 && err == NULL)
12298 err = got_error_from_errno2("unlink", path);
12303 static const struct got_error *
12304 histedit_save_list(struct got_histedit_list *histedit_cmds,
12305 struct got_worktree *worktree, struct got_repository *repo)
12307 const struct got_error *err = NULL;
12310 struct got_histedit_list_entry *hle;
12312 err = got_worktree_get_histedit_script_path(&path, worktree);
12316 f = fopen(path, "we");
12318 err = got_error_from_errno2("fopen", path);
12321 TAILQ_FOREACH(hle, histedit_cmds, entry) {
12322 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
12328 if (f && fclose(f) == EOF && err == NULL)
12329 err = got_error_from_errno("fclose");
12335 histedit_free_list(struct got_histedit_list *histedit_cmds)
12337 struct got_histedit_list_entry *hle;
12339 while ((hle = TAILQ_FIRST(histedit_cmds))) {
12340 TAILQ_REMOVE(histedit_cmds, hle, entry);
12345 static const struct got_error *
12346 histedit_load_list(struct got_histedit_list *histedit_cmds,
12347 const char *path, struct got_repository *repo)
12349 const struct got_error *err = NULL;
12352 f = fopen(path, "re");
12354 err = got_error_from_errno2("fopen", path);
12358 err = histedit_parse_list(histedit_cmds, f, repo);
12360 if (f && fclose(f) == EOF && err == NULL)
12361 err = got_error_from_errno("fclose");
12365 static const struct got_error *
12366 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
12367 const struct got_error *edit_err, struct got_object_id_queue *commits,
12368 const char *editor, const char *path, const char *branch_name,
12369 struct got_repository *repo)
12371 const struct got_error *err = NULL, *prev_err = edit_err;
12374 while (resp != 'c' && resp != 'r' && resp != 'a') {
12375 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
12376 "or (a)bort: ", getprogname(), prev_err->msg);
12381 histedit_free_list(histedit_cmds);
12382 err = histedit_run_editor(histedit_cmds, editor, path,
12385 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12386 err->code != GOT_ERR_HISTEDIT_CMD)
12393 } else if (resp == 'r') {
12394 histedit_free_list(histedit_cmds);
12395 err = histedit_edit_script(histedit_cmds,
12396 commits, branch_name, 0, 0, 0, 0, editor, repo);
12398 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
12399 err->code != GOT_ERR_HISTEDIT_CMD)
12406 } else if (resp == 'a') {
12407 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
12410 printf("invalid response '%c'\n", resp);
12416 static const struct got_error *
12417 histedit_complete(struct got_worktree *worktree,
12418 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
12419 struct got_reference *branch, struct got_repository *repo)
12421 printf("Switching work tree to %s\n",
12422 got_ref_get_symref_target(branch));
12423 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
12427 static const struct got_error *
12428 show_histedit_progress(struct got_commit_object *commit,
12429 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
12431 const struct got_error *err;
12432 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
12434 err = got_object_id_str(&old_id_str, hle->commit_id);
12439 err = got_object_id_str(&new_id_str, new_id);
12444 old_id_str[12] = '\0';
12446 new_id_str[12] = '\0';
12449 logmsg = strdup(hle->logmsg);
12450 if (logmsg == NULL) {
12451 err = got_error_from_errno("strdup");
12454 trim_logmsg(logmsg, 42);
12456 err = get_short_logmsg(&logmsg, 42, commit);
12461 switch (hle->cmd->code) {
12462 case GOT_HISTEDIT_PICK:
12463 case GOT_HISTEDIT_EDIT:
12464 case GOT_HISTEDIT_MESG:
12465 printf("%s -> %s: %s\n", old_id_str,
12466 new_id_str ? new_id_str : "no-op change", logmsg);
12468 case GOT_HISTEDIT_DROP:
12469 case GOT_HISTEDIT_FOLD:
12470 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
12482 static const struct got_error *
12483 histedit_commit(struct got_pathlist_head *merged_paths,
12484 struct got_worktree *worktree, struct got_fileindex *fileindex,
12485 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
12486 const char *committer, int allow_conflict, const char *editor,
12487 struct got_repository *repo)
12489 const struct got_error *err;
12490 struct got_commit_object *commit;
12491 struct got_object_id *new_commit_id;
12493 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
12494 && hle->logmsg == NULL) {
12495 err = histedit_edit_logmsg(hle, editor, repo);
12500 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
12504 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
12505 worktree, fileindex, tmp_branch, committer, commit, hle->commit_id,
12506 hle->logmsg, allow_conflict, repo);
12508 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
12510 err = show_histedit_progress(commit, hle, NULL);
12512 err = show_histedit_progress(commit, hle, new_commit_id);
12513 free(new_commit_id);
12516 got_object_commit_close(commit);
12520 static const struct got_error *
12521 histedit_skip_commit(struct got_histedit_list_entry *hle,
12522 struct got_worktree *worktree, struct got_repository *repo)
12524 const struct got_error *error;
12525 struct got_commit_object *commit;
12527 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
12532 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
12536 error = show_histedit_progress(commit, hle, NULL);
12537 got_object_commit_close(commit);
12541 static const struct got_error *
12542 check_local_changes(void *arg, unsigned char status,
12543 unsigned char staged_status, const char *path,
12544 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
12545 struct got_object_id *commit_id, int dirfd, const char *de_name)
12547 int *have_local_changes = arg;
12550 case GOT_STATUS_ADD:
12551 case GOT_STATUS_DELETE:
12552 case GOT_STATUS_MODIFY:
12553 case GOT_STATUS_CONFLICT:
12554 *have_local_changes = 1;
12555 return got_error(GOT_ERR_CANCELLED);
12560 switch (staged_status) {
12561 case GOT_STATUS_ADD:
12562 case GOT_STATUS_DELETE:
12563 case GOT_STATUS_MODIFY:
12564 *have_local_changes = 1;
12565 return got_error(GOT_ERR_CANCELLED);
12573 static const struct got_error *
12574 cmd_histedit(int argc, char *argv[])
12576 const struct got_error *error = NULL;
12577 struct got_worktree *worktree = NULL;
12578 struct got_fileindex *fileindex = NULL;
12579 struct got_repository *repo = NULL;
12580 char *cwd = NULL, *committer = NULL, *gitconfig_path = NULL;
12581 struct got_reference *branch = NULL;
12582 struct got_reference *tmp_branch = NULL;
12583 struct got_object_id *resume_commit_id = NULL;
12584 struct got_object_id *base_commit_id = NULL;
12585 struct got_object_id *head_commit_id = NULL;
12586 struct got_commit_object *commit = NULL;
12587 int ch, rebase_in_progress = 0, merge_in_progress = 0;
12588 struct got_update_progress_arg upa;
12589 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
12590 int drop_only = 0, edit_logmsg_only = 0, fold_only = 0, edit_only = 0;
12591 int allow_conflict = 0, list_backups = 0, delete_backups = 0;
12592 const char *edit_script_path = NULL;
12593 char *editor = NULL;
12594 struct got_object_id_queue commits;
12595 struct got_pathlist_head merged_paths;
12596 const struct got_object_id_queue *parent_ids;
12597 struct got_object_qid *pid;
12598 struct got_histedit_list histedit_cmds;
12599 struct got_histedit_list_entry *hle;
12600 int *pack_fds = NULL;
12602 STAILQ_INIT(&commits);
12603 TAILQ_INIT(&histedit_cmds);
12604 TAILQ_INIT(&merged_paths);
12605 memset(&upa, 0, sizeof(upa));
12608 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
12609 "unveil", NULL) == -1)
12613 while ((ch = getopt(argc, argv, "aCcdeF:flmX")) != -1) {
12619 allow_conflict = 1;
12631 edit_script_path = optarg;
12640 edit_logmsg_only = 1;
12643 delete_backups = 1;
12654 if (abort_edit && allow_conflict)
12655 option_conflict('a', 'C');
12656 if (abort_edit && continue_edit)
12657 option_conflict('a', 'c');
12658 if (edit_script_path && allow_conflict)
12659 option_conflict('F', 'C');
12660 if (edit_script_path && edit_logmsg_only)
12661 option_conflict('F', 'm');
12662 if (abort_edit && edit_logmsg_only)
12663 option_conflict('a', 'm');
12664 if (edit_logmsg_only && allow_conflict)
12665 option_conflict('m', 'C');
12666 if (continue_edit && edit_logmsg_only)
12667 option_conflict('c', 'm');
12668 if (abort_edit && fold_only)
12669 option_conflict('a', 'f');
12670 if (fold_only && allow_conflict)
12671 option_conflict('f', 'C');
12672 if (continue_edit && fold_only)
12673 option_conflict('c', 'f');
12674 if (fold_only && edit_logmsg_only)
12675 option_conflict('f', 'm');
12676 if (edit_script_path && fold_only)
12677 option_conflict('F', 'f');
12678 if (abort_edit && edit_only)
12679 option_conflict('a', 'e');
12680 if (continue_edit && edit_only)
12681 option_conflict('c', 'e');
12682 if (edit_only && edit_logmsg_only)
12683 option_conflict('e', 'm');
12684 if (edit_script_path && edit_only)
12685 option_conflict('F', 'e');
12686 if (fold_only && edit_only)
12687 option_conflict('f', 'e');
12688 if (drop_only && abort_edit)
12689 option_conflict('d', 'a');
12690 if (drop_only && allow_conflict)
12691 option_conflict('d', 'C');
12692 if (drop_only && continue_edit)
12693 option_conflict('d', 'c');
12694 if (drop_only && edit_logmsg_only)
12695 option_conflict('d', 'm');
12696 if (drop_only && edit_only)
12697 option_conflict('d', 'e');
12698 if (drop_only && edit_script_path)
12699 option_conflict('d', 'F');
12700 if (drop_only && fold_only)
12701 option_conflict('d', 'f');
12702 if (list_backups) {
12704 option_conflict('l', 'a');
12705 if (allow_conflict)
12706 option_conflict('l', 'C');
12708 option_conflict('l', 'c');
12709 if (edit_script_path)
12710 option_conflict('l', 'F');
12711 if (edit_logmsg_only)
12712 option_conflict('l', 'm');
12714 option_conflict('l', 'd');
12716 option_conflict('l', 'f');
12718 option_conflict('l', 'e');
12719 if (delete_backups)
12720 option_conflict('l', 'X');
12721 if (argc != 0 && argc != 1)
12723 } else if (delete_backups) {
12725 option_conflict('X', 'a');
12726 if (allow_conflict)
12727 option_conflict('X', 'C');
12729 option_conflict('X', 'c');
12731 option_conflict('X', 'd');
12732 if (edit_script_path)
12733 option_conflict('X', 'F');
12734 if (edit_logmsg_only)
12735 option_conflict('X', 'm');
12737 option_conflict('X', 'f');
12739 option_conflict('X', 'e');
12741 option_conflict('X', 'l');
12742 if (argc != 0 && argc != 1)
12744 } else if (allow_conflict && !continue_edit)
12745 errx(1, "-C option requires -c");
12746 else if (argc != 0)
12749 cwd = getcwd(NULL, 0);
12751 error = got_error_from_errno("getcwd");
12755 error = got_repo_pack_fds_open(&pack_fds);
12759 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
12761 if (list_backups || delete_backups) {
12762 if (error->code != GOT_ERR_NOT_WORKTREE)
12765 if (error->code == GOT_ERR_NOT_WORKTREE)
12766 error = wrap_not_worktree_error(error,
12772 if (list_backups || delete_backups) {
12773 error = got_repo_open(&repo,
12774 worktree ? got_worktree_get_repo_path(worktree) : cwd,
12778 error = apply_unveil(got_repo_get_path(repo), 0,
12779 worktree ? got_worktree_get_root_path(worktree) : NULL);
12782 error = process_backup_refs(
12783 GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
12784 argc == 1 ? argv[0] : NULL, delete_backups, repo);
12785 goto done; /* nothing else to do */
12787 error = get_gitconfig_path(&gitconfig_path);
12790 error = got_repo_open(&repo,
12791 got_worktree_get_repo_path(worktree), gitconfig_path,
12795 error = get_editor(&editor);
12798 if (unveil(editor, "x") != 0) {
12799 error = got_error_from_errno2("unveil", editor);
12802 if (edit_script_path) {
12803 if (unveil(edit_script_path, "r") != 0) {
12804 error = got_error_from_errno2("unveil",
12809 error = apply_unveil(got_repo_get_path(repo), 0,
12810 got_worktree_get_root_path(worktree));
12815 if (worktree != NULL && !list_backups && !delete_backups) {
12816 error = worktree_has_logmsg_ref("histedit", worktree, repo);
12821 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
12824 if (rebase_in_progress) {
12825 error = got_error(GOT_ERR_REBASING);
12829 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
12833 if (merge_in_progress) {
12834 error = got_error(GOT_ERR_MERGE_BUSY);
12838 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
12842 if (edit_in_progress && edit_logmsg_only) {
12843 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12844 "histedit operation is in progress in this "
12845 "work tree and must be continued or aborted "
12846 "before the -m option can be used");
12849 if (edit_in_progress && drop_only) {
12850 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12851 "histedit operation is in progress in this "
12852 "work tree and must be continued or aborted "
12853 "before the -d option can be used");
12856 if (edit_in_progress && fold_only) {
12857 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12858 "histedit operation is in progress in this "
12859 "work tree and must be continued or aborted "
12860 "before the -f option can be used");
12863 if (edit_in_progress && edit_only) {
12864 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
12865 "histedit operation is in progress in this "
12866 "work tree and must be continued or aborted "
12867 "before the -e option can be used");
12871 if (edit_in_progress && abort_edit) {
12872 error = got_worktree_histedit_continue(&resume_commit_id,
12873 &tmp_branch, &branch, &base_commit_id, &fileindex,
12877 printf("Switching work tree to %s\n",
12878 got_ref_get_symref_target(branch));
12879 error = got_worktree_histedit_abort(worktree, fileindex, repo,
12880 branch, base_commit_id, abort_progress, &upa);
12883 printf("Histedit of %s aborted\n",
12884 got_ref_get_symref_target(branch));
12885 print_merge_progress_stats(&upa);
12886 goto done; /* nothing else to do */
12887 } else if (abort_edit) {
12888 error = got_error(GOT_ERR_NOT_HISTEDIT);
12892 error = get_author(&committer, repo, worktree);
12896 if (continue_edit) {
12899 if (!edit_in_progress) {
12900 error = got_error(GOT_ERR_NOT_HISTEDIT);
12904 error = got_worktree_get_histedit_script_path(&path, worktree);
12908 error = histedit_load_list(&histedit_cmds, path, repo);
12913 error = got_worktree_histedit_continue(&resume_commit_id,
12914 &tmp_branch, &branch, &base_commit_id, &fileindex,
12919 error = got_ref_resolve(&head_commit_id, repo, branch);
12923 error = got_object_open_as_commit(&commit, repo,
12927 parent_ids = got_object_commit_get_parent_ids(commit);
12928 pid = STAILQ_FIRST(parent_ids);
12930 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12933 error = collect_commits(&commits, head_commit_id, &pid->id,
12934 base_commit_id, got_worktree_get_path_prefix(worktree),
12935 GOT_ERR_HISTEDIT_PATH, repo);
12936 got_object_commit_close(commit);
12941 if (edit_in_progress) {
12942 error = got_error(GOT_ERR_HISTEDIT_BUSY);
12946 error = got_ref_open(&branch, repo,
12947 got_worktree_get_head_ref_name(worktree), 0);
12951 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
12952 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
12953 "will not edit commit history of a branch outside "
12954 "the \"refs/heads/\" reference namespace");
12958 error = got_ref_resolve(&head_commit_id, repo, branch);
12959 got_ref_close(branch);
12964 error = got_object_open_as_commit(&commit, repo,
12968 parent_ids = got_object_commit_get_parent_ids(commit);
12969 pid = STAILQ_FIRST(parent_ids);
12971 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12974 error = collect_commits(&commits, head_commit_id, &pid->id,
12975 got_worktree_get_base_commit_id(worktree),
12976 got_worktree_get_path_prefix(worktree),
12977 GOT_ERR_HISTEDIT_PATH, repo);
12978 got_object_commit_close(commit);
12983 if (STAILQ_EMPTY(&commits)) {
12984 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
12988 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
12989 &base_commit_id, &fileindex, worktree, repo);
12993 if (edit_script_path) {
12994 error = histedit_load_list(&histedit_cmds,
12995 edit_script_path, repo);
12997 got_worktree_histedit_abort(worktree, fileindex,
12998 repo, branch, base_commit_id,
12999 abort_progress, &upa);
13000 print_merge_progress_stats(&upa);
13004 const char *branch_name;
13005 branch_name = got_ref_get_symref_target(branch);
13006 if (strncmp(branch_name, "refs/heads/", 11) == 0)
13008 error = histedit_edit_script(&histedit_cmds, &commits,
13009 branch_name, edit_logmsg_only, fold_only,
13010 drop_only, edit_only, editor, repo);
13012 got_worktree_histedit_abort(worktree, fileindex,
13013 repo, branch, base_commit_id,
13014 abort_progress, &upa);
13015 print_merge_progress_stats(&upa);
13021 error = histedit_save_list(&histedit_cmds, worktree,
13024 got_worktree_histedit_abort(worktree, fileindex,
13025 repo, branch, base_commit_id,
13026 abort_progress, &upa);
13027 print_merge_progress_stats(&upa);
13033 error = histedit_check_script(&histedit_cmds, &commits, repo);
13037 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
13038 if (resume_commit_id) {
13039 if (got_object_id_cmp(hle->commit_id,
13040 resume_commit_id) != 0)
13043 resume_commit_id = NULL;
13044 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
13045 hle->cmd->code == GOT_HISTEDIT_FOLD) {
13046 error = histedit_skip_commit(hle, worktree,
13051 struct got_pathlist_head paths;
13052 int have_changes = 0;
13054 TAILQ_INIT(&paths);
13055 error = got_pathlist_append(&paths, "", NULL);
13058 error = got_worktree_status(worktree, &paths,
13059 repo, 0, check_local_changes, &have_changes,
13060 check_cancelled, NULL);
13061 got_pathlist_free(&paths,
13062 GOT_PATHLIST_FREE_NONE);
13064 if (error->code != GOT_ERR_CANCELLED)
13066 if (sigint_received || sigpipe_received)
13069 if (have_changes) {
13070 error = histedit_commit(NULL, worktree,
13071 fileindex, tmp_branch, hle,
13072 committer, allow_conflict, editor,
13077 error = got_object_open_as_commit(
13078 &commit, repo, hle->commit_id);
13081 error = show_histedit_progress(commit,
13083 got_object_commit_close(commit);
13092 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
13093 error = histedit_skip_commit(hle, worktree, repo);
13098 error = got_object_open_as_commit(&commit, repo,
13102 parent_ids = got_object_commit_get_parent_ids(commit);
13103 pid = STAILQ_FIRST(parent_ids);
13105 error = got_worktree_histedit_merge_files(&merged_paths,
13106 worktree, fileindex, &pid->id, hle->commit_id, repo,
13107 update_progress, &upa, check_cancelled, NULL);
13110 got_object_commit_close(commit);
13113 print_merge_progress_stats(&upa);
13114 if (upa.conflicts > 0 || upa.missing > 0 ||
13115 upa.not_deleted > 0 || upa.unversioned > 0) {
13116 if (upa.conflicts > 0) {
13117 error = show_rebase_merge_conflict(
13118 hle->commit_id, repo);
13122 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13126 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
13128 error = got_object_id_str(&id_str, hle->commit_id);
13131 printf("Stopping histedit for amending commit %s\n",
13134 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13135 error = got_worktree_histedit_postpone(worktree,
13138 } else if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
13139 error = histedit_skip_commit(hle, worktree, repo);
13143 } else if (hle->cmd->code == GOT_HISTEDIT_MESG) {
13144 error = histedit_edit_logmsg(hle, editor, repo);
13149 error = histedit_commit(&merged_paths, worktree, fileindex,
13150 tmp_branch, hle, committer, allow_conflict, editor, repo);
13151 got_pathlist_free(&merged_paths, GOT_PATHLIST_FREE_PATH);
13156 if (upa.conflicts > 0 || upa.missing > 0 ||
13157 upa.not_deleted > 0 || upa.unversioned > 0) {
13158 error = got_worktree_histedit_postpone(worktree, fileindex);
13161 if (upa.conflicts > 0 && upa.missing == 0 &&
13162 upa.not_deleted == 0 && upa.unversioned == 0) {
13163 error = got_error_msg(GOT_ERR_CONFLICTS,
13164 "conflicts must be resolved before histedit "
13166 } else if (upa.conflicts > 0) {
13167 error = got_error_msg(GOT_ERR_CONFLICTS,
13168 "conflicts must be resolved before histedit "
13169 "can continue; changes destined for some "
13170 "files were not yet merged and should be "
13171 "merged manually if required before the "
13172 "histedit operation is continued");
13174 error = got_error_msg(GOT_ERR_CONFLICTS,
13175 "changes destined for some files were not "
13176 "yet merged and should be merged manually "
13177 "if required before the histedit operation "
13181 error = histedit_complete(worktree, fileindex, tmp_branch,
13187 free(gitconfig_path);
13188 got_object_id_queue_free(&commits);
13189 histedit_free_list(&histedit_cmds);
13190 free(head_commit_id);
13191 free(base_commit_id);
13192 free(resume_commit_id);
13194 got_object_commit_close(commit);
13196 got_ref_close(branch);
13198 got_ref_close(tmp_branch);
13200 got_worktree_close(worktree);
13202 const struct got_error *close_err = got_repo_close(repo);
13207 const struct got_error *pack_err =
13208 got_repo_pack_fds_close(pack_fds);
13216 usage_integrate(void)
13218 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
13222 static const struct got_error *
13223 cmd_integrate(int argc, char *argv[])
13225 const struct got_error *error = NULL;
13226 struct got_repository *repo = NULL;
13227 struct got_worktree *worktree = NULL;
13228 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
13229 const char *branch_arg = NULL;
13230 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
13231 struct got_fileindex *fileindex = NULL;
13232 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
13234 struct got_update_progress_arg upa;
13235 int *pack_fds = NULL;
13238 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13239 "unveil", NULL) == -1)
13243 while ((ch = getopt(argc, argv, "")) != -1) {
13256 branch_arg = argv[0];
13258 cwd = getcwd(NULL, 0);
13260 error = got_error_from_errno("getcwd");
13264 error = got_repo_pack_fds_open(&pack_fds);
13268 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13270 if (error->code == GOT_ERR_NOT_WORKTREE)
13271 error = wrap_not_worktree_error(error, "integrate",
13276 error = check_rebase_or_histedit_in_progress(worktree);
13280 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13285 error = apply_unveil(got_repo_get_path(repo), 0,
13286 got_worktree_get_root_path(worktree));
13290 error = check_merge_in_progress(worktree, repo);
13294 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
13295 error = got_error_from_errno("asprintf");
13299 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
13300 &base_branch_ref, worktree, refname, repo);
13304 refname = strdup(got_ref_get_name(branch_ref));
13305 if (refname == NULL) {
13306 error = got_error_from_errno("strdup");
13307 got_worktree_integrate_abort(worktree, fileindex, repo,
13308 branch_ref, base_branch_ref);
13311 base_refname = strdup(got_ref_get_name(base_branch_ref));
13312 if (base_refname == NULL) {
13313 error = got_error_from_errno("strdup");
13314 got_worktree_integrate_abort(worktree, fileindex, repo,
13315 branch_ref, base_branch_ref);
13318 if (strncmp(base_refname, "refs/heads/", 11) != 0) {
13319 error = got_error(GOT_ERR_INTEGRATE_BRANCH);
13320 got_worktree_integrate_abort(worktree, fileindex, repo,
13321 branch_ref, base_branch_ref);
13325 error = got_ref_resolve(&commit_id, repo, branch_ref);
13329 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
13333 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
13334 error = got_error_msg(GOT_ERR_SAME_BRANCH,
13335 "specified branch has already been integrated");
13336 got_worktree_integrate_abort(worktree, fileindex, repo,
13337 branch_ref, base_branch_ref);
13341 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
13343 if (error->code == GOT_ERR_ANCESTRY)
13344 error = got_error(GOT_ERR_REBASE_REQUIRED);
13345 got_worktree_integrate_abort(worktree, fileindex, repo,
13346 branch_ref, base_branch_ref);
13350 memset(&upa, 0, sizeof(upa));
13351 error = got_worktree_integrate_continue(worktree, fileindex, repo,
13352 branch_ref, base_branch_ref, update_progress, &upa,
13353 check_cancelled, NULL);
13357 printf("Integrated %s into %s\n", refname, base_refname);
13358 print_update_progress_stats(&upa);
13361 const struct got_error *close_err = got_repo_close(repo);
13366 got_worktree_close(worktree);
13368 const struct got_error *pack_err =
13369 got_repo_pack_fds_close(pack_fds);
13374 free(base_commit_id);
13377 free(base_refname);
13384 fprintf(stderr, "usage: %s merge [-aCcn] [branch]\n", getprogname());
13388 static const struct got_error *
13389 cmd_merge(int argc, char *argv[])
13391 const struct got_error *error = NULL;
13392 struct got_worktree *worktree = NULL;
13393 struct got_repository *repo = NULL;
13394 struct got_fileindex *fileindex = NULL;
13395 char *cwd = NULL, *id_str = NULL, *author = NULL;
13396 char *gitconfig_path = NULL;
13397 struct got_reference *branch = NULL, *wt_branch = NULL;
13398 struct got_object_id *branch_tip = NULL, *yca_id = NULL;
13399 struct got_object_id *wt_branch_tip = NULL;
13400 int ch, merge_in_progress = 0, abort_merge = 0, continue_merge = 0;
13401 int allow_conflict = 0, prefer_fast_forward = 1, interrupt_merge = 0;
13402 struct got_update_progress_arg upa;
13403 struct got_object_id *merge_commit_id = NULL;
13404 char *branch_name = NULL;
13405 int *pack_fds = NULL;
13407 memset(&upa, 0, sizeof(upa));
13410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13411 "unveil", NULL) == -1)
13415 while ((ch = getopt(argc, argv, "aCcMn")) != -1) {
13421 allow_conflict = 1;
13424 continue_merge = 1;
13427 prefer_fast_forward = 0;
13430 interrupt_merge = 1;
13442 if (continue_merge)
13443 option_conflict('a', 'c');
13444 if (!prefer_fast_forward)
13445 option_conflict('a', 'M');
13446 if (interrupt_merge)
13447 option_conflict('a', 'n');
13448 } else if (continue_merge) {
13449 if (!prefer_fast_forward)
13450 option_conflict('c', 'M');
13451 if (interrupt_merge)
13452 option_conflict('c', 'n');
13454 if (allow_conflict) {
13455 if (!continue_merge)
13456 errx(1, "-C option requires -c");
13458 if (abort_merge || continue_merge) {
13461 } else if (argc != 1)
13464 cwd = getcwd(NULL, 0);
13466 error = got_error_from_errno("getcwd");
13470 error = got_repo_pack_fds_open(&pack_fds);
13474 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13476 if (error->code == GOT_ERR_NOT_WORKTREE)
13477 error = wrap_not_worktree_error(error,
13482 error = get_gitconfig_path(&gitconfig_path);
13485 error = got_repo_open(&repo,
13486 worktree ? got_worktree_get_repo_path(worktree) : cwd,
13487 gitconfig_path, pack_fds);
13491 if (worktree != NULL) {
13492 error = worktree_has_logmsg_ref("merge", worktree, repo);
13497 error = apply_unveil(got_repo_get_path(repo), 0,
13498 worktree ? got_worktree_get_root_path(worktree) : NULL);
13502 error = check_rebase_or_histedit_in_progress(worktree);
13506 error = got_worktree_merge_in_progress(&merge_in_progress, worktree,
13511 if (merge_in_progress && !(abort_merge || continue_merge)) {
13512 error = got_error(GOT_ERR_MERGE_BUSY);
13516 if (!merge_in_progress && (abort_merge || continue_merge)) {
13517 error = got_error(GOT_ERR_NOT_MERGING);
13522 error = got_worktree_merge_continue(&branch_name,
13523 &branch_tip, &fileindex, worktree, repo);
13526 error = got_worktree_merge_abort(worktree, fileindex, repo,
13527 abort_progress, &upa);
13530 printf("Merge of %s aborted\n", branch_name);
13531 goto done; /* nothing else to do */
13534 if (strncmp(got_worktree_get_head_ref_name(worktree),
13535 "refs/heads/", 11) != 0) {
13536 error = got_error_fmt(GOT_ERR_COMMIT_BRANCH,
13537 "work tree's current branch %s is outside the "
13538 "\"refs/heads/\" reference namespace; "
13539 "update -b required",
13540 got_worktree_get_head_ref_name(worktree));
13544 error = get_author(&author, repo, worktree);
13548 error = got_ref_open(&wt_branch, repo,
13549 got_worktree_get_head_ref_name(worktree), 0);
13552 error = got_ref_resolve(&wt_branch_tip, repo, wt_branch);
13556 if (continue_merge) {
13557 struct got_object_id *base_commit_id;
13558 base_commit_id = got_worktree_get_base_commit_id(worktree);
13559 if (got_object_id_cmp(wt_branch_tip, base_commit_id) != 0) {
13560 error = got_error(GOT_ERR_MERGE_COMMIT_OUT_OF_DATE);
13563 error = got_worktree_merge_continue(&branch_name,
13564 &branch_tip, &fileindex, worktree, repo);
13568 error = got_ref_open(&branch, repo, argv[0], 0);
13571 branch_name = strdup(got_ref_get_name(branch));
13572 if (branch_name == NULL) {
13573 error = got_error_from_errno("strdup");
13576 error = got_ref_resolve(&branch_tip, repo, branch);
13581 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
13582 wt_branch_tip, branch_tip, 0, 0, repo,
13583 check_cancelled, NULL);
13584 if (error && error->code != GOT_ERR_ANCESTRY)
13587 if (!continue_merge) {
13588 error = check_path_prefix(wt_branch_tip, branch_tip,
13589 got_worktree_get_path_prefix(worktree),
13590 GOT_ERR_MERGE_PATH, repo);
13593 error = got_worktree_merge_prepare(&fileindex, worktree, repo);
13596 if (prefer_fast_forward && yca_id &&
13597 got_object_id_cmp(wt_branch_tip, yca_id) == 0) {
13598 struct got_pathlist_head paths;
13599 if (interrupt_merge) {
13600 error = got_error_fmt(GOT_ERR_BAD_OPTION,
13601 "there are no changes to merge since %s "
13602 "is already based on %s; merge cannot be "
13603 "interrupted for amending; -n",
13604 branch_name, got_ref_get_name(wt_branch));
13607 printf("Forwarding %s to %s\n",
13608 got_ref_get_name(wt_branch), branch_name);
13609 error = got_ref_change_ref(wt_branch, branch_tip);
13612 error = got_ref_write(wt_branch, repo);
13615 error = got_worktree_set_base_commit_id(worktree, repo,
13619 TAILQ_INIT(&paths);
13620 error = got_pathlist_append(&paths, "", NULL);
13623 error = got_worktree_checkout_files(worktree,
13624 &paths, repo, update_progress, &upa,
13625 check_cancelled, NULL);
13626 got_pathlist_free(&paths, GOT_PATHLIST_FREE_NONE);
13629 if (upa.did_something) {
13631 error = got_object_id_str(&id_str, branch_tip);
13634 printf("Updated to commit %s\n", id_str);
13637 printf("Already up-to-date\n");
13638 print_update_progress_stats(&upa);
13641 error = got_worktree_merge_write_refs(worktree, branch, repo);
13645 error = got_worktree_merge_branch(worktree, fileindex,
13646 yca_id, branch_tip, repo, update_progress, &upa,
13647 check_cancelled, NULL);
13650 print_merge_progress_stats(&upa);
13651 if (!upa.did_something) {
13652 error = got_worktree_merge_abort(worktree, fileindex,
13653 repo, abort_progress, &upa);
13656 printf("Already up-to-date\n");
13661 if (interrupt_merge) {
13662 error = got_worktree_merge_postpone(worktree, fileindex);
13665 printf("Merge of %s interrupted on request\n", branch_name);
13666 } else if (upa.conflicts > 0 || upa.missing > 0 ||
13667 upa.not_deleted > 0 || upa.unversioned > 0) {
13668 error = got_worktree_merge_postpone(worktree, fileindex);
13671 if (upa.conflicts > 0 && upa.missing == 0 &&
13672 upa.not_deleted == 0 && upa.unversioned == 0) {
13673 error = got_error_msg(GOT_ERR_CONFLICTS,
13674 "conflicts must be resolved before merging "
13676 } else if (upa.conflicts > 0) {
13677 error = got_error_msg(GOT_ERR_CONFLICTS,
13678 "conflicts must be resolved before merging "
13679 "can continue; changes destined for some "
13680 "files were not yet merged and "
13681 "should be merged manually if required before the "
13682 "merge operation is continued");
13684 error = got_error_msg(GOT_ERR_CONFLICTS,
13685 "changes destined for some "
13686 "files were not yet merged and should be "
13687 "merged manually if required before the "
13688 "merge operation is continued");
13692 error = got_worktree_merge_commit(&merge_commit_id, worktree,
13693 fileindex, author, NULL, 1, branch_tip, branch_name,
13694 allow_conflict, repo, continue_merge ? print_status : NULL,
13698 error = got_worktree_merge_complete(worktree, fileindex, repo);
13701 error = got_object_id_str(&id_str, merge_commit_id);
13704 printf("Merged %s into %s: %s\n", branch_name,
13705 got_worktree_get_head_ref_name(worktree),
13710 free(gitconfig_path);
13712 free(merge_commit_id);
13718 got_ref_close(branch);
13720 got_ref_close(wt_branch);
13722 got_worktree_close(worktree);
13724 const struct got_error *close_err = got_repo_close(repo);
13729 const struct got_error *pack_err =
13730 got_repo_pack_fds_close(pack_fds);
13740 fprintf(stderr, "usage: %s stage [-lpS] [-F response-script] "
13741 "[path ...]\n", getprogname());
13745 static const struct got_error *
13746 print_stage(void *arg, unsigned char status, unsigned char staged_status,
13747 const char *path, struct got_object_id *blob_id,
13748 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
13749 int dirfd, const char *de_name)
13751 const struct got_error *err = NULL;
13752 char *id_str = NULL;
13754 if (staged_status != GOT_STATUS_ADD &&
13755 staged_status != GOT_STATUS_MODIFY &&
13756 staged_status != GOT_STATUS_DELETE)
13759 if (staged_status == GOT_STATUS_ADD ||
13760 staged_status == GOT_STATUS_MODIFY)
13761 err = got_object_id_str(&id_str, staged_blob_id);
13763 err = got_object_id_str(&id_str, blob_id);
13767 printf("%s %c %s\n", id_str, staged_status, path);
13772 static const struct got_error *
13773 cmd_stage(int argc, char *argv[])
13775 const struct got_error *error = NULL;
13776 struct got_repository *repo = NULL;
13777 struct got_worktree *worktree = NULL;
13779 struct got_pathlist_head paths;
13780 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
13781 FILE *patch_script_file = NULL;
13782 const char *patch_script_path = NULL;
13783 struct choose_patch_arg cpa;
13784 int *pack_fds = NULL;
13786 TAILQ_INIT(&paths);
13789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13790 "unveil", NULL) == -1)
13794 while ((ch = getopt(argc, argv, "F:lpS")) != -1) {
13797 patch_script_path = optarg;
13806 allow_bad_symlinks = 1;
13817 if (list_stage && (pflag || patch_script_path))
13818 errx(1, "-l option cannot be used with other options");
13819 if (patch_script_path && !pflag)
13820 errx(1, "-F option can only be used together with -p option");
13822 cwd = getcwd(NULL, 0);
13824 error = got_error_from_errno("getcwd");
13828 error = got_repo_pack_fds_open(&pack_fds);
13832 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13834 if (error->code == GOT_ERR_NOT_WORKTREE)
13835 error = wrap_not_worktree_error(error, "stage", cwd);
13839 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13844 if (patch_script_path) {
13845 patch_script_file = fopen(patch_script_path, "re");
13846 if (patch_script_file == NULL) {
13847 error = got_error_from_errno2("fopen",
13848 patch_script_path);
13852 error = apply_unveil(got_repo_get_path(repo), 0,
13853 got_worktree_get_root_path(worktree));
13857 error = check_merge_in_progress(worktree, repo);
13861 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13866 error = got_worktree_status(worktree, &paths, repo, 0,
13867 print_stage, NULL, check_cancelled, NULL);
13869 cpa.patch_script_file = patch_script_file;
13870 cpa.action = "stage";
13871 error = got_worktree_stage(worktree, &paths,
13872 pflag ? NULL : print_status, NULL,
13873 pflag ? choose_patch : NULL, &cpa,
13874 allow_bad_symlinks, repo);
13877 if (patch_script_file && fclose(patch_script_file) == EOF &&
13879 error = got_error_from_errno2("fclose", patch_script_path);
13881 const struct got_error *close_err = got_repo_close(repo);
13886 got_worktree_close(worktree);
13888 const struct got_error *pack_err =
13889 got_repo_pack_fds_close(pack_fds);
13893 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
13899 usage_unstage(void)
13901 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
13902 "[path ...]\n", getprogname());
13907 static const struct got_error *
13908 cmd_unstage(int argc, char *argv[])
13910 const struct got_error *error = NULL;
13911 struct got_repository *repo = NULL;
13912 struct got_worktree *worktree = NULL;
13914 struct got_pathlist_head paths;
13916 struct got_update_progress_arg upa;
13917 FILE *patch_script_file = NULL;
13918 const char *patch_script_path = NULL;
13919 struct choose_patch_arg cpa;
13920 int *pack_fds = NULL;
13922 TAILQ_INIT(&paths);
13925 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
13926 "unveil", NULL) == -1)
13930 while ((ch = getopt(argc, argv, "F:p")) != -1) {
13933 patch_script_path = optarg;
13947 if (patch_script_path && !pflag)
13948 errx(1, "-F option can only be used together with -p option");
13950 cwd = getcwd(NULL, 0);
13952 error = got_error_from_errno("getcwd");
13956 error = got_repo_pack_fds_open(&pack_fds);
13960 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
13962 if (error->code == GOT_ERR_NOT_WORKTREE)
13963 error = wrap_not_worktree_error(error, "unstage", cwd);
13967 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
13972 if (patch_script_path) {
13973 patch_script_file = fopen(patch_script_path, "re");
13974 if (patch_script_file == NULL) {
13975 error = got_error_from_errno2("fopen",
13976 patch_script_path);
13981 error = apply_unveil(got_repo_get_path(repo), 0,
13982 got_worktree_get_root_path(worktree));
13986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
13990 cpa.patch_script_file = patch_script_file;
13991 cpa.action = "unstage";
13992 memset(&upa, 0, sizeof(upa));
13993 error = got_worktree_unstage(worktree, &paths, update_progress,
13994 &upa, pflag ? choose_patch : NULL, &cpa, repo);
13996 print_merge_progress_stats(&upa);
13998 if (patch_script_file && fclose(patch_script_file) == EOF &&
14000 error = got_error_from_errno2("fclose", patch_script_path);
14002 const struct got_error *close_err = got_repo_close(repo);
14007 got_worktree_close(worktree);
14009 const struct got_error *pack_err =
14010 got_repo_pack_fds_close(pack_fds);
14014 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);
14022 fprintf(stderr, "usage: %s cat [-P] [-c commit] [-r repository-path] "
14023 "arg ...\n", getprogname());
14027 static const struct got_error *
14028 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14030 const struct got_error *err;
14031 struct got_blob_object *blob;
14034 fd = got_opentempfd();
14036 return got_error_from_errno("got_opentempfd");
14038 err = got_object_open_as_blob(&blob, repo, id, 8192, fd);
14042 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
14044 if (fd != -1 && close(fd) == -1 && err == NULL)
14045 err = got_error_from_errno("close");
14047 got_object_blob_close(blob);
14051 static const struct got_error *
14052 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14054 const struct got_error *err;
14055 struct got_tree_object *tree;
14058 err = got_object_open_as_tree(&tree, repo, id);
14062 nentries = got_object_tree_get_nentries(tree);
14063 for (i = 0; i < nentries; i++) {
14064 struct got_tree_entry *te;
14066 if (sigint_received || sigpipe_received)
14068 te = got_object_tree_get_entry(tree, i);
14069 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
14072 fprintf(outfile, "%s %.7o %s\n", id_str,
14073 got_tree_entry_get_mode(te),
14074 got_tree_entry_get_name(te));
14078 got_object_tree_close(tree);
14082 static const struct got_error *
14083 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14085 const struct got_error *err;
14086 struct got_commit_object *commit;
14087 const struct got_object_id_queue *parent_ids;
14088 struct got_object_qid *pid;
14089 char *id_str = NULL;
14090 const char *logmsg = NULL;
14093 err = got_object_open_as_commit(&commit, repo, id);
14097 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
14101 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
14102 parent_ids = got_object_commit_get_parent_ids(commit);
14103 fprintf(outfile, "numparents %d\n",
14104 got_object_commit_get_nparents(commit));
14105 STAILQ_FOREACH(pid, parent_ids, entry) {
14107 err = got_object_id_str(&pid_str, &pid->id);
14110 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
14113 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14114 got_object_commit_get_author_gmtoff(commit));
14115 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_AUTHOR,
14116 got_object_commit_get_author(commit),
14117 (long long)got_object_commit_get_author_time(commit),
14120 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14121 got_object_commit_get_committer_gmtoff(commit));
14122 fprintf(outfile, "%s%s %lld %s\n", GOT_COMMIT_LABEL_COMMITTER,
14123 got_object_commit_get_committer(commit),
14124 (long long)got_object_commit_get_committer_time(commit),
14127 logmsg = got_object_commit_get_logmsg_raw(commit);
14128 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
14129 fprintf(outfile, "%s", logmsg);
14132 got_object_commit_close(commit);
14136 static const struct got_error *
14137 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
14139 const struct got_error *err;
14140 struct got_tag_object *tag;
14141 char *id_str = NULL;
14142 const char *tagmsg = NULL;
14145 err = got_object_open_as_tag(&tag, repo, id);
14149 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
14153 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
14155 switch (got_object_tag_get_object_type(tag)) {
14156 case GOT_OBJ_TYPE_BLOB:
14157 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14158 GOT_OBJ_LABEL_BLOB);
14160 case GOT_OBJ_TYPE_TREE:
14161 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14162 GOT_OBJ_LABEL_TREE);
14164 case GOT_OBJ_TYPE_COMMIT:
14165 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14166 GOT_OBJ_LABEL_COMMIT);
14168 case GOT_OBJ_TYPE_TAG:
14169 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
14170 GOT_OBJ_LABEL_TAG);
14176 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
14177 got_object_tag_get_name(tag));
14179 got_date_format_gmtoff(gmtoff, sizeof(gmtoff),
14180 got_object_tag_get_tagger_gmtoff(tag));
14181 fprintf(outfile, "%s%s %lld %s\n", GOT_TAG_LABEL_TAGGER,
14182 got_object_tag_get_tagger(tag),
14183 (long long)got_object_tag_get_tagger_time(tag),
14186 tagmsg = got_object_tag_get_message(tag);
14187 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
14188 fprintf(outfile, "%s", tagmsg);
14191 got_object_tag_close(tag);
14195 static const struct got_error *
14196 cmd_cat(int argc, char *argv[])
14198 const struct got_error *error;
14199 struct got_repository *repo = NULL;
14200 struct got_worktree *worktree = NULL;
14201 char *cwd = NULL, *repo_path = NULL, *label = NULL;
14202 char *keyword_idstr = NULL;
14203 const char *commit_id_str = NULL;
14204 struct got_object_id *id = NULL, *commit_id = NULL;
14205 struct got_commit_object *commit = NULL;
14206 int ch, obj_type, i, force_path = 0;
14207 struct got_reflist_head refs;
14208 int *pack_fds = NULL;
14213 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14218 while ((ch = getopt(argc, argv, "c:Pr:")) != -1) {
14221 commit_id_str = optarg;
14227 repo_path = realpath(optarg, NULL);
14228 if (repo_path == NULL)
14229 return got_error_from_errno2("realpath",
14231 got_path_strip_trailing_slashes(repo_path);
14242 cwd = getcwd(NULL, 0);
14244 error = got_error_from_errno("getcwd");
14248 error = got_repo_pack_fds_open(&pack_fds);
14252 if (repo_path == NULL) {
14253 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14254 if (error && error->code != GOT_ERR_NOT_WORKTREE)
14257 repo_path = strdup(
14258 got_worktree_get_repo_path(worktree));
14259 if (repo_path == NULL) {
14260 error = got_error_from_errno("strdup");
14264 if (commit_id_str == NULL) {
14265 /* Release work tree lock. */
14266 got_worktree_close(worktree);
14272 if (repo_path == NULL) {
14273 repo_path = strdup(cwd);
14274 if (repo_path == NULL)
14275 return got_error_from_errno("strdup");
14278 error = got_repo_open(&repo, repo_path, NULL, pack_fds);
14283 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
14287 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
14291 if (commit_id_str != NULL) {
14292 error = got_keyword_to_idstr(&keyword_idstr, commit_id_str,
14296 if (keyword_idstr != NULL)
14297 commit_id_str = keyword_idstr;
14298 if (worktree != NULL) {
14299 got_worktree_close(worktree);
14303 commit_id_str = GOT_REF_HEAD;
14304 error = got_repo_match_object_id(&commit_id, NULL,
14305 commit_id_str, GOT_OBJ_TYPE_COMMIT, &refs, repo);
14309 error = got_object_open_as_commit(&commit, repo, commit_id);
14313 for (i = 0; i < argc; i++) {
14315 error = got_object_id_by_path(&id, repo, commit,
14320 error = got_repo_match_object_id(&id, &label, argv[i],
14321 GOT_OBJ_TYPE_ANY, NULL /* do not resolve tags */,
14324 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
14325 error->code != GOT_ERR_NOT_REF)
14327 error = got_object_id_by_path(&id, repo,
14334 error = got_object_get_type(&obj_type, repo, id);
14338 switch (obj_type) {
14339 case GOT_OBJ_TYPE_BLOB:
14340 error = cat_blob(id, repo, stdout);
14342 case GOT_OBJ_TYPE_TREE:
14343 error = cat_tree(id, repo, stdout);
14345 case GOT_OBJ_TYPE_COMMIT:
14346 error = cat_commit(id, repo, stdout);
14348 case GOT_OBJ_TYPE_TAG:
14349 error = cat_tag(id, repo, stdout);
14352 error = got_error(GOT_ERR_OBJ_TYPE);
14366 free(keyword_idstr);
14368 got_object_commit_close(commit);
14370 got_worktree_close(worktree);
14372 const struct got_error *close_err = got_repo_close(repo);
14377 const struct got_error *pack_err =
14378 got_repo_pack_fds_close(pack_fds);
14383 got_ref_list_free(&refs);
14390 fprintf(stderr, "usage: %s info [path ...]\n",
14395 static const struct got_error *
14396 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
14397 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
14398 struct got_object_id *commit_id)
14400 const struct got_error *err = NULL;
14401 char *id_str = NULL;
14403 struct tm mytm, *tm;
14404 struct got_pathlist_head *paths = arg;
14405 struct got_pathlist_entry *pe;
14408 * Clear error indication from any of the path arguments which
14409 * would cause this file index entry to be displayed.
14411 TAILQ_FOREACH(pe, paths, entry) {
14412 if (got_path_cmp(path, pe->path, strlen(path),
14413 pe->path_len) == 0 ||
14414 got_path_is_child(path, pe->path, pe->path_len))
14415 pe->data = NULL; /* no error */
14418 printf(GOT_COMMIT_SEP_STR);
14420 printf("symlink: %s\n", path);
14421 else if (S_ISREG(mode)) {
14422 printf("file: %s\n", path);
14423 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
14424 } else if (S_ISDIR(mode))
14425 printf("directory: %s\n", path);
14427 printf("something: %s\n", path);
14429 tm = localtime_r(&mtime, &mytm);
14432 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) == 0)
14433 return got_error(GOT_ERR_NO_SPACE);
14434 printf("timestamp: %s\n", datebuf);
14437 err = got_object_id_str(&id_str, blob_id);
14440 printf("based on blob: %s\n", id_str);
14444 if (staged_blob_id) {
14445 err = got_object_id_str(&id_str, staged_blob_id);
14448 printf("based on staged blob: %s\n", id_str);
14453 err = got_object_id_str(&id_str, commit_id);
14456 printf("based on commit: %s\n", id_str);
14463 static const struct got_error *
14464 cmd_info(int argc, char *argv[])
14466 const struct got_error *error = NULL;
14467 struct got_worktree *worktree = NULL;
14468 char *cwd = NULL, *id_str = NULL;
14469 struct got_pathlist_head paths;
14470 char *uuidstr = NULL;
14471 int ch, show_files = 0;
14473 TAILQ_INIT(&paths);
14476 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
14481 while ((ch = getopt(argc, argv, "")) != -1) {
14492 cwd = getcwd(NULL, 0);
14494 error = got_error_from_errno("getcwd");
14498 error = got_worktree_open(&worktree, cwd, GOT_WORKTREE_GOT_DIR);
14500 if (error->code == GOT_ERR_NOT_WORKTREE)
14501 error = wrap_not_worktree_error(error, "info", cwd);
14506 /* Remove "wpath cpath proc exec sendfd" promises. */
14507 if (pledge("stdio rpath flock unveil", NULL) == -1)
14510 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
14515 error = get_worktree_paths_from_argv(&paths, argc, argv,
14522 error = got_object_id_str(&id_str,
14523 got_worktree_get_base_commit_id(worktree));
14527 error = got_worktree_get_uuid(&uuidstr, worktree);
14531 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
14532 printf("work tree base commit: %s\n", id_str);
14533 printf("work tree path prefix: %s\n",
14534 got_worktree_get_path_prefix(worktree));
14535 printf("work tree branch reference: %s\n",
14536 got_worktree_get_head_ref_name(worktree));
14537 printf("work tree UUID: %s\n", uuidstr);
14538 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
14541 struct got_pathlist_entry *pe;
14542 TAILQ_FOREACH(pe, &paths, entry) {
14543 if (pe->path_len == 0)
14546 * Assume this path will fail. This will be corrected
14547 * in print_path_info() in case the path does suceeed.
14549 pe->data = (void *)got_error(GOT_ERR_BAD_PATH);
14551 error = got_worktree_path_info(worktree, &paths,
14552 print_path_info, &paths, check_cancelled, NULL);
14555 TAILQ_FOREACH(pe, &paths, entry) {
14556 if (pe->data != NULL) {
14557 const struct got_error *perr;
14560 error = got_error_fmt(perr->code, "%s",
14568 got_worktree_close(worktree);
14569 got_pathlist_free(&paths, GOT_PATHLIST_FREE_PATH);