2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/limits.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
59 catch_sigint(int signo)
65 catch_sigpipe(int signo)
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
161 main(int argc, char *argv[])
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
189 got_version_print_str();
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
241 editor = getenv("VISUAL");
243 editor = getenv("EDITOR");
246 err = got_path_find_prog(abspath, editor);
251 if (*abspath == NULL) {
252 *abspath = strdup("/bin/ed");
253 if (*abspath == NULL)
254 return got_error_from_errno("strdup");
260 static const struct got_error *
261 apply_unveil(const char *repo_path, int repo_read_only,
262 const char *worktree_path)
264 const struct got_error *err;
267 if (unveil("gmon.out", "rwc") != 0)
268 return got_error_from_errno2("unveil", "gmon.out");
270 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
271 return got_error_from_errno2("unveil", repo_path);
273 if (worktree_path && unveil(worktree_path, "rwc") != 0)
274 return got_error_from_errno2("unveil", worktree_path);
276 if (unveil("/tmp", "rwc") != 0)
277 return got_error_from_errno2("unveil", "/tmp");
279 err = got_privsep_unveil_exec_helpers();
283 if (unveil(NULL, NULL) != 0)
284 return got_error_from_errno("unveil");
292 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
296 static const struct got_error *
297 cmd_init(int argc, char *argv[])
299 const struct got_error *error = NULL;
300 char *repo_path = NULL;
303 while ((ch = getopt(argc, argv, "")) != -1) {
315 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
321 repo_path = strdup(argv[0]);
322 if (repo_path == NULL)
323 return got_error_from_errno("strdup");
325 got_path_strip_trailing_slashes(repo_path);
327 error = got_path_mkdir(repo_path);
329 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
332 error = apply_unveil(repo_path, 0, NULL);
336 error = got_repo_init(repo_path);
348 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
349 "[-r repository-path] [-I pattern] path\n", getprogname());
354 spawn_editor(const char *editor, const char *file)
357 sig_t sighup, sigint, sigquit;
360 sighup = signal(SIGHUP, SIG_IGN);
361 sigint = signal(SIGINT, SIG_IGN);
362 sigquit = signal(SIGQUIT, SIG_IGN);
364 switch (pid = fork()) {
368 execl(editor, editor, file, (char *)NULL);
372 while (waitpid(pid, &st, 0) == -1)
377 (void)signal(SIGHUP, sighup);
378 (void)signal(SIGINT, sigint);
379 (void)signal(SIGQUIT, sigquit);
381 if (!WIFEXITED(st)) {
386 return WEXITSTATUS(st);
389 static const struct got_error *
390 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
391 const char *initial_content)
393 const struct got_error *err = NULL;
397 int content_changed = 0;
402 if (stat(logmsg_path, &st) == -1)
403 return got_error_from_errno2("stat", logmsg_path);
405 if (spawn_editor(editor, logmsg_path) == -1)
406 return got_error_from_errno("failed spawning editor");
408 if (stat(logmsg_path, &st2) == -1)
409 return got_error_from_errno("stat");
411 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
412 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
413 "no changes made to commit message, aborting");
415 *logmsg = malloc(st2.st_size + 1);
417 return got_error_from_errno("malloc");
421 fp = fopen(logmsg_path, "r");
423 err = got_error_from_errno("fopen");
426 while (fgets(buf, sizeof(buf), fp) != NULL) {
427 if (!content_changed && strcmp(buf, initial_content) != 0)
429 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
430 continue; /* remove comments and leading empty lines */
431 len = strlcat(*logmsg, buf, st2.st_size);
435 while (len > 0 && (*logmsg)[len - 1] == '\n') {
436 (*logmsg)[len - 1] = '\0';
440 if (len == 0 || !content_changed)
441 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
442 "commit message cannot be empty, aborting");
451 static const struct got_error *
452 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
453 const char *branch_name)
455 char *initial_content = NULL, *logmsg_path = NULL;
456 const struct got_error *err = NULL;
459 if (asprintf(&initial_content,
460 "\n# %s to be imported to branch %s\n", path_dir,
462 return got_error_from_errno("asprintf");
464 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
468 dprintf(fd, initial_content);
471 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
473 free(initial_content);
478 static const struct got_error *
479 import_progress(void *arg, const char *path)
481 printf("A %s\n", path);
485 static const struct got_error *
486 get_author(const char **author)
488 const char *got_author;
492 got_author = getenv("GOT_AUTHOR");
493 if (got_author == NULL) {
494 /* TODO: Look up user in password database? */
495 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
498 *author = got_author;
501 * Really dumb email address check; we're only doing this to
502 * avoid git's object parser breaking on commits we create.
504 while (*got_author && *got_author != '<')
506 if (*got_author != '<')
507 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
508 while (*got_author && *got_author != '@')
510 if (*got_author != '@')
511 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
512 while (*got_author && *got_author != '>')
514 if (*got_author != '>')
515 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
520 static const struct got_error *
521 cmd_import(int argc, char *argv[])
523 const struct got_error *error = NULL;
524 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
527 const char *branch_name = "master";
528 char *refname = NULL, *id_str = NULL;
529 struct got_repository *repo = NULL;
530 struct got_reference *branch_ref = NULL, *head_ref = NULL;
531 struct got_object_id *new_commit_id = NULL;
533 struct got_pathlist_head ignores;
534 struct got_pathlist_entry *pe;
536 TAILQ_INIT(&ignores);
538 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
541 branch_name = optarg;
544 logmsg = strdup(optarg);
545 if (logmsg == NULL) {
546 error = got_error_from_errno("strdup");
551 repo_path = realpath(optarg, NULL);
552 if (repo_path == NULL) {
553 error = got_error_from_errno("realpath");
558 if (optarg[0] == '\0')
560 error = got_pathlist_insert(&pe, &ignores, optarg,
575 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
582 error = get_author(&author);
586 if (repo_path == NULL) {
587 repo_path = getcwd(NULL, 0);
588 if (repo_path == NULL)
589 return got_error_from_errno("getcwd");
591 got_path_strip_trailing_slashes(repo_path);
592 error = got_repo_open(&repo, repo_path);
596 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
597 error = got_error_from_errno("asprintf");
601 error = got_ref_open(&branch_ref, repo, refname, 0);
603 if (error->code != GOT_ERR_NOT_REF)
606 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
607 "import target branch already exists");
611 path_dir = realpath(argv[0], NULL);
612 if (path_dir == NULL) {
613 error = got_error_from_errno("realpath");
616 got_path_strip_trailing_slashes(path_dir);
619 * unveil(2) traverses exec(2); if an editor is used we have
620 * to apply unveil after the log message has been written.
622 if (logmsg == NULL || strlen(logmsg) == 0) {
623 error = get_editor(&editor);
626 error = collect_import_msg(&logmsg, editor, path_dir, refname);
631 if (unveil(path_dir, "r") != 0)
632 return got_error_from_errno2("unveil", path_dir);
634 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
638 error = got_repo_import(&new_commit_id, path_dir, logmsg,
639 author, &ignores, repo, import_progress, NULL);
643 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
647 error = got_ref_write(branch_ref, repo);
651 error = got_object_id_str(&id_str, new_commit_id);
655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
657 if (error->code != GOT_ERR_NOT_REF)
660 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
665 error = got_ref_write(head_ref, repo);
670 printf("Created branch %s with commit %s\n",
671 got_ref_get_name(branch_ref), id_str);
679 got_ref_close(branch_ref);
681 got_ref_close(head_ref);
688 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
689 "[-p prefix] repository-path [worktree-path]\n", getprogname());
693 static const struct got_error *
694 checkout_progress(void *arg, unsigned char status, const char *path)
696 char *worktree_path = arg;
698 /* Base commit bump happens silently. */
699 if (status == GOT_STATUS_BUMP_BASE)
702 while (path[0] == '/')
705 printf("%c %s/%s\n", status, worktree_path, path);
709 static const struct got_error *
710 check_cancelled(void *arg)
712 if (sigint_received || sigpipe_received)
713 return got_error(GOT_ERR_CANCELLED);
717 static const struct got_error *
718 check_linear_ancestry(struct got_object_id *commit_id,
719 struct got_object_id *base_commit_id, struct got_repository *repo)
721 const struct got_error *err = NULL;
722 struct got_object_id *yca_id;
724 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
725 commit_id, base_commit_id, repo);
730 return got_error(GOT_ERR_ANCESTRY);
733 * Require a straight line of history between the target commit
734 * and the work tree's base commit.
736 * Non-linear situations such as this require a rebase:
738 * (commit) D F (base_commit)
746 * 'got update' only handles linear cases:
747 * Update forwards in time: A (base/yca) - B - C - D (commit)
748 * Update backwards in time: D (base) - C - B - A (commit/yca)
750 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
751 got_object_id_cmp(base_commit_id, yca_id) != 0)
752 return got_error(GOT_ERR_ANCESTRY);
758 static const struct got_error *
759 check_same_branch(struct got_object_id *commit_id,
760 struct got_reference *head_ref, struct got_object_id *yca_id,
761 struct got_repository *repo)
763 const struct got_error *err = NULL;
764 struct got_commit_graph *graph = NULL;
765 struct got_object_id *head_commit_id = NULL;
766 int is_same_branch = 0;
768 err = got_ref_resolve(&head_commit_id, repo, head_ref);
772 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
776 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
781 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
785 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
790 struct got_object_id *id;
791 err = got_commit_graph_iter_next(&id, graph);
793 if (err->code == GOT_ERR_ITER_COMPLETED) {
796 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
798 err = got_commit_graph_fetch_commits(graph, 1,
805 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
807 if (got_object_id_cmp(id, commit_id) == 0) {
815 got_commit_graph_close(graph);
816 free(head_commit_id);
817 if (!err && !is_same_branch)
818 err = got_error(GOT_ERR_ANCESTRY);
822 static const struct got_error *
823 resolve_commit_arg(struct got_object_id **commit_id,
824 const char *commit_id_arg, struct got_repository *repo)
826 const struct got_error *err;
827 struct got_reference *ref;
828 struct got_tag_object *tag;
830 err = got_repo_object_match_tag(&tag, commit_id_arg,
831 GOT_OBJ_TYPE_COMMIT, repo);
833 *commit_id = got_object_id_dup(
834 got_object_tag_get_object_id(tag));
835 if (*commit_id == NULL)
836 err = got_error_from_errno("got_object_id_dup");
837 got_object_tag_close(tag);
839 } else if (err->code != GOT_ERR_NO_OBJ)
842 err = got_ref_open(&ref, repo, commit_id_arg, 0);
844 err = got_ref_resolve(commit_id, repo, ref);
847 if (err->code != GOT_ERR_NOT_REF)
849 err = got_repo_match_object_id_prefix(commit_id,
850 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
855 static const struct got_error *
856 cmd_checkout(int argc, char *argv[])
858 const struct got_error *error = NULL;
859 struct got_repository *repo = NULL;
860 struct got_reference *head_ref = NULL;
861 struct got_worktree *worktree = NULL;
862 char *repo_path = NULL;
863 char *worktree_path = NULL;
864 const char *path_prefix = "";
865 const char *branch_name = GOT_REF_HEAD;
866 char *commit_id_str = NULL;
867 int ch, same_path_prefix;
868 struct got_pathlist_head paths;
872 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
875 branch_name = optarg;
878 commit_id_str = strdup(optarg);
879 if (commit_id_str == NULL)
880 return got_error_from_errno("strdup");
883 path_prefix = optarg;
895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
896 "unveil", NULL) == -1)
900 char *cwd, *base, *dotgit;
901 repo_path = realpath(argv[0], NULL);
902 if (repo_path == NULL)
903 return got_error_from_errno2("realpath", argv[0]);
904 cwd = getcwd(NULL, 0);
906 error = got_error_from_errno("getcwd");
909 if (path_prefix[0]) {
910 base = basename(path_prefix);
912 error = got_error_from_errno2("basename",
917 base = basename(repo_path);
919 error = got_error_from_errno2("basename",
924 dotgit = strstr(base, ".git");
927 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
928 error = got_error_from_errno("asprintf");
933 } else if (argc == 2) {
934 repo_path = realpath(argv[0], NULL);
935 if (repo_path == NULL) {
936 error = got_error_from_errno2("realpath", argv[0]);
939 worktree_path = realpath(argv[1], NULL);
940 if (worktree_path == NULL) {
941 if (errno != ENOENT) {
942 error = got_error_from_errno2("realpath",
946 worktree_path = strdup(argv[1]);
947 if (worktree_path == NULL) {
948 error = got_error_from_errno("strdup");
955 got_path_strip_trailing_slashes(repo_path);
956 got_path_strip_trailing_slashes(worktree_path);
958 error = got_repo_open(&repo, repo_path);
962 /* Pre-create work tree path for unveil(2) */
963 error = got_path_mkdir(worktree_path);
965 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
966 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
968 if (!got_path_dir_is_empty(worktree_path)) {
969 error = got_error_path(worktree_path,
970 GOT_ERR_DIR_NOT_EMPTY);
975 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
979 error = got_ref_open(&head_ref, repo, branch_name, 0);
983 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
984 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
987 error = got_worktree_open(&worktree, worktree_path);
991 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
995 if (!same_path_prefix) {
996 error = got_error(GOT_ERR_PATH_PREFIX);
1000 if (commit_id_str) {
1001 struct got_object_id *commit_id;
1002 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1005 error = check_linear_ancestry(commit_id,
1006 got_worktree_get_base_commit_id(worktree), repo);
1007 if (error != NULL) {
1011 error = check_same_branch(commit_id, head_ref, NULL, repo);
1014 error = got_worktree_set_base_commit_id(worktree, repo,
1021 error = got_pathlist_append(&paths, "", NULL);
1024 error = got_worktree_checkout_files(worktree, &paths, repo,
1025 checkout_progress, worktree_path, check_cancelled, NULL);
1029 printf("Now shut up and hack\n");
1032 got_pathlist_free(&paths);
1033 free(commit_id_str);
1035 free(worktree_path);
1042 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1047 static const struct got_error *
1048 update_progress(void *arg, unsigned char status, const char *path)
1050 int *did_something = arg;
1052 if (status == GOT_STATUS_EXISTS)
1057 /* Base commit bump happens silently. */
1058 if (status == GOT_STATUS_BUMP_BASE)
1061 while (path[0] == '/')
1063 printf("%c %s\n", status, path);
1067 static const struct got_error *
1068 switch_head_ref(struct got_reference *head_ref,
1069 struct got_object_id *commit_id, struct got_worktree *worktree,
1070 struct got_repository *repo)
1072 const struct got_error *err = NULL;
1074 int ref_has_moved = 0;
1076 /* Trivial case: switching between two different references. */
1077 if (strcmp(got_ref_get_name(head_ref),
1078 got_worktree_get_head_ref_name(worktree)) != 0) {
1079 printf("Switching work tree from %s to %s\n",
1080 got_worktree_get_head_ref_name(worktree),
1081 got_ref_get_name(head_ref));
1082 return got_worktree_set_head_ref(worktree, head_ref);
1085 err = check_linear_ancestry(commit_id,
1086 got_worktree_get_base_commit_id(worktree), repo);
1088 if (err->code != GOT_ERR_ANCESTRY)
1095 /* Switching to a rebased branch with the same reference name. */
1096 err = got_object_id_str(&base_id_str,
1097 got_worktree_get_base_commit_id(worktree));
1100 printf("Reference %s now points at a different branch\n",
1101 got_worktree_get_head_ref_name(worktree));
1102 printf("Switching work tree from %s to %s\n", base_id_str,
1103 got_worktree_get_head_ref_name(worktree));
1107 static const struct got_error *
1108 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1110 const struct got_error *err;
1113 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1117 return got_error(GOT_ERR_REBASING);
1119 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1123 return got_error(GOT_ERR_HISTEDIT_BUSY);
1128 static const struct got_error *
1129 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1130 char *argv[], struct got_worktree *worktree)
1132 const struct got_error *err = NULL;
1139 return got_error_from_errno("strdup");
1140 return got_pathlist_append(paths, path, NULL);
1143 for (i = 0; i < argc; i++) {
1144 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1147 err = got_pathlist_append(paths, path, NULL);
1157 static const struct got_error *
1158 cmd_update(int argc, char *argv[])
1160 const struct got_error *error = NULL;
1161 struct got_repository *repo = NULL;
1162 struct got_worktree *worktree = NULL;
1163 char *worktree_path = NULL;
1164 struct got_object_id *commit_id = NULL;
1165 char *commit_id_str = NULL;
1166 const char *branch_name = NULL;
1167 struct got_reference *head_ref = NULL;
1168 struct got_pathlist_head paths;
1169 struct got_pathlist_entry *pe;
1170 int ch, did_something = 0;
1174 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1177 branch_name = optarg;
1180 commit_id_str = strdup(optarg);
1181 if (commit_id_str == NULL)
1182 return got_error_from_errno("strdup");
1194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1195 "unveil", NULL) == -1)
1198 worktree_path = getcwd(NULL, 0);
1199 if (worktree_path == NULL) {
1200 error = got_error_from_errno("getcwd");
1203 error = got_worktree_open(&worktree, worktree_path);
1207 error = check_rebase_or_histedit_in_progress(worktree);
1211 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1215 error = apply_unveil(got_repo_get_path(repo), 0,
1216 got_worktree_get_root_path(worktree));
1220 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1224 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1225 got_worktree_get_head_ref_name(worktree), 0);
1228 if (commit_id_str == NULL) {
1229 error = got_ref_resolve(&commit_id, repo, head_ref);
1232 error = got_object_id_str(&commit_id_str, commit_id);
1236 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1237 free(commit_id_str);
1238 commit_id_str = NULL;
1241 error = got_object_id_str(&commit_id_str, commit_id);
1247 struct got_object_id *head_commit_id;
1248 TAILQ_FOREACH(pe, &paths, entry) {
1249 if (pe->path_len == 0)
1251 error = got_error_msg(GOT_ERR_BAD_PATH,
1252 "switching between branches requires that "
1253 "the entire work tree gets updated");
1256 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1259 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1260 free(head_commit_id);
1263 error = check_same_branch(commit_id, head_ref, NULL, repo);
1266 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1270 error = check_linear_ancestry(commit_id,
1271 got_worktree_get_base_commit_id(worktree), repo);
1272 if (error != NULL) {
1273 if (error->code == GOT_ERR_ANCESTRY)
1274 error = got_error(GOT_ERR_BRANCH_MOVED);
1277 error = check_same_branch(commit_id, head_ref, NULL, repo);
1282 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1284 error = got_worktree_set_base_commit_id(worktree, repo,
1290 error = got_worktree_checkout_files(worktree, &paths, repo,
1291 update_progress, &did_something, check_cancelled, NULL);
1296 printf("Updated to commit %s\n", commit_id_str);
1298 printf("Already up-to-date\n");
1300 free(worktree_path);
1301 TAILQ_FOREACH(pe, &paths, entry)
1302 free((char *)pe->path);
1303 got_pathlist_free(&paths);
1305 free(commit_id_str);
1309 static const struct got_error *
1310 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1311 int diff_context, struct got_repository *repo)
1313 const struct got_error *err = NULL;
1314 struct got_tree_object *tree1 = NULL, *tree2;
1315 struct got_object_qid *qid;
1316 char *id_str1 = NULL, *id_str2;
1317 struct got_diff_blob_output_unidiff_arg arg;
1319 err = got_object_open_as_tree(&tree2, repo,
1320 got_object_commit_get_tree_id(commit));
1324 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1326 struct got_commit_object *pcommit;
1328 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1332 err = got_object_open_as_tree(&tree1, repo,
1333 got_object_commit_get_tree_id(pcommit));
1334 got_object_commit_close(pcommit);
1338 err = got_object_id_str(&id_str1, qid->id);
1343 err = got_object_id_str(&id_str2, id);
1347 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1348 arg.diff_context = diff_context;
1349 arg.outfile = stdout;
1350 err = got_diff_tree(tree1, tree2, "", "", repo,
1351 got_diff_blob_output_unidiff, &arg, 1);
1354 got_object_tree_close(tree1);
1355 got_object_tree_close(tree2);
1362 get_datestr(time_t *time, char *datebuf)
1364 char *p, *s = ctime_r(time, datebuf);
1365 p = strchr(s, '\n');
1371 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1373 static const struct got_error *
1374 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1375 struct got_repository *repo, int show_patch, int diff_context,
1376 struct got_reflist_head *refs)
1378 const struct got_error *err = NULL;
1379 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1381 time_t committer_time;
1382 const char *author, *committer;
1383 char *refs_str = NULL;
1384 struct got_reflist_entry *re;
1386 SIMPLEQ_FOREACH(re, refs, entry) {
1389 if (got_object_id_cmp(re->id, id) != 0)
1391 name = got_ref_get_name(re->ref);
1392 if (strcmp(name, GOT_REF_HEAD) == 0)
1394 if (strncmp(name, "refs/", 5) == 0)
1396 if (strncmp(name, "got/", 4) == 0)
1398 if (strncmp(name, "heads/", 6) == 0)
1400 if (strncmp(name, "remotes/", 8) == 0)
1403 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1405 err = got_error_from_errno("asprintf");
1411 err = got_object_id_str(&id_str, id);
1415 printf(GOT_COMMIT_SEP_STR);
1416 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1417 refs_str ? refs_str : "", refs_str ? ")" : "");
1422 printf("from: %s\n", got_object_commit_get_author(commit));
1423 committer_time = got_object_commit_get_committer_time(commit);
1424 datestr = get_datestr(&committer_time, datebuf);
1425 printf("date: %s UTC\n", datestr);
1426 author = got_object_commit_get_author(commit);
1427 committer = got_object_commit_get_committer(commit);
1428 if (strcmp(author, committer) != 0)
1429 printf("via: %s\n", committer);
1430 if (got_object_commit_get_nparents(commit) > 1) {
1431 const struct got_object_id_queue *parent_ids;
1432 struct got_object_qid *qid;
1434 parent_ids = got_object_commit_get_parent_ids(commit);
1435 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1436 err = got_object_id_str(&id_str, qid->id);
1439 printf("parent %d: %s\n", n++, id_str);
1444 logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1445 if (logmsg0 == NULL)
1446 return got_error_from_errno("strdup");
1450 line = strsep(&logmsg, "\n");
1452 printf(" %s\n", line);
1457 err = print_patch(commit, id, diff_context, repo);
1462 if (fflush(stdout) != 0 && err == NULL)
1463 err = got_error_from_errno("fflush");
1467 static const struct got_error *
1468 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1469 char *path, int show_patch, int diff_context, int limit,
1470 int first_parent_traversal, struct got_reflist_head *refs)
1472 const struct got_error *err;
1473 struct got_commit_graph *graph;
1475 err = got_commit_graph_open(&graph, root_id, path,
1476 first_parent_traversal, repo);
1479 err = got_commit_graph_iter_start(graph, root_id, repo);
1483 struct got_commit_object *commit;
1484 struct got_object_id *id;
1486 if (sigint_received || sigpipe_received)
1489 err = got_commit_graph_iter_next(&id, graph);
1491 if (err->code == GOT_ERR_ITER_COMPLETED) {
1495 if (err->code != GOT_ERR_ITER_NEED_MORE)
1497 err = got_commit_graph_fetch_commits(graph, 1, repo);
1506 err = got_object_open_as_commit(&commit, repo, id);
1509 err = print_commit(commit, id, repo, show_patch, diff_context,
1511 got_object_commit_close(commit);
1512 if (err || (limit && --limit == 0))
1516 got_commit_graph_close(graph);
1523 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1524 "[-r repository-path] [path]\n", getprogname());
1528 static const struct got_error *
1529 cmd_log(int argc, char *argv[])
1531 const struct got_error *error;
1532 struct got_repository *repo = NULL;
1533 struct got_worktree *worktree = NULL;
1534 struct got_commit_object *commit = NULL;
1535 struct got_object_id *id = NULL;
1536 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1537 char *start_commit = NULL;
1538 int diff_context = 3, ch;
1539 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1541 struct got_reflist_head refs;
1543 SIMPLEQ_INIT(&refs);
1546 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1552 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1558 start_commit = optarg;
1561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1564 err(1, "-C option %s", errstr);
1567 limit = strtonum(optarg, 1, INT_MAX, &errstr);
1569 err(1, "-l option %s", errstr);
1572 first_parent_traversal = 1;
1575 repo_path = realpath(optarg, NULL);
1576 if (repo_path == NULL)
1577 err(1, "-r option");
1578 got_path_strip_trailing_slashes(repo_path);
1589 cwd = getcwd(NULL, 0);
1591 error = got_error_from_errno("getcwd");
1595 error = got_worktree_open(&worktree, cwd);
1596 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1603 error = got_error_from_errno("strdup");
1606 } else if (argc == 1) {
1608 error = got_worktree_resolve_path(&path, worktree,
1613 path = strdup(argv[0]);
1615 error = got_error_from_errno("strdup");
1622 if (repo_path == NULL) {
1623 repo_path = worktree ?
1624 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1626 if (repo_path == NULL) {
1627 error = got_error_from_errno("strdup");
1631 error = got_repo_open(&repo, repo_path);
1635 error = apply_unveil(got_repo_get_path(repo), 1,
1636 worktree ? got_worktree_get_root_path(worktree) : NULL);
1640 if (start_commit == NULL) {
1641 struct got_reference *head_ref;
1642 error = got_ref_open(&head_ref, repo,
1643 worktree ? got_worktree_get_head_ref_name(worktree)
1647 error = got_ref_resolve(&id, repo, head_ref);
1648 got_ref_close(head_ref);
1651 error = got_object_open_as_commit(&commit, repo, id);
1653 struct got_reference *ref;
1654 error = got_ref_open(&ref, repo, start_commit, 0);
1655 if (error == NULL) {
1657 error = got_ref_resolve(&id, repo, ref);
1661 error = got_object_get_type(&obj_type, repo, id);
1664 if (obj_type == GOT_OBJ_TYPE_TAG) {
1665 struct got_tag_object *tag;
1666 error = got_object_open_as_tag(&tag, repo, id);
1669 if (got_object_tag_get_object_type(tag) !=
1670 GOT_OBJ_TYPE_COMMIT) {
1671 got_object_tag_close(tag);
1672 error = got_error(GOT_ERR_OBJ_TYPE);
1676 id = got_object_id_dup(
1677 got_object_tag_get_object_id(tag));
1679 error = got_error_from_errno(
1680 "got_object_id_dup");
1681 got_object_tag_close(tag);
1684 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1685 error = got_error(GOT_ERR_OBJ_TYPE);
1688 error = got_object_open_as_commit(&commit, repo, id);
1692 if (commit == NULL) {
1693 error = got_repo_match_object_id_prefix(&id,
1694 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1702 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1707 path = in_repo_path;
1710 error = got_ref_list(&refs, repo);
1714 error = print_commits(id, repo, path, show_patch,
1715 diff_context, limit, first_parent_traversal, &refs);
1722 got_worktree_close(worktree);
1724 const struct got_error *repo_error;
1725 repo_error = got_repo_close(repo);
1729 got_ref_list_free(&refs);
1736 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1737 "[object1 object2 | path]\n", getprogname());
1741 struct print_diff_arg {
1742 struct got_repository *repo;
1743 struct got_worktree *worktree;
1750 static const struct got_error *
1751 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1752 const char *path, struct got_object_id *blob_id,
1753 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1755 struct print_diff_arg *a = arg;
1756 const struct got_error *err = NULL;
1757 struct got_blob_object *blob1 = NULL;
1759 char *abspath = NULL, *label1 = NULL;
1762 if (a->diff_staged) {
1763 if (staged_status != GOT_STATUS_MODIFY &&
1764 staged_status != GOT_STATUS_ADD &&
1765 staged_status != GOT_STATUS_DELETE)
1768 if (staged_status == GOT_STATUS_DELETE)
1770 if (status != GOT_STATUS_MODIFY &&
1771 status != GOT_STATUS_ADD &&
1772 status != GOT_STATUS_DELETE &&
1773 status != GOT_STATUS_CONFLICT)
1777 if (!a->header_shown) {
1778 printf("diff %s %s%s\n", a->id_str,
1779 got_worktree_get_root_path(a->worktree),
1780 a->diff_staged ? " (staged changes)" : "");
1781 a->header_shown = 1;
1784 if (a->diff_staged) {
1785 const char *label1 = NULL, *label2 = NULL;
1786 switch (staged_status) {
1787 case GOT_STATUS_MODIFY:
1791 case GOT_STATUS_ADD:
1794 case GOT_STATUS_DELETE:
1798 return got_error(GOT_ERR_FILE_STATUS);
1800 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1801 label1, label2, a->diff_context, a->repo, stdout);
1804 if (staged_status == GOT_STATUS_ADD ||
1805 staged_status == GOT_STATUS_MODIFY) {
1807 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1811 err = got_object_id_str(&id_str, staged_blob_id);
1814 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1815 err = got_error_from_errno("asprintf");
1820 } else if (status != GOT_STATUS_ADD) {
1821 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1826 if (status != GOT_STATUS_DELETE) {
1827 if (asprintf(&abspath, "%s/%s",
1828 got_worktree_get_root_path(a->worktree), path) == -1) {
1829 err = got_error_from_errno("asprintf");
1833 f2 = fopen(abspath, "r");
1835 err = got_error_from_errno2("fopen", abspath);
1838 if (lstat(abspath, &sb) == -1) {
1839 err = got_error_from_errno2("lstat", abspath);
1845 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1846 a->diff_context, stdout);
1849 got_object_blob_close(blob1);
1850 if (f2 && fclose(f2) != 0 && err == NULL)
1851 err = got_error_from_errno("fclose");
1856 static const struct got_error *
1857 cmd_diff(int argc, char *argv[])
1859 const struct got_error *error;
1860 struct got_repository *repo = NULL;
1861 struct got_worktree *worktree = NULL;
1862 char *cwd = NULL, *repo_path = NULL;
1863 struct got_object_id *id1 = NULL, *id2 = NULL;
1864 const char *id_str1 = NULL, *id_str2 = NULL;
1865 char *label1 = NULL, *label2 = NULL;
1867 int diff_context = 3, diff_staged = 0, ch;
1872 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1877 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1880 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1882 err(1, "-C option %s", errstr);
1885 repo_path = realpath(optarg, NULL);
1886 if (repo_path == NULL)
1887 err(1, "-r option");
1888 got_path_strip_trailing_slashes(repo_path);
1902 cwd = getcwd(NULL, 0);
1904 error = got_error_from_errno("getcwd");
1907 error = got_worktree_open(&worktree, cwd);
1908 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1911 if (worktree == NULL) {
1912 error = got_error(GOT_ERR_NOT_WORKTREE);
1917 "-r option can't be used when diffing a work tree");
1918 repo_path = strdup(got_worktree_get_repo_path(worktree));
1919 if (repo_path == NULL) {
1920 error = got_error_from_errno("strdup");
1924 error = got_worktree_resolve_path(&path, worktree,
1931 error = got_error_from_errno("strdup");
1935 } else if (argc == 2) {
1937 errx(1, "-s option can't be used when diffing "
1938 "objects in repository");
1941 if (worktree && repo_path == NULL) {
1943 strdup(got_worktree_get_repo_path(worktree));
1944 if (repo_path == NULL) {
1945 error = got_error_from_errno("strdup");
1952 if (repo_path == NULL) {
1953 repo_path = getcwd(NULL, 0);
1954 if (repo_path == NULL)
1955 return got_error_from_errno("getcwd");
1958 error = got_repo_open(&repo, repo_path);
1963 error = apply_unveil(got_repo_get_path(repo), 1,
1964 worktree ? got_worktree_get_root_path(worktree) : NULL);
1969 struct print_diff_arg arg;
1970 struct got_pathlist_head paths;
1975 error = got_object_id_str(&id_str,
1976 got_worktree_get_base_commit_id(worktree));
1980 arg.worktree = worktree;
1981 arg.diff_context = diff_context;
1982 arg.id_str = id_str;
1983 arg.header_shown = 0;
1984 arg.diff_staged = diff_staged;
1986 error = got_pathlist_append(&paths, path, NULL);
1990 error = got_worktree_status(worktree, &paths, repo, print_diff,
1991 &arg, check_cancelled, NULL);
1993 got_pathlist_free(&paths);
1997 error = got_repo_match_object_id_prefix(&id1, id_str1,
1998 GOT_OBJ_TYPE_ANY, repo);
2000 struct got_reference *ref;
2001 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2003 error = got_ref_open(&ref, repo, id_str1, 0);
2006 label1 = strdup(got_ref_get_name(ref));
2007 if (label1 == NULL) {
2008 error = got_error_from_errno("strdup");
2011 error = got_ref_resolve(&id1, repo, ref);
2016 error = got_object_id_str(&label1, id1);
2017 if (label1 == NULL) {
2018 error = got_error_from_errno("strdup");
2023 error = got_repo_match_object_id_prefix(&id2, id_str2,
2024 GOT_OBJ_TYPE_ANY, repo);
2026 struct got_reference *ref;
2027 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
2029 error = got_ref_open(&ref, repo, id_str2, 0);
2032 label2 = strdup(got_ref_get_name(ref));
2033 if (label2 == NULL) {
2034 error = got_error_from_errno("strdup");
2037 error = got_ref_resolve(&id2, repo, ref);
2042 error = got_object_id_str(&label2, id2);
2043 if (label2 == NULL) {
2044 error = got_error_from_errno("strdup");
2049 error = got_object_get_type(&type1, repo, id1);
2053 error = got_object_get_type(&type2, repo, id2);
2057 if (type1 != type2) {
2058 error = got_error(GOT_ERR_OBJ_TYPE);
2063 case GOT_OBJ_TYPE_BLOB:
2064 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2065 diff_context, repo, stdout);
2067 case GOT_OBJ_TYPE_TREE:
2068 error = got_diff_objects_as_trees(id1, id2, "", "",
2069 diff_context, repo, stdout);
2071 case GOT_OBJ_TYPE_COMMIT:
2072 printf("diff %s %s\n", label1, label2);
2073 error = got_diff_objects_as_commits(id1, id2, diff_context,
2077 error = got_error(GOT_ERR_OBJ_TYPE);
2087 got_worktree_close(worktree);
2089 const struct got_error *repo_error;
2090 repo_error = got_repo_close(repo);
2101 "usage: %s blame [-c commit] [-r repository-path] path\n",
2106 static const struct got_error *
2107 cmd_blame(int argc, char *argv[])
2109 const struct got_error *error;
2110 struct got_repository *repo = NULL;
2111 struct got_worktree *worktree = NULL;
2112 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2113 struct got_object_id *commit_id = NULL;
2114 char *commit_id_str = NULL;
2118 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2123 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2126 commit_id_str = optarg;
2129 repo_path = realpath(optarg, NULL);
2130 if (repo_path == NULL)
2131 err(1, "-r option");
2132 got_path_strip_trailing_slashes(repo_path);
2148 cwd = getcwd(NULL, 0);
2150 error = got_error_from_errno("getcwd");
2153 if (repo_path == NULL) {
2154 error = got_worktree_open(&worktree, cwd);
2155 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2161 strdup(got_worktree_get_repo_path(worktree));
2162 if (repo_path == NULL)
2163 error = got_error_from_errno("strdup");
2167 repo_path = strdup(cwd);
2168 if (repo_path == NULL) {
2169 error = got_error_from_errno("strdup");
2175 error = got_repo_open(&repo, repo_path);
2179 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2184 const char *prefix = got_worktree_get_path_prefix(worktree);
2185 char *p, *worktree_subdir = cwd +
2186 strlen(got_worktree_get_root_path(worktree));
2187 if (asprintf(&p, "%s%s%s%s%s",
2188 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2189 worktree_subdir, worktree_subdir[0] ? "/" : "",
2191 error = got_error_from_errno("asprintf");
2194 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2197 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2202 if (commit_id_str == NULL) {
2203 struct got_reference *head_ref;
2204 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2207 error = got_ref_resolve(&commit_id, repo, head_ref);
2208 got_ref_close(head_ref);
2212 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2217 error = got_blame(in_repo_path, commit_id, repo, stdout);
2224 got_worktree_close(worktree);
2226 const struct got_error *repo_error;
2227 repo_error = got_repo_close(repo);
2238 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2244 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2245 const char *root_path)
2247 int is_root_path = (strcmp(path, root_path) == 0);
2249 path += strlen(root_path);
2250 while (path[0] == '/')
2253 printf("%s%s%s%s%s\n", id ? id : "", path,
2254 is_root_path ? "" : "/", te->name,
2255 S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2258 static const struct got_error *
2259 print_tree(const char *path, struct got_object_id *commit_id,
2260 int show_ids, int recurse, const char *root_path,
2261 struct got_repository *repo)
2263 const struct got_error *err = NULL;
2264 struct got_object_id *tree_id = NULL;
2265 struct got_tree_object *tree = NULL;
2266 const struct got_tree_entries *entries;
2267 struct got_tree_entry *te;
2269 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2273 err = got_object_open_as_tree(&tree, repo, tree_id);
2276 entries = got_object_tree_get_entries(tree);
2277 te = SIMPLEQ_FIRST(&entries->head);
2281 if (sigint_received || sigpipe_received)
2286 err = got_object_id_str(&id_str, te->id);
2289 if (asprintf(&id, "%s ", id_str) == -1) {
2290 err = got_error_from_errno("asprintf");
2296 print_entry(te, id, path, root_path);
2299 if (recurse && S_ISDIR(te->mode)) {
2301 if (asprintf(&child_path, "%s%s%s", path,
2302 path[0] == '/' && path[1] == '\0' ? "" : "/",
2304 err = got_error_from_errno("asprintf");
2307 err = print_tree(child_path, commit_id, show_ids, 1,
2314 te = SIMPLEQ_NEXT(te, entry);
2318 got_object_tree_close(tree);
2323 static const struct got_error *
2324 cmd_tree(int argc, char *argv[])
2326 const struct got_error *error;
2327 struct got_repository *repo = NULL;
2328 struct got_worktree *worktree = NULL;
2330 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2331 struct got_object_id *commit_id = NULL;
2332 char *commit_id_str = NULL;
2333 int show_ids = 0, recurse = 0;
2337 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2342 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2345 commit_id_str = optarg;
2348 repo_path = realpath(optarg, NULL);
2349 if (repo_path == NULL)
2350 err(1, "-r option");
2351 got_path_strip_trailing_slashes(repo_path);
2375 cwd = getcwd(NULL, 0);
2377 error = got_error_from_errno("getcwd");
2380 if (repo_path == NULL) {
2381 error = got_worktree_open(&worktree, cwd);
2382 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2388 strdup(got_worktree_get_repo_path(worktree));
2389 if (repo_path == NULL)
2390 error = got_error_from_errno("strdup");
2394 repo_path = strdup(cwd);
2395 if (repo_path == NULL) {
2396 error = got_error_from_errno("strdup");
2402 error = got_repo_open(&repo, repo_path);
2406 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2412 char *p, *worktree_subdir = cwd +
2413 strlen(got_worktree_get_root_path(worktree));
2414 if (asprintf(&p, "%s/%s",
2415 got_worktree_get_path_prefix(worktree),
2416 worktree_subdir) == -1) {
2417 error = got_error_from_errno("asprintf");
2420 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2427 if (in_repo_path == NULL) {
2428 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2433 if (commit_id_str == NULL) {
2434 struct got_reference *head_ref;
2435 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2438 error = got_ref_resolve(&commit_id, repo, head_ref);
2439 got_ref_close(head_ref);
2443 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2448 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2449 in_repo_path, repo);
2456 got_worktree_close(worktree);
2458 const struct got_error *repo_error;
2459 repo_error = got_repo_close(repo);
2469 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2473 static const struct got_error *
2474 print_status(void *arg, unsigned char status, unsigned char staged_status,
2475 const char *path, struct got_object_id *blob_id,
2476 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2478 if (status == staged_status && (status == GOT_STATUS_DELETE))
2479 status = GOT_STATUS_NO_CHANGE;
2480 printf("%c%c %s\n", status, staged_status, path);
2484 static const struct got_error *
2485 cmd_status(int argc, char *argv[])
2487 const struct got_error *error = NULL;
2488 struct got_repository *repo = NULL;
2489 struct got_worktree *worktree = NULL;
2491 struct got_pathlist_head paths;
2492 struct got_pathlist_entry *pe;
2497 while ((ch = getopt(argc, argv, "")) != -1) {
2509 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2513 cwd = getcwd(NULL, 0);
2515 error = got_error_from_errno("getcwd");
2519 error = got_worktree_open(&worktree, cwd);
2523 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2527 error = apply_unveil(got_repo_get_path(repo), 1,
2528 got_worktree_get_root_path(worktree));
2532 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2536 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2537 check_cancelled, NULL);
2539 TAILQ_FOREACH(pe, &paths, entry)
2540 free((char *)pe->path);
2541 got_pathlist_free(&paths);
2550 "usage: %s ref [-r repository] -l | -d name | name target\n",
2555 static const struct got_error *
2556 list_refs(struct got_repository *repo)
2558 static const struct got_error *err = NULL;
2559 struct got_reflist_head refs;
2560 struct got_reflist_entry *re;
2562 SIMPLEQ_INIT(&refs);
2563 err = got_ref_list(&refs, repo);
2567 SIMPLEQ_FOREACH(re, &refs, entry) {
2569 refstr = got_ref_to_str(re->ref);
2571 return got_error_from_errno("got_ref_to_str");
2572 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2576 got_ref_list_free(&refs);
2580 static const struct got_error *
2581 delete_ref(struct got_repository *repo, const char *refname)
2583 const struct got_error *err = NULL;
2584 struct got_reference *ref;
2586 err = got_ref_open(&ref, repo, refname, 0);
2590 err = got_ref_delete(ref, repo);
2595 static const struct got_error *
2596 add_ref(struct got_repository *repo, const char *refname, const char *target)
2598 const struct got_error *err = NULL;
2599 struct got_object_id *id;
2600 struct got_reference *ref = NULL;
2603 * Don't let the user create a reference named '-'.
2604 * While technically a valid reference name, this case is usually
2605 * an unintended typo.
2607 if (refname[0] == '-' && refname[1] == '\0')
2608 return got_error(GOT_ERR_BAD_REF_NAME);
2610 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2613 struct got_reference *target_ref;
2615 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2617 err = got_ref_open(&target_ref, repo, target, 0);
2620 err = got_ref_resolve(&id, repo, target_ref);
2621 got_ref_close(target_ref);
2626 err = got_ref_alloc(&ref, refname, id);
2630 err = got_ref_write(ref, repo);
2638 static const struct got_error *
2639 cmd_ref(int argc, char *argv[])
2641 const struct got_error *error = NULL;
2642 struct got_repository *repo = NULL;
2643 struct got_worktree *worktree = NULL;
2644 char *cwd = NULL, *repo_path = NULL;
2645 int ch, do_list = 0;
2646 const char *delref = NULL;
2648 /* TODO: Add -s option for adding symbolic references. */
2649 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2655 repo_path = realpath(optarg, NULL);
2656 if (repo_path == NULL)
2657 err(1, "-r option");
2658 got_path_strip_trailing_slashes(repo_path);
2669 if (do_list && delref)
2670 errx(1, "-l and -d options are mutually exclusive\n");
2675 if (do_list || delref) {
2678 } else if (argc != 2)
2683 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2687 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2688 "sendfd unveil", NULL) == -1)
2692 cwd = getcwd(NULL, 0);
2694 error = got_error_from_errno("getcwd");
2698 if (repo_path == NULL) {
2699 error = got_worktree_open(&worktree, cwd);
2700 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2706 strdup(got_worktree_get_repo_path(worktree));
2707 if (repo_path == NULL)
2708 error = got_error_from_errno("strdup");
2712 repo_path = strdup(cwd);
2713 if (repo_path == NULL) {
2714 error = got_error_from_errno("strdup");
2720 error = got_repo_open(&repo, repo_path);
2724 error = apply_unveil(got_repo_get_path(repo), do_list,
2725 worktree ? got_worktree_get_root_path(worktree) : NULL);
2730 error = list_refs(repo);
2732 error = delete_ref(repo, delref);
2734 error = add_ref(repo, argv[0], argv[1]);
2737 got_repo_close(repo);
2739 got_worktree_close(worktree);
2749 "usage: %s branch [-r repository] -l | -d name | "
2750 "name [base-branch]\n", getprogname());
2754 static const struct got_error *
2755 list_branches(struct got_repository *repo, struct got_worktree *worktree)
2757 static const struct got_error *err = NULL;
2758 struct got_reflist_head refs;
2759 struct got_reflist_entry *re;
2761 SIMPLEQ_INIT(&refs);
2763 err = got_ref_list(&refs, repo);
2767 SIMPLEQ_FOREACH(re, &refs, entry) {
2768 const char *refname, *marker = " ";
2770 refname = got_ref_get_name(re->ref);
2771 if (strncmp(refname, "refs/heads/", 11) != 0)
2773 if (worktree && strcmp(refname,
2774 got_worktree_get_head_ref_name(worktree)) == 0) {
2775 struct got_object_id *id = NULL;
2776 err = got_ref_resolve(&id, repo, re->ref);
2779 if (got_object_id_cmp(id,
2780 got_worktree_get_base_commit_id(worktree)) == 0)
2787 refstr = got_ref_to_str(re->ref);
2789 return got_error_from_errno("got_ref_to_str");
2790 printf("%s%s: %s\n", marker, refname, refstr);
2794 got_ref_list_free(&refs);
2798 static const struct got_error *
2799 delete_branch(struct got_repository *repo, const char *branch_name)
2801 const struct got_error *err = NULL;
2802 struct got_reference *ref;
2805 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2806 return got_error_from_errno("asprintf");
2808 err = got_ref_open(&ref, repo, refname, 0);
2812 err = got_ref_delete(ref, repo);
2819 static const struct got_error *
2820 add_branch(struct got_repository *repo, const char *branch_name,
2821 const char *base_branch)
2823 const struct got_error *err = NULL;
2824 struct got_object_id *id = NULL;
2825 struct got_reference *ref = NULL;
2826 char *base_refname = NULL, *refname = NULL;
2827 struct got_reference *base_ref;
2830 * Don't let the user create a branch named '-'.
2831 * While technically a valid reference name, this case is usually
2832 * an unintended typo.
2834 if (branch_name[0] == '-' && branch_name[1] == '\0')
2835 return got_error(GOT_ERR_BAD_REF_NAME);
2837 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2838 base_refname = strdup(GOT_REF_HEAD);
2839 if (base_refname == NULL)
2840 return got_error_from_errno("strdup");
2841 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2842 return got_error_from_errno("asprintf");
2844 err = got_ref_open(&base_ref, repo, base_refname, 0);
2847 err = got_ref_resolve(&id, repo, base_ref);
2848 got_ref_close(base_ref);
2852 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2853 err = got_error_from_errno("asprintf");
2857 err = got_ref_open(&ref, repo, refname, 0);
2859 err = got_error(GOT_ERR_BRANCH_EXISTS);
2861 } else if (err->code != GOT_ERR_NOT_REF)
2864 err = got_ref_alloc(&ref, refname, id);
2868 err = got_ref_write(ref, repo);
2878 static const struct got_error *
2879 cmd_branch(int argc, char *argv[])
2881 const struct got_error *error = NULL;
2882 struct got_repository *repo = NULL;
2883 struct got_worktree *worktree = NULL;
2884 char *cwd = NULL, *repo_path = NULL;
2885 int ch, do_list = 0;
2886 const char *delref = NULL;
2888 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2894 repo_path = realpath(optarg, NULL);
2895 if (repo_path == NULL)
2896 err(1, "-r option");
2897 got_path_strip_trailing_slashes(repo_path);
2908 if (do_list && delref)
2909 errx(1, "-l and -d options are mutually exclusive\n");
2914 if (do_list || delref) {
2917 } else if (argc < 1 || argc > 2)
2922 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2926 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2927 "sendfd unveil", NULL) == -1)
2931 cwd = getcwd(NULL, 0);
2933 error = got_error_from_errno("getcwd");
2937 if (repo_path == NULL) {
2938 error = got_worktree_open(&worktree, cwd);
2939 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2945 strdup(got_worktree_get_repo_path(worktree));
2946 if (repo_path == NULL)
2947 error = got_error_from_errno("strdup");
2951 repo_path = strdup(cwd);
2952 if (repo_path == NULL) {
2953 error = got_error_from_errno("strdup");
2959 error = got_repo_open(&repo, repo_path);
2963 error = apply_unveil(got_repo_get_path(repo), do_list,
2964 worktree ? got_worktree_get_root_path(worktree) : NULL);
2969 error = list_branches(repo, worktree);
2971 error = delete_branch(repo, delref);
2973 const char *base_branch;
2975 base_branch = worktree ?
2976 got_worktree_get_head_ref_name(worktree) :
2978 if (strncmp(base_branch, "refs/heads/", 11) == 0)
2981 base_branch = argv[1];
2982 error = add_branch(repo, argv[0], base_branch);
2986 got_repo_close(repo);
2988 got_worktree_close(worktree);
2997 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3001 static const struct got_error *
3002 cmd_add(int argc, char *argv[])
3004 const struct got_error *error = NULL;
3005 struct got_repository *repo = NULL;
3006 struct got_worktree *worktree = NULL;
3008 struct got_pathlist_head paths;
3009 struct got_pathlist_entry *pe;
3014 while ((ch = getopt(argc, argv, "")) != -1) {
3026 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3033 cwd = getcwd(NULL, 0);
3035 error = got_error_from_errno("getcwd");
3039 error = got_worktree_open(&worktree, cwd);
3043 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3047 error = apply_unveil(got_repo_get_path(repo), 1,
3048 got_worktree_get_root_path(worktree));
3052 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3056 error = got_worktree_schedule_add(worktree, &paths, print_status,
3060 got_repo_close(repo);
3062 got_worktree_close(worktree);
3063 TAILQ_FOREACH(pe, &paths, entry)
3064 free((char *)pe->path);
3065 got_pathlist_free(&paths);
3073 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3077 static const struct got_error *
3078 cmd_remove(int argc, char *argv[])
3080 const struct got_error *error = NULL;
3081 struct got_worktree *worktree = NULL;
3082 struct got_repository *repo = NULL;
3084 struct got_pathlist_head paths;
3085 struct got_pathlist_entry *pe;
3086 int ch, delete_local_mods = 0;
3090 while ((ch = getopt(argc, argv, "f")) != -1) {
3093 delete_local_mods = 1;
3105 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3112 cwd = getcwd(NULL, 0);
3114 error = got_error_from_errno("getcwd");
3117 error = got_worktree_open(&worktree, cwd);
3121 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3125 error = apply_unveil(got_repo_get_path(repo), 1,
3126 got_worktree_get_root_path(worktree));
3130 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3134 error = got_worktree_schedule_delete(worktree, &paths,
3135 delete_local_mods, print_status, NULL, repo);
3140 got_repo_close(repo);
3142 got_worktree_close(worktree);
3143 TAILQ_FOREACH(pe, &paths, entry)
3144 free((char *)pe->path);
3145 got_pathlist_free(&paths);
3153 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3154 "path ...\n", getprogname());
3158 static const struct got_error *
3159 revert_progress(void *arg, unsigned char status, const char *path)
3161 while (path[0] == '/')
3163 printf("%c %s\n", status, path);
3167 struct choose_patch_arg {
3168 FILE *patch_script_file;
3172 static const struct got_error *
3173 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3174 int nchanges, const char *action)
3177 size_t linesize = 0;
3181 case GOT_STATUS_ADD:
3182 printf("A %s\n%s this addition? [y/n] ", path, action);
3184 case GOT_STATUS_DELETE:
3185 printf("D %s\n%s this deletion? [y/n] ", path, action);
3187 case GOT_STATUS_MODIFY:
3188 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3189 return got_error_from_errno("fseek");
3190 printf(GOT_COMMIT_SEP_STR);
3191 while ((linelen = getline(&line, &linesize, patch_file) != -1))
3193 if (ferror(patch_file))
3194 return got_error_from_errno("getline");
3195 printf(GOT_COMMIT_SEP_STR);
3196 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3197 path, n, nchanges, action);
3200 return got_error_path(path, GOT_ERR_FILE_STATUS);
3206 static const struct got_error *
3207 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3208 FILE *patch_file, int n, int nchanges)
3210 const struct got_error *err = NULL;
3212 size_t linesize = 0;
3215 struct choose_patch_arg *a = arg;
3217 *choice = GOT_PATCH_CHOICE_NONE;
3219 if (a->patch_script_file) {
3221 err = show_change(status, path, patch_file, n, nchanges,
3225 linelen = getline(&line, &linesize, a->patch_script_file);
3226 if (linelen == -1) {
3227 if (ferror(a->patch_script_file))
3228 return got_error_from_errno("getline");
3231 nl = strchr(line, '\n');
3234 if (strcmp(line, "y") == 0) {
3235 *choice = GOT_PATCH_CHOICE_YES;
3237 } else if (strcmp(line, "n") == 0) {
3238 *choice = GOT_PATCH_CHOICE_NO;
3240 } else if (strcmp(line, "q") == 0 &&
3241 status == GOT_STATUS_MODIFY) {
3242 *choice = GOT_PATCH_CHOICE_QUIT;
3245 printf("invalid response '%s'\n", line);
3250 while (resp != 'y' && resp != 'n' && resp != 'q') {
3251 err = show_change(status, path, patch_file, n, nchanges,
3258 if (status == GOT_STATUS_MODIFY) {
3259 if (resp != 'y' && resp != 'n' && resp != 'q') {
3260 printf("invalid response '%c'\n", resp);
3263 } else if (resp != 'y' && resp != 'n') {
3264 printf("invalid response '%c'\n", resp);
3270 *choice = GOT_PATCH_CHOICE_YES;
3271 else if (resp == 'n')
3272 *choice = GOT_PATCH_CHOICE_NO;
3273 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3274 *choice = GOT_PATCH_CHOICE_QUIT;
3280 static const struct got_error *
3281 cmd_revert(int argc, char *argv[])
3283 const struct got_error *error = NULL;
3284 struct got_worktree *worktree = NULL;
3285 struct got_repository *repo = NULL;
3286 char *cwd = NULL, *path = NULL;
3287 struct got_pathlist_head paths;
3288 struct got_pathlist_entry *pe;
3289 int ch, can_recurse = 0, pflag = 0;
3290 FILE *patch_script_file = NULL;
3291 const char *patch_script_path = NULL;
3292 struct choose_patch_arg cpa;
3296 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3302 patch_script_path = optarg;
3317 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3318 "unveil", NULL) == -1)
3323 if (patch_script_path && !pflag)
3324 errx(1, "-F option can only be used together with -p option");
3326 cwd = getcwd(NULL, 0);
3328 error = got_error_from_errno("getcwd");
3331 error = got_worktree_open(&worktree, cwd);
3335 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3339 if (patch_script_path) {
3340 patch_script_file = fopen(patch_script_path, "r");
3341 if (patch_script_file == NULL) {
3342 error = got_error_from_errno2("fopen",
3347 error = apply_unveil(got_repo_get_path(repo), 1,
3348 got_worktree_get_root_path(worktree));
3352 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3359 TAILQ_FOREACH(pe, &paths, entry) {
3360 if (asprintf(&ondisk_path, "%s/%s",
3361 got_worktree_get_root_path(worktree),
3363 error = got_error_from_errno("asprintf");
3366 if (lstat(ondisk_path, &sb) == -1) {
3367 if (errno == ENOENT) {
3371 error = got_error_from_errno2("lstat",
3377 if (S_ISDIR(sb.st_mode)) {
3378 error = got_error_msg(GOT_ERR_BAD_PATH,
3379 "reverting directories requires -R option");
3385 cpa.patch_script_file = patch_script_file;
3386 cpa.action = "revert";
3387 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3388 pflag ? choose_patch : NULL, &cpa, repo);
3392 if (patch_script_file && fclose(patch_script_file) == EOF &&
3394 error = got_error_from_errno2("fclose", patch_script_path);
3396 got_repo_close(repo);
3398 got_worktree_close(worktree);
3407 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3412 struct collect_commit_logmsg_arg {
3413 const char *cmdline_log;
3415 const char *worktree_path;
3416 const char *branch_name;
3417 const char *repo_path;
3422 static const struct got_error *
3423 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3426 char *initial_content = NULL;
3427 struct got_pathlist_entry *pe;
3428 const struct got_error *err = NULL;
3429 char *template = NULL;
3430 struct collect_commit_logmsg_arg *a = arg;
3434 /* if a message was specified on the command line, just use it */
3435 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3436 len = strlen(a->cmdline_log) + 1;
3437 *logmsg = malloc(len + 1);
3438 if (*logmsg == NULL)
3439 return got_error_from_errno("malloc");
3440 strlcpy(*logmsg, a->cmdline_log, len);
3444 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3445 return got_error_from_errno("asprintf");
3447 if (asprintf(&initial_content,
3448 "\n# changes to be committed on branch %s:\n",
3449 a->branch_name) == -1)
3450 return got_error_from_errno("asprintf");
3452 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3456 dprintf(fd, initial_content);
3458 TAILQ_FOREACH(pe, commitable_paths, entry) {
3459 struct got_commitable *ct = pe->data;
3460 dprintf(fd, "# %c %s\n",
3461 got_commitable_get_status(ct),
3462 got_commitable_get_path(ct));
3466 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3468 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3469 unlink(a->logmsg_path);
3470 free(a->logmsg_path);
3471 a->logmsg_path = NULL;
3473 free(initial_content);
3476 /* Editor is done; we can now apply unveil(2) */
3478 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3487 static const struct got_error *
3488 cmd_commit(int argc, char *argv[])
3490 const struct got_error *error = NULL;
3491 struct got_worktree *worktree = NULL;
3492 struct got_repository *repo = NULL;
3493 char *cwd = NULL, *id_str = NULL;
3494 struct got_object_id *id = NULL;
3495 const char *logmsg = NULL;
3497 struct collect_commit_logmsg_arg cl_arg;
3498 char *editor = NULL;
3499 int ch, rebase_in_progress, histedit_in_progress;
3500 struct got_pathlist_head paths;
3503 cl_arg.logmsg_path = NULL;
3505 while ((ch = getopt(argc, argv, "m:")) != -1) {
3520 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3521 "unveil", NULL) == -1)
3524 error = get_author(&author);
3528 cwd = getcwd(NULL, 0);
3530 error = got_error_from_errno("getcwd");
3533 error = got_worktree_open(&worktree, cwd);
3537 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3540 if (rebase_in_progress) {
3541 error = got_error(GOT_ERR_REBASING);
3545 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3550 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3555 * unveil(2) traverses exec(2); if an editor is used we have
3556 * to apply unveil after the log message has been written.
3558 if (logmsg == NULL || strlen(logmsg) == 0)
3559 error = get_editor(&editor);
3561 error = apply_unveil(got_repo_get_path(repo), 0,
3562 got_worktree_get_root_path(worktree));
3566 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3570 cl_arg.editor = editor;
3571 cl_arg.cmdline_log = logmsg;
3572 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3573 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3574 if (!histedit_in_progress) {
3575 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3576 error = got_error(GOT_ERR_COMMIT_BRANCH);
3579 cl_arg.branch_name += 11;
3581 cl_arg.repo_path = got_repo_get_path(repo);
3582 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3583 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3585 if (cl_arg.logmsg_path)
3586 fprintf(stderr, "%s: log message preserved in %s\n",
3587 getprogname(), cl_arg.logmsg_path);
3591 if (cl_arg.logmsg_path)
3592 unlink(cl_arg.logmsg_path);
3594 error = got_object_id_str(&id_str, id);
3597 printf("Created commit %s\n", id_str);
3599 free(cl_arg.logmsg_path);
3601 got_repo_close(repo);
3603 got_worktree_close(worktree);
3611 usage_cherrypick(void)
3613 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3617 static const struct got_error *
3618 cmd_cherrypick(int argc, char *argv[])
3620 const struct got_error *error = NULL;
3621 struct got_worktree *worktree = NULL;
3622 struct got_repository *repo = NULL;
3623 char *cwd = NULL, *commit_id_str = NULL;
3624 struct got_object_id *commit_id = NULL;
3625 struct got_commit_object *commit = NULL;
3626 struct got_object_qid *pid;
3627 struct got_reference *head_ref = NULL;
3628 int ch, did_something = 0;
3630 while ((ch = getopt(argc, argv, "")) != -1) {
3642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3643 "unveil", NULL) == -1)
3649 cwd = getcwd(NULL, 0);
3651 error = got_error_from_errno("getcwd");
3654 error = got_worktree_open(&worktree, cwd);
3658 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3662 error = apply_unveil(got_repo_get_path(repo), 0,
3663 got_worktree_get_root_path(worktree));
3667 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3668 GOT_OBJ_TYPE_COMMIT, repo);
3669 if (error != NULL) {
3670 struct got_reference *ref;
3671 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3673 error = got_ref_open(&ref, repo, argv[0], 0);
3676 error = got_ref_resolve(&commit_id, repo, ref);
3681 error = got_object_id_str(&commit_id_str, commit_id);
3685 error = got_ref_open(&head_ref, repo,
3686 got_worktree_get_head_ref_name(worktree), 0);
3690 error = check_same_branch(commit_id, head_ref, NULL, repo);
3692 if (error->code != GOT_ERR_ANCESTRY)
3696 error = got_error(GOT_ERR_SAME_BRANCH);
3700 error = got_object_open_as_commit(&commit, repo, commit_id);
3703 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3704 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3705 commit_id, repo, update_progress, &did_something, check_cancelled,
3711 printf("Merged commit %s\n", commit_id_str);
3714 got_object_commit_close(commit);
3715 free(commit_id_str);
3717 got_ref_close(head_ref);
3719 got_worktree_close(worktree);
3721 got_repo_close(repo);
3728 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3732 static const struct got_error *
3733 cmd_backout(int argc, char *argv[])
3735 const struct got_error *error = NULL;
3736 struct got_worktree *worktree = NULL;
3737 struct got_repository *repo = NULL;
3738 char *cwd = NULL, *commit_id_str = NULL;
3739 struct got_object_id *commit_id = NULL;
3740 struct got_commit_object *commit = NULL;
3741 struct got_object_qid *pid;
3742 struct got_reference *head_ref = NULL;
3743 int ch, did_something = 0;
3745 while ((ch = getopt(argc, argv, "")) != -1) {
3757 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3758 "unveil", NULL) == -1)
3764 cwd = getcwd(NULL, 0);
3766 error = got_error_from_errno("getcwd");
3769 error = got_worktree_open(&worktree, cwd);
3773 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3777 error = apply_unveil(got_repo_get_path(repo), 0,
3778 got_worktree_get_root_path(worktree));
3782 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3783 GOT_OBJ_TYPE_COMMIT, repo);
3784 if (error != NULL) {
3785 struct got_reference *ref;
3786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3788 error = got_ref_open(&ref, repo, argv[0], 0);
3791 error = got_ref_resolve(&commit_id, repo, ref);
3796 error = got_object_id_str(&commit_id_str, commit_id);
3800 error = got_ref_open(&head_ref, repo,
3801 got_worktree_get_head_ref_name(worktree), 0);
3805 error = check_same_branch(commit_id, head_ref, NULL, repo);
3809 error = got_object_open_as_commit(&commit, repo, commit_id);
3812 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3814 error = got_error(GOT_ERR_ROOT_COMMIT);
3818 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3819 update_progress, &did_something, check_cancelled, NULL);
3824 printf("Backed out commit %s\n", commit_id_str);
3827 got_object_commit_close(commit);
3828 free(commit_id_str);
3830 got_ref_close(head_ref);
3832 got_worktree_close(worktree);
3834 got_repo_close(repo);
3841 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3847 trim_logmsg(char *logmsg, int limit)
3852 len = strlen(logmsg);
3856 nl = strchr(logmsg, '\n');
3861 static const struct got_error *
3862 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3864 const char *logmsg0 = NULL;
3866 logmsg0 = got_object_commit_get_logmsg(commit);
3868 while (isspace((unsigned char)logmsg0[0]))
3871 *logmsg = strdup(logmsg0);
3872 if (*logmsg == NULL)
3873 return got_error_from_errno("strdup");
3875 trim_logmsg(*logmsg, limit);
3879 static const struct got_error *
3880 show_rebase_progress(struct got_commit_object *commit,
3881 struct got_object_id *old_id, struct got_object_id *new_id)
3883 const struct got_error *err;
3884 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3886 err = got_object_id_str(&old_id_str, old_id);
3891 err = got_object_id_str(&new_id_str, new_id);
3896 old_id_str[12] = '\0';
3898 new_id_str[12] = '\0';
3900 err = get_short_logmsg(&logmsg, 42, commit);
3904 printf("%s -> %s: %s\n", old_id_str,
3905 new_id_str ? new_id_str : "no-op change", logmsg);
3912 static const struct got_error *
3913 rebase_progress(void *arg, unsigned char status, const char *path)
3915 unsigned char *rebase_status = arg;
3917 while (path[0] == '/')
3919 printf("%c %s\n", status, path);
3921 if (*rebase_status == GOT_STATUS_CONFLICT)
3923 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3924 *rebase_status = status;
3928 static const struct got_error *
3929 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3930 struct got_reference *branch, struct got_reference *new_base_branch,
3931 struct got_reference *tmp_branch, struct got_repository *repo)
3933 printf("Switching work tree to %s\n", got_ref_get_name(branch));
3934 return got_worktree_rebase_complete(worktree, fileindex,
3935 new_base_branch, tmp_branch, branch, repo);
3938 static const struct got_error *
3939 rebase_commit(struct got_pathlist_head *merged_paths,
3940 struct got_worktree *worktree, struct got_fileindex *fileindex,
3941 struct got_reference *tmp_branch,
3942 struct got_object_id *commit_id, struct got_repository *repo)
3944 const struct got_error *error;
3945 struct got_commit_object *commit;
3946 struct got_object_id *new_commit_id;
3948 error = got_object_open_as_commit(&commit, repo, commit_id);
3952 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3953 worktree, fileindex, tmp_branch, commit, commit_id, repo);
3955 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3957 error = show_rebase_progress(commit, commit_id, NULL);
3959 error = show_rebase_progress(commit, commit_id, new_commit_id);
3960 free(new_commit_id);
3963 got_object_commit_close(commit);
3967 struct check_path_prefix_arg {
3968 const char *path_prefix;
3973 static const struct got_error *
3974 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3975 struct got_blob_object *blob2, struct got_object_id *id1,
3976 struct got_object_id *id2, const char *path1, const char *path2,
3977 struct got_repository *repo)
3979 struct check_path_prefix_arg *a = arg;
3981 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3982 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3983 return got_error(a->errcode);
3988 static const struct got_error *
3989 check_path_prefix(struct got_object_id *parent_id,
3990 struct got_object_id *commit_id, const char *path_prefix,
3991 int errcode, struct got_repository *repo)
3993 const struct got_error *err;
3994 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3995 struct got_commit_object *commit = NULL, *parent_commit = NULL;
3996 struct check_path_prefix_arg cpp_arg;
3998 if (got_path_is_root_dir(path_prefix))
4001 err = got_object_open_as_commit(&commit, repo, commit_id);
4005 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4009 err = got_object_open_as_tree(&tree1, repo,
4010 got_object_commit_get_tree_id(parent_commit));
4014 err = got_object_open_as_tree(&tree2, repo,
4015 got_object_commit_get_tree_id(commit));
4019 cpp_arg.path_prefix = path_prefix;
4020 while (cpp_arg.path_prefix[0] == '/')
4021 cpp_arg.path_prefix++;
4022 cpp_arg.len = strlen(cpp_arg.path_prefix);
4023 cpp_arg.errcode = errcode;
4024 err = got_diff_tree(tree1, tree2, "", "", repo,
4025 check_path_prefix_in_diff, &cpp_arg, 0);
4028 got_object_tree_close(tree1);
4030 got_object_tree_close(tree2);
4032 got_object_commit_close(commit);
4034 got_object_commit_close(parent_commit);
4038 static const struct got_error *
4039 collect_commits(struct got_object_id_queue *commits,
4040 struct got_object_id *initial_commit_id,
4041 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4042 const char *path_prefix, int path_prefix_errcode,
4043 struct got_repository *repo)
4045 const struct got_error *err = NULL;
4046 struct got_commit_graph *graph = NULL;
4047 struct got_object_id *parent_id = NULL;
4048 struct got_object_qid *qid;
4049 struct got_object_id *commit_id = initial_commit_id;
4051 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4055 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4058 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4059 err = got_commit_graph_iter_next(&parent_id, graph);
4061 if (err->code == GOT_ERR_ITER_COMPLETED) {
4062 err = got_error_msg(GOT_ERR_ANCESTRY,
4063 "ran out of commits to rebase before "
4064 "youngest common ancestor commit has "
4067 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4069 err = got_commit_graph_fetch_commits(graph, 1, repo);
4073 err = check_path_prefix(parent_id, commit_id,
4074 path_prefix, path_prefix_errcode, repo);
4078 err = got_object_qid_alloc(&qid, commit_id);
4081 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4082 commit_id = parent_id;
4086 got_commit_graph_close(graph);
4090 static const struct got_error *
4091 cmd_rebase(int argc, char *argv[])
4093 const struct got_error *error = NULL;
4094 struct got_worktree *worktree = NULL;
4095 struct got_repository *repo = NULL;
4096 struct got_fileindex *fileindex = NULL;
4098 struct got_reference *branch = NULL;
4099 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4100 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4101 struct got_object_id *resume_commit_id = NULL;
4102 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4103 struct got_commit_object *commit = NULL;
4104 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4105 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4106 struct got_object_id_queue commits;
4107 struct got_pathlist_head merged_paths;
4108 const struct got_object_id_queue *parent_ids;
4109 struct got_object_qid *qid, *pid;
4111 SIMPLEQ_INIT(&commits);
4112 TAILQ_INIT(&merged_paths);
4114 while ((ch = getopt(argc, argv, "ac")) != -1) {
4120 continue_rebase = 1;
4132 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4133 "unveil", NULL) == -1)
4136 if (abort_rebase && continue_rebase)
4138 else if (abort_rebase || continue_rebase) {
4141 } else if (argc != 1)
4144 cwd = getcwd(NULL, 0);
4146 error = got_error_from_errno("getcwd");
4149 error = got_worktree_open(&worktree, cwd);
4153 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4157 error = apply_unveil(got_repo_get_path(repo), 0,
4158 got_worktree_get_root_path(worktree));
4162 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4168 if (!rebase_in_progress) {
4169 error = got_error(GOT_ERR_NOT_REBASING);
4172 error = got_worktree_rebase_continue(&resume_commit_id,
4173 &new_base_branch, &tmp_branch, &branch, &fileindex,
4177 printf("Switching work tree to %s\n",
4178 got_ref_get_symref_target(new_base_branch));
4179 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4180 new_base_branch, update_progress, &did_something);
4183 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4184 goto done; /* nothing else to do */
4187 if (continue_rebase) {
4188 if (!rebase_in_progress) {
4189 error = got_error(GOT_ERR_NOT_REBASING);
4192 error = got_worktree_rebase_continue(&resume_commit_id,
4193 &new_base_branch, &tmp_branch, &branch, &fileindex,
4198 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4199 resume_commit_id, repo);
4203 yca_id = got_object_id_dup(resume_commit_id);
4204 if (yca_id == NULL) {
4205 error = got_error_from_errno("got_object_id_dup");
4209 error = got_ref_open(&branch, repo, argv[0], 0);
4214 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4218 if (!continue_rebase) {
4219 struct got_object_id *base_commit_id;
4221 base_commit_id = got_worktree_get_base_commit_id(worktree);
4222 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4223 base_commit_id, branch_head_commit_id, repo);
4226 if (yca_id == NULL) {
4227 error = got_error_msg(GOT_ERR_ANCESTRY,
4228 "specified branch shares no common ancestry "
4229 "with work tree's branch");
4233 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4235 if (error->code != GOT_ERR_ANCESTRY)
4239 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4240 "specified branch resolves to a commit which "
4241 "is already contained in work tree's branch");
4244 error = got_worktree_rebase_prepare(&new_base_branch,
4245 &tmp_branch, &fileindex, worktree, branch, repo);
4250 commit_id = branch_head_commit_id;
4251 error = got_object_open_as_commit(&commit, repo, commit_id);
4255 parent_ids = got_object_commit_get_parent_ids(commit);
4256 pid = SIMPLEQ_FIRST(parent_ids);
4257 error = collect_commits(&commits, commit_id, pid->id,
4258 yca_id, got_worktree_get_path_prefix(worktree),
4259 GOT_ERR_REBASE_PATH, repo);
4260 got_object_commit_close(commit);
4265 if (SIMPLEQ_EMPTY(&commits)) {
4266 if (continue_rebase)
4267 error = rebase_complete(worktree, fileindex,
4268 branch, new_base_branch, tmp_branch, repo);
4270 error = got_error(GOT_ERR_EMPTY_REBASE);
4275 SIMPLEQ_FOREACH(qid, &commits, entry) {
4276 commit_id = qid->id;
4277 parent_id = pid ? pid->id : yca_id;
4280 error = got_worktree_rebase_merge_files(&merged_paths,
4281 worktree, fileindex, parent_id, commit_id, repo,
4282 rebase_progress, &rebase_status, check_cancelled, NULL);
4286 if (rebase_status == GOT_STATUS_CONFLICT) {
4287 got_worktree_rebase_pathlist_free(&merged_paths);
4291 error = rebase_commit(&merged_paths, worktree, fileindex,
4292 tmp_branch, commit_id, repo);
4293 got_worktree_rebase_pathlist_free(&merged_paths);
4298 if (rebase_status == GOT_STATUS_CONFLICT) {
4299 error = got_worktree_rebase_postpone(worktree, fileindex);
4302 error = got_error_msg(GOT_ERR_CONFLICTS,
4303 "conflicts must be resolved before rebasing can continue");
4305 error = rebase_complete(worktree, fileindex, branch,
4306 new_base_branch, tmp_branch, repo);
4308 got_object_id_queue_free(&commits);
4309 free(branch_head_commit_id);
4310 free(resume_commit_id);
4313 got_object_commit_close(commit);
4315 got_ref_close(branch);
4316 if (new_base_branch)
4317 got_ref_close(new_base_branch);
4319 got_ref_close(tmp_branch);
4321 got_worktree_close(worktree);
4323 got_repo_close(repo);
4328 usage_histedit(void)
4330 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4335 #define GOT_HISTEDIT_PICK 'p'
4336 #define GOT_HISTEDIT_EDIT 'e'
4337 #define GOT_HISTEDIT_FOLD 'f'
4338 #define GOT_HISTEDIT_DROP 'd'
4339 #define GOT_HISTEDIT_MESG 'm'
4341 static struct got_histedit_cmd {
4345 } got_histedit_cmds[] = {
4346 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4347 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4348 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4349 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4350 { GOT_HISTEDIT_MESG, "mesg",
4351 "single-line log message for commit above (open editor if empty)" },
4354 struct got_histedit_list_entry {
4355 TAILQ_ENTRY(got_histedit_list_entry) entry;
4356 struct got_object_id *commit_id;
4357 const struct got_histedit_cmd *cmd;
4360 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4362 static const struct got_error *
4363 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4364 FILE *f, struct got_repository *repo)
4366 const struct got_error *err = NULL;
4367 char *logmsg = NULL, *id_str = NULL;
4368 struct got_commit_object *commit = NULL;
4371 err = got_object_open_as_commit(&commit, repo, commit_id);
4375 err = get_short_logmsg(&logmsg, 34, commit);
4379 err = got_object_id_str(&id_str, commit_id);
4383 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4385 err = got_ferror(f, GOT_ERR_IO);
4388 got_object_commit_close(commit);
4394 static const struct got_error *
4395 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4396 struct got_repository *repo)
4398 const struct got_error *err = NULL;
4399 struct got_object_qid *qid;
4401 if (SIMPLEQ_EMPTY(commits))
4402 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4404 SIMPLEQ_FOREACH(qid, commits, entry) {
4405 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4414 static const struct got_error *
4415 write_cmd_list(FILE *f)
4417 const struct got_error *err = NULL;
4420 n = fprintf(f, "# Available histedit commands:\n");
4422 return got_ferror(f, GOT_ERR_IO);
4424 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4425 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4426 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4429 err = got_ferror(f, GOT_ERR_IO);
4433 n = fprintf(f, "# Commits will be processed in order from top to "
4434 "bottom of this file.\n");
4436 return got_ferror(f, GOT_ERR_IO);
4440 static const struct got_error *
4441 histedit_syntax_error(int lineno)
4443 static char msg[42];
4446 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4448 if (ret == -1 || ret >= sizeof(msg))
4449 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4451 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4454 static const struct got_error *
4455 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4456 char *logmsg, struct got_repository *repo)
4458 const struct got_error *err;
4459 struct got_commit_object *folded_commit = NULL;
4462 err = got_object_id_str(&id_str, hle->commit_id);
4466 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4470 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4471 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4472 got_object_commit_get_logmsg(folded_commit)) == -1) {
4473 err = got_error_from_errno("asprintf");
4478 got_object_commit_close(folded_commit);
4483 static struct got_histedit_list_entry *
4484 get_folded_commits(struct got_histedit_list_entry *hle)
4486 struct got_histedit_list_entry *prev, *folded = NULL;
4488 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4489 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4490 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4491 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4493 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4499 static const struct got_error *
4500 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4501 struct got_repository *repo)
4503 char *logmsg_path = NULL, *id_str = NULL;
4504 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4505 const struct got_error *err = NULL;
4506 struct got_commit_object *commit = NULL;
4508 struct got_histedit_list_entry *folded = NULL;
4510 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4514 folded = get_folded_commits(hle);
4516 while (folded != hle) {
4517 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4518 folded = TAILQ_NEXT(folded, entry);
4521 err = append_folded_commit_msg(&new_msg, folded,
4527 folded = TAILQ_NEXT(folded, entry);
4531 err = got_object_id_str(&id_str, hle->commit_id);
4534 if (asprintf(&new_msg,
4535 "%s\n# original log message of commit %s: %s",
4536 logmsg ? logmsg : "", id_str,
4537 got_object_commit_get_logmsg(commit)) == -1) {
4538 err = got_error_from_errno("asprintf");
4544 err = got_object_id_str(&id_str, hle->commit_id);
4548 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4552 dprintf(fd, logmsg);
4555 err = get_editor(&editor);
4559 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4561 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4564 hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4565 if (hle->logmsg == NULL)
4566 err = got_error_from_errno("strdup");
4569 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4570 err = got_error_from_errno2("unlink", logmsg_path);
4575 got_object_commit_close(commit);
4579 static const struct got_error *
4580 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4581 FILE *f, struct got_repository *repo)
4583 const struct got_error *err = NULL;
4584 char *line = NULL, *p, *end;
4588 const struct got_histedit_cmd *cmd;
4589 struct got_object_id *commit_id = NULL;
4590 struct got_histedit_list_entry *hle = NULL;
4593 len = getline(&line, &size, f);
4595 const struct got_error *getline_err;
4598 getline_err = got_error_from_errno("getline");
4599 err = got_ferror(f, getline_err->code);
4604 while (isspace((unsigned char)p[0]))
4606 if (p[0] == '#' || p[0] == '\0') {
4612 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4613 cmd = &got_histedit_cmds[i];
4614 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4615 isspace((unsigned char)p[strlen(cmd->name)])) {
4616 p += strlen(cmd->name);
4619 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4624 if (i == nitems(got_histedit_cmds)) {
4625 err = histedit_syntax_error(lineno);
4628 while (isspace((unsigned char)p[0]))
4630 if (cmd->code == GOT_HISTEDIT_MESG) {
4631 if (hle == NULL || hle->logmsg != NULL) {
4632 err = got_error(GOT_ERR_HISTEDIT_CMD);
4636 err = histedit_edit_logmsg(hle, repo);
4640 hle->logmsg = strdup(p);
4641 if (hle->logmsg == NULL) {
4642 err = got_error_from_errno("strdup");
4651 while (end[0] && !isspace((unsigned char)end[0]))
4655 err = got_object_resolve_id_str(&commit_id, repo, p);
4657 /* override error code */
4658 err = histedit_syntax_error(lineno);
4662 hle = malloc(sizeof(*hle));
4664 err = got_error_from_errno("malloc");
4668 hle->commit_id = commit_id;
4673 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4681 static const struct got_error *
4682 histedit_check_script(struct got_histedit_list *histedit_cmds,
4683 struct got_object_id_queue *commits, struct got_repository *repo)
4685 const struct got_error *err = NULL;
4686 struct got_object_qid *qid;
4687 struct got_histedit_list_entry *hle;
4688 static char msg[80];
4691 if (TAILQ_EMPTY(histedit_cmds))
4692 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4693 "histedit script contains no commands");
4694 if (SIMPLEQ_EMPTY(commits))
4695 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4697 SIMPLEQ_FOREACH(qid, commits, entry) {
4698 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4699 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4703 err = got_object_id_str(&id_str, qid->id);
4706 snprintf(msg, sizeof(msg),
4707 "commit %s missing from histedit script", id_str);
4709 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4713 if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4714 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4715 "last commit in histedit script cannot be folded");
4720 static const struct got_error *
4721 histedit_run_editor(struct got_histedit_list *histedit_cmds,
4722 const char *path, struct got_object_id_queue *commits,
4723 struct got_repository *repo)
4725 const struct got_error *err = NULL;
4729 err = get_editor(&editor);
4733 if (spawn_editor(editor, path) == -1) {
4734 err = got_error_from_errno("failed spawning editor");
4738 f = fopen(path, "r");
4740 err = got_error_from_errno("fopen");
4743 err = histedit_parse_list(histedit_cmds, f, repo);
4747 err = histedit_check_script(histedit_cmds, commits, repo);
4749 if (f && fclose(f) != 0 && err == NULL)
4750 err = got_error_from_errno("fclose");
4755 static const struct got_error *
4756 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4757 struct got_object_id_queue *, const char *, struct got_repository *);
4759 static const struct got_error *
4760 histedit_edit_script(struct got_histedit_list *histedit_cmds,
4761 struct got_object_id_queue *commits, struct got_repository *repo)
4763 const struct got_error *err;
4767 err = got_opentemp_named(&path, &f, "got-histedit");
4771 err = write_cmd_list(f);
4775 err = histedit_write_commit_list(commits, f, repo);
4779 if (fclose(f) != 0) {
4780 err = got_error_from_errno("fclose");
4785 err = histedit_run_editor(histedit_cmds, path, commits, repo);
4787 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4788 err->code != GOT_ERR_HISTEDIT_CMD)
4790 err = histedit_edit_list_retry(histedit_cmds, err,
4791 commits, path, repo);
4794 if (f && fclose(f) != 0 && err == NULL)
4795 err = got_error_from_errno("fclose");
4796 if (path && unlink(path) != 0 && err == NULL)
4797 err = got_error_from_errno2("unlink", path);
4802 static const struct got_error *
4803 histedit_save_list(struct got_histedit_list *histedit_cmds,
4804 struct got_worktree *worktree, struct got_repository *repo)
4806 const struct got_error *err = NULL;
4809 struct got_histedit_list_entry *hle;
4810 struct got_commit_object *commit = NULL;
4812 err = got_worktree_get_histedit_script_path(&path, worktree);
4816 f = fopen(path, "w");
4818 err = got_error_from_errno2("fopen", path);
4821 TAILQ_FOREACH(hle, histedit_cmds, entry) {
4822 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4828 int n = fprintf(f, "%c %s\n",
4829 GOT_HISTEDIT_MESG, hle->logmsg);
4831 err = got_ferror(f, GOT_ERR_IO);
4837 if (f && fclose(f) != 0 && err == NULL)
4838 err = got_error_from_errno("fclose");
4841 got_object_commit_close(commit);
4846 histedit_free_list(struct got_histedit_list *histedit_cmds)
4848 struct got_histedit_list_entry *hle;
4850 while ((hle = TAILQ_FIRST(histedit_cmds))) {
4851 TAILQ_REMOVE(histedit_cmds, hle, entry);
4856 static const struct got_error *
4857 histedit_load_list(struct got_histedit_list *histedit_cmds,
4858 const char *path, struct got_repository *repo)
4860 const struct got_error *err = NULL;
4863 f = fopen(path, "r");
4865 err = got_error_from_errno2("fopen", path);
4869 err = histedit_parse_list(histedit_cmds, f, repo);
4871 if (f && fclose(f) != 0 && err == NULL)
4872 err = got_error_from_errno("fclose");
4876 static const struct got_error *
4877 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4878 const struct got_error *edit_err, struct got_object_id_queue *commits,
4879 const char *path, struct got_repository *repo)
4881 const struct got_error *err = NULL, *prev_err = edit_err;
4884 while (resp != 'c' && resp != 'r' && resp != 'a') {
4885 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4886 "or (a)bort: ", getprogname(), prev_err->msg);
4891 histedit_free_list(histedit_cmds);
4892 err = histedit_run_editor(histedit_cmds, path, commits,
4895 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4896 err->code != GOT_ERR_HISTEDIT_CMD)
4903 } else if (resp == 'r') {
4904 histedit_free_list(histedit_cmds);
4905 err = histedit_edit_script(histedit_cmds,
4908 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4909 err->code != GOT_ERR_HISTEDIT_CMD)
4916 } else if (resp == 'a') {
4917 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4920 printf("invalid response '%c'\n", resp);
4926 static const struct got_error *
4927 histedit_complete(struct got_worktree *worktree,
4928 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4929 struct got_reference *branch, struct got_repository *repo)
4931 printf("Switching work tree to %s\n",
4932 got_ref_get_symref_target(branch));
4933 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4937 static const struct got_error *
4938 show_histedit_progress(struct got_commit_object *commit,
4939 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4941 const struct got_error *err;
4942 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4944 err = got_object_id_str(&old_id_str, hle->commit_id);
4949 err = got_object_id_str(&new_id_str, new_id);
4954 old_id_str[12] = '\0';
4956 new_id_str[12] = '\0';
4959 logmsg = strdup(hle->logmsg);
4960 if (logmsg == NULL) {
4961 err = got_error_from_errno("strdup");
4964 trim_logmsg(logmsg, 42);
4966 err = get_short_logmsg(&logmsg, 42, commit);
4971 switch (hle->cmd->code) {
4972 case GOT_HISTEDIT_PICK:
4973 case GOT_HISTEDIT_EDIT:
4974 printf("%s -> %s: %s\n", old_id_str,
4975 new_id_str ? new_id_str : "no-op change", logmsg);
4977 case GOT_HISTEDIT_DROP:
4978 case GOT_HISTEDIT_FOLD:
4979 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4992 static const struct got_error *
4993 histedit_commit(struct got_pathlist_head *merged_paths,
4994 struct got_worktree *worktree, struct got_fileindex *fileindex,
4995 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4996 struct got_repository *repo)
4998 const struct got_error *err;
4999 struct got_commit_object *commit;
5000 struct got_object_id *new_commit_id;
5002 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5003 && hle->logmsg == NULL) {
5004 err = histedit_edit_logmsg(hle, repo);
5009 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5013 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5014 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5017 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5019 err = show_histedit_progress(commit, hle, NULL);
5021 err = show_histedit_progress(commit, hle, new_commit_id);
5022 free(new_commit_id);
5025 got_object_commit_close(commit);
5029 static const struct got_error *
5030 histedit_skip_commit(struct got_histedit_list_entry *hle,
5031 struct got_worktree *worktree, struct got_repository *repo)
5033 const struct got_error *error;
5034 struct got_commit_object *commit;
5036 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5041 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5045 error = show_histedit_progress(commit, hle, NULL);
5046 got_object_commit_close(commit);
5050 static const struct got_error *
5051 cmd_histedit(int argc, char *argv[])
5053 const struct got_error *error = NULL;
5054 struct got_worktree *worktree = NULL;
5055 struct got_fileindex *fileindex = NULL;
5056 struct got_repository *repo = NULL;
5058 struct got_reference *branch = NULL;
5059 struct got_reference *tmp_branch = NULL;
5060 struct got_object_id *resume_commit_id = NULL;
5061 struct got_object_id *base_commit_id = NULL;
5062 struct got_object_id *head_commit_id = NULL;
5063 struct got_commit_object *commit = NULL;
5064 int ch, rebase_in_progress = 0, did_something;
5065 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5066 const char *edit_script_path = NULL;
5067 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5068 struct got_object_id_queue commits;
5069 struct got_pathlist_head merged_paths;
5070 const struct got_object_id_queue *parent_ids;
5071 struct got_object_qid *pid;
5072 struct got_histedit_list histedit_cmds;
5073 struct got_histedit_list_entry *hle;
5075 SIMPLEQ_INIT(&commits);
5076 TAILQ_INIT(&histedit_cmds);
5077 TAILQ_INIT(&merged_paths);
5079 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5088 edit_script_path = optarg;
5100 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5101 "unveil", NULL) == -1)
5104 if (abort_edit && continue_edit)
5110 * This command cannot apply unveil(2) in all cases because the
5111 * user may choose to run an editor to edit the histedit script
5112 * and to edit individual commit log messages.
5113 * unveil(2) traverses exec(2); if an editor is used we have to
5114 * apply unveil after edit script and log messages have been written.
5115 * XXX TODO: Make use of unveil(2) where possible.
5118 cwd = getcwd(NULL, 0);
5120 error = got_error_from_errno("getcwd");
5123 error = got_worktree_open(&worktree, cwd);
5127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5131 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5134 if (rebase_in_progress) {
5135 error = got_error(GOT_ERR_REBASING);
5139 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5143 if (edit_in_progress && abort_edit) {
5144 error = got_worktree_histedit_continue(&resume_commit_id,
5145 &tmp_branch, &branch, &base_commit_id, &fileindex,
5149 printf("Switching work tree to %s\n",
5150 got_ref_get_symref_target(branch));
5151 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5152 branch, base_commit_id, update_progress, &did_something);
5155 printf("Histedit of %s aborted\n",
5156 got_ref_get_symref_target(branch));
5157 goto done; /* nothing else to do */
5158 } else if (abort_edit) {
5159 error = got_error(GOT_ERR_NOT_HISTEDIT);
5163 if (continue_edit) {
5166 if (!edit_in_progress) {
5167 error = got_error(GOT_ERR_NOT_HISTEDIT);
5171 error = got_worktree_get_histedit_script_path(&path, worktree);
5175 error = histedit_load_list(&histedit_cmds, path, repo);
5180 error = got_worktree_histedit_continue(&resume_commit_id,
5181 &tmp_branch, &branch, &base_commit_id, &fileindex,
5186 error = got_ref_resolve(&head_commit_id, repo, branch);
5190 error = got_object_open_as_commit(&commit, repo,
5194 parent_ids = got_object_commit_get_parent_ids(commit);
5195 pid = SIMPLEQ_FIRST(parent_ids);
5197 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5200 error = collect_commits(&commits, head_commit_id, pid->id,
5201 base_commit_id, got_worktree_get_path_prefix(worktree),
5202 GOT_ERR_HISTEDIT_PATH, repo);
5203 got_object_commit_close(commit);
5208 if (edit_in_progress) {
5209 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5213 error = got_ref_open(&branch, repo,
5214 got_worktree_get_head_ref_name(worktree), 0);
5218 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5219 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5220 "will not edit commit history of a branch outside "
5221 "the \"refs/heads/\" reference namespace");
5225 error = got_ref_resolve(&head_commit_id, repo, branch);
5226 got_ref_close(branch);
5231 error = got_object_open_as_commit(&commit, repo,
5235 parent_ids = got_object_commit_get_parent_ids(commit);
5236 pid = SIMPLEQ_FIRST(parent_ids);
5238 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5241 error = collect_commits(&commits, head_commit_id, pid->id,
5242 got_worktree_get_base_commit_id(worktree),
5243 got_worktree_get_path_prefix(worktree),
5244 GOT_ERR_HISTEDIT_PATH, repo);
5245 got_object_commit_close(commit);
5250 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5251 &base_commit_id, &fileindex, worktree, repo);
5255 if (edit_script_path) {
5256 error = histedit_load_list(&histedit_cmds,
5257 edit_script_path, repo);
5259 got_worktree_histedit_abort(worktree, fileindex,
5260 repo, branch, base_commit_id,
5261 update_progress, &did_something);
5265 error = histedit_edit_script(&histedit_cmds, &commits,
5268 got_worktree_histedit_abort(worktree, fileindex,
5269 repo, branch, base_commit_id,
5270 update_progress, &did_something);
5276 error = histedit_save_list(&histedit_cmds, worktree,
5279 got_worktree_histedit_abort(worktree, fileindex,
5280 repo, branch, base_commit_id,
5281 update_progress, &did_something);
5287 error = histedit_check_script(&histedit_cmds, &commits, repo);
5291 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5292 if (resume_commit_id) {
5293 if (got_object_id_cmp(hle->commit_id,
5294 resume_commit_id) != 0)
5297 resume_commit_id = NULL;
5298 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5299 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5300 error = histedit_skip_commit(hle, worktree,
5303 error = histedit_commit(NULL, worktree,
5304 fileindex, tmp_branch, hle, repo);
5311 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5312 error = histedit_skip_commit(hle, worktree, repo);
5318 error = got_object_open_as_commit(&commit, repo,
5322 parent_ids = got_object_commit_get_parent_ids(commit);
5323 pid = SIMPLEQ_FIRST(parent_ids);
5325 error = got_worktree_histedit_merge_files(&merged_paths,
5326 worktree, fileindex, pid->id, hle->commit_id, repo,
5327 rebase_progress, &rebase_status, check_cancelled, NULL);
5330 got_object_commit_close(commit);
5333 if (rebase_status == GOT_STATUS_CONFLICT) {
5334 got_worktree_rebase_pathlist_free(&merged_paths);
5338 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5340 error = got_object_id_str(&id_str, hle->commit_id);
5343 printf("Stopping histedit for amending commit %s\n",
5346 got_worktree_rebase_pathlist_free(&merged_paths);
5347 error = got_worktree_histedit_postpone(worktree,
5352 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5353 error = histedit_skip_commit(hle, worktree, repo);
5359 error = histedit_commit(&merged_paths, worktree, fileindex,
5360 tmp_branch, hle, repo);
5361 got_worktree_rebase_pathlist_free(&merged_paths);
5366 if (rebase_status == GOT_STATUS_CONFLICT) {
5367 error = got_worktree_histedit_postpone(worktree, fileindex);
5370 error = got_error_msg(GOT_ERR_CONFLICTS,
5371 "conflicts must be resolved before rebasing can continue");
5373 error = histedit_complete(worktree, fileindex, tmp_branch,
5376 got_object_id_queue_free(&commits);
5377 histedit_free_list(&histedit_cmds);
5378 free(head_commit_id);
5379 free(base_commit_id);
5380 free(resume_commit_id);
5382 got_object_commit_close(commit);
5384 got_ref_close(branch);
5386 got_ref_close(tmp_branch);
5388 got_worktree_close(worktree);
5390 got_repo_close(repo);
5397 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5398 "[file-path ...]\n",
5403 static const struct got_error *
5404 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5405 const char *path, struct got_object_id *blob_id,
5406 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5408 const struct got_error *err = NULL;
5409 char *id_str = NULL;
5411 if (staged_status != GOT_STATUS_ADD &&
5412 staged_status != GOT_STATUS_MODIFY &&
5413 staged_status != GOT_STATUS_DELETE)
5416 if (staged_status == GOT_STATUS_ADD ||
5417 staged_status == GOT_STATUS_MODIFY)
5418 err = got_object_id_str(&id_str, staged_blob_id);
5420 err = got_object_id_str(&id_str, blob_id);
5424 printf("%s %c %s\n", id_str, staged_status, path);
5429 static const struct got_error *
5430 cmd_stage(int argc, char *argv[])
5432 const struct got_error *error = NULL;
5433 struct got_repository *repo = NULL;
5434 struct got_worktree *worktree = NULL;
5436 struct got_pathlist_head paths;
5437 struct got_pathlist_entry *pe;
5438 int ch, list_stage = 0, pflag = 0;
5439 FILE *patch_script_file = NULL;
5440 const char *patch_script_path = NULL;
5441 struct choose_patch_arg cpa;
5445 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5454 patch_script_path = optarg;
5466 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5467 "unveil", NULL) == -1)
5470 if (list_stage && (pflag || patch_script_path))
5471 errx(1, "-l option cannot be used with other options");
5472 if (patch_script_path && !pflag)
5473 errx(1, "-F option can only be used together with -p option");
5475 cwd = getcwd(NULL, 0);
5477 error = got_error_from_errno("getcwd");
5481 error = got_worktree_open(&worktree, cwd);
5485 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5489 if (patch_script_path) {
5490 patch_script_file = fopen(patch_script_path, "r");
5491 if (patch_script_file == NULL) {
5492 error = got_error_from_errno2("fopen",
5497 error = apply_unveil(got_repo_get_path(repo), 1,
5498 got_worktree_get_root_path(worktree));
5502 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5507 error = got_worktree_status(worktree, &paths, repo,
5508 print_stage, NULL, check_cancelled, NULL);
5510 cpa.patch_script_file = patch_script_file;
5511 cpa.action = "stage";
5512 error = got_worktree_stage(worktree, &paths,
5513 pflag ? NULL : print_status, NULL,
5514 pflag ? choose_patch : NULL, &cpa, repo);
5517 if (patch_script_file && fclose(patch_script_file) == EOF &&
5519 error = got_error_from_errno2("fclose", patch_script_path);
5521 got_repo_close(repo);
5523 got_worktree_close(worktree);
5524 TAILQ_FOREACH(pe, &paths, entry)
5525 free((char *)pe->path);
5526 got_pathlist_free(&paths);
5534 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5535 "[file-path ...]\n",
5541 static const struct got_error *
5542 cmd_unstage(int argc, char *argv[])
5544 const struct got_error *error = NULL;
5545 struct got_repository *repo = NULL;
5546 struct got_worktree *worktree = NULL;
5548 struct got_pathlist_head paths;
5549 struct got_pathlist_entry *pe;
5550 int ch, did_something = 0, pflag = 0;
5551 FILE *patch_script_file = NULL;
5552 const char *patch_script_path = NULL;
5553 struct choose_patch_arg cpa;
5557 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5563 patch_script_path = optarg;
5575 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5576 "unveil", NULL) == -1)
5579 if (patch_script_path && !pflag)
5580 errx(1, "-F option can only be used together with -p option");
5582 cwd = getcwd(NULL, 0);
5584 error = got_error_from_errno("getcwd");
5588 error = got_worktree_open(&worktree, cwd);
5592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5596 if (patch_script_path) {
5597 patch_script_file = fopen(patch_script_path, "r");
5598 if (patch_script_file == NULL) {
5599 error = got_error_from_errno2("fopen",
5605 error = apply_unveil(got_repo_get_path(repo), 1,
5606 got_worktree_get_root_path(worktree));
5610 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5614 cpa.patch_script_file = patch_script_file;
5615 cpa.action = "unstage";
5616 error = got_worktree_unstage(worktree, &paths, update_progress,
5617 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5619 if (patch_script_file && fclose(patch_script_file) == EOF &&
5621 error = got_error_from_errno2("fclose", patch_script_path);
5623 got_repo_close(repo);
5625 got_worktree_close(worktree);
5626 TAILQ_FOREACH(pe, &paths, entry)
5627 free((char *)pe->path);
5628 got_pathlist_free(&paths);