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, rebase_in_progress;
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);
732 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
735 if (rebase_in_progress) {
736 error = got_error(GOT_ERR_REBASING);
743 error = got_error_from_errno("strdup");
746 } else if (argc == 1) {
747 error = got_worktree_resolve_path(&path, worktree, argv[0]);
753 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
757 error = apply_unveil(got_repo_get_path(repo), 0,
758 got_worktree_get_root_path(worktree), 0);
762 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
763 got_worktree_get_head_ref_name(worktree), 0);
766 if (commit_id_str == NULL) {
767 error = got_ref_resolve(&commit_id, repo, head_ref);
770 error = got_object_id_str(&commit_id_str, commit_id);
774 error = got_repo_match_object_id_prefix(&commit_id,
775 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
781 struct got_object_id *head_commit_id;
782 if (strlen(path) != 0) {
783 fprintf(stderr, "%s: switching between branches "
784 "requires that the entire work tree "
785 "gets updated, not just '%s'\n",
786 getprogname(), path);
787 error = got_error(GOT_ERR_BAD_PATH);
790 error = got_ref_resolve(&head_commit_id, repo, head_ref);
793 error = check_linear_ancestry(commit_id, head_commit_id, repo);
794 free(head_commit_id);
797 error = check_same_branch(commit_id, head_ref, repo);
800 error = switch_head_ref(head_ref, commit_id, worktree, repo);
804 error = check_linear_ancestry(commit_id,
805 got_worktree_get_base_commit_id(worktree), repo);
807 if (error->code == GOT_ERR_ANCESTRY)
808 error = got_error(GOT_ERR_BRANCH_MOVED);
811 error = check_same_branch(commit_id, head_ref, repo);
816 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
818 error = got_worktree_set_base_commit_id(worktree, repo,
824 error = got_worktree_checkout_files(worktree, path, repo,
825 update_progress, &did_something, check_cancelled, NULL);
830 printf("Updated to commit %s\n", commit_id_str);
832 printf("Already up-to-date\n");
841 static const struct got_error *
842 print_patch(struct got_commit_object *commit, struct got_object_id *id,
843 int diff_context, struct got_repository *repo)
845 const struct got_error *err = NULL;
846 struct got_tree_object *tree1 = NULL, *tree2;
847 struct got_object_qid *qid;
848 char *id_str1 = NULL, *id_str2;
849 struct got_diff_blob_output_unidiff_arg arg;
851 err = got_object_open_as_tree(&tree2, repo,
852 got_object_commit_get_tree_id(commit));
856 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
858 struct got_commit_object *pcommit;
860 err = got_object_open_as_commit(&pcommit, repo, qid->id);
864 err = got_object_open_as_tree(&tree1, repo,
865 got_object_commit_get_tree_id(pcommit));
866 got_object_commit_close(pcommit);
870 err = got_object_id_str(&id_str1, qid->id);
875 err = got_object_id_str(&id_str2, id);
879 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
880 arg.diff_context = diff_context;
881 arg.outfile = stdout;
882 err = got_diff_tree(tree1, tree2, "", "", repo,
883 got_diff_blob_output_unidiff, &arg);
886 got_object_tree_close(tree1);
887 got_object_tree_close(tree2);
894 get_datestr(time_t *time, char *datebuf)
896 char *p, *s = ctime_r(time, datebuf);
903 static const struct got_error *
904 print_commit(struct got_commit_object *commit, struct got_object_id *id,
905 struct got_repository *repo, int show_patch, int diff_context,
906 struct got_reflist_head *refs)
908 const struct got_error *err = NULL;
909 char *id_str, *datestr, *logmsg0, *logmsg, *line;
911 time_t committer_time;
912 const char *author, *committer;
913 char *refs_str = NULL;
914 struct got_reflist_entry *re;
916 SIMPLEQ_FOREACH(re, refs, entry) {
919 if (got_object_id_cmp(re->id, id) != 0)
921 name = got_ref_get_name(re->ref);
922 if (strcmp(name, GOT_REF_HEAD) == 0)
924 if (strncmp(name, "refs/", 5) == 0)
926 if (strncmp(name, "got/", 4) == 0)
928 if (strncmp(name, "heads/", 6) == 0)
930 if (strncmp(name, "remotes/", 8) == 0)
933 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
935 err = got_error_from_errno("asprintf");
941 err = got_object_id_str(&id_str, id);
945 printf("-----------------------------------------------\n");
946 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
947 refs_str ? refs_str : "", refs_str ? ")" : "");
952 printf("from: %s\n", got_object_commit_get_author(commit));
953 committer_time = got_object_commit_get_committer_time(commit);
954 datestr = get_datestr(&committer_time, datebuf);
955 printf("date: %s UTC\n", datestr);
956 author = got_object_commit_get_author(commit);
957 committer = got_object_commit_get_committer(commit);
958 if (strcmp(author, committer) != 0)
959 printf("via: %s\n", committer);
960 if (got_object_commit_get_nparents(commit) > 1) {
961 const struct got_object_id_queue *parent_ids;
962 struct got_object_qid *qid;
964 parent_ids = got_object_commit_get_parent_ids(commit);
965 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
966 err = got_object_id_str(&id_str, qid->id);
969 printf("parent %d: %s\n", n++, id_str);
974 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
976 return got_error_from_errno("strdup");
980 line = strsep(&logmsg, "\n");
982 printf(" %s\n", line);
987 err = print_patch(commit, id, diff_context, repo);
992 if (fflush(stdout) != 0 && err == NULL)
993 err = got_error_from_errno("fflush");
997 static const struct got_error *
998 print_commits(struct got_object_id *root_id, struct got_repository *repo,
999 char *path, int show_patch, int diff_context, int limit,
1000 int first_parent_traversal, struct got_reflist_head *refs)
1002 const struct got_error *err;
1003 struct got_commit_graph *graph;
1005 err = got_commit_graph_open(&graph, root_id, path,
1006 first_parent_traversal, repo);
1009 err = got_commit_graph_iter_start(graph, root_id, repo);
1013 struct got_commit_object *commit;
1014 struct got_object_id *id;
1016 if (sigint_received || sigpipe_received)
1019 err = got_commit_graph_iter_next(&id, graph);
1021 if (err->code == GOT_ERR_ITER_COMPLETED) {
1025 if (err->code != GOT_ERR_ITER_NEED_MORE)
1027 err = got_commit_graph_fetch_commits(graph, 1, repo);
1036 err = got_object_open_as_commit(&commit, repo, id);
1039 err = print_commit(commit, id, repo, show_patch, diff_context,
1041 got_object_commit_close(commit);
1042 if (err || (limit && --limit == 0))
1046 got_commit_graph_close(graph);
1053 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1054 "[-r repository-path] [path]\n", getprogname());
1058 static const struct got_error *
1059 cmd_log(int argc, char *argv[])
1061 const struct got_error *error;
1062 struct got_repository *repo = NULL;
1063 struct got_worktree *worktree = NULL;
1064 struct got_commit_object *commit = NULL;
1065 struct got_object_id *id = NULL;
1066 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1067 char *start_commit = NULL;
1068 int diff_context = 3, ch;
1069 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1071 struct got_reflist_head refs;
1073 SIMPLEQ_INIT(&refs);
1076 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1082 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1085 first_parent_traversal = 1;
1091 start_commit = optarg;
1094 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1097 err(1, "-C option %s", errstr);
1100 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1102 err(1, "-l option %s", errstr);
1105 repo_path = realpath(optarg, NULL);
1106 if (repo_path == NULL)
1107 err(1, "-r option");
1108 got_path_strip_trailing_slashes(repo_path);
1119 cwd = getcwd(NULL, 0);
1121 error = got_error_from_errno("getcwd");
1125 error = got_worktree_open(&worktree, cwd);
1126 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1133 error = got_error_from_errno("strdup");
1136 } else if (argc == 1) {
1138 error = got_worktree_resolve_path(&path, worktree,
1143 path = strdup(argv[0]);
1145 error = got_error_from_errno("strdup");
1152 if (repo_path == NULL) {
1153 repo_path = worktree ?
1154 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1156 if (repo_path == NULL) {
1157 error = got_error_from_errno("strdup");
1161 error = got_repo_open(&repo, repo_path);
1165 error = apply_unveil(got_repo_get_path(repo), 1,
1166 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1170 if (start_commit == NULL) {
1171 struct got_reference *head_ref;
1172 error = got_ref_open(&head_ref, repo,
1173 worktree ? got_worktree_get_head_ref_name(worktree)
1177 error = got_ref_resolve(&id, repo, head_ref);
1178 got_ref_close(head_ref);
1181 error = got_object_open_as_commit(&commit, repo, id);
1183 struct got_reference *ref;
1184 error = got_ref_open(&ref, repo, start_commit, 0);
1185 if (error == NULL) {
1187 error = got_ref_resolve(&id, repo, ref);
1191 error = got_object_get_type(&obj_type, repo, id);
1194 if (obj_type == GOT_OBJ_TYPE_TAG) {
1195 struct got_tag_object *tag;
1196 error = got_object_open_as_tag(&tag, repo, id);
1199 if (got_object_tag_get_object_type(tag) !=
1200 GOT_OBJ_TYPE_COMMIT) {
1201 got_object_tag_close(tag);
1202 error = got_error(GOT_ERR_OBJ_TYPE);
1206 id = got_object_id_dup(
1207 got_object_tag_get_object_id(tag));
1209 error = got_error_from_errno(
1210 "got_object_id_dup");
1211 got_object_tag_close(tag);
1214 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1215 error = got_error(GOT_ERR_OBJ_TYPE);
1218 error = got_object_open_as_commit(&commit, repo, id);
1222 if (commit == NULL) {
1223 error = got_repo_match_object_id_prefix(&id,
1224 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1232 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1237 path = in_repo_path;
1240 error = got_ref_list(&refs, repo);
1244 error = print_commits(id, repo, path, show_patch,
1245 diff_context, limit, first_parent_traversal, &refs);
1252 got_worktree_close(worktree);
1254 const struct got_error *repo_error;
1255 repo_error = got_repo_close(repo);
1259 got_ref_list_free(&refs);
1266 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1267 "[object1 object2 | path]\n", getprogname());
1271 struct print_diff_arg {
1272 struct got_repository *repo;
1273 struct got_worktree *worktree;
1279 static const struct got_error *
1280 print_diff(void *arg, unsigned char status, const char *path,
1281 struct got_object_id *blob_id, struct got_object_id *commit_id)
1283 struct print_diff_arg *a = arg;
1284 const struct got_error *err = NULL;
1285 struct got_blob_object *blob1 = NULL;
1287 char *abspath = NULL;
1290 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1291 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1294 if (!a->header_shown) {
1295 printf("diff %s %s\n", a->id_str,
1296 got_worktree_get_root_path(a->worktree));
1297 a->header_shown = 1;
1300 if (status != GOT_STATUS_ADD) {
1301 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1307 if (status != GOT_STATUS_DELETE) {
1308 if (asprintf(&abspath, "%s/%s",
1309 got_worktree_get_root_path(a->worktree), path) == -1) {
1310 err = got_error_from_errno("asprintf");
1314 f2 = fopen(abspath, "r");
1316 err = got_error_from_errno2("fopen", abspath);
1319 if (lstat(abspath, &sb) == -1) {
1320 err = got_error_from_errno2("lstat", abspath);
1326 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1330 got_object_blob_close(blob1);
1331 if (f2 && fclose(f2) != 0 && err == NULL)
1332 err = got_error_from_errno("fclose");
1337 static const struct got_error *
1338 cmd_diff(int argc, char *argv[])
1340 const struct got_error *error;
1341 struct got_repository *repo = NULL;
1342 struct got_worktree *worktree = NULL;
1343 char *cwd = NULL, *repo_path = NULL;
1344 struct got_object_id *id1 = NULL, *id2 = NULL;
1345 const char *id_str1 = NULL, *id_str2 = NULL;
1346 char *label1 = NULL, *label2 = NULL;
1348 int diff_context = 3, ch;
1353 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1358 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1361 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1363 err(1, "-C option %s", errstr);
1366 repo_path = realpath(optarg, NULL);
1367 if (repo_path == NULL)
1368 err(1, "-r option");
1369 got_path_strip_trailing_slashes(repo_path);
1380 cwd = getcwd(NULL, 0);
1382 error = got_error_from_errno("getcwd");
1385 error = got_worktree_open(&worktree, cwd);
1386 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1389 if (worktree == NULL) {
1390 error = got_error(GOT_ERR_NOT_WORKTREE);
1395 "-r option can't be used when diffing a work tree");
1396 repo_path = strdup(got_worktree_get_repo_path(worktree));
1397 if (repo_path == NULL) {
1398 error = got_error_from_errno("strdup");
1402 error = got_worktree_resolve_path(&path, worktree,
1409 error = got_error_from_errno("strdup");
1413 } else if (argc == 2) {
1416 if (worktree && repo_path == NULL) {
1418 strdup(got_worktree_get_repo_path(worktree));
1419 if (repo_path == NULL) {
1420 error = got_error_from_errno("strdup");
1427 if (repo_path == NULL) {
1428 repo_path = getcwd(NULL, 0);
1429 if (repo_path == NULL)
1430 return got_error_from_errno("getcwd");
1433 error = got_repo_open(&repo, repo_path);
1438 error = apply_unveil(got_repo_get_path(repo), 1,
1439 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1444 struct print_diff_arg arg;
1446 error = got_object_id_str(&id_str,
1447 got_worktree_get_base_commit_id(worktree));
1451 arg.worktree = worktree;
1452 arg.diff_context = diff_context;
1453 arg.id_str = id_str;
1454 arg.header_shown = 0;
1456 error = got_worktree_status(worktree, path, repo, print_diff,
1457 &arg, check_cancelled, NULL);
1462 error = got_repo_match_object_id_prefix(&id1, id_str1,
1463 GOT_OBJ_TYPE_ANY, repo);
1465 struct got_reference *ref;
1466 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1468 error = got_ref_open(&ref, repo, id_str1, 0);
1471 label1 = strdup(got_ref_get_name(ref));
1472 if (label1 == NULL) {
1473 error = got_error_from_errno("strdup");
1476 error = got_ref_resolve(&id1, repo, ref);
1481 label1 = strdup(id_str1);
1482 if (label1 == NULL) {
1483 error = got_error_from_errno("strdup");
1488 error = got_repo_match_object_id_prefix(&id2, id_str2,
1489 GOT_OBJ_TYPE_ANY, repo);
1491 struct got_reference *ref;
1492 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1494 error = got_ref_open(&ref, repo, id_str2, 0);
1497 label2 = strdup(got_ref_get_name(ref));
1498 if (label2 == NULL) {
1499 error = got_error_from_errno("strdup");
1502 error = got_ref_resolve(&id2, repo, ref);
1507 label2 = strdup(id_str2);
1508 if (label2 == NULL) {
1509 error = got_error_from_errno("strdup");
1514 error = got_object_get_type(&type1, repo, id1);
1518 error = got_object_get_type(&type2, repo, id2);
1522 if (type1 != type2) {
1523 error = got_error(GOT_ERR_OBJ_TYPE);
1528 case GOT_OBJ_TYPE_BLOB:
1529 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1530 diff_context, repo, stdout);
1532 case GOT_OBJ_TYPE_TREE:
1533 error = got_diff_objects_as_trees(id1, id2, "", "",
1534 diff_context, repo, stdout);
1536 case GOT_OBJ_TYPE_COMMIT:
1537 printf("diff %s %s\n", label1, label2);
1538 error = got_diff_objects_as_commits(id1, id2, diff_context,
1542 error = got_error(GOT_ERR_OBJ_TYPE);
1552 got_worktree_close(worktree);
1554 const struct got_error *repo_error;
1555 repo_error = got_repo_close(repo);
1566 "usage: %s blame [-c commit] [-r repository-path] path\n",
1571 static const struct got_error *
1572 cmd_blame(int argc, char *argv[])
1574 const struct got_error *error;
1575 struct got_repository *repo = NULL;
1576 struct got_worktree *worktree = NULL;
1577 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1578 struct got_object_id *commit_id = NULL;
1579 char *commit_id_str = NULL;
1583 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1588 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1591 commit_id_str = optarg;
1594 repo_path = realpath(optarg, NULL);
1595 if (repo_path == NULL)
1596 err(1, "-r option");
1597 got_path_strip_trailing_slashes(repo_path);
1613 cwd = getcwd(NULL, 0);
1615 error = got_error_from_errno("getcwd");
1618 if (repo_path == NULL) {
1619 error = got_worktree_open(&worktree, cwd);
1620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1626 strdup(got_worktree_get_repo_path(worktree));
1627 if (repo_path == NULL)
1628 error = got_error_from_errno("strdup");
1632 repo_path = strdup(cwd);
1633 if (repo_path == NULL) {
1634 error = got_error_from_errno("strdup");
1640 error = got_repo_open(&repo, repo_path);
1644 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1649 const char *prefix = got_worktree_get_path_prefix(worktree);
1650 char *p, *worktree_subdir = cwd +
1651 strlen(got_worktree_get_root_path(worktree));
1652 if (asprintf(&p, "%s%s%s%s%s",
1653 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1654 worktree_subdir, worktree_subdir[0] ? "/" : "",
1656 error = got_error_from_errno("asprintf");
1659 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1662 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1667 if (commit_id_str == NULL) {
1668 struct got_reference *head_ref;
1669 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1672 error = got_ref_resolve(&commit_id, repo, head_ref);
1673 got_ref_close(head_ref);
1677 error = got_repo_match_object_id_prefix(&commit_id,
1678 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1683 error = got_blame(in_repo_path, commit_id, repo, stdout);
1690 got_worktree_close(worktree);
1692 const struct got_error *repo_error;
1693 repo_error = got_repo_close(repo);
1704 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1710 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1711 const char *root_path)
1713 int is_root_path = (strcmp(path, root_path) == 0);
1715 path += strlen(root_path);
1716 while (path[0] == '/')
1719 printf("%s%s%s%s%s\n", id ? id : "", path,
1720 is_root_path ? "" : "/", te->name,
1721 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1724 static const struct got_error *
1725 print_tree(const char *path, struct got_object_id *commit_id,
1726 int show_ids, int recurse, const char *root_path,
1727 struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_object_id *tree_id = NULL;
1731 struct got_tree_object *tree = NULL;
1732 const struct got_tree_entries *entries;
1733 struct got_tree_entry *te;
1735 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1739 err = got_object_open_as_tree(&tree, repo, tree_id);
1742 entries = got_object_tree_get_entries(tree);
1743 te = SIMPLEQ_FIRST(&entries->head);
1747 if (sigint_received || sigpipe_received)
1752 err = got_object_id_str(&id_str, te->id);
1755 if (asprintf(&id, "%s ", id_str) == -1) {
1756 err = got_error_from_errno("asprintf");
1762 print_entry(te, id, path, root_path);
1765 if (recurse && S_ISDIR(te->mode)) {
1767 if (asprintf(&child_path, "%s%s%s", path,
1768 path[0] == '/' && path[1] == '\0' ? "" : "/",
1770 err = got_error_from_errno("asprintf");
1773 err = print_tree(child_path, commit_id, show_ids, 1,
1780 te = SIMPLEQ_NEXT(te, entry);
1784 got_object_tree_close(tree);
1789 static const struct got_error *
1790 cmd_tree(int argc, char *argv[])
1792 const struct got_error *error;
1793 struct got_repository *repo = NULL;
1794 struct got_worktree *worktree = NULL;
1796 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1797 struct got_object_id *commit_id = NULL;
1798 char *commit_id_str = NULL;
1799 int show_ids = 0, recurse = 0;
1803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1808 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1811 commit_id_str = optarg;
1814 repo_path = realpath(optarg, NULL);
1815 if (repo_path == NULL)
1816 err(1, "-r option");
1817 got_path_strip_trailing_slashes(repo_path);
1841 cwd = getcwd(NULL, 0);
1843 error = got_error_from_errno("getcwd");
1846 if (repo_path == NULL) {
1847 error = got_worktree_open(&worktree, cwd);
1848 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1854 strdup(got_worktree_get_repo_path(worktree));
1855 if (repo_path == NULL)
1856 error = got_error_from_errno("strdup");
1860 repo_path = strdup(cwd);
1861 if (repo_path == NULL) {
1862 error = got_error_from_errno("strdup");
1868 error = got_repo_open(&repo, repo_path);
1872 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1878 char *p, *worktree_subdir = cwd +
1879 strlen(got_worktree_get_root_path(worktree));
1880 if (asprintf(&p, "%s/%s",
1881 got_worktree_get_path_prefix(worktree),
1882 worktree_subdir) == -1) {
1883 error = got_error_from_errno("asprintf");
1886 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1893 if (in_repo_path == NULL) {
1894 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1899 if (commit_id_str == NULL) {
1900 struct got_reference *head_ref;
1901 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1904 error = got_ref_resolve(&commit_id, repo, head_ref);
1905 got_ref_close(head_ref);
1909 error = got_repo_match_object_id_prefix(&commit_id,
1910 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1915 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1916 in_repo_path, repo);
1923 got_worktree_close(worktree);
1925 const struct got_error *repo_error;
1926 repo_error = got_repo_close(repo);
1936 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1940 static const struct got_error *
1941 print_status(void *arg, unsigned char status, const char *path,
1942 struct got_object_id *blob_id, struct got_object_id *commit_id)
1944 printf("%c %s\n", status, path);
1948 static const struct got_error *
1949 cmd_status(int argc, char *argv[])
1951 const struct got_error *error = NULL;
1952 struct got_repository *repo = NULL;
1953 struct got_worktree *worktree = NULL;
1954 char *cwd = NULL, *path = NULL;
1957 while ((ch = getopt(argc, argv, "")) != -1) {
1969 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1973 cwd = getcwd(NULL, 0);
1975 error = got_error_from_errno("getcwd");
1979 error = got_worktree_open(&worktree, cwd);
1986 error = got_error_from_errno("strdup");
1989 } else if (argc == 1) {
1990 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1996 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2000 error = apply_unveil(got_repo_get_path(repo), 1,
2001 got_worktree_get_root_path(worktree), 0);
2005 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2006 check_cancelled, NULL);
2017 "usage: %s ref [-r repository] -l | -d name | name target\n",
2022 static const struct got_error *
2023 list_refs(struct got_repository *repo)
2025 static const struct got_error *err = NULL;
2026 struct got_reflist_head refs;
2027 struct got_reflist_entry *re;
2029 SIMPLEQ_INIT(&refs);
2030 err = got_ref_list(&refs, repo);
2034 SIMPLEQ_FOREACH(re, &refs, entry) {
2036 refstr = got_ref_to_str(re->ref);
2038 return got_error_from_errno("got_ref_to_str");
2039 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2043 got_ref_list_free(&refs);
2047 static const struct got_error *
2048 delete_ref(struct got_repository *repo, const char *refname)
2050 const struct got_error *err = NULL;
2051 struct got_reference *ref;
2053 err = got_ref_open(&ref, repo, refname, 0);
2057 err = got_ref_delete(ref, repo);
2062 static const struct got_error *
2063 add_ref(struct got_repository *repo, const char *refname, const char *target)
2065 const struct got_error *err = NULL;
2066 struct got_object_id *id;
2067 struct got_reference *ref = NULL;
2069 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2072 struct got_reference *target_ref;
2074 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2076 err = got_ref_open(&target_ref, repo, target, 0);
2079 err = got_ref_resolve(&id, repo, target_ref);
2080 got_ref_close(target_ref);
2085 err = got_ref_alloc(&ref, refname, id);
2089 err = got_ref_write(ref, repo);
2097 static const struct got_error *
2098 cmd_ref(int argc, char *argv[])
2100 const struct got_error *error = NULL;
2101 struct got_repository *repo = NULL;
2102 struct got_worktree *worktree = NULL;
2103 char *cwd = NULL, *repo_path = NULL;
2104 int ch, do_list = 0;
2105 const char *delref = NULL;
2107 /* TODO: Add -s option for adding symbolic references. */
2108 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2114 repo_path = realpath(optarg, NULL);
2115 if (repo_path == NULL)
2116 err(1, "-r option");
2117 got_path_strip_trailing_slashes(repo_path);
2128 if (do_list && delref)
2129 errx(1, "-l and -d options are mutually exclusive\n");
2134 if (do_list || delref) {
2137 } else if (argc != 2)
2142 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2146 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2147 "sendfd unveil", NULL) == -1)
2151 cwd = getcwd(NULL, 0);
2153 error = got_error_from_errno("getcwd");
2157 if (repo_path == NULL) {
2158 error = got_worktree_open(&worktree, cwd);
2159 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2165 strdup(got_worktree_get_repo_path(worktree));
2166 if (repo_path == NULL)
2167 error = got_error_from_errno("strdup");
2171 repo_path = strdup(cwd);
2172 if (repo_path == NULL) {
2173 error = got_error_from_errno("strdup");
2179 error = got_repo_open(&repo, repo_path);
2183 error = apply_unveil(got_repo_get_path(repo), do_list,
2184 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2189 error = list_refs(repo);
2191 error = delete_ref(repo, delref);
2193 error = add_ref(repo, argv[0], argv[1]);
2196 got_repo_close(repo);
2198 got_worktree_close(worktree);
2208 "usage: %s branch [-r repository] -l | -d name | "
2209 "name [base-branch]\n", getprogname());
2213 static const struct got_error *
2214 list_branches(struct got_repository *repo)
2216 static const struct got_error *err = NULL;
2217 struct got_reflist_head refs;
2218 struct got_reflist_entry *re;
2220 SIMPLEQ_INIT(&refs);
2221 err = got_ref_list(&refs, repo);
2225 SIMPLEQ_FOREACH(re, &refs, entry) {
2226 const char *refname;
2228 refname = got_ref_get_name(re->ref);
2229 if (strncmp(refname, "refs/heads/", 11) != 0)
2232 refstr = got_ref_to_str(re->ref);
2234 return got_error_from_errno("got_ref_to_str");
2235 printf("%s: %s\n", refname, refstr);
2239 got_ref_list_free(&refs);
2243 static const struct got_error *
2244 delete_branch(struct got_repository *repo, const char *branch_name)
2246 const struct got_error *err = NULL;
2247 struct got_reference *ref;
2250 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2251 return got_error_from_errno("asprintf");
2253 err = got_ref_open(&ref, repo, refname, 0);
2257 err = got_ref_delete(ref, repo);
2264 static const struct got_error *
2265 add_branch(struct got_repository *repo, const char *branch_name,
2266 const char *base_branch)
2268 const struct got_error *err = NULL;
2269 struct got_object_id *id = NULL;
2270 struct got_reference *ref = NULL;
2271 char *base_refname = NULL, *refname = NULL;
2272 struct got_reference *base_ref;
2274 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2275 base_refname = strdup(GOT_REF_HEAD);
2276 if (base_refname == NULL)
2277 return got_error_from_errno("strdup");
2278 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2279 return got_error_from_errno("asprintf");
2281 err = got_ref_open(&base_ref, repo, base_refname, 0);
2284 err = got_ref_resolve(&id, repo, base_ref);
2285 got_ref_close(base_ref);
2289 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2290 err = got_error_from_errno("asprintf");
2294 err = got_ref_open(&ref, repo, refname, 0);
2296 err = got_error(GOT_ERR_BRANCH_EXISTS);
2298 } else if (err->code != GOT_ERR_NOT_REF)
2301 err = got_ref_alloc(&ref, refname, id);
2305 err = got_ref_write(ref, repo);
2315 static const struct got_error *
2316 cmd_branch(int argc, char *argv[])
2318 const struct got_error *error = NULL;
2319 struct got_repository *repo = NULL;
2320 struct got_worktree *worktree = NULL;
2321 char *cwd = NULL, *repo_path = NULL;
2322 int ch, do_list = 0;
2323 const char *delref = NULL;
2325 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2331 repo_path = realpath(optarg, NULL);
2332 if (repo_path == NULL)
2333 err(1, "-r option");
2334 got_path_strip_trailing_slashes(repo_path);
2345 if (do_list && delref)
2346 errx(1, "-l and -d options are mutually exclusive\n");
2351 if (do_list || delref) {
2354 } else if (argc < 1 || argc > 2)
2359 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2363 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2364 "sendfd unveil", NULL) == -1)
2368 cwd = getcwd(NULL, 0);
2370 error = got_error_from_errno("getcwd");
2374 if (repo_path == NULL) {
2375 error = got_worktree_open(&worktree, cwd);
2376 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2382 strdup(got_worktree_get_repo_path(worktree));
2383 if (repo_path == NULL)
2384 error = got_error_from_errno("strdup");
2388 repo_path = strdup(cwd);
2389 if (repo_path == NULL) {
2390 error = got_error_from_errno("strdup");
2396 error = got_repo_open(&repo, repo_path);
2400 error = apply_unveil(got_repo_get_path(repo), do_list,
2401 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2406 error = list_branches(repo);
2408 error = delete_branch(repo, delref);
2410 const char *base_branch;
2412 base_branch = worktree ?
2413 got_worktree_get_head_ref_name(worktree) :
2416 base_branch = argv[1];
2417 error = add_branch(repo, argv[0], base_branch);
2421 got_repo_close(repo);
2423 got_worktree_close(worktree);
2432 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2436 static const struct got_error *
2437 cmd_add(int argc, char *argv[])
2439 const struct got_error *error = NULL;
2440 struct got_repository *repo = NULL;
2441 struct got_worktree *worktree = NULL;
2443 struct got_pathlist_head paths;
2444 struct got_pathlist_entry *pe;
2449 while ((ch = getopt(argc, argv, "")) != -1) {
2463 /* make sure each file exists before doing anything halfway */
2464 for (x = 0; x < argc; x++) {
2465 char *path = realpath(argv[x], NULL);
2467 error = got_error_from_errno2("realpath", argv[x]);
2473 cwd = getcwd(NULL, 0);
2475 error = got_error_from_errno("getcwd");
2479 error = got_worktree_open(&worktree, cwd);
2483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2487 error = apply_unveil(got_repo_get_path(repo), 1,
2488 got_worktree_get_root_path(worktree), 0);
2492 for (x = 0; x < argc; x++) {
2493 char *path = realpath(argv[x], NULL);
2495 error = got_error_from_errno2("realpath", argv[x]);
2499 got_path_strip_trailing_slashes(path);
2500 error = got_pathlist_insert(&pe, &paths, path, NULL);
2506 error = got_worktree_schedule_add(worktree, &paths, print_status,
2510 got_repo_close(repo);
2512 got_worktree_close(worktree);
2513 TAILQ_FOREACH(pe, &paths, entry)
2514 free((char *)pe->path);
2515 got_pathlist_free(&paths);
2523 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2527 static const struct got_error *
2528 cmd_remove(int argc, char *argv[])
2530 const struct got_error *error = NULL;
2531 struct got_worktree *worktree = NULL;
2532 struct got_repository *repo = NULL;
2534 struct got_pathlist_head paths;
2535 struct got_pathlist_entry *pe;
2536 int ch, i, delete_local_mods = 0;
2540 while ((ch = getopt(argc, argv, "f")) != -1) {
2543 delete_local_mods = 1;
2557 /* make sure each file exists before doing anything halfway */
2558 for (i = 0; i < argc; i++) {
2559 char *path = realpath(argv[i], NULL);
2561 error = got_error_from_errno2("realpath", argv[i]);
2567 cwd = getcwd(NULL, 0);
2569 error = got_error_from_errno("getcwd");
2572 error = got_worktree_open(&worktree, cwd);
2576 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2580 error = apply_unveil(got_repo_get_path(repo), 1,
2581 got_worktree_get_root_path(worktree), 0);
2585 for (i = 0; i < argc; i++) {
2586 char *path = realpath(argv[i], NULL);
2588 error = got_error_from_errno2("realpath", argv[i]);
2592 got_path_strip_trailing_slashes(path);
2593 error = got_pathlist_insert(&pe, &paths, path, NULL);
2599 error = got_worktree_schedule_delete(worktree, &paths,
2600 delete_local_mods, print_status, NULL, repo);
2605 got_repo_close(repo);
2607 got_worktree_close(worktree);
2608 TAILQ_FOREACH(pe, &paths, entry)
2609 free((char *)pe->path);
2610 got_pathlist_free(&paths);
2618 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2623 revert_progress(void *arg, unsigned char status, const char *path)
2625 while (path[0] == '/')
2627 printf("%c %s\n", status, path);
2630 static const struct got_error *
2631 cmd_revert(int argc, char *argv[])
2633 const struct got_error *error = NULL;
2634 struct got_worktree *worktree = NULL;
2635 struct got_repository *repo = NULL;
2636 char *cwd = NULL, *path = NULL;
2637 struct got_pathlist_head paths;
2638 struct got_pathlist_entry *pe;
2643 while ((ch = getopt(argc, argv, "")) != -1) {
2657 for (i = 0; i < argc; i++) {
2658 char *path = realpath(argv[i], NULL);
2660 error = got_error_from_errno2("realpath", argv[i]);
2664 got_path_strip_trailing_slashes(path);
2665 error = got_pathlist_insert(&pe, &paths, path, NULL);
2672 cwd = getcwd(NULL, 0);
2674 error = got_error_from_errno("getcwd");
2677 error = got_worktree_open(&worktree, cwd);
2681 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2685 error = apply_unveil(got_repo_get_path(repo), 1,
2686 got_worktree_get_root_path(worktree), 0);
2690 error = got_worktree_revert(worktree, &paths,
2691 revert_progress, NULL, repo);
2696 got_repo_close(repo);
2698 got_worktree_close(worktree);
2707 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2712 spawn_editor(const char *editor, const char *file)
2715 sig_t sighup, sigint, sigquit;
2718 sighup = signal(SIGHUP, SIG_IGN);
2719 sigint = signal(SIGINT, SIG_IGN);
2720 sigquit = signal(SIGQUIT, SIG_IGN);
2722 switch (pid = fork()) {
2726 execl(editor, editor, file, (char *)NULL);
2730 while (waitpid(pid, &st, 0) == -1)
2735 (void)signal(SIGHUP, sighup);
2736 (void)signal(SIGINT, sigint);
2737 (void)signal(SIGQUIT, sigquit);
2739 if (!WIFEXITED(st)) {
2744 return WEXITSTATUS(st);
2747 struct collect_commit_logmsg_arg {
2748 const char *cmdline_log;
2750 const char *worktree_path;
2751 const char *branch_name;
2752 const char *repo_path;
2757 static const struct got_error *
2758 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2761 char *initial_content = NULL;
2762 struct got_pathlist_entry *pe;
2763 const struct got_error *err = NULL;
2764 char *template = NULL;
2765 struct collect_commit_logmsg_arg *a = arg;
2767 struct stat st, st2;
2770 int fd, content_changed = 0;
2772 /* if a message was specified on the command line, just use it */
2773 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2774 len = strlen(a->cmdline_log) + 1;
2775 *logmsg = malloc(len + 1);
2776 if (*logmsg == NULL)
2777 return got_error_from_errno("malloc");
2778 strlcpy(*logmsg, a->cmdline_log, len);
2782 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2783 return got_error_from_errno("asprintf");
2785 if (asprintf(&initial_content,
2786 "\n# changes to be committed on branch %s:\n",
2787 a->branch_name) == -1)
2788 return got_error_from_errno("asprintf");
2790 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2794 dprintf(fd, initial_content);
2796 TAILQ_FOREACH(pe, commitable_paths, entry) {
2797 struct got_commitable *ct = pe->data;
2798 dprintf(fd, "# %c %s\n",
2799 got_commitable_get_status(ct),
2800 got_commitable_get_path(ct));
2804 if (stat(a->logmsg_path, &st) == -1) {
2805 err = got_error_from_errno2("stat", a->logmsg_path);
2809 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2810 err = got_error_from_errno("failed spawning editor");
2814 if (stat(a->logmsg_path, &st2) == -1) {
2815 err = got_error_from_errno("stat");
2819 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2820 unlink(a->logmsg_path);
2821 free(a->logmsg_path);
2822 a->logmsg_path = NULL;
2823 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2824 "no changes made to commit message, aborting");
2828 *logmsg = malloc(st2.st_size + 1);
2829 if (*logmsg == NULL) {
2830 err = got_error_from_errno("malloc");
2833 (*logmsg)[0] = '\0';
2836 fp = fopen(a->logmsg_path, "r");
2838 err = got_error_from_errno("fopen");
2841 while (fgets(buf, sizeof(buf), fp) != NULL) {
2842 if (!content_changed && strcmp(buf, initial_content) != 0)
2843 content_changed = 1;
2844 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2845 continue; /* remove comments and leading empty lines */
2846 len = strlcat(*logmsg, buf, st2.st_size);
2850 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2851 (*logmsg)[len - 1] = '\0';
2855 if (len == 0 || !content_changed) {
2856 unlink(a->logmsg_path);
2857 free(a->logmsg_path);
2858 a->logmsg_path = NULL;
2859 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2860 "commit message cannot be empty, aborting");
2864 free(initial_content);
2867 /* Editor is done; we can now apply unveil(2) */
2869 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2873 static const struct got_error *
2874 cmd_commit(int argc, char *argv[])
2876 const struct got_error *error = NULL;
2877 struct got_worktree *worktree = NULL;
2878 struct got_repository *repo = NULL;
2879 char *cwd = NULL, *path = NULL, *id_str = NULL;
2880 struct got_object_id *id = NULL;
2881 const char *logmsg = NULL;
2882 const char *got_author = getenv("GOT_AUTHOR");
2883 struct collect_commit_logmsg_arg cl_arg;
2884 char *editor = NULL;
2885 int ch, rebase_in_progress;
2887 while ((ch = getopt(argc, argv, "m:")) != -1) {
2902 path = realpath(argv[0], NULL);
2904 error = got_error_from_errno2("realpath", argv[0]);
2907 got_path_strip_trailing_slashes(path);
2908 } else if (argc != 0)
2911 if (got_author == NULL) {
2912 /* TODO: Look current user up in password database */
2913 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2917 cwd = getcwd(NULL, 0);
2919 error = got_error_from_errno("getcwd");
2922 error = got_worktree_open(&worktree, cwd);
2926 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2929 if (rebase_in_progress) {
2930 error = got_error(GOT_ERR_REBASING);
2934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2939 * unveil(2) traverses exec(2); if an editor is used we have
2940 * to apply unveil after the log message has been written.
2942 if (logmsg == NULL || strlen(logmsg) == 0)
2943 error = get_editor(&editor);
2945 error = apply_unveil(got_repo_get_path(repo), 0,
2946 got_worktree_get_root_path(worktree), 0);
2950 cl_arg.editor = editor;
2951 cl_arg.cmdline_log = logmsg;
2952 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2953 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2954 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2955 cl_arg.branch_name += 5;
2956 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2957 cl_arg.branch_name += 6;
2958 cl_arg.repo_path = got_repo_get_path(repo);
2959 cl_arg.logmsg_path = NULL;
2960 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2961 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2963 if (cl_arg.logmsg_path)
2964 fprintf(stderr, "%s: log message preserved in %s\n",
2965 getprogname(), cl_arg.logmsg_path);
2969 if (cl_arg.logmsg_path)
2970 unlink(cl_arg.logmsg_path);
2972 error = got_object_id_str(&id_str, id);
2975 printf("Created commit %s\n", id_str);
2978 got_repo_close(repo);
2980 got_worktree_close(worktree);
2989 usage_cherrypick(void)
2991 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
2995 static const struct got_error *
2996 cmd_cherrypick(int argc, char *argv[])
2998 const struct got_error *error = NULL;
2999 struct got_worktree *worktree = NULL;
3000 struct got_repository *repo = NULL;
3001 char *cwd = NULL, *commit_id_str = NULL;
3002 struct got_object_id *commit_id = NULL;
3003 struct got_commit_object *commit = NULL;
3004 struct got_object_qid *pid;
3005 struct got_reference *head_ref = NULL;
3006 int ch, did_something = 0;
3008 while ((ch = getopt(argc, argv, "")) != -1) {
3022 cwd = getcwd(NULL, 0);
3024 error = got_error_from_errno("getcwd");
3027 error = got_worktree_open(&worktree, cwd);
3031 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3035 error = apply_unveil(got_repo_get_path(repo), 0,
3036 got_worktree_get_root_path(worktree), 0);
3040 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3041 GOT_OBJ_TYPE_COMMIT, repo);
3042 if (error != NULL) {
3043 struct got_reference *ref;
3044 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3046 error = got_ref_open(&ref, repo, argv[0], 0);
3049 error = got_ref_resolve(&commit_id, repo, ref);
3054 error = got_object_id_str(&commit_id_str, commit_id);
3058 error = got_ref_open(&head_ref, repo,
3059 got_worktree_get_head_ref_name(worktree), 0);
3063 error = check_same_branch(commit_id, head_ref, repo);
3065 if (error->code != GOT_ERR_ANCESTRY)
3069 error = got_error(GOT_ERR_SAME_BRANCH);
3073 error = got_object_open_as_commit(&commit, repo, commit_id);
3076 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3077 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3078 commit_id, repo, update_progress, &did_something, check_cancelled,
3084 printf("Merged commit %s\n", commit_id_str);
3087 got_object_commit_close(commit);
3088 free(commit_id_str);
3090 got_ref_close(head_ref);
3092 got_worktree_close(worktree);
3094 got_repo_close(repo);
3101 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3105 static const struct got_error *
3106 cmd_backout(int argc, char *argv[])
3108 const struct got_error *error = NULL;
3109 struct got_worktree *worktree = NULL;
3110 struct got_repository *repo = NULL;
3111 char *cwd = NULL, *commit_id_str = NULL;
3112 struct got_object_id *commit_id = NULL;
3113 struct got_commit_object *commit = NULL;
3114 struct got_object_qid *pid;
3115 struct got_reference *head_ref = NULL;
3116 int ch, did_something = 0;
3118 while ((ch = getopt(argc, argv, "")) != -1) {
3132 cwd = getcwd(NULL, 0);
3134 error = got_error_from_errno("getcwd");
3137 error = got_worktree_open(&worktree, cwd);
3141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3145 error = apply_unveil(got_repo_get_path(repo), 0,
3146 got_worktree_get_root_path(worktree), 0);
3150 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3151 GOT_OBJ_TYPE_COMMIT, repo);
3152 if (error != NULL) {
3153 struct got_reference *ref;
3154 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3156 error = got_ref_open(&ref, repo, argv[0], 0);
3159 error = got_ref_resolve(&commit_id, repo, ref);
3164 error = got_object_id_str(&commit_id_str, commit_id);
3168 error = got_ref_open(&head_ref, repo,
3169 got_worktree_get_head_ref_name(worktree), 0);
3173 error = check_same_branch(commit_id, head_ref, repo);
3177 error = got_object_open_as_commit(&commit, repo, commit_id);
3180 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3182 error = got_error(GOT_ERR_ROOT_COMMIT);
3186 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3187 update_progress, &did_something, check_cancelled, NULL);
3192 printf("Backed out commit %s\n", commit_id_str);
3195 got_object_commit_close(commit);
3196 free(commit_id_str);
3198 got_ref_close(head_ref);
3200 got_worktree_close(worktree);
3202 got_repo_close(repo);
3209 fprintf(stderr, "usage: %s rebase [-a] [-c] | branch\n", getprogname());
3213 static const struct got_error *
3214 show_rebase_progress(struct got_commit_object *commit,
3215 struct got_object_id *old_id, struct got_object_id *new_id)
3217 const struct got_error *err;
3218 char *old_id_str = NULL, *new_id_str = NULL;
3219 char *logmsg0 = NULL, *logmsg, *nl;
3222 err = got_object_id_str(&old_id_str, old_id);
3227 err = got_object_id_str(&new_id_str, new_id);
3232 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3233 if (logmsg0 == NULL) {
3234 err = got_error_from_errno("strdup");
3239 while (isspace(logmsg[0]))
3242 old_id_str[12] = '\0';
3244 new_id_str[12] = '\0';
3245 len = strlen(logmsg);
3249 nl = strchr(logmsg, '\n');
3252 printf("%s -> %s: %s\n", old_id_str,
3253 new_id_str ? new_id_str : "no-op change", logmsg);
3262 rebase_progress(void *arg, unsigned char status, const char *path)
3264 unsigned char *rebase_status = arg;
3266 while (path[0] == '/')
3268 printf("%c %s\n", status, path);
3270 if (*rebase_status == GOT_STATUS_CONFLICT)
3272 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3273 *rebase_status = status;
3276 static const struct got_error *
3277 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3278 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3279 struct got_repository *repo)
3281 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3282 return got_worktree_rebase_complete(worktree,
3283 new_base_branch, tmp_branch, branch, repo);
3286 static const struct got_error *
3287 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3288 struct got_object_id *commit_id, struct got_repository *repo)
3290 const struct got_error *error;
3291 struct got_commit_object *commit;
3292 struct got_object_id *new_commit_id;
3294 error = got_object_open_as_commit(&commit, repo, commit_id);
3298 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3299 tmp_branch, commit, commit_id, repo);
3301 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3303 error = show_rebase_progress(commit, commit_id, NULL);
3305 error = show_rebase_progress(commit, commit_id, new_commit_id);
3306 free(new_commit_id);
3309 got_object_commit_close(commit);
3313 static const struct got_error *
3314 cmd_rebase(int argc, char *argv[])
3316 const struct got_error *error = NULL;
3317 struct got_worktree *worktree = NULL;
3318 struct got_repository *repo = NULL;
3320 struct got_reference *branch = NULL;
3321 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3322 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3323 struct got_object_id *resume_commit_id = NULL;
3324 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3325 struct got_commit_graph *graph = NULL;
3326 struct got_commit_object *commit = NULL;
3327 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3328 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3329 struct got_object_id_queue commits;
3330 const struct got_object_id_queue *parent_ids;
3331 struct got_object_qid *qid, *pid;
3333 SIMPLEQ_INIT(&commits);
3335 while ((ch = getopt(argc, argv, "ac")) != -1) {
3341 continue_rebase = 1;
3352 if (abort_rebase && continue_rebase)
3354 else if (abort_rebase || continue_rebase) {
3357 } else if (argc != 1)
3360 cwd = getcwd(NULL, 0);
3362 error = got_error_from_errno("getcwd");
3365 error = got_worktree_open(&worktree, cwd);
3370 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3374 error = apply_unveil(got_repo_get_path(repo), 0,
3375 got_worktree_get_root_path(worktree), 0);
3379 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3383 if (rebase_in_progress && abort_rebase) {
3385 error = got_worktree_rebase_continue(&resume_commit_id,
3386 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3389 printf("Switching work tree to %s\n",
3390 got_ref_get_symref_target(new_base_branch));
3391 error = got_worktree_rebase_abort(worktree, repo,
3392 new_base_branch, update_progress, &did_something);
3395 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3396 goto done; /* nothing else to do */
3397 } else if (abort_rebase) {
3398 error = got_error(GOT_ERR_NOT_REBASING);
3402 if (continue_rebase) {
3403 error = got_worktree_rebase_continue(&resume_commit_id,
3404 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3408 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3413 yca_id = got_object_id_dup(resume_commit_id);
3414 if (yca_id == NULL) {
3415 error = got_error_from_errno("got_object_id_dup");
3419 error = got_ref_open(&branch, repo, argv[0], 0);
3423 error = check_same_branch(
3424 got_worktree_get_base_commit_id(worktree), branch, repo);
3426 if (error->code != GOT_ERR_ANCESTRY)
3430 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3431 "specified branch resolves to a commit which "
3432 "is already contained in work tree's branch");
3437 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3441 if (!continue_rebase) {
3442 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3443 got_worktree_get_base_commit_id(worktree),
3444 branch_head_commit_id, repo);
3447 if (yca_id == NULL) {
3448 error = got_error_msg(GOT_ERR_ANCESTRY,
3449 "specified branch shares no common ancestry "
3450 "with work tree's branch");
3454 error = got_worktree_rebase_prepare(&new_base_branch,
3455 &tmp_branch, worktree, branch, repo);
3460 commit_id = branch_head_commit_id;
3461 error = got_object_open_as_commit(&commit, repo, commit_id);
3465 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3468 parent_ids = got_object_commit_get_parent_ids(commit);
3469 pid = SIMPLEQ_FIRST(parent_ids);
3470 error = got_commit_graph_iter_start(graph, pid->id, repo);
3471 got_object_commit_close(commit);
3475 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3476 error = got_commit_graph_iter_next(&parent_id, graph);
3478 if (error->code == GOT_ERR_ITER_COMPLETED) {
3479 error = got_error_msg(GOT_ERR_ANCESTRY,
3480 "ran out of commits to rebase before "
3481 "youngest common ancestor commit has "
3484 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3486 error = got_commit_graph_fetch_commits(graph, 1, repo);
3490 error = got_object_qid_alloc(&qid, commit_id);
3493 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3494 commit_id = parent_id;
3498 if (SIMPLEQ_EMPTY(&commits)) {
3499 if (continue_rebase)
3500 error = rebase_complete(worktree, branch,
3501 new_base_branch, tmp_branch, repo);
3503 error = got_error(GOT_ERR_EMPTY_REBASE);
3508 SIMPLEQ_FOREACH(qid, &commits, entry) {
3509 commit_id = qid->id;
3510 parent_id = pid ? pid->id : yca_id;
3513 error = got_worktree_rebase_merge_files(worktree, parent_id,
3514 commit_id, repo, rebase_progress, &rebase_status,
3515 check_cancelled, NULL);
3519 if (rebase_status == GOT_STATUS_CONFLICT)
3522 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3527 if (rebase_status == GOT_STATUS_CONFLICT) {
3528 error = got_worktree_rebase_postpone(worktree);
3531 error = got_error_msg(GOT_ERR_CONFLICTS,
3532 "conflicts must be resolved before rebase can be resumed");
3534 error = rebase_complete(worktree, branch, new_base_branch,
3537 got_object_id_queue_free(&commits);
3538 free(branch_head_commit_id);
3539 free(resume_commit_id);
3542 got_commit_graph_close(graph);
3544 got_object_commit_close(commit);
3546 got_ref_close(branch);
3547 if (new_base_branch)
3548 got_ref_close(new_base_branch);
3550 got_ref_close(tmp_branch);
3552 got_worktree_close(worktree);
3554 got_repo_close(repo);