2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
171 main(int argc, char *argv[])
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
199 got_version_print_str();
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && !(sigint_received || sigpipe_received)) {
223 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
230 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
238 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
245 static const struct got_error *
246 get_editor(char **abspath)
248 const struct got_error *err = NULL;
253 editor = getenv("VISUAL");
255 editor = getenv("EDITOR");
258 err = got_path_find_prog(abspath, editor);
263 if (*abspath == NULL) {
264 *abspath = strdup("/bin/ed");
265 if (*abspath == NULL)
266 return got_error_from_errno("strdup");
272 static const struct got_error *
273 apply_unveil(const char *repo_path, int repo_read_only,
274 const char *worktree_path)
276 const struct got_error *err;
279 if (unveil("gmon.out", "rwc") != 0)
280 return got_error_from_errno2("unveil", "gmon.out");
282 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 return got_error_from_errno2("unveil", repo_path);
285 if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 return got_error_from_errno2("unveil", worktree_path);
288 if (unveil("/tmp", "rwc") != 0)
289 return got_error_from_errno2("unveil", "/tmp");
291 err = got_privsep_unveil_exec_helpers();
295 if (unveil(NULL, NULL) != 0)
296 return got_error_from_errno("unveil");
304 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
308 static const struct got_error *
309 cmd_init(int argc, char *argv[])
311 const struct got_error *error = NULL;
312 char *repo_path = NULL;
315 while ((ch = getopt(argc, argv, "")) != -1) {
327 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
333 repo_path = strdup(argv[0]);
334 if (repo_path == NULL)
335 return got_error_from_errno("strdup");
337 got_path_strip_trailing_slashes(repo_path);
339 error = got_path_mkdir(repo_path);
341 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
344 error = apply_unveil(repo_path, 0, NULL);
348 error = got_repo_init(repo_path);
360 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 "[-r repository-path] [-I pattern] path\n", getprogname());
366 spawn_editor(const char *editor, const char *file)
369 sig_t sighup, sigint, sigquit;
372 sighup = signal(SIGHUP, SIG_IGN);
373 sigint = signal(SIGINT, SIG_IGN);
374 sigquit = signal(SIGQUIT, SIG_IGN);
376 switch (pid = fork()) {
380 execl(editor, editor, file, (char *)NULL);
384 while (waitpid(pid, &st, 0) == -1)
389 (void)signal(SIGHUP, sighup);
390 (void)signal(SIGINT, sigint);
391 (void)signal(SIGQUIT, sigquit);
393 if (!WIFEXITED(st)) {
398 return WEXITSTATUS(st);
401 static const struct got_error *
402 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 const char *initial_content)
405 const struct got_error *err = NULL;
409 int content_changed = 0;
414 if (stat(logmsg_path, &st) == -1)
415 return got_error_from_errno2("stat", logmsg_path);
417 if (spawn_editor(editor, logmsg_path) == -1)
418 return got_error_from_errno("failed spawning editor");
420 if (stat(logmsg_path, &st2) == -1)
421 return got_error_from_errno("stat");
423 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 "no changes made to commit message, aborting");
427 *logmsg = malloc(st2.st_size + 1);
429 return got_error_from_errno("malloc");
433 fp = fopen(logmsg_path, "r");
435 err = got_error_from_errno("fopen");
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
439 if (!content_changed && strcmp(buf, initial_content) != 0)
441 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 continue; /* remove comments and leading empty lines */
443 len = strlcat(*logmsg, buf, st2.st_size);
447 while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 (*logmsg)[len - 1] = '\0';
452 if (len == 0 || !content_changed)
453 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 "commit message cannot be empty, aborting");
463 static const struct got_error *
464 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
465 const char *path_dir, const char *branch_name)
467 char *initial_content = NULL;
468 const struct got_error *err = NULL;
471 if (asprintf(&initial_content,
472 "\n# %s to be imported to branch %s\n", path_dir,
474 return got_error_from_errno("asprintf");
476 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
480 dprintf(fd, initial_content);
483 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
485 free(initial_content);
489 static const struct got_error *
490 import_progress(void *arg, const char *path)
492 printf("A %s\n", path);
496 static const struct got_error *
497 get_author(char **author, struct got_repository *repo)
499 const struct got_error *err = NULL;
500 const char *got_author, *name, *email;
504 name = got_repo_get_gitconfig_author_name(repo);
505 email = got_repo_get_gitconfig_author_email(repo);
507 if (asprintf(author, "%s <%s>", name, email) == -1)
508 return got_error_from_errno("asprintf");
512 got_author = getenv("GOT_AUTHOR");
513 if (got_author == NULL) {
514 name = got_repo_get_global_gitconfig_author_name(repo);
515 email = got_repo_get_global_gitconfig_author_email(repo);
517 if (asprintf(author, "%s <%s>", name, email) == -1)
518 return got_error_from_errno("asprintf");
521 /* TODO: Look up user in password database? */
522 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
525 *author = strdup(got_author);
527 return got_error_from_errno("strdup");
530 * Really dumb email address check; we're only doing this to
531 * avoid git's object parser breaking on commits we create.
533 while (*got_author && *got_author != '<')
535 if (*got_author != '<') {
536 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
539 while (*got_author && *got_author != '@')
541 if (*got_author != '@') {
542 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
545 while (*got_author && *got_author != '>')
547 if (*got_author != '>')
548 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
557 static const struct got_error *
558 get_gitconfig_path(char **gitconfig_path)
560 const char *homedir = getenv("HOME");
562 *gitconfig_path = NULL;
564 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
565 return got_error_from_errno("asprintf");
571 static const struct got_error *
572 cmd_import(int argc, char *argv[])
574 const struct got_error *error = NULL;
575 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
576 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
577 const char *branch_name = "master";
578 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
579 struct got_repository *repo = NULL;
580 struct got_reference *branch_ref = NULL, *head_ref = NULL;
581 struct got_object_id *new_commit_id = NULL;
583 struct got_pathlist_head ignores;
584 struct got_pathlist_entry *pe;
585 int preserve_logmsg = 0;
587 TAILQ_INIT(&ignores);
589 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
592 branch_name = optarg;
595 logmsg = strdup(optarg);
596 if (logmsg == NULL) {
597 error = got_error_from_errno("strdup");
602 repo_path = realpath(optarg, NULL);
603 if (repo_path == NULL) {
604 error = got_error_from_errno2("realpath",
610 if (optarg[0] == '\0')
612 error = got_pathlist_insert(&pe, &ignores, optarg,
627 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
635 if (repo_path == NULL) {
636 repo_path = getcwd(NULL, 0);
637 if (repo_path == NULL)
638 return got_error_from_errno("getcwd");
640 got_path_strip_trailing_slashes(repo_path);
641 error = get_gitconfig_path(&gitconfig_path);
644 error = got_repo_open(&repo, repo_path, gitconfig_path);
648 error = get_author(&author, repo);
652 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
653 error = got_error_from_errno("asprintf");
657 error = got_ref_open(&branch_ref, repo, refname, 0);
659 if (error->code != GOT_ERR_NOT_REF)
662 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
663 "import target branch already exists");
667 path_dir = realpath(argv[0], NULL);
668 if (path_dir == NULL) {
669 error = got_error_from_errno2("realpath", argv[0]);
672 got_path_strip_trailing_slashes(path_dir);
675 * unveil(2) traverses exec(2); if an editor is used we have
676 * to apply unveil after the log message has been written.
678 if (logmsg == NULL || strlen(logmsg) == 0) {
679 error = get_editor(&editor);
683 error = collect_import_msg(&logmsg, &logmsg_path, editor,
686 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
693 if (unveil(path_dir, "r") != 0) {
694 error = got_error_from_errno2("unveil", path_dir);
700 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
707 error = got_repo_import(&new_commit_id, path_dir, logmsg,
708 author, &ignores, repo, import_progress, NULL);
715 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
722 error = got_ref_write(branch_ref, repo);
729 error = got_object_id_str(&id_str, new_commit_id);
736 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
738 if (error->code != GOT_ERR_NOT_REF) {
744 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
752 error = got_ref_write(head_ref, repo);
760 printf("Created branch %s with commit %s\n",
761 got_ref_get_name(branch_ref), id_str);
763 if (preserve_logmsg) {
764 fprintf(stderr, "%s: log message preserved in %s\n",
765 getprogname(), logmsg_path);
766 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
767 error = got_error_from_errno2("unlink", logmsg_path);
776 free(gitconfig_path);
778 got_ref_close(branch_ref);
780 got_ref_close(head_ref);
787 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
788 "[-p prefix] repository-path [worktree-path]\n", getprogname());
792 static const struct got_error *
793 checkout_progress(void *arg, unsigned char status, const char *path)
795 char *worktree_path = arg;
797 /* Base commit bump happens silently. */
798 if (status == GOT_STATUS_BUMP_BASE)
801 while (path[0] == '/')
804 printf("%c %s/%s\n", status, worktree_path, path);
808 static const struct got_error *
809 check_cancelled(void *arg)
811 if (sigint_received || sigpipe_received)
812 return got_error(GOT_ERR_CANCELLED);
816 static const struct got_error *
817 check_linear_ancestry(struct got_object_id *commit_id,
818 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
819 struct got_repository *repo)
821 const struct got_error *err = NULL;
822 struct got_object_id *yca_id;
824 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
825 commit_id, base_commit_id, repo, check_cancelled, NULL);
830 return got_error(GOT_ERR_ANCESTRY);
833 * Require a straight line of history between the target commit
834 * and the work tree's base commit.
836 * Non-linear situations such as this require a rebase:
838 * (commit) D F (base_commit)
846 * 'got update' only handles linear cases:
847 * Update forwards in time: A (base/yca) - B - C - D (commit)
848 * Update backwards in time: D (base) - C - B - A (commit/yca)
850 if (allow_forwards_in_time_only) {
851 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
852 return got_error(GOT_ERR_ANCESTRY);
853 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
854 got_object_id_cmp(base_commit_id, yca_id) != 0)
855 return got_error(GOT_ERR_ANCESTRY);
861 static const struct got_error *
862 check_same_branch(struct got_object_id *commit_id,
863 struct got_reference *head_ref, struct got_object_id *yca_id,
864 struct got_repository *repo)
866 const struct got_error *err = NULL;
867 struct got_commit_graph *graph = NULL;
868 struct got_object_id *head_commit_id = NULL;
869 int is_same_branch = 0;
871 err = got_ref_resolve(&head_commit_id, repo, head_ref);
875 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
879 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
884 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
888 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
889 check_cancelled, NULL);
894 struct got_object_id *id;
895 err = got_commit_graph_iter_next(&id, graph);
897 if (err->code == GOT_ERR_ITER_COMPLETED) {
900 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
902 err = got_commit_graph_fetch_commits(graph, 1,
903 repo, check_cancelled, NULL);
909 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
911 if (got_object_id_cmp(id, commit_id) == 0) {
919 got_commit_graph_close(graph);
920 free(head_commit_id);
921 if (!err && !is_same_branch)
922 err = got_error(GOT_ERR_ANCESTRY);
926 static const struct got_error *
927 resolve_commit_arg(struct got_object_id **commit_id,
928 const char *commit_id_arg, struct got_repository *repo)
930 const struct got_error *err;
931 struct got_reference *ref;
932 struct got_tag_object *tag;
934 err = got_repo_object_match_tag(&tag, commit_id_arg,
935 GOT_OBJ_TYPE_COMMIT, repo);
937 *commit_id = got_object_id_dup(
938 got_object_tag_get_object_id(tag));
939 if (*commit_id == NULL)
940 err = got_error_from_errno("got_object_id_dup");
941 got_object_tag_close(tag);
943 } else if (err->code != GOT_ERR_NO_OBJ)
946 err = got_ref_open(&ref, repo, commit_id_arg, 0);
948 err = got_ref_resolve(commit_id, repo, ref);
951 if (err->code != GOT_ERR_NOT_REF)
953 err = got_repo_match_object_id_prefix(commit_id,
954 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
959 static const struct got_error *
960 cmd_checkout(int argc, char *argv[])
962 const struct got_error *error = NULL;
963 struct got_repository *repo = NULL;
964 struct got_reference *head_ref = NULL;
965 struct got_worktree *worktree = NULL;
966 char *repo_path = NULL;
967 char *worktree_path = NULL;
968 const char *path_prefix = "";
969 const char *branch_name = GOT_REF_HEAD;
970 char *commit_id_str = NULL;
971 int ch, same_path_prefix;
972 struct got_pathlist_head paths;
976 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
979 branch_name = optarg;
982 commit_id_str = strdup(optarg);
983 if (commit_id_str == NULL)
984 return got_error_from_errno("strdup");
987 path_prefix = optarg;
999 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1000 "unveil", NULL) == -1)
1004 char *cwd, *base, *dotgit;
1005 repo_path = realpath(argv[0], NULL);
1006 if (repo_path == NULL)
1007 return got_error_from_errno2("realpath", argv[0]);
1008 cwd = getcwd(NULL, 0);
1010 error = got_error_from_errno("getcwd");
1013 if (path_prefix[0]) {
1014 base = basename(path_prefix);
1016 error = got_error_from_errno2("basename",
1021 base = basename(repo_path);
1023 error = got_error_from_errno2("basename",
1028 dotgit = strstr(base, ".git");
1031 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1032 error = got_error_from_errno("asprintf");
1037 } else if (argc == 2) {
1038 repo_path = realpath(argv[0], NULL);
1039 if (repo_path == NULL) {
1040 error = got_error_from_errno2("realpath", argv[0]);
1043 worktree_path = realpath(argv[1], NULL);
1044 if (worktree_path == NULL) {
1045 if (errno != ENOENT) {
1046 error = got_error_from_errno2("realpath",
1050 worktree_path = strdup(argv[1]);
1051 if (worktree_path == NULL) {
1052 error = got_error_from_errno("strdup");
1059 got_path_strip_trailing_slashes(repo_path);
1060 got_path_strip_trailing_slashes(worktree_path);
1062 error = got_repo_open(&repo, repo_path, NULL);
1066 /* Pre-create work tree path for unveil(2) */
1067 error = got_path_mkdir(worktree_path);
1069 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1070 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1072 if (!got_path_dir_is_empty(worktree_path)) {
1073 error = got_error_path(worktree_path,
1074 GOT_ERR_DIR_NOT_EMPTY);
1079 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1083 error = got_ref_open(&head_ref, repo, branch_name, 0);
1087 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1088 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1091 error = got_worktree_open(&worktree, worktree_path);
1095 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1099 if (!same_path_prefix) {
1100 error = got_error(GOT_ERR_PATH_PREFIX);
1104 if (commit_id_str) {
1105 struct got_object_id *commit_id;
1106 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1109 error = check_linear_ancestry(commit_id,
1110 got_worktree_get_base_commit_id(worktree), 0, repo);
1111 if (error != NULL) {
1115 error = check_same_branch(commit_id, head_ref, NULL, repo);
1118 error = got_worktree_set_base_commit_id(worktree, repo,
1125 error = got_pathlist_append(&paths, "", NULL);
1128 error = got_worktree_checkout_files(worktree, &paths, repo,
1129 checkout_progress, worktree_path, check_cancelled, NULL);
1133 printf("Now shut up and hack\n");
1136 got_pathlist_free(&paths);
1137 free(commit_id_str);
1139 free(worktree_path);
1146 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1151 static const struct got_error *
1152 update_progress(void *arg, unsigned char status, const char *path)
1154 int *did_something = arg;
1156 if (status == GOT_STATUS_EXISTS)
1161 /* Base commit bump happens silently. */
1162 if (status == GOT_STATUS_BUMP_BASE)
1165 while (path[0] == '/')
1167 printf("%c %s\n", status, path);
1171 static const struct got_error *
1172 switch_head_ref(struct got_reference *head_ref,
1173 struct got_object_id *commit_id, struct got_worktree *worktree,
1174 struct got_repository *repo)
1176 const struct got_error *err = NULL;
1178 int ref_has_moved = 0;
1180 /* Trivial case: switching between two different references. */
1181 if (strcmp(got_ref_get_name(head_ref),
1182 got_worktree_get_head_ref_name(worktree)) != 0) {
1183 printf("Switching work tree from %s to %s\n",
1184 got_worktree_get_head_ref_name(worktree),
1185 got_ref_get_name(head_ref));
1186 return got_worktree_set_head_ref(worktree, head_ref);
1189 err = check_linear_ancestry(commit_id,
1190 got_worktree_get_base_commit_id(worktree), 0, repo);
1192 if (err->code != GOT_ERR_ANCESTRY)
1199 /* Switching to a rebased branch with the same reference name. */
1200 err = got_object_id_str(&base_id_str,
1201 got_worktree_get_base_commit_id(worktree));
1204 printf("Reference %s now points at a different branch\n",
1205 got_worktree_get_head_ref_name(worktree));
1206 printf("Switching work tree from %s to %s\n", base_id_str,
1207 got_worktree_get_head_ref_name(worktree));
1211 static const struct got_error *
1212 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1214 const struct got_error *err;
1217 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1221 return got_error(GOT_ERR_REBASING);
1223 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1227 return got_error(GOT_ERR_HISTEDIT_BUSY);
1232 static const struct got_error *
1233 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1234 char *argv[], struct got_worktree *worktree)
1236 const struct got_error *err = NULL;
1243 return got_error_from_errno("strdup");
1244 return got_pathlist_append(paths, path, NULL);
1247 for (i = 0; i < argc; i++) {
1248 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1251 err = got_pathlist_append(paths, path, NULL);
1261 static const struct got_error *
1262 cmd_update(int argc, char *argv[])
1264 const struct got_error *error = NULL;
1265 struct got_repository *repo = NULL;
1266 struct got_worktree *worktree = NULL;
1267 char *worktree_path = NULL;
1268 struct got_object_id *commit_id = NULL;
1269 char *commit_id_str = NULL;
1270 const char *branch_name = NULL;
1271 struct got_reference *head_ref = NULL;
1272 struct got_pathlist_head paths;
1273 struct got_pathlist_entry *pe;
1274 int ch, did_something = 0;
1278 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1281 branch_name = optarg;
1284 commit_id_str = strdup(optarg);
1285 if (commit_id_str == NULL)
1286 return got_error_from_errno("strdup");
1298 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1299 "unveil", NULL) == -1)
1302 worktree_path = getcwd(NULL, 0);
1303 if (worktree_path == NULL) {
1304 error = got_error_from_errno("getcwd");
1307 error = got_worktree_open(&worktree, worktree_path);
1311 error = check_rebase_or_histedit_in_progress(worktree);
1315 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1320 error = apply_unveil(got_repo_get_path(repo), 0,
1321 got_worktree_get_root_path(worktree));
1325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1329 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1330 got_worktree_get_head_ref_name(worktree), 0);
1333 if (commit_id_str == NULL) {
1334 error = got_ref_resolve(&commit_id, repo, head_ref);
1337 error = got_object_id_str(&commit_id_str, commit_id);
1341 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1342 free(commit_id_str);
1343 commit_id_str = NULL;
1346 error = got_object_id_str(&commit_id_str, commit_id);
1352 struct got_object_id *head_commit_id;
1353 TAILQ_FOREACH(pe, &paths, entry) {
1354 if (pe->path_len == 0)
1356 error = got_error_msg(GOT_ERR_BAD_PATH,
1357 "switching between branches requires that "
1358 "the entire work tree gets updated");
1361 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1364 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1366 free(head_commit_id);
1369 error = check_same_branch(commit_id, head_ref, NULL, repo);
1372 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1376 error = check_linear_ancestry(commit_id,
1377 got_worktree_get_base_commit_id(worktree), 0, repo);
1378 if (error != NULL) {
1379 if (error->code == GOT_ERR_ANCESTRY)
1380 error = got_error(GOT_ERR_BRANCH_MOVED);
1383 error = check_same_branch(commit_id, head_ref, NULL, repo);
1388 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1390 error = got_worktree_set_base_commit_id(worktree, repo,
1396 error = got_worktree_checkout_files(worktree, &paths, repo,
1397 update_progress, &did_something, check_cancelled, NULL);
1402 printf("Updated to commit %s\n", commit_id_str);
1404 printf("Already up-to-date\n");
1406 free(worktree_path);
1407 TAILQ_FOREACH(pe, &paths, entry)
1408 free((char *)pe->path);
1409 got_pathlist_free(&paths);
1411 free(commit_id_str);
1415 static const struct got_error *
1416 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1417 const char *path, int diff_context, int ignore_whitespace,
1418 struct got_repository *repo)
1420 const struct got_error *err = NULL;
1421 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1424 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1429 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1433 while (path[0] == '/')
1435 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1436 ignore_whitespace, stdout);
1439 got_object_blob_close(blob1);
1440 got_object_blob_close(blob2);
1444 static const struct got_error *
1445 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1446 const char *path, int diff_context, int ignore_whitespace,
1447 struct got_repository *repo)
1449 const struct got_error *err = NULL;
1450 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1451 struct got_diff_blob_output_unidiff_arg arg;
1454 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1459 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1463 arg.diff_context = diff_context;
1464 arg.ignore_whitespace = ignore_whitespace;
1465 arg.outfile = stdout;
1466 while (path[0] == '/')
1468 err = got_diff_tree(tree1, tree2, path, path, repo,
1469 got_diff_blob_output_unidiff, &arg, 1);
1472 got_object_tree_close(tree1);
1474 got_object_tree_close(tree2);
1478 static const struct got_error *
1479 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1480 const char *path, int diff_context, struct got_repository *repo)
1482 const struct got_error *err = NULL;
1483 struct got_commit_object *pcommit = NULL;
1484 char *id_str1 = NULL, *id_str2 = NULL;
1485 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1486 struct got_object_qid *qid;
1488 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1490 err = got_object_open_as_commit(&pcommit, repo,
1496 if (path && path[0] != '\0') {
1498 err = got_object_id_by_path(&obj_id2, repo, id, path);
1501 err = got_object_id_str(&id_str2, obj_id2);
1507 err = got_object_id_by_path(&obj_id1, repo,
1513 err = got_object_id_str(&id_str1, obj_id1);
1519 err = got_object_get_type(&obj_type, repo, obj_id2);
1524 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1526 case GOT_OBJ_TYPE_BLOB:
1527 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1530 case GOT_OBJ_TYPE_TREE:
1531 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1535 err = got_error(GOT_ERR_OBJ_TYPE);
1541 obj_id2 = got_object_commit_get_tree_id(commit);
1542 err = got_object_id_str(&id_str2, obj_id2);
1545 obj_id1 = got_object_commit_get_tree_id(pcommit);
1546 err = got_object_id_str(&id_str1, obj_id1);
1549 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1550 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1557 got_object_commit_close(pcommit);
1562 get_datestr(time_t *time, char *datebuf)
1564 struct tm mytm, *tm;
1567 tm = gmtime_r(time, &mytm);
1570 s = asctime_r(tm, datebuf);
1573 p = strchr(s, '\n');
1579 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1581 static const struct got_error *
1582 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1583 struct got_repository *repo, const char *path, int show_patch,
1584 int diff_context, struct got_reflist_head *refs)
1586 const struct got_error *err = NULL;
1587 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1589 time_t committer_time;
1590 const char *author, *committer;
1591 char *refs_str = NULL;
1592 struct got_reflist_entry *re;
1594 SIMPLEQ_FOREACH(re, refs, entry) {
1597 struct got_tag_object *tag = NULL;
1600 name = got_ref_get_name(re->ref);
1601 if (strcmp(name, GOT_REF_HEAD) == 0)
1603 if (strncmp(name, "refs/", 5) == 0)
1605 if (strncmp(name, "got/", 4) == 0)
1607 if (strncmp(name, "heads/", 6) == 0)
1609 if (strncmp(name, "remotes/", 8) == 0)
1611 if (strncmp(name, "tags/", 5) == 0) {
1612 err = got_object_open_as_tag(&tag, repo, re->id);
1614 if (err->code != GOT_ERR_OBJ_TYPE)
1616 /* Ref points at something other than a tag. */
1621 cmp = got_object_id_cmp(tag ?
1622 got_object_tag_get_object_id(tag) : re->id, id);
1624 got_object_tag_close(tag);
1628 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1630 err = got_error_from_errno("asprintf");
1636 err = got_object_id_str(&id_str, id);
1640 printf(GOT_COMMIT_SEP_STR);
1641 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1642 refs_str ? refs_str : "", refs_str ? ")" : "");
1647 printf("from: %s\n", got_object_commit_get_author(commit));
1648 committer_time = got_object_commit_get_committer_time(commit);
1649 datestr = get_datestr(&committer_time, datebuf);
1651 printf("date: %s UTC\n", datestr);
1652 author = got_object_commit_get_author(commit);
1653 committer = got_object_commit_get_committer(commit);
1654 if (strcmp(author, committer) != 0)
1655 printf("via: %s\n", committer);
1656 if (got_object_commit_get_nparents(commit) > 1) {
1657 const struct got_object_id_queue *parent_ids;
1658 struct got_object_qid *qid;
1660 parent_ids = got_object_commit_get_parent_ids(commit);
1661 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1662 err = got_object_id_str(&id_str, qid->id);
1665 printf("parent %d: %s\n", n++, id_str);
1670 err = got_object_commit_get_logmsg(&logmsg0, commit);
1676 line = strsep(&logmsg, "\n");
1678 printf(" %s\n", line);
1683 err = print_patch(commit, id, path, diff_context, repo);
1688 if (fflush(stdout) != 0 && err == NULL)
1689 err = got_error_from_errno("fflush");
1693 static const struct got_error *
1694 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1695 char *path, int show_patch, int diff_context, int limit,
1696 int first_parent_traversal, struct got_reflist_head *refs)
1698 const struct got_error *err;
1699 struct got_commit_graph *graph;
1701 err = got_commit_graph_open(&graph, root_id, path,
1702 first_parent_traversal, repo);
1705 err = got_commit_graph_iter_start(graph, root_id, repo,
1706 check_cancelled, NULL);
1710 struct got_commit_object *commit;
1711 struct got_object_id *id;
1713 if (sigint_received || sigpipe_received)
1716 err = got_commit_graph_iter_next(&id, graph);
1718 if (err->code == GOT_ERR_ITER_COMPLETED) {
1722 if (err->code != GOT_ERR_ITER_NEED_MORE)
1724 err = got_commit_graph_fetch_commits(graph, 1, repo,
1725 check_cancelled, NULL);
1734 err = got_object_open_as_commit(&commit, repo, id);
1737 err = print_commit(commit, id, repo, path, show_patch,
1738 diff_context, refs);
1739 got_object_commit_close(commit);
1740 if (err || (limit && --limit == 0))
1744 got_commit_graph_close(graph);
1751 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1752 "[-r repository-path] [path]\n", getprogname());
1757 get_default_log_limit(void)
1759 const char *got_default_log_limit;
1763 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1764 if (got_default_log_limit == NULL)
1766 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1772 static const struct got_error *
1773 cmd_log(int argc, char *argv[])
1775 const struct got_error *error;
1776 struct got_repository *repo = NULL;
1777 struct got_worktree *worktree = NULL;
1778 struct got_commit_object *commit = NULL;
1779 struct got_object_id *id = NULL;
1780 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1781 char *start_commit = NULL;
1782 int diff_context = 3, ch;
1783 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1785 struct got_reflist_head refs;
1787 SIMPLEQ_INIT(&refs);
1790 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 limit = get_default_log_limit();
1798 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1804 start_commit = optarg;
1807 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1810 err(1, "-C option %s", errstr);
1813 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1815 err(1, "-l option %s", errstr);
1818 first_parent_traversal = 1;
1821 repo_path = realpath(optarg, NULL);
1822 if (repo_path == NULL)
1823 return got_error_from_errno2("realpath",
1825 got_path_strip_trailing_slashes(repo_path);
1836 cwd = getcwd(NULL, 0);
1838 error = got_error_from_errno("getcwd");
1842 error = got_worktree_open(&worktree, cwd);
1843 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1850 error = got_error_from_errno("strdup");
1853 } else if (argc == 1) {
1855 error = got_worktree_resolve_path(&path, worktree,
1860 path = strdup(argv[0]);
1862 error = got_error_from_errno("strdup");
1869 if (repo_path == NULL) {
1870 repo_path = worktree ?
1871 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1873 if (repo_path == NULL) {
1874 error = got_error_from_errno("strdup");
1878 error = got_repo_open(&repo, repo_path, NULL);
1882 error = apply_unveil(got_repo_get_path(repo), 1,
1883 worktree ? got_worktree_get_root_path(worktree) : NULL);
1887 if (start_commit == NULL) {
1888 struct got_reference *head_ref;
1889 error = got_ref_open(&head_ref, repo,
1890 worktree ? got_worktree_get_head_ref_name(worktree)
1894 error = got_ref_resolve(&id, repo, head_ref);
1895 got_ref_close(head_ref);
1898 error = got_object_open_as_commit(&commit, repo, id);
1900 struct got_reference *ref;
1901 error = got_ref_open(&ref, repo, start_commit, 0);
1902 if (error == NULL) {
1904 error = got_ref_resolve(&id, repo, ref);
1908 error = got_object_get_type(&obj_type, repo, id);
1911 if (obj_type == GOT_OBJ_TYPE_TAG) {
1912 struct got_tag_object *tag;
1913 error = got_object_open_as_tag(&tag, repo, id);
1916 if (got_object_tag_get_object_type(tag) !=
1917 GOT_OBJ_TYPE_COMMIT) {
1918 got_object_tag_close(tag);
1919 error = got_error(GOT_ERR_OBJ_TYPE);
1923 id = got_object_id_dup(
1924 got_object_tag_get_object_id(tag));
1926 error = got_error_from_errno(
1927 "got_object_id_dup");
1928 got_object_tag_close(tag);
1931 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1932 error = got_error(GOT_ERR_OBJ_TYPE);
1935 error = got_object_open_as_commit(&commit, repo, id);
1939 if (commit == NULL) {
1940 error = got_repo_match_object_id_prefix(&id,
1941 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1950 const char *prefix = got_worktree_get_path_prefix(worktree);
1952 if (asprintf(&p, "%s%s%s", prefix,
1953 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1954 error = got_error_from_errno("asprintf");
1957 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1960 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1965 path = in_repo_path;
1968 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1972 error = print_commits(id, repo, path, show_patch,
1973 diff_context, limit, first_parent_traversal, &refs);
1980 got_worktree_close(worktree);
1982 const struct got_error *repo_error;
1983 repo_error = got_repo_close(repo);
1987 got_ref_list_free(&refs);
1994 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1995 "[-w] [object1 object2 | path]\n", getprogname());
1999 struct print_diff_arg {
2000 struct got_repository *repo;
2001 struct got_worktree *worktree;
2006 int ignore_whitespace;
2009 static const struct got_error *
2010 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2011 const char *path, struct got_object_id *blob_id,
2012 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2014 struct print_diff_arg *a = arg;
2015 const struct got_error *err = NULL;
2016 struct got_blob_object *blob1 = NULL;
2018 char *abspath = NULL, *label1 = NULL;
2021 if (a->diff_staged) {
2022 if (staged_status != GOT_STATUS_MODIFY &&
2023 staged_status != GOT_STATUS_ADD &&
2024 staged_status != GOT_STATUS_DELETE)
2027 if (staged_status == GOT_STATUS_DELETE)
2029 if (status == GOT_STATUS_NONEXISTENT)
2030 return got_error_set_errno(ENOENT, path);
2031 if (status != GOT_STATUS_MODIFY &&
2032 status != GOT_STATUS_ADD &&
2033 status != GOT_STATUS_DELETE &&
2034 status != GOT_STATUS_CONFLICT)
2038 if (!a->header_shown) {
2039 printf("diff %s %s%s\n", a->id_str,
2040 got_worktree_get_root_path(a->worktree),
2041 a->diff_staged ? " (staged changes)" : "");
2042 a->header_shown = 1;
2045 if (a->diff_staged) {
2046 const char *label1 = NULL, *label2 = NULL;
2047 switch (staged_status) {
2048 case GOT_STATUS_MODIFY:
2052 case GOT_STATUS_ADD:
2055 case GOT_STATUS_DELETE:
2059 return got_error(GOT_ERR_FILE_STATUS);
2061 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2062 label1, label2, a->diff_context, a->ignore_whitespace,
2066 if (staged_status == GOT_STATUS_ADD ||
2067 staged_status == GOT_STATUS_MODIFY) {
2069 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2073 err = got_object_id_str(&id_str, staged_blob_id);
2076 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2077 err = got_error_from_errno("asprintf");
2082 } else if (status != GOT_STATUS_ADD) {
2083 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2088 if (status != GOT_STATUS_DELETE) {
2089 if (asprintf(&abspath, "%s/%s",
2090 got_worktree_get_root_path(a->worktree), path) == -1) {
2091 err = got_error_from_errno("asprintf");
2095 f2 = fopen(abspath, "r");
2097 err = got_error_from_errno2("fopen", abspath);
2100 if (lstat(abspath, &sb) == -1) {
2101 err = got_error_from_errno2("lstat", abspath);
2107 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2108 a->diff_context, a->ignore_whitespace, stdout);
2111 got_object_blob_close(blob1);
2112 if (f2 && fclose(f2) != 0 && err == NULL)
2113 err = got_error_from_errno("fclose");
2118 static const struct got_error *
2119 match_object_id(struct got_object_id **id, char **label,
2120 const char *id_str, int obj_type, int resolve_tags,
2121 struct got_repository *repo)
2123 const struct got_error *err;
2124 struct got_tag_object *tag;
2125 struct got_reference *ref = NULL;
2131 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2134 *id = got_object_id_dup(
2135 got_object_tag_get_object_id(tag));
2137 err = got_error_from_errno("got_object_id_dup");
2138 else if (asprintf(label, "refs/tags/%s",
2139 got_object_tag_get_name(tag)) == -1) {
2140 err = got_error_from_errno("asprintf");
2144 got_object_tag_close(tag);
2146 } else if (err->code != GOT_ERR_NO_OBJ)
2150 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2152 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2154 err = got_ref_open(&ref, repo, id_str, 0);
2157 *label = strdup(got_ref_get_name(ref));
2158 if (*label == NULL) {
2159 err = got_error_from_errno("strdup");
2162 err = got_ref_resolve(id, repo, ref);
2164 err = got_object_id_str(label, *id);
2165 if (*label == NULL) {
2166 err = got_error_from_errno("strdup");
2177 static const struct got_error *
2178 cmd_diff(int argc, char *argv[])
2180 const struct got_error *error;
2181 struct got_repository *repo = NULL;
2182 struct got_worktree *worktree = NULL;
2183 char *cwd = NULL, *repo_path = NULL;
2184 struct got_object_id *id1 = NULL, *id2 = NULL;
2185 const char *id_str1 = NULL, *id_str2 = NULL;
2186 char *label1 = NULL, *label2 = NULL;
2188 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2193 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2198 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2201 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2203 err(1, "-C option %s", errstr);
2206 repo_path = realpath(optarg, NULL);
2207 if (repo_path == NULL)
2208 return got_error_from_errno2("realpath",
2210 got_path_strip_trailing_slashes(repo_path);
2216 ignore_whitespace = 1;
2227 cwd = getcwd(NULL, 0);
2229 error = got_error_from_errno("getcwd");
2232 error = got_worktree_open(&worktree, cwd);
2233 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2236 if (worktree == NULL) {
2237 error = got_error(GOT_ERR_NOT_WORKTREE);
2242 "-r option can't be used when diffing a work tree");
2243 repo_path = strdup(got_worktree_get_repo_path(worktree));
2244 if (repo_path == NULL) {
2245 error = got_error_from_errno("strdup");
2249 error = got_worktree_resolve_path(&path, worktree,
2256 error = got_error_from_errno("strdup");
2260 } else if (argc == 2) {
2262 errx(1, "-s option can't be used when diffing "
2263 "objects in repository");
2266 if (worktree && repo_path == NULL) {
2268 strdup(got_worktree_get_repo_path(worktree));
2269 if (repo_path == NULL) {
2270 error = got_error_from_errno("strdup");
2277 if (repo_path == NULL) {
2278 repo_path = getcwd(NULL, 0);
2279 if (repo_path == NULL)
2280 return got_error_from_errno("getcwd");
2283 error = got_repo_open(&repo, repo_path, NULL);
2288 error = apply_unveil(got_repo_get_path(repo), 1,
2289 worktree ? got_worktree_get_root_path(worktree) : NULL);
2294 struct print_diff_arg arg;
2295 struct got_pathlist_head paths;
2300 error = got_object_id_str(&id_str,
2301 got_worktree_get_base_commit_id(worktree));
2305 arg.worktree = worktree;
2306 arg.diff_context = diff_context;
2307 arg.id_str = id_str;
2308 arg.header_shown = 0;
2309 arg.diff_staged = diff_staged;
2310 arg.ignore_whitespace = ignore_whitespace;
2312 error = got_pathlist_append(&paths, path, NULL);
2316 error = got_worktree_status(worktree, &paths, repo, print_diff,
2317 &arg, check_cancelled, NULL);
2319 got_pathlist_free(&paths);
2323 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2328 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2333 error = got_object_get_type(&type1, repo, id1);
2337 error = got_object_get_type(&type2, repo, id2);
2341 if (type1 != type2) {
2342 error = got_error(GOT_ERR_OBJ_TYPE);
2347 case GOT_OBJ_TYPE_BLOB:
2348 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2349 diff_context, ignore_whitespace, repo, stdout);
2351 case GOT_OBJ_TYPE_TREE:
2352 error = got_diff_objects_as_trees(id1, id2, "", "",
2353 diff_context, ignore_whitespace, repo, stdout);
2355 case GOT_OBJ_TYPE_COMMIT:
2356 printf("diff %s %s\n", label1, label2);
2357 error = got_diff_objects_as_commits(id1, id2, diff_context,
2358 ignore_whitespace, repo, stdout);
2361 error = got_error(GOT_ERR_OBJ_TYPE);
2371 got_worktree_close(worktree);
2373 const struct got_error *repo_error;
2374 repo_error = got_repo_close(repo);
2385 "usage: %s blame [-c commit] [-r repository-path] path\n",
2394 char datebuf[11]; /* YYYY-MM-DD + NUL */
2397 struct blame_cb_args {
2398 struct blame_line *lines;
2402 off_t *line_offsets;
2404 struct got_repository *repo;
2407 static const struct got_error *
2408 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2410 const struct got_error *err = NULL;
2411 struct blame_cb_args *a = arg;
2412 struct blame_line *bline;
2414 size_t linesize = 0;
2415 struct got_commit_object *commit = NULL;
2418 time_t committer_time;
2420 if (nlines != a->nlines ||
2421 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2422 return got_error(GOT_ERR_RANGE);
2424 if (sigint_received)
2425 return got_error(GOT_ERR_ITER_COMPLETED);
2428 return NULL; /* no change in this commit */
2430 /* Annotate this line. */
2431 bline = &a->lines[lineno - 1];
2432 if (bline->annotated)
2434 err = got_object_id_str(&bline->id_str, id);
2438 err = got_object_open_as_commit(&commit, a->repo, id);
2442 bline->committer = strdup(got_object_commit_get_committer(commit));
2443 if (bline->committer == NULL) {
2444 err = got_error_from_errno("strdup");
2448 committer_time = got_object_commit_get_committer_time(commit);
2449 if (localtime_r(&committer_time, &tm) == NULL)
2450 return got_error_from_errno("localtime_r");
2451 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2452 &tm) >= sizeof(bline->datebuf)) {
2453 err = got_error(GOT_ERR_NO_SPACE);
2456 bline->annotated = 1;
2458 /* Print lines annotated so far. */
2459 bline = &a->lines[a->lineno_cur - 1];
2460 if (!bline->annotated)
2463 offset = a->line_offsets[a->lineno_cur - 1];
2464 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2465 err = got_error_from_errno("fseeko");
2469 while (bline->annotated) {
2470 char *smallerthan, *at, *nl, *committer;
2473 if (getline(&line, &linesize, a->f) == -1) {
2475 err = got_error_from_errno("getline");
2479 committer = bline->committer;
2480 smallerthan = strchr(committer, '<');
2481 if (smallerthan && smallerthan[1] != '\0')
2482 committer = smallerthan + 1;
2483 at = strchr(committer, '@');
2486 len = strlen(committer);
2488 committer[8] = '\0';
2490 nl = strchr(line, '\n');
2493 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2494 bline->id_str, bline->datebuf, committer, line);
2497 bline = &a->lines[a->lineno_cur - 1];
2501 got_object_commit_close(commit);
2506 static const struct got_error *
2507 cmd_blame(int argc, char *argv[])
2509 const struct got_error *error;
2510 struct got_repository *repo = NULL;
2511 struct got_worktree *worktree = NULL;
2512 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2513 struct got_object_id *obj_id = NULL;
2514 struct got_object_id *commit_id = NULL;
2515 struct got_blob_object *blob = NULL;
2516 char *commit_id_str = NULL;
2517 struct blame_cb_args bca;
2518 int ch, obj_type, i;
2521 memset(&bca, 0, sizeof(bca));
2524 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2529 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2532 commit_id_str = optarg;
2535 repo_path = realpath(optarg, NULL);
2536 if (repo_path == NULL)
2537 return got_error_from_errno2("realpath",
2539 got_path_strip_trailing_slashes(repo_path);
2555 cwd = getcwd(NULL, 0);
2557 error = got_error_from_errno("getcwd");
2560 if (repo_path == NULL) {
2561 error = got_worktree_open(&worktree, cwd);
2562 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2568 strdup(got_worktree_get_repo_path(worktree));
2569 if (repo_path == NULL)
2570 error = got_error_from_errno("strdup");
2574 repo_path = strdup(cwd);
2575 if (repo_path == NULL) {
2576 error = got_error_from_errno("strdup");
2582 error = got_repo_open(&repo, repo_path, NULL);
2586 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2591 const char *prefix = got_worktree_get_path_prefix(worktree);
2592 char *p, *worktree_subdir = cwd +
2593 strlen(got_worktree_get_root_path(worktree));
2594 if (asprintf(&p, "%s%s%s%s%s",
2595 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2596 worktree_subdir, worktree_subdir[0] ? "/" : "",
2598 error = got_error_from_errno("asprintf");
2601 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2604 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2609 if (commit_id_str == NULL) {
2610 struct got_reference *head_ref;
2611 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2614 error = got_ref_resolve(&commit_id, repo, head_ref);
2615 got_ref_close(head_ref);
2619 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2624 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2627 if (obj_id == NULL) {
2628 error = got_error(GOT_ERR_NO_OBJ);
2632 error = got_object_get_type(&obj_type, repo, obj_id);
2636 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2637 error = got_error(GOT_ERR_OBJ_TYPE);
2641 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2644 bca.f = got_opentemp();
2645 if (bca.f == NULL) {
2646 error = got_error_from_errno("got_opentemp");
2649 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2650 &bca.line_offsets, bca.f, blob);
2651 if (error || bca.nlines == 0)
2654 /* Don't include \n at EOF in the blame line count. */
2655 if (bca.line_offsets[bca.nlines - 1] == filesize)
2658 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2659 if (bca.lines == NULL) {
2660 error = got_error_from_errno("calloc");
2664 bca.nlines_prec = 0;
2672 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2673 check_cancelled, NULL);
2683 got_object_blob_close(blob);
2685 got_worktree_close(worktree);
2687 const struct got_error *repo_error;
2688 repo_error = got_repo_close(repo);
2693 for (i = 0; i < bca.nlines; i++) {
2694 struct blame_line *bline = &bca.lines[i];
2695 free(bline->id_str);
2696 free(bline->committer);
2700 free(bca.line_offsets);
2701 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2702 error = got_error_from_errno("fclose");
2710 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2716 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2717 const char *root_path)
2719 int is_root_path = (strcmp(path, root_path) == 0);
2720 const char *modestr = "";
2722 path += strlen(root_path);
2723 while (path[0] == '/')
2726 if (got_object_tree_entry_is_submodule(te))
2728 else if (S_ISLNK(te->mode))
2730 else if (S_ISDIR(te->mode))
2732 else if (te->mode & S_IXUSR)
2735 printf("%s%s%s%s%s\n", id ? id : "", path,
2736 is_root_path ? "" : "/", te->name, modestr);
2739 static const struct got_error *
2740 print_tree(const char *path, struct got_object_id *commit_id,
2741 int show_ids, int recurse, const char *root_path,
2742 struct got_repository *repo)
2744 const struct got_error *err = NULL;
2745 struct got_object_id *tree_id = NULL;
2746 struct got_tree_object *tree = NULL;
2747 const struct got_tree_entries *entries;
2748 struct got_tree_entry *te;
2750 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2754 err = got_object_open_as_tree(&tree, repo, tree_id);
2757 entries = got_object_tree_get_entries(tree);
2758 te = SIMPLEQ_FIRST(&entries->head);
2762 if (sigint_received || sigpipe_received)
2767 err = got_object_id_str(&id_str, te->id);
2770 if (asprintf(&id, "%s ", id_str) == -1) {
2771 err = got_error_from_errno("asprintf");
2777 print_entry(te, id, path, root_path);
2780 if (recurse && S_ISDIR(te->mode)) {
2782 if (asprintf(&child_path, "%s%s%s", path,
2783 path[0] == '/' && path[1] == '\0' ? "" : "/",
2785 err = got_error_from_errno("asprintf");
2788 err = print_tree(child_path, commit_id, show_ids, 1,
2795 te = SIMPLEQ_NEXT(te, entry);
2799 got_object_tree_close(tree);
2804 static const struct got_error *
2805 cmd_tree(int argc, char *argv[])
2807 const struct got_error *error;
2808 struct got_repository *repo = NULL;
2809 struct got_worktree *worktree = NULL;
2811 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2812 struct got_object_id *commit_id = NULL;
2813 char *commit_id_str = NULL;
2814 int show_ids = 0, recurse = 0;
2818 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2823 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2826 commit_id_str = optarg;
2829 repo_path = realpath(optarg, NULL);
2830 if (repo_path == NULL)
2831 return got_error_from_errno2("realpath",
2833 got_path_strip_trailing_slashes(repo_path);
2857 cwd = getcwd(NULL, 0);
2859 error = got_error_from_errno("getcwd");
2862 if (repo_path == NULL) {
2863 error = got_worktree_open(&worktree, cwd);
2864 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2870 strdup(got_worktree_get_repo_path(worktree));
2871 if (repo_path == NULL)
2872 error = got_error_from_errno("strdup");
2876 repo_path = strdup(cwd);
2877 if (repo_path == NULL) {
2878 error = got_error_from_errno("strdup");
2884 error = got_repo_open(&repo, repo_path, NULL);
2888 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2894 char *p, *worktree_subdir = cwd +
2895 strlen(got_worktree_get_root_path(worktree));
2896 if (asprintf(&p, "%s/%s",
2897 got_worktree_get_path_prefix(worktree),
2898 worktree_subdir) == -1) {
2899 error = got_error_from_errno("asprintf");
2902 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2909 if (in_repo_path == NULL) {
2910 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2915 if (commit_id_str == NULL) {
2916 struct got_reference *head_ref;
2917 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2920 error = got_ref_resolve(&commit_id, repo, head_ref);
2921 got_ref_close(head_ref);
2925 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2930 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2931 in_repo_path, repo);
2938 got_worktree_close(worktree);
2940 const struct got_error *repo_error;
2941 repo_error = got_repo_close(repo);
2951 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2955 static const struct got_error *
2956 print_status(void *arg, unsigned char status, unsigned char staged_status,
2957 const char *path, struct got_object_id *blob_id,
2958 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2960 if (status == staged_status && (status == GOT_STATUS_DELETE))
2961 status = GOT_STATUS_NO_CHANGE;
2962 printf("%c%c %s\n", status, staged_status, path);
2966 static const struct got_error *
2967 cmd_status(int argc, char *argv[])
2969 const struct got_error *error = NULL;
2970 struct got_repository *repo = NULL;
2971 struct got_worktree *worktree = NULL;
2973 struct got_pathlist_head paths;
2974 struct got_pathlist_entry *pe;
2979 while ((ch = getopt(argc, argv, "")) != -1) {
2991 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2995 cwd = getcwd(NULL, 0);
2997 error = got_error_from_errno("getcwd");
3001 error = got_worktree_open(&worktree, cwd);
3005 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3010 error = apply_unveil(got_repo_get_path(repo), 1,
3011 got_worktree_get_root_path(worktree));
3015 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3019 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3020 check_cancelled, NULL);
3022 TAILQ_FOREACH(pe, &paths, entry)
3023 free((char *)pe->path);
3024 got_pathlist_free(&paths);
3033 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3038 static const struct got_error *
3039 list_refs(struct got_repository *repo)
3041 static const struct got_error *err = NULL;
3042 struct got_reflist_head refs;
3043 struct got_reflist_entry *re;
3045 SIMPLEQ_INIT(&refs);
3046 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3050 SIMPLEQ_FOREACH(re, &refs, entry) {
3052 refstr = got_ref_to_str(re->ref);
3054 return got_error_from_errno("got_ref_to_str");
3055 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3059 got_ref_list_free(&refs);
3063 static const struct got_error *
3064 delete_ref(struct got_repository *repo, const char *refname)
3066 const struct got_error *err = NULL;
3067 struct got_reference *ref;
3069 err = got_ref_open(&ref, repo, refname, 0);
3073 err = got_ref_delete(ref, repo);
3078 static const struct got_error *
3079 add_ref(struct got_repository *repo, const char *refname, const char *target)
3081 const struct got_error *err = NULL;
3082 struct got_object_id *id;
3083 struct got_reference *ref = NULL;
3086 * Don't let the user create a reference named '-'.
3087 * While technically a valid reference name, this case is usually
3088 * an unintended typo.
3090 if (refname[0] == '-' && refname[1] == '\0')
3091 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3093 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3096 struct got_reference *target_ref;
3098 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3100 err = got_ref_open(&target_ref, repo, target, 0);
3103 err = got_ref_resolve(&id, repo, target_ref);
3104 got_ref_close(target_ref);
3109 err = got_ref_alloc(&ref, refname, id);
3113 err = got_ref_write(ref, repo);
3121 static const struct got_error *
3122 add_symref(struct got_repository *repo, const char *refname, const char *target)
3124 const struct got_error *err = NULL;
3125 struct got_reference *ref = NULL;
3126 struct got_reference *target_ref = NULL;
3129 * Don't let the user create a reference named '-'.
3130 * While technically a valid reference name, this case is usually
3131 * an unintended typo.
3133 if (refname[0] == '-' && refname[1] == '\0')
3134 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3136 err = got_ref_open(&target_ref, repo, target, 0);
3140 err = got_ref_alloc_symref(&ref, refname, target_ref);
3144 err = got_ref_write(ref, repo);
3147 got_ref_close(target_ref);
3153 static const struct got_error *
3154 cmd_ref(int argc, char *argv[])
3156 const struct got_error *error = NULL;
3157 struct got_repository *repo = NULL;
3158 struct got_worktree *worktree = NULL;
3159 char *cwd = NULL, *repo_path = NULL;
3160 int ch, do_list = 0, create_symref = 0;
3161 const char *delref = NULL;
3163 /* TODO: Add -s option for adding symbolic references. */
3164 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3170 repo_path = realpath(optarg, NULL);
3171 if (repo_path == NULL)
3172 return got_error_from_errno2("realpath",
3174 got_path_strip_trailing_slashes(repo_path);
3188 if (do_list && delref)
3189 errx(1, "-l and -d options are mutually exclusive\n");
3194 if (do_list || delref) {
3196 errx(1, "-s option cannot be used together with the "
3197 "-l or -d options");
3200 } else if (argc != 2)
3205 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3209 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3210 "sendfd unveil", NULL) == -1)
3214 cwd = getcwd(NULL, 0);
3216 error = got_error_from_errno("getcwd");
3220 if (repo_path == NULL) {
3221 error = got_worktree_open(&worktree, cwd);
3222 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3228 strdup(got_worktree_get_repo_path(worktree));
3229 if (repo_path == NULL)
3230 error = got_error_from_errno("strdup");
3234 repo_path = strdup(cwd);
3235 if (repo_path == NULL) {
3236 error = got_error_from_errno("strdup");
3242 error = got_repo_open(&repo, repo_path, NULL);
3246 error = apply_unveil(got_repo_get_path(repo), do_list,
3247 worktree ? got_worktree_get_root_path(worktree) : NULL);
3252 error = list_refs(repo);
3254 error = delete_ref(repo, delref);
3255 else if (create_symref)
3256 error = add_symref(repo, argv[0], argv[1]);
3258 error = add_ref(repo, argv[0], argv[1]);
3261 got_repo_close(repo);
3263 got_worktree_close(worktree);
3273 "usage: %s branch [-r repository] [-l] | -d name | "
3274 "[name [commit]]\n", getprogname());
3278 static const struct got_error *
3279 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3280 struct got_reference *ref)
3282 const struct got_error *err = NULL;
3283 const char *refname, *marker = " ";
3286 refname = got_ref_get_name(ref);
3287 if (worktree && strcmp(refname,
3288 got_worktree_get_head_ref_name(worktree)) == 0) {
3289 struct got_object_id *id = NULL;
3291 err = got_ref_resolve(&id, repo, ref);
3294 if (got_object_id_cmp(id,
3295 got_worktree_get_base_commit_id(worktree)) == 0)
3302 if (strncmp(refname, "refs/heads/", 11) == 0)
3304 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3307 refstr = got_ref_to_str(ref);
3309 return got_error_from_errno("got_ref_to_str");
3311 printf("%s%s: %s\n", marker, refname, refstr);
3316 static const struct got_error *
3317 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3319 const char *refname;
3321 if (worktree == NULL)
3322 return got_error(GOT_ERR_NOT_WORKTREE);
3324 refname = got_worktree_get_head_ref_name(worktree);
3326 if (strncmp(refname, "refs/heads/", 11) == 0)
3328 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3331 printf("%s\n", refname);
3336 static const struct got_error *
3337 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3339 static const struct got_error *err = NULL;
3340 struct got_reflist_head refs;
3341 struct got_reflist_entry *re;
3342 struct got_reference *temp_ref = NULL;
3343 int rebase_in_progress, histedit_in_progress;
3345 SIMPLEQ_INIT(&refs);
3348 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3353 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3358 if (rebase_in_progress || histedit_in_progress) {
3359 err = got_ref_open(&temp_ref, repo,
3360 got_worktree_get_head_ref_name(worktree), 0);
3363 list_branch(repo, worktree, temp_ref);
3364 got_ref_close(temp_ref);
3368 err = got_ref_list(&refs, repo, "refs/heads",
3369 got_ref_cmp_by_name, NULL);
3373 SIMPLEQ_FOREACH(re, &refs, entry)
3374 list_branch(repo, worktree, re->ref);
3376 got_ref_list_free(&refs);
3380 static const struct got_error *
3381 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3382 const char *branch_name)
3384 const struct got_error *err = NULL;
3385 struct got_reference *ref = NULL;
3388 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3389 return got_error_from_errno("asprintf");
3391 err = got_ref_open(&ref, repo, refname, 0);
3396 strcmp(got_worktree_get_head_ref_name(worktree),
3397 got_ref_get_name(ref)) == 0) {
3398 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3399 "will not delete this work tree's current branch");
3403 err = got_ref_delete(ref, repo);
3411 static const struct got_error *
3412 add_branch(struct got_repository *repo, const char *branch_name,
3413 const char *base_branch)
3415 const struct got_error *err = NULL;
3416 struct got_object_id *id = NULL;
3418 struct got_reference *ref = NULL;
3419 char *base_refname = NULL, *refname = NULL;
3422 * Don't let the user create a branch named '-'.
3423 * While technically a valid reference name, this case is usually
3424 * an unintended typo.
3426 if (branch_name[0] == '-' && branch_name[1] == '\0')
3427 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3429 err = match_object_id(&id, &label, base_branch,
3430 GOT_OBJ_TYPE_COMMIT, 1, repo);
3434 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3435 err = got_error_from_errno("asprintf");
3439 err = got_ref_open(&ref, repo, refname, 0);
3441 err = got_error(GOT_ERR_BRANCH_EXISTS);
3443 } else if (err->code != GOT_ERR_NOT_REF)
3446 err = got_ref_alloc(&ref, refname, id);
3450 err = got_ref_write(ref, repo);
3460 static const struct got_error *
3461 cmd_branch(int argc, char *argv[])
3463 const struct got_error *error = NULL;
3464 struct got_repository *repo = NULL;
3465 struct got_worktree *worktree = NULL;
3466 char *cwd = NULL, *repo_path = NULL;
3467 int ch, do_list = 0, do_show = 0;
3468 const char *delref = NULL;
3470 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3476 repo_path = realpath(optarg, NULL);
3477 if (repo_path == NULL)
3478 return got_error_from_errno2("realpath",
3480 got_path_strip_trailing_slashes(repo_path);
3491 if (do_list && delref)
3492 errx(1, "-l and -d options are mutually exclusive\n");
3497 if (!do_list && !delref && argc == 0)
3500 if (do_list || delref) {
3503 } else if (!do_show && (argc < 1 || argc > 2))
3507 if (do_list || do_show) {
3508 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3512 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3513 "sendfd unveil", NULL) == -1)
3517 cwd = getcwd(NULL, 0);
3519 error = got_error_from_errno("getcwd");
3523 if (repo_path == NULL) {
3524 error = got_worktree_open(&worktree, cwd);
3525 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3531 strdup(got_worktree_get_repo_path(worktree));
3532 if (repo_path == NULL)
3533 error = got_error_from_errno("strdup");
3537 repo_path = strdup(cwd);
3538 if (repo_path == NULL) {
3539 error = got_error_from_errno("strdup");
3545 error = got_repo_open(&repo, repo_path, NULL);
3549 error = apply_unveil(got_repo_get_path(repo), do_list,
3550 worktree ? got_worktree_get_root_path(worktree) : NULL);
3555 error = show_current_branch(repo, worktree);
3557 error = list_branches(repo, worktree);
3559 error = delete_branch(repo, worktree, delref);
3561 const char *base_branch;
3563 base_branch = worktree ?
3564 got_worktree_get_head_ref_name(worktree) :
3566 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3569 base_branch = argv[1];
3570 error = add_branch(repo, argv[0], base_branch);
3574 got_repo_close(repo);
3576 got_worktree_close(worktree);
3587 "usage: %s tag [-r repository] | -l | "
3588 "[-m message] name [commit]\n", getprogname());
3593 static const struct got_error *
3594 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3596 const struct got_error *err = NULL;
3597 struct got_reflist_entry *re, *se, *new;
3598 struct got_object_id *re_id, *se_id;
3599 struct got_tag_object *re_tag, *se_tag;
3600 time_t re_time, se_time;
3602 SIMPLEQ_FOREACH(re, tags, entry) {
3603 se = SIMPLEQ_FIRST(sorted);
3605 err = got_reflist_entry_dup(&new, re);
3608 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3611 err = got_ref_resolve(&re_id, repo, re->ref);
3614 err = got_object_open_as_tag(&re_tag, repo, re_id);
3618 re_time = got_object_tag_get_tagger_time(re_tag);
3619 got_object_tag_close(re_tag);
3623 err = got_ref_resolve(&se_id, repo, re->ref);
3626 err = got_object_open_as_tag(&se_tag, repo, se_id);
3630 se_time = got_object_tag_get_tagger_time(se_tag);
3631 got_object_tag_close(se_tag);
3633 if (se_time > re_time) {
3634 err = got_reflist_entry_dup(&new, re);
3637 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3640 se = SIMPLEQ_NEXT(se, entry);
3649 static const struct got_error *
3650 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3651 struct got_reference *ref2)
3653 const struct got_error *err = NULL;
3654 struct got_repository *repo = arg;
3655 struct got_object_id *id1, *id2 = NULL;
3656 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3657 time_t time1, time2;
3661 err = got_ref_resolve(&id1, repo, ref1);
3664 err = got_object_open_as_tag(&tag1, repo, id1);
3668 err = got_ref_resolve(&id2, repo, ref2);
3671 err = got_object_open_as_tag(&tag2, repo, id2);
3675 time1 = got_object_tag_get_tagger_time(tag1);
3676 time2 = got_object_tag_get_tagger_time(tag2);
3678 /* Put latest tags first. */
3681 else if (time1 > time2)
3684 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3689 got_object_tag_close(tag1);
3691 got_object_tag_close(tag2);
3695 static const struct got_error *
3696 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3698 static const struct got_error *err = NULL;
3699 struct got_reflist_head refs;
3700 struct got_reflist_entry *re;
3702 SIMPLEQ_INIT(&refs);
3704 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3708 SIMPLEQ_FOREACH(re, &refs, entry) {
3709 const char *refname;
3710 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3713 struct got_object_id *id;
3714 struct got_tag_object *tag;
3716 refname = got_ref_get_name(re->ref);
3717 if (strncmp(refname, "refs/tags/", 10) != 0)
3720 refstr = got_ref_to_str(re->ref);
3721 if (refstr == NULL) {
3722 err = got_error_from_errno("got_ref_to_str");
3725 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3728 err = got_ref_resolve(&id, repo, re->ref);
3731 err = got_object_open_as_tag(&tag, repo, id);
3735 printf("from: %s\n", got_object_tag_get_tagger(tag));
3736 tagger_time = got_object_tag_get_tagger_time(tag);
3737 datestr = get_datestr(&tagger_time, datebuf);
3739 printf("date: %s UTC\n", datestr);
3740 err = got_object_id_str(&id_str,
3741 got_object_tag_get_object_id(tag));
3744 switch (got_object_tag_get_object_type(tag)) {
3745 case GOT_OBJ_TYPE_BLOB:
3746 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3748 case GOT_OBJ_TYPE_TREE:
3749 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3751 case GOT_OBJ_TYPE_COMMIT:
3752 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3754 case GOT_OBJ_TYPE_TAG:
3755 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3761 tagmsg0 = strdup(got_object_tag_get_message(tag));
3762 got_object_tag_close(tag);
3763 if (tagmsg0 == NULL) {
3764 err = got_error_from_errno("strdup");
3770 line = strsep(&tagmsg, "\n");
3772 printf(" %s\n", line);
3777 got_ref_list_free(&refs);
3781 static const struct got_error *
3782 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3783 const char *tag_name, const char *repo_path)
3785 const struct got_error *err = NULL;
3786 char *template = NULL, *initial_content = NULL;
3787 char *editor = NULL;
3790 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3791 err = got_error_from_errno("asprintf");
3795 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3796 commit_id_str, tag_name) == -1) {
3797 err = got_error_from_errno("asprintf");
3801 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3805 dprintf(fd, initial_content);
3808 err = get_editor(&editor);
3811 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3813 free(initial_content);
3817 /* Editor is done; we can now apply unveil(2) */
3819 err = apply_unveil(repo_path, 0, NULL);
3828 static const struct got_error *
3829 add_tag(struct got_repository *repo, const char *tag_name,
3830 const char *commit_arg, const char *tagmsg_arg)
3832 const struct got_error *err = NULL;
3833 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3834 char *label = NULL, *commit_id_str = NULL;
3835 struct got_reference *ref = NULL;
3836 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3837 char *tagmsg_path = NULL, *tag_id_str = NULL;
3838 int preserve_tagmsg = 0;
3841 * Don't let the user create a tag named '-'.
3842 * While technically a valid reference name, this case is usually
3843 * an unintended typo.
3845 if (tag_name[0] == '-' && tag_name[1] == '\0')
3846 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3848 err = get_author(&tagger, repo);
3852 err = match_object_id(&commit_id, &label, commit_arg,
3853 GOT_OBJ_TYPE_COMMIT, 1, repo);
3857 err = got_object_id_str(&commit_id_str, commit_id);
3861 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3862 refname = strdup(tag_name);
3863 if (refname == NULL) {
3864 err = got_error_from_errno("strdup");
3868 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3869 err = got_error_from_errno("asprintf");
3873 err = got_ref_open(&ref, repo, refname, 0);
3875 err = got_error(GOT_ERR_TAG_EXISTS);
3877 } else if (err->code != GOT_ERR_NOT_REF)
3880 if (tagmsg_arg == NULL) {
3881 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3882 tag_name, got_repo_get_path(repo));
3884 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3885 tagmsg_path != NULL)
3886 preserve_tagmsg = 1;
3891 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3892 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3895 preserve_tagmsg = 1;
3899 err = got_ref_alloc(&ref, refname, tag_id);
3902 preserve_tagmsg = 1;
3906 err = got_ref_write(ref, repo);
3909 preserve_tagmsg = 1;
3913 err = got_object_id_str(&tag_id_str, tag_id);
3916 preserve_tagmsg = 1;
3919 printf("Created tag %s\n", tag_id_str);
3921 if (preserve_tagmsg) {
3922 fprintf(stderr, "%s: tag message preserved in %s\n",
3923 getprogname(), tagmsg_path);
3924 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3925 err = got_error_from_errno2("unlink", tagmsg_path);
3930 free(commit_id_str);
3938 static const struct got_error *
3939 cmd_tag(int argc, char *argv[])
3941 const struct got_error *error = NULL;
3942 struct got_repository *repo = NULL;
3943 struct got_worktree *worktree = NULL;
3944 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3945 char *gitconfig_path = NULL;
3946 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3947 int ch, do_list = 0;
3949 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3955 repo_path = realpath(optarg, NULL);
3956 if (repo_path == NULL)
3957 return got_error_from_errno2("realpath",
3959 got_path_strip_trailing_slashes(repo_path);
3975 errx(1, "-l and -m options are mutually exclusive\n");
3978 } else if (argc < 1 || argc > 2)
3981 commit_id_arg = argv[1];
3986 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3990 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3991 "sendfd unveil", NULL) == -1)
3995 cwd = getcwd(NULL, 0);
3997 error = got_error_from_errno("getcwd");
4001 if (repo_path == NULL) {
4002 error = got_worktree_open(&worktree, cwd);
4003 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4009 strdup(got_worktree_get_repo_path(worktree));
4010 if (repo_path == NULL)
4011 error = got_error_from_errno("strdup");
4015 repo_path = strdup(cwd);
4016 if (repo_path == NULL) {
4017 error = got_error_from_errno("strdup");
4024 error = got_repo_open(&repo, repo_path, NULL);
4027 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4030 error = list_tags(repo, worktree);
4032 error = get_gitconfig_path(&gitconfig_path);
4035 error = got_repo_open(&repo, repo_path, gitconfig_path);
4040 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4045 if (commit_id_arg == NULL) {
4046 struct got_reference *head_ref;
4047 struct got_object_id *commit_id;
4048 error = got_ref_open(&head_ref, repo,
4049 worktree ? got_worktree_get_head_ref_name(worktree)
4053 error = got_ref_resolve(&commit_id, repo, head_ref);
4054 got_ref_close(head_ref);
4057 error = got_object_id_str(&commit_id_str, commit_id);
4063 error = add_tag(repo, tag_name,
4064 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4068 got_repo_close(repo);
4070 got_worktree_close(worktree);
4073 free(gitconfig_path);
4074 free(commit_id_str);
4081 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4085 static const struct got_error *
4086 cmd_add(int argc, char *argv[])
4088 const struct got_error *error = NULL;
4089 struct got_repository *repo = NULL;
4090 struct got_worktree *worktree = NULL;
4092 struct got_pathlist_head paths;
4093 struct got_pathlist_entry *pe;
4098 while ((ch = getopt(argc, argv, "")) != -1) {
4110 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4117 cwd = getcwd(NULL, 0);
4119 error = got_error_from_errno("getcwd");
4123 error = got_worktree_open(&worktree, cwd);
4127 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4132 error = apply_unveil(got_repo_get_path(repo), 1,
4133 got_worktree_get_root_path(worktree));
4137 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4141 error = got_worktree_schedule_add(worktree, &paths, print_status,
4145 got_repo_close(repo);
4147 got_worktree_close(worktree);
4148 TAILQ_FOREACH(pe, &paths, entry)
4149 free((char *)pe->path);
4150 got_pathlist_free(&paths);
4158 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4162 static const struct got_error *
4163 print_remove_status(void *arg, unsigned char status,
4164 unsigned char staged_status, const char *path,
4165 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4166 struct got_object_id *commit_id)
4168 if (status == GOT_STATUS_NONEXISTENT)
4170 if (status == staged_status && (status == GOT_STATUS_DELETE))
4171 status = GOT_STATUS_NO_CHANGE;
4172 printf("%c%c %s\n", status, staged_status, path);
4176 static const struct got_error *
4177 cmd_remove(int argc, char *argv[])
4179 const struct got_error *error = NULL;
4180 struct got_worktree *worktree = NULL;
4181 struct got_repository *repo = NULL;
4183 struct got_pathlist_head paths;
4184 struct got_pathlist_entry *pe;
4185 int ch, delete_local_mods = 0;
4189 while ((ch = getopt(argc, argv, "f")) != -1) {
4192 delete_local_mods = 1;
4204 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4211 cwd = getcwd(NULL, 0);
4213 error = got_error_from_errno("getcwd");
4216 error = got_worktree_open(&worktree, cwd);
4220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4225 error = apply_unveil(got_repo_get_path(repo), 1,
4226 got_worktree_get_root_path(worktree));
4230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4234 error = got_worktree_schedule_delete(worktree, &paths,
4235 delete_local_mods, print_remove_status, NULL, repo);
4240 got_repo_close(repo);
4242 got_worktree_close(worktree);
4243 TAILQ_FOREACH(pe, &paths, entry)
4244 free((char *)pe->path);
4245 got_pathlist_free(&paths);
4253 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4254 "path ...\n", getprogname());
4258 static const struct got_error *
4259 revert_progress(void *arg, unsigned char status, const char *path)
4261 while (path[0] == '/')
4263 printf("%c %s\n", status, path);
4267 struct choose_patch_arg {
4268 FILE *patch_script_file;
4272 static const struct got_error *
4273 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4274 int nchanges, const char *action)
4277 size_t linesize = 0;
4281 case GOT_STATUS_ADD:
4282 printf("A %s\n%s this addition? [y/n] ", path, action);
4284 case GOT_STATUS_DELETE:
4285 printf("D %s\n%s this deletion? [y/n] ", path, action);
4287 case GOT_STATUS_MODIFY:
4288 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4289 return got_error_from_errno("fseek");
4290 printf(GOT_COMMIT_SEP_STR);
4291 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4293 if (ferror(patch_file))
4294 return got_error_from_errno("getline");
4295 printf(GOT_COMMIT_SEP_STR);
4296 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4297 path, n, nchanges, action);
4300 return got_error_path(path, GOT_ERR_FILE_STATUS);
4306 static const struct got_error *
4307 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4308 FILE *patch_file, int n, int nchanges)
4310 const struct got_error *err = NULL;
4312 size_t linesize = 0;
4315 struct choose_patch_arg *a = arg;
4317 *choice = GOT_PATCH_CHOICE_NONE;
4319 if (a->patch_script_file) {
4321 err = show_change(status, path, patch_file, n, nchanges,
4325 linelen = getline(&line, &linesize, a->patch_script_file);
4326 if (linelen == -1) {
4327 if (ferror(a->patch_script_file))
4328 return got_error_from_errno("getline");
4331 nl = strchr(line, '\n');
4334 if (strcmp(line, "y") == 0) {
4335 *choice = GOT_PATCH_CHOICE_YES;
4337 } else if (strcmp(line, "n") == 0) {
4338 *choice = GOT_PATCH_CHOICE_NO;
4340 } else if (strcmp(line, "q") == 0 &&
4341 status == GOT_STATUS_MODIFY) {
4342 *choice = GOT_PATCH_CHOICE_QUIT;
4345 printf("invalid response '%s'\n", line);
4350 while (resp != 'y' && resp != 'n' && resp != 'q') {
4351 err = show_change(status, path, patch_file, n, nchanges,
4358 if (status == GOT_STATUS_MODIFY) {
4359 if (resp != 'y' && resp != 'n' && resp != 'q') {
4360 printf("invalid response '%c'\n", resp);
4363 } else if (resp != 'y' && resp != 'n') {
4364 printf("invalid response '%c'\n", resp);
4370 *choice = GOT_PATCH_CHOICE_YES;
4371 else if (resp == 'n')
4372 *choice = GOT_PATCH_CHOICE_NO;
4373 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4374 *choice = GOT_PATCH_CHOICE_QUIT;
4380 static const struct got_error *
4381 cmd_revert(int argc, char *argv[])
4383 const struct got_error *error = NULL;
4384 struct got_worktree *worktree = NULL;
4385 struct got_repository *repo = NULL;
4386 char *cwd = NULL, *path = NULL;
4387 struct got_pathlist_head paths;
4388 struct got_pathlist_entry *pe;
4389 int ch, can_recurse = 0, pflag = 0;
4390 FILE *patch_script_file = NULL;
4391 const char *patch_script_path = NULL;
4392 struct choose_patch_arg cpa;
4396 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4402 patch_script_path = optarg;
4417 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4418 "unveil", NULL) == -1)
4423 if (patch_script_path && !pflag)
4424 errx(1, "-F option can only be used together with -p option");
4426 cwd = getcwd(NULL, 0);
4428 error = got_error_from_errno("getcwd");
4431 error = got_worktree_open(&worktree, cwd);
4435 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4440 if (patch_script_path) {
4441 patch_script_file = fopen(patch_script_path, "r");
4442 if (patch_script_file == NULL) {
4443 error = got_error_from_errno2("fopen",
4448 error = apply_unveil(got_repo_get_path(repo), 1,
4449 got_worktree_get_root_path(worktree));
4453 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4460 TAILQ_FOREACH(pe, &paths, entry) {
4461 if (asprintf(&ondisk_path, "%s/%s",
4462 got_worktree_get_root_path(worktree),
4464 error = got_error_from_errno("asprintf");
4467 if (lstat(ondisk_path, &sb) == -1) {
4468 if (errno == ENOENT) {
4472 error = got_error_from_errno2("lstat",
4478 if (S_ISDIR(sb.st_mode)) {
4479 error = got_error_msg(GOT_ERR_BAD_PATH,
4480 "reverting directories requires -R option");
4486 cpa.patch_script_file = patch_script_file;
4487 cpa.action = "revert";
4488 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4489 pflag ? choose_patch : NULL, &cpa, repo);
4493 if (patch_script_file && fclose(patch_script_file) == EOF &&
4495 error = got_error_from_errno2("fclose", patch_script_path);
4497 got_repo_close(repo);
4499 got_worktree_close(worktree);
4508 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4513 struct collect_commit_logmsg_arg {
4514 const char *cmdline_log;
4516 const char *worktree_path;
4517 const char *branch_name;
4518 const char *repo_path;
4523 static const struct got_error *
4524 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4527 char *initial_content = NULL;
4528 struct got_pathlist_entry *pe;
4529 const struct got_error *err = NULL;
4530 char *template = NULL;
4531 struct collect_commit_logmsg_arg *a = arg;
4535 /* if a message was specified on the command line, just use it */
4536 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4537 len = strlen(a->cmdline_log) + 1;
4538 *logmsg = malloc(len + 1);
4539 if (*logmsg == NULL)
4540 return got_error_from_errno("malloc");
4541 strlcpy(*logmsg, a->cmdline_log, len);
4545 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4546 return got_error_from_errno("asprintf");
4548 if (asprintf(&initial_content,
4549 "\n# changes to be committed on branch %s:\n",
4550 a->branch_name) == -1)
4551 return got_error_from_errno("asprintf");
4553 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4557 dprintf(fd, initial_content);
4559 TAILQ_FOREACH(pe, commitable_paths, entry) {
4560 struct got_commitable *ct = pe->data;
4561 dprintf(fd, "# %c %s\n",
4562 got_commitable_get_status(ct),
4563 got_commitable_get_path(ct));
4567 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4569 free(initial_content);
4572 /* Editor is done; we can now apply unveil(2) */
4574 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4583 static const struct got_error *
4584 cmd_commit(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_worktree *worktree = NULL;
4588 struct got_repository *repo = NULL;
4589 char *cwd = NULL, *id_str = NULL;
4590 struct got_object_id *id = NULL;
4591 const char *logmsg = NULL;
4592 struct collect_commit_logmsg_arg cl_arg;
4593 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4594 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4595 struct got_pathlist_head paths;
4598 cl_arg.logmsg_path = NULL;
4600 while ((ch = getopt(argc, argv, "m:")) != -1) {
4615 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4616 "unveil", NULL) == -1)
4619 cwd = getcwd(NULL, 0);
4621 error = got_error_from_errno("getcwd");
4624 error = got_worktree_open(&worktree, cwd);
4628 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4631 if (rebase_in_progress) {
4632 error = got_error(GOT_ERR_REBASING);
4636 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4641 error = get_gitconfig_path(&gitconfig_path);
4644 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4649 error = get_author(&author, repo);
4654 * unveil(2) traverses exec(2); if an editor is used we have
4655 * to apply unveil after the log message has been written.
4657 if (logmsg == NULL || strlen(logmsg) == 0)
4658 error = get_editor(&editor);
4660 error = apply_unveil(got_repo_get_path(repo), 0,
4661 got_worktree_get_root_path(worktree));
4665 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4669 cl_arg.editor = editor;
4670 cl_arg.cmdline_log = logmsg;
4671 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4672 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4673 if (!histedit_in_progress) {
4674 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4675 error = got_error(GOT_ERR_COMMIT_BRANCH);
4678 cl_arg.branch_name += 11;
4680 cl_arg.repo_path = got_repo_get_path(repo);
4681 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4682 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4684 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4685 cl_arg.logmsg_path != NULL)
4686 preserve_logmsg = 1;
4690 error = got_object_id_str(&id_str, id);
4693 printf("Created commit %s\n", id_str);
4695 if (preserve_logmsg) {
4696 fprintf(stderr, "%s: log message preserved in %s\n",
4697 getprogname(), cl_arg.logmsg_path);
4698 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4700 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4701 free(cl_arg.logmsg_path);
4703 got_repo_close(repo);
4705 got_worktree_close(worktree);
4708 free(gitconfig_path);
4715 usage_cherrypick(void)
4717 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4721 static const struct got_error *
4722 cmd_cherrypick(int argc, char *argv[])
4724 const struct got_error *error = NULL;
4725 struct got_worktree *worktree = NULL;
4726 struct got_repository *repo = NULL;
4727 char *cwd = NULL, *commit_id_str = NULL;
4728 struct got_object_id *commit_id = NULL;
4729 struct got_commit_object *commit = NULL;
4730 struct got_object_qid *pid;
4731 struct got_reference *head_ref = NULL;
4732 int ch, did_something = 0;
4734 while ((ch = getopt(argc, argv, "")) != -1) {
4746 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4747 "unveil", NULL) == -1)
4753 cwd = getcwd(NULL, 0);
4755 error = got_error_from_errno("getcwd");
4758 error = got_worktree_open(&worktree, cwd);
4762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4767 error = apply_unveil(got_repo_get_path(repo), 0,
4768 got_worktree_get_root_path(worktree));
4772 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4773 GOT_OBJ_TYPE_COMMIT, repo);
4774 if (error != NULL) {
4775 struct got_reference *ref;
4776 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4778 error = got_ref_open(&ref, repo, argv[0], 0);
4781 error = got_ref_resolve(&commit_id, repo, ref);
4786 error = got_object_id_str(&commit_id_str, commit_id);
4790 error = got_ref_open(&head_ref, repo,
4791 got_worktree_get_head_ref_name(worktree), 0);
4795 error = check_same_branch(commit_id, head_ref, NULL, repo);
4797 if (error->code != GOT_ERR_ANCESTRY)
4801 error = got_error(GOT_ERR_SAME_BRANCH);
4805 error = got_object_open_as_commit(&commit, repo, commit_id);
4808 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4809 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4810 commit_id, repo, update_progress, &did_something, check_cancelled,
4816 printf("Merged commit %s\n", commit_id_str);
4819 got_object_commit_close(commit);
4820 free(commit_id_str);
4822 got_ref_close(head_ref);
4824 got_worktree_close(worktree);
4826 got_repo_close(repo);
4833 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4837 static const struct got_error *
4838 cmd_backout(int argc, char *argv[])
4840 const struct got_error *error = NULL;
4841 struct got_worktree *worktree = NULL;
4842 struct got_repository *repo = NULL;
4843 char *cwd = NULL, *commit_id_str = NULL;
4844 struct got_object_id *commit_id = NULL;
4845 struct got_commit_object *commit = NULL;
4846 struct got_object_qid *pid;
4847 struct got_reference *head_ref = NULL;
4848 int ch, did_something = 0;
4850 while ((ch = getopt(argc, argv, "")) != -1) {
4862 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4863 "unveil", NULL) == -1)
4869 cwd = getcwd(NULL, 0);
4871 error = got_error_from_errno("getcwd");
4874 error = got_worktree_open(&worktree, cwd);
4878 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4883 error = apply_unveil(got_repo_get_path(repo), 0,
4884 got_worktree_get_root_path(worktree));
4888 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4889 GOT_OBJ_TYPE_COMMIT, repo);
4890 if (error != NULL) {
4891 struct got_reference *ref;
4892 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4894 error = got_ref_open(&ref, repo, argv[0], 0);
4897 error = got_ref_resolve(&commit_id, repo, ref);
4902 error = got_object_id_str(&commit_id_str, commit_id);
4906 error = got_ref_open(&head_ref, repo,
4907 got_worktree_get_head_ref_name(worktree), 0);
4911 error = check_same_branch(commit_id, head_ref, NULL, repo);
4915 error = got_object_open_as_commit(&commit, repo, commit_id);
4918 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4920 error = got_error(GOT_ERR_ROOT_COMMIT);
4924 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4925 update_progress, &did_something, check_cancelled, NULL);
4930 printf("Backed out commit %s\n", commit_id_str);
4933 got_object_commit_close(commit);
4934 free(commit_id_str);
4936 got_ref_close(head_ref);
4938 got_worktree_close(worktree);
4940 got_repo_close(repo);
4947 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4953 trim_logmsg(char *logmsg, int limit)
4958 len = strlen(logmsg);
4962 nl = strchr(logmsg, '\n');
4967 static const struct got_error *
4968 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4970 const struct got_error *err;
4971 char *logmsg0 = NULL;
4974 err = got_object_commit_get_logmsg(&logmsg0, commit);
4979 while (isspace((unsigned char)s[0]))
4982 *logmsg = strdup(s);
4983 if (*logmsg == NULL) {
4984 err = got_error_from_errno("strdup");
4988 trim_logmsg(*logmsg, limit);
4994 static const struct got_error *
4995 show_rebase_progress(struct got_commit_object *commit,
4996 struct got_object_id *old_id, struct got_object_id *new_id)
4998 const struct got_error *err;
4999 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5001 err = got_object_id_str(&old_id_str, old_id);
5006 err = got_object_id_str(&new_id_str, new_id);
5011 old_id_str[12] = '\0';
5013 new_id_str[12] = '\0';
5015 err = get_short_logmsg(&logmsg, 42, commit);
5019 printf("%s -> %s: %s\n", old_id_str,
5020 new_id_str ? new_id_str : "no-op change", logmsg);
5027 static const struct got_error *
5028 rebase_progress(void *arg, unsigned char status, const char *path)
5030 unsigned char *rebase_status = arg;
5032 while (path[0] == '/')
5034 printf("%c %s\n", status, path);
5036 if (*rebase_status == GOT_STATUS_CONFLICT)
5038 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5039 *rebase_status = status;
5043 static const struct got_error *
5044 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5045 struct got_reference *branch, struct got_reference *new_base_branch,
5046 struct got_reference *tmp_branch, struct got_repository *repo)
5048 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5049 return got_worktree_rebase_complete(worktree, fileindex,
5050 new_base_branch, tmp_branch, branch, repo);
5053 static const struct got_error *
5054 rebase_commit(struct got_pathlist_head *merged_paths,
5055 struct got_worktree *worktree, struct got_fileindex *fileindex,
5056 struct got_reference *tmp_branch,
5057 struct got_object_id *commit_id, struct got_repository *repo)
5059 const struct got_error *error;
5060 struct got_commit_object *commit;
5061 struct got_object_id *new_commit_id;
5063 error = got_object_open_as_commit(&commit, repo, commit_id);
5067 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5068 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5070 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5072 error = show_rebase_progress(commit, commit_id, NULL);
5074 error = show_rebase_progress(commit, commit_id, new_commit_id);
5075 free(new_commit_id);
5078 got_object_commit_close(commit);
5082 struct check_path_prefix_arg {
5083 const char *path_prefix;
5088 static const struct got_error *
5089 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5090 struct got_blob_object *blob2, struct got_object_id *id1,
5091 struct got_object_id *id2, const char *path1, const char *path2,
5092 mode_t mode1, mode_t mode2, struct got_repository *repo)
5094 struct check_path_prefix_arg *a = arg;
5096 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5097 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5098 return got_error(a->errcode);
5103 static const struct got_error *
5104 check_path_prefix(struct got_object_id *parent_id,
5105 struct got_object_id *commit_id, const char *path_prefix,
5106 int errcode, struct got_repository *repo)
5108 const struct got_error *err;
5109 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5110 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5111 struct check_path_prefix_arg cpp_arg;
5113 if (got_path_is_root_dir(path_prefix))
5116 err = got_object_open_as_commit(&commit, repo, commit_id);
5120 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5124 err = got_object_open_as_tree(&tree1, repo,
5125 got_object_commit_get_tree_id(parent_commit));
5129 err = got_object_open_as_tree(&tree2, repo,
5130 got_object_commit_get_tree_id(commit));
5134 cpp_arg.path_prefix = path_prefix;
5135 while (cpp_arg.path_prefix[0] == '/')
5136 cpp_arg.path_prefix++;
5137 cpp_arg.len = strlen(cpp_arg.path_prefix);
5138 cpp_arg.errcode = errcode;
5139 err = got_diff_tree(tree1, tree2, "", "", repo,
5140 check_path_prefix_in_diff, &cpp_arg, 0);
5143 got_object_tree_close(tree1);
5145 got_object_tree_close(tree2);
5147 got_object_commit_close(commit);
5149 got_object_commit_close(parent_commit);
5153 static const struct got_error *
5154 collect_commits(struct got_object_id_queue *commits,
5155 struct got_object_id *initial_commit_id,
5156 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5157 const char *path_prefix, int path_prefix_errcode,
5158 struct got_repository *repo)
5160 const struct got_error *err = NULL;
5161 struct got_commit_graph *graph = NULL;
5162 struct got_object_id *parent_id = NULL;
5163 struct got_object_qid *qid;
5164 struct got_object_id *commit_id = initial_commit_id;
5166 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5170 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5171 check_cancelled, NULL);
5174 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5175 err = got_commit_graph_iter_next(&parent_id, graph);
5177 if (err->code == GOT_ERR_ITER_COMPLETED) {
5178 err = got_error_msg(GOT_ERR_ANCESTRY,
5179 "ran out of commits to rebase before "
5180 "youngest common ancestor commit has "
5183 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5185 err = got_commit_graph_fetch_commits(graph, 1, repo,
5186 check_cancelled, NULL);
5190 err = check_path_prefix(parent_id, commit_id,
5191 path_prefix, path_prefix_errcode, repo);
5195 err = got_object_qid_alloc(&qid, commit_id);
5198 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5199 commit_id = parent_id;
5203 got_commit_graph_close(graph);
5207 static const struct got_error *
5208 cmd_rebase(int argc, char *argv[])
5210 const struct got_error *error = NULL;
5211 struct got_worktree *worktree = NULL;
5212 struct got_repository *repo = NULL;
5213 struct got_fileindex *fileindex = NULL;
5215 struct got_reference *branch = NULL;
5216 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5217 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5218 struct got_object_id *resume_commit_id = NULL;
5219 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5220 struct got_commit_object *commit = NULL;
5221 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5222 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5223 struct got_object_id_queue commits;
5224 struct got_pathlist_head merged_paths;
5225 const struct got_object_id_queue *parent_ids;
5226 struct got_object_qid *qid, *pid;
5228 SIMPLEQ_INIT(&commits);
5229 TAILQ_INIT(&merged_paths);
5231 while ((ch = getopt(argc, argv, "ac")) != -1) {
5237 continue_rebase = 1;
5249 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5250 "unveil", NULL) == -1)
5253 if (abort_rebase && continue_rebase)
5255 else if (abort_rebase || continue_rebase) {
5258 } else if (argc != 1)
5261 cwd = getcwd(NULL, 0);
5263 error = got_error_from_errno("getcwd");
5266 error = got_worktree_open(&worktree, cwd);
5270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5275 error = apply_unveil(got_repo_get_path(repo), 0,
5276 got_worktree_get_root_path(worktree));
5280 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5286 if (!rebase_in_progress) {
5287 error = got_error(GOT_ERR_NOT_REBASING);
5290 error = got_worktree_rebase_continue(&resume_commit_id,
5291 &new_base_branch, &tmp_branch, &branch, &fileindex,
5295 printf("Switching work tree to %s\n",
5296 got_ref_get_symref_target(new_base_branch));
5297 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5298 new_base_branch, update_progress, &did_something);
5301 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5302 goto done; /* nothing else to do */
5305 if (continue_rebase) {
5306 if (!rebase_in_progress) {
5307 error = got_error(GOT_ERR_NOT_REBASING);
5310 error = got_worktree_rebase_continue(&resume_commit_id,
5311 &new_base_branch, &tmp_branch, &branch, &fileindex,
5316 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5317 resume_commit_id, repo);
5321 yca_id = got_object_id_dup(resume_commit_id);
5322 if (yca_id == NULL) {
5323 error = got_error_from_errno("got_object_id_dup");
5327 error = got_ref_open(&branch, repo, argv[0], 0);
5332 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5336 if (!continue_rebase) {
5337 struct got_object_id *base_commit_id;
5339 base_commit_id = got_worktree_get_base_commit_id(worktree);
5340 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5341 base_commit_id, branch_head_commit_id, repo,
5342 check_cancelled, NULL);
5345 if (yca_id == NULL) {
5346 error = got_error_msg(GOT_ERR_ANCESTRY,
5347 "specified branch shares no common ancestry "
5348 "with work tree's branch");
5352 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5354 if (error->code != GOT_ERR_ANCESTRY)
5358 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5359 "specified branch resolves to a commit which "
5360 "is already contained in work tree's branch");
5363 error = got_worktree_rebase_prepare(&new_base_branch,
5364 &tmp_branch, &fileindex, worktree, branch, repo);
5369 commit_id = branch_head_commit_id;
5370 error = got_object_open_as_commit(&commit, repo, commit_id);
5374 parent_ids = got_object_commit_get_parent_ids(commit);
5375 pid = SIMPLEQ_FIRST(parent_ids);
5377 if (!continue_rebase) {
5379 error = got_worktree_rebase_abort(worktree, fileindex,
5380 repo, new_base_branch, update_progress,
5384 printf("Rebase of %s aborted\n",
5385 got_ref_get_name(branch));
5387 error = got_error(GOT_ERR_EMPTY_REBASE);
5390 error = collect_commits(&commits, commit_id, pid->id,
5391 yca_id, got_worktree_get_path_prefix(worktree),
5392 GOT_ERR_REBASE_PATH, repo);
5393 got_object_commit_close(commit);
5398 if (SIMPLEQ_EMPTY(&commits)) {
5399 if (continue_rebase)
5400 error = rebase_complete(worktree, fileindex,
5401 branch, new_base_branch, tmp_branch, repo);
5403 error = got_error(GOT_ERR_EMPTY_REBASE);
5408 SIMPLEQ_FOREACH(qid, &commits, entry) {
5409 commit_id = qid->id;
5410 parent_id = pid ? pid->id : yca_id;
5413 error = got_worktree_rebase_merge_files(&merged_paths,
5414 worktree, fileindex, parent_id, commit_id, repo,
5415 rebase_progress, &rebase_status, check_cancelled, NULL);
5419 if (rebase_status == GOT_STATUS_CONFLICT) {
5420 got_worktree_rebase_pathlist_free(&merged_paths);
5424 error = rebase_commit(&merged_paths, worktree, fileindex,
5425 tmp_branch, commit_id, repo);
5426 got_worktree_rebase_pathlist_free(&merged_paths);
5431 if (rebase_status == GOT_STATUS_CONFLICT) {
5432 error = got_worktree_rebase_postpone(worktree, fileindex);
5435 error = got_error_msg(GOT_ERR_CONFLICTS,
5436 "conflicts must be resolved before rebasing can continue");
5438 error = rebase_complete(worktree, fileindex, branch,
5439 new_base_branch, tmp_branch, repo);
5441 got_object_id_queue_free(&commits);
5442 free(branch_head_commit_id);
5443 free(resume_commit_id);
5446 got_object_commit_close(commit);
5448 got_ref_close(branch);
5449 if (new_base_branch)
5450 got_ref_close(new_base_branch);
5452 got_ref_close(tmp_branch);
5454 got_worktree_close(worktree);
5456 got_repo_close(repo);
5461 usage_histedit(void)
5463 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5468 #define GOT_HISTEDIT_PICK 'p'
5469 #define GOT_HISTEDIT_EDIT 'e'
5470 #define GOT_HISTEDIT_FOLD 'f'
5471 #define GOT_HISTEDIT_DROP 'd'
5472 #define GOT_HISTEDIT_MESG 'm'
5474 static struct got_histedit_cmd {
5478 } got_histedit_cmds[] = {
5479 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5480 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5481 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5482 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5483 { GOT_HISTEDIT_MESG, "mesg",
5484 "single-line log message for commit above (open editor if empty)" },
5487 struct got_histedit_list_entry {
5488 TAILQ_ENTRY(got_histedit_list_entry) entry;
5489 struct got_object_id *commit_id;
5490 const struct got_histedit_cmd *cmd;
5493 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5495 static const struct got_error *
5496 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5497 FILE *f, struct got_repository *repo)
5499 const struct got_error *err = NULL;
5500 char *logmsg = NULL, *id_str = NULL;
5501 struct got_commit_object *commit = NULL;
5504 err = got_object_open_as_commit(&commit, repo, commit_id);
5508 err = get_short_logmsg(&logmsg, 34, commit);
5512 err = got_object_id_str(&id_str, commit_id);
5516 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5518 err = got_ferror(f, GOT_ERR_IO);
5521 got_object_commit_close(commit);
5527 static const struct got_error *
5528 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5529 struct got_repository *repo)
5531 const struct got_error *err = NULL;
5532 struct got_object_qid *qid;
5534 if (SIMPLEQ_EMPTY(commits))
5535 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5537 SIMPLEQ_FOREACH(qid, commits, entry) {
5538 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5547 static const struct got_error *
5548 write_cmd_list(FILE *f)
5550 const struct got_error *err = NULL;
5553 n = fprintf(f, "# Available histedit commands:\n");
5555 return got_ferror(f, GOT_ERR_IO);
5557 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5558 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5559 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5562 err = got_ferror(f, GOT_ERR_IO);
5566 n = fprintf(f, "# Commits will be processed in order from top to "
5567 "bottom of this file.\n");
5569 return got_ferror(f, GOT_ERR_IO);
5573 static const struct got_error *
5574 histedit_syntax_error(int lineno)
5576 static char msg[42];
5579 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5581 if (ret == -1 || ret >= sizeof(msg))
5582 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5584 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5587 static const struct got_error *
5588 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5589 char *logmsg, struct got_repository *repo)
5591 const struct got_error *err;
5592 struct got_commit_object *folded_commit = NULL;
5593 char *id_str, *folded_logmsg = NULL;
5595 err = got_object_id_str(&id_str, hle->commit_id);
5599 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5603 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5606 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5607 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5608 folded_logmsg) == -1) {
5609 err = got_error_from_errno("asprintf");
5614 got_object_commit_close(folded_commit);
5616 free(folded_logmsg);
5620 static struct got_histedit_list_entry *
5621 get_folded_commits(struct got_histedit_list_entry *hle)
5623 struct got_histedit_list_entry *prev, *folded = NULL;
5625 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5626 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5627 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5628 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5630 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5636 static const struct got_error *
5637 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5638 struct got_repository *repo)
5640 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5641 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5642 const struct got_error *err = NULL;
5643 struct got_commit_object *commit = NULL;
5645 struct got_histedit_list_entry *folded = NULL;
5647 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5651 folded = get_folded_commits(hle);
5653 while (folded != hle) {
5654 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5655 folded = TAILQ_NEXT(folded, entry);
5658 err = append_folded_commit_msg(&new_msg, folded,
5664 folded = TAILQ_NEXT(folded, entry);
5668 err = got_object_id_str(&id_str, hle->commit_id);
5671 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5674 if (asprintf(&new_msg,
5675 "%s\n# original log message of commit %s: %s",
5676 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5677 err = got_error_from_errno("asprintf");
5683 err = got_object_id_str(&id_str, hle->commit_id);
5687 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5691 dprintf(fd, logmsg);
5694 err = get_editor(&editor);
5698 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5700 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5702 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5705 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5706 err = got_error_from_errno2("unlink", logmsg_path);
5712 got_object_commit_close(commit);
5716 static const struct got_error *
5717 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5718 FILE *f, struct got_repository *repo)
5720 const struct got_error *err = NULL;
5721 char *line = NULL, *p, *end;
5725 const struct got_histedit_cmd *cmd;
5726 struct got_object_id *commit_id = NULL;
5727 struct got_histedit_list_entry *hle = NULL;
5730 len = getline(&line, &size, f);
5732 const struct got_error *getline_err;
5735 getline_err = got_error_from_errno("getline");
5736 err = got_ferror(f, getline_err->code);
5741 while (isspace((unsigned char)p[0]))
5743 if (p[0] == '#' || p[0] == '\0') {
5749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5750 cmd = &got_histedit_cmds[i];
5751 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5752 isspace((unsigned char)p[strlen(cmd->name)])) {
5753 p += strlen(cmd->name);
5756 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5761 if (i == nitems(got_histedit_cmds)) {
5762 err = histedit_syntax_error(lineno);
5765 while (isspace((unsigned char)p[0]))
5767 if (cmd->code == GOT_HISTEDIT_MESG) {
5768 if (hle == NULL || hle->logmsg != NULL) {
5769 err = got_error(GOT_ERR_HISTEDIT_CMD);
5773 err = histedit_edit_logmsg(hle, repo);
5777 hle->logmsg = strdup(p);
5778 if (hle->logmsg == NULL) {
5779 err = got_error_from_errno("strdup");
5788 while (end[0] && !isspace((unsigned char)end[0]))
5792 err = got_object_resolve_id_str(&commit_id, repo, p);
5794 /* override error code */
5795 err = histedit_syntax_error(lineno);
5799 hle = malloc(sizeof(*hle));
5801 err = got_error_from_errno("malloc");
5805 hle->commit_id = commit_id;
5810 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5818 static const struct got_error *
5819 histedit_check_script(struct got_histedit_list *histedit_cmds,
5820 struct got_object_id_queue *commits, struct got_repository *repo)
5822 const struct got_error *err = NULL;
5823 struct got_object_qid *qid;
5824 struct got_histedit_list_entry *hle;
5825 static char msg[80];
5828 if (TAILQ_EMPTY(histedit_cmds))
5829 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5830 "histedit script contains no commands");
5831 if (SIMPLEQ_EMPTY(commits))
5832 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5834 SIMPLEQ_FOREACH(qid, commits, entry) {
5835 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5836 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5840 err = got_object_id_str(&id_str, qid->id);
5843 snprintf(msg, sizeof(msg),
5844 "commit %s missing from histedit script", id_str);
5846 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5850 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5851 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5852 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5853 "last commit in histedit script cannot be folded");
5858 static const struct got_error *
5859 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5860 const char *path, struct got_object_id_queue *commits,
5861 struct got_repository *repo)
5863 const struct got_error *err = NULL;
5867 err = get_editor(&editor);
5871 if (spawn_editor(editor, path) == -1) {
5872 err = got_error_from_errno("failed spawning editor");
5876 f = fopen(path, "r");
5878 err = got_error_from_errno("fopen");
5881 err = histedit_parse_list(histedit_cmds, f, repo);
5885 err = histedit_check_script(histedit_cmds, commits, repo);
5887 if (f && fclose(f) != 0 && err == NULL)
5888 err = got_error_from_errno("fclose");
5893 static const struct got_error *
5894 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5895 struct got_object_id_queue *, const char *, struct got_repository *);
5897 static const struct got_error *
5898 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5899 struct got_object_id_queue *commits, struct got_repository *repo)
5901 const struct got_error *err;
5905 err = got_opentemp_named(&path, &f, "got-histedit");
5909 err = write_cmd_list(f);
5913 err = histedit_write_commit_list(commits, f, repo);
5917 if (fclose(f) != 0) {
5918 err = got_error_from_errno("fclose");
5923 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5925 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5926 err->code != GOT_ERR_HISTEDIT_CMD)
5928 err = histedit_edit_list_retry(histedit_cmds, err,
5929 commits, path, repo);
5932 if (f && fclose(f) != 0 && err == NULL)
5933 err = got_error_from_errno("fclose");
5934 if (path && unlink(path) != 0 && err == NULL)
5935 err = got_error_from_errno2("unlink", path);
5940 static const struct got_error *
5941 histedit_save_list(struct got_histedit_list *histedit_cmds,
5942 struct got_worktree *worktree, struct got_repository *repo)
5944 const struct got_error *err = NULL;
5947 struct got_histedit_list_entry *hle;
5948 struct got_commit_object *commit = NULL;
5950 err = got_worktree_get_histedit_script_path(&path, worktree);
5954 f = fopen(path, "w");
5956 err = got_error_from_errno2("fopen", path);
5959 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5960 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5966 int n = fprintf(f, "%c %s\n",
5967 GOT_HISTEDIT_MESG, hle->logmsg);
5969 err = got_ferror(f, GOT_ERR_IO);
5975 if (f && fclose(f) != 0 && err == NULL)
5976 err = got_error_from_errno("fclose");
5979 got_object_commit_close(commit);
5984 histedit_free_list(struct got_histedit_list *histedit_cmds)
5986 struct got_histedit_list_entry *hle;
5988 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5989 TAILQ_REMOVE(histedit_cmds, hle, entry);
5994 static const struct got_error *
5995 histedit_load_list(struct got_histedit_list *histedit_cmds,
5996 const char *path, struct got_repository *repo)
5998 const struct got_error *err = NULL;
6001 f = fopen(path, "r");
6003 err = got_error_from_errno2("fopen", path);
6007 err = histedit_parse_list(histedit_cmds, f, repo);
6009 if (f && fclose(f) != 0 && err == NULL)
6010 err = got_error_from_errno("fclose");
6014 static const struct got_error *
6015 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6016 const struct got_error *edit_err, struct got_object_id_queue *commits,
6017 const char *path, struct got_repository *repo)
6019 const struct got_error *err = NULL, *prev_err = edit_err;
6022 while (resp != 'c' && resp != 'r' && resp != 'a') {
6023 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6024 "or (a)bort: ", getprogname(), prev_err->msg);
6029 histedit_free_list(histedit_cmds);
6030 err = histedit_run_editor(histedit_cmds, path, commits,
6033 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6034 err->code != GOT_ERR_HISTEDIT_CMD)
6041 } else if (resp == 'r') {
6042 histedit_free_list(histedit_cmds);
6043 err = histedit_edit_script(histedit_cmds,
6046 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6047 err->code != GOT_ERR_HISTEDIT_CMD)
6054 } else if (resp == 'a') {
6055 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6058 printf("invalid response '%c'\n", resp);
6064 static const struct got_error *
6065 histedit_complete(struct got_worktree *worktree,
6066 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6067 struct got_reference *branch, struct got_repository *repo)
6069 printf("Switching work tree to %s\n",
6070 got_ref_get_symref_target(branch));
6071 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6075 static const struct got_error *
6076 show_histedit_progress(struct got_commit_object *commit,
6077 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6079 const struct got_error *err;
6080 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6082 err = got_object_id_str(&old_id_str, hle->commit_id);
6087 err = got_object_id_str(&new_id_str, new_id);
6092 old_id_str[12] = '\0';
6094 new_id_str[12] = '\0';
6097 logmsg = strdup(hle->logmsg);
6098 if (logmsg == NULL) {
6099 err = got_error_from_errno("strdup");
6102 trim_logmsg(logmsg, 42);
6104 err = get_short_logmsg(&logmsg, 42, commit);
6109 switch (hle->cmd->code) {
6110 case GOT_HISTEDIT_PICK:
6111 case GOT_HISTEDIT_EDIT:
6112 printf("%s -> %s: %s\n", old_id_str,
6113 new_id_str ? new_id_str : "no-op change", logmsg);
6115 case GOT_HISTEDIT_DROP:
6116 case GOT_HISTEDIT_FOLD:
6117 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6130 static const struct got_error *
6131 histedit_commit(struct got_pathlist_head *merged_paths,
6132 struct got_worktree *worktree, struct got_fileindex *fileindex,
6133 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6134 struct got_repository *repo)
6136 const struct got_error *err;
6137 struct got_commit_object *commit;
6138 struct got_object_id *new_commit_id;
6140 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6141 && hle->logmsg == NULL) {
6142 err = histedit_edit_logmsg(hle, repo);
6147 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6151 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6152 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6155 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6157 err = show_histedit_progress(commit, hle, NULL);
6159 err = show_histedit_progress(commit, hle, new_commit_id);
6160 free(new_commit_id);
6163 got_object_commit_close(commit);
6167 static const struct got_error *
6168 histedit_skip_commit(struct got_histedit_list_entry *hle,
6169 struct got_worktree *worktree, struct got_repository *repo)
6171 const struct got_error *error;
6172 struct got_commit_object *commit;
6174 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6179 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6183 error = show_histedit_progress(commit, hle, NULL);
6184 got_object_commit_close(commit);
6188 static const struct got_error *
6189 cmd_histedit(int argc, char *argv[])
6191 const struct got_error *error = NULL;
6192 struct got_worktree *worktree = NULL;
6193 struct got_fileindex *fileindex = NULL;
6194 struct got_repository *repo = NULL;
6196 struct got_reference *branch = NULL;
6197 struct got_reference *tmp_branch = NULL;
6198 struct got_object_id *resume_commit_id = NULL;
6199 struct got_object_id *base_commit_id = NULL;
6200 struct got_object_id *head_commit_id = NULL;
6201 struct got_commit_object *commit = NULL;
6202 int ch, rebase_in_progress = 0, did_something;
6203 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6204 const char *edit_script_path = NULL;
6205 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6206 struct got_object_id_queue commits;
6207 struct got_pathlist_head merged_paths;
6208 const struct got_object_id_queue *parent_ids;
6209 struct got_object_qid *pid;
6210 struct got_histedit_list histedit_cmds;
6211 struct got_histedit_list_entry *hle;
6213 SIMPLEQ_INIT(&commits);
6214 TAILQ_INIT(&histedit_cmds);
6215 TAILQ_INIT(&merged_paths);
6217 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6226 edit_script_path = optarg;
6238 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6239 "unveil", NULL) == -1)
6242 if (abort_edit && continue_edit)
6248 * This command cannot apply unveil(2) in all cases because the
6249 * user may choose to run an editor to edit the histedit script
6250 * and to edit individual commit log messages.
6251 * unveil(2) traverses exec(2); if an editor is used we have to
6252 * apply unveil after edit script and log messages have been written.
6253 * XXX TODO: Make use of unveil(2) where possible.
6256 cwd = getcwd(NULL, 0);
6258 error = got_error_from_errno("getcwd");
6261 error = got_worktree_open(&worktree, cwd);
6265 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6270 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6273 if (rebase_in_progress) {
6274 error = got_error(GOT_ERR_REBASING);
6278 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6282 if (edit_in_progress && abort_edit) {
6283 error = got_worktree_histedit_continue(&resume_commit_id,
6284 &tmp_branch, &branch, &base_commit_id, &fileindex,
6288 printf("Switching work tree to %s\n",
6289 got_ref_get_symref_target(branch));
6290 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6291 branch, base_commit_id, update_progress, &did_something);
6294 printf("Histedit of %s aborted\n",
6295 got_ref_get_symref_target(branch));
6296 goto done; /* nothing else to do */
6297 } else if (abort_edit) {
6298 error = got_error(GOT_ERR_NOT_HISTEDIT);
6302 if (continue_edit) {
6305 if (!edit_in_progress) {
6306 error = got_error(GOT_ERR_NOT_HISTEDIT);
6310 error = got_worktree_get_histedit_script_path(&path, worktree);
6314 error = histedit_load_list(&histedit_cmds, path, repo);
6319 error = got_worktree_histedit_continue(&resume_commit_id,
6320 &tmp_branch, &branch, &base_commit_id, &fileindex,
6325 error = got_ref_resolve(&head_commit_id, repo, branch);
6329 error = got_object_open_as_commit(&commit, repo,
6333 parent_ids = got_object_commit_get_parent_ids(commit);
6334 pid = SIMPLEQ_FIRST(parent_ids);
6336 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6339 error = collect_commits(&commits, head_commit_id, pid->id,
6340 base_commit_id, got_worktree_get_path_prefix(worktree),
6341 GOT_ERR_HISTEDIT_PATH, repo);
6342 got_object_commit_close(commit);
6347 if (edit_in_progress) {
6348 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6352 error = got_ref_open(&branch, repo,
6353 got_worktree_get_head_ref_name(worktree), 0);
6357 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6358 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6359 "will not edit commit history of a branch outside "
6360 "the \"refs/heads/\" reference namespace");
6364 error = got_ref_resolve(&head_commit_id, repo, branch);
6365 got_ref_close(branch);
6370 error = got_object_open_as_commit(&commit, repo,
6374 parent_ids = got_object_commit_get_parent_ids(commit);
6375 pid = SIMPLEQ_FIRST(parent_ids);
6377 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6380 error = collect_commits(&commits, head_commit_id, pid->id,
6381 got_worktree_get_base_commit_id(worktree),
6382 got_worktree_get_path_prefix(worktree),
6383 GOT_ERR_HISTEDIT_PATH, repo);
6384 got_object_commit_close(commit);
6389 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6390 &base_commit_id, &fileindex, worktree, repo);
6394 if (edit_script_path) {
6395 error = histedit_load_list(&histedit_cmds,
6396 edit_script_path, repo);
6398 got_worktree_histedit_abort(worktree, fileindex,
6399 repo, branch, base_commit_id,
6400 update_progress, &did_something);
6404 error = histedit_edit_script(&histedit_cmds, &commits,
6407 got_worktree_histedit_abort(worktree, fileindex,
6408 repo, branch, base_commit_id,
6409 update_progress, &did_something);
6415 error = histedit_save_list(&histedit_cmds, worktree,
6418 got_worktree_histedit_abort(worktree, fileindex,
6419 repo, branch, base_commit_id,
6420 update_progress, &did_something);
6426 error = histedit_check_script(&histedit_cmds, &commits, repo);
6430 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6431 if (resume_commit_id) {
6432 if (got_object_id_cmp(hle->commit_id,
6433 resume_commit_id) != 0)
6436 resume_commit_id = NULL;
6437 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6438 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6439 error = histedit_skip_commit(hle, worktree,
6442 error = histedit_commit(NULL, worktree,
6443 fileindex, tmp_branch, hle, repo);
6450 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6451 error = histedit_skip_commit(hle, worktree, repo);
6457 error = got_object_open_as_commit(&commit, repo,
6461 parent_ids = got_object_commit_get_parent_ids(commit);
6462 pid = SIMPLEQ_FIRST(parent_ids);
6464 error = got_worktree_histedit_merge_files(&merged_paths,
6465 worktree, fileindex, pid->id, hle->commit_id, repo,
6466 rebase_progress, &rebase_status, check_cancelled, NULL);
6469 got_object_commit_close(commit);
6472 if (rebase_status == GOT_STATUS_CONFLICT) {
6473 got_worktree_rebase_pathlist_free(&merged_paths);
6477 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6479 error = got_object_id_str(&id_str, hle->commit_id);
6482 printf("Stopping histedit for amending commit %s\n",
6485 got_worktree_rebase_pathlist_free(&merged_paths);
6486 error = got_worktree_histedit_postpone(worktree,
6491 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6492 error = histedit_skip_commit(hle, worktree, repo);
6498 error = histedit_commit(&merged_paths, worktree, fileindex,
6499 tmp_branch, hle, repo);
6500 got_worktree_rebase_pathlist_free(&merged_paths);
6505 if (rebase_status == GOT_STATUS_CONFLICT) {
6506 error = got_worktree_histedit_postpone(worktree, fileindex);
6509 error = got_error_msg(GOT_ERR_CONFLICTS,
6510 "conflicts must be resolved before rebasing can continue");
6512 error = histedit_complete(worktree, fileindex, tmp_branch,
6515 got_object_id_queue_free(&commits);
6516 histedit_free_list(&histedit_cmds);
6517 free(head_commit_id);
6518 free(base_commit_id);
6519 free(resume_commit_id);
6521 got_object_commit_close(commit);
6523 got_ref_close(branch);
6525 got_ref_close(tmp_branch);
6527 got_worktree_close(worktree);
6529 got_repo_close(repo);
6534 usage_integrate(void)
6536 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6540 static const struct got_error *
6541 cmd_integrate(int argc, char *argv[])
6543 const struct got_error *error = NULL;
6544 struct got_repository *repo = NULL;
6545 struct got_worktree *worktree = NULL;
6546 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6547 const char *branch_arg = NULL;
6548 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6549 struct got_fileindex *fileindex = NULL;
6550 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6551 int ch, did_something = 0;
6553 while ((ch = getopt(argc, argv, "")) != -1) {
6566 branch_arg = argv[0];
6568 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6569 "unveil", NULL) == -1)
6572 cwd = getcwd(NULL, 0);
6574 error = got_error_from_errno("getcwd");
6578 error = got_worktree_open(&worktree, cwd);
6582 error = check_rebase_or_histedit_in_progress(worktree);
6586 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6591 error = apply_unveil(got_repo_get_path(repo), 0,
6592 got_worktree_get_root_path(worktree));
6596 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6597 error = got_error_from_errno("asprintf");
6601 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6602 &base_branch_ref, worktree, refname, repo);
6606 refname = strdup(got_ref_get_name(branch_ref));
6607 if (refname == NULL) {
6608 error = got_error_from_errno("strdup");
6609 got_worktree_integrate_abort(worktree, fileindex, repo,
6610 branch_ref, base_branch_ref);
6613 base_refname = strdup(got_ref_get_name(base_branch_ref));
6614 if (base_refname == NULL) {
6615 error = got_error_from_errno("strdup");
6616 got_worktree_integrate_abort(worktree, fileindex, repo,
6617 branch_ref, base_branch_ref);
6621 error = got_ref_resolve(&commit_id, repo, branch_ref);
6625 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6629 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6630 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6631 "specified branch has already been integrated");
6632 got_worktree_integrate_abort(worktree, fileindex, repo,
6633 branch_ref, base_branch_ref);
6637 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6639 if (error->code == GOT_ERR_ANCESTRY)
6640 error = got_error(GOT_ERR_REBASE_REQUIRED);
6641 got_worktree_integrate_abort(worktree, fileindex, repo,
6642 branch_ref, base_branch_ref);
6646 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6647 branch_ref, base_branch_ref, update_progress, &did_something,
6648 check_cancelled, NULL);
6652 printf("Integrated %s into %s\n", refname, base_refname);
6655 got_repo_close(repo);
6657 got_worktree_close(worktree);
6659 free(base_commit_id);
6669 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6670 "[file-path ...]\n",
6675 static const struct got_error *
6676 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6677 const char *path, struct got_object_id *blob_id,
6678 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6680 const struct got_error *err = NULL;
6681 char *id_str = NULL;
6683 if (staged_status != GOT_STATUS_ADD &&
6684 staged_status != GOT_STATUS_MODIFY &&
6685 staged_status != GOT_STATUS_DELETE)
6688 if (staged_status == GOT_STATUS_ADD ||
6689 staged_status == GOT_STATUS_MODIFY)
6690 err = got_object_id_str(&id_str, staged_blob_id);
6692 err = got_object_id_str(&id_str, blob_id);
6696 printf("%s %c %s\n", id_str, staged_status, path);
6701 static const struct got_error *
6702 cmd_stage(int argc, char *argv[])
6704 const struct got_error *error = NULL;
6705 struct got_repository *repo = NULL;
6706 struct got_worktree *worktree = NULL;
6708 struct got_pathlist_head paths;
6709 struct got_pathlist_entry *pe;
6710 int ch, list_stage = 0, pflag = 0;
6711 FILE *patch_script_file = NULL;
6712 const char *patch_script_path = NULL;
6713 struct choose_patch_arg cpa;
6717 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6726 patch_script_path = optarg;
6738 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6739 "unveil", NULL) == -1)
6742 if (list_stage && (pflag || patch_script_path))
6743 errx(1, "-l option cannot be used with other options");
6744 if (patch_script_path && !pflag)
6745 errx(1, "-F option can only be used together with -p option");
6747 cwd = getcwd(NULL, 0);
6749 error = got_error_from_errno("getcwd");
6753 error = got_worktree_open(&worktree, cwd);
6757 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6762 if (patch_script_path) {
6763 patch_script_file = fopen(patch_script_path, "r");
6764 if (patch_script_file == NULL) {
6765 error = got_error_from_errno2("fopen",
6770 error = apply_unveil(got_repo_get_path(repo), 0,
6771 got_worktree_get_root_path(worktree));
6775 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6780 error = got_worktree_status(worktree, &paths, repo,
6781 print_stage, NULL, check_cancelled, NULL);
6783 cpa.patch_script_file = patch_script_file;
6784 cpa.action = "stage";
6785 error = got_worktree_stage(worktree, &paths,
6786 pflag ? NULL : print_status, NULL,
6787 pflag ? choose_patch : NULL, &cpa, repo);
6790 if (patch_script_file && fclose(patch_script_file) == EOF &&
6792 error = got_error_from_errno2("fclose", patch_script_path);
6794 got_repo_close(repo);
6796 got_worktree_close(worktree);
6797 TAILQ_FOREACH(pe, &paths, entry)
6798 free((char *)pe->path);
6799 got_pathlist_free(&paths);
6807 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6808 "[file-path ...]\n",
6814 static const struct got_error *
6815 cmd_unstage(int argc, char *argv[])
6817 const struct got_error *error = NULL;
6818 struct got_repository *repo = NULL;
6819 struct got_worktree *worktree = NULL;
6821 struct got_pathlist_head paths;
6822 struct got_pathlist_entry *pe;
6823 int ch, did_something = 0, pflag = 0;
6824 FILE *patch_script_file = NULL;
6825 const char *patch_script_path = NULL;
6826 struct choose_patch_arg cpa;
6830 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6836 patch_script_path = optarg;
6848 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6849 "unveil", NULL) == -1)
6852 if (patch_script_path && !pflag)
6853 errx(1, "-F option can only be used together with -p option");
6855 cwd = getcwd(NULL, 0);
6857 error = got_error_from_errno("getcwd");
6861 error = got_worktree_open(&worktree, cwd);
6865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6870 if (patch_script_path) {
6871 patch_script_file = fopen(patch_script_path, "r");
6872 if (patch_script_file == NULL) {
6873 error = got_error_from_errno2("fopen",
6879 error = apply_unveil(got_repo_get_path(repo), 0,
6880 got_worktree_get_root_path(worktree));
6884 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6888 cpa.patch_script_file = patch_script_file;
6889 cpa.action = "unstage";
6890 error = got_worktree_unstage(worktree, &paths, update_progress,
6891 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6893 if (patch_script_file && fclose(patch_script_file) == EOF &&
6895 error = got_error_from_errno2("fclose", patch_script_path);
6897 got_repo_close(repo);
6899 got_worktree_close(worktree);
6900 TAILQ_FOREACH(pe, &paths, entry)
6901 free((char *)pe->path);
6902 got_pathlist_free(&paths);
6910 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6911 "arg1 [arg2 ...]\n", getprogname());
6915 static const struct got_error *
6916 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6918 const struct got_error *err;
6919 struct got_blob_object *blob;
6921 err = got_object_open_as_blob(&blob, repo, id, 8192);
6925 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6926 got_object_blob_close(blob);
6930 static const struct got_error *
6931 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6933 const struct got_error *err;
6934 struct got_tree_object *tree;
6935 const struct got_tree_entries *entries;
6936 struct got_tree_entry *te;
6938 err = got_object_open_as_tree(&tree, repo, id);
6942 entries = got_object_tree_get_entries(tree);
6943 te = SIMPLEQ_FIRST(&entries->head);
6946 if (sigint_received || sigpipe_received)
6948 err = got_object_id_str(&id_str, te->id);
6951 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6953 te = SIMPLEQ_NEXT(te, entry);
6956 got_object_tree_close(tree);
6960 static const struct got_error *
6961 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6963 const struct got_error *err;
6964 struct got_commit_object *commit;
6965 const struct got_object_id_queue *parent_ids;
6966 struct got_object_qid *pid;
6967 char *id_str = NULL;
6968 const char *logmsg = NULL;
6970 err = got_object_open_as_commit(&commit, repo, id);
6974 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6978 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6979 parent_ids = got_object_commit_get_parent_ids(commit);
6980 fprintf(outfile, "numparents %d\n",
6981 got_object_commit_get_nparents(commit));
6982 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6984 err = got_object_id_str(&pid_str, pid->id);
6987 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6990 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6991 got_object_commit_get_author(commit),
6992 got_object_commit_get_author_time(commit));
6994 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6995 got_object_commit_get_author(commit),
6996 got_object_commit_get_committer_time(commit));
6998 logmsg = got_object_commit_get_logmsg_raw(commit);
6999 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7000 fprintf(outfile, "%s", logmsg);
7003 got_object_commit_close(commit);
7007 static const struct got_error *
7008 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7010 const struct got_error *err;
7011 struct got_tag_object *tag;
7012 char *id_str = NULL;
7013 const char *tagmsg = NULL;
7015 err = got_object_open_as_tag(&tag, repo, id);
7019 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7023 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7025 switch (got_object_tag_get_object_type(tag)) {
7026 case GOT_OBJ_TYPE_BLOB:
7027 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7028 GOT_OBJ_LABEL_BLOB);
7030 case GOT_OBJ_TYPE_TREE:
7031 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7032 GOT_OBJ_LABEL_TREE);
7034 case GOT_OBJ_TYPE_COMMIT:
7035 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7036 GOT_OBJ_LABEL_COMMIT);
7038 case GOT_OBJ_TYPE_TAG:
7039 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7046 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7047 got_object_tag_get_name(tag));
7049 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7050 got_object_tag_get_tagger(tag),
7051 got_object_tag_get_tagger_time(tag));
7053 tagmsg = got_object_tag_get_message(tag);
7054 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7055 fprintf(outfile, "%s", tagmsg);
7058 got_object_tag_close(tag);
7062 static const struct got_error *
7063 cmd_cat(int argc, char *argv[])
7065 const struct got_error *error;
7066 struct got_repository *repo = NULL;
7067 struct got_worktree *worktree = NULL;
7068 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7069 const char *commit_id_str = NULL;
7070 struct got_object_id *id = NULL, *commit_id = NULL;
7071 int ch, obj_type, i, force_path = 0;
7074 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7079 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7082 commit_id_str = optarg;
7085 repo_path = realpath(optarg, NULL);
7086 if (repo_path == NULL)
7087 return got_error_from_errno2("realpath",
7089 got_path_strip_trailing_slashes(repo_path);
7103 cwd = getcwd(NULL, 0);
7105 error = got_error_from_errno("getcwd");
7108 error = got_worktree_open(&worktree, cwd);
7109 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7112 if (repo_path == NULL) {
7114 got_worktree_get_repo_path(worktree));
7115 if (repo_path == NULL) {
7116 error = got_error_from_errno("strdup");
7122 if (repo_path == NULL) {
7123 repo_path = getcwd(NULL, 0);
7124 if (repo_path == NULL)
7125 return got_error_from_errno("getcwd");
7128 error = got_repo_open(&repo, repo_path, NULL);
7133 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7137 if (commit_id_str == NULL)
7138 commit_id_str = GOT_REF_HEAD;
7139 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7143 for (i = 0; i < argc; i++) {
7145 error = got_object_id_by_path(&id, repo, commit_id,
7150 error = match_object_id(&id, &label, argv[i],
7151 GOT_OBJ_TYPE_ANY, 0, repo);
7153 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7154 error->code != GOT_ERR_NOT_REF)
7156 error = got_object_id_by_path(&id, repo,
7157 commit_id, argv[i]);
7163 error = got_object_get_type(&obj_type, repo, id);
7168 case GOT_OBJ_TYPE_BLOB:
7169 error = cat_blob(id, repo, stdout);
7171 case GOT_OBJ_TYPE_TREE:
7172 error = cat_tree(id, repo, stdout);
7174 case GOT_OBJ_TYPE_COMMIT:
7175 error = cat_commit(id, repo, stdout);
7177 case GOT_OBJ_TYPE_TAG:
7178 error = cat_tag(id, repo, stdout);
7181 error = got_error(GOT_ERR_OBJ_TYPE);
7197 got_worktree_close(worktree);
7199 const struct got_error *repo_error;
7200 repo_error = got_repo_close(repo);