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);
97 static const struct got_error* cmd_init(int, char *[]);
98 static const struct got_error* cmd_import(int, char *[]);
99 static const struct got_error* cmd_checkout(int, char *[]);
100 static const struct got_error* cmd_update(int, char *[]);
101 static const struct got_error* cmd_log(int, char *[]);
102 static const struct got_error* cmd_diff(int, char *[]);
103 static const struct got_error* cmd_blame(int, char *[]);
104 static const struct got_error* cmd_tree(int, char *[]);
105 static const struct got_error* cmd_status(int, char *[]);
106 static const struct got_error* cmd_ref(int, char *[]);
107 static const struct got_error* cmd_branch(int, char *[]);
108 static const struct got_error* cmd_add(int, char *[]);
109 static const struct got_error* cmd_remove(int, char *[]);
110 static const struct got_error* cmd_revert(int, char *[]);
111 static const struct got_error* cmd_commit(int, char *[]);
112 static const struct got_error* cmd_cherrypick(int, char *[]);
113 static const struct got_error* cmd_backout(int, char *[]);
114 static const struct got_error* cmd_rebase(int, char *[]);
116 static struct got_cmd got_commands[] = {
117 { "init", cmd_init, usage_init, "" },
118 { "import", cmd_import, usage_import, "" },
119 { "checkout", cmd_checkout, usage_checkout, "co" },
120 { "update", cmd_update, usage_update, "up" },
121 { "log", cmd_log, usage_log, "" },
122 { "diff", cmd_diff, usage_diff, "" },
123 { "blame", cmd_blame, usage_blame, "" },
124 { "tree", cmd_tree, usage_tree, "" },
125 { "status", cmd_status, usage_status, "st" },
126 { "ref", cmd_ref, usage_ref, "" },
127 { "branch", cmd_branch, usage_branch, "br" },
128 { "add", cmd_add, usage_add, "" },
129 { "remove", cmd_remove, usage_remove, "rm" },
130 { "revert", cmd_revert, usage_revert, "rv" },
131 { "commit", cmd_commit, usage_commit, "ci" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 { "backout", cmd_backout, usage_backout, "bo" },
134 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 fprintf(stderr, "commands:");
143 for (i = 0; i < nitems(got_commands); i++) {
144 struct got_cmd *cmd = &got_commands[i];
145 fprintf(stderr, " %s", cmd->cmd_name);
151 main(int argc, char *argv[])
158 setlocale(LC_CTYPE, "");
160 while ((ch = getopt(argc, argv, "h")) != -1) {
178 signal(SIGINT, catch_sigint);
179 signal(SIGPIPE, catch_sigpipe);
181 for (i = 0; i < nitems(got_commands); i++) {
182 const struct got_error *error;
184 cmd = &got_commands[i];
186 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 strcmp(cmd->cmd_alias, argv[0]) != 0)
191 got_commands[i].cmd_usage();
193 error = got_commands[i].cmd_main(argc, argv);
194 if (error && !(sigint_received || sigpipe_received)) {
195 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
202 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
210 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
216 static const struct got_error *
217 get_editor(char **abspath)
219 const struct got_error *err = NULL;
222 editor = getenv("VISUAL");
224 editor = getenv("EDITOR");
227 err = got_path_find_prog(abspath, editor);
232 if (*abspath == NULL) {
233 *abspath = strdup("/bin/ed");
234 if (*abspath == NULL)
235 return got_error_from_errno("strdup");
241 static const struct got_error *
242 apply_unveil(const char *repo_path, int repo_read_only,
243 const char *worktree_path, int create_worktree)
245 const struct got_error *err;
248 if (unveil("gmon.out", "rwc") != 0)
249 return got_error_from_errno2("unveil", "gmon.out");
251 if (create_worktree) {
252 /* Pre-create work tree path to avoid unveiling its parents. */
253 err = got_path_mkdir(worktree_path);
255 if (errno == EEXIST) {
256 if (got_path_dir_is_empty(worktree_path)) {
260 err = got_error_path(worktree_path,
261 GOT_ERR_DIR_NOT_EMPTY);
265 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
269 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
270 return got_error_from_errno2("unveil", repo_path);
272 if (worktree_path && unveil(worktree_path, "rwc") != 0)
273 return got_error_from_errno2("unveil", worktree_path);
275 if (unveil("/tmp", "rwc") != 0)
276 return got_error_from_errno2("unveil", "/tmp");
278 err = got_privsep_unveil_exec_helpers();
282 if (unveil(NULL, NULL) != 0)
283 return got_error_from_errno("unveil");
291 fprintf(stderr, "usage: %s init path\n", getprogname());
295 static const struct got_error *
296 cmd_init(int argc, char *argv[])
298 const struct got_error *error = NULL;
299 char *repo_path = NULL;
302 while ((ch = getopt(argc, argv, "")) != -1) {
314 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
320 repo_path = strdup(argv[0]);
321 if (repo_path == NULL)
322 return got_error_from_errno("strdup");
324 got_path_strip_trailing_slashes(repo_path);
326 error = got_path_mkdir(repo_path);
328 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
331 error = apply_unveil(repo_path, 0, NULL, 0);
335 error = got_repo_init(repo_path);
347 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
348 "[-r repository-path] [-I pattern] path\n", getprogname());
353 spawn_editor(const char *editor, const char *file)
356 sig_t sighup, sigint, sigquit;
359 sighup = signal(SIGHUP, SIG_IGN);
360 sigint = signal(SIGINT, SIG_IGN);
361 sigquit = signal(SIGQUIT, SIG_IGN);
363 switch (pid = fork()) {
367 execl(editor, editor, file, (char *)NULL);
371 while (waitpid(pid, &st, 0) == -1)
376 (void)signal(SIGHUP, sighup);
377 (void)signal(SIGINT, sigint);
378 (void)signal(SIGQUIT, sigquit);
380 if (!WIFEXITED(st)) {
385 return WEXITSTATUS(st);
388 static const struct got_error *
389 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
390 const char *initial_content)
392 const struct got_error *err = NULL;
396 int content_changed = 0;
401 if (stat(logmsg_path, &st) == -1)
402 return got_error_from_errno2("stat", logmsg_path);
404 if (spawn_editor(editor, logmsg_path) == -1)
405 return got_error_from_errno("failed spawning editor");
407 if (stat(logmsg_path, &st2) == -1)
408 return got_error_from_errno("stat");
410 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
411 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
412 "no changes made to commit message, aborting");
414 *logmsg = malloc(st2.st_size + 1);
416 return got_error_from_errno("malloc");
420 fp = fopen(logmsg_path, "r");
422 err = got_error_from_errno("fopen");
425 while (fgets(buf, sizeof(buf), fp) != NULL) {
426 if (!content_changed && strcmp(buf, initial_content) != 0)
428 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
429 continue; /* remove comments and leading empty lines */
430 len = strlcat(*logmsg, buf, st2.st_size);
434 while (len > 0 && (*logmsg)[len - 1] == '\n') {
435 (*logmsg)[len - 1] = '\0';
439 if (len == 0 || !content_changed)
440 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 "commit message cannot be empty, aborting");
450 static const struct got_error *
451 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
452 const char *branch_name)
454 char *initial_content = NULL, *logmsg_path = NULL;
455 const struct got_error *err = NULL;
458 if (asprintf(&initial_content,
459 "\n# %s to be imported to branch %s\n", path_dir,
461 return got_error_from_errno("asprintf");
463 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
467 dprintf(fd, initial_content);
470 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
472 free(initial_content);
477 static const struct got_error *
478 import_progress(void *arg, const char *path)
480 printf("A %s\n", path);
484 static const struct got_error *
485 cmd_import(int argc, char *argv[])
487 const struct got_error *error = NULL;
488 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
490 const char *got_author = getenv("GOT_AUTHOR");
491 const char *branch_name = "master";
492 char *refname = NULL, *id_str = NULL;
493 struct got_repository *repo = NULL;
494 struct got_reference *branch_ref = NULL, *head_ref = NULL;
495 struct got_object_id *new_commit_id = NULL;
497 struct got_pathlist_head ignores;
498 struct got_pathlist_entry *pe;
500 TAILQ_INIT(&ignores);
502 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
505 branch_name = optarg;
508 logmsg = strdup(optarg);
509 if (logmsg == NULL) {
510 error = got_error_from_errno("strdup");
515 repo_path = realpath(optarg, NULL);
516 if (repo_path == NULL) {
517 error = got_error_from_errno("realpath");
522 if (optarg[0] == '\0')
524 error = got_pathlist_insert(&pe, &ignores, optarg,
539 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
546 if (got_author == NULL) {
547 /* TODO: Look current user up in password database */
548 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
552 if (repo_path == NULL) {
553 repo_path = getcwd(NULL, 0);
554 if (repo_path == NULL)
555 return got_error_from_errno("getcwd");
557 got_path_strip_trailing_slashes(repo_path);
558 error = got_repo_open(&repo, repo_path);
562 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
563 error = got_error_from_errno("asprintf");
567 error = got_ref_open(&branch_ref, repo, refname, 0);
569 if (error->code != GOT_ERR_NOT_REF)
572 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
573 "import target branch already exists");
577 path_dir = realpath(argv[0], NULL);
578 if (path_dir == NULL) {
579 error = got_error_from_errno("realpath");
582 got_path_strip_trailing_slashes(path_dir);
585 * unveil(2) traverses exec(2); if an editor is used we have
586 * to apply unveil after the log message has been written.
588 if (logmsg == NULL || strlen(logmsg) == 0) {
589 error = get_editor(&editor);
592 error = collect_import_msg(&logmsg, editor, path_dir, refname);
597 if (unveil(path_dir, "r") != 0)
598 return got_error_from_errno2("unveil", path_dir);
600 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
604 error = got_repo_import(&new_commit_id, path_dir, logmsg,
605 got_author, &ignores, repo, import_progress, NULL);
609 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
613 error = got_ref_write(branch_ref, repo);
617 error = got_object_id_str(&id_str, new_commit_id);
621 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
623 if (error->code != GOT_ERR_NOT_REF)
626 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
631 error = got_ref_write(head_ref, repo);
636 printf("Created branch %s with commit %s\n",
637 got_ref_get_name(branch_ref), id_str);
645 got_ref_close(branch_ref);
647 got_ref_close(head_ref);
654 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
655 "[-p prefix] repository-path [worktree-path]\n", getprogname());
659 static const struct got_error *
660 checkout_progress(void *arg, unsigned char status, const char *path)
662 char *worktree_path = arg;
664 /* Base commit bump happens silently. */
665 if (status == GOT_STATUS_BUMP_BASE)
668 while (path[0] == '/')
671 printf("%c %s/%s\n", status, worktree_path, path);
675 static const struct got_error *
676 check_cancelled(void *arg)
678 if (sigint_received || sigpipe_received)
679 return got_error(GOT_ERR_CANCELLED);
683 static const struct got_error *
684 check_linear_ancestry(struct got_object_id *commit_id,
685 struct got_object_id *base_commit_id, struct got_repository *repo)
687 const struct got_error *err = NULL;
688 struct got_object_id *yca_id;
690 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
691 commit_id, base_commit_id, repo);
696 return got_error(GOT_ERR_ANCESTRY);
699 * Require a straight line of history between the target commit
700 * and the work tree's base commit.
702 * Non-linear situations such as this require a rebase:
704 * (commit) D F (base_commit)
712 * 'got update' only handles linear cases:
713 * Update forwards in time: A (base/yca) - B - C - D (commit)
714 * Update backwards in time: D (base) - C - B - A (commit/yca)
716 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
717 got_object_id_cmp(base_commit_id, yca_id) != 0)
718 return got_error(GOT_ERR_ANCESTRY);
724 static const struct got_error *
725 check_same_branch(struct got_object_id *commit_id,
726 struct got_reference *head_ref, struct got_repository *repo)
728 const struct got_error *err = NULL;
729 struct got_commit_graph *graph = NULL;
730 struct got_object_id *head_commit_id = NULL;
731 int is_same_branch = 0;
733 err = got_ref_resolve(&head_commit_id, repo, head_ref);
737 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
741 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
746 struct got_object_id *id;
747 err = got_commit_graph_iter_next(&id, graph);
749 if (err->code == GOT_ERR_ITER_COMPLETED) {
752 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
754 err = got_commit_graph_fetch_commits(graph, 1,
761 if (got_object_id_cmp(id, commit_id) == 0) {
769 got_commit_graph_close(graph);
770 free(head_commit_id);
771 if (!err && !is_same_branch)
772 err = got_error(GOT_ERR_ANCESTRY);
776 static const struct got_error *
777 cmd_checkout(int argc, char *argv[])
779 const struct got_error *error = NULL;
780 struct got_repository *repo = NULL;
781 struct got_reference *head_ref = NULL;
782 struct got_worktree *worktree = NULL;
783 char *repo_path = NULL;
784 char *worktree_path = NULL;
785 const char *path_prefix = "";
786 const char *branch_name = GOT_REF_HEAD;
787 char *commit_id_str = NULL;
788 int ch, same_path_prefix;
790 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
793 branch_name = optarg;
796 commit_id_str = strdup(optarg);
797 if (commit_id_str == NULL)
798 return got_error_from_errno("strdup");
801 path_prefix = optarg;
813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
814 "unveil", NULL) == -1)
818 char *cwd, *base, *dotgit;
819 repo_path = realpath(argv[0], NULL);
820 if (repo_path == NULL)
821 return got_error_from_errno2("realpath", argv[0]);
822 cwd = getcwd(NULL, 0);
824 error = got_error_from_errno("getcwd");
827 if (path_prefix[0]) {
828 base = basename(path_prefix);
830 error = got_error_from_errno2("basename",
835 base = basename(repo_path);
837 error = got_error_from_errno2("basename",
842 dotgit = strstr(base, ".git");
845 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
846 error = got_error_from_errno("asprintf");
851 } else if (argc == 2) {
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL) {
854 error = got_error_from_errno2("realpath", argv[0]);
857 worktree_path = realpath(argv[1], NULL);
858 if (worktree_path == NULL) {
859 if (errno != ENOENT) {
860 error = got_error_from_errno2("realpath",
864 worktree_path = strdup(argv[1]);
865 if (worktree_path == NULL) {
866 error = got_error_from_errno("strdup");
873 got_path_strip_trailing_slashes(repo_path);
874 got_path_strip_trailing_slashes(worktree_path);
876 error = got_repo_open(&repo, repo_path);
880 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
884 error = got_ref_open(&head_ref, repo, branch_name, 0);
888 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
889 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
892 error = got_worktree_open(&worktree, worktree_path);
896 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
900 if (!same_path_prefix) {
901 error = got_error(GOT_ERR_PATH_PREFIX);
906 struct got_object_id *commit_id;
907 error = got_repo_match_object_id_prefix(&commit_id,
908 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
911 error = check_linear_ancestry(commit_id,
912 got_worktree_get_base_commit_id(worktree), repo);
917 error = check_same_branch(commit_id, head_ref, repo);
920 error = got_worktree_set_base_commit_id(worktree, repo,
927 error = got_worktree_checkout_files(worktree, "", repo,
928 checkout_progress, worktree_path, check_cancelled, NULL);
932 printf("Now shut up and hack\n");
944 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
949 static const struct got_error *
950 update_progress(void *arg, unsigned char status, const char *path)
952 int *did_something = arg;
954 if (status == GOT_STATUS_EXISTS)
959 /* Base commit bump happens silently. */
960 if (status == GOT_STATUS_BUMP_BASE)
963 while (path[0] == '/')
965 printf("%c %s\n", status, path);
969 static const struct got_error *
970 switch_head_ref(struct got_reference *head_ref,
971 struct got_object_id *commit_id, struct got_worktree *worktree,
972 struct got_repository *repo)
974 const struct got_error *err = NULL;
976 int ref_has_moved = 0;
978 /* Trivial case: switching between two different references. */
979 if (strcmp(got_ref_get_name(head_ref),
980 got_worktree_get_head_ref_name(worktree)) != 0) {
981 printf("Switching work tree from %s to %s\n",
982 got_worktree_get_head_ref_name(worktree),
983 got_ref_get_name(head_ref));
984 return got_worktree_set_head_ref(worktree, head_ref);
987 err = check_linear_ancestry(commit_id,
988 got_worktree_get_base_commit_id(worktree), repo);
990 if (err->code != GOT_ERR_ANCESTRY)
997 /* Switching to a rebased branch with the same reference name. */
998 err = got_object_id_str(&base_id_str,
999 got_worktree_get_base_commit_id(worktree));
1002 printf("Reference %s now points at a different branch\n",
1003 got_worktree_get_head_ref_name(worktree));
1004 printf("Switching work tree from %s to %s\n", base_id_str,
1005 got_worktree_get_head_ref_name(worktree));
1009 static const struct got_error *
1010 cmd_update(int argc, char *argv[])
1012 const struct got_error *error = NULL;
1013 struct got_repository *repo = NULL;
1014 struct got_worktree *worktree = NULL;
1015 char *worktree_path = NULL, *path = NULL;
1016 struct got_object_id *commit_id = NULL;
1017 char *commit_id_str = NULL;
1018 const char *branch_name = NULL;
1019 struct got_reference *head_ref = NULL;
1020 int ch, did_something = 0, rebase_in_progress;
1022 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1025 branch_name = optarg;
1028 commit_id_str = strdup(optarg);
1029 if (commit_id_str == NULL)
1030 return got_error_from_errno("strdup");
1042 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1043 "unveil", NULL) == -1)
1046 worktree_path = getcwd(NULL, 0);
1047 if (worktree_path == NULL) {
1048 error = got_error_from_errno("getcwd");
1051 error = got_worktree_open(&worktree, worktree_path);
1055 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1058 if (rebase_in_progress) {
1059 error = got_error(GOT_ERR_REBASING);
1066 error = got_error_from_errno("strdup");
1069 } else if (argc == 1) {
1070 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1076 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1080 error = apply_unveil(got_repo_get_path(repo), 0,
1081 got_worktree_get_root_path(worktree), 0);
1085 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1086 got_worktree_get_head_ref_name(worktree), 0);
1089 if (commit_id_str == NULL) {
1090 error = got_ref_resolve(&commit_id, repo, head_ref);
1093 error = got_object_id_str(&commit_id_str, commit_id);
1097 error = got_repo_match_object_id_prefix(&commit_id,
1098 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1101 free(commit_id_str);
1102 error = got_object_id_str(&commit_id_str, commit_id);
1108 struct got_object_id *head_commit_id;
1109 if (strlen(path) != 0) {
1110 fprintf(stderr, "%s: switching between branches "
1111 "requires that the entire work tree "
1112 "gets updated, not just '%s'\n",
1113 getprogname(), path);
1114 error = got_error(GOT_ERR_BAD_PATH);
1117 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1120 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1121 free(head_commit_id);
1124 error = check_same_branch(commit_id, head_ref, repo);
1127 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1131 error = check_linear_ancestry(commit_id,
1132 got_worktree_get_base_commit_id(worktree), repo);
1133 if (error != NULL) {
1134 if (error->code == GOT_ERR_ANCESTRY)
1135 error = got_error(GOT_ERR_BRANCH_MOVED);
1138 error = check_same_branch(commit_id, head_ref, repo);
1143 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1145 error = got_worktree_set_base_commit_id(worktree, repo,
1151 error = got_worktree_checkout_files(worktree, path, repo,
1152 update_progress, &did_something, check_cancelled, NULL);
1157 printf("Updated to commit %s\n", commit_id_str);
1159 printf("Already up-to-date\n");
1161 free(worktree_path);
1164 free(commit_id_str);
1168 static const struct got_error *
1169 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1170 int diff_context, struct got_repository *repo)
1172 const struct got_error *err = NULL;
1173 struct got_tree_object *tree1 = NULL, *tree2;
1174 struct got_object_qid *qid;
1175 char *id_str1 = NULL, *id_str2;
1176 struct got_diff_blob_output_unidiff_arg arg;
1178 err = got_object_open_as_tree(&tree2, repo,
1179 got_object_commit_get_tree_id(commit));
1183 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1185 struct got_commit_object *pcommit;
1187 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1191 err = got_object_open_as_tree(&tree1, repo,
1192 got_object_commit_get_tree_id(pcommit));
1193 got_object_commit_close(pcommit);
1197 err = got_object_id_str(&id_str1, qid->id);
1202 err = got_object_id_str(&id_str2, id);
1206 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1207 arg.diff_context = diff_context;
1208 arg.outfile = stdout;
1209 err = got_diff_tree(tree1, tree2, "", "", repo,
1210 got_diff_blob_output_unidiff, &arg);
1213 got_object_tree_close(tree1);
1214 got_object_tree_close(tree2);
1221 get_datestr(time_t *time, char *datebuf)
1223 char *p, *s = ctime_r(time, datebuf);
1224 p = strchr(s, '\n');
1230 static const struct got_error *
1231 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1232 struct got_repository *repo, int show_patch, int diff_context,
1233 struct got_reflist_head *refs)
1235 const struct got_error *err = NULL;
1236 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1238 time_t committer_time;
1239 const char *author, *committer;
1240 char *refs_str = NULL;
1241 struct got_reflist_entry *re;
1243 SIMPLEQ_FOREACH(re, refs, entry) {
1246 if (got_object_id_cmp(re->id, id) != 0)
1248 name = got_ref_get_name(re->ref);
1249 if (strcmp(name, GOT_REF_HEAD) == 0)
1251 if (strncmp(name, "refs/", 5) == 0)
1253 if (strncmp(name, "got/", 4) == 0)
1255 if (strncmp(name, "heads/", 6) == 0)
1257 if (strncmp(name, "remotes/", 8) == 0)
1260 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1262 err = got_error_from_errno("asprintf");
1268 err = got_object_id_str(&id_str, id);
1272 printf("-----------------------------------------------\n");
1273 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1274 refs_str ? refs_str : "", refs_str ? ")" : "");
1279 printf("from: %s\n", got_object_commit_get_author(commit));
1280 committer_time = got_object_commit_get_committer_time(commit);
1281 datestr = get_datestr(&committer_time, datebuf);
1282 printf("date: %s UTC\n", datestr);
1283 author = got_object_commit_get_author(commit);
1284 committer = got_object_commit_get_committer(commit);
1285 if (strcmp(author, committer) != 0)
1286 printf("via: %s\n", committer);
1287 if (got_object_commit_get_nparents(commit) > 1) {
1288 const struct got_object_id_queue *parent_ids;
1289 struct got_object_qid *qid;
1291 parent_ids = got_object_commit_get_parent_ids(commit);
1292 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1293 err = got_object_id_str(&id_str, qid->id);
1296 printf("parent %d: %s\n", n++, id_str);
1301 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1302 if (logmsg0 == NULL)
1303 return got_error_from_errno("strdup");
1307 line = strsep(&logmsg, "\n");
1309 printf(" %s\n", line);
1314 err = print_patch(commit, id, diff_context, repo);
1319 if (fflush(stdout) != 0 && err == NULL)
1320 err = got_error_from_errno("fflush");
1324 static const struct got_error *
1325 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1326 char *path, int show_patch, int diff_context, int limit,
1327 int first_parent_traversal, struct got_reflist_head *refs)
1329 const struct got_error *err;
1330 struct got_commit_graph *graph;
1332 err = got_commit_graph_open(&graph, root_id, path,
1333 first_parent_traversal, repo);
1336 err = got_commit_graph_iter_start(graph, root_id, repo);
1340 struct got_commit_object *commit;
1341 struct got_object_id *id;
1343 if (sigint_received || sigpipe_received)
1346 err = got_commit_graph_iter_next(&id, graph);
1348 if (err->code == GOT_ERR_ITER_COMPLETED) {
1352 if (err->code != GOT_ERR_ITER_NEED_MORE)
1354 err = got_commit_graph_fetch_commits(graph, 1, repo);
1363 err = got_object_open_as_commit(&commit, repo, id);
1366 err = print_commit(commit, id, repo, show_patch, diff_context,
1368 got_object_commit_close(commit);
1369 if (err || (limit && --limit == 0))
1373 got_commit_graph_close(graph);
1380 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1381 "[-r repository-path] [path]\n", getprogname());
1385 static const struct got_error *
1386 cmd_log(int argc, char *argv[])
1388 const struct got_error *error;
1389 struct got_repository *repo = NULL;
1390 struct got_worktree *worktree = NULL;
1391 struct got_commit_object *commit = NULL;
1392 struct got_object_id *id = NULL;
1393 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1394 char *start_commit = NULL;
1395 int diff_context = 3, ch;
1396 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1398 struct got_reflist_head refs;
1400 SIMPLEQ_INIT(&refs);
1403 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1409 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1415 start_commit = optarg;
1418 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1421 err(1, "-C option %s", errstr);
1424 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1426 err(1, "-l option %s", errstr);
1429 first_parent_traversal = 1;
1432 repo_path = realpath(optarg, NULL);
1433 if (repo_path == NULL)
1434 err(1, "-r option");
1435 got_path_strip_trailing_slashes(repo_path);
1446 cwd = getcwd(NULL, 0);
1448 error = got_error_from_errno("getcwd");
1452 error = got_worktree_open(&worktree, cwd);
1453 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1460 error = got_error_from_errno("strdup");
1463 } else if (argc == 1) {
1465 error = got_worktree_resolve_path(&path, worktree,
1470 path = strdup(argv[0]);
1472 error = got_error_from_errno("strdup");
1479 if (repo_path == NULL) {
1480 repo_path = worktree ?
1481 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1483 if (repo_path == NULL) {
1484 error = got_error_from_errno("strdup");
1488 error = got_repo_open(&repo, repo_path);
1492 error = apply_unveil(got_repo_get_path(repo), 1,
1493 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1497 if (start_commit == NULL) {
1498 struct got_reference *head_ref;
1499 error = got_ref_open(&head_ref, repo,
1500 worktree ? got_worktree_get_head_ref_name(worktree)
1504 error = got_ref_resolve(&id, repo, head_ref);
1505 got_ref_close(head_ref);
1508 error = got_object_open_as_commit(&commit, repo, id);
1510 struct got_reference *ref;
1511 error = got_ref_open(&ref, repo, start_commit, 0);
1512 if (error == NULL) {
1514 error = got_ref_resolve(&id, repo, ref);
1518 error = got_object_get_type(&obj_type, repo, id);
1521 if (obj_type == GOT_OBJ_TYPE_TAG) {
1522 struct got_tag_object *tag;
1523 error = got_object_open_as_tag(&tag, repo, id);
1526 if (got_object_tag_get_object_type(tag) !=
1527 GOT_OBJ_TYPE_COMMIT) {
1528 got_object_tag_close(tag);
1529 error = got_error(GOT_ERR_OBJ_TYPE);
1533 id = got_object_id_dup(
1534 got_object_tag_get_object_id(tag));
1536 error = got_error_from_errno(
1537 "got_object_id_dup");
1538 got_object_tag_close(tag);
1541 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1542 error = got_error(GOT_ERR_OBJ_TYPE);
1545 error = got_object_open_as_commit(&commit, repo, id);
1549 if (commit == NULL) {
1550 error = got_repo_match_object_id_prefix(&id,
1551 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1559 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1564 path = in_repo_path;
1567 error = got_ref_list(&refs, repo);
1571 error = print_commits(id, repo, path, show_patch,
1572 diff_context, limit, first_parent_traversal, &refs);
1579 got_worktree_close(worktree);
1581 const struct got_error *repo_error;
1582 repo_error = got_repo_close(repo);
1586 got_ref_list_free(&refs);
1593 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1594 "[object1 object2 | path]\n", getprogname());
1598 struct print_diff_arg {
1599 struct got_repository *repo;
1600 struct got_worktree *worktree;
1606 static const struct got_error *
1607 print_diff(void *arg, unsigned char status, const char *path,
1608 struct got_object_id *blob_id, struct got_object_id *commit_id)
1610 struct print_diff_arg *a = arg;
1611 const struct got_error *err = NULL;
1612 struct got_blob_object *blob1 = NULL;
1614 char *abspath = NULL;
1617 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1618 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1621 if (!a->header_shown) {
1622 printf("diff %s %s\n", a->id_str,
1623 got_worktree_get_root_path(a->worktree));
1624 a->header_shown = 1;
1627 if (status != GOT_STATUS_ADD) {
1628 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1634 if (status != GOT_STATUS_DELETE) {
1635 if (asprintf(&abspath, "%s/%s",
1636 got_worktree_get_root_path(a->worktree), path) == -1) {
1637 err = got_error_from_errno("asprintf");
1641 f2 = fopen(abspath, "r");
1643 err = got_error_from_errno2("fopen", abspath);
1646 if (lstat(abspath, &sb) == -1) {
1647 err = got_error_from_errno2("lstat", abspath);
1653 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1657 got_object_blob_close(blob1);
1658 if (f2 && fclose(f2) != 0 && err == NULL)
1659 err = got_error_from_errno("fclose");
1664 static const struct got_error *
1665 cmd_diff(int argc, char *argv[])
1667 const struct got_error *error;
1668 struct got_repository *repo = NULL;
1669 struct got_worktree *worktree = NULL;
1670 char *cwd = NULL, *repo_path = NULL;
1671 struct got_object_id *id1 = NULL, *id2 = NULL;
1672 const char *id_str1 = NULL, *id_str2 = NULL;
1673 char *label1 = NULL, *label2 = NULL;
1675 int diff_context = 3, ch;
1680 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1685 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1688 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1690 err(1, "-C option %s", errstr);
1693 repo_path = realpath(optarg, NULL);
1694 if (repo_path == NULL)
1695 err(1, "-r option");
1696 got_path_strip_trailing_slashes(repo_path);
1707 cwd = getcwd(NULL, 0);
1709 error = got_error_from_errno("getcwd");
1712 error = got_worktree_open(&worktree, cwd);
1713 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1716 if (worktree == NULL) {
1717 error = got_error(GOT_ERR_NOT_WORKTREE);
1722 "-r option can't be used when diffing a work tree");
1723 repo_path = strdup(got_worktree_get_repo_path(worktree));
1724 if (repo_path == NULL) {
1725 error = got_error_from_errno("strdup");
1729 error = got_worktree_resolve_path(&path, worktree,
1736 error = got_error_from_errno("strdup");
1740 } else if (argc == 2) {
1743 if (worktree && repo_path == NULL) {
1745 strdup(got_worktree_get_repo_path(worktree));
1746 if (repo_path == NULL) {
1747 error = got_error_from_errno("strdup");
1754 if (repo_path == NULL) {
1755 repo_path = getcwd(NULL, 0);
1756 if (repo_path == NULL)
1757 return got_error_from_errno("getcwd");
1760 error = got_repo_open(&repo, repo_path);
1765 error = apply_unveil(got_repo_get_path(repo), 1,
1766 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1771 struct print_diff_arg arg;
1773 error = got_object_id_str(&id_str,
1774 got_worktree_get_base_commit_id(worktree));
1778 arg.worktree = worktree;
1779 arg.diff_context = diff_context;
1780 arg.id_str = id_str;
1781 arg.header_shown = 0;
1783 error = got_worktree_status(worktree, path, repo, print_diff,
1784 &arg, check_cancelled, NULL);
1789 error = got_repo_match_object_id_prefix(&id1, id_str1,
1790 GOT_OBJ_TYPE_ANY, repo);
1792 struct got_reference *ref;
1793 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1795 error = got_ref_open(&ref, repo, id_str1, 0);
1798 label1 = strdup(got_ref_get_name(ref));
1799 if (label1 == NULL) {
1800 error = got_error_from_errno("strdup");
1803 error = got_ref_resolve(&id1, repo, ref);
1808 error = got_object_id_str(&label1, id1);
1809 if (label1 == NULL) {
1810 error = got_error_from_errno("strdup");
1815 error = got_repo_match_object_id_prefix(&id2, id_str2,
1816 GOT_OBJ_TYPE_ANY, repo);
1818 struct got_reference *ref;
1819 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1821 error = got_ref_open(&ref, repo, id_str2, 0);
1824 label2 = strdup(got_ref_get_name(ref));
1825 if (label2 == NULL) {
1826 error = got_error_from_errno("strdup");
1829 error = got_ref_resolve(&id2, repo, ref);
1834 error = got_object_id_str(&label2, id2);
1835 if (label2 == NULL) {
1836 error = got_error_from_errno("strdup");
1841 error = got_object_get_type(&type1, repo, id1);
1845 error = got_object_get_type(&type2, repo, id2);
1849 if (type1 != type2) {
1850 error = got_error(GOT_ERR_OBJ_TYPE);
1855 case GOT_OBJ_TYPE_BLOB:
1856 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1857 diff_context, repo, stdout);
1859 case GOT_OBJ_TYPE_TREE:
1860 error = got_diff_objects_as_trees(id1, id2, "", "",
1861 diff_context, repo, stdout);
1863 case GOT_OBJ_TYPE_COMMIT:
1864 printf("diff %s %s\n", label1, label2);
1865 error = got_diff_objects_as_commits(id1, id2, diff_context,
1869 error = got_error(GOT_ERR_OBJ_TYPE);
1879 got_worktree_close(worktree);
1881 const struct got_error *repo_error;
1882 repo_error = got_repo_close(repo);
1893 "usage: %s blame [-c commit] [-r repository-path] path\n",
1898 static const struct got_error *
1899 cmd_blame(int argc, char *argv[])
1901 const struct got_error *error;
1902 struct got_repository *repo = NULL;
1903 struct got_worktree *worktree = NULL;
1904 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1905 struct got_object_id *commit_id = NULL;
1906 char *commit_id_str = NULL;
1910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1915 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1918 commit_id_str = optarg;
1921 repo_path = realpath(optarg, NULL);
1922 if (repo_path == NULL)
1923 err(1, "-r option");
1924 got_path_strip_trailing_slashes(repo_path);
1940 cwd = getcwd(NULL, 0);
1942 error = got_error_from_errno("getcwd");
1945 if (repo_path == NULL) {
1946 error = got_worktree_open(&worktree, cwd);
1947 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1953 strdup(got_worktree_get_repo_path(worktree));
1954 if (repo_path == NULL)
1955 error = got_error_from_errno("strdup");
1959 repo_path = strdup(cwd);
1960 if (repo_path == NULL) {
1961 error = got_error_from_errno("strdup");
1967 error = got_repo_open(&repo, repo_path);
1971 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1976 const char *prefix = got_worktree_get_path_prefix(worktree);
1977 char *p, *worktree_subdir = cwd +
1978 strlen(got_worktree_get_root_path(worktree));
1979 if (asprintf(&p, "%s%s%s%s%s",
1980 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1981 worktree_subdir, worktree_subdir[0] ? "/" : "",
1983 error = got_error_from_errno("asprintf");
1986 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1989 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1994 if (commit_id_str == NULL) {
1995 struct got_reference *head_ref;
1996 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1999 error = got_ref_resolve(&commit_id, repo, head_ref);
2000 got_ref_close(head_ref);
2004 error = got_repo_match_object_id_prefix(&commit_id,
2005 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2010 error = got_blame(in_repo_path, commit_id, repo, stdout);
2017 got_worktree_close(worktree);
2019 const struct got_error *repo_error;
2020 repo_error = got_repo_close(repo);
2031 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2037 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2038 const char *root_path)
2040 int is_root_path = (strcmp(path, root_path) == 0);
2042 path += strlen(root_path);
2043 while (path[0] == '/')
2046 printf("%s%s%s%s%s\n", id ? id : "", path,
2047 is_root_path ? "" : "/", te->name,
2048 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2051 static const struct got_error *
2052 print_tree(const char *path, struct got_object_id *commit_id,
2053 int show_ids, int recurse, const char *root_path,
2054 struct got_repository *repo)
2056 const struct got_error *err = NULL;
2057 struct got_object_id *tree_id = NULL;
2058 struct got_tree_object *tree = NULL;
2059 const struct got_tree_entries *entries;
2060 struct got_tree_entry *te;
2062 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2066 err = got_object_open_as_tree(&tree, repo, tree_id);
2069 entries = got_object_tree_get_entries(tree);
2070 te = SIMPLEQ_FIRST(&entries->head);
2074 if (sigint_received || sigpipe_received)
2079 err = got_object_id_str(&id_str, te->id);
2082 if (asprintf(&id, "%s ", id_str) == -1) {
2083 err = got_error_from_errno("asprintf");
2089 print_entry(te, id, path, root_path);
2092 if (recurse && S_ISDIR(te->mode)) {
2094 if (asprintf(&child_path, "%s%s%s", path,
2095 path[0] == '/' && path[1] == '\0' ? "" : "/",
2097 err = got_error_from_errno("asprintf");
2100 err = print_tree(child_path, commit_id, show_ids, 1,
2107 te = SIMPLEQ_NEXT(te, entry);
2111 got_object_tree_close(tree);
2116 static const struct got_error *
2117 cmd_tree(int argc, char *argv[])
2119 const struct got_error *error;
2120 struct got_repository *repo = NULL;
2121 struct got_worktree *worktree = NULL;
2123 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2124 struct got_object_id *commit_id = NULL;
2125 char *commit_id_str = NULL;
2126 int show_ids = 0, recurse = 0;
2130 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2135 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2138 commit_id_str = optarg;
2141 repo_path = realpath(optarg, NULL);
2142 if (repo_path == NULL)
2143 err(1, "-r option");
2144 got_path_strip_trailing_slashes(repo_path);
2168 cwd = getcwd(NULL, 0);
2170 error = got_error_from_errno("getcwd");
2173 if (repo_path == NULL) {
2174 error = got_worktree_open(&worktree, cwd);
2175 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2181 strdup(got_worktree_get_repo_path(worktree));
2182 if (repo_path == NULL)
2183 error = got_error_from_errno("strdup");
2187 repo_path = strdup(cwd);
2188 if (repo_path == NULL) {
2189 error = got_error_from_errno("strdup");
2195 error = got_repo_open(&repo, repo_path);
2199 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2205 char *p, *worktree_subdir = cwd +
2206 strlen(got_worktree_get_root_path(worktree));
2207 if (asprintf(&p, "%s/%s",
2208 got_worktree_get_path_prefix(worktree),
2209 worktree_subdir) == -1) {
2210 error = got_error_from_errno("asprintf");
2213 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2220 if (in_repo_path == NULL) {
2221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2226 if (commit_id_str == NULL) {
2227 struct got_reference *head_ref;
2228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2231 error = got_ref_resolve(&commit_id, repo, head_ref);
2232 got_ref_close(head_ref);
2236 error = got_repo_match_object_id_prefix(&commit_id,
2237 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2242 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2243 in_repo_path, repo);
2250 got_worktree_close(worktree);
2252 const struct got_error *repo_error;
2253 repo_error = got_repo_close(repo);
2263 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2267 static const struct got_error *
2268 print_status(void *arg, unsigned char status, const char *path,
2269 struct got_object_id *blob_id, struct got_object_id *commit_id)
2271 printf("%c %s\n", status, path);
2275 static const struct got_error *
2276 cmd_status(int argc, char *argv[])
2278 const struct got_error *error = NULL;
2279 struct got_repository *repo = NULL;
2280 struct got_worktree *worktree = NULL;
2281 char *cwd = NULL, *path = NULL;
2284 while ((ch = getopt(argc, argv, "")) != -1) {
2296 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2300 cwd = getcwd(NULL, 0);
2302 error = got_error_from_errno("getcwd");
2306 error = got_worktree_open(&worktree, cwd);
2313 error = got_error_from_errno("strdup");
2316 } else if (argc == 1) {
2317 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2323 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2327 error = apply_unveil(got_repo_get_path(repo), 1,
2328 got_worktree_get_root_path(worktree), 0);
2332 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2333 check_cancelled, NULL);
2344 "usage: %s ref [-r repository] -l | -d name | name target\n",
2349 static const struct got_error *
2350 list_refs(struct got_repository *repo)
2352 static const struct got_error *err = NULL;
2353 struct got_reflist_head refs;
2354 struct got_reflist_entry *re;
2356 SIMPLEQ_INIT(&refs);
2357 err = got_ref_list(&refs, repo);
2361 SIMPLEQ_FOREACH(re, &refs, entry) {
2363 refstr = got_ref_to_str(re->ref);
2365 return got_error_from_errno("got_ref_to_str");
2366 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2370 got_ref_list_free(&refs);
2374 static const struct got_error *
2375 delete_ref(struct got_repository *repo, const char *refname)
2377 const struct got_error *err = NULL;
2378 struct got_reference *ref;
2380 err = got_ref_open(&ref, repo, refname, 0);
2384 err = got_ref_delete(ref, repo);
2389 static const struct got_error *
2390 add_ref(struct got_repository *repo, const char *refname, const char *target)
2392 const struct got_error *err = NULL;
2393 struct got_object_id *id;
2394 struct got_reference *ref = NULL;
2397 * Don't let the user create a reference named '-'.
2398 * While technically a valid reference name, this case is usually
2399 * an unintended typo.
2401 if (refname[0] == '-' && refname[1] == '\0')
2402 return got_error(GOT_ERR_BAD_REF_NAME);
2404 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2407 struct got_reference *target_ref;
2409 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2411 err = got_ref_open(&target_ref, repo, target, 0);
2414 err = got_ref_resolve(&id, repo, target_ref);
2415 got_ref_close(target_ref);
2420 err = got_ref_alloc(&ref, refname, id);
2424 err = got_ref_write(ref, repo);
2432 static const struct got_error *
2433 cmd_ref(int argc, char *argv[])
2435 const struct got_error *error = NULL;
2436 struct got_repository *repo = NULL;
2437 struct got_worktree *worktree = NULL;
2438 char *cwd = NULL, *repo_path = NULL;
2439 int ch, do_list = 0;
2440 const char *delref = NULL;
2442 /* TODO: Add -s option for adding symbolic references. */
2443 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2449 repo_path = realpath(optarg, NULL);
2450 if (repo_path == NULL)
2451 err(1, "-r option");
2452 got_path_strip_trailing_slashes(repo_path);
2463 if (do_list && delref)
2464 errx(1, "-l and -d options are mutually exclusive\n");
2469 if (do_list || delref) {
2472 } else if (argc != 2)
2477 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2481 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2482 "sendfd unveil", NULL) == -1)
2486 cwd = getcwd(NULL, 0);
2488 error = got_error_from_errno("getcwd");
2492 if (repo_path == NULL) {
2493 error = got_worktree_open(&worktree, cwd);
2494 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2500 strdup(got_worktree_get_repo_path(worktree));
2501 if (repo_path == NULL)
2502 error = got_error_from_errno("strdup");
2506 repo_path = strdup(cwd);
2507 if (repo_path == NULL) {
2508 error = got_error_from_errno("strdup");
2514 error = got_repo_open(&repo, repo_path);
2518 error = apply_unveil(got_repo_get_path(repo), do_list,
2519 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2524 error = list_refs(repo);
2526 error = delete_ref(repo, delref);
2528 error = add_ref(repo, argv[0], argv[1]);
2531 got_repo_close(repo);
2533 got_worktree_close(worktree);
2543 "usage: %s branch [-r repository] -l | -d name | "
2544 "name [base-branch]\n", getprogname());
2548 static const struct got_error *
2549 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2551 static const struct got_error *err = NULL;
2552 struct got_reflist_head refs;
2553 struct got_reflist_entry *re;
2555 SIMPLEQ_INIT(&refs);
2557 err = got_ref_list(&refs, repo);
2561 SIMPLEQ_FOREACH(re, &refs, entry) {
2562 const char *refname, *marker = " ";
2564 refname = got_ref_get_name(re->ref);
2565 if (strncmp(refname, "refs/heads/", 11) != 0)
2567 if (worktree && strcmp(refname,
2568 got_worktree_get_head_ref_name(worktree)) == 0) {
2569 struct got_object_id *id = NULL;
2570 err = got_ref_resolve(&id, repo, re->ref);
2573 if (got_object_id_cmp(id,
2574 got_worktree_get_base_commit_id(worktree)) == 0)
2581 refstr = got_ref_to_str(re->ref);
2583 return got_error_from_errno("got_ref_to_str");
2584 printf("%s%s: %s\n", marker, refname, refstr);
2588 got_ref_list_free(&refs);
2592 static const struct got_error *
2593 delete_branch(struct got_repository *repo, const char *branch_name)
2595 const struct got_error *err = NULL;
2596 struct got_reference *ref;
2599 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2600 return got_error_from_errno("asprintf");
2602 err = got_ref_open(&ref, repo, refname, 0);
2606 err = got_ref_delete(ref, repo);
2613 static const struct got_error *
2614 add_branch(struct got_repository *repo, const char *branch_name,
2615 const char *base_branch)
2617 const struct got_error *err = NULL;
2618 struct got_object_id *id = NULL;
2619 struct got_reference *ref = NULL;
2620 char *base_refname = NULL, *refname = NULL;
2621 struct got_reference *base_ref;
2624 * Don't let the user create a branch named '-'.
2625 * While technically a valid reference name, this case is usually
2626 * an unintended typo.
2628 if (branch_name[0] == '-' && branch_name[1] == '\0')
2629 return got_error(GOT_ERR_BAD_REF_NAME);
2631 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2632 base_refname = strdup(GOT_REF_HEAD);
2633 if (base_refname == NULL)
2634 return got_error_from_errno("strdup");
2635 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2636 return got_error_from_errno("asprintf");
2638 err = got_ref_open(&base_ref, repo, base_refname, 0);
2641 err = got_ref_resolve(&id, repo, base_ref);
2642 got_ref_close(base_ref);
2646 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2647 err = got_error_from_errno("asprintf");
2651 err = got_ref_open(&ref, repo, refname, 0);
2653 err = got_error(GOT_ERR_BRANCH_EXISTS);
2655 } else if (err->code != GOT_ERR_NOT_REF)
2658 err = got_ref_alloc(&ref, refname, id);
2662 err = got_ref_write(ref, repo);
2672 static const struct got_error *
2673 cmd_branch(int argc, char *argv[])
2675 const struct got_error *error = NULL;
2676 struct got_repository *repo = NULL;
2677 struct got_worktree *worktree = NULL;
2678 char *cwd = NULL, *repo_path = NULL;
2679 int ch, do_list = 0;
2680 const char *delref = NULL;
2682 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2688 repo_path = realpath(optarg, NULL);
2689 if (repo_path == NULL)
2690 err(1, "-r option");
2691 got_path_strip_trailing_slashes(repo_path);
2702 if (do_list && delref)
2703 errx(1, "-l and -d options are mutually exclusive\n");
2708 if (do_list || delref) {
2711 } else if (argc < 1 || argc > 2)
2716 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2720 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2721 "sendfd unveil", NULL) == -1)
2725 cwd = getcwd(NULL, 0);
2727 error = got_error_from_errno("getcwd");
2731 if (repo_path == NULL) {
2732 error = got_worktree_open(&worktree, cwd);
2733 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2739 strdup(got_worktree_get_repo_path(worktree));
2740 if (repo_path == NULL)
2741 error = got_error_from_errno("strdup");
2745 repo_path = strdup(cwd);
2746 if (repo_path == NULL) {
2747 error = got_error_from_errno("strdup");
2753 error = got_repo_open(&repo, repo_path);
2757 error = apply_unveil(got_repo_get_path(repo), do_list,
2758 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2763 error = list_branches(repo, worktree);
2765 error = delete_branch(repo, delref);
2767 const char *base_branch;
2769 base_branch = worktree ?
2770 got_worktree_get_head_ref_name(worktree) :
2773 base_branch = argv[1];
2774 error = add_branch(repo, argv[0], base_branch);
2778 got_repo_close(repo);
2780 got_worktree_close(worktree);
2789 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2793 static const struct got_error *
2794 cmd_add(int argc, char *argv[])
2796 const struct got_error *error = NULL;
2797 struct got_repository *repo = NULL;
2798 struct got_worktree *worktree = NULL;
2800 struct got_pathlist_head paths;
2801 struct got_pathlist_entry *pe;
2806 while ((ch = getopt(argc, argv, "")) != -1) {
2818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2825 cwd = getcwd(NULL, 0);
2827 error = got_error_from_errno("getcwd");
2831 error = got_worktree_open(&worktree, cwd);
2835 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2839 error = apply_unveil(got_repo_get_path(repo), 1,
2840 got_worktree_get_root_path(worktree), 0);
2844 for (x = 0; x < argc; x++) {
2845 char *path = realpath(argv[x], NULL);
2847 error = got_error_from_errno2("realpath", argv[x]);
2851 got_path_strip_trailing_slashes(path);
2852 error = got_pathlist_insert(&pe, &paths, path, NULL);
2858 error = got_worktree_schedule_add(worktree, &paths, print_status,
2862 got_repo_close(repo);
2864 got_worktree_close(worktree);
2865 TAILQ_FOREACH(pe, &paths, entry)
2866 free((char *)pe->path);
2867 got_pathlist_free(&paths);
2875 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2879 static const struct got_error *
2880 cmd_remove(int argc, char *argv[])
2882 const struct got_error *error = NULL;
2883 struct got_worktree *worktree = NULL;
2884 struct got_repository *repo = NULL;
2886 struct got_pathlist_head paths;
2887 struct got_pathlist_entry *pe;
2888 int ch, i, delete_local_mods = 0;
2892 while ((ch = getopt(argc, argv, "f")) != -1) {
2895 delete_local_mods = 1;
2907 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2914 cwd = getcwd(NULL, 0);
2916 error = got_error_from_errno("getcwd");
2919 error = got_worktree_open(&worktree, cwd);
2923 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2927 error = apply_unveil(got_repo_get_path(repo), 1,
2928 got_worktree_get_root_path(worktree), 0);
2932 for (i = 0; i < argc; i++) {
2933 char *path = realpath(argv[i], NULL);
2935 error = got_error_from_errno2("realpath", argv[i]);
2939 got_path_strip_trailing_slashes(path);
2940 error = got_pathlist_insert(&pe, &paths, path, NULL);
2946 error = got_worktree_schedule_delete(worktree, &paths,
2947 delete_local_mods, print_status, NULL, repo);
2952 got_repo_close(repo);
2954 got_worktree_close(worktree);
2955 TAILQ_FOREACH(pe, &paths, entry)
2956 free((char *)pe->path);
2957 got_pathlist_free(&paths);
2965 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2969 static const struct got_error *
2970 revert_progress(void *arg, unsigned char status, const char *path)
2972 while (path[0] == '/')
2974 printf("%c %s\n", status, path);
2978 static const struct got_error *
2979 cmd_revert(int argc, char *argv[])
2981 const struct got_error *error = NULL;
2982 struct got_worktree *worktree = NULL;
2983 struct got_repository *repo = NULL;
2984 char *cwd = NULL, *path = NULL;
2985 struct got_pathlist_head paths;
2986 struct got_pathlist_entry *pe;
2991 while ((ch = getopt(argc, argv, "")) != -1) {
3003 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3004 "unveil", NULL) == -1)
3010 for (i = 0; i < argc; i++) {
3011 char *path = realpath(argv[i], NULL);
3013 error = got_error_from_errno2("realpath", argv[i]);
3017 got_path_strip_trailing_slashes(path);
3018 error = got_pathlist_insert(&pe, &paths, path, NULL);
3025 cwd = getcwd(NULL, 0);
3027 error = got_error_from_errno("getcwd");
3030 error = got_worktree_open(&worktree, cwd);
3034 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3038 error = apply_unveil(got_repo_get_path(repo), 1,
3039 got_worktree_get_root_path(worktree), 0);
3043 error = got_worktree_revert(worktree, &paths,
3044 revert_progress, NULL, repo);
3049 got_repo_close(repo);
3051 got_worktree_close(worktree);
3060 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3064 struct collect_commit_logmsg_arg {
3065 const char *cmdline_log;
3067 const char *worktree_path;
3068 const char *branch_name;
3069 const char *repo_path;
3074 static const struct got_error *
3075 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3078 char *initial_content = NULL;
3079 struct got_pathlist_entry *pe;
3080 const struct got_error *err = NULL;
3081 char *template = NULL;
3082 struct collect_commit_logmsg_arg *a = arg;
3086 /* if a message was specified on the command line, just use it */
3087 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3088 len = strlen(a->cmdline_log) + 1;
3089 *logmsg = malloc(len + 1);
3090 if (*logmsg == NULL)
3091 return got_error_from_errno("malloc");
3092 strlcpy(*logmsg, a->cmdline_log, len);
3096 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3097 return got_error_from_errno("asprintf");
3099 if (asprintf(&initial_content,
3100 "\n# changes to be committed on branch %s:\n",
3101 a->branch_name) == -1)
3102 return got_error_from_errno("asprintf");
3104 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3108 dprintf(fd, initial_content);
3110 TAILQ_FOREACH(pe, commitable_paths, entry) {
3111 struct got_commitable *ct = pe->data;
3112 dprintf(fd, "# %c %s\n",
3113 got_commitable_get_status(ct),
3114 got_commitable_get_path(ct));
3118 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3120 unlink(a->logmsg_path);
3121 free(a->logmsg_path);
3122 free(initial_content);
3125 /* Editor is done; we can now apply unveil(2) */
3127 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3136 static const struct got_error *
3137 cmd_commit(int argc, char *argv[])
3139 const struct got_error *error = NULL;
3140 struct got_worktree *worktree = NULL;
3141 struct got_repository *repo = NULL;
3142 char *cwd = NULL, *path = NULL, *id_str = NULL;
3143 struct got_object_id *id = NULL;
3144 const char *logmsg = NULL;
3145 const char *got_author = getenv("GOT_AUTHOR");
3146 struct collect_commit_logmsg_arg cl_arg;
3147 char *editor = NULL;
3148 int ch, rebase_in_progress;
3150 while ((ch = getopt(argc, argv, "m:")) != -1) {
3165 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3166 "unveil", NULL) == -1)
3170 path = realpath(argv[0], NULL);
3172 error = got_error_from_errno2("realpath", argv[0]);
3175 got_path_strip_trailing_slashes(path);
3176 } else if (argc != 0)
3179 if (got_author == NULL) {
3180 /* TODO: Look current user up in password database */
3181 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3185 cwd = getcwd(NULL, 0);
3187 error = got_error_from_errno("getcwd");
3190 error = got_worktree_open(&worktree, cwd);
3194 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3197 if (rebase_in_progress) {
3198 error = got_error(GOT_ERR_REBASING);
3202 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3207 * unveil(2) traverses exec(2); if an editor is used we have
3208 * to apply unveil after the log message has been written.
3210 if (logmsg == NULL || strlen(logmsg) == 0)
3211 error = get_editor(&editor);
3213 error = apply_unveil(got_repo_get_path(repo), 0,
3214 got_worktree_get_root_path(worktree), 0);
3218 cl_arg.editor = editor;
3219 cl_arg.cmdline_log = logmsg;
3220 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3221 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3222 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3223 cl_arg.branch_name += 5;
3224 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3225 cl_arg.branch_name += 6;
3226 cl_arg.repo_path = got_repo_get_path(repo);
3227 cl_arg.logmsg_path = NULL;
3228 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3229 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3231 if (cl_arg.logmsg_path)
3232 fprintf(stderr, "%s: log message preserved in %s\n",
3233 getprogname(), cl_arg.logmsg_path);
3237 if (cl_arg.logmsg_path)
3238 unlink(cl_arg.logmsg_path);
3240 error = got_object_id_str(&id_str, id);
3243 printf("Created commit %s\n", id_str);
3246 got_repo_close(repo);
3248 got_worktree_close(worktree);
3257 usage_cherrypick(void)
3259 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3263 static const struct got_error *
3264 cmd_cherrypick(int argc, char *argv[])
3266 const struct got_error *error = NULL;
3267 struct got_worktree *worktree = NULL;
3268 struct got_repository *repo = NULL;
3269 char *cwd = NULL, *commit_id_str = NULL;
3270 struct got_object_id *commit_id = NULL;
3271 struct got_commit_object *commit = NULL;
3272 struct got_object_qid *pid;
3273 struct got_reference *head_ref = NULL;
3274 int ch, did_something = 0;
3276 while ((ch = getopt(argc, argv, "")) != -1) {
3288 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3289 "unveil", NULL) == -1)
3295 cwd = getcwd(NULL, 0);
3297 error = got_error_from_errno("getcwd");
3300 error = got_worktree_open(&worktree, cwd);
3304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3308 error = apply_unveil(got_repo_get_path(repo), 0,
3309 got_worktree_get_root_path(worktree), 0);
3313 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3314 GOT_OBJ_TYPE_COMMIT, repo);
3315 if (error != NULL) {
3316 struct got_reference *ref;
3317 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3319 error = got_ref_open(&ref, repo, argv[0], 0);
3322 error = got_ref_resolve(&commit_id, repo, ref);
3327 error = got_object_id_str(&commit_id_str, commit_id);
3331 error = got_ref_open(&head_ref, repo,
3332 got_worktree_get_head_ref_name(worktree), 0);
3336 error = check_same_branch(commit_id, head_ref, repo);
3338 if (error->code != GOT_ERR_ANCESTRY)
3342 error = got_error(GOT_ERR_SAME_BRANCH);
3346 error = got_object_open_as_commit(&commit, repo, commit_id);
3349 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3350 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3351 commit_id, repo, update_progress, &did_something, check_cancelled,
3357 printf("Merged commit %s\n", commit_id_str);
3360 got_object_commit_close(commit);
3361 free(commit_id_str);
3363 got_ref_close(head_ref);
3365 got_worktree_close(worktree);
3367 got_repo_close(repo);
3374 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3378 static const struct got_error *
3379 cmd_backout(int argc, char *argv[])
3381 const struct got_error *error = NULL;
3382 struct got_worktree *worktree = NULL;
3383 struct got_repository *repo = NULL;
3384 char *cwd = NULL, *commit_id_str = NULL;
3385 struct got_object_id *commit_id = NULL;
3386 struct got_commit_object *commit = NULL;
3387 struct got_object_qid *pid;
3388 struct got_reference *head_ref = NULL;
3389 int ch, did_something = 0;
3391 while ((ch = getopt(argc, argv, "")) != -1) {
3403 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3404 "unveil", NULL) == -1)
3410 cwd = getcwd(NULL, 0);
3412 error = got_error_from_errno("getcwd");
3415 error = got_worktree_open(&worktree, cwd);
3419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3423 error = apply_unveil(got_repo_get_path(repo), 0,
3424 got_worktree_get_root_path(worktree), 0);
3428 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3429 GOT_OBJ_TYPE_COMMIT, repo);
3430 if (error != NULL) {
3431 struct got_reference *ref;
3432 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3434 error = got_ref_open(&ref, repo, argv[0], 0);
3437 error = got_ref_resolve(&commit_id, repo, ref);
3442 error = got_object_id_str(&commit_id_str, commit_id);
3446 error = got_ref_open(&head_ref, repo,
3447 got_worktree_get_head_ref_name(worktree), 0);
3451 error = check_same_branch(commit_id, head_ref, repo);
3455 error = got_object_open_as_commit(&commit, repo, commit_id);
3458 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3460 error = got_error(GOT_ERR_ROOT_COMMIT);
3464 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3465 update_progress, &did_something, check_cancelled, NULL);
3470 printf("Backed out commit %s\n", commit_id_str);
3473 got_object_commit_close(commit);
3474 free(commit_id_str);
3476 got_ref_close(head_ref);
3478 got_worktree_close(worktree);
3480 got_repo_close(repo);
3487 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3492 static const struct got_error *
3493 show_rebase_progress(struct got_commit_object *commit,
3494 struct got_object_id *old_id, struct got_object_id *new_id)
3496 const struct got_error *err;
3497 char *old_id_str = NULL, *new_id_str = NULL;
3498 char *logmsg0 = NULL, *logmsg, *nl;
3501 err = got_object_id_str(&old_id_str, old_id);
3506 err = got_object_id_str(&new_id_str, new_id);
3511 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3512 if (logmsg0 == NULL) {
3513 err = got_error_from_errno("strdup");
3518 while (isspace((unsigned char)logmsg[0]))
3521 old_id_str[12] = '\0';
3523 new_id_str[12] = '\0';
3524 len = strlen(logmsg);
3528 nl = strchr(logmsg, '\n');
3531 printf("%s -> %s: %s\n", old_id_str,
3532 new_id_str ? new_id_str : "no-op change", logmsg);
3540 static const struct got_error *
3541 rebase_progress(void *arg, unsigned char status, const char *path)
3543 unsigned char *rebase_status = arg;
3545 while (path[0] == '/')
3547 printf("%c %s\n", status, path);
3549 if (*rebase_status == GOT_STATUS_CONFLICT)
3551 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3552 *rebase_status = status;
3556 static const struct got_error *
3557 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3558 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3559 struct got_repository *repo)
3561 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3562 return got_worktree_rebase_complete(worktree,
3563 new_base_branch, tmp_branch, branch, repo);
3566 static const struct got_error *
3567 rebase_commit(struct got_pathlist_head *merged_paths,
3568 struct got_worktree *worktree, struct got_reference *tmp_branch,
3569 struct got_object_id *commit_id, struct got_repository *repo)
3571 const struct got_error *error;
3572 struct got_commit_object *commit;
3573 struct got_object_id *new_commit_id;
3575 error = got_object_open_as_commit(&commit, repo, commit_id);
3579 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3580 worktree, tmp_branch, commit, commit_id, repo);
3582 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3584 error = show_rebase_progress(commit, commit_id, NULL);
3586 error = show_rebase_progress(commit, commit_id, new_commit_id);
3587 free(new_commit_id);
3590 got_object_commit_close(commit);
3594 struct check_path_prefix_arg {
3595 const char *path_prefix;
3599 static const struct got_error *
3600 check_path_prefix(void *arg, struct got_blob_object *blob1,
3601 struct got_blob_object *blob2, struct got_object_id *id1,
3602 struct got_object_id *id2, const char *path1, const char *path2,
3603 struct got_repository *repo)
3605 struct check_path_prefix_arg *a = arg;
3607 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3608 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3609 return got_error(GOT_ERR_REBASE_PATH);
3614 static const struct got_error *
3615 rebase_check_path_prefix(struct got_object_id *parent_id,
3616 struct got_object_id *commit_id, const char *path_prefix,
3617 struct got_repository *repo)
3619 const struct got_error *err;
3620 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3621 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3622 struct check_path_prefix_arg cpp_arg;
3624 if (got_path_is_root_dir(path_prefix))
3627 err = got_object_open_as_commit(&commit, repo, commit_id);
3631 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3635 err = got_object_open_as_tree(&tree1, repo,
3636 got_object_commit_get_tree_id(parent_commit));
3640 err = got_object_open_as_tree(&tree2, repo,
3641 got_object_commit_get_tree_id(commit));
3645 cpp_arg.path_prefix = path_prefix;
3646 cpp_arg.len = strlen(path_prefix);
3647 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3651 got_object_tree_close(tree1);
3653 got_object_tree_close(tree2);
3655 got_object_commit_close(commit);
3657 got_object_commit_close(parent_commit);
3661 static const struct got_error *
3662 collect_commits_to_rebase(struct got_object_id_queue *commits,
3663 struct got_object_id *initial_commit_id,
3664 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3665 const char *path_prefix, struct got_repository *repo)
3667 const struct got_error *err = NULL;
3668 struct got_commit_graph *graph = NULL;
3669 struct got_object_id *parent_id = NULL;
3670 struct got_object_qid *qid;
3671 struct got_object_id *commit_id = initial_commit_id;
3673 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3677 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3680 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3681 err = got_commit_graph_iter_next(&parent_id, graph);
3683 if (err->code == GOT_ERR_ITER_COMPLETED) {
3684 err = got_error_msg(GOT_ERR_ANCESTRY,
3685 "ran out of commits to rebase before "
3686 "youngest common ancestor commit has "
3689 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3691 err = got_commit_graph_fetch_commits(graph, 1, repo);
3695 err = rebase_check_path_prefix(parent_id, commit_id,
3700 err = got_object_qid_alloc(&qid, commit_id);
3703 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3704 commit_id = parent_id;
3708 got_commit_graph_close(graph);
3712 static const struct got_error *
3713 cmd_rebase(int argc, char *argv[])
3715 const struct got_error *error = NULL;
3716 struct got_worktree *worktree = NULL;
3717 struct got_repository *repo = NULL;
3719 struct got_reference *branch = NULL;
3720 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3721 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3722 struct got_object_id *resume_commit_id = NULL;
3723 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3724 struct got_commit_object *commit = NULL;
3725 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3726 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3727 struct got_object_id_queue commits;
3728 struct got_pathlist_head merged_paths;
3729 const struct got_object_id_queue *parent_ids;
3730 struct got_object_qid *qid, *pid;
3732 SIMPLEQ_INIT(&commits);
3733 TAILQ_INIT(&merged_paths);
3735 while ((ch = getopt(argc, argv, "ac")) != -1) {
3741 continue_rebase = 1;
3753 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3754 "unveil", NULL) == -1)
3757 if (abort_rebase && continue_rebase)
3759 else if (abort_rebase || continue_rebase) {
3762 } else if (argc != 1)
3765 cwd = getcwd(NULL, 0);
3767 error = got_error_from_errno("getcwd");
3770 error = got_worktree_open(&worktree, cwd);
3774 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3778 error = apply_unveil(got_repo_get_path(repo), 0,
3779 got_worktree_get_root_path(worktree), 0);
3783 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3787 if (rebase_in_progress && abort_rebase) {
3789 error = got_worktree_rebase_continue(&resume_commit_id,
3790 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3793 printf("Switching work tree to %s\n",
3794 got_ref_get_symref_target(new_base_branch));
3795 error = got_worktree_rebase_abort(worktree, repo,
3796 new_base_branch, update_progress, &did_something);
3799 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3800 goto done; /* nothing else to do */
3801 } else if (abort_rebase) {
3802 error = got_error(GOT_ERR_NOT_REBASING);
3806 if (continue_rebase) {
3807 error = got_worktree_rebase_continue(&resume_commit_id,
3808 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3812 error = rebase_commit(NULL, worktree, tmp_branch,
3813 resume_commit_id, repo);
3817 yca_id = got_object_id_dup(resume_commit_id);
3818 if (yca_id == NULL) {
3819 error = got_error_from_errno("got_object_id_dup");
3823 error = got_ref_open(&branch, repo, argv[0], 0);
3827 error = check_same_branch(
3828 got_worktree_get_base_commit_id(worktree), branch, repo);
3830 if (error->code != GOT_ERR_ANCESTRY)
3834 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3835 "specified branch resolves to a commit which "
3836 "is already contained in work tree's branch");
3841 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3845 if (!continue_rebase) {
3846 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3847 got_worktree_get_base_commit_id(worktree),
3848 branch_head_commit_id, repo);
3851 if (yca_id == NULL) {
3852 error = got_error_msg(GOT_ERR_ANCESTRY,
3853 "specified branch shares no common ancestry "
3854 "with work tree's branch");
3858 error = got_worktree_rebase_prepare(&new_base_branch,
3859 &tmp_branch, worktree, branch, repo);
3864 commit_id = branch_head_commit_id;
3865 error = got_object_open_as_commit(&commit, repo, commit_id);
3869 parent_ids = got_object_commit_get_parent_ids(commit);
3870 pid = SIMPLEQ_FIRST(parent_ids);
3871 error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3872 yca_id, got_worktree_get_path_prefix(worktree), repo);
3873 got_object_commit_close(commit);
3878 if (SIMPLEQ_EMPTY(&commits)) {
3879 if (continue_rebase)
3880 error = rebase_complete(worktree, branch,
3881 new_base_branch, tmp_branch, repo);
3883 error = got_error(GOT_ERR_EMPTY_REBASE);
3888 SIMPLEQ_FOREACH(qid, &commits, entry) {
3889 commit_id = qid->id;
3890 parent_id = pid ? pid->id : yca_id;
3893 error = got_worktree_rebase_merge_files(&merged_paths,
3894 worktree, parent_id, commit_id, repo, rebase_progress,
3895 &rebase_status, check_cancelled, NULL);
3899 if (rebase_status == GOT_STATUS_CONFLICT) {
3900 got_worktree_rebase_pathlist_free(&merged_paths);
3904 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3906 got_worktree_rebase_pathlist_free(&merged_paths);
3911 if (rebase_status == GOT_STATUS_CONFLICT) {
3912 error = got_worktree_rebase_postpone(worktree);
3915 error = got_error_msg(GOT_ERR_CONFLICTS,
3916 "conflicts must be resolved before rebasing can continue");
3918 error = rebase_complete(worktree, branch, new_base_branch,
3921 got_object_id_queue_free(&commits);
3922 free(branch_head_commit_id);
3923 free(resume_commit_id);
3926 got_object_commit_close(commit);
3928 got_ref_close(branch);
3929 if (new_base_branch)
3930 got_ref_close(new_base_branch);
3932 got_ref_close(tmp_branch);
3934 got_worktree_close(worktree);
3936 got_repo_close(repo);