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) &&
920 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
922 if (!got_path_dir_is_empty(worktree_path)) {
923 error = got_error_path(worktree_path,
924 GOT_ERR_DIR_NOT_EMPTY);
929 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
933 error = got_ref_open(&head_ref, repo, branch_name, 0);
937 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
938 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
941 error = got_worktree_open(&worktree, worktree_path);
945 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
949 if (!same_path_prefix) {
950 error = got_error(GOT_ERR_PATH_PREFIX);
955 struct got_object_id *commit_id;
956 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
959 error = check_linear_ancestry(commit_id,
960 got_worktree_get_base_commit_id(worktree), repo);
965 error = check_same_branch(commit_id, head_ref, NULL, repo);
968 error = got_worktree_set_base_commit_id(worktree, repo,
975 error = got_pathlist_append(&paths, "", NULL);
978 error = got_worktree_checkout_files(worktree, &paths, repo,
979 checkout_progress, worktree_path, check_cancelled, NULL);
983 printf("Now shut up and hack\n");
986 got_pathlist_free(&paths);
996 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1001 static const struct got_error *
1002 update_progress(void *arg, unsigned char status, const char *path)
1004 int *did_something = arg;
1006 if (status == GOT_STATUS_EXISTS)
1011 /* Base commit bump happens silently. */
1012 if (status == GOT_STATUS_BUMP_BASE)
1015 while (path[0] == '/')
1017 printf("%c %s\n", status, path);
1021 static const struct got_error *
1022 switch_head_ref(struct got_reference *head_ref,
1023 struct got_object_id *commit_id, struct got_worktree *worktree,
1024 struct got_repository *repo)
1026 const struct got_error *err = NULL;
1028 int ref_has_moved = 0;
1030 /* Trivial case: switching between two different references. */
1031 if (strcmp(got_ref_get_name(head_ref),
1032 got_worktree_get_head_ref_name(worktree)) != 0) {
1033 printf("Switching work tree from %s to %s\n",
1034 got_worktree_get_head_ref_name(worktree),
1035 got_ref_get_name(head_ref));
1036 return got_worktree_set_head_ref(worktree, head_ref);
1039 err = check_linear_ancestry(commit_id,
1040 got_worktree_get_base_commit_id(worktree), repo);
1042 if (err->code != GOT_ERR_ANCESTRY)
1049 /* Switching to a rebased branch with the same reference name. */
1050 err = got_object_id_str(&base_id_str,
1051 got_worktree_get_base_commit_id(worktree));
1054 printf("Reference %s now points at a different branch\n",
1055 got_worktree_get_head_ref_name(worktree));
1056 printf("Switching work tree from %s to %s\n", base_id_str,
1057 got_worktree_get_head_ref_name(worktree));
1061 static const struct got_error *
1062 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1064 const struct got_error *err;
1067 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1071 return got_error(GOT_ERR_REBASING);
1073 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1077 return got_error(GOT_ERR_HISTEDIT_BUSY);
1082 static const struct got_error *
1083 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1084 char *argv[], struct got_worktree *worktree)
1086 const struct got_error *err;
1093 return got_error_from_errno("strdup");
1094 return got_pathlist_append(paths, path, NULL);
1097 for (i = 0; i < argc; i++) {
1098 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1101 err = got_pathlist_append(paths, path, NULL);
1111 static const struct got_error *
1112 cmd_update(int argc, char *argv[])
1114 const struct got_error *error = NULL;
1115 struct got_repository *repo = NULL;
1116 struct got_worktree *worktree = NULL;
1117 char *worktree_path = NULL;
1118 struct got_object_id *commit_id = NULL;
1119 char *commit_id_str = NULL;
1120 const char *branch_name = NULL;
1121 struct got_reference *head_ref = NULL;
1122 struct got_pathlist_head paths;
1123 struct got_pathlist_entry *pe;
1124 int ch, did_something = 0;
1128 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1131 branch_name = optarg;
1134 commit_id_str = strdup(optarg);
1135 if (commit_id_str == NULL)
1136 return got_error_from_errno("strdup");
1148 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1149 "unveil", NULL) == -1)
1152 worktree_path = getcwd(NULL, 0);
1153 if (worktree_path == NULL) {
1154 error = got_error_from_errno("getcwd");
1157 error = got_worktree_open(&worktree, worktree_path);
1161 error = check_rebase_or_histedit_in_progress(worktree);
1165 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1169 error = apply_unveil(got_repo_get_path(repo), 0,
1170 got_worktree_get_root_path(worktree));
1174 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1178 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1179 got_worktree_get_head_ref_name(worktree), 0);
1182 if (commit_id_str == NULL) {
1183 error = got_ref_resolve(&commit_id, repo, head_ref);
1186 error = got_object_id_str(&commit_id_str, commit_id);
1190 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1191 free(commit_id_str);
1192 commit_id_str = NULL;
1195 error = got_object_id_str(&commit_id_str, commit_id);
1201 struct got_object_id *head_commit_id;
1202 TAILQ_FOREACH(pe, &paths, entry) {
1203 if (pe->path_len == 0)
1205 error = got_error_msg(GOT_ERR_BAD_PATH,
1206 "switching between branches requires that "
1207 "the entire work tree gets updated");
1210 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1213 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1214 free(head_commit_id);
1217 error = check_same_branch(commit_id, head_ref, NULL, repo);
1220 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1224 error = check_linear_ancestry(commit_id,
1225 got_worktree_get_base_commit_id(worktree), repo);
1226 if (error != NULL) {
1227 if (error->code == GOT_ERR_ANCESTRY)
1228 error = got_error(GOT_ERR_BRANCH_MOVED);
1231 error = check_same_branch(commit_id, head_ref, NULL, repo);
1236 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1238 error = got_worktree_set_base_commit_id(worktree, repo,
1244 error = got_worktree_checkout_files(worktree, &paths, repo,
1245 update_progress, &did_something, check_cancelled, NULL);
1250 printf("Updated to commit %s\n", commit_id_str);
1252 printf("Already up-to-date\n");
1254 free(worktree_path);
1255 TAILQ_FOREACH(pe, &paths, entry)
1256 free((char *)pe->path);
1257 got_pathlist_free(&paths);
1259 free(commit_id_str);
1263 static const struct got_error *
1264 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1265 int diff_context, struct got_repository *repo)
1267 const struct got_error *err = NULL;
1268 struct got_tree_object *tree1 = NULL, *tree2;
1269 struct got_object_qid *qid;
1270 char *id_str1 = NULL, *id_str2;
1271 struct got_diff_blob_output_unidiff_arg arg;
1273 err = got_object_open_as_tree(&tree2, repo,
1274 got_object_commit_get_tree_id(commit));
1278 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1280 struct got_commit_object *pcommit;
1282 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1286 err = got_object_open_as_tree(&tree1, repo,
1287 got_object_commit_get_tree_id(pcommit));
1288 got_object_commit_close(pcommit);
1292 err = got_object_id_str(&id_str1, qid->id);
1297 err = got_object_id_str(&id_str2, id);
1301 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1302 arg.diff_context = diff_context;
1303 arg.outfile = stdout;
1304 err = got_diff_tree(tree1, tree2, "", "", repo,
1305 got_diff_blob_output_unidiff, &arg, 1);
1308 got_object_tree_close(tree1);
1309 got_object_tree_close(tree2);
1316 get_datestr(time_t *time, char *datebuf)
1318 char *p, *s = ctime_r(time, datebuf);
1319 p = strchr(s, '\n');
1325 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1327 static const struct got_error *
1328 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1329 struct got_repository *repo, int show_patch, int diff_context,
1330 struct got_reflist_head *refs)
1332 const struct got_error *err = NULL;
1333 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1335 time_t committer_time;
1336 const char *author, *committer;
1337 char *refs_str = NULL;
1338 struct got_reflist_entry *re;
1340 SIMPLEQ_FOREACH(re, refs, entry) {
1343 if (got_object_id_cmp(re->id, id) != 0)
1345 name = got_ref_get_name(re->ref);
1346 if (strcmp(name, GOT_REF_HEAD) == 0)
1348 if (strncmp(name, "refs/", 5) == 0)
1350 if (strncmp(name, "got/", 4) == 0)
1352 if (strncmp(name, "heads/", 6) == 0)
1354 if (strncmp(name, "remotes/", 8) == 0)
1357 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1359 err = got_error_from_errno("asprintf");
1365 err = got_object_id_str(&id_str, id);
1369 printf(GOT_COMMIT_SEP_STR);
1370 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1371 refs_str ? refs_str : "", refs_str ? ")" : "");
1376 printf("from: %s\n", got_object_commit_get_author(commit));
1377 committer_time = got_object_commit_get_committer_time(commit);
1378 datestr = get_datestr(&committer_time, datebuf);
1379 printf("date: %s UTC\n", datestr);
1380 author = got_object_commit_get_author(commit);
1381 committer = got_object_commit_get_committer(commit);
1382 if (strcmp(author, committer) != 0)
1383 printf("via: %s\n", committer);
1384 if (got_object_commit_get_nparents(commit) > 1) {
1385 const struct got_object_id_queue *parent_ids;
1386 struct got_object_qid *qid;
1388 parent_ids = got_object_commit_get_parent_ids(commit);
1389 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1390 err = got_object_id_str(&id_str, qid->id);
1393 printf("parent %d: %s\n", n++, id_str);
1398 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1399 if (logmsg0 == NULL)
1400 return got_error_from_errno("strdup");
1404 line = strsep(&logmsg, "\n");
1406 printf(" %s\n", line);
1411 err = print_patch(commit, id, diff_context, repo);
1416 if (fflush(stdout) != 0 && err == NULL)
1417 err = got_error_from_errno("fflush");
1421 static const struct got_error *
1422 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1423 char *path, int show_patch, int diff_context, int limit,
1424 int first_parent_traversal, struct got_reflist_head *refs)
1426 const struct got_error *err;
1427 struct got_commit_graph *graph;
1429 err = got_commit_graph_open(&graph, root_id, path,
1430 first_parent_traversal, repo);
1433 err = got_commit_graph_iter_start(graph, root_id, repo);
1437 struct got_commit_object *commit;
1438 struct got_object_id *id;
1440 if (sigint_received || sigpipe_received)
1443 err = got_commit_graph_iter_next(&id, graph);
1445 if (err->code == GOT_ERR_ITER_COMPLETED) {
1449 if (err->code != GOT_ERR_ITER_NEED_MORE)
1451 err = got_commit_graph_fetch_commits(graph, 1, repo);
1460 err = got_object_open_as_commit(&commit, repo, id);
1463 err = print_commit(commit, id, repo, show_patch, diff_context,
1465 got_object_commit_close(commit);
1466 if (err || (limit && --limit == 0))
1470 got_commit_graph_close(graph);
1477 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1478 "[-r repository-path] [path]\n", getprogname());
1482 static const struct got_error *
1483 cmd_log(int argc, char *argv[])
1485 const struct got_error *error;
1486 struct got_repository *repo = NULL;
1487 struct got_worktree *worktree = NULL;
1488 struct got_commit_object *commit = NULL;
1489 struct got_object_id *id = NULL;
1490 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1491 char *start_commit = NULL;
1492 int diff_context = 3, ch;
1493 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1495 struct got_reflist_head refs;
1497 SIMPLEQ_INIT(&refs);
1500 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1506 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1512 start_commit = optarg;
1515 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1518 err(1, "-C option %s", errstr);
1521 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1523 err(1, "-l option %s", errstr);
1526 first_parent_traversal = 1;
1529 repo_path = realpath(optarg, NULL);
1530 if (repo_path == NULL)
1531 err(1, "-r option");
1532 got_path_strip_trailing_slashes(repo_path);
1543 cwd = getcwd(NULL, 0);
1545 error = got_error_from_errno("getcwd");
1549 error = got_worktree_open(&worktree, cwd);
1550 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1557 error = got_error_from_errno("strdup");
1560 } else if (argc == 1) {
1562 error = got_worktree_resolve_path(&path, worktree,
1567 path = strdup(argv[0]);
1569 error = got_error_from_errno("strdup");
1576 if (repo_path == NULL) {
1577 repo_path = worktree ?
1578 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1580 if (repo_path == NULL) {
1581 error = got_error_from_errno("strdup");
1585 error = got_repo_open(&repo, repo_path);
1589 error = apply_unveil(got_repo_get_path(repo), 1,
1590 worktree ? got_worktree_get_root_path(worktree) : NULL);
1594 if (start_commit == NULL) {
1595 struct got_reference *head_ref;
1596 error = got_ref_open(&head_ref, repo,
1597 worktree ? got_worktree_get_head_ref_name(worktree)
1601 error = got_ref_resolve(&id, repo, head_ref);
1602 got_ref_close(head_ref);
1605 error = got_object_open_as_commit(&commit, repo, id);
1607 struct got_reference *ref;
1608 error = got_ref_open(&ref, repo, start_commit, 0);
1609 if (error == NULL) {
1611 error = got_ref_resolve(&id, repo, ref);
1615 error = got_object_get_type(&obj_type, repo, id);
1618 if (obj_type == GOT_OBJ_TYPE_TAG) {
1619 struct got_tag_object *tag;
1620 error = got_object_open_as_tag(&tag, repo, id);
1623 if (got_object_tag_get_object_type(tag) !=
1624 GOT_OBJ_TYPE_COMMIT) {
1625 got_object_tag_close(tag);
1626 error = got_error(GOT_ERR_OBJ_TYPE);
1630 id = got_object_id_dup(
1631 got_object_tag_get_object_id(tag));
1633 error = got_error_from_errno(
1634 "got_object_id_dup");
1635 got_object_tag_close(tag);
1638 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1639 error = got_error(GOT_ERR_OBJ_TYPE);
1642 error = got_object_open_as_commit(&commit, repo, id);
1646 if (commit == NULL) {
1647 error = got_repo_match_object_id_prefix(&id,
1648 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1656 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1661 path = in_repo_path;
1664 error = got_ref_list(&refs, repo);
1668 error = print_commits(id, repo, path, show_patch,
1669 diff_context, limit, first_parent_traversal, &refs);
1676 got_worktree_close(worktree);
1678 const struct got_error *repo_error;
1679 repo_error = got_repo_close(repo);
1683 got_ref_list_free(&refs);
1690 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1691 "[object1 object2 | path]\n", getprogname());
1695 struct print_diff_arg {
1696 struct got_repository *repo;
1697 struct got_worktree *worktree;
1704 static const struct got_error *
1705 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1706 const char *path, struct got_object_id *blob_id,
1707 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1709 struct print_diff_arg *a = arg;
1710 const struct got_error *err = NULL;
1711 struct got_blob_object *blob1 = NULL;
1713 char *abspath = NULL, *label1 = NULL;
1716 if (a->diff_staged) {
1717 if (staged_status != GOT_STATUS_MODIFY &&
1718 staged_status != GOT_STATUS_ADD &&
1719 staged_status != GOT_STATUS_DELETE)
1722 if (staged_status == GOT_STATUS_DELETE)
1724 if (status != GOT_STATUS_MODIFY &&
1725 status != GOT_STATUS_ADD &&
1726 status != GOT_STATUS_DELETE &&
1727 status != GOT_STATUS_CONFLICT)
1731 if (!a->header_shown) {
1732 printf("diff %s %s%s\n", a->id_str,
1733 got_worktree_get_root_path(a->worktree),
1734 a->diff_staged ? " (staged changes)" : "");
1735 a->header_shown = 1;
1738 if (a->diff_staged) {
1739 const char *label1 = NULL, *label2 = NULL;
1740 switch (staged_status) {
1741 case GOT_STATUS_MODIFY:
1745 case GOT_STATUS_ADD:
1748 case GOT_STATUS_DELETE:
1752 return got_error(GOT_ERR_FILE_STATUS);
1754 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1755 label1, label2, a->diff_context, a->repo, stdout);
1758 if (staged_status == GOT_STATUS_ADD ||
1759 staged_status == GOT_STATUS_MODIFY) {
1761 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1765 err = got_object_id_str(&id_str, staged_blob_id);
1768 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1769 err = got_error_from_errno("asprintf");
1774 } else if (status != GOT_STATUS_ADD) {
1775 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1780 if (status != GOT_STATUS_DELETE) {
1781 if (asprintf(&abspath, "%s/%s",
1782 got_worktree_get_root_path(a->worktree), path) == -1) {
1783 err = got_error_from_errno("asprintf");
1787 f2 = fopen(abspath, "r");
1789 err = got_error_from_errno2("fopen", abspath);
1792 if (lstat(abspath, &sb) == -1) {
1793 err = got_error_from_errno2("lstat", abspath);
1799 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1800 a->diff_context, stdout);
1803 got_object_blob_close(blob1);
1804 if (f2 && fclose(f2) != 0 && err == NULL)
1805 err = got_error_from_errno("fclose");
1810 static const struct got_error *
1811 cmd_diff(int argc, char *argv[])
1813 const struct got_error *error;
1814 struct got_repository *repo = NULL;
1815 struct got_worktree *worktree = NULL;
1816 char *cwd = NULL, *repo_path = NULL;
1817 struct got_object_id *id1 = NULL, *id2 = NULL;
1818 const char *id_str1 = NULL, *id_str2 = NULL;
1819 char *label1 = NULL, *label2 = NULL;
1821 int diff_context = 3, diff_staged = 0, ch;
1826 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1831 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1834 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1836 err(1, "-C option %s", errstr);
1839 repo_path = realpath(optarg, NULL);
1840 if (repo_path == NULL)
1841 err(1, "-r option");
1842 got_path_strip_trailing_slashes(repo_path);
1856 cwd = getcwd(NULL, 0);
1858 error = got_error_from_errno("getcwd");
1861 error = got_worktree_open(&worktree, cwd);
1862 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1865 if (worktree == NULL) {
1866 error = got_error(GOT_ERR_NOT_WORKTREE);
1871 "-r option can't be used when diffing a work tree");
1872 repo_path = strdup(got_worktree_get_repo_path(worktree));
1873 if (repo_path == NULL) {
1874 error = got_error_from_errno("strdup");
1878 error = got_worktree_resolve_path(&path, worktree,
1885 error = got_error_from_errno("strdup");
1889 } else if (argc == 2) {
1891 errx(1, "-s option can't be used when diffing "
1892 "objects in repository");
1895 if (worktree && repo_path == NULL) {
1897 strdup(got_worktree_get_repo_path(worktree));
1898 if (repo_path == NULL) {
1899 error = got_error_from_errno("strdup");
1906 if (repo_path == NULL) {
1907 repo_path = getcwd(NULL, 0);
1908 if (repo_path == NULL)
1909 return got_error_from_errno("getcwd");
1912 error = got_repo_open(&repo, repo_path);
1917 error = apply_unveil(got_repo_get_path(repo), 1,
1918 worktree ? got_worktree_get_root_path(worktree) : NULL);
1923 struct print_diff_arg arg;
1924 struct got_pathlist_head paths;
1929 error = got_object_id_str(&id_str,
1930 got_worktree_get_base_commit_id(worktree));
1934 arg.worktree = worktree;
1935 arg.diff_context = diff_context;
1936 arg.id_str = id_str;
1937 arg.header_shown = 0;
1938 arg.diff_staged = diff_staged;
1940 error = got_pathlist_append(&paths, path, NULL);
1944 error = got_worktree_status(worktree, &paths, repo, print_diff,
1945 &arg, check_cancelled, NULL);
1947 got_pathlist_free(&paths);
1951 error = got_repo_match_object_id_prefix(&id1, id_str1,
1952 GOT_OBJ_TYPE_ANY, repo);
1954 struct got_reference *ref;
1955 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1957 error = got_ref_open(&ref, repo, id_str1, 0);
1960 label1 = strdup(got_ref_get_name(ref));
1961 if (label1 == NULL) {
1962 error = got_error_from_errno("strdup");
1965 error = got_ref_resolve(&id1, repo, ref);
1970 error = got_object_id_str(&label1, id1);
1971 if (label1 == NULL) {
1972 error = got_error_from_errno("strdup");
1977 error = got_repo_match_object_id_prefix(&id2, id_str2,
1978 GOT_OBJ_TYPE_ANY, repo);
1980 struct got_reference *ref;
1981 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1983 error = got_ref_open(&ref, repo, id_str2, 0);
1986 label2 = strdup(got_ref_get_name(ref));
1987 if (label2 == NULL) {
1988 error = got_error_from_errno("strdup");
1991 error = got_ref_resolve(&id2, repo, ref);
1996 error = got_object_id_str(&label2, id2);
1997 if (label2 == NULL) {
1998 error = got_error_from_errno("strdup");
2003 error = got_object_get_type(&type1, repo, id1);
2007 error = got_object_get_type(&type2, repo, id2);
2011 if (type1 != type2) {
2012 error = got_error(GOT_ERR_OBJ_TYPE);
2017 case GOT_OBJ_TYPE_BLOB:
2018 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2019 diff_context, repo, stdout);
2021 case GOT_OBJ_TYPE_TREE:
2022 error = got_diff_objects_as_trees(id1, id2, "", "",
2023 diff_context, repo, stdout);
2025 case GOT_OBJ_TYPE_COMMIT:
2026 printf("diff %s %s\n", label1, label2);
2027 error = got_diff_objects_as_commits(id1, id2, diff_context,
2031 error = got_error(GOT_ERR_OBJ_TYPE);
2041 got_worktree_close(worktree);
2043 const struct got_error *repo_error;
2044 repo_error = got_repo_close(repo);
2055 "usage: %s blame [-c commit] [-r repository-path] path\n",
2060 static const struct got_error *
2061 cmd_blame(int argc, char *argv[])
2063 const struct got_error *error;
2064 struct got_repository *repo = NULL;
2065 struct got_worktree *worktree = NULL;
2066 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2067 struct got_object_id *commit_id = NULL;
2068 char *commit_id_str = NULL;
2072 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2077 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2080 commit_id_str = optarg;
2083 repo_path = realpath(optarg, NULL);
2084 if (repo_path == NULL)
2085 err(1, "-r option");
2086 got_path_strip_trailing_slashes(repo_path);
2102 cwd = getcwd(NULL, 0);
2104 error = got_error_from_errno("getcwd");
2107 if (repo_path == NULL) {
2108 error = got_worktree_open(&worktree, cwd);
2109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2115 strdup(got_worktree_get_repo_path(worktree));
2116 if (repo_path == NULL)
2117 error = got_error_from_errno("strdup");
2121 repo_path = strdup(cwd);
2122 if (repo_path == NULL) {
2123 error = got_error_from_errno("strdup");
2129 error = got_repo_open(&repo, repo_path);
2133 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2138 const char *prefix = got_worktree_get_path_prefix(worktree);
2139 char *p, *worktree_subdir = cwd +
2140 strlen(got_worktree_get_root_path(worktree));
2141 if (asprintf(&p, "%s%s%s%s%s",
2142 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2143 worktree_subdir, worktree_subdir[0] ? "/" : "",
2145 error = got_error_from_errno("asprintf");
2148 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2151 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2156 if (commit_id_str == NULL) {
2157 struct got_reference *head_ref;
2158 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2161 error = got_ref_resolve(&commit_id, repo, head_ref);
2162 got_ref_close(head_ref);
2166 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2171 error = got_blame(in_repo_path, commit_id, repo, stdout);
2178 got_worktree_close(worktree);
2180 const struct got_error *repo_error;
2181 repo_error = got_repo_close(repo);
2192 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2198 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2199 const char *root_path)
2201 int is_root_path = (strcmp(path, root_path) == 0);
2203 path += strlen(root_path);
2204 while (path[0] == '/')
2207 printf("%s%s%s%s%s\n", id ? id : "", path,
2208 is_root_path ? "" : "/", te->name,
2209 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2212 static const struct got_error *
2213 print_tree(const char *path, struct got_object_id *commit_id,
2214 int show_ids, int recurse, const char *root_path,
2215 struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_object_id *tree_id = NULL;
2219 struct got_tree_object *tree = NULL;
2220 const struct got_tree_entries *entries;
2221 struct got_tree_entry *te;
2223 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2227 err = got_object_open_as_tree(&tree, repo, tree_id);
2230 entries = got_object_tree_get_entries(tree);
2231 te = SIMPLEQ_FIRST(&entries->head);
2235 if (sigint_received || sigpipe_received)
2240 err = got_object_id_str(&id_str, te->id);
2243 if (asprintf(&id, "%s ", id_str) == -1) {
2244 err = got_error_from_errno("asprintf");
2250 print_entry(te, id, path, root_path);
2253 if (recurse && S_ISDIR(te->mode)) {
2255 if (asprintf(&child_path, "%s%s%s", path,
2256 path[0] == '/' && path[1] == '\0' ? "" : "/",
2258 err = got_error_from_errno("asprintf");
2261 err = print_tree(child_path, commit_id, show_ids, 1,
2268 te = SIMPLEQ_NEXT(te, entry);
2272 got_object_tree_close(tree);
2277 static const struct got_error *
2278 cmd_tree(int argc, char *argv[])
2280 const struct got_error *error;
2281 struct got_repository *repo = NULL;
2282 struct got_worktree *worktree = NULL;
2284 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2285 struct got_object_id *commit_id = NULL;
2286 char *commit_id_str = NULL;
2287 int show_ids = 0, recurse = 0;
2291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2296 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2299 commit_id_str = optarg;
2302 repo_path = realpath(optarg, NULL);
2303 if (repo_path == NULL)
2304 err(1, "-r option");
2305 got_path_strip_trailing_slashes(repo_path);
2329 cwd = getcwd(NULL, 0);
2331 error = got_error_from_errno("getcwd");
2334 if (repo_path == NULL) {
2335 error = got_worktree_open(&worktree, cwd);
2336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2342 strdup(got_worktree_get_repo_path(worktree));
2343 if (repo_path == NULL)
2344 error = got_error_from_errno("strdup");
2348 repo_path = strdup(cwd);
2349 if (repo_path == NULL) {
2350 error = got_error_from_errno("strdup");
2356 error = got_repo_open(&repo, repo_path);
2360 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2366 char *p, *worktree_subdir = cwd +
2367 strlen(got_worktree_get_root_path(worktree));
2368 if (asprintf(&p, "%s/%s",
2369 got_worktree_get_path_prefix(worktree),
2370 worktree_subdir) == -1) {
2371 error = got_error_from_errno("asprintf");
2374 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2381 if (in_repo_path == NULL) {
2382 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2387 if (commit_id_str == NULL) {
2388 struct got_reference *head_ref;
2389 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2392 error = got_ref_resolve(&commit_id, repo, head_ref);
2393 got_ref_close(head_ref);
2397 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2402 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2403 in_repo_path, repo);
2410 got_worktree_close(worktree);
2412 const struct got_error *repo_error;
2413 repo_error = got_repo_close(repo);
2423 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2427 static const struct got_error *
2428 print_status(void *arg, unsigned char status, unsigned char staged_status,
2429 const char *path, struct got_object_id *blob_id,
2430 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2432 if (status == staged_status && (status == GOT_STATUS_DELETE))
2433 status = GOT_STATUS_NO_CHANGE;
2434 printf("%c%c %s\n", status, staged_status, path);
2438 static const struct got_error *
2439 cmd_status(int argc, char *argv[])
2441 const struct got_error *error = NULL;
2442 struct got_repository *repo = NULL;
2443 struct got_worktree *worktree = NULL;
2445 struct got_pathlist_head paths;
2446 struct got_pathlist_entry *pe;
2451 while ((ch = getopt(argc, argv, "")) != -1) {
2463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2467 cwd = getcwd(NULL, 0);
2469 error = got_error_from_errno("getcwd");
2473 error = got_worktree_open(&worktree, cwd);
2477 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2481 error = apply_unveil(got_repo_get_path(repo), 1,
2482 got_worktree_get_root_path(worktree));
2486 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2490 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2491 check_cancelled, NULL);
2493 TAILQ_FOREACH(pe, &paths, entry)
2494 free((char *)pe->path);
2495 got_pathlist_free(&paths);
2504 "usage: %s ref [-r repository] -l | -d name | name target\n",
2509 static const struct got_error *
2510 list_refs(struct got_repository *repo)
2512 static const struct got_error *err = NULL;
2513 struct got_reflist_head refs;
2514 struct got_reflist_entry *re;
2516 SIMPLEQ_INIT(&refs);
2517 err = got_ref_list(&refs, repo);
2521 SIMPLEQ_FOREACH(re, &refs, entry) {
2523 refstr = got_ref_to_str(re->ref);
2525 return got_error_from_errno("got_ref_to_str");
2526 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2530 got_ref_list_free(&refs);
2534 static const struct got_error *
2535 delete_ref(struct got_repository *repo, const char *refname)
2537 const struct got_error *err = NULL;
2538 struct got_reference *ref;
2540 err = got_ref_open(&ref, repo, refname, 0);
2544 err = got_ref_delete(ref, repo);
2549 static const struct got_error *
2550 add_ref(struct got_repository *repo, const char *refname, const char *target)
2552 const struct got_error *err = NULL;
2553 struct got_object_id *id;
2554 struct got_reference *ref = NULL;
2557 * Don't let the user create a reference named '-'.
2558 * While technically a valid reference name, this case is usually
2559 * an unintended typo.
2561 if (refname[0] == '-' && refname[1] == '\0')
2562 return got_error(GOT_ERR_BAD_REF_NAME);
2564 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2567 struct got_reference *target_ref;
2569 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2571 err = got_ref_open(&target_ref, repo, target, 0);
2574 err = got_ref_resolve(&id, repo, target_ref);
2575 got_ref_close(target_ref);
2580 err = got_ref_alloc(&ref, refname, id);
2584 err = got_ref_write(ref, repo);
2592 static const struct got_error *
2593 cmd_ref(int argc, char *argv[])
2595 const struct got_error *error = NULL;
2596 struct got_repository *repo = NULL;
2597 struct got_worktree *worktree = NULL;
2598 char *cwd = NULL, *repo_path = NULL;
2599 int ch, do_list = 0;
2600 const char *delref = NULL;
2602 /* TODO: Add -s option for adding symbolic references. */
2603 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2609 repo_path = realpath(optarg, NULL);
2610 if (repo_path == NULL)
2611 err(1, "-r option");
2612 got_path_strip_trailing_slashes(repo_path);
2623 if (do_list && delref)
2624 errx(1, "-l and -d options are mutually exclusive\n");
2629 if (do_list || delref) {
2632 } else if (argc != 2)
2637 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2641 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2642 "sendfd unveil", NULL) == -1)
2646 cwd = getcwd(NULL, 0);
2648 error = got_error_from_errno("getcwd");
2652 if (repo_path == NULL) {
2653 error = got_worktree_open(&worktree, cwd);
2654 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2660 strdup(got_worktree_get_repo_path(worktree));
2661 if (repo_path == NULL)
2662 error = got_error_from_errno("strdup");
2666 repo_path = strdup(cwd);
2667 if (repo_path == NULL) {
2668 error = got_error_from_errno("strdup");
2674 error = got_repo_open(&repo, repo_path);
2678 error = apply_unveil(got_repo_get_path(repo), do_list,
2679 worktree ? got_worktree_get_root_path(worktree) : NULL);
2684 error = list_refs(repo);
2686 error = delete_ref(repo, delref);
2688 error = add_ref(repo, argv[0], argv[1]);
2691 got_repo_close(repo);
2693 got_worktree_close(worktree);
2703 "usage: %s branch [-r repository] -l | -d name | "
2704 "name [base-branch]\n", getprogname());
2708 static const struct got_error *
2709 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2711 static const struct got_error *err = NULL;
2712 struct got_reflist_head refs;
2713 struct got_reflist_entry *re;
2715 SIMPLEQ_INIT(&refs);
2717 err = got_ref_list(&refs, repo);
2721 SIMPLEQ_FOREACH(re, &refs, entry) {
2722 const char *refname, *marker = " ";
2724 refname = got_ref_get_name(re->ref);
2725 if (strncmp(refname, "refs/heads/", 11) != 0)
2727 if (worktree && strcmp(refname,
2728 got_worktree_get_head_ref_name(worktree)) == 0) {
2729 struct got_object_id *id = NULL;
2730 err = got_ref_resolve(&id, repo, re->ref);
2733 if (got_object_id_cmp(id,
2734 got_worktree_get_base_commit_id(worktree)) == 0)
2741 refstr = got_ref_to_str(re->ref);
2743 return got_error_from_errno("got_ref_to_str");
2744 printf("%s%s: %s\n", marker, refname, refstr);
2748 got_ref_list_free(&refs);
2752 static const struct got_error *
2753 delete_branch(struct got_repository *repo, const char *branch_name)
2755 const struct got_error *err = NULL;
2756 struct got_reference *ref;
2759 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2760 return got_error_from_errno("asprintf");
2762 err = got_ref_open(&ref, repo, refname, 0);
2766 err = got_ref_delete(ref, repo);
2773 static const struct got_error *
2774 add_branch(struct got_repository *repo, const char *branch_name,
2775 const char *base_branch)
2777 const struct got_error *err = NULL;
2778 struct got_object_id *id = NULL;
2779 struct got_reference *ref = NULL;
2780 char *base_refname = NULL, *refname = NULL;
2781 struct got_reference *base_ref;
2784 * Don't let the user create a branch named '-'.
2785 * While technically a valid reference name, this case is usually
2786 * an unintended typo.
2788 if (branch_name[0] == '-' && branch_name[1] == '\0')
2789 return got_error(GOT_ERR_BAD_REF_NAME);
2791 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2792 base_refname = strdup(GOT_REF_HEAD);
2793 if (base_refname == NULL)
2794 return got_error_from_errno("strdup");
2795 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2796 return got_error_from_errno("asprintf");
2798 err = got_ref_open(&base_ref, repo, base_refname, 0);
2801 err = got_ref_resolve(&id, repo, base_ref);
2802 got_ref_close(base_ref);
2806 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2807 err = got_error_from_errno("asprintf");
2811 err = got_ref_open(&ref, repo, refname, 0);
2813 err = got_error(GOT_ERR_BRANCH_EXISTS);
2815 } else if (err->code != GOT_ERR_NOT_REF)
2818 err = got_ref_alloc(&ref, refname, id);
2822 err = got_ref_write(ref, repo);
2832 static const struct got_error *
2833 cmd_branch(int argc, char *argv[])
2835 const struct got_error *error = NULL;
2836 struct got_repository *repo = NULL;
2837 struct got_worktree *worktree = NULL;
2838 char *cwd = NULL, *repo_path = NULL;
2839 int ch, do_list = 0;
2840 const char *delref = NULL;
2842 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2848 repo_path = realpath(optarg, NULL);
2849 if (repo_path == NULL)
2850 err(1, "-r option");
2851 got_path_strip_trailing_slashes(repo_path);
2862 if (do_list && delref)
2863 errx(1, "-l and -d options are mutually exclusive\n");
2868 if (do_list || delref) {
2871 } else if (argc < 1 || argc > 2)
2876 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2880 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2881 "sendfd unveil", NULL) == -1)
2885 cwd = getcwd(NULL, 0);
2887 error = got_error_from_errno("getcwd");
2891 if (repo_path == NULL) {
2892 error = got_worktree_open(&worktree, cwd);
2893 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2899 strdup(got_worktree_get_repo_path(worktree));
2900 if (repo_path == NULL)
2901 error = got_error_from_errno("strdup");
2905 repo_path = strdup(cwd);
2906 if (repo_path == NULL) {
2907 error = got_error_from_errno("strdup");
2913 error = got_repo_open(&repo, repo_path);
2917 error = apply_unveil(got_repo_get_path(repo), do_list,
2918 worktree ? got_worktree_get_root_path(worktree) : NULL);
2923 error = list_branches(repo, worktree);
2925 error = delete_branch(repo, delref);
2927 const char *base_branch;
2929 base_branch = worktree ?
2930 got_worktree_get_head_ref_name(worktree) :
2932 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2935 base_branch = argv[1];
2936 error = add_branch(repo, argv[0], base_branch);
2940 got_repo_close(repo);
2942 got_worktree_close(worktree);
2951 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2955 static const struct got_error *
2956 cmd_add(int argc, char *argv[])
2958 const struct got_error *error = NULL;
2959 struct got_repository *repo = NULL;
2960 struct got_worktree *worktree = NULL;
2962 struct got_pathlist_head paths;
2963 struct got_pathlist_entry *pe;
2968 while ((ch = getopt(argc, argv, "")) != -1) {
2980 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2987 cwd = getcwd(NULL, 0);
2989 error = got_error_from_errno("getcwd");
2993 error = got_worktree_open(&worktree, cwd);
2997 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3001 error = apply_unveil(got_repo_get_path(repo), 1,
3002 got_worktree_get_root_path(worktree));
3006 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3010 error = got_worktree_schedule_add(worktree, &paths, print_status,
3014 got_repo_close(repo);
3016 got_worktree_close(worktree);
3017 TAILQ_FOREACH(pe, &paths, entry)
3018 free((char *)pe->path);
3019 got_pathlist_free(&paths);
3027 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3031 static const struct got_error *
3032 cmd_remove(int argc, char *argv[])
3034 const struct got_error *error = NULL;
3035 struct got_worktree *worktree = NULL;
3036 struct got_repository *repo = NULL;
3038 struct got_pathlist_head paths;
3039 struct got_pathlist_entry *pe;
3040 int ch, delete_local_mods = 0;
3044 while ((ch = getopt(argc, argv, "f")) != -1) {
3047 delete_local_mods = 1;
3059 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3066 cwd = getcwd(NULL, 0);
3068 error = got_error_from_errno("getcwd");
3071 error = got_worktree_open(&worktree, cwd);
3075 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3079 error = apply_unveil(got_repo_get_path(repo), 1,
3080 got_worktree_get_root_path(worktree));
3084 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3088 error = got_worktree_schedule_delete(worktree, &paths,
3089 delete_local_mods, print_status, NULL, repo);
3094 got_repo_close(repo);
3096 got_worktree_close(worktree);
3097 TAILQ_FOREACH(pe, &paths, entry)
3098 free((char *)pe->path);
3099 got_pathlist_free(&paths);
3107 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3108 "path ...\n", getprogname());
3112 static const struct got_error *
3113 revert_progress(void *arg, unsigned char status, const char *path)
3115 while (path[0] == '/')
3117 printf("%c %s\n", status, path);
3121 struct choose_patch_arg {
3122 FILE *patch_script_file;
3126 static const struct got_error *
3127 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3128 int nchanges, const char *action)
3131 size_t linesize = 0;
3135 case GOT_STATUS_ADD:
3136 printf("A %s\n%s this addition? [y/n] ", path, action);
3138 case GOT_STATUS_DELETE:
3139 printf("D %s\n%s this deletion? [y/n] ", path, action);
3141 case GOT_STATUS_MODIFY:
3142 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3143 return got_error_from_errno("fseek");
3144 printf(GOT_COMMIT_SEP_STR);
3145 while ((linelen = getline(&line, &linesize, patch_file) != -1))
3147 if (ferror(patch_file))
3148 return got_error_from_errno("getline");
3149 printf(GOT_COMMIT_SEP_STR);
3150 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3151 path, n, nchanges, action);
3154 return got_error_path(path, GOT_ERR_FILE_STATUS);
3160 static const struct got_error *
3161 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3162 FILE *patch_file, int n, int nchanges)
3164 const struct got_error *err = NULL;
3166 size_t linesize = 0;
3169 struct choose_patch_arg *a = arg;
3171 *choice = GOT_PATCH_CHOICE_NONE;
3173 if (a->patch_script_file) {
3175 err = show_change(status, path, patch_file, n, nchanges,
3179 linelen = getline(&line, &linesize, a->patch_script_file);
3180 if (linelen == -1) {
3181 if (ferror(a->patch_script_file))
3182 return got_error_from_errno("getline");
3185 nl = strchr(line, '\n');
3188 if (strcmp(line, "y") == 0) {
3189 *choice = GOT_PATCH_CHOICE_YES;
3191 } else if (strcmp(line, "n") == 0) {
3192 *choice = GOT_PATCH_CHOICE_NO;
3194 } else if (strcmp(line, "q") == 0 &&
3195 status == GOT_STATUS_MODIFY) {
3196 *choice = GOT_PATCH_CHOICE_QUIT;
3199 printf("invalid response '%s'\n", line);
3204 while (resp != 'y' && resp != 'n' && resp != 'q') {
3205 err = show_change(status, path, patch_file, n, nchanges,
3212 if (status == GOT_STATUS_MODIFY) {
3213 if (resp != 'y' && resp != 'n' && resp != 'q') {
3214 printf("invalid response '%c'\n", resp);
3217 } else if (resp != 'y' && resp != 'n') {
3218 printf("invalid response '%c'\n", resp);
3224 *choice = GOT_PATCH_CHOICE_YES;
3225 else if (resp == 'n')
3226 *choice = GOT_PATCH_CHOICE_NO;
3227 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3228 *choice = GOT_PATCH_CHOICE_QUIT;
3234 static const struct got_error *
3235 cmd_revert(int argc, char *argv[])
3237 const struct got_error *error = NULL;
3238 struct got_worktree *worktree = NULL;
3239 struct got_repository *repo = NULL;
3240 char *cwd = NULL, *path = NULL;
3241 struct got_pathlist_head paths;
3242 struct got_pathlist_entry *pe;
3243 int ch, can_recurse = 0, pflag = 0;
3244 FILE *patch_script_file = NULL;
3245 const char *patch_script_path = NULL;
3246 struct choose_patch_arg cpa;
3250 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3256 patch_script_path = optarg;
3271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3272 "unveil", NULL) == -1)
3277 if (patch_script_path && !pflag)
3278 errx(1, "-F option can only be used together with -p option");
3280 cwd = getcwd(NULL, 0);
3282 error = got_error_from_errno("getcwd");
3285 error = got_worktree_open(&worktree, cwd);
3289 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3293 if (patch_script_path) {
3294 patch_script_file = fopen(patch_script_path, "r");
3295 if (patch_script_file == NULL) {
3296 error = got_error_from_errno2("fopen",
3301 error = apply_unveil(got_repo_get_path(repo), 1,
3302 got_worktree_get_root_path(worktree));
3306 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3313 TAILQ_FOREACH(pe, &paths, entry) {
3314 if (asprintf(&ondisk_path, "%s/%s",
3315 got_worktree_get_root_path(worktree),
3317 error = got_error_from_errno("asprintf");
3320 if (lstat(ondisk_path, &sb) == -1) {
3321 if (errno == ENOENT) {
3325 error = got_error_from_errno2("lstat",
3331 if (S_ISDIR(sb.st_mode)) {
3332 error = got_error_msg(GOT_ERR_BAD_PATH,
3333 "reverting directories requires -R option");
3339 cpa.patch_script_file = patch_script_file;
3340 cpa.action = "revert";
3341 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3342 pflag ? choose_patch : NULL, &cpa, repo);
3346 if (patch_script_file && fclose(patch_script_file) == EOF &&
3348 error = got_error_from_errno2("fclose", patch_script_path);
3350 got_repo_close(repo);
3352 got_worktree_close(worktree);
3361 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3366 struct collect_commit_logmsg_arg {
3367 const char *cmdline_log;
3369 const char *worktree_path;
3370 const char *branch_name;
3371 const char *repo_path;
3376 static const struct got_error *
3377 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3380 char *initial_content = NULL;
3381 struct got_pathlist_entry *pe;
3382 const struct got_error *err = NULL;
3383 char *template = NULL;
3384 struct collect_commit_logmsg_arg *a = arg;
3388 /* if a message was specified on the command line, just use it */
3389 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3390 len = strlen(a->cmdline_log) + 1;
3391 *logmsg = malloc(len + 1);
3392 if (*logmsg == NULL)
3393 return got_error_from_errno("malloc");
3394 strlcpy(*logmsg, a->cmdline_log, len);
3398 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3399 return got_error_from_errno("asprintf");
3401 if (asprintf(&initial_content,
3402 "\n# changes to be committed on branch %s:\n",
3403 a->branch_name) == -1)
3404 return got_error_from_errno("asprintf");
3406 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3410 dprintf(fd, initial_content);
3412 TAILQ_FOREACH(pe, commitable_paths, entry) {
3413 struct got_commitable *ct = pe->data;
3414 dprintf(fd, "# %c %s\n",
3415 got_commitable_get_status(ct),
3416 got_commitable_get_path(ct));
3420 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3422 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3423 unlink(a->logmsg_path);
3424 free(a->logmsg_path);
3425 a->logmsg_path = NULL;
3427 free(initial_content);
3430 /* Editor is done; we can now apply unveil(2) */
3432 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3441 static const struct got_error *
3442 cmd_commit(int argc, char *argv[])
3444 const struct got_error *error = NULL;
3445 struct got_worktree *worktree = NULL;
3446 struct got_repository *repo = NULL;
3447 char *cwd = NULL, *id_str = NULL;
3448 struct got_object_id *id = NULL;
3449 const char *logmsg = NULL;
3450 const char *got_author = getenv("GOT_AUTHOR");
3451 struct collect_commit_logmsg_arg cl_arg;
3452 char *editor = NULL;
3453 int ch, rebase_in_progress, histedit_in_progress;
3454 struct got_pathlist_head paths;
3458 while ((ch = getopt(argc, argv, "m:")) != -1) {
3473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3474 "unveil", NULL) == -1)
3477 if (got_author == NULL) {
3478 /* TODO: Look current user up in password database */
3479 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3483 cwd = getcwd(NULL, 0);
3485 error = got_error_from_errno("getcwd");
3488 error = got_worktree_open(&worktree, cwd);
3492 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3495 if (rebase_in_progress) {
3496 error = got_error(GOT_ERR_REBASING);
3500 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3505 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3510 * unveil(2) traverses exec(2); if an editor is used we have
3511 * to apply unveil after the log message has been written.
3513 if (logmsg == NULL || strlen(logmsg) == 0)
3514 error = get_editor(&editor);
3516 error = apply_unveil(got_repo_get_path(repo), 0,
3517 got_worktree_get_root_path(worktree));
3521 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3525 cl_arg.editor = editor;
3526 cl_arg.cmdline_log = logmsg;
3527 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3528 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3529 if (!histedit_in_progress) {
3530 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3531 error = got_error(GOT_ERR_COMMIT_BRANCH);
3534 cl_arg.branch_name += 11;
3536 cl_arg.repo_path = got_repo_get_path(repo);
3537 cl_arg.logmsg_path = NULL;
3538 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3539 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3541 if (cl_arg.logmsg_path)
3542 fprintf(stderr, "%s: log message preserved in %s\n",
3543 getprogname(), cl_arg.logmsg_path);
3547 if (cl_arg.logmsg_path)
3548 unlink(cl_arg.logmsg_path);
3550 error = got_object_id_str(&id_str, id);
3553 printf("Created commit %s\n", id_str);
3556 got_repo_close(repo);
3558 got_worktree_close(worktree);
3566 usage_cherrypick(void)
3568 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3572 static const struct got_error *
3573 cmd_cherrypick(int argc, char *argv[])
3575 const struct got_error *error = NULL;
3576 struct got_worktree *worktree = NULL;
3577 struct got_repository *repo = NULL;
3578 char *cwd = NULL, *commit_id_str = NULL;
3579 struct got_object_id *commit_id = NULL;
3580 struct got_commit_object *commit = NULL;
3581 struct got_object_qid *pid;
3582 struct got_reference *head_ref = NULL;
3583 int ch, did_something = 0;
3585 while ((ch = getopt(argc, argv, "")) != -1) {
3597 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3598 "unveil", NULL) == -1)
3604 cwd = getcwd(NULL, 0);
3606 error = got_error_from_errno("getcwd");
3609 error = got_worktree_open(&worktree, cwd);
3613 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3617 error = apply_unveil(got_repo_get_path(repo), 0,
3618 got_worktree_get_root_path(worktree));
3622 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3623 GOT_OBJ_TYPE_COMMIT, repo);
3624 if (error != NULL) {
3625 struct got_reference *ref;
3626 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3628 error = got_ref_open(&ref, repo, argv[0], 0);
3631 error = got_ref_resolve(&commit_id, repo, ref);
3636 error = got_object_id_str(&commit_id_str, commit_id);
3640 error = got_ref_open(&head_ref, repo,
3641 got_worktree_get_head_ref_name(worktree), 0);
3645 error = check_same_branch(commit_id, head_ref, NULL, repo);
3647 if (error->code != GOT_ERR_ANCESTRY)
3651 error = got_error(GOT_ERR_SAME_BRANCH);
3655 error = got_object_open_as_commit(&commit, repo, commit_id);
3658 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3659 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3660 commit_id, repo, update_progress, &did_something, check_cancelled,
3666 printf("Merged commit %s\n", commit_id_str);
3669 got_object_commit_close(commit);
3670 free(commit_id_str);
3672 got_ref_close(head_ref);
3674 got_worktree_close(worktree);
3676 got_repo_close(repo);
3683 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3687 static const struct got_error *
3688 cmd_backout(int argc, char *argv[])
3690 const struct got_error *error = NULL;
3691 struct got_worktree *worktree = NULL;
3692 struct got_repository *repo = NULL;
3693 char *cwd = NULL, *commit_id_str = NULL;
3694 struct got_object_id *commit_id = NULL;
3695 struct got_commit_object *commit = NULL;
3696 struct got_object_qid *pid;
3697 struct got_reference *head_ref = NULL;
3698 int ch, did_something = 0;
3700 while ((ch = getopt(argc, argv, "")) != -1) {
3712 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3713 "unveil", NULL) == -1)
3719 cwd = getcwd(NULL, 0);
3721 error = got_error_from_errno("getcwd");
3724 error = got_worktree_open(&worktree, cwd);
3728 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3732 error = apply_unveil(got_repo_get_path(repo), 0,
3733 got_worktree_get_root_path(worktree));
3737 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3738 GOT_OBJ_TYPE_COMMIT, repo);
3739 if (error != NULL) {
3740 struct got_reference *ref;
3741 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3743 error = got_ref_open(&ref, repo, argv[0], 0);
3746 error = got_ref_resolve(&commit_id, repo, ref);
3751 error = got_object_id_str(&commit_id_str, commit_id);
3755 error = got_ref_open(&head_ref, repo,
3756 got_worktree_get_head_ref_name(worktree), 0);
3760 error = check_same_branch(commit_id, head_ref, NULL, repo);
3764 error = got_object_open_as_commit(&commit, repo, commit_id);
3767 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3769 error = got_error(GOT_ERR_ROOT_COMMIT);
3773 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3774 update_progress, &did_something, check_cancelled, NULL);
3779 printf("Backed out commit %s\n", commit_id_str);
3782 got_object_commit_close(commit);
3783 free(commit_id_str);
3785 got_ref_close(head_ref);
3787 got_worktree_close(worktree);
3789 got_repo_close(repo);
3796 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3802 trim_logmsg(char *logmsg, int limit)
3807 len = strlen(logmsg);
3811 nl = strchr(logmsg, '\n');
3816 static const struct got_error *
3817 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3819 const char *logmsg0 = NULL;
3821 logmsg0 = got_object_commit_get_logmsg(commit);
3823 while (isspace((unsigned char)logmsg0[0]))
3826 *logmsg = strdup(logmsg0);
3827 if (*logmsg == NULL)
3828 return got_error_from_errno("strdup");
3830 trim_logmsg(*logmsg, limit);
3834 static const struct got_error *
3835 show_rebase_progress(struct got_commit_object *commit,
3836 struct got_object_id *old_id, struct got_object_id *new_id)
3838 const struct got_error *err;
3839 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3841 err = got_object_id_str(&old_id_str, old_id);
3846 err = got_object_id_str(&new_id_str, new_id);
3851 old_id_str[12] = '\0';
3853 new_id_str[12] = '\0';
3855 err = get_short_logmsg(&logmsg, 42, commit);
3859 printf("%s -> %s: %s\n", old_id_str,
3860 new_id_str ? new_id_str : "no-op change", logmsg);
3867 static const struct got_error *
3868 rebase_progress(void *arg, unsigned char status, const char *path)
3870 unsigned char *rebase_status = arg;
3872 while (path[0] == '/')
3874 printf("%c %s\n", status, path);
3876 if (*rebase_status == GOT_STATUS_CONFLICT)
3878 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3879 *rebase_status = status;
3883 static const struct got_error *
3884 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3885 struct got_reference *branch, struct got_reference *new_base_branch,
3886 struct got_reference *tmp_branch, struct got_repository *repo)
3888 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3889 return got_worktree_rebase_complete(worktree, fileindex,
3890 new_base_branch, tmp_branch, branch, repo);
3893 static const struct got_error *
3894 rebase_commit(struct got_pathlist_head *merged_paths,
3895 struct got_worktree *worktree, struct got_fileindex *fileindex,
3896 struct got_reference *tmp_branch,
3897 struct got_object_id *commit_id, struct got_repository *repo)
3899 const struct got_error *error;
3900 struct got_commit_object *commit;
3901 struct got_object_id *new_commit_id;
3903 error = got_object_open_as_commit(&commit, repo, commit_id);
3907 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3908 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3910 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3912 error = show_rebase_progress(commit, commit_id, NULL);
3914 error = show_rebase_progress(commit, commit_id, new_commit_id);
3915 free(new_commit_id);
3918 got_object_commit_close(commit);
3922 struct check_path_prefix_arg {
3923 const char *path_prefix;
3928 static const struct got_error *
3929 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3930 struct got_blob_object *blob2, struct got_object_id *id1,
3931 struct got_object_id *id2, const char *path1, const char *path2,
3932 struct got_repository *repo)
3934 struct check_path_prefix_arg *a = arg;
3936 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3937 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3938 return got_error(a->errcode);
3943 static const struct got_error *
3944 check_path_prefix(struct got_object_id *parent_id,
3945 struct got_object_id *commit_id, const char *path_prefix,
3946 int errcode, struct got_repository *repo)
3948 const struct got_error *err;
3949 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3950 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3951 struct check_path_prefix_arg cpp_arg;
3953 if (got_path_is_root_dir(path_prefix))
3956 err = got_object_open_as_commit(&commit, repo, commit_id);
3960 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3964 err = got_object_open_as_tree(&tree1, repo,
3965 got_object_commit_get_tree_id(parent_commit));
3969 err = got_object_open_as_tree(&tree2, repo,
3970 got_object_commit_get_tree_id(commit));
3974 cpp_arg.path_prefix = path_prefix;
3975 while (cpp_arg.path_prefix[0] == '/')
3976 cpp_arg.path_prefix++;
3977 cpp_arg.len = strlen(cpp_arg.path_prefix);
3978 cpp_arg.errcode = errcode;
3979 err = got_diff_tree(tree1, tree2, "", "", repo,
3980 check_path_prefix_in_diff, &cpp_arg, 0);
3983 got_object_tree_close(tree1);
3985 got_object_tree_close(tree2);
3987 got_object_commit_close(commit);
3989 got_object_commit_close(parent_commit);
3993 static const struct got_error *
3994 collect_commits(struct got_object_id_queue *commits,
3995 struct got_object_id *initial_commit_id,
3996 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3997 const char *path_prefix, int path_prefix_errcode,
3998 struct got_repository *repo)
4000 const struct got_error *err = NULL;
4001 struct got_commit_graph *graph = NULL;
4002 struct got_object_id *parent_id = NULL;
4003 struct got_object_qid *qid;
4004 struct got_object_id *commit_id = initial_commit_id;
4006 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4010 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4013 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4014 err = got_commit_graph_iter_next(&parent_id, graph);
4016 if (err->code == GOT_ERR_ITER_COMPLETED) {
4017 err = got_error_msg(GOT_ERR_ANCESTRY,
4018 "ran out of commits to rebase before "
4019 "youngest common ancestor commit has "
4022 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4024 err = got_commit_graph_fetch_commits(graph, 1, repo);
4028 err = check_path_prefix(parent_id, commit_id,
4029 path_prefix, path_prefix_errcode, repo);
4033 err = got_object_qid_alloc(&qid, commit_id);
4036 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4037 commit_id = parent_id;
4041 got_commit_graph_close(graph);
4045 static const struct got_error *
4046 cmd_rebase(int argc, char *argv[])
4048 const struct got_error *error = NULL;
4049 struct got_worktree *worktree = NULL;
4050 struct got_repository *repo = NULL;
4051 struct got_fileindex *fileindex = NULL;
4053 struct got_reference *branch = NULL;
4054 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4055 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4056 struct got_object_id *resume_commit_id = NULL;
4057 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4058 struct got_commit_object *commit = NULL;
4059 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4060 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4061 struct got_object_id_queue commits;
4062 struct got_pathlist_head merged_paths;
4063 const struct got_object_id_queue *parent_ids;
4064 struct got_object_qid *qid, *pid;
4066 SIMPLEQ_INIT(&commits);
4067 TAILQ_INIT(&merged_paths);
4069 while ((ch = getopt(argc, argv, "ac")) != -1) {
4075 continue_rebase = 1;
4087 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4088 "unveil", NULL) == -1)
4091 if (abort_rebase && continue_rebase)
4093 else if (abort_rebase || continue_rebase) {
4096 } else if (argc != 1)
4099 cwd = getcwd(NULL, 0);
4101 error = got_error_from_errno("getcwd");
4104 error = got_worktree_open(&worktree, cwd);
4108 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4112 error = apply_unveil(got_repo_get_path(repo), 0,
4113 got_worktree_get_root_path(worktree));
4117 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4123 if (!rebase_in_progress) {
4124 error = got_error(GOT_ERR_NOT_REBASING);
4127 error = got_worktree_rebase_continue(&resume_commit_id,
4128 &new_base_branch, &tmp_branch, &branch, &fileindex,
4132 printf("Switching work tree to %s\n",
4133 got_ref_get_symref_target(new_base_branch));
4134 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4135 new_base_branch, update_progress, &did_something);
4138 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4139 goto done; /* nothing else to do */
4142 if (continue_rebase) {
4143 if (!rebase_in_progress) {
4144 error = got_error(GOT_ERR_NOT_REBASING);
4147 error = got_worktree_rebase_continue(&resume_commit_id,
4148 &new_base_branch, &tmp_branch, &branch, &fileindex,
4153 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4154 resume_commit_id, repo);
4158 yca_id = got_object_id_dup(resume_commit_id);
4159 if (yca_id == NULL) {
4160 error = got_error_from_errno("got_object_id_dup");
4164 error = got_ref_open(&branch, repo, argv[0], 0);
4169 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4173 if (!continue_rebase) {
4174 struct got_object_id *base_commit_id;
4176 base_commit_id = got_worktree_get_base_commit_id(worktree);
4177 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4178 base_commit_id, branch_head_commit_id, repo);
4181 if (yca_id == NULL) {
4182 error = got_error_msg(GOT_ERR_ANCESTRY,
4183 "specified branch shares no common ancestry "
4184 "with work tree's branch");
4188 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4190 if (error->code != GOT_ERR_ANCESTRY)
4194 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4195 "specified branch resolves to a commit which "
4196 "is already contained in work tree's branch");
4199 error = got_worktree_rebase_prepare(&new_base_branch,
4200 &tmp_branch, &fileindex, worktree, branch, repo);
4205 commit_id = branch_head_commit_id;
4206 error = got_object_open_as_commit(&commit, repo, commit_id);
4210 parent_ids = got_object_commit_get_parent_ids(commit);
4211 pid = SIMPLEQ_FIRST(parent_ids);
4212 error = collect_commits(&commits, commit_id, pid->id,
4213 yca_id, got_worktree_get_path_prefix(worktree),
4214 GOT_ERR_REBASE_PATH, repo);
4215 got_object_commit_close(commit);
4220 if (SIMPLEQ_EMPTY(&commits)) {
4221 if (continue_rebase)
4222 error = rebase_complete(worktree, fileindex,
4223 branch, new_base_branch, tmp_branch, repo);
4225 error = got_error(GOT_ERR_EMPTY_REBASE);
4230 SIMPLEQ_FOREACH(qid, &commits, entry) {
4231 commit_id = qid->id;
4232 parent_id = pid ? pid->id : yca_id;
4235 error = got_worktree_rebase_merge_files(&merged_paths,
4236 worktree, fileindex, parent_id, commit_id, repo,
4237 rebase_progress, &rebase_status, check_cancelled, NULL);
4241 if (rebase_status == GOT_STATUS_CONFLICT) {
4242 got_worktree_rebase_pathlist_free(&merged_paths);
4246 error = rebase_commit(&merged_paths, worktree, fileindex,
4247 tmp_branch, commit_id, repo);
4248 got_worktree_rebase_pathlist_free(&merged_paths);
4253 if (rebase_status == GOT_STATUS_CONFLICT) {
4254 error = got_worktree_rebase_postpone(worktree, fileindex);
4257 error = got_error_msg(GOT_ERR_CONFLICTS,
4258 "conflicts must be resolved before rebasing can continue");
4260 error = rebase_complete(worktree, fileindex, branch,
4261 new_base_branch, tmp_branch, repo);
4263 got_object_id_queue_free(&commits);
4264 free(branch_head_commit_id);
4265 free(resume_commit_id);
4268 got_object_commit_close(commit);
4270 got_ref_close(branch);
4271 if (new_base_branch)
4272 got_ref_close(new_base_branch);
4274 got_ref_close(tmp_branch);
4276 got_worktree_close(worktree);
4278 got_repo_close(repo);
4283 usage_histedit(void)
4285 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4290 #define GOT_HISTEDIT_PICK 'p'
4291 #define GOT_HISTEDIT_EDIT 'e'
4292 #define GOT_HISTEDIT_FOLD 'f'
4293 #define GOT_HISTEDIT_DROP 'd'
4294 #define GOT_HISTEDIT_MESG 'm'
4296 static struct got_histedit_cmd {
4300 } got_histedit_cmds[] = {
4301 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4302 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4303 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4304 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4305 { GOT_HISTEDIT_MESG, "mesg",
4306 "single-line log message for commit above (open editor if empty)" },
4309 struct got_histedit_list_entry {
4310 TAILQ_ENTRY(got_histedit_list_entry) entry;
4311 struct got_object_id *commit_id;
4312 const struct got_histedit_cmd *cmd;
4315 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4317 static const struct got_error *
4318 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4319 FILE *f, struct got_repository *repo)
4321 const struct got_error *err = NULL;
4322 char *logmsg = NULL, *id_str = NULL;
4323 struct got_commit_object *commit = NULL;
4326 err = got_object_open_as_commit(&commit, repo, commit_id);
4330 err = get_short_logmsg(&logmsg, 34, commit);
4334 err = got_object_id_str(&id_str, commit_id);
4338 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4340 err = got_ferror(f, GOT_ERR_IO);
4343 got_object_commit_close(commit);
4349 static const struct got_error *
4350 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4351 struct got_repository *repo)
4353 const struct got_error *err = NULL;
4354 struct got_object_qid *qid;
4356 if (SIMPLEQ_EMPTY(commits))
4357 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4359 SIMPLEQ_FOREACH(qid, commits, entry) {
4360 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4369 static const struct got_error *
4370 write_cmd_list(FILE *f)
4372 const struct got_error *err = NULL;
4375 n = fprintf(f, "# Available histedit commands:\n");
4377 return got_ferror(f, GOT_ERR_IO);
4379 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4380 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4381 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4384 err = got_ferror(f, GOT_ERR_IO);
4388 n = fprintf(f, "# Commits will be processed in order from top to "
4389 "bottom of this file.\n");
4391 return got_ferror(f, GOT_ERR_IO);
4395 static const struct got_error *
4396 histedit_syntax_error(int lineno)
4398 static char msg[42];
4401 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4403 if (ret == -1 || ret >= sizeof(msg))
4404 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4406 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4409 static const struct got_error *
4410 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4411 char *logmsg, struct got_repository *repo)
4413 const struct got_error *err;
4414 struct got_commit_object *folded_commit = NULL;
4417 err = got_object_id_str(&id_str, hle->commit_id);
4421 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4425 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4426 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4427 got_object_commit_get_logmsg(folded_commit)) == -1) {
4428 err = got_error_from_errno("asprintf");
4433 got_object_commit_close(folded_commit);
4438 static struct got_histedit_list_entry *
4439 get_folded_commits(struct got_histedit_list_entry *hle)
4441 struct got_histedit_list_entry *prev, *folded = NULL;
4443 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4444 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4445 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4446 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4448 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4454 static const struct got_error *
4455 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4456 struct got_repository *repo)
4458 char *logmsg_path = NULL, *id_str = NULL;
4459 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4460 const struct got_error *err = NULL;
4461 struct got_commit_object *commit = NULL;
4463 struct got_histedit_list_entry *folded = NULL;
4465 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4469 folded = get_folded_commits(hle);
4471 while (folded != hle) {
4472 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4473 folded = TAILQ_NEXT(folded, entry);
4476 err = append_folded_commit_msg(&new_msg, folded,
4482 folded = TAILQ_NEXT(folded, entry);
4486 err = got_object_id_str(&id_str, hle->commit_id);
4489 if (asprintf(&new_msg,
4490 "%s\n# original log message of commit %s: %s",
4491 logmsg ? logmsg : "", id_str,
4492 got_object_commit_get_logmsg(commit)) == -1) {
4493 err = got_error_from_errno("asprintf");
4499 err = got_object_id_str(&id_str, hle->commit_id);
4503 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4507 dprintf(fd, logmsg);
4510 err = get_editor(&editor);
4514 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4516 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4519 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4520 if (hle->logmsg == NULL)
4521 err = got_error_from_errno("strdup");
4524 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4525 err = got_error_from_errno2("unlink", logmsg_path);
4530 got_object_commit_close(commit);
4534 static const struct got_error *
4535 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4536 FILE *f, struct got_repository *repo)
4538 const struct got_error *err = NULL;
4539 char *line = NULL, *p, *end;
4543 const struct got_histedit_cmd *cmd;
4544 struct got_object_id *commit_id = NULL;
4545 struct got_histedit_list_entry *hle = NULL;
4548 len = getline(&line, &size, f);
4550 const struct got_error *getline_err;
4553 getline_err = got_error_from_errno("getline");
4554 err = got_ferror(f, getline_err->code);
4559 while (isspace((unsigned char)p[0]))
4561 if (p[0] == '#' || p[0] == '\0') {
4567 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4568 cmd = &got_histedit_cmds[i];
4569 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4570 isspace((unsigned char)p[strlen(cmd->name)])) {
4571 p += strlen(cmd->name);
4574 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4579 if (i == nitems(got_histedit_cmds)) {
4580 err = histedit_syntax_error(lineno);
4583 while (isspace((unsigned char)p[0]))
4585 if (cmd->code == GOT_HISTEDIT_MESG) {
4586 if (hle == NULL || hle->logmsg != NULL) {
4587 err = got_error(GOT_ERR_HISTEDIT_CMD);
4591 err = histedit_edit_logmsg(hle, repo);
4595 hle->logmsg = strdup(p);
4596 if (hle->logmsg == NULL) {
4597 err = got_error_from_errno("strdup");
4606 while (end[0] && !isspace((unsigned char)end[0]))
4610 err = got_object_resolve_id_str(&commit_id, repo, p);
4612 /* override error code */
4613 err = histedit_syntax_error(lineno);
4617 hle = malloc(sizeof(*hle));
4619 err = got_error_from_errno("malloc");
4623 hle->commit_id = commit_id;
4628 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4636 static const struct got_error *
4637 histedit_check_script(struct got_histedit_list *histedit_cmds,
4638 struct got_object_id_queue *commits, struct got_repository *repo)
4640 const struct got_error *err = NULL;
4641 struct got_object_qid *qid;
4642 struct got_histedit_list_entry *hle;
4643 static char msg[80];
4646 if (TAILQ_EMPTY(histedit_cmds))
4647 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4648 "histedit script contains no commands");
4650 SIMPLEQ_FOREACH(qid, commits, entry) {
4651 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4652 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4656 err = got_object_id_str(&id_str, qid->id);
4659 snprintf(msg, sizeof(msg),
4660 "commit %s missing from histedit script", id_str);
4662 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4666 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4667 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4668 "last commit in histedit script cannot be folded");
4673 static const struct got_error *
4674 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4675 const char *path, struct got_object_id_queue *commits,
4676 struct got_repository *repo)
4678 const struct got_error *err = NULL;
4682 err = get_editor(&editor);
4686 if (spawn_editor(editor, path) == -1) {
4687 err = got_error_from_errno("failed spawning editor");
4691 f = fopen(path, "r");
4693 err = got_error_from_errno("fopen");
4696 err = histedit_parse_list(histedit_cmds, f, repo);
4700 err = histedit_check_script(histedit_cmds, commits, repo);
4702 if (f && fclose(f) != 0 && err == NULL)
4703 err = got_error_from_errno("fclose");
4708 static const struct got_error *
4709 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4710 struct got_object_id_queue *, const char *, struct got_repository *);
4712 static const struct got_error *
4713 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4714 struct got_object_id_queue *commits, struct got_repository *repo)
4716 const struct got_error *err;
4720 err = got_opentemp_named(&path, &f, "got-histedit");
4724 err = write_cmd_list(f);
4728 err = histedit_write_commit_list(commits, f, repo);
4732 if (fclose(f) != 0) {
4733 err = got_error_from_errno("fclose");
4738 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4740 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4741 err->code != GOT_ERR_HISTEDIT_CMD)
4743 err = histedit_edit_list_retry(histedit_cmds, err,
4744 commits, path, repo);
4747 if (f && fclose(f) != 0 && err == NULL)
4748 err = got_error_from_errno("fclose");
4749 if (path && unlink(path) != 0 && err == NULL)
4750 err = got_error_from_errno2("unlink", path);
4755 static const struct got_error *
4756 histedit_save_list(struct got_histedit_list *histedit_cmds,
4757 struct got_worktree *worktree, struct got_repository *repo)
4759 const struct got_error *err = NULL;
4762 struct got_histedit_list_entry *hle;
4763 struct got_commit_object *commit = NULL;
4765 err = got_worktree_get_histedit_script_path(&path, worktree);
4769 f = fopen(path, "w");
4771 err = got_error_from_errno2("fopen", path);
4774 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4775 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4781 int n = fprintf(f, "%c %s\n",
4782 GOT_HISTEDIT_MESG, hle->logmsg);
4784 err = got_ferror(f, GOT_ERR_IO);
4790 if (f && fclose(f) != 0 && err == NULL)
4791 err = got_error_from_errno("fclose");
4794 got_object_commit_close(commit);
4799 histedit_free_list(struct got_histedit_list *histedit_cmds)
4801 struct got_histedit_list_entry *hle;
4803 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4804 TAILQ_REMOVE(histedit_cmds, hle, entry);
4809 static const struct got_error *
4810 histedit_load_list(struct got_histedit_list *histedit_cmds,
4811 const char *path, struct got_repository *repo)
4813 const struct got_error *err = NULL;
4816 f = fopen(path, "r");
4818 err = got_error_from_errno2("fopen", path);
4822 err = histedit_parse_list(histedit_cmds, f, repo);
4824 if (f && fclose(f) != 0 && err == NULL)
4825 err = got_error_from_errno("fclose");
4829 static const struct got_error *
4830 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4831 const struct got_error *edit_err, struct got_object_id_queue *commits,
4832 const char *path, struct got_repository *repo)
4834 const struct got_error *err = NULL, *prev_err = edit_err;
4837 while (resp != 'c' && resp != 'r' && resp != 'a') {
4838 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4839 "or (a)bort: ", getprogname(), prev_err->msg);
4844 histedit_free_list(histedit_cmds);
4845 err = histedit_run_editor(histedit_cmds, path, commits,
4848 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4849 err->code != GOT_ERR_HISTEDIT_CMD)
4856 } else if (resp == 'r') {
4857 histedit_free_list(histedit_cmds);
4858 err = histedit_edit_script(histedit_cmds,
4861 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4862 err->code != GOT_ERR_HISTEDIT_CMD)
4869 } else if (resp == 'a') {
4870 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4873 printf("invalid response '%c'\n", resp);
4879 static const struct got_error *
4880 histedit_complete(struct got_worktree *worktree,
4881 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4882 struct got_reference *branch, struct got_repository *repo)
4884 printf("Switching work tree to %s\n",
4885 got_ref_get_symref_target(branch));
4886 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4890 static const struct got_error *
4891 show_histedit_progress(struct got_commit_object *commit,
4892 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4894 const struct got_error *err;
4895 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4897 err = got_object_id_str(&old_id_str, hle->commit_id);
4902 err = got_object_id_str(&new_id_str, new_id);
4907 old_id_str[12] = '\0';
4909 new_id_str[12] = '\0';
4912 logmsg = strdup(hle->logmsg);
4913 if (logmsg == NULL) {
4914 err = got_error_from_errno("strdup");
4917 trim_logmsg(logmsg, 42);
4919 err = get_short_logmsg(&logmsg, 42, commit);
4924 switch (hle->cmd->code) {
4925 case GOT_HISTEDIT_PICK:
4926 case GOT_HISTEDIT_EDIT:
4927 printf("%s -> %s: %s\n", old_id_str,
4928 new_id_str ? new_id_str : "no-op change", logmsg);
4930 case GOT_HISTEDIT_DROP:
4931 case GOT_HISTEDIT_FOLD:
4932 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4945 static const struct got_error *
4946 histedit_commit(struct got_pathlist_head *merged_paths,
4947 struct got_worktree *worktree, struct got_fileindex *fileindex,
4948 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4949 struct got_repository *repo)
4951 const struct got_error *err;
4952 struct got_commit_object *commit;
4953 struct got_object_id *new_commit_id;
4955 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4956 && hle->logmsg == NULL) {
4957 err = histedit_edit_logmsg(hle, repo);
4962 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4966 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4967 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4970 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4972 err = show_histedit_progress(commit, hle, NULL);
4974 err = show_histedit_progress(commit, hle, new_commit_id);
4975 free(new_commit_id);
4978 got_object_commit_close(commit);
4982 static const struct got_error *
4983 histedit_skip_commit(struct got_histedit_list_entry *hle,
4984 struct got_worktree *worktree, struct got_repository *repo)
4986 const struct got_error *error;
4987 struct got_commit_object *commit;
4989 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4994 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4998 error = show_histedit_progress(commit, hle, NULL);
4999 got_object_commit_close(commit);
5003 static const struct got_error *
5004 cmd_histedit(int argc, char *argv[])
5006 const struct got_error *error = NULL;
5007 struct got_worktree *worktree = NULL;
5008 struct got_fileindex *fileindex = NULL;
5009 struct got_repository *repo = NULL;
5011 struct got_reference *branch = NULL;
5012 struct got_reference *tmp_branch = NULL;
5013 struct got_object_id *resume_commit_id = NULL;
5014 struct got_object_id *base_commit_id = NULL;
5015 struct got_object_id *head_commit_id = NULL;
5016 struct got_commit_object *commit = NULL;
5017 int ch, rebase_in_progress = 0, did_something;
5018 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5019 const char *edit_script_path = NULL;
5020 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5021 struct got_object_id_queue commits;
5022 struct got_pathlist_head merged_paths;
5023 const struct got_object_id_queue *parent_ids;
5024 struct got_object_qid *pid;
5025 struct got_histedit_list histedit_cmds;
5026 struct got_histedit_list_entry *hle;
5028 SIMPLEQ_INIT(&commits);
5029 TAILQ_INIT(&histedit_cmds);
5030 TAILQ_INIT(&merged_paths);
5032 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5041 edit_script_path = optarg;
5053 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5054 "unveil", NULL) == -1)
5057 if (abort_edit && continue_edit)
5063 * This command cannot apply unveil(2) in all cases because the
5064 * user may choose to run an editor to edit the histedit script
5065 * and to edit individual commit log messages.
5066 * unveil(2) traverses exec(2); if an editor is used we have to
5067 * apply unveil after edit script and log messages have been written.
5068 * XXX TODO: Make use of unveil(2) where possible.
5071 cwd = getcwd(NULL, 0);
5073 error = got_error_from_errno("getcwd");
5076 error = got_worktree_open(&worktree, cwd);
5080 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5084 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5087 if (rebase_in_progress) {
5088 error = got_error(GOT_ERR_REBASING);
5092 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5096 if (edit_in_progress && abort_edit) {
5097 error = got_worktree_histedit_continue(&resume_commit_id,
5098 &tmp_branch, &branch, &base_commit_id, &fileindex,
5102 printf("Switching work tree to %s\n",
5103 got_ref_get_symref_target(branch));
5104 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5105 branch, base_commit_id, update_progress, &did_something);
5108 printf("Histedit of %s aborted\n",
5109 got_ref_get_symref_target(branch));
5110 goto done; /* nothing else to do */
5111 } else if (abort_edit) {
5112 error = got_error(GOT_ERR_NOT_HISTEDIT);
5116 if (continue_edit) {
5119 if (!edit_in_progress) {
5120 error = got_error(GOT_ERR_NOT_HISTEDIT);
5124 error = got_worktree_get_histedit_script_path(&path, worktree);
5128 error = histedit_load_list(&histedit_cmds, path, repo);
5133 error = got_worktree_histedit_continue(&resume_commit_id,
5134 &tmp_branch, &branch, &base_commit_id, &fileindex,
5139 error = got_ref_resolve(&head_commit_id, repo, branch);
5143 error = got_object_open_as_commit(&commit, repo,
5147 parent_ids = got_object_commit_get_parent_ids(commit);
5148 pid = SIMPLEQ_FIRST(parent_ids);
5150 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5153 error = collect_commits(&commits, head_commit_id, pid->id,
5154 base_commit_id, got_worktree_get_path_prefix(worktree),
5155 GOT_ERR_HISTEDIT_PATH, repo);
5156 got_object_commit_close(commit);
5161 if (edit_in_progress) {
5162 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5166 error = got_ref_open(&branch, repo,
5167 got_worktree_get_head_ref_name(worktree), 0);
5171 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5172 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5173 "will not edit commit history of a branch outside "
5174 "the \"refs/heads/\" reference namespace");
5178 error = got_ref_resolve(&head_commit_id, repo, branch);
5179 got_ref_close(branch);
5184 error = got_object_open_as_commit(&commit, repo,
5188 parent_ids = got_object_commit_get_parent_ids(commit);
5189 pid = SIMPLEQ_FIRST(parent_ids);
5191 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5194 error = collect_commits(&commits, head_commit_id, pid->id,
5195 got_worktree_get_base_commit_id(worktree),
5196 got_worktree_get_path_prefix(worktree),
5197 GOT_ERR_HISTEDIT_PATH, repo);
5198 got_object_commit_close(commit);
5203 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5204 &base_commit_id, &fileindex, worktree, repo);
5208 if (edit_script_path) {
5209 error = histedit_load_list(&histedit_cmds,
5210 edit_script_path, repo);
5212 got_worktree_histedit_abort(worktree, fileindex,
5213 repo, branch, base_commit_id,
5214 update_progress, &did_something);
5218 error = histedit_edit_script(&histedit_cmds, &commits,
5221 got_worktree_histedit_abort(worktree, fileindex,
5222 repo, branch, base_commit_id,
5223 update_progress, &did_something);
5229 error = histedit_save_list(&histedit_cmds, worktree,
5232 got_worktree_histedit_abort(worktree, fileindex,
5233 repo, branch, base_commit_id,
5234 update_progress, &did_something);
5240 error = histedit_check_script(&histedit_cmds, &commits, repo);
5244 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5245 if (resume_commit_id) {
5246 if (got_object_id_cmp(hle->commit_id,
5247 resume_commit_id) != 0)
5250 resume_commit_id = NULL;
5251 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5252 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5253 error = histedit_skip_commit(hle, worktree,
5256 error = histedit_commit(NULL, worktree,
5257 fileindex, tmp_branch, hle, repo);
5264 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5265 error = histedit_skip_commit(hle, worktree, repo);
5271 error = got_object_open_as_commit(&commit, repo,
5275 parent_ids = got_object_commit_get_parent_ids(commit);
5276 pid = SIMPLEQ_FIRST(parent_ids);
5278 error = got_worktree_histedit_merge_files(&merged_paths,
5279 worktree, fileindex, pid->id, hle->commit_id, repo,
5280 rebase_progress, &rebase_status, check_cancelled, NULL);
5283 got_object_commit_close(commit);
5286 if (rebase_status == GOT_STATUS_CONFLICT) {
5287 got_worktree_rebase_pathlist_free(&merged_paths);
5291 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5293 error = got_object_id_str(&id_str, hle->commit_id);
5296 printf("Stopping histedit for amending commit %s\n",
5299 got_worktree_rebase_pathlist_free(&merged_paths);
5300 error = got_worktree_histedit_postpone(worktree,
5305 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5306 error = histedit_skip_commit(hle, worktree, repo);
5312 error = histedit_commit(&merged_paths, worktree, fileindex,
5313 tmp_branch, hle, repo);
5314 got_worktree_rebase_pathlist_free(&merged_paths);
5319 if (rebase_status == GOT_STATUS_CONFLICT) {
5320 error = got_worktree_histedit_postpone(worktree, fileindex);
5323 error = got_error_msg(GOT_ERR_CONFLICTS,
5324 "conflicts must be resolved before rebasing can continue");
5326 error = histedit_complete(worktree, fileindex, tmp_branch,
5329 got_object_id_queue_free(&commits);
5330 histedit_free_list(&histedit_cmds);
5331 free(head_commit_id);
5332 free(base_commit_id);
5333 free(resume_commit_id);
5335 got_object_commit_close(commit);
5337 got_ref_close(branch);
5339 got_ref_close(tmp_branch);
5341 got_worktree_close(worktree);
5343 got_repo_close(repo);
5350 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5351 "[file-path ...]\n",
5356 static const struct got_error *
5357 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5358 const char *path, struct got_object_id *blob_id,
5359 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5361 const struct got_error *err = NULL;
5362 char *id_str = NULL;
5364 if (staged_status != GOT_STATUS_ADD &&
5365 staged_status != GOT_STATUS_MODIFY &&
5366 staged_status != GOT_STATUS_DELETE)
5369 if (staged_status == GOT_STATUS_ADD ||
5370 staged_status == GOT_STATUS_MODIFY)
5371 err = got_object_id_str(&id_str, staged_blob_id);
5373 err = got_object_id_str(&id_str, blob_id);
5377 printf("%s %c %s\n", id_str, staged_status, path);
5382 static const struct got_error *
5383 cmd_stage(int argc, char *argv[])
5385 const struct got_error *error = NULL;
5386 struct got_repository *repo = NULL;
5387 struct got_worktree *worktree = NULL;
5389 struct got_pathlist_head paths;
5390 struct got_pathlist_entry *pe;
5391 int ch, list_stage = 0, pflag = 0;
5392 FILE *patch_script_file = NULL;
5393 const char *patch_script_path = NULL;
5394 struct choose_patch_arg cpa;
5398 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5407 patch_script_path = optarg;
5419 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5420 "unveil", NULL) == -1)
5423 if (list_stage && (pflag || patch_script_path))
5424 errx(1, "-l option cannot be used with other options");
5425 if (patch_script_path && !pflag)
5426 errx(1, "-F option can only be used together with -p option");
5428 cwd = getcwd(NULL, 0);
5430 error = got_error_from_errno("getcwd");
5434 error = got_worktree_open(&worktree, cwd);
5438 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5442 if (patch_script_path) {
5443 patch_script_file = fopen(patch_script_path, "r");
5444 if (patch_script_file == NULL) {
5445 error = got_error_from_errno2("fopen",
5450 error = apply_unveil(got_repo_get_path(repo), 1,
5451 got_worktree_get_root_path(worktree));
5455 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5460 error = got_worktree_status(worktree, &paths, repo,
5461 print_stage, NULL, check_cancelled, NULL);
5463 cpa.patch_script_file = patch_script_file;
5464 cpa.action = "stage";
5465 error = got_worktree_stage(worktree, &paths,
5466 pflag ? NULL : print_status, NULL,
5467 pflag ? choose_patch : NULL, &cpa, repo);
5470 if (patch_script_file && fclose(patch_script_file) == EOF &&
5472 error = got_error_from_errno2("fclose", patch_script_path);
5474 got_repo_close(repo);
5476 got_worktree_close(worktree);
5477 TAILQ_FOREACH(pe, &paths, entry)
5478 free((char *)pe->path);
5479 got_pathlist_free(&paths);
5487 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5488 "[file-path ...]\n",
5494 static const struct got_error *
5495 cmd_unstage(int argc, char *argv[])
5497 const struct got_error *error = NULL;
5498 struct got_repository *repo = NULL;
5499 struct got_worktree *worktree = NULL;
5501 struct got_pathlist_head paths;
5502 struct got_pathlist_entry *pe;
5503 int ch, did_something = 0, pflag = 0;
5504 FILE *patch_script_file = NULL;
5505 const char *patch_script_path = NULL;
5506 struct choose_patch_arg cpa;
5510 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5516 patch_script_path = optarg;
5528 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5529 "unveil", NULL) == -1)
5532 if (patch_script_path && !pflag)
5533 errx(1, "-F option can only be used together with -p option");
5535 cwd = getcwd(NULL, 0);
5537 error = got_error_from_errno("getcwd");
5541 error = got_worktree_open(&worktree, cwd);
5545 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5549 if (patch_script_path) {
5550 patch_script_file = fopen(patch_script_path, "r");
5551 if (patch_script_file == NULL) {
5552 error = got_error_from_errno2("fopen",
5558 error = apply_unveil(got_repo_get_path(repo), 1,
5559 got_worktree_get_root_path(worktree));
5563 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5567 cpa.patch_script_file = patch_script_file;
5568 cpa.action = "unstage";
5569 error = got_worktree_unstage(worktree, &paths, update_progress,
5570 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5572 if (patch_script_file && fclose(patch_script_file) == EOF &&
5574 error = got_error_from_errno2("fclose", patch_script_path);
5576 got_repo_close(repo);
5578 got_worktree_close(worktree);
5579 TAILQ_FOREACH(pe, &paths, entry)
5580 free((char *)pe->path);
5581 got_pathlist_free(&paths);