2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_worktree.h"
46 #include "got_commit_graph.h"
47 #include "got_blame.h"
48 #include "got_privsep.h"
49 #include "got_opentemp.h"
52 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
55 static volatile sig_atomic_t sigint_received;
56 static volatile sig_atomic_t sigpipe_received;
59 catch_sigint(int signo)
65 catch_sigpipe(int signo)
73 const struct got_error *(*cmd_main)(int, char *[]);
74 void (*cmd_usage)(void);
75 const char *cmd_alias;
78 __dead static void usage(int);
79 __dead static void usage_init(void);
80 __dead static void usage_import(void);
81 __dead static void usage_checkout(void);
82 __dead static void usage_update(void);
83 __dead static void usage_log(void);
84 __dead static void usage_diff(void);
85 __dead static void usage_blame(void);
86 __dead static void usage_tree(void);
87 __dead static void usage_status(void);
88 __dead static void usage_ref(void);
89 __dead static void usage_branch(void);
90 __dead static void usage_add(void);
91 __dead static void usage_remove(void);
92 __dead static void usage_revert(void);
93 __dead static void usage_commit(void);
94 __dead static void usage_cherrypick(void);
95 __dead static void usage_backout(void);
96 __dead static void usage_rebase(void);
97 __dead static void usage_histedit(void);
98 __dead static void usage_stage(void);
99 __dead static void usage_unstage(void);
101 static const struct got_error* cmd_init(int, char *[]);
102 static const struct got_error* cmd_import(int, char *[]);
103 static const struct got_error* cmd_checkout(int, char *[]);
104 static const struct got_error* cmd_update(int, char *[]);
105 static const struct got_error* cmd_log(int, char *[]);
106 static const struct got_error* cmd_diff(int, char *[]);
107 static const struct got_error* cmd_blame(int, char *[]);
108 static const struct got_error* cmd_tree(int, char *[]);
109 static const struct got_error* cmd_status(int, char *[]);
110 static const struct got_error* cmd_ref(int, char *[]);
111 static const struct got_error* cmd_branch(int, char *[]);
112 static const struct got_error* cmd_add(int, char *[]);
113 static const struct got_error* cmd_remove(int, char *[]);
114 static const struct got_error* cmd_revert(int, char *[]);
115 static const struct got_error* cmd_commit(int, char *[]);
116 static const struct got_error* cmd_cherrypick(int, char *[]);
117 static const struct got_error* cmd_backout(int, char *[]);
118 static const struct got_error* cmd_rebase(int, char *[]);
119 static const struct got_error* cmd_histedit(int, char *[]);
120 static const struct got_error* cmd_stage(int, char *[]);
121 static const struct got_error* cmd_unstage(int, char *[]);
123 static struct got_cmd got_commands[] = {
124 { "init", cmd_init, usage_init, "in" },
125 { "import", cmd_import, usage_import, "im" },
126 { "checkout", cmd_checkout, usage_checkout, "co" },
127 { "update", cmd_update, usage_update, "up" },
128 { "log", cmd_log, usage_log, "" },
129 { "diff", cmd_diff, usage_diff, "di" },
130 { "blame", cmd_blame, usage_blame, "bl" },
131 { "tree", cmd_tree, usage_tree, "tr" },
132 { "status", cmd_status, usage_status, "st" },
133 { "ref", cmd_ref, usage_ref, "" },
134 { "branch", cmd_branch, usage_branch, "br" },
135 { "add", cmd_add, usage_add, "" },
136 { "remove", cmd_remove, usage_remove, "rm" },
137 { "revert", cmd_revert, usage_revert, "rv" },
138 { "commit", cmd_commit, usage_commit, "ci" },
139 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
140 { "backout", cmd_backout, usage_backout, "bo" },
141 { "rebase", cmd_rebase, usage_rebase, "rb" },
142 { "histedit", cmd_histedit, usage_histedit, "he" },
143 { "stage", cmd_stage, usage_stage, "sg" },
144 { "unstage", cmd_unstage, usage_unstage, "ug" },
152 fprintf(stderr, "commands:");
153 for (i = 0; i < nitems(got_commands); i++) {
154 struct got_cmd *cmd = &got_commands[i];
155 fprintf(stderr, " %s", cmd->cmd_name);
161 main(int argc, char *argv[])
166 int hflag = 0, Vflag = 0;
168 setlocale(LC_CTYPE, "");
170 while ((ch = getopt(argc, argv, "hV")) != -1) {
189 got_version_print_str();
196 signal(SIGINT, catch_sigint);
197 signal(SIGPIPE, catch_sigpipe);
199 for (i = 0; i < nitems(got_commands); i++) {
200 const struct got_error *error;
202 cmd = &got_commands[i];
204 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
205 strcmp(cmd->cmd_alias, argv[0]) != 0)
209 got_commands[i].cmd_usage();
211 error = got_commands[i].cmd_main(argc, argv);
212 if (error && !(sigint_received || sigpipe_received)) {
213 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
220 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
228 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
235 static const struct got_error *
236 get_editor(char **abspath)
238 const struct got_error *err = NULL;
243 editor = getenv("VISUAL");
245 editor = getenv("EDITOR");
248 err = got_path_find_prog(abspath, editor);
253 if (*abspath == NULL) {
254 *abspath = strdup("/bin/ed");
255 if (*abspath == NULL)
256 return got_error_from_errno("strdup");
262 static const struct got_error *
263 apply_unveil(const char *repo_path, int repo_read_only,
264 const char *worktree_path)
266 const struct got_error *err;
269 if (unveil("gmon.out", "rwc") != 0)
270 return got_error_from_errno2("unveil", "gmon.out");
272 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
273 return got_error_from_errno2("unveil", repo_path);
275 if (worktree_path && unveil(worktree_path, "rwc") != 0)
276 return got_error_from_errno2("unveil", worktree_path);
278 if (unveil("/tmp", "rwc") != 0)
279 return got_error_from_errno2("unveil", "/tmp");
281 err = got_privsep_unveil_exec_helpers();
285 if (unveil(NULL, NULL) != 0)
286 return got_error_from_errno("unveil");
294 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
298 static const struct got_error *
299 cmd_init(int argc, char *argv[])
301 const struct got_error *error = NULL;
302 char *repo_path = NULL;
305 while ((ch = getopt(argc, argv, "")) != -1) {
317 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
323 repo_path = strdup(argv[0]);
324 if (repo_path == NULL)
325 return got_error_from_errno("strdup");
327 got_path_strip_trailing_slashes(repo_path);
329 error = got_path_mkdir(repo_path);
331 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
334 error = apply_unveil(repo_path, 0, NULL);
338 error = got_repo_init(repo_path);
350 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
351 "[-r repository-path] [-I pattern] path\n", getprogname());
356 spawn_editor(const char *editor, const char *file)
359 sig_t sighup, sigint, sigquit;
362 sighup = signal(SIGHUP, SIG_IGN);
363 sigint = signal(SIGINT, SIG_IGN);
364 sigquit = signal(SIGQUIT, SIG_IGN);
366 switch (pid = fork()) {
370 execl(editor, editor, file, (char *)NULL);
374 while (waitpid(pid, &st, 0) == -1)
379 (void)signal(SIGHUP, sighup);
380 (void)signal(SIGINT, sigint);
381 (void)signal(SIGQUIT, sigquit);
383 if (!WIFEXITED(st)) {
388 return WEXITSTATUS(st);
391 static const struct got_error *
392 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
393 const char *initial_content)
395 const struct got_error *err = NULL;
399 int content_changed = 0;
404 if (stat(logmsg_path, &st) == -1)
405 return got_error_from_errno2("stat", logmsg_path);
407 if (spawn_editor(editor, logmsg_path) == -1)
408 return got_error_from_errno("failed spawning editor");
410 if (stat(logmsg_path, &st2) == -1)
411 return got_error_from_errno("stat");
413 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
414 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
415 "no changes made to commit message, aborting");
417 *logmsg = malloc(st2.st_size + 1);
419 return got_error_from_errno("malloc");
423 fp = fopen(logmsg_path, "r");
425 err = got_error_from_errno("fopen");
428 while (fgets(buf, sizeof(buf), fp) != NULL) {
429 if (!content_changed && strcmp(buf, initial_content) != 0)
431 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
432 continue; /* remove comments and leading empty lines */
433 len = strlcat(*logmsg, buf, st2.st_size);
437 while (len > 0 && (*logmsg)[len - 1] == '\n') {
438 (*logmsg)[len - 1] = '\0';
442 if (len == 0 || !content_changed)
443 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
444 "commit message cannot be empty, aborting");
453 static const struct got_error *
454 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
455 const char *branch_name)
457 char *initial_content = NULL, *logmsg_path = NULL;
458 const struct got_error *err = NULL;
461 if (asprintf(&initial_content,
462 "\n# %s to be imported to branch %s\n", path_dir,
464 return got_error_from_errno("asprintf");
466 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
470 dprintf(fd, initial_content);
473 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
475 free(initial_content);
480 static const struct got_error *
481 import_progress(void *arg, const char *path)
483 printf("A %s\n", path);
487 static const struct got_error *
488 get_author(const char **author)
490 const char *got_author;
494 got_author = getenv("GOT_AUTHOR");
495 if (got_author == NULL) {
496 /* TODO: Look up user in password database? */
497 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
500 *author = got_author;
503 * Really dumb email address check; we're only doing this to
504 * avoid git's object parser breaking on commits we create.
506 while (*got_author && *got_author != '<')
508 if (*got_author != '<')
509 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
510 while (*got_author && *got_author != '@')
512 if (*got_author != '@')
513 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
514 while (*got_author && *got_author != '>')
516 if (*got_author != '>')
517 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
522 static const struct got_error *
523 cmd_import(int argc, char *argv[])
525 const struct got_error *error = NULL;
526 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
529 const char *branch_name = "master";
530 char *refname = NULL, *id_str = NULL;
531 struct got_repository *repo = NULL;
532 struct got_reference *branch_ref = NULL, *head_ref = NULL;
533 struct got_object_id *new_commit_id = NULL;
535 struct got_pathlist_head ignores;
536 struct got_pathlist_entry *pe;
538 TAILQ_INIT(&ignores);
540 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
543 branch_name = optarg;
546 logmsg = strdup(optarg);
547 if (logmsg == NULL) {
548 error = got_error_from_errno("strdup");
553 repo_path = realpath(optarg, NULL);
554 if (repo_path == NULL) {
555 error = got_error_from_errno("realpath");
560 if (optarg[0] == '\0')
562 error = got_pathlist_insert(&pe, &ignores, optarg,
577 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
584 error = get_author(&author);
588 if (repo_path == NULL) {
589 repo_path = getcwd(NULL, 0);
590 if (repo_path == NULL)
591 return got_error_from_errno("getcwd");
593 got_path_strip_trailing_slashes(repo_path);
594 error = got_repo_open(&repo, repo_path);
598 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
599 error = got_error_from_errno("asprintf");
603 error = got_ref_open(&branch_ref, repo, refname, 0);
605 if (error->code != GOT_ERR_NOT_REF)
608 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
609 "import target branch already exists");
613 path_dir = realpath(argv[0], NULL);
614 if (path_dir == NULL) {
615 error = got_error_from_errno("realpath");
618 got_path_strip_trailing_slashes(path_dir);
621 * unveil(2) traverses exec(2); if an editor is used we have
622 * to apply unveil after the log message has been written.
624 if (logmsg == NULL || strlen(logmsg) == 0) {
625 error = get_editor(&editor);
628 error = collect_import_msg(&logmsg, editor, path_dir, refname);
633 if (unveil(path_dir, "r") != 0)
634 return got_error_from_errno2("unveil", path_dir);
636 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
640 error = got_repo_import(&new_commit_id, path_dir, logmsg,
641 author, &ignores, repo, import_progress, NULL);
645 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
649 error = got_ref_write(branch_ref, repo);
653 error = got_object_id_str(&id_str, new_commit_id);
657 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
659 if (error->code != GOT_ERR_NOT_REF)
662 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
667 error = got_ref_write(head_ref, repo);
672 printf("Created branch %s with commit %s\n",
673 got_ref_get_name(branch_ref), id_str);
681 got_ref_close(branch_ref);
683 got_ref_close(head_ref);
690 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
691 "[-p prefix] repository-path [worktree-path]\n", getprogname());
695 static const struct got_error *
696 checkout_progress(void *arg, unsigned char status, const char *path)
698 char *worktree_path = arg;
700 /* Base commit bump happens silently. */
701 if (status == GOT_STATUS_BUMP_BASE)
704 while (path[0] == '/')
707 printf("%c %s/%s\n", status, worktree_path, path);
711 static const struct got_error *
712 check_cancelled(void *arg)
714 if (sigint_received || sigpipe_received)
715 return got_error(GOT_ERR_CANCELLED);
719 static const struct got_error *
720 check_linear_ancestry(struct got_object_id *commit_id,
721 struct got_object_id *base_commit_id, struct got_repository *repo)
723 const struct got_error *err = NULL;
724 struct got_object_id *yca_id;
726 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
727 commit_id, base_commit_id, repo);
732 return got_error(GOT_ERR_ANCESTRY);
735 * Require a straight line of history between the target commit
736 * and the work tree's base commit.
738 * Non-linear situations such as this require a rebase:
740 * (commit) D F (base_commit)
748 * 'got update' only handles linear cases:
749 * Update forwards in time: A (base/yca) - B - C - D (commit)
750 * Update backwards in time: D (base) - C - B - A (commit/yca)
752 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
753 got_object_id_cmp(base_commit_id, yca_id) != 0)
754 return got_error(GOT_ERR_ANCESTRY);
760 static const struct got_error *
761 check_same_branch(struct got_object_id *commit_id,
762 struct got_reference *head_ref, struct got_object_id *yca_id,
763 struct got_repository *repo)
765 const struct got_error *err = NULL;
766 struct got_commit_graph *graph = NULL;
767 struct got_object_id *head_commit_id = NULL;
768 int is_same_branch = 0;
770 err = got_ref_resolve(&head_commit_id, repo, head_ref);
774 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
778 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
783 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
787 err = got_commit_graph_iter_start(graph, head_commit_id, repo);
792 struct got_object_id *id;
793 err = got_commit_graph_iter_next(&id, graph);
795 if (err->code == GOT_ERR_ITER_COMPLETED) {
798 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
800 err = got_commit_graph_fetch_commits(graph, 1,
807 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
809 if (got_object_id_cmp(id, commit_id) == 0) {
817 got_commit_graph_close(graph);
818 free(head_commit_id);
819 if (!err && !is_same_branch)
820 err = got_error(GOT_ERR_ANCESTRY);
824 static const struct got_error *
825 resolve_commit_arg(struct got_object_id **commit_id,
826 const char *commit_id_arg, struct got_repository *repo)
828 const struct got_error *err;
829 struct got_reference *ref;
830 struct got_tag_object *tag;
832 err = got_repo_object_match_tag(&tag, commit_id_arg,
833 GOT_OBJ_TYPE_COMMIT, repo);
835 *commit_id = got_object_id_dup(
836 got_object_tag_get_object_id(tag));
837 if (*commit_id == NULL)
838 err = got_error_from_errno("got_object_id_dup");
839 got_object_tag_close(tag);
841 } else if (err->code != GOT_ERR_NO_OBJ)
844 err = got_ref_open(&ref, repo, commit_id_arg, 0);
846 err = got_ref_resolve(commit_id, repo, ref);
849 if (err->code != GOT_ERR_NOT_REF)
851 err = got_repo_match_object_id_prefix(commit_id,
852 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
857 static const struct got_error *
858 cmd_checkout(int argc, char *argv[])
860 const struct got_error *error = NULL;
861 struct got_repository *repo = NULL;
862 struct got_reference *head_ref = NULL;
863 struct got_worktree *worktree = NULL;
864 char *repo_path = NULL;
865 char *worktree_path = NULL;
866 const char *path_prefix = "";
867 const char *branch_name = GOT_REF_HEAD;
868 char *commit_id_str = NULL;
869 int ch, same_path_prefix;
870 struct got_pathlist_head paths;
874 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
877 branch_name = optarg;
880 commit_id_str = strdup(optarg);
881 if (commit_id_str == NULL)
882 return got_error_from_errno("strdup");
885 path_prefix = optarg;
897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
898 "unveil", NULL) == -1)
902 char *cwd, *base, *dotgit;
903 repo_path = realpath(argv[0], NULL);
904 if (repo_path == NULL)
905 return got_error_from_errno2("realpath", argv[0]);
906 cwd = getcwd(NULL, 0);
908 error = got_error_from_errno("getcwd");
911 if (path_prefix[0]) {
912 base = basename(path_prefix);
914 error = got_error_from_errno2("basename",
919 base = basename(repo_path);
921 error = got_error_from_errno2("basename",
926 dotgit = strstr(base, ".git");
929 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
930 error = got_error_from_errno("asprintf");
935 } else if (argc == 2) {
936 repo_path = realpath(argv[0], NULL);
937 if (repo_path == NULL) {
938 error = got_error_from_errno2("realpath", argv[0]);
941 worktree_path = realpath(argv[1], NULL);
942 if (worktree_path == NULL) {
943 if (errno != ENOENT) {
944 error = got_error_from_errno2("realpath",
948 worktree_path = strdup(argv[1]);
949 if (worktree_path == NULL) {
950 error = got_error_from_errno("strdup");
957 got_path_strip_trailing_slashes(repo_path);
958 got_path_strip_trailing_slashes(worktree_path);
960 error = got_repo_open(&repo, repo_path);
964 /* Pre-create work tree path for unveil(2) */
965 error = got_path_mkdir(worktree_path);
967 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
968 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
970 if (!got_path_dir_is_empty(worktree_path)) {
971 error = got_error_path(worktree_path,
972 GOT_ERR_DIR_NOT_EMPTY);
977 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
981 error = got_ref_open(&head_ref, repo, branch_name, 0);
985 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
986 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
989 error = got_worktree_open(&worktree, worktree_path);
993 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
997 if (!same_path_prefix) {
998 error = got_error(GOT_ERR_PATH_PREFIX);
1002 if (commit_id_str) {
1003 struct got_object_id *commit_id;
1004 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1007 error = check_linear_ancestry(commit_id,
1008 got_worktree_get_base_commit_id(worktree), repo);
1009 if (error != NULL) {
1013 error = check_same_branch(commit_id, head_ref, NULL, repo);
1016 error = got_worktree_set_base_commit_id(worktree, repo,
1023 error = got_pathlist_append(&paths, "", NULL);
1026 error = got_worktree_checkout_files(worktree, &paths, repo,
1027 checkout_progress, worktree_path, check_cancelled, NULL);
1031 printf("Now shut up and hack\n");
1034 got_pathlist_free(&paths);
1035 free(commit_id_str);
1037 free(worktree_path);
1044 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1049 static const struct got_error *
1050 update_progress(void *arg, unsigned char status, const char *path)
1052 int *did_something = arg;
1054 if (status == GOT_STATUS_EXISTS)
1059 /* Base commit bump happens silently. */
1060 if (status == GOT_STATUS_BUMP_BASE)
1063 while (path[0] == '/')
1065 printf("%c %s\n", status, path);
1069 static const struct got_error *
1070 switch_head_ref(struct got_reference *head_ref,
1071 struct got_object_id *commit_id, struct got_worktree *worktree,
1072 struct got_repository *repo)
1074 const struct got_error *err = NULL;
1076 int ref_has_moved = 0;
1078 /* Trivial case: switching between two different references. */
1079 if (strcmp(got_ref_get_name(head_ref),
1080 got_worktree_get_head_ref_name(worktree)) != 0) {
1081 printf("Switching work tree from %s to %s\n",
1082 got_worktree_get_head_ref_name(worktree),
1083 got_ref_get_name(head_ref));
1084 return got_worktree_set_head_ref(worktree, head_ref);
1087 err = check_linear_ancestry(commit_id,
1088 got_worktree_get_base_commit_id(worktree), repo);
1090 if (err->code != GOT_ERR_ANCESTRY)
1097 /* Switching to a rebased branch with the same reference name. */
1098 err = got_object_id_str(&base_id_str,
1099 got_worktree_get_base_commit_id(worktree));
1102 printf("Reference %s now points at a different branch\n",
1103 got_worktree_get_head_ref_name(worktree));
1104 printf("Switching work tree from %s to %s\n", base_id_str,
1105 got_worktree_get_head_ref_name(worktree));
1109 static const struct got_error *
1110 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1112 const struct got_error *err;
1115 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1119 return got_error(GOT_ERR_REBASING);
1121 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1125 return got_error(GOT_ERR_HISTEDIT_BUSY);
1130 static const struct got_error *
1131 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1132 char *argv[], struct got_worktree *worktree)
1134 const struct got_error *err = NULL;
1141 return got_error_from_errno("strdup");
1142 return got_pathlist_append(paths, path, NULL);
1145 for (i = 0; i < argc; i++) {
1146 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1149 err = got_pathlist_append(paths, path, NULL);
1159 static const struct got_error *
1160 cmd_update(int argc, char *argv[])
1162 const struct got_error *error = NULL;
1163 struct got_repository *repo = NULL;
1164 struct got_worktree *worktree = NULL;
1165 char *worktree_path = NULL;
1166 struct got_object_id *commit_id = NULL;
1167 char *commit_id_str = NULL;
1168 const char *branch_name = NULL;
1169 struct got_reference *head_ref = NULL;
1170 struct got_pathlist_head paths;
1171 struct got_pathlist_entry *pe;
1172 int ch, did_something = 0;
1176 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1179 branch_name = optarg;
1182 commit_id_str = strdup(optarg);
1183 if (commit_id_str == NULL)
1184 return got_error_from_errno("strdup");
1196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1197 "unveil", NULL) == -1)
1200 worktree_path = getcwd(NULL, 0);
1201 if (worktree_path == NULL) {
1202 error = got_error_from_errno("getcwd");
1205 error = got_worktree_open(&worktree, worktree_path);
1209 error = check_rebase_or_histedit_in_progress(worktree);
1213 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1217 error = apply_unveil(got_repo_get_path(repo), 0,
1218 got_worktree_get_root_path(worktree));
1222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1226 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1227 got_worktree_get_head_ref_name(worktree), 0);
1230 if (commit_id_str == NULL) {
1231 error = got_ref_resolve(&commit_id, repo, head_ref);
1234 error = got_object_id_str(&commit_id_str, commit_id);
1238 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1239 free(commit_id_str);
1240 commit_id_str = NULL;
1243 error = got_object_id_str(&commit_id_str, commit_id);
1249 struct got_object_id *head_commit_id;
1250 TAILQ_FOREACH(pe, &paths, entry) {
1251 if (pe->path_len == 0)
1253 error = got_error_msg(GOT_ERR_BAD_PATH,
1254 "switching between branches requires that "
1255 "the entire work tree gets updated");
1258 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1261 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1262 free(head_commit_id);
1265 error = check_same_branch(commit_id, head_ref, NULL, repo);
1268 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1272 error = check_linear_ancestry(commit_id,
1273 got_worktree_get_base_commit_id(worktree), repo);
1274 if (error != NULL) {
1275 if (error->code == GOT_ERR_ANCESTRY)
1276 error = got_error(GOT_ERR_BRANCH_MOVED);
1279 error = check_same_branch(commit_id, head_ref, NULL, repo);
1284 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1286 error = got_worktree_set_base_commit_id(worktree, repo,
1292 error = got_worktree_checkout_files(worktree, &paths, repo,
1293 update_progress, &did_something, check_cancelled, NULL);
1298 printf("Updated to commit %s\n", commit_id_str);
1300 printf("Already up-to-date\n");
1302 free(worktree_path);
1303 TAILQ_FOREACH(pe, &paths, entry)
1304 free((char *)pe->path);
1305 got_pathlist_free(&paths);
1307 free(commit_id_str);
1311 static const struct got_error *
1312 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1313 int diff_context, struct got_repository *repo)
1315 const struct got_error *err = NULL;
1316 struct got_tree_object *tree1 = NULL, *tree2;
1317 struct got_object_qid *qid;
1318 char *id_str1 = NULL, *id_str2;
1319 struct got_diff_blob_output_unidiff_arg arg;
1321 err = got_object_open_as_tree(&tree2, repo,
1322 got_object_commit_get_tree_id(commit));
1326 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1328 struct got_commit_object *pcommit;
1330 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1334 err = got_object_open_as_tree(&tree1, repo,
1335 got_object_commit_get_tree_id(pcommit));
1336 got_object_commit_close(pcommit);
1340 err = got_object_id_str(&id_str1, qid->id);
1345 err = got_object_id_str(&id_str2, id);
1349 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1350 arg.diff_context = diff_context;
1351 arg.outfile = stdout;
1352 err = got_diff_tree(tree1, tree2, "", "", repo,
1353 got_diff_blob_output_unidiff, &arg, 1);
1356 got_object_tree_close(tree1);
1357 got_object_tree_close(tree2);
1364 get_datestr(time_t *time, char *datebuf)
1366 struct tm mytm, *tm;
1369 tm = gmtime_r(time, &mytm);
1372 s = asctime_r(tm, datebuf);
1375 p = strchr(s, '\n');
1381 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1383 static const struct got_error *
1384 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1385 struct got_repository *repo, int show_patch, int diff_context,
1386 struct got_reflist_head *refs)
1388 const struct got_error *err = NULL;
1389 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1391 time_t committer_time;
1392 const char *author, *committer;
1393 char *refs_str = NULL;
1394 struct got_reflist_entry *re;
1396 SIMPLEQ_FOREACH(re, refs, entry) {
1399 struct got_tag_object *tag = NULL;
1402 name = got_ref_get_name(re->ref);
1403 if (strcmp(name, GOT_REF_HEAD) == 0)
1405 if (strncmp(name, "refs/", 5) == 0)
1407 if (strncmp(name, "got/", 4) == 0)
1409 if (strncmp(name, "heads/", 6) == 0)
1411 if (strncmp(name, "remotes/", 8) == 0)
1413 if (strncmp(name, "tags/", 5) == 0) {
1414 err = got_object_open_as_tag(&tag, repo, re->id);
1416 if (err->code != GOT_ERR_OBJ_TYPE)
1418 /* Ref points at something other than a tag. */
1423 cmp = got_object_id_cmp(tag ?
1424 got_object_tag_get_object_id(tag) : re->id, id);
1426 got_object_tag_close(tag);
1430 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1432 err = got_error_from_errno("asprintf");
1438 err = got_object_id_str(&id_str, id);
1442 printf(GOT_COMMIT_SEP_STR);
1443 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1444 refs_str ? refs_str : "", refs_str ? ")" : "");
1449 printf("from: %s\n", got_object_commit_get_author(commit));
1450 committer_time = got_object_commit_get_committer_time(commit);
1451 datestr = get_datestr(&committer_time, datebuf);
1453 printf("date: %s UTC\n", datestr);
1454 author = got_object_commit_get_author(commit);
1455 committer = got_object_commit_get_committer(commit);
1456 if (strcmp(author, committer) != 0)
1457 printf("via: %s\n", committer);
1458 if (got_object_commit_get_nparents(commit) > 1) {
1459 const struct got_object_id_queue *parent_ids;
1460 struct got_object_qid *qid;
1462 parent_ids = got_object_commit_get_parent_ids(commit);
1463 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1464 err = got_object_id_str(&id_str, qid->id);
1467 printf("parent %d: %s\n", n++, id_str);
1472 err = got_object_commit_get_logmsg(&logmsg0, commit);
1478 line = strsep(&logmsg, "\n");
1480 printf(" %s\n", line);
1485 err = print_patch(commit, id, diff_context, repo);
1490 if (fflush(stdout) != 0 && err == NULL)
1491 err = got_error_from_errno("fflush");
1495 static const struct got_error *
1496 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1497 char *path, int show_patch, int diff_context, int limit,
1498 int first_parent_traversal, struct got_reflist_head *refs)
1500 const struct got_error *err;
1501 struct got_commit_graph *graph;
1503 err = got_commit_graph_open(&graph, root_id, path,
1504 first_parent_traversal, repo);
1507 err = got_commit_graph_iter_start(graph, root_id, repo);
1511 struct got_commit_object *commit;
1512 struct got_object_id *id;
1514 if (sigint_received || sigpipe_received)
1517 err = got_commit_graph_iter_next(&id, graph);
1519 if (err->code == GOT_ERR_ITER_COMPLETED) {
1523 if (err->code != GOT_ERR_ITER_NEED_MORE)
1525 err = got_commit_graph_fetch_commits(graph, 1, repo);
1534 err = got_object_open_as_commit(&commit, repo, id);
1537 err = print_commit(commit, id, repo, show_patch, diff_context,
1539 got_object_commit_close(commit);
1540 if (err || (limit && --limit == 0))
1544 got_commit_graph_close(graph);
1551 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1552 "[-r repository-path] [path]\n", getprogname());
1557 get_default_log_limit(void)
1559 const char *got_default_log_limit;
1563 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1564 if (got_default_log_limit == NULL)
1566 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1572 static const struct got_error *
1573 cmd_log(int argc, char *argv[])
1575 const struct got_error *error;
1576 struct got_repository *repo = NULL;
1577 struct got_worktree *worktree = NULL;
1578 struct got_commit_object *commit = NULL;
1579 struct got_object_id *id = NULL;
1580 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1581 char *start_commit = NULL;
1582 int diff_context = 3, ch;
1583 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1585 struct got_reflist_head refs;
1587 SIMPLEQ_INIT(&refs);
1590 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1596 limit = get_default_log_limit();
1598 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1604 start_commit = optarg;
1607 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1610 err(1, "-C option %s", errstr);
1613 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1615 err(1, "-l option %s", errstr);
1618 first_parent_traversal = 1;
1621 repo_path = realpath(optarg, NULL);
1622 if (repo_path == NULL)
1623 err(1, "-r option");
1624 got_path_strip_trailing_slashes(repo_path);
1635 cwd = getcwd(NULL, 0);
1637 error = got_error_from_errno("getcwd");
1641 error = got_worktree_open(&worktree, cwd);
1642 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1649 error = got_error_from_errno("strdup");
1652 } else if (argc == 1) {
1654 error = got_worktree_resolve_path(&path, worktree,
1659 path = strdup(argv[0]);
1661 error = got_error_from_errno("strdup");
1668 if (repo_path == NULL) {
1669 repo_path = worktree ?
1670 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1672 if (repo_path == NULL) {
1673 error = got_error_from_errno("strdup");
1677 error = got_repo_open(&repo, repo_path);
1681 error = apply_unveil(got_repo_get_path(repo), 1,
1682 worktree ? got_worktree_get_root_path(worktree) : NULL);
1686 if (start_commit == NULL) {
1687 struct got_reference *head_ref;
1688 error = got_ref_open(&head_ref, repo,
1689 worktree ? got_worktree_get_head_ref_name(worktree)
1693 error = got_ref_resolve(&id, repo, head_ref);
1694 got_ref_close(head_ref);
1697 error = got_object_open_as_commit(&commit, repo, id);
1699 struct got_reference *ref;
1700 error = got_ref_open(&ref, repo, start_commit, 0);
1701 if (error == NULL) {
1703 error = got_ref_resolve(&id, repo, ref);
1707 error = got_object_get_type(&obj_type, repo, id);
1710 if (obj_type == GOT_OBJ_TYPE_TAG) {
1711 struct got_tag_object *tag;
1712 error = got_object_open_as_tag(&tag, repo, id);
1715 if (got_object_tag_get_object_type(tag) !=
1716 GOT_OBJ_TYPE_COMMIT) {
1717 got_object_tag_close(tag);
1718 error = got_error(GOT_ERR_OBJ_TYPE);
1722 id = got_object_id_dup(
1723 got_object_tag_get_object_id(tag));
1725 error = got_error_from_errno(
1726 "got_object_id_dup");
1727 got_object_tag_close(tag);
1730 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1731 error = got_error(GOT_ERR_OBJ_TYPE);
1734 error = got_object_open_as_commit(&commit, repo, id);
1738 if (commit == NULL) {
1739 error = got_repo_match_object_id_prefix(&id,
1740 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1748 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1753 path = in_repo_path;
1756 error = got_ref_list(&refs, repo);
1760 error = print_commits(id, repo, path, show_patch,
1761 diff_context, limit, first_parent_traversal, &refs);
1768 got_worktree_close(worktree);
1770 const struct got_error *repo_error;
1771 repo_error = got_repo_close(repo);
1775 got_ref_list_free(&refs);
1782 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1783 "[object1 object2 | path]\n", getprogname());
1787 struct print_diff_arg {
1788 struct got_repository *repo;
1789 struct got_worktree *worktree;
1796 static const struct got_error *
1797 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1798 const char *path, struct got_object_id *blob_id,
1799 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1801 struct print_diff_arg *a = arg;
1802 const struct got_error *err = NULL;
1803 struct got_blob_object *blob1 = NULL;
1805 char *abspath = NULL, *label1 = NULL;
1808 if (a->diff_staged) {
1809 if (staged_status != GOT_STATUS_MODIFY &&
1810 staged_status != GOT_STATUS_ADD &&
1811 staged_status != GOT_STATUS_DELETE)
1814 if (staged_status == GOT_STATUS_DELETE)
1816 if (status != GOT_STATUS_MODIFY &&
1817 status != GOT_STATUS_ADD &&
1818 status != GOT_STATUS_DELETE &&
1819 status != GOT_STATUS_CONFLICT)
1823 if (!a->header_shown) {
1824 printf("diff %s %s%s\n", a->id_str,
1825 got_worktree_get_root_path(a->worktree),
1826 a->diff_staged ? " (staged changes)" : "");
1827 a->header_shown = 1;
1830 if (a->diff_staged) {
1831 const char *label1 = NULL, *label2 = NULL;
1832 switch (staged_status) {
1833 case GOT_STATUS_MODIFY:
1837 case GOT_STATUS_ADD:
1840 case GOT_STATUS_DELETE:
1844 return got_error(GOT_ERR_FILE_STATUS);
1846 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1847 label1, label2, a->diff_context, a->repo, stdout);
1850 if (staged_status == GOT_STATUS_ADD ||
1851 staged_status == GOT_STATUS_MODIFY) {
1853 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1857 err = got_object_id_str(&id_str, staged_blob_id);
1860 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1861 err = got_error_from_errno("asprintf");
1866 } else if (status != GOT_STATUS_ADD) {
1867 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1872 if (status != GOT_STATUS_DELETE) {
1873 if (asprintf(&abspath, "%s/%s",
1874 got_worktree_get_root_path(a->worktree), path) == -1) {
1875 err = got_error_from_errno("asprintf");
1879 f2 = fopen(abspath, "r");
1881 err = got_error_from_errno2("fopen", abspath);
1884 if (lstat(abspath, &sb) == -1) {
1885 err = got_error_from_errno2("lstat", abspath);
1891 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1892 a->diff_context, stdout);
1895 got_object_blob_close(blob1);
1896 if (f2 && fclose(f2) != 0 && err == NULL)
1897 err = got_error_from_errno("fclose");
1902 static const struct got_error *
1903 match_object_id(struct got_object_id **id, char **label,
1904 const char *id_str, int obj_type, struct got_repository *repo)
1906 const struct got_error *err;
1907 struct got_tag_object *tag;
1908 struct got_reference *ref = NULL;
1913 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1915 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1917 err = got_error_from_errno("got_object_id_dup");
1918 if (asprintf(label, "refs/tags/%s",
1919 got_object_tag_get_name(tag)) == -1)
1920 err = got_error_from_errno("asprintf");
1921 got_object_tag_close(tag);
1923 } else if (err->code != GOT_ERR_NO_OBJ)
1926 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1928 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1930 err = got_ref_open(&ref, repo, id_str, 0);
1933 *label = strdup(got_ref_get_name(ref));
1934 if (*label == NULL) {
1935 err = got_error_from_errno("strdup");
1938 err = got_ref_resolve(id, repo, ref);
1940 err = got_object_id_str(label, *id);
1941 if (*label == NULL) {
1942 err = got_error_from_errno("strdup");
1953 static const struct got_error *
1954 cmd_diff(int argc, char *argv[])
1956 const struct got_error *error;
1957 struct got_repository *repo = NULL;
1958 struct got_worktree *worktree = NULL;
1959 char *cwd = NULL, *repo_path = NULL;
1960 struct got_object_id *id1 = NULL, *id2 = NULL;
1961 const char *id_str1 = NULL, *id_str2 = NULL;
1962 char *label1 = NULL, *label2 = NULL;
1964 int diff_context = 3, diff_staged = 0, ch;
1969 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1974 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1977 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1979 err(1, "-C option %s", errstr);
1982 repo_path = realpath(optarg, NULL);
1983 if (repo_path == NULL)
1984 err(1, "-r option");
1985 got_path_strip_trailing_slashes(repo_path);
1999 cwd = getcwd(NULL, 0);
2001 error = got_error_from_errno("getcwd");
2004 error = got_worktree_open(&worktree, cwd);
2005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2008 if (worktree == NULL) {
2009 error = got_error(GOT_ERR_NOT_WORKTREE);
2014 "-r option can't be used when diffing a work tree");
2015 repo_path = strdup(got_worktree_get_repo_path(worktree));
2016 if (repo_path == NULL) {
2017 error = got_error_from_errno("strdup");
2021 error = got_worktree_resolve_path(&path, worktree,
2028 error = got_error_from_errno("strdup");
2032 } else if (argc == 2) {
2034 errx(1, "-s option can't be used when diffing "
2035 "objects in repository");
2038 if (worktree && repo_path == NULL) {
2040 strdup(got_worktree_get_repo_path(worktree));
2041 if (repo_path == NULL) {
2042 error = got_error_from_errno("strdup");
2049 if (repo_path == NULL) {
2050 repo_path = getcwd(NULL, 0);
2051 if (repo_path == NULL)
2052 return got_error_from_errno("getcwd");
2055 error = got_repo_open(&repo, repo_path);
2060 error = apply_unveil(got_repo_get_path(repo), 1,
2061 worktree ? got_worktree_get_root_path(worktree) : NULL);
2066 struct print_diff_arg arg;
2067 struct got_pathlist_head paths;
2072 error = got_object_id_str(&id_str,
2073 got_worktree_get_base_commit_id(worktree));
2077 arg.worktree = worktree;
2078 arg.diff_context = diff_context;
2079 arg.id_str = id_str;
2080 arg.header_shown = 0;
2081 arg.diff_staged = diff_staged;
2083 error = got_pathlist_append(&paths, path, NULL);
2087 error = got_worktree_status(worktree, &paths, repo, print_diff,
2088 &arg, check_cancelled, NULL);
2090 got_pathlist_free(&paths);
2094 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2098 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2102 error = got_object_get_type(&type1, repo, id1);
2106 error = got_object_get_type(&type2, repo, id2);
2110 if (type1 != type2) {
2111 error = got_error(GOT_ERR_OBJ_TYPE);
2116 case GOT_OBJ_TYPE_BLOB:
2117 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2118 diff_context, repo, stdout);
2120 case GOT_OBJ_TYPE_TREE:
2121 error = got_diff_objects_as_trees(id1, id2, "", "",
2122 diff_context, repo, stdout);
2124 case GOT_OBJ_TYPE_COMMIT:
2125 printf("diff %s %s\n", label1, label2);
2126 error = got_diff_objects_as_commits(id1, id2, diff_context,
2130 error = got_error(GOT_ERR_OBJ_TYPE);
2140 got_worktree_close(worktree);
2142 const struct got_error *repo_error;
2143 repo_error = got_repo_close(repo);
2154 "usage: %s blame [-c commit] [-r repository-path] path\n",
2163 char datebuf[9]; /* YY-MM-DD + NUL */
2166 struct blame_cb_args {
2167 struct blame_line *lines;
2171 off_t *line_offsets;
2173 struct got_repository *repo;
2176 static const struct got_error *
2177 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2179 const struct got_error *err = NULL;
2180 struct blame_cb_args *a = arg;
2181 struct blame_line *bline;
2183 size_t linesize = 0;
2184 struct got_commit_object *commit = NULL;
2187 time_t committer_time;
2189 if (nlines != a->nlines ||
2190 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2191 return got_error(GOT_ERR_RANGE);
2193 if (sigint_received)
2194 return got_error(GOT_ERR_ITER_COMPLETED);
2197 return NULL; /* no change in this commit */
2199 /* Annotate this line. */
2200 bline = &a->lines[lineno - 1];
2201 if (bline->annotated)
2203 err = got_object_id_str(&bline->id_str, id);
2207 err = got_object_open_as_commit(&commit, a->repo, id);
2211 bline->committer = strdup(got_object_commit_get_committer(commit));
2212 if (bline->committer == NULL) {
2213 err = got_error_from_errno("strdup");
2217 committer_time = got_object_commit_get_committer_time(commit);
2218 if (localtime_r(&committer_time, &tm) == NULL)
2219 return got_error_from_errno("localtime_r");
2220 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2221 &tm) >= sizeof(bline->datebuf)) {
2222 err = got_error(GOT_ERR_NO_SPACE);
2225 bline->annotated = 1;
2227 /* Print lines annotated so far. */
2228 bline = &a->lines[a->lineno_cur - 1];
2229 if (!bline->annotated)
2232 offset = a->line_offsets[a->lineno_cur - 1];
2233 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2234 err = got_error_from_errno("fseeko");
2238 while (bline->annotated) {
2239 char *smallerthan, *at, *nl, *committer;
2242 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2244 err = got_error_from_errno("getline");
2248 committer = bline->committer;
2249 smallerthan = strchr(committer, '<');
2250 if (smallerthan && smallerthan[1] != '\0')
2251 committer = smallerthan + 1;
2252 at = strchr(committer, '@');
2255 len = strlen(committer);
2257 committer[8] = '\0';
2259 nl = strchr(line, '\n');
2262 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2263 bline->id_str, bline->datebuf, committer, line);
2266 bline = &a->lines[a->lineno_cur - 1];
2270 got_object_commit_close(commit);
2275 static const struct got_error *
2276 cmd_blame(int argc, char *argv[])
2278 const struct got_error *error;
2279 struct got_repository *repo = NULL;
2280 struct got_worktree *worktree = NULL;
2281 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2282 struct got_object_id *obj_id = NULL;
2283 struct got_object_id *commit_id = NULL;
2284 struct got_blob_object *blob = NULL;
2285 char *commit_id_str = NULL;
2286 struct blame_cb_args bca;
2287 int ch, obj_type, i;
2291 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2296 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2299 commit_id_str = optarg;
2302 repo_path = realpath(optarg, NULL);
2303 if (repo_path == NULL)
2304 err(1, "-r option");
2305 got_path_strip_trailing_slashes(repo_path);
2321 cwd = getcwd(NULL, 0);
2323 error = got_error_from_errno("getcwd");
2326 if (repo_path == NULL) {
2327 error = got_worktree_open(&worktree, cwd);
2328 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2334 strdup(got_worktree_get_repo_path(worktree));
2335 if (repo_path == NULL)
2336 error = got_error_from_errno("strdup");
2340 repo_path = strdup(cwd);
2341 if (repo_path == NULL) {
2342 error = got_error_from_errno("strdup");
2348 error = got_repo_open(&repo, repo_path);
2352 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2357 const char *prefix = got_worktree_get_path_prefix(worktree);
2358 char *p, *worktree_subdir = cwd +
2359 strlen(got_worktree_get_root_path(worktree));
2360 if (asprintf(&p, "%s%s%s%s%s",
2361 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2362 worktree_subdir, worktree_subdir[0] ? "/" : "",
2364 error = got_error_from_errno("asprintf");
2367 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2370 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2375 if (commit_id_str == NULL) {
2376 struct got_reference *head_ref;
2377 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2380 error = got_ref_resolve(&commit_id, repo, head_ref);
2381 got_ref_close(head_ref);
2385 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2390 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2393 if (obj_id == NULL) {
2394 error = got_error(GOT_ERR_NO_OBJ);
2398 error = got_object_get_type(&obj_type, repo, obj_id);
2402 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2403 error = got_error(GOT_ERR_OBJ_TYPE);
2407 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2410 bca.f = got_opentemp();
2411 if (bca.f == NULL) {
2412 error = got_error_from_errno("got_opentemp");
2415 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2416 &bca.line_offsets, bca.f, blob);
2417 if (error || bca.nlines == 0)
2420 /* Don't include \n at EOF in the blame line count. */
2421 if (bca.line_offsets[bca.nlines - 1] == filesize)
2424 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2425 if (bca.lines == NULL) {
2426 error = got_error_from_errno("calloc");
2430 bca.nlines_prec = 0;
2438 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2448 got_object_blob_close(blob);
2450 got_worktree_close(worktree);
2452 const struct got_error *repo_error;
2453 repo_error = got_repo_close(repo);
2457 for (i = 0; i < bca.nlines; i++) {
2458 struct blame_line *bline = &bca.lines[i];
2459 free(bline->id_str);
2460 free(bline->committer);
2463 free(bca.line_offsets);
2464 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2465 error = got_error_from_errno("fclose");
2473 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2479 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2480 const char *root_path)
2482 int is_root_path = (strcmp(path, root_path) == 0);
2483 const char *modestr = "";
2485 path += strlen(root_path);
2486 while (path[0] == '/')
2489 if (S_ISLNK(te->mode))
2491 else if (S_ISDIR(te->mode))
2493 else if (te->mode & S_IXUSR)
2496 printf("%s%s%s%s%s\n", id ? id : "", path,
2497 is_root_path ? "" : "/", te->name, modestr);
2500 static const struct got_error *
2501 print_tree(const char *path, struct got_object_id *commit_id,
2502 int show_ids, int recurse, const char *root_path,
2503 struct got_repository *repo)
2505 const struct got_error *err = NULL;
2506 struct got_object_id *tree_id = NULL;
2507 struct got_tree_object *tree = NULL;
2508 const struct got_tree_entries *entries;
2509 struct got_tree_entry *te;
2511 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2515 err = got_object_open_as_tree(&tree, repo, tree_id);
2518 entries = got_object_tree_get_entries(tree);
2519 te = SIMPLEQ_FIRST(&entries->head);
2523 if (sigint_received || sigpipe_received)
2528 err = got_object_id_str(&id_str, te->id);
2531 if (asprintf(&id, "%s ", id_str) == -1) {
2532 err = got_error_from_errno("asprintf");
2538 print_entry(te, id, path, root_path);
2541 if (recurse && S_ISDIR(te->mode)) {
2543 if (asprintf(&child_path, "%s%s%s", path,
2544 path[0] == '/' && path[1] == '\0' ? "" : "/",
2546 err = got_error_from_errno("asprintf");
2549 err = print_tree(child_path, commit_id, show_ids, 1,
2556 te = SIMPLEQ_NEXT(te, entry);
2560 got_object_tree_close(tree);
2565 static const struct got_error *
2566 cmd_tree(int argc, char *argv[])
2568 const struct got_error *error;
2569 struct got_repository *repo = NULL;
2570 struct got_worktree *worktree = NULL;
2572 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2573 struct got_object_id *commit_id = NULL;
2574 char *commit_id_str = NULL;
2575 int show_ids = 0, recurse = 0;
2579 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2584 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2587 commit_id_str = optarg;
2590 repo_path = realpath(optarg, NULL);
2591 if (repo_path == NULL)
2592 err(1, "-r option");
2593 got_path_strip_trailing_slashes(repo_path);
2617 cwd = getcwd(NULL, 0);
2619 error = got_error_from_errno("getcwd");
2622 if (repo_path == NULL) {
2623 error = got_worktree_open(&worktree, cwd);
2624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2630 strdup(got_worktree_get_repo_path(worktree));
2631 if (repo_path == NULL)
2632 error = got_error_from_errno("strdup");
2636 repo_path = strdup(cwd);
2637 if (repo_path == NULL) {
2638 error = got_error_from_errno("strdup");
2644 error = got_repo_open(&repo, repo_path);
2648 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2654 char *p, *worktree_subdir = cwd +
2655 strlen(got_worktree_get_root_path(worktree));
2656 if (asprintf(&p, "%s/%s",
2657 got_worktree_get_path_prefix(worktree),
2658 worktree_subdir) == -1) {
2659 error = got_error_from_errno("asprintf");
2662 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2669 if (in_repo_path == NULL) {
2670 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2675 if (commit_id_str == NULL) {
2676 struct got_reference *head_ref;
2677 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2680 error = got_ref_resolve(&commit_id, repo, head_ref);
2681 got_ref_close(head_ref);
2685 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2690 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2691 in_repo_path, repo);
2698 got_worktree_close(worktree);
2700 const struct got_error *repo_error;
2701 repo_error = got_repo_close(repo);
2711 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2715 static const struct got_error *
2716 print_status(void *arg, unsigned char status, unsigned char staged_status,
2717 const char *path, struct got_object_id *blob_id,
2718 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2720 if (status == staged_status && (status == GOT_STATUS_DELETE))
2721 status = GOT_STATUS_NO_CHANGE;
2722 printf("%c%c %s\n", status, staged_status, path);
2726 static const struct got_error *
2727 cmd_status(int argc, char *argv[])
2729 const struct got_error *error = NULL;
2730 struct got_repository *repo = NULL;
2731 struct got_worktree *worktree = NULL;
2733 struct got_pathlist_head paths;
2734 struct got_pathlist_entry *pe;
2739 while ((ch = getopt(argc, argv, "")) != -1) {
2751 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2755 cwd = getcwd(NULL, 0);
2757 error = got_error_from_errno("getcwd");
2761 error = got_worktree_open(&worktree, cwd);
2765 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2769 error = apply_unveil(got_repo_get_path(repo), 1,
2770 got_worktree_get_root_path(worktree));
2774 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2778 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2779 check_cancelled, NULL);
2781 TAILQ_FOREACH(pe, &paths, entry)
2782 free((char *)pe->path);
2783 got_pathlist_free(&paths);
2792 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2797 static const struct got_error *
2798 list_refs(struct got_repository *repo)
2800 static const struct got_error *err = NULL;
2801 struct got_reflist_head refs;
2802 struct got_reflist_entry *re;
2804 SIMPLEQ_INIT(&refs);
2805 err = got_ref_list(&refs, repo);
2809 SIMPLEQ_FOREACH(re, &refs, entry) {
2811 refstr = got_ref_to_str(re->ref);
2813 return got_error_from_errno("got_ref_to_str");
2814 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2818 got_ref_list_free(&refs);
2822 static const struct got_error *
2823 delete_ref(struct got_repository *repo, const char *refname)
2825 const struct got_error *err = NULL;
2826 struct got_reference *ref;
2828 err = got_ref_open(&ref, repo, refname, 0);
2832 err = got_ref_delete(ref, repo);
2837 static const struct got_error *
2838 add_ref(struct got_repository *repo, const char *refname, const char *target)
2840 const struct got_error *err = NULL;
2841 struct got_object_id *id;
2842 struct got_reference *ref = NULL;
2845 * Don't let the user create a reference named '-'.
2846 * While technically a valid reference name, this case is usually
2847 * an unintended typo.
2849 if (refname[0] == '-' && refname[1] == '\0')
2850 return got_error(GOT_ERR_BAD_REF_NAME);
2852 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2855 struct got_reference *target_ref;
2857 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2859 err = got_ref_open(&target_ref, repo, target, 0);
2862 err = got_ref_resolve(&id, repo, target_ref);
2863 got_ref_close(target_ref);
2868 err = got_ref_alloc(&ref, refname, id);
2872 err = got_ref_write(ref, repo);
2880 static const struct got_error *
2881 add_symref(struct got_repository *repo, const char *refname, const char *target)
2883 const struct got_error *err = NULL;
2884 struct got_reference *ref = NULL;
2885 struct got_reference *target_ref = NULL;
2888 * Don't let the user create a reference named '-'.
2889 * While technically a valid reference name, this case is usually
2890 * an unintended typo.
2892 if (refname[0] == '-' && refname[1] == '\0')
2893 return got_error(GOT_ERR_BAD_REF_NAME);
2895 err = got_ref_open(&target_ref, repo, target, 0);
2899 err = got_ref_alloc_symref(&ref, refname, target_ref);
2903 err = got_ref_write(ref, repo);
2906 got_ref_close(target_ref);
2912 static const struct got_error *
2913 cmd_ref(int argc, char *argv[])
2915 const struct got_error *error = NULL;
2916 struct got_repository *repo = NULL;
2917 struct got_worktree *worktree = NULL;
2918 char *cwd = NULL, *repo_path = NULL;
2919 int ch, do_list = 0, create_symref = 0;
2920 const char *delref = NULL;
2922 /* TODO: Add -s option for adding symbolic references. */
2923 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2929 repo_path = realpath(optarg, NULL);
2930 if (repo_path == NULL)
2931 err(1, "-r option");
2932 got_path_strip_trailing_slashes(repo_path);
2946 if (do_list && delref)
2947 errx(1, "-l and -d options are mutually exclusive\n");
2952 if (do_list || delref) {
2954 errx(1, "-s option cannot be used together with the "
2955 "-l or -d options");
2958 } else if (argc != 2)
2963 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2968 "sendfd unveil", NULL) == -1)
2972 cwd = getcwd(NULL, 0);
2974 error = got_error_from_errno("getcwd");
2978 if (repo_path == NULL) {
2979 error = got_worktree_open(&worktree, cwd);
2980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2986 strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL)
2988 error = got_error_from_errno("strdup");
2992 repo_path = strdup(cwd);
2993 if (repo_path == NULL) {
2994 error = got_error_from_errno("strdup");
3000 error = got_repo_open(&repo, repo_path);
3004 error = apply_unveil(got_repo_get_path(repo), do_list,
3005 worktree ? got_worktree_get_root_path(worktree) : NULL);
3010 error = list_refs(repo);
3012 error = delete_ref(repo, delref);
3013 else if (create_symref)
3014 error = add_symref(repo, argv[0], argv[1]);
3016 error = add_ref(repo, argv[0], argv[1]);
3019 got_repo_close(repo);
3021 got_worktree_close(worktree);
3031 "usage: %s branch [-r repository] -l | -d name | "
3032 "name [base-branch]\n", getprogname());
3036 static const struct got_error *
3037 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3039 static const struct got_error *err = NULL;
3040 struct got_reflist_head refs;
3041 struct got_reflist_entry *re;
3043 SIMPLEQ_INIT(&refs);
3045 err = got_ref_list(&refs, repo);
3049 SIMPLEQ_FOREACH(re, &refs, entry) {
3050 const char *refname, *marker = " ";
3052 refname = got_ref_get_name(re->ref);
3053 if (strncmp(refname, "refs/heads/", 11) != 0)
3055 if (worktree && strcmp(refname,
3056 got_worktree_get_head_ref_name(worktree)) == 0) {
3057 struct got_object_id *id = NULL;
3058 err = got_ref_resolve(&id, repo, re->ref);
3061 if (got_object_id_cmp(id,
3062 got_worktree_get_base_commit_id(worktree)) == 0)
3069 refstr = got_ref_to_str(re->ref);
3071 return got_error_from_errno("got_ref_to_str");
3072 printf("%s%s: %s\n", marker, refname, refstr);
3076 got_ref_list_free(&refs);
3080 static const struct got_error *
3081 delete_branch(struct got_repository *repo, const char *branch_name)
3083 const struct got_error *err = NULL;
3084 struct got_reference *ref;
3087 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3088 return got_error_from_errno("asprintf");
3090 err = got_ref_open(&ref, repo, refname, 0);
3094 err = got_ref_delete(ref, repo);
3101 static const struct got_error *
3102 add_branch(struct got_repository *repo, const char *branch_name,
3103 const char *base_branch)
3105 const struct got_error *err = NULL;
3106 struct got_object_id *id = NULL;
3107 struct got_reference *ref = NULL;
3108 char *base_refname = NULL, *refname = NULL;
3109 struct got_reference *base_ref;
3112 * Don't let the user create a branch named '-'.
3113 * While technically a valid reference name, this case is usually
3114 * an unintended typo.
3116 if (branch_name[0] == '-' && branch_name[1] == '\0')
3117 return got_error(GOT_ERR_BAD_REF_NAME);
3119 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3120 base_refname = strdup(GOT_REF_HEAD);
3121 if (base_refname == NULL)
3122 return got_error_from_errno("strdup");
3123 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3124 return got_error_from_errno("asprintf");
3126 err = got_ref_open(&base_ref, repo, base_refname, 0);
3129 err = got_ref_resolve(&id, repo, base_ref);
3130 got_ref_close(base_ref);
3134 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3135 err = got_error_from_errno("asprintf");
3139 err = got_ref_open(&ref, repo, refname, 0);
3141 err = got_error(GOT_ERR_BRANCH_EXISTS);
3143 } else if (err->code != GOT_ERR_NOT_REF)
3146 err = got_ref_alloc(&ref, refname, id);
3150 err = got_ref_write(ref, repo);
3160 static const struct got_error *
3161 cmd_branch(int argc, char *argv[])
3163 const struct got_error *error = NULL;
3164 struct got_repository *repo = NULL;
3165 struct got_worktree *worktree = NULL;
3166 char *cwd = NULL, *repo_path = NULL;
3167 int ch, do_list = 0;
3168 const char *delref = NULL;
3170 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3176 repo_path = realpath(optarg, NULL);
3177 if (repo_path == NULL)
3178 err(1, "-r option");
3179 got_path_strip_trailing_slashes(repo_path);
3190 if (do_list && delref)
3191 errx(1, "-l and -d options are mutually exclusive\n");
3196 if (do_list || delref) {
3199 } else if (argc < 1 || argc > 2)
3204 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3208 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3209 "sendfd unveil", NULL) == -1)
3213 cwd = getcwd(NULL, 0);
3215 error = got_error_from_errno("getcwd");
3219 if (repo_path == NULL) {
3220 error = got_worktree_open(&worktree, cwd);
3221 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3227 strdup(got_worktree_get_repo_path(worktree));
3228 if (repo_path == NULL)
3229 error = got_error_from_errno("strdup");
3233 repo_path = strdup(cwd);
3234 if (repo_path == NULL) {
3235 error = got_error_from_errno("strdup");
3241 error = got_repo_open(&repo, repo_path);
3245 error = apply_unveil(got_repo_get_path(repo), do_list,
3246 worktree ? got_worktree_get_root_path(worktree) : NULL);
3251 error = list_branches(repo, worktree);
3253 error = delete_branch(repo, delref);
3255 const char *base_branch;
3257 base_branch = worktree ?
3258 got_worktree_get_head_ref_name(worktree) :
3260 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3263 base_branch = argv[1];
3264 error = add_branch(repo, argv[0], base_branch);
3268 got_repo_close(repo);
3270 got_worktree_close(worktree);
3279 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3283 static const struct got_error *
3284 cmd_add(int argc, char *argv[])
3286 const struct got_error *error = NULL;
3287 struct got_repository *repo = NULL;
3288 struct got_worktree *worktree = NULL;
3290 struct got_pathlist_head paths;
3291 struct got_pathlist_entry *pe;
3296 while ((ch = getopt(argc, argv, "")) != -1) {
3308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3315 cwd = getcwd(NULL, 0);
3317 error = got_error_from_errno("getcwd");
3321 error = got_worktree_open(&worktree, cwd);
3325 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3329 error = apply_unveil(got_repo_get_path(repo), 1,
3330 got_worktree_get_root_path(worktree));
3334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3338 error = got_worktree_schedule_add(worktree, &paths, print_status,
3342 got_repo_close(repo);
3344 got_worktree_close(worktree);
3345 TAILQ_FOREACH(pe, &paths, entry)
3346 free((char *)pe->path);
3347 got_pathlist_free(&paths);
3355 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3359 static const struct got_error *
3360 cmd_remove(int argc, char *argv[])
3362 const struct got_error *error = NULL;
3363 struct got_worktree *worktree = NULL;
3364 struct got_repository *repo = NULL;
3366 struct got_pathlist_head paths;
3367 struct got_pathlist_entry *pe;
3368 int ch, delete_local_mods = 0;
3372 while ((ch = getopt(argc, argv, "f")) != -1) {
3375 delete_local_mods = 1;
3387 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3394 cwd = getcwd(NULL, 0);
3396 error = got_error_from_errno("getcwd");
3399 error = got_worktree_open(&worktree, cwd);
3403 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3407 error = apply_unveil(got_repo_get_path(repo), 1,
3408 got_worktree_get_root_path(worktree));
3412 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3416 error = got_worktree_schedule_delete(worktree, &paths,
3417 delete_local_mods, print_status, NULL, repo);
3422 got_repo_close(repo);
3424 got_worktree_close(worktree);
3425 TAILQ_FOREACH(pe, &paths, entry)
3426 free((char *)pe->path);
3427 got_pathlist_free(&paths);
3435 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3436 "path ...\n", getprogname());
3440 static const struct got_error *
3441 revert_progress(void *arg, unsigned char status, const char *path)
3443 while (path[0] == '/')
3445 printf("%c %s\n", status, path);
3449 struct choose_patch_arg {
3450 FILE *patch_script_file;
3454 static const struct got_error *
3455 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3456 int nchanges, const char *action)
3459 size_t linesize = 0;
3463 case GOT_STATUS_ADD:
3464 printf("A %s\n%s this addition? [y/n] ", path, action);
3466 case GOT_STATUS_DELETE:
3467 printf("D %s\n%s this deletion? [y/n] ", path, action);
3469 case GOT_STATUS_MODIFY:
3470 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3471 return got_error_from_errno("fseek");
3472 printf(GOT_COMMIT_SEP_STR);
3473 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3475 if (ferror(patch_file))
3476 return got_error_from_errno("getline");
3477 printf(GOT_COMMIT_SEP_STR);
3478 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3479 path, n, nchanges, action);
3482 return got_error_path(path, GOT_ERR_FILE_STATUS);
3488 static const struct got_error *
3489 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3490 FILE *patch_file, int n, int nchanges)
3492 const struct got_error *err = NULL;
3494 size_t linesize = 0;
3497 struct choose_patch_arg *a = arg;
3499 *choice = GOT_PATCH_CHOICE_NONE;
3501 if (a->patch_script_file) {
3503 err = show_change(status, path, patch_file, n, nchanges,
3507 linelen = getline(&line, &linesize, a->patch_script_file);
3508 if (linelen == -1) {
3509 if (ferror(a->patch_script_file))
3510 return got_error_from_errno("getline");
3513 nl = strchr(line, '\n');
3516 if (strcmp(line, "y") == 0) {
3517 *choice = GOT_PATCH_CHOICE_YES;
3519 } else if (strcmp(line, "n") == 0) {
3520 *choice = GOT_PATCH_CHOICE_NO;
3522 } else if (strcmp(line, "q") == 0 &&
3523 status == GOT_STATUS_MODIFY) {
3524 *choice = GOT_PATCH_CHOICE_QUIT;
3527 printf("invalid response '%s'\n", line);
3532 while (resp != 'y' && resp != 'n' && resp != 'q') {
3533 err = show_change(status, path, patch_file, n, nchanges,
3540 if (status == GOT_STATUS_MODIFY) {
3541 if (resp != 'y' && resp != 'n' && resp != 'q') {
3542 printf("invalid response '%c'\n", resp);
3545 } else if (resp != 'y' && resp != 'n') {
3546 printf("invalid response '%c'\n", resp);
3552 *choice = GOT_PATCH_CHOICE_YES;
3553 else if (resp == 'n')
3554 *choice = GOT_PATCH_CHOICE_NO;
3555 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3556 *choice = GOT_PATCH_CHOICE_QUIT;
3562 static const struct got_error *
3563 cmd_revert(int argc, char *argv[])
3565 const struct got_error *error = NULL;
3566 struct got_worktree *worktree = NULL;
3567 struct got_repository *repo = NULL;
3568 char *cwd = NULL, *path = NULL;
3569 struct got_pathlist_head paths;
3570 struct got_pathlist_entry *pe;
3571 int ch, can_recurse = 0, pflag = 0;
3572 FILE *patch_script_file = NULL;
3573 const char *patch_script_path = NULL;
3574 struct choose_patch_arg cpa;
3578 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3584 patch_script_path = optarg;
3599 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3600 "unveil", NULL) == -1)
3605 if (patch_script_path && !pflag)
3606 errx(1, "-F option can only be used together with -p option");
3608 cwd = getcwd(NULL, 0);
3610 error = got_error_from_errno("getcwd");
3613 error = got_worktree_open(&worktree, cwd);
3617 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3621 if (patch_script_path) {
3622 patch_script_file = fopen(patch_script_path, "r");
3623 if (patch_script_file == NULL) {
3624 error = got_error_from_errno2("fopen",
3629 error = apply_unveil(got_repo_get_path(repo), 1,
3630 got_worktree_get_root_path(worktree));
3634 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3641 TAILQ_FOREACH(pe, &paths, entry) {
3642 if (asprintf(&ondisk_path, "%s/%s",
3643 got_worktree_get_root_path(worktree),
3645 error = got_error_from_errno("asprintf");
3648 if (lstat(ondisk_path, &sb) == -1) {
3649 if (errno == ENOENT) {
3653 error = got_error_from_errno2("lstat",
3659 if (S_ISDIR(sb.st_mode)) {
3660 error = got_error_msg(GOT_ERR_BAD_PATH,
3661 "reverting directories requires -R option");
3667 cpa.patch_script_file = patch_script_file;
3668 cpa.action = "revert";
3669 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3670 pflag ? choose_patch : NULL, &cpa, repo);
3674 if (patch_script_file && fclose(patch_script_file) == EOF &&
3676 error = got_error_from_errno2("fclose", patch_script_path);
3678 got_repo_close(repo);
3680 got_worktree_close(worktree);
3689 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3694 struct collect_commit_logmsg_arg {
3695 const char *cmdline_log;
3697 const char *worktree_path;
3698 const char *branch_name;
3699 const char *repo_path;
3704 static const struct got_error *
3705 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3708 char *initial_content = NULL;
3709 struct got_pathlist_entry *pe;
3710 const struct got_error *err = NULL;
3711 char *template = NULL;
3712 struct collect_commit_logmsg_arg *a = arg;
3716 /* if a message was specified on the command line, just use it */
3717 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3718 len = strlen(a->cmdline_log) + 1;
3719 *logmsg = malloc(len + 1);
3720 if (*logmsg == NULL)
3721 return got_error_from_errno("malloc");
3722 strlcpy(*logmsg, a->cmdline_log, len);
3726 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3727 return got_error_from_errno("asprintf");
3729 if (asprintf(&initial_content,
3730 "\n# changes to be committed on branch %s:\n",
3731 a->branch_name) == -1)
3732 return got_error_from_errno("asprintf");
3734 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3738 dprintf(fd, initial_content);
3740 TAILQ_FOREACH(pe, commitable_paths, entry) {
3741 struct got_commitable *ct = pe->data;
3742 dprintf(fd, "# %c %s\n",
3743 got_commitable_get_status(ct),
3744 got_commitable_get_path(ct));
3748 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3750 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3751 unlink(a->logmsg_path);
3752 free(a->logmsg_path);
3753 a->logmsg_path = NULL;
3755 free(initial_content);
3758 /* Editor is done; we can now apply unveil(2) */
3760 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3769 static const struct got_error *
3770 cmd_commit(int argc, char *argv[])
3772 const struct got_error *error = NULL;
3773 struct got_worktree *worktree = NULL;
3774 struct got_repository *repo = NULL;
3775 char *cwd = NULL, *id_str = NULL;
3776 struct got_object_id *id = NULL;
3777 const char *logmsg = NULL;
3779 struct collect_commit_logmsg_arg cl_arg;
3780 char *editor = NULL;
3781 int ch, rebase_in_progress, histedit_in_progress;
3782 struct got_pathlist_head paths;
3785 cl_arg.logmsg_path = NULL;
3787 while ((ch = getopt(argc, argv, "m:")) != -1) {
3802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3803 "unveil", NULL) == -1)
3806 error = get_author(&author);
3810 cwd = getcwd(NULL, 0);
3812 error = got_error_from_errno("getcwd");
3815 error = got_worktree_open(&worktree, cwd);
3819 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3822 if (rebase_in_progress) {
3823 error = got_error(GOT_ERR_REBASING);
3827 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3832 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3837 * unveil(2) traverses exec(2); if an editor is used we have
3838 * to apply unveil after the log message has been written.
3840 if (logmsg == NULL || strlen(logmsg) == 0)
3841 error = get_editor(&editor);
3843 error = apply_unveil(got_repo_get_path(repo), 0,
3844 got_worktree_get_root_path(worktree));
3848 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3852 cl_arg.editor = editor;
3853 cl_arg.cmdline_log = logmsg;
3854 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3855 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3856 if (!histedit_in_progress) {
3857 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3858 error = got_error(GOT_ERR_COMMIT_BRANCH);
3861 cl_arg.branch_name += 11;
3863 cl_arg.repo_path = got_repo_get_path(repo);
3864 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3865 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3867 if (cl_arg.logmsg_path)
3868 fprintf(stderr, "%s: log message preserved in %s\n",
3869 getprogname(), cl_arg.logmsg_path);
3873 if (cl_arg.logmsg_path)
3874 unlink(cl_arg.logmsg_path);
3876 error = got_object_id_str(&id_str, id);
3879 printf("Created commit %s\n", id_str);
3881 free(cl_arg.logmsg_path);
3883 got_repo_close(repo);
3885 got_worktree_close(worktree);
3893 usage_cherrypick(void)
3895 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3899 static const struct got_error *
3900 cmd_cherrypick(int argc, char *argv[])
3902 const struct got_error *error = NULL;
3903 struct got_worktree *worktree = NULL;
3904 struct got_repository *repo = NULL;
3905 char *cwd = NULL, *commit_id_str = NULL;
3906 struct got_object_id *commit_id = NULL;
3907 struct got_commit_object *commit = NULL;
3908 struct got_object_qid *pid;
3909 struct got_reference *head_ref = NULL;
3910 int ch, did_something = 0;
3912 while ((ch = getopt(argc, argv, "")) != -1) {
3924 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3925 "unveil", NULL) == -1)
3931 cwd = getcwd(NULL, 0);
3933 error = got_error_from_errno("getcwd");
3936 error = got_worktree_open(&worktree, cwd);
3940 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3944 error = apply_unveil(got_repo_get_path(repo), 0,
3945 got_worktree_get_root_path(worktree));
3949 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3950 GOT_OBJ_TYPE_COMMIT, repo);
3951 if (error != NULL) {
3952 struct got_reference *ref;
3953 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3955 error = got_ref_open(&ref, repo, argv[0], 0);
3958 error = got_ref_resolve(&commit_id, repo, ref);
3963 error = got_object_id_str(&commit_id_str, commit_id);
3967 error = got_ref_open(&head_ref, repo,
3968 got_worktree_get_head_ref_name(worktree), 0);
3972 error = check_same_branch(commit_id, head_ref, NULL, repo);
3974 if (error->code != GOT_ERR_ANCESTRY)
3978 error = got_error(GOT_ERR_SAME_BRANCH);
3982 error = got_object_open_as_commit(&commit, repo, commit_id);
3985 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3986 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3987 commit_id, repo, update_progress, &did_something, check_cancelled,
3993 printf("Merged commit %s\n", commit_id_str);
3996 got_object_commit_close(commit);
3997 free(commit_id_str);
3999 got_ref_close(head_ref);
4001 got_worktree_close(worktree);
4003 got_repo_close(repo);
4010 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4014 static const struct got_error *
4015 cmd_backout(int argc, char *argv[])
4017 const struct got_error *error = NULL;
4018 struct got_worktree *worktree = NULL;
4019 struct got_repository *repo = NULL;
4020 char *cwd = NULL, *commit_id_str = NULL;
4021 struct got_object_id *commit_id = NULL;
4022 struct got_commit_object *commit = NULL;
4023 struct got_object_qid *pid;
4024 struct got_reference *head_ref = NULL;
4025 int ch, did_something = 0;
4027 while ((ch = getopt(argc, argv, "")) != -1) {
4039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4040 "unveil", NULL) == -1)
4046 cwd = getcwd(NULL, 0);
4048 error = got_error_from_errno("getcwd");
4051 error = got_worktree_open(&worktree, cwd);
4055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4059 error = apply_unveil(got_repo_get_path(repo), 0,
4060 got_worktree_get_root_path(worktree));
4064 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4065 GOT_OBJ_TYPE_COMMIT, repo);
4066 if (error != NULL) {
4067 struct got_reference *ref;
4068 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4070 error = got_ref_open(&ref, repo, argv[0], 0);
4073 error = got_ref_resolve(&commit_id, repo, ref);
4078 error = got_object_id_str(&commit_id_str, commit_id);
4082 error = got_ref_open(&head_ref, repo,
4083 got_worktree_get_head_ref_name(worktree), 0);
4087 error = check_same_branch(commit_id, head_ref, NULL, repo);
4091 error = got_object_open_as_commit(&commit, repo, commit_id);
4094 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4096 error = got_error(GOT_ERR_ROOT_COMMIT);
4100 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4101 update_progress, &did_something, check_cancelled, NULL);
4106 printf("Backed out commit %s\n", commit_id_str);
4109 got_object_commit_close(commit);
4110 free(commit_id_str);
4112 got_ref_close(head_ref);
4114 got_worktree_close(worktree);
4116 got_repo_close(repo);
4123 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4129 trim_logmsg(char *logmsg, int limit)
4134 len = strlen(logmsg);
4138 nl = strchr(logmsg, '\n');
4143 static const struct got_error *
4144 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4146 const struct got_error *err;
4147 char *logmsg0 = NULL;
4150 err = got_object_commit_get_logmsg(&logmsg0, commit);
4155 while (isspace((unsigned char)s[0]))
4158 *logmsg = strdup(s);
4159 if (*logmsg == NULL) {
4160 err = got_error_from_errno("strdup");
4164 trim_logmsg(*logmsg, limit);
4170 static const struct got_error *
4171 show_rebase_progress(struct got_commit_object *commit,
4172 struct got_object_id *old_id, struct got_object_id *new_id)
4174 const struct got_error *err;
4175 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4177 err = got_object_id_str(&old_id_str, old_id);
4182 err = got_object_id_str(&new_id_str, new_id);
4187 old_id_str[12] = '\0';
4189 new_id_str[12] = '\0';
4191 err = get_short_logmsg(&logmsg, 42, commit);
4195 printf("%s -> %s: %s\n", old_id_str,
4196 new_id_str ? new_id_str : "no-op change", logmsg);
4203 static const struct got_error *
4204 rebase_progress(void *arg, unsigned char status, const char *path)
4206 unsigned char *rebase_status = arg;
4208 while (path[0] == '/')
4210 printf("%c %s\n", status, path);
4212 if (*rebase_status == GOT_STATUS_CONFLICT)
4214 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4215 *rebase_status = status;
4219 static const struct got_error *
4220 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4221 struct got_reference *branch, struct got_reference *new_base_branch,
4222 struct got_reference *tmp_branch, struct got_repository *repo)
4224 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4225 return got_worktree_rebase_complete(worktree, fileindex,
4226 new_base_branch, tmp_branch, branch, repo);
4229 static const struct got_error *
4230 rebase_commit(struct got_pathlist_head *merged_paths,
4231 struct got_worktree *worktree, struct got_fileindex *fileindex,
4232 struct got_reference *tmp_branch,
4233 struct got_object_id *commit_id, struct got_repository *repo)
4235 const struct got_error *error;
4236 struct got_commit_object *commit;
4237 struct got_object_id *new_commit_id;
4239 error = got_object_open_as_commit(&commit, repo, commit_id);
4243 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4244 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4246 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4248 error = show_rebase_progress(commit, commit_id, NULL);
4250 error = show_rebase_progress(commit, commit_id, new_commit_id);
4251 free(new_commit_id);
4254 got_object_commit_close(commit);
4258 struct check_path_prefix_arg {
4259 const char *path_prefix;
4264 static const struct got_error *
4265 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4266 struct got_blob_object *blob2, struct got_object_id *id1,
4267 struct got_object_id *id2, const char *path1, const char *path2,
4268 struct got_repository *repo)
4270 struct check_path_prefix_arg *a = arg;
4272 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4273 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4274 return got_error(a->errcode);
4279 static const struct got_error *
4280 check_path_prefix(struct got_object_id *parent_id,
4281 struct got_object_id *commit_id, const char *path_prefix,
4282 int errcode, struct got_repository *repo)
4284 const struct got_error *err;
4285 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4286 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4287 struct check_path_prefix_arg cpp_arg;
4289 if (got_path_is_root_dir(path_prefix))
4292 err = got_object_open_as_commit(&commit, repo, commit_id);
4296 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4300 err = got_object_open_as_tree(&tree1, repo,
4301 got_object_commit_get_tree_id(parent_commit));
4305 err = got_object_open_as_tree(&tree2, repo,
4306 got_object_commit_get_tree_id(commit));
4310 cpp_arg.path_prefix = path_prefix;
4311 while (cpp_arg.path_prefix[0] == '/')
4312 cpp_arg.path_prefix++;
4313 cpp_arg.len = strlen(cpp_arg.path_prefix);
4314 cpp_arg.errcode = errcode;
4315 err = got_diff_tree(tree1, tree2, "", "", repo,
4316 check_path_prefix_in_diff, &cpp_arg, 0);
4319 got_object_tree_close(tree1);
4321 got_object_tree_close(tree2);
4323 got_object_commit_close(commit);
4325 got_object_commit_close(parent_commit);
4329 static const struct got_error *
4330 collect_commits(struct got_object_id_queue *commits,
4331 struct got_object_id *initial_commit_id,
4332 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4333 const char *path_prefix, int path_prefix_errcode,
4334 struct got_repository *repo)
4336 const struct got_error *err = NULL;
4337 struct got_commit_graph *graph = NULL;
4338 struct got_object_id *parent_id = NULL;
4339 struct got_object_qid *qid;
4340 struct got_object_id *commit_id = initial_commit_id;
4342 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4346 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4349 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4350 err = got_commit_graph_iter_next(&parent_id, graph);
4352 if (err->code == GOT_ERR_ITER_COMPLETED) {
4353 err = got_error_msg(GOT_ERR_ANCESTRY,
4354 "ran out of commits to rebase before "
4355 "youngest common ancestor commit has "
4358 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4360 err = got_commit_graph_fetch_commits(graph, 1, repo);
4364 err = check_path_prefix(parent_id, commit_id,
4365 path_prefix, path_prefix_errcode, repo);
4369 err = got_object_qid_alloc(&qid, commit_id);
4372 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4373 commit_id = parent_id;
4377 got_commit_graph_close(graph);
4381 static const struct got_error *
4382 cmd_rebase(int argc, char *argv[])
4384 const struct got_error *error = NULL;
4385 struct got_worktree *worktree = NULL;
4386 struct got_repository *repo = NULL;
4387 struct got_fileindex *fileindex = NULL;
4389 struct got_reference *branch = NULL;
4390 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4391 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4392 struct got_object_id *resume_commit_id = NULL;
4393 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4394 struct got_commit_object *commit = NULL;
4395 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4396 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4397 struct got_object_id_queue commits;
4398 struct got_pathlist_head merged_paths;
4399 const struct got_object_id_queue *parent_ids;
4400 struct got_object_qid *qid, *pid;
4402 SIMPLEQ_INIT(&commits);
4403 TAILQ_INIT(&merged_paths);
4405 while ((ch = getopt(argc, argv, "ac")) != -1) {
4411 continue_rebase = 1;
4423 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4424 "unveil", NULL) == -1)
4427 if (abort_rebase && continue_rebase)
4429 else if (abort_rebase || continue_rebase) {
4432 } else if (argc != 1)
4435 cwd = getcwd(NULL, 0);
4437 error = got_error_from_errno("getcwd");
4440 error = got_worktree_open(&worktree, cwd);
4444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4448 error = apply_unveil(got_repo_get_path(repo), 0,
4449 got_worktree_get_root_path(worktree));
4453 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4459 if (!rebase_in_progress) {
4460 error = got_error(GOT_ERR_NOT_REBASING);
4463 error = got_worktree_rebase_continue(&resume_commit_id,
4464 &new_base_branch, &tmp_branch, &branch, &fileindex,
4468 printf("Switching work tree to %s\n",
4469 got_ref_get_symref_target(new_base_branch));
4470 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4471 new_base_branch, update_progress, &did_something);
4474 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4475 goto done; /* nothing else to do */
4478 if (continue_rebase) {
4479 if (!rebase_in_progress) {
4480 error = got_error(GOT_ERR_NOT_REBASING);
4483 error = got_worktree_rebase_continue(&resume_commit_id,
4484 &new_base_branch, &tmp_branch, &branch, &fileindex,
4489 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4490 resume_commit_id, repo);
4494 yca_id = got_object_id_dup(resume_commit_id);
4495 if (yca_id == NULL) {
4496 error = got_error_from_errno("got_object_id_dup");
4500 error = got_ref_open(&branch, repo, argv[0], 0);
4505 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4509 if (!continue_rebase) {
4510 struct got_object_id *base_commit_id;
4512 base_commit_id = got_worktree_get_base_commit_id(worktree);
4513 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4514 base_commit_id, branch_head_commit_id, repo);
4517 if (yca_id == NULL) {
4518 error = got_error_msg(GOT_ERR_ANCESTRY,
4519 "specified branch shares no common ancestry "
4520 "with work tree's branch");
4524 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4526 if (error->code != GOT_ERR_ANCESTRY)
4530 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4531 "specified branch resolves to a commit which "
4532 "is already contained in work tree's branch");
4535 error = got_worktree_rebase_prepare(&new_base_branch,
4536 &tmp_branch, &fileindex, worktree, branch, repo);
4541 commit_id = branch_head_commit_id;
4542 error = got_object_open_as_commit(&commit, repo, commit_id);
4546 parent_ids = got_object_commit_get_parent_ids(commit);
4547 pid = SIMPLEQ_FIRST(parent_ids);
4549 if (!continue_rebase) {
4551 error = got_worktree_rebase_abort(worktree, fileindex,
4552 repo, new_base_branch, update_progress,
4556 printf("Rebase of %s aborted\n",
4557 got_ref_get_name(branch));
4559 error = got_error(GOT_ERR_EMPTY_REBASE);
4562 error = collect_commits(&commits, commit_id, pid->id,
4563 yca_id, got_worktree_get_path_prefix(worktree),
4564 GOT_ERR_REBASE_PATH, repo);
4565 got_object_commit_close(commit);
4570 if (SIMPLEQ_EMPTY(&commits)) {
4571 if (continue_rebase)
4572 error = rebase_complete(worktree, fileindex,
4573 branch, new_base_branch, tmp_branch, repo);
4575 error = got_error(GOT_ERR_EMPTY_REBASE);
4580 SIMPLEQ_FOREACH(qid, &commits, entry) {
4581 commit_id = qid->id;
4582 parent_id = pid ? pid->id : yca_id;
4585 error = got_worktree_rebase_merge_files(&merged_paths,
4586 worktree, fileindex, parent_id, commit_id, repo,
4587 rebase_progress, &rebase_status, check_cancelled, NULL);
4591 if (rebase_status == GOT_STATUS_CONFLICT) {
4592 got_worktree_rebase_pathlist_free(&merged_paths);
4596 error = rebase_commit(&merged_paths, worktree, fileindex,
4597 tmp_branch, commit_id, repo);
4598 got_worktree_rebase_pathlist_free(&merged_paths);
4603 if (rebase_status == GOT_STATUS_CONFLICT) {
4604 error = got_worktree_rebase_postpone(worktree, fileindex);
4607 error = got_error_msg(GOT_ERR_CONFLICTS,
4608 "conflicts must be resolved before rebasing can continue");
4610 error = rebase_complete(worktree, fileindex, branch,
4611 new_base_branch, tmp_branch, repo);
4613 got_object_id_queue_free(&commits);
4614 free(branch_head_commit_id);
4615 free(resume_commit_id);
4618 got_object_commit_close(commit);
4620 got_ref_close(branch);
4621 if (new_base_branch)
4622 got_ref_close(new_base_branch);
4624 got_ref_close(tmp_branch);
4626 got_worktree_close(worktree);
4628 got_repo_close(repo);
4633 usage_histedit(void)
4635 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4640 #define GOT_HISTEDIT_PICK 'p'
4641 #define GOT_HISTEDIT_EDIT 'e'
4642 #define GOT_HISTEDIT_FOLD 'f'
4643 #define GOT_HISTEDIT_DROP 'd'
4644 #define GOT_HISTEDIT_MESG 'm'
4646 static struct got_histedit_cmd {
4650 } got_histedit_cmds[] = {
4651 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4652 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4653 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4654 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4655 { GOT_HISTEDIT_MESG, "mesg",
4656 "single-line log message for commit above (open editor if empty)" },
4659 struct got_histedit_list_entry {
4660 TAILQ_ENTRY(got_histedit_list_entry) entry;
4661 struct got_object_id *commit_id;
4662 const struct got_histedit_cmd *cmd;
4665 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4667 static const struct got_error *
4668 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4669 FILE *f, struct got_repository *repo)
4671 const struct got_error *err = NULL;
4672 char *logmsg = NULL, *id_str = NULL;
4673 struct got_commit_object *commit = NULL;
4676 err = got_object_open_as_commit(&commit, repo, commit_id);
4680 err = get_short_logmsg(&logmsg, 34, commit);
4684 err = got_object_id_str(&id_str, commit_id);
4688 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4690 err = got_ferror(f, GOT_ERR_IO);
4693 got_object_commit_close(commit);
4699 static const struct got_error *
4700 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4701 struct got_repository *repo)
4703 const struct got_error *err = NULL;
4704 struct got_object_qid *qid;
4706 if (SIMPLEQ_EMPTY(commits))
4707 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4709 SIMPLEQ_FOREACH(qid, commits, entry) {
4710 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4719 static const struct got_error *
4720 write_cmd_list(FILE *f)
4722 const struct got_error *err = NULL;
4725 n = fprintf(f, "# Available histedit commands:\n");
4727 return got_ferror(f, GOT_ERR_IO);
4729 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4730 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4731 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4734 err = got_ferror(f, GOT_ERR_IO);
4738 n = fprintf(f, "# Commits will be processed in order from top to "
4739 "bottom of this file.\n");
4741 return got_ferror(f, GOT_ERR_IO);
4745 static const struct got_error *
4746 histedit_syntax_error(int lineno)
4748 static char msg[42];
4751 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4753 if (ret == -1 || ret >= sizeof(msg))
4754 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4756 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4759 static const struct got_error *
4760 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4761 char *logmsg, struct got_repository *repo)
4763 const struct got_error *err;
4764 struct got_commit_object *folded_commit = NULL;
4765 char *id_str, *folded_logmsg = NULL;
4767 err = got_object_id_str(&id_str, hle->commit_id);
4771 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4775 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4778 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4779 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4780 folded_logmsg) == -1) {
4781 err = got_error_from_errno("asprintf");
4786 got_object_commit_close(folded_commit);
4788 free(folded_logmsg);
4792 static struct got_histedit_list_entry *
4793 get_folded_commits(struct got_histedit_list_entry *hle)
4795 struct got_histedit_list_entry *prev, *folded = NULL;
4797 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4798 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4799 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4800 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4802 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4808 static const struct got_error *
4809 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4810 struct got_repository *repo)
4812 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4813 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4814 const struct got_error *err = NULL;
4815 struct got_commit_object *commit = NULL;
4817 struct got_histedit_list_entry *folded = NULL;
4819 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4823 folded = get_folded_commits(hle);
4825 while (folded != hle) {
4826 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4827 folded = TAILQ_NEXT(folded, entry);
4830 err = append_folded_commit_msg(&new_msg, folded,
4836 folded = TAILQ_NEXT(folded, entry);
4840 err = got_object_id_str(&id_str, hle->commit_id);
4843 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4846 if (asprintf(&new_msg,
4847 "%s\n# original log message of commit %s: %s",
4848 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4849 err = got_error_from_errno("asprintf");
4855 err = got_object_id_str(&id_str, hle->commit_id);
4859 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4863 dprintf(fd, logmsg);
4866 err = get_editor(&editor);
4870 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4872 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4874 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4877 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4878 err = got_error_from_errno2("unlink", logmsg_path);
4884 got_object_commit_close(commit);
4888 static const struct got_error *
4889 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4890 FILE *f, struct got_repository *repo)
4892 const struct got_error *err = NULL;
4893 char *line = NULL, *p, *end;
4897 const struct got_histedit_cmd *cmd;
4898 struct got_object_id *commit_id = NULL;
4899 struct got_histedit_list_entry *hle = NULL;
4902 len = getline(&line, &size, f);
4904 const struct got_error *getline_err;
4907 getline_err = got_error_from_errno("getline");
4908 err = got_ferror(f, getline_err->code);
4913 while (isspace((unsigned char)p[0]))
4915 if (p[0] == '#' || p[0] == '\0') {
4921 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4922 cmd = &got_histedit_cmds[i];
4923 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4924 isspace((unsigned char)p[strlen(cmd->name)])) {
4925 p += strlen(cmd->name);
4928 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4933 if (i == nitems(got_histedit_cmds)) {
4934 err = histedit_syntax_error(lineno);
4937 while (isspace((unsigned char)p[0]))
4939 if (cmd->code == GOT_HISTEDIT_MESG) {
4940 if (hle == NULL || hle->logmsg != NULL) {
4941 err = got_error(GOT_ERR_HISTEDIT_CMD);
4945 err = histedit_edit_logmsg(hle, repo);
4949 hle->logmsg = strdup(p);
4950 if (hle->logmsg == NULL) {
4951 err = got_error_from_errno("strdup");
4960 while (end[0] && !isspace((unsigned char)end[0]))
4964 err = got_object_resolve_id_str(&commit_id, repo, p);
4966 /* override error code */
4967 err = histedit_syntax_error(lineno);
4971 hle = malloc(sizeof(*hle));
4973 err = got_error_from_errno("malloc");
4977 hle->commit_id = commit_id;
4982 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4990 static const struct got_error *
4991 histedit_check_script(struct got_histedit_list *histedit_cmds,
4992 struct got_object_id_queue *commits, struct got_repository *repo)
4994 const struct got_error *err = NULL;
4995 struct got_object_qid *qid;
4996 struct got_histedit_list_entry *hle;
4997 static char msg[80];
5000 if (TAILQ_EMPTY(histedit_cmds))
5001 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5002 "histedit script contains no commands");
5003 if (SIMPLEQ_EMPTY(commits))
5004 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5006 SIMPLEQ_FOREACH(qid, commits, entry) {
5007 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5008 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5012 err = got_object_id_str(&id_str, qid->id);
5015 snprintf(msg, sizeof(msg),
5016 "commit %s missing from histedit script", id_str);
5018 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5022 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5023 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5024 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5025 "last commit in histedit script cannot be folded");
5030 static const struct got_error *
5031 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5032 const char *path, struct got_object_id_queue *commits,
5033 struct got_repository *repo)
5035 const struct got_error *err = NULL;
5039 err = get_editor(&editor);
5043 if (spawn_editor(editor, path) == -1) {
5044 err = got_error_from_errno("failed spawning editor");
5048 f = fopen(path, "r");
5050 err = got_error_from_errno("fopen");
5053 err = histedit_parse_list(histedit_cmds, f, repo);
5057 err = histedit_check_script(histedit_cmds, commits, repo);
5059 if (f && fclose(f) != 0 && err == NULL)
5060 err = got_error_from_errno("fclose");
5065 static const struct got_error *
5066 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5067 struct got_object_id_queue *, const char *, struct got_repository *);
5069 static const struct got_error *
5070 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5071 struct got_object_id_queue *commits, struct got_repository *repo)
5073 const struct got_error *err;
5077 err = got_opentemp_named(&path, &f, "got-histedit");
5081 err = write_cmd_list(f);
5085 err = histedit_write_commit_list(commits, f, repo);
5089 if (fclose(f) != 0) {
5090 err = got_error_from_errno("fclose");
5095 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5097 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5098 err->code != GOT_ERR_HISTEDIT_CMD)
5100 err = histedit_edit_list_retry(histedit_cmds, err,
5101 commits, path, repo);
5104 if (f && fclose(f) != 0 && err == NULL)
5105 err = got_error_from_errno("fclose");
5106 if (path && unlink(path) != 0 && err == NULL)
5107 err = got_error_from_errno2("unlink", path);
5112 static const struct got_error *
5113 histedit_save_list(struct got_histedit_list *histedit_cmds,
5114 struct got_worktree *worktree, struct got_repository *repo)
5116 const struct got_error *err = NULL;
5119 struct got_histedit_list_entry *hle;
5120 struct got_commit_object *commit = NULL;
5122 err = got_worktree_get_histedit_script_path(&path, worktree);
5126 f = fopen(path, "w");
5128 err = got_error_from_errno2("fopen", path);
5131 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5132 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5138 int n = fprintf(f, "%c %s\n",
5139 GOT_HISTEDIT_MESG, hle->logmsg);
5141 err = got_ferror(f, GOT_ERR_IO);
5147 if (f && fclose(f) != 0 && err == NULL)
5148 err = got_error_from_errno("fclose");
5151 got_object_commit_close(commit);
5156 histedit_free_list(struct got_histedit_list *histedit_cmds)
5158 struct got_histedit_list_entry *hle;
5160 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5161 TAILQ_REMOVE(histedit_cmds, hle, entry);
5166 static const struct got_error *
5167 histedit_load_list(struct got_histedit_list *histedit_cmds,
5168 const char *path, struct got_repository *repo)
5170 const struct got_error *err = NULL;
5173 f = fopen(path, "r");
5175 err = got_error_from_errno2("fopen", path);
5179 err = histedit_parse_list(histedit_cmds, f, repo);
5181 if (f && fclose(f) != 0 && err == NULL)
5182 err = got_error_from_errno("fclose");
5186 static const struct got_error *
5187 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5188 const struct got_error *edit_err, struct got_object_id_queue *commits,
5189 const char *path, struct got_repository *repo)
5191 const struct got_error *err = NULL, *prev_err = edit_err;
5194 while (resp != 'c' && resp != 'r' && resp != 'a') {
5195 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5196 "or (a)bort: ", getprogname(), prev_err->msg);
5201 histedit_free_list(histedit_cmds);
5202 err = histedit_run_editor(histedit_cmds, path, commits,
5205 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5206 err->code != GOT_ERR_HISTEDIT_CMD)
5213 } else if (resp == 'r') {
5214 histedit_free_list(histedit_cmds);
5215 err = histedit_edit_script(histedit_cmds,
5218 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5219 err->code != GOT_ERR_HISTEDIT_CMD)
5226 } else if (resp == 'a') {
5227 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5230 printf("invalid response '%c'\n", resp);
5236 static const struct got_error *
5237 histedit_complete(struct got_worktree *worktree,
5238 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5239 struct got_reference *branch, struct got_repository *repo)
5241 printf("Switching work tree to %s\n",
5242 got_ref_get_symref_target(branch));
5243 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5247 static const struct got_error *
5248 show_histedit_progress(struct got_commit_object *commit,
5249 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5251 const struct got_error *err;
5252 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5254 err = got_object_id_str(&old_id_str, hle->commit_id);
5259 err = got_object_id_str(&new_id_str, new_id);
5264 old_id_str[12] = '\0';
5266 new_id_str[12] = '\0';
5269 logmsg = strdup(hle->logmsg);
5270 if (logmsg == NULL) {
5271 err = got_error_from_errno("strdup");
5274 trim_logmsg(logmsg, 42);
5276 err = get_short_logmsg(&logmsg, 42, commit);
5281 switch (hle->cmd->code) {
5282 case GOT_HISTEDIT_PICK:
5283 case GOT_HISTEDIT_EDIT:
5284 printf("%s -> %s: %s\n", old_id_str,
5285 new_id_str ? new_id_str : "no-op change", logmsg);
5287 case GOT_HISTEDIT_DROP:
5288 case GOT_HISTEDIT_FOLD:
5289 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5302 static const struct got_error *
5303 histedit_commit(struct got_pathlist_head *merged_paths,
5304 struct got_worktree *worktree, struct got_fileindex *fileindex,
5305 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5306 struct got_repository *repo)
5308 const struct got_error *err;
5309 struct got_commit_object *commit;
5310 struct got_object_id *new_commit_id;
5312 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5313 && hle->logmsg == NULL) {
5314 err = histedit_edit_logmsg(hle, repo);
5319 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5323 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5324 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5327 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5329 err = show_histedit_progress(commit, hle, NULL);
5331 err = show_histedit_progress(commit, hle, new_commit_id);
5332 free(new_commit_id);
5335 got_object_commit_close(commit);
5339 static const struct got_error *
5340 histedit_skip_commit(struct got_histedit_list_entry *hle,
5341 struct got_worktree *worktree, struct got_repository *repo)
5343 const struct got_error *error;
5344 struct got_commit_object *commit;
5346 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5351 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5355 error = show_histedit_progress(commit, hle, NULL);
5356 got_object_commit_close(commit);
5360 static const struct got_error *
5361 cmd_histedit(int argc, char *argv[])
5363 const struct got_error *error = NULL;
5364 struct got_worktree *worktree = NULL;
5365 struct got_fileindex *fileindex = NULL;
5366 struct got_repository *repo = NULL;
5368 struct got_reference *branch = NULL;
5369 struct got_reference *tmp_branch = NULL;
5370 struct got_object_id *resume_commit_id = NULL;
5371 struct got_object_id *base_commit_id = NULL;
5372 struct got_object_id *head_commit_id = NULL;
5373 struct got_commit_object *commit = NULL;
5374 int ch, rebase_in_progress = 0, did_something;
5375 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5376 const char *edit_script_path = NULL;
5377 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5378 struct got_object_id_queue commits;
5379 struct got_pathlist_head merged_paths;
5380 const struct got_object_id_queue *parent_ids;
5381 struct got_object_qid *pid;
5382 struct got_histedit_list histedit_cmds;
5383 struct got_histedit_list_entry *hle;
5385 SIMPLEQ_INIT(&commits);
5386 TAILQ_INIT(&histedit_cmds);
5387 TAILQ_INIT(&merged_paths);
5389 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5398 edit_script_path = optarg;
5410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5411 "unveil", NULL) == -1)
5414 if (abort_edit && continue_edit)
5420 * This command cannot apply unveil(2) in all cases because the
5421 * user may choose to run an editor to edit the histedit script
5422 * and to edit individual commit log messages.
5423 * unveil(2) traverses exec(2); if an editor is used we have to
5424 * apply unveil after edit script and log messages have been written.
5425 * XXX TODO: Make use of unveil(2) where possible.
5428 cwd = getcwd(NULL, 0);
5430 error = got_error_from_errno("getcwd");
5433 error = got_worktree_open(&worktree, cwd);
5437 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5441 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5444 if (rebase_in_progress) {
5445 error = got_error(GOT_ERR_REBASING);
5449 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5453 if (edit_in_progress && abort_edit) {
5454 error = got_worktree_histedit_continue(&resume_commit_id,
5455 &tmp_branch, &branch, &base_commit_id, &fileindex,
5459 printf("Switching work tree to %s\n",
5460 got_ref_get_symref_target(branch));
5461 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5462 branch, base_commit_id, update_progress, &did_something);
5465 printf("Histedit of %s aborted\n",
5466 got_ref_get_symref_target(branch));
5467 goto done; /* nothing else to do */
5468 } else if (abort_edit) {
5469 error = got_error(GOT_ERR_NOT_HISTEDIT);
5473 if (continue_edit) {
5476 if (!edit_in_progress) {
5477 error = got_error(GOT_ERR_NOT_HISTEDIT);
5481 error = got_worktree_get_histedit_script_path(&path, worktree);
5485 error = histedit_load_list(&histedit_cmds, path, repo);
5490 error = got_worktree_histedit_continue(&resume_commit_id,
5491 &tmp_branch, &branch, &base_commit_id, &fileindex,
5496 error = got_ref_resolve(&head_commit_id, repo, branch);
5500 error = got_object_open_as_commit(&commit, repo,
5504 parent_ids = got_object_commit_get_parent_ids(commit);
5505 pid = SIMPLEQ_FIRST(parent_ids);
5507 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5510 error = collect_commits(&commits, head_commit_id, pid->id,
5511 base_commit_id, got_worktree_get_path_prefix(worktree),
5512 GOT_ERR_HISTEDIT_PATH, repo);
5513 got_object_commit_close(commit);
5518 if (edit_in_progress) {
5519 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5523 error = got_ref_open(&branch, repo,
5524 got_worktree_get_head_ref_name(worktree), 0);
5528 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5529 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5530 "will not edit commit history of a branch outside "
5531 "the \"refs/heads/\" reference namespace");
5535 error = got_ref_resolve(&head_commit_id, repo, branch);
5536 got_ref_close(branch);
5541 error = got_object_open_as_commit(&commit, repo,
5545 parent_ids = got_object_commit_get_parent_ids(commit);
5546 pid = SIMPLEQ_FIRST(parent_ids);
5548 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5551 error = collect_commits(&commits, head_commit_id, pid->id,
5552 got_worktree_get_base_commit_id(worktree),
5553 got_worktree_get_path_prefix(worktree),
5554 GOT_ERR_HISTEDIT_PATH, repo);
5555 got_object_commit_close(commit);
5560 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5561 &base_commit_id, &fileindex, worktree, repo);
5565 if (edit_script_path) {
5566 error = histedit_load_list(&histedit_cmds,
5567 edit_script_path, repo);
5569 got_worktree_histedit_abort(worktree, fileindex,
5570 repo, branch, base_commit_id,
5571 update_progress, &did_something);
5575 error = histedit_edit_script(&histedit_cmds, &commits,
5578 got_worktree_histedit_abort(worktree, fileindex,
5579 repo, branch, base_commit_id,
5580 update_progress, &did_something);
5586 error = histedit_save_list(&histedit_cmds, worktree,
5589 got_worktree_histedit_abort(worktree, fileindex,
5590 repo, branch, base_commit_id,
5591 update_progress, &did_something);
5597 error = histedit_check_script(&histedit_cmds, &commits, repo);
5601 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5602 if (resume_commit_id) {
5603 if (got_object_id_cmp(hle->commit_id,
5604 resume_commit_id) != 0)
5607 resume_commit_id = NULL;
5608 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5609 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5610 error = histedit_skip_commit(hle, worktree,
5613 error = histedit_commit(NULL, worktree,
5614 fileindex, tmp_branch, hle, repo);
5621 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5622 error = histedit_skip_commit(hle, worktree, repo);
5628 error = got_object_open_as_commit(&commit, repo,
5632 parent_ids = got_object_commit_get_parent_ids(commit);
5633 pid = SIMPLEQ_FIRST(parent_ids);
5635 error = got_worktree_histedit_merge_files(&merged_paths,
5636 worktree, fileindex, pid->id, hle->commit_id, repo,
5637 rebase_progress, &rebase_status, check_cancelled, NULL);
5640 got_object_commit_close(commit);
5643 if (rebase_status == GOT_STATUS_CONFLICT) {
5644 got_worktree_rebase_pathlist_free(&merged_paths);
5648 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5650 error = got_object_id_str(&id_str, hle->commit_id);
5653 printf("Stopping histedit for amending commit %s\n",
5656 got_worktree_rebase_pathlist_free(&merged_paths);
5657 error = got_worktree_histedit_postpone(worktree,
5662 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5663 error = histedit_skip_commit(hle, worktree, repo);
5669 error = histedit_commit(&merged_paths, worktree, fileindex,
5670 tmp_branch, hle, repo);
5671 got_worktree_rebase_pathlist_free(&merged_paths);
5676 if (rebase_status == GOT_STATUS_CONFLICT) {
5677 error = got_worktree_histedit_postpone(worktree, fileindex);
5680 error = got_error_msg(GOT_ERR_CONFLICTS,
5681 "conflicts must be resolved before rebasing can continue");
5683 error = histedit_complete(worktree, fileindex, tmp_branch,
5686 got_object_id_queue_free(&commits);
5687 histedit_free_list(&histedit_cmds);
5688 free(head_commit_id);
5689 free(base_commit_id);
5690 free(resume_commit_id);
5692 got_object_commit_close(commit);
5694 got_ref_close(branch);
5696 got_ref_close(tmp_branch);
5698 got_worktree_close(worktree);
5700 got_repo_close(repo);
5707 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5708 "[file-path ...]\n",
5713 static const struct got_error *
5714 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5715 const char *path, struct got_object_id *blob_id,
5716 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5718 const struct got_error *err = NULL;
5719 char *id_str = NULL;
5721 if (staged_status != GOT_STATUS_ADD &&
5722 staged_status != GOT_STATUS_MODIFY &&
5723 staged_status != GOT_STATUS_DELETE)
5726 if (staged_status == GOT_STATUS_ADD ||
5727 staged_status == GOT_STATUS_MODIFY)
5728 err = got_object_id_str(&id_str, staged_blob_id);
5730 err = got_object_id_str(&id_str, blob_id);
5734 printf("%s %c %s\n", id_str, staged_status, path);
5739 static const struct got_error *
5740 cmd_stage(int argc, char *argv[])
5742 const struct got_error *error = NULL;
5743 struct got_repository *repo = NULL;
5744 struct got_worktree *worktree = NULL;
5746 struct got_pathlist_head paths;
5747 struct got_pathlist_entry *pe;
5748 int ch, list_stage = 0, pflag = 0;
5749 FILE *patch_script_file = NULL;
5750 const char *patch_script_path = NULL;
5751 struct choose_patch_arg cpa;
5755 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5764 patch_script_path = optarg;
5776 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5777 "unveil", NULL) == -1)
5780 if (list_stage && (pflag || patch_script_path))
5781 errx(1, "-l option cannot be used with other options");
5782 if (patch_script_path && !pflag)
5783 errx(1, "-F option can only be used together with -p option");
5785 cwd = getcwd(NULL, 0);
5787 error = got_error_from_errno("getcwd");
5791 error = got_worktree_open(&worktree, cwd);
5795 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5799 if (patch_script_path) {
5800 patch_script_file = fopen(patch_script_path, "r");
5801 if (patch_script_file == NULL) {
5802 error = got_error_from_errno2("fopen",
5807 error = apply_unveil(got_repo_get_path(repo), 1,
5808 got_worktree_get_root_path(worktree));
5812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5817 error = got_worktree_status(worktree, &paths, repo,
5818 print_stage, NULL, check_cancelled, NULL);
5820 cpa.patch_script_file = patch_script_file;
5821 cpa.action = "stage";
5822 error = got_worktree_stage(worktree, &paths,
5823 pflag ? NULL : print_status, NULL,
5824 pflag ? choose_patch : NULL, &cpa, repo);
5827 if (patch_script_file && fclose(patch_script_file) == EOF &&
5829 error = got_error_from_errno2("fclose", patch_script_path);
5831 got_repo_close(repo);
5833 got_worktree_close(worktree);
5834 TAILQ_FOREACH(pe, &paths, entry)
5835 free((char *)pe->path);
5836 got_pathlist_free(&paths);
5844 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5845 "[file-path ...]\n",
5851 static const struct got_error *
5852 cmd_unstage(int argc, char *argv[])
5854 const struct got_error *error = NULL;
5855 struct got_repository *repo = NULL;
5856 struct got_worktree *worktree = NULL;
5858 struct got_pathlist_head paths;
5859 struct got_pathlist_entry *pe;
5860 int ch, did_something = 0, pflag = 0;
5861 FILE *patch_script_file = NULL;
5862 const char *patch_script_path = NULL;
5863 struct choose_patch_arg cpa;
5867 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5873 patch_script_path = optarg;
5885 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5886 "unveil", NULL) == -1)
5889 if (patch_script_path && !pflag)
5890 errx(1, "-F option can only be used together with -p option");
5892 cwd = getcwd(NULL, 0);
5894 error = got_error_from_errno("getcwd");
5898 error = got_worktree_open(&worktree, cwd);
5902 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5906 if (patch_script_path) {
5907 patch_script_file = fopen(patch_script_path, "r");
5908 if (patch_script_file == NULL) {
5909 error = got_error_from_errno2("fopen",
5915 error = apply_unveil(got_repo_get_path(repo), 1,
5916 got_worktree_get_root_path(worktree));
5920 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5924 cpa.patch_script_file = patch_script_file;
5925 cpa.action = "unstage";
5926 error = got_worktree_unstage(worktree, &paths, update_progress,
5927 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5929 if (patch_script_file && fclose(patch_script_file) == EOF &&
5931 error = got_error_from_errno2("fclose", patch_script_path);
5933 got_repo_close(repo);
5935 got_worktree_close(worktree);
5936 TAILQ_FOREACH(pe, &paths, entry)
5937 free((char *)pe->path);
5938 got_pathlist_free(&paths);