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);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
161 main(int argc, char *argv[])
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
189 got_version_print_str();
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
241 editor = getenv("VISUAL");
243 editor = getenv("EDITOR");
246 err = got_path_find_prog(abspath, editor);
251 if (*abspath == NULL) {
252 *abspath = strdup("/bin/ed");
253 if (*abspath == NULL)
254 return got_error_from_errno("strdup");
260 static const struct got_error *
261 apply_unveil(const char *repo_path, int repo_read_only,
262 const char *worktree_path)
264 const struct got_error *err;
267 if (unveil("gmon.out", "rwc") != 0)
268 return got_error_from_errno2("unveil", "gmon.out");
270 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 return got_error_from_errno2("unveil", repo_path);
273 if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 return got_error_from_errno2("unveil", worktree_path);
276 if (unveil("/tmp", "rwc") != 0)
277 return got_error_from_errno2("unveil", "/tmp");
279 err = got_privsep_unveil_exec_helpers();
283 if (unveil(NULL, NULL) != 0)
284 return got_error_from_errno("unveil");
292 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
296 static const struct got_error *
297 cmd_init(int argc, char *argv[])
299 const struct got_error *error = NULL;
300 char *repo_path = NULL;
303 while ((ch = getopt(argc, argv, "")) != -1) {
315 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
321 repo_path = strdup(argv[0]);
322 if (repo_path == NULL)
323 return got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(repo_path);
327 error = got_path_mkdir(repo_path);
329 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
332 error = apply_unveil(repo_path, 0, NULL);
336 error = got_repo_init(repo_path);
348 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 "[-r repository-path] [-I pattern] path\n", getprogname());
354 spawn_editor(const char *editor, const char *file)
357 sig_t sighup, sigint, sigquit;
360 sighup = signal(SIGHUP, SIG_IGN);
361 sigint = signal(SIGINT, SIG_IGN);
362 sigquit = signal(SIGQUIT, SIG_IGN);
364 switch (pid = fork()) {
368 execl(editor, editor, file, (char *)NULL);
372 while (waitpid(pid, &st, 0) == -1)
377 (void)signal(SIGHUP, sighup);
378 (void)signal(SIGINT, sigint);
379 (void)signal(SIGQUIT, sigquit);
381 if (!WIFEXITED(st)) {
386 return WEXITSTATUS(st);
389 static const struct got_error *
390 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 const char *initial_content)
393 const struct got_error *err = NULL;
397 int content_changed = 0;
402 if (stat(logmsg_path, &st) == -1)
403 return got_error_from_errno2("stat", logmsg_path);
405 if (spawn_editor(editor, logmsg_path) == -1)
406 return got_error_from_errno("failed spawning editor");
408 if (stat(logmsg_path, &st2) == -1)
409 return got_error_from_errno("stat");
411 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 "no changes made to commit message, aborting");
415 *logmsg = malloc(st2.st_size + 1);
417 return got_error_from_errno("malloc");
421 fp = fopen(logmsg_path, "r");
423 err = got_error_from_errno("fopen");
426 while (fgets(buf, sizeof(buf), fp) != NULL) {
427 if (!content_changed && strcmp(buf, initial_content) != 0)
429 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 continue; /* remove comments and leading empty lines */
431 len = strlcat(*logmsg, buf, st2.st_size);
435 while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 (*logmsg)[len - 1] = '\0';
440 if (len == 0 || !content_changed)
441 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 "commit message cannot be empty, aborting");
451 static const struct got_error *
452 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 const char *branch_name)
455 char *initial_content = NULL, *logmsg_path = NULL;
456 const struct got_error *err = NULL;
459 if (asprintf(&initial_content,
460 "\n# %s to be imported to branch %s\n", path_dir,
462 return got_error_from_errno("asprintf");
464 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
468 dprintf(fd, initial_content);
471 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
473 free(initial_content);
478 static const struct got_error *
479 import_progress(void *arg, const char *path)
481 printf("A %s\n", path);
485 static const struct got_error *
486 cmd_import(int argc, char *argv[])
488 const struct got_error *error = NULL;
489 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
491 const char *got_author = getenv("GOT_AUTHOR");
492 const char *branch_name = "master";
493 char *refname = NULL, *id_str = NULL;
494 struct got_repository *repo = NULL;
495 struct got_reference *branch_ref = NULL, *head_ref = NULL;
496 struct got_object_id *new_commit_id = NULL;
498 struct got_pathlist_head ignores;
499 struct got_pathlist_entry *pe;
501 TAILQ_INIT(&ignores);
503 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
506 branch_name = optarg;
509 logmsg = strdup(optarg);
510 if (logmsg == NULL) {
511 error = got_error_from_errno("strdup");
516 repo_path = realpath(optarg, NULL);
517 if (repo_path == NULL) {
518 error = got_error_from_errno("realpath");
523 if (optarg[0] == '\0')
525 error = got_pathlist_insert(&pe, &ignores, optarg,
540 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
547 if (got_author == NULL) {
548 /* TODO: Look current user up in password database */
549 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
553 if (repo_path == NULL) {
554 repo_path = getcwd(NULL, 0);
555 if (repo_path == NULL)
556 return got_error_from_errno("getcwd");
558 got_path_strip_trailing_slashes(repo_path);
559 error = got_repo_open(&repo, repo_path);
563 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
564 error = got_error_from_errno("asprintf");
568 error = got_ref_open(&branch_ref, repo, refname, 0);
570 if (error->code != GOT_ERR_NOT_REF)
573 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
574 "import target branch already exists");
578 path_dir = realpath(argv[0], NULL);
579 if (path_dir == NULL) {
580 error = got_error_from_errno("realpath");
583 got_path_strip_trailing_slashes(path_dir);
586 * unveil(2) traverses exec(2); if an editor is used we have
587 * to apply unveil after the log message has been written.
589 if (logmsg == NULL || strlen(logmsg) == 0) {
590 error = get_editor(&editor);
593 error = collect_import_msg(&logmsg, editor, path_dir, refname);
598 if (unveil(path_dir, "r") != 0)
599 return got_error_from_errno2("unveil", path_dir);
601 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
605 error = got_repo_import(&new_commit_id, path_dir, logmsg,
606 got_author, &ignores, repo, import_progress, NULL);
610 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
614 error = got_ref_write(branch_ref, repo);
618 error = got_object_id_str(&id_str, new_commit_id);
622 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
624 if (error->code != GOT_ERR_NOT_REF)
627 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
632 error = got_ref_write(head_ref, repo);
637 printf("Created branch %s with commit %s\n",
638 got_ref_get_name(branch_ref), id_str);
646 got_ref_close(branch_ref);
648 got_ref_close(head_ref);
655 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
656 "[-p prefix] repository-path [worktree-path]\n", getprogname());
660 static const struct got_error *
661 checkout_progress(void *arg, unsigned char status, const char *path)
663 char *worktree_path = arg;
665 /* Base commit bump happens silently. */
666 if (status == GOT_STATUS_BUMP_BASE)
669 while (path[0] == '/')
672 printf("%c %s/%s\n", status, worktree_path, path);
676 static const struct got_error *
677 check_cancelled(void *arg)
679 if (sigint_received || sigpipe_received)
680 return got_error(GOT_ERR_CANCELLED);
684 static const struct got_error *
685 check_linear_ancestry(struct got_object_id *commit_id,
686 struct got_object_id *base_commit_id, struct got_repository *repo)
688 const struct got_error *err = NULL;
689 struct got_object_id *yca_id;
691 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
692 commit_id, base_commit_id, repo);
697 return got_error(GOT_ERR_ANCESTRY);
700 * Require a straight line of history between the target commit
701 * and the work tree's base commit.
703 * Non-linear situations such as this require a rebase:
705 * (commit) D F (base_commit)
713 * 'got update' only handles linear cases:
714 * Update forwards in time: A (base/yca) - B - C - D (commit)
715 * Update backwards in time: D (base) - C - B - A (commit/yca)
717 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
718 got_object_id_cmp(base_commit_id, yca_id) != 0)
719 return got_error(GOT_ERR_ANCESTRY);
725 static const struct got_error *
726 check_same_branch(struct got_object_id *commit_id,
727 struct got_reference *head_ref, struct got_object_id *yca_id,
728 struct got_repository *repo)
730 const struct got_error *err = NULL;
731 struct got_commit_graph *graph = NULL;
732 struct got_object_id *head_commit_id = NULL;
733 int is_same_branch = 0;
735 err = got_ref_resolve(&head_commit_id, repo, head_ref);
739 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
743 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
748 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
752 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
757 struct got_object_id *id;
758 err = got_commit_graph_iter_next(&id, graph);
760 if (err->code == GOT_ERR_ITER_COMPLETED) {
763 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
765 err = got_commit_graph_fetch_commits(graph, 1,
772 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
774 if (got_object_id_cmp(id, commit_id) == 0) {
782 got_commit_graph_close(graph);
783 free(head_commit_id);
784 if (!err && !is_same_branch)
785 err = got_error(GOT_ERR_ANCESTRY);
789 static const struct got_error *
790 resolve_commit_arg(struct got_object_id **commit_id,
791 const char *commit_id_arg, struct got_repository *repo)
793 const struct got_error *err;
794 struct got_reference *ref;
796 err = got_ref_open(&ref, repo, commit_id_arg, 0);
798 err = got_ref_resolve(commit_id, repo, ref);
801 if (err->code != GOT_ERR_NOT_REF)
803 err = got_repo_match_object_id_prefix(commit_id,
804 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
809 static const struct got_error *
810 cmd_checkout(int argc, char *argv[])
812 const struct got_error *error = NULL;
813 struct got_repository *repo = NULL;
814 struct got_reference *head_ref = NULL;
815 struct got_worktree *worktree = NULL;
816 char *repo_path = NULL;
817 char *worktree_path = NULL;
818 const char *path_prefix = "";
819 const char *branch_name = GOT_REF_HEAD;
820 char *commit_id_str = NULL;
821 int ch, same_path_prefix;
822 struct got_pathlist_head paths;
826 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
829 branch_name = optarg;
832 commit_id_str = strdup(optarg);
833 if (commit_id_str == NULL)
834 return got_error_from_errno("strdup");
837 path_prefix = optarg;
849 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
850 "unveil", NULL) == -1)
854 char *cwd, *base, *dotgit;
855 repo_path = realpath(argv[0], NULL);
856 if (repo_path == NULL)
857 return got_error_from_errno2("realpath", argv[0]);
858 cwd = getcwd(NULL, 0);
860 error = got_error_from_errno("getcwd");
863 if (path_prefix[0]) {
864 base = basename(path_prefix);
866 error = got_error_from_errno2("basename",
871 base = basename(repo_path);
873 error = got_error_from_errno2("basename",
878 dotgit = strstr(base, ".git");
881 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
882 error = got_error_from_errno("asprintf");
887 } else if (argc == 2) {
888 repo_path = realpath(argv[0], NULL);
889 if (repo_path == NULL) {
890 error = got_error_from_errno2("realpath", argv[0]);
893 worktree_path = realpath(argv[1], NULL);
894 if (worktree_path == NULL) {
895 if (errno != ENOENT) {
896 error = got_error_from_errno2("realpath",
900 worktree_path = strdup(argv[1]);
901 if (worktree_path == NULL) {
902 error = got_error_from_errno("strdup");
909 got_path_strip_trailing_slashes(repo_path);
910 got_path_strip_trailing_slashes(worktree_path);
912 error = got_repo_open(&repo, repo_path);
916 /* Pre-create work tree path for unveil(2) */
917 error = got_path_mkdir(worktree_path);
919 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
921 if (!got_path_dir_is_empty(worktree_path)) {
922 error = got_error_path(worktree_path,
923 GOT_ERR_DIR_NOT_EMPTY);
928 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
932 error = got_ref_open(&head_ref, repo, branch_name, 0);
936 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
937 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
940 error = got_worktree_open(&worktree, worktree_path);
944 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
948 if (!same_path_prefix) {
949 error = got_error(GOT_ERR_PATH_PREFIX);
954 struct got_object_id *commit_id;
955 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
958 error = check_linear_ancestry(commit_id,
959 got_worktree_get_base_commit_id(worktree), repo);
964 error = check_same_branch(commit_id, head_ref, NULL, repo);
967 error = got_worktree_set_base_commit_id(worktree, repo,
974 error = got_pathlist_append(&paths, "", NULL);
977 error = got_worktree_checkout_files(worktree, &paths, repo,
978 checkout_progress, worktree_path, check_cancelled, NULL);
982 printf("Now shut up and hack\n");
985 got_pathlist_free(&paths);
995 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1000 static const struct got_error *
1001 update_progress(void *arg, unsigned char status, const char *path)
1003 int *did_something = arg;
1005 if (status == GOT_STATUS_EXISTS)
1010 /* Base commit bump happens silently. */
1011 if (status == GOT_STATUS_BUMP_BASE)
1014 while (path[0] == '/')
1016 printf("%c %s\n", status, path);
1020 static const struct got_error *
1021 switch_head_ref(struct got_reference *head_ref,
1022 struct got_object_id *commit_id, struct got_worktree *worktree,
1023 struct got_repository *repo)
1025 const struct got_error *err = NULL;
1027 int ref_has_moved = 0;
1029 /* Trivial case: switching between two different references. */
1030 if (strcmp(got_ref_get_name(head_ref),
1031 got_worktree_get_head_ref_name(worktree)) != 0) {
1032 printf("Switching work tree from %s to %s\n",
1033 got_worktree_get_head_ref_name(worktree),
1034 got_ref_get_name(head_ref));
1035 return got_worktree_set_head_ref(worktree, head_ref);
1038 err = check_linear_ancestry(commit_id,
1039 got_worktree_get_base_commit_id(worktree), repo);
1041 if (err->code != GOT_ERR_ANCESTRY)
1048 /* Switching to a rebased branch with the same reference name. */
1049 err = got_object_id_str(&base_id_str,
1050 got_worktree_get_base_commit_id(worktree));
1053 printf("Reference %s now points at a different branch\n",
1054 got_worktree_get_head_ref_name(worktree));
1055 printf("Switching work tree from %s to %s\n", base_id_str,
1056 got_worktree_get_head_ref_name(worktree));
1060 static const struct got_error *
1061 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1063 const struct got_error *err;
1066 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1070 return got_error(GOT_ERR_REBASING);
1072 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1076 return got_error(GOT_ERR_HISTEDIT_BUSY);
1081 static const struct got_error *
1082 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1083 char *argv[], struct got_worktree *worktree)
1085 const struct got_error *err;
1092 return got_error_from_errno("strdup");
1093 return got_pathlist_append(paths, path, NULL);
1096 for (i = 0; i < argc; i++) {
1097 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1100 err = got_pathlist_append(paths, path, NULL);
1110 static const struct got_error *
1111 cmd_update(int argc, char *argv[])
1113 const struct got_error *error = NULL;
1114 struct got_repository *repo = NULL;
1115 struct got_worktree *worktree = NULL;
1116 char *worktree_path = NULL;
1117 struct got_object_id *commit_id = NULL;
1118 char *commit_id_str = NULL;
1119 const char *branch_name = NULL;
1120 struct got_reference *head_ref = NULL;
1121 struct got_pathlist_head paths;
1122 struct got_pathlist_entry *pe;
1123 int ch, did_something = 0;
1127 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1130 branch_name = optarg;
1133 commit_id_str = strdup(optarg);
1134 if (commit_id_str == NULL)
1135 return got_error_from_errno("strdup");
1147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1148 "unveil", NULL) == -1)
1151 worktree_path = getcwd(NULL, 0);
1152 if (worktree_path == NULL) {
1153 error = got_error_from_errno("getcwd");
1156 error = got_worktree_open(&worktree, worktree_path);
1160 error = check_rebase_or_histedit_in_progress(worktree);
1164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1168 error = apply_unveil(got_repo_get_path(repo), 0,
1169 got_worktree_get_root_path(worktree));
1173 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1177 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1178 got_worktree_get_head_ref_name(worktree), 0);
1181 if (commit_id_str == NULL) {
1182 error = got_ref_resolve(&commit_id, repo, head_ref);
1185 error = got_object_id_str(&commit_id_str, commit_id);
1189 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1190 free(commit_id_str);
1191 commit_id_str = NULL;
1194 error = got_object_id_str(&commit_id_str, commit_id);
1200 struct got_object_id *head_commit_id;
1201 TAILQ_FOREACH(pe, &paths, entry) {
1202 if (pe->path_len == 0)
1204 error = got_error_msg(GOT_ERR_BAD_PATH,
1205 "switching between branches requires that "
1206 "the entire work tree gets updated");
1209 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1212 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1213 free(head_commit_id);
1216 error = check_same_branch(commit_id, head_ref, NULL, repo);
1219 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1223 error = check_linear_ancestry(commit_id,
1224 got_worktree_get_base_commit_id(worktree), repo);
1225 if (error != NULL) {
1226 if (error->code == GOT_ERR_ANCESTRY)
1227 error = got_error(GOT_ERR_BRANCH_MOVED);
1230 error = check_same_branch(commit_id, head_ref, NULL, repo);
1235 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1237 error = got_worktree_set_base_commit_id(worktree, repo,
1243 error = got_worktree_checkout_files(worktree, &paths, repo,
1244 update_progress, &did_something, check_cancelled, NULL);
1249 printf("Updated to commit %s\n", commit_id_str);
1251 printf("Already up-to-date\n");
1253 free(worktree_path);
1254 TAILQ_FOREACH(pe, &paths, entry)
1255 free((char *)pe->path);
1256 got_pathlist_free(&paths);
1258 free(commit_id_str);
1262 static const struct got_error *
1263 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1264 int diff_context, struct got_repository *repo)
1266 const struct got_error *err = NULL;
1267 struct got_tree_object *tree1 = NULL, *tree2;
1268 struct got_object_qid *qid;
1269 char *id_str1 = NULL, *id_str2;
1270 struct got_diff_blob_output_unidiff_arg arg;
1272 err = got_object_open_as_tree(&tree2, repo,
1273 got_object_commit_get_tree_id(commit));
1277 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1279 struct got_commit_object *pcommit;
1281 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1285 err = got_object_open_as_tree(&tree1, repo,
1286 got_object_commit_get_tree_id(pcommit));
1287 got_object_commit_close(pcommit);
1291 err = got_object_id_str(&id_str1, qid->id);
1296 err = got_object_id_str(&id_str2, id);
1300 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1301 arg.diff_context = diff_context;
1302 arg.outfile = stdout;
1303 err = got_diff_tree(tree1, tree2, "", "", repo,
1304 got_diff_blob_output_unidiff, &arg, 1);
1307 got_object_tree_close(tree1);
1308 got_object_tree_close(tree2);
1315 get_datestr(time_t *time, char *datebuf)
1317 char *p, *s = ctime_r(time, datebuf);
1318 p = strchr(s, '\n');
1324 static const struct got_error *
1325 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1326 struct got_repository *repo, int show_patch, int diff_context,
1327 struct got_reflist_head *refs)
1329 const struct got_error *err = NULL;
1330 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1332 time_t committer_time;
1333 const char *author, *committer;
1334 char *refs_str = NULL;
1335 struct got_reflist_entry *re;
1337 SIMPLEQ_FOREACH(re, refs, entry) {
1340 if (got_object_id_cmp(re->id, id) != 0)
1342 name = got_ref_get_name(re->ref);
1343 if (strcmp(name, GOT_REF_HEAD) == 0)
1345 if (strncmp(name, "refs/", 5) == 0)
1347 if (strncmp(name, "got/", 4) == 0)
1349 if (strncmp(name, "heads/", 6) == 0)
1351 if (strncmp(name, "remotes/", 8) == 0)
1354 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1356 err = got_error_from_errno("asprintf");
1362 err = got_object_id_str(&id_str, id);
1366 printf("-----------------------------------------------\n");
1367 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1368 refs_str ? refs_str : "", refs_str ? ")" : "");
1373 printf("from: %s\n", got_object_commit_get_author(commit));
1374 committer_time = got_object_commit_get_committer_time(commit);
1375 datestr = get_datestr(&committer_time, datebuf);
1376 printf("date: %s UTC\n", datestr);
1377 author = got_object_commit_get_author(commit);
1378 committer = got_object_commit_get_committer(commit);
1379 if (strcmp(author, committer) != 0)
1380 printf("via: %s\n", committer);
1381 if (got_object_commit_get_nparents(commit) > 1) {
1382 const struct got_object_id_queue *parent_ids;
1383 struct got_object_qid *qid;
1385 parent_ids = got_object_commit_get_parent_ids(commit);
1386 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1387 err = got_object_id_str(&id_str, qid->id);
1390 printf("parent %d: %s\n", n++, id_str);
1395 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1396 if (logmsg0 == NULL)
1397 return got_error_from_errno("strdup");
1401 line = strsep(&logmsg, "\n");
1403 printf(" %s\n", line);
1408 err = print_patch(commit, id, diff_context, repo);
1413 if (fflush(stdout) != 0 && err == NULL)
1414 err = got_error_from_errno("fflush");
1418 static const struct got_error *
1419 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1420 char *path, int show_patch, int diff_context, int limit,
1421 int first_parent_traversal, struct got_reflist_head *refs)
1423 const struct got_error *err;
1424 struct got_commit_graph *graph;
1426 err = got_commit_graph_open(&graph, root_id, path,
1427 first_parent_traversal, repo);
1430 err = got_commit_graph_iter_start(graph, root_id, repo);
1434 struct got_commit_object *commit;
1435 struct got_object_id *id;
1437 if (sigint_received || sigpipe_received)
1440 err = got_commit_graph_iter_next(&id, graph);
1442 if (err->code == GOT_ERR_ITER_COMPLETED) {
1446 if (err->code != GOT_ERR_ITER_NEED_MORE)
1448 err = got_commit_graph_fetch_commits(graph, 1, repo);
1457 err = got_object_open_as_commit(&commit, repo, id);
1460 err = print_commit(commit, id, repo, show_patch, diff_context,
1462 got_object_commit_close(commit);
1463 if (err || (limit && --limit == 0))
1467 got_commit_graph_close(graph);
1474 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1475 "[-r repository-path] [path]\n", getprogname());
1479 static const struct got_error *
1480 cmd_log(int argc, char *argv[])
1482 const struct got_error *error;
1483 struct got_repository *repo = NULL;
1484 struct got_worktree *worktree = NULL;
1485 struct got_commit_object *commit = NULL;
1486 struct got_object_id *id = NULL;
1487 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1488 char *start_commit = NULL;
1489 int diff_context = 3, ch;
1490 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1492 struct got_reflist_head refs;
1494 SIMPLEQ_INIT(&refs);
1497 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1503 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1509 start_commit = optarg;
1512 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1515 err(1, "-C option %s", errstr);
1518 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1520 err(1, "-l option %s", errstr);
1523 first_parent_traversal = 1;
1526 repo_path = realpath(optarg, NULL);
1527 if (repo_path == NULL)
1528 err(1, "-r option");
1529 got_path_strip_trailing_slashes(repo_path);
1540 cwd = getcwd(NULL, 0);
1542 error = got_error_from_errno("getcwd");
1546 error = got_worktree_open(&worktree, cwd);
1547 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1554 error = got_error_from_errno("strdup");
1557 } else if (argc == 1) {
1559 error = got_worktree_resolve_path(&path, worktree,
1564 path = strdup(argv[0]);
1566 error = got_error_from_errno("strdup");
1573 if (repo_path == NULL) {
1574 repo_path = worktree ?
1575 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1577 if (repo_path == NULL) {
1578 error = got_error_from_errno("strdup");
1582 error = got_repo_open(&repo, repo_path);
1586 error = apply_unveil(got_repo_get_path(repo), 1,
1587 worktree ? got_worktree_get_root_path(worktree) : NULL);
1591 if (start_commit == NULL) {
1592 struct got_reference *head_ref;
1593 error = got_ref_open(&head_ref, repo,
1594 worktree ? got_worktree_get_head_ref_name(worktree)
1598 error = got_ref_resolve(&id, repo, head_ref);
1599 got_ref_close(head_ref);
1602 error = got_object_open_as_commit(&commit, repo, id);
1604 struct got_reference *ref;
1605 error = got_ref_open(&ref, repo, start_commit, 0);
1606 if (error == NULL) {
1608 error = got_ref_resolve(&id, repo, ref);
1612 error = got_object_get_type(&obj_type, repo, id);
1615 if (obj_type == GOT_OBJ_TYPE_TAG) {
1616 struct got_tag_object *tag;
1617 error = got_object_open_as_tag(&tag, repo, id);
1620 if (got_object_tag_get_object_type(tag) !=
1621 GOT_OBJ_TYPE_COMMIT) {
1622 got_object_tag_close(tag);
1623 error = got_error(GOT_ERR_OBJ_TYPE);
1627 id = got_object_id_dup(
1628 got_object_tag_get_object_id(tag));
1630 error = got_error_from_errno(
1631 "got_object_id_dup");
1632 got_object_tag_close(tag);
1635 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1636 error = got_error(GOT_ERR_OBJ_TYPE);
1639 error = got_object_open_as_commit(&commit, repo, id);
1643 if (commit == NULL) {
1644 error = got_repo_match_object_id_prefix(&id,
1645 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1653 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1658 path = in_repo_path;
1661 error = got_ref_list(&refs, repo);
1665 error = print_commits(id, repo, path, show_patch,
1666 diff_context, limit, first_parent_traversal, &refs);
1673 got_worktree_close(worktree);
1675 const struct got_error *repo_error;
1676 repo_error = got_repo_close(repo);
1680 got_ref_list_free(&refs);
1687 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1688 "[object1 object2 | path]\n", getprogname());
1692 struct print_diff_arg {
1693 struct got_repository *repo;
1694 struct got_worktree *worktree;
1701 static const struct got_error *
1702 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1703 const char *path, struct got_object_id *blob_id,
1704 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1706 struct print_diff_arg *a = arg;
1707 const struct got_error *err = NULL;
1708 struct got_blob_object *blob1 = NULL;
1710 char *abspath = NULL;
1713 if (a->diff_staged) {
1714 if (staged_status != GOT_STATUS_MODIFY &&
1715 staged_status != GOT_STATUS_ADD &&
1716 staged_status != GOT_STATUS_DELETE)
1719 if (staged_status == GOT_STATUS_DELETE)
1721 if (status != GOT_STATUS_MODIFY &&
1722 status != GOT_STATUS_ADD &&
1723 status != GOT_STATUS_DELETE &&
1724 status != GOT_STATUS_CONFLICT)
1728 if (!a->header_shown) {
1729 printf("diff %s %s%s\n", a->id_str,
1730 got_worktree_get_root_path(a->worktree),
1731 a->diff_staged ? " (staged changes)" : "");
1732 a->header_shown = 1;
1735 if (a->diff_staged) {
1736 const char *label1 = NULL, *label2 = NULL;
1737 switch (staged_status) {
1738 case GOT_STATUS_MODIFY:
1742 case GOT_STATUS_ADD:
1745 case GOT_STATUS_DELETE:
1749 return got_error(GOT_ERR_FILE_STATUS);
1751 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1752 label1, label2, a->diff_context, a->repo, stdout);
1755 if (staged_status == GOT_STATUS_ADD ||
1756 staged_status == GOT_STATUS_MODIFY)
1757 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1759 else if (status != GOT_STATUS_ADD)
1760 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1764 if (status != GOT_STATUS_DELETE) {
1765 if (asprintf(&abspath, "%s/%s",
1766 got_worktree_get_root_path(a->worktree), path) == -1) {
1767 err = got_error_from_errno("asprintf");
1771 f2 = fopen(abspath, "r");
1773 err = got_error_from_errno2("fopen", abspath);
1776 if (lstat(abspath, &sb) == -1) {
1777 err = got_error_from_errno2("lstat", abspath);
1783 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1787 got_object_blob_close(blob1);
1788 if (f2 && fclose(f2) != 0 && err == NULL)
1789 err = got_error_from_errno("fclose");
1794 static const struct got_error *
1795 cmd_diff(int argc, char *argv[])
1797 const struct got_error *error;
1798 struct got_repository *repo = NULL;
1799 struct got_worktree *worktree = NULL;
1800 char *cwd = NULL, *repo_path = NULL;
1801 struct got_object_id *id1 = NULL, *id2 = NULL;
1802 const char *id_str1 = NULL, *id_str2 = NULL;
1803 char *label1 = NULL, *label2 = NULL;
1805 int diff_context = 3, diff_staged = 0, ch;
1810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1815 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1818 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1820 err(1, "-C option %s", errstr);
1823 repo_path = realpath(optarg, NULL);
1824 if (repo_path == NULL)
1825 err(1, "-r option");
1826 got_path_strip_trailing_slashes(repo_path);
1840 cwd = getcwd(NULL, 0);
1842 error = got_error_from_errno("getcwd");
1845 error = got_worktree_open(&worktree, cwd);
1846 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 if (worktree == NULL) {
1850 error = got_error(GOT_ERR_NOT_WORKTREE);
1855 "-r option can't be used when diffing a work tree");
1856 repo_path = strdup(got_worktree_get_repo_path(worktree));
1857 if (repo_path == NULL) {
1858 error = got_error_from_errno("strdup");
1862 error = got_worktree_resolve_path(&path, worktree,
1869 error = got_error_from_errno("strdup");
1873 } else if (argc == 2) {
1875 errx(1, "-s option can't be used when diffing "
1876 "objects in repository");
1879 if (worktree && repo_path == NULL) {
1881 strdup(got_worktree_get_repo_path(worktree));
1882 if (repo_path == NULL) {
1883 error = got_error_from_errno("strdup");
1890 if (repo_path == NULL) {
1891 repo_path = getcwd(NULL, 0);
1892 if (repo_path == NULL)
1893 return got_error_from_errno("getcwd");
1896 error = got_repo_open(&repo, repo_path);
1901 error = apply_unveil(got_repo_get_path(repo), 1,
1902 worktree ? got_worktree_get_root_path(worktree) : NULL);
1907 struct print_diff_arg arg;
1908 struct got_pathlist_head paths;
1913 error = got_object_id_str(&id_str,
1914 got_worktree_get_base_commit_id(worktree));
1918 arg.worktree = worktree;
1919 arg.diff_context = diff_context;
1920 arg.id_str = id_str;
1921 arg.header_shown = 0;
1922 arg.diff_staged = diff_staged;
1924 error = got_pathlist_append(&paths, path, NULL);
1928 error = got_worktree_status(worktree, &paths, repo, print_diff,
1929 &arg, check_cancelled, NULL);
1931 got_pathlist_free(&paths);
1935 error = got_repo_match_object_id_prefix(&id1, id_str1,
1936 GOT_OBJ_TYPE_ANY, repo);
1938 struct got_reference *ref;
1939 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1941 error = got_ref_open(&ref, repo, id_str1, 0);
1944 label1 = strdup(got_ref_get_name(ref));
1945 if (label1 == NULL) {
1946 error = got_error_from_errno("strdup");
1949 error = got_ref_resolve(&id1, repo, ref);
1954 error = got_object_id_str(&label1, id1);
1955 if (label1 == NULL) {
1956 error = got_error_from_errno("strdup");
1961 error = got_repo_match_object_id_prefix(&id2, id_str2,
1962 GOT_OBJ_TYPE_ANY, repo);
1964 struct got_reference *ref;
1965 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1967 error = got_ref_open(&ref, repo, id_str2, 0);
1970 label2 = strdup(got_ref_get_name(ref));
1971 if (label2 == NULL) {
1972 error = got_error_from_errno("strdup");
1975 error = got_ref_resolve(&id2, repo, ref);
1980 error = got_object_id_str(&label2, id2);
1981 if (label2 == NULL) {
1982 error = got_error_from_errno("strdup");
1987 error = got_object_get_type(&type1, repo, id1);
1991 error = got_object_get_type(&type2, repo, id2);
1995 if (type1 != type2) {
1996 error = got_error(GOT_ERR_OBJ_TYPE);
2001 case GOT_OBJ_TYPE_BLOB:
2002 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2003 diff_context, repo, stdout);
2005 case GOT_OBJ_TYPE_TREE:
2006 error = got_diff_objects_as_trees(id1, id2, "", "",
2007 diff_context, repo, stdout);
2009 case GOT_OBJ_TYPE_COMMIT:
2010 printf("diff %s %s\n", label1, label2);
2011 error = got_diff_objects_as_commits(id1, id2, diff_context,
2015 error = got_error(GOT_ERR_OBJ_TYPE);
2025 got_worktree_close(worktree);
2027 const struct got_error *repo_error;
2028 repo_error = got_repo_close(repo);
2039 "usage: %s blame [-c commit] [-r repository-path] path\n",
2044 static const struct got_error *
2045 cmd_blame(int argc, char *argv[])
2047 const struct got_error *error;
2048 struct got_repository *repo = NULL;
2049 struct got_worktree *worktree = NULL;
2050 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2051 struct got_object_id *commit_id = NULL;
2052 char *commit_id_str = NULL;
2056 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2061 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2064 commit_id_str = optarg;
2067 repo_path = realpath(optarg, NULL);
2068 if (repo_path == NULL)
2069 err(1, "-r option");
2070 got_path_strip_trailing_slashes(repo_path);
2086 cwd = getcwd(NULL, 0);
2088 error = got_error_from_errno("getcwd");
2091 if (repo_path == NULL) {
2092 error = got_worktree_open(&worktree, cwd);
2093 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2099 strdup(got_worktree_get_repo_path(worktree));
2100 if (repo_path == NULL)
2101 error = got_error_from_errno("strdup");
2105 repo_path = strdup(cwd);
2106 if (repo_path == NULL) {
2107 error = got_error_from_errno("strdup");
2113 error = got_repo_open(&repo, repo_path);
2117 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2122 const char *prefix = got_worktree_get_path_prefix(worktree);
2123 char *p, *worktree_subdir = cwd +
2124 strlen(got_worktree_get_root_path(worktree));
2125 if (asprintf(&p, "%s%s%s%s%s",
2126 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2127 worktree_subdir, worktree_subdir[0] ? "/" : "",
2129 error = got_error_from_errno("asprintf");
2132 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2135 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2140 if (commit_id_str == NULL) {
2141 struct got_reference *head_ref;
2142 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2145 error = got_ref_resolve(&commit_id, repo, head_ref);
2146 got_ref_close(head_ref);
2150 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2155 error = got_blame(in_repo_path, commit_id, repo, stdout);
2162 got_worktree_close(worktree);
2164 const struct got_error *repo_error;
2165 repo_error = got_repo_close(repo);
2176 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2182 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2183 const char *root_path)
2185 int is_root_path = (strcmp(path, root_path) == 0);
2187 path += strlen(root_path);
2188 while (path[0] == '/')
2191 printf("%s%s%s%s%s\n", id ? id : "", path,
2192 is_root_path ? "" : "/", te->name,
2193 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2196 static const struct got_error *
2197 print_tree(const char *path, struct got_object_id *commit_id,
2198 int show_ids, int recurse, const char *root_path,
2199 struct got_repository *repo)
2201 const struct got_error *err = NULL;
2202 struct got_object_id *tree_id = NULL;
2203 struct got_tree_object *tree = NULL;
2204 const struct got_tree_entries *entries;
2205 struct got_tree_entry *te;
2207 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2211 err = got_object_open_as_tree(&tree, repo, tree_id);
2214 entries = got_object_tree_get_entries(tree);
2215 te = SIMPLEQ_FIRST(&entries->head);
2219 if (sigint_received || sigpipe_received)
2224 err = got_object_id_str(&id_str, te->id);
2227 if (asprintf(&id, "%s ", id_str) == -1) {
2228 err = got_error_from_errno("asprintf");
2234 print_entry(te, id, path, root_path);
2237 if (recurse && S_ISDIR(te->mode)) {
2239 if (asprintf(&child_path, "%s%s%s", path,
2240 path[0] == '/' && path[1] == '\0' ? "" : "/",
2242 err = got_error_from_errno("asprintf");
2245 err = print_tree(child_path, commit_id, show_ids, 1,
2252 te = SIMPLEQ_NEXT(te, entry);
2256 got_object_tree_close(tree);
2261 static const struct got_error *
2262 cmd_tree(int argc, char *argv[])
2264 const struct got_error *error;
2265 struct got_repository *repo = NULL;
2266 struct got_worktree *worktree = NULL;
2268 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2269 struct got_object_id *commit_id = NULL;
2270 char *commit_id_str = NULL;
2271 int show_ids = 0, recurse = 0;
2275 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2280 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2283 commit_id_str = optarg;
2286 repo_path = realpath(optarg, NULL);
2287 if (repo_path == NULL)
2288 err(1, "-r option");
2289 got_path_strip_trailing_slashes(repo_path);
2313 cwd = getcwd(NULL, 0);
2315 error = got_error_from_errno("getcwd");
2318 if (repo_path == NULL) {
2319 error = got_worktree_open(&worktree, cwd);
2320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2326 strdup(got_worktree_get_repo_path(worktree));
2327 if (repo_path == NULL)
2328 error = got_error_from_errno("strdup");
2332 repo_path = strdup(cwd);
2333 if (repo_path == NULL) {
2334 error = got_error_from_errno("strdup");
2340 error = got_repo_open(&repo, repo_path);
2344 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2350 char *p, *worktree_subdir = cwd +
2351 strlen(got_worktree_get_root_path(worktree));
2352 if (asprintf(&p, "%s/%s",
2353 got_worktree_get_path_prefix(worktree),
2354 worktree_subdir) == -1) {
2355 error = got_error_from_errno("asprintf");
2358 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2365 if (in_repo_path == NULL) {
2366 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2371 if (commit_id_str == NULL) {
2372 struct got_reference *head_ref;
2373 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2376 error = got_ref_resolve(&commit_id, repo, head_ref);
2377 got_ref_close(head_ref);
2381 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2386 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2387 in_repo_path, repo);
2394 got_worktree_close(worktree);
2396 const struct got_error *repo_error;
2397 repo_error = got_repo_close(repo);
2407 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2411 static const struct got_error *
2412 print_status(void *arg, unsigned char status, unsigned char staged_status,
2413 const char *path, struct got_object_id *blob_id,
2414 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2416 if (status == staged_status && (status == GOT_STATUS_DELETE))
2417 status = GOT_STATUS_NO_CHANGE;
2418 printf("%c%c %s\n", status, staged_status, path);
2422 static const struct got_error *
2423 cmd_status(int argc, char *argv[])
2425 const struct got_error *error = NULL;
2426 struct got_repository *repo = NULL;
2427 struct got_worktree *worktree = NULL;
2429 struct got_pathlist_head paths;
2430 struct got_pathlist_entry *pe;
2435 while ((ch = getopt(argc, argv, "")) != -1) {
2447 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2451 cwd = getcwd(NULL, 0);
2453 error = got_error_from_errno("getcwd");
2457 error = got_worktree_open(&worktree, cwd);
2461 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2465 error = apply_unveil(got_repo_get_path(repo), 1,
2466 got_worktree_get_root_path(worktree));
2470 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2474 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2475 check_cancelled, NULL);
2477 TAILQ_FOREACH(pe, &paths, entry)
2478 free((char *)pe->path);
2479 got_pathlist_free(&paths);
2488 "usage: %s ref [-r repository] -l | -d name | name target\n",
2493 static const struct got_error *
2494 list_refs(struct got_repository *repo)
2496 static const struct got_error *err = NULL;
2497 struct got_reflist_head refs;
2498 struct got_reflist_entry *re;
2500 SIMPLEQ_INIT(&refs);
2501 err = got_ref_list(&refs, repo);
2505 SIMPLEQ_FOREACH(re, &refs, entry) {
2507 refstr = got_ref_to_str(re->ref);
2509 return got_error_from_errno("got_ref_to_str");
2510 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2514 got_ref_list_free(&refs);
2518 static const struct got_error *
2519 delete_ref(struct got_repository *repo, const char *refname)
2521 const struct got_error *err = NULL;
2522 struct got_reference *ref;
2524 err = got_ref_open(&ref, repo, refname, 0);
2528 err = got_ref_delete(ref, repo);
2533 static const struct got_error *
2534 add_ref(struct got_repository *repo, const char *refname, const char *target)
2536 const struct got_error *err = NULL;
2537 struct got_object_id *id;
2538 struct got_reference *ref = NULL;
2541 * Don't let the user create a reference named '-'.
2542 * While technically a valid reference name, this case is usually
2543 * an unintended typo.
2545 if (refname[0] == '-' && refname[1] == '\0')
2546 return got_error(GOT_ERR_BAD_REF_NAME);
2548 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2551 struct got_reference *target_ref;
2553 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2555 err = got_ref_open(&target_ref, repo, target, 0);
2558 err = got_ref_resolve(&id, repo, target_ref);
2559 got_ref_close(target_ref);
2564 err = got_ref_alloc(&ref, refname, id);
2568 err = got_ref_write(ref, repo);
2576 static const struct got_error *
2577 cmd_ref(int argc, char *argv[])
2579 const struct got_error *error = NULL;
2580 struct got_repository *repo = NULL;
2581 struct got_worktree *worktree = NULL;
2582 char *cwd = NULL, *repo_path = NULL;
2583 int ch, do_list = 0;
2584 const char *delref = NULL;
2586 /* TODO: Add -s option for adding symbolic references. */
2587 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2593 repo_path = realpath(optarg, NULL);
2594 if (repo_path == NULL)
2595 err(1, "-r option");
2596 got_path_strip_trailing_slashes(repo_path);
2607 if (do_list && delref)
2608 errx(1, "-l and -d options are mutually exclusive\n");
2613 if (do_list || delref) {
2616 } else if (argc != 2)
2621 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2625 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2626 "sendfd unveil", NULL) == -1)
2630 cwd = getcwd(NULL, 0);
2632 error = got_error_from_errno("getcwd");
2636 if (repo_path == NULL) {
2637 error = got_worktree_open(&worktree, cwd);
2638 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2644 strdup(got_worktree_get_repo_path(worktree));
2645 if (repo_path == NULL)
2646 error = got_error_from_errno("strdup");
2650 repo_path = strdup(cwd);
2651 if (repo_path == NULL) {
2652 error = got_error_from_errno("strdup");
2658 error = got_repo_open(&repo, repo_path);
2662 error = apply_unveil(got_repo_get_path(repo), do_list,
2663 worktree ? got_worktree_get_root_path(worktree) : NULL);
2668 error = list_refs(repo);
2670 error = delete_ref(repo, delref);
2672 error = add_ref(repo, argv[0], argv[1]);
2675 got_repo_close(repo);
2677 got_worktree_close(worktree);
2687 "usage: %s branch [-r repository] -l | -d name | "
2688 "name [base-branch]\n", getprogname());
2692 static const struct got_error *
2693 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2695 static const struct got_error *err = NULL;
2696 struct got_reflist_head refs;
2697 struct got_reflist_entry *re;
2699 SIMPLEQ_INIT(&refs);
2701 err = got_ref_list(&refs, repo);
2705 SIMPLEQ_FOREACH(re, &refs, entry) {
2706 const char *refname, *marker = " ";
2708 refname = got_ref_get_name(re->ref);
2709 if (strncmp(refname, "refs/heads/", 11) != 0)
2711 if (worktree && strcmp(refname,
2712 got_worktree_get_head_ref_name(worktree)) == 0) {
2713 struct got_object_id *id = NULL;
2714 err = got_ref_resolve(&id, repo, re->ref);
2717 if (got_object_id_cmp(id,
2718 got_worktree_get_base_commit_id(worktree)) == 0)
2725 refstr = got_ref_to_str(re->ref);
2727 return got_error_from_errno("got_ref_to_str");
2728 printf("%s%s: %s\n", marker, refname, refstr);
2732 got_ref_list_free(&refs);
2736 static const struct got_error *
2737 delete_branch(struct got_repository *repo, const char *branch_name)
2739 const struct got_error *err = NULL;
2740 struct got_reference *ref;
2743 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2744 return got_error_from_errno("asprintf");
2746 err = got_ref_open(&ref, repo, refname, 0);
2750 err = got_ref_delete(ref, repo);
2757 static const struct got_error *
2758 add_branch(struct got_repository *repo, const char *branch_name,
2759 const char *base_branch)
2761 const struct got_error *err = NULL;
2762 struct got_object_id *id = NULL;
2763 struct got_reference *ref = NULL;
2764 char *base_refname = NULL, *refname = NULL;
2765 struct got_reference *base_ref;
2768 * Don't let the user create a branch named '-'.
2769 * While technically a valid reference name, this case is usually
2770 * an unintended typo.
2772 if (branch_name[0] == '-' && branch_name[1] == '\0')
2773 return got_error(GOT_ERR_BAD_REF_NAME);
2775 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2776 base_refname = strdup(GOT_REF_HEAD);
2777 if (base_refname == NULL)
2778 return got_error_from_errno("strdup");
2779 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2780 return got_error_from_errno("asprintf");
2782 err = got_ref_open(&base_ref, repo, base_refname, 0);
2785 err = got_ref_resolve(&id, repo, base_ref);
2786 got_ref_close(base_ref);
2790 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2791 err = got_error_from_errno("asprintf");
2795 err = got_ref_open(&ref, repo, refname, 0);
2797 err = got_error(GOT_ERR_BRANCH_EXISTS);
2799 } else if (err->code != GOT_ERR_NOT_REF)
2802 err = got_ref_alloc(&ref, refname, id);
2806 err = got_ref_write(ref, repo);
2816 static const struct got_error *
2817 cmd_branch(int argc, char *argv[])
2819 const struct got_error *error = NULL;
2820 struct got_repository *repo = NULL;
2821 struct got_worktree *worktree = NULL;
2822 char *cwd = NULL, *repo_path = NULL;
2823 int ch, do_list = 0;
2824 const char *delref = NULL;
2826 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2832 repo_path = realpath(optarg, NULL);
2833 if (repo_path == NULL)
2834 err(1, "-r option");
2835 got_path_strip_trailing_slashes(repo_path);
2846 if (do_list && delref)
2847 errx(1, "-l and -d options are mutually exclusive\n");
2852 if (do_list || delref) {
2855 } else if (argc < 1 || argc > 2)
2860 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2864 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2865 "sendfd unveil", NULL) == -1)
2869 cwd = getcwd(NULL, 0);
2871 error = got_error_from_errno("getcwd");
2875 if (repo_path == NULL) {
2876 error = got_worktree_open(&worktree, cwd);
2877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2883 strdup(got_worktree_get_repo_path(worktree));
2884 if (repo_path == NULL)
2885 error = got_error_from_errno("strdup");
2889 repo_path = strdup(cwd);
2890 if (repo_path == NULL) {
2891 error = got_error_from_errno("strdup");
2897 error = got_repo_open(&repo, repo_path);
2901 error = apply_unveil(got_repo_get_path(repo), do_list,
2902 worktree ? got_worktree_get_root_path(worktree) : NULL);
2907 error = list_branches(repo, worktree);
2909 error = delete_branch(repo, delref);
2911 const char *base_branch;
2913 base_branch = worktree ?
2914 got_worktree_get_head_ref_name(worktree) :
2916 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2919 base_branch = argv[1];
2920 error = add_branch(repo, argv[0], base_branch);
2924 got_repo_close(repo);
2926 got_worktree_close(worktree);
2935 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2939 static const struct got_error *
2940 cmd_add(int argc, char *argv[])
2942 const struct got_error *error = NULL;
2943 struct got_repository *repo = NULL;
2944 struct got_worktree *worktree = NULL;
2946 struct got_pathlist_head paths;
2947 struct got_pathlist_entry *pe;
2952 while ((ch = getopt(argc, argv, "")) != -1) {
2964 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2971 cwd = getcwd(NULL, 0);
2973 error = got_error_from_errno("getcwd");
2977 error = got_worktree_open(&worktree, cwd);
2981 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2985 error = apply_unveil(got_repo_get_path(repo), 1,
2986 got_worktree_get_root_path(worktree));
2990 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2994 error = got_worktree_schedule_add(worktree, &paths, print_status,
2998 got_repo_close(repo);
3000 got_worktree_close(worktree);
3001 TAILQ_FOREACH(pe, &paths, entry)
3002 free((char *)pe->path);
3003 got_pathlist_free(&paths);
3011 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3015 static const struct got_error *
3016 cmd_remove(int argc, char *argv[])
3018 const struct got_error *error = NULL;
3019 struct got_worktree *worktree = NULL;
3020 struct got_repository *repo = NULL;
3022 struct got_pathlist_head paths;
3023 struct got_pathlist_entry *pe;
3024 int ch, delete_local_mods = 0;
3028 while ((ch = getopt(argc, argv, "f")) != -1) {
3031 delete_local_mods = 1;
3043 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3050 cwd = getcwd(NULL, 0);
3052 error = got_error_from_errno("getcwd");
3055 error = got_worktree_open(&worktree, cwd);
3059 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3063 error = apply_unveil(got_repo_get_path(repo), 1,
3064 got_worktree_get_root_path(worktree));
3068 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3072 error = got_worktree_schedule_delete(worktree, &paths,
3073 delete_local_mods, print_status, NULL, repo);
3078 got_repo_close(repo);
3080 got_worktree_close(worktree);
3081 TAILQ_FOREACH(pe, &paths, entry)
3082 free((char *)pe->path);
3083 got_pathlist_free(&paths);
3091 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3095 static const struct got_error *
3096 revert_progress(void *arg, unsigned char status, const char *path)
3098 while (path[0] == '/')
3100 printf("%c %s\n", status, path);
3104 static const struct got_error *
3105 cmd_revert(int argc, char *argv[])
3107 const struct got_error *error = NULL;
3108 struct got_worktree *worktree = NULL;
3109 struct got_repository *repo = NULL;
3110 char *cwd = NULL, *path = NULL;
3111 struct got_pathlist_head paths;
3116 while ((ch = getopt(argc, argv, "")) != -1) {
3128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3129 "unveil", NULL) == -1)
3135 cwd = getcwd(NULL, 0);
3137 error = got_error_from_errno("getcwd");
3140 error = got_worktree_open(&worktree, cwd);
3144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3148 error = apply_unveil(got_repo_get_path(repo), 1,
3149 got_worktree_get_root_path(worktree));
3153 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3157 error = got_worktree_revert(worktree, &paths,
3158 revert_progress, NULL, repo);
3163 got_repo_close(repo);
3165 got_worktree_close(worktree);
3174 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3179 struct collect_commit_logmsg_arg {
3180 const char *cmdline_log;
3182 const char *worktree_path;
3183 const char *branch_name;
3184 const char *repo_path;
3189 static const struct got_error *
3190 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3193 char *initial_content = NULL;
3194 struct got_pathlist_entry *pe;
3195 const struct got_error *err = NULL;
3196 char *template = NULL;
3197 struct collect_commit_logmsg_arg *a = arg;
3201 /* if a message was specified on the command line, just use it */
3202 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3203 len = strlen(a->cmdline_log) + 1;
3204 *logmsg = malloc(len + 1);
3205 if (*logmsg == NULL)
3206 return got_error_from_errno("malloc");
3207 strlcpy(*logmsg, a->cmdline_log, len);
3211 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3212 return got_error_from_errno("asprintf");
3214 if (asprintf(&initial_content,
3215 "\n# changes to be committed on branch %s:\n",
3216 a->branch_name) == -1)
3217 return got_error_from_errno("asprintf");
3219 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3223 dprintf(fd, initial_content);
3225 TAILQ_FOREACH(pe, commitable_paths, entry) {
3226 struct got_commitable *ct = pe->data;
3227 dprintf(fd, "# %c %s\n",
3228 got_commitable_get_status(ct),
3229 got_commitable_get_path(ct));
3233 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3235 unlink(a->logmsg_path);
3236 free(a->logmsg_path);
3237 free(initial_content);
3240 /* Editor is done; we can now apply unveil(2) */
3242 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3251 static const struct got_error *
3252 cmd_commit(int argc, char *argv[])
3254 const struct got_error *error = NULL;
3255 struct got_worktree *worktree = NULL;
3256 struct got_repository *repo = NULL;
3257 char *cwd = NULL, *id_str = NULL;
3258 struct got_object_id *id = NULL;
3259 const char *logmsg = NULL;
3260 const char *got_author = getenv("GOT_AUTHOR");
3261 struct collect_commit_logmsg_arg cl_arg;
3262 char *editor = NULL;
3263 int ch, rebase_in_progress, histedit_in_progress;
3264 struct got_pathlist_head paths;
3268 while ((ch = getopt(argc, argv, "m:")) != -1) {
3283 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3284 "unveil", NULL) == -1)
3287 if (got_author == NULL) {
3288 /* TODO: Look current user up in password database */
3289 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3293 cwd = getcwd(NULL, 0);
3295 error = got_error_from_errno("getcwd");
3298 error = got_worktree_open(&worktree, cwd);
3302 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3305 if (rebase_in_progress) {
3306 error = got_error(GOT_ERR_REBASING);
3310 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3320 * unveil(2) traverses exec(2); if an editor is used we have
3321 * to apply unveil after the log message has been written.
3323 if (logmsg == NULL || strlen(logmsg) == 0)
3324 error = get_editor(&editor);
3326 error = apply_unveil(got_repo_get_path(repo), 0,
3327 got_worktree_get_root_path(worktree));
3331 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3335 cl_arg.editor = editor;
3336 cl_arg.cmdline_log = logmsg;
3337 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3338 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3339 if (!histedit_in_progress) {
3340 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3341 error = got_error(GOT_ERR_COMMIT_BRANCH);
3344 cl_arg.branch_name += 11;
3346 cl_arg.repo_path = got_repo_get_path(repo);
3347 cl_arg.logmsg_path = NULL;
3348 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3349 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3351 if (cl_arg.logmsg_path)
3352 fprintf(stderr, "%s: log message preserved in %s\n",
3353 getprogname(), cl_arg.logmsg_path);
3357 if (cl_arg.logmsg_path)
3358 unlink(cl_arg.logmsg_path);
3360 error = got_object_id_str(&id_str, id);
3363 printf("Created commit %s\n", id_str);
3366 got_repo_close(repo);
3368 got_worktree_close(worktree);
3376 usage_cherrypick(void)
3378 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3382 static const struct got_error *
3383 cmd_cherrypick(int argc, char *argv[])
3385 const struct got_error *error = NULL;
3386 struct got_worktree *worktree = NULL;
3387 struct got_repository *repo = NULL;
3388 char *cwd = NULL, *commit_id_str = NULL;
3389 struct got_object_id *commit_id = NULL;
3390 struct got_commit_object *commit = NULL;
3391 struct got_object_qid *pid;
3392 struct got_reference *head_ref = NULL;
3393 int ch, did_something = 0;
3395 while ((ch = getopt(argc, argv, "")) != -1) {
3407 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3408 "unveil", NULL) == -1)
3414 cwd = getcwd(NULL, 0);
3416 error = got_error_from_errno("getcwd");
3419 error = got_worktree_open(&worktree, cwd);
3423 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3427 error = apply_unveil(got_repo_get_path(repo), 0,
3428 got_worktree_get_root_path(worktree));
3432 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3433 GOT_OBJ_TYPE_COMMIT, repo);
3434 if (error != NULL) {
3435 struct got_reference *ref;
3436 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3438 error = got_ref_open(&ref, repo, argv[0], 0);
3441 error = got_ref_resolve(&commit_id, repo, ref);
3446 error = got_object_id_str(&commit_id_str, commit_id);
3450 error = got_ref_open(&head_ref, repo,
3451 got_worktree_get_head_ref_name(worktree), 0);
3455 error = check_same_branch(commit_id, head_ref, NULL, repo);
3457 if (error->code != GOT_ERR_ANCESTRY)
3461 error = got_error(GOT_ERR_SAME_BRANCH);
3465 error = got_object_open_as_commit(&commit, repo, commit_id);
3468 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3469 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3470 commit_id, repo, update_progress, &did_something, check_cancelled,
3476 printf("Merged commit %s\n", commit_id_str);
3479 got_object_commit_close(commit);
3480 free(commit_id_str);
3482 got_ref_close(head_ref);
3484 got_worktree_close(worktree);
3486 got_repo_close(repo);
3493 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3497 static const struct got_error *
3498 cmd_backout(int argc, char *argv[])
3500 const struct got_error *error = NULL;
3501 struct got_worktree *worktree = NULL;
3502 struct got_repository *repo = NULL;
3503 char *cwd = NULL, *commit_id_str = NULL;
3504 struct got_object_id *commit_id = NULL;
3505 struct got_commit_object *commit = NULL;
3506 struct got_object_qid *pid;
3507 struct got_reference *head_ref = NULL;
3508 int ch, did_something = 0;
3510 while ((ch = getopt(argc, argv, "")) != -1) {
3522 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3523 "unveil", NULL) == -1)
3529 cwd = getcwd(NULL, 0);
3531 error = got_error_from_errno("getcwd");
3534 error = got_worktree_open(&worktree, cwd);
3538 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3542 error = apply_unveil(got_repo_get_path(repo), 0,
3543 got_worktree_get_root_path(worktree));
3547 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3548 GOT_OBJ_TYPE_COMMIT, repo);
3549 if (error != NULL) {
3550 struct got_reference *ref;
3551 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3553 error = got_ref_open(&ref, repo, argv[0], 0);
3556 error = got_ref_resolve(&commit_id, repo, ref);
3561 error = got_object_id_str(&commit_id_str, commit_id);
3565 error = got_ref_open(&head_ref, repo,
3566 got_worktree_get_head_ref_name(worktree), 0);
3570 error = check_same_branch(commit_id, head_ref, NULL, repo);
3574 error = got_object_open_as_commit(&commit, repo, commit_id);
3577 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3579 error = got_error(GOT_ERR_ROOT_COMMIT);
3583 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3584 update_progress, &did_something, check_cancelled, NULL);
3589 printf("Backed out commit %s\n", commit_id_str);
3592 got_object_commit_close(commit);
3593 free(commit_id_str);
3595 got_ref_close(head_ref);
3597 got_worktree_close(worktree);
3599 got_repo_close(repo);
3606 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3612 trim_logmsg(char *logmsg, int limit)
3617 len = strlen(logmsg);
3621 nl = strchr(logmsg, '\n');
3626 static const struct got_error *
3627 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3629 const char *logmsg0 = NULL;
3631 logmsg0 = got_object_commit_get_logmsg(commit);
3633 while (isspace((unsigned char)logmsg0[0]))
3636 *logmsg = strdup(logmsg0);
3637 if (*logmsg == NULL)
3638 return got_error_from_errno("strdup");
3640 trim_logmsg(*logmsg, limit);
3644 static const struct got_error *
3645 show_rebase_progress(struct got_commit_object *commit,
3646 struct got_object_id *old_id, struct got_object_id *new_id)
3648 const struct got_error *err;
3649 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3651 err = got_object_id_str(&old_id_str, old_id);
3656 err = got_object_id_str(&new_id_str, new_id);
3661 old_id_str[12] = '\0';
3663 new_id_str[12] = '\0';
3665 err = get_short_logmsg(&logmsg, 42, commit);
3669 printf("%s -> %s: %s\n", old_id_str,
3670 new_id_str ? new_id_str : "no-op change", logmsg);
3677 static const struct got_error *
3678 rebase_progress(void *arg, unsigned char status, const char *path)
3680 unsigned char *rebase_status = arg;
3682 while (path[0] == '/')
3684 printf("%c %s\n", status, path);
3686 if (*rebase_status == GOT_STATUS_CONFLICT)
3688 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3689 *rebase_status = status;
3693 static const struct got_error *
3694 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3695 struct got_reference *branch, struct got_reference *new_base_branch,
3696 struct got_reference *tmp_branch, struct got_repository *repo)
3698 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3699 return got_worktree_rebase_complete(worktree, fileindex,
3700 new_base_branch, tmp_branch, branch, repo);
3703 static const struct got_error *
3704 rebase_commit(struct got_pathlist_head *merged_paths,
3705 struct got_worktree *worktree, struct got_fileindex *fileindex,
3706 struct got_reference *tmp_branch,
3707 struct got_object_id *commit_id, struct got_repository *repo)
3709 const struct got_error *error;
3710 struct got_commit_object *commit;
3711 struct got_object_id *new_commit_id;
3713 error = got_object_open_as_commit(&commit, repo, commit_id);
3717 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3718 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3720 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3722 error = show_rebase_progress(commit, commit_id, NULL);
3724 error = show_rebase_progress(commit, commit_id, new_commit_id);
3725 free(new_commit_id);
3728 got_object_commit_close(commit);
3732 struct check_path_prefix_arg {
3733 const char *path_prefix;
3738 static const struct got_error *
3739 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3740 struct got_blob_object *blob2, struct got_object_id *id1,
3741 struct got_object_id *id2, const char *path1, const char *path2,
3742 struct got_repository *repo)
3744 struct check_path_prefix_arg *a = arg;
3746 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3747 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3748 return got_error(a->errcode);
3753 static const struct got_error *
3754 check_path_prefix(struct got_object_id *parent_id,
3755 struct got_object_id *commit_id, const char *path_prefix,
3756 int errcode, struct got_repository *repo)
3758 const struct got_error *err;
3759 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3760 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3761 struct check_path_prefix_arg cpp_arg;
3763 if (got_path_is_root_dir(path_prefix))
3766 err = got_object_open_as_commit(&commit, repo, commit_id);
3770 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3774 err = got_object_open_as_tree(&tree1, repo,
3775 got_object_commit_get_tree_id(parent_commit));
3779 err = got_object_open_as_tree(&tree2, repo,
3780 got_object_commit_get_tree_id(commit));
3784 cpp_arg.path_prefix = path_prefix;
3785 while (cpp_arg.path_prefix[0] == '/')
3786 cpp_arg.path_prefix++;
3787 cpp_arg.len = strlen(cpp_arg.path_prefix);
3788 cpp_arg.errcode = errcode;
3789 err = got_diff_tree(tree1, tree2, "", "", repo,
3790 check_path_prefix_in_diff, &cpp_arg, 0);
3793 got_object_tree_close(tree1);
3795 got_object_tree_close(tree2);
3797 got_object_commit_close(commit);
3799 got_object_commit_close(parent_commit);
3803 static const struct got_error *
3804 collect_commits(struct got_object_id_queue *commits,
3805 struct got_object_id *initial_commit_id,
3806 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3807 const char *path_prefix, int path_prefix_errcode,
3808 struct got_repository *repo)
3810 const struct got_error *err = NULL;
3811 struct got_commit_graph *graph = NULL;
3812 struct got_object_id *parent_id = NULL;
3813 struct got_object_qid *qid;
3814 struct got_object_id *commit_id = initial_commit_id;
3816 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3820 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3823 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3824 err = got_commit_graph_iter_next(&parent_id, graph);
3826 if (err->code == GOT_ERR_ITER_COMPLETED) {
3827 err = got_error_msg(GOT_ERR_ANCESTRY,
3828 "ran out of commits to rebase before "
3829 "youngest common ancestor commit has "
3832 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3834 err = got_commit_graph_fetch_commits(graph, 1, repo);
3838 err = check_path_prefix(parent_id, commit_id,
3839 path_prefix, path_prefix_errcode, repo);
3843 err = got_object_qid_alloc(&qid, commit_id);
3846 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3847 commit_id = parent_id;
3851 got_commit_graph_close(graph);
3855 static const struct got_error *
3856 cmd_rebase(int argc, char *argv[])
3858 const struct got_error *error = NULL;
3859 struct got_worktree *worktree = NULL;
3860 struct got_repository *repo = NULL;
3861 struct got_fileindex *fileindex = NULL;
3863 struct got_reference *branch = NULL;
3864 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3865 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3866 struct got_object_id *resume_commit_id = NULL;
3867 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3868 struct got_commit_object *commit = NULL;
3869 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3870 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3871 struct got_object_id_queue commits;
3872 struct got_pathlist_head merged_paths;
3873 const struct got_object_id_queue *parent_ids;
3874 struct got_object_qid *qid, *pid;
3876 SIMPLEQ_INIT(&commits);
3877 TAILQ_INIT(&merged_paths);
3879 while ((ch = getopt(argc, argv, "ac")) != -1) {
3885 continue_rebase = 1;
3897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3898 "unveil", NULL) == -1)
3901 if (abort_rebase && continue_rebase)
3903 else if (abort_rebase || continue_rebase) {
3906 } else if (argc != 1)
3909 cwd = getcwd(NULL, 0);
3911 error = got_error_from_errno("getcwd");
3914 error = got_worktree_open(&worktree, cwd);
3918 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3922 error = apply_unveil(got_repo_get_path(repo), 0,
3923 got_worktree_get_root_path(worktree));
3927 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3933 if (!rebase_in_progress) {
3934 error = got_error(GOT_ERR_NOT_REBASING);
3937 error = got_worktree_rebase_continue(&resume_commit_id,
3938 &new_base_branch, &tmp_branch, &branch, &fileindex,
3942 printf("Switching work tree to %s\n",
3943 got_ref_get_symref_target(new_base_branch));
3944 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3945 new_base_branch, update_progress, &did_something);
3948 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3949 goto done; /* nothing else to do */
3952 if (continue_rebase) {
3953 if (!rebase_in_progress) {
3954 error = got_error(GOT_ERR_NOT_REBASING);
3957 error = got_worktree_rebase_continue(&resume_commit_id,
3958 &new_base_branch, &tmp_branch, &branch, &fileindex,
3963 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3964 resume_commit_id, repo);
3968 yca_id = got_object_id_dup(resume_commit_id);
3969 if (yca_id == NULL) {
3970 error = got_error_from_errno("got_object_id_dup");
3974 error = got_ref_open(&branch, repo, argv[0], 0);
3979 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3983 if (!continue_rebase) {
3984 struct got_object_id *base_commit_id;
3986 base_commit_id = got_worktree_get_base_commit_id(worktree);
3987 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3988 base_commit_id, branch_head_commit_id, repo);
3991 if (yca_id == NULL) {
3992 error = got_error_msg(GOT_ERR_ANCESTRY,
3993 "specified branch shares no common ancestry "
3994 "with work tree's branch");
3998 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4000 if (error->code != GOT_ERR_ANCESTRY)
4004 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4005 "specified branch resolves to a commit which "
4006 "is already contained in work tree's branch");
4009 error = got_worktree_rebase_prepare(&new_base_branch,
4010 &tmp_branch, &fileindex, worktree, branch, repo);
4015 commit_id = branch_head_commit_id;
4016 error = got_object_open_as_commit(&commit, repo, commit_id);
4020 parent_ids = got_object_commit_get_parent_ids(commit);
4021 pid = SIMPLEQ_FIRST(parent_ids);
4022 error = collect_commits(&commits, commit_id, pid->id,
4023 yca_id, got_worktree_get_path_prefix(worktree),
4024 GOT_ERR_REBASE_PATH, repo);
4025 got_object_commit_close(commit);
4030 if (SIMPLEQ_EMPTY(&commits)) {
4031 if (continue_rebase)
4032 error = rebase_complete(worktree, fileindex,
4033 branch, new_base_branch, tmp_branch, repo);
4035 error = got_error(GOT_ERR_EMPTY_REBASE);
4040 SIMPLEQ_FOREACH(qid, &commits, entry) {
4041 commit_id = qid->id;
4042 parent_id = pid ? pid->id : yca_id;
4045 error = got_worktree_rebase_merge_files(&merged_paths,
4046 worktree, fileindex, parent_id, commit_id, repo,
4047 rebase_progress, &rebase_status, check_cancelled, NULL);
4051 if (rebase_status == GOT_STATUS_CONFLICT) {
4052 got_worktree_rebase_pathlist_free(&merged_paths);
4056 error = rebase_commit(&merged_paths, worktree, fileindex,
4057 tmp_branch, commit_id, repo);
4058 got_worktree_rebase_pathlist_free(&merged_paths);
4063 if (rebase_status == GOT_STATUS_CONFLICT) {
4064 error = got_worktree_rebase_postpone(worktree, fileindex);
4067 error = got_error_msg(GOT_ERR_CONFLICTS,
4068 "conflicts must be resolved before rebasing can continue");
4070 error = rebase_complete(worktree, fileindex, branch,
4071 new_base_branch, tmp_branch, repo);
4073 got_object_id_queue_free(&commits);
4074 free(branch_head_commit_id);
4075 free(resume_commit_id);
4078 got_object_commit_close(commit);
4080 got_ref_close(branch);
4081 if (new_base_branch)
4082 got_ref_close(new_base_branch);
4084 got_ref_close(tmp_branch);
4086 got_worktree_close(worktree);
4088 got_repo_close(repo);
4093 usage_histedit(void)
4095 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4100 #define GOT_HISTEDIT_PICK 'p'
4101 #define GOT_HISTEDIT_EDIT 'e'
4102 #define GOT_HISTEDIT_FOLD 'f'
4103 #define GOT_HISTEDIT_DROP 'd'
4104 #define GOT_HISTEDIT_MESG 'm'
4106 static struct got_histedit_cmd {
4110 } got_histedit_cmds[] = {
4111 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4112 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4113 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4114 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4115 { GOT_HISTEDIT_MESG, "mesg",
4116 "single-line log message for commit above (open editor if empty)" },
4119 struct got_histedit_list_entry {
4120 TAILQ_ENTRY(got_histedit_list_entry) entry;
4121 struct got_object_id *commit_id;
4122 const struct got_histedit_cmd *cmd;
4125 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4127 static const struct got_error *
4128 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4129 FILE *f, struct got_repository *repo)
4131 const struct got_error *err = NULL;
4132 char *logmsg = NULL, *id_str = NULL;
4133 struct got_commit_object *commit = NULL;
4136 err = got_object_open_as_commit(&commit, repo, commit_id);
4140 err = get_short_logmsg(&logmsg, 34, commit);
4144 err = got_object_id_str(&id_str, commit_id);
4148 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4150 err = got_ferror(f, GOT_ERR_IO);
4153 got_object_commit_close(commit);
4159 static const struct got_error *
4160 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4161 struct got_repository *repo)
4163 const struct got_error *err = NULL;
4164 struct got_object_qid *qid;
4166 if (SIMPLEQ_EMPTY(commits))
4167 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4169 SIMPLEQ_FOREACH(qid, commits, entry) {
4170 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4179 static const struct got_error *
4180 write_cmd_list(FILE *f)
4182 const struct got_error *err = NULL;
4185 n = fprintf(f, "# Available histedit commands:\n");
4187 return got_ferror(f, GOT_ERR_IO);
4189 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4190 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4191 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4194 err = got_ferror(f, GOT_ERR_IO);
4198 n = fprintf(f, "# Commits will be processed in order from top to "
4199 "bottom of this file.\n");
4201 return got_ferror(f, GOT_ERR_IO);
4205 static const struct got_error *
4206 histedit_syntax_error(int lineno)
4208 static char msg[42];
4211 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4213 if (ret == -1 || ret >= sizeof(msg))
4214 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4216 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4219 static const struct got_error *
4220 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4221 char *logmsg, struct got_repository *repo)
4223 const struct got_error *err;
4224 struct got_commit_object *folded_commit = NULL;
4227 err = got_object_id_str(&id_str, hle->commit_id);
4231 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4235 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4236 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4237 got_object_commit_get_logmsg(folded_commit)) == -1) {
4238 err = got_error_from_errno("asprintf");
4243 got_object_commit_close(folded_commit);
4248 static struct got_histedit_list_entry *
4249 get_folded_commits(struct got_histedit_list_entry *hle)
4251 struct got_histedit_list_entry *prev, *folded = NULL;
4253 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4254 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4255 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4256 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4258 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4264 static const struct got_error *
4265 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4266 struct got_repository *repo)
4268 char *logmsg_path = NULL, *id_str = NULL;
4269 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4270 const struct got_error *err = NULL;
4271 struct got_commit_object *commit = NULL;
4273 struct got_histedit_list_entry *folded = NULL;
4275 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4279 folded = get_folded_commits(hle);
4281 while (folded != hle) {
4282 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4283 folded = TAILQ_NEXT(folded, entry);
4286 err = append_folded_commit_msg(&new_msg, folded,
4292 folded = TAILQ_NEXT(folded, entry);
4296 err = got_object_id_str(&id_str, hle->commit_id);
4299 if (asprintf(&new_msg,
4300 "%s\n# original log message of commit %s: %s",
4301 logmsg ? logmsg : "", id_str,
4302 got_object_commit_get_logmsg(commit)) == -1) {
4303 err = got_error_from_errno("asprintf");
4309 err = got_object_id_str(&id_str, hle->commit_id);
4313 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4317 dprintf(fd, logmsg);
4320 err = get_editor(&editor);
4324 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4326 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4329 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4330 if (hle->logmsg == NULL)
4331 err = got_error_from_errno("strdup");
4334 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4335 err = got_error_from_errno2("unlink", logmsg_path);
4340 got_object_commit_close(commit);
4344 static const struct got_error *
4345 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4346 FILE *f, struct got_repository *repo)
4348 const struct got_error *err = NULL;
4349 char *line = NULL, *p, *end;
4353 const struct got_histedit_cmd *cmd;
4354 struct got_object_id *commit_id = NULL;
4355 struct got_histedit_list_entry *hle = NULL;
4358 len = getline(&line, &size, f);
4360 const struct got_error *getline_err;
4363 getline_err = got_error_from_errno("getline");
4364 err = got_ferror(f, getline_err->code);
4369 while (isspace((unsigned char)p[0]))
4371 if (p[0] == '#' || p[0] == '\0') {
4377 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4378 cmd = &got_histedit_cmds[i];
4379 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4380 isspace((unsigned char)p[strlen(cmd->name)])) {
4381 p += strlen(cmd->name);
4384 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4389 if (i == nitems(got_histedit_cmds)) {
4390 err = histedit_syntax_error(lineno);
4393 while (isspace((unsigned char)p[0]))
4395 if (cmd->code == GOT_HISTEDIT_MESG) {
4396 if (hle == NULL || hle->logmsg != NULL) {
4397 err = got_error(GOT_ERR_HISTEDIT_CMD);
4401 err = histedit_edit_logmsg(hle, repo);
4405 hle->logmsg = strdup(p);
4406 if (hle->logmsg == NULL) {
4407 err = got_error_from_errno("strdup");
4416 while (end[0] && !isspace((unsigned char)end[0]))
4420 err = got_object_resolve_id_str(&commit_id, repo, p);
4422 /* override error code */
4423 err = histedit_syntax_error(lineno);
4427 hle = malloc(sizeof(*hle));
4429 err = got_error_from_errno("malloc");
4433 hle->commit_id = commit_id;
4438 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4446 static const struct got_error *
4447 histedit_check_script(struct got_histedit_list *histedit_cmds,
4448 struct got_object_id_queue *commits, struct got_repository *repo)
4450 const struct got_error *err = NULL;
4451 struct got_object_qid *qid;
4452 struct got_histedit_list_entry *hle;
4453 static char msg[80];
4456 if (TAILQ_EMPTY(histedit_cmds))
4457 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4458 "histedit script contains no commands");
4460 SIMPLEQ_FOREACH(qid, commits, entry) {
4461 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4462 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4466 err = got_object_id_str(&id_str, qid->id);
4469 snprintf(msg, sizeof(msg),
4470 "commit %s missing from histedit script", id_str);
4472 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4476 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4477 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4478 "last commit in histedit script cannot be folded");
4483 static const struct got_error *
4484 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4485 const char *path, struct got_object_id_queue *commits,
4486 struct got_repository *repo)
4488 const struct got_error *err = NULL;
4492 err = get_editor(&editor);
4496 if (spawn_editor(editor, path) == -1) {
4497 err = got_error_from_errno("failed spawning editor");
4501 f = fopen(path, "r");
4503 err = got_error_from_errno("fopen");
4506 err = histedit_parse_list(histedit_cmds, f, repo);
4510 err = histedit_check_script(histedit_cmds, commits, repo);
4512 if (f && fclose(f) != 0 && err == NULL)
4513 err = got_error_from_errno("fclose");
4518 static const struct got_error *
4519 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4520 struct got_object_id_queue *, const char *, struct got_repository *);
4522 static const struct got_error *
4523 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4524 struct got_object_id_queue *commits, struct got_repository *repo)
4526 const struct got_error *err;
4530 err = got_opentemp_named(&path, &f, "got-histedit");
4534 err = write_cmd_list(f);
4538 err = histedit_write_commit_list(commits, f, repo);
4542 if (fclose(f) != 0) {
4543 err = got_error_from_errno("fclose");
4548 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4550 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4551 err->code != GOT_ERR_HISTEDIT_CMD)
4553 err = histedit_edit_list_retry(histedit_cmds, err,
4554 commits, path, repo);
4557 if (f && fclose(f) != 0 && err == NULL)
4558 err = got_error_from_errno("fclose");
4559 if (path && unlink(path) != 0 && err == NULL)
4560 err = got_error_from_errno2("unlink", path);
4565 static const struct got_error *
4566 histedit_save_list(struct got_histedit_list *histedit_cmds,
4567 struct got_worktree *worktree, struct got_repository *repo)
4569 const struct got_error *err = NULL;
4572 struct got_histedit_list_entry *hle;
4573 struct got_commit_object *commit = NULL;
4575 err = got_worktree_get_histedit_script_path(&path, worktree);
4579 f = fopen(path, "w");
4581 err = got_error_from_errno2("fopen", path);
4584 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4585 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4591 int n = fprintf(f, "%c %s\n",
4592 GOT_HISTEDIT_MESG, hle->logmsg);
4594 err = got_ferror(f, GOT_ERR_IO);
4600 if (f && fclose(f) != 0 && err == NULL)
4601 err = got_error_from_errno("fclose");
4604 got_object_commit_close(commit);
4609 histedit_free_list(struct got_histedit_list *histedit_cmds)
4611 struct got_histedit_list_entry *hle;
4613 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4614 TAILQ_REMOVE(histedit_cmds, hle, entry);
4619 static const struct got_error *
4620 histedit_load_list(struct got_histedit_list *histedit_cmds,
4621 const char *path, struct got_repository *repo)
4623 const struct got_error *err = NULL;
4626 f = fopen(path, "r");
4628 err = got_error_from_errno2("fopen", path);
4632 err = histedit_parse_list(histedit_cmds, f, repo);
4634 if (f && fclose(f) != 0 && err == NULL)
4635 err = got_error_from_errno("fclose");
4639 static const struct got_error *
4640 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4641 const struct got_error *edit_err, struct got_object_id_queue *commits,
4642 const char *path, struct got_repository *repo)
4644 const struct got_error *err = NULL, *prev_err = edit_err;
4647 while (resp != 'c' && resp != 'r' && resp != 'a') {
4648 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4649 "or (a)bort: ", getprogname(), prev_err->msg);
4652 histedit_free_list(histedit_cmds);
4653 err = histedit_run_editor(histedit_cmds, path, commits,
4656 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4657 err->code != GOT_ERR_HISTEDIT_CMD)
4664 } else if (resp == 'r') {
4665 histedit_free_list(histedit_cmds);
4666 err = histedit_edit_script(histedit_cmds,
4669 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4670 err->code != GOT_ERR_HISTEDIT_CMD)
4677 } else if (resp == 'a') {
4678 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4681 printf("invalid response '%c'\n", resp);
4687 static const struct got_error *
4688 histedit_complete(struct got_worktree *worktree,
4689 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4690 struct got_reference *branch, struct got_repository *repo)
4692 printf("Switching work tree to %s\n",
4693 got_ref_get_symref_target(branch));
4694 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4698 static const struct got_error *
4699 show_histedit_progress(struct got_commit_object *commit,
4700 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4702 const struct got_error *err;
4703 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4705 err = got_object_id_str(&old_id_str, hle->commit_id);
4710 err = got_object_id_str(&new_id_str, new_id);
4715 old_id_str[12] = '\0';
4717 new_id_str[12] = '\0';
4720 logmsg = strdup(hle->logmsg);
4721 if (logmsg == NULL) {
4722 err = got_error_from_errno("strdup");
4725 trim_logmsg(logmsg, 42);
4727 err = get_short_logmsg(&logmsg, 42, commit);
4732 switch (hle->cmd->code) {
4733 case GOT_HISTEDIT_PICK:
4734 case GOT_HISTEDIT_EDIT:
4735 printf("%s -> %s: %s\n", old_id_str,
4736 new_id_str ? new_id_str : "no-op change", logmsg);
4738 case GOT_HISTEDIT_DROP:
4739 case GOT_HISTEDIT_FOLD:
4740 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4753 static const struct got_error *
4754 histedit_commit(struct got_pathlist_head *merged_paths,
4755 struct got_worktree *worktree, struct got_fileindex *fileindex,
4756 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4757 struct got_repository *repo)
4759 const struct got_error *err;
4760 struct got_commit_object *commit;
4761 struct got_object_id *new_commit_id;
4763 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4764 && hle->logmsg == NULL) {
4765 err = histedit_edit_logmsg(hle, repo);
4770 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4774 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4775 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4778 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4780 err = show_histedit_progress(commit, hle, NULL);
4782 err = show_histedit_progress(commit, hle, new_commit_id);
4783 free(new_commit_id);
4786 got_object_commit_close(commit);
4790 static const struct got_error *
4791 histedit_skip_commit(struct got_histedit_list_entry *hle,
4792 struct got_worktree *worktree, struct got_repository *repo)
4794 const struct got_error *error;
4795 struct got_commit_object *commit;
4797 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4802 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4806 error = show_histedit_progress(commit, hle, NULL);
4807 got_object_commit_close(commit);
4811 static const struct got_error *
4812 cmd_histedit(int argc, char *argv[])
4814 const struct got_error *error = NULL;
4815 struct got_worktree *worktree = NULL;
4816 struct got_fileindex *fileindex = NULL;
4817 struct got_repository *repo = NULL;
4819 struct got_reference *branch = NULL;
4820 struct got_reference *tmp_branch = NULL;
4821 struct got_object_id *resume_commit_id = NULL;
4822 struct got_object_id *base_commit_id = NULL;
4823 struct got_object_id *head_commit_id = NULL;
4824 struct got_commit_object *commit = NULL;
4825 int ch, rebase_in_progress = 0, did_something;
4826 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4827 const char *edit_script_path = NULL;
4828 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4829 struct got_object_id_queue commits;
4830 struct got_pathlist_head merged_paths;
4831 const struct got_object_id_queue *parent_ids;
4832 struct got_object_qid *pid;
4833 struct got_histedit_list histedit_cmds;
4834 struct got_histedit_list_entry *hle;
4836 SIMPLEQ_INIT(&commits);
4837 TAILQ_INIT(&histedit_cmds);
4838 TAILQ_INIT(&merged_paths);
4840 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4849 edit_script_path = optarg;
4861 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4862 "unveil", NULL) == -1)
4865 if (abort_edit && continue_edit)
4871 * This command cannot apply unveil(2) in all cases because the
4872 * user may choose to run an editor to edit the histedit script
4873 * and to edit individual commit log messages.
4874 * unveil(2) traverses exec(2); if an editor is used we have to
4875 * apply unveil after edit script and log messages have been written.
4876 * XXX TODO: Make use of unveil(2) where possible.
4879 cwd = getcwd(NULL, 0);
4881 error = got_error_from_errno("getcwd");
4884 error = got_worktree_open(&worktree, cwd);
4888 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4892 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4895 if (rebase_in_progress) {
4896 error = got_error(GOT_ERR_REBASING);
4900 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4904 if (edit_in_progress && abort_edit) {
4905 error = got_worktree_histedit_continue(&resume_commit_id,
4906 &tmp_branch, &branch, &base_commit_id, &fileindex,
4910 printf("Switching work tree to %s\n",
4911 got_ref_get_symref_target(branch));
4912 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4913 branch, base_commit_id, update_progress, &did_something);
4916 printf("Histedit of %s aborted\n",
4917 got_ref_get_symref_target(branch));
4918 goto done; /* nothing else to do */
4919 } else if (abort_edit) {
4920 error = got_error(GOT_ERR_NOT_HISTEDIT);
4924 if (continue_edit) {
4927 if (!edit_in_progress) {
4928 error = got_error(GOT_ERR_NOT_HISTEDIT);
4932 error = got_worktree_get_histedit_script_path(&path, worktree);
4936 error = histedit_load_list(&histedit_cmds, path, repo);
4941 error = got_worktree_histedit_continue(&resume_commit_id,
4942 &tmp_branch, &branch, &base_commit_id, &fileindex,
4947 error = got_ref_resolve(&head_commit_id, repo, branch);
4951 error = got_object_open_as_commit(&commit, repo,
4955 parent_ids = got_object_commit_get_parent_ids(commit);
4956 pid = SIMPLEQ_FIRST(parent_ids);
4958 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4961 error = collect_commits(&commits, head_commit_id, pid->id,
4962 base_commit_id, got_worktree_get_path_prefix(worktree),
4963 GOT_ERR_HISTEDIT_PATH, repo);
4964 got_object_commit_close(commit);
4969 if (edit_in_progress) {
4970 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4974 error = got_ref_open(&branch, repo,
4975 got_worktree_get_head_ref_name(worktree), 0);
4979 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4980 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4981 "will not edit commit history of a branch outside "
4982 "the \"refs/heads/\" reference namespace");
4986 error = got_ref_resolve(&head_commit_id, repo, branch);
4987 got_ref_close(branch);
4992 error = got_object_open_as_commit(&commit, repo,
4996 parent_ids = got_object_commit_get_parent_ids(commit);
4997 pid = SIMPLEQ_FIRST(parent_ids);
4999 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5002 error = collect_commits(&commits, head_commit_id, pid->id,
5003 got_worktree_get_base_commit_id(worktree),
5004 got_worktree_get_path_prefix(worktree),
5005 GOT_ERR_HISTEDIT_PATH, repo);
5006 got_object_commit_close(commit);
5011 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5012 &base_commit_id, &fileindex, worktree, repo);
5016 if (edit_script_path) {
5017 error = histedit_load_list(&histedit_cmds,
5018 edit_script_path, repo);
5020 got_worktree_histedit_abort(worktree, fileindex,
5021 repo, branch, base_commit_id,
5022 update_progress, &did_something);
5026 error = histedit_edit_script(&histedit_cmds, &commits,
5029 got_worktree_histedit_abort(worktree, fileindex,
5030 repo, branch, base_commit_id,
5031 update_progress, &did_something);
5037 error = histedit_save_list(&histedit_cmds, worktree,
5040 got_worktree_histedit_abort(worktree, fileindex,
5041 repo, branch, base_commit_id,
5042 update_progress, &did_something);
5048 error = histedit_check_script(&histedit_cmds, &commits, repo);
5052 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5053 if (resume_commit_id) {
5054 if (got_object_id_cmp(hle->commit_id,
5055 resume_commit_id) != 0)
5058 resume_commit_id = NULL;
5059 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5060 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5061 error = histedit_skip_commit(hle, worktree,
5064 error = histedit_commit(NULL, worktree,
5065 fileindex, tmp_branch, hle, repo);
5072 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5073 error = histedit_skip_commit(hle, worktree, repo);
5079 error = got_object_open_as_commit(&commit, repo,
5083 parent_ids = got_object_commit_get_parent_ids(commit);
5084 pid = SIMPLEQ_FIRST(parent_ids);
5086 error = got_worktree_histedit_merge_files(&merged_paths,
5087 worktree, fileindex, pid->id, hle->commit_id, repo,
5088 rebase_progress, &rebase_status, check_cancelled, NULL);
5091 got_object_commit_close(commit);
5094 if (rebase_status == GOT_STATUS_CONFLICT) {
5095 got_worktree_rebase_pathlist_free(&merged_paths);
5099 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5101 error = got_object_id_str(&id_str, hle->commit_id);
5104 printf("Stopping histedit for amending commit %s\n",
5107 got_worktree_rebase_pathlist_free(&merged_paths);
5108 error = got_worktree_histedit_postpone(worktree,
5113 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5114 error = histedit_skip_commit(hle, worktree, repo);
5120 error = histedit_commit(&merged_paths, worktree, fileindex,
5121 tmp_branch, hle, repo);
5122 got_worktree_rebase_pathlist_free(&merged_paths);
5127 if (rebase_status == GOT_STATUS_CONFLICT) {
5128 error = got_worktree_histedit_postpone(worktree, fileindex);
5131 error = got_error_msg(GOT_ERR_CONFLICTS,
5132 "conflicts must be resolved before rebasing can continue");
5134 error = histedit_complete(worktree, fileindex, tmp_branch,
5137 got_object_id_queue_free(&commits);
5138 histedit_free_list(&histedit_cmds);
5139 free(head_commit_id);
5140 free(base_commit_id);
5141 free(resume_commit_id);
5143 got_object_commit_close(commit);
5145 got_ref_close(branch);
5147 got_ref_close(tmp_branch);
5149 got_worktree_close(worktree);
5151 got_repo_close(repo);
5158 fprintf(stderr, "usage: %s stage [-l] | file-path ...\n",
5163 static const struct got_error *
5164 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5165 const char *path, struct got_object_id *blob_id,
5166 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5168 const struct got_error *err = NULL;
5169 char *id_str = NULL;
5171 if (staged_status != GOT_STATUS_ADD &&
5172 staged_status != GOT_STATUS_MODIFY &&
5173 staged_status != GOT_STATUS_DELETE)
5176 if (staged_status == GOT_STATUS_ADD ||
5177 staged_status == GOT_STATUS_MODIFY)
5178 err = got_object_id_str(&id_str, staged_blob_id);
5180 err = got_object_id_str(&id_str, blob_id);
5184 printf("%s %c %s\n", id_str, staged_status, path);
5189 static const struct got_error *
5190 cmd_stage(int argc, char *argv[])
5192 const struct got_error *error = NULL;
5193 struct got_repository *repo = NULL;
5194 struct got_worktree *worktree = NULL;
5196 struct got_pathlist_head paths;
5197 struct got_pathlist_entry *pe;
5198 int ch, list_stage = 0;
5202 while ((ch = getopt(argc, argv, "l")) != -1) {
5217 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5218 "unveil", NULL) == -1)
5221 if (!list_stage && argc < 1)
5224 cwd = getcwd(NULL, 0);
5226 error = got_error_from_errno("getcwd");
5230 error = got_worktree_open(&worktree, cwd);
5234 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5238 error = apply_unveil(got_repo_get_path(repo), 1,
5239 got_worktree_get_root_path(worktree));
5243 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5248 error = got_worktree_status(worktree, &paths, repo,
5249 print_stage, NULL, check_cancelled, NULL);
5251 error = got_worktree_stage(worktree, &paths, print_status,
5255 got_repo_close(repo);
5257 got_worktree_close(worktree);
5258 TAILQ_FOREACH(pe, &paths, entry)
5259 free((char *)pe->path);
5260 got_pathlist_free(&paths);
5268 fprintf(stderr, "usage: %s unstage [file-path ...]\n",
5274 static const struct got_error *
5275 cmd_unstage(int argc, char *argv[])
5277 const struct got_error *error = NULL;
5278 struct got_repository *repo = NULL;
5279 struct got_worktree *worktree = NULL;
5281 struct got_pathlist_head paths;
5282 struct got_pathlist_entry *pe;
5283 int ch, did_something = 0;
5287 while ((ch = getopt(argc, argv, "")) != -1) {
5299 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5300 "unveil", NULL) == -1)
5303 cwd = getcwd(NULL, 0);
5305 error = got_error_from_errno("getcwd");
5309 error = got_worktree_open(&worktree, cwd);
5313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5317 error = apply_unveil(got_repo_get_path(repo), 1,
5318 got_worktree_get_root_path(worktree));
5322 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5326 error = got_worktree_unstage(worktree, &paths, update_progress,
5327 &did_something, repo);
5330 got_repo_close(repo);
5332 got_worktree_close(worktree);
5333 TAILQ_FOREACH(pe, &paths, entry)
5334 free((char *)pe->path);
5335 got_pathlist_free(&paths);