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(&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(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(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(&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",
3147 struct collect_commit_logmsg_arg {
3148 const char *cmdline_log;
3150 const char *worktree_path;
3151 const char *branch_name;
3152 const char *repo_path;
3157 static const struct got_error *
3158 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3161 char *initial_content = NULL;
3162 struct got_pathlist_entry *pe;
3163 const struct got_error *err = NULL;
3164 char *template = NULL;
3165 struct collect_commit_logmsg_arg *a = arg;
3169 /* if a message was specified on the command line, just use it */
3170 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3171 len = strlen(a->cmdline_log) + 1;
3172 *logmsg = malloc(len + 1);
3173 if (*logmsg == NULL)
3174 return got_error_from_errno("malloc");
3175 strlcpy(*logmsg, a->cmdline_log, len);
3179 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3180 return got_error_from_errno("asprintf");
3182 if (asprintf(&initial_content,
3183 "\n# changes to be committed on branch %s:\n",
3184 a->branch_name) == -1)
3185 return got_error_from_errno("asprintf");
3187 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3191 dprintf(fd, initial_content);
3193 TAILQ_FOREACH(pe, commitable_paths, entry) {
3194 struct got_commitable *ct = pe->data;
3195 dprintf(fd, "# %c %s\n",
3196 got_commitable_get_status(ct),
3197 got_commitable_get_path(ct));
3201 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3203 unlink(a->logmsg_path);
3204 free(a->logmsg_path);
3205 free(initial_content);
3208 /* Editor is done; we can now apply unveil(2) */
3210 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3219 static const struct got_error *
3220 cmd_commit(int argc, char *argv[])
3222 const struct got_error *error = NULL;
3223 struct got_worktree *worktree = NULL;
3224 struct got_repository *repo = NULL;
3225 char *cwd = NULL, *id_str = NULL;
3226 struct got_object_id *id = NULL;
3227 const char *logmsg = NULL;
3228 const char *got_author = getenv("GOT_AUTHOR");
3229 struct collect_commit_logmsg_arg cl_arg;
3230 char *editor = NULL;
3231 int ch, rebase_in_progress;
3232 struct got_pathlist_head paths;
3236 while ((ch = getopt(argc, argv, "m:")) != -1) {
3251 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3252 "unveil", NULL) == -1)
3255 if (got_author == NULL) {
3256 /* TODO: Look current user up in password database */
3257 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3261 cwd = getcwd(NULL, 0);
3263 error = got_error_from_errno("getcwd");
3266 error = got_worktree_open(&worktree, cwd);
3270 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3273 if (rebase_in_progress) {
3274 error = got_error(GOT_ERR_REBASING);
3278 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3282 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3287 * unveil(2) traverses exec(2); if an editor is used we have
3288 * to apply unveil after the log message has been written.
3290 if (logmsg == NULL || strlen(logmsg) == 0)
3291 error = get_editor(&editor);
3293 error = apply_unveil(got_repo_get_path(repo), 0,
3294 got_worktree_get_root_path(worktree));
3298 cl_arg.editor = editor;
3299 cl_arg.cmdline_log = logmsg;
3300 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3301 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3302 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3303 cl_arg.branch_name += 5;
3304 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3305 cl_arg.branch_name += 6;
3306 cl_arg.repo_path = got_repo_get_path(repo);
3307 cl_arg.logmsg_path = NULL;
3308 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3309 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3311 if (cl_arg.logmsg_path)
3312 fprintf(stderr, "%s: log message preserved in %s\n",
3313 getprogname(), cl_arg.logmsg_path);
3317 if (cl_arg.logmsg_path)
3318 unlink(cl_arg.logmsg_path);
3320 error = got_object_id_str(&id_str, id);
3323 printf("Created commit %s\n", id_str);
3326 got_repo_close(repo);
3328 got_worktree_close(worktree);
3336 usage_cherrypick(void)
3338 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3342 static const struct got_error *
3343 cmd_cherrypick(int argc, char *argv[])
3345 const struct got_error *error = NULL;
3346 struct got_worktree *worktree = NULL;
3347 struct got_repository *repo = NULL;
3348 char *cwd = NULL, *commit_id_str = NULL;
3349 struct got_object_id *commit_id = NULL;
3350 struct got_commit_object *commit = NULL;
3351 struct got_object_qid *pid;
3352 struct got_reference *head_ref = NULL;
3353 int ch, did_something = 0;
3355 while ((ch = getopt(argc, argv, "")) != -1) {
3367 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3368 "unveil", NULL) == -1)
3374 cwd = getcwd(NULL, 0);
3376 error = got_error_from_errno("getcwd");
3379 error = got_worktree_open(&worktree, cwd);
3383 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3387 error = apply_unveil(got_repo_get_path(repo), 0,
3388 got_worktree_get_root_path(worktree));
3392 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3393 GOT_OBJ_TYPE_COMMIT, repo);
3394 if (error != NULL) {
3395 struct got_reference *ref;
3396 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3398 error = got_ref_open(&ref, repo, argv[0], 0);
3401 error = got_ref_resolve(&commit_id, repo, ref);
3406 error = got_object_id_str(&commit_id_str, commit_id);
3410 error = got_ref_open(&head_ref, repo,
3411 got_worktree_get_head_ref_name(worktree), 0);
3415 error = check_same_branch(commit_id, head_ref, NULL, repo);
3417 if (error->code != GOT_ERR_ANCESTRY)
3421 error = got_error(GOT_ERR_SAME_BRANCH);
3425 error = got_object_open_as_commit(&commit, repo, commit_id);
3428 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3429 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3430 commit_id, repo, update_progress, &did_something, check_cancelled,
3436 printf("Merged commit %s\n", commit_id_str);
3439 got_object_commit_close(commit);
3440 free(commit_id_str);
3442 got_ref_close(head_ref);
3444 got_worktree_close(worktree);
3446 got_repo_close(repo);
3453 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3457 static const struct got_error *
3458 cmd_backout(int argc, char *argv[])
3460 const struct got_error *error = NULL;
3461 struct got_worktree *worktree = NULL;
3462 struct got_repository *repo = NULL;
3463 char *cwd = NULL, *commit_id_str = NULL;
3464 struct got_object_id *commit_id = NULL;
3465 struct got_commit_object *commit = NULL;
3466 struct got_object_qid *pid;
3467 struct got_reference *head_ref = NULL;
3468 int ch, did_something = 0;
3470 while ((ch = getopt(argc, argv, "")) != -1) {
3482 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3483 "unveil", NULL) == -1)
3489 cwd = getcwd(NULL, 0);
3491 error = got_error_from_errno("getcwd");
3494 error = got_worktree_open(&worktree, cwd);
3498 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3502 error = apply_unveil(got_repo_get_path(repo), 0,
3503 got_worktree_get_root_path(worktree));
3507 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3508 GOT_OBJ_TYPE_COMMIT, repo);
3509 if (error != NULL) {
3510 struct got_reference *ref;
3511 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3513 error = got_ref_open(&ref, repo, argv[0], 0);
3516 error = got_ref_resolve(&commit_id, repo, ref);
3521 error = got_object_id_str(&commit_id_str, commit_id);
3525 error = got_ref_open(&head_ref, repo,
3526 got_worktree_get_head_ref_name(worktree), 0);
3530 error = check_same_branch(commit_id, head_ref, NULL, repo);
3534 error = got_object_open_as_commit(&commit, repo, commit_id);
3537 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3539 error = got_error(GOT_ERR_ROOT_COMMIT);
3543 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3544 update_progress, &did_something, check_cancelled, NULL);
3549 printf("Backed out commit %s\n", commit_id_str);
3552 got_object_commit_close(commit);
3553 free(commit_id_str);
3555 got_ref_close(head_ref);
3557 got_worktree_close(worktree);
3559 got_repo_close(repo);
3566 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3572 trim_logmsg(char *logmsg, int limit)
3577 len = strlen(logmsg);
3581 nl = strchr(logmsg, '\n');
3586 static const struct got_error *
3587 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3589 const char *logmsg0 = NULL;
3591 logmsg0 = got_object_commit_get_logmsg(commit);
3593 while (isspace((unsigned char)logmsg0[0]))
3596 *logmsg = strdup(logmsg0);
3597 if (*logmsg == NULL)
3598 return got_error_from_errno("strdup");
3600 trim_logmsg(*logmsg, limit);
3604 static const struct got_error *
3605 show_rebase_progress(struct got_commit_object *commit,
3606 struct got_object_id *old_id, struct got_object_id *new_id)
3608 const struct got_error *err;
3609 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3611 err = got_object_id_str(&old_id_str, old_id);
3616 err = got_object_id_str(&new_id_str, new_id);
3621 old_id_str[12] = '\0';
3623 new_id_str[12] = '\0';
3625 err = get_short_logmsg(&logmsg, 42, commit);
3629 printf("%s -> %s: %s\n", old_id_str,
3630 new_id_str ? new_id_str : "no-op change", logmsg);
3637 static const struct got_error *
3638 rebase_progress(void *arg, unsigned char status, const char *path)
3640 unsigned char *rebase_status = arg;
3642 while (path[0] == '/')
3644 printf("%c %s\n", status, path);
3646 if (*rebase_status == GOT_STATUS_CONFLICT)
3648 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3649 *rebase_status = status;
3653 static const struct got_error *
3654 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3655 struct got_reference *branch, struct got_reference *new_base_branch,
3656 struct got_reference *tmp_branch, struct got_repository *repo)
3658 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3659 return got_worktree_rebase_complete(worktree, fileindex,
3660 new_base_branch, tmp_branch, branch, repo);
3663 static const struct got_error *
3664 rebase_commit(struct got_pathlist_head *merged_paths,
3665 struct got_worktree *worktree, struct got_fileindex *fileindex,
3666 struct got_reference *tmp_branch,
3667 struct got_object_id *commit_id, struct got_repository *repo)
3669 const struct got_error *error;
3670 struct got_commit_object *commit;
3671 struct got_object_id *new_commit_id;
3673 error = got_object_open_as_commit(&commit, repo, commit_id);
3677 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3678 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3680 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3682 error = show_rebase_progress(commit, commit_id, NULL);
3684 error = show_rebase_progress(commit, commit_id, new_commit_id);
3685 free(new_commit_id);
3688 got_object_commit_close(commit);
3692 struct check_path_prefix_arg {
3693 const char *path_prefix;
3698 static const struct got_error *
3699 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3700 struct got_blob_object *blob2, struct got_object_id *id1,
3701 struct got_object_id *id2, const char *path1, const char *path2,
3702 struct got_repository *repo)
3704 struct check_path_prefix_arg *a = arg;
3706 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3707 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3708 return got_error(a->errcode);
3713 static const struct got_error *
3714 check_path_prefix(struct got_object_id *parent_id,
3715 struct got_object_id *commit_id, const char *path_prefix,
3716 int errcode, struct got_repository *repo)
3718 const struct got_error *err;
3719 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3720 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3721 struct check_path_prefix_arg cpp_arg;
3723 if (got_path_is_root_dir(path_prefix))
3726 err = got_object_open_as_commit(&commit, repo, commit_id);
3730 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3734 err = got_object_open_as_tree(&tree1, repo,
3735 got_object_commit_get_tree_id(parent_commit));
3739 err = got_object_open_as_tree(&tree2, repo,
3740 got_object_commit_get_tree_id(commit));
3744 cpp_arg.path_prefix = path_prefix;
3745 while (cpp_arg.path_prefix[0] == '/')
3746 cpp_arg.path_prefix++;
3747 cpp_arg.len = strlen(cpp_arg.path_prefix);
3748 cpp_arg.errcode = errcode;
3749 err = got_diff_tree(tree1, tree2, "", "", repo,
3750 check_path_prefix_in_diff, &cpp_arg, 0);
3753 got_object_tree_close(tree1);
3755 got_object_tree_close(tree2);
3757 got_object_commit_close(commit);
3759 got_object_commit_close(parent_commit);
3763 static const struct got_error *
3764 collect_commits(struct got_object_id_queue *commits,
3765 struct got_object_id *initial_commit_id,
3766 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3767 const char *path_prefix, int path_prefix_errcode,
3768 struct got_repository *repo)
3770 const struct got_error *err = NULL;
3771 struct got_commit_graph *graph = NULL;
3772 struct got_object_id *parent_id = NULL;
3773 struct got_object_qid *qid;
3774 struct got_object_id *commit_id = initial_commit_id;
3776 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3780 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3783 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3784 err = got_commit_graph_iter_next(&parent_id, graph);
3786 if (err->code == GOT_ERR_ITER_COMPLETED) {
3787 err = got_error_msg(GOT_ERR_ANCESTRY,
3788 "ran out of commits to rebase before "
3789 "youngest common ancestor commit has "
3792 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3794 err = got_commit_graph_fetch_commits(graph, 1, repo);
3798 err = check_path_prefix(parent_id, commit_id,
3799 path_prefix, path_prefix_errcode, repo);
3803 err = got_object_qid_alloc(&qid, commit_id);
3806 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3807 commit_id = parent_id;
3811 got_commit_graph_close(graph);
3815 static const struct got_error *
3816 cmd_rebase(int argc, char *argv[])
3818 const struct got_error *error = NULL;
3819 struct got_worktree *worktree = NULL;
3820 struct got_repository *repo = NULL;
3821 struct got_fileindex *fileindex = NULL;
3823 struct got_reference *branch = NULL;
3824 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3825 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3826 struct got_object_id *resume_commit_id = NULL;
3827 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3828 struct got_commit_object *commit = NULL;
3829 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3830 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3831 struct got_object_id_queue commits;
3832 struct got_pathlist_head merged_paths;
3833 const struct got_object_id_queue *parent_ids;
3834 struct got_object_qid *qid, *pid;
3836 SIMPLEQ_INIT(&commits);
3837 TAILQ_INIT(&merged_paths);
3839 while ((ch = getopt(argc, argv, "ac")) != -1) {
3845 continue_rebase = 1;
3857 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3858 "unveil", NULL) == -1)
3861 if (abort_rebase && continue_rebase)
3863 else if (abort_rebase || continue_rebase) {
3866 } else if (argc != 1)
3869 cwd = getcwd(NULL, 0);
3871 error = got_error_from_errno("getcwd");
3874 error = got_worktree_open(&worktree, cwd);
3878 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3882 error = apply_unveil(got_repo_get_path(repo), 0,
3883 got_worktree_get_root_path(worktree));
3887 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3893 if (!rebase_in_progress) {
3894 error = got_error(GOT_ERR_NOT_REBASING);
3897 error = got_worktree_rebase_continue(&resume_commit_id,
3898 &new_base_branch, &tmp_branch, &branch, &fileindex,
3902 printf("Switching work tree to %s\n",
3903 got_ref_get_symref_target(new_base_branch));
3904 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3905 new_base_branch, update_progress, &did_something);
3908 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3909 goto done; /* nothing else to do */
3912 if (continue_rebase) {
3913 if (!rebase_in_progress) {
3914 error = got_error(GOT_ERR_NOT_REBASING);
3917 error = got_worktree_rebase_continue(&resume_commit_id,
3918 &new_base_branch, &tmp_branch, &branch, &fileindex,
3923 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3924 resume_commit_id, repo);
3928 yca_id = got_object_id_dup(resume_commit_id);
3929 if (yca_id == NULL) {
3930 error = got_error_from_errno("got_object_id_dup");
3934 error = got_ref_open(&branch, repo, argv[0], 0);
3939 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3943 if (!continue_rebase) {
3944 struct got_object_id *base_commit_id;
3946 base_commit_id = got_worktree_get_base_commit_id(worktree);
3947 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3948 base_commit_id, branch_head_commit_id, repo);
3951 if (yca_id == NULL) {
3952 error = got_error_msg(GOT_ERR_ANCESTRY,
3953 "specified branch shares no common ancestry "
3954 "with work tree's branch");
3958 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3960 if (error->code != GOT_ERR_ANCESTRY)
3964 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3965 "specified branch resolves to a commit which "
3966 "is already contained in work tree's branch");
3969 error = got_worktree_rebase_prepare(&new_base_branch,
3970 &tmp_branch, &fileindex, worktree, branch, repo);
3975 commit_id = branch_head_commit_id;
3976 error = got_object_open_as_commit(&commit, repo, commit_id);
3980 parent_ids = got_object_commit_get_parent_ids(commit);
3981 pid = SIMPLEQ_FIRST(parent_ids);
3982 error = collect_commits(&commits, commit_id, pid->id,
3983 yca_id, got_worktree_get_path_prefix(worktree),
3984 GOT_ERR_REBASE_PATH, repo);
3985 got_object_commit_close(commit);
3990 if (SIMPLEQ_EMPTY(&commits)) {
3991 if (continue_rebase)
3992 error = rebase_complete(worktree, fileindex,
3993 branch, new_base_branch, tmp_branch, repo);
3995 error = got_error(GOT_ERR_EMPTY_REBASE);
4000 SIMPLEQ_FOREACH(qid, &commits, entry) {
4001 commit_id = qid->id;
4002 parent_id = pid ? pid->id : yca_id;
4005 error = got_worktree_rebase_merge_files(&merged_paths,
4006 worktree, fileindex, parent_id, commit_id, repo,
4007 rebase_progress, &rebase_status, check_cancelled, NULL);
4011 if (rebase_status == GOT_STATUS_CONFLICT) {
4012 got_worktree_rebase_pathlist_free(&merged_paths);
4016 error = rebase_commit(&merged_paths, worktree, fileindex,
4017 tmp_branch, commit_id, repo);
4018 got_worktree_rebase_pathlist_free(&merged_paths);
4023 if (rebase_status == GOT_STATUS_CONFLICT) {
4024 error = got_worktree_rebase_postpone(worktree, fileindex);
4027 error = got_error_msg(GOT_ERR_CONFLICTS,
4028 "conflicts must be resolved before rebasing can continue");
4030 error = rebase_complete(worktree, fileindex, branch,
4031 new_base_branch, tmp_branch, repo);
4033 got_object_id_queue_free(&commits);
4034 free(branch_head_commit_id);
4035 free(resume_commit_id);
4038 got_object_commit_close(commit);
4040 got_ref_close(branch);
4041 if (new_base_branch)
4042 got_ref_close(new_base_branch);
4044 got_ref_close(tmp_branch);
4046 got_worktree_close(worktree);
4048 got_repo_close(repo);
4053 usage_histedit(void)
4055 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4060 #define GOT_HISTEDIT_PICK 'p'
4061 #define GOT_HISTEDIT_EDIT 'e'
4062 #define GOT_HISTEDIT_FOLD 'f'
4063 #define GOT_HISTEDIT_DROP 'd'
4064 #define GOT_HISTEDIT_MESG 'm'
4066 static struct got_histedit_cmd {
4070 } got_histedit_cmds[] = {
4071 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4072 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4073 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4074 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4075 { GOT_HISTEDIT_MESG, "mesg",
4076 "single-line log message for commit above (open editor if empty)" },
4079 struct got_histedit_list_entry {
4080 TAILQ_ENTRY(got_histedit_list_entry) entry;
4081 struct got_object_id *commit_id;
4082 const struct got_histedit_cmd *cmd;
4085 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4087 static const struct got_error *
4088 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4089 FILE *f, struct got_repository *repo)
4091 const struct got_error *err = NULL;
4092 char *logmsg = NULL, *id_str = NULL;
4093 struct got_commit_object *commit = NULL;
4096 err = got_object_open_as_commit(&commit, repo, commit_id);
4100 err = get_short_logmsg(&logmsg, 34, commit);
4104 err = got_object_id_str(&id_str, commit_id);
4108 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4110 err = got_ferror(f, GOT_ERR_IO);
4113 got_object_commit_close(commit);
4119 static const struct got_error *
4120 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4121 struct got_repository *repo)
4123 const struct got_error *err = NULL;
4124 struct got_object_qid *qid;
4126 if (SIMPLEQ_EMPTY(commits))
4127 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4129 SIMPLEQ_FOREACH(qid, commits, entry) {
4130 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4139 static const struct got_error *
4140 write_cmd_list(FILE *f)
4142 const struct got_error *err = NULL;
4145 n = fprintf(f, "# Available histedit commands:\n");
4147 return got_ferror(f, GOT_ERR_IO);
4149 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4150 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4151 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4154 err = got_ferror(f, GOT_ERR_IO);
4158 n = fprintf(f, "# Commits will be processed in order from top to "
4159 "bottom of this file.\n");
4161 return got_ferror(f, GOT_ERR_IO);
4165 static const struct got_error *
4166 histedit_syntax_error(int lineno)
4168 static char msg[42];
4171 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4173 if (ret == -1 || ret >= sizeof(msg))
4174 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4176 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4179 static const struct got_error *
4180 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4181 char *logmsg, struct got_repository *repo)
4183 const struct got_error *err;
4184 struct got_commit_object *folded_commit = NULL;
4187 err = got_object_id_str(&id_str, hle->commit_id);
4191 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4195 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4196 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4197 got_object_commit_get_logmsg(folded_commit)) == -1) {
4198 err = got_error_from_errno("asprintf");
4203 got_object_commit_close(folded_commit);
4208 static struct got_histedit_list_entry *
4209 get_folded_commits(struct got_histedit_list_entry *hle)
4211 struct got_histedit_list_entry *prev, *folded = NULL;
4213 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4214 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4215 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4216 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4218 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4224 static const struct got_error *
4225 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4226 struct got_repository *repo)
4228 char *logmsg_path = NULL, *id_str = NULL;
4229 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4230 const struct got_error *err = NULL;
4231 struct got_commit_object *commit = NULL;
4233 struct got_histedit_list_entry *folded = NULL;
4235 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4239 folded = get_folded_commits(hle);
4241 while (folded != hle) {
4242 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4243 folded = TAILQ_NEXT(folded, entry);
4246 err = append_folded_commit_msg(&new_msg, folded,
4252 folded = TAILQ_NEXT(folded, entry);
4256 err = got_object_id_str(&id_str, hle->commit_id);
4259 if (asprintf(&new_msg,
4260 "%s\n# original log message of commit %s: %s",
4261 logmsg ? logmsg : "", id_str,
4262 got_object_commit_get_logmsg(commit)) == -1) {
4263 err = got_error_from_errno("asprintf");
4269 err = got_object_id_str(&id_str, hle->commit_id);
4273 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4277 dprintf(fd, logmsg);
4280 err = get_editor(&editor);
4284 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4286 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4289 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4290 if (hle->logmsg == NULL)
4291 err = got_error_from_errno("strdup");
4294 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4295 err = got_error_from_errno2("unlink", logmsg_path);
4300 got_object_commit_close(commit);
4304 static const struct got_error *
4305 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4306 FILE *f, struct got_repository *repo)
4308 const struct got_error *err = NULL;
4309 char *line = NULL, *p, *end;
4313 const struct got_histedit_cmd *cmd;
4314 struct got_object_id *commit_id = NULL;
4315 struct got_histedit_list_entry *hle = NULL;
4318 len = getline(&line, &size, f);
4320 const struct got_error *getline_err;
4323 getline_err = got_error_from_errno("getline");
4324 err = got_ferror(f, getline_err->code);
4329 while (isspace((unsigned char)p[0]))
4331 if (p[0] == '#' || p[0] == '\0') {
4337 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4338 cmd = &got_histedit_cmds[i];
4339 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4340 isspace((unsigned char)p[strlen(cmd->name)])) {
4341 p += strlen(cmd->name);
4344 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4349 if (i == nitems(got_histedit_cmds)) {
4350 err = histedit_syntax_error(lineno);
4353 while (isspace((unsigned char)p[0]))
4355 if (cmd->code == GOT_HISTEDIT_MESG) {
4356 if (hle == NULL || hle->logmsg != NULL) {
4357 err = got_error(GOT_ERR_HISTEDIT_CMD);
4361 err = histedit_edit_logmsg(hle, repo);
4365 hle->logmsg = strdup(p);
4366 if (hle->logmsg == NULL) {
4367 err = got_error_from_errno("strdup");
4376 while (end[0] && !isspace((unsigned char)end[0]))
4380 err = got_object_resolve_id_str(&commit_id, repo, p);
4382 /* override error code */
4383 err = histedit_syntax_error(lineno);
4387 hle = malloc(sizeof(*hle));
4389 err = got_error_from_errno("malloc");
4393 hle->commit_id = commit_id;
4398 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4406 static const struct got_error *
4407 histedit_check_script(struct got_histedit_list *histedit_cmds,
4408 struct got_object_id_queue *commits, struct got_repository *repo)
4410 const struct got_error *err = NULL;
4411 struct got_object_qid *qid;
4412 struct got_histedit_list_entry *hle;
4413 static char msg[80];
4416 if (TAILQ_EMPTY(histedit_cmds))
4417 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4418 "histedit script contains no commands");
4420 SIMPLEQ_FOREACH(qid, commits, entry) {
4421 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4422 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4426 err = got_object_id_str(&id_str, qid->id);
4429 snprintf(msg, sizeof(msg),
4430 "commit %s missing from histedit script", id_str);
4432 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4436 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4437 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4438 "last commit in histedit script cannot be folded");
4443 static const struct got_error *
4444 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4445 const char *path, struct got_object_id_queue *commits,
4446 struct got_repository *repo)
4448 const struct got_error *err = NULL;
4452 err = get_editor(&editor);
4456 if (spawn_editor(editor, path) == -1) {
4457 err = got_error_from_errno("failed spawning editor");
4461 f = fopen(path, "r");
4463 err = got_error_from_errno("fopen");
4466 err = histedit_parse_list(histedit_cmds, f, repo);
4470 err = histedit_check_script(histedit_cmds, commits, repo);
4472 if (f && fclose(f) != 0 && err == NULL)
4473 err = got_error_from_errno("fclose");
4478 static const struct got_error *
4479 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4480 struct got_object_id_queue *, const char *, struct got_repository *);
4482 static const struct got_error *
4483 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4484 struct got_object_id_queue *commits, struct got_repository *repo)
4486 const struct got_error *err;
4490 err = got_opentemp_named(&path, &f, "got-histedit");
4494 err = write_cmd_list(f);
4498 err = histedit_write_commit_list(commits, f, repo);
4502 if (fclose(f) != 0) {
4503 err = got_error_from_errno("fclose");
4508 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4510 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4511 err->code != GOT_ERR_HISTEDIT_CMD)
4513 err = histedit_edit_list_retry(histedit_cmds, err,
4514 commits, path, repo);
4517 if (f && fclose(f) != 0 && err == NULL)
4518 err = got_error_from_errno("fclose");
4519 if (path && unlink(path) != 0 && err == NULL)
4520 err = got_error_from_errno2("unlink", path);
4525 static const struct got_error *
4526 histedit_save_list(struct got_histedit_list *histedit_cmds,
4527 struct got_worktree *worktree, struct got_repository *repo)
4529 const struct got_error *err = NULL;
4532 struct got_histedit_list_entry *hle;
4533 struct got_commit_object *commit = NULL;
4535 err = got_worktree_get_histedit_script_path(&path, worktree);
4539 f = fopen(path, "w");
4541 err = got_error_from_errno2("fopen", path);
4544 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4545 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4551 int n = fprintf(f, "%c %s\n",
4552 GOT_HISTEDIT_MESG, hle->logmsg);
4554 err = got_ferror(f, GOT_ERR_IO);
4560 if (f && fclose(f) != 0 && err == NULL)
4561 err = got_error_from_errno("fclose");
4564 got_object_commit_close(commit);
4569 histedit_free_list(struct got_histedit_list *histedit_cmds)
4571 struct got_histedit_list_entry *hle;
4573 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4574 TAILQ_REMOVE(histedit_cmds, hle, entry);
4579 static const struct got_error *
4580 histedit_load_list(struct got_histedit_list *histedit_cmds,
4581 const char *path, struct got_repository *repo)
4583 const struct got_error *err = NULL;
4586 f = fopen(path, "r");
4588 err = got_error_from_errno2("fopen", path);
4592 err = histedit_parse_list(histedit_cmds, f, repo);
4594 if (f && fclose(f) != 0 && err == NULL)
4595 err = got_error_from_errno("fclose");
4599 static const struct got_error *
4600 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4601 const struct got_error *edit_err, struct got_object_id_queue *commits,
4602 const char *path, struct got_repository *repo)
4604 const struct got_error *err = NULL, *prev_err = edit_err;
4607 while (resp != 'c' && resp != 'r' && resp != 'a') {
4608 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4609 "or (a)bort: ", getprogname(), prev_err->msg);
4612 histedit_free_list(histedit_cmds);
4613 err = histedit_run_editor(histedit_cmds, path, commits,
4616 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4617 err->code != GOT_ERR_HISTEDIT_CMD)
4624 } else if (resp == 'r') {
4625 histedit_free_list(histedit_cmds);
4626 err = histedit_edit_script(histedit_cmds,
4629 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4630 err->code != GOT_ERR_HISTEDIT_CMD)
4637 } else if (resp == 'a') {
4638 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4641 printf("invalid response '%c'\n", resp);
4647 static const struct got_error *
4648 histedit_complete(struct got_worktree *worktree,
4649 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4650 struct got_reference *branch, struct got_repository *repo)
4652 printf("Switching work tree to %s\n",
4653 got_ref_get_symref_target(branch));
4654 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4658 static const struct got_error *
4659 show_histedit_progress(struct got_commit_object *commit,
4660 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4662 const struct got_error *err;
4663 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4665 err = got_object_id_str(&old_id_str, hle->commit_id);
4670 err = got_object_id_str(&new_id_str, new_id);
4675 old_id_str[12] = '\0';
4677 new_id_str[12] = '\0';
4680 logmsg = strdup(hle->logmsg);
4681 if (logmsg == NULL) {
4682 err = got_error_from_errno("strdup");
4685 trim_logmsg(logmsg, 42);
4687 err = get_short_logmsg(&logmsg, 42, commit);
4692 switch (hle->cmd->code) {
4693 case GOT_HISTEDIT_PICK:
4694 case GOT_HISTEDIT_EDIT:
4695 printf("%s -> %s: %s\n", old_id_str,
4696 new_id_str ? new_id_str : "no-op change", logmsg);
4698 case GOT_HISTEDIT_DROP:
4699 case GOT_HISTEDIT_FOLD:
4700 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4713 static const struct got_error *
4714 histedit_commit(struct got_pathlist_head *merged_paths,
4715 struct got_worktree *worktree, struct got_fileindex *fileindex,
4716 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4717 struct got_repository *repo)
4719 const struct got_error *err;
4720 struct got_commit_object *commit;
4721 struct got_object_id *new_commit_id;
4723 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4724 && hle->logmsg == NULL) {
4725 err = histedit_edit_logmsg(hle, repo);
4730 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4734 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4735 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4738 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4740 err = show_histedit_progress(commit, hle, NULL);
4742 err = show_histedit_progress(commit, hle, new_commit_id);
4743 free(new_commit_id);
4746 got_object_commit_close(commit);
4750 static const struct got_error *
4751 histedit_skip_commit(struct got_histedit_list_entry *hle,
4752 struct got_worktree *worktree, struct got_repository *repo)
4754 const struct got_error *error;
4755 struct got_commit_object *commit;
4757 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4762 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4766 error = show_histedit_progress(commit, hle, NULL);
4767 got_object_commit_close(commit);
4771 static const struct got_error *
4772 cmd_histedit(int argc, char *argv[])
4774 const struct got_error *error = NULL;
4775 struct got_worktree *worktree = NULL;
4776 struct got_fileindex *fileindex = NULL;
4777 struct got_repository *repo = NULL;
4779 struct got_reference *branch = NULL;
4780 struct got_reference *tmp_branch = NULL;
4781 struct got_object_id *resume_commit_id = NULL;
4782 struct got_object_id *base_commit_id = NULL;
4783 struct got_object_id *head_commit_id = NULL;
4784 struct got_commit_object *commit = NULL;
4785 int ch, rebase_in_progress = 0, did_something;
4786 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4787 const char *edit_script_path = NULL;
4788 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4789 struct got_object_id_queue commits;
4790 struct got_pathlist_head merged_paths;
4791 const struct got_object_id_queue *parent_ids;
4792 struct got_object_qid *pid;
4793 struct got_histedit_list histedit_cmds;
4794 struct got_histedit_list_entry *hle;
4796 SIMPLEQ_INIT(&commits);
4797 TAILQ_INIT(&histedit_cmds);
4798 TAILQ_INIT(&merged_paths);
4800 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4809 edit_script_path = optarg;
4821 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4822 "unveil", NULL) == -1)
4825 if (abort_edit && continue_edit)
4831 * This command cannot apply unveil(2) in all cases because the
4832 * user may choose to run an editor to edit the histedit script
4833 * and to edit individual commit log messages.
4834 * unveil(2) traverses exec(2); if an editor is used we have to
4835 * apply unveil after edit script and log messages have been written.
4836 * XXX TODO: Make use of unveil(2) where possible.
4839 cwd = getcwd(NULL, 0);
4841 error = got_error_from_errno("getcwd");
4844 error = got_worktree_open(&worktree, cwd);
4848 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4852 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4855 if (rebase_in_progress) {
4856 error = got_error(GOT_ERR_REBASING);
4860 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4864 if (edit_in_progress && abort_edit) {
4865 error = got_worktree_histedit_continue(&resume_commit_id,
4866 &tmp_branch, &branch, &base_commit_id, &fileindex,
4870 printf("Switching work tree to %s\n",
4871 got_ref_get_symref_target(branch));
4872 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4873 branch, base_commit_id, update_progress, &did_something);
4876 printf("Histedit of %s aborted\n",
4877 got_ref_get_symref_target(branch));
4878 goto done; /* nothing else to do */
4879 } else if (abort_edit) {
4880 error = got_error(GOT_ERR_NOT_HISTEDIT);
4884 if (continue_edit) {
4887 if (!edit_in_progress) {
4888 error = got_error(GOT_ERR_NOT_HISTEDIT);
4892 error = got_worktree_get_histedit_script_path(&path, worktree);
4896 error = histedit_load_list(&histedit_cmds, path, repo);
4901 error = got_worktree_histedit_continue(&resume_commit_id,
4902 &tmp_branch, &branch, &base_commit_id, &fileindex,
4907 error = got_ref_resolve(&head_commit_id, repo, branch);
4911 error = got_object_open_as_commit(&commit, repo,
4915 parent_ids = got_object_commit_get_parent_ids(commit);
4916 pid = SIMPLEQ_FIRST(parent_ids);
4918 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4921 error = collect_commits(&commits, head_commit_id, pid->id,
4922 base_commit_id, got_worktree_get_path_prefix(worktree),
4923 GOT_ERR_HISTEDIT_PATH, repo);
4924 got_object_commit_close(commit);
4929 if (edit_in_progress) {
4930 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4934 error = got_ref_open(&branch, repo,
4935 got_worktree_get_head_ref_name(worktree), 0);
4939 error = got_ref_resolve(&head_commit_id, repo, branch);
4940 got_ref_close(branch);
4945 error = got_object_open_as_commit(&commit, repo,
4949 parent_ids = got_object_commit_get_parent_ids(commit);
4950 pid = SIMPLEQ_FIRST(parent_ids);
4952 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4955 error = collect_commits(&commits, head_commit_id, pid->id,
4956 got_worktree_get_base_commit_id(worktree),
4957 got_worktree_get_path_prefix(worktree),
4958 GOT_ERR_HISTEDIT_PATH, repo);
4959 got_object_commit_close(commit);
4964 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4965 &base_commit_id, &fileindex, worktree, repo);
4969 if (edit_script_path) {
4970 error = histedit_load_list(&histedit_cmds,
4971 edit_script_path, repo);
4973 got_worktree_histedit_abort(worktree, fileindex,
4974 repo, branch, base_commit_id,
4975 update_progress, &did_something);
4979 error = histedit_edit_script(&histedit_cmds, &commits,
4982 got_worktree_histedit_abort(worktree, fileindex,
4983 repo, branch, base_commit_id,
4984 update_progress, &did_something);
4990 error = histedit_save_list(&histedit_cmds, worktree,
4993 got_worktree_histedit_abort(worktree, fileindex,
4994 repo, branch, base_commit_id,
4995 update_progress, &did_something);
5001 error = histedit_check_script(&histedit_cmds, &commits, repo);
5005 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5006 if (resume_commit_id) {
5007 if (got_object_id_cmp(hle->commit_id,
5008 resume_commit_id) != 0)
5011 resume_commit_id = NULL;
5012 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5013 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5014 error = histedit_skip_commit(hle, worktree,
5017 error = histedit_commit(NULL, worktree,
5018 fileindex, tmp_branch, hle, repo);
5025 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5026 error = histedit_skip_commit(hle, worktree, repo);
5032 error = got_object_open_as_commit(&commit, repo,
5036 parent_ids = got_object_commit_get_parent_ids(commit);
5037 pid = SIMPLEQ_FIRST(parent_ids);
5039 error = got_worktree_histedit_merge_files(&merged_paths,
5040 worktree, fileindex, pid->id, hle->commit_id, repo,
5041 rebase_progress, &rebase_status, check_cancelled, NULL);
5044 got_object_commit_close(commit);
5047 if (rebase_status == GOT_STATUS_CONFLICT) {
5048 got_worktree_rebase_pathlist_free(&merged_paths);
5052 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5054 error = got_object_id_str(&id_str, hle->commit_id);
5057 printf("Stopping histedit for amending commit %s\n",
5060 got_worktree_rebase_pathlist_free(&merged_paths);
5061 error = got_worktree_histedit_postpone(worktree,
5066 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5067 error = histedit_skip_commit(hle, worktree, repo);
5073 error = histedit_commit(&merged_paths, worktree, fileindex,
5074 tmp_branch, hle, repo);
5075 got_worktree_rebase_pathlist_free(&merged_paths);
5080 if (rebase_status == GOT_STATUS_CONFLICT) {
5081 error = got_worktree_histedit_postpone(worktree, fileindex);
5084 error = got_error_msg(GOT_ERR_CONFLICTS,
5085 "conflicts must be resolved before rebasing can continue");
5087 error = histedit_complete(worktree, fileindex, tmp_branch,
5090 got_object_id_queue_free(&commits);
5091 histedit_free_list(&histedit_cmds);
5092 free(head_commit_id);
5093 free(base_commit_id);
5094 free(resume_commit_id);
5096 got_object_commit_close(commit);
5098 got_ref_close(branch);
5100 got_ref_close(tmp_branch);
5102 got_worktree_close(worktree);
5104 got_repo_close(repo);