2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
40 #include "got_version.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
49 #include "got_commit_graph.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_opentemp.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static volatile sig_atomic_t sigint_received;
59 static volatile sig_atomic_t sigpipe_received;
62 catch_sigint(int signo)
68 catch_sigpipe(int signo)
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
81 __dead static void usage(int);
82 __dead static void usage_init(void);
83 __dead static void usage_import(void);
84 __dead static void usage_checkout(void);
85 __dead static void usage_update(void);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_status(void);
91 __dead static void usage_ref(void);
92 __dead static void usage_branch(void);
93 __dead static void usage_tag(void);
94 __dead static void usage_add(void);
95 __dead static void usage_remove(void);
96 __dead static void usage_revert(void);
97 __dead static void usage_commit(void);
98 __dead static void usage_cherrypick(void);
99 __dead static void usage_backout(void);
100 __dead static void usage_rebase(void);
101 __dead static void usage_histedit(void);
102 __dead static void usage_integrate(void);
103 __dead static void usage_stage(void);
104 __dead static void usage_unstage(void);
105 __dead static void usage_cat(void);
107 static const struct got_error* cmd_init(int, char *[]);
108 static const struct got_error* cmd_import(int, char *[]);
109 static const struct got_error* cmd_checkout(int, char *[]);
110 static const struct got_error* cmd_update(int, char *[]);
111 static const struct got_error* cmd_log(int, char *[]);
112 static const struct got_error* cmd_diff(int, char *[]);
113 static const struct got_error* cmd_blame(int, char *[]);
114 static const struct got_error* cmd_tree(int, char *[]);
115 static const struct got_error* cmd_status(int, char *[]);
116 static const struct got_error* cmd_ref(int, char *[]);
117 static const struct got_error* cmd_branch(int, char *[]);
118 static const struct got_error* cmd_tag(int, char *[]);
119 static const struct got_error* cmd_add(int, char *[]);
120 static const struct got_error* cmd_remove(int, char *[]);
121 static const struct got_error* cmd_revert(int, char *[]);
122 static const struct got_error* cmd_commit(int, char *[]);
123 static const struct got_error* cmd_cherrypick(int, char *[]);
124 static const struct got_error* cmd_backout(int, char *[]);
125 static const struct got_error* cmd_rebase(int, char *[]);
126 static const struct got_error* cmd_histedit(int, char *[]);
127 static const struct got_error* cmd_integrate(int, char *[]);
128 static const struct got_error* cmd_stage(int, char *[]);
129 static const struct got_error* cmd_unstage(int, char *[]);
130 static const struct got_error* cmd_cat(int, char *[]);
132 static struct got_cmd got_commands[] = {
133 { "init", cmd_init, usage_init, "in" },
134 { "import", cmd_import, usage_import, "im" },
135 { "checkout", cmd_checkout, usage_checkout, "co" },
136 { "update", cmd_update, usage_update, "up" },
137 { "log", cmd_log, usage_log, "" },
138 { "diff", cmd_diff, usage_diff, "di" },
139 { "blame", cmd_blame, usage_blame, "bl" },
140 { "tree", cmd_tree, usage_tree, "tr" },
141 { "status", cmd_status, usage_status, "st" },
142 { "ref", cmd_ref, usage_ref, "" },
143 { "branch", cmd_branch, usage_branch, "br" },
144 { "tag", cmd_tag, usage_tag, "" },
145 { "add", cmd_add, usage_add, "" },
146 { "remove", cmd_remove, usage_remove, "rm" },
147 { "revert", cmd_revert, usage_revert, "rv" },
148 { "commit", cmd_commit, usage_commit, "ci" },
149 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 { "backout", cmd_backout, usage_backout, "bo" },
151 { "rebase", cmd_rebase, usage_rebase, "rb" },
152 { "histedit", cmd_histedit, usage_histedit, "he" },
153 { "integrate", cmd_integrate, usage_integrate,"ig" },
154 { "stage", cmd_stage, usage_stage, "sg" },
155 { "unstage", cmd_unstage, usage_unstage, "ug" },
156 { "cat", cmd_cat, usage_cat, "" },
164 fprintf(stderr, "commands:");
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct got_cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s", cmd->cmd_name);
173 main(int argc, char *argv[])
178 int hflag = 0, Vflag = 0;
180 setlocale(LC_CTYPE, "");
182 while ((ch = getopt(argc, argv, "hV")) != -1) {
201 got_version_print_str();
208 signal(SIGINT, catch_sigint);
209 signal(SIGPIPE, catch_sigpipe);
211 for (i = 0; i < nitems(got_commands); i++) {
212 const struct got_error *error;
214 cmd = &got_commands[i];
216 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 strcmp(cmd->cmd_alias, argv[0]) != 0)
221 got_commands[i].cmd_usage();
223 error = got_commands[i].cmd_main(argc, argv);
224 if (error && error->code != GOT_ERR_CANCELLED &&
225 error->code != GOT_ERR_PRIVSEP_EXIT &&
226 !(sigpipe_received &&
227 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
229 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
237 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
245 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
252 static const struct got_error *
253 get_editor(char **abspath)
255 const struct got_error *err = NULL;
260 editor = getenv("VISUAL");
262 editor = getenv("EDITOR");
265 err = got_path_find_prog(abspath, editor);
270 if (*abspath == NULL) {
271 *abspath = strdup("/bin/ed");
272 if (*abspath == NULL)
273 return got_error_from_errno("strdup");
279 static const struct got_error *
280 apply_unveil(const char *repo_path, int repo_read_only,
281 const char *worktree_path)
283 const struct got_error *err;
286 if (unveil("gmon.out", "rwc") != 0)
287 return got_error_from_errno2("unveil", "gmon.out");
289 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 return got_error_from_errno2("unveil", repo_path);
292 if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 return got_error_from_errno2("unveil", worktree_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
311 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
315 static const struct got_error *
316 cmd_init(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 char *repo_path = NULL;
322 while ((ch = getopt(argc, argv, "")) != -1) {
334 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 repo_path = strdup(argv[0]);
341 if (repo_path == NULL)
342 return got_error_from_errno("strdup");
344 got_path_strip_trailing_slashes(repo_path);
346 error = got_path_mkdir(repo_path);
348 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
351 error = apply_unveil(repo_path, 0, NULL);
355 error = got_repo_init(repo_path);
367 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 "[-r repository-path] [-I pattern] path\n", getprogname());
373 spawn_editor(const char *editor, const char *file)
376 sig_t sighup, sigint, sigquit;
379 sighup = signal(SIGHUP, SIG_IGN);
380 sigint = signal(SIGINT, SIG_IGN);
381 sigquit = signal(SIGQUIT, SIG_IGN);
383 switch (pid = fork()) {
387 execl(editor, editor, file, (char *)NULL);
391 while (waitpid(pid, &st, 0) == -1)
396 (void)signal(SIGHUP, sighup);
397 (void)signal(SIGINT, sigint);
398 (void)signal(SIGQUIT, sigquit);
400 if (!WIFEXITED(st)) {
405 return WEXITSTATUS(st);
408 static const struct got_error *
409 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 const char *initial_content)
412 const struct got_error *err = NULL;
416 int content_changed = 0;
421 if (stat(logmsg_path, &st) == -1)
422 return got_error_from_errno2("stat", logmsg_path);
424 if (spawn_editor(editor, logmsg_path) == -1)
425 return got_error_from_errno("failed spawning editor");
427 if (stat(logmsg_path, &st2) == -1)
428 return got_error_from_errno("stat");
430 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 "no changes made to commit message, aborting");
434 *logmsg = malloc(st2.st_size + 1);
436 return got_error_from_errno("malloc");
440 fp = fopen(logmsg_path, "r");
442 err = got_error_from_errno("fopen");
445 while (fgets(buf, sizeof(buf), fp) != NULL) {
446 if (!content_changed && strcmp(buf, initial_content) != 0)
448 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 continue; /* remove comments and leading empty lines */
450 len = strlcat(*logmsg, buf, st2.st_size);
454 while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 (*logmsg)[len - 1] = '\0';
459 if (len == 0 || !content_changed)
460 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 "commit message cannot be empty, aborting");
470 static const struct got_error *
471 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 const char *path_dir, const char *branch_name)
474 char *initial_content = NULL;
475 const struct got_error *err = NULL;
478 if (asprintf(&initial_content,
479 "\n# %s to be imported to branch %s\n", path_dir,
481 return got_error_from_errno("asprintf");
483 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
487 dprintf(fd, initial_content);
490 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
492 free(initial_content);
496 static const struct got_error *
497 import_progress(void *arg, const char *path)
499 printf("A %s\n", path);
503 static const struct got_error *
504 get_author(char **author, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 const char *got_author, *name, *email;
511 name = got_repo_get_gitconfig_author_name(repo);
512 email = got_repo_get_gitconfig_author_email(repo);
514 if (asprintf(author, "%s <%s>", name, email) == -1)
515 return got_error_from_errno("asprintf");
519 got_author = getenv("GOT_AUTHOR");
520 if (got_author == NULL) {
521 name = got_repo_get_global_gitconfig_author_name(repo);
522 email = got_repo_get_global_gitconfig_author_email(repo);
524 if (asprintf(author, "%s <%s>", name, email) == -1)
525 return got_error_from_errno("asprintf");
528 /* TODO: Look up user in password database? */
529 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 *author = strdup(got_author);
534 return got_error_from_errno("strdup");
537 * Really dumb email address check; we're only doing this to
538 * avoid git's object parser breaking on commits we create.
540 while (*got_author && *got_author != '<')
542 if (*got_author != '<') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 while (*got_author && *got_author != '@')
548 if (*got_author != '@') {
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 while (*got_author && *got_author != '>')
554 if (*got_author != '>')
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 static const struct got_error *
565 get_gitconfig_path(char **gitconfig_path)
567 const char *homedir = getenv("HOME");
569 *gitconfig_path = NULL;
571 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 return got_error_from_errno("asprintf");
578 static const struct got_error *
579 cmd_import(int argc, char *argv[])
581 const struct got_error *error = NULL;
582 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 const char *branch_name = "main";
585 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 struct got_repository *repo = NULL;
587 struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 struct got_object_id *new_commit_id = NULL;
590 struct got_pathlist_head ignores;
591 struct got_pathlist_entry *pe;
592 int preserve_logmsg = 0;
594 TAILQ_INIT(&ignores);
596 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 branch_name = optarg;
602 logmsg = strdup(optarg);
603 if (logmsg == NULL) {
604 error = got_error_from_errno("strdup");
609 repo_path = realpath(optarg, NULL);
610 if (repo_path == NULL) {
611 error = got_error_from_errno2("realpath",
617 if (optarg[0] == '\0')
619 error = got_pathlist_insert(&pe, &ignores, optarg,
634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
642 if (repo_path == NULL) {
643 repo_path = getcwd(NULL, 0);
644 if (repo_path == NULL)
645 return got_error_from_errno("getcwd");
647 got_path_strip_trailing_slashes(repo_path);
648 error = get_gitconfig_path(&gitconfig_path);
651 error = got_repo_open(&repo, repo_path, gitconfig_path);
655 error = get_author(&author, repo);
660 * Don't let the user create a branch name with a leading '-'.
661 * While technically a valid reference name, this case is usually
662 * an unintended typo.
664 if (branch_name[0] == '-')
665 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
667 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 error = got_error_from_errno("asprintf");
672 error = got_ref_open(&branch_ref, repo, refname, 0);
674 if (error->code != GOT_ERR_NOT_REF)
677 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 "import target branch already exists");
682 path_dir = realpath(argv[0], NULL);
683 if (path_dir == NULL) {
684 error = got_error_from_errno2("realpath", argv[0]);
687 got_path_strip_trailing_slashes(path_dir);
690 * unveil(2) traverses exec(2); if an editor is used we have
691 * to apply unveil after the log message has been written.
693 if (logmsg == NULL || strlen(logmsg) == 0) {
694 error = get_editor(&editor);
698 error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
708 if (unveil(path_dir, "r") != 0) {
709 error = got_error_from_errno2("unveil", path_dir);
715 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
722 error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 author, &ignores, repo, import_progress, NULL);
730 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
737 error = got_ref_write(branch_ref, repo);
744 error = got_object_id_str(&id_str, new_commit_id);
751 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
753 if (error->code != GOT_ERR_NOT_REF) {
759 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
767 error = got_ref_write(head_ref, repo);
775 printf("Created branch %s with commit %s\n",
776 got_ref_get_name(branch_ref), id_str);
778 if (preserve_logmsg) {
779 fprintf(stderr, "%s: log message preserved in %s\n",
780 getprogname(), logmsg_path);
781 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 error = got_error_from_errno2("unlink", logmsg_path);
791 free(gitconfig_path);
793 got_ref_close(branch_ref);
795 got_ref_close(head_ref);
802 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 "[-p prefix] repository-path [worktree-path]\n", getprogname());
808 show_worktree_base_ref_warning(void)
810 fprintf(stderr, "%s: warning: could not create a reference "
811 "to the work tree's base commit; the commit could be "
812 "garbage-collected by Git; making the repository "
813 "writable and running 'got update' will prevent this\n",
817 struct got_checkout_progress_arg {
818 const char *worktree_path;
819 int had_base_commit_ref_error;
822 static const struct got_error *
823 checkout_progress(void *arg, unsigned char status, const char *path)
825 struct got_checkout_progress_arg *a = arg;
827 /* Base commit bump happens silently. */
828 if (status == GOT_STATUS_BUMP_BASE)
831 if (status == GOT_STATUS_BASE_REF_ERR) {
832 a->had_base_commit_ref_error = 1;
836 while (path[0] == '/')
839 printf("%c %s/%s\n", status, a->worktree_path, path);
843 static const struct got_error *
844 check_cancelled(void *arg)
846 if (sigint_received || sigpipe_received)
847 return got_error(GOT_ERR_CANCELLED);
851 static const struct got_error *
852 check_linear_ancestry(struct got_object_id *commit_id,
853 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 struct got_repository *repo)
856 const struct got_error *err = NULL;
857 struct got_object_id *yca_id;
859 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 commit_id, base_commit_id, repo, check_cancelled, NULL);
865 return got_error(GOT_ERR_ANCESTRY);
868 * Require a straight line of history between the target commit
869 * and the work tree's base commit.
871 * Non-linear situations such as this require a rebase:
873 * (commit) D F (base_commit)
881 * 'got update' only handles linear cases:
882 * Update forwards in time: A (base/yca) - B - C - D (commit)
883 * Update backwards in time: D (base) - C - B - A (commit/yca)
885 if (allow_forwards_in_time_only) {
886 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 return got_error(GOT_ERR_ANCESTRY);
888 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
896 static const struct got_error *
897 check_same_branch(struct got_object_id *commit_id,
898 struct got_reference *head_ref, struct got_object_id *yca_id,
899 struct got_repository *repo)
901 const struct got_error *err = NULL;
902 struct got_commit_graph *graph = NULL;
903 struct got_object_id *head_commit_id = NULL;
904 int is_same_branch = 0;
906 err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
919 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
923 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 check_cancelled, NULL);
929 struct got_object_id *id;
930 err = got_commit_graph_iter_next(&id, graph, repo,
931 check_cancelled, NULL);
933 if (err->code == GOT_ERR_ITER_COMPLETED)
939 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
941 if (got_object_id_cmp(id, commit_id) == 0) {
949 got_commit_graph_close(graph);
950 free(head_commit_id);
951 if (!err && !is_same_branch)
952 err = got_error(GOT_ERR_ANCESTRY);
956 static const struct got_error *
957 resolve_commit_arg(struct got_object_id **commit_id,
958 const char *commit_id_arg, struct got_repository *repo)
960 const struct got_error *err;
961 struct got_reference *ref;
962 struct got_tag_object *tag;
964 err = got_repo_object_match_tag(&tag, commit_id_arg,
965 GOT_OBJ_TYPE_COMMIT, repo);
967 *commit_id = got_object_id_dup(
968 got_object_tag_get_object_id(tag));
969 if (*commit_id == NULL)
970 err = got_error_from_errno("got_object_id_dup");
971 got_object_tag_close(tag);
973 } else if (err->code != GOT_ERR_NO_OBJ)
976 err = got_ref_open(&ref, repo, commit_id_arg, 0);
978 err = got_ref_resolve(commit_id, repo, ref);
981 if (err->code != GOT_ERR_NOT_REF)
983 err = got_repo_match_object_id_prefix(commit_id,
984 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
989 static const struct got_error *
990 cmd_checkout(int argc, char *argv[])
992 const struct got_error *error = NULL;
993 struct got_repository *repo = NULL;
994 struct got_reference *head_ref = NULL;
995 struct got_worktree *worktree = NULL;
996 char *repo_path = NULL;
997 char *worktree_path = NULL;
998 const char *path_prefix = "";
999 const char *branch_name = GOT_REF_HEAD;
1000 char *commit_id_str = NULL;
1001 int ch, same_path_prefix;
1002 struct got_pathlist_head paths;
1003 struct got_checkout_progress_arg cpa;
1007 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1010 branch_name = optarg;
1013 commit_id_str = strdup(optarg);
1014 if (commit_id_str == NULL)
1015 return got_error_from_errno("strdup");
1018 path_prefix = optarg;
1030 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1031 "unveil", NULL) == -1)
1035 char *cwd, *base, *dotgit;
1036 repo_path = realpath(argv[0], NULL);
1037 if (repo_path == NULL)
1038 return got_error_from_errno2("realpath", argv[0]);
1039 cwd = getcwd(NULL, 0);
1041 error = got_error_from_errno("getcwd");
1044 if (path_prefix[0]) {
1045 base = basename(path_prefix);
1047 error = got_error_from_errno2("basename",
1052 base = basename(repo_path);
1054 error = got_error_from_errno2("basename",
1059 dotgit = strstr(base, ".git");
1062 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1063 error = got_error_from_errno("asprintf");
1068 } else if (argc == 2) {
1069 repo_path = realpath(argv[0], NULL);
1070 if (repo_path == NULL) {
1071 error = got_error_from_errno2("realpath", argv[0]);
1074 worktree_path = realpath(argv[1], NULL);
1075 if (worktree_path == NULL) {
1076 if (errno != ENOENT) {
1077 error = got_error_from_errno2("realpath",
1081 worktree_path = strdup(argv[1]);
1082 if (worktree_path == NULL) {
1083 error = got_error_from_errno("strdup");
1090 got_path_strip_trailing_slashes(repo_path);
1091 got_path_strip_trailing_slashes(worktree_path);
1093 error = got_repo_open(&repo, repo_path, NULL);
1097 /* Pre-create work tree path for unveil(2) */
1098 error = got_path_mkdir(worktree_path);
1100 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1101 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1103 if (!got_path_dir_is_empty(worktree_path)) {
1104 error = got_error_path(worktree_path,
1105 GOT_ERR_DIR_NOT_EMPTY);
1110 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1114 error = got_ref_open(&head_ref, repo, branch_name, 0);
1118 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1119 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1122 error = got_worktree_open(&worktree, worktree_path);
1126 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1130 if (!same_path_prefix) {
1131 error = got_error(GOT_ERR_PATH_PREFIX);
1135 if (commit_id_str) {
1136 struct got_object_id *commit_id;
1137 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1140 error = check_linear_ancestry(commit_id,
1141 got_worktree_get_base_commit_id(worktree), 0, repo);
1142 if (error != NULL) {
1146 error = check_same_branch(commit_id, head_ref, NULL, repo);
1149 error = got_worktree_set_base_commit_id(worktree, repo,
1156 error = got_pathlist_append(&paths, "", NULL);
1159 cpa.worktree_path = worktree_path;
1160 cpa.had_base_commit_ref_error = 0;
1161 error = got_worktree_checkout_files(worktree, &paths, repo,
1162 checkout_progress, &cpa, check_cancelled, NULL);
1166 printf("Now shut up and hack\n");
1167 if (cpa.had_base_commit_ref_error)
1168 show_worktree_base_ref_warning();
1171 got_pathlist_free(&paths);
1172 free(commit_id_str);
1174 free(worktree_path);
1181 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1186 static const struct got_error *
1187 update_progress(void *arg, unsigned char status, const char *path)
1189 int *did_something = arg;
1191 if (status == GOT_STATUS_EXISTS ||
1192 status == GOT_STATUS_BASE_REF_ERR)
1197 /* Base commit bump happens silently. */
1198 if (status == GOT_STATUS_BUMP_BASE)
1201 while (path[0] == '/')
1203 printf("%c %s\n", status, path);
1207 static const struct got_error *
1208 switch_head_ref(struct got_reference *head_ref,
1209 struct got_object_id *commit_id, struct got_worktree *worktree,
1210 struct got_repository *repo)
1212 const struct got_error *err = NULL;
1214 int ref_has_moved = 0;
1216 /* Trivial case: switching between two different references. */
1217 if (strcmp(got_ref_get_name(head_ref),
1218 got_worktree_get_head_ref_name(worktree)) != 0) {
1219 printf("Switching work tree from %s to %s\n",
1220 got_worktree_get_head_ref_name(worktree),
1221 got_ref_get_name(head_ref));
1222 return got_worktree_set_head_ref(worktree, head_ref);
1225 err = check_linear_ancestry(commit_id,
1226 got_worktree_get_base_commit_id(worktree), 0, repo);
1228 if (err->code != GOT_ERR_ANCESTRY)
1235 /* Switching to a rebased branch with the same reference name. */
1236 err = got_object_id_str(&base_id_str,
1237 got_worktree_get_base_commit_id(worktree));
1240 printf("Reference %s now points at a different branch\n",
1241 got_worktree_get_head_ref_name(worktree));
1242 printf("Switching work tree from %s to %s\n", base_id_str,
1243 got_worktree_get_head_ref_name(worktree));
1247 static const struct got_error *
1248 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1250 const struct got_error *err;
1253 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1257 return got_error(GOT_ERR_REBASING);
1259 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1263 return got_error(GOT_ERR_HISTEDIT_BUSY);
1268 static const struct got_error *
1269 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1270 char *argv[], struct got_worktree *worktree)
1272 const struct got_error *err = NULL;
1279 return got_error_from_errno("strdup");
1280 return got_pathlist_append(paths, path, NULL);
1283 for (i = 0; i < argc; i++) {
1284 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1287 err = got_pathlist_append(paths, path, NULL);
1297 static const struct got_error *
1298 cmd_update(int argc, char *argv[])
1300 const struct got_error *error = NULL;
1301 struct got_repository *repo = NULL;
1302 struct got_worktree *worktree = NULL;
1303 char *worktree_path = NULL;
1304 struct got_object_id *commit_id = NULL;
1305 char *commit_id_str = NULL;
1306 const char *branch_name = NULL;
1307 struct got_reference *head_ref = NULL;
1308 struct got_pathlist_head paths;
1309 struct got_pathlist_entry *pe;
1310 int ch, did_something = 0;
1314 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1317 branch_name = optarg;
1320 commit_id_str = strdup(optarg);
1321 if (commit_id_str == NULL)
1322 return got_error_from_errno("strdup");
1334 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1335 "unveil", NULL) == -1)
1338 worktree_path = getcwd(NULL, 0);
1339 if (worktree_path == NULL) {
1340 error = got_error_from_errno("getcwd");
1343 error = got_worktree_open(&worktree, worktree_path);
1347 error = check_rebase_or_histedit_in_progress(worktree);
1351 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1356 error = apply_unveil(got_repo_get_path(repo), 0,
1357 got_worktree_get_root_path(worktree));
1361 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1365 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1366 got_worktree_get_head_ref_name(worktree), 0);
1369 if (commit_id_str == NULL) {
1370 error = got_ref_resolve(&commit_id, repo, head_ref);
1373 error = got_object_id_str(&commit_id_str, commit_id);
1377 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1378 free(commit_id_str);
1379 commit_id_str = NULL;
1382 error = got_object_id_str(&commit_id_str, commit_id);
1388 struct got_object_id *head_commit_id;
1389 TAILQ_FOREACH(pe, &paths, entry) {
1390 if (pe->path_len == 0)
1392 error = got_error_msg(GOT_ERR_BAD_PATH,
1393 "switching between branches requires that "
1394 "the entire work tree gets updated");
1397 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1400 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1402 free(head_commit_id);
1405 error = check_same_branch(commit_id, head_ref, NULL, repo);
1408 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1412 error = check_linear_ancestry(commit_id,
1413 got_worktree_get_base_commit_id(worktree), 0, repo);
1414 if (error != NULL) {
1415 if (error->code == GOT_ERR_ANCESTRY)
1416 error = got_error(GOT_ERR_BRANCH_MOVED);
1419 error = check_same_branch(commit_id, head_ref, NULL, repo);
1424 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1426 error = got_worktree_set_base_commit_id(worktree, repo,
1432 error = got_worktree_checkout_files(worktree, &paths, repo,
1433 update_progress, &did_something, check_cancelled, NULL);
1438 printf("Updated to commit %s\n", commit_id_str);
1440 printf("Already up-to-date\n");
1442 free(worktree_path);
1443 TAILQ_FOREACH(pe, &paths, entry)
1444 free((char *)pe->path);
1445 got_pathlist_free(&paths);
1447 free(commit_id_str);
1451 static const struct got_error *
1452 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1453 const char *path, int diff_context, int ignore_whitespace,
1454 struct got_repository *repo)
1456 const struct got_error *err = NULL;
1457 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1460 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1465 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1469 while (path[0] == '/')
1471 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1472 ignore_whitespace, stdout);
1475 got_object_blob_close(blob1);
1476 got_object_blob_close(blob2);
1480 static const struct got_error *
1481 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1482 const char *path, int diff_context, int ignore_whitespace,
1483 struct got_repository *repo)
1485 const struct got_error *err = NULL;
1486 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1487 struct got_diff_blob_output_unidiff_arg arg;
1490 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1495 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1499 arg.diff_context = diff_context;
1500 arg.ignore_whitespace = ignore_whitespace;
1501 arg.outfile = stdout;
1502 while (path[0] == '/')
1504 err = got_diff_tree(tree1, tree2, path, path, repo,
1505 got_diff_blob_output_unidiff, &arg, 1);
1508 got_object_tree_close(tree1);
1510 got_object_tree_close(tree2);
1514 static const struct got_error *
1515 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1516 const char *path, int diff_context, struct got_repository *repo)
1518 const struct got_error *err = NULL;
1519 struct got_commit_object *pcommit = NULL;
1520 char *id_str1 = NULL, *id_str2 = NULL;
1521 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1522 struct got_object_qid *qid;
1524 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1526 err = got_object_open_as_commit(&pcommit, repo,
1532 if (path && path[0] != '\0') {
1534 err = got_object_id_by_path(&obj_id2, repo, id, path);
1537 err = got_object_id_str(&id_str2, obj_id2);
1543 err = got_object_id_by_path(&obj_id1, repo,
1549 err = got_object_id_str(&id_str1, obj_id1);
1555 err = got_object_get_type(&obj_type, repo, obj_id2);
1560 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1562 case GOT_OBJ_TYPE_BLOB:
1563 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1566 case GOT_OBJ_TYPE_TREE:
1567 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1571 err = got_error(GOT_ERR_OBJ_TYPE);
1577 obj_id2 = got_object_commit_get_tree_id(commit);
1578 err = got_object_id_str(&id_str2, obj_id2);
1581 obj_id1 = got_object_commit_get_tree_id(pcommit);
1582 err = got_object_id_str(&id_str1, obj_id1);
1585 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1586 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1593 got_object_commit_close(pcommit);
1598 get_datestr(time_t *time, char *datebuf)
1600 struct tm mytm, *tm;
1603 tm = gmtime_r(time, &mytm);
1606 s = asctime_r(tm, datebuf);
1609 p = strchr(s, '\n');
1615 static const struct got_error *
1616 match_logmsg(int *have_match, struct got_object_id *id,
1617 struct got_commit_object *commit, regex_t *regex)
1619 const struct got_error *err = NULL;
1620 regmatch_t regmatch;
1621 char *id_str = NULL, *logmsg = NULL;
1625 err = got_object_id_str(&id_str, id);
1629 err = got_object_commit_get_logmsg(&logmsg, commit);
1633 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1641 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1643 static const struct got_error *
1644 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1645 struct got_repository *repo, const char *path, int show_patch,
1646 int diff_context, struct got_reflist_head *refs)
1648 const struct got_error *err = NULL;
1649 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1651 time_t committer_time;
1652 const char *author, *committer;
1653 char *refs_str = NULL;
1654 struct got_reflist_entry *re;
1656 SIMPLEQ_FOREACH(re, refs, entry) {
1659 struct got_tag_object *tag = NULL;
1662 name = got_ref_get_name(re->ref);
1663 if (strcmp(name, GOT_REF_HEAD) == 0)
1665 if (strncmp(name, "refs/", 5) == 0)
1667 if (strncmp(name, "got/", 4) == 0)
1669 if (strncmp(name, "heads/", 6) == 0)
1671 if (strncmp(name, "remotes/", 8) == 0)
1673 if (strncmp(name, "tags/", 5) == 0) {
1674 err = got_object_open_as_tag(&tag, repo, re->id);
1676 if (err->code != GOT_ERR_OBJ_TYPE)
1678 /* Ref points at something other than a tag. */
1683 cmp = got_object_id_cmp(tag ?
1684 got_object_tag_get_object_id(tag) : re->id, id);
1686 got_object_tag_close(tag);
1690 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1692 err = got_error_from_errno("asprintf");
1698 err = got_object_id_str(&id_str, id);
1702 printf(GOT_COMMIT_SEP_STR);
1703 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1704 refs_str ? refs_str : "", refs_str ? ")" : "");
1709 printf("from: %s\n", got_object_commit_get_author(commit));
1710 committer_time = got_object_commit_get_committer_time(commit);
1711 datestr = get_datestr(&committer_time, datebuf);
1713 printf("date: %s UTC\n", datestr);
1714 author = got_object_commit_get_author(commit);
1715 committer = got_object_commit_get_committer(commit);
1716 if (strcmp(author, committer) != 0)
1717 printf("via: %s\n", committer);
1718 if (got_object_commit_get_nparents(commit) > 1) {
1719 const struct got_object_id_queue *parent_ids;
1720 struct got_object_qid *qid;
1722 parent_ids = got_object_commit_get_parent_ids(commit);
1723 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1724 err = got_object_id_str(&id_str, qid->id);
1727 printf("parent %d: %s\n", n++, id_str);
1732 err = got_object_commit_get_logmsg(&logmsg0, commit);
1738 line = strsep(&logmsg, "\n");
1740 printf(" %s\n", line);
1745 err = print_patch(commit, id, path, diff_context, repo);
1750 if (fflush(stdout) != 0 && err == NULL)
1751 err = got_error_from_errno("fflush");
1755 static const struct got_error *
1756 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1757 const char *path, int show_patch, const char *search_pattern,
1758 int diff_context, int limit, int first_parent_traversal,
1759 struct got_reflist_head *refs)
1761 const struct got_error *err;
1762 struct got_commit_graph *graph;
1766 if (search_pattern &&
1767 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1768 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1770 err = got_commit_graph_open(&graph, root_id, path,
1771 first_parent_traversal, repo);
1774 err = got_commit_graph_iter_start(graph, root_id, repo,
1775 check_cancelled, NULL);
1779 struct got_commit_object *commit;
1780 struct got_object_id *id;
1782 if (sigint_received || sigpipe_received)
1785 err = got_commit_graph_iter_next(&id, graph, repo,
1786 check_cancelled, NULL);
1788 if (err->code == GOT_ERR_ITER_COMPLETED)
1795 err = got_object_open_as_commit(&commit, repo, id);
1799 if (search_pattern) {
1800 err = match_logmsg(&have_match, id, commit, ®ex);
1802 got_object_commit_close(commit);
1805 if (have_match == 0) {
1806 got_object_commit_close(commit);
1811 err = print_commit(commit, id, repo, path, show_patch,
1812 diff_context, refs);
1813 got_object_commit_close(commit);
1814 if (err || (limit && --limit == 0))
1820 got_commit_graph_close(graph);
1827 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1828 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1833 get_default_log_limit(void)
1835 const char *got_default_log_limit;
1839 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1840 if (got_default_log_limit == NULL)
1842 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1848 static const struct got_error *
1849 cmd_log(int argc, char *argv[])
1851 const struct got_error *error;
1852 struct got_repository *repo = NULL;
1853 struct got_worktree *worktree = NULL;
1854 struct got_commit_object *commit = NULL;
1855 struct got_object_id *id = NULL;
1856 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1857 const char *start_commit = NULL, *search_pattern = NULL;
1858 int diff_context = -1, ch;
1859 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1861 struct got_reflist_head refs;
1863 SIMPLEQ_INIT(&refs);
1866 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1872 limit = get_default_log_limit();
1874 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1880 start_commit = optarg;
1883 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1886 err(1, "-C option %s", errstr);
1889 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1891 err(1, "-l option %s", errstr);
1894 first_parent_traversal = 1;
1897 repo_path = realpath(optarg, NULL);
1898 if (repo_path == NULL)
1899 return got_error_from_errno2("realpath",
1901 got_path_strip_trailing_slashes(repo_path);
1904 search_pattern = optarg;
1915 if (diff_context == -1)
1917 else if (!show_patch)
1918 errx(1, "-C reguires -p");
1920 cwd = getcwd(NULL, 0);
1922 error = got_error_from_errno("getcwd");
1926 error = got_worktree_open(&worktree, cwd);
1927 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1934 error = got_error_from_errno("strdup");
1937 } else if (argc == 1) {
1939 error = got_worktree_resolve_path(&path, worktree,
1944 path = strdup(argv[0]);
1946 error = got_error_from_errno("strdup");
1953 if (repo_path == NULL) {
1954 repo_path = worktree ?
1955 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1957 if (repo_path == NULL) {
1958 error = got_error_from_errno("strdup");
1962 error = got_repo_open(&repo, repo_path, NULL);
1966 error = apply_unveil(got_repo_get_path(repo), 1,
1967 worktree ? got_worktree_get_root_path(worktree) : NULL);
1971 if (start_commit == NULL) {
1972 struct got_reference *head_ref;
1973 error = got_ref_open(&head_ref, repo,
1974 worktree ? got_worktree_get_head_ref_name(worktree)
1978 error = got_ref_resolve(&id, repo, head_ref);
1979 got_ref_close(head_ref);
1982 error = got_object_open_as_commit(&commit, repo, id);
1984 struct got_reference *ref;
1985 error = got_ref_open(&ref, repo, start_commit, 0);
1986 if (error == NULL) {
1988 error = got_ref_resolve(&id, repo, ref);
1992 error = got_object_get_type(&obj_type, repo, id);
1995 if (obj_type == GOT_OBJ_TYPE_TAG) {
1996 struct got_tag_object *tag;
1997 error = got_object_open_as_tag(&tag, repo, id);
2000 if (got_object_tag_get_object_type(tag) !=
2001 GOT_OBJ_TYPE_COMMIT) {
2002 got_object_tag_close(tag);
2003 error = got_error(GOT_ERR_OBJ_TYPE);
2007 id = got_object_id_dup(
2008 got_object_tag_get_object_id(tag));
2010 error = got_error_from_errno(
2011 "got_object_id_dup");
2012 got_object_tag_close(tag);
2015 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2016 error = got_error(GOT_ERR_OBJ_TYPE);
2019 error = got_object_open_as_commit(&commit, repo, id);
2023 if (commit == NULL) {
2024 error = got_repo_match_object_id_prefix(&id,
2025 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2034 const char *prefix = got_worktree_get_path_prefix(worktree);
2036 if (asprintf(&p, "%s%s%s", prefix,
2037 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2038 error = got_error_from_errno("asprintf");
2041 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2044 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2049 path = in_repo_path;
2052 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2056 error = print_commits(id, repo, path, show_patch, search_pattern,
2057 diff_context, limit, first_parent_traversal, &refs);
2064 got_worktree_close(worktree);
2066 const struct got_error *repo_error;
2067 repo_error = got_repo_close(repo);
2071 got_ref_list_free(&refs);
2078 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2079 "[-w] [object1 object2 | path]\n", getprogname());
2083 struct print_diff_arg {
2084 struct got_repository *repo;
2085 struct got_worktree *worktree;
2090 int ignore_whitespace;
2093 static const struct got_error *
2094 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2095 const char *path, struct got_object_id *blob_id,
2096 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2097 int dirfd, const char *de_name)
2099 struct print_diff_arg *a = arg;
2100 const struct got_error *err = NULL;
2101 struct got_blob_object *blob1 = NULL;
2104 char *abspath = NULL, *label1 = NULL;
2107 if (a->diff_staged) {
2108 if (staged_status != GOT_STATUS_MODIFY &&
2109 staged_status != GOT_STATUS_ADD &&
2110 staged_status != GOT_STATUS_DELETE)
2113 if (staged_status == GOT_STATUS_DELETE)
2115 if (status == GOT_STATUS_NONEXISTENT)
2116 return got_error_set_errno(ENOENT, path);
2117 if (status != GOT_STATUS_MODIFY &&
2118 status != GOT_STATUS_ADD &&
2119 status != GOT_STATUS_DELETE &&
2120 status != GOT_STATUS_CONFLICT)
2124 if (!a->header_shown) {
2125 printf("diff %s %s%s\n", a->id_str,
2126 got_worktree_get_root_path(a->worktree),
2127 a->diff_staged ? " (staged changes)" : "");
2128 a->header_shown = 1;
2131 if (a->diff_staged) {
2132 const char *label1 = NULL, *label2 = NULL;
2133 switch (staged_status) {
2134 case GOT_STATUS_MODIFY:
2138 case GOT_STATUS_ADD:
2141 case GOT_STATUS_DELETE:
2145 return got_error(GOT_ERR_FILE_STATUS);
2147 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2148 label1, label2, a->diff_context, a->ignore_whitespace,
2152 if (staged_status == GOT_STATUS_ADD ||
2153 staged_status == GOT_STATUS_MODIFY) {
2155 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2159 err = got_object_id_str(&id_str, staged_blob_id);
2162 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2163 err = got_error_from_errno("asprintf");
2168 } else if (status != GOT_STATUS_ADD) {
2169 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2174 if (status != GOT_STATUS_DELETE) {
2175 if (asprintf(&abspath, "%s/%s",
2176 got_worktree_get_root_path(a->worktree), path) == -1) {
2177 err = got_error_from_errno("asprintf");
2182 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2184 err = got_error_from_errno2("openat", abspath);
2188 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2190 err = got_error_from_errno2("open", abspath);
2194 if (fstat(fd, &sb) == -1) {
2195 err = got_error_from_errno2("fstat", abspath);
2198 f2 = fdopen(fd, "r");
2200 err = got_error_from_errno2("fdopen", abspath);
2207 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2208 a->diff_context, a->ignore_whitespace, stdout);
2211 got_object_blob_close(blob1);
2212 if (f2 && fclose(f2) == EOF && err == NULL)
2213 err = got_error_from_errno("fclose");
2214 if (fd != -1 && close(fd) == -1 && err == NULL)
2215 err = got_error_from_errno("close");
2220 static const struct got_error *
2221 match_object_id(struct got_object_id **id, char **label,
2222 const char *id_str, int obj_type, int resolve_tags,
2223 struct got_repository *repo)
2225 const struct got_error *err;
2226 struct got_tag_object *tag;
2227 struct got_reference *ref = NULL;
2233 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2236 *id = got_object_id_dup(
2237 got_object_tag_get_object_id(tag));
2239 err = got_error_from_errno("got_object_id_dup");
2240 else if (asprintf(label, "refs/tags/%s",
2241 got_object_tag_get_name(tag)) == -1) {
2242 err = got_error_from_errno("asprintf");
2246 got_object_tag_close(tag);
2248 } else if (err->code != GOT_ERR_NO_OBJ)
2252 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2254 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2256 err = got_ref_open(&ref, repo, id_str, 0);
2259 *label = strdup(got_ref_get_name(ref));
2260 if (*label == NULL) {
2261 err = got_error_from_errno("strdup");
2264 err = got_ref_resolve(id, repo, ref);
2266 err = got_object_id_str(label, *id);
2267 if (*label == NULL) {
2268 err = got_error_from_errno("strdup");
2279 static const struct got_error *
2280 cmd_diff(int argc, char *argv[])
2282 const struct got_error *error;
2283 struct got_repository *repo = NULL;
2284 struct got_worktree *worktree = NULL;
2285 char *cwd = NULL, *repo_path = NULL;
2286 struct got_object_id *id1 = NULL, *id2 = NULL;
2287 const char *id_str1 = NULL, *id_str2 = NULL;
2288 char *label1 = NULL, *label2 = NULL;
2290 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2295 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2300 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2303 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2306 err(1, "-C option %s", errstr);
2309 repo_path = realpath(optarg, NULL);
2310 if (repo_path == NULL)
2311 return got_error_from_errno2("realpath",
2313 got_path_strip_trailing_slashes(repo_path);
2319 ignore_whitespace = 1;
2330 cwd = getcwd(NULL, 0);
2332 error = got_error_from_errno("getcwd");
2335 error = got_worktree_open(&worktree, cwd);
2336 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2339 if (worktree == NULL) {
2340 error = got_error(GOT_ERR_NOT_WORKTREE);
2345 "-r option can't be used when diffing a work tree");
2346 repo_path = strdup(got_worktree_get_repo_path(worktree));
2347 if (repo_path == NULL) {
2348 error = got_error_from_errno("strdup");
2352 error = got_worktree_resolve_path(&path, worktree,
2359 error = got_error_from_errno("strdup");
2363 } else if (argc == 2) {
2365 errx(1, "-s option can't be used when diffing "
2366 "objects in repository");
2369 if (worktree && repo_path == NULL) {
2371 strdup(got_worktree_get_repo_path(worktree));
2372 if (repo_path == NULL) {
2373 error = got_error_from_errno("strdup");
2380 if (repo_path == NULL) {
2381 repo_path = getcwd(NULL, 0);
2382 if (repo_path == NULL)
2383 return got_error_from_errno("getcwd");
2386 error = got_repo_open(&repo, repo_path, NULL);
2391 error = apply_unveil(got_repo_get_path(repo), 1,
2392 worktree ? got_worktree_get_root_path(worktree) : NULL);
2397 struct print_diff_arg arg;
2398 struct got_pathlist_head paths;
2403 error = got_object_id_str(&id_str,
2404 got_worktree_get_base_commit_id(worktree));
2408 arg.worktree = worktree;
2409 arg.diff_context = diff_context;
2410 arg.id_str = id_str;
2411 arg.header_shown = 0;
2412 arg.diff_staged = diff_staged;
2413 arg.ignore_whitespace = ignore_whitespace;
2415 error = got_pathlist_append(&paths, path, NULL);
2419 error = got_worktree_status(worktree, &paths, repo, print_diff,
2420 &arg, check_cancelled, NULL);
2422 got_pathlist_free(&paths);
2426 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2431 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2436 error = got_object_get_type(&type1, repo, id1);
2440 error = got_object_get_type(&type2, repo, id2);
2444 if (type1 != type2) {
2445 error = got_error(GOT_ERR_OBJ_TYPE);
2450 case GOT_OBJ_TYPE_BLOB:
2451 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2452 diff_context, ignore_whitespace, repo, stdout);
2454 case GOT_OBJ_TYPE_TREE:
2455 error = got_diff_objects_as_trees(id1, id2, "", "",
2456 diff_context, ignore_whitespace, repo, stdout);
2458 case GOT_OBJ_TYPE_COMMIT:
2459 printf("diff %s %s\n", label1, label2);
2460 error = got_diff_objects_as_commits(id1, id2, diff_context,
2461 ignore_whitespace, repo, stdout);
2464 error = got_error(GOT_ERR_OBJ_TYPE);
2474 got_worktree_close(worktree);
2476 const struct got_error *repo_error;
2477 repo_error = got_repo_close(repo);
2488 "usage: %s blame [-c commit] [-r repository-path] path\n",
2497 char datebuf[11]; /* YYYY-MM-DD + NUL */
2500 struct blame_cb_args {
2501 struct blame_line *lines;
2505 off_t *line_offsets;
2507 struct got_repository *repo;
2510 static const struct got_error *
2511 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2513 const struct got_error *err = NULL;
2514 struct blame_cb_args *a = arg;
2515 struct blame_line *bline;
2517 size_t linesize = 0;
2518 struct got_commit_object *commit = NULL;
2521 time_t committer_time;
2523 if (nlines != a->nlines ||
2524 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2525 return got_error(GOT_ERR_RANGE);
2527 if (sigint_received)
2528 return got_error(GOT_ERR_ITER_COMPLETED);
2531 return NULL; /* no change in this commit */
2533 /* Annotate this line. */
2534 bline = &a->lines[lineno - 1];
2535 if (bline->annotated)
2537 err = got_object_id_str(&bline->id_str, id);
2541 err = got_object_open_as_commit(&commit, a->repo, id);
2545 bline->committer = strdup(got_object_commit_get_committer(commit));
2546 if (bline->committer == NULL) {
2547 err = got_error_from_errno("strdup");
2551 committer_time = got_object_commit_get_committer_time(commit);
2552 if (localtime_r(&committer_time, &tm) == NULL)
2553 return got_error_from_errno("localtime_r");
2554 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2555 &tm) >= sizeof(bline->datebuf)) {
2556 err = got_error(GOT_ERR_NO_SPACE);
2559 bline->annotated = 1;
2561 /* Print lines annotated so far. */
2562 bline = &a->lines[a->lineno_cur - 1];
2563 if (!bline->annotated)
2566 offset = a->line_offsets[a->lineno_cur - 1];
2567 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2568 err = got_error_from_errno("fseeko");
2572 while (bline->annotated) {
2573 char *smallerthan, *at, *nl, *committer;
2576 if (getline(&line, &linesize, a->f) == -1) {
2578 err = got_error_from_errno("getline");
2582 committer = bline->committer;
2583 smallerthan = strchr(committer, '<');
2584 if (smallerthan && smallerthan[1] != '\0')
2585 committer = smallerthan + 1;
2586 at = strchr(committer, '@');
2589 len = strlen(committer);
2591 committer[8] = '\0';
2593 nl = strchr(line, '\n');
2596 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2597 bline->id_str, bline->datebuf, committer, line);
2600 bline = &a->lines[a->lineno_cur - 1];
2604 got_object_commit_close(commit);
2609 static const struct got_error *
2610 cmd_blame(int argc, char *argv[])
2612 const struct got_error *error;
2613 struct got_repository *repo = NULL;
2614 struct got_worktree *worktree = NULL;
2615 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2616 struct got_object_id *obj_id = NULL;
2617 struct got_object_id *commit_id = NULL;
2618 struct got_blob_object *blob = NULL;
2619 char *commit_id_str = NULL;
2620 struct blame_cb_args bca;
2621 int ch, obj_type, i;
2624 memset(&bca, 0, sizeof(bca));
2627 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2632 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2635 commit_id_str = optarg;
2638 repo_path = realpath(optarg, NULL);
2639 if (repo_path == NULL)
2640 return got_error_from_errno2("realpath",
2642 got_path_strip_trailing_slashes(repo_path);
2658 cwd = getcwd(NULL, 0);
2660 error = got_error_from_errno("getcwd");
2663 if (repo_path == NULL) {
2664 error = got_worktree_open(&worktree, cwd);
2665 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2671 strdup(got_worktree_get_repo_path(worktree));
2672 if (repo_path == NULL)
2673 error = got_error_from_errno("strdup");
2677 repo_path = strdup(cwd);
2678 if (repo_path == NULL) {
2679 error = got_error_from_errno("strdup");
2685 error = got_repo_open(&repo, repo_path, NULL);
2689 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2694 const char *prefix = got_worktree_get_path_prefix(worktree);
2695 char *p, *worktree_subdir = cwd +
2696 strlen(got_worktree_get_root_path(worktree));
2697 if (asprintf(&p, "%s%s%s%s%s",
2698 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2699 worktree_subdir, worktree_subdir[0] ? "/" : "",
2701 error = got_error_from_errno("asprintf");
2704 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2707 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2712 if (commit_id_str == NULL) {
2713 struct got_reference *head_ref;
2714 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2717 error = got_ref_resolve(&commit_id, repo, head_ref);
2718 got_ref_close(head_ref);
2722 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2727 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2730 if (obj_id == NULL) {
2731 error = got_error(GOT_ERR_NO_OBJ);
2735 error = got_object_get_type(&obj_type, repo, obj_id);
2739 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2740 error = got_error(GOT_ERR_OBJ_TYPE);
2744 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2747 bca.f = got_opentemp();
2748 if (bca.f == NULL) {
2749 error = got_error_from_errno("got_opentemp");
2752 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2753 &bca.line_offsets, bca.f, blob);
2754 if (error || bca.nlines == 0)
2757 /* Don't include \n at EOF in the blame line count. */
2758 if (bca.line_offsets[bca.nlines - 1] == filesize)
2761 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2762 if (bca.lines == NULL) {
2763 error = got_error_from_errno("calloc");
2767 bca.nlines_prec = 0;
2775 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2776 check_cancelled, NULL);
2786 got_object_blob_close(blob);
2788 got_worktree_close(worktree);
2790 const struct got_error *repo_error;
2791 repo_error = got_repo_close(repo);
2796 for (i = 0; i < bca.nlines; i++) {
2797 struct blame_line *bline = &bca.lines[i];
2798 free(bline->id_str);
2799 free(bline->committer);
2803 free(bca.line_offsets);
2804 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2805 error = got_error_from_errno("fclose");
2813 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2819 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2820 const char *root_path)
2822 int is_root_path = (strcmp(path, root_path) == 0);
2823 const char *modestr = "";
2824 mode_t mode = got_tree_entry_get_mode(te);
2826 path += strlen(root_path);
2827 while (path[0] == '/')
2830 if (got_object_tree_entry_is_submodule(te))
2832 else if (S_ISLNK(mode))
2834 else if (S_ISDIR(mode))
2836 else if (mode & S_IXUSR)
2839 printf("%s%s%s%s%s\n", id ? id : "", path,
2840 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2843 static const struct got_error *
2844 print_tree(const char *path, struct got_object_id *commit_id,
2845 int show_ids, int recurse, const char *root_path,
2846 struct got_repository *repo)
2848 const struct got_error *err = NULL;
2849 struct got_object_id *tree_id = NULL;
2850 struct got_tree_object *tree = NULL;
2853 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2857 err = got_object_open_as_tree(&tree, repo, tree_id);
2860 nentries = got_object_tree_get_nentries(tree);
2861 for (i = 0; i < nentries; i++) {
2862 struct got_tree_entry *te;
2865 if (sigint_received || sigpipe_received)
2868 te = got_object_tree_get_entry(tree, i);
2871 err = got_object_id_str(&id_str,
2872 got_tree_entry_get_id(te));
2875 if (asprintf(&id, "%s ", id_str) == -1) {
2876 err = got_error_from_errno("asprintf");
2882 print_entry(te, id, path, root_path);
2885 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2887 if (asprintf(&child_path, "%s%s%s", path,
2888 path[0] == '/' && path[1] == '\0' ? "" : "/",
2889 got_tree_entry_get_name(te)) == -1) {
2890 err = got_error_from_errno("asprintf");
2893 err = print_tree(child_path, commit_id, show_ids, 1,
2902 got_object_tree_close(tree);
2907 static const struct got_error *
2908 cmd_tree(int argc, char *argv[])
2910 const struct got_error *error;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2914 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2915 struct got_object_id *commit_id = NULL;
2916 char *commit_id_str = NULL;
2917 int show_ids = 0, recurse = 0;
2921 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2926 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2929 commit_id_str = optarg;
2932 repo_path = realpath(optarg, NULL);
2933 if (repo_path == NULL)
2934 return got_error_from_errno2("realpath",
2936 got_path_strip_trailing_slashes(repo_path);
2960 cwd = getcwd(NULL, 0);
2962 error = got_error_from_errno("getcwd");
2965 if (repo_path == NULL) {
2966 error = got_worktree_open(&worktree, cwd);
2967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2973 strdup(got_worktree_get_repo_path(worktree));
2974 if (repo_path == NULL)
2975 error = got_error_from_errno("strdup");
2979 repo_path = strdup(cwd);
2980 if (repo_path == NULL) {
2981 error = got_error_from_errno("strdup");
2987 error = got_repo_open(&repo, repo_path, NULL);
2991 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2997 char *p, *worktree_subdir = cwd +
2998 strlen(got_worktree_get_root_path(worktree));
2999 if (asprintf(&p, "%s/%s",
3000 got_worktree_get_path_prefix(worktree),
3001 worktree_subdir) == -1) {
3002 error = got_error_from_errno("asprintf");
3005 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3012 if (in_repo_path == NULL) {
3013 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3018 if (commit_id_str == NULL) {
3019 struct got_reference *head_ref;
3020 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3023 error = got_ref_resolve(&commit_id, repo, head_ref);
3024 got_ref_close(head_ref);
3028 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3033 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3034 in_repo_path, repo);
3041 got_worktree_close(worktree);
3043 const struct got_error *repo_error;
3044 repo_error = got_repo_close(repo);
3054 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3058 static const struct got_error *
3059 print_status(void *arg, unsigned char status, unsigned char staged_status,
3060 const char *path, struct got_object_id *blob_id,
3061 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3062 int dirfd, const char *de_name)
3064 if (status == staged_status && (status == GOT_STATUS_DELETE))
3065 status = GOT_STATUS_NO_CHANGE;
3066 printf("%c%c %s\n", status, staged_status, path);
3070 static const struct got_error *
3071 cmd_status(int argc, char *argv[])
3073 const struct got_error *error = NULL;
3074 struct got_repository *repo = NULL;
3075 struct got_worktree *worktree = NULL;
3077 struct got_pathlist_head paths;
3078 struct got_pathlist_entry *pe;
3083 while ((ch = getopt(argc, argv, "")) != -1) {
3095 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3099 cwd = getcwd(NULL, 0);
3101 error = got_error_from_errno("getcwd");
3105 error = got_worktree_open(&worktree, cwd);
3109 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3114 error = apply_unveil(got_repo_get_path(repo), 1,
3115 got_worktree_get_root_path(worktree));
3119 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3123 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3124 check_cancelled, NULL);
3126 TAILQ_FOREACH(pe, &paths, entry)
3127 free((char *)pe->path);
3128 got_pathlist_free(&paths);
3137 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3142 static const struct got_error *
3143 list_refs(struct got_repository *repo)
3145 static const struct got_error *err = NULL;
3146 struct got_reflist_head refs;
3147 struct got_reflist_entry *re;
3149 SIMPLEQ_INIT(&refs);
3150 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3154 SIMPLEQ_FOREACH(re, &refs, entry) {
3156 refstr = got_ref_to_str(re->ref);
3158 return got_error_from_errno("got_ref_to_str");
3159 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3163 got_ref_list_free(&refs);
3167 static const struct got_error *
3168 delete_ref(struct got_repository *repo, const char *refname)
3170 const struct got_error *err = NULL;
3171 struct got_reference *ref;
3173 err = got_ref_open(&ref, repo, refname, 0);
3177 err = got_ref_delete(ref, repo);
3182 static const struct got_error *
3183 add_ref(struct got_repository *repo, const char *refname, const char *target)
3185 const struct got_error *err = NULL;
3186 struct got_object_id *id;
3187 struct got_reference *ref = NULL;
3190 * Don't let the user create a reference name with a leading '-'.
3191 * While technically a valid reference name, this case is usually
3192 * an unintended typo.
3194 if (refname[0] == '-')
3195 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3197 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3200 struct got_reference *target_ref;
3202 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3204 err = got_ref_open(&target_ref, repo, target, 0);
3207 err = got_ref_resolve(&id, repo, target_ref);
3208 got_ref_close(target_ref);
3213 err = got_ref_alloc(&ref, refname, id);
3217 err = got_ref_write(ref, repo);
3225 static const struct got_error *
3226 add_symref(struct got_repository *repo, const char *refname, const char *target)
3228 const struct got_error *err = NULL;
3229 struct got_reference *ref = NULL;
3230 struct got_reference *target_ref = NULL;
3233 * Don't let the user create a reference name with a leading '-'.
3234 * While technically a valid reference name, this case is usually
3235 * an unintended typo.
3237 if (refname[0] == '-')
3238 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3240 err = got_ref_open(&target_ref, repo, target, 0);
3244 err = got_ref_alloc_symref(&ref, refname, target_ref);
3248 err = got_ref_write(ref, repo);
3251 got_ref_close(target_ref);
3257 static const struct got_error *
3258 cmd_ref(int argc, char *argv[])
3260 const struct got_error *error = NULL;
3261 struct got_repository *repo = NULL;
3262 struct got_worktree *worktree = NULL;
3263 char *cwd = NULL, *repo_path = NULL;
3264 int ch, do_list = 0, create_symref = 0;
3265 const char *delref = NULL;
3267 /* TODO: Add -s option for adding symbolic references. */
3268 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3274 repo_path = realpath(optarg, NULL);
3275 if (repo_path == NULL)
3276 return got_error_from_errno2("realpath",
3278 got_path_strip_trailing_slashes(repo_path);
3292 if (do_list && delref)
3293 errx(1, "-l and -d options are mutually exclusive\n");
3298 if (do_list || delref) {
3300 errx(1, "-s option cannot be used together with the "
3301 "-l or -d options");
3304 } else if (argc != 2)
3309 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3313 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3314 "sendfd unveil", NULL) == -1)
3318 cwd = getcwd(NULL, 0);
3320 error = got_error_from_errno("getcwd");
3324 if (repo_path == NULL) {
3325 error = got_worktree_open(&worktree, cwd);
3326 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3332 strdup(got_worktree_get_repo_path(worktree));
3333 if (repo_path == NULL)
3334 error = got_error_from_errno("strdup");
3338 repo_path = strdup(cwd);
3339 if (repo_path == NULL) {
3340 error = got_error_from_errno("strdup");
3346 error = got_repo_open(&repo, repo_path, NULL);
3350 error = apply_unveil(got_repo_get_path(repo), do_list,
3351 worktree ? got_worktree_get_root_path(worktree) : NULL);
3356 error = list_refs(repo);
3358 error = delete_ref(repo, delref);
3359 else if (create_symref)
3360 error = add_symref(repo, argv[0], argv[1]);
3362 error = add_ref(repo, argv[0], argv[1]);
3365 got_repo_close(repo);
3367 got_worktree_close(worktree);
3377 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3378 "[name]\n", getprogname());
3382 static const struct got_error *
3383 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3384 struct got_reference *ref)
3386 const struct got_error *err = NULL;
3387 const char *refname, *marker = " ";
3390 refname = got_ref_get_name(ref);
3391 if (worktree && strcmp(refname,
3392 got_worktree_get_head_ref_name(worktree)) == 0) {
3393 struct got_object_id *id = NULL;
3395 err = got_ref_resolve(&id, repo, ref);
3398 if (got_object_id_cmp(id,
3399 got_worktree_get_base_commit_id(worktree)) == 0)
3406 if (strncmp(refname, "refs/heads/", 11) == 0)
3408 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3411 refstr = got_ref_to_str(ref);
3413 return got_error_from_errno("got_ref_to_str");
3415 printf("%s%s: %s\n", marker, refname, refstr);
3420 static const struct got_error *
3421 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3423 const char *refname;
3425 if (worktree == NULL)
3426 return got_error(GOT_ERR_NOT_WORKTREE);
3428 refname = got_worktree_get_head_ref_name(worktree);
3430 if (strncmp(refname, "refs/heads/", 11) == 0)
3432 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3435 printf("%s\n", refname);
3440 static const struct got_error *
3441 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3443 static const struct got_error *err = NULL;
3444 struct got_reflist_head refs;
3445 struct got_reflist_entry *re;
3446 struct got_reference *temp_ref = NULL;
3447 int rebase_in_progress, histedit_in_progress;
3449 SIMPLEQ_INIT(&refs);
3452 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3457 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3462 if (rebase_in_progress || histedit_in_progress) {
3463 err = got_ref_open(&temp_ref, repo,
3464 got_worktree_get_head_ref_name(worktree), 0);
3467 list_branch(repo, worktree, temp_ref);
3468 got_ref_close(temp_ref);
3472 err = got_ref_list(&refs, repo, "refs/heads",
3473 got_ref_cmp_by_name, NULL);
3477 SIMPLEQ_FOREACH(re, &refs, entry)
3478 list_branch(repo, worktree, re->ref);
3480 got_ref_list_free(&refs);
3484 static const struct got_error *
3485 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3486 const char *branch_name)
3488 const struct got_error *err = NULL;
3489 struct got_reference *ref = NULL;
3492 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3493 return got_error_from_errno("asprintf");
3495 err = got_ref_open(&ref, repo, refname, 0);
3500 strcmp(got_worktree_get_head_ref_name(worktree),
3501 got_ref_get_name(ref)) == 0) {
3502 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3503 "will not delete this work tree's current branch");
3507 err = got_ref_delete(ref, repo);
3515 static const struct got_error *
3516 add_branch(struct got_repository *repo, const char *branch_name,
3517 struct got_object_id *base_commit_id)
3519 const struct got_error *err = NULL;
3520 struct got_reference *ref = NULL;
3521 char *base_refname = NULL, *refname = NULL;
3524 * Don't let the user create a branch name with a leading '-'.
3525 * While technically a valid reference name, this case is usually
3526 * an unintended typo.
3528 if (branch_name[0] == '-')
3529 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3531 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3532 err = got_error_from_errno("asprintf");
3536 err = got_ref_open(&ref, repo, refname, 0);
3538 err = got_error(GOT_ERR_BRANCH_EXISTS);
3540 } else if (err->code != GOT_ERR_NOT_REF)
3543 err = got_ref_alloc(&ref, refname, base_commit_id);
3547 err = got_ref_write(ref, repo);
3556 static const struct got_error *
3557 cmd_branch(int argc, char *argv[])
3559 const struct got_error *error = NULL;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 char *cwd = NULL, *repo_path = NULL;
3563 int ch, do_list = 0, do_show = 0;
3564 const char *delref = NULL, *commit_id_arg = NULL;
3566 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3569 commit_id_arg = optarg;
3575 repo_path = realpath(optarg, NULL);
3576 if (repo_path == NULL)
3577 return got_error_from_errno2("realpath",
3579 got_path_strip_trailing_slashes(repo_path);
3590 if (do_list && delref)
3591 errx(1, "-l and -d options are mutually exclusive\n");
3596 if (!do_list && !delref && argc == 0)
3599 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3600 errx(1, "-c option can only be used when creating a branch");
3602 if (do_list || delref) {
3605 } else if (!do_show && argc != 1)
3609 if (do_list || do_show) {
3610 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3614 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3615 "sendfd unveil", NULL) == -1)
3619 cwd = getcwd(NULL, 0);
3621 error = got_error_from_errno("getcwd");
3625 if (repo_path == NULL) {
3626 error = got_worktree_open(&worktree, cwd);
3627 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3633 strdup(got_worktree_get_repo_path(worktree));
3634 if (repo_path == NULL)
3635 error = got_error_from_errno("strdup");
3639 repo_path = strdup(cwd);
3640 if (repo_path == NULL) {
3641 error = got_error_from_errno("strdup");
3647 error = got_repo_open(&repo, repo_path, NULL);
3651 error = apply_unveil(got_repo_get_path(repo), do_list,
3652 worktree ? got_worktree_get_root_path(worktree) : NULL);
3657 error = show_current_branch(repo, worktree);
3659 error = list_branches(repo, worktree);
3661 error = delete_branch(repo, worktree, delref);
3663 struct got_object_id *commit_id;
3664 if (commit_id_arg == NULL)
3665 commit_id_arg = worktree ?
3666 got_worktree_get_head_ref_name(worktree) :
3668 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3671 error = add_branch(repo, argv[0], commit_id);
3676 got_repo_close(repo);
3678 got_worktree_close(worktree);
3689 "usage: %s tag [-r repository] | -l | "
3690 "[-m message] name [commit]\n", getprogname());
3695 static const struct got_error *
3696 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3698 const struct got_error *err = NULL;
3699 struct got_reflist_entry *re, *se, *new;
3700 struct got_object_id *re_id, *se_id;
3701 struct got_tag_object *re_tag, *se_tag;
3702 time_t re_time, se_time;
3704 SIMPLEQ_FOREACH(re, tags, entry) {
3705 se = SIMPLEQ_FIRST(sorted);
3707 err = got_reflist_entry_dup(&new, re);
3710 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3713 err = got_ref_resolve(&re_id, repo, re->ref);
3716 err = got_object_open_as_tag(&re_tag, repo, re_id);
3720 re_time = got_object_tag_get_tagger_time(re_tag);
3721 got_object_tag_close(re_tag);
3725 err = got_ref_resolve(&se_id, repo, re->ref);
3728 err = got_object_open_as_tag(&se_tag, repo, se_id);
3732 se_time = got_object_tag_get_tagger_time(se_tag);
3733 got_object_tag_close(se_tag);
3735 if (se_time > re_time) {
3736 err = got_reflist_entry_dup(&new, re);
3739 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3742 se = SIMPLEQ_NEXT(se, entry);
3751 static const struct got_error *
3752 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3753 struct got_reference *ref2)
3755 const struct got_error *err = NULL;
3756 struct got_repository *repo = arg;
3757 struct got_object_id *id1, *id2 = NULL;
3758 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3759 time_t time1, time2;
3763 err = got_ref_resolve(&id1, repo, ref1);
3766 err = got_object_open_as_tag(&tag1, repo, id1);
3770 err = got_ref_resolve(&id2, repo, ref2);
3773 err = got_object_open_as_tag(&tag2, repo, id2);
3777 time1 = got_object_tag_get_tagger_time(tag1);
3778 time2 = got_object_tag_get_tagger_time(tag2);
3780 /* Put latest tags first. */
3783 else if (time1 > time2)
3786 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3791 got_object_tag_close(tag1);
3793 got_object_tag_close(tag2);
3797 static const struct got_error *
3798 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3800 static const struct got_error *err = NULL;
3801 struct got_reflist_head refs;
3802 struct got_reflist_entry *re;
3804 SIMPLEQ_INIT(&refs);
3806 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3810 SIMPLEQ_FOREACH(re, &refs, entry) {
3811 const char *refname;
3812 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3815 struct got_object_id *id;
3816 struct got_tag_object *tag;
3818 refname = got_ref_get_name(re->ref);
3819 if (strncmp(refname, "refs/tags/", 10) != 0)
3822 refstr = got_ref_to_str(re->ref);
3823 if (refstr == NULL) {
3824 err = got_error_from_errno("got_ref_to_str");
3827 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3830 err = got_ref_resolve(&id, repo, re->ref);
3833 err = got_object_open_as_tag(&tag, repo, id);
3837 printf("from: %s\n", got_object_tag_get_tagger(tag));
3838 tagger_time = got_object_tag_get_tagger_time(tag);
3839 datestr = get_datestr(&tagger_time, datebuf);
3841 printf("date: %s UTC\n", datestr);
3842 err = got_object_id_str(&id_str,
3843 got_object_tag_get_object_id(tag));
3846 switch (got_object_tag_get_object_type(tag)) {
3847 case GOT_OBJ_TYPE_BLOB:
3848 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3850 case GOT_OBJ_TYPE_TREE:
3851 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3853 case GOT_OBJ_TYPE_COMMIT:
3854 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3856 case GOT_OBJ_TYPE_TAG:
3857 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3863 tagmsg0 = strdup(got_object_tag_get_message(tag));
3864 got_object_tag_close(tag);
3865 if (tagmsg0 == NULL) {
3866 err = got_error_from_errno("strdup");
3872 line = strsep(&tagmsg, "\n");
3874 printf(" %s\n", line);
3879 got_ref_list_free(&refs);
3883 static const struct got_error *
3884 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3885 const char *tag_name, const char *repo_path)
3887 const struct got_error *err = NULL;
3888 char *template = NULL, *initial_content = NULL;
3889 char *editor = NULL;
3892 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3893 err = got_error_from_errno("asprintf");
3897 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3898 commit_id_str, tag_name) == -1) {
3899 err = got_error_from_errno("asprintf");
3903 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3907 dprintf(fd, initial_content);
3910 err = get_editor(&editor);
3913 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3915 free(initial_content);
3919 /* Editor is done; we can now apply unveil(2) */
3921 err = apply_unveil(repo_path, 0, NULL);
3930 static const struct got_error *
3931 add_tag(struct got_repository *repo, const char *tag_name,
3932 const char *commit_arg, const char *tagmsg_arg)
3934 const struct got_error *err = NULL;
3935 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3936 char *label = NULL, *commit_id_str = NULL;
3937 struct got_reference *ref = NULL;
3938 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3939 char *tagmsg_path = NULL, *tag_id_str = NULL;
3940 int preserve_tagmsg = 0;
3943 * Don't let the user create a tag name with a leading '-'.
3944 * While technically a valid reference name, this case is usually
3945 * an unintended typo.
3947 if (tag_name[0] == '-')
3948 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3950 err = get_author(&tagger, repo);
3954 err = match_object_id(&commit_id, &label, commit_arg,
3955 GOT_OBJ_TYPE_COMMIT, 1, repo);
3959 err = got_object_id_str(&commit_id_str, commit_id);
3963 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3964 refname = strdup(tag_name);
3965 if (refname == NULL) {
3966 err = got_error_from_errno("strdup");
3970 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3971 err = got_error_from_errno("asprintf");
3975 err = got_ref_open(&ref, repo, refname, 0);
3977 err = got_error(GOT_ERR_TAG_EXISTS);
3979 } else if (err->code != GOT_ERR_NOT_REF)
3982 if (tagmsg_arg == NULL) {
3983 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3984 tag_name, got_repo_get_path(repo));
3986 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3987 tagmsg_path != NULL)
3988 preserve_tagmsg = 1;
3993 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3994 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3997 preserve_tagmsg = 1;
4001 err = got_ref_alloc(&ref, refname, tag_id);
4004 preserve_tagmsg = 1;
4008 err = got_ref_write(ref, repo);
4011 preserve_tagmsg = 1;
4015 err = got_object_id_str(&tag_id_str, tag_id);
4018 preserve_tagmsg = 1;
4021 printf("Created tag %s\n", tag_id_str);
4023 if (preserve_tagmsg) {
4024 fprintf(stderr, "%s: tag message preserved in %s\n",
4025 getprogname(), tagmsg_path);
4026 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4027 err = got_error_from_errno2("unlink", tagmsg_path);
4032 free(commit_id_str);
4040 static const struct got_error *
4041 cmd_tag(int argc, char *argv[])
4043 const struct got_error *error = NULL;
4044 struct got_repository *repo = NULL;
4045 struct got_worktree *worktree = NULL;
4046 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4047 char *gitconfig_path = NULL;
4048 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4049 int ch, do_list = 0;
4051 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4057 repo_path = realpath(optarg, NULL);
4058 if (repo_path == NULL)
4059 return got_error_from_errno2("realpath",
4061 got_path_strip_trailing_slashes(repo_path);
4077 errx(1, "-l and -m options are mutually exclusive\n");
4080 } else if (argc < 1 || argc > 2)
4083 commit_id_arg = argv[1];
4088 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4092 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4093 "sendfd unveil", NULL) == -1)
4097 cwd = getcwd(NULL, 0);
4099 error = got_error_from_errno("getcwd");
4103 if (repo_path == NULL) {
4104 error = got_worktree_open(&worktree, cwd);
4105 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4111 strdup(got_worktree_get_repo_path(worktree));
4112 if (repo_path == NULL)
4113 error = got_error_from_errno("strdup");
4117 repo_path = strdup(cwd);
4118 if (repo_path == NULL) {
4119 error = got_error_from_errno("strdup");
4126 error = got_repo_open(&repo, repo_path, NULL);
4129 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4132 error = list_tags(repo, worktree);
4134 error = get_gitconfig_path(&gitconfig_path);
4137 error = got_repo_open(&repo, repo_path, gitconfig_path);
4142 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4147 if (commit_id_arg == NULL) {
4148 struct got_reference *head_ref;
4149 struct got_object_id *commit_id;
4150 error = got_ref_open(&head_ref, repo,
4151 worktree ? got_worktree_get_head_ref_name(worktree)
4155 error = got_ref_resolve(&commit_id, repo, head_ref);
4156 got_ref_close(head_ref);
4159 error = got_object_id_str(&commit_id_str, commit_id);
4165 error = add_tag(repo, tag_name,
4166 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4170 got_repo_close(repo);
4172 got_worktree_close(worktree);
4175 free(gitconfig_path);
4176 free(commit_id_str);
4183 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4188 static const struct got_error *
4189 add_progress(void *arg, unsigned char status, const char *path)
4191 while (path[0] == '/')
4193 printf("%c %s\n", status, path);
4197 static const struct got_error *
4198 cmd_add(int argc, char *argv[])
4200 const struct got_error *error = NULL;
4201 struct got_repository *repo = NULL;
4202 struct got_worktree *worktree = NULL;
4204 struct got_pathlist_head paths;
4205 struct got_pathlist_entry *pe;
4206 int ch, can_recurse = 0, no_ignores = 0;
4210 while ((ch = getopt(argc, argv, "IR")) != -1) {
4228 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4235 cwd = getcwd(NULL, 0);
4237 error = got_error_from_errno("getcwd");
4241 error = got_worktree_open(&worktree, cwd);
4245 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4250 error = apply_unveil(got_repo_get_path(repo), 1,
4251 got_worktree_get_root_path(worktree));
4255 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4259 if (!can_recurse && no_ignores) {
4260 error = got_error_msg(GOT_ERR_BAD_PATH,
4261 "disregarding ignores requires -R option");
4269 TAILQ_FOREACH(pe, &paths, entry) {
4270 if (asprintf(&ondisk_path, "%s/%s",
4271 got_worktree_get_root_path(worktree),
4273 error = got_error_from_errno("asprintf");
4276 if (lstat(ondisk_path, &sb) == -1) {
4277 if (errno == ENOENT) {
4281 error = got_error_from_errno2("lstat",
4287 if (S_ISDIR(sb.st_mode)) {
4288 error = got_error_msg(GOT_ERR_BAD_PATH,
4289 "adding directories requires -R option");
4295 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4296 NULL, repo, no_ignores);
4299 got_repo_close(repo);
4301 got_worktree_close(worktree);
4302 TAILQ_FOREACH(pe, &paths, entry)
4303 free((char *)pe->path);
4304 got_pathlist_free(&paths);
4312 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4317 static const struct got_error *
4318 print_remove_status(void *arg, unsigned char status,
4319 unsigned char staged_status, const char *path)
4321 while (path[0] == '/')
4323 if (status == GOT_STATUS_NONEXISTENT)
4325 if (status == staged_status && (status == GOT_STATUS_DELETE))
4326 status = GOT_STATUS_NO_CHANGE;
4327 printf("%c%c %s\n", status, staged_status, path);
4331 static const struct got_error *
4332 cmd_remove(int argc, char *argv[])
4334 const struct got_error *error = NULL;
4335 struct got_worktree *worktree = NULL;
4336 struct got_repository *repo = NULL;
4338 struct got_pathlist_head paths;
4339 struct got_pathlist_entry *pe;
4340 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4344 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4347 delete_local_mods = 1;
4365 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4372 cwd = getcwd(NULL, 0);
4374 error = got_error_from_errno("getcwd");
4377 error = got_worktree_open(&worktree, cwd);
4381 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4386 error = apply_unveil(got_repo_get_path(repo), 1,
4387 got_worktree_get_root_path(worktree));
4391 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4398 TAILQ_FOREACH(pe, &paths, entry) {
4399 if (asprintf(&ondisk_path, "%s/%s",
4400 got_worktree_get_root_path(worktree),
4402 error = got_error_from_errno("asprintf");
4405 if (lstat(ondisk_path, &sb) == -1) {
4406 if (errno == ENOENT) {
4410 error = got_error_from_errno2("lstat",
4416 if (S_ISDIR(sb.st_mode)) {
4417 error = got_error_msg(GOT_ERR_BAD_PATH,
4418 "removing directories requires -R option");
4424 error = got_worktree_schedule_delete(worktree, &paths,
4425 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4430 got_repo_close(repo);
4432 got_worktree_close(worktree);
4433 TAILQ_FOREACH(pe, &paths, entry)
4434 free((char *)pe->path);
4435 got_pathlist_free(&paths);
4443 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4444 "path ...\n", getprogname());
4448 static const struct got_error *
4449 revert_progress(void *arg, unsigned char status, const char *path)
4451 while (path[0] == '/')
4453 printf("%c %s\n", status, path);
4457 struct choose_patch_arg {
4458 FILE *patch_script_file;
4462 static const struct got_error *
4463 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4464 int nchanges, const char *action)
4467 size_t linesize = 0;
4471 case GOT_STATUS_ADD:
4472 printf("A %s\n%s this addition? [y/n] ", path, action);
4474 case GOT_STATUS_DELETE:
4475 printf("D %s\n%s this deletion? [y/n] ", path, action);
4477 case GOT_STATUS_MODIFY:
4478 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4479 return got_error_from_errno("fseek");
4480 printf(GOT_COMMIT_SEP_STR);
4481 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4483 if (ferror(patch_file))
4484 return got_error_from_errno("getline");
4485 printf(GOT_COMMIT_SEP_STR);
4486 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4487 path, n, nchanges, action);
4490 return got_error_path(path, GOT_ERR_FILE_STATUS);
4496 static const struct got_error *
4497 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4498 FILE *patch_file, int n, int nchanges)
4500 const struct got_error *err = NULL;
4502 size_t linesize = 0;
4505 struct choose_patch_arg *a = arg;
4507 *choice = GOT_PATCH_CHOICE_NONE;
4509 if (a->patch_script_file) {
4511 err = show_change(status, path, patch_file, n, nchanges,
4515 linelen = getline(&line, &linesize, a->patch_script_file);
4516 if (linelen == -1) {
4517 if (ferror(a->patch_script_file))
4518 return got_error_from_errno("getline");
4521 nl = strchr(line, '\n');
4524 if (strcmp(line, "y") == 0) {
4525 *choice = GOT_PATCH_CHOICE_YES;
4527 } else if (strcmp(line, "n") == 0) {
4528 *choice = GOT_PATCH_CHOICE_NO;
4530 } else if (strcmp(line, "q") == 0 &&
4531 status == GOT_STATUS_MODIFY) {
4532 *choice = GOT_PATCH_CHOICE_QUIT;
4535 printf("invalid response '%s'\n", line);
4540 while (resp != 'y' && resp != 'n' && resp != 'q') {
4541 err = show_change(status, path, patch_file, n, nchanges,
4548 if (status == GOT_STATUS_MODIFY) {
4549 if (resp != 'y' && resp != 'n' && resp != 'q') {
4550 printf("invalid response '%c'\n", resp);
4553 } else if (resp != 'y' && resp != 'n') {
4554 printf("invalid response '%c'\n", resp);
4560 *choice = GOT_PATCH_CHOICE_YES;
4561 else if (resp == 'n')
4562 *choice = GOT_PATCH_CHOICE_NO;
4563 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4564 *choice = GOT_PATCH_CHOICE_QUIT;
4570 static const struct got_error *
4571 cmd_revert(int argc, char *argv[])
4573 const struct got_error *error = NULL;
4574 struct got_worktree *worktree = NULL;
4575 struct got_repository *repo = NULL;
4576 char *cwd = NULL, *path = NULL;
4577 struct got_pathlist_head paths;
4578 struct got_pathlist_entry *pe;
4579 int ch, can_recurse = 0, pflag = 0;
4580 FILE *patch_script_file = NULL;
4581 const char *patch_script_path = NULL;
4582 struct choose_patch_arg cpa;
4586 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4592 patch_script_path = optarg;
4607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4608 "unveil", NULL) == -1)
4613 if (patch_script_path && !pflag)
4614 errx(1, "-F option can only be used together with -p option");
4616 cwd = getcwd(NULL, 0);
4618 error = got_error_from_errno("getcwd");
4621 error = got_worktree_open(&worktree, cwd);
4625 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4630 if (patch_script_path) {
4631 patch_script_file = fopen(patch_script_path, "r");
4632 if (patch_script_file == NULL) {
4633 error = got_error_from_errno2("fopen",
4638 error = apply_unveil(got_repo_get_path(repo), 1,
4639 got_worktree_get_root_path(worktree));
4643 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4650 TAILQ_FOREACH(pe, &paths, entry) {
4651 if (asprintf(&ondisk_path, "%s/%s",
4652 got_worktree_get_root_path(worktree),
4654 error = got_error_from_errno("asprintf");
4657 if (lstat(ondisk_path, &sb) == -1) {
4658 if (errno == ENOENT) {
4662 error = got_error_from_errno2("lstat",
4668 if (S_ISDIR(sb.st_mode)) {
4669 error = got_error_msg(GOT_ERR_BAD_PATH,
4670 "reverting directories requires -R option");
4676 cpa.patch_script_file = patch_script_file;
4677 cpa.action = "revert";
4678 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4679 pflag ? choose_patch : NULL, &cpa, repo);
4683 if (patch_script_file && fclose(patch_script_file) == EOF &&
4685 error = got_error_from_errno2("fclose", patch_script_path);
4687 got_repo_close(repo);
4689 got_worktree_close(worktree);
4698 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4703 struct collect_commit_logmsg_arg {
4704 const char *cmdline_log;
4706 const char *worktree_path;
4707 const char *branch_name;
4708 const char *repo_path;
4713 static const struct got_error *
4714 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4717 char *initial_content = NULL;
4718 struct got_pathlist_entry *pe;
4719 const struct got_error *err = NULL;
4720 char *template = NULL;
4721 struct collect_commit_logmsg_arg *a = arg;
4725 /* if a message was specified on the command line, just use it */
4726 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4727 len = strlen(a->cmdline_log) + 1;
4728 *logmsg = malloc(len + 1);
4729 if (*logmsg == NULL)
4730 return got_error_from_errno("malloc");
4731 strlcpy(*logmsg, a->cmdline_log, len);
4735 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4736 return got_error_from_errno("asprintf");
4738 if (asprintf(&initial_content,
4739 "\n# changes to be committed on branch %s:\n",
4740 a->branch_name) == -1)
4741 return got_error_from_errno("asprintf");
4743 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4747 dprintf(fd, initial_content);
4749 TAILQ_FOREACH(pe, commitable_paths, entry) {
4750 struct got_commitable *ct = pe->data;
4751 dprintf(fd, "# %c %s\n",
4752 got_commitable_get_status(ct),
4753 got_commitable_get_path(ct));
4757 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4759 free(initial_content);
4762 /* Editor is done; we can now apply unveil(2) */
4764 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4773 static const struct got_error *
4774 cmd_commit(int argc, char *argv[])
4776 const struct got_error *error = NULL;
4777 struct got_worktree *worktree = NULL;
4778 struct got_repository *repo = NULL;
4779 char *cwd = NULL, *id_str = NULL;
4780 struct got_object_id *id = NULL;
4781 const char *logmsg = NULL;
4782 struct collect_commit_logmsg_arg cl_arg;
4783 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4784 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4785 struct got_pathlist_head paths;
4788 cl_arg.logmsg_path = NULL;
4790 while ((ch = getopt(argc, argv, "m:")) != -1) {
4805 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 "unveil", NULL) == -1)
4809 cwd = getcwd(NULL, 0);
4811 error = got_error_from_errno("getcwd");
4814 error = got_worktree_open(&worktree, cwd);
4818 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4821 if (rebase_in_progress) {
4822 error = got_error(GOT_ERR_REBASING);
4826 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4831 error = get_gitconfig_path(&gitconfig_path);
4834 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4839 error = get_author(&author, repo);
4844 * unveil(2) traverses exec(2); if an editor is used we have
4845 * to apply unveil after the log message has been written.
4847 if (logmsg == NULL || strlen(logmsg) == 0)
4848 error = get_editor(&editor);
4850 error = apply_unveil(got_repo_get_path(repo), 0,
4851 got_worktree_get_root_path(worktree));
4855 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4859 cl_arg.editor = editor;
4860 cl_arg.cmdline_log = logmsg;
4861 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4862 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4863 if (!histedit_in_progress) {
4864 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4865 error = got_error(GOT_ERR_COMMIT_BRANCH);
4868 cl_arg.branch_name += 11;
4870 cl_arg.repo_path = got_repo_get_path(repo);
4871 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4872 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4874 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4875 cl_arg.logmsg_path != NULL)
4876 preserve_logmsg = 1;
4880 error = got_object_id_str(&id_str, id);
4883 printf("Created commit %s\n", id_str);
4885 if (preserve_logmsg) {
4886 fprintf(stderr, "%s: log message preserved in %s\n",
4887 getprogname(), cl_arg.logmsg_path);
4888 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4890 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4891 free(cl_arg.logmsg_path);
4893 got_repo_close(repo);
4895 got_worktree_close(worktree);
4898 free(gitconfig_path);
4905 usage_cherrypick(void)
4907 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4911 static const struct got_error *
4912 cmd_cherrypick(int argc, char *argv[])
4914 const struct got_error *error = NULL;
4915 struct got_worktree *worktree = NULL;
4916 struct got_repository *repo = NULL;
4917 char *cwd = NULL, *commit_id_str = NULL;
4918 struct got_object_id *commit_id = NULL;
4919 struct got_commit_object *commit = NULL;
4920 struct got_object_qid *pid;
4921 struct got_reference *head_ref = NULL;
4922 int ch, did_something = 0;
4924 while ((ch = getopt(argc, argv, "")) != -1) {
4936 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4937 "unveil", NULL) == -1)
4943 cwd = getcwd(NULL, 0);
4945 error = got_error_from_errno("getcwd");
4948 error = got_worktree_open(&worktree, cwd);
4952 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4957 error = apply_unveil(got_repo_get_path(repo), 0,
4958 got_worktree_get_root_path(worktree));
4962 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4963 GOT_OBJ_TYPE_COMMIT, repo);
4964 if (error != NULL) {
4965 struct got_reference *ref;
4966 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4968 error = got_ref_open(&ref, repo, argv[0], 0);
4971 error = got_ref_resolve(&commit_id, repo, ref);
4976 error = got_object_id_str(&commit_id_str, commit_id);
4980 error = got_ref_open(&head_ref, repo,
4981 got_worktree_get_head_ref_name(worktree), 0);
4985 error = check_same_branch(commit_id, head_ref, NULL, repo);
4987 if (error->code != GOT_ERR_ANCESTRY)
4991 error = got_error(GOT_ERR_SAME_BRANCH);
4995 error = got_object_open_as_commit(&commit, repo, commit_id);
4998 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4999 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5000 commit_id, repo, update_progress, &did_something, check_cancelled,
5006 printf("Merged commit %s\n", commit_id_str);
5009 got_object_commit_close(commit);
5010 free(commit_id_str);
5012 got_ref_close(head_ref);
5014 got_worktree_close(worktree);
5016 got_repo_close(repo);
5023 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5027 static const struct got_error *
5028 cmd_backout(int argc, char *argv[])
5030 const struct got_error *error = NULL;
5031 struct got_worktree *worktree = NULL;
5032 struct got_repository *repo = NULL;
5033 char *cwd = NULL, *commit_id_str = NULL;
5034 struct got_object_id *commit_id = NULL;
5035 struct got_commit_object *commit = NULL;
5036 struct got_object_qid *pid;
5037 struct got_reference *head_ref = NULL;
5038 int ch, did_something = 0;
5040 while ((ch = getopt(argc, argv, "")) != -1) {
5052 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5053 "unveil", NULL) == -1)
5059 cwd = getcwd(NULL, 0);
5061 error = got_error_from_errno("getcwd");
5064 error = got_worktree_open(&worktree, cwd);
5068 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5073 error = apply_unveil(got_repo_get_path(repo), 0,
5074 got_worktree_get_root_path(worktree));
5078 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5079 GOT_OBJ_TYPE_COMMIT, repo);
5080 if (error != NULL) {
5081 struct got_reference *ref;
5082 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5084 error = got_ref_open(&ref, repo, argv[0], 0);
5087 error = got_ref_resolve(&commit_id, repo, ref);
5092 error = got_object_id_str(&commit_id_str, commit_id);
5096 error = got_ref_open(&head_ref, repo,
5097 got_worktree_get_head_ref_name(worktree), 0);
5101 error = check_same_branch(commit_id, head_ref, NULL, repo);
5105 error = got_object_open_as_commit(&commit, repo, commit_id);
5108 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5110 error = got_error(GOT_ERR_ROOT_COMMIT);
5114 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5115 update_progress, &did_something, check_cancelled, NULL);
5120 printf("Backed out commit %s\n", commit_id_str);
5123 got_object_commit_close(commit);
5124 free(commit_id_str);
5126 got_ref_close(head_ref);
5128 got_worktree_close(worktree);
5130 got_repo_close(repo);
5137 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5143 trim_logmsg(char *logmsg, int limit)
5148 len = strlen(logmsg);
5152 nl = strchr(logmsg, '\n');
5157 static const struct got_error *
5158 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5160 const struct got_error *err;
5161 char *logmsg0 = NULL;
5164 err = got_object_commit_get_logmsg(&logmsg0, commit);
5169 while (isspace((unsigned char)s[0]))
5172 *logmsg = strdup(s);
5173 if (*logmsg == NULL) {
5174 err = got_error_from_errno("strdup");
5178 trim_logmsg(*logmsg, limit);
5184 static const struct got_error *
5185 show_rebase_progress(struct got_commit_object *commit,
5186 struct got_object_id *old_id, struct got_object_id *new_id)
5188 const struct got_error *err;
5189 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5191 err = got_object_id_str(&old_id_str, old_id);
5196 err = got_object_id_str(&new_id_str, new_id);
5201 old_id_str[12] = '\0';
5203 new_id_str[12] = '\0';
5205 err = get_short_logmsg(&logmsg, 42, commit);
5209 printf("%s -> %s: %s\n", old_id_str,
5210 new_id_str ? new_id_str : "no-op change", logmsg);
5217 static const struct got_error *
5218 rebase_progress(void *arg, unsigned char status, const char *path)
5220 unsigned char *rebase_status = arg;
5222 while (path[0] == '/')
5224 printf("%c %s\n", status, path);
5226 if (*rebase_status == GOT_STATUS_CONFLICT)
5228 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5229 *rebase_status = status;
5233 static const struct got_error *
5234 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5235 struct got_reference *branch, struct got_reference *new_base_branch,
5236 struct got_reference *tmp_branch, struct got_repository *repo)
5238 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5239 return got_worktree_rebase_complete(worktree, fileindex,
5240 new_base_branch, tmp_branch, branch, repo);
5243 static const struct got_error *
5244 rebase_commit(struct got_pathlist_head *merged_paths,
5245 struct got_worktree *worktree, struct got_fileindex *fileindex,
5246 struct got_reference *tmp_branch,
5247 struct got_object_id *commit_id, struct got_repository *repo)
5249 const struct got_error *error;
5250 struct got_commit_object *commit;
5251 struct got_object_id *new_commit_id;
5253 error = got_object_open_as_commit(&commit, repo, commit_id);
5257 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5258 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5260 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5262 error = show_rebase_progress(commit, commit_id, NULL);
5264 error = show_rebase_progress(commit, commit_id, new_commit_id);
5265 free(new_commit_id);
5268 got_object_commit_close(commit);
5272 struct check_path_prefix_arg {
5273 const char *path_prefix;
5278 static const struct got_error *
5279 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5280 struct got_blob_object *blob2, struct got_object_id *id1,
5281 struct got_object_id *id2, const char *path1, const char *path2,
5282 mode_t mode1, mode_t mode2, struct got_repository *repo)
5284 struct check_path_prefix_arg *a = arg;
5286 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5287 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5288 return got_error(a->errcode);
5293 static const struct got_error *
5294 check_path_prefix(struct got_object_id *parent_id,
5295 struct got_object_id *commit_id, const char *path_prefix,
5296 int errcode, struct got_repository *repo)
5298 const struct got_error *err;
5299 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5300 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5301 struct check_path_prefix_arg cpp_arg;
5303 if (got_path_is_root_dir(path_prefix))
5306 err = got_object_open_as_commit(&commit, repo, commit_id);
5310 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5314 err = got_object_open_as_tree(&tree1, repo,
5315 got_object_commit_get_tree_id(parent_commit));
5319 err = got_object_open_as_tree(&tree2, repo,
5320 got_object_commit_get_tree_id(commit));
5324 cpp_arg.path_prefix = path_prefix;
5325 while (cpp_arg.path_prefix[0] == '/')
5326 cpp_arg.path_prefix++;
5327 cpp_arg.len = strlen(cpp_arg.path_prefix);
5328 cpp_arg.errcode = errcode;
5329 err = got_diff_tree(tree1, tree2, "", "", repo,
5330 check_path_prefix_in_diff, &cpp_arg, 0);
5333 got_object_tree_close(tree1);
5335 got_object_tree_close(tree2);
5337 got_object_commit_close(commit);
5339 got_object_commit_close(parent_commit);
5343 static const struct got_error *
5344 collect_commits(struct got_object_id_queue *commits,
5345 struct got_object_id *initial_commit_id,
5346 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5347 const char *path_prefix, int path_prefix_errcode,
5348 struct got_repository *repo)
5350 const struct got_error *err = NULL;
5351 struct got_commit_graph *graph = NULL;
5352 struct got_object_id *parent_id = NULL;
5353 struct got_object_qid *qid;
5354 struct got_object_id *commit_id = initial_commit_id;
5356 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5360 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5361 check_cancelled, NULL);
5364 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5365 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5366 check_cancelled, NULL);
5368 if (err->code == GOT_ERR_ITER_COMPLETED) {
5369 err = got_error_msg(GOT_ERR_ANCESTRY,
5370 "ran out of commits to rebase before "
5371 "youngest common ancestor commit has "
5376 err = check_path_prefix(parent_id, commit_id,
5377 path_prefix, path_prefix_errcode, repo);
5381 err = got_object_qid_alloc(&qid, commit_id);
5384 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5385 commit_id = parent_id;
5389 got_commit_graph_close(graph);
5393 static const struct got_error *
5394 cmd_rebase(int argc, char *argv[])
5396 const struct got_error *error = NULL;
5397 struct got_worktree *worktree = NULL;
5398 struct got_repository *repo = NULL;
5399 struct got_fileindex *fileindex = NULL;
5401 struct got_reference *branch = NULL;
5402 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5403 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5404 struct got_object_id *resume_commit_id = NULL;
5405 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5406 struct got_commit_object *commit = NULL;
5407 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5408 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5409 struct got_object_id_queue commits;
5410 struct got_pathlist_head merged_paths;
5411 const struct got_object_id_queue *parent_ids;
5412 struct got_object_qid *qid, *pid;
5414 SIMPLEQ_INIT(&commits);
5415 TAILQ_INIT(&merged_paths);
5417 while ((ch = getopt(argc, argv, "ac")) != -1) {
5423 continue_rebase = 1;
5435 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5436 "unveil", NULL) == -1)
5439 if (abort_rebase && continue_rebase)
5441 else if (abort_rebase || continue_rebase) {
5444 } else if (argc != 1)
5447 cwd = getcwd(NULL, 0);
5449 error = got_error_from_errno("getcwd");
5452 error = got_worktree_open(&worktree, cwd);
5456 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5461 error = apply_unveil(got_repo_get_path(repo), 0,
5462 got_worktree_get_root_path(worktree));
5466 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5472 if (!rebase_in_progress) {
5473 error = got_error(GOT_ERR_NOT_REBASING);
5476 error = got_worktree_rebase_continue(&resume_commit_id,
5477 &new_base_branch, &tmp_branch, &branch, &fileindex,
5481 printf("Switching work tree to %s\n",
5482 got_ref_get_symref_target(new_base_branch));
5483 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5484 new_base_branch, update_progress, &did_something);
5487 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5488 goto done; /* nothing else to do */
5491 if (continue_rebase) {
5492 if (!rebase_in_progress) {
5493 error = got_error(GOT_ERR_NOT_REBASING);
5496 error = got_worktree_rebase_continue(&resume_commit_id,
5497 &new_base_branch, &tmp_branch, &branch, &fileindex,
5502 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5503 resume_commit_id, repo);
5507 yca_id = got_object_id_dup(resume_commit_id);
5508 if (yca_id == NULL) {
5509 error = got_error_from_errno("got_object_id_dup");
5513 error = got_ref_open(&branch, repo, argv[0], 0);
5518 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5522 if (!continue_rebase) {
5523 struct got_object_id *base_commit_id;
5525 base_commit_id = got_worktree_get_base_commit_id(worktree);
5526 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5527 base_commit_id, branch_head_commit_id, repo,
5528 check_cancelled, NULL);
5531 if (yca_id == NULL) {
5532 error = got_error_msg(GOT_ERR_ANCESTRY,
5533 "specified branch shares no common ancestry "
5534 "with work tree's branch");
5538 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5540 if (error->code != GOT_ERR_ANCESTRY)
5544 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5545 "specified branch resolves to a commit which "
5546 "is already contained in work tree's branch");
5549 error = got_worktree_rebase_prepare(&new_base_branch,
5550 &tmp_branch, &fileindex, worktree, branch, repo);
5555 commit_id = branch_head_commit_id;
5556 error = got_object_open_as_commit(&commit, repo, commit_id);
5560 parent_ids = got_object_commit_get_parent_ids(commit);
5561 pid = SIMPLEQ_FIRST(parent_ids);
5563 if (!continue_rebase) {
5565 error = got_worktree_rebase_abort(worktree, fileindex,
5566 repo, new_base_branch, update_progress,
5570 printf("Rebase of %s aborted\n",
5571 got_ref_get_name(branch));
5573 error = got_error(GOT_ERR_EMPTY_REBASE);
5576 error = collect_commits(&commits, commit_id, pid->id,
5577 yca_id, got_worktree_get_path_prefix(worktree),
5578 GOT_ERR_REBASE_PATH, repo);
5579 got_object_commit_close(commit);
5584 if (SIMPLEQ_EMPTY(&commits)) {
5585 if (continue_rebase) {
5586 error = rebase_complete(worktree, fileindex,
5587 branch, new_base_branch, tmp_branch, repo);
5590 /* Fast-forward the reference of the branch. */
5591 struct got_object_id *new_head_commit_id;
5593 error = got_ref_resolve(&new_head_commit_id, repo,
5597 error = got_object_id_str(&id_str, new_head_commit_id);
5598 printf("Forwarding %s to commit %s\n",
5599 got_ref_get_name(branch), id_str);
5601 error = got_ref_change_ref(branch,
5602 new_head_commit_id);
5609 SIMPLEQ_FOREACH(qid, &commits, entry) {
5610 commit_id = qid->id;
5611 parent_id = pid ? pid->id : yca_id;
5614 error = got_worktree_rebase_merge_files(&merged_paths,
5615 worktree, fileindex, parent_id, commit_id, repo,
5616 rebase_progress, &rebase_status, check_cancelled, NULL);
5620 if (rebase_status == GOT_STATUS_CONFLICT) {
5621 got_worktree_rebase_pathlist_free(&merged_paths);
5625 error = rebase_commit(&merged_paths, worktree, fileindex,
5626 tmp_branch, commit_id, repo);
5627 got_worktree_rebase_pathlist_free(&merged_paths);
5632 if (rebase_status == GOT_STATUS_CONFLICT) {
5633 error = got_worktree_rebase_postpone(worktree, fileindex);
5636 error = got_error_msg(GOT_ERR_CONFLICTS,
5637 "conflicts must be resolved before rebasing can continue");
5639 error = rebase_complete(worktree, fileindex, branch,
5640 new_base_branch, tmp_branch, repo);
5642 got_object_id_queue_free(&commits);
5643 free(branch_head_commit_id);
5644 free(resume_commit_id);
5647 got_object_commit_close(commit);
5649 got_ref_close(branch);
5650 if (new_base_branch)
5651 got_ref_close(new_base_branch);
5653 got_ref_close(tmp_branch);
5655 got_worktree_close(worktree);
5657 got_repo_close(repo);
5662 usage_histedit(void)
5664 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5669 #define GOT_HISTEDIT_PICK 'p'
5670 #define GOT_HISTEDIT_EDIT 'e'
5671 #define GOT_HISTEDIT_FOLD 'f'
5672 #define GOT_HISTEDIT_DROP 'd'
5673 #define GOT_HISTEDIT_MESG 'm'
5675 static struct got_histedit_cmd {
5679 } got_histedit_cmds[] = {
5680 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5681 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5682 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5683 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5684 { GOT_HISTEDIT_MESG, "mesg",
5685 "single-line log message for commit above (open editor if empty)" },
5688 struct got_histedit_list_entry {
5689 TAILQ_ENTRY(got_histedit_list_entry) entry;
5690 struct got_object_id *commit_id;
5691 const struct got_histedit_cmd *cmd;
5694 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5696 static const struct got_error *
5697 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5698 FILE *f, struct got_repository *repo)
5700 const struct got_error *err = NULL;
5701 char *logmsg = NULL, *id_str = NULL;
5702 struct got_commit_object *commit = NULL;
5705 err = got_object_open_as_commit(&commit, repo, commit_id);
5709 err = get_short_logmsg(&logmsg, 34, commit);
5713 err = got_object_id_str(&id_str, commit_id);
5717 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5719 err = got_ferror(f, GOT_ERR_IO);
5722 got_object_commit_close(commit);
5728 static const struct got_error *
5729 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5730 struct got_repository *repo)
5732 const struct got_error *err = NULL;
5733 struct got_object_qid *qid;
5735 if (SIMPLEQ_EMPTY(commits))
5736 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5738 SIMPLEQ_FOREACH(qid, commits, entry) {
5739 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5748 static const struct got_error *
5749 write_cmd_list(FILE *f)
5751 const struct got_error *err = NULL;
5754 n = fprintf(f, "# Available histedit commands:\n");
5756 return got_ferror(f, GOT_ERR_IO);
5758 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5759 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5760 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5763 err = got_ferror(f, GOT_ERR_IO);
5767 n = fprintf(f, "# Commits will be processed in order from top to "
5768 "bottom of this file.\n");
5770 return got_ferror(f, GOT_ERR_IO);
5774 static const struct got_error *
5775 histedit_syntax_error(int lineno)
5777 static char msg[42];
5780 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5782 if (ret == -1 || ret >= sizeof(msg))
5783 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5785 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5788 static const struct got_error *
5789 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5790 char *logmsg, struct got_repository *repo)
5792 const struct got_error *err;
5793 struct got_commit_object *folded_commit = NULL;
5794 char *id_str, *folded_logmsg = NULL;
5796 err = got_object_id_str(&id_str, hle->commit_id);
5800 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5804 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5807 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5808 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5809 folded_logmsg) == -1) {
5810 err = got_error_from_errno("asprintf");
5815 got_object_commit_close(folded_commit);
5817 free(folded_logmsg);
5821 static struct got_histedit_list_entry *
5822 get_folded_commits(struct got_histedit_list_entry *hle)
5824 struct got_histedit_list_entry *prev, *folded = NULL;
5826 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5827 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5828 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5829 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5831 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5837 static const struct got_error *
5838 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5839 struct got_repository *repo)
5841 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5842 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5843 const struct got_error *err = NULL;
5844 struct got_commit_object *commit = NULL;
5846 struct got_histedit_list_entry *folded = NULL;
5848 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5852 folded = get_folded_commits(hle);
5854 while (folded != hle) {
5855 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5856 folded = TAILQ_NEXT(folded, entry);
5859 err = append_folded_commit_msg(&new_msg, folded,
5865 folded = TAILQ_NEXT(folded, entry);
5869 err = got_object_id_str(&id_str, hle->commit_id);
5872 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5875 if (asprintf(&new_msg,
5876 "%s\n# original log message of commit %s: %s",
5877 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5878 err = got_error_from_errno("asprintf");
5884 err = got_object_id_str(&id_str, hle->commit_id);
5888 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5892 dprintf(fd, logmsg);
5895 err = get_editor(&editor);
5899 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5901 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5903 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5906 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5907 err = got_error_from_errno2("unlink", logmsg_path);
5913 got_object_commit_close(commit);
5917 static const struct got_error *
5918 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5919 FILE *f, struct got_repository *repo)
5921 const struct got_error *err = NULL;
5922 char *line = NULL, *p, *end;
5926 const struct got_histedit_cmd *cmd;
5927 struct got_object_id *commit_id = NULL;
5928 struct got_histedit_list_entry *hle = NULL;
5931 len = getline(&line, &size, f);
5933 const struct got_error *getline_err;
5936 getline_err = got_error_from_errno("getline");
5937 err = got_ferror(f, getline_err->code);
5942 while (isspace((unsigned char)p[0]))
5944 if (p[0] == '#' || p[0] == '\0') {
5950 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5951 cmd = &got_histedit_cmds[i];
5952 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5953 isspace((unsigned char)p[strlen(cmd->name)])) {
5954 p += strlen(cmd->name);
5957 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5962 if (i == nitems(got_histedit_cmds)) {
5963 err = histedit_syntax_error(lineno);
5966 while (isspace((unsigned char)p[0]))
5968 if (cmd->code == GOT_HISTEDIT_MESG) {
5969 if (hle == NULL || hle->logmsg != NULL) {
5970 err = got_error(GOT_ERR_HISTEDIT_CMD);
5974 err = histedit_edit_logmsg(hle, repo);
5978 hle->logmsg = strdup(p);
5979 if (hle->logmsg == NULL) {
5980 err = got_error_from_errno("strdup");
5989 while (end[0] && !isspace((unsigned char)end[0]))
5993 err = got_object_resolve_id_str(&commit_id, repo, p);
5995 /* override error code */
5996 err = histedit_syntax_error(lineno);
6000 hle = malloc(sizeof(*hle));
6002 err = got_error_from_errno("malloc");
6006 hle->commit_id = commit_id;
6011 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6019 static const struct got_error *
6020 histedit_check_script(struct got_histedit_list *histedit_cmds,
6021 struct got_object_id_queue *commits, struct got_repository *repo)
6023 const struct got_error *err = NULL;
6024 struct got_object_qid *qid;
6025 struct got_histedit_list_entry *hle;
6026 static char msg[80];
6029 if (TAILQ_EMPTY(histedit_cmds))
6030 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6031 "histedit script contains no commands");
6032 if (SIMPLEQ_EMPTY(commits))
6033 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6035 SIMPLEQ_FOREACH(qid, commits, entry) {
6036 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6037 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6041 err = got_object_id_str(&id_str, qid->id);
6044 snprintf(msg, sizeof(msg),
6045 "commit %s missing from histedit script", id_str);
6047 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6051 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6052 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6053 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6054 "last commit in histedit script cannot be folded");
6059 static const struct got_error *
6060 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6061 const char *path, struct got_object_id_queue *commits,
6062 struct got_repository *repo)
6064 const struct got_error *err = NULL;
6068 err = get_editor(&editor);
6072 if (spawn_editor(editor, path) == -1) {
6073 err = got_error_from_errno("failed spawning editor");
6077 f = fopen(path, "r");
6079 err = got_error_from_errno("fopen");
6082 err = histedit_parse_list(histedit_cmds, f, repo);
6086 err = histedit_check_script(histedit_cmds, commits, repo);
6088 if (f && fclose(f) != 0 && err == NULL)
6089 err = got_error_from_errno("fclose");
6094 static const struct got_error *
6095 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6096 struct got_object_id_queue *, const char *, struct got_repository *);
6098 static const struct got_error *
6099 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6100 struct got_object_id_queue *commits, struct got_repository *repo)
6102 const struct got_error *err;
6106 err = got_opentemp_named(&path, &f, "got-histedit");
6110 err = write_cmd_list(f);
6114 err = histedit_write_commit_list(commits, f, repo);
6118 if (fclose(f) != 0) {
6119 err = got_error_from_errno("fclose");
6124 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6126 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6127 err->code != GOT_ERR_HISTEDIT_CMD)
6129 err = histedit_edit_list_retry(histedit_cmds, err,
6130 commits, path, repo);
6133 if (f && fclose(f) != 0 && err == NULL)
6134 err = got_error_from_errno("fclose");
6135 if (path && unlink(path) != 0 && err == NULL)
6136 err = got_error_from_errno2("unlink", path);
6141 static const struct got_error *
6142 histedit_save_list(struct got_histedit_list *histedit_cmds,
6143 struct got_worktree *worktree, struct got_repository *repo)
6145 const struct got_error *err = NULL;
6148 struct got_histedit_list_entry *hle;
6149 struct got_commit_object *commit = NULL;
6151 err = got_worktree_get_histedit_script_path(&path, worktree);
6155 f = fopen(path, "w");
6157 err = got_error_from_errno2("fopen", path);
6160 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6161 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6167 int n = fprintf(f, "%c %s\n",
6168 GOT_HISTEDIT_MESG, hle->logmsg);
6170 err = got_ferror(f, GOT_ERR_IO);
6176 if (f && fclose(f) != 0 && err == NULL)
6177 err = got_error_from_errno("fclose");
6180 got_object_commit_close(commit);
6185 histedit_free_list(struct got_histedit_list *histedit_cmds)
6187 struct got_histedit_list_entry *hle;
6189 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6190 TAILQ_REMOVE(histedit_cmds, hle, entry);
6195 static const struct got_error *
6196 histedit_load_list(struct got_histedit_list *histedit_cmds,
6197 const char *path, struct got_repository *repo)
6199 const struct got_error *err = NULL;
6202 f = fopen(path, "r");
6204 err = got_error_from_errno2("fopen", path);
6208 err = histedit_parse_list(histedit_cmds, f, repo);
6210 if (f && fclose(f) != 0 && err == NULL)
6211 err = got_error_from_errno("fclose");
6215 static const struct got_error *
6216 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6217 const struct got_error *edit_err, struct got_object_id_queue *commits,
6218 const char *path, struct got_repository *repo)
6220 const struct got_error *err = NULL, *prev_err = edit_err;
6223 while (resp != 'c' && resp != 'r' && resp != 'a') {
6224 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6225 "or (a)bort: ", getprogname(), prev_err->msg);
6230 histedit_free_list(histedit_cmds);
6231 err = histedit_run_editor(histedit_cmds, path, commits,
6234 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6235 err->code != GOT_ERR_HISTEDIT_CMD)
6242 } else if (resp == 'r') {
6243 histedit_free_list(histedit_cmds);
6244 err = histedit_edit_script(histedit_cmds,
6247 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6248 err->code != GOT_ERR_HISTEDIT_CMD)
6255 } else if (resp == 'a') {
6256 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6259 printf("invalid response '%c'\n", resp);
6265 static const struct got_error *
6266 histedit_complete(struct got_worktree *worktree,
6267 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6268 struct got_reference *branch, struct got_repository *repo)
6270 printf("Switching work tree to %s\n",
6271 got_ref_get_symref_target(branch));
6272 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6276 static const struct got_error *
6277 show_histedit_progress(struct got_commit_object *commit,
6278 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6280 const struct got_error *err;
6281 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6283 err = got_object_id_str(&old_id_str, hle->commit_id);
6288 err = got_object_id_str(&new_id_str, new_id);
6293 old_id_str[12] = '\0';
6295 new_id_str[12] = '\0';
6298 logmsg = strdup(hle->logmsg);
6299 if (logmsg == NULL) {
6300 err = got_error_from_errno("strdup");
6303 trim_logmsg(logmsg, 42);
6305 err = get_short_logmsg(&logmsg, 42, commit);
6310 switch (hle->cmd->code) {
6311 case GOT_HISTEDIT_PICK:
6312 case GOT_HISTEDIT_EDIT:
6313 printf("%s -> %s: %s\n", old_id_str,
6314 new_id_str ? new_id_str : "no-op change", logmsg);
6316 case GOT_HISTEDIT_DROP:
6317 case GOT_HISTEDIT_FOLD:
6318 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6331 static const struct got_error *
6332 histedit_commit(struct got_pathlist_head *merged_paths,
6333 struct got_worktree *worktree, struct got_fileindex *fileindex,
6334 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6335 struct got_repository *repo)
6337 const struct got_error *err;
6338 struct got_commit_object *commit;
6339 struct got_object_id *new_commit_id;
6341 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6342 && hle->logmsg == NULL) {
6343 err = histedit_edit_logmsg(hle, repo);
6348 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6352 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6353 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6356 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6358 err = show_histedit_progress(commit, hle, NULL);
6360 err = show_histedit_progress(commit, hle, new_commit_id);
6361 free(new_commit_id);
6364 got_object_commit_close(commit);
6368 static const struct got_error *
6369 histedit_skip_commit(struct got_histedit_list_entry *hle,
6370 struct got_worktree *worktree, struct got_repository *repo)
6372 const struct got_error *error;
6373 struct got_commit_object *commit;
6375 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6380 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6384 error = show_histedit_progress(commit, hle, NULL);
6385 got_object_commit_close(commit);
6389 static const struct got_error *
6390 cmd_histedit(int argc, char *argv[])
6392 const struct got_error *error = NULL;
6393 struct got_worktree *worktree = NULL;
6394 struct got_fileindex *fileindex = NULL;
6395 struct got_repository *repo = NULL;
6397 struct got_reference *branch = NULL;
6398 struct got_reference *tmp_branch = NULL;
6399 struct got_object_id *resume_commit_id = NULL;
6400 struct got_object_id *base_commit_id = NULL;
6401 struct got_object_id *head_commit_id = NULL;
6402 struct got_commit_object *commit = NULL;
6403 int ch, rebase_in_progress = 0, did_something;
6404 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6405 const char *edit_script_path = NULL;
6406 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6407 struct got_object_id_queue commits;
6408 struct got_pathlist_head merged_paths;
6409 const struct got_object_id_queue *parent_ids;
6410 struct got_object_qid *pid;
6411 struct got_histedit_list histedit_cmds;
6412 struct got_histedit_list_entry *hle;
6414 SIMPLEQ_INIT(&commits);
6415 TAILQ_INIT(&histedit_cmds);
6416 TAILQ_INIT(&merged_paths);
6418 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6427 edit_script_path = optarg;
6439 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6440 "unveil", NULL) == -1)
6443 if (abort_edit && continue_edit)
6449 * This command cannot apply unveil(2) in all cases because the
6450 * user may choose to run an editor to edit the histedit script
6451 * and to edit individual commit log messages.
6452 * unveil(2) traverses exec(2); if an editor is used we have to
6453 * apply unveil after edit script and log messages have been written.
6454 * XXX TODO: Make use of unveil(2) where possible.
6457 cwd = getcwd(NULL, 0);
6459 error = got_error_from_errno("getcwd");
6462 error = got_worktree_open(&worktree, cwd);
6466 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6471 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6474 if (rebase_in_progress) {
6475 error = got_error(GOT_ERR_REBASING);
6479 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6483 if (edit_in_progress && abort_edit) {
6484 error = got_worktree_histedit_continue(&resume_commit_id,
6485 &tmp_branch, &branch, &base_commit_id, &fileindex,
6489 printf("Switching work tree to %s\n",
6490 got_ref_get_symref_target(branch));
6491 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6492 branch, base_commit_id, update_progress, &did_something);
6495 printf("Histedit of %s aborted\n",
6496 got_ref_get_symref_target(branch));
6497 goto done; /* nothing else to do */
6498 } else if (abort_edit) {
6499 error = got_error(GOT_ERR_NOT_HISTEDIT);
6503 if (continue_edit) {
6506 if (!edit_in_progress) {
6507 error = got_error(GOT_ERR_NOT_HISTEDIT);
6511 error = got_worktree_get_histedit_script_path(&path, worktree);
6515 error = histedit_load_list(&histedit_cmds, path, repo);
6520 error = got_worktree_histedit_continue(&resume_commit_id,
6521 &tmp_branch, &branch, &base_commit_id, &fileindex,
6526 error = got_ref_resolve(&head_commit_id, repo, branch);
6530 error = got_object_open_as_commit(&commit, repo,
6534 parent_ids = got_object_commit_get_parent_ids(commit);
6535 pid = SIMPLEQ_FIRST(parent_ids);
6537 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6540 error = collect_commits(&commits, head_commit_id, pid->id,
6541 base_commit_id, got_worktree_get_path_prefix(worktree),
6542 GOT_ERR_HISTEDIT_PATH, repo);
6543 got_object_commit_close(commit);
6548 if (edit_in_progress) {
6549 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6553 error = got_ref_open(&branch, repo,
6554 got_worktree_get_head_ref_name(worktree), 0);
6558 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6559 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6560 "will not edit commit history of a branch outside "
6561 "the \"refs/heads/\" reference namespace");
6565 error = got_ref_resolve(&head_commit_id, repo, branch);
6566 got_ref_close(branch);
6571 error = got_object_open_as_commit(&commit, repo,
6575 parent_ids = got_object_commit_get_parent_ids(commit);
6576 pid = SIMPLEQ_FIRST(parent_ids);
6578 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6581 error = collect_commits(&commits, head_commit_id, pid->id,
6582 got_worktree_get_base_commit_id(worktree),
6583 got_worktree_get_path_prefix(worktree),
6584 GOT_ERR_HISTEDIT_PATH, repo);
6585 got_object_commit_close(commit);
6590 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6591 &base_commit_id, &fileindex, worktree, repo);
6595 if (edit_script_path) {
6596 error = histedit_load_list(&histedit_cmds,
6597 edit_script_path, repo);
6599 got_worktree_histedit_abort(worktree, fileindex,
6600 repo, branch, base_commit_id,
6601 update_progress, &did_something);
6605 error = histedit_edit_script(&histedit_cmds, &commits,
6608 got_worktree_histedit_abort(worktree, fileindex,
6609 repo, branch, base_commit_id,
6610 update_progress, &did_something);
6616 error = histedit_save_list(&histedit_cmds, worktree,
6619 got_worktree_histedit_abort(worktree, fileindex,
6620 repo, branch, base_commit_id,
6621 update_progress, &did_something);
6627 error = histedit_check_script(&histedit_cmds, &commits, repo);
6631 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6632 if (resume_commit_id) {
6633 if (got_object_id_cmp(hle->commit_id,
6634 resume_commit_id) != 0)
6637 resume_commit_id = NULL;
6638 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6639 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6640 error = histedit_skip_commit(hle, worktree,
6643 error = histedit_commit(NULL, worktree,
6644 fileindex, tmp_branch, hle, repo);
6651 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6652 error = histedit_skip_commit(hle, worktree, repo);
6658 error = got_object_open_as_commit(&commit, repo,
6662 parent_ids = got_object_commit_get_parent_ids(commit);
6663 pid = SIMPLEQ_FIRST(parent_ids);
6665 error = got_worktree_histedit_merge_files(&merged_paths,
6666 worktree, fileindex, pid->id, hle->commit_id, repo,
6667 rebase_progress, &rebase_status, check_cancelled, NULL);
6670 got_object_commit_close(commit);
6673 if (rebase_status == GOT_STATUS_CONFLICT) {
6674 got_worktree_rebase_pathlist_free(&merged_paths);
6678 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6680 error = got_object_id_str(&id_str, hle->commit_id);
6683 printf("Stopping histedit for amending commit %s\n",
6686 got_worktree_rebase_pathlist_free(&merged_paths);
6687 error = got_worktree_histedit_postpone(worktree,
6692 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6693 error = histedit_skip_commit(hle, worktree, repo);
6699 error = histedit_commit(&merged_paths, worktree, fileindex,
6700 tmp_branch, hle, repo);
6701 got_worktree_rebase_pathlist_free(&merged_paths);
6706 if (rebase_status == GOT_STATUS_CONFLICT) {
6707 error = got_worktree_histedit_postpone(worktree, fileindex);
6710 error = got_error_msg(GOT_ERR_CONFLICTS,
6711 "conflicts must be resolved before rebasing can continue");
6713 error = histedit_complete(worktree, fileindex, tmp_branch,
6716 got_object_id_queue_free(&commits);
6717 histedit_free_list(&histedit_cmds);
6718 free(head_commit_id);
6719 free(base_commit_id);
6720 free(resume_commit_id);
6722 got_object_commit_close(commit);
6724 got_ref_close(branch);
6726 got_ref_close(tmp_branch);
6728 got_worktree_close(worktree);
6730 got_repo_close(repo);
6735 usage_integrate(void)
6737 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6741 static const struct got_error *
6742 cmd_integrate(int argc, char *argv[])
6744 const struct got_error *error = NULL;
6745 struct got_repository *repo = NULL;
6746 struct got_worktree *worktree = NULL;
6747 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6748 const char *branch_arg = NULL;
6749 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6750 struct got_fileindex *fileindex = NULL;
6751 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6752 int ch, did_something = 0;
6754 while ((ch = getopt(argc, argv, "")) != -1) {
6767 branch_arg = argv[0];
6769 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6770 "unveil", NULL) == -1)
6773 cwd = getcwd(NULL, 0);
6775 error = got_error_from_errno("getcwd");
6779 error = got_worktree_open(&worktree, cwd);
6783 error = check_rebase_or_histedit_in_progress(worktree);
6787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6792 error = apply_unveil(got_repo_get_path(repo), 0,
6793 got_worktree_get_root_path(worktree));
6797 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6798 error = got_error_from_errno("asprintf");
6802 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6803 &base_branch_ref, worktree, refname, repo);
6807 refname = strdup(got_ref_get_name(branch_ref));
6808 if (refname == NULL) {
6809 error = got_error_from_errno("strdup");
6810 got_worktree_integrate_abort(worktree, fileindex, repo,
6811 branch_ref, base_branch_ref);
6814 base_refname = strdup(got_ref_get_name(base_branch_ref));
6815 if (base_refname == NULL) {
6816 error = got_error_from_errno("strdup");
6817 got_worktree_integrate_abort(worktree, fileindex, repo,
6818 branch_ref, base_branch_ref);
6822 error = got_ref_resolve(&commit_id, repo, branch_ref);
6826 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6830 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6831 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6832 "specified branch has already been integrated");
6833 got_worktree_integrate_abort(worktree, fileindex, repo,
6834 branch_ref, base_branch_ref);
6838 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6840 if (error->code == GOT_ERR_ANCESTRY)
6841 error = got_error(GOT_ERR_REBASE_REQUIRED);
6842 got_worktree_integrate_abort(worktree, fileindex, repo,
6843 branch_ref, base_branch_ref);
6847 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6848 branch_ref, base_branch_ref, update_progress, &did_something,
6849 check_cancelled, NULL);
6853 printf("Integrated %s into %s\n", refname, base_refname);
6856 got_repo_close(repo);
6858 got_worktree_close(worktree);
6860 free(base_commit_id);
6870 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6871 "[file-path ...]\n",
6876 static const struct got_error *
6877 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6878 const char *path, struct got_object_id *blob_id,
6879 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6880 int dirfd, const char *de_name)
6882 const struct got_error *err = NULL;
6883 char *id_str = NULL;
6885 if (staged_status != GOT_STATUS_ADD &&
6886 staged_status != GOT_STATUS_MODIFY &&
6887 staged_status != GOT_STATUS_DELETE)
6890 if (staged_status == GOT_STATUS_ADD ||
6891 staged_status == GOT_STATUS_MODIFY)
6892 err = got_object_id_str(&id_str, staged_blob_id);
6894 err = got_object_id_str(&id_str, blob_id);
6898 printf("%s %c %s\n", id_str, staged_status, path);
6903 static const struct got_error *
6904 cmd_stage(int argc, char *argv[])
6906 const struct got_error *error = NULL;
6907 struct got_repository *repo = NULL;
6908 struct got_worktree *worktree = NULL;
6910 struct got_pathlist_head paths;
6911 struct got_pathlist_entry *pe;
6912 int ch, list_stage = 0, pflag = 0;
6913 FILE *patch_script_file = NULL;
6914 const char *patch_script_path = NULL;
6915 struct choose_patch_arg cpa;
6919 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6928 patch_script_path = optarg;
6940 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6941 "unveil", NULL) == -1)
6944 if (list_stage && (pflag || patch_script_path))
6945 errx(1, "-l option cannot be used with other options");
6946 if (patch_script_path && !pflag)
6947 errx(1, "-F option can only be used together with -p option");
6949 cwd = getcwd(NULL, 0);
6951 error = got_error_from_errno("getcwd");
6955 error = got_worktree_open(&worktree, cwd);
6959 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6964 if (patch_script_path) {
6965 patch_script_file = fopen(patch_script_path, "r");
6966 if (patch_script_file == NULL) {
6967 error = got_error_from_errno2("fopen",
6972 error = apply_unveil(got_repo_get_path(repo), 0,
6973 got_worktree_get_root_path(worktree));
6977 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6982 error = got_worktree_status(worktree, &paths, repo,
6983 print_stage, NULL, check_cancelled, NULL);
6985 cpa.patch_script_file = patch_script_file;
6986 cpa.action = "stage";
6987 error = got_worktree_stage(worktree, &paths,
6988 pflag ? NULL : print_status, NULL,
6989 pflag ? choose_patch : NULL, &cpa, repo);
6992 if (patch_script_file && fclose(patch_script_file) == EOF &&
6994 error = got_error_from_errno2("fclose", patch_script_path);
6996 got_repo_close(repo);
6998 got_worktree_close(worktree);
6999 TAILQ_FOREACH(pe, &paths, entry)
7000 free((char *)pe->path);
7001 got_pathlist_free(&paths);
7009 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7010 "[file-path ...]\n",
7016 static const struct got_error *
7017 cmd_unstage(int argc, char *argv[])
7019 const struct got_error *error = NULL;
7020 struct got_repository *repo = NULL;
7021 struct got_worktree *worktree = NULL;
7023 struct got_pathlist_head paths;
7024 struct got_pathlist_entry *pe;
7025 int ch, did_something = 0, pflag = 0;
7026 FILE *patch_script_file = NULL;
7027 const char *patch_script_path = NULL;
7028 struct choose_patch_arg cpa;
7032 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7038 patch_script_path = optarg;
7050 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7051 "unveil", NULL) == -1)
7054 if (patch_script_path && !pflag)
7055 errx(1, "-F option can only be used together with -p option");
7057 cwd = getcwd(NULL, 0);
7059 error = got_error_from_errno("getcwd");
7063 error = got_worktree_open(&worktree, cwd);
7067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7072 if (patch_script_path) {
7073 patch_script_file = fopen(patch_script_path, "r");
7074 if (patch_script_file == NULL) {
7075 error = got_error_from_errno2("fopen",
7081 error = apply_unveil(got_repo_get_path(repo), 0,
7082 got_worktree_get_root_path(worktree));
7086 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7090 cpa.patch_script_file = patch_script_file;
7091 cpa.action = "unstage";
7092 error = got_worktree_unstage(worktree, &paths, update_progress,
7093 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7095 if (patch_script_file && fclose(patch_script_file) == EOF &&
7097 error = got_error_from_errno2("fclose", patch_script_path);
7099 got_repo_close(repo);
7101 got_worktree_close(worktree);
7102 TAILQ_FOREACH(pe, &paths, entry)
7103 free((char *)pe->path);
7104 got_pathlist_free(&paths);
7112 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7113 "arg1 [arg2 ...]\n", getprogname());
7117 static const struct got_error *
7118 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7120 const struct got_error *err;
7121 struct got_blob_object *blob;
7123 err = got_object_open_as_blob(&blob, repo, id, 8192);
7127 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7128 got_object_blob_close(blob);
7132 static const struct got_error *
7133 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7135 const struct got_error *err;
7136 struct got_tree_object *tree;
7139 err = got_object_open_as_tree(&tree, repo, id);
7143 nentries = got_object_tree_get_nentries(tree);
7144 for (i = 0; i < nentries; i++) {
7145 struct got_tree_entry *te;
7147 if (sigint_received || sigpipe_received)
7149 te = got_object_tree_get_entry(tree, i);
7150 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7153 fprintf(outfile, "%s %.7o %s\n", id_str,
7154 got_tree_entry_get_mode(te),
7155 got_tree_entry_get_name(te));
7159 got_object_tree_close(tree);
7163 static const struct got_error *
7164 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7166 const struct got_error *err;
7167 struct got_commit_object *commit;
7168 const struct got_object_id_queue *parent_ids;
7169 struct got_object_qid *pid;
7170 char *id_str = NULL;
7171 const char *logmsg = NULL;
7173 err = got_object_open_as_commit(&commit, repo, id);
7177 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7181 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7182 parent_ids = got_object_commit_get_parent_ids(commit);
7183 fprintf(outfile, "numparents %d\n",
7184 got_object_commit_get_nparents(commit));
7185 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7187 err = got_object_id_str(&pid_str, pid->id);
7190 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7193 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7194 got_object_commit_get_author(commit),
7195 got_object_commit_get_author_time(commit));
7197 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7198 got_object_commit_get_author(commit),
7199 got_object_commit_get_committer_time(commit));
7201 logmsg = got_object_commit_get_logmsg_raw(commit);
7202 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7203 fprintf(outfile, "%s", logmsg);
7206 got_object_commit_close(commit);
7210 static const struct got_error *
7211 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7213 const struct got_error *err;
7214 struct got_tag_object *tag;
7215 char *id_str = NULL;
7216 const char *tagmsg = NULL;
7218 err = got_object_open_as_tag(&tag, repo, id);
7222 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7226 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7228 switch (got_object_tag_get_object_type(tag)) {
7229 case GOT_OBJ_TYPE_BLOB:
7230 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7231 GOT_OBJ_LABEL_BLOB);
7233 case GOT_OBJ_TYPE_TREE:
7234 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7235 GOT_OBJ_LABEL_TREE);
7237 case GOT_OBJ_TYPE_COMMIT:
7238 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7239 GOT_OBJ_LABEL_COMMIT);
7241 case GOT_OBJ_TYPE_TAG:
7242 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7249 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7250 got_object_tag_get_name(tag));
7252 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7253 got_object_tag_get_tagger(tag),
7254 got_object_tag_get_tagger_time(tag));
7256 tagmsg = got_object_tag_get_message(tag);
7257 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7258 fprintf(outfile, "%s", tagmsg);
7261 got_object_tag_close(tag);
7265 static const struct got_error *
7266 cmd_cat(int argc, char *argv[])
7268 const struct got_error *error;
7269 struct got_repository *repo = NULL;
7270 struct got_worktree *worktree = NULL;
7271 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7272 const char *commit_id_str = NULL;
7273 struct got_object_id *id = NULL, *commit_id = NULL;
7274 int ch, obj_type, i, force_path = 0;
7277 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7282 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7285 commit_id_str = optarg;
7288 repo_path = realpath(optarg, NULL);
7289 if (repo_path == NULL)
7290 return got_error_from_errno2("realpath",
7292 got_path_strip_trailing_slashes(repo_path);
7306 cwd = getcwd(NULL, 0);
7308 error = got_error_from_errno("getcwd");
7311 error = got_worktree_open(&worktree, cwd);
7312 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7315 if (repo_path == NULL) {
7317 got_worktree_get_repo_path(worktree));
7318 if (repo_path == NULL) {
7319 error = got_error_from_errno("strdup");
7325 if (repo_path == NULL) {
7326 repo_path = getcwd(NULL, 0);
7327 if (repo_path == NULL)
7328 return got_error_from_errno("getcwd");
7331 error = got_repo_open(&repo, repo_path, NULL);
7336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7340 if (commit_id_str == NULL)
7341 commit_id_str = GOT_REF_HEAD;
7342 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7346 for (i = 0; i < argc; i++) {
7348 error = got_object_id_by_path(&id, repo, commit_id,
7353 error = match_object_id(&id, &label, argv[i],
7354 GOT_OBJ_TYPE_ANY, 0, repo);
7356 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7357 error->code != GOT_ERR_NOT_REF)
7359 error = got_object_id_by_path(&id, repo,
7360 commit_id, argv[i]);
7366 error = got_object_get_type(&obj_type, repo, id);
7371 case GOT_OBJ_TYPE_BLOB:
7372 error = cat_blob(id, repo, stdout);
7374 case GOT_OBJ_TYPE_TREE:
7375 error = cat_tree(id, repo, stdout);
7377 case GOT_OBJ_TYPE_COMMIT:
7378 error = cat_commit(id, repo, stdout);
7380 case GOT_OBJ_TYPE_TAG:
7381 error = cat_tag(id, repo, stdout);
7384 error = got_error(GOT_ERR_OBJ_TYPE);
7400 got_worktree_close(worktree);
7402 const struct got_error *repo_error;
7403 repo_error = got_repo_close(repo);