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 struct got_tag_object *tag = NULL;
1392 name = got_ref_get_name(re->ref);
1393 if (strcmp(name, GOT_REF_HEAD) == 0)
1395 if (strncmp(name, "refs/", 5) == 0)
1397 if (strncmp(name, "got/", 4) == 0)
1399 if (strncmp(name, "heads/", 6) == 0)
1401 if (strncmp(name, "remotes/", 8) == 0)
1403 if (strncmp(name, "tags/", 5) == 0) {
1404 err = got_object_open_as_tag(&tag, repo, re->id);
1406 if (err->code != GOT_ERR_OBJ_TYPE)
1408 /* Ref points at something other than a tag. */
1413 cmp = got_object_id_cmp(tag ?
1414 got_object_tag_get_object_id(tag) : re->id, id);
1416 got_object_tag_close(tag);
1420 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1422 err = got_error_from_errno("asprintf");
1428 err = got_object_id_str(&id_str, id);
1432 printf(GOT_COMMIT_SEP_STR);
1433 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1434 refs_str ? refs_str : "", refs_str ? ")" : "");
1439 printf("from: %s\n", got_object_commit_get_author(commit));
1440 committer_time = got_object_commit_get_committer_time(commit);
1441 datestr = get_datestr(&committer_time, datebuf);
1442 printf("date: %s UTC\n", datestr);
1443 author = got_object_commit_get_author(commit);
1444 committer = got_object_commit_get_committer(commit);
1445 if (strcmp(author, committer) != 0)
1446 printf("via: %s\n", committer);
1447 if (got_object_commit_get_nparents(commit) > 1) {
1448 const struct got_object_id_queue *parent_ids;
1449 struct got_object_qid *qid;
1451 parent_ids = got_object_commit_get_parent_ids(commit);
1452 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1453 err = got_object_id_str(&id_str, qid->id);
1456 printf("parent %d: %s\n", n++, id_str);
1461 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1462 if (logmsg0 == NULL)
1463 return got_error_from_errno("strdup");
1467 line = strsep(&logmsg, "\n");
1469 printf(" %s\n", line);
1474 err = print_patch(commit, id, diff_context, repo);
1479 if (fflush(stdout) != 0 && err == NULL)
1480 err = got_error_from_errno("fflush");
1484 static const struct got_error *
1485 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1486 char *path, int show_patch, int diff_context, int limit,
1487 int first_parent_traversal, struct got_reflist_head *refs)
1489 const struct got_error *err;
1490 struct got_commit_graph *graph;
1492 err = got_commit_graph_open(&graph, root_id, path,
1493 first_parent_traversal, repo);
1496 err = got_commit_graph_iter_start(graph, root_id, repo);
1500 struct got_commit_object *commit;
1501 struct got_object_id *id;
1503 if (sigint_received || sigpipe_received)
1506 err = got_commit_graph_iter_next(&id, graph);
1508 if (err->code == GOT_ERR_ITER_COMPLETED) {
1512 if (err->code != GOT_ERR_ITER_NEED_MORE)
1514 err = got_commit_graph_fetch_commits(graph, 1, repo);
1523 err = got_object_open_as_commit(&commit, repo, id);
1526 err = print_commit(commit, id, repo, show_patch, diff_context,
1528 got_object_commit_close(commit);
1529 if (err || (limit && --limit == 0))
1533 got_commit_graph_close(graph);
1540 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1541 "[-r repository-path] [path]\n", getprogname());
1545 static const struct got_error *
1546 cmd_log(int argc, char *argv[])
1548 const struct got_error *error;
1549 struct got_repository *repo = NULL;
1550 struct got_worktree *worktree = NULL;
1551 struct got_commit_object *commit = NULL;
1552 struct got_object_id *id = NULL;
1553 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1554 char *start_commit = NULL;
1555 int diff_context = 3, ch;
1556 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1558 struct got_reflist_head refs;
1560 SIMPLEQ_INIT(&refs);
1563 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1569 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1575 start_commit = optarg;
1578 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1581 err(1, "-C option %s", errstr);
1584 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1586 err(1, "-l option %s", errstr);
1589 first_parent_traversal = 1;
1592 repo_path = realpath(optarg, NULL);
1593 if (repo_path == NULL)
1594 err(1, "-r option");
1595 got_path_strip_trailing_slashes(repo_path);
1606 cwd = getcwd(NULL, 0);
1608 error = got_error_from_errno("getcwd");
1612 error = got_worktree_open(&worktree, cwd);
1613 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1620 error = got_error_from_errno("strdup");
1623 } else if (argc == 1) {
1625 error = got_worktree_resolve_path(&path, worktree,
1630 path = strdup(argv[0]);
1632 error = got_error_from_errno("strdup");
1639 if (repo_path == NULL) {
1640 repo_path = worktree ?
1641 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1643 if (repo_path == NULL) {
1644 error = got_error_from_errno("strdup");
1648 error = got_repo_open(&repo, repo_path);
1652 error = apply_unveil(got_repo_get_path(repo), 1,
1653 worktree ? got_worktree_get_root_path(worktree) : NULL);
1657 if (start_commit == NULL) {
1658 struct got_reference *head_ref;
1659 error = got_ref_open(&head_ref, repo,
1660 worktree ? got_worktree_get_head_ref_name(worktree)
1664 error = got_ref_resolve(&id, repo, head_ref);
1665 got_ref_close(head_ref);
1668 error = got_object_open_as_commit(&commit, repo, id);
1670 struct got_reference *ref;
1671 error = got_ref_open(&ref, repo, start_commit, 0);
1672 if (error == NULL) {
1674 error = got_ref_resolve(&id, repo, ref);
1678 error = got_object_get_type(&obj_type, repo, id);
1681 if (obj_type == GOT_OBJ_TYPE_TAG) {
1682 struct got_tag_object *tag;
1683 error = got_object_open_as_tag(&tag, repo, id);
1686 if (got_object_tag_get_object_type(tag) !=
1687 GOT_OBJ_TYPE_COMMIT) {
1688 got_object_tag_close(tag);
1689 error = got_error(GOT_ERR_OBJ_TYPE);
1693 id = got_object_id_dup(
1694 got_object_tag_get_object_id(tag));
1696 error = got_error_from_errno(
1697 "got_object_id_dup");
1698 got_object_tag_close(tag);
1701 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1702 error = got_error(GOT_ERR_OBJ_TYPE);
1705 error = got_object_open_as_commit(&commit, repo, id);
1709 if (commit == NULL) {
1710 error = got_repo_match_object_id_prefix(&id,
1711 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1719 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1724 path = in_repo_path;
1727 error = got_ref_list(&refs, repo);
1731 error = print_commits(id, repo, path, show_patch,
1732 diff_context, limit, first_parent_traversal, &refs);
1739 got_worktree_close(worktree);
1741 const struct got_error *repo_error;
1742 repo_error = got_repo_close(repo);
1746 got_ref_list_free(&refs);
1753 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1754 "[object1 object2 | path]\n", getprogname());
1758 struct print_diff_arg {
1759 struct got_repository *repo;
1760 struct got_worktree *worktree;
1767 static const struct got_error *
1768 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1769 const char *path, struct got_object_id *blob_id,
1770 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1772 struct print_diff_arg *a = arg;
1773 const struct got_error *err = NULL;
1774 struct got_blob_object *blob1 = NULL;
1776 char *abspath = NULL, *label1 = NULL;
1779 if (a->diff_staged) {
1780 if (staged_status != GOT_STATUS_MODIFY &&
1781 staged_status != GOT_STATUS_ADD &&
1782 staged_status != GOT_STATUS_DELETE)
1785 if (staged_status == GOT_STATUS_DELETE)
1787 if (status != GOT_STATUS_MODIFY &&
1788 status != GOT_STATUS_ADD &&
1789 status != GOT_STATUS_DELETE &&
1790 status != GOT_STATUS_CONFLICT)
1794 if (!a->header_shown) {
1795 printf("diff %s %s%s\n", a->id_str,
1796 got_worktree_get_root_path(a->worktree),
1797 a->diff_staged ? " (staged changes)" : "");
1798 a->header_shown = 1;
1801 if (a->diff_staged) {
1802 const char *label1 = NULL, *label2 = NULL;
1803 switch (staged_status) {
1804 case GOT_STATUS_MODIFY:
1808 case GOT_STATUS_ADD:
1811 case GOT_STATUS_DELETE:
1815 return got_error(GOT_ERR_FILE_STATUS);
1817 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1818 label1, label2, a->diff_context, a->repo, stdout);
1821 if (staged_status == GOT_STATUS_ADD ||
1822 staged_status == GOT_STATUS_MODIFY) {
1824 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1828 err = got_object_id_str(&id_str, staged_blob_id);
1831 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1832 err = got_error_from_errno("asprintf");
1837 } else if (status != GOT_STATUS_ADD) {
1838 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1843 if (status != GOT_STATUS_DELETE) {
1844 if (asprintf(&abspath, "%s/%s",
1845 got_worktree_get_root_path(a->worktree), path) == -1) {
1846 err = got_error_from_errno("asprintf");
1850 f2 = fopen(abspath, "r");
1852 err = got_error_from_errno2("fopen", abspath);
1855 if (lstat(abspath, &sb) == -1) {
1856 err = got_error_from_errno2("lstat", abspath);
1862 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1863 a->diff_context, stdout);
1866 got_object_blob_close(blob1);
1867 if (f2 && fclose(f2) != 0 && err == NULL)
1868 err = got_error_from_errno("fclose");
1873 static const struct got_error *
1874 match_object_id(struct got_object_id **id, char **label,
1875 const char *id_str, int obj_type, struct got_repository *repo)
1877 const struct got_error *err;
1878 struct got_tag_object *tag;
1879 struct got_reference *ref = NULL;
1884 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1886 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1888 err = got_error_from_errno("got_object_id_dup");
1889 if (asprintf(label, "refs/tags/%s",
1890 got_object_tag_get_name(tag)) == -1)
1891 err = got_error_from_errno("asprintf");
1892 got_object_tag_close(tag);
1894 } else if (err->code != GOT_ERR_NO_OBJ)
1897 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1899 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1901 err = got_ref_open(&ref, repo, id_str, 0);
1904 *label = strdup(got_ref_get_name(ref));
1905 if (*label == NULL) {
1906 err = got_error_from_errno("strdup");
1909 err = got_ref_resolve(id, repo, ref);
1911 err = got_object_id_str(label, *id);
1912 if (*label == NULL) {
1913 err = got_error_from_errno("strdup");
1924 static const struct got_error *
1925 cmd_diff(int argc, char *argv[])
1927 const struct got_error *error;
1928 struct got_repository *repo = NULL;
1929 struct got_worktree *worktree = NULL;
1930 char *cwd = NULL, *repo_path = NULL;
1931 struct got_object_id *id1 = NULL, *id2 = NULL;
1932 const char *id_str1 = NULL, *id_str2 = NULL;
1933 char *label1 = NULL, *label2 = NULL;
1935 int diff_context = 3, diff_staged = 0, ch;
1940 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1945 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1948 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1950 err(1, "-C option %s", errstr);
1953 repo_path = realpath(optarg, NULL);
1954 if (repo_path == NULL)
1955 err(1, "-r option");
1956 got_path_strip_trailing_slashes(repo_path);
1970 cwd = getcwd(NULL, 0);
1972 error = got_error_from_errno("getcwd");
1975 error = got_worktree_open(&worktree, cwd);
1976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1979 if (worktree == NULL) {
1980 error = got_error(GOT_ERR_NOT_WORKTREE);
1985 "-r option can't be used when diffing a work tree");
1986 repo_path = strdup(got_worktree_get_repo_path(worktree));
1987 if (repo_path == NULL) {
1988 error = got_error_from_errno("strdup");
1992 error = got_worktree_resolve_path(&path, worktree,
1999 error = got_error_from_errno("strdup");
2003 } else if (argc == 2) {
2005 errx(1, "-s option can't be used when diffing "
2006 "objects in repository");
2009 if (worktree && repo_path == NULL) {
2011 strdup(got_worktree_get_repo_path(worktree));
2012 if (repo_path == NULL) {
2013 error = got_error_from_errno("strdup");
2020 if (repo_path == NULL) {
2021 repo_path = getcwd(NULL, 0);
2022 if (repo_path == NULL)
2023 return got_error_from_errno("getcwd");
2026 error = got_repo_open(&repo, repo_path);
2031 error = apply_unveil(got_repo_get_path(repo), 1,
2032 worktree ? got_worktree_get_root_path(worktree) : NULL);
2037 struct print_diff_arg arg;
2038 struct got_pathlist_head paths;
2043 error = got_object_id_str(&id_str,
2044 got_worktree_get_base_commit_id(worktree));
2048 arg.worktree = worktree;
2049 arg.diff_context = diff_context;
2050 arg.id_str = id_str;
2051 arg.header_shown = 0;
2052 arg.diff_staged = diff_staged;
2054 error = got_pathlist_append(&paths, path, NULL);
2058 error = got_worktree_status(worktree, &paths, repo, print_diff,
2059 &arg, check_cancelled, NULL);
2061 got_pathlist_free(&paths);
2065 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2069 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2073 error = got_object_get_type(&type1, repo, id1);
2077 error = got_object_get_type(&type2, repo, id2);
2081 if (type1 != type2) {
2082 error = got_error(GOT_ERR_OBJ_TYPE);
2087 case GOT_OBJ_TYPE_BLOB:
2088 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2089 diff_context, repo, stdout);
2091 case GOT_OBJ_TYPE_TREE:
2092 error = got_diff_objects_as_trees(id1, id2, "", "",
2093 diff_context, repo, stdout);
2095 case GOT_OBJ_TYPE_COMMIT:
2096 printf("diff %s %s\n", label1, label2);
2097 error = got_diff_objects_as_commits(id1, id2, diff_context,
2101 error = got_error(GOT_ERR_OBJ_TYPE);
2111 got_worktree_close(worktree);
2113 const struct got_error *repo_error;
2114 repo_error = got_repo_close(repo);
2125 "usage: %s blame [-c commit] [-r repository-path] path\n",
2130 static const struct got_error *
2131 cmd_blame(int argc, char *argv[])
2133 const struct got_error *error;
2134 struct got_repository *repo = NULL;
2135 struct got_worktree *worktree = NULL;
2136 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2137 struct got_object_id *commit_id = NULL;
2138 char *commit_id_str = NULL;
2142 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2147 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2150 commit_id_str = optarg;
2153 repo_path = realpath(optarg, NULL);
2154 if (repo_path == NULL)
2155 err(1, "-r option");
2156 got_path_strip_trailing_slashes(repo_path);
2172 cwd = getcwd(NULL, 0);
2174 error = got_error_from_errno("getcwd");
2177 if (repo_path == NULL) {
2178 error = got_worktree_open(&worktree, cwd);
2179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2185 strdup(got_worktree_get_repo_path(worktree));
2186 if (repo_path == NULL)
2187 error = got_error_from_errno("strdup");
2191 repo_path = strdup(cwd);
2192 if (repo_path == NULL) {
2193 error = got_error_from_errno("strdup");
2199 error = got_repo_open(&repo, repo_path);
2203 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2208 const char *prefix = got_worktree_get_path_prefix(worktree);
2209 char *p, *worktree_subdir = cwd +
2210 strlen(got_worktree_get_root_path(worktree));
2211 if (asprintf(&p, "%s%s%s%s%s",
2212 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2213 worktree_subdir, worktree_subdir[0] ? "/" : "",
2215 error = got_error_from_errno("asprintf");
2218 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2221 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2226 if (commit_id_str == NULL) {
2227 struct got_reference *head_ref;
2228 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2231 error = got_ref_resolve(&commit_id, repo, head_ref);
2232 got_ref_close(head_ref);
2236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2241 error = got_blame(in_repo_path, commit_id, repo, stdout);
2248 got_worktree_close(worktree);
2250 const struct got_error *repo_error;
2251 repo_error = got_repo_close(repo);
2262 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2268 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2269 const char *root_path)
2271 int is_root_path = (strcmp(path, root_path) == 0);
2272 const char *modestr = "";
2274 path += strlen(root_path);
2275 while (path[0] == '/')
2278 if (S_ISLNK(te->mode))
2280 else if (S_ISDIR(te->mode))
2282 else if (te->mode & S_IXUSR)
2285 printf("%s%s%s%s%s\n", id ? id : "", path,
2286 is_root_path ? "" : "/", te->name, modestr);
2289 static const struct got_error *
2290 print_tree(const char *path, struct got_object_id *commit_id,
2291 int show_ids, int recurse, const char *root_path,
2292 struct got_repository *repo)
2294 const struct got_error *err = NULL;
2295 struct got_object_id *tree_id = NULL;
2296 struct got_tree_object *tree = NULL;
2297 const struct got_tree_entries *entries;
2298 struct got_tree_entry *te;
2300 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2304 err = got_object_open_as_tree(&tree, repo, tree_id);
2307 entries = got_object_tree_get_entries(tree);
2308 te = SIMPLEQ_FIRST(&entries->head);
2312 if (sigint_received || sigpipe_received)
2317 err = got_object_id_str(&id_str, te->id);
2320 if (asprintf(&id, "%s ", id_str) == -1) {
2321 err = got_error_from_errno("asprintf");
2327 print_entry(te, id, path, root_path);
2330 if (recurse && S_ISDIR(te->mode)) {
2332 if (asprintf(&child_path, "%s%s%s", path,
2333 path[0] == '/' && path[1] == '\0' ? "" : "/",
2335 err = got_error_from_errno("asprintf");
2338 err = print_tree(child_path, commit_id, show_ids, 1,
2345 te = SIMPLEQ_NEXT(te, entry);
2349 got_object_tree_close(tree);
2354 static const struct got_error *
2355 cmd_tree(int argc, char *argv[])
2357 const struct got_error *error;
2358 struct got_repository *repo = NULL;
2359 struct got_worktree *worktree = NULL;
2361 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2362 struct got_object_id *commit_id = NULL;
2363 char *commit_id_str = NULL;
2364 int show_ids = 0, recurse = 0;
2368 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2373 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2376 commit_id_str = optarg;
2379 repo_path = realpath(optarg, NULL);
2380 if (repo_path == NULL)
2381 err(1, "-r option");
2382 got_path_strip_trailing_slashes(repo_path);
2406 cwd = getcwd(NULL, 0);
2408 error = got_error_from_errno("getcwd");
2411 if (repo_path == NULL) {
2412 error = got_worktree_open(&worktree, cwd);
2413 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2419 strdup(got_worktree_get_repo_path(worktree));
2420 if (repo_path == NULL)
2421 error = got_error_from_errno("strdup");
2425 repo_path = strdup(cwd);
2426 if (repo_path == NULL) {
2427 error = got_error_from_errno("strdup");
2433 error = got_repo_open(&repo, repo_path);
2437 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2443 char *p, *worktree_subdir = cwd +
2444 strlen(got_worktree_get_root_path(worktree));
2445 if (asprintf(&p, "%s/%s",
2446 got_worktree_get_path_prefix(worktree),
2447 worktree_subdir) == -1) {
2448 error = got_error_from_errno("asprintf");
2451 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2458 if (in_repo_path == NULL) {
2459 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2464 if (commit_id_str == NULL) {
2465 struct got_reference *head_ref;
2466 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2469 error = got_ref_resolve(&commit_id, repo, head_ref);
2470 got_ref_close(head_ref);
2474 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2479 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2480 in_repo_path, repo);
2487 got_worktree_close(worktree);
2489 const struct got_error *repo_error;
2490 repo_error = got_repo_close(repo);
2500 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2504 static const struct got_error *
2505 print_status(void *arg, unsigned char status, unsigned char staged_status,
2506 const char *path, struct got_object_id *blob_id,
2507 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2509 if (status == staged_status && (status == GOT_STATUS_DELETE))
2510 status = GOT_STATUS_NO_CHANGE;
2511 printf("%c%c %s\n", status, staged_status, path);
2515 static const struct got_error *
2516 cmd_status(int argc, char *argv[])
2518 const struct got_error *error = NULL;
2519 struct got_repository *repo = NULL;
2520 struct got_worktree *worktree = NULL;
2522 struct got_pathlist_head paths;
2523 struct got_pathlist_entry *pe;
2528 while ((ch = getopt(argc, argv, "")) != -1) {
2540 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2544 cwd = getcwd(NULL, 0);
2546 error = got_error_from_errno("getcwd");
2550 error = got_worktree_open(&worktree, cwd);
2554 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2558 error = apply_unveil(got_repo_get_path(repo), 1,
2559 got_worktree_get_root_path(worktree));
2563 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2567 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2568 check_cancelled, NULL);
2570 TAILQ_FOREACH(pe, &paths, entry)
2571 free((char *)pe->path);
2572 got_pathlist_free(&paths);
2581 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2586 static const struct got_error *
2587 list_refs(struct got_repository *repo)
2589 static const struct got_error *err = NULL;
2590 struct got_reflist_head refs;
2591 struct got_reflist_entry *re;
2593 SIMPLEQ_INIT(&refs);
2594 err = got_ref_list(&refs, repo);
2598 SIMPLEQ_FOREACH(re, &refs, entry) {
2600 refstr = got_ref_to_str(re->ref);
2602 return got_error_from_errno("got_ref_to_str");
2603 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2607 got_ref_list_free(&refs);
2611 static const struct got_error *
2612 delete_ref(struct got_repository *repo, const char *refname)
2614 const struct got_error *err = NULL;
2615 struct got_reference *ref;
2617 err = got_ref_open(&ref, repo, refname, 0);
2621 err = got_ref_delete(ref, repo);
2626 static const struct got_error *
2627 add_ref(struct got_repository *repo, const char *refname, const char *target)
2629 const struct got_error *err = NULL;
2630 struct got_object_id *id;
2631 struct got_reference *ref = NULL;
2634 * Don't let the user create a reference named '-'.
2635 * While technically a valid reference name, this case is usually
2636 * an unintended typo.
2638 if (refname[0] == '-' && refname[1] == '\0')
2639 return got_error(GOT_ERR_BAD_REF_NAME);
2641 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2644 struct got_reference *target_ref;
2646 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2648 err = got_ref_open(&target_ref, repo, target, 0);
2651 err = got_ref_resolve(&id, repo, target_ref);
2652 got_ref_close(target_ref);
2657 err = got_ref_alloc(&ref, refname, id);
2661 err = got_ref_write(ref, repo);
2669 static const struct got_error *
2670 add_symref(struct got_repository *repo, const char *refname, const char *target)
2672 const struct got_error *err = NULL;
2673 struct got_reference *ref = NULL;
2674 struct got_reference *target_ref = NULL;
2677 * Don't let the user create a reference named '-'.
2678 * While technically a valid reference name, this case is usually
2679 * an unintended typo.
2681 if (refname[0] == '-' && refname[1] == '\0')
2682 return got_error(GOT_ERR_BAD_REF_NAME);
2684 err = got_ref_open(&target_ref, repo, target, 0);
2688 err = got_ref_alloc_symref(&ref, refname, target_ref);
2692 err = got_ref_write(ref, repo);
2695 got_ref_close(target_ref);
2701 static const struct got_error *
2702 cmd_ref(int argc, char *argv[])
2704 const struct got_error *error = NULL;
2705 struct got_repository *repo = NULL;
2706 struct got_worktree *worktree = NULL;
2707 char *cwd = NULL, *repo_path = NULL;
2708 int ch, do_list = 0, create_symref = 0;
2709 const char *delref = NULL;
2711 /* TODO: Add -s option for adding symbolic references. */
2712 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2718 repo_path = realpath(optarg, NULL);
2719 if (repo_path == NULL)
2720 err(1, "-r option");
2721 got_path_strip_trailing_slashes(repo_path);
2735 if (do_list && delref)
2736 errx(1, "-l and -d options are mutually exclusive\n");
2741 if (do_list || delref) {
2743 errx(1, "-s option cannot be used together with the "
2744 "-l or -d options");
2747 } else if (argc != 2)
2752 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2756 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2757 "sendfd unveil", NULL) == -1)
2761 cwd = getcwd(NULL, 0);
2763 error = got_error_from_errno("getcwd");
2767 if (repo_path == NULL) {
2768 error = got_worktree_open(&worktree, cwd);
2769 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2775 strdup(got_worktree_get_repo_path(worktree));
2776 if (repo_path == NULL)
2777 error = got_error_from_errno("strdup");
2781 repo_path = strdup(cwd);
2782 if (repo_path == NULL) {
2783 error = got_error_from_errno("strdup");
2789 error = got_repo_open(&repo, repo_path);
2793 error = apply_unveil(got_repo_get_path(repo), do_list,
2794 worktree ? got_worktree_get_root_path(worktree) : NULL);
2799 error = list_refs(repo);
2801 error = delete_ref(repo, delref);
2802 else if (create_symref)
2803 error = add_symref(repo, argv[0], argv[1]);
2805 error = add_ref(repo, argv[0], argv[1]);
2808 got_repo_close(repo);
2810 got_worktree_close(worktree);
2820 "usage: %s branch [-r repository] -l | -d name | "
2821 "name [base-branch]\n", getprogname());
2825 static const struct got_error *
2826 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2828 static const struct got_error *err = NULL;
2829 struct got_reflist_head refs;
2830 struct got_reflist_entry *re;
2832 SIMPLEQ_INIT(&refs);
2834 err = got_ref_list(&refs, repo);
2838 SIMPLEQ_FOREACH(re, &refs, entry) {
2839 const char *refname, *marker = " ";
2841 refname = got_ref_get_name(re->ref);
2842 if (strncmp(refname, "refs/heads/", 11) != 0)
2844 if (worktree && strcmp(refname,
2845 got_worktree_get_head_ref_name(worktree)) == 0) {
2846 struct got_object_id *id = NULL;
2847 err = got_ref_resolve(&id, repo, re->ref);
2850 if (got_object_id_cmp(id,
2851 got_worktree_get_base_commit_id(worktree)) == 0)
2858 refstr = got_ref_to_str(re->ref);
2860 return got_error_from_errno("got_ref_to_str");
2861 printf("%s%s: %s\n", marker, refname, refstr);
2865 got_ref_list_free(&refs);
2869 static const struct got_error *
2870 delete_branch(struct got_repository *repo, const char *branch_name)
2872 const struct got_error *err = NULL;
2873 struct got_reference *ref;
2876 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2877 return got_error_from_errno("asprintf");
2879 err = got_ref_open(&ref, repo, refname, 0);
2883 err = got_ref_delete(ref, repo);
2890 static const struct got_error *
2891 add_branch(struct got_repository *repo, const char *branch_name,
2892 const char *base_branch)
2894 const struct got_error *err = NULL;
2895 struct got_object_id *id = NULL;
2896 struct got_reference *ref = NULL;
2897 char *base_refname = NULL, *refname = NULL;
2898 struct got_reference *base_ref;
2901 * Don't let the user create a branch named '-'.
2902 * While technically a valid reference name, this case is usually
2903 * an unintended typo.
2905 if (branch_name[0] == '-' && branch_name[1] == '\0')
2906 return got_error(GOT_ERR_BAD_REF_NAME);
2908 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2909 base_refname = strdup(GOT_REF_HEAD);
2910 if (base_refname == NULL)
2911 return got_error_from_errno("strdup");
2912 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2913 return got_error_from_errno("asprintf");
2915 err = got_ref_open(&base_ref, repo, base_refname, 0);
2918 err = got_ref_resolve(&id, repo, base_ref);
2919 got_ref_close(base_ref);
2923 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2924 err = got_error_from_errno("asprintf");
2928 err = got_ref_open(&ref, repo, refname, 0);
2930 err = got_error(GOT_ERR_BRANCH_EXISTS);
2932 } else if (err->code != GOT_ERR_NOT_REF)
2935 err = got_ref_alloc(&ref, refname, id);
2939 err = got_ref_write(ref, repo);
2949 static const struct got_error *
2950 cmd_branch(int argc, char *argv[])
2952 const struct got_error *error = NULL;
2953 struct got_repository *repo = NULL;
2954 struct got_worktree *worktree = NULL;
2955 char *cwd = NULL, *repo_path = NULL;
2956 int ch, do_list = 0;
2957 const char *delref = NULL;
2959 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2965 repo_path = realpath(optarg, NULL);
2966 if (repo_path == NULL)
2967 err(1, "-r option");
2968 got_path_strip_trailing_slashes(repo_path);
2979 if (do_list && delref)
2980 errx(1, "-l and -d options are mutually exclusive\n");
2985 if (do_list || delref) {
2988 } else if (argc < 1 || argc > 2)
2993 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2997 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2998 "sendfd unveil", NULL) == -1)
3002 cwd = getcwd(NULL, 0);
3004 error = got_error_from_errno("getcwd");
3008 if (repo_path == NULL) {
3009 error = got_worktree_open(&worktree, cwd);
3010 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3016 strdup(got_worktree_get_repo_path(worktree));
3017 if (repo_path == NULL)
3018 error = got_error_from_errno("strdup");
3022 repo_path = strdup(cwd);
3023 if (repo_path == NULL) {
3024 error = got_error_from_errno("strdup");
3030 error = got_repo_open(&repo, repo_path);
3034 error = apply_unveil(got_repo_get_path(repo), do_list,
3035 worktree ? got_worktree_get_root_path(worktree) : NULL);
3040 error = list_branches(repo, worktree);
3042 error = delete_branch(repo, delref);
3044 const char *base_branch;
3046 base_branch = worktree ?
3047 got_worktree_get_head_ref_name(worktree) :
3049 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3052 base_branch = argv[1];
3053 error = add_branch(repo, argv[0], base_branch);
3057 got_repo_close(repo);
3059 got_worktree_close(worktree);
3068 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3072 static const struct got_error *
3073 cmd_add(int argc, char *argv[])
3075 const struct got_error *error = NULL;
3076 struct got_repository *repo = NULL;
3077 struct got_worktree *worktree = NULL;
3079 struct got_pathlist_head paths;
3080 struct got_pathlist_entry *pe;
3085 while ((ch = getopt(argc, argv, "")) != -1) {
3097 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3104 cwd = getcwd(NULL, 0);
3106 error = got_error_from_errno("getcwd");
3110 error = got_worktree_open(&worktree, cwd);
3114 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3118 error = apply_unveil(got_repo_get_path(repo), 1,
3119 got_worktree_get_root_path(worktree));
3123 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3127 error = got_worktree_schedule_add(worktree, &paths, print_status,
3131 got_repo_close(repo);
3133 got_worktree_close(worktree);
3134 TAILQ_FOREACH(pe, &paths, entry)
3135 free((char *)pe->path);
3136 got_pathlist_free(&paths);
3144 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3148 static const struct got_error *
3149 cmd_remove(int argc, char *argv[])
3151 const struct got_error *error = NULL;
3152 struct got_worktree *worktree = NULL;
3153 struct got_repository *repo = NULL;
3155 struct got_pathlist_head paths;
3156 struct got_pathlist_entry *pe;
3157 int ch, delete_local_mods = 0;
3161 while ((ch = getopt(argc, argv, "f")) != -1) {
3164 delete_local_mods = 1;
3176 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3183 cwd = getcwd(NULL, 0);
3185 error = got_error_from_errno("getcwd");
3188 error = got_worktree_open(&worktree, cwd);
3192 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3196 error = apply_unveil(got_repo_get_path(repo), 1,
3197 got_worktree_get_root_path(worktree));
3201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3205 error = got_worktree_schedule_delete(worktree, &paths,
3206 delete_local_mods, print_status, NULL, repo);
3211 got_repo_close(repo);
3213 got_worktree_close(worktree);
3214 TAILQ_FOREACH(pe, &paths, entry)
3215 free((char *)pe->path);
3216 got_pathlist_free(&paths);
3224 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3225 "path ...\n", getprogname());
3229 static const struct got_error *
3230 revert_progress(void *arg, unsigned char status, const char *path)
3232 while (path[0] == '/')
3234 printf("%c %s\n", status, path);
3238 struct choose_patch_arg {
3239 FILE *patch_script_file;
3243 static const struct got_error *
3244 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3245 int nchanges, const char *action)
3248 size_t linesize = 0;
3252 case GOT_STATUS_ADD:
3253 printf("A %s\n%s this addition? [y/n] ", path, action);
3255 case GOT_STATUS_DELETE:
3256 printf("D %s\n%s this deletion? [y/n] ", path, action);
3258 case GOT_STATUS_MODIFY:
3259 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3260 return got_error_from_errno("fseek");
3261 printf(GOT_COMMIT_SEP_STR);
3262 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3264 if (ferror(patch_file))
3265 return got_error_from_errno("getline");
3266 printf(GOT_COMMIT_SEP_STR);
3267 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3268 path, n, nchanges, action);
3271 return got_error_path(path, GOT_ERR_FILE_STATUS);
3277 static const struct got_error *
3278 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3279 FILE *patch_file, int n, int nchanges)
3281 const struct got_error *err = NULL;
3283 size_t linesize = 0;
3286 struct choose_patch_arg *a = arg;
3288 *choice = GOT_PATCH_CHOICE_NONE;
3290 if (a->patch_script_file) {
3292 err = show_change(status, path, patch_file, n, nchanges,
3296 linelen = getline(&line, &linesize, a->patch_script_file);
3297 if (linelen == -1) {
3298 if (ferror(a->patch_script_file))
3299 return got_error_from_errno("getline");
3302 nl = strchr(line, '\n');
3305 if (strcmp(line, "y") == 0) {
3306 *choice = GOT_PATCH_CHOICE_YES;
3308 } else if (strcmp(line, "n") == 0) {
3309 *choice = GOT_PATCH_CHOICE_NO;
3311 } else if (strcmp(line, "q") == 0 &&
3312 status == GOT_STATUS_MODIFY) {
3313 *choice = GOT_PATCH_CHOICE_QUIT;
3316 printf("invalid response '%s'\n", line);
3321 while (resp != 'y' && resp != 'n' && resp != 'q') {
3322 err = show_change(status, path, patch_file, n, nchanges,
3329 if (status == GOT_STATUS_MODIFY) {
3330 if (resp != 'y' && resp != 'n' && resp != 'q') {
3331 printf("invalid response '%c'\n", resp);
3334 } else if (resp != 'y' && resp != 'n') {
3335 printf("invalid response '%c'\n", resp);
3341 *choice = GOT_PATCH_CHOICE_YES;
3342 else if (resp == 'n')
3343 *choice = GOT_PATCH_CHOICE_NO;
3344 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3345 *choice = GOT_PATCH_CHOICE_QUIT;
3351 static const struct got_error *
3352 cmd_revert(int argc, char *argv[])
3354 const struct got_error *error = NULL;
3355 struct got_worktree *worktree = NULL;
3356 struct got_repository *repo = NULL;
3357 char *cwd = NULL, *path = NULL;
3358 struct got_pathlist_head paths;
3359 struct got_pathlist_entry *pe;
3360 int ch, can_recurse = 0, pflag = 0;
3361 FILE *patch_script_file = NULL;
3362 const char *patch_script_path = NULL;
3363 struct choose_patch_arg cpa;
3367 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3373 patch_script_path = optarg;
3388 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3389 "unveil", NULL) == -1)
3394 if (patch_script_path && !pflag)
3395 errx(1, "-F option can only be used together with -p option");
3397 cwd = getcwd(NULL, 0);
3399 error = got_error_from_errno("getcwd");
3402 error = got_worktree_open(&worktree, cwd);
3406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3410 if (patch_script_path) {
3411 patch_script_file = fopen(patch_script_path, "r");
3412 if (patch_script_file == NULL) {
3413 error = got_error_from_errno2("fopen",
3418 error = apply_unveil(got_repo_get_path(repo), 1,
3419 got_worktree_get_root_path(worktree));
3423 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3430 TAILQ_FOREACH(pe, &paths, entry) {
3431 if (asprintf(&ondisk_path, "%s/%s",
3432 got_worktree_get_root_path(worktree),
3434 error = got_error_from_errno("asprintf");
3437 if (lstat(ondisk_path, &sb) == -1) {
3438 if (errno == ENOENT) {
3442 error = got_error_from_errno2("lstat",
3448 if (S_ISDIR(sb.st_mode)) {
3449 error = got_error_msg(GOT_ERR_BAD_PATH,
3450 "reverting directories requires -R option");
3456 cpa.patch_script_file = patch_script_file;
3457 cpa.action = "revert";
3458 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3459 pflag ? choose_patch : NULL, &cpa, repo);
3463 if (patch_script_file && fclose(patch_script_file) == EOF &&
3465 error = got_error_from_errno2("fclose", patch_script_path);
3467 got_repo_close(repo);
3469 got_worktree_close(worktree);
3478 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3483 struct collect_commit_logmsg_arg {
3484 const char *cmdline_log;
3486 const char *worktree_path;
3487 const char *branch_name;
3488 const char *repo_path;
3493 static const struct got_error *
3494 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3497 char *initial_content = NULL;
3498 struct got_pathlist_entry *pe;
3499 const struct got_error *err = NULL;
3500 char *template = NULL;
3501 struct collect_commit_logmsg_arg *a = arg;
3505 /* if a message was specified on the command line, just use it */
3506 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3507 len = strlen(a->cmdline_log) + 1;
3508 *logmsg = malloc(len + 1);
3509 if (*logmsg == NULL)
3510 return got_error_from_errno("malloc");
3511 strlcpy(*logmsg, a->cmdline_log, len);
3515 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3516 return got_error_from_errno("asprintf");
3518 if (asprintf(&initial_content,
3519 "\n# changes to be committed on branch %s:\n",
3520 a->branch_name) == -1)
3521 return got_error_from_errno("asprintf");
3523 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3527 dprintf(fd, initial_content);
3529 TAILQ_FOREACH(pe, commitable_paths, entry) {
3530 struct got_commitable *ct = pe->data;
3531 dprintf(fd, "# %c %s\n",
3532 got_commitable_get_status(ct),
3533 got_commitable_get_path(ct));
3537 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3539 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3540 unlink(a->logmsg_path);
3541 free(a->logmsg_path);
3542 a->logmsg_path = NULL;
3544 free(initial_content);
3547 /* Editor is done; we can now apply unveil(2) */
3549 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3558 static const struct got_error *
3559 cmd_commit(int argc, char *argv[])
3561 const struct got_error *error = NULL;
3562 struct got_worktree *worktree = NULL;
3563 struct got_repository *repo = NULL;
3564 char *cwd = NULL, *id_str = NULL;
3565 struct got_object_id *id = NULL;
3566 const char *logmsg = NULL;
3568 struct collect_commit_logmsg_arg cl_arg;
3569 char *editor = NULL;
3570 int ch, rebase_in_progress, histedit_in_progress;
3571 struct got_pathlist_head paths;
3574 cl_arg.logmsg_path = NULL;
3576 while ((ch = getopt(argc, argv, "m:")) != -1) {
3591 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3592 "unveil", NULL) == -1)
3595 error = get_author(&author);
3599 cwd = getcwd(NULL, 0);
3601 error = got_error_from_errno("getcwd");
3604 error = got_worktree_open(&worktree, cwd);
3608 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3611 if (rebase_in_progress) {
3612 error = got_error(GOT_ERR_REBASING);
3616 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3621 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3626 * unveil(2) traverses exec(2); if an editor is used we have
3627 * to apply unveil after the log message has been written.
3629 if (logmsg == NULL || strlen(logmsg) == 0)
3630 error = get_editor(&editor);
3632 error = apply_unveil(got_repo_get_path(repo), 0,
3633 got_worktree_get_root_path(worktree));
3637 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3641 cl_arg.editor = editor;
3642 cl_arg.cmdline_log = logmsg;
3643 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3644 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3645 if (!histedit_in_progress) {
3646 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3647 error = got_error(GOT_ERR_COMMIT_BRANCH);
3650 cl_arg.branch_name += 11;
3652 cl_arg.repo_path = got_repo_get_path(repo);
3653 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3654 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3656 if (cl_arg.logmsg_path)
3657 fprintf(stderr, "%s: log message preserved in %s\n",
3658 getprogname(), cl_arg.logmsg_path);
3662 if (cl_arg.logmsg_path)
3663 unlink(cl_arg.logmsg_path);
3665 error = got_object_id_str(&id_str, id);
3668 printf("Created commit %s\n", id_str);
3670 free(cl_arg.logmsg_path);
3672 got_repo_close(repo);
3674 got_worktree_close(worktree);
3682 usage_cherrypick(void)
3684 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3688 static const struct got_error *
3689 cmd_cherrypick(int argc, char *argv[])
3691 const struct got_error *error = NULL;
3692 struct got_worktree *worktree = NULL;
3693 struct got_repository *repo = NULL;
3694 char *cwd = NULL, *commit_id_str = NULL;
3695 struct got_object_id *commit_id = NULL;
3696 struct got_commit_object *commit = NULL;
3697 struct got_object_qid *pid;
3698 struct got_reference *head_ref = NULL;
3699 int ch, did_something = 0;
3701 while ((ch = getopt(argc, argv, "")) != -1) {
3713 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3714 "unveil", NULL) == -1)
3720 cwd = getcwd(NULL, 0);
3722 error = got_error_from_errno("getcwd");
3725 error = got_worktree_open(&worktree, cwd);
3729 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3733 error = apply_unveil(got_repo_get_path(repo), 0,
3734 got_worktree_get_root_path(worktree));
3738 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3739 GOT_OBJ_TYPE_COMMIT, repo);
3740 if (error != NULL) {
3741 struct got_reference *ref;
3742 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3744 error = got_ref_open(&ref, repo, argv[0], 0);
3747 error = got_ref_resolve(&commit_id, repo, ref);
3752 error = got_object_id_str(&commit_id_str, commit_id);
3756 error = got_ref_open(&head_ref, repo,
3757 got_worktree_get_head_ref_name(worktree), 0);
3761 error = check_same_branch(commit_id, head_ref, NULL, repo);
3763 if (error->code != GOT_ERR_ANCESTRY)
3767 error = got_error(GOT_ERR_SAME_BRANCH);
3771 error = got_object_open_as_commit(&commit, repo, commit_id);
3774 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3775 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3776 commit_id, repo, update_progress, &did_something, check_cancelled,
3782 printf("Merged commit %s\n", commit_id_str);
3785 got_object_commit_close(commit);
3786 free(commit_id_str);
3788 got_ref_close(head_ref);
3790 got_worktree_close(worktree);
3792 got_repo_close(repo);
3799 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3803 static const struct got_error *
3804 cmd_backout(int argc, char *argv[])
3806 const struct got_error *error = NULL;
3807 struct got_worktree *worktree = NULL;
3808 struct got_repository *repo = NULL;
3809 char *cwd = NULL, *commit_id_str = NULL;
3810 struct got_object_id *commit_id = NULL;
3811 struct got_commit_object *commit = NULL;
3812 struct got_object_qid *pid;
3813 struct got_reference *head_ref = NULL;
3814 int ch, did_something = 0;
3816 while ((ch = getopt(argc, argv, "")) != -1) {
3828 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3829 "unveil", NULL) == -1)
3835 cwd = getcwd(NULL, 0);
3837 error = got_error_from_errno("getcwd");
3840 error = got_worktree_open(&worktree, cwd);
3844 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3848 error = apply_unveil(got_repo_get_path(repo), 0,
3849 got_worktree_get_root_path(worktree));
3853 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3854 GOT_OBJ_TYPE_COMMIT, repo);
3855 if (error != NULL) {
3856 struct got_reference *ref;
3857 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3859 error = got_ref_open(&ref, repo, argv[0], 0);
3862 error = got_ref_resolve(&commit_id, repo, ref);
3867 error = got_object_id_str(&commit_id_str, commit_id);
3871 error = got_ref_open(&head_ref, repo,
3872 got_worktree_get_head_ref_name(worktree), 0);
3876 error = check_same_branch(commit_id, head_ref, NULL, repo);
3880 error = got_object_open_as_commit(&commit, repo, commit_id);
3883 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3885 error = got_error(GOT_ERR_ROOT_COMMIT);
3889 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3890 update_progress, &did_something, check_cancelled, NULL);
3895 printf("Backed out commit %s\n", commit_id_str);
3898 got_object_commit_close(commit);
3899 free(commit_id_str);
3901 got_ref_close(head_ref);
3903 got_worktree_close(worktree);
3905 got_repo_close(repo);
3912 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3918 trim_logmsg(char *logmsg, int limit)
3923 len = strlen(logmsg);
3927 nl = strchr(logmsg, '\n');
3932 static const struct got_error *
3933 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3935 const char *logmsg0 = NULL;
3937 logmsg0 = got_object_commit_get_logmsg(commit);
3939 while (isspace((unsigned char)logmsg0[0]))
3942 *logmsg = strdup(logmsg0);
3943 if (*logmsg == NULL)
3944 return got_error_from_errno("strdup");
3946 trim_logmsg(*logmsg, limit);
3950 static const struct got_error *
3951 show_rebase_progress(struct got_commit_object *commit,
3952 struct got_object_id *old_id, struct got_object_id *new_id)
3954 const struct got_error *err;
3955 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3957 err = got_object_id_str(&old_id_str, old_id);
3962 err = got_object_id_str(&new_id_str, new_id);
3967 old_id_str[12] = '\0';
3969 new_id_str[12] = '\0';
3971 err = get_short_logmsg(&logmsg, 42, commit);
3975 printf("%s -> %s: %s\n", old_id_str,
3976 new_id_str ? new_id_str : "no-op change", logmsg);
3983 static const struct got_error *
3984 rebase_progress(void *arg, unsigned char status, const char *path)
3986 unsigned char *rebase_status = arg;
3988 while (path[0] == '/')
3990 printf("%c %s\n", status, path);
3992 if (*rebase_status == GOT_STATUS_CONFLICT)
3994 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3995 *rebase_status = status;
3999 static const struct got_error *
4000 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4001 struct got_reference *branch, struct got_reference *new_base_branch,
4002 struct got_reference *tmp_branch, struct got_repository *repo)
4004 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4005 return got_worktree_rebase_complete(worktree, fileindex,
4006 new_base_branch, tmp_branch, branch, repo);
4009 static const struct got_error *
4010 rebase_commit(struct got_pathlist_head *merged_paths,
4011 struct got_worktree *worktree, struct got_fileindex *fileindex,
4012 struct got_reference *tmp_branch,
4013 struct got_object_id *commit_id, struct got_repository *repo)
4015 const struct got_error *error;
4016 struct got_commit_object *commit;
4017 struct got_object_id *new_commit_id;
4019 error = got_object_open_as_commit(&commit, repo, commit_id);
4023 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4024 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4026 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4028 error = show_rebase_progress(commit, commit_id, NULL);
4030 error = show_rebase_progress(commit, commit_id, new_commit_id);
4031 free(new_commit_id);
4034 got_object_commit_close(commit);
4038 struct check_path_prefix_arg {
4039 const char *path_prefix;
4044 static const struct got_error *
4045 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4046 struct got_blob_object *blob2, struct got_object_id *id1,
4047 struct got_object_id *id2, const char *path1, const char *path2,
4048 struct got_repository *repo)
4050 struct check_path_prefix_arg *a = arg;
4052 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4053 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4054 return got_error(a->errcode);
4059 static const struct got_error *
4060 check_path_prefix(struct got_object_id *parent_id,
4061 struct got_object_id *commit_id, const char *path_prefix,
4062 int errcode, struct got_repository *repo)
4064 const struct got_error *err;
4065 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4066 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4067 struct check_path_prefix_arg cpp_arg;
4069 if (got_path_is_root_dir(path_prefix))
4072 err = got_object_open_as_commit(&commit, repo, commit_id);
4076 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4080 err = got_object_open_as_tree(&tree1, repo,
4081 got_object_commit_get_tree_id(parent_commit));
4085 err = got_object_open_as_tree(&tree2, repo,
4086 got_object_commit_get_tree_id(commit));
4090 cpp_arg.path_prefix = path_prefix;
4091 while (cpp_arg.path_prefix[0] == '/')
4092 cpp_arg.path_prefix++;
4093 cpp_arg.len = strlen(cpp_arg.path_prefix);
4094 cpp_arg.errcode = errcode;
4095 err = got_diff_tree(tree1, tree2, "", "", repo,
4096 check_path_prefix_in_diff, &cpp_arg, 0);
4099 got_object_tree_close(tree1);
4101 got_object_tree_close(tree2);
4103 got_object_commit_close(commit);
4105 got_object_commit_close(parent_commit);
4109 static const struct got_error *
4110 collect_commits(struct got_object_id_queue *commits,
4111 struct got_object_id *initial_commit_id,
4112 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4113 const char *path_prefix, int path_prefix_errcode,
4114 struct got_repository *repo)
4116 const struct got_error *err = NULL;
4117 struct got_commit_graph *graph = NULL;
4118 struct got_object_id *parent_id = NULL;
4119 struct got_object_qid *qid;
4120 struct got_object_id *commit_id = initial_commit_id;
4122 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4126 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4129 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4130 err = got_commit_graph_iter_next(&parent_id, graph);
4132 if (err->code == GOT_ERR_ITER_COMPLETED) {
4133 err = got_error_msg(GOT_ERR_ANCESTRY,
4134 "ran out of commits to rebase before "
4135 "youngest common ancestor commit has "
4138 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4140 err = got_commit_graph_fetch_commits(graph, 1, repo);
4144 err = check_path_prefix(parent_id, commit_id,
4145 path_prefix, path_prefix_errcode, repo);
4149 err = got_object_qid_alloc(&qid, commit_id);
4152 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4153 commit_id = parent_id;
4157 got_commit_graph_close(graph);
4161 static const struct got_error *
4162 cmd_rebase(int argc, char *argv[])
4164 const struct got_error *error = NULL;
4165 struct got_worktree *worktree = NULL;
4166 struct got_repository *repo = NULL;
4167 struct got_fileindex *fileindex = NULL;
4169 struct got_reference *branch = NULL;
4170 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4171 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4172 struct got_object_id *resume_commit_id = NULL;
4173 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4174 struct got_commit_object *commit = NULL;
4175 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4176 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4177 struct got_object_id_queue commits;
4178 struct got_pathlist_head merged_paths;
4179 const struct got_object_id_queue *parent_ids;
4180 struct got_object_qid *qid, *pid;
4182 SIMPLEQ_INIT(&commits);
4183 TAILQ_INIT(&merged_paths);
4185 while ((ch = getopt(argc, argv, "ac")) != -1) {
4191 continue_rebase = 1;
4203 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4204 "unveil", NULL) == -1)
4207 if (abort_rebase && continue_rebase)
4209 else if (abort_rebase || continue_rebase) {
4212 } else if (argc != 1)
4215 cwd = getcwd(NULL, 0);
4217 error = got_error_from_errno("getcwd");
4220 error = got_worktree_open(&worktree, cwd);
4224 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4228 error = apply_unveil(got_repo_get_path(repo), 0,
4229 got_worktree_get_root_path(worktree));
4233 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4239 if (!rebase_in_progress) {
4240 error = got_error(GOT_ERR_NOT_REBASING);
4243 error = got_worktree_rebase_continue(&resume_commit_id,
4244 &new_base_branch, &tmp_branch, &branch, &fileindex,
4248 printf("Switching work tree to %s\n",
4249 got_ref_get_symref_target(new_base_branch));
4250 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4251 new_base_branch, update_progress, &did_something);
4254 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4255 goto done; /* nothing else to do */
4258 if (continue_rebase) {
4259 if (!rebase_in_progress) {
4260 error = got_error(GOT_ERR_NOT_REBASING);
4263 error = got_worktree_rebase_continue(&resume_commit_id,
4264 &new_base_branch, &tmp_branch, &branch, &fileindex,
4269 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4270 resume_commit_id, repo);
4274 yca_id = got_object_id_dup(resume_commit_id);
4275 if (yca_id == NULL) {
4276 error = got_error_from_errno("got_object_id_dup");
4280 error = got_ref_open(&branch, repo, argv[0], 0);
4285 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4289 if (!continue_rebase) {
4290 struct got_object_id *base_commit_id;
4292 base_commit_id = got_worktree_get_base_commit_id(worktree);
4293 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4294 base_commit_id, branch_head_commit_id, repo);
4297 if (yca_id == NULL) {
4298 error = got_error_msg(GOT_ERR_ANCESTRY,
4299 "specified branch shares no common ancestry "
4300 "with work tree's branch");
4304 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4306 if (error->code != GOT_ERR_ANCESTRY)
4310 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4311 "specified branch resolves to a commit which "
4312 "is already contained in work tree's branch");
4315 error = got_worktree_rebase_prepare(&new_base_branch,
4316 &tmp_branch, &fileindex, worktree, branch, repo);
4321 commit_id = branch_head_commit_id;
4322 error = got_object_open_as_commit(&commit, repo, commit_id);
4326 parent_ids = got_object_commit_get_parent_ids(commit);
4327 pid = SIMPLEQ_FIRST(parent_ids);
4329 if (!continue_rebase) {
4331 error = got_worktree_rebase_abort(worktree, fileindex,
4332 repo, new_base_branch, update_progress,
4336 printf("Rebase of %s aborted\n",
4337 got_ref_get_name(branch));
4339 error = got_error(GOT_ERR_EMPTY_REBASE);
4342 error = collect_commits(&commits, commit_id, pid->id,
4343 yca_id, got_worktree_get_path_prefix(worktree),
4344 GOT_ERR_REBASE_PATH, repo);
4345 got_object_commit_close(commit);
4350 if (SIMPLEQ_EMPTY(&commits)) {
4351 if (continue_rebase)
4352 error = rebase_complete(worktree, fileindex,
4353 branch, new_base_branch, tmp_branch, repo);
4355 error = got_error(GOT_ERR_EMPTY_REBASE);
4360 SIMPLEQ_FOREACH(qid, &commits, entry) {
4361 commit_id = qid->id;
4362 parent_id = pid ? pid->id : yca_id;
4365 error = got_worktree_rebase_merge_files(&merged_paths,
4366 worktree, fileindex, parent_id, commit_id, repo,
4367 rebase_progress, &rebase_status, check_cancelled, NULL);
4371 if (rebase_status == GOT_STATUS_CONFLICT) {
4372 got_worktree_rebase_pathlist_free(&merged_paths);
4376 error = rebase_commit(&merged_paths, worktree, fileindex,
4377 tmp_branch, commit_id, repo);
4378 got_worktree_rebase_pathlist_free(&merged_paths);
4383 if (rebase_status == GOT_STATUS_CONFLICT) {
4384 error = got_worktree_rebase_postpone(worktree, fileindex);
4387 error = got_error_msg(GOT_ERR_CONFLICTS,
4388 "conflicts must be resolved before rebasing can continue");
4390 error = rebase_complete(worktree, fileindex, branch,
4391 new_base_branch, tmp_branch, repo);
4393 got_object_id_queue_free(&commits);
4394 free(branch_head_commit_id);
4395 free(resume_commit_id);
4398 got_object_commit_close(commit);
4400 got_ref_close(branch);
4401 if (new_base_branch)
4402 got_ref_close(new_base_branch);
4404 got_ref_close(tmp_branch);
4406 got_worktree_close(worktree);
4408 got_repo_close(repo);
4413 usage_histedit(void)
4415 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4420 #define GOT_HISTEDIT_PICK 'p'
4421 #define GOT_HISTEDIT_EDIT 'e'
4422 #define GOT_HISTEDIT_FOLD 'f'
4423 #define GOT_HISTEDIT_DROP 'd'
4424 #define GOT_HISTEDIT_MESG 'm'
4426 static struct got_histedit_cmd {
4430 } got_histedit_cmds[] = {
4431 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4432 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4433 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4434 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4435 { GOT_HISTEDIT_MESG, "mesg",
4436 "single-line log message for commit above (open editor if empty)" },
4439 struct got_histedit_list_entry {
4440 TAILQ_ENTRY(got_histedit_list_entry) entry;
4441 struct got_object_id *commit_id;
4442 const struct got_histedit_cmd *cmd;
4445 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4447 static const struct got_error *
4448 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4449 FILE *f, struct got_repository *repo)
4451 const struct got_error *err = NULL;
4452 char *logmsg = NULL, *id_str = NULL;
4453 struct got_commit_object *commit = NULL;
4456 err = got_object_open_as_commit(&commit, repo, commit_id);
4460 err = get_short_logmsg(&logmsg, 34, commit);
4464 err = got_object_id_str(&id_str, commit_id);
4468 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4470 err = got_ferror(f, GOT_ERR_IO);
4473 got_object_commit_close(commit);
4479 static const struct got_error *
4480 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4481 struct got_repository *repo)
4483 const struct got_error *err = NULL;
4484 struct got_object_qid *qid;
4486 if (SIMPLEQ_EMPTY(commits))
4487 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4489 SIMPLEQ_FOREACH(qid, commits, entry) {
4490 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4499 static const struct got_error *
4500 write_cmd_list(FILE *f)
4502 const struct got_error *err = NULL;
4505 n = fprintf(f, "# Available histedit commands:\n");
4507 return got_ferror(f, GOT_ERR_IO);
4509 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4510 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4511 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4514 err = got_ferror(f, GOT_ERR_IO);
4518 n = fprintf(f, "# Commits will be processed in order from top to "
4519 "bottom of this file.\n");
4521 return got_ferror(f, GOT_ERR_IO);
4525 static const struct got_error *
4526 histedit_syntax_error(int lineno)
4528 static char msg[42];
4531 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4533 if (ret == -1 || ret >= sizeof(msg))
4534 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4536 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4539 static const struct got_error *
4540 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4541 char *logmsg, struct got_repository *repo)
4543 const struct got_error *err;
4544 struct got_commit_object *folded_commit = NULL;
4547 err = got_object_id_str(&id_str, hle->commit_id);
4551 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4555 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4556 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4557 got_object_commit_get_logmsg(folded_commit)) == -1) {
4558 err = got_error_from_errno("asprintf");
4563 got_object_commit_close(folded_commit);
4568 static struct got_histedit_list_entry *
4569 get_folded_commits(struct got_histedit_list_entry *hle)
4571 struct got_histedit_list_entry *prev, *folded = NULL;
4573 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4574 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4575 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4576 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4578 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4584 static const struct got_error *
4585 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4586 struct got_repository *repo)
4588 char *logmsg_path = NULL, *id_str = NULL;
4589 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4590 const struct got_error *err = NULL;
4591 struct got_commit_object *commit = NULL;
4593 struct got_histedit_list_entry *folded = NULL;
4595 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4599 folded = get_folded_commits(hle);
4601 while (folded != hle) {
4602 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4603 folded = TAILQ_NEXT(folded, entry);
4606 err = append_folded_commit_msg(&new_msg, folded,
4612 folded = TAILQ_NEXT(folded, entry);
4616 err = got_object_id_str(&id_str, hle->commit_id);
4619 if (asprintf(&new_msg,
4620 "%s\n# original log message of commit %s: %s",
4621 logmsg ? logmsg : "", id_str,
4622 got_object_commit_get_logmsg(commit)) == -1) {
4623 err = got_error_from_errno("asprintf");
4629 err = got_object_id_str(&id_str, hle->commit_id);
4633 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4637 dprintf(fd, logmsg);
4640 err = get_editor(&editor);
4644 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4646 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4649 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4650 if (hle->logmsg == NULL)
4651 err = got_error_from_errno("strdup");
4654 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4655 err = got_error_from_errno2("unlink", logmsg_path);
4660 got_object_commit_close(commit);
4664 static const struct got_error *
4665 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4666 FILE *f, struct got_repository *repo)
4668 const struct got_error *err = NULL;
4669 char *line = NULL, *p, *end;
4673 const struct got_histedit_cmd *cmd;
4674 struct got_object_id *commit_id = NULL;
4675 struct got_histedit_list_entry *hle = NULL;
4678 len = getline(&line, &size, f);
4680 const struct got_error *getline_err;
4683 getline_err = got_error_from_errno("getline");
4684 err = got_ferror(f, getline_err->code);
4689 while (isspace((unsigned char)p[0]))
4691 if (p[0] == '#' || p[0] == '\0') {
4697 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4698 cmd = &got_histedit_cmds[i];
4699 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4700 isspace((unsigned char)p[strlen(cmd->name)])) {
4701 p += strlen(cmd->name);
4704 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4709 if (i == nitems(got_histedit_cmds)) {
4710 err = histedit_syntax_error(lineno);
4713 while (isspace((unsigned char)p[0]))
4715 if (cmd->code == GOT_HISTEDIT_MESG) {
4716 if (hle == NULL || hle->logmsg != NULL) {
4717 err = got_error(GOT_ERR_HISTEDIT_CMD);
4721 err = histedit_edit_logmsg(hle, repo);
4725 hle->logmsg = strdup(p);
4726 if (hle->logmsg == NULL) {
4727 err = got_error_from_errno("strdup");
4736 while (end[0] && !isspace((unsigned char)end[0]))
4740 err = got_object_resolve_id_str(&commit_id, repo, p);
4742 /* override error code */
4743 err = histedit_syntax_error(lineno);
4747 hle = malloc(sizeof(*hle));
4749 err = got_error_from_errno("malloc");
4753 hle->commit_id = commit_id;
4758 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4766 static const struct got_error *
4767 histedit_check_script(struct got_histedit_list *histedit_cmds,
4768 struct got_object_id_queue *commits, struct got_repository *repo)
4770 const struct got_error *err = NULL;
4771 struct got_object_qid *qid;
4772 struct got_histedit_list_entry *hle;
4773 static char msg[80];
4776 if (TAILQ_EMPTY(histedit_cmds))
4777 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4778 "histedit script contains no commands");
4779 if (SIMPLEQ_EMPTY(commits))
4780 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4782 SIMPLEQ_FOREACH(qid, commits, entry) {
4783 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4784 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4788 err = got_object_id_str(&id_str, qid->id);
4791 snprintf(msg, sizeof(msg),
4792 "commit %s missing from histedit script", id_str);
4794 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4798 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4799 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4800 "last commit in histedit script cannot be folded");
4805 static const struct got_error *
4806 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4807 const char *path, struct got_object_id_queue *commits,
4808 struct got_repository *repo)
4810 const struct got_error *err = NULL;
4814 err = get_editor(&editor);
4818 if (spawn_editor(editor, path) == -1) {
4819 err = got_error_from_errno("failed spawning editor");
4823 f = fopen(path, "r");
4825 err = got_error_from_errno("fopen");
4828 err = histedit_parse_list(histedit_cmds, f, repo);
4832 err = histedit_check_script(histedit_cmds, commits, repo);
4834 if (f && fclose(f) != 0 && err == NULL)
4835 err = got_error_from_errno("fclose");
4840 static const struct got_error *
4841 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4842 struct got_object_id_queue *, const char *, struct got_repository *);
4844 static const struct got_error *
4845 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4846 struct got_object_id_queue *commits, struct got_repository *repo)
4848 const struct got_error *err;
4852 err = got_opentemp_named(&path, &f, "got-histedit");
4856 err = write_cmd_list(f);
4860 err = histedit_write_commit_list(commits, f, repo);
4864 if (fclose(f) != 0) {
4865 err = got_error_from_errno("fclose");
4870 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4872 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4873 err->code != GOT_ERR_HISTEDIT_CMD)
4875 err = histedit_edit_list_retry(histedit_cmds, err,
4876 commits, path, repo);
4879 if (f && fclose(f) != 0 && err == NULL)
4880 err = got_error_from_errno("fclose");
4881 if (path && unlink(path) != 0 && err == NULL)
4882 err = got_error_from_errno2("unlink", path);
4887 static const struct got_error *
4888 histedit_save_list(struct got_histedit_list *histedit_cmds,
4889 struct got_worktree *worktree, struct got_repository *repo)
4891 const struct got_error *err = NULL;
4894 struct got_histedit_list_entry *hle;
4895 struct got_commit_object *commit = NULL;
4897 err = got_worktree_get_histedit_script_path(&path, worktree);
4901 f = fopen(path, "w");
4903 err = got_error_from_errno2("fopen", path);
4906 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4907 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4913 int n = fprintf(f, "%c %s\n",
4914 GOT_HISTEDIT_MESG, hle->logmsg);
4916 err = got_ferror(f, GOT_ERR_IO);
4922 if (f && fclose(f) != 0 && err == NULL)
4923 err = got_error_from_errno("fclose");
4926 got_object_commit_close(commit);
4931 histedit_free_list(struct got_histedit_list *histedit_cmds)
4933 struct got_histedit_list_entry *hle;
4935 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4936 TAILQ_REMOVE(histedit_cmds, hle, entry);
4941 static const struct got_error *
4942 histedit_load_list(struct got_histedit_list *histedit_cmds,
4943 const char *path, struct got_repository *repo)
4945 const struct got_error *err = NULL;
4948 f = fopen(path, "r");
4950 err = got_error_from_errno2("fopen", path);
4954 err = histedit_parse_list(histedit_cmds, f, repo);
4956 if (f && fclose(f) != 0 && err == NULL)
4957 err = got_error_from_errno("fclose");
4961 static const struct got_error *
4962 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4963 const struct got_error *edit_err, struct got_object_id_queue *commits,
4964 const char *path, struct got_repository *repo)
4966 const struct got_error *err = NULL, *prev_err = edit_err;
4969 while (resp != 'c' && resp != 'r' && resp != 'a') {
4970 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4971 "or (a)bort: ", getprogname(), prev_err->msg);
4976 histedit_free_list(histedit_cmds);
4977 err = histedit_run_editor(histedit_cmds, path, commits,
4980 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4981 err->code != GOT_ERR_HISTEDIT_CMD)
4988 } else if (resp == 'r') {
4989 histedit_free_list(histedit_cmds);
4990 err = histedit_edit_script(histedit_cmds,
4993 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4994 err->code != GOT_ERR_HISTEDIT_CMD)
5001 } else if (resp == 'a') {
5002 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5005 printf("invalid response '%c'\n", resp);
5011 static const struct got_error *
5012 histedit_complete(struct got_worktree *worktree,
5013 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5014 struct got_reference *branch, struct got_repository *repo)
5016 printf("Switching work tree to %s\n",
5017 got_ref_get_symref_target(branch));
5018 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5022 static const struct got_error *
5023 show_histedit_progress(struct got_commit_object *commit,
5024 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5026 const struct got_error *err;
5027 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5029 err = got_object_id_str(&old_id_str, hle->commit_id);
5034 err = got_object_id_str(&new_id_str, new_id);
5039 old_id_str[12] = '\0';
5041 new_id_str[12] = '\0';
5044 logmsg = strdup(hle->logmsg);
5045 if (logmsg == NULL) {
5046 err = got_error_from_errno("strdup");
5049 trim_logmsg(logmsg, 42);
5051 err = get_short_logmsg(&logmsg, 42, commit);
5056 switch (hle->cmd->code) {
5057 case GOT_HISTEDIT_PICK:
5058 case GOT_HISTEDIT_EDIT:
5059 printf("%s -> %s: %s\n", old_id_str,
5060 new_id_str ? new_id_str : "no-op change", logmsg);
5062 case GOT_HISTEDIT_DROP:
5063 case GOT_HISTEDIT_FOLD:
5064 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5077 static const struct got_error *
5078 histedit_commit(struct got_pathlist_head *merged_paths,
5079 struct got_worktree *worktree, struct got_fileindex *fileindex,
5080 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5081 struct got_repository *repo)
5083 const struct got_error *err;
5084 struct got_commit_object *commit;
5085 struct got_object_id *new_commit_id;
5087 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5088 && hle->logmsg == NULL) {
5089 err = histedit_edit_logmsg(hle, repo);
5094 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5098 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5099 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5102 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5104 err = show_histedit_progress(commit, hle, NULL);
5106 err = show_histedit_progress(commit, hle, new_commit_id);
5107 free(new_commit_id);
5110 got_object_commit_close(commit);
5114 static const struct got_error *
5115 histedit_skip_commit(struct got_histedit_list_entry *hle,
5116 struct got_worktree *worktree, struct got_repository *repo)
5118 const struct got_error *error;
5119 struct got_commit_object *commit;
5121 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5126 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5130 error = show_histedit_progress(commit, hle, NULL);
5131 got_object_commit_close(commit);
5135 static const struct got_error *
5136 cmd_histedit(int argc, char *argv[])
5138 const struct got_error *error = NULL;
5139 struct got_worktree *worktree = NULL;
5140 struct got_fileindex *fileindex = NULL;
5141 struct got_repository *repo = NULL;
5143 struct got_reference *branch = NULL;
5144 struct got_reference *tmp_branch = NULL;
5145 struct got_object_id *resume_commit_id = NULL;
5146 struct got_object_id *base_commit_id = NULL;
5147 struct got_object_id *head_commit_id = NULL;
5148 struct got_commit_object *commit = NULL;
5149 int ch, rebase_in_progress = 0, did_something;
5150 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5151 const char *edit_script_path = NULL;
5152 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5153 struct got_object_id_queue commits;
5154 struct got_pathlist_head merged_paths;
5155 const struct got_object_id_queue *parent_ids;
5156 struct got_object_qid *pid;
5157 struct got_histedit_list histedit_cmds;
5158 struct got_histedit_list_entry *hle;
5160 SIMPLEQ_INIT(&commits);
5161 TAILQ_INIT(&histedit_cmds);
5162 TAILQ_INIT(&merged_paths);
5164 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5173 edit_script_path = optarg;
5185 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5186 "unveil", NULL) == -1)
5189 if (abort_edit && continue_edit)
5195 * This command cannot apply unveil(2) in all cases because the
5196 * user may choose to run an editor to edit the histedit script
5197 * and to edit individual commit log messages.
5198 * unveil(2) traverses exec(2); if an editor is used we have to
5199 * apply unveil after edit script and log messages have been written.
5200 * XXX TODO: Make use of unveil(2) where possible.
5203 cwd = getcwd(NULL, 0);
5205 error = got_error_from_errno("getcwd");
5208 error = got_worktree_open(&worktree, cwd);
5212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5216 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5219 if (rebase_in_progress) {
5220 error = got_error(GOT_ERR_REBASING);
5224 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5228 if (edit_in_progress && abort_edit) {
5229 error = got_worktree_histedit_continue(&resume_commit_id,
5230 &tmp_branch, &branch, &base_commit_id, &fileindex,
5234 printf("Switching work tree to %s\n",
5235 got_ref_get_symref_target(branch));
5236 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5237 branch, base_commit_id, update_progress, &did_something);
5240 printf("Histedit of %s aborted\n",
5241 got_ref_get_symref_target(branch));
5242 goto done; /* nothing else to do */
5243 } else if (abort_edit) {
5244 error = got_error(GOT_ERR_NOT_HISTEDIT);
5248 if (continue_edit) {
5251 if (!edit_in_progress) {
5252 error = got_error(GOT_ERR_NOT_HISTEDIT);
5256 error = got_worktree_get_histedit_script_path(&path, worktree);
5260 error = histedit_load_list(&histedit_cmds, path, repo);
5265 error = got_worktree_histedit_continue(&resume_commit_id,
5266 &tmp_branch, &branch, &base_commit_id, &fileindex,
5271 error = got_ref_resolve(&head_commit_id, repo, branch);
5275 error = got_object_open_as_commit(&commit, repo,
5279 parent_ids = got_object_commit_get_parent_ids(commit);
5280 pid = SIMPLEQ_FIRST(parent_ids);
5282 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5285 error = collect_commits(&commits, head_commit_id, pid->id,
5286 base_commit_id, got_worktree_get_path_prefix(worktree),
5287 GOT_ERR_HISTEDIT_PATH, repo);
5288 got_object_commit_close(commit);
5293 if (edit_in_progress) {
5294 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5298 error = got_ref_open(&branch, repo,
5299 got_worktree_get_head_ref_name(worktree), 0);
5303 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5304 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5305 "will not edit commit history of a branch outside "
5306 "the \"refs/heads/\" reference namespace");
5310 error = got_ref_resolve(&head_commit_id, repo, branch);
5311 got_ref_close(branch);
5316 error = got_object_open_as_commit(&commit, repo,
5320 parent_ids = got_object_commit_get_parent_ids(commit);
5321 pid = SIMPLEQ_FIRST(parent_ids);
5323 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5326 error = collect_commits(&commits, head_commit_id, pid->id,
5327 got_worktree_get_base_commit_id(worktree),
5328 got_worktree_get_path_prefix(worktree),
5329 GOT_ERR_HISTEDIT_PATH, repo);
5330 got_object_commit_close(commit);
5335 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5336 &base_commit_id, &fileindex, worktree, repo);
5340 if (edit_script_path) {
5341 error = histedit_load_list(&histedit_cmds,
5342 edit_script_path, repo);
5344 got_worktree_histedit_abort(worktree, fileindex,
5345 repo, branch, base_commit_id,
5346 update_progress, &did_something);
5350 error = histedit_edit_script(&histedit_cmds, &commits,
5353 got_worktree_histedit_abort(worktree, fileindex,
5354 repo, branch, base_commit_id,
5355 update_progress, &did_something);
5361 error = histedit_save_list(&histedit_cmds, worktree,
5364 got_worktree_histedit_abort(worktree, fileindex,
5365 repo, branch, base_commit_id,
5366 update_progress, &did_something);
5372 error = histedit_check_script(&histedit_cmds, &commits, repo);
5376 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5377 if (resume_commit_id) {
5378 if (got_object_id_cmp(hle->commit_id,
5379 resume_commit_id) != 0)
5382 resume_commit_id = NULL;
5383 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5384 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5385 error = histedit_skip_commit(hle, worktree,
5388 error = histedit_commit(NULL, worktree,
5389 fileindex, tmp_branch, hle, repo);
5396 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5397 error = histedit_skip_commit(hle, worktree, repo);
5403 error = got_object_open_as_commit(&commit, repo,
5407 parent_ids = got_object_commit_get_parent_ids(commit);
5408 pid = SIMPLEQ_FIRST(parent_ids);
5410 error = got_worktree_histedit_merge_files(&merged_paths,
5411 worktree, fileindex, pid->id, hle->commit_id, repo,
5412 rebase_progress, &rebase_status, check_cancelled, NULL);
5415 got_object_commit_close(commit);
5418 if (rebase_status == GOT_STATUS_CONFLICT) {
5419 got_worktree_rebase_pathlist_free(&merged_paths);
5423 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5425 error = got_object_id_str(&id_str, hle->commit_id);
5428 printf("Stopping histedit for amending commit %s\n",
5431 got_worktree_rebase_pathlist_free(&merged_paths);
5432 error = got_worktree_histedit_postpone(worktree,
5437 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5438 error = histedit_skip_commit(hle, worktree, repo);
5444 error = histedit_commit(&merged_paths, worktree, fileindex,
5445 tmp_branch, hle, repo);
5446 got_worktree_rebase_pathlist_free(&merged_paths);
5451 if (rebase_status == GOT_STATUS_CONFLICT) {
5452 error = got_worktree_histedit_postpone(worktree, fileindex);
5455 error = got_error_msg(GOT_ERR_CONFLICTS,
5456 "conflicts must be resolved before rebasing can continue");
5458 error = histedit_complete(worktree, fileindex, tmp_branch,
5461 got_object_id_queue_free(&commits);
5462 histedit_free_list(&histedit_cmds);
5463 free(head_commit_id);
5464 free(base_commit_id);
5465 free(resume_commit_id);
5467 got_object_commit_close(commit);
5469 got_ref_close(branch);
5471 got_ref_close(tmp_branch);
5473 got_worktree_close(worktree);
5475 got_repo_close(repo);
5482 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5483 "[file-path ...]\n",
5488 static const struct got_error *
5489 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5490 const char *path, struct got_object_id *blob_id,
5491 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5493 const struct got_error *err = NULL;
5494 char *id_str = NULL;
5496 if (staged_status != GOT_STATUS_ADD &&
5497 staged_status != GOT_STATUS_MODIFY &&
5498 staged_status != GOT_STATUS_DELETE)
5501 if (staged_status == GOT_STATUS_ADD ||
5502 staged_status == GOT_STATUS_MODIFY)
5503 err = got_object_id_str(&id_str, staged_blob_id);
5505 err = got_object_id_str(&id_str, blob_id);
5509 printf("%s %c %s\n", id_str, staged_status, path);
5514 static const struct got_error *
5515 cmd_stage(int argc, char *argv[])
5517 const struct got_error *error = NULL;
5518 struct got_repository *repo = NULL;
5519 struct got_worktree *worktree = NULL;
5521 struct got_pathlist_head paths;
5522 struct got_pathlist_entry *pe;
5523 int ch, list_stage = 0, pflag = 0;
5524 FILE *patch_script_file = NULL;
5525 const char *patch_script_path = NULL;
5526 struct choose_patch_arg cpa;
5530 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5539 patch_script_path = optarg;
5551 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5552 "unveil", NULL) == -1)
5555 if (list_stage && (pflag || patch_script_path))
5556 errx(1, "-l option cannot be used with other options");
5557 if (patch_script_path && !pflag)
5558 errx(1, "-F option can only be used together with -p option");
5560 cwd = getcwd(NULL, 0);
5562 error = got_error_from_errno("getcwd");
5566 error = got_worktree_open(&worktree, cwd);
5570 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5574 if (patch_script_path) {
5575 patch_script_file = fopen(patch_script_path, "r");
5576 if (patch_script_file == NULL) {
5577 error = got_error_from_errno2("fopen",
5582 error = apply_unveil(got_repo_get_path(repo), 1,
5583 got_worktree_get_root_path(worktree));
5587 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5592 error = got_worktree_status(worktree, &paths, repo,
5593 print_stage, NULL, check_cancelled, NULL);
5595 cpa.patch_script_file = patch_script_file;
5596 cpa.action = "stage";
5597 error = got_worktree_stage(worktree, &paths,
5598 pflag ? NULL : print_status, NULL,
5599 pflag ? choose_patch : NULL, &cpa, repo);
5602 if (patch_script_file && fclose(patch_script_file) == EOF &&
5604 error = got_error_from_errno2("fclose", patch_script_path);
5606 got_repo_close(repo);
5608 got_worktree_close(worktree);
5609 TAILQ_FOREACH(pe, &paths, entry)
5610 free((char *)pe->path);
5611 got_pathlist_free(&paths);
5619 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5620 "[file-path ...]\n",
5626 static const struct got_error *
5627 cmd_unstage(int argc, char *argv[])
5629 const struct got_error *error = NULL;
5630 struct got_repository *repo = NULL;
5631 struct got_worktree *worktree = NULL;
5633 struct got_pathlist_head paths;
5634 struct got_pathlist_entry *pe;
5635 int ch, did_something = 0, pflag = 0;
5636 FILE *patch_script_file = NULL;
5637 const char *patch_script_path = NULL;
5638 struct choose_patch_arg cpa;
5642 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5648 patch_script_path = optarg;
5660 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5661 "unveil", NULL) == -1)
5664 if (patch_script_path && !pflag)
5665 errx(1, "-F option can only be used together with -p option");
5667 cwd = getcwd(NULL, 0);
5669 error = got_error_from_errno("getcwd");
5673 error = got_worktree_open(&worktree, cwd);
5677 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5681 if (patch_script_path) {
5682 patch_script_file = fopen(patch_script_path, "r");
5683 if (patch_script_file == NULL) {
5684 error = got_error_from_errno2("fopen",
5690 error = apply_unveil(got_repo_get_path(repo), 1,
5691 got_worktree_get_root_path(worktree));
5695 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5699 cpa.patch_script_file = patch_script_file;
5700 cpa.action = "unstage";
5701 error = got_worktree_unstage(worktree, &paths, update_progress,
5702 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5704 if (patch_script_file && fclose(patch_script_file) == EOF &&
5706 error = got_error_from_errno2("fclose", patch_script_path);
5708 got_repo_close(repo);
5710 got_worktree_close(worktree);
5711 TAILQ_FOREACH(pe, &paths, entry)
5712 free((char *)pe->path);
5713 got_pathlist_free(&paths);