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);
779 error = got_object_id_str(&commit_id_str, commit_id);
785 struct got_object_id *head_commit_id;
786 if (strlen(path) != 0) {
787 fprintf(stderr, "%s: switching between branches "
788 "requires that the entire work tree "
789 "gets updated, not just '%s'\n",
790 getprogname(), path);
791 error = got_error(GOT_ERR_BAD_PATH);
794 error = got_ref_resolve(&head_commit_id, repo, head_ref);
797 error = check_linear_ancestry(commit_id, head_commit_id, repo);
798 free(head_commit_id);
801 error = check_same_branch(commit_id, head_ref, repo);
804 error = switch_head_ref(head_ref, commit_id, worktree, repo);
808 error = check_linear_ancestry(commit_id,
809 got_worktree_get_base_commit_id(worktree), repo);
811 if (error->code == GOT_ERR_ANCESTRY)
812 error = got_error(GOT_ERR_BRANCH_MOVED);
815 error = check_same_branch(commit_id, head_ref, repo);
820 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
822 error = got_worktree_set_base_commit_id(worktree, repo,
828 error = got_worktree_checkout_files(worktree, path, repo,
829 update_progress, &did_something, check_cancelled, NULL);
834 printf("Updated to commit %s\n", commit_id_str);
836 printf("Already up-to-date\n");
845 static const struct got_error *
846 print_patch(struct got_commit_object *commit, struct got_object_id *id,
847 int diff_context, struct got_repository *repo)
849 const struct got_error *err = NULL;
850 struct got_tree_object *tree1 = NULL, *tree2;
851 struct got_object_qid *qid;
852 char *id_str1 = NULL, *id_str2;
853 struct got_diff_blob_output_unidiff_arg arg;
855 err = got_object_open_as_tree(&tree2, repo,
856 got_object_commit_get_tree_id(commit));
860 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
862 struct got_commit_object *pcommit;
864 err = got_object_open_as_commit(&pcommit, repo, qid->id);
868 err = got_object_open_as_tree(&tree1, repo,
869 got_object_commit_get_tree_id(pcommit));
870 got_object_commit_close(pcommit);
874 err = got_object_id_str(&id_str1, qid->id);
879 err = got_object_id_str(&id_str2, id);
883 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
884 arg.diff_context = diff_context;
885 arg.outfile = stdout;
886 err = got_diff_tree(tree1, tree2, "", "", repo,
887 got_diff_blob_output_unidiff, &arg);
890 got_object_tree_close(tree1);
891 got_object_tree_close(tree2);
898 get_datestr(time_t *time, char *datebuf)
900 char *p, *s = ctime_r(time, datebuf);
907 static const struct got_error *
908 print_commit(struct got_commit_object *commit, struct got_object_id *id,
909 struct got_repository *repo, int show_patch, int diff_context,
910 struct got_reflist_head *refs)
912 const struct got_error *err = NULL;
913 char *id_str, *datestr, *logmsg0, *logmsg, *line;
915 time_t committer_time;
916 const char *author, *committer;
917 char *refs_str = NULL;
918 struct got_reflist_entry *re;
920 SIMPLEQ_FOREACH(re, refs, entry) {
923 if (got_object_id_cmp(re->id, id) != 0)
925 name = got_ref_get_name(re->ref);
926 if (strcmp(name, GOT_REF_HEAD) == 0)
928 if (strncmp(name, "refs/", 5) == 0)
930 if (strncmp(name, "got/", 4) == 0)
932 if (strncmp(name, "heads/", 6) == 0)
934 if (strncmp(name, "remotes/", 8) == 0)
937 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
939 err = got_error_from_errno("asprintf");
945 err = got_object_id_str(&id_str, id);
949 printf("-----------------------------------------------\n");
950 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
951 refs_str ? refs_str : "", refs_str ? ")" : "");
956 printf("from: %s\n", got_object_commit_get_author(commit));
957 committer_time = got_object_commit_get_committer_time(commit);
958 datestr = get_datestr(&committer_time, datebuf);
959 printf("date: %s UTC\n", datestr);
960 author = got_object_commit_get_author(commit);
961 committer = got_object_commit_get_committer(commit);
962 if (strcmp(author, committer) != 0)
963 printf("via: %s\n", committer);
964 if (got_object_commit_get_nparents(commit) > 1) {
965 const struct got_object_id_queue *parent_ids;
966 struct got_object_qid *qid;
968 parent_ids = got_object_commit_get_parent_ids(commit);
969 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
970 err = got_object_id_str(&id_str, qid->id);
973 printf("parent %d: %s\n", n++, id_str);
978 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
980 return got_error_from_errno("strdup");
984 line = strsep(&logmsg, "\n");
986 printf(" %s\n", line);
991 err = print_patch(commit, id, diff_context, repo);
996 if (fflush(stdout) != 0 && err == NULL)
997 err = got_error_from_errno("fflush");
1001 static const struct got_error *
1002 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1003 char *path, int show_patch, int diff_context, int limit,
1004 int first_parent_traversal, struct got_reflist_head *refs)
1006 const struct got_error *err;
1007 struct got_commit_graph *graph;
1009 err = got_commit_graph_open(&graph, root_id, path,
1010 first_parent_traversal, repo);
1013 err = got_commit_graph_iter_start(graph, root_id, repo);
1017 struct got_commit_object *commit;
1018 struct got_object_id *id;
1020 if (sigint_received || sigpipe_received)
1023 err = got_commit_graph_iter_next(&id, graph);
1025 if (err->code == GOT_ERR_ITER_COMPLETED) {
1029 if (err->code != GOT_ERR_ITER_NEED_MORE)
1031 err = got_commit_graph_fetch_commits(graph, 1, repo);
1040 err = got_object_open_as_commit(&commit, repo, id);
1043 err = print_commit(commit, id, repo, show_patch, diff_context,
1045 got_object_commit_close(commit);
1046 if (err || (limit && --limit == 0))
1050 got_commit_graph_close(graph);
1057 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1058 "[-r repository-path] [path]\n", getprogname());
1062 static const struct got_error *
1063 cmd_log(int argc, char *argv[])
1065 const struct got_error *error;
1066 struct got_repository *repo = NULL;
1067 struct got_worktree *worktree = NULL;
1068 struct got_commit_object *commit = NULL;
1069 struct got_object_id *id = NULL;
1070 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1071 char *start_commit = NULL;
1072 int diff_context = 3, ch;
1073 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1075 struct got_reflist_head refs;
1077 SIMPLEQ_INIT(&refs);
1080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1086 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1089 first_parent_traversal = 1;
1095 start_commit = optarg;
1098 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1101 err(1, "-C option %s", errstr);
1104 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1106 err(1, "-l option %s", errstr);
1109 repo_path = realpath(optarg, NULL);
1110 if (repo_path == NULL)
1111 err(1, "-r option");
1112 got_path_strip_trailing_slashes(repo_path);
1123 cwd = getcwd(NULL, 0);
1125 error = got_error_from_errno("getcwd");
1129 error = got_worktree_open(&worktree, cwd);
1130 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1137 error = got_error_from_errno("strdup");
1140 } else if (argc == 1) {
1142 error = got_worktree_resolve_path(&path, worktree,
1147 path = strdup(argv[0]);
1149 error = got_error_from_errno("strdup");
1156 if (repo_path == NULL) {
1157 repo_path = worktree ?
1158 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1160 if (repo_path == NULL) {
1161 error = got_error_from_errno("strdup");
1165 error = got_repo_open(&repo, repo_path);
1169 error = apply_unveil(got_repo_get_path(repo), 1,
1170 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1174 if (start_commit == NULL) {
1175 struct got_reference *head_ref;
1176 error = got_ref_open(&head_ref, repo,
1177 worktree ? got_worktree_get_head_ref_name(worktree)
1181 error = got_ref_resolve(&id, repo, head_ref);
1182 got_ref_close(head_ref);
1185 error = got_object_open_as_commit(&commit, repo, id);
1187 struct got_reference *ref;
1188 error = got_ref_open(&ref, repo, start_commit, 0);
1189 if (error == NULL) {
1191 error = got_ref_resolve(&id, repo, ref);
1195 error = got_object_get_type(&obj_type, repo, id);
1198 if (obj_type == GOT_OBJ_TYPE_TAG) {
1199 struct got_tag_object *tag;
1200 error = got_object_open_as_tag(&tag, repo, id);
1203 if (got_object_tag_get_object_type(tag) !=
1204 GOT_OBJ_TYPE_COMMIT) {
1205 got_object_tag_close(tag);
1206 error = got_error(GOT_ERR_OBJ_TYPE);
1210 id = got_object_id_dup(
1211 got_object_tag_get_object_id(tag));
1213 error = got_error_from_errno(
1214 "got_object_id_dup");
1215 got_object_tag_close(tag);
1218 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1219 error = got_error(GOT_ERR_OBJ_TYPE);
1222 error = got_object_open_as_commit(&commit, repo, id);
1226 if (commit == NULL) {
1227 error = got_repo_match_object_id_prefix(&id,
1228 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1236 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1241 path = in_repo_path;
1244 error = got_ref_list(&refs, repo);
1248 error = print_commits(id, repo, path, show_patch,
1249 diff_context, limit, first_parent_traversal, &refs);
1256 got_worktree_close(worktree);
1258 const struct got_error *repo_error;
1259 repo_error = got_repo_close(repo);
1263 got_ref_list_free(&refs);
1270 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1271 "[object1 object2 | path]\n", getprogname());
1275 struct print_diff_arg {
1276 struct got_repository *repo;
1277 struct got_worktree *worktree;
1283 static const struct got_error *
1284 print_diff(void *arg, unsigned char status, const char *path,
1285 struct got_object_id *blob_id, struct got_object_id *commit_id)
1287 struct print_diff_arg *a = arg;
1288 const struct got_error *err = NULL;
1289 struct got_blob_object *blob1 = NULL;
1291 char *abspath = NULL;
1294 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1295 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1298 if (!a->header_shown) {
1299 printf("diff %s %s\n", a->id_str,
1300 got_worktree_get_root_path(a->worktree));
1301 a->header_shown = 1;
1304 if (status != GOT_STATUS_ADD) {
1305 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1311 if (status != GOT_STATUS_DELETE) {
1312 if (asprintf(&abspath, "%s/%s",
1313 got_worktree_get_root_path(a->worktree), path) == -1) {
1314 err = got_error_from_errno("asprintf");
1318 f2 = fopen(abspath, "r");
1320 err = got_error_from_errno2("fopen", abspath);
1323 if (lstat(abspath, &sb) == -1) {
1324 err = got_error_from_errno2("lstat", abspath);
1330 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1334 got_object_blob_close(blob1);
1335 if (f2 && fclose(f2) != 0 && err == NULL)
1336 err = got_error_from_errno("fclose");
1341 static const struct got_error *
1342 cmd_diff(int argc, char *argv[])
1344 const struct got_error *error;
1345 struct got_repository *repo = NULL;
1346 struct got_worktree *worktree = NULL;
1347 char *cwd = NULL, *repo_path = NULL;
1348 struct got_object_id *id1 = NULL, *id2 = NULL;
1349 const char *id_str1 = NULL, *id_str2 = NULL;
1350 char *label1 = NULL, *label2 = NULL;
1352 int diff_context = 3, ch;
1357 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1362 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1365 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1367 err(1, "-C option %s", errstr);
1370 repo_path = realpath(optarg, NULL);
1371 if (repo_path == NULL)
1372 err(1, "-r option");
1373 got_path_strip_trailing_slashes(repo_path);
1384 cwd = getcwd(NULL, 0);
1386 error = got_error_from_errno("getcwd");
1389 error = got_worktree_open(&worktree, cwd);
1390 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1393 if (worktree == NULL) {
1394 error = got_error(GOT_ERR_NOT_WORKTREE);
1399 "-r option can't be used when diffing a work tree");
1400 repo_path = strdup(got_worktree_get_repo_path(worktree));
1401 if (repo_path == NULL) {
1402 error = got_error_from_errno("strdup");
1406 error = got_worktree_resolve_path(&path, worktree,
1413 error = got_error_from_errno("strdup");
1417 } else if (argc == 2) {
1420 if (worktree && repo_path == NULL) {
1422 strdup(got_worktree_get_repo_path(worktree));
1423 if (repo_path == NULL) {
1424 error = got_error_from_errno("strdup");
1431 if (repo_path == NULL) {
1432 repo_path = getcwd(NULL, 0);
1433 if (repo_path == NULL)
1434 return got_error_from_errno("getcwd");
1437 error = got_repo_open(&repo, repo_path);
1442 error = apply_unveil(got_repo_get_path(repo), 1,
1443 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1448 struct print_diff_arg arg;
1450 error = got_object_id_str(&id_str,
1451 got_worktree_get_base_commit_id(worktree));
1455 arg.worktree = worktree;
1456 arg.diff_context = diff_context;
1457 arg.id_str = id_str;
1458 arg.header_shown = 0;
1460 error = got_worktree_status(worktree, path, repo, print_diff,
1461 &arg, check_cancelled, NULL);
1466 error = got_repo_match_object_id_prefix(&id1, id_str1,
1467 GOT_OBJ_TYPE_ANY, repo);
1469 struct got_reference *ref;
1470 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1472 error = got_ref_open(&ref, repo, id_str1, 0);
1475 label1 = strdup(got_ref_get_name(ref));
1476 if (label1 == NULL) {
1477 error = got_error_from_errno("strdup");
1480 error = got_ref_resolve(&id1, repo, ref);
1485 error = got_object_id_str(&label1, id1);
1486 if (label1 == NULL) {
1487 error = got_error_from_errno("strdup");
1492 error = got_repo_match_object_id_prefix(&id2, id_str2,
1493 GOT_OBJ_TYPE_ANY, repo);
1495 struct got_reference *ref;
1496 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1498 error = got_ref_open(&ref, repo, id_str2, 0);
1501 label2 = strdup(got_ref_get_name(ref));
1502 if (label2 == NULL) {
1503 error = got_error_from_errno("strdup");
1506 error = got_ref_resolve(&id2, repo, ref);
1511 error = got_object_id_str(&label2, id2);
1512 if (label2 == NULL) {
1513 error = got_error_from_errno("strdup");
1518 error = got_object_get_type(&type1, repo, id1);
1522 error = got_object_get_type(&type2, repo, id2);
1526 if (type1 != type2) {
1527 error = got_error(GOT_ERR_OBJ_TYPE);
1532 case GOT_OBJ_TYPE_BLOB:
1533 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1534 diff_context, repo, stdout);
1536 case GOT_OBJ_TYPE_TREE:
1537 error = got_diff_objects_as_trees(id1, id2, "", "",
1538 diff_context, repo, stdout);
1540 case GOT_OBJ_TYPE_COMMIT:
1541 printf("diff %s %s\n", label1, label2);
1542 error = got_diff_objects_as_commits(id1, id2, diff_context,
1546 error = got_error(GOT_ERR_OBJ_TYPE);
1556 got_worktree_close(worktree);
1558 const struct got_error *repo_error;
1559 repo_error = got_repo_close(repo);
1570 "usage: %s blame [-c commit] [-r repository-path] path\n",
1575 static const struct got_error *
1576 cmd_blame(int argc, char *argv[])
1578 const struct got_error *error;
1579 struct got_repository *repo = NULL;
1580 struct got_worktree *worktree = NULL;
1581 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1582 struct got_object_id *commit_id = NULL;
1583 char *commit_id_str = NULL;
1587 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1592 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1595 commit_id_str = optarg;
1598 repo_path = realpath(optarg, NULL);
1599 if (repo_path == NULL)
1600 err(1, "-r option");
1601 got_path_strip_trailing_slashes(repo_path);
1617 cwd = getcwd(NULL, 0);
1619 error = got_error_from_errno("getcwd");
1622 if (repo_path == NULL) {
1623 error = got_worktree_open(&worktree, cwd);
1624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1630 strdup(got_worktree_get_repo_path(worktree));
1631 if (repo_path == NULL)
1632 error = got_error_from_errno("strdup");
1636 repo_path = strdup(cwd);
1637 if (repo_path == NULL) {
1638 error = got_error_from_errno("strdup");
1644 error = got_repo_open(&repo, repo_path);
1648 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1653 const char *prefix = got_worktree_get_path_prefix(worktree);
1654 char *p, *worktree_subdir = cwd +
1655 strlen(got_worktree_get_root_path(worktree));
1656 if (asprintf(&p, "%s%s%s%s%s",
1657 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1658 worktree_subdir, worktree_subdir[0] ? "/" : "",
1660 error = got_error_from_errno("asprintf");
1663 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1666 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1671 if (commit_id_str == NULL) {
1672 struct got_reference *head_ref;
1673 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1676 error = got_ref_resolve(&commit_id, repo, head_ref);
1677 got_ref_close(head_ref);
1681 error = got_repo_match_object_id_prefix(&commit_id,
1682 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1687 error = got_blame(in_repo_path, commit_id, repo, stdout);
1694 got_worktree_close(worktree);
1696 const struct got_error *repo_error;
1697 repo_error = got_repo_close(repo);
1708 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1714 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1715 const char *root_path)
1717 int is_root_path = (strcmp(path, root_path) == 0);
1719 path += strlen(root_path);
1720 while (path[0] == '/')
1723 printf("%s%s%s%s%s\n", id ? id : "", path,
1724 is_root_path ? "" : "/", te->name,
1725 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1728 static const struct got_error *
1729 print_tree(const char *path, struct got_object_id *commit_id,
1730 int show_ids, int recurse, const char *root_path,
1731 struct got_repository *repo)
1733 const struct got_error *err = NULL;
1734 struct got_object_id *tree_id = NULL;
1735 struct got_tree_object *tree = NULL;
1736 const struct got_tree_entries *entries;
1737 struct got_tree_entry *te;
1739 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1743 err = got_object_open_as_tree(&tree, repo, tree_id);
1746 entries = got_object_tree_get_entries(tree);
1747 te = SIMPLEQ_FIRST(&entries->head);
1751 if (sigint_received || sigpipe_received)
1756 err = got_object_id_str(&id_str, te->id);
1759 if (asprintf(&id, "%s ", id_str) == -1) {
1760 err = got_error_from_errno("asprintf");
1766 print_entry(te, id, path, root_path);
1769 if (recurse && S_ISDIR(te->mode)) {
1771 if (asprintf(&child_path, "%s%s%s", path,
1772 path[0] == '/' && path[1] == '\0' ? "" : "/",
1774 err = got_error_from_errno("asprintf");
1777 err = print_tree(child_path, commit_id, show_ids, 1,
1784 te = SIMPLEQ_NEXT(te, entry);
1788 got_object_tree_close(tree);
1793 static const struct got_error *
1794 cmd_tree(int argc, char *argv[])
1796 const struct got_error *error;
1797 struct got_repository *repo = NULL;
1798 struct got_worktree *worktree = NULL;
1800 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1801 struct got_object_id *commit_id = NULL;
1802 char *commit_id_str = NULL;
1803 int show_ids = 0, recurse = 0;
1807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1812 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1815 commit_id_str = optarg;
1818 repo_path = realpath(optarg, NULL);
1819 if (repo_path == NULL)
1820 err(1, "-r option");
1821 got_path_strip_trailing_slashes(repo_path);
1845 cwd = getcwd(NULL, 0);
1847 error = got_error_from_errno("getcwd");
1850 if (repo_path == NULL) {
1851 error = got_worktree_open(&worktree, cwd);
1852 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1858 strdup(got_worktree_get_repo_path(worktree));
1859 if (repo_path == NULL)
1860 error = got_error_from_errno("strdup");
1864 repo_path = strdup(cwd);
1865 if (repo_path == NULL) {
1866 error = got_error_from_errno("strdup");
1872 error = got_repo_open(&repo, repo_path);
1876 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1882 char *p, *worktree_subdir = cwd +
1883 strlen(got_worktree_get_root_path(worktree));
1884 if (asprintf(&p, "%s/%s",
1885 got_worktree_get_path_prefix(worktree),
1886 worktree_subdir) == -1) {
1887 error = got_error_from_errno("asprintf");
1890 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1897 if (in_repo_path == NULL) {
1898 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1903 if (commit_id_str == NULL) {
1904 struct got_reference *head_ref;
1905 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1908 error = got_ref_resolve(&commit_id, repo, head_ref);
1909 got_ref_close(head_ref);
1913 error = got_repo_match_object_id_prefix(&commit_id,
1914 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1919 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1920 in_repo_path, repo);
1927 got_worktree_close(worktree);
1929 const struct got_error *repo_error;
1930 repo_error = got_repo_close(repo);
1940 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1944 static const struct got_error *
1945 print_status(void *arg, unsigned char status, const char *path,
1946 struct got_object_id *blob_id, struct got_object_id *commit_id)
1948 printf("%c %s\n", status, path);
1952 static const struct got_error *
1953 cmd_status(int argc, char *argv[])
1955 const struct got_error *error = NULL;
1956 struct got_repository *repo = NULL;
1957 struct got_worktree *worktree = NULL;
1958 char *cwd = NULL, *path = NULL;
1961 while ((ch = getopt(argc, argv, "")) != -1) {
1973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1977 cwd = getcwd(NULL, 0);
1979 error = got_error_from_errno("getcwd");
1983 error = got_worktree_open(&worktree, cwd);
1990 error = got_error_from_errno("strdup");
1993 } else if (argc == 1) {
1994 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2000 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2004 error = apply_unveil(got_repo_get_path(repo), 1,
2005 got_worktree_get_root_path(worktree), 0);
2009 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2010 check_cancelled, NULL);
2021 "usage: %s ref [-r repository] -l | -d name | name target\n",
2026 static const struct got_error *
2027 list_refs(struct got_repository *repo)
2029 static const struct got_error *err = NULL;
2030 struct got_reflist_head refs;
2031 struct got_reflist_entry *re;
2033 SIMPLEQ_INIT(&refs);
2034 err = got_ref_list(&refs, repo);
2038 SIMPLEQ_FOREACH(re, &refs, entry) {
2040 refstr = got_ref_to_str(re->ref);
2042 return got_error_from_errno("got_ref_to_str");
2043 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2047 got_ref_list_free(&refs);
2051 static const struct got_error *
2052 delete_ref(struct got_repository *repo, const char *refname)
2054 const struct got_error *err = NULL;
2055 struct got_reference *ref;
2057 err = got_ref_open(&ref, repo, refname, 0);
2061 err = got_ref_delete(ref, repo);
2066 static const struct got_error *
2067 add_ref(struct got_repository *repo, const char *refname, const char *target)
2069 const struct got_error *err = NULL;
2070 struct got_object_id *id;
2071 struct got_reference *ref = NULL;
2073 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2076 struct got_reference *target_ref;
2078 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2080 err = got_ref_open(&target_ref, repo, target, 0);
2083 err = got_ref_resolve(&id, repo, target_ref);
2084 got_ref_close(target_ref);
2089 err = got_ref_alloc(&ref, refname, id);
2093 err = got_ref_write(ref, repo);
2101 static const struct got_error *
2102 cmd_ref(int argc, char *argv[])
2104 const struct got_error *error = NULL;
2105 struct got_repository *repo = NULL;
2106 struct got_worktree *worktree = NULL;
2107 char *cwd = NULL, *repo_path = NULL;
2108 int ch, do_list = 0;
2109 const char *delref = NULL;
2111 /* TODO: Add -s option for adding symbolic references. */
2112 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2118 repo_path = realpath(optarg, NULL);
2119 if (repo_path == NULL)
2120 err(1, "-r option");
2121 got_path_strip_trailing_slashes(repo_path);
2132 if (do_list && delref)
2133 errx(1, "-l and -d options are mutually exclusive\n");
2138 if (do_list || delref) {
2141 } else if (argc != 2)
2146 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2150 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2151 "sendfd unveil", NULL) == -1)
2155 cwd = getcwd(NULL, 0);
2157 error = got_error_from_errno("getcwd");
2161 if (repo_path == NULL) {
2162 error = got_worktree_open(&worktree, cwd);
2163 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2169 strdup(got_worktree_get_repo_path(worktree));
2170 if (repo_path == NULL)
2171 error = got_error_from_errno("strdup");
2175 repo_path = strdup(cwd);
2176 if (repo_path == NULL) {
2177 error = got_error_from_errno("strdup");
2183 error = got_repo_open(&repo, repo_path);
2187 error = apply_unveil(got_repo_get_path(repo), do_list,
2188 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2193 error = list_refs(repo);
2195 error = delete_ref(repo, delref);
2197 error = add_ref(repo, argv[0], argv[1]);
2200 got_repo_close(repo);
2202 got_worktree_close(worktree);
2212 "usage: %s branch [-r repository] -l | -d name | "
2213 "name [base-branch]\n", getprogname());
2217 static const struct got_error *
2218 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2220 static const struct got_error *err = NULL;
2221 struct got_reflist_head refs;
2222 struct got_reflist_entry *re;
2224 SIMPLEQ_INIT(&refs);
2226 err = got_ref_list(&refs, repo);
2230 SIMPLEQ_FOREACH(re, &refs, entry) {
2231 const char *refname, *marker = " ";
2233 refname = got_ref_get_name(re->ref);
2234 if (strncmp(refname, "refs/heads/", 11) != 0)
2236 if (worktree && strcmp(refname,
2237 got_worktree_get_head_ref_name(worktree)) == 0) {
2238 struct got_object_id *id = NULL;
2239 err = got_ref_resolve(&id, repo, re->ref);
2242 if (got_object_id_cmp(id,
2243 got_worktree_get_base_commit_id(worktree)) == 0)
2250 refstr = got_ref_to_str(re->ref);
2252 return got_error_from_errno("got_ref_to_str");
2253 printf("%s%s: %s\n", marker, refname, refstr);
2257 got_ref_list_free(&refs);
2261 static const struct got_error *
2262 delete_branch(struct got_repository *repo, const char *branch_name)
2264 const struct got_error *err = NULL;
2265 struct got_reference *ref;
2268 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2269 return got_error_from_errno("asprintf");
2271 err = got_ref_open(&ref, repo, refname, 0);
2275 err = got_ref_delete(ref, repo);
2282 static const struct got_error *
2283 add_branch(struct got_repository *repo, const char *branch_name,
2284 const char *base_branch)
2286 const struct got_error *err = NULL;
2287 struct got_object_id *id = NULL;
2288 struct got_reference *ref = NULL;
2289 char *base_refname = NULL, *refname = NULL;
2290 struct got_reference *base_ref;
2293 * Don't let the user create a branch named '-'.
2294 * While technically a valid reference name, this case is usually
2295 * an unintended typo.
2297 if (branch_name[0] == '-' && branch_name[1] == '\0')
2298 return got_error(GOT_ERR_BAD_REF_NAME);
2300 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2301 base_refname = strdup(GOT_REF_HEAD);
2302 if (base_refname == NULL)
2303 return got_error_from_errno("strdup");
2304 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2305 return got_error_from_errno("asprintf");
2307 err = got_ref_open(&base_ref, repo, base_refname, 0);
2310 err = got_ref_resolve(&id, repo, base_ref);
2311 got_ref_close(base_ref);
2315 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2316 err = got_error_from_errno("asprintf");
2320 err = got_ref_open(&ref, repo, refname, 0);
2322 err = got_error(GOT_ERR_BRANCH_EXISTS);
2324 } else if (err->code != GOT_ERR_NOT_REF)
2327 err = got_ref_alloc(&ref, refname, id);
2331 err = got_ref_write(ref, repo);
2341 static const struct got_error *
2342 cmd_branch(int argc, char *argv[])
2344 const struct got_error *error = NULL;
2345 struct got_repository *repo = NULL;
2346 struct got_worktree *worktree = NULL;
2347 char *cwd = NULL, *repo_path = NULL;
2348 int ch, do_list = 0;
2349 const char *delref = NULL;
2351 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2357 repo_path = realpath(optarg, NULL);
2358 if (repo_path == NULL)
2359 err(1, "-r option");
2360 got_path_strip_trailing_slashes(repo_path);
2371 if (do_list && delref)
2372 errx(1, "-l and -d options are mutually exclusive\n");
2377 if (do_list || delref) {
2380 } else if (argc < 1 || argc > 2)
2385 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2389 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2390 "sendfd unveil", NULL) == -1)
2394 cwd = getcwd(NULL, 0);
2396 error = got_error_from_errno("getcwd");
2400 if (repo_path == NULL) {
2401 error = got_worktree_open(&worktree, cwd);
2402 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2408 strdup(got_worktree_get_repo_path(worktree));
2409 if (repo_path == NULL)
2410 error = got_error_from_errno("strdup");
2414 repo_path = strdup(cwd);
2415 if (repo_path == NULL) {
2416 error = got_error_from_errno("strdup");
2422 error = got_repo_open(&repo, repo_path);
2426 error = apply_unveil(got_repo_get_path(repo), do_list,
2427 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2432 error = list_branches(repo, worktree);
2434 error = delete_branch(repo, delref);
2436 const char *base_branch;
2438 base_branch = worktree ?
2439 got_worktree_get_head_ref_name(worktree) :
2442 base_branch = argv[1];
2443 error = add_branch(repo, argv[0], base_branch);
2447 got_repo_close(repo);
2449 got_worktree_close(worktree);
2458 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2462 static const struct got_error *
2463 cmd_add(int argc, char *argv[])
2465 const struct got_error *error = NULL;
2466 struct got_repository *repo = NULL;
2467 struct got_worktree *worktree = NULL;
2469 struct got_pathlist_head paths;
2470 struct got_pathlist_entry *pe;
2475 while ((ch = getopt(argc, argv, "")) != -1) {
2489 /* make sure each file exists before doing anything halfway */
2490 for (x = 0; x < argc; x++) {
2491 char *path = realpath(argv[x], NULL);
2493 error = got_error_from_errno2("realpath", argv[x]);
2499 cwd = getcwd(NULL, 0);
2501 error = got_error_from_errno("getcwd");
2505 error = got_worktree_open(&worktree, cwd);
2509 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2513 error = apply_unveil(got_repo_get_path(repo), 1,
2514 got_worktree_get_root_path(worktree), 0);
2518 for (x = 0; x < argc; x++) {
2519 char *path = realpath(argv[x], NULL);
2521 error = got_error_from_errno2("realpath", argv[x]);
2525 got_path_strip_trailing_slashes(path);
2526 error = got_pathlist_insert(&pe, &paths, path, NULL);
2532 error = got_worktree_schedule_add(worktree, &paths, print_status,
2536 got_repo_close(repo);
2538 got_worktree_close(worktree);
2539 TAILQ_FOREACH(pe, &paths, entry)
2540 free((char *)pe->path);
2541 got_pathlist_free(&paths);
2549 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2553 static const struct got_error *
2554 cmd_remove(int argc, char *argv[])
2556 const struct got_error *error = NULL;
2557 struct got_worktree *worktree = NULL;
2558 struct got_repository *repo = NULL;
2560 struct got_pathlist_head paths;
2561 struct got_pathlist_entry *pe;
2562 int ch, i, delete_local_mods = 0;
2566 while ((ch = getopt(argc, argv, "f")) != -1) {
2569 delete_local_mods = 1;
2583 /* make sure each file exists before doing anything halfway */
2584 for (i = 0; i < argc; i++) {
2585 char *path = realpath(argv[i], NULL);
2587 error = got_error_from_errno2("realpath", argv[i]);
2593 cwd = getcwd(NULL, 0);
2595 error = got_error_from_errno("getcwd");
2598 error = got_worktree_open(&worktree, cwd);
2602 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2606 error = apply_unveil(got_repo_get_path(repo), 1,
2607 got_worktree_get_root_path(worktree), 0);
2611 for (i = 0; i < argc; i++) {
2612 char *path = realpath(argv[i], NULL);
2614 error = got_error_from_errno2("realpath", argv[i]);
2618 got_path_strip_trailing_slashes(path);
2619 error = got_pathlist_insert(&pe, &paths, path, NULL);
2625 error = got_worktree_schedule_delete(worktree, &paths,
2626 delete_local_mods, print_status, NULL, repo);
2631 got_repo_close(repo);
2633 got_worktree_close(worktree);
2634 TAILQ_FOREACH(pe, &paths, entry)
2635 free((char *)pe->path);
2636 got_pathlist_free(&paths);
2644 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2649 revert_progress(void *arg, unsigned char status, const char *path)
2651 while (path[0] == '/')
2653 printf("%c %s\n", status, path);
2656 static const struct got_error *
2657 cmd_revert(int argc, char *argv[])
2659 const struct got_error *error = NULL;
2660 struct got_worktree *worktree = NULL;
2661 struct got_repository *repo = NULL;
2662 char *cwd = NULL, *path = NULL;
2663 struct got_pathlist_head paths;
2664 struct got_pathlist_entry *pe;
2669 while ((ch = getopt(argc, argv, "")) != -1) {
2683 for (i = 0; i < argc; i++) {
2684 char *path = realpath(argv[i], NULL);
2686 error = got_error_from_errno2("realpath", argv[i]);
2690 got_path_strip_trailing_slashes(path);
2691 error = got_pathlist_insert(&pe, &paths, path, NULL);
2698 cwd = getcwd(NULL, 0);
2700 error = got_error_from_errno("getcwd");
2703 error = got_worktree_open(&worktree, cwd);
2707 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2711 error = apply_unveil(got_repo_get_path(repo), 1,
2712 got_worktree_get_root_path(worktree), 0);
2716 error = got_worktree_revert(worktree, &paths,
2717 revert_progress, NULL, repo);
2722 got_repo_close(repo);
2724 got_worktree_close(worktree);
2733 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2738 spawn_editor(const char *editor, const char *file)
2741 sig_t sighup, sigint, sigquit;
2744 sighup = signal(SIGHUP, SIG_IGN);
2745 sigint = signal(SIGINT, SIG_IGN);
2746 sigquit = signal(SIGQUIT, SIG_IGN);
2748 switch (pid = fork()) {
2752 execl(editor, editor, file, (char *)NULL);
2756 while (waitpid(pid, &st, 0) == -1)
2761 (void)signal(SIGHUP, sighup);
2762 (void)signal(SIGINT, sigint);
2763 (void)signal(SIGQUIT, sigquit);
2765 if (!WIFEXITED(st)) {
2770 return WEXITSTATUS(st);
2773 struct collect_commit_logmsg_arg {
2774 const char *cmdline_log;
2776 const char *worktree_path;
2777 const char *branch_name;
2778 const char *repo_path;
2783 static const struct got_error *
2784 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2787 char *initial_content = NULL;
2788 struct got_pathlist_entry *pe;
2789 const struct got_error *err = NULL;
2790 char *template = NULL;
2791 struct collect_commit_logmsg_arg *a = arg;
2793 struct stat st, st2;
2796 int fd, content_changed = 0;
2798 /* if a message was specified on the command line, just use it */
2799 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2800 len = strlen(a->cmdline_log) + 1;
2801 *logmsg = malloc(len + 1);
2802 if (*logmsg == NULL)
2803 return got_error_from_errno("malloc");
2804 strlcpy(*logmsg, a->cmdline_log, len);
2808 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2809 return got_error_from_errno("asprintf");
2811 if (asprintf(&initial_content,
2812 "\n# changes to be committed on branch %s:\n",
2813 a->branch_name) == -1)
2814 return got_error_from_errno("asprintf");
2816 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2820 dprintf(fd, initial_content);
2822 TAILQ_FOREACH(pe, commitable_paths, entry) {
2823 struct got_commitable *ct = pe->data;
2824 dprintf(fd, "# %c %s\n",
2825 got_commitable_get_status(ct),
2826 got_commitable_get_path(ct));
2830 if (stat(a->logmsg_path, &st) == -1) {
2831 err = got_error_from_errno2("stat", a->logmsg_path);
2835 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2836 err = got_error_from_errno("failed spawning editor");
2840 if (stat(a->logmsg_path, &st2) == -1) {
2841 err = got_error_from_errno("stat");
2845 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2846 unlink(a->logmsg_path);
2847 free(a->logmsg_path);
2848 a->logmsg_path = NULL;
2849 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2850 "no changes made to commit message, aborting");
2854 *logmsg = malloc(st2.st_size + 1);
2855 if (*logmsg == NULL) {
2856 err = got_error_from_errno("malloc");
2859 (*logmsg)[0] = '\0';
2862 fp = fopen(a->logmsg_path, "r");
2864 err = got_error_from_errno("fopen");
2867 while (fgets(buf, sizeof(buf), fp) != NULL) {
2868 if (!content_changed && strcmp(buf, initial_content) != 0)
2869 content_changed = 1;
2870 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2871 continue; /* remove comments and leading empty lines */
2872 len = strlcat(*logmsg, buf, st2.st_size);
2876 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2877 (*logmsg)[len - 1] = '\0';
2881 if (len == 0 || !content_changed) {
2882 unlink(a->logmsg_path);
2883 free(a->logmsg_path);
2884 a->logmsg_path = NULL;
2885 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2886 "commit message cannot be empty, aborting");
2890 free(initial_content);
2893 /* Editor is done; we can now apply unveil(2) */
2895 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2899 static const struct got_error *
2900 cmd_commit(int argc, char *argv[])
2902 const struct got_error *error = NULL;
2903 struct got_worktree *worktree = NULL;
2904 struct got_repository *repo = NULL;
2905 char *cwd = NULL, *path = NULL, *id_str = NULL;
2906 struct got_object_id *id = NULL;
2907 const char *logmsg = NULL;
2908 const char *got_author = getenv("GOT_AUTHOR");
2909 struct collect_commit_logmsg_arg cl_arg;
2910 char *editor = NULL;
2911 int ch, rebase_in_progress;
2913 while ((ch = getopt(argc, argv, "m:")) != -1) {
2928 path = realpath(argv[0], NULL);
2930 error = got_error_from_errno2("realpath", argv[0]);
2933 got_path_strip_trailing_slashes(path);
2934 } else if (argc != 0)
2937 if (got_author == NULL) {
2938 /* TODO: Look current user up in password database */
2939 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2943 cwd = getcwd(NULL, 0);
2945 error = got_error_from_errno("getcwd");
2948 error = got_worktree_open(&worktree, cwd);
2952 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2955 if (rebase_in_progress) {
2956 error = got_error(GOT_ERR_REBASING);
2960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2965 * unveil(2) traverses exec(2); if an editor is used we have
2966 * to apply unveil after the log message has been written.
2968 if (logmsg == NULL || strlen(logmsg) == 0)
2969 error = get_editor(&editor);
2971 error = apply_unveil(got_repo_get_path(repo), 0,
2972 got_worktree_get_root_path(worktree), 0);
2976 cl_arg.editor = editor;
2977 cl_arg.cmdline_log = logmsg;
2978 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2979 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2980 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2981 cl_arg.branch_name += 5;
2982 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2983 cl_arg.branch_name += 6;
2984 cl_arg.repo_path = got_repo_get_path(repo);
2985 cl_arg.logmsg_path = NULL;
2986 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2987 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2989 if (cl_arg.logmsg_path)
2990 fprintf(stderr, "%s: log message preserved in %s\n",
2991 getprogname(), cl_arg.logmsg_path);
2995 if (cl_arg.logmsg_path)
2996 unlink(cl_arg.logmsg_path);
2998 error = got_object_id_str(&id_str, id);
3001 printf("Created commit %s\n", id_str);
3004 got_repo_close(repo);
3006 got_worktree_close(worktree);
3015 usage_cherrypick(void)
3017 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3021 static const struct got_error *
3022 cmd_cherrypick(int argc, char *argv[])
3024 const struct got_error *error = NULL;
3025 struct got_worktree *worktree = NULL;
3026 struct got_repository *repo = NULL;
3027 char *cwd = NULL, *commit_id_str = NULL;
3028 struct got_object_id *commit_id = NULL;
3029 struct got_commit_object *commit = NULL;
3030 struct got_object_qid *pid;
3031 struct got_reference *head_ref = NULL;
3032 int ch, did_something = 0;
3034 while ((ch = getopt(argc, argv, "")) != -1) {
3048 cwd = getcwd(NULL, 0);
3050 error = got_error_from_errno("getcwd");
3053 error = got_worktree_open(&worktree, cwd);
3057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3061 error = apply_unveil(got_repo_get_path(repo), 0,
3062 got_worktree_get_root_path(worktree), 0);
3066 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3067 GOT_OBJ_TYPE_COMMIT, repo);
3068 if (error != NULL) {
3069 struct got_reference *ref;
3070 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3072 error = got_ref_open(&ref, repo, argv[0], 0);
3075 error = got_ref_resolve(&commit_id, repo, ref);
3080 error = got_object_id_str(&commit_id_str, commit_id);
3084 error = got_ref_open(&head_ref, repo,
3085 got_worktree_get_head_ref_name(worktree), 0);
3089 error = check_same_branch(commit_id, head_ref, repo);
3091 if (error->code != GOT_ERR_ANCESTRY)
3095 error = got_error(GOT_ERR_SAME_BRANCH);
3099 error = got_object_open_as_commit(&commit, repo, commit_id);
3102 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3103 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3104 commit_id, repo, update_progress, &did_something, check_cancelled,
3110 printf("Merged commit %s\n", commit_id_str);
3113 got_object_commit_close(commit);
3114 free(commit_id_str);
3116 got_ref_close(head_ref);
3118 got_worktree_close(worktree);
3120 got_repo_close(repo);
3127 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3131 static const struct got_error *
3132 cmd_backout(int argc, char *argv[])
3134 const struct got_error *error = NULL;
3135 struct got_worktree *worktree = NULL;
3136 struct got_repository *repo = NULL;
3137 char *cwd = NULL, *commit_id_str = NULL;
3138 struct got_object_id *commit_id = NULL;
3139 struct got_commit_object *commit = NULL;
3140 struct got_object_qid *pid;
3141 struct got_reference *head_ref = NULL;
3142 int ch, did_something = 0;
3144 while ((ch = getopt(argc, argv, "")) != -1) {
3158 cwd = getcwd(NULL, 0);
3160 error = got_error_from_errno("getcwd");
3163 error = got_worktree_open(&worktree, cwd);
3167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3171 error = apply_unveil(got_repo_get_path(repo), 0,
3172 got_worktree_get_root_path(worktree), 0);
3176 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3177 GOT_OBJ_TYPE_COMMIT, repo);
3178 if (error != NULL) {
3179 struct got_reference *ref;
3180 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3182 error = got_ref_open(&ref, repo, argv[0], 0);
3185 error = got_ref_resolve(&commit_id, repo, ref);
3190 error = got_object_id_str(&commit_id_str, commit_id);
3194 error = got_ref_open(&head_ref, repo,
3195 got_worktree_get_head_ref_name(worktree), 0);
3199 error = check_same_branch(commit_id, head_ref, repo);
3203 error = got_object_open_as_commit(&commit, repo, commit_id);
3206 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3208 error = got_error(GOT_ERR_ROOT_COMMIT);
3212 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3213 update_progress, &did_something, check_cancelled, NULL);
3218 printf("Backed out commit %s\n", commit_id_str);
3221 got_object_commit_close(commit);
3222 free(commit_id_str);
3224 got_ref_close(head_ref);
3226 got_worktree_close(worktree);
3228 got_repo_close(repo);
3235 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3240 static const struct got_error *
3241 show_rebase_progress(struct got_commit_object *commit,
3242 struct got_object_id *old_id, struct got_object_id *new_id)
3244 const struct got_error *err;
3245 char *old_id_str = NULL, *new_id_str = NULL;
3246 char *logmsg0 = NULL, *logmsg, *nl;
3249 err = got_object_id_str(&old_id_str, old_id);
3254 err = got_object_id_str(&new_id_str, new_id);
3259 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3260 if (logmsg0 == NULL) {
3261 err = got_error_from_errno("strdup");
3266 while (isspace((unsigned char)logmsg[0]))
3269 old_id_str[12] = '\0';
3271 new_id_str[12] = '\0';
3272 len = strlen(logmsg);
3276 nl = strchr(logmsg, '\n');
3279 printf("%s -> %s: %s\n", old_id_str,
3280 new_id_str ? new_id_str : "no-op change", logmsg);
3289 rebase_progress(void *arg, unsigned char status, const char *path)
3291 unsigned char *rebase_status = arg;
3293 while (path[0] == '/')
3295 printf("%c %s\n", status, path);
3297 if (*rebase_status == GOT_STATUS_CONFLICT)
3299 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3300 *rebase_status = status;
3303 static const struct got_error *
3304 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3305 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3306 struct got_repository *repo)
3308 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3309 return got_worktree_rebase_complete(worktree,
3310 new_base_branch, tmp_branch, branch, repo);
3313 static const struct got_error *
3314 rebase_commit(struct got_worktree *worktree, struct got_reference *tmp_branch,
3315 struct got_object_id *commit_id, struct got_repository *repo)
3317 const struct got_error *error;
3318 struct got_commit_object *commit;
3319 struct got_object_id *new_commit_id;
3321 error = got_object_open_as_commit(&commit, repo, commit_id);
3325 error = got_worktree_rebase_commit(&new_commit_id, worktree,
3326 tmp_branch, commit, commit_id, repo);
3328 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3330 error = show_rebase_progress(commit, commit_id, NULL);
3332 error = show_rebase_progress(commit, commit_id, new_commit_id);
3333 free(new_commit_id);
3336 got_object_commit_close(commit);
3340 struct check_path_prefix_arg {
3341 const char *path_prefix;
3345 static const struct got_error *
3346 check_path_prefix(void *arg, struct got_blob_object *blob1,
3347 struct got_blob_object *blob2, struct got_object_id *id1,
3348 struct got_object_id *id2, const char *path1, const char *path2,
3349 struct got_repository *repo)
3351 struct check_path_prefix_arg *a = arg;
3353 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3354 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3355 return got_error(GOT_ERR_REBASE_PATH);
3360 static const struct got_error *
3361 rebase_check_path_prefix(struct got_object_id *parent_id,
3362 struct got_object_id *commit_id, const char *path_prefix,
3363 struct got_repository *repo)
3365 const struct got_error *err;
3366 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3367 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3368 struct check_path_prefix_arg cpp_arg;
3370 if (got_path_is_root_dir(path_prefix))
3373 err = got_object_open_as_commit(&commit, repo, commit_id);
3377 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3381 err = got_object_open_as_tree(&tree1, repo,
3382 got_object_commit_get_tree_id(parent_commit));
3386 err = got_object_open_as_tree(&tree2, repo,
3387 got_object_commit_get_tree_id(commit));
3391 cpp_arg.path_prefix = path_prefix;
3392 cpp_arg.len = strlen(path_prefix);
3393 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3397 got_object_tree_close(tree1);
3399 got_object_tree_close(tree2);
3401 got_object_commit_close(commit);
3403 got_object_commit_close(parent_commit);
3407 static const struct got_error *
3408 cmd_rebase(int argc, char *argv[])
3410 const struct got_error *error = NULL;
3411 struct got_worktree *worktree = NULL;
3412 struct got_repository *repo = NULL;
3414 struct got_reference *branch = NULL;
3415 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3416 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3417 struct got_object_id *resume_commit_id = NULL;
3418 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3419 struct got_commit_graph *graph = NULL;
3420 struct got_commit_object *commit = NULL;
3421 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3422 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3423 struct got_object_id_queue commits;
3424 const struct got_object_id_queue *parent_ids;
3425 struct got_object_qid *qid, *pid;
3427 SIMPLEQ_INIT(&commits);
3429 while ((ch = getopt(argc, argv, "ac")) != -1) {
3435 continue_rebase = 1;
3446 if (abort_rebase && continue_rebase)
3448 else if (abort_rebase || continue_rebase) {
3451 } else if (argc != 1)
3454 cwd = getcwd(NULL, 0);
3456 error = got_error_from_errno("getcwd");
3459 error = got_worktree_open(&worktree, cwd);
3463 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3467 error = apply_unveil(got_repo_get_path(repo), 0,
3468 got_worktree_get_root_path(worktree), 0);
3472 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3476 if (rebase_in_progress && abort_rebase) {
3478 error = got_worktree_rebase_continue(&resume_commit_id,
3479 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3482 printf("Switching work tree to %s\n",
3483 got_ref_get_symref_target(new_base_branch));
3484 error = got_worktree_rebase_abort(worktree, repo,
3485 new_base_branch, update_progress, &did_something);
3488 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3489 goto done; /* nothing else to do */
3490 } else if (abort_rebase) {
3491 error = got_error(GOT_ERR_NOT_REBASING);
3495 if (continue_rebase) {
3496 error = got_worktree_rebase_continue(&resume_commit_id,
3497 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3501 error = rebase_commit(worktree, tmp_branch, resume_commit_id,
3506 yca_id = got_object_id_dup(resume_commit_id);
3507 if (yca_id == NULL) {
3508 error = got_error_from_errno("got_object_id_dup");
3512 error = got_ref_open(&branch, repo, argv[0], 0);
3516 error = check_same_branch(
3517 got_worktree_get_base_commit_id(worktree), branch, repo);
3519 if (error->code != GOT_ERR_ANCESTRY)
3523 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3524 "specified branch resolves to a commit which "
3525 "is already contained in work tree's branch");
3530 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3534 if (!continue_rebase) {
3535 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3536 got_worktree_get_base_commit_id(worktree),
3537 branch_head_commit_id, repo);
3540 if (yca_id == NULL) {
3541 error = got_error_msg(GOT_ERR_ANCESTRY,
3542 "specified branch shares no common ancestry "
3543 "with work tree's branch");
3547 error = got_worktree_rebase_prepare(&new_base_branch,
3548 &tmp_branch, worktree, branch, repo);
3553 commit_id = branch_head_commit_id;
3554 error = got_object_open_as_commit(&commit, repo, commit_id);
3558 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3561 parent_ids = got_object_commit_get_parent_ids(commit);
3562 pid = SIMPLEQ_FIRST(parent_ids);
3563 error = got_commit_graph_iter_start(graph, pid->id, repo);
3564 got_object_commit_close(commit);
3568 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3569 error = got_commit_graph_iter_next(&parent_id, graph);
3571 if (error->code == GOT_ERR_ITER_COMPLETED) {
3572 error = got_error_msg(GOT_ERR_ANCESTRY,
3573 "ran out of commits to rebase before "
3574 "youngest common ancestor commit has "
3577 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3579 error = got_commit_graph_fetch_commits(graph, 1, repo);
3583 error = rebase_check_path_prefix(parent_id, commit_id,
3584 got_worktree_get_path_prefix(worktree), repo);
3588 error = got_object_qid_alloc(&qid, commit_id);
3591 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3592 commit_id = parent_id;
3596 if (SIMPLEQ_EMPTY(&commits)) {
3597 if (continue_rebase)
3598 error = rebase_complete(worktree, branch,
3599 new_base_branch, tmp_branch, repo);
3601 error = got_error(GOT_ERR_EMPTY_REBASE);
3606 SIMPLEQ_FOREACH(qid, &commits, entry) {
3607 commit_id = qid->id;
3608 parent_id = pid ? pid->id : yca_id;
3611 error = got_worktree_rebase_merge_files(worktree, parent_id,
3612 commit_id, repo, rebase_progress, &rebase_status,
3613 check_cancelled, NULL);
3617 if (rebase_status == GOT_STATUS_CONFLICT)
3620 error = rebase_commit(worktree, tmp_branch, commit_id, repo);
3625 if (rebase_status == GOT_STATUS_CONFLICT) {
3626 error = got_worktree_rebase_postpone(worktree);
3629 error = got_error_msg(GOT_ERR_CONFLICTS,
3630 "conflicts must be resolved before rebase can be resumed");
3632 error = rebase_complete(worktree, branch, new_base_branch,
3635 got_object_id_queue_free(&commits);
3636 free(branch_head_commit_id);
3637 free(resume_commit_id);
3640 got_commit_graph_close(graph);
3642 got_object_commit_close(commit);
3644 got_ref_close(branch);
3645 if (new_base_branch)
3646 got_ref_close(new_base_branch);
3648 got_ref_close(tmp_branch);
3650 got_worktree_close(worktree);
3652 got_repo_close(repo);