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] [-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;
4331 while ((ch = getopt(argc, argv, "fR")) != -1) {
4334 delete_local_mods = 1;
4349 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4356 cwd = getcwd(NULL, 0);
4358 error = got_error_from_errno("getcwd");
4361 error = got_worktree_open(&worktree, cwd);
4365 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4370 error = apply_unveil(got_repo_get_path(repo), 1,
4371 got_worktree_get_root_path(worktree));
4375 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4382 TAILQ_FOREACH(pe, &paths, entry) {
4383 if (asprintf(&ondisk_path, "%s/%s",
4384 got_worktree_get_root_path(worktree),
4386 error = got_error_from_errno("asprintf");
4389 if (lstat(ondisk_path, &sb) == -1) {
4390 if (errno == ENOENT) {
4394 error = got_error_from_errno2("lstat",
4400 if (S_ISDIR(sb.st_mode)) {
4401 error = got_error_msg(GOT_ERR_BAD_PATH,
4402 "removing directories requires -R option");
4408 error = got_worktree_schedule_delete(worktree, &paths,
4409 delete_local_mods, print_remove_status, NULL, repo);
4414 got_repo_close(repo);
4416 got_worktree_close(worktree);
4417 TAILQ_FOREACH(pe, &paths, entry)
4418 free((char *)pe->path);
4419 got_pathlist_free(&paths);
4427 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4428 "path ...\n", getprogname());
4432 static const struct got_error *
4433 revert_progress(void *arg, unsigned char status, const char *path)
4435 while (path[0] == '/')
4437 printf("%c %s\n", status, path);
4441 struct choose_patch_arg {
4442 FILE *patch_script_file;
4446 static const struct got_error *
4447 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4448 int nchanges, const char *action)
4451 size_t linesize = 0;
4455 case GOT_STATUS_ADD:
4456 printf("A %s\n%s this addition? [y/n] ", path, action);
4458 case GOT_STATUS_DELETE:
4459 printf("D %s\n%s this deletion? [y/n] ", path, action);
4461 case GOT_STATUS_MODIFY:
4462 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4463 return got_error_from_errno("fseek");
4464 printf(GOT_COMMIT_SEP_STR);
4465 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4467 if (ferror(patch_file))
4468 return got_error_from_errno("getline");
4469 printf(GOT_COMMIT_SEP_STR);
4470 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4471 path, n, nchanges, action);
4474 return got_error_path(path, GOT_ERR_FILE_STATUS);
4480 static const struct got_error *
4481 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4482 FILE *patch_file, int n, int nchanges)
4484 const struct got_error *err = NULL;
4486 size_t linesize = 0;
4489 struct choose_patch_arg *a = arg;
4491 *choice = GOT_PATCH_CHOICE_NONE;
4493 if (a->patch_script_file) {
4495 err = show_change(status, path, patch_file, n, nchanges,
4499 linelen = getline(&line, &linesize, a->patch_script_file);
4500 if (linelen == -1) {
4501 if (ferror(a->patch_script_file))
4502 return got_error_from_errno("getline");
4505 nl = strchr(line, '\n');
4508 if (strcmp(line, "y") == 0) {
4509 *choice = GOT_PATCH_CHOICE_YES;
4511 } else if (strcmp(line, "n") == 0) {
4512 *choice = GOT_PATCH_CHOICE_NO;
4514 } else if (strcmp(line, "q") == 0 &&
4515 status == GOT_STATUS_MODIFY) {
4516 *choice = GOT_PATCH_CHOICE_QUIT;
4519 printf("invalid response '%s'\n", line);
4524 while (resp != 'y' && resp != 'n' && resp != 'q') {
4525 err = show_change(status, path, patch_file, n, nchanges,
4532 if (status == GOT_STATUS_MODIFY) {
4533 if (resp != 'y' && resp != 'n' && resp != 'q') {
4534 printf("invalid response '%c'\n", resp);
4537 } else if (resp != 'y' && resp != 'n') {
4538 printf("invalid response '%c'\n", resp);
4544 *choice = GOT_PATCH_CHOICE_YES;
4545 else if (resp == 'n')
4546 *choice = GOT_PATCH_CHOICE_NO;
4547 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4548 *choice = GOT_PATCH_CHOICE_QUIT;
4554 static const struct got_error *
4555 cmd_revert(int argc, char *argv[])
4557 const struct got_error *error = NULL;
4558 struct got_worktree *worktree = NULL;
4559 struct got_repository *repo = NULL;
4560 char *cwd = NULL, *path = NULL;
4561 struct got_pathlist_head paths;
4562 struct got_pathlist_entry *pe;
4563 int ch, can_recurse = 0, pflag = 0;
4564 FILE *patch_script_file = NULL;
4565 const char *patch_script_path = NULL;
4566 struct choose_patch_arg cpa;
4570 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4576 patch_script_path = optarg;
4591 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4592 "unveil", NULL) == -1)
4597 if (patch_script_path && !pflag)
4598 errx(1, "-F option can only be used together with -p option");
4600 cwd = getcwd(NULL, 0);
4602 error = got_error_from_errno("getcwd");
4605 error = got_worktree_open(&worktree, cwd);
4609 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4614 if (patch_script_path) {
4615 patch_script_file = fopen(patch_script_path, "r");
4616 if (patch_script_file == NULL) {
4617 error = got_error_from_errno2("fopen",
4622 error = apply_unveil(got_repo_get_path(repo), 1,
4623 got_worktree_get_root_path(worktree));
4627 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4634 TAILQ_FOREACH(pe, &paths, entry) {
4635 if (asprintf(&ondisk_path, "%s/%s",
4636 got_worktree_get_root_path(worktree),
4638 error = got_error_from_errno("asprintf");
4641 if (lstat(ondisk_path, &sb) == -1) {
4642 if (errno == ENOENT) {
4646 error = got_error_from_errno2("lstat",
4652 if (S_ISDIR(sb.st_mode)) {
4653 error = got_error_msg(GOT_ERR_BAD_PATH,
4654 "reverting directories requires -R option");
4660 cpa.patch_script_file = patch_script_file;
4661 cpa.action = "revert";
4662 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4663 pflag ? choose_patch : NULL, &cpa, repo);
4667 if (patch_script_file && fclose(patch_script_file) == EOF &&
4669 error = got_error_from_errno2("fclose", patch_script_path);
4671 got_repo_close(repo);
4673 got_worktree_close(worktree);
4682 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4687 struct collect_commit_logmsg_arg {
4688 const char *cmdline_log;
4690 const char *worktree_path;
4691 const char *branch_name;
4692 const char *repo_path;
4697 static const struct got_error *
4698 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4701 char *initial_content = NULL;
4702 struct got_pathlist_entry *pe;
4703 const struct got_error *err = NULL;
4704 char *template = NULL;
4705 struct collect_commit_logmsg_arg *a = arg;
4709 /* if a message was specified on the command line, just use it */
4710 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4711 len = strlen(a->cmdline_log) + 1;
4712 *logmsg = malloc(len + 1);
4713 if (*logmsg == NULL)
4714 return got_error_from_errno("malloc");
4715 strlcpy(*logmsg, a->cmdline_log, len);
4719 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4720 return got_error_from_errno("asprintf");
4722 if (asprintf(&initial_content,
4723 "\n# changes to be committed on branch %s:\n",
4724 a->branch_name) == -1)
4725 return got_error_from_errno("asprintf");
4727 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4731 dprintf(fd, initial_content);
4733 TAILQ_FOREACH(pe, commitable_paths, entry) {
4734 struct got_commitable *ct = pe->data;
4735 dprintf(fd, "# %c %s\n",
4736 got_commitable_get_status(ct),
4737 got_commitable_get_path(ct));
4741 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4743 free(initial_content);
4746 /* Editor is done; we can now apply unveil(2) */
4748 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4757 static const struct got_error *
4758 cmd_commit(int argc, char *argv[])
4760 const struct got_error *error = NULL;
4761 struct got_worktree *worktree = NULL;
4762 struct got_repository *repo = NULL;
4763 char *cwd = NULL, *id_str = NULL;
4764 struct got_object_id *id = NULL;
4765 const char *logmsg = NULL;
4766 struct collect_commit_logmsg_arg cl_arg;
4767 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4768 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4769 struct got_pathlist_head paths;
4772 cl_arg.logmsg_path = NULL;
4774 while ((ch = getopt(argc, argv, "m:")) != -1) {
4789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4790 "unveil", NULL) == -1)
4793 cwd = getcwd(NULL, 0);
4795 error = got_error_from_errno("getcwd");
4798 error = got_worktree_open(&worktree, cwd);
4802 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4805 if (rebase_in_progress) {
4806 error = got_error(GOT_ERR_REBASING);
4810 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4815 error = get_gitconfig_path(&gitconfig_path);
4818 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4823 error = get_author(&author, repo);
4828 * unveil(2) traverses exec(2); if an editor is used we have
4829 * to apply unveil after the log message has been written.
4831 if (logmsg == NULL || strlen(logmsg) == 0)
4832 error = get_editor(&editor);
4834 error = apply_unveil(got_repo_get_path(repo), 0,
4835 got_worktree_get_root_path(worktree));
4839 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4843 cl_arg.editor = editor;
4844 cl_arg.cmdline_log = logmsg;
4845 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4846 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4847 if (!histedit_in_progress) {
4848 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4849 error = got_error(GOT_ERR_COMMIT_BRANCH);
4852 cl_arg.branch_name += 11;
4854 cl_arg.repo_path = got_repo_get_path(repo);
4855 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4856 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4858 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4859 cl_arg.logmsg_path != NULL)
4860 preserve_logmsg = 1;
4864 error = got_object_id_str(&id_str, id);
4867 printf("Created commit %s\n", id_str);
4869 if (preserve_logmsg) {
4870 fprintf(stderr, "%s: log message preserved in %s\n",
4871 getprogname(), cl_arg.logmsg_path);
4872 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4874 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4875 free(cl_arg.logmsg_path);
4877 got_repo_close(repo);
4879 got_worktree_close(worktree);
4882 free(gitconfig_path);
4889 usage_cherrypick(void)
4891 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4895 static const struct got_error *
4896 cmd_cherrypick(int argc, char *argv[])
4898 const struct got_error *error = NULL;
4899 struct got_worktree *worktree = NULL;
4900 struct got_repository *repo = NULL;
4901 char *cwd = NULL, *commit_id_str = NULL;
4902 struct got_object_id *commit_id = NULL;
4903 struct got_commit_object *commit = NULL;
4904 struct got_object_qid *pid;
4905 struct got_reference *head_ref = NULL;
4906 int ch, did_something = 0;
4908 while ((ch = getopt(argc, argv, "")) != -1) {
4920 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4921 "unveil", NULL) == -1)
4927 cwd = getcwd(NULL, 0);
4929 error = got_error_from_errno("getcwd");
4932 error = got_worktree_open(&worktree, cwd);
4936 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4941 error = apply_unveil(got_repo_get_path(repo), 0,
4942 got_worktree_get_root_path(worktree));
4946 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4947 GOT_OBJ_TYPE_COMMIT, repo);
4948 if (error != NULL) {
4949 struct got_reference *ref;
4950 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4952 error = got_ref_open(&ref, repo, argv[0], 0);
4955 error = got_ref_resolve(&commit_id, repo, ref);
4960 error = got_object_id_str(&commit_id_str, commit_id);
4964 error = got_ref_open(&head_ref, repo,
4965 got_worktree_get_head_ref_name(worktree), 0);
4969 error = check_same_branch(commit_id, head_ref, NULL, repo);
4971 if (error->code != GOT_ERR_ANCESTRY)
4975 error = got_error(GOT_ERR_SAME_BRANCH);
4979 error = got_object_open_as_commit(&commit, repo, commit_id);
4982 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4983 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4984 commit_id, repo, update_progress, &did_something, check_cancelled,
4990 printf("Merged commit %s\n", commit_id_str);
4993 got_object_commit_close(commit);
4994 free(commit_id_str);
4996 got_ref_close(head_ref);
4998 got_worktree_close(worktree);
5000 got_repo_close(repo);
5007 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5011 static const struct got_error *
5012 cmd_backout(int argc, char *argv[])
5014 const struct got_error *error = NULL;
5015 struct got_worktree *worktree = NULL;
5016 struct got_repository *repo = NULL;
5017 char *cwd = NULL, *commit_id_str = NULL;
5018 struct got_object_id *commit_id = NULL;
5019 struct got_commit_object *commit = NULL;
5020 struct got_object_qid *pid;
5021 struct got_reference *head_ref = NULL;
5022 int ch, did_something = 0;
5024 while ((ch = getopt(argc, argv, "")) != -1) {
5036 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5037 "unveil", NULL) == -1)
5043 cwd = getcwd(NULL, 0);
5045 error = got_error_from_errno("getcwd");
5048 error = got_worktree_open(&worktree, cwd);
5052 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5057 error = apply_unveil(got_repo_get_path(repo), 0,
5058 got_worktree_get_root_path(worktree));
5062 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5063 GOT_OBJ_TYPE_COMMIT, repo);
5064 if (error != NULL) {
5065 struct got_reference *ref;
5066 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5068 error = got_ref_open(&ref, repo, argv[0], 0);
5071 error = got_ref_resolve(&commit_id, repo, ref);
5076 error = got_object_id_str(&commit_id_str, commit_id);
5080 error = got_ref_open(&head_ref, repo,
5081 got_worktree_get_head_ref_name(worktree), 0);
5085 error = check_same_branch(commit_id, head_ref, NULL, repo);
5089 error = got_object_open_as_commit(&commit, repo, commit_id);
5092 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5094 error = got_error(GOT_ERR_ROOT_COMMIT);
5098 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5099 update_progress, &did_something, check_cancelled, NULL);
5104 printf("Backed out commit %s\n", commit_id_str);
5107 got_object_commit_close(commit);
5108 free(commit_id_str);
5110 got_ref_close(head_ref);
5112 got_worktree_close(worktree);
5114 got_repo_close(repo);
5121 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5127 trim_logmsg(char *logmsg, int limit)
5132 len = strlen(logmsg);
5136 nl = strchr(logmsg, '\n');
5141 static const struct got_error *
5142 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5144 const struct got_error *err;
5145 char *logmsg0 = NULL;
5148 err = got_object_commit_get_logmsg(&logmsg0, commit);
5153 while (isspace((unsigned char)s[0]))
5156 *logmsg = strdup(s);
5157 if (*logmsg == NULL) {
5158 err = got_error_from_errno("strdup");
5162 trim_logmsg(*logmsg, limit);
5168 static const struct got_error *
5169 show_rebase_progress(struct got_commit_object *commit,
5170 struct got_object_id *old_id, struct got_object_id *new_id)
5172 const struct got_error *err;
5173 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5175 err = got_object_id_str(&old_id_str, old_id);
5180 err = got_object_id_str(&new_id_str, new_id);
5185 old_id_str[12] = '\0';
5187 new_id_str[12] = '\0';
5189 err = get_short_logmsg(&logmsg, 42, commit);
5193 printf("%s -> %s: %s\n", old_id_str,
5194 new_id_str ? new_id_str : "no-op change", logmsg);
5201 static const struct got_error *
5202 rebase_progress(void *arg, unsigned char status, const char *path)
5204 unsigned char *rebase_status = arg;
5206 while (path[0] == '/')
5208 printf("%c %s\n", status, path);
5210 if (*rebase_status == GOT_STATUS_CONFLICT)
5212 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5213 *rebase_status = status;
5217 static const struct got_error *
5218 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5219 struct got_reference *branch, struct got_reference *new_base_branch,
5220 struct got_reference *tmp_branch, struct got_repository *repo)
5222 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5223 return got_worktree_rebase_complete(worktree, fileindex,
5224 new_base_branch, tmp_branch, branch, repo);
5227 static const struct got_error *
5228 rebase_commit(struct got_pathlist_head *merged_paths,
5229 struct got_worktree *worktree, struct got_fileindex *fileindex,
5230 struct got_reference *tmp_branch,
5231 struct got_object_id *commit_id, struct got_repository *repo)
5233 const struct got_error *error;
5234 struct got_commit_object *commit;
5235 struct got_object_id *new_commit_id;
5237 error = got_object_open_as_commit(&commit, repo, commit_id);
5241 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5242 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5244 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5246 error = show_rebase_progress(commit, commit_id, NULL);
5248 error = show_rebase_progress(commit, commit_id, new_commit_id);
5249 free(new_commit_id);
5252 got_object_commit_close(commit);
5256 struct check_path_prefix_arg {
5257 const char *path_prefix;
5262 static const struct got_error *
5263 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5264 struct got_blob_object *blob2, struct got_object_id *id1,
5265 struct got_object_id *id2, const char *path1, const char *path2,
5266 mode_t mode1, mode_t mode2, struct got_repository *repo)
5268 struct check_path_prefix_arg *a = arg;
5270 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5271 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5272 return got_error(a->errcode);
5277 static const struct got_error *
5278 check_path_prefix(struct got_object_id *parent_id,
5279 struct got_object_id *commit_id, const char *path_prefix,
5280 int errcode, struct got_repository *repo)
5282 const struct got_error *err;
5283 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5284 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5285 struct check_path_prefix_arg cpp_arg;
5287 if (got_path_is_root_dir(path_prefix))
5290 err = got_object_open_as_commit(&commit, repo, commit_id);
5294 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5298 err = got_object_open_as_tree(&tree1, repo,
5299 got_object_commit_get_tree_id(parent_commit));
5303 err = got_object_open_as_tree(&tree2, repo,
5304 got_object_commit_get_tree_id(commit));
5308 cpp_arg.path_prefix = path_prefix;
5309 while (cpp_arg.path_prefix[0] == '/')
5310 cpp_arg.path_prefix++;
5311 cpp_arg.len = strlen(cpp_arg.path_prefix);
5312 cpp_arg.errcode = errcode;
5313 err = got_diff_tree(tree1, tree2, "", "", repo,
5314 check_path_prefix_in_diff, &cpp_arg, 0);
5317 got_object_tree_close(tree1);
5319 got_object_tree_close(tree2);
5321 got_object_commit_close(commit);
5323 got_object_commit_close(parent_commit);
5327 static const struct got_error *
5328 collect_commits(struct got_object_id_queue *commits,
5329 struct got_object_id *initial_commit_id,
5330 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5331 const char *path_prefix, int path_prefix_errcode,
5332 struct got_repository *repo)
5334 const struct got_error *err = NULL;
5335 struct got_commit_graph *graph = NULL;
5336 struct got_object_id *parent_id = NULL;
5337 struct got_object_qid *qid;
5338 struct got_object_id *commit_id = initial_commit_id;
5340 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5344 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5345 check_cancelled, NULL);
5348 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5349 err = got_commit_graph_iter_next(&parent_id, graph);
5351 if (err->code == GOT_ERR_ITER_COMPLETED) {
5352 err = got_error_msg(GOT_ERR_ANCESTRY,
5353 "ran out of commits to rebase before "
5354 "youngest common ancestor commit has "
5357 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5359 err = got_commit_graph_fetch_commits(graph, 1, repo,
5360 check_cancelled, NULL);
5364 err = check_path_prefix(parent_id, commit_id,
5365 path_prefix, path_prefix_errcode, repo);
5369 err = got_object_qid_alloc(&qid, commit_id);
5372 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5373 commit_id = parent_id;
5377 got_commit_graph_close(graph);
5381 static const struct got_error *
5382 cmd_rebase(int argc, char *argv[])
5384 const struct got_error *error = NULL;
5385 struct got_worktree *worktree = NULL;
5386 struct got_repository *repo = NULL;
5387 struct got_fileindex *fileindex = NULL;
5389 struct got_reference *branch = NULL;
5390 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5391 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5392 struct got_object_id *resume_commit_id = NULL;
5393 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5394 struct got_commit_object *commit = NULL;
5395 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5396 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5397 struct got_object_id_queue commits;
5398 struct got_pathlist_head merged_paths;
5399 const struct got_object_id_queue *parent_ids;
5400 struct got_object_qid *qid, *pid;
5402 SIMPLEQ_INIT(&commits);
5403 TAILQ_INIT(&merged_paths);
5405 while ((ch = getopt(argc, argv, "ac")) != -1) {
5411 continue_rebase = 1;
5423 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5424 "unveil", NULL) == -1)
5427 if (abort_rebase && continue_rebase)
5429 else if (abort_rebase || continue_rebase) {
5432 } else if (argc != 1)
5435 cwd = getcwd(NULL, 0);
5437 error = got_error_from_errno("getcwd");
5440 error = got_worktree_open(&worktree, cwd);
5444 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5449 error = apply_unveil(got_repo_get_path(repo), 0,
5450 got_worktree_get_root_path(worktree));
5454 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5460 if (!rebase_in_progress) {
5461 error = got_error(GOT_ERR_NOT_REBASING);
5464 error = got_worktree_rebase_continue(&resume_commit_id,
5465 &new_base_branch, &tmp_branch, &branch, &fileindex,
5469 printf("Switching work tree to %s\n",
5470 got_ref_get_symref_target(new_base_branch));
5471 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5472 new_base_branch, update_progress, &did_something);
5475 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5476 goto done; /* nothing else to do */
5479 if (continue_rebase) {
5480 if (!rebase_in_progress) {
5481 error = got_error(GOT_ERR_NOT_REBASING);
5484 error = got_worktree_rebase_continue(&resume_commit_id,
5485 &new_base_branch, &tmp_branch, &branch, &fileindex,
5490 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5491 resume_commit_id, repo);
5495 yca_id = got_object_id_dup(resume_commit_id);
5496 if (yca_id == NULL) {
5497 error = got_error_from_errno("got_object_id_dup");
5501 error = got_ref_open(&branch, repo, argv[0], 0);
5506 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5510 if (!continue_rebase) {
5511 struct got_object_id *base_commit_id;
5513 base_commit_id = got_worktree_get_base_commit_id(worktree);
5514 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5515 base_commit_id, branch_head_commit_id, repo,
5516 check_cancelled, NULL);
5519 if (yca_id == NULL) {
5520 error = got_error_msg(GOT_ERR_ANCESTRY,
5521 "specified branch shares no common ancestry "
5522 "with work tree's branch");
5526 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5528 if (error->code != GOT_ERR_ANCESTRY)
5532 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5533 "specified branch resolves to a commit which "
5534 "is already contained in work tree's branch");
5537 error = got_worktree_rebase_prepare(&new_base_branch,
5538 &tmp_branch, &fileindex, worktree, branch, repo);
5543 commit_id = branch_head_commit_id;
5544 error = got_object_open_as_commit(&commit, repo, commit_id);
5548 parent_ids = got_object_commit_get_parent_ids(commit);
5549 pid = SIMPLEQ_FIRST(parent_ids);
5551 if (!continue_rebase) {
5553 error = got_worktree_rebase_abort(worktree, fileindex,
5554 repo, new_base_branch, update_progress,
5558 printf("Rebase of %s aborted\n",
5559 got_ref_get_name(branch));
5561 error = got_error(GOT_ERR_EMPTY_REBASE);
5564 error = collect_commits(&commits, commit_id, pid->id,
5565 yca_id, got_worktree_get_path_prefix(worktree),
5566 GOT_ERR_REBASE_PATH, repo);
5567 got_object_commit_close(commit);
5572 if (SIMPLEQ_EMPTY(&commits)) {
5573 if (continue_rebase) {
5574 error = rebase_complete(worktree, fileindex,
5575 branch, new_base_branch, tmp_branch, repo);
5578 /* Fast-forward the reference of the branch. */
5579 struct got_object_id *new_head_commit_id;
5581 error = got_ref_resolve(&new_head_commit_id, repo,
5585 error = got_object_id_str(&id_str, new_head_commit_id);
5586 printf("Forwarding %s to commit %s\n",
5587 got_ref_get_name(branch), id_str);
5589 error = got_ref_change_ref(branch,
5590 new_head_commit_id);
5597 SIMPLEQ_FOREACH(qid, &commits, entry) {
5598 commit_id = qid->id;
5599 parent_id = pid ? pid->id : yca_id;
5602 error = got_worktree_rebase_merge_files(&merged_paths,
5603 worktree, fileindex, parent_id, commit_id, repo,
5604 rebase_progress, &rebase_status, check_cancelled, NULL);
5608 if (rebase_status == GOT_STATUS_CONFLICT) {
5609 got_worktree_rebase_pathlist_free(&merged_paths);
5613 error = rebase_commit(&merged_paths, worktree, fileindex,
5614 tmp_branch, commit_id, repo);
5615 got_worktree_rebase_pathlist_free(&merged_paths);
5620 if (rebase_status == GOT_STATUS_CONFLICT) {
5621 error = got_worktree_rebase_postpone(worktree, fileindex);
5624 error = got_error_msg(GOT_ERR_CONFLICTS,
5625 "conflicts must be resolved before rebasing can continue");
5627 error = rebase_complete(worktree, fileindex, branch,
5628 new_base_branch, tmp_branch, repo);
5630 got_object_id_queue_free(&commits);
5631 free(branch_head_commit_id);
5632 free(resume_commit_id);
5635 got_object_commit_close(commit);
5637 got_ref_close(branch);
5638 if (new_base_branch)
5639 got_ref_close(new_base_branch);
5641 got_ref_close(tmp_branch);
5643 got_worktree_close(worktree);
5645 got_repo_close(repo);
5650 usage_histedit(void)
5652 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5657 #define GOT_HISTEDIT_PICK 'p'
5658 #define GOT_HISTEDIT_EDIT 'e'
5659 #define GOT_HISTEDIT_FOLD 'f'
5660 #define GOT_HISTEDIT_DROP 'd'
5661 #define GOT_HISTEDIT_MESG 'm'
5663 static struct got_histedit_cmd {
5667 } got_histedit_cmds[] = {
5668 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5669 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5670 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5671 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5672 { GOT_HISTEDIT_MESG, "mesg",
5673 "single-line log message for commit above (open editor if empty)" },
5676 struct got_histedit_list_entry {
5677 TAILQ_ENTRY(got_histedit_list_entry) entry;
5678 struct got_object_id *commit_id;
5679 const struct got_histedit_cmd *cmd;
5682 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5684 static const struct got_error *
5685 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5686 FILE *f, struct got_repository *repo)
5688 const struct got_error *err = NULL;
5689 char *logmsg = NULL, *id_str = NULL;
5690 struct got_commit_object *commit = NULL;
5693 err = got_object_open_as_commit(&commit, repo, commit_id);
5697 err = get_short_logmsg(&logmsg, 34, commit);
5701 err = got_object_id_str(&id_str, commit_id);
5705 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5707 err = got_ferror(f, GOT_ERR_IO);
5710 got_object_commit_close(commit);
5716 static const struct got_error *
5717 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5718 struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 struct got_object_qid *qid;
5723 if (SIMPLEQ_EMPTY(commits))
5724 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5726 SIMPLEQ_FOREACH(qid, commits, entry) {
5727 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5736 static const struct got_error *
5737 write_cmd_list(FILE *f)
5739 const struct got_error *err = NULL;
5742 n = fprintf(f, "# Available histedit commands:\n");
5744 return got_ferror(f, GOT_ERR_IO);
5746 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5747 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5748 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5751 err = got_ferror(f, GOT_ERR_IO);
5755 n = fprintf(f, "# Commits will be processed in order from top to "
5756 "bottom of this file.\n");
5758 return got_ferror(f, GOT_ERR_IO);
5762 static const struct got_error *
5763 histedit_syntax_error(int lineno)
5765 static char msg[42];
5768 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5770 if (ret == -1 || ret >= sizeof(msg))
5771 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5773 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5776 static const struct got_error *
5777 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5778 char *logmsg, struct got_repository *repo)
5780 const struct got_error *err;
5781 struct got_commit_object *folded_commit = NULL;
5782 char *id_str, *folded_logmsg = NULL;
5784 err = got_object_id_str(&id_str, hle->commit_id);
5788 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5792 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5795 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5796 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5797 folded_logmsg) == -1) {
5798 err = got_error_from_errno("asprintf");
5803 got_object_commit_close(folded_commit);
5805 free(folded_logmsg);
5809 static struct got_histedit_list_entry *
5810 get_folded_commits(struct got_histedit_list_entry *hle)
5812 struct got_histedit_list_entry *prev, *folded = NULL;
5814 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5815 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5816 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5817 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5819 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5825 static const struct got_error *
5826 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5827 struct got_repository *repo)
5829 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5830 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5831 const struct got_error *err = NULL;
5832 struct got_commit_object *commit = NULL;
5834 struct got_histedit_list_entry *folded = NULL;
5836 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5840 folded = get_folded_commits(hle);
5842 while (folded != hle) {
5843 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5844 folded = TAILQ_NEXT(folded, entry);
5847 err = append_folded_commit_msg(&new_msg, folded,
5853 folded = TAILQ_NEXT(folded, entry);
5857 err = got_object_id_str(&id_str, hle->commit_id);
5860 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5863 if (asprintf(&new_msg,
5864 "%s\n# original log message of commit %s: %s",
5865 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5866 err = got_error_from_errno("asprintf");
5872 err = got_object_id_str(&id_str, hle->commit_id);
5876 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5880 dprintf(fd, logmsg);
5883 err = get_editor(&editor);
5887 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5889 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5891 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5894 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5895 err = got_error_from_errno2("unlink", logmsg_path);
5901 got_object_commit_close(commit);
5905 static const struct got_error *
5906 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5907 FILE *f, struct got_repository *repo)
5909 const struct got_error *err = NULL;
5910 char *line = NULL, *p, *end;
5914 const struct got_histedit_cmd *cmd;
5915 struct got_object_id *commit_id = NULL;
5916 struct got_histedit_list_entry *hle = NULL;
5919 len = getline(&line, &size, f);
5921 const struct got_error *getline_err;
5924 getline_err = got_error_from_errno("getline");
5925 err = got_ferror(f, getline_err->code);
5930 while (isspace((unsigned char)p[0]))
5932 if (p[0] == '#' || p[0] == '\0') {
5938 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5939 cmd = &got_histedit_cmds[i];
5940 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5941 isspace((unsigned char)p[strlen(cmd->name)])) {
5942 p += strlen(cmd->name);
5945 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5950 if (i == nitems(got_histedit_cmds)) {
5951 err = histedit_syntax_error(lineno);
5954 while (isspace((unsigned char)p[0]))
5956 if (cmd->code == GOT_HISTEDIT_MESG) {
5957 if (hle == NULL || hle->logmsg != NULL) {
5958 err = got_error(GOT_ERR_HISTEDIT_CMD);
5962 err = histedit_edit_logmsg(hle, repo);
5966 hle->logmsg = strdup(p);
5967 if (hle->logmsg == NULL) {
5968 err = got_error_from_errno("strdup");
5977 while (end[0] && !isspace((unsigned char)end[0]))
5981 err = got_object_resolve_id_str(&commit_id, repo, p);
5983 /* override error code */
5984 err = histedit_syntax_error(lineno);
5988 hle = malloc(sizeof(*hle));
5990 err = got_error_from_errno("malloc");
5994 hle->commit_id = commit_id;
5999 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6007 static const struct got_error *
6008 histedit_check_script(struct got_histedit_list *histedit_cmds,
6009 struct got_object_id_queue *commits, struct got_repository *repo)
6011 const struct got_error *err = NULL;
6012 struct got_object_qid *qid;
6013 struct got_histedit_list_entry *hle;
6014 static char msg[80];
6017 if (TAILQ_EMPTY(histedit_cmds))
6018 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6019 "histedit script contains no commands");
6020 if (SIMPLEQ_EMPTY(commits))
6021 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6023 SIMPLEQ_FOREACH(qid, commits, entry) {
6024 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6025 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6029 err = got_object_id_str(&id_str, qid->id);
6032 snprintf(msg, sizeof(msg),
6033 "commit %s missing from histedit script", id_str);
6035 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6039 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6040 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6041 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6042 "last commit in histedit script cannot be folded");
6047 static const struct got_error *
6048 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6049 const char *path, struct got_object_id_queue *commits,
6050 struct got_repository *repo)
6052 const struct got_error *err = NULL;
6056 err = get_editor(&editor);
6060 if (spawn_editor(editor, path) == -1) {
6061 err = got_error_from_errno("failed spawning editor");
6065 f = fopen(path, "r");
6067 err = got_error_from_errno("fopen");
6070 err = histedit_parse_list(histedit_cmds, f, repo);
6074 err = histedit_check_script(histedit_cmds, commits, repo);
6076 if (f && fclose(f) != 0 && err == NULL)
6077 err = got_error_from_errno("fclose");
6082 static const struct got_error *
6083 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6084 struct got_object_id_queue *, const char *, struct got_repository *);
6086 static const struct got_error *
6087 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6088 struct got_object_id_queue *commits, struct got_repository *repo)
6090 const struct got_error *err;
6094 err = got_opentemp_named(&path, &f, "got-histedit");
6098 err = write_cmd_list(f);
6102 err = histedit_write_commit_list(commits, f, repo);
6106 if (fclose(f) != 0) {
6107 err = got_error_from_errno("fclose");
6112 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6114 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6115 err->code != GOT_ERR_HISTEDIT_CMD)
6117 err = histedit_edit_list_retry(histedit_cmds, err,
6118 commits, path, repo);
6121 if (f && fclose(f) != 0 && err == NULL)
6122 err = got_error_from_errno("fclose");
6123 if (path && unlink(path) != 0 && err == NULL)
6124 err = got_error_from_errno2("unlink", path);
6129 static const struct got_error *
6130 histedit_save_list(struct got_histedit_list *histedit_cmds,
6131 struct got_worktree *worktree, struct got_repository *repo)
6133 const struct got_error *err = NULL;
6136 struct got_histedit_list_entry *hle;
6137 struct got_commit_object *commit = NULL;
6139 err = got_worktree_get_histedit_script_path(&path, worktree);
6143 f = fopen(path, "w");
6145 err = got_error_from_errno2("fopen", path);
6148 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6149 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6155 int n = fprintf(f, "%c %s\n",
6156 GOT_HISTEDIT_MESG, hle->logmsg);
6158 err = got_ferror(f, GOT_ERR_IO);
6164 if (f && fclose(f) != 0 && err == NULL)
6165 err = got_error_from_errno("fclose");
6168 got_object_commit_close(commit);
6173 histedit_free_list(struct got_histedit_list *histedit_cmds)
6175 struct got_histedit_list_entry *hle;
6177 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6178 TAILQ_REMOVE(histedit_cmds, hle, entry);
6183 static const struct got_error *
6184 histedit_load_list(struct got_histedit_list *histedit_cmds,
6185 const char *path, struct got_repository *repo)
6187 const struct got_error *err = NULL;
6190 f = fopen(path, "r");
6192 err = got_error_from_errno2("fopen", path);
6196 err = histedit_parse_list(histedit_cmds, f, repo);
6198 if (f && fclose(f) != 0 && err == NULL)
6199 err = got_error_from_errno("fclose");
6203 static const struct got_error *
6204 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6205 const struct got_error *edit_err, struct got_object_id_queue *commits,
6206 const char *path, struct got_repository *repo)
6208 const struct got_error *err = NULL, *prev_err = edit_err;
6211 while (resp != 'c' && resp != 'r' && resp != 'a') {
6212 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6213 "or (a)bort: ", getprogname(), prev_err->msg);
6218 histedit_free_list(histedit_cmds);
6219 err = histedit_run_editor(histedit_cmds, path, commits,
6222 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6223 err->code != GOT_ERR_HISTEDIT_CMD)
6230 } else if (resp == 'r') {
6231 histedit_free_list(histedit_cmds);
6232 err = histedit_edit_script(histedit_cmds,
6235 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6236 err->code != GOT_ERR_HISTEDIT_CMD)
6243 } else if (resp == 'a') {
6244 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6247 printf("invalid response '%c'\n", resp);
6253 static const struct got_error *
6254 histedit_complete(struct got_worktree *worktree,
6255 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6256 struct got_reference *branch, struct got_repository *repo)
6258 printf("Switching work tree to %s\n",
6259 got_ref_get_symref_target(branch));
6260 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6264 static const struct got_error *
6265 show_histedit_progress(struct got_commit_object *commit,
6266 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6268 const struct got_error *err;
6269 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6271 err = got_object_id_str(&old_id_str, hle->commit_id);
6276 err = got_object_id_str(&new_id_str, new_id);
6281 old_id_str[12] = '\0';
6283 new_id_str[12] = '\0';
6286 logmsg = strdup(hle->logmsg);
6287 if (logmsg == NULL) {
6288 err = got_error_from_errno("strdup");
6291 trim_logmsg(logmsg, 42);
6293 err = get_short_logmsg(&logmsg, 42, commit);
6298 switch (hle->cmd->code) {
6299 case GOT_HISTEDIT_PICK:
6300 case GOT_HISTEDIT_EDIT:
6301 printf("%s -> %s: %s\n", old_id_str,
6302 new_id_str ? new_id_str : "no-op change", logmsg);
6304 case GOT_HISTEDIT_DROP:
6305 case GOT_HISTEDIT_FOLD:
6306 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6319 static const struct got_error *
6320 histedit_commit(struct got_pathlist_head *merged_paths,
6321 struct got_worktree *worktree, struct got_fileindex *fileindex,
6322 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6323 struct got_repository *repo)
6325 const struct got_error *err;
6326 struct got_commit_object *commit;
6327 struct got_object_id *new_commit_id;
6329 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6330 && hle->logmsg == NULL) {
6331 err = histedit_edit_logmsg(hle, repo);
6336 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6340 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6341 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6344 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6346 err = show_histedit_progress(commit, hle, NULL);
6348 err = show_histedit_progress(commit, hle, new_commit_id);
6349 free(new_commit_id);
6352 got_object_commit_close(commit);
6356 static const struct got_error *
6357 histedit_skip_commit(struct got_histedit_list_entry *hle,
6358 struct got_worktree *worktree, struct got_repository *repo)
6360 const struct got_error *error;
6361 struct got_commit_object *commit;
6363 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6368 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6372 error = show_histedit_progress(commit, hle, NULL);
6373 got_object_commit_close(commit);
6377 static const struct got_error *
6378 cmd_histedit(int argc, char *argv[])
6380 const struct got_error *error = NULL;
6381 struct got_worktree *worktree = NULL;
6382 struct got_fileindex *fileindex = NULL;
6383 struct got_repository *repo = NULL;
6385 struct got_reference *branch = NULL;
6386 struct got_reference *tmp_branch = NULL;
6387 struct got_object_id *resume_commit_id = NULL;
6388 struct got_object_id *base_commit_id = NULL;
6389 struct got_object_id *head_commit_id = NULL;
6390 struct got_commit_object *commit = NULL;
6391 int ch, rebase_in_progress = 0, did_something;
6392 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6393 const char *edit_script_path = NULL;
6394 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6395 struct got_object_id_queue commits;
6396 struct got_pathlist_head merged_paths;
6397 const struct got_object_id_queue *parent_ids;
6398 struct got_object_qid *pid;
6399 struct got_histedit_list histedit_cmds;
6400 struct got_histedit_list_entry *hle;
6402 SIMPLEQ_INIT(&commits);
6403 TAILQ_INIT(&histedit_cmds);
6404 TAILQ_INIT(&merged_paths);
6406 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6415 edit_script_path = optarg;
6427 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6428 "unveil", NULL) == -1)
6431 if (abort_edit && continue_edit)
6437 * This command cannot apply unveil(2) in all cases because the
6438 * user may choose to run an editor to edit the histedit script
6439 * and to edit individual commit log messages.
6440 * unveil(2) traverses exec(2); if an editor is used we have to
6441 * apply unveil after edit script and log messages have been written.
6442 * XXX TODO: Make use of unveil(2) where possible.
6445 cwd = getcwd(NULL, 0);
6447 error = got_error_from_errno("getcwd");
6450 error = got_worktree_open(&worktree, cwd);
6454 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6459 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6462 if (rebase_in_progress) {
6463 error = got_error(GOT_ERR_REBASING);
6467 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6471 if (edit_in_progress && abort_edit) {
6472 error = got_worktree_histedit_continue(&resume_commit_id,
6473 &tmp_branch, &branch, &base_commit_id, &fileindex,
6477 printf("Switching work tree to %s\n",
6478 got_ref_get_symref_target(branch));
6479 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6480 branch, base_commit_id, update_progress, &did_something);
6483 printf("Histedit of %s aborted\n",
6484 got_ref_get_symref_target(branch));
6485 goto done; /* nothing else to do */
6486 } else if (abort_edit) {
6487 error = got_error(GOT_ERR_NOT_HISTEDIT);
6491 if (continue_edit) {
6494 if (!edit_in_progress) {
6495 error = got_error(GOT_ERR_NOT_HISTEDIT);
6499 error = got_worktree_get_histedit_script_path(&path, worktree);
6503 error = histedit_load_list(&histedit_cmds, path, repo);
6508 error = got_worktree_histedit_continue(&resume_commit_id,
6509 &tmp_branch, &branch, &base_commit_id, &fileindex,
6514 error = got_ref_resolve(&head_commit_id, repo, branch);
6518 error = got_object_open_as_commit(&commit, repo,
6522 parent_ids = got_object_commit_get_parent_ids(commit);
6523 pid = SIMPLEQ_FIRST(parent_ids);
6525 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6528 error = collect_commits(&commits, head_commit_id, pid->id,
6529 base_commit_id, got_worktree_get_path_prefix(worktree),
6530 GOT_ERR_HISTEDIT_PATH, repo);
6531 got_object_commit_close(commit);
6536 if (edit_in_progress) {
6537 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6541 error = got_ref_open(&branch, repo,
6542 got_worktree_get_head_ref_name(worktree), 0);
6546 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6547 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6548 "will not edit commit history of a branch outside "
6549 "the \"refs/heads/\" reference namespace");
6553 error = got_ref_resolve(&head_commit_id, repo, branch);
6554 got_ref_close(branch);
6559 error = got_object_open_as_commit(&commit, repo,
6563 parent_ids = got_object_commit_get_parent_ids(commit);
6564 pid = SIMPLEQ_FIRST(parent_ids);
6566 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6569 error = collect_commits(&commits, head_commit_id, pid->id,
6570 got_worktree_get_base_commit_id(worktree),
6571 got_worktree_get_path_prefix(worktree),
6572 GOT_ERR_HISTEDIT_PATH, repo);
6573 got_object_commit_close(commit);
6578 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6579 &base_commit_id, &fileindex, worktree, repo);
6583 if (edit_script_path) {
6584 error = histedit_load_list(&histedit_cmds,
6585 edit_script_path, repo);
6587 got_worktree_histedit_abort(worktree, fileindex,
6588 repo, branch, base_commit_id,
6589 update_progress, &did_something);
6593 error = histedit_edit_script(&histedit_cmds, &commits,
6596 got_worktree_histedit_abort(worktree, fileindex,
6597 repo, branch, base_commit_id,
6598 update_progress, &did_something);
6604 error = histedit_save_list(&histedit_cmds, worktree,
6607 got_worktree_histedit_abort(worktree, fileindex,
6608 repo, branch, base_commit_id,
6609 update_progress, &did_something);
6615 error = histedit_check_script(&histedit_cmds, &commits, repo);
6619 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6620 if (resume_commit_id) {
6621 if (got_object_id_cmp(hle->commit_id,
6622 resume_commit_id) != 0)
6625 resume_commit_id = NULL;
6626 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6627 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6628 error = histedit_skip_commit(hle, worktree,
6631 error = histedit_commit(NULL, worktree,
6632 fileindex, tmp_branch, hle, repo);
6639 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6640 error = histedit_skip_commit(hle, worktree, repo);
6646 error = got_object_open_as_commit(&commit, repo,
6650 parent_ids = got_object_commit_get_parent_ids(commit);
6651 pid = SIMPLEQ_FIRST(parent_ids);
6653 error = got_worktree_histedit_merge_files(&merged_paths,
6654 worktree, fileindex, pid->id, hle->commit_id, repo,
6655 rebase_progress, &rebase_status, check_cancelled, NULL);
6658 got_object_commit_close(commit);
6661 if (rebase_status == GOT_STATUS_CONFLICT) {
6662 got_worktree_rebase_pathlist_free(&merged_paths);
6666 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6668 error = got_object_id_str(&id_str, hle->commit_id);
6671 printf("Stopping histedit for amending commit %s\n",
6674 got_worktree_rebase_pathlist_free(&merged_paths);
6675 error = got_worktree_histedit_postpone(worktree,
6680 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6681 error = histedit_skip_commit(hle, worktree, repo);
6687 error = histedit_commit(&merged_paths, worktree, fileindex,
6688 tmp_branch, hle, repo);
6689 got_worktree_rebase_pathlist_free(&merged_paths);
6694 if (rebase_status == GOT_STATUS_CONFLICT) {
6695 error = got_worktree_histedit_postpone(worktree, fileindex);
6698 error = got_error_msg(GOT_ERR_CONFLICTS,
6699 "conflicts must be resolved before rebasing can continue");
6701 error = histedit_complete(worktree, fileindex, tmp_branch,
6704 got_object_id_queue_free(&commits);
6705 histedit_free_list(&histedit_cmds);
6706 free(head_commit_id);
6707 free(base_commit_id);
6708 free(resume_commit_id);
6710 got_object_commit_close(commit);
6712 got_ref_close(branch);
6714 got_ref_close(tmp_branch);
6716 got_worktree_close(worktree);
6718 got_repo_close(repo);
6723 usage_integrate(void)
6725 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6729 static const struct got_error *
6730 cmd_integrate(int argc, char *argv[])
6732 const struct got_error *error = NULL;
6733 struct got_repository *repo = NULL;
6734 struct got_worktree *worktree = NULL;
6735 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6736 const char *branch_arg = NULL;
6737 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6738 struct got_fileindex *fileindex = NULL;
6739 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6740 int ch, did_something = 0;
6742 while ((ch = getopt(argc, argv, "")) != -1) {
6755 branch_arg = argv[0];
6757 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6758 "unveil", NULL) == -1)
6761 cwd = getcwd(NULL, 0);
6763 error = got_error_from_errno("getcwd");
6767 error = got_worktree_open(&worktree, cwd);
6771 error = check_rebase_or_histedit_in_progress(worktree);
6775 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6780 error = apply_unveil(got_repo_get_path(repo), 0,
6781 got_worktree_get_root_path(worktree));
6785 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6786 error = got_error_from_errno("asprintf");
6790 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6791 &base_branch_ref, worktree, refname, repo);
6795 refname = strdup(got_ref_get_name(branch_ref));
6796 if (refname == NULL) {
6797 error = got_error_from_errno("strdup");
6798 got_worktree_integrate_abort(worktree, fileindex, repo,
6799 branch_ref, base_branch_ref);
6802 base_refname = strdup(got_ref_get_name(base_branch_ref));
6803 if (base_refname == NULL) {
6804 error = got_error_from_errno("strdup");
6805 got_worktree_integrate_abort(worktree, fileindex, repo,
6806 branch_ref, base_branch_ref);
6810 error = got_ref_resolve(&commit_id, repo, branch_ref);
6814 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6818 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6819 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6820 "specified branch has already been integrated");
6821 got_worktree_integrate_abort(worktree, fileindex, repo,
6822 branch_ref, base_branch_ref);
6826 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6828 if (error->code == GOT_ERR_ANCESTRY)
6829 error = got_error(GOT_ERR_REBASE_REQUIRED);
6830 got_worktree_integrate_abort(worktree, fileindex, repo,
6831 branch_ref, base_branch_ref);
6835 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6836 branch_ref, base_branch_ref, update_progress, &did_something,
6837 check_cancelled, NULL);
6841 printf("Integrated %s into %s\n", refname, base_refname);
6844 got_repo_close(repo);
6846 got_worktree_close(worktree);
6848 free(base_commit_id);
6858 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6859 "[file-path ...]\n",
6864 static const struct got_error *
6865 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6866 const char *path, struct got_object_id *blob_id,
6867 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6868 int dirfd, const char *de_name)
6870 const struct got_error *err = NULL;
6871 char *id_str = NULL;
6873 if (staged_status != GOT_STATUS_ADD &&
6874 staged_status != GOT_STATUS_MODIFY &&
6875 staged_status != GOT_STATUS_DELETE)
6878 if (staged_status == GOT_STATUS_ADD ||
6879 staged_status == GOT_STATUS_MODIFY)
6880 err = got_object_id_str(&id_str, staged_blob_id);
6882 err = got_object_id_str(&id_str, blob_id);
6886 printf("%s %c %s\n", id_str, staged_status, path);
6891 static const struct got_error *
6892 cmd_stage(int argc, char *argv[])
6894 const struct got_error *error = NULL;
6895 struct got_repository *repo = NULL;
6896 struct got_worktree *worktree = NULL;
6898 struct got_pathlist_head paths;
6899 struct got_pathlist_entry *pe;
6900 int ch, list_stage = 0, pflag = 0;
6901 FILE *patch_script_file = NULL;
6902 const char *patch_script_path = NULL;
6903 struct choose_patch_arg cpa;
6907 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6916 patch_script_path = optarg;
6928 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6929 "unveil", NULL) == -1)
6932 if (list_stage && (pflag || patch_script_path))
6933 errx(1, "-l option cannot be used with other options");
6934 if (patch_script_path && !pflag)
6935 errx(1, "-F option can only be used together with -p option");
6937 cwd = getcwd(NULL, 0);
6939 error = got_error_from_errno("getcwd");
6943 error = got_worktree_open(&worktree, cwd);
6947 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6952 if (patch_script_path) {
6953 patch_script_file = fopen(patch_script_path, "r");
6954 if (patch_script_file == NULL) {
6955 error = got_error_from_errno2("fopen",
6960 error = apply_unveil(got_repo_get_path(repo), 0,
6961 got_worktree_get_root_path(worktree));
6965 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6970 error = got_worktree_status(worktree, &paths, repo,
6971 print_stage, NULL, check_cancelled, NULL);
6973 cpa.patch_script_file = patch_script_file;
6974 cpa.action = "stage";
6975 error = got_worktree_stage(worktree, &paths,
6976 pflag ? NULL : print_status, NULL,
6977 pflag ? choose_patch : NULL, &cpa, repo);
6980 if (patch_script_file && fclose(patch_script_file) == EOF &&
6982 error = got_error_from_errno2("fclose", patch_script_path);
6984 got_repo_close(repo);
6986 got_worktree_close(worktree);
6987 TAILQ_FOREACH(pe, &paths, entry)
6988 free((char *)pe->path);
6989 got_pathlist_free(&paths);
6997 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6998 "[file-path ...]\n",
7004 static const struct got_error *
7005 cmd_unstage(int argc, char *argv[])
7007 const struct got_error *error = NULL;
7008 struct got_repository *repo = NULL;
7009 struct got_worktree *worktree = NULL;
7011 struct got_pathlist_head paths;
7012 struct got_pathlist_entry *pe;
7013 int ch, did_something = 0, pflag = 0;
7014 FILE *patch_script_file = NULL;
7015 const char *patch_script_path = NULL;
7016 struct choose_patch_arg cpa;
7020 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7026 patch_script_path = optarg;
7038 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7039 "unveil", NULL) == -1)
7042 if (patch_script_path && !pflag)
7043 errx(1, "-F option can only be used together with -p option");
7045 cwd = getcwd(NULL, 0);
7047 error = got_error_from_errno("getcwd");
7051 error = got_worktree_open(&worktree, cwd);
7055 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7060 if (patch_script_path) {
7061 patch_script_file = fopen(patch_script_path, "r");
7062 if (patch_script_file == NULL) {
7063 error = got_error_from_errno2("fopen",
7069 error = apply_unveil(got_repo_get_path(repo), 0,
7070 got_worktree_get_root_path(worktree));
7074 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7078 cpa.patch_script_file = patch_script_file;
7079 cpa.action = "unstage";
7080 error = got_worktree_unstage(worktree, &paths, update_progress,
7081 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7083 if (patch_script_file && fclose(patch_script_file) == EOF &&
7085 error = got_error_from_errno2("fclose", patch_script_path);
7087 got_repo_close(repo);
7089 got_worktree_close(worktree);
7090 TAILQ_FOREACH(pe, &paths, entry)
7091 free((char *)pe->path);
7092 got_pathlist_free(&paths);
7100 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7101 "arg1 [arg2 ...]\n", getprogname());
7105 static const struct got_error *
7106 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7108 const struct got_error *err;
7109 struct got_blob_object *blob;
7111 err = got_object_open_as_blob(&blob, repo, id, 8192);
7115 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7116 got_object_blob_close(blob);
7120 static const struct got_error *
7121 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7123 const struct got_error *err;
7124 struct got_tree_object *tree;
7127 err = got_object_open_as_tree(&tree, repo, id);
7131 nentries = got_object_tree_get_nentries(tree);
7132 for (i = 0; i < nentries; i++) {
7133 struct got_tree_entry *te;
7135 if (sigint_received || sigpipe_received)
7137 te = got_object_tree_get_entry(tree, i);
7138 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7141 fprintf(outfile, "%s %.7o %s\n", id_str,
7142 got_tree_entry_get_mode(te),
7143 got_tree_entry_get_name(te));
7147 got_object_tree_close(tree);
7151 static const struct got_error *
7152 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7154 const struct got_error *err;
7155 struct got_commit_object *commit;
7156 const struct got_object_id_queue *parent_ids;
7157 struct got_object_qid *pid;
7158 char *id_str = NULL;
7159 const char *logmsg = NULL;
7161 err = got_object_open_as_commit(&commit, repo, id);
7165 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7169 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7170 parent_ids = got_object_commit_get_parent_ids(commit);
7171 fprintf(outfile, "numparents %d\n",
7172 got_object_commit_get_nparents(commit));
7173 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7175 err = got_object_id_str(&pid_str, pid->id);
7178 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7181 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7182 got_object_commit_get_author(commit),
7183 got_object_commit_get_author_time(commit));
7185 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7186 got_object_commit_get_author(commit),
7187 got_object_commit_get_committer_time(commit));
7189 logmsg = got_object_commit_get_logmsg_raw(commit);
7190 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7191 fprintf(outfile, "%s", logmsg);
7194 got_object_commit_close(commit);
7198 static const struct got_error *
7199 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7201 const struct got_error *err;
7202 struct got_tag_object *tag;
7203 char *id_str = NULL;
7204 const char *tagmsg = NULL;
7206 err = got_object_open_as_tag(&tag, repo, id);
7210 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7214 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7216 switch (got_object_tag_get_object_type(tag)) {
7217 case GOT_OBJ_TYPE_BLOB:
7218 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7219 GOT_OBJ_LABEL_BLOB);
7221 case GOT_OBJ_TYPE_TREE:
7222 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7223 GOT_OBJ_LABEL_TREE);
7225 case GOT_OBJ_TYPE_COMMIT:
7226 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7227 GOT_OBJ_LABEL_COMMIT);
7229 case GOT_OBJ_TYPE_TAG:
7230 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7237 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7238 got_object_tag_get_name(tag));
7240 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7241 got_object_tag_get_tagger(tag),
7242 got_object_tag_get_tagger_time(tag));
7244 tagmsg = got_object_tag_get_message(tag);
7245 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7246 fprintf(outfile, "%s", tagmsg);
7249 got_object_tag_close(tag);
7253 static const struct got_error *
7254 cmd_cat(int argc, char *argv[])
7256 const struct got_error *error;
7257 struct got_repository *repo = NULL;
7258 struct got_worktree *worktree = NULL;
7259 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7260 const char *commit_id_str = NULL;
7261 struct got_object_id *id = NULL, *commit_id = NULL;
7262 int ch, obj_type, i, force_path = 0;
7265 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7270 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7273 commit_id_str = optarg;
7276 repo_path = realpath(optarg, NULL);
7277 if (repo_path == NULL)
7278 return got_error_from_errno2("realpath",
7280 got_path_strip_trailing_slashes(repo_path);
7294 cwd = getcwd(NULL, 0);
7296 error = got_error_from_errno("getcwd");
7299 error = got_worktree_open(&worktree, cwd);
7300 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7303 if (repo_path == NULL) {
7305 got_worktree_get_repo_path(worktree));
7306 if (repo_path == NULL) {
7307 error = got_error_from_errno("strdup");
7313 if (repo_path == NULL) {
7314 repo_path = getcwd(NULL, 0);
7315 if (repo_path == NULL)
7316 return got_error_from_errno("getcwd");
7319 error = got_repo_open(&repo, repo_path, NULL);
7324 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7328 if (commit_id_str == NULL)
7329 commit_id_str = GOT_REF_HEAD;
7330 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7334 for (i = 0; i < argc; i++) {
7336 error = got_object_id_by_path(&id, repo, commit_id,
7341 error = match_object_id(&id, &label, argv[i],
7342 GOT_OBJ_TYPE_ANY, 0, repo);
7344 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7345 error->code != GOT_ERR_NOT_REF)
7347 error = got_object_id_by_path(&id, repo,
7348 commit_id, argv[i]);
7354 error = got_object_get_type(&obj_type, repo, id);
7359 case GOT_OBJ_TYPE_BLOB:
7360 error = cat_blob(id, repo, stdout);
7362 case GOT_OBJ_TYPE_TREE:
7363 error = cat_tree(id, repo, stdout);
7365 case GOT_OBJ_TYPE_COMMIT:
7366 error = cat_commit(id, repo, stdout);
7368 case GOT_OBJ_TYPE_TAG:
7369 error = cat_tag(id, repo, stdout);
7372 error = got_error(GOT_ERR_OBJ_TYPE);
7388 got_worktree_close(worktree);
7390 const struct got_error *repo_error;
7391 repo_error = got_repo_close(repo);