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>
40 #include "got_version.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
49 #include "got_commit_graph.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_opentemp.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static volatile sig_atomic_t sigint_received;
59 static volatile sig_atomic_t sigpipe_received;
62 catch_sigint(int signo)
68 catch_sigpipe(int signo)
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
81 __dead static void usage(int);
82 __dead static void usage_init(void);
83 __dead static void usage_import(void);
84 __dead static void usage_checkout(void);
85 __dead static void usage_update(void);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_status(void);
91 __dead static void usage_ref(void);
92 __dead static void usage_branch(void);
93 __dead static void usage_tag(void);
94 __dead static void usage_add(void);
95 __dead static void usage_remove(void);
96 __dead static void usage_revert(void);
97 __dead static void usage_commit(void);
98 __dead static void usage_cherrypick(void);
99 __dead static void usage_backout(void);
100 __dead static void usage_rebase(void);
101 __dead static void usage_histedit(void);
102 __dead static void usage_integrate(void);
103 __dead static void usage_stage(void);
104 __dead static void usage_unstage(void);
105 __dead static void usage_cat(void);
107 static const struct got_error* cmd_init(int, char *[]);
108 static const struct got_error* cmd_import(int, char *[]);
109 static const struct got_error* cmd_checkout(int, char *[]);
110 static const struct got_error* cmd_update(int, char *[]);
111 static const struct got_error* cmd_log(int, char *[]);
112 static const struct got_error* cmd_diff(int, char *[]);
113 static const struct got_error* cmd_blame(int, char *[]);
114 static const struct got_error* cmd_tree(int, char *[]);
115 static const struct got_error* cmd_status(int, char *[]);
116 static const struct got_error* cmd_ref(int, char *[]);
117 static const struct got_error* cmd_branch(int, char *[]);
118 static const struct got_error* cmd_tag(int, char *[]);
119 static const struct got_error* cmd_add(int, char *[]);
120 static const struct got_error* cmd_remove(int, char *[]);
121 static const struct got_error* cmd_revert(int, char *[]);
122 static const struct got_error* cmd_commit(int, char *[]);
123 static const struct got_error* cmd_cherrypick(int, char *[]);
124 static const struct got_error* cmd_backout(int, char *[]);
125 static const struct got_error* cmd_rebase(int, char *[]);
126 static const struct got_error* cmd_histedit(int, char *[]);
127 static const struct got_error* cmd_integrate(int, char *[]);
128 static const struct got_error* cmd_stage(int, char *[]);
129 static const struct got_error* cmd_unstage(int, char *[]);
130 static const struct got_error* cmd_cat(int, char *[]);
132 static struct got_cmd got_commands[] = {
133 { "init", cmd_init, usage_init, "in" },
134 { "import", cmd_import, usage_import, "im" },
135 { "checkout", cmd_checkout, usage_checkout, "co" },
136 { "update", cmd_update, usage_update, "up" },
137 { "log", cmd_log, usage_log, "" },
138 { "diff", cmd_diff, usage_diff, "di" },
139 { "blame", cmd_blame, usage_blame, "bl" },
140 { "tree", cmd_tree, usage_tree, "tr" },
141 { "status", cmd_status, usage_status, "st" },
142 { "ref", cmd_ref, usage_ref, "" },
143 { "branch", cmd_branch, usage_branch, "br" },
144 { "tag", cmd_tag, usage_tag, "" },
145 { "add", cmd_add, usage_add, "" },
146 { "remove", cmd_remove, usage_remove, "rm" },
147 { "revert", cmd_revert, usage_revert, "rv" },
148 { "commit", cmd_commit, usage_commit, "ci" },
149 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 { "backout", cmd_backout, usage_backout, "bo" },
151 { "rebase", cmd_rebase, usage_rebase, "rb" },
152 { "histedit", cmd_histedit, usage_histedit, "he" },
153 { "integrate", cmd_integrate, usage_integrate,"ig" },
154 { "stage", cmd_stage, usage_stage, "sg" },
155 { "unstage", cmd_unstage, usage_unstage, "ug" },
156 { "cat", cmd_cat, usage_cat, "" },
164 fprintf(stderr, "commands:");
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct got_cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s", cmd->cmd_name);
173 main(int argc, char *argv[])
178 int hflag = 0, Vflag = 0;
180 setlocale(LC_CTYPE, "");
182 while ((ch = getopt(argc, argv, "hV")) != -1) {
201 got_version_print_str();
208 signal(SIGINT, catch_sigint);
209 signal(SIGPIPE, catch_sigpipe);
211 for (i = 0; i < nitems(got_commands); i++) {
212 const struct got_error *error;
214 cmd = &got_commands[i];
216 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 strcmp(cmd->cmd_alias, argv[0]) != 0)
221 got_commands[i].cmd_usage();
223 error = got_commands[i].cmd_main(argc, argv);
224 if (error && error->code != GOT_ERR_CANCELLED &&
225 error->code != GOT_ERR_PRIVSEP_EXIT &&
226 !(sigpipe_received &&
227 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
229 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
237 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
245 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
252 static const struct got_error *
253 get_editor(char **abspath)
255 const struct got_error *err = NULL;
260 editor = getenv("VISUAL");
262 editor = getenv("EDITOR");
265 err = got_path_find_prog(abspath, editor);
270 if (*abspath == NULL) {
271 *abspath = strdup("/bin/ed");
272 if (*abspath == NULL)
273 return got_error_from_errno("strdup");
279 static const struct got_error *
280 apply_unveil(const char *repo_path, int repo_read_only,
281 const char *worktree_path)
283 const struct got_error *err;
286 if (unveil("gmon.out", "rwc") != 0)
287 return got_error_from_errno2("unveil", "gmon.out");
289 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 return got_error_from_errno2("unveil", repo_path);
292 if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 return got_error_from_errno2("unveil", worktree_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
311 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
315 static const struct got_error *
316 cmd_init(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 char *repo_path = NULL;
322 while ((ch = getopt(argc, argv, "")) != -1) {
334 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 repo_path = strdup(argv[0]);
341 if (repo_path == NULL)
342 return got_error_from_errno("strdup");
344 got_path_strip_trailing_slashes(repo_path);
346 error = got_path_mkdir(repo_path);
348 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
351 error = apply_unveil(repo_path, 0, NULL);
355 error = got_repo_init(repo_path);
367 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 "[-r repository-path] [-I pattern] path\n", getprogname());
373 spawn_editor(const char *editor, const char *file)
376 sig_t sighup, sigint, sigquit;
379 sighup = signal(SIGHUP, SIG_IGN);
380 sigint = signal(SIGINT, SIG_IGN);
381 sigquit = signal(SIGQUIT, SIG_IGN);
383 switch (pid = fork()) {
387 execl(editor, editor, file, (char *)NULL);
391 while (waitpid(pid, &st, 0) == -1)
396 (void)signal(SIGHUP, sighup);
397 (void)signal(SIGINT, sigint);
398 (void)signal(SIGQUIT, sigquit);
400 if (!WIFEXITED(st)) {
405 return WEXITSTATUS(st);
408 static const struct got_error *
409 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 const char *initial_content)
412 const struct got_error *err = NULL;
416 int content_changed = 0;
421 if (stat(logmsg_path, &st) == -1)
422 return got_error_from_errno2("stat", logmsg_path);
424 if (spawn_editor(editor, logmsg_path) == -1)
425 return got_error_from_errno("failed spawning editor");
427 if (stat(logmsg_path, &st2) == -1)
428 return got_error_from_errno("stat");
430 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 "no changes made to commit message, aborting");
434 *logmsg = malloc(st2.st_size + 1);
436 return got_error_from_errno("malloc");
440 fp = fopen(logmsg_path, "r");
442 err = got_error_from_errno("fopen");
445 while (fgets(buf, sizeof(buf), fp) != NULL) {
446 if (!content_changed && strcmp(buf, initial_content) != 0)
448 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 continue; /* remove comments and leading empty lines */
450 len = strlcat(*logmsg, buf, st2.st_size);
454 while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 (*logmsg)[len - 1] = '\0';
459 if (len == 0 || !content_changed)
460 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 "commit message cannot be empty, aborting");
470 static const struct got_error *
471 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 const char *path_dir, const char *branch_name)
474 char *initial_content = NULL;
475 const struct got_error *err = NULL;
478 if (asprintf(&initial_content,
479 "\n# %s to be imported to branch %s\n", path_dir,
481 return got_error_from_errno("asprintf");
483 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
487 dprintf(fd, initial_content);
490 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
492 free(initial_content);
496 static const struct got_error *
497 import_progress(void *arg, const char *path)
499 printf("A %s\n", path);
503 static const struct got_error *
504 get_author(char **author, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 const char *got_author, *name, *email;
511 name = got_repo_get_gitconfig_author_name(repo);
512 email = got_repo_get_gitconfig_author_email(repo);
514 if (asprintf(author, "%s <%s>", name, email) == -1)
515 return got_error_from_errno("asprintf");
519 got_author = getenv("GOT_AUTHOR");
520 if (got_author == NULL) {
521 name = got_repo_get_global_gitconfig_author_name(repo);
522 email = got_repo_get_global_gitconfig_author_email(repo);
524 if (asprintf(author, "%s <%s>", name, email) == -1)
525 return got_error_from_errno("asprintf");
528 /* TODO: Look up user in password database? */
529 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 *author = strdup(got_author);
534 return got_error_from_errno("strdup");
537 * Really dumb email address check; we're only doing this to
538 * avoid git's object parser breaking on commits we create.
540 while (*got_author && *got_author != '<')
542 if (*got_author != '<') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 while (*got_author && *got_author != '@')
548 if (*got_author != '@') {
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 while (*got_author && *got_author != '>')
554 if (*got_author != '>')
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 static const struct got_error *
565 get_gitconfig_path(char **gitconfig_path)
567 const char *homedir = getenv("HOME");
569 *gitconfig_path = NULL;
571 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 return got_error_from_errno("asprintf");
578 static const struct got_error *
579 cmd_import(int argc, char *argv[])
581 const struct got_error *error = NULL;
582 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 const char *branch_name = "main";
585 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 struct got_repository *repo = NULL;
587 struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 struct got_object_id *new_commit_id = NULL;
590 struct got_pathlist_head ignores;
591 struct got_pathlist_entry *pe;
592 int preserve_logmsg = 0;
594 TAILQ_INIT(&ignores);
596 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 branch_name = optarg;
602 logmsg = strdup(optarg);
603 if (logmsg == NULL) {
604 error = got_error_from_errno("strdup");
609 repo_path = realpath(optarg, NULL);
610 if (repo_path == NULL) {
611 error = got_error_from_errno2("realpath",
617 if (optarg[0] == '\0')
619 error = got_pathlist_insert(&pe, &ignores, optarg,
634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
642 if (repo_path == NULL) {
643 repo_path = getcwd(NULL, 0);
644 if (repo_path == NULL)
645 return got_error_from_errno("getcwd");
647 got_path_strip_trailing_slashes(repo_path);
648 error = get_gitconfig_path(&gitconfig_path);
651 error = got_repo_open(&repo, repo_path, gitconfig_path);
655 error = get_author(&author, repo);
660 * Don't let the user create a branch name with a leading '-'.
661 * While technically a valid reference name, this case is usually
662 * an unintended typo.
664 if (branch_name[0] == '-')
665 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
667 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 error = got_error_from_errno("asprintf");
672 error = got_ref_open(&branch_ref, repo, refname, 0);
674 if (error->code != GOT_ERR_NOT_REF)
677 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 "import target branch already exists");
682 path_dir = realpath(argv[0], NULL);
683 if (path_dir == NULL) {
684 error = got_error_from_errno2("realpath", argv[0]);
687 got_path_strip_trailing_slashes(path_dir);
690 * unveil(2) traverses exec(2); if an editor is used we have
691 * to apply unveil after the log message has been written.
693 if (logmsg == NULL || strlen(logmsg) == 0) {
694 error = get_editor(&editor);
698 error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
708 if (unveil(path_dir, "r") != 0) {
709 error = got_error_from_errno2("unveil", path_dir);
715 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
722 error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 author, &ignores, repo, import_progress, NULL);
730 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
737 error = got_ref_write(branch_ref, repo);
744 error = got_object_id_str(&id_str, new_commit_id);
751 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
753 if (error->code != GOT_ERR_NOT_REF) {
759 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
767 error = got_ref_write(head_ref, repo);
775 printf("Created branch %s with commit %s\n",
776 got_ref_get_name(branch_ref), id_str);
778 if (preserve_logmsg) {
779 fprintf(stderr, "%s: log message preserved in %s\n",
780 getprogname(), logmsg_path);
781 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 error = got_error_from_errno2("unlink", logmsg_path);
791 free(gitconfig_path);
793 got_ref_close(branch_ref);
795 got_ref_close(head_ref);
802 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 static const struct got_error *
808 checkout_progress(void *arg, unsigned char status, const char *path)
810 char *worktree_path = arg;
812 /* Base commit bump happens silently. */
813 if (status == GOT_STATUS_BUMP_BASE)
816 while (path[0] == '/')
819 printf("%c %s/%s\n", status, worktree_path, path);
823 static const struct got_error *
824 check_cancelled(void *arg)
826 if (sigint_received || sigpipe_received)
827 return got_error(GOT_ERR_CANCELLED);
831 static const struct got_error *
832 check_linear_ancestry(struct got_object_id *commit_id,
833 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
834 struct got_repository *repo)
836 const struct got_error *err = NULL;
837 struct got_object_id *yca_id;
839 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
840 commit_id, base_commit_id, repo, check_cancelled, NULL);
845 return got_error(GOT_ERR_ANCESTRY);
848 * Require a straight line of history between the target commit
849 * and the work tree's base commit.
851 * Non-linear situations such as this require a rebase:
853 * (commit) D F (base_commit)
861 * 'got update' only handles linear cases:
862 * Update forwards in time: A (base/yca) - B - C - D (commit)
863 * Update backwards in time: D (base) - C - B - A (commit/yca)
865 if (allow_forwards_in_time_only) {
866 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
867 return got_error(GOT_ERR_ANCESTRY);
868 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
869 got_object_id_cmp(base_commit_id, yca_id) != 0)
870 return got_error(GOT_ERR_ANCESTRY);
876 static const struct got_error *
877 check_same_branch(struct got_object_id *commit_id,
878 struct got_reference *head_ref, struct got_object_id *yca_id,
879 struct got_repository *repo)
881 const struct got_error *err = NULL;
882 struct got_commit_graph *graph = NULL;
883 struct got_object_id *head_commit_id = NULL;
884 int is_same_branch = 0;
886 err = got_ref_resolve(&head_commit_id, repo, head_ref);
890 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
894 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
899 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
903 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
904 check_cancelled, NULL);
909 struct got_object_id *id;
910 err = got_commit_graph_iter_next(&id, graph);
912 if (err->code == GOT_ERR_ITER_COMPLETED) {
915 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
917 err = got_commit_graph_fetch_commits(graph, 1,
918 repo, check_cancelled, NULL);
924 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
926 if (got_object_id_cmp(id, commit_id) == 0) {
934 got_commit_graph_close(graph);
935 free(head_commit_id);
936 if (!err && !is_same_branch)
937 err = got_error(GOT_ERR_ANCESTRY);
941 static const struct got_error *
942 resolve_commit_arg(struct got_object_id **commit_id,
943 const char *commit_id_arg, struct got_repository *repo)
945 const struct got_error *err;
946 struct got_reference *ref;
947 struct got_tag_object *tag;
949 err = got_repo_object_match_tag(&tag, commit_id_arg,
950 GOT_OBJ_TYPE_COMMIT, repo);
952 *commit_id = got_object_id_dup(
953 got_object_tag_get_object_id(tag));
954 if (*commit_id == NULL)
955 err = got_error_from_errno("got_object_id_dup");
956 got_object_tag_close(tag);
958 } else if (err->code != GOT_ERR_NO_OBJ)
961 err = got_ref_open(&ref, repo, commit_id_arg, 0);
963 err = got_ref_resolve(commit_id, repo, ref);
966 if (err->code != GOT_ERR_NOT_REF)
968 err = got_repo_match_object_id_prefix(commit_id,
969 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
974 static const struct got_error *
975 cmd_checkout(int argc, char *argv[])
977 const struct got_error *error = NULL;
978 struct got_repository *repo = NULL;
979 struct got_reference *head_ref = NULL;
980 struct got_worktree *worktree = NULL;
981 char *repo_path = NULL;
982 char *worktree_path = NULL;
983 const char *path_prefix = "";
984 const char *branch_name = GOT_REF_HEAD;
985 char *commit_id_str = NULL;
986 int ch, same_path_prefix;
987 struct got_pathlist_head paths;
991 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
994 branch_name = optarg;
997 commit_id_str = strdup(optarg);
998 if (commit_id_str == NULL)
999 return got_error_from_errno("strdup");
1002 path_prefix = optarg;
1014 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1015 "unveil", NULL) == -1)
1019 char *cwd, *base, *dotgit;
1020 repo_path = realpath(argv[0], NULL);
1021 if (repo_path == NULL)
1022 return got_error_from_errno2("realpath", argv[0]);
1023 cwd = getcwd(NULL, 0);
1025 error = got_error_from_errno("getcwd");
1028 if (path_prefix[0]) {
1029 base = basename(path_prefix);
1031 error = got_error_from_errno2("basename",
1036 base = basename(repo_path);
1038 error = got_error_from_errno2("basename",
1043 dotgit = strstr(base, ".git");
1046 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1047 error = got_error_from_errno("asprintf");
1052 } else if (argc == 2) {
1053 repo_path = realpath(argv[0], NULL);
1054 if (repo_path == NULL) {
1055 error = got_error_from_errno2("realpath", argv[0]);
1058 worktree_path = realpath(argv[1], NULL);
1059 if (worktree_path == NULL) {
1060 if (errno != ENOENT) {
1061 error = got_error_from_errno2("realpath",
1065 worktree_path = strdup(argv[1]);
1066 if (worktree_path == NULL) {
1067 error = got_error_from_errno("strdup");
1074 got_path_strip_trailing_slashes(repo_path);
1075 got_path_strip_trailing_slashes(worktree_path);
1077 error = got_repo_open(&repo, repo_path, NULL);
1081 /* Pre-create work tree path for unveil(2) */
1082 error = got_path_mkdir(worktree_path);
1084 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1085 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1087 if (!got_path_dir_is_empty(worktree_path)) {
1088 error = got_error_path(worktree_path,
1089 GOT_ERR_DIR_NOT_EMPTY);
1094 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1098 error = got_ref_open(&head_ref, repo, branch_name, 0);
1102 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1103 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1106 error = got_worktree_open(&worktree, worktree_path);
1110 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1114 if (!same_path_prefix) {
1115 error = got_error(GOT_ERR_PATH_PREFIX);
1119 if (commit_id_str) {
1120 struct got_object_id *commit_id;
1121 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1124 error = check_linear_ancestry(commit_id,
1125 got_worktree_get_base_commit_id(worktree), 0, repo);
1126 if (error != NULL) {
1130 error = check_same_branch(commit_id, head_ref, NULL, repo);
1133 error = got_worktree_set_base_commit_id(worktree, repo,
1140 error = got_pathlist_append(&paths, "", NULL);
1143 error = got_worktree_checkout_files(worktree, &paths, repo,
1144 checkout_progress, worktree_path, check_cancelled, NULL);
1148 printf("Now shut up and hack\n");
1151 got_pathlist_free(&paths);
1152 free(commit_id_str);
1154 free(worktree_path);
1161 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1166 static const struct got_error *
1167 update_progress(void *arg, unsigned char status, const char *path)
1169 int *did_something = arg;
1171 if (status == GOT_STATUS_EXISTS)
1176 /* Base commit bump happens silently. */
1177 if (status == GOT_STATUS_BUMP_BASE)
1180 while (path[0] == '/')
1182 printf("%c %s\n", status, path);
1186 static const struct got_error *
1187 switch_head_ref(struct got_reference *head_ref,
1188 struct got_object_id *commit_id, struct got_worktree *worktree,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1193 int ref_has_moved = 0;
1195 /* Trivial case: switching between two different references. */
1196 if (strcmp(got_ref_get_name(head_ref),
1197 got_worktree_get_head_ref_name(worktree)) != 0) {
1198 printf("Switching work tree from %s to %s\n",
1199 got_worktree_get_head_ref_name(worktree),
1200 got_ref_get_name(head_ref));
1201 return got_worktree_set_head_ref(worktree, head_ref);
1204 err = check_linear_ancestry(commit_id,
1205 got_worktree_get_base_commit_id(worktree), 0, repo);
1207 if (err->code != GOT_ERR_ANCESTRY)
1214 /* Switching to a rebased branch with the same reference name. */
1215 err = got_object_id_str(&base_id_str,
1216 got_worktree_get_base_commit_id(worktree));
1219 printf("Reference %s now points at a different branch\n",
1220 got_worktree_get_head_ref_name(worktree));
1221 printf("Switching work tree from %s to %s\n", base_id_str,
1222 got_worktree_get_head_ref_name(worktree));
1226 static const struct got_error *
1227 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1229 const struct got_error *err;
1232 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1236 return got_error(GOT_ERR_REBASING);
1238 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1242 return got_error(GOT_ERR_HISTEDIT_BUSY);
1247 static const struct got_error *
1248 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1249 char *argv[], struct got_worktree *worktree)
1251 const struct got_error *err = NULL;
1258 return got_error_from_errno("strdup");
1259 return got_pathlist_append(paths, path, NULL);
1262 for (i = 0; i < argc; i++) {
1263 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1266 err = got_pathlist_append(paths, path, NULL);
1276 static const struct got_error *
1277 cmd_update(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 struct got_repository *repo = NULL;
1281 struct got_worktree *worktree = NULL;
1282 char *worktree_path = NULL;
1283 struct got_object_id *commit_id = NULL;
1284 char *commit_id_str = NULL;
1285 const char *branch_name = NULL;
1286 struct got_reference *head_ref = NULL;
1287 struct got_pathlist_head paths;
1288 struct got_pathlist_entry *pe;
1289 int ch, did_something = 0;
1293 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1296 branch_name = optarg;
1299 commit_id_str = strdup(optarg);
1300 if (commit_id_str == NULL)
1301 return got_error_from_errno("strdup");
1313 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1314 "unveil", NULL) == -1)
1317 worktree_path = getcwd(NULL, 0);
1318 if (worktree_path == NULL) {
1319 error = got_error_from_errno("getcwd");
1322 error = got_worktree_open(&worktree, worktree_path);
1326 error = check_rebase_or_histedit_in_progress(worktree);
1330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1335 error = apply_unveil(got_repo_get_path(repo), 0,
1336 got_worktree_get_root_path(worktree));
1340 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1344 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1345 got_worktree_get_head_ref_name(worktree), 0);
1348 if (commit_id_str == NULL) {
1349 error = got_ref_resolve(&commit_id, repo, head_ref);
1352 error = got_object_id_str(&commit_id_str, commit_id);
1356 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1357 free(commit_id_str);
1358 commit_id_str = NULL;
1361 error = got_object_id_str(&commit_id_str, commit_id);
1367 struct got_object_id *head_commit_id;
1368 TAILQ_FOREACH(pe, &paths, entry) {
1369 if (pe->path_len == 0)
1371 error = got_error_msg(GOT_ERR_BAD_PATH,
1372 "switching between branches requires that "
1373 "the entire work tree gets updated");
1376 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1379 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1381 free(head_commit_id);
1384 error = check_same_branch(commit_id, head_ref, NULL, repo);
1387 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1391 error = check_linear_ancestry(commit_id,
1392 got_worktree_get_base_commit_id(worktree), 0, repo);
1393 if (error != NULL) {
1394 if (error->code == GOT_ERR_ANCESTRY)
1395 error = got_error(GOT_ERR_BRANCH_MOVED);
1398 error = check_same_branch(commit_id, head_ref, NULL, repo);
1403 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1405 error = got_worktree_set_base_commit_id(worktree, repo,
1411 error = got_worktree_checkout_files(worktree, &paths, repo,
1412 update_progress, &did_something, check_cancelled, NULL);
1417 printf("Updated to commit %s\n", commit_id_str);
1419 printf("Already up-to-date\n");
1421 free(worktree_path);
1422 TAILQ_FOREACH(pe, &paths, entry)
1423 free((char *)pe->path);
1424 got_pathlist_free(&paths);
1426 free(commit_id_str);
1430 static const struct got_error *
1431 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1432 const char *path, int diff_context, int ignore_whitespace,
1433 struct got_repository *repo)
1435 const struct got_error *err = NULL;
1436 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1439 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1444 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1448 while (path[0] == '/')
1450 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1451 ignore_whitespace, stdout);
1454 got_object_blob_close(blob1);
1455 got_object_blob_close(blob2);
1459 static const struct got_error *
1460 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1461 const char *path, int diff_context, int ignore_whitespace,
1462 struct got_repository *repo)
1464 const struct got_error *err = NULL;
1465 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1466 struct got_diff_blob_output_unidiff_arg arg;
1469 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1474 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1478 arg.diff_context = diff_context;
1479 arg.ignore_whitespace = ignore_whitespace;
1480 arg.outfile = stdout;
1481 while (path[0] == '/')
1483 err = got_diff_tree(tree1, tree2, path, path, repo,
1484 got_diff_blob_output_unidiff, &arg, 1);
1487 got_object_tree_close(tree1);
1489 got_object_tree_close(tree2);
1493 static const struct got_error *
1494 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1495 const char *path, int diff_context, struct got_repository *repo)
1497 const struct got_error *err = NULL;
1498 struct got_commit_object *pcommit = NULL;
1499 char *id_str1 = NULL, *id_str2 = NULL;
1500 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1501 struct got_object_qid *qid;
1503 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1505 err = got_object_open_as_commit(&pcommit, repo,
1511 if (path && path[0] != '\0') {
1513 err = got_object_id_by_path(&obj_id2, repo, id, path);
1516 err = got_object_id_str(&id_str2, obj_id2);
1522 err = got_object_id_by_path(&obj_id1, repo,
1528 err = got_object_id_str(&id_str1, obj_id1);
1534 err = got_object_get_type(&obj_type, repo, obj_id2);
1539 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1541 case GOT_OBJ_TYPE_BLOB:
1542 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1545 case GOT_OBJ_TYPE_TREE:
1546 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1550 err = got_error(GOT_ERR_OBJ_TYPE);
1556 obj_id2 = got_object_commit_get_tree_id(commit);
1557 err = got_object_id_str(&id_str2, obj_id2);
1560 obj_id1 = got_object_commit_get_tree_id(pcommit);
1561 err = got_object_id_str(&id_str1, obj_id1);
1564 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1565 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1572 got_object_commit_close(pcommit);
1577 get_datestr(time_t *time, char *datebuf)
1579 struct tm mytm, *tm;
1582 tm = gmtime_r(time, &mytm);
1585 s = asctime_r(tm, datebuf);
1588 p = strchr(s, '\n');
1594 static const struct got_error *
1595 match_logmsg(int *have_match, struct got_object_id *id,
1596 struct got_commit_object *commit, regex_t *regex)
1598 const struct got_error *err = NULL;
1599 regmatch_t regmatch;
1600 char *id_str = NULL, *logmsg = NULL;
1604 err = got_object_id_str(&id_str, id);
1608 err = got_object_commit_get_logmsg(&logmsg, commit);
1612 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1620 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1622 static const struct got_error *
1623 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1624 struct got_repository *repo, const char *path, int show_patch,
1625 int diff_context, struct got_reflist_head *refs)
1627 const struct got_error *err = NULL;
1628 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1630 time_t committer_time;
1631 const char *author, *committer;
1632 char *refs_str = NULL;
1633 struct got_reflist_entry *re;
1635 SIMPLEQ_FOREACH(re, refs, entry) {
1638 struct got_tag_object *tag = NULL;
1641 name = got_ref_get_name(re->ref);
1642 if (strcmp(name, GOT_REF_HEAD) == 0)
1644 if (strncmp(name, "refs/", 5) == 0)
1646 if (strncmp(name, "got/", 4) == 0)
1648 if (strncmp(name, "heads/", 6) == 0)
1650 if (strncmp(name, "remotes/", 8) == 0)
1652 if (strncmp(name, "tags/", 5) == 0) {
1653 err = got_object_open_as_tag(&tag, repo, re->id);
1655 if (err->code != GOT_ERR_OBJ_TYPE)
1657 /* Ref points at something other than a tag. */
1662 cmp = got_object_id_cmp(tag ?
1663 got_object_tag_get_object_id(tag) : re->id, id);
1665 got_object_tag_close(tag);
1669 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1671 err = got_error_from_errno("asprintf");
1677 err = got_object_id_str(&id_str, id);
1681 printf(GOT_COMMIT_SEP_STR);
1682 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1683 refs_str ? refs_str : "", refs_str ? ")" : "");
1688 printf("from: %s\n", got_object_commit_get_author(commit));
1689 committer_time = got_object_commit_get_committer_time(commit);
1690 datestr = get_datestr(&committer_time, datebuf);
1692 printf("date: %s UTC\n", datestr);
1693 author = got_object_commit_get_author(commit);
1694 committer = got_object_commit_get_committer(commit);
1695 if (strcmp(author, committer) != 0)
1696 printf("via: %s\n", committer);
1697 if (got_object_commit_get_nparents(commit) > 1) {
1698 const struct got_object_id_queue *parent_ids;
1699 struct got_object_qid *qid;
1701 parent_ids = got_object_commit_get_parent_ids(commit);
1702 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1703 err = got_object_id_str(&id_str, qid->id);
1706 printf("parent %d: %s\n", n++, id_str);
1711 err = got_object_commit_get_logmsg(&logmsg0, commit);
1717 line = strsep(&logmsg, "\n");
1719 printf(" %s\n", line);
1724 err = print_patch(commit, id, path, diff_context, repo);
1729 if (fflush(stdout) != 0 && err == NULL)
1730 err = got_error_from_errno("fflush");
1734 static const struct got_error *
1735 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1736 const char *path, int show_patch, const char *search_pattern,
1737 int diff_context, int limit, int first_parent_traversal,
1738 struct got_reflist_head *refs)
1740 const struct got_error *err;
1741 struct got_commit_graph *graph;
1745 if (search_pattern &&
1746 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1747 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1749 err = got_commit_graph_open(&graph, root_id, path,
1750 first_parent_traversal, repo);
1753 err = got_commit_graph_iter_start(graph, root_id, repo,
1754 check_cancelled, NULL);
1758 struct got_commit_object *commit;
1759 struct got_object_id *id;
1761 if (sigint_received || sigpipe_received)
1764 err = got_commit_graph_iter_next(&id, graph);
1766 if (err->code == GOT_ERR_ITER_COMPLETED) {
1770 if (err->code != GOT_ERR_ITER_NEED_MORE)
1772 err = got_commit_graph_fetch_commits(graph, 1, repo,
1773 check_cancelled, NULL);
1782 err = got_object_open_as_commit(&commit, repo, id);
1786 if (search_pattern) {
1787 err = match_logmsg(&have_match, id, commit, ®ex);
1789 got_object_commit_close(commit);
1792 if (have_match == 0) {
1793 got_object_commit_close(commit);
1798 err = print_commit(commit, id, repo, path, show_patch,
1799 diff_context, refs);
1800 got_object_commit_close(commit);
1801 if (err || (limit && --limit == 0))
1807 got_commit_graph_close(graph);
1814 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1815 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1820 get_default_log_limit(void)
1822 const char *got_default_log_limit;
1826 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1827 if (got_default_log_limit == NULL)
1829 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1835 static const struct got_error *
1836 cmd_log(int argc, char *argv[])
1838 const struct got_error *error;
1839 struct got_repository *repo = NULL;
1840 struct got_worktree *worktree = NULL;
1841 struct got_commit_object *commit = NULL;
1842 struct got_object_id *id = NULL;
1843 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1844 const char *start_commit = NULL, *search_pattern = NULL;
1845 int diff_context = -1, ch;
1846 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1848 struct got_reflist_head refs;
1850 SIMPLEQ_INIT(&refs);
1853 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1859 limit = get_default_log_limit();
1861 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1867 start_commit = optarg;
1870 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1873 err(1, "-C option %s", errstr);
1876 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1878 err(1, "-l option %s", errstr);
1881 first_parent_traversal = 1;
1884 repo_path = realpath(optarg, NULL);
1885 if (repo_path == NULL)
1886 return got_error_from_errno2("realpath",
1888 got_path_strip_trailing_slashes(repo_path);
1891 search_pattern = optarg;
1902 if (diff_context == -1)
1904 else if (!show_patch)
1905 errx(1, "-C reguires -p");
1907 cwd = getcwd(NULL, 0);
1909 error = got_error_from_errno("getcwd");
1913 error = got_worktree_open(&worktree, cwd);
1914 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1921 error = got_error_from_errno("strdup");
1924 } else if (argc == 1) {
1926 error = got_worktree_resolve_path(&path, worktree,
1931 path = strdup(argv[0]);
1933 error = got_error_from_errno("strdup");
1940 if (repo_path == NULL) {
1941 repo_path = worktree ?
1942 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1944 if (repo_path == NULL) {
1945 error = got_error_from_errno("strdup");
1949 error = got_repo_open(&repo, repo_path, NULL);
1953 error = apply_unveil(got_repo_get_path(repo), 1,
1954 worktree ? got_worktree_get_root_path(worktree) : NULL);
1958 if (start_commit == NULL) {
1959 struct got_reference *head_ref;
1960 error = got_ref_open(&head_ref, repo,
1961 worktree ? got_worktree_get_head_ref_name(worktree)
1965 error = got_ref_resolve(&id, repo, head_ref);
1966 got_ref_close(head_ref);
1969 error = got_object_open_as_commit(&commit, repo, id);
1971 struct got_reference *ref;
1972 error = got_ref_open(&ref, repo, start_commit, 0);
1973 if (error == NULL) {
1975 error = got_ref_resolve(&id, repo, ref);
1979 error = got_object_get_type(&obj_type, repo, id);
1982 if (obj_type == GOT_OBJ_TYPE_TAG) {
1983 struct got_tag_object *tag;
1984 error = got_object_open_as_tag(&tag, repo, id);
1987 if (got_object_tag_get_object_type(tag) !=
1988 GOT_OBJ_TYPE_COMMIT) {
1989 got_object_tag_close(tag);
1990 error = got_error(GOT_ERR_OBJ_TYPE);
1994 id = got_object_id_dup(
1995 got_object_tag_get_object_id(tag));
1997 error = got_error_from_errno(
1998 "got_object_id_dup");
1999 got_object_tag_close(tag);
2002 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2003 error = got_error(GOT_ERR_OBJ_TYPE);
2006 error = got_object_open_as_commit(&commit, repo, id);
2010 if (commit == NULL) {
2011 error = got_repo_match_object_id_prefix(&id,
2012 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2021 const char *prefix = got_worktree_get_path_prefix(worktree);
2023 if (asprintf(&p, "%s%s%s", prefix,
2024 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2025 error = got_error_from_errno("asprintf");
2028 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2031 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2036 path = in_repo_path;
2039 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2043 error = print_commits(id, repo, path, show_patch, search_pattern,
2044 diff_context, limit, first_parent_traversal, &refs);
2051 got_worktree_close(worktree);
2053 const struct got_error *repo_error;
2054 repo_error = got_repo_close(repo);
2058 got_ref_list_free(&refs);
2065 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2066 "[-w] [object1 object2 | path]\n", getprogname());
2070 struct print_diff_arg {
2071 struct got_repository *repo;
2072 struct got_worktree *worktree;
2077 int ignore_whitespace;
2080 static const struct got_error *
2081 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2082 const char *path, struct got_object_id *blob_id,
2083 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2084 int dirfd, const char *de_name)
2086 struct print_diff_arg *a = arg;
2087 const struct got_error *err = NULL;
2088 struct got_blob_object *blob1 = NULL;
2091 char *abspath = NULL, *label1 = NULL;
2094 if (a->diff_staged) {
2095 if (staged_status != GOT_STATUS_MODIFY &&
2096 staged_status != GOT_STATUS_ADD &&
2097 staged_status != GOT_STATUS_DELETE)
2100 if (staged_status == GOT_STATUS_DELETE)
2102 if (status == GOT_STATUS_NONEXISTENT)
2103 return got_error_set_errno(ENOENT, path);
2104 if (status != GOT_STATUS_MODIFY &&
2105 status != GOT_STATUS_ADD &&
2106 status != GOT_STATUS_DELETE &&
2107 status != GOT_STATUS_CONFLICT)
2111 if (!a->header_shown) {
2112 printf("diff %s %s%s\n", a->id_str,
2113 got_worktree_get_root_path(a->worktree),
2114 a->diff_staged ? " (staged changes)" : "");
2115 a->header_shown = 1;
2118 if (a->diff_staged) {
2119 const char *label1 = NULL, *label2 = NULL;
2120 switch (staged_status) {
2121 case GOT_STATUS_MODIFY:
2125 case GOT_STATUS_ADD:
2128 case GOT_STATUS_DELETE:
2132 return got_error(GOT_ERR_FILE_STATUS);
2134 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2135 label1, label2, a->diff_context, a->ignore_whitespace,
2139 if (staged_status == GOT_STATUS_ADD ||
2140 staged_status == GOT_STATUS_MODIFY) {
2142 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2146 err = got_object_id_str(&id_str, staged_blob_id);
2149 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2150 err = got_error_from_errno("asprintf");
2155 } else if (status != GOT_STATUS_ADD) {
2156 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2161 if (status != GOT_STATUS_DELETE) {
2162 if (asprintf(&abspath, "%s/%s",
2163 got_worktree_get_root_path(a->worktree), path) == -1) {
2164 err = got_error_from_errno("asprintf");
2169 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2171 err = got_error_from_errno2("openat", abspath);
2175 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2177 err = got_error_from_errno2("open", abspath);
2181 if (fstat(fd, &sb) == -1) {
2182 err = got_error_from_errno2("fstat", abspath);
2185 f2 = fdopen(fd, "r");
2187 err = got_error_from_errno2("fdopen", abspath);
2194 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2195 a->diff_context, a->ignore_whitespace, stdout);
2198 got_object_blob_close(blob1);
2199 if (f2 && fclose(f2) == EOF && err == NULL)
2200 err = got_error_from_errno("fclose");
2201 if (fd != -1 && close(fd) == -1 && err == NULL)
2202 err = got_error_from_errno("close");
2207 static const struct got_error *
2208 match_object_id(struct got_object_id **id, char **label,
2209 const char *id_str, int obj_type, int resolve_tags,
2210 struct got_repository *repo)
2212 const struct got_error *err;
2213 struct got_tag_object *tag;
2214 struct got_reference *ref = NULL;
2220 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2223 *id = got_object_id_dup(
2224 got_object_tag_get_object_id(tag));
2226 err = got_error_from_errno("got_object_id_dup");
2227 else if (asprintf(label, "refs/tags/%s",
2228 got_object_tag_get_name(tag)) == -1) {
2229 err = got_error_from_errno("asprintf");
2233 got_object_tag_close(tag);
2235 } else if (err->code != GOT_ERR_NO_OBJ)
2239 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2241 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2243 err = got_ref_open(&ref, repo, id_str, 0);
2246 *label = strdup(got_ref_get_name(ref));
2247 if (*label == NULL) {
2248 err = got_error_from_errno("strdup");
2251 err = got_ref_resolve(id, repo, ref);
2253 err = got_object_id_str(label, *id);
2254 if (*label == NULL) {
2255 err = got_error_from_errno("strdup");
2266 static const struct got_error *
2267 cmd_diff(int argc, char *argv[])
2269 const struct got_error *error;
2270 struct got_repository *repo = NULL;
2271 struct got_worktree *worktree = NULL;
2272 char *cwd = NULL, *repo_path = NULL;
2273 struct got_object_id *id1 = NULL, *id2 = NULL;
2274 const char *id_str1 = NULL, *id_str2 = NULL;
2275 char *label1 = NULL, *label2 = NULL;
2277 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2287 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2290 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2293 err(1, "-C option %s", errstr);
2296 repo_path = realpath(optarg, NULL);
2297 if (repo_path == NULL)
2298 return got_error_from_errno2("realpath",
2300 got_path_strip_trailing_slashes(repo_path);
2306 ignore_whitespace = 1;
2317 cwd = getcwd(NULL, 0);
2319 error = got_error_from_errno("getcwd");
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2326 if (worktree == NULL) {
2327 error = got_error(GOT_ERR_NOT_WORKTREE);
2332 "-r option can't be used when diffing a work tree");
2333 repo_path = strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL) {
2335 error = got_error_from_errno("strdup");
2339 error = got_worktree_resolve_path(&path, worktree,
2346 error = got_error_from_errno("strdup");
2350 } else if (argc == 2) {
2352 errx(1, "-s option can't be used when diffing "
2353 "objects in repository");
2356 if (worktree && repo_path == NULL) {
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2367 if (repo_path == NULL) {
2368 repo_path = getcwd(NULL, 0);
2369 if (repo_path == NULL)
2370 return got_error_from_errno("getcwd");
2373 error = got_repo_open(&repo, repo_path, NULL);
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 worktree ? got_worktree_get_root_path(worktree) : NULL);
2384 struct print_diff_arg arg;
2385 struct got_pathlist_head paths;
2390 error = got_object_id_str(&id_str,
2391 got_worktree_get_base_commit_id(worktree));
2395 arg.worktree = worktree;
2396 arg.diff_context = diff_context;
2397 arg.id_str = id_str;
2398 arg.header_shown = 0;
2399 arg.diff_staged = diff_staged;
2400 arg.ignore_whitespace = ignore_whitespace;
2402 error = got_pathlist_append(&paths, path, NULL);
2406 error = got_worktree_status(worktree, &paths, repo, print_diff,
2407 &arg, check_cancelled, NULL);
2409 got_pathlist_free(&paths);
2413 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2418 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2423 error = got_object_get_type(&type1, repo, id1);
2427 error = got_object_get_type(&type2, repo, id2);
2431 if (type1 != type2) {
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2437 case GOT_OBJ_TYPE_BLOB:
2438 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2439 diff_context, ignore_whitespace, repo, stdout);
2441 case GOT_OBJ_TYPE_TREE:
2442 error = got_diff_objects_as_trees(id1, id2, "", "",
2443 diff_context, ignore_whitespace, repo, stdout);
2445 case GOT_OBJ_TYPE_COMMIT:
2446 printf("diff %s %s\n", label1, label2);
2447 error = got_diff_objects_as_commits(id1, id2, diff_context,
2448 ignore_whitespace, repo, stdout);
2451 error = got_error(GOT_ERR_OBJ_TYPE);
2461 got_worktree_close(worktree);
2463 const struct got_error *repo_error;
2464 repo_error = got_repo_close(repo);
2475 "usage: %s blame [-c commit] [-r repository-path] path\n",
2484 char datebuf[11]; /* YYYY-MM-DD + NUL */
2487 struct blame_cb_args {
2488 struct blame_line *lines;
2492 off_t *line_offsets;
2494 struct got_repository *repo;
2497 static const struct got_error *
2498 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2500 const struct got_error *err = NULL;
2501 struct blame_cb_args *a = arg;
2502 struct blame_line *bline;
2504 size_t linesize = 0;
2505 struct got_commit_object *commit = NULL;
2508 time_t committer_time;
2510 if (nlines != a->nlines ||
2511 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2512 return got_error(GOT_ERR_RANGE);
2514 if (sigint_received)
2515 return got_error(GOT_ERR_ITER_COMPLETED);
2518 return NULL; /* no change in this commit */
2520 /* Annotate this line. */
2521 bline = &a->lines[lineno - 1];
2522 if (bline->annotated)
2524 err = got_object_id_str(&bline->id_str, id);
2528 err = got_object_open_as_commit(&commit, a->repo, id);
2532 bline->committer = strdup(got_object_commit_get_committer(commit));
2533 if (bline->committer == NULL) {
2534 err = got_error_from_errno("strdup");
2538 committer_time = got_object_commit_get_committer_time(commit);
2539 if (localtime_r(&committer_time, &tm) == NULL)
2540 return got_error_from_errno("localtime_r");
2541 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2542 &tm) >= sizeof(bline->datebuf)) {
2543 err = got_error(GOT_ERR_NO_SPACE);
2546 bline->annotated = 1;
2548 /* Print lines annotated so far. */
2549 bline = &a->lines[a->lineno_cur - 1];
2550 if (!bline->annotated)
2553 offset = a->line_offsets[a->lineno_cur - 1];
2554 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2555 err = got_error_from_errno("fseeko");
2559 while (bline->annotated) {
2560 char *smallerthan, *at, *nl, *committer;
2563 if (getline(&line, &linesize, a->f) == -1) {
2565 err = got_error_from_errno("getline");
2569 committer = bline->committer;
2570 smallerthan = strchr(committer, '<');
2571 if (smallerthan && smallerthan[1] != '\0')
2572 committer = smallerthan + 1;
2573 at = strchr(committer, '@');
2576 len = strlen(committer);
2578 committer[8] = '\0';
2580 nl = strchr(line, '\n');
2583 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2584 bline->id_str, bline->datebuf, committer, line);
2587 bline = &a->lines[a->lineno_cur - 1];
2591 got_object_commit_close(commit);
2596 static const struct got_error *
2597 cmd_blame(int argc, char *argv[])
2599 const struct got_error *error;
2600 struct got_repository *repo = NULL;
2601 struct got_worktree *worktree = NULL;
2602 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2603 struct got_object_id *obj_id = NULL;
2604 struct got_object_id *commit_id = NULL;
2605 struct got_blob_object *blob = NULL;
2606 char *commit_id_str = NULL;
2607 struct blame_cb_args bca;
2608 int ch, obj_type, i;
2611 memset(&bca, 0, sizeof(bca));
2614 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2619 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2622 commit_id_str = optarg;
2625 repo_path = realpath(optarg, NULL);
2626 if (repo_path == NULL)
2627 return got_error_from_errno2("realpath",
2629 got_path_strip_trailing_slashes(repo_path);
2645 cwd = getcwd(NULL, 0);
2647 error = got_error_from_errno("getcwd");
2650 if (repo_path == NULL) {
2651 error = got_worktree_open(&worktree, cwd);
2652 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2658 strdup(got_worktree_get_repo_path(worktree));
2659 if (repo_path == NULL)
2660 error = got_error_from_errno("strdup");
2664 repo_path = strdup(cwd);
2665 if (repo_path == NULL) {
2666 error = got_error_from_errno("strdup");
2672 error = got_repo_open(&repo, repo_path, NULL);
2676 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2681 const char *prefix = got_worktree_get_path_prefix(worktree);
2682 char *p, *worktree_subdir = cwd +
2683 strlen(got_worktree_get_root_path(worktree));
2684 if (asprintf(&p, "%s%s%s%s%s",
2685 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2686 worktree_subdir, worktree_subdir[0] ? "/" : "",
2688 error = got_error_from_errno("asprintf");
2691 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2694 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2699 if (commit_id_str == NULL) {
2700 struct got_reference *head_ref;
2701 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2704 error = got_ref_resolve(&commit_id, repo, head_ref);
2705 got_ref_close(head_ref);
2709 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2714 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2717 if (obj_id == NULL) {
2718 error = got_error(GOT_ERR_NO_OBJ);
2722 error = got_object_get_type(&obj_type, repo, obj_id);
2726 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2727 error = got_error(GOT_ERR_OBJ_TYPE);
2731 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2734 bca.f = got_opentemp();
2735 if (bca.f == NULL) {
2736 error = got_error_from_errno("got_opentemp");
2739 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2740 &bca.line_offsets, bca.f, blob);
2741 if (error || bca.nlines == 0)
2744 /* Don't include \n at EOF in the blame line count. */
2745 if (bca.line_offsets[bca.nlines - 1] == filesize)
2748 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2749 if (bca.lines == NULL) {
2750 error = got_error_from_errno("calloc");
2754 bca.nlines_prec = 0;
2762 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2763 check_cancelled, NULL);
2773 got_object_blob_close(blob);
2775 got_worktree_close(worktree);
2777 const struct got_error *repo_error;
2778 repo_error = got_repo_close(repo);
2783 for (i = 0; i < bca.nlines; i++) {
2784 struct blame_line *bline = &bca.lines[i];
2785 free(bline->id_str);
2786 free(bline->committer);
2790 free(bca.line_offsets);
2791 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2792 error = got_error_from_errno("fclose");
2800 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2806 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2807 const char *root_path)
2809 int is_root_path = (strcmp(path, root_path) == 0);
2810 const char *modestr = "";
2811 mode_t mode = got_tree_entry_get_mode(te);
2813 path += strlen(root_path);
2814 while (path[0] == '/')
2817 if (got_object_tree_entry_is_submodule(te))
2819 else if (S_ISLNK(mode))
2821 else if (S_ISDIR(mode))
2823 else if (mode & S_IXUSR)
2826 printf("%s%s%s%s%s\n", id ? id : "", path,
2827 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2830 static const struct got_error *
2831 print_tree(const char *path, struct got_object_id *commit_id,
2832 int show_ids, int recurse, const char *root_path,
2833 struct got_repository *repo)
2835 const struct got_error *err = NULL;
2836 struct got_object_id *tree_id = NULL;
2837 struct got_tree_object *tree = NULL;
2840 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2844 err = got_object_open_as_tree(&tree, repo, tree_id);
2847 nentries = got_object_tree_get_nentries(tree);
2848 for (i = 0; i < nentries; i++) {
2849 struct got_tree_entry *te;
2852 if (sigint_received || sigpipe_received)
2855 te = got_object_tree_get_entry(tree, i);
2858 err = got_object_id_str(&id_str,
2859 got_tree_entry_get_id(te));
2862 if (asprintf(&id, "%s ", id_str) == -1) {
2863 err = got_error_from_errno("asprintf");
2869 print_entry(te, id, path, root_path);
2872 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2874 if (asprintf(&child_path, "%s%s%s", path,
2875 path[0] == '/' && path[1] == '\0' ? "" : "/",
2876 got_tree_entry_get_name(te)) == -1) {
2877 err = got_error_from_errno("asprintf");
2880 err = print_tree(child_path, commit_id, show_ids, 1,
2889 got_object_tree_close(tree);
2894 static const struct got_error *
2895 cmd_tree(int argc, char *argv[])
2897 const struct got_error *error;
2898 struct got_repository *repo = NULL;
2899 struct got_worktree *worktree = NULL;
2901 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2902 struct got_object_id *commit_id = NULL;
2903 char *commit_id_str = NULL;
2904 int show_ids = 0, recurse = 0;
2908 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2913 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2916 commit_id_str = optarg;
2919 repo_path = realpath(optarg, NULL);
2920 if (repo_path == NULL)
2921 return got_error_from_errno2("realpath",
2923 got_path_strip_trailing_slashes(repo_path);
2947 cwd = getcwd(NULL, 0);
2949 error = got_error_from_errno("getcwd");
2952 if (repo_path == NULL) {
2953 error = got_worktree_open(&worktree, cwd);
2954 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2960 strdup(got_worktree_get_repo_path(worktree));
2961 if (repo_path == NULL)
2962 error = got_error_from_errno("strdup");
2966 repo_path = strdup(cwd);
2967 if (repo_path == NULL) {
2968 error = got_error_from_errno("strdup");
2974 error = got_repo_open(&repo, repo_path, NULL);
2978 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2984 char *p, *worktree_subdir = cwd +
2985 strlen(got_worktree_get_root_path(worktree));
2986 if (asprintf(&p, "%s/%s",
2987 got_worktree_get_path_prefix(worktree),
2988 worktree_subdir) == -1) {
2989 error = got_error_from_errno("asprintf");
2992 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2999 if (in_repo_path == NULL) {
3000 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3005 if (commit_id_str == NULL) {
3006 struct got_reference *head_ref;
3007 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3010 error = got_ref_resolve(&commit_id, repo, head_ref);
3011 got_ref_close(head_ref);
3015 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3020 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3021 in_repo_path, repo);
3028 got_worktree_close(worktree);
3030 const struct got_error *repo_error;
3031 repo_error = got_repo_close(repo);
3041 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3045 static const struct got_error *
3046 print_status(void *arg, unsigned char status, unsigned char staged_status,
3047 const char *path, struct got_object_id *blob_id,
3048 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3049 int dirfd, const char *de_name)
3051 if (status == staged_status && (status == GOT_STATUS_DELETE))
3052 status = GOT_STATUS_NO_CHANGE;
3053 printf("%c%c %s\n", status, staged_status, path);
3057 static const struct got_error *
3058 cmd_status(int argc, char *argv[])
3060 const struct got_error *error = NULL;
3061 struct got_repository *repo = NULL;
3062 struct got_worktree *worktree = NULL;
3064 struct got_pathlist_head paths;
3065 struct got_pathlist_entry *pe;
3070 while ((ch = getopt(argc, argv, "")) != -1) {
3082 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3086 cwd = getcwd(NULL, 0);
3088 error = got_error_from_errno("getcwd");
3092 error = got_worktree_open(&worktree, cwd);
3096 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3101 error = apply_unveil(got_repo_get_path(repo), 1,
3102 got_worktree_get_root_path(worktree));
3106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3110 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3111 check_cancelled, NULL);
3113 TAILQ_FOREACH(pe, &paths, entry)
3114 free((char *)pe->path);
3115 got_pathlist_free(&paths);
3124 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3129 static const struct got_error *
3130 list_refs(struct got_repository *repo)
3132 static const struct got_error *err = NULL;
3133 struct got_reflist_head refs;
3134 struct got_reflist_entry *re;
3136 SIMPLEQ_INIT(&refs);
3137 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3141 SIMPLEQ_FOREACH(re, &refs, entry) {
3143 refstr = got_ref_to_str(re->ref);
3145 return got_error_from_errno("got_ref_to_str");
3146 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3150 got_ref_list_free(&refs);
3154 static const struct got_error *
3155 delete_ref(struct got_repository *repo, const char *refname)
3157 const struct got_error *err = NULL;
3158 struct got_reference *ref;
3160 err = got_ref_open(&ref, repo, refname, 0);
3164 err = got_ref_delete(ref, repo);
3169 static const struct got_error *
3170 add_ref(struct got_repository *repo, const char *refname, const char *target)
3172 const struct got_error *err = NULL;
3173 struct got_object_id *id;
3174 struct got_reference *ref = NULL;
3177 * Don't let the user create a reference name with a leading '-'.
3178 * While technically a valid reference name, this case is usually
3179 * an unintended typo.
3181 if (refname[0] == '-')
3182 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3184 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3187 struct got_reference *target_ref;
3189 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3191 err = got_ref_open(&target_ref, repo, target, 0);
3194 err = got_ref_resolve(&id, repo, target_ref);
3195 got_ref_close(target_ref);
3200 err = got_ref_alloc(&ref, refname, id);
3204 err = got_ref_write(ref, repo);
3212 static const struct got_error *
3213 add_symref(struct got_repository *repo, const char *refname, const char *target)
3215 const struct got_error *err = NULL;
3216 struct got_reference *ref = NULL;
3217 struct got_reference *target_ref = NULL;
3220 * Don't let the user create a reference name with a leading '-'.
3221 * While technically a valid reference name, this case is usually
3222 * an unintended typo.
3224 if (refname[0] == '-')
3225 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3227 err = got_ref_open(&target_ref, repo, target, 0);
3231 err = got_ref_alloc_symref(&ref, refname, target_ref);
3235 err = got_ref_write(ref, repo);
3238 got_ref_close(target_ref);
3244 static const struct got_error *
3245 cmd_ref(int argc, char *argv[])
3247 const struct got_error *error = NULL;
3248 struct got_repository *repo = NULL;
3249 struct got_worktree *worktree = NULL;
3250 char *cwd = NULL, *repo_path = NULL;
3251 int ch, do_list = 0, create_symref = 0;
3252 const char *delref = NULL;
3254 /* TODO: Add -s option for adding symbolic references. */
3255 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3261 repo_path = realpath(optarg, NULL);
3262 if (repo_path == NULL)
3263 return got_error_from_errno2("realpath",
3265 got_path_strip_trailing_slashes(repo_path);
3279 if (do_list && delref)
3280 errx(1, "-l and -d options are mutually exclusive\n");
3285 if (do_list || delref) {
3287 errx(1, "-s option cannot be used together with the "
3288 "-l or -d options");
3291 } else if (argc != 2)
3296 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3300 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3301 "sendfd unveil", NULL) == -1)
3305 cwd = getcwd(NULL, 0);
3307 error = got_error_from_errno("getcwd");
3311 if (repo_path == NULL) {
3312 error = got_worktree_open(&worktree, cwd);
3313 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3319 strdup(got_worktree_get_repo_path(worktree));
3320 if (repo_path == NULL)
3321 error = got_error_from_errno("strdup");
3325 repo_path = strdup(cwd);
3326 if (repo_path == NULL) {
3327 error = got_error_from_errno("strdup");
3333 error = got_repo_open(&repo, repo_path, NULL);
3337 error = apply_unveil(got_repo_get_path(repo), do_list,
3338 worktree ? got_worktree_get_root_path(worktree) : NULL);
3343 error = list_refs(repo);
3345 error = delete_ref(repo, delref);
3346 else if (create_symref)
3347 error = add_symref(repo, argv[0], argv[1]);
3349 error = add_ref(repo, argv[0], argv[1]);
3352 got_repo_close(repo);
3354 got_worktree_close(worktree);
3364 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3365 "[name]\n", getprogname());
3369 static const struct got_error *
3370 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3371 struct got_reference *ref)
3373 const struct got_error *err = NULL;
3374 const char *refname, *marker = " ";
3377 refname = got_ref_get_name(ref);
3378 if (worktree && strcmp(refname,
3379 got_worktree_get_head_ref_name(worktree)) == 0) {
3380 struct got_object_id *id = NULL;
3382 err = got_ref_resolve(&id, repo, ref);
3385 if (got_object_id_cmp(id,
3386 got_worktree_get_base_commit_id(worktree)) == 0)
3393 if (strncmp(refname, "refs/heads/", 11) == 0)
3395 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3398 refstr = got_ref_to_str(ref);
3400 return got_error_from_errno("got_ref_to_str");
3402 printf("%s%s: %s\n", marker, refname, refstr);
3407 static const struct got_error *
3408 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3410 const char *refname;
3412 if (worktree == NULL)
3413 return got_error(GOT_ERR_NOT_WORKTREE);
3415 refname = got_worktree_get_head_ref_name(worktree);
3417 if (strncmp(refname, "refs/heads/", 11) == 0)
3419 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3422 printf("%s\n", refname);
3427 static const struct got_error *
3428 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3430 static const struct got_error *err = NULL;
3431 struct got_reflist_head refs;
3432 struct got_reflist_entry *re;
3433 struct got_reference *temp_ref = NULL;
3434 int rebase_in_progress, histedit_in_progress;
3436 SIMPLEQ_INIT(&refs);
3439 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3444 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3449 if (rebase_in_progress || histedit_in_progress) {
3450 err = got_ref_open(&temp_ref, repo,
3451 got_worktree_get_head_ref_name(worktree), 0);
3454 list_branch(repo, worktree, temp_ref);
3455 got_ref_close(temp_ref);
3459 err = got_ref_list(&refs, repo, "refs/heads",
3460 got_ref_cmp_by_name, NULL);
3464 SIMPLEQ_FOREACH(re, &refs, entry)
3465 list_branch(repo, worktree, re->ref);
3467 got_ref_list_free(&refs);
3471 static const struct got_error *
3472 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3473 const char *branch_name)
3475 const struct got_error *err = NULL;
3476 struct got_reference *ref = NULL;
3479 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3480 return got_error_from_errno("asprintf");
3482 err = got_ref_open(&ref, repo, refname, 0);
3487 strcmp(got_worktree_get_head_ref_name(worktree),
3488 got_ref_get_name(ref)) == 0) {
3489 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3490 "will not delete this work tree's current branch");
3494 err = got_ref_delete(ref, repo);
3502 static const struct got_error *
3503 add_branch(struct got_repository *repo, const char *branch_name,
3504 struct got_object_id *base_commit_id)
3506 const struct got_error *err = NULL;
3507 struct got_reference *ref = NULL;
3508 char *base_refname = NULL, *refname = NULL;
3511 * Don't let the user create a branch name with a leading '-'.
3512 * While technically a valid reference name, this case is usually
3513 * an unintended typo.
3515 if (branch_name[0] == '-')
3516 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3518 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3519 err = got_error_from_errno("asprintf");
3523 err = got_ref_open(&ref, repo, refname, 0);
3525 err = got_error(GOT_ERR_BRANCH_EXISTS);
3527 } else if (err->code != GOT_ERR_NOT_REF)
3530 err = got_ref_alloc(&ref, refname, base_commit_id);
3534 err = got_ref_write(ref, repo);
3543 static const struct got_error *
3544 cmd_branch(int argc, char *argv[])
3546 const struct got_error *error = NULL;
3547 struct got_repository *repo = NULL;
3548 struct got_worktree *worktree = NULL;
3549 char *cwd = NULL, *repo_path = NULL;
3550 int ch, do_list = 0, do_show = 0;
3551 const char *delref = NULL, *commit_id_arg = NULL;
3553 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3556 commit_id_arg = optarg;
3562 repo_path = realpath(optarg, NULL);
3563 if (repo_path == NULL)
3564 return got_error_from_errno2("realpath",
3566 got_path_strip_trailing_slashes(repo_path);
3577 if (do_list && delref)
3578 errx(1, "-l and -d options are mutually exclusive\n");
3583 if (!do_list && !delref && argc == 0)
3586 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3587 errx(1, "-c option can only be used when creating a branch");
3589 if (do_list || delref) {
3592 } else if (!do_show && argc != 1)
3596 if (do_list || do_show) {
3597 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3601 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3602 "sendfd unveil", NULL) == -1)
3606 cwd = getcwd(NULL, 0);
3608 error = got_error_from_errno("getcwd");
3612 if (repo_path == NULL) {
3613 error = got_worktree_open(&worktree, cwd);
3614 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3620 strdup(got_worktree_get_repo_path(worktree));
3621 if (repo_path == NULL)
3622 error = got_error_from_errno("strdup");
3626 repo_path = strdup(cwd);
3627 if (repo_path == NULL) {
3628 error = got_error_from_errno("strdup");
3634 error = got_repo_open(&repo, repo_path, NULL);
3638 error = apply_unveil(got_repo_get_path(repo), do_list,
3639 worktree ? got_worktree_get_root_path(worktree) : NULL);
3644 error = show_current_branch(repo, worktree);
3646 error = list_branches(repo, worktree);
3648 error = delete_branch(repo, worktree, delref);
3650 struct got_object_id *commit_id;
3651 if (commit_id_arg == NULL)
3652 commit_id_arg = worktree ?
3653 got_worktree_get_head_ref_name(worktree) :
3655 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3658 error = add_branch(repo, argv[0], commit_id);
3663 got_repo_close(repo);
3665 got_worktree_close(worktree);
3676 "usage: %s tag [-r repository] | -l | "
3677 "[-m message] name [commit]\n", getprogname());
3682 static const struct got_error *
3683 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3685 const struct got_error *err = NULL;
3686 struct got_reflist_entry *re, *se, *new;
3687 struct got_object_id *re_id, *se_id;
3688 struct got_tag_object *re_tag, *se_tag;
3689 time_t re_time, se_time;
3691 SIMPLEQ_FOREACH(re, tags, entry) {
3692 se = SIMPLEQ_FIRST(sorted);
3694 err = got_reflist_entry_dup(&new, re);
3697 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3700 err = got_ref_resolve(&re_id, repo, re->ref);
3703 err = got_object_open_as_tag(&re_tag, repo, re_id);
3707 re_time = got_object_tag_get_tagger_time(re_tag);
3708 got_object_tag_close(re_tag);
3712 err = got_ref_resolve(&se_id, repo, re->ref);
3715 err = got_object_open_as_tag(&se_tag, repo, se_id);
3719 se_time = got_object_tag_get_tagger_time(se_tag);
3720 got_object_tag_close(se_tag);
3722 if (se_time > re_time) {
3723 err = got_reflist_entry_dup(&new, re);
3726 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3729 se = SIMPLEQ_NEXT(se, entry);
3738 static const struct got_error *
3739 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3740 struct got_reference *ref2)
3742 const struct got_error *err = NULL;
3743 struct got_repository *repo = arg;
3744 struct got_object_id *id1, *id2 = NULL;
3745 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3746 time_t time1, time2;
3750 err = got_ref_resolve(&id1, repo, ref1);
3753 err = got_object_open_as_tag(&tag1, repo, id1);
3757 err = got_ref_resolve(&id2, repo, ref2);
3760 err = got_object_open_as_tag(&tag2, repo, id2);
3764 time1 = got_object_tag_get_tagger_time(tag1);
3765 time2 = got_object_tag_get_tagger_time(tag2);
3767 /* Put latest tags first. */
3770 else if (time1 > time2)
3773 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3778 got_object_tag_close(tag1);
3780 got_object_tag_close(tag2);
3784 static const struct got_error *
3785 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3787 static const struct got_error *err = NULL;
3788 struct got_reflist_head refs;
3789 struct got_reflist_entry *re;
3791 SIMPLEQ_INIT(&refs);
3793 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3797 SIMPLEQ_FOREACH(re, &refs, entry) {
3798 const char *refname;
3799 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3802 struct got_object_id *id;
3803 struct got_tag_object *tag;
3805 refname = got_ref_get_name(re->ref);
3806 if (strncmp(refname, "refs/tags/", 10) != 0)
3809 refstr = got_ref_to_str(re->ref);
3810 if (refstr == NULL) {
3811 err = got_error_from_errno("got_ref_to_str");
3814 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3817 err = got_ref_resolve(&id, repo, re->ref);
3820 err = got_object_open_as_tag(&tag, repo, id);
3824 printf("from: %s\n", got_object_tag_get_tagger(tag));
3825 tagger_time = got_object_tag_get_tagger_time(tag);
3826 datestr = get_datestr(&tagger_time, datebuf);
3828 printf("date: %s UTC\n", datestr);
3829 err = got_object_id_str(&id_str,
3830 got_object_tag_get_object_id(tag));
3833 switch (got_object_tag_get_object_type(tag)) {
3834 case GOT_OBJ_TYPE_BLOB:
3835 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3837 case GOT_OBJ_TYPE_TREE:
3838 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3840 case GOT_OBJ_TYPE_COMMIT:
3841 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3843 case GOT_OBJ_TYPE_TAG:
3844 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3850 tagmsg0 = strdup(got_object_tag_get_message(tag));
3851 got_object_tag_close(tag);
3852 if (tagmsg0 == NULL) {
3853 err = got_error_from_errno("strdup");
3859 line = strsep(&tagmsg, "\n");
3861 printf(" %s\n", line);
3866 got_ref_list_free(&refs);
3870 static const struct got_error *
3871 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3872 const char *tag_name, const char *repo_path)
3874 const struct got_error *err = NULL;
3875 char *template = NULL, *initial_content = NULL;
3876 char *editor = NULL;
3879 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3880 err = got_error_from_errno("asprintf");
3884 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3885 commit_id_str, tag_name) == -1) {
3886 err = got_error_from_errno("asprintf");
3890 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3894 dprintf(fd, initial_content);
3897 err = get_editor(&editor);
3900 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3902 free(initial_content);
3906 /* Editor is done; we can now apply unveil(2) */
3908 err = apply_unveil(repo_path, 0, NULL);
3917 static const struct got_error *
3918 add_tag(struct got_repository *repo, const char *tag_name,
3919 const char *commit_arg, const char *tagmsg_arg)
3921 const struct got_error *err = NULL;
3922 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3923 char *label = NULL, *commit_id_str = NULL;
3924 struct got_reference *ref = NULL;
3925 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3926 char *tagmsg_path = NULL, *tag_id_str = NULL;
3927 int preserve_tagmsg = 0;
3930 * Don't let the user create a tag name with a leading '-'.
3931 * While technically a valid reference name, this case is usually
3932 * an unintended typo.
3934 if (tag_name[0] == '-')
3935 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3937 err = get_author(&tagger, repo);
3941 err = match_object_id(&commit_id, &label, commit_arg,
3942 GOT_OBJ_TYPE_COMMIT, 1, repo);
3946 err = got_object_id_str(&commit_id_str, commit_id);
3950 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3951 refname = strdup(tag_name);
3952 if (refname == NULL) {
3953 err = got_error_from_errno("strdup");
3957 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3958 err = got_error_from_errno("asprintf");
3962 err = got_ref_open(&ref, repo, refname, 0);
3964 err = got_error(GOT_ERR_TAG_EXISTS);
3966 } else if (err->code != GOT_ERR_NOT_REF)
3969 if (tagmsg_arg == NULL) {
3970 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3971 tag_name, got_repo_get_path(repo));
3973 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3974 tagmsg_path != NULL)
3975 preserve_tagmsg = 1;
3980 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3981 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3984 preserve_tagmsg = 1;
3988 err = got_ref_alloc(&ref, refname, tag_id);
3991 preserve_tagmsg = 1;
3995 err = got_ref_write(ref, repo);
3998 preserve_tagmsg = 1;
4002 err = got_object_id_str(&tag_id_str, tag_id);
4005 preserve_tagmsg = 1;
4008 printf("Created tag %s\n", tag_id_str);
4010 if (preserve_tagmsg) {
4011 fprintf(stderr, "%s: tag message preserved in %s\n",
4012 getprogname(), tagmsg_path);
4013 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4014 err = got_error_from_errno2("unlink", tagmsg_path);
4019 free(commit_id_str);
4027 static const struct got_error *
4028 cmd_tag(int argc, char *argv[])
4030 const struct got_error *error = NULL;
4031 struct got_repository *repo = NULL;
4032 struct got_worktree *worktree = NULL;
4033 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4034 char *gitconfig_path = NULL;
4035 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4036 int ch, do_list = 0;
4038 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4044 repo_path = realpath(optarg, NULL);
4045 if (repo_path == NULL)
4046 return got_error_from_errno2("realpath",
4048 got_path_strip_trailing_slashes(repo_path);
4064 errx(1, "-l and -m options are mutually exclusive\n");
4067 } else if (argc < 1 || argc > 2)
4070 commit_id_arg = argv[1];
4075 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4079 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4080 "sendfd unveil", NULL) == -1)
4084 cwd = getcwd(NULL, 0);
4086 error = got_error_from_errno("getcwd");
4090 if (repo_path == NULL) {
4091 error = got_worktree_open(&worktree, cwd);
4092 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4098 strdup(got_worktree_get_repo_path(worktree));
4099 if (repo_path == NULL)
4100 error = got_error_from_errno("strdup");
4104 repo_path = strdup(cwd);
4105 if (repo_path == NULL) {
4106 error = got_error_from_errno("strdup");
4113 error = got_repo_open(&repo, repo_path, NULL);
4116 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4119 error = list_tags(repo, worktree);
4121 error = get_gitconfig_path(&gitconfig_path);
4124 error = got_repo_open(&repo, repo_path, gitconfig_path);
4129 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4134 if (commit_id_arg == NULL) {
4135 struct got_reference *head_ref;
4136 struct got_object_id *commit_id;
4137 error = got_ref_open(&head_ref, repo,
4138 worktree ? got_worktree_get_head_ref_name(worktree)
4142 error = got_ref_resolve(&commit_id, repo, head_ref);
4143 got_ref_close(head_ref);
4146 error = got_object_id_str(&commit_id_str, commit_id);
4152 error = add_tag(repo, tag_name,
4153 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4157 got_repo_close(repo);
4159 got_worktree_close(worktree);
4162 free(gitconfig_path);
4163 free(commit_id_str);
4170 fprintf(stderr, "usage: %s add [-R] [-I] file-path ...\n",
4175 static const struct got_error *
4176 add_progress(void *arg, unsigned char status, const char *path)
4178 while (path[0] == '/')
4180 printf("%c %s\n", status, path);
4184 static const struct got_error *
4185 cmd_add(int argc, char *argv[])
4187 const struct got_error *error = NULL;
4188 struct got_repository *repo = NULL;
4189 struct got_worktree *worktree = NULL;
4191 struct got_pathlist_head paths;
4192 struct got_pathlist_entry *pe;
4193 int ch, can_recurse = 0, no_ignores = 0;
4197 while ((ch = getopt(argc, argv, "IR")) != -1) {
4215 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4222 cwd = getcwd(NULL, 0);
4224 error = got_error_from_errno("getcwd");
4228 error = got_worktree_open(&worktree, cwd);
4232 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4237 error = apply_unveil(got_repo_get_path(repo), 1,
4238 got_worktree_get_root_path(worktree));
4242 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4246 if (!can_recurse && no_ignores) {
4247 error = got_error_msg(GOT_ERR_BAD_PATH,
4248 "disregarding ignores requires -R option");
4256 TAILQ_FOREACH(pe, &paths, entry) {
4257 if (asprintf(&ondisk_path, "%s/%s",
4258 got_worktree_get_root_path(worktree),
4260 error = got_error_from_errno("asprintf");
4263 if (lstat(ondisk_path, &sb) == -1) {
4264 if (errno == ENOENT) {
4268 error = got_error_from_errno2("lstat",
4274 if (S_ISDIR(sb.st_mode)) {
4275 error = got_error_msg(GOT_ERR_BAD_PATH,
4276 "adding directories requires -R option");
4282 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4283 NULL, repo, no_ignores);
4286 got_repo_close(repo);
4288 got_worktree_close(worktree);
4289 TAILQ_FOREACH(pe, &paths, entry)
4290 free((char *)pe->path);
4291 got_pathlist_free(&paths);
4299 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] file-path ...\n",
4304 static const struct got_error *
4305 print_remove_status(void *arg, unsigned char status,
4306 unsigned char staged_status, const char *path)
4308 while (path[0] == '/')
4310 if (status == GOT_STATUS_NONEXISTENT)
4312 if (status == staged_status && (status == GOT_STATUS_DELETE))
4313 status = GOT_STATUS_NO_CHANGE;
4314 printf("%c%c %s\n", status, staged_status, path);
4318 static const struct got_error *
4319 cmd_remove(int argc, char *argv[])
4321 const struct got_error *error = NULL;
4322 struct got_worktree *worktree = NULL;
4323 struct got_repository *repo = NULL;
4325 struct got_pathlist_head paths;
4326 struct got_pathlist_entry *pe;
4327 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4331 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4334 delete_local_mods = 1;
4352 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4359 cwd = getcwd(NULL, 0);
4361 error = got_error_from_errno("getcwd");
4364 error = got_worktree_open(&worktree, cwd);
4368 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4373 error = apply_unveil(got_repo_get_path(repo), 1,
4374 got_worktree_get_root_path(worktree));
4378 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4385 TAILQ_FOREACH(pe, &paths, entry) {
4386 if (asprintf(&ondisk_path, "%s/%s",
4387 got_worktree_get_root_path(worktree),
4389 error = got_error_from_errno("asprintf");
4392 if (lstat(ondisk_path, &sb) == -1) {
4393 if (errno == ENOENT) {
4397 error = got_error_from_errno2("lstat",
4403 if (S_ISDIR(sb.st_mode)) {
4404 error = got_error_msg(GOT_ERR_BAD_PATH,
4405 "removing directories requires -R option");
4411 error = got_worktree_schedule_delete(worktree, &paths,
4412 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4417 got_repo_close(repo);
4419 got_worktree_close(worktree);
4420 TAILQ_FOREACH(pe, &paths, entry)
4421 free((char *)pe->path);
4422 got_pathlist_free(&paths);
4430 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4431 "path ...\n", getprogname());
4435 static const struct got_error *
4436 revert_progress(void *arg, unsigned char status, const char *path)
4438 while (path[0] == '/')
4440 printf("%c %s\n", status, path);
4444 struct choose_patch_arg {
4445 FILE *patch_script_file;
4449 static const struct got_error *
4450 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4451 int nchanges, const char *action)
4454 size_t linesize = 0;
4458 case GOT_STATUS_ADD:
4459 printf("A %s\n%s this addition? [y/n] ", path, action);
4461 case GOT_STATUS_DELETE:
4462 printf("D %s\n%s this deletion? [y/n] ", path, action);
4464 case GOT_STATUS_MODIFY:
4465 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4466 return got_error_from_errno("fseek");
4467 printf(GOT_COMMIT_SEP_STR);
4468 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4470 if (ferror(patch_file))
4471 return got_error_from_errno("getline");
4472 printf(GOT_COMMIT_SEP_STR);
4473 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4474 path, n, nchanges, action);
4477 return got_error_path(path, GOT_ERR_FILE_STATUS);
4483 static const struct got_error *
4484 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4485 FILE *patch_file, int n, int nchanges)
4487 const struct got_error *err = NULL;
4489 size_t linesize = 0;
4492 struct choose_patch_arg *a = arg;
4494 *choice = GOT_PATCH_CHOICE_NONE;
4496 if (a->patch_script_file) {
4498 err = show_change(status, path, patch_file, n, nchanges,
4502 linelen = getline(&line, &linesize, a->patch_script_file);
4503 if (linelen == -1) {
4504 if (ferror(a->patch_script_file))
4505 return got_error_from_errno("getline");
4508 nl = strchr(line, '\n');
4511 if (strcmp(line, "y") == 0) {
4512 *choice = GOT_PATCH_CHOICE_YES;
4514 } else if (strcmp(line, "n") == 0) {
4515 *choice = GOT_PATCH_CHOICE_NO;
4517 } else if (strcmp(line, "q") == 0 &&
4518 status == GOT_STATUS_MODIFY) {
4519 *choice = GOT_PATCH_CHOICE_QUIT;
4522 printf("invalid response '%s'\n", line);
4527 while (resp != 'y' && resp != 'n' && resp != 'q') {
4528 err = show_change(status, path, patch_file, n, nchanges,
4535 if (status == GOT_STATUS_MODIFY) {
4536 if (resp != 'y' && resp != 'n' && resp != 'q') {
4537 printf("invalid response '%c'\n", resp);
4540 } else if (resp != 'y' && resp != 'n') {
4541 printf("invalid response '%c'\n", resp);
4547 *choice = GOT_PATCH_CHOICE_YES;
4548 else if (resp == 'n')
4549 *choice = GOT_PATCH_CHOICE_NO;
4550 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4551 *choice = GOT_PATCH_CHOICE_QUIT;
4557 static const struct got_error *
4558 cmd_revert(int argc, char *argv[])
4560 const struct got_error *error = NULL;
4561 struct got_worktree *worktree = NULL;
4562 struct got_repository *repo = NULL;
4563 char *cwd = NULL, *path = NULL;
4564 struct got_pathlist_head paths;
4565 struct got_pathlist_entry *pe;
4566 int ch, can_recurse = 0, pflag = 0;
4567 FILE *patch_script_file = NULL;
4568 const char *patch_script_path = NULL;
4569 struct choose_patch_arg cpa;
4573 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4579 patch_script_path = optarg;
4594 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4595 "unveil", NULL) == -1)
4600 if (patch_script_path && !pflag)
4601 errx(1, "-F option can only be used together with -p option");
4603 cwd = getcwd(NULL, 0);
4605 error = got_error_from_errno("getcwd");
4608 error = got_worktree_open(&worktree, cwd);
4612 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4617 if (patch_script_path) {
4618 patch_script_file = fopen(patch_script_path, "r");
4619 if (patch_script_file == NULL) {
4620 error = got_error_from_errno2("fopen",
4625 error = apply_unveil(got_repo_get_path(repo), 1,
4626 got_worktree_get_root_path(worktree));
4630 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4637 TAILQ_FOREACH(pe, &paths, entry) {
4638 if (asprintf(&ondisk_path, "%s/%s",
4639 got_worktree_get_root_path(worktree),
4641 error = got_error_from_errno("asprintf");
4644 if (lstat(ondisk_path, &sb) == -1) {
4645 if (errno == ENOENT) {
4649 error = got_error_from_errno2("lstat",
4655 if (S_ISDIR(sb.st_mode)) {
4656 error = got_error_msg(GOT_ERR_BAD_PATH,
4657 "reverting directories requires -R option");
4663 cpa.patch_script_file = patch_script_file;
4664 cpa.action = "revert";
4665 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4666 pflag ? choose_patch : NULL, &cpa, repo);
4670 if (patch_script_file && fclose(patch_script_file) == EOF &&
4672 error = got_error_from_errno2("fclose", patch_script_path);
4674 got_repo_close(repo);
4676 got_worktree_close(worktree);
4685 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4690 struct collect_commit_logmsg_arg {
4691 const char *cmdline_log;
4693 const char *worktree_path;
4694 const char *branch_name;
4695 const char *repo_path;
4700 static const struct got_error *
4701 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4704 char *initial_content = NULL;
4705 struct got_pathlist_entry *pe;
4706 const struct got_error *err = NULL;
4707 char *template = NULL;
4708 struct collect_commit_logmsg_arg *a = arg;
4712 /* if a message was specified on the command line, just use it */
4713 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4714 len = strlen(a->cmdline_log) + 1;
4715 *logmsg = malloc(len + 1);
4716 if (*logmsg == NULL)
4717 return got_error_from_errno("malloc");
4718 strlcpy(*logmsg, a->cmdline_log, len);
4722 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4723 return got_error_from_errno("asprintf");
4725 if (asprintf(&initial_content,
4726 "\n# changes to be committed on branch %s:\n",
4727 a->branch_name) == -1)
4728 return got_error_from_errno("asprintf");
4730 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4734 dprintf(fd, initial_content);
4736 TAILQ_FOREACH(pe, commitable_paths, entry) {
4737 struct got_commitable *ct = pe->data;
4738 dprintf(fd, "# %c %s\n",
4739 got_commitable_get_status(ct),
4740 got_commitable_get_path(ct));
4744 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4746 free(initial_content);
4749 /* Editor is done; we can now apply unveil(2) */
4751 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4760 static const struct got_error *
4761 cmd_commit(int argc, char *argv[])
4763 const struct got_error *error = NULL;
4764 struct got_worktree *worktree = NULL;
4765 struct got_repository *repo = NULL;
4766 char *cwd = NULL, *id_str = NULL;
4767 struct got_object_id *id = NULL;
4768 const char *logmsg = NULL;
4769 struct collect_commit_logmsg_arg cl_arg;
4770 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4771 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4772 struct got_pathlist_head paths;
4775 cl_arg.logmsg_path = NULL;
4777 while ((ch = getopt(argc, argv, "m:")) != -1) {
4792 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4793 "unveil", NULL) == -1)
4796 cwd = getcwd(NULL, 0);
4798 error = got_error_from_errno("getcwd");
4801 error = got_worktree_open(&worktree, cwd);
4805 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4808 if (rebase_in_progress) {
4809 error = got_error(GOT_ERR_REBASING);
4813 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4818 error = get_gitconfig_path(&gitconfig_path);
4821 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4826 error = get_author(&author, repo);
4831 * unveil(2) traverses exec(2); if an editor is used we have
4832 * to apply unveil after the log message has been written.
4834 if (logmsg == NULL || strlen(logmsg) == 0)
4835 error = get_editor(&editor);
4837 error = apply_unveil(got_repo_get_path(repo), 0,
4838 got_worktree_get_root_path(worktree));
4842 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4846 cl_arg.editor = editor;
4847 cl_arg.cmdline_log = logmsg;
4848 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4849 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4850 if (!histedit_in_progress) {
4851 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4852 error = got_error(GOT_ERR_COMMIT_BRANCH);
4855 cl_arg.branch_name += 11;
4857 cl_arg.repo_path = got_repo_get_path(repo);
4858 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4859 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4861 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4862 cl_arg.logmsg_path != NULL)
4863 preserve_logmsg = 1;
4867 error = got_object_id_str(&id_str, id);
4870 printf("Created commit %s\n", id_str);
4872 if (preserve_logmsg) {
4873 fprintf(stderr, "%s: log message preserved in %s\n",
4874 getprogname(), cl_arg.logmsg_path);
4875 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4877 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4878 free(cl_arg.logmsg_path);
4880 got_repo_close(repo);
4882 got_worktree_close(worktree);
4885 free(gitconfig_path);
4892 usage_cherrypick(void)
4894 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4898 static const struct got_error *
4899 cmd_cherrypick(int argc, char *argv[])
4901 const struct got_error *error = NULL;
4902 struct got_worktree *worktree = NULL;
4903 struct got_repository *repo = NULL;
4904 char *cwd = NULL, *commit_id_str = NULL;
4905 struct got_object_id *commit_id = NULL;
4906 struct got_commit_object *commit = NULL;
4907 struct got_object_qid *pid;
4908 struct got_reference *head_ref = NULL;
4909 int ch, did_something = 0;
4911 while ((ch = getopt(argc, argv, "")) != -1) {
4923 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4924 "unveil", NULL) == -1)
4930 cwd = getcwd(NULL, 0);
4932 error = got_error_from_errno("getcwd");
4935 error = got_worktree_open(&worktree, cwd);
4939 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4944 error = apply_unveil(got_repo_get_path(repo), 0,
4945 got_worktree_get_root_path(worktree));
4949 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4950 GOT_OBJ_TYPE_COMMIT, repo);
4951 if (error != NULL) {
4952 struct got_reference *ref;
4953 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4955 error = got_ref_open(&ref, repo, argv[0], 0);
4958 error = got_ref_resolve(&commit_id, repo, ref);
4963 error = got_object_id_str(&commit_id_str, commit_id);
4967 error = got_ref_open(&head_ref, repo,
4968 got_worktree_get_head_ref_name(worktree), 0);
4972 error = check_same_branch(commit_id, head_ref, NULL, repo);
4974 if (error->code != GOT_ERR_ANCESTRY)
4978 error = got_error(GOT_ERR_SAME_BRANCH);
4982 error = got_object_open_as_commit(&commit, repo, commit_id);
4985 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4986 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4987 commit_id, repo, update_progress, &did_something, check_cancelled,
4993 printf("Merged commit %s\n", commit_id_str);
4996 got_object_commit_close(commit);
4997 free(commit_id_str);
4999 got_ref_close(head_ref);
5001 got_worktree_close(worktree);
5003 got_repo_close(repo);
5010 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5014 static const struct got_error *
5015 cmd_backout(int argc, char *argv[])
5017 const struct got_error *error = NULL;
5018 struct got_worktree *worktree = NULL;
5019 struct got_repository *repo = NULL;
5020 char *cwd = NULL, *commit_id_str = NULL;
5021 struct got_object_id *commit_id = NULL;
5022 struct got_commit_object *commit = NULL;
5023 struct got_object_qid *pid;
5024 struct got_reference *head_ref = NULL;
5025 int ch, did_something = 0;
5027 while ((ch = getopt(argc, argv, "")) != -1) {
5039 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5040 "unveil", NULL) == -1)
5046 cwd = getcwd(NULL, 0);
5048 error = got_error_from_errno("getcwd");
5051 error = got_worktree_open(&worktree, cwd);
5055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5060 error = apply_unveil(got_repo_get_path(repo), 0,
5061 got_worktree_get_root_path(worktree));
5065 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5066 GOT_OBJ_TYPE_COMMIT, repo);
5067 if (error != NULL) {
5068 struct got_reference *ref;
5069 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5071 error = got_ref_open(&ref, repo, argv[0], 0);
5074 error = got_ref_resolve(&commit_id, repo, ref);
5079 error = got_object_id_str(&commit_id_str, commit_id);
5083 error = got_ref_open(&head_ref, repo,
5084 got_worktree_get_head_ref_name(worktree), 0);
5088 error = check_same_branch(commit_id, head_ref, NULL, repo);
5092 error = got_object_open_as_commit(&commit, repo, commit_id);
5095 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5097 error = got_error(GOT_ERR_ROOT_COMMIT);
5101 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5102 update_progress, &did_something, check_cancelled, NULL);
5107 printf("Backed out commit %s\n", commit_id_str);
5110 got_object_commit_close(commit);
5111 free(commit_id_str);
5113 got_ref_close(head_ref);
5115 got_worktree_close(worktree);
5117 got_repo_close(repo);
5124 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5130 trim_logmsg(char *logmsg, int limit)
5135 len = strlen(logmsg);
5139 nl = strchr(logmsg, '\n');
5144 static const struct got_error *
5145 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5147 const struct got_error *err;
5148 char *logmsg0 = NULL;
5151 err = got_object_commit_get_logmsg(&logmsg0, commit);
5156 while (isspace((unsigned char)s[0]))
5159 *logmsg = strdup(s);
5160 if (*logmsg == NULL) {
5161 err = got_error_from_errno("strdup");
5165 trim_logmsg(*logmsg, limit);
5171 static const struct got_error *
5172 show_rebase_progress(struct got_commit_object *commit,
5173 struct got_object_id *old_id, struct got_object_id *new_id)
5175 const struct got_error *err;
5176 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5178 err = got_object_id_str(&old_id_str, old_id);
5183 err = got_object_id_str(&new_id_str, new_id);
5188 old_id_str[12] = '\0';
5190 new_id_str[12] = '\0';
5192 err = get_short_logmsg(&logmsg, 42, commit);
5196 printf("%s -> %s: %s\n", old_id_str,
5197 new_id_str ? new_id_str : "no-op change", logmsg);
5204 static const struct got_error *
5205 rebase_progress(void *arg, unsigned char status, const char *path)
5207 unsigned char *rebase_status = arg;
5209 while (path[0] == '/')
5211 printf("%c %s\n", status, path);
5213 if (*rebase_status == GOT_STATUS_CONFLICT)
5215 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5216 *rebase_status = status;
5220 static const struct got_error *
5221 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5222 struct got_reference *branch, struct got_reference *new_base_branch,
5223 struct got_reference *tmp_branch, struct got_repository *repo)
5225 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5226 return got_worktree_rebase_complete(worktree, fileindex,
5227 new_base_branch, tmp_branch, branch, repo);
5230 static const struct got_error *
5231 rebase_commit(struct got_pathlist_head *merged_paths,
5232 struct got_worktree *worktree, struct got_fileindex *fileindex,
5233 struct got_reference *tmp_branch,
5234 struct got_object_id *commit_id, struct got_repository *repo)
5236 const struct got_error *error;
5237 struct got_commit_object *commit;
5238 struct got_object_id *new_commit_id;
5240 error = got_object_open_as_commit(&commit, repo, commit_id);
5244 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5245 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5247 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5249 error = show_rebase_progress(commit, commit_id, NULL);
5251 error = show_rebase_progress(commit, commit_id, new_commit_id);
5252 free(new_commit_id);
5255 got_object_commit_close(commit);
5259 struct check_path_prefix_arg {
5260 const char *path_prefix;
5265 static const struct got_error *
5266 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5267 struct got_blob_object *blob2, struct got_object_id *id1,
5268 struct got_object_id *id2, const char *path1, const char *path2,
5269 mode_t mode1, mode_t mode2, struct got_repository *repo)
5271 struct check_path_prefix_arg *a = arg;
5273 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5274 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5275 return got_error(a->errcode);
5280 static const struct got_error *
5281 check_path_prefix(struct got_object_id *parent_id,
5282 struct got_object_id *commit_id, const char *path_prefix,
5283 int errcode, struct got_repository *repo)
5285 const struct got_error *err;
5286 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5287 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5288 struct check_path_prefix_arg cpp_arg;
5290 if (got_path_is_root_dir(path_prefix))
5293 err = got_object_open_as_commit(&commit, repo, commit_id);
5297 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5301 err = got_object_open_as_tree(&tree1, repo,
5302 got_object_commit_get_tree_id(parent_commit));
5306 err = got_object_open_as_tree(&tree2, repo,
5307 got_object_commit_get_tree_id(commit));
5311 cpp_arg.path_prefix = path_prefix;
5312 while (cpp_arg.path_prefix[0] == '/')
5313 cpp_arg.path_prefix++;
5314 cpp_arg.len = strlen(cpp_arg.path_prefix);
5315 cpp_arg.errcode = errcode;
5316 err = got_diff_tree(tree1, tree2, "", "", repo,
5317 check_path_prefix_in_diff, &cpp_arg, 0);
5320 got_object_tree_close(tree1);
5322 got_object_tree_close(tree2);
5324 got_object_commit_close(commit);
5326 got_object_commit_close(parent_commit);
5330 static const struct got_error *
5331 collect_commits(struct got_object_id_queue *commits,
5332 struct got_object_id *initial_commit_id,
5333 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5334 const char *path_prefix, int path_prefix_errcode,
5335 struct got_repository *repo)
5337 const struct got_error *err = NULL;
5338 struct got_commit_graph *graph = NULL;
5339 struct got_object_id *parent_id = NULL;
5340 struct got_object_qid *qid;
5341 struct got_object_id *commit_id = initial_commit_id;
5343 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5347 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5348 check_cancelled, NULL);
5351 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5352 err = got_commit_graph_iter_next(&parent_id, graph);
5354 if (err->code == GOT_ERR_ITER_COMPLETED) {
5355 err = got_error_msg(GOT_ERR_ANCESTRY,
5356 "ran out of commits to rebase before "
5357 "youngest common ancestor commit has "
5360 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5362 err = got_commit_graph_fetch_commits(graph, 1, repo,
5363 check_cancelled, NULL);
5367 err = check_path_prefix(parent_id, commit_id,
5368 path_prefix, path_prefix_errcode, repo);
5372 err = got_object_qid_alloc(&qid, commit_id);
5375 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5376 commit_id = parent_id;
5380 got_commit_graph_close(graph);
5384 static const struct got_error *
5385 cmd_rebase(int argc, char *argv[])
5387 const struct got_error *error = NULL;
5388 struct got_worktree *worktree = NULL;
5389 struct got_repository *repo = NULL;
5390 struct got_fileindex *fileindex = NULL;
5392 struct got_reference *branch = NULL;
5393 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5394 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5395 struct got_object_id *resume_commit_id = NULL;
5396 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5397 struct got_commit_object *commit = NULL;
5398 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5399 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5400 struct got_object_id_queue commits;
5401 struct got_pathlist_head merged_paths;
5402 const struct got_object_id_queue *parent_ids;
5403 struct got_object_qid *qid, *pid;
5405 SIMPLEQ_INIT(&commits);
5406 TAILQ_INIT(&merged_paths);
5408 while ((ch = getopt(argc, argv, "ac")) != -1) {
5414 continue_rebase = 1;
5426 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5427 "unveil", NULL) == -1)
5430 if (abort_rebase && continue_rebase)
5432 else if (abort_rebase || continue_rebase) {
5435 } else if (argc != 1)
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),
5452 error = apply_unveil(got_repo_get_path(repo), 0,
5453 got_worktree_get_root_path(worktree));
5457 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5463 if (!rebase_in_progress) {
5464 error = got_error(GOT_ERR_NOT_REBASING);
5467 error = got_worktree_rebase_continue(&resume_commit_id,
5468 &new_base_branch, &tmp_branch, &branch, &fileindex,
5472 printf("Switching work tree to %s\n",
5473 got_ref_get_symref_target(new_base_branch));
5474 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5475 new_base_branch, update_progress, &did_something);
5478 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5479 goto done; /* nothing else to do */
5482 if (continue_rebase) {
5483 if (!rebase_in_progress) {
5484 error = got_error(GOT_ERR_NOT_REBASING);
5487 error = got_worktree_rebase_continue(&resume_commit_id,
5488 &new_base_branch, &tmp_branch, &branch, &fileindex,
5493 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5494 resume_commit_id, repo);
5498 yca_id = got_object_id_dup(resume_commit_id);
5499 if (yca_id == NULL) {
5500 error = got_error_from_errno("got_object_id_dup");
5504 error = got_ref_open(&branch, repo, argv[0], 0);
5509 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5513 if (!continue_rebase) {
5514 struct got_object_id *base_commit_id;
5516 base_commit_id = got_worktree_get_base_commit_id(worktree);
5517 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5518 base_commit_id, branch_head_commit_id, repo,
5519 check_cancelled, NULL);
5522 if (yca_id == NULL) {
5523 error = got_error_msg(GOT_ERR_ANCESTRY,
5524 "specified branch shares no common ancestry "
5525 "with work tree's branch");
5529 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5531 if (error->code != GOT_ERR_ANCESTRY)
5535 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5536 "specified branch resolves to a commit which "
5537 "is already contained in work tree's branch");
5540 error = got_worktree_rebase_prepare(&new_base_branch,
5541 &tmp_branch, &fileindex, worktree, branch, repo);
5546 commit_id = branch_head_commit_id;
5547 error = got_object_open_as_commit(&commit, repo, commit_id);
5551 parent_ids = got_object_commit_get_parent_ids(commit);
5552 pid = SIMPLEQ_FIRST(parent_ids);
5554 if (!continue_rebase) {
5556 error = got_worktree_rebase_abort(worktree, fileindex,
5557 repo, new_base_branch, update_progress,
5561 printf("Rebase of %s aborted\n",
5562 got_ref_get_name(branch));
5564 error = got_error(GOT_ERR_EMPTY_REBASE);
5567 error = collect_commits(&commits, commit_id, pid->id,
5568 yca_id, got_worktree_get_path_prefix(worktree),
5569 GOT_ERR_REBASE_PATH, repo);
5570 got_object_commit_close(commit);
5575 if (SIMPLEQ_EMPTY(&commits)) {
5576 if (continue_rebase) {
5577 error = rebase_complete(worktree, fileindex,
5578 branch, new_base_branch, tmp_branch, repo);
5581 /* Fast-forward the reference of the branch. */
5582 struct got_object_id *new_head_commit_id;
5584 error = got_ref_resolve(&new_head_commit_id, repo,
5588 error = got_object_id_str(&id_str, new_head_commit_id);
5589 printf("Forwarding %s to commit %s\n",
5590 got_ref_get_name(branch), id_str);
5592 error = got_ref_change_ref(branch,
5593 new_head_commit_id);
5600 SIMPLEQ_FOREACH(qid, &commits, entry) {
5601 commit_id = qid->id;
5602 parent_id = pid ? pid->id : yca_id;
5605 error = got_worktree_rebase_merge_files(&merged_paths,
5606 worktree, fileindex, parent_id, commit_id, repo,
5607 rebase_progress, &rebase_status, check_cancelled, NULL);
5611 if (rebase_status == GOT_STATUS_CONFLICT) {
5612 got_worktree_rebase_pathlist_free(&merged_paths);
5616 error = rebase_commit(&merged_paths, worktree, fileindex,
5617 tmp_branch, commit_id, repo);
5618 got_worktree_rebase_pathlist_free(&merged_paths);
5623 if (rebase_status == GOT_STATUS_CONFLICT) {
5624 error = got_worktree_rebase_postpone(worktree, fileindex);
5627 error = got_error_msg(GOT_ERR_CONFLICTS,
5628 "conflicts must be resolved before rebasing can continue");
5630 error = rebase_complete(worktree, fileindex, branch,
5631 new_base_branch, tmp_branch, repo);
5633 got_object_id_queue_free(&commits);
5634 free(branch_head_commit_id);
5635 free(resume_commit_id);
5638 got_object_commit_close(commit);
5640 got_ref_close(branch);
5641 if (new_base_branch)
5642 got_ref_close(new_base_branch);
5644 got_ref_close(tmp_branch);
5646 got_worktree_close(worktree);
5648 got_repo_close(repo);
5653 usage_histedit(void)
5655 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5660 #define GOT_HISTEDIT_PICK 'p'
5661 #define GOT_HISTEDIT_EDIT 'e'
5662 #define GOT_HISTEDIT_FOLD 'f'
5663 #define GOT_HISTEDIT_DROP 'd'
5664 #define GOT_HISTEDIT_MESG 'm'
5666 static struct got_histedit_cmd {
5670 } got_histedit_cmds[] = {
5671 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5672 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5673 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5674 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5675 { GOT_HISTEDIT_MESG, "mesg",
5676 "single-line log message for commit above (open editor if empty)" },
5679 struct got_histedit_list_entry {
5680 TAILQ_ENTRY(got_histedit_list_entry) entry;
5681 struct got_object_id *commit_id;
5682 const struct got_histedit_cmd *cmd;
5685 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5687 static const struct got_error *
5688 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5689 FILE *f, struct got_repository *repo)
5691 const struct got_error *err = NULL;
5692 char *logmsg = NULL, *id_str = NULL;
5693 struct got_commit_object *commit = NULL;
5696 err = got_object_open_as_commit(&commit, repo, commit_id);
5700 err = get_short_logmsg(&logmsg, 34, commit);
5704 err = got_object_id_str(&id_str, commit_id);
5708 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5710 err = got_ferror(f, GOT_ERR_IO);
5713 got_object_commit_close(commit);
5719 static const struct got_error *
5720 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5721 struct got_repository *repo)
5723 const struct got_error *err = NULL;
5724 struct got_object_qid *qid;
5726 if (SIMPLEQ_EMPTY(commits))
5727 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5729 SIMPLEQ_FOREACH(qid, commits, entry) {
5730 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5739 static const struct got_error *
5740 write_cmd_list(FILE *f)
5742 const struct got_error *err = NULL;
5745 n = fprintf(f, "# Available histedit commands:\n");
5747 return got_ferror(f, GOT_ERR_IO);
5749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5750 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5751 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5754 err = got_ferror(f, GOT_ERR_IO);
5758 n = fprintf(f, "# Commits will be processed in order from top to "
5759 "bottom of this file.\n");
5761 return got_ferror(f, GOT_ERR_IO);
5765 static const struct got_error *
5766 histedit_syntax_error(int lineno)
5768 static char msg[42];
5771 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5773 if (ret == -1 || ret >= sizeof(msg))
5774 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5776 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5779 static const struct got_error *
5780 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5781 char *logmsg, struct got_repository *repo)
5783 const struct got_error *err;
5784 struct got_commit_object *folded_commit = NULL;
5785 char *id_str, *folded_logmsg = NULL;
5787 err = got_object_id_str(&id_str, hle->commit_id);
5791 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5795 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5798 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5799 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5800 folded_logmsg) == -1) {
5801 err = got_error_from_errno("asprintf");
5806 got_object_commit_close(folded_commit);
5808 free(folded_logmsg);
5812 static struct got_histedit_list_entry *
5813 get_folded_commits(struct got_histedit_list_entry *hle)
5815 struct got_histedit_list_entry *prev, *folded = NULL;
5817 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5818 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5819 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5820 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5822 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5828 static const struct got_error *
5829 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5830 struct got_repository *repo)
5832 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5833 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5834 const struct got_error *err = NULL;
5835 struct got_commit_object *commit = NULL;
5837 struct got_histedit_list_entry *folded = NULL;
5839 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5843 folded = get_folded_commits(hle);
5845 while (folded != hle) {
5846 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5847 folded = TAILQ_NEXT(folded, entry);
5850 err = append_folded_commit_msg(&new_msg, folded,
5856 folded = TAILQ_NEXT(folded, entry);
5860 err = got_object_id_str(&id_str, hle->commit_id);
5863 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5866 if (asprintf(&new_msg,
5867 "%s\n# original log message of commit %s: %s",
5868 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5869 err = got_error_from_errno("asprintf");
5875 err = got_object_id_str(&id_str, hle->commit_id);
5879 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5883 dprintf(fd, logmsg);
5886 err = get_editor(&editor);
5890 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5892 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5894 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5897 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5898 err = got_error_from_errno2("unlink", logmsg_path);
5904 got_object_commit_close(commit);
5908 static const struct got_error *
5909 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5910 FILE *f, struct got_repository *repo)
5912 const struct got_error *err = NULL;
5913 char *line = NULL, *p, *end;
5917 const struct got_histedit_cmd *cmd;
5918 struct got_object_id *commit_id = NULL;
5919 struct got_histedit_list_entry *hle = NULL;
5922 len = getline(&line, &size, f);
5924 const struct got_error *getline_err;
5927 getline_err = got_error_from_errno("getline");
5928 err = got_ferror(f, getline_err->code);
5933 while (isspace((unsigned char)p[0]))
5935 if (p[0] == '#' || p[0] == '\0') {
5941 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5942 cmd = &got_histedit_cmds[i];
5943 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5944 isspace((unsigned char)p[strlen(cmd->name)])) {
5945 p += strlen(cmd->name);
5948 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5953 if (i == nitems(got_histedit_cmds)) {
5954 err = histedit_syntax_error(lineno);
5957 while (isspace((unsigned char)p[0]))
5959 if (cmd->code == GOT_HISTEDIT_MESG) {
5960 if (hle == NULL || hle->logmsg != NULL) {
5961 err = got_error(GOT_ERR_HISTEDIT_CMD);
5965 err = histedit_edit_logmsg(hle, repo);
5969 hle->logmsg = strdup(p);
5970 if (hle->logmsg == NULL) {
5971 err = got_error_from_errno("strdup");
5980 while (end[0] && !isspace((unsigned char)end[0]))
5984 err = got_object_resolve_id_str(&commit_id, repo, p);
5986 /* override error code */
5987 err = histedit_syntax_error(lineno);
5991 hle = malloc(sizeof(*hle));
5993 err = got_error_from_errno("malloc");
5997 hle->commit_id = commit_id;
6002 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6010 static const struct got_error *
6011 histedit_check_script(struct got_histedit_list *histedit_cmds,
6012 struct got_object_id_queue *commits, struct got_repository *repo)
6014 const struct got_error *err = NULL;
6015 struct got_object_qid *qid;
6016 struct got_histedit_list_entry *hle;
6017 static char msg[80];
6020 if (TAILQ_EMPTY(histedit_cmds))
6021 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6022 "histedit script contains no commands");
6023 if (SIMPLEQ_EMPTY(commits))
6024 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6026 SIMPLEQ_FOREACH(qid, commits, entry) {
6027 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6028 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6032 err = got_object_id_str(&id_str, qid->id);
6035 snprintf(msg, sizeof(msg),
6036 "commit %s missing from histedit script", id_str);
6038 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6042 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6043 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6044 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6045 "last commit in histedit script cannot be folded");
6050 static const struct got_error *
6051 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6052 const char *path, struct got_object_id_queue *commits,
6053 struct got_repository *repo)
6055 const struct got_error *err = NULL;
6059 err = get_editor(&editor);
6063 if (spawn_editor(editor, path) == -1) {
6064 err = got_error_from_errno("failed spawning editor");
6068 f = fopen(path, "r");
6070 err = got_error_from_errno("fopen");
6073 err = histedit_parse_list(histedit_cmds, f, repo);
6077 err = histedit_check_script(histedit_cmds, commits, repo);
6079 if (f && fclose(f) != 0 && err == NULL)
6080 err = got_error_from_errno("fclose");
6085 static const struct got_error *
6086 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6087 struct got_object_id_queue *, const char *, struct got_repository *);
6089 static const struct got_error *
6090 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6091 struct got_object_id_queue *commits, struct got_repository *repo)
6093 const struct got_error *err;
6097 err = got_opentemp_named(&path, &f, "got-histedit");
6101 err = write_cmd_list(f);
6105 err = histedit_write_commit_list(commits, f, repo);
6109 if (fclose(f) != 0) {
6110 err = got_error_from_errno("fclose");
6115 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6117 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6118 err->code != GOT_ERR_HISTEDIT_CMD)
6120 err = histedit_edit_list_retry(histedit_cmds, err,
6121 commits, path, repo);
6124 if (f && fclose(f) != 0 && err == NULL)
6125 err = got_error_from_errno("fclose");
6126 if (path && unlink(path) != 0 && err == NULL)
6127 err = got_error_from_errno2("unlink", path);
6132 static const struct got_error *
6133 histedit_save_list(struct got_histedit_list *histedit_cmds,
6134 struct got_worktree *worktree, struct got_repository *repo)
6136 const struct got_error *err = NULL;
6139 struct got_histedit_list_entry *hle;
6140 struct got_commit_object *commit = NULL;
6142 err = got_worktree_get_histedit_script_path(&path, worktree);
6146 f = fopen(path, "w");
6148 err = got_error_from_errno2("fopen", path);
6151 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6152 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6158 int n = fprintf(f, "%c %s\n",
6159 GOT_HISTEDIT_MESG, hle->logmsg);
6161 err = got_ferror(f, GOT_ERR_IO);
6167 if (f && fclose(f) != 0 && err == NULL)
6168 err = got_error_from_errno("fclose");
6171 got_object_commit_close(commit);
6176 histedit_free_list(struct got_histedit_list *histedit_cmds)
6178 struct got_histedit_list_entry *hle;
6180 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6181 TAILQ_REMOVE(histedit_cmds, hle, entry);
6186 static const struct got_error *
6187 histedit_load_list(struct got_histedit_list *histedit_cmds,
6188 const char *path, struct got_repository *repo)
6190 const struct got_error *err = NULL;
6193 f = fopen(path, "r");
6195 err = got_error_from_errno2("fopen", path);
6199 err = histedit_parse_list(histedit_cmds, f, repo);
6201 if (f && fclose(f) != 0 && err == NULL)
6202 err = got_error_from_errno("fclose");
6206 static const struct got_error *
6207 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6208 const struct got_error *edit_err, struct got_object_id_queue *commits,
6209 const char *path, struct got_repository *repo)
6211 const struct got_error *err = NULL, *prev_err = edit_err;
6214 while (resp != 'c' && resp != 'r' && resp != 'a') {
6215 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6216 "or (a)bort: ", getprogname(), prev_err->msg);
6221 histedit_free_list(histedit_cmds);
6222 err = histedit_run_editor(histedit_cmds, path, commits,
6225 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6226 err->code != GOT_ERR_HISTEDIT_CMD)
6233 } else if (resp == 'r') {
6234 histedit_free_list(histedit_cmds);
6235 err = histedit_edit_script(histedit_cmds,
6238 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6239 err->code != GOT_ERR_HISTEDIT_CMD)
6246 } else if (resp == 'a') {
6247 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6250 printf("invalid response '%c'\n", resp);
6256 static const struct got_error *
6257 histedit_complete(struct got_worktree *worktree,
6258 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6259 struct got_reference *branch, struct got_repository *repo)
6261 printf("Switching work tree to %s\n",
6262 got_ref_get_symref_target(branch));
6263 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6267 static const struct got_error *
6268 show_histedit_progress(struct got_commit_object *commit,
6269 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6271 const struct got_error *err;
6272 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6274 err = got_object_id_str(&old_id_str, hle->commit_id);
6279 err = got_object_id_str(&new_id_str, new_id);
6284 old_id_str[12] = '\0';
6286 new_id_str[12] = '\0';
6289 logmsg = strdup(hle->logmsg);
6290 if (logmsg == NULL) {
6291 err = got_error_from_errno("strdup");
6294 trim_logmsg(logmsg, 42);
6296 err = get_short_logmsg(&logmsg, 42, commit);
6301 switch (hle->cmd->code) {
6302 case GOT_HISTEDIT_PICK:
6303 case GOT_HISTEDIT_EDIT:
6304 printf("%s -> %s: %s\n", old_id_str,
6305 new_id_str ? new_id_str : "no-op change", logmsg);
6307 case GOT_HISTEDIT_DROP:
6308 case GOT_HISTEDIT_FOLD:
6309 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6322 static const struct got_error *
6323 histedit_commit(struct got_pathlist_head *merged_paths,
6324 struct got_worktree *worktree, struct got_fileindex *fileindex,
6325 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6326 struct got_repository *repo)
6328 const struct got_error *err;
6329 struct got_commit_object *commit;
6330 struct got_object_id *new_commit_id;
6332 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6333 && hle->logmsg == NULL) {
6334 err = histedit_edit_logmsg(hle, repo);
6339 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6343 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6344 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6347 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6349 err = show_histedit_progress(commit, hle, NULL);
6351 err = show_histedit_progress(commit, hle, new_commit_id);
6352 free(new_commit_id);
6355 got_object_commit_close(commit);
6359 static const struct got_error *
6360 histedit_skip_commit(struct got_histedit_list_entry *hle,
6361 struct got_worktree *worktree, struct got_repository *repo)
6363 const struct got_error *error;
6364 struct got_commit_object *commit;
6366 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6371 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6375 error = show_histedit_progress(commit, hle, NULL);
6376 got_object_commit_close(commit);
6380 static const struct got_error *
6381 cmd_histedit(int argc, char *argv[])
6383 const struct got_error *error = NULL;
6384 struct got_worktree *worktree = NULL;
6385 struct got_fileindex *fileindex = NULL;
6386 struct got_repository *repo = NULL;
6388 struct got_reference *branch = NULL;
6389 struct got_reference *tmp_branch = NULL;
6390 struct got_object_id *resume_commit_id = NULL;
6391 struct got_object_id *base_commit_id = NULL;
6392 struct got_object_id *head_commit_id = NULL;
6393 struct got_commit_object *commit = NULL;
6394 int ch, rebase_in_progress = 0, did_something;
6395 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6396 const char *edit_script_path = NULL;
6397 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6398 struct got_object_id_queue commits;
6399 struct got_pathlist_head merged_paths;
6400 const struct got_object_id_queue *parent_ids;
6401 struct got_object_qid *pid;
6402 struct got_histedit_list histedit_cmds;
6403 struct got_histedit_list_entry *hle;
6405 SIMPLEQ_INIT(&commits);
6406 TAILQ_INIT(&histedit_cmds);
6407 TAILQ_INIT(&merged_paths);
6409 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6418 edit_script_path = optarg;
6430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6431 "unveil", NULL) == -1)
6434 if (abort_edit && continue_edit)
6440 * This command cannot apply unveil(2) in all cases because the
6441 * user may choose to run an editor to edit the histedit script
6442 * and to edit individual commit log messages.
6443 * unveil(2) traverses exec(2); if an editor is used we have to
6444 * apply unveil after edit script and log messages have been written.
6445 * XXX TODO: Make use of unveil(2) where possible.
6448 cwd = getcwd(NULL, 0);
6450 error = got_error_from_errno("getcwd");
6453 error = got_worktree_open(&worktree, cwd);
6457 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6462 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6465 if (rebase_in_progress) {
6466 error = got_error(GOT_ERR_REBASING);
6470 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6474 if (edit_in_progress && abort_edit) {
6475 error = got_worktree_histedit_continue(&resume_commit_id,
6476 &tmp_branch, &branch, &base_commit_id, &fileindex,
6480 printf("Switching work tree to %s\n",
6481 got_ref_get_symref_target(branch));
6482 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6483 branch, base_commit_id, update_progress, &did_something);
6486 printf("Histedit of %s aborted\n",
6487 got_ref_get_symref_target(branch));
6488 goto done; /* nothing else to do */
6489 } else if (abort_edit) {
6490 error = got_error(GOT_ERR_NOT_HISTEDIT);
6494 if (continue_edit) {
6497 if (!edit_in_progress) {
6498 error = got_error(GOT_ERR_NOT_HISTEDIT);
6502 error = got_worktree_get_histedit_script_path(&path, worktree);
6506 error = histedit_load_list(&histedit_cmds, path, repo);
6511 error = got_worktree_histedit_continue(&resume_commit_id,
6512 &tmp_branch, &branch, &base_commit_id, &fileindex,
6517 error = got_ref_resolve(&head_commit_id, repo, branch);
6521 error = got_object_open_as_commit(&commit, repo,
6525 parent_ids = got_object_commit_get_parent_ids(commit);
6526 pid = SIMPLEQ_FIRST(parent_ids);
6528 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6531 error = collect_commits(&commits, head_commit_id, pid->id,
6532 base_commit_id, got_worktree_get_path_prefix(worktree),
6533 GOT_ERR_HISTEDIT_PATH, repo);
6534 got_object_commit_close(commit);
6539 if (edit_in_progress) {
6540 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6544 error = got_ref_open(&branch, repo,
6545 got_worktree_get_head_ref_name(worktree), 0);
6549 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6550 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6551 "will not edit commit history of a branch outside "
6552 "the \"refs/heads/\" reference namespace");
6556 error = got_ref_resolve(&head_commit_id, repo, branch);
6557 got_ref_close(branch);
6562 error = got_object_open_as_commit(&commit, repo,
6566 parent_ids = got_object_commit_get_parent_ids(commit);
6567 pid = SIMPLEQ_FIRST(parent_ids);
6569 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6572 error = collect_commits(&commits, head_commit_id, pid->id,
6573 got_worktree_get_base_commit_id(worktree),
6574 got_worktree_get_path_prefix(worktree),
6575 GOT_ERR_HISTEDIT_PATH, repo);
6576 got_object_commit_close(commit);
6581 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6582 &base_commit_id, &fileindex, worktree, repo);
6586 if (edit_script_path) {
6587 error = histedit_load_list(&histedit_cmds,
6588 edit_script_path, repo);
6590 got_worktree_histedit_abort(worktree, fileindex,
6591 repo, branch, base_commit_id,
6592 update_progress, &did_something);
6596 error = histedit_edit_script(&histedit_cmds, &commits,
6599 got_worktree_histedit_abort(worktree, fileindex,
6600 repo, branch, base_commit_id,
6601 update_progress, &did_something);
6607 error = histedit_save_list(&histedit_cmds, worktree,
6610 got_worktree_histedit_abort(worktree, fileindex,
6611 repo, branch, base_commit_id,
6612 update_progress, &did_something);
6618 error = histedit_check_script(&histedit_cmds, &commits, repo);
6622 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6623 if (resume_commit_id) {
6624 if (got_object_id_cmp(hle->commit_id,
6625 resume_commit_id) != 0)
6628 resume_commit_id = NULL;
6629 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6630 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6631 error = histedit_skip_commit(hle, worktree,
6634 error = histedit_commit(NULL, worktree,
6635 fileindex, tmp_branch, hle, repo);
6642 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6643 error = histedit_skip_commit(hle, worktree, repo);
6649 error = got_object_open_as_commit(&commit, repo,
6653 parent_ids = got_object_commit_get_parent_ids(commit);
6654 pid = SIMPLEQ_FIRST(parent_ids);
6656 error = got_worktree_histedit_merge_files(&merged_paths,
6657 worktree, fileindex, pid->id, hle->commit_id, repo,
6658 rebase_progress, &rebase_status, check_cancelled, NULL);
6661 got_object_commit_close(commit);
6664 if (rebase_status == GOT_STATUS_CONFLICT) {
6665 got_worktree_rebase_pathlist_free(&merged_paths);
6669 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6671 error = got_object_id_str(&id_str, hle->commit_id);
6674 printf("Stopping histedit for amending commit %s\n",
6677 got_worktree_rebase_pathlist_free(&merged_paths);
6678 error = got_worktree_histedit_postpone(worktree,
6683 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6684 error = histedit_skip_commit(hle, worktree, repo);
6690 error = histedit_commit(&merged_paths, worktree, fileindex,
6691 tmp_branch, hle, repo);
6692 got_worktree_rebase_pathlist_free(&merged_paths);
6697 if (rebase_status == GOT_STATUS_CONFLICT) {
6698 error = got_worktree_histedit_postpone(worktree, fileindex);
6701 error = got_error_msg(GOT_ERR_CONFLICTS,
6702 "conflicts must be resolved before rebasing can continue");
6704 error = histedit_complete(worktree, fileindex, tmp_branch,
6707 got_object_id_queue_free(&commits);
6708 histedit_free_list(&histedit_cmds);
6709 free(head_commit_id);
6710 free(base_commit_id);
6711 free(resume_commit_id);
6713 got_object_commit_close(commit);
6715 got_ref_close(branch);
6717 got_ref_close(tmp_branch);
6719 got_worktree_close(worktree);
6721 got_repo_close(repo);
6726 usage_integrate(void)
6728 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6732 static const struct got_error *
6733 cmd_integrate(int argc, char *argv[])
6735 const struct got_error *error = NULL;
6736 struct got_repository *repo = NULL;
6737 struct got_worktree *worktree = NULL;
6738 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6739 const char *branch_arg = NULL;
6740 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6741 struct got_fileindex *fileindex = NULL;
6742 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6743 int ch, did_something = 0;
6745 while ((ch = getopt(argc, argv, "")) != -1) {
6758 branch_arg = argv[0];
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6761 "unveil", NULL) == -1)
6764 cwd = getcwd(NULL, 0);
6766 error = got_error_from_errno("getcwd");
6770 error = got_worktree_open(&worktree, cwd);
6774 error = check_rebase_or_histedit_in_progress(worktree);
6778 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6783 error = apply_unveil(got_repo_get_path(repo), 0,
6784 got_worktree_get_root_path(worktree));
6788 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6789 error = got_error_from_errno("asprintf");
6793 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6794 &base_branch_ref, worktree, refname, repo);
6798 refname = strdup(got_ref_get_name(branch_ref));
6799 if (refname == NULL) {
6800 error = got_error_from_errno("strdup");
6801 got_worktree_integrate_abort(worktree, fileindex, repo,
6802 branch_ref, base_branch_ref);
6805 base_refname = strdup(got_ref_get_name(base_branch_ref));
6806 if (base_refname == NULL) {
6807 error = got_error_from_errno("strdup");
6808 got_worktree_integrate_abort(worktree, fileindex, repo,
6809 branch_ref, base_branch_ref);
6813 error = got_ref_resolve(&commit_id, repo, branch_ref);
6817 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6821 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6822 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6823 "specified branch has already been integrated");
6824 got_worktree_integrate_abort(worktree, fileindex, repo,
6825 branch_ref, base_branch_ref);
6829 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6831 if (error->code == GOT_ERR_ANCESTRY)
6832 error = got_error(GOT_ERR_REBASE_REQUIRED);
6833 got_worktree_integrate_abort(worktree, fileindex, repo,
6834 branch_ref, base_branch_ref);
6838 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6839 branch_ref, base_branch_ref, update_progress, &did_something,
6840 check_cancelled, NULL);
6844 printf("Integrated %s into %s\n", refname, base_refname);
6847 got_repo_close(repo);
6849 got_worktree_close(worktree);
6851 free(base_commit_id);
6861 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6862 "[file-path ...]\n",
6867 static const struct got_error *
6868 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6869 const char *path, struct got_object_id *blob_id,
6870 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6871 int dirfd, const char *de_name)
6873 const struct got_error *err = NULL;
6874 char *id_str = NULL;
6876 if (staged_status != GOT_STATUS_ADD &&
6877 staged_status != GOT_STATUS_MODIFY &&
6878 staged_status != GOT_STATUS_DELETE)
6881 if (staged_status == GOT_STATUS_ADD ||
6882 staged_status == GOT_STATUS_MODIFY)
6883 err = got_object_id_str(&id_str, staged_blob_id);
6885 err = got_object_id_str(&id_str, blob_id);
6889 printf("%s %c %s\n", id_str, staged_status, path);
6894 static const struct got_error *
6895 cmd_stage(int argc, char *argv[])
6897 const struct got_error *error = NULL;
6898 struct got_repository *repo = NULL;
6899 struct got_worktree *worktree = NULL;
6901 struct got_pathlist_head paths;
6902 struct got_pathlist_entry *pe;
6903 int ch, list_stage = 0, pflag = 0;
6904 FILE *patch_script_file = NULL;
6905 const char *patch_script_path = NULL;
6906 struct choose_patch_arg cpa;
6910 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6919 patch_script_path = optarg;
6931 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6932 "unveil", NULL) == -1)
6935 if (list_stage && (pflag || patch_script_path))
6936 errx(1, "-l option cannot be used with other options");
6937 if (patch_script_path && !pflag)
6938 errx(1, "-F option can only be used together with -p option");
6940 cwd = getcwd(NULL, 0);
6942 error = got_error_from_errno("getcwd");
6946 error = got_worktree_open(&worktree, cwd);
6950 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6955 if (patch_script_path) {
6956 patch_script_file = fopen(patch_script_path, "r");
6957 if (patch_script_file == NULL) {
6958 error = got_error_from_errno2("fopen",
6963 error = apply_unveil(got_repo_get_path(repo), 0,
6964 got_worktree_get_root_path(worktree));
6968 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6973 error = got_worktree_status(worktree, &paths, repo,
6974 print_stage, NULL, check_cancelled, NULL);
6976 cpa.patch_script_file = patch_script_file;
6977 cpa.action = "stage";
6978 error = got_worktree_stage(worktree, &paths,
6979 pflag ? NULL : print_status, NULL,
6980 pflag ? choose_patch : NULL, &cpa, repo);
6983 if (patch_script_file && fclose(patch_script_file) == EOF &&
6985 error = got_error_from_errno2("fclose", patch_script_path);
6987 got_repo_close(repo);
6989 got_worktree_close(worktree);
6990 TAILQ_FOREACH(pe, &paths, entry)
6991 free((char *)pe->path);
6992 got_pathlist_free(&paths);
7000 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7001 "[file-path ...]\n",
7007 static const struct got_error *
7008 cmd_unstage(int argc, char *argv[])
7010 const struct got_error *error = NULL;
7011 struct got_repository *repo = NULL;
7012 struct got_worktree *worktree = NULL;
7014 struct got_pathlist_head paths;
7015 struct got_pathlist_entry *pe;
7016 int ch, did_something = 0, pflag = 0;
7017 FILE *patch_script_file = NULL;
7018 const char *patch_script_path = NULL;
7019 struct choose_patch_arg cpa;
7023 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7029 patch_script_path = optarg;
7041 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7042 "unveil", NULL) == -1)
7045 if (patch_script_path && !pflag)
7046 errx(1, "-F option can only be used together with -p option");
7048 cwd = getcwd(NULL, 0);
7050 error = got_error_from_errno("getcwd");
7054 error = got_worktree_open(&worktree, cwd);
7058 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7063 if (patch_script_path) {
7064 patch_script_file = fopen(patch_script_path, "r");
7065 if (patch_script_file == NULL) {
7066 error = got_error_from_errno2("fopen",
7072 error = apply_unveil(got_repo_get_path(repo), 0,
7073 got_worktree_get_root_path(worktree));
7077 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7081 cpa.patch_script_file = patch_script_file;
7082 cpa.action = "unstage";
7083 error = got_worktree_unstage(worktree, &paths, update_progress,
7084 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7086 if (patch_script_file && fclose(patch_script_file) == EOF &&
7088 error = got_error_from_errno2("fclose", patch_script_path);
7090 got_repo_close(repo);
7092 got_worktree_close(worktree);
7093 TAILQ_FOREACH(pe, &paths, entry)
7094 free((char *)pe->path);
7095 got_pathlist_free(&paths);
7103 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7104 "arg1 [arg2 ...]\n", getprogname());
7108 static const struct got_error *
7109 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7111 const struct got_error *err;
7112 struct got_blob_object *blob;
7114 err = got_object_open_as_blob(&blob, repo, id, 8192);
7118 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7119 got_object_blob_close(blob);
7123 static const struct got_error *
7124 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7126 const struct got_error *err;
7127 struct got_tree_object *tree;
7130 err = got_object_open_as_tree(&tree, repo, id);
7134 nentries = got_object_tree_get_nentries(tree);
7135 for (i = 0; i < nentries; i++) {
7136 struct got_tree_entry *te;
7138 if (sigint_received || sigpipe_received)
7140 te = got_object_tree_get_entry(tree, i);
7141 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7144 fprintf(outfile, "%s %.7o %s\n", id_str,
7145 got_tree_entry_get_mode(te),
7146 got_tree_entry_get_name(te));
7150 got_object_tree_close(tree);
7154 static const struct got_error *
7155 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7157 const struct got_error *err;
7158 struct got_commit_object *commit;
7159 const struct got_object_id_queue *parent_ids;
7160 struct got_object_qid *pid;
7161 char *id_str = NULL;
7162 const char *logmsg = NULL;
7164 err = got_object_open_as_commit(&commit, repo, id);
7168 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7172 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7173 parent_ids = got_object_commit_get_parent_ids(commit);
7174 fprintf(outfile, "numparents %d\n",
7175 got_object_commit_get_nparents(commit));
7176 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7178 err = got_object_id_str(&pid_str, pid->id);
7181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7184 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7185 got_object_commit_get_author(commit),
7186 got_object_commit_get_author_time(commit));
7188 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7189 got_object_commit_get_author(commit),
7190 got_object_commit_get_committer_time(commit));
7192 logmsg = got_object_commit_get_logmsg_raw(commit);
7193 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7194 fprintf(outfile, "%s", logmsg);
7197 got_object_commit_close(commit);
7201 static const struct got_error *
7202 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7204 const struct got_error *err;
7205 struct got_tag_object *tag;
7206 char *id_str = NULL;
7207 const char *tagmsg = NULL;
7209 err = got_object_open_as_tag(&tag, repo, id);
7213 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7217 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7219 switch (got_object_tag_get_object_type(tag)) {
7220 case GOT_OBJ_TYPE_BLOB:
7221 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7222 GOT_OBJ_LABEL_BLOB);
7224 case GOT_OBJ_TYPE_TREE:
7225 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7226 GOT_OBJ_LABEL_TREE);
7228 case GOT_OBJ_TYPE_COMMIT:
7229 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7230 GOT_OBJ_LABEL_COMMIT);
7232 case GOT_OBJ_TYPE_TAG:
7233 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7240 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7241 got_object_tag_get_name(tag));
7243 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7244 got_object_tag_get_tagger(tag),
7245 got_object_tag_get_tagger_time(tag));
7247 tagmsg = got_object_tag_get_message(tag);
7248 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7249 fprintf(outfile, "%s", tagmsg);
7252 got_object_tag_close(tag);
7256 static const struct got_error *
7257 cmd_cat(int argc, char *argv[])
7259 const struct got_error *error;
7260 struct got_repository *repo = NULL;
7261 struct got_worktree *worktree = NULL;
7262 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7263 const char *commit_id_str = NULL;
7264 struct got_object_id *id = NULL, *commit_id = NULL;
7265 int ch, obj_type, i, force_path = 0;
7268 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7273 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7276 commit_id_str = optarg;
7279 repo_path = realpath(optarg, NULL);
7280 if (repo_path == NULL)
7281 return got_error_from_errno2("realpath",
7283 got_path_strip_trailing_slashes(repo_path);
7297 cwd = getcwd(NULL, 0);
7299 error = got_error_from_errno("getcwd");
7302 error = got_worktree_open(&worktree, cwd);
7303 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7306 if (repo_path == NULL) {
7308 got_worktree_get_repo_path(worktree));
7309 if (repo_path == NULL) {
7310 error = got_error_from_errno("strdup");
7316 if (repo_path == NULL) {
7317 repo_path = getcwd(NULL, 0);
7318 if (repo_path == NULL)
7319 return got_error_from_errno("getcwd");
7322 error = got_repo_open(&repo, repo_path, NULL);
7327 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7331 if (commit_id_str == NULL)
7332 commit_id_str = GOT_REF_HEAD;
7333 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7337 for (i = 0; i < argc; i++) {
7339 error = got_object_id_by_path(&id, repo, commit_id,
7344 error = match_object_id(&id, &label, argv[i],
7345 GOT_OBJ_TYPE_ANY, 0, repo);
7347 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7348 error->code != GOT_ERR_NOT_REF)
7350 error = got_object_id_by_path(&id, repo,
7351 commit_id, argv[i]);
7357 error = got_object_get_type(&obj_type, repo, id);
7362 case GOT_OBJ_TYPE_BLOB:
7363 error = cat_blob(id, repo, stdout);
7365 case GOT_OBJ_TYPE_TREE:
7366 error = cat_tree(id, repo, stdout);
7368 case GOT_OBJ_TYPE_COMMIT:
7369 error = cat_commit(id, repo, stdout);
7371 case GOT_OBJ_TYPE_TAG:
7372 error = cat_tag(id, repo, stdout);
7375 error = got_error(GOT_ERR_OBJ_TYPE);
7391 got_worktree_close(worktree);
7393 const struct got_error *repo_error;
7394 repo_error = got_repo_close(repo);