2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
59 catch_sigint(int signo)
65 catch_sigpipe(int signo)
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
99 static const struct got_error* cmd_init(int, char *[]);
100 static const struct got_error* cmd_import(int, char *[]);
101 static const struct got_error* cmd_checkout(int, char *[]);
102 static const struct got_error* cmd_update(int, char *[]);
103 static const struct got_error* cmd_log(int, char *[]);
104 static const struct got_error* cmd_diff(int, char *[]);
105 static const struct got_error* cmd_blame(int, char *[]);
106 static const struct got_error* cmd_tree(int, char *[]);
107 static const struct got_error* cmd_status(int, char *[]);
108 static const struct got_error* cmd_ref(int, char *[]);
109 static const struct got_error* cmd_branch(int, char *[]);
110 static const struct got_error* cmd_add(int, char *[]);
111 static const struct got_error* cmd_remove(int, char *[]);
112 static const struct got_error* cmd_revert(int, char *[]);
113 static const struct got_error* cmd_commit(int, char *[]);
114 static const struct got_error* cmd_cherrypick(int, char *[]);
115 static const struct got_error* cmd_backout(int, char *[]);
116 static const struct got_error* cmd_rebase(int, char *[]);
117 static const struct got_error* cmd_histedit(int, char *[]);
119 static struct got_cmd got_commands[] = {
120 { "init", cmd_init, usage_init, "" },
121 { "import", cmd_import, usage_import, "" },
122 { "checkout", cmd_checkout, usage_checkout, "co" },
123 { "update", cmd_update, usage_update, "up" },
124 { "log", cmd_log, usage_log, "" },
125 { "diff", cmd_diff, usage_diff, "" },
126 { "blame", cmd_blame, usage_blame, "" },
127 { "tree", cmd_tree, usage_tree, "" },
128 { "status", cmd_status, usage_status, "st" },
129 { "ref", cmd_ref, usage_ref, "" },
130 { "branch", cmd_branch, usage_branch, "br" },
131 { "add", cmd_add, usage_add, "" },
132 { "remove", cmd_remove, usage_remove, "rm" },
133 { "revert", cmd_revert, usage_revert, "rv" },
134 { "commit", cmd_commit, usage_commit, "ci" },
135 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
136 { "backout", cmd_backout, usage_backout, "bo" },
137 { "rebase", cmd_rebase, usage_rebase, "rb" },
138 { "histedit", cmd_histedit, usage_histedit, "he" },
146 fprintf(stderr, "commands:");
147 for (i = 0; i < nitems(got_commands); i++) {
148 struct got_cmd *cmd = &got_commands[i];
149 fprintf(stderr, " %s", cmd->cmd_name);
155 main(int argc, char *argv[])
160 int hflag = 0, Vflag = 0;
162 setlocale(LC_CTYPE, "");
164 while ((ch = getopt(argc, argv, "hV")) != -1) {
183 got_version_print_str();
190 signal(SIGINT, catch_sigint);
191 signal(SIGPIPE, catch_sigpipe);
193 for (i = 0; i < nitems(got_commands); i++) {
194 const struct got_error *error;
196 cmd = &got_commands[i];
198 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
199 strcmp(cmd->cmd_alias, argv[0]) != 0)
203 got_commands[i].cmd_usage();
205 error = got_commands[i].cmd_main(argc, argv);
206 if (error && !(sigint_received || sigpipe_received)) {
207 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
214 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
222 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
229 static const struct got_error *
230 get_editor(char **abspath)
232 const struct got_error *err = NULL;
235 editor = getenv("VISUAL");
237 editor = getenv("EDITOR");
240 err = got_path_find_prog(abspath, editor);
245 if (*abspath == NULL) {
246 *abspath = strdup("/bin/ed");
247 if (*abspath == NULL)
248 return got_error_from_errno("strdup");
254 static const struct got_error *
255 apply_unveil(const char *repo_path, int repo_read_only,
256 const char *worktree_path)
258 const struct got_error *err;
261 if (unveil("gmon.out", "rwc") != 0)
262 return got_error_from_errno2("unveil", "gmon.out");
264 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 return got_error_from_errno2("unveil", repo_path);
267 if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 return got_error_from_errno2("unveil", worktree_path);
270 if (unveil("/tmp", "rwc") != 0)
271 return got_error_from_errno2("unveil", "/tmp");
273 err = got_privsep_unveil_exec_helpers();
277 if (unveil(NULL, NULL) != 0)
278 return got_error_from_errno("unveil");
286 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
290 static const struct got_error *
291 cmd_init(int argc, char *argv[])
293 const struct got_error *error = NULL;
294 char *repo_path = NULL;
297 while ((ch = getopt(argc, argv, "")) != -1) {
309 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
315 repo_path = strdup(argv[0]);
316 if (repo_path == NULL)
317 return got_error_from_errno("strdup");
319 got_path_strip_trailing_slashes(repo_path);
321 error = got_path_mkdir(repo_path);
323 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
326 error = apply_unveil(repo_path, 0, NULL);
330 error = got_repo_init(repo_path);
342 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
343 "[-r repository-path] [-I pattern] path\n", getprogname());
348 spawn_editor(const char *editor, const char *file)
351 sig_t sighup, sigint, sigquit;
354 sighup = signal(SIGHUP, SIG_IGN);
355 sigint = signal(SIGINT, SIG_IGN);
356 sigquit = signal(SIGQUIT, SIG_IGN);
358 switch (pid = fork()) {
362 execl(editor, editor, file, (char *)NULL);
366 while (waitpid(pid, &st, 0) == -1)
371 (void)signal(SIGHUP, sighup);
372 (void)signal(SIGINT, sigint);
373 (void)signal(SIGQUIT, sigquit);
375 if (!WIFEXITED(st)) {
380 return WEXITSTATUS(st);
383 static const struct got_error *
384 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
385 const char *initial_content)
387 const struct got_error *err = NULL;
391 int content_changed = 0;
396 if (stat(logmsg_path, &st) == -1)
397 return got_error_from_errno2("stat", logmsg_path);
399 if (spawn_editor(editor, logmsg_path) == -1)
400 return got_error_from_errno("failed spawning editor");
402 if (stat(logmsg_path, &st2) == -1)
403 return got_error_from_errno("stat");
405 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
406 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
407 "no changes made to commit message, aborting");
409 *logmsg = malloc(st2.st_size + 1);
411 return got_error_from_errno("malloc");
415 fp = fopen(logmsg_path, "r");
417 err = got_error_from_errno("fopen");
420 while (fgets(buf, sizeof(buf), fp) != NULL) {
421 if (!content_changed && strcmp(buf, initial_content) != 0)
423 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
424 continue; /* remove comments and leading empty lines */
425 len = strlcat(*logmsg, buf, st2.st_size);
429 while (len > 0 && (*logmsg)[len - 1] == '\n') {
430 (*logmsg)[len - 1] = '\0';
434 if (len == 0 || !content_changed)
435 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
436 "commit message cannot be empty, aborting");
445 static const struct got_error *
446 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
447 const char *branch_name)
449 char *initial_content = NULL, *logmsg_path = NULL;
450 const struct got_error *err = NULL;
453 if (asprintf(&initial_content,
454 "\n# %s to be imported to branch %s\n", path_dir,
456 return got_error_from_errno("asprintf");
458 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
462 dprintf(fd, initial_content);
465 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
467 free(initial_content);
472 static const struct got_error *
473 import_progress(void *arg, const char *path)
475 printf("A %s\n", path);
479 static const struct got_error *
480 cmd_import(int argc, char *argv[])
482 const struct got_error *error = NULL;
483 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
485 const char *got_author = getenv("GOT_AUTHOR");
486 const char *branch_name = "master";
487 char *refname = NULL, *id_str = NULL;
488 struct got_repository *repo = NULL;
489 struct got_reference *branch_ref = NULL, *head_ref = NULL;
490 struct got_object_id *new_commit_id = NULL;
492 struct got_pathlist_head ignores;
493 struct got_pathlist_entry *pe;
495 TAILQ_INIT(&ignores);
497 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
500 branch_name = optarg;
503 logmsg = strdup(optarg);
504 if (logmsg == NULL) {
505 error = got_error_from_errno("strdup");
510 repo_path = realpath(optarg, NULL);
511 if (repo_path == NULL) {
512 error = got_error_from_errno("realpath");
517 if (optarg[0] == '\0')
519 error = got_pathlist_insert(&pe, &ignores, optarg,
534 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
541 if (got_author == NULL) {
542 /* TODO: Look current user up in password database */
543 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
547 if (repo_path == NULL) {
548 repo_path = getcwd(NULL, 0);
549 if (repo_path == NULL)
550 return got_error_from_errno("getcwd");
552 got_path_strip_trailing_slashes(repo_path);
553 error = got_repo_open(&repo, repo_path);
557 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
558 error = got_error_from_errno("asprintf");
562 error = got_ref_open(&branch_ref, repo, refname, 0);
564 if (error->code != GOT_ERR_NOT_REF)
567 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
568 "import target branch already exists");
572 path_dir = realpath(argv[0], NULL);
573 if (path_dir == NULL) {
574 error = got_error_from_errno("realpath");
577 got_path_strip_trailing_slashes(path_dir);
580 * unveil(2) traverses exec(2); if an editor is used we have
581 * to apply unveil after the log message has been written.
583 if (logmsg == NULL || strlen(logmsg) == 0) {
584 error = get_editor(&editor);
587 error = collect_import_msg(&logmsg, editor, path_dir, refname);
592 if (unveil(path_dir, "r") != 0)
593 return got_error_from_errno2("unveil", path_dir);
595 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
599 error = got_repo_import(&new_commit_id, path_dir, logmsg,
600 got_author, &ignores, repo, import_progress, NULL);
604 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
608 error = got_ref_write(branch_ref, repo);
612 error = got_object_id_str(&id_str, new_commit_id);
616 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
618 if (error->code != GOT_ERR_NOT_REF)
621 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
626 error = got_ref_write(head_ref, repo);
631 printf("Created branch %s with commit %s\n",
632 got_ref_get_name(branch_ref), id_str);
640 got_ref_close(branch_ref);
642 got_ref_close(head_ref);
649 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
650 "[-p prefix] repository-path [worktree-path]\n", getprogname());
654 static const struct got_error *
655 checkout_progress(void *arg, unsigned char status, const char *path)
657 char *worktree_path = arg;
659 /* Base commit bump happens silently. */
660 if (status == GOT_STATUS_BUMP_BASE)
663 while (path[0] == '/')
666 printf("%c %s/%s\n", status, worktree_path, path);
670 static const struct got_error *
671 check_cancelled(void *arg)
673 if (sigint_received || sigpipe_received)
674 return got_error(GOT_ERR_CANCELLED);
678 static const struct got_error *
679 check_linear_ancestry(struct got_object_id *commit_id,
680 struct got_object_id *base_commit_id, struct got_repository *repo)
682 const struct got_error *err = NULL;
683 struct got_object_id *yca_id;
685 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
686 commit_id, base_commit_id, repo);
691 return got_error(GOT_ERR_ANCESTRY);
694 * Require a straight line of history between the target commit
695 * and the work tree's base commit.
697 * Non-linear situations such as this require a rebase:
699 * (commit) D F (base_commit)
707 * 'got update' only handles linear cases:
708 * Update forwards in time: A (base/yca) - B - C - D (commit)
709 * Update backwards in time: D (base) - C - B - A (commit/yca)
711 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
712 got_object_id_cmp(base_commit_id, yca_id) != 0)
713 return got_error(GOT_ERR_ANCESTRY);
719 static const struct got_error *
720 check_same_branch(struct got_object_id *commit_id,
721 struct got_reference *head_ref, struct got_object_id *yca_id,
722 struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_commit_graph *graph = NULL;
726 struct got_object_id *head_commit_id = NULL;
727 int is_same_branch = 0;
729 err = got_ref_resolve(&head_commit_id, repo, head_ref);
733 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
737 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
742 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
746 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
751 struct got_object_id *id;
752 err = got_commit_graph_iter_next(&id, graph);
754 if (err->code == GOT_ERR_ITER_COMPLETED) {
757 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
759 err = got_commit_graph_fetch_commits(graph, 1,
766 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
768 if (got_object_id_cmp(id, commit_id) == 0) {
776 got_commit_graph_close(graph);
777 free(head_commit_id);
778 if (!err && !is_same_branch)
779 err = got_error(GOT_ERR_ANCESTRY);
783 static const struct got_error *
784 resolve_commit_arg(struct got_object_id **commit_id,
785 const char *commit_id_arg, struct got_repository *repo)
787 const struct got_error *err;
788 struct got_reference *ref;
790 err = got_ref_open(&ref, repo, commit_id_arg, 0);
792 err = got_ref_resolve(commit_id, repo, ref);
795 if (err->code != GOT_ERR_NOT_REF)
797 err = got_repo_match_object_id_prefix(commit_id,
798 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
803 static const struct got_error *
804 cmd_checkout(int argc, char *argv[])
806 const struct got_error *error = NULL;
807 struct got_repository *repo = NULL;
808 struct got_reference *head_ref = NULL;
809 struct got_worktree *worktree = NULL;
810 char *repo_path = NULL;
811 char *worktree_path = NULL;
812 const char *path_prefix = "";
813 const char *branch_name = GOT_REF_HEAD;
814 char *commit_id_str = NULL;
815 int ch, same_path_prefix;
816 struct got_pathlist_head paths;
820 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
823 branch_name = optarg;
826 commit_id_str = strdup(optarg);
827 if (commit_id_str == NULL)
828 return got_error_from_errno("strdup");
831 path_prefix = optarg;
843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
844 "unveil", NULL) == -1)
848 char *cwd, *base, *dotgit;
849 repo_path = realpath(argv[0], NULL);
850 if (repo_path == NULL)
851 return got_error_from_errno2("realpath", argv[0]);
852 cwd = getcwd(NULL, 0);
854 error = got_error_from_errno("getcwd");
857 if (path_prefix[0]) {
858 base = basename(path_prefix);
860 error = got_error_from_errno2("basename",
865 base = basename(repo_path);
867 error = got_error_from_errno2("basename",
872 dotgit = strstr(base, ".git");
875 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
876 error = got_error_from_errno("asprintf");
881 } else if (argc == 2) {
882 repo_path = realpath(argv[0], NULL);
883 if (repo_path == NULL) {
884 error = got_error_from_errno2("realpath", argv[0]);
887 worktree_path = realpath(argv[1], NULL);
888 if (worktree_path == NULL) {
889 if (errno != ENOENT) {
890 error = got_error_from_errno2("realpath",
894 worktree_path = strdup(argv[1]);
895 if (worktree_path == NULL) {
896 error = got_error_from_errno("strdup");
903 got_path_strip_trailing_slashes(repo_path);
904 got_path_strip_trailing_slashes(worktree_path);
906 error = got_repo_open(&repo, repo_path);
910 /* Pre-create work tree path for unveil(2) */
911 error = got_path_mkdir(worktree_path);
913 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
915 if (!got_path_dir_is_empty(worktree_path)) {
916 error = got_error_path(worktree_path,
917 GOT_ERR_DIR_NOT_EMPTY);
922 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
926 error = got_ref_open(&head_ref, repo, branch_name, 0);
930 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
931 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
934 error = got_worktree_open(&worktree, worktree_path);
938 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
942 if (!same_path_prefix) {
943 error = got_error(GOT_ERR_PATH_PREFIX);
948 struct got_object_id *commit_id;
949 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
952 error = check_linear_ancestry(commit_id,
953 got_worktree_get_base_commit_id(worktree), repo);
958 error = check_same_branch(commit_id, head_ref, NULL, repo);
961 error = got_worktree_set_base_commit_id(worktree, repo,
968 error = got_pathlist_append(&paths, "", NULL);
971 error = got_worktree_checkout_files(worktree, &paths, repo,
972 checkout_progress, worktree_path, check_cancelled, NULL);
976 printf("Now shut up and hack\n");
979 got_pathlist_free(&paths);
989 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
994 static const struct got_error *
995 update_progress(void *arg, unsigned char status, const char *path)
997 int *did_something = arg;
999 if (status == GOT_STATUS_EXISTS)
1004 /* Base commit bump happens silently. */
1005 if (status == GOT_STATUS_BUMP_BASE)
1008 while (path[0] == '/')
1010 printf("%c %s\n", status, path);
1014 static const struct got_error *
1015 switch_head_ref(struct got_reference *head_ref,
1016 struct got_object_id *commit_id, struct got_worktree *worktree,
1017 struct got_repository *repo)
1019 const struct got_error *err = NULL;
1021 int ref_has_moved = 0;
1023 /* Trivial case: switching between two different references. */
1024 if (strcmp(got_ref_get_name(head_ref),
1025 got_worktree_get_head_ref_name(worktree)) != 0) {
1026 printf("Switching work tree from %s to %s\n",
1027 got_worktree_get_head_ref_name(worktree),
1028 got_ref_get_name(head_ref));
1029 return got_worktree_set_head_ref(worktree, head_ref);
1032 err = check_linear_ancestry(commit_id,
1033 got_worktree_get_base_commit_id(worktree), repo);
1035 if (err->code != GOT_ERR_ANCESTRY)
1042 /* Switching to a rebased branch with the same reference name. */
1043 err = got_object_id_str(&base_id_str,
1044 got_worktree_get_base_commit_id(worktree));
1047 printf("Reference %s now points at a different branch\n",
1048 got_worktree_get_head_ref_name(worktree));
1049 printf("Switching work tree from %s to %s\n", base_id_str,
1050 got_worktree_get_head_ref_name(worktree));
1054 static const struct got_error *
1055 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1057 const struct got_error *err;
1060 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1064 return got_error(GOT_ERR_REBASING);
1066 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1070 return got_error(GOT_ERR_HISTEDIT_BUSY);
1075 static const struct got_error *
1076 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1077 char *argv[], struct got_worktree *worktree)
1079 const struct got_error *err;
1086 return got_error_from_errno("strdup");
1087 return got_pathlist_append(paths, path, NULL);
1090 for (i = 0; i < argc; i++) {
1091 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1094 err = got_pathlist_append(paths, path, NULL);
1104 static const struct got_error *
1105 cmd_update(int argc, char *argv[])
1107 const struct got_error *error = NULL;
1108 struct got_repository *repo = NULL;
1109 struct got_worktree *worktree = NULL;
1110 char *worktree_path = NULL;
1111 struct got_object_id *commit_id = NULL;
1112 char *commit_id_str = NULL;
1113 const char *branch_name = NULL;
1114 struct got_reference *head_ref = NULL;
1115 struct got_pathlist_head paths;
1116 struct got_pathlist_entry *pe;
1117 int ch, did_something = 0;
1121 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1124 branch_name = optarg;
1127 commit_id_str = strdup(optarg);
1128 if (commit_id_str == NULL)
1129 return got_error_from_errno("strdup");
1141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1142 "unveil", NULL) == -1)
1145 worktree_path = getcwd(NULL, 0);
1146 if (worktree_path == NULL) {
1147 error = got_error_from_errno("getcwd");
1150 error = got_worktree_open(&worktree, worktree_path);
1154 error = check_rebase_or_histedit_in_progress(worktree);
1158 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1162 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1166 error = apply_unveil(got_repo_get_path(repo), 0,
1167 got_worktree_get_root_path(worktree));
1171 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1172 got_worktree_get_head_ref_name(worktree), 0);
1175 if (commit_id_str == NULL) {
1176 error = got_ref_resolve(&commit_id, repo, head_ref);
1179 error = got_object_id_str(&commit_id_str, commit_id);
1183 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1184 free(commit_id_str);
1185 commit_id_str = NULL;
1188 error = got_object_id_str(&commit_id_str, commit_id);
1194 struct got_object_id *head_commit_id;
1195 TAILQ_FOREACH(pe, &paths, entry) {
1196 if (pe->path_len == 0)
1198 error = got_error_msg(GOT_ERR_BAD_PATH,
1199 "switching between branches requires that "
1200 "the entire work tree gets updated");
1203 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1206 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1207 free(head_commit_id);
1210 error = check_same_branch(commit_id, head_ref, NULL, repo);
1213 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1217 error = check_linear_ancestry(commit_id,
1218 got_worktree_get_base_commit_id(worktree), repo);
1219 if (error != NULL) {
1220 if (error->code == GOT_ERR_ANCESTRY)
1221 error = got_error(GOT_ERR_BRANCH_MOVED);
1224 error = check_same_branch(commit_id, head_ref, NULL, repo);
1229 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1231 error = got_worktree_set_base_commit_id(worktree, repo,
1237 error = got_worktree_checkout_files(worktree, &paths, repo,
1238 update_progress, &did_something, check_cancelled, NULL);
1243 printf("Updated to commit %s\n", commit_id_str);
1245 printf("Already up-to-date\n");
1247 free(worktree_path);
1248 TAILQ_FOREACH(pe, &paths, entry)
1249 free((char *)pe->path);
1250 got_pathlist_free(&paths);
1252 free(commit_id_str);
1256 static const struct got_error *
1257 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1258 int diff_context, struct got_repository *repo)
1260 const struct got_error *err = NULL;
1261 struct got_tree_object *tree1 = NULL, *tree2;
1262 struct got_object_qid *qid;
1263 char *id_str1 = NULL, *id_str2;
1264 struct got_diff_blob_output_unidiff_arg arg;
1266 err = got_object_open_as_tree(&tree2, repo,
1267 got_object_commit_get_tree_id(commit));
1271 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1273 struct got_commit_object *pcommit;
1275 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1279 err = got_object_open_as_tree(&tree1, repo,
1280 got_object_commit_get_tree_id(pcommit));
1281 got_object_commit_close(pcommit);
1285 err = got_object_id_str(&id_str1, qid->id);
1290 err = got_object_id_str(&id_str2, id);
1294 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1295 arg.diff_context = diff_context;
1296 arg.outfile = stdout;
1297 err = got_diff_tree(tree1, tree2, "", "", repo,
1298 got_diff_blob_output_unidiff, &arg, 1);
1301 got_object_tree_close(tree1);
1302 got_object_tree_close(tree2);
1309 get_datestr(time_t *time, char *datebuf)
1311 char *p, *s = ctime_r(time, datebuf);
1312 p = strchr(s, '\n');
1318 static const struct got_error *
1319 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1320 struct got_repository *repo, int show_patch, int diff_context,
1321 struct got_reflist_head *refs)
1323 const struct got_error *err = NULL;
1324 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1326 time_t committer_time;
1327 const char *author, *committer;
1328 char *refs_str = NULL;
1329 struct got_reflist_entry *re;
1331 SIMPLEQ_FOREACH(re, refs, entry) {
1334 if (got_object_id_cmp(re->id, id) != 0)
1336 name = got_ref_get_name(re->ref);
1337 if (strcmp(name, GOT_REF_HEAD) == 0)
1339 if (strncmp(name, "refs/", 5) == 0)
1341 if (strncmp(name, "got/", 4) == 0)
1343 if (strncmp(name, "heads/", 6) == 0)
1345 if (strncmp(name, "remotes/", 8) == 0)
1348 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1350 err = got_error_from_errno("asprintf");
1356 err = got_object_id_str(&id_str, id);
1360 printf("-----------------------------------------------\n");
1361 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1362 refs_str ? refs_str : "", refs_str ? ")" : "");
1367 printf("from: %s\n", got_object_commit_get_author(commit));
1368 committer_time = got_object_commit_get_committer_time(commit);
1369 datestr = get_datestr(&committer_time, datebuf);
1370 printf("date: %s UTC\n", datestr);
1371 author = got_object_commit_get_author(commit);
1372 committer = got_object_commit_get_committer(commit);
1373 if (strcmp(author, committer) != 0)
1374 printf("via: %s\n", committer);
1375 if (got_object_commit_get_nparents(commit) > 1) {
1376 const struct got_object_id_queue *parent_ids;
1377 struct got_object_qid *qid;
1379 parent_ids = got_object_commit_get_parent_ids(commit);
1380 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1381 err = got_object_id_str(&id_str, qid->id);
1384 printf("parent %d: %s\n", n++, id_str);
1389 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1390 if (logmsg0 == NULL)
1391 return got_error_from_errno("strdup");
1395 line = strsep(&logmsg, "\n");
1397 printf(" %s\n", line);
1402 err = print_patch(commit, id, diff_context, repo);
1407 if (fflush(stdout) != 0 && err == NULL)
1408 err = got_error_from_errno("fflush");
1412 static const struct got_error *
1413 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1414 char *path, int show_patch, int diff_context, int limit,
1415 int first_parent_traversal, struct got_reflist_head *refs)
1417 const struct got_error *err;
1418 struct got_commit_graph *graph;
1420 err = got_commit_graph_open(&graph, root_id, path,
1421 first_parent_traversal, repo);
1424 err = got_commit_graph_iter_start(graph, root_id, repo);
1428 struct got_commit_object *commit;
1429 struct got_object_id *id;
1431 if (sigint_received || sigpipe_received)
1434 err = got_commit_graph_iter_next(&id, graph);
1436 if (err->code == GOT_ERR_ITER_COMPLETED) {
1440 if (err->code != GOT_ERR_ITER_NEED_MORE)
1442 err = got_commit_graph_fetch_commits(graph, 1, repo);
1451 err = got_object_open_as_commit(&commit, repo, id);
1454 err = print_commit(commit, id, repo, show_patch, diff_context,
1456 got_object_commit_close(commit);
1457 if (err || (limit && --limit == 0))
1461 got_commit_graph_close(graph);
1468 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1469 "[-r repository-path] [path]\n", getprogname());
1473 static const struct got_error *
1474 cmd_log(int argc, char *argv[])
1476 const struct got_error *error;
1477 struct got_repository *repo = NULL;
1478 struct got_worktree *worktree = NULL;
1479 struct got_commit_object *commit = NULL;
1480 struct got_object_id *id = NULL;
1481 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1482 char *start_commit = NULL;
1483 int diff_context = 3, ch;
1484 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1486 struct got_reflist_head refs;
1488 SIMPLEQ_INIT(&refs);
1491 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1497 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1503 start_commit = optarg;
1506 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1509 err(1, "-C option %s", errstr);
1512 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1514 err(1, "-l option %s", errstr);
1517 first_parent_traversal = 1;
1520 repo_path = realpath(optarg, NULL);
1521 if (repo_path == NULL)
1522 err(1, "-r option");
1523 got_path_strip_trailing_slashes(repo_path);
1534 cwd = getcwd(NULL, 0);
1536 error = got_error_from_errno("getcwd");
1540 error = got_worktree_open(&worktree, cwd);
1541 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1548 error = got_error_from_errno("strdup");
1551 } else if (argc == 1) {
1553 error = got_worktree_resolve_path(&path, worktree,
1558 path = strdup(argv[0]);
1560 error = got_error_from_errno("strdup");
1567 if (repo_path == NULL) {
1568 repo_path = worktree ?
1569 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1571 if (repo_path == NULL) {
1572 error = got_error_from_errno("strdup");
1576 error = got_repo_open(&repo, repo_path);
1580 error = apply_unveil(got_repo_get_path(repo), 1,
1581 worktree ? got_worktree_get_root_path(worktree) : NULL);
1585 if (start_commit == NULL) {
1586 struct got_reference *head_ref;
1587 error = got_ref_open(&head_ref, repo,
1588 worktree ? got_worktree_get_head_ref_name(worktree)
1592 error = got_ref_resolve(&id, repo, head_ref);
1593 got_ref_close(head_ref);
1596 error = got_object_open_as_commit(&commit, repo, id);
1598 struct got_reference *ref;
1599 error = got_ref_open(&ref, repo, start_commit, 0);
1600 if (error == NULL) {
1602 error = got_ref_resolve(&id, repo, ref);
1606 error = got_object_get_type(&obj_type, repo, id);
1609 if (obj_type == GOT_OBJ_TYPE_TAG) {
1610 struct got_tag_object *tag;
1611 error = got_object_open_as_tag(&tag, repo, id);
1614 if (got_object_tag_get_object_type(tag) !=
1615 GOT_OBJ_TYPE_COMMIT) {
1616 got_object_tag_close(tag);
1617 error = got_error(GOT_ERR_OBJ_TYPE);
1621 id = got_object_id_dup(
1622 got_object_tag_get_object_id(tag));
1624 error = got_error_from_errno(
1625 "got_object_id_dup");
1626 got_object_tag_close(tag);
1629 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1630 error = got_error(GOT_ERR_OBJ_TYPE);
1633 error = got_object_open_as_commit(&commit, repo, id);
1637 if (commit == NULL) {
1638 error = got_repo_match_object_id_prefix(&id,
1639 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1647 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1652 path = in_repo_path;
1655 error = got_ref_list(&refs, repo);
1659 error = print_commits(id, repo, path, show_patch,
1660 diff_context, limit, first_parent_traversal, &refs);
1667 got_worktree_close(worktree);
1669 const struct got_error *repo_error;
1670 repo_error = got_repo_close(repo);
1674 got_ref_list_free(&refs);
1681 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1682 "[object1 object2 | path]\n", getprogname());
1686 struct print_diff_arg {
1687 struct got_repository *repo;
1688 struct got_worktree *worktree;
1694 static const struct got_error *
1695 print_diff(void *arg, unsigned char status, const char *path,
1696 struct got_object_id *blob_id, struct got_object_id *commit_id)
1698 struct print_diff_arg *a = arg;
1699 const struct got_error *err = NULL;
1700 struct got_blob_object *blob1 = NULL;
1702 char *abspath = NULL;
1705 if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1706 status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1709 if (!a->header_shown) {
1710 printf("diff %s %s\n", a->id_str,
1711 got_worktree_get_root_path(a->worktree));
1712 a->header_shown = 1;
1715 if (status != GOT_STATUS_ADD) {
1716 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1722 if (status != GOT_STATUS_DELETE) {
1723 if (asprintf(&abspath, "%s/%s",
1724 got_worktree_get_root_path(a->worktree), path) == -1) {
1725 err = got_error_from_errno("asprintf");
1729 f2 = fopen(abspath, "r");
1731 err = got_error_from_errno2("fopen", abspath);
1734 if (lstat(abspath, &sb) == -1) {
1735 err = got_error_from_errno2("lstat", abspath);
1741 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1745 got_object_blob_close(blob1);
1746 if (f2 && fclose(f2) != 0 && err == NULL)
1747 err = got_error_from_errno("fclose");
1752 static const struct got_error *
1753 cmd_diff(int argc, char *argv[])
1755 const struct got_error *error;
1756 struct got_repository *repo = NULL;
1757 struct got_worktree *worktree = NULL;
1758 char *cwd = NULL, *repo_path = NULL;
1759 struct got_object_id *id1 = NULL, *id2 = NULL;
1760 const char *id_str1 = NULL, *id_str2 = NULL;
1761 char *label1 = NULL, *label2 = NULL;
1763 int diff_context = 3, ch;
1768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1773 while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1776 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1778 err(1, "-C option %s", errstr);
1781 repo_path = realpath(optarg, NULL);
1782 if (repo_path == NULL)
1783 err(1, "-r option");
1784 got_path_strip_trailing_slashes(repo_path);
1795 cwd = getcwd(NULL, 0);
1797 error = got_error_from_errno("getcwd");
1800 error = got_worktree_open(&worktree, cwd);
1801 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1804 if (worktree == NULL) {
1805 error = got_error(GOT_ERR_NOT_WORKTREE);
1810 "-r option can't be used when diffing a work tree");
1811 repo_path = strdup(got_worktree_get_repo_path(worktree));
1812 if (repo_path == NULL) {
1813 error = got_error_from_errno("strdup");
1817 error = got_worktree_resolve_path(&path, worktree,
1824 error = got_error_from_errno("strdup");
1828 } else if (argc == 2) {
1831 if (worktree && repo_path == NULL) {
1833 strdup(got_worktree_get_repo_path(worktree));
1834 if (repo_path == NULL) {
1835 error = got_error_from_errno("strdup");
1842 if (repo_path == NULL) {
1843 repo_path = getcwd(NULL, 0);
1844 if (repo_path == NULL)
1845 return got_error_from_errno("getcwd");
1848 error = got_repo_open(&repo, repo_path);
1853 error = apply_unveil(got_repo_get_path(repo), 1,
1854 worktree ? got_worktree_get_root_path(worktree) : NULL);
1859 struct print_diff_arg arg;
1860 struct got_pathlist_head paths;
1865 error = got_object_id_str(&id_str,
1866 got_worktree_get_base_commit_id(worktree));
1870 arg.worktree = worktree;
1871 arg.diff_context = diff_context;
1872 arg.id_str = id_str;
1873 arg.header_shown = 0;
1875 error = got_pathlist_append(&paths, path, NULL);
1879 error = got_worktree_status(worktree, &paths, repo, print_diff,
1880 &arg, check_cancelled, NULL);
1882 got_pathlist_free(&paths);
1886 error = got_repo_match_object_id_prefix(&id1, id_str1,
1887 GOT_OBJ_TYPE_ANY, repo);
1889 struct got_reference *ref;
1890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1892 error = got_ref_open(&ref, repo, id_str1, 0);
1895 label1 = strdup(got_ref_get_name(ref));
1896 if (label1 == NULL) {
1897 error = got_error_from_errno("strdup");
1900 error = got_ref_resolve(&id1, repo, ref);
1905 error = got_object_id_str(&label1, id1);
1906 if (label1 == NULL) {
1907 error = got_error_from_errno("strdup");
1912 error = got_repo_match_object_id_prefix(&id2, id_str2,
1913 GOT_OBJ_TYPE_ANY, repo);
1915 struct got_reference *ref;
1916 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1918 error = got_ref_open(&ref, repo, id_str2, 0);
1921 label2 = strdup(got_ref_get_name(ref));
1922 if (label2 == NULL) {
1923 error = got_error_from_errno("strdup");
1926 error = got_ref_resolve(&id2, repo, ref);
1931 error = got_object_id_str(&label2, id2);
1932 if (label2 == NULL) {
1933 error = got_error_from_errno("strdup");
1938 error = got_object_get_type(&type1, repo, id1);
1942 error = got_object_get_type(&type2, repo, id2);
1946 if (type1 != type2) {
1947 error = got_error(GOT_ERR_OBJ_TYPE);
1952 case GOT_OBJ_TYPE_BLOB:
1953 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1954 diff_context, repo, stdout);
1956 case GOT_OBJ_TYPE_TREE:
1957 error = got_diff_objects_as_trees(id1, id2, "", "",
1958 diff_context, repo, stdout);
1960 case GOT_OBJ_TYPE_COMMIT:
1961 printf("diff %s %s\n", label1, label2);
1962 error = got_diff_objects_as_commits(id1, id2, diff_context,
1966 error = got_error(GOT_ERR_OBJ_TYPE);
1976 got_worktree_close(worktree);
1978 const struct got_error *repo_error;
1979 repo_error = got_repo_close(repo);
1990 "usage: %s blame [-c commit] [-r repository-path] path\n",
1995 static const struct got_error *
1996 cmd_blame(int argc, char *argv[])
1998 const struct got_error *error;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2002 struct got_object_id *commit_id = NULL;
2003 char *commit_id_str = NULL;
2007 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2012 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2015 commit_id_str = optarg;
2018 repo_path = realpath(optarg, NULL);
2019 if (repo_path == NULL)
2020 err(1, "-r option");
2021 got_path_strip_trailing_slashes(repo_path);
2037 cwd = getcwd(NULL, 0);
2039 error = got_error_from_errno("getcwd");
2042 if (repo_path == NULL) {
2043 error = got_worktree_open(&worktree, cwd);
2044 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2050 strdup(got_worktree_get_repo_path(worktree));
2051 if (repo_path == NULL)
2052 error = got_error_from_errno("strdup");
2056 repo_path = strdup(cwd);
2057 if (repo_path == NULL) {
2058 error = got_error_from_errno("strdup");
2064 error = got_repo_open(&repo, repo_path);
2068 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2073 const char *prefix = got_worktree_get_path_prefix(worktree);
2074 char *p, *worktree_subdir = cwd +
2075 strlen(got_worktree_get_root_path(worktree));
2076 if (asprintf(&p, "%s%s%s%s%s",
2077 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2078 worktree_subdir, worktree_subdir[0] ? "/" : "",
2080 error = got_error_from_errno("asprintf");
2083 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2086 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2091 if (commit_id_str == NULL) {
2092 struct got_reference *head_ref;
2093 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2096 error = got_ref_resolve(&commit_id, repo, head_ref);
2097 got_ref_close(head_ref);
2101 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2106 error = got_blame(in_repo_path, commit_id, repo, stdout);
2113 got_worktree_close(worktree);
2115 const struct got_error *repo_error;
2116 repo_error = got_repo_close(repo);
2127 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2133 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2134 const char *root_path)
2136 int is_root_path = (strcmp(path, root_path) == 0);
2138 path += strlen(root_path);
2139 while (path[0] == '/')
2142 printf("%s%s%s%s%s\n", id ? id : "", path,
2143 is_root_path ? "" : "/", te->name,
2144 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2147 static const struct got_error *
2148 print_tree(const char *path, struct got_object_id *commit_id,
2149 int show_ids, int recurse, const char *root_path,
2150 struct got_repository *repo)
2152 const struct got_error *err = NULL;
2153 struct got_object_id *tree_id = NULL;
2154 struct got_tree_object *tree = NULL;
2155 const struct got_tree_entries *entries;
2156 struct got_tree_entry *te;
2158 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2162 err = got_object_open_as_tree(&tree, repo, tree_id);
2165 entries = got_object_tree_get_entries(tree);
2166 te = SIMPLEQ_FIRST(&entries->head);
2170 if (sigint_received || sigpipe_received)
2175 err = got_object_id_str(&id_str, te->id);
2178 if (asprintf(&id, "%s ", id_str) == -1) {
2179 err = got_error_from_errno("asprintf");
2185 print_entry(te, id, path, root_path);
2188 if (recurse && S_ISDIR(te->mode)) {
2190 if (asprintf(&child_path, "%s%s%s", path,
2191 path[0] == '/' && path[1] == '\0' ? "" : "/",
2193 err = got_error_from_errno("asprintf");
2196 err = print_tree(child_path, commit_id, show_ids, 1,
2203 te = SIMPLEQ_NEXT(te, entry);
2207 got_object_tree_close(tree);
2212 static const struct got_error *
2213 cmd_tree(int argc, char *argv[])
2215 const struct got_error *error;
2216 struct got_repository *repo = NULL;
2217 struct got_worktree *worktree = NULL;
2219 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2220 struct got_object_id *commit_id = NULL;
2221 char *commit_id_str = NULL;
2222 int show_ids = 0, recurse = 0;
2226 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2231 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2234 commit_id_str = optarg;
2237 repo_path = realpath(optarg, NULL);
2238 if (repo_path == NULL)
2239 err(1, "-r option");
2240 got_path_strip_trailing_slashes(repo_path);
2264 cwd = getcwd(NULL, 0);
2266 error = got_error_from_errno("getcwd");
2269 if (repo_path == NULL) {
2270 error = got_worktree_open(&worktree, cwd);
2271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2277 strdup(got_worktree_get_repo_path(worktree));
2278 if (repo_path == NULL)
2279 error = got_error_from_errno("strdup");
2283 repo_path = strdup(cwd);
2284 if (repo_path == NULL) {
2285 error = got_error_from_errno("strdup");
2291 error = got_repo_open(&repo, repo_path);
2295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2301 char *p, *worktree_subdir = cwd +
2302 strlen(got_worktree_get_root_path(worktree));
2303 if (asprintf(&p, "%s/%s",
2304 got_worktree_get_path_prefix(worktree),
2305 worktree_subdir) == -1) {
2306 error = got_error_from_errno("asprintf");
2309 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2316 if (in_repo_path == NULL) {
2317 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2322 if (commit_id_str == NULL) {
2323 struct got_reference *head_ref;
2324 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2327 error = got_ref_resolve(&commit_id, repo, head_ref);
2328 got_ref_close(head_ref);
2332 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2337 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2338 in_repo_path, repo);
2345 got_worktree_close(worktree);
2347 const struct got_error *repo_error;
2348 repo_error = got_repo_close(repo);
2358 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2362 static const struct got_error *
2363 print_status(void *arg, unsigned char status, const char *path,
2364 struct got_object_id *blob_id, struct got_object_id *commit_id)
2366 printf("%c %s\n", status, path);
2370 static const struct got_error *
2371 cmd_status(int argc, char *argv[])
2373 const struct got_error *error = NULL;
2374 struct got_repository *repo = NULL;
2375 struct got_worktree *worktree = NULL;
2377 struct got_pathlist_head paths;
2378 struct got_pathlist_entry *pe;
2383 while ((ch = getopt(argc, argv, "")) != -1) {
2395 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2399 cwd = getcwd(NULL, 0);
2401 error = got_error_from_errno("getcwd");
2405 error = got_worktree_open(&worktree, cwd);
2409 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2417 error = apply_unveil(got_repo_get_path(repo), 1,
2418 got_worktree_get_root_path(worktree));
2422 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2423 check_cancelled, NULL);
2425 TAILQ_FOREACH(pe, &paths, entry)
2426 free((char *)pe->path);
2427 got_pathlist_free(&paths);
2436 "usage: %s ref [-r repository] -l | -d name | name target\n",
2441 static const struct got_error *
2442 list_refs(struct got_repository *repo)
2444 static const struct got_error *err = NULL;
2445 struct got_reflist_head refs;
2446 struct got_reflist_entry *re;
2448 SIMPLEQ_INIT(&refs);
2449 err = got_ref_list(&refs, repo);
2453 SIMPLEQ_FOREACH(re, &refs, entry) {
2455 refstr = got_ref_to_str(re->ref);
2457 return got_error_from_errno("got_ref_to_str");
2458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2462 got_ref_list_free(&refs);
2466 static const struct got_error *
2467 delete_ref(struct got_repository *repo, const char *refname)
2469 const struct got_error *err = NULL;
2470 struct got_reference *ref;
2472 err = got_ref_open(&ref, repo, refname, 0);
2476 err = got_ref_delete(ref, repo);
2481 static const struct got_error *
2482 add_ref(struct got_repository *repo, const char *refname, const char *target)
2484 const struct got_error *err = NULL;
2485 struct got_object_id *id;
2486 struct got_reference *ref = NULL;
2489 * Don't let the user create a reference named '-'.
2490 * While technically a valid reference name, this case is usually
2491 * an unintended typo.
2493 if (refname[0] == '-' && refname[1] == '\0')
2494 return got_error(GOT_ERR_BAD_REF_NAME);
2496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2499 struct got_reference *target_ref;
2501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2503 err = got_ref_open(&target_ref, repo, target, 0);
2506 err = got_ref_resolve(&id, repo, target_ref);
2507 got_ref_close(target_ref);
2512 err = got_ref_alloc(&ref, refname, id);
2516 err = got_ref_write(ref, repo);
2524 static const struct got_error *
2525 cmd_ref(int argc, char *argv[])
2527 const struct got_error *error = NULL;
2528 struct got_repository *repo = NULL;
2529 struct got_worktree *worktree = NULL;
2530 char *cwd = NULL, *repo_path = NULL;
2531 int ch, do_list = 0;
2532 const char *delref = NULL;
2534 /* TODO: Add -s option for adding symbolic references. */
2535 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2541 repo_path = realpath(optarg, NULL);
2542 if (repo_path == NULL)
2543 err(1, "-r option");
2544 got_path_strip_trailing_slashes(repo_path);
2555 if (do_list && delref)
2556 errx(1, "-l and -d options are mutually exclusive\n");
2561 if (do_list || delref) {
2564 } else if (argc != 2)
2569 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2573 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2574 "sendfd unveil", NULL) == -1)
2578 cwd = getcwd(NULL, 0);
2580 error = got_error_from_errno("getcwd");
2584 if (repo_path == NULL) {
2585 error = got_worktree_open(&worktree, cwd);
2586 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2592 strdup(got_worktree_get_repo_path(worktree));
2593 if (repo_path == NULL)
2594 error = got_error_from_errno("strdup");
2598 repo_path = strdup(cwd);
2599 if (repo_path == NULL) {
2600 error = got_error_from_errno("strdup");
2606 error = got_repo_open(&repo, repo_path);
2610 error = apply_unveil(got_repo_get_path(repo), do_list,
2611 worktree ? got_worktree_get_root_path(worktree) : NULL);
2616 error = list_refs(repo);
2618 error = delete_ref(repo, delref);
2620 error = add_ref(repo, argv[0], argv[1]);
2623 got_repo_close(repo);
2625 got_worktree_close(worktree);
2635 "usage: %s branch [-r repository] -l | -d name | "
2636 "name [base-branch]\n", getprogname());
2640 static const struct got_error *
2641 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2643 static const struct got_error *err = NULL;
2644 struct got_reflist_head refs;
2645 struct got_reflist_entry *re;
2647 SIMPLEQ_INIT(&refs);
2649 err = got_ref_list(&refs, repo);
2653 SIMPLEQ_FOREACH(re, &refs, entry) {
2654 const char *refname, *marker = " ";
2656 refname = got_ref_get_name(re->ref);
2657 if (strncmp(refname, "refs/heads/", 11) != 0)
2659 if (worktree && strcmp(refname,
2660 got_worktree_get_head_ref_name(worktree)) == 0) {
2661 struct got_object_id *id = NULL;
2662 err = got_ref_resolve(&id, repo, re->ref);
2665 if (got_object_id_cmp(id,
2666 got_worktree_get_base_commit_id(worktree)) == 0)
2673 refstr = got_ref_to_str(re->ref);
2675 return got_error_from_errno("got_ref_to_str");
2676 printf("%s%s: %s\n", marker, refname, refstr);
2680 got_ref_list_free(&refs);
2684 static const struct got_error *
2685 delete_branch(struct got_repository *repo, const char *branch_name)
2687 const struct got_error *err = NULL;
2688 struct got_reference *ref;
2691 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2692 return got_error_from_errno("asprintf");
2694 err = got_ref_open(&ref, repo, refname, 0);
2698 err = got_ref_delete(ref, repo);
2705 static const struct got_error *
2706 add_branch(struct got_repository *repo, const char *branch_name,
2707 const char *base_branch)
2709 const struct got_error *err = NULL;
2710 struct got_object_id *id = NULL;
2711 struct got_reference *ref = NULL;
2712 char *base_refname = NULL, *refname = NULL;
2713 struct got_reference *base_ref;
2716 * Don't let the user create a branch named '-'.
2717 * While technically a valid reference name, this case is usually
2718 * an unintended typo.
2720 if (branch_name[0] == '-' && branch_name[1] == '\0')
2721 return got_error(GOT_ERR_BAD_REF_NAME);
2723 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2724 base_refname = strdup(GOT_REF_HEAD);
2725 if (base_refname == NULL)
2726 return got_error_from_errno("strdup");
2727 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2728 return got_error_from_errno("asprintf");
2730 err = got_ref_open(&base_ref, repo, base_refname, 0);
2733 err = got_ref_resolve(&id, repo, base_ref);
2734 got_ref_close(base_ref);
2738 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2739 err = got_error_from_errno("asprintf");
2743 err = got_ref_open(&ref, repo, refname, 0);
2745 err = got_error(GOT_ERR_BRANCH_EXISTS);
2747 } else if (err->code != GOT_ERR_NOT_REF)
2750 err = got_ref_alloc(&ref, refname, id);
2754 err = got_ref_write(ref, repo);
2764 static const struct got_error *
2765 cmd_branch(int argc, char *argv[])
2767 const struct got_error *error = NULL;
2768 struct got_repository *repo = NULL;
2769 struct got_worktree *worktree = NULL;
2770 char *cwd = NULL, *repo_path = NULL;
2771 int ch, do_list = 0;
2772 const char *delref = NULL;
2774 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2780 repo_path = realpath(optarg, NULL);
2781 if (repo_path == NULL)
2782 err(1, "-r option");
2783 got_path_strip_trailing_slashes(repo_path);
2794 if (do_list && delref)
2795 errx(1, "-l and -d options are mutually exclusive\n");
2800 if (do_list || delref) {
2803 } else if (argc < 1 || argc > 2)
2808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2813 "sendfd unveil", NULL) == -1)
2817 cwd = getcwd(NULL, 0);
2819 error = got_error_from_errno("getcwd");
2823 if (repo_path == NULL) {
2824 error = got_worktree_open(&worktree, cwd);
2825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2831 strdup(got_worktree_get_repo_path(worktree));
2832 if (repo_path == NULL)
2833 error = got_error_from_errno("strdup");
2837 repo_path = strdup(cwd);
2838 if (repo_path == NULL) {
2839 error = got_error_from_errno("strdup");
2845 error = got_repo_open(&repo, repo_path);
2849 error = apply_unveil(got_repo_get_path(repo), do_list,
2850 worktree ? got_worktree_get_root_path(worktree) : NULL);
2855 error = list_branches(repo, worktree);
2857 error = delete_branch(repo, delref);
2859 const char *base_branch;
2861 base_branch = worktree ?
2862 got_worktree_get_head_ref_name(worktree) :
2864 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2867 base_branch = argv[1];
2868 error = add_branch(repo, argv[0], base_branch);
2872 got_repo_close(repo);
2874 got_worktree_close(worktree);
2883 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2887 static const struct got_error *
2888 cmd_add(int argc, char *argv[])
2890 const struct got_error *error = NULL;
2891 struct got_repository *repo = NULL;
2892 struct got_worktree *worktree = NULL;
2894 struct got_pathlist_head paths;
2895 struct got_pathlist_entry *pe;
2900 while ((ch = getopt(argc, argv, "")) != -1) {
2912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2919 cwd = getcwd(NULL, 0);
2921 error = got_error_from_errno("getcwd");
2925 error = got_worktree_open(&worktree, cwd);
2929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2933 error = apply_unveil(got_repo_get_path(repo), 1,
2934 got_worktree_get_root_path(worktree));
2938 for (x = 0; x < argc; x++) {
2939 char *path = realpath(argv[x], NULL);
2941 error = got_error_from_errno2("realpath", argv[x]);
2945 got_path_strip_trailing_slashes(path);
2946 error = got_pathlist_insert(&pe, &paths, path, NULL);
2952 error = got_worktree_schedule_add(worktree, &paths, print_status,
2956 got_repo_close(repo);
2958 got_worktree_close(worktree);
2959 TAILQ_FOREACH(pe, &paths, entry)
2960 free((char *)pe->path);
2961 got_pathlist_free(&paths);
2969 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2973 static const struct got_error *
2974 cmd_remove(int argc, char *argv[])
2976 const struct got_error *error = NULL;
2977 struct got_worktree *worktree = NULL;
2978 struct got_repository *repo = NULL;
2980 struct got_pathlist_head paths;
2981 struct got_pathlist_entry *pe;
2982 int ch, i, delete_local_mods = 0;
2986 while ((ch = getopt(argc, argv, "f")) != -1) {
2989 delete_local_mods = 1;
3001 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3008 cwd = getcwd(NULL, 0);
3010 error = got_error_from_errno("getcwd");
3013 error = got_worktree_open(&worktree, cwd);
3017 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3021 error = apply_unveil(got_repo_get_path(repo), 1,
3022 got_worktree_get_root_path(worktree));
3026 for (i = 0; i < argc; i++) {
3027 char *path = realpath(argv[i], NULL);
3029 error = got_error_from_errno2("realpath", argv[i]);
3033 got_path_strip_trailing_slashes(path);
3034 error = got_pathlist_insert(&pe, &paths, path, NULL);
3040 error = got_worktree_schedule_delete(worktree, &paths,
3041 delete_local_mods, print_status, NULL, repo);
3046 got_repo_close(repo);
3048 got_worktree_close(worktree);
3049 TAILQ_FOREACH(pe, &paths, entry)
3050 free((char *)pe->path);
3051 got_pathlist_free(&paths);
3059 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3063 static const struct got_error *
3064 revert_progress(void *arg, unsigned char status, const char *path)
3066 while (path[0] == '/')
3068 printf("%c %s\n", status, path);
3072 static const struct got_error *
3073 cmd_revert(int argc, char *argv[])
3075 const struct got_error *error = NULL;
3076 struct got_worktree *worktree = NULL;
3077 struct got_repository *repo = NULL;
3078 char *cwd = NULL, *path = NULL;
3079 struct got_pathlist_head paths;
3080 struct got_pathlist_entry *pe;
3081 const char *worktree_path;
3086 while ((ch = getopt(argc, argv, "")) != -1) {
3098 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3099 "unveil", NULL) == -1)
3105 cwd = getcwd(NULL, 0);
3107 error = got_error_from_errno("getcwd");
3110 error = got_worktree_open(&worktree, cwd);
3114 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3118 error = apply_unveil(got_repo_get_path(repo), 1,
3119 got_worktree_get_root_path(worktree));
3123 worktree_path = got_worktree_get_root_path(worktree);
3124 for (i = 0; i < argc; i++) {
3125 char *path = realpath(argv[i], NULL);
3127 if (errno != ENOENT) {
3128 error = got_error_from_errno2("realpath",
3132 if (got_path_is_child(argv[i], worktree_path,
3133 strlen(worktree_path))) {
3134 path = strdup(argv[i]);
3136 error = got_error_from_errno("strdup");
3140 } else if (asprintf(&path, "%s/%s",
3141 got_worktree_get_root_path(worktree),
3143 error = got_error_from_errno("asprintf");
3148 got_path_strip_trailing_slashes(path);
3149 error = got_pathlist_insert(&pe, &paths, path, NULL);
3156 error = got_worktree_revert(worktree, &paths,
3157 revert_progress, NULL, repo);
3162 got_repo_close(repo);
3164 got_worktree_close(worktree);
3173 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3178 struct collect_commit_logmsg_arg {
3179 const char *cmdline_log;
3181 const char *worktree_path;
3182 const char *branch_name;
3183 const char *repo_path;
3188 static const struct got_error *
3189 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3192 char *initial_content = NULL;
3193 struct got_pathlist_entry *pe;
3194 const struct got_error *err = NULL;
3195 char *template = NULL;
3196 struct collect_commit_logmsg_arg *a = arg;
3200 /* if a message was specified on the command line, just use it */
3201 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3202 len = strlen(a->cmdline_log) + 1;
3203 *logmsg = malloc(len + 1);
3204 if (*logmsg == NULL)
3205 return got_error_from_errno("malloc");
3206 strlcpy(*logmsg, a->cmdline_log, len);
3210 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3211 return got_error_from_errno("asprintf");
3213 if (asprintf(&initial_content,
3214 "\n# changes to be committed on branch %s:\n",
3215 a->branch_name) == -1)
3216 return got_error_from_errno("asprintf");
3218 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3222 dprintf(fd, initial_content);
3224 TAILQ_FOREACH(pe, commitable_paths, entry) {
3225 struct got_commitable *ct = pe->data;
3226 dprintf(fd, "# %c %s\n",
3227 got_commitable_get_status(ct),
3228 got_commitable_get_path(ct));
3232 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3234 unlink(a->logmsg_path);
3235 free(a->logmsg_path);
3236 free(initial_content);
3239 /* Editor is done; we can now apply unveil(2) */
3241 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3250 static const struct got_error *
3251 cmd_commit(int argc, char *argv[])
3253 const struct got_error *error = NULL;
3254 struct got_worktree *worktree = NULL;
3255 struct got_repository *repo = NULL;
3256 char *cwd = NULL, *id_str = NULL;
3257 struct got_object_id *id = NULL;
3258 const char *logmsg = NULL;
3259 const char *got_author = getenv("GOT_AUTHOR");
3260 struct collect_commit_logmsg_arg cl_arg;
3261 char *editor = NULL;
3262 int ch, rebase_in_progress, histedit_in_progress;
3263 struct got_pathlist_head paths;
3267 while ((ch = getopt(argc, argv, "m:")) != -1) {
3282 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3283 "unveil", NULL) == -1)
3286 if (got_author == NULL) {
3287 /* TODO: Look current user up in password database */
3288 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3292 cwd = getcwd(NULL, 0);
3294 error = got_error_from_errno("getcwd");
3297 error = got_worktree_open(&worktree, cwd);
3301 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3304 if (rebase_in_progress) {
3305 error = got_error(GOT_ERR_REBASING);
3309 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3314 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3323 * unveil(2) traverses exec(2); if an editor is used we have
3324 * to apply unveil after the log message has been written.
3326 if (logmsg == NULL || strlen(logmsg) == 0)
3327 error = get_editor(&editor);
3329 error = apply_unveil(got_repo_get_path(repo), 0,
3330 got_worktree_get_root_path(worktree));
3334 cl_arg.editor = editor;
3335 cl_arg.cmdline_log = logmsg;
3336 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3337 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3338 if (!histedit_in_progress) {
3339 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3340 error = got_error(GOT_ERR_COMMIT_BRANCH);
3343 cl_arg.branch_name += 11;
3345 cl_arg.repo_path = got_repo_get_path(repo);
3346 cl_arg.logmsg_path = NULL;
3347 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3348 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3350 if (cl_arg.logmsg_path)
3351 fprintf(stderr, "%s: log message preserved in %s\n",
3352 getprogname(), cl_arg.logmsg_path);
3356 if (cl_arg.logmsg_path)
3357 unlink(cl_arg.logmsg_path);
3359 error = got_object_id_str(&id_str, id);
3362 printf("Created commit %s\n", id_str);
3365 got_repo_close(repo);
3367 got_worktree_close(worktree);
3375 usage_cherrypick(void)
3377 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3381 static const struct got_error *
3382 cmd_cherrypick(int argc, char *argv[])
3384 const struct got_error *error = NULL;
3385 struct got_worktree *worktree = NULL;
3386 struct got_repository *repo = NULL;
3387 char *cwd = NULL, *commit_id_str = NULL;
3388 struct got_object_id *commit_id = NULL;
3389 struct got_commit_object *commit = NULL;
3390 struct got_object_qid *pid;
3391 struct got_reference *head_ref = NULL;
3392 int ch, did_something = 0;
3394 while ((ch = getopt(argc, argv, "")) != -1) {
3406 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3407 "unveil", NULL) == -1)
3413 cwd = getcwd(NULL, 0);
3415 error = got_error_from_errno("getcwd");
3418 error = got_worktree_open(&worktree, cwd);
3422 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3426 error = apply_unveil(got_repo_get_path(repo), 0,
3427 got_worktree_get_root_path(worktree));
3431 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3432 GOT_OBJ_TYPE_COMMIT, repo);
3433 if (error != NULL) {
3434 struct got_reference *ref;
3435 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3437 error = got_ref_open(&ref, repo, argv[0], 0);
3440 error = got_ref_resolve(&commit_id, repo, ref);
3445 error = got_object_id_str(&commit_id_str, commit_id);
3449 error = got_ref_open(&head_ref, repo,
3450 got_worktree_get_head_ref_name(worktree), 0);
3454 error = check_same_branch(commit_id, head_ref, NULL, repo);
3456 if (error->code != GOT_ERR_ANCESTRY)
3460 error = got_error(GOT_ERR_SAME_BRANCH);
3464 error = got_object_open_as_commit(&commit, repo, commit_id);
3467 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3468 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3469 commit_id, repo, update_progress, &did_something, check_cancelled,
3475 printf("Merged commit %s\n", commit_id_str);
3478 got_object_commit_close(commit);
3479 free(commit_id_str);
3481 got_ref_close(head_ref);
3483 got_worktree_close(worktree);
3485 got_repo_close(repo);
3492 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3496 static const struct got_error *
3497 cmd_backout(int argc, char *argv[])
3499 const struct got_error *error = NULL;
3500 struct got_worktree *worktree = NULL;
3501 struct got_repository *repo = NULL;
3502 char *cwd = NULL, *commit_id_str = NULL;
3503 struct got_object_id *commit_id = NULL;
3504 struct got_commit_object *commit = NULL;
3505 struct got_object_qid *pid;
3506 struct got_reference *head_ref = NULL;
3507 int ch, did_something = 0;
3509 while ((ch = getopt(argc, argv, "")) != -1) {
3521 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3522 "unveil", NULL) == -1)
3528 cwd = getcwd(NULL, 0);
3530 error = got_error_from_errno("getcwd");
3533 error = got_worktree_open(&worktree, cwd);
3537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3541 error = apply_unveil(got_repo_get_path(repo), 0,
3542 got_worktree_get_root_path(worktree));
3546 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3547 GOT_OBJ_TYPE_COMMIT, repo);
3548 if (error != NULL) {
3549 struct got_reference *ref;
3550 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3552 error = got_ref_open(&ref, repo, argv[0], 0);
3555 error = got_ref_resolve(&commit_id, repo, ref);
3560 error = got_object_id_str(&commit_id_str, commit_id);
3564 error = got_ref_open(&head_ref, repo,
3565 got_worktree_get_head_ref_name(worktree), 0);
3569 error = check_same_branch(commit_id, head_ref, NULL, repo);
3573 error = got_object_open_as_commit(&commit, repo, commit_id);
3576 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3578 error = got_error(GOT_ERR_ROOT_COMMIT);
3582 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3583 update_progress, &did_something, check_cancelled, NULL);
3588 printf("Backed out commit %s\n", commit_id_str);
3591 got_object_commit_close(commit);
3592 free(commit_id_str);
3594 got_ref_close(head_ref);
3596 got_worktree_close(worktree);
3598 got_repo_close(repo);
3605 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3611 trim_logmsg(char *logmsg, int limit)
3616 len = strlen(logmsg);
3620 nl = strchr(logmsg, '\n');
3625 static const struct got_error *
3626 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3628 const char *logmsg0 = NULL;
3630 logmsg0 = got_object_commit_get_logmsg(commit);
3632 while (isspace((unsigned char)logmsg0[0]))
3635 *logmsg = strdup(logmsg0);
3636 if (*logmsg == NULL)
3637 return got_error_from_errno("strdup");
3639 trim_logmsg(*logmsg, limit);
3643 static const struct got_error *
3644 show_rebase_progress(struct got_commit_object *commit,
3645 struct got_object_id *old_id, struct got_object_id *new_id)
3647 const struct got_error *err;
3648 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3650 err = got_object_id_str(&old_id_str, old_id);
3655 err = got_object_id_str(&new_id_str, new_id);
3660 old_id_str[12] = '\0';
3662 new_id_str[12] = '\0';
3664 err = get_short_logmsg(&logmsg, 42, commit);
3668 printf("%s -> %s: %s\n", old_id_str,
3669 new_id_str ? new_id_str : "no-op change", logmsg);
3676 static const struct got_error *
3677 rebase_progress(void *arg, unsigned char status, const char *path)
3679 unsigned char *rebase_status = arg;
3681 while (path[0] == '/')
3683 printf("%c %s\n", status, path);
3685 if (*rebase_status == GOT_STATUS_CONFLICT)
3687 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3688 *rebase_status = status;
3692 static const struct got_error *
3693 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3694 struct got_reference *branch, struct got_reference *new_base_branch,
3695 struct got_reference *tmp_branch, struct got_repository *repo)
3697 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3698 return got_worktree_rebase_complete(worktree, fileindex,
3699 new_base_branch, tmp_branch, branch, repo);
3702 static const struct got_error *
3703 rebase_commit(struct got_pathlist_head *merged_paths,
3704 struct got_worktree *worktree, struct got_fileindex *fileindex,
3705 struct got_reference *tmp_branch,
3706 struct got_object_id *commit_id, struct got_repository *repo)
3708 const struct got_error *error;
3709 struct got_commit_object *commit;
3710 struct got_object_id *new_commit_id;
3712 error = got_object_open_as_commit(&commit, repo, commit_id);
3716 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3717 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3719 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3721 error = show_rebase_progress(commit, commit_id, NULL);
3723 error = show_rebase_progress(commit, commit_id, new_commit_id);
3724 free(new_commit_id);
3727 got_object_commit_close(commit);
3731 struct check_path_prefix_arg {
3732 const char *path_prefix;
3737 static const struct got_error *
3738 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3739 struct got_blob_object *blob2, struct got_object_id *id1,
3740 struct got_object_id *id2, const char *path1, const char *path2,
3741 struct got_repository *repo)
3743 struct check_path_prefix_arg *a = arg;
3745 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3746 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3747 return got_error(a->errcode);
3752 static const struct got_error *
3753 check_path_prefix(struct got_object_id *parent_id,
3754 struct got_object_id *commit_id, const char *path_prefix,
3755 int errcode, struct got_repository *repo)
3757 const struct got_error *err;
3758 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3759 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3760 struct check_path_prefix_arg cpp_arg;
3762 if (got_path_is_root_dir(path_prefix))
3765 err = got_object_open_as_commit(&commit, repo, commit_id);
3769 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3773 err = got_object_open_as_tree(&tree1, repo,
3774 got_object_commit_get_tree_id(parent_commit));
3778 err = got_object_open_as_tree(&tree2, repo,
3779 got_object_commit_get_tree_id(commit));
3783 cpp_arg.path_prefix = path_prefix;
3784 while (cpp_arg.path_prefix[0] == '/')
3785 cpp_arg.path_prefix++;
3786 cpp_arg.len = strlen(cpp_arg.path_prefix);
3787 cpp_arg.errcode = errcode;
3788 err = got_diff_tree(tree1, tree2, "", "", repo,
3789 check_path_prefix_in_diff, &cpp_arg, 0);
3792 got_object_tree_close(tree1);
3794 got_object_tree_close(tree2);
3796 got_object_commit_close(commit);
3798 got_object_commit_close(parent_commit);
3802 static const struct got_error *
3803 collect_commits(struct got_object_id_queue *commits,
3804 struct got_object_id *initial_commit_id,
3805 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3806 const char *path_prefix, int path_prefix_errcode,
3807 struct got_repository *repo)
3809 const struct got_error *err = NULL;
3810 struct got_commit_graph *graph = NULL;
3811 struct got_object_id *parent_id = NULL;
3812 struct got_object_qid *qid;
3813 struct got_object_id *commit_id = initial_commit_id;
3815 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3819 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3822 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3823 err = got_commit_graph_iter_next(&parent_id, graph);
3825 if (err->code == GOT_ERR_ITER_COMPLETED) {
3826 err = got_error_msg(GOT_ERR_ANCESTRY,
3827 "ran out of commits to rebase before "
3828 "youngest common ancestor commit has "
3831 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3833 err = got_commit_graph_fetch_commits(graph, 1, repo);
3837 err = check_path_prefix(parent_id, commit_id,
3838 path_prefix, path_prefix_errcode, repo);
3842 err = got_object_qid_alloc(&qid, commit_id);
3845 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3846 commit_id = parent_id;
3850 got_commit_graph_close(graph);
3854 static const struct got_error *
3855 cmd_rebase(int argc, char *argv[])
3857 const struct got_error *error = NULL;
3858 struct got_worktree *worktree = NULL;
3859 struct got_repository *repo = NULL;
3860 struct got_fileindex *fileindex = NULL;
3862 struct got_reference *branch = NULL;
3863 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3864 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3865 struct got_object_id *resume_commit_id = NULL;
3866 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3867 struct got_commit_object *commit = NULL;
3868 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3869 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3870 struct got_object_id_queue commits;
3871 struct got_pathlist_head merged_paths;
3872 const struct got_object_id_queue *parent_ids;
3873 struct got_object_qid *qid, *pid;
3875 SIMPLEQ_INIT(&commits);
3876 TAILQ_INIT(&merged_paths);
3878 while ((ch = getopt(argc, argv, "ac")) != -1) {
3884 continue_rebase = 1;
3896 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3897 "unveil", NULL) == -1)
3900 if (abort_rebase && continue_rebase)
3902 else if (abort_rebase || continue_rebase) {
3905 } else if (argc != 1)
3908 cwd = getcwd(NULL, 0);
3910 error = got_error_from_errno("getcwd");
3913 error = got_worktree_open(&worktree, cwd);
3917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3921 error = apply_unveil(got_repo_get_path(repo), 0,
3922 got_worktree_get_root_path(worktree));
3926 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3932 if (!rebase_in_progress) {
3933 error = got_error(GOT_ERR_NOT_REBASING);
3936 error = got_worktree_rebase_continue(&resume_commit_id,
3937 &new_base_branch, &tmp_branch, &branch, &fileindex,
3941 printf("Switching work tree to %s\n",
3942 got_ref_get_symref_target(new_base_branch));
3943 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3944 new_base_branch, update_progress, &did_something);
3947 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3948 goto done; /* nothing else to do */
3951 if (continue_rebase) {
3952 if (!rebase_in_progress) {
3953 error = got_error(GOT_ERR_NOT_REBASING);
3956 error = got_worktree_rebase_continue(&resume_commit_id,
3957 &new_base_branch, &tmp_branch, &branch, &fileindex,
3962 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3963 resume_commit_id, repo);
3967 yca_id = got_object_id_dup(resume_commit_id);
3968 if (yca_id == NULL) {
3969 error = got_error_from_errno("got_object_id_dup");
3973 error = got_ref_open(&branch, repo, argv[0], 0);
3978 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3982 if (!continue_rebase) {
3983 struct got_object_id *base_commit_id;
3985 base_commit_id = got_worktree_get_base_commit_id(worktree);
3986 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3987 base_commit_id, branch_head_commit_id, repo);
3990 if (yca_id == NULL) {
3991 error = got_error_msg(GOT_ERR_ANCESTRY,
3992 "specified branch shares no common ancestry "
3993 "with work tree's branch");
3997 error = check_same_branch(base_commit_id, branch, yca_id, repo);
3999 if (error->code != GOT_ERR_ANCESTRY)
4003 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4004 "specified branch resolves to a commit which "
4005 "is already contained in work tree's branch");
4008 error = got_worktree_rebase_prepare(&new_base_branch,
4009 &tmp_branch, &fileindex, worktree, branch, repo);
4014 commit_id = branch_head_commit_id;
4015 error = got_object_open_as_commit(&commit, repo, commit_id);
4019 parent_ids = got_object_commit_get_parent_ids(commit);
4020 pid = SIMPLEQ_FIRST(parent_ids);
4021 error = collect_commits(&commits, commit_id, pid->id,
4022 yca_id, got_worktree_get_path_prefix(worktree),
4023 GOT_ERR_REBASE_PATH, repo);
4024 got_object_commit_close(commit);
4029 if (SIMPLEQ_EMPTY(&commits)) {
4030 if (continue_rebase)
4031 error = rebase_complete(worktree, fileindex,
4032 branch, new_base_branch, tmp_branch, repo);
4034 error = got_error(GOT_ERR_EMPTY_REBASE);
4039 SIMPLEQ_FOREACH(qid, &commits, entry) {
4040 commit_id = qid->id;
4041 parent_id = pid ? pid->id : yca_id;
4044 error = got_worktree_rebase_merge_files(&merged_paths,
4045 worktree, fileindex, parent_id, commit_id, repo,
4046 rebase_progress, &rebase_status, check_cancelled, NULL);
4050 if (rebase_status == GOT_STATUS_CONFLICT) {
4051 got_worktree_rebase_pathlist_free(&merged_paths);
4055 error = rebase_commit(&merged_paths, worktree, fileindex,
4056 tmp_branch, commit_id, repo);
4057 got_worktree_rebase_pathlist_free(&merged_paths);
4062 if (rebase_status == GOT_STATUS_CONFLICT) {
4063 error = got_worktree_rebase_postpone(worktree, fileindex);
4066 error = got_error_msg(GOT_ERR_CONFLICTS,
4067 "conflicts must be resolved before rebasing can continue");
4069 error = rebase_complete(worktree, fileindex, branch,
4070 new_base_branch, tmp_branch, repo);
4072 got_object_id_queue_free(&commits);
4073 free(branch_head_commit_id);
4074 free(resume_commit_id);
4077 got_object_commit_close(commit);
4079 got_ref_close(branch);
4080 if (new_base_branch)
4081 got_ref_close(new_base_branch);
4083 got_ref_close(tmp_branch);
4085 got_worktree_close(worktree);
4087 got_repo_close(repo);
4092 usage_histedit(void)
4094 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4099 #define GOT_HISTEDIT_PICK 'p'
4100 #define GOT_HISTEDIT_EDIT 'e'
4101 #define GOT_HISTEDIT_FOLD 'f'
4102 #define GOT_HISTEDIT_DROP 'd'
4103 #define GOT_HISTEDIT_MESG 'm'
4105 static struct got_histedit_cmd {
4109 } got_histedit_cmds[] = {
4110 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4111 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4112 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4113 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4114 { GOT_HISTEDIT_MESG, "mesg",
4115 "single-line log message for commit above (open editor if empty)" },
4118 struct got_histedit_list_entry {
4119 TAILQ_ENTRY(got_histedit_list_entry) entry;
4120 struct got_object_id *commit_id;
4121 const struct got_histedit_cmd *cmd;
4124 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4126 static const struct got_error *
4127 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4128 FILE *f, struct got_repository *repo)
4130 const struct got_error *err = NULL;
4131 char *logmsg = NULL, *id_str = NULL;
4132 struct got_commit_object *commit = NULL;
4135 err = got_object_open_as_commit(&commit, repo, commit_id);
4139 err = get_short_logmsg(&logmsg, 34, commit);
4143 err = got_object_id_str(&id_str, commit_id);
4147 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4149 err = got_ferror(f, GOT_ERR_IO);
4152 got_object_commit_close(commit);
4158 static const struct got_error *
4159 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4160 struct got_repository *repo)
4162 const struct got_error *err = NULL;
4163 struct got_object_qid *qid;
4165 if (SIMPLEQ_EMPTY(commits))
4166 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4168 SIMPLEQ_FOREACH(qid, commits, entry) {
4169 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4178 static const struct got_error *
4179 write_cmd_list(FILE *f)
4181 const struct got_error *err = NULL;
4184 n = fprintf(f, "# Available histedit commands:\n");
4186 return got_ferror(f, GOT_ERR_IO);
4188 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4189 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4190 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4193 err = got_ferror(f, GOT_ERR_IO);
4197 n = fprintf(f, "# Commits will be processed in order from top to "
4198 "bottom of this file.\n");
4200 return got_ferror(f, GOT_ERR_IO);
4204 static const struct got_error *
4205 histedit_syntax_error(int lineno)
4207 static char msg[42];
4210 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4212 if (ret == -1 || ret >= sizeof(msg))
4213 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4215 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4218 static const struct got_error *
4219 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4220 char *logmsg, struct got_repository *repo)
4222 const struct got_error *err;
4223 struct got_commit_object *folded_commit = NULL;
4226 err = got_object_id_str(&id_str, hle->commit_id);
4230 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4234 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4235 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4236 got_object_commit_get_logmsg(folded_commit)) == -1) {
4237 err = got_error_from_errno("asprintf");
4242 got_object_commit_close(folded_commit);
4247 static struct got_histedit_list_entry *
4248 get_folded_commits(struct got_histedit_list_entry *hle)
4250 struct got_histedit_list_entry *prev, *folded = NULL;
4252 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4253 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4254 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4255 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4257 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4263 static const struct got_error *
4264 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4265 struct got_repository *repo)
4267 char *logmsg_path = NULL, *id_str = NULL;
4268 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4269 const struct got_error *err = NULL;
4270 struct got_commit_object *commit = NULL;
4272 struct got_histedit_list_entry *folded = NULL;
4274 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4278 folded = get_folded_commits(hle);
4280 while (folded != hle) {
4281 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4282 folded = TAILQ_NEXT(folded, entry);
4285 err = append_folded_commit_msg(&new_msg, folded,
4291 folded = TAILQ_NEXT(folded, entry);
4295 err = got_object_id_str(&id_str, hle->commit_id);
4298 if (asprintf(&new_msg,
4299 "%s\n# original log message of commit %s: %s",
4300 logmsg ? logmsg : "", id_str,
4301 got_object_commit_get_logmsg(commit)) == -1) {
4302 err = got_error_from_errno("asprintf");
4308 err = got_object_id_str(&id_str, hle->commit_id);
4312 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4316 dprintf(fd, logmsg);
4319 err = get_editor(&editor);
4323 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4325 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4328 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4329 if (hle->logmsg == NULL)
4330 err = got_error_from_errno("strdup");
4333 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4334 err = got_error_from_errno2("unlink", logmsg_path);
4339 got_object_commit_close(commit);
4343 static const struct got_error *
4344 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4345 FILE *f, struct got_repository *repo)
4347 const struct got_error *err = NULL;
4348 char *line = NULL, *p, *end;
4352 const struct got_histedit_cmd *cmd;
4353 struct got_object_id *commit_id = NULL;
4354 struct got_histedit_list_entry *hle = NULL;
4357 len = getline(&line, &size, f);
4359 const struct got_error *getline_err;
4362 getline_err = got_error_from_errno("getline");
4363 err = got_ferror(f, getline_err->code);
4368 while (isspace((unsigned char)p[0]))
4370 if (p[0] == '#' || p[0] == '\0') {
4376 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4377 cmd = &got_histedit_cmds[i];
4378 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4379 isspace((unsigned char)p[strlen(cmd->name)])) {
4380 p += strlen(cmd->name);
4383 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4388 if (i == nitems(got_histedit_cmds)) {
4389 err = histedit_syntax_error(lineno);
4392 while (isspace((unsigned char)p[0]))
4394 if (cmd->code == GOT_HISTEDIT_MESG) {
4395 if (hle == NULL || hle->logmsg != NULL) {
4396 err = got_error(GOT_ERR_HISTEDIT_CMD);
4400 err = histedit_edit_logmsg(hle, repo);
4404 hle->logmsg = strdup(p);
4405 if (hle->logmsg == NULL) {
4406 err = got_error_from_errno("strdup");
4415 while (end[0] && !isspace((unsigned char)end[0]))
4419 err = got_object_resolve_id_str(&commit_id, repo, p);
4421 /* override error code */
4422 err = histedit_syntax_error(lineno);
4426 hle = malloc(sizeof(*hle));
4428 err = got_error_from_errno("malloc");
4432 hle->commit_id = commit_id;
4437 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4445 static const struct got_error *
4446 histedit_check_script(struct got_histedit_list *histedit_cmds,
4447 struct got_object_id_queue *commits, struct got_repository *repo)
4449 const struct got_error *err = NULL;
4450 struct got_object_qid *qid;
4451 struct got_histedit_list_entry *hle;
4452 static char msg[80];
4455 if (TAILQ_EMPTY(histedit_cmds))
4456 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4457 "histedit script contains no commands");
4459 SIMPLEQ_FOREACH(qid, commits, entry) {
4460 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4461 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4465 err = got_object_id_str(&id_str, qid->id);
4468 snprintf(msg, sizeof(msg),
4469 "commit %s missing from histedit script", id_str);
4471 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4475 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4476 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4477 "last commit in histedit script cannot be folded");
4482 static const struct got_error *
4483 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4484 const char *path, struct got_object_id_queue *commits,
4485 struct got_repository *repo)
4487 const struct got_error *err = NULL;
4491 err = get_editor(&editor);
4495 if (spawn_editor(editor, path) == -1) {
4496 err = got_error_from_errno("failed spawning editor");
4500 f = fopen(path, "r");
4502 err = got_error_from_errno("fopen");
4505 err = histedit_parse_list(histedit_cmds, f, repo);
4509 err = histedit_check_script(histedit_cmds, commits, repo);
4511 if (f && fclose(f) != 0 && err == NULL)
4512 err = got_error_from_errno("fclose");
4517 static const struct got_error *
4518 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4519 struct got_object_id_queue *, const char *, struct got_repository *);
4521 static const struct got_error *
4522 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4523 struct got_object_id_queue *commits, struct got_repository *repo)
4525 const struct got_error *err;
4529 err = got_opentemp_named(&path, &f, "got-histedit");
4533 err = write_cmd_list(f);
4537 err = histedit_write_commit_list(commits, f, repo);
4541 if (fclose(f) != 0) {
4542 err = got_error_from_errno("fclose");
4547 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4549 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4550 err->code != GOT_ERR_HISTEDIT_CMD)
4552 err = histedit_edit_list_retry(histedit_cmds, err,
4553 commits, path, repo);
4556 if (f && fclose(f) != 0 && err == NULL)
4557 err = got_error_from_errno("fclose");
4558 if (path && unlink(path) != 0 && err == NULL)
4559 err = got_error_from_errno2("unlink", path);
4564 static const struct got_error *
4565 histedit_save_list(struct got_histedit_list *histedit_cmds,
4566 struct got_worktree *worktree, struct got_repository *repo)
4568 const struct got_error *err = NULL;
4571 struct got_histedit_list_entry *hle;
4572 struct got_commit_object *commit = NULL;
4574 err = got_worktree_get_histedit_script_path(&path, worktree);
4578 f = fopen(path, "w");
4580 err = got_error_from_errno2("fopen", path);
4583 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4584 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4590 int n = fprintf(f, "%c %s\n",
4591 GOT_HISTEDIT_MESG, hle->logmsg);
4593 err = got_ferror(f, GOT_ERR_IO);
4599 if (f && fclose(f) != 0 && err == NULL)
4600 err = got_error_from_errno("fclose");
4603 got_object_commit_close(commit);
4608 histedit_free_list(struct got_histedit_list *histedit_cmds)
4610 struct got_histedit_list_entry *hle;
4612 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4613 TAILQ_REMOVE(histedit_cmds, hle, entry);
4618 static const struct got_error *
4619 histedit_load_list(struct got_histedit_list *histedit_cmds,
4620 const char *path, struct got_repository *repo)
4622 const struct got_error *err = NULL;
4625 f = fopen(path, "r");
4627 err = got_error_from_errno2("fopen", path);
4631 err = histedit_parse_list(histedit_cmds, f, repo);
4633 if (f && fclose(f) != 0 && err == NULL)
4634 err = got_error_from_errno("fclose");
4638 static const struct got_error *
4639 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4640 const struct got_error *edit_err, struct got_object_id_queue *commits,
4641 const char *path, struct got_repository *repo)
4643 const struct got_error *err = NULL, *prev_err = edit_err;
4646 while (resp != 'c' && resp != 'r' && resp != 'a') {
4647 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4648 "or (a)bort: ", getprogname(), prev_err->msg);
4651 histedit_free_list(histedit_cmds);
4652 err = histedit_run_editor(histedit_cmds, path, commits,
4655 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4656 err->code != GOT_ERR_HISTEDIT_CMD)
4663 } else if (resp == 'r') {
4664 histedit_free_list(histedit_cmds);
4665 err = histedit_edit_script(histedit_cmds,
4668 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4669 err->code != GOT_ERR_HISTEDIT_CMD)
4676 } else if (resp == 'a') {
4677 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4680 printf("invalid response '%c'\n", resp);
4686 static const struct got_error *
4687 histedit_complete(struct got_worktree *worktree,
4688 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4689 struct got_reference *branch, struct got_repository *repo)
4691 printf("Switching work tree to %s\n",
4692 got_ref_get_symref_target(branch));
4693 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4697 static const struct got_error *
4698 show_histedit_progress(struct got_commit_object *commit,
4699 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4701 const struct got_error *err;
4702 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4704 err = got_object_id_str(&old_id_str, hle->commit_id);
4709 err = got_object_id_str(&new_id_str, new_id);
4714 old_id_str[12] = '\0';
4716 new_id_str[12] = '\0';
4719 logmsg = strdup(hle->logmsg);
4720 if (logmsg == NULL) {
4721 err = got_error_from_errno("strdup");
4724 trim_logmsg(logmsg, 42);
4726 err = get_short_logmsg(&logmsg, 42, commit);
4731 switch (hle->cmd->code) {
4732 case GOT_HISTEDIT_PICK:
4733 case GOT_HISTEDIT_EDIT:
4734 printf("%s -> %s: %s\n", old_id_str,
4735 new_id_str ? new_id_str : "no-op change", logmsg);
4737 case GOT_HISTEDIT_DROP:
4738 case GOT_HISTEDIT_FOLD:
4739 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4752 static const struct got_error *
4753 histedit_commit(struct got_pathlist_head *merged_paths,
4754 struct got_worktree *worktree, struct got_fileindex *fileindex,
4755 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4756 struct got_repository *repo)
4758 const struct got_error *err;
4759 struct got_commit_object *commit;
4760 struct got_object_id *new_commit_id;
4762 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4763 && hle->logmsg == NULL) {
4764 err = histedit_edit_logmsg(hle, repo);
4769 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4773 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4774 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4777 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4779 err = show_histedit_progress(commit, hle, NULL);
4781 err = show_histedit_progress(commit, hle, new_commit_id);
4782 free(new_commit_id);
4785 got_object_commit_close(commit);
4789 static const struct got_error *
4790 histedit_skip_commit(struct got_histedit_list_entry *hle,
4791 struct got_worktree *worktree, struct got_repository *repo)
4793 const struct got_error *error;
4794 struct got_commit_object *commit;
4796 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4801 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4805 error = show_histedit_progress(commit, hle, NULL);
4806 got_object_commit_close(commit);
4810 static const struct got_error *
4811 cmd_histedit(int argc, char *argv[])
4813 const struct got_error *error = NULL;
4814 struct got_worktree *worktree = NULL;
4815 struct got_fileindex *fileindex = NULL;
4816 struct got_repository *repo = NULL;
4818 struct got_reference *branch = NULL;
4819 struct got_reference *tmp_branch = NULL;
4820 struct got_object_id *resume_commit_id = NULL;
4821 struct got_object_id *base_commit_id = NULL;
4822 struct got_object_id *head_commit_id = NULL;
4823 struct got_commit_object *commit = NULL;
4824 int ch, rebase_in_progress = 0, did_something;
4825 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4826 const char *edit_script_path = NULL;
4827 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4828 struct got_object_id_queue commits;
4829 struct got_pathlist_head merged_paths;
4830 const struct got_object_id_queue *parent_ids;
4831 struct got_object_qid *pid;
4832 struct got_histedit_list histedit_cmds;
4833 struct got_histedit_list_entry *hle;
4835 SIMPLEQ_INIT(&commits);
4836 TAILQ_INIT(&histedit_cmds);
4837 TAILQ_INIT(&merged_paths);
4839 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4848 edit_script_path = optarg;
4860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4861 "unveil", NULL) == -1)
4864 if (abort_edit && continue_edit)
4870 * This command cannot apply unveil(2) in all cases because the
4871 * user may choose to run an editor to edit the histedit script
4872 * and to edit individual commit log messages.
4873 * unveil(2) traverses exec(2); if an editor is used we have to
4874 * apply unveil after edit script and log messages have been written.
4875 * XXX TODO: Make use of unveil(2) where possible.
4878 cwd = getcwd(NULL, 0);
4880 error = got_error_from_errno("getcwd");
4883 error = got_worktree_open(&worktree, cwd);
4887 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4891 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4894 if (rebase_in_progress) {
4895 error = got_error(GOT_ERR_REBASING);
4899 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4903 if (edit_in_progress && abort_edit) {
4904 error = got_worktree_histedit_continue(&resume_commit_id,
4905 &tmp_branch, &branch, &base_commit_id, &fileindex,
4909 printf("Switching work tree to %s\n",
4910 got_ref_get_symref_target(branch));
4911 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4912 branch, base_commit_id, update_progress, &did_something);
4915 printf("Histedit of %s aborted\n",
4916 got_ref_get_symref_target(branch));
4917 goto done; /* nothing else to do */
4918 } else if (abort_edit) {
4919 error = got_error(GOT_ERR_NOT_HISTEDIT);
4923 if (continue_edit) {
4926 if (!edit_in_progress) {
4927 error = got_error(GOT_ERR_NOT_HISTEDIT);
4931 error = got_worktree_get_histedit_script_path(&path, worktree);
4935 error = histedit_load_list(&histedit_cmds, path, repo);
4940 error = got_worktree_histedit_continue(&resume_commit_id,
4941 &tmp_branch, &branch, &base_commit_id, &fileindex,
4946 error = got_ref_resolve(&head_commit_id, repo, branch);
4950 error = got_object_open_as_commit(&commit, repo,
4954 parent_ids = got_object_commit_get_parent_ids(commit);
4955 pid = SIMPLEQ_FIRST(parent_ids);
4957 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4960 error = collect_commits(&commits, head_commit_id, pid->id,
4961 base_commit_id, got_worktree_get_path_prefix(worktree),
4962 GOT_ERR_HISTEDIT_PATH, repo);
4963 got_object_commit_close(commit);
4968 if (edit_in_progress) {
4969 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4973 error = got_ref_open(&branch, repo,
4974 got_worktree_get_head_ref_name(worktree), 0);
4978 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4979 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4980 "will not edit commit history of a branch outside "
4981 "the \"refs/heads/\" reference namespace");
4985 error = got_ref_resolve(&head_commit_id, repo, branch);
4986 got_ref_close(branch);
4991 error = got_object_open_as_commit(&commit, repo,
4995 parent_ids = got_object_commit_get_parent_ids(commit);
4996 pid = SIMPLEQ_FIRST(parent_ids);
4998 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5001 error = collect_commits(&commits, head_commit_id, pid->id,
5002 got_worktree_get_base_commit_id(worktree),
5003 got_worktree_get_path_prefix(worktree),
5004 GOT_ERR_HISTEDIT_PATH, repo);
5005 got_object_commit_close(commit);
5010 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5011 &base_commit_id, &fileindex, worktree, repo);
5015 if (edit_script_path) {
5016 error = histedit_load_list(&histedit_cmds,
5017 edit_script_path, repo);
5019 got_worktree_histedit_abort(worktree, fileindex,
5020 repo, branch, base_commit_id,
5021 update_progress, &did_something);
5025 error = histedit_edit_script(&histedit_cmds, &commits,
5028 got_worktree_histedit_abort(worktree, fileindex,
5029 repo, branch, base_commit_id,
5030 update_progress, &did_something);
5036 error = histedit_save_list(&histedit_cmds, worktree,
5039 got_worktree_histedit_abort(worktree, fileindex,
5040 repo, branch, base_commit_id,
5041 update_progress, &did_something);
5047 error = histedit_check_script(&histedit_cmds, &commits, repo);
5051 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5052 if (resume_commit_id) {
5053 if (got_object_id_cmp(hle->commit_id,
5054 resume_commit_id) != 0)
5057 resume_commit_id = NULL;
5058 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5059 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5060 error = histedit_skip_commit(hle, worktree,
5063 error = histedit_commit(NULL, worktree,
5064 fileindex, tmp_branch, hle, repo);
5071 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5072 error = histedit_skip_commit(hle, worktree, repo);
5078 error = got_object_open_as_commit(&commit, repo,
5082 parent_ids = got_object_commit_get_parent_ids(commit);
5083 pid = SIMPLEQ_FIRST(parent_ids);
5085 error = got_worktree_histedit_merge_files(&merged_paths,
5086 worktree, fileindex, pid->id, hle->commit_id, repo,
5087 rebase_progress, &rebase_status, check_cancelled, NULL);
5090 got_object_commit_close(commit);
5093 if (rebase_status == GOT_STATUS_CONFLICT) {
5094 got_worktree_rebase_pathlist_free(&merged_paths);
5098 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5100 error = got_object_id_str(&id_str, hle->commit_id);
5103 printf("Stopping histedit for amending commit %s\n",
5106 got_worktree_rebase_pathlist_free(&merged_paths);
5107 error = got_worktree_histedit_postpone(worktree,
5112 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5113 error = histedit_skip_commit(hle, worktree, repo);
5119 error = histedit_commit(&merged_paths, worktree, fileindex,
5120 tmp_branch, hle, repo);
5121 got_worktree_rebase_pathlist_free(&merged_paths);
5126 if (rebase_status == GOT_STATUS_CONFLICT) {
5127 error = got_worktree_histedit_postpone(worktree, fileindex);
5130 error = got_error_msg(GOT_ERR_CONFLICTS,
5131 "conflicts must be resolved before rebasing can continue");
5133 error = histedit_complete(worktree, fileindex, tmp_branch,
5136 got_object_id_queue_free(&commits);
5137 histedit_free_list(&histedit_cmds);
5138 free(head_commit_id);
5139 free(base_commit_id);
5140 free(resume_commit_id);
5142 got_object_commit_close(commit);
5144 got_ref_close(branch);
5146 got_ref_close(tmp_branch);
5148 got_worktree_close(worktree);
5150 got_repo_close(repo);