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_import(void);
80 __dead static void usage_checkout(void);
81 __dead static void usage_update(void);
82 __dead static void usage_log(void);
83 __dead static void usage_diff(void);
84 __dead static void usage_blame(void);
85 __dead static void usage_tree(void);
86 __dead static void usage_status(void);
87 __dead static void usage_ref(void);
88 __dead static void usage_branch(void);
89 __dead static void usage_add(void);
90 __dead static void usage_remove(void);
91 __dead static void usage_revert(void);
92 __dead static void usage_commit(void);
93 __dead static void usage_cherrypick(void);
94 __dead static void usage_backout(void);
95 __dead static void usage_rebase(void);
97 static const struct got_error* cmd_init(int, char *[]);
98 static const struct got_error* cmd_import(int, char *[]);
99 static const struct got_error* cmd_checkout(int, char *[]);
100 static const struct got_error* cmd_update(int, char *[]);
101 static const struct got_error* cmd_log(int, char *[]);
102 static const struct got_error* cmd_diff(int, char *[]);
103 static const struct got_error* cmd_blame(int, char *[]);
104 static const struct got_error* cmd_tree(int, char *[]);
105 static const struct got_error* cmd_status(int, char *[]);
106 static const struct got_error* cmd_ref(int, char *[]);
107 static const struct got_error* cmd_branch(int, char *[]);
108 static const struct got_error* cmd_add(int, char *[]);
109 static const struct got_error* cmd_remove(int, char *[]);
110 static const struct got_error* cmd_revert(int, char *[]);
111 static const struct got_error* cmd_commit(int, char *[]);
112 static const struct got_error* cmd_cherrypick(int, char *[]);
113 static const struct got_error* cmd_backout(int, char *[]);
114 static const struct got_error* cmd_rebase(int, char *[]);
116 static struct got_cmd got_commands[] = {
117 { "init", cmd_init, usage_init, "" },
118 { "import", cmd_import, usage_import, "" },
119 { "checkout", cmd_checkout, usage_checkout, "co" },
120 { "update", cmd_update, usage_update, "up" },
121 { "log", cmd_log, usage_log, "" },
122 { "diff", cmd_diff, usage_diff, "" },
123 { "blame", cmd_blame, usage_blame, "" },
124 { "tree", cmd_tree, usage_tree, "" },
125 { "status", cmd_status, usage_status, "st" },
126 { "ref", cmd_ref, usage_ref, "" },
127 { "branch", cmd_branch, usage_branch, "br" },
128 { "add", cmd_add, usage_add, "" },
129 { "remove", cmd_remove, usage_remove, "rm" },
130 { "revert", cmd_revert, usage_revert, "rv" },
131 { "commit", cmd_commit, usage_commit, "ci" },
132 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 { "backout", cmd_backout, usage_backout, "bo" },
134 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 fprintf(stderr, "commands:");
143 for (i = 0; i < nitems(got_commands); i++) {
144 struct got_cmd *cmd = &got_commands[i];
145 fprintf(stderr, " %s", cmd->cmd_name);
151 main(int argc, char *argv[])
158 setlocale(LC_CTYPE, "");
160 while ((ch = getopt(argc, argv, "h")) != -1) {
178 signal(SIGINT, catch_sigint);
179 signal(SIGPIPE, catch_sigpipe);
181 for (i = 0; i < nitems(got_commands); i++) {
182 const struct got_error *error;
184 cmd = &got_commands[i];
186 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 strcmp(cmd->cmd_alias, argv[0]) != 0)
191 got_commands[i].cmd_usage();
193 error = got_commands[i].cmd_main(argc, argv);
194 if (error && !(sigint_received || sigpipe_received)) {
195 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
202 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
210 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
216 static const struct got_error *
217 get_editor(char **abspath)
219 const struct got_error *err = NULL;
222 editor = getenv("VISUAL");
224 editor = getenv("EDITOR");
227 err = got_path_find_prog(abspath, editor);
232 if (*abspath == NULL) {
233 *abspath = strdup("/bin/ed");
234 if (*abspath == NULL)
235 return got_error_from_errno("strdup");
241 static const struct got_error *
242 apply_unveil(const char *repo_path, int repo_read_only,
243 const char *worktree_path, int create_worktree)
245 const struct got_error *err;
247 if (create_worktree) {
248 /* Pre-create work tree path to avoid unveiling its parents. */
249 err = got_path_mkdir(worktree_path);
251 if (errno == EEXIST) {
252 if (got_path_dir_is_empty(worktree_path)) {
256 err = got_error_path(worktree_path,
257 GOT_ERR_DIR_NOT_EMPTY);
261 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
265 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
266 return got_error_from_errno2("unveil", repo_path);
268 if (worktree_path && unveil(worktree_path, "rwc") != 0)
269 return got_error_from_errno2("unveil", worktree_path);
271 if (unveil("/tmp", "rwc") != 0)
272 return got_error_from_errno2("unveil", "/tmp");
274 err = got_privsep_unveil_exec_helpers();
278 if (unveil(NULL, NULL) != 0)
279 return got_error_from_errno("unveil");
287 fprintf(stderr, "usage: %s init path\n", getprogname());
291 static const struct got_error *
292 cmd_init(int argc, char *argv[])
294 const struct got_error *error = NULL;
295 char *repo_path = NULL;
298 while ((ch = getopt(argc, argv, "")) != -1) {
310 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
316 repo_path = strdup(argv[0]);
317 if (repo_path == NULL)
318 return got_error_from_errno("strdup");
320 got_path_strip_trailing_slashes(repo_path);
322 error = got_path_mkdir(repo_path);
324 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
327 error = apply_unveil(repo_path, 0, NULL, 0);
331 error = got_repo_init(repo_path);
343 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
344 "[-r repository-path] [-I pattern] path\n", getprogname());
349 spawn_editor(const char *editor, const char *file)
352 sig_t sighup, sigint, sigquit;
355 sighup = signal(SIGHUP, SIG_IGN);
356 sigint = signal(SIGINT, SIG_IGN);
357 sigquit = signal(SIGQUIT, SIG_IGN);
359 switch (pid = fork()) {
363 execl(editor, editor, file, (char *)NULL);
367 while (waitpid(pid, &st, 0) == -1)
372 (void)signal(SIGHUP, sighup);
373 (void)signal(SIGINT, sigint);
374 (void)signal(SIGQUIT, sigquit);
376 if (!WIFEXITED(st)) {
381 return WEXITSTATUS(st);
384 static const struct got_error *
385 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
386 const char *initial_content)
388 const struct got_error *err = NULL;
392 int content_changed = 0;
397 if (stat(logmsg_path, &st) == -1)
398 return got_error_from_errno2("stat", logmsg_path);
400 if (spawn_editor(editor, logmsg_path) == -1)
401 return got_error_from_errno("failed spawning editor");
403 if (stat(logmsg_path, &st2) == -1)
404 return got_error_from_errno("stat");
406 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
407 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
408 "no changes made to commit message, aborting");
410 *logmsg = malloc(st2.st_size + 1);
412 return got_error_from_errno("malloc");
416 fp = fopen(logmsg_path, "r");
418 err = got_error_from_errno("fopen");
421 while (fgets(buf, sizeof(buf), fp) != NULL) {
422 if (!content_changed && strcmp(buf, initial_content) != 0)
424 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
425 continue; /* remove comments and leading empty lines */
426 len = strlcat(*logmsg, buf, st2.st_size);
430 while (len > 0 && (*logmsg)[len - 1] == '\n') {
431 (*logmsg)[len - 1] = '\0';
435 if (len == 0 || !content_changed)
436 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
437 "commit message cannot be empty, aborting");
446 static const struct got_error *
447 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
448 const char *branch_name)
450 char *initial_content = NULL, *logmsg_path = NULL;
451 const struct got_error *err = NULL;
454 if (asprintf(&initial_content,
455 "\n# %s to be imported to branch %s\n", path_dir,
457 return got_error_from_errno("asprintf");
459 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
463 dprintf(fd, initial_content);
466 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
468 free(initial_content);
473 static const struct got_error *
474 import_progress(void *arg, const char *path)
476 printf("A %s\n", path);
480 static const struct got_error *
481 cmd_import(int argc, char *argv[])
483 const struct got_error *error = NULL;
484 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
486 const char *got_author = getenv("GOT_AUTHOR");
487 const char *branch_name = "master";
488 char *refname = NULL, *id_str = NULL;
489 struct got_repository *repo = NULL;
490 struct got_reference *branch_ref = NULL, *head_ref = NULL;
491 struct got_object_id *new_commit_id = NULL;
493 struct got_pathlist_head ignores;
494 struct got_pathlist_entry *pe;
496 TAILQ_INIT(&ignores);
498 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
501 branch_name = optarg;
504 logmsg = strdup(optarg);
505 if (logmsg == NULL) {
506 error = got_error_from_errno("strdup");
511 repo_path = realpath(optarg, NULL);
512 if (repo_path == NULL) {
513 error = got_error_from_errno("realpath");
518 if (optarg[0] == '\0')
520 error = got_pathlist_insert(&pe, &ignores, optarg,
535 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
542 if (got_author == NULL) {
543 /* TODO: Look current user up in password database */
544 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
548 if (repo_path == NULL) {
549 repo_path = getcwd(NULL, 0);
550 if (repo_path == NULL)
551 return got_error_from_errno("getcwd");
553 got_path_strip_trailing_slashes(repo_path);
554 error = got_repo_open(&repo, repo_path);
558 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
559 error = got_error_from_errno("asprintf");
563 error = got_ref_open(&branch_ref, repo, refname, 0);
565 if (error->code != GOT_ERR_NOT_REF)
568 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
569 "import target branch already exists");
573 path_dir = realpath(argv[0], NULL);
574 if (path_dir == NULL) {
575 error = got_error_from_errno("realpath");
578 got_path_strip_trailing_slashes(path_dir);
581 * unveil(2) traverses exec(2); if an editor is used we have
582 * to apply unveil after the log message has been written.
584 if (logmsg == NULL || strlen(logmsg) == 0) {
585 error = get_editor(&editor);
588 error = collect_import_msg(&logmsg, editor, path_dir, refname);
593 if (unveil(path_dir, "r") != 0)
594 return got_error_from_errno2("unveil", path_dir);
596 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
600 error = got_repo_import(&new_commit_id, path_dir, logmsg,
601 got_author, &ignores, repo, import_progress, NULL);
605 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
609 error = got_ref_write(branch_ref, repo);
613 error = got_object_id_str(&id_str, new_commit_id);
617 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
619 if (error->code != GOT_ERR_NOT_REF)
622 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
627 error = got_ref_write(head_ref, repo);
632 printf("Created branch %s with commit %s\n",
633 got_ref_get_name(branch_ref), id_str);
641 got_ref_close(branch_ref);
643 got_ref_close(head_ref);
650 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
651 "[-p prefix] repository-path [worktree-path]\n", getprogname());
655 static const struct got_error *
656 checkout_progress(void *arg, unsigned char status, const char *path)
658 char *worktree_path = arg;
660 /* Base commit bump happens silently. */
661 if (status == GOT_STATUS_BUMP_BASE)
664 while (path[0] == '/')
667 printf("%c %s/%s\n", status, worktree_path, path);
671 static const struct got_error *
672 check_cancelled(void *arg)
674 if (sigint_received || sigpipe_received)
675 return got_error(GOT_ERR_CANCELLED);
679 static const struct got_error *
680 check_linear_ancestry(struct got_object_id *commit_id,
681 struct got_object_id *base_commit_id, struct got_repository *repo)
683 const struct got_error *err = NULL;
684 struct got_object_id *yca_id;
686 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
687 commit_id, base_commit_id, repo);
692 return got_error(GOT_ERR_ANCESTRY);
695 * Require a straight line of history between the target commit
696 * and the work tree's base commit.
698 * Non-linear situations such as this require a rebase:
700 * (commit) D F (base_commit)
708 * 'got update' only handles linear cases:
709 * Update forwards in time: A (base/yca) - B - C - D (commit)
710 * Update backwards in time: D (base) - C - B - A (commit/yca)
712 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
713 got_object_id_cmp(base_commit_id, yca_id) != 0)
714 return got_error(GOT_ERR_ANCESTRY);
720 static const struct got_error *
721 check_same_branch(struct got_object_id *commit_id,
722 struct got_reference *head_ref, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_commit_graph *graph = NULL;
726 struct got_object_id *head_commit_id = NULL;
727 int is_same_branch = 0;
729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
733 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
737 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
742 struct got_object_id *id;
743 err = got_commit_graph_iter_next(&id, graph);
745 if (err->code == GOT_ERR_ITER_COMPLETED) {
749 else if (err->code != GOT_ERR_ITER_NEED_MORE)
751 err = got_commit_graph_fetch_commits(graph, 1,
758 if (got_object_id_cmp(id, commit_id) == 0) {
766 got_commit_graph_close(graph);
767 free(head_commit_id);
768 if (!err && !is_same_branch)
769 err = got_error(GOT_ERR_ANCESTRY);
773 static const struct got_error *
774 cmd_checkout(int argc, char *argv[])
776 const struct got_error *error = NULL;
777 struct got_repository *repo = NULL;
778 struct got_reference *head_ref = NULL;
779 struct got_worktree *worktree = NULL;
780 char *repo_path = NULL;
781 char *worktree_path = NULL;
782 const char *path_prefix = "";
783 const char *branch_name = GOT_REF_HEAD;
784 char *commit_id_str = NULL;
785 int ch, same_path_prefix;
787 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
790 branch_name = optarg;
793 commit_id_str = strdup(optarg);
794 if (commit_id_str == NULL)
795 return got_error_from_errno("strdup");
798 path_prefix = optarg;
810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
811 "unveil", NULL) == -1)
815 char *cwd, *base, *dotgit;
816 repo_path = realpath(argv[0], NULL);
817 if (repo_path == NULL)
818 return got_error_from_errno2("realpath", argv[0]);
819 cwd = getcwd(NULL, 0);
821 error = got_error_from_errno("getcwd");
824 if (path_prefix[0]) {
825 base = basename(path_prefix);
827 error = got_error_from_errno2("basename",
832 base = basename(repo_path);
834 error = got_error_from_errno2("basename",
839 dotgit = strstr(base, ".git");
842 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
843 error = got_error_from_errno("asprintf");
848 } else if (argc == 2) {
849 repo_path = realpath(argv[0], NULL);
850 if (repo_path == NULL) {
851 error = got_error_from_errno2("realpath", argv[0]);
854 worktree_path = realpath(argv[1], NULL);
855 if (worktree_path == NULL) {
856 error = got_error_from_errno2("realpath", argv[1]);
862 got_path_strip_trailing_slashes(repo_path);
863 got_path_strip_trailing_slashes(worktree_path);
865 error = got_repo_open(&repo, repo_path);
869 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
873 error = got_ref_open(&head_ref, repo, branch_name, 0);
877 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
878 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
881 error = got_worktree_open(&worktree, worktree_path);
885 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
889 if (!same_path_prefix) {
890 error = got_error(GOT_ERR_PATH_PREFIX);
895 struct got_object_id *commit_id;
896 error = got_repo_match_object_id_prefix(&commit_id,
897 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
900 error = check_linear_ancestry(commit_id,
901 got_worktree_get_base_commit_id(worktree), repo);
906 error = check_same_branch(commit_id, head_ref, repo);
909 error = got_worktree_set_base_commit_id(worktree, repo,
916 error = got_worktree_checkout_files(worktree, "", repo,
917 checkout_progress, worktree_path, check_cancelled, NULL);
921 printf("Now shut up and hack\n");
933 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
938 static const struct got_error *
939 update_progress(void *arg, unsigned char status, const char *path)
941 int *did_something = arg;
943 if (status == GOT_STATUS_EXISTS)
948 /* Base commit bump happens silently. */
949 if (status == GOT_STATUS_BUMP_BASE)
952 while (path[0] == '/')
954 printf("%c %s\n", status, path);
958 static const struct got_error *
959 switch_head_ref(struct got_reference *head_ref,
960 struct got_object_id *commit_id, struct got_worktree *worktree,
961 struct got_repository *repo)
963 const struct got_error *err = NULL;
965 int ref_has_moved = 0;
967 /* Trivial case: switching between two different references. */
968 if (strcmp(got_ref_get_name(head_ref),
969 got_worktree_get_head_ref_name(worktree)) != 0) {
970 printf("Switching work tree from %s to %s\n",
971 got_worktree_get_head_ref_name(worktree),
972 got_ref_get_name(head_ref));
973 return got_worktree_set_head_ref(worktree, head_ref);
976 err = check_linear_ancestry(commit_id,
977 got_worktree_get_base_commit_id(worktree), repo);
979 if (err->code != GOT_ERR_ANCESTRY)
986 /* Switching to a rebased branch with the same reference name. */
987 err = got_object_id_str(&base_id_str,
988 got_worktree_get_base_commit_id(worktree));
991 printf("Reference %s now points at a different branch\n",
992 got_worktree_get_head_ref_name(worktree));
993 printf("Switching work tree from %s to %s\n", base_id_str,
994 got_worktree_get_head_ref_name(worktree));
998 static const struct got_error *
999 cmd_update(int argc, char *argv[])
1001 const struct got_error *error = NULL;
1002 struct got_repository *repo = NULL;
1003 struct got_worktree *worktree = NULL;
1004 char *worktree_path = NULL, *path = NULL;
1005 struct got_object_id *commit_id = NULL;
1006 char *commit_id_str = NULL;
1007 const char *branch_name = NULL;
1008 struct got_reference *head_ref = NULL;
1009 int ch, did_something = 0, rebase_in_progress;
1011 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1014 branch_name = optarg;
1017 commit_id_str = strdup(optarg);
1018 if (commit_id_str == NULL)
1019 return got_error_from_errno("strdup");
1031 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1032 "unveil", NULL) == -1)
1035 worktree_path = getcwd(NULL, 0);
1036 if (worktree_path == NULL) {
1037 error = got_error_from_errno("getcwd");
1040 error = got_worktree_open(&worktree, worktree_path);
1044 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1047 if (rebase_in_progress) {
1048 error = got_error(GOT_ERR_REBASING);
1055 error = got_error_from_errno("strdup");
1058 } else if (argc == 1) {
1059 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1069 error = apply_unveil(got_repo_get_path(repo), 0,
1070 got_worktree_get_root_path(worktree), 0);
1074 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1075 got_worktree_get_head_ref_name(worktree), 0);
1078 if (commit_id_str == NULL) {
1079 error = got_ref_resolve(&commit_id, repo, head_ref);
1082 error = got_object_id_str(&commit_id_str, commit_id);
1086 error = got_repo_match_object_id_prefix(&commit_id,
1087 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1090 free(commit_id_str);
1091 error = got_object_id_str(&commit_id_str, commit_id);
1097 struct got_object_id *head_commit_id;
1098 if (strlen(path) != 0) {
1099 fprintf(stderr, "%s: switching between branches "
1100 "requires that the entire work tree "
1101 "gets updated, not just '%s'\n",
1102 getprogname(), path);
1103 error = got_error(GOT_ERR_BAD_PATH);
1106 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1109 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1110 free(head_commit_id);
1113 error = check_same_branch(commit_id, head_ref, repo);
1116 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1120 error = check_linear_ancestry(commit_id,
1121 got_worktree_get_base_commit_id(worktree), repo);
1122 if (error != NULL) {
1123 if (error->code == GOT_ERR_ANCESTRY)
1124 error = got_error(GOT_ERR_BRANCH_MOVED);
1127 error = check_same_branch(commit_id, head_ref, repo);
1132 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1134 error = got_worktree_set_base_commit_id(worktree, repo,
1140 error = got_worktree_checkout_files(worktree, path, repo,
1141 update_progress, &did_something, check_cancelled, NULL);
1146 printf("Updated to commit %s\n", commit_id_str);
1148 printf("Already up-to-date\n");
1150 free(worktree_path);
1153 free(commit_id_str);
1157 static const struct got_error *
1158 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1159 int diff_context, struct got_repository *repo)
1161 const struct got_error *err = NULL;
1162 struct got_tree_object *tree1 = NULL, *tree2;
1163 struct got_object_qid *qid;
1164 char *id_str1 = NULL, *id_str2;
1165 struct got_diff_blob_output_unidiff_arg arg;
1167 err = got_object_open_as_tree(&tree2, repo,
1168 got_object_commit_get_tree_id(commit));
1172 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1174 struct got_commit_object *pcommit;
1176 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1180 err = got_object_open_as_tree(&tree1, repo,
1181 got_object_commit_get_tree_id(pcommit));
1182 got_object_commit_close(pcommit);
1186 err = got_object_id_str(&id_str1, qid->id);
1191 err = got_object_id_str(&id_str2, id);
1195 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1196 arg.diff_context = diff_context;
1197 arg.outfile = stdout;
1198 err = got_diff_tree(tree1, tree2, "", "", repo,
1199 got_diff_blob_output_unidiff, &arg);
1202 got_object_tree_close(tree1);
1203 got_object_tree_close(tree2);
1210 get_datestr(time_t *time, char *datebuf)
1212 char *p, *s = ctime_r(time, datebuf);
1213 p = strchr(s, '\n');
1219 static const struct got_error *
1220 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1221 struct got_repository *repo, int show_patch, int diff_context,
1222 struct got_reflist_head *refs)
1224 const struct got_error *err = NULL;
1225 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1227 time_t committer_time;
1228 const char *author, *committer;
1229 char *refs_str = NULL;
1230 struct got_reflist_entry *re;
1232 SIMPLEQ_FOREACH(re, refs, entry) {
1235 if (got_object_id_cmp(re->id, id) != 0)
1237 name = got_ref_get_name(re->ref);
1238 if (strcmp(name, GOT_REF_HEAD) == 0)
1240 if (strncmp(name, "refs/", 5) == 0)
1242 if (strncmp(name, "got/", 4) == 0)
1244 if (strncmp(name, "heads/", 6) == 0)
1246 if (strncmp(name, "remotes/", 8) == 0)
1249 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1251 err = got_error_from_errno("asprintf");
1257 err = got_object_id_str(&id_str, id);
1261 printf("-----------------------------------------------\n");
1262 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1263 refs_str ? refs_str : "", refs_str ? ")" : "");
1268 printf("from: %s\n", got_object_commit_get_author(commit));
1269 committer_time = got_object_commit_get_committer_time(commit);
1270 datestr = get_datestr(&committer_time, datebuf);
1271 printf("date: %s UTC\n", datestr);
1272 author = got_object_commit_get_author(commit);
1273 committer = got_object_commit_get_committer(commit);
1274 if (strcmp(author, committer) != 0)
1275 printf("via: %s\n", committer);
1276 if (got_object_commit_get_nparents(commit) > 1) {
1277 const struct got_object_id_queue *parent_ids;
1278 struct got_object_qid *qid;
1280 parent_ids = got_object_commit_get_parent_ids(commit);
1281 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1282 err = got_object_id_str(&id_str, qid->id);
1285 printf("parent %d: %s\n", n++, id_str);
1290 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1291 if (logmsg0 == NULL)
1292 return got_error_from_errno("strdup");
1296 line = strsep(&logmsg, "\n");
1298 printf(" %s\n", line);
1303 err = print_patch(commit, id, diff_context, repo);
1308 if (fflush(stdout) != 0 && err == NULL)
1309 err = got_error_from_errno("fflush");
1313 static const struct got_error *
1314 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1315 char *path, int show_patch, int diff_context, int limit,
1316 int first_parent_traversal, struct got_reflist_head *refs)
1318 const struct got_error *err;
1319 struct got_commit_graph *graph;
1321 err = got_commit_graph_open(&graph, root_id, path,
1322 first_parent_traversal, repo);
1325 err = got_commit_graph_iter_start(graph, root_id, repo);
1329 struct got_commit_object *commit;
1330 struct got_object_id *id;
1332 if (sigint_received || sigpipe_received)
1335 err = got_commit_graph_iter_next(&id, graph);
1337 if (err->code == GOT_ERR_ITER_COMPLETED) {
1341 if (err->code != GOT_ERR_ITER_NEED_MORE)
1343 err = got_commit_graph_fetch_commits(graph, 1, repo);
1352 err = got_object_open_as_commit(&commit, repo, id);
1355 err = print_commit(commit, id, repo, show_patch, diff_context,
1357 got_object_commit_close(commit);
1358 if (err || (limit && --limit == 0))
1362 got_commit_graph_close(graph);
1369 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1370 "[-r repository-path] [path]\n", getprogname());
1374 static const struct got_error *
1375 cmd_log(int argc, char *argv[])
1377 const struct got_error *error;
1378 struct got_repository *repo = NULL;
1379 struct got_worktree *worktree = NULL;
1380 struct got_commit_object *commit = NULL;
1381 struct got_object_id *id = NULL;
1382 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1383 char *start_commit = NULL;
1384 int diff_context = 3, ch;
1385 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1387 struct got_reflist_head refs;
1389 SIMPLEQ_INIT(&refs);
1392 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1398 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1404 start_commit = optarg;
1407 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1410 err(1, "-C option %s", errstr);
1413 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1415 err(1, "-l option %s", errstr);
1418 first_parent_traversal = 1;
1421 repo_path = realpath(optarg, NULL);
1422 if (repo_path == NULL)
1423 err(1, "-r option");
1424 got_path_strip_trailing_slashes(repo_path);
1435 cwd = getcwd(NULL, 0);
1437 error = got_error_from_errno("getcwd");
1441 error = got_worktree_open(&worktree, cwd);
1442 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1449 error = got_error_from_errno("strdup");
1452 } else if (argc == 1) {
1454 error = got_worktree_resolve_path(&path, worktree,
1459 path = strdup(argv[0]);
1461 error = got_error_from_errno("strdup");
1468 if (repo_path == NULL) {
1469 repo_path = worktree ?
1470 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1472 if (repo_path == NULL) {
1473 error = got_error_from_errno("strdup");
1477 error = got_repo_open(&repo, repo_path);
1481 error = apply_unveil(got_repo_get_path(repo), 1,
1482 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1486 if (start_commit == NULL) {
1487 struct got_reference *head_ref;
1488 error = got_ref_open(&head_ref, repo,
1489 worktree ? got_worktree_get_head_ref_name(worktree)
1493 error = got_ref_resolve(&id, repo, head_ref);
1494 got_ref_close(head_ref);
1497 error = got_object_open_as_commit(&commit, repo, id);
1499 struct got_reference *ref;
1500 error = got_ref_open(&ref, repo, start_commit, 0);
1501 if (error == NULL) {
1503 error = got_ref_resolve(&id, repo, ref);
1507 error = got_object_get_type(&obj_type, repo, id);
1510 if (obj_type == GOT_OBJ_TYPE_TAG) {
1511 struct got_tag_object *tag;
1512 error = got_object_open_as_tag(&tag, repo, id);
1515 if (got_object_tag_get_object_type(tag) !=
1516 GOT_OBJ_TYPE_COMMIT) {
1517 got_object_tag_close(tag);
1518 error = got_error(GOT_ERR_OBJ_TYPE);
1522 id = got_object_id_dup(
1523 got_object_tag_get_object_id(tag));
1525 error = got_error_from_errno(
1526 "got_object_id_dup");
1527 got_object_tag_close(tag);
1530 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1531 error = got_error(GOT_ERR_OBJ_TYPE);
1534 error = got_object_open_as_commit(&commit, repo, id);
1538 if (commit == NULL) {
1539 error = got_repo_match_object_id_prefix(&id,
1540 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1548 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1553 path = in_repo_path;
1556 error = got_ref_list(&refs, repo);
1560 error = print_commits(id, repo, path, show_patch,
1561 diff_context, limit, first_parent_traversal, &refs);
1568 got_worktree_close(worktree);
1570 const struct got_error *repo_error;
1571 repo_error = got_repo_close(repo);
1575 got_ref_list_free(&refs);
1582 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1583 "[object1 object2 | path]\n", getprogname());
1587 struct print_diff_arg {
1588 struct got_repository *repo;
1589 struct got_worktree *worktree;
1595 static const struct got_error *
1596 print_diff(void *arg, unsigned char status, const char *path,
1597 struct got_object_id *blob_id, struct got_object_id *commit_id)
1599 struct print_diff_arg *a = arg;
1600 const struct got_error *err = NULL;
1601 struct got_blob_object *blob1 = NULL;
1603 char *abspath = NULL;
1606 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1607 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1610 if (!a->header_shown) {
1611 printf("diff %s %s\n", a->id_str,
1612 got_worktree_get_root_path(a->worktree));
1613 a->header_shown = 1;
1616 if (status != GOT_STATUS_ADD) {
1617 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1623 if (status != GOT_STATUS_DELETE) {
1624 if (asprintf(&abspath, "%s/%s",
1625 got_worktree_get_root_path(a->worktree), path) == -1) {
1626 err = got_error_from_errno("asprintf");
1630 f2 = fopen(abspath, "r");
1632 err = got_error_from_errno2("fopen", abspath);
1635 if (lstat(abspath, &sb) == -1) {
1636 err = got_error_from_errno2("lstat", abspath);
1642 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1646 got_object_blob_close(blob1);
1647 if (f2 && fclose(f2) != 0 && err == NULL)
1648 err = got_error_from_errno("fclose");
1653 static const struct got_error *
1654 cmd_diff(int argc, char *argv[])
1656 const struct got_error *error;
1657 struct got_repository *repo = NULL;
1658 struct got_worktree *worktree = NULL;
1659 char *cwd = NULL, *repo_path = NULL;
1660 struct got_object_id *id1 = NULL, *id2 = NULL;
1661 const char *id_str1 = NULL, *id_str2 = NULL;
1662 char *label1 = NULL, *label2 = NULL;
1664 int diff_context = 3, ch;
1669 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1674 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1677 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1679 err(1, "-C option %s", errstr);
1682 repo_path = realpath(optarg, NULL);
1683 if (repo_path == NULL)
1684 err(1, "-r option");
1685 got_path_strip_trailing_slashes(repo_path);
1696 cwd = getcwd(NULL, 0);
1698 error = got_error_from_errno("getcwd");
1701 error = got_worktree_open(&worktree, cwd);
1702 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1705 if (worktree == NULL) {
1706 error = got_error(GOT_ERR_NOT_WORKTREE);
1711 "-r option can't be used when diffing a work tree");
1712 repo_path = strdup(got_worktree_get_repo_path(worktree));
1713 if (repo_path == NULL) {
1714 error = got_error_from_errno("strdup");
1718 error = got_worktree_resolve_path(&path, worktree,
1725 error = got_error_from_errno("strdup");
1729 } else if (argc == 2) {
1732 if (worktree && repo_path == NULL) {
1734 strdup(got_worktree_get_repo_path(worktree));
1735 if (repo_path == NULL) {
1736 error = got_error_from_errno("strdup");
1743 if (repo_path == NULL) {
1744 repo_path = getcwd(NULL, 0);
1745 if (repo_path == NULL)
1746 return got_error_from_errno("getcwd");
1749 error = got_repo_open(&repo, repo_path);
1754 error = apply_unveil(got_repo_get_path(repo), 1,
1755 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1760 struct print_diff_arg arg;
1762 error = got_object_id_str(&id_str,
1763 got_worktree_get_base_commit_id(worktree));
1767 arg.worktree = worktree;
1768 arg.diff_context = diff_context;
1769 arg.id_str = id_str;
1770 arg.header_shown = 0;
1772 error = got_worktree_status(worktree, path, repo, print_diff,
1773 &arg, check_cancelled, NULL);
1778 error = got_repo_match_object_id_prefix(&id1, id_str1,
1779 GOT_OBJ_TYPE_ANY, repo);
1781 struct got_reference *ref;
1782 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1784 error = got_ref_open(&ref, repo, id_str1, 0);
1787 label1 = strdup(got_ref_get_name(ref));
1788 if (label1 == NULL) {
1789 error = got_error_from_errno("strdup");
1792 error = got_ref_resolve(&id1, repo, ref);
1797 error = got_object_id_str(&label1, id1);
1798 if (label1 == NULL) {
1799 error = got_error_from_errno("strdup");
1804 error = got_repo_match_object_id_prefix(&id2, id_str2,
1805 GOT_OBJ_TYPE_ANY, repo);
1807 struct got_reference *ref;
1808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1810 error = got_ref_open(&ref, repo, id_str2, 0);
1813 label2 = strdup(got_ref_get_name(ref));
1814 if (label2 == NULL) {
1815 error = got_error_from_errno("strdup");
1818 error = got_ref_resolve(&id2, repo, ref);
1823 error = got_object_id_str(&label2, id2);
1824 if (label2 == NULL) {
1825 error = got_error_from_errno("strdup");
1830 error = got_object_get_type(&type1, repo, id1);
1834 error = got_object_get_type(&type2, repo, id2);
1838 if (type1 != type2) {
1839 error = got_error(GOT_ERR_OBJ_TYPE);
1844 case GOT_OBJ_TYPE_BLOB:
1845 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1846 diff_context, repo, stdout);
1848 case GOT_OBJ_TYPE_TREE:
1849 error = got_diff_objects_as_trees(id1, id2, "", "",
1850 diff_context, repo, stdout);
1852 case GOT_OBJ_TYPE_COMMIT:
1853 printf("diff %s %s\n", label1, label2);
1854 error = got_diff_objects_as_commits(id1, id2, diff_context,
1858 error = got_error(GOT_ERR_OBJ_TYPE);
1868 got_worktree_close(worktree);
1870 const struct got_error *repo_error;
1871 repo_error = got_repo_close(repo);
1882 "usage: %s blame [-c commit] [-r repository-path] path\n",
1887 static const struct got_error *
1888 cmd_blame(int argc, char *argv[])
1890 const struct got_error *error;
1891 struct got_repository *repo = NULL;
1892 struct got_worktree *worktree = NULL;
1893 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1894 struct got_object_id *commit_id = NULL;
1895 char *commit_id_str = NULL;
1899 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1904 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1907 commit_id_str = optarg;
1910 repo_path = realpath(optarg, NULL);
1911 if (repo_path == NULL)
1912 err(1, "-r option");
1913 got_path_strip_trailing_slashes(repo_path);
1929 cwd = getcwd(NULL, 0);
1931 error = got_error_from_errno("getcwd");
1934 if (repo_path == NULL) {
1935 error = got_worktree_open(&worktree, cwd);
1936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1942 strdup(got_worktree_get_repo_path(worktree));
1943 if (repo_path == NULL)
1944 error = got_error_from_errno("strdup");
1948 repo_path = strdup(cwd);
1949 if (repo_path == NULL) {
1950 error = got_error_from_errno("strdup");
1956 error = got_repo_open(&repo, repo_path);
1960 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1965 const char *prefix = got_worktree_get_path_prefix(worktree);
1966 char *p, *worktree_subdir = cwd +
1967 strlen(got_worktree_get_root_path(worktree));
1968 if (asprintf(&p, "%s%s%s%s%s",
1969 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1970 worktree_subdir, worktree_subdir[0] ? "/" : "",
1972 error = got_error_from_errno("asprintf");
1975 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1978 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1983 if (commit_id_str == NULL) {
1984 struct got_reference *head_ref;
1985 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1988 error = got_ref_resolve(&commit_id, repo, head_ref);
1989 got_ref_close(head_ref);
1993 error = got_repo_match_object_id_prefix(&commit_id,
1994 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1999 error = got_blame(in_repo_path, commit_id, repo, stdout);
2006 got_worktree_close(worktree);
2008 const struct got_error *repo_error;
2009 repo_error = got_repo_close(repo);
2020 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2026 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2027 const char *root_path)
2029 int is_root_path = (strcmp(path, root_path) == 0);
2031 path += strlen(root_path);
2032 while (path[0] == '/')
2035 printf("%s%s%s%s%s\n", id ? id : "", path,
2036 is_root_path ? "" : "/", te->name,
2037 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2040 static const struct got_error *
2041 print_tree(const char *path, struct got_object_id *commit_id,
2042 int show_ids, int recurse, const char *root_path,
2043 struct got_repository *repo)
2045 const struct got_error *err = NULL;
2046 struct got_object_id *tree_id = NULL;
2047 struct got_tree_object *tree = NULL;
2048 const struct got_tree_entries *entries;
2049 struct got_tree_entry *te;
2051 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2055 err = got_object_open_as_tree(&tree, repo, tree_id);
2058 entries = got_object_tree_get_entries(tree);
2059 te = SIMPLEQ_FIRST(&entries->head);
2063 if (sigint_received || sigpipe_received)
2068 err = got_object_id_str(&id_str, te->id);
2071 if (asprintf(&id, "%s ", id_str) == -1) {
2072 err = got_error_from_errno("asprintf");
2078 print_entry(te, id, path, root_path);
2081 if (recurse && S_ISDIR(te->mode)) {
2083 if (asprintf(&child_path, "%s%s%s", path,
2084 path[0] == '/' && path[1] == '\0' ? "" : "/",
2086 err = got_error_from_errno("asprintf");
2089 err = print_tree(child_path, commit_id, show_ids, 1,
2096 te = SIMPLEQ_NEXT(te, entry);
2100 got_object_tree_close(tree);
2105 static const struct got_error *
2106 cmd_tree(int argc, char *argv[])
2108 const struct got_error *error;
2109 struct got_repository *repo = NULL;
2110 struct got_worktree *worktree = NULL;
2112 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2113 struct got_object_id *commit_id = NULL;
2114 char *commit_id_str = NULL;
2115 int show_ids = 0, recurse = 0;
2119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2124 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2127 commit_id_str = optarg;
2130 repo_path = realpath(optarg, NULL);
2131 if (repo_path == NULL)
2132 err(1, "-r option");
2133 got_path_strip_trailing_slashes(repo_path);
2157 cwd = getcwd(NULL, 0);
2159 error = got_error_from_errno("getcwd");
2162 if (repo_path == NULL) {
2163 error = got_worktree_open(&worktree, cwd);
2164 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2170 strdup(got_worktree_get_repo_path(worktree));
2171 if (repo_path == NULL)
2172 error = got_error_from_errno("strdup");
2176 repo_path = strdup(cwd);
2177 if (repo_path == NULL) {
2178 error = got_error_from_errno("strdup");
2184 error = got_repo_open(&repo, repo_path);
2188 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2194 char *p, *worktree_subdir = cwd +
2195 strlen(got_worktree_get_root_path(worktree));
2196 if (asprintf(&p, "%s/%s",
2197 got_worktree_get_path_prefix(worktree),
2198 worktree_subdir) == -1) {
2199 error = got_error_from_errno("asprintf");
2202 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2209 if (in_repo_path == NULL) {
2210 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2215 if (commit_id_str == NULL) {
2216 struct got_reference *head_ref;
2217 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2220 error = got_ref_resolve(&commit_id, repo, head_ref);
2221 got_ref_close(head_ref);
2225 error = got_repo_match_object_id_prefix(&commit_id,
2226 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2231 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2232 in_repo_path, repo);
2239 got_worktree_close(worktree);
2241 const struct got_error *repo_error;
2242 repo_error = got_repo_close(repo);
2252 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2256 static const struct got_error *
2257 print_status(void *arg, unsigned char status, const char *path,
2258 struct got_object_id *blob_id, struct got_object_id *commit_id)
2260 printf("%c %s\n", status, path);
2264 static const struct got_error *
2265 cmd_status(int argc, char *argv[])
2267 const struct got_error *error = NULL;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2270 char *cwd = NULL, *path = NULL;
2273 while ((ch = getopt(argc, argv, "")) != -1) {
2285 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2289 cwd = getcwd(NULL, 0);
2291 error = got_error_from_errno("getcwd");
2295 error = got_worktree_open(&worktree, cwd);
2302 error = got_error_from_errno("strdup");
2305 } else if (argc == 1) {
2306 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2312 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2316 error = apply_unveil(got_repo_get_path(repo), 1,
2317 got_worktree_get_root_path(worktree), 0);
2321 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2322 check_cancelled, NULL);
2333 "usage: %s ref [-r repository] -l | -d name | name target\n",
2338 static const struct got_error *
2339 list_refs(struct got_repository *repo)
2341 static const struct got_error *err = NULL;
2342 struct got_reflist_head refs;
2343 struct got_reflist_entry *re;
2345 SIMPLEQ_INIT(&refs);
2346 err = got_ref_list(&refs, repo);
2350 SIMPLEQ_FOREACH(re, &refs, entry) {
2352 refstr = got_ref_to_str(re->ref);
2354 return got_error_from_errno("got_ref_to_str");
2355 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2359 got_ref_list_free(&refs);
2363 static const struct got_error *
2364 delete_ref(struct got_repository *repo, const char *refname)
2366 const struct got_error *err = NULL;
2367 struct got_reference *ref;
2369 err = got_ref_open(&ref, repo, refname, 0);
2373 err = got_ref_delete(ref, repo);
2378 static const struct got_error *
2379 add_ref(struct got_repository *repo, const char *refname, const char *target)
2381 const struct got_error *err = NULL;
2382 struct got_object_id *id;
2383 struct got_reference *ref = NULL;
2386 * Don't let the user create a reference named '-'.
2387 * While technically a valid reference name, this case is usually
2388 * an unintended typo.
2390 if (refname[0] == '-' && refname[1] == '\0')
2391 return got_error(GOT_ERR_BAD_REF_NAME);
2393 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2396 struct got_reference *target_ref;
2398 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2400 err = got_ref_open(&target_ref, repo, target, 0);
2403 err = got_ref_resolve(&id, repo, target_ref);
2404 got_ref_close(target_ref);
2409 err = got_ref_alloc(&ref, refname, id);
2413 err = got_ref_write(ref, repo);
2421 static const struct got_error *
2422 cmd_ref(int argc, char *argv[])
2424 const struct got_error *error = NULL;
2425 struct got_repository *repo = NULL;
2426 struct got_worktree *worktree = NULL;
2427 char *cwd = NULL, *repo_path = NULL;
2428 int ch, do_list = 0;
2429 const char *delref = NULL;
2431 /* TODO: Add -s option for adding symbolic references. */
2432 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2438 repo_path = realpath(optarg, NULL);
2439 if (repo_path == NULL)
2440 err(1, "-r option");
2441 got_path_strip_trailing_slashes(repo_path);
2452 if (do_list && delref)
2453 errx(1, "-l and -d options are mutually exclusive\n");
2458 if (do_list || delref) {
2461 } else if (argc != 2)
2466 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2470 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2471 "sendfd unveil", NULL) == -1)
2475 cwd = getcwd(NULL, 0);
2477 error = got_error_from_errno("getcwd");
2481 if (repo_path == NULL) {
2482 error = got_worktree_open(&worktree, cwd);
2483 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2489 strdup(got_worktree_get_repo_path(worktree));
2490 if (repo_path == NULL)
2491 error = got_error_from_errno("strdup");
2495 repo_path = strdup(cwd);
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2503 error = got_repo_open(&repo, repo_path);
2507 error = apply_unveil(got_repo_get_path(repo), do_list,
2508 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2513 error = list_refs(repo);
2515 error = delete_ref(repo, delref);
2517 error = add_ref(repo, argv[0], argv[1]);
2520 got_repo_close(repo);
2522 got_worktree_close(worktree);
2532 "usage: %s branch [-r repository] -l | -d name | "
2533 "name [base-branch]\n", getprogname());
2537 static const struct got_error *
2538 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2540 static const struct got_error *err = NULL;
2541 struct got_reflist_head refs;
2542 struct got_reflist_entry *re;
2544 SIMPLEQ_INIT(&refs);
2546 err = got_ref_list(&refs, repo);
2550 SIMPLEQ_FOREACH(re, &refs, entry) {
2551 const char *refname, *marker = " ";
2553 refname = got_ref_get_name(re->ref);
2554 if (strncmp(refname, "refs/heads/", 11) != 0)
2556 if (worktree && strcmp(refname,
2557 got_worktree_get_head_ref_name(worktree)) == 0) {
2558 struct got_object_id *id = NULL;
2559 err = got_ref_resolve(&id, repo, re->ref);
2562 if (got_object_id_cmp(id,
2563 got_worktree_get_base_commit_id(worktree)) == 0)
2570 refstr = got_ref_to_str(re->ref);
2572 return got_error_from_errno("got_ref_to_str");
2573 printf("%s%s: %s\n", marker, refname, refstr);
2577 got_ref_list_free(&refs);
2581 static const struct got_error *
2582 delete_branch(struct got_repository *repo, const char *branch_name)
2584 const struct got_error *err = NULL;
2585 struct got_reference *ref;
2588 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2589 return got_error_from_errno("asprintf");
2591 err = got_ref_open(&ref, repo, refname, 0);
2595 err = got_ref_delete(ref, repo);
2602 static const struct got_error *
2603 add_branch(struct got_repository *repo, const char *branch_name,
2604 const char *base_branch)
2606 const struct got_error *err = NULL;
2607 struct got_object_id *id = NULL;
2608 struct got_reference *ref = NULL;
2609 char *base_refname = NULL, *refname = NULL;
2610 struct got_reference *base_ref;
2613 * Don't let the user create a branch named '-'.
2614 * While technically a valid reference name, this case is usually
2615 * an unintended typo.
2617 if (branch_name[0] == '-' && branch_name[1] == '\0')
2618 return got_error(GOT_ERR_BAD_REF_NAME);
2620 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2621 base_refname = strdup(GOT_REF_HEAD);
2622 if (base_refname == NULL)
2623 return got_error_from_errno("strdup");
2624 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2625 return got_error_from_errno("asprintf");
2627 err = got_ref_open(&base_ref, repo, base_refname, 0);
2630 err = got_ref_resolve(&id, repo, base_ref);
2631 got_ref_close(base_ref);
2635 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2636 err = got_error_from_errno("asprintf");
2640 err = got_ref_open(&ref, repo, refname, 0);
2642 err = got_error(GOT_ERR_BRANCH_EXISTS);
2644 } else if (err->code != GOT_ERR_NOT_REF)
2647 err = got_ref_alloc(&ref, refname, id);
2651 err = got_ref_write(ref, repo);
2661 static const struct got_error *
2662 cmd_branch(int argc, char *argv[])
2664 const struct got_error *error = NULL;
2665 struct got_repository *repo = NULL;
2666 struct got_worktree *worktree = NULL;
2667 char *cwd = NULL, *repo_path = NULL;
2668 int ch, do_list = 0;
2669 const char *delref = NULL;
2671 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2677 repo_path = realpath(optarg, NULL);
2678 if (repo_path == NULL)
2679 err(1, "-r option");
2680 got_path_strip_trailing_slashes(repo_path);
2691 if (do_list && delref)
2692 errx(1, "-l and -d options are mutually exclusive\n");
2697 if (do_list || delref) {
2700 } else if (argc < 1 || argc > 2)
2705 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2709 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2710 "sendfd unveil", NULL) == -1)
2714 cwd = getcwd(NULL, 0);
2716 error = got_error_from_errno("getcwd");
2720 if (repo_path == NULL) {
2721 error = got_worktree_open(&worktree, cwd);
2722 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2728 strdup(got_worktree_get_repo_path(worktree));
2729 if (repo_path == NULL)
2730 error = got_error_from_errno("strdup");
2734 repo_path = strdup(cwd);
2735 if (repo_path == NULL) {
2736 error = got_error_from_errno("strdup");
2742 error = got_repo_open(&repo, repo_path);
2746 error = apply_unveil(got_repo_get_path(repo), do_list,
2747 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2752 error = list_branches(repo, worktree);
2754 error = delete_branch(repo, delref);
2756 const char *base_branch;
2758 base_branch = worktree ?
2759 got_worktree_get_head_ref_name(worktree) :
2762 base_branch = argv[1];
2763 error = add_branch(repo, argv[0], base_branch);
2767 got_repo_close(repo);
2769 got_worktree_close(worktree);
2778 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2782 static const struct got_error *
2783 cmd_add(int argc, char *argv[])
2785 const struct got_error *error = NULL;
2786 struct got_repository *repo = NULL;
2787 struct got_worktree *worktree = NULL;
2789 struct got_pathlist_head paths;
2790 struct got_pathlist_entry *pe;
2795 while ((ch = getopt(argc, argv, "")) != -1) {
2807 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2814 /* make sure each file exists before doing anything halfway */
2815 for (x = 0; x < argc; x++) {
2816 char *path = realpath(argv[x], NULL);
2818 error = got_error_from_errno2("realpath", argv[x]);
2824 cwd = getcwd(NULL, 0);
2826 error = got_error_from_errno("getcwd");
2830 error = got_worktree_open(&worktree, cwd);
2834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2838 error = apply_unveil(got_repo_get_path(repo), 1,
2839 got_worktree_get_root_path(worktree), 0);
2843 for (x = 0; x < argc; x++) {
2844 char *path = realpath(argv[x], NULL);
2846 error = got_error_from_errno2("realpath", argv[x]);
2850 got_path_strip_trailing_slashes(path);
2851 error = got_pathlist_insert(&pe, &paths, path, NULL);
2857 error = got_worktree_schedule_add(worktree, &paths, print_status,
2861 got_repo_close(repo);
2863 got_worktree_close(worktree);
2864 TAILQ_FOREACH(pe, &paths, entry)
2865 free((char *)pe->path);
2866 got_pathlist_free(&paths);
2874 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2878 static const struct got_error *
2879 cmd_remove(int argc, char *argv[])
2881 const struct got_error *error = NULL;
2882 struct got_worktree *worktree = NULL;
2883 struct got_repository *repo = NULL;
2885 struct got_pathlist_head paths;
2886 struct got_pathlist_entry *pe;
2887 int ch, i, delete_local_mods = 0;
2891 while ((ch = getopt(argc, argv, "f")) != -1) {
2894 delete_local_mods = 1;
2906 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2913 /* make sure each file exists before doing anything halfway */
2914 for (i = 0; i < argc; i++) {
2915 char *path = realpath(argv[i], NULL);
2917 error = got_error_from_errno2("realpath", argv[i]);
2923 cwd = getcwd(NULL, 0);
2925 error = got_error_from_errno("getcwd");
2928 error = got_worktree_open(&worktree, cwd);
2932 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2936 error = apply_unveil(got_repo_get_path(repo), 1,
2937 got_worktree_get_root_path(worktree), 0);
2941 for (i = 0; i < argc; i++) {
2942 char *path = realpath(argv[i], NULL);
2944 error = got_error_from_errno2("realpath", argv[i]);
2948 got_path_strip_trailing_slashes(path);
2949 error = got_pathlist_insert(&pe, &paths, path, NULL);
2955 error = got_worktree_schedule_delete(worktree, &paths,
2956 delete_local_mods, print_status, NULL, repo);
2961 got_repo_close(repo);
2963 got_worktree_close(worktree);
2964 TAILQ_FOREACH(pe, &paths, entry)
2965 free((char *)pe->path);
2966 got_pathlist_free(&paths);
2974 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2978 static const struct got_error *
2979 revert_progress(void *arg, unsigned char status, const char *path)
2981 while (path[0] == '/')
2983 printf("%c %s\n", status, path);
2987 static const struct got_error *
2988 cmd_revert(int argc, char *argv[])
2990 const struct got_error *error = NULL;
2991 struct got_worktree *worktree = NULL;
2992 struct got_repository *repo = NULL;
2993 char *cwd = NULL, *path = NULL;
2994 struct got_pathlist_head paths;
2995 struct got_pathlist_entry *pe;
3000 while ((ch = getopt(argc, argv, "")) != -1) {
3012 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3013 "unveil", NULL) == -1)
3019 for (i = 0; i < argc; i++) {
3020 char *path = realpath(argv[i], NULL);
3022 error = got_error_from_errno2("realpath", argv[i]);
3026 got_path_strip_trailing_slashes(path);
3027 error = got_pathlist_insert(&pe, &paths, path, NULL);
3034 cwd = getcwd(NULL, 0);
3036 error = got_error_from_errno("getcwd");
3039 error = got_worktree_open(&worktree, cwd);
3043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3047 error = apply_unveil(got_repo_get_path(repo), 1,
3048 got_worktree_get_root_path(worktree), 0);
3052 error = got_worktree_revert(worktree, &paths,
3053 revert_progress, NULL, repo);
3058 got_repo_close(repo);
3060 got_worktree_close(worktree);
3069 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3073 struct collect_commit_logmsg_arg {
3074 const char *cmdline_log;
3076 const char *worktree_path;
3077 const char *branch_name;
3078 const char *repo_path;
3083 static const struct got_error *
3084 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3087 char *initial_content = NULL;
3088 struct got_pathlist_entry *pe;
3089 const struct got_error *err = NULL;
3090 char *template = NULL;
3091 struct collect_commit_logmsg_arg *a = arg;
3095 /* if a message was specified on the command line, just use it */
3096 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3097 len = strlen(a->cmdline_log) + 1;
3098 *logmsg = malloc(len + 1);
3099 if (*logmsg == NULL)
3100 return got_error_from_errno("malloc");
3101 strlcpy(*logmsg, a->cmdline_log, len);
3105 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3106 return got_error_from_errno("asprintf");
3108 if (asprintf(&initial_content,
3109 "\n# changes to be committed on branch %s:\n",
3110 a->branch_name) == -1)
3111 return got_error_from_errno("asprintf");
3113 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3117 dprintf(fd, initial_content);
3119 TAILQ_FOREACH(pe, commitable_paths, entry) {
3120 struct got_commitable *ct = pe->data;
3121 dprintf(fd, "# %c %s\n",
3122 got_commitable_get_status(ct),
3123 got_commitable_get_path(ct));
3127 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3129 unlink(a->logmsg_path);
3130 free(a->logmsg_path);
3131 free(initial_content);
3134 /* Editor is done; we can now apply unveil(2) */
3136 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3145 static const struct got_error *
3146 cmd_commit(int argc, char *argv[])
3148 const struct got_error *error = NULL;
3149 struct got_worktree *worktree = NULL;
3150 struct got_repository *repo = NULL;
3151 char *cwd = NULL, *path = NULL, *id_str = NULL;
3152 struct got_object_id *id = NULL;
3153 const char *logmsg = NULL;
3154 const char *got_author = getenv("GOT_AUTHOR");
3155 struct collect_commit_logmsg_arg cl_arg;
3156 char *editor = NULL;
3157 int ch, rebase_in_progress;
3159 while ((ch = getopt(argc, argv, "m:")) != -1) {
3174 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3175 "unveil", NULL) == -1)
3179 path = realpath(argv[0], NULL);
3181 error = got_error_from_errno2("realpath", argv[0]);
3184 got_path_strip_trailing_slashes(path);
3185 } else if (argc != 0)
3188 if (got_author == NULL) {
3189 /* TODO: Look current user up in password database */
3190 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3194 cwd = getcwd(NULL, 0);
3196 error = got_error_from_errno("getcwd");
3199 error = got_worktree_open(&worktree, cwd);
3203 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3206 if (rebase_in_progress) {
3207 error = got_error(GOT_ERR_REBASING);
3211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3216 * unveil(2) traverses exec(2); if an editor is used we have
3217 * to apply unveil after the log message has been written.
3219 if (logmsg == NULL || strlen(logmsg) == 0)
3220 error = get_editor(&editor);
3222 error = apply_unveil(got_repo_get_path(repo), 0,
3223 got_worktree_get_root_path(worktree), 0);
3227 cl_arg.editor = editor;
3228 cl_arg.cmdline_log = logmsg;
3229 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3230 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3231 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3232 cl_arg.branch_name += 5;
3233 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3234 cl_arg.branch_name += 6;
3235 cl_arg.repo_path = got_repo_get_path(repo);
3236 cl_arg.logmsg_path = NULL;
3237 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3238 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3240 if (cl_arg.logmsg_path)
3241 fprintf(stderr, "%s: log message preserved in %s\n",
3242 getprogname(), cl_arg.logmsg_path);
3246 if (cl_arg.logmsg_path)
3247 unlink(cl_arg.logmsg_path);
3249 error = got_object_id_str(&id_str, id);
3252 printf("Created commit %s\n", id_str);
3255 got_repo_close(repo);
3257 got_worktree_close(worktree);
3266 usage_cherrypick(void)
3268 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3272 static const struct got_error *
3273 cmd_cherrypick(int argc, char *argv[])
3275 const struct got_error *error = NULL;
3276 struct got_worktree *worktree = NULL;
3277 struct got_repository *repo = NULL;
3278 char *cwd = NULL, *commit_id_str = NULL;
3279 struct got_object_id *commit_id = NULL;
3280 struct got_commit_object *commit = NULL;
3281 struct got_object_qid *pid;
3282 struct got_reference *head_ref = NULL;
3283 int ch, did_something = 0;
3285 while ((ch = getopt(argc, argv, "")) != -1) {
3297 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3298 "unveil", NULL) == -1)
3304 cwd = getcwd(NULL, 0);
3306 error = got_error_from_errno("getcwd");
3309 error = got_worktree_open(&worktree, cwd);
3313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3317 error = apply_unveil(got_repo_get_path(repo), 0,
3318 got_worktree_get_root_path(worktree), 0);
3322 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3323 GOT_OBJ_TYPE_COMMIT, repo);
3324 if (error != NULL) {
3325 struct got_reference *ref;
3326 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3328 error = got_ref_open(&ref, repo, argv[0], 0);
3331 error = got_ref_resolve(&commit_id, repo, ref);
3336 error = got_object_id_str(&commit_id_str, commit_id);
3340 error = got_ref_open(&head_ref, repo,
3341 got_worktree_get_head_ref_name(worktree), 0);
3345 error = check_same_branch(commit_id, head_ref, repo);
3347 if (error->code != GOT_ERR_ANCESTRY)
3351 error = got_error(GOT_ERR_SAME_BRANCH);
3355 error = got_object_open_as_commit(&commit, repo, commit_id);
3358 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3359 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3360 commit_id, repo, update_progress, &did_something, check_cancelled,
3366 printf("Merged commit %s\n", commit_id_str);
3369 got_object_commit_close(commit);
3370 free(commit_id_str);
3372 got_ref_close(head_ref);
3374 got_worktree_close(worktree);
3376 got_repo_close(repo);
3383 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3387 static const struct got_error *
3388 cmd_backout(int argc, char *argv[])
3390 const struct got_error *error = NULL;
3391 struct got_worktree *worktree = NULL;
3392 struct got_repository *repo = NULL;
3393 char *cwd = NULL, *commit_id_str = NULL;
3394 struct got_object_id *commit_id = NULL;
3395 struct got_commit_object *commit = NULL;
3396 struct got_object_qid *pid;
3397 struct got_reference *head_ref = NULL;
3398 int ch, did_something = 0;
3400 while ((ch = getopt(argc, argv, "")) != -1) {
3412 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3413 "unveil", NULL) == -1)
3419 cwd = getcwd(NULL, 0);
3421 error = got_error_from_errno("getcwd");
3424 error = got_worktree_open(&worktree, cwd);
3428 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3432 error = apply_unveil(got_repo_get_path(repo), 0,
3433 got_worktree_get_root_path(worktree), 0);
3437 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3438 GOT_OBJ_TYPE_COMMIT, repo);
3439 if (error != NULL) {
3440 struct got_reference *ref;
3441 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3443 error = got_ref_open(&ref, repo, argv[0], 0);
3446 error = got_ref_resolve(&commit_id, repo, ref);
3451 error = got_object_id_str(&commit_id_str, commit_id);
3455 error = got_ref_open(&head_ref, repo,
3456 got_worktree_get_head_ref_name(worktree), 0);
3460 error = check_same_branch(commit_id, head_ref, repo);
3464 error = got_object_open_as_commit(&commit, repo, commit_id);
3467 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3469 error = got_error(GOT_ERR_ROOT_COMMIT);
3473 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3474 update_progress, &did_something, check_cancelled, NULL);
3479 printf("Backed out commit %s\n", commit_id_str);
3482 got_object_commit_close(commit);
3483 free(commit_id_str);
3485 got_ref_close(head_ref);
3487 got_worktree_close(worktree);
3489 got_repo_close(repo);
3496 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3501 static const struct got_error *
3502 show_rebase_progress(struct got_commit_object *commit,
3503 struct got_object_id *old_id, struct got_object_id *new_id)
3505 const struct got_error *err;
3506 char *old_id_str = NULL, *new_id_str = NULL;
3507 char *logmsg0 = NULL, *logmsg, *nl;
3510 err = got_object_id_str(&old_id_str, old_id);
3515 err = got_object_id_str(&new_id_str, new_id);
3520 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3521 if (logmsg0 == NULL) {
3522 err = got_error_from_errno("strdup");
3527 while (isspace((unsigned char)logmsg[0]))
3530 old_id_str[12] = '\0';
3532 new_id_str[12] = '\0';
3533 len = strlen(logmsg);
3537 nl = strchr(logmsg, '\n');
3540 printf("%s -> %s: %s\n", old_id_str,
3541 new_id_str ? new_id_str : "no-op change", logmsg);
3549 static const struct got_error *
3550 rebase_progress(void *arg, unsigned char status, const char *path)
3552 unsigned char *rebase_status = arg;
3554 while (path[0] == '/')
3556 printf("%c %s\n", status, path);
3558 if (*rebase_status == GOT_STATUS_CONFLICT)
3560 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3561 *rebase_status = status;
3565 static const struct got_error *
3566 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3567 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3568 struct got_repository *repo)
3570 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3571 return got_worktree_rebase_complete(worktree,
3572 new_base_branch, tmp_branch, branch, repo);
3575 static const struct got_error *
3576 rebase_commit(struct got_pathlist_head *merged_paths,
3577 struct got_worktree *worktree, struct got_reference *tmp_branch,
3578 struct got_object_id *commit_id, struct got_repository *repo)
3580 const struct got_error *error;
3581 struct got_commit_object *commit;
3582 struct got_object_id *new_commit_id;
3584 error = got_object_open_as_commit(&commit, repo, commit_id);
3588 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3589 worktree, tmp_branch, commit, commit_id, repo);
3591 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3593 error = show_rebase_progress(commit, commit_id, NULL);
3595 error = show_rebase_progress(commit, commit_id, new_commit_id);
3596 free(new_commit_id);
3599 got_object_commit_close(commit);
3603 struct check_path_prefix_arg {
3604 const char *path_prefix;
3608 static const struct got_error *
3609 check_path_prefix(void *arg, struct got_blob_object *blob1,
3610 struct got_blob_object *blob2, struct got_object_id *id1,
3611 struct got_object_id *id2, const char *path1, const char *path2,
3612 struct got_repository *repo)
3614 struct check_path_prefix_arg *a = arg;
3616 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3617 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3618 return got_error(GOT_ERR_REBASE_PATH);
3623 static const struct got_error *
3624 rebase_check_path_prefix(struct got_object_id *parent_id,
3625 struct got_object_id *commit_id, const char *path_prefix,
3626 struct got_repository *repo)
3628 const struct got_error *err;
3629 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3630 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3631 struct check_path_prefix_arg cpp_arg;
3633 if (got_path_is_root_dir(path_prefix))
3636 err = got_object_open_as_commit(&commit, repo, commit_id);
3640 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3644 err = got_object_open_as_tree(&tree1, repo,
3645 got_object_commit_get_tree_id(parent_commit));
3649 err = got_object_open_as_tree(&tree2, repo,
3650 got_object_commit_get_tree_id(commit));
3654 cpp_arg.path_prefix = path_prefix;
3655 cpp_arg.len = strlen(path_prefix);
3656 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3660 got_object_tree_close(tree1);
3662 got_object_tree_close(tree2);
3664 got_object_commit_close(commit);
3666 got_object_commit_close(parent_commit);
3670 static const struct got_error *
3671 cmd_rebase(int argc, char *argv[])
3673 const struct got_error *error = NULL;
3674 struct got_worktree *worktree = NULL;
3675 struct got_repository *repo = NULL;
3677 struct got_reference *branch = NULL;
3678 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3679 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3680 struct got_object_id *resume_commit_id = NULL;
3681 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3682 struct got_commit_graph *graph = NULL;
3683 struct got_commit_object *commit = NULL;
3684 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3685 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3686 struct got_object_id_queue commits;
3687 struct got_pathlist_head merged_paths;
3688 const struct got_object_id_queue *parent_ids;
3689 struct got_object_qid *qid, *pid;
3691 SIMPLEQ_INIT(&commits);
3692 TAILQ_INIT(&merged_paths);
3694 while ((ch = getopt(argc, argv, "ac")) != -1) {
3700 continue_rebase = 1;
3712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3713 "unveil", NULL) == -1)
3716 if (abort_rebase && continue_rebase)
3718 else if (abort_rebase || continue_rebase) {
3721 } else if (argc != 1)
3724 cwd = getcwd(NULL, 0);
3726 error = got_error_from_errno("getcwd");
3729 error = got_worktree_open(&worktree, cwd);
3733 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3737 error = apply_unveil(got_repo_get_path(repo), 0,
3738 got_worktree_get_root_path(worktree), 0);
3742 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3746 if (rebase_in_progress && abort_rebase) {
3748 error = got_worktree_rebase_continue(&resume_commit_id,
3749 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3752 printf("Switching work tree to %s\n",
3753 got_ref_get_symref_target(new_base_branch));
3754 error = got_worktree_rebase_abort(worktree, repo,
3755 new_base_branch, update_progress, &did_something);
3758 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3759 goto done; /* nothing else to do */
3760 } else if (abort_rebase) {
3761 error = got_error(GOT_ERR_NOT_REBASING);
3765 if (continue_rebase) {
3766 error = got_worktree_rebase_continue(&resume_commit_id,
3767 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3771 error = rebase_commit(NULL, worktree, tmp_branch,
3772 resume_commit_id, repo);
3776 yca_id = got_object_id_dup(resume_commit_id);
3777 if (yca_id == NULL) {
3778 error = got_error_from_errno("got_object_id_dup");
3782 error = got_ref_open(&branch, repo, argv[0], 0);
3786 error = check_same_branch(
3787 got_worktree_get_base_commit_id(worktree), branch, repo);
3789 if (error->code != GOT_ERR_ANCESTRY)
3793 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3794 "specified branch resolves to a commit which "
3795 "is already contained in work tree's branch");
3800 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3804 if (!continue_rebase) {
3805 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3806 got_worktree_get_base_commit_id(worktree),
3807 branch_head_commit_id, repo);
3810 if (yca_id == NULL) {
3811 error = got_error_msg(GOT_ERR_ANCESTRY,
3812 "specified branch shares no common ancestry "
3813 "with work tree's branch");
3817 error = got_worktree_rebase_prepare(&new_base_branch,
3818 &tmp_branch, worktree, branch, repo);
3823 commit_id = branch_head_commit_id;
3824 error = got_object_open_as_commit(&commit, repo, commit_id);
3828 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3831 parent_ids = got_object_commit_get_parent_ids(commit);
3832 pid = SIMPLEQ_FIRST(parent_ids);
3833 error = got_commit_graph_iter_start(graph, pid->id, repo);
3834 got_object_commit_close(commit);
3838 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3839 error = got_commit_graph_iter_next(&parent_id, graph);
3841 if (error->code == GOT_ERR_ITER_COMPLETED) {
3842 error = got_error_msg(GOT_ERR_ANCESTRY,
3843 "ran out of commits to rebase before "
3844 "youngest common ancestor commit has "
3847 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3849 error = got_commit_graph_fetch_commits(graph, 1, repo);
3853 error = rebase_check_path_prefix(parent_id, commit_id,
3854 got_worktree_get_path_prefix(worktree), repo);
3858 error = got_object_qid_alloc(&qid, commit_id);
3861 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3862 commit_id = parent_id;
3866 if (SIMPLEQ_EMPTY(&commits)) {
3867 if (continue_rebase)
3868 error = rebase_complete(worktree, branch,
3869 new_base_branch, tmp_branch, repo);
3871 error = got_error(GOT_ERR_EMPTY_REBASE);
3876 SIMPLEQ_FOREACH(qid, &commits, entry) {
3877 commit_id = qid->id;
3878 parent_id = pid ? pid->id : yca_id;
3881 error = got_worktree_rebase_merge_files(&merged_paths,
3882 worktree, parent_id, commit_id, repo, rebase_progress,
3883 &rebase_status, check_cancelled, NULL);
3887 if (rebase_status == GOT_STATUS_CONFLICT) {
3888 got_worktree_rebase_pathlist_free(&merged_paths);
3892 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3894 got_worktree_rebase_pathlist_free(&merged_paths);
3899 if (rebase_status == GOT_STATUS_CONFLICT) {
3900 error = got_worktree_rebase_postpone(worktree);
3903 error = got_error_msg(GOT_ERR_CONFLICTS,
3904 "conflicts must be resolved before rebasing can continue");
3906 error = rebase_complete(worktree, branch, new_base_branch,
3909 got_object_id_queue_free(&commits);
3910 free(branch_head_commit_id);
3911 free(resume_commit_id);
3914 got_commit_graph_close(graph);
3916 got_object_commit_close(commit);
3918 got_ref_close(branch);
3919 if (new_base_branch)
3920 got_ref_close(new_base_branch);
3922 got_ref_close(tmp_branch);
3924 got_worktree_close(worktree);
3926 got_repo_close(repo);