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);
96 __dead static void usage_histedit(void);
98 static const struct got_error* cmd_init(int, char *[]);
99 static const struct got_error* cmd_import(int, char *[]);
100 static const struct got_error* cmd_checkout(int, char *[]);
101 static const struct got_error* cmd_update(int, char *[]);
102 static const struct got_error* cmd_log(int, char *[]);
103 static const struct got_error* cmd_diff(int, char *[]);
104 static const struct got_error* cmd_blame(int, char *[]);
105 static const struct got_error* cmd_tree(int, char *[]);
106 static const struct got_error* cmd_status(int, char *[]);
107 static const struct got_error* cmd_ref(int, char *[]);
108 static const struct got_error* cmd_branch(int, char *[]);
109 static const struct got_error* cmd_add(int, char *[]);
110 static const struct got_error* cmd_remove(int, char *[]);
111 static const struct got_error* cmd_revert(int, char *[]);
112 static const struct got_error* cmd_commit(int, char *[]);
113 static const struct got_error* cmd_cherrypick(int, char *[]);
114 static const struct got_error* cmd_backout(int, char *[]);
115 static const struct got_error* cmd_rebase(int, char *[]);
116 static const struct got_error* cmd_histedit(int, char *[]);
118 static struct got_cmd got_commands[] = {
119 { "init", cmd_init, usage_init, "" },
120 { "import", cmd_import, usage_import, "" },
121 { "checkout", cmd_checkout, usage_checkout, "co" },
122 { "update", cmd_update, usage_update, "up" },
123 { "log", cmd_log, usage_log, "" },
124 { "diff", cmd_diff, usage_diff, "" },
125 { "blame", cmd_blame, usage_blame, "" },
126 { "tree", cmd_tree, usage_tree, "" },
127 { "status", cmd_status, usage_status, "st" },
128 { "ref", cmd_ref, usage_ref, "" },
129 { "branch", cmd_branch, usage_branch, "br" },
130 { "add", cmd_add, usage_add, "" },
131 { "remove", cmd_remove, usage_remove, "rm" },
132 { "revert", cmd_revert, usage_revert, "rv" },
133 { "commit", cmd_commit, usage_commit, "ci" },
134 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
135 { "backout", cmd_backout, usage_backout, "bo" },
136 { "rebase", cmd_rebase, usage_rebase, "rb" },
137 { "histedit", cmd_histedit, usage_histedit, "he" },
145 fprintf(stderr, "commands:");
146 for (i = 0; i < nitems(got_commands); i++) {
147 struct got_cmd *cmd = &got_commands[i];
148 fprintf(stderr, " %s", cmd->cmd_name);
154 main(int argc, char *argv[])
161 setlocale(LC_CTYPE, "");
163 while ((ch = getopt(argc, argv, "h")) != -1) {
181 signal(SIGINT, catch_sigint);
182 signal(SIGPIPE, catch_sigpipe);
184 for (i = 0; i < nitems(got_commands); i++) {
185 const struct got_error *error;
187 cmd = &got_commands[i];
189 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
190 strcmp(cmd->cmd_alias, argv[0]) != 0)
194 got_commands[i].cmd_usage();
196 error = got_commands[i].cmd_main(argc, argv);
197 if (error && !(sigint_received || sigpipe_received)) {
198 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
205 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
213 fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
219 static const struct got_error *
220 get_editor(char **abspath)
222 const struct got_error *err = NULL;
225 editor = getenv("VISUAL");
227 editor = getenv("EDITOR");
230 err = got_path_find_prog(abspath, editor);
235 if (*abspath == NULL) {
236 *abspath = strdup("/bin/ed");
237 if (*abspath == NULL)
238 return got_error_from_errno("strdup");
244 static const struct got_error *
245 apply_unveil(const char *repo_path, int repo_read_only,
246 const char *worktree_path)
248 const struct got_error *err;
251 if (unveil("gmon.out", "rwc") != 0)
252 return got_error_from_errno2("unveil", "gmon.out");
254 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
255 return got_error_from_errno2("unveil", repo_path);
257 if (worktree_path && unveil(worktree_path, "rwc") != 0)
258 return got_error_from_errno2("unveil", worktree_path);
260 if (unveil("/tmp", "rwc") != 0)
261 return got_error_from_errno2("unveil", "/tmp");
263 err = got_privsep_unveil_exec_helpers();
267 if (unveil(NULL, NULL) != 0)
268 return got_error_from_errno("unveil");
276 fprintf(stderr, "usage: %s init path\n", getprogname());
280 static const struct got_error *
281 cmd_init(int argc, char *argv[])
283 const struct got_error *error = NULL;
284 char *repo_path = NULL;
287 while ((ch = getopt(argc, argv, "")) != -1) {
299 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
305 repo_path = strdup(argv[0]);
306 if (repo_path == NULL)
307 return got_error_from_errno("strdup");
309 got_path_strip_trailing_slashes(repo_path);
311 error = got_path_mkdir(repo_path);
313 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
316 error = apply_unveil(repo_path, 0, NULL);
320 error = got_repo_init(repo_path);
332 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
333 "[-r repository-path] [-I pattern] path\n", getprogname());
338 spawn_editor(const char *editor, const char *file)
341 sig_t sighup, sigint, sigquit;
344 sighup = signal(SIGHUP, SIG_IGN);
345 sigint = signal(SIGINT, SIG_IGN);
346 sigquit = signal(SIGQUIT, SIG_IGN);
348 switch (pid = fork()) {
352 execl(editor, editor, file, (char *)NULL);
356 while (waitpid(pid, &st, 0) == -1)
361 (void)signal(SIGHUP, sighup);
362 (void)signal(SIGINT, sigint);
363 (void)signal(SIGQUIT, sigquit);
365 if (!WIFEXITED(st)) {
370 return WEXITSTATUS(st);
373 static const struct got_error *
374 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
375 const char *initial_content)
377 const struct got_error *err = NULL;
381 int content_changed = 0;
386 if (stat(logmsg_path, &st) == -1)
387 return got_error_from_errno2("stat", logmsg_path);
389 if (spawn_editor(editor, logmsg_path) == -1)
390 return got_error_from_errno("failed spawning editor");
392 if (stat(logmsg_path, &st2) == -1)
393 return got_error_from_errno("stat");
395 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
396 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
397 "no changes made to commit message, aborting");
399 *logmsg = malloc(st2.st_size + 1);
401 return got_error_from_errno("malloc");
405 fp = fopen(logmsg_path, "r");
407 err = got_error_from_errno("fopen");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
411 if (!content_changed && strcmp(buf, initial_content) != 0)
413 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
414 continue; /* remove comments and leading empty lines */
415 len = strlcat(*logmsg, buf, st2.st_size);
419 while (len > 0 && (*logmsg)[len - 1] == '\n') {
420 (*logmsg)[len - 1] = '\0';
424 if (len == 0 || !content_changed)
425 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
426 "commit message cannot be empty, aborting");
435 static const struct got_error *
436 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
437 const char *branch_name)
439 char *initial_content = NULL, *logmsg_path = NULL;
440 const struct got_error *err = NULL;
443 if (asprintf(&initial_content,
444 "\n# %s to be imported to branch %s\n", path_dir,
446 return got_error_from_errno("asprintf");
448 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
452 dprintf(fd, initial_content);
455 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
457 free(initial_content);
462 static const struct got_error *
463 import_progress(void *arg, const char *path)
465 printf("A %s\n", path);
469 static const struct got_error *
470 cmd_import(int argc, char *argv[])
472 const struct got_error *error = NULL;
473 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
475 const char *got_author = getenv("GOT_AUTHOR");
476 const char *branch_name = "master";
477 char *refname = NULL, *id_str = NULL;
478 struct got_repository *repo = NULL;
479 struct got_reference *branch_ref = NULL, *head_ref = NULL;
480 struct got_object_id *new_commit_id = NULL;
482 struct got_pathlist_head ignores;
483 struct got_pathlist_entry *pe;
485 TAILQ_INIT(&ignores);
487 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
490 branch_name = optarg;
493 logmsg = strdup(optarg);
494 if (logmsg == NULL) {
495 error = got_error_from_errno("strdup");
500 repo_path = realpath(optarg, NULL);
501 if (repo_path == NULL) {
502 error = got_error_from_errno("realpath");
507 if (optarg[0] == '\0')
509 error = got_pathlist_insert(&pe, &ignores, optarg,
524 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
531 if (got_author == NULL) {
532 /* TODO: Look current user up in password database */
533 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
537 if (repo_path == NULL) {
538 repo_path = getcwd(NULL, 0);
539 if (repo_path == NULL)
540 return got_error_from_errno("getcwd");
542 got_path_strip_trailing_slashes(repo_path);
543 error = got_repo_open(&repo, repo_path);
547 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
548 error = got_error_from_errno("asprintf");
552 error = got_ref_open(&branch_ref, repo, refname, 0);
554 if (error->code != GOT_ERR_NOT_REF)
557 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
558 "import target branch already exists");
562 path_dir = realpath(argv[0], NULL);
563 if (path_dir == NULL) {
564 error = got_error_from_errno("realpath");
567 got_path_strip_trailing_slashes(path_dir);
570 * unveil(2) traverses exec(2); if an editor is used we have
571 * to apply unveil after the log message has been written.
573 if (logmsg == NULL || strlen(logmsg) == 0) {
574 error = get_editor(&editor);
577 error = collect_import_msg(&logmsg, editor, path_dir, refname);
582 if (unveil(path_dir, "r") != 0)
583 return got_error_from_errno2("unveil", path_dir);
585 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
589 error = got_repo_import(&new_commit_id, path_dir, logmsg,
590 got_author, &ignores, repo, import_progress, NULL);
594 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
598 error = got_ref_write(branch_ref, repo);
602 error = got_object_id_str(&id_str, new_commit_id);
606 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
608 if (error->code != GOT_ERR_NOT_REF)
611 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
616 error = got_ref_write(head_ref, repo);
621 printf("Created branch %s with commit %s\n",
622 got_ref_get_name(branch_ref), id_str);
630 got_ref_close(branch_ref);
632 got_ref_close(head_ref);
639 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
640 "[-p prefix] repository-path [worktree-path]\n", getprogname());
644 static const struct got_error *
645 checkout_progress(void *arg, unsigned char status, const char *path)
647 char *worktree_path = arg;
649 /* Base commit bump happens silently. */
650 if (status == GOT_STATUS_BUMP_BASE)
653 while (path[0] == '/')
656 printf("%c %s/%s\n", status, worktree_path, path);
660 static const struct got_error *
661 check_cancelled(void *arg)
663 if (sigint_received || sigpipe_received)
664 return got_error(GOT_ERR_CANCELLED);
668 static const struct got_error *
669 check_linear_ancestry(struct got_object_id *commit_id,
670 struct got_object_id *base_commit_id, struct got_repository *repo)
672 const struct got_error *err = NULL;
673 struct got_object_id *yca_id;
675 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
676 commit_id, base_commit_id, repo);
681 return got_error(GOT_ERR_ANCESTRY);
684 * Require a straight line of history between the target commit
685 * and the work tree's base commit.
687 * Non-linear situations such as this require a rebase:
689 * (commit) D F (base_commit)
697 * 'got update' only handles linear cases:
698 * Update forwards in time: A (base/yca) - B - C - D (commit)
699 * Update backwards in time: D (base) - C - B - A (commit/yca)
701 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
702 got_object_id_cmp(base_commit_id, yca_id) != 0)
703 return got_error(GOT_ERR_ANCESTRY);
709 static const struct got_error *
710 check_same_branch(struct got_object_id *commit_id,
711 struct got_reference *head_ref, struct got_repository *repo)
713 const struct got_error *err = NULL;
714 struct got_commit_graph *graph = NULL;
715 struct got_object_id *head_commit_id = NULL;
716 int is_same_branch = 0;
718 err = got_ref_resolve(&head_commit_id, repo, head_ref);
722 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
726 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
731 struct got_object_id *id;
732 err = got_commit_graph_iter_next(&id, graph);
734 if (err->code == GOT_ERR_ITER_COMPLETED) {
737 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
739 err = got_commit_graph_fetch_commits(graph, 1,
746 if (got_object_id_cmp(id, commit_id) == 0) {
754 got_commit_graph_close(graph);
755 free(head_commit_id);
756 if (!err && !is_same_branch)
757 err = got_error(GOT_ERR_ANCESTRY);
761 static const struct got_error *
762 cmd_checkout(int argc, char *argv[])
764 const struct got_error *error = NULL;
765 struct got_repository *repo = NULL;
766 struct got_reference *head_ref = NULL;
767 struct got_worktree *worktree = NULL;
768 char *repo_path = NULL;
769 char *worktree_path = NULL;
770 const char *path_prefix = "";
771 const char *branch_name = GOT_REF_HEAD;
772 char *commit_id_str = NULL;
773 int ch, same_path_prefix;
775 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
778 branch_name = optarg;
781 commit_id_str = strdup(optarg);
782 if (commit_id_str == NULL)
783 return got_error_from_errno("strdup");
786 path_prefix = optarg;
798 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
799 "unveil", NULL) == -1)
803 char *cwd, *base, *dotgit;
804 repo_path = realpath(argv[0], NULL);
805 if (repo_path == NULL)
806 return got_error_from_errno2("realpath", argv[0]);
807 cwd = getcwd(NULL, 0);
809 error = got_error_from_errno("getcwd");
812 if (path_prefix[0]) {
813 base = basename(path_prefix);
815 error = got_error_from_errno2("basename",
820 base = basename(repo_path);
822 error = got_error_from_errno2("basename",
827 dotgit = strstr(base, ".git");
830 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
831 error = got_error_from_errno("asprintf");
836 } else if (argc == 2) {
837 repo_path = realpath(argv[0], NULL);
838 if (repo_path == NULL) {
839 error = got_error_from_errno2("realpath", argv[0]);
842 worktree_path = realpath(argv[1], NULL);
843 if (worktree_path == NULL) {
844 if (errno != ENOENT) {
845 error = got_error_from_errno2("realpath",
849 worktree_path = strdup(argv[1]);
850 if (worktree_path == NULL) {
851 error = got_error_from_errno("strdup");
858 got_path_strip_trailing_slashes(repo_path);
859 got_path_strip_trailing_slashes(worktree_path);
861 error = got_repo_open(&repo, repo_path);
865 /* Pre-create work tree path for unveil(2) */
866 error = got_path_mkdir(worktree_path);
868 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
870 if (!got_path_dir_is_empty(worktree_path)) {
871 error = got_error_path(worktree_path,
872 GOT_ERR_DIR_NOT_EMPTY);
877 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
881 error = got_ref_open(&head_ref, repo, branch_name, 0);
885 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
886 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
889 error = got_worktree_open(&worktree, worktree_path);
893 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
897 if (!same_path_prefix) {
898 error = got_error(GOT_ERR_PATH_PREFIX);
903 struct got_object_id *commit_id;
904 error = got_repo_match_object_id_prefix(&commit_id,
905 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
908 error = check_linear_ancestry(commit_id,
909 got_worktree_get_base_commit_id(worktree), repo);
914 error = check_same_branch(commit_id, head_ref, repo);
917 error = got_worktree_set_base_commit_id(worktree, repo,
924 error = got_worktree_checkout_files(worktree, "", repo,
925 checkout_progress, worktree_path, check_cancelled, NULL);
929 printf("Now shut up and hack\n");
941 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
946 static const struct got_error *
947 update_progress(void *arg, unsigned char status, const char *path)
949 int *did_something = arg;
951 if (status == GOT_STATUS_EXISTS)
956 /* Base commit bump happens silently. */
957 if (status == GOT_STATUS_BUMP_BASE)
960 while (path[0] == '/')
962 printf("%c %s\n", status, path);
966 static const struct got_error *
967 switch_head_ref(struct got_reference *head_ref,
968 struct got_object_id *commit_id, struct got_worktree *worktree,
969 struct got_repository *repo)
971 const struct got_error *err = NULL;
973 int ref_has_moved = 0;
975 /* Trivial case: switching between two different references. */
976 if (strcmp(got_ref_get_name(head_ref),
977 got_worktree_get_head_ref_name(worktree)) != 0) {
978 printf("Switching work tree from %s to %s\n",
979 got_worktree_get_head_ref_name(worktree),
980 got_ref_get_name(head_ref));
981 return got_worktree_set_head_ref(worktree, head_ref);
984 err = check_linear_ancestry(commit_id,
985 got_worktree_get_base_commit_id(worktree), repo);
987 if (err->code != GOT_ERR_ANCESTRY)
994 /* Switching to a rebased branch with the same reference name. */
995 err = got_object_id_str(&base_id_str,
996 got_worktree_get_base_commit_id(worktree));
999 printf("Reference %s now points at a different branch\n",
1000 got_worktree_get_head_ref_name(worktree));
1001 printf("Switching work tree from %s to %s\n", base_id_str,
1002 got_worktree_get_head_ref_name(worktree));
1006 static const struct got_error *
1007 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1009 const struct got_error *err;
1012 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1016 return got_error(GOT_ERR_REBASING);
1018 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1022 return got_error(GOT_ERR_HISTEDIT_BUSY);
1027 static const struct got_error *
1028 cmd_update(int argc, char *argv[])
1030 const struct got_error *error = NULL;
1031 struct got_repository *repo = NULL;
1032 struct got_worktree *worktree = NULL;
1033 char *worktree_path = NULL, *path = NULL;
1034 struct got_object_id *commit_id = NULL;
1035 char *commit_id_str = NULL;
1036 const char *branch_name = NULL;
1037 struct got_reference *head_ref = NULL;
1038 int ch, did_something = 0;
1040 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1043 branch_name = optarg;
1046 commit_id_str = strdup(optarg);
1047 if (commit_id_str == NULL)
1048 return got_error_from_errno("strdup");
1060 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1061 "unveil", NULL) == -1)
1064 worktree_path = getcwd(NULL, 0);
1065 if (worktree_path == NULL) {
1066 error = got_error_from_errno("getcwd");
1069 error = got_worktree_open(&worktree, worktree_path);
1073 error = check_rebase_or_histedit_in_progress(worktree);
1080 error = got_error_from_errno("strdup");
1083 } else if (argc == 1) {
1084 error = got_worktree_resolve_path(&path, worktree, argv[0]);
1090 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1094 error = apply_unveil(got_repo_get_path(repo), 0,
1095 got_worktree_get_root_path(worktree));
1099 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1100 got_worktree_get_head_ref_name(worktree), 0);
1103 if (commit_id_str == NULL) {
1104 error = got_ref_resolve(&commit_id, repo, head_ref);
1107 error = got_object_id_str(&commit_id_str, commit_id);
1111 error = got_repo_match_object_id_prefix(&commit_id,
1112 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1115 free(commit_id_str);
1116 error = got_object_id_str(&commit_id_str, commit_id);
1122 struct got_object_id *head_commit_id;
1123 if (strlen(path) != 0) {
1124 fprintf(stderr, "%s: switching between branches "
1125 "requires that the entire work tree "
1126 "gets updated, not just '%s'\n",
1127 getprogname(), path);
1128 error = got_error(GOT_ERR_BAD_PATH);
1131 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1134 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1135 free(head_commit_id);
1138 error = check_same_branch(commit_id, head_ref, repo);
1141 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1145 error = check_linear_ancestry(commit_id,
1146 got_worktree_get_base_commit_id(worktree), repo);
1147 if (error != NULL) {
1148 if (error->code == GOT_ERR_ANCESTRY)
1149 error = got_error(GOT_ERR_BRANCH_MOVED);
1152 error = check_same_branch(commit_id, head_ref, repo);
1157 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1159 error = got_worktree_set_base_commit_id(worktree, repo,
1165 error = got_worktree_checkout_files(worktree, path, repo,
1166 update_progress, &did_something, check_cancelled, NULL);
1171 printf("Updated to commit %s\n", commit_id_str);
1173 printf("Already up-to-date\n");
1175 free(worktree_path);
1178 free(commit_id_str);
1182 static const struct got_error *
1183 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1184 int diff_context, struct got_repository *repo)
1186 const struct got_error *err = NULL;
1187 struct got_tree_object *tree1 = NULL, *tree2;
1188 struct got_object_qid *qid;
1189 char *id_str1 = NULL, *id_str2;
1190 struct got_diff_blob_output_unidiff_arg arg;
1192 err = got_object_open_as_tree(&tree2, repo,
1193 got_object_commit_get_tree_id(commit));
1197 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1199 struct got_commit_object *pcommit;
1201 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1205 err = got_object_open_as_tree(&tree1, repo,
1206 got_object_commit_get_tree_id(pcommit));
1207 got_object_commit_close(pcommit);
1211 err = got_object_id_str(&id_str1, qid->id);
1216 err = got_object_id_str(&id_str2, id);
1220 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1221 arg.diff_context = diff_context;
1222 arg.outfile = stdout;
1223 err = got_diff_tree(tree1, tree2, "", "", repo,
1224 got_diff_blob_output_unidiff, &arg);
1227 got_object_tree_close(tree1);
1228 got_object_tree_close(tree2);
1235 get_datestr(time_t *time, char *datebuf)
1237 char *p, *s = ctime_r(time, datebuf);
1238 p = strchr(s, '\n');
1244 static const struct got_error *
1245 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1246 struct got_repository *repo, int show_patch, int diff_context,
1247 struct got_reflist_head *refs)
1249 const struct got_error *err = NULL;
1250 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1252 time_t committer_time;
1253 const char *author, *committer;
1254 char *refs_str = NULL;
1255 struct got_reflist_entry *re;
1257 SIMPLEQ_FOREACH(re, refs, entry) {
1260 if (got_object_id_cmp(re->id, id) != 0)
1262 name = got_ref_get_name(re->ref);
1263 if (strcmp(name, GOT_REF_HEAD) == 0)
1265 if (strncmp(name, "refs/", 5) == 0)
1267 if (strncmp(name, "got/", 4) == 0)
1269 if (strncmp(name, "heads/", 6) == 0)
1271 if (strncmp(name, "remotes/", 8) == 0)
1274 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1276 err = got_error_from_errno("asprintf");
1282 err = got_object_id_str(&id_str, id);
1286 printf("-----------------------------------------------\n");
1287 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1288 refs_str ? refs_str : "", refs_str ? ")" : "");
1293 printf("from: %s\n", got_object_commit_get_author(commit));
1294 committer_time = got_object_commit_get_committer_time(commit);
1295 datestr = get_datestr(&committer_time, datebuf);
1296 printf("date: %s UTC\n", datestr);
1297 author = got_object_commit_get_author(commit);
1298 committer = got_object_commit_get_committer(commit);
1299 if (strcmp(author, committer) != 0)
1300 printf("via: %s\n", committer);
1301 if (got_object_commit_get_nparents(commit) > 1) {
1302 const struct got_object_id_queue *parent_ids;
1303 struct got_object_qid *qid;
1305 parent_ids = got_object_commit_get_parent_ids(commit);
1306 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1307 err = got_object_id_str(&id_str, qid->id);
1310 printf("parent %d: %s\n", n++, id_str);
1315 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1316 if (logmsg0 == NULL)
1317 return got_error_from_errno("strdup");
1321 line = strsep(&logmsg, "\n");
1323 printf(" %s\n", line);
1328 err = print_patch(commit, id, diff_context, repo);
1333 if (fflush(stdout) != 0 && err == NULL)
1334 err = got_error_from_errno("fflush");
1338 static const struct got_error *
1339 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1340 char *path, int show_patch, int diff_context, int limit,
1341 int first_parent_traversal, struct got_reflist_head *refs)
1343 const struct got_error *err;
1344 struct got_commit_graph *graph;
1346 err = got_commit_graph_open(&graph, root_id, path,
1347 first_parent_traversal, repo);
1350 err = got_commit_graph_iter_start(graph, root_id, repo);
1354 struct got_commit_object *commit;
1355 struct got_object_id *id;
1357 if (sigint_received || sigpipe_received)
1360 err = got_commit_graph_iter_next(&id, graph);
1362 if (err->code == GOT_ERR_ITER_COMPLETED) {
1366 if (err->code != GOT_ERR_ITER_NEED_MORE)
1368 err = got_commit_graph_fetch_commits(graph, 1, repo);
1377 err = got_object_open_as_commit(&commit, repo, id);
1380 err = print_commit(commit, id, repo, show_patch, diff_context,
1382 got_object_commit_close(commit);
1383 if (err || (limit && --limit == 0))
1387 got_commit_graph_close(graph);
1394 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1395 "[-r repository-path] [path]\n", getprogname());
1399 static const struct got_error *
1400 cmd_log(int argc, char *argv[])
1402 const struct got_error *error;
1403 struct got_repository *repo = NULL;
1404 struct got_worktree *worktree = NULL;
1405 struct got_commit_object *commit = NULL;
1406 struct got_object_id *id = NULL;
1407 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1408 char *start_commit = NULL;
1409 int diff_context = 3, ch;
1410 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1412 struct got_reflist_head refs;
1414 SIMPLEQ_INIT(&refs);
1417 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1423 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1429 start_commit = optarg;
1432 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1435 err(1, "-C option %s", errstr);
1438 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1440 err(1, "-l option %s", errstr);
1443 first_parent_traversal = 1;
1446 repo_path = realpath(optarg, NULL);
1447 if (repo_path == NULL)
1448 err(1, "-r option");
1449 got_path_strip_trailing_slashes(repo_path);
1460 cwd = getcwd(NULL, 0);
1462 error = got_error_from_errno("getcwd");
1466 error = got_worktree_open(&worktree, cwd);
1467 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1474 error = got_error_from_errno("strdup");
1477 } else if (argc == 1) {
1479 error = got_worktree_resolve_path(&path, worktree,
1484 path = strdup(argv[0]);
1486 error = got_error_from_errno("strdup");
1493 if (repo_path == NULL) {
1494 repo_path = worktree ?
1495 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1497 if (repo_path == NULL) {
1498 error = got_error_from_errno("strdup");
1502 error = got_repo_open(&repo, repo_path);
1506 error = apply_unveil(got_repo_get_path(repo), 1,
1507 worktree ? got_worktree_get_root_path(worktree) : NULL);
1511 if (start_commit == NULL) {
1512 struct got_reference *head_ref;
1513 error = got_ref_open(&head_ref, repo,
1514 worktree ? got_worktree_get_head_ref_name(worktree)
1518 error = got_ref_resolve(&id, repo, head_ref);
1519 got_ref_close(head_ref);
1522 error = got_object_open_as_commit(&commit, repo, id);
1524 struct got_reference *ref;
1525 error = got_ref_open(&ref, repo, start_commit, 0);
1526 if (error == NULL) {
1528 error = got_ref_resolve(&id, repo, ref);
1532 error = got_object_get_type(&obj_type, repo, id);
1535 if (obj_type == GOT_OBJ_TYPE_TAG) {
1536 struct got_tag_object *tag;
1537 error = got_object_open_as_tag(&tag, repo, id);
1540 if (got_object_tag_get_object_type(tag) !=
1541 GOT_OBJ_TYPE_COMMIT) {
1542 got_object_tag_close(tag);
1543 error = got_error(GOT_ERR_OBJ_TYPE);
1547 id = got_object_id_dup(
1548 got_object_tag_get_object_id(tag));
1550 error = got_error_from_errno(
1551 "got_object_id_dup");
1552 got_object_tag_close(tag);
1555 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1556 error = got_error(GOT_ERR_OBJ_TYPE);
1559 error = got_object_open_as_commit(&commit, repo, id);
1563 if (commit == NULL) {
1564 error = got_repo_match_object_id_prefix(&id,
1565 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1573 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1578 path = in_repo_path;
1581 error = got_ref_list(&refs, repo);
1585 error = print_commits(id, repo, path, show_patch,
1586 diff_context, limit, first_parent_traversal, &refs);
1593 got_worktree_close(worktree);
1595 const struct got_error *repo_error;
1596 repo_error = got_repo_close(repo);
1600 got_ref_list_free(&refs);
1607 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1608 "[object1 object2 | path]\n", getprogname());
1612 struct print_diff_arg {
1613 struct got_repository *repo;
1614 struct got_worktree *worktree;
1620 static const struct got_error *
1621 print_diff(void *arg, unsigned char status, const char *path,
1622 struct got_object_id *blob_id, struct got_object_id *commit_id)
1624 struct print_diff_arg *a = arg;
1625 const struct got_error *err = NULL;
1626 struct got_blob_object *blob1 = NULL;
1628 char *abspath = NULL;
1631 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1632 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1635 if (!a->header_shown) {
1636 printf("diff %s %s\n", a->id_str,
1637 got_worktree_get_root_path(a->worktree));
1638 a->header_shown = 1;
1641 if (status != GOT_STATUS_ADD) {
1642 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1648 if (status != GOT_STATUS_DELETE) {
1649 if (asprintf(&abspath, "%s/%s",
1650 got_worktree_get_root_path(a->worktree), path) == -1) {
1651 err = got_error_from_errno("asprintf");
1655 f2 = fopen(abspath, "r");
1657 err = got_error_from_errno2("fopen", abspath);
1660 if (lstat(abspath, &sb) == -1) {
1661 err = got_error_from_errno2("lstat", abspath);
1667 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1671 got_object_blob_close(blob1);
1672 if (f2 && fclose(f2) != 0 && err == NULL)
1673 err = got_error_from_errno("fclose");
1678 static const struct got_error *
1679 cmd_diff(int argc, char *argv[])
1681 const struct got_error *error;
1682 struct got_repository *repo = NULL;
1683 struct got_worktree *worktree = NULL;
1684 char *cwd = NULL, *repo_path = NULL;
1685 struct got_object_id *id1 = NULL, *id2 = NULL;
1686 const char *id_str1 = NULL, *id_str2 = NULL;
1687 char *label1 = NULL, *label2 = NULL;
1689 int diff_context = 3, ch;
1694 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1699 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1702 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1704 err(1, "-C option %s", errstr);
1707 repo_path = realpath(optarg, NULL);
1708 if (repo_path == NULL)
1709 err(1, "-r option");
1710 got_path_strip_trailing_slashes(repo_path);
1721 cwd = getcwd(NULL, 0);
1723 error = got_error_from_errno("getcwd");
1726 error = got_worktree_open(&worktree, cwd);
1727 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1730 if (worktree == NULL) {
1731 error = got_error(GOT_ERR_NOT_WORKTREE);
1736 "-r option can't be used when diffing a work tree");
1737 repo_path = strdup(got_worktree_get_repo_path(worktree));
1738 if (repo_path == NULL) {
1739 error = got_error_from_errno("strdup");
1743 error = got_worktree_resolve_path(&path, worktree,
1750 error = got_error_from_errno("strdup");
1754 } else if (argc == 2) {
1757 if (worktree && repo_path == NULL) {
1759 strdup(got_worktree_get_repo_path(worktree));
1760 if (repo_path == NULL) {
1761 error = got_error_from_errno("strdup");
1768 if (repo_path == NULL) {
1769 repo_path = getcwd(NULL, 0);
1770 if (repo_path == NULL)
1771 return got_error_from_errno("getcwd");
1774 error = got_repo_open(&repo, repo_path);
1779 error = apply_unveil(got_repo_get_path(repo), 1,
1780 worktree ? got_worktree_get_root_path(worktree) : NULL);
1785 struct print_diff_arg arg;
1787 error = got_object_id_str(&id_str,
1788 got_worktree_get_base_commit_id(worktree));
1792 arg.worktree = worktree;
1793 arg.diff_context = diff_context;
1794 arg.id_str = id_str;
1795 arg.header_shown = 0;
1797 error = got_worktree_status(worktree, path, repo, print_diff,
1798 &arg, check_cancelled, NULL);
1803 error = got_repo_match_object_id_prefix(&id1, id_str1,
1804 GOT_OBJ_TYPE_ANY, repo);
1806 struct got_reference *ref;
1807 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1809 error = got_ref_open(&ref, repo, id_str1, 0);
1812 label1 = strdup(got_ref_get_name(ref));
1813 if (label1 == NULL) {
1814 error = got_error_from_errno("strdup");
1817 error = got_ref_resolve(&id1, repo, ref);
1822 error = got_object_id_str(&label1, id1);
1823 if (label1 == NULL) {
1824 error = got_error_from_errno("strdup");
1829 error = got_repo_match_object_id_prefix(&id2, id_str2,
1830 GOT_OBJ_TYPE_ANY, repo);
1832 struct got_reference *ref;
1833 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1835 error = got_ref_open(&ref, repo, id_str2, 0);
1838 label2 = strdup(got_ref_get_name(ref));
1839 if (label2 == NULL) {
1840 error = got_error_from_errno("strdup");
1843 error = got_ref_resolve(&id2, repo, ref);
1848 error = got_object_id_str(&label2, id2);
1849 if (label2 == NULL) {
1850 error = got_error_from_errno("strdup");
1855 error = got_object_get_type(&type1, repo, id1);
1859 error = got_object_get_type(&type2, repo, id2);
1863 if (type1 != type2) {
1864 error = got_error(GOT_ERR_OBJ_TYPE);
1869 case GOT_OBJ_TYPE_BLOB:
1870 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1871 diff_context, repo, stdout);
1873 case GOT_OBJ_TYPE_TREE:
1874 error = got_diff_objects_as_trees(id1, id2, "", "",
1875 diff_context, repo, stdout);
1877 case GOT_OBJ_TYPE_COMMIT:
1878 printf("diff %s %s\n", label1, label2);
1879 error = got_diff_objects_as_commits(id1, id2, diff_context,
1883 error = got_error(GOT_ERR_OBJ_TYPE);
1893 got_worktree_close(worktree);
1895 const struct got_error *repo_error;
1896 repo_error = got_repo_close(repo);
1907 "usage: %s blame [-c commit] [-r repository-path] path\n",
1912 static const struct got_error *
1913 cmd_blame(int argc, char *argv[])
1915 const struct got_error *error;
1916 struct got_repository *repo = NULL;
1917 struct got_worktree *worktree = NULL;
1918 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1919 struct got_object_id *commit_id = NULL;
1920 char *commit_id_str = NULL;
1924 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1929 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1932 commit_id_str = optarg;
1935 repo_path = realpath(optarg, NULL);
1936 if (repo_path == NULL)
1937 err(1, "-r option");
1938 got_path_strip_trailing_slashes(repo_path);
1954 cwd = getcwd(NULL, 0);
1956 error = got_error_from_errno("getcwd");
1959 if (repo_path == NULL) {
1960 error = got_worktree_open(&worktree, cwd);
1961 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1967 strdup(got_worktree_get_repo_path(worktree));
1968 if (repo_path == NULL)
1969 error = got_error_from_errno("strdup");
1973 repo_path = strdup(cwd);
1974 if (repo_path == NULL) {
1975 error = got_error_from_errno("strdup");
1981 error = got_repo_open(&repo, repo_path);
1985 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
1990 const char *prefix = got_worktree_get_path_prefix(worktree);
1991 char *p, *worktree_subdir = cwd +
1992 strlen(got_worktree_get_root_path(worktree));
1993 if (asprintf(&p, "%s%s%s%s%s",
1994 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1995 worktree_subdir, worktree_subdir[0] ? "/" : "",
1997 error = got_error_from_errno("asprintf");
2000 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2003 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2008 if (commit_id_str == NULL) {
2009 struct got_reference *head_ref;
2010 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2013 error = got_ref_resolve(&commit_id, repo, head_ref);
2014 got_ref_close(head_ref);
2018 error = got_repo_match_object_id_prefix(&commit_id,
2019 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2024 error = got_blame(in_repo_path, commit_id, repo, stdout);
2031 got_worktree_close(worktree);
2033 const struct got_error *repo_error;
2034 repo_error = got_repo_close(repo);
2045 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2051 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2052 const char *root_path)
2054 int is_root_path = (strcmp(path, root_path) == 0);
2056 path += strlen(root_path);
2057 while (path[0] == '/')
2060 printf("%s%s%s%s%s\n", id ? id : "", path,
2061 is_root_path ? "" : "/", te->name,
2062 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2065 static const struct got_error *
2066 print_tree(const char *path, struct got_object_id *commit_id,
2067 int show_ids, int recurse, const char *root_path,
2068 struct got_repository *repo)
2070 const struct got_error *err = NULL;
2071 struct got_object_id *tree_id = NULL;
2072 struct got_tree_object *tree = NULL;
2073 const struct got_tree_entries *entries;
2074 struct got_tree_entry *te;
2076 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2080 err = got_object_open_as_tree(&tree, repo, tree_id);
2083 entries = got_object_tree_get_entries(tree);
2084 te = SIMPLEQ_FIRST(&entries->head);
2088 if (sigint_received || sigpipe_received)
2093 err = got_object_id_str(&id_str, te->id);
2096 if (asprintf(&id, "%s ", id_str) == -1) {
2097 err = got_error_from_errno("asprintf");
2103 print_entry(te, id, path, root_path);
2106 if (recurse && S_ISDIR(te->mode)) {
2108 if (asprintf(&child_path, "%s%s%s", path,
2109 path[0] == '/' && path[1] == '\0' ? "" : "/",
2111 err = got_error_from_errno("asprintf");
2114 err = print_tree(child_path, commit_id, show_ids, 1,
2121 te = SIMPLEQ_NEXT(te, entry);
2125 got_object_tree_close(tree);
2130 static const struct got_error *
2131 cmd_tree(int argc, char *argv[])
2133 const struct got_error *error;
2134 struct got_repository *repo = NULL;
2135 struct got_worktree *worktree = NULL;
2137 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2138 struct got_object_id *commit_id = NULL;
2139 char *commit_id_str = NULL;
2140 int show_ids = 0, recurse = 0;
2144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2149 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2152 commit_id_str = optarg;
2155 repo_path = realpath(optarg, NULL);
2156 if (repo_path == NULL)
2157 err(1, "-r option");
2158 got_path_strip_trailing_slashes(repo_path);
2182 cwd = getcwd(NULL, 0);
2184 error = got_error_from_errno("getcwd");
2187 if (repo_path == NULL) {
2188 error = got_worktree_open(&worktree, cwd);
2189 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2195 strdup(got_worktree_get_repo_path(worktree));
2196 if (repo_path == NULL)
2197 error = got_error_from_errno("strdup");
2201 repo_path = strdup(cwd);
2202 if (repo_path == NULL) {
2203 error = got_error_from_errno("strdup");
2209 error = got_repo_open(&repo, repo_path);
2213 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2219 char *p, *worktree_subdir = cwd +
2220 strlen(got_worktree_get_root_path(worktree));
2221 if (asprintf(&p, "%s/%s",
2222 got_worktree_get_path_prefix(worktree),
2223 worktree_subdir) == -1) {
2224 error = got_error_from_errno("asprintf");
2227 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2234 if (in_repo_path == NULL) {
2235 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2240 if (commit_id_str == NULL) {
2241 struct got_reference *head_ref;
2242 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2245 error = got_ref_resolve(&commit_id, repo, head_ref);
2246 got_ref_close(head_ref);
2250 error = got_repo_match_object_id_prefix(&commit_id,
2251 commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2256 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2257 in_repo_path, repo);
2264 got_worktree_close(worktree);
2266 const struct got_error *repo_error;
2267 repo_error = got_repo_close(repo);
2277 fprintf(stderr, "usage: %s status [path]\n", getprogname());
2281 static const struct got_error *
2282 print_status(void *arg, unsigned char status, const char *path,
2283 struct got_object_id *blob_id, struct got_object_id *commit_id)
2285 printf("%c %s\n", status, path);
2289 static const struct got_error *
2290 cmd_status(int argc, char *argv[])
2292 const struct got_error *error = NULL;
2293 struct got_repository *repo = NULL;
2294 struct got_worktree *worktree = NULL;
2295 char *cwd = NULL, *path = NULL;
2298 while ((ch = getopt(argc, argv, "")) != -1) {
2310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2314 cwd = getcwd(NULL, 0);
2316 error = got_error_from_errno("getcwd");
2320 error = got_worktree_open(&worktree, cwd);
2327 error = got_error_from_errno("strdup");
2330 } else if (argc == 1) {
2331 error = got_worktree_resolve_path(&path, worktree, argv[0]);
2337 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2341 error = apply_unveil(got_repo_get_path(repo), 1,
2342 got_worktree_get_root_path(worktree));
2346 error = got_worktree_status(worktree, path, repo, print_status, NULL,
2347 check_cancelled, NULL);
2358 "usage: %s ref [-r repository] -l | -d name | name target\n",
2363 static const struct got_error *
2364 list_refs(struct got_repository *repo)
2366 static const struct got_error *err = NULL;
2367 struct got_reflist_head refs;
2368 struct got_reflist_entry *re;
2370 SIMPLEQ_INIT(&refs);
2371 err = got_ref_list(&refs, repo);
2375 SIMPLEQ_FOREACH(re, &refs, entry) {
2377 refstr = got_ref_to_str(re->ref);
2379 return got_error_from_errno("got_ref_to_str");
2380 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2384 got_ref_list_free(&refs);
2388 static const struct got_error *
2389 delete_ref(struct got_repository *repo, const char *refname)
2391 const struct got_error *err = NULL;
2392 struct got_reference *ref;
2394 err = got_ref_open(&ref, repo, refname, 0);
2398 err = got_ref_delete(ref, repo);
2403 static const struct got_error *
2404 add_ref(struct got_repository *repo, const char *refname, const char *target)
2406 const struct got_error *err = NULL;
2407 struct got_object_id *id;
2408 struct got_reference *ref = NULL;
2411 * Don't let the user create a reference named '-'.
2412 * While technically a valid reference name, this case is usually
2413 * an unintended typo.
2415 if (refname[0] == '-' && refname[1] == '\0')
2416 return got_error(GOT_ERR_BAD_REF_NAME);
2418 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2421 struct got_reference *target_ref;
2423 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2425 err = got_ref_open(&target_ref, repo, target, 0);
2428 err = got_ref_resolve(&id, repo, target_ref);
2429 got_ref_close(target_ref);
2434 err = got_ref_alloc(&ref, refname, id);
2438 err = got_ref_write(ref, repo);
2446 static const struct got_error *
2447 cmd_ref(int argc, char *argv[])
2449 const struct got_error *error = NULL;
2450 struct got_repository *repo = NULL;
2451 struct got_worktree *worktree = NULL;
2452 char *cwd = NULL, *repo_path = NULL;
2453 int ch, do_list = 0;
2454 const char *delref = NULL;
2456 /* TODO: Add -s option for adding symbolic references. */
2457 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2463 repo_path = realpath(optarg, NULL);
2464 if (repo_path == NULL)
2465 err(1, "-r option");
2466 got_path_strip_trailing_slashes(repo_path);
2477 if (do_list && delref)
2478 errx(1, "-l and -d options are mutually exclusive\n");
2483 if (do_list || delref) {
2486 } else if (argc != 2)
2491 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2495 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2496 "sendfd unveil", NULL) == -1)
2500 cwd = getcwd(NULL, 0);
2502 error = got_error_from_errno("getcwd");
2506 if (repo_path == NULL) {
2507 error = got_worktree_open(&worktree, cwd);
2508 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2514 strdup(got_worktree_get_repo_path(worktree));
2515 if (repo_path == NULL)
2516 error = got_error_from_errno("strdup");
2520 repo_path = strdup(cwd);
2521 if (repo_path == NULL) {
2522 error = got_error_from_errno("strdup");
2528 error = got_repo_open(&repo, repo_path);
2532 error = apply_unveil(got_repo_get_path(repo), do_list,
2533 worktree ? got_worktree_get_root_path(worktree) : NULL);
2538 error = list_refs(repo);
2540 error = delete_ref(repo, delref);
2542 error = add_ref(repo, argv[0], argv[1]);
2545 got_repo_close(repo);
2547 got_worktree_close(worktree);
2557 "usage: %s branch [-r repository] -l | -d name | "
2558 "name [base-branch]\n", getprogname());
2562 static const struct got_error *
2563 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2565 static const struct got_error *err = NULL;
2566 struct got_reflist_head refs;
2567 struct got_reflist_entry *re;
2569 SIMPLEQ_INIT(&refs);
2571 err = got_ref_list(&refs, repo);
2575 SIMPLEQ_FOREACH(re, &refs, entry) {
2576 const char *refname, *marker = " ";
2578 refname = got_ref_get_name(re->ref);
2579 if (strncmp(refname, "refs/heads/", 11) != 0)
2581 if (worktree && strcmp(refname,
2582 got_worktree_get_head_ref_name(worktree)) == 0) {
2583 struct got_object_id *id = NULL;
2584 err = got_ref_resolve(&id, repo, re->ref);
2587 if (got_object_id_cmp(id,
2588 got_worktree_get_base_commit_id(worktree)) == 0)
2595 refstr = got_ref_to_str(re->ref);
2597 return got_error_from_errno("got_ref_to_str");
2598 printf("%s%s: %s\n", marker, refname, refstr);
2602 got_ref_list_free(&refs);
2606 static const struct got_error *
2607 delete_branch(struct got_repository *repo, const char *branch_name)
2609 const struct got_error *err = NULL;
2610 struct got_reference *ref;
2613 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2614 return got_error_from_errno("asprintf");
2616 err = got_ref_open(&ref, repo, refname, 0);
2620 err = got_ref_delete(ref, repo);
2627 static const struct got_error *
2628 add_branch(struct got_repository *repo, const char *branch_name,
2629 const char *base_branch)
2631 const struct got_error *err = NULL;
2632 struct got_object_id *id = NULL;
2633 struct got_reference *ref = NULL;
2634 char *base_refname = NULL, *refname = NULL;
2635 struct got_reference *base_ref;
2638 * Don't let the user create a branch named '-'.
2639 * While technically a valid reference name, this case is usually
2640 * an unintended typo.
2642 if (branch_name[0] == '-' && branch_name[1] == '\0')
2643 return got_error(GOT_ERR_BAD_REF_NAME);
2645 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2646 base_refname = strdup(GOT_REF_HEAD);
2647 if (base_refname == NULL)
2648 return got_error_from_errno("strdup");
2649 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2650 return got_error_from_errno("asprintf");
2652 err = got_ref_open(&base_ref, repo, base_refname, 0);
2655 err = got_ref_resolve(&id, repo, base_ref);
2656 got_ref_close(base_ref);
2660 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2661 err = got_error_from_errno("asprintf");
2665 err = got_ref_open(&ref, repo, refname, 0);
2667 err = got_error(GOT_ERR_BRANCH_EXISTS);
2669 } else if (err->code != GOT_ERR_NOT_REF)
2672 err = got_ref_alloc(&ref, refname, id);
2676 err = got_ref_write(ref, repo);
2686 static const struct got_error *
2687 cmd_branch(int argc, char *argv[])
2689 const struct got_error *error = NULL;
2690 struct got_repository *repo = NULL;
2691 struct got_worktree *worktree = NULL;
2692 char *cwd = NULL, *repo_path = NULL;
2693 int ch, do_list = 0;
2694 const char *delref = NULL;
2696 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2702 repo_path = realpath(optarg, NULL);
2703 if (repo_path == NULL)
2704 err(1, "-r option");
2705 got_path_strip_trailing_slashes(repo_path);
2716 if (do_list && delref)
2717 errx(1, "-l and -d options are mutually exclusive\n");
2722 if (do_list || delref) {
2725 } else if (argc < 1 || argc > 2)
2730 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2734 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2735 "sendfd unveil", NULL) == -1)
2739 cwd = getcwd(NULL, 0);
2741 error = got_error_from_errno("getcwd");
2745 if (repo_path == NULL) {
2746 error = got_worktree_open(&worktree, cwd);
2747 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2753 strdup(got_worktree_get_repo_path(worktree));
2754 if (repo_path == NULL)
2755 error = got_error_from_errno("strdup");
2759 repo_path = strdup(cwd);
2760 if (repo_path == NULL) {
2761 error = got_error_from_errno("strdup");
2767 error = got_repo_open(&repo, repo_path);
2771 error = apply_unveil(got_repo_get_path(repo), do_list,
2772 worktree ? got_worktree_get_root_path(worktree) : NULL);
2777 error = list_branches(repo, worktree);
2779 error = delete_branch(repo, delref);
2781 const char *base_branch;
2783 base_branch = worktree ?
2784 got_worktree_get_head_ref_name(worktree) :
2787 base_branch = argv[1];
2788 error = add_branch(repo, argv[0], base_branch);
2792 got_repo_close(repo);
2794 got_worktree_close(worktree);
2803 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2807 static const struct got_error *
2808 cmd_add(int argc, char *argv[])
2810 const struct got_error *error = NULL;
2811 struct got_repository *repo = NULL;
2812 struct got_worktree *worktree = NULL;
2814 struct got_pathlist_head paths;
2815 struct got_pathlist_entry *pe;
2820 while ((ch = getopt(argc, argv, "")) != -1) {
2832 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2839 cwd = getcwd(NULL, 0);
2841 error = got_error_from_errno("getcwd");
2845 error = got_worktree_open(&worktree, cwd);
2849 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2853 error = apply_unveil(got_repo_get_path(repo), 1,
2854 got_worktree_get_root_path(worktree));
2858 for (x = 0; x < argc; x++) {
2859 char *path = realpath(argv[x], NULL);
2861 error = got_error_from_errno2("realpath", argv[x]);
2865 got_path_strip_trailing_slashes(path);
2866 error = got_pathlist_insert(&pe, &paths, path, NULL);
2872 error = got_worktree_schedule_add(worktree, &paths, print_status,
2876 got_repo_close(repo);
2878 got_worktree_close(worktree);
2879 TAILQ_FOREACH(pe, &paths, entry)
2880 free((char *)pe->path);
2881 got_pathlist_free(&paths);
2889 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2893 static const struct got_error *
2894 cmd_remove(int argc, char *argv[])
2896 const struct got_error *error = NULL;
2897 struct got_worktree *worktree = NULL;
2898 struct got_repository *repo = NULL;
2900 struct got_pathlist_head paths;
2901 struct got_pathlist_entry *pe;
2902 int ch, i, delete_local_mods = 0;
2906 while ((ch = getopt(argc, argv, "f")) != -1) {
2909 delete_local_mods = 1;
2921 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2928 cwd = getcwd(NULL, 0);
2930 error = got_error_from_errno("getcwd");
2933 error = got_worktree_open(&worktree, cwd);
2937 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2941 error = apply_unveil(got_repo_get_path(repo), 1,
2942 got_worktree_get_root_path(worktree));
2946 for (i = 0; i < argc; i++) {
2947 char *path = realpath(argv[i], NULL);
2949 error = got_error_from_errno2("realpath", argv[i]);
2953 got_path_strip_trailing_slashes(path);
2954 error = got_pathlist_insert(&pe, &paths, path, NULL);
2960 error = got_worktree_schedule_delete(worktree, &paths,
2961 delete_local_mods, print_status, NULL, repo);
2966 got_repo_close(repo);
2968 got_worktree_close(worktree);
2969 TAILQ_FOREACH(pe, &paths, entry)
2970 free((char *)pe->path);
2971 got_pathlist_free(&paths);
2979 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2983 static const struct got_error *
2984 revert_progress(void *arg, unsigned char status, const char *path)
2986 while (path[0] == '/')
2988 printf("%c %s\n", status, path);
2992 static const struct got_error *
2993 cmd_revert(int argc, char *argv[])
2995 const struct got_error *error = NULL;
2996 struct got_worktree *worktree = NULL;
2997 struct got_repository *repo = NULL;
2998 char *cwd = NULL, *path = NULL;
2999 struct got_pathlist_head paths;
3000 struct got_pathlist_entry *pe;
3005 while ((ch = getopt(argc, argv, "")) != -1) {
3017 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3018 "unveil", NULL) == -1)
3024 for (i = 0; i < argc; i++) {
3025 char *path = realpath(argv[i], NULL);
3027 error = got_error_from_errno2("realpath", argv[i]);
3031 got_path_strip_trailing_slashes(path);
3032 error = got_pathlist_insert(&pe, &paths, path, NULL);
3039 cwd = getcwd(NULL, 0);
3041 error = got_error_from_errno("getcwd");
3044 error = got_worktree_open(&worktree, cwd);
3048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3052 error = apply_unveil(got_repo_get_path(repo), 1,
3053 got_worktree_get_root_path(worktree));
3057 error = got_worktree_revert(worktree, &paths,
3058 revert_progress, NULL, repo);
3063 got_repo_close(repo);
3065 got_worktree_close(worktree);
3074 fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3078 struct collect_commit_logmsg_arg {
3079 const char *cmdline_log;
3081 const char *worktree_path;
3082 const char *branch_name;
3083 const char *repo_path;
3088 static const struct got_error *
3089 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3092 char *initial_content = NULL;
3093 struct got_pathlist_entry *pe;
3094 const struct got_error *err = NULL;
3095 char *template = NULL;
3096 struct collect_commit_logmsg_arg *a = arg;
3100 /* if a message was specified on the command line, just use it */
3101 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3102 len = strlen(a->cmdline_log) + 1;
3103 *logmsg = malloc(len + 1);
3104 if (*logmsg == NULL)
3105 return got_error_from_errno("malloc");
3106 strlcpy(*logmsg, a->cmdline_log, len);
3110 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3111 return got_error_from_errno("asprintf");
3113 if (asprintf(&initial_content,
3114 "\n# changes to be committed on branch %s:\n",
3115 a->branch_name) == -1)
3116 return got_error_from_errno("asprintf");
3118 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3122 dprintf(fd, initial_content);
3124 TAILQ_FOREACH(pe, commitable_paths, entry) {
3125 struct got_commitable *ct = pe->data;
3126 dprintf(fd, "# %c %s\n",
3127 got_commitable_get_status(ct),
3128 got_commitable_get_path(ct));
3132 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3134 unlink(a->logmsg_path);
3135 free(a->logmsg_path);
3136 free(initial_content);
3139 /* Editor is done; we can now apply unveil(2) */
3141 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3150 static const struct got_error *
3151 cmd_commit(int argc, char *argv[])
3153 const struct got_error *error = NULL;
3154 struct got_worktree *worktree = NULL;
3155 struct got_repository *repo = NULL;
3156 char *cwd = NULL, *path = NULL, *id_str = NULL;
3157 struct got_object_id *id = NULL;
3158 const char *logmsg = NULL;
3159 const char *got_author = getenv("GOT_AUTHOR");
3160 struct collect_commit_logmsg_arg cl_arg;
3161 char *editor = NULL;
3164 while ((ch = getopt(argc, argv, "m:")) != -1) {
3179 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3180 "unveil", NULL) == -1)
3184 path = realpath(argv[0], NULL);
3186 error = got_error_from_errno2("realpath", argv[0]);
3189 got_path_strip_trailing_slashes(path);
3190 } else if (argc != 0)
3193 if (got_author == NULL) {
3194 /* TODO: Look current user up in password database */
3195 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3199 cwd = getcwd(NULL, 0);
3201 error = got_error_from_errno("getcwd");
3204 error = got_worktree_open(&worktree, cwd);
3208 error = check_rebase_or_histedit_in_progress(worktree);
3212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3217 * unveil(2) traverses exec(2); if an editor is used we have
3218 * to apply unveil after the log message has been written.
3220 if (logmsg == NULL || strlen(logmsg) == 0)
3221 error = get_editor(&editor);
3223 error = apply_unveil(got_repo_get_path(repo), 0,
3224 got_worktree_get_root_path(worktree));
3228 cl_arg.editor = editor;
3229 cl_arg.cmdline_log = logmsg;
3230 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3231 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3232 if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3233 cl_arg.branch_name += 5;
3234 if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3235 cl_arg.branch_name += 6;
3236 cl_arg.repo_path = got_repo_get_path(repo);
3237 cl_arg.logmsg_path = NULL;
3238 error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3239 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3241 if (cl_arg.logmsg_path)
3242 fprintf(stderr, "%s: log message preserved in %s\n",
3243 getprogname(), cl_arg.logmsg_path);
3247 if (cl_arg.logmsg_path)
3248 unlink(cl_arg.logmsg_path);
3250 error = got_object_id_str(&id_str, id);
3253 printf("Created commit %s\n", id_str);
3256 got_repo_close(repo);
3258 got_worktree_close(worktree);
3267 usage_cherrypick(void)
3269 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3273 static const struct got_error *
3274 cmd_cherrypick(int argc, char *argv[])
3276 const struct got_error *error = NULL;
3277 struct got_worktree *worktree = NULL;
3278 struct got_repository *repo = NULL;
3279 char *cwd = NULL, *commit_id_str = NULL;
3280 struct got_object_id *commit_id = NULL;
3281 struct got_commit_object *commit = NULL;
3282 struct got_object_qid *pid;
3283 struct got_reference *head_ref = NULL;
3284 int ch, did_something = 0;
3286 while ((ch = getopt(argc, argv, "")) != -1) {
3298 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3299 "unveil", NULL) == -1)
3305 cwd = getcwd(NULL, 0);
3307 error = got_error_from_errno("getcwd");
3310 error = got_worktree_open(&worktree, cwd);
3314 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3318 error = apply_unveil(got_repo_get_path(repo), 0,
3319 got_worktree_get_root_path(worktree));
3323 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3324 GOT_OBJ_TYPE_COMMIT, repo);
3325 if (error != NULL) {
3326 struct got_reference *ref;
3327 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3329 error = got_ref_open(&ref, repo, argv[0], 0);
3332 error = got_ref_resolve(&commit_id, repo, ref);
3337 error = got_object_id_str(&commit_id_str, commit_id);
3341 error = got_ref_open(&head_ref, repo,
3342 got_worktree_get_head_ref_name(worktree), 0);
3346 error = check_same_branch(commit_id, head_ref, repo);
3348 if (error->code != GOT_ERR_ANCESTRY)
3352 error = got_error(GOT_ERR_SAME_BRANCH);
3356 error = got_object_open_as_commit(&commit, repo, commit_id);
3359 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3360 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3361 commit_id, repo, update_progress, &did_something, check_cancelled,
3367 printf("Merged commit %s\n", commit_id_str);
3370 got_object_commit_close(commit);
3371 free(commit_id_str);
3373 got_ref_close(head_ref);
3375 got_worktree_close(worktree);
3377 got_repo_close(repo);
3384 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3388 static const struct got_error *
3389 cmd_backout(int argc, char *argv[])
3391 const struct got_error *error = NULL;
3392 struct got_worktree *worktree = NULL;
3393 struct got_repository *repo = NULL;
3394 char *cwd = NULL, *commit_id_str = NULL;
3395 struct got_object_id *commit_id = NULL;
3396 struct got_commit_object *commit = NULL;
3397 struct got_object_qid *pid;
3398 struct got_reference *head_ref = NULL;
3399 int ch, did_something = 0;
3401 while ((ch = getopt(argc, argv, "")) != -1) {
3413 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3414 "unveil", NULL) == -1)
3420 cwd = getcwd(NULL, 0);
3422 error = got_error_from_errno("getcwd");
3425 error = got_worktree_open(&worktree, cwd);
3429 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3433 error = apply_unveil(got_repo_get_path(repo), 0,
3434 got_worktree_get_root_path(worktree));
3438 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3439 GOT_OBJ_TYPE_COMMIT, repo);
3440 if (error != NULL) {
3441 struct got_reference *ref;
3442 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3444 error = got_ref_open(&ref, repo, argv[0], 0);
3447 error = got_ref_resolve(&commit_id, repo, ref);
3452 error = got_object_id_str(&commit_id_str, commit_id);
3456 error = got_ref_open(&head_ref, repo,
3457 got_worktree_get_head_ref_name(worktree), 0);
3461 error = check_same_branch(commit_id, head_ref, repo);
3465 error = got_object_open_as_commit(&commit, repo, commit_id);
3468 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3470 error = got_error(GOT_ERR_ROOT_COMMIT);
3474 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3475 update_progress, &did_something, check_cancelled, NULL);
3480 printf("Backed out commit %s\n", commit_id_str);
3483 got_object_commit_close(commit);
3484 free(commit_id_str);
3486 got_ref_close(head_ref);
3488 got_worktree_close(worktree);
3490 got_repo_close(repo);
3497 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3503 trim_logmsg(char *logmsg, int limit)
3508 len = strlen(logmsg);
3512 nl = strchr(logmsg, '\n');
3517 static const struct got_error *
3518 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3520 const char *logmsg0 = NULL;
3522 logmsg0 = got_object_commit_get_logmsg(commit);
3524 while (isspace((unsigned char)logmsg0[0]))
3527 *logmsg = strdup(logmsg0);
3528 if (*logmsg == NULL)
3529 return got_error_from_errno("strdup");
3531 trim_logmsg(*logmsg, limit);
3535 static const struct got_error *
3536 show_rebase_progress(struct got_commit_object *commit,
3537 struct got_object_id *old_id, struct got_object_id *new_id)
3539 const struct got_error *err;
3540 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3542 err = got_object_id_str(&old_id_str, old_id);
3547 err = got_object_id_str(&new_id_str, new_id);
3552 old_id_str[12] = '\0';
3554 new_id_str[12] = '\0';
3556 err = get_short_logmsg(&logmsg, 42, commit);
3560 printf("%s -> %s: %s\n", old_id_str,
3561 new_id_str ? new_id_str : "no-op change", logmsg);
3568 static const struct got_error *
3569 rebase_progress(void *arg, unsigned char status, const char *path)
3571 unsigned char *rebase_status = arg;
3573 while (path[0] == '/')
3575 printf("%c %s\n", status, path);
3577 if (*rebase_status == GOT_STATUS_CONFLICT)
3579 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3580 *rebase_status = status;
3584 static const struct got_error *
3585 rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3586 struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3587 struct got_repository *repo)
3589 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3590 return got_worktree_rebase_complete(worktree,
3591 new_base_branch, tmp_branch, branch, repo);
3594 static const struct got_error *
3595 rebase_commit(struct got_pathlist_head *merged_paths,
3596 struct got_worktree *worktree, struct got_reference *tmp_branch,
3597 struct got_object_id *commit_id, struct got_repository *repo)
3599 const struct got_error *error;
3600 struct got_commit_object *commit;
3601 struct got_object_id *new_commit_id;
3603 error = got_object_open_as_commit(&commit, repo, commit_id);
3607 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3608 worktree, tmp_branch, commit, commit_id, repo);
3610 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3612 error = show_rebase_progress(commit, commit_id, NULL);
3614 error = show_rebase_progress(commit, commit_id, new_commit_id);
3615 free(new_commit_id);
3618 got_object_commit_close(commit);
3622 struct check_path_prefix_arg {
3623 const char *path_prefix;
3627 static const struct got_error *
3628 check_path_prefix(void *arg, struct got_blob_object *blob1,
3629 struct got_blob_object *blob2, struct got_object_id *id1,
3630 struct got_object_id *id2, const char *path1, const char *path2,
3631 struct got_repository *repo)
3633 struct check_path_prefix_arg *a = arg;
3635 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3636 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3637 return got_error(GOT_ERR_REBASE_PATH);
3642 static const struct got_error *
3643 rebase_check_path_prefix(struct got_object_id *parent_id,
3644 struct got_object_id *commit_id, const char *path_prefix,
3645 struct got_repository *repo)
3647 const struct got_error *err;
3648 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3649 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3650 struct check_path_prefix_arg cpp_arg;
3652 if (got_path_is_root_dir(path_prefix))
3655 err = got_object_open_as_commit(&commit, repo, commit_id);
3659 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3663 err = got_object_open_as_tree(&tree1, repo,
3664 got_object_commit_get_tree_id(parent_commit));
3668 err = got_object_open_as_tree(&tree2, repo,
3669 got_object_commit_get_tree_id(commit));
3673 cpp_arg.path_prefix = path_prefix;
3674 cpp_arg.len = strlen(path_prefix);
3675 err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3679 got_object_tree_close(tree1);
3681 got_object_tree_close(tree2);
3683 got_object_commit_close(commit);
3685 got_object_commit_close(parent_commit);
3689 static const struct got_error *
3690 collect_commits_to_rebase(struct got_object_id_queue *commits,
3691 struct got_object_id *initial_commit_id,
3692 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3693 const char *path_prefix, struct got_repository *repo)
3695 const struct got_error *err = NULL;
3696 struct got_commit_graph *graph = NULL;
3697 struct got_object_id *parent_id = NULL;
3698 struct got_object_qid *qid;
3699 struct got_object_id *commit_id = initial_commit_id;
3701 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3705 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3708 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3709 err = got_commit_graph_iter_next(&parent_id, graph);
3711 if (err->code == GOT_ERR_ITER_COMPLETED) {
3712 err = got_error_msg(GOT_ERR_ANCESTRY,
3713 "ran out of commits to rebase before "
3714 "youngest common ancestor commit has "
3717 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3719 err = got_commit_graph_fetch_commits(graph, 1, repo);
3723 err = rebase_check_path_prefix(parent_id, commit_id,
3728 err = got_object_qid_alloc(&qid, commit_id);
3731 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3732 commit_id = parent_id;
3736 got_commit_graph_close(graph);
3740 static const struct got_error *
3741 cmd_rebase(int argc, char *argv[])
3743 const struct got_error *error = NULL;
3744 struct got_worktree *worktree = NULL;
3745 struct got_repository *repo = NULL;
3747 struct got_reference *branch = NULL;
3748 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3749 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3750 struct got_object_id *resume_commit_id = NULL;
3751 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3752 struct got_commit_object *commit = NULL;
3753 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3754 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3755 struct got_object_id_queue commits;
3756 struct got_pathlist_head merged_paths;
3757 const struct got_object_id_queue *parent_ids;
3758 struct got_object_qid *qid, *pid;
3760 SIMPLEQ_INIT(&commits);
3761 TAILQ_INIT(&merged_paths);
3763 while ((ch = getopt(argc, argv, "ac")) != -1) {
3769 continue_rebase = 1;
3781 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3782 "unveil", NULL) == -1)
3785 if (abort_rebase && continue_rebase)
3787 else if (abort_rebase || continue_rebase) {
3790 } else if (argc != 1)
3793 cwd = getcwd(NULL, 0);
3795 error = got_error_from_errno("getcwd");
3798 error = got_worktree_open(&worktree, cwd);
3802 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3806 error = apply_unveil(got_repo_get_path(repo), 0,
3807 got_worktree_get_root_path(worktree));
3811 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3817 if (!rebase_in_progress) {
3818 error = got_error(GOT_ERR_NOT_REBASING);
3821 error = got_worktree_rebase_continue(&resume_commit_id,
3822 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3825 printf("Switching work tree to %s\n",
3826 got_ref_get_symref_target(new_base_branch));
3827 error = got_worktree_rebase_abort(worktree, repo,
3828 new_base_branch, update_progress, &did_something);
3831 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3832 goto done; /* nothing else to do */
3835 if (continue_rebase) {
3836 if (!rebase_in_progress) {
3837 error = got_error(GOT_ERR_NOT_REBASING);
3840 error = got_worktree_rebase_continue(&resume_commit_id,
3841 &new_base_branch, &tmp_branch, &branch, worktree, repo);
3845 error = rebase_commit(NULL, worktree, tmp_branch,
3846 resume_commit_id, repo);
3850 yca_id = got_object_id_dup(resume_commit_id);
3851 if (yca_id == NULL) {
3852 error = got_error_from_errno("got_object_id_dup");
3856 error = got_ref_open(&branch, repo, argv[0], 0);
3860 error = check_same_branch(
3861 got_worktree_get_base_commit_id(worktree), branch, repo);
3863 if (error->code != GOT_ERR_ANCESTRY)
3867 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3868 "specified branch resolves to a commit which "
3869 "is already contained in work tree's branch");
3874 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3878 if (!continue_rebase) {
3879 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3880 got_worktree_get_base_commit_id(worktree),
3881 branch_head_commit_id, repo);
3884 if (yca_id == NULL) {
3885 error = got_error_msg(GOT_ERR_ANCESTRY,
3886 "specified branch shares no common ancestry "
3887 "with work tree's branch");
3891 error = got_worktree_rebase_prepare(&new_base_branch,
3892 &tmp_branch, worktree, branch, repo);
3897 commit_id = branch_head_commit_id;
3898 error = got_object_open_as_commit(&commit, repo, commit_id);
3902 parent_ids = got_object_commit_get_parent_ids(commit);
3903 pid = SIMPLEQ_FIRST(parent_ids);
3904 error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3905 yca_id, got_worktree_get_path_prefix(worktree), repo);
3906 got_object_commit_close(commit);
3911 if (SIMPLEQ_EMPTY(&commits)) {
3912 if (continue_rebase)
3913 error = rebase_complete(worktree, branch,
3914 new_base_branch, tmp_branch, repo);
3916 error = got_error(GOT_ERR_EMPTY_REBASE);
3921 SIMPLEQ_FOREACH(qid, &commits, entry) {
3922 commit_id = qid->id;
3923 parent_id = pid ? pid->id : yca_id;
3926 error = got_worktree_rebase_merge_files(&merged_paths,
3927 worktree, parent_id, commit_id, repo, rebase_progress,
3928 &rebase_status, check_cancelled, NULL);
3932 if (rebase_status == GOT_STATUS_CONFLICT) {
3933 got_worktree_rebase_pathlist_free(&merged_paths);
3937 error = rebase_commit(&merged_paths, worktree, tmp_branch,
3939 got_worktree_rebase_pathlist_free(&merged_paths);
3944 if (rebase_status == GOT_STATUS_CONFLICT) {
3945 error = got_worktree_rebase_postpone(worktree);
3948 error = got_error_msg(GOT_ERR_CONFLICTS,
3949 "conflicts must be resolved before rebasing can continue");
3951 error = rebase_complete(worktree, branch, new_base_branch,
3954 got_object_id_queue_free(&commits);
3955 free(branch_head_commit_id);
3956 free(resume_commit_id);
3959 got_object_commit_close(commit);
3961 got_ref_close(branch);
3962 if (new_base_branch)
3963 got_ref_close(new_base_branch);
3965 got_ref_close(tmp_branch);
3967 got_worktree_close(worktree);
3969 got_repo_close(repo);
3974 usage_histedit(void)
3976 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
3981 #define GOT_HISTEDIT_PICK 'p'
3982 #define GOT_HISTEDIT_EDIT 'e'
3983 #define GOT_HISTEDIT_FOLD 'f'
3984 #define GOT_HISTEDIT_DROP 'd'
3985 #define GOT_HISTEDIT_MESG 'm'
3987 static struct got_histedit_cmd {
3991 } got_histedit_cmds[] = {
3992 { GOT_HISTEDIT_PICK, "pick", "use commit" },
3993 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
3994 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
3995 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
3996 { GOT_HISTEDIT_MESG, "mesg",
3997 "single-line log message for commit above (open editor if empty)" },
4000 struct got_histedit_list_entry {
4001 TAILQ_ENTRY(got_histedit_list_entry) entry;
4002 struct got_object_id *commit_id;
4003 const struct got_histedit_cmd *cmd;
4006 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4008 static const struct got_error *
4009 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4010 FILE *f, struct got_repository *repo)
4012 const struct got_error *err = NULL;
4013 char *logmsg = NULL, *id_str = NULL;
4014 struct got_commit_object *commit = NULL;
4017 err = got_object_open_as_commit(&commit, repo, commit_id);
4021 err = get_short_logmsg(&logmsg, 34, commit);
4025 err = got_object_id_str(&id_str, commit_id);
4029 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4031 err = got_ferror(f, GOT_ERR_IO);
4034 got_object_commit_close(commit);
4040 static const struct got_error *
4041 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4042 struct got_repository *repo)
4044 const struct got_error *err = NULL;
4045 struct got_object_qid *qid;
4047 if (SIMPLEQ_EMPTY(commits))
4048 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4050 SIMPLEQ_FOREACH(qid, commits, entry) {
4051 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4060 static const struct got_error *
4061 write_cmd_list(FILE *f)
4063 const struct got_error *err = NULL;
4066 n = fprintf(f, "# Available histedit commands:\n");
4068 return got_ferror(f, GOT_ERR_IO);
4070 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4071 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4072 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4075 err = got_ferror(f, GOT_ERR_IO);
4079 n = fprintf(f, "# Commits will be processed in order from top to "
4080 "bottom of this file.\n");
4082 return got_ferror(f, GOT_ERR_IO);
4086 static const struct got_error *
4087 histedit_syntax_error(int lineno)
4089 static char msg[42];
4092 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4094 if (ret == -1 || ret >= sizeof(msg))
4095 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4097 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4100 static const struct got_error *
4101 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4102 char *logmsg, struct got_repository *repo)
4104 const struct got_error *err;
4105 struct got_commit_object *folded_commit = NULL;
4108 err = got_object_id_str(&id_str, hle->commit_id);
4112 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4116 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4117 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4118 got_object_commit_get_logmsg(folded_commit)) == -1) {
4119 err = got_error_from_errno("asprintf");
4124 got_object_commit_close(folded_commit);
4129 static struct got_histedit_list_entry *
4130 get_folded_commits(struct got_histedit_list_entry *hle)
4132 struct got_histedit_list_entry *prev, *folded = NULL;
4134 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4135 while (prev && prev->cmd->code == GOT_HISTEDIT_FOLD) {
4137 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4143 static const struct got_error *
4144 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4145 struct got_repository *repo)
4147 char *logmsg_path = NULL, *id_str = NULL;
4148 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4149 const struct got_error *err = NULL;
4150 struct got_commit_object *commit = NULL;
4152 struct got_histedit_list_entry *folded = NULL;
4154 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4158 folded = get_folded_commits(hle);
4160 while (folded != hle) {
4161 err = append_folded_commit_msg(&new_msg, folded,
4167 folded = TAILQ_NEXT(folded, entry);
4171 err = got_object_id_str(&id_str, hle->commit_id);
4174 if (asprintf(&new_msg,
4175 "%s\n# original log message of commit %s: %s",
4176 logmsg ? logmsg : "", id_str,
4177 got_object_commit_get_logmsg(commit)) == -1) {
4178 err = got_error_from_errno("asprintf");
4184 err = got_object_id_str(&id_str, hle->commit_id);
4188 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4192 dprintf(fd, logmsg);
4195 err = get_editor(&editor);
4199 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4201 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4204 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4205 if (hle->logmsg == NULL)
4206 err = got_error_from_errno("strdup");
4209 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4210 err = got_error_from_errno2("unlink", logmsg_path);
4215 got_object_commit_close(commit);
4219 static const struct got_error *
4220 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4221 FILE *f, struct got_repository *repo)
4223 const struct got_error *err = NULL;
4224 char *line = NULL, *p, *end;
4228 const struct got_histedit_cmd *cmd;
4229 struct got_object_id *commit_id = NULL;
4230 struct got_histedit_list_entry *hle = NULL;
4233 len = getline(&line, &size, f);
4235 const struct got_error *getline_err;
4238 getline_err = got_error_from_errno("getline");
4239 err = got_ferror(f, getline_err->code);
4244 while (isspace((unsigned char)p[0]))
4246 if (p[0] == '#' || p[0] == '\0') {
4252 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4253 cmd = &got_histedit_cmds[i];
4254 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4255 isspace((unsigned char)p[strlen(cmd->name)])) {
4256 p += strlen(cmd->name);
4259 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4265 err = histedit_syntax_error(lineno);
4268 while (isspace((unsigned char)p[0]))
4270 if (cmd->code == GOT_HISTEDIT_MESG) {
4271 if (hle == NULL || hle->logmsg != NULL) {
4272 err = got_error(GOT_ERR_HISTEDIT_CMD);
4276 err = histedit_edit_logmsg(hle, repo);
4280 hle->logmsg = strdup(p);
4281 if (hle->logmsg == NULL) {
4282 err = got_error_from_errno("strdup");
4291 while (end[0] && !isspace((unsigned char)end[0]))
4295 err = got_object_resolve_id_str(&commit_id, repo, p);
4297 /* override error code */
4298 err = histedit_syntax_error(lineno);
4302 hle = malloc(sizeof(*hle));
4304 err = got_error_from_errno("malloc");
4308 hle->commit_id = commit_id;
4313 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4321 static const struct got_error *
4322 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4323 const char *path, struct got_repository *repo)
4325 const struct got_error *err = NULL;
4329 err = get_editor(&editor);
4333 if (spawn_editor(editor, path) == -1) {
4334 err = got_error_from_errno("failed spawning editor");
4338 f = fopen(path, "r");
4340 err = got_error_from_errno("fopen");
4343 err = histedit_parse_list(histedit_cmds, f, repo);
4345 if (f && fclose(f) != 0 && err == NULL)
4346 err = got_error_from_errno("fclose");
4351 static const struct got_error *
4352 histedit_edit_list_retry(struct got_histedit_list *, const char *,
4353 struct got_object_id_queue *, const char *, struct got_repository *);
4355 static const struct got_error *
4356 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4357 struct got_object_id_queue *commits, struct got_repository *repo)
4359 const struct got_error *err;
4363 err = got_opentemp_named(&path, &f, "got-histedit");
4367 err = write_cmd_list(f);
4371 err = histedit_write_commit_list(commits, f, repo);
4375 if (fclose(f) != 0) {
4376 err = got_error_from_errno("fclose");
4381 err = histedit_run_editor(histedit_cmds, path, repo);
4383 const char *errmsg = err->msg;
4384 if (err->code != GOT_ERR_HISTEDIT_SYNTAX)
4386 err = histedit_edit_list_retry(histedit_cmds, errmsg,
4387 commits, path, repo);
4390 if (f && fclose(f) != 0 && err == NULL)
4391 err = got_error_from_errno("fclose");
4392 if (path && unlink(path) != 0 && err == NULL)
4393 err = got_error_from_errno2("unlink", path);
4398 static const struct got_error *
4399 histedit_save_list(struct got_histedit_list *histedit_cmds,
4400 struct got_worktree *worktree, struct got_repository *repo)
4402 const struct got_error *err = NULL;
4405 struct got_histedit_list_entry *hle;
4406 struct got_commit_object *commit = NULL;
4408 err = got_worktree_get_histedit_list_path(&path, worktree);
4412 f = fopen(path, "w");
4414 err = got_error_from_errno2("fopen", path);
4417 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4418 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4424 int n = fprintf(f, "%c %s\n",
4425 GOT_HISTEDIT_MESG, hle->logmsg);
4427 err = got_ferror(f, GOT_ERR_IO);
4433 if (f && fclose(f) != 0 && err == NULL)
4434 err = got_error_from_errno("fclose");
4437 got_object_commit_close(commit);
4441 static const struct got_error *
4442 histedit_load_list(struct got_histedit_list *histedit_cmds,
4443 const char *path, struct got_repository *repo)
4445 const struct got_error *err = NULL;
4448 f = fopen(path, "r");
4450 err = got_error_from_errno2("fopen", path);
4454 err = histedit_parse_list(histedit_cmds, f, repo);
4456 if (f && fclose(f) != 0 && err == NULL)
4457 err = got_error_from_errno("fclose");
4461 static const struct got_error *
4462 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4463 const char *errmsg, struct got_object_id_queue *commits,
4464 const char *path, struct got_repository *repo)
4466 const struct got_error *err = NULL;
4469 while (resp != 'c' && resp != 'r' && resp != 'q') {
4470 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4471 "or (a)bort: ", getprogname(), errmsg);
4475 err = histedit_run_editor(histedit_cmds, path, repo);
4478 err = histedit_edit_script(histedit_cmds,
4482 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4485 printf("invalid response '%c'\n", resp);
4493 static const struct got_error *
4494 histedit_complete(struct got_worktree *worktree,
4495 struct got_reference *tmp_branch, struct got_reference *branch,
4496 struct got_repository *repo)
4498 printf("Switching work tree to %s\n",
4499 got_ref_get_symref_target(branch));
4500 return got_worktree_histedit_complete(worktree, tmp_branch, branch,
4504 static const struct got_error *
4505 show_histedit_progress(struct got_commit_object *commit,
4506 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4508 const struct got_error *err;
4509 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4511 err = got_object_id_str(&old_id_str, hle->commit_id);
4516 err = got_object_id_str(&new_id_str, new_id);
4521 old_id_str[12] = '\0';
4523 new_id_str[12] = '\0';
4526 logmsg = strdup(hle->logmsg);
4527 if (logmsg == NULL) {
4528 err = got_error_from_errno("strdup");
4531 trim_logmsg(logmsg, 42);
4533 err = get_short_logmsg(&logmsg, 42, commit);
4538 switch (hle->cmd->code) {
4539 case GOT_HISTEDIT_PICK:
4540 case GOT_HISTEDIT_EDIT:
4541 printf("%s -> %s: %s\n", old_id_str,
4542 new_id_str ? new_id_str : "no-op change", logmsg);
4544 case GOT_HISTEDIT_DROP:
4545 case GOT_HISTEDIT_FOLD:
4546 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4559 static const struct got_error *
4560 histedit_commit(struct got_pathlist_head *merged_paths,
4561 struct got_worktree *worktree, struct got_reference *tmp_branch,
4562 struct got_histedit_list_entry *hle, struct got_repository *repo)
4564 const struct got_error *err;
4565 struct got_commit_object *commit;
4566 struct got_object_id *new_commit_id;
4568 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4569 && hle->logmsg == NULL) {
4570 err = histedit_edit_logmsg(hle, repo);
4575 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4579 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4580 worktree, tmp_branch, commit, hle->commit_id, hle->logmsg, repo);
4582 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4584 err = show_histedit_progress(commit, hle, NULL);
4586 err = show_histedit_progress(commit, hle, new_commit_id);
4587 free(new_commit_id);
4590 got_object_commit_close(commit);
4594 static const struct got_error *
4595 histedit_skip_commit(struct got_histedit_list_entry *hle,
4596 struct got_worktree *worktree, struct got_repository *repo)
4598 const struct got_error *error;
4599 struct got_commit_object *commit;
4601 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4606 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4610 error = show_histedit_progress(commit, hle, NULL);
4611 got_object_commit_close(commit);
4615 static const struct got_error *
4616 histedit_check_script(struct got_histedit_list *histedit_cmds,
4617 struct got_object_id_queue *commits, struct got_repository *repo)
4619 const struct got_error *err = NULL;
4620 struct got_object_qid *qid;
4621 struct got_histedit_list_entry *hle;
4622 static char msg[80];
4625 if (TAILQ_EMPTY(histedit_cmds))
4626 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4628 SIMPLEQ_FOREACH(qid, commits, entry) {
4629 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4630 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4634 err = got_object_id_str(&id_str, qid->id);
4637 snprintf(msg, sizeof(msg),
4638 "commit %s missing from histedit script", id_str);
4640 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4644 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4645 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4646 "last commit in histedit script cannot be folded");
4651 static const struct got_error *
4652 cmd_histedit(int argc, char *argv[])
4654 const struct got_error *error = NULL;
4655 struct got_worktree *worktree = NULL;
4656 struct got_repository *repo = NULL;
4658 struct got_reference *branch = NULL;
4659 struct got_reference *tmp_branch = NULL;
4660 struct got_object_id *resume_commit_id = NULL;
4661 struct got_object_id *base_commit_id = NULL;
4662 struct got_object_id *head_commit_id = NULL;
4663 struct got_commit_object *commit = NULL;
4664 int ch, rebase_in_progress = 0;
4665 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4666 const char *edit_script_path = NULL;
4667 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4668 struct got_object_id_queue commits;
4669 struct got_pathlist_head merged_paths;
4670 const struct got_object_id_queue *parent_ids;
4671 struct got_object_qid *pid;
4672 struct got_histedit_list histedit_cmds;
4673 struct got_histedit_list_entry *hle;
4675 SIMPLEQ_INIT(&commits);
4676 TAILQ_INIT(&histedit_cmds);
4677 TAILQ_INIT(&merged_paths);
4679 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4688 edit_script_path = optarg;
4700 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4701 "unveil", NULL) == -1)
4704 if (abort_edit && continue_edit)
4710 * This command cannot apply unveil(2) in all cases because the
4711 * user may choose to run an editor to edit the histedit script
4712 * and to edit individual commit log messages.
4713 * unveil(2) traverses exec(2); if an editor is used we have to
4714 * apply unveil after edit script and log messages have been written.
4715 * XXX TODO: Make use of unveil(2) where possible.
4718 cwd = getcwd(NULL, 0);
4720 error = got_error_from_errno("getcwd");
4723 error = got_worktree_open(&worktree, cwd);
4727 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4731 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4734 if (rebase_in_progress) {
4735 error = got_error(GOT_ERR_REBASING);
4739 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4743 if (edit_in_progress && abort_edit) {
4745 error = got_worktree_histedit_continue(&resume_commit_id,
4746 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4749 printf("Switching work tree to %s\n",
4750 got_ref_get_symref_target(branch));
4751 error = got_worktree_histedit_abort(worktree, repo,
4752 branch, base_commit_id, update_progress, &did_something);
4755 printf("Histedit of %s aborted\n",
4756 got_ref_get_symref_target(branch));
4757 goto done; /* nothing else to do */
4758 } else if (abort_edit) {
4759 error = got_error(GOT_ERR_NOT_HISTEDIT);
4763 if (continue_edit) {
4766 if (!edit_in_progress) {
4767 error = got_error(GOT_ERR_NOT_HISTEDIT);
4771 error = got_worktree_get_histedit_list_path(&path, worktree);
4775 error = histedit_load_list(&histedit_cmds, path, repo);
4780 error = got_worktree_histedit_continue(&resume_commit_id,
4781 &tmp_branch, &branch, &base_commit_id, worktree, repo);
4785 error = got_ref_resolve(&head_commit_id, repo, branch);
4789 error = got_object_open_as_commit(&commit, repo,
4793 parent_ids = got_object_commit_get_parent_ids(commit);
4794 pid = SIMPLEQ_FIRST(parent_ids);
4795 error = collect_commits_to_rebase(&commits, head_commit_id,
4796 pid->id, base_commit_id,
4797 got_worktree_get_path_prefix(worktree), repo);
4798 got_object_commit_close(commit);
4803 if (edit_in_progress) {
4804 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4808 error = got_ref_open(&branch, repo,
4809 got_worktree_get_head_ref_name(worktree), 0);
4813 error = got_ref_resolve(&head_commit_id, repo, branch);
4817 error = got_object_open_as_commit(&commit, repo,
4821 parent_ids = got_object_commit_get_parent_ids(commit);
4822 pid = SIMPLEQ_FIRST(parent_ids);
4823 error = collect_commits_to_rebase(&commits, head_commit_id,
4824 pid->id, got_worktree_get_base_commit_id(worktree),
4825 got_worktree_get_path_prefix(worktree), repo);
4826 got_object_commit_close(commit);
4831 if (edit_script_path) {
4832 error = histedit_load_list(&histedit_cmds,
4833 edit_script_path, repo);
4837 error = histedit_edit_script(&histedit_cmds, &commits,
4844 error = histedit_save_list(&histedit_cmds, worktree,
4849 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4850 &base_commit_id, worktree, repo);
4856 error = histedit_check_script(&histedit_cmds, &commits, repo);
4860 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
4861 if (resume_commit_id) {
4862 if (got_object_id_cmp(hle->commit_id,
4863 resume_commit_id) != 0)
4866 resume_commit_id = NULL;
4867 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
4868 hle->cmd->code == GOT_HISTEDIT_FOLD) {
4869 error = histedit_skip_commit(hle, worktree,
4872 error = histedit_commit(NULL, worktree,
4873 tmp_branch, hle, repo);
4880 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
4881 error = histedit_skip_commit(hle, worktree, repo);
4887 error = got_object_open_as_commit(&commit, repo,
4891 parent_ids = got_object_commit_get_parent_ids(commit);
4892 pid = SIMPLEQ_FIRST(parent_ids);
4894 error = got_worktree_histedit_merge_files(&merged_paths,
4895 worktree, pid->id, hle->commit_id, repo, rebase_progress,
4896 &rebase_status, check_cancelled, NULL);
4899 got_object_commit_close(commit);
4902 if (rebase_status == GOT_STATUS_CONFLICT) {
4903 got_worktree_rebase_pathlist_free(&merged_paths);
4907 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
4909 error = got_object_id_str(&id_str, hle->commit_id);
4912 printf("Stopping histedit for amending commit %s\n",
4915 got_worktree_rebase_pathlist_free(&merged_paths);
4916 error = got_worktree_histedit_postpone(worktree);
4920 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
4921 error = histedit_skip_commit(hle, worktree, repo);
4927 error = histedit_commit(&merged_paths, worktree, tmp_branch,
4929 got_worktree_rebase_pathlist_free(&merged_paths);
4934 if (rebase_status == GOT_STATUS_CONFLICT) {
4935 error = got_worktree_histedit_postpone(worktree);
4938 error = got_error_msg(GOT_ERR_CONFLICTS,
4939 "conflicts must be resolved before rebasing can continue");
4941 error = histedit_complete(worktree, tmp_branch, branch, repo);
4943 got_object_id_queue_free(&commits);
4944 free(head_commit_id);
4945 free(base_commit_id);
4946 free(resume_commit_id);
4948 got_object_commit_close(commit);
4950 got_ref_close(branch);
4952 got_ref_close(tmp_branch);
4954 got_worktree_close(worktree);
4956 got_repo_close(repo);