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_checkout(void);
80 __dead static void usage_update(void);
81 __dead static void usage_log(void);
82 __dead static void usage_diff(void);
83 __dead static void usage_blame(void);
84 __dead static void usage_tree(void);
85 __dead static void usage_status(void);
86 __dead static void usage_ref(void);
87 __dead static void usage_branch(void);
88 __dead static void usage_add(void);
89 __dead static void usage_remove(void);
90 __dead static void usage_revert(void);
91 __dead static void usage_commit(void);
92 __dead static void usage_cherrypick(void);
93 __dead static void usage_backout(void);
94 __dead static void usage_rebase(void);
96 static const struct got_error* cmd_init(int, char *[]);
97 static const struct got_error* cmd_checkout(int, char *[]);
98 static const struct got_error* cmd_update(int, char *[]);
99 static const struct got_error* cmd_log(int, char *[]);
100 static const struct got_error* cmd_diff(int, char *[]);
101 static const struct got_error* cmd_blame(int, char *[]);
102 static const struct got_error* cmd_tree(int, char *[]);
103 static const struct got_error* cmd_status(int, char *[]);
104 static const struct got_error* cmd_ref(int, char *[]);
105 static const struct got_error* cmd_branch(int, char *[]);
106 static const struct got_error* cmd_add(int, char *[]);
107 static const struct got_error* cmd_remove(int, char *[]);
108 static const struct got_error* cmd_revert(int, char *[]);
109 static const struct got_error* cmd_commit(int, char *[]);
110 static const struct got_error* cmd_cherrypick(int, char *[]);
111 static const struct got_error* cmd_backout(int, char *[]);
112 static const struct got_error* cmd_rebase(int, char *[]);
114 static struct got_cmd got_commands[] = {
115 { "init", cmd_init, usage_init, "" },
116 { "checkout", cmd_checkout, usage_checkout, "co" },
117 { "update", cmd_update, usage_update, "up" },
118 { "log", cmd_log, usage_log, "" },
119 { "diff", cmd_diff, usage_diff, "" },
120 { "blame", cmd_blame, usage_blame, "" },
121 { "tree", cmd_tree, usage_tree, "" },
122 { "status", cmd_status, usage_status, "st" },
123 { "ref", cmd_ref, usage_ref, "" },
124 { "branch", cmd_branch, usage_branch, "br" },
125 { "add", cmd_add, usage_add, "" },
126 { "remove", cmd_remove, usage_remove, "rm" },
127 { "revert", cmd_revert, usage_revert, "rv" },
128 { "commit", cmd_commit, usage_commit, "ci" },
129 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
130 { "backout", cmd_backout, usage_backout, "bo" },
131 { "rebase", cmd_rebase, usage_rebase, "rb" },
139 fprintf(stderr, "commands:");
140 for (i = 0; i < nitems(got_commands); i++) {
141 struct got_cmd *cmd = &got_commands[i];
142 fprintf(stderr, " %s", cmd->cmd_name);
148 main(int argc, char *argv[])
155 setlocale(LC_CTYPE, "");
157 while ((ch = getopt(argc, argv, "h")) != -1) {
175 signal(SIGINT, catch_sigint);
176 signal(SIGPIPE, catch_sigpipe);
178 for (i = 0; i < nitems(got_commands); i++) {
179 const struct got_error *error;
181 cmd = &got_commands[i];
183 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
184 strcmp(cmd->cmd_alias, argv[0]) != 0)
188 got_commands[i].cmd_usage();
190 error = got_commands[i].cmd_main(argc, argv);
191 if (error && !(sigint_received || sigpipe_received)) {
192 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
199 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
207 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
213 static const struct got_error *
214 get_editor(char **abspath)
216 const struct got_error *err = NULL;
219 editor = getenv("VISUAL");
221 editor = getenv("EDITOR");
224 err = got_path_find_prog(abspath, editor);
229 if (*abspath == NULL) {
230 *abspath = strdup("/bin/ed");
231 if (*abspath == NULL)
232 return got_error_from_errno("strdup");
238 static const struct got_error *
239 apply_unveil(const char *repo_path, int repo_read_only,
240 const char *worktree_path, int create_worktree)
242 const struct got_error *err;
244 if (create_worktree) {
245 /* Pre-create work tree path to avoid unveiling its parents. */
246 err = got_path_mkdir(worktree_path);
248 if (errno == EEXIST) {
249 if (got_path_dir_is_empty(worktree_path)) {
253 err = got_error_path(worktree_path,
254 GOT_ERR_DIR_NOT_EMPTY);
258 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
262 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
263 return got_error_from_errno2("unveil", repo_path);
265 if (worktree_path && unveil(worktree_path, "rwc") != 0)
266 return got_error_from_errno2("unveil", worktree_path);
268 if (unveil("/tmp", "rwc") != 0)
269 return got_error_from_errno2("unveil", "/tmp");
271 err = got_privsep_unveil_exec_helpers();
275 if (unveil(NULL, NULL) != 0)
276 return got_error_from_errno("unveil");
284 fprintf(stderr, "usage: %s init path\n", getprogname());
288 static const struct got_error *
289 cmd_init(int argc, char *argv[])
291 const struct got_error *error = NULL;
292 char *repo_path = NULL;
295 while ((ch = getopt(argc, argv, "")) != -1) {
307 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
313 repo_path = strdup(argv[0]);
314 if (repo_path == NULL)
315 return got_error_from_errno("strdup");
317 got_path_strip_trailing_slashes(repo_path);
319 error = got_path_mkdir(repo_path);
321 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
324 error = apply_unveil(repo_path, 0, NULL, 0);
328 error = got_repo_init(repo_path);
340 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
341 "[-p prefix] repository-path [worktree-path]\n", getprogname());
346 checkout_progress(void *arg, unsigned char status, const char *path)
348 char *worktree_path = arg;
350 /* Base commit bump happens silently. */
351 if (status == GOT_STATUS_BUMP_BASE)
354 while (path[0] == '/')
357 printf("%c %s/%s\n", status, worktree_path, path);
360 static const struct got_error *
361 check_cancelled(void *arg)
363 if (sigint_received || sigpipe_received)
364 return got_error(GOT_ERR_CANCELLED);
368 static const struct got_error *
369 check_linear_ancestry(struct got_object_id *commit_id,
370 struct got_object_id *base_commit_id, struct got_repository *repo)
372 const struct got_error *err = NULL;
373 struct got_object_id *yca_id;
375 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
376 commit_id, base_commit_id, repo);
381 return got_error(GOT_ERR_ANCESTRY);
384 * Require a straight line of history between the target commit
385 * and the work tree's base commit.
387 * Non-linear situations such as this require a rebase:
389 * (commit) D F (base_commit)
397 * 'got update' only handles linear cases:
398 * Update forwards in time: A (base/yca) - B - C - D (commit)
399 * Update backwards in time: D (base) - C - B - A (commit/yca)
401 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
402 got_object_id_cmp(base_commit_id, yca_id) != 0)
403 return got_error(GOT_ERR_ANCESTRY);
409 static const struct got_error *
410 check_same_branch(struct got_object_id *commit_id,
411 struct got_reference *head_ref, struct got_repository *repo)
413 const struct got_error *err = NULL;
414 struct got_commit_graph *graph = NULL;
415 struct got_object_id *head_commit_id = NULL;
416 int is_same_branch = 0;
418 err = got_ref_resolve(&head_commit_id, repo, head_ref);
422 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
426 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
431 struct got_object_id *id;
432 err = got_commit_graph_iter_next(&id, graph);
434 if (err->code == GOT_ERR_ITER_COMPLETED) {
438 else if (err->code != GOT_ERR_ITER_NEED_MORE)
440 err = got_commit_graph_fetch_commits(graph, 1,
447 if (got_object_id_cmp(id, commit_id) == 0) {
455 got_commit_graph_close(graph);
456 free(head_commit_id);
457 if (!err && !is_same_branch)
458 err = got_error(GOT_ERR_ANCESTRY);
462 static const struct got_error *
463 cmd_checkout(int argc, char *argv[])
465 const struct got_error *error = NULL;
466 struct got_repository *repo = NULL;
467 struct got_reference *head_ref = NULL;
468 struct got_worktree *worktree = NULL;
469 char *repo_path = NULL;
470 char *worktree_path = NULL;
471 const char *path_prefix = "";
472 const char *branch_name = GOT_REF_HEAD;
473 char *commit_id_str = NULL;
474 int ch, same_path_prefix;
476 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
479 branch_name = optarg;
482 commit_id_str = strdup(optarg);
483 if (commit_id_str == NULL)
484 return got_error_from_errno("strdup");
487 path_prefix = optarg;
499 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
500 "unveil", NULL) == -1)
504 char *cwd, *base, *dotgit;
505 repo_path = realpath(argv[0], NULL);
506 if (repo_path == NULL)
507 return got_error_from_errno2("realpath", argv[0]);
508 cwd = getcwd(NULL, 0);
510 error = got_error_from_errno("getcwd");
513 if (path_prefix[0]) {
514 base = basename(path_prefix);
516 error = got_error_from_errno2("basename",
521 base = basename(repo_path);
523 error = got_error_from_errno2("basename",
528 dotgit = strstr(base, ".git");
531 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
532 error = got_error_from_errno("asprintf");
537 } else if (argc == 2) {
538 repo_path = realpath(argv[0], NULL);
539 if (repo_path == NULL) {
540 error = got_error_from_errno2("realpath", argv[0]);
543 worktree_path = realpath(argv[1], NULL);
544 if (worktree_path == NULL) {
545 error = got_error_from_errno2("realpath", argv[1]);
551 got_path_strip_trailing_slashes(repo_path);
552 got_path_strip_trailing_slashes(worktree_path);
554 error = got_repo_open(&repo, repo_path);
558 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
562 error = got_ref_open(&head_ref, repo, branch_name, 0);
566 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
567 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
570 error = got_worktree_open(&worktree, worktree_path);
574 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
578 if (!same_path_prefix) {
579 error = got_error(GOT_ERR_PATH_PREFIX);
584 struct got_object_id *commit_id;
585 error = got_repo_match_object_id_prefix(&commit_id,
586 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
589 error = check_linear_ancestry(commit_id,
590 got_worktree_get_base_commit_id(worktree), repo);
595 error = check_same_branch(commit_id, head_ref, repo);
598 error = got_worktree_set_base_commit_id(worktree, repo,
605 error = got_worktree_checkout_files(worktree, "", repo,
606 checkout_progress, worktree_path, check_cancelled, NULL);
610 printf("Now shut up and hack\n");
622 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
628 update_progress(void *arg, unsigned char status, const char *path)
630 int *did_something = arg;
632 if (status == GOT_STATUS_EXISTS)
637 /* Base commit bump happens silently. */
638 if (status == GOT_STATUS_BUMP_BASE)
641 while (path[0] == '/')
643 printf("%c %s\n", status, path);
646 static const struct got_error *
647 switch_head_ref(struct got_reference *head_ref,
648 struct got_object_id *commit_id, struct got_worktree *worktree,
649 struct got_repository *repo)
651 const struct got_error *err = NULL;
653 int ref_has_moved = 0;
655 /* Trivial case: switching between two different references. */
656 if (strcmp(got_ref_get_name(head_ref),
657 got_worktree_get_head_ref_name(worktree)) != 0) {
658 printf("Switching work tree from %s to %s\n",
659 got_worktree_get_head_ref_name(worktree),
660 got_ref_get_name(head_ref));
661 return got_worktree_set_head_ref(worktree, head_ref);
664 err = check_linear_ancestry(commit_id,
665 got_worktree_get_base_commit_id(worktree), repo);
667 if (err->code != GOT_ERR_ANCESTRY)
674 /* Switching to a rebased branch with the same reference name. */
675 err = got_object_id_str(&base_id_str,
676 got_worktree_get_base_commit_id(worktree));
679 printf("Reference %s now points at a different branch\n",
680 got_worktree_get_head_ref_name(worktree));
681 printf("Switching work tree from %s to %s\n", base_id_str,
682 got_worktree_get_head_ref_name(worktree));
686 static const struct got_error *
687 cmd_update(int argc, char *argv[])
689 const struct got_error *error = NULL;
690 struct got_repository *repo = NULL;
691 struct got_worktree *worktree = NULL;
692 char *worktree_path = NULL, *path = NULL;
693 struct got_object_id *commit_id = NULL;
694 char *commit_id_str = NULL;
695 const char *branch_name = NULL;
696 struct got_reference *head_ref = NULL;
697 int ch, did_something = 0;
699 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
702 branch_name = optarg;
705 commit_id_str = strdup(optarg);
706 if (commit_id_str == NULL)
707 return got_error_from_errno("strdup");
719 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
720 "unveil", NULL) == -1)
723 worktree_path = getcwd(NULL, 0);
724 if (worktree_path == NULL) {
725 error = got_error_from_errno("getcwd");
728 error = got_worktree_open(&worktree, worktree_path);
735 error = got_error_from_errno("strdup");
738 } else if (argc == 1) {
739 error = got_worktree_resolve_path(&path, worktree, argv[0]);
745 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
749 error = apply_unveil(got_repo_get_path(repo), 0,
750 got_worktree_get_root_path(worktree), 0);
754 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
755 got_worktree_get_head_ref_name(worktree), 0);
758 if (commit_id_str == NULL) {
759 error = got_ref_resolve(&commit_id, repo, head_ref);
762 error = got_object_id_str(&commit_id_str, commit_id);
766 error = got_repo_match_object_id_prefix(&commit_id,
767 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
773 struct got_object_id *head_commit_id;
774 if (strlen(path) != 0) {
775 fprintf(stderr, "%s: switching between branches "
776 "requires that the entire work tree "
777 "gets updated, not just '%s'\n",
778 getprogname(), path);
779 error = got_error(GOT_ERR_BAD_PATH);
782 error = got_ref_resolve(&head_commit_id, repo, head_ref);
785 error = check_linear_ancestry(commit_id, head_commit_id, repo);
786 free(head_commit_id);
789 error = check_same_branch(commit_id, head_ref, repo);
792 error = switch_head_ref(head_ref, commit_id, worktree, repo);
796 error = check_linear_ancestry(commit_id,
797 got_worktree_get_base_commit_id(worktree), repo);
799 if (error->code == GOT_ERR_ANCESTRY)
800 error = got_error(GOT_ERR_BRANCH_MOVED);
803 error = check_same_branch(commit_id, head_ref, repo);
808 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
810 error = got_worktree_set_base_commit_id(worktree, repo,
816 error = got_worktree_checkout_files(worktree, path, repo,
817 update_progress, &did_something, check_cancelled, NULL);
822 printf("Updated to commit %s\n", commit_id_str);
824 printf("Already up-to-date\n");
833 static const struct got_error *
834 print_patch(struct got_commit_object *commit, struct got_object_id *id,
835 int diff_context, struct got_repository *repo)
837 const struct got_error *err = NULL;
838 struct got_tree_object *tree1 = NULL, *tree2;
839 struct got_object_qid *qid;
840 char *id_str1 = NULL, *id_str2;
841 struct got_diff_blob_output_unidiff_arg arg;
843 err = got_object_open_as_tree(&tree2, repo,
844 got_object_commit_get_tree_id(commit));
848 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
850 struct got_commit_object *pcommit;
852 err = got_object_open_as_commit(&pcommit, repo, qid->id);
856 err = got_object_open_as_tree(&tree1, repo,
857 got_object_commit_get_tree_id(pcommit));
858 got_object_commit_close(pcommit);
862 err = got_object_id_str(&id_str1, qid->id);
867 err = got_object_id_str(&id_str2, id);
871 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
872 arg.diff_context = diff_context;
873 arg.outfile = stdout;
874 err = got_diff_tree(tree1, tree2, "", "", repo,
875 got_diff_blob_output_unidiff, &arg);
878 got_object_tree_close(tree1);
879 got_object_tree_close(tree2);
886 get_datestr(time_t *time, char *datebuf)
888 char *p, *s = ctime_r(time, datebuf);
895 static const struct got_error *
896 print_commit(struct got_commit_object *commit, struct got_object_id *id,
897 struct got_repository *repo, int show_patch, int diff_context,
898 struct got_reflist_head *refs)
900 const struct got_error *err = NULL;
901 char *id_str, *datestr, *logmsg0, *logmsg, *line;
903 time_t committer_time;
904 const char *author, *committer;
905 char *refs_str = NULL;
906 struct got_reflist_entry *re;
908 SIMPLEQ_FOREACH(re, refs, entry) {
911 if (got_object_id_cmp(re->id, id) != 0)
913 name = got_ref_get_name(re->ref);
914 if (strcmp(name, GOT_REF_HEAD) == 0)
916 if (strncmp(name, "refs/", 5) == 0)
918 if (strncmp(name, "got/", 4) == 0)
920 if (strncmp(name, "heads/", 6) == 0)
922 if (strncmp(name, "remotes/", 8) == 0)
925 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
927 err = got_error_from_errno("asprintf");
933 err = got_object_id_str(&id_str, id);
937 printf("-----------------------------------------------\n");
938 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
939 refs_str ? refs_str : "", refs_str ? ")" : "");
944 printf("from: %s\n", got_object_commit_get_author(commit));
945 committer_time = got_object_commit_get_committer_time(commit);
946 datestr = get_datestr(&committer_time, datebuf);
947 printf("date: %s UTC\n", datestr);
948 author = got_object_commit_get_author(commit);
949 committer = got_object_commit_get_committer(commit);
950 if (strcmp(author, committer) != 0)
951 printf("via: %s\n", committer);
952 if (got_object_commit_get_nparents(commit) > 1) {
953 const struct got_object_id_queue *parent_ids;
954 struct got_object_qid *qid;
956 parent_ids = got_object_commit_get_parent_ids(commit);
957 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
958 err = got_object_id_str(&id_str, qid->id);
961 printf("parent %d: %s\n", n++, id_str);
966 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
968 return got_error_from_errno("strdup");
972 line = strsep(&logmsg, "\n");
974 printf(" %s\n", line);
979 err = print_patch(commit, id, diff_context, repo);
984 if (fflush(stdout) != 0 && err == NULL)
985 err = got_error_from_errno("fflush");
989 static const struct got_error *
990 print_commits(struct got_object_id *root_id, struct got_repository *repo,
991 char *path, int show_patch, int diff_context, int limit,
992 int first_parent_traversal, struct got_reflist_head *refs)
994 const struct got_error *err;
995 struct got_commit_graph *graph;
997 err = got_commit_graph_open(&graph, root_id, path,
998 first_parent_traversal, repo);
1001 err = got_commit_graph_iter_start(graph, root_id, repo);
1005 struct got_commit_object *commit;
1006 struct got_object_id *id;
1008 if (sigint_received || sigpipe_received)
1011 err = got_commit_graph_iter_next(&id, graph);
1013 if (err->code == GOT_ERR_ITER_COMPLETED) {
1017 if (err->code != GOT_ERR_ITER_NEED_MORE)
1019 err = got_commit_graph_fetch_commits(graph, 1, repo);
1028 err = got_object_open_as_commit(&commit, repo, id);
1031 err = print_commit(commit, id, repo, show_patch, diff_context,
1033 got_object_commit_close(commit);
1034 if (err || (limit && --limit == 0))
1038 got_commit_graph_close(graph);
1045 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1046 "[-r repository-path] [path]\n", getprogname());
1050 static const struct got_error *
1051 cmd_log(int argc, char *argv[])
1053 const struct got_error *error;
1054 struct got_repository *repo = NULL;
1055 struct got_worktree *worktree = NULL;
1056 struct got_commit_object *commit = NULL;
1057 struct got_object_id *id = NULL;
1058 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1059 char *start_commit = NULL;
1060 int diff_context = 3, ch;
1061 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1063 struct got_reflist_head refs;
1065 SIMPLEQ_INIT(&refs);
1068 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1074 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1077 first_parent_traversal = 1;
1083 start_commit = optarg;
1086 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1089 err(1, "-C option %s", errstr);
1092 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1094 err(1, "-l option %s", errstr);
1097 repo_path = realpath(optarg, NULL);
1098 if (repo_path == NULL)
1099 err(1, "-r option");
1100 got_path_strip_trailing_slashes(repo_path);
1111 cwd = getcwd(NULL, 0);
1113 error = got_error_from_errno("getcwd");
1117 error = got_worktree_open(&worktree, cwd);
1118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1125 error = got_error_from_errno("strdup");
1128 } else if (argc == 1) {
1130 error = got_worktree_resolve_path(&path, worktree,
1135 path = strdup(argv[0]);
1137 error = got_error_from_errno("strdup");
1144 if (repo_path == NULL) {
1145 repo_path = worktree ?
1146 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1148 if (repo_path == NULL) {
1149 error = got_error_from_errno("strdup");
1153 error = got_repo_open(&repo, repo_path);
1157 error = apply_unveil(got_repo_get_path(repo), 1,
1158 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1162 if (start_commit == NULL) {
1163 struct got_reference *head_ref;
1164 error = got_ref_open(&head_ref, repo,
1165 worktree ? got_worktree_get_head_ref_name(worktree)
1169 error = got_ref_resolve(&id, repo, head_ref);
1170 got_ref_close(head_ref);
1173 error = got_object_open_as_commit(&commit, repo, id);
1175 struct got_reference *ref;
1176 error = got_ref_open(&ref, repo, start_commit, 0);
1177 if (error == NULL) {
1179 error = got_ref_resolve(&id, repo, ref);
1183 error = got_object_get_type(&obj_type, repo, id);
1186 if (obj_type == GOT_OBJ_TYPE_TAG) {
1187 struct got_tag_object *tag;
1188 error = got_object_open_as_tag(&tag, repo, id);
1191 if (got_object_tag_get_object_type(tag) !=
1192 GOT_OBJ_TYPE_COMMIT) {
1193 got_object_tag_close(tag);
1194 error = got_error(GOT_ERR_OBJ_TYPE);
1198 id = got_object_id_dup(
1199 got_object_tag_get_object_id(tag));
1201 error = got_error_from_errno(
1202 "got_object_id_dup");
1203 got_object_tag_close(tag);
1206 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1207 error = got_error(GOT_ERR_OBJ_TYPE);
1210 error = got_object_open_as_commit(&commit, repo, id);
1214 if (commit == NULL) {
1215 error = got_repo_match_object_id_prefix(&id,
1216 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1224 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1229 path = in_repo_path;
1232 error = got_ref_list(&refs, repo);
1236 error = print_commits(id, repo, path, show_patch,
1237 diff_context, limit, first_parent_traversal, &refs);
1244 got_worktree_close(worktree);
1246 const struct got_error *repo_error;
1247 repo_error = got_repo_close(repo);
1251 got_ref_list_free(&refs);
1258 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1259 "[object1 object2 | path]\n", getprogname());
1263 struct print_diff_arg {
1264 struct got_repository *repo;
1265 struct got_worktree *worktree;
1271 static const struct got_error *
1272 print_diff(void *arg, unsigned char status, const char *path,
1273 struct got_object_id *blob_id, struct got_object_id *commit_id)
1275 struct print_diff_arg *a = arg;
1276 const struct got_error *err = NULL;
1277 struct got_blob_object *blob1 = NULL;
1279 char *abspath = NULL;
1282 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1283 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1286 if (!a->header_shown) {
1287 printf("diff %s %s\n", a->id_str,
1288 got_worktree_get_root_path(a->worktree));
1289 a->header_shown = 1;
1292 if (status != GOT_STATUS_ADD) {
1293 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1299 if (status != GOT_STATUS_DELETE) {
1300 if (asprintf(&abspath, "%s/%s",
1301 got_worktree_get_root_path(a->worktree), path) == -1) {
1302 err = got_error_from_errno("asprintf");
1306 f2 = fopen(abspath, "r");
1308 err = got_error_from_errno2("fopen", abspath);
1311 if (lstat(abspath, &sb) == -1) {
1312 err = got_error_from_errno2("lstat", abspath);
1318 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1322 got_object_blob_close(blob1);
1323 if (f2 && fclose(f2) != 0 && err == NULL)
1324 err = got_error_from_errno("fclose");
1329 static const struct got_error *
1330 cmd_diff(int argc, char *argv[])
1332 const struct got_error *error;
1333 struct got_repository *repo = NULL;
1334 struct got_worktree *worktree = NULL;
1335 char *cwd = NULL, *repo_path = NULL;
1336 struct got_object_id *id1 = NULL, *id2 = NULL;
1337 const char *id_str1 = NULL, *id_str2 = NULL;
1338 char *label1 = NULL, *label2 = NULL;
1340 int diff_context = 3, ch;
1345 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1350 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1353 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1355 err(1, "-C option %s", errstr);
1358 repo_path = realpath(optarg, NULL);
1359 if (repo_path == NULL)
1360 err(1, "-r option");
1361 got_path_strip_trailing_slashes(repo_path);
1372 cwd = getcwd(NULL, 0);
1374 error = got_error_from_errno("getcwd");
1377 error = got_worktree_open(&worktree, cwd);
1378 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1381 if (worktree == NULL) {
1382 error = got_error(GOT_ERR_NOT_WORKTREE);
1387 "-r option can't be used when diffing a work tree");
1388 repo_path = strdup(got_worktree_get_repo_path(worktree));
1389 if (repo_path == NULL) {
1390 error = got_error_from_errno("strdup");
1394 error = got_worktree_resolve_path(&path, worktree,
1401 error = got_error_from_errno("strdup");
1405 } else if (argc == 2) {
1408 if (worktree && repo_path == NULL) {
1410 strdup(got_worktree_get_repo_path(worktree));
1411 if (repo_path == NULL) {
1412 error = got_error_from_errno("strdup");
1419 if (repo_path == NULL) {
1420 repo_path = getcwd(NULL, 0);
1421 if (repo_path == NULL)
1422 return got_error_from_errno("getcwd");
1425 error = got_repo_open(&repo, repo_path);
1430 error = apply_unveil(got_repo_get_path(repo), 1,
1431 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1436 struct print_diff_arg arg;
1438 error = got_object_id_str(&id_str,
1439 got_worktree_get_base_commit_id(worktree));
1443 arg.worktree = worktree;
1444 arg.diff_context = diff_context;
1445 arg.id_str = id_str;
1446 arg.header_shown = 0;
1448 error = got_worktree_status(worktree, path, repo, print_diff,
1449 &arg, check_cancelled, NULL);
1454 error = got_repo_match_object_id_prefix(&id1, id_str1,
1455 GOT_OBJ_TYPE_ANY, repo);
1457 struct got_reference *ref;
1458 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1460 error = got_ref_open(&ref, repo, id_str1, 0);
1463 label1 = strdup(got_ref_get_name(ref));
1464 if (label1 == NULL) {
1465 error = got_error_from_errno("strdup");
1468 error = got_ref_resolve(&id1, repo, ref);
1473 label1 = strdup(id_str1);
1474 if (label1 == NULL) {
1475 error = got_error_from_errno("strdup");
1480 error = got_repo_match_object_id_prefix(&id2, id_str2,
1481 GOT_OBJ_TYPE_ANY, repo);
1483 struct got_reference *ref;
1484 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1486 error = got_ref_open(&ref, repo, id_str2, 0);
1489 label2 = strdup(got_ref_get_name(ref));
1490 if (label2 == NULL) {
1491 error = got_error_from_errno("strdup");
1494 error = got_ref_resolve(&id2, repo, ref);
1499 label2 = strdup(id_str2);
1500 if (label2 == NULL) {
1501 error = got_error_from_errno("strdup");
1506 error = got_object_get_type(&type1, repo, id1);
1510 error = got_object_get_type(&type2, repo, id2);
1514 if (type1 != type2) {
1515 error = got_error(GOT_ERR_OBJ_TYPE);
1520 case GOT_OBJ_TYPE_BLOB:
1521 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1522 diff_context, repo, stdout);
1524 case GOT_OBJ_TYPE_TREE:
1525 error = got_diff_objects_as_trees(id1, id2, "", "",
1526 diff_context, repo, stdout);
1528 case GOT_OBJ_TYPE_COMMIT:
1529 printf("diff %s %s\n", label1, label2);
1530 error = got_diff_objects_as_commits(id1, id2, diff_context,
1534 error = got_error(GOT_ERR_OBJ_TYPE);
1544 got_worktree_close(worktree);
1546 const struct got_error *repo_error;
1547 repo_error = got_repo_close(repo);
1558 "usage: %s blame [-c commit] [-r repository-path] path\n",
1563 static const struct got_error *
1564 cmd_blame(int argc, char *argv[])
1566 const struct got_error *error;
1567 struct got_repository *repo = NULL;
1568 struct got_worktree *worktree = NULL;
1569 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1570 struct got_object_id *commit_id = NULL;
1571 char *commit_id_str = NULL;
1575 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1580 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1583 commit_id_str = optarg;
1586 repo_path = realpath(optarg, NULL);
1587 if (repo_path == NULL)
1588 err(1, "-r option");
1589 got_path_strip_trailing_slashes(repo_path);
1605 cwd = getcwd(NULL, 0);
1607 error = got_error_from_errno("getcwd");
1610 if (repo_path == NULL) {
1611 error = got_worktree_open(&worktree, cwd);
1612 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1618 strdup(got_worktree_get_repo_path(worktree));
1619 if (repo_path == NULL)
1620 error = got_error_from_errno("strdup");
1624 repo_path = strdup(cwd);
1625 if (repo_path == NULL) {
1626 error = got_error_from_errno("strdup");
1632 error = got_repo_open(&repo, repo_path);
1636 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1641 const char *prefix = got_worktree_get_path_prefix(worktree);
1642 char *p, *worktree_subdir = cwd +
1643 strlen(got_worktree_get_root_path(worktree));
1644 if (asprintf(&p, "%s%s%s%s%s",
1645 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1646 worktree_subdir, worktree_subdir[0] ? "/" : "",
1648 error = got_error_from_errno("asprintf");
1651 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1654 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1659 if (commit_id_str == NULL) {
1660 struct got_reference *head_ref;
1661 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1664 error = got_ref_resolve(&commit_id, repo, head_ref);
1665 got_ref_close(head_ref);
1669 error = got_repo_match_object_id_prefix(&commit_id,
1670 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1675 error = got_blame(in_repo_path, commit_id, repo, stdout);
1682 got_worktree_close(worktree);
1684 const struct got_error *repo_error;
1685 repo_error = got_repo_close(repo);
1696 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1702 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1703 const char *root_path)
1705 int is_root_path = (strcmp(path, root_path) == 0);
1707 path += strlen(root_path);
1708 while (path[0] == '/')
1711 printf("%s%s%s%s%s\n", id ? id : "", path,
1712 is_root_path ? "" : "/", te->name,
1713 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1716 static const struct got_error *
1717 print_tree(const char *path, struct got_object_id *commit_id,
1718 int show_ids, int recurse, const char *root_path,
1719 struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 struct got_object_id *tree_id = NULL;
1723 struct got_tree_object *tree = NULL;
1724 const struct got_tree_entries *entries;
1725 struct got_tree_entry *te;
1727 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1731 err = got_object_open_as_tree(&tree, repo, tree_id);
1734 entries = got_object_tree_get_entries(tree);
1735 te = SIMPLEQ_FIRST(&entries->head);
1739 if (sigint_received || sigpipe_received)
1744 err = got_object_id_str(&id_str, te->id);
1747 if (asprintf(&id, "%s ", id_str) == -1) {
1748 err = got_error_from_errno("asprintf");
1754 print_entry(te, id, path, root_path);
1757 if (recurse && S_ISDIR(te->mode)) {
1759 if (asprintf(&child_path, "%s%s%s", path,
1760 path[0] == '/' && path[1] == '\0' ? "" : "/",
1762 err = got_error_from_errno("asprintf");
1765 err = print_tree(child_path, commit_id, show_ids, 1,
1772 te = SIMPLEQ_NEXT(te, entry);
1776 got_object_tree_close(tree);
1781 static const struct got_error *
1782 cmd_tree(int argc, char *argv[])
1784 const struct got_error *error;
1785 struct got_repository *repo = NULL;
1786 struct got_worktree *worktree = NULL;
1788 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1789 struct got_object_id *commit_id = NULL;
1790 char *commit_id_str = NULL;
1791 int show_ids = 0, recurse = 0;
1795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1800 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1803 commit_id_str = optarg;
1806 repo_path = realpath(optarg, NULL);
1807 if (repo_path == NULL)
1808 err(1, "-r option");
1809 got_path_strip_trailing_slashes(repo_path);
1833 cwd = getcwd(NULL, 0);
1835 error = got_error_from_errno("getcwd");
1838 if (repo_path == NULL) {
1839 error = got_worktree_open(&worktree, cwd);
1840 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1846 strdup(got_worktree_get_repo_path(worktree));
1847 if (repo_path == NULL)
1848 error = got_error_from_errno("strdup");
1852 repo_path = strdup(cwd);
1853 if (repo_path == NULL) {
1854 error = got_error_from_errno("strdup");
1860 error = got_repo_open(&repo, repo_path);
1864 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1870 char *p, *worktree_subdir = cwd +
1871 strlen(got_worktree_get_root_path(worktree));
1872 if (asprintf(&p, "%s/%s",
1873 got_worktree_get_path_prefix(worktree),
1874 worktree_subdir) == -1) {
1875 error = got_error_from_errno("asprintf");
1878 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1885 if (in_repo_path == NULL) {
1886 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1891 if (commit_id_str == NULL) {
1892 struct got_reference *head_ref;
1893 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1896 error = got_ref_resolve(&commit_id, repo, head_ref);
1897 got_ref_close(head_ref);
1901 error = got_repo_match_object_id_prefix(&commit_id,
1902 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1907 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1908 in_repo_path, repo);
1915 got_worktree_close(worktree);
1917 const struct got_error *repo_error;
1918 repo_error = got_repo_close(repo);
1928 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1932 static const struct got_error *
1933 print_status(void *arg, unsigned char status, const char *path,
1934 struct got_object_id *blob_id, struct got_object_id *commit_id)
1936 printf("%c %s\n", status, path);
1940 static const struct got_error *
1941 cmd_status(int argc, char *argv[])
1943 const struct got_error *error = NULL;
1944 struct got_repository *repo = NULL;
1945 struct got_worktree *worktree = NULL;
1946 char *cwd = NULL, *path = NULL;
1949 while ((ch = getopt(argc, argv, "")) != -1) {
1961 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1965 cwd = getcwd(NULL, 0);
1967 error = got_error_from_errno("getcwd");
1971 error = got_worktree_open(&worktree, cwd);
1978 error = got_error_from_errno("strdup");
1981 } else if (argc == 1) {
1982 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1988 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1992 error = apply_unveil(got_repo_get_path(repo), 1,
1993 got_worktree_get_root_path(worktree), 0);
1997 error = got_worktree_status(worktree, path, repo, print_status, NULL,
1998 check_cancelled, NULL);
2009 "usage: %s ref [-r repository] -l | -d name | name target\n",
2014 static const struct got_error *
2015 list_refs(struct got_repository *repo)
2017 static const struct got_error *err = NULL;
2018 struct got_reflist_head refs;
2019 struct got_reflist_entry *re;
2021 SIMPLEQ_INIT(&refs);
2022 err = got_ref_list(&refs, repo);
2026 SIMPLEQ_FOREACH(re, &refs, entry) {
2028 refstr = got_ref_to_str(re->ref);
2030 return got_error_from_errno("got_ref_to_str");
2031 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2035 got_ref_list_free(&refs);
2039 static const struct got_error *
2040 delete_ref(struct got_repository *repo, const char *refname)
2042 const struct got_error *err = NULL;
2043 struct got_reference *ref;
2045 err = got_ref_open(&ref, repo, refname, 0);
2049 err = got_ref_delete(ref, repo);
2054 static const struct got_error *
2055 add_ref(struct got_repository *repo, const char *refname, const char *target)
2057 const struct got_error *err = NULL;
2058 struct got_object_id *id;
2059 struct got_reference *ref = NULL;
2061 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2064 struct got_reference *target_ref;
2066 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2068 err = got_ref_open(&target_ref, repo, target, 0);
2071 err = got_ref_resolve(&id, repo, target_ref);
2072 got_ref_close(target_ref);
2077 err = got_ref_alloc(&ref, refname, id);
2081 err = got_ref_write(ref, repo);
2089 static const struct got_error *
2090 cmd_ref(int argc, char *argv[])
2092 const struct got_error *error = NULL;
2093 struct got_repository *repo = NULL;
2094 struct got_worktree *worktree = NULL;
2095 char *cwd = NULL, *repo_path = NULL;
2096 int ch, do_list = 0;
2097 const char *delref = NULL;
2099 /* TODO: Add -s option for adding symbolic references. */
2100 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2106 repo_path = realpath(optarg, NULL);
2107 if (repo_path == NULL)
2108 err(1, "-r option");
2109 got_path_strip_trailing_slashes(repo_path);
2120 if (do_list && delref)
2121 errx(1, "-l and -d options are mutually exclusive\n");
2126 if (do_list || delref) {
2129 } else if (argc != 2)
2134 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2138 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2139 "sendfd unveil", NULL) == -1)
2143 cwd = getcwd(NULL, 0);
2145 error = got_error_from_errno("getcwd");
2149 if (repo_path == NULL) {
2150 error = got_worktree_open(&worktree, cwd);
2151 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2157 strdup(got_worktree_get_repo_path(worktree));
2158 if (repo_path == NULL)
2159 error = got_error_from_errno("strdup");
2163 repo_path = strdup(cwd);
2164 if (repo_path == NULL) {
2165 error = got_error_from_errno("strdup");
2171 error = got_repo_open(&repo, repo_path);
2175 error = apply_unveil(got_repo_get_path(repo), do_list,
2176 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2181 error = list_refs(repo);
2183 error = delete_ref(repo, delref);
2185 error = add_ref(repo, argv[0], argv[1]);
2188 got_repo_close(repo);
2190 got_worktree_close(worktree);
2200 "usage: %s branch [-r repository] -l | -d name | "
2201 "name [base-branch]\n", getprogname());
2205 static const struct got_error *
2206 list_branches(struct got_repository *repo)
2208 static const struct got_error *err = NULL;
2209 struct got_reflist_head refs;
2210 struct got_reflist_entry *re;
2212 SIMPLEQ_INIT(&refs);
2213 err = got_ref_list(&refs, repo);
2217 SIMPLEQ_FOREACH(re, &refs, entry) {
2218 const char *refname;
2220 refname = got_ref_get_name(re->ref);
2221 if (strncmp(refname, "refs/heads/", 11) != 0)
2224 refstr = got_ref_to_str(re->ref);
2226 return got_error_from_errno("got_ref_to_str");
2227 printf("%s: %s\n", refname, refstr);
2231 got_ref_list_free(&refs);
2235 static const struct got_error *
2236 delete_branch(struct got_repository *repo, const char *branch_name)
2238 const struct got_error *err = NULL;
2239 struct got_reference *ref;
2242 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2243 return got_error_from_errno("asprintf");
2245 err = got_ref_open(&ref, repo, refname, 0);
2249 err = got_ref_delete(ref, repo);
2256 static const struct got_error *
2257 add_branch(struct got_repository *repo, const char *branch_name,
2258 const char *base_branch)
2260 const struct got_error *err = NULL;
2261 struct got_object_id *id = NULL;
2262 struct got_reference *ref = NULL;
2263 char *base_refname = NULL, *refname = NULL;
2264 struct got_reference *base_ref;
2266 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2267 base_refname = strdup(GOT_REF_HEAD);
2268 if (base_refname == NULL)
2269 return got_error_from_errno("strdup");
2270 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2271 return got_error_from_errno("asprintf");
2273 err = got_ref_open(&base_ref, repo, base_refname, 0);
2276 err = got_ref_resolve(&id, repo, base_ref);
2277 got_ref_close(base_ref);
2281 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2282 err = got_error_from_errno("asprintf");
2286 err = got_ref_open(&ref, repo, refname, 0);
2288 err = got_error(GOT_ERR_BRANCH_EXISTS);
2290 } else if (err->code != GOT_ERR_NOT_REF)
2293 err = got_ref_alloc(&ref, refname, id);
2297 err = got_ref_write(ref, repo);
2307 static const struct got_error *
2308 cmd_branch(int argc, char *argv[])
2310 const struct got_error *error = NULL;
2311 struct got_repository *repo = NULL;
2312 struct got_worktree *worktree = NULL;
2313 char *cwd = NULL, *repo_path = NULL;
2314 int ch, do_list = 0;
2315 const char *delref = NULL;
2317 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2323 repo_path = realpath(optarg, NULL);
2324 if (repo_path == NULL)
2325 err(1, "-r option");
2326 got_path_strip_trailing_slashes(repo_path);
2337 if (do_list && delref)
2338 errx(1, "-l and -d options are mutually exclusive\n");
2343 if (do_list || delref) {
2346 } else if (argc < 1 || argc > 2)
2351 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2355 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2356 "sendfd unveil", NULL) == -1)
2360 cwd = getcwd(NULL, 0);
2362 error = got_error_from_errno("getcwd");
2366 if (repo_path == NULL) {
2367 error = got_worktree_open(&worktree, cwd);
2368 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2374 strdup(got_worktree_get_repo_path(worktree));
2375 if (repo_path == NULL)
2376 error = got_error_from_errno("strdup");
2380 repo_path = strdup(cwd);
2381 if (repo_path == NULL) {
2382 error = got_error_from_errno("strdup");
2388 error = got_repo_open(&repo, repo_path);
2392 error = apply_unveil(got_repo_get_path(repo), do_list,
2393 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2398 error = list_branches(repo);
2400 error = delete_branch(repo, delref);
2402 const char *base_branch;
2404 base_branch = worktree ?
2405 got_worktree_get_head_ref_name(worktree) :
2408 base_branch = argv[1];
2409 error = add_branch(repo, argv[0], base_branch);
2413 got_repo_close(repo);
2415 got_worktree_close(worktree);
2424 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2428 static const struct got_error *
2429 cmd_add(int argc, char *argv[])
2431 const struct got_error *error = NULL;
2432 struct got_repository *repo = NULL;
2433 struct got_worktree *worktree = NULL;
2435 struct got_pathlist_head paths;
2436 struct got_pathlist_entry *pe;
2441 while ((ch = getopt(argc, argv, "")) != -1) {
2455 /* make sure each file exists before doing anything halfway */
2456 for (x = 0; x < argc; x++) {
2457 char *path = realpath(argv[x], NULL);
2459 error = got_error_from_errno2("realpath", argv[x]);
2465 cwd = getcwd(NULL, 0);
2467 error = got_error_from_errno("getcwd");
2471 error = got_worktree_open(&worktree, cwd);
2475 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2479 error = apply_unveil(got_repo_get_path(repo), 1,
2480 got_worktree_get_root_path(worktree), 0);
2484 for (x = 0; x < argc; x++) {
2485 char *path = realpath(argv[x], NULL);
2487 error = got_error_from_errno2("realpath", argv[x]);
2491 got_path_strip_trailing_slashes(path);
2492 error = got_pathlist_insert(&pe, &paths, path, NULL);
2498 error = got_worktree_schedule_add(worktree, &paths, print_status,
2502 got_repo_close(repo);
2504 got_worktree_close(worktree);
2505 TAILQ_FOREACH(pe, &paths, entry)
2506 free((char *)pe->path);
2507 got_pathlist_free(&paths);
2515 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2519 static const struct got_error *
2520 cmd_remove(int argc, char *argv[])
2522 const struct got_error *error = NULL;
2523 struct got_worktree *worktree = NULL;
2524 struct got_repository *repo = NULL;
2526 struct got_pathlist_head paths;
2527 struct got_pathlist_entry *pe;
2528 int ch, i, delete_local_mods = 0;
2532 while ((ch = getopt(argc, argv, "f")) != -1) {
2535 delete_local_mods = 1;
2549 /* make sure each file exists before doing anything halfway */
2550 for (i = 0; i < argc; i++) {
2551 char *path = realpath(argv[i], NULL);
2553 error = got_error_from_errno2("realpath", argv[i]);
2559 cwd = getcwd(NULL, 0);
2561 error = got_error_from_errno("getcwd");
2564 error = got_worktree_open(&worktree, cwd);
2568 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2572 error = apply_unveil(got_repo_get_path(repo), 1,
2573 got_worktree_get_root_path(worktree), 0);
2577 for (i = 0; i < argc; i++) {
2578 char *path = realpath(argv[i], NULL);
2580 error = got_error_from_errno2("realpath", argv[i]);
2584 got_path_strip_trailing_slashes(path);
2585 error = got_pathlist_insert(&pe, &paths, path, NULL);
2591 error = got_worktree_schedule_delete(worktree, &paths,
2592 delete_local_mods, print_status, NULL, repo);
2597 got_repo_close(repo);
2599 got_worktree_close(worktree);
2600 TAILQ_FOREACH(pe, &paths, entry)
2601 free((char *)pe->path);
2602 got_pathlist_free(&paths);
2610 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2615 revert_progress(void *arg, unsigned char status, const char *path)
2617 while (path[0] == '/')
2619 printf("%c %s\n", status, path);
2622 static const struct got_error *
2623 cmd_revert(int argc, char *argv[])
2625 const struct got_error *error = NULL;
2626 struct got_worktree *worktree = NULL;
2627 struct got_repository *repo = NULL;
2628 char *cwd = NULL, *path = NULL;
2629 struct got_pathlist_head paths;
2630 struct got_pathlist_entry *pe;
2635 while ((ch = getopt(argc, argv, "")) != -1) {
2649 for (i = 0; i < argc; i++) {
2650 char *path = realpath(argv[i], NULL);
2652 error = got_error_from_errno2("realpath", argv[i]);
2656 got_path_strip_trailing_slashes(path);
2657 error = got_pathlist_insert(&pe, &paths, path, NULL);
2664 cwd = getcwd(NULL, 0);
2666 error = got_error_from_errno("getcwd");
2669 error = got_worktree_open(&worktree, cwd);
2673 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2677 error = apply_unveil(got_repo_get_path(repo), 1,
2678 got_worktree_get_root_path(worktree), 0);
2682 error = got_worktree_revert(worktree, &paths,
2683 revert_progress, NULL, repo);
2688 got_repo_close(repo);
2690 got_worktree_close(worktree);
2699 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2704 spawn_editor(const char *editor, const char *file)
2707 sig_t sighup, sigint, sigquit;
2710 sighup = signal(SIGHUP, SIG_IGN);
2711 sigint = signal(SIGINT, SIG_IGN);
2712 sigquit = signal(SIGQUIT, SIG_IGN);
2714 switch (pid = fork()) {
2718 execl(editor, editor, file, (char *)NULL);
2722 while (waitpid(pid, &st, 0) == -1)
2727 (void)signal(SIGHUP, sighup);
2728 (void)signal(SIGINT, sigint);
2729 (void)signal(SIGQUIT, sigquit);
2731 if (!WIFEXITED(st)) {
2736 return WEXITSTATUS(st);
2739 struct collect_commit_logmsg_arg {
2740 const char *cmdline_log;
2742 const char *worktree_path;
2743 const char *branch_name;
2744 const char *repo_path;
2749 static const struct got_error *
2750 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2753 char *initial_content = NULL;
2754 struct got_pathlist_entry *pe;
2755 const struct got_error *err = NULL;
2756 char *template = NULL;
2757 struct collect_commit_logmsg_arg *a = arg;
2759 struct stat st, st2;
2762 int fd, content_changed = 0;
2764 /* if a message was specified on the command line, just use it */
2765 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2766 len = strlen(a->cmdline_log) + 1;
2767 *logmsg = malloc(len + 1);
2768 if (*logmsg == NULL)
2769 return got_error_from_errno("malloc");
2770 strlcpy(*logmsg, a->cmdline_log, len);
2774 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2775 return got_error_from_errno("asprintf");
2777 if (asprintf(&initial_content,
2778 "\n# changes to be committed on branch %s:\n",
2779 a->branch_name) == -1)
2780 return got_error_from_errno("asprintf");
2782 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2786 dprintf(fd, initial_content);
2788 TAILQ_FOREACH(pe, commitable_paths, entry) {
2789 struct got_commitable *ct = pe->data;
2790 dprintf(fd, "# %c %s\n",
2791 got_commitable_get_status(ct),
2792 got_commitable_get_path(ct));
2796 if (stat(a->logmsg_path, &st) == -1) {
2797 err = got_error_from_errno2("stat", a->logmsg_path);
2801 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2802 err = got_error_from_errno("failed spawning editor");
2806 if (stat(a->logmsg_path, &st2) == -1) {
2807 err = got_error_from_errno("stat");
2811 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2812 unlink(a->logmsg_path);
2813 free(a->logmsg_path);
2814 a->logmsg_path = NULL;
2815 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2816 "no changes made to commit message, aborting");
2820 *logmsg = malloc(st2.st_size + 1);
2821 if (*logmsg == NULL) {
2822 err = got_error_from_errno("malloc");
2825 (*logmsg)[0] = '\0';
2828 fp = fopen(a->logmsg_path, "r");
2830 err = got_error_from_errno("fopen");
2833 while (fgets(buf, sizeof(buf), fp) != NULL) {
2834 if (!content_changed && strcmp(buf, initial_content) != 0)
2835 content_changed = 1;
2836 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2837 continue; /* remove comments and leading empty lines */
2838 len = strlcat(*logmsg, buf, st2.st_size);
2842 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2843 (*logmsg)[len - 1] = '\0';
2847 if (len == 0 || !content_changed) {
2848 unlink(a->logmsg_path);
2849 free(a->logmsg_path);
2850 a->logmsg_path = NULL;
2851 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2852 "commit message cannot be empty, aborting");
2856 free(initial_content);
2859 /* Editor is done; we can now apply unveil(2) */
2861 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2865 static const struct got_error *
2866 cmd_commit(int argc, char *argv[])
2868 const struct got_error *error = NULL;
2869 struct got_worktree *worktree = NULL;
2870 struct got_repository *repo = NULL;
2871 char *cwd = NULL, *path = NULL, *id_str = NULL;
2872 struct got_object_id *id = NULL;
2873 const char *logmsg = NULL;
2874 const char *got_author = getenv("GOT_AUTHOR");
2875 struct collect_commit_logmsg_arg cl_arg;
2876 char *editor = NULL;
2879 while ((ch = getopt(argc, argv, "m:")) != -1) {
2894 path = realpath(argv[0], NULL);
2896 error = got_error_from_errno2("realpath", argv[0]);
2899 got_path_strip_trailing_slashes(path);
2900 } else if (argc != 0)
2903 if (got_author == NULL) {
2904 /* TODO: Look current user up in password database */
2905 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2909 cwd = getcwd(NULL, 0);
2911 error = got_error_from_errno("getcwd");
2914 error = got_worktree_open(&worktree, cwd);
2918 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2923 * unveil(2) traverses exec(2); if an editor is used we have
2924 * to apply unveil after the log message has been written.
2926 if (logmsg == NULL || strlen(logmsg) == 0)
2927 error = get_editor(&editor);
2929 error = apply_unveil(got_repo_get_path(repo), 0,
2930 got_worktree_get_root_path(worktree), 0);
2934 cl_arg.editor = editor;
2935 cl_arg.cmdline_log = logmsg;
2936 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2937 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2938 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2939 cl_arg.branch_name += 5;
2940 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2941 cl_arg.branch_name += 6;
2942 cl_arg.repo_path = got_repo_get_path(repo);
2943 cl_arg.logmsg_path = NULL;
2944 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2945 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2947 if (cl_arg.logmsg_path)
2948 fprintf(stderr, "%s: log message preserved in %s\n",
2949 getprogname(), cl_arg.logmsg_path);
2953 if (cl_arg.logmsg_path)
2954 unlink(cl_arg.logmsg_path);
2956 error = got_object_id_str(&id_str, id);
2959 printf("Created commit %s\n", id_str);
2962 got_repo_close(repo);
2964 got_worktree_close(worktree);
2973 usage_cherrypick(void)
2975 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2979 static const struct got_error *
2980 cmd_cherrypick(int argc, char *argv[])
2982 const struct got_error *error = NULL;
2983 struct got_worktree *worktree = NULL;
2984 struct got_repository *repo = NULL;
2985 char *cwd = NULL, *commit_id_str = NULL;
2986 struct got_object_id *commit_id = NULL;
2987 struct got_commit_object *commit = NULL;
2988 struct got_object_qid *pid;
2989 struct got_reference *head_ref = NULL;
2990 int ch, did_something = 0;
2992 while ((ch = getopt(argc, argv, "")) != -1) {
3006 cwd = getcwd(NULL, 0);
3008 error = got_error_from_errno("getcwd");
3011 error = got_worktree_open(&worktree, cwd);
3015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3019 error = apply_unveil(got_repo_get_path(repo), 0,
3020 got_worktree_get_root_path(worktree), 0);
3024 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3025 GOT_OBJ_TYPE_COMMIT, repo);
3026 if (error != NULL) {
3027 struct got_reference *ref;
3028 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3030 error = got_ref_open(&ref, repo, argv[0], 0);
3033 error = got_ref_resolve(&commit_id, repo, ref);
3038 error = got_object_id_str(&commit_id_str, commit_id);
3042 error = got_ref_open(&head_ref, repo,
3043 got_worktree_get_head_ref_name(worktree), 0);
3047 error = check_same_branch(commit_id, head_ref, repo);
3049 if (error->code != GOT_ERR_ANCESTRY)
3053 error = got_error(GOT_ERR_SAME_BRANCH);
3057 error = got_object_open_as_commit(&commit, repo, commit_id);
3060 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3061 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3062 commit_id, repo, update_progress, &did_something, check_cancelled,
3068 printf("Merged commit %s\n", commit_id_str);
3071 got_object_commit_close(commit);
3072 free(commit_id_str);
3074 got_ref_close(head_ref);
3076 got_worktree_close(worktree);
3078 got_repo_close(repo);
3085 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3089 static const struct got_error *
3090 cmd_backout(int argc, char *argv[])
3092 const struct got_error *error = NULL;
3093 struct got_worktree *worktree = NULL;
3094 struct got_repository *repo = NULL;
3095 char *cwd = NULL, *commit_id_str = NULL;
3096 struct got_object_id *commit_id = NULL;
3097 struct got_commit_object *commit = NULL;
3098 struct got_object_qid *pid;
3099 struct got_reference *head_ref = NULL;
3100 int ch, did_something = 0;
3102 while ((ch = getopt(argc, argv, "")) != -1) {
3116 cwd = getcwd(NULL, 0);
3118 error = got_error_from_errno("getcwd");
3121 error = got_worktree_open(&worktree, cwd);
3125 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3129 error = apply_unveil(got_repo_get_path(repo), 0,
3130 got_worktree_get_root_path(worktree), 0);
3134 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3135 GOT_OBJ_TYPE_COMMIT, repo);
3136 if (error != NULL) {
3137 struct got_reference *ref;
3138 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3140 error = got_ref_open(&ref, repo, argv[0], 0);
3143 error = got_ref_resolve(&commit_id, repo, ref);
3148 error = got_object_id_str(&commit_id_str, commit_id);
3152 error = got_ref_open(&head_ref, repo,
3153 got_worktree_get_head_ref_name(worktree), 0);
3157 error = check_same_branch(commit_id, head_ref, repo);
3161 error = got_object_open_as_commit(&commit, repo, commit_id);
3164 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3166 error = got_error(GOT_ERR_ROOT_COMMIT);
3170 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3171 update_progress, &did_something, check_cancelled, NULL);
3176 printf("Backed out commit %s\n", commit_id_str);
3179 got_object_commit_close(commit);
3180 free(commit_id_str);
3182 got_ref_close(head_ref);
3184 got_worktree_close(worktree);
3186 got_repo_close(repo);
3193 fprintf(stderr, "usage: %s rebase [-a] [-c] | branch\n", getprogname());
3197 static const struct got_error *
3198 show_rebase_progress(struct got_commit_object *commit,
3199 struct got_object_id *old_id, struct got_object_id *new_id)
3201 const struct got_error *err;
3202 char *old_id_str = NULL, *new_id_str = NULL;
3203 char *logmsg0 = NULL, *logmsg, *nl;
3206 err = got_object_id_str(&old_id_str, old_id);
3211 err = got_object_id_str(&new_id_str, new_id);
3216 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3217 if (logmsg0 == NULL) {
3218 err = got_error_from_errno("strdup");
3223 while (isspace(logmsg[0]))
3226 old_id_str[12] = '\0';
3228 new_id_str[12] = '\0';
3229 len = strlen(logmsg);
3233 nl = strchr(logmsg, '\n');
3236 printf("%s -> %s: %s\n", old_id_str,
3237 new_id_str ? new_id_str : "no-op change", logmsg);
3246 rebase_progress(void *arg, unsigned char status, const char *path)
3248 unsigned char *rebase_status = arg;
3250 while (path[0] == '/')
3252 printf("%c %s\n", status, path);
3254 if (*rebase_status == GOT_STATUS_CONFLICT)
3256 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3257 *rebase_status = status;
3260 static const struct got_error *
3261 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3262 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3263 struct got_repository *repo)
3265 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3266 return got_worktree_rebase_complete(worktree,
3267 new_base_branch, tmp_branch, branch, repo);
3270 static const struct got_error *
3271 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3272 struct got_object_id *commit_id, struct got_repository *repo)
3274 const struct got_error *error;
3275 struct got_commit_object *commit;
3276 struct got_object_id *new_commit_id;
3278 error = got_object_open_as_commit(&commit, repo, commit_id);
3282 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3283 tmp_branch, commit, commit_id, repo);
3285 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3287 error = show_rebase_progress(commit, commit_id, NULL);
3289 error = show_rebase_progress(commit, commit_id, new_commit_id);
3290 free(new_commit_id);
3293 got_object_commit_close(commit);
3297 static const struct got_error *
3298 cmd_rebase(int argc, char *argv[])
3300 const struct got_error *error = NULL;
3301 struct got_worktree *worktree = NULL;
3302 struct got_repository *repo = NULL;
3304 struct got_reference *branch = NULL;
3305 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3306 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3307 struct got_object_id *resume_commit_id = NULL;
3308 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3309 struct got_commit_graph *graph = NULL;
3310 struct got_commit_object *commit = NULL;
3311 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3312 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3313 struct got_object_id_queue commits;
3314 const struct got_object_id_queue *parent_ids;
3315 struct got_object_qid *qid, *pid;
3317 SIMPLEQ_INIT(&commits);
3319 while ((ch = getopt(argc, argv, "ac")) != -1) {
3325 continue_rebase = 1;
3336 if (abort_rebase && continue_rebase)
3338 else if (abort_rebase || continue_rebase) {
3341 } else if (argc != 1)
3344 cwd = getcwd(NULL, 0);
3346 error = got_error_from_errno("getcwd");
3349 error = got_worktree_open(&worktree, cwd);
3354 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3358 error = apply_unveil(got_repo_get_path(repo), 0,
3359 got_worktree_get_root_path(worktree), 0);
3363 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3367 if (rebase_in_progress && abort_rebase) {
3369 error = got_worktree_rebase_continue(&resume_commit_id,
3370 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3373 printf("Switching work tree to %s\n",
3374 got_ref_get_symref_target(new_base_branch));
3375 error = got_worktree_rebase_abort(worktree, repo,
3376 new_base_branch, update_progress, &did_something);
3379 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3380 goto done; /* nothing else to do */
3381 } else if (abort_rebase) {
3382 error = got_error(GOT_ERR_NOT_REBASING);
3386 if (continue_rebase) {
3387 error = got_worktree_rebase_continue(&resume_commit_id,
3388 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3392 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3397 yca_id = got_object_id_dup(resume_commit_id);
3398 if (yca_id == NULL) {
3399 error = got_error_from_errno("got_object_id_dup");
3403 error = got_ref_open(&branch, repo, argv[0], 0);
3407 error = check_same_branch(
3408 got_worktree_get_base_commit_id(worktree), branch, repo);
3410 if (error->code != GOT_ERR_ANCESTRY)
3414 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3415 "specified branch resolves to a commit which "
3416 "is already contained in work tree's branch");
3421 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3425 if (!continue_rebase) {
3426 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3427 got_worktree_get_base_commit_id(worktree),
3428 branch_head_commit_id, repo);
3431 if (yca_id == NULL) {
3432 error = got_error_msg(GOT_ERR_ANCESTRY,
3433 "specified branch shares no common ancestry "
3434 "with work tree's branch");
3438 error = got_worktree_rebase_prepare(&new_base_branch,
3439 &tmp_branch, worktree, branch, repo);
3444 commit_id = branch_head_commit_id;
3445 error = got_object_open_as_commit(&commit, repo, commit_id);
3449 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3452 parent_ids = got_object_commit_get_parent_ids(commit);
3453 pid = SIMPLEQ_FIRST(parent_ids);
3454 error = got_commit_graph_iter_start(graph, pid->id, repo);
3455 got_object_commit_close(commit);
3459 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3460 error = got_commit_graph_iter_next(&parent_id, graph);
3462 if (error->code == GOT_ERR_ITER_COMPLETED) {
3463 error = got_error_msg(GOT_ERR_ANCESTRY,
3464 "ran out of commits to rebase before "
3465 "youngest common ancestor commit has "
3468 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3470 error = got_commit_graph_fetch_commits(graph, 1, repo);
3474 error = got_object_qid_alloc(&qid, commit_id);
3477 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3478 commit_id = parent_id;
3482 if (SIMPLEQ_EMPTY(&commits)) {
3483 if (continue_rebase)
3484 error = rebase_complete(worktree, branch,
3485 new_base_branch, tmp_branch, repo);
3487 error = got_error(GOT_ERR_EMPTY_REBASE);
3492 SIMPLEQ_FOREACH(qid, &commits, entry) {
3493 commit_id = qid->id;
3494 parent_id = pid ? pid->id : yca_id;
3497 error = got_worktree_rebase_merge_files(worktree, parent_id,
3498 commit_id, repo, rebase_progress, &rebase_status,
3499 check_cancelled, NULL);
3503 if (rebase_status == GOT_STATUS_CONFLICT)
3506 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3511 if (rebase_status == GOT_STATUS_CONFLICT) {
3512 error = got_worktree_rebase_postpone(worktree);
3515 error = got_error_msg(GOT_ERR_CONFLICTS,
3516 "conflicts must be resolved before rebase can be resumed");
3518 error = rebase_complete(worktree, branch, new_base_branch,
3521 got_object_id_queue_free(&commits);
3522 free(branch_head_commit_id);
3523 free(resume_commit_id);
3526 got_commit_graph_close(graph);
3528 got_object_commit_close(commit);
3530 got_ref_close(branch);
3531 if (new_base_branch)
3532 got_ref_close(new_base_branch);
3534 got_ref_close(tmp_branch);
3536 got_worktree_close(worktree);
3538 got_repo_close(repo);