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;
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)
1760 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1762 else if (status != GOT_STATUS_ADD)
1763 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1767 if (status != GOT_STATUS_DELETE) {
1768 if (asprintf(&abspath, "%s/%s",
1769 got_worktree_get_root_path(a->worktree), path) == -1) {
1770 err = got_error_from_errno("asprintf");
1774 f2 = fopen(abspath, "r");
1776 err = got_error_from_errno2("fopen", abspath);
1779 if (lstat(abspath, &sb) == -1) {
1780 err = got_error_from_errno2("lstat", abspath);
1786 err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1790 got_object_blob_close(blob1);
1791 if (f2 && fclose(f2) != 0 && err == NULL)
1792 err = got_error_from_errno("fclose");
1797 static const struct got_error *
1798 cmd_diff(int argc, char *argv[])
1800 const struct got_error *error;
1801 struct got_repository *repo = NULL;
1802 struct got_worktree *worktree = NULL;
1803 char *cwd = NULL, *repo_path = NULL;
1804 struct got_object_id *id1 = NULL, *id2 = NULL;
1805 const char *id_str1 = NULL, *id_str2 = NULL;
1806 char *label1 = NULL, *label2 = NULL;
1808 int diff_context = 3, diff_staged = 0, ch;
1813 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1818 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1821 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1823 err(1, "-C option %s", errstr);
1826 repo_path = realpath(optarg, NULL);
1827 if (repo_path == NULL)
1828 err(1, "-r option");
1829 got_path_strip_trailing_slashes(repo_path);
1843 cwd = getcwd(NULL, 0);
1845 error = got_error_from_errno("getcwd");
1848 error = got_worktree_open(&worktree, cwd);
1849 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1852 if (worktree == NULL) {
1853 error = got_error(GOT_ERR_NOT_WORKTREE);
1858 "-r option can't be used when diffing a work tree");
1859 repo_path = strdup(got_worktree_get_repo_path(worktree));
1860 if (repo_path == NULL) {
1861 error = got_error_from_errno("strdup");
1865 error = got_worktree_resolve_path(&path, worktree,
1872 error = got_error_from_errno("strdup");
1876 } else if (argc == 2) {
1878 errx(1, "-s option can't be used when diffing "
1879 "objects in repository");
1882 if (worktree && repo_path == NULL) {
1884 strdup(got_worktree_get_repo_path(worktree));
1885 if (repo_path == NULL) {
1886 error = got_error_from_errno("strdup");
1893 if (repo_path == NULL) {
1894 repo_path = getcwd(NULL, 0);
1895 if (repo_path == NULL)
1896 return got_error_from_errno("getcwd");
1899 error = got_repo_open(&repo, repo_path);
1904 error = apply_unveil(got_repo_get_path(repo), 1,
1905 worktree ? got_worktree_get_root_path(worktree) : NULL);
1910 struct print_diff_arg arg;
1911 struct got_pathlist_head paths;
1916 error = got_object_id_str(&id_str,
1917 got_worktree_get_base_commit_id(worktree));
1921 arg.worktree = worktree;
1922 arg.diff_context = diff_context;
1923 arg.id_str = id_str;
1924 arg.header_shown = 0;
1925 arg.diff_staged = diff_staged;
1927 error = got_pathlist_append(&paths, path, NULL);
1931 error = got_worktree_status(worktree, &paths, repo, print_diff,
1932 &arg, check_cancelled, NULL);
1934 got_pathlist_free(&paths);
1938 error = got_repo_match_object_id_prefix(&id1, id_str1,
1939 GOT_OBJ_TYPE_ANY, repo);
1941 struct got_reference *ref;
1942 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1944 error = got_ref_open(&ref, repo, id_str1, 0);
1947 label1 = strdup(got_ref_get_name(ref));
1948 if (label1 == NULL) {
1949 error = got_error_from_errno("strdup");
1952 error = got_ref_resolve(&id1, repo, ref);
1957 error = got_object_id_str(&label1, id1);
1958 if (label1 == NULL) {
1959 error = got_error_from_errno("strdup");
1964 error = got_repo_match_object_id_prefix(&id2, id_str2,
1965 GOT_OBJ_TYPE_ANY, repo);
1967 struct got_reference *ref;
1968 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1970 error = got_ref_open(&ref, repo, id_str2, 0);
1973 label2 = strdup(got_ref_get_name(ref));
1974 if (label2 == NULL) {
1975 error = got_error_from_errno("strdup");
1978 error = got_ref_resolve(&id2, repo, ref);
1983 error = got_object_id_str(&label2, id2);
1984 if (label2 == NULL) {
1985 error = got_error_from_errno("strdup");
1990 error = got_object_get_type(&type1, repo, id1);
1994 error = got_object_get_type(&type2, repo, id2);
1998 if (type1 != type2) {
1999 error = got_error(GOT_ERR_OBJ_TYPE);
2004 case GOT_OBJ_TYPE_BLOB:
2005 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2006 diff_context, repo, stdout);
2008 case GOT_OBJ_TYPE_TREE:
2009 error = got_diff_objects_as_trees(id1, id2, "", "",
2010 diff_context, repo, stdout);
2012 case GOT_OBJ_TYPE_COMMIT:
2013 printf("diff %s %s\n", label1, label2);
2014 error = got_diff_objects_as_commits(id1, id2, diff_context,
2018 error = got_error(GOT_ERR_OBJ_TYPE);
2028 got_worktree_close(worktree);
2030 const struct got_error *repo_error;
2031 repo_error = got_repo_close(repo);
2042 "usage: %s blame [-c commit] [-r repository-path] path\n",
2047 static const struct got_error *
2048 cmd_blame(int argc, char *argv[])
2050 const struct got_error *error;
2051 struct got_repository *repo = NULL;
2052 struct got_worktree *worktree = NULL;
2053 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2054 struct got_object_id *commit_id = NULL;
2055 char *commit_id_str = NULL;
2059 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2064 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2067 commit_id_str = optarg;
2070 repo_path = realpath(optarg, NULL);
2071 if (repo_path == NULL)
2072 err(1, "-r option");
2073 got_path_strip_trailing_slashes(repo_path);
2089 cwd = getcwd(NULL, 0);
2091 error = got_error_from_errno("getcwd");
2094 if (repo_path == NULL) {
2095 error = got_worktree_open(&worktree, cwd);
2096 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2102 strdup(got_worktree_get_repo_path(worktree));
2103 if (repo_path == NULL)
2104 error = got_error_from_errno("strdup");
2108 repo_path = strdup(cwd);
2109 if (repo_path == NULL) {
2110 error = got_error_from_errno("strdup");
2116 error = got_repo_open(&repo, repo_path);
2120 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2125 const char *prefix = got_worktree_get_path_prefix(worktree);
2126 char *p, *worktree_subdir = cwd +
2127 strlen(got_worktree_get_root_path(worktree));
2128 if (asprintf(&p, "%s%s%s%s%s",
2129 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2130 worktree_subdir, worktree_subdir[0] ? "/" : "",
2132 error = got_error_from_errno("asprintf");
2135 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2138 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2143 if (commit_id_str == NULL) {
2144 struct got_reference *head_ref;
2145 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2148 error = got_ref_resolve(&commit_id, repo, head_ref);
2149 got_ref_close(head_ref);
2153 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2158 error = got_blame(in_repo_path, commit_id, repo, stdout);
2165 got_worktree_close(worktree);
2167 const struct got_error *repo_error;
2168 repo_error = got_repo_close(repo);
2179 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2185 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2186 const char *root_path)
2188 int is_root_path = (strcmp(path, root_path) == 0);
2190 path += strlen(root_path);
2191 while (path[0] == '/')
2194 printf("%s%s%s%s%s\n", id ? id : "", path,
2195 is_root_path ? "" : "/", te->name,
2196 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2199 static const struct got_error *
2200 print_tree(const char *path, struct got_object_id *commit_id,
2201 int show_ids, int recurse, const char *root_path,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 struct got_object_id *tree_id = NULL;
2206 struct got_tree_object *tree = NULL;
2207 const struct got_tree_entries *entries;
2208 struct got_tree_entry *te;
2210 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2214 err = got_object_open_as_tree(&tree, repo, tree_id);
2217 entries = got_object_tree_get_entries(tree);
2218 te = SIMPLEQ_FIRST(&entries->head);
2222 if (sigint_received || sigpipe_received)
2227 err = got_object_id_str(&id_str, te->id);
2230 if (asprintf(&id, "%s ", id_str) == -1) {
2231 err = got_error_from_errno("asprintf");
2237 print_entry(te, id, path, root_path);
2240 if (recurse && S_ISDIR(te->mode)) {
2242 if (asprintf(&child_path, "%s%s%s", path,
2243 path[0] == '/' && path[1] == '\0' ? "" : "/",
2245 err = got_error_from_errno("asprintf");
2248 err = print_tree(child_path, commit_id, show_ids, 1,
2255 te = SIMPLEQ_NEXT(te, entry);
2259 got_object_tree_close(tree);
2264 static const struct got_error *
2265 cmd_tree(int argc, char *argv[])
2267 const struct got_error *error;
2268 struct got_repository *repo = NULL;
2269 struct got_worktree *worktree = NULL;
2271 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2272 struct got_object_id *commit_id = NULL;
2273 char *commit_id_str = NULL;
2274 int show_ids = 0, recurse = 0;
2278 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2283 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2286 commit_id_str = optarg;
2289 repo_path = realpath(optarg, NULL);
2290 if (repo_path == NULL)
2291 err(1, "-r option");
2292 got_path_strip_trailing_slashes(repo_path);
2316 cwd = getcwd(NULL, 0);
2318 error = got_error_from_errno("getcwd");
2321 if (repo_path == NULL) {
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2329 strdup(got_worktree_get_repo_path(worktree));
2330 if (repo_path == NULL)
2331 error = got_error_from_errno("strdup");
2335 repo_path = strdup(cwd);
2336 if (repo_path == NULL) {
2337 error = got_error_from_errno("strdup");
2343 error = got_repo_open(&repo, repo_path);
2347 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2353 char *p, *worktree_subdir = cwd +
2354 strlen(got_worktree_get_root_path(worktree));
2355 if (asprintf(&p, "%s/%s",
2356 got_worktree_get_path_prefix(worktree),
2357 worktree_subdir) == -1) {
2358 error = got_error_from_errno("asprintf");
2361 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2368 if (in_repo_path == NULL) {
2369 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2374 if (commit_id_str == NULL) {
2375 struct got_reference *head_ref;
2376 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2379 error = got_ref_resolve(&commit_id, repo, head_ref);
2380 got_ref_close(head_ref);
2384 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2389 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2390 in_repo_path, repo);
2397 got_worktree_close(worktree);
2399 const struct got_error *repo_error;
2400 repo_error = got_repo_close(repo);
2410 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2414 static const struct got_error *
2415 print_status(void *arg, unsigned char status, unsigned char staged_status,
2416 const char *path, struct got_object_id *blob_id,
2417 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2419 if (status == staged_status && (status == GOT_STATUS_DELETE))
2420 status = GOT_STATUS_NO_CHANGE;
2421 printf("%c%c %s\n", status, staged_status, path);
2425 static const struct got_error *
2426 cmd_status(int argc, char *argv[])
2428 const struct got_error *error = NULL;
2429 struct got_repository *repo = NULL;
2430 struct got_worktree *worktree = NULL;
2432 struct got_pathlist_head paths;
2433 struct got_pathlist_entry *pe;
2438 while ((ch = getopt(argc, argv, "")) != -1) {
2450 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2454 cwd = getcwd(NULL, 0);
2456 error = got_error_from_errno("getcwd");
2460 error = got_worktree_open(&worktree, cwd);
2464 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2468 error = apply_unveil(got_repo_get_path(repo), 1,
2469 got_worktree_get_root_path(worktree));
2473 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2477 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2478 check_cancelled, NULL);
2480 TAILQ_FOREACH(pe, &paths, entry)
2481 free((char *)pe->path);
2482 got_pathlist_free(&paths);
2491 "usage: %s ref [-r repository] -l | -d name | name target\n",
2496 static const struct got_error *
2497 list_refs(struct got_repository *repo)
2499 static const struct got_error *err = NULL;
2500 struct got_reflist_head refs;
2501 struct got_reflist_entry *re;
2503 SIMPLEQ_INIT(&refs);
2504 err = got_ref_list(&refs, repo);
2508 SIMPLEQ_FOREACH(re, &refs, entry) {
2510 refstr = got_ref_to_str(re->ref);
2512 return got_error_from_errno("got_ref_to_str");
2513 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2517 got_ref_list_free(&refs);
2521 static const struct got_error *
2522 delete_ref(struct got_repository *repo, const char *refname)
2524 const struct got_error *err = NULL;
2525 struct got_reference *ref;
2527 err = got_ref_open(&ref, repo, refname, 0);
2531 err = got_ref_delete(ref, repo);
2536 static const struct got_error *
2537 add_ref(struct got_repository *repo, const char *refname, const char *target)
2539 const struct got_error *err = NULL;
2540 struct got_object_id *id;
2541 struct got_reference *ref = NULL;
2544 * Don't let the user create a reference named '-'.
2545 * While technically a valid reference name, this case is usually
2546 * an unintended typo.
2548 if (refname[0] == '-' && refname[1] == '\0')
2549 return got_error(GOT_ERR_BAD_REF_NAME);
2551 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2554 struct got_reference *target_ref;
2556 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2558 err = got_ref_open(&target_ref, repo, target, 0);
2561 err = got_ref_resolve(&id, repo, target_ref);
2562 got_ref_close(target_ref);
2567 err = got_ref_alloc(&ref, refname, id);
2571 err = got_ref_write(ref, repo);
2579 static const struct got_error *
2580 cmd_ref(int argc, char *argv[])
2582 const struct got_error *error = NULL;
2583 struct got_repository *repo = NULL;
2584 struct got_worktree *worktree = NULL;
2585 char *cwd = NULL, *repo_path = NULL;
2586 int ch, do_list = 0;
2587 const char *delref = NULL;
2589 /* TODO: Add -s option for adding symbolic references. */
2590 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2596 repo_path = realpath(optarg, NULL);
2597 if (repo_path == NULL)
2598 err(1, "-r option");
2599 got_path_strip_trailing_slashes(repo_path);
2610 if (do_list && delref)
2611 errx(1, "-l and -d options are mutually exclusive\n");
2616 if (do_list || delref) {
2619 } else if (argc != 2)
2624 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2628 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2629 "sendfd unveil", NULL) == -1)
2633 cwd = getcwd(NULL, 0);
2635 error = got_error_from_errno("getcwd");
2639 if (repo_path == NULL) {
2640 error = got_worktree_open(&worktree, cwd);
2641 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2647 strdup(got_worktree_get_repo_path(worktree));
2648 if (repo_path == NULL)
2649 error = got_error_from_errno("strdup");
2653 repo_path = strdup(cwd);
2654 if (repo_path == NULL) {
2655 error = got_error_from_errno("strdup");
2661 error = got_repo_open(&repo, repo_path);
2665 error = apply_unveil(got_repo_get_path(repo), do_list,
2666 worktree ? got_worktree_get_root_path(worktree) : NULL);
2671 error = list_refs(repo);
2673 error = delete_ref(repo, delref);
2675 error = add_ref(repo, argv[0], argv[1]);
2678 got_repo_close(repo);
2680 got_worktree_close(worktree);
2690 "usage: %s branch [-r repository] -l | -d name | "
2691 "name [base-branch]\n", getprogname());
2695 static const struct got_error *
2696 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2698 static const struct got_error *err = NULL;
2699 struct got_reflist_head refs;
2700 struct got_reflist_entry *re;
2702 SIMPLEQ_INIT(&refs);
2704 err = got_ref_list(&refs, repo);
2708 SIMPLEQ_FOREACH(re, &refs, entry) {
2709 const char *refname, *marker = " ";
2711 refname = got_ref_get_name(re->ref);
2712 if (strncmp(refname, "refs/heads/", 11) != 0)
2714 if (worktree && strcmp(refname,
2715 got_worktree_get_head_ref_name(worktree)) == 0) {
2716 struct got_object_id *id = NULL;
2717 err = got_ref_resolve(&id, repo, re->ref);
2720 if (got_object_id_cmp(id,
2721 got_worktree_get_base_commit_id(worktree)) == 0)
2728 refstr = got_ref_to_str(re->ref);
2730 return got_error_from_errno("got_ref_to_str");
2731 printf("%s%s: %s\n", marker, refname, refstr);
2735 got_ref_list_free(&refs);
2739 static const struct got_error *
2740 delete_branch(struct got_repository *repo, const char *branch_name)
2742 const struct got_error *err = NULL;
2743 struct got_reference *ref;
2746 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2747 return got_error_from_errno("asprintf");
2749 err = got_ref_open(&ref, repo, refname, 0);
2753 err = got_ref_delete(ref, repo);
2760 static const struct got_error *
2761 add_branch(struct got_repository *repo, const char *branch_name,
2762 const char *base_branch)
2764 const struct got_error *err = NULL;
2765 struct got_object_id *id = NULL;
2766 struct got_reference *ref = NULL;
2767 char *base_refname = NULL, *refname = NULL;
2768 struct got_reference *base_ref;
2771 * Don't let the user create a branch named '-'.
2772 * While technically a valid reference name, this case is usually
2773 * an unintended typo.
2775 if (branch_name[0] == '-' && branch_name[1] == '\0')
2776 return got_error(GOT_ERR_BAD_REF_NAME);
2778 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2779 base_refname = strdup(GOT_REF_HEAD);
2780 if (base_refname == NULL)
2781 return got_error_from_errno("strdup");
2782 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2783 return got_error_from_errno("asprintf");
2785 err = got_ref_open(&base_ref, repo, base_refname, 0);
2788 err = got_ref_resolve(&id, repo, base_ref);
2789 got_ref_close(base_ref);
2793 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2794 err = got_error_from_errno("asprintf");
2798 err = got_ref_open(&ref, repo, refname, 0);
2800 err = got_error(GOT_ERR_BRANCH_EXISTS);
2802 } else if (err->code != GOT_ERR_NOT_REF)
2805 err = got_ref_alloc(&ref, refname, id);
2809 err = got_ref_write(ref, repo);
2819 static const struct got_error *
2820 cmd_branch(int argc, char *argv[])
2822 const struct got_error *error = NULL;
2823 struct got_repository *repo = NULL;
2824 struct got_worktree *worktree = NULL;
2825 char *cwd = NULL, *repo_path = NULL;
2826 int ch, do_list = 0;
2827 const char *delref = NULL;
2829 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2835 repo_path = realpath(optarg, NULL);
2836 if (repo_path == NULL)
2837 err(1, "-r option");
2838 got_path_strip_trailing_slashes(repo_path);
2849 if (do_list && delref)
2850 errx(1, "-l and -d options are mutually exclusive\n");
2855 if (do_list || delref) {
2858 } else if (argc < 1 || argc > 2)
2863 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2867 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2868 "sendfd unveil", NULL) == -1)
2872 cwd = getcwd(NULL, 0);
2874 error = got_error_from_errno("getcwd");
2878 if (repo_path == NULL) {
2879 error = got_worktree_open(&worktree, cwd);
2880 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2886 strdup(got_worktree_get_repo_path(worktree));
2887 if (repo_path == NULL)
2888 error = got_error_from_errno("strdup");
2892 repo_path = strdup(cwd);
2893 if (repo_path == NULL) {
2894 error = got_error_from_errno("strdup");
2900 error = got_repo_open(&repo, repo_path);
2904 error = apply_unveil(got_repo_get_path(repo), do_list,
2905 worktree ? got_worktree_get_root_path(worktree) : NULL);
2910 error = list_branches(repo, worktree);
2912 error = delete_branch(repo, delref);
2914 const char *base_branch;
2916 base_branch = worktree ?
2917 got_worktree_get_head_ref_name(worktree) :
2919 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2922 base_branch = argv[1];
2923 error = add_branch(repo, argv[0], base_branch);
2927 got_repo_close(repo);
2929 got_worktree_close(worktree);
2938 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2942 static const struct got_error *
2943 cmd_add(int argc, char *argv[])
2945 const struct got_error *error = NULL;
2946 struct got_repository *repo = NULL;
2947 struct got_worktree *worktree = NULL;
2949 struct got_pathlist_head paths;
2950 struct got_pathlist_entry *pe;
2955 while ((ch = getopt(argc, argv, "")) != -1) {
2967 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2974 cwd = getcwd(NULL, 0);
2976 error = got_error_from_errno("getcwd");
2980 error = got_worktree_open(&worktree, cwd);
2984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2988 error = apply_unveil(got_repo_get_path(repo), 1,
2989 got_worktree_get_root_path(worktree));
2993 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2997 error = got_worktree_schedule_add(worktree, &paths, print_status,
3001 got_repo_close(repo);
3003 got_worktree_close(worktree);
3004 TAILQ_FOREACH(pe, &paths, entry)
3005 free((char *)pe->path);
3006 got_pathlist_free(&paths);
3014 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3018 static const struct got_error *
3019 cmd_remove(int argc, char *argv[])
3021 const struct got_error *error = NULL;
3022 struct got_worktree *worktree = NULL;
3023 struct got_repository *repo = NULL;
3025 struct got_pathlist_head paths;
3026 struct got_pathlist_entry *pe;
3027 int ch, delete_local_mods = 0;
3031 while ((ch = getopt(argc, argv, "f")) != -1) {
3034 delete_local_mods = 1;
3046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3053 cwd = getcwd(NULL, 0);
3055 error = got_error_from_errno("getcwd");
3058 error = got_worktree_open(&worktree, cwd);
3062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3066 error = apply_unveil(got_repo_get_path(repo), 1,
3067 got_worktree_get_root_path(worktree));
3071 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3075 error = got_worktree_schedule_delete(worktree, &paths,
3076 delete_local_mods, print_status, NULL, repo);
3081 got_repo_close(repo);
3083 got_worktree_close(worktree);
3084 TAILQ_FOREACH(pe, &paths, entry)
3085 free((char *)pe->path);
3086 got_pathlist_free(&paths);
3094 fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3098 static const struct got_error *
3099 revert_progress(void *arg, unsigned char status, const char *path)
3101 while (path[0] == '/')
3103 printf("%c %s\n", status, path);
3107 static const struct got_error *
3108 cmd_revert(int argc, char *argv[])
3110 const struct got_error *error = NULL;
3111 struct got_worktree *worktree = NULL;
3112 struct got_repository *repo = NULL;
3113 char *cwd = NULL, *path = NULL;
3114 struct got_pathlist_head paths;
3119 while ((ch = getopt(argc, argv, "")) != -1) {
3131 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3132 "unveil", NULL) == -1)
3138 cwd = getcwd(NULL, 0);
3140 error = got_error_from_errno("getcwd");
3143 error = got_worktree_open(&worktree, cwd);
3147 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3151 error = apply_unveil(got_repo_get_path(repo), 1,
3152 got_worktree_get_root_path(worktree));
3156 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3160 error = got_worktree_revert(worktree, &paths,
3161 revert_progress, NULL, repo);
3166 got_repo_close(repo);
3168 got_worktree_close(worktree);
3177 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3182 struct collect_commit_logmsg_arg {
3183 const char *cmdline_log;
3185 const char *worktree_path;
3186 const char *branch_name;
3187 const char *repo_path;
3192 static const struct got_error *
3193 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3196 char *initial_content = NULL;
3197 struct got_pathlist_entry *pe;
3198 const struct got_error *err = NULL;
3199 char *template = NULL;
3200 struct collect_commit_logmsg_arg *a = arg;
3204 /* if a message was specified on the command line, just use it */
3205 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3206 len = strlen(a->cmdline_log) + 1;
3207 *logmsg = malloc(len + 1);
3208 if (*logmsg == NULL)
3209 return got_error_from_errno("malloc");
3210 strlcpy(*logmsg, a->cmdline_log, len);
3214 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3215 return got_error_from_errno("asprintf");
3217 if (asprintf(&initial_content,
3218 "\n# changes to be committed on branch %s:\n",
3219 a->branch_name) == -1)
3220 return got_error_from_errno("asprintf");
3222 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3226 dprintf(fd, initial_content);
3228 TAILQ_FOREACH(pe, commitable_paths, entry) {
3229 struct got_commitable *ct = pe->data;
3230 dprintf(fd, "# %c %s\n",
3231 got_commitable_get_status(ct),
3232 got_commitable_get_path(ct));
3236 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3238 unlink(a->logmsg_path);
3239 free(a->logmsg_path);
3240 free(initial_content);
3243 /* Editor is done; we can now apply unveil(2) */
3245 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3254 static const struct got_error *
3255 cmd_commit(int argc, char *argv[])
3257 const struct got_error *error = NULL;
3258 struct got_worktree *worktree = NULL;
3259 struct got_repository *repo = NULL;
3260 char *cwd = NULL, *id_str = NULL;
3261 struct got_object_id *id = NULL;
3262 const char *logmsg = NULL;
3263 const char *got_author = getenv("GOT_AUTHOR");
3264 struct collect_commit_logmsg_arg cl_arg;
3265 char *editor = NULL;
3266 int ch, rebase_in_progress, histedit_in_progress;
3267 struct got_pathlist_head paths;
3271 while ((ch = getopt(argc, argv, "m:")) != -1) {
3286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3287 "unveil", NULL) == -1)
3290 if (got_author == NULL) {
3291 /* TODO: Look current user up in password database */
3292 error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3296 cwd = getcwd(NULL, 0);
3298 error = got_error_from_errno("getcwd");
3301 error = got_worktree_open(&worktree, cwd);
3305 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3308 if (rebase_in_progress) {
3309 error = got_error(GOT_ERR_REBASING);
3313 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3323 * unveil(2) traverses exec(2); if an editor is used we have
3324 * to apply unveil after the log message has been written.
3326 if (logmsg == NULL || strlen(logmsg) == 0)
3327 error = get_editor(&editor);
3329 error = apply_unveil(got_repo_get_path(repo), 0,
3330 got_worktree_get_root_path(worktree));
3334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3338 cl_arg.editor = editor;
3339 cl_arg.cmdline_log = logmsg;
3340 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3341 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3342 if (!histedit_in_progress) {
3343 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3344 error = got_error(GOT_ERR_COMMIT_BRANCH);
3347 cl_arg.branch_name += 11;
3349 cl_arg.repo_path = got_repo_get_path(repo);
3350 cl_arg.logmsg_path = NULL;
3351 error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3352 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3354 if (cl_arg.logmsg_path)
3355 fprintf(stderr, "%s: log message preserved in %s\n",
3356 getprogname(), cl_arg.logmsg_path);
3360 if (cl_arg.logmsg_path)
3361 unlink(cl_arg.logmsg_path);
3363 error = got_object_id_str(&id_str, id);
3366 printf("Created commit %s\n", id_str);
3369 got_repo_close(repo);
3371 got_worktree_close(worktree);
3379 usage_cherrypick(void)
3381 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3385 static const struct got_error *
3386 cmd_cherrypick(int argc, char *argv[])
3388 const struct got_error *error = NULL;
3389 struct got_worktree *worktree = NULL;
3390 struct got_repository *repo = NULL;
3391 char *cwd = NULL, *commit_id_str = NULL;
3392 struct got_object_id *commit_id = NULL;
3393 struct got_commit_object *commit = NULL;
3394 struct got_object_qid *pid;
3395 struct got_reference *head_ref = NULL;
3396 int ch, did_something = 0;
3398 while ((ch = getopt(argc, argv, "")) != -1) {
3410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3411 "unveil", NULL) == -1)
3417 cwd = getcwd(NULL, 0);
3419 error = got_error_from_errno("getcwd");
3422 error = got_worktree_open(&worktree, cwd);
3426 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3430 error = apply_unveil(got_repo_get_path(repo), 0,
3431 got_worktree_get_root_path(worktree));
3435 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3436 GOT_OBJ_TYPE_COMMIT, repo);
3437 if (error != NULL) {
3438 struct got_reference *ref;
3439 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3441 error = got_ref_open(&ref, repo, argv[0], 0);
3444 error = got_ref_resolve(&commit_id, repo, ref);
3449 error = got_object_id_str(&commit_id_str, commit_id);
3453 error = got_ref_open(&head_ref, repo,
3454 got_worktree_get_head_ref_name(worktree), 0);
3458 error = check_same_branch(commit_id, head_ref, NULL, repo);
3460 if (error->code != GOT_ERR_ANCESTRY)
3464 error = got_error(GOT_ERR_SAME_BRANCH);
3468 error = got_object_open_as_commit(&commit, repo, commit_id);
3471 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3472 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3473 commit_id, repo, update_progress, &did_something, check_cancelled,
3479 printf("Merged commit %s\n", commit_id_str);
3482 got_object_commit_close(commit);
3483 free(commit_id_str);
3485 got_ref_close(head_ref);
3487 got_worktree_close(worktree);
3489 got_repo_close(repo);
3496 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3500 static const struct got_error *
3501 cmd_backout(int argc, char *argv[])
3503 const struct got_error *error = NULL;
3504 struct got_worktree *worktree = NULL;
3505 struct got_repository *repo = NULL;
3506 char *cwd = NULL, *commit_id_str = NULL;
3507 struct got_object_id *commit_id = NULL;
3508 struct got_commit_object *commit = NULL;
3509 struct got_object_qid *pid;
3510 struct got_reference *head_ref = NULL;
3511 int ch, did_something = 0;
3513 while ((ch = getopt(argc, argv, "")) != -1) {
3525 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3526 "unveil", NULL) == -1)
3532 cwd = getcwd(NULL, 0);
3534 error = got_error_from_errno("getcwd");
3537 error = got_worktree_open(&worktree, cwd);
3541 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3545 error = apply_unveil(got_repo_get_path(repo), 0,
3546 got_worktree_get_root_path(worktree));
3550 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3551 GOT_OBJ_TYPE_COMMIT, repo);
3552 if (error != NULL) {
3553 struct got_reference *ref;
3554 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3556 error = got_ref_open(&ref, repo, argv[0], 0);
3559 error = got_ref_resolve(&commit_id, repo, ref);
3564 error = got_object_id_str(&commit_id_str, commit_id);
3568 error = got_ref_open(&head_ref, repo,
3569 got_worktree_get_head_ref_name(worktree), 0);
3573 error = check_same_branch(commit_id, head_ref, NULL, repo);
3577 error = got_object_open_as_commit(&commit, repo, commit_id);
3580 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3582 error = got_error(GOT_ERR_ROOT_COMMIT);
3586 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3587 update_progress, &did_something, check_cancelled, NULL);
3592 printf("Backed out commit %s\n", commit_id_str);
3595 got_object_commit_close(commit);
3596 free(commit_id_str);
3598 got_ref_close(head_ref);
3600 got_worktree_close(worktree);
3602 got_repo_close(repo);
3609 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3615 trim_logmsg(char *logmsg, int limit)
3620 len = strlen(logmsg);
3624 nl = strchr(logmsg, '\n');
3629 static const struct got_error *
3630 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3632 const char *logmsg0 = NULL;
3634 logmsg0 = got_object_commit_get_logmsg(commit);
3636 while (isspace((unsigned char)logmsg0[0]))
3639 *logmsg = strdup(logmsg0);
3640 if (*logmsg == NULL)
3641 return got_error_from_errno("strdup");
3643 trim_logmsg(*logmsg, limit);
3647 static const struct got_error *
3648 show_rebase_progress(struct got_commit_object *commit,
3649 struct got_object_id *old_id, struct got_object_id *new_id)
3651 const struct got_error *err;
3652 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3654 err = got_object_id_str(&old_id_str, old_id);
3659 err = got_object_id_str(&new_id_str, new_id);
3664 old_id_str[12] = '\0';
3666 new_id_str[12] = '\0';
3668 err = get_short_logmsg(&logmsg, 42, commit);
3672 printf("%s -> %s: %s\n", old_id_str,
3673 new_id_str ? new_id_str : "no-op change", logmsg);
3680 static const struct got_error *
3681 rebase_progress(void *arg, unsigned char status, const char *path)
3683 unsigned char *rebase_status = arg;
3685 while (path[0] == '/')
3687 printf("%c %s\n", status, path);
3689 if (*rebase_status == GOT_STATUS_CONFLICT)
3691 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3692 *rebase_status = status;
3696 static const struct got_error *
3697 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3698 struct got_reference *branch, struct got_reference *new_base_branch,
3699 struct got_reference *tmp_branch, struct got_repository *repo)
3701 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3702 return got_worktree_rebase_complete(worktree, fileindex,
3703 new_base_branch, tmp_branch, branch, repo);
3706 static const struct got_error *
3707 rebase_commit(struct got_pathlist_head *merged_paths,
3708 struct got_worktree *worktree, struct got_fileindex *fileindex,
3709 struct got_reference *tmp_branch,
3710 struct got_object_id *commit_id, struct got_repository *repo)
3712 const struct got_error *error;
3713 struct got_commit_object *commit;
3714 struct got_object_id *new_commit_id;
3716 error = got_object_open_as_commit(&commit, repo, commit_id);
3720 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3721 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3723 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3725 error = show_rebase_progress(commit, commit_id, NULL);
3727 error = show_rebase_progress(commit, commit_id, new_commit_id);
3728 free(new_commit_id);
3731 got_object_commit_close(commit);
3735 struct check_path_prefix_arg {
3736 const char *path_prefix;
3741 static const struct got_error *
3742 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3743 struct got_blob_object *blob2, struct got_object_id *id1,
3744 struct got_object_id *id2, const char *path1, const char *path2,
3745 struct got_repository *repo)
3747 struct check_path_prefix_arg *a = arg;
3749 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3750 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3751 return got_error(a->errcode);
3756 static const struct got_error *
3757 check_path_prefix(struct got_object_id *parent_id,
3758 struct got_object_id *commit_id, const char *path_prefix,
3759 int errcode, struct got_repository *repo)
3761 const struct got_error *err;
3762 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3763 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3764 struct check_path_prefix_arg cpp_arg;
3766 if (got_path_is_root_dir(path_prefix))
3769 err = got_object_open_as_commit(&commit, repo, commit_id);
3773 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3777 err = got_object_open_as_tree(&tree1, repo,
3778 got_object_commit_get_tree_id(parent_commit));
3782 err = got_object_open_as_tree(&tree2, repo,
3783 got_object_commit_get_tree_id(commit));
3787 cpp_arg.path_prefix = path_prefix;
3788 while (cpp_arg.path_prefix[0] == '/')
3789 cpp_arg.path_prefix++;
3790 cpp_arg.len = strlen(cpp_arg.path_prefix);
3791 cpp_arg.errcode = errcode;
3792 err = got_diff_tree(tree1, tree2, "", "", repo,
3793 check_path_prefix_in_diff, &cpp_arg, 0);
3796 got_object_tree_close(tree1);
3798 got_object_tree_close(tree2);
3800 got_object_commit_close(commit);
3802 got_object_commit_close(parent_commit);
3806 static const struct got_error *
3807 collect_commits(struct got_object_id_queue *commits,
3808 struct got_object_id *initial_commit_id,
3809 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3810 const char *path_prefix, int path_prefix_errcode,
3811 struct got_repository *repo)
3813 const struct got_error *err = NULL;
3814 struct got_commit_graph *graph = NULL;
3815 struct got_object_id *parent_id = NULL;
3816 struct got_object_qid *qid;
3817 struct got_object_id *commit_id = initial_commit_id;
3819 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3823 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3826 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3827 err = got_commit_graph_iter_next(&parent_id, graph);
3829 if (err->code == GOT_ERR_ITER_COMPLETED) {
3830 err = got_error_msg(GOT_ERR_ANCESTRY,
3831 "ran out of commits to rebase before "
3832 "youngest common ancestor commit has "
3835 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3837 err = got_commit_graph_fetch_commits(graph, 1, repo);
3841 err = check_path_prefix(parent_id, commit_id,
3842 path_prefix, path_prefix_errcode, repo);
3846 err = got_object_qid_alloc(&qid, commit_id);
3849 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3850 commit_id = parent_id;
3854 got_commit_graph_close(graph);
3858 static const struct got_error *
3859 cmd_rebase(int argc, char *argv[])
3861 const struct got_error *error = NULL;
3862 struct got_worktree *worktree = NULL;
3863 struct got_repository *repo = NULL;
3864 struct got_fileindex *fileindex = NULL;
3866 struct got_reference *branch = NULL;
3867 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3868 struct got_object_id *commit_id = NULL, *parent_id = NULL;
3869 struct got_object_id *resume_commit_id = NULL;
3870 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3871 struct got_commit_object *commit = NULL;
3872 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3873 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3874 struct got_object_id_queue commits;
3875 struct got_pathlist_head merged_paths;
3876 const struct got_object_id_queue *parent_ids;
3877 struct got_object_qid *qid, *pid;
3879 SIMPLEQ_INIT(&commits);
3880 TAILQ_INIT(&merged_paths);
3882 while ((ch = getopt(argc, argv, "ac")) != -1) {
3888 continue_rebase = 1;
3900 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3901 "unveil", NULL) == -1)
3904 if (abort_rebase && continue_rebase)
3906 else if (abort_rebase || continue_rebase) {
3909 } else if (argc != 1)
3912 cwd = getcwd(NULL, 0);
3914 error = got_error_from_errno("getcwd");
3917 error = got_worktree_open(&worktree, cwd);
3921 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3925 error = apply_unveil(got_repo_get_path(repo), 0,
3926 got_worktree_get_root_path(worktree));
3930 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3936 if (!rebase_in_progress) {
3937 error = got_error(GOT_ERR_NOT_REBASING);
3940 error = got_worktree_rebase_continue(&resume_commit_id,
3941 &new_base_branch, &tmp_branch, &branch, &fileindex,
3945 printf("Switching work tree to %s\n",
3946 got_ref_get_symref_target(new_base_branch));
3947 error = got_worktree_rebase_abort(worktree, fileindex, repo,
3948 new_base_branch, update_progress, &did_something);
3951 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3952 goto done; /* nothing else to do */
3955 if (continue_rebase) {
3956 if (!rebase_in_progress) {
3957 error = got_error(GOT_ERR_NOT_REBASING);
3960 error = got_worktree_rebase_continue(&resume_commit_id,
3961 &new_base_branch, &tmp_branch, &branch, &fileindex,
3966 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3967 resume_commit_id, repo);
3971 yca_id = got_object_id_dup(resume_commit_id);
3972 if (yca_id == NULL) {
3973 error = got_error_from_errno("got_object_id_dup");
3977 error = got_ref_open(&branch, repo, argv[0], 0);
3982 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3986 if (!continue_rebase) {
3987 struct got_object_id *base_commit_id;
3989 base_commit_id = got_worktree_get_base_commit_id(worktree);
3990 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3991 base_commit_id, branch_head_commit_id, repo);
3994 if (yca_id == NULL) {
3995 error = got_error_msg(GOT_ERR_ANCESTRY,
3996 "specified branch shares no common ancestry "
3997 "with work tree's branch");
4001 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4003 if (error->code != GOT_ERR_ANCESTRY)
4007 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4008 "specified branch resolves to a commit which "
4009 "is already contained in work tree's branch");
4012 error = got_worktree_rebase_prepare(&new_base_branch,
4013 &tmp_branch, &fileindex, worktree, branch, repo);
4018 commit_id = branch_head_commit_id;
4019 error = got_object_open_as_commit(&commit, repo, commit_id);
4023 parent_ids = got_object_commit_get_parent_ids(commit);
4024 pid = SIMPLEQ_FIRST(parent_ids);
4025 error = collect_commits(&commits, commit_id, pid->id,
4026 yca_id, got_worktree_get_path_prefix(worktree),
4027 GOT_ERR_REBASE_PATH, repo);
4028 got_object_commit_close(commit);
4033 if (SIMPLEQ_EMPTY(&commits)) {
4034 if (continue_rebase)
4035 error = rebase_complete(worktree, fileindex,
4036 branch, new_base_branch, tmp_branch, repo);
4038 error = got_error(GOT_ERR_EMPTY_REBASE);
4043 SIMPLEQ_FOREACH(qid, &commits, entry) {
4044 commit_id = qid->id;
4045 parent_id = pid ? pid->id : yca_id;
4048 error = got_worktree_rebase_merge_files(&merged_paths,
4049 worktree, fileindex, parent_id, commit_id, repo,
4050 rebase_progress, &rebase_status, check_cancelled, NULL);
4054 if (rebase_status == GOT_STATUS_CONFLICT) {
4055 got_worktree_rebase_pathlist_free(&merged_paths);
4059 error = rebase_commit(&merged_paths, worktree, fileindex,
4060 tmp_branch, commit_id, repo);
4061 got_worktree_rebase_pathlist_free(&merged_paths);
4066 if (rebase_status == GOT_STATUS_CONFLICT) {
4067 error = got_worktree_rebase_postpone(worktree, fileindex);
4070 error = got_error_msg(GOT_ERR_CONFLICTS,
4071 "conflicts must be resolved before rebasing can continue");
4073 error = rebase_complete(worktree, fileindex, branch,
4074 new_base_branch, tmp_branch, repo);
4076 got_object_id_queue_free(&commits);
4077 free(branch_head_commit_id);
4078 free(resume_commit_id);
4081 got_object_commit_close(commit);
4083 got_ref_close(branch);
4084 if (new_base_branch)
4085 got_ref_close(new_base_branch);
4087 got_ref_close(tmp_branch);
4089 got_worktree_close(worktree);
4091 got_repo_close(repo);
4096 usage_histedit(void)
4098 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4103 #define GOT_HISTEDIT_PICK 'p'
4104 #define GOT_HISTEDIT_EDIT 'e'
4105 #define GOT_HISTEDIT_FOLD 'f'
4106 #define GOT_HISTEDIT_DROP 'd'
4107 #define GOT_HISTEDIT_MESG 'm'
4109 static struct got_histedit_cmd {
4113 } got_histedit_cmds[] = {
4114 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4115 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4116 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4117 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4118 { GOT_HISTEDIT_MESG, "mesg",
4119 "single-line log message for commit above (open editor if empty)" },
4122 struct got_histedit_list_entry {
4123 TAILQ_ENTRY(got_histedit_list_entry) entry;
4124 struct got_object_id *commit_id;
4125 const struct got_histedit_cmd *cmd;
4128 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4130 static const struct got_error *
4131 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4132 FILE *f, struct got_repository *repo)
4134 const struct got_error *err = NULL;
4135 char *logmsg = NULL, *id_str = NULL;
4136 struct got_commit_object *commit = NULL;
4139 err = got_object_open_as_commit(&commit, repo, commit_id);
4143 err = get_short_logmsg(&logmsg, 34, commit);
4147 err = got_object_id_str(&id_str, commit_id);
4151 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4153 err = got_ferror(f, GOT_ERR_IO);
4156 got_object_commit_close(commit);
4162 static const struct got_error *
4163 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4164 struct got_repository *repo)
4166 const struct got_error *err = NULL;
4167 struct got_object_qid *qid;
4169 if (SIMPLEQ_EMPTY(commits))
4170 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4172 SIMPLEQ_FOREACH(qid, commits, entry) {
4173 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4182 static const struct got_error *
4183 write_cmd_list(FILE *f)
4185 const struct got_error *err = NULL;
4188 n = fprintf(f, "# Available histedit commands:\n");
4190 return got_ferror(f, GOT_ERR_IO);
4192 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4193 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4194 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4197 err = got_ferror(f, GOT_ERR_IO);
4201 n = fprintf(f, "# Commits will be processed in order from top to "
4202 "bottom of this file.\n");
4204 return got_ferror(f, GOT_ERR_IO);
4208 static const struct got_error *
4209 histedit_syntax_error(int lineno)
4211 static char msg[42];
4214 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4216 if (ret == -1 || ret >= sizeof(msg))
4217 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4219 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4222 static const struct got_error *
4223 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4224 char *logmsg, struct got_repository *repo)
4226 const struct got_error *err;
4227 struct got_commit_object *folded_commit = NULL;
4230 err = got_object_id_str(&id_str, hle->commit_id);
4234 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4238 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4239 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4240 got_object_commit_get_logmsg(folded_commit)) == -1) {
4241 err = got_error_from_errno("asprintf");
4246 got_object_commit_close(folded_commit);
4251 static struct got_histedit_list_entry *
4252 get_folded_commits(struct got_histedit_list_entry *hle)
4254 struct got_histedit_list_entry *prev, *folded = NULL;
4256 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4257 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4258 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4259 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4261 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4267 static const struct got_error *
4268 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4269 struct got_repository *repo)
4271 char *logmsg_path = NULL, *id_str = NULL;
4272 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4273 const struct got_error *err = NULL;
4274 struct got_commit_object *commit = NULL;
4276 struct got_histedit_list_entry *folded = NULL;
4278 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4282 folded = get_folded_commits(hle);
4284 while (folded != hle) {
4285 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4286 folded = TAILQ_NEXT(folded, entry);
4289 err = append_folded_commit_msg(&new_msg, folded,
4295 folded = TAILQ_NEXT(folded, entry);
4299 err = got_object_id_str(&id_str, hle->commit_id);
4302 if (asprintf(&new_msg,
4303 "%s\n# original log message of commit %s: %s",
4304 logmsg ? logmsg : "", id_str,
4305 got_object_commit_get_logmsg(commit)) == -1) {
4306 err = got_error_from_errno("asprintf");
4312 err = got_object_id_str(&id_str, hle->commit_id);
4316 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4320 dprintf(fd, logmsg);
4323 err = get_editor(&editor);
4327 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4329 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4332 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4333 if (hle->logmsg == NULL)
4334 err = got_error_from_errno("strdup");
4337 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4338 err = got_error_from_errno2("unlink", logmsg_path);
4343 got_object_commit_close(commit);
4347 static const struct got_error *
4348 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4349 FILE *f, struct got_repository *repo)
4351 const struct got_error *err = NULL;
4352 char *line = NULL, *p, *end;
4356 const struct got_histedit_cmd *cmd;
4357 struct got_object_id *commit_id = NULL;
4358 struct got_histedit_list_entry *hle = NULL;
4361 len = getline(&line, &size, f);
4363 const struct got_error *getline_err;
4366 getline_err = got_error_from_errno("getline");
4367 err = got_ferror(f, getline_err->code);
4372 while (isspace((unsigned char)p[0]))
4374 if (p[0] == '#' || p[0] == '\0') {
4380 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4381 cmd = &got_histedit_cmds[i];
4382 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4383 isspace((unsigned char)p[strlen(cmd->name)])) {
4384 p += strlen(cmd->name);
4387 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4392 if (i == nitems(got_histedit_cmds)) {
4393 err = histedit_syntax_error(lineno);
4396 while (isspace((unsigned char)p[0]))
4398 if (cmd->code == GOT_HISTEDIT_MESG) {
4399 if (hle == NULL || hle->logmsg != NULL) {
4400 err = got_error(GOT_ERR_HISTEDIT_CMD);
4404 err = histedit_edit_logmsg(hle, repo);
4408 hle->logmsg = strdup(p);
4409 if (hle->logmsg == NULL) {
4410 err = got_error_from_errno("strdup");
4419 while (end[0] && !isspace((unsigned char)end[0]))
4423 err = got_object_resolve_id_str(&commit_id, repo, p);
4425 /* override error code */
4426 err = histedit_syntax_error(lineno);
4430 hle = malloc(sizeof(*hle));
4432 err = got_error_from_errno("malloc");
4436 hle->commit_id = commit_id;
4441 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4449 static const struct got_error *
4450 histedit_check_script(struct got_histedit_list *histedit_cmds,
4451 struct got_object_id_queue *commits, struct got_repository *repo)
4453 const struct got_error *err = NULL;
4454 struct got_object_qid *qid;
4455 struct got_histedit_list_entry *hle;
4456 static char msg[80];
4459 if (TAILQ_EMPTY(histedit_cmds))
4460 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4461 "histedit script contains no commands");
4463 SIMPLEQ_FOREACH(qid, commits, entry) {
4464 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4465 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4469 err = got_object_id_str(&id_str, qid->id);
4472 snprintf(msg, sizeof(msg),
4473 "commit %s missing from histedit script", id_str);
4475 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4479 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4480 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4481 "last commit in histedit script cannot be folded");
4486 static const struct got_error *
4487 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4488 const char *path, struct got_object_id_queue *commits,
4489 struct got_repository *repo)
4491 const struct got_error *err = NULL;
4495 err = get_editor(&editor);
4499 if (spawn_editor(editor, path) == -1) {
4500 err = got_error_from_errno("failed spawning editor");
4504 f = fopen(path, "r");
4506 err = got_error_from_errno("fopen");
4509 err = histedit_parse_list(histedit_cmds, f, repo);
4513 err = histedit_check_script(histedit_cmds, commits, repo);
4515 if (f && fclose(f) != 0 && err == NULL)
4516 err = got_error_from_errno("fclose");
4521 static const struct got_error *
4522 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4523 struct got_object_id_queue *, const char *, struct got_repository *);
4525 static const struct got_error *
4526 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4527 struct got_object_id_queue *commits, struct got_repository *repo)
4529 const struct got_error *err;
4533 err = got_opentemp_named(&path, &f, "got-histedit");
4537 err = write_cmd_list(f);
4541 err = histedit_write_commit_list(commits, f, repo);
4545 if (fclose(f) != 0) {
4546 err = got_error_from_errno("fclose");
4551 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4553 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4554 err->code != GOT_ERR_HISTEDIT_CMD)
4556 err = histedit_edit_list_retry(histedit_cmds, err,
4557 commits, path, repo);
4560 if (f && fclose(f) != 0 && err == NULL)
4561 err = got_error_from_errno("fclose");
4562 if (path && unlink(path) != 0 && err == NULL)
4563 err = got_error_from_errno2("unlink", path);
4568 static const struct got_error *
4569 histedit_save_list(struct got_histedit_list *histedit_cmds,
4570 struct got_worktree *worktree, struct got_repository *repo)
4572 const struct got_error *err = NULL;
4575 struct got_histedit_list_entry *hle;
4576 struct got_commit_object *commit = NULL;
4578 err = got_worktree_get_histedit_script_path(&path, worktree);
4582 f = fopen(path, "w");
4584 err = got_error_from_errno2("fopen", path);
4587 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4588 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4594 int n = fprintf(f, "%c %s\n",
4595 GOT_HISTEDIT_MESG, hle->logmsg);
4597 err = got_ferror(f, GOT_ERR_IO);
4603 if (f && fclose(f) != 0 && err == NULL)
4604 err = got_error_from_errno("fclose");
4607 got_object_commit_close(commit);
4612 histedit_free_list(struct got_histedit_list *histedit_cmds)
4614 struct got_histedit_list_entry *hle;
4616 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4617 TAILQ_REMOVE(histedit_cmds, hle, entry);
4622 static const struct got_error *
4623 histedit_load_list(struct got_histedit_list *histedit_cmds,
4624 const char *path, struct got_repository *repo)
4626 const struct got_error *err = NULL;
4629 f = fopen(path, "r");
4631 err = got_error_from_errno2("fopen", path);
4635 err = histedit_parse_list(histedit_cmds, f, repo);
4637 if (f && fclose(f) != 0 && err == NULL)
4638 err = got_error_from_errno("fclose");
4642 static const struct got_error *
4643 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4644 const struct got_error *edit_err, struct got_object_id_queue *commits,
4645 const char *path, struct got_repository *repo)
4647 const struct got_error *err = NULL, *prev_err = edit_err;
4650 while (resp != 'c' && resp != 'r' && resp != 'a') {
4651 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4652 "or (a)bort: ", getprogname(), prev_err->msg);
4657 histedit_free_list(histedit_cmds);
4658 err = histedit_run_editor(histedit_cmds, path, commits,
4661 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4662 err->code != GOT_ERR_HISTEDIT_CMD)
4669 } else if (resp == 'r') {
4670 histedit_free_list(histedit_cmds);
4671 err = histedit_edit_script(histedit_cmds,
4674 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4675 err->code != GOT_ERR_HISTEDIT_CMD)
4682 } else if (resp == 'a') {
4683 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4686 printf("invalid response '%c'\n", resp);
4692 static const struct got_error *
4693 histedit_complete(struct got_worktree *worktree,
4694 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4695 struct got_reference *branch, struct got_repository *repo)
4697 printf("Switching work tree to %s\n",
4698 got_ref_get_symref_target(branch));
4699 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4703 static const struct got_error *
4704 show_histedit_progress(struct got_commit_object *commit,
4705 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4707 const struct got_error *err;
4708 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4710 err = got_object_id_str(&old_id_str, hle->commit_id);
4715 err = got_object_id_str(&new_id_str, new_id);
4720 old_id_str[12] = '\0';
4722 new_id_str[12] = '\0';
4725 logmsg = strdup(hle->logmsg);
4726 if (logmsg == NULL) {
4727 err = got_error_from_errno("strdup");
4730 trim_logmsg(logmsg, 42);
4732 err = get_short_logmsg(&logmsg, 42, commit);
4737 switch (hle->cmd->code) {
4738 case GOT_HISTEDIT_PICK:
4739 case GOT_HISTEDIT_EDIT:
4740 printf("%s -> %s: %s\n", old_id_str,
4741 new_id_str ? new_id_str : "no-op change", logmsg);
4743 case GOT_HISTEDIT_DROP:
4744 case GOT_HISTEDIT_FOLD:
4745 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4758 static const struct got_error *
4759 histedit_commit(struct got_pathlist_head *merged_paths,
4760 struct got_worktree *worktree, struct got_fileindex *fileindex,
4761 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4762 struct got_repository *repo)
4764 const struct got_error *err;
4765 struct got_commit_object *commit;
4766 struct got_object_id *new_commit_id;
4768 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4769 && hle->logmsg == NULL) {
4770 err = histedit_edit_logmsg(hle, repo);
4775 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4779 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4780 worktree, fileindex, tmp_branch, commit, hle->commit_id,
4783 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4785 err = show_histedit_progress(commit, hle, NULL);
4787 err = show_histedit_progress(commit, hle, new_commit_id);
4788 free(new_commit_id);
4791 got_object_commit_close(commit);
4795 static const struct got_error *
4796 histedit_skip_commit(struct got_histedit_list_entry *hle,
4797 struct got_worktree *worktree, struct got_repository *repo)
4799 const struct got_error *error;
4800 struct got_commit_object *commit;
4802 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4807 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4811 error = show_histedit_progress(commit, hle, NULL);
4812 got_object_commit_close(commit);
4816 static const struct got_error *
4817 cmd_histedit(int argc, char *argv[])
4819 const struct got_error *error = NULL;
4820 struct got_worktree *worktree = NULL;
4821 struct got_fileindex *fileindex = NULL;
4822 struct got_repository *repo = NULL;
4824 struct got_reference *branch = NULL;
4825 struct got_reference *tmp_branch = NULL;
4826 struct got_object_id *resume_commit_id = NULL;
4827 struct got_object_id *base_commit_id = NULL;
4828 struct got_object_id *head_commit_id = NULL;
4829 struct got_commit_object *commit = NULL;
4830 int ch, rebase_in_progress = 0, did_something;
4831 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4832 const char *edit_script_path = NULL;
4833 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4834 struct got_object_id_queue commits;
4835 struct got_pathlist_head merged_paths;
4836 const struct got_object_id_queue *parent_ids;
4837 struct got_object_qid *pid;
4838 struct got_histedit_list histedit_cmds;
4839 struct got_histedit_list_entry *hle;
4841 SIMPLEQ_INIT(&commits);
4842 TAILQ_INIT(&histedit_cmds);
4843 TAILQ_INIT(&merged_paths);
4845 while ((ch = getopt(argc, argv, "acF:")) != -1) {
4854 edit_script_path = optarg;
4866 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4867 "unveil", NULL) == -1)
4870 if (abort_edit && continue_edit)
4876 * This command cannot apply unveil(2) in all cases because the
4877 * user may choose to run an editor to edit the histedit script
4878 * and to edit individual commit log messages.
4879 * unveil(2) traverses exec(2); if an editor is used we have to
4880 * apply unveil after edit script and log messages have been written.
4881 * XXX TODO: Make use of unveil(2) where possible.
4884 cwd = getcwd(NULL, 0);
4886 error = got_error_from_errno("getcwd");
4889 error = got_worktree_open(&worktree, cwd);
4893 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4897 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4900 if (rebase_in_progress) {
4901 error = got_error(GOT_ERR_REBASING);
4905 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4909 if (edit_in_progress && abort_edit) {
4910 error = got_worktree_histedit_continue(&resume_commit_id,
4911 &tmp_branch, &branch, &base_commit_id, &fileindex,
4915 printf("Switching work tree to %s\n",
4916 got_ref_get_symref_target(branch));
4917 error = got_worktree_histedit_abort(worktree, fileindex, repo,
4918 branch, base_commit_id, update_progress, &did_something);
4921 printf("Histedit of %s aborted\n",
4922 got_ref_get_symref_target(branch));
4923 goto done; /* nothing else to do */
4924 } else if (abort_edit) {
4925 error = got_error(GOT_ERR_NOT_HISTEDIT);
4929 if (continue_edit) {
4932 if (!edit_in_progress) {
4933 error = got_error(GOT_ERR_NOT_HISTEDIT);
4937 error = got_worktree_get_histedit_script_path(&path, worktree);
4941 error = histedit_load_list(&histedit_cmds, path, repo);
4946 error = got_worktree_histedit_continue(&resume_commit_id,
4947 &tmp_branch, &branch, &base_commit_id, &fileindex,
4952 error = got_ref_resolve(&head_commit_id, repo, branch);
4956 error = got_object_open_as_commit(&commit, repo,
4960 parent_ids = got_object_commit_get_parent_ids(commit);
4961 pid = SIMPLEQ_FIRST(parent_ids);
4963 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4966 error = collect_commits(&commits, head_commit_id, pid->id,
4967 base_commit_id, got_worktree_get_path_prefix(worktree),
4968 GOT_ERR_HISTEDIT_PATH, repo);
4969 got_object_commit_close(commit);
4974 if (edit_in_progress) {
4975 error = got_error(GOT_ERR_HISTEDIT_BUSY);
4979 error = got_ref_open(&branch, repo,
4980 got_worktree_get_head_ref_name(worktree), 0);
4984 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
4985 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
4986 "will not edit commit history of a branch outside "
4987 "the \"refs/heads/\" reference namespace");
4991 error = got_ref_resolve(&head_commit_id, repo, branch);
4992 got_ref_close(branch);
4997 error = got_object_open_as_commit(&commit, repo,
5001 parent_ids = got_object_commit_get_parent_ids(commit);
5002 pid = SIMPLEQ_FIRST(parent_ids);
5004 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5007 error = collect_commits(&commits, head_commit_id, pid->id,
5008 got_worktree_get_base_commit_id(worktree),
5009 got_worktree_get_path_prefix(worktree),
5010 GOT_ERR_HISTEDIT_PATH, repo);
5011 got_object_commit_close(commit);
5016 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5017 &base_commit_id, &fileindex, worktree, repo);
5021 if (edit_script_path) {
5022 error = histedit_load_list(&histedit_cmds,
5023 edit_script_path, repo);
5025 got_worktree_histedit_abort(worktree, fileindex,
5026 repo, branch, base_commit_id,
5027 update_progress, &did_something);
5031 error = histedit_edit_script(&histedit_cmds, &commits,
5034 got_worktree_histedit_abort(worktree, fileindex,
5035 repo, branch, base_commit_id,
5036 update_progress, &did_something);
5042 error = histedit_save_list(&histedit_cmds, worktree,
5045 got_worktree_histedit_abort(worktree, fileindex,
5046 repo, branch, base_commit_id,
5047 update_progress, &did_something);
5053 error = histedit_check_script(&histedit_cmds, &commits, repo);
5057 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5058 if (resume_commit_id) {
5059 if (got_object_id_cmp(hle->commit_id,
5060 resume_commit_id) != 0)
5063 resume_commit_id = NULL;
5064 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5065 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5066 error = histedit_skip_commit(hle, worktree,
5069 error = histedit_commit(NULL, worktree,
5070 fileindex, tmp_branch, hle, repo);
5077 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5078 error = histedit_skip_commit(hle, worktree, repo);
5084 error = got_object_open_as_commit(&commit, repo,
5088 parent_ids = got_object_commit_get_parent_ids(commit);
5089 pid = SIMPLEQ_FIRST(parent_ids);
5091 error = got_worktree_histedit_merge_files(&merged_paths,
5092 worktree, fileindex, pid->id, hle->commit_id, repo,
5093 rebase_progress, &rebase_status, check_cancelled, NULL);
5096 got_object_commit_close(commit);
5099 if (rebase_status == GOT_STATUS_CONFLICT) {
5100 got_worktree_rebase_pathlist_free(&merged_paths);
5104 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5106 error = got_object_id_str(&id_str, hle->commit_id);
5109 printf("Stopping histedit for amending commit %s\n",
5112 got_worktree_rebase_pathlist_free(&merged_paths);
5113 error = got_worktree_histedit_postpone(worktree,
5118 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5119 error = histedit_skip_commit(hle, worktree, repo);
5125 error = histedit_commit(&merged_paths, worktree, fileindex,
5126 tmp_branch, hle, repo);
5127 got_worktree_rebase_pathlist_free(&merged_paths);
5132 if (rebase_status == GOT_STATUS_CONFLICT) {
5133 error = got_worktree_histedit_postpone(worktree, fileindex);
5136 error = got_error_msg(GOT_ERR_CONFLICTS,
5137 "conflicts must be resolved before rebasing can continue");
5139 error = histedit_complete(worktree, fileindex, tmp_branch,
5142 got_object_id_queue_free(&commits);
5143 histedit_free_list(&histedit_cmds);
5144 free(head_commit_id);
5145 free(base_commit_id);
5146 free(resume_commit_id);
5148 got_object_commit_close(commit);
5150 got_ref_close(branch);
5152 got_ref_close(tmp_branch);
5154 got_worktree_close(worktree);
5156 got_repo_close(repo);
5163 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5164 "[file-path ...]\n",
5169 static const struct got_error *
5170 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5171 const char *path, struct got_object_id *blob_id,
5172 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5174 const struct got_error *err = NULL;
5175 char *id_str = NULL;
5177 if (staged_status != GOT_STATUS_ADD &&
5178 staged_status != GOT_STATUS_MODIFY &&
5179 staged_status != GOT_STATUS_DELETE)
5182 if (staged_status == GOT_STATUS_ADD ||
5183 staged_status == GOT_STATUS_MODIFY)
5184 err = got_object_id_str(&id_str, staged_blob_id);
5186 err = got_object_id_str(&id_str, blob_id);
5190 printf("%s %c %s\n", id_str, staged_status, path);
5195 static const struct got_error *
5196 show_change(unsigned char status, const char *path, FILE *patch_file)
5199 size_t linesize = 0;
5203 case GOT_STATUS_ADD:
5204 printf("A %s\nstage this addition? [y/n/q] ", path);
5206 case GOT_STATUS_DELETE:
5207 printf("D %s\nstage deletion? [y/n/q] ", path);
5209 case GOT_STATUS_MODIFY:
5210 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5211 return got_error_from_errno("fseek");
5212 printf(GOT_COMMIT_SEP_STR);
5213 while ((linelen = getline(&line, &linesize, patch_file) != -1))
5215 if (ferror(patch_file))
5216 return got_error_from_errno("getline");
5217 printf(GOT_COMMIT_SEP_STR);
5218 printf("M %s\nstage this change? [y/n/q] ", path);
5221 return got_error_path(path, GOT_ERR_FILE_STATUS);
5227 static const struct got_error *
5228 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5231 const struct got_error *err = NULL;
5233 size_t linesize = 0;
5236 FILE *patch_script_file = arg;
5238 *choice = GOT_PATCH_CHOICE_NONE;
5240 if (patch_script_file) {
5242 linelen = getline(&line, &linesize, patch_script_file);
5243 if (linelen == -1) {
5244 if (ferror(patch_script_file))
5245 return got_error_from_errno("getline");
5248 nl = strchr(line, '\n');
5251 err = show_change(status, path, patch_file);
5254 if (strcmp(line, "y") == 0) {
5255 *choice = GOT_PATCH_CHOICE_YES;
5258 if (strcmp(line, "n") == 0) {
5259 *choice = GOT_PATCH_CHOICE_NO;
5262 if (strcmp(line, "q") == 0) {
5263 *choice = GOT_PATCH_CHOICE_QUIT;
5270 while (resp != 'y' && resp != 'n' && resp != 'q') {
5271 err = show_change(status, path, patch_file);
5277 if (resp != 'y' && resp != 'n' && resp != 'q')
5278 printf("invalid response '%c'\n", resp);
5282 *choice = GOT_PATCH_CHOICE_YES;
5283 else if (resp == 'n')
5284 *choice = GOT_PATCH_CHOICE_NO;
5285 else if (resp == 'q')
5286 *choice = GOT_PATCH_CHOICE_QUIT;
5291 static const struct got_error *
5292 cmd_stage(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_repository *repo = NULL;
5296 struct got_worktree *worktree = NULL;
5298 struct got_pathlist_head paths;
5299 struct got_pathlist_entry *pe;
5300 int ch, list_stage = 0, pflag = 0;
5301 const char *patch_script_path = NULL;
5302 FILE *patch_script_file = NULL;
5306 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5315 patch_script_path = optarg;
5327 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5328 "unveil", NULL) == -1)
5331 if (list_stage && (pflag || patch_script_path))
5332 errx(1, "-l option cannot be used with other options");
5333 if (patch_script_path && !pflag)
5334 errx(1, "-F option can only be used together with -p option");
5336 cwd = getcwd(NULL, 0);
5338 error = got_error_from_errno("getcwd");
5342 error = got_worktree_open(&worktree, cwd);
5346 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5350 if (patch_script_path) {
5351 patch_script_file = fopen(patch_script_path, "r");
5352 if (patch_script_file == NULL) {
5353 error = got_error_from_errno2("fopen",
5358 error = apply_unveil(got_repo_get_path(repo), 1,
5359 got_worktree_get_root_path(worktree));
5363 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5368 error = got_worktree_status(worktree, &paths, repo,
5369 print_stage, NULL, check_cancelled, NULL);
5371 error = got_worktree_stage(worktree, &paths,
5372 pflag ? NULL : print_status, NULL,
5373 pflag ? choose_patch : NULL, patch_script_file, repo);
5376 got_repo_close(repo);
5378 got_worktree_close(worktree);
5379 TAILQ_FOREACH(pe, &paths, entry)
5380 free((char *)pe->path);
5381 got_pathlist_free(&paths);
5389 fprintf(stderr, "usage: %s unstage [file-path ...]\n",
5395 static const struct got_error *
5396 cmd_unstage(int argc, char *argv[])
5398 const struct got_error *error = NULL;
5399 struct got_repository *repo = NULL;
5400 struct got_worktree *worktree = NULL;
5402 struct got_pathlist_head paths;
5403 struct got_pathlist_entry *pe;
5404 int ch, did_something = 0;
5408 while ((ch = getopt(argc, argv, "")) != -1) {
5420 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5421 "unveil", NULL) == -1)
5424 cwd = getcwd(NULL, 0);
5426 error = got_error_from_errno("getcwd");
5430 error = got_worktree_open(&worktree, cwd);
5434 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5438 error = apply_unveil(got_repo_get_path(repo), 1,
5439 got_worktree_get_root_path(worktree));
5443 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5447 error = got_worktree_unstage(worktree, &paths, update_progress,
5448 &did_something, repo);
5451 got_repo_close(repo);
5453 got_worktree_close(worktree);
5454 TAILQ_FOREACH(pe, &paths, entry)
5455 free((char *)pe->path);
5456 got_pathlist_free(&paths);