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/types.h>
21 #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 get_author(const char **author)
488 const char *got_author;
492 got_author = getenv("GOT_AUTHOR");
493 if (got_author == NULL) {
494 /* TODO: Look up user in password database? */
495 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 *author = got_author;
501 * Really dumb email address check; we're only doing this to
502 * avoid git's object parser breaking on commits we create.
504 while (*got_author && *got_author != '<')
506 if (*got_author != '<')
507 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 while (*got_author && *got_author != '@')
510 if (*got_author != '@')
511 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 while (*got_author && *got_author != '>')
514 if (*got_author != '>')
515 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
520 static const struct got_error *
521 cmd_import(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
527 const char *branch_name = "master";
528 char *refname = NULL, *id_str = NULL;
529 struct got_repository *repo = NULL;
530 struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 struct got_object_id *new_commit_id = NULL;
533 struct got_pathlist_head ignores;
534 struct got_pathlist_entry *pe;
536 TAILQ_INIT(&ignores);
538 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
541 branch_name = optarg;
544 logmsg = strdup(optarg);
545 if (logmsg == NULL) {
546 error = got_error_from_errno("strdup");
551 repo_path = realpath(optarg, NULL);
552 if (repo_path == NULL) {
553 error = got_error_from_errno("realpath");
558 if (optarg[0] == '\0')
560 error = got_pathlist_insert(&pe, &ignores, optarg,
575 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
582 error = get_author(&author);
586 if (repo_path == NULL) {
587 repo_path = getcwd(NULL, 0);
588 if (repo_path == NULL)
589 return got_error_from_errno("getcwd");
591 got_path_strip_trailing_slashes(repo_path);
592 error = got_repo_open(&repo, repo_path);
596 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 error = got_error_from_errno("asprintf");
601 error = got_ref_open(&branch_ref, repo, refname, 0);
603 if (error->code != GOT_ERR_NOT_REF)
606 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 "import target branch already exists");
611 path_dir = realpath(argv[0], NULL);
612 if (path_dir == NULL) {
613 error = got_error_from_errno("realpath");
616 got_path_strip_trailing_slashes(path_dir);
619 * unveil(2) traverses exec(2); if an editor is used we have
620 * to apply unveil after the log message has been written.
622 if (logmsg == NULL || strlen(logmsg) == 0) {
623 error = get_editor(&editor);
626 error = collect_import_msg(&logmsg, editor, path_dir, refname);
631 if (unveil(path_dir, "r") != 0)
632 return got_error_from_errno2("unveil", path_dir);
634 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
638 error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 author, &ignores, repo, import_progress, NULL);
643 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
647 error = got_ref_write(branch_ref, repo);
651 error = got_object_id_str(&id_str, new_commit_id);
655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
657 if (error->code != GOT_ERR_NOT_REF)
660 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
665 error = got_ref_write(head_ref, repo);
670 printf("Created branch %s with commit %s\n",
671 got_ref_get_name(branch_ref), id_str);
679 got_ref_close(branch_ref);
681 got_ref_close(head_ref);
688 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 "[-p prefix] repository-path [worktree-path]\n", getprogname());
693 static const struct got_error *
694 checkout_progress(void *arg, unsigned char status, const char *path)
696 char *worktree_path = arg;
698 /* Base commit bump happens silently. */
699 if (status == GOT_STATUS_BUMP_BASE)
702 while (path[0] == '/')
705 printf("%c %s/%s\n", status, worktree_path, path);
709 static const struct got_error *
710 check_cancelled(void *arg)
712 if (sigint_received || sigpipe_received)
713 return got_error(GOT_ERR_CANCELLED);
717 static const struct got_error *
718 check_linear_ancestry(struct got_object_id *commit_id,
719 struct got_object_id *base_commit_id, struct got_repository *repo)
721 const struct got_error *err = NULL;
722 struct got_object_id *yca_id;
724 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 commit_id, base_commit_id, repo);
730 return got_error(GOT_ERR_ANCESTRY);
733 * Require a straight line of history between the target commit
734 * and the work tree's base commit.
736 * Non-linear situations such as this require a rebase:
738 * (commit) D F (base_commit)
746 * 'got update' only handles linear cases:
747 * Update forwards in time: A (base/yca) - B - C - D (commit)
748 * Update backwards in time: D (base) - C - B - A (commit/yca)
750 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 got_object_id_cmp(base_commit_id, yca_id) != 0)
752 return got_error(GOT_ERR_ANCESTRY);
758 static const struct got_error *
759 check_same_branch(struct got_object_id *commit_id,
760 struct got_reference *head_ref, struct got_object_id *yca_id,
761 struct got_repository *repo)
763 const struct got_error *err = NULL;
764 struct got_commit_graph *graph = NULL;
765 struct got_object_id *head_commit_id = NULL;
766 int is_same_branch = 0;
768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
772 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
776 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
781 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
785 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
790 struct got_object_id *id;
791 err = got_commit_graph_iter_next(&id, graph);
793 if (err->code == GOT_ERR_ITER_COMPLETED) {
796 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
798 err = got_commit_graph_fetch_commits(graph, 1,
805 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
807 if (got_object_id_cmp(id, commit_id) == 0) {
815 got_commit_graph_close(graph);
816 free(head_commit_id);
817 if (!err && !is_same_branch)
818 err = got_error(GOT_ERR_ANCESTRY);
822 static const struct got_error *
823 resolve_commit_arg(struct got_object_id **commit_id,
824 const char *commit_id_arg, struct got_repository *repo)
826 const struct got_error *err;
827 struct got_reference *ref;
828 struct got_tag_object *tag;
830 err = got_repo_object_match_tag(&tag, commit_id_arg,
831 GOT_OBJ_TYPE_COMMIT, repo);
833 *commit_id = got_object_id_dup(
834 got_object_tag_get_object_id(tag));
835 if (*commit_id == NULL)
836 err = got_error_from_errno("got_object_id_dup");
837 got_object_tag_close(tag);
839 } else if (err->code != GOT_ERR_NO_OBJ)
842 err = got_ref_open(&ref, repo, commit_id_arg, 0);
844 err = got_ref_resolve(commit_id, repo, ref);
847 if (err->code != GOT_ERR_NOT_REF)
849 err = got_repo_match_object_id_prefix(commit_id,
850 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
855 static const struct got_error *
856 cmd_checkout(int argc, char *argv[])
858 const struct got_error *error = NULL;
859 struct got_repository *repo = NULL;
860 struct got_reference *head_ref = NULL;
861 struct got_worktree *worktree = NULL;
862 char *repo_path = NULL;
863 char *worktree_path = NULL;
864 const char *path_prefix = "";
865 const char *branch_name = GOT_REF_HEAD;
866 char *commit_id_str = NULL;
867 int ch, same_path_prefix;
868 struct got_pathlist_head paths;
872 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
875 branch_name = optarg;
878 commit_id_str = strdup(optarg);
879 if (commit_id_str == NULL)
880 return got_error_from_errno("strdup");
883 path_prefix = optarg;
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 "unveil", NULL) == -1)
900 char *cwd, *base, *dotgit;
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno2("realpath", argv[0]);
904 cwd = getcwd(NULL, 0);
906 error = got_error_from_errno("getcwd");
909 if (path_prefix[0]) {
910 base = basename(path_prefix);
912 error = got_error_from_errno2("basename",
917 base = basename(repo_path);
919 error = got_error_from_errno2("basename",
924 dotgit = strstr(base, ".git");
927 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 error = got_error_from_errno("asprintf");
933 } else if (argc == 2) {
934 repo_path = realpath(argv[0], NULL);
935 if (repo_path == NULL) {
936 error = got_error_from_errno2("realpath", argv[0]);
939 worktree_path = realpath(argv[1], NULL);
940 if (worktree_path == NULL) {
941 if (errno != ENOENT) {
942 error = got_error_from_errno2("realpath",
946 worktree_path = strdup(argv[1]);
947 if (worktree_path == NULL) {
948 error = got_error_from_errno("strdup");
955 got_path_strip_trailing_slashes(repo_path);
956 got_path_strip_trailing_slashes(worktree_path);
958 error = got_repo_open(&repo, repo_path);
962 /* Pre-create work tree path for unveil(2) */
963 error = got_path_mkdir(worktree_path);
965 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
968 if (!got_path_dir_is_empty(worktree_path)) {
969 error = got_error_path(worktree_path,
970 GOT_ERR_DIR_NOT_EMPTY);
975 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
979 error = got_ref_open(&head_ref, repo, branch_name, 0);
983 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
987 error = got_worktree_open(&worktree, worktree_path);
991 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
995 if (!same_path_prefix) {
996 error = got_error(GOT_ERR_PATH_PREFIX);
1000 if (commit_id_str) {
1001 struct got_object_id *commit_id;
1002 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1005 error = check_linear_ancestry(commit_id,
1006 got_worktree_get_base_commit_id(worktree), repo);
1007 if (error != NULL) {
1011 error = check_same_branch(commit_id, head_ref, NULL, repo);
1014 error = got_worktree_set_base_commit_id(worktree, repo,
1021 error = got_pathlist_append(&paths, "", NULL);
1024 error = got_worktree_checkout_files(worktree, &paths, repo,
1025 checkout_progress, worktree_path, check_cancelled, NULL);
1029 printf("Now shut up and hack\n");
1032 got_pathlist_free(&paths);
1033 free(commit_id_str);
1035 free(worktree_path);
1042 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1047 static const struct got_error *
1048 update_progress(void *arg, unsigned char status, const char *path)
1050 int *did_something = arg;
1052 if (status == GOT_STATUS_EXISTS)
1057 /* Base commit bump happens silently. */
1058 if (status == GOT_STATUS_BUMP_BASE)
1061 while (path[0] == '/')
1063 printf("%c %s\n", status, path);
1067 static const struct got_error *
1068 switch_head_ref(struct got_reference *head_ref,
1069 struct got_object_id *commit_id, struct got_worktree *worktree,
1070 struct got_repository *repo)
1072 const struct got_error *err = NULL;
1074 int ref_has_moved = 0;
1076 /* Trivial case: switching between two different references. */
1077 if (strcmp(got_ref_get_name(head_ref),
1078 got_worktree_get_head_ref_name(worktree)) != 0) {
1079 printf("Switching work tree from %s to %s\n",
1080 got_worktree_get_head_ref_name(worktree),
1081 got_ref_get_name(head_ref));
1082 return got_worktree_set_head_ref(worktree, head_ref);
1085 err = check_linear_ancestry(commit_id,
1086 got_worktree_get_base_commit_id(worktree), repo);
1088 if (err->code != GOT_ERR_ANCESTRY)
1095 /* Switching to a rebased branch with the same reference name. */
1096 err = got_object_id_str(&base_id_str,
1097 got_worktree_get_base_commit_id(worktree));
1100 printf("Reference %s now points at a different branch\n",
1101 got_worktree_get_head_ref_name(worktree));
1102 printf("Switching work tree from %s to %s\n", base_id_str,
1103 got_worktree_get_head_ref_name(worktree));
1107 static const struct got_error *
1108 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 const struct got_error *err;
1113 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1117 return got_error(GOT_ERR_REBASING);
1119 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1123 return got_error(GOT_ERR_HISTEDIT_BUSY);
1128 static const struct got_error *
1129 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 char *argv[], struct got_worktree *worktree)
1132 const struct got_error *err = NULL;
1139 return got_error_from_errno("strdup");
1140 return got_pathlist_append(paths, path, NULL);
1143 for (i = 0; i < argc; i++) {
1144 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1147 err = got_pathlist_append(paths, path, NULL);
1157 static const struct got_error *
1158 cmd_update(int argc, char *argv[])
1160 const struct got_error *error = NULL;
1161 struct got_repository *repo = NULL;
1162 struct got_worktree *worktree = NULL;
1163 char *worktree_path = NULL;
1164 struct got_object_id *commit_id = NULL;
1165 char *commit_id_str = NULL;
1166 const char *branch_name = NULL;
1167 struct got_reference *head_ref = NULL;
1168 struct got_pathlist_head paths;
1169 struct got_pathlist_entry *pe;
1170 int ch, did_something = 0;
1174 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1177 branch_name = optarg;
1180 commit_id_str = strdup(optarg);
1181 if (commit_id_str == NULL)
1182 return got_error_from_errno("strdup");
1194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 "unveil", NULL) == -1)
1198 worktree_path = getcwd(NULL, 0);
1199 if (worktree_path == NULL) {
1200 error = got_error_from_errno("getcwd");
1203 error = got_worktree_open(&worktree, worktree_path);
1207 error = check_rebase_or_histedit_in_progress(worktree);
1211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1215 error = apply_unveil(got_repo_get_path(repo), 0,
1216 got_worktree_get_root_path(worktree));
1220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1224 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 got_worktree_get_head_ref_name(worktree), 0);
1228 if (commit_id_str == NULL) {
1229 error = got_ref_resolve(&commit_id, repo, head_ref);
1232 error = got_object_id_str(&commit_id_str, commit_id);
1236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 free(commit_id_str);
1238 commit_id_str = NULL;
1241 error = got_object_id_str(&commit_id_str, commit_id);
1247 struct got_object_id *head_commit_id;
1248 TAILQ_FOREACH(pe, &paths, entry) {
1249 if (pe->path_len == 0)
1251 error = got_error_msg(GOT_ERR_BAD_PATH,
1252 "switching between branches requires that "
1253 "the entire work tree gets updated");
1256 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1259 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 free(head_commit_id);
1263 error = check_same_branch(commit_id, head_ref, NULL, repo);
1266 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1270 error = check_linear_ancestry(commit_id,
1271 got_worktree_get_base_commit_id(worktree), repo);
1272 if (error != NULL) {
1273 if (error->code == GOT_ERR_ANCESTRY)
1274 error = got_error(GOT_ERR_BRANCH_MOVED);
1277 error = check_same_branch(commit_id, head_ref, NULL, repo);
1282 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1284 error = got_worktree_set_base_commit_id(worktree, repo,
1290 error = got_worktree_checkout_files(worktree, &paths, repo,
1291 update_progress, &did_something, check_cancelled, NULL);
1296 printf("Updated to commit %s\n", commit_id_str);
1298 printf("Already up-to-date\n");
1300 free(worktree_path);
1301 TAILQ_FOREACH(pe, &paths, entry)
1302 free((char *)pe->path);
1303 got_pathlist_free(&paths);
1305 free(commit_id_str);
1309 static const struct got_error *
1310 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 int diff_context, struct got_repository *repo)
1313 const struct got_error *err = NULL;
1314 struct got_tree_object *tree1 = NULL, *tree2;
1315 struct got_object_qid *qid;
1316 char *id_str1 = NULL, *id_str2;
1317 struct got_diff_blob_output_unidiff_arg arg;
1319 err = got_object_open_as_tree(&tree2, repo,
1320 got_object_commit_get_tree_id(commit));
1324 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1326 struct got_commit_object *pcommit;
1328 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1332 err = got_object_open_as_tree(&tree1, repo,
1333 got_object_commit_get_tree_id(pcommit));
1334 got_object_commit_close(pcommit);
1338 err = got_object_id_str(&id_str1, qid->id);
1343 err = got_object_id_str(&id_str2, id);
1347 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 arg.diff_context = diff_context;
1349 arg.outfile = stdout;
1350 err = got_diff_tree(tree1, tree2, "", "", repo,
1351 got_diff_blob_output_unidiff, &arg, 1);
1354 got_object_tree_close(tree1);
1355 got_object_tree_close(tree2);
1362 get_datestr(time_t *time, char *datebuf)
1364 char *p, *s = ctime_r(time, datebuf);
1365 p = strchr(s, '\n');
1371 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1373 static const struct got_error *
1374 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1375 struct got_repository *repo, int show_patch, int diff_context,
1376 struct got_reflist_head *refs)
1378 const struct got_error *err = NULL;
1379 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1381 time_t committer_time;
1382 const char *author, *committer;
1383 char *refs_str = NULL;
1384 struct got_reflist_entry *re;
1386 SIMPLEQ_FOREACH(re, refs, entry) {
1389 if (got_object_id_cmp(re->id, id) != 0)
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1394 if (strncmp(name, "refs/", 5) == 0)
1396 if (strncmp(name, "got/", 4) == 0)
1398 if (strncmp(name, "heads/", 6) == 0)
1400 if (strncmp(name, "remotes/", 8) == 0)
1403 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1405 err = got_error_from_errno("asprintf");
1411 err = got_object_id_str(&id_str, id);
1415 printf(GOT_COMMIT_SEP_STR);
1416 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1417 refs_str ? refs_str : "", refs_str ? ")" : "");
1422 printf("from: %s\n", got_object_commit_get_author(commit));
1423 committer_time = got_object_commit_get_committer_time(commit);
1424 datestr = get_datestr(&committer_time, datebuf);
1425 printf("date: %s UTC\n", datestr);
1426 author = got_object_commit_get_author(commit);
1427 committer = got_object_commit_get_committer(commit);
1428 if (strcmp(author, committer) != 0)
1429 printf("via: %s\n", committer);
1430 if (got_object_commit_get_nparents(commit) > 1) {
1431 const struct got_object_id_queue *parent_ids;
1432 struct got_object_qid *qid;
1434 parent_ids = got_object_commit_get_parent_ids(commit);
1435 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1436 err = got_object_id_str(&id_str, qid->id);
1439 printf("parent %d: %s\n", n++, id_str);
1444 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1445 if (logmsg0 == NULL)
1446 return got_error_from_errno("strdup");
1450 line = strsep(&logmsg, "\n");
1452 printf(" %s\n", line);
1457 err = print_patch(commit, id, diff_context, repo);
1462 if (fflush(stdout) != 0 && err == NULL)
1463 err = got_error_from_errno("fflush");
1467 static const struct got_error *
1468 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1469 char *path, int show_patch, int diff_context, int limit,
1470 int first_parent_traversal, struct got_reflist_head *refs)
1472 const struct got_error *err;
1473 struct got_commit_graph *graph;
1475 err = got_commit_graph_open(&graph, root_id, path,
1476 first_parent_traversal, repo);
1479 err = got_commit_graph_iter_start(graph, root_id, repo);
1483 struct got_commit_object *commit;
1484 struct got_object_id *id;
1486 if (sigint_received || sigpipe_received)
1489 err = got_commit_graph_iter_next(&id, graph);
1491 if (err->code == GOT_ERR_ITER_COMPLETED) {
1495 if (err->code != GOT_ERR_ITER_NEED_MORE)
1497 err = got_commit_graph_fetch_commits(graph, 1, repo);
1506 err = got_object_open_as_commit(&commit, repo, id);
1509 err = print_commit(commit, id, repo, show_patch, diff_context,
1511 got_object_commit_close(commit);
1512 if (err || (limit && --limit == 0))
1516 got_commit_graph_close(graph);
1523 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1524 "[-r repository-path] [path]\n", getprogname());
1528 static const struct got_error *
1529 cmd_log(int argc, char *argv[])
1531 const struct got_error *error;
1532 struct got_repository *repo = NULL;
1533 struct got_worktree *worktree = NULL;
1534 struct got_commit_object *commit = NULL;
1535 struct got_object_id *id = NULL;
1536 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1537 char *start_commit = NULL;
1538 int diff_context = 3, ch;
1539 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1541 struct got_reflist_head refs;
1543 SIMPLEQ_INIT(&refs);
1546 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1552 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1558 start_commit = optarg;
1561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1564 err(1, "-C option %s", errstr);
1567 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1569 err(1, "-l option %s", errstr);
1572 first_parent_traversal = 1;
1575 repo_path = realpath(optarg, NULL);
1576 if (repo_path == NULL)
1577 err(1, "-r option");
1578 got_path_strip_trailing_slashes(repo_path);
1589 cwd = getcwd(NULL, 0);
1591 error = got_error_from_errno("getcwd");
1595 error = got_worktree_open(&worktree, cwd);
1596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1603 error = got_error_from_errno("strdup");
1606 } else if (argc == 1) {
1608 error = got_worktree_resolve_path(&path, worktree,
1613 path = strdup(argv[0]);
1615 error = got_error_from_errno("strdup");
1622 if (repo_path == NULL) {
1623 repo_path = worktree ?
1624 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1626 if (repo_path == NULL) {
1627 error = got_error_from_errno("strdup");
1631 error = got_repo_open(&repo, repo_path);
1635 error = apply_unveil(got_repo_get_path(repo), 1,
1636 worktree ? got_worktree_get_root_path(worktree) : NULL);
1640 if (start_commit == NULL) {
1641 struct got_reference *head_ref;
1642 error = got_ref_open(&head_ref, repo,
1643 worktree ? got_worktree_get_head_ref_name(worktree)
1647 error = got_ref_resolve(&id, repo, head_ref);
1648 got_ref_close(head_ref);
1651 error = got_object_open_as_commit(&commit, repo, id);
1653 struct got_reference *ref;
1654 error = got_ref_open(&ref, repo, start_commit, 0);
1655 if (error == NULL) {
1657 error = got_ref_resolve(&id, repo, ref);
1661 error = got_object_get_type(&obj_type, repo, id);
1664 if (obj_type == GOT_OBJ_TYPE_TAG) {
1665 struct got_tag_object *tag;
1666 error = got_object_open_as_tag(&tag, repo, id);
1669 if (got_object_tag_get_object_type(tag) !=
1670 GOT_OBJ_TYPE_COMMIT) {
1671 got_object_tag_close(tag);
1672 error = got_error(GOT_ERR_OBJ_TYPE);
1676 id = got_object_id_dup(
1677 got_object_tag_get_object_id(tag));
1679 error = got_error_from_errno(
1680 "got_object_id_dup");
1681 got_object_tag_close(tag);
1684 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1685 error = got_error(GOT_ERR_OBJ_TYPE);
1688 error = got_object_open_as_commit(&commit, repo, id);
1692 if (commit == NULL) {
1693 error = got_repo_match_object_id_prefix(&id,
1694 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1702 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1707 path = in_repo_path;
1710 error = got_ref_list(&refs, repo);
1714 error = print_commits(id, repo, path, show_patch,
1715 diff_context, limit, first_parent_traversal, &refs);
1722 got_worktree_close(worktree);
1724 const struct got_error *repo_error;
1725 repo_error = got_repo_close(repo);
1729 got_ref_list_free(&refs);
1736 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1737 "[object1 object2 | path]\n", getprogname());
1741 struct print_diff_arg {
1742 struct got_repository *repo;
1743 struct got_worktree *worktree;
1750 static const struct got_error *
1751 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1752 const char *path, struct got_object_id *blob_id,
1753 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1755 struct print_diff_arg *a = arg;
1756 const struct got_error *err = NULL;
1757 struct got_blob_object *blob1 = NULL;
1759 char *abspath = NULL, *label1 = NULL;
1762 if (a->diff_staged) {
1763 if (staged_status != GOT_STATUS_MODIFY &&
1764 staged_status != GOT_STATUS_ADD &&
1765 staged_status != GOT_STATUS_DELETE)
1768 if (staged_status == GOT_STATUS_DELETE)
1770 if (status != GOT_STATUS_MODIFY &&
1771 status != GOT_STATUS_ADD &&
1772 status != GOT_STATUS_DELETE &&
1773 status != GOT_STATUS_CONFLICT)
1777 if (!a->header_shown) {
1778 printf("diff %s %s%s\n", a->id_str,
1779 got_worktree_get_root_path(a->worktree),
1780 a->diff_staged ? " (staged changes)" : "");
1781 a->header_shown = 1;
1784 if (a->diff_staged) {
1785 const char *label1 = NULL, *label2 = NULL;
1786 switch (staged_status) {
1787 case GOT_STATUS_MODIFY:
1791 case GOT_STATUS_ADD:
1794 case GOT_STATUS_DELETE:
1798 return got_error(GOT_ERR_FILE_STATUS);
1800 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1801 label1, label2, a->diff_context, a->repo, stdout);
1804 if (staged_status == GOT_STATUS_ADD ||
1805 staged_status == GOT_STATUS_MODIFY) {
1807 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1811 err = got_object_id_str(&id_str, staged_blob_id);
1814 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1820 } else if (status != GOT_STATUS_ADD) {
1821 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1826 if (status != GOT_STATUS_DELETE) {
1827 if (asprintf(&abspath, "%s/%s",
1828 got_worktree_get_root_path(a->worktree), path) == -1) {
1829 err = got_error_from_errno("asprintf");
1833 f2 = fopen(abspath, "r");
1835 err = got_error_from_errno2("fopen", abspath);
1838 if (lstat(abspath, &sb) == -1) {
1839 err = got_error_from_errno2("lstat", abspath);
1845 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1846 a->diff_context, stdout);
1849 got_object_blob_close(blob1);
1850 if (f2 && fclose(f2) != 0 && err == NULL)
1851 err = got_error_from_errno("fclose");
1856 static const struct got_error *
1857 match_object_id(struct got_object_id **id, char **label,
1858 const char *id_str, int obj_type, struct got_repository *repo)
1860 const struct got_error *err;
1861 struct got_tag_object *tag;
1862 struct got_reference *ref = NULL;
1867 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1869 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1871 err = got_error_from_errno("got_object_id_dup");
1872 if (asprintf(label, "refs/tags/%s",
1873 got_object_tag_get_name(tag)) == -1)
1874 err = got_error_from_errno("asprintf");
1875 got_object_tag_close(tag);
1877 } else if (err->code != GOT_ERR_NO_OBJ)
1880 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1882 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1884 err = got_ref_open(&ref, repo, id_str, 0);
1887 *label = strdup(got_ref_get_name(ref));
1888 if (*label == NULL) {
1889 err = got_error_from_errno("strdup");
1892 err = got_ref_resolve(id, repo, ref);
1894 err = got_object_id_str(label, *id);
1895 if (*label == NULL) {
1896 err = got_error_from_errno("strdup");
1907 static const struct got_error *
1908 cmd_diff(int argc, char *argv[])
1910 const struct got_error *error;
1911 struct got_repository *repo = NULL;
1912 struct got_worktree *worktree = NULL;
1913 char *cwd = NULL, *repo_path = NULL;
1914 struct got_object_id *id1 = NULL, *id2 = NULL;
1915 const char *id_str1 = NULL, *id_str2 = NULL;
1916 char *label1 = NULL, *label2 = NULL;
1918 int diff_context = 3, diff_staged = 0, ch;
1923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1928 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1931 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1933 err(1, "-C option %s", errstr);
1936 repo_path = realpath(optarg, NULL);
1937 if (repo_path == NULL)
1938 err(1, "-r option");
1939 got_path_strip_trailing_slashes(repo_path);
1953 cwd = getcwd(NULL, 0);
1955 error = got_error_from_errno("getcwd");
1958 error = got_worktree_open(&worktree, cwd);
1959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1962 if (worktree == NULL) {
1963 error = got_error(GOT_ERR_NOT_WORKTREE);
1968 "-r option can't be used when diffing a work tree");
1969 repo_path = strdup(got_worktree_get_repo_path(worktree));
1970 if (repo_path == NULL) {
1971 error = got_error_from_errno("strdup");
1975 error = got_worktree_resolve_path(&path, worktree,
1982 error = got_error_from_errno("strdup");
1986 } else if (argc == 2) {
1988 errx(1, "-s option can't be used when diffing "
1989 "objects in repository");
1992 if (worktree && repo_path == NULL) {
1994 strdup(got_worktree_get_repo_path(worktree));
1995 if (repo_path == NULL) {
1996 error = got_error_from_errno("strdup");
2003 if (repo_path == NULL) {
2004 repo_path = getcwd(NULL, 0);
2005 if (repo_path == NULL)
2006 return got_error_from_errno("getcwd");
2009 error = got_repo_open(&repo, repo_path);
2014 error = apply_unveil(got_repo_get_path(repo), 1,
2015 worktree ? got_worktree_get_root_path(worktree) : NULL);
2020 struct print_diff_arg arg;
2021 struct got_pathlist_head paths;
2026 error = got_object_id_str(&id_str,
2027 got_worktree_get_base_commit_id(worktree));
2031 arg.worktree = worktree;
2032 arg.diff_context = diff_context;
2033 arg.id_str = id_str;
2034 arg.header_shown = 0;
2035 arg.diff_staged = diff_staged;
2037 error = got_pathlist_append(&paths, path, NULL);
2041 error = got_worktree_status(worktree, &paths, repo, print_diff,
2042 &arg, check_cancelled, NULL);
2044 got_pathlist_free(&paths);
2048 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2052 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2056 error = got_object_get_type(&type1, repo, id1);
2060 error = got_object_get_type(&type2, repo, id2);
2064 if (type1 != type2) {
2065 error = got_error(GOT_ERR_OBJ_TYPE);
2070 case GOT_OBJ_TYPE_BLOB:
2071 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2072 diff_context, repo, stdout);
2074 case GOT_OBJ_TYPE_TREE:
2075 error = got_diff_objects_as_trees(id1, id2, "", "",
2076 diff_context, repo, stdout);
2078 case GOT_OBJ_TYPE_COMMIT:
2079 printf("diff %s %s\n", label1, label2);
2080 error = got_diff_objects_as_commits(id1, id2, diff_context,
2084 error = got_error(GOT_ERR_OBJ_TYPE);
2094 got_worktree_close(worktree);
2096 const struct got_error *repo_error;
2097 repo_error = got_repo_close(repo);
2108 "usage: %s blame [-c commit] [-r repository-path] path\n",
2113 static const struct got_error *
2114 cmd_blame(int argc, char *argv[])
2116 const struct got_error *error;
2117 struct got_repository *repo = NULL;
2118 struct got_worktree *worktree = NULL;
2119 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2120 struct got_object_id *commit_id = NULL;
2121 char *commit_id_str = NULL;
2125 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2130 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2133 commit_id_str = optarg;
2136 repo_path = realpath(optarg, NULL);
2137 if (repo_path == NULL)
2138 err(1, "-r option");
2139 got_path_strip_trailing_slashes(repo_path);
2155 cwd = getcwd(NULL, 0);
2157 error = got_error_from_errno("getcwd");
2160 if (repo_path == NULL) {
2161 error = got_worktree_open(&worktree, cwd);
2162 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2168 strdup(got_worktree_get_repo_path(worktree));
2169 if (repo_path == NULL)
2170 error = got_error_from_errno("strdup");
2174 repo_path = strdup(cwd);
2175 if (repo_path == NULL) {
2176 error = got_error_from_errno("strdup");
2182 error = got_repo_open(&repo, repo_path);
2186 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2191 const char *prefix = got_worktree_get_path_prefix(worktree);
2192 char *p, *worktree_subdir = cwd +
2193 strlen(got_worktree_get_root_path(worktree));
2194 if (asprintf(&p, "%s%s%s%s%s",
2195 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2196 worktree_subdir, worktree_subdir[0] ? "/" : "",
2198 error = got_error_from_errno("asprintf");
2201 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2204 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2209 if (commit_id_str == NULL) {
2210 struct got_reference *head_ref;
2211 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2214 error = got_ref_resolve(&commit_id, repo, head_ref);
2215 got_ref_close(head_ref);
2219 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2224 error = got_blame(in_repo_path, commit_id, repo, stdout);
2231 got_worktree_close(worktree);
2233 const struct got_error *repo_error;
2234 repo_error = got_repo_close(repo);
2245 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2251 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2252 const char *root_path)
2254 int is_root_path = (strcmp(path, root_path) == 0);
2255 const char *modestr = "";
2257 path += strlen(root_path);
2258 while (path[0] == '/')
2261 if (S_ISLNK(te->mode))
2263 else if (S_ISDIR(te->mode))
2265 else if (te->mode & S_IXUSR)
2268 printf("%s%s%s%s%s\n", id ? id : "", path,
2269 is_root_path ? "" : "/", te->name, modestr);
2272 static const struct got_error *
2273 print_tree(const char *path, struct got_object_id *commit_id,
2274 int show_ids, int recurse, const char *root_path,
2275 struct got_repository *repo)
2277 const struct got_error *err = NULL;
2278 struct got_object_id *tree_id = NULL;
2279 struct got_tree_object *tree = NULL;
2280 const struct got_tree_entries *entries;
2281 struct got_tree_entry *te;
2283 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2287 err = got_object_open_as_tree(&tree, repo, tree_id);
2290 entries = got_object_tree_get_entries(tree);
2291 te = SIMPLEQ_FIRST(&entries->head);
2295 if (sigint_received || sigpipe_received)
2300 err = got_object_id_str(&id_str, te->id);
2303 if (asprintf(&id, "%s ", id_str) == -1) {
2304 err = got_error_from_errno("asprintf");
2310 print_entry(te, id, path, root_path);
2313 if (recurse && S_ISDIR(te->mode)) {
2315 if (asprintf(&child_path, "%s%s%s", path,
2316 path[0] == '/' && path[1] == '\0' ? "" : "/",
2318 err = got_error_from_errno("asprintf");
2321 err = print_tree(child_path, commit_id, show_ids, 1,
2328 te = SIMPLEQ_NEXT(te, entry);
2332 got_object_tree_close(tree);
2337 static const struct got_error *
2338 cmd_tree(int argc, char *argv[])
2340 const struct got_error *error;
2341 struct got_repository *repo = NULL;
2342 struct got_worktree *worktree = NULL;
2344 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2345 struct got_object_id *commit_id = NULL;
2346 char *commit_id_str = NULL;
2347 int show_ids = 0, recurse = 0;
2351 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2356 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2359 commit_id_str = optarg;
2362 repo_path = realpath(optarg, NULL);
2363 if (repo_path == NULL)
2364 err(1, "-r option");
2365 got_path_strip_trailing_slashes(repo_path);
2389 cwd = getcwd(NULL, 0);
2391 error = got_error_from_errno("getcwd");
2394 if (repo_path == NULL) {
2395 error = got_worktree_open(&worktree, cwd);
2396 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2402 strdup(got_worktree_get_repo_path(worktree));
2403 if (repo_path == NULL)
2404 error = got_error_from_errno("strdup");
2408 repo_path = strdup(cwd);
2409 if (repo_path == NULL) {
2410 error = got_error_from_errno("strdup");
2416 error = got_repo_open(&repo, repo_path);
2420 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2426 char *p, *worktree_subdir = cwd +
2427 strlen(got_worktree_get_root_path(worktree));
2428 if (asprintf(&p, "%s/%s",
2429 got_worktree_get_path_prefix(worktree),
2430 worktree_subdir) == -1) {
2431 error = got_error_from_errno("asprintf");
2434 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2441 if (in_repo_path == NULL) {
2442 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2447 if (commit_id_str == NULL) {
2448 struct got_reference *head_ref;
2449 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2452 error = got_ref_resolve(&commit_id, repo, head_ref);
2453 got_ref_close(head_ref);
2457 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2462 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2463 in_repo_path, repo);
2470 got_worktree_close(worktree);
2472 const struct got_error *repo_error;
2473 repo_error = got_repo_close(repo);
2483 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2487 static const struct got_error *
2488 print_status(void *arg, unsigned char status, unsigned char staged_status,
2489 const char *path, struct got_object_id *blob_id,
2490 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2492 if (status == staged_status && (status == GOT_STATUS_DELETE))
2493 status = GOT_STATUS_NO_CHANGE;
2494 printf("%c%c %s\n", status, staged_status, path);
2498 static const struct got_error *
2499 cmd_status(int argc, char *argv[])
2501 const struct got_error *error = NULL;
2502 struct got_repository *repo = NULL;
2503 struct got_worktree *worktree = NULL;
2505 struct got_pathlist_head paths;
2506 struct got_pathlist_entry *pe;
2511 while ((ch = getopt(argc, argv, "")) != -1) {
2523 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2527 cwd = getcwd(NULL, 0);
2529 error = got_error_from_errno("getcwd");
2533 error = got_worktree_open(&worktree, cwd);
2537 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2541 error = apply_unveil(got_repo_get_path(repo), 1,
2542 got_worktree_get_root_path(worktree));
2546 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2550 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2551 check_cancelled, NULL);
2553 TAILQ_FOREACH(pe, &paths, entry)
2554 free((char *)pe->path);
2555 got_pathlist_free(&paths);
2564 "usage: %s ref [-r repository] -l | -d name | name target\n",
2569 static const struct got_error *
2570 list_refs(struct got_repository *repo)
2572 static const struct got_error *err = NULL;
2573 struct got_reflist_head refs;
2574 struct got_reflist_entry *re;
2576 SIMPLEQ_INIT(&refs);
2577 err = got_ref_list(&refs, repo);
2581 SIMPLEQ_FOREACH(re, &refs, entry) {
2583 refstr = got_ref_to_str(re->ref);
2585 return got_error_from_errno("got_ref_to_str");
2586 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2590 got_ref_list_free(&refs);
2594 static const struct got_error *
2595 delete_ref(struct got_repository *repo, const char *refname)
2597 const struct got_error *err = NULL;
2598 struct got_reference *ref;
2600 err = got_ref_open(&ref, repo, refname, 0);
2604 err = got_ref_delete(ref, repo);
2609 static const struct got_error *
2610 add_ref(struct got_repository *repo, const char *refname, const char *target)
2612 const struct got_error *err = NULL;
2613 struct got_object_id *id;
2614 struct got_reference *ref = NULL;
2617 * Don't let the user create a reference named '-'.
2618 * While technically a valid reference name, this case is usually
2619 * an unintended typo.
2621 if (refname[0] == '-' && refname[1] == '\0')
2622 return got_error(GOT_ERR_BAD_REF_NAME);
2624 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2627 struct got_reference *target_ref;
2629 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2631 err = got_ref_open(&target_ref, repo, target, 0);
2634 err = got_ref_resolve(&id, repo, target_ref);
2635 got_ref_close(target_ref);
2640 err = got_ref_alloc(&ref, refname, id);
2644 err = got_ref_write(ref, repo);
2652 static const struct got_error *
2653 cmd_ref(int argc, char *argv[])
2655 const struct got_error *error = NULL;
2656 struct got_repository *repo = NULL;
2657 struct got_worktree *worktree = NULL;
2658 char *cwd = NULL, *repo_path = NULL;
2659 int ch, do_list = 0;
2660 const char *delref = NULL;
2662 /* TODO: Add -s option for adding symbolic references. */
2663 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2669 repo_path = realpath(optarg, NULL);
2670 if (repo_path == NULL)
2671 err(1, "-r option");
2672 got_path_strip_trailing_slashes(repo_path);
2683 if (do_list && delref)
2684 errx(1, "-l and -d options are mutually exclusive\n");
2689 if (do_list || delref) {
2692 } else if (argc != 2)
2697 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2701 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2702 "sendfd unveil", NULL) == -1)
2706 cwd = getcwd(NULL, 0);
2708 error = got_error_from_errno("getcwd");
2712 if (repo_path == NULL) {
2713 error = got_worktree_open(&worktree, cwd);
2714 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2720 strdup(got_worktree_get_repo_path(worktree));
2721 if (repo_path == NULL)
2722 error = got_error_from_errno("strdup");
2726 repo_path = strdup(cwd);
2727 if (repo_path == NULL) {
2728 error = got_error_from_errno("strdup");
2734 error = got_repo_open(&repo, repo_path);
2738 error = apply_unveil(got_repo_get_path(repo), do_list,
2739 worktree ? got_worktree_get_root_path(worktree) : NULL);
2744 error = list_refs(repo);
2746 error = delete_ref(repo, delref);
2748 error = add_ref(repo, argv[0], argv[1]);
2751 got_repo_close(repo);
2753 got_worktree_close(worktree);
2763 "usage: %s branch [-r repository] -l | -d name | "
2764 "name [base-branch]\n", getprogname());
2768 static const struct got_error *
2769 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2771 static const struct got_error *err = NULL;
2772 struct got_reflist_head refs;
2773 struct got_reflist_entry *re;
2775 SIMPLEQ_INIT(&refs);
2777 err = got_ref_list(&refs, repo);
2781 SIMPLEQ_FOREACH(re, &refs, entry) {
2782 const char *refname, *marker = " ";
2784 refname = got_ref_get_name(re->ref);
2785 if (strncmp(refname, "refs/heads/", 11) != 0)
2787 if (worktree && strcmp(refname,
2788 got_worktree_get_head_ref_name(worktree)) == 0) {
2789 struct got_object_id *id = NULL;
2790 err = got_ref_resolve(&id, repo, re->ref);
2793 if (got_object_id_cmp(id,
2794 got_worktree_get_base_commit_id(worktree)) == 0)
2801 refstr = got_ref_to_str(re->ref);
2803 return got_error_from_errno("got_ref_to_str");
2804 printf("%s%s: %s\n", marker, refname, refstr);
2808 got_ref_list_free(&refs);
2812 static const struct got_error *
2813 delete_branch(struct got_repository *repo, const char *branch_name)
2815 const struct got_error *err = NULL;
2816 struct got_reference *ref;
2819 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2820 return got_error_from_errno("asprintf");
2822 err = got_ref_open(&ref, repo, refname, 0);
2826 err = got_ref_delete(ref, repo);
2833 static const struct got_error *
2834 add_branch(struct got_repository *repo, const char *branch_name,
2835 const char *base_branch)
2837 const struct got_error *err = NULL;
2838 struct got_object_id *id = NULL;
2839 struct got_reference *ref = NULL;
2840 char *base_refname = NULL, *refname = NULL;
2841 struct got_reference *base_ref;
2844 * Don't let the user create a branch named '-'.
2845 * While technically a valid reference name, this case is usually
2846 * an unintended typo.
2848 if (branch_name[0] == '-' && branch_name[1] == '\0')
2849 return got_error(GOT_ERR_BAD_REF_NAME);
2851 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2852 base_refname = strdup(GOT_REF_HEAD);
2853 if (base_refname == NULL)
2854 return got_error_from_errno("strdup");
2855 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2856 return got_error_from_errno("asprintf");
2858 err = got_ref_open(&base_ref, repo, base_refname, 0);
2861 err = got_ref_resolve(&id, repo, base_ref);
2862 got_ref_close(base_ref);
2866 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2867 err = got_error_from_errno("asprintf");
2871 err = got_ref_open(&ref, repo, refname, 0);
2873 err = got_error(GOT_ERR_BRANCH_EXISTS);
2875 } else if (err->code != GOT_ERR_NOT_REF)
2878 err = got_ref_alloc(&ref, refname, id);
2882 err = got_ref_write(ref, repo);
2892 static const struct got_error *
2893 cmd_branch(int argc, char *argv[])
2895 const struct got_error *error = NULL;
2896 struct got_repository *repo = NULL;
2897 struct got_worktree *worktree = NULL;
2898 char *cwd = NULL, *repo_path = NULL;
2899 int ch, do_list = 0;
2900 const char *delref = NULL;
2902 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2908 repo_path = realpath(optarg, NULL);
2909 if (repo_path == NULL)
2910 err(1, "-r option");
2911 got_path_strip_trailing_slashes(repo_path);
2922 if (do_list && delref)
2923 errx(1, "-l and -d options are mutually exclusive\n");
2928 if (do_list || delref) {
2931 } else if (argc < 1 || argc > 2)
2936 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2940 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2941 "sendfd unveil", NULL) == -1)
2945 cwd = getcwd(NULL, 0);
2947 error = got_error_from_errno("getcwd");
2951 if (repo_path == NULL) {
2952 error = got_worktree_open(&worktree, cwd);
2953 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2959 strdup(got_worktree_get_repo_path(worktree));
2960 if (repo_path == NULL)
2961 error = got_error_from_errno("strdup");
2965 repo_path = strdup(cwd);
2966 if (repo_path == NULL) {
2967 error = got_error_from_errno("strdup");
2973 error = got_repo_open(&repo, repo_path);
2977 error = apply_unveil(got_repo_get_path(repo), do_list,
2978 worktree ? got_worktree_get_root_path(worktree) : NULL);
2983 error = list_branches(repo, worktree);
2985 error = delete_branch(repo, delref);
2987 const char *base_branch;
2989 base_branch = worktree ?
2990 got_worktree_get_head_ref_name(worktree) :
2992 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2995 base_branch = argv[1];
2996 error = add_branch(repo, argv[0], base_branch);
3000 got_repo_close(repo);
3002 got_worktree_close(worktree);
3011 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3015 static const struct got_error *
3016 cmd_add(int argc, char *argv[])
3018 const struct got_error *error = NULL;
3019 struct got_repository *repo = NULL;
3020 struct got_worktree *worktree = NULL;
3022 struct got_pathlist_head paths;
3023 struct got_pathlist_entry *pe;
3028 while ((ch = getopt(argc, argv, "")) != -1) {
3040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3047 cwd = getcwd(NULL, 0);
3049 error = got_error_from_errno("getcwd");
3053 error = got_worktree_open(&worktree, cwd);
3057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3061 error = apply_unveil(got_repo_get_path(repo), 1,
3062 got_worktree_get_root_path(worktree));
3066 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3070 error = got_worktree_schedule_add(worktree, &paths, print_status,
3074 got_repo_close(repo);
3076 got_worktree_close(worktree);
3077 TAILQ_FOREACH(pe, &paths, entry)
3078 free((char *)pe->path);
3079 got_pathlist_free(&paths);
3087 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3091 static const struct got_error *
3092 cmd_remove(int argc, char *argv[])
3094 const struct got_error *error = NULL;
3095 struct got_worktree *worktree = NULL;
3096 struct got_repository *repo = NULL;
3098 struct got_pathlist_head paths;
3099 struct got_pathlist_entry *pe;
3100 int ch, delete_local_mods = 0;
3104 while ((ch = getopt(argc, argv, "f")) != -1) {
3107 delete_local_mods = 1;
3119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3126 cwd = getcwd(NULL, 0);
3128 error = got_error_from_errno("getcwd");
3131 error = got_worktree_open(&worktree, cwd);
3135 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3139 error = apply_unveil(got_repo_get_path(repo), 1,
3140 got_worktree_get_root_path(worktree));
3144 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3148 error = got_worktree_schedule_delete(worktree, &paths,
3149 delete_local_mods, print_status, NULL, repo);
3154 got_repo_close(repo);
3156 got_worktree_close(worktree);
3157 TAILQ_FOREACH(pe, &paths, entry)
3158 free((char *)pe->path);
3159 got_pathlist_free(&paths);
3167 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3168 "path ...\n", getprogname());
3172 static const struct got_error *
3173 revert_progress(void *arg, unsigned char status, const char *path)
3175 while (path[0] == '/')
3177 printf("%c %s\n", status, path);
3181 struct choose_patch_arg {
3182 FILE *patch_script_file;
3186 static const struct got_error *
3187 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3188 int nchanges, const char *action)
3191 size_t linesize = 0;
3195 case GOT_STATUS_ADD:
3196 printf("A %s\n%s this addition? [y/n] ", path, action);
3198 case GOT_STATUS_DELETE:
3199 printf("D %s\n%s this deletion? [y/n] ", path, action);
3201 case GOT_STATUS_MODIFY:
3202 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3203 return got_error_from_errno("fseek");
3204 printf(GOT_COMMIT_SEP_STR);
3205 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3207 if (ferror(patch_file))
3208 return got_error_from_errno("getline");
3209 printf(GOT_COMMIT_SEP_STR);
3210 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3211 path, n, nchanges, action);
3214 return got_error_path(path, GOT_ERR_FILE_STATUS);
3220 static const struct got_error *
3221 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3222 FILE *patch_file, int n, int nchanges)
3224 const struct got_error *err = NULL;
3226 size_t linesize = 0;
3229 struct choose_patch_arg *a = arg;
3231 *choice = GOT_PATCH_CHOICE_NONE;
3233 if (a->patch_script_file) {
3235 err = show_change(status, path, patch_file, n, nchanges,
3239 linelen = getline(&line, &linesize, a->patch_script_file);
3240 if (linelen == -1) {
3241 if (ferror(a->patch_script_file))
3242 return got_error_from_errno("getline");
3245 nl = strchr(line, '\n');
3248 if (strcmp(line, "y") == 0) {
3249 *choice = GOT_PATCH_CHOICE_YES;
3251 } else if (strcmp(line, "n") == 0) {
3252 *choice = GOT_PATCH_CHOICE_NO;
3254 } else if (strcmp(line, "q") == 0 &&
3255 status == GOT_STATUS_MODIFY) {
3256 *choice = GOT_PATCH_CHOICE_QUIT;
3259 printf("invalid response '%s'\n", line);
3264 while (resp != 'y' && resp != 'n' && resp != 'q') {
3265 err = show_change(status, path, patch_file, n, nchanges,
3272 if (status == GOT_STATUS_MODIFY) {
3273 if (resp != 'y' && resp != 'n' && resp != 'q') {
3274 printf("invalid response '%c'\n", resp);
3277 } else if (resp != 'y' && resp != 'n') {
3278 printf("invalid response '%c'\n", resp);
3284 *choice = GOT_PATCH_CHOICE_YES;
3285 else if (resp == 'n')
3286 *choice = GOT_PATCH_CHOICE_NO;
3287 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3288 *choice = GOT_PATCH_CHOICE_QUIT;
3294 static const struct got_error *
3295 cmd_revert(int argc, char *argv[])
3297 const struct got_error *error = NULL;
3298 struct got_worktree *worktree = NULL;
3299 struct got_repository *repo = NULL;
3300 char *cwd = NULL, *path = NULL;
3301 struct got_pathlist_head paths;
3302 struct got_pathlist_entry *pe;
3303 int ch, can_recurse = 0, pflag = 0;
3304 FILE *patch_script_file = NULL;
3305 const char *patch_script_path = NULL;
3306 struct choose_patch_arg cpa;
3310 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3316 patch_script_path = optarg;
3331 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3332 "unveil", NULL) == -1)
3337 if (patch_script_path && !pflag)
3338 errx(1, "-F option can only be used together with -p option");
3340 cwd = getcwd(NULL, 0);
3342 error = got_error_from_errno("getcwd");
3345 error = got_worktree_open(&worktree, cwd);
3349 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3353 if (patch_script_path) {
3354 patch_script_file = fopen(patch_script_path, "r");
3355 if (patch_script_file == NULL) {
3356 error = got_error_from_errno2("fopen",
3361 error = apply_unveil(got_repo_get_path(repo), 1,
3362 got_worktree_get_root_path(worktree));
3366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3373 TAILQ_FOREACH(pe, &paths, entry) {
3374 if (asprintf(&ondisk_path, "%s/%s",
3375 got_worktree_get_root_path(worktree),
3377 error = got_error_from_errno("asprintf");
3380 if (lstat(ondisk_path, &sb) == -1) {
3381 if (errno == ENOENT) {
3385 error = got_error_from_errno2("lstat",
3391 if (S_ISDIR(sb.st_mode)) {
3392 error = got_error_msg(GOT_ERR_BAD_PATH,
3393 "reverting directories requires -R option");
3399 cpa.patch_script_file = patch_script_file;
3400 cpa.action = "revert";
3401 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3402 pflag ? choose_patch : NULL, &cpa, repo);
3406 if (patch_script_file && fclose(patch_script_file) == EOF &&
3408 error = got_error_from_errno2("fclose", patch_script_path);
3410 got_repo_close(repo);
3412 got_worktree_close(worktree);
3421 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3426 struct collect_commit_logmsg_arg {
3427 const char *cmdline_log;
3429 const char *worktree_path;
3430 const char *branch_name;
3431 const char *repo_path;
3436 static const struct got_error *
3437 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3440 char *initial_content = NULL;
3441 struct got_pathlist_entry *pe;
3442 const struct got_error *err = NULL;
3443 char *template = NULL;
3444 struct collect_commit_logmsg_arg *a = arg;
3448 /* if a message was specified on the command line, just use it */
3449 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3450 len = strlen(a->cmdline_log) + 1;
3451 *logmsg = malloc(len + 1);
3452 if (*logmsg == NULL)
3453 return got_error_from_errno("malloc");
3454 strlcpy(*logmsg, a->cmdline_log, len);
3458 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3459 return got_error_from_errno("asprintf");
3461 if (asprintf(&initial_content,
3462 "\n# changes to be committed on branch %s:\n",
3463 a->branch_name) == -1)
3464 return got_error_from_errno("asprintf");
3466 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3470 dprintf(fd, initial_content);
3472 TAILQ_FOREACH(pe, commitable_paths, entry) {
3473 struct got_commitable *ct = pe->data;
3474 dprintf(fd, "# %c %s\n",
3475 got_commitable_get_status(ct),
3476 got_commitable_get_path(ct));
3480 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3482 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3483 unlink(a->logmsg_path);
3484 free(a->logmsg_path);
3485 a->logmsg_path = NULL;
3487 free(initial_content);
3490 /* Editor is done; we can now apply unveil(2) */
3492 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3501 static const struct got_error *
3502 cmd_commit(int argc, char *argv[])
3504 const struct got_error *error = NULL;
3505 struct got_worktree *worktree = NULL;
3506 struct got_repository *repo = NULL;
3507 char *cwd = NULL, *id_str = NULL;
3508 struct got_object_id *id = NULL;
3509 const char *logmsg = NULL;
3511 struct collect_commit_logmsg_arg cl_arg;
3512 char *editor = NULL;
3513 int ch, rebase_in_progress, histedit_in_progress;
3514 struct got_pathlist_head paths;
3517 cl_arg.logmsg_path = NULL;
3519 while ((ch = getopt(argc, argv, "m:")) != -1) {
3534 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3535 "unveil", NULL) == -1)
3538 error = get_author(&author);
3542 cwd = getcwd(NULL, 0);
3544 error = got_error_from_errno("getcwd");
3547 error = got_worktree_open(&worktree, cwd);
3551 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3554 if (rebase_in_progress) {
3555 error = got_error(GOT_ERR_REBASING);
3559 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3569 * unveil(2) traverses exec(2); if an editor is used we have
3570 * to apply unveil after the log message has been written.
3572 if (logmsg == NULL || strlen(logmsg) == 0)
3573 error = get_editor(&editor);
3575 error = apply_unveil(got_repo_get_path(repo), 0,
3576 got_worktree_get_root_path(worktree));
3580 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3584 cl_arg.editor = editor;
3585 cl_arg.cmdline_log = logmsg;
3586 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3587 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3588 if (!histedit_in_progress) {
3589 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3590 error = got_error(GOT_ERR_COMMIT_BRANCH);
3593 cl_arg.branch_name += 11;
3595 cl_arg.repo_path = got_repo_get_path(repo);
3596 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3597 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3599 if (cl_arg.logmsg_path)
3600 fprintf(stderr, "%s: log message preserved in %s\n",
3601 getprogname(), cl_arg.logmsg_path);
3605 if (cl_arg.logmsg_path)
3606 unlink(cl_arg.logmsg_path);
3608 error = got_object_id_str(&id_str, id);
3611 printf("Created commit %s\n", id_str);
3613 free(cl_arg.logmsg_path);
3615 got_repo_close(repo);
3617 got_worktree_close(worktree);
3625 usage_cherrypick(void)
3627 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3631 static const struct got_error *
3632 cmd_cherrypick(int argc, char *argv[])
3634 const struct got_error *error = NULL;
3635 struct got_worktree *worktree = NULL;
3636 struct got_repository *repo = NULL;
3637 char *cwd = NULL, *commit_id_str = NULL;
3638 struct got_object_id *commit_id = NULL;
3639 struct got_commit_object *commit = NULL;
3640 struct got_object_qid *pid;
3641 struct got_reference *head_ref = NULL;
3642 int ch, did_something = 0;
3644 while ((ch = getopt(argc, argv, "")) != -1) {
3656 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3657 "unveil", NULL) == -1)
3663 cwd = getcwd(NULL, 0);
3665 error = got_error_from_errno("getcwd");
3668 error = got_worktree_open(&worktree, cwd);
3672 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3676 error = apply_unveil(got_repo_get_path(repo), 0,
3677 got_worktree_get_root_path(worktree));
3681 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3682 GOT_OBJ_TYPE_COMMIT, repo);
3683 if (error != NULL) {
3684 struct got_reference *ref;
3685 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3687 error = got_ref_open(&ref, repo, argv[0], 0);
3690 error = got_ref_resolve(&commit_id, repo, ref);
3695 error = got_object_id_str(&commit_id_str, commit_id);
3699 error = got_ref_open(&head_ref, repo,
3700 got_worktree_get_head_ref_name(worktree), 0);
3704 error = check_same_branch(commit_id, head_ref, NULL, repo);
3706 if (error->code != GOT_ERR_ANCESTRY)
3710 error = got_error(GOT_ERR_SAME_BRANCH);
3714 error = got_object_open_as_commit(&commit, repo, commit_id);
3717 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3718 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3719 commit_id, repo, update_progress, &did_something, check_cancelled,
3725 printf("Merged commit %s\n", commit_id_str);
3728 got_object_commit_close(commit);
3729 free(commit_id_str);
3731 got_ref_close(head_ref);
3733 got_worktree_close(worktree);
3735 got_repo_close(repo);
3742 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3746 static const struct got_error *
3747 cmd_backout(int argc, char *argv[])
3749 const struct got_error *error = NULL;
3750 struct got_worktree *worktree = NULL;
3751 struct got_repository *repo = NULL;
3752 char *cwd = NULL, *commit_id_str = NULL;
3753 struct got_object_id *commit_id = NULL;
3754 struct got_commit_object *commit = NULL;
3755 struct got_object_qid *pid;
3756 struct got_reference *head_ref = NULL;
3757 int ch, did_something = 0;
3759 while ((ch = getopt(argc, argv, "")) != -1) {
3771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3772 "unveil", NULL) == -1)
3778 cwd = getcwd(NULL, 0);
3780 error = got_error_from_errno("getcwd");
3783 error = got_worktree_open(&worktree, cwd);
3787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3791 error = apply_unveil(got_repo_get_path(repo), 0,
3792 got_worktree_get_root_path(worktree));
3796 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3797 GOT_OBJ_TYPE_COMMIT, repo);
3798 if (error != NULL) {
3799 struct got_reference *ref;
3800 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3802 error = got_ref_open(&ref, repo, argv[0], 0);
3805 error = got_ref_resolve(&commit_id, repo, ref);
3810 error = got_object_id_str(&commit_id_str, commit_id);
3814 error = got_ref_open(&head_ref, repo,
3815 got_worktree_get_head_ref_name(worktree), 0);
3819 error = check_same_branch(commit_id, head_ref, NULL, repo);
3823 error = got_object_open_as_commit(&commit, repo, commit_id);
3826 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3828 error = got_error(GOT_ERR_ROOT_COMMIT);
3832 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3833 update_progress, &did_something, check_cancelled, NULL);
3838 printf("Backed out commit %s\n", commit_id_str);
3841 got_object_commit_close(commit);
3842 free(commit_id_str);
3844 got_ref_close(head_ref);
3846 got_worktree_close(worktree);
3848 got_repo_close(repo);
3855 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3861 trim_logmsg(char *logmsg, int limit)
3866 len = strlen(logmsg);
3870 nl = strchr(logmsg, '\n');
3875 static const struct got_error *
3876 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3878 const char *logmsg0 = NULL;
3880 logmsg0 = got_object_commit_get_logmsg(commit);
3882 while (isspace((unsigned char)logmsg0[0]))
3885 *logmsg = strdup(logmsg0);
3886 if (*logmsg == NULL)
3887 return got_error_from_errno("strdup");
3889 trim_logmsg(*logmsg, limit);
3893 static const struct got_error *
3894 show_rebase_progress(struct got_commit_object *commit,
3895 struct got_object_id *old_id, struct got_object_id *new_id)
3897 const struct got_error *err;
3898 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3900 err = got_object_id_str(&old_id_str, old_id);
3905 err = got_object_id_str(&new_id_str, new_id);
3910 old_id_str[12] = '\0';
3912 new_id_str[12] = '\0';
3914 err = get_short_logmsg(&logmsg, 42, commit);
3918 printf("%s -> %s: %s\n", old_id_str,
3919 new_id_str ? new_id_str : "no-op change", logmsg);
3926 static const struct got_error *
3927 rebase_progress(void *arg, unsigned char status, const char *path)
3929 unsigned char *rebase_status = arg;
3931 while (path[0] == '/')
3933 printf("%c %s\n", status, path);
3935 if (*rebase_status == GOT_STATUS_CONFLICT)
3937 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3938 *rebase_status = status;
3942 static const struct got_error *
3943 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3944 struct got_reference *branch, struct got_reference *new_base_branch,
3945 struct got_reference *tmp_branch, struct got_repository *repo)
3947 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3948 return got_worktree_rebase_complete(worktree, fileindex,
3949 new_base_branch, tmp_branch, branch, repo);
3952 static const struct got_error *
3953 rebase_commit(struct got_pathlist_head *merged_paths,
3954 struct got_worktree *worktree, struct got_fileindex *fileindex,
3955 struct got_reference *tmp_branch,
3956 struct got_object_id *commit_id, struct got_repository *repo)
3958 const struct got_error *error;
3959 struct got_commit_object *commit;
3960 struct got_object_id *new_commit_id;
3962 error = got_object_open_as_commit(&commit, repo, commit_id);
3966 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3967 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3969 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3971 error = show_rebase_progress(commit, commit_id, NULL);
3973 error = show_rebase_progress(commit, commit_id, new_commit_id);
3974 free(new_commit_id);
3977 got_object_commit_close(commit);
3981 struct check_path_prefix_arg {
3982 const char *path_prefix;
3987 static const struct got_error *
3988 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3989 struct got_blob_object *blob2, struct got_object_id *id1,
3990 struct got_object_id *id2, const char *path1, const char *path2,
3991 struct got_repository *repo)
3993 struct check_path_prefix_arg *a = arg;
3995 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3996 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3997 return got_error(a->errcode);
4002 static const struct got_error *
4003 check_path_prefix(struct got_object_id *parent_id,
4004 struct got_object_id *commit_id, const char *path_prefix,
4005 int errcode, struct got_repository *repo)
4007 const struct got_error *err;
4008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4009 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4010 struct check_path_prefix_arg cpp_arg;
4012 if (got_path_is_root_dir(path_prefix))
4015 err = got_object_open_as_commit(&commit, repo, commit_id);
4019 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4023 err = got_object_open_as_tree(&tree1, repo,
4024 got_object_commit_get_tree_id(parent_commit));
4028 err = got_object_open_as_tree(&tree2, repo,
4029 got_object_commit_get_tree_id(commit));
4033 cpp_arg.path_prefix = path_prefix;
4034 while (cpp_arg.path_prefix[0] == '/')
4035 cpp_arg.path_prefix++;
4036 cpp_arg.len = strlen(cpp_arg.path_prefix);
4037 cpp_arg.errcode = errcode;
4038 err = got_diff_tree(tree1, tree2, "", "", repo,
4039 check_path_prefix_in_diff, &cpp_arg, 0);
4042 got_object_tree_close(tree1);
4044 got_object_tree_close(tree2);
4046 got_object_commit_close(commit);
4048 got_object_commit_close(parent_commit);
4052 static const struct got_error *
4053 collect_commits(struct got_object_id_queue *commits,
4054 struct got_object_id *initial_commit_id,
4055 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4056 const char *path_prefix, int path_prefix_errcode,
4057 struct got_repository *repo)
4059 const struct got_error *err = NULL;
4060 struct got_commit_graph *graph = NULL;
4061 struct got_object_id *parent_id = NULL;
4062 struct got_object_qid *qid;
4063 struct got_object_id *commit_id = initial_commit_id;
4065 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4069 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4072 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4073 err = got_commit_graph_iter_next(&parent_id, graph);
4075 if (err->code == GOT_ERR_ITER_COMPLETED) {
4076 err = got_error_msg(GOT_ERR_ANCESTRY,
4077 "ran out of commits to rebase before "
4078 "youngest common ancestor commit has "
4081 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4083 err = got_commit_graph_fetch_commits(graph, 1, repo);
4087 err = check_path_prefix(parent_id, commit_id,
4088 path_prefix, path_prefix_errcode, repo);
4092 err = got_object_qid_alloc(&qid, commit_id);
4095 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4096 commit_id = parent_id;
4100 got_commit_graph_close(graph);
4104 static const struct got_error *
4105 cmd_rebase(int argc, char *argv[])
4107 const struct got_error *error = NULL;
4108 struct got_worktree *worktree = NULL;
4109 struct got_repository *repo = NULL;
4110 struct got_fileindex *fileindex = NULL;
4112 struct got_reference *branch = NULL;
4113 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4114 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4115 struct got_object_id *resume_commit_id = NULL;
4116 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4117 struct got_commit_object *commit = NULL;
4118 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4119 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4120 struct got_object_id_queue commits;
4121 struct got_pathlist_head merged_paths;
4122 const struct got_object_id_queue *parent_ids;
4123 struct got_object_qid *qid, *pid;
4125 SIMPLEQ_INIT(&commits);
4126 TAILQ_INIT(&merged_paths);
4128 while ((ch = getopt(argc, argv, "ac")) != -1) {
4134 continue_rebase = 1;
4146 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4147 "unveil", NULL) == -1)
4150 if (abort_rebase && continue_rebase)
4152 else if (abort_rebase || continue_rebase) {
4155 } else if (argc != 1)
4158 cwd = getcwd(NULL, 0);
4160 error = got_error_from_errno("getcwd");
4163 error = got_worktree_open(&worktree, cwd);
4167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4171 error = apply_unveil(got_repo_get_path(repo), 0,
4172 got_worktree_get_root_path(worktree));
4176 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4182 if (!rebase_in_progress) {
4183 error = got_error(GOT_ERR_NOT_REBASING);
4186 error = got_worktree_rebase_continue(&resume_commit_id,
4187 &new_base_branch, &tmp_branch, &branch, &fileindex,
4191 printf("Switching work tree to %s\n",
4192 got_ref_get_symref_target(new_base_branch));
4193 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4194 new_base_branch, update_progress, &did_something);
4197 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4198 goto done; /* nothing else to do */
4201 if (continue_rebase) {
4202 if (!rebase_in_progress) {
4203 error = got_error(GOT_ERR_NOT_REBASING);
4206 error = got_worktree_rebase_continue(&resume_commit_id,
4207 &new_base_branch, &tmp_branch, &branch, &fileindex,
4212 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4213 resume_commit_id, repo);
4217 yca_id = got_object_id_dup(resume_commit_id);
4218 if (yca_id == NULL) {
4219 error = got_error_from_errno("got_object_id_dup");
4223 error = got_ref_open(&branch, repo, argv[0], 0);
4228 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4232 if (!continue_rebase) {
4233 struct got_object_id *base_commit_id;
4235 base_commit_id = got_worktree_get_base_commit_id(worktree);
4236 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4237 base_commit_id, branch_head_commit_id, repo);
4240 if (yca_id == NULL) {
4241 error = got_error_msg(GOT_ERR_ANCESTRY,
4242 "specified branch shares no common ancestry "
4243 "with work tree's branch");
4247 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4249 if (error->code != GOT_ERR_ANCESTRY)
4253 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4254 "specified branch resolves to a commit which "
4255 "is already contained in work tree's branch");
4258 error = got_worktree_rebase_prepare(&new_base_branch,
4259 &tmp_branch, &fileindex, worktree, branch, repo);
4264 commit_id = branch_head_commit_id;
4265 error = got_object_open_as_commit(&commit, repo, commit_id);
4269 parent_ids = got_object_commit_get_parent_ids(commit);
4270 pid = SIMPLEQ_FIRST(parent_ids);
4272 if (!continue_rebase) {
4274 error = got_worktree_rebase_abort(worktree, fileindex,
4275 repo, new_base_branch, update_progress,
4279 printf("Rebase of %s aborted\n",
4280 got_ref_get_name(branch));
4282 error = got_error(GOT_ERR_EMPTY_REBASE);
4285 error = collect_commits(&commits, commit_id, pid->id,
4286 yca_id, got_worktree_get_path_prefix(worktree),
4287 GOT_ERR_REBASE_PATH, repo);
4288 got_object_commit_close(commit);
4293 if (SIMPLEQ_EMPTY(&commits)) {
4294 if (continue_rebase)
4295 error = rebase_complete(worktree, fileindex,
4296 branch, new_base_branch, tmp_branch, repo);
4298 error = got_error(GOT_ERR_EMPTY_REBASE);
4303 SIMPLEQ_FOREACH(qid, &commits, entry) {
4304 commit_id = qid->id;
4305 parent_id = pid ? pid->id : yca_id;
4308 error = got_worktree_rebase_merge_files(&merged_paths,
4309 worktree, fileindex, parent_id, commit_id, repo,
4310 rebase_progress, &rebase_status, check_cancelled, NULL);
4314 if (rebase_status == GOT_STATUS_CONFLICT) {
4315 got_worktree_rebase_pathlist_free(&merged_paths);
4319 error = rebase_commit(&merged_paths, worktree, fileindex,
4320 tmp_branch, commit_id, repo);
4321 got_worktree_rebase_pathlist_free(&merged_paths);
4326 if (rebase_status == GOT_STATUS_CONFLICT) {
4327 error = got_worktree_rebase_postpone(worktree, fileindex);
4330 error = got_error_msg(GOT_ERR_CONFLICTS,
4331 "conflicts must be resolved before rebasing can continue");
4333 error = rebase_complete(worktree, fileindex, branch,
4334 new_base_branch, tmp_branch, repo);
4336 got_object_id_queue_free(&commits);
4337 free(branch_head_commit_id);
4338 free(resume_commit_id);
4341 got_object_commit_close(commit);
4343 got_ref_close(branch);
4344 if (new_base_branch)
4345 got_ref_close(new_base_branch);
4347 got_ref_close(tmp_branch);
4349 got_worktree_close(worktree);
4351 got_repo_close(repo);
4356 usage_histedit(void)
4358 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4363 #define GOT_HISTEDIT_PICK 'p'
4364 #define GOT_HISTEDIT_EDIT 'e'
4365 #define GOT_HISTEDIT_FOLD 'f'
4366 #define GOT_HISTEDIT_DROP 'd'
4367 #define GOT_HISTEDIT_MESG 'm'
4369 static struct got_histedit_cmd {
4373 } got_histedit_cmds[] = {
4374 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4375 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4376 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4377 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4378 { GOT_HISTEDIT_MESG, "mesg",
4379 "single-line log message for commit above (open editor if empty)" },
4382 struct got_histedit_list_entry {
4383 TAILQ_ENTRY(got_histedit_list_entry) entry;
4384 struct got_object_id *commit_id;
4385 const struct got_histedit_cmd *cmd;
4388 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4390 static const struct got_error *
4391 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4392 FILE *f, struct got_repository *repo)
4394 const struct got_error *err = NULL;
4395 char *logmsg = NULL, *id_str = NULL;
4396 struct got_commit_object *commit = NULL;
4399 err = got_object_open_as_commit(&commit, repo, commit_id);
4403 err = get_short_logmsg(&logmsg, 34, commit);
4407 err = got_object_id_str(&id_str, commit_id);
4411 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4413 err = got_ferror(f, GOT_ERR_IO);
4416 got_object_commit_close(commit);
4422 static const struct got_error *
4423 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4424 struct got_repository *repo)
4426 const struct got_error *err = NULL;
4427 struct got_object_qid *qid;
4429 if (SIMPLEQ_EMPTY(commits))
4430 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4432 SIMPLEQ_FOREACH(qid, commits, entry) {
4433 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4442 static const struct got_error *
4443 write_cmd_list(FILE *f)
4445 const struct got_error *err = NULL;
4448 n = fprintf(f, "# Available histedit commands:\n");
4450 return got_ferror(f, GOT_ERR_IO);
4452 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4453 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4454 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4457 err = got_ferror(f, GOT_ERR_IO);
4461 n = fprintf(f, "# Commits will be processed in order from top to "
4462 "bottom of this file.\n");
4464 return got_ferror(f, GOT_ERR_IO);
4468 static const struct got_error *
4469 histedit_syntax_error(int lineno)
4471 static char msg[42];
4474 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4476 if (ret == -1 || ret >= sizeof(msg))
4477 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4479 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4482 static const struct got_error *
4483 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4484 char *logmsg, struct got_repository *repo)
4486 const struct got_error *err;
4487 struct got_commit_object *folded_commit = NULL;
4490 err = got_object_id_str(&id_str, hle->commit_id);
4494 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4498 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4499 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4500 got_object_commit_get_logmsg(folded_commit)) == -1) {
4501 err = got_error_from_errno("asprintf");
4506 got_object_commit_close(folded_commit);
4511 static struct got_histedit_list_entry *
4512 get_folded_commits(struct got_histedit_list_entry *hle)
4514 struct got_histedit_list_entry *prev, *folded = NULL;
4516 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4517 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4518 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4519 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4521 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4527 static const struct got_error *
4528 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4529 struct got_repository *repo)
4531 char *logmsg_path = NULL, *id_str = NULL;
4532 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4533 const struct got_error *err = NULL;
4534 struct got_commit_object *commit = NULL;
4536 struct got_histedit_list_entry *folded = NULL;
4538 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4542 folded = get_folded_commits(hle);
4544 while (folded != hle) {
4545 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4546 folded = TAILQ_NEXT(folded, entry);
4549 err = append_folded_commit_msg(&new_msg, folded,
4555 folded = TAILQ_NEXT(folded, entry);
4559 err = got_object_id_str(&id_str, hle->commit_id);
4562 if (asprintf(&new_msg,
4563 "%s\n# original log message of commit %s: %s",
4564 logmsg ? logmsg : "", id_str,
4565 got_object_commit_get_logmsg(commit)) == -1) {
4566 err = got_error_from_errno("asprintf");
4572 err = got_object_id_str(&id_str, hle->commit_id);
4576 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4580 dprintf(fd, logmsg);
4583 err = get_editor(&editor);
4587 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4589 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4592 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4593 if (hle->logmsg == NULL)
4594 err = got_error_from_errno("strdup");
4597 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4598 err = got_error_from_errno2("unlink", logmsg_path);
4603 got_object_commit_close(commit);
4607 static const struct got_error *
4608 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4609 FILE *f, struct got_repository *repo)
4611 const struct got_error *err = NULL;
4612 char *line = NULL, *p, *end;
4616 const struct got_histedit_cmd *cmd;
4617 struct got_object_id *commit_id = NULL;
4618 struct got_histedit_list_entry *hle = NULL;
4621 len = getline(&line, &size, f);
4623 const struct got_error *getline_err;
4626 getline_err = got_error_from_errno("getline");
4627 err = got_ferror(f, getline_err->code);
4632 while (isspace((unsigned char)p[0]))
4634 if (p[0] == '#' || p[0] == '\0') {
4640 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4641 cmd = &got_histedit_cmds[i];
4642 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4643 isspace((unsigned char)p[strlen(cmd->name)])) {
4644 p += strlen(cmd->name);
4647 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4652 if (i == nitems(got_histedit_cmds)) {
4653 err = histedit_syntax_error(lineno);
4656 while (isspace((unsigned char)p[0]))
4658 if (cmd->code == GOT_HISTEDIT_MESG) {
4659 if (hle == NULL || hle->logmsg != NULL) {
4660 err = got_error(GOT_ERR_HISTEDIT_CMD);
4664 err = histedit_edit_logmsg(hle, repo);
4668 hle->logmsg = strdup(p);
4669 if (hle->logmsg == NULL) {
4670 err = got_error_from_errno("strdup");
4679 while (end[0] && !isspace((unsigned char)end[0]))
4683 err = got_object_resolve_id_str(&commit_id, repo, p);
4685 /* override error code */
4686 err = histedit_syntax_error(lineno);
4690 hle = malloc(sizeof(*hle));
4692 err = got_error_from_errno("malloc");
4696 hle->commit_id = commit_id;
4701 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4709 static const struct got_error *
4710 histedit_check_script(struct got_histedit_list *histedit_cmds,
4711 struct got_object_id_queue *commits, struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_object_qid *qid;
4715 struct got_histedit_list_entry *hle;
4716 static char msg[80];
4719 if (TAILQ_EMPTY(histedit_cmds))
4720 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4721 "histedit script contains no commands");
4722 if (SIMPLEQ_EMPTY(commits))
4723 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4725 SIMPLEQ_FOREACH(qid, commits, entry) {
4726 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4727 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4731 err = got_object_id_str(&id_str, qid->id);
4734 snprintf(msg, sizeof(msg),
4735 "commit %s missing from histedit script", id_str);
4737 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4741 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4742 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4743 "last commit in histedit script cannot be folded");
4748 static const struct got_error *
4749 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4750 const char *path, struct got_object_id_queue *commits,
4751 struct got_repository *repo)
4753 const struct got_error *err = NULL;
4757 err = get_editor(&editor);
4761 if (spawn_editor(editor, path) == -1) {
4762 err = got_error_from_errno("failed spawning editor");
4766 f = fopen(path, "r");
4768 err = got_error_from_errno("fopen");
4771 err = histedit_parse_list(histedit_cmds, f, repo);
4775 err = histedit_check_script(histedit_cmds, commits, repo);
4777 if (f && fclose(f) != 0 && err == NULL)
4778 err = got_error_from_errno("fclose");
4783 static const struct got_error *
4784 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4785 struct got_object_id_queue *, const char *, struct got_repository *);
4787 static const struct got_error *
4788 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4789 struct got_object_id_queue *commits, struct got_repository *repo)
4791 const struct got_error *err;
4795 err = got_opentemp_named(&path, &f, "got-histedit");
4799 err = write_cmd_list(f);
4803 err = histedit_write_commit_list(commits, f, repo);
4807 if (fclose(f) != 0) {
4808 err = got_error_from_errno("fclose");
4813 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4815 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4816 err->code != GOT_ERR_HISTEDIT_CMD)
4818 err = histedit_edit_list_retry(histedit_cmds, err,
4819 commits, path, repo);
4822 if (f && fclose(f) != 0 && err == NULL)
4823 err = got_error_from_errno("fclose");
4824 if (path && unlink(path) != 0 && err == NULL)
4825 err = got_error_from_errno2("unlink", path);
4830 static const struct got_error *
4831 histedit_save_list(struct got_histedit_list *histedit_cmds,
4832 struct got_worktree *worktree, struct got_repository *repo)
4834 const struct got_error *err = NULL;
4837 struct got_histedit_list_entry *hle;
4838 struct got_commit_object *commit = NULL;
4840 err = got_worktree_get_histedit_script_path(&path, worktree);
4844 f = fopen(path, "w");
4846 err = got_error_from_errno2("fopen", path);
4849 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4850 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4856 int n = fprintf(f, "%c %s\n",
4857 GOT_HISTEDIT_MESG, hle->logmsg);
4859 err = got_ferror(f, GOT_ERR_IO);
4865 if (f && fclose(f) != 0 && err == NULL)
4866 err = got_error_from_errno("fclose");
4869 got_object_commit_close(commit);
4874 histedit_free_list(struct got_histedit_list *histedit_cmds)
4876 struct got_histedit_list_entry *hle;
4878 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4879 TAILQ_REMOVE(histedit_cmds, hle, entry);
4884 static const struct got_error *
4885 histedit_load_list(struct got_histedit_list *histedit_cmds,
4886 const char *path, struct got_repository *repo)
4888 const struct got_error *err = NULL;
4891 f = fopen(path, "r");
4893 err = got_error_from_errno2("fopen", path);
4897 err = histedit_parse_list(histedit_cmds, f, repo);
4899 if (f && fclose(f) != 0 && err == NULL)
4900 err = got_error_from_errno("fclose");
4904 static const struct got_error *
4905 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4906 const struct got_error *edit_err, struct got_object_id_queue *commits,
4907 const char *path, struct got_repository *repo)
4909 const struct got_error *err = NULL, *prev_err = edit_err;
4912 while (resp != 'c' && resp != 'r' && resp != 'a') {
4913 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4914 "or (a)bort: ", getprogname(), prev_err->msg);
4919 histedit_free_list(histedit_cmds);
4920 err = histedit_run_editor(histedit_cmds, path, commits,
4923 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4924 err->code != GOT_ERR_HISTEDIT_CMD)
4931 } else if (resp == 'r') {
4932 histedit_free_list(histedit_cmds);
4933 err = histedit_edit_script(histedit_cmds,
4936 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4937 err->code != GOT_ERR_HISTEDIT_CMD)
4944 } else if (resp == 'a') {
4945 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4948 printf("invalid response '%c'\n", resp);
4954 static const struct got_error *
4955 histedit_complete(struct got_worktree *worktree,
4956 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4957 struct got_reference *branch, struct got_repository *repo)
4959 printf("Switching work tree to %s\n",
4960 got_ref_get_symref_target(branch));
4961 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4965 static const struct got_error *
4966 show_histedit_progress(struct got_commit_object *commit,
4967 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4969 const struct got_error *err;
4970 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4972 err = got_object_id_str(&old_id_str, hle->commit_id);
4977 err = got_object_id_str(&new_id_str, new_id);
4982 old_id_str[12] = '\0';
4984 new_id_str[12] = '\0';
4987 logmsg = strdup(hle->logmsg);
4988 if (logmsg == NULL) {
4989 err = got_error_from_errno("strdup");
4992 trim_logmsg(logmsg, 42);
4994 err = get_short_logmsg(&logmsg, 42, commit);
4999 switch (hle->cmd->code) {
5000 case GOT_HISTEDIT_PICK:
5001 case GOT_HISTEDIT_EDIT:
5002 printf("%s -> %s: %s\n", old_id_str,
5003 new_id_str ? new_id_str : "no-op change", logmsg);
5005 case GOT_HISTEDIT_DROP:
5006 case GOT_HISTEDIT_FOLD:
5007 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5020 static const struct got_error *
5021 histedit_commit(struct got_pathlist_head *merged_paths,
5022 struct got_worktree *worktree, struct got_fileindex *fileindex,
5023 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5024 struct got_repository *repo)
5026 const struct got_error *err;
5027 struct got_commit_object *commit;
5028 struct got_object_id *new_commit_id;
5030 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5031 && hle->logmsg == NULL) {
5032 err = histedit_edit_logmsg(hle, repo);
5037 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5041 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5042 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5045 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5047 err = show_histedit_progress(commit, hle, NULL);
5049 err = show_histedit_progress(commit, hle, new_commit_id);
5050 free(new_commit_id);
5053 got_object_commit_close(commit);
5057 static const struct got_error *
5058 histedit_skip_commit(struct got_histedit_list_entry *hle,
5059 struct got_worktree *worktree, struct got_repository *repo)
5061 const struct got_error *error;
5062 struct got_commit_object *commit;
5064 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5069 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5073 error = show_histedit_progress(commit, hle, NULL);
5074 got_object_commit_close(commit);
5078 static const struct got_error *
5079 cmd_histedit(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_fileindex *fileindex = NULL;
5084 struct got_repository *repo = NULL;
5086 struct got_reference *branch = NULL;
5087 struct got_reference *tmp_branch = NULL;
5088 struct got_object_id *resume_commit_id = NULL;
5089 struct got_object_id *base_commit_id = NULL;
5090 struct got_object_id *head_commit_id = NULL;
5091 struct got_commit_object *commit = NULL;
5092 int ch, rebase_in_progress = 0, did_something;
5093 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5094 const char *edit_script_path = NULL;
5095 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5096 struct got_object_id_queue commits;
5097 struct got_pathlist_head merged_paths;
5098 const struct got_object_id_queue *parent_ids;
5099 struct got_object_qid *pid;
5100 struct got_histedit_list histedit_cmds;
5101 struct got_histedit_list_entry *hle;
5103 SIMPLEQ_INIT(&commits);
5104 TAILQ_INIT(&histedit_cmds);
5105 TAILQ_INIT(&merged_paths);
5107 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5116 edit_script_path = optarg;
5128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5129 "unveil", NULL) == -1)
5132 if (abort_edit && continue_edit)
5138 * This command cannot apply unveil(2) in all cases because the
5139 * user may choose to run an editor to edit the histedit script
5140 * and to edit individual commit log messages.
5141 * unveil(2) traverses exec(2); if an editor is used we have to
5142 * apply unveil after edit script and log messages have been written.
5143 * XXX TODO: Make use of unveil(2) where possible.
5146 cwd = getcwd(NULL, 0);
5148 error = got_error_from_errno("getcwd");
5151 error = got_worktree_open(&worktree, cwd);
5155 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5159 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5162 if (rebase_in_progress) {
5163 error = got_error(GOT_ERR_REBASING);
5167 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5171 if (edit_in_progress && abort_edit) {
5172 error = got_worktree_histedit_continue(&resume_commit_id,
5173 &tmp_branch, &branch, &base_commit_id, &fileindex,
5177 printf("Switching work tree to %s\n",
5178 got_ref_get_symref_target(branch));
5179 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5180 branch, base_commit_id, update_progress, &did_something);
5183 printf("Histedit of %s aborted\n",
5184 got_ref_get_symref_target(branch));
5185 goto done; /* nothing else to do */
5186 } else if (abort_edit) {
5187 error = got_error(GOT_ERR_NOT_HISTEDIT);
5191 if (continue_edit) {
5194 if (!edit_in_progress) {
5195 error = got_error(GOT_ERR_NOT_HISTEDIT);
5199 error = got_worktree_get_histedit_script_path(&path, worktree);
5203 error = histedit_load_list(&histedit_cmds, path, repo);
5208 error = got_worktree_histedit_continue(&resume_commit_id,
5209 &tmp_branch, &branch, &base_commit_id, &fileindex,
5214 error = got_ref_resolve(&head_commit_id, repo, branch);
5218 error = got_object_open_as_commit(&commit, repo,
5222 parent_ids = got_object_commit_get_parent_ids(commit);
5223 pid = SIMPLEQ_FIRST(parent_ids);
5225 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5228 error = collect_commits(&commits, head_commit_id, pid->id,
5229 base_commit_id, got_worktree_get_path_prefix(worktree),
5230 GOT_ERR_HISTEDIT_PATH, repo);
5231 got_object_commit_close(commit);
5236 if (edit_in_progress) {
5237 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5241 error = got_ref_open(&branch, repo,
5242 got_worktree_get_head_ref_name(worktree), 0);
5246 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5247 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5248 "will not edit commit history of a branch outside "
5249 "the \"refs/heads/\" reference namespace");
5253 error = got_ref_resolve(&head_commit_id, repo, branch);
5254 got_ref_close(branch);
5259 error = got_object_open_as_commit(&commit, repo,
5263 parent_ids = got_object_commit_get_parent_ids(commit);
5264 pid = SIMPLEQ_FIRST(parent_ids);
5266 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5269 error = collect_commits(&commits, head_commit_id, pid->id,
5270 got_worktree_get_base_commit_id(worktree),
5271 got_worktree_get_path_prefix(worktree),
5272 GOT_ERR_HISTEDIT_PATH, repo);
5273 got_object_commit_close(commit);
5278 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5279 &base_commit_id, &fileindex, worktree, repo);
5283 if (edit_script_path) {
5284 error = histedit_load_list(&histedit_cmds,
5285 edit_script_path, repo);
5287 got_worktree_histedit_abort(worktree, fileindex,
5288 repo, branch, base_commit_id,
5289 update_progress, &did_something);
5293 error = histedit_edit_script(&histedit_cmds, &commits,
5296 got_worktree_histedit_abort(worktree, fileindex,
5297 repo, branch, base_commit_id,
5298 update_progress, &did_something);
5304 error = histedit_save_list(&histedit_cmds, worktree,
5307 got_worktree_histedit_abort(worktree, fileindex,
5308 repo, branch, base_commit_id,
5309 update_progress, &did_something);
5315 error = histedit_check_script(&histedit_cmds, &commits, repo);
5319 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5320 if (resume_commit_id) {
5321 if (got_object_id_cmp(hle->commit_id,
5322 resume_commit_id) != 0)
5325 resume_commit_id = NULL;
5326 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5327 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5328 error = histedit_skip_commit(hle, worktree,
5331 error = histedit_commit(NULL, worktree,
5332 fileindex, tmp_branch, hle, repo);
5339 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5340 error = histedit_skip_commit(hle, worktree, repo);
5346 error = got_object_open_as_commit(&commit, repo,
5350 parent_ids = got_object_commit_get_parent_ids(commit);
5351 pid = SIMPLEQ_FIRST(parent_ids);
5353 error = got_worktree_histedit_merge_files(&merged_paths,
5354 worktree, fileindex, pid->id, hle->commit_id, repo,
5355 rebase_progress, &rebase_status, check_cancelled, NULL);
5358 got_object_commit_close(commit);
5361 if (rebase_status == GOT_STATUS_CONFLICT) {
5362 got_worktree_rebase_pathlist_free(&merged_paths);
5366 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5368 error = got_object_id_str(&id_str, hle->commit_id);
5371 printf("Stopping histedit for amending commit %s\n",
5374 got_worktree_rebase_pathlist_free(&merged_paths);
5375 error = got_worktree_histedit_postpone(worktree,
5380 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5381 error = histedit_skip_commit(hle, worktree, repo);
5387 error = histedit_commit(&merged_paths, worktree, fileindex,
5388 tmp_branch, hle, repo);
5389 got_worktree_rebase_pathlist_free(&merged_paths);
5394 if (rebase_status == GOT_STATUS_CONFLICT) {
5395 error = got_worktree_histedit_postpone(worktree, fileindex);
5398 error = got_error_msg(GOT_ERR_CONFLICTS,
5399 "conflicts must be resolved before rebasing can continue");
5401 error = histedit_complete(worktree, fileindex, tmp_branch,
5404 got_object_id_queue_free(&commits);
5405 histedit_free_list(&histedit_cmds);
5406 free(head_commit_id);
5407 free(base_commit_id);
5408 free(resume_commit_id);
5410 got_object_commit_close(commit);
5412 got_ref_close(branch);
5414 got_ref_close(tmp_branch);
5416 got_worktree_close(worktree);
5418 got_repo_close(repo);
5425 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5426 "[file-path ...]\n",
5431 static const struct got_error *
5432 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5433 const char *path, struct got_object_id *blob_id,
5434 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5436 const struct got_error *err = NULL;
5437 char *id_str = NULL;
5439 if (staged_status != GOT_STATUS_ADD &&
5440 staged_status != GOT_STATUS_MODIFY &&
5441 staged_status != GOT_STATUS_DELETE)
5444 if (staged_status == GOT_STATUS_ADD ||
5445 staged_status == GOT_STATUS_MODIFY)
5446 err = got_object_id_str(&id_str, staged_blob_id);
5448 err = got_object_id_str(&id_str, blob_id);
5452 printf("%s %c %s\n", id_str, staged_status, path);
5457 static const struct got_error *
5458 cmd_stage(int argc, char *argv[])
5460 const struct got_error *error = NULL;
5461 struct got_repository *repo = NULL;
5462 struct got_worktree *worktree = NULL;
5464 struct got_pathlist_head paths;
5465 struct got_pathlist_entry *pe;
5466 int ch, list_stage = 0, pflag = 0;
5467 FILE *patch_script_file = NULL;
5468 const char *patch_script_path = NULL;
5469 struct choose_patch_arg cpa;
5473 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5482 patch_script_path = optarg;
5494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5495 "unveil", NULL) == -1)
5498 if (list_stage && (pflag || patch_script_path))
5499 errx(1, "-l option cannot be used with other options");
5500 if (patch_script_path && !pflag)
5501 errx(1, "-F option can only be used together with -p option");
5503 cwd = getcwd(NULL, 0);
5505 error = got_error_from_errno("getcwd");
5509 error = got_worktree_open(&worktree, cwd);
5513 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5517 if (patch_script_path) {
5518 patch_script_file = fopen(patch_script_path, "r");
5519 if (patch_script_file == NULL) {
5520 error = got_error_from_errno2("fopen",
5525 error = apply_unveil(got_repo_get_path(repo), 1,
5526 got_worktree_get_root_path(worktree));
5530 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5535 error = got_worktree_status(worktree, &paths, repo,
5536 print_stage, NULL, check_cancelled, NULL);
5538 cpa.patch_script_file = patch_script_file;
5539 cpa.action = "stage";
5540 error = got_worktree_stage(worktree, &paths,
5541 pflag ? NULL : print_status, NULL,
5542 pflag ? choose_patch : NULL, &cpa, repo);
5545 if (patch_script_file && fclose(patch_script_file) == EOF &&
5547 error = got_error_from_errno2("fclose", patch_script_path);
5549 got_repo_close(repo);
5551 got_worktree_close(worktree);
5552 TAILQ_FOREACH(pe, &paths, entry)
5553 free((char *)pe->path);
5554 got_pathlist_free(&paths);
5562 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5563 "[file-path ...]\n",
5569 static const struct got_error *
5570 cmd_unstage(int argc, char *argv[])
5572 const struct got_error *error = NULL;
5573 struct got_repository *repo = NULL;
5574 struct got_worktree *worktree = NULL;
5576 struct got_pathlist_head paths;
5577 struct got_pathlist_entry *pe;
5578 int ch, did_something = 0, pflag = 0;
5579 FILE *patch_script_file = NULL;
5580 const char *patch_script_path = NULL;
5581 struct choose_patch_arg cpa;
5585 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5591 patch_script_path = optarg;
5603 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5604 "unveil", NULL) == -1)
5607 if (patch_script_path && !pflag)
5608 errx(1, "-F option can only be used together with -p option");
5610 cwd = getcwd(NULL, 0);
5612 error = got_error_from_errno("getcwd");
5616 error = got_worktree_open(&worktree, cwd);
5620 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5624 if (patch_script_path) {
5625 patch_script_file = fopen(patch_script_path, "r");
5626 if (patch_script_file == NULL) {
5627 error = got_error_from_errno2("fopen",
5633 error = apply_unveil(got_repo_get_path(repo), 1,
5634 got_worktree_get_root_path(worktree));
5638 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5642 cpa.patch_script_file = patch_script_file;
5643 cpa.action = "unstage";
5644 error = got_worktree_unstage(worktree, &paths, update_progress,
5645 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5647 if (patch_script_file && fclose(patch_script_file) == EOF &&
5649 error = got_error_from_errno2("fclose", patch_script_path);
5651 got_repo_close(repo);
5653 got_worktree_close(worktree);
5654 TAILQ_FOREACH(pe, &paths, entry)
5655 free((char *)pe->path);
5656 got_pathlist_free(&paths);