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());
345 static const struct got_error *
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);
361 static const struct got_error *
362 check_cancelled(void *arg)
364 if (sigint_received || sigpipe_received)
365 return got_error(GOT_ERR_CANCELLED);
369 static const struct got_error *
370 check_linear_ancestry(struct got_object_id *commit_id,
371 struct got_object_id *base_commit_id, struct got_repository *repo)
373 const struct got_error *err = NULL;
374 struct got_object_id *yca_id;
376 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
377 commit_id, base_commit_id, repo);
382 return got_error(GOT_ERR_ANCESTRY);
385 * Require a straight line of history between the target commit
386 * and the work tree's base commit.
388 * Non-linear situations such as this require a rebase:
390 * (commit) D F (base_commit)
398 * 'got update' only handles linear cases:
399 * Update forwards in time: A (base/yca) - B - C - D (commit)
400 * Update backwards in time: D (base) - C - B - A (commit/yca)
402 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
403 got_object_id_cmp(base_commit_id, yca_id) != 0)
404 return got_error(GOT_ERR_ANCESTRY);
410 static const struct got_error *
411 check_same_branch(struct got_object_id *commit_id,
412 struct got_reference *head_ref, struct got_repository *repo)
414 const struct got_error *err = NULL;
415 struct got_commit_graph *graph = NULL;
416 struct got_object_id *head_commit_id = NULL;
417 int is_same_branch = 0;
419 err = got_ref_resolve(&head_commit_id, repo, head_ref);
423 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
427 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
432 struct got_object_id *id;
433 err = got_commit_graph_iter_next(&id, graph);
435 if (err->code == GOT_ERR_ITER_COMPLETED) {
439 else if (err->code != GOT_ERR_ITER_NEED_MORE)
441 err = got_commit_graph_fetch_commits(graph, 1,
448 if (got_object_id_cmp(id, commit_id) == 0) {
456 got_commit_graph_close(graph);
457 free(head_commit_id);
458 if (!err && !is_same_branch)
459 err = got_error(GOT_ERR_ANCESTRY);
463 static const struct got_error *
464 cmd_checkout(int argc, char *argv[])
466 const struct got_error *error = NULL;
467 struct got_repository *repo = NULL;
468 struct got_reference *head_ref = NULL;
469 struct got_worktree *worktree = NULL;
470 char *repo_path = NULL;
471 char *worktree_path = NULL;
472 const char *path_prefix = "";
473 const char *branch_name = GOT_REF_HEAD;
474 char *commit_id_str = NULL;
475 int ch, same_path_prefix;
477 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
480 branch_name = optarg;
483 commit_id_str = strdup(optarg);
484 if (commit_id_str == NULL)
485 return got_error_from_errno("strdup");
488 path_prefix = optarg;
500 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
501 "unveil", NULL) == -1)
505 char *cwd, *base, *dotgit;
506 repo_path = realpath(argv[0], NULL);
507 if (repo_path == NULL)
508 return got_error_from_errno2("realpath", argv[0]);
509 cwd = getcwd(NULL, 0);
511 error = got_error_from_errno("getcwd");
514 if (path_prefix[0]) {
515 base = basename(path_prefix);
517 error = got_error_from_errno2("basename",
522 base = basename(repo_path);
524 error = got_error_from_errno2("basename",
529 dotgit = strstr(base, ".git");
532 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
533 error = got_error_from_errno("asprintf");
538 } else if (argc == 2) {
539 repo_path = realpath(argv[0], NULL);
540 if (repo_path == NULL) {
541 error = got_error_from_errno2("realpath", argv[0]);
544 worktree_path = realpath(argv[1], NULL);
545 if (worktree_path == NULL) {
546 error = got_error_from_errno2("realpath", argv[1]);
552 got_path_strip_trailing_slashes(repo_path);
553 got_path_strip_trailing_slashes(worktree_path);
555 error = got_repo_open(&repo, repo_path);
559 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
563 error = got_ref_open(&head_ref, repo, branch_name, 0);
567 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
568 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
571 error = got_worktree_open(&worktree, worktree_path);
575 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
579 if (!same_path_prefix) {
580 error = got_error(GOT_ERR_PATH_PREFIX);
585 struct got_object_id *commit_id;
586 error = got_repo_match_object_id_prefix(&commit_id,
587 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
590 error = check_linear_ancestry(commit_id,
591 got_worktree_get_base_commit_id(worktree), repo);
596 error = check_same_branch(commit_id, head_ref, repo);
599 error = got_worktree_set_base_commit_id(worktree, repo,
606 error = got_worktree_checkout_files(worktree, "", repo,
607 checkout_progress, worktree_path, check_cancelled, NULL);
611 printf("Now shut up and hack\n");
623 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
628 static const struct got_error *
629 update_progress(void *arg, unsigned char status, const char *path)
631 int *did_something = arg;
633 if (status == GOT_STATUS_EXISTS)
638 /* Base commit bump happens silently. */
639 if (status == GOT_STATUS_BUMP_BASE)
642 while (path[0] == '/')
644 printf("%c %s\n", status, path);
648 static const struct got_error *
649 switch_head_ref(struct got_reference *head_ref,
650 struct got_object_id *commit_id, struct got_worktree *worktree,
651 struct got_repository *repo)
653 const struct got_error *err = NULL;
655 int ref_has_moved = 0;
657 /* Trivial case: switching between two different references. */
658 if (strcmp(got_ref_get_name(head_ref),
659 got_worktree_get_head_ref_name(worktree)) != 0) {
660 printf("Switching work tree from %s to %s\n",
661 got_worktree_get_head_ref_name(worktree),
662 got_ref_get_name(head_ref));
663 return got_worktree_set_head_ref(worktree, head_ref);
666 err = check_linear_ancestry(commit_id,
667 got_worktree_get_base_commit_id(worktree), repo);
669 if (err->code != GOT_ERR_ANCESTRY)
676 /* Switching to a rebased branch with the same reference name. */
677 err = got_object_id_str(&base_id_str,
678 got_worktree_get_base_commit_id(worktree));
681 printf("Reference %s now points at a different branch\n",
682 got_worktree_get_head_ref_name(worktree));
683 printf("Switching work tree from %s to %s\n", base_id_str,
684 got_worktree_get_head_ref_name(worktree));
688 static const struct got_error *
689 cmd_update(int argc, char *argv[])
691 const struct got_error *error = NULL;
692 struct got_repository *repo = NULL;
693 struct got_worktree *worktree = NULL;
694 char *worktree_path = NULL, *path = NULL;
695 struct got_object_id *commit_id = NULL;
696 char *commit_id_str = NULL;
697 const char *branch_name = NULL;
698 struct got_reference *head_ref = NULL;
699 int ch, did_something = 0, rebase_in_progress;
701 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
704 branch_name = optarg;
707 commit_id_str = strdup(optarg);
708 if (commit_id_str == NULL)
709 return got_error_from_errno("strdup");
721 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
722 "unveil", NULL) == -1)
725 worktree_path = getcwd(NULL, 0);
726 if (worktree_path == NULL) {
727 error = got_error_from_errno("getcwd");
730 error = got_worktree_open(&worktree, worktree_path);
734 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
737 if (rebase_in_progress) {
738 error = got_error(GOT_ERR_REBASING);
745 error = got_error_from_errno("strdup");
748 } else if (argc == 1) {
749 error = got_worktree_resolve_path(&path, worktree, argv[0]);
755 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
759 error = apply_unveil(got_repo_get_path(repo), 0,
760 got_worktree_get_root_path(worktree), 0);
764 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
765 got_worktree_get_head_ref_name(worktree), 0);
768 if (commit_id_str == NULL) {
769 error = got_ref_resolve(&commit_id, repo, head_ref);
772 error = got_object_id_str(&commit_id_str, commit_id);
776 error = got_repo_match_object_id_prefix(&commit_id,
777 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
781 error = got_object_id_str(&commit_id_str, commit_id);
787 struct got_object_id *head_commit_id;
788 if (strlen(path) != 0) {
789 fprintf(stderr, "%s: switching between branches "
790 "requires that the entire work tree "
791 "gets updated, not just '%s'\n",
792 getprogname(), path);
793 error = got_error(GOT_ERR_BAD_PATH);
796 error = got_ref_resolve(&head_commit_id, repo, head_ref);
799 error = check_linear_ancestry(commit_id, head_commit_id, repo);
800 free(head_commit_id);
803 error = check_same_branch(commit_id, head_ref, repo);
806 error = switch_head_ref(head_ref, commit_id, worktree, repo);
810 error = check_linear_ancestry(commit_id,
811 got_worktree_get_base_commit_id(worktree), repo);
813 if (error->code == GOT_ERR_ANCESTRY)
814 error = got_error(GOT_ERR_BRANCH_MOVED);
817 error = check_same_branch(commit_id, head_ref, repo);
822 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
824 error = got_worktree_set_base_commit_id(worktree, repo,
830 error = got_worktree_checkout_files(worktree, path, repo,
831 update_progress, &did_something, check_cancelled, NULL);
836 printf("Updated to commit %s\n", commit_id_str);
838 printf("Already up-to-date\n");
847 static const struct got_error *
848 print_patch(struct got_commit_object *commit, struct got_object_id *id,
849 int diff_context, struct got_repository *repo)
851 const struct got_error *err = NULL;
852 struct got_tree_object *tree1 = NULL, *tree2;
853 struct got_object_qid *qid;
854 char *id_str1 = NULL, *id_str2;
855 struct got_diff_blob_output_unidiff_arg arg;
857 err = got_object_open_as_tree(&tree2, repo,
858 got_object_commit_get_tree_id(commit));
862 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
864 struct got_commit_object *pcommit;
866 err = got_object_open_as_commit(&pcommit, repo, qid->id);
870 err = got_object_open_as_tree(&tree1, repo,
871 got_object_commit_get_tree_id(pcommit));
872 got_object_commit_close(pcommit);
876 err = got_object_id_str(&id_str1, qid->id);
881 err = got_object_id_str(&id_str2, id);
885 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
886 arg.diff_context = diff_context;
887 arg.outfile = stdout;
888 err = got_diff_tree(tree1, tree2, "", "", repo,
889 got_diff_blob_output_unidiff, &arg);
892 got_object_tree_close(tree1);
893 got_object_tree_close(tree2);
900 get_datestr(time_t *time, char *datebuf)
902 char *p, *s = ctime_r(time, datebuf);
909 static const struct got_error *
910 print_commit(struct got_commit_object *commit, struct got_object_id *id,
911 struct got_repository *repo, int show_patch, int diff_context,
912 struct got_reflist_head *refs)
914 const struct got_error *err = NULL;
915 char *id_str, *datestr, *logmsg0, *logmsg, *line;
917 time_t committer_time;
918 const char *author, *committer;
919 char *refs_str = NULL;
920 struct got_reflist_entry *re;
922 SIMPLEQ_FOREACH(re, refs, entry) {
925 if (got_object_id_cmp(re->id, id) != 0)
927 name = got_ref_get_name(re->ref);
928 if (strcmp(name, GOT_REF_HEAD) == 0)
930 if (strncmp(name, "refs/", 5) == 0)
932 if (strncmp(name, "got/", 4) == 0)
934 if (strncmp(name, "heads/", 6) == 0)
936 if (strncmp(name, "remotes/", 8) == 0)
939 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
941 err = got_error_from_errno("asprintf");
947 err = got_object_id_str(&id_str, id);
951 printf("-----------------------------------------------\n");
952 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
953 refs_str ? refs_str : "", refs_str ? ")" : "");
958 printf("from: %s\n", got_object_commit_get_author(commit));
959 committer_time = got_object_commit_get_committer_time(commit);
960 datestr = get_datestr(&committer_time, datebuf);
961 printf("date: %s UTC\n", datestr);
962 author = got_object_commit_get_author(commit);
963 committer = got_object_commit_get_committer(commit);
964 if (strcmp(author, committer) != 0)
965 printf("via: %s\n", committer);
966 if (got_object_commit_get_nparents(commit) > 1) {
967 const struct got_object_id_queue *parent_ids;
968 struct got_object_qid *qid;
970 parent_ids = got_object_commit_get_parent_ids(commit);
971 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
972 err = got_object_id_str(&id_str, qid->id);
975 printf("parent %d: %s\n", n++, id_str);
980 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
982 return got_error_from_errno("strdup");
986 line = strsep(&logmsg, "\n");
988 printf(" %s\n", line);
993 err = print_patch(commit, id, diff_context, repo);
998 if (fflush(stdout) != 0 && err == NULL)
999 err = got_error_from_errno("fflush");
1003 static const struct got_error *
1004 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1005 char *path, int show_patch, int diff_context, int limit,
1006 int first_parent_traversal, struct got_reflist_head *refs)
1008 const struct got_error *err;
1009 struct got_commit_graph *graph;
1011 err = got_commit_graph_open(&graph, root_id, path,
1012 first_parent_traversal, repo);
1015 err = got_commit_graph_iter_start(graph, root_id, repo);
1019 struct got_commit_object *commit;
1020 struct got_object_id *id;
1022 if (sigint_received || sigpipe_received)
1025 err = got_commit_graph_iter_next(&id, graph);
1027 if (err->code == GOT_ERR_ITER_COMPLETED) {
1031 if (err->code != GOT_ERR_ITER_NEED_MORE)
1033 err = got_commit_graph_fetch_commits(graph, 1, repo);
1042 err = got_object_open_as_commit(&commit, repo, id);
1045 err = print_commit(commit, id, repo, show_patch, diff_context,
1047 got_object_commit_close(commit);
1048 if (err || (limit && --limit == 0))
1052 got_commit_graph_close(graph);
1059 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1060 "[-r repository-path] [path]\n", getprogname());
1064 static const struct got_error *
1065 cmd_log(int argc, char *argv[])
1067 const struct got_error *error;
1068 struct got_repository *repo = NULL;
1069 struct got_worktree *worktree = NULL;
1070 struct got_commit_object *commit = NULL;
1071 struct got_object_id *id = NULL;
1072 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1073 char *start_commit = NULL;
1074 int diff_context = 3, ch;
1075 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1077 struct got_reflist_head refs;
1079 SIMPLEQ_INIT(&refs);
1082 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1088 while ((ch = getopt(argc, argv, "bpc:C:l:r:")) != -1) {
1091 first_parent_traversal = 1;
1097 start_commit = optarg;
1100 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1103 err(1, "-C option %s", errstr);
1106 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1108 err(1, "-l option %s", errstr);
1111 repo_path = realpath(optarg, NULL);
1112 if (repo_path == NULL)
1113 err(1, "-r option");
1114 got_path_strip_trailing_slashes(repo_path);
1125 cwd = getcwd(NULL, 0);
1127 error = got_error_from_errno("getcwd");
1131 error = got_worktree_open(&worktree, cwd);
1132 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1139 error = got_error_from_errno("strdup");
1142 } else if (argc == 1) {
1144 error = got_worktree_resolve_path(&path, worktree,
1149 path = strdup(argv[0]);
1151 error = got_error_from_errno("strdup");
1158 if (repo_path == NULL) {
1159 repo_path = worktree ?
1160 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1162 if (repo_path == NULL) {
1163 error = got_error_from_errno("strdup");
1167 error = got_repo_open(&repo, repo_path);
1171 error = apply_unveil(got_repo_get_path(repo), 1,
1172 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1176 if (start_commit == NULL) {
1177 struct got_reference *head_ref;
1178 error = got_ref_open(&head_ref, repo,
1179 worktree ? got_worktree_get_head_ref_name(worktree)
1183 error = got_ref_resolve(&id, repo, head_ref);
1184 got_ref_close(head_ref);
1187 error = got_object_open_as_commit(&commit, repo, id);
1189 struct got_reference *ref;
1190 error = got_ref_open(&ref, repo, start_commit, 0);
1191 if (error == NULL) {
1193 error = got_ref_resolve(&id, repo, ref);
1197 error = got_object_get_type(&obj_type, repo, id);
1200 if (obj_type == GOT_OBJ_TYPE_TAG) {
1201 struct got_tag_object *tag;
1202 error = got_object_open_as_tag(&tag, repo, id);
1205 if (got_object_tag_get_object_type(tag) !=
1206 GOT_OBJ_TYPE_COMMIT) {
1207 got_object_tag_close(tag);
1208 error = got_error(GOT_ERR_OBJ_TYPE);
1212 id = got_object_id_dup(
1213 got_object_tag_get_object_id(tag));
1215 error = got_error_from_errno(
1216 "got_object_id_dup");
1217 got_object_tag_close(tag);
1220 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1221 error = got_error(GOT_ERR_OBJ_TYPE);
1224 error = got_object_open_as_commit(&commit, repo, id);
1228 if (commit == NULL) {
1229 error = got_repo_match_object_id_prefix(&id,
1230 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1238 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1243 path = in_repo_path;
1246 error = got_ref_list(&refs, repo);
1250 error = print_commits(id, repo, path, show_patch,
1251 diff_context, limit, first_parent_traversal, &refs);
1258 got_worktree_close(worktree);
1260 const struct got_error *repo_error;
1261 repo_error = got_repo_close(repo);
1265 got_ref_list_free(&refs);
1272 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1273 "[object1 object2 | path]\n", getprogname());
1277 struct print_diff_arg {
1278 struct got_repository *repo;
1279 struct got_worktree *worktree;
1285 static const struct got_error *
1286 print_diff(void *arg, unsigned char status, const char *path,
1287 struct got_object_id *blob_id, struct got_object_id *commit_id)
1289 struct print_diff_arg *a = arg;
1290 const struct got_error *err = NULL;
1291 struct got_blob_object *blob1 = NULL;
1293 char *abspath = NULL;
1296 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1297 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1300 if (!a->header_shown) {
1301 printf("diff %s %s\n", a->id_str,
1302 got_worktree_get_root_path(a->worktree));
1303 a->header_shown = 1;
1306 if (status != GOT_STATUS_ADD) {
1307 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1313 if (status != GOT_STATUS_DELETE) {
1314 if (asprintf(&abspath, "%s/%s",
1315 got_worktree_get_root_path(a->worktree), path) == -1) {
1316 err = got_error_from_errno("asprintf");
1320 f2 = fopen(abspath, "r");
1322 err = got_error_from_errno2("fopen", abspath);
1325 if (lstat(abspath, &sb) == -1) {
1326 err = got_error_from_errno2("lstat", abspath);
1332 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1336 got_object_blob_close(blob1);
1337 if (f2 && fclose(f2) != 0 && err == NULL)
1338 err = got_error_from_errno("fclose");
1343 static const struct got_error *
1344 cmd_diff(int argc, char *argv[])
1346 const struct got_error *error;
1347 struct got_repository *repo = NULL;
1348 struct got_worktree *worktree = NULL;
1349 char *cwd = NULL, *repo_path = NULL;
1350 struct got_object_id *id1 = NULL, *id2 = NULL;
1351 const char *id_str1 = NULL, *id_str2 = NULL;
1352 char *label1 = NULL, *label2 = NULL;
1354 int diff_context = 3, ch;
1359 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1364 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1367 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1369 err(1, "-C option %s", errstr);
1372 repo_path = realpath(optarg, NULL);
1373 if (repo_path == NULL)
1374 err(1, "-r option");
1375 got_path_strip_trailing_slashes(repo_path);
1386 cwd = getcwd(NULL, 0);
1388 error = got_error_from_errno("getcwd");
1391 error = got_worktree_open(&worktree, cwd);
1392 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1395 if (worktree == NULL) {
1396 error = got_error(GOT_ERR_NOT_WORKTREE);
1401 "-r option can't be used when diffing a work tree");
1402 repo_path = strdup(got_worktree_get_repo_path(worktree));
1403 if (repo_path == NULL) {
1404 error = got_error_from_errno("strdup");
1408 error = got_worktree_resolve_path(&path, worktree,
1415 error = got_error_from_errno("strdup");
1419 } else if (argc == 2) {
1422 if (worktree && repo_path == NULL) {
1424 strdup(got_worktree_get_repo_path(worktree));
1425 if (repo_path == NULL) {
1426 error = got_error_from_errno("strdup");
1433 if (repo_path == NULL) {
1434 repo_path = getcwd(NULL, 0);
1435 if (repo_path == NULL)
1436 return got_error_from_errno("getcwd");
1439 error = got_repo_open(&repo, repo_path);
1444 error = apply_unveil(got_repo_get_path(repo), 1,
1445 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1450 struct print_diff_arg arg;
1452 error = got_object_id_str(&id_str,
1453 got_worktree_get_base_commit_id(worktree));
1457 arg.worktree = worktree;
1458 arg.diff_context = diff_context;
1459 arg.id_str = id_str;
1460 arg.header_shown = 0;
1462 error = got_worktree_status(worktree, path, repo, print_diff,
1463 &arg, check_cancelled, NULL);
1468 error = got_repo_match_object_id_prefix(&id1, id_str1,
1469 GOT_OBJ_TYPE_ANY, repo);
1471 struct got_reference *ref;
1472 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1474 error = got_ref_open(&ref, repo, id_str1, 0);
1477 label1 = strdup(got_ref_get_name(ref));
1478 if (label1 == NULL) {
1479 error = got_error_from_errno("strdup");
1482 error = got_ref_resolve(&id1, repo, ref);
1487 error = got_object_id_str(&label1, id1);
1488 if (label1 == NULL) {
1489 error = got_error_from_errno("strdup");
1494 error = got_repo_match_object_id_prefix(&id2, id_str2,
1495 GOT_OBJ_TYPE_ANY, repo);
1497 struct got_reference *ref;
1498 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1500 error = got_ref_open(&ref, repo, id_str2, 0);
1503 label2 = strdup(got_ref_get_name(ref));
1504 if (label2 == NULL) {
1505 error = got_error_from_errno("strdup");
1508 error = got_ref_resolve(&id2, repo, ref);
1513 error = got_object_id_str(&label2, id2);
1514 if (label2 == NULL) {
1515 error = got_error_from_errno("strdup");
1520 error = got_object_get_type(&type1, repo, id1);
1524 error = got_object_get_type(&type2, repo, id2);
1528 if (type1 != type2) {
1529 error = got_error(GOT_ERR_OBJ_TYPE);
1534 case GOT_OBJ_TYPE_BLOB:
1535 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1536 diff_context, repo, stdout);
1538 case GOT_OBJ_TYPE_TREE:
1539 error = got_diff_objects_as_trees(id1, id2, "", "",
1540 diff_context, repo, stdout);
1542 case GOT_OBJ_TYPE_COMMIT:
1543 printf("diff %s %s\n", label1, label2);
1544 error = got_diff_objects_as_commits(id1, id2, diff_context,
1548 error = got_error(GOT_ERR_OBJ_TYPE);
1558 got_worktree_close(worktree);
1560 const struct got_error *repo_error;
1561 repo_error = got_repo_close(repo);
1572 "usage: %s blame [-c commit] [-r repository-path] path\n",
1577 static const struct got_error *
1578 cmd_blame(int argc, char *argv[])
1580 const struct got_error *error;
1581 struct got_repository *repo = NULL;
1582 struct got_worktree *worktree = NULL;
1583 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1584 struct got_object_id *commit_id = NULL;
1585 char *commit_id_str = NULL;
1589 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1594 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1597 commit_id_str = optarg;
1600 repo_path = realpath(optarg, NULL);
1601 if (repo_path == NULL)
1602 err(1, "-r option");
1603 got_path_strip_trailing_slashes(repo_path);
1619 cwd = getcwd(NULL, 0);
1621 error = got_error_from_errno("getcwd");
1624 if (repo_path == NULL) {
1625 error = got_worktree_open(&worktree, cwd);
1626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1632 strdup(got_worktree_get_repo_path(worktree));
1633 if (repo_path == NULL)
1634 error = got_error_from_errno("strdup");
1638 repo_path = strdup(cwd);
1639 if (repo_path == NULL) {
1640 error = got_error_from_errno("strdup");
1646 error = got_repo_open(&repo, repo_path);
1650 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1655 const char *prefix = got_worktree_get_path_prefix(worktree);
1656 char *p, *worktree_subdir = cwd +
1657 strlen(got_worktree_get_root_path(worktree));
1658 if (asprintf(&p, "%s%s%s%s%s",
1659 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1660 worktree_subdir, worktree_subdir[0] ? "/" : "",
1662 error = got_error_from_errno("asprintf");
1665 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1668 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1673 if (commit_id_str == NULL) {
1674 struct got_reference *head_ref;
1675 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1678 error = got_ref_resolve(&commit_id, repo, head_ref);
1679 got_ref_close(head_ref);
1683 error = got_repo_match_object_id_prefix(&commit_id,
1684 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1689 error = got_blame(in_repo_path, commit_id, repo, stdout);
1696 got_worktree_close(worktree);
1698 const struct got_error *repo_error;
1699 repo_error = got_repo_close(repo);
1710 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
1716 print_entry(struct got_tree_entry *te, const char *id, const char *path,
1717 const char *root_path)
1719 int is_root_path = (strcmp(path, root_path) == 0);
1721 path += strlen(root_path);
1722 while (path[0] == '/')
1725 printf("%s%s%s%s%s\n", id ? id : "", path,
1726 is_root_path ? "" : "/", te->name,
1727 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
1730 static const struct got_error *
1731 print_tree(const char *path, struct got_object_id *commit_id,
1732 int show_ids, int recurse, const char *root_path,
1733 struct got_repository *repo)
1735 const struct got_error *err = NULL;
1736 struct got_object_id *tree_id = NULL;
1737 struct got_tree_object *tree = NULL;
1738 const struct got_tree_entries *entries;
1739 struct got_tree_entry *te;
1741 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
1745 err = got_object_open_as_tree(&tree, repo, tree_id);
1748 entries = got_object_tree_get_entries(tree);
1749 te = SIMPLEQ_FIRST(&entries->head);
1753 if (sigint_received || sigpipe_received)
1758 err = got_object_id_str(&id_str, te->id);
1761 if (asprintf(&id, "%s ", id_str) == -1) {
1762 err = got_error_from_errno("asprintf");
1768 print_entry(te, id, path, root_path);
1771 if (recurse && S_ISDIR(te->mode)) {
1773 if (asprintf(&child_path, "%s%s%s", path,
1774 path[0] == '/' && path[1] == '\0' ? "" : "/",
1776 err = got_error_from_errno("asprintf");
1779 err = print_tree(child_path, commit_id, show_ids, 1,
1786 te = SIMPLEQ_NEXT(te, entry);
1790 got_object_tree_close(tree);
1795 static const struct got_error *
1796 cmd_tree(int argc, char *argv[])
1798 const struct got_error *error;
1799 struct got_repository *repo = NULL;
1800 struct got_worktree *worktree = NULL;
1802 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1803 struct got_object_id *commit_id = NULL;
1804 char *commit_id_str = NULL;
1805 int show_ids = 0, recurse = 0;
1809 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1814 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
1817 commit_id_str = optarg;
1820 repo_path = realpath(optarg, NULL);
1821 if (repo_path == NULL)
1822 err(1, "-r option");
1823 got_path_strip_trailing_slashes(repo_path);
1847 cwd = getcwd(NULL, 0);
1849 error = got_error_from_errno("getcwd");
1852 if (repo_path == NULL) {
1853 error = got_worktree_open(&worktree, cwd);
1854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1860 strdup(got_worktree_get_repo_path(worktree));
1861 if (repo_path == NULL)
1862 error = got_error_from_errno("strdup");
1866 repo_path = strdup(cwd);
1867 if (repo_path == NULL) {
1868 error = got_error_from_errno("strdup");
1874 error = got_repo_open(&repo, repo_path);
1878 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1884 char *p, *worktree_subdir = cwd +
1885 strlen(got_worktree_get_root_path(worktree));
1886 if (asprintf(&p, "%s/%s",
1887 got_worktree_get_path_prefix(worktree),
1888 worktree_subdir) == -1) {
1889 error = got_error_from_errno("asprintf");
1892 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1899 if (in_repo_path == NULL) {
1900 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1905 if (commit_id_str == NULL) {
1906 struct got_reference *head_ref;
1907 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1910 error = got_ref_resolve(&commit_id, repo, head_ref);
1911 got_ref_close(head_ref);
1915 error = got_repo_match_object_id_prefix(&commit_id,
1916 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1921 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
1922 in_repo_path, repo);
1929 got_worktree_close(worktree);
1931 const struct got_error *repo_error;
1932 repo_error = got_repo_close(repo);
1942 fprintf(stderr, "usage: %s status [path]\n", getprogname());
1946 static const struct got_error *
1947 print_status(void *arg, unsigned char status, const char *path,
1948 struct got_object_id *blob_id, struct got_object_id *commit_id)
1950 printf("%c %s\n", status, path);
1954 static const struct got_error *
1955 cmd_status(int argc, char *argv[])
1957 const struct got_error *error = NULL;
1958 struct got_repository *repo = NULL;
1959 struct got_worktree *worktree = NULL;
1960 char *cwd = NULL, *path = NULL;
1963 while ((ch = getopt(argc, argv, "")) != -1) {
1975 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1979 cwd = getcwd(NULL, 0);
1981 error = got_error_from_errno("getcwd");
1985 error = got_worktree_open(&worktree, cwd);
1992 error = got_error_from_errno("strdup");
1995 } else if (argc == 1) {
1996 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2002 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2006 error = apply_unveil(got_repo_get_path(repo), 1,
2007 got_worktree_get_root_path(worktree), 0);
2011 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2012 check_cancelled, NULL);
2023 "usage: %s ref [-r repository] -l | -d name | name target\n",
2028 static const struct got_error *
2029 list_refs(struct got_repository *repo)
2031 static const struct got_error *err = NULL;
2032 struct got_reflist_head refs;
2033 struct got_reflist_entry *re;
2035 SIMPLEQ_INIT(&refs);
2036 err = got_ref_list(&refs, repo);
2040 SIMPLEQ_FOREACH(re, &refs, entry) {
2042 refstr = got_ref_to_str(re->ref);
2044 return got_error_from_errno("got_ref_to_str");
2045 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2049 got_ref_list_free(&refs);
2053 static const struct got_error *
2054 delete_ref(struct got_repository *repo, const char *refname)
2056 const struct got_error *err = NULL;
2057 struct got_reference *ref;
2059 err = got_ref_open(&ref, repo, refname, 0);
2063 err = got_ref_delete(ref, repo);
2068 static const struct got_error *
2069 add_ref(struct got_repository *repo, const char *refname, const char *target)
2071 const struct got_error *err = NULL;
2072 struct got_object_id *id;
2073 struct got_reference *ref = NULL;
2075 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2078 struct got_reference *target_ref;
2080 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2082 err = got_ref_open(&target_ref, repo, target, 0);
2085 err = got_ref_resolve(&id, repo, target_ref);
2086 got_ref_close(target_ref);
2091 err = got_ref_alloc(&ref, refname, id);
2095 err = got_ref_write(ref, repo);
2103 static const struct got_error *
2104 cmd_ref(int argc, char *argv[])
2106 const struct got_error *error = NULL;
2107 struct got_repository *repo = NULL;
2108 struct got_worktree *worktree = NULL;
2109 char *cwd = NULL, *repo_path = NULL;
2110 int ch, do_list = 0;
2111 const char *delref = NULL;
2113 /* TODO: Add -s option for adding symbolic references. */
2114 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2120 repo_path = realpath(optarg, NULL);
2121 if (repo_path == NULL)
2122 err(1, "-r option");
2123 got_path_strip_trailing_slashes(repo_path);
2134 if (do_list && delref)
2135 errx(1, "-l and -d options are mutually exclusive\n");
2140 if (do_list || delref) {
2143 } else if (argc != 2)
2148 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2152 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2153 "sendfd unveil", NULL) == -1)
2157 cwd = getcwd(NULL, 0);
2159 error = got_error_from_errno("getcwd");
2163 if (repo_path == NULL) {
2164 error = got_worktree_open(&worktree, cwd);
2165 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2171 strdup(got_worktree_get_repo_path(worktree));
2172 if (repo_path == NULL)
2173 error = got_error_from_errno("strdup");
2177 repo_path = strdup(cwd);
2178 if (repo_path == NULL) {
2179 error = got_error_from_errno("strdup");
2185 error = got_repo_open(&repo, repo_path);
2189 error = apply_unveil(got_repo_get_path(repo), do_list,
2190 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2195 error = list_refs(repo);
2197 error = delete_ref(repo, delref);
2199 error = add_ref(repo, argv[0], argv[1]);
2202 got_repo_close(repo);
2204 got_worktree_close(worktree);
2214 "usage: %s branch [-r repository] -l | -d name | "
2215 "name [base-branch]\n", getprogname());
2219 static const struct got_error *
2220 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2222 static const struct got_error *err = NULL;
2223 struct got_reflist_head refs;
2224 struct got_reflist_entry *re;
2226 SIMPLEQ_INIT(&refs);
2228 err = got_ref_list(&refs, repo);
2232 SIMPLEQ_FOREACH(re, &refs, entry) {
2233 const char *refname, *marker = " ";
2235 refname = got_ref_get_name(re->ref);
2236 if (strncmp(refname, "refs/heads/", 11) != 0)
2238 if (worktree && strcmp(refname,
2239 got_worktree_get_head_ref_name(worktree)) == 0) {
2240 struct got_object_id *id = NULL;
2241 err = got_ref_resolve(&id, repo, re->ref);
2244 if (got_object_id_cmp(id,
2245 got_worktree_get_base_commit_id(worktree)) == 0)
2252 refstr = got_ref_to_str(re->ref);
2254 return got_error_from_errno("got_ref_to_str");
2255 printf("%s%s: %s\n", marker, refname, refstr);
2259 got_ref_list_free(&refs);
2263 static const struct got_error *
2264 delete_branch(struct got_repository *repo, const char *branch_name)
2266 const struct got_error *err = NULL;
2267 struct got_reference *ref;
2270 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2271 return got_error_from_errno("asprintf");
2273 err = got_ref_open(&ref, repo, refname, 0);
2277 err = got_ref_delete(ref, repo);
2284 static const struct got_error *
2285 add_branch(struct got_repository *repo, const char *branch_name,
2286 const char *base_branch)
2288 const struct got_error *err = NULL;
2289 struct got_object_id *id = NULL;
2290 struct got_reference *ref = NULL;
2291 char *base_refname = NULL, *refname = NULL;
2292 struct got_reference *base_ref;
2295 * Don't let the user create a branch named '-'.
2296 * While technically a valid reference name, this case is usually
2297 * an unintended typo.
2299 if (branch_name[0] == '-' && branch_name[1] == '\0')
2300 return got_error(GOT_ERR_BAD_REF_NAME);
2302 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2303 base_refname = strdup(GOT_REF_HEAD);
2304 if (base_refname == NULL)
2305 return got_error_from_errno("strdup");
2306 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2307 return got_error_from_errno("asprintf");
2309 err = got_ref_open(&base_ref, repo, base_refname, 0);
2312 err = got_ref_resolve(&id, repo, base_ref);
2313 got_ref_close(base_ref);
2317 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2318 err = got_error_from_errno("asprintf");
2322 err = got_ref_open(&ref, repo, refname, 0);
2324 err = got_error(GOT_ERR_BRANCH_EXISTS);
2326 } else if (err->code != GOT_ERR_NOT_REF)
2329 err = got_ref_alloc(&ref, refname, id);
2333 err = got_ref_write(ref, repo);
2343 static const struct got_error *
2344 cmd_branch(int argc, char *argv[])
2346 const struct got_error *error = NULL;
2347 struct got_repository *repo = NULL;
2348 struct got_worktree *worktree = NULL;
2349 char *cwd = NULL, *repo_path = NULL;
2350 int ch, do_list = 0;
2351 const char *delref = NULL;
2353 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2359 repo_path = realpath(optarg, NULL);
2360 if (repo_path == NULL)
2361 err(1, "-r option");
2362 got_path_strip_trailing_slashes(repo_path);
2373 if (do_list && delref)
2374 errx(1, "-l and -d options are mutually exclusive\n");
2379 if (do_list || delref) {
2382 } else if (argc < 1 || argc > 2)
2387 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2391 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2392 "sendfd unveil", NULL) == -1)
2396 cwd = getcwd(NULL, 0);
2398 error = got_error_from_errno("getcwd");
2402 if (repo_path == NULL) {
2403 error = got_worktree_open(&worktree, cwd);
2404 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2410 strdup(got_worktree_get_repo_path(worktree));
2411 if (repo_path == NULL)
2412 error = got_error_from_errno("strdup");
2416 repo_path = strdup(cwd);
2417 if (repo_path == NULL) {
2418 error = got_error_from_errno("strdup");
2424 error = got_repo_open(&repo, repo_path);
2428 error = apply_unveil(got_repo_get_path(repo), do_list,
2429 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2434 error = list_branches(repo, worktree);
2436 error = delete_branch(repo, delref);
2438 const char *base_branch;
2440 base_branch = worktree ?
2441 got_worktree_get_head_ref_name(worktree) :
2444 base_branch = argv[1];
2445 error = add_branch(repo, argv[0], base_branch);
2449 got_repo_close(repo);
2451 got_worktree_close(worktree);
2460 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2464 static const struct got_error *
2465 cmd_add(int argc, char *argv[])
2467 const struct got_error *error = NULL;
2468 struct got_repository *repo = NULL;
2469 struct got_worktree *worktree = NULL;
2471 struct got_pathlist_head paths;
2472 struct got_pathlist_entry *pe;
2477 while ((ch = getopt(argc, argv, "")) != -1) {
2491 /* make sure each file exists before doing anything halfway */
2492 for (x = 0; x < argc; x++) {
2493 char *path = realpath(argv[x], NULL);
2495 error = got_error_from_errno2("realpath", argv[x]);
2501 cwd = getcwd(NULL, 0);
2503 error = got_error_from_errno("getcwd");
2507 error = got_worktree_open(&worktree, cwd);
2511 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2515 error = apply_unveil(got_repo_get_path(repo), 1,
2516 got_worktree_get_root_path(worktree), 0);
2520 for (x = 0; x < argc; x++) {
2521 char *path = realpath(argv[x], NULL);
2523 error = got_error_from_errno2("realpath", argv[x]);
2527 got_path_strip_trailing_slashes(path);
2528 error = got_pathlist_insert(&pe, &paths, path, NULL);
2534 error = got_worktree_schedule_add(worktree, &paths, print_status,
2538 got_repo_close(repo);
2540 got_worktree_close(worktree);
2541 TAILQ_FOREACH(pe, &paths, entry)
2542 free((char *)pe->path);
2543 got_pathlist_free(&paths);
2551 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2555 static const struct got_error *
2556 cmd_remove(int argc, char *argv[])
2558 const struct got_error *error = NULL;
2559 struct got_worktree *worktree = NULL;
2560 struct got_repository *repo = NULL;
2562 struct got_pathlist_head paths;
2563 struct got_pathlist_entry *pe;
2564 int ch, i, delete_local_mods = 0;
2568 while ((ch = getopt(argc, argv, "f")) != -1) {
2571 delete_local_mods = 1;
2585 /* make sure each file exists before doing anything halfway */
2586 for (i = 0; i < argc; i++) {
2587 char *path = realpath(argv[i], NULL);
2589 error = got_error_from_errno2("realpath", argv[i]);
2595 cwd = getcwd(NULL, 0);
2597 error = got_error_from_errno("getcwd");
2600 error = got_worktree_open(&worktree, cwd);
2604 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2608 error = apply_unveil(got_repo_get_path(repo), 1,
2609 got_worktree_get_root_path(worktree), 0);
2613 for (i = 0; i < argc; i++) {
2614 char *path = realpath(argv[i], NULL);
2616 error = got_error_from_errno2("realpath", argv[i]);
2620 got_path_strip_trailing_slashes(path);
2621 error = got_pathlist_insert(&pe, &paths, path, NULL);
2627 error = got_worktree_schedule_delete(worktree, &paths,
2628 delete_local_mods, print_status, NULL, repo);
2633 got_repo_close(repo);
2635 got_worktree_close(worktree);
2636 TAILQ_FOREACH(pe, &paths, entry)
2637 free((char *)pe->path);
2638 got_pathlist_free(&paths);
2646 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2650 static const struct got_error *
2651 revert_progress(void *arg, unsigned char status, const char *path)
2653 while (path[0] == '/')
2655 printf("%c %s\n", status, path);
2659 static const struct got_error *
2660 cmd_revert(int argc, char *argv[])
2662 const struct got_error *error = NULL;
2663 struct got_worktree *worktree = NULL;
2664 struct got_repository *repo = NULL;
2665 char *cwd = NULL, *path = NULL;
2666 struct got_pathlist_head paths;
2667 struct got_pathlist_entry *pe;
2672 while ((ch = getopt(argc, argv, "")) != -1) {
2686 for (i = 0; i < argc; i++) {
2687 char *path = realpath(argv[i], NULL);
2689 error = got_error_from_errno2("realpath", argv[i]);
2693 got_path_strip_trailing_slashes(path);
2694 error = got_pathlist_insert(&pe, &paths, path, NULL);
2701 cwd = getcwd(NULL, 0);
2703 error = got_error_from_errno("getcwd");
2706 error = got_worktree_open(&worktree, cwd);
2710 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2714 error = apply_unveil(got_repo_get_path(repo), 1,
2715 got_worktree_get_root_path(worktree), 0);
2719 error = got_worktree_revert(worktree, &paths,
2720 revert_progress, NULL, repo);
2725 got_repo_close(repo);
2727 got_worktree_close(worktree);
2736 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2741 spawn_editor(const char *editor, const char *file)
2744 sig_t sighup, sigint, sigquit;
2747 sighup = signal(SIGHUP, SIG_IGN);
2748 sigint = signal(SIGINT, SIG_IGN);
2749 sigquit = signal(SIGQUIT, SIG_IGN);
2751 switch (pid = fork()) {
2755 execl(editor, editor, file, (char *)NULL);
2759 while (waitpid(pid, &st, 0) == -1)
2764 (void)signal(SIGHUP, sighup);
2765 (void)signal(SIGINT, sigint);
2766 (void)signal(SIGQUIT, sigquit);
2768 if (!WIFEXITED(st)) {
2773 return WEXITSTATUS(st);
2776 struct collect_commit_logmsg_arg {
2777 const char *cmdline_log;
2779 const char *worktree_path;
2780 const char *branch_name;
2781 const char *repo_path;
2786 static const struct got_error *
2787 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2790 char *initial_content = NULL;
2791 struct got_pathlist_entry *pe;
2792 const struct got_error *err = NULL;
2793 char *template = NULL;
2794 struct collect_commit_logmsg_arg *a = arg;
2796 struct stat st, st2;
2799 int fd, content_changed = 0;
2801 /* if a message was specified on the command line, just use it */
2802 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2803 len = strlen(a->cmdline_log) + 1;
2804 *logmsg = malloc(len + 1);
2805 if (*logmsg == NULL)
2806 return got_error_from_errno("malloc");
2807 strlcpy(*logmsg, a->cmdline_log, len);
2811 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2812 return got_error_from_errno("asprintf");
2814 if (asprintf(&initial_content,
2815 "\n# changes to be committed on branch %s:\n",
2816 a->branch_name) == -1)
2817 return got_error_from_errno("asprintf");
2819 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2823 dprintf(fd, initial_content);
2825 TAILQ_FOREACH(pe, commitable_paths, entry) {
2826 struct got_commitable *ct = pe->data;
2827 dprintf(fd, "# %c %s\n",
2828 got_commitable_get_status(ct),
2829 got_commitable_get_path(ct));
2833 if (stat(a->logmsg_path, &st) == -1) {
2834 err = got_error_from_errno2("stat", a->logmsg_path);
2838 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2839 err = got_error_from_errno("failed spawning editor");
2843 if (stat(a->logmsg_path, &st2) == -1) {
2844 err = got_error_from_errno("stat");
2848 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2849 unlink(a->logmsg_path);
2850 free(a->logmsg_path);
2851 a->logmsg_path = NULL;
2852 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2853 "no changes made to commit message, aborting");
2857 *logmsg = malloc(st2.st_size + 1);
2858 if (*logmsg == NULL) {
2859 err = got_error_from_errno("malloc");
2862 (*logmsg)[0] = '\0';
2865 fp = fopen(a->logmsg_path, "r");
2867 err = got_error_from_errno("fopen");
2870 while (fgets(buf, sizeof(buf), fp) != NULL) {
2871 if (!content_changed && strcmp(buf, initial_content) != 0)
2872 content_changed = 1;
2873 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2874 continue; /* remove comments and leading empty lines */
2875 len = strlcat(*logmsg, buf, st2.st_size);
2879 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2880 (*logmsg)[len - 1] = '\0';
2884 if (len == 0 || !content_changed) {
2885 unlink(a->logmsg_path);
2886 free(a->logmsg_path);
2887 a->logmsg_path = NULL;
2888 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2889 "commit message cannot be empty, aborting");
2893 free(initial_content);
2896 /* Editor is done; we can now apply unveil(2) */
2898 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2902 static const struct got_error *
2903 cmd_commit(int argc, char *argv[])
2905 const struct got_error *error = NULL;
2906 struct got_worktree *worktree = NULL;
2907 struct got_repository *repo = NULL;
2908 char *cwd = NULL, *path = NULL, *id_str = NULL;
2909 struct got_object_id *id = NULL;
2910 const char *logmsg = NULL;
2911 const char *got_author = getenv("GOT_AUTHOR");
2912 struct collect_commit_logmsg_arg cl_arg;
2913 char *editor = NULL;
2914 int ch, rebase_in_progress;
2916 while ((ch = getopt(argc, argv, "m:")) != -1) {
2931 path = realpath(argv[0], NULL);
2933 error = got_error_from_errno2("realpath", argv[0]);
2936 got_path_strip_trailing_slashes(path);
2937 } else if (argc != 0)
2940 if (got_author == NULL) {
2941 /* TODO: Look current user up in password database */
2942 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2946 cwd = getcwd(NULL, 0);
2948 error = got_error_from_errno("getcwd");
2951 error = got_worktree_open(&worktree, cwd);
2955 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2958 if (rebase_in_progress) {
2959 error = got_error(GOT_ERR_REBASING);
2963 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2968 * unveil(2) traverses exec(2); if an editor is used we have
2969 * to apply unveil after the log message has been written.
2971 if (logmsg == NULL || strlen(logmsg) == 0)
2972 error = get_editor(&editor);
2974 error = apply_unveil(got_repo_get_path(repo), 0,
2975 got_worktree_get_root_path(worktree), 0);
2979 cl_arg.editor = editor;
2980 cl_arg.cmdline_log = logmsg;
2981 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2982 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2983 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2984 cl_arg.branch_name += 5;
2985 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2986 cl_arg.branch_name += 6;
2987 cl_arg.repo_path = got_repo_get_path(repo);
2988 cl_arg.logmsg_path = NULL;
2989 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2990 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
2992 if (cl_arg.logmsg_path)
2993 fprintf(stderr, "%s: log message preserved in %s\n",
2994 getprogname(), cl_arg.logmsg_path);
2998 if (cl_arg.logmsg_path)
2999 unlink(cl_arg.logmsg_path);
3001 error = got_object_id_str(&id_str, id);
3004 printf("Created commit %s\n", id_str);
3007 got_repo_close(repo);
3009 got_worktree_close(worktree);
3018 usage_cherrypick(void)
3020 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3024 static const struct got_error *
3025 cmd_cherrypick(int argc, char *argv[])
3027 const struct got_error *error = NULL;
3028 struct got_worktree *worktree = NULL;
3029 struct got_repository *repo = NULL;
3030 char *cwd = NULL, *commit_id_str = NULL;
3031 struct got_object_id *commit_id = NULL;
3032 struct got_commit_object *commit = NULL;
3033 struct got_object_qid *pid;
3034 struct got_reference *head_ref = NULL;
3035 int ch, did_something = 0;
3037 while ((ch = getopt(argc, argv, "")) != -1) {
3051 cwd = getcwd(NULL, 0);
3053 error = got_error_from_errno("getcwd");
3056 error = got_worktree_open(&worktree, cwd);
3060 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3064 error = apply_unveil(got_repo_get_path(repo), 0,
3065 got_worktree_get_root_path(worktree), 0);
3069 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3070 GOT_OBJ_TYPE_COMMIT, repo);
3071 if (error != NULL) {
3072 struct got_reference *ref;
3073 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3075 error = got_ref_open(&ref, repo, argv[0], 0);
3078 error = got_ref_resolve(&commit_id, repo, ref);
3083 error = got_object_id_str(&commit_id_str, commit_id);
3087 error = got_ref_open(&head_ref, repo,
3088 got_worktree_get_head_ref_name(worktree), 0);
3092 error = check_same_branch(commit_id, head_ref, repo);
3094 if (error->code != GOT_ERR_ANCESTRY)
3098 error = got_error(GOT_ERR_SAME_BRANCH);
3102 error = got_object_open_as_commit(&commit, repo, commit_id);
3105 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3106 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3107 commit_id, repo, update_progress, &did_something, check_cancelled,
3113 printf("Merged commit %s\n", commit_id_str);
3116 got_object_commit_close(commit);
3117 free(commit_id_str);
3119 got_ref_close(head_ref);
3121 got_worktree_close(worktree);
3123 got_repo_close(repo);
3130 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3134 static const struct got_error *
3135 cmd_backout(int argc, char *argv[])
3137 const struct got_error *error = NULL;
3138 struct got_worktree *worktree = NULL;
3139 struct got_repository *repo = NULL;
3140 char *cwd = NULL, *commit_id_str = NULL;
3141 struct got_object_id *commit_id = NULL;
3142 struct got_commit_object *commit = NULL;
3143 struct got_object_qid *pid;
3144 struct got_reference *head_ref = NULL;
3145 int ch, did_something = 0;
3147 while ((ch = getopt(argc, argv, "")) != -1) {
3161 cwd = getcwd(NULL, 0);
3163 error = got_error_from_errno("getcwd");
3166 error = got_worktree_open(&worktree, cwd);
3170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3174 error = apply_unveil(got_repo_get_path(repo), 0,
3175 got_worktree_get_root_path(worktree), 0);
3179 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3180 GOT_OBJ_TYPE_COMMIT, repo);
3181 if (error != NULL) {
3182 struct got_reference *ref;
3183 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3185 error = got_ref_open(&ref, repo, argv[0], 0);
3188 error = got_ref_resolve(&commit_id, repo, ref);
3193 error = got_object_id_str(&commit_id_str, commit_id);
3197 error = got_ref_open(&head_ref, repo,
3198 got_worktree_get_head_ref_name(worktree), 0);
3202 error = check_same_branch(commit_id, head_ref, repo);
3206 error = got_object_open_as_commit(&commit, repo, commit_id);
3209 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3211 error = got_error(GOT_ERR_ROOT_COMMIT);
3215 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3216 update_progress, &did_something, check_cancelled, NULL);
3221 printf("Backed out commit %s\n", commit_id_str);
3224 got_object_commit_close(commit);
3225 free(commit_id_str);
3227 got_ref_close(head_ref);
3229 got_worktree_close(worktree);
3231 got_repo_close(repo);
3238 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3243 static const struct got_error *
3244 show_rebase_progress(struct got_commit_object *commit,
3245 struct got_object_id *old_id, struct got_object_id *new_id)
3247 const struct got_error *err;
3248 char *old_id_str = NULL, *new_id_str = NULL;
3249 char *logmsg0 = NULL, *logmsg, *nl;
3252 err = got_object_id_str(&old_id_str, old_id);
3257 err = got_object_id_str(&new_id_str, new_id);
3262 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3263 if (logmsg0 == NULL) {
3264 err = got_error_from_errno("strdup");
3269 while (isspace((unsigned char)logmsg[0]))
3272 old_id_str[12] = '\0';
3274 new_id_str[12] = '\0';
3275 len = strlen(logmsg);
3279 nl = strchr(logmsg, '\n');
3282 printf("%s -> %s: %s\n", old_id_str,
3283 new_id_str ? new_id_str : "no-op change", logmsg);
3291 static const struct got_error *
3292 rebase_progress(void *arg, unsigned char status, const char *path)
3294 unsigned char *rebase_status = arg;
3296 while (path[0] == '/')
3298 printf("%c %s\n", status, path);
3300 if (*rebase_status == GOT_STATUS_CONFLICT)
3302 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3303 *rebase_status = status;
3307 static const struct got_error *
3308 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3309 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3310 struct got_repository *repo)
3312 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3313 return got_worktree_rebase_complete(worktree,
3314 new_base_branch, tmp_branch, branch, repo);
3317 static const struct got_error *
3318 rebase_commit(struct got_pathlist_head *merged_paths,
3319 struct got_worktree *worktree, struct got_reference *tmp_branch,
3320 struct got_object_id *commit_id, struct got_repository *repo)
3322 const struct got_error *error;
3323 struct got_commit_object *commit;
3324 struct got_object_id *new_commit_id;
3326 error = got_object_open_as_commit(&commit, repo, commit_id);
3330 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3331 worktree, tmp_branch, commit, commit_id, repo);
3333 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3335 error = show_rebase_progress(commit, commit_id, NULL);
3337 error = show_rebase_progress(commit, commit_id, new_commit_id);
3338 free(new_commit_id);
3341 got_object_commit_close(commit);
3345 struct check_path_prefix_arg {
3346 const char *path_prefix;
3350 static const struct got_error *
3351 check_path_prefix(void *arg, struct got_blob_object *blob1,
3352 struct got_blob_object *blob2, struct got_object_id *id1,
3353 struct got_object_id *id2, const char *path1, const char *path2,
3354 struct got_repository *repo)
3356 struct check_path_prefix_arg *a = arg;
3358 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3359 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3360 return got_error(GOT_ERR_REBASE_PATH);
3365 static const struct got_error *
3366 rebase_check_path_prefix(struct got_object_id *parent_id,
3367 struct got_object_id *commit_id, const char *path_prefix,
3368 struct got_repository *repo)
3370 const struct got_error *err;
3371 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3372 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3373 struct check_path_prefix_arg cpp_arg;
3375 if (got_path_is_root_dir(path_prefix))
3378 err = got_object_open_as_commit(&commit, repo, commit_id);
3382 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3386 err = got_object_open_as_tree(&tree1, repo,
3387 got_object_commit_get_tree_id(parent_commit));
3391 err = got_object_open_as_tree(&tree2, repo,
3392 got_object_commit_get_tree_id(commit));
3396 cpp_arg.path_prefix = path_prefix;
3397 cpp_arg.len = strlen(path_prefix);
3398 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3402 got_object_tree_close(tree1);
3404 got_object_tree_close(tree2);
3406 got_object_commit_close(commit);
3408 got_object_commit_close(parent_commit);
3412 static const struct got_error *
3413 cmd_rebase(int argc, char *argv[])
3415 const struct got_error *error = NULL;
3416 struct got_worktree *worktree = NULL;
3417 struct got_repository *repo = NULL;
3419 struct got_reference *branch = NULL;
3420 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3421 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3422 struct got_object_id *resume_commit_id = NULL;
3423 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3424 struct got_commit_graph *graph = NULL;
3425 struct got_commit_object *commit = NULL;
3426 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3427 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3428 struct got_object_id_queue commits;
3429 struct got_pathlist_head merged_paths;
3430 const struct got_object_id_queue *parent_ids;
3431 struct got_object_qid *qid, *pid;
3433 SIMPLEQ_INIT(&commits);
3434 TAILQ_INIT(&merged_paths);
3436 while ((ch = getopt(argc, argv, "ac")) != -1) {
3442 continue_rebase = 1;
3453 if (abort_rebase && continue_rebase)
3455 else if (abort_rebase || continue_rebase) {
3458 } else if (argc != 1)
3461 cwd = getcwd(NULL, 0);
3463 error = got_error_from_errno("getcwd");
3466 error = got_worktree_open(&worktree, cwd);
3470 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3474 error = apply_unveil(got_repo_get_path(repo), 0,
3475 got_worktree_get_root_path(worktree), 0);
3479 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3483 if (rebase_in_progress && abort_rebase) {
3485 error = got_worktree_rebase_continue(&resume_commit_id,
3486 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3489 printf("Switching work tree to %s\n",
3490 got_ref_get_symref_target(new_base_branch));
3491 error = got_worktree_rebase_abort(worktree, repo,
3492 new_base_branch, update_progress, &did_something);
3495 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3496 goto done; /* nothing else to do */
3497 } else if (abort_rebase) {
3498 error = got_error(GOT_ERR_NOT_REBASING);
3502 if (continue_rebase) {
3503 error = got_worktree_rebase_continue(&resume_commit_id,
3504 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3508 error = rebase_commit(NULL, worktree, tmp_branch,
3509 resume_commit_id, repo);
3513 yca_id = got_object_id_dup(resume_commit_id);
3514 if (yca_id == NULL) {
3515 error = got_error_from_errno("got_object_id_dup");
3519 error = got_ref_open(&branch, repo, argv[0], 0);
3523 error = check_same_branch(
3524 got_worktree_get_base_commit_id(worktree), branch, repo);
3526 if (error->code != GOT_ERR_ANCESTRY)
3530 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3531 "specified branch resolves to a commit which "
3532 "is already contained in work tree's branch");
3537 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3541 if (!continue_rebase) {
3542 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3543 got_worktree_get_base_commit_id(worktree),
3544 branch_head_commit_id, repo);
3547 if (yca_id == NULL) {
3548 error = got_error_msg(GOT_ERR_ANCESTRY,
3549 "specified branch shares no common ancestry "
3550 "with work tree's branch");
3554 error = got_worktree_rebase_prepare(&new_base_branch,
3555 &tmp_branch, worktree, branch, repo);
3560 commit_id = branch_head_commit_id;
3561 error = got_object_open_as_commit(&commit, repo, commit_id);
3565 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3568 parent_ids = got_object_commit_get_parent_ids(commit);
3569 pid = SIMPLEQ_FIRST(parent_ids);
3570 error = got_commit_graph_iter_start(graph, pid->id, repo);
3571 got_object_commit_close(commit);
3575 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3576 error = got_commit_graph_iter_next(&parent_id, graph);
3578 if (error->code == GOT_ERR_ITER_COMPLETED) {
3579 error = got_error_msg(GOT_ERR_ANCESTRY,
3580 "ran out of commits to rebase before "
3581 "youngest common ancestor commit has "
3584 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3586 error = got_commit_graph_fetch_commits(graph, 1, repo);
3590 error = rebase_check_path_prefix(parent_id, commit_id,
3591 got_worktree_get_path_prefix(worktree), repo);
3595 error = got_object_qid_alloc(&qid, commit_id);
3598 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3599 commit_id = parent_id;
3603 if (SIMPLEQ_EMPTY(&commits)) {
3604 if (continue_rebase)
3605 error = rebase_complete(worktree, branch,
3606 new_base_branch, tmp_branch, repo);
3608 error = got_error(GOT_ERR_EMPTY_REBASE);
3613 SIMPLEQ_FOREACH(qid, &commits, entry) {
3614 commit_id = qid->id;
3615 parent_id = pid ? pid->id : yca_id;
3618 error = got_worktree_rebase_merge_files(&merged_paths,
3619 worktree, parent_id, commit_id, repo, rebase_progress,
3620 &rebase_status, check_cancelled, NULL);
3624 if (rebase_status == GOT_STATUS_CONFLICT) {
3625 got_worktree_rebase_pathlist_free(&merged_paths);
3629 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3631 got_worktree_rebase_pathlist_free(&merged_paths);
3636 if (rebase_status == GOT_STATUS_CONFLICT) {
3637 error = got_worktree_rebase_postpone(worktree);
3640 error = got_error_msg(GOT_ERR_CONFLICTS,
3641 "conflicts must be resolved before rebasing can continue");
3643 error = rebase_complete(worktree, branch, new_base_branch,
3646 got_object_id_queue_free(&commits);
3647 free(branch_head_commit_id);
3648 free(resume_commit_id);
3651 got_commit_graph_close(graph);
3653 got_object_commit_close(commit);
3655 got_ref_close(branch);
3656 if (new_base_branch)
3657 got_ref_close(new_base_branch);
3659 got_ref_close(tmp_branch);
3661 got_worktree_close(worktree);
3663 got_repo_close(repo);