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 memset(&bca, 0, sizeof(bca));
2293 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2298 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2301 commit_id_str = optarg;
2304 repo_path = realpath(optarg, NULL);
2305 if (repo_path == NULL)
2306 err(1, "-r option");
2307 got_path_strip_trailing_slashes(repo_path);
2323 cwd = getcwd(NULL, 0);
2325 error = got_error_from_errno("getcwd");
2328 if (repo_path == NULL) {
2329 error = got_worktree_open(&worktree, cwd);
2330 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2336 strdup(got_worktree_get_repo_path(worktree));
2337 if (repo_path == NULL)
2338 error = got_error_from_errno("strdup");
2342 repo_path = strdup(cwd);
2343 if (repo_path == NULL) {
2344 error = got_error_from_errno("strdup");
2350 error = got_repo_open(&repo, repo_path);
2354 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2359 const char *prefix = got_worktree_get_path_prefix(worktree);
2360 char *p, *worktree_subdir = cwd +
2361 strlen(got_worktree_get_root_path(worktree));
2362 if (asprintf(&p, "%s%s%s%s%s",
2363 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2364 worktree_subdir, worktree_subdir[0] ? "/" : "",
2366 error = got_error_from_errno("asprintf");
2369 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2372 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2377 if (commit_id_str == NULL) {
2378 struct got_reference *head_ref;
2379 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2382 error = got_ref_resolve(&commit_id, repo, head_ref);
2383 got_ref_close(head_ref);
2387 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2392 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2395 if (obj_id == NULL) {
2396 error = got_error(GOT_ERR_NO_OBJ);
2400 error = got_object_get_type(&obj_type, repo, obj_id);
2404 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2405 error = got_error(GOT_ERR_OBJ_TYPE);
2409 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2412 bca.f = got_opentemp();
2413 if (bca.f == NULL) {
2414 error = got_error_from_errno("got_opentemp");
2417 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2418 &bca.line_offsets, bca.f, blob);
2419 if (error || bca.nlines == 0)
2422 /* Don't include \n at EOF in the blame line count. */
2423 if (bca.line_offsets[bca.nlines - 1] == filesize)
2426 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2427 if (bca.lines == NULL) {
2428 error = got_error_from_errno("calloc");
2432 bca.nlines_prec = 0;
2440 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca);
2450 got_object_blob_close(blob);
2452 got_worktree_close(worktree);
2454 const struct got_error *repo_error;
2455 repo_error = got_repo_close(repo);
2459 for (i = 0; i < bca.nlines; i++) {
2460 struct blame_line *bline = &bca.lines[i];
2461 free(bline->id_str);
2462 free(bline->committer);
2465 free(bca.line_offsets);
2466 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2467 error = got_error_from_errno("fclose");
2475 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2481 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2482 const char *root_path)
2484 int is_root_path = (strcmp(path, root_path) == 0);
2485 const char *modestr = "";
2487 path += strlen(root_path);
2488 while (path[0] == '/')
2491 if (S_ISLNK(te->mode))
2493 else if (S_ISDIR(te->mode))
2495 else if (te->mode & S_IXUSR)
2498 printf("%s%s%s%s%s\n", id ? id : "", path,
2499 is_root_path ? "" : "/", te->name, modestr);
2502 static const struct got_error *
2503 print_tree(const char *path, struct got_object_id *commit_id,
2504 int show_ids, int recurse, const char *root_path,
2505 struct got_repository *repo)
2507 const struct got_error *err = NULL;
2508 struct got_object_id *tree_id = NULL;
2509 struct got_tree_object *tree = NULL;
2510 const struct got_tree_entries *entries;
2511 struct got_tree_entry *te;
2513 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2517 err = got_object_open_as_tree(&tree, repo, tree_id);
2520 entries = got_object_tree_get_entries(tree);
2521 te = SIMPLEQ_FIRST(&entries->head);
2525 if (sigint_received || sigpipe_received)
2530 err = got_object_id_str(&id_str, te->id);
2533 if (asprintf(&id, "%s ", id_str) == -1) {
2534 err = got_error_from_errno("asprintf");
2540 print_entry(te, id, path, root_path);
2543 if (recurse && S_ISDIR(te->mode)) {
2545 if (asprintf(&child_path, "%s%s%s", path,
2546 path[0] == '/' && path[1] == '\0' ? "" : "/",
2548 err = got_error_from_errno("asprintf");
2551 err = print_tree(child_path, commit_id, show_ids, 1,
2558 te = SIMPLEQ_NEXT(te, entry);
2562 got_object_tree_close(tree);
2567 static const struct got_error *
2568 cmd_tree(int argc, char *argv[])
2570 const struct got_error *error;
2571 struct got_repository *repo = NULL;
2572 struct got_worktree *worktree = NULL;
2574 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2575 struct got_object_id *commit_id = NULL;
2576 char *commit_id_str = NULL;
2577 int show_ids = 0, recurse = 0;
2581 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2586 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2589 commit_id_str = optarg;
2592 repo_path = realpath(optarg, NULL);
2593 if (repo_path == NULL)
2594 err(1, "-r option");
2595 got_path_strip_trailing_slashes(repo_path);
2619 cwd = getcwd(NULL, 0);
2621 error = got_error_from_errno("getcwd");
2624 if (repo_path == NULL) {
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2632 strdup(got_worktree_get_repo_path(worktree));
2633 if (repo_path == NULL)
2634 error = got_error_from_errno("strdup");
2638 repo_path = strdup(cwd);
2639 if (repo_path == NULL) {
2640 error = got_error_from_errno("strdup");
2646 error = got_repo_open(&repo, repo_path);
2650 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2656 char *p, *worktree_subdir = cwd +
2657 strlen(got_worktree_get_root_path(worktree));
2658 if (asprintf(&p, "%s/%s",
2659 got_worktree_get_path_prefix(worktree),
2660 worktree_subdir) == -1) {
2661 error = got_error_from_errno("asprintf");
2664 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2671 if (in_repo_path == NULL) {
2672 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2677 if (commit_id_str == NULL) {
2678 struct got_reference *head_ref;
2679 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2682 error = got_ref_resolve(&commit_id, repo, head_ref);
2683 got_ref_close(head_ref);
2687 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2692 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2693 in_repo_path, repo);
2700 got_worktree_close(worktree);
2702 const struct got_error *repo_error;
2703 repo_error = got_repo_close(repo);
2713 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2717 static const struct got_error *
2718 print_status(void *arg, unsigned char status, unsigned char staged_status,
2719 const char *path, struct got_object_id *blob_id,
2720 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2722 if (status == staged_status && (status == GOT_STATUS_DELETE))
2723 status = GOT_STATUS_NO_CHANGE;
2724 printf("%c%c %s\n", status, staged_status, path);
2728 static const struct got_error *
2729 cmd_status(int argc, char *argv[])
2731 const struct got_error *error = NULL;
2732 struct got_repository *repo = NULL;
2733 struct got_worktree *worktree = NULL;
2735 struct got_pathlist_head paths;
2736 struct got_pathlist_entry *pe;
2741 while ((ch = getopt(argc, argv, "")) != -1) {
2753 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2757 cwd = getcwd(NULL, 0);
2759 error = got_error_from_errno("getcwd");
2763 error = got_worktree_open(&worktree, cwd);
2767 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2771 error = apply_unveil(got_repo_get_path(repo), 1,
2772 got_worktree_get_root_path(worktree));
2776 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2780 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2781 check_cancelled, NULL);
2783 TAILQ_FOREACH(pe, &paths, entry)
2784 free((char *)pe->path);
2785 got_pathlist_free(&paths);
2794 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2799 static const struct got_error *
2800 list_refs(struct got_repository *repo)
2802 static const struct got_error *err = NULL;
2803 struct got_reflist_head refs;
2804 struct got_reflist_entry *re;
2806 SIMPLEQ_INIT(&refs);
2807 err = got_ref_list(&refs, repo);
2811 SIMPLEQ_FOREACH(re, &refs, entry) {
2813 refstr = got_ref_to_str(re->ref);
2815 return got_error_from_errno("got_ref_to_str");
2816 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2820 got_ref_list_free(&refs);
2824 static const struct got_error *
2825 delete_ref(struct got_repository *repo, const char *refname)
2827 const struct got_error *err = NULL;
2828 struct got_reference *ref;
2830 err = got_ref_open(&ref, repo, refname, 0);
2834 err = got_ref_delete(ref, repo);
2839 static const struct got_error *
2840 add_ref(struct got_repository *repo, const char *refname, const char *target)
2842 const struct got_error *err = NULL;
2843 struct got_object_id *id;
2844 struct got_reference *ref = NULL;
2847 * Don't let the user create a reference named '-'.
2848 * While technically a valid reference name, this case is usually
2849 * an unintended typo.
2851 if (refname[0] == '-' && refname[1] == '\0')
2852 return got_error(GOT_ERR_BAD_REF_NAME);
2854 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2857 struct got_reference *target_ref;
2859 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2861 err = got_ref_open(&target_ref, repo, target, 0);
2864 err = got_ref_resolve(&id, repo, target_ref);
2865 got_ref_close(target_ref);
2870 err = got_ref_alloc(&ref, refname, id);
2874 err = got_ref_write(ref, repo);
2882 static const struct got_error *
2883 add_symref(struct got_repository *repo, const char *refname, const char *target)
2885 const struct got_error *err = NULL;
2886 struct got_reference *ref = NULL;
2887 struct got_reference *target_ref = NULL;
2890 * Don't let the user create a reference named '-'.
2891 * While technically a valid reference name, this case is usually
2892 * an unintended typo.
2894 if (refname[0] == '-' && refname[1] == '\0')
2895 return got_error(GOT_ERR_BAD_REF_NAME);
2897 err = got_ref_open(&target_ref, repo, target, 0);
2901 err = got_ref_alloc_symref(&ref, refname, target_ref);
2905 err = got_ref_write(ref, repo);
2908 got_ref_close(target_ref);
2914 static const struct got_error *
2915 cmd_ref(int argc, char *argv[])
2917 const struct got_error *error = NULL;
2918 struct got_repository *repo = NULL;
2919 struct got_worktree *worktree = NULL;
2920 char *cwd = NULL, *repo_path = NULL;
2921 int ch, do_list = 0, create_symref = 0;
2922 const char *delref = NULL;
2924 /* TODO: Add -s option for adding symbolic references. */
2925 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2931 repo_path = realpath(optarg, NULL);
2932 if (repo_path == NULL)
2933 err(1, "-r option");
2934 got_path_strip_trailing_slashes(repo_path);
2948 if (do_list && delref)
2949 errx(1, "-l and -d options are mutually exclusive\n");
2954 if (do_list || delref) {
2956 errx(1, "-s option cannot be used together with the "
2957 "-l or -d options");
2960 } else if (argc != 2)
2965 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2969 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2970 "sendfd unveil", NULL) == -1)
2974 cwd = getcwd(NULL, 0);
2976 error = got_error_from_errno("getcwd");
2980 if (repo_path == NULL) {
2981 error = got_worktree_open(&worktree, cwd);
2982 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2988 strdup(got_worktree_get_repo_path(worktree));
2989 if (repo_path == NULL)
2990 error = got_error_from_errno("strdup");
2994 repo_path = strdup(cwd);
2995 if (repo_path == NULL) {
2996 error = got_error_from_errno("strdup");
3002 error = got_repo_open(&repo, repo_path);
3006 error = apply_unveil(got_repo_get_path(repo), do_list,
3007 worktree ? got_worktree_get_root_path(worktree) : NULL);
3012 error = list_refs(repo);
3014 error = delete_ref(repo, delref);
3015 else if (create_symref)
3016 error = add_symref(repo, argv[0], argv[1]);
3018 error = add_ref(repo, argv[0], argv[1]);
3021 got_repo_close(repo);
3023 got_worktree_close(worktree);
3033 "usage: %s branch [-r repository] -l | -d name | "
3034 "name [base-branch]\n", getprogname());
3038 static const struct got_error *
3039 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3041 static const struct got_error *err = NULL;
3042 struct got_reflist_head refs;
3043 struct got_reflist_entry *re;
3045 SIMPLEQ_INIT(&refs);
3047 err = got_ref_list(&refs, repo);
3051 SIMPLEQ_FOREACH(re, &refs, entry) {
3052 const char *refname, *marker = " ";
3054 refname = got_ref_get_name(re->ref);
3055 if (strncmp(refname, "refs/heads/", 11) != 0)
3057 if (worktree && strcmp(refname,
3058 got_worktree_get_head_ref_name(worktree)) == 0) {
3059 struct got_object_id *id = NULL;
3060 err = got_ref_resolve(&id, repo, re->ref);
3063 if (got_object_id_cmp(id,
3064 got_worktree_get_base_commit_id(worktree)) == 0)
3071 refstr = got_ref_to_str(re->ref);
3073 return got_error_from_errno("got_ref_to_str");
3074 printf("%s%s: %s\n", marker, refname, refstr);
3078 got_ref_list_free(&refs);
3082 static const struct got_error *
3083 delete_branch(struct got_repository *repo, const char *branch_name)
3085 const struct got_error *err = NULL;
3086 struct got_reference *ref;
3089 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3090 return got_error_from_errno("asprintf");
3092 err = got_ref_open(&ref, repo, refname, 0);
3096 err = got_ref_delete(ref, repo);
3103 static const struct got_error *
3104 add_branch(struct got_repository *repo, const char *branch_name,
3105 const char *base_branch)
3107 const struct got_error *err = NULL;
3108 struct got_object_id *id = NULL;
3109 struct got_reference *ref = NULL;
3110 char *base_refname = NULL, *refname = NULL;
3111 struct got_reference *base_ref;
3114 * Don't let the user create a branch named '-'.
3115 * While technically a valid reference name, this case is usually
3116 * an unintended typo.
3118 if (branch_name[0] == '-' && branch_name[1] == '\0')
3119 return got_error(GOT_ERR_BAD_REF_NAME);
3121 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3122 base_refname = strdup(GOT_REF_HEAD);
3123 if (base_refname == NULL)
3124 return got_error_from_errno("strdup");
3125 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3126 return got_error_from_errno("asprintf");
3128 err = got_ref_open(&base_ref, repo, base_refname, 0);
3131 err = got_ref_resolve(&id, repo, base_ref);
3132 got_ref_close(base_ref);
3136 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3137 err = got_error_from_errno("asprintf");
3141 err = got_ref_open(&ref, repo, refname, 0);
3143 err = got_error(GOT_ERR_BRANCH_EXISTS);
3145 } else if (err->code != GOT_ERR_NOT_REF)
3148 err = got_ref_alloc(&ref, refname, id);
3152 err = got_ref_write(ref, repo);
3162 static const struct got_error *
3163 cmd_branch(int argc, char *argv[])
3165 const struct got_error *error = NULL;
3166 struct got_repository *repo = NULL;
3167 struct got_worktree *worktree = NULL;
3168 char *cwd = NULL, *repo_path = NULL;
3169 int ch, do_list = 0;
3170 const char *delref = NULL;
3172 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3178 repo_path = realpath(optarg, NULL);
3179 if (repo_path == NULL)
3180 err(1, "-r option");
3181 got_path_strip_trailing_slashes(repo_path);
3192 if (do_list && delref)
3193 errx(1, "-l and -d options are mutually exclusive\n");
3198 if (do_list || delref) {
3201 } else if (argc < 1 || argc > 2)
3206 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3210 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3211 "sendfd unveil", NULL) == -1)
3215 cwd = getcwd(NULL, 0);
3217 error = got_error_from_errno("getcwd");
3221 if (repo_path == NULL) {
3222 error = got_worktree_open(&worktree, cwd);
3223 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3229 strdup(got_worktree_get_repo_path(worktree));
3230 if (repo_path == NULL)
3231 error = got_error_from_errno("strdup");
3235 repo_path = strdup(cwd);
3236 if (repo_path == NULL) {
3237 error = got_error_from_errno("strdup");
3243 error = got_repo_open(&repo, repo_path);
3247 error = apply_unveil(got_repo_get_path(repo), do_list,
3248 worktree ? got_worktree_get_root_path(worktree) : NULL);
3253 error = list_branches(repo, worktree);
3255 error = delete_branch(repo, delref);
3257 const char *base_branch;
3259 base_branch = worktree ?
3260 got_worktree_get_head_ref_name(worktree) :
3262 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3265 base_branch = argv[1];
3266 error = add_branch(repo, argv[0], base_branch);
3270 got_repo_close(repo);
3272 got_worktree_close(worktree);
3281 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3285 static const struct got_error *
3286 cmd_add(int argc, char *argv[])
3288 const struct got_error *error = NULL;
3289 struct got_repository *repo = NULL;
3290 struct got_worktree *worktree = NULL;
3292 struct got_pathlist_head paths;
3293 struct got_pathlist_entry *pe;
3298 while ((ch = getopt(argc, argv, "")) != -1) {
3310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3317 cwd = getcwd(NULL, 0);
3319 error = got_error_from_errno("getcwd");
3323 error = got_worktree_open(&worktree, cwd);
3327 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3331 error = apply_unveil(got_repo_get_path(repo), 1,
3332 got_worktree_get_root_path(worktree));
3336 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3340 error = got_worktree_schedule_add(worktree, &paths, print_status,
3344 got_repo_close(repo);
3346 got_worktree_close(worktree);
3347 TAILQ_FOREACH(pe, &paths, entry)
3348 free((char *)pe->path);
3349 got_pathlist_free(&paths);
3357 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3361 static const struct got_error *
3362 cmd_remove(int argc, char *argv[])
3364 const struct got_error *error = NULL;
3365 struct got_worktree *worktree = NULL;
3366 struct got_repository *repo = NULL;
3368 struct got_pathlist_head paths;
3369 struct got_pathlist_entry *pe;
3370 int ch, delete_local_mods = 0;
3374 while ((ch = getopt(argc, argv, "f")) != -1) {
3377 delete_local_mods = 1;
3389 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3396 cwd = getcwd(NULL, 0);
3398 error = got_error_from_errno("getcwd");
3401 error = got_worktree_open(&worktree, cwd);
3405 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3409 error = apply_unveil(got_repo_get_path(repo), 1,
3410 got_worktree_get_root_path(worktree));
3414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3418 error = got_worktree_schedule_delete(worktree, &paths,
3419 delete_local_mods, print_status, NULL, repo);
3424 got_repo_close(repo);
3426 got_worktree_close(worktree);
3427 TAILQ_FOREACH(pe, &paths, entry)
3428 free((char *)pe->path);
3429 got_pathlist_free(&paths);
3437 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3438 "path ...\n", getprogname());
3442 static const struct got_error *
3443 revert_progress(void *arg, unsigned char status, const char *path)
3445 while (path[0] == '/')
3447 printf("%c %s\n", status, path);
3451 struct choose_patch_arg {
3452 FILE *patch_script_file;
3456 static const struct got_error *
3457 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3458 int nchanges, const char *action)
3461 size_t linesize = 0;
3465 case GOT_STATUS_ADD:
3466 printf("A %s\n%s this addition? [y/n] ", path, action);
3468 case GOT_STATUS_DELETE:
3469 printf("D %s\n%s this deletion? [y/n] ", path, action);
3471 case GOT_STATUS_MODIFY:
3472 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3473 return got_error_from_errno("fseek");
3474 printf(GOT_COMMIT_SEP_STR);
3475 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3477 if (ferror(patch_file))
3478 return got_error_from_errno("getline");
3479 printf(GOT_COMMIT_SEP_STR);
3480 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3481 path, n, nchanges, action);
3484 return got_error_path(path, GOT_ERR_FILE_STATUS);
3490 static const struct got_error *
3491 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3492 FILE *patch_file, int n, int nchanges)
3494 const struct got_error *err = NULL;
3496 size_t linesize = 0;
3499 struct choose_patch_arg *a = arg;
3501 *choice = GOT_PATCH_CHOICE_NONE;
3503 if (a->patch_script_file) {
3505 err = show_change(status, path, patch_file, n, nchanges,
3509 linelen = getline(&line, &linesize, a->patch_script_file);
3510 if (linelen == -1) {
3511 if (ferror(a->patch_script_file))
3512 return got_error_from_errno("getline");
3515 nl = strchr(line, '\n');
3518 if (strcmp(line, "y") == 0) {
3519 *choice = GOT_PATCH_CHOICE_YES;
3521 } else if (strcmp(line, "n") == 0) {
3522 *choice = GOT_PATCH_CHOICE_NO;
3524 } else if (strcmp(line, "q") == 0 &&
3525 status == GOT_STATUS_MODIFY) {
3526 *choice = GOT_PATCH_CHOICE_QUIT;
3529 printf("invalid response '%s'\n", line);
3534 while (resp != 'y' && resp != 'n' && resp != 'q') {
3535 err = show_change(status, path, patch_file, n, nchanges,
3542 if (status == GOT_STATUS_MODIFY) {
3543 if (resp != 'y' && resp != 'n' && resp != 'q') {
3544 printf("invalid response '%c'\n", resp);
3547 } else if (resp != 'y' && resp != 'n') {
3548 printf("invalid response '%c'\n", resp);
3554 *choice = GOT_PATCH_CHOICE_YES;
3555 else if (resp == 'n')
3556 *choice = GOT_PATCH_CHOICE_NO;
3557 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3558 *choice = GOT_PATCH_CHOICE_QUIT;
3564 static const struct got_error *
3565 cmd_revert(int argc, char *argv[])
3567 const struct got_error *error = NULL;
3568 struct got_worktree *worktree = NULL;
3569 struct got_repository *repo = NULL;
3570 char *cwd = NULL, *path = NULL;
3571 struct got_pathlist_head paths;
3572 struct got_pathlist_entry *pe;
3573 int ch, can_recurse = 0, pflag = 0;
3574 FILE *patch_script_file = NULL;
3575 const char *patch_script_path = NULL;
3576 struct choose_patch_arg cpa;
3580 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3586 patch_script_path = optarg;
3601 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3602 "unveil", NULL) == -1)
3607 if (patch_script_path && !pflag)
3608 errx(1, "-F option can only be used together with -p option");
3610 cwd = getcwd(NULL, 0);
3612 error = got_error_from_errno("getcwd");
3615 error = got_worktree_open(&worktree, cwd);
3619 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3623 if (patch_script_path) {
3624 patch_script_file = fopen(patch_script_path, "r");
3625 if (patch_script_file == NULL) {
3626 error = got_error_from_errno2("fopen",
3631 error = apply_unveil(got_repo_get_path(repo), 1,
3632 got_worktree_get_root_path(worktree));
3636 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3643 TAILQ_FOREACH(pe, &paths, entry) {
3644 if (asprintf(&ondisk_path, "%s/%s",
3645 got_worktree_get_root_path(worktree),
3647 error = got_error_from_errno("asprintf");
3650 if (lstat(ondisk_path, &sb) == -1) {
3651 if (errno == ENOENT) {
3655 error = got_error_from_errno2("lstat",
3661 if (S_ISDIR(sb.st_mode)) {
3662 error = got_error_msg(GOT_ERR_BAD_PATH,
3663 "reverting directories requires -R option");
3669 cpa.patch_script_file = patch_script_file;
3670 cpa.action = "revert";
3671 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3672 pflag ? choose_patch : NULL, &cpa, repo);
3676 if (patch_script_file && fclose(patch_script_file) == EOF &&
3678 error = got_error_from_errno2("fclose", patch_script_path);
3680 got_repo_close(repo);
3682 got_worktree_close(worktree);
3691 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3696 struct collect_commit_logmsg_arg {
3697 const char *cmdline_log;
3699 const char *worktree_path;
3700 const char *branch_name;
3701 const char *repo_path;
3706 static const struct got_error *
3707 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3710 char *initial_content = NULL;
3711 struct got_pathlist_entry *pe;
3712 const struct got_error *err = NULL;
3713 char *template = NULL;
3714 struct collect_commit_logmsg_arg *a = arg;
3718 /* if a message was specified on the command line, just use it */
3719 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3720 len = strlen(a->cmdline_log) + 1;
3721 *logmsg = malloc(len + 1);
3722 if (*logmsg == NULL)
3723 return got_error_from_errno("malloc");
3724 strlcpy(*logmsg, a->cmdline_log, len);
3728 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3729 return got_error_from_errno("asprintf");
3731 if (asprintf(&initial_content,
3732 "\n# changes to be committed on branch %s:\n",
3733 a->branch_name) == -1)
3734 return got_error_from_errno("asprintf");
3736 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3740 dprintf(fd, initial_content);
3742 TAILQ_FOREACH(pe, commitable_paths, entry) {
3743 struct got_commitable *ct = pe->data;
3744 dprintf(fd, "# %c %s\n",
3745 got_commitable_get_status(ct),
3746 got_commitable_get_path(ct));
3750 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3752 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3753 unlink(a->logmsg_path);
3754 free(a->logmsg_path);
3755 a->logmsg_path = NULL;
3757 free(initial_content);
3760 /* Editor is done; we can now apply unveil(2) */
3762 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3771 static const struct got_error *
3772 cmd_commit(int argc, char *argv[])
3774 const struct got_error *error = NULL;
3775 struct got_worktree *worktree = NULL;
3776 struct got_repository *repo = NULL;
3777 char *cwd = NULL, *id_str = NULL;
3778 struct got_object_id *id = NULL;
3779 const char *logmsg = NULL;
3781 struct collect_commit_logmsg_arg cl_arg;
3782 char *editor = NULL;
3783 int ch, rebase_in_progress, histedit_in_progress;
3784 struct got_pathlist_head paths;
3787 cl_arg.logmsg_path = NULL;
3789 while ((ch = getopt(argc, argv, "m:")) != -1) {
3804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3805 "unveil", NULL) == -1)
3808 error = get_author(&author);
3812 cwd = getcwd(NULL, 0);
3814 error = got_error_from_errno("getcwd");
3817 error = got_worktree_open(&worktree, cwd);
3821 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3824 if (rebase_in_progress) {
3825 error = got_error(GOT_ERR_REBASING);
3829 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3839 * unveil(2) traverses exec(2); if an editor is used we have
3840 * to apply unveil after the log message has been written.
3842 if (logmsg == NULL || strlen(logmsg) == 0)
3843 error = get_editor(&editor);
3845 error = apply_unveil(got_repo_get_path(repo), 0,
3846 got_worktree_get_root_path(worktree));
3850 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3854 cl_arg.editor = editor;
3855 cl_arg.cmdline_log = logmsg;
3856 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3857 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3858 if (!histedit_in_progress) {
3859 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3860 error = got_error(GOT_ERR_COMMIT_BRANCH);
3863 cl_arg.branch_name += 11;
3865 cl_arg.repo_path = got_repo_get_path(repo);
3866 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3867 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3869 if (cl_arg.logmsg_path)
3870 fprintf(stderr, "%s: log message preserved in %s\n",
3871 getprogname(), cl_arg.logmsg_path);
3875 if (cl_arg.logmsg_path)
3876 unlink(cl_arg.logmsg_path);
3878 error = got_object_id_str(&id_str, id);
3881 printf("Created commit %s\n", id_str);
3883 free(cl_arg.logmsg_path);
3885 got_repo_close(repo);
3887 got_worktree_close(worktree);
3895 usage_cherrypick(void)
3897 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3901 static const struct got_error *
3902 cmd_cherrypick(int argc, char *argv[])
3904 const struct got_error *error = NULL;
3905 struct got_worktree *worktree = NULL;
3906 struct got_repository *repo = NULL;
3907 char *cwd = NULL, *commit_id_str = NULL;
3908 struct got_object_id *commit_id = NULL;
3909 struct got_commit_object *commit = NULL;
3910 struct got_object_qid *pid;
3911 struct got_reference *head_ref = NULL;
3912 int ch, did_something = 0;
3914 while ((ch = getopt(argc, argv, "")) != -1) {
3926 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3927 "unveil", NULL) == -1)
3933 cwd = getcwd(NULL, 0);
3935 error = got_error_from_errno("getcwd");
3938 error = got_worktree_open(&worktree, cwd);
3942 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3946 error = apply_unveil(got_repo_get_path(repo), 0,
3947 got_worktree_get_root_path(worktree));
3951 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3952 GOT_OBJ_TYPE_COMMIT, repo);
3953 if (error != NULL) {
3954 struct got_reference *ref;
3955 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3957 error = got_ref_open(&ref, repo, argv[0], 0);
3960 error = got_ref_resolve(&commit_id, repo, ref);
3965 error = got_object_id_str(&commit_id_str, commit_id);
3969 error = got_ref_open(&head_ref, repo,
3970 got_worktree_get_head_ref_name(worktree), 0);
3974 error = check_same_branch(commit_id, head_ref, NULL, repo);
3976 if (error->code != GOT_ERR_ANCESTRY)
3980 error = got_error(GOT_ERR_SAME_BRANCH);
3984 error = got_object_open_as_commit(&commit, repo, commit_id);
3987 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3988 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3989 commit_id, repo, update_progress, &did_something, check_cancelled,
3995 printf("Merged commit %s\n", commit_id_str);
3998 got_object_commit_close(commit);
3999 free(commit_id_str);
4001 got_ref_close(head_ref);
4003 got_worktree_close(worktree);
4005 got_repo_close(repo);
4012 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4016 static const struct got_error *
4017 cmd_backout(int argc, char *argv[])
4019 const struct got_error *error = NULL;
4020 struct got_worktree *worktree = NULL;
4021 struct got_repository *repo = NULL;
4022 char *cwd = NULL, *commit_id_str = NULL;
4023 struct got_object_id *commit_id = NULL;
4024 struct got_commit_object *commit = NULL;
4025 struct got_object_qid *pid;
4026 struct got_reference *head_ref = NULL;
4027 int ch, did_something = 0;
4029 while ((ch = getopt(argc, argv, "")) != -1) {
4041 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4042 "unveil", NULL) == -1)
4048 cwd = getcwd(NULL, 0);
4050 error = got_error_from_errno("getcwd");
4053 error = got_worktree_open(&worktree, cwd);
4057 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4061 error = apply_unveil(got_repo_get_path(repo), 0,
4062 got_worktree_get_root_path(worktree));
4066 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4067 GOT_OBJ_TYPE_COMMIT, repo);
4068 if (error != NULL) {
4069 struct got_reference *ref;
4070 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4072 error = got_ref_open(&ref, repo, argv[0], 0);
4075 error = got_ref_resolve(&commit_id, repo, ref);
4080 error = got_object_id_str(&commit_id_str, commit_id);
4084 error = got_ref_open(&head_ref, repo,
4085 got_worktree_get_head_ref_name(worktree), 0);
4089 error = check_same_branch(commit_id, head_ref, NULL, repo);
4093 error = got_object_open_as_commit(&commit, repo, commit_id);
4096 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4098 error = got_error(GOT_ERR_ROOT_COMMIT);
4102 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4103 update_progress, &did_something, check_cancelled, NULL);
4108 printf("Backed out commit %s\n", commit_id_str);
4111 got_object_commit_close(commit);
4112 free(commit_id_str);
4114 got_ref_close(head_ref);
4116 got_worktree_close(worktree);
4118 got_repo_close(repo);
4125 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4131 trim_logmsg(char *logmsg, int limit)
4136 len = strlen(logmsg);
4140 nl = strchr(logmsg, '\n');
4145 static const struct got_error *
4146 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4148 const struct got_error *err;
4149 char *logmsg0 = NULL;
4152 err = got_object_commit_get_logmsg(&logmsg0, commit);
4157 while (isspace((unsigned char)s[0]))
4160 *logmsg = strdup(s);
4161 if (*logmsg == NULL) {
4162 err = got_error_from_errno("strdup");
4166 trim_logmsg(*logmsg, limit);
4172 static const struct got_error *
4173 show_rebase_progress(struct got_commit_object *commit,
4174 struct got_object_id *old_id, struct got_object_id *new_id)
4176 const struct got_error *err;
4177 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4179 err = got_object_id_str(&old_id_str, old_id);
4184 err = got_object_id_str(&new_id_str, new_id);
4189 old_id_str[12] = '\0';
4191 new_id_str[12] = '\0';
4193 err = get_short_logmsg(&logmsg, 42, commit);
4197 printf("%s -> %s: %s\n", old_id_str,
4198 new_id_str ? new_id_str : "no-op change", logmsg);
4205 static const struct got_error *
4206 rebase_progress(void *arg, unsigned char status, const char *path)
4208 unsigned char *rebase_status = arg;
4210 while (path[0] == '/')
4212 printf("%c %s\n", status, path);
4214 if (*rebase_status == GOT_STATUS_CONFLICT)
4216 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4217 *rebase_status = status;
4221 static const struct got_error *
4222 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4223 struct got_reference *branch, struct got_reference *new_base_branch,
4224 struct got_reference *tmp_branch, struct got_repository *repo)
4226 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4227 return got_worktree_rebase_complete(worktree, fileindex,
4228 new_base_branch, tmp_branch, branch, repo);
4231 static const struct got_error *
4232 rebase_commit(struct got_pathlist_head *merged_paths,
4233 struct got_worktree *worktree, struct got_fileindex *fileindex,
4234 struct got_reference *tmp_branch,
4235 struct got_object_id *commit_id, struct got_repository *repo)
4237 const struct got_error *error;
4238 struct got_commit_object *commit;
4239 struct got_object_id *new_commit_id;
4241 error = got_object_open_as_commit(&commit, repo, commit_id);
4245 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4246 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4248 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4250 error = show_rebase_progress(commit, commit_id, NULL);
4252 error = show_rebase_progress(commit, commit_id, new_commit_id);
4253 free(new_commit_id);
4256 got_object_commit_close(commit);
4260 struct check_path_prefix_arg {
4261 const char *path_prefix;
4266 static const struct got_error *
4267 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4268 struct got_blob_object *blob2, struct got_object_id *id1,
4269 struct got_object_id *id2, const char *path1, const char *path2,
4270 struct got_repository *repo)
4272 struct check_path_prefix_arg *a = arg;
4274 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4275 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4276 return got_error(a->errcode);
4281 static const struct got_error *
4282 check_path_prefix(struct got_object_id *parent_id,
4283 struct got_object_id *commit_id, const char *path_prefix,
4284 int errcode, struct got_repository *repo)
4286 const struct got_error *err;
4287 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4288 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4289 struct check_path_prefix_arg cpp_arg;
4291 if (got_path_is_root_dir(path_prefix))
4294 err = got_object_open_as_commit(&commit, repo, commit_id);
4298 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4302 err = got_object_open_as_tree(&tree1, repo,
4303 got_object_commit_get_tree_id(parent_commit));
4307 err = got_object_open_as_tree(&tree2, repo,
4308 got_object_commit_get_tree_id(commit));
4312 cpp_arg.path_prefix = path_prefix;
4313 while (cpp_arg.path_prefix[0] == '/')
4314 cpp_arg.path_prefix++;
4315 cpp_arg.len = strlen(cpp_arg.path_prefix);
4316 cpp_arg.errcode = errcode;
4317 err = got_diff_tree(tree1, tree2, "", "", repo,
4318 check_path_prefix_in_diff, &cpp_arg, 0);
4321 got_object_tree_close(tree1);
4323 got_object_tree_close(tree2);
4325 got_object_commit_close(commit);
4327 got_object_commit_close(parent_commit);
4331 static const struct got_error *
4332 collect_commits(struct got_object_id_queue *commits,
4333 struct got_object_id *initial_commit_id,
4334 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4335 const char *path_prefix, int path_prefix_errcode,
4336 struct got_repository *repo)
4338 const struct got_error *err = NULL;
4339 struct got_commit_graph *graph = NULL;
4340 struct got_object_id *parent_id = NULL;
4341 struct got_object_qid *qid;
4342 struct got_object_id *commit_id = initial_commit_id;
4344 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4348 err = got_commit_graph_iter_start(graph, iter_start_id, repo);
4351 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4352 err = got_commit_graph_iter_next(&parent_id, graph);
4354 if (err->code == GOT_ERR_ITER_COMPLETED) {
4355 err = got_error_msg(GOT_ERR_ANCESTRY,
4356 "ran out of commits to rebase before "
4357 "youngest common ancestor commit has "
4360 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4362 err = got_commit_graph_fetch_commits(graph, 1, repo);
4366 err = check_path_prefix(parent_id, commit_id,
4367 path_prefix, path_prefix_errcode, repo);
4371 err = got_object_qid_alloc(&qid, commit_id);
4374 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4375 commit_id = parent_id;
4379 got_commit_graph_close(graph);
4383 static const struct got_error *
4384 cmd_rebase(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_worktree *worktree = NULL;
4388 struct got_repository *repo = NULL;
4389 struct got_fileindex *fileindex = NULL;
4391 struct got_reference *branch = NULL;
4392 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4393 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4394 struct got_object_id *resume_commit_id = NULL;
4395 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4396 struct got_commit_object *commit = NULL;
4397 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4398 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4399 struct got_object_id_queue commits;
4400 struct got_pathlist_head merged_paths;
4401 const struct got_object_id_queue *parent_ids;
4402 struct got_object_qid *qid, *pid;
4404 SIMPLEQ_INIT(&commits);
4405 TAILQ_INIT(&merged_paths);
4407 while ((ch = getopt(argc, argv, "ac")) != -1) {
4413 continue_rebase = 1;
4425 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4426 "unveil", NULL) == -1)
4429 if (abort_rebase && continue_rebase)
4431 else if (abort_rebase || continue_rebase) {
4434 } else if (argc != 1)
4437 cwd = getcwd(NULL, 0);
4439 error = got_error_from_errno("getcwd");
4442 error = got_worktree_open(&worktree, cwd);
4446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4450 error = apply_unveil(got_repo_get_path(repo), 0,
4451 got_worktree_get_root_path(worktree));
4455 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4461 if (!rebase_in_progress) {
4462 error = got_error(GOT_ERR_NOT_REBASING);
4465 error = got_worktree_rebase_continue(&resume_commit_id,
4466 &new_base_branch, &tmp_branch, &branch, &fileindex,
4470 printf("Switching work tree to %s\n",
4471 got_ref_get_symref_target(new_base_branch));
4472 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4473 new_base_branch, update_progress, &did_something);
4476 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4477 goto done; /* nothing else to do */
4480 if (continue_rebase) {
4481 if (!rebase_in_progress) {
4482 error = got_error(GOT_ERR_NOT_REBASING);
4485 error = got_worktree_rebase_continue(&resume_commit_id,
4486 &new_base_branch, &tmp_branch, &branch, &fileindex,
4491 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4492 resume_commit_id, repo);
4496 yca_id = got_object_id_dup(resume_commit_id);
4497 if (yca_id == NULL) {
4498 error = got_error_from_errno("got_object_id_dup");
4502 error = got_ref_open(&branch, repo, argv[0], 0);
4507 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4511 if (!continue_rebase) {
4512 struct got_object_id *base_commit_id;
4514 base_commit_id = got_worktree_get_base_commit_id(worktree);
4515 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4516 base_commit_id, branch_head_commit_id, repo);
4519 if (yca_id == NULL) {
4520 error = got_error_msg(GOT_ERR_ANCESTRY,
4521 "specified branch shares no common ancestry "
4522 "with work tree's branch");
4526 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4528 if (error->code != GOT_ERR_ANCESTRY)
4532 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4533 "specified branch resolves to a commit which "
4534 "is already contained in work tree's branch");
4537 error = got_worktree_rebase_prepare(&new_base_branch,
4538 &tmp_branch, &fileindex, worktree, branch, repo);
4543 commit_id = branch_head_commit_id;
4544 error = got_object_open_as_commit(&commit, repo, commit_id);
4548 parent_ids = got_object_commit_get_parent_ids(commit);
4549 pid = SIMPLEQ_FIRST(parent_ids);
4551 if (!continue_rebase) {
4553 error = got_worktree_rebase_abort(worktree, fileindex,
4554 repo, new_base_branch, update_progress,
4558 printf("Rebase of %s aborted\n",
4559 got_ref_get_name(branch));
4561 error = got_error(GOT_ERR_EMPTY_REBASE);
4564 error = collect_commits(&commits, commit_id, pid->id,
4565 yca_id, got_worktree_get_path_prefix(worktree),
4566 GOT_ERR_REBASE_PATH, repo);
4567 got_object_commit_close(commit);
4572 if (SIMPLEQ_EMPTY(&commits)) {
4573 if (continue_rebase)
4574 error = rebase_complete(worktree, fileindex,
4575 branch, new_base_branch, tmp_branch, repo);
4577 error = got_error(GOT_ERR_EMPTY_REBASE);
4582 SIMPLEQ_FOREACH(qid, &commits, entry) {
4583 commit_id = qid->id;
4584 parent_id = pid ? pid->id : yca_id;
4587 error = got_worktree_rebase_merge_files(&merged_paths,
4588 worktree, fileindex, parent_id, commit_id, repo,
4589 rebase_progress, &rebase_status, check_cancelled, NULL);
4593 if (rebase_status == GOT_STATUS_CONFLICT) {
4594 got_worktree_rebase_pathlist_free(&merged_paths);
4598 error = rebase_commit(&merged_paths, worktree, fileindex,
4599 tmp_branch, commit_id, repo);
4600 got_worktree_rebase_pathlist_free(&merged_paths);
4605 if (rebase_status == GOT_STATUS_CONFLICT) {
4606 error = got_worktree_rebase_postpone(worktree, fileindex);
4609 error = got_error_msg(GOT_ERR_CONFLICTS,
4610 "conflicts must be resolved before rebasing can continue");
4612 error = rebase_complete(worktree, fileindex, branch,
4613 new_base_branch, tmp_branch, repo);
4615 got_object_id_queue_free(&commits);
4616 free(branch_head_commit_id);
4617 free(resume_commit_id);
4620 got_object_commit_close(commit);
4622 got_ref_close(branch);
4623 if (new_base_branch)
4624 got_ref_close(new_base_branch);
4626 got_ref_close(tmp_branch);
4628 got_worktree_close(worktree);
4630 got_repo_close(repo);
4635 usage_histedit(void)
4637 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4642 #define GOT_HISTEDIT_PICK 'p'
4643 #define GOT_HISTEDIT_EDIT 'e'
4644 #define GOT_HISTEDIT_FOLD 'f'
4645 #define GOT_HISTEDIT_DROP 'd'
4646 #define GOT_HISTEDIT_MESG 'm'
4648 static struct got_histedit_cmd {
4652 } got_histedit_cmds[] = {
4653 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4654 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4655 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4656 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4657 { GOT_HISTEDIT_MESG, "mesg",
4658 "single-line log message for commit above (open editor if empty)" },
4661 struct got_histedit_list_entry {
4662 TAILQ_ENTRY(got_histedit_list_entry) entry;
4663 struct got_object_id *commit_id;
4664 const struct got_histedit_cmd *cmd;
4667 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4669 static const struct got_error *
4670 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4671 FILE *f, struct got_repository *repo)
4673 const struct got_error *err = NULL;
4674 char *logmsg = NULL, *id_str = NULL;
4675 struct got_commit_object *commit = NULL;
4678 err = got_object_open_as_commit(&commit, repo, commit_id);
4682 err = get_short_logmsg(&logmsg, 34, commit);
4686 err = got_object_id_str(&id_str, commit_id);
4690 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4692 err = got_ferror(f, GOT_ERR_IO);
4695 got_object_commit_close(commit);
4701 static const struct got_error *
4702 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4703 struct got_repository *repo)
4705 const struct got_error *err = NULL;
4706 struct got_object_qid *qid;
4708 if (SIMPLEQ_EMPTY(commits))
4709 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4711 SIMPLEQ_FOREACH(qid, commits, entry) {
4712 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4721 static const struct got_error *
4722 write_cmd_list(FILE *f)
4724 const struct got_error *err = NULL;
4727 n = fprintf(f, "# Available histedit commands:\n");
4729 return got_ferror(f, GOT_ERR_IO);
4731 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4732 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4733 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4736 err = got_ferror(f, GOT_ERR_IO);
4740 n = fprintf(f, "# Commits will be processed in order from top to "
4741 "bottom of this file.\n");
4743 return got_ferror(f, GOT_ERR_IO);
4747 static const struct got_error *
4748 histedit_syntax_error(int lineno)
4750 static char msg[42];
4753 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4755 if (ret == -1 || ret >= sizeof(msg))
4756 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4758 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4761 static const struct got_error *
4762 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4763 char *logmsg, struct got_repository *repo)
4765 const struct got_error *err;
4766 struct got_commit_object *folded_commit = NULL;
4767 char *id_str, *folded_logmsg = NULL;
4769 err = got_object_id_str(&id_str, hle->commit_id);
4773 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4777 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4780 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4781 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4782 folded_logmsg) == -1) {
4783 err = got_error_from_errno("asprintf");
4788 got_object_commit_close(folded_commit);
4790 free(folded_logmsg);
4794 static struct got_histedit_list_entry *
4795 get_folded_commits(struct got_histedit_list_entry *hle)
4797 struct got_histedit_list_entry *prev, *folded = NULL;
4799 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4800 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4801 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4802 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4804 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4810 static const struct got_error *
4811 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4812 struct got_repository *repo)
4814 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4815 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4816 const struct got_error *err = NULL;
4817 struct got_commit_object *commit = NULL;
4819 struct got_histedit_list_entry *folded = NULL;
4821 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4825 folded = get_folded_commits(hle);
4827 while (folded != hle) {
4828 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4829 folded = TAILQ_NEXT(folded, entry);
4832 err = append_folded_commit_msg(&new_msg, folded,
4838 folded = TAILQ_NEXT(folded, entry);
4842 err = got_object_id_str(&id_str, hle->commit_id);
4845 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4848 if (asprintf(&new_msg,
4849 "%s\n# original log message of commit %s: %s",
4850 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4851 err = got_error_from_errno("asprintf");
4857 err = got_object_id_str(&id_str, hle->commit_id);
4861 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4865 dprintf(fd, logmsg);
4868 err = get_editor(&editor);
4872 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4874 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4876 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4879 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4880 err = got_error_from_errno2("unlink", logmsg_path);
4886 got_object_commit_close(commit);
4890 static const struct got_error *
4891 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4892 FILE *f, struct got_repository *repo)
4894 const struct got_error *err = NULL;
4895 char *line = NULL, *p, *end;
4899 const struct got_histedit_cmd *cmd;
4900 struct got_object_id *commit_id = NULL;
4901 struct got_histedit_list_entry *hle = NULL;
4904 len = getline(&line, &size, f);
4906 const struct got_error *getline_err;
4909 getline_err = got_error_from_errno("getline");
4910 err = got_ferror(f, getline_err->code);
4915 while (isspace((unsigned char)p[0]))
4917 if (p[0] == '#' || p[0] == '\0') {
4923 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4924 cmd = &got_histedit_cmds[i];
4925 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4926 isspace((unsigned char)p[strlen(cmd->name)])) {
4927 p += strlen(cmd->name);
4930 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4935 if (i == nitems(got_histedit_cmds)) {
4936 err = histedit_syntax_error(lineno);
4939 while (isspace((unsigned char)p[0]))
4941 if (cmd->code == GOT_HISTEDIT_MESG) {
4942 if (hle == NULL || hle->logmsg != NULL) {
4943 err = got_error(GOT_ERR_HISTEDIT_CMD);
4947 err = histedit_edit_logmsg(hle, repo);
4951 hle->logmsg = strdup(p);
4952 if (hle->logmsg == NULL) {
4953 err = got_error_from_errno("strdup");
4962 while (end[0] && !isspace((unsigned char)end[0]))
4966 err = got_object_resolve_id_str(&commit_id, repo, p);
4968 /* override error code */
4969 err = histedit_syntax_error(lineno);
4973 hle = malloc(sizeof(*hle));
4975 err = got_error_from_errno("malloc");
4979 hle->commit_id = commit_id;
4984 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4992 static const struct got_error *
4993 histedit_check_script(struct got_histedit_list *histedit_cmds,
4994 struct got_object_id_queue *commits, struct got_repository *repo)
4996 const struct got_error *err = NULL;
4997 struct got_object_qid *qid;
4998 struct got_histedit_list_entry *hle;
4999 static char msg[80];
5002 if (TAILQ_EMPTY(histedit_cmds))
5003 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5004 "histedit script contains no commands");
5005 if (SIMPLEQ_EMPTY(commits))
5006 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5008 SIMPLEQ_FOREACH(qid, commits, entry) {
5009 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5010 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5014 err = got_object_id_str(&id_str, qid->id);
5017 snprintf(msg, sizeof(msg),
5018 "commit %s missing from histedit script", id_str);
5020 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5024 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5025 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5026 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5027 "last commit in histedit script cannot be folded");
5032 static const struct got_error *
5033 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5034 const char *path, struct got_object_id_queue *commits,
5035 struct got_repository *repo)
5037 const struct got_error *err = NULL;
5041 err = get_editor(&editor);
5045 if (spawn_editor(editor, path) == -1) {
5046 err = got_error_from_errno("failed spawning editor");
5050 f = fopen(path, "r");
5052 err = got_error_from_errno("fopen");
5055 err = histedit_parse_list(histedit_cmds, f, repo);
5059 err = histedit_check_script(histedit_cmds, commits, repo);
5061 if (f && fclose(f) != 0 && err == NULL)
5062 err = got_error_from_errno("fclose");
5067 static const struct got_error *
5068 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5069 struct got_object_id_queue *, const char *, struct got_repository *);
5071 static const struct got_error *
5072 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5073 struct got_object_id_queue *commits, struct got_repository *repo)
5075 const struct got_error *err;
5079 err = got_opentemp_named(&path, &f, "got-histedit");
5083 err = write_cmd_list(f);
5087 err = histedit_write_commit_list(commits, f, repo);
5091 if (fclose(f) != 0) {
5092 err = got_error_from_errno("fclose");
5097 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5099 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5100 err->code != GOT_ERR_HISTEDIT_CMD)
5102 err = histedit_edit_list_retry(histedit_cmds, err,
5103 commits, path, repo);
5106 if (f && fclose(f) != 0 && err == NULL)
5107 err = got_error_from_errno("fclose");
5108 if (path && unlink(path) != 0 && err == NULL)
5109 err = got_error_from_errno2("unlink", path);
5114 static const struct got_error *
5115 histedit_save_list(struct got_histedit_list *histedit_cmds,
5116 struct got_worktree *worktree, struct got_repository *repo)
5118 const struct got_error *err = NULL;
5121 struct got_histedit_list_entry *hle;
5122 struct got_commit_object *commit = NULL;
5124 err = got_worktree_get_histedit_script_path(&path, worktree);
5128 f = fopen(path, "w");
5130 err = got_error_from_errno2("fopen", path);
5133 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5134 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5140 int n = fprintf(f, "%c %s\n",
5141 GOT_HISTEDIT_MESG, hle->logmsg);
5143 err = got_ferror(f, GOT_ERR_IO);
5149 if (f && fclose(f) != 0 && err == NULL)
5150 err = got_error_from_errno("fclose");
5153 got_object_commit_close(commit);
5158 histedit_free_list(struct got_histedit_list *histedit_cmds)
5160 struct got_histedit_list_entry *hle;
5162 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5163 TAILQ_REMOVE(histedit_cmds, hle, entry);
5168 static const struct got_error *
5169 histedit_load_list(struct got_histedit_list *histedit_cmds,
5170 const char *path, struct got_repository *repo)
5172 const struct got_error *err = NULL;
5175 f = fopen(path, "r");
5177 err = got_error_from_errno2("fopen", path);
5181 err = histedit_parse_list(histedit_cmds, f, repo);
5183 if (f && fclose(f) != 0 && err == NULL)
5184 err = got_error_from_errno("fclose");
5188 static const struct got_error *
5189 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5190 const struct got_error *edit_err, struct got_object_id_queue *commits,
5191 const char *path, struct got_repository *repo)
5193 const struct got_error *err = NULL, *prev_err = edit_err;
5196 while (resp != 'c' && resp != 'r' && resp != 'a') {
5197 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5198 "or (a)bort: ", getprogname(), prev_err->msg);
5203 histedit_free_list(histedit_cmds);
5204 err = histedit_run_editor(histedit_cmds, path, commits,
5207 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5208 err->code != GOT_ERR_HISTEDIT_CMD)
5215 } else if (resp == 'r') {
5216 histedit_free_list(histedit_cmds);
5217 err = histedit_edit_script(histedit_cmds,
5220 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5221 err->code != GOT_ERR_HISTEDIT_CMD)
5228 } else if (resp == 'a') {
5229 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5232 printf("invalid response '%c'\n", resp);
5238 static const struct got_error *
5239 histedit_complete(struct got_worktree *worktree,
5240 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5241 struct got_reference *branch, struct got_repository *repo)
5243 printf("Switching work tree to %s\n",
5244 got_ref_get_symref_target(branch));
5245 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5249 static const struct got_error *
5250 show_histedit_progress(struct got_commit_object *commit,
5251 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5253 const struct got_error *err;
5254 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5256 err = got_object_id_str(&old_id_str, hle->commit_id);
5261 err = got_object_id_str(&new_id_str, new_id);
5266 old_id_str[12] = '\0';
5268 new_id_str[12] = '\0';
5271 logmsg = strdup(hle->logmsg);
5272 if (logmsg == NULL) {
5273 err = got_error_from_errno("strdup");
5276 trim_logmsg(logmsg, 42);
5278 err = get_short_logmsg(&logmsg, 42, commit);
5283 switch (hle->cmd->code) {
5284 case GOT_HISTEDIT_PICK:
5285 case GOT_HISTEDIT_EDIT:
5286 printf("%s -> %s: %s\n", old_id_str,
5287 new_id_str ? new_id_str : "no-op change", logmsg);
5289 case GOT_HISTEDIT_DROP:
5290 case GOT_HISTEDIT_FOLD:
5291 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5304 static const struct got_error *
5305 histedit_commit(struct got_pathlist_head *merged_paths,
5306 struct got_worktree *worktree, struct got_fileindex *fileindex,
5307 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5308 struct got_repository *repo)
5310 const struct got_error *err;
5311 struct got_commit_object *commit;
5312 struct got_object_id *new_commit_id;
5314 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5315 && hle->logmsg == NULL) {
5316 err = histedit_edit_logmsg(hle, repo);
5321 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5325 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5326 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5329 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5331 err = show_histedit_progress(commit, hle, NULL);
5333 err = show_histedit_progress(commit, hle, new_commit_id);
5334 free(new_commit_id);
5337 got_object_commit_close(commit);
5341 static const struct got_error *
5342 histedit_skip_commit(struct got_histedit_list_entry *hle,
5343 struct got_worktree *worktree, struct got_repository *repo)
5345 const struct got_error *error;
5346 struct got_commit_object *commit;
5348 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5353 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5357 error = show_histedit_progress(commit, hle, NULL);
5358 got_object_commit_close(commit);
5362 static const struct got_error *
5363 cmd_histedit(int argc, char *argv[])
5365 const struct got_error *error = NULL;
5366 struct got_worktree *worktree = NULL;
5367 struct got_fileindex *fileindex = NULL;
5368 struct got_repository *repo = NULL;
5370 struct got_reference *branch = NULL;
5371 struct got_reference *tmp_branch = NULL;
5372 struct got_object_id *resume_commit_id = NULL;
5373 struct got_object_id *base_commit_id = NULL;
5374 struct got_object_id *head_commit_id = NULL;
5375 struct got_commit_object *commit = NULL;
5376 int ch, rebase_in_progress = 0, did_something;
5377 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5378 const char *edit_script_path = NULL;
5379 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5380 struct got_object_id_queue commits;
5381 struct got_pathlist_head merged_paths;
5382 const struct got_object_id_queue *parent_ids;
5383 struct got_object_qid *pid;
5384 struct got_histedit_list histedit_cmds;
5385 struct got_histedit_list_entry *hle;
5387 SIMPLEQ_INIT(&commits);
5388 TAILQ_INIT(&histedit_cmds);
5389 TAILQ_INIT(&merged_paths);
5391 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5400 edit_script_path = optarg;
5412 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5413 "unveil", NULL) == -1)
5416 if (abort_edit && continue_edit)
5422 * This command cannot apply unveil(2) in all cases because the
5423 * user may choose to run an editor to edit the histedit script
5424 * and to edit individual commit log messages.
5425 * unveil(2) traverses exec(2); if an editor is used we have to
5426 * apply unveil after edit script and log messages have been written.
5427 * XXX TODO: Make use of unveil(2) where possible.
5430 cwd = getcwd(NULL, 0);
5432 error = got_error_from_errno("getcwd");
5435 error = got_worktree_open(&worktree, cwd);
5439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5443 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5446 if (rebase_in_progress) {
5447 error = got_error(GOT_ERR_REBASING);
5451 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5455 if (edit_in_progress && abort_edit) {
5456 error = got_worktree_histedit_continue(&resume_commit_id,
5457 &tmp_branch, &branch, &base_commit_id, &fileindex,
5461 printf("Switching work tree to %s\n",
5462 got_ref_get_symref_target(branch));
5463 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5464 branch, base_commit_id, update_progress, &did_something);
5467 printf("Histedit of %s aborted\n",
5468 got_ref_get_symref_target(branch));
5469 goto done; /* nothing else to do */
5470 } else if (abort_edit) {
5471 error = got_error(GOT_ERR_NOT_HISTEDIT);
5475 if (continue_edit) {
5478 if (!edit_in_progress) {
5479 error = got_error(GOT_ERR_NOT_HISTEDIT);
5483 error = got_worktree_get_histedit_script_path(&path, worktree);
5487 error = histedit_load_list(&histedit_cmds, path, repo);
5492 error = got_worktree_histedit_continue(&resume_commit_id,
5493 &tmp_branch, &branch, &base_commit_id, &fileindex,
5498 error = got_ref_resolve(&head_commit_id, repo, branch);
5502 error = got_object_open_as_commit(&commit, repo,
5506 parent_ids = got_object_commit_get_parent_ids(commit);
5507 pid = SIMPLEQ_FIRST(parent_ids);
5509 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5512 error = collect_commits(&commits, head_commit_id, pid->id,
5513 base_commit_id, got_worktree_get_path_prefix(worktree),
5514 GOT_ERR_HISTEDIT_PATH, repo);
5515 got_object_commit_close(commit);
5520 if (edit_in_progress) {
5521 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5525 error = got_ref_open(&branch, repo,
5526 got_worktree_get_head_ref_name(worktree), 0);
5530 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5531 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5532 "will not edit commit history of a branch outside "
5533 "the \"refs/heads/\" reference namespace");
5537 error = got_ref_resolve(&head_commit_id, repo, branch);
5538 got_ref_close(branch);
5543 error = got_object_open_as_commit(&commit, repo,
5547 parent_ids = got_object_commit_get_parent_ids(commit);
5548 pid = SIMPLEQ_FIRST(parent_ids);
5550 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5553 error = collect_commits(&commits, head_commit_id, pid->id,
5554 got_worktree_get_base_commit_id(worktree),
5555 got_worktree_get_path_prefix(worktree),
5556 GOT_ERR_HISTEDIT_PATH, repo);
5557 got_object_commit_close(commit);
5562 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5563 &base_commit_id, &fileindex, worktree, repo);
5567 if (edit_script_path) {
5568 error = histedit_load_list(&histedit_cmds,
5569 edit_script_path, repo);
5571 got_worktree_histedit_abort(worktree, fileindex,
5572 repo, branch, base_commit_id,
5573 update_progress, &did_something);
5577 error = histedit_edit_script(&histedit_cmds, &commits,
5580 got_worktree_histedit_abort(worktree, fileindex,
5581 repo, branch, base_commit_id,
5582 update_progress, &did_something);
5588 error = histedit_save_list(&histedit_cmds, worktree,
5591 got_worktree_histedit_abort(worktree, fileindex,
5592 repo, branch, base_commit_id,
5593 update_progress, &did_something);
5599 error = histedit_check_script(&histedit_cmds, &commits, repo);
5603 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5604 if (resume_commit_id) {
5605 if (got_object_id_cmp(hle->commit_id,
5606 resume_commit_id) != 0)
5609 resume_commit_id = NULL;
5610 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5611 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5612 error = histedit_skip_commit(hle, worktree,
5615 error = histedit_commit(NULL, worktree,
5616 fileindex, tmp_branch, hle, repo);
5623 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5624 error = histedit_skip_commit(hle, worktree, repo);
5630 error = got_object_open_as_commit(&commit, repo,
5634 parent_ids = got_object_commit_get_parent_ids(commit);
5635 pid = SIMPLEQ_FIRST(parent_ids);
5637 error = got_worktree_histedit_merge_files(&merged_paths,
5638 worktree, fileindex, pid->id, hle->commit_id, repo,
5639 rebase_progress, &rebase_status, check_cancelled, NULL);
5642 got_object_commit_close(commit);
5645 if (rebase_status == GOT_STATUS_CONFLICT) {
5646 got_worktree_rebase_pathlist_free(&merged_paths);
5650 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5652 error = got_object_id_str(&id_str, hle->commit_id);
5655 printf("Stopping histedit for amending commit %s\n",
5658 got_worktree_rebase_pathlist_free(&merged_paths);
5659 error = got_worktree_histedit_postpone(worktree,
5664 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5665 error = histedit_skip_commit(hle, worktree, repo);
5671 error = histedit_commit(&merged_paths, worktree, fileindex,
5672 tmp_branch, hle, repo);
5673 got_worktree_rebase_pathlist_free(&merged_paths);
5678 if (rebase_status == GOT_STATUS_CONFLICT) {
5679 error = got_worktree_histedit_postpone(worktree, fileindex);
5682 error = got_error_msg(GOT_ERR_CONFLICTS,
5683 "conflicts must be resolved before rebasing can continue");
5685 error = histedit_complete(worktree, fileindex, tmp_branch,
5688 got_object_id_queue_free(&commits);
5689 histedit_free_list(&histedit_cmds);
5690 free(head_commit_id);
5691 free(base_commit_id);
5692 free(resume_commit_id);
5694 got_object_commit_close(commit);
5696 got_ref_close(branch);
5698 got_ref_close(tmp_branch);
5700 got_worktree_close(worktree);
5702 got_repo_close(repo);
5709 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5710 "[file-path ...]\n",
5715 static const struct got_error *
5716 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5717 const char *path, struct got_object_id *blob_id,
5718 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5720 const struct got_error *err = NULL;
5721 char *id_str = NULL;
5723 if (staged_status != GOT_STATUS_ADD &&
5724 staged_status != GOT_STATUS_MODIFY &&
5725 staged_status != GOT_STATUS_DELETE)
5728 if (staged_status == GOT_STATUS_ADD ||
5729 staged_status == GOT_STATUS_MODIFY)
5730 err = got_object_id_str(&id_str, staged_blob_id);
5732 err = got_object_id_str(&id_str, blob_id);
5736 printf("%s %c %s\n", id_str, staged_status, path);
5741 static const struct got_error *
5742 cmd_stage(int argc, char *argv[])
5744 const struct got_error *error = NULL;
5745 struct got_repository *repo = NULL;
5746 struct got_worktree *worktree = NULL;
5748 struct got_pathlist_head paths;
5749 struct got_pathlist_entry *pe;
5750 int ch, list_stage = 0, pflag = 0;
5751 FILE *patch_script_file = NULL;
5752 const char *patch_script_path = NULL;
5753 struct choose_patch_arg cpa;
5757 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5766 patch_script_path = optarg;
5778 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 "unveil", NULL) == -1)
5782 if (list_stage && (pflag || patch_script_path))
5783 errx(1, "-l option cannot be used with other options");
5784 if (patch_script_path && !pflag)
5785 errx(1, "-F option can only be used together with -p option");
5787 cwd = getcwd(NULL, 0);
5789 error = got_error_from_errno("getcwd");
5793 error = got_worktree_open(&worktree, cwd);
5797 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5801 if (patch_script_path) {
5802 patch_script_file = fopen(patch_script_path, "r");
5803 if (patch_script_file == NULL) {
5804 error = got_error_from_errno2("fopen",
5809 error = apply_unveil(got_repo_get_path(repo), 1,
5810 got_worktree_get_root_path(worktree));
5814 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5819 error = got_worktree_status(worktree, &paths, repo,
5820 print_stage, NULL, check_cancelled, NULL);
5822 cpa.patch_script_file = patch_script_file;
5823 cpa.action = "stage";
5824 error = got_worktree_stage(worktree, &paths,
5825 pflag ? NULL : print_status, NULL,
5826 pflag ? choose_patch : NULL, &cpa, repo);
5829 if (patch_script_file && fclose(patch_script_file) == EOF &&
5831 error = got_error_from_errno2("fclose", patch_script_path);
5833 got_repo_close(repo);
5835 got_worktree_close(worktree);
5836 TAILQ_FOREACH(pe, &paths, entry)
5837 free((char *)pe->path);
5838 got_pathlist_free(&paths);
5846 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5847 "[file-path ...]\n",
5853 static const struct got_error *
5854 cmd_unstage(int argc, char *argv[])
5856 const struct got_error *error = NULL;
5857 struct got_repository *repo = NULL;
5858 struct got_worktree *worktree = NULL;
5860 struct got_pathlist_head paths;
5861 struct got_pathlist_entry *pe;
5862 int ch, did_something = 0, pflag = 0;
5863 FILE *patch_script_file = NULL;
5864 const char *patch_script_path = NULL;
5865 struct choose_patch_arg cpa;
5869 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5875 patch_script_path = optarg;
5887 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5888 "unveil", NULL) == -1)
5891 if (patch_script_path && !pflag)
5892 errx(1, "-F option can only be used together with -p option");
5894 cwd = getcwd(NULL, 0);
5896 error = got_error_from_errno("getcwd");
5900 error = got_worktree_open(&worktree, cwd);
5904 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5908 if (patch_script_path) {
5909 patch_script_file = fopen(patch_script_path, "r");
5910 if (patch_script_file == NULL) {
5911 error = got_error_from_errno2("fopen",
5917 error = apply_unveil(got_repo_get_path(repo), 1,
5918 got_worktree_get_root_path(worktree));
5922 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5926 cpa.patch_script_file = patch_script_file;
5927 cpa.action = "unstage";
5928 error = got_worktree_unstage(worktree, &paths, update_progress,
5929 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5931 if (patch_script_file && fclose(patch_script_file) == EOF &&
5933 error = got_error_from_errno2("fclose", patch_script_path);
5935 got_repo_close(repo);
5937 got_worktree_close(worktree);
5938 TAILQ_FOREACH(pe, &paths, entry)
5939 free((char *)pe->path);
5940 got_pathlist_free(&paths);