2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
63 catch_sigint(int signo)
69 catch_sigpipe(int signo)
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
174 main(int argc, char *argv[])
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "hV", longopts, NULL)) != -1) {
206 got_version_print_str();
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
265 editor = getenv("VISUAL");
267 editor = getenv("EDITOR");
270 err = got_path_find_prog(abspath, editor);
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
327 while ((ch = getopt(argc, argv, "")) != -1) {
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
356 error = apply_unveil(repo_path, 0, NULL);
360 error = got_repo_init(repo_path);
372 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
373 "[-r repository-path] [-I pattern] path\n", getprogname());
378 spawn_editor(const char *editor, const char *file)
381 sig_t sighup, sigint, sigquit;
384 sighup = signal(SIGHUP, SIG_IGN);
385 sigint = signal(SIGINT, SIG_IGN);
386 sigquit = signal(SIGQUIT, SIG_IGN);
388 switch (pid = fork()) {
392 execl(editor, editor, file, (char *)NULL);
396 while (waitpid(pid, &st, 0) == -1)
401 (void)signal(SIGHUP, sighup);
402 (void)signal(SIGINT, sigint);
403 (void)signal(SIGQUIT, sigquit);
405 if (!WIFEXITED(st)) {
410 return WEXITSTATUS(st);
413 static const struct got_error *
414 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
415 const char *initial_content)
417 const struct got_error *err = NULL;
421 int content_changed = 0;
426 if (stat(logmsg_path, &st) == -1)
427 return got_error_from_errno2("stat", logmsg_path);
429 if (spawn_editor(editor, logmsg_path) == -1)
430 return got_error_from_errno("failed spawning editor");
432 if (stat(logmsg_path, &st2) == -1)
433 return got_error_from_errno("stat");
435 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
436 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "no changes made to commit message, aborting");
439 *logmsg = malloc(st2.st_size + 1);
441 return got_error_from_errno("malloc");
445 fp = fopen(logmsg_path, "r");
447 err = got_error_from_errno("fopen");
450 while (fgets(buf, sizeof(buf), fp) != NULL) {
451 if (!content_changed && strcmp(buf, initial_content) != 0)
453 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
454 continue; /* remove comments and leading empty lines */
455 len = strlcat(*logmsg, buf, st2.st_size);
459 while (len > 0 && (*logmsg)[len - 1] == '\n') {
460 (*logmsg)[len - 1] = '\0';
464 if (len == 0 || !content_changed)
465 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
466 "commit message cannot be empty, aborting");
475 static const struct got_error *
476 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
477 const char *path_dir, const char *branch_name)
479 char *initial_content = NULL;
480 const struct got_error *err = NULL;
483 if (asprintf(&initial_content,
484 "\n# %s to be imported to branch %s\n", path_dir,
486 return got_error_from_errno("asprintf");
488 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
492 dprintf(fd, initial_content);
495 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
497 free(initial_content);
501 static const struct got_error *
502 import_progress(void *arg, const char *path)
504 printf("A %s\n", path);
508 static const struct got_error *
509 get_author(char **author, struct got_repository *repo)
511 const struct got_error *err = NULL;
512 const char *got_author, *name, *email;
516 name = got_repo_get_gitconfig_author_name(repo);
517 email = got_repo_get_gitconfig_author_email(repo);
519 if (asprintf(author, "%s <%s>", name, email) == -1)
520 return got_error_from_errno("asprintf");
524 got_author = getenv("GOT_AUTHOR");
525 if (got_author == NULL) {
526 name = got_repo_get_global_gitconfig_author_name(repo);
527 email = got_repo_get_global_gitconfig_author_email(repo);
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
533 /* TODO: Look up user in password database? */
534 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 *author = strdup(got_author);
539 return got_error_from_errno("strdup");
542 * Really dumb email address check; we're only doing this to
543 * avoid git's object parser breaking on commits we create.
545 while (*got_author && *got_author != '<')
547 if (*got_author != '<') {
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
551 while (*got_author && *got_author != '@')
553 if (*got_author != '@') {
554 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
557 while (*got_author && *got_author != '>')
559 if (*got_author != '>')
560 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
569 static const struct got_error *
570 get_gitconfig_path(char **gitconfig_path)
572 const char *homedir = getenv("HOME");
574 *gitconfig_path = NULL;
576 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
577 return got_error_from_errno("asprintf");
583 static const struct got_error *
584 cmd_import(int argc, char *argv[])
586 const struct got_error *error = NULL;
587 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
588 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
589 const char *branch_name = "main";
590 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
591 struct got_repository *repo = NULL;
592 struct got_reference *branch_ref = NULL, *head_ref = NULL;
593 struct got_object_id *new_commit_id = NULL;
595 struct got_pathlist_head ignores;
596 struct got_pathlist_entry *pe;
597 int preserve_logmsg = 0;
599 TAILQ_INIT(&ignores);
601 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
604 branch_name = optarg;
607 logmsg = strdup(optarg);
608 if (logmsg == NULL) {
609 error = got_error_from_errno("strdup");
614 repo_path = realpath(optarg, NULL);
615 if (repo_path == NULL) {
616 error = got_error_from_errno2("realpath",
622 if (optarg[0] == '\0')
624 error = got_pathlist_insert(&pe, &ignores, optarg,
639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 if (repo_path == NULL) {
648 repo_path = getcwd(NULL, 0);
649 if (repo_path == NULL)
650 return got_error_from_errno("getcwd");
652 got_path_strip_trailing_slashes(repo_path);
653 error = get_gitconfig_path(&gitconfig_path);
656 error = got_repo_open(&repo, repo_path, gitconfig_path);
660 error = get_author(&author, repo);
665 * Don't let the user create a branch name with a leading '-'.
666 * While technically a valid reference name, this case is usually
667 * an unintended typo.
669 if (branch_name[0] == '-')
670 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
672 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
673 error = got_error_from_errno("asprintf");
677 error = got_ref_open(&branch_ref, repo, refname, 0);
679 if (error->code != GOT_ERR_NOT_REF)
682 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
683 "import target branch already exists");
687 path_dir = realpath(argv[0], NULL);
688 if (path_dir == NULL) {
689 error = got_error_from_errno2("realpath", argv[0]);
692 got_path_strip_trailing_slashes(path_dir);
695 * unveil(2) traverses exec(2); if an editor is used we have
696 * to apply unveil after the log message has been written.
698 if (logmsg == NULL || strlen(logmsg) == 0) {
699 error = get_editor(&editor);
703 error = collect_import_msg(&logmsg, &logmsg_path, editor,
706 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
713 if (unveil(path_dir, "r") != 0) {
714 error = got_error_from_errno2("unveil", path_dir);
720 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
727 error = got_repo_import(&new_commit_id, path_dir, logmsg,
728 author, &ignores, repo, import_progress, NULL);
735 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
742 error = got_ref_write(branch_ref, repo);
749 error = got_object_id_str(&id_str, new_commit_id);
756 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
758 if (error->code != GOT_ERR_NOT_REF) {
764 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 error = got_ref_write(head_ref, repo);
780 printf("Created branch %s with commit %s\n",
781 got_ref_get_name(branch_ref), id_str);
783 if (preserve_logmsg) {
784 fprintf(stderr, "%s: log message preserved in %s\n",
785 getprogname(), logmsg_path);
786 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
787 error = got_error_from_errno2("unlink", logmsg_path);
796 free(gitconfig_path);
798 got_ref_close(branch_ref);
800 got_ref_close(head_ref);
807 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
808 "[-p prefix] repository-path [worktree-path]\n", getprogname());
813 show_worktree_base_ref_warning(void)
815 fprintf(stderr, "%s: warning: could not create a reference "
816 "to the work tree's base commit; the commit could be "
817 "garbage-collected by Git; making the repository "
818 "writable and running 'got update' will prevent this\n",
822 struct got_checkout_progress_arg {
823 const char *worktree_path;
824 int had_base_commit_ref_error;
827 static const struct got_error *
828 checkout_progress(void *arg, unsigned char status, const char *path)
830 struct got_checkout_progress_arg *a = arg;
832 /* Base commit bump happens silently. */
833 if (status == GOT_STATUS_BUMP_BASE)
836 if (status == GOT_STATUS_BASE_REF_ERR) {
837 a->had_base_commit_ref_error = 1;
841 while (path[0] == '/')
844 printf("%c %s/%s\n", status, a->worktree_path, path);
848 static const struct got_error *
849 check_cancelled(void *arg)
851 if (sigint_received || sigpipe_received)
852 return got_error(GOT_ERR_CANCELLED);
856 static const struct got_error *
857 check_linear_ancestry(struct got_object_id *commit_id,
858 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
859 struct got_repository *repo)
861 const struct got_error *err = NULL;
862 struct got_object_id *yca_id;
864 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
865 commit_id, base_commit_id, repo, check_cancelled, NULL);
870 return got_error(GOT_ERR_ANCESTRY);
873 * Require a straight line of history between the target commit
874 * and the work tree's base commit.
876 * Non-linear situations such as this require a rebase:
878 * (commit) D F (base_commit)
886 * 'got update' only handles linear cases:
887 * Update forwards in time: A (base/yca) - B - C - D (commit)
888 * Update backwards in time: D (base) - C - B - A (commit/yca)
890 if (allow_forwards_in_time_only) {
891 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
893 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
894 got_object_id_cmp(base_commit_id, yca_id) != 0)
895 return got_error(GOT_ERR_ANCESTRY);
901 static const struct got_error *
902 check_same_branch(struct got_object_id *commit_id,
903 struct got_reference *head_ref, struct got_object_id *yca_id,
904 struct got_repository *repo)
906 const struct got_error *err = NULL;
907 struct got_commit_graph *graph = NULL;
908 struct got_object_id *head_commit_id = NULL;
909 int is_same_branch = 0;
911 err = got_ref_resolve(&head_commit_id, repo, head_ref);
915 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
919 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
924 err = got_commit_graph_open(&graph, "/", 1);
928 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
929 check_cancelled, NULL);
934 struct got_object_id *id;
935 err = got_commit_graph_iter_next(&id, graph, repo,
936 check_cancelled, NULL);
938 if (err->code == GOT_ERR_ITER_COMPLETED)
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
946 if (got_object_id_cmp(id, commit_id) == 0) {
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
978 } else if (err->code != GOT_ERR_NO_OBJ)
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
983 err = got_ref_resolve(commit_id, repo, ref);
986 if (err->code != GOT_ERR_NOT_REF)
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix, allow_nonempty = 0;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1012 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1015 branch_name = optarg;
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1026 path_prefix = optarg;
1038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1039 "unveil", NULL) == -1)
1043 char *cwd, *base, *dotgit;
1044 repo_path = realpath(argv[0], NULL);
1045 if (repo_path == NULL)
1046 return got_error_from_errno2("realpath", argv[0]);
1047 cwd = getcwd(NULL, 0);
1049 error = got_error_from_errno("getcwd");
1052 if (path_prefix[0]) {
1053 base = basename(path_prefix);
1055 error = got_error_from_errno2("basename",
1060 base = basename(repo_path);
1062 error = got_error_from_errno2("basename",
1067 dotgit = strstr(base, ".git");
1070 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1071 error = got_error_from_errno("asprintf");
1076 } else if (argc == 2) {
1077 repo_path = realpath(argv[0], NULL);
1078 if (repo_path == NULL) {
1079 error = got_error_from_errno2("realpath", argv[0]);
1082 worktree_path = realpath(argv[1], NULL);
1083 if (worktree_path == NULL) {
1084 if (errno != ENOENT) {
1085 error = got_error_from_errno2("realpath",
1089 worktree_path = strdup(argv[1]);
1090 if (worktree_path == NULL) {
1091 error = got_error_from_errno("strdup");
1098 got_path_strip_trailing_slashes(repo_path);
1099 got_path_strip_trailing_slashes(worktree_path);
1101 error = got_repo_open(&repo, repo_path, NULL);
1105 /* Pre-create work tree path for unveil(2) */
1106 error = got_path_mkdir(worktree_path);
1108 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1109 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1111 if (!allow_nonempty &&
1112 !got_path_dir_is_empty(worktree_path)) {
1113 error = got_error_path(worktree_path,
1114 GOT_ERR_DIR_NOT_EMPTY);
1119 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1123 error = got_ref_open(&head_ref, repo, branch_name, 0);
1127 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1128 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1131 error = got_worktree_open(&worktree, worktree_path);
1135 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1139 if (!same_path_prefix) {
1140 error = got_error(GOT_ERR_PATH_PREFIX);
1144 if (commit_id_str) {
1145 struct got_object_id *commit_id;
1146 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1149 error = check_linear_ancestry(commit_id,
1150 got_worktree_get_base_commit_id(worktree), 0, repo);
1151 if (error != NULL) {
1155 error = check_same_branch(commit_id, head_ref, NULL, repo);
1158 error = got_worktree_set_base_commit_id(worktree, repo,
1165 error = got_pathlist_append(&paths, "", NULL);
1168 cpa.worktree_path = worktree_path;
1169 cpa.had_base_commit_ref_error = 0;
1170 error = got_worktree_checkout_files(worktree, &paths, repo,
1171 checkout_progress, &cpa, check_cancelled, NULL);
1175 printf("Now shut up and hack\n");
1176 if (cpa.had_base_commit_ref_error)
1177 show_worktree_base_ref_warning();
1180 got_pathlist_free(&paths);
1181 free(commit_id_str);
1183 free(worktree_path);
1190 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1195 static const struct got_error *
1196 update_progress(void *arg, unsigned char status, const char *path)
1198 int *did_something = arg;
1200 if (status == GOT_STATUS_EXISTS ||
1201 status == GOT_STATUS_BASE_REF_ERR)
1206 /* Base commit bump happens silently. */
1207 if (status == GOT_STATUS_BUMP_BASE)
1210 while (path[0] == '/')
1212 printf("%c %s\n", status, path);
1216 static const struct got_error *
1217 switch_head_ref(struct got_reference *head_ref,
1218 struct got_object_id *commit_id, struct got_worktree *worktree,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1223 int ref_has_moved = 0;
1225 /* Trivial case: switching between two different references. */
1226 if (strcmp(got_ref_get_name(head_ref),
1227 got_worktree_get_head_ref_name(worktree)) != 0) {
1228 printf("Switching work tree from %s to %s\n",
1229 got_worktree_get_head_ref_name(worktree),
1230 got_ref_get_name(head_ref));
1231 return got_worktree_set_head_ref(worktree, head_ref);
1234 err = check_linear_ancestry(commit_id,
1235 got_worktree_get_base_commit_id(worktree), 0, repo);
1237 if (err->code != GOT_ERR_ANCESTRY)
1244 /* Switching to a rebased branch with the same reference name. */
1245 err = got_object_id_str(&base_id_str,
1246 got_worktree_get_base_commit_id(worktree));
1249 printf("Reference %s now points at a different branch\n",
1250 got_worktree_get_head_ref_name(worktree));
1251 printf("Switching work tree from %s to %s\n", base_id_str,
1252 got_worktree_get_head_ref_name(worktree));
1256 static const struct got_error *
1257 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1259 const struct got_error *err;
1262 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1266 return got_error(GOT_ERR_REBASING);
1268 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1272 return got_error(GOT_ERR_HISTEDIT_BUSY);
1277 static const struct got_error *
1278 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1279 char *argv[], struct got_worktree *worktree)
1281 const struct got_error *err = NULL;
1288 return got_error_from_errno("strdup");
1289 return got_pathlist_append(paths, path, NULL);
1292 for (i = 0; i < argc; i++) {
1293 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1296 err = got_pathlist_append(paths, path, NULL);
1306 static const struct got_error *
1307 cmd_update(int argc, char *argv[])
1309 const struct got_error *error = NULL;
1310 struct got_repository *repo = NULL;
1311 struct got_worktree *worktree = NULL;
1312 char *worktree_path = NULL;
1313 struct got_object_id *commit_id = NULL;
1314 char *commit_id_str = NULL;
1315 const char *branch_name = NULL;
1316 struct got_reference *head_ref = NULL;
1317 struct got_pathlist_head paths;
1318 struct got_pathlist_entry *pe;
1319 int ch, did_something = 0;
1323 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1326 branch_name = optarg;
1329 commit_id_str = strdup(optarg);
1330 if (commit_id_str == NULL)
1331 return got_error_from_errno("strdup");
1343 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1344 "unveil", NULL) == -1)
1347 worktree_path = getcwd(NULL, 0);
1348 if (worktree_path == NULL) {
1349 error = got_error_from_errno("getcwd");
1352 error = got_worktree_open(&worktree, worktree_path);
1356 error = check_rebase_or_histedit_in_progress(worktree);
1360 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1365 error = apply_unveil(got_repo_get_path(repo), 0,
1366 got_worktree_get_root_path(worktree));
1370 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1374 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1375 got_worktree_get_head_ref_name(worktree), 0);
1378 if (commit_id_str == NULL) {
1379 error = got_ref_resolve(&commit_id, repo, head_ref);
1382 error = got_object_id_str(&commit_id_str, commit_id);
1386 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1387 free(commit_id_str);
1388 commit_id_str = NULL;
1391 error = got_object_id_str(&commit_id_str, commit_id);
1397 struct got_object_id *head_commit_id;
1398 TAILQ_FOREACH(pe, &paths, entry) {
1399 if (pe->path_len == 0)
1401 error = got_error_msg(GOT_ERR_BAD_PATH,
1402 "switching between branches requires that "
1403 "the entire work tree gets updated");
1406 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1409 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1411 free(head_commit_id);
1414 error = check_same_branch(commit_id, head_ref, NULL, repo);
1417 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1421 error = check_linear_ancestry(commit_id,
1422 got_worktree_get_base_commit_id(worktree), 0, repo);
1423 if (error != NULL) {
1424 if (error->code == GOT_ERR_ANCESTRY)
1425 error = got_error(GOT_ERR_BRANCH_MOVED);
1428 error = check_same_branch(commit_id, head_ref, NULL, repo);
1433 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1435 error = got_worktree_set_base_commit_id(worktree, repo,
1441 error = got_worktree_checkout_files(worktree, &paths, repo,
1442 update_progress, &did_something, check_cancelled, NULL);
1447 printf("Updated to commit %s\n", commit_id_str);
1449 printf("Already up-to-date\n");
1451 free(worktree_path);
1452 TAILQ_FOREACH(pe, &paths, entry)
1453 free((char *)pe->path);
1454 got_pathlist_free(&paths);
1456 free(commit_id_str);
1460 static const struct got_error *
1461 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1462 const char *path, int diff_context, int ignore_whitespace,
1463 struct got_repository *repo)
1465 const struct got_error *err = NULL;
1466 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1469 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1474 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1478 while (path[0] == '/')
1480 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1481 ignore_whitespace, stdout);
1484 got_object_blob_close(blob1);
1485 got_object_blob_close(blob2);
1489 static const struct got_error *
1490 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1491 const char *path, int diff_context, int ignore_whitespace,
1492 struct got_repository *repo)
1494 const struct got_error *err = NULL;
1495 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1496 struct got_diff_blob_output_unidiff_arg arg;
1499 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1504 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1508 arg.diff_context = diff_context;
1509 arg.ignore_whitespace = ignore_whitespace;
1510 arg.outfile = stdout;
1511 while (path[0] == '/')
1513 err = got_diff_tree(tree1, tree2, path, path, repo,
1514 got_diff_blob_output_unidiff, &arg, 1);
1517 got_object_tree_close(tree1);
1519 got_object_tree_close(tree2);
1523 static const struct got_error *
1524 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1525 const char *path, int diff_context, struct got_repository *repo)
1527 const struct got_error *err = NULL;
1528 struct got_commit_object *pcommit = NULL;
1529 char *id_str1 = NULL, *id_str2 = NULL;
1530 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1531 struct got_object_qid *qid;
1533 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1535 err = got_object_open_as_commit(&pcommit, repo,
1541 if (path && path[0] != '\0') {
1543 err = got_object_id_by_path(&obj_id2, repo, id, path);
1546 err = got_object_id_str(&id_str2, obj_id2);
1552 err = got_object_id_by_path(&obj_id1, repo,
1558 err = got_object_id_str(&id_str1, obj_id1);
1564 err = got_object_get_type(&obj_type, repo, obj_id2);
1569 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1571 case GOT_OBJ_TYPE_BLOB:
1572 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1575 case GOT_OBJ_TYPE_TREE:
1576 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1580 err = got_error(GOT_ERR_OBJ_TYPE);
1586 obj_id2 = got_object_commit_get_tree_id(commit);
1587 err = got_object_id_str(&id_str2, obj_id2);
1590 obj_id1 = got_object_commit_get_tree_id(pcommit);
1591 err = got_object_id_str(&id_str1, obj_id1);
1594 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1595 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1602 got_object_commit_close(pcommit);
1607 get_datestr(time_t *time, char *datebuf)
1609 struct tm mytm, *tm;
1612 tm = gmtime_r(time, &mytm);
1615 s = asctime_r(tm, datebuf);
1618 p = strchr(s, '\n');
1624 static const struct got_error *
1625 match_logmsg(int *have_match, struct got_object_id *id,
1626 struct got_commit_object *commit, regex_t *regex)
1628 const struct got_error *err = NULL;
1629 regmatch_t regmatch;
1630 char *id_str = NULL, *logmsg = NULL;
1634 err = got_object_id_str(&id_str, id);
1638 err = got_object_commit_get_logmsg(&logmsg, commit);
1642 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1650 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1652 static const struct got_error *
1653 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1654 struct got_repository *repo, const char *path, int show_patch,
1655 int diff_context, struct got_reflist_head *refs)
1657 const struct got_error *err = NULL;
1658 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1660 time_t committer_time;
1661 const char *author, *committer;
1662 char *refs_str = NULL;
1663 struct got_reflist_entry *re;
1665 SIMPLEQ_FOREACH(re, refs, entry) {
1668 struct got_tag_object *tag = NULL;
1671 name = got_ref_get_name(re->ref);
1672 if (strcmp(name, GOT_REF_HEAD) == 0)
1674 if (strncmp(name, "refs/", 5) == 0)
1676 if (strncmp(name, "got/", 4) == 0)
1678 if (strncmp(name, "heads/", 6) == 0)
1680 if (strncmp(name, "remotes/", 8) == 0)
1682 if (strncmp(name, "tags/", 5) == 0) {
1683 err = got_object_open_as_tag(&tag, repo, re->id);
1685 if (err->code != GOT_ERR_OBJ_TYPE)
1687 /* Ref points at something other than a tag. */
1692 cmp = got_object_id_cmp(tag ?
1693 got_object_tag_get_object_id(tag) : re->id, id);
1695 got_object_tag_close(tag);
1699 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1701 err = got_error_from_errno("asprintf");
1707 err = got_object_id_str(&id_str, id);
1711 printf(GOT_COMMIT_SEP_STR);
1712 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1713 refs_str ? refs_str : "", refs_str ? ")" : "");
1718 printf("from: %s\n", got_object_commit_get_author(commit));
1719 committer_time = got_object_commit_get_committer_time(commit);
1720 datestr = get_datestr(&committer_time, datebuf);
1722 printf("date: %s UTC\n", datestr);
1723 author = got_object_commit_get_author(commit);
1724 committer = got_object_commit_get_committer(commit);
1725 if (strcmp(author, committer) != 0)
1726 printf("via: %s\n", committer);
1727 if (got_object_commit_get_nparents(commit) > 1) {
1728 const struct got_object_id_queue *parent_ids;
1729 struct got_object_qid *qid;
1731 parent_ids = got_object_commit_get_parent_ids(commit);
1732 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1733 err = got_object_id_str(&id_str, qid->id);
1736 printf("parent %d: %s\n", n++, id_str);
1741 err = got_object_commit_get_logmsg(&logmsg0, commit);
1747 line = strsep(&logmsg, "\n");
1749 printf(" %s\n", line);
1754 err = print_patch(commit, id, path, diff_context, repo);
1759 if (fflush(stdout) != 0 && err == NULL)
1760 err = got_error_from_errno("fflush");
1764 static const struct got_error *
1765 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1766 const char *path, int show_patch, const char *search_pattern,
1767 int diff_context, int limit, int first_parent_traversal,
1768 struct got_reflist_head *refs)
1770 const struct got_error *err;
1771 struct got_commit_graph *graph;
1775 if (search_pattern &&
1776 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1777 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1779 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1782 err = got_commit_graph_iter_start(graph, root_id, repo,
1783 check_cancelled, NULL);
1787 struct got_commit_object *commit;
1788 struct got_object_id *id;
1790 if (sigint_received || sigpipe_received)
1793 err = got_commit_graph_iter_next(&id, graph, repo,
1794 check_cancelled, NULL);
1796 if (err->code == GOT_ERR_ITER_COMPLETED)
1803 err = got_object_open_as_commit(&commit, repo, id);
1807 if (search_pattern) {
1808 err = match_logmsg(&have_match, id, commit, ®ex);
1810 got_object_commit_close(commit);
1813 if (have_match == 0) {
1814 got_object_commit_close(commit);
1819 err = print_commit(commit, id, repo, path, show_patch,
1820 diff_context, refs);
1821 got_object_commit_close(commit);
1822 if (err || (limit && --limit == 0))
1828 got_commit_graph_close(graph);
1835 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1836 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1841 get_default_log_limit(void)
1843 const char *got_default_log_limit;
1847 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1848 if (got_default_log_limit == NULL)
1850 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1856 static const struct got_error *
1857 cmd_log(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 struct got_commit_object *commit = NULL;
1863 struct got_object_id *id = NULL;
1864 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1865 const char *start_commit = NULL, *search_pattern = NULL;
1866 int diff_context = -1, ch;
1867 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1869 struct got_reflist_head refs;
1871 SIMPLEQ_INIT(&refs);
1874 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1880 limit = get_default_log_limit();
1882 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1888 start_commit = optarg;
1891 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1894 err(1, "-C option %s", errstr);
1897 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1899 err(1, "-l option %s", errstr);
1902 first_parent_traversal = 1;
1905 repo_path = realpath(optarg, NULL);
1906 if (repo_path == NULL)
1907 return got_error_from_errno2("realpath",
1909 got_path_strip_trailing_slashes(repo_path);
1912 search_pattern = optarg;
1923 if (diff_context == -1)
1925 else if (!show_patch)
1926 errx(1, "-C reguires -p");
1928 cwd = getcwd(NULL, 0);
1930 error = got_error_from_errno("getcwd");
1934 error = got_worktree_open(&worktree, cwd);
1935 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1942 error = got_error_from_errno("strdup");
1945 } else if (argc == 1) {
1947 error = got_worktree_resolve_path(&path, worktree,
1952 path = strdup(argv[0]);
1954 error = got_error_from_errno("strdup");
1961 if (repo_path == NULL) {
1962 repo_path = worktree ?
1963 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1965 if (repo_path == NULL) {
1966 error = got_error_from_errno("strdup");
1970 error = got_repo_open(&repo, repo_path, NULL);
1974 error = apply_unveil(got_repo_get_path(repo), 1,
1975 worktree ? got_worktree_get_root_path(worktree) : NULL);
1979 if (start_commit == NULL) {
1980 struct got_reference *head_ref;
1981 error = got_ref_open(&head_ref, repo,
1982 worktree ? got_worktree_get_head_ref_name(worktree)
1986 error = got_ref_resolve(&id, repo, head_ref);
1987 got_ref_close(head_ref);
1990 error = got_object_open_as_commit(&commit, repo, id);
1992 struct got_reference *ref;
1993 error = got_ref_open(&ref, repo, start_commit, 0);
1994 if (error == NULL) {
1996 error = got_ref_resolve(&id, repo, ref);
2000 error = got_object_get_type(&obj_type, repo, id);
2003 if (obj_type == GOT_OBJ_TYPE_TAG) {
2004 struct got_tag_object *tag;
2005 error = got_object_open_as_tag(&tag, repo, id);
2008 if (got_object_tag_get_object_type(tag) !=
2009 GOT_OBJ_TYPE_COMMIT) {
2010 got_object_tag_close(tag);
2011 error = got_error(GOT_ERR_OBJ_TYPE);
2015 id = got_object_id_dup(
2016 got_object_tag_get_object_id(tag));
2018 error = got_error_from_errno(
2019 "got_object_id_dup");
2020 got_object_tag_close(tag);
2023 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2024 error = got_error(GOT_ERR_OBJ_TYPE);
2027 error = got_object_open_as_commit(&commit, repo, id);
2031 if (commit == NULL) {
2032 error = got_repo_match_object_id_prefix(&id,
2033 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2042 const char *prefix = got_worktree_get_path_prefix(worktree);
2044 if (asprintf(&p, "%s%s%s", prefix,
2045 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2046 error = got_error_from_errno("asprintf");
2049 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2052 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2057 path = in_repo_path;
2060 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2064 error = print_commits(id, repo, path, show_patch, search_pattern,
2065 diff_context, limit, first_parent_traversal, &refs);
2072 got_worktree_close(worktree);
2074 const struct got_error *repo_error;
2075 repo_error = got_repo_close(repo);
2079 got_ref_list_free(&refs);
2086 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2087 "[-w] [object1 object2 | path]\n", getprogname());
2091 struct print_diff_arg {
2092 struct got_repository *repo;
2093 struct got_worktree *worktree;
2098 int ignore_whitespace;
2101 static const struct got_error *
2102 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2103 const char *path, struct got_object_id *blob_id,
2104 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2105 int dirfd, const char *de_name)
2107 struct print_diff_arg *a = arg;
2108 const struct got_error *err = NULL;
2109 struct got_blob_object *blob1 = NULL;
2112 char *abspath = NULL, *label1 = NULL;
2115 if (a->diff_staged) {
2116 if (staged_status != GOT_STATUS_MODIFY &&
2117 staged_status != GOT_STATUS_ADD &&
2118 staged_status != GOT_STATUS_DELETE)
2121 if (staged_status == GOT_STATUS_DELETE)
2123 if (status == GOT_STATUS_NONEXISTENT)
2124 return got_error_set_errno(ENOENT, path);
2125 if (status != GOT_STATUS_MODIFY &&
2126 status != GOT_STATUS_ADD &&
2127 status != GOT_STATUS_DELETE &&
2128 status != GOT_STATUS_CONFLICT)
2132 if (!a->header_shown) {
2133 printf("diff %s %s%s\n", a->id_str,
2134 got_worktree_get_root_path(a->worktree),
2135 a->diff_staged ? " (staged changes)" : "");
2136 a->header_shown = 1;
2139 if (a->diff_staged) {
2140 const char *label1 = NULL, *label2 = NULL;
2141 switch (staged_status) {
2142 case GOT_STATUS_MODIFY:
2146 case GOT_STATUS_ADD:
2149 case GOT_STATUS_DELETE:
2153 return got_error(GOT_ERR_FILE_STATUS);
2155 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2156 label1, label2, a->diff_context, a->ignore_whitespace,
2160 if (staged_status == GOT_STATUS_ADD ||
2161 staged_status == GOT_STATUS_MODIFY) {
2163 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2167 err = got_object_id_str(&id_str, staged_blob_id);
2170 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2171 err = got_error_from_errno("asprintf");
2176 } else if (status != GOT_STATUS_ADD) {
2177 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2182 if (status != GOT_STATUS_DELETE) {
2183 if (asprintf(&abspath, "%s/%s",
2184 got_worktree_get_root_path(a->worktree), path) == -1) {
2185 err = got_error_from_errno("asprintf");
2190 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2192 err = got_error_from_errno2("openat", abspath);
2196 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2198 err = got_error_from_errno2("open", abspath);
2202 if (fstat(fd, &sb) == -1) {
2203 err = got_error_from_errno2("fstat", abspath);
2206 f2 = fdopen(fd, "r");
2208 err = got_error_from_errno2("fdopen", abspath);
2215 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2216 a->diff_context, a->ignore_whitespace, stdout);
2219 got_object_blob_close(blob1);
2220 if (f2 && fclose(f2) == EOF && err == NULL)
2221 err = got_error_from_errno("fclose");
2222 if (fd != -1 && close(fd) == -1 && err == NULL)
2223 err = got_error_from_errno("close");
2228 static const struct got_error *
2229 match_object_id(struct got_object_id **id, char **label,
2230 const char *id_str, int obj_type, int resolve_tags,
2231 struct got_repository *repo)
2233 const struct got_error *err;
2234 struct got_tag_object *tag;
2235 struct got_reference *ref = NULL;
2241 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2244 *id = got_object_id_dup(
2245 got_object_tag_get_object_id(tag));
2247 err = got_error_from_errno("got_object_id_dup");
2248 else if (asprintf(label, "refs/tags/%s",
2249 got_object_tag_get_name(tag)) == -1) {
2250 err = got_error_from_errno("asprintf");
2254 got_object_tag_close(tag);
2256 } else if (err->code != GOT_ERR_NO_OBJ)
2260 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2262 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2264 err = got_ref_open(&ref, repo, id_str, 0);
2267 *label = strdup(got_ref_get_name(ref));
2268 if (*label == NULL) {
2269 err = got_error_from_errno("strdup");
2272 err = got_ref_resolve(id, repo, ref);
2274 err = got_object_id_str(label, *id);
2275 if (*label == NULL) {
2276 err = got_error_from_errno("strdup");
2287 static const struct got_error *
2288 cmd_diff(int argc, char *argv[])
2290 const struct got_error *error;
2291 struct got_repository *repo = NULL;
2292 struct got_worktree *worktree = NULL;
2293 char *cwd = NULL, *repo_path = NULL;
2294 struct got_object_id *id1 = NULL, *id2 = NULL;
2295 const char *id_str1 = NULL, *id_str2 = NULL;
2296 char *label1 = NULL, *label2 = NULL;
2298 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2303 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2308 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2311 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2314 err(1, "-C option %s", errstr);
2317 repo_path = realpath(optarg, NULL);
2318 if (repo_path == NULL)
2319 return got_error_from_errno2("realpath",
2321 got_path_strip_trailing_slashes(repo_path);
2327 ignore_whitespace = 1;
2338 cwd = getcwd(NULL, 0);
2340 error = got_error_from_errno("getcwd");
2343 error = got_worktree_open(&worktree, cwd);
2344 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2347 if (worktree == NULL) {
2348 error = got_error(GOT_ERR_NOT_WORKTREE);
2353 "-r option can't be used when diffing a work tree");
2354 repo_path = strdup(got_worktree_get_repo_path(worktree));
2355 if (repo_path == NULL) {
2356 error = got_error_from_errno("strdup");
2360 error = got_worktree_resolve_path(&path, worktree,
2367 error = got_error_from_errno("strdup");
2371 } else if (argc == 2) {
2373 errx(1, "-s option can't be used when diffing "
2374 "objects in repository");
2377 if (worktree && repo_path == NULL) {
2379 strdup(got_worktree_get_repo_path(worktree));
2380 if (repo_path == NULL) {
2381 error = got_error_from_errno("strdup");
2388 if (repo_path == NULL) {
2389 repo_path = getcwd(NULL, 0);
2390 if (repo_path == NULL)
2391 return got_error_from_errno("getcwd");
2394 error = got_repo_open(&repo, repo_path, NULL);
2399 error = apply_unveil(got_repo_get_path(repo), 1,
2400 worktree ? got_worktree_get_root_path(worktree) : NULL);
2405 struct print_diff_arg arg;
2406 struct got_pathlist_head paths;
2411 error = got_object_id_str(&id_str,
2412 got_worktree_get_base_commit_id(worktree));
2416 arg.worktree = worktree;
2417 arg.diff_context = diff_context;
2418 arg.id_str = id_str;
2419 arg.header_shown = 0;
2420 arg.diff_staged = diff_staged;
2421 arg.ignore_whitespace = ignore_whitespace;
2423 error = got_pathlist_append(&paths, path, NULL);
2427 error = got_worktree_status(worktree, &paths, repo, print_diff,
2428 &arg, check_cancelled, NULL);
2430 got_pathlist_free(&paths);
2434 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2439 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2444 error = got_object_get_type(&type1, repo, id1);
2448 error = got_object_get_type(&type2, repo, id2);
2452 if (type1 != type2) {
2453 error = got_error(GOT_ERR_OBJ_TYPE);
2458 case GOT_OBJ_TYPE_BLOB:
2459 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2460 diff_context, ignore_whitespace, repo, stdout);
2462 case GOT_OBJ_TYPE_TREE:
2463 error = got_diff_objects_as_trees(id1, id2, "", "",
2464 diff_context, ignore_whitespace, repo, stdout);
2466 case GOT_OBJ_TYPE_COMMIT:
2467 printf("diff %s %s\n", label1, label2);
2468 error = got_diff_objects_as_commits(id1, id2, diff_context,
2469 ignore_whitespace, repo, stdout);
2472 error = got_error(GOT_ERR_OBJ_TYPE);
2482 got_worktree_close(worktree);
2484 const struct got_error *repo_error;
2485 repo_error = got_repo_close(repo);
2496 "usage: %s blame [-c commit] [-r repository-path] path\n",
2505 char datebuf[11]; /* YYYY-MM-DD + NUL */
2508 struct blame_cb_args {
2509 struct blame_line *lines;
2513 off_t *line_offsets;
2515 struct got_repository *repo;
2518 static const struct got_error *
2519 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2521 const struct got_error *err = NULL;
2522 struct blame_cb_args *a = arg;
2523 struct blame_line *bline;
2525 size_t linesize = 0;
2526 struct got_commit_object *commit = NULL;
2529 time_t committer_time;
2531 if (nlines != a->nlines ||
2532 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2533 return got_error(GOT_ERR_RANGE);
2535 if (sigint_received)
2536 return got_error(GOT_ERR_ITER_COMPLETED);
2539 return NULL; /* no change in this commit */
2541 /* Annotate this line. */
2542 bline = &a->lines[lineno - 1];
2543 if (bline->annotated)
2545 err = got_object_id_str(&bline->id_str, id);
2549 err = got_object_open_as_commit(&commit, a->repo, id);
2553 bline->committer = strdup(got_object_commit_get_committer(commit));
2554 if (bline->committer == NULL) {
2555 err = got_error_from_errno("strdup");
2559 committer_time = got_object_commit_get_committer_time(commit);
2560 if (localtime_r(&committer_time, &tm) == NULL)
2561 return got_error_from_errno("localtime_r");
2562 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2563 &tm) >= sizeof(bline->datebuf)) {
2564 err = got_error(GOT_ERR_NO_SPACE);
2567 bline->annotated = 1;
2569 /* Print lines annotated so far. */
2570 bline = &a->lines[a->lineno_cur - 1];
2571 if (!bline->annotated)
2574 offset = a->line_offsets[a->lineno_cur - 1];
2575 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2576 err = got_error_from_errno("fseeko");
2580 while (bline->annotated) {
2581 char *smallerthan, *at, *nl, *committer;
2584 if (getline(&line, &linesize, a->f) == -1) {
2586 err = got_error_from_errno("getline");
2590 committer = bline->committer;
2591 smallerthan = strchr(committer, '<');
2592 if (smallerthan && smallerthan[1] != '\0')
2593 committer = smallerthan + 1;
2594 at = strchr(committer, '@');
2597 len = strlen(committer);
2599 committer[8] = '\0';
2601 nl = strchr(line, '\n');
2604 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2605 bline->id_str, bline->datebuf, committer, line);
2608 bline = &a->lines[a->lineno_cur - 1];
2612 got_object_commit_close(commit);
2617 static const struct got_error *
2618 cmd_blame(int argc, char *argv[])
2620 const struct got_error *error;
2621 struct got_repository *repo = NULL;
2622 struct got_worktree *worktree = NULL;
2623 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2624 struct got_object_id *obj_id = NULL;
2625 struct got_object_id *commit_id = NULL;
2626 struct got_blob_object *blob = NULL;
2627 char *commit_id_str = NULL;
2628 struct blame_cb_args bca;
2629 int ch, obj_type, i;
2632 memset(&bca, 0, sizeof(bca));
2635 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2640 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2643 commit_id_str = optarg;
2646 repo_path = realpath(optarg, NULL);
2647 if (repo_path == NULL)
2648 return got_error_from_errno2("realpath",
2650 got_path_strip_trailing_slashes(repo_path);
2666 cwd = getcwd(NULL, 0);
2668 error = got_error_from_errno("getcwd");
2671 if (repo_path == NULL) {
2672 error = got_worktree_open(&worktree, cwd);
2673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2679 strdup(got_worktree_get_repo_path(worktree));
2680 if (repo_path == NULL)
2681 error = got_error_from_errno("strdup");
2685 repo_path = strdup(cwd);
2686 if (repo_path == NULL) {
2687 error = got_error_from_errno("strdup");
2693 error = got_repo_open(&repo, repo_path, NULL);
2697 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2702 const char *prefix = got_worktree_get_path_prefix(worktree);
2703 char *p, *worktree_subdir = cwd +
2704 strlen(got_worktree_get_root_path(worktree));
2705 if (asprintf(&p, "%s%s%s%s%s",
2706 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2707 worktree_subdir, worktree_subdir[0] ? "/" : "",
2709 error = got_error_from_errno("asprintf");
2712 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2715 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2720 if (commit_id_str == NULL) {
2721 struct got_reference *head_ref;
2722 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2725 error = got_ref_resolve(&commit_id, repo, head_ref);
2726 got_ref_close(head_ref);
2730 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2735 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2738 if (obj_id == NULL) {
2739 error = got_error(GOT_ERR_NO_OBJ);
2743 error = got_object_get_type(&obj_type, repo, obj_id);
2747 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2748 error = got_error(GOT_ERR_OBJ_TYPE);
2752 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2755 bca.f = got_opentemp();
2756 if (bca.f == NULL) {
2757 error = got_error_from_errno("got_opentemp");
2760 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2761 &bca.line_offsets, bca.f, blob);
2762 if (error || bca.nlines == 0)
2765 /* Don't include \n at EOF in the blame line count. */
2766 if (bca.line_offsets[bca.nlines - 1] == filesize)
2769 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2770 if (bca.lines == NULL) {
2771 error = got_error_from_errno("calloc");
2775 bca.nlines_prec = 0;
2783 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2784 check_cancelled, NULL);
2794 got_object_blob_close(blob);
2796 got_worktree_close(worktree);
2798 const struct got_error *repo_error;
2799 repo_error = got_repo_close(repo);
2804 for (i = 0; i < bca.nlines; i++) {
2805 struct blame_line *bline = &bca.lines[i];
2806 free(bline->id_str);
2807 free(bline->committer);
2811 free(bca.line_offsets);
2812 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2813 error = got_error_from_errno("fclose");
2821 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2827 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2828 const char *root_path)
2830 int is_root_path = (strcmp(path, root_path) == 0);
2831 const char *modestr = "";
2832 mode_t mode = got_tree_entry_get_mode(te);
2834 path += strlen(root_path);
2835 while (path[0] == '/')
2838 if (got_object_tree_entry_is_submodule(te))
2840 else if (S_ISLNK(mode))
2842 else if (S_ISDIR(mode))
2844 else if (mode & S_IXUSR)
2847 printf("%s%s%s%s%s\n", id ? id : "", path,
2848 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2851 static const struct got_error *
2852 print_tree(const char *path, struct got_object_id *commit_id,
2853 int show_ids, int recurse, const char *root_path,
2854 struct got_repository *repo)
2856 const struct got_error *err = NULL;
2857 struct got_object_id *tree_id = NULL;
2858 struct got_tree_object *tree = NULL;
2861 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2865 err = got_object_open_as_tree(&tree, repo, tree_id);
2868 nentries = got_object_tree_get_nentries(tree);
2869 for (i = 0; i < nentries; i++) {
2870 struct got_tree_entry *te;
2873 if (sigint_received || sigpipe_received)
2876 te = got_object_tree_get_entry(tree, i);
2879 err = got_object_id_str(&id_str,
2880 got_tree_entry_get_id(te));
2883 if (asprintf(&id, "%s ", id_str) == -1) {
2884 err = got_error_from_errno("asprintf");
2890 print_entry(te, id, path, root_path);
2893 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2895 if (asprintf(&child_path, "%s%s%s", path,
2896 path[0] == '/' && path[1] == '\0' ? "" : "/",
2897 got_tree_entry_get_name(te)) == -1) {
2898 err = got_error_from_errno("asprintf");
2901 err = print_tree(child_path, commit_id, show_ids, 1,
2910 got_object_tree_close(tree);
2915 static const struct got_error *
2916 cmd_tree(int argc, char *argv[])
2918 const struct got_error *error;
2919 struct got_repository *repo = NULL;
2920 struct got_worktree *worktree = NULL;
2922 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2923 struct got_object_id *commit_id = NULL;
2924 char *commit_id_str = NULL;
2925 int show_ids = 0, recurse = 0;
2929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2934 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2937 commit_id_str = optarg;
2940 repo_path = realpath(optarg, NULL);
2941 if (repo_path == NULL)
2942 return got_error_from_errno2("realpath",
2944 got_path_strip_trailing_slashes(repo_path);
2968 cwd = getcwd(NULL, 0);
2970 error = got_error_from_errno("getcwd");
2973 if (repo_path == NULL) {
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2981 strdup(got_worktree_get_repo_path(worktree));
2982 if (repo_path == NULL)
2983 error = got_error_from_errno("strdup");
2987 repo_path = strdup(cwd);
2988 if (repo_path == NULL) {
2989 error = got_error_from_errno("strdup");
2995 error = got_repo_open(&repo, repo_path, NULL);
2999 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3005 char *p, *worktree_subdir = cwd +
3006 strlen(got_worktree_get_root_path(worktree));
3007 if (asprintf(&p, "%s/%s",
3008 got_worktree_get_path_prefix(worktree),
3009 worktree_subdir) == -1) {
3010 error = got_error_from_errno("asprintf");
3013 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3020 if (in_repo_path == NULL) {
3021 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3026 if (commit_id_str == NULL) {
3027 struct got_reference *head_ref;
3028 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3031 error = got_ref_resolve(&commit_id, repo, head_ref);
3032 got_ref_close(head_ref);
3036 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3041 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3042 in_repo_path, repo);
3049 got_worktree_close(worktree);
3051 const struct got_error *repo_error;
3052 repo_error = got_repo_close(repo);
3062 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3066 static const struct got_error *
3067 print_status(void *arg, unsigned char status, unsigned char staged_status,
3068 const char *path, struct got_object_id *blob_id,
3069 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3070 int dirfd, const char *de_name)
3072 if (status == staged_status && (status == GOT_STATUS_DELETE))
3073 status = GOT_STATUS_NO_CHANGE;
3074 printf("%c%c %s\n", status, staged_status, path);
3078 static const struct got_error *
3079 cmd_status(int argc, char *argv[])
3081 const struct got_error *error = NULL;
3082 struct got_repository *repo = NULL;
3083 struct got_worktree *worktree = NULL;
3085 struct got_pathlist_head paths;
3086 struct got_pathlist_entry *pe;
3091 while ((ch = getopt(argc, argv, "")) != -1) {
3103 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3107 cwd = getcwd(NULL, 0);
3109 error = got_error_from_errno("getcwd");
3113 error = got_worktree_open(&worktree, cwd);
3117 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3122 error = apply_unveil(got_repo_get_path(repo), 1,
3123 got_worktree_get_root_path(worktree));
3127 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3131 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3132 check_cancelled, NULL);
3134 TAILQ_FOREACH(pe, &paths, entry)
3135 free((char *)pe->path);
3136 got_pathlist_free(&paths);
3145 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3150 static const struct got_error *
3151 list_refs(struct got_repository *repo)
3153 static const struct got_error *err = NULL;
3154 struct got_reflist_head refs;
3155 struct got_reflist_entry *re;
3157 SIMPLEQ_INIT(&refs);
3158 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3162 SIMPLEQ_FOREACH(re, &refs, entry) {
3164 refstr = got_ref_to_str(re->ref);
3166 return got_error_from_errno("got_ref_to_str");
3167 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3171 got_ref_list_free(&refs);
3175 static const struct got_error *
3176 delete_ref(struct got_repository *repo, const char *refname)
3178 const struct got_error *err = NULL;
3179 struct got_reference *ref;
3181 err = got_ref_open(&ref, repo, refname, 0);
3185 err = got_ref_delete(ref, repo);
3190 static const struct got_error *
3191 add_ref(struct got_repository *repo, const char *refname, const char *target)
3193 const struct got_error *err = NULL;
3194 struct got_object_id *id;
3195 struct got_reference *ref = NULL;
3198 * Don't let the user create a reference name with a leading '-'.
3199 * While technically a valid reference name, this case is usually
3200 * an unintended typo.
3202 if (refname[0] == '-')
3203 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3205 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3208 struct got_reference *target_ref;
3210 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3212 err = got_ref_open(&target_ref, repo, target, 0);
3215 err = got_ref_resolve(&id, repo, target_ref);
3216 got_ref_close(target_ref);
3221 err = got_ref_alloc(&ref, refname, id);
3225 err = got_ref_write(ref, repo);
3233 static const struct got_error *
3234 add_symref(struct got_repository *repo, const char *refname, const char *target)
3236 const struct got_error *err = NULL;
3237 struct got_reference *ref = NULL;
3238 struct got_reference *target_ref = NULL;
3241 * Don't let the user create a reference name with a leading '-'.
3242 * While technically a valid reference name, this case is usually
3243 * an unintended typo.
3245 if (refname[0] == '-')
3246 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3248 err = got_ref_open(&target_ref, repo, target, 0);
3252 err = got_ref_alloc_symref(&ref, refname, target_ref);
3256 err = got_ref_write(ref, repo);
3259 got_ref_close(target_ref);
3265 static const struct got_error *
3266 cmd_ref(int argc, char *argv[])
3268 const struct got_error *error = NULL;
3269 struct got_repository *repo = NULL;
3270 struct got_worktree *worktree = NULL;
3271 char *cwd = NULL, *repo_path = NULL;
3272 int ch, do_list = 0, create_symref = 0;
3273 const char *delref = NULL;
3275 /* TODO: Add -s option for adding symbolic references. */
3276 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3282 repo_path = realpath(optarg, NULL);
3283 if (repo_path == NULL)
3284 return got_error_from_errno2("realpath",
3286 got_path_strip_trailing_slashes(repo_path);
3300 if (do_list && delref)
3301 errx(1, "-l and -d options are mutually exclusive\n");
3306 if (do_list || delref) {
3308 errx(1, "-s option cannot be used together with the "
3309 "-l or -d options");
3312 } else if (argc != 2)
3317 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3321 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3322 "sendfd unveil", NULL) == -1)
3326 cwd = getcwd(NULL, 0);
3328 error = got_error_from_errno("getcwd");
3332 if (repo_path == NULL) {
3333 error = got_worktree_open(&worktree, cwd);
3334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3340 strdup(got_worktree_get_repo_path(worktree));
3341 if (repo_path == NULL)
3342 error = got_error_from_errno("strdup");
3346 repo_path = strdup(cwd);
3347 if (repo_path == NULL) {
3348 error = got_error_from_errno("strdup");
3354 error = got_repo_open(&repo, repo_path, NULL);
3358 error = apply_unveil(got_repo_get_path(repo), do_list,
3359 worktree ? got_worktree_get_root_path(worktree) : NULL);
3364 error = list_refs(repo);
3366 error = delete_ref(repo, delref);
3367 else if (create_symref)
3368 error = add_symref(repo, argv[0], argv[1]);
3370 error = add_ref(repo, argv[0], argv[1]);
3373 got_repo_close(repo);
3375 got_worktree_close(worktree);
3385 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3386 "[name]\n", getprogname());
3390 static const struct got_error *
3391 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3392 struct got_reference *ref)
3394 const struct got_error *err = NULL;
3395 const char *refname, *marker = " ";
3398 refname = got_ref_get_name(ref);
3399 if (worktree && strcmp(refname,
3400 got_worktree_get_head_ref_name(worktree)) == 0) {
3401 struct got_object_id *id = NULL;
3403 err = got_ref_resolve(&id, repo, ref);
3406 if (got_object_id_cmp(id,
3407 got_worktree_get_base_commit_id(worktree)) == 0)
3414 if (strncmp(refname, "refs/heads/", 11) == 0)
3416 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3419 refstr = got_ref_to_str(ref);
3421 return got_error_from_errno("got_ref_to_str");
3423 printf("%s%s: %s\n", marker, refname, refstr);
3428 static const struct got_error *
3429 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3431 const char *refname;
3433 if (worktree == NULL)
3434 return got_error(GOT_ERR_NOT_WORKTREE);
3436 refname = got_worktree_get_head_ref_name(worktree);
3438 if (strncmp(refname, "refs/heads/", 11) == 0)
3440 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3443 printf("%s\n", refname);
3448 static const struct got_error *
3449 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3451 static const struct got_error *err = NULL;
3452 struct got_reflist_head refs;
3453 struct got_reflist_entry *re;
3454 struct got_reference *temp_ref = NULL;
3455 int rebase_in_progress, histedit_in_progress;
3457 SIMPLEQ_INIT(&refs);
3460 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3465 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3470 if (rebase_in_progress || histedit_in_progress) {
3471 err = got_ref_open(&temp_ref, repo,
3472 got_worktree_get_head_ref_name(worktree), 0);
3475 list_branch(repo, worktree, temp_ref);
3476 got_ref_close(temp_ref);
3480 err = got_ref_list(&refs, repo, "refs/heads",
3481 got_ref_cmp_by_name, NULL);
3485 SIMPLEQ_FOREACH(re, &refs, entry)
3486 list_branch(repo, worktree, re->ref);
3488 got_ref_list_free(&refs);
3492 static const struct got_error *
3493 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3494 const char *branch_name)
3496 const struct got_error *err = NULL;
3497 struct got_reference *ref = NULL;
3500 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3501 return got_error_from_errno("asprintf");
3503 err = got_ref_open(&ref, repo, refname, 0);
3508 strcmp(got_worktree_get_head_ref_name(worktree),
3509 got_ref_get_name(ref)) == 0) {
3510 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3511 "will not delete this work tree's current branch");
3515 err = got_ref_delete(ref, repo);
3523 static const struct got_error *
3524 add_branch(struct got_repository *repo, const char *branch_name,
3525 struct got_object_id *base_commit_id)
3527 const struct got_error *err = NULL;
3528 struct got_reference *ref = NULL;
3529 char *base_refname = NULL, *refname = NULL;
3532 * Don't let the user create a branch name with a leading '-'.
3533 * While technically a valid reference name, this case is usually
3534 * an unintended typo.
3536 if (branch_name[0] == '-')
3537 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3539 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3540 err = got_error_from_errno("asprintf");
3544 err = got_ref_open(&ref, repo, refname, 0);
3546 err = got_error(GOT_ERR_BRANCH_EXISTS);
3548 } else if (err->code != GOT_ERR_NOT_REF)
3551 err = got_ref_alloc(&ref, refname, base_commit_id);
3555 err = got_ref_write(ref, repo);
3564 static const struct got_error *
3565 cmd_branch(int argc, char *argv[])
3567 const struct got_error *error = NULL;
3568 struct got_repository *repo = NULL;
3569 struct got_worktree *worktree = NULL;
3570 char *cwd = NULL, *repo_path = NULL;
3571 int ch, do_list = 0, do_show = 0;
3572 const char *delref = NULL, *commit_id_arg = NULL;
3574 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3577 commit_id_arg = optarg;
3583 repo_path = realpath(optarg, NULL);
3584 if (repo_path == NULL)
3585 return got_error_from_errno2("realpath",
3587 got_path_strip_trailing_slashes(repo_path);
3598 if (do_list && delref)
3599 errx(1, "-l and -d options are mutually exclusive\n");
3604 if (!do_list && !delref && argc == 0)
3607 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3608 errx(1, "-c option can only be used when creating a branch");
3610 if (do_list || delref) {
3613 } else if (!do_show && argc != 1)
3617 if (do_list || do_show) {
3618 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3622 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3623 "sendfd unveil", NULL) == -1)
3627 cwd = getcwd(NULL, 0);
3629 error = got_error_from_errno("getcwd");
3633 if (repo_path == NULL) {
3634 error = got_worktree_open(&worktree, cwd);
3635 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3641 strdup(got_worktree_get_repo_path(worktree));
3642 if (repo_path == NULL)
3643 error = got_error_from_errno("strdup");
3647 repo_path = strdup(cwd);
3648 if (repo_path == NULL) {
3649 error = got_error_from_errno("strdup");
3655 error = got_repo_open(&repo, repo_path, NULL);
3659 error = apply_unveil(got_repo_get_path(repo), do_list,
3660 worktree ? got_worktree_get_root_path(worktree) : NULL);
3665 error = show_current_branch(repo, worktree);
3667 error = list_branches(repo, worktree);
3669 error = delete_branch(repo, worktree, delref);
3671 struct got_object_id *commit_id;
3672 if (commit_id_arg == NULL)
3673 commit_id_arg = worktree ?
3674 got_worktree_get_head_ref_name(worktree) :
3676 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3679 error = add_branch(repo, argv[0], commit_id);
3684 got_repo_close(repo);
3686 got_worktree_close(worktree);
3697 "usage: %s tag [-r repository] | -l | "
3698 "[-m message] name [commit]\n", getprogname());
3703 static const struct got_error *
3704 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3706 const struct got_error *err = NULL;
3707 struct got_reflist_entry *re, *se, *new;
3708 struct got_object_id *re_id, *se_id;
3709 struct got_tag_object *re_tag, *se_tag;
3710 time_t re_time, se_time;
3712 SIMPLEQ_FOREACH(re, tags, entry) {
3713 se = SIMPLEQ_FIRST(sorted);
3715 err = got_reflist_entry_dup(&new, re);
3718 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3721 err = got_ref_resolve(&re_id, repo, re->ref);
3724 err = got_object_open_as_tag(&re_tag, repo, re_id);
3728 re_time = got_object_tag_get_tagger_time(re_tag);
3729 got_object_tag_close(re_tag);
3733 err = got_ref_resolve(&se_id, repo, re->ref);
3736 err = got_object_open_as_tag(&se_tag, repo, se_id);
3740 se_time = got_object_tag_get_tagger_time(se_tag);
3741 got_object_tag_close(se_tag);
3743 if (se_time > re_time) {
3744 err = got_reflist_entry_dup(&new, re);
3747 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3750 se = SIMPLEQ_NEXT(se, entry);
3759 static const struct got_error *
3760 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3761 struct got_reference *ref2)
3763 const struct got_error *err = NULL;
3764 struct got_repository *repo = arg;
3765 struct got_object_id *id1, *id2 = NULL;
3766 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3767 time_t time1, time2;
3771 err = got_ref_resolve(&id1, repo, ref1);
3774 err = got_object_open_as_tag(&tag1, repo, id1);
3778 err = got_ref_resolve(&id2, repo, ref2);
3781 err = got_object_open_as_tag(&tag2, repo, id2);
3785 time1 = got_object_tag_get_tagger_time(tag1);
3786 time2 = got_object_tag_get_tagger_time(tag2);
3788 /* Put latest tags first. */
3791 else if (time1 > time2)
3794 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3799 got_object_tag_close(tag1);
3801 got_object_tag_close(tag2);
3805 static const struct got_error *
3806 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3808 static const struct got_error *err = NULL;
3809 struct got_reflist_head refs;
3810 struct got_reflist_entry *re;
3812 SIMPLEQ_INIT(&refs);
3814 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3818 SIMPLEQ_FOREACH(re, &refs, entry) {
3819 const char *refname;
3820 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3823 struct got_object_id *id;
3824 struct got_tag_object *tag;
3826 refname = got_ref_get_name(re->ref);
3827 if (strncmp(refname, "refs/tags/", 10) != 0)
3830 refstr = got_ref_to_str(re->ref);
3831 if (refstr == NULL) {
3832 err = got_error_from_errno("got_ref_to_str");
3835 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3838 err = got_ref_resolve(&id, repo, re->ref);
3841 err = got_object_open_as_tag(&tag, repo, id);
3845 printf("from: %s\n", got_object_tag_get_tagger(tag));
3846 tagger_time = got_object_tag_get_tagger_time(tag);
3847 datestr = get_datestr(&tagger_time, datebuf);
3849 printf("date: %s UTC\n", datestr);
3850 err = got_object_id_str(&id_str,
3851 got_object_tag_get_object_id(tag));
3854 switch (got_object_tag_get_object_type(tag)) {
3855 case GOT_OBJ_TYPE_BLOB:
3856 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3858 case GOT_OBJ_TYPE_TREE:
3859 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3861 case GOT_OBJ_TYPE_COMMIT:
3862 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3864 case GOT_OBJ_TYPE_TAG:
3865 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3871 tagmsg0 = strdup(got_object_tag_get_message(tag));
3872 got_object_tag_close(tag);
3873 if (tagmsg0 == NULL) {
3874 err = got_error_from_errno("strdup");
3880 line = strsep(&tagmsg, "\n");
3882 printf(" %s\n", line);
3887 got_ref_list_free(&refs);
3891 static const struct got_error *
3892 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3893 const char *tag_name, const char *repo_path)
3895 const struct got_error *err = NULL;
3896 char *template = NULL, *initial_content = NULL;
3897 char *editor = NULL;
3900 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3901 err = got_error_from_errno("asprintf");
3905 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3906 commit_id_str, tag_name) == -1) {
3907 err = got_error_from_errno("asprintf");
3911 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3915 dprintf(fd, initial_content);
3918 err = get_editor(&editor);
3921 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3923 free(initial_content);
3927 /* Editor is done; we can now apply unveil(2) */
3929 err = apply_unveil(repo_path, 0, NULL);
3938 static const struct got_error *
3939 add_tag(struct got_repository *repo, const char *tag_name,
3940 const char *commit_arg, const char *tagmsg_arg)
3942 const struct got_error *err = NULL;
3943 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3944 char *label = NULL, *commit_id_str = NULL;
3945 struct got_reference *ref = NULL;
3946 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3947 char *tagmsg_path = NULL, *tag_id_str = NULL;
3948 int preserve_tagmsg = 0;
3951 * Don't let the user create a tag name with a leading '-'.
3952 * While technically a valid reference name, this case is usually
3953 * an unintended typo.
3955 if (tag_name[0] == '-')
3956 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3958 err = get_author(&tagger, repo);
3962 err = match_object_id(&commit_id, &label, commit_arg,
3963 GOT_OBJ_TYPE_COMMIT, 1, repo);
3967 err = got_object_id_str(&commit_id_str, commit_id);
3971 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3972 refname = strdup(tag_name);
3973 if (refname == NULL) {
3974 err = got_error_from_errno("strdup");
3978 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3979 err = got_error_from_errno("asprintf");
3983 err = got_ref_open(&ref, repo, refname, 0);
3985 err = got_error(GOT_ERR_TAG_EXISTS);
3987 } else if (err->code != GOT_ERR_NOT_REF)
3990 if (tagmsg_arg == NULL) {
3991 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3992 tag_name, got_repo_get_path(repo));
3994 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3995 tagmsg_path != NULL)
3996 preserve_tagmsg = 1;
4001 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4002 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4005 preserve_tagmsg = 1;
4009 err = got_ref_alloc(&ref, refname, tag_id);
4012 preserve_tagmsg = 1;
4016 err = got_ref_write(ref, repo);
4019 preserve_tagmsg = 1;
4023 err = got_object_id_str(&tag_id_str, tag_id);
4026 preserve_tagmsg = 1;
4029 printf("Created tag %s\n", tag_id_str);
4031 if (preserve_tagmsg) {
4032 fprintf(stderr, "%s: tag message preserved in %s\n",
4033 getprogname(), tagmsg_path);
4034 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4035 err = got_error_from_errno2("unlink", tagmsg_path);
4040 free(commit_id_str);
4048 static const struct got_error *
4049 cmd_tag(int argc, char *argv[])
4051 const struct got_error *error = NULL;
4052 struct got_repository *repo = NULL;
4053 struct got_worktree *worktree = NULL;
4054 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4055 char *gitconfig_path = NULL;
4056 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4057 int ch, do_list = 0;
4059 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4065 repo_path = realpath(optarg, NULL);
4066 if (repo_path == NULL)
4067 return got_error_from_errno2("realpath",
4069 got_path_strip_trailing_slashes(repo_path);
4085 errx(1, "-l and -m options are mutually exclusive\n");
4088 } else if (argc < 1 || argc > 2)
4091 commit_id_arg = argv[1];
4096 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4100 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4101 "sendfd unveil", NULL) == -1)
4105 cwd = getcwd(NULL, 0);
4107 error = got_error_from_errno("getcwd");
4111 if (repo_path == NULL) {
4112 error = got_worktree_open(&worktree, cwd);
4113 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4119 strdup(got_worktree_get_repo_path(worktree));
4120 if (repo_path == NULL)
4121 error = got_error_from_errno("strdup");
4125 repo_path = strdup(cwd);
4126 if (repo_path == NULL) {
4127 error = got_error_from_errno("strdup");
4134 error = got_repo_open(&repo, repo_path, NULL);
4137 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4140 error = list_tags(repo, worktree);
4142 error = get_gitconfig_path(&gitconfig_path);
4145 error = got_repo_open(&repo, repo_path, gitconfig_path);
4150 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4155 if (commit_id_arg == NULL) {
4156 struct got_reference *head_ref;
4157 struct got_object_id *commit_id;
4158 error = got_ref_open(&head_ref, repo,
4159 worktree ? got_worktree_get_head_ref_name(worktree)
4163 error = got_ref_resolve(&commit_id, repo, head_ref);
4164 got_ref_close(head_ref);
4167 error = got_object_id_str(&commit_id_str, commit_id);
4173 error = add_tag(repo, tag_name,
4174 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4178 got_repo_close(repo);
4180 got_worktree_close(worktree);
4183 free(gitconfig_path);
4184 free(commit_id_str);
4191 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4196 static const struct got_error *
4197 add_progress(void *arg, unsigned char status, const char *path)
4199 while (path[0] == '/')
4201 printf("%c %s\n", status, path);
4205 static const struct got_error *
4206 cmd_add(int argc, char *argv[])
4208 const struct got_error *error = NULL;
4209 struct got_repository *repo = NULL;
4210 struct got_worktree *worktree = NULL;
4212 struct got_pathlist_head paths;
4213 struct got_pathlist_entry *pe;
4214 int ch, can_recurse = 0, no_ignores = 0;
4218 while ((ch = getopt(argc, argv, "IR")) != -1) {
4236 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4243 cwd = getcwd(NULL, 0);
4245 error = got_error_from_errno("getcwd");
4249 error = got_worktree_open(&worktree, cwd);
4253 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4258 error = apply_unveil(got_repo_get_path(repo), 1,
4259 got_worktree_get_root_path(worktree));
4263 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4267 if (!can_recurse && no_ignores) {
4268 error = got_error_msg(GOT_ERR_BAD_PATH,
4269 "disregarding ignores requires -R option");
4277 TAILQ_FOREACH(pe, &paths, entry) {
4278 if (asprintf(&ondisk_path, "%s/%s",
4279 got_worktree_get_root_path(worktree),
4281 error = got_error_from_errno("asprintf");
4284 if (lstat(ondisk_path, &sb) == -1) {
4285 if (errno == ENOENT) {
4289 error = got_error_from_errno2("lstat",
4295 if (S_ISDIR(sb.st_mode)) {
4296 error = got_error_msg(GOT_ERR_BAD_PATH,
4297 "adding directories requires -R option");
4303 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4304 NULL, repo, no_ignores);
4307 got_repo_close(repo);
4309 got_worktree_close(worktree);
4310 TAILQ_FOREACH(pe, &paths, entry)
4311 free((char *)pe->path);
4312 got_pathlist_free(&paths);
4320 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4325 static const struct got_error *
4326 print_remove_status(void *arg, unsigned char status,
4327 unsigned char staged_status, const char *path)
4329 while (path[0] == '/')
4331 if (status == GOT_STATUS_NONEXISTENT)
4333 if (status == staged_status && (status == GOT_STATUS_DELETE))
4334 status = GOT_STATUS_NO_CHANGE;
4335 printf("%c%c %s\n", status, staged_status, path);
4339 static const struct got_error *
4340 cmd_remove(int argc, char *argv[])
4342 const struct got_error *error = NULL;
4343 struct got_worktree *worktree = NULL;
4344 struct got_repository *repo = NULL;
4346 struct got_pathlist_head paths;
4347 struct got_pathlist_entry *pe;
4348 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4352 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4355 delete_local_mods = 1;
4373 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4380 cwd = getcwd(NULL, 0);
4382 error = got_error_from_errno("getcwd");
4385 error = got_worktree_open(&worktree, cwd);
4389 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4394 error = apply_unveil(got_repo_get_path(repo), 1,
4395 got_worktree_get_root_path(worktree));
4399 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4406 TAILQ_FOREACH(pe, &paths, entry) {
4407 if (asprintf(&ondisk_path, "%s/%s",
4408 got_worktree_get_root_path(worktree),
4410 error = got_error_from_errno("asprintf");
4413 if (lstat(ondisk_path, &sb) == -1) {
4414 if (errno == ENOENT) {
4418 error = got_error_from_errno2("lstat",
4424 if (S_ISDIR(sb.st_mode)) {
4425 error = got_error_msg(GOT_ERR_BAD_PATH,
4426 "removing directories requires -R option");
4432 error = got_worktree_schedule_delete(worktree, &paths,
4433 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4438 got_repo_close(repo);
4440 got_worktree_close(worktree);
4441 TAILQ_FOREACH(pe, &paths, entry)
4442 free((char *)pe->path);
4443 got_pathlist_free(&paths);
4451 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4452 "path ...\n", getprogname());
4456 static const struct got_error *
4457 revert_progress(void *arg, unsigned char status, const char *path)
4459 while (path[0] == '/')
4461 printf("%c %s\n", status, path);
4465 struct choose_patch_arg {
4466 FILE *patch_script_file;
4470 static const struct got_error *
4471 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4472 int nchanges, const char *action)
4475 size_t linesize = 0;
4479 case GOT_STATUS_ADD:
4480 printf("A %s\n%s this addition? [y/n] ", path, action);
4482 case GOT_STATUS_DELETE:
4483 printf("D %s\n%s this deletion? [y/n] ", path, action);
4485 case GOT_STATUS_MODIFY:
4486 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4487 return got_error_from_errno("fseek");
4488 printf(GOT_COMMIT_SEP_STR);
4489 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4491 if (ferror(patch_file))
4492 return got_error_from_errno("getline");
4493 printf(GOT_COMMIT_SEP_STR);
4494 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4495 path, n, nchanges, action);
4498 return got_error_path(path, GOT_ERR_FILE_STATUS);
4504 static const struct got_error *
4505 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4506 FILE *patch_file, int n, int nchanges)
4508 const struct got_error *err = NULL;
4510 size_t linesize = 0;
4513 struct choose_patch_arg *a = arg;
4515 *choice = GOT_PATCH_CHOICE_NONE;
4517 if (a->patch_script_file) {
4519 err = show_change(status, path, patch_file, n, nchanges,
4523 linelen = getline(&line, &linesize, a->patch_script_file);
4524 if (linelen == -1) {
4525 if (ferror(a->patch_script_file))
4526 return got_error_from_errno("getline");
4529 nl = strchr(line, '\n');
4532 if (strcmp(line, "y") == 0) {
4533 *choice = GOT_PATCH_CHOICE_YES;
4535 } else if (strcmp(line, "n") == 0) {
4536 *choice = GOT_PATCH_CHOICE_NO;
4538 } else if (strcmp(line, "q") == 0 &&
4539 status == GOT_STATUS_MODIFY) {
4540 *choice = GOT_PATCH_CHOICE_QUIT;
4543 printf("invalid response '%s'\n", line);
4548 while (resp != 'y' && resp != 'n' && resp != 'q') {
4549 err = show_change(status, path, patch_file, n, nchanges,
4556 if (status == GOT_STATUS_MODIFY) {
4557 if (resp != 'y' && resp != 'n' && resp != 'q') {
4558 printf("invalid response '%c'\n", resp);
4561 } else if (resp != 'y' && resp != 'n') {
4562 printf("invalid response '%c'\n", resp);
4568 *choice = GOT_PATCH_CHOICE_YES;
4569 else if (resp == 'n')
4570 *choice = GOT_PATCH_CHOICE_NO;
4571 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4572 *choice = GOT_PATCH_CHOICE_QUIT;
4578 static const struct got_error *
4579 cmd_revert(int argc, char *argv[])
4581 const struct got_error *error = NULL;
4582 struct got_worktree *worktree = NULL;
4583 struct got_repository *repo = NULL;
4584 char *cwd = NULL, *path = NULL;
4585 struct got_pathlist_head paths;
4586 struct got_pathlist_entry *pe;
4587 int ch, can_recurse = 0, pflag = 0;
4588 FILE *patch_script_file = NULL;
4589 const char *patch_script_path = NULL;
4590 struct choose_patch_arg cpa;
4594 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4600 patch_script_path = optarg;
4615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4616 "unveil", NULL) == -1)
4621 if (patch_script_path && !pflag)
4622 errx(1, "-F option can only be used together with -p option");
4624 cwd = getcwd(NULL, 0);
4626 error = got_error_from_errno("getcwd");
4629 error = got_worktree_open(&worktree, cwd);
4633 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4638 if (patch_script_path) {
4639 patch_script_file = fopen(patch_script_path, "r");
4640 if (patch_script_file == NULL) {
4641 error = got_error_from_errno2("fopen",
4646 error = apply_unveil(got_repo_get_path(repo), 1,
4647 got_worktree_get_root_path(worktree));
4651 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4658 TAILQ_FOREACH(pe, &paths, entry) {
4659 if (asprintf(&ondisk_path, "%s/%s",
4660 got_worktree_get_root_path(worktree),
4662 error = got_error_from_errno("asprintf");
4665 if (lstat(ondisk_path, &sb) == -1) {
4666 if (errno == ENOENT) {
4670 error = got_error_from_errno2("lstat",
4676 if (S_ISDIR(sb.st_mode)) {
4677 error = got_error_msg(GOT_ERR_BAD_PATH,
4678 "reverting directories requires -R option");
4684 cpa.patch_script_file = patch_script_file;
4685 cpa.action = "revert";
4686 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4687 pflag ? choose_patch : NULL, &cpa, repo);
4691 if (patch_script_file && fclose(patch_script_file) == EOF &&
4693 error = got_error_from_errno2("fclose", patch_script_path);
4695 got_repo_close(repo);
4697 got_worktree_close(worktree);
4706 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4711 struct collect_commit_logmsg_arg {
4712 const char *cmdline_log;
4714 const char *worktree_path;
4715 const char *branch_name;
4716 const char *repo_path;
4721 static const struct got_error *
4722 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4725 char *initial_content = NULL;
4726 struct got_pathlist_entry *pe;
4727 const struct got_error *err = NULL;
4728 char *template = NULL;
4729 struct collect_commit_logmsg_arg *a = arg;
4733 /* if a message was specified on the command line, just use it */
4734 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4735 len = strlen(a->cmdline_log) + 1;
4736 *logmsg = malloc(len + 1);
4737 if (*logmsg == NULL)
4738 return got_error_from_errno("malloc");
4739 strlcpy(*logmsg, a->cmdline_log, len);
4743 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4744 return got_error_from_errno("asprintf");
4746 if (asprintf(&initial_content,
4747 "\n# changes to be committed on branch %s:\n",
4748 a->branch_name) == -1)
4749 return got_error_from_errno("asprintf");
4751 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4755 dprintf(fd, initial_content);
4757 TAILQ_FOREACH(pe, commitable_paths, entry) {
4758 struct got_commitable *ct = pe->data;
4759 dprintf(fd, "# %c %s\n",
4760 got_commitable_get_status(ct),
4761 got_commitable_get_path(ct));
4765 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4767 free(initial_content);
4770 /* Editor is done; we can now apply unveil(2) */
4772 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4781 static const struct got_error *
4782 cmd_commit(int argc, char *argv[])
4784 const struct got_error *error = NULL;
4785 struct got_worktree *worktree = NULL;
4786 struct got_repository *repo = NULL;
4787 char *cwd = NULL, *id_str = NULL;
4788 struct got_object_id *id = NULL;
4789 const char *logmsg = NULL;
4790 struct collect_commit_logmsg_arg cl_arg;
4791 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4792 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4793 struct got_pathlist_head paths;
4796 cl_arg.logmsg_path = NULL;
4798 while ((ch = getopt(argc, argv, "m:")) != -1) {
4813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4814 "unveil", NULL) == -1)
4817 cwd = getcwd(NULL, 0);
4819 error = got_error_from_errno("getcwd");
4822 error = got_worktree_open(&worktree, cwd);
4826 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4829 if (rebase_in_progress) {
4830 error = got_error(GOT_ERR_REBASING);
4834 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4839 error = get_gitconfig_path(&gitconfig_path);
4842 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4847 error = get_author(&author, repo);
4852 * unveil(2) traverses exec(2); if an editor is used we have
4853 * to apply unveil after the log message has been written.
4855 if (logmsg == NULL || strlen(logmsg) == 0)
4856 error = get_editor(&editor);
4858 error = apply_unveil(got_repo_get_path(repo), 0,
4859 got_worktree_get_root_path(worktree));
4863 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4867 cl_arg.editor = editor;
4868 cl_arg.cmdline_log = logmsg;
4869 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4870 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4871 if (!histedit_in_progress) {
4872 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4873 error = got_error(GOT_ERR_COMMIT_BRANCH);
4876 cl_arg.branch_name += 11;
4878 cl_arg.repo_path = got_repo_get_path(repo);
4879 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4880 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4882 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4883 cl_arg.logmsg_path != NULL)
4884 preserve_logmsg = 1;
4888 error = got_object_id_str(&id_str, id);
4891 printf("Created commit %s\n", id_str);
4893 if (preserve_logmsg) {
4894 fprintf(stderr, "%s: log message preserved in %s\n",
4895 getprogname(), cl_arg.logmsg_path);
4896 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4898 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4899 free(cl_arg.logmsg_path);
4901 got_repo_close(repo);
4903 got_worktree_close(worktree);
4906 free(gitconfig_path);
4913 usage_cherrypick(void)
4915 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4919 static const struct got_error *
4920 cmd_cherrypick(int argc, char *argv[])
4922 const struct got_error *error = NULL;
4923 struct got_worktree *worktree = NULL;
4924 struct got_repository *repo = NULL;
4925 char *cwd = NULL, *commit_id_str = NULL;
4926 struct got_object_id *commit_id = NULL;
4927 struct got_commit_object *commit = NULL;
4928 struct got_object_qid *pid;
4929 struct got_reference *head_ref = NULL;
4930 int ch, did_something = 0;
4932 while ((ch = getopt(argc, argv, "")) != -1) {
4944 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4945 "unveil", NULL) == -1)
4951 cwd = getcwd(NULL, 0);
4953 error = got_error_from_errno("getcwd");
4956 error = got_worktree_open(&worktree, cwd);
4960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4965 error = apply_unveil(got_repo_get_path(repo), 0,
4966 got_worktree_get_root_path(worktree));
4970 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4971 GOT_OBJ_TYPE_COMMIT, repo);
4972 if (error != NULL) {
4973 struct got_reference *ref;
4974 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4976 error = got_ref_open(&ref, repo, argv[0], 0);
4979 error = got_ref_resolve(&commit_id, repo, ref);
4984 error = got_object_id_str(&commit_id_str, commit_id);
4988 error = got_ref_open(&head_ref, repo,
4989 got_worktree_get_head_ref_name(worktree), 0);
4993 error = check_same_branch(commit_id, head_ref, NULL, repo);
4995 if (error->code != GOT_ERR_ANCESTRY)
4999 error = got_error(GOT_ERR_SAME_BRANCH);
5003 error = got_object_open_as_commit(&commit, repo, commit_id);
5006 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5007 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5008 commit_id, repo, update_progress, &did_something, check_cancelled,
5014 printf("Merged commit %s\n", commit_id_str);
5017 got_object_commit_close(commit);
5018 free(commit_id_str);
5020 got_ref_close(head_ref);
5022 got_worktree_close(worktree);
5024 got_repo_close(repo);
5031 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5035 static const struct got_error *
5036 cmd_backout(int argc, char *argv[])
5038 const struct got_error *error = NULL;
5039 struct got_worktree *worktree = NULL;
5040 struct got_repository *repo = NULL;
5041 char *cwd = NULL, *commit_id_str = NULL;
5042 struct got_object_id *commit_id = NULL;
5043 struct got_commit_object *commit = NULL;
5044 struct got_object_qid *pid;
5045 struct got_reference *head_ref = NULL;
5046 int ch, did_something = 0;
5048 while ((ch = getopt(argc, argv, "")) != -1) {
5060 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5061 "unveil", NULL) == -1)
5067 cwd = getcwd(NULL, 0);
5069 error = got_error_from_errno("getcwd");
5072 error = got_worktree_open(&worktree, cwd);
5076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5081 error = apply_unveil(got_repo_get_path(repo), 0,
5082 got_worktree_get_root_path(worktree));
5086 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5087 GOT_OBJ_TYPE_COMMIT, repo);
5088 if (error != NULL) {
5089 struct got_reference *ref;
5090 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5092 error = got_ref_open(&ref, repo, argv[0], 0);
5095 error = got_ref_resolve(&commit_id, repo, ref);
5100 error = got_object_id_str(&commit_id_str, commit_id);
5104 error = got_ref_open(&head_ref, repo,
5105 got_worktree_get_head_ref_name(worktree), 0);
5109 error = check_same_branch(commit_id, head_ref, NULL, repo);
5113 error = got_object_open_as_commit(&commit, repo, commit_id);
5116 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5118 error = got_error(GOT_ERR_ROOT_COMMIT);
5122 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5123 update_progress, &did_something, check_cancelled, NULL);
5128 printf("Backed out commit %s\n", commit_id_str);
5131 got_object_commit_close(commit);
5132 free(commit_id_str);
5134 got_ref_close(head_ref);
5136 got_worktree_close(worktree);
5138 got_repo_close(repo);
5145 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5151 trim_logmsg(char *logmsg, int limit)
5156 len = strlen(logmsg);
5160 nl = strchr(logmsg, '\n');
5165 static const struct got_error *
5166 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5168 const struct got_error *err;
5169 char *logmsg0 = NULL;
5172 err = got_object_commit_get_logmsg(&logmsg0, commit);
5177 while (isspace((unsigned char)s[0]))
5180 *logmsg = strdup(s);
5181 if (*logmsg == NULL) {
5182 err = got_error_from_errno("strdup");
5186 trim_logmsg(*logmsg, limit);
5192 static const struct got_error *
5193 show_rebase_progress(struct got_commit_object *commit,
5194 struct got_object_id *old_id, struct got_object_id *new_id)
5196 const struct got_error *err;
5197 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5199 err = got_object_id_str(&old_id_str, old_id);
5204 err = got_object_id_str(&new_id_str, new_id);
5209 old_id_str[12] = '\0';
5211 new_id_str[12] = '\0';
5213 err = get_short_logmsg(&logmsg, 42, commit);
5217 printf("%s -> %s: %s\n", old_id_str,
5218 new_id_str ? new_id_str : "no-op change", logmsg);
5225 static const struct got_error *
5226 rebase_progress(void *arg, unsigned char status, const char *path)
5228 unsigned char *rebase_status = arg;
5230 while (path[0] == '/')
5232 printf("%c %s\n", status, path);
5234 if (*rebase_status == GOT_STATUS_CONFLICT)
5236 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5237 *rebase_status = status;
5241 static const struct got_error *
5242 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5243 struct got_reference *branch, struct got_reference *new_base_branch,
5244 struct got_reference *tmp_branch, struct got_repository *repo)
5246 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5247 return got_worktree_rebase_complete(worktree, fileindex,
5248 new_base_branch, tmp_branch, branch, repo);
5251 static const struct got_error *
5252 rebase_commit(struct got_pathlist_head *merged_paths,
5253 struct got_worktree *worktree, struct got_fileindex *fileindex,
5254 struct got_reference *tmp_branch,
5255 struct got_object_id *commit_id, struct got_repository *repo)
5257 const struct got_error *error;
5258 struct got_commit_object *commit;
5259 struct got_object_id *new_commit_id;
5261 error = got_object_open_as_commit(&commit, repo, commit_id);
5265 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5266 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5268 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5270 error = show_rebase_progress(commit, commit_id, NULL);
5272 error = show_rebase_progress(commit, commit_id, new_commit_id);
5273 free(new_commit_id);
5276 got_object_commit_close(commit);
5280 struct check_path_prefix_arg {
5281 const char *path_prefix;
5286 static const struct got_error *
5287 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5288 struct got_blob_object *blob2, struct got_object_id *id1,
5289 struct got_object_id *id2, const char *path1, const char *path2,
5290 mode_t mode1, mode_t mode2, struct got_repository *repo)
5292 struct check_path_prefix_arg *a = arg;
5294 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5295 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5296 return got_error(a->errcode);
5301 static const struct got_error *
5302 check_path_prefix(struct got_object_id *parent_id,
5303 struct got_object_id *commit_id, const char *path_prefix,
5304 int errcode, struct got_repository *repo)
5306 const struct got_error *err;
5307 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5308 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5309 struct check_path_prefix_arg cpp_arg;
5311 if (got_path_is_root_dir(path_prefix))
5314 err = got_object_open_as_commit(&commit, repo, commit_id);
5318 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5322 err = got_object_open_as_tree(&tree1, repo,
5323 got_object_commit_get_tree_id(parent_commit));
5327 err = got_object_open_as_tree(&tree2, repo,
5328 got_object_commit_get_tree_id(commit));
5332 cpp_arg.path_prefix = path_prefix;
5333 while (cpp_arg.path_prefix[0] == '/')
5334 cpp_arg.path_prefix++;
5335 cpp_arg.len = strlen(cpp_arg.path_prefix);
5336 cpp_arg.errcode = errcode;
5337 err = got_diff_tree(tree1, tree2, "", "", repo,
5338 check_path_prefix_in_diff, &cpp_arg, 0);
5341 got_object_tree_close(tree1);
5343 got_object_tree_close(tree2);
5345 got_object_commit_close(commit);
5347 got_object_commit_close(parent_commit);
5351 static const struct got_error *
5352 collect_commits(struct got_object_id_queue *commits,
5353 struct got_object_id *initial_commit_id,
5354 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5355 const char *path_prefix, int path_prefix_errcode,
5356 struct got_repository *repo)
5358 const struct got_error *err = NULL;
5359 struct got_commit_graph *graph = NULL;
5360 struct got_object_id *parent_id = NULL;
5361 struct got_object_qid *qid;
5362 struct got_object_id *commit_id = initial_commit_id;
5364 err = got_commit_graph_open(&graph, "/", 1);
5368 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5369 check_cancelled, NULL);
5372 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5373 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5374 check_cancelled, NULL);
5376 if (err->code == GOT_ERR_ITER_COMPLETED) {
5377 err = got_error_msg(GOT_ERR_ANCESTRY,
5378 "ran out of commits to rebase before "
5379 "youngest common ancestor commit has "
5384 err = check_path_prefix(parent_id, commit_id,
5385 path_prefix, path_prefix_errcode, repo);
5389 err = got_object_qid_alloc(&qid, commit_id);
5392 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5393 commit_id = parent_id;
5397 got_commit_graph_close(graph);
5401 static const struct got_error *
5402 cmd_rebase(int argc, char *argv[])
5404 const struct got_error *error = NULL;
5405 struct got_worktree *worktree = NULL;
5406 struct got_repository *repo = NULL;
5407 struct got_fileindex *fileindex = NULL;
5409 struct got_reference *branch = NULL;
5410 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5411 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5412 struct got_object_id *resume_commit_id = NULL;
5413 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5414 struct got_commit_object *commit = NULL;
5415 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5416 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5417 struct got_object_id_queue commits;
5418 struct got_pathlist_head merged_paths;
5419 const struct got_object_id_queue *parent_ids;
5420 struct got_object_qid *qid, *pid;
5422 SIMPLEQ_INIT(&commits);
5423 TAILQ_INIT(&merged_paths);
5425 while ((ch = getopt(argc, argv, "ac")) != -1) {
5431 continue_rebase = 1;
5443 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5444 "unveil", NULL) == -1)
5447 if (abort_rebase && continue_rebase)
5449 else if (abort_rebase || continue_rebase) {
5452 } else if (argc != 1)
5455 cwd = getcwd(NULL, 0);
5457 error = got_error_from_errno("getcwd");
5460 error = got_worktree_open(&worktree, cwd);
5464 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5469 error = apply_unveil(got_repo_get_path(repo), 0,
5470 got_worktree_get_root_path(worktree));
5474 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5480 if (!rebase_in_progress) {
5481 error = got_error(GOT_ERR_NOT_REBASING);
5484 error = got_worktree_rebase_continue(&resume_commit_id,
5485 &new_base_branch, &tmp_branch, &branch, &fileindex,
5489 printf("Switching work tree to %s\n",
5490 got_ref_get_symref_target(new_base_branch));
5491 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5492 new_base_branch, update_progress, &did_something);
5495 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5496 goto done; /* nothing else to do */
5499 if (continue_rebase) {
5500 if (!rebase_in_progress) {
5501 error = got_error(GOT_ERR_NOT_REBASING);
5504 error = got_worktree_rebase_continue(&resume_commit_id,
5505 &new_base_branch, &tmp_branch, &branch, &fileindex,
5510 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5511 resume_commit_id, repo);
5515 yca_id = got_object_id_dup(resume_commit_id);
5516 if (yca_id == NULL) {
5517 error = got_error_from_errno("got_object_id_dup");
5521 error = got_ref_open(&branch, repo, argv[0], 0);
5526 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5530 if (!continue_rebase) {
5531 struct got_object_id *base_commit_id;
5533 base_commit_id = got_worktree_get_base_commit_id(worktree);
5534 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5535 base_commit_id, branch_head_commit_id, repo,
5536 check_cancelled, NULL);
5539 if (yca_id == NULL) {
5540 error = got_error_msg(GOT_ERR_ANCESTRY,
5541 "specified branch shares no common ancestry "
5542 "with work tree's branch");
5546 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5548 if (error->code != GOT_ERR_ANCESTRY)
5552 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5553 "specified branch resolves to a commit which "
5554 "is already contained in work tree's branch");
5557 error = got_worktree_rebase_prepare(&new_base_branch,
5558 &tmp_branch, &fileindex, worktree, branch, repo);
5563 commit_id = branch_head_commit_id;
5564 error = got_object_open_as_commit(&commit, repo, commit_id);
5568 parent_ids = got_object_commit_get_parent_ids(commit);
5569 pid = SIMPLEQ_FIRST(parent_ids);
5571 if (!continue_rebase) {
5573 error = got_worktree_rebase_abort(worktree, fileindex,
5574 repo, new_base_branch, update_progress,
5578 printf("Rebase of %s aborted\n",
5579 got_ref_get_name(branch));
5581 error = got_error(GOT_ERR_EMPTY_REBASE);
5584 error = collect_commits(&commits, commit_id, pid->id,
5585 yca_id, got_worktree_get_path_prefix(worktree),
5586 GOT_ERR_REBASE_PATH, repo);
5587 got_object_commit_close(commit);
5592 if (SIMPLEQ_EMPTY(&commits)) {
5593 if (continue_rebase) {
5594 error = rebase_complete(worktree, fileindex,
5595 branch, new_base_branch, tmp_branch, repo);
5598 /* Fast-forward the reference of the branch. */
5599 struct got_object_id *new_head_commit_id;
5601 error = got_ref_resolve(&new_head_commit_id, repo,
5605 error = got_object_id_str(&id_str, new_head_commit_id);
5606 printf("Forwarding %s to commit %s\n",
5607 got_ref_get_name(branch), id_str);
5609 error = got_ref_change_ref(branch,
5610 new_head_commit_id);
5617 SIMPLEQ_FOREACH(qid, &commits, entry) {
5618 commit_id = qid->id;
5619 parent_id = pid ? pid->id : yca_id;
5622 error = got_worktree_rebase_merge_files(&merged_paths,
5623 worktree, fileindex, parent_id, commit_id, repo,
5624 rebase_progress, &rebase_status, check_cancelled, NULL);
5628 if (rebase_status == GOT_STATUS_CONFLICT) {
5629 got_worktree_rebase_pathlist_free(&merged_paths);
5633 error = rebase_commit(&merged_paths, worktree, fileindex,
5634 tmp_branch, commit_id, repo);
5635 got_worktree_rebase_pathlist_free(&merged_paths);
5640 if (rebase_status == GOT_STATUS_CONFLICT) {
5641 error = got_worktree_rebase_postpone(worktree, fileindex);
5644 error = got_error_msg(GOT_ERR_CONFLICTS,
5645 "conflicts must be resolved before rebasing can continue");
5647 error = rebase_complete(worktree, fileindex, branch,
5648 new_base_branch, tmp_branch, repo);
5650 got_object_id_queue_free(&commits);
5651 free(branch_head_commit_id);
5652 free(resume_commit_id);
5655 got_object_commit_close(commit);
5657 got_ref_close(branch);
5658 if (new_base_branch)
5659 got_ref_close(new_base_branch);
5661 got_ref_close(tmp_branch);
5663 got_worktree_close(worktree);
5665 got_repo_close(repo);
5670 usage_histedit(void)
5672 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5677 #define GOT_HISTEDIT_PICK 'p'
5678 #define GOT_HISTEDIT_EDIT 'e'
5679 #define GOT_HISTEDIT_FOLD 'f'
5680 #define GOT_HISTEDIT_DROP 'd'
5681 #define GOT_HISTEDIT_MESG 'm'
5683 static struct got_histedit_cmd {
5687 } got_histedit_cmds[] = {
5688 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5689 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5690 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5691 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5692 { GOT_HISTEDIT_MESG, "mesg",
5693 "single-line log message for commit above (open editor if empty)" },
5696 struct got_histedit_list_entry {
5697 TAILQ_ENTRY(got_histedit_list_entry) entry;
5698 struct got_object_id *commit_id;
5699 const struct got_histedit_cmd *cmd;
5702 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5704 static const struct got_error *
5705 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5706 FILE *f, struct got_repository *repo)
5708 const struct got_error *err = NULL;
5709 char *logmsg = NULL, *id_str = NULL;
5710 struct got_commit_object *commit = NULL;
5713 err = got_object_open_as_commit(&commit, repo, commit_id);
5717 err = get_short_logmsg(&logmsg, 34, commit);
5721 err = got_object_id_str(&id_str, commit_id);
5725 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5727 err = got_ferror(f, GOT_ERR_IO);
5730 got_object_commit_close(commit);
5736 static const struct got_error *
5737 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5738 struct got_repository *repo)
5740 const struct got_error *err = NULL;
5741 struct got_object_qid *qid;
5743 if (SIMPLEQ_EMPTY(commits))
5744 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5746 SIMPLEQ_FOREACH(qid, commits, entry) {
5747 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5756 static const struct got_error *
5757 write_cmd_list(FILE *f)
5759 const struct got_error *err = NULL;
5762 n = fprintf(f, "# Available histedit commands:\n");
5764 return got_ferror(f, GOT_ERR_IO);
5766 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5767 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5768 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5771 err = got_ferror(f, GOT_ERR_IO);
5775 n = fprintf(f, "# Commits will be processed in order from top to "
5776 "bottom of this file.\n");
5778 return got_ferror(f, GOT_ERR_IO);
5782 static const struct got_error *
5783 histedit_syntax_error(int lineno)
5785 static char msg[42];
5788 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5790 if (ret == -1 || ret >= sizeof(msg))
5791 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5793 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5796 static const struct got_error *
5797 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5798 char *logmsg, struct got_repository *repo)
5800 const struct got_error *err;
5801 struct got_commit_object *folded_commit = NULL;
5802 char *id_str, *folded_logmsg = NULL;
5804 err = got_object_id_str(&id_str, hle->commit_id);
5808 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5812 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5815 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5816 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5817 folded_logmsg) == -1) {
5818 err = got_error_from_errno("asprintf");
5823 got_object_commit_close(folded_commit);
5825 free(folded_logmsg);
5829 static struct got_histedit_list_entry *
5830 get_folded_commits(struct got_histedit_list_entry *hle)
5832 struct got_histedit_list_entry *prev, *folded = NULL;
5834 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5835 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5836 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5837 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5839 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5845 static const struct got_error *
5846 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5847 struct got_repository *repo)
5849 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5850 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5851 const struct got_error *err = NULL;
5852 struct got_commit_object *commit = NULL;
5854 struct got_histedit_list_entry *folded = NULL;
5856 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5860 folded = get_folded_commits(hle);
5862 while (folded != hle) {
5863 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5864 folded = TAILQ_NEXT(folded, entry);
5867 err = append_folded_commit_msg(&new_msg, folded,
5873 folded = TAILQ_NEXT(folded, entry);
5877 err = got_object_id_str(&id_str, hle->commit_id);
5880 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5883 if (asprintf(&new_msg,
5884 "%s\n# original log message of commit %s: %s",
5885 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5886 err = got_error_from_errno("asprintf");
5892 err = got_object_id_str(&id_str, hle->commit_id);
5896 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5900 dprintf(fd, logmsg);
5903 err = get_editor(&editor);
5907 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5909 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5911 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5914 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5915 err = got_error_from_errno2("unlink", logmsg_path);
5921 got_object_commit_close(commit);
5925 static const struct got_error *
5926 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5927 FILE *f, struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 char *line = NULL, *p, *end;
5934 const struct got_histedit_cmd *cmd;
5935 struct got_object_id *commit_id = NULL;
5936 struct got_histedit_list_entry *hle = NULL;
5939 len = getline(&line, &size, f);
5941 const struct got_error *getline_err;
5944 getline_err = got_error_from_errno("getline");
5945 err = got_ferror(f, getline_err->code);
5950 while (isspace((unsigned char)p[0]))
5952 if (p[0] == '#' || p[0] == '\0') {
5958 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5959 cmd = &got_histedit_cmds[i];
5960 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5961 isspace((unsigned char)p[strlen(cmd->name)])) {
5962 p += strlen(cmd->name);
5965 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5970 if (i == nitems(got_histedit_cmds)) {
5971 err = histedit_syntax_error(lineno);
5974 while (isspace((unsigned char)p[0]))
5976 if (cmd->code == GOT_HISTEDIT_MESG) {
5977 if (hle == NULL || hle->logmsg != NULL) {
5978 err = got_error(GOT_ERR_HISTEDIT_CMD);
5982 err = histedit_edit_logmsg(hle, repo);
5986 hle->logmsg = strdup(p);
5987 if (hle->logmsg == NULL) {
5988 err = got_error_from_errno("strdup");
5997 while (end[0] && !isspace((unsigned char)end[0]))
6001 err = got_object_resolve_id_str(&commit_id, repo, p);
6003 /* override error code */
6004 err = histedit_syntax_error(lineno);
6008 hle = malloc(sizeof(*hle));
6010 err = got_error_from_errno("malloc");
6014 hle->commit_id = commit_id;
6019 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6027 static const struct got_error *
6028 histedit_check_script(struct got_histedit_list *histedit_cmds,
6029 struct got_object_id_queue *commits, struct got_repository *repo)
6031 const struct got_error *err = NULL;
6032 struct got_object_qid *qid;
6033 struct got_histedit_list_entry *hle;
6034 static char msg[80];
6037 if (TAILQ_EMPTY(histedit_cmds))
6038 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6039 "histedit script contains no commands");
6040 if (SIMPLEQ_EMPTY(commits))
6041 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6043 SIMPLEQ_FOREACH(qid, commits, entry) {
6044 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6045 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6049 err = got_object_id_str(&id_str, qid->id);
6052 snprintf(msg, sizeof(msg),
6053 "commit %s missing from histedit script", id_str);
6055 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6059 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6060 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6061 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6062 "last commit in histedit script cannot be folded");
6067 static const struct got_error *
6068 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6069 const char *path, struct got_object_id_queue *commits,
6070 struct got_repository *repo)
6072 const struct got_error *err = NULL;
6076 err = get_editor(&editor);
6080 if (spawn_editor(editor, path) == -1) {
6081 err = got_error_from_errno("failed spawning editor");
6085 f = fopen(path, "r");
6087 err = got_error_from_errno("fopen");
6090 err = histedit_parse_list(histedit_cmds, f, repo);
6094 err = histedit_check_script(histedit_cmds, commits, repo);
6096 if (f && fclose(f) != 0 && err == NULL)
6097 err = got_error_from_errno("fclose");
6102 static const struct got_error *
6103 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6104 struct got_object_id_queue *, const char *, struct got_repository *);
6106 static const struct got_error *
6107 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6108 struct got_object_id_queue *commits, struct got_repository *repo)
6110 const struct got_error *err;
6114 err = got_opentemp_named(&path, &f, "got-histedit");
6118 err = write_cmd_list(f);
6122 err = histedit_write_commit_list(commits, f, repo);
6126 if (fclose(f) != 0) {
6127 err = got_error_from_errno("fclose");
6132 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6134 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6135 err->code != GOT_ERR_HISTEDIT_CMD)
6137 err = histedit_edit_list_retry(histedit_cmds, err,
6138 commits, path, repo);
6141 if (f && fclose(f) != 0 && err == NULL)
6142 err = got_error_from_errno("fclose");
6143 if (path && unlink(path) != 0 && err == NULL)
6144 err = got_error_from_errno2("unlink", path);
6149 static const struct got_error *
6150 histedit_save_list(struct got_histedit_list *histedit_cmds,
6151 struct got_worktree *worktree, struct got_repository *repo)
6153 const struct got_error *err = NULL;
6156 struct got_histedit_list_entry *hle;
6157 struct got_commit_object *commit = NULL;
6159 err = got_worktree_get_histedit_script_path(&path, worktree);
6163 f = fopen(path, "w");
6165 err = got_error_from_errno2("fopen", path);
6168 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6169 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6175 int n = fprintf(f, "%c %s\n",
6176 GOT_HISTEDIT_MESG, hle->logmsg);
6178 err = got_ferror(f, GOT_ERR_IO);
6184 if (f && fclose(f) != 0 && err == NULL)
6185 err = got_error_from_errno("fclose");
6188 got_object_commit_close(commit);
6193 histedit_free_list(struct got_histedit_list *histedit_cmds)
6195 struct got_histedit_list_entry *hle;
6197 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6198 TAILQ_REMOVE(histedit_cmds, hle, entry);
6203 static const struct got_error *
6204 histedit_load_list(struct got_histedit_list *histedit_cmds,
6205 const char *path, struct got_repository *repo)
6207 const struct got_error *err = NULL;
6210 f = fopen(path, "r");
6212 err = got_error_from_errno2("fopen", path);
6216 err = histedit_parse_list(histedit_cmds, f, repo);
6218 if (f && fclose(f) != 0 && err == NULL)
6219 err = got_error_from_errno("fclose");
6223 static const struct got_error *
6224 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6225 const struct got_error *edit_err, struct got_object_id_queue *commits,
6226 const char *path, struct got_repository *repo)
6228 const struct got_error *err = NULL, *prev_err = edit_err;
6231 while (resp != 'c' && resp != 'r' && resp != 'a') {
6232 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6233 "or (a)bort: ", getprogname(), prev_err->msg);
6238 histedit_free_list(histedit_cmds);
6239 err = histedit_run_editor(histedit_cmds, path, commits,
6242 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6243 err->code != GOT_ERR_HISTEDIT_CMD)
6250 } else if (resp == 'r') {
6251 histedit_free_list(histedit_cmds);
6252 err = histedit_edit_script(histedit_cmds,
6255 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6256 err->code != GOT_ERR_HISTEDIT_CMD)
6263 } else if (resp == 'a') {
6264 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6267 printf("invalid response '%c'\n", resp);
6273 static const struct got_error *
6274 histedit_complete(struct got_worktree *worktree,
6275 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6276 struct got_reference *branch, struct got_repository *repo)
6278 printf("Switching work tree to %s\n",
6279 got_ref_get_symref_target(branch));
6280 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6284 static const struct got_error *
6285 show_histedit_progress(struct got_commit_object *commit,
6286 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6288 const struct got_error *err;
6289 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6291 err = got_object_id_str(&old_id_str, hle->commit_id);
6296 err = got_object_id_str(&new_id_str, new_id);
6301 old_id_str[12] = '\0';
6303 new_id_str[12] = '\0';
6306 logmsg = strdup(hle->logmsg);
6307 if (logmsg == NULL) {
6308 err = got_error_from_errno("strdup");
6311 trim_logmsg(logmsg, 42);
6313 err = get_short_logmsg(&logmsg, 42, commit);
6318 switch (hle->cmd->code) {
6319 case GOT_HISTEDIT_PICK:
6320 case GOT_HISTEDIT_EDIT:
6321 printf("%s -> %s: %s\n", old_id_str,
6322 new_id_str ? new_id_str : "no-op change", logmsg);
6324 case GOT_HISTEDIT_DROP:
6325 case GOT_HISTEDIT_FOLD:
6326 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6339 static const struct got_error *
6340 histedit_commit(struct got_pathlist_head *merged_paths,
6341 struct got_worktree *worktree, struct got_fileindex *fileindex,
6342 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6343 struct got_repository *repo)
6345 const struct got_error *err;
6346 struct got_commit_object *commit;
6347 struct got_object_id *new_commit_id;
6349 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6350 && hle->logmsg == NULL) {
6351 err = histedit_edit_logmsg(hle, repo);
6356 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6360 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6361 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6364 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6366 err = show_histedit_progress(commit, hle, NULL);
6368 err = show_histedit_progress(commit, hle, new_commit_id);
6369 free(new_commit_id);
6372 got_object_commit_close(commit);
6376 static const struct got_error *
6377 histedit_skip_commit(struct got_histedit_list_entry *hle,
6378 struct got_worktree *worktree, struct got_repository *repo)
6380 const struct got_error *error;
6381 struct got_commit_object *commit;
6383 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6388 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6392 error = show_histedit_progress(commit, hle, NULL);
6393 got_object_commit_close(commit);
6397 static const struct got_error *
6398 cmd_histedit(int argc, char *argv[])
6400 const struct got_error *error = NULL;
6401 struct got_worktree *worktree = NULL;
6402 struct got_fileindex *fileindex = NULL;
6403 struct got_repository *repo = NULL;
6405 struct got_reference *branch = NULL;
6406 struct got_reference *tmp_branch = NULL;
6407 struct got_object_id *resume_commit_id = NULL;
6408 struct got_object_id *base_commit_id = NULL;
6409 struct got_object_id *head_commit_id = NULL;
6410 struct got_commit_object *commit = NULL;
6411 int ch, rebase_in_progress = 0, did_something;
6412 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6413 const char *edit_script_path = NULL;
6414 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6415 struct got_object_id_queue commits;
6416 struct got_pathlist_head merged_paths;
6417 const struct got_object_id_queue *parent_ids;
6418 struct got_object_qid *pid;
6419 struct got_histedit_list histedit_cmds;
6420 struct got_histedit_list_entry *hle;
6422 SIMPLEQ_INIT(&commits);
6423 TAILQ_INIT(&histedit_cmds);
6424 TAILQ_INIT(&merged_paths);
6426 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6435 edit_script_path = optarg;
6447 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6448 "unveil", NULL) == -1)
6451 if (abort_edit && continue_edit)
6457 * This command cannot apply unveil(2) in all cases because the
6458 * user may choose to run an editor to edit the histedit script
6459 * and to edit individual commit log messages.
6460 * unveil(2) traverses exec(2); if an editor is used we have to
6461 * apply unveil after edit script and log messages have been written.
6462 * XXX TODO: Make use of unveil(2) where possible.
6465 cwd = getcwd(NULL, 0);
6467 error = got_error_from_errno("getcwd");
6470 error = got_worktree_open(&worktree, cwd);
6474 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6479 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6482 if (rebase_in_progress) {
6483 error = got_error(GOT_ERR_REBASING);
6487 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6491 if (edit_in_progress && abort_edit) {
6492 error = got_worktree_histedit_continue(&resume_commit_id,
6493 &tmp_branch, &branch, &base_commit_id, &fileindex,
6497 printf("Switching work tree to %s\n",
6498 got_ref_get_symref_target(branch));
6499 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6500 branch, base_commit_id, update_progress, &did_something);
6503 printf("Histedit of %s aborted\n",
6504 got_ref_get_symref_target(branch));
6505 goto done; /* nothing else to do */
6506 } else if (abort_edit) {
6507 error = got_error(GOT_ERR_NOT_HISTEDIT);
6511 if (continue_edit) {
6514 if (!edit_in_progress) {
6515 error = got_error(GOT_ERR_NOT_HISTEDIT);
6519 error = got_worktree_get_histedit_script_path(&path, worktree);
6523 error = histedit_load_list(&histedit_cmds, path, repo);
6528 error = got_worktree_histedit_continue(&resume_commit_id,
6529 &tmp_branch, &branch, &base_commit_id, &fileindex,
6534 error = got_ref_resolve(&head_commit_id, repo, branch);
6538 error = got_object_open_as_commit(&commit, repo,
6542 parent_ids = got_object_commit_get_parent_ids(commit);
6543 pid = SIMPLEQ_FIRST(parent_ids);
6545 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6548 error = collect_commits(&commits, head_commit_id, pid->id,
6549 base_commit_id, got_worktree_get_path_prefix(worktree),
6550 GOT_ERR_HISTEDIT_PATH, repo);
6551 got_object_commit_close(commit);
6556 if (edit_in_progress) {
6557 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6561 error = got_ref_open(&branch, repo,
6562 got_worktree_get_head_ref_name(worktree), 0);
6566 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6567 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6568 "will not edit commit history of a branch outside "
6569 "the \"refs/heads/\" reference namespace");
6573 error = got_ref_resolve(&head_commit_id, repo, branch);
6574 got_ref_close(branch);
6579 error = got_object_open_as_commit(&commit, repo,
6583 parent_ids = got_object_commit_get_parent_ids(commit);
6584 pid = SIMPLEQ_FIRST(parent_ids);
6586 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6589 error = collect_commits(&commits, head_commit_id, pid->id,
6590 got_worktree_get_base_commit_id(worktree),
6591 got_worktree_get_path_prefix(worktree),
6592 GOT_ERR_HISTEDIT_PATH, repo);
6593 got_object_commit_close(commit);
6598 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6599 &base_commit_id, &fileindex, worktree, repo);
6603 if (edit_script_path) {
6604 error = histedit_load_list(&histedit_cmds,
6605 edit_script_path, repo);
6607 got_worktree_histedit_abort(worktree, fileindex,
6608 repo, branch, base_commit_id,
6609 update_progress, &did_something);
6613 error = histedit_edit_script(&histedit_cmds, &commits,
6616 got_worktree_histedit_abort(worktree, fileindex,
6617 repo, branch, base_commit_id,
6618 update_progress, &did_something);
6624 error = histedit_save_list(&histedit_cmds, worktree,
6627 got_worktree_histedit_abort(worktree, fileindex,
6628 repo, branch, base_commit_id,
6629 update_progress, &did_something);
6635 error = histedit_check_script(&histedit_cmds, &commits, repo);
6639 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6640 if (resume_commit_id) {
6641 if (got_object_id_cmp(hle->commit_id,
6642 resume_commit_id) != 0)
6645 resume_commit_id = NULL;
6646 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6647 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6648 error = histedit_skip_commit(hle, worktree,
6651 error = histedit_commit(NULL, worktree,
6652 fileindex, tmp_branch, hle, repo);
6659 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6660 error = histedit_skip_commit(hle, worktree, repo);
6666 error = got_object_open_as_commit(&commit, repo,
6670 parent_ids = got_object_commit_get_parent_ids(commit);
6671 pid = SIMPLEQ_FIRST(parent_ids);
6673 error = got_worktree_histedit_merge_files(&merged_paths,
6674 worktree, fileindex, pid->id, hle->commit_id, repo,
6675 rebase_progress, &rebase_status, check_cancelled, NULL);
6678 got_object_commit_close(commit);
6681 if (rebase_status == GOT_STATUS_CONFLICT) {
6682 got_worktree_rebase_pathlist_free(&merged_paths);
6686 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6688 error = got_object_id_str(&id_str, hle->commit_id);
6691 printf("Stopping histedit for amending commit %s\n",
6694 got_worktree_rebase_pathlist_free(&merged_paths);
6695 error = got_worktree_histedit_postpone(worktree,
6700 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6701 error = histedit_skip_commit(hle, worktree, repo);
6707 error = histedit_commit(&merged_paths, worktree, fileindex,
6708 tmp_branch, hle, repo);
6709 got_worktree_rebase_pathlist_free(&merged_paths);
6714 if (rebase_status == GOT_STATUS_CONFLICT) {
6715 error = got_worktree_histedit_postpone(worktree, fileindex);
6718 error = got_error_msg(GOT_ERR_CONFLICTS,
6719 "conflicts must be resolved before rebasing can continue");
6721 error = histedit_complete(worktree, fileindex, tmp_branch,
6724 got_object_id_queue_free(&commits);
6725 histedit_free_list(&histedit_cmds);
6726 free(head_commit_id);
6727 free(base_commit_id);
6728 free(resume_commit_id);
6730 got_object_commit_close(commit);
6732 got_ref_close(branch);
6734 got_ref_close(tmp_branch);
6736 got_worktree_close(worktree);
6738 got_repo_close(repo);
6743 usage_integrate(void)
6745 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6749 static const struct got_error *
6750 cmd_integrate(int argc, char *argv[])
6752 const struct got_error *error = NULL;
6753 struct got_repository *repo = NULL;
6754 struct got_worktree *worktree = NULL;
6755 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6756 const char *branch_arg = NULL;
6757 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6758 struct got_fileindex *fileindex = NULL;
6759 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6760 int ch, did_something = 0;
6762 while ((ch = getopt(argc, argv, "")) != -1) {
6775 branch_arg = argv[0];
6777 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6778 "unveil", NULL) == -1)
6781 cwd = getcwd(NULL, 0);
6783 error = got_error_from_errno("getcwd");
6787 error = got_worktree_open(&worktree, cwd);
6791 error = check_rebase_or_histedit_in_progress(worktree);
6795 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6800 error = apply_unveil(got_repo_get_path(repo), 0,
6801 got_worktree_get_root_path(worktree));
6805 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6806 error = got_error_from_errno("asprintf");
6810 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6811 &base_branch_ref, worktree, refname, repo);
6815 refname = strdup(got_ref_get_name(branch_ref));
6816 if (refname == NULL) {
6817 error = got_error_from_errno("strdup");
6818 got_worktree_integrate_abort(worktree, fileindex, repo,
6819 branch_ref, base_branch_ref);
6822 base_refname = strdup(got_ref_get_name(base_branch_ref));
6823 if (base_refname == NULL) {
6824 error = got_error_from_errno("strdup");
6825 got_worktree_integrate_abort(worktree, fileindex, repo,
6826 branch_ref, base_branch_ref);
6830 error = got_ref_resolve(&commit_id, repo, branch_ref);
6834 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6838 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6839 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6840 "specified branch has already been integrated");
6841 got_worktree_integrate_abort(worktree, fileindex, repo,
6842 branch_ref, base_branch_ref);
6846 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6848 if (error->code == GOT_ERR_ANCESTRY)
6849 error = got_error(GOT_ERR_REBASE_REQUIRED);
6850 got_worktree_integrate_abort(worktree, fileindex, repo,
6851 branch_ref, base_branch_ref);
6855 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6856 branch_ref, base_branch_ref, update_progress, &did_something,
6857 check_cancelled, NULL);
6861 printf("Integrated %s into %s\n", refname, base_refname);
6864 got_repo_close(repo);
6866 got_worktree_close(worktree);
6868 free(base_commit_id);
6878 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6879 "[file-path ...]\n",
6884 static const struct got_error *
6885 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6886 const char *path, struct got_object_id *blob_id,
6887 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6888 int dirfd, const char *de_name)
6890 const struct got_error *err = NULL;
6891 char *id_str = NULL;
6893 if (staged_status != GOT_STATUS_ADD &&
6894 staged_status != GOT_STATUS_MODIFY &&
6895 staged_status != GOT_STATUS_DELETE)
6898 if (staged_status == GOT_STATUS_ADD ||
6899 staged_status == GOT_STATUS_MODIFY)
6900 err = got_object_id_str(&id_str, staged_blob_id);
6902 err = got_object_id_str(&id_str, blob_id);
6906 printf("%s %c %s\n", id_str, staged_status, path);
6911 static const struct got_error *
6912 cmd_stage(int argc, char *argv[])
6914 const struct got_error *error = NULL;
6915 struct got_repository *repo = NULL;
6916 struct got_worktree *worktree = NULL;
6918 struct got_pathlist_head paths;
6919 struct got_pathlist_entry *pe;
6920 int ch, list_stage = 0, pflag = 0;
6921 FILE *patch_script_file = NULL;
6922 const char *patch_script_path = NULL;
6923 struct choose_patch_arg cpa;
6927 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6936 patch_script_path = optarg;
6948 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6949 "unveil", NULL) == -1)
6952 if (list_stage && (pflag || patch_script_path))
6953 errx(1, "-l option cannot be used with other options");
6954 if (patch_script_path && !pflag)
6955 errx(1, "-F option can only be used together with -p option");
6957 cwd = getcwd(NULL, 0);
6959 error = got_error_from_errno("getcwd");
6963 error = got_worktree_open(&worktree, cwd);
6967 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6972 if (patch_script_path) {
6973 patch_script_file = fopen(patch_script_path, "r");
6974 if (patch_script_file == NULL) {
6975 error = got_error_from_errno2("fopen",
6980 error = apply_unveil(got_repo_get_path(repo), 0,
6981 got_worktree_get_root_path(worktree));
6985 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6990 error = got_worktree_status(worktree, &paths, repo,
6991 print_stage, NULL, check_cancelled, NULL);
6993 cpa.patch_script_file = patch_script_file;
6994 cpa.action = "stage";
6995 error = got_worktree_stage(worktree, &paths,
6996 pflag ? NULL : print_status, NULL,
6997 pflag ? choose_patch : NULL, &cpa, repo);
7000 if (patch_script_file && fclose(patch_script_file) == EOF &&
7002 error = got_error_from_errno2("fclose", patch_script_path);
7004 got_repo_close(repo);
7006 got_worktree_close(worktree);
7007 TAILQ_FOREACH(pe, &paths, entry)
7008 free((char *)pe->path);
7009 got_pathlist_free(&paths);
7017 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7018 "[file-path ...]\n",
7024 static const struct got_error *
7025 cmd_unstage(int argc, char *argv[])
7027 const struct got_error *error = NULL;
7028 struct got_repository *repo = NULL;
7029 struct got_worktree *worktree = NULL;
7031 struct got_pathlist_head paths;
7032 struct got_pathlist_entry *pe;
7033 int ch, did_something = 0, pflag = 0;
7034 FILE *patch_script_file = NULL;
7035 const char *patch_script_path = NULL;
7036 struct choose_patch_arg cpa;
7040 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7046 patch_script_path = optarg;
7058 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7059 "unveil", NULL) == -1)
7062 if (patch_script_path && !pflag)
7063 errx(1, "-F option can only be used together with -p option");
7065 cwd = getcwd(NULL, 0);
7067 error = got_error_from_errno("getcwd");
7071 error = got_worktree_open(&worktree, cwd);
7075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7080 if (patch_script_path) {
7081 patch_script_file = fopen(patch_script_path, "r");
7082 if (patch_script_file == NULL) {
7083 error = got_error_from_errno2("fopen",
7089 error = apply_unveil(got_repo_get_path(repo), 0,
7090 got_worktree_get_root_path(worktree));
7094 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7098 cpa.patch_script_file = patch_script_file;
7099 cpa.action = "unstage";
7100 error = got_worktree_unstage(worktree, &paths, update_progress,
7101 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7103 if (patch_script_file && fclose(patch_script_file) == EOF &&
7105 error = got_error_from_errno2("fclose", patch_script_path);
7107 got_repo_close(repo);
7109 got_worktree_close(worktree);
7110 TAILQ_FOREACH(pe, &paths, entry)
7111 free((char *)pe->path);
7112 got_pathlist_free(&paths);
7120 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7121 "arg1 [arg2 ...]\n", getprogname());
7125 static const struct got_error *
7126 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7128 const struct got_error *err;
7129 struct got_blob_object *blob;
7131 err = got_object_open_as_blob(&blob, repo, id, 8192);
7135 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7136 got_object_blob_close(blob);
7140 static const struct got_error *
7141 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7143 const struct got_error *err;
7144 struct got_tree_object *tree;
7147 err = got_object_open_as_tree(&tree, repo, id);
7151 nentries = got_object_tree_get_nentries(tree);
7152 for (i = 0; i < nentries; i++) {
7153 struct got_tree_entry *te;
7155 if (sigint_received || sigpipe_received)
7157 te = got_object_tree_get_entry(tree, i);
7158 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7161 fprintf(outfile, "%s %.7o %s\n", id_str,
7162 got_tree_entry_get_mode(te),
7163 got_tree_entry_get_name(te));
7167 got_object_tree_close(tree);
7171 static const struct got_error *
7172 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7174 const struct got_error *err;
7175 struct got_commit_object *commit;
7176 const struct got_object_id_queue *parent_ids;
7177 struct got_object_qid *pid;
7178 char *id_str = NULL;
7179 const char *logmsg = NULL;
7181 err = got_object_open_as_commit(&commit, repo, id);
7185 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7189 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7190 parent_ids = got_object_commit_get_parent_ids(commit);
7191 fprintf(outfile, "numparents %d\n",
7192 got_object_commit_get_nparents(commit));
7193 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7195 err = got_object_id_str(&pid_str, pid->id);
7198 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7201 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7202 got_object_commit_get_author(commit),
7203 got_object_commit_get_author_time(commit));
7205 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7206 got_object_commit_get_author(commit),
7207 got_object_commit_get_committer_time(commit));
7209 logmsg = got_object_commit_get_logmsg_raw(commit);
7210 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7211 fprintf(outfile, "%s", logmsg);
7214 got_object_commit_close(commit);
7218 static const struct got_error *
7219 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7221 const struct got_error *err;
7222 struct got_tag_object *tag;
7223 char *id_str = NULL;
7224 const char *tagmsg = NULL;
7226 err = got_object_open_as_tag(&tag, repo, id);
7230 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7234 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7236 switch (got_object_tag_get_object_type(tag)) {
7237 case GOT_OBJ_TYPE_BLOB:
7238 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7239 GOT_OBJ_LABEL_BLOB);
7241 case GOT_OBJ_TYPE_TREE:
7242 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7243 GOT_OBJ_LABEL_TREE);
7245 case GOT_OBJ_TYPE_COMMIT:
7246 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7247 GOT_OBJ_LABEL_COMMIT);
7249 case GOT_OBJ_TYPE_TAG:
7250 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7257 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7258 got_object_tag_get_name(tag));
7260 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7261 got_object_tag_get_tagger(tag),
7262 got_object_tag_get_tagger_time(tag));
7264 tagmsg = got_object_tag_get_message(tag);
7265 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7266 fprintf(outfile, "%s", tagmsg);
7269 got_object_tag_close(tag);
7273 static const struct got_error *
7274 cmd_cat(int argc, char *argv[])
7276 const struct got_error *error;
7277 struct got_repository *repo = NULL;
7278 struct got_worktree *worktree = NULL;
7279 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7280 const char *commit_id_str = NULL;
7281 struct got_object_id *id = NULL, *commit_id = NULL;
7282 int ch, obj_type, i, force_path = 0;
7285 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7290 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7293 commit_id_str = optarg;
7296 repo_path = realpath(optarg, NULL);
7297 if (repo_path == NULL)
7298 return got_error_from_errno2("realpath",
7300 got_path_strip_trailing_slashes(repo_path);
7314 cwd = getcwd(NULL, 0);
7316 error = got_error_from_errno("getcwd");
7319 error = got_worktree_open(&worktree, cwd);
7320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7323 if (repo_path == NULL) {
7325 got_worktree_get_repo_path(worktree));
7326 if (repo_path == NULL) {
7327 error = got_error_from_errno("strdup");
7333 if (repo_path == NULL) {
7334 repo_path = getcwd(NULL, 0);
7335 if (repo_path == NULL)
7336 return got_error_from_errno("getcwd");
7339 error = got_repo_open(&repo, repo_path, NULL);
7344 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7348 if (commit_id_str == NULL)
7349 commit_id_str = GOT_REF_HEAD;
7350 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7354 for (i = 0; i < argc; i++) {
7356 error = got_object_id_by_path(&id, repo, commit_id,
7361 error = match_object_id(&id, &label, argv[i],
7362 GOT_OBJ_TYPE_ANY, 0, repo);
7364 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7365 error->code != GOT_ERR_NOT_REF)
7367 error = got_object_id_by_path(&id, repo,
7368 commit_id, argv[i]);
7374 error = got_object_get_type(&obj_type, repo, id);
7379 case GOT_OBJ_TYPE_BLOB:
7380 error = cat_blob(id, repo, stdout);
7382 case GOT_OBJ_TYPE_TREE:
7383 error = cat_tree(id, repo, stdout);
7385 case GOT_OBJ_TYPE_COMMIT:
7386 error = cat_commit(id, repo, stdout);
7388 case GOT_OBJ_TYPE_TAG:
7389 error = cat_tag(id, repo, stdout);
7392 error = got_error(GOT_ERR_OBJ_TYPE);
7408 got_worktree_close(worktree);
7410 const struct got_error *repo_error;
7411 repo_error = got_repo_close(repo);