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) {
752 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
754 err = got_commit_graph_fetch_commits(graph, 1,
761 if (got_object_id_cmp(id, commit_id) == 0) {
769 got_commit_graph_close(graph);
770 free(head_commit_id);
771 if (!err && !is_same_branch)
772 err = got_error(GOT_ERR_ANCESTRY);
776 static const struct got_error *
777 cmd_checkout(int argc, char *argv[])
779 const struct got_error *error = NULL;
780 struct got_repository *repo = NULL;
781 struct got_reference *head_ref = NULL;
782 struct got_worktree *worktree = NULL;
783 char *repo_path = NULL;
784 char *worktree_path = NULL;
785 const char *path_prefix = "";
786 const char *branch_name = GOT_REF_HEAD;
787 char *commit_id_str = NULL;
788 int ch, same_path_prefix;
790 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
793 branch_name = optarg;
796 commit_id_str = strdup(optarg);
797 if (commit_id_str == NULL)
798 return got_error_from_errno("strdup");
801 path_prefix = optarg;
813 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
814 "unveil", NULL) == -1)
818 char *cwd, *base, *dotgit;
819 repo_path = realpath(argv[0], NULL);
820 if (repo_path == NULL)
821 return got_error_from_errno2("realpath", argv[0]);
822 cwd = getcwd(NULL, 0);
824 error = got_error_from_errno("getcwd");
827 if (path_prefix[0]) {
828 base = basename(path_prefix);
830 error = got_error_from_errno2("basename",
835 base = basename(repo_path);
837 error = got_error_from_errno2("basename",
842 dotgit = strstr(base, ".git");
845 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
846 error = got_error_from_errno("asprintf");
851 } else if (argc == 2) {
852 repo_path = realpath(argv[0], NULL);
853 if (repo_path == NULL) {
854 error = got_error_from_errno2("realpath", argv[0]);
857 worktree_path = realpath(argv[1], NULL);
858 if (worktree_path == NULL) {
859 error = got_error_from_errno2("realpath", argv[1]);
865 got_path_strip_trailing_slashes(repo_path);
866 got_path_strip_trailing_slashes(worktree_path);
868 error = got_repo_open(&repo, repo_path);
872 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
876 error = got_ref_open(&head_ref, repo, branch_name, 0);
880 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
881 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
884 error = got_worktree_open(&worktree, worktree_path);
888 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
892 if (!same_path_prefix) {
893 error = got_error(GOT_ERR_PATH_PREFIX);
898 struct got_object_id *commit_id;
899 error = got_repo_match_object_id_prefix(&commit_id,
900 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
903 error = check_linear_ancestry(commit_id,
904 got_worktree_get_base_commit_id(worktree), repo);
909 error = check_same_branch(commit_id, head_ref, repo);
912 error = got_worktree_set_base_commit_id(worktree, repo,
919 error = got_worktree_checkout_files(worktree, "", repo,
920 checkout_progress, worktree_path, check_cancelled, NULL);
924 printf("Now shut up and hack\n");
936 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
941 static const struct got_error *
942 update_progress(void *arg, unsigned char status, const char *path)
944 int *did_something = arg;
946 if (status == GOT_STATUS_EXISTS)
951 /* Base commit bump happens silently. */
952 if (status == GOT_STATUS_BUMP_BASE)
955 while (path[0] == '/')
957 printf("%c %s\n", status, path);
961 static const struct got_error *
962 switch_head_ref(struct got_reference *head_ref,
963 struct got_object_id *commit_id, struct got_worktree *worktree,
964 struct got_repository *repo)
966 const struct got_error *err = NULL;
968 int ref_has_moved = 0;
970 /* Trivial case: switching between two different references. */
971 if (strcmp(got_ref_get_name(head_ref),
972 got_worktree_get_head_ref_name(worktree)) != 0) {
973 printf("Switching work tree from %s to %s\n",
974 got_worktree_get_head_ref_name(worktree),
975 got_ref_get_name(head_ref));
976 return got_worktree_set_head_ref(worktree, head_ref);
979 err = check_linear_ancestry(commit_id,
980 got_worktree_get_base_commit_id(worktree), repo);
982 if (err->code != GOT_ERR_ANCESTRY)
989 /* Switching to a rebased branch with the same reference name. */
990 err = got_object_id_str(&base_id_str,
991 got_worktree_get_base_commit_id(worktree));
994 printf("Reference %s now points at a different branch\n",
995 got_worktree_get_head_ref_name(worktree));
996 printf("Switching work tree from %s to %s\n", base_id_str,
997 got_worktree_get_head_ref_name(worktree));
1001 static const struct got_error *
1002 cmd_update(int argc, char *argv[])
1004 const struct got_error *error = NULL;
1005 struct got_repository *repo = NULL;
1006 struct got_worktree *worktree = NULL;
1007 char *worktree_path = NULL, *path = NULL;
1008 struct got_object_id *commit_id = NULL;
1009 char *commit_id_str = NULL;
1010 const char *branch_name = NULL;
1011 struct got_reference *head_ref = NULL;
1012 int ch, did_something = 0, rebase_in_progress;
1014 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1017 branch_name = optarg;
1020 commit_id_str = strdup(optarg);
1021 if (commit_id_str == NULL)
1022 return got_error_from_errno("strdup");
1034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1035 "unveil", NULL) == -1)
1038 worktree_path = getcwd(NULL, 0);
1039 if (worktree_path == NULL) {
1040 error = got_error_from_errno("getcwd");
1043 error = got_worktree_open(&worktree, worktree_path);
1047 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1050 if (rebase_in_progress) {
1051 error = got_error(GOT_ERR_REBASING);
1058 error = got_error_from_errno("strdup");
1061 } else if (argc == 1) {
1062 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1072 error = apply_unveil(got_repo_get_path(repo), 0,
1073 got_worktree_get_root_path(worktree), 0);
1077 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1078 got_worktree_get_head_ref_name(worktree), 0);
1081 if (commit_id_str == NULL) {
1082 error = got_ref_resolve(&commit_id, repo, head_ref);
1085 error = got_object_id_str(&commit_id_str, commit_id);
1089 error = got_repo_match_object_id_prefix(&commit_id,
1090 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1093 free(commit_id_str);
1094 error = got_object_id_str(&commit_id_str, commit_id);
1100 struct got_object_id *head_commit_id;
1101 if (strlen(path) != 0) {
1102 fprintf(stderr, "%s: switching between branches "
1103 "requires that the entire work tree "
1104 "gets updated, not just '%s'\n",
1105 getprogname(), path);
1106 error = got_error(GOT_ERR_BAD_PATH);
1109 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1112 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1113 free(head_commit_id);
1116 error = check_same_branch(commit_id, head_ref, repo);
1119 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1123 error = check_linear_ancestry(commit_id,
1124 got_worktree_get_base_commit_id(worktree), repo);
1125 if (error != NULL) {
1126 if (error->code == GOT_ERR_ANCESTRY)
1127 error = got_error(GOT_ERR_BRANCH_MOVED);
1130 error = check_same_branch(commit_id, head_ref, repo);
1135 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1137 error = got_worktree_set_base_commit_id(worktree, repo,
1143 error = got_worktree_checkout_files(worktree, path, repo,
1144 update_progress, &did_something, check_cancelled, NULL);
1149 printf("Updated to commit %s\n", commit_id_str);
1151 printf("Already up-to-date\n");
1153 free(worktree_path);
1156 free(commit_id_str);
1160 static const struct got_error *
1161 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1162 int diff_context, struct got_repository *repo)
1164 const struct got_error *err = NULL;
1165 struct got_tree_object *tree1 = NULL, *tree2;
1166 struct got_object_qid *qid;
1167 char *id_str1 = NULL, *id_str2;
1168 struct got_diff_blob_output_unidiff_arg arg;
1170 err = got_object_open_as_tree(&tree2, repo,
1171 got_object_commit_get_tree_id(commit));
1175 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1177 struct got_commit_object *pcommit;
1179 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1183 err = got_object_open_as_tree(&tree1, repo,
1184 got_object_commit_get_tree_id(pcommit));
1185 got_object_commit_close(pcommit);
1189 err = got_object_id_str(&id_str1, qid->id);
1194 err = got_object_id_str(&id_str2, id);
1198 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1199 arg.diff_context = diff_context;
1200 arg.outfile = stdout;
1201 err = got_diff_tree(tree1, tree2, "", "", repo,
1202 got_diff_blob_output_unidiff, &arg);
1205 got_object_tree_close(tree1);
1206 got_object_tree_close(tree2);
1213 get_datestr(time_t *time, char *datebuf)
1215 char *p, *s = ctime_r(time, datebuf);
1216 p = strchr(s, '\n');
1222 static const struct got_error *
1223 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1224 struct got_repository *repo, int show_patch, int diff_context,
1225 struct got_reflist_head *refs)
1227 const struct got_error *err = NULL;
1228 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1230 time_t committer_time;
1231 const char *author, *committer;
1232 char *refs_str = NULL;
1233 struct got_reflist_entry *re;
1235 SIMPLEQ_FOREACH(re, refs, entry) {
1238 if (got_object_id_cmp(re->id, id) != 0)
1240 name = got_ref_get_name(re->ref);
1241 if (strcmp(name, GOT_REF_HEAD) == 0)
1243 if (strncmp(name, "refs/", 5) == 0)
1245 if (strncmp(name, "got/", 4) == 0)
1247 if (strncmp(name, "heads/", 6) == 0)
1249 if (strncmp(name, "remotes/", 8) == 0)
1252 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1254 err = got_error_from_errno("asprintf");
1260 err = got_object_id_str(&id_str, id);
1264 printf("-----------------------------------------------\n");
1265 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1266 refs_str ? refs_str : "", refs_str ? ")" : "");
1271 printf("from: %s\n", got_object_commit_get_author(commit));
1272 committer_time = got_object_commit_get_committer_time(commit);
1273 datestr = get_datestr(&committer_time, datebuf);
1274 printf("date: %s UTC\n", datestr);
1275 author = got_object_commit_get_author(commit);
1276 committer = got_object_commit_get_committer(commit);
1277 if (strcmp(author, committer) != 0)
1278 printf("via: %s\n", committer);
1279 if (got_object_commit_get_nparents(commit) > 1) {
1280 const struct got_object_id_queue *parent_ids;
1281 struct got_object_qid *qid;
1283 parent_ids = got_object_commit_get_parent_ids(commit);
1284 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1285 err = got_object_id_str(&id_str, qid->id);
1288 printf("parent %d: %s\n", n++, id_str);
1293 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1294 if (logmsg0 == NULL)
1295 return got_error_from_errno("strdup");
1299 line = strsep(&logmsg, "\n");
1301 printf(" %s\n", line);
1306 err = print_patch(commit, id, diff_context, repo);
1311 if (fflush(stdout) != 0 && err == NULL)
1312 err = got_error_from_errno("fflush");
1316 static const struct got_error *
1317 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1318 char *path, int show_patch, int diff_context, int limit,
1319 int first_parent_traversal, struct got_reflist_head *refs)
1321 const struct got_error *err;
1322 struct got_commit_graph *graph;
1324 err = got_commit_graph_open(&graph, root_id, path,
1325 first_parent_traversal, repo);
1328 err = got_commit_graph_iter_start(graph, root_id, repo);
1332 struct got_commit_object *commit;
1333 struct got_object_id *id;
1335 if (sigint_received || sigpipe_received)
1338 err = got_commit_graph_iter_next(&id, graph);
1340 if (err->code == GOT_ERR_ITER_COMPLETED) {
1344 if (err->code != GOT_ERR_ITER_NEED_MORE)
1346 err = got_commit_graph_fetch_commits(graph, 1, repo);
1355 err = got_object_open_as_commit(&commit, repo, id);
1358 err = print_commit(commit, id, repo, show_patch, diff_context,
1360 got_object_commit_close(commit);
1361 if (err || (limit && --limit == 0))
1365 got_commit_graph_close(graph);
1372 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1373 "[-r repository-path] [path]\n", getprogname());
1377 static const struct got_error *
1378 cmd_log(int argc, char *argv[])
1380 const struct got_error *error;
1381 struct got_repository *repo = NULL;
1382 struct got_worktree *worktree = NULL;
1383 struct got_commit_object *commit = NULL;
1384 struct got_object_id *id = NULL;
1385 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1386 char *start_commit = NULL;
1387 int diff_context = 3, ch;
1388 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1390 struct got_reflist_head refs;
1392 SIMPLEQ_INIT(&refs);
1395 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1401 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1407 start_commit = optarg;
1410 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1413 err(1, "-C option %s", errstr);
1416 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1418 err(1, "-l option %s", errstr);
1421 first_parent_traversal = 1;
1424 repo_path = realpath(optarg, NULL);
1425 if (repo_path == NULL)
1426 err(1, "-r option");
1427 got_path_strip_trailing_slashes(repo_path);
1438 cwd = getcwd(NULL, 0);
1440 error = got_error_from_errno("getcwd");
1444 error = got_worktree_open(&worktree, cwd);
1445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1452 error = got_error_from_errno("strdup");
1455 } else if (argc == 1) {
1457 error = got_worktree_resolve_path(&path, worktree,
1462 path = strdup(argv[0]);
1464 error = got_error_from_errno("strdup");
1471 if (repo_path == NULL) {
1472 repo_path = worktree ?
1473 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1475 if (repo_path == NULL) {
1476 error = got_error_from_errno("strdup");
1480 error = got_repo_open(&repo, repo_path);
1484 error = apply_unveil(got_repo_get_path(repo), 1,
1485 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1489 if (start_commit == NULL) {
1490 struct got_reference *head_ref;
1491 error = got_ref_open(&head_ref, repo,
1492 worktree ? got_worktree_get_head_ref_name(worktree)
1496 error = got_ref_resolve(&id, repo, head_ref);
1497 got_ref_close(head_ref);
1500 error = got_object_open_as_commit(&commit, repo, id);
1502 struct got_reference *ref;
1503 error = got_ref_open(&ref, repo, start_commit, 0);
1504 if (error == NULL) {
1506 error = got_ref_resolve(&id, repo, ref);
1510 error = got_object_get_type(&obj_type, repo, id);
1513 if (obj_type == GOT_OBJ_TYPE_TAG) {
1514 struct got_tag_object *tag;
1515 error = got_object_open_as_tag(&tag, repo, id);
1518 if (got_object_tag_get_object_type(tag) !=
1519 GOT_OBJ_TYPE_COMMIT) {
1520 got_object_tag_close(tag);
1521 error = got_error(GOT_ERR_OBJ_TYPE);
1525 id = got_object_id_dup(
1526 got_object_tag_get_object_id(tag));
1528 error = got_error_from_errno(
1529 "got_object_id_dup");
1530 got_object_tag_close(tag);
1533 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1534 error = got_error(GOT_ERR_OBJ_TYPE);
1537 error = got_object_open_as_commit(&commit, repo, id);
1541 if (commit == NULL) {
1542 error = got_repo_match_object_id_prefix(&id,
1543 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1551 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1556 path = in_repo_path;
1559 error = got_ref_list(&refs, repo);
1563 error = print_commits(id, repo, path, show_patch,
1564 diff_context, limit, first_parent_traversal, &refs);
1571 got_worktree_close(worktree);
1573 const struct got_error *repo_error;
1574 repo_error = got_repo_close(repo);
1578 got_ref_list_free(&refs);
1585 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1586 "[object1 object2 | path]\n", getprogname());
1590 struct print_diff_arg {
1591 struct got_repository *repo;
1592 struct got_worktree *worktree;
1598 static const struct got_error *
1599 print_diff(void *arg, unsigned char status, const char *path,
1600 struct got_object_id *blob_id, struct got_object_id *commit_id)
1602 struct print_diff_arg *a = arg;
1603 const struct got_error *err = NULL;
1604 struct got_blob_object *blob1 = NULL;
1606 char *abspath = NULL;
1609 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1610 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1613 if (!a->header_shown) {
1614 printf("diff %s %s\n", a->id_str,
1615 got_worktree_get_root_path(a->worktree));
1616 a->header_shown = 1;
1619 if (status != GOT_STATUS_ADD) {
1620 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1626 if (status != GOT_STATUS_DELETE) {
1627 if (asprintf(&abspath, "%s/%s",
1628 got_worktree_get_root_path(a->worktree), path) == -1) {
1629 err = got_error_from_errno("asprintf");
1633 f2 = fopen(abspath, "r");
1635 err = got_error_from_errno2("fopen", abspath);
1638 if (lstat(abspath, &sb) == -1) {
1639 err = got_error_from_errno2("lstat", abspath);
1645 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1649 got_object_blob_close(blob1);
1650 if (f2 && fclose(f2) != 0 && err == NULL)
1651 err = got_error_from_errno("fclose");
1656 static const struct got_error *
1657 cmd_diff(int argc, char *argv[])
1659 const struct got_error *error;
1660 struct got_repository *repo = NULL;
1661 struct got_worktree *worktree = NULL;
1662 char *cwd = NULL, *repo_path = NULL;
1663 struct got_object_id *id1 = NULL, *id2 = NULL;
1664 const char *id_str1 = NULL, *id_str2 = NULL;
1665 char *label1 = NULL, *label2 = NULL;
1667 int diff_context = 3, ch;
1672 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1677 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1680 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1682 err(1, "-C option %s", errstr);
1685 repo_path = realpath(optarg, NULL);
1686 if (repo_path == NULL)
1687 err(1, "-r option");
1688 got_path_strip_trailing_slashes(repo_path);
1699 cwd = getcwd(NULL, 0);
1701 error = got_error_from_errno("getcwd");
1704 error = got_worktree_open(&worktree, cwd);
1705 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1708 if (worktree == NULL) {
1709 error = got_error(GOT_ERR_NOT_WORKTREE);
1714 "-r option can't be used when diffing a work tree");
1715 repo_path = strdup(got_worktree_get_repo_path(worktree));
1716 if (repo_path == NULL) {
1717 error = got_error_from_errno("strdup");
1721 error = got_worktree_resolve_path(&path, worktree,
1728 error = got_error_from_errno("strdup");
1732 } else if (argc == 2) {
1735 if (worktree && repo_path == NULL) {
1737 strdup(got_worktree_get_repo_path(worktree));
1738 if (repo_path == NULL) {
1739 error = got_error_from_errno("strdup");
1746 if (repo_path == NULL) {
1747 repo_path = getcwd(NULL, 0);
1748 if (repo_path == NULL)
1749 return got_error_from_errno("getcwd");
1752 error = got_repo_open(&repo, repo_path);
1757 error = apply_unveil(got_repo_get_path(repo), 1,
1758 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1763 struct print_diff_arg arg;
1765 error = got_object_id_str(&id_str,
1766 got_worktree_get_base_commit_id(worktree));
1770 arg.worktree = worktree;
1771 arg.diff_context = diff_context;
1772 arg.id_str = id_str;
1773 arg.header_shown = 0;
1775 error = got_worktree_status(worktree, path, repo, print_diff,
1776 &arg, check_cancelled, NULL);
1781 error = got_repo_match_object_id_prefix(&id1, id_str1,
1782 GOT_OBJ_TYPE_ANY, repo);
1784 struct got_reference *ref;
1785 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1787 error = got_ref_open(&ref, repo, id_str1, 0);
1790 label1 = strdup(got_ref_get_name(ref));
1791 if (label1 == NULL) {
1792 error = got_error_from_errno("strdup");
1795 error = got_ref_resolve(&id1, repo, ref);
1800 error = got_object_id_str(&label1, id1);
1801 if (label1 == NULL) {
1802 error = got_error_from_errno("strdup");
1807 error = got_repo_match_object_id_prefix(&id2, id_str2,
1808 GOT_OBJ_TYPE_ANY, repo);
1810 struct got_reference *ref;
1811 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1813 error = got_ref_open(&ref, repo, id_str2, 0);
1816 label2 = strdup(got_ref_get_name(ref));
1817 if (label2 == NULL) {
1818 error = got_error_from_errno("strdup");
1821 error = got_ref_resolve(&id2, repo, ref);
1826 error = got_object_id_str(&label2, id2);
1827 if (label2 == NULL) {
1828 error = got_error_from_errno("strdup");
1833 error = got_object_get_type(&type1, repo, id1);
1837 error = got_object_get_type(&type2, repo, id2);
1841 if (type1 != type2) {
1842 error = got_error(GOT_ERR_OBJ_TYPE);
1847 case GOT_OBJ_TYPE_BLOB:
1848 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1849 diff_context, repo, stdout);
1851 case GOT_OBJ_TYPE_TREE:
1852 error = got_diff_objects_as_trees(id1, id2, "", "",
1853 diff_context, repo, stdout);
1855 case GOT_OBJ_TYPE_COMMIT:
1856 printf("diff %s %s\n", label1, label2);
1857 error = got_diff_objects_as_commits(id1, id2, diff_context,
1861 error = got_error(GOT_ERR_OBJ_TYPE);
1871 got_worktree_close(worktree);
1873 const struct got_error *repo_error;
1874 repo_error = got_repo_close(repo);
1885 "usage: %s blame [-c commit] [-r repository-path] path\n",
1890 static const struct got_error *
1891 cmd_blame(int argc, char *argv[])
1893 const struct got_error *error;
1894 struct got_repository *repo = NULL;
1895 struct got_worktree *worktree = NULL;
1896 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1897 struct got_object_id *commit_id = NULL;
1898 char *commit_id_str = NULL;
1902 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1907 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1910 commit_id_str = optarg;
1913 repo_path = realpath(optarg, NULL);
1914 if (repo_path == NULL)
1915 err(1, "-r option");
1916 got_path_strip_trailing_slashes(repo_path);
1932 cwd = getcwd(NULL, 0);
1934 error = got_error_from_errno("getcwd");
1937 if (repo_path == NULL) {
1938 error = got_worktree_open(&worktree, cwd);
1939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1945 strdup(got_worktree_get_repo_path(worktree));
1946 if (repo_path == NULL)
1947 error = got_error_from_errno("strdup");
1951 repo_path = strdup(cwd);
1952 if (repo_path == NULL) {
1953 error = got_error_from_errno("strdup");
1959 error = got_repo_open(&repo, repo_path);
1963 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1968 const char *prefix = got_worktree_get_path_prefix(worktree);
1969 char *p, *worktree_subdir = cwd +
1970 strlen(got_worktree_get_root_path(worktree));
1971 if (asprintf(&p, "%s%s%s%s%s",
1972 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1973 worktree_subdir, worktree_subdir[0] ? "/" : "",
1975 error = got_error_from_errno("asprintf");
1978 error = got_repo_map_path(&in_repo_path, repo, p, 0);
1981 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1986 if (commit_id_str == NULL) {
1987 struct got_reference *head_ref;
1988 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1991 error = got_ref_resolve(&commit_id, repo, head_ref);
1992 got_ref_close(head_ref);
1996 error = got_repo_match_object_id_prefix(&commit_id,
1997 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2002 error = got_blame(in_repo_path, commit_id, repo, stdout);
2009 got_worktree_close(worktree);
2011 const struct got_error *repo_error;
2012 repo_error = got_repo_close(repo);
2023 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2029 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2030 const char *root_path)
2032 int is_root_path = (strcmp(path, root_path) == 0);
2034 path += strlen(root_path);
2035 while (path[0] == '/')
2038 printf("%s%s%s%s%s\n", id ? id : "", path,
2039 is_root_path ? "" : "/", te->name,
2040 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2043 static const struct got_error *
2044 print_tree(const char *path, struct got_object_id *commit_id,
2045 int show_ids, int recurse, const char *root_path,
2046 struct got_repository *repo)
2048 const struct got_error *err = NULL;
2049 struct got_object_id *tree_id = NULL;
2050 struct got_tree_object *tree = NULL;
2051 const struct got_tree_entries *entries;
2052 struct got_tree_entry *te;
2054 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2058 err = got_object_open_as_tree(&tree, repo, tree_id);
2061 entries = got_object_tree_get_entries(tree);
2062 te = SIMPLEQ_FIRST(&entries->head);
2066 if (sigint_received || sigpipe_received)
2071 err = got_object_id_str(&id_str, te->id);
2074 if (asprintf(&id, "%s ", id_str) == -1) {
2075 err = got_error_from_errno("asprintf");
2081 print_entry(te, id, path, root_path);
2084 if (recurse && S_ISDIR(te->mode)) {
2086 if (asprintf(&child_path, "%s%s%s", path,
2087 path[0] == '/' && path[1] == '\0' ? "" : "/",
2089 err = got_error_from_errno("asprintf");
2092 err = print_tree(child_path, commit_id, show_ids, 1,
2099 te = SIMPLEQ_NEXT(te, entry);
2103 got_object_tree_close(tree);
2108 static const struct got_error *
2109 cmd_tree(int argc, char *argv[])
2111 const struct got_error *error;
2112 struct got_repository *repo = NULL;
2113 struct got_worktree *worktree = NULL;
2115 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2116 struct got_object_id *commit_id = NULL;
2117 char *commit_id_str = NULL;
2118 int show_ids = 0, recurse = 0;
2122 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2127 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2130 commit_id_str = optarg;
2133 repo_path = realpath(optarg, NULL);
2134 if (repo_path == NULL)
2135 err(1, "-r option");
2136 got_path_strip_trailing_slashes(repo_path);
2160 cwd = getcwd(NULL, 0);
2162 error = got_error_from_errno("getcwd");
2165 if (repo_path == NULL) {
2166 error = got_worktree_open(&worktree, cwd);
2167 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2173 strdup(got_worktree_get_repo_path(worktree));
2174 if (repo_path == NULL)
2175 error = got_error_from_errno("strdup");
2179 repo_path = strdup(cwd);
2180 if (repo_path == NULL) {
2181 error = got_error_from_errno("strdup");
2187 error = got_repo_open(&repo, repo_path);
2191 error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2197 char *p, *worktree_subdir = cwd +
2198 strlen(got_worktree_get_root_path(worktree));
2199 if (asprintf(&p, "%s/%s",
2200 got_worktree_get_path_prefix(worktree),
2201 worktree_subdir) == -1) {
2202 error = got_error_from_errno("asprintf");
2205 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2212 if (in_repo_path == NULL) {
2213 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2218 if (commit_id_str == NULL) {
2219 struct got_reference *head_ref;
2220 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2223 error = got_ref_resolve(&commit_id, repo, head_ref);
2224 got_ref_close(head_ref);
2228 error = got_repo_match_object_id_prefix(&commit_id,
2229 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2234 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2235 in_repo_path, repo);
2242 got_worktree_close(worktree);
2244 const struct got_error *repo_error;
2245 repo_error = got_repo_close(repo);
2255 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2259 static const struct got_error *
2260 print_status(void *arg, unsigned char status, const char *path,
2261 struct got_object_id *blob_id, struct got_object_id *commit_id)
2263 printf("%c %s\n", status, path);
2267 static const struct got_error *
2268 cmd_status(int argc, char *argv[])
2270 const struct got_error *error = NULL;
2271 struct got_repository *repo = NULL;
2272 struct got_worktree *worktree = NULL;
2273 char *cwd = NULL, *path = NULL;
2276 while ((ch = getopt(argc, argv, "")) != -1) {
2288 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2292 cwd = getcwd(NULL, 0);
2294 error = got_error_from_errno("getcwd");
2298 error = got_worktree_open(&worktree, cwd);
2305 error = got_error_from_errno("strdup");
2308 } else if (argc == 1) {
2309 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2319 error = apply_unveil(got_repo_get_path(repo), 1,
2320 got_worktree_get_root_path(worktree), 0);
2324 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2325 check_cancelled, NULL);
2336 "usage: %s ref [-r repository] -l | -d name | name target\n",
2341 static const struct got_error *
2342 list_refs(struct got_repository *repo)
2344 static const struct got_error *err = NULL;
2345 struct got_reflist_head refs;
2346 struct got_reflist_entry *re;
2348 SIMPLEQ_INIT(&refs);
2349 err = got_ref_list(&refs, repo);
2353 SIMPLEQ_FOREACH(re, &refs, entry) {
2355 refstr = got_ref_to_str(re->ref);
2357 return got_error_from_errno("got_ref_to_str");
2358 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2362 got_ref_list_free(&refs);
2366 static const struct got_error *
2367 delete_ref(struct got_repository *repo, const char *refname)
2369 const struct got_error *err = NULL;
2370 struct got_reference *ref;
2372 err = got_ref_open(&ref, repo, refname, 0);
2376 err = got_ref_delete(ref, repo);
2381 static const struct got_error *
2382 add_ref(struct got_repository *repo, const char *refname, const char *target)
2384 const struct got_error *err = NULL;
2385 struct got_object_id *id;
2386 struct got_reference *ref = NULL;
2389 * Don't let the user create a reference named '-'.
2390 * While technically a valid reference name, this case is usually
2391 * an unintended typo.
2393 if (refname[0] == '-' && refname[1] == '\0')
2394 return got_error(GOT_ERR_BAD_REF_NAME);
2396 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2399 struct got_reference *target_ref;
2401 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2403 err = got_ref_open(&target_ref, repo, target, 0);
2406 err = got_ref_resolve(&id, repo, target_ref);
2407 got_ref_close(target_ref);
2412 err = got_ref_alloc(&ref, refname, id);
2416 err = got_ref_write(ref, repo);
2424 static const struct got_error *
2425 cmd_ref(int argc, char *argv[])
2427 const struct got_error *error = NULL;
2428 struct got_repository *repo = NULL;
2429 struct got_worktree *worktree = NULL;
2430 char *cwd = NULL, *repo_path = NULL;
2431 int ch, do_list = 0;
2432 const char *delref = NULL;
2434 /* TODO: Add -s option for adding symbolic references. */
2435 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2441 repo_path = realpath(optarg, NULL);
2442 if (repo_path == NULL)
2443 err(1, "-r option");
2444 got_path_strip_trailing_slashes(repo_path);
2455 if (do_list && delref)
2456 errx(1, "-l and -d options are mutually exclusive\n");
2461 if (do_list || delref) {
2464 } else if (argc != 2)
2469 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2473 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2474 "sendfd unveil", NULL) == -1)
2478 cwd = getcwd(NULL, 0);
2480 error = got_error_from_errno("getcwd");
2484 if (repo_path == NULL) {
2485 error = got_worktree_open(&worktree, cwd);
2486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2492 strdup(got_worktree_get_repo_path(worktree));
2493 if (repo_path == NULL)
2494 error = got_error_from_errno("strdup");
2498 repo_path = strdup(cwd);
2499 if (repo_path == NULL) {
2500 error = got_error_from_errno("strdup");
2506 error = got_repo_open(&repo, repo_path);
2510 error = apply_unveil(got_repo_get_path(repo), do_list,
2511 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2516 error = list_refs(repo);
2518 error = delete_ref(repo, delref);
2520 error = add_ref(repo, argv[0], argv[1]);
2523 got_repo_close(repo);
2525 got_worktree_close(worktree);
2535 "usage: %s branch [-r repository] -l | -d name | "
2536 "name [base-branch]\n", getprogname());
2540 static const struct got_error *
2541 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2543 static const struct got_error *err = NULL;
2544 struct got_reflist_head refs;
2545 struct got_reflist_entry *re;
2547 SIMPLEQ_INIT(&refs);
2549 err = got_ref_list(&refs, repo);
2553 SIMPLEQ_FOREACH(re, &refs, entry) {
2554 const char *refname, *marker = " ";
2556 refname = got_ref_get_name(re->ref);
2557 if (strncmp(refname, "refs/heads/", 11) != 0)
2559 if (worktree && strcmp(refname,
2560 got_worktree_get_head_ref_name(worktree)) == 0) {
2561 struct got_object_id *id = NULL;
2562 err = got_ref_resolve(&id, repo, re->ref);
2565 if (got_object_id_cmp(id,
2566 got_worktree_get_base_commit_id(worktree)) == 0)
2573 refstr = got_ref_to_str(re->ref);
2575 return got_error_from_errno("got_ref_to_str");
2576 printf("%s%s: %s\n", marker, refname, refstr);
2580 got_ref_list_free(&refs);
2584 static const struct got_error *
2585 delete_branch(struct got_repository *repo, const char *branch_name)
2587 const struct got_error *err = NULL;
2588 struct got_reference *ref;
2591 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2592 return got_error_from_errno("asprintf");
2594 err = got_ref_open(&ref, repo, refname, 0);
2598 err = got_ref_delete(ref, repo);
2605 static const struct got_error *
2606 add_branch(struct got_repository *repo, const char *branch_name,
2607 const char *base_branch)
2609 const struct got_error *err = NULL;
2610 struct got_object_id *id = NULL;
2611 struct got_reference *ref = NULL;
2612 char *base_refname = NULL, *refname = NULL;
2613 struct got_reference *base_ref;
2616 * Don't let the user create a branch named '-'.
2617 * While technically a valid reference name, this case is usually
2618 * an unintended typo.
2620 if (branch_name[0] == '-' && branch_name[1] == '\0')
2621 return got_error(GOT_ERR_BAD_REF_NAME);
2623 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2624 base_refname = strdup(GOT_REF_HEAD);
2625 if (base_refname == NULL)
2626 return got_error_from_errno("strdup");
2627 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2628 return got_error_from_errno("asprintf");
2630 err = got_ref_open(&base_ref, repo, base_refname, 0);
2633 err = got_ref_resolve(&id, repo, base_ref);
2634 got_ref_close(base_ref);
2638 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2639 err = got_error_from_errno("asprintf");
2643 err = got_ref_open(&ref, repo, refname, 0);
2645 err = got_error(GOT_ERR_BRANCH_EXISTS);
2647 } else if (err->code != GOT_ERR_NOT_REF)
2650 err = got_ref_alloc(&ref, refname, id);
2654 err = got_ref_write(ref, repo);
2664 static const struct got_error *
2665 cmd_branch(int argc, char *argv[])
2667 const struct got_error *error = NULL;
2668 struct got_repository *repo = NULL;
2669 struct got_worktree *worktree = NULL;
2670 char *cwd = NULL, *repo_path = NULL;
2671 int ch, do_list = 0;
2672 const char *delref = NULL;
2674 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2680 repo_path = realpath(optarg, NULL);
2681 if (repo_path == NULL)
2682 err(1, "-r option");
2683 got_path_strip_trailing_slashes(repo_path);
2694 if (do_list && delref)
2695 errx(1, "-l and -d options are mutually exclusive\n");
2700 if (do_list || delref) {
2703 } else if (argc < 1 || argc > 2)
2708 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2712 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2713 "sendfd unveil", NULL) == -1)
2717 cwd = getcwd(NULL, 0);
2719 error = got_error_from_errno("getcwd");
2723 if (repo_path == NULL) {
2724 error = got_worktree_open(&worktree, cwd);
2725 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2731 strdup(got_worktree_get_repo_path(worktree));
2732 if (repo_path == NULL)
2733 error = got_error_from_errno("strdup");
2737 repo_path = strdup(cwd);
2738 if (repo_path == NULL) {
2739 error = got_error_from_errno("strdup");
2745 error = got_repo_open(&repo, repo_path);
2749 error = apply_unveil(got_repo_get_path(repo), do_list,
2750 worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2755 error = list_branches(repo, worktree);
2757 error = delete_branch(repo, delref);
2759 const char *base_branch;
2761 base_branch = worktree ?
2762 got_worktree_get_head_ref_name(worktree) :
2765 base_branch = argv[1];
2766 error = add_branch(repo, argv[0], base_branch);
2770 got_repo_close(repo);
2772 got_worktree_close(worktree);
2781 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2785 static const struct got_error *
2786 cmd_add(int argc, char *argv[])
2788 const struct got_error *error = NULL;
2789 struct got_repository *repo = NULL;
2790 struct got_worktree *worktree = NULL;
2792 struct got_pathlist_head paths;
2793 struct got_pathlist_entry *pe;
2798 while ((ch = getopt(argc, argv, "")) != -1) {
2810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2817 /* make sure each file exists before doing anything halfway */
2818 for (x = 0; x < argc; x++) {
2819 char *path = realpath(argv[x], NULL);
2821 error = got_error_from_errno2("realpath", argv[x]);
2827 cwd = getcwd(NULL, 0);
2829 error = got_error_from_errno("getcwd");
2833 error = got_worktree_open(&worktree, cwd);
2837 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2841 error = apply_unveil(got_repo_get_path(repo), 1,
2842 got_worktree_get_root_path(worktree), 0);
2846 for (x = 0; x < argc; x++) {
2847 char *path = realpath(argv[x], NULL);
2849 error = got_error_from_errno2("realpath", argv[x]);
2853 got_path_strip_trailing_slashes(path);
2854 error = got_pathlist_insert(&pe, &paths, path, NULL);
2860 error = got_worktree_schedule_add(worktree, &paths, print_status,
2864 got_repo_close(repo);
2866 got_worktree_close(worktree);
2867 TAILQ_FOREACH(pe, &paths, entry)
2868 free((char *)pe->path);
2869 got_pathlist_free(&paths);
2877 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2881 static const struct got_error *
2882 cmd_remove(int argc, char *argv[])
2884 const struct got_error *error = NULL;
2885 struct got_worktree *worktree = NULL;
2886 struct got_repository *repo = NULL;
2888 struct got_pathlist_head paths;
2889 struct got_pathlist_entry *pe;
2890 int ch, i, delete_local_mods = 0;
2894 while ((ch = getopt(argc, argv, "f")) != -1) {
2897 delete_local_mods = 1;
2909 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2916 /* make sure each file exists before doing anything halfway */
2917 for (i = 0; i < argc; i++) {
2918 char *path = realpath(argv[i], NULL);
2920 error = got_error_from_errno2("realpath", argv[i]);
2926 cwd = getcwd(NULL, 0);
2928 error = got_error_from_errno("getcwd");
2931 error = got_worktree_open(&worktree, cwd);
2935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2939 error = apply_unveil(got_repo_get_path(repo), 1,
2940 got_worktree_get_root_path(worktree), 0);
2944 for (i = 0; i < argc; i++) {
2945 char *path = realpath(argv[i], NULL);
2947 error = got_error_from_errno2("realpath", argv[i]);
2951 got_path_strip_trailing_slashes(path);
2952 error = got_pathlist_insert(&pe, &paths, path, NULL);
2958 error = got_worktree_schedule_delete(worktree, &paths,
2959 delete_local_mods, print_status, NULL, repo);
2964 got_repo_close(repo);
2966 got_worktree_close(worktree);
2967 TAILQ_FOREACH(pe, &paths, entry)
2968 free((char *)pe->path);
2969 got_pathlist_free(&paths);
2977 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2981 static const struct got_error *
2982 revert_progress(void *arg, unsigned char status, const char *path)
2984 while (path[0] == '/')
2986 printf("%c %s\n", status, path);
2990 static const struct got_error *
2991 cmd_revert(int argc, char *argv[])
2993 const struct got_error *error = NULL;
2994 struct got_worktree *worktree = NULL;
2995 struct got_repository *repo = NULL;
2996 char *cwd = NULL, *path = NULL;
2997 struct got_pathlist_head paths;
2998 struct got_pathlist_entry *pe;
3003 while ((ch = getopt(argc, argv, "")) != -1) {
3015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3016 "unveil", NULL) == -1)
3022 for (i = 0; i < argc; i++) {
3023 char *path = realpath(argv[i], NULL);
3025 error = got_error_from_errno2("realpath", argv[i]);
3029 got_path_strip_trailing_slashes(path);
3030 error = got_pathlist_insert(&pe, &paths, path, NULL);
3037 cwd = getcwd(NULL, 0);
3039 error = got_error_from_errno("getcwd");
3042 error = got_worktree_open(&worktree, cwd);
3046 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3050 error = apply_unveil(got_repo_get_path(repo), 1,
3051 got_worktree_get_root_path(worktree), 0);
3055 error = got_worktree_revert(worktree, &paths,
3056 revert_progress, NULL, repo);
3061 got_repo_close(repo);
3063 got_worktree_close(worktree);
3072 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3076 struct collect_commit_logmsg_arg {
3077 const char *cmdline_log;
3079 const char *worktree_path;
3080 const char *branch_name;
3081 const char *repo_path;
3086 static const struct got_error *
3087 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3090 char *initial_content = NULL;
3091 struct got_pathlist_entry *pe;
3092 const struct got_error *err = NULL;
3093 char *template = NULL;
3094 struct collect_commit_logmsg_arg *a = arg;
3098 /* if a message was specified on the command line, just use it */
3099 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3100 len = strlen(a->cmdline_log) + 1;
3101 *logmsg = malloc(len + 1);
3102 if (*logmsg == NULL)
3103 return got_error_from_errno("malloc");
3104 strlcpy(*logmsg, a->cmdline_log, len);
3108 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3109 return got_error_from_errno("asprintf");
3111 if (asprintf(&initial_content,
3112 "\n# changes to be committed on branch %s:\n",
3113 a->branch_name) == -1)
3114 return got_error_from_errno("asprintf");
3116 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3120 dprintf(fd, initial_content);
3122 TAILQ_FOREACH(pe, commitable_paths, entry) {
3123 struct got_commitable *ct = pe->data;
3124 dprintf(fd, "# %c %s\n",
3125 got_commitable_get_status(ct),
3126 got_commitable_get_path(ct));
3130 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3132 unlink(a->logmsg_path);
3133 free(a->logmsg_path);
3134 free(initial_content);
3137 /* Editor is done; we can now apply unveil(2) */
3139 err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3148 static const struct got_error *
3149 cmd_commit(int argc, char *argv[])
3151 const struct got_error *error = NULL;
3152 struct got_worktree *worktree = NULL;
3153 struct got_repository *repo = NULL;
3154 char *cwd = NULL, *path = NULL, *id_str = NULL;
3155 struct got_object_id *id = NULL;
3156 const char *logmsg = NULL;
3157 const char *got_author = getenv("GOT_AUTHOR");
3158 struct collect_commit_logmsg_arg cl_arg;
3159 char *editor = NULL;
3160 int ch, rebase_in_progress;
3162 while ((ch = getopt(argc, argv, "m:")) != -1) {
3177 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3178 "unveil", NULL) == -1)
3182 path = realpath(argv[0], NULL);
3184 error = got_error_from_errno2("realpath", argv[0]);
3187 got_path_strip_trailing_slashes(path);
3188 } else if (argc != 0)
3191 if (got_author == NULL) {
3192 /* TODO: Look current user up in password database */
3193 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3197 cwd = getcwd(NULL, 0);
3199 error = got_error_from_errno("getcwd");
3202 error = got_worktree_open(&worktree, cwd);
3206 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3209 if (rebase_in_progress) {
3210 error = got_error(GOT_ERR_REBASING);
3214 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3219 * unveil(2) traverses exec(2); if an editor is used we have
3220 * to apply unveil after the log message has been written.
3222 if (logmsg == NULL || strlen(logmsg) == 0)
3223 error = get_editor(&editor);
3225 error = apply_unveil(got_repo_get_path(repo), 0,
3226 got_worktree_get_root_path(worktree), 0);
3230 cl_arg.editor = editor;
3231 cl_arg.cmdline_log = logmsg;
3232 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3233 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3234 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3235 cl_arg.branch_name += 5;
3236 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3237 cl_arg.branch_name += 6;
3238 cl_arg.repo_path = got_repo_get_path(repo);
3239 cl_arg.logmsg_path = NULL;
3240 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3241 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3243 if (cl_arg.logmsg_path)
3244 fprintf(stderr, "%s: log message preserved in %s\n",
3245 getprogname(), cl_arg.logmsg_path);
3249 if (cl_arg.logmsg_path)
3250 unlink(cl_arg.logmsg_path);
3252 error = got_object_id_str(&id_str, id);
3255 printf("Created commit %s\n", id_str);
3258 got_repo_close(repo);
3260 got_worktree_close(worktree);
3269 usage_cherrypick(void)
3271 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3275 static const struct got_error *
3276 cmd_cherrypick(int argc, char *argv[])
3278 const struct got_error *error = NULL;
3279 struct got_worktree *worktree = NULL;
3280 struct got_repository *repo = NULL;
3281 char *cwd = NULL, *commit_id_str = NULL;
3282 struct got_object_id *commit_id = NULL;
3283 struct got_commit_object *commit = NULL;
3284 struct got_object_qid *pid;
3285 struct got_reference *head_ref = NULL;
3286 int ch, did_something = 0;
3288 while ((ch = getopt(argc, argv, "")) != -1) {
3300 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3301 "unveil", NULL) == -1)
3307 cwd = getcwd(NULL, 0);
3309 error = got_error_from_errno("getcwd");
3312 error = got_worktree_open(&worktree, cwd);
3316 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3320 error = apply_unveil(got_repo_get_path(repo), 0,
3321 got_worktree_get_root_path(worktree), 0);
3325 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3326 GOT_OBJ_TYPE_COMMIT, repo);
3327 if (error != NULL) {
3328 struct got_reference *ref;
3329 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3331 error = got_ref_open(&ref, repo, argv[0], 0);
3334 error = got_ref_resolve(&commit_id, repo, ref);
3339 error = got_object_id_str(&commit_id_str, commit_id);
3343 error = got_ref_open(&head_ref, repo,
3344 got_worktree_get_head_ref_name(worktree), 0);
3348 error = check_same_branch(commit_id, head_ref, repo);
3350 if (error->code != GOT_ERR_ANCESTRY)
3354 error = got_error(GOT_ERR_SAME_BRANCH);
3358 error = got_object_open_as_commit(&commit, repo, commit_id);
3361 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3362 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3363 commit_id, repo, update_progress, &did_something, check_cancelled,
3369 printf("Merged commit %s\n", commit_id_str);
3372 got_object_commit_close(commit);
3373 free(commit_id_str);
3375 got_ref_close(head_ref);
3377 got_worktree_close(worktree);
3379 got_repo_close(repo);
3386 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3390 static const struct got_error *
3391 cmd_backout(int argc, char *argv[])
3393 const struct got_error *error = NULL;
3394 struct got_worktree *worktree = NULL;
3395 struct got_repository *repo = NULL;
3396 char *cwd = NULL, *commit_id_str = NULL;
3397 struct got_object_id *commit_id = NULL;
3398 struct got_commit_object *commit = NULL;
3399 struct got_object_qid *pid;
3400 struct got_reference *head_ref = NULL;
3401 int ch, did_something = 0;
3403 while ((ch = getopt(argc, argv, "")) != -1) {
3415 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3416 "unveil", NULL) == -1)
3422 cwd = getcwd(NULL, 0);
3424 error = got_error_from_errno("getcwd");
3427 error = got_worktree_open(&worktree, cwd);
3431 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3435 error = apply_unveil(got_repo_get_path(repo), 0,
3436 got_worktree_get_root_path(worktree), 0);
3440 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3441 GOT_OBJ_TYPE_COMMIT, repo);
3442 if (error != NULL) {
3443 struct got_reference *ref;
3444 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3446 error = got_ref_open(&ref, repo, argv[0], 0);
3449 error = got_ref_resolve(&commit_id, repo, ref);
3454 error = got_object_id_str(&commit_id_str, commit_id);
3458 error = got_ref_open(&head_ref, repo,
3459 got_worktree_get_head_ref_name(worktree), 0);
3463 error = check_same_branch(commit_id, head_ref, repo);
3467 error = got_object_open_as_commit(&commit, repo, commit_id);
3470 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3472 error = got_error(GOT_ERR_ROOT_COMMIT);
3476 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3477 update_progress, &did_something, check_cancelled, NULL);
3482 printf("Backed out commit %s\n", commit_id_str);
3485 got_object_commit_close(commit);
3486 free(commit_id_str);
3488 got_ref_close(head_ref);
3490 got_worktree_close(worktree);
3492 got_repo_close(repo);
3499 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3504 static const struct got_error *
3505 show_rebase_progress(struct got_commit_object *commit,
3506 struct got_object_id *old_id, struct got_object_id *new_id)
3508 const struct got_error *err;
3509 char *old_id_str = NULL, *new_id_str = NULL;
3510 char *logmsg0 = NULL, *logmsg, *nl;
3513 err = got_object_id_str(&old_id_str, old_id);
3518 err = got_object_id_str(&new_id_str, new_id);
3523 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3524 if (logmsg0 == NULL) {
3525 err = got_error_from_errno("strdup");
3530 while (isspace((unsigned char)logmsg[0]))
3533 old_id_str[12] = '\0';
3535 new_id_str[12] = '\0';
3536 len = strlen(logmsg);
3540 nl = strchr(logmsg, '\n');
3543 printf("%s -> %s: %s\n", old_id_str,
3544 new_id_str ? new_id_str : "no-op change", logmsg);
3552 static const struct got_error *
3553 rebase_progress(void *arg, unsigned char status, const char *path)
3555 unsigned char *rebase_status = arg;
3557 while (path[0] == '/')
3559 printf("%c %s\n", status, path);
3561 if (*rebase_status == GOT_STATUS_CONFLICT)
3563 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3564 *rebase_status = status;
3568 static const struct got_error *
3569 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3570 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3571 struct got_repository *repo)
3573 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3574 return got_worktree_rebase_complete(worktree,
3575 new_base_branch, tmp_branch, branch, repo);
3578 static const struct got_error *
3579 rebase_commit(struct got_pathlist_head *merged_paths,
3580 struct got_worktree *worktree, struct got_reference *tmp_branch,
3581 struct got_object_id *commit_id, struct got_repository *repo)
3583 const struct got_error *error;
3584 struct got_commit_object *commit;
3585 struct got_object_id *new_commit_id;
3587 error = got_object_open_as_commit(&commit, repo, commit_id);
3591 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3592 worktree, tmp_branch, commit, commit_id, repo);
3594 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3596 error = show_rebase_progress(commit, commit_id, NULL);
3598 error = show_rebase_progress(commit, commit_id, new_commit_id);
3599 free(new_commit_id);
3602 got_object_commit_close(commit);
3606 struct check_path_prefix_arg {
3607 const char *path_prefix;
3611 static const struct got_error *
3612 check_path_prefix(void *arg, struct got_blob_object *blob1,
3613 struct got_blob_object *blob2, struct got_object_id *id1,
3614 struct got_object_id *id2, const char *path1, const char *path2,
3615 struct got_repository *repo)
3617 struct check_path_prefix_arg *a = arg;
3619 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3620 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3621 return got_error(GOT_ERR_REBASE_PATH);
3626 static const struct got_error *
3627 rebase_check_path_prefix(struct got_object_id *parent_id,
3628 struct got_object_id *commit_id, const char *path_prefix,
3629 struct got_repository *repo)
3631 const struct got_error *err;
3632 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3633 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3634 struct check_path_prefix_arg cpp_arg;
3636 if (got_path_is_root_dir(path_prefix))
3639 err = got_object_open_as_commit(&commit, repo, commit_id);
3643 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3647 err = got_object_open_as_tree(&tree1, repo,
3648 got_object_commit_get_tree_id(parent_commit));
3652 err = got_object_open_as_tree(&tree2, repo,
3653 got_object_commit_get_tree_id(commit));
3657 cpp_arg.path_prefix = path_prefix;
3658 cpp_arg.len = strlen(path_prefix);
3659 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3663 got_object_tree_close(tree1);
3665 got_object_tree_close(tree2);
3667 got_object_commit_close(commit);
3669 got_object_commit_close(parent_commit);
3673 static const struct got_error *
3674 cmd_rebase(int argc, char *argv[])
3676 const struct got_error *error = NULL;
3677 struct got_worktree *worktree = NULL;
3678 struct got_repository *repo = NULL;
3680 struct got_reference *branch = NULL;
3681 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3682 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3683 struct got_object_id *resume_commit_id = NULL;
3684 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3685 struct got_commit_graph *graph = NULL;
3686 struct got_commit_object *commit = NULL;
3687 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3688 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3689 struct got_object_id_queue commits;
3690 struct got_pathlist_head merged_paths;
3691 const struct got_object_id_queue *parent_ids;
3692 struct got_object_qid *qid, *pid;
3694 SIMPLEQ_INIT(&commits);
3695 TAILQ_INIT(&merged_paths);
3697 while ((ch = getopt(argc, argv, "ac")) != -1) {
3703 continue_rebase = 1;
3715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3716 "unveil", NULL) == -1)
3719 if (abort_rebase && continue_rebase)
3721 else if (abort_rebase || continue_rebase) {
3724 } else if (argc != 1)
3727 cwd = getcwd(NULL, 0);
3729 error = got_error_from_errno("getcwd");
3732 error = got_worktree_open(&worktree, cwd);
3736 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3740 error = apply_unveil(got_repo_get_path(repo), 0,
3741 got_worktree_get_root_path(worktree), 0);
3745 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3749 if (rebase_in_progress && abort_rebase) {
3751 error = got_worktree_rebase_continue(&resume_commit_id,
3752 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3755 printf("Switching work tree to %s\n",
3756 got_ref_get_symref_target(new_base_branch));
3757 error = got_worktree_rebase_abort(worktree, repo,
3758 new_base_branch, update_progress, &did_something);
3761 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3762 goto done; /* nothing else to do */
3763 } else if (abort_rebase) {
3764 error = got_error(GOT_ERR_NOT_REBASING);
3768 if (continue_rebase) {
3769 error = got_worktree_rebase_continue(&resume_commit_id,
3770 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3774 error = rebase_commit(NULL, worktree, tmp_branch,
3775 resume_commit_id, repo);
3779 yca_id = got_object_id_dup(resume_commit_id);
3780 if (yca_id == NULL) {
3781 error = got_error_from_errno("got_object_id_dup");
3785 error = got_ref_open(&branch, repo, argv[0], 0);
3789 error = check_same_branch(
3790 got_worktree_get_base_commit_id(worktree), branch, repo);
3792 if (error->code != GOT_ERR_ANCESTRY)
3796 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3797 "specified branch resolves to a commit which "
3798 "is already contained in work tree's branch");
3803 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3807 if (!continue_rebase) {
3808 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3809 got_worktree_get_base_commit_id(worktree),
3810 branch_head_commit_id, repo);
3813 if (yca_id == NULL) {
3814 error = got_error_msg(GOT_ERR_ANCESTRY,
3815 "specified branch shares no common ancestry "
3816 "with work tree's branch");
3820 error = got_worktree_rebase_prepare(&new_base_branch,
3821 &tmp_branch, worktree, branch, repo);
3826 commit_id = branch_head_commit_id;
3827 error = got_object_open_as_commit(&commit, repo, commit_id);
3831 error = got_commit_graph_open(&graph, commit_id, "/", 1, repo);
3834 parent_ids = got_object_commit_get_parent_ids(commit);
3835 pid = SIMPLEQ_FIRST(parent_ids);
3836 error = got_commit_graph_iter_start(graph, pid->id, repo);
3837 got_object_commit_close(commit);
3841 while (got_object_id_cmp(commit_id, yca_id) != 0) {
3842 error = got_commit_graph_iter_next(&parent_id, graph);
3844 if (error->code == GOT_ERR_ITER_COMPLETED) {
3845 error = got_error_msg(GOT_ERR_ANCESTRY,
3846 "ran out of commits to rebase before "
3847 "youngest common ancestor commit has "
3850 } else if (error->code != GOT_ERR_ITER_NEED_MORE)
3852 error = got_commit_graph_fetch_commits(graph, 1, repo);
3856 error = rebase_check_path_prefix(parent_id, commit_id,
3857 got_worktree_get_path_prefix(worktree), repo);
3861 error = got_object_qid_alloc(&qid, commit_id);
3864 SIMPLEQ_INSERT_HEAD(&commits, qid, entry);
3865 commit_id = parent_id;
3868 got_commit_graph_close(graph);
3871 if (SIMPLEQ_EMPTY(&commits)) {
3872 if (continue_rebase)
3873 error = rebase_complete(worktree, branch,
3874 new_base_branch, tmp_branch, repo);
3876 error = got_error(GOT_ERR_EMPTY_REBASE);
3881 SIMPLEQ_FOREACH(qid, &commits, entry) {
3882 commit_id = qid->id;
3883 parent_id = pid ? pid->id : yca_id;
3886 error = got_worktree_rebase_merge_files(&merged_paths,
3887 worktree, parent_id, commit_id, repo, rebase_progress,
3888 &rebase_status, check_cancelled, NULL);
3892 if (rebase_status == GOT_STATUS_CONFLICT) {
3893 got_worktree_rebase_pathlist_free(&merged_paths);
3897 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3899 got_worktree_rebase_pathlist_free(&merged_paths);
3904 if (rebase_status == GOT_STATUS_CONFLICT) {
3905 error = got_worktree_rebase_postpone(worktree);
3908 error = got_error_msg(GOT_ERR_CONFLICTS,
3909 "conflicts must be resolved before rebasing can continue");
3911 error = rebase_complete(worktree, branch, new_base_branch,
3914 got_object_id_queue_free(&commits);
3915 free(branch_head_commit_id);
3916 free(resume_commit_id);
3919 got_commit_graph_close(graph);
3921 got_object_commit_close(commit);
3923 got_ref_close(branch);
3924 if (new_base_branch)
3925 got_ref_close(new_base_branch);
3927 got_ref_close(tmp_branch);
3929 got_worktree_close(worktree);
3931 got_repo_close(repo);