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 (strlen(pe->path) == 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) :
2865 base_branch = argv[1];
2866 error = add_branch(repo, argv[0], base_branch);
2870 got_repo_close(repo);
2872 got_worktree_close(worktree);
2881 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2885 static const struct got_error *
2886 cmd_add(int argc, char *argv[])
2888 const struct got_error *error = NULL;
2889 struct got_repository *repo = NULL;
2890 struct got_worktree *worktree = NULL;
2892 struct got_pathlist_head paths;
2893 struct got_pathlist_entry *pe;
2898 while ((ch = getopt(argc, argv, "")) != -1) {
2910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2917 cwd = getcwd(NULL, 0);
2919 error = got_error_from_errno("getcwd");
2923 error = got_worktree_open(&worktree, cwd);
2927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2931 error = apply_unveil(got_repo_get_path(repo), 1,
2932 got_worktree_get_root_path(worktree));
2936 for (x = 0; x < argc; x++) {
2937 char *path = realpath(argv[x], NULL);
2939 error = got_error_from_errno2("realpath", argv[x]);
2943 got_path_strip_trailing_slashes(path);
2944 error = got_pathlist_insert(&pe, &paths, path, NULL);
2950 error = got_worktree_schedule_add(worktree, &paths, print_status,
2954 got_repo_close(repo);
2956 got_worktree_close(worktree);
2957 TAILQ_FOREACH(pe, &paths, entry)
2958 free((char *)pe->path);
2959 got_pathlist_free(&paths);
2967 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2971 static const struct got_error *
2972 cmd_remove(int argc, char *argv[])
2974 const struct got_error *error = NULL;
2975 struct got_worktree *worktree = NULL;
2976 struct got_repository *repo = NULL;
2978 struct got_pathlist_head paths;
2979 struct got_pathlist_entry *pe;
2980 int ch, i, delete_local_mods = 0;
2984 while ((ch = getopt(argc, argv, "f")) != -1) {
2987 delete_local_mods = 1;
2999 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3006 cwd = getcwd(NULL, 0);
3008 error = got_error_from_errno("getcwd");
3011 error = got_worktree_open(&worktree, cwd);
3015 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3019 error = apply_unveil(got_repo_get_path(repo), 1,
3020 got_worktree_get_root_path(worktree));
3024 for (i = 0; i < argc; i++) {
3025 char *path = realpath(argv[i], NULL);
3027 error = got_error_from_errno2("realpath", argv[i]);
3031 got_path_strip_trailing_slashes(path);
3032 error = got_pathlist_insert(&pe, &paths, path, NULL);
3038 error = got_worktree_schedule_delete(worktree, &paths,
3039 delete_local_mods, print_status, NULL, repo);
3044 got_repo_close(repo);
3046 got_worktree_close(worktree);
3047 TAILQ_FOREACH(pe, &paths, entry)
3048 free((char *)pe->path);
3049 got_pathlist_free(&paths);
3057 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3061 static const struct got_error *
3062 revert_progress(void *arg, unsigned char status, const char *path)
3064 while (path[0] == '/')
3066 printf("%c %s\n", status, path);
3070 static const struct got_error *
3071 cmd_revert(int argc, char *argv[])
3073 const struct got_error *error = NULL;
3074 struct got_worktree *worktree = NULL;
3075 struct got_repository *repo = NULL;
3076 char *cwd = NULL, *path = NULL;
3077 struct got_pathlist_head paths;
3078 struct got_pathlist_entry *pe;
3083 while ((ch = getopt(argc, argv, "")) != -1) {
3095 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3096 "unveil", NULL) == -1)
3102 for (i = 0; i < argc; i++) {
3103 char *path = realpath(argv[i], NULL);
3105 error = got_error_from_errno2("realpath", argv[i]);
3109 got_path_strip_trailing_slashes(path);
3110 error = got_pathlist_insert(&pe, &paths, path, NULL);
3117 cwd = getcwd(NULL, 0);
3119 error = got_error_from_errno("getcwd");
3122 error = got_worktree_open(&worktree, cwd);
3126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3130 error = apply_unveil(got_repo_get_path(repo), 1,
3131 got_worktree_get_root_path(worktree));
3135 error = got_worktree_revert(worktree, &paths,
3136 revert_progress, NULL, repo);
3141 got_repo_close(repo);
3143 got_worktree_close(worktree);
3152 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3157 struct collect_commit_logmsg_arg {
3158 const char *cmdline_log;
3160 const char *worktree_path;
3161 const char *branch_name;
3162 const char *repo_path;
3167 static const struct got_error *
3168 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3171 char *initial_content = NULL;
3172 struct got_pathlist_entry *pe;
3173 const struct got_error *err = NULL;
3174 char *template = NULL;
3175 struct collect_commit_logmsg_arg *a = arg;
3179 /* if a message was specified on the command line, just use it */
3180 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3181 len = strlen(a->cmdline_log) + 1;
3182 *logmsg = malloc(len + 1);
3183 if (*logmsg == NULL)
3184 return got_error_from_errno("malloc");
3185 strlcpy(*logmsg, a->cmdline_log, len);
3189 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3190 return got_error_from_errno("asprintf");
3192 if (asprintf(&initial_content,
3193 "\n# changes to be committed on branch %s:\n",
3194 a->branch_name) == -1)
3195 return got_error_from_errno("asprintf");
3197 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3201 dprintf(fd, initial_content);
3203 TAILQ_FOREACH(pe, commitable_paths, entry) {
3204 struct got_commitable *ct = pe->data;
3205 dprintf(fd, "# %c %s\n",
3206 got_commitable_get_status(ct),
3207 got_commitable_get_path(ct));
3211 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3213 unlink(a->logmsg_path);
3214 free(a->logmsg_path);
3215 free(initial_content);
3218 /* Editor is done; we can now apply unveil(2) */
3220 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3229 static const struct got_error *
3230 cmd_commit(int argc, char *argv[])
3232 const struct got_error *error = NULL;
3233 struct got_worktree *worktree = NULL;
3234 struct got_repository *repo = NULL;
3235 char *cwd = NULL, *id_str = NULL;
3236 struct got_object_id *id = NULL;
3237 const char *logmsg = NULL;
3238 const char *got_author = getenv("GOT_AUTHOR");
3239 struct collect_commit_logmsg_arg cl_arg;
3240 char *editor = NULL;
3241 int ch, rebase_in_progress, histedit_in_progress;
3242 struct got_pathlist_head paths;
3246 while ((ch = getopt(argc, argv, "m:")) != -1) {
3261 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3262 "unveil", NULL) == -1)
3265 if (got_author == NULL) {
3266 /* TODO: Look current user up in password database */
3267 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3271 cwd = getcwd(NULL, 0);
3273 error = got_error_from_errno("getcwd");
3276 error = got_worktree_open(&worktree, cwd);
3280 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3283 if (rebase_in_progress) {
3284 error = got_error(GOT_ERR_REBASING);
3288 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3293 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3297 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3302 * unveil(2) traverses exec(2); if an editor is used we have
3303 * to apply unveil after the log message has been written.
3305 if (logmsg == NULL || strlen(logmsg) == 0)
3306 error = get_editor(&editor);
3308 error = apply_unveil(got_repo_get_path(repo), 0,
3309 got_worktree_get_root_path(worktree));
3313 cl_arg.editor = editor;
3314 cl_arg.cmdline_log = logmsg;
3315 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3316 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3317 if (!histedit_in_progress) {
3318 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3319 error = got_error(GOT_ERR_COMMIT_BRANCH);
3322 cl_arg.branch_name += 11;
3324 cl_arg.repo_path = got_repo_get_path(repo);
3325 cl_arg.logmsg_path = NULL;
3326 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3327 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3329 if (cl_arg.logmsg_path)
3330 fprintf(stderr, "%s: log message preserved in %s\n",
3331 getprogname(), cl_arg.logmsg_path);
3335 if (cl_arg.logmsg_path)
3336 unlink(cl_arg.logmsg_path);
3338 error = got_object_id_str(&id_str, id);
3341 printf("Created commit %s\n", id_str);
3344 got_repo_close(repo);
3346 got_worktree_close(worktree);
3354 usage_cherrypick(void)
3356 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3360 static const struct got_error *
3361 cmd_cherrypick(int argc, char *argv[])
3363 const struct got_error *error = NULL;
3364 struct got_worktree *worktree = NULL;
3365 struct got_repository *repo = NULL;
3366 char *cwd = NULL, *commit_id_str = NULL;
3367 struct got_object_id *commit_id = NULL;
3368 struct got_commit_object *commit = NULL;
3369 struct got_object_qid *pid;
3370 struct got_reference *head_ref = NULL;
3371 int ch, did_something = 0;
3373 while ((ch = getopt(argc, argv, "")) != -1) {
3385 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3386 "unveil", NULL) == -1)
3392 cwd = getcwd(NULL, 0);
3394 error = got_error_from_errno("getcwd");
3397 error = got_worktree_open(&worktree, cwd);
3401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3405 error = apply_unveil(got_repo_get_path(repo), 0,
3406 got_worktree_get_root_path(worktree));
3410 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3411 GOT_OBJ_TYPE_COMMIT, repo);
3412 if (error != NULL) {
3413 struct got_reference *ref;
3414 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3416 error = got_ref_open(&ref, repo, argv[0], 0);
3419 error = got_ref_resolve(&commit_id, repo, ref);
3424 error = got_object_id_str(&commit_id_str, commit_id);
3428 error = got_ref_open(&head_ref, repo,
3429 got_worktree_get_head_ref_name(worktree), 0);
3433 error = check_same_branch(commit_id, head_ref, NULL, repo);
3435 if (error->code != GOT_ERR_ANCESTRY)
3439 error = got_error(GOT_ERR_SAME_BRANCH);
3443 error = got_object_open_as_commit(&commit, repo, commit_id);
3446 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3447 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3448 commit_id, repo, update_progress, &did_something, check_cancelled,
3454 printf("Merged commit %s\n", commit_id_str);
3457 got_object_commit_close(commit);
3458 free(commit_id_str);
3460 got_ref_close(head_ref);
3462 got_worktree_close(worktree);
3464 got_repo_close(repo);
3471 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3475 static const struct got_error *
3476 cmd_backout(int argc, char *argv[])
3478 const struct got_error *error = NULL;
3479 struct got_worktree *worktree = NULL;
3480 struct got_repository *repo = NULL;
3481 char *cwd = NULL, *commit_id_str = NULL;
3482 struct got_object_id *commit_id = NULL;
3483 struct got_commit_object *commit = NULL;
3484 struct got_object_qid *pid;
3485 struct got_reference *head_ref = NULL;
3486 int ch, did_something = 0;
3488 while ((ch = getopt(argc, argv, "")) != -1) {
3500 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3501 "unveil", NULL) == -1)
3507 cwd = getcwd(NULL, 0);
3509 error = got_error_from_errno("getcwd");
3512 error = got_worktree_open(&worktree, cwd);
3516 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3520 error = apply_unveil(got_repo_get_path(repo), 0,
3521 got_worktree_get_root_path(worktree));
3525 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3526 GOT_OBJ_TYPE_COMMIT, repo);
3527 if (error != NULL) {
3528 struct got_reference *ref;
3529 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3531 error = got_ref_open(&ref, repo, argv[0], 0);
3534 error = got_ref_resolve(&commit_id, repo, ref);
3539 error = got_object_id_str(&commit_id_str, commit_id);
3543 error = got_ref_open(&head_ref, repo,
3544 got_worktree_get_head_ref_name(worktree), 0);
3548 error = check_same_branch(commit_id, head_ref, NULL, repo);
3552 error = got_object_open_as_commit(&commit, repo, commit_id);
3555 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3557 error = got_error(GOT_ERR_ROOT_COMMIT);
3561 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3562 update_progress, &did_something, check_cancelled, NULL);
3567 printf("Backed out commit %s\n", commit_id_str);
3570 got_object_commit_close(commit);
3571 free(commit_id_str);
3573 got_ref_close(head_ref);
3575 got_worktree_close(worktree);
3577 got_repo_close(repo);
3584 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3590 trim_logmsg(char *logmsg, int limit)
3595 len = strlen(logmsg);
3599 nl = strchr(logmsg, '\n');
3604 static const struct got_error *
3605 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3607 const char *logmsg0 = NULL;
3609 logmsg0 = got_object_commit_get_logmsg(commit);
3611 while (isspace((unsigned char)logmsg0[0]))
3614 *logmsg = strdup(logmsg0);
3615 if (*logmsg == NULL)
3616 return got_error_from_errno("strdup");
3618 trim_logmsg(*logmsg, limit);
3622 static const struct got_error *
3623 show_rebase_progress(struct got_commit_object *commit,
3624 struct got_object_id *old_id, struct got_object_id *new_id)
3626 const struct got_error *err;
3627 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3629 err = got_object_id_str(&old_id_str, old_id);
3634 err = got_object_id_str(&new_id_str, new_id);
3639 old_id_str[12] = '\0';
3641 new_id_str[12] = '\0';
3643 err = get_short_logmsg(&logmsg, 42, commit);
3647 printf("%s -> %s: %s\n", old_id_str,
3648 new_id_str ? new_id_str : "no-op change", logmsg);
3655 static const struct got_error *
3656 rebase_progress(void *arg, unsigned char status, const char *path)
3658 unsigned char *rebase_status = arg;
3660 while (path[0] == '/')
3662 printf("%c %s\n", status, path);
3664 if (*rebase_status == GOT_STATUS_CONFLICT)
3666 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3667 *rebase_status = status;
3671 static const struct got_error *
3672 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3673 struct got_reference *branch, struct got_reference *new_base_branch,
3674 struct got_reference *tmp_branch, struct got_repository *repo)
3676 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3677 return got_worktree_rebase_complete(worktree, fileindex,
3678 new_base_branch, tmp_branch, branch, repo);
3681 static const struct got_error *
3682 rebase_commit(struct got_pathlist_head *merged_paths,
3683 struct got_worktree *worktree, struct got_fileindex *fileindex,
3684 struct got_reference *tmp_branch,
3685 struct got_object_id *commit_id, struct got_repository *repo)
3687 const struct got_error *error;
3688 struct got_commit_object *commit;
3689 struct got_object_id *new_commit_id;
3691 error = got_object_open_as_commit(&commit, repo, commit_id);
3695 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3696 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3698 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3700 error = show_rebase_progress(commit, commit_id, NULL);
3702 error = show_rebase_progress(commit, commit_id, new_commit_id);
3703 free(new_commit_id);
3706 got_object_commit_close(commit);
3710 struct check_path_prefix_arg {
3711 const char *path_prefix;
3716 static const struct got_error *
3717 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3718 struct got_blob_object *blob2, struct got_object_id *id1,
3719 struct got_object_id *id2, const char *path1, const char *path2,
3720 struct got_repository *repo)
3722 struct check_path_prefix_arg *a = arg;
3724 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3725 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3726 return got_error(a->errcode);
3731 static const struct got_error *
3732 check_path_prefix(struct got_object_id *parent_id,
3733 struct got_object_id *commit_id, const char *path_prefix,
3734 int errcode, struct got_repository *repo)
3736 const struct got_error *err;
3737 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3738 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3739 struct check_path_prefix_arg cpp_arg;
3741 if (got_path_is_root_dir(path_prefix))
3744 err = got_object_open_as_commit(&commit, repo, commit_id);
3748 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3752 err = got_object_open_as_tree(&tree1, repo,
3753 got_object_commit_get_tree_id(parent_commit));
3757 err = got_object_open_as_tree(&tree2, repo,
3758 got_object_commit_get_tree_id(commit));
3762 cpp_arg.path_prefix = path_prefix;
3763 while (cpp_arg.path_prefix[0] == '/')
3764 cpp_arg.path_prefix++;
3765 cpp_arg.len = strlen(cpp_arg.path_prefix);
3766 cpp_arg.errcode = errcode;
3767 err = got_diff_tree(tree1, tree2, "", "", repo,
3768 check_path_prefix_in_diff, &cpp_arg, 0);
3771 got_object_tree_close(tree1);
3773 got_object_tree_close(tree2);
3775 got_object_commit_close(commit);
3777 got_object_commit_close(parent_commit);
3781 static const struct got_error *
3782 collect_commits(struct got_object_id_queue *commits,
3783 struct got_object_id *initial_commit_id,
3784 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3785 const char *path_prefix, int path_prefix_errcode,
3786 struct got_repository *repo)
3788 const struct got_error *err = NULL;
3789 struct got_commit_graph *graph = NULL;
3790 struct got_object_id *parent_id = NULL;
3791 struct got_object_qid *qid;
3792 struct got_object_id *commit_id = initial_commit_id;
3794 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3798 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3801 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3802 err = got_commit_graph_iter_next(&parent_id, graph);
3804 if (err->code == GOT_ERR_ITER_COMPLETED) {
3805 err = got_error_msg(GOT_ERR_ANCESTRY,
3806 "ran out of commits to rebase before "
3807 "youngest common ancestor commit has "
3810 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3812 err = got_commit_graph_fetch_commits(graph, 1, repo);
3816 err = check_path_prefix(parent_id, commit_id,
3817 path_prefix, path_prefix_errcode, repo);
3821 err = got_object_qid_alloc(&qid, commit_id);
3824 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3825 commit_id = parent_id;
3829 got_commit_graph_close(graph);
3833 static const struct got_error *
3834 cmd_rebase(int argc, char *argv[])
3836 const struct got_error *error = NULL;
3837 struct got_worktree *worktree = NULL;
3838 struct got_repository *repo = NULL;
3839 struct got_fileindex *fileindex = NULL;
3841 struct got_reference *branch = NULL;
3842 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3843 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3844 struct got_object_id *resume_commit_id = NULL;
3845 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3846 struct got_commit_object *commit = NULL;
3847 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3848 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3849 struct got_object_id_queue commits;
3850 struct got_pathlist_head merged_paths;
3851 const struct got_object_id_queue *parent_ids;
3852 struct got_object_qid *qid, *pid;
3854 SIMPLEQ_INIT(&commits);
3855 TAILQ_INIT(&merged_paths);
3857 while ((ch = getopt(argc, argv, "ac")) != -1) {
3863 continue_rebase = 1;
3875 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3876 "unveil", NULL) == -1)
3879 if (abort_rebase && continue_rebase)
3881 else if (abort_rebase || continue_rebase) {
3884 } else if (argc != 1)
3887 cwd = getcwd(NULL, 0);
3889 error = got_error_from_errno("getcwd");
3892 error = got_worktree_open(&worktree, cwd);
3896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3900 error = apply_unveil(got_repo_get_path(repo), 0,
3901 got_worktree_get_root_path(worktree));
3905 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3911 if (!rebase_in_progress) {
3912 error = got_error(GOT_ERR_NOT_REBASING);
3915 error = got_worktree_rebase_continue(&resume_commit_id,
3916 &new_base_branch, &tmp_branch, &branch, &fileindex,
3920 printf("Switching work tree to %s\n",
3921 got_ref_get_symref_target(new_base_branch));
3922 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3923 new_base_branch, update_progress, &did_something);
3926 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3927 goto done; /* nothing else to do */
3930 if (continue_rebase) {
3931 if (!rebase_in_progress) {
3932 error = got_error(GOT_ERR_NOT_REBASING);
3935 error = got_worktree_rebase_continue(&resume_commit_id,
3936 &new_base_branch, &tmp_branch, &branch, &fileindex,
3941 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3942 resume_commit_id, repo);
3946 yca_id = got_object_id_dup(resume_commit_id);
3947 if (yca_id == NULL) {
3948 error = got_error_from_errno("got_object_id_dup");
3952 error = got_ref_open(&branch, repo, argv[0], 0);
3957 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3961 if (!continue_rebase) {
3962 struct got_object_id *base_commit_id;
3964 base_commit_id = got_worktree_get_base_commit_id(worktree);
3965 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3966 base_commit_id, branch_head_commit_id, repo);
3969 if (yca_id == NULL) {
3970 error = got_error_msg(GOT_ERR_ANCESTRY,
3971 "specified branch shares no common ancestry "
3972 "with work tree's branch");
3976 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3978 if (error->code != GOT_ERR_ANCESTRY)
3982 error = got_error_msg(GOT_ERR_SAME_BRANCH,
3983 "specified branch resolves to a commit which "
3984 "is already contained in work tree's branch");
3987 error = got_worktree_rebase_prepare(&new_base_branch,
3988 &tmp_branch, &fileindex, worktree, branch, repo);
3993 commit_id = branch_head_commit_id;
3994 error = got_object_open_as_commit(&commit, repo, commit_id);
3998 parent_ids = got_object_commit_get_parent_ids(commit);
3999 pid = SIMPLEQ_FIRST(parent_ids);
4000 error = collect_commits(&commits, commit_id, pid->id,
4001 yca_id, got_worktree_get_path_prefix(worktree),
4002 GOT_ERR_REBASE_PATH, repo);
4003 got_object_commit_close(commit);
4008 if (SIMPLEQ_EMPTY(&commits)) {
4009 if (continue_rebase)
4010 error = rebase_complete(worktree, fileindex,
4011 branch, new_base_branch, tmp_branch, repo);
4013 error = got_error(GOT_ERR_EMPTY_REBASE);
4018 SIMPLEQ_FOREACH(qid, &commits, entry) {
4019 commit_id = qid->id;
4020 parent_id = pid ? pid->id : yca_id;
4023 error = got_worktree_rebase_merge_files(&merged_paths,
4024 worktree, fileindex, parent_id, commit_id, repo,
4025 rebase_progress, &rebase_status, check_cancelled, NULL);
4029 if (rebase_status == GOT_STATUS_CONFLICT) {
4030 got_worktree_rebase_pathlist_free(&merged_paths);
4034 error = rebase_commit(&merged_paths, worktree, fileindex,
4035 tmp_branch, commit_id, repo);
4036 got_worktree_rebase_pathlist_free(&merged_paths);
4041 if (rebase_status == GOT_STATUS_CONFLICT) {
4042 error = got_worktree_rebase_postpone(worktree, fileindex);
4045 error = got_error_msg(GOT_ERR_CONFLICTS,
4046 "conflicts must be resolved before rebasing can continue");
4048 error = rebase_complete(worktree, fileindex, branch,
4049 new_base_branch, tmp_branch, repo);
4051 got_object_id_queue_free(&commits);
4052 free(branch_head_commit_id);
4053 free(resume_commit_id);
4056 got_object_commit_close(commit);
4058 got_ref_close(branch);
4059 if (new_base_branch)
4060 got_ref_close(new_base_branch);
4062 got_ref_close(tmp_branch);
4064 got_worktree_close(worktree);
4066 got_repo_close(repo);
4071 usage_histedit(void)
4073 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4078 #define GOT_HISTEDIT_PICK 'p'
4079 #define GOT_HISTEDIT_EDIT 'e'
4080 #define GOT_HISTEDIT_FOLD 'f'
4081 #define GOT_HISTEDIT_DROP 'd'
4082 #define GOT_HISTEDIT_MESG 'm'
4084 static struct got_histedit_cmd {
4088 } got_histedit_cmds[] = {
4089 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4090 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4091 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4092 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4093 { GOT_HISTEDIT_MESG, "mesg",
4094 "single-line log message for commit above (open editor if empty)" },
4097 struct got_histedit_list_entry {
4098 TAILQ_ENTRY(got_histedit_list_entry) entry;
4099 struct got_object_id *commit_id;
4100 const struct got_histedit_cmd *cmd;
4103 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4105 static const struct got_error *
4106 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4107 FILE *f, struct got_repository *repo)
4109 const struct got_error *err = NULL;
4110 char *logmsg = NULL, *id_str = NULL;
4111 struct got_commit_object *commit = NULL;
4114 err = got_object_open_as_commit(&commit, repo, commit_id);
4118 err = get_short_logmsg(&logmsg, 34, commit);
4122 err = got_object_id_str(&id_str, commit_id);
4126 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4128 err = got_ferror(f, GOT_ERR_IO);
4131 got_object_commit_close(commit);
4137 static const struct got_error *
4138 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4139 struct got_repository *repo)
4141 const struct got_error *err = NULL;
4142 struct got_object_qid *qid;
4144 if (SIMPLEQ_EMPTY(commits))
4145 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4147 SIMPLEQ_FOREACH(qid, commits, entry) {
4148 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4157 static const struct got_error *
4158 write_cmd_list(FILE *f)
4160 const struct got_error *err = NULL;
4163 n = fprintf(f, "# Available histedit commands:\n");
4165 return got_ferror(f, GOT_ERR_IO);
4167 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4168 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4169 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4172 err = got_ferror(f, GOT_ERR_IO);
4176 n = fprintf(f, "# Commits will be processed in order from top to "
4177 "bottom of this file.\n");
4179 return got_ferror(f, GOT_ERR_IO);
4183 static const struct got_error *
4184 histedit_syntax_error(int lineno)
4186 static char msg[42];
4189 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4191 if (ret == -1 || ret >= sizeof(msg))
4192 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4194 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4197 static const struct got_error *
4198 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4199 char *logmsg, struct got_repository *repo)
4201 const struct got_error *err;
4202 struct got_commit_object *folded_commit = NULL;
4205 err = got_object_id_str(&id_str, hle->commit_id);
4209 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4213 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4214 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4215 got_object_commit_get_logmsg(folded_commit)) == -1) {
4216 err = got_error_from_errno("asprintf");
4221 got_object_commit_close(folded_commit);
4226 static struct got_histedit_list_entry *
4227 get_folded_commits(struct got_histedit_list_entry *hle)
4229 struct got_histedit_list_entry *prev, *folded = NULL;
4231 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4232 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4233 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4234 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4236 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4242 static const struct got_error *
4243 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4244 struct got_repository *repo)
4246 char *logmsg_path = NULL, *id_str = NULL;
4247 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4248 const struct got_error *err = NULL;
4249 struct got_commit_object *commit = NULL;
4251 struct got_histedit_list_entry *folded = NULL;
4253 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4257 folded = get_folded_commits(hle);
4259 while (folded != hle) {
4260 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4261 folded = TAILQ_NEXT(folded, entry);
4264 err = append_folded_commit_msg(&new_msg, folded,
4270 folded = TAILQ_NEXT(folded, entry);
4274 err = got_object_id_str(&id_str, hle->commit_id);
4277 if (asprintf(&new_msg,
4278 "%s\n# original log message of commit %s: %s",
4279 logmsg ? logmsg : "", id_str,
4280 got_object_commit_get_logmsg(commit)) == -1) {
4281 err = got_error_from_errno("asprintf");
4287 err = got_object_id_str(&id_str, hle->commit_id);
4291 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4295 dprintf(fd, logmsg);
4298 err = get_editor(&editor);
4302 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4304 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4307 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4308 if (hle->logmsg == NULL)
4309 err = got_error_from_errno("strdup");
4312 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4313 err = got_error_from_errno2("unlink", logmsg_path);
4318 got_object_commit_close(commit);
4322 static const struct got_error *
4323 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4324 FILE *f, struct got_repository *repo)
4326 const struct got_error *err = NULL;
4327 char *line = NULL, *p, *end;
4331 const struct got_histedit_cmd *cmd;
4332 struct got_object_id *commit_id = NULL;
4333 struct got_histedit_list_entry *hle = NULL;
4336 len = getline(&line, &size, f);
4338 const struct got_error *getline_err;
4341 getline_err = got_error_from_errno("getline");
4342 err = got_ferror(f, getline_err->code);
4347 while (isspace((unsigned char)p[0]))
4349 if (p[0] == '#' || p[0] == '\0') {
4355 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4356 cmd = &got_histedit_cmds[i];
4357 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4358 isspace((unsigned char)p[strlen(cmd->name)])) {
4359 p += strlen(cmd->name);
4362 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4367 if (i == nitems(got_histedit_cmds)) {
4368 err = histedit_syntax_error(lineno);
4371 while (isspace((unsigned char)p[0]))
4373 if (cmd->code == GOT_HISTEDIT_MESG) {
4374 if (hle == NULL || hle->logmsg != NULL) {
4375 err = got_error(GOT_ERR_HISTEDIT_CMD);
4379 err = histedit_edit_logmsg(hle, repo);
4383 hle->logmsg = strdup(p);
4384 if (hle->logmsg == NULL) {
4385 err = got_error_from_errno("strdup");
4394 while (end[0] && !isspace((unsigned char)end[0]))
4398 err = got_object_resolve_id_str(&commit_id, repo, p);
4400 /* override error code */
4401 err = histedit_syntax_error(lineno);
4405 hle = malloc(sizeof(*hle));
4407 err = got_error_from_errno("malloc");
4411 hle->commit_id = commit_id;
4416 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4424 static const struct got_error *
4425 histedit_check_script(struct got_histedit_list *histedit_cmds,
4426 struct got_object_id_queue *commits, struct got_repository *repo)
4428 const struct got_error *err = NULL;
4429 struct got_object_qid *qid;
4430 struct got_histedit_list_entry *hle;
4431 static char msg[80];
4434 if (TAILQ_EMPTY(histedit_cmds))
4435 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4436 "histedit script contains no commands");
4438 SIMPLEQ_FOREACH(qid, commits, entry) {
4439 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4440 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4444 err = got_object_id_str(&id_str, qid->id);
4447 snprintf(msg, sizeof(msg),
4448 "commit %s missing from histedit script", id_str);
4450 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4454 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4455 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4456 "last commit in histedit script cannot be folded");
4461 static const struct got_error *
4462 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4463 const char *path, struct got_object_id_queue *commits,
4464 struct got_repository *repo)
4466 const struct got_error *err = NULL;
4470 err = get_editor(&editor);
4474 if (spawn_editor(editor, path) == -1) {
4475 err = got_error_from_errno("failed spawning editor");
4479 f = fopen(path, "r");
4481 err = got_error_from_errno("fopen");
4484 err = histedit_parse_list(histedit_cmds, f, repo);
4488 err = histedit_check_script(histedit_cmds, commits, repo);
4490 if (f && fclose(f) != 0 && err == NULL)
4491 err = got_error_from_errno("fclose");
4496 static const struct got_error *
4497 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4498 struct got_object_id_queue *, const char *, struct got_repository *);
4500 static const struct got_error *
4501 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4502 struct got_object_id_queue *commits, struct got_repository *repo)
4504 const struct got_error *err;
4508 err = got_opentemp_named(&path, &f, "got-histedit");
4512 err = write_cmd_list(f);
4516 err = histedit_write_commit_list(commits, f, repo);
4520 if (fclose(f) != 0) {
4521 err = got_error_from_errno("fclose");
4526 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4528 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4529 err->code != GOT_ERR_HISTEDIT_CMD)
4531 err = histedit_edit_list_retry(histedit_cmds, err,
4532 commits, path, repo);
4535 if (f && fclose(f) != 0 && err == NULL)
4536 err = got_error_from_errno("fclose");
4537 if (path && unlink(path) != 0 && err == NULL)
4538 err = got_error_from_errno2("unlink", path);
4543 static const struct got_error *
4544 histedit_save_list(struct got_histedit_list *histedit_cmds,
4545 struct got_worktree *worktree, struct got_repository *repo)
4547 const struct got_error *err = NULL;
4550 struct got_histedit_list_entry *hle;
4551 struct got_commit_object *commit = NULL;
4553 err = got_worktree_get_histedit_script_path(&path, worktree);
4557 f = fopen(path, "w");
4559 err = got_error_from_errno2("fopen", path);
4562 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4563 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4569 int n = fprintf(f, "%c %s\n",
4570 GOT_HISTEDIT_MESG, hle->logmsg);
4572 err = got_ferror(f, GOT_ERR_IO);
4578 if (f && fclose(f) != 0 && err == NULL)
4579 err = got_error_from_errno("fclose");
4582 got_object_commit_close(commit);
4587 histedit_free_list(struct got_histedit_list *histedit_cmds)
4589 struct got_histedit_list_entry *hle;
4591 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4592 TAILQ_REMOVE(histedit_cmds, hle, entry);
4597 static const struct got_error *
4598 histedit_load_list(struct got_histedit_list *histedit_cmds,
4599 const char *path, struct got_repository *repo)
4601 const struct got_error *err = NULL;
4604 f = fopen(path, "r");
4606 err = got_error_from_errno2("fopen", path);
4610 err = histedit_parse_list(histedit_cmds, f, repo);
4612 if (f && fclose(f) != 0 && err == NULL)
4613 err = got_error_from_errno("fclose");
4617 static const struct got_error *
4618 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4619 const struct got_error *edit_err, struct got_object_id_queue *commits,
4620 const char *path, struct got_repository *repo)
4622 const struct got_error *err = NULL, *prev_err = edit_err;
4625 while (resp != 'c' && resp != 'r' && resp != 'a') {
4626 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4627 "or (a)bort: ", getprogname(), prev_err->msg);
4630 histedit_free_list(histedit_cmds);
4631 err = histedit_run_editor(histedit_cmds, path, commits,
4634 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4635 err->code != GOT_ERR_HISTEDIT_CMD)
4642 } else if (resp == 'r') {
4643 histedit_free_list(histedit_cmds);
4644 err = histedit_edit_script(histedit_cmds,
4647 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4648 err->code != GOT_ERR_HISTEDIT_CMD)
4655 } else if (resp == 'a') {
4656 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4659 printf("invalid response '%c'\n", resp);
4665 static const struct got_error *
4666 histedit_complete(struct got_worktree *worktree,
4667 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4668 struct got_reference *branch, struct got_repository *repo)
4670 printf("Switching work tree to %s\n",
4671 got_ref_get_symref_target(branch));
4672 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4676 static const struct got_error *
4677 show_histedit_progress(struct got_commit_object *commit,
4678 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4680 const struct got_error *err;
4681 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4683 err = got_object_id_str(&old_id_str, hle->commit_id);
4688 err = got_object_id_str(&new_id_str, new_id);
4693 old_id_str[12] = '\0';
4695 new_id_str[12] = '\0';
4698 logmsg = strdup(hle->logmsg);
4699 if (logmsg == NULL) {
4700 err = got_error_from_errno("strdup");
4703 trim_logmsg(logmsg, 42);
4705 err = get_short_logmsg(&logmsg, 42, commit);
4710 switch (hle->cmd->code) {
4711 case GOT_HISTEDIT_PICK:
4712 case GOT_HISTEDIT_EDIT:
4713 printf("%s -> %s: %s\n", old_id_str,
4714 new_id_str ? new_id_str : "no-op change", logmsg);
4716 case GOT_HISTEDIT_DROP:
4717 case GOT_HISTEDIT_FOLD:
4718 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4731 static const struct got_error *
4732 histedit_commit(struct got_pathlist_head *merged_paths,
4733 struct got_worktree *worktree, struct got_fileindex *fileindex,
4734 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4735 struct got_repository *repo)
4737 const struct got_error *err;
4738 struct got_commit_object *commit;
4739 struct got_object_id *new_commit_id;
4741 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4742 && hle->logmsg == NULL) {
4743 err = histedit_edit_logmsg(hle, repo);
4748 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4752 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4753 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4756 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4758 err = show_histedit_progress(commit, hle, NULL);
4760 err = show_histedit_progress(commit, hle, new_commit_id);
4761 free(new_commit_id);
4764 got_object_commit_close(commit);
4768 static const struct got_error *
4769 histedit_skip_commit(struct got_histedit_list_entry *hle,
4770 struct got_worktree *worktree, struct got_repository *repo)
4772 const struct got_error *error;
4773 struct got_commit_object *commit;
4775 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4780 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4784 error = show_histedit_progress(commit, hle, NULL);
4785 got_object_commit_close(commit);
4789 static const struct got_error *
4790 cmd_histedit(int argc, char *argv[])
4792 const struct got_error *error = NULL;
4793 struct got_worktree *worktree = NULL;
4794 struct got_fileindex *fileindex = NULL;
4795 struct got_repository *repo = NULL;
4797 struct got_reference *branch = NULL;
4798 struct got_reference *tmp_branch = NULL;
4799 struct got_object_id *resume_commit_id = NULL;
4800 struct got_object_id *base_commit_id = NULL;
4801 struct got_object_id *head_commit_id = NULL;
4802 struct got_commit_object *commit = NULL;
4803 int ch, rebase_in_progress = 0, did_something;
4804 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4805 const char *edit_script_path = NULL;
4806 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4807 struct got_object_id_queue commits;
4808 struct got_pathlist_head merged_paths;
4809 const struct got_object_id_queue *parent_ids;
4810 struct got_object_qid *pid;
4811 struct got_histedit_list histedit_cmds;
4812 struct got_histedit_list_entry *hle;
4814 SIMPLEQ_INIT(&commits);
4815 TAILQ_INIT(&histedit_cmds);
4816 TAILQ_INIT(&merged_paths);
4818 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4827 edit_script_path = optarg;
4839 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4840 "unveil", NULL) == -1)
4843 if (abort_edit && continue_edit)
4849 * This command cannot apply unveil(2) in all cases because the
4850 * user may choose to run an editor to edit the histedit script
4851 * and to edit individual commit log messages.
4852 * unveil(2) traverses exec(2); if an editor is used we have to
4853 * apply unveil after edit script and log messages have been written.
4854 * XXX TODO: Make use of unveil(2) where possible.
4857 cwd = getcwd(NULL, 0);
4859 error = got_error_from_errno("getcwd");
4862 error = got_worktree_open(&worktree, cwd);
4866 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4870 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4873 if (rebase_in_progress) {
4874 error = got_error(GOT_ERR_REBASING);
4878 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4882 if (edit_in_progress && abort_edit) {
4883 error = got_worktree_histedit_continue(&resume_commit_id,
4884 &tmp_branch, &branch, &base_commit_id, &fileindex,
4888 printf("Switching work tree to %s\n",
4889 got_ref_get_symref_target(branch));
4890 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4891 branch, base_commit_id, update_progress, &did_something);
4894 printf("Histedit of %s aborted\n",
4895 got_ref_get_symref_target(branch));
4896 goto done; /* nothing else to do */
4897 } else if (abort_edit) {
4898 error = got_error(GOT_ERR_NOT_HISTEDIT);
4902 if (continue_edit) {
4905 if (!edit_in_progress) {
4906 error = got_error(GOT_ERR_NOT_HISTEDIT);
4910 error = got_worktree_get_histedit_script_path(&path, worktree);
4914 error = histedit_load_list(&histedit_cmds, path, repo);
4919 error = got_worktree_histedit_continue(&resume_commit_id,
4920 &tmp_branch, &branch, &base_commit_id, &fileindex,
4925 error = got_ref_resolve(&head_commit_id, repo, branch);
4929 error = got_object_open_as_commit(&commit, repo,
4933 parent_ids = got_object_commit_get_parent_ids(commit);
4934 pid = SIMPLEQ_FIRST(parent_ids);
4936 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4939 error = collect_commits(&commits, head_commit_id, pid->id,
4940 base_commit_id, got_worktree_get_path_prefix(worktree),
4941 GOT_ERR_HISTEDIT_PATH, repo);
4942 got_object_commit_close(commit);
4947 if (edit_in_progress) {
4948 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4952 error = got_ref_open(&branch, repo,
4953 got_worktree_get_head_ref_name(worktree), 0);
4957 error = got_ref_resolve(&head_commit_id, repo, branch);
4958 got_ref_close(branch);
4963 error = got_object_open_as_commit(&commit, repo,
4967 parent_ids = got_object_commit_get_parent_ids(commit);
4968 pid = SIMPLEQ_FIRST(parent_ids);
4970 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4973 error = collect_commits(&commits, head_commit_id, pid->id,
4974 got_worktree_get_base_commit_id(worktree),
4975 got_worktree_get_path_prefix(worktree),
4976 GOT_ERR_HISTEDIT_PATH, repo);
4977 got_object_commit_close(commit);
4982 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4983 &base_commit_id, &fileindex, worktree, repo);
4987 if (edit_script_path) {
4988 error = histedit_load_list(&histedit_cmds,
4989 edit_script_path, repo);
4991 got_worktree_histedit_abort(worktree, fileindex,
4992 repo, branch, base_commit_id,
4993 update_progress, &did_something);
4997 error = histedit_edit_script(&histedit_cmds, &commits,
5000 got_worktree_histedit_abort(worktree, fileindex,
5001 repo, branch, base_commit_id,
5002 update_progress, &did_something);
5008 error = histedit_save_list(&histedit_cmds, worktree,
5011 got_worktree_histedit_abort(worktree, fileindex,
5012 repo, branch, base_commit_id,
5013 update_progress, &did_something);
5019 error = histedit_check_script(&histedit_cmds, &commits, repo);
5023 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5024 if (resume_commit_id) {
5025 if (got_object_id_cmp(hle->commit_id,
5026 resume_commit_id) != 0)
5029 resume_commit_id = NULL;
5030 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5031 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5032 error = histedit_skip_commit(hle, worktree,
5035 error = histedit_commit(NULL, worktree,
5036 fileindex, tmp_branch, hle, repo);
5043 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5044 error = histedit_skip_commit(hle, worktree, repo);
5050 error = got_object_open_as_commit(&commit, repo,
5054 parent_ids = got_object_commit_get_parent_ids(commit);
5055 pid = SIMPLEQ_FIRST(parent_ids);
5057 error = got_worktree_histedit_merge_files(&merged_paths,
5058 worktree, fileindex, pid->id, hle->commit_id, repo,
5059 rebase_progress, &rebase_status, check_cancelled, NULL);
5062 got_object_commit_close(commit);
5065 if (rebase_status == GOT_STATUS_CONFLICT) {
5066 got_worktree_rebase_pathlist_free(&merged_paths);
5070 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5072 error = got_object_id_str(&id_str, hle->commit_id);
5075 printf("Stopping histedit for amending commit %s\n",
5078 got_worktree_rebase_pathlist_free(&merged_paths);
5079 error = got_worktree_histedit_postpone(worktree,
5084 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5085 error = histedit_skip_commit(hle, worktree, repo);
5091 error = histedit_commit(&merged_paths, worktree, fileindex,
5092 tmp_branch, hle, repo);
5093 got_worktree_rebase_pathlist_free(&merged_paths);
5098 if (rebase_status == GOT_STATUS_CONFLICT) {
5099 error = got_worktree_histedit_postpone(worktree, fileindex);
5102 error = got_error_msg(GOT_ERR_CONFLICTS,
5103 "conflicts must be resolved before rebasing can continue");
5105 error = histedit_complete(worktree, fileindex, tmp_branch,
5108 got_object_id_queue_free(&commits);
5109 histedit_free_list(&histedit_cmds);
5110 free(head_commit_id);
5111 free(base_commit_id);
5112 free(resume_commit_id);
5114 got_object_commit_close(commit);
5116 got_ref_close(branch);
5118 got_ref_close(tmp_branch);
5120 got_worktree_close(worktree);
5122 got_repo_close(repo);