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;
2290 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2295 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2298 commit_id_str = optarg;
2301 repo_path = realpath(optarg, NULL);
2302 if (repo_path == NULL)
2303 err(1, "-r option");
2304 got_path_strip_trailing_slashes(repo_path);
2320 cwd = getcwd(NULL, 0);
2322 error = got_error_from_errno("getcwd");
2325 if (repo_path == NULL) {
2326 error = got_worktree_open(&worktree, cwd);
2327 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2333 strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL)
2335 error = got_error_from_errno("strdup");
2339 repo_path = strdup(cwd);
2340 if (repo_path == NULL) {
2341 error = got_error_from_errno("strdup");
2347 error = got_repo_open(&repo, repo_path);
2351 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2356 const char *prefix = got_worktree_get_path_prefix(worktree);
2357 char *p, *worktree_subdir = cwd +
2358 strlen(got_worktree_get_root_path(worktree));
2359 if (asprintf(&p, "%s%s%s%s%s",
2360 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2361 worktree_subdir, worktree_subdir[0] ? "/" : "",
2363 error = got_error_from_errno("asprintf");
2366 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2369 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2374 if (commit_id_str == NULL) {
2375 struct got_reference *head_ref;
2376 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2379 error = got_ref_resolve(&commit_id, repo, head_ref);
2380 got_ref_close(head_ref);
2384 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2389 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2392 if (obj_id == NULL) {
2393 error = got_error(GOT_ERR_NO_OBJ);
2397 error = got_object_get_type(&obj_type, repo, obj_id);
2401 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2402 error = got_error(GOT_ERR_OBJ_TYPE);
2406 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2409 bca.f = got_opentemp();
2410 if (bca.f == NULL) {
2411 error = got_error_from_errno("got_opentemp");
2414 error = got_object_blob_dump_to_file(NULL, &bca.nlines,
2415 &bca.line_offsets, bca.f, blob);
2416 if (error || bca.nlines == 0)
2419 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2420 if (bca.lines == NULL) {
2421 error = got_error_from_errno("calloc");
2425 bca.nlines_prec = 0;
2433 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2443 got_object_blob_close(blob);
2445 got_worktree_close(worktree);
2447 const struct got_error *repo_error;
2448 repo_error = got_repo_close(repo);
2452 for (i = 0; i < bca.nlines; i++) {
2453 struct blame_line *bline = &bca.lines[i];
2454 free(bline->id_str);
2455 free(bline->committer);
2458 free(bca.line_offsets);
2459 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2460 error = got_error_from_errno("fclose");
2468 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2474 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2475 const char *root_path)
2477 int is_root_path = (strcmp(path, root_path) == 0);
2478 const char *modestr = "";
2480 path += strlen(root_path);
2481 while (path[0] == '/')
2484 if (S_ISLNK(te->mode))
2486 else if (S_ISDIR(te->mode))
2488 else if (te->mode & S_IXUSR)
2491 printf("%s%s%s%s%s\n", id ? id : "", path,
2492 is_root_path ? "" : "/", te->name, modestr);
2495 static const struct got_error *
2496 print_tree(const char *path, struct got_object_id *commit_id,
2497 int show_ids, int recurse, const char *root_path,
2498 struct got_repository *repo)
2500 const struct got_error *err = NULL;
2501 struct got_object_id *tree_id = NULL;
2502 struct got_tree_object *tree = NULL;
2503 const struct got_tree_entries *entries;
2504 struct got_tree_entry *te;
2506 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2510 err = got_object_open_as_tree(&tree, repo, tree_id);
2513 entries = got_object_tree_get_entries(tree);
2514 te = SIMPLEQ_FIRST(&entries->head);
2518 if (sigint_received || sigpipe_received)
2523 err = got_object_id_str(&id_str, te->id);
2526 if (asprintf(&id, "%s ", id_str) == -1) {
2527 err = got_error_from_errno("asprintf");
2533 print_entry(te, id, path, root_path);
2536 if (recurse && S_ISDIR(te->mode)) {
2538 if (asprintf(&child_path, "%s%s%s", path,
2539 path[0] == '/' && path[1] == '\0' ? "" : "/",
2541 err = got_error_from_errno("asprintf");
2544 err = print_tree(child_path, commit_id, show_ids, 1,
2551 te = SIMPLEQ_NEXT(te, entry);
2555 got_object_tree_close(tree);
2560 static const struct got_error *
2561 cmd_tree(int argc, char *argv[])
2563 const struct got_error *error;
2564 struct got_repository *repo = NULL;
2565 struct got_worktree *worktree = NULL;
2567 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2568 struct got_object_id *commit_id = NULL;
2569 char *commit_id_str = NULL;
2570 int show_ids = 0, recurse = 0;
2574 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2579 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2582 commit_id_str = optarg;
2585 repo_path = realpath(optarg, NULL);
2586 if (repo_path == NULL)
2587 err(1, "-r option");
2588 got_path_strip_trailing_slashes(repo_path);
2612 cwd = getcwd(NULL, 0);
2614 error = got_error_from_errno("getcwd");
2617 if (repo_path == NULL) {
2618 error = got_worktree_open(&worktree, cwd);
2619 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2625 strdup(got_worktree_get_repo_path(worktree));
2626 if (repo_path == NULL)
2627 error = got_error_from_errno("strdup");
2631 repo_path = strdup(cwd);
2632 if (repo_path == NULL) {
2633 error = got_error_from_errno("strdup");
2639 error = got_repo_open(&repo, repo_path);
2643 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2649 char *p, *worktree_subdir = cwd +
2650 strlen(got_worktree_get_root_path(worktree));
2651 if (asprintf(&p, "%s/%s",
2652 got_worktree_get_path_prefix(worktree),
2653 worktree_subdir) == -1) {
2654 error = got_error_from_errno("asprintf");
2657 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2664 if (in_repo_path == NULL) {
2665 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2670 if (commit_id_str == NULL) {
2671 struct got_reference *head_ref;
2672 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2675 error = got_ref_resolve(&commit_id, repo, head_ref);
2676 got_ref_close(head_ref);
2680 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2685 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2686 in_repo_path, repo);
2693 got_worktree_close(worktree);
2695 const struct got_error *repo_error;
2696 repo_error = got_repo_close(repo);
2706 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2710 static const struct got_error *
2711 print_status(void *arg, unsigned char status, unsigned char staged_status,
2712 const char *path, struct got_object_id *blob_id,
2713 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2715 if (status == staged_status && (status == GOT_STATUS_DELETE))
2716 status = GOT_STATUS_NO_CHANGE;
2717 printf("%c%c %s\n", status, staged_status, path);
2721 static const struct got_error *
2722 cmd_status(int argc, char *argv[])
2724 const struct got_error *error = NULL;
2725 struct got_repository *repo = NULL;
2726 struct got_worktree *worktree = NULL;
2728 struct got_pathlist_head paths;
2729 struct got_pathlist_entry *pe;
2734 while ((ch = getopt(argc, argv, "")) != -1) {
2746 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2750 cwd = getcwd(NULL, 0);
2752 error = got_error_from_errno("getcwd");
2756 error = got_worktree_open(&worktree, cwd);
2760 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2764 error = apply_unveil(got_repo_get_path(repo), 1,
2765 got_worktree_get_root_path(worktree));
2769 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2773 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2774 check_cancelled, NULL);
2776 TAILQ_FOREACH(pe, &paths, entry)
2777 free((char *)pe->path);
2778 got_pathlist_free(&paths);
2787 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2792 static const struct got_error *
2793 list_refs(struct got_repository *repo)
2795 static const struct got_error *err = NULL;
2796 struct got_reflist_head refs;
2797 struct got_reflist_entry *re;
2799 SIMPLEQ_INIT(&refs);
2800 err = got_ref_list(&refs, repo);
2804 SIMPLEQ_FOREACH(re, &refs, entry) {
2806 refstr = got_ref_to_str(re->ref);
2808 return got_error_from_errno("got_ref_to_str");
2809 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2813 got_ref_list_free(&refs);
2817 static const struct got_error *
2818 delete_ref(struct got_repository *repo, const char *refname)
2820 const struct got_error *err = NULL;
2821 struct got_reference *ref;
2823 err = got_ref_open(&ref, repo, refname, 0);
2827 err = got_ref_delete(ref, repo);
2832 static const struct got_error *
2833 add_ref(struct got_repository *repo, const char *refname, const char *target)
2835 const struct got_error *err = NULL;
2836 struct got_object_id *id;
2837 struct got_reference *ref = NULL;
2840 * Don't let the user create a reference named '-'.
2841 * While technically a valid reference name, this case is usually
2842 * an unintended typo.
2844 if (refname[0] == '-' && refname[1] == '\0')
2845 return got_error(GOT_ERR_BAD_REF_NAME);
2847 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2850 struct got_reference *target_ref;
2852 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2854 err = got_ref_open(&target_ref, repo, target, 0);
2857 err = got_ref_resolve(&id, repo, target_ref);
2858 got_ref_close(target_ref);
2863 err = got_ref_alloc(&ref, refname, id);
2867 err = got_ref_write(ref, repo);
2875 static const struct got_error *
2876 add_symref(struct got_repository *repo, const char *refname, const char *target)
2878 const struct got_error *err = NULL;
2879 struct got_reference *ref = NULL;
2880 struct got_reference *target_ref = NULL;
2883 * Don't let the user create a reference named '-'.
2884 * While technically a valid reference name, this case is usually
2885 * an unintended typo.
2887 if (refname[0] == '-' && refname[1] == '\0')
2888 return got_error(GOT_ERR_BAD_REF_NAME);
2890 err = got_ref_open(&target_ref, repo, target, 0);
2894 err = got_ref_alloc_symref(&ref, refname, target_ref);
2898 err = got_ref_write(ref, repo);
2901 got_ref_close(target_ref);
2907 static const struct got_error *
2908 cmd_ref(int argc, char *argv[])
2910 const struct got_error *error = NULL;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2913 char *cwd = NULL, *repo_path = NULL;
2914 int ch, do_list = 0, create_symref = 0;
2915 const char *delref = NULL;
2917 /* TODO: Add -s option for adding symbolic references. */
2918 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2924 repo_path = realpath(optarg, NULL);
2925 if (repo_path == NULL)
2926 err(1, "-r option");
2927 got_path_strip_trailing_slashes(repo_path);
2941 if (do_list && delref)
2942 errx(1, "-l and -d options are mutually exclusive\n");
2947 if (do_list || delref) {
2949 errx(1, "-s option cannot be used together with the "
2950 "-l or -d options");
2953 } else if (argc != 2)
2958 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2962 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2963 "sendfd unveil", NULL) == -1)
2967 cwd = getcwd(NULL, 0);
2969 error = got_error_from_errno("getcwd");
2973 if (repo_path == NULL) {
2974 error = got_worktree_open(&worktree, cwd);
2975 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2981 strdup(got_worktree_get_repo_path(worktree));
2982 if (repo_path == NULL)
2983 error = got_error_from_errno("strdup");
2987 repo_path = strdup(cwd);
2988 if (repo_path == NULL) {
2989 error = got_error_from_errno("strdup");
2995 error = got_repo_open(&repo, repo_path);
2999 error = apply_unveil(got_repo_get_path(repo), do_list,
3000 worktree ? got_worktree_get_root_path(worktree) : NULL);
3005 error = list_refs(repo);
3007 error = delete_ref(repo, delref);
3008 else if (create_symref)
3009 error = add_symref(repo, argv[0], argv[1]);
3011 error = add_ref(repo, argv[0], argv[1]);
3014 got_repo_close(repo);
3016 got_worktree_close(worktree);
3026 "usage: %s branch [-r repository] -l | -d name | "
3027 "name [base-branch]\n", getprogname());
3031 static const struct got_error *
3032 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3034 static const struct got_error *err = NULL;
3035 struct got_reflist_head refs;
3036 struct got_reflist_entry *re;
3038 SIMPLEQ_INIT(&refs);
3040 err = got_ref_list(&refs, repo);
3044 SIMPLEQ_FOREACH(re, &refs, entry) {
3045 const char *refname, *marker = " ";
3047 refname = got_ref_get_name(re->ref);
3048 if (strncmp(refname, "refs/heads/", 11) != 0)
3050 if (worktree && strcmp(refname,
3051 got_worktree_get_head_ref_name(worktree)) == 0) {
3052 struct got_object_id *id = NULL;
3053 err = got_ref_resolve(&id, repo, re->ref);
3056 if (got_object_id_cmp(id,
3057 got_worktree_get_base_commit_id(worktree)) == 0)
3064 refstr = got_ref_to_str(re->ref);
3066 return got_error_from_errno("got_ref_to_str");
3067 printf("%s%s: %s\n", marker, refname, refstr);
3071 got_ref_list_free(&refs);
3075 static const struct got_error *
3076 delete_branch(struct got_repository *repo, const char *branch_name)
3078 const struct got_error *err = NULL;
3079 struct got_reference *ref;
3082 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3083 return got_error_from_errno("asprintf");
3085 err = got_ref_open(&ref, repo, refname, 0);
3089 err = got_ref_delete(ref, repo);
3096 static const struct got_error *
3097 add_branch(struct got_repository *repo, const char *branch_name,
3098 const char *base_branch)
3100 const struct got_error *err = NULL;
3101 struct got_object_id *id = NULL;
3102 struct got_reference *ref = NULL;
3103 char *base_refname = NULL, *refname = NULL;
3104 struct got_reference *base_ref;
3107 * Don't let the user create a branch named '-'.
3108 * While technically a valid reference name, this case is usually
3109 * an unintended typo.
3111 if (branch_name[0] == '-' && branch_name[1] == '\0')
3112 return got_error(GOT_ERR_BAD_REF_NAME);
3114 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3115 base_refname = strdup(GOT_REF_HEAD);
3116 if (base_refname == NULL)
3117 return got_error_from_errno("strdup");
3118 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3119 return got_error_from_errno("asprintf");
3121 err = got_ref_open(&base_ref, repo, base_refname, 0);
3124 err = got_ref_resolve(&id, repo, base_ref);
3125 got_ref_close(base_ref);
3129 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3130 err = got_error_from_errno("asprintf");
3134 err = got_ref_open(&ref, repo, refname, 0);
3136 err = got_error(GOT_ERR_BRANCH_EXISTS);
3138 } else if (err->code != GOT_ERR_NOT_REF)
3141 err = got_ref_alloc(&ref, refname, id);
3145 err = got_ref_write(ref, repo);
3155 static const struct got_error *
3156 cmd_branch(int argc, char *argv[])
3158 const struct got_error *error = NULL;
3159 struct got_repository *repo = NULL;
3160 struct got_worktree *worktree = NULL;
3161 char *cwd = NULL, *repo_path = NULL;
3162 int ch, do_list = 0;
3163 const char *delref = NULL;
3165 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3171 repo_path = realpath(optarg, NULL);
3172 if (repo_path == NULL)
3173 err(1, "-r option");
3174 got_path_strip_trailing_slashes(repo_path);
3185 if (do_list && delref)
3186 errx(1, "-l and -d options are mutually exclusive\n");
3191 if (do_list || delref) {
3194 } else if (argc < 1 || argc > 2)
3199 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3203 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3204 "sendfd unveil", NULL) == -1)
3208 cwd = getcwd(NULL, 0);
3210 error = got_error_from_errno("getcwd");
3214 if (repo_path == NULL) {
3215 error = got_worktree_open(&worktree, cwd);
3216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3222 strdup(got_worktree_get_repo_path(worktree));
3223 if (repo_path == NULL)
3224 error = got_error_from_errno("strdup");
3228 repo_path = strdup(cwd);
3229 if (repo_path == NULL) {
3230 error = got_error_from_errno("strdup");
3236 error = got_repo_open(&repo, repo_path);
3240 error = apply_unveil(got_repo_get_path(repo), do_list,
3241 worktree ? got_worktree_get_root_path(worktree) : NULL);
3246 error = list_branches(repo, worktree);
3248 error = delete_branch(repo, delref);
3250 const char *base_branch;
3252 base_branch = worktree ?
3253 got_worktree_get_head_ref_name(worktree) :
3255 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3258 base_branch = argv[1];
3259 error = add_branch(repo, argv[0], base_branch);
3263 got_repo_close(repo);
3265 got_worktree_close(worktree);
3274 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3278 static const struct got_error *
3279 cmd_add(int argc, char *argv[])
3281 const struct got_error *error = NULL;
3282 struct got_repository *repo = NULL;
3283 struct got_worktree *worktree = NULL;
3285 struct got_pathlist_head paths;
3286 struct got_pathlist_entry *pe;
3291 while ((ch = getopt(argc, argv, "")) != -1) {
3303 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3310 cwd = getcwd(NULL, 0);
3312 error = got_error_from_errno("getcwd");
3316 error = got_worktree_open(&worktree, cwd);
3320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3324 error = apply_unveil(got_repo_get_path(repo), 1,
3325 got_worktree_get_root_path(worktree));
3329 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3333 error = got_worktree_schedule_add(worktree, &paths, print_status,
3337 got_repo_close(repo);
3339 got_worktree_close(worktree);
3340 TAILQ_FOREACH(pe, &paths, entry)
3341 free((char *)pe->path);
3342 got_pathlist_free(&paths);
3350 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3354 static const struct got_error *
3355 cmd_remove(int argc, char *argv[])
3357 const struct got_error *error = NULL;
3358 struct got_worktree *worktree = NULL;
3359 struct got_repository *repo = NULL;
3361 struct got_pathlist_head paths;
3362 struct got_pathlist_entry *pe;
3363 int ch, delete_local_mods = 0;
3367 while ((ch = getopt(argc, argv, "f")) != -1) {
3370 delete_local_mods = 1;
3382 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3389 cwd = getcwd(NULL, 0);
3391 error = got_error_from_errno("getcwd");
3394 error = got_worktree_open(&worktree, cwd);
3398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3402 error = apply_unveil(got_repo_get_path(repo), 1,
3403 got_worktree_get_root_path(worktree));
3407 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3411 error = got_worktree_schedule_delete(worktree, &paths,
3412 delete_local_mods, print_status, NULL, repo);
3417 got_repo_close(repo);
3419 got_worktree_close(worktree);
3420 TAILQ_FOREACH(pe, &paths, entry)
3421 free((char *)pe->path);
3422 got_pathlist_free(&paths);
3430 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3431 "path ...\n", getprogname());
3435 static const struct got_error *
3436 revert_progress(void *arg, unsigned char status, const char *path)
3438 while (path[0] == '/')
3440 printf("%c %s\n", status, path);
3444 struct choose_patch_arg {
3445 FILE *patch_script_file;
3449 static const struct got_error *
3450 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3451 int nchanges, const char *action)
3454 size_t linesize = 0;
3458 case GOT_STATUS_ADD:
3459 printf("A %s\n%s this addition? [y/n] ", path, action);
3461 case GOT_STATUS_DELETE:
3462 printf("D %s\n%s this deletion? [y/n] ", path, action);
3464 case GOT_STATUS_MODIFY:
3465 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3466 return got_error_from_errno("fseek");
3467 printf(GOT_COMMIT_SEP_STR);
3468 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3470 if (ferror(patch_file))
3471 return got_error_from_errno("getline");
3472 printf(GOT_COMMIT_SEP_STR);
3473 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3474 path, n, nchanges, action);
3477 return got_error_path(path, GOT_ERR_FILE_STATUS);
3483 static const struct got_error *
3484 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3485 FILE *patch_file, int n, int nchanges)
3487 const struct got_error *err = NULL;
3489 size_t linesize = 0;
3492 struct choose_patch_arg *a = arg;
3494 *choice = GOT_PATCH_CHOICE_NONE;
3496 if (a->patch_script_file) {
3498 err = show_change(status, path, patch_file, n, nchanges,
3502 linelen = getline(&line, &linesize, a->patch_script_file);
3503 if (linelen == -1) {
3504 if (ferror(a->patch_script_file))
3505 return got_error_from_errno("getline");
3508 nl = strchr(line, '\n');
3511 if (strcmp(line, "y") == 0) {
3512 *choice = GOT_PATCH_CHOICE_YES;
3514 } else if (strcmp(line, "n") == 0) {
3515 *choice = GOT_PATCH_CHOICE_NO;
3517 } else if (strcmp(line, "q") == 0 &&
3518 status == GOT_STATUS_MODIFY) {
3519 *choice = GOT_PATCH_CHOICE_QUIT;
3522 printf("invalid response '%s'\n", line);
3527 while (resp != 'y' && resp != 'n' && resp != 'q') {
3528 err = show_change(status, path, patch_file, n, nchanges,
3535 if (status == GOT_STATUS_MODIFY) {
3536 if (resp != 'y' && resp != 'n' && resp != 'q') {
3537 printf("invalid response '%c'\n", resp);
3540 } else if (resp != 'y' && resp != 'n') {
3541 printf("invalid response '%c'\n", resp);
3547 *choice = GOT_PATCH_CHOICE_YES;
3548 else if (resp == 'n')
3549 *choice = GOT_PATCH_CHOICE_NO;
3550 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3551 *choice = GOT_PATCH_CHOICE_QUIT;
3557 static const struct got_error *
3558 cmd_revert(int argc, char *argv[])
3560 const struct got_error *error = NULL;
3561 struct got_worktree *worktree = NULL;
3562 struct got_repository *repo = NULL;
3563 char *cwd = NULL, *path = NULL;
3564 struct got_pathlist_head paths;
3565 struct got_pathlist_entry *pe;
3566 int ch, can_recurse = 0, pflag = 0;
3567 FILE *patch_script_file = NULL;
3568 const char *patch_script_path = NULL;
3569 struct choose_patch_arg cpa;
3573 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3579 patch_script_path = optarg;
3594 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3595 "unveil", NULL) == -1)
3600 if (patch_script_path && !pflag)
3601 errx(1, "-F option can only be used together with -p option");
3603 cwd = getcwd(NULL, 0);
3605 error = got_error_from_errno("getcwd");
3608 error = got_worktree_open(&worktree, cwd);
3612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3616 if (patch_script_path) {
3617 patch_script_file = fopen(patch_script_path, "r");
3618 if (patch_script_file == NULL) {
3619 error = got_error_from_errno2("fopen",
3624 error = apply_unveil(got_repo_get_path(repo), 1,
3625 got_worktree_get_root_path(worktree));
3629 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3636 TAILQ_FOREACH(pe, &paths, entry) {
3637 if (asprintf(&ondisk_path, "%s/%s",
3638 got_worktree_get_root_path(worktree),
3640 error = got_error_from_errno("asprintf");
3643 if (lstat(ondisk_path, &sb) == -1) {
3644 if (errno == ENOENT) {
3648 error = got_error_from_errno2("lstat",
3654 if (S_ISDIR(sb.st_mode)) {
3655 error = got_error_msg(GOT_ERR_BAD_PATH,
3656 "reverting directories requires -R option");
3662 cpa.patch_script_file = patch_script_file;
3663 cpa.action = "revert";
3664 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3665 pflag ? choose_patch : NULL, &cpa, repo);
3669 if (patch_script_file && fclose(patch_script_file) == EOF &&
3671 error = got_error_from_errno2("fclose", patch_script_path);
3673 got_repo_close(repo);
3675 got_worktree_close(worktree);
3684 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3689 struct collect_commit_logmsg_arg {
3690 const char *cmdline_log;
3692 const char *worktree_path;
3693 const char *branch_name;
3694 const char *repo_path;
3699 static const struct got_error *
3700 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3703 char *initial_content = NULL;
3704 struct got_pathlist_entry *pe;
3705 const struct got_error *err = NULL;
3706 char *template = NULL;
3707 struct collect_commit_logmsg_arg *a = arg;
3711 /* if a message was specified on the command line, just use it */
3712 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3713 len = strlen(a->cmdline_log) + 1;
3714 *logmsg = malloc(len + 1);
3715 if (*logmsg == NULL)
3716 return got_error_from_errno("malloc");
3717 strlcpy(*logmsg, a->cmdline_log, len);
3721 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3722 return got_error_from_errno("asprintf");
3724 if (asprintf(&initial_content,
3725 "\n# changes to be committed on branch %s:\n",
3726 a->branch_name) == -1)
3727 return got_error_from_errno("asprintf");
3729 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3733 dprintf(fd, initial_content);
3735 TAILQ_FOREACH(pe, commitable_paths, entry) {
3736 struct got_commitable *ct = pe->data;
3737 dprintf(fd, "# %c %s\n",
3738 got_commitable_get_status(ct),
3739 got_commitable_get_path(ct));
3743 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3745 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3746 unlink(a->logmsg_path);
3747 free(a->logmsg_path);
3748 a->logmsg_path = NULL;
3750 free(initial_content);
3753 /* Editor is done; we can now apply unveil(2) */
3755 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3764 static const struct got_error *
3765 cmd_commit(int argc, char *argv[])
3767 const struct got_error *error = NULL;
3768 struct got_worktree *worktree = NULL;
3769 struct got_repository *repo = NULL;
3770 char *cwd = NULL, *id_str = NULL;
3771 struct got_object_id *id = NULL;
3772 const char *logmsg = NULL;
3774 struct collect_commit_logmsg_arg cl_arg;
3775 char *editor = NULL;
3776 int ch, rebase_in_progress, histedit_in_progress;
3777 struct got_pathlist_head paths;
3780 cl_arg.logmsg_path = NULL;
3782 while ((ch = getopt(argc, argv, "m:")) != -1) {
3797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3798 "unveil", NULL) == -1)
3801 error = get_author(&author);
3805 cwd = getcwd(NULL, 0);
3807 error = got_error_from_errno("getcwd");
3810 error = got_worktree_open(&worktree, cwd);
3814 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3817 if (rebase_in_progress) {
3818 error = got_error(GOT_ERR_REBASING);
3822 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3832 * unveil(2) traverses exec(2); if an editor is used we have
3833 * to apply unveil after the log message has been written.
3835 if (logmsg == NULL || strlen(logmsg) == 0)
3836 error = get_editor(&editor);
3838 error = apply_unveil(got_repo_get_path(repo), 0,
3839 got_worktree_get_root_path(worktree));
3843 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3847 cl_arg.editor = editor;
3848 cl_arg.cmdline_log = logmsg;
3849 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3850 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3851 if (!histedit_in_progress) {
3852 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3853 error = got_error(GOT_ERR_COMMIT_BRANCH);
3856 cl_arg.branch_name += 11;
3858 cl_arg.repo_path = got_repo_get_path(repo);
3859 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3860 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3862 if (cl_arg.logmsg_path)
3863 fprintf(stderr, "%s: log message preserved in %s\n",
3864 getprogname(), cl_arg.logmsg_path);
3868 if (cl_arg.logmsg_path)
3869 unlink(cl_arg.logmsg_path);
3871 error = got_object_id_str(&id_str, id);
3874 printf("Created commit %s\n", id_str);
3876 free(cl_arg.logmsg_path);
3878 got_repo_close(repo);
3880 got_worktree_close(worktree);
3888 usage_cherrypick(void)
3890 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3894 static const struct got_error *
3895 cmd_cherrypick(int argc, char *argv[])
3897 const struct got_error *error = NULL;
3898 struct got_worktree *worktree = NULL;
3899 struct got_repository *repo = NULL;
3900 char *cwd = NULL, *commit_id_str = NULL;
3901 struct got_object_id *commit_id = NULL;
3902 struct got_commit_object *commit = NULL;
3903 struct got_object_qid *pid;
3904 struct got_reference *head_ref = NULL;
3905 int ch, did_something = 0;
3907 while ((ch = getopt(argc, argv, "")) != -1) {
3919 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3920 "unveil", NULL) == -1)
3926 cwd = getcwd(NULL, 0);
3928 error = got_error_from_errno("getcwd");
3931 error = got_worktree_open(&worktree, cwd);
3935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3939 error = apply_unveil(got_repo_get_path(repo), 0,
3940 got_worktree_get_root_path(worktree));
3944 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3945 GOT_OBJ_TYPE_COMMIT, repo);
3946 if (error != NULL) {
3947 struct got_reference *ref;
3948 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3950 error = got_ref_open(&ref, repo, argv[0], 0);
3953 error = got_ref_resolve(&commit_id, repo, ref);
3958 error = got_object_id_str(&commit_id_str, commit_id);
3962 error = got_ref_open(&head_ref, repo,
3963 got_worktree_get_head_ref_name(worktree), 0);
3967 error = check_same_branch(commit_id, head_ref, NULL, repo);
3969 if (error->code != GOT_ERR_ANCESTRY)
3973 error = got_error(GOT_ERR_SAME_BRANCH);
3977 error = got_object_open_as_commit(&commit, repo, commit_id);
3980 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3981 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3982 commit_id, repo, update_progress, &did_something, check_cancelled,
3988 printf("Merged commit %s\n", commit_id_str);
3991 got_object_commit_close(commit);
3992 free(commit_id_str);
3994 got_ref_close(head_ref);
3996 got_worktree_close(worktree);
3998 got_repo_close(repo);
4005 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4009 static const struct got_error *
4010 cmd_backout(int argc, char *argv[])
4012 const struct got_error *error = NULL;
4013 struct got_worktree *worktree = NULL;
4014 struct got_repository *repo = NULL;
4015 char *cwd = NULL, *commit_id_str = NULL;
4016 struct got_object_id *commit_id = NULL;
4017 struct got_commit_object *commit = NULL;
4018 struct got_object_qid *pid;
4019 struct got_reference *head_ref = NULL;
4020 int ch, did_something = 0;
4022 while ((ch = getopt(argc, argv, "")) != -1) {
4034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4035 "unveil", NULL) == -1)
4041 cwd = getcwd(NULL, 0);
4043 error = got_error_from_errno("getcwd");
4046 error = got_worktree_open(&worktree, cwd);
4050 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4054 error = apply_unveil(got_repo_get_path(repo), 0,
4055 got_worktree_get_root_path(worktree));
4059 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4060 GOT_OBJ_TYPE_COMMIT, repo);
4061 if (error != NULL) {
4062 struct got_reference *ref;
4063 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4065 error = got_ref_open(&ref, repo, argv[0], 0);
4068 error = got_ref_resolve(&commit_id, repo, ref);
4073 error = got_object_id_str(&commit_id_str, commit_id);
4077 error = got_ref_open(&head_ref, repo,
4078 got_worktree_get_head_ref_name(worktree), 0);
4082 error = check_same_branch(commit_id, head_ref, NULL, repo);
4086 error = got_object_open_as_commit(&commit, repo, commit_id);
4089 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4091 error = got_error(GOT_ERR_ROOT_COMMIT);
4095 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4096 update_progress, &did_something, check_cancelled, NULL);
4101 printf("Backed out commit %s\n", commit_id_str);
4104 got_object_commit_close(commit);
4105 free(commit_id_str);
4107 got_ref_close(head_ref);
4109 got_worktree_close(worktree);
4111 got_repo_close(repo);
4118 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4124 trim_logmsg(char *logmsg, int limit)
4129 len = strlen(logmsg);
4133 nl = strchr(logmsg, '\n');
4138 static const struct got_error *
4139 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4141 const struct got_error *err;
4142 char *logmsg0 = NULL;
4145 err = got_object_commit_get_logmsg(&logmsg0, commit);
4150 while (isspace((unsigned char)s[0]))
4153 *logmsg = strdup(s);
4154 if (*logmsg == NULL) {
4155 err = got_error_from_errno("strdup");
4159 trim_logmsg(*logmsg, limit);
4165 static const struct got_error *
4166 show_rebase_progress(struct got_commit_object *commit,
4167 struct got_object_id *old_id, struct got_object_id *new_id)
4169 const struct got_error *err;
4170 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4172 err = got_object_id_str(&old_id_str, old_id);
4177 err = got_object_id_str(&new_id_str, new_id);
4182 old_id_str[12] = '\0';
4184 new_id_str[12] = '\0';
4186 err = get_short_logmsg(&logmsg, 42, commit);
4190 printf("%s -> %s: %s\n", old_id_str,
4191 new_id_str ? new_id_str : "no-op change", logmsg);
4198 static const struct got_error *
4199 rebase_progress(void *arg, unsigned char status, const char *path)
4201 unsigned char *rebase_status = arg;
4203 while (path[0] == '/')
4205 printf("%c %s\n", status, path);
4207 if (*rebase_status == GOT_STATUS_CONFLICT)
4209 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4210 *rebase_status = status;
4214 static const struct got_error *
4215 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4216 struct got_reference *branch, struct got_reference *new_base_branch,
4217 struct got_reference *tmp_branch, struct got_repository *repo)
4219 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4220 return got_worktree_rebase_complete(worktree, fileindex,
4221 new_base_branch, tmp_branch, branch, repo);
4224 static const struct got_error *
4225 rebase_commit(struct got_pathlist_head *merged_paths,
4226 struct got_worktree *worktree, struct got_fileindex *fileindex,
4227 struct got_reference *tmp_branch,
4228 struct got_object_id *commit_id, struct got_repository *repo)
4230 const struct got_error *error;
4231 struct got_commit_object *commit;
4232 struct got_object_id *new_commit_id;
4234 error = got_object_open_as_commit(&commit, repo, commit_id);
4238 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4239 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4241 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4243 error = show_rebase_progress(commit, commit_id, NULL);
4245 error = show_rebase_progress(commit, commit_id, new_commit_id);
4246 free(new_commit_id);
4249 got_object_commit_close(commit);
4253 struct check_path_prefix_arg {
4254 const char *path_prefix;
4259 static const struct got_error *
4260 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4261 struct got_blob_object *blob2, struct got_object_id *id1,
4262 struct got_object_id *id2, const char *path1, const char *path2,
4263 struct got_repository *repo)
4265 struct check_path_prefix_arg *a = arg;
4267 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4268 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4269 return got_error(a->errcode);
4274 static const struct got_error *
4275 check_path_prefix(struct got_object_id *parent_id,
4276 struct got_object_id *commit_id, const char *path_prefix,
4277 int errcode, struct got_repository *repo)
4279 const struct got_error *err;
4280 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4281 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4282 struct check_path_prefix_arg cpp_arg;
4284 if (got_path_is_root_dir(path_prefix))
4287 err = got_object_open_as_commit(&commit, repo, commit_id);
4291 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4295 err = got_object_open_as_tree(&tree1, repo,
4296 got_object_commit_get_tree_id(parent_commit));
4300 err = got_object_open_as_tree(&tree2, repo,
4301 got_object_commit_get_tree_id(commit));
4305 cpp_arg.path_prefix = path_prefix;
4306 while (cpp_arg.path_prefix[0] == '/')
4307 cpp_arg.path_prefix++;
4308 cpp_arg.len = strlen(cpp_arg.path_prefix);
4309 cpp_arg.errcode = errcode;
4310 err = got_diff_tree(tree1, tree2, "", "", repo,
4311 check_path_prefix_in_diff, &cpp_arg, 0);
4314 got_object_tree_close(tree1);
4316 got_object_tree_close(tree2);
4318 got_object_commit_close(commit);
4320 got_object_commit_close(parent_commit);
4324 static const struct got_error *
4325 collect_commits(struct got_object_id_queue *commits,
4326 struct got_object_id *initial_commit_id,
4327 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4328 const char *path_prefix, int path_prefix_errcode,
4329 struct got_repository *repo)
4331 const struct got_error *err = NULL;
4332 struct got_commit_graph *graph = NULL;
4333 struct got_object_id *parent_id = NULL;
4334 struct got_object_qid *qid;
4335 struct got_object_id *commit_id = initial_commit_id;
4337 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4341 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4344 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4345 err = got_commit_graph_iter_next(&parent_id, graph);
4347 if (err->code == GOT_ERR_ITER_COMPLETED) {
4348 err = got_error_msg(GOT_ERR_ANCESTRY,
4349 "ran out of commits to rebase before "
4350 "youngest common ancestor commit has "
4353 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4355 err = got_commit_graph_fetch_commits(graph, 1, repo);
4359 err = check_path_prefix(parent_id, commit_id,
4360 path_prefix, path_prefix_errcode, repo);
4364 err = got_object_qid_alloc(&qid, commit_id);
4367 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4368 commit_id = parent_id;
4372 got_commit_graph_close(graph);
4376 static const struct got_error *
4377 cmd_rebase(int argc, char *argv[])
4379 const struct got_error *error = NULL;
4380 struct got_worktree *worktree = NULL;
4381 struct got_repository *repo = NULL;
4382 struct got_fileindex *fileindex = NULL;
4384 struct got_reference *branch = NULL;
4385 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4386 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4387 struct got_object_id *resume_commit_id = NULL;
4388 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4389 struct got_commit_object *commit = NULL;
4390 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4391 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4392 struct got_object_id_queue commits;
4393 struct got_pathlist_head merged_paths;
4394 const struct got_object_id_queue *parent_ids;
4395 struct got_object_qid *qid, *pid;
4397 SIMPLEQ_INIT(&commits);
4398 TAILQ_INIT(&merged_paths);
4400 while ((ch = getopt(argc, argv, "ac")) != -1) {
4406 continue_rebase = 1;
4418 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4419 "unveil", NULL) == -1)
4422 if (abort_rebase && continue_rebase)
4424 else if (abort_rebase || continue_rebase) {
4427 } else if (argc != 1)
4430 cwd = getcwd(NULL, 0);
4432 error = got_error_from_errno("getcwd");
4435 error = got_worktree_open(&worktree, cwd);
4439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4443 error = apply_unveil(got_repo_get_path(repo), 0,
4444 got_worktree_get_root_path(worktree));
4448 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4454 if (!rebase_in_progress) {
4455 error = got_error(GOT_ERR_NOT_REBASING);
4458 error = got_worktree_rebase_continue(&resume_commit_id,
4459 &new_base_branch, &tmp_branch, &branch, &fileindex,
4463 printf("Switching work tree to %s\n",
4464 got_ref_get_symref_target(new_base_branch));
4465 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4466 new_base_branch, update_progress, &did_something);
4469 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4470 goto done; /* nothing else to do */
4473 if (continue_rebase) {
4474 if (!rebase_in_progress) {
4475 error = got_error(GOT_ERR_NOT_REBASING);
4478 error = got_worktree_rebase_continue(&resume_commit_id,
4479 &new_base_branch, &tmp_branch, &branch, &fileindex,
4484 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4485 resume_commit_id, repo);
4489 yca_id = got_object_id_dup(resume_commit_id);
4490 if (yca_id == NULL) {
4491 error = got_error_from_errno("got_object_id_dup");
4495 error = got_ref_open(&branch, repo, argv[0], 0);
4500 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4504 if (!continue_rebase) {
4505 struct got_object_id *base_commit_id;
4507 base_commit_id = got_worktree_get_base_commit_id(worktree);
4508 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4509 base_commit_id, branch_head_commit_id, repo);
4512 if (yca_id == NULL) {
4513 error = got_error_msg(GOT_ERR_ANCESTRY,
4514 "specified branch shares no common ancestry "
4515 "with work tree's branch");
4519 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4521 if (error->code != GOT_ERR_ANCESTRY)
4525 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4526 "specified branch resolves to a commit which "
4527 "is already contained in work tree's branch");
4530 error = got_worktree_rebase_prepare(&new_base_branch,
4531 &tmp_branch, &fileindex, worktree, branch, repo);
4536 commit_id = branch_head_commit_id;
4537 error = got_object_open_as_commit(&commit, repo, commit_id);
4541 parent_ids = got_object_commit_get_parent_ids(commit);
4542 pid = SIMPLEQ_FIRST(parent_ids);
4544 if (!continue_rebase) {
4546 error = got_worktree_rebase_abort(worktree, fileindex,
4547 repo, new_base_branch, update_progress,
4551 printf("Rebase of %s aborted\n",
4552 got_ref_get_name(branch));
4554 error = got_error(GOT_ERR_EMPTY_REBASE);
4557 error = collect_commits(&commits, commit_id, pid->id,
4558 yca_id, got_worktree_get_path_prefix(worktree),
4559 GOT_ERR_REBASE_PATH, repo);
4560 got_object_commit_close(commit);
4565 if (SIMPLEQ_EMPTY(&commits)) {
4566 if (continue_rebase)
4567 error = rebase_complete(worktree, fileindex,
4568 branch, new_base_branch, tmp_branch, repo);
4570 error = got_error(GOT_ERR_EMPTY_REBASE);
4575 SIMPLEQ_FOREACH(qid, &commits, entry) {
4576 commit_id = qid->id;
4577 parent_id = pid ? pid->id : yca_id;
4580 error = got_worktree_rebase_merge_files(&merged_paths,
4581 worktree, fileindex, parent_id, commit_id, repo,
4582 rebase_progress, &rebase_status, check_cancelled, NULL);
4586 if (rebase_status == GOT_STATUS_CONFLICT) {
4587 got_worktree_rebase_pathlist_free(&merged_paths);
4591 error = rebase_commit(&merged_paths, worktree, fileindex,
4592 tmp_branch, commit_id, repo);
4593 got_worktree_rebase_pathlist_free(&merged_paths);
4598 if (rebase_status == GOT_STATUS_CONFLICT) {
4599 error = got_worktree_rebase_postpone(worktree, fileindex);
4602 error = got_error_msg(GOT_ERR_CONFLICTS,
4603 "conflicts must be resolved before rebasing can continue");
4605 error = rebase_complete(worktree, fileindex, branch,
4606 new_base_branch, tmp_branch, repo);
4608 got_object_id_queue_free(&commits);
4609 free(branch_head_commit_id);
4610 free(resume_commit_id);
4613 got_object_commit_close(commit);
4615 got_ref_close(branch);
4616 if (new_base_branch)
4617 got_ref_close(new_base_branch);
4619 got_ref_close(tmp_branch);
4621 got_worktree_close(worktree);
4623 got_repo_close(repo);
4628 usage_histedit(void)
4630 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4635 #define GOT_HISTEDIT_PICK 'p'
4636 #define GOT_HISTEDIT_EDIT 'e'
4637 #define GOT_HISTEDIT_FOLD 'f'
4638 #define GOT_HISTEDIT_DROP 'd'
4639 #define GOT_HISTEDIT_MESG 'm'
4641 static struct got_histedit_cmd {
4645 } got_histedit_cmds[] = {
4646 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4647 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4648 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4649 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4650 { GOT_HISTEDIT_MESG, "mesg",
4651 "single-line log message for commit above (open editor if empty)" },
4654 struct got_histedit_list_entry {
4655 TAILQ_ENTRY(got_histedit_list_entry) entry;
4656 struct got_object_id *commit_id;
4657 const struct got_histedit_cmd *cmd;
4660 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4662 static const struct got_error *
4663 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4664 FILE *f, struct got_repository *repo)
4666 const struct got_error *err = NULL;
4667 char *logmsg = NULL, *id_str = NULL;
4668 struct got_commit_object *commit = NULL;
4671 err = got_object_open_as_commit(&commit, repo, commit_id);
4675 err = get_short_logmsg(&logmsg, 34, commit);
4679 err = got_object_id_str(&id_str, commit_id);
4683 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4685 err = got_ferror(f, GOT_ERR_IO);
4688 got_object_commit_close(commit);
4694 static const struct got_error *
4695 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4696 struct got_repository *repo)
4698 const struct got_error *err = NULL;
4699 struct got_object_qid *qid;
4701 if (SIMPLEQ_EMPTY(commits))
4702 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4704 SIMPLEQ_FOREACH(qid, commits, entry) {
4705 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4714 static const struct got_error *
4715 write_cmd_list(FILE *f)
4717 const struct got_error *err = NULL;
4720 n = fprintf(f, "# Available histedit commands:\n");
4722 return got_ferror(f, GOT_ERR_IO);
4724 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4725 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4726 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4729 err = got_ferror(f, GOT_ERR_IO);
4733 n = fprintf(f, "# Commits will be processed in order from top to "
4734 "bottom of this file.\n");
4736 return got_ferror(f, GOT_ERR_IO);
4740 static const struct got_error *
4741 histedit_syntax_error(int lineno)
4743 static char msg[42];
4746 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4748 if (ret == -1 || ret >= sizeof(msg))
4749 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4751 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4754 static const struct got_error *
4755 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4756 char *logmsg, struct got_repository *repo)
4758 const struct got_error *err;
4759 struct got_commit_object *folded_commit = NULL;
4760 char *id_str, *folded_logmsg = NULL;
4762 err = got_object_id_str(&id_str, hle->commit_id);
4766 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4770 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4773 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4774 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4775 folded_logmsg) == -1) {
4776 err = got_error_from_errno("asprintf");
4781 got_object_commit_close(folded_commit);
4783 free(folded_logmsg);
4787 static struct got_histedit_list_entry *
4788 get_folded_commits(struct got_histedit_list_entry *hle)
4790 struct got_histedit_list_entry *prev, *folded = NULL;
4792 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4793 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4794 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4795 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4797 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4803 static const struct got_error *
4804 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4805 struct got_repository *repo)
4807 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4808 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4809 const struct got_error *err = NULL;
4810 struct got_commit_object *commit = NULL;
4812 struct got_histedit_list_entry *folded = NULL;
4814 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4818 folded = get_folded_commits(hle);
4820 while (folded != hle) {
4821 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4822 folded = TAILQ_NEXT(folded, entry);
4825 err = append_folded_commit_msg(&new_msg, folded,
4831 folded = TAILQ_NEXT(folded, entry);
4835 err = got_object_id_str(&id_str, hle->commit_id);
4838 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4841 if (asprintf(&new_msg,
4842 "%s\n# original log message of commit %s: %s",
4843 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4844 err = got_error_from_errno("asprintf");
4850 err = got_object_id_str(&id_str, hle->commit_id);
4854 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4858 dprintf(fd, logmsg);
4861 err = get_editor(&editor);
4865 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4867 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4869 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4872 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4873 err = got_error_from_errno2("unlink", logmsg_path);
4879 got_object_commit_close(commit);
4883 static const struct got_error *
4884 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4885 FILE *f, struct got_repository *repo)
4887 const struct got_error *err = NULL;
4888 char *line = NULL, *p, *end;
4892 const struct got_histedit_cmd *cmd;
4893 struct got_object_id *commit_id = NULL;
4894 struct got_histedit_list_entry *hle = NULL;
4897 len = getline(&line, &size, f);
4899 const struct got_error *getline_err;
4902 getline_err = got_error_from_errno("getline");
4903 err = got_ferror(f, getline_err->code);
4908 while (isspace((unsigned char)p[0]))
4910 if (p[0] == '#' || p[0] == '\0') {
4916 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4917 cmd = &got_histedit_cmds[i];
4918 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4919 isspace((unsigned char)p[strlen(cmd->name)])) {
4920 p += strlen(cmd->name);
4923 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4928 if (i == nitems(got_histedit_cmds)) {
4929 err = histedit_syntax_error(lineno);
4932 while (isspace((unsigned char)p[0]))
4934 if (cmd->code == GOT_HISTEDIT_MESG) {
4935 if (hle == NULL || hle->logmsg != NULL) {
4936 err = got_error(GOT_ERR_HISTEDIT_CMD);
4940 err = histedit_edit_logmsg(hle, repo);
4944 hle->logmsg = strdup(p);
4945 if (hle->logmsg == NULL) {
4946 err = got_error_from_errno("strdup");
4955 while (end[0] && !isspace((unsigned char)end[0]))
4959 err = got_object_resolve_id_str(&commit_id, repo, p);
4961 /* override error code */
4962 err = histedit_syntax_error(lineno);
4966 hle = malloc(sizeof(*hle));
4968 err = got_error_from_errno("malloc");
4972 hle->commit_id = commit_id;
4977 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4985 static const struct got_error *
4986 histedit_check_script(struct got_histedit_list *histedit_cmds,
4987 struct got_object_id_queue *commits, struct got_repository *repo)
4989 const struct got_error *err = NULL;
4990 struct got_object_qid *qid;
4991 struct got_histedit_list_entry *hle;
4992 static char msg[80];
4995 if (TAILQ_EMPTY(histedit_cmds))
4996 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4997 "histedit script contains no commands");
4998 if (SIMPLEQ_EMPTY(commits))
4999 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5001 SIMPLEQ_FOREACH(qid, commits, entry) {
5002 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5003 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5007 err = got_object_id_str(&id_str, qid->id);
5010 snprintf(msg, sizeof(msg),
5011 "commit %s missing from histedit script", id_str);
5013 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5017 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5018 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5019 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5020 "last commit in histedit script cannot be folded");
5025 static const struct got_error *
5026 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5027 const char *path, struct got_object_id_queue *commits,
5028 struct got_repository *repo)
5030 const struct got_error *err = NULL;
5034 err = get_editor(&editor);
5038 if (spawn_editor(editor, path) == -1) {
5039 err = got_error_from_errno("failed spawning editor");
5043 f = fopen(path, "r");
5045 err = got_error_from_errno("fopen");
5048 err = histedit_parse_list(histedit_cmds, f, repo);
5052 err = histedit_check_script(histedit_cmds, commits, repo);
5054 if (f && fclose(f) != 0 && err == NULL)
5055 err = got_error_from_errno("fclose");
5060 static const struct got_error *
5061 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5062 struct got_object_id_queue *, const char *, struct got_repository *);
5064 static const struct got_error *
5065 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5066 struct got_object_id_queue *commits, struct got_repository *repo)
5068 const struct got_error *err;
5072 err = got_opentemp_named(&path, &f, "got-histedit");
5076 err = write_cmd_list(f);
5080 err = histedit_write_commit_list(commits, f, repo);
5084 if (fclose(f) != 0) {
5085 err = got_error_from_errno("fclose");
5090 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5092 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5093 err->code != GOT_ERR_HISTEDIT_CMD)
5095 err = histedit_edit_list_retry(histedit_cmds, err,
5096 commits, path, repo);
5099 if (f && fclose(f) != 0 && err == NULL)
5100 err = got_error_from_errno("fclose");
5101 if (path && unlink(path) != 0 && err == NULL)
5102 err = got_error_from_errno2("unlink", path);
5107 static const struct got_error *
5108 histedit_save_list(struct got_histedit_list *histedit_cmds,
5109 struct got_worktree *worktree, struct got_repository *repo)
5111 const struct got_error *err = NULL;
5114 struct got_histedit_list_entry *hle;
5115 struct got_commit_object *commit = NULL;
5117 err = got_worktree_get_histedit_script_path(&path, worktree);
5121 f = fopen(path, "w");
5123 err = got_error_from_errno2("fopen", path);
5126 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5127 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5133 int n = fprintf(f, "%c %s\n",
5134 GOT_HISTEDIT_MESG, hle->logmsg);
5136 err = got_ferror(f, GOT_ERR_IO);
5142 if (f && fclose(f) != 0 && err == NULL)
5143 err = got_error_from_errno("fclose");
5146 got_object_commit_close(commit);
5151 histedit_free_list(struct got_histedit_list *histedit_cmds)
5153 struct got_histedit_list_entry *hle;
5155 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5156 TAILQ_REMOVE(histedit_cmds, hle, entry);
5161 static const struct got_error *
5162 histedit_load_list(struct got_histedit_list *histedit_cmds,
5163 const char *path, struct got_repository *repo)
5165 const struct got_error *err = NULL;
5168 f = fopen(path, "r");
5170 err = got_error_from_errno2("fopen", path);
5174 err = histedit_parse_list(histedit_cmds, f, repo);
5176 if (f && fclose(f) != 0 && err == NULL)
5177 err = got_error_from_errno("fclose");
5181 static const struct got_error *
5182 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5183 const struct got_error *edit_err, struct got_object_id_queue *commits,
5184 const char *path, struct got_repository *repo)
5186 const struct got_error *err = NULL, *prev_err = edit_err;
5189 while (resp != 'c' && resp != 'r' && resp != 'a') {
5190 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5191 "or (a)bort: ", getprogname(), prev_err->msg);
5196 histedit_free_list(histedit_cmds);
5197 err = histedit_run_editor(histedit_cmds, path, commits,
5200 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5201 err->code != GOT_ERR_HISTEDIT_CMD)
5208 } else if (resp == 'r') {
5209 histedit_free_list(histedit_cmds);
5210 err = histedit_edit_script(histedit_cmds,
5213 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5214 err->code != GOT_ERR_HISTEDIT_CMD)
5221 } else if (resp == 'a') {
5222 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5225 printf("invalid response '%c'\n", resp);
5231 static const struct got_error *
5232 histedit_complete(struct got_worktree *worktree,
5233 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5234 struct got_reference *branch, struct got_repository *repo)
5236 printf("Switching work tree to %s\n",
5237 got_ref_get_symref_target(branch));
5238 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5242 static const struct got_error *
5243 show_histedit_progress(struct got_commit_object *commit,
5244 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5246 const struct got_error *err;
5247 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5249 err = got_object_id_str(&old_id_str, hle->commit_id);
5254 err = got_object_id_str(&new_id_str, new_id);
5259 old_id_str[12] = '\0';
5261 new_id_str[12] = '\0';
5264 logmsg = strdup(hle->logmsg);
5265 if (logmsg == NULL) {
5266 err = got_error_from_errno("strdup");
5269 trim_logmsg(logmsg, 42);
5271 err = get_short_logmsg(&logmsg, 42, commit);
5276 switch (hle->cmd->code) {
5277 case GOT_HISTEDIT_PICK:
5278 case GOT_HISTEDIT_EDIT:
5279 printf("%s -> %s: %s\n", old_id_str,
5280 new_id_str ? new_id_str : "no-op change", logmsg);
5282 case GOT_HISTEDIT_DROP:
5283 case GOT_HISTEDIT_FOLD:
5284 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5297 static const struct got_error *
5298 histedit_commit(struct got_pathlist_head *merged_paths,
5299 struct got_worktree *worktree, struct got_fileindex *fileindex,
5300 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5301 struct got_repository *repo)
5303 const struct got_error *err;
5304 struct got_commit_object *commit;
5305 struct got_object_id *new_commit_id;
5307 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5308 && hle->logmsg == NULL) {
5309 err = histedit_edit_logmsg(hle, repo);
5314 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5318 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5319 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5322 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5324 err = show_histedit_progress(commit, hle, NULL);
5326 err = show_histedit_progress(commit, hle, new_commit_id);
5327 free(new_commit_id);
5330 got_object_commit_close(commit);
5334 static const struct got_error *
5335 histedit_skip_commit(struct got_histedit_list_entry *hle,
5336 struct got_worktree *worktree, struct got_repository *repo)
5338 const struct got_error *error;
5339 struct got_commit_object *commit;
5341 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5346 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5350 error = show_histedit_progress(commit, hle, NULL);
5351 got_object_commit_close(commit);
5355 static const struct got_error *
5356 cmd_histedit(int argc, char *argv[])
5358 const struct got_error *error = NULL;
5359 struct got_worktree *worktree = NULL;
5360 struct got_fileindex *fileindex = NULL;
5361 struct got_repository *repo = NULL;
5363 struct got_reference *branch = NULL;
5364 struct got_reference *tmp_branch = NULL;
5365 struct got_object_id *resume_commit_id = NULL;
5366 struct got_object_id *base_commit_id = NULL;
5367 struct got_object_id *head_commit_id = NULL;
5368 struct got_commit_object *commit = NULL;
5369 int ch, rebase_in_progress = 0, did_something;
5370 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5371 const char *edit_script_path = NULL;
5372 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5373 struct got_object_id_queue commits;
5374 struct got_pathlist_head merged_paths;
5375 const struct got_object_id_queue *parent_ids;
5376 struct got_object_qid *pid;
5377 struct got_histedit_list histedit_cmds;
5378 struct got_histedit_list_entry *hle;
5380 SIMPLEQ_INIT(&commits);
5381 TAILQ_INIT(&histedit_cmds);
5382 TAILQ_INIT(&merged_paths);
5384 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5393 edit_script_path = optarg;
5405 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5406 "unveil", NULL) == -1)
5409 if (abort_edit && continue_edit)
5415 * This command cannot apply unveil(2) in all cases because the
5416 * user may choose to run an editor to edit the histedit script
5417 * and to edit individual commit log messages.
5418 * unveil(2) traverses exec(2); if an editor is used we have to
5419 * apply unveil after edit script and log messages have been written.
5420 * XXX TODO: Make use of unveil(2) where possible.
5423 cwd = getcwd(NULL, 0);
5425 error = got_error_from_errno("getcwd");
5428 error = got_worktree_open(&worktree, cwd);
5432 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5436 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5439 if (rebase_in_progress) {
5440 error = got_error(GOT_ERR_REBASING);
5444 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5448 if (edit_in_progress && abort_edit) {
5449 error = got_worktree_histedit_continue(&resume_commit_id,
5450 &tmp_branch, &branch, &base_commit_id, &fileindex,
5454 printf("Switching work tree to %s\n",
5455 got_ref_get_symref_target(branch));
5456 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5457 branch, base_commit_id, update_progress, &did_something);
5460 printf("Histedit of %s aborted\n",
5461 got_ref_get_symref_target(branch));
5462 goto done; /* nothing else to do */
5463 } else if (abort_edit) {
5464 error = got_error(GOT_ERR_NOT_HISTEDIT);
5468 if (continue_edit) {
5471 if (!edit_in_progress) {
5472 error = got_error(GOT_ERR_NOT_HISTEDIT);
5476 error = got_worktree_get_histedit_script_path(&path, worktree);
5480 error = histedit_load_list(&histedit_cmds, path, repo);
5485 error = got_worktree_histedit_continue(&resume_commit_id,
5486 &tmp_branch, &branch, &base_commit_id, &fileindex,
5491 error = got_ref_resolve(&head_commit_id, repo, branch);
5495 error = got_object_open_as_commit(&commit, repo,
5499 parent_ids = got_object_commit_get_parent_ids(commit);
5500 pid = SIMPLEQ_FIRST(parent_ids);
5502 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5505 error = collect_commits(&commits, head_commit_id, pid->id,
5506 base_commit_id, got_worktree_get_path_prefix(worktree),
5507 GOT_ERR_HISTEDIT_PATH, repo);
5508 got_object_commit_close(commit);
5513 if (edit_in_progress) {
5514 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5518 error = got_ref_open(&branch, repo,
5519 got_worktree_get_head_ref_name(worktree), 0);
5523 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5524 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5525 "will not edit commit history of a branch outside "
5526 "the \"refs/heads/\" reference namespace");
5530 error = got_ref_resolve(&head_commit_id, repo, branch);
5531 got_ref_close(branch);
5536 error = got_object_open_as_commit(&commit, repo,
5540 parent_ids = got_object_commit_get_parent_ids(commit);
5541 pid = SIMPLEQ_FIRST(parent_ids);
5543 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5546 error = collect_commits(&commits, head_commit_id, pid->id,
5547 got_worktree_get_base_commit_id(worktree),
5548 got_worktree_get_path_prefix(worktree),
5549 GOT_ERR_HISTEDIT_PATH, repo);
5550 got_object_commit_close(commit);
5555 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5556 &base_commit_id, &fileindex, worktree, repo);
5560 if (edit_script_path) {
5561 error = histedit_load_list(&histedit_cmds,
5562 edit_script_path, repo);
5564 got_worktree_histedit_abort(worktree, fileindex,
5565 repo, branch, base_commit_id,
5566 update_progress, &did_something);
5570 error = histedit_edit_script(&histedit_cmds, &commits,
5573 got_worktree_histedit_abort(worktree, fileindex,
5574 repo, branch, base_commit_id,
5575 update_progress, &did_something);
5581 error = histedit_save_list(&histedit_cmds, worktree,
5584 got_worktree_histedit_abort(worktree, fileindex,
5585 repo, branch, base_commit_id,
5586 update_progress, &did_something);
5592 error = histedit_check_script(&histedit_cmds, &commits, repo);
5596 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5597 if (resume_commit_id) {
5598 if (got_object_id_cmp(hle->commit_id,
5599 resume_commit_id) != 0)
5602 resume_commit_id = NULL;
5603 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5604 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5605 error = histedit_skip_commit(hle, worktree,
5608 error = histedit_commit(NULL, worktree,
5609 fileindex, tmp_branch, hle, repo);
5616 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5617 error = histedit_skip_commit(hle, worktree, repo);
5623 error = got_object_open_as_commit(&commit, repo,
5627 parent_ids = got_object_commit_get_parent_ids(commit);
5628 pid = SIMPLEQ_FIRST(parent_ids);
5630 error = got_worktree_histedit_merge_files(&merged_paths,
5631 worktree, fileindex, pid->id, hle->commit_id, repo,
5632 rebase_progress, &rebase_status, check_cancelled, NULL);
5635 got_object_commit_close(commit);
5638 if (rebase_status == GOT_STATUS_CONFLICT) {
5639 got_worktree_rebase_pathlist_free(&merged_paths);
5643 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5645 error = got_object_id_str(&id_str, hle->commit_id);
5648 printf("Stopping histedit for amending commit %s\n",
5651 got_worktree_rebase_pathlist_free(&merged_paths);
5652 error = got_worktree_histedit_postpone(worktree,
5657 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5658 error = histedit_skip_commit(hle, worktree, repo);
5664 error = histedit_commit(&merged_paths, worktree, fileindex,
5665 tmp_branch, hle, repo);
5666 got_worktree_rebase_pathlist_free(&merged_paths);
5671 if (rebase_status == GOT_STATUS_CONFLICT) {
5672 error = got_worktree_histedit_postpone(worktree, fileindex);
5675 error = got_error_msg(GOT_ERR_CONFLICTS,
5676 "conflicts must be resolved before rebasing can continue");
5678 error = histedit_complete(worktree, fileindex, tmp_branch,
5681 got_object_id_queue_free(&commits);
5682 histedit_free_list(&histedit_cmds);
5683 free(head_commit_id);
5684 free(base_commit_id);
5685 free(resume_commit_id);
5687 got_object_commit_close(commit);
5689 got_ref_close(branch);
5691 got_ref_close(tmp_branch);
5693 got_worktree_close(worktree);
5695 got_repo_close(repo);
5702 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5703 "[file-path ...]\n",
5708 static const struct got_error *
5709 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5710 const char *path, struct got_object_id *blob_id,
5711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5713 const struct got_error *err = NULL;
5714 char *id_str = NULL;
5716 if (staged_status != GOT_STATUS_ADD &&
5717 staged_status != GOT_STATUS_MODIFY &&
5718 staged_status != GOT_STATUS_DELETE)
5721 if (staged_status == GOT_STATUS_ADD ||
5722 staged_status == GOT_STATUS_MODIFY)
5723 err = got_object_id_str(&id_str, staged_blob_id);
5725 err = got_object_id_str(&id_str, blob_id);
5729 printf("%s %c %s\n", id_str, staged_status, path);
5734 static const struct got_error *
5735 cmd_stage(int argc, char *argv[])
5737 const struct got_error *error = NULL;
5738 struct got_repository *repo = NULL;
5739 struct got_worktree *worktree = NULL;
5741 struct got_pathlist_head paths;
5742 struct got_pathlist_entry *pe;
5743 int ch, list_stage = 0, pflag = 0;
5744 FILE *patch_script_file = NULL;
5745 const char *patch_script_path = NULL;
5746 struct choose_patch_arg cpa;
5750 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5759 patch_script_path = optarg;
5771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5772 "unveil", NULL) == -1)
5775 if (list_stage && (pflag || patch_script_path))
5776 errx(1, "-l option cannot be used with other options");
5777 if (patch_script_path && !pflag)
5778 errx(1, "-F option can only be used together with -p option");
5780 cwd = getcwd(NULL, 0);
5782 error = got_error_from_errno("getcwd");
5786 error = got_worktree_open(&worktree, cwd);
5790 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5794 if (patch_script_path) {
5795 patch_script_file = fopen(patch_script_path, "r");
5796 if (patch_script_file == NULL) {
5797 error = got_error_from_errno2("fopen",
5802 error = apply_unveil(got_repo_get_path(repo), 1,
5803 got_worktree_get_root_path(worktree));
5807 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5812 error = got_worktree_status(worktree, &paths, repo,
5813 print_stage, NULL, check_cancelled, NULL);
5815 cpa.patch_script_file = patch_script_file;
5816 cpa.action = "stage";
5817 error = got_worktree_stage(worktree, &paths,
5818 pflag ? NULL : print_status, NULL,
5819 pflag ? choose_patch : NULL, &cpa, repo);
5822 if (patch_script_file && fclose(patch_script_file) == EOF &&
5824 error = got_error_from_errno2("fclose", patch_script_path);
5826 got_repo_close(repo);
5828 got_worktree_close(worktree);
5829 TAILQ_FOREACH(pe, &paths, entry)
5830 free((char *)pe->path);
5831 got_pathlist_free(&paths);
5839 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5840 "[file-path ...]\n",
5846 static const struct got_error *
5847 cmd_unstage(int argc, char *argv[])
5849 const struct got_error *error = NULL;
5850 struct got_repository *repo = NULL;
5851 struct got_worktree *worktree = NULL;
5853 struct got_pathlist_head paths;
5854 struct got_pathlist_entry *pe;
5855 int ch, did_something = 0, pflag = 0;
5856 FILE *patch_script_file = NULL;
5857 const char *patch_script_path = NULL;
5858 struct choose_patch_arg cpa;
5862 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5868 patch_script_path = optarg;
5880 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5881 "unveil", NULL) == -1)
5884 if (patch_script_path && !pflag)
5885 errx(1, "-F option can only be used together with -p option");
5887 cwd = getcwd(NULL, 0);
5889 error = got_error_from_errno("getcwd");
5893 error = got_worktree_open(&worktree, cwd);
5897 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5901 if (patch_script_path) {
5902 patch_script_file = fopen(patch_script_path, "r");
5903 if (patch_script_file == NULL) {
5904 error = got_error_from_errno2("fopen",
5910 error = apply_unveil(got_repo_get_path(repo), 1,
5911 got_worktree_get_root_path(worktree));
5915 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5919 cpa.patch_script_file = patch_script_file;
5920 cpa.action = "unstage";
5921 error = got_worktree_unstage(worktree, &paths, update_progress,
5922 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5924 if (patch_script_file && fclose(patch_script_file) == EOF &&
5926 error = got_error_from_errno2("fclose", patch_script_path);
5928 got_repo_close(repo);
5930 got_worktree_close(worktree);
5931 TAILQ_FOREACH(pe, &paths, entry)
5932 free((char *)pe->path);
5933 got_pathlist_free(&paths);