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;
248 if (unveil("gmon.out", "rwc") != 0)
249 return got_error_from_errno2("unveil", "gmon.out");
251 if (create_worktree) {
252 /* Pre-create work tree path to avoid unveiling its parents. */
253 err = got_path_mkdir(worktree_path);
255 if (errno == EEXIST) {
256 if (got_path_dir_is_empty(worktree_path)) {
260 err = got_error_path(worktree_path,
261 GOT_ERR_DIR_NOT_EMPTY);
265 if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
269 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
270 return got_error_from_errno2("unveil", repo_path);
272 if (worktree_path && unveil(worktree_path, "rwc") != 0)
273 return got_error_from_errno2("unveil", worktree_path);
275 if (unveil("/tmp", "rwc") != 0)
276 return got_error_from_errno2("unveil", "/tmp");
278 err = got_privsep_unveil_exec_helpers();
282 if (unveil(NULL, NULL) != 0)
283 return got_error_from_errno("unveil");
291 fprintf(stderr, "usage: %s init path\n", getprogname());
295 static const struct got_error *
296 cmd_init(int argc, char *argv[])
298 const struct got_error *error = NULL;
299 char *repo_path = NULL;
302 while ((ch = getopt(argc, argv, "")) != -1) {
314 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
320 repo_path = strdup(argv[0]);
321 if (repo_path == NULL)
322 return got_error_from_errno("strdup");
324 got_path_strip_trailing_slashes(repo_path);
326 error = got_path_mkdir(repo_path);
328 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
331 error = apply_unveil(repo_path, 0, NULL, 0);
335 error = got_repo_init(repo_path);
347 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
348 "[-r repository-path] [-I pattern] path\n", getprogname());
353 spawn_editor(const char *editor, const char *file)
356 sig_t sighup, sigint, sigquit;
359 sighup = signal(SIGHUP, SIG_IGN);
360 sigint = signal(SIGINT, SIG_IGN);
361 sigquit = signal(SIGQUIT, SIG_IGN);
363 switch (pid = fork()) {
367 execl(editor, editor, file, (char *)NULL);
371 while (waitpid(pid, &st, 0) == -1)
376 (void)signal(SIGHUP, sighup);
377 (void)signal(SIGINT, sigint);
378 (void)signal(SIGQUIT, sigquit);
380 if (!WIFEXITED(st)) {
385 return WEXITSTATUS(st);
388 static const struct got_error *
389 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
390 const char *initial_content)
392 const struct got_error *err = NULL;
396 int content_changed = 0;
401 if (stat(logmsg_path, &st) == -1)
402 return got_error_from_errno2("stat", logmsg_path);
404 if (spawn_editor(editor, logmsg_path) == -1)
405 return got_error_from_errno("failed spawning editor");
407 if (stat(logmsg_path, &st2) == -1)
408 return got_error_from_errno("stat");
410 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
411 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
412 "no changes made to commit message, aborting");
414 *logmsg = malloc(st2.st_size + 1);
416 return got_error_from_errno("malloc");
420 fp = fopen(logmsg_path, "r");
422 err = got_error_from_errno("fopen");
425 while (fgets(buf, sizeof(buf), fp) != NULL) {
426 if (!content_changed && strcmp(buf, initial_content) != 0)
428 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
429 continue; /* remove comments and leading empty lines */
430 len = strlcat(*logmsg, buf, st2.st_size);
434 while (len > 0 && (*logmsg)[len - 1] == '\n') {
435 (*logmsg)[len - 1] = '\0';
439 if (len == 0 || !content_changed)
440 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 "commit message cannot be empty, aborting");
450 static const struct got_error *
451 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
452 const char *branch_name)
454 char *initial_content = NULL, *logmsg_path = NULL;
455 const struct got_error *err = NULL;
458 if (asprintf(&initial_content,
459 "\n# %s to be imported to branch %s\n", path_dir,
461 return got_error_from_errno("asprintf");
463 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
467 dprintf(fd, initial_content);
470 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
472 free(initial_content);
477 static const struct got_error *
478 import_progress(void *arg, const char *path)
480 printf("A %s\n", path);
484 static const struct got_error *
485 cmd_import(int argc, char *argv[])
487 const struct got_error *error = NULL;
488 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
490 const char *got_author = getenv("GOT_AUTHOR");
491 const char *branch_name = "master";
492 char *refname = NULL, *id_str = NULL;
493 struct got_repository *repo = NULL;
494 struct got_reference *branch_ref = NULL, *head_ref = NULL;
495 struct got_object_id *new_commit_id = NULL;
497 struct got_pathlist_head ignores;
498 struct got_pathlist_entry *pe;
500 TAILQ_INIT(&ignores);
502 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
505 branch_name = optarg;
508 logmsg = strdup(optarg);
509 if (logmsg == NULL) {
510 error = got_error_from_errno("strdup");
515 repo_path = realpath(optarg, NULL);
516 if (repo_path == NULL) {
517 error = got_error_from_errno("realpath");
522 if (optarg[0] == '\0')
524 error = got_pathlist_insert(&pe, &ignores, optarg,
539 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
546 if (got_author == NULL) {
547 /* TODO: Look current user up in password database */
548 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
552 if (repo_path == NULL) {
553 repo_path = getcwd(NULL, 0);
554 if (repo_path == NULL)
555 return got_error_from_errno("getcwd");
557 got_path_strip_trailing_slashes(repo_path);
558 error = got_repo_open(&repo, repo_path);
562 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
563 error = got_error_from_errno("asprintf");
567 error = got_ref_open(&branch_ref, repo, refname, 0);
569 if (error->code != GOT_ERR_NOT_REF)
572 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
573 "import target branch already exists");
577 path_dir = realpath(argv[0], NULL);
578 if (path_dir == NULL) {
579 error = got_error_from_errno("realpath");
582 got_path_strip_trailing_slashes(path_dir);
585 * unveil(2) traverses exec(2); if an editor is used we have
586 * to apply unveil after the log message has been written.
588 if (logmsg == NULL || strlen(logmsg) == 0) {
589 error = get_editor(&editor);
592 error = collect_import_msg(&logmsg, editor, path_dir, refname);
597 if (unveil(path_dir, "r") != 0)
598 return got_error_from_errno2("unveil", path_dir);
600 error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
604 error = got_repo_import(&new_commit_id, path_dir, logmsg,
605 got_author, &ignores, repo, import_progress, NULL);
609 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
613 error = got_ref_write(branch_ref, repo);
617 error = got_object_id_str(&id_str, new_commit_id);
621 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
623 if (error->code != GOT_ERR_NOT_REF)
626 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
631 error = got_ref_write(head_ref, repo);
636 printf("Created branch %s with commit %s\n",
637 got_ref_get_name(branch_ref), id_str);
645 got_ref_close(branch_ref);
647 got_ref_close(head_ref);
654 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
655 "[-p prefix] repository-path [worktree-path]\n", getprogname());
659 static const struct got_error *
660 checkout_progress(void *arg, unsigned char status, const char *path)
662 char *worktree_path = arg;
664 /* Base commit bump happens silently. */
665 if (status == GOT_STATUS_BUMP_BASE)
668 while (path[0] == '/')
671 printf("%c %s/%s\n", status, worktree_path, path);
675 static const struct got_error *
676 check_cancelled(void *arg)
678 if (sigint_received || sigpipe_received)
679 return got_error(GOT_ERR_CANCELLED);
683 static const struct got_error *
684 check_linear_ancestry(struct got_object_id *commit_id,
685 struct got_object_id *base_commit_id, struct got_repository *repo)
687 const struct got_error *err = NULL;
688 struct got_object_id *yca_id;
690 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
691 commit_id, base_commit_id, repo);
696 return got_error(GOT_ERR_ANCESTRY);
699 * Require a straight line of history between the target commit
700 * and the work tree's base commit.
702 * Non-linear situations such as this require a rebase:
704 * (commit) D F (base_commit)
712 * 'got update' only handles linear cases:
713 * Update forwards in time: A (base/yca) - B - C - D (commit)
714 * Update backwards in time: D (base) - C - B - A (commit/yca)
716 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
717 got_object_id_cmp(base_commit_id, yca_id) != 0)
718 return got_error(GOT_ERR_ANCESTRY);
724 static const struct got_error *
725 check_same_branch(struct got_object_id *commit_id,
726 struct got_reference *head_ref, struct got_repository *repo)
728 const struct got_error *err = NULL;
729 struct got_commit_graph *graph = NULL;
730 struct got_object_id *head_commit_id = NULL;
731 int is_same_branch = 0;
733 err = got_ref_resolve(&head_commit_id, repo, head_ref);
737 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
741 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
746 struct got_object_id *id;
747 err = got_commit_graph_iter_next(&id, graph);
749 if (err->code == GOT_ERR_ITER_COMPLETED) {
753 else if (err->code != GOT_ERR_ITER_NEED_MORE)
755 err = got_commit_graph_fetch_commits(graph, 1,
762 if (got_object_id_cmp(id, commit_id) == 0) {
770 got_commit_graph_close(graph);
771 free(head_commit_id);
772 if (!err && !is_same_branch)
773 err = got_error(GOT_ERR_ANCESTRY);
777 static const struct got_error *
778 cmd_checkout(int argc, char *argv[])
780 const struct got_error *error = NULL;
781 struct got_repository *repo = NULL;
782 struct got_reference *head_ref = NULL;
783 struct got_worktree *worktree = NULL;
784 char *repo_path = NULL;
785 char *worktree_path = NULL;
786 const char *path_prefix = "";
787 const char *branch_name = GOT_REF_HEAD;
788 char *commit_id_str = NULL;
789 int ch, same_path_prefix;
791 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
794 branch_name = optarg;
797 commit_id_str = strdup(optarg);
798 if (commit_id_str == NULL)
799 return got_error_from_errno("strdup");
802 path_prefix = optarg;
814 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
815 "unveil", NULL) == -1)
819 char *cwd, *base, *dotgit;
820 repo_path = realpath(argv[0], NULL);
821 if (repo_path == NULL)
822 return got_error_from_errno2("realpath", argv[0]);
823 cwd = getcwd(NULL, 0);
825 error = got_error_from_errno("getcwd");
828 if (path_prefix[0]) {
829 base = basename(path_prefix);
831 error = got_error_from_errno2("basename",
836 base = basename(repo_path);
838 error = got_error_from_errno2("basename",
843 dotgit = strstr(base, ".git");
846 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
847 error = got_error_from_errno("asprintf");
852 } else if (argc == 2) {
853 repo_path = realpath(argv[0], NULL);
854 if (repo_path == NULL) {
855 error = got_error_from_errno2("realpath", argv[0]);
858 worktree_path = realpath(argv[1], NULL);
859 if (worktree_path == NULL) {
860 error = got_error_from_errno2("realpath", argv[1]);
866 got_path_strip_trailing_slashes(repo_path);
867 got_path_strip_trailing_slashes(worktree_path);
869 error = got_repo_open(&repo, repo_path);
873 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
877 error = got_ref_open(&head_ref, repo, branch_name, 0);
881 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
882 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
885 error = got_worktree_open(&worktree, worktree_path);
889 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
893 if (!same_path_prefix) {
894 error = got_error(GOT_ERR_PATH_PREFIX);
899 struct got_object_id *commit_id;
900 error = got_repo_match_object_id_prefix(&commit_id,
901 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
904 error = check_linear_ancestry(commit_id,
905 got_worktree_get_base_commit_id(worktree), repo);
910 error = check_same_branch(commit_id, head_ref, repo);
913 error = got_worktree_set_base_commit_id(worktree, repo,
920 error = got_worktree_checkout_files(worktree, "", repo,
921 checkout_progress, worktree_path, check_cancelled, NULL);
925 printf("Now shut up and hack\n");
937 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
942 static const struct got_error *
943 update_progress(void *arg, unsigned char status, const char *path)
945 int *did_something = arg;
947 if (status == GOT_STATUS_EXISTS)
952 /* Base commit bump happens silently. */
953 if (status == GOT_STATUS_BUMP_BASE)
956 while (path[0] == '/')
958 printf("%c %s\n", status, path);
962 static const struct got_error *
963 switch_head_ref(struct got_reference *head_ref,
964 struct got_object_id *commit_id, struct got_worktree *worktree,
965 struct got_repository *repo)
967 const struct got_error *err = NULL;
969 int ref_has_moved = 0;
971 /* Trivial case: switching between two different references. */
972 if (strcmp(got_ref_get_name(head_ref),
973 got_worktree_get_head_ref_name(worktree)) != 0) {
974 printf("Switching work tree from %s to %s\n",
975 got_worktree_get_head_ref_name(worktree),
976 got_ref_get_name(head_ref));
977 return got_worktree_set_head_ref(worktree, head_ref);
980 err = check_linear_ancestry(commit_id,
981 got_worktree_get_base_commit_id(worktree), repo);
983 if (err->code != GOT_ERR_ANCESTRY)
990 /* Switching to a rebased branch with the same reference name. */
991 err = got_object_id_str(&base_id_str,
992 got_worktree_get_base_commit_id(worktree));
995 printf("Reference %s now points at a different branch\n",
996 got_worktree_get_head_ref_name(worktree));
997 printf("Switching work tree from %s to %s\n", base_id_str,
998 got_worktree_get_head_ref_name(worktree));
1002 static const struct got_error *
1003 cmd_update(int argc, char *argv[])
1005 const struct got_error *error = NULL;
1006 struct got_repository *repo = NULL;
1007 struct got_worktree *worktree = NULL;
1008 char *worktree_path = NULL, *path = NULL;
1009 struct got_object_id *commit_id = NULL;
1010 char *commit_id_str = NULL;
1011 const char *branch_name = NULL;
1012 struct got_reference *head_ref = NULL;
1013 int ch, did_something = 0, rebase_in_progress;
1015 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1018 branch_name = optarg;
1021 commit_id_str = strdup(optarg);
1022 if (commit_id_str == NULL)
1023 return got_error_from_errno("strdup");
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1039 worktree_path = getcwd(NULL, 0);
1040 if (worktree_path == NULL) {
1041 error = got_error_from_errno("getcwd");
1044 error = got_worktree_open(&worktree, worktree_path);
1048 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1051 if (rebase_in_progress) {
1052 error = got_error(GOT_ERR_REBASING);
1059 error = got_error_from_errno("strdup");
1062 } else if (argc == 1) {
1063 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1069 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1073 error = apply_unveil(got_repo_get_path(repo), 0,
1074 got_worktree_get_root_path(worktree), 0);
1078 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1079 got_worktree_get_head_ref_name(worktree), 0);
1082 if (commit_id_str == NULL) {
1083 error = got_ref_resolve(&commit_id, repo, head_ref);
1086 error = got_object_id_str(&commit_id_str, commit_id);
1090 error = got_repo_match_object_id_prefix(&commit_id,
1091 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1094 free(commit_id_str);
1095 error = got_object_id_str(&commit_id_str, commit_id);
1101 struct got_object_id *head_commit_id;
1102 if (strlen(path) != 0) {
1103 fprintf(stderr, "%s: switching between branches "
1104 "requires that the entire work tree "
1105 "gets updated, not just '%s'\n",
1106 getprogname(), path);
1107 error = got_error(GOT_ERR_BAD_PATH);
1110 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1113 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1114 free(head_commit_id);
1117 error = check_same_branch(commit_id, head_ref, repo);
1120 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1124 error = check_linear_ancestry(commit_id,
1125 got_worktree_get_base_commit_id(worktree), repo);
1126 if (error != NULL) {
1127 if (error->code == GOT_ERR_ANCESTRY)
1128 error = got_error(GOT_ERR_BRANCH_MOVED);
1131 error = check_same_branch(commit_id, head_ref, repo);
1136 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1138 error = got_worktree_set_base_commit_id(worktree, repo,
1144 error = got_worktree_checkout_files(worktree, path, repo,
1145 update_progress, &did_something, check_cancelled, NULL);
1150 printf("Updated to commit %s\n", commit_id_str);
1152 printf("Already up-to-date\n");
1154 free(worktree_path);
1157 free(commit_id_str);
1161 static const struct got_error *
1162 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1163 int diff_context, struct got_repository *repo)
1165 const struct got_error *err = NULL;
1166 struct got_tree_object *tree1 = NULL, *tree2;
1167 struct got_object_qid *qid;
1168 char *id_str1 = NULL, *id_str2;
1169 struct got_diff_blob_output_unidiff_arg arg;
1171 err = got_object_open_as_tree(&tree2, repo,
1172 got_object_commit_get_tree_id(commit));
1176 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1178 struct got_commit_object *pcommit;
1180 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1184 err = got_object_open_as_tree(&tree1, repo,
1185 got_object_commit_get_tree_id(pcommit));
1186 got_object_commit_close(pcommit);
1190 err = got_object_id_str(&id_str1, qid->id);
1195 err = got_object_id_str(&id_str2, id);
1199 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1200 arg.diff_context = diff_context;
1201 arg.outfile = stdout;
1202 err = got_diff_tree(tree1, tree2, "", "", repo,
1203 got_diff_blob_output_unidiff, &arg);
1206 got_object_tree_close(tree1);
1207 got_object_tree_close(tree2);
1214 get_datestr(time_t *time, char *datebuf)
1216 char *p, *s = ctime_r(time, datebuf);
1217 p = strchr(s, '\n');
1223 static const struct got_error *
1224 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1225 struct got_repository *repo, int show_patch, int diff_context,
1226 struct got_reflist_head *refs)
1228 const struct got_error *err = NULL;
1229 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1231 time_t committer_time;
1232 const char *author, *committer;
1233 char *refs_str = NULL;
1234 struct got_reflist_entry *re;
1236 SIMPLEQ_FOREACH(re, refs, entry) {
1239 if (got_object_id_cmp(re->id, id) != 0)
1241 name = got_ref_get_name(re->ref);
1242 if (strcmp(name, GOT_REF_HEAD) == 0)
1244 if (strncmp(name, "refs/", 5) == 0)
1246 if (strncmp(name, "got/", 4) == 0)
1248 if (strncmp(name, "heads/", 6) == 0)
1250 if (strncmp(name, "remotes/", 8) == 0)
1253 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1255 err = got_error_from_errno("asprintf");
1261 err = got_object_id_str(&id_str, id);
1265 printf("-----------------------------------------------\n");
1266 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1267 refs_str ? refs_str : "", refs_str ? ")" : "");
1272 printf("from: %s\n", got_object_commit_get_author(commit));
1273 committer_time = got_object_commit_get_committer_time(commit);
1274 datestr = get_datestr(&committer_time, datebuf);
1275 printf("date: %s UTC\n", datestr);
1276 author = got_object_commit_get_author(commit);
1277 committer = got_object_commit_get_committer(commit);
1278 if (strcmp(author, committer) != 0)
1279 printf("via: %s\n", committer);
1280 if (got_object_commit_get_nparents(commit) > 1) {
1281 const struct got_object_id_queue *parent_ids;
1282 struct got_object_qid *qid;
1284 parent_ids = got_object_commit_get_parent_ids(commit);
1285 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1286 err = got_object_id_str(&id_str, qid->id);
1289 printf("parent %d: %s\n", n++, id_str);
1294 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1295 if (logmsg0 == NULL)
1296 return got_error_from_errno("strdup");
1300 line = strsep(&logmsg, "\n");
1302 printf(" %s\n", line);
1307 err = print_patch(commit, id, diff_context, repo);
1312 if (fflush(stdout) != 0 && err == NULL)
1313 err = got_error_from_errno("fflush");
1317 static const struct got_error *
1318 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1319 char *path, int show_patch, int diff_context, int limit,
1320 int first_parent_traversal, struct got_reflist_head *refs)
1322 const struct got_error *err;
1323 struct got_commit_graph *graph;
1325 err = got_commit_graph_open(&graph, root_id, path,
1326 first_parent_traversal, repo);
1329 err = got_commit_graph_iter_start(graph, root_id, repo);
1333 struct got_commit_object *commit;
1334 struct got_object_id *id;
1336 if (sigint_received || sigpipe_received)
1339 err = got_commit_graph_iter_next(&id, graph);
1341 if (err->code == GOT_ERR_ITER_COMPLETED) {
1345 if (err->code != GOT_ERR_ITER_NEED_MORE)
1347 err = got_commit_graph_fetch_commits(graph, 1, repo);
1356 err = got_object_open_as_commit(&commit, repo, id);
1359 err = print_commit(commit, id, repo, show_patch, diff_context,
1361 got_object_commit_close(commit);
1362 if (err || (limit && --limit == 0))
1366 got_commit_graph_close(graph);
1373 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1374 "[-r repository-path] [path]\n", getprogname());
1378 static const struct got_error *
1379 cmd_log(int argc, char *argv[])
1381 const struct got_error *error;
1382 struct got_repository *repo = NULL;
1383 struct got_worktree *worktree = NULL;
1384 struct got_commit_object *commit = NULL;
1385 struct got_object_id *id = NULL;
1386 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1387 char *start_commit = NULL;
1388 int diff_context = 3, ch;
1389 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1391 struct got_reflist_head refs;
1393 SIMPLEQ_INIT(&refs);
1396 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1402 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1408 start_commit = optarg;
1411 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1414 err(1, "-C option %s", errstr);
1417 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1419 err(1, "-l option %s", errstr);
1422 first_parent_traversal = 1;
1425 repo_path = realpath(optarg, NULL);
1426 if (repo_path == NULL)
1427 err(1, "-r option");
1428 got_path_strip_trailing_slashes(repo_path);
1439 cwd = getcwd(NULL, 0);
1441 error = got_error_from_errno("getcwd");
1445 error = got_worktree_open(&worktree, cwd);
1446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1453 error = got_error_from_errno("strdup");
1456 } else if (argc == 1) {
1458 error = got_worktree_resolve_path(&path, worktree,
1463 path = strdup(argv[0]);
1465 error = got_error_from_errno("strdup");
1472 if (repo_path == NULL) {
1473 repo_path = worktree ?
1474 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1476 if (repo_path == NULL) {
1477 error = got_error_from_errno("strdup");
1481 error = got_repo_open(&repo, repo_path);
1485 error = apply_unveil(got_repo_get_path(repo), 1,
1486 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1490 if (start_commit == NULL) {
1491 struct got_reference *head_ref;
1492 error = got_ref_open(&head_ref, repo,
1493 worktree ? got_worktree_get_head_ref_name(worktree)
1497 error = got_ref_resolve(&id, repo, head_ref);
1498 got_ref_close(head_ref);
1501 error = got_object_open_as_commit(&commit, repo, id);
1503 struct got_reference *ref;
1504 error = got_ref_open(&ref, repo, start_commit, 0);
1505 if (error == NULL) {
1507 error = got_ref_resolve(&id, repo, ref);
1511 error = got_object_get_type(&obj_type, repo, id);
1514 if (obj_type == GOT_OBJ_TYPE_TAG) {
1515 struct got_tag_object *tag;
1516 error = got_object_open_as_tag(&tag, repo, id);
1519 if (got_object_tag_get_object_type(tag) !=
1520 GOT_OBJ_TYPE_COMMIT) {
1521 got_object_tag_close(tag);
1522 error = got_error(GOT_ERR_OBJ_TYPE);
1526 id = got_object_id_dup(
1527 got_object_tag_get_object_id(tag));
1529 error = got_error_from_errno(
1530 "got_object_id_dup");
1531 got_object_tag_close(tag);
1534 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1535 error = got_error(GOT_ERR_OBJ_TYPE);
1538 error = got_object_open_as_commit(&commit, repo, id);
1542 if (commit == NULL) {
1543 error = got_repo_match_object_id_prefix(&id,
1544 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1552 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1557 path = in_repo_path;
1560 error = got_ref_list(&refs, repo);
1564 error = print_commits(id, repo, path, show_patch,
1565 diff_context, limit, first_parent_traversal, &refs);
1572 got_worktree_close(worktree);
1574 const struct got_error *repo_error;
1575 repo_error = got_repo_close(repo);
1579 got_ref_list_free(&refs);
1586 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1587 "[object1 object2 | path]\n", getprogname());
1591 struct print_diff_arg {
1592 struct got_repository *repo;
1593 struct got_worktree *worktree;
1599 static const struct got_error *
1600 print_diff(void *arg, unsigned char status, const char *path,
1601 struct got_object_id *blob_id, struct got_object_id *commit_id)
1603 struct print_diff_arg *a = arg;
1604 const struct got_error *err = NULL;
1605 struct got_blob_object *blob1 = NULL;
1607 char *abspath = NULL;
1610 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1611 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1614 if (!a->header_shown) {
1615 printf("diff %s %s\n", a->id_str,
1616 got_worktree_get_root_path(a->worktree));
1617 a->header_shown = 1;
1620 if (status != GOT_STATUS_ADD) {
1621 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1627 if (status != GOT_STATUS_DELETE) {
1628 if (asprintf(&abspath, "%s/%s",
1629 got_worktree_get_root_path(a->worktree), path) == -1) {
1630 err = got_error_from_errno("asprintf");
1634 f2 = fopen(abspath, "r");
1636 err = got_error_from_errno2("fopen", abspath);
1639 if (lstat(abspath, &sb) == -1) {
1640 err = got_error_from_errno2("lstat", abspath);
1646 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1650 got_object_blob_close(blob1);
1651 if (f2 && fclose(f2) != 0 && err == NULL)
1652 err = got_error_from_errno("fclose");
1657 static const struct got_error *
1658 cmd_diff(int argc, char *argv[])
1660 const struct got_error *error;
1661 struct got_repository *repo = NULL;
1662 struct got_worktree *worktree = NULL;
1663 char *cwd = NULL, *repo_path = NULL;
1664 struct got_object_id *id1 = NULL, *id2 = NULL;
1665 const char *id_str1 = NULL, *id_str2 = NULL;
1666 char *label1 = NULL, *label2 = NULL;
1668 int diff_context = 3, ch;
1673 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1678 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1681 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1683 err(1, "-C option %s", errstr);
1686 repo_path = realpath(optarg, NULL);
1687 if (repo_path == NULL)
1688 err(1, "-r option");
1689 got_path_strip_trailing_slashes(repo_path);
1700 cwd = getcwd(NULL, 0);
1702 error = got_error_from_errno("getcwd");
1705 error = got_worktree_open(&worktree, cwd);
1706 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1709 if (worktree == NULL) {
1710 error = got_error(GOT_ERR_NOT_WORKTREE);
1715 "-r option can't be used when diffing a work tree");
1716 repo_path = strdup(got_worktree_get_repo_path(worktree));
1717 if (repo_path == NULL) {
1718 error = got_error_from_errno("strdup");
1722 error = got_worktree_resolve_path(&path, worktree,
1729 error = got_error_from_errno("strdup");
1733 } else if (argc == 2) {
1736 if (worktree && repo_path == NULL) {
1738 strdup(got_worktree_get_repo_path(worktree));
1739 if (repo_path == NULL) {
1740 error = got_error_from_errno("strdup");
1747 if (repo_path == NULL) {
1748 repo_path = getcwd(NULL, 0);
1749 if (repo_path == NULL)
1750 return got_error_from_errno("getcwd");
1753 error = got_repo_open(&repo, repo_path);
1758 error = apply_unveil(got_repo_get_path(repo), 1,
1759 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1764 struct print_diff_arg arg;
1766 error = got_object_id_str(&id_str,
1767 got_worktree_get_base_commit_id(worktree));
1771 arg.worktree = worktree;
1772 arg.diff_context = diff_context;
1773 arg.id_str = id_str;
1774 arg.header_shown = 0;
1776 error = got_worktree_status(worktree, path, repo, print_diff,
1777 &arg, check_cancelled, NULL);
1782 error = got_repo_match_object_id_prefix(&id1, id_str1,
1783 GOT_OBJ_TYPE_ANY, repo);
1785 struct got_reference *ref;
1786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1788 error = got_ref_open(&ref, repo, id_str1, 0);
1791 label1 = strdup(got_ref_get_name(ref));
1792 if (label1 == NULL) {
1793 error = got_error_from_errno("strdup");
1796 error = got_ref_resolve(&id1, repo, ref);
1801 error = got_object_id_str(&label1, id1);
1802 if (label1 == NULL) {
1803 error = got_error_from_errno("strdup");
1808 error = got_repo_match_object_id_prefix(&id2, id_str2,
1809 GOT_OBJ_TYPE_ANY, repo);
1811 struct got_reference *ref;
1812 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1814 error = got_ref_open(&ref, repo, id_str2, 0);
1817 label2 = strdup(got_ref_get_name(ref));
1818 if (label2 == NULL) {
1819 error = got_error_from_errno("strdup");
1822 error = got_ref_resolve(&id2, repo, ref);
1827 error = got_object_id_str(&label2, id2);
1828 if (label2 == NULL) {
1829 error = got_error_from_errno("strdup");
1834 error = got_object_get_type(&type1, repo, id1);
1838 error = got_object_get_type(&type2, repo, id2);
1842 if (type1 != type2) {
1843 error = got_error(GOT_ERR_OBJ_TYPE);
1848 case GOT_OBJ_TYPE_BLOB:
1849 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1850 diff_context, repo, stdout);
1852 case GOT_OBJ_TYPE_TREE:
1853 error = got_diff_objects_as_trees(id1, id2, "", "",
1854 diff_context, repo, stdout);
1856 case GOT_OBJ_TYPE_COMMIT:
1857 printf("diff %s %s\n", label1, label2);
1858 error = got_diff_objects_as_commits(id1, id2, diff_context,
1862 error = got_error(GOT_ERR_OBJ_TYPE);
1872 got_worktree_close(worktree);
1874 const struct got_error *repo_error;
1875 repo_error = got_repo_close(repo);
1886 "usage: %s blame [-c commit] [-r repository-path] path\n",
1891 static const struct got_error *
1892 cmd_blame(int argc, char *argv[])
1894 const struct got_error *error;
1895 struct got_repository *repo = NULL;
1896 struct got_worktree *worktree = NULL;
1897 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1898 struct got_object_id *commit_id = NULL;
1899 char *commit_id_str = NULL;
1903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1908 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1911 commit_id_str = optarg;
1914 repo_path = realpath(optarg, NULL);
1915 if (repo_path == NULL)
1916 err(1, "-r option");
1917 got_path_strip_trailing_slashes(repo_path);
1933 cwd = getcwd(NULL, 0);
1935 error = got_error_from_errno("getcwd");
1938 if (repo_path == NULL) {
1939 error = got_worktree_open(&worktree, cwd);
1940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1946 strdup(got_worktree_get_repo_path(worktree));
1947 if (repo_path == NULL)
1948 error = got_error_from_errno("strdup");
1952 repo_path = strdup(cwd);
1953 if (repo_path == NULL) {
1954 error = got_error_from_errno("strdup");
1960 error = got_repo_open(&repo, repo_path);
1964 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1969 const char *prefix = got_worktree_get_path_prefix(worktree);
1970 char *p, *worktree_subdir = cwd +
1971 strlen(got_worktree_get_root_path(worktree));
1972 if (asprintf(&p, "%s%s%s%s%s",
1973 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1974 worktree_subdir, worktree_subdir[0] ? "/" : "",
1976 error = got_error_from_errno("asprintf");
1979 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1982 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1987 if (commit_id_str == NULL) {
1988 struct got_reference *head_ref;
1989 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1992 error = got_ref_resolve(&commit_id, repo, head_ref);
1993 got_ref_close(head_ref);
1997 error = got_repo_match_object_id_prefix(&commit_id,
1998 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2003 error = got_blame(in_repo_path, commit_id, repo, stdout);
2010 got_worktree_close(worktree);
2012 const struct got_error *repo_error;
2013 repo_error = got_repo_close(repo);
2024 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2030 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2031 const char *root_path)
2033 int is_root_path = (strcmp(path, root_path) == 0);
2035 path += strlen(root_path);
2036 while (path[0] == '/')
2039 printf("%s%s%s%s%s\n", id ? id : "", path,
2040 is_root_path ? "" : "/", te->name,
2041 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2044 static const struct got_error *
2045 print_tree(const char *path, struct got_object_id *commit_id,
2046 int show_ids, int recurse, const char *root_path,
2047 struct got_repository *repo)
2049 const struct got_error *err = NULL;
2050 struct got_object_id *tree_id = NULL;
2051 struct got_tree_object *tree = NULL;
2052 const struct got_tree_entries *entries;
2053 struct got_tree_entry *te;
2055 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2059 err = got_object_open_as_tree(&tree, repo, tree_id);
2062 entries = got_object_tree_get_entries(tree);
2063 te = SIMPLEQ_FIRST(&entries->head);
2067 if (sigint_received || sigpipe_received)
2072 err = got_object_id_str(&id_str, te->id);
2075 if (asprintf(&id, "%s ", id_str) == -1) {
2076 err = got_error_from_errno("asprintf");
2082 print_entry(te, id, path, root_path);
2085 if (recurse && S_ISDIR(te->mode)) {
2087 if (asprintf(&child_path, "%s%s%s", path,
2088 path[0] == '/' && path[1] == '\0' ? "" : "/",
2090 err = got_error_from_errno("asprintf");
2093 err = print_tree(child_path, commit_id, show_ids, 1,
2100 te = SIMPLEQ_NEXT(te, entry);
2104 got_object_tree_close(tree);
2109 static const struct got_error *
2110 cmd_tree(int argc, char *argv[])
2112 const struct got_error *error;
2113 struct got_repository *repo = NULL;
2114 struct got_worktree *worktree = NULL;
2116 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2117 struct got_object_id *commit_id = NULL;
2118 char *commit_id_str = NULL;
2119 int show_ids = 0, recurse = 0;
2123 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2128 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2131 commit_id_str = optarg;
2134 repo_path = realpath(optarg, NULL);
2135 if (repo_path == NULL)
2136 err(1, "-r option");
2137 got_path_strip_trailing_slashes(repo_path);
2161 cwd = getcwd(NULL, 0);
2163 error = got_error_from_errno("getcwd");
2166 if (repo_path == NULL) {
2167 error = got_worktree_open(&worktree, cwd);
2168 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2174 strdup(got_worktree_get_repo_path(worktree));
2175 if (repo_path == NULL)
2176 error = got_error_from_errno("strdup");
2180 repo_path = strdup(cwd);
2181 if (repo_path == NULL) {
2182 error = got_error_from_errno("strdup");
2188 error = got_repo_open(&repo, repo_path);
2192 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2198 char *p, *worktree_subdir = cwd +
2199 strlen(got_worktree_get_root_path(worktree));
2200 if (asprintf(&p, "%s/%s",
2201 got_worktree_get_path_prefix(worktree),
2202 worktree_subdir) == -1) {
2203 error = got_error_from_errno("asprintf");
2206 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2213 if (in_repo_path == NULL) {
2214 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2219 if (commit_id_str == NULL) {
2220 struct got_reference *head_ref;
2221 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2224 error = got_ref_resolve(&commit_id, repo, head_ref);
2225 got_ref_close(head_ref);
2229 error = got_repo_match_object_id_prefix(&commit_id,
2230 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2235 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2236 in_repo_path, repo);
2243 got_worktree_close(worktree);
2245 const struct got_error *repo_error;
2246 repo_error = got_repo_close(repo);
2256 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2260 static const struct got_error *
2261 print_status(void *arg, unsigned char status, const char *path,
2262 struct got_object_id *blob_id, struct got_object_id *commit_id)
2264 printf("%c %s\n", status, path);
2268 static const struct got_error *
2269 cmd_status(int argc, char *argv[])
2271 const struct got_error *error = NULL;
2272 struct got_repository *repo = NULL;
2273 struct got_worktree *worktree = NULL;
2274 char *cwd = NULL, *path = NULL;
2277 while ((ch = getopt(argc, argv, "")) != -1) {
2289 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2293 cwd = getcwd(NULL, 0);
2295 error = got_error_from_errno("getcwd");
2299 error = got_worktree_open(&worktree, cwd);
2306 error = got_error_from_errno("strdup");
2309 } else if (argc == 1) {
2310 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2320 error = apply_unveil(got_repo_get_path(repo), 1,
2321 got_worktree_get_root_path(worktree), 0);
2325 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2326 check_cancelled, NULL);
2337 "usage: %s ref [-r repository] -l | -d name | name target\n",
2342 static const struct got_error *
2343 list_refs(struct got_repository *repo)
2345 static const struct got_error *err = NULL;
2346 struct got_reflist_head refs;
2347 struct got_reflist_entry *re;
2349 SIMPLEQ_INIT(&refs);
2350 err = got_ref_list(&refs, repo);
2354 SIMPLEQ_FOREACH(re, &refs, entry) {
2356 refstr = got_ref_to_str(re->ref);
2358 return got_error_from_errno("got_ref_to_str");
2359 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2363 got_ref_list_free(&refs);
2367 static const struct got_error *
2368 delete_ref(struct got_repository *repo, const char *refname)
2370 const struct got_error *err = NULL;
2371 struct got_reference *ref;
2373 err = got_ref_open(&ref, repo, refname, 0);
2377 err = got_ref_delete(ref, repo);
2382 static const struct got_error *
2383 add_ref(struct got_repository *repo, const char *refname, const char *target)
2385 const struct got_error *err = NULL;
2386 struct got_object_id *id;
2387 struct got_reference *ref = NULL;
2390 * Don't let the user create a reference named '-'.
2391 * While technically a valid reference name, this case is usually
2392 * an unintended typo.
2394 if (refname[0] == '-' && refname[1] == '\0')
2395 return got_error(GOT_ERR_BAD_REF_NAME);
2397 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2400 struct got_reference *target_ref;
2402 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2404 err = got_ref_open(&target_ref, repo, target, 0);
2407 err = got_ref_resolve(&id, repo, target_ref);
2408 got_ref_close(target_ref);
2413 err = got_ref_alloc(&ref, refname, id);
2417 err = got_ref_write(ref, repo);
2425 static const struct got_error *
2426 cmd_ref(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_repository *repo = NULL;
2430 struct got_worktree *worktree = NULL;
2431 char *cwd = NULL, *repo_path = NULL;
2432 int ch, do_list = 0;
2433 const char *delref = NULL;
2435 /* TODO: Add -s option for adding symbolic references. */
2436 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2442 repo_path = realpath(optarg, NULL);
2443 if (repo_path == NULL)
2444 err(1, "-r option");
2445 got_path_strip_trailing_slashes(repo_path);
2456 if (do_list && delref)
2457 errx(1, "-l and -d options are mutually exclusive\n");
2462 if (do_list || delref) {
2465 } else if (argc != 2)
2470 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2474 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2475 "sendfd unveil", NULL) == -1)
2479 cwd = getcwd(NULL, 0);
2481 error = got_error_from_errno("getcwd");
2485 if (repo_path == NULL) {
2486 error = got_worktree_open(&worktree, cwd);
2487 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2493 strdup(got_worktree_get_repo_path(worktree));
2494 if (repo_path == NULL)
2495 error = got_error_from_errno("strdup");
2499 repo_path = strdup(cwd);
2500 if (repo_path == NULL) {
2501 error = got_error_from_errno("strdup");
2507 error = got_repo_open(&repo, repo_path);
2511 error = apply_unveil(got_repo_get_path(repo), do_list,
2512 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2517 error = list_refs(repo);
2519 error = delete_ref(repo, delref);
2521 error = add_ref(repo, argv[0], argv[1]);
2524 got_repo_close(repo);
2526 got_worktree_close(worktree);
2536 "usage: %s branch [-r repository] -l | -d name | "
2537 "name [base-branch]\n", getprogname());
2541 static const struct got_error *
2542 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2544 static const struct got_error *err = NULL;
2545 struct got_reflist_head refs;
2546 struct got_reflist_entry *re;
2548 SIMPLEQ_INIT(&refs);
2550 err = got_ref_list(&refs, repo);
2554 SIMPLEQ_FOREACH(re, &refs, entry) {
2555 const char *refname, *marker = " ";
2557 refname = got_ref_get_name(re->ref);
2558 if (strncmp(refname, "refs/heads/", 11) != 0)
2560 if (worktree && strcmp(refname,
2561 got_worktree_get_head_ref_name(worktree)) == 0) {
2562 struct got_object_id *id = NULL;
2563 err = got_ref_resolve(&id, repo, re->ref);
2566 if (got_object_id_cmp(id,
2567 got_worktree_get_base_commit_id(worktree)) == 0)
2574 refstr = got_ref_to_str(re->ref);
2576 return got_error_from_errno("got_ref_to_str");
2577 printf("%s%s: %s\n", marker, refname, refstr);
2581 got_ref_list_free(&refs);
2585 static const struct got_error *
2586 delete_branch(struct got_repository *repo, const char *branch_name)
2588 const struct got_error *err = NULL;
2589 struct got_reference *ref;
2592 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2593 return got_error_from_errno("asprintf");
2595 err = got_ref_open(&ref, repo, refname, 0);
2599 err = got_ref_delete(ref, repo);
2606 static const struct got_error *
2607 add_branch(struct got_repository *repo, const char *branch_name,
2608 const char *base_branch)
2610 const struct got_error *err = NULL;
2611 struct got_object_id *id = NULL;
2612 struct got_reference *ref = NULL;
2613 char *base_refname = NULL, *refname = NULL;
2614 struct got_reference *base_ref;
2617 * Don't let the user create a branch named '-'.
2618 * While technically a valid reference name, this case is usually
2619 * an unintended typo.
2621 if (branch_name[0] == '-' && branch_name[1] == '\0')
2622 return got_error(GOT_ERR_BAD_REF_NAME);
2624 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2625 base_refname = strdup(GOT_REF_HEAD);
2626 if (base_refname == NULL)
2627 return got_error_from_errno("strdup");
2628 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2629 return got_error_from_errno("asprintf");
2631 err = got_ref_open(&base_ref, repo, base_refname, 0);
2634 err = got_ref_resolve(&id, repo, base_ref);
2635 got_ref_close(base_ref);
2639 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2640 err = got_error_from_errno("asprintf");
2644 err = got_ref_open(&ref, repo, refname, 0);
2646 err = got_error(GOT_ERR_BRANCH_EXISTS);
2648 } else if (err->code != GOT_ERR_NOT_REF)
2651 err = got_ref_alloc(&ref, refname, id);
2655 err = got_ref_write(ref, repo);
2665 static const struct got_error *
2666 cmd_branch(int argc, char *argv[])
2668 const struct got_error *error = NULL;
2669 struct got_repository *repo = NULL;
2670 struct got_worktree *worktree = NULL;
2671 char *cwd = NULL, *repo_path = NULL;
2672 int ch, do_list = 0;
2673 const char *delref = NULL;
2675 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2681 repo_path = realpath(optarg, NULL);
2682 if (repo_path == NULL)
2683 err(1, "-r option");
2684 got_path_strip_trailing_slashes(repo_path);
2695 if (do_list && delref)
2696 errx(1, "-l and -d options are mutually exclusive\n");
2701 if (do_list || delref) {
2704 } else if (argc < 1 || argc > 2)
2709 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2713 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2714 "sendfd unveil", NULL) == -1)
2718 cwd = getcwd(NULL, 0);
2720 error = got_error_from_errno("getcwd");
2724 if (repo_path == NULL) {
2725 error = got_worktree_open(&worktree, cwd);
2726 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2732 strdup(got_worktree_get_repo_path(worktree));
2733 if (repo_path == NULL)
2734 error = got_error_from_errno("strdup");
2738 repo_path = strdup(cwd);
2739 if (repo_path == NULL) {
2740 error = got_error_from_errno("strdup");
2746 error = got_repo_open(&repo, repo_path);
2750 error = apply_unveil(got_repo_get_path(repo), do_list,
2751 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2756 error = list_branches(repo, worktree);
2758 error = delete_branch(repo, delref);
2760 const char *base_branch;
2762 base_branch = worktree ?
2763 got_worktree_get_head_ref_name(worktree) :
2766 base_branch = argv[1];
2767 error = add_branch(repo, argv[0], base_branch);
2771 got_repo_close(repo);
2773 got_worktree_close(worktree);
2782 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2786 static const struct got_error *
2787 cmd_add(int argc, char *argv[])
2789 const struct got_error *error = NULL;
2790 struct got_repository *repo = NULL;
2791 struct got_worktree *worktree = NULL;
2793 struct got_pathlist_head paths;
2794 struct got_pathlist_entry *pe;
2799 while ((ch = getopt(argc, argv, "")) != -1) {
2811 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2818 /* make sure each file exists before doing anything halfway */
2819 for (x = 0; x < argc; x++) {
2820 char *path = realpath(argv[x], NULL);
2822 error = got_error_from_errno2("realpath", argv[x]);
2828 cwd = getcwd(NULL, 0);
2830 error = got_error_from_errno("getcwd");
2834 error = got_worktree_open(&worktree, cwd);
2838 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2842 error = apply_unveil(got_repo_get_path(repo), 1,
2843 got_worktree_get_root_path(worktree), 0);
2847 for (x = 0; x < argc; x++) {
2848 char *path = realpath(argv[x], NULL);
2850 error = got_error_from_errno2("realpath", argv[x]);
2854 got_path_strip_trailing_slashes(path);
2855 error = got_pathlist_insert(&pe, &paths, path, NULL);
2861 error = got_worktree_schedule_add(worktree, &paths, print_status,
2865 got_repo_close(repo);
2867 got_worktree_close(worktree);
2868 TAILQ_FOREACH(pe, &paths, entry)
2869 free((char *)pe->path);
2870 got_pathlist_free(&paths);
2878 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2882 static const struct got_error *
2883 cmd_remove(int argc, char *argv[])
2885 const struct got_error *error = NULL;
2886 struct got_worktree *worktree = NULL;
2887 struct got_repository *repo = NULL;
2889 struct got_pathlist_head paths;
2890 struct got_pathlist_entry *pe;
2891 int ch, i, delete_local_mods = 0;
2895 while ((ch = getopt(argc, argv, "f")) != -1) {
2898 delete_local_mods = 1;
2910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2917 /* make sure each file exists before doing anything halfway */
2918 for (i = 0; i < argc; i++) {
2919 char *path = realpath(argv[i], NULL);
2921 error = got_error_from_errno2("realpath", argv[i]);
2927 cwd = getcwd(NULL, 0);
2929 error = got_error_from_errno("getcwd");
2932 error = got_worktree_open(&worktree, cwd);
2936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2940 error = apply_unveil(got_repo_get_path(repo), 1,
2941 got_worktree_get_root_path(worktree), 0);
2945 for (i = 0; i < argc; i++) {
2946 char *path = realpath(argv[i], NULL);
2948 error = got_error_from_errno2("realpath", argv[i]);
2952 got_path_strip_trailing_slashes(path);
2953 error = got_pathlist_insert(&pe, &paths, path, NULL);
2959 error = got_worktree_schedule_delete(worktree, &paths,
2960 delete_local_mods, print_status, NULL, repo);
2965 got_repo_close(repo);
2967 got_worktree_close(worktree);
2968 TAILQ_FOREACH(pe, &paths, entry)
2969 free((char *)pe->path);
2970 got_pathlist_free(&paths);
2978 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2982 static const struct got_error *
2983 revert_progress(void *arg, unsigned char status, const char *path)
2985 while (path[0] == '/')
2987 printf("%c %s\n", status, path);
2991 static const struct got_error *
2992 cmd_revert(int argc, char *argv[])
2994 const struct got_error *error = NULL;
2995 struct got_worktree *worktree = NULL;
2996 struct got_repository *repo = NULL;
2997 char *cwd = NULL, *path = NULL;
2998 struct got_pathlist_head paths;
2999 struct got_pathlist_entry *pe;
3004 while ((ch = getopt(argc, argv, "")) != -1) {
3016 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3017 "unveil", NULL) == -1)
3023 for (i = 0; i < argc; i++) {
3024 char *path = realpath(argv[i], NULL);
3026 error = got_error_from_errno2("realpath", argv[i]);
3030 got_path_strip_trailing_slashes(path);
3031 error = got_pathlist_insert(&pe, &paths, path, NULL);
3038 cwd = getcwd(NULL, 0);
3040 error = got_error_from_errno("getcwd");
3043 error = got_worktree_open(&worktree, cwd);
3047 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3051 error = apply_unveil(got_repo_get_path(repo), 1,
3052 got_worktree_get_root_path(worktree), 0);
3056 error = got_worktree_revert(worktree, &paths,
3057 revert_progress, NULL, repo);
3062 got_repo_close(repo);
3064 got_worktree_close(worktree);
3073 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3077 struct collect_commit_logmsg_arg {
3078 const char *cmdline_log;
3080 const char *worktree_path;
3081 const char *branch_name;
3082 const char *repo_path;
3087 static const struct got_error *
3088 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3091 char *initial_content = NULL;
3092 struct got_pathlist_entry *pe;
3093 const struct got_error *err = NULL;
3094 char *template = NULL;
3095 struct collect_commit_logmsg_arg *a = arg;
3099 /* if a message was specified on the command line, just use it */
3100 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3101 len = strlen(a->cmdline_log) + 1;
3102 *logmsg = malloc(len + 1);
3103 if (*logmsg == NULL)
3104 return got_error_from_errno("malloc");
3105 strlcpy(*logmsg, a->cmdline_log, len);
3109 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3110 return got_error_from_errno("asprintf");
3112 if (asprintf(&initial_content,
3113 "\n# changes to be committed on branch %s:\n",
3114 a->branch_name) == -1)
3115 return got_error_from_errno("asprintf");
3117 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3121 dprintf(fd, initial_content);
3123 TAILQ_FOREACH(pe, commitable_paths, entry) {
3124 struct got_commitable *ct = pe->data;
3125 dprintf(fd, "# %c %s\n",
3126 got_commitable_get_status(ct),
3127 got_commitable_get_path(ct));
3131 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3133 unlink(a->logmsg_path);
3134 free(a->logmsg_path);
3135 free(initial_content);
3138 /* Editor is done; we can now apply unveil(2) */
3140 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3149 static const struct got_error *
3150 cmd_commit(int argc, char *argv[])
3152 const struct got_error *error = NULL;
3153 struct got_worktree *worktree = NULL;
3154 struct got_repository *repo = NULL;
3155 char *cwd = NULL, *path = NULL, *id_str = NULL;
3156 struct got_object_id *id = NULL;
3157 const char *logmsg = NULL;
3158 const char *got_author = getenv("GOT_AUTHOR");
3159 struct collect_commit_logmsg_arg cl_arg;
3160 char *editor = NULL;
3161 int ch, rebase_in_progress;
3163 while ((ch = getopt(argc, argv, "m:")) != -1) {
3178 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3179 "unveil", NULL) == -1)
3183 path = realpath(argv[0], NULL);
3185 error = got_error_from_errno2("realpath", argv[0]);
3188 got_path_strip_trailing_slashes(path);
3189 } else if (argc != 0)
3192 if (got_author == NULL) {
3193 /* TODO: Look current user up in password database */
3194 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3198 cwd = getcwd(NULL, 0);
3200 error = got_error_from_errno("getcwd");
3203 error = got_worktree_open(&worktree, cwd);
3207 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3210 if (rebase_in_progress) {
3211 error = got_error(GOT_ERR_REBASING);
3215 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3220 * unveil(2) traverses exec(2); if an editor is used we have
3221 * to apply unveil after the log message has been written.
3223 if (logmsg == NULL || strlen(logmsg) == 0)
3224 error = get_editor(&editor);
3226 error = apply_unveil(got_repo_get_path(repo), 0,
3227 got_worktree_get_root_path(worktree), 0);
3231 cl_arg.editor = editor;
3232 cl_arg.cmdline_log = logmsg;
3233 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3234 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3235 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3236 cl_arg.branch_name += 5;
3237 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3238 cl_arg.branch_name += 6;
3239 cl_arg.repo_path = got_repo_get_path(repo);
3240 cl_arg.logmsg_path = NULL;
3241 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3242 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3244 if (cl_arg.logmsg_path)
3245 fprintf(stderr, "%s: log message preserved in %s\n",
3246 getprogname(), cl_arg.logmsg_path);
3250 if (cl_arg.logmsg_path)
3251 unlink(cl_arg.logmsg_path);
3253 error = got_object_id_str(&id_str, id);
3256 printf("Created commit %s\n", id_str);
3259 got_repo_close(repo);
3261 got_worktree_close(worktree);
3270 usage_cherrypick(void)
3272 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3276 static const struct got_error *
3277 cmd_cherrypick(int argc, char *argv[])
3279 const struct got_error *error = NULL;
3280 struct got_worktree *worktree = NULL;
3281 struct got_repository *repo = NULL;
3282 char *cwd = NULL, *commit_id_str = NULL;
3283 struct got_object_id *commit_id = NULL;
3284 struct got_commit_object *commit = NULL;
3285 struct got_object_qid *pid;
3286 struct got_reference *head_ref = NULL;
3287 int ch, did_something = 0;
3289 while ((ch = getopt(argc, argv, "")) != -1) {
3301 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3302 "unveil", NULL) == -1)
3308 cwd = getcwd(NULL, 0);
3310 error = got_error_from_errno("getcwd");
3313 error = got_worktree_open(&worktree, cwd);
3317 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3321 error = apply_unveil(got_repo_get_path(repo), 0,
3322 got_worktree_get_root_path(worktree), 0);
3326 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3327 GOT_OBJ_TYPE_COMMIT, repo);
3328 if (error != NULL) {
3329 struct got_reference *ref;
3330 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3332 error = got_ref_open(&ref, repo, argv[0], 0);
3335 error = got_ref_resolve(&commit_id, repo, ref);
3340 error = got_object_id_str(&commit_id_str, commit_id);
3344 error = got_ref_open(&head_ref, repo,
3345 got_worktree_get_head_ref_name(worktree), 0);
3349 error = check_same_branch(commit_id, head_ref, repo);
3351 if (error->code != GOT_ERR_ANCESTRY)
3355 error = got_error(GOT_ERR_SAME_BRANCH);
3359 error = got_object_open_as_commit(&commit, repo, commit_id);
3362 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3363 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3364 commit_id, repo, update_progress, &did_something, check_cancelled,
3370 printf("Merged commit %s\n", commit_id_str);
3373 got_object_commit_close(commit);
3374 free(commit_id_str);
3376 got_ref_close(head_ref);
3378 got_worktree_close(worktree);
3380 got_repo_close(repo);
3387 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3391 static const struct got_error *
3392 cmd_backout(int argc, char *argv[])
3394 const struct got_error *error = NULL;
3395 struct got_worktree *worktree = NULL;
3396 struct got_repository *repo = NULL;
3397 char *cwd = NULL, *commit_id_str = NULL;
3398 struct got_object_id *commit_id = NULL;
3399 struct got_commit_object *commit = NULL;
3400 struct got_object_qid *pid;
3401 struct got_reference *head_ref = NULL;
3402 int ch, did_something = 0;
3404 while ((ch = getopt(argc, argv, "")) != -1) {
3416 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3417 "unveil", NULL) == -1)
3423 cwd = getcwd(NULL, 0);
3425 error = got_error_from_errno("getcwd");
3428 error = got_worktree_open(&worktree, cwd);
3432 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3436 error = apply_unveil(got_repo_get_path(repo), 0,
3437 got_worktree_get_root_path(worktree), 0);
3441 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3442 GOT_OBJ_TYPE_COMMIT, repo);
3443 if (error != NULL) {
3444 struct got_reference *ref;
3445 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3447 error = got_ref_open(&ref, repo, argv[0], 0);
3450 error = got_ref_resolve(&commit_id, repo, ref);
3455 error = got_object_id_str(&commit_id_str, commit_id);
3459 error = got_ref_open(&head_ref, repo,
3460 got_worktree_get_head_ref_name(worktree), 0);
3464 error = check_same_branch(commit_id, head_ref, repo);
3468 error = got_object_open_as_commit(&commit, repo, commit_id);
3471 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3473 error = got_error(GOT_ERR_ROOT_COMMIT);
3477 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3478 update_progress, &did_something, check_cancelled, NULL);
3483 printf("Backed out commit %s\n", commit_id_str);
3486 got_object_commit_close(commit);
3487 free(commit_id_str);
3489 got_ref_close(head_ref);
3491 got_worktree_close(worktree);
3493 got_repo_close(repo);
3500 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3505 static const struct got_error *
3506 show_rebase_progress(struct got_commit_object *commit,
3507 struct got_object_id *old_id, struct got_object_id *new_id)
3509 const struct got_error *err;
3510 char *old_id_str = NULL, *new_id_str = NULL;
3511 char *logmsg0 = NULL, *logmsg, *nl;
3514 err = got_object_id_str(&old_id_str, old_id);
3519 err = got_object_id_str(&new_id_str, new_id);
3524 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3525 if (logmsg0 == NULL) {
3526 err = got_error_from_errno("strdup");
3531 while (isspace((unsigned char)logmsg[0]))
3534 old_id_str[12] = '\0';
3536 new_id_str[12] = '\0';
3537 len = strlen(logmsg);
3541 nl = strchr(logmsg, '\n');
3544 printf("%s -> %s: %s\n", old_id_str,
3545 new_id_str ? new_id_str : "no-op change", logmsg);
3553 static const struct got_error *
3554 rebase_progress(void *arg, unsigned char status, const char *path)
3556 unsigned char *rebase_status = arg;
3558 while (path[0] == '/')
3560 printf("%c %s\n", status, path);
3562 if (*rebase_status == GOT_STATUS_CONFLICT)
3564 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3565 *rebase_status = status;
3569 static const struct got_error *
3570 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3571 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3572 struct got_repository *repo)
3574 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3575 return got_worktree_rebase_complete(worktree,
3576 new_base_branch, tmp_branch, branch, repo);
3579 static const struct got_error *
3580 rebase_commit(struct got_pathlist_head *merged_paths,
3581 struct got_worktree *worktree, struct got_reference *tmp_branch,
3582 struct got_object_id *commit_id, struct got_repository *repo)
3584 const struct got_error *error;
3585 struct got_commit_object *commit;
3586 struct got_object_id *new_commit_id;
3588 error = got_object_open_as_commit(&commit, repo, commit_id);
3592 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3593 worktree, tmp_branch, commit, commit_id, repo);
3595 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3597 error = show_rebase_progress(commit, commit_id, NULL);
3599 error = show_rebase_progress(commit, commit_id, new_commit_id);
3600 free(new_commit_id);
3603 got_object_commit_close(commit);
3607 struct check_path_prefix_arg {
3608 const char *path_prefix;
3612 static const struct got_error *
3613 check_path_prefix(void *arg, struct got_blob_object *blob1,
3614 struct got_blob_object *blob2, struct got_object_id *id1,
3615 struct got_object_id *id2, const char *path1, const char *path2,
3616 struct got_repository *repo)
3618 struct check_path_prefix_arg *a = arg;
3620 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3621 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3622 return got_error(GOT_ERR_REBASE_PATH);
3627 static const struct got_error *
3628 rebase_check_path_prefix(struct got_object_id *parent_id,
3629 struct got_object_id *commit_id, const char *path_prefix,
3630 struct got_repository *repo)
3632 const struct got_error *err;
3633 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3634 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3635 struct check_path_prefix_arg cpp_arg;
3637 if (got_path_is_root_dir(path_prefix))
3640 err = got_object_open_as_commit(&commit, repo, commit_id);
3644 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3648 err = got_object_open_as_tree(&tree1, repo,
3649 got_object_commit_get_tree_id(parent_commit));
3653 err = got_object_open_as_tree(&tree2, repo,
3654 got_object_commit_get_tree_id(commit));
3658 cpp_arg.path_prefix = path_prefix;
3659 cpp_arg.len = strlen(path_prefix);
3660 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3664 got_object_tree_close(tree1);
3666 got_object_tree_close(tree2);
3668 got_object_commit_close(commit);
3670 got_object_commit_close(parent_commit);
3674 static const struct got_error *
3675 cmd_rebase(int argc, char *argv[])
3677 const struct got_error *error = NULL;
3678 struct got_worktree *worktree = NULL;
3679 struct got_repository *repo = NULL;
3681 struct got_reference *branch = NULL;
3682 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3683 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3684 struct got_object_id *resume_commit_id = NULL;
3685 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3686 struct got_commit_graph *graph = NULL;
3687 struct got_commit_object *commit = NULL;
3688 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3690 struct got_object_id_queue commits;
3691 struct got_pathlist_head merged_paths;
3692 const struct got_object_id_queue *parent_ids;
3693 struct got_object_qid *qid, *pid;
3695 SIMPLEQ_INIT(&commits);
3696 TAILQ_INIT(&merged_paths);
3698 while ((ch = getopt(argc, argv, "ac")) != -1) {
3704 continue_rebase = 1;
3716 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3717 "unveil", NULL) == -1)
3720 if (abort_rebase && continue_rebase)
3722 else if (abort_rebase || continue_rebase) {
3725 } else if (argc != 1)
3728 cwd = getcwd(NULL, 0);
3730 error = got_error_from_errno("getcwd");
3733 error = got_worktree_open(&worktree, cwd);
3737 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3741 error = apply_unveil(got_repo_get_path(repo), 0,
3742 got_worktree_get_root_path(worktree), 0);
3746 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3750 if (rebase_in_progress && abort_rebase) {
3752 error = got_worktree_rebase_continue(&resume_commit_id,
3753 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3756 printf("Switching work tree to %s\n",
3757 got_ref_get_symref_target(new_base_branch));
3758 error = got_worktree_rebase_abort(worktree, repo,
3759 new_base_branch, update_progress, &did_something);
3762 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3763 goto done; /* nothing else to do */
3764 } else if (abort_rebase) {
3765 error = got_error(GOT_ERR_NOT_REBASING);
3769 if (continue_rebase) {
3770 error = got_worktree_rebase_continue(&resume_commit_id,
3771 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3775 error = rebase_commit(NULL, worktree, tmp_branch,
3776 resume_commit_id, repo);
3780 yca_id = got_object_id_dup(resume_commit_id);
3781 if (yca_id == NULL) {
3782 error = got_error_from_errno("got_object_id_dup");
3786 error = got_ref_open(&branch, repo, argv[0], 0);
3790 error = check_same_branch(
3791 got_worktree_get_base_commit_id(worktree), branch, repo);
3793 if (error->code != GOT_ERR_ANCESTRY)
3797 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3798 "specified branch resolves to a commit which "
3799 "is already contained in work tree's branch");
3804 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3808 if (!continue_rebase) {
3809 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3810 got_worktree_get_base_commit_id(worktree),
3811 branch_head_commit_id, repo);
3814 if (yca_id == NULL) {
3815 error = got_error_msg(GOT_ERR_ANCESTRY,
3816 "specified branch shares no common ancestry "
3817 "with work tree's branch");
3821 error = got_worktree_rebase_prepare(&new_base_branch,
3822 &tmp_branch, worktree, branch, repo);
3827 commit_id = branch_head_commit_id;
3828 error = got_object_open_as_commit(&commit, repo, commit_id);
3832 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3835 parent_ids = got_object_commit_get_parent_ids(commit);
3836 pid = SIMPLEQ_FIRST(parent_ids);
3837 error = got_commit_graph_iter_start(graph, pid->id, repo);
3838 got_object_commit_close(commit);
3842 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3843 error = got_commit_graph_iter_next(&parent_id, graph);
3845 if (error->code == GOT_ERR_ITER_COMPLETED) {
3846 error = got_error_msg(GOT_ERR_ANCESTRY,
3847 "ran out of commits to rebase before "
3848 "youngest common ancestor commit has "
3851 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3853 error = got_commit_graph_fetch_commits(graph, 1, repo);
3857 error = rebase_check_path_prefix(parent_id, commit_id,
3858 got_worktree_get_path_prefix(worktree), repo);
3862 error = got_object_qid_alloc(&qid, commit_id);
3865 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3866 commit_id = parent_id;
3869 got_commit_graph_close(graph);
3872 if (SIMPLEQ_EMPTY(&commits)) {
3873 if (continue_rebase)
3874 error = rebase_complete(worktree, branch,
3875 new_base_branch, tmp_branch, repo);
3877 error = got_error(GOT_ERR_EMPTY_REBASE);
3882 SIMPLEQ_FOREACH(qid, &commits, entry) {
3883 commit_id = qid->id;
3884 parent_id = pid ? pid->id : yca_id;
3887 error = got_worktree_rebase_merge_files(&merged_paths,
3888 worktree, parent_id, commit_id, repo, rebase_progress,
3889 &rebase_status, check_cancelled, NULL);
3893 if (rebase_status == GOT_STATUS_CONFLICT) {
3894 got_worktree_rebase_pathlist_free(&merged_paths);
3898 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3900 got_worktree_rebase_pathlist_free(&merged_paths);
3905 if (rebase_status == GOT_STATUS_CONFLICT) {
3906 error = got_worktree_rebase_postpone(worktree);
3909 error = got_error_msg(GOT_ERR_CONFLICTS,
3910 "conflicts must be resolved before rebasing can continue");
3912 error = rebase_complete(worktree, branch, new_base_branch,
3915 got_object_id_queue_free(&commits);
3916 free(branch_head_commit_id);
3917 free(resume_commit_id);
3920 got_commit_graph_close(graph);
3922 got_object_commit_close(commit);
3924 got_ref_close(branch);
3925 if (new_base_branch)
3926 got_ref_close(new_base_branch);
3928 got_ref_close(tmp_branch);
3930 got_worktree_close(worktree);
3932 got_repo_close(repo);