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_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_add(void);
92 __dead static void usage_remove(void);
93 __dead static void usage_revert(void);
94 __dead static void usage_commit(void);
95 __dead static void usage_cherrypick(void);
96 __dead static void usage_backout(void);
97 __dead static void usage_rebase(void);
98 __dead static void usage_histedit(void);
99 __dead static void usage_stage(void);
100 __dead static void usage_unstage(void);
102 static const struct got_error* cmd_init(int, char *[]);
103 static const struct got_error* cmd_import(int, char *[]);
104 static const struct got_error* cmd_checkout(int, char *[]);
105 static const struct got_error* cmd_update(int, char *[]);
106 static const struct got_error* cmd_log(int, char *[]);
107 static const struct got_error* cmd_diff(int, char *[]);
108 static const struct got_error* cmd_blame(int, char *[]);
109 static const struct got_error* cmd_tree(int, char *[]);
110 static const struct got_error* cmd_status(int, char *[]);
111 static const struct got_error* cmd_ref(int, char *[]);
112 static const struct got_error* cmd_branch(int, char *[]);
113 static const struct got_error* cmd_add(int, char *[]);
114 static const struct got_error* cmd_remove(int, char *[]);
115 static const struct got_error* cmd_revert(int, char *[]);
116 static const struct got_error* cmd_commit(int, char *[]);
117 static const struct got_error* cmd_cherrypick(int, char *[]);
118 static const struct got_error* cmd_backout(int, char *[]);
119 static const struct got_error* cmd_rebase(int, char *[]);
120 static const struct got_error* cmd_histedit(int, char *[]);
121 static const struct got_error* cmd_stage(int, char *[]);
122 static const struct got_error* cmd_unstage(int, char *[]);
124 static struct got_cmd got_commands[] = {
125 { "init", cmd_init, usage_init, "in" },
126 { "import", cmd_import, usage_import, "im" },
127 { "checkout", cmd_checkout, usage_checkout, "co" },
128 { "update", cmd_update, usage_update, "up" },
129 { "log", cmd_log, usage_log, "" },
130 { "diff", cmd_diff, usage_diff, "di" },
131 { "blame", cmd_blame, usage_blame, "bl" },
132 { "tree", cmd_tree, usage_tree, "tr" },
133 { "status", cmd_status, usage_status, "st" },
134 { "ref", cmd_ref, usage_ref, "" },
135 { "branch", cmd_branch, usage_branch, "br" },
136 { "add", cmd_add, usage_add, "" },
137 { "remove", cmd_remove, usage_remove, "rm" },
138 { "revert", cmd_revert, usage_revert, "rv" },
139 { "commit", cmd_commit, usage_commit, "ci" },
140 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
141 { "backout", cmd_backout, usage_backout, "bo" },
142 { "rebase", cmd_rebase, usage_rebase, "rb" },
143 { "histedit", cmd_histedit, usage_histedit, "he" },
144 { "stage", cmd_stage, usage_stage, "sg" },
145 { "unstage", cmd_unstage, usage_unstage, "ug" },
153 fprintf(stderr, "commands:");
154 for (i = 0; i < nitems(got_commands); i++) {
155 struct got_cmd *cmd = &got_commands[i];
156 fprintf(stderr, " %s", cmd->cmd_name);
162 main(int argc, char *argv[])
167 int hflag = 0, Vflag = 0;
169 setlocale(LC_CTYPE, "");
171 while ((ch = getopt(argc, argv, "hV")) != -1) {
190 got_version_print_str();
197 signal(SIGINT, catch_sigint);
198 signal(SIGPIPE, catch_sigpipe);
200 for (i = 0; i < nitems(got_commands); i++) {
201 const struct got_error *error;
203 cmd = &got_commands[i];
205 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
206 strcmp(cmd->cmd_alias, argv[0]) != 0)
210 got_commands[i].cmd_usage();
212 error = got_commands[i].cmd_main(argc, argv);
213 if (error && !(sigint_received || sigpipe_received)) {
214 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
221 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
229 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
236 static const struct got_error *
237 get_editor(char **abspath)
239 const struct got_error *err = NULL;
244 editor = getenv("VISUAL");
246 editor = getenv("EDITOR");
249 err = got_path_find_prog(abspath, editor);
254 if (*abspath == NULL) {
255 *abspath = strdup("/bin/ed");
256 if (*abspath == NULL)
257 return got_error_from_errno("strdup");
263 static const struct got_error *
264 apply_unveil(const char *repo_path, int repo_read_only,
265 const char *worktree_path)
267 const struct got_error *err;
270 if (unveil("gmon.out", "rwc") != 0)
271 return got_error_from_errno2("unveil", "gmon.out");
273 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
274 return got_error_from_errno2("unveil", repo_path);
276 if (worktree_path && unveil(worktree_path, "rwc") != 0)
277 return got_error_from_errno2("unveil", worktree_path);
279 if (unveil("/tmp", "rwc") != 0)
280 return got_error_from_errno2("unveil", "/tmp");
282 err = got_privsep_unveil_exec_helpers();
286 if (unveil(NULL, NULL) != 0)
287 return got_error_from_errno("unveil");
295 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
299 static const struct got_error *
300 cmd_init(int argc, char *argv[])
302 const struct got_error *error = NULL;
303 char *repo_path = NULL;
306 while ((ch = getopt(argc, argv, "")) != -1) {
318 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
324 repo_path = strdup(argv[0]);
325 if (repo_path == NULL)
326 return got_error_from_errno("strdup");
328 got_path_strip_trailing_slashes(repo_path);
330 error = got_path_mkdir(repo_path);
332 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
335 error = apply_unveil(repo_path, 0, NULL);
339 error = got_repo_init(repo_path);
351 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
352 "[-r repository-path] [-I pattern] path\n", getprogname());
357 spawn_editor(const char *editor, const char *file)
360 sig_t sighup, sigint, sigquit;
363 sighup = signal(SIGHUP, SIG_IGN);
364 sigint = signal(SIGINT, SIG_IGN);
365 sigquit = signal(SIGQUIT, SIG_IGN);
367 switch (pid = fork()) {
371 execl(editor, editor, file, (char *)NULL);
375 while (waitpid(pid, &st, 0) == -1)
380 (void)signal(SIGHUP, sighup);
381 (void)signal(SIGINT, sigint);
382 (void)signal(SIGQUIT, sigquit);
384 if (!WIFEXITED(st)) {
389 return WEXITSTATUS(st);
392 static const struct got_error *
393 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
394 const char *initial_content)
396 const struct got_error *err = NULL;
400 int content_changed = 0;
405 if (stat(logmsg_path, &st) == -1)
406 return got_error_from_errno2("stat", logmsg_path);
408 if (spawn_editor(editor, logmsg_path) == -1)
409 return got_error_from_errno("failed spawning editor");
411 if (stat(logmsg_path, &st2) == -1)
412 return got_error_from_errno("stat");
414 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
415 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
416 "no changes made to commit message, aborting");
418 *logmsg = malloc(st2.st_size + 1);
420 return got_error_from_errno("malloc");
424 fp = fopen(logmsg_path, "r");
426 err = got_error_from_errno("fopen");
429 while (fgets(buf, sizeof(buf), fp) != NULL) {
430 if (!content_changed && strcmp(buf, initial_content) != 0)
432 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
433 continue; /* remove comments and leading empty lines */
434 len = strlcat(*logmsg, buf, st2.st_size);
438 while (len > 0 && (*logmsg)[len - 1] == '\n') {
439 (*logmsg)[len - 1] = '\0';
443 if (len == 0 || !content_changed)
444 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
445 "commit message cannot be empty, aborting");
454 static const struct got_error *
455 collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
456 const char *branch_name)
458 char *initial_content = NULL, *logmsg_path = NULL;
459 const struct got_error *err = NULL;
462 if (asprintf(&initial_content,
463 "\n# %s to be imported to branch %s\n", path_dir,
465 return got_error_from_errno("asprintf");
467 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
471 dprintf(fd, initial_content);
474 err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
476 free(initial_content);
481 static const struct got_error *
482 import_progress(void *arg, const char *path)
484 printf("A %s\n", path);
488 static const struct got_error *
489 get_author(const char **author)
491 const char *got_author;
495 got_author = getenv("GOT_AUTHOR");
496 if (got_author == NULL) {
497 /* TODO: Look up user in password database? */
498 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
501 *author = got_author;
504 * Really dumb email address check; we're only doing this to
505 * avoid git's object parser breaking on commits we create.
507 while (*got_author && *got_author != '<')
509 if (*got_author != '<')
510 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
511 while (*got_author && *got_author != '@')
513 if (*got_author != '@')
514 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
515 while (*got_author && *got_author != '>')
517 if (*got_author != '>')
518 return got_error(GOT_ERR_COMMIT_NO_EMAIL);
523 static const struct got_error *
524 cmd_import(int argc, char *argv[])
526 const struct got_error *error = NULL;
527 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
530 const char *branch_name = "master";
531 char *refname = NULL, *id_str = NULL;
532 struct got_repository *repo = NULL;
533 struct got_reference *branch_ref = NULL, *head_ref = NULL;
534 struct got_object_id *new_commit_id = NULL;
536 struct got_pathlist_head ignores;
537 struct got_pathlist_entry *pe;
539 TAILQ_INIT(&ignores);
541 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
544 branch_name = optarg;
547 logmsg = strdup(optarg);
548 if (logmsg == NULL) {
549 error = got_error_from_errno("strdup");
554 repo_path = realpath(optarg, NULL);
555 if (repo_path == NULL) {
556 error = got_error_from_errno("realpath");
561 if (optarg[0] == '\0')
563 error = got_pathlist_insert(&pe, &ignores, optarg,
578 if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
585 error = get_author(&author);
589 if (repo_path == NULL) {
590 repo_path = getcwd(NULL, 0);
591 if (repo_path == NULL)
592 return got_error_from_errno("getcwd");
594 got_path_strip_trailing_slashes(repo_path);
595 error = got_repo_open(&repo, repo_path);
599 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
600 error = got_error_from_errno("asprintf");
604 error = got_ref_open(&branch_ref, repo, refname, 0);
606 if (error->code != GOT_ERR_NOT_REF)
609 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
610 "import target branch already exists");
614 path_dir = realpath(argv[0], NULL);
615 if (path_dir == NULL) {
616 error = got_error_from_errno("realpath");
619 got_path_strip_trailing_slashes(path_dir);
622 * unveil(2) traverses exec(2); if an editor is used we have
623 * to apply unveil after the log message has been written.
625 if (logmsg == NULL || strlen(logmsg) == 0) {
626 error = get_editor(&editor);
629 error = collect_import_msg(&logmsg, editor, path_dir, refname);
634 if (unveil(path_dir, "r") != 0)
635 return got_error_from_errno2("unveil", path_dir);
637 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
641 error = got_repo_import(&new_commit_id, path_dir, logmsg,
642 author, &ignores, repo, import_progress, NULL);
646 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
650 error = got_ref_write(branch_ref, repo);
654 error = got_object_id_str(&id_str, new_commit_id);
658 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
660 if (error->code != GOT_ERR_NOT_REF)
663 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
668 error = got_ref_write(head_ref, repo);
673 printf("Created branch %s with commit %s\n",
674 got_ref_get_name(branch_ref), id_str);
682 got_ref_close(branch_ref);
684 got_ref_close(head_ref);
691 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
692 "[-p prefix] repository-path [worktree-path]\n", getprogname());
696 static const struct got_error *
697 checkout_progress(void *arg, unsigned char status, const char *path)
699 char *worktree_path = arg;
701 /* Base commit bump happens silently. */
702 if (status == GOT_STATUS_BUMP_BASE)
705 while (path[0] == '/')
708 printf("%c %s/%s\n", status, worktree_path, path);
712 static const struct got_error *
713 check_cancelled(void *arg)
715 if (sigint_received || sigpipe_received)
716 return got_error(GOT_ERR_CANCELLED);
720 static const struct got_error *
721 check_linear_ancestry(struct got_object_id *commit_id,
722 struct got_object_id *base_commit_id, struct got_repository *repo)
724 const struct got_error *err = NULL;
725 struct got_object_id *yca_id;
727 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
728 commit_id, base_commit_id, repo, check_cancelled, NULL);
733 return got_error(GOT_ERR_ANCESTRY);
736 * Require a straight line of history between the target commit
737 * and the work tree's base commit.
739 * Non-linear situations such as this require a rebase:
741 * (commit) D F (base_commit)
749 * 'got update' only handles linear cases:
750 * Update forwards in time: A (base/yca) - B - C - D (commit)
751 * Update backwards in time: D (base) - C - B - A (commit/yca)
753 if (got_object_id_cmp(commit_id, yca_id) != 0 &&
754 got_object_id_cmp(base_commit_id, yca_id) != 0)
755 return got_error(GOT_ERR_ANCESTRY);
761 static const struct got_error *
762 check_same_branch(struct got_object_id *commit_id,
763 struct got_reference *head_ref, struct got_object_id *yca_id,
764 struct got_repository *repo)
766 const struct got_error *err = NULL;
767 struct got_commit_graph *graph = NULL;
768 struct got_object_id *head_commit_id = NULL;
769 int is_same_branch = 0;
771 err = got_ref_resolve(&head_commit_id, repo, head_ref);
775 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
779 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
784 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
788 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
789 check_cancelled, NULL);
794 struct got_object_id *id;
795 err = got_commit_graph_iter_next(&id, graph);
797 if (err->code == GOT_ERR_ITER_COMPLETED) {
800 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
802 err = got_commit_graph_fetch_commits(graph, 1,
803 repo, check_cancelled, NULL);
809 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
811 if (got_object_id_cmp(id, commit_id) == 0) {
819 got_commit_graph_close(graph);
820 free(head_commit_id);
821 if (!err && !is_same_branch)
822 err = got_error(GOT_ERR_ANCESTRY);
826 static const struct got_error *
827 resolve_commit_arg(struct got_object_id **commit_id,
828 const char *commit_id_arg, struct got_repository *repo)
830 const struct got_error *err;
831 struct got_reference *ref;
832 struct got_tag_object *tag;
834 err = got_repo_object_match_tag(&tag, commit_id_arg,
835 GOT_OBJ_TYPE_COMMIT, repo);
837 *commit_id = got_object_id_dup(
838 got_object_tag_get_object_id(tag));
839 if (*commit_id == NULL)
840 err = got_error_from_errno("got_object_id_dup");
841 got_object_tag_close(tag);
843 } else if (err->code != GOT_ERR_NO_OBJ)
846 err = got_ref_open(&ref, repo, commit_id_arg, 0);
848 err = got_ref_resolve(commit_id, repo, ref);
851 if (err->code != GOT_ERR_NOT_REF)
853 err = got_repo_match_object_id_prefix(commit_id,
854 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
859 static const struct got_error *
860 cmd_checkout(int argc, char *argv[])
862 const struct got_error *error = NULL;
863 struct got_repository *repo = NULL;
864 struct got_reference *head_ref = NULL;
865 struct got_worktree *worktree = NULL;
866 char *repo_path = NULL;
867 char *worktree_path = NULL;
868 const char *path_prefix = "";
869 const char *branch_name = GOT_REF_HEAD;
870 char *commit_id_str = NULL;
871 int ch, same_path_prefix;
872 struct got_pathlist_head paths;
876 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
879 branch_name = optarg;
882 commit_id_str = strdup(optarg);
883 if (commit_id_str == NULL)
884 return got_error_from_errno("strdup");
887 path_prefix = optarg;
899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
900 "unveil", NULL) == -1)
904 char *cwd, *base, *dotgit;
905 repo_path = realpath(argv[0], NULL);
906 if (repo_path == NULL)
907 return got_error_from_errno2("realpath", argv[0]);
908 cwd = getcwd(NULL, 0);
910 error = got_error_from_errno("getcwd");
913 if (path_prefix[0]) {
914 base = basename(path_prefix);
916 error = got_error_from_errno2("basename",
921 base = basename(repo_path);
923 error = got_error_from_errno2("basename",
928 dotgit = strstr(base, ".git");
931 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
932 error = got_error_from_errno("asprintf");
937 } else if (argc == 2) {
938 repo_path = realpath(argv[0], NULL);
939 if (repo_path == NULL) {
940 error = got_error_from_errno2("realpath", argv[0]);
943 worktree_path = realpath(argv[1], NULL);
944 if (worktree_path == NULL) {
945 if (errno != ENOENT) {
946 error = got_error_from_errno2("realpath",
950 worktree_path = strdup(argv[1]);
951 if (worktree_path == NULL) {
952 error = got_error_from_errno("strdup");
959 got_path_strip_trailing_slashes(repo_path);
960 got_path_strip_trailing_slashes(worktree_path);
962 error = got_repo_open(&repo, repo_path);
966 /* Pre-create work tree path for unveil(2) */
967 error = got_path_mkdir(worktree_path);
969 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
970 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
972 if (!got_path_dir_is_empty(worktree_path)) {
973 error = got_error_path(worktree_path,
974 GOT_ERR_DIR_NOT_EMPTY);
979 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
983 error = got_ref_open(&head_ref, repo, branch_name, 0);
987 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
988 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
991 error = got_worktree_open(&worktree, worktree_path);
995 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
999 if (!same_path_prefix) {
1000 error = got_error(GOT_ERR_PATH_PREFIX);
1004 if (commit_id_str) {
1005 struct got_object_id *commit_id;
1006 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1009 error = check_linear_ancestry(commit_id,
1010 got_worktree_get_base_commit_id(worktree), repo);
1011 if (error != NULL) {
1015 error = check_same_branch(commit_id, head_ref, NULL, repo);
1018 error = got_worktree_set_base_commit_id(worktree, repo,
1025 error = got_pathlist_append(&paths, "", NULL);
1028 error = got_worktree_checkout_files(worktree, &paths, repo,
1029 checkout_progress, worktree_path, check_cancelled, NULL);
1033 printf("Now shut up and hack\n");
1036 got_pathlist_free(&paths);
1037 free(commit_id_str);
1039 free(worktree_path);
1046 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1051 static const struct got_error *
1052 update_progress(void *arg, unsigned char status, const char *path)
1054 int *did_something = arg;
1056 if (status == GOT_STATUS_EXISTS)
1061 /* Base commit bump happens silently. */
1062 if (status == GOT_STATUS_BUMP_BASE)
1065 while (path[0] == '/')
1067 printf("%c %s\n", status, path);
1071 static const struct got_error *
1072 switch_head_ref(struct got_reference *head_ref,
1073 struct got_object_id *commit_id, struct got_worktree *worktree,
1074 struct got_repository *repo)
1076 const struct got_error *err = NULL;
1078 int ref_has_moved = 0;
1080 /* Trivial case: switching between two different references. */
1081 if (strcmp(got_ref_get_name(head_ref),
1082 got_worktree_get_head_ref_name(worktree)) != 0) {
1083 printf("Switching work tree from %s to %s\n",
1084 got_worktree_get_head_ref_name(worktree),
1085 got_ref_get_name(head_ref));
1086 return got_worktree_set_head_ref(worktree, head_ref);
1089 err = check_linear_ancestry(commit_id,
1090 got_worktree_get_base_commit_id(worktree), repo);
1092 if (err->code != GOT_ERR_ANCESTRY)
1099 /* Switching to a rebased branch with the same reference name. */
1100 err = got_object_id_str(&base_id_str,
1101 got_worktree_get_base_commit_id(worktree));
1104 printf("Reference %s now points at a different branch\n",
1105 got_worktree_get_head_ref_name(worktree));
1106 printf("Switching work tree from %s to %s\n", base_id_str,
1107 got_worktree_get_head_ref_name(worktree));
1111 static const struct got_error *
1112 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1114 const struct got_error *err;
1117 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1121 return got_error(GOT_ERR_REBASING);
1123 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1127 return got_error(GOT_ERR_HISTEDIT_BUSY);
1132 static const struct got_error *
1133 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1134 char *argv[], struct got_worktree *worktree)
1136 const struct got_error *err = NULL;
1143 return got_error_from_errno("strdup");
1144 return got_pathlist_append(paths, path, NULL);
1147 for (i = 0; i < argc; i++) {
1148 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1151 err = got_pathlist_append(paths, path, NULL);
1161 static const struct got_error *
1162 cmd_update(int argc, char *argv[])
1164 const struct got_error *error = NULL;
1165 struct got_repository *repo = NULL;
1166 struct got_worktree *worktree = NULL;
1167 char *worktree_path = NULL;
1168 struct got_object_id *commit_id = NULL;
1169 char *commit_id_str = NULL;
1170 const char *branch_name = NULL;
1171 struct got_reference *head_ref = NULL;
1172 struct got_pathlist_head paths;
1173 struct got_pathlist_entry *pe;
1174 int ch, did_something = 0;
1178 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1181 branch_name = optarg;
1184 commit_id_str = strdup(optarg);
1185 if (commit_id_str == NULL)
1186 return got_error_from_errno("strdup");
1198 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1199 "unveil", NULL) == -1)
1202 worktree_path = getcwd(NULL, 0);
1203 if (worktree_path == NULL) {
1204 error = got_error_from_errno("getcwd");
1207 error = got_worktree_open(&worktree, worktree_path);
1211 error = check_rebase_or_histedit_in_progress(worktree);
1215 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1219 error = apply_unveil(got_repo_get_path(repo), 0,
1220 got_worktree_get_root_path(worktree));
1224 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1228 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1229 got_worktree_get_head_ref_name(worktree), 0);
1232 if (commit_id_str == NULL) {
1233 error = got_ref_resolve(&commit_id, repo, head_ref);
1236 error = got_object_id_str(&commit_id_str, commit_id);
1240 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1241 free(commit_id_str);
1242 commit_id_str = NULL;
1245 error = got_object_id_str(&commit_id_str, commit_id);
1251 struct got_object_id *head_commit_id;
1252 TAILQ_FOREACH(pe, &paths, entry) {
1253 if (pe->path_len == 0)
1255 error = got_error_msg(GOT_ERR_BAD_PATH,
1256 "switching between branches requires that "
1257 "the entire work tree gets updated");
1260 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1263 error = check_linear_ancestry(commit_id, head_commit_id, repo);
1264 free(head_commit_id);
1267 error = check_same_branch(commit_id, head_ref, NULL, repo);
1270 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1274 error = check_linear_ancestry(commit_id,
1275 got_worktree_get_base_commit_id(worktree), repo);
1276 if (error != NULL) {
1277 if (error->code == GOT_ERR_ANCESTRY)
1278 error = got_error(GOT_ERR_BRANCH_MOVED);
1281 error = check_same_branch(commit_id, head_ref, NULL, repo);
1286 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1288 error = got_worktree_set_base_commit_id(worktree, repo,
1294 error = got_worktree_checkout_files(worktree, &paths, repo,
1295 update_progress, &did_something, check_cancelled, NULL);
1300 printf("Updated to commit %s\n", commit_id_str);
1302 printf("Already up-to-date\n");
1304 free(worktree_path);
1305 TAILQ_FOREACH(pe, &paths, entry)
1306 free((char *)pe->path);
1307 got_pathlist_free(&paths);
1309 free(commit_id_str);
1313 static const struct got_error *
1314 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1315 int diff_context, struct got_repository *repo)
1317 const struct got_error *err = NULL;
1318 struct got_tree_object *tree1 = NULL, *tree2;
1319 struct got_object_qid *qid;
1320 char *id_str1 = NULL, *id_str2;
1321 struct got_diff_blob_output_unidiff_arg arg;
1323 err = got_object_open_as_tree(&tree2, repo,
1324 got_object_commit_get_tree_id(commit));
1328 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1330 struct got_commit_object *pcommit;
1332 err = got_object_open_as_commit(&pcommit, repo, qid->id);
1336 err = got_object_open_as_tree(&tree1, repo,
1337 got_object_commit_get_tree_id(pcommit));
1338 got_object_commit_close(pcommit);
1342 err = got_object_id_str(&id_str1, qid->id);
1347 err = got_object_id_str(&id_str2, id);
1351 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1352 arg.diff_context = diff_context;
1353 arg.outfile = stdout;
1354 err = got_diff_tree(tree1, tree2, "", "", repo,
1355 got_diff_blob_output_unidiff, &arg, 1);
1358 got_object_tree_close(tree1);
1359 got_object_tree_close(tree2);
1366 get_datestr(time_t *time, char *datebuf)
1368 struct tm mytm, *tm;
1371 tm = gmtime_r(time, &mytm);
1374 s = asctime_r(tm, datebuf);
1377 p = strchr(s, '\n');
1383 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1385 static const struct got_error *
1386 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1387 struct got_repository *repo, int show_patch, int diff_context,
1388 struct got_reflist_head *refs)
1390 const struct got_error *err = NULL;
1391 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1393 time_t committer_time;
1394 const char *author, *committer;
1395 char *refs_str = NULL;
1396 struct got_reflist_entry *re;
1398 SIMPLEQ_FOREACH(re, refs, entry) {
1401 struct got_tag_object *tag = NULL;
1404 name = got_ref_get_name(re->ref);
1405 if (strcmp(name, GOT_REF_HEAD) == 0)
1407 if (strncmp(name, "refs/", 5) == 0)
1409 if (strncmp(name, "got/", 4) == 0)
1411 if (strncmp(name, "heads/", 6) == 0)
1413 if (strncmp(name, "remotes/", 8) == 0)
1415 if (strncmp(name, "tags/", 5) == 0) {
1416 err = got_object_open_as_tag(&tag, repo, re->id);
1418 if (err->code != GOT_ERR_OBJ_TYPE)
1420 /* Ref points at something other than a tag. */
1425 cmp = got_object_id_cmp(tag ?
1426 got_object_tag_get_object_id(tag) : re->id, id);
1428 got_object_tag_close(tag);
1432 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1434 err = got_error_from_errno("asprintf");
1440 err = got_object_id_str(&id_str, id);
1444 printf(GOT_COMMIT_SEP_STR);
1445 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1446 refs_str ? refs_str : "", refs_str ? ")" : "");
1451 printf("from: %s\n", got_object_commit_get_author(commit));
1452 committer_time = got_object_commit_get_committer_time(commit);
1453 datestr = get_datestr(&committer_time, datebuf);
1455 printf("date: %s UTC\n", datestr);
1456 author = got_object_commit_get_author(commit);
1457 committer = got_object_commit_get_committer(commit);
1458 if (strcmp(author, committer) != 0)
1459 printf("via: %s\n", committer);
1460 if (got_object_commit_get_nparents(commit) > 1) {
1461 const struct got_object_id_queue *parent_ids;
1462 struct got_object_qid *qid;
1464 parent_ids = got_object_commit_get_parent_ids(commit);
1465 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1466 err = got_object_id_str(&id_str, qid->id);
1469 printf("parent %d: %s\n", n++, id_str);
1474 err = got_object_commit_get_logmsg(&logmsg0, commit);
1480 line = strsep(&logmsg, "\n");
1482 printf(" %s\n", line);
1487 err = print_patch(commit, id, diff_context, repo);
1492 if (fflush(stdout) != 0 && err == NULL)
1493 err = got_error_from_errno("fflush");
1497 static const struct got_error *
1498 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1499 char *path, int show_patch, int diff_context, int limit,
1500 int first_parent_traversal, struct got_reflist_head *refs)
1502 const struct got_error *err;
1503 struct got_commit_graph *graph;
1505 err = got_commit_graph_open(&graph, root_id, path,
1506 first_parent_traversal, repo);
1509 err = got_commit_graph_iter_start(graph, root_id, repo,
1510 check_cancelled, NULL);
1514 struct got_commit_object *commit;
1515 struct got_object_id *id;
1517 if (sigint_received || sigpipe_received)
1520 err = got_commit_graph_iter_next(&id, graph);
1522 if (err->code == GOT_ERR_ITER_COMPLETED) {
1526 if (err->code != GOT_ERR_ITER_NEED_MORE)
1528 err = got_commit_graph_fetch_commits(graph, 1, repo,
1529 check_cancelled, NULL);
1538 err = got_object_open_as_commit(&commit, repo, id);
1541 err = print_commit(commit, id, repo, show_patch, diff_context,
1543 got_object_commit_close(commit);
1544 if (err || (limit && --limit == 0))
1548 got_commit_graph_close(graph);
1555 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1556 "[-r repository-path] [path]\n", getprogname());
1561 get_default_log_limit(void)
1563 const char *got_default_log_limit;
1567 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1568 if (got_default_log_limit == NULL)
1570 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1576 static const struct got_error *
1577 cmd_log(int argc, char *argv[])
1579 const struct got_error *error;
1580 struct got_repository *repo = NULL;
1581 struct got_worktree *worktree = NULL;
1582 struct got_commit_object *commit = NULL;
1583 struct got_object_id *id = NULL;
1584 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1585 char *start_commit = NULL;
1586 int diff_context = 3, ch;
1587 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1589 struct got_reflist_head refs;
1591 SIMPLEQ_INIT(&refs);
1594 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1600 limit = get_default_log_limit();
1602 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1608 start_commit = optarg;
1611 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1614 err(1, "-C option %s", errstr);
1617 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1619 err(1, "-l option %s", errstr);
1622 first_parent_traversal = 1;
1625 repo_path = realpath(optarg, NULL);
1626 if (repo_path == NULL)
1627 err(1, "-r option");
1628 got_path_strip_trailing_slashes(repo_path);
1639 cwd = getcwd(NULL, 0);
1641 error = got_error_from_errno("getcwd");
1645 error = got_worktree_open(&worktree, cwd);
1646 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1653 error = got_error_from_errno("strdup");
1656 } else if (argc == 1) {
1658 error = got_worktree_resolve_path(&path, worktree,
1663 path = strdup(argv[0]);
1665 error = got_error_from_errno("strdup");
1672 if (repo_path == NULL) {
1673 repo_path = worktree ?
1674 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1676 if (repo_path == NULL) {
1677 error = got_error_from_errno("strdup");
1681 error = got_repo_open(&repo, repo_path);
1685 error = apply_unveil(got_repo_get_path(repo), 1,
1686 worktree ? got_worktree_get_root_path(worktree) : NULL);
1690 if (start_commit == NULL) {
1691 struct got_reference *head_ref;
1692 error = got_ref_open(&head_ref, repo,
1693 worktree ? got_worktree_get_head_ref_name(worktree)
1697 error = got_ref_resolve(&id, repo, head_ref);
1698 got_ref_close(head_ref);
1701 error = got_object_open_as_commit(&commit, repo, id);
1703 struct got_reference *ref;
1704 error = got_ref_open(&ref, repo, start_commit, 0);
1705 if (error == NULL) {
1707 error = got_ref_resolve(&id, repo, ref);
1711 error = got_object_get_type(&obj_type, repo, id);
1714 if (obj_type == GOT_OBJ_TYPE_TAG) {
1715 struct got_tag_object *tag;
1716 error = got_object_open_as_tag(&tag, repo, id);
1719 if (got_object_tag_get_object_type(tag) !=
1720 GOT_OBJ_TYPE_COMMIT) {
1721 got_object_tag_close(tag);
1722 error = got_error(GOT_ERR_OBJ_TYPE);
1726 id = got_object_id_dup(
1727 got_object_tag_get_object_id(tag));
1729 error = got_error_from_errno(
1730 "got_object_id_dup");
1731 got_object_tag_close(tag);
1734 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1735 error = got_error(GOT_ERR_OBJ_TYPE);
1738 error = got_object_open_as_commit(&commit, repo, id);
1742 if (commit == NULL) {
1743 error = got_repo_match_object_id_prefix(&id,
1744 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1752 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1757 path = in_repo_path;
1760 error = got_ref_list(&refs, repo);
1764 error = print_commits(id, repo, path, show_patch,
1765 diff_context, limit, first_parent_traversal, &refs);
1772 got_worktree_close(worktree);
1774 const struct got_error *repo_error;
1775 repo_error = got_repo_close(repo);
1779 got_ref_list_free(&refs);
1786 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1787 "[object1 object2 | path]\n", getprogname());
1791 struct print_diff_arg {
1792 struct got_repository *repo;
1793 struct got_worktree *worktree;
1800 static const struct got_error *
1801 print_diff(void *arg, unsigned char status, unsigned char staged_status,
1802 const char *path, struct got_object_id *blob_id,
1803 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1805 struct print_diff_arg *a = arg;
1806 const struct got_error *err = NULL;
1807 struct got_blob_object *blob1 = NULL;
1809 char *abspath = NULL, *label1 = NULL;
1812 if (a->diff_staged) {
1813 if (staged_status != GOT_STATUS_MODIFY &&
1814 staged_status != GOT_STATUS_ADD &&
1815 staged_status != GOT_STATUS_DELETE)
1818 if (staged_status == GOT_STATUS_DELETE)
1820 if (status != GOT_STATUS_MODIFY &&
1821 status != GOT_STATUS_ADD &&
1822 status != GOT_STATUS_DELETE &&
1823 status != GOT_STATUS_CONFLICT)
1827 if (!a->header_shown) {
1828 printf("diff %s %s%s\n", a->id_str,
1829 got_worktree_get_root_path(a->worktree),
1830 a->diff_staged ? " (staged changes)" : "");
1831 a->header_shown = 1;
1834 if (a->diff_staged) {
1835 const char *label1 = NULL, *label2 = NULL;
1836 switch (staged_status) {
1837 case GOT_STATUS_MODIFY:
1841 case GOT_STATUS_ADD:
1844 case GOT_STATUS_DELETE:
1848 return got_error(GOT_ERR_FILE_STATUS);
1850 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
1851 label1, label2, a->diff_context, a->repo, stdout);
1854 if (staged_status == GOT_STATUS_ADD ||
1855 staged_status == GOT_STATUS_MODIFY) {
1857 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
1861 err = got_object_id_str(&id_str, staged_blob_id);
1864 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
1865 err = got_error_from_errno("asprintf");
1870 } else if (status != GOT_STATUS_ADD) {
1871 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1876 if (status != GOT_STATUS_DELETE) {
1877 if (asprintf(&abspath, "%s/%s",
1878 got_worktree_get_root_path(a->worktree), path) == -1) {
1879 err = got_error_from_errno("asprintf");
1883 f2 = fopen(abspath, "r");
1885 err = got_error_from_errno2("fopen", abspath);
1888 if (lstat(abspath, &sb) == -1) {
1889 err = got_error_from_errno2("lstat", abspath);
1895 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
1896 a->diff_context, stdout);
1899 got_object_blob_close(blob1);
1900 if (f2 && fclose(f2) != 0 && err == NULL)
1901 err = got_error_from_errno("fclose");
1906 static const struct got_error *
1907 match_object_id(struct got_object_id **id, char **label,
1908 const char *id_str, int obj_type, struct got_repository *repo)
1910 const struct got_error *err;
1911 struct got_tag_object *tag;
1912 struct got_reference *ref = NULL;
1917 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY, repo);
1919 *id = got_object_id_dup(got_object_tag_get_object_id(tag));
1921 err = got_error_from_errno("got_object_id_dup");
1922 if (asprintf(label, "refs/tags/%s",
1923 got_object_tag_get_name(tag)) == -1)
1924 err = got_error_from_errno("asprintf");
1925 got_object_tag_close(tag);
1927 } else if (err->code != GOT_ERR_NO_OBJ)
1930 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
1932 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
1934 err = got_ref_open(&ref, repo, id_str, 0);
1937 *label = strdup(got_ref_get_name(ref));
1938 if (*label == NULL) {
1939 err = got_error_from_errno("strdup");
1942 err = got_ref_resolve(id, repo, ref);
1944 err = got_object_id_str(label, *id);
1945 if (*label == NULL) {
1946 err = got_error_from_errno("strdup");
1957 static const struct got_error *
1958 cmd_diff(int argc, char *argv[])
1960 const struct got_error *error;
1961 struct got_repository *repo = NULL;
1962 struct got_worktree *worktree = NULL;
1963 char *cwd = NULL, *repo_path = NULL;
1964 struct got_object_id *id1 = NULL, *id2 = NULL;
1965 const char *id_str1 = NULL, *id_str2 = NULL;
1966 char *label1 = NULL, *label2 = NULL;
1968 int diff_context = 3, diff_staged = 0, ch;
1973 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1978 while ((ch = getopt(argc, argv, "C:r:s")) != -1) {
1981 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1983 err(1, "-C option %s", errstr);
1986 repo_path = realpath(optarg, NULL);
1987 if (repo_path == NULL)
1988 err(1, "-r option");
1989 got_path_strip_trailing_slashes(repo_path);
2003 cwd = getcwd(NULL, 0);
2005 error = got_error_from_errno("getcwd");
2008 error = got_worktree_open(&worktree, cwd);
2009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2012 if (worktree == NULL) {
2013 error = got_error(GOT_ERR_NOT_WORKTREE);
2018 "-r option can't be used when diffing a work tree");
2019 repo_path = strdup(got_worktree_get_repo_path(worktree));
2020 if (repo_path == NULL) {
2021 error = got_error_from_errno("strdup");
2025 error = got_worktree_resolve_path(&path, worktree,
2032 error = got_error_from_errno("strdup");
2036 } else if (argc == 2) {
2038 errx(1, "-s option can't be used when diffing "
2039 "objects in repository");
2042 if (worktree && repo_path == NULL) {
2044 strdup(got_worktree_get_repo_path(worktree));
2045 if (repo_path == NULL) {
2046 error = got_error_from_errno("strdup");
2053 if (repo_path == NULL) {
2054 repo_path = getcwd(NULL, 0);
2055 if (repo_path == NULL)
2056 return got_error_from_errno("getcwd");
2059 error = got_repo_open(&repo, repo_path);
2064 error = apply_unveil(got_repo_get_path(repo), 1,
2065 worktree ? got_worktree_get_root_path(worktree) : NULL);
2070 struct print_diff_arg arg;
2071 struct got_pathlist_head paths;
2076 error = got_object_id_str(&id_str,
2077 got_worktree_get_base_commit_id(worktree));
2081 arg.worktree = worktree;
2082 arg.diff_context = diff_context;
2083 arg.id_str = id_str;
2084 arg.header_shown = 0;
2085 arg.diff_staged = diff_staged;
2087 error = got_pathlist_append(&paths, path, NULL);
2091 error = got_worktree_status(worktree, &paths, repo, print_diff,
2092 &arg, check_cancelled, NULL);
2094 got_pathlist_free(&paths);
2098 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, repo);
2102 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, repo);
2106 error = got_object_get_type(&type1, repo, id1);
2110 error = got_object_get_type(&type2, repo, id2);
2114 if (type1 != type2) {
2115 error = got_error(GOT_ERR_OBJ_TYPE);
2120 case GOT_OBJ_TYPE_BLOB:
2121 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2122 diff_context, repo, stdout);
2124 case GOT_OBJ_TYPE_TREE:
2125 error = got_diff_objects_as_trees(id1, id2, "", "",
2126 diff_context, repo, stdout);
2128 case GOT_OBJ_TYPE_COMMIT:
2129 printf("diff %s %s\n", label1, label2);
2130 error = got_diff_objects_as_commits(id1, id2, diff_context,
2134 error = got_error(GOT_ERR_OBJ_TYPE);
2144 got_worktree_close(worktree);
2146 const struct got_error *repo_error;
2147 repo_error = got_repo_close(repo);
2158 "usage: %s blame [-c commit] [-r repository-path] path\n",
2167 char datebuf[9]; /* YY-MM-DD + NUL */
2170 struct blame_cb_args {
2171 struct blame_line *lines;
2175 off_t *line_offsets;
2177 struct got_repository *repo;
2180 static const struct got_error *
2181 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2183 const struct got_error *err = NULL;
2184 struct blame_cb_args *a = arg;
2185 struct blame_line *bline;
2187 size_t linesize = 0;
2188 struct got_commit_object *commit = NULL;
2191 time_t committer_time;
2193 if (nlines != a->nlines ||
2194 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2195 return got_error(GOT_ERR_RANGE);
2197 if (sigint_received)
2198 return got_error(GOT_ERR_ITER_COMPLETED);
2201 return NULL; /* no change in this commit */
2203 /* Annotate this line. */
2204 bline = &a->lines[lineno - 1];
2205 if (bline->annotated)
2207 err = got_object_id_str(&bline->id_str, id);
2211 err = got_object_open_as_commit(&commit, a->repo, id);
2215 bline->committer = strdup(got_object_commit_get_committer(commit));
2216 if (bline->committer == NULL) {
2217 err = got_error_from_errno("strdup");
2221 committer_time = got_object_commit_get_committer_time(commit);
2222 if (localtime_r(&committer_time, &tm) == NULL)
2223 return got_error_from_errno("localtime_r");
2224 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2225 &tm) >= sizeof(bline->datebuf)) {
2226 err = got_error(GOT_ERR_NO_SPACE);
2229 bline->annotated = 1;
2231 /* Print lines annotated so far. */
2232 bline = &a->lines[a->lineno_cur - 1];
2233 if (!bline->annotated)
2236 offset = a->line_offsets[a->lineno_cur - 1];
2237 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2238 err = got_error_from_errno("fseeko");
2242 while (bline->annotated) {
2243 char *smallerthan, *at, *nl, *committer;
2246 if (getline(&line, &linesize, a->f) == (ssize_t)-1) {
2248 err = got_error_from_errno("getline");
2252 committer = bline->committer;
2253 smallerthan = strchr(committer, '<');
2254 if (smallerthan && smallerthan[1] != '\0')
2255 committer = smallerthan + 1;
2256 at = strchr(committer, '@');
2259 len = strlen(committer);
2261 committer[8] = '\0';
2263 nl = strchr(line, '\n');
2266 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2267 bline->id_str, bline->datebuf, committer, line);
2270 bline = &a->lines[a->lineno_cur - 1];
2274 got_object_commit_close(commit);
2279 static const struct got_error *
2280 cmd_blame(int argc, char *argv[])
2282 const struct got_error *error;
2283 struct got_repository *repo = NULL;
2284 struct got_worktree *worktree = NULL;
2285 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2286 struct got_object_id *obj_id = NULL;
2287 struct got_object_id *commit_id = NULL;
2288 struct got_blob_object *blob = NULL;
2289 char *commit_id_str = NULL;
2290 struct blame_cb_args bca;
2291 int ch, obj_type, i;
2294 memset(&bca, 0, sizeof(bca));
2297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2302 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2305 commit_id_str = optarg;
2308 repo_path = realpath(optarg, NULL);
2309 if (repo_path == NULL)
2310 err(1, "-r option");
2311 got_path_strip_trailing_slashes(repo_path);
2327 cwd = getcwd(NULL, 0);
2329 error = got_error_from_errno("getcwd");
2332 if (repo_path == NULL) {
2333 error = got_worktree_open(&worktree, cwd);
2334 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2340 strdup(got_worktree_get_repo_path(worktree));
2341 if (repo_path == NULL)
2342 error = got_error_from_errno("strdup");
2346 repo_path = strdup(cwd);
2347 if (repo_path == NULL) {
2348 error = got_error_from_errno("strdup");
2354 error = got_repo_open(&repo, repo_path);
2358 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2363 const char *prefix = got_worktree_get_path_prefix(worktree);
2364 char *p, *worktree_subdir = cwd +
2365 strlen(got_worktree_get_root_path(worktree));
2366 if (asprintf(&p, "%s%s%s%s%s",
2367 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2368 worktree_subdir, worktree_subdir[0] ? "/" : "",
2370 error = got_error_from_errno("asprintf");
2373 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2376 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2381 if (commit_id_str == NULL) {
2382 struct got_reference *head_ref;
2383 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2386 error = got_ref_resolve(&commit_id, repo, head_ref);
2387 got_ref_close(head_ref);
2391 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2396 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2399 if (obj_id == NULL) {
2400 error = got_error(GOT_ERR_NO_OBJ);
2404 error = got_object_get_type(&obj_type, repo, obj_id);
2408 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2409 error = got_error(GOT_ERR_OBJ_TYPE);
2413 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2416 bca.f = got_opentemp();
2417 if (bca.f == NULL) {
2418 error = got_error_from_errno("got_opentemp");
2421 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2422 &bca.line_offsets, bca.f, blob);
2423 if (error || bca.nlines == 0)
2426 /* Don't include \n at EOF in the blame line count. */
2427 if (bca.line_offsets[bca.nlines - 1] == filesize)
2430 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2431 if (bca.lines == NULL) {
2432 error = got_error_from_errno("calloc");
2436 bca.nlines_prec = 0;
2444 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2445 check_cancelled, NULL);
2455 got_object_blob_close(blob);
2457 got_worktree_close(worktree);
2459 const struct got_error *repo_error;
2460 repo_error = got_repo_close(repo);
2464 for (i = 0; i < bca.nlines; i++) {
2465 struct blame_line *bline = &bca.lines[i];
2466 free(bline->id_str);
2467 free(bline->committer);
2470 free(bca.line_offsets);
2471 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2472 error = got_error_from_errno("fclose");
2480 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2486 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2487 const char *root_path)
2489 int is_root_path = (strcmp(path, root_path) == 0);
2490 const char *modestr = "";
2492 path += strlen(root_path);
2493 while (path[0] == '/')
2496 if (S_ISLNK(te->mode))
2498 else if (S_ISDIR(te->mode))
2500 else if (te->mode & S_IXUSR)
2503 printf("%s%s%s%s%s\n", id ? id : "", path,
2504 is_root_path ? "" : "/", te->name, modestr);
2507 static const struct got_error *
2508 print_tree(const char *path, struct got_object_id *commit_id,
2509 int show_ids, int recurse, const char *root_path,
2510 struct got_repository *repo)
2512 const struct got_error *err = NULL;
2513 struct got_object_id *tree_id = NULL;
2514 struct got_tree_object *tree = NULL;
2515 const struct got_tree_entries *entries;
2516 struct got_tree_entry *te;
2518 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2522 err = got_object_open_as_tree(&tree, repo, tree_id);
2525 entries = got_object_tree_get_entries(tree);
2526 te = SIMPLEQ_FIRST(&entries->head);
2530 if (sigint_received || sigpipe_received)
2535 err = got_object_id_str(&id_str, te->id);
2538 if (asprintf(&id, "%s ", id_str) == -1) {
2539 err = got_error_from_errno("asprintf");
2545 print_entry(te, id, path, root_path);
2548 if (recurse && S_ISDIR(te->mode)) {
2550 if (asprintf(&child_path, "%s%s%s", path,
2551 path[0] == '/' && path[1] == '\0' ? "" : "/",
2553 err = got_error_from_errno("asprintf");
2556 err = print_tree(child_path, commit_id, show_ids, 1,
2563 te = SIMPLEQ_NEXT(te, entry);
2567 got_object_tree_close(tree);
2572 static const struct got_error *
2573 cmd_tree(int argc, char *argv[])
2575 const struct got_error *error;
2576 struct got_repository *repo = NULL;
2577 struct got_worktree *worktree = NULL;
2579 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2580 struct got_object_id *commit_id = NULL;
2581 char *commit_id_str = NULL;
2582 int show_ids = 0, recurse = 0;
2586 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2591 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2594 commit_id_str = optarg;
2597 repo_path = realpath(optarg, NULL);
2598 if (repo_path == NULL)
2599 err(1, "-r option");
2600 got_path_strip_trailing_slashes(repo_path);
2624 cwd = getcwd(NULL, 0);
2626 error = got_error_from_errno("getcwd");
2629 if (repo_path == NULL) {
2630 error = got_worktree_open(&worktree, cwd);
2631 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2637 strdup(got_worktree_get_repo_path(worktree));
2638 if (repo_path == NULL)
2639 error = got_error_from_errno("strdup");
2643 repo_path = strdup(cwd);
2644 if (repo_path == NULL) {
2645 error = got_error_from_errno("strdup");
2651 error = got_repo_open(&repo, repo_path);
2655 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2661 char *p, *worktree_subdir = cwd +
2662 strlen(got_worktree_get_root_path(worktree));
2663 if (asprintf(&p, "%s/%s",
2664 got_worktree_get_path_prefix(worktree),
2665 worktree_subdir) == -1) {
2666 error = got_error_from_errno("asprintf");
2669 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2676 if (in_repo_path == NULL) {
2677 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2682 if (commit_id_str == NULL) {
2683 struct got_reference *head_ref;
2684 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2687 error = got_ref_resolve(&commit_id, repo, head_ref);
2688 got_ref_close(head_ref);
2692 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2697 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2698 in_repo_path, repo);
2705 got_worktree_close(worktree);
2707 const struct got_error *repo_error;
2708 repo_error = got_repo_close(repo);
2718 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2722 static const struct got_error *
2723 print_status(void *arg, unsigned char status, unsigned char staged_status,
2724 const char *path, struct got_object_id *blob_id,
2725 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2727 if (status == staged_status && (status == GOT_STATUS_DELETE))
2728 status = GOT_STATUS_NO_CHANGE;
2729 printf("%c%c %s\n", status, staged_status, path);
2733 static const struct got_error *
2734 cmd_status(int argc, char *argv[])
2736 const struct got_error *error = NULL;
2737 struct got_repository *repo = NULL;
2738 struct got_worktree *worktree = NULL;
2740 struct got_pathlist_head paths;
2741 struct got_pathlist_entry *pe;
2746 while ((ch = getopt(argc, argv, "")) != -1) {
2758 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2762 cwd = getcwd(NULL, 0);
2764 error = got_error_from_errno("getcwd");
2768 error = got_worktree_open(&worktree, cwd);
2772 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2776 error = apply_unveil(got_repo_get_path(repo), 1,
2777 got_worktree_get_root_path(worktree));
2781 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2785 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2786 check_cancelled, NULL);
2788 TAILQ_FOREACH(pe, &paths, entry)
2789 free((char *)pe->path);
2790 got_pathlist_free(&paths);
2799 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2804 static const struct got_error *
2805 list_refs(struct got_repository *repo)
2807 static const struct got_error *err = NULL;
2808 struct got_reflist_head refs;
2809 struct got_reflist_entry *re;
2811 SIMPLEQ_INIT(&refs);
2812 err = got_ref_list(&refs, repo);
2816 SIMPLEQ_FOREACH(re, &refs, entry) {
2818 refstr = got_ref_to_str(re->ref);
2820 return got_error_from_errno("got_ref_to_str");
2821 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2825 got_ref_list_free(&refs);
2829 static const struct got_error *
2830 delete_ref(struct got_repository *repo, const char *refname)
2832 const struct got_error *err = NULL;
2833 struct got_reference *ref;
2835 err = got_ref_open(&ref, repo, refname, 0);
2839 err = got_ref_delete(ref, repo);
2844 static const struct got_error *
2845 add_ref(struct got_repository *repo, const char *refname, const char *target)
2847 const struct got_error *err = NULL;
2848 struct got_object_id *id;
2849 struct got_reference *ref = NULL;
2852 * Don't let the user create a reference named '-'.
2853 * While technically a valid reference name, this case is usually
2854 * an unintended typo.
2856 if (refname[0] == '-' && refname[1] == '\0')
2857 return got_error(GOT_ERR_BAD_REF_NAME);
2859 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2862 struct got_reference *target_ref;
2864 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2866 err = got_ref_open(&target_ref, repo, target, 0);
2869 err = got_ref_resolve(&id, repo, target_ref);
2870 got_ref_close(target_ref);
2875 err = got_ref_alloc(&ref, refname, id);
2879 err = got_ref_write(ref, repo);
2887 static const struct got_error *
2888 add_symref(struct got_repository *repo, const char *refname, const char *target)
2890 const struct got_error *err = NULL;
2891 struct got_reference *ref = NULL;
2892 struct got_reference *target_ref = NULL;
2895 * Don't let the user create a reference named '-'.
2896 * While technically a valid reference name, this case is usually
2897 * an unintended typo.
2899 if (refname[0] == '-' && refname[1] == '\0')
2900 return got_error(GOT_ERR_BAD_REF_NAME);
2902 err = got_ref_open(&target_ref, repo, target, 0);
2906 err = got_ref_alloc_symref(&ref, refname, target_ref);
2910 err = got_ref_write(ref, repo);
2913 got_ref_close(target_ref);
2919 static const struct got_error *
2920 cmd_ref(int argc, char *argv[])
2922 const struct got_error *error = NULL;
2923 struct got_repository *repo = NULL;
2924 struct got_worktree *worktree = NULL;
2925 char *cwd = NULL, *repo_path = NULL;
2926 int ch, do_list = 0, create_symref = 0;
2927 const char *delref = NULL;
2929 /* TODO: Add -s option for adding symbolic references. */
2930 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
2936 repo_path = realpath(optarg, NULL);
2937 if (repo_path == NULL)
2938 err(1, "-r option");
2939 got_path_strip_trailing_slashes(repo_path);
2953 if (do_list && delref)
2954 errx(1, "-l and -d options are mutually exclusive\n");
2959 if (do_list || delref) {
2961 errx(1, "-s option cannot be used together with the "
2962 "-l or -d options");
2965 } else if (argc != 2)
2970 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2974 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2975 "sendfd unveil", NULL) == -1)
2979 cwd = getcwd(NULL, 0);
2981 error = got_error_from_errno("getcwd");
2985 if (repo_path == NULL) {
2986 error = got_worktree_open(&worktree, cwd);
2987 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2993 strdup(got_worktree_get_repo_path(worktree));
2994 if (repo_path == NULL)
2995 error = got_error_from_errno("strdup");
2999 repo_path = strdup(cwd);
3000 if (repo_path == NULL) {
3001 error = got_error_from_errno("strdup");
3007 error = got_repo_open(&repo, repo_path);
3011 error = apply_unveil(got_repo_get_path(repo), do_list,
3012 worktree ? got_worktree_get_root_path(worktree) : NULL);
3017 error = list_refs(repo);
3019 error = delete_ref(repo, delref);
3020 else if (create_symref)
3021 error = add_symref(repo, argv[0], argv[1]);
3023 error = add_ref(repo, argv[0], argv[1]);
3026 got_repo_close(repo);
3028 got_worktree_close(worktree);
3038 "usage: %s branch [-r repository] -l | -d name | "
3039 "name [base-branch]\n", getprogname());
3043 static const struct got_error *
3044 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3046 static const struct got_error *err = NULL;
3047 struct got_reflist_head refs;
3048 struct got_reflist_entry *re;
3050 SIMPLEQ_INIT(&refs);
3052 err = got_ref_list(&refs, repo);
3056 SIMPLEQ_FOREACH(re, &refs, entry) {
3057 const char *refname, *marker = " ";
3059 refname = got_ref_get_name(re->ref);
3060 if (strncmp(refname, "refs/heads/", 11) != 0)
3062 if (worktree && strcmp(refname,
3063 got_worktree_get_head_ref_name(worktree)) == 0) {
3064 struct got_object_id *id = NULL;
3065 err = got_ref_resolve(&id, repo, re->ref);
3068 if (got_object_id_cmp(id,
3069 got_worktree_get_base_commit_id(worktree)) == 0)
3076 refstr = got_ref_to_str(re->ref);
3078 return got_error_from_errno("got_ref_to_str");
3079 printf("%s%s: %s\n", marker, refname, refstr);
3083 got_ref_list_free(&refs);
3087 static const struct got_error *
3088 delete_branch(struct got_repository *repo, const char *branch_name)
3090 const struct got_error *err = NULL;
3091 struct got_reference *ref;
3094 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3095 return got_error_from_errno("asprintf");
3097 err = got_ref_open(&ref, repo, refname, 0);
3101 err = got_ref_delete(ref, repo);
3108 static const struct got_error *
3109 add_branch(struct got_repository *repo, const char *branch_name,
3110 const char *base_branch)
3112 const struct got_error *err = NULL;
3113 struct got_object_id *id = NULL;
3114 struct got_reference *ref = NULL;
3115 char *base_refname = NULL, *refname = NULL;
3116 struct got_reference *base_ref;
3119 * Don't let the user create a branch named '-'.
3120 * While technically a valid reference name, this case is usually
3121 * an unintended typo.
3123 if (branch_name[0] == '-' && branch_name[1] == '\0')
3124 return got_error(GOT_ERR_BAD_REF_NAME);
3126 if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
3127 base_refname = strdup(GOT_REF_HEAD);
3128 if (base_refname == NULL)
3129 return got_error_from_errno("strdup");
3130 } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
3131 return got_error_from_errno("asprintf");
3133 err = got_ref_open(&base_ref, repo, base_refname, 0);
3136 err = got_ref_resolve(&id, repo, base_ref);
3137 got_ref_close(base_ref);
3141 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3142 err = got_error_from_errno("asprintf");
3146 err = got_ref_open(&ref, repo, refname, 0);
3148 err = got_error(GOT_ERR_BRANCH_EXISTS);
3150 } else if (err->code != GOT_ERR_NOT_REF)
3153 err = got_ref_alloc(&ref, refname, id);
3157 err = got_ref_write(ref, repo);
3167 static const struct got_error *
3168 cmd_branch(int argc, char *argv[])
3170 const struct got_error *error = NULL;
3171 struct got_repository *repo = NULL;
3172 struct got_worktree *worktree = NULL;
3173 char *cwd = NULL, *repo_path = NULL;
3174 int ch, do_list = 0;
3175 const char *delref = NULL;
3177 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 err(1, "-r option");
3186 got_path_strip_trailing_slashes(repo_path);
3197 if (do_list && delref)
3198 errx(1, "-l and -d options are mutually exclusive\n");
3203 if (do_list || delref) {
3206 } else if (argc < 1 || argc > 2)
3211 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3215 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3216 "sendfd unveil", NULL) == -1)
3220 cwd = getcwd(NULL, 0);
3222 error = got_error_from_errno("getcwd");
3226 if (repo_path == NULL) {
3227 error = got_worktree_open(&worktree, cwd);
3228 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3234 strdup(got_worktree_get_repo_path(worktree));
3235 if (repo_path == NULL)
3236 error = got_error_from_errno("strdup");
3240 repo_path = strdup(cwd);
3241 if (repo_path == NULL) {
3242 error = got_error_from_errno("strdup");
3248 error = got_repo_open(&repo, repo_path);
3252 error = apply_unveil(got_repo_get_path(repo), do_list,
3253 worktree ? got_worktree_get_root_path(worktree) : NULL);
3258 error = list_branches(repo, worktree);
3260 error = delete_branch(repo, delref);
3262 const char *base_branch;
3264 base_branch = worktree ?
3265 got_worktree_get_head_ref_name(worktree) :
3267 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3270 base_branch = argv[1];
3271 error = add_branch(repo, argv[0], base_branch);
3275 got_repo_close(repo);
3277 got_worktree_close(worktree);
3286 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
3290 static const struct got_error *
3291 cmd_add(int argc, char *argv[])
3293 const struct got_error *error = NULL;
3294 struct got_repository *repo = NULL;
3295 struct got_worktree *worktree = NULL;
3297 struct got_pathlist_head paths;
3298 struct got_pathlist_entry *pe;
3303 while ((ch = getopt(argc, argv, "")) != -1) {
3315 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3322 cwd = getcwd(NULL, 0);
3324 error = got_error_from_errno("getcwd");
3328 error = got_worktree_open(&worktree, cwd);
3332 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3336 error = apply_unveil(got_repo_get_path(repo), 1,
3337 got_worktree_get_root_path(worktree));
3341 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3345 error = got_worktree_schedule_add(worktree, &paths, print_status,
3349 got_repo_close(repo);
3351 got_worktree_close(worktree);
3352 TAILQ_FOREACH(pe, &paths, entry)
3353 free((char *)pe->path);
3354 got_pathlist_free(&paths);
3362 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
3366 static const struct got_error *
3367 cmd_remove(int argc, char *argv[])
3369 const struct got_error *error = NULL;
3370 struct got_worktree *worktree = NULL;
3371 struct got_repository *repo = NULL;
3373 struct got_pathlist_head paths;
3374 struct got_pathlist_entry *pe;
3375 int ch, delete_local_mods = 0;
3379 while ((ch = getopt(argc, argv, "f")) != -1) {
3382 delete_local_mods = 1;
3394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3401 cwd = getcwd(NULL, 0);
3403 error = got_error_from_errno("getcwd");
3406 error = got_worktree_open(&worktree, cwd);
3410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3414 error = apply_unveil(got_repo_get_path(repo), 1,
3415 got_worktree_get_root_path(worktree));
3419 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3423 error = got_worktree_schedule_delete(worktree, &paths,
3424 delete_local_mods, print_status, NULL, repo);
3429 got_repo_close(repo);
3431 got_worktree_close(worktree);
3432 TAILQ_FOREACH(pe, &paths, entry)
3433 free((char *)pe->path);
3434 got_pathlist_free(&paths);
3442 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
3443 "path ...\n", getprogname());
3447 static const struct got_error *
3448 revert_progress(void *arg, unsigned char status, const char *path)
3450 while (path[0] == '/')
3452 printf("%c %s\n", status, path);
3456 struct choose_patch_arg {
3457 FILE *patch_script_file;
3461 static const struct got_error *
3462 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
3463 int nchanges, const char *action)
3466 size_t linesize = 0;
3470 case GOT_STATUS_ADD:
3471 printf("A %s\n%s this addition? [y/n] ", path, action);
3473 case GOT_STATUS_DELETE:
3474 printf("D %s\n%s this deletion? [y/n] ", path, action);
3476 case GOT_STATUS_MODIFY:
3477 if (fseek(patch_file, 0L, SEEK_SET) == -1)
3478 return got_error_from_errno("fseek");
3479 printf(GOT_COMMIT_SEP_STR);
3480 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
3482 if (ferror(patch_file))
3483 return got_error_from_errno("getline");
3484 printf(GOT_COMMIT_SEP_STR);
3485 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
3486 path, n, nchanges, action);
3489 return got_error_path(path, GOT_ERR_FILE_STATUS);
3495 static const struct got_error *
3496 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
3497 FILE *patch_file, int n, int nchanges)
3499 const struct got_error *err = NULL;
3501 size_t linesize = 0;
3504 struct choose_patch_arg *a = arg;
3506 *choice = GOT_PATCH_CHOICE_NONE;
3508 if (a->patch_script_file) {
3510 err = show_change(status, path, patch_file, n, nchanges,
3514 linelen = getline(&line, &linesize, a->patch_script_file);
3515 if (linelen == -1) {
3516 if (ferror(a->patch_script_file))
3517 return got_error_from_errno("getline");
3520 nl = strchr(line, '\n');
3523 if (strcmp(line, "y") == 0) {
3524 *choice = GOT_PATCH_CHOICE_YES;
3526 } else if (strcmp(line, "n") == 0) {
3527 *choice = GOT_PATCH_CHOICE_NO;
3529 } else if (strcmp(line, "q") == 0 &&
3530 status == GOT_STATUS_MODIFY) {
3531 *choice = GOT_PATCH_CHOICE_QUIT;
3534 printf("invalid response '%s'\n", line);
3539 while (resp != 'y' && resp != 'n' && resp != 'q') {
3540 err = show_change(status, path, patch_file, n, nchanges,
3547 if (status == GOT_STATUS_MODIFY) {
3548 if (resp != 'y' && resp != 'n' && resp != 'q') {
3549 printf("invalid response '%c'\n", resp);
3552 } else if (resp != 'y' && resp != 'n') {
3553 printf("invalid response '%c'\n", resp);
3559 *choice = GOT_PATCH_CHOICE_YES;
3560 else if (resp == 'n')
3561 *choice = GOT_PATCH_CHOICE_NO;
3562 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
3563 *choice = GOT_PATCH_CHOICE_QUIT;
3569 static const struct got_error *
3570 cmd_revert(int argc, char *argv[])
3572 const struct got_error *error = NULL;
3573 struct got_worktree *worktree = NULL;
3574 struct got_repository *repo = NULL;
3575 char *cwd = NULL, *path = NULL;
3576 struct got_pathlist_head paths;
3577 struct got_pathlist_entry *pe;
3578 int ch, can_recurse = 0, pflag = 0;
3579 FILE *patch_script_file = NULL;
3580 const char *patch_script_path = NULL;
3581 struct choose_patch_arg cpa;
3585 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
3591 patch_script_path = optarg;
3606 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3607 "unveil", NULL) == -1)
3612 if (patch_script_path && !pflag)
3613 errx(1, "-F option can only be used together with -p option");
3615 cwd = getcwd(NULL, 0);
3617 error = got_error_from_errno("getcwd");
3620 error = got_worktree_open(&worktree, cwd);
3624 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3628 if (patch_script_path) {
3629 patch_script_file = fopen(patch_script_path, "r");
3630 if (patch_script_file == NULL) {
3631 error = got_error_from_errno2("fopen",
3636 error = apply_unveil(got_repo_get_path(repo), 1,
3637 got_worktree_get_root_path(worktree));
3641 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3648 TAILQ_FOREACH(pe, &paths, entry) {
3649 if (asprintf(&ondisk_path, "%s/%s",
3650 got_worktree_get_root_path(worktree),
3652 error = got_error_from_errno("asprintf");
3655 if (lstat(ondisk_path, &sb) == -1) {
3656 if (errno == ENOENT) {
3660 error = got_error_from_errno2("lstat",
3666 if (S_ISDIR(sb.st_mode)) {
3667 error = got_error_msg(GOT_ERR_BAD_PATH,
3668 "reverting directories requires -R option");
3674 cpa.patch_script_file = patch_script_file;
3675 cpa.action = "revert";
3676 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
3677 pflag ? choose_patch : NULL, &cpa, repo);
3681 if (patch_script_file && fclose(patch_script_file) == EOF &&
3683 error = got_error_from_errno2("fclose", patch_script_path);
3685 got_repo_close(repo);
3687 got_worktree_close(worktree);
3696 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3701 struct collect_commit_logmsg_arg {
3702 const char *cmdline_log;
3704 const char *worktree_path;
3705 const char *branch_name;
3706 const char *repo_path;
3711 static const struct got_error *
3712 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3715 char *initial_content = NULL;
3716 struct got_pathlist_entry *pe;
3717 const struct got_error *err = NULL;
3718 char *template = NULL;
3719 struct collect_commit_logmsg_arg *a = arg;
3723 /* if a message was specified on the command line, just use it */
3724 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3725 len = strlen(a->cmdline_log) + 1;
3726 *logmsg = malloc(len + 1);
3727 if (*logmsg == NULL)
3728 return got_error_from_errno("malloc");
3729 strlcpy(*logmsg, a->cmdline_log, len);
3733 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3734 return got_error_from_errno("asprintf");
3736 if (asprintf(&initial_content,
3737 "\n# changes to be committed on branch %s:\n",
3738 a->branch_name) == -1)
3739 return got_error_from_errno("asprintf");
3741 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3745 dprintf(fd, initial_content);
3747 TAILQ_FOREACH(pe, commitable_paths, entry) {
3748 struct got_commitable *ct = pe->data;
3749 dprintf(fd, "# %c %s\n",
3750 got_commitable_get_status(ct),
3751 got_commitable_get_path(ct));
3755 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3757 if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3758 unlink(a->logmsg_path);
3759 free(a->logmsg_path);
3760 a->logmsg_path = NULL;
3762 free(initial_content);
3765 /* Editor is done; we can now apply unveil(2) */
3767 err = apply_unveil(a->repo_path, 0, a->worktree_path);
3776 static const struct got_error *
3777 cmd_commit(int argc, char *argv[])
3779 const struct got_error *error = NULL;
3780 struct got_worktree *worktree = NULL;
3781 struct got_repository *repo = NULL;
3782 char *cwd = NULL, *id_str = NULL;
3783 struct got_object_id *id = NULL;
3784 const char *logmsg = NULL;
3786 struct collect_commit_logmsg_arg cl_arg;
3787 char *editor = NULL;
3788 int ch, rebase_in_progress, histedit_in_progress;
3789 struct got_pathlist_head paths;
3792 cl_arg.logmsg_path = NULL;
3794 while ((ch = getopt(argc, argv, "m:")) != -1) {
3809 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3810 "unveil", NULL) == -1)
3813 error = get_author(&author);
3817 cwd = getcwd(NULL, 0);
3819 error = got_error_from_errno("getcwd");
3822 error = got_worktree_open(&worktree, cwd);
3826 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3829 if (rebase_in_progress) {
3830 error = got_error(GOT_ERR_REBASING);
3834 error = got_worktree_histedit_in_progress(&histedit_in_progress,
3839 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3844 * unveil(2) traverses exec(2); if an editor is used we have
3845 * to apply unveil after the log message has been written.
3847 if (logmsg == NULL || strlen(logmsg) == 0)
3848 error = get_editor(&editor);
3850 error = apply_unveil(got_repo_get_path(repo), 0,
3851 got_worktree_get_root_path(worktree));
3855 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3859 cl_arg.editor = editor;
3860 cl_arg.cmdline_log = logmsg;
3861 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3862 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3863 if (!histedit_in_progress) {
3864 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3865 error = got_error(GOT_ERR_COMMIT_BRANCH);
3868 cl_arg.branch_name += 11;
3870 cl_arg.repo_path = got_repo_get_path(repo);
3871 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
3872 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3874 if (cl_arg.logmsg_path)
3875 fprintf(stderr, "%s: log message preserved in %s\n",
3876 getprogname(), cl_arg.logmsg_path);
3880 if (cl_arg.logmsg_path)
3881 unlink(cl_arg.logmsg_path);
3883 error = got_object_id_str(&id_str, id);
3886 printf("Created commit %s\n", id_str);
3888 free(cl_arg.logmsg_path);
3890 got_repo_close(repo);
3892 got_worktree_close(worktree);
3900 usage_cherrypick(void)
3902 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3906 static const struct got_error *
3907 cmd_cherrypick(int argc, char *argv[])
3909 const struct got_error *error = NULL;
3910 struct got_worktree *worktree = NULL;
3911 struct got_repository *repo = NULL;
3912 char *cwd = NULL, *commit_id_str = NULL;
3913 struct got_object_id *commit_id = NULL;
3914 struct got_commit_object *commit = NULL;
3915 struct got_object_qid *pid;
3916 struct got_reference *head_ref = NULL;
3917 int ch, did_something = 0;
3919 while ((ch = getopt(argc, argv, "")) != -1) {
3931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3932 "unveil", NULL) == -1)
3938 cwd = getcwd(NULL, 0);
3940 error = got_error_from_errno("getcwd");
3943 error = got_worktree_open(&worktree, cwd);
3947 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3951 error = apply_unveil(got_repo_get_path(repo), 0,
3952 got_worktree_get_root_path(worktree));
3956 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3957 GOT_OBJ_TYPE_COMMIT, repo);
3958 if (error != NULL) {
3959 struct got_reference *ref;
3960 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3962 error = got_ref_open(&ref, repo, argv[0], 0);
3965 error = got_ref_resolve(&commit_id, repo, ref);
3970 error = got_object_id_str(&commit_id_str, commit_id);
3974 error = got_ref_open(&head_ref, repo,
3975 got_worktree_get_head_ref_name(worktree), 0);
3979 error = check_same_branch(commit_id, head_ref, NULL, repo);
3981 if (error->code != GOT_ERR_ANCESTRY)
3985 error = got_error(GOT_ERR_SAME_BRANCH);
3989 error = got_object_open_as_commit(&commit, repo, commit_id);
3992 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3993 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3994 commit_id, repo, update_progress, &did_something, check_cancelled,
4000 printf("Merged commit %s\n", commit_id_str);
4003 got_object_commit_close(commit);
4004 free(commit_id_str);
4006 got_ref_close(head_ref);
4008 got_worktree_close(worktree);
4010 got_repo_close(repo);
4017 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4021 static const struct got_error *
4022 cmd_backout(int argc, char *argv[])
4024 const struct got_error *error = NULL;
4025 struct got_worktree *worktree = NULL;
4026 struct got_repository *repo = NULL;
4027 char *cwd = NULL, *commit_id_str = NULL;
4028 struct got_object_id *commit_id = NULL;
4029 struct got_commit_object *commit = NULL;
4030 struct got_object_qid *pid;
4031 struct got_reference *head_ref = NULL;
4032 int ch, did_something = 0;
4034 while ((ch = getopt(argc, argv, "")) != -1) {
4046 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4047 "unveil", NULL) == -1)
4053 cwd = getcwd(NULL, 0);
4055 error = got_error_from_errno("getcwd");
4058 error = got_worktree_open(&worktree, cwd);
4062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4066 error = apply_unveil(got_repo_get_path(repo), 0,
4067 got_worktree_get_root_path(worktree));
4071 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4072 GOT_OBJ_TYPE_COMMIT, repo);
4073 if (error != NULL) {
4074 struct got_reference *ref;
4075 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4077 error = got_ref_open(&ref, repo, argv[0], 0);
4080 error = got_ref_resolve(&commit_id, repo, ref);
4085 error = got_object_id_str(&commit_id_str, commit_id);
4089 error = got_ref_open(&head_ref, repo,
4090 got_worktree_get_head_ref_name(worktree), 0);
4094 error = check_same_branch(commit_id, head_ref, NULL, repo);
4098 error = got_object_open_as_commit(&commit, repo, commit_id);
4101 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4103 error = got_error(GOT_ERR_ROOT_COMMIT);
4107 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4108 update_progress, &did_something, check_cancelled, NULL);
4113 printf("Backed out commit %s\n", commit_id_str);
4116 got_object_commit_close(commit);
4117 free(commit_id_str);
4119 got_ref_close(head_ref);
4121 got_worktree_close(worktree);
4123 got_repo_close(repo);
4130 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4136 trim_logmsg(char *logmsg, int limit)
4141 len = strlen(logmsg);
4145 nl = strchr(logmsg, '\n');
4150 static const struct got_error *
4151 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4153 const struct got_error *err;
4154 char *logmsg0 = NULL;
4157 err = got_object_commit_get_logmsg(&logmsg0, commit);
4162 while (isspace((unsigned char)s[0]))
4165 *logmsg = strdup(s);
4166 if (*logmsg == NULL) {
4167 err = got_error_from_errno("strdup");
4171 trim_logmsg(*logmsg, limit);
4177 static const struct got_error *
4178 show_rebase_progress(struct got_commit_object *commit,
4179 struct got_object_id *old_id, struct got_object_id *new_id)
4181 const struct got_error *err;
4182 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4184 err = got_object_id_str(&old_id_str, old_id);
4189 err = got_object_id_str(&new_id_str, new_id);
4194 old_id_str[12] = '\0';
4196 new_id_str[12] = '\0';
4198 err = get_short_logmsg(&logmsg, 42, commit);
4202 printf("%s -> %s: %s\n", old_id_str,
4203 new_id_str ? new_id_str : "no-op change", logmsg);
4210 static const struct got_error *
4211 rebase_progress(void *arg, unsigned char status, const char *path)
4213 unsigned char *rebase_status = arg;
4215 while (path[0] == '/')
4217 printf("%c %s\n", status, path);
4219 if (*rebase_status == GOT_STATUS_CONFLICT)
4221 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4222 *rebase_status = status;
4226 static const struct got_error *
4227 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4228 struct got_reference *branch, struct got_reference *new_base_branch,
4229 struct got_reference *tmp_branch, struct got_repository *repo)
4231 printf("Switching work tree to %s\n", got_ref_get_name(branch));
4232 return got_worktree_rebase_complete(worktree, fileindex,
4233 new_base_branch, tmp_branch, branch, repo);
4236 static const struct got_error *
4237 rebase_commit(struct got_pathlist_head *merged_paths,
4238 struct got_worktree *worktree, struct got_fileindex *fileindex,
4239 struct got_reference *tmp_branch,
4240 struct got_object_id *commit_id, struct got_repository *repo)
4242 const struct got_error *error;
4243 struct got_commit_object *commit;
4244 struct got_object_id *new_commit_id;
4246 error = got_object_open_as_commit(&commit, repo, commit_id);
4250 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
4251 worktree, fileindex, tmp_branch, commit, commit_id, repo);
4253 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
4255 error = show_rebase_progress(commit, commit_id, NULL);
4257 error = show_rebase_progress(commit, commit_id, new_commit_id);
4258 free(new_commit_id);
4261 got_object_commit_close(commit);
4265 struct check_path_prefix_arg {
4266 const char *path_prefix;
4271 static const struct got_error *
4272 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
4273 struct got_blob_object *blob2, struct got_object_id *id1,
4274 struct got_object_id *id2, const char *path1, const char *path2,
4275 struct got_repository *repo)
4277 struct check_path_prefix_arg *a = arg;
4279 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
4280 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
4281 return got_error(a->errcode);
4286 static const struct got_error *
4287 check_path_prefix(struct got_object_id *parent_id,
4288 struct got_object_id *commit_id, const char *path_prefix,
4289 int errcode, struct got_repository *repo)
4291 const struct got_error *err;
4292 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
4293 struct got_commit_object *commit = NULL, *parent_commit = NULL;
4294 struct check_path_prefix_arg cpp_arg;
4296 if (got_path_is_root_dir(path_prefix))
4299 err = got_object_open_as_commit(&commit, repo, commit_id);
4303 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
4307 err = got_object_open_as_tree(&tree1, repo,
4308 got_object_commit_get_tree_id(parent_commit));
4312 err = got_object_open_as_tree(&tree2, repo,
4313 got_object_commit_get_tree_id(commit));
4317 cpp_arg.path_prefix = path_prefix;
4318 while (cpp_arg.path_prefix[0] == '/')
4319 cpp_arg.path_prefix++;
4320 cpp_arg.len = strlen(cpp_arg.path_prefix);
4321 cpp_arg.errcode = errcode;
4322 err = got_diff_tree(tree1, tree2, "", "", repo,
4323 check_path_prefix_in_diff, &cpp_arg, 0);
4326 got_object_tree_close(tree1);
4328 got_object_tree_close(tree2);
4330 got_object_commit_close(commit);
4332 got_object_commit_close(parent_commit);
4336 static const struct got_error *
4337 collect_commits(struct got_object_id_queue *commits,
4338 struct got_object_id *initial_commit_id,
4339 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
4340 const char *path_prefix, int path_prefix_errcode,
4341 struct got_repository *repo)
4343 const struct got_error *err = NULL;
4344 struct got_commit_graph *graph = NULL;
4345 struct got_object_id *parent_id = NULL;
4346 struct got_object_qid *qid;
4347 struct got_object_id *commit_id = initial_commit_id;
4349 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
4353 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
4354 check_cancelled, NULL);
4357 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
4358 err = got_commit_graph_iter_next(&parent_id, graph);
4360 if (err->code == GOT_ERR_ITER_COMPLETED) {
4361 err = got_error_msg(GOT_ERR_ANCESTRY,
4362 "ran out of commits to rebase before "
4363 "youngest common ancestor commit has "
4366 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
4368 err = got_commit_graph_fetch_commits(graph, 1, repo,
4369 check_cancelled, NULL);
4373 err = check_path_prefix(parent_id, commit_id,
4374 path_prefix, path_prefix_errcode, repo);
4378 err = got_object_qid_alloc(&qid, commit_id);
4381 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
4382 commit_id = parent_id;
4386 got_commit_graph_close(graph);
4390 static const struct got_error *
4391 cmd_rebase(int argc, char *argv[])
4393 const struct got_error *error = NULL;
4394 struct got_worktree *worktree = NULL;
4395 struct got_repository *repo = NULL;
4396 struct got_fileindex *fileindex = NULL;
4398 struct got_reference *branch = NULL;
4399 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
4400 struct got_object_id *commit_id = NULL, *parent_id = NULL;
4401 struct got_object_id *resume_commit_id = NULL;
4402 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
4403 struct got_commit_object *commit = NULL;
4404 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
4405 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4406 struct got_object_id_queue commits;
4407 struct got_pathlist_head merged_paths;
4408 const struct got_object_id_queue *parent_ids;
4409 struct got_object_qid *qid, *pid;
4411 SIMPLEQ_INIT(&commits);
4412 TAILQ_INIT(&merged_paths);
4414 while ((ch = getopt(argc, argv, "ac")) != -1) {
4420 continue_rebase = 1;
4432 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4433 "unveil", NULL) == -1)
4436 if (abort_rebase && continue_rebase)
4438 else if (abort_rebase || continue_rebase) {
4441 } else if (argc != 1)
4444 cwd = getcwd(NULL, 0);
4446 error = got_error_from_errno("getcwd");
4449 error = got_worktree_open(&worktree, cwd);
4453 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4457 error = apply_unveil(got_repo_get_path(repo), 0,
4458 got_worktree_get_root_path(worktree));
4462 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4468 if (!rebase_in_progress) {
4469 error = got_error(GOT_ERR_NOT_REBASING);
4472 error = got_worktree_rebase_continue(&resume_commit_id,
4473 &new_base_branch, &tmp_branch, &branch, &fileindex,
4477 printf("Switching work tree to %s\n",
4478 got_ref_get_symref_target(new_base_branch));
4479 error = got_worktree_rebase_abort(worktree, fileindex, repo,
4480 new_base_branch, update_progress, &did_something);
4483 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
4484 goto done; /* nothing else to do */
4487 if (continue_rebase) {
4488 if (!rebase_in_progress) {
4489 error = got_error(GOT_ERR_NOT_REBASING);
4492 error = got_worktree_rebase_continue(&resume_commit_id,
4493 &new_base_branch, &tmp_branch, &branch, &fileindex,
4498 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
4499 resume_commit_id, repo);
4503 yca_id = got_object_id_dup(resume_commit_id);
4504 if (yca_id == NULL) {
4505 error = got_error_from_errno("got_object_id_dup");
4509 error = got_ref_open(&branch, repo, argv[0], 0);
4514 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
4518 if (!continue_rebase) {
4519 struct got_object_id *base_commit_id;
4521 base_commit_id = got_worktree_get_base_commit_id(worktree);
4522 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
4523 base_commit_id, branch_head_commit_id, repo,
4524 check_cancelled, NULL);
4527 if (yca_id == NULL) {
4528 error = got_error_msg(GOT_ERR_ANCESTRY,
4529 "specified branch shares no common ancestry "
4530 "with work tree's branch");
4534 error = check_same_branch(base_commit_id, branch, yca_id, repo);
4536 if (error->code != GOT_ERR_ANCESTRY)
4540 error = got_error_msg(GOT_ERR_SAME_BRANCH,
4541 "specified branch resolves to a commit which "
4542 "is already contained in work tree's branch");
4545 error = got_worktree_rebase_prepare(&new_base_branch,
4546 &tmp_branch, &fileindex, worktree, branch, repo);
4551 commit_id = branch_head_commit_id;
4552 error = got_object_open_as_commit(&commit, repo, commit_id);
4556 parent_ids = got_object_commit_get_parent_ids(commit);
4557 pid = SIMPLEQ_FIRST(parent_ids);
4559 if (!continue_rebase) {
4561 error = got_worktree_rebase_abort(worktree, fileindex,
4562 repo, new_base_branch, update_progress,
4566 printf("Rebase of %s aborted\n",
4567 got_ref_get_name(branch));
4569 error = got_error(GOT_ERR_EMPTY_REBASE);
4572 error = collect_commits(&commits, commit_id, pid->id,
4573 yca_id, got_worktree_get_path_prefix(worktree),
4574 GOT_ERR_REBASE_PATH, repo);
4575 got_object_commit_close(commit);
4580 if (SIMPLEQ_EMPTY(&commits)) {
4581 if (continue_rebase)
4582 error = rebase_complete(worktree, fileindex,
4583 branch, new_base_branch, tmp_branch, repo);
4585 error = got_error(GOT_ERR_EMPTY_REBASE);
4590 SIMPLEQ_FOREACH(qid, &commits, entry) {
4591 commit_id = qid->id;
4592 parent_id = pid ? pid->id : yca_id;
4595 error = got_worktree_rebase_merge_files(&merged_paths,
4596 worktree, fileindex, parent_id, commit_id, repo,
4597 rebase_progress, &rebase_status, check_cancelled, NULL);
4601 if (rebase_status == GOT_STATUS_CONFLICT) {
4602 got_worktree_rebase_pathlist_free(&merged_paths);
4606 error = rebase_commit(&merged_paths, worktree, fileindex,
4607 tmp_branch, commit_id, repo);
4608 got_worktree_rebase_pathlist_free(&merged_paths);
4613 if (rebase_status == GOT_STATUS_CONFLICT) {
4614 error = got_worktree_rebase_postpone(worktree, fileindex);
4617 error = got_error_msg(GOT_ERR_CONFLICTS,
4618 "conflicts must be resolved before rebasing can continue");
4620 error = rebase_complete(worktree, fileindex, branch,
4621 new_base_branch, tmp_branch, repo);
4623 got_object_id_queue_free(&commits);
4624 free(branch_head_commit_id);
4625 free(resume_commit_id);
4628 got_object_commit_close(commit);
4630 got_ref_close(branch);
4631 if (new_base_branch)
4632 got_ref_close(new_base_branch);
4634 got_ref_close(tmp_branch);
4636 got_worktree_close(worktree);
4638 got_repo_close(repo);
4643 usage_histedit(void)
4645 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
4650 #define GOT_HISTEDIT_PICK 'p'
4651 #define GOT_HISTEDIT_EDIT 'e'
4652 #define GOT_HISTEDIT_FOLD 'f'
4653 #define GOT_HISTEDIT_DROP 'd'
4654 #define GOT_HISTEDIT_MESG 'm'
4656 static struct got_histedit_cmd {
4660 } got_histedit_cmds[] = {
4661 { GOT_HISTEDIT_PICK, "pick", "use commit" },
4662 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4663 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4664 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4665 { GOT_HISTEDIT_MESG, "mesg",
4666 "single-line log message for commit above (open editor if empty)" },
4669 struct got_histedit_list_entry {
4670 TAILQ_ENTRY(got_histedit_list_entry) entry;
4671 struct got_object_id *commit_id;
4672 const struct got_histedit_cmd *cmd;
4675 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4677 static const struct got_error *
4678 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4679 FILE *f, struct got_repository *repo)
4681 const struct got_error *err = NULL;
4682 char *logmsg = NULL, *id_str = NULL;
4683 struct got_commit_object *commit = NULL;
4686 err = got_object_open_as_commit(&commit, repo, commit_id);
4690 err = get_short_logmsg(&logmsg, 34, commit);
4694 err = got_object_id_str(&id_str, commit_id);
4698 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4700 err = got_ferror(f, GOT_ERR_IO);
4703 got_object_commit_close(commit);
4709 static const struct got_error *
4710 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4711 struct got_repository *repo)
4713 const struct got_error *err = NULL;
4714 struct got_object_qid *qid;
4716 if (SIMPLEQ_EMPTY(commits))
4717 return got_error(GOT_ERR_EMPTY_HISTEDIT);
4719 SIMPLEQ_FOREACH(qid, commits, entry) {
4720 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4729 static const struct got_error *
4730 write_cmd_list(FILE *f)
4732 const struct got_error *err = NULL;
4735 n = fprintf(f, "# Available histedit commands:\n");
4737 return got_ferror(f, GOT_ERR_IO);
4739 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4740 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4741 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4744 err = got_ferror(f, GOT_ERR_IO);
4748 n = fprintf(f, "# Commits will be processed in order from top to "
4749 "bottom of this file.\n");
4751 return got_ferror(f, GOT_ERR_IO);
4755 static const struct got_error *
4756 histedit_syntax_error(int lineno)
4758 static char msg[42];
4761 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4763 if (ret == -1 || ret >= sizeof(msg))
4764 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4766 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4769 static const struct got_error *
4770 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4771 char *logmsg, struct got_repository *repo)
4773 const struct got_error *err;
4774 struct got_commit_object *folded_commit = NULL;
4775 char *id_str, *folded_logmsg = NULL;
4777 err = got_object_id_str(&id_str, hle->commit_id);
4781 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4785 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
4788 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4789 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4790 folded_logmsg) == -1) {
4791 err = got_error_from_errno("asprintf");
4796 got_object_commit_close(folded_commit);
4798 free(folded_logmsg);
4802 static struct got_histedit_list_entry *
4803 get_folded_commits(struct got_histedit_list_entry *hle)
4805 struct got_histedit_list_entry *prev, *folded = NULL;
4807 prev = TAILQ_PREV(hle, got_histedit_list, entry);
4808 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4809 prev->cmd->code == GOT_HISTEDIT_DROP)) {
4810 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4812 prev = TAILQ_PREV(prev, got_histedit_list, entry);
4818 static const struct got_error *
4819 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4820 struct got_repository *repo)
4822 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
4823 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4824 const struct got_error *err = NULL;
4825 struct got_commit_object *commit = NULL;
4827 struct got_histedit_list_entry *folded = NULL;
4829 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4833 folded = get_folded_commits(hle);
4835 while (folded != hle) {
4836 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4837 folded = TAILQ_NEXT(folded, entry);
4840 err = append_folded_commit_msg(&new_msg, folded,
4846 folded = TAILQ_NEXT(folded, entry);
4850 err = got_object_id_str(&id_str, hle->commit_id);
4853 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
4856 if (asprintf(&new_msg,
4857 "%s\n# original log message of commit %s: %s",
4858 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
4859 err = got_error_from_errno("asprintf");
4865 err = got_object_id_str(&id_str, hle->commit_id);
4869 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4873 dprintf(fd, logmsg);
4876 err = get_editor(&editor);
4880 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4882 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4884 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
4887 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4888 err = got_error_from_errno2("unlink", logmsg_path);
4894 got_object_commit_close(commit);
4898 static const struct got_error *
4899 histedit_parse_list(struct got_histedit_list *histedit_cmds,
4900 FILE *f, struct got_repository *repo)
4902 const struct got_error *err = NULL;
4903 char *line = NULL, *p, *end;
4907 const struct got_histedit_cmd *cmd;
4908 struct got_object_id *commit_id = NULL;
4909 struct got_histedit_list_entry *hle = NULL;
4912 len = getline(&line, &size, f);
4914 const struct got_error *getline_err;
4917 getline_err = got_error_from_errno("getline");
4918 err = got_ferror(f, getline_err->code);
4923 while (isspace((unsigned char)p[0]))
4925 if (p[0] == '#' || p[0] == '\0') {
4931 for (i = 0; i < nitems(got_histedit_cmds); i++) {
4932 cmd = &got_histedit_cmds[i];
4933 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4934 isspace((unsigned char)p[strlen(cmd->name)])) {
4935 p += strlen(cmd->name);
4938 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4943 if (i == nitems(got_histedit_cmds)) {
4944 err = histedit_syntax_error(lineno);
4947 while (isspace((unsigned char)p[0]))
4949 if (cmd->code == GOT_HISTEDIT_MESG) {
4950 if (hle == NULL || hle->logmsg != NULL) {
4951 err = got_error(GOT_ERR_HISTEDIT_CMD);
4955 err = histedit_edit_logmsg(hle, repo);
4959 hle->logmsg = strdup(p);
4960 if (hle->logmsg == NULL) {
4961 err = got_error_from_errno("strdup");
4970 while (end[0] && !isspace((unsigned char)end[0]))
4974 err = got_object_resolve_id_str(&commit_id, repo, p);
4976 /* override error code */
4977 err = histedit_syntax_error(lineno);
4981 hle = malloc(sizeof(*hle));
4983 err = got_error_from_errno("malloc");
4987 hle->commit_id = commit_id;
4992 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5000 static const struct got_error *
5001 histedit_check_script(struct got_histedit_list *histedit_cmds,
5002 struct got_object_id_queue *commits, struct got_repository *repo)
5004 const struct got_error *err = NULL;
5005 struct got_object_qid *qid;
5006 struct got_histedit_list_entry *hle;
5007 static char msg[80];
5010 if (TAILQ_EMPTY(histedit_cmds))
5011 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5012 "histedit script contains no commands");
5013 if (SIMPLEQ_EMPTY(commits))
5014 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5016 SIMPLEQ_FOREACH(qid, commits, entry) {
5017 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5018 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5022 err = got_object_id_str(&id_str, qid->id);
5025 snprintf(msg, sizeof(msg),
5026 "commit %s missing from histedit script", id_str);
5028 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5032 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5033 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5034 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5035 "last commit in histedit script cannot be folded");
5040 static const struct got_error *
5041 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5042 const char *path, struct got_object_id_queue *commits,
5043 struct got_repository *repo)
5045 const struct got_error *err = NULL;
5049 err = get_editor(&editor);
5053 if (spawn_editor(editor, path) == -1) {
5054 err = got_error_from_errno("failed spawning editor");
5058 f = fopen(path, "r");
5060 err = got_error_from_errno("fopen");
5063 err = histedit_parse_list(histedit_cmds, f, repo);
5067 err = histedit_check_script(histedit_cmds, commits, repo);
5069 if (f && fclose(f) != 0 && err == NULL)
5070 err = got_error_from_errno("fclose");
5075 static const struct got_error *
5076 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5077 struct got_object_id_queue *, const char *, struct got_repository *);
5079 static const struct got_error *
5080 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5081 struct got_object_id_queue *commits, struct got_repository *repo)
5083 const struct got_error *err;
5087 err = got_opentemp_named(&path, &f, "got-histedit");
5091 err = write_cmd_list(f);
5095 err = histedit_write_commit_list(commits, f, repo);
5099 if (fclose(f) != 0) {
5100 err = got_error_from_errno("fclose");
5105 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5107 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5108 err->code != GOT_ERR_HISTEDIT_CMD)
5110 err = histedit_edit_list_retry(histedit_cmds, err,
5111 commits, path, repo);
5114 if (f && fclose(f) != 0 && err == NULL)
5115 err = got_error_from_errno("fclose");
5116 if (path && unlink(path) != 0 && err == NULL)
5117 err = got_error_from_errno2("unlink", path);
5122 static const struct got_error *
5123 histedit_save_list(struct got_histedit_list *histedit_cmds,
5124 struct got_worktree *worktree, struct got_repository *repo)
5126 const struct got_error *err = NULL;
5129 struct got_histedit_list_entry *hle;
5130 struct got_commit_object *commit = NULL;
5132 err = got_worktree_get_histedit_script_path(&path, worktree);
5136 f = fopen(path, "w");
5138 err = got_error_from_errno2("fopen", path);
5141 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5142 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5148 int n = fprintf(f, "%c %s\n",
5149 GOT_HISTEDIT_MESG, hle->logmsg);
5151 err = got_ferror(f, GOT_ERR_IO);
5157 if (f && fclose(f) != 0 && err == NULL)
5158 err = got_error_from_errno("fclose");
5161 got_object_commit_close(commit);
5166 histedit_free_list(struct got_histedit_list *histedit_cmds)
5168 struct got_histedit_list_entry *hle;
5170 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5171 TAILQ_REMOVE(histedit_cmds, hle, entry);
5176 static const struct got_error *
5177 histedit_load_list(struct got_histedit_list *histedit_cmds,
5178 const char *path, struct got_repository *repo)
5180 const struct got_error *err = NULL;
5183 f = fopen(path, "r");
5185 err = got_error_from_errno2("fopen", path);
5189 err = histedit_parse_list(histedit_cmds, f, repo);
5191 if (f && fclose(f) != 0 && err == NULL)
5192 err = got_error_from_errno("fclose");
5196 static const struct got_error *
5197 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5198 const struct got_error *edit_err, struct got_object_id_queue *commits,
5199 const char *path, struct got_repository *repo)
5201 const struct got_error *err = NULL, *prev_err = edit_err;
5204 while (resp != 'c' && resp != 'r' && resp != 'a') {
5205 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5206 "or (a)bort: ", getprogname(), prev_err->msg);
5211 histedit_free_list(histedit_cmds);
5212 err = histedit_run_editor(histedit_cmds, path, commits,
5215 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5216 err->code != GOT_ERR_HISTEDIT_CMD)
5223 } else if (resp == 'r') {
5224 histedit_free_list(histedit_cmds);
5225 err = histedit_edit_script(histedit_cmds,
5228 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5229 err->code != GOT_ERR_HISTEDIT_CMD)
5236 } else if (resp == 'a') {
5237 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5240 printf("invalid response '%c'\n", resp);
5246 static const struct got_error *
5247 histedit_complete(struct got_worktree *worktree,
5248 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
5249 struct got_reference *branch, struct got_repository *repo)
5251 printf("Switching work tree to %s\n",
5252 got_ref_get_symref_target(branch));
5253 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
5257 static const struct got_error *
5258 show_histedit_progress(struct got_commit_object *commit,
5259 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
5261 const struct got_error *err;
5262 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5264 err = got_object_id_str(&old_id_str, hle->commit_id);
5269 err = got_object_id_str(&new_id_str, new_id);
5274 old_id_str[12] = '\0';
5276 new_id_str[12] = '\0';
5279 logmsg = strdup(hle->logmsg);
5280 if (logmsg == NULL) {
5281 err = got_error_from_errno("strdup");
5284 trim_logmsg(logmsg, 42);
5286 err = get_short_logmsg(&logmsg, 42, commit);
5291 switch (hle->cmd->code) {
5292 case GOT_HISTEDIT_PICK:
5293 case GOT_HISTEDIT_EDIT:
5294 printf("%s -> %s: %s\n", old_id_str,
5295 new_id_str ? new_id_str : "no-op change", logmsg);
5297 case GOT_HISTEDIT_DROP:
5298 case GOT_HISTEDIT_FOLD:
5299 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
5312 static const struct got_error *
5313 histedit_commit(struct got_pathlist_head *merged_paths,
5314 struct got_worktree *worktree, struct got_fileindex *fileindex,
5315 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
5316 struct got_repository *repo)
5318 const struct got_error *err;
5319 struct got_commit_object *commit;
5320 struct got_object_id *new_commit_id;
5322 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
5323 && hle->logmsg == NULL) {
5324 err = histedit_edit_logmsg(hle, repo);
5329 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5333 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
5334 worktree, fileindex, tmp_branch, commit, hle->commit_id,
5337 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
5339 err = show_histedit_progress(commit, hle, NULL);
5341 err = show_histedit_progress(commit, hle, new_commit_id);
5342 free(new_commit_id);
5345 got_object_commit_close(commit);
5349 static const struct got_error *
5350 histedit_skip_commit(struct got_histedit_list_entry *hle,
5351 struct got_worktree *worktree, struct got_repository *repo)
5353 const struct got_error *error;
5354 struct got_commit_object *commit;
5356 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
5361 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
5365 error = show_histedit_progress(commit, hle, NULL);
5366 got_object_commit_close(commit);
5370 static const struct got_error *
5371 cmd_histedit(int argc, char *argv[])
5373 const struct got_error *error = NULL;
5374 struct got_worktree *worktree = NULL;
5375 struct got_fileindex *fileindex = NULL;
5376 struct got_repository *repo = NULL;
5378 struct got_reference *branch = NULL;
5379 struct got_reference *tmp_branch = NULL;
5380 struct got_object_id *resume_commit_id = NULL;
5381 struct got_object_id *base_commit_id = NULL;
5382 struct got_object_id *head_commit_id = NULL;
5383 struct got_commit_object *commit = NULL;
5384 int ch, rebase_in_progress = 0, did_something;
5385 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
5386 const char *edit_script_path = NULL;
5387 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5388 struct got_object_id_queue commits;
5389 struct got_pathlist_head merged_paths;
5390 const struct got_object_id_queue *parent_ids;
5391 struct got_object_qid *pid;
5392 struct got_histedit_list histedit_cmds;
5393 struct got_histedit_list_entry *hle;
5395 SIMPLEQ_INIT(&commits);
5396 TAILQ_INIT(&histedit_cmds);
5397 TAILQ_INIT(&merged_paths);
5399 while ((ch = getopt(argc, argv, "acF:")) != -1) {
5408 edit_script_path = optarg;
5420 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5421 "unveil", NULL) == -1)
5424 if (abort_edit && continue_edit)
5430 * This command cannot apply unveil(2) in all cases because the
5431 * user may choose to run an editor to edit the histedit script
5432 * and to edit individual commit log messages.
5433 * unveil(2) traverses exec(2); if an editor is used we have to
5434 * apply unveil after edit script and log messages have been written.
5435 * XXX TODO: Make use of unveil(2) where possible.
5438 cwd = getcwd(NULL, 0);
5440 error = got_error_from_errno("getcwd");
5443 error = got_worktree_open(&worktree, cwd);
5447 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5451 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5454 if (rebase_in_progress) {
5455 error = got_error(GOT_ERR_REBASING);
5459 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
5463 if (edit_in_progress && abort_edit) {
5464 error = got_worktree_histedit_continue(&resume_commit_id,
5465 &tmp_branch, &branch, &base_commit_id, &fileindex,
5469 printf("Switching work tree to %s\n",
5470 got_ref_get_symref_target(branch));
5471 error = got_worktree_histedit_abort(worktree, fileindex, repo,
5472 branch, base_commit_id, update_progress, &did_something);
5475 printf("Histedit of %s aborted\n",
5476 got_ref_get_symref_target(branch));
5477 goto done; /* nothing else to do */
5478 } else if (abort_edit) {
5479 error = got_error(GOT_ERR_NOT_HISTEDIT);
5483 if (continue_edit) {
5486 if (!edit_in_progress) {
5487 error = got_error(GOT_ERR_NOT_HISTEDIT);
5491 error = got_worktree_get_histedit_script_path(&path, worktree);
5495 error = histedit_load_list(&histedit_cmds, path, repo);
5500 error = got_worktree_histedit_continue(&resume_commit_id,
5501 &tmp_branch, &branch, &base_commit_id, &fileindex,
5506 error = got_ref_resolve(&head_commit_id, repo, branch);
5510 error = got_object_open_as_commit(&commit, repo,
5514 parent_ids = got_object_commit_get_parent_ids(commit);
5515 pid = SIMPLEQ_FIRST(parent_ids);
5517 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5520 error = collect_commits(&commits, head_commit_id, pid->id,
5521 base_commit_id, got_worktree_get_path_prefix(worktree),
5522 GOT_ERR_HISTEDIT_PATH, repo);
5523 got_object_commit_close(commit);
5528 if (edit_in_progress) {
5529 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5533 error = got_ref_open(&branch, repo,
5534 got_worktree_get_head_ref_name(worktree), 0);
5538 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
5539 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
5540 "will not edit commit history of a branch outside "
5541 "the \"refs/heads/\" reference namespace");
5545 error = got_ref_resolve(&head_commit_id, repo, branch);
5546 got_ref_close(branch);
5551 error = got_object_open_as_commit(&commit, repo,
5555 parent_ids = got_object_commit_get_parent_ids(commit);
5556 pid = SIMPLEQ_FIRST(parent_ids);
5558 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
5561 error = collect_commits(&commits, head_commit_id, pid->id,
5562 got_worktree_get_base_commit_id(worktree),
5563 got_worktree_get_path_prefix(worktree),
5564 GOT_ERR_HISTEDIT_PATH, repo);
5565 got_object_commit_close(commit);
5570 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
5571 &base_commit_id, &fileindex, worktree, repo);
5575 if (edit_script_path) {
5576 error = histedit_load_list(&histedit_cmds,
5577 edit_script_path, repo);
5579 got_worktree_histedit_abort(worktree, fileindex,
5580 repo, branch, base_commit_id,
5581 update_progress, &did_something);
5585 error = histedit_edit_script(&histedit_cmds, &commits,
5588 got_worktree_histedit_abort(worktree, fileindex,
5589 repo, branch, base_commit_id,
5590 update_progress, &did_something);
5596 error = histedit_save_list(&histedit_cmds, worktree,
5599 got_worktree_histedit_abort(worktree, fileindex,
5600 repo, branch, base_commit_id,
5601 update_progress, &did_something);
5607 error = histedit_check_script(&histedit_cmds, &commits, repo);
5611 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5612 if (resume_commit_id) {
5613 if (got_object_id_cmp(hle->commit_id,
5614 resume_commit_id) != 0)
5617 resume_commit_id = NULL;
5618 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5619 hle->cmd->code == GOT_HISTEDIT_FOLD) {
5620 error = histedit_skip_commit(hle, worktree,
5623 error = histedit_commit(NULL, worktree,
5624 fileindex, tmp_branch, hle, repo);
5631 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5632 error = histedit_skip_commit(hle, worktree, repo);
5638 error = got_object_open_as_commit(&commit, repo,
5642 parent_ids = got_object_commit_get_parent_ids(commit);
5643 pid = SIMPLEQ_FIRST(parent_ids);
5645 error = got_worktree_histedit_merge_files(&merged_paths,
5646 worktree, fileindex, pid->id, hle->commit_id, repo,
5647 rebase_progress, &rebase_status, check_cancelled, NULL);
5650 got_object_commit_close(commit);
5653 if (rebase_status == GOT_STATUS_CONFLICT) {
5654 got_worktree_rebase_pathlist_free(&merged_paths);
5658 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5660 error = got_object_id_str(&id_str, hle->commit_id);
5663 printf("Stopping histedit for amending commit %s\n",
5666 got_worktree_rebase_pathlist_free(&merged_paths);
5667 error = got_worktree_histedit_postpone(worktree,
5672 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5673 error = histedit_skip_commit(hle, worktree, repo);
5679 error = histedit_commit(&merged_paths, worktree, fileindex,
5680 tmp_branch, hle, repo);
5681 got_worktree_rebase_pathlist_free(&merged_paths);
5686 if (rebase_status == GOT_STATUS_CONFLICT) {
5687 error = got_worktree_histedit_postpone(worktree, fileindex);
5690 error = got_error_msg(GOT_ERR_CONFLICTS,
5691 "conflicts must be resolved before rebasing can continue");
5693 error = histedit_complete(worktree, fileindex, tmp_branch,
5696 got_object_id_queue_free(&commits);
5697 histedit_free_list(&histedit_cmds);
5698 free(head_commit_id);
5699 free(base_commit_id);
5700 free(resume_commit_id);
5702 got_object_commit_close(commit);
5704 got_ref_close(branch);
5706 got_ref_close(tmp_branch);
5708 got_worktree_close(worktree);
5710 got_repo_close(repo);
5717 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
5718 "[file-path ...]\n",
5723 static const struct got_error *
5724 print_stage(void *arg, unsigned char status, unsigned char staged_status,
5725 const char *path, struct got_object_id *blob_id,
5726 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
5728 const struct got_error *err = NULL;
5729 char *id_str = NULL;
5731 if (staged_status != GOT_STATUS_ADD &&
5732 staged_status != GOT_STATUS_MODIFY &&
5733 staged_status != GOT_STATUS_DELETE)
5736 if (staged_status == GOT_STATUS_ADD ||
5737 staged_status == GOT_STATUS_MODIFY)
5738 err = got_object_id_str(&id_str, staged_blob_id);
5740 err = got_object_id_str(&id_str, blob_id);
5744 printf("%s %c %s\n", id_str, staged_status, path);
5749 static const struct got_error *
5750 cmd_stage(int argc, char *argv[])
5752 const struct got_error *error = NULL;
5753 struct got_repository *repo = NULL;
5754 struct got_worktree *worktree = NULL;
5756 struct got_pathlist_head paths;
5757 struct got_pathlist_entry *pe;
5758 int ch, list_stage = 0, pflag = 0;
5759 FILE *patch_script_file = NULL;
5760 const char *patch_script_path = NULL;
5761 struct choose_patch_arg cpa;
5765 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
5774 patch_script_path = optarg;
5786 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5787 "unveil", NULL) == -1)
5790 if (list_stage && (pflag || patch_script_path))
5791 errx(1, "-l option cannot be used with other options");
5792 if (patch_script_path && !pflag)
5793 errx(1, "-F option can only be used together with -p option");
5795 cwd = getcwd(NULL, 0);
5797 error = got_error_from_errno("getcwd");
5801 error = got_worktree_open(&worktree, cwd);
5805 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5809 if (patch_script_path) {
5810 patch_script_file = fopen(patch_script_path, "r");
5811 if (patch_script_file == NULL) {
5812 error = got_error_from_errno2("fopen",
5817 error = apply_unveil(got_repo_get_path(repo), 1,
5818 got_worktree_get_root_path(worktree));
5822 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5827 error = got_worktree_status(worktree, &paths, repo,
5828 print_stage, NULL, check_cancelled, NULL);
5830 cpa.patch_script_file = patch_script_file;
5831 cpa.action = "stage";
5832 error = got_worktree_stage(worktree, &paths,
5833 pflag ? NULL : print_status, NULL,
5834 pflag ? choose_patch : NULL, &cpa, repo);
5837 if (patch_script_file && fclose(patch_script_file) == EOF &&
5839 error = got_error_from_errno2("fclose", patch_script_path);
5841 got_repo_close(repo);
5843 got_worktree_close(worktree);
5844 TAILQ_FOREACH(pe, &paths, entry)
5845 free((char *)pe->path);
5846 got_pathlist_free(&paths);
5854 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
5855 "[file-path ...]\n",
5861 static const struct got_error *
5862 cmd_unstage(int argc, char *argv[])
5864 const struct got_error *error = NULL;
5865 struct got_repository *repo = NULL;
5866 struct got_worktree *worktree = NULL;
5868 struct got_pathlist_head paths;
5869 struct got_pathlist_entry *pe;
5870 int ch, did_something = 0, pflag = 0;
5871 FILE *patch_script_file = NULL;
5872 const char *patch_script_path = NULL;
5873 struct choose_patch_arg cpa;
5877 while ((ch = getopt(argc, argv, "pF:")) != -1) {
5883 patch_script_path = optarg;
5895 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5896 "unveil", NULL) == -1)
5899 if (patch_script_path && !pflag)
5900 errx(1, "-F option can only be used together with -p option");
5902 cwd = getcwd(NULL, 0);
5904 error = got_error_from_errno("getcwd");
5908 error = got_worktree_open(&worktree, cwd);
5912 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
5916 if (patch_script_path) {
5917 patch_script_file = fopen(patch_script_path, "r");
5918 if (patch_script_file == NULL) {
5919 error = got_error_from_errno2("fopen",
5925 error = apply_unveil(got_repo_get_path(repo), 1,
5926 got_worktree_get_root_path(worktree));
5930 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5934 cpa.patch_script_file = patch_script_file;
5935 cpa.action = "unstage";
5936 error = got_worktree_unstage(worktree, &paths, update_progress,
5937 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
5939 if (patch_script_file && fclose(patch_script_file) == EOF &&
5941 error = got_error_from_errno2("fclose", patch_script_path);
5943 got_repo_close(repo);
5945 got_worktree_close(worktree);
5946 TAILQ_FOREACH(pe, &paths, entry)
5947 free((char *)pe->path);
5948 got_pathlist_free(&paths);