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;
2076 * Don't let the user create a reference named '-'.
2077 * While technically a valid reference name, this case is usually
2078 * an unintended typo.
2080 if (refname[0] == '-' && refname[1] == '\0')
2081 return got_error(GOT_ERR_BAD_REF_NAME);
2083 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2086 struct got_reference *target_ref;
2088 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2090 err = got_ref_open(&target_ref, repo, target, 0);
2093 err = got_ref_resolve(&id, repo, target_ref);
2094 got_ref_close(target_ref);
2099 err = got_ref_alloc(&ref, refname, id);
2103 err = got_ref_write(ref, repo);
2111 static const struct got_error *
2112 cmd_ref(int argc, char *argv[])
2114 const struct got_error *error = NULL;
2115 struct got_repository *repo = NULL;
2116 struct got_worktree *worktree = NULL;
2117 char *cwd = NULL, *repo_path = NULL;
2118 int ch, do_list = 0;
2119 const char *delref = NULL;
2121 /* TODO: Add -s option for adding symbolic references. */
2122 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2128 repo_path = realpath(optarg, NULL);
2129 if (repo_path == NULL)
2130 err(1, "-r option");
2131 got_path_strip_trailing_slashes(repo_path);
2142 if (do_list && delref)
2143 errx(1, "-l and -d options are mutually exclusive\n");
2148 if (do_list || delref) {
2151 } else if (argc != 2)
2156 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2160 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2161 "sendfd unveil", NULL) == -1)
2165 cwd = getcwd(NULL, 0);
2167 error = got_error_from_errno("getcwd");
2171 if (repo_path == NULL) {
2172 error = got_worktree_open(&worktree, cwd);
2173 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2179 strdup(got_worktree_get_repo_path(worktree));
2180 if (repo_path == NULL)
2181 error = got_error_from_errno("strdup");
2185 repo_path = strdup(cwd);
2186 if (repo_path == NULL) {
2187 error = got_error_from_errno("strdup");
2193 error = got_repo_open(&repo, repo_path);
2197 error = apply_unveil(got_repo_get_path(repo), do_list,
2198 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2203 error = list_refs(repo);
2205 error = delete_ref(repo, delref);
2207 error = add_ref(repo, argv[0], argv[1]);
2210 got_repo_close(repo);
2212 got_worktree_close(worktree);
2222 "usage: %s branch [-r repository] -l | -d name | "
2223 "name [base-branch]\n", getprogname());
2227 static const struct got_error *
2228 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2230 static const struct got_error *err = NULL;
2231 struct got_reflist_head refs;
2232 struct got_reflist_entry *re;
2234 SIMPLEQ_INIT(&refs);
2236 err = got_ref_list(&refs, repo);
2240 SIMPLEQ_FOREACH(re, &refs, entry) {
2241 const char *refname, *marker = " ";
2243 refname = got_ref_get_name(re->ref);
2244 if (strncmp(refname, "refs/heads/", 11) != 0)
2246 if (worktree && strcmp(refname,
2247 got_worktree_get_head_ref_name(worktree)) == 0) {
2248 struct got_object_id *id = NULL;
2249 err = got_ref_resolve(&id, repo, re->ref);
2252 if (got_object_id_cmp(id,
2253 got_worktree_get_base_commit_id(worktree)) == 0)
2260 refstr = got_ref_to_str(re->ref);
2262 return got_error_from_errno("got_ref_to_str");
2263 printf("%s%s: %s\n", marker, refname, refstr);
2267 got_ref_list_free(&refs);
2271 static const struct got_error *
2272 delete_branch(struct got_repository *repo, const char *branch_name)
2274 const struct got_error *err = NULL;
2275 struct got_reference *ref;
2278 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2279 return got_error_from_errno("asprintf");
2281 err = got_ref_open(&ref, repo, refname, 0);
2285 err = got_ref_delete(ref, repo);
2292 static const struct got_error *
2293 add_branch(struct got_repository *repo, const char *branch_name,
2294 const char *base_branch)
2296 const struct got_error *err = NULL;
2297 struct got_object_id *id = NULL;
2298 struct got_reference *ref = NULL;
2299 char *base_refname = NULL, *refname = NULL;
2300 struct got_reference *base_ref;
2303 * Don't let the user create a branch named '-'.
2304 * While technically a valid reference name, this case is usually
2305 * an unintended typo.
2307 if (branch_name[0] == '-' && branch_name[1] == '\0')
2308 return got_error(GOT_ERR_BAD_REF_NAME);
2310 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2311 base_refname = strdup(GOT_REF_HEAD);
2312 if (base_refname == NULL)
2313 return got_error_from_errno("strdup");
2314 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2315 return got_error_from_errno("asprintf");
2317 err = got_ref_open(&base_ref, repo, base_refname, 0);
2320 err = got_ref_resolve(&id, repo, base_ref);
2321 got_ref_close(base_ref);
2325 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2326 err = got_error_from_errno("asprintf");
2330 err = got_ref_open(&ref, repo, refname, 0);
2332 err = got_error(GOT_ERR_BRANCH_EXISTS);
2334 } else if (err->code != GOT_ERR_NOT_REF)
2337 err = got_ref_alloc(&ref, refname, id);
2341 err = got_ref_write(ref, repo);
2351 static const struct got_error *
2352 cmd_branch(int argc, char *argv[])
2354 const struct got_error *error = NULL;
2355 struct got_repository *repo = NULL;
2356 struct got_worktree *worktree = NULL;
2357 char *cwd = NULL, *repo_path = NULL;
2358 int ch, do_list = 0;
2359 const char *delref = NULL;
2361 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2367 repo_path = realpath(optarg, NULL);
2368 if (repo_path == NULL)
2369 err(1, "-r option");
2370 got_path_strip_trailing_slashes(repo_path);
2381 if (do_list && delref)
2382 errx(1, "-l and -d options are mutually exclusive\n");
2387 if (do_list || delref) {
2390 } else if (argc < 1 || argc > 2)
2395 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2399 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2400 "sendfd unveil", NULL) == -1)
2404 cwd = getcwd(NULL, 0);
2406 error = got_error_from_errno("getcwd");
2410 if (repo_path == NULL) {
2411 error = got_worktree_open(&worktree, cwd);
2412 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2418 strdup(got_worktree_get_repo_path(worktree));
2419 if (repo_path == NULL)
2420 error = got_error_from_errno("strdup");
2424 repo_path = strdup(cwd);
2425 if (repo_path == NULL) {
2426 error = got_error_from_errno("strdup");
2432 error = got_repo_open(&repo, repo_path);
2436 error = apply_unveil(got_repo_get_path(repo), do_list,
2437 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2442 error = list_branches(repo, worktree);
2444 error = delete_branch(repo, delref);
2446 const char *base_branch;
2448 base_branch = worktree ?
2449 got_worktree_get_head_ref_name(worktree) :
2452 base_branch = argv[1];
2453 error = add_branch(repo, argv[0], base_branch);
2457 got_repo_close(repo);
2459 got_worktree_close(worktree);
2468 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2472 static const struct got_error *
2473 cmd_add(int argc, char *argv[])
2475 const struct got_error *error = NULL;
2476 struct got_repository *repo = NULL;
2477 struct got_worktree *worktree = NULL;
2479 struct got_pathlist_head paths;
2480 struct got_pathlist_entry *pe;
2485 while ((ch = getopt(argc, argv, "")) != -1) {
2499 /* make sure each file exists before doing anything halfway */
2500 for (x = 0; x < argc; x++) {
2501 char *path = realpath(argv[x], NULL);
2503 error = got_error_from_errno2("realpath", argv[x]);
2509 cwd = getcwd(NULL, 0);
2511 error = got_error_from_errno("getcwd");
2515 error = got_worktree_open(&worktree, cwd);
2519 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2523 error = apply_unveil(got_repo_get_path(repo), 1,
2524 got_worktree_get_root_path(worktree), 0);
2528 for (x = 0; x < argc; x++) {
2529 char *path = realpath(argv[x], NULL);
2531 error = got_error_from_errno2("realpath", argv[x]);
2535 got_path_strip_trailing_slashes(path);
2536 error = got_pathlist_insert(&pe, &paths, path, NULL);
2542 error = got_worktree_schedule_add(worktree, &paths, print_status,
2546 got_repo_close(repo);
2548 got_worktree_close(worktree);
2549 TAILQ_FOREACH(pe, &paths, entry)
2550 free((char *)pe->path);
2551 got_pathlist_free(&paths);
2559 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2563 static const struct got_error *
2564 cmd_remove(int argc, char *argv[])
2566 const struct got_error *error = NULL;
2567 struct got_worktree *worktree = NULL;
2568 struct got_repository *repo = NULL;
2570 struct got_pathlist_head paths;
2571 struct got_pathlist_entry *pe;
2572 int ch, i, delete_local_mods = 0;
2576 while ((ch = getopt(argc, argv, "f")) != -1) {
2579 delete_local_mods = 1;
2593 /* make sure each file exists before doing anything halfway */
2594 for (i = 0; i < argc; i++) {
2595 char *path = realpath(argv[i], NULL);
2597 error = got_error_from_errno2("realpath", argv[i]);
2603 cwd = getcwd(NULL, 0);
2605 error = got_error_from_errno("getcwd");
2608 error = got_worktree_open(&worktree, cwd);
2612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2616 error = apply_unveil(got_repo_get_path(repo), 1,
2617 got_worktree_get_root_path(worktree), 0);
2621 for (i = 0; i < argc; i++) {
2622 char *path = realpath(argv[i], NULL);
2624 error = got_error_from_errno2("realpath", argv[i]);
2628 got_path_strip_trailing_slashes(path);
2629 error = got_pathlist_insert(&pe, &paths, path, NULL);
2635 error = got_worktree_schedule_delete(worktree, &paths,
2636 delete_local_mods, print_status, NULL, repo);
2641 got_repo_close(repo);
2643 got_worktree_close(worktree);
2644 TAILQ_FOREACH(pe, &paths, entry)
2645 free((char *)pe->path);
2646 got_pathlist_free(&paths);
2654 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2658 static const struct got_error *
2659 revert_progress(void *arg, unsigned char status, const char *path)
2661 while (path[0] == '/')
2663 printf("%c %s\n", status, path);
2667 static const struct got_error *
2668 cmd_revert(int argc, char *argv[])
2670 const struct got_error *error = NULL;
2671 struct got_worktree *worktree = NULL;
2672 struct got_repository *repo = NULL;
2673 char *cwd = NULL, *path = NULL;
2674 struct got_pathlist_head paths;
2675 struct got_pathlist_entry *pe;
2680 while ((ch = getopt(argc, argv, "")) != -1) {
2694 for (i = 0; i < argc; i++) {
2695 char *path = realpath(argv[i], NULL);
2697 error = got_error_from_errno2("realpath", argv[i]);
2701 got_path_strip_trailing_slashes(path);
2702 error = got_pathlist_insert(&pe, &paths, path, NULL);
2709 cwd = getcwd(NULL, 0);
2711 error = got_error_from_errno("getcwd");
2714 error = got_worktree_open(&worktree, cwd);
2718 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2722 error = apply_unveil(got_repo_get_path(repo), 1,
2723 got_worktree_get_root_path(worktree), 0);
2727 error = got_worktree_revert(worktree, &paths,
2728 revert_progress, NULL, repo);
2733 got_repo_close(repo);
2735 got_worktree_close(worktree);
2744 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
2749 spawn_editor(const char *editor, const char *file)
2752 sig_t sighup, sigint, sigquit;
2755 sighup = signal(SIGHUP, SIG_IGN);
2756 sigint = signal(SIGINT, SIG_IGN);
2757 sigquit = signal(SIGQUIT, SIG_IGN);
2759 switch (pid = fork()) {
2763 execl(editor, editor, file, (char *)NULL);
2767 while (waitpid(pid, &st, 0) == -1)
2772 (void)signal(SIGHUP, sighup);
2773 (void)signal(SIGINT, sigint);
2774 (void)signal(SIGQUIT, sigquit);
2776 if (!WIFEXITED(st)) {
2781 return WEXITSTATUS(st);
2784 struct collect_commit_logmsg_arg {
2785 const char *cmdline_log;
2787 const char *worktree_path;
2788 const char *branch_name;
2789 const char *repo_path;
2794 static const struct got_error *
2795 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
2798 char *initial_content = NULL;
2799 struct got_pathlist_entry *pe;
2800 const struct got_error *err = NULL;
2801 char *template = NULL;
2802 struct collect_commit_logmsg_arg *a = arg;
2804 struct stat st, st2;
2807 int fd, content_changed = 0;
2809 /* if a message was specified on the command line, just use it */
2810 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
2811 len = strlen(a->cmdline_log) + 1;
2812 *logmsg = malloc(len + 1);
2813 if (*logmsg == NULL)
2814 return got_error_from_errno("malloc");
2815 strlcpy(*logmsg, a->cmdline_log, len);
2819 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
2820 return got_error_from_errno("asprintf");
2822 if (asprintf(&initial_content,
2823 "\n# changes to be committed on branch %s:\n",
2824 a->branch_name) == -1)
2825 return got_error_from_errno("asprintf");
2827 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
2831 dprintf(fd, initial_content);
2833 TAILQ_FOREACH(pe, commitable_paths, entry) {
2834 struct got_commitable *ct = pe->data;
2835 dprintf(fd, "# %c %s\n",
2836 got_commitable_get_status(ct),
2837 got_commitable_get_path(ct));
2841 if (stat(a->logmsg_path, &st) == -1) {
2842 err = got_error_from_errno2("stat", a->logmsg_path);
2846 if (spawn_editor(a->editor, a->logmsg_path) == -1) {
2847 err = got_error_from_errno("failed spawning editor");
2851 if (stat(a->logmsg_path, &st2) == -1) {
2852 err = got_error_from_errno("stat");
2856 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size) {
2857 unlink(a->logmsg_path);
2858 free(a->logmsg_path);
2859 a->logmsg_path = NULL;
2860 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2861 "no changes made to commit message, aborting");
2865 *logmsg = malloc(st2.st_size + 1);
2866 if (*logmsg == NULL) {
2867 err = got_error_from_errno("malloc");
2870 (*logmsg)[0] = '\0';
2873 fp = fopen(a->logmsg_path, "r");
2875 err = got_error_from_errno("fopen");
2878 while (fgets(buf, sizeof(buf), fp) != NULL) {
2879 if (!content_changed && strcmp(buf, initial_content) != 0)
2880 content_changed = 1;
2881 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
2882 continue; /* remove comments and leading empty lines */
2883 len = strlcat(*logmsg, buf, st2.st_size);
2887 while (len > 0 && (*logmsg)[len - 1] == '\n') {
2888 (*logmsg)[len - 1] = '\0';
2892 if (len == 0 || !content_changed) {
2893 unlink(a->logmsg_path);
2894 free(a->logmsg_path);
2895 a->logmsg_path = NULL;
2896 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
2897 "commit message cannot be empty, aborting");
2901 free(initial_content);
2904 /* Editor is done; we can now apply unveil(2) */
2906 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
2910 static const struct got_error *
2911 cmd_commit(int argc, char *argv[])
2913 const struct got_error *error = NULL;
2914 struct got_worktree *worktree = NULL;
2915 struct got_repository *repo = NULL;
2916 char *cwd = NULL, *path = NULL, *id_str = NULL;
2917 struct got_object_id *id = NULL;
2918 const char *logmsg = NULL;
2919 const char *got_author = getenv("GOT_AUTHOR");
2920 struct collect_commit_logmsg_arg cl_arg;
2921 char *editor = NULL;
2922 int ch, rebase_in_progress;
2924 while ((ch = getopt(argc, argv, "m:")) != -1) {
2939 path = realpath(argv[0], NULL);
2941 error = got_error_from_errno2("realpath", argv[0]);
2944 got_path_strip_trailing_slashes(path);
2945 } else if (argc != 0)
2948 if (got_author == NULL) {
2949 /* TODO: Look current user up in password database */
2950 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
2954 cwd = getcwd(NULL, 0);
2956 error = got_error_from_errno("getcwd");
2959 error = got_worktree_open(&worktree, cwd);
2963 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
2966 if (rebase_in_progress) {
2967 error = got_error(GOT_ERR_REBASING);
2971 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2976 * unveil(2) traverses exec(2); if an editor is used we have
2977 * to apply unveil after the log message has been written.
2979 if (logmsg == NULL || strlen(logmsg) == 0)
2980 error = get_editor(&editor);
2982 error = apply_unveil(got_repo_get_path(repo), 0,
2983 got_worktree_get_root_path(worktree), 0);
2987 cl_arg.editor = editor;
2988 cl_arg.cmdline_log = logmsg;
2989 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
2990 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
2991 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
2992 cl_arg.branch_name += 5;
2993 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
2994 cl_arg.branch_name += 6;
2995 cl_arg.repo_path = got_repo_get_path(repo);
2996 cl_arg.logmsg_path = NULL;
2997 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
2998 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3000 if (cl_arg.logmsg_path)
3001 fprintf(stderr, "%s: log message preserved in %s\n",
3002 getprogname(), cl_arg.logmsg_path);
3006 if (cl_arg.logmsg_path)
3007 unlink(cl_arg.logmsg_path);
3009 error = got_object_id_str(&id_str, id);
3012 printf("Created commit %s\n", id_str);
3015 got_repo_close(repo);
3017 got_worktree_close(worktree);
3026 usage_cherrypick(void)
3028 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3032 static const struct got_error *
3033 cmd_cherrypick(int argc, char *argv[])
3035 const struct got_error *error = NULL;
3036 struct got_worktree *worktree = NULL;
3037 struct got_repository *repo = NULL;
3038 char *cwd = NULL, *commit_id_str = NULL;
3039 struct got_object_id *commit_id = NULL;
3040 struct got_commit_object *commit = NULL;
3041 struct got_object_qid *pid;
3042 struct got_reference *head_ref = NULL;
3043 int ch, did_something = 0;
3045 while ((ch = getopt(argc, argv, "")) != -1) {
3059 cwd = getcwd(NULL, 0);
3061 error = got_error_from_errno("getcwd");
3064 error = got_worktree_open(&worktree, cwd);
3068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3072 error = apply_unveil(got_repo_get_path(repo), 0,
3073 got_worktree_get_root_path(worktree), 0);
3077 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3078 GOT_OBJ_TYPE_COMMIT, repo);
3079 if (error != NULL) {
3080 struct got_reference *ref;
3081 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3083 error = got_ref_open(&ref, repo, argv[0], 0);
3086 error = got_ref_resolve(&commit_id, repo, ref);
3091 error = got_object_id_str(&commit_id_str, commit_id);
3095 error = got_ref_open(&head_ref, repo,
3096 got_worktree_get_head_ref_name(worktree), 0);
3100 error = check_same_branch(commit_id, head_ref, repo);
3102 if (error->code != GOT_ERR_ANCESTRY)
3106 error = got_error(GOT_ERR_SAME_BRANCH);
3110 error = got_object_open_as_commit(&commit, repo, commit_id);
3113 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3114 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3115 commit_id, repo, update_progress, &did_something, check_cancelled,
3121 printf("Merged commit %s\n", commit_id_str);
3124 got_object_commit_close(commit);
3125 free(commit_id_str);
3127 got_ref_close(head_ref);
3129 got_worktree_close(worktree);
3131 got_repo_close(repo);
3138 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3142 static const struct got_error *
3143 cmd_backout(int argc, char *argv[])
3145 const struct got_error *error = NULL;
3146 struct got_worktree *worktree = NULL;
3147 struct got_repository *repo = NULL;
3148 char *cwd = NULL, *commit_id_str = NULL;
3149 struct got_object_id *commit_id = NULL;
3150 struct got_commit_object *commit = NULL;
3151 struct got_object_qid *pid;
3152 struct got_reference *head_ref = NULL;
3153 int ch, did_something = 0;
3155 while ((ch = getopt(argc, argv, "")) != -1) {
3169 cwd = getcwd(NULL, 0);
3171 error = got_error_from_errno("getcwd");
3174 error = got_worktree_open(&worktree, cwd);
3178 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3182 error = apply_unveil(got_repo_get_path(repo), 0,
3183 got_worktree_get_root_path(worktree), 0);
3187 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3188 GOT_OBJ_TYPE_COMMIT, repo);
3189 if (error != NULL) {
3190 struct got_reference *ref;
3191 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3193 error = got_ref_open(&ref, repo, argv[0], 0);
3196 error = got_ref_resolve(&commit_id, repo, ref);
3201 error = got_object_id_str(&commit_id_str, commit_id);
3205 error = got_ref_open(&head_ref, repo,
3206 got_worktree_get_head_ref_name(worktree), 0);
3210 error = check_same_branch(commit_id, head_ref, repo);
3214 error = got_object_open_as_commit(&commit, repo, commit_id);
3217 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3219 error = got_error(GOT_ERR_ROOT_COMMIT);
3223 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3224 update_progress, &did_something, check_cancelled, NULL);
3229 printf("Backed out commit %s\n", commit_id_str);
3232 got_object_commit_close(commit);
3233 free(commit_id_str);
3235 got_ref_close(head_ref);
3237 got_worktree_close(worktree);
3239 got_repo_close(repo);
3246 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3251 static const struct got_error *
3252 show_rebase_progress(struct got_commit_object *commit,
3253 struct got_object_id *old_id, struct got_object_id *new_id)
3255 const struct got_error *err;
3256 char *old_id_str = NULL, *new_id_str = NULL;
3257 char *logmsg0 = NULL, *logmsg, *nl;
3260 err = got_object_id_str(&old_id_str, old_id);
3265 err = got_object_id_str(&new_id_str, new_id);
3270 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3271 if (logmsg0 == NULL) {
3272 err = got_error_from_errno("strdup");
3277 while (isspace((unsigned char)logmsg[0]))
3280 old_id_str[12] = '\0';
3282 new_id_str[12] = '\0';
3283 len = strlen(logmsg);
3287 nl = strchr(logmsg, '\n');
3290 printf("%s -> %s: %s\n", old_id_str,
3291 new_id_str ? new_id_str : "no-op change", logmsg);
3299 static const struct got_error *
3300 rebase_progress(void *arg, unsigned char status, const char *path)
3302 unsigned char *rebase_status = arg;
3304 while (path[0] == '/')
3306 printf("%c %s\n", status, path);
3308 if (*rebase_status == GOT_STATUS_CONFLICT)
3310 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3311 *rebase_status = status;
3315 static const struct got_error *
3316 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3317 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3318 struct got_repository *repo)
3320 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3321 return got_worktree_rebase_complete(worktree,
3322 new_base_branch, tmp_branch, branch, repo);
3325 static const struct got_error *
3326 rebase_commit(struct got_pathlist_head *merged_paths,
3327 struct got_worktree *worktree, struct got_reference *tmp_branch,
3328 struct got_object_id *commit_id, struct got_repository *repo)
3330 const struct got_error *error;
3331 struct got_commit_object *commit;
3332 struct got_object_id *new_commit_id;
3334 error = got_object_open_as_commit(&commit, repo, commit_id);
3338 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3339 worktree, tmp_branch, commit, commit_id, repo);
3341 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3343 error = show_rebase_progress(commit, commit_id, NULL);
3345 error = show_rebase_progress(commit, commit_id, new_commit_id);
3346 free(new_commit_id);
3349 got_object_commit_close(commit);
3353 struct check_path_prefix_arg {
3354 const char *path_prefix;
3358 static const struct got_error *
3359 check_path_prefix(void *arg, struct got_blob_object *blob1,
3360 struct got_blob_object *blob2, struct got_object_id *id1,
3361 struct got_object_id *id2, const char *path1, const char *path2,
3362 struct got_repository *repo)
3364 struct check_path_prefix_arg *a = arg;
3366 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3367 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3368 return got_error(GOT_ERR_REBASE_PATH);
3373 static const struct got_error *
3374 rebase_check_path_prefix(struct got_object_id *parent_id,
3375 struct got_object_id *commit_id, const char *path_prefix,
3376 struct got_repository *repo)
3378 const struct got_error *err;
3379 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3380 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3381 struct check_path_prefix_arg cpp_arg;
3383 if (got_path_is_root_dir(path_prefix))
3386 err = got_object_open_as_commit(&commit, repo, commit_id);
3390 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3394 err = got_object_open_as_tree(&tree1, repo,
3395 got_object_commit_get_tree_id(parent_commit));
3399 err = got_object_open_as_tree(&tree2, repo,
3400 got_object_commit_get_tree_id(commit));
3404 cpp_arg.path_prefix = path_prefix;
3405 cpp_arg.len = strlen(path_prefix);
3406 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3410 got_object_tree_close(tree1);
3412 got_object_tree_close(tree2);
3414 got_object_commit_close(commit);
3416 got_object_commit_close(parent_commit);
3420 static const struct got_error *
3421 cmd_rebase(int argc, char *argv[])
3423 const struct got_error *error = NULL;
3424 struct got_worktree *worktree = NULL;
3425 struct got_repository *repo = NULL;
3427 struct got_reference *branch = NULL;
3428 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3429 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3430 struct got_object_id *resume_commit_id = NULL;
3431 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3432 struct got_commit_graph *graph = NULL;
3433 struct got_commit_object *commit = NULL;
3434 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3435 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3436 struct got_object_id_queue commits;
3437 struct got_pathlist_head merged_paths;
3438 const struct got_object_id_queue *parent_ids;
3439 struct got_object_qid *qid, *pid;
3441 SIMPLEQ_INIT(&commits);
3442 TAILQ_INIT(&merged_paths);
3444 while ((ch = getopt(argc, argv, "ac")) != -1) {
3450 continue_rebase = 1;
3461 if (abort_rebase && continue_rebase)
3463 else if (abort_rebase || continue_rebase) {
3466 } else if (argc != 1)
3469 cwd = getcwd(NULL, 0);
3471 error = got_error_from_errno("getcwd");
3474 error = got_worktree_open(&worktree, cwd);
3478 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3482 error = apply_unveil(got_repo_get_path(repo), 0,
3483 got_worktree_get_root_path(worktree), 0);
3487 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3491 if (rebase_in_progress && abort_rebase) {
3493 error = got_worktree_rebase_continue(&resume_commit_id,
3494 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3497 printf("Switching work tree to %s\n",
3498 got_ref_get_symref_target(new_base_branch));
3499 error = got_worktree_rebase_abort(worktree, repo,
3500 new_base_branch, update_progress, &did_something);
3503 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3504 goto done; /* nothing else to do */
3505 } else if (abort_rebase) {
3506 error = got_error(GOT_ERR_NOT_REBASING);
3510 if (continue_rebase) {
3511 error = got_worktree_rebase_continue(&resume_commit_id,
3512 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3516 error = rebase_commit(NULL, worktree, tmp_branch,
3517 resume_commit_id, repo);
3521 yca_id = got_object_id_dup(resume_commit_id);
3522 if (yca_id == NULL) {
3523 error = got_error_from_errno("got_object_id_dup");
3527 error = got_ref_open(&branch, repo, argv[0], 0);
3531 error = check_same_branch(
3532 got_worktree_get_base_commit_id(worktree), branch, repo);
3534 if (error->code != GOT_ERR_ANCESTRY)
3538 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3539 "specified branch resolves to a commit which "
3540 "is already contained in work tree's branch");
3545 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3549 if (!continue_rebase) {
3550 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3551 got_worktree_get_base_commit_id(worktree),
3552 branch_head_commit_id, repo);
3555 if (yca_id == NULL) {
3556 error = got_error_msg(GOT_ERR_ANCESTRY,
3557 "specified branch shares no common ancestry "
3558 "with work tree's branch");
3562 error = got_worktree_rebase_prepare(&new_base_branch,
3563 &tmp_branch, worktree, branch, repo);
3568 commit_id = branch_head_commit_id;
3569 error = got_object_open_as_commit(&commit, repo, commit_id);
3573 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3576 parent_ids = got_object_commit_get_parent_ids(commit);
3577 pid = SIMPLEQ_FIRST(parent_ids);
3578 error = got_commit_graph_iter_start(graph, pid->id, repo);
3579 got_object_commit_close(commit);
3583 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3584 error = got_commit_graph_iter_next(&parent_id, graph);
3586 if (error->code == GOT_ERR_ITER_COMPLETED) {
3587 error = got_error_msg(GOT_ERR_ANCESTRY,
3588 "ran out of commits to rebase before "
3589 "youngest common ancestor commit has "
3592 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3594 error = got_commit_graph_fetch_commits(graph, 1, repo);
3598 error = rebase_check_path_prefix(parent_id, commit_id,
3599 got_worktree_get_path_prefix(worktree), repo);
3603 error = got_object_qid_alloc(&qid, commit_id);
3606 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3607 commit_id = parent_id;
3611 if (SIMPLEQ_EMPTY(&commits)) {
3612 if (continue_rebase)
3613 error = rebase_complete(worktree, branch,
3614 new_base_branch, tmp_branch, repo);
3616 error = got_error(GOT_ERR_EMPTY_REBASE);
3621 SIMPLEQ_FOREACH(qid, &commits, entry) {
3622 commit_id = qid->id;
3623 parent_id = pid ? pid->id : yca_id;
3626 error = got_worktree_rebase_merge_files(&merged_paths,
3627 worktree, parent_id, commit_id, repo, rebase_progress,
3628 &rebase_status, check_cancelled, NULL);
3632 if (rebase_status == GOT_STATUS_CONFLICT) {
3633 got_worktree_rebase_pathlist_free(&merged_paths);
3637 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3639 got_worktree_rebase_pathlist_free(&merged_paths);
3644 if (rebase_status == GOT_STATUS_CONFLICT) {
3645 error = got_worktree_rebase_postpone(worktree);
3648 error = got_error_msg(GOT_ERR_CONFLICTS,
3649 "conflicts must be resolved before rebasing can continue");
3651 error = rebase_complete(worktree, branch, new_base_branch,
3654 got_object_id_queue_free(&commits);
3655 free(branch_head_commit_id);
3656 free(resume_commit_id);
3659 got_commit_graph_close(graph);
3661 got_object_commit_close(commit);
3663 got_ref_close(branch);
3664 if (new_base_branch)
3665 got_ref_close(new_base_branch);
3667 got_ref_close(tmp_branch);
3669 got_worktree_close(worktree);
3671 got_repo_close(repo);