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_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
59 catch_sigint(int signo)
65 catch_sigpipe(int signo)
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
99 static const struct got_error* cmd_init(int, char *[]);
100 static const struct got_error* cmd_import(int, char *[]);
101 static const struct got_error* cmd_checkout(int, char *[]);
102 static const struct got_error* cmd_update(int, char *[]);
103 static const struct got_error* cmd_log(int, char *[]);
104 static const struct got_error* cmd_diff(int, char *[]);
105 static const struct got_error* cmd_blame(int, char *[]);
106 static const struct got_error* cmd_tree(int, char *[]);
107 static const struct got_error* cmd_status(int, char *[]);
108 static const struct got_error* cmd_ref(int, char *[]);
109 static const struct got_error* cmd_branch(int, char *[]);
110 static const struct got_error* cmd_add(int, char *[]);
111 static const struct got_error* cmd_remove(int, char *[]);
112 static const struct got_error* cmd_revert(int, char *[]);
113 static const struct got_error* cmd_commit(int, char *[]);
114 static const struct got_error* cmd_cherrypick(int, char *[]);
115 static const struct got_error* cmd_backout(int, char *[]);
116 static const struct got_error* cmd_rebase(int, char *[]);
117 static const struct got_error* cmd_histedit(int, char *[]);
119 static struct got_cmd got_commands[] = {
120 { "init", cmd_init, usage_init, "" },
121 { "import", cmd_import, usage_import, "" },
122 { "checkout", cmd_checkout, usage_checkout, "co" },
123 { "update", cmd_update, usage_update, "up" },
124 { "log", cmd_log, usage_log, "" },
125 { "diff", cmd_diff, usage_diff, "" },
126 { "blame", cmd_blame, usage_blame, "" },
127 { "tree", cmd_tree, usage_tree, "" },
128 { "status", cmd_status, usage_status, "st" },
129 { "ref", cmd_ref, usage_ref, "" },
130 { "branch", cmd_branch, usage_branch, "br" },
131 { "add", cmd_add, usage_add, "" },
132 { "remove", cmd_remove, usage_remove, "rm" },
133 { "revert", cmd_revert, usage_revert, "rv" },
134 { "commit", cmd_commit, usage_commit, "ci" },
135 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
136 { "backout", cmd_backout, usage_backout, "bo" },
137 { "rebase", cmd_rebase, usage_rebase, "rb" },
138 { "histedit", cmd_histedit, usage_histedit, "he" },
146 fprintf(stderr, "commands:");
147 for (i = 0; i < nitems(got_commands); i++) {
148 struct got_cmd *cmd = &got_commands[i];
149 fprintf(stderr, " %s", cmd->cmd_name);
155 main(int argc, char *argv[])
160 int hflag = 0, Vflag = 0;
162 setlocale(LC_CTYPE, "");
164 while ((ch = getopt(argc, argv, "hV")) != -1) {
183 got_version_print_str();
190 signal(SIGINT, catch_sigint);
191 signal(SIGPIPE, catch_sigpipe);
193 for (i = 0; i < nitems(got_commands); i++) {
194 const struct got_error *error;
196 cmd = &got_commands[i];
198 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
199 strcmp(cmd->cmd_alias, argv[0]) != 0)
203 got_commands[i].cmd_usage();
205 error = got_commands[i].cmd_main(argc, argv);
206 if (error && !(sigint_received || sigpipe_received)) {
207 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
214 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
222 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 static const struct got_error *
230 get_editor(char **abspath)
232 const struct got_error *err = NULL;
235 editor = getenv("VISUAL");
237 editor = getenv("EDITOR");
240 err = got_path_find_prog(abspath, editor);
245 if (*abspath == NULL) {
246 *abspath = strdup("/bin/ed");
247 if (*abspath == NULL)
248 return got_error_from_errno("strdup");
254 static const struct got_error *
255 apply_unveil(const char *repo_path, int repo_read_only,
256 const char *worktree_path)
258 const struct got_error *err;
261 if (unveil("gmon.out", "rwc") != 0)
262 return got_error_from_errno2("unveil", "gmon.out");
264 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 return got_error_from_errno2("unveil", repo_path);
267 if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 return got_error_from_errno2("unveil", worktree_path);
270 if (unveil("/tmp", "rwc") != 0)
271 return got_error_from_errno2("unveil", "/tmp");
273 err = got_privsep_unveil_exec_helpers();
277 if (unveil(NULL, NULL) != 0)
278 return got_error_from_errno("unveil");
286 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
290 static const struct got_error *
291 cmd_init(int argc, char *argv[])
293 const struct got_error *error = NULL;
294 char *repo_path = NULL;
297 while ((ch = getopt(argc, argv, "")) != -1) {
309 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
315 repo_path = strdup(argv[0]);
316 if (repo_path == NULL)
317 return got_error_from_errno("strdup");
319 got_path_strip_trailing_slashes(repo_path);
321 error = got_path_mkdir(repo_path);
323 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
326 error = apply_unveil(repo_path, 0, NULL);
330 error = got_repo_init(repo_path);
342 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
343 "[-r repository-path] [-I pattern] path\n", getprogname());
348 spawn_editor(const char *editor, const char *file)
351 sig_t sighup, sigint, sigquit;
354 sighup = signal(SIGHUP, SIG_IGN);
355 sigint = signal(SIGINT, SIG_IGN);
356 sigquit = signal(SIGQUIT, SIG_IGN);
358 switch (pid = fork()) {
362 execl(editor, editor, file, (char *)NULL);
366 while (waitpid(pid, &st, 0) == -1)
371 (void)signal(SIGHUP, sighup);
372 (void)signal(SIGINT, sigint);
373 (void)signal(SIGQUIT, sigquit);
375 if (!WIFEXITED(st)) {
380 return WEXITSTATUS(st);
383 static const struct got_error *
384 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
385 const char *initial_content)
387 const struct got_error *err = NULL;
391 int content_changed = 0;
396 if (stat(logmsg_path, &st) == -1)
397 return got_error_from_errno2("stat", logmsg_path);
399 if (spawn_editor(editor, logmsg_path) == -1)
400 return got_error_from_errno("failed spawning editor");
402 if (stat(logmsg_path, &st2) == -1)
403 return got_error_from_errno("stat");
405 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
406 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
407 "no changes made to commit message, aborting");
409 *logmsg = malloc(st2.st_size + 1);
411 return got_error_from_errno("malloc");
415 fp = fopen(logmsg_path, "r");
417 err = got_error_from_errno("fopen");
420 while (fgets(buf, sizeof(buf), fp) != NULL) {
421 if (!content_changed && strcmp(buf, initial_content) != 0)
423 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
424 continue; /* remove comments and leading empty lines */
425 len = strlcat(*logmsg, buf, st2.st_size);
429 while (len > 0 && (*logmsg)[len - 1] == '\n') {
430 (*logmsg)[len - 1] = '\0';
434 if (len == 0 || !content_changed)
435 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
436 "commit message cannot be empty, aborting");
445 static const struct got_error *
446 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
447 const char *branch_name)
449 char *initial_content = NULL, *logmsg_path = NULL;
450 const struct got_error *err = NULL;
453 if (asprintf(&initial_content,
454 "\n# %s to be imported to branch %s\n", path_dir,
456 return got_error_from_errno("asprintf");
458 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
462 dprintf(fd, initial_content);
465 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
467 free(initial_content);
472 static const struct got_error *
473 import_progress(void *arg, const char *path)
475 printf("A %s\n", path);
479 static const struct got_error *
480 cmd_import(int argc, char *argv[])
482 const struct got_error *error = NULL;
483 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
485 const char *got_author = getenv("GOT_AUTHOR");
486 const char *branch_name = "master";
487 char *refname = NULL, *id_str = NULL;
488 struct got_repository *repo = NULL;
489 struct got_reference *branch_ref = NULL, *head_ref = NULL;
490 struct got_object_id *new_commit_id = NULL;
492 struct got_pathlist_head ignores;
493 struct got_pathlist_entry *pe;
495 TAILQ_INIT(&ignores);
497 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
500 branch_name = optarg;
503 logmsg = strdup(optarg);
504 if (logmsg == NULL) {
505 error = got_error_from_errno("strdup");
510 repo_path = realpath(optarg, NULL);
511 if (repo_path == NULL) {
512 error = got_error_from_errno("realpath");
517 if (optarg[0] == '\0')
519 error = got_pathlist_insert(&pe, &ignores, optarg,
534 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
541 if (got_author == NULL) {
542 /* TODO: Look current user up in password database */
543 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
547 if (repo_path == NULL) {
548 repo_path = getcwd(NULL, 0);
549 if (repo_path == NULL)
550 return got_error_from_errno("getcwd");
552 got_path_strip_trailing_slashes(repo_path);
553 error = got_repo_open(&repo, repo_path);
557 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
558 error = got_error_from_errno("asprintf");
562 error = got_ref_open(&branch_ref, repo, refname, 0);
564 if (error->code != GOT_ERR_NOT_REF)
567 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
568 "import target branch already exists");
572 path_dir = realpath(argv[0], NULL);
573 if (path_dir == NULL) {
574 error = got_error_from_errno("realpath");
577 got_path_strip_trailing_slashes(path_dir);
580 * unveil(2) traverses exec(2); if an editor is used we have
581 * to apply unveil after the log message has been written.
583 if (logmsg == NULL || strlen(logmsg) == 0) {
584 error = get_editor(&editor);
587 error = collect_import_msg(&logmsg, editor, path_dir, refname);
592 if (unveil(path_dir, "r") != 0)
593 return got_error_from_errno2("unveil", path_dir);
595 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
599 error = got_repo_import(&new_commit_id, path_dir, logmsg,
600 got_author, &ignores, repo, import_progress, NULL);
604 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
608 error = got_ref_write(branch_ref, repo);
612 error = got_object_id_str(&id_str, new_commit_id);
616 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
618 if (error->code != GOT_ERR_NOT_REF)
621 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
626 error = got_ref_write(head_ref, repo);
631 printf("Created branch %s with commit %s\n",
632 got_ref_get_name(branch_ref), id_str);
640 got_ref_close(branch_ref);
642 got_ref_close(head_ref);
649 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
650 "[-p prefix] repository-path [worktree-path]\n", getprogname());
654 static const struct got_error *
655 checkout_progress(void *arg, unsigned char status, const char *path)
657 char *worktree_path = arg;
659 /* Base commit bump happens silently. */
660 if (status == GOT_STATUS_BUMP_BASE)
663 while (path[0] == '/')
666 printf("%c %s/%s\n", status, worktree_path, path);
670 static const struct got_error *
671 check_cancelled(void *arg)
673 if (sigint_received || sigpipe_received)
674 return got_error(GOT_ERR_CANCELLED);
678 static const struct got_error *
679 check_linear_ancestry(struct got_object_id *commit_id,
680 struct got_object_id *base_commit_id, struct got_repository *repo)
682 const struct got_error *err = NULL;
683 struct got_object_id *yca_id;
685 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
686 commit_id, base_commit_id, repo);
691 return got_error(GOT_ERR_ANCESTRY);
694 * Require a straight line of history between the target commit
695 * and the work tree's base commit.
697 * Non-linear situations such as this require a rebase:
699 * (commit) D F (base_commit)
707 * 'got update' only handles linear cases:
708 * Update forwards in time: A (base/yca) - B - C - D (commit)
709 * Update backwards in time: D (base) - C - B - A (commit/yca)
711 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
712 got_object_id_cmp(base_commit_id, yca_id) != 0)
713 return got_error(GOT_ERR_ANCESTRY);
719 static const struct got_error *
720 check_same_branch(struct got_object_id *commit_id,
721 struct got_reference *head_ref, struct got_object_id *yca_id,
722 struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_commit_graph *graph = NULL;
726 struct got_object_id *head_commit_id = NULL;
727 int is_same_branch = 0;
729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
733 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
737 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
742 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
746 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
751 struct got_object_id *id;
752 err = got_commit_graph_iter_next(&id, graph);
754 if (err->code == GOT_ERR_ITER_COMPLETED) {
757 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
759 err = got_commit_graph_fetch_commits(graph, 1,
766 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
768 if (got_object_id_cmp(id, commit_id) == 0) {
776 got_commit_graph_close(graph);
777 free(head_commit_id);
778 if (!err && !is_same_branch)
779 err = got_error(GOT_ERR_ANCESTRY);
783 static const struct got_error *
784 resolve_commit_arg(struct got_object_id **commit_id,
785 const char *commit_id_arg, struct got_repository *repo)
787 const struct got_error *err;
788 struct got_reference *ref;
790 err = got_ref_open(&ref, repo, commit_id_arg, 0);
792 err = got_ref_resolve(commit_id, repo, ref);
795 if (err->code != GOT_ERR_NOT_REF)
797 err = got_repo_match_object_id_prefix(commit_id,
798 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
803 static const struct got_error *
804 cmd_checkout(int argc, char *argv[])
806 const struct got_error *error = NULL;
807 struct got_repository *repo = NULL;
808 struct got_reference *head_ref = NULL;
809 struct got_worktree *worktree = NULL;
810 char *repo_path = NULL;
811 char *worktree_path = NULL;
812 const char *path_prefix = "";
813 const char *branch_name = GOT_REF_HEAD;
814 char *commit_id_str = NULL;
815 int ch, same_path_prefix;
816 struct got_pathlist_head paths;
820 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
823 branch_name = optarg;
826 commit_id_str = strdup(optarg);
827 if (commit_id_str == NULL)
828 return got_error_from_errno("strdup");
831 path_prefix = optarg;
843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
844 "unveil", NULL) == -1)
848 char *cwd, *base, *dotgit;
849 repo_path = realpath(argv[0], NULL);
850 if (repo_path == NULL)
851 return got_error_from_errno2("realpath", argv[0]);
852 cwd = getcwd(NULL, 0);
854 error = got_error_from_errno("getcwd");
857 if (path_prefix[0]) {
858 base = basename(path_prefix);
860 error = got_error_from_errno2("basename",
865 base = basename(repo_path);
867 error = got_error_from_errno2("basename",
872 dotgit = strstr(base, ".git");
875 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
876 error = got_error_from_errno("asprintf");
881 } else if (argc == 2) {
882 repo_path = realpath(argv[0], NULL);
883 if (repo_path == NULL) {
884 error = got_error_from_errno2("realpath", argv[0]);
887 worktree_path = realpath(argv[1], NULL);
888 if (worktree_path == NULL) {
889 if (errno != ENOENT) {
890 error = got_error_from_errno2("realpath",
894 worktree_path = strdup(argv[1]);
895 if (worktree_path == NULL) {
896 error = got_error_from_errno("strdup");
903 got_path_strip_trailing_slashes(repo_path);
904 got_path_strip_trailing_slashes(worktree_path);
906 error = got_repo_open(&repo, repo_path);
910 /* Pre-create work tree path for unveil(2) */
911 error = got_path_mkdir(worktree_path);
913 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
915 if (!got_path_dir_is_empty(worktree_path)) {
916 error = got_error_path(worktree_path,
917 GOT_ERR_DIR_NOT_EMPTY);
922 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
926 error = got_ref_open(&head_ref, repo, branch_name, 0);
930 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
931 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
934 error = got_worktree_open(&worktree, worktree_path);
938 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
942 if (!same_path_prefix) {
943 error = got_error(GOT_ERR_PATH_PREFIX);
948 struct got_object_id *commit_id;
949 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
952 error = check_linear_ancestry(commit_id,
953 got_worktree_get_base_commit_id(worktree), repo);
958 error = check_same_branch(commit_id, head_ref, NULL, repo);
961 error = got_worktree_set_base_commit_id(worktree, repo,
968 error = got_pathlist_append(&paths, "", NULL);
971 error = got_worktree_checkout_files(worktree, &paths, repo,
972 checkout_progress, worktree_path, check_cancelled, NULL);
976 printf("Now shut up and hack\n");
979 got_pathlist_free(&paths);
989 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
994 static const struct got_error *
995 update_progress(void *arg, unsigned char status, const char *path)
997 int *did_something = arg;
999 if (status == GOT_STATUS_EXISTS)
1004 /* Base commit bump happens silently. */
1005 if (status == GOT_STATUS_BUMP_BASE)
1008 while (path[0] == '/')
1010 printf("%c %s\n", status, path);
1014 static const struct got_error *
1015 switch_head_ref(struct got_reference *head_ref,
1016 struct got_object_id *commit_id, struct got_worktree *worktree,
1017 struct got_repository *repo)
1019 const struct got_error *err = NULL;
1021 int ref_has_moved = 0;
1023 /* Trivial case: switching between two different references. */
1024 if (strcmp(got_ref_get_name(head_ref),
1025 got_worktree_get_head_ref_name(worktree)) != 0) {
1026 printf("Switching work tree from %s to %s\n",
1027 got_worktree_get_head_ref_name(worktree),
1028 got_ref_get_name(head_ref));
1029 return got_worktree_set_head_ref(worktree, head_ref);
1032 err = check_linear_ancestry(commit_id,
1033 got_worktree_get_base_commit_id(worktree), repo);
1035 if (err->code != GOT_ERR_ANCESTRY)
1042 /* Switching to a rebased branch with the same reference name. */
1043 err = got_object_id_str(&base_id_str,
1044 got_worktree_get_base_commit_id(worktree));
1047 printf("Reference %s now points at a different branch\n",
1048 got_worktree_get_head_ref_name(worktree));
1049 printf("Switching work tree from %s to %s\n", base_id_str,
1050 got_worktree_get_head_ref_name(worktree));
1054 static const struct got_error *
1055 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1057 const struct got_error *err;
1060 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1064 return got_error(GOT_ERR_REBASING);
1066 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1070 return got_error(GOT_ERR_HISTEDIT_BUSY);
1075 static const struct got_error *
1076 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1077 char *argv[], struct got_worktree *worktree)
1079 const struct got_error *err;
1086 return got_error_from_errno("strdup");
1087 return got_pathlist_append(paths, path, NULL);
1090 for (i = 0; i < argc; i++) {
1091 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1094 err = got_pathlist_append(paths, path, NULL);
1104 static const struct got_error *
1105 cmd_update(int argc, char *argv[])
1107 const struct got_error *error = NULL;
1108 struct got_repository *repo = NULL;
1109 struct got_worktree *worktree = NULL;
1110 char *worktree_path = NULL;
1111 struct got_object_id *commit_id = NULL;
1112 char *commit_id_str = NULL;
1113 const char *branch_name = NULL;
1114 struct got_reference *head_ref = NULL;
1115 struct got_pathlist_head paths;
1116 struct got_pathlist_entry *pe;
1117 int ch, did_something = 0;
1121 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1124 branch_name = optarg;
1127 commit_id_str = strdup(optarg);
1128 if (commit_id_str == NULL)
1129 return got_error_from_errno("strdup");
1141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1142 "unveil", NULL) == -1)
1145 worktree_path = getcwd(NULL, 0);
1146 if (worktree_path == NULL) {
1147 error = got_error_from_errno("getcwd");
1150 error = got_worktree_open(&worktree, worktree_path);
1154 error = check_rebase_or_histedit_in_progress(worktree);
1158 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 error = apply_unveil(got_repo_get_path(repo), 0,
1167 got_worktree_get_root_path(worktree));
1171 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1172 got_worktree_get_head_ref_name(worktree), 0);
1175 if (commit_id_str == NULL) {
1176 error = got_ref_resolve(&commit_id, repo, head_ref);
1179 error = got_object_id_str(&commit_id_str, commit_id);
1183 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1184 free(commit_id_str);
1185 commit_id_str = NULL;
1188 error = got_object_id_str(&commit_id_str, commit_id);
1194 struct got_object_id *head_commit_id;
1195 TAILQ_FOREACH(pe, &paths, entry) {
1196 if (pe->path_len == 0)
1198 error = got_error_msg(GOT_ERR_BAD_PATH,
1199 "switching between branches requires that "
1200 "the entire work tree gets updated");
1203 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1206 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1207 free(head_commit_id);
1210 error = check_same_branch(commit_id, head_ref, NULL, repo);
1213 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1217 error = check_linear_ancestry(commit_id,
1218 got_worktree_get_base_commit_id(worktree), repo);
1219 if (error != NULL) {
1220 if (error->code == GOT_ERR_ANCESTRY)
1221 error = got_error(GOT_ERR_BRANCH_MOVED);
1224 error = check_same_branch(commit_id, head_ref, NULL, repo);
1229 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1231 error = got_worktree_set_base_commit_id(worktree, repo,
1237 error = got_worktree_checkout_files(worktree, &paths, repo,
1238 update_progress, &did_something, check_cancelled, NULL);
1243 printf("Updated to commit %s\n", commit_id_str);
1245 printf("Already up-to-date\n");
1247 free(worktree_path);
1248 TAILQ_FOREACH(pe, &paths, entry)
1249 free((char *)pe->path);
1250 got_pathlist_free(&paths);
1252 free(commit_id_str);
1256 static const struct got_error *
1257 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1258 int diff_context, struct got_repository *repo)
1260 const struct got_error *err = NULL;
1261 struct got_tree_object *tree1 = NULL, *tree2;
1262 struct got_object_qid *qid;
1263 char *id_str1 = NULL, *id_str2;
1264 struct got_diff_blob_output_unidiff_arg arg;
1266 err = got_object_open_as_tree(&tree2, repo,
1267 got_object_commit_get_tree_id(commit));
1271 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1273 struct got_commit_object *pcommit;
1275 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1279 err = got_object_open_as_tree(&tree1, repo,
1280 got_object_commit_get_tree_id(pcommit));
1281 got_object_commit_close(pcommit);
1285 err = got_object_id_str(&id_str1, qid->id);
1290 err = got_object_id_str(&id_str2, id);
1294 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1295 arg.diff_context = diff_context;
1296 arg.outfile = stdout;
1297 err = got_diff_tree(tree1, tree2, "", "", repo,
1298 got_diff_blob_output_unidiff, &arg, 1);
1301 got_object_tree_close(tree1);
1302 got_object_tree_close(tree2);
1309 get_datestr(time_t *time, char *datebuf)
1311 char *p, *s = ctime_r(time, datebuf);
1312 p = strchr(s, '\n');
1318 static const struct got_error *
1319 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1320 struct got_repository *repo, int show_patch, int diff_context,
1321 struct got_reflist_head *refs)
1323 const struct got_error *err = NULL;
1324 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1326 time_t committer_time;
1327 const char *author, *committer;
1328 char *refs_str = NULL;
1329 struct got_reflist_entry *re;
1331 SIMPLEQ_FOREACH(re, refs, entry) {
1334 if (got_object_id_cmp(re->id, id) != 0)
1336 name = got_ref_get_name(re->ref);
1337 if (strcmp(name, GOT_REF_HEAD) == 0)
1339 if (strncmp(name, "refs/", 5) == 0)
1341 if (strncmp(name, "got/", 4) == 0)
1343 if (strncmp(name, "heads/", 6) == 0)
1345 if (strncmp(name, "remotes/", 8) == 0)
1348 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1350 err = got_error_from_errno("asprintf");
1356 err = got_object_id_str(&id_str, id);
1360 printf("-----------------------------------------------\n");
1361 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1362 refs_str ? refs_str : "", refs_str ? ")" : "");
1367 printf("from: %s\n", got_object_commit_get_author(commit));
1368 committer_time = got_object_commit_get_committer_time(commit);
1369 datestr = get_datestr(&committer_time, datebuf);
1370 printf("date: %s UTC\n", datestr);
1371 author = got_object_commit_get_author(commit);
1372 committer = got_object_commit_get_committer(commit);
1373 if (strcmp(author, committer) != 0)
1374 printf("via: %s\n", committer);
1375 if (got_object_commit_get_nparents(commit) > 1) {
1376 const struct got_object_id_queue *parent_ids;
1377 struct got_object_qid *qid;
1379 parent_ids = got_object_commit_get_parent_ids(commit);
1380 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1381 err = got_object_id_str(&id_str, qid->id);
1384 printf("parent %d: %s\n", n++, id_str);
1389 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1390 if (logmsg0 == NULL)
1391 return got_error_from_errno("strdup");
1395 line = strsep(&logmsg, "\n");
1397 printf(" %s\n", line);
1402 err = print_patch(commit, id, diff_context, repo);
1407 if (fflush(stdout) != 0 && err == NULL)
1408 err = got_error_from_errno("fflush");
1412 static const struct got_error *
1413 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1414 char *path, int show_patch, int diff_context, int limit,
1415 int first_parent_traversal, struct got_reflist_head *refs)
1417 const struct got_error *err;
1418 struct got_commit_graph *graph;
1420 err = got_commit_graph_open(&graph, root_id, path,
1421 first_parent_traversal, repo);
1424 err = got_commit_graph_iter_start(graph, root_id, repo);
1428 struct got_commit_object *commit;
1429 struct got_object_id *id;
1431 if (sigint_received || sigpipe_received)
1434 err = got_commit_graph_iter_next(&id, graph);
1436 if (err->code == GOT_ERR_ITER_COMPLETED) {
1440 if (err->code != GOT_ERR_ITER_NEED_MORE)
1442 err = got_commit_graph_fetch_commits(graph, 1, repo);
1451 err = got_object_open_as_commit(&commit, repo, id);
1454 err = print_commit(commit, id, repo, show_patch, diff_context,
1456 got_object_commit_close(commit);
1457 if (err || (limit && --limit == 0))
1461 got_commit_graph_close(graph);
1468 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1469 "[-r repository-path] [path]\n", getprogname());
1473 static const struct got_error *
1474 cmd_log(int argc, char *argv[])
1476 const struct got_error *error;
1477 struct got_repository *repo = NULL;
1478 struct got_worktree *worktree = NULL;
1479 struct got_commit_object *commit = NULL;
1480 struct got_object_id *id = NULL;
1481 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1482 char *start_commit = NULL;
1483 int diff_context = 3, ch;
1484 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1486 struct got_reflist_head refs;
1488 SIMPLEQ_INIT(&refs);
1491 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1497 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1503 start_commit = optarg;
1506 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1509 err(1, "-C option %s", errstr);
1512 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1514 err(1, "-l option %s", errstr);
1517 first_parent_traversal = 1;
1520 repo_path = realpath(optarg, NULL);
1521 if (repo_path == NULL)
1522 err(1, "-r option");
1523 got_path_strip_trailing_slashes(repo_path);
1534 cwd = getcwd(NULL, 0);
1536 error = got_error_from_errno("getcwd");
1540 error = got_worktree_open(&worktree, cwd);
1541 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1548 error = got_error_from_errno("strdup");
1551 } else if (argc == 1) {
1553 error = got_worktree_resolve_path(&path, worktree,
1558 path = strdup(argv[0]);
1560 error = got_error_from_errno("strdup");
1567 if (repo_path == NULL) {
1568 repo_path = worktree ?
1569 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1571 if (repo_path == NULL) {
1572 error = got_error_from_errno("strdup");
1576 error = got_repo_open(&repo, repo_path);
1580 error = apply_unveil(got_repo_get_path(repo), 1,
1581 worktree ? got_worktree_get_root_path(worktree) : NULL);
1585 if (start_commit == NULL) {
1586 struct got_reference *head_ref;
1587 error = got_ref_open(&head_ref, repo,
1588 worktree ? got_worktree_get_head_ref_name(worktree)
1592 error = got_ref_resolve(&id, repo, head_ref);
1593 got_ref_close(head_ref);
1596 error = got_object_open_as_commit(&commit, repo, id);
1598 struct got_reference *ref;
1599 error = got_ref_open(&ref, repo, start_commit, 0);
1600 if (error == NULL) {
1602 error = got_ref_resolve(&id, repo, ref);
1606 error = got_object_get_type(&obj_type, repo, id);
1609 if (obj_type == GOT_OBJ_TYPE_TAG) {
1610 struct got_tag_object *tag;
1611 error = got_object_open_as_tag(&tag, repo, id);
1614 if (got_object_tag_get_object_type(tag) !=
1615 GOT_OBJ_TYPE_COMMIT) {
1616 got_object_tag_close(tag);
1617 error = got_error(GOT_ERR_OBJ_TYPE);
1621 id = got_object_id_dup(
1622 got_object_tag_get_object_id(tag));
1624 error = got_error_from_errno(
1625 "got_object_id_dup");
1626 got_object_tag_close(tag);
1629 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1630 error = got_error(GOT_ERR_OBJ_TYPE);
1633 error = got_object_open_as_commit(&commit, repo, id);
1637 if (commit == NULL) {
1638 error = got_repo_match_object_id_prefix(&id,
1639 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1647 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 path = in_repo_path;
1655 error = got_ref_list(&refs, repo);
1659 error = print_commits(id, repo, path, show_patch,
1660 diff_context, limit, first_parent_traversal, &refs);
1667 got_worktree_close(worktree);
1669 const struct got_error *repo_error;
1670 repo_error = got_repo_close(repo);
1674 got_ref_list_free(&refs);
1681 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1682 "[object1 object2 | path]\n", getprogname());
1686 struct print_diff_arg {
1687 struct got_repository *repo;
1688 struct got_worktree *worktree;
1694 static const struct got_error *
1695 print_diff(void *arg, unsigned char status, const char *path,
1696 struct got_object_id *blob_id, struct got_object_id *commit_id)
1698 struct print_diff_arg *a = arg;
1699 const struct got_error *err = NULL;
1700 struct got_blob_object *blob1 = NULL;
1702 char *abspath = NULL;
1705 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1706 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1709 if (!a->header_shown) {
1710 printf("diff %s %s\n", a->id_str,
1711 got_worktree_get_root_path(a->worktree));
1712 a->header_shown = 1;
1715 if (status != GOT_STATUS_ADD) {
1716 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1722 if (status != GOT_STATUS_DELETE) {
1723 if (asprintf(&abspath, "%s/%s",
1724 got_worktree_get_root_path(a->worktree), path) == -1) {
1725 err = got_error_from_errno("asprintf");
1729 f2 = fopen(abspath, "r");
1731 err = got_error_from_errno2("fopen", abspath);
1734 if (lstat(abspath, &sb) == -1) {
1735 err = got_error_from_errno2("lstat", abspath);
1741 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1745 got_object_blob_close(blob1);
1746 if (f2 && fclose(f2) != 0 && err == NULL)
1747 err = got_error_from_errno("fclose");
1752 static const struct got_error *
1753 cmd_diff(int argc, char *argv[])
1755 const struct got_error *error;
1756 struct got_repository *repo = NULL;
1757 struct got_worktree *worktree = NULL;
1758 char *cwd = NULL, *repo_path = NULL;
1759 struct got_object_id *id1 = NULL, *id2 = NULL;
1760 const char *id_str1 = NULL, *id_str2 = NULL;
1761 char *label1 = NULL, *label2 = NULL;
1763 int diff_context = 3, ch;
1768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1773 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1776 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1778 err(1, "-C option %s", errstr);
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1795 cwd = getcwd(NULL, 0);
1797 error = got_error_from_errno("getcwd");
1800 error = got_worktree_open(&worktree, cwd);
1801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1804 if (worktree == NULL) {
1805 error = got_error(GOT_ERR_NOT_WORKTREE);
1810 "-r option can't be used when diffing a work tree");
1811 repo_path = strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL) {
1813 error = got_error_from_errno("strdup");
1817 error = got_worktree_resolve_path(&path, worktree,
1824 error = got_error_from_errno("strdup");
1828 } else if (argc == 2) {
1831 if (worktree && repo_path == NULL) {
1833 strdup(got_worktree_get_repo_path(worktree));
1834 if (repo_path == NULL) {
1835 error = got_error_from_errno("strdup");
1842 if (repo_path == NULL) {
1843 repo_path = getcwd(NULL, 0);
1844 if (repo_path == NULL)
1845 return got_error_from_errno("getcwd");
1848 error = got_repo_open(&repo, repo_path);
1853 error = apply_unveil(got_repo_get_path(repo), 1,
1854 worktree ? got_worktree_get_root_path(worktree) : NULL);
1859 struct print_diff_arg arg;
1860 struct got_pathlist_head paths;
1865 error = got_object_id_str(&id_str,
1866 got_worktree_get_base_commit_id(worktree));
1870 arg.worktree = worktree;
1871 arg.diff_context = diff_context;
1872 arg.id_str = id_str;
1873 arg.header_shown = 0;
1875 error = got_pathlist_append(&paths, path, NULL);
1879 error = got_worktree_status(worktree, &paths, repo, print_diff,
1880 &arg, check_cancelled, NULL);
1882 got_pathlist_free(&paths);
1886 error = got_repo_match_object_id_prefix(&id1, id_str1,
1887 GOT_OBJ_TYPE_ANY, repo);
1889 struct got_reference *ref;
1890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1892 error = got_ref_open(&ref, repo, id_str1, 0);
1895 label1 = strdup(got_ref_get_name(ref));
1896 if (label1 == NULL) {
1897 error = got_error_from_errno("strdup");
1900 error = got_ref_resolve(&id1, repo, ref);
1905 error = got_object_id_str(&label1, id1);
1906 if (label1 == NULL) {
1907 error = got_error_from_errno("strdup");
1912 error = got_repo_match_object_id_prefix(&id2, id_str2,
1913 GOT_OBJ_TYPE_ANY, repo);
1915 struct got_reference *ref;
1916 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1918 error = got_ref_open(&ref, repo, id_str2, 0);
1921 label2 = strdup(got_ref_get_name(ref));
1922 if (label2 == NULL) {
1923 error = got_error_from_errno("strdup");
1926 error = got_ref_resolve(&id2, repo, ref);
1931 error = got_object_id_str(&label2, id2);
1932 if (label2 == NULL) {
1933 error = got_error_from_errno("strdup");
1938 error = got_object_get_type(&type1, repo, id1);
1942 error = got_object_get_type(&type2, repo, id2);
1946 if (type1 != type2) {
1947 error = got_error(GOT_ERR_OBJ_TYPE);
1952 case GOT_OBJ_TYPE_BLOB:
1953 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1954 diff_context, repo, stdout);
1956 case GOT_OBJ_TYPE_TREE:
1957 error = got_diff_objects_as_trees(id1, id2, "", "",
1958 diff_context, repo, stdout);
1960 case GOT_OBJ_TYPE_COMMIT:
1961 printf("diff %s %s\n", label1, label2);
1962 error = got_diff_objects_as_commits(id1, id2, diff_context,
1966 error = got_error(GOT_ERR_OBJ_TYPE);
1976 got_worktree_close(worktree);
1978 const struct got_error *repo_error;
1979 repo_error = got_repo_close(repo);
1990 "usage: %s blame [-c commit] [-r repository-path] path\n",
1995 static const struct got_error *
1996 cmd_blame(int argc, char *argv[])
1998 const struct got_error *error;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2002 struct got_object_id *commit_id = NULL;
2003 char *commit_id_str = NULL;
2007 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2012 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2015 commit_id_str = optarg;
2018 repo_path = realpath(optarg, NULL);
2019 if (repo_path == NULL)
2020 err(1, "-r option");
2021 got_path_strip_trailing_slashes(repo_path);
2037 cwd = getcwd(NULL, 0);
2039 error = got_error_from_errno("getcwd");
2042 if (repo_path == NULL) {
2043 error = got_worktree_open(&worktree, cwd);
2044 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2050 strdup(got_worktree_get_repo_path(worktree));
2051 if (repo_path == NULL)
2052 error = got_error_from_errno("strdup");
2056 repo_path = strdup(cwd);
2057 if (repo_path == NULL) {
2058 error = got_error_from_errno("strdup");
2064 error = got_repo_open(&repo, repo_path);
2068 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2073 const char *prefix = got_worktree_get_path_prefix(worktree);
2074 char *p, *worktree_subdir = cwd +
2075 strlen(got_worktree_get_root_path(worktree));
2076 if (asprintf(&p, "%s%s%s%s%s",
2077 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2078 worktree_subdir, worktree_subdir[0] ? "/" : "",
2080 error = got_error_from_errno("asprintf");
2083 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2086 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2091 if (commit_id_str == NULL) {
2092 struct got_reference *head_ref;
2093 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2096 error = got_ref_resolve(&commit_id, repo, head_ref);
2097 got_ref_close(head_ref);
2101 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2106 error = got_blame(in_repo_path, commit_id, repo, stdout);
2113 got_worktree_close(worktree);
2115 const struct got_error *repo_error;
2116 repo_error = got_repo_close(repo);
2127 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2133 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2134 const char *root_path)
2136 int is_root_path = (strcmp(path, root_path) == 0);
2138 path += strlen(root_path);
2139 while (path[0] == '/')
2142 printf("%s%s%s%s%s\n", id ? id : "", path,
2143 is_root_path ? "" : "/", te->name,
2144 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2147 static const struct got_error *
2148 print_tree(const char *path, struct got_object_id *commit_id,
2149 int show_ids, int recurse, const char *root_path,
2150 struct got_repository *repo)
2152 const struct got_error *err = NULL;
2153 struct got_object_id *tree_id = NULL;
2154 struct got_tree_object *tree = NULL;
2155 const struct got_tree_entries *entries;
2156 struct got_tree_entry *te;
2158 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2162 err = got_object_open_as_tree(&tree, repo, tree_id);
2165 entries = got_object_tree_get_entries(tree);
2166 te = SIMPLEQ_FIRST(&entries->head);
2170 if (sigint_received || sigpipe_received)
2175 err = got_object_id_str(&id_str, te->id);
2178 if (asprintf(&id, "%s ", id_str) == -1) {
2179 err = got_error_from_errno("asprintf");
2185 print_entry(te, id, path, root_path);
2188 if (recurse && S_ISDIR(te->mode)) {
2190 if (asprintf(&child_path, "%s%s%s", path,
2191 path[0] == '/' && path[1] == '\0' ? "" : "/",
2193 err = got_error_from_errno("asprintf");
2196 err = print_tree(child_path, commit_id, show_ids, 1,
2203 te = SIMPLEQ_NEXT(te, entry);
2207 got_object_tree_close(tree);
2212 static const struct got_error *
2213 cmd_tree(int argc, char *argv[])
2215 const struct got_error *error;
2216 struct got_repository *repo = NULL;
2217 struct got_worktree *worktree = NULL;
2219 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2220 struct got_object_id *commit_id = NULL;
2221 char *commit_id_str = NULL;
2222 int show_ids = 0, recurse = 0;
2226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2231 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2234 commit_id_str = optarg;
2237 repo_path = realpath(optarg, NULL);
2238 if (repo_path == NULL)
2239 err(1, "-r option");
2240 got_path_strip_trailing_slashes(repo_path);
2264 cwd = getcwd(NULL, 0);
2266 error = got_error_from_errno("getcwd");
2269 if (repo_path == NULL) {
2270 error = got_worktree_open(&worktree, cwd);
2271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2277 strdup(got_worktree_get_repo_path(worktree));
2278 if (repo_path == NULL)
2279 error = got_error_from_errno("strdup");
2283 repo_path = strdup(cwd);
2284 if (repo_path == NULL) {
2285 error = got_error_from_errno("strdup");
2291 error = got_repo_open(&repo, repo_path);
2295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2301 char *p, *worktree_subdir = cwd +
2302 strlen(got_worktree_get_root_path(worktree));
2303 if (asprintf(&p, "%s/%s",
2304 got_worktree_get_path_prefix(worktree),
2305 worktree_subdir) == -1) {
2306 error = got_error_from_errno("asprintf");
2309 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2316 if (in_repo_path == NULL) {
2317 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2322 if (commit_id_str == NULL) {
2323 struct got_reference *head_ref;
2324 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2327 error = got_ref_resolve(&commit_id, repo, head_ref);
2328 got_ref_close(head_ref);
2332 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2337 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2338 in_repo_path, repo);
2345 got_worktree_close(worktree);
2347 const struct got_error *repo_error;
2348 repo_error = got_repo_close(repo);
2358 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2362 static const struct got_error *
2363 print_status(void *arg, unsigned char status, const char *path,
2364 struct got_object_id *blob_id, struct got_object_id *commit_id)
2366 printf("%c %s\n", status, path);
2370 static const struct got_error *
2371 cmd_status(int argc, char *argv[])
2373 const struct got_error *error = NULL;
2374 struct got_repository *repo = NULL;
2375 struct got_worktree *worktree = NULL;
2377 struct got_pathlist_head paths;
2378 struct got_pathlist_entry *pe;
2383 while ((ch = getopt(argc, argv, "")) != -1) {
2395 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2399 cwd = getcwd(NULL, 0);
2401 error = got_error_from_errno("getcwd");
2405 error = got_worktree_open(&worktree, cwd);
2409 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2417 error = apply_unveil(got_repo_get_path(repo), 1,
2418 got_worktree_get_root_path(worktree));
2422 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2423 check_cancelled, NULL);
2425 TAILQ_FOREACH(pe, &paths, entry)
2426 free((char *)pe->path);
2427 got_pathlist_free(&paths);
2436 "usage: %s ref [-r repository] -l | -d name | name target\n",
2441 static const struct got_error *
2442 list_refs(struct got_repository *repo)
2444 static const struct got_error *err = NULL;
2445 struct got_reflist_head refs;
2446 struct got_reflist_entry *re;
2448 SIMPLEQ_INIT(&refs);
2449 err = got_ref_list(&refs, repo);
2453 SIMPLEQ_FOREACH(re, &refs, entry) {
2455 refstr = got_ref_to_str(re->ref);
2457 return got_error_from_errno("got_ref_to_str");
2458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2462 got_ref_list_free(&refs);
2466 static const struct got_error *
2467 delete_ref(struct got_repository *repo, const char *refname)
2469 const struct got_error *err = NULL;
2470 struct got_reference *ref;
2472 err = got_ref_open(&ref, repo, refname, 0);
2476 err = got_ref_delete(ref, repo);
2481 static const struct got_error *
2482 add_ref(struct got_repository *repo, const char *refname, const char *target)
2484 const struct got_error *err = NULL;
2485 struct got_object_id *id;
2486 struct got_reference *ref = NULL;
2489 * Don't let the user create a reference named '-'.
2490 * While technically a valid reference name, this case is usually
2491 * an unintended typo.
2493 if (refname[0] == '-' && refname[1] == '\0')
2494 return got_error(GOT_ERR_BAD_REF_NAME);
2496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2499 struct got_reference *target_ref;
2501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2503 err = got_ref_open(&target_ref, repo, target, 0);
2506 err = got_ref_resolve(&id, repo, target_ref);
2507 got_ref_close(target_ref);
2512 err = got_ref_alloc(&ref, refname, id);
2516 err = got_ref_write(ref, repo);
2524 static const struct got_error *
2525 cmd_ref(int argc, char *argv[])
2527 const struct got_error *error = NULL;
2528 struct got_repository *repo = NULL;
2529 struct got_worktree *worktree = NULL;
2530 char *cwd = NULL, *repo_path = NULL;
2531 int ch, do_list = 0;
2532 const char *delref = NULL;
2534 /* TODO: Add -s option for adding symbolic references. */
2535 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2541 repo_path = realpath(optarg, NULL);
2542 if (repo_path == NULL)
2543 err(1, "-r option");
2544 got_path_strip_trailing_slashes(repo_path);
2555 if (do_list && delref)
2556 errx(1, "-l and -d options are mutually exclusive\n");
2561 if (do_list || delref) {
2564 } else if (argc != 2)
2569 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2573 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2574 "sendfd unveil", NULL) == -1)
2578 cwd = getcwd(NULL, 0);
2580 error = got_error_from_errno("getcwd");
2584 if (repo_path == NULL) {
2585 error = got_worktree_open(&worktree, cwd);
2586 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2592 strdup(got_worktree_get_repo_path(worktree));
2593 if (repo_path == NULL)
2594 error = got_error_from_errno("strdup");
2598 repo_path = strdup(cwd);
2599 if (repo_path == NULL) {
2600 error = got_error_from_errno("strdup");
2606 error = got_repo_open(&repo, repo_path);
2610 error = apply_unveil(got_repo_get_path(repo), do_list,
2611 worktree ? got_worktree_get_root_path(worktree) : NULL);
2616 error = list_refs(repo);
2618 error = delete_ref(repo, delref);
2620 error = add_ref(repo, argv[0], argv[1]);
2623 got_repo_close(repo);
2625 got_worktree_close(worktree);
2635 "usage: %s branch [-r repository] -l | -d name | "
2636 "name [base-branch]\n", getprogname());
2640 static const struct got_error *
2641 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2643 static const struct got_error *err = NULL;
2644 struct got_reflist_head refs;
2645 struct got_reflist_entry *re;
2647 SIMPLEQ_INIT(&refs);
2649 err = got_ref_list(&refs, repo);
2653 SIMPLEQ_FOREACH(re, &refs, entry) {
2654 const char *refname, *marker = " ";
2656 refname = got_ref_get_name(re->ref);
2657 if (strncmp(refname, "refs/heads/", 11) != 0)
2659 if (worktree && strcmp(refname,
2660 got_worktree_get_head_ref_name(worktree)) == 0) {
2661 struct got_object_id *id = NULL;
2662 err = got_ref_resolve(&id, repo, re->ref);
2665 if (got_object_id_cmp(id,
2666 got_worktree_get_base_commit_id(worktree)) == 0)
2673 refstr = got_ref_to_str(re->ref);
2675 return got_error_from_errno("got_ref_to_str");
2676 printf("%s%s: %s\n", marker, refname, refstr);
2680 got_ref_list_free(&refs);
2684 static const struct got_error *
2685 delete_branch(struct got_repository *repo, const char *branch_name)
2687 const struct got_error *err = NULL;
2688 struct got_reference *ref;
2691 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2692 return got_error_from_errno("asprintf");
2694 err = got_ref_open(&ref, repo, refname, 0);
2698 err = got_ref_delete(ref, repo);
2705 static const struct got_error *
2706 add_branch(struct got_repository *repo, const char *branch_name,
2707 const char *base_branch)
2709 const struct got_error *err = NULL;
2710 struct got_object_id *id = NULL;
2711 struct got_reference *ref = NULL;
2712 char *base_refname = NULL, *refname = NULL;
2713 struct got_reference *base_ref;
2716 * Don't let the user create a branch named '-'.
2717 * While technically a valid reference name, this case is usually
2718 * an unintended typo.
2720 if (branch_name[0] == '-' && branch_name[1] == '\0')
2721 return got_error(GOT_ERR_BAD_REF_NAME);
2723 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2724 base_refname = strdup(GOT_REF_HEAD);
2725 if (base_refname == NULL)
2726 return got_error_from_errno("strdup");
2727 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2728 return got_error_from_errno("asprintf");
2730 err = got_ref_open(&base_ref, repo, base_refname, 0);
2733 err = got_ref_resolve(&id, repo, base_ref);
2734 got_ref_close(base_ref);
2738 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2739 err = got_error_from_errno("asprintf");
2743 err = got_ref_open(&ref, repo, refname, 0);
2745 err = got_error(GOT_ERR_BRANCH_EXISTS);
2747 } else if (err->code != GOT_ERR_NOT_REF)
2750 err = got_ref_alloc(&ref, refname, id);
2754 err = got_ref_write(ref, repo);
2764 static const struct got_error *
2765 cmd_branch(int argc, char *argv[])
2767 const struct got_error *error = NULL;
2768 struct got_repository *repo = NULL;
2769 struct got_worktree *worktree = NULL;
2770 char *cwd = NULL, *repo_path = NULL;
2771 int ch, do_list = 0;
2772 const char *delref = NULL;
2774 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2780 repo_path = realpath(optarg, NULL);
2781 if (repo_path == NULL)
2782 err(1, "-r option");
2783 got_path_strip_trailing_slashes(repo_path);
2794 if (do_list && delref)
2795 errx(1, "-l and -d options are mutually exclusive\n");
2800 if (do_list || delref) {
2803 } else if (argc < 1 || argc > 2)
2808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2813 "sendfd unveil", NULL) == -1)
2817 cwd = getcwd(NULL, 0);
2819 error = got_error_from_errno("getcwd");
2823 if (repo_path == NULL) {
2824 error = got_worktree_open(&worktree, cwd);
2825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2831 strdup(got_worktree_get_repo_path(worktree));
2832 if (repo_path == NULL)
2833 error = got_error_from_errno("strdup");
2837 repo_path = strdup(cwd);
2838 if (repo_path == NULL) {
2839 error = got_error_from_errno("strdup");
2845 error = got_repo_open(&repo, repo_path);
2849 error = apply_unveil(got_repo_get_path(repo), do_list,
2850 worktree ? got_worktree_get_root_path(worktree) : NULL);
2855 error = list_branches(repo, worktree);
2857 error = delete_branch(repo, delref);
2859 const char *base_branch;
2861 base_branch = worktree ?
2862 got_worktree_get_head_ref_name(worktree) :
2864 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2867 base_branch = argv[1];
2868 error = add_branch(repo, argv[0], base_branch);
2872 got_repo_close(repo);
2874 got_worktree_close(worktree);
2883 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2887 static const struct got_error *
2888 cmd_add(int argc, char *argv[])
2890 const struct got_error *error = NULL;
2891 struct got_repository *repo = NULL;
2892 struct got_worktree *worktree = NULL;
2894 struct got_pathlist_head paths;
2895 struct got_pathlist_entry *pe;
2900 while ((ch = getopt(argc, argv, "")) != -1) {
2912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2919 cwd = getcwd(NULL, 0);
2921 error = got_error_from_errno("getcwd");
2925 error = got_worktree_open(&worktree, cwd);
2929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2933 error = apply_unveil(got_repo_get_path(repo), 1,
2934 got_worktree_get_root_path(worktree));
2938 for (x = 0; x < argc; x++) {
2939 char *path = realpath(argv[x], NULL);
2941 error = got_error_from_errno2("realpath", argv[x]);
2945 got_path_strip_trailing_slashes(path);
2946 error = got_pathlist_insert(&pe, &paths, path, NULL);
2952 error = got_worktree_schedule_add(worktree, &paths, print_status,
2956 got_repo_close(repo);
2958 got_worktree_close(worktree);
2959 TAILQ_FOREACH(pe, &paths, entry)
2960 free((char *)pe->path);
2961 got_pathlist_free(&paths);
2969 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2973 static const struct got_error *
2974 cmd_remove(int argc, char *argv[])
2976 const struct got_error *error = NULL;
2977 struct got_worktree *worktree = NULL;
2978 struct got_repository *repo = NULL;
2980 struct got_pathlist_head paths;
2981 struct got_pathlist_entry *pe;
2982 int ch, i, delete_local_mods = 0;
2986 while ((ch = getopt(argc, argv, "f")) != -1) {
2989 delete_local_mods = 1;
3001 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3008 cwd = getcwd(NULL, 0);
3010 error = got_error_from_errno("getcwd");
3013 error = got_worktree_open(&worktree, cwd);
3017 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3021 error = apply_unveil(got_repo_get_path(repo), 1,
3022 got_worktree_get_root_path(worktree));
3026 for (i = 0; i < argc; i++) {
3027 char *path = realpath(argv[i], NULL);
3029 error = got_error_from_errno2("realpath", argv[i]);
3033 got_path_strip_trailing_slashes(path);
3034 error = got_pathlist_insert(&pe, &paths, path, NULL);
3040 error = got_worktree_schedule_delete(worktree, &paths,
3041 delete_local_mods, print_status, NULL, repo);
3046 got_repo_close(repo);
3048 got_worktree_close(worktree);
3049 TAILQ_FOREACH(pe, &paths, entry)
3050 free((char *)pe->path);
3051 got_pathlist_free(&paths);
3059 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3063 static const struct got_error *
3064 revert_progress(void *arg, unsigned char status, const char *path)
3066 while (path[0] == '/')
3068 printf("%c %s\n", status, path);
3072 static const struct got_error *
3073 cmd_revert(int argc, char *argv[])
3075 const struct got_error *error = NULL;
3076 struct got_worktree *worktree = NULL;
3077 struct got_repository *repo = NULL;
3078 char *cwd = NULL, *path = NULL;
3079 struct got_pathlist_head paths;
3080 struct got_pathlist_entry *pe;
3085 while ((ch = getopt(argc, argv, "")) != -1) {
3097 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3098 "unveil", NULL) == -1)
3104 for (i = 0; i < argc; i++) {
3105 char *path = realpath(argv[i], NULL);
3107 error = got_error_from_errno2("realpath", argv[i]);
3111 got_path_strip_trailing_slashes(path);
3112 error = got_pathlist_insert(&pe, &paths, path, NULL);
3119 cwd = getcwd(NULL, 0);
3121 error = got_error_from_errno("getcwd");
3124 error = got_worktree_open(&worktree, cwd);
3128 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3132 error = apply_unveil(got_repo_get_path(repo), 1,
3133 got_worktree_get_root_path(worktree));
3137 error = got_worktree_revert(worktree, &paths,
3138 revert_progress, NULL, repo);
3143 got_repo_close(repo);
3145 got_worktree_close(worktree);
3154 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3159 struct collect_commit_logmsg_arg {
3160 const char *cmdline_log;
3162 const char *worktree_path;
3163 const char *branch_name;
3164 const char *repo_path;
3169 static const struct got_error *
3170 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3173 char *initial_content = NULL;
3174 struct got_pathlist_entry *pe;
3175 const struct got_error *err = NULL;
3176 char *template = NULL;
3177 struct collect_commit_logmsg_arg *a = arg;
3181 /* if a message was specified on the command line, just use it */
3182 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3183 len = strlen(a->cmdline_log) + 1;
3184 *logmsg = malloc(len + 1);
3185 if (*logmsg == NULL)
3186 return got_error_from_errno("malloc");
3187 strlcpy(*logmsg, a->cmdline_log, len);
3191 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3192 return got_error_from_errno("asprintf");
3194 if (asprintf(&initial_content,
3195 "\n# changes to be committed on branch %s:\n",
3196 a->branch_name) == -1)
3197 return got_error_from_errno("asprintf");
3199 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3203 dprintf(fd, initial_content);
3205 TAILQ_FOREACH(pe, commitable_paths, entry) {
3206 struct got_commitable *ct = pe->data;
3207 dprintf(fd, "# %c %s\n",
3208 got_commitable_get_status(ct),
3209 got_commitable_get_path(ct));
3213 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3215 unlink(a->logmsg_path);
3216 free(a->logmsg_path);
3217 free(initial_content);
3220 /* Editor is done; we can now apply unveil(2) */
3222 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3231 static const struct got_error *
3232 cmd_commit(int argc, char *argv[])
3234 const struct got_error *error = NULL;
3235 struct got_worktree *worktree = NULL;
3236 struct got_repository *repo = NULL;
3237 char *cwd = NULL, *id_str = NULL;
3238 struct got_object_id *id = NULL;
3239 const char *logmsg = NULL;
3240 const char *got_author = getenv("GOT_AUTHOR");
3241 struct collect_commit_logmsg_arg cl_arg;
3242 char *editor = NULL;
3243 int ch, rebase_in_progress, histedit_in_progress;
3244 struct got_pathlist_head paths;
3248 while ((ch = getopt(argc, argv, "m:")) != -1) {
3263 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3264 "unveil", NULL) == -1)
3267 if (got_author == NULL) {
3268 /* TODO: Look current user up in password database */
3269 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3273 cwd = getcwd(NULL, 0);
3275 error = got_error_from_errno("getcwd");
3278 error = got_worktree_open(&worktree, cwd);
3282 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3285 if (rebase_in_progress) {
3286 error = got_error(GOT_ERR_REBASING);
3290 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3295 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3299 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3304 * unveil(2) traverses exec(2); if an editor is used we have
3305 * to apply unveil after the log message has been written.
3307 if (logmsg == NULL || strlen(logmsg) == 0)
3308 error = get_editor(&editor);
3310 error = apply_unveil(got_repo_get_path(repo), 0,
3311 got_worktree_get_root_path(worktree));
3315 cl_arg.editor = editor;
3316 cl_arg.cmdline_log = logmsg;
3317 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3318 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3319 if (!histedit_in_progress) {
3320 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3321 error = got_error(GOT_ERR_COMMIT_BRANCH);
3324 cl_arg.branch_name += 11;
3326 cl_arg.repo_path = got_repo_get_path(repo);
3327 cl_arg.logmsg_path = NULL;
3328 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3329 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3331 if (cl_arg.logmsg_path)
3332 fprintf(stderr, "%s: log message preserved in %s\n",
3333 getprogname(), cl_arg.logmsg_path);
3337 if (cl_arg.logmsg_path)
3338 unlink(cl_arg.logmsg_path);
3340 error = got_object_id_str(&id_str, id);
3343 printf("Created commit %s\n", id_str);
3346 got_repo_close(repo);
3348 got_worktree_close(worktree);
3356 usage_cherrypick(void)
3358 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3362 static const struct got_error *
3363 cmd_cherrypick(int argc, char *argv[])
3365 const struct got_error *error = NULL;
3366 struct got_worktree *worktree = NULL;
3367 struct got_repository *repo = NULL;
3368 char *cwd = NULL, *commit_id_str = NULL;
3369 struct got_object_id *commit_id = NULL;
3370 struct got_commit_object *commit = NULL;
3371 struct got_object_qid *pid;
3372 struct got_reference *head_ref = NULL;
3373 int ch, did_something = 0;
3375 while ((ch = getopt(argc, argv, "")) != -1) {
3387 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3388 "unveil", NULL) == -1)
3394 cwd = getcwd(NULL, 0);
3396 error = got_error_from_errno("getcwd");
3399 error = got_worktree_open(&worktree, cwd);
3403 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3407 error = apply_unveil(got_repo_get_path(repo), 0,
3408 got_worktree_get_root_path(worktree));
3412 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3413 GOT_OBJ_TYPE_COMMIT, repo);
3414 if (error != NULL) {
3415 struct got_reference *ref;
3416 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3418 error = got_ref_open(&ref, repo, argv[0], 0);
3421 error = got_ref_resolve(&commit_id, repo, ref);
3426 error = got_object_id_str(&commit_id_str, commit_id);
3430 error = got_ref_open(&head_ref, repo,
3431 got_worktree_get_head_ref_name(worktree), 0);
3435 error = check_same_branch(commit_id, head_ref, NULL, repo);
3437 if (error->code != GOT_ERR_ANCESTRY)
3441 error = got_error(GOT_ERR_SAME_BRANCH);
3445 error = got_object_open_as_commit(&commit, repo, commit_id);
3448 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3449 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3450 commit_id, repo, update_progress, &did_something, check_cancelled,
3456 printf("Merged commit %s\n", commit_id_str);
3459 got_object_commit_close(commit);
3460 free(commit_id_str);
3462 got_ref_close(head_ref);
3464 got_worktree_close(worktree);
3466 got_repo_close(repo);
3473 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3477 static const struct got_error *
3478 cmd_backout(int argc, char *argv[])
3480 const struct got_error *error = NULL;
3481 struct got_worktree *worktree = NULL;
3482 struct got_repository *repo = NULL;
3483 char *cwd = NULL, *commit_id_str = NULL;
3484 struct got_object_id *commit_id = NULL;
3485 struct got_commit_object *commit = NULL;
3486 struct got_object_qid *pid;
3487 struct got_reference *head_ref = NULL;
3488 int ch, did_something = 0;
3490 while ((ch = getopt(argc, argv, "")) != -1) {
3502 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3503 "unveil", NULL) == -1)
3509 cwd = getcwd(NULL, 0);
3511 error = got_error_from_errno("getcwd");
3514 error = got_worktree_open(&worktree, cwd);
3518 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3522 error = apply_unveil(got_repo_get_path(repo), 0,
3523 got_worktree_get_root_path(worktree));
3527 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3528 GOT_OBJ_TYPE_COMMIT, repo);
3529 if (error != NULL) {
3530 struct got_reference *ref;
3531 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3533 error = got_ref_open(&ref, repo, argv[0], 0);
3536 error = got_ref_resolve(&commit_id, repo, ref);
3541 error = got_object_id_str(&commit_id_str, commit_id);
3545 error = got_ref_open(&head_ref, repo,
3546 got_worktree_get_head_ref_name(worktree), 0);
3550 error = check_same_branch(commit_id, head_ref, NULL, repo);
3554 error = got_object_open_as_commit(&commit, repo, commit_id);
3557 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3559 error = got_error(GOT_ERR_ROOT_COMMIT);
3563 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3564 update_progress, &did_something, check_cancelled, NULL);
3569 printf("Backed out commit %s\n", commit_id_str);
3572 got_object_commit_close(commit);
3573 free(commit_id_str);
3575 got_ref_close(head_ref);
3577 got_worktree_close(worktree);
3579 got_repo_close(repo);
3586 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3592 trim_logmsg(char *logmsg, int limit)
3597 len = strlen(logmsg);
3601 nl = strchr(logmsg, '\n');
3606 static const struct got_error *
3607 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3609 const char *logmsg0 = NULL;
3611 logmsg0 = got_object_commit_get_logmsg(commit);
3613 while (isspace((unsigned char)logmsg0[0]))
3616 *logmsg = strdup(logmsg0);
3617 if (*logmsg == NULL)
3618 return got_error_from_errno("strdup");
3620 trim_logmsg(*logmsg, limit);
3624 static const struct got_error *
3625 show_rebase_progress(struct got_commit_object *commit,
3626 struct got_object_id *old_id, struct got_object_id *new_id)
3628 const struct got_error *err;
3629 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3631 err = got_object_id_str(&old_id_str, old_id);
3636 err = got_object_id_str(&new_id_str, new_id);
3641 old_id_str[12] = '\0';
3643 new_id_str[12] = '\0';
3645 err = get_short_logmsg(&logmsg, 42, commit);
3649 printf("%s -> %s: %s\n", old_id_str,
3650 new_id_str ? new_id_str : "no-op change", logmsg);
3657 static const struct got_error *
3658 rebase_progress(void *arg, unsigned char status, const char *path)
3660 unsigned char *rebase_status = arg;
3662 while (path[0] == '/')
3664 printf("%c %s\n", status, path);
3666 if (*rebase_status == GOT_STATUS_CONFLICT)
3668 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3669 *rebase_status = status;
3673 static const struct got_error *
3674 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3675 struct got_reference *branch, struct got_reference *new_base_branch,
3676 struct got_reference *tmp_branch, struct got_repository *repo)
3678 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3679 return got_worktree_rebase_complete(worktree, fileindex,
3680 new_base_branch, tmp_branch, branch, repo);
3683 static const struct got_error *
3684 rebase_commit(struct got_pathlist_head *merged_paths,
3685 struct got_worktree *worktree, struct got_fileindex *fileindex,
3686 struct got_reference *tmp_branch,
3687 struct got_object_id *commit_id, struct got_repository *repo)
3689 const struct got_error *error;
3690 struct got_commit_object *commit;
3691 struct got_object_id *new_commit_id;
3693 error = got_object_open_as_commit(&commit, repo, commit_id);
3697 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3698 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3700 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3702 error = show_rebase_progress(commit, commit_id, NULL);
3704 error = show_rebase_progress(commit, commit_id, new_commit_id);
3705 free(new_commit_id);
3708 got_object_commit_close(commit);
3712 struct check_path_prefix_arg {
3713 const char *path_prefix;
3718 static const struct got_error *
3719 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3720 struct got_blob_object *blob2, struct got_object_id *id1,
3721 struct got_object_id *id2, const char *path1, const char *path2,
3722 struct got_repository *repo)
3724 struct check_path_prefix_arg *a = arg;
3726 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3727 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3728 return got_error(a->errcode);
3733 static const struct got_error *
3734 check_path_prefix(struct got_object_id *parent_id,
3735 struct got_object_id *commit_id, const char *path_prefix,
3736 int errcode, struct got_repository *repo)
3738 const struct got_error *err;
3739 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3740 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3741 struct check_path_prefix_arg cpp_arg;
3743 if (got_path_is_root_dir(path_prefix))
3746 err = got_object_open_as_commit(&commit, repo, commit_id);
3750 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3754 err = got_object_open_as_tree(&tree1, repo,
3755 got_object_commit_get_tree_id(parent_commit));
3759 err = got_object_open_as_tree(&tree2, repo,
3760 got_object_commit_get_tree_id(commit));
3764 cpp_arg.path_prefix = path_prefix;
3765 while (cpp_arg.path_prefix[0] == '/')
3766 cpp_arg.path_prefix++;
3767 cpp_arg.len = strlen(cpp_arg.path_prefix);
3768 cpp_arg.errcode = errcode;
3769 err = got_diff_tree(tree1, tree2, "", "", repo,
3770 check_path_prefix_in_diff, &cpp_arg, 0);
3773 got_object_tree_close(tree1);
3775 got_object_tree_close(tree2);
3777 got_object_commit_close(commit);
3779 got_object_commit_close(parent_commit);
3783 static const struct got_error *
3784 collect_commits(struct got_object_id_queue *commits,
3785 struct got_object_id *initial_commit_id,
3786 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3787 const char *path_prefix, int path_prefix_errcode,
3788 struct got_repository *repo)
3790 const struct got_error *err = NULL;
3791 struct got_commit_graph *graph = NULL;
3792 struct got_object_id *parent_id = NULL;
3793 struct got_object_qid *qid;
3794 struct got_object_id *commit_id = initial_commit_id;
3796 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3800 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3803 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3804 err = got_commit_graph_iter_next(&parent_id, graph);
3806 if (err->code == GOT_ERR_ITER_COMPLETED) {
3807 err = got_error_msg(GOT_ERR_ANCESTRY,
3808 "ran out of commits to rebase before "
3809 "youngest common ancestor commit has "
3812 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3814 err = got_commit_graph_fetch_commits(graph, 1, repo);
3818 err = check_path_prefix(parent_id, commit_id,
3819 path_prefix, path_prefix_errcode, repo);
3823 err = got_object_qid_alloc(&qid, commit_id);
3826 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3827 commit_id = parent_id;
3831 got_commit_graph_close(graph);
3835 static const struct got_error *
3836 cmd_rebase(int argc, char *argv[])
3838 const struct got_error *error = NULL;
3839 struct got_worktree *worktree = NULL;
3840 struct got_repository *repo = NULL;
3841 struct got_fileindex *fileindex = NULL;
3843 struct got_reference *branch = NULL;
3844 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3845 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3846 struct got_object_id *resume_commit_id = NULL;
3847 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3848 struct got_commit_object *commit = NULL;
3849 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3850 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3851 struct got_object_id_queue commits;
3852 struct got_pathlist_head merged_paths;
3853 const struct got_object_id_queue *parent_ids;
3854 struct got_object_qid *qid, *pid;
3856 SIMPLEQ_INIT(&commits);
3857 TAILQ_INIT(&merged_paths);
3859 while ((ch = getopt(argc, argv, "ac")) != -1) {
3865 continue_rebase = 1;
3877 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3878 "unveil", NULL) == -1)
3881 if (abort_rebase && continue_rebase)
3883 else if (abort_rebase || continue_rebase) {
3886 } else if (argc != 1)
3889 cwd = getcwd(NULL, 0);
3891 error = got_error_from_errno("getcwd");
3894 error = got_worktree_open(&worktree, cwd);
3898 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3902 error = apply_unveil(got_repo_get_path(repo), 0,
3903 got_worktree_get_root_path(worktree));
3907 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3913 if (!rebase_in_progress) {
3914 error = got_error(GOT_ERR_NOT_REBASING);
3917 error = got_worktree_rebase_continue(&resume_commit_id,
3918 &new_base_branch, &tmp_branch, &branch, &fileindex,
3922 printf("Switching work tree to %s\n",
3923 got_ref_get_symref_target(new_base_branch));
3924 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3925 new_base_branch, update_progress, &did_something);
3928 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3929 goto done; /* nothing else to do */
3932 if (continue_rebase) {
3933 if (!rebase_in_progress) {
3934 error = got_error(GOT_ERR_NOT_REBASING);
3937 error = got_worktree_rebase_continue(&resume_commit_id,
3938 &new_base_branch, &tmp_branch, &branch, &fileindex,
3943 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3944 resume_commit_id, repo);
3948 yca_id = got_object_id_dup(resume_commit_id);
3949 if (yca_id == NULL) {
3950 error = got_error_from_errno("got_object_id_dup");
3954 error = got_ref_open(&branch, repo, argv[0], 0);
3959 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3963 if (!continue_rebase) {
3964 struct got_object_id *base_commit_id;
3966 base_commit_id = got_worktree_get_base_commit_id(worktree);
3967 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3968 base_commit_id, branch_head_commit_id, repo);
3971 if (yca_id == NULL) {
3972 error = got_error_msg(GOT_ERR_ANCESTRY,
3973 "specified branch shares no common ancestry "
3974 "with work tree's branch");
3978 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3980 if (error->code != GOT_ERR_ANCESTRY)
3984 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3985 "specified branch resolves to a commit which "
3986 "is already contained in work tree's branch");
3989 error = got_worktree_rebase_prepare(&new_base_branch,
3990 &tmp_branch, &fileindex, worktree, branch, repo);
3995 commit_id = branch_head_commit_id;
3996 error = got_object_open_as_commit(&commit, repo, commit_id);
4000 parent_ids = got_object_commit_get_parent_ids(commit);
4001 pid = SIMPLEQ_FIRST(parent_ids);
4002 error = collect_commits(&commits, commit_id, pid->id,
4003 yca_id, got_worktree_get_path_prefix(worktree),
4004 GOT_ERR_REBASE_PATH, repo);
4005 got_object_commit_close(commit);
4010 if (SIMPLEQ_EMPTY(&commits)) {
4011 if (continue_rebase)
4012 error = rebase_complete(worktree, fileindex,
4013 branch, new_base_branch, tmp_branch, repo);
4015 error = got_error(GOT_ERR_EMPTY_REBASE);
4020 SIMPLEQ_FOREACH(qid, &commits, entry) {
4021 commit_id = qid->id;
4022 parent_id = pid ? pid->id : yca_id;
4025 error = got_worktree_rebase_merge_files(&merged_paths,
4026 worktree, fileindex, parent_id, commit_id, repo,
4027 rebase_progress, &rebase_status, check_cancelled, NULL);
4031 if (rebase_status == GOT_STATUS_CONFLICT) {
4032 got_worktree_rebase_pathlist_free(&merged_paths);
4036 error = rebase_commit(&merged_paths, worktree, fileindex,
4037 tmp_branch, commit_id, repo);
4038 got_worktree_rebase_pathlist_free(&merged_paths);
4043 if (rebase_status == GOT_STATUS_CONFLICT) {
4044 error = got_worktree_rebase_postpone(worktree, fileindex);
4047 error = got_error_msg(GOT_ERR_CONFLICTS,
4048 "conflicts must be resolved before rebasing can continue");
4050 error = rebase_complete(worktree, fileindex, branch,
4051 new_base_branch, tmp_branch, repo);
4053 got_object_id_queue_free(&commits);
4054 free(branch_head_commit_id);
4055 free(resume_commit_id);
4058 got_object_commit_close(commit);
4060 got_ref_close(branch);
4061 if (new_base_branch)
4062 got_ref_close(new_base_branch);
4064 got_ref_close(tmp_branch);
4066 got_worktree_close(worktree);
4068 got_repo_close(repo);
4073 usage_histedit(void)
4075 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4080 #define GOT_HISTEDIT_PICK 'p'
4081 #define GOT_HISTEDIT_EDIT 'e'
4082 #define GOT_HISTEDIT_FOLD 'f'
4083 #define GOT_HISTEDIT_DROP 'd'
4084 #define GOT_HISTEDIT_MESG 'm'
4086 static struct got_histedit_cmd {
4090 } got_histedit_cmds[] = {
4091 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4092 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4093 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4094 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4095 { GOT_HISTEDIT_MESG, "mesg",
4096 "single-line log message for commit above (open editor if empty)" },
4099 struct got_histedit_list_entry {
4100 TAILQ_ENTRY(got_histedit_list_entry) entry;
4101 struct got_object_id *commit_id;
4102 const struct got_histedit_cmd *cmd;
4105 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4107 static const struct got_error *
4108 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4109 FILE *f, struct got_repository *repo)
4111 const struct got_error *err = NULL;
4112 char *logmsg = NULL, *id_str = NULL;
4113 struct got_commit_object *commit = NULL;
4116 err = got_object_open_as_commit(&commit, repo, commit_id);
4120 err = get_short_logmsg(&logmsg, 34, commit);
4124 err = got_object_id_str(&id_str, commit_id);
4128 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4130 err = got_ferror(f, GOT_ERR_IO);
4133 got_object_commit_close(commit);
4139 static const struct got_error *
4140 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4141 struct got_repository *repo)
4143 const struct got_error *err = NULL;
4144 struct got_object_qid *qid;
4146 if (SIMPLEQ_EMPTY(commits))
4147 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4149 SIMPLEQ_FOREACH(qid, commits, entry) {
4150 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4159 static const struct got_error *
4160 write_cmd_list(FILE *f)
4162 const struct got_error *err = NULL;
4165 n = fprintf(f, "# Available histedit commands:\n");
4167 return got_ferror(f, GOT_ERR_IO);
4169 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4170 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4171 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4174 err = got_ferror(f, GOT_ERR_IO);
4178 n = fprintf(f, "# Commits will be processed in order from top to "
4179 "bottom of this file.\n");
4181 return got_ferror(f, GOT_ERR_IO);
4185 static const struct got_error *
4186 histedit_syntax_error(int lineno)
4188 static char msg[42];
4191 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4193 if (ret == -1 || ret >= sizeof(msg))
4194 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4196 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4199 static const struct got_error *
4200 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4201 char *logmsg, struct got_repository *repo)
4203 const struct got_error *err;
4204 struct got_commit_object *folded_commit = NULL;
4207 err = got_object_id_str(&id_str, hle->commit_id);
4211 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4215 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4216 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4217 got_object_commit_get_logmsg(folded_commit)) == -1) {
4218 err = got_error_from_errno("asprintf");
4223 got_object_commit_close(folded_commit);
4228 static struct got_histedit_list_entry *
4229 get_folded_commits(struct got_histedit_list_entry *hle)
4231 struct got_histedit_list_entry *prev, *folded = NULL;
4233 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4234 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4235 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4236 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4238 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4244 static const struct got_error *
4245 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4246 struct got_repository *repo)
4248 char *logmsg_path = NULL, *id_str = NULL;
4249 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4250 const struct got_error *err = NULL;
4251 struct got_commit_object *commit = NULL;
4253 struct got_histedit_list_entry *folded = NULL;
4255 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4259 folded = get_folded_commits(hle);
4261 while (folded != hle) {
4262 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4263 folded = TAILQ_NEXT(folded, entry);
4266 err = append_folded_commit_msg(&new_msg, folded,
4272 folded = TAILQ_NEXT(folded, entry);
4276 err = got_object_id_str(&id_str, hle->commit_id);
4279 if (asprintf(&new_msg,
4280 "%s\n# original log message of commit %s: %s",
4281 logmsg ? logmsg : "", id_str,
4282 got_object_commit_get_logmsg(commit)) == -1) {
4283 err = got_error_from_errno("asprintf");
4289 err = got_object_id_str(&id_str, hle->commit_id);
4293 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4297 dprintf(fd, logmsg);
4300 err = get_editor(&editor);
4304 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4306 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4309 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4310 if (hle->logmsg == NULL)
4311 err = got_error_from_errno("strdup");
4314 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4315 err = got_error_from_errno2("unlink", logmsg_path);
4320 got_object_commit_close(commit);
4324 static const struct got_error *
4325 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4326 FILE *f, struct got_repository *repo)
4328 const struct got_error *err = NULL;
4329 char *line = NULL, *p, *end;
4333 const struct got_histedit_cmd *cmd;
4334 struct got_object_id *commit_id = NULL;
4335 struct got_histedit_list_entry *hle = NULL;
4338 len = getline(&line, &size, f);
4340 const struct got_error *getline_err;
4343 getline_err = got_error_from_errno("getline");
4344 err = got_ferror(f, getline_err->code);
4349 while (isspace((unsigned char)p[0]))
4351 if (p[0] == '#' || p[0] == '\0') {
4357 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4358 cmd = &got_histedit_cmds[i];
4359 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4360 isspace((unsigned char)p[strlen(cmd->name)])) {
4361 p += strlen(cmd->name);
4364 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4369 if (i == nitems(got_histedit_cmds)) {
4370 err = histedit_syntax_error(lineno);
4373 while (isspace((unsigned char)p[0]))
4375 if (cmd->code == GOT_HISTEDIT_MESG) {
4376 if (hle == NULL || hle->logmsg != NULL) {
4377 err = got_error(GOT_ERR_HISTEDIT_CMD);
4381 err = histedit_edit_logmsg(hle, repo);
4385 hle->logmsg = strdup(p);
4386 if (hle->logmsg == NULL) {
4387 err = got_error_from_errno("strdup");
4396 while (end[0] && !isspace((unsigned char)end[0]))
4400 err = got_object_resolve_id_str(&commit_id, repo, p);
4402 /* override error code */
4403 err = histedit_syntax_error(lineno);
4407 hle = malloc(sizeof(*hle));
4409 err = got_error_from_errno("malloc");
4413 hle->commit_id = commit_id;
4418 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4426 static const struct got_error *
4427 histedit_check_script(struct got_histedit_list *histedit_cmds,
4428 struct got_object_id_queue *commits, struct got_repository *repo)
4430 const struct got_error *err = NULL;
4431 struct got_object_qid *qid;
4432 struct got_histedit_list_entry *hle;
4433 static char msg[80];
4436 if (TAILQ_EMPTY(histedit_cmds))
4437 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4438 "histedit script contains no commands");
4440 SIMPLEQ_FOREACH(qid, commits, entry) {
4441 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4442 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4446 err = got_object_id_str(&id_str, qid->id);
4449 snprintf(msg, sizeof(msg),
4450 "commit %s missing from histedit script", id_str);
4452 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4456 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4457 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4458 "last commit in histedit script cannot be folded");
4463 static const struct got_error *
4464 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4465 const char *path, struct got_object_id_queue *commits,
4466 struct got_repository *repo)
4468 const struct got_error *err = NULL;
4472 err = get_editor(&editor);
4476 if (spawn_editor(editor, path) == -1) {
4477 err = got_error_from_errno("failed spawning editor");
4481 f = fopen(path, "r");
4483 err = got_error_from_errno("fopen");
4486 err = histedit_parse_list(histedit_cmds, f, repo);
4490 err = histedit_check_script(histedit_cmds, commits, repo);
4492 if (f && fclose(f) != 0 && err == NULL)
4493 err = got_error_from_errno("fclose");
4498 static const struct got_error *
4499 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4500 struct got_object_id_queue *, const char *, struct got_repository *);
4502 static const struct got_error *
4503 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4504 struct got_object_id_queue *commits, struct got_repository *repo)
4506 const struct got_error *err;
4510 err = got_opentemp_named(&path, &f, "got-histedit");
4514 err = write_cmd_list(f);
4518 err = histedit_write_commit_list(commits, f, repo);
4522 if (fclose(f) != 0) {
4523 err = got_error_from_errno("fclose");
4528 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4530 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4531 err->code != GOT_ERR_HISTEDIT_CMD)
4533 err = histedit_edit_list_retry(histedit_cmds, err,
4534 commits, path, repo);
4537 if (f && fclose(f) != 0 && err == NULL)
4538 err = got_error_from_errno("fclose");
4539 if (path && unlink(path) != 0 && err == NULL)
4540 err = got_error_from_errno2("unlink", path);
4545 static const struct got_error *
4546 histedit_save_list(struct got_histedit_list *histedit_cmds,
4547 struct got_worktree *worktree, struct got_repository *repo)
4549 const struct got_error *err = NULL;
4552 struct got_histedit_list_entry *hle;
4553 struct got_commit_object *commit = NULL;
4555 err = got_worktree_get_histedit_script_path(&path, worktree);
4559 f = fopen(path, "w");
4561 err = got_error_from_errno2("fopen", path);
4564 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4565 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4571 int n = fprintf(f, "%c %s\n",
4572 GOT_HISTEDIT_MESG, hle->logmsg);
4574 err = got_ferror(f, GOT_ERR_IO);
4580 if (f && fclose(f) != 0 && err == NULL)
4581 err = got_error_from_errno("fclose");
4584 got_object_commit_close(commit);
4589 histedit_free_list(struct got_histedit_list *histedit_cmds)
4591 struct got_histedit_list_entry *hle;
4593 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4594 TAILQ_REMOVE(histedit_cmds, hle, entry);
4599 static const struct got_error *
4600 histedit_load_list(struct got_histedit_list *histedit_cmds,
4601 const char *path, struct got_repository *repo)
4603 const struct got_error *err = NULL;
4606 f = fopen(path, "r");
4608 err = got_error_from_errno2("fopen", path);
4612 err = histedit_parse_list(histedit_cmds, f, repo);
4614 if (f && fclose(f) != 0 && err == NULL)
4615 err = got_error_from_errno("fclose");
4619 static const struct got_error *
4620 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4621 const struct got_error *edit_err, struct got_object_id_queue *commits,
4622 const char *path, struct got_repository *repo)
4624 const struct got_error *err = NULL, *prev_err = edit_err;
4627 while (resp != 'c' && resp != 'r' && resp != 'a') {
4628 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4629 "or (a)bort: ", getprogname(), prev_err->msg);
4632 histedit_free_list(histedit_cmds);
4633 err = histedit_run_editor(histedit_cmds, path, commits,
4636 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4637 err->code != GOT_ERR_HISTEDIT_CMD)
4644 } else if (resp == 'r') {
4645 histedit_free_list(histedit_cmds);
4646 err = histedit_edit_script(histedit_cmds,
4649 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4650 err->code != GOT_ERR_HISTEDIT_CMD)
4657 } else if (resp == 'a') {
4658 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4661 printf("invalid response '%c'\n", resp);
4667 static const struct got_error *
4668 histedit_complete(struct got_worktree *worktree,
4669 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4670 struct got_reference *branch, struct got_repository *repo)
4672 printf("Switching work tree to %s\n",
4673 got_ref_get_symref_target(branch));
4674 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4678 static const struct got_error *
4679 show_histedit_progress(struct got_commit_object *commit,
4680 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4682 const struct got_error *err;
4683 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4685 err = got_object_id_str(&old_id_str, hle->commit_id);
4690 err = got_object_id_str(&new_id_str, new_id);
4695 old_id_str[12] = '\0';
4697 new_id_str[12] = '\0';
4700 logmsg = strdup(hle->logmsg);
4701 if (logmsg == NULL) {
4702 err = got_error_from_errno("strdup");
4705 trim_logmsg(logmsg, 42);
4707 err = get_short_logmsg(&logmsg, 42, commit);
4712 switch (hle->cmd->code) {
4713 case GOT_HISTEDIT_PICK:
4714 case GOT_HISTEDIT_EDIT:
4715 printf("%s -> %s: %s\n", old_id_str,
4716 new_id_str ? new_id_str : "no-op change", logmsg);
4718 case GOT_HISTEDIT_DROP:
4719 case GOT_HISTEDIT_FOLD:
4720 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4733 static const struct got_error *
4734 histedit_commit(struct got_pathlist_head *merged_paths,
4735 struct got_worktree *worktree, struct got_fileindex *fileindex,
4736 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4737 struct got_repository *repo)
4739 const struct got_error *err;
4740 struct got_commit_object *commit;
4741 struct got_object_id *new_commit_id;
4743 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4744 && hle->logmsg == NULL) {
4745 err = histedit_edit_logmsg(hle, repo);
4750 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4754 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4755 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4758 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4760 err = show_histedit_progress(commit, hle, NULL);
4762 err = show_histedit_progress(commit, hle, new_commit_id);
4763 free(new_commit_id);
4766 got_object_commit_close(commit);
4770 static const struct got_error *
4771 histedit_skip_commit(struct got_histedit_list_entry *hle,
4772 struct got_worktree *worktree, struct got_repository *repo)
4774 const struct got_error *error;
4775 struct got_commit_object *commit;
4777 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4782 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4786 error = show_histedit_progress(commit, hle, NULL);
4787 got_object_commit_close(commit);
4791 static const struct got_error *
4792 cmd_histedit(int argc, char *argv[])
4794 const struct got_error *error = NULL;
4795 struct got_worktree *worktree = NULL;
4796 struct got_fileindex *fileindex = NULL;
4797 struct got_repository *repo = NULL;
4799 struct got_reference *branch = NULL;
4800 struct got_reference *tmp_branch = NULL;
4801 struct got_object_id *resume_commit_id = NULL;
4802 struct got_object_id *base_commit_id = NULL;
4803 struct got_object_id *head_commit_id = NULL;
4804 struct got_commit_object *commit = NULL;
4805 int ch, rebase_in_progress = 0, did_something;
4806 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4807 const char *edit_script_path = NULL;
4808 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4809 struct got_object_id_queue commits;
4810 struct got_pathlist_head merged_paths;
4811 const struct got_object_id_queue *parent_ids;
4812 struct got_object_qid *pid;
4813 struct got_histedit_list histedit_cmds;
4814 struct got_histedit_list_entry *hle;
4816 SIMPLEQ_INIT(&commits);
4817 TAILQ_INIT(&histedit_cmds);
4818 TAILQ_INIT(&merged_paths);
4820 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4829 edit_script_path = optarg;
4841 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4842 "unveil", NULL) == -1)
4845 if (abort_edit && continue_edit)
4851 * This command cannot apply unveil(2) in all cases because the
4852 * user may choose to run an editor to edit the histedit script
4853 * and to edit individual commit log messages.
4854 * unveil(2) traverses exec(2); if an editor is used we have to
4855 * apply unveil after edit script and log messages have been written.
4856 * XXX TODO: Make use of unveil(2) where possible.
4859 cwd = getcwd(NULL, 0);
4861 error = got_error_from_errno("getcwd");
4864 error = got_worktree_open(&worktree, cwd);
4868 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4872 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4875 if (rebase_in_progress) {
4876 error = got_error(GOT_ERR_REBASING);
4880 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4884 if (edit_in_progress && abort_edit) {
4885 error = got_worktree_histedit_continue(&resume_commit_id,
4886 &tmp_branch, &branch, &base_commit_id, &fileindex,
4890 printf("Switching work tree to %s\n",
4891 got_ref_get_symref_target(branch));
4892 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4893 branch, base_commit_id, update_progress, &did_something);
4896 printf("Histedit of %s aborted\n",
4897 got_ref_get_symref_target(branch));
4898 goto done; /* nothing else to do */
4899 } else if (abort_edit) {
4900 error = got_error(GOT_ERR_NOT_HISTEDIT);
4904 if (continue_edit) {
4907 if (!edit_in_progress) {
4908 error = got_error(GOT_ERR_NOT_HISTEDIT);
4912 error = got_worktree_get_histedit_script_path(&path, worktree);
4916 error = histedit_load_list(&histedit_cmds, path, repo);
4921 error = got_worktree_histedit_continue(&resume_commit_id,
4922 &tmp_branch, &branch, &base_commit_id, &fileindex,
4927 error = got_ref_resolve(&head_commit_id, repo, branch);
4931 error = got_object_open_as_commit(&commit, repo,
4935 parent_ids = got_object_commit_get_parent_ids(commit);
4936 pid = SIMPLEQ_FIRST(parent_ids);
4938 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4941 error = collect_commits(&commits, head_commit_id, pid->id,
4942 base_commit_id, got_worktree_get_path_prefix(worktree),
4943 GOT_ERR_HISTEDIT_PATH, repo);
4944 got_object_commit_close(commit);
4949 if (edit_in_progress) {
4950 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4954 error = got_ref_open(&branch, repo,
4955 got_worktree_get_head_ref_name(worktree), 0);
4959 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4960 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4961 "will not edit commit history of a branch outside "
4962 "the \"refs/heads/\" reference namespace");
4966 error = got_ref_resolve(&head_commit_id, repo, branch);
4967 got_ref_close(branch);
4972 error = got_object_open_as_commit(&commit, repo,
4976 parent_ids = got_object_commit_get_parent_ids(commit);
4977 pid = SIMPLEQ_FIRST(parent_ids);
4979 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4982 error = collect_commits(&commits, head_commit_id, pid->id,
4983 got_worktree_get_base_commit_id(worktree),
4984 got_worktree_get_path_prefix(worktree),
4985 GOT_ERR_HISTEDIT_PATH, repo);
4986 got_object_commit_close(commit);
4991 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4992 &base_commit_id, &fileindex, worktree, repo);
4996 if (edit_script_path) {
4997 error = histedit_load_list(&histedit_cmds,
4998 edit_script_path, repo);
5000 got_worktree_histedit_abort(worktree, fileindex,
5001 repo, branch, base_commit_id,
5002 update_progress, &did_something);
5006 error = histedit_edit_script(&histedit_cmds, &commits,
5009 got_worktree_histedit_abort(worktree, fileindex,
5010 repo, branch, base_commit_id,
5011 update_progress, &did_something);
5017 error = histedit_save_list(&histedit_cmds, worktree,
5020 got_worktree_histedit_abort(worktree, fileindex,
5021 repo, branch, base_commit_id,
5022 update_progress, &did_something);
5028 error = histedit_check_script(&histedit_cmds, &commits, repo);
5032 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5033 if (resume_commit_id) {
5034 if (got_object_id_cmp(hle->commit_id,
5035 resume_commit_id) != 0)
5038 resume_commit_id = NULL;
5039 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5040 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5041 error = histedit_skip_commit(hle, worktree,
5044 error = histedit_commit(NULL, worktree,
5045 fileindex, tmp_branch, hle, repo);
5052 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5053 error = histedit_skip_commit(hle, worktree, repo);
5059 error = got_object_open_as_commit(&commit, repo,
5063 parent_ids = got_object_commit_get_parent_ids(commit);
5064 pid = SIMPLEQ_FIRST(parent_ids);
5066 error = got_worktree_histedit_merge_files(&merged_paths,
5067 worktree, fileindex, pid->id, hle->commit_id, repo,
5068 rebase_progress, &rebase_status, check_cancelled, NULL);
5071 got_object_commit_close(commit);
5074 if (rebase_status == GOT_STATUS_CONFLICT) {
5075 got_worktree_rebase_pathlist_free(&merged_paths);
5079 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5081 error = got_object_id_str(&id_str, hle->commit_id);
5084 printf("Stopping histedit for amending commit %s\n",
5087 got_worktree_rebase_pathlist_free(&merged_paths);
5088 error = got_worktree_histedit_postpone(worktree,
5093 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5094 error = histedit_skip_commit(hle, worktree, repo);
5100 error = histedit_commit(&merged_paths, worktree, fileindex,
5101 tmp_branch, hle, repo);
5102 got_worktree_rebase_pathlist_free(&merged_paths);
5107 if (rebase_status == GOT_STATUS_CONFLICT) {
5108 error = got_worktree_histedit_postpone(worktree, fileindex);
5111 error = got_error_msg(GOT_ERR_CONFLICTS,
5112 "conflicts must be resolved before rebasing can continue");
5114 error = histedit_complete(worktree, fileindex, tmp_branch,
5117 got_object_id_queue_free(&commits);
5118 histedit_free_list(&histedit_cmds);
5119 free(head_commit_id);
5120 free(base_commit_id);
5121 free(resume_commit_id);
5123 got_object_commit_close(commit);
5125 got_ref_close(branch);
5127 got_ref_close(tmp_branch);
5129 got_worktree_close(worktree);
5131 got_repo_close(repo);