2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 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/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository.h"
43 #include "got_worktree.h"
45 #include "got_commit_graph.h"
46 #include "got_blame.h"
47 #include "got_privsep.h"
48 #include "got_opentemp.h"
51 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 static volatile sig_atomic_t sigint_received;
55 static volatile sig_atomic_t sigpipe_received;
58 catch_sigint(int signo)
64 catch_sigpipe(int signo)
72 const struct got_error *(*cmd_main)(int, char *[]);
73 void (*cmd_usage)(void);
74 const char *cmd_alias;
77 __dead static void usage(int);
78 __dead static void usage_init(void);
79 __dead static void usage_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
154 main(int argc, char *argv[])
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
225 editor = getenv("VISUAL");
227 editor = getenv("EDITOR");
230 err = got_path_find_prog(abspath, editor);
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
276 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
287 while ((ch = getopt(argc, argv, "")) != -1) {
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
316 error = apply_unveil(repo_path, 0, NULL);
320 error = got_repo_init(repo_path);
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
338 spawn_editor(const char *editor, const char *file)
341 sig_t sighup, sigint, sigquit;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
352 execl(editor, editor, file, (char *)NULL);
356 while (waitpid(pid, &st, 0) == -1)
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
381 int content_changed = 0;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
401 return got_error_from_errno("malloc");
405 fp = fopen(logmsg_path, "r");
407 err = got_error_from_errno("fopen");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
452 dprintf(fd, initial_content);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
457 free(initial_content);
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
490 branch_name = optarg;
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
507 if (optarg[0] == '\0')
509 error = got_pathlist_insert(&pe, &ignores, optarg,
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
552 error = got_ref_open(&branch_ref, repo, refname, 0);
554 if (error->code != GOT_ERR_NOT_REF)
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
567 got_path_strip_trailing_slashes(path_dir);
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
598 error = got_ref_write(branch_ref, repo);
602 error = got_object_id_str(&id_str, new_commit_id);
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
608 if (error->code != GOT_ERR_NOT_REF)
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
616 error = got_ref_write(head_ref, repo);
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
630 got_ref_close(branch_ref);
632 got_ref_close(head_ref);
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
653 while (path[0] == '/')
656 printf("%c %s/%s\n", status, worktree_path, path);
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
681 return got_error(GOT_ERR_ANCESTRY);
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_object_id *yca_id,
712 struct got_repository *repo)
714 const struct got_error *err = NULL;
715 struct got_commit_graph *graph = NULL;
716 struct got_object_id *head_commit_id = NULL;
717 int is_same_branch = 0;
719 err = got_ref_resolve(&head_commit_id, repo, head_ref);
723 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
727 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
732 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
736 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
741 struct got_object_id *id;
742 err = got_commit_graph_iter_next(&id, graph);
744 if (err->code == GOT_ERR_ITER_COMPLETED) {
747 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
749 err = got_commit_graph_fetch_commits(graph, 1,
756 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
758 if (got_object_id_cmp(id, commit_id) == 0) {
766 got_commit_graph_close(graph);
767 free(head_commit_id);
768 if (!err && !is_same_branch)
769 err = got_error(GOT_ERR_ANCESTRY);
773 static const struct got_error *
774 resolve_commit_arg(struct got_object_id **commit_id,
775 const char *commit_id_arg, struct got_repository *repo)
777 const struct got_error *err;
778 struct got_reference *ref;
780 err = got_ref_open(&ref, repo, commit_id_arg, 0);
782 err = got_ref_resolve(commit_id, repo, ref);
785 if (err->code != GOT_ERR_NOT_REF)
787 err = got_repo_match_object_id_prefix(commit_id,
788 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
793 static const struct got_error *
794 cmd_checkout(int argc, char *argv[])
796 const struct got_error *error = NULL;
797 struct got_repository *repo = NULL;
798 struct got_reference *head_ref = NULL;
799 struct got_worktree *worktree = NULL;
800 char *repo_path = NULL;
801 char *worktree_path = NULL;
802 const char *path_prefix = "";
803 const char *branch_name = GOT_REF_HEAD;
804 char *commit_id_str = NULL;
805 int ch, same_path_prefix;
806 struct got_pathlist_head paths;
810 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
813 branch_name = optarg;
816 commit_id_str = strdup(optarg);
817 if (commit_id_str == NULL)
818 return got_error_from_errno("strdup");
821 path_prefix = optarg;
833 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
834 "unveil", NULL) == -1)
838 char *cwd, *base, *dotgit;
839 repo_path = realpath(argv[0], NULL);
840 if (repo_path == NULL)
841 return got_error_from_errno2("realpath", argv[0]);
842 cwd = getcwd(NULL, 0);
844 error = got_error_from_errno("getcwd");
847 if (path_prefix[0]) {
848 base = basename(path_prefix);
850 error = got_error_from_errno2("basename",
855 base = basename(repo_path);
857 error = got_error_from_errno2("basename",
862 dotgit = strstr(base, ".git");
865 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
866 error = got_error_from_errno("asprintf");
871 } else if (argc == 2) {
872 repo_path = realpath(argv[0], NULL);
873 if (repo_path == NULL) {
874 error = got_error_from_errno2("realpath", argv[0]);
877 worktree_path = realpath(argv[1], NULL);
878 if (worktree_path == NULL) {
879 if (errno != ENOENT) {
880 error = got_error_from_errno2("realpath",
884 worktree_path = strdup(argv[1]);
885 if (worktree_path == NULL) {
886 error = got_error_from_errno("strdup");
893 got_path_strip_trailing_slashes(repo_path);
894 got_path_strip_trailing_slashes(worktree_path);
896 error = got_repo_open(&repo, repo_path);
900 /* Pre-create work tree path for unveil(2) */
901 error = got_path_mkdir(worktree_path);
903 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
905 if (!got_path_dir_is_empty(worktree_path)) {
906 error = got_error_path(worktree_path,
907 GOT_ERR_DIR_NOT_EMPTY);
912 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
916 error = got_ref_open(&head_ref, repo, branch_name, 0);
920 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
921 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
924 error = got_worktree_open(&worktree, worktree_path);
928 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
932 if (!same_path_prefix) {
933 error = got_error(GOT_ERR_PATH_PREFIX);
938 struct got_object_id *commit_id;
939 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
942 error = check_linear_ancestry(commit_id,
943 got_worktree_get_base_commit_id(worktree), repo);
948 error = check_same_branch(commit_id, head_ref, NULL, repo);
951 error = got_worktree_set_base_commit_id(worktree, repo,
958 error = got_pathlist_append(NULL, &paths, "", NULL);
961 error = got_worktree_checkout_files(worktree, &paths, repo,
962 checkout_progress, worktree_path, check_cancelled, NULL);
966 printf("Now shut up and hack\n");
969 got_pathlist_free(&paths);
979 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
984 static const struct got_error *
985 update_progress(void *arg, unsigned char status, const char *path)
987 int *did_something = arg;
989 if (status == GOT_STATUS_EXISTS)
994 /* Base commit bump happens silently. */
995 if (status == GOT_STATUS_BUMP_BASE)
998 while (path[0] == '/')
1000 printf("%c %s\n", status, path);
1004 static const struct got_error *
1005 switch_head_ref(struct got_reference *head_ref,
1006 struct got_object_id *commit_id, struct got_worktree *worktree,
1007 struct got_repository *repo)
1009 const struct got_error *err = NULL;
1011 int ref_has_moved = 0;
1013 /* Trivial case: switching between two different references. */
1014 if (strcmp(got_ref_get_name(head_ref),
1015 got_worktree_get_head_ref_name(worktree)) != 0) {
1016 printf("Switching work tree from %s to %s\n",
1017 got_worktree_get_head_ref_name(worktree),
1018 got_ref_get_name(head_ref));
1019 return got_worktree_set_head_ref(worktree, head_ref);
1022 err = check_linear_ancestry(commit_id,
1023 got_worktree_get_base_commit_id(worktree), repo);
1025 if (err->code != GOT_ERR_ANCESTRY)
1032 /* Switching to a rebased branch with the same reference name. */
1033 err = got_object_id_str(&base_id_str,
1034 got_worktree_get_base_commit_id(worktree));
1037 printf("Reference %s now points at a different branch\n",
1038 got_worktree_get_head_ref_name(worktree));
1039 printf("Switching work tree from %s to %s\n", base_id_str,
1040 got_worktree_get_head_ref_name(worktree));
1044 static const struct got_error *
1045 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1047 const struct got_error *err;
1050 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1054 return got_error(GOT_ERR_REBASING);
1056 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1060 return got_error(GOT_ERR_HISTEDIT_BUSY);
1065 static const struct got_error *
1066 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1067 char *argv[], struct got_worktree *worktree)
1069 const struct got_error *err;
1076 return got_error_from_errno("strdup");
1077 return got_pathlist_append(NULL, paths, path, NULL);
1080 for (i = 0; i < argc; i++) {
1081 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1084 err = got_pathlist_append(NULL, paths, path, NULL);
1094 static const struct got_error *
1095 cmd_update(int argc, char *argv[])
1097 const struct got_error *error = NULL;
1098 struct got_repository *repo = NULL;
1099 struct got_worktree *worktree = NULL;
1100 char *worktree_path = NULL;
1101 struct got_object_id *commit_id = NULL;
1102 char *commit_id_str = NULL;
1103 const char *branch_name = NULL;
1104 struct got_reference *head_ref = NULL;
1105 struct got_pathlist_head paths;
1106 struct got_pathlist_entry *pe;
1107 int ch, did_something = 0;
1111 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1114 branch_name = optarg;
1117 commit_id_str = strdup(optarg);
1118 if (commit_id_str == NULL)
1119 return got_error_from_errno("strdup");
1131 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1132 "unveil", NULL) == -1)
1135 worktree_path = getcwd(NULL, 0);
1136 if (worktree_path == NULL) {
1137 error = got_error_from_errno("getcwd");
1140 error = got_worktree_open(&worktree, worktree_path);
1144 error = check_rebase_or_histedit_in_progress(worktree);
1148 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1152 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1156 error = apply_unveil(got_repo_get_path(repo), 0,
1157 got_worktree_get_root_path(worktree));
1161 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1162 got_worktree_get_head_ref_name(worktree), 0);
1165 if (commit_id_str == NULL) {
1166 error = got_ref_resolve(&commit_id, repo, head_ref);
1169 error = got_object_id_str(&commit_id_str, commit_id);
1173 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1174 free(commit_id_str);
1175 commit_id_str = NULL;
1178 error = got_object_id_str(&commit_id_str, commit_id);
1184 struct got_object_id *head_commit_id;
1185 TAILQ_FOREACH(pe, &paths, entry) {
1186 if (strlen(pe->path) == 0)
1188 error = got_error_msg(GOT_ERR_BAD_PATH,
1189 "switching between branches requires that "
1190 "the entire work tree gets updated");
1193 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1196 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1197 free(head_commit_id);
1200 error = check_same_branch(commit_id, head_ref, NULL, repo);
1203 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1207 error = check_linear_ancestry(commit_id,
1208 got_worktree_get_base_commit_id(worktree), repo);
1209 if (error != NULL) {
1210 if (error->code == GOT_ERR_ANCESTRY)
1211 error = got_error(GOT_ERR_BRANCH_MOVED);
1214 error = check_same_branch(commit_id, head_ref, NULL, repo);
1219 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1221 error = got_worktree_set_base_commit_id(worktree, repo,
1227 error = got_worktree_checkout_files(worktree, &paths, repo,
1228 update_progress, &did_something, check_cancelled, NULL);
1233 printf("Updated to commit %s\n", commit_id_str);
1235 printf("Already up-to-date\n");
1237 free(worktree_path);
1238 TAILQ_FOREACH(pe, &paths, entry)
1239 free((char *)pe->path);
1240 got_pathlist_free(&paths);
1242 free(commit_id_str);
1246 static const struct got_error *
1247 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1248 int diff_context, struct got_repository *repo)
1250 const struct got_error *err = NULL;
1251 struct got_tree_object *tree1 = NULL, *tree2;
1252 struct got_object_qid *qid;
1253 char *id_str1 = NULL, *id_str2;
1254 struct got_diff_blob_output_unidiff_arg arg;
1256 err = got_object_open_as_tree(&tree2, repo,
1257 got_object_commit_get_tree_id(commit));
1261 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1263 struct got_commit_object *pcommit;
1265 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1269 err = got_object_open_as_tree(&tree1, repo,
1270 got_object_commit_get_tree_id(pcommit));
1271 got_object_commit_close(pcommit);
1275 err = got_object_id_str(&id_str1, qid->id);
1280 err = got_object_id_str(&id_str2, id);
1284 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1285 arg.diff_context = diff_context;
1286 arg.outfile = stdout;
1287 err = got_diff_tree(tree1, tree2, "", "", repo,
1288 got_diff_blob_output_unidiff, &arg, 1);
1291 got_object_tree_close(tree1);
1292 got_object_tree_close(tree2);
1299 get_datestr(time_t *time, char *datebuf)
1301 char *p, *s = ctime_r(time, datebuf);
1302 p = strchr(s, '\n');
1308 static const struct got_error *
1309 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1310 struct got_repository *repo, int show_patch, int diff_context,
1311 struct got_reflist_head *refs)
1313 const struct got_error *err = NULL;
1314 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1316 time_t committer_time;
1317 const char *author, *committer;
1318 char *refs_str = NULL;
1319 struct got_reflist_entry *re;
1321 SIMPLEQ_FOREACH(re, refs, entry) {
1324 if (got_object_id_cmp(re->id, id) != 0)
1326 name = got_ref_get_name(re->ref);
1327 if (strcmp(name, GOT_REF_HEAD) == 0)
1329 if (strncmp(name, "refs/", 5) == 0)
1331 if (strncmp(name, "got/", 4) == 0)
1333 if (strncmp(name, "heads/", 6) == 0)
1335 if (strncmp(name, "remotes/", 8) == 0)
1338 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1340 err = got_error_from_errno("asprintf");
1346 err = got_object_id_str(&id_str, id);
1350 printf("-----------------------------------------------\n");
1351 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1352 refs_str ? refs_str : "", refs_str ? ")" : "");
1357 printf("from: %s\n", got_object_commit_get_author(commit));
1358 committer_time = got_object_commit_get_committer_time(commit);
1359 datestr = get_datestr(&committer_time, datebuf);
1360 printf("date: %s UTC\n", datestr);
1361 author = got_object_commit_get_author(commit);
1362 committer = got_object_commit_get_committer(commit);
1363 if (strcmp(author, committer) != 0)
1364 printf("via: %s\n", committer);
1365 if (got_object_commit_get_nparents(commit) > 1) {
1366 const struct got_object_id_queue *parent_ids;
1367 struct got_object_qid *qid;
1369 parent_ids = got_object_commit_get_parent_ids(commit);
1370 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1371 err = got_object_id_str(&id_str, qid->id);
1374 printf("parent %d: %s\n", n++, id_str);
1379 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1380 if (logmsg0 == NULL)
1381 return got_error_from_errno("strdup");
1385 line = strsep(&logmsg, "\n");
1387 printf(" %s\n", line);
1392 err = print_patch(commit, id, diff_context, repo);
1397 if (fflush(stdout) != 0 && err == NULL)
1398 err = got_error_from_errno("fflush");
1402 static const struct got_error *
1403 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1404 char *path, int show_patch, int diff_context, int limit,
1405 int first_parent_traversal, struct got_reflist_head *refs)
1407 const struct got_error *err;
1408 struct got_commit_graph *graph;
1410 err = got_commit_graph_open(&graph, root_id, path,
1411 first_parent_traversal, repo);
1414 err = got_commit_graph_iter_start(graph, root_id, repo);
1418 struct got_commit_object *commit;
1419 struct got_object_id *id;
1421 if (sigint_received || sigpipe_received)
1424 err = got_commit_graph_iter_next(&id, graph);
1426 if (err->code == GOT_ERR_ITER_COMPLETED) {
1430 if (err->code != GOT_ERR_ITER_NEED_MORE)
1432 err = got_commit_graph_fetch_commits(graph, 1, repo);
1441 err = got_object_open_as_commit(&commit, repo, id);
1444 err = print_commit(commit, id, repo, show_patch, diff_context,
1446 got_object_commit_close(commit);
1447 if (err || (limit && --limit == 0))
1451 got_commit_graph_close(graph);
1458 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1459 "[-r repository-path] [path]\n", getprogname());
1463 static const struct got_error *
1464 cmd_log(int argc, char *argv[])
1466 const struct got_error *error;
1467 struct got_repository *repo = NULL;
1468 struct got_worktree *worktree = NULL;
1469 struct got_commit_object *commit = NULL;
1470 struct got_object_id *id = NULL;
1471 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1472 char *start_commit = NULL;
1473 int diff_context = 3, ch;
1474 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1476 struct got_reflist_head refs;
1478 SIMPLEQ_INIT(&refs);
1481 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1487 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1493 start_commit = optarg;
1496 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1499 err(1, "-C option %s", errstr);
1502 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1504 err(1, "-l option %s", errstr);
1507 first_parent_traversal = 1;
1510 repo_path = realpath(optarg, NULL);
1511 if (repo_path == NULL)
1512 err(1, "-r option");
1513 got_path_strip_trailing_slashes(repo_path);
1524 cwd = getcwd(NULL, 0);
1526 error = got_error_from_errno("getcwd");
1530 error = got_worktree_open(&worktree, cwd);
1531 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1538 error = got_error_from_errno("strdup");
1541 } else if (argc == 1) {
1543 error = got_worktree_resolve_path(&path, worktree,
1548 path = strdup(argv[0]);
1550 error = got_error_from_errno("strdup");
1557 if (repo_path == NULL) {
1558 repo_path = worktree ?
1559 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1561 if (repo_path == NULL) {
1562 error = got_error_from_errno("strdup");
1566 error = got_repo_open(&repo, repo_path);
1570 error = apply_unveil(got_repo_get_path(repo), 1,
1571 worktree ? got_worktree_get_root_path(worktree) : NULL);
1575 if (start_commit == NULL) {
1576 struct got_reference *head_ref;
1577 error = got_ref_open(&head_ref, repo,
1578 worktree ? got_worktree_get_head_ref_name(worktree)
1582 error = got_ref_resolve(&id, repo, head_ref);
1583 got_ref_close(head_ref);
1586 error = got_object_open_as_commit(&commit, repo, id);
1588 struct got_reference *ref;
1589 error = got_ref_open(&ref, repo, start_commit, 0);
1590 if (error == NULL) {
1592 error = got_ref_resolve(&id, repo, ref);
1596 error = got_object_get_type(&obj_type, repo, id);
1599 if (obj_type == GOT_OBJ_TYPE_TAG) {
1600 struct got_tag_object *tag;
1601 error = got_object_open_as_tag(&tag, repo, id);
1604 if (got_object_tag_get_object_type(tag) !=
1605 GOT_OBJ_TYPE_COMMIT) {
1606 got_object_tag_close(tag);
1607 error = got_error(GOT_ERR_OBJ_TYPE);
1611 id = got_object_id_dup(
1612 got_object_tag_get_object_id(tag));
1614 error = got_error_from_errno(
1615 "got_object_id_dup");
1616 got_object_tag_close(tag);
1619 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1620 error = got_error(GOT_ERR_OBJ_TYPE);
1623 error = got_object_open_as_commit(&commit, repo, id);
1627 if (commit == NULL) {
1628 error = got_repo_match_object_id_prefix(&id,
1629 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1637 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1642 path = in_repo_path;
1645 error = got_ref_list(&refs, repo);
1649 error = print_commits(id, repo, path, show_patch,
1650 diff_context, limit, first_parent_traversal, &refs);
1657 got_worktree_close(worktree);
1659 const struct got_error *repo_error;
1660 repo_error = got_repo_close(repo);
1664 got_ref_list_free(&refs);
1671 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1672 "[object1 object2 | path]\n", getprogname());
1676 struct print_diff_arg {
1677 struct got_repository *repo;
1678 struct got_worktree *worktree;
1684 static const struct got_error *
1685 print_diff(void *arg, unsigned char status, const char *path,
1686 struct got_object_id *blob_id, struct got_object_id *commit_id)
1688 struct print_diff_arg *a = arg;
1689 const struct got_error *err = NULL;
1690 struct got_blob_object *blob1 = NULL;
1692 char *abspath = NULL;
1695 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1696 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1699 if (!a->header_shown) {
1700 printf("diff %s %s\n", a->id_str,
1701 got_worktree_get_root_path(a->worktree));
1702 a->header_shown = 1;
1705 if (status != GOT_STATUS_ADD) {
1706 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1712 if (status != GOT_STATUS_DELETE) {
1713 if (asprintf(&abspath, "%s/%s",
1714 got_worktree_get_root_path(a->worktree), path) == -1) {
1715 err = got_error_from_errno("asprintf");
1719 f2 = fopen(abspath, "r");
1721 err = got_error_from_errno2("fopen", abspath);
1724 if (lstat(abspath, &sb) == -1) {
1725 err = got_error_from_errno2("lstat", abspath);
1731 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1735 got_object_blob_close(blob1);
1736 if (f2 && fclose(f2) != 0 && err == NULL)
1737 err = got_error_from_errno("fclose");
1742 static const struct got_error *
1743 cmd_diff(int argc, char *argv[])
1745 const struct got_error *error;
1746 struct got_repository *repo = NULL;
1747 struct got_worktree *worktree = NULL;
1748 char *cwd = NULL, *repo_path = NULL;
1749 struct got_object_id *id1 = NULL, *id2 = NULL;
1750 const char *id_str1 = NULL, *id_str2 = NULL;
1751 char *label1 = NULL, *label2 = NULL;
1753 int diff_context = 3, ch;
1758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1763 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1766 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1768 err(1, "-C option %s", errstr);
1771 repo_path = realpath(optarg, NULL);
1772 if (repo_path == NULL)
1773 err(1, "-r option");
1774 got_path_strip_trailing_slashes(repo_path);
1785 cwd = getcwd(NULL, 0);
1787 error = got_error_from_errno("getcwd");
1790 error = got_worktree_open(&worktree, cwd);
1791 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1794 if (worktree == NULL) {
1795 error = got_error(GOT_ERR_NOT_WORKTREE);
1800 "-r option can't be used when diffing a work tree");
1801 repo_path = strdup(got_worktree_get_repo_path(worktree));
1802 if (repo_path == NULL) {
1803 error = got_error_from_errno("strdup");
1807 error = got_worktree_resolve_path(&path, worktree,
1814 error = got_error_from_errno("strdup");
1818 } else if (argc == 2) {
1821 if (worktree && repo_path == NULL) {
1823 strdup(got_worktree_get_repo_path(worktree));
1824 if (repo_path == NULL) {
1825 error = got_error_from_errno("strdup");
1832 if (repo_path == NULL) {
1833 repo_path = getcwd(NULL, 0);
1834 if (repo_path == NULL)
1835 return got_error_from_errno("getcwd");
1838 error = got_repo_open(&repo, repo_path);
1843 error = apply_unveil(got_repo_get_path(repo), 1,
1844 worktree ? got_worktree_get_root_path(worktree) : NULL);
1849 struct print_diff_arg arg;
1850 struct got_pathlist_head paths;
1855 error = got_object_id_str(&id_str,
1856 got_worktree_get_base_commit_id(worktree));
1860 arg.worktree = worktree;
1861 arg.diff_context = diff_context;
1862 arg.id_str = id_str;
1863 arg.header_shown = 0;
1865 error = got_pathlist_append(NULL, &paths, path, NULL);
1869 error = got_worktree_status(worktree, &paths, repo, print_diff,
1870 &arg, check_cancelled, NULL);
1872 got_pathlist_free(&paths);
1876 error = got_repo_match_object_id_prefix(&id1, id_str1,
1877 GOT_OBJ_TYPE_ANY, repo);
1879 struct got_reference *ref;
1880 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1882 error = got_ref_open(&ref, repo, id_str1, 0);
1885 label1 = strdup(got_ref_get_name(ref));
1886 if (label1 == NULL) {
1887 error = got_error_from_errno("strdup");
1890 error = got_ref_resolve(&id1, repo, ref);
1895 error = got_object_id_str(&label1, id1);
1896 if (label1 == NULL) {
1897 error = got_error_from_errno("strdup");
1902 error = got_repo_match_object_id_prefix(&id2, id_str2,
1903 GOT_OBJ_TYPE_ANY, repo);
1905 struct got_reference *ref;
1906 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1908 error = got_ref_open(&ref, repo, id_str2, 0);
1911 label2 = strdup(got_ref_get_name(ref));
1912 if (label2 == NULL) {
1913 error = got_error_from_errno("strdup");
1916 error = got_ref_resolve(&id2, repo, ref);
1921 error = got_object_id_str(&label2, id2);
1922 if (label2 == NULL) {
1923 error = got_error_from_errno("strdup");
1928 error = got_object_get_type(&type1, repo, id1);
1932 error = got_object_get_type(&type2, repo, id2);
1936 if (type1 != type2) {
1937 error = got_error(GOT_ERR_OBJ_TYPE);
1942 case GOT_OBJ_TYPE_BLOB:
1943 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1944 diff_context, repo, stdout);
1946 case GOT_OBJ_TYPE_TREE:
1947 error = got_diff_objects_as_trees(id1, id2, "", "",
1948 diff_context, repo, stdout);
1950 case GOT_OBJ_TYPE_COMMIT:
1951 printf("diff %s %s\n", label1, label2);
1952 error = got_diff_objects_as_commits(id1, id2, diff_context,
1956 error = got_error(GOT_ERR_OBJ_TYPE);
1966 got_worktree_close(worktree);
1968 const struct got_error *repo_error;
1969 repo_error = got_repo_close(repo);
1980 "usage: %s blame [-c commit] [-r repository-path] path\n",
1985 static const struct got_error *
1986 cmd_blame(int argc, char *argv[])
1988 const struct got_error *error;
1989 struct got_repository *repo = NULL;
1990 struct got_worktree *worktree = NULL;
1991 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1992 struct got_object_id *commit_id = NULL;
1993 char *commit_id_str = NULL;
1997 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2002 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2005 commit_id_str = optarg;
2008 repo_path = realpath(optarg, NULL);
2009 if (repo_path == NULL)
2010 err(1, "-r option");
2011 got_path_strip_trailing_slashes(repo_path);
2027 cwd = getcwd(NULL, 0);
2029 error = got_error_from_errno("getcwd");
2032 if (repo_path == NULL) {
2033 error = got_worktree_open(&worktree, cwd);
2034 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2040 strdup(got_worktree_get_repo_path(worktree));
2041 if (repo_path == NULL)
2042 error = got_error_from_errno("strdup");
2046 repo_path = strdup(cwd);
2047 if (repo_path == NULL) {
2048 error = got_error_from_errno("strdup");
2054 error = got_repo_open(&repo, repo_path);
2058 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2063 const char *prefix = got_worktree_get_path_prefix(worktree);
2064 char *p, *worktree_subdir = cwd +
2065 strlen(got_worktree_get_root_path(worktree));
2066 if (asprintf(&p, "%s%s%s%s%s",
2067 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2068 worktree_subdir, worktree_subdir[0] ? "/" : "",
2070 error = got_error_from_errno("asprintf");
2073 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2076 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2081 if (commit_id_str == NULL) {
2082 struct got_reference *head_ref;
2083 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2086 error = got_ref_resolve(&commit_id, repo, head_ref);
2087 got_ref_close(head_ref);
2091 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2096 error = got_blame(in_repo_path, commit_id, repo, stdout);
2103 got_worktree_close(worktree);
2105 const struct got_error *repo_error;
2106 repo_error = got_repo_close(repo);
2117 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2123 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2124 const char *root_path)
2126 int is_root_path = (strcmp(path, root_path) == 0);
2128 path += strlen(root_path);
2129 while (path[0] == '/')
2132 printf("%s%s%s%s%s\n", id ? id : "", path,
2133 is_root_path ? "" : "/", te->name,
2134 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2137 static const struct got_error *
2138 print_tree(const char *path, struct got_object_id *commit_id,
2139 int show_ids, int recurse, const char *root_path,
2140 struct got_repository *repo)
2142 const struct got_error *err = NULL;
2143 struct got_object_id *tree_id = NULL;
2144 struct got_tree_object *tree = NULL;
2145 const struct got_tree_entries *entries;
2146 struct got_tree_entry *te;
2148 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2152 err = got_object_open_as_tree(&tree, repo, tree_id);
2155 entries = got_object_tree_get_entries(tree);
2156 te = SIMPLEQ_FIRST(&entries->head);
2160 if (sigint_received || sigpipe_received)
2165 err = got_object_id_str(&id_str, te->id);
2168 if (asprintf(&id, "%s ", id_str) == -1) {
2169 err = got_error_from_errno("asprintf");
2175 print_entry(te, id, path, root_path);
2178 if (recurse && S_ISDIR(te->mode)) {
2180 if (asprintf(&child_path, "%s%s%s", path,
2181 path[0] == '/' && path[1] == '\0' ? "" : "/",
2183 err = got_error_from_errno("asprintf");
2186 err = print_tree(child_path, commit_id, show_ids, 1,
2193 te = SIMPLEQ_NEXT(te, entry);
2197 got_object_tree_close(tree);
2202 static const struct got_error *
2203 cmd_tree(int argc, char *argv[])
2205 const struct got_error *error;
2206 struct got_repository *repo = NULL;
2207 struct got_worktree *worktree = NULL;
2209 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2210 struct got_object_id *commit_id = NULL;
2211 char *commit_id_str = NULL;
2212 int show_ids = 0, recurse = 0;
2216 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2221 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2224 commit_id_str = optarg;
2227 repo_path = realpath(optarg, NULL);
2228 if (repo_path == NULL)
2229 err(1, "-r option");
2230 got_path_strip_trailing_slashes(repo_path);
2254 cwd = getcwd(NULL, 0);
2256 error = got_error_from_errno("getcwd");
2259 if (repo_path == NULL) {
2260 error = got_worktree_open(&worktree, cwd);
2261 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2267 strdup(got_worktree_get_repo_path(worktree));
2268 if (repo_path == NULL)
2269 error = got_error_from_errno("strdup");
2273 repo_path = strdup(cwd);
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2281 error = got_repo_open(&repo, repo_path);
2285 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2291 char *p, *worktree_subdir = cwd +
2292 strlen(got_worktree_get_root_path(worktree));
2293 if (asprintf(&p, "%s/%s",
2294 got_worktree_get_path_prefix(worktree),
2295 worktree_subdir) == -1) {
2296 error = got_error_from_errno("asprintf");
2299 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2306 if (in_repo_path == NULL) {
2307 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2312 if (commit_id_str == NULL) {
2313 struct got_reference *head_ref;
2314 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2317 error = got_ref_resolve(&commit_id, repo, head_ref);
2318 got_ref_close(head_ref);
2322 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2327 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2328 in_repo_path, repo);
2335 got_worktree_close(worktree);
2337 const struct got_error *repo_error;
2338 repo_error = got_repo_close(repo);
2348 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2352 static const struct got_error *
2353 print_status(void *arg, unsigned char status, const char *path,
2354 struct got_object_id *blob_id, struct got_object_id *commit_id)
2356 printf("%c %s\n", status, path);
2360 static const struct got_error *
2361 cmd_status(int argc, char *argv[])
2363 const struct got_error *error = NULL;
2364 struct got_repository *repo = NULL;
2365 struct got_worktree *worktree = NULL;
2367 struct got_pathlist_head paths;
2368 struct got_pathlist_entry *pe;
2373 while ((ch = getopt(argc, argv, "")) != -1) {
2385 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2389 cwd = getcwd(NULL, 0);
2391 error = got_error_from_errno("getcwd");
2395 error = got_worktree_open(&worktree, cwd);
2399 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2403 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2407 error = apply_unveil(got_repo_get_path(repo), 1,
2408 got_worktree_get_root_path(worktree));
2412 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2413 check_cancelled, NULL);
2415 TAILQ_FOREACH(pe, &paths, entry)
2416 free((char *)pe->path);
2417 got_pathlist_free(&paths);
2426 "usage: %s ref [-r repository] -l | -d name | name target\n",
2431 static const struct got_error *
2432 list_refs(struct got_repository *repo)
2434 static const struct got_error *err = NULL;
2435 struct got_reflist_head refs;
2436 struct got_reflist_entry *re;
2438 SIMPLEQ_INIT(&refs);
2439 err = got_ref_list(&refs, repo);
2443 SIMPLEQ_FOREACH(re, &refs, entry) {
2445 refstr = got_ref_to_str(re->ref);
2447 return got_error_from_errno("got_ref_to_str");
2448 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2452 got_ref_list_free(&refs);
2456 static const struct got_error *
2457 delete_ref(struct got_repository *repo, const char *refname)
2459 const struct got_error *err = NULL;
2460 struct got_reference *ref;
2462 err = got_ref_open(&ref, repo, refname, 0);
2466 err = got_ref_delete(ref, repo);
2471 static const struct got_error *
2472 add_ref(struct got_repository *repo, const char *refname, const char *target)
2474 const struct got_error *err = NULL;
2475 struct got_object_id *id;
2476 struct got_reference *ref = NULL;
2479 * Don't let the user create a reference named '-'.
2480 * While technically a valid reference name, this case is usually
2481 * an unintended typo.
2483 if (refname[0] == '-' && refname[1] == '\0')
2484 return got_error(GOT_ERR_BAD_REF_NAME);
2486 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2489 struct got_reference *target_ref;
2491 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2493 err = got_ref_open(&target_ref, repo, target, 0);
2496 err = got_ref_resolve(&id, repo, target_ref);
2497 got_ref_close(target_ref);
2502 err = got_ref_alloc(&ref, refname, id);
2506 err = got_ref_write(ref, repo);
2514 static const struct got_error *
2515 cmd_ref(int argc, char *argv[])
2517 const struct got_error *error = NULL;
2518 struct got_repository *repo = NULL;
2519 struct got_worktree *worktree = NULL;
2520 char *cwd = NULL, *repo_path = NULL;
2521 int ch, do_list = 0;
2522 const char *delref = NULL;
2524 /* TODO: Add -s option for adding symbolic references. */
2525 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2531 repo_path = realpath(optarg, NULL);
2532 if (repo_path == NULL)
2533 err(1, "-r option");
2534 got_path_strip_trailing_slashes(repo_path);
2545 if (do_list && delref)
2546 errx(1, "-l and -d options are mutually exclusive\n");
2551 if (do_list || delref) {
2554 } else if (argc != 2)
2559 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2563 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2564 "sendfd unveil", NULL) == -1)
2568 cwd = getcwd(NULL, 0);
2570 error = got_error_from_errno("getcwd");
2574 if (repo_path == NULL) {
2575 error = got_worktree_open(&worktree, cwd);
2576 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2582 strdup(got_worktree_get_repo_path(worktree));
2583 if (repo_path == NULL)
2584 error = got_error_from_errno("strdup");
2588 repo_path = strdup(cwd);
2589 if (repo_path == NULL) {
2590 error = got_error_from_errno("strdup");
2596 error = got_repo_open(&repo, repo_path);
2600 error = apply_unveil(got_repo_get_path(repo), do_list,
2601 worktree ? got_worktree_get_root_path(worktree) : NULL);
2606 error = list_refs(repo);
2608 error = delete_ref(repo, delref);
2610 error = add_ref(repo, argv[0], argv[1]);
2613 got_repo_close(repo);
2615 got_worktree_close(worktree);
2625 "usage: %s branch [-r repository] -l | -d name | "
2626 "name [base-branch]\n", getprogname());
2630 static const struct got_error *
2631 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2633 static const struct got_error *err = NULL;
2634 struct got_reflist_head refs;
2635 struct got_reflist_entry *re;
2637 SIMPLEQ_INIT(&refs);
2639 err = got_ref_list(&refs, repo);
2643 SIMPLEQ_FOREACH(re, &refs, entry) {
2644 const char *refname, *marker = " ";
2646 refname = got_ref_get_name(re->ref);
2647 if (strncmp(refname, "refs/heads/", 11) != 0)
2649 if (worktree && strcmp(refname,
2650 got_worktree_get_head_ref_name(worktree)) == 0) {
2651 struct got_object_id *id = NULL;
2652 err = got_ref_resolve(&id, repo, re->ref);
2655 if (got_object_id_cmp(id,
2656 got_worktree_get_base_commit_id(worktree)) == 0)
2663 refstr = got_ref_to_str(re->ref);
2665 return got_error_from_errno("got_ref_to_str");
2666 printf("%s%s: %s\n", marker, refname, refstr);
2670 got_ref_list_free(&refs);
2674 static const struct got_error *
2675 delete_branch(struct got_repository *repo, const char *branch_name)
2677 const struct got_error *err = NULL;
2678 struct got_reference *ref;
2681 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2682 return got_error_from_errno("asprintf");
2684 err = got_ref_open(&ref, repo, refname, 0);
2688 err = got_ref_delete(ref, repo);
2695 static const struct got_error *
2696 add_branch(struct got_repository *repo, const char *branch_name,
2697 const char *base_branch)
2699 const struct got_error *err = NULL;
2700 struct got_object_id *id = NULL;
2701 struct got_reference *ref = NULL;
2702 char *base_refname = NULL, *refname = NULL;
2703 struct got_reference *base_ref;
2706 * Don't let the user create a branch named '-'.
2707 * While technically a valid reference name, this case is usually
2708 * an unintended typo.
2710 if (branch_name[0] == '-' && branch_name[1] == '\0')
2711 return got_error(GOT_ERR_BAD_REF_NAME);
2713 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2714 base_refname = strdup(GOT_REF_HEAD);
2715 if (base_refname == NULL)
2716 return got_error_from_errno("strdup");
2717 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2718 return got_error_from_errno("asprintf");
2720 err = got_ref_open(&base_ref, repo, base_refname, 0);
2723 err = got_ref_resolve(&id, repo, base_ref);
2724 got_ref_close(base_ref);
2728 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2729 err = got_error_from_errno("asprintf");
2733 err = got_ref_open(&ref, repo, refname, 0);
2735 err = got_error(GOT_ERR_BRANCH_EXISTS);
2737 } else if (err->code != GOT_ERR_NOT_REF)
2740 err = got_ref_alloc(&ref, refname, id);
2744 err = got_ref_write(ref, repo);
2754 static const struct got_error *
2755 cmd_branch(int argc, char *argv[])
2757 const struct got_error *error = NULL;
2758 struct got_repository *repo = NULL;
2759 struct got_worktree *worktree = NULL;
2760 char *cwd = NULL, *repo_path = NULL;
2761 int ch, do_list = 0;
2762 const char *delref = NULL;
2764 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2770 repo_path = realpath(optarg, NULL);
2771 if (repo_path == NULL)
2772 err(1, "-r option");
2773 got_path_strip_trailing_slashes(repo_path);
2784 if (do_list && delref)
2785 errx(1, "-l and -d options are mutually exclusive\n");
2790 if (do_list || delref) {
2793 } else if (argc < 1 || argc > 2)
2798 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2802 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2803 "sendfd unveil", NULL) == -1)
2807 cwd = getcwd(NULL, 0);
2809 error = got_error_from_errno("getcwd");
2813 if (repo_path == NULL) {
2814 error = got_worktree_open(&worktree, cwd);
2815 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2821 strdup(got_worktree_get_repo_path(worktree));
2822 if (repo_path == NULL)
2823 error = got_error_from_errno("strdup");
2827 repo_path = strdup(cwd);
2828 if (repo_path == NULL) {
2829 error = got_error_from_errno("strdup");
2835 error = got_repo_open(&repo, repo_path);
2839 error = apply_unveil(got_repo_get_path(repo), do_list,
2840 worktree ? got_worktree_get_root_path(worktree) : NULL);
2845 error = list_branches(repo, worktree);
2847 error = delete_branch(repo, delref);
2849 const char *base_branch;
2851 base_branch = worktree ?
2852 got_worktree_get_head_ref_name(worktree) :
2855 base_branch = argv[1];
2856 error = add_branch(repo, argv[0], base_branch);
2860 got_repo_close(repo);
2862 got_worktree_close(worktree);
2871 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2875 static const struct got_error *
2876 cmd_add(int argc, char *argv[])
2878 const struct got_error *error = NULL;
2879 struct got_repository *repo = NULL;
2880 struct got_worktree *worktree = NULL;
2882 struct got_pathlist_head paths;
2883 struct got_pathlist_entry *pe;
2888 while ((ch = getopt(argc, argv, "")) != -1) {
2900 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2907 cwd = getcwd(NULL, 0);
2909 error = got_error_from_errno("getcwd");
2913 error = got_worktree_open(&worktree, cwd);
2917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2921 error = apply_unveil(got_repo_get_path(repo), 1,
2922 got_worktree_get_root_path(worktree));
2926 for (x = 0; x < argc; x++) {
2927 char *path = realpath(argv[x], NULL);
2929 error = got_error_from_errno2("realpath", argv[x]);
2933 got_path_strip_trailing_slashes(path);
2934 error = got_pathlist_insert(&pe, &paths, path, NULL);
2940 error = got_worktree_schedule_add(worktree, &paths, print_status,
2944 got_repo_close(repo);
2946 got_worktree_close(worktree);
2947 TAILQ_FOREACH(pe, &paths, entry)
2948 free((char *)pe->path);
2949 got_pathlist_free(&paths);
2957 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2961 static const struct got_error *
2962 cmd_remove(int argc, char *argv[])
2964 const struct got_error *error = NULL;
2965 struct got_worktree *worktree = NULL;
2966 struct got_repository *repo = NULL;
2968 struct got_pathlist_head paths;
2969 struct got_pathlist_entry *pe;
2970 int ch, i, delete_local_mods = 0;
2974 while ((ch = getopt(argc, argv, "f")) != -1) {
2977 delete_local_mods = 1;
2989 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2996 cwd = getcwd(NULL, 0);
2998 error = got_error_from_errno("getcwd");
3001 error = got_worktree_open(&worktree, cwd);
3005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3009 error = apply_unveil(got_repo_get_path(repo), 1,
3010 got_worktree_get_root_path(worktree));
3014 for (i = 0; i < argc; i++) {
3015 char *path = realpath(argv[i], NULL);
3017 error = got_error_from_errno2("realpath", argv[i]);
3021 got_path_strip_trailing_slashes(path);
3022 error = got_pathlist_insert(&pe, &paths, path, NULL);
3028 error = got_worktree_schedule_delete(worktree, &paths,
3029 delete_local_mods, print_status, NULL, repo);
3034 got_repo_close(repo);
3036 got_worktree_close(worktree);
3037 TAILQ_FOREACH(pe, &paths, entry)
3038 free((char *)pe->path);
3039 got_pathlist_free(&paths);
3047 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3051 static const struct got_error *
3052 revert_progress(void *arg, unsigned char status, const char *path)
3054 while (path[0] == '/')
3056 printf("%c %s\n", status, path);
3060 static const struct got_error *
3061 cmd_revert(int argc, char *argv[])
3063 const struct got_error *error = NULL;
3064 struct got_worktree *worktree = NULL;
3065 struct got_repository *repo = NULL;
3066 char *cwd = NULL, *path = NULL;
3067 struct got_pathlist_head paths;
3068 struct got_pathlist_entry *pe;
3073 while ((ch = getopt(argc, argv, "")) != -1) {
3085 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3086 "unveil", NULL) == -1)
3092 for (i = 0; i < argc; i++) {
3093 char *path = realpath(argv[i], NULL);
3095 error = got_error_from_errno2("realpath", argv[i]);
3099 got_path_strip_trailing_slashes(path);
3100 error = got_pathlist_insert(&pe, &paths, path, NULL);
3107 cwd = getcwd(NULL, 0);
3109 error = got_error_from_errno("getcwd");
3112 error = got_worktree_open(&worktree, cwd);
3116 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3120 error = apply_unveil(got_repo_get_path(repo), 1,
3121 got_worktree_get_root_path(worktree));
3125 error = got_worktree_revert(worktree, &paths,
3126 revert_progress, NULL, repo);
3131 got_repo_close(repo);
3133 got_worktree_close(worktree);
3142 fprintf(stderr, "usage: %s commit [-m msg] [path]\n", getprogname());
3146 struct collect_commit_logmsg_arg {
3147 const char *cmdline_log;
3149 const char *worktree_path;
3150 const char *branch_name;
3151 const char *repo_path;
3156 static const struct got_error *
3157 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3160 char *initial_content = NULL;
3161 struct got_pathlist_entry *pe;
3162 const struct got_error *err = NULL;
3163 char *template = NULL;
3164 struct collect_commit_logmsg_arg *a = arg;
3168 /* if a message was specified on the command line, just use it */
3169 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3170 len = strlen(a->cmdline_log) + 1;
3171 *logmsg = malloc(len + 1);
3172 if (*logmsg == NULL)
3173 return got_error_from_errno("malloc");
3174 strlcpy(*logmsg, a->cmdline_log, len);
3178 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3179 return got_error_from_errno("asprintf");
3181 if (asprintf(&initial_content,
3182 "\n# changes to be committed on branch %s:\n",
3183 a->branch_name) == -1)
3184 return got_error_from_errno("asprintf");
3186 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3190 dprintf(fd, initial_content);
3192 TAILQ_FOREACH(pe, commitable_paths, entry) {
3193 struct got_commitable *ct = pe->data;
3194 dprintf(fd, "# %c %s\n",
3195 got_commitable_get_status(ct),
3196 got_commitable_get_path(ct));
3200 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3202 unlink(a->logmsg_path);
3203 free(a->logmsg_path);
3204 free(initial_content);
3207 /* Editor is done; we can now apply unveil(2) */
3209 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3218 static const struct got_error *
3219 cmd_commit(int argc, char *argv[])
3221 const struct got_error *error = NULL;
3222 struct got_worktree *worktree = NULL;
3223 struct got_repository *repo = NULL;
3224 char *cwd = NULL, *path = NULL, *id_str = NULL;
3225 struct got_object_id *id = NULL;
3226 const char *logmsg = NULL;
3227 const char *got_author = getenv("GOT_AUTHOR");
3228 struct collect_commit_logmsg_arg cl_arg;
3229 char *editor = NULL;
3230 int ch, rebase_in_progress;
3232 while ((ch = getopt(argc, argv, "m:")) != -1) {
3247 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3248 "unveil", NULL) == -1)
3252 path = realpath(argv[0], NULL);
3254 error = got_error_from_errno2("realpath", argv[0]);
3257 got_path_strip_trailing_slashes(path);
3258 } else if (argc != 0)
3261 if (got_author == NULL) {
3262 /* TODO: Look current user up in password database */
3263 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3267 cwd = getcwd(NULL, 0);
3269 error = got_error_from_errno("getcwd");
3272 error = got_worktree_open(&worktree, cwd);
3276 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3279 if (rebase_in_progress) {
3280 error = got_error(GOT_ERR_REBASING);
3284 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3289 * unveil(2) traverses exec(2); if an editor is used we have
3290 * to apply unveil after the log message has been written.
3292 if (logmsg == NULL || strlen(logmsg) == 0)
3293 error = get_editor(&editor);
3295 error = apply_unveil(got_repo_get_path(repo), 0,
3296 got_worktree_get_root_path(worktree));
3300 cl_arg.editor = editor;
3301 cl_arg.cmdline_log = logmsg;
3302 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3303 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3304 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3305 cl_arg.branch_name += 5;
3306 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3307 cl_arg.branch_name += 6;
3308 cl_arg.repo_path = got_repo_get_path(repo);
3309 cl_arg.logmsg_path = NULL;
3310 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3311 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3313 if (cl_arg.logmsg_path)
3314 fprintf(stderr, "%s: log message preserved in %s\n",
3315 getprogname(), cl_arg.logmsg_path);
3319 if (cl_arg.logmsg_path)
3320 unlink(cl_arg.logmsg_path);
3322 error = got_object_id_str(&id_str, id);
3325 printf("Created commit %s\n", id_str);
3328 got_repo_close(repo);
3330 got_worktree_close(worktree);
3339 usage_cherrypick(void)
3341 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3345 static const struct got_error *
3346 cmd_cherrypick(int argc, char *argv[])
3348 const struct got_error *error = NULL;
3349 struct got_worktree *worktree = NULL;
3350 struct got_repository *repo = NULL;
3351 char *cwd = NULL, *commit_id_str = NULL;
3352 struct got_object_id *commit_id = NULL;
3353 struct got_commit_object *commit = NULL;
3354 struct got_object_qid *pid;
3355 struct got_reference *head_ref = NULL;
3356 int ch, did_something = 0;
3358 while ((ch = getopt(argc, argv, "")) != -1) {
3370 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3371 "unveil", NULL) == -1)
3377 cwd = getcwd(NULL, 0);
3379 error = got_error_from_errno("getcwd");
3382 error = got_worktree_open(&worktree, cwd);
3386 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3390 error = apply_unveil(got_repo_get_path(repo), 0,
3391 got_worktree_get_root_path(worktree));
3395 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3396 GOT_OBJ_TYPE_COMMIT, repo);
3397 if (error != NULL) {
3398 struct got_reference *ref;
3399 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3401 error = got_ref_open(&ref, repo, argv[0], 0);
3404 error = got_ref_resolve(&commit_id, repo, ref);
3409 error = got_object_id_str(&commit_id_str, commit_id);
3413 error = got_ref_open(&head_ref, repo,
3414 got_worktree_get_head_ref_name(worktree), 0);
3418 error = check_same_branch(commit_id, head_ref, NULL, repo);
3420 if (error->code != GOT_ERR_ANCESTRY)
3424 error = got_error(GOT_ERR_SAME_BRANCH);
3428 error = got_object_open_as_commit(&commit, repo, commit_id);
3431 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3432 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3433 commit_id, repo, update_progress, &did_something, check_cancelled,
3439 printf("Merged commit %s\n", commit_id_str);
3442 got_object_commit_close(commit);
3443 free(commit_id_str);
3445 got_ref_close(head_ref);
3447 got_worktree_close(worktree);
3449 got_repo_close(repo);
3456 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3460 static const struct got_error *
3461 cmd_backout(int argc, char *argv[])
3463 const struct got_error *error = NULL;
3464 struct got_worktree *worktree = NULL;
3465 struct got_repository *repo = NULL;
3466 char *cwd = NULL, *commit_id_str = NULL;
3467 struct got_object_id *commit_id = NULL;
3468 struct got_commit_object *commit = NULL;
3469 struct got_object_qid *pid;
3470 struct got_reference *head_ref = NULL;
3471 int ch, did_something = 0;
3473 while ((ch = getopt(argc, argv, "")) != -1) {
3485 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3486 "unveil", NULL) == -1)
3492 cwd = getcwd(NULL, 0);
3494 error = got_error_from_errno("getcwd");
3497 error = got_worktree_open(&worktree, cwd);
3501 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3505 error = apply_unveil(got_repo_get_path(repo), 0,
3506 got_worktree_get_root_path(worktree));
3510 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3511 GOT_OBJ_TYPE_COMMIT, repo);
3512 if (error != NULL) {
3513 struct got_reference *ref;
3514 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3516 error = got_ref_open(&ref, repo, argv[0], 0);
3519 error = got_ref_resolve(&commit_id, repo, ref);
3524 error = got_object_id_str(&commit_id_str, commit_id);
3528 error = got_ref_open(&head_ref, repo,
3529 got_worktree_get_head_ref_name(worktree), 0);
3533 error = check_same_branch(commit_id, head_ref, NULL, repo);
3537 error = got_object_open_as_commit(&commit, repo, commit_id);
3540 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3542 error = got_error(GOT_ERR_ROOT_COMMIT);
3546 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3547 update_progress, &did_something, check_cancelled, NULL);
3552 printf("Backed out commit %s\n", commit_id_str);
3555 got_object_commit_close(commit);
3556 free(commit_id_str);
3558 got_ref_close(head_ref);
3560 got_worktree_close(worktree);
3562 got_repo_close(repo);
3569 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3575 trim_logmsg(char *logmsg, int limit)
3580 len = strlen(logmsg);
3584 nl = strchr(logmsg, '\n');
3589 static const struct got_error *
3590 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3592 const char *logmsg0 = NULL;
3594 logmsg0 = got_object_commit_get_logmsg(commit);
3596 while (isspace((unsigned char)logmsg0[0]))
3599 *logmsg = strdup(logmsg0);
3600 if (*logmsg == NULL)
3601 return got_error_from_errno("strdup");
3603 trim_logmsg(*logmsg, limit);
3607 static const struct got_error *
3608 show_rebase_progress(struct got_commit_object *commit,
3609 struct got_object_id *old_id, struct got_object_id *new_id)
3611 const struct got_error *err;
3612 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3614 err = got_object_id_str(&old_id_str, old_id);
3619 err = got_object_id_str(&new_id_str, new_id);
3624 old_id_str[12] = '\0';
3626 new_id_str[12] = '\0';
3628 err = get_short_logmsg(&logmsg, 42, commit);
3632 printf("%s -> %s: %s\n", old_id_str,
3633 new_id_str ? new_id_str : "no-op change", logmsg);
3640 static const struct got_error *
3641 rebase_progress(void *arg, unsigned char status, const char *path)
3643 unsigned char *rebase_status = arg;
3645 while (path[0] == '/')
3647 printf("%c %s\n", status, path);
3649 if (*rebase_status == GOT_STATUS_CONFLICT)
3651 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3652 *rebase_status = status;
3656 static const struct got_error *
3657 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3658 struct got_reference *branch, struct got_reference *new_base_branch,
3659 struct got_reference *tmp_branch, struct got_repository *repo)
3661 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3662 return got_worktree_rebase_complete(worktree, fileindex,
3663 new_base_branch, tmp_branch, branch, repo);
3666 static const struct got_error *
3667 rebase_commit(struct got_pathlist_head *merged_paths,
3668 struct got_worktree *worktree, struct got_fileindex *fileindex,
3669 struct got_reference *tmp_branch,
3670 struct got_object_id *commit_id, struct got_repository *repo)
3672 const struct got_error *error;
3673 struct got_commit_object *commit;
3674 struct got_object_id *new_commit_id;
3676 error = got_object_open_as_commit(&commit, repo, commit_id);
3680 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3681 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3683 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3685 error = show_rebase_progress(commit, commit_id, NULL);
3687 error = show_rebase_progress(commit, commit_id, new_commit_id);
3688 free(new_commit_id);
3691 got_object_commit_close(commit);
3695 struct check_path_prefix_arg {
3696 const char *path_prefix;
3701 static const struct got_error *
3702 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3703 struct got_blob_object *blob2, struct got_object_id *id1,
3704 struct got_object_id *id2, const char *path1, const char *path2,
3705 struct got_repository *repo)
3707 struct check_path_prefix_arg *a = arg;
3709 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3710 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3711 return got_error(a->errcode);
3716 static const struct got_error *
3717 check_path_prefix(struct got_object_id *parent_id,
3718 struct got_object_id *commit_id, const char *path_prefix,
3719 int errcode, struct got_repository *repo)
3721 const struct got_error *err;
3722 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3723 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3724 struct check_path_prefix_arg cpp_arg;
3726 if (got_path_is_root_dir(path_prefix))
3729 err = got_object_open_as_commit(&commit, repo, commit_id);
3733 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3737 err = got_object_open_as_tree(&tree1, repo,
3738 got_object_commit_get_tree_id(parent_commit));
3742 err = got_object_open_as_tree(&tree2, repo,
3743 got_object_commit_get_tree_id(commit));
3747 cpp_arg.path_prefix = path_prefix;
3748 while (cpp_arg.path_prefix[0] == '/')
3749 cpp_arg.path_prefix++;
3750 cpp_arg.len = strlen(cpp_arg.path_prefix);
3751 cpp_arg.errcode = errcode;
3752 err = got_diff_tree(tree1, tree2, "", "", repo,
3753 check_path_prefix_in_diff, &cpp_arg, 0);
3756 got_object_tree_close(tree1);
3758 got_object_tree_close(tree2);
3760 got_object_commit_close(commit);
3762 got_object_commit_close(parent_commit);
3766 static const struct got_error *
3767 collect_commits(struct got_object_id_queue *commits,
3768 struct got_object_id *initial_commit_id,
3769 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3770 const char *path_prefix, int path_prefix_errcode,
3771 struct got_repository *repo)
3773 const struct got_error *err = NULL;
3774 struct got_commit_graph *graph = NULL;
3775 struct got_object_id *parent_id = NULL;
3776 struct got_object_qid *qid;
3777 struct got_object_id *commit_id = initial_commit_id;
3779 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3783 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3786 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3787 err = got_commit_graph_iter_next(&parent_id, graph);
3789 if (err->code == GOT_ERR_ITER_COMPLETED) {
3790 err = got_error_msg(GOT_ERR_ANCESTRY,
3791 "ran out of commits to rebase before "
3792 "youngest common ancestor commit has "
3795 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3797 err = got_commit_graph_fetch_commits(graph, 1, repo);
3801 err = check_path_prefix(parent_id, commit_id,
3802 path_prefix, path_prefix_errcode, repo);
3806 err = got_object_qid_alloc(&qid, commit_id);
3809 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3810 commit_id = parent_id;
3814 got_commit_graph_close(graph);
3818 static const struct got_error *
3819 cmd_rebase(int argc, char *argv[])
3821 const struct got_error *error = NULL;
3822 struct got_worktree *worktree = NULL;
3823 struct got_repository *repo = NULL;
3824 struct got_fileindex *fileindex = NULL;
3826 struct got_reference *branch = NULL;
3827 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3828 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3829 struct got_object_id *resume_commit_id = NULL;
3830 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3831 struct got_commit_object *commit = NULL;
3832 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3833 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3834 struct got_object_id_queue commits;
3835 struct got_pathlist_head merged_paths;
3836 const struct got_object_id_queue *parent_ids;
3837 struct got_object_qid *qid, *pid;
3839 SIMPLEQ_INIT(&commits);
3840 TAILQ_INIT(&merged_paths);
3842 while ((ch = getopt(argc, argv, "ac")) != -1) {
3848 continue_rebase = 1;
3860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3861 "unveil", NULL) == -1)
3864 if (abort_rebase && continue_rebase)
3866 else if (abort_rebase || continue_rebase) {
3869 } else if (argc != 1)
3872 cwd = getcwd(NULL, 0);
3874 error = got_error_from_errno("getcwd");
3877 error = got_worktree_open(&worktree, cwd);
3881 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3885 error = apply_unveil(got_repo_get_path(repo), 0,
3886 got_worktree_get_root_path(worktree));
3890 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3896 if (!rebase_in_progress) {
3897 error = got_error(GOT_ERR_NOT_REBASING);
3900 error = got_worktree_rebase_continue(&resume_commit_id,
3901 &new_base_branch, &tmp_branch, &branch, &fileindex,
3905 printf("Switching work tree to %s\n",
3906 got_ref_get_symref_target(new_base_branch));
3907 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3908 new_base_branch, update_progress, &did_something);
3911 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3912 goto done; /* nothing else to do */
3915 if (continue_rebase) {
3916 if (!rebase_in_progress) {
3917 error = got_error(GOT_ERR_NOT_REBASING);
3920 error = got_worktree_rebase_continue(&resume_commit_id,
3921 &new_base_branch, &tmp_branch, &branch, &fileindex,
3926 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3927 resume_commit_id, repo);
3931 yca_id = got_object_id_dup(resume_commit_id);
3932 if (yca_id == NULL) {
3933 error = got_error_from_errno("got_object_id_dup");
3937 error = got_ref_open(&branch, repo, argv[0], 0);
3942 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3946 if (!continue_rebase) {
3947 struct got_object_id *base_commit_id;
3949 base_commit_id = got_worktree_get_base_commit_id(worktree);
3950 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3951 base_commit_id, branch_head_commit_id, repo);
3954 if (yca_id == NULL) {
3955 error = got_error_msg(GOT_ERR_ANCESTRY,
3956 "specified branch shares no common ancestry "
3957 "with work tree's branch");
3961 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3963 if (error->code != GOT_ERR_ANCESTRY)
3967 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3968 "specified branch resolves to a commit which "
3969 "is already contained in work tree's branch");
3972 error = got_worktree_rebase_prepare(&new_base_branch,
3973 &tmp_branch, &fileindex, worktree, branch, repo);
3978 commit_id = branch_head_commit_id;
3979 error = got_object_open_as_commit(&commit, repo, commit_id);
3983 parent_ids = got_object_commit_get_parent_ids(commit);
3984 pid = SIMPLEQ_FIRST(parent_ids);
3985 error = collect_commits(&commits, commit_id, pid->id,
3986 yca_id, got_worktree_get_path_prefix(worktree),
3987 GOT_ERR_REBASE_PATH, repo);
3988 got_object_commit_close(commit);
3993 if (SIMPLEQ_EMPTY(&commits)) {
3994 if (continue_rebase)
3995 error = rebase_complete(worktree, fileindex,
3996 branch, new_base_branch, tmp_branch, repo);
3998 error = got_error(GOT_ERR_EMPTY_REBASE);
4003 SIMPLEQ_FOREACH(qid, &commits, entry) {
4004 commit_id = qid->id;
4005 parent_id = pid ? pid->id : yca_id;
4008 error = got_worktree_rebase_merge_files(&merged_paths,
4009 worktree, fileindex, parent_id, commit_id, repo,
4010 rebase_progress, &rebase_status, check_cancelled, NULL);
4014 if (rebase_status == GOT_STATUS_CONFLICT) {
4015 got_worktree_rebase_pathlist_free(&merged_paths);
4019 error = rebase_commit(&merged_paths, worktree, fileindex,
4020 tmp_branch, commit_id, repo);
4021 got_worktree_rebase_pathlist_free(&merged_paths);
4026 if (rebase_status == GOT_STATUS_CONFLICT) {
4027 error = got_worktree_rebase_postpone(worktree, fileindex);
4030 error = got_error_msg(GOT_ERR_CONFLICTS,
4031 "conflicts must be resolved before rebasing can continue");
4033 error = rebase_complete(worktree, fileindex, branch,
4034 new_base_branch, tmp_branch, repo);
4036 got_object_id_queue_free(&commits);
4037 free(branch_head_commit_id);
4038 free(resume_commit_id);
4041 got_object_commit_close(commit);
4043 got_ref_close(branch);
4044 if (new_base_branch)
4045 got_ref_close(new_base_branch);
4047 got_ref_close(tmp_branch);
4049 got_worktree_close(worktree);
4051 got_repo_close(repo);
4056 usage_histedit(void)
4058 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4063 #define GOT_HISTEDIT_PICK 'p'
4064 #define GOT_HISTEDIT_EDIT 'e'
4065 #define GOT_HISTEDIT_FOLD 'f'
4066 #define GOT_HISTEDIT_DROP 'd'
4067 #define GOT_HISTEDIT_MESG 'm'
4069 static struct got_histedit_cmd {
4073 } got_histedit_cmds[] = {
4074 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4075 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4076 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4077 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4078 { GOT_HISTEDIT_MESG, "mesg",
4079 "single-line log message for commit above (open editor if empty)" },
4082 struct got_histedit_list_entry {
4083 TAILQ_ENTRY(got_histedit_list_entry) entry;
4084 struct got_object_id *commit_id;
4085 const struct got_histedit_cmd *cmd;
4088 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4090 static const struct got_error *
4091 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4092 FILE *f, struct got_repository *repo)
4094 const struct got_error *err = NULL;
4095 char *logmsg = NULL, *id_str = NULL;
4096 struct got_commit_object *commit = NULL;
4099 err = got_object_open_as_commit(&commit, repo, commit_id);
4103 err = get_short_logmsg(&logmsg, 34, commit);
4107 err = got_object_id_str(&id_str, commit_id);
4111 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4113 err = got_ferror(f, GOT_ERR_IO);
4116 got_object_commit_close(commit);
4122 static const struct got_error *
4123 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4124 struct got_repository *repo)
4126 const struct got_error *err = NULL;
4127 struct got_object_qid *qid;
4129 if (SIMPLEQ_EMPTY(commits))
4130 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4132 SIMPLEQ_FOREACH(qid, commits, entry) {
4133 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4142 static const struct got_error *
4143 write_cmd_list(FILE *f)
4145 const struct got_error *err = NULL;
4148 n = fprintf(f, "# Available histedit commands:\n");
4150 return got_ferror(f, GOT_ERR_IO);
4152 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4153 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4154 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4157 err = got_ferror(f, GOT_ERR_IO);
4161 n = fprintf(f, "# Commits will be processed in order from top to "
4162 "bottom of this file.\n");
4164 return got_ferror(f, GOT_ERR_IO);
4168 static const struct got_error *
4169 histedit_syntax_error(int lineno)
4171 static char msg[42];
4174 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4176 if (ret == -1 || ret >= sizeof(msg))
4177 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4179 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4182 static const struct got_error *
4183 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4184 char *logmsg, struct got_repository *repo)
4186 const struct got_error *err;
4187 struct got_commit_object *folded_commit = NULL;
4190 err = got_object_id_str(&id_str, hle->commit_id);
4194 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4198 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4199 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4200 got_object_commit_get_logmsg(folded_commit)) == -1) {
4201 err = got_error_from_errno("asprintf");
4206 got_object_commit_close(folded_commit);
4211 static struct got_histedit_list_entry *
4212 get_folded_commits(struct got_histedit_list_entry *hle)
4214 struct got_histedit_list_entry *prev, *folded = NULL;
4216 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4217 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4218 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4219 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4221 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4227 static const struct got_error *
4228 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4229 struct got_repository *repo)
4231 char *logmsg_path = NULL, *id_str = NULL;
4232 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4233 const struct got_error *err = NULL;
4234 struct got_commit_object *commit = NULL;
4236 struct got_histedit_list_entry *folded = NULL;
4238 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4242 folded = get_folded_commits(hle);
4244 while (folded != hle) {
4245 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4246 folded = TAILQ_NEXT(folded, entry);
4249 err = append_folded_commit_msg(&new_msg, folded,
4255 folded = TAILQ_NEXT(folded, entry);
4259 err = got_object_id_str(&id_str, hle->commit_id);
4262 if (asprintf(&new_msg,
4263 "%s\n# original log message of commit %s: %s",
4264 logmsg ? logmsg : "", id_str,
4265 got_object_commit_get_logmsg(commit)) == -1) {
4266 err = got_error_from_errno("asprintf");
4272 err = got_object_id_str(&id_str, hle->commit_id);
4276 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4280 dprintf(fd, logmsg);
4283 err = get_editor(&editor);
4287 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4289 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4292 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4293 if (hle->logmsg == NULL)
4294 err = got_error_from_errno("strdup");
4297 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4298 err = got_error_from_errno2("unlink", logmsg_path);
4303 got_object_commit_close(commit);
4307 static const struct got_error *
4308 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4309 FILE *f, struct got_repository *repo)
4311 const struct got_error *err = NULL;
4312 char *line = NULL, *p, *end;
4316 const struct got_histedit_cmd *cmd;
4317 struct got_object_id *commit_id = NULL;
4318 struct got_histedit_list_entry *hle = NULL;
4321 len = getline(&line, &size, f);
4323 const struct got_error *getline_err;
4326 getline_err = got_error_from_errno("getline");
4327 err = got_ferror(f, getline_err->code);
4332 while (isspace((unsigned char)p[0]))
4334 if (p[0] == '#' || p[0] == '\0') {
4340 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4341 cmd = &got_histedit_cmds[i];
4342 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4343 isspace((unsigned char)p[strlen(cmd->name)])) {
4344 p += strlen(cmd->name);
4347 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4352 if (i == nitems(got_histedit_cmds)) {
4353 err = histedit_syntax_error(lineno);
4356 while (isspace((unsigned char)p[0]))
4358 if (cmd->code == GOT_HISTEDIT_MESG) {
4359 if (hle == NULL || hle->logmsg != NULL) {
4360 err = got_error(GOT_ERR_HISTEDIT_CMD);
4364 err = histedit_edit_logmsg(hle, repo);
4368 hle->logmsg = strdup(p);
4369 if (hle->logmsg == NULL) {
4370 err = got_error_from_errno("strdup");
4379 while (end[0] && !isspace((unsigned char)end[0]))
4383 err = got_object_resolve_id_str(&commit_id, repo, p);
4385 /* override error code */
4386 err = histedit_syntax_error(lineno);
4390 hle = malloc(sizeof(*hle));
4392 err = got_error_from_errno("malloc");
4396 hle->commit_id = commit_id;
4401 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4409 static const struct got_error *
4410 histedit_check_script(struct got_histedit_list *histedit_cmds,
4411 struct got_object_id_queue *commits, struct got_repository *repo)
4413 const struct got_error *err = NULL;
4414 struct got_object_qid *qid;
4415 struct got_histedit_list_entry *hle;
4416 static char msg[80];
4419 if (TAILQ_EMPTY(histedit_cmds))
4420 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4421 "histedit script contains no commands");
4423 SIMPLEQ_FOREACH(qid, commits, entry) {
4424 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4425 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4429 err = got_object_id_str(&id_str, qid->id);
4432 snprintf(msg, sizeof(msg),
4433 "commit %s missing from histedit script", id_str);
4435 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4439 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4440 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4441 "last commit in histedit script cannot be folded");
4446 static const struct got_error *
4447 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4448 const char *path, struct got_object_id_queue *commits,
4449 struct got_repository *repo)
4451 const struct got_error *err = NULL;
4455 err = get_editor(&editor);
4459 if (spawn_editor(editor, path) == -1) {
4460 err = got_error_from_errno("failed spawning editor");
4464 f = fopen(path, "r");
4466 err = got_error_from_errno("fopen");
4469 err = histedit_parse_list(histedit_cmds, f, repo);
4473 err = histedit_check_script(histedit_cmds, commits, repo);
4475 if (f && fclose(f) != 0 && err == NULL)
4476 err = got_error_from_errno("fclose");
4481 static const struct got_error *
4482 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4483 struct got_object_id_queue *, const char *, struct got_repository *);
4485 static const struct got_error *
4486 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4487 struct got_object_id_queue *commits, struct got_repository *repo)
4489 const struct got_error *err;
4493 err = got_opentemp_named(&path, &f, "got-histedit");
4497 err = write_cmd_list(f);
4501 err = histedit_write_commit_list(commits, f, repo);
4505 if (fclose(f) != 0) {
4506 err = got_error_from_errno("fclose");
4511 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4513 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4514 err->code != GOT_ERR_HISTEDIT_CMD)
4516 err = histedit_edit_list_retry(histedit_cmds, err,
4517 commits, path, repo);
4520 if (f && fclose(f) != 0 && err == NULL)
4521 err = got_error_from_errno("fclose");
4522 if (path && unlink(path) != 0 && err == NULL)
4523 err = got_error_from_errno2("unlink", path);
4528 static const struct got_error *
4529 histedit_save_list(struct got_histedit_list *histedit_cmds,
4530 struct got_worktree *worktree, struct got_repository *repo)
4532 const struct got_error *err = NULL;
4535 struct got_histedit_list_entry *hle;
4536 struct got_commit_object *commit = NULL;
4538 err = got_worktree_get_histedit_script_path(&path, worktree);
4542 f = fopen(path, "w");
4544 err = got_error_from_errno2("fopen", path);
4547 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4548 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4554 int n = fprintf(f, "%c %s\n",
4555 GOT_HISTEDIT_MESG, hle->logmsg);
4557 err = got_ferror(f, GOT_ERR_IO);
4563 if (f && fclose(f) != 0 && err == NULL)
4564 err = got_error_from_errno("fclose");
4567 got_object_commit_close(commit);
4572 histedit_free_list(struct got_histedit_list *histedit_cmds)
4574 struct got_histedit_list_entry *hle;
4576 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4577 TAILQ_REMOVE(histedit_cmds, hle, entry);
4582 static const struct got_error *
4583 histedit_load_list(struct got_histedit_list *histedit_cmds,
4584 const char *path, struct got_repository *repo)
4586 const struct got_error *err = NULL;
4589 f = fopen(path, "r");
4591 err = got_error_from_errno2("fopen", path);
4595 err = histedit_parse_list(histedit_cmds, f, repo);
4597 if (f && fclose(f) != 0 && err == NULL)
4598 err = got_error_from_errno("fclose");
4602 static const struct got_error *
4603 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4604 const struct got_error *edit_err, struct got_object_id_queue *commits,
4605 const char *path, struct got_repository *repo)
4607 const struct got_error *err = NULL, *prev_err = edit_err;
4610 while (resp != 'c' && resp != 'r' && resp != 'a') {
4611 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4612 "or (a)bort: ", getprogname(), prev_err->msg);
4615 histedit_free_list(histedit_cmds);
4616 err = histedit_run_editor(histedit_cmds, path, commits,
4619 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4620 err->code != GOT_ERR_HISTEDIT_CMD)
4627 } else if (resp == 'r') {
4628 histedit_free_list(histedit_cmds);
4629 err = histedit_edit_script(histedit_cmds,
4632 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4633 err->code != GOT_ERR_HISTEDIT_CMD)
4640 } else if (resp == 'a') {
4641 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4644 printf("invalid response '%c'\n", resp);
4650 static const struct got_error *
4651 histedit_complete(struct got_worktree *worktree,
4652 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4653 struct got_reference *branch, struct got_repository *repo)
4655 printf("Switching work tree to %s\n",
4656 got_ref_get_symref_target(branch));
4657 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4661 static const struct got_error *
4662 show_histedit_progress(struct got_commit_object *commit,
4663 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4665 const struct got_error *err;
4666 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4668 err = got_object_id_str(&old_id_str, hle->commit_id);
4673 err = got_object_id_str(&new_id_str, new_id);
4678 old_id_str[12] = '\0';
4680 new_id_str[12] = '\0';
4683 logmsg = strdup(hle->logmsg);
4684 if (logmsg == NULL) {
4685 err = got_error_from_errno("strdup");
4688 trim_logmsg(logmsg, 42);
4690 err = get_short_logmsg(&logmsg, 42, commit);
4695 switch (hle->cmd->code) {
4696 case GOT_HISTEDIT_PICK:
4697 case GOT_HISTEDIT_EDIT:
4698 printf("%s -> %s: %s\n", old_id_str,
4699 new_id_str ? new_id_str : "no-op change", logmsg);
4701 case GOT_HISTEDIT_DROP:
4702 case GOT_HISTEDIT_FOLD:
4703 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4716 static const struct got_error *
4717 histedit_commit(struct got_pathlist_head *merged_paths,
4718 struct got_worktree *worktree, struct got_fileindex *fileindex,
4719 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4720 struct got_repository *repo)
4722 const struct got_error *err;
4723 struct got_commit_object *commit;
4724 struct got_object_id *new_commit_id;
4726 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4727 && hle->logmsg == NULL) {
4728 err = histedit_edit_logmsg(hle, repo);
4733 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4737 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4738 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4741 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4743 err = show_histedit_progress(commit, hle, NULL);
4745 err = show_histedit_progress(commit, hle, new_commit_id);
4746 free(new_commit_id);
4749 got_object_commit_close(commit);
4753 static const struct got_error *
4754 histedit_skip_commit(struct got_histedit_list_entry *hle,
4755 struct got_worktree *worktree, struct got_repository *repo)
4757 const struct got_error *error;
4758 struct got_commit_object *commit;
4760 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4765 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4769 error = show_histedit_progress(commit, hle, NULL);
4770 got_object_commit_close(commit);
4774 static const struct got_error *
4775 cmd_histedit(int argc, char *argv[])
4777 const struct got_error *error = NULL;
4778 struct got_worktree *worktree = NULL;
4779 struct got_fileindex *fileindex = NULL;
4780 struct got_repository *repo = NULL;
4782 struct got_reference *branch = NULL;
4783 struct got_reference *tmp_branch = NULL;
4784 struct got_object_id *resume_commit_id = NULL;
4785 struct got_object_id *base_commit_id = NULL;
4786 struct got_object_id *head_commit_id = NULL;
4787 struct got_commit_object *commit = NULL;
4788 int ch, rebase_in_progress = 0, did_something;
4789 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4790 const char *edit_script_path = NULL;
4791 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4792 struct got_object_id_queue commits;
4793 struct got_pathlist_head merged_paths;
4794 const struct got_object_id_queue *parent_ids;
4795 struct got_object_qid *pid;
4796 struct got_histedit_list histedit_cmds;
4797 struct got_histedit_list_entry *hle;
4799 SIMPLEQ_INIT(&commits);
4800 TAILQ_INIT(&histedit_cmds);
4801 TAILQ_INIT(&merged_paths);
4803 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4812 edit_script_path = optarg;
4824 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4825 "unveil", NULL) == -1)
4828 if (abort_edit && continue_edit)
4834 * This command cannot apply unveil(2) in all cases because the
4835 * user may choose to run an editor to edit the histedit script
4836 * and to edit individual commit log messages.
4837 * unveil(2) traverses exec(2); if an editor is used we have to
4838 * apply unveil after edit script and log messages have been written.
4839 * XXX TODO: Make use of unveil(2) where possible.
4842 cwd = getcwd(NULL, 0);
4844 error = got_error_from_errno("getcwd");
4847 error = got_worktree_open(&worktree, cwd);
4851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4855 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4858 if (rebase_in_progress) {
4859 error = got_error(GOT_ERR_REBASING);
4863 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4867 if (edit_in_progress && abort_edit) {
4868 error = got_worktree_histedit_continue(&resume_commit_id,
4869 &tmp_branch, &branch, &base_commit_id, &fileindex,
4873 printf("Switching work tree to %s\n",
4874 got_ref_get_symref_target(branch));
4875 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4876 branch, base_commit_id, update_progress, &did_something);
4879 printf("Histedit of %s aborted\n",
4880 got_ref_get_symref_target(branch));
4881 goto done; /* nothing else to do */
4882 } else if (abort_edit) {
4883 error = got_error(GOT_ERR_NOT_HISTEDIT);
4887 if (continue_edit) {
4890 if (!edit_in_progress) {
4891 error = got_error(GOT_ERR_NOT_HISTEDIT);
4895 error = got_worktree_get_histedit_script_path(&path, worktree);
4899 error = histedit_load_list(&histedit_cmds, path, repo);
4904 error = got_worktree_histedit_continue(&resume_commit_id,
4905 &tmp_branch, &branch, &base_commit_id, &fileindex,
4910 error = got_ref_resolve(&head_commit_id, repo, branch);
4914 error = got_object_open_as_commit(&commit, repo,
4918 parent_ids = got_object_commit_get_parent_ids(commit);
4919 pid = SIMPLEQ_FIRST(parent_ids);
4921 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4924 error = collect_commits(&commits, head_commit_id, pid->id,
4925 base_commit_id, got_worktree_get_path_prefix(worktree),
4926 GOT_ERR_HISTEDIT_PATH, repo);
4927 got_object_commit_close(commit);
4932 if (edit_in_progress) {
4933 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4937 error = got_ref_open(&branch, repo,
4938 got_worktree_get_head_ref_name(worktree), 0);
4942 error = got_ref_resolve(&head_commit_id, repo, branch);
4943 got_ref_close(branch);
4948 error = got_object_open_as_commit(&commit, repo,
4952 parent_ids = got_object_commit_get_parent_ids(commit);
4953 pid = SIMPLEQ_FIRST(parent_ids);
4955 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4958 error = collect_commits(&commits, head_commit_id, pid->id,
4959 got_worktree_get_base_commit_id(worktree),
4960 got_worktree_get_path_prefix(worktree),
4961 GOT_ERR_HISTEDIT_PATH, repo);
4962 got_object_commit_close(commit);
4967 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4968 &base_commit_id, &fileindex, worktree, repo);
4972 if (edit_script_path) {
4973 error = histedit_load_list(&histedit_cmds,
4974 edit_script_path, repo);
4976 got_worktree_histedit_abort(worktree, fileindex,
4977 repo, branch, base_commit_id,
4978 update_progress, &did_something);
4982 error = histedit_edit_script(&histedit_cmds, &commits,
4985 got_worktree_histedit_abort(worktree, fileindex,
4986 repo, branch, base_commit_id,
4987 update_progress, &did_something);
4993 error = histedit_save_list(&histedit_cmds, worktree,
4996 got_worktree_histedit_abort(worktree, fileindex,
4997 repo, branch, base_commit_id,
4998 update_progress, &did_something);
5004 error = histedit_check_script(&histedit_cmds, &commits, repo);
5008 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5009 if (resume_commit_id) {
5010 if (got_object_id_cmp(hle->commit_id,
5011 resume_commit_id) != 0)
5014 resume_commit_id = NULL;
5015 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5016 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5017 error = histedit_skip_commit(hle, worktree,
5020 error = histedit_commit(NULL, worktree,
5021 fileindex, tmp_branch, hle, repo);
5028 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5029 error = histedit_skip_commit(hle, worktree, repo);
5035 error = got_object_open_as_commit(&commit, repo,
5039 parent_ids = got_object_commit_get_parent_ids(commit);
5040 pid = SIMPLEQ_FIRST(parent_ids);
5042 error = got_worktree_histedit_merge_files(&merged_paths,
5043 worktree, fileindex, pid->id, hle->commit_id, repo,
5044 rebase_progress, &rebase_status, check_cancelled, NULL);
5047 got_object_commit_close(commit);
5050 if (rebase_status == GOT_STATUS_CONFLICT) {
5051 got_worktree_rebase_pathlist_free(&merged_paths);
5055 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5057 error = got_object_id_str(&id_str, hle->commit_id);
5060 printf("Stopping histedit for amending commit %s\n",
5063 got_worktree_rebase_pathlist_free(&merged_paths);
5064 error = got_worktree_histedit_postpone(worktree,
5069 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5070 error = histedit_skip_commit(hle, worktree, repo);
5076 error = histedit_commit(&merged_paths, worktree, fileindex,
5077 tmp_branch, hle, repo);
5078 got_worktree_rebase_pathlist_free(&merged_paths);
5083 if (rebase_status == GOT_STATUS_CONFLICT) {
5084 error = got_worktree_histedit_postpone(worktree, fileindex);
5087 error = got_error_msg(GOT_ERR_CONFLICTS,
5088 "conflicts must be resolved before rebasing can continue");
5090 error = histedit_complete(worktree, fileindex, tmp_branch,
5093 got_object_id_queue_free(&commits);
5094 histedit_free_list(&histedit_cmds);
5095 free(head_commit_id);
5096 free(base_commit_id);
5097 free(resume_commit_id);
5099 got_object_commit_close(commit);
5101 got_ref_close(branch);
5103 got_ref_close(tmp_branch);
5105 got_worktree_close(worktree);
5107 got_repo_close(repo);