2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 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, "/", 1);
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, path, first_parent_traversal);
1773 err = got_commit_graph_iter_start(graph, root_id, repo,
1774 check_cancelled, NULL);
1778 struct got_commit_object *commit;
1779 struct got_object_id *id;
1781 if (sigint_received || sigpipe_received)
1784 err = got_commit_graph_iter_next(&id, graph, repo,
1785 check_cancelled, NULL);
1787 if (err->code == GOT_ERR_ITER_COMPLETED)
1794 err = got_object_open_as_commit(&commit, repo, id);
1798 if (search_pattern) {
1799 err = match_logmsg(&have_match, id, commit, ®ex);
1801 got_object_commit_close(commit);
1804 if (have_match == 0) {
1805 got_object_commit_close(commit);
1810 err = print_commit(commit, id, repo, path, show_patch,
1811 diff_context, refs);
1812 got_object_commit_close(commit);
1813 if (err || (limit && --limit == 0))
1819 got_commit_graph_close(graph);
1826 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1827 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1832 get_default_log_limit(void)
1834 const char *got_default_log_limit;
1838 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1839 if (got_default_log_limit == NULL)
1841 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1847 static const struct got_error *
1848 cmd_log(int argc, char *argv[])
1850 const struct got_error *error;
1851 struct got_repository *repo = NULL;
1852 struct got_worktree *worktree = NULL;
1853 struct got_commit_object *commit = NULL;
1854 struct got_object_id *id = NULL;
1855 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1856 const char *start_commit = NULL, *search_pattern = NULL;
1857 int diff_context = -1, ch;
1858 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1860 struct got_reflist_head refs;
1862 SIMPLEQ_INIT(&refs);
1865 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1871 limit = get_default_log_limit();
1873 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1879 start_commit = optarg;
1882 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1885 err(1, "-C option %s", errstr);
1888 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1890 err(1, "-l option %s", errstr);
1893 first_parent_traversal = 1;
1896 repo_path = realpath(optarg, NULL);
1897 if (repo_path == NULL)
1898 return got_error_from_errno2("realpath",
1900 got_path_strip_trailing_slashes(repo_path);
1903 search_pattern = optarg;
1914 if (diff_context == -1)
1916 else if (!show_patch)
1917 errx(1, "-C reguires -p");
1919 cwd = getcwd(NULL, 0);
1921 error = got_error_from_errno("getcwd");
1925 error = got_worktree_open(&worktree, cwd);
1926 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1933 error = got_error_from_errno("strdup");
1936 } else if (argc == 1) {
1938 error = got_worktree_resolve_path(&path, worktree,
1943 path = strdup(argv[0]);
1945 error = got_error_from_errno("strdup");
1952 if (repo_path == NULL) {
1953 repo_path = worktree ?
1954 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1956 if (repo_path == NULL) {
1957 error = got_error_from_errno("strdup");
1961 error = got_repo_open(&repo, repo_path, NULL);
1965 error = apply_unveil(got_repo_get_path(repo), 1,
1966 worktree ? got_worktree_get_root_path(worktree) : NULL);
1970 if (start_commit == NULL) {
1971 struct got_reference *head_ref;
1972 error = got_ref_open(&head_ref, repo,
1973 worktree ? got_worktree_get_head_ref_name(worktree)
1977 error = got_ref_resolve(&id, repo, head_ref);
1978 got_ref_close(head_ref);
1981 error = got_object_open_as_commit(&commit, repo, id);
1983 struct got_reference *ref;
1984 error = got_ref_open(&ref, repo, start_commit, 0);
1985 if (error == NULL) {
1987 error = got_ref_resolve(&id, repo, ref);
1991 error = got_object_get_type(&obj_type, repo, id);
1994 if (obj_type == GOT_OBJ_TYPE_TAG) {
1995 struct got_tag_object *tag;
1996 error = got_object_open_as_tag(&tag, repo, id);
1999 if (got_object_tag_get_object_type(tag) !=
2000 GOT_OBJ_TYPE_COMMIT) {
2001 got_object_tag_close(tag);
2002 error = got_error(GOT_ERR_OBJ_TYPE);
2006 id = got_object_id_dup(
2007 got_object_tag_get_object_id(tag));
2009 error = got_error_from_errno(
2010 "got_object_id_dup");
2011 got_object_tag_close(tag);
2014 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2015 error = got_error(GOT_ERR_OBJ_TYPE);
2018 error = got_object_open_as_commit(&commit, repo, id);
2022 if (commit == NULL) {
2023 error = got_repo_match_object_id_prefix(&id,
2024 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2033 const char *prefix = got_worktree_get_path_prefix(worktree);
2035 if (asprintf(&p, "%s%s%s", prefix,
2036 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2037 error = got_error_from_errno("asprintf");
2040 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2043 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2048 path = in_repo_path;
2051 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2055 error = print_commits(id, repo, path, show_patch, search_pattern,
2056 diff_context, limit, first_parent_traversal, &refs);
2063 got_worktree_close(worktree);
2065 const struct got_error *repo_error;
2066 repo_error = got_repo_close(repo);
2070 got_ref_list_free(&refs);
2077 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2078 "[-w] [object1 object2 | path]\n", getprogname());
2082 struct print_diff_arg {
2083 struct got_repository *repo;
2084 struct got_worktree *worktree;
2089 int ignore_whitespace;
2092 static const struct got_error *
2093 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2094 const char *path, struct got_object_id *blob_id,
2095 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2096 int dirfd, const char *de_name)
2098 struct print_diff_arg *a = arg;
2099 const struct got_error *err = NULL;
2100 struct got_blob_object *blob1 = NULL;
2103 char *abspath = NULL, *label1 = NULL;
2106 if (a->diff_staged) {
2107 if (staged_status != GOT_STATUS_MODIFY &&
2108 staged_status != GOT_STATUS_ADD &&
2109 staged_status != GOT_STATUS_DELETE)
2112 if (staged_status == GOT_STATUS_DELETE)
2114 if (status == GOT_STATUS_NONEXISTENT)
2115 return got_error_set_errno(ENOENT, path);
2116 if (status != GOT_STATUS_MODIFY &&
2117 status != GOT_STATUS_ADD &&
2118 status != GOT_STATUS_DELETE &&
2119 status != GOT_STATUS_CONFLICT)
2123 if (!a->header_shown) {
2124 printf("diff %s %s%s\n", a->id_str,
2125 got_worktree_get_root_path(a->worktree),
2126 a->diff_staged ? " (staged changes)" : "");
2127 a->header_shown = 1;
2130 if (a->diff_staged) {
2131 const char *label1 = NULL, *label2 = NULL;
2132 switch (staged_status) {
2133 case GOT_STATUS_MODIFY:
2137 case GOT_STATUS_ADD:
2140 case GOT_STATUS_DELETE:
2144 return got_error(GOT_ERR_FILE_STATUS);
2146 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2147 label1, label2, a->diff_context, a->ignore_whitespace,
2151 if (staged_status == GOT_STATUS_ADD ||
2152 staged_status == GOT_STATUS_MODIFY) {
2154 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2158 err = got_object_id_str(&id_str, staged_blob_id);
2161 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2162 err = got_error_from_errno("asprintf");
2167 } else if (status != GOT_STATUS_ADD) {
2168 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2173 if (status != GOT_STATUS_DELETE) {
2174 if (asprintf(&abspath, "%s/%s",
2175 got_worktree_get_root_path(a->worktree), path) == -1) {
2176 err = got_error_from_errno("asprintf");
2181 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2183 err = got_error_from_errno2("openat", abspath);
2187 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2189 err = got_error_from_errno2("open", abspath);
2193 if (fstat(fd, &sb) == -1) {
2194 err = got_error_from_errno2("fstat", abspath);
2197 f2 = fdopen(fd, "r");
2199 err = got_error_from_errno2("fdopen", abspath);
2206 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2207 a->diff_context, a->ignore_whitespace, stdout);
2210 got_object_blob_close(blob1);
2211 if (f2 && fclose(f2) == EOF && err == NULL)
2212 err = got_error_from_errno("fclose");
2213 if (fd != -1 && close(fd) == -1 && err == NULL)
2214 err = got_error_from_errno("close");
2219 static const struct got_error *
2220 match_object_id(struct got_object_id **id, char **label,
2221 const char *id_str, int obj_type, int resolve_tags,
2222 struct got_repository *repo)
2224 const struct got_error *err;
2225 struct got_tag_object *tag;
2226 struct got_reference *ref = NULL;
2232 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2235 *id = got_object_id_dup(
2236 got_object_tag_get_object_id(tag));
2238 err = got_error_from_errno("got_object_id_dup");
2239 else if (asprintf(label, "refs/tags/%s",
2240 got_object_tag_get_name(tag)) == -1) {
2241 err = got_error_from_errno("asprintf");
2245 got_object_tag_close(tag);
2247 } else if (err->code != GOT_ERR_NO_OBJ)
2251 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2253 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2255 err = got_ref_open(&ref, repo, id_str, 0);
2258 *label = strdup(got_ref_get_name(ref));
2259 if (*label == NULL) {
2260 err = got_error_from_errno("strdup");
2263 err = got_ref_resolve(id, repo, ref);
2265 err = got_object_id_str(label, *id);
2266 if (*label == NULL) {
2267 err = got_error_from_errno("strdup");
2278 static const struct got_error *
2279 cmd_diff(int argc, char *argv[])
2281 const struct got_error *error;
2282 struct got_repository *repo = NULL;
2283 struct got_worktree *worktree = NULL;
2284 char *cwd = NULL, *repo_path = NULL;
2285 struct got_object_id *id1 = NULL, *id2 = NULL;
2286 const char *id_str1 = NULL, *id_str2 = NULL;
2287 char *label1 = NULL, *label2 = NULL;
2289 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2299 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2302 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2305 err(1, "-C option %s", errstr);
2308 repo_path = realpath(optarg, NULL);
2309 if (repo_path == NULL)
2310 return got_error_from_errno2("realpath",
2312 got_path_strip_trailing_slashes(repo_path);
2318 ignore_whitespace = 1;
2329 cwd = getcwd(NULL, 0);
2331 error = got_error_from_errno("getcwd");
2334 error = got_worktree_open(&worktree, cwd);
2335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2338 if (worktree == NULL) {
2339 error = got_error(GOT_ERR_NOT_WORKTREE);
2344 "-r option can't be used when diffing a work tree");
2345 repo_path = strdup(got_worktree_get_repo_path(worktree));
2346 if (repo_path == NULL) {
2347 error = got_error_from_errno("strdup");
2351 error = got_worktree_resolve_path(&path, worktree,
2358 error = got_error_from_errno("strdup");
2362 } else if (argc == 2) {
2364 errx(1, "-s option can't be used when diffing "
2365 "objects in repository");
2368 if (worktree && repo_path == NULL) {
2370 strdup(got_worktree_get_repo_path(worktree));
2371 if (repo_path == NULL) {
2372 error = got_error_from_errno("strdup");
2379 if (repo_path == NULL) {
2380 repo_path = getcwd(NULL, 0);
2381 if (repo_path == NULL)
2382 return got_error_from_errno("getcwd");
2385 error = got_repo_open(&repo, repo_path, NULL);
2390 error = apply_unveil(got_repo_get_path(repo), 1,
2391 worktree ? got_worktree_get_root_path(worktree) : NULL);
2396 struct print_diff_arg arg;
2397 struct got_pathlist_head paths;
2402 error = got_object_id_str(&id_str,
2403 got_worktree_get_base_commit_id(worktree));
2407 arg.worktree = worktree;
2408 arg.diff_context = diff_context;
2409 arg.id_str = id_str;
2410 arg.header_shown = 0;
2411 arg.diff_staged = diff_staged;
2412 arg.ignore_whitespace = ignore_whitespace;
2414 error = got_pathlist_append(&paths, path, NULL);
2418 error = got_worktree_status(worktree, &paths, repo, print_diff,
2419 &arg, check_cancelled, NULL);
2421 got_pathlist_free(&paths);
2425 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2430 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2435 error = got_object_get_type(&type1, repo, id1);
2439 error = got_object_get_type(&type2, repo, id2);
2443 if (type1 != type2) {
2444 error = got_error(GOT_ERR_OBJ_TYPE);
2449 case GOT_OBJ_TYPE_BLOB:
2450 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2451 diff_context, ignore_whitespace, repo, stdout);
2453 case GOT_OBJ_TYPE_TREE:
2454 error = got_diff_objects_as_trees(id1, id2, "", "",
2455 diff_context, ignore_whitespace, repo, stdout);
2457 case GOT_OBJ_TYPE_COMMIT:
2458 printf("diff %s %s\n", label1, label2);
2459 error = got_diff_objects_as_commits(id1, id2, diff_context,
2460 ignore_whitespace, repo, stdout);
2463 error = got_error(GOT_ERR_OBJ_TYPE);
2473 got_worktree_close(worktree);
2475 const struct got_error *repo_error;
2476 repo_error = got_repo_close(repo);
2487 "usage: %s blame [-c commit] [-r repository-path] path\n",
2496 char datebuf[11]; /* YYYY-MM-DD + NUL */
2499 struct blame_cb_args {
2500 struct blame_line *lines;
2504 off_t *line_offsets;
2506 struct got_repository *repo;
2509 static const struct got_error *
2510 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2512 const struct got_error *err = NULL;
2513 struct blame_cb_args *a = arg;
2514 struct blame_line *bline;
2516 size_t linesize = 0;
2517 struct got_commit_object *commit = NULL;
2520 time_t committer_time;
2522 if (nlines != a->nlines ||
2523 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2524 return got_error(GOT_ERR_RANGE);
2526 if (sigint_received)
2527 return got_error(GOT_ERR_ITER_COMPLETED);
2530 return NULL; /* no change in this commit */
2532 /* Annotate this line. */
2533 bline = &a->lines[lineno - 1];
2534 if (bline->annotated)
2536 err = got_object_id_str(&bline->id_str, id);
2540 err = got_object_open_as_commit(&commit, a->repo, id);
2544 bline->committer = strdup(got_object_commit_get_committer(commit));
2545 if (bline->committer == NULL) {
2546 err = got_error_from_errno("strdup");
2550 committer_time = got_object_commit_get_committer_time(commit);
2551 if (localtime_r(&committer_time, &tm) == NULL)
2552 return got_error_from_errno("localtime_r");
2553 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2554 &tm) >= sizeof(bline->datebuf)) {
2555 err = got_error(GOT_ERR_NO_SPACE);
2558 bline->annotated = 1;
2560 /* Print lines annotated so far. */
2561 bline = &a->lines[a->lineno_cur - 1];
2562 if (!bline->annotated)
2565 offset = a->line_offsets[a->lineno_cur - 1];
2566 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2567 err = got_error_from_errno("fseeko");
2571 while (bline->annotated) {
2572 char *smallerthan, *at, *nl, *committer;
2575 if (getline(&line, &linesize, a->f) == -1) {
2577 err = got_error_from_errno("getline");
2581 committer = bline->committer;
2582 smallerthan = strchr(committer, '<');
2583 if (smallerthan && smallerthan[1] != '\0')
2584 committer = smallerthan + 1;
2585 at = strchr(committer, '@');
2588 len = strlen(committer);
2590 committer[8] = '\0';
2592 nl = strchr(line, '\n');
2595 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2596 bline->id_str, bline->datebuf, committer, line);
2599 bline = &a->lines[a->lineno_cur - 1];
2603 got_object_commit_close(commit);
2608 static const struct got_error *
2609 cmd_blame(int argc, char *argv[])
2611 const struct got_error *error;
2612 struct got_repository *repo = NULL;
2613 struct got_worktree *worktree = NULL;
2614 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2615 struct got_object_id *obj_id = NULL;
2616 struct got_object_id *commit_id = NULL;
2617 struct got_blob_object *blob = NULL;
2618 char *commit_id_str = NULL;
2619 struct blame_cb_args bca;
2620 int ch, obj_type, i;
2623 memset(&bca, 0, sizeof(bca));
2626 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2631 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2634 commit_id_str = optarg;
2637 repo_path = realpath(optarg, NULL);
2638 if (repo_path == NULL)
2639 return got_error_from_errno2("realpath",
2641 got_path_strip_trailing_slashes(repo_path);
2657 cwd = getcwd(NULL, 0);
2659 error = got_error_from_errno("getcwd");
2662 if (repo_path == NULL) {
2663 error = got_worktree_open(&worktree, cwd);
2664 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2670 strdup(got_worktree_get_repo_path(worktree));
2671 if (repo_path == NULL)
2672 error = got_error_from_errno("strdup");
2676 repo_path = strdup(cwd);
2677 if (repo_path == NULL) {
2678 error = got_error_from_errno("strdup");
2684 error = got_repo_open(&repo, repo_path, NULL);
2688 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2693 const char *prefix = got_worktree_get_path_prefix(worktree);
2694 char *p, *worktree_subdir = cwd +
2695 strlen(got_worktree_get_root_path(worktree));
2696 if (asprintf(&p, "%s%s%s%s%s",
2697 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2698 worktree_subdir, worktree_subdir[0] ? "/" : "",
2700 error = got_error_from_errno("asprintf");
2703 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2706 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2711 if (commit_id_str == NULL) {
2712 struct got_reference *head_ref;
2713 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2716 error = got_ref_resolve(&commit_id, repo, head_ref);
2717 got_ref_close(head_ref);
2721 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2726 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2729 if (obj_id == NULL) {
2730 error = got_error(GOT_ERR_NO_OBJ);
2734 error = got_object_get_type(&obj_type, repo, obj_id);
2738 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2739 error = got_error(GOT_ERR_OBJ_TYPE);
2743 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2746 bca.f = got_opentemp();
2747 if (bca.f == NULL) {
2748 error = got_error_from_errno("got_opentemp");
2751 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2752 &bca.line_offsets, bca.f, blob);
2753 if (error || bca.nlines == 0)
2756 /* Don't include \n at EOF in the blame line count. */
2757 if (bca.line_offsets[bca.nlines - 1] == filesize)
2760 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2761 if (bca.lines == NULL) {
2762 error = got_error_from_errno("calloc");
2766 bca.nlines_prec = 0;
2774 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2775 check_cancelled, NULL);
2785 got_object_blob_close(blob);
2787 got_worktree_close(worktree);
2789 const struct got_error *repo_error;
2790 repo_error = got_repo_close(repo);
2795 for (i = 0; i < bca.nlines; i++) {
2796 struct blame_line *bline = &bca.lines[i];
2797 free(bline->id_str);
2798 free(bline->committer);
2802 free(bca.line_offsets);
2803 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2804 error = got_error_from_errno("fclose");
2812 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2818 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2819 const char *root_path)
2821 int is_root_path = (strcmp(path, root_path) == 0);
2822 const char *modestr = "";
2823 mode_t mode = got_tree_entry_get_mode(te);
2825 path += strlen(root_path);
2826 while (path[0] == '/')
2829 if (got_object_tree_entry_is_submodule(te))
2831 else if (S_ISLNK(mode))
2833 else if (S_ISDIR(mode))
2835 else if (mode & S_IXUSR)
2838 printf("%s%s%s%s%s\n", id ? id : "", path,
2839 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2842 static const struct got_error *
2843 print_tree(const char *path, struct got_object_id *commit_id,
2844 int show_ids, int recurse, const char *root_path,
2845 struct got_repository *repo)
2847 const struct got_error *err = NULL;
2848 struct got_object_id *tree_id = NULL;
2849 struct got_tree_object *tree = NULL;
2852 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2856 err = got_object_open_as_tree(&tree, repo, tree_id);
2859 nentries = got_object_tree_get_nentries(tree);
2860 for (i = 0; i < nentries; i++) {
2861 struct got_tree_entry *te;
2864 if (sigint_received || sigpipe_received)
2867 te = got_object_tree_get_entry(tree, i);
2870 err = got_object_id_str(&id_str,
2871 got_tree_entry_get_id(te));
2874 if (asprintf(&id, "%s ", id_str) == -1) {
2875 err = got_error_from_errno("asprintf");
2881 print_entry(te, id, path, root_path);
2884 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2886 if (asprintf(&child_path, "%s%s%s", path,
2887 path[0] == '/' && path[1] == '\0' ? "" : "/",
2888 got_tree_entry_get_name(te)) == -1) {
2889 err = got_error_from_errno("asprintf");
2892 err = print_tree(child_path, commit_id, show_ids, 1,
2901 got_object_tree_close(tree);
2906 static const struct got_error *
2907 cmd_tree(int argc, char *argv[])
2909 const struct got_error *error;
2910 struct got_repository *repo = NULL;
2911 struct got_worktree *worktree = NULL;
2913 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2914 struct got_object_id *commit_id = NULL;
2915 char *commit_id_str = NULL;
2916 int show_ids = 0, recurse = 0;
2920 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2925 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2928 commit_id_str = optarg;
2931 repo_path = realpath(optarg, NULL);
2932 if (repo_path == NULL)
2933 return got_error_from_errno2("realpath",
2935 got_path_strip_trailing_slashes(repo_path);
2959 cwd = getcwd(NULL, 0);
2961 error = got_error_from_errno("getcwd");
2964 if (repo_path == NULL) {
2965 error = got_worktree_open(&worktree, cwd);
2966 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2972 strdup(got_worktree_get_repo_path(worktree));
2973 if (repo_path == NULL)
2974 error = got_error_from_errno("strdup");
2978 repo_path = strdup(cwd);
2979 if (repo_path == NULL) {
2980 error = got_error_from_errno("strdup");
2986 error = got_repo_open(&repo, repo_path, NULL);
2990 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2996 char *p, *worktree_subdir = cwd +
2997 strlen(got_worktree_get_root_path(worktree));
2998 if (asprintf(&p, "%s/%s",
2999 got_worktree_get_path_prefix(worktree),
3000 worktree_subdir) == -1) {
3001 error = got_error_from_errno("asprintf");
3004 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3011 if (in_repo_path == NULL) {
3012 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3017 if (commit_id_str == NULL) {
3018 struct got_reference *head_ref;
3019 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3022 error = got_ref_resolve(&commit_id, repo, head_ref);
3023 got_ref_close(head_ref);
3027 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3032 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3033 in_repo_path, repo);
3040 got_worktree_close(worktree);
3042 const struct got_error *repo_error;
3043 repo_error = got_repo_close(repo);
3053 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3057 static const struct got_error *
3058 print_status(void *arg, unsigned char status, unsigned char staged_status,
3059 const char *path, struct got_object_id *blob_id,
3060 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3061 int dirfd, const char *de_name)
3063 if (status == staged_status && (status == GOT_STATUS_DELETE))
3064 status = GOT_STATUS_NO_CHANGE;
3065 printf("%c%c %s\n", status, staged_status, path);
3069 static const struct got_error *
3070 cmd_status(int argc, char *argv[])
3072 const struct got_error *error = NULL;
3073 struct got_repository *repo = NULL;
3074 struct got_worktree *worktree = NULL;
3076 struct got_pathlist_head paths;
3077 struct got_pathlist_entry *pe;
3082 while ((ch = getopt(argc, argv, "")) != -1) {
3094 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3098 cwd = getcwd(NULL, 0);
3100 error = got_error_from_errno("getcwd");
3104 error = got_worktree_open(&worktree, cwd);
3108 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3113 error = apply_unveil(got_repo_get_path(repo), 1,
3114 got_worktree_get_root_path(worktree));
3118 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3122 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3123 check_cancelled, NULL);
3125 TAILQ_FOREACH(pe, &paths, entry)
3126 free((char *)pe->path);
3127 got_pathlist_free(&paths);
3136 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3141 static const struct got_error *
3142 list_refs(struct got_repository *repo)
3144 static const struct got_error *err = NULL;
3145 struct got_reflist_head refs;
3146 struct got_reflist_entry *re;
3148 SIMPLEQ_INIT(&refs);
3149 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3153 SIMPLEQ_FOREACH(re, &refs, entry) {
3155 refstr = got_ref_to_str(re->ref);
3157 return got_error_from_errno("got_ref_to_str");
3158 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3162 got_ref_list_free(&refs);
3166 static const struct got_error *
3167 delete_ref(struct got_repository *repo, const char *refname)
3169 const struct got_error *err = NULL;
3170 struct got_reference *ref;
3172 err = got_ref_open(&ref, repo, refname, 0);
3176 err = got_ref_delete(ref, repo);
3181 static const struct got_error *
3182 add_ref(struct got_repository *repo, const char *refname, const char *target)
3184 const struct got_error *err = NULL;
3185 struct got_object_id *id;
3186 struct got_reference *ref = NULL;
3189 * Don't let the user create a reference name with a leading '-'.
3190 * While technically a valid reference name, this case is usually
3191 * an unintended typo.
3193 if (refname[0] == '-')
3194 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3196 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3199 struct got_reference *target_ref;
3201 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3203 err = got_ref_open(&target_ref, repo, target, 0);
3206 err = got_ref_resolve(&id, repo, target_ref);
3207 got_ref_close(target_ref);
3212 err = got_ref_alloc(&ref, refname, id);
3216 err = got_ref_write(ref, repo);
3224 static const struct got_error *
3225 add_symref(struct got_repository *repo, const char *refname, const char *target)
3227 const struct got_error *err = NULL;
3228 struct got_reference *ref = NULL;
3229 struct got_reference *target_ref = NULL;
3232 * Don't let the user create a reference name with a leading '-'.
3233 * While technically a valid reference name, this case is usually
3234 * an unintended typo.
3236 if (refname[0] == '-')
3237 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3239 err = got_ref_open(&target_ref, repo, target, 0);
3243 err = got_ref_alloc_symref(&ref, refname, target_ref);
3247 err = got_ref_write(ref, repo);
3250 got_ref_close(target_ref);
3256 static const struct got_error *
3257 cmd_ref(int argc, char *argv[])
3259 const struct got_error *error = NULL;
3260 struct got_repository *repo = NULL;
3261 struct got_worktree *worktree = NULL;
3262 char *cwd = NULL, *repo_path = NULL;
3263 int ch, do_list = 0, create_symref = 0;
3264 const char *delref = NULL;
3266 /* TODO: Add -s option for adding symbolic references. */
3267 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3273 repo_path = realpath(optarg, NULL);
3274 if (repo_path == NULL)
3275 return got_error_from_errno2("realpath",
3277 got_path_strip_trailing_slashes(repo_path);
3291 if (do_list && delref)
3292 errx(1, "-l and -d options are mutually exclusive\n");
3297 if (do_list || delref) {
3299 errx(1, "-s option cannot be used together with the "
3300 "-l or -d options");
3303 } else if (argc != 2)
3308 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3312 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3313 "sendfd unveil", NULL) == -1)
3317 cwd = getcwd(NULL, 0);
3319 error = got_error_from_errno("getcwd");
3323 if (repo_path == NULL) {
3324 error = got_worktree_open(&worktree, cwd);
3325 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3331 strdup(got_worktree_get_repo_path(worktree));
3332 if (repo_path == NULL)
3333 error = got_error_from_errno("strdup");
3337 repo_path = strdup(cwd);
3338 if (repo_path == NULL) {
3339 error = got_error_from_errno("strdup");
3345 error = got_repo_open(&repo, repo_path, NULL);
3349 error = apply_unveil(got_repo_get_path(repo), do_list,
3350 worktree ? got_worktree_get_root_path(worktree) : NULL);
3355 error = list_refs(repo);
3357 error = delete_ref(repo, delref);
3358 else if (create_symref)
3359 error = add_symref(repo, argv[0], argv[1]);
3361 error = add_ref(repo, argv[0], argv[1]);
3364 got_repo_close(repo);
3366 got_worktree_close(worktree);
3376 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3377 "[name]\n", getprogname());
3381 static const struct got_error *
3382 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3383 struct got_reference *ref)
3385 const struct got_error *err = NULL;
3386 const char *refname, *marker = " ";
3389 refname = got_ref_get_name(ref);
3390 if (worktree && strcmp(refname,
3391 got_worktree_get_head_ref_name(worktree)) == 0) {
3392 struct got_object_id *id = NULL;
3394 err = got_ref_resolve(&id, repo, ref);
3397 if (got_object_id_cmp(id,
3398 got_worktree_get_base_commit_id(worktree)) == 0)
3405 if (strncmp(refname, "refs/heads/", 11) == 0)
3407 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3410 refstr = got_ref_to_str(ref);
3412 return got_error_from_errno("got_ref_to_str");
3414 printf("%s%s: %s\n", marker, refname, refstr);
3419 static const struct got_error *
3420 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3422 const char *refname;
3424 if (worktree == NULL)
3425 return got_error(GOT_ERR_NOT_WORKTREE);
3427 refname = got_worktree_get_head_ref_name(worktree);
3429 if (strncmp(refname, "refs/heads/", 11) == 0)
3431 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3434 printf("%s\n", refname);
3439 static const struct got_error *
3440 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3442 static const struct got_error *err = NULL;
3443 struct got_reflist_head refs;
3444 struct got_reflist_entry *re;
3445 struct got_reference *temp_ref = NULL;
3446 int rebase_in_progress, histedit_in_progress;
3448 SIMPLEQ_INIT(&refs);
3451 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3456 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3461 if (rebase_in_progress || histedit_in_progress) {
3462 err = got_ref_open(&temp_ref, repo,
3463 got_worktree_get_head_ref_name(worktree), 0);
3466 list_branch(repo, worktree, temp_ref);
3467 got_ref_close(temp_ref);
3471 err = got_ref_list(&refs, repo, "refs/heads",
3472 got_ref_cmp_by_name, NULL);
3476 SIMPLEQ_FOREACH(re, &refs, entry)
3477 list_branch(repo, worktree, re->ref);
3479 got_ref_list_free(&refs);
3483 static const struct got_error *
3484 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3485 const char *branch_name)
3487 const struct got_error *err = NULL;
3488 struct got_reference *ref = NULL;
3491 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3492 return got_error_from_errno("asprintf");
3494 err = got_ref_open(&ref, repo, refname, 0);
3499 strcmp(got_worktree_get_head_ref_name(worktree),
3500 got_ref_get_name(ref)) == 0) {
3501 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3502 "will not delete this work tree's current branch");
3506 err = got_ref_delete(ref, repo);
3514 static const struct got_error *
3515 add_branch(struct got_repository *repo, const char *branch_name,
3516 struct got_object_id *base_commit_id)
3518 const struct got_error *err = NULL;
3519 struct got_reference *ref = NULL;
3520 char *base_refname = NULL, *refname = NULL;
3523 * Don't let the user create a branch name with a leading '-'.
3524 * While technically a valid reference name, this case is usually
3525 * an unintended typo.
3527 if (branch_name[0] == '-')
3528 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3530 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3531 err = got_error_from_errno("asprintf");
3535 err = got_ref_open(&ref, repo, refname, 0);
3537 err = got_error(GOT_ERR_BRANCH_EXISTS);
3539 } else if (err->code != GOT_ERR_NOT_REF)
3542 err = got_ref_alloc(&ref, refname, base_commit_id);
3546 err = got_ref_write(ref, repo);
3555 static const struct got_error *
3556 cmd_branch(int argc, char *argv[])
3558 const struct got_error *error = NULL;
3559 struct got_repository *repo = NULL;
3560 struct got_worktree *worktree = NULL;
3561 char *cwd = NULL, *repo_path = NULL;
3562 int ch, do_list = 0, do_show = 0;
3563 const char *delref = NULL, *commit_id_arg = NULL;
3565 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3568 commit_id_arg = optarg;
3574 repo_path = realpath(optarg, NULL);
3575 if (repo_path == NULL)
3576 return got_error_from_errno2("realpath",
3578 got_path_strip_trailing_slashes(repo_path);
3589 if (do_list && delref)
3590 errx(1, "-l and -d options are mutually exclusive\n");
3595 if (!do_list && !delref && argc == 0)
3598 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3599 errx(1, "-c option can only be used when creating a branch");
3601 if (do_list || delref) {
3604 } else if (!do_show && argc != 1)
3608 if (do_list || do_show) {
3609 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3613 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3614 "sendfd unveil", NULL) == -1)
3618 cwd = getcwd(NULL, 0);
3620 error = got_error_from_errno("getcwd");
3624 if (repo_path == NULL) {
3625 error = got_worktree_open(&worktree, cwd);
3626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3632 strdup(got_worktree_get_repo_path(worktree));
3633 if (repo_path == NULL)
3634 error = got_error_from_errno("strdup");
3638 repo_path = strdup(cwd);
3639 if (repo_path == NULL) {
3640 error = got_error_from_errno("strdup");
3646 error = got_repo_open(&repo, repo_path, NULL);
3650 error = apply_unveil(got_repo_get_path(repo), do_list,
3651 worktree ? got_worktree_get_root_path(worktree) : NULL);
3656 error = show_current_branch(repo, worktree);
3658 error = list_branches(repo, worktree);
3660 error = delete_branch(repo, worktree, delref);
3662 struct got_object_id *commit_id;
3663 if (commit_id_arg == NULL)
3664 commit_id_arg = worktree ?
3665 got_worktree_get_head_ref_name(worktree) :
3667 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3670 error = add_branch(repo, argv[0], commit_id);
3675 got_repo_close(repo);
3677 got_worktree_close(worktree);
3688 "usage: %s tag [-r repository] | -l | "
3689 "[-m message] name [commit]\n", getprogname());
3694 static const struct got_error *
3695 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3697 const struct got_error *err = NULL;
3698 struct got_reflist_entry *re, *se, *new;
3699 struct got_object_id *re_id, *se_id;
3700 struct got_tag_object *re_tag, *se_tag;
3701 time_t re_time, se_time;
3703 SIMPLEQ_FOREACH(re, tags, entry) {
3704 se = SIMPLEQ_FIRST(sorted);
3706 err = got_reflist_entry_dup(&new, re);
3709 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3712 err = got_ref_resolve(&re_id, repo, re->ref);
3715 err = got_object_open_as_tag(&re_tag, repo, re_id);
3719 re_time = got_object_tag_get_tagger_time(re_tag);
3720 got_object_tag_close(re_tag);
3724 err = got_ref_resolve(&se_id, repo, re->ref);
3727 err = got_object_open_as_tag(&se_tag, repo, se_id);
3731 se_time = got_object_tag_get_tagger_time(se_tag);
3732 got_object_tag_close(se_tag);
3734 if (se_time > re_time) {
3735 err = got_reflist_entry_dup(&new, re);
3738 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3741 se = SIMPLEQ_NEXT(se, entry);
3750 static const struct got_error *
3751 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3752 struct got_reference *ref2)
3754 const struct got_error *err = NULL;
3755 struct got_repository *repo = arg;
3756 struct got_object_id *id1, *id2 = NULL;
3757 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3758 time_t time1, time2;
3762 err = got_ref_resolve(&id1, repo, ref1);
3765 err = got_object_open_as_tag(&tag1, repo, id1);
3769 err = got_ref_resolve(&id2, repo, ref2);
3772 err = got_object_open_as_tag(&tag2, repo, id2);
3776 time1 = got_object_tag_get_tagger_time(tag1);
3777 time2 = got_object_tag_get_tagger_time(tag2);
3779 /* Put latest tags first. */
3782 else if (time1 > time2)
3785 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3790 got_object_tag_close(tag1);
3792 got_object_tag_close(tag2);
3796 static const struct got_error *
3797 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3799 static const struct got_error *err = NULL;
3800 struct got_reflist_head refs;
3801 struct got_reflist_entry *re;
3803 SIMPLEQ_INIT(&refs);
3805 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3809 SIMPLEQ_FOREACH(re, &refs, entry) {
3810 const char *refname;
3811 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3814 struct got_object_id *id;
3815 struct got_tag_object *tag;
3817 refname = got_ref_get_name(re->ref);
3818 if (strncmp(refname, "refs/tags/", 10) != 0)
3821 refstr = got_ref_to_str(re->ref);
3822 if (refstr == NULL) {
3823 err = got_error_from_errno("got_ref_to_str");
3826 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3829 err = got_ref_resolve(&id, repo, re->ref);
3832 err = got_object_open_as_tag(&tag, repo, id);
3836 printf("from: %s\n", got_object_tag_get_tagger(tag));
3837 tagger_time = got_object_tag_get_tagger_time(tag);
3838 datestr = get_datestr(&tagger_time, datebuf);
3840 printf("date: %s UTC\n", datestr);
3841 err = got_object_id_str(&id_str,
3842 got_object_tag_get_object_id(tag));
3845 switch (got_object_tag_get_object_type(tag)) {
3846 case GOT_OBJ_TYPE_BLOB:
3847 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3849 case GOT_OBJ_TYPE_TREE:
3850 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3852 case GOT_OBJ_TYPE_COMMIT:
3853 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3855 case GOT_OBJ_TYPE_TAG:
3856 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3862 tagmsg0 = strdup(got_object_tag_get_message(tag));
3863 got_object_tag_close(tag);
3864 if (tagmsg0 == NULL) {
3865 err = got_error_from_errno("strdup");
3871 line = strsep(&tagmsg, "\n");
3873 printf(" %s\n", line);
3878 got_ref_list_free(&refs);
3882 static const struct got_error *
3883 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3884 const char *tag_name, const char *repo_path)
3886 const struct got_error *err = NULL;
3887 char *template = NULL, *initial_content = NULL;
3888 char *editor = NULL;
3891 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3892 err = got_error_from_errno("asprintf");
3896 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3897 commit_id_str, tag_name) == -1) {
3898 err = got_error_from_errno("asprintf");
3902 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3906 dprintf(fd, initial_content);
3909 err = get_editor(&editor);
3912 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3914 free(initial_content);
3918 /* Editor is done; we can now apply unveil(2) */
3920 err = apply_unveil(repo_path, 0, NULL);
3929 static const struct got_error *
3930 add_tag(struct got_repository *repo, const char *tag_name,
3931 const char *commit_arg, const char *tagmsg_arg)
3933 const struct got_error *err = NULL;
3934 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3935 char *label = NULL, *commit_id_str = NULL;
3936 struct got_reference *ref = NULL;
3937 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3938 char *tagmsg_path = NULL, *tag_id_str = NULL;
3939 int preserve_tagmsg = 0;
3942 * Don't let the user create a tag name with a leading '-'.
3943 * While technically a valid reference name, this case is usually
3944 * an unintended typo.
3946 if (tag_name[0] == '-')
3947 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3949 err = get_author(&tagger, repo);
3953 err = match_object_id(&commit_id, &label, commit_arg,
3954 GOT_OBJ_TYPE_COMMIT, 1, repo);
3958 err = got_object_id_str(&commit_id_str, commit_id);
3962 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3963 refname = strdup(tag_name);
3964 if (refname == NULL) {
3965 err = got_error_from_errno("strdup");
3969 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3970 err = got_error_from_errno("asprintf");
3974 err = got_ref_open(&ref, repo, refname, 0);
3976 err = got_error(GOT_ERR_TAG_EXISTS);
3978 } else if (err->code != GOT_ERR_NOT_REF)
3981 if (tagmsg_arg == NULL) {
3982 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3983 tag_name, got_repo_get_path(repo));
3985 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3986 tagmsg_path != NULL)
3987 preserve_tagmsg = 1;
3992 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3993 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3996 preserve_tagmsg = 1;
4000 err = got_ref_alloc(&ref, refname, tag_id);
4003 preserve_tagmsg = 1;
4007 err = got_ref_write(ref, repo);
4010 preserve_tagmsg = 1;
4014 err = got_object_id_str(&tag_id_str, tag_id);
4017 preserve_tagmsg = 1;
4020 printf("Created tag %s\n", tag_id_str);
4022 if (preserve_tagmsg) {
4023 fprintf(stderr, "%s: tag message preserved in %s\n",
4024 getprogname(), tagmsg_path);
4025 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4026 err = got_error_from_errno2("unlink", tagmsg_path);
4031 free(commit_id_str);
4039 static const struct got_error *
4040 cmd_tag(int argc, char *argv[])
4042 const struct got_error *error = NULL;
4043 struct got_repository *repo = NULL;
4044 struct got_worktree *worktree = NULL;
4045 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4046 char *gitconfig_path = NULL;
4047 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4048 int ch, do_list = 0;
4050 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4056 repo_path = realpath(optarg, NULL);
4057 if (repo_path == NULL)
4058 return got_error_from_errno2("realpath",
4060 got_path_strip_trailing_slashes(repo_path);
4076 errx(1, "-l and -m options are mutually exclusive\n");
4079 } else if (argc < 1 || argc > 2)
4082 commit_id_arg = argv[1];
4087 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4091 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4092 "sendfd unveil", NULL) == -1)
4096 cwd = getcwd(NULL, 0);
4098 error = got_error_from_errno("getcwd");
4102 if (repo_path == NULL) {
4103 error = got_worktree_open(&worktree, cwd);
4104 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4110 strdup(got_worktree_get_repo_path(worktree));
4111 if (repo_path == NULL)
4112 error = got_error_from_errno("strdup");
4116 repo_path = strdup(cwd);
4117 if (repo_path == NULL) {
4118 error = got_error_from_errno("strdup");
4125 error = got_repo_open(&repo, repo_path, NULL);
4128 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4131 error = list_tags(repo, worktree);
4133 error = get_gitconfig_path(&gitconfig_path);
4136 error = got_repo_open(&repo, repo_path, gitconfig_path);
4141 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4146 if (commit_id_arg == NULL) {
4147 struct got_reference *head_ref;
4148 struct got_object_id *commit_id;
4149 error = got_ref_open(&head_ref, repo,
4150 worktree ? got_worktree_get_head_ref_name(worktree)
4154 error = got_ref_resolve(&commit_id, repo, head_ref);
4155 got_ref_close(head_ref);
4158 error = got_object_id_str(&commit_id_str, commit_id);
4164 error = add_tag(repo, tag_name,
4165 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4169 got_repo_close(repo);
4171 got_worktree_close(worktree);
4174 free(gitconfig_path);
4175 free(commit_id_str);
4182 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4187 static const struct got_error *
4188 add_progress(void *arg, unsigned char status, const char *path)
4190 while (path[0] == '/')
4192 printf("%c %s\n", status, path);
4196 static const struct got_error *
4197 cmd_add(int argc, char *argv[])
4199 const struct got_error *error = NULL;
4200 struct got_repository *repo = NULL;
4201 struct got_worktree *worktree = NULL;
4203 struct got_pathlist_head paths;
4204 struct got_pathlist_entry *pe;
4205 int ch, can_recurse = 0, no_ignores = 0;
4209 while ((ch = getopt(argc, argv, "IR")) != -1) {
4227 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4234 cwd = getcwd(NULL, 0);
4236 error = got_error_from_errno("getcwd");
4240 error = got_worktree_open(&worktree, cwd);
4244 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4249 error = apply_unveil(got_repo_get_path(repo), 1,
4250 got_worktree_get_root_path(worktree));
4254 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4258 if (!can_recurse && no_ignores) {
4259 error = got_error_msg(GOT_ERR_BAD_PATH,
4260 "disregarding ignores requires -R option");
4268 TAILQ_FOREACH(pe, &paths, entry) {
4269 if (asprintf(&ondisk_path, "%s/%s",
4270 got_worktree_get_root_path(worktree),
4272 error = got_error_from_errno("asprintf");
4275 if (lstat(ondisk_path, &sb) == -1) {
4276 if (errno == ENOENT) {
4280 error = got_error_from_errno2("lstat",
4286 if (S_ISDIR(sb.st_mode)) {
4287 error = got_error_msg(GOT_ERR_BAD_PATH,
4288 "adding directories requires -R option");
4294 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4295 NULL, repo, no_ignores);
4298 got_repo_close(repo);
4300 got_worktree_close(worktree);
4301 TAILQ_FOREACH(pe, &paths, entry)
4302 free((char *)pe->path);
4303 got_pathlist_free(&paths);
4311 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4316 static const struct got_error *
4317 print_remove_status(void *arg, unsigned char status,
4318 unsigned char staged_status, const char *path)
4320 while (path[0] == '/')
4322 if (status == GOT_STATUS_NONEXISTENT)
4324 if (status == staged_status && (status == GOT_STATUS_DELETE))
4325 status = GOT_STATUS_NO_CHANGE;
4326 printf("%c%c %s\n", status, staged_status, path);
4330 static const struct got_error *
4331 cmd_remove(int argc, char *argv[])
4333 const struct got_error *error = NULL;
4334 struct got_worktree *worktree = NULL;
4335 struct got_repository *repo = NULL;
4337 struct got_pathlist_head paths;
4338 struct got_pathlist_entry *pe;
4339 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4343 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4346 delete_local_mods = 1;
4364 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4371 cwd = getcwd(NULL, 0);
4373 error = got_error_from_errno("getcwd");
4376 error = got_worktree_open(&worktree, cwd);
4380 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4385 error = apply_unveil(got_repo_get_path(repo), 1,
4386 got_worktree_get_root_path(worktree));
4390 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4397 TAILQ_FOREACH(pe, &paths, entry) {
4398 if (asprintf(&ondisk_path, "%s/%s",
4399 got_worktree_get_root_path(worktree),
4401 error = got_error_from_errno("asprintf");
4404 if (lstat(ondisk_path, &sb) == -1) {
4405 if (errno == ENOENT) {
4409 error = got_error_from_errno2("lstat",
4415 if (S_ISDIR(sb.st_mode)) {
4416 error = got_error_msg(GOT_ERR_BAD_PATH,
4417 "removing directories requires -R option");
4423 error = got_worktree_schedule_delete(worktree, &paths,
4424 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4429 got_repo_close(repo);
4431 got_worktree_close(worktree);
4432 TAILQ_FOREACH(pe, &paths, entry)
4433 free((char *)pe->path);
4434 got_pathlist_free(&paths);
4442 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4443 "path ...\n", getprogname());
4447 static const struct got_error *
4448 revert_progress(void *arg, unsigned char status, const char *path)
4450 while (path[0] == '/')
4452 printf("%c %s\n", status, path);
4456 struct choose_patch_arg {
4457 FILE *patch_script_file;
4461 static const struct got_error *
4462 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4463 int nchanges, const char *action)
4466 size_t linesize = 0;
4470 case GOT_STATUS_ADD:
4471 printf("A %s\n%s this addition? [y/n] ", path, action);
4473 case GOT_STATUS_DELETE:
4474 printf("D %s\n%s this deletion? [y/n] ", path, action);
4476 case GOT_STATUS_MODIFY:
4477 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4478 return got_error_from_errno("fseek");
4479 printf(GOT_COMMIT_SEP_STR);
4480 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4482 if (ferror(patch_file))
4483 return got_error_from_errno("getline");
4484 printf(GOT_COMMIT_SEP_STR);
4485 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4486 path, n, nchanges, action);
4489 return got_error_path(path, GOT_ERR_FILE_STATUS);
4495 static const struct got_error *
4496 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4497 FILE *patch_file, int n, int nchanges)
4499 const struct got_error *err = NULL;
4501 size_t linesize = 0;
4504 struct choose_patch_arg *a = arg;
4506 *choice = GOT_PATCH_CHOICE_NONE;
4508 if (a->patch_script_file) {
4510 err = show_change(status, path, patch_file, n, nchanges,
4514 linelen = getline(&line, &linesize, a->patch_script_file);
4515 if (linelen == -1) {
4516 if (ferror(a->patch_script_file))
4517 return got_error_from_errno("getline");
4520 nl = strchr(line, '\n');
4523 if (strcmp(line, "y") == 0) {
4524 *choice = GOT_PATCH_CHOICE_YES;
4526 } else if (strcmp(line, "n") == 0) {
4527 *choice = GOT_PATCH_CHOICE_NO;
4529 } else if (strcmp(line, "q") == 0 &&
4530 status == GOT_STATUS_MODIFY) {
4531 *choice = GOT_PATCH_CHOICE_QUIT;
4534 printf("invalid response '%s'\n", line);
4539 while (resp != 'y' && resp != 'n' && resp != 'q') {
4540 err = show_change(status, path, patch_file, n, nchanges,
4547 if (status == GOT_STATUS_MODIFY) {
4548 if (resp != 'y' && resp != 'n' && resp != 'q') {
4549 printf("invalid response '%c'\n", resp);
4552 } else if (resp != 'y' && resp != 'n') {
4553 printf("invalid response '%c'\n", resp);
4559 *choice = GOT_PATCH_CHOICE_YES;
4560 else if (resp == 'n')
4561 *choice = GOT_PATCH_CHOICE_NO;
4562 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4563 *choice = GOT_PATCH_CHOICE_QUIT;
4569 static const struct got_error *
4570 cmd_revert(int argc, char *argv[])
4572 const struct got_error *error = NULL;
4573 struct got_worktree *worktree = NULL;
4574 struct got_repository *repo = NULL;
4575 char *cwd = NULL, *path = NULL;
4576 struct got_pathlist_head paths;
4577 struct got_pathlist_entry *pe;
4578 int ch, can_recurse = 0, pflag = 0;
4579 FILE *patch_script_file = NULL;
4580 const char *patch_script_path = NULL;
4581 struct choose_patch_arg cpa;
4585 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4591 patch_script_path = optarg;
4606 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4607 "unveil", NULL) == -1)
4612 if (patch_script_path && !pflag)
4613 errx(1, "-F option can only be used together with -p option");
4615 cwd = getcwd(NULL, 0);
4617 error = got_error_from_errno("getcwd");
4620 error = got_worktree_open(&worktree, cwd);
4624 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4629 if (patch_script_path) {
4630 patch_script_file = fopen(patch_script_path, "r");
4631 if (patch_script_file == NULL) {
4632 error = got_error_from_errno2("fopen",
4637 error = apply_unveil(got_repo_get_path(repo), 1,
4638 got_worktree_get_root_path(worktree));
4642 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4649 TAILQ_FOREACH(pe, &paths, entry) {
4650 if (asprintf(&ondisk_path, "%s/%s",
4651 got_worktree_get_root_path(worktree),
4653 error = got_error_from_errno("asprintf");
4656 if (lstat(ondisk_path, &sb) == -1) {
4657 if (errno == ENOENT) {
4661 error = got_error_from_errno2("lstat",
4667 if (S_ISDIR(sb.st_mode)) {
4668 error = got_error_msg(GOT_ERR_BAD_PATH,
4669 "reverting directories requires -R option");
4675 cpa.patch_script_file = patch_script_file;
4676 cpa.action = "revert";
4677 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4678 pflag ? choose_patch : NULL, &cpa, repo);
4682 if (patch_script_file && fclose(patch_script_file) == EOF &&
4684 error = got_error_from_errno2("fclose", patch_script_path);
4686 got_repo_close(repo);
4688 got_worktree_close(worktree);
4697 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4702 struct collect_commit_logmsg_arg {
4703 const char *cmdline_log;
4705 const char *worktree_path;
4706 const char *branch_name;
4707 const char *repo_path;
4712 static const struct got_error *
4713 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4716 char *initial_content = NULL;
4717 struct got_pathlist_entry *pe;
4718 const struct got_error *err = NULL;
4719 char *template = NULL;
4720 struct collect_commit_logmsg_arg *a = arg;
4724 /* if a message was specified on the command line, just use it */
4725 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4726 len = strlen(a->cmdline_log) + 1;
4727 *logmsg = malloc(len + 1);
4728 if (*logmsg == NULL)
4729 return got_error_from_errno("malloc");
4730 strlcpy(*logmsg, a->cmdline_log, len);
4734 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4735 return got_error_from_errno("asprintf");
4737 if (asprintf(&initial_content,
4738 "\n# changes to be committed on branch %s:\n",
4739 a->branch_name) == -1)
4740 return got_error_from_errno("asprintf");
4742 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4746 dprintf(fd, initial_content);
4748 TAILQ_FOREACH(pe, commitable_paths, entry) {
4749 struct got_commitable *ct = pe->data;
4750 dprintf(fd, "# %c %s\n",
4751 got_commitable_get_status(ct),
4752 got_commitable_get_path(ct));
4756 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4758 free(initial_content);
4761 /* Editor is done; we can now apply unveil(2) */
4763 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4772 static const struct got_error *
4773 cmd_commit(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *id_str = NULL;
4779 struct got_object_id *id = NULL;
4780 const char *logmsg = NULL;
4781 struct collect_commit_logmsg_arg cl_arg;
4782 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4783 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4784 struct got_pathlist_head paths;
4787 cl_arg.logmsg_path = NULL;
4789 while ((ch = getopt(argc, argv, "m:")) != -1) {
4804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4805 "unveil", NULL) == -1)
4808 cwd = getcwd(NULL, 0);
4810 error = got_error_from_errno("getcwd");
4813 error = got_worktree_open(&worktree, cwd);
4817 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4820 if (rebase_in_progress) {
4821 error = got_error(GOT_ERR_REBASING);
4825 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4830 error = get_gitconfig_path(&gitconfig_path);
4833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4838 error = get_author(&author, repo);
4843 * unveil(2) traverses exec(2); if an editor is used we have
4844 * to apply unveil after the log message has been written.
4846 if (logmsg == NULL || strlen(logmsg) == 0)
4847 error = get_editor(&editor);
4849 error = apply_unveil(got_repo_get_path(repo), 0,
4850 got_worktree_get_root_path(worktree));
4854 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4858 cl_arg.editor = editor;
4859 cl_arg.cmdline_log = logmsg;
4860 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4861 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4862 if (!histedit_in_progress) {
4863 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4864 error = got_error(GOT_ERR_COMMIT_BRANCH);
4867 cl_arg.branch_name += 11;
4869 cl_arg.repo_path = got_repo_get_path(repo);
4870 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4871 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4873 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4874 cl_arg.logmsg_path != NULL)
4875 preserve_logmsg = 1;
4879 error = got_object_id_str(&id_str, id);
4882 printf("Created commit %s\n", id_str);
4884 if (preserve_logmsg) {
4885 fprintf(stderr, "%s: log message preserved in %s\n",
4886 getprogname(), cl_arg.logmsg_path);
4887 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4889 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4890 free(cl_arg.logmsg_path);
4892 got_repo_close(repo);
4894 got_worktree_close(worktree);
4897 free(gitconfig_path);
4904 usage_cherrypick(void)
4906 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4910 static const struct got_error *
4911 cmd_cherrypick(int argc, char *argv[])
4913 const struct got_error *error = NULL;
4914 struct got_worktree *worktree = NULL;
4915 struct got_repository *repo = NULL;
4916 char *cwd = NULL, *commit_id_str = NULL;
4917 struct got_object_id *commit_id = NULL;
4918 struct got_commit_object *commit = NULL;
4919 struct got_object_qid *pid;
4920 struct got_reference *head_ref = NULL;
4921 int ch, did_something = 0;
4923 while ((ch = getopt(argc, argv, "")) != -1) {
4935 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4936 "unveil", NULL) == -1)
4942 cwd = getcwd(NULL, 0);
4944 error = got_error_from_errno("getcwd");
4947 error = got_worktree_open(&worktree, cwd);
4951 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4956 error = apply_unveil(got_repo_get_path(repo), 0,
4957 got_worktree_get_root_path(worktree));
4961 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4962 GOT_OBJ_TYPE_COMMIT, repo);
4963 if (error != NULL) {
4964 struct got_reference *ref;
4965 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4967 error = got_ref_open(&ref, repo, argv[0], 0);
4970 error = got_ref_resolve(&commit_id, repo, ref);
4975 error = got_object_id_str(&commit_id_str, commit_id);
4979 error = got_ref_open(&head_ref, repo,
4980 got_worktree_get_head_ref_name(worktree), 0);
4984 error = check_same_branch(commit_id, head_ref, NULL, repo);
4986 if (error->code != GOT_ERR_ANCESTRY)
4990 error = got_error(GOT_ERR_SAME_BRANCH);
4994 error = got_object_open_as_commit(&commit, repo, commit_id);
4997 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4998 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4999 commit_id, repo, update_progress, &did_something, check_cancelled,
5005 printf("Merged commit %s\n", commit_id_str);
5008 got_object_commit_close(commit);
5009 free(commit_id_str);
5011 got_ref_close(head_ref);
5013 got_worktree_close(worktree);
5015 got_repo_close(repo);
5022 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5026 static const struct got_error *
5027 cmd_backout(int argc, char *argv[])
5029 const struct got_error *error = NULL;
5030 struct got_worktree *worktree = NULL;
5031 struct got_repository *repo = NULL;
5032 char *cwd = NULL, *commit_id_str = NULL;
5033 struct got_object_id *commit_id = NULL;
5034 struct got_commit_object *commit = NULL;
5035 struct got_object_qid *pid;
5036 struct got_reference *head_ref = NULL;
5037 int ch, did_something = 0;
5039 while ((ch = getopt(argc, argv, "")) != -1) {
5051 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5052 "unveil", NULL) == -1)
5058 cwd = getcwd(NULL, 0);
5060 error = got_error_from_errno("getcwd");
5063 error = got_worktree_open(&worktree, cwd);
5067 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5072 error = apply_unveil(got_repo_get_path(repo), 0,
5073 got_worktree_get_root_path(worktree));
5077 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5078 GOT_OBJ_TYPE_COMMIT, repo);
5079 if (error != NULL) {
5080 struct got_reference *ref;
5081 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5083 error = got_ref_open(&ref, repo, argv[0], 0);
5086 error = got_ref_resolve(&commit_id, repo, ref);
5091 error = got_object_id_str(&commit_id_str, commit_id);
5095 error = got_ref_open(&head_ref, repo,
5096 got_worktree_get_head_ref_name(worktree), 0);
5100 error = check_same_branch(commit_id, head_ref, NULL, repo);
5104 error = got_object_open_as_commit(&commit, repo, commit_id);
5107 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5109 error = got_error(GOT_ERR_ROOT_COMMIT);
5113 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5114 update_progress, &did_something, check_cancelled, NULL);
5119 printf("Backed out commit %s\n", commit_id_str);
5122 got_object_commit_close(commit);
5123 free(commit_id_str);
5125 got_ref_close(head_ref);
5127 got_worktree_close(worktree);
5129 got_repo_close(repo);
5136 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5142 trim_logmsg(char *logmsg, int limit)
5147 len = strlen(logmsg);
5151 nl = strchr(logmsg, '\n');
5156 static const struct got_error *
5157 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5159 const struct got_error *err;
5160 char *logmsg0 = NULL;
5163 err = got_object_commit_get_logmsg(&logmsg0, commit);
5168 while (isspace((unsigned char)s[0]))
5171 *logmsg = strdup(s);
5172 if (*logmsg == NULL) {
5173 err = got_error_from_errno("strdup");
5177 trim_logmsg(*logmsg, limit);
5183 static const struct got_error *
5184 show_rebase_progress(struct got_commit_object *commit,
5185 struct got_object_id *old_id, struct got_object_id *new_id)
5187 const struct got_error *err;
5188 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5190 err = got_object_id_str(&old_id_str, old_id);
5195 err = got_object_id_str(&new_id_str, new_id);
5200 old_id_str[12] = '\0';
5202 new_id_str[12] = '\0';
5204 err = get_short_logmsg(&logmsg, 42, commit);
5208 printf("%s -> %s: %s\n", old_id_str,
5209 new_id_str ? new_id_str : "no-op change", logmsg);
5216 static const struct got_error *
5217 rebase_progress(void *arg, unsigned char status, const char *path)
5219 unsigned char *rebase_status = arg;
5221 while (path[0] == '/')
5223 printf("%c %s\n", status, path);
5225 if (*rebase_status == GOT_STATUS_CONFLICT)
5227 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5228 *rebase_status = status;
5232 static const struct got_error *
5233 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5234 struct got_reference *branch, struct got_reference *new_base_branch,
5235 struct got_reference *tmp_branch, struct got_repository *repo)
5237 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5238 return got_worktree_rebase_complete(worktree, fileindex,
5239 new_base_branch, tmp_branch, branch, repo);
5242 static const struct got_error *
5243 rebase_commit(struct got_pathlist_head *merged_paths,
5244 struct got_worktree *worktree, struct got_fileindex *fileindex,
5245 struct got_reference *tmp_branch,
5246 struct got_object_id *commit_id, struct got_repository *repo)
5248 const struct got_error *error;
5249 struct got_commit_object *commit;
5250 struct got_object_id *new_commit_id;
5252 error = got_object_open_as_commit(&commit, repo, commit_id);
5256 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5257 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5259 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5261 error = show_rebase_progress(commit, commit_id, NULL);
5263 error = show_rebase_progress(commit, commit_id, new_commit_id);
5264 free(new_commit_id);
5267 got_object_commit_close(commit);
5271 struct check_path_prefix_arg {
5272 const char *path_prefix;
5277 static const struct got_error *
5278 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5279 struct got_blob_object *blob2, struct got_object_id *id1,
5280 struct got_object_id *id2, const char *path1, const char *path2,
5281 mode_t mode1, mode_t mode2, struct got_repository *repo)
5283 struct check_path_prefix_arg *a = arg;
5285 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5286 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5287 return got_error(a->errcode);
5292 static const struct got_error *
5293 check_path_prefix(struct got_object_id *parent_id,
5294 struct got_object_id *commit_id, const char *path_prefix,
5295 int errcode, struct got_repository *repo)
5297 const struct got_error *err;
5298 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5299 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5300 struct check_path_prefix_arg cpp_arg;
5302 if (got_path_is_root_dir(path_prefix))
5305 err = got_object_open_as_commit(&commit, repo, commit_id);
5309 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5313 err = got_object_open_as_tree(&tree1, repo,
5314 got_object_commit_get_tree_id(parent_commit));
5318 err = got_object_open_as_tree(&tree2, repo,
5319 got_object_commit_get_tree_id(commit));
5323 cpp_arg.path_prefix = path_prefix;
5324 while (cpp_arg.path_prefix[0] == '/')
5325 cpp_arg.path_prefix++;
5326 cpp_arg.len = strlen(cpp_arg.path_prefix);
5327 cpp_arg.errcode = errcode;
5328 err = got_diff_tree(tree1, tree2, "", "", repo,
5329 check_path_prefix_in_diff, &cpp_arg, 0);
5332 got_object_tree_close(tree1);
5334 got_object_tree_close(tree2);
5336 got_object_commit_close(commit);
5338 got_object_commit_close(parent_commit);
5342 static const struct got_error *
5343 collect_commits(struct got_object_id_queue *commits,
5344 struct got_object_id *initial_commit_id,
5345 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5346 const char *path_prefix, int path_prefix_errcode,
5347 struct got_repository *repo)
5349 const struct got_error *err = NULL;
5350 struct got_commit_graph *graph = NULL;
5351 struct got_object_id *parent_id = NULL;
5352 struct got_object_qid *qid;
5353 struct got_object_id *commit_id = initial_commit_id;
5355 err = got_commit_graph_open(&graph, "/", 1);
5359 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5360 check_cancelled, NULL);
5363 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5364 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5365 check_cancelled, NULL);
5367 if (err->code == GOT_ERR_ITER_COMPLETED) {
5368 err = got_error_msg(GOT_ERR_ANCESTRY,
5369 "ran out of commits to rebase before "
5370 "youngest common ancestor commit has "
5375 err = check_path_prefix(parent_id, commit_id,
5376 path_prefix, path_prefix_errcode, repo);
5380 err = got_object_qid_alloc(&qid, commit_id);
5383 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5384 commit_id = parent_id;
5388 got_commit_graph_close(graph);
5392 static const struct got_error *
5393 cmd_rebase(int argc, char *argv[])
5395 const struct got_error *error = NULL;
5396 struct got_worktree *worktree = NULL;
5397 struct got_repository *repo = NULL;
5398 struct got_fileindex *fileindex = NULL;
5400 struct got_reference *branch = NULL;
5401 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5402 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5403 struct got_object_id *resume_commit_id = NULL;
5404 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5405 struct got_commit_object *commit = NULL;
5406 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5407 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5408 struct got_object_id_queue commits;
5409 struct got_pathlist_head merged_paths;
5410 const struct got_object_id_queue *parent_ids;
5411 struct got_object_qid *qid, *pid;
5413 SIMPLEQ_INIT(&commits);
5414 TAILQ_INIT(&merged_paths);
5416 while ((ch = getopt(argc, argv, "ac")) != -1) {
5422 continue_rebase = 1;
5434 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5435 "unveil", NULL) == -1)
5438 if (abort_rebase && continue_rebase)
5440 else if (abort_rebase || continue_rebase) {
5443 } else if (argc != 1)
5446 cwd = getcwd(NULL, 0);
5448 error = got_error_from_errno("getcwd");
5451 error = got_worktree_open(&worktree, cwd);
5455 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5460 error = apply_unveil(got_repo_get_path(repo), 0,
5461 got_worktree_get_root_path(worktree));
5465 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5471 if (!rebase_in_progress) {
5472 error = got_error(GOT_ERR_NOT_REBASING);
5475 error = got_worktree_rebase_continue(&resume_commit_id,
5476 &new_base_branch, &tmp_branch, &branch, &fileindex,
5480 printf("Switching work tree to %s\n",
5481 got_ref_get_symref_target(new_base_branch));
5482 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5483 new_base_branch, update_progress, &did_something);
5486 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5487 goto done; /* nothing else to do */
5490 if (continue_rebase) {
5491 if (!rebase_in_progress) {
5492 error = got_error(GOT_ERR_NOT_REBASING);
5495 error = got_worktree_rebase_continue(&resume_commit_id,
5496 &new_base_branch, &tmp_branch, &branch, &fileindex,
5501 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5502 resume_commit_id, repo);
5506 yca_id = got_object_id_dup(resume_commit_id);
5507 if (yca_id == NULL) {
5508 error = got_error_from_errno("got_object_id_dup");
5512 error = got_ref_open(&branch, repo, argv[0], 0);
5517 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5521 if (!continue_rebase) {
5522 struct got_object_id *base_commit_id;
5524 base_commit_id = got_worktree_get_base_commit_id(worktree);
5525 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5526 base_commit_id, branch_head_commit_id, repo,
5527 check_cancelled, NULL);
5530 if (yca_id == NULL) {
5531 error = got_error_msg(GOT_ERR_ANCESTRY,
5532 "specified branch shares no common ancestry "
5533 "with work tree's branch");
5537 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5539 if (error->code != GOT_ERR_ANCESTRY)
5543 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5544 "specified branch resolves to a commit which "
5545 "is already contained in work tree's branch");
5548 error = got_worktree_rebase_prepare(&new_base_branch,
5549 &tmp_branch, &fileindex, worktree, branch, repo);
5554 commit_id = branch_head_commit_id;
5555 error = got_object_open_as_commit(&commit, repo, commit_id);
5559 parent_ids = got_object_commit_get_parent_ids(commit);
5560 pid = SIMPLEQ_FIRST(parent_ids);
5562 if (!continue_rebase) {
5564 error = got_worktree_rebase_abort(worktree, fileindex,
5565 repo, new_base_branch, update_progress,
5569 printf("Rebase of %s aborted\n",
5570 got_ref_get_name(branch));
5572 error = got_error(GOT_ERR_EMPTY_REBASE);
5575 error = collect_commits(&commits, commit_id, pid->id,
5576 yca_id, got_worktree_get_path_prefix(worktree),
5577 GOT_ERR_REBASE_PATH, repo);
5578 got_object_commit_close(commit);
5583 if (SIMPLEQ_EMPTY(&commits)) {
5584 if (continue_rebase) {
5585 error = rebase_complete(worktree, fileindex,
5586 branch, new_base_branch, tmp_branch, repo);
5589 /* Fast-forward the reference of the branch. */
5590 struct got_object_id *new_head_commit_id;
5592 error = got_ref_resolve(&new_head_commit_id, repo,
5596 error = got_object_id_str(&id_str, new_head_commit_id);
5597 printf("Forwarding %s to commit %s\n",
5598 got_ref_get_name(branch), id_str);
5600 error = got_ref_change_ref(branch,
5601 new_head_commit_id);
5608 SIMPLEQ_FOREACH(qid, &commits, entry) {
5609 commit_id = qid->id;
5610 parent_id = pid ? pid->id : yca_id;
5613 error = got_worktree_rebase_merge_files(&merged_paths,
5614 worktree, fileindex, parent_id, commit_id, repo,
5615 rebase_progress, &rebase_status, check_cancelled, NULL);
5619 if (rebase_status == GOT_STATUS_CONFLICT) {
5620 got_worktree_rebase_pathlist_free(&merged_paths);
5624 error = rebase_commit(&merged_paths, worktree, fileindex,
5625 tmp_branch, commit_id, repo);
5626 got_worktree_rebase_pathlist_free(&merged_paths);
5631 if (rebase_status == GOT_STATUS_CONFLICT) {
5632 error = got_worktree_rebase_postpone(worktree, fileindex);
5635 error = got_error_msg(GOT_ERR_CONFLICTS,
5636 "conflicts must be resolved before rebasing can continue");
5638 error = rebase_complete(worktree, fileindex, branch,
5639 new_base_branch, tmp_branch, repo);
5641 got_object_id_queue_free(&commits);
5642 free(branch_head_commit_id);
5643 free(resume_commit_id);
5646 got_object_commit_close(commit);
5648 got_ref_close(branch);
5649 if (new_base_branch)
5650 got_ref_close(new_base_branch);
5652 got_ref_close(tmp_branch);
5654 got_worktree_close(worktree);
5656 got_repo_close(repo);
5661 usage_histedit(void)
5663 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5668 #define GOT_HISTEDIT_PICK 'p'
5669 #define GOT_HISTEDIT_EDIT 'e'
5670 #define GOT_HISTEDIT_FOLD 'f'
5671 #define GOT_HISTEDIT_DROP 'd'
5672 #define GOT_HISTEDIT_MESG 'm'
5674 static struct got_histedit_cmd {
5678 } got_histedit_cmds[] = {
5679 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5680 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5681 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5682 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5683 { GOT_HISTEDIT_MESG, "mesg",
5684 "single-line log message for commit above (open editor if empty)" },
5687 struct got_histedit_list_entry {
5688 TAILQ_ENTRY(got_histedit_list_entry) entry;
5689 struct got_object_id *commit_id;
5690 const struct got_histedit_cmd *cmd;
5693 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5695 static const struct got_error *
5696 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5697 FILE *f, struct got_repository *repo)
5699 const struct got_error *err = NULL;
5700 char *logmsg = NULL, *id_str = NULL;
5701 struct got_commit_object *commit = NULL;
5704 err = got_object_open_as_commit(&commit, repo, commit_id);
5708 err = get_short_logmsg(&logmsg, 34, commit);
5712 err = got_object_id_str(&id_str, commit_id);
5716 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5718 err = got_ferror(f, GOT_ERR_IO);
5721 got_object_commit_close(commit);
5727 static const struct got_error *
5728 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5729 struct got_repository *repo)
5731 const struct got_error *err = NULL;
5732 struct got_object_qid *qid;
5734 if (SIMPLEQ_EMPTY(commits))
5735 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5737 SIMPLEQ_FOREACH(qid, commits, entry) {
5738 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5747 static const struct got_error *
5748 write_cmd_list(FILE *f)
5750 const struct got_error *err = NULL;
5753 n = fprintf(f, "# Available histedit commands:\n");
5755 return got_ferror(f, GOT_ERR_IO);
5757 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5758 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5759 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5762 err = got_ferror(f, GOT_ERR_IO);
5766 n = fprintf(f, "# Commits will be processed in order from top to "
5767 "bottom of this file.\n");
5769 return got_ferror(f, GOT_ERR_IO);
5773 static const struct got_error *
5774 histedit_syntax_error(int lineno)
5776 static char msg[42];
5779 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5781 if (ret == -1 || ret >= sizeof(msg))
5782 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5784 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5787 static const struct got_error *
5788 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5789 char *logmsg, struct got_repository *repo)
5791 const struct got_error *err;
5792 struct got_commit_object *folded_commit = NULL;
5793 char *id_str, *folded_logmsg = NULL;
5795 err = got_object_id_str(&id_str, hle->commit_id);
5799 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5803 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5806 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5807 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5808 folded_logmsg) == -1) {
5809 err = got_error_from_errno("asprintf");
5814 got_object_commit_close(folded_commit);
5816 free(folded_logmsg);
5820 static struct got_histedit_list_entry *
5821 get_folded_commits(struct got_histedit_list_entry *hle)
5823 struct got_histedit_list_entry *prev, *folded = NULL;
5825 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5826 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5827 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5828 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5830 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5836 static const struct got_error *
5837 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5838 struct got_repository *repo)
5840 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5841 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5842 const struct got_error *err = NULL;
5843 struct got_commit_object *commit = NULL;
5845 struct got_histedit_list_entry *folded = NULL;
5847 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5851 folded = get_folded_commits(hle);
5853 while (folded != hle) {
5854 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5855 folded = TAILQ_NEXT(folded, entry);
5858 err = append_folded_commit_msg(&new_msg, folded,
5864 folded = TAILQ_NEXT(folded, entry);
5868 err = got_object_id_str(&id_str, hle->commit_id);
5871 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5874 if (asprintf(&new_msg,
5875 "%s\n# original log message of commit %s: %s",
5876 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5877 err = got_error_from_errno("asprintf");
5883 err = got_object_id_str(&id_str, hle->commit_id);
5887 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5891 dprintf(fd, logmsg);
5894 err = get_editor(&editor);
5898 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5900 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5902 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5905 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5906 err = got_error_from_errno2("unlink", logmsg_path);
5912 got_object_commit_close(commit);
5916 static const struct got_error *
5917 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5918 FILE *f, struct got_repository *repo)
5920 const struct got_error *err = NULL;
5921 char *line = NULL, *p, *end;
5925 const struct got_histedit_cmd *cmd;
5926 struct got_object_id *commit_id = NULL;
5927 struct got_histedit_list_entry *hle = NULL;
5930 len = getline(&line, &size, f);
5932 const struct got_error *getline_err;
5935 getline_err = got_error_from_errno("getline");
5936 err = got_ferror(f, getline_err->code);
5941 while (isspace((unsigned char)p[0]))
5943 if (p[0] == '#' || p[0] == '\0') {
5949 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5950 cmd = &got_histedit_cmds[i];
5951 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5952 isspace((unsigned char)p[strlen(cmd->name)])) {
5953 p += strlen(cmd->name);
5956 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5961 if (i == nitems(got_histedit_cmds)) {
5962 err = histedit_syntax_error(lineno);
5965 while (isspace((unsigned char)p[0]))
5967 if (cmd->code == GOT_HISTEDIT_MESG) {
5968 if (hle == NULL || hle->logmsg != NULL) {
5969 err = got_error(GOT_ERR_HISTEDIT_CMD);
5973 err = histedit_edit_logmsg(hle, repo);
5977 hle->logmsg = strdup(p);
5978 if (hle->logmsg == NULL) {
5979 err = got_error_from_errno("strdup");
5988 while (end[0] && !isspace((unsigned char)end[0]))
5992 err = got_object_resolve_id_str(&commit_id, repo, p);
5994 /* override error code */
5995 err = histedit_syntax_error(lineno);
5999 hle = malloc(sizeof(*hle));
6001 err = got_error_from_errno("malloc");
6005 hle->commit_id = commit_id;
6010 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6018 static const struct got_error *
6019 histedit_check_script(struct got_histedit_list *histedit_cmds,
6020 struct got_object_id_queue *commits, struct got_repository *repo)
6022 const struct got_error *err = NULL;
6023 struct got_object_qid *qid;
6024 struct got_histedit_list_entry *hle;
6025 static char msg[80];
6028 if (TAILQ_EMPTY(histedit_cmds))
6029 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6030 "histedit script contains no commands");
6031 if (SIMPLEQ_EMPTY(commits))
6032 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6034 SIMPLEQ_FOREACH(qid, commits, entry) {
6035 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6036 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6040 err = got_object_id_str(&id_str, qid->id);
6043 snprintf(msg, sizeof(msg),
6044 "commit %s missing from histedit script", id_str);
6046 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6050 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6051 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6052 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6053 "last commit in histedit script cannot be folded");
6058 static const struct got_error *
6059 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6060 const char *path, struct got_object_id_queue *commits,
6061 struct got_repository *repo)
6063 const struct got_error *err = NULL;
6067 err = get_editor(&editor);
6071 if (spawn_editor(editor, path) == -1) {
6072 err = got_error_from_errno("failed spawning editor");
6076 f = fopen(path, "r");
6078 err = got_error_from_errno("fopen");
6081 err = histedit_parse_list(histedit_cmds, f, repo);
6085 err = histedit_check_script(histedit_cmds, commits, repo);
6087 if (f && fclose(f) != 0 && err == NULL)
6088 err = got_error_from_errno("fclose");
6093 static const struct got_error *
6094 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6095 struct got_object_id_queue *, const char *, struct got_repository *);
6097 static const struct got_error *
6098 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6099 struct got_object_id_queue *commits, struct got_repository *repo)
6101 const struct got_error *err;
6105 err = got_opentemp_named(&path, &f, "got-histedit");
6109 err = write_cmd_list(f);
6113 err = histedit_write_commit_list(commits, f, repo);
6117 if (fclose(f) != 0) {
6118 err = got_error_from_errno("fclose");
6123 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6125 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6126 err->code != GOT_ERR_HISTEDIT_CMD)
6128 err = histedit_edit_list_retry(histedit_cmds, err,
6129 commits, path, repo);
6132 if (f && fclose(f) != 0 && err == NULL)
6133 err = got_error_from_errno("fclose");
6134 if (path && unlink(path) != 0 && err == NULL)
6135 err = got_error_from_errno2("unlink", path);
6140 static const struct got_error *
6141 histedit_save_list(struct got_histedit_list *histedit_cmds,
6142 struct got_worktree *worktree, struct got_repository *repo)
6144 const struct got_error *err = NULL;
6147 struct got_histedit_list_entry *hle;
6148 struct got_commit_object *commit = NULL;
6150 err = got_worktree_get_histedit_script_path(&path, worktree);
6154 f = fopen(path, "w");
6156 err = got_error_from_errno2("fopen", path);
6159 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6160 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6166 int n = fprintf(f, "%c %s\n",
6167 GOT_HISTEDIT_MESG, hle->logmsg);
6169 err = got_ferror(f, GOT_ERR_IO);
6175 if (f && fclose(f) != 0 && err == NULL)
6176 err = got_error_from_errno("fclose");
6179 got_object_commit_close(commit);
6184 histedit_free_list(struct got_histedit_list *histedit_cmds)
6186 struct got_histedit_list_entry *hle;
6188 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6189 TAILQ_REMOVE(histedit_cmds, hle, entry);
6194 static const struct got_error *
6195 histedit_load_list(struct got_histedit_list *histedit_cmds,
6196 const char *path, struct got_repository *repo)
6198 const struct got_error *err = NULL;
6201 f = fopen(path, "r");
6203 err = got_error_from_errno2("fopen", path);
6207 err = histedit_parse_list(histedit_cmds, f, repo);
6209 if (f && fclose(f) != 0 && err == NULL)
6210 err = got_error_from_errno("fclose");
6214 static const struct got_error *
6215 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6216 const struct got_error *edit_err, struct got_object_id_queue *commits,
6217 const char *path, struct got_repository *repo)
6219 const struct got_error *err = NULL, *prev_err = edit_err;
6222 while (resp != 'c' && resp != 'r' && resp != 'a') {
6223 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6224 "or (a)bort: ", getprogname(), prev_err->msg);
6229 histedit_free_list(histedit_cmds);
6230 err = histedit_run_editor(histedit_cmds, path, commits,
6233 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6234 err->code != GOT_ERR_HISTEDIT_CMD)
6241 } else if (resp == 'r') {
6242 histedit_free_list(histedit_cmds);
6243 err = histedit_edit_script(histedit_cmds,
6246 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6247 err->code != GOT_ERR_HISTEDIT_CMD)
6254 } else if (resp == 'a') {
6255 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6258 printf("invalid response '%c'\n", resp);
6264 static const struct got_error *
6265 histedit_complete(struct got_worktree *worktree,
6266 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6267 struct got_reference *branch, struct got_repository *repo)
6269 printf("Switching work tree to %s\n",
6270 got_ref_get_symref_target(branch));
6271 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6275 static const struct got_error *
6276 show_histedit_progress(struct got_commit_object *commit,
6277 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6279 const struct got_error *err;
6280 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6282 err = got_object_id_str(&old_id_str, hle->commit_id);
6287 err = got_object_id_str(&new_id_str, new_id);
6292 old_id_str[12] = '\0';
6294 new_id_str[12] = '\0';
6297 logmsg = strdup(hle->logmsg);
6298 if (logmsg == NULL) {
6299 err = got_error_from_errno("strdup");
6302 trim_logmsg(logmsg, 42);
6304 err = get_short_logmsg(&logmsg, 42, commit);
6309 switch (hle->cmd->code) {
6310 case GOT_HISTEDIT_PICK:
6311 case GOT_HISTEDIT_EDIT:
6312 printf("%s -> %s: %s\n", old_id_str,
6313 new_id_str ? new_id_str : "no-op change", logmsg);
6315 case GOT_HISTEDIT_DROP:
6316 case GOT_HISTEDIT_FOLD:
6317 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6330 static const struct got_error *
6331 histedit_commit(struct got_pathlist_head *merged_paths,
6332 struct got_worktree *worktree, struct got_fileindex *fileindex,
6333 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6334 struct got_repository *repo)
6336 const struct got_error *err;
6337 struct got_commit_object *commit;
6338 struct got_object_id *new_commit_id;
6340 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6341 && hle->logmsg == NULL) {
6342 err = histedit_edit_logmsg(hle, repo);
6347 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6351 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6352 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6355 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6357 err = show_histedit_progress(commit, hle, NULL);
6359 err = show_histedit_progress(commit, hle, new_commit_id);
6360 free(new_commit_id);
6363 got_object_commit_close(commit);
6367 static const struct got_error *
6368 histedit_skip_commit(struct got_histedit_list_entry *hle,
6369 struct got_worktree *worktree, struct got_repository *repo)
6371 const struct got_error *error;
6372 struct got_commit_object *commit;
6374 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6379 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6383 error = show_histedit_progress(commit, hle, NULL);
6384 got_object_commit_close(commit);
6388 static const struct got_error *
6389 cmd_histedit(int argc, char *argv[])
6391 const struct got_error *error = NULL;
6392 struct got_worktree *worktree = NULL;
6393 struct got_fileindex *fileindex = NULL;
6394 struct got_repository *repo = NULL;
6396 struct got_reference *branch = NULL;
6397 struct got_reference *tmp_branch = NULL;
6398 struct got_object_id *resume_commit_id = NULL;
6399 struct got_object_id *base_commit_id = NULL;
6400 struct got_object_id *head_commit_id = NULL;
6401 struct got_commit_object *commit = NULL;
6402 int ch, rebase_in_progress = 0, did_something;
6403 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6404 const char *edit_script_path = NULL;
6405 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6406 struct got_object_id_queue commits;
6407 struct got_pathlist_head merged_paths;
6408 const struct got_object_id_queue *parent_ids;
6409 struct got_object_qid *pid;
6410 struct got_histedit_list histedit_cmds;
6411 struct got_histedit_list_entry *hle;
6413 SIMPLEQ_INIT(&commits);
6414 TAILQ_INIT(&histedit_cmds);
6415 TAILQ_INIT(&merged_paths);
6417 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6426 edit_script_path = optarg;
6438 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6439 "unveil", NULL) == -1)
6442 if (abort_edit && continue_edit)
6448 * This command cannot apply unveil(2) in all cases because the
6449 * user may choose to run an editor to edit the histedit script
6450 * and to edit individual commit log messages.
6451 * unveil(2) traverses exec(2); if an editor is used we have to
6452 * apply unveil after edit script and log messages have been written.
6453 * XXX TODO: Make use of unveil(2) where possible.
6456 cwd = getcwd(NULL, 0);
6458 error = got_error_from_errno("getcwd");
6461 error = got_worktree_open(&worktree, cwd);
6465 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6470 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6473 if (rebase_in_progress) {
6474 error = got_error(GOT_ERR_REBASING);
6478 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6482 if (edit_in_progress && abort_edit) {
6483 error = got_worktree_histedit_continue(&resume_commit_id,
6484 &tmp_branch, &branch, &base_commit_id, &fileindex,
6488 printf("Switching work tree to %s\n",
6489 got_ref_get_symref_target(branch));
6490 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6491 branch, base_commit_id, update_progress, &did_something);
6494 printf("Histedit of %s aborted\n",
6495 got_ref_get_symref_target(branch));
6496 goto done; /* nothing else to do */
6497 } else if (abort_edit) {
6498 error = got_error(GOT_ERR_NOT_HISTEDIT);
6502 if (continue_edit) {
6505 if (!edit_in_progress) {
6506 error = got_error(GOT_ERR_NOT_HISTEDIT);
6510 error = got_worktree_get_histedit_script_path(&path, worktree);
6514 error = histedit_load_list(&histedit_cmds, path, repo);
6519 error = got_worktree_histedit_continue(&resume_commit_id,
6520 &tmp_branch, &branch, &base_commit_id, &fileindex,
6525 error = got_ref_resolve(&head_commit_id, repo, branch);
6529 error = got_object_open_as_commit(&commit, repo,
6533 parent_ids = got_object_commit_get_parent_ids(commit);
6534 pid = SIMPLEQ_FIRST(parent_ids);
6536 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6539 error = collect_commits(&commits, head_commit_id, pid->id,
6540 base_commit_id, got_worktree_get_path_prefix(worktree),
6541 GOT_ERR_HISTEDIT_PATH, repo);
6542 got_object_commit_close(commit);
6547 if (edit_in_progress) {
6548 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6552 error = got_ref_open(&branch, repo,
6553 got_worktree_get_head_ref_name(worktree), 0);
6557 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6558 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6559 "will not edit commit history of a branch outside "
6560 "the \"refs/heads/\" reference namespace");
6564 error = got_ref_resolve(&head_commit_id, repo, branch);
6565 got_ref_close(branch);
6570 error = got_object_open_as_commit(&commit, repo,
6574 parent_ids = got_object_commit_get_parent_ids(commit);
6575 pid = SIMPLEQ_FIRST(parent_ids);
6577 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6580 error = collect_commits(&commits, head_commit_id, pid->id,
6581 got_worktree_get_base_commit_id(worktree),
6582 got_worktree_get_path_prefix(worktree),
6583 GOT_ERR_HISTEDIT_PATH, repo);
6584 got_object_commit_close(commit);
6589 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6590 &base_commit_id, &fileindex, worktree, repo);
6594 if (edit_script_path) {
6595 error = histedit_load_list(&histedit_cmds,
6596 edit_script_path, repo);
6598 got_worktree_histedit_abort(worktree, fileindex,
6599 repo, branch, base_commit_id,
6600 update_progress, &did_something);
6604 error = histedit_edit_script(&histedit_cmds, &commits,
6607 got_worktree_histedit_abort(worktree, fileindex,
6608 repo, branch, base_commit_id,
6609 update_progress, &did_something);
6615 error = histedit_save_list(&histedit_cmds, worktree,
6618 got_worktree_histedit_abort(worktree, fileindex,
6619 repo, branch, base_commit_id,
6620 update_progress, &did_something);
6626 error = histedit_check_script(&histedit_cmds, &commits, repo);
6630 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6631 if (resume_commit_id) {
6632 if (got_object_id_cmp(hle->commit_id,
6633 resume_commit_id) != 0)
6636 resume_commit_id = NULL;
6637 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6638 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6639 error = histedit_skip_commit(hle, worktree,
6642 error = histedit_commit(NULL, worktree,
6643 fileindex, tmp_branch, hle, repo);
6650 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6651 error = histedit_skip_commit(hle, worktree, repo);
6657 error = got_object_open_as_commit(&commit, repo,
6661 parent_ids = got_object_commit_get_parent_ids(commit);
6662 pid = SIMPLEQ_FIRST(parent_ids);
6664 error = got_worktree_histedit_merge_files(&merged_paths,
6665 worktree, fileindex, pid->id, hle->commit_id, repo,
6666 rebase_progress, &rebase_status, check_cancelled, NULL);
6669 got_object_commit_close(commit);
6672 if (rebase_status == GOT_STATUS_CONFLICT) {
6673 got_worktree_rebase_pathlist_free(&merged_paths);
6677 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6679 error = got_object_id_str(&id_str, hle->commit_id);
6682 printf("Stopping histedit for amending commit %s\n",
6685 got_worktree_rebase_pathlist_free(&merged_paths);
6686 error = got_worktree_histedit_postpone(worktree,
6691 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6692 error = histedit_skip_commit(hle, worktree, repo);
6698 error = histedit_commit(&merged_paths, worktree, fileindex,
6699 tmp_branch, hle, repo);
6700 got_worktree_rebase_pathlist_free(&merged_paths);
6705 if (rebase_status == GOT_STATUS_CONFLICT) {
6706 error = got_worktree_histedit_postpone(worktree, fileindex);
6709 error = got_error_msg(GOT_ERR_CONFLICTS,
6710 "conflicts must be resolved before rebasing can continue");
6712 error = histedit_complete(worktree, fileindex, tmp_branch,
6715 got_object_id_queue_free(&commits);
6716 histedit_free_list(&histedit_cmds);
6717 free(head_commit_id);
6718 free(base_commit_id);
6719 free(resume_commit_id);
6721 got_object_commit_close(commit);
6723 got_ref_close(branch);
6725 got_ref_close(tmp_branch);
6727 got_worktree_close(worktree);
6729 got_repo_close(repo);
6734 usage_integrate(void)
6736 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6740 static const struct got_error *
6741 cmd_integrate(int argc, char *argv[])
6743 const struct got_error *error = NULL;
6744 struct got_repository *repo = NULL;
6745 struct got_worktree *worktree = NULL;
6746 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6747 const char *branch_arg = NULL;
6748 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6749 struct got_fileindex *fileindex = NULL;
6750 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6751 int ch, did_something = 0;
6753 while ((ch = getopt(argc, argv, "")) != -1) {
6766 branch_arg = argv[0];
6768 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6769 "unveil", NULL) == -1)
6772 cwd = getcwd(NULL, 0);
6774 error = got_error_from_errno("getcwd");
6778 error = got_worktree_open(&worktree, cwd);
6782 error = check_rebase_or_histedit_in_progress(worktree);
6786 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6791 error = apply_unveil(got_repo_get_path(repo), 0,
6792 got_worktree_get_root_path(worktree));
6796 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6797 error = got_error_from_errno("asprintf");
6801 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6802 &base_branch_ref, worktree, refname, repo);
6806 refname = strdup(got_ref_get_name(branch_ref));
6807 if (refname == NULL) {
6808 error = got_error_from_errno("strdup");
6809 got_worktree_integrate_abort(worktree, fileindex, repo,
6810 branch_ref, base_branch_ref);
6813 base_refname = strdup(got_ref_get_name(base_branch_ref));
6814 if (base_refname == NULL) {
6815 error = got_error_from_errno("strdup");
6816 got_worktree_integrate_abort(worktree, fileindex, repo,
6817 branch_ref, base_branch_ref);
6821 error = got_ref_resolve(&commit_id, repo, branch_ref);
6825 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6829 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6830 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6831 "specified branch has already been integrated");
6832 got_worktree_integrate_abort(worktree, fileindex, repo,
6833 branch_ref, base_branch_ref);
6837 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6839 if (error->code == GOT_ERR_ANCESTRY)
6840 error = got_error(GOT_ERR_REBASE_REQUIRED);
6841 got_worktree_integrate_abort(worktree, fileindex, repo,
6842 branch_ref, base_branch_ref);
6846 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6847 branch_ref, base_branch_ref, update_progress, &did_something,
6848 check_cancelled, NULL);
6852 printf("Integrated %s into %s\n", refname, base_refname);
6855 got_repo_close(repo);
6857 got_worktree_close(worktree);
6859 free(base_commit_id);
6869 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6870 "[file-path ...]\n",
6875 static const struct got_error *
6876 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6877 const char *path, struct got_object_id *blob_id,
6878 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6879 int dirfd, const char *de_name)
6881 const struct got_error *err = NULL;
6882 char *id_str = NULL;
6884 if (staged_status != GOT_STATUS_ADD &&
6885 staged_status != GOT_STATUS_MODIFY &&
6886 staged_status != GOT_STATUS_DELETE)
6889 if (staged_status == GOT_STATUS_ADD ||
6890 staged_status == GOT_STATUS_MODIFY)
6891 err = got_object_id_str(&id_str, staged_blob_id);
6893 err = got_object_id_str(&id_str, blob_id);
6897 printf("%s %c %s\n", id_str, staged_status, path);
6902 static const struct got_error *
6903 cmd_stage(int argc, char *argv[])
6905 const struct got_error *error = NULL;
6906 struct got_repository *repo = NULL;
6907 struct got_worktree *worktree = NULL;
6909 struct got_pathlist_head paths;
6910 struct got_pathlist_entry *pe;
6911 int ch, list_stage = 0, pflag = 0;
6912 FILE *patch_script_file = NULL;
6913 const char *patch_script_path = NULL;
6914 struct choose_patch_arg cpa;
6918 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6927 patch_script_path = optarg;
6939 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6940 "unveil", NULL) == -1)
6943 if (list_stage && (pflag || patch_script_path))
6944 errx(1, "-l option cannot be used with other options");
6945 if (patch_script_path && !pflag)
6946 errx(1, "-F option can only be used together with -p option");
6948 cwd = getcwd(NULL, 0);
6950 error = got_error_from_errno("getcwd");
6954 error = got_worktree_open(&worktree, cwd);
6958 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6963 if (patch_script_path) {
6964 patch_script_file = fopen(patch_script_path, "r");
6965 if (patch_script_file == NULL) {
6966 error = got_error_from_errno2("fopen",
6971 error = apply_unveil(got_repo_get_path(repo), 0,
6972 got_worktree_get_root_path(worktree));
6976 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6981 error = got_worktree_status(worktree, &paths, repo,
6982 print_stage, NULL, check_cancelled, NULL);
6984 cpa.patch_script_file = patch_script_file;
6985 cpa.action = "stage";
6986 error = got_worktree_stage(worktree, &paths,
6987 pflag ? NULL : print_status, NULL,
6988 pflag ? choose_patch : NULL, &cpa, repo);
6991 if (patch_script_file && fclose(patch_script_file) == EOF &&
6993 error = got_error_from_errno2("fclose", patch_script_path);
6995 got_repo_close(repo);
6997 got_worktree_close(worktree);
6998 TAILQ_FOREACH(pe, &paths, entry)
6999 free((char *)pe->path);
7000 got_pathlist_free(&paths);
7008 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7009 "[file-path ...]\n",
7015 static const struct got_error *
7016 cmd_unstage(int argc, char *argv[])
7018 const struct got_error *error = NULL;
7019 struct got_repository *repo = NULL;
7020 struct got_worktree *worktree = NULL;
7022 struct got_pathlist_head paths;
7023 struct got_pathlist_entry *pe;
7024 int ch, did_something = 0, pflag = 0;
7025 FILE *patch_script_file = NULL;
7026 const char *patch_script_path = NULL;
7027 struct choose_patch_arg cpa;
7031 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7037 patch_script_path = optarg;
7049 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7050 "unveil", NULL) == -1)
7053 if (patch_script_path && !pflag)
7054 errx(1, "-F option can only be used together with -p option");
7056 cwd = getcwd(NULL, 0);
7058 error = got_error_from_errno("getcwd");
7062 error = got_worktree_open(&worktree, cwd);
7066 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7071 if (patch_script_path) {
7072 patch_script_file = fopen(patch_script_path, "r");
7073 if (patch_script_file == NULL) {
7074 error = got_error_from_errno2("fopen",
7080 error = apply_unveil(got_repo_get_path(repo), 0,
7081 got_worktree_get_root_path(worktree));
7085 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7089 cpa.patch_script_file = patch_script_file;
7090 cpa.action = "unstage";
7091 error = got_worktree_unstage(worktree, &paths, update_progress,
7092 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7094 if (patch_script_file && fclose(patch_script_file) == EOF &&
7096 error = got_error_from_errno2("fclose", patch_script_path);
7098 got_repo_close(repo);
7100 got_worktree_close(worktree);
7101 TAILQ_FOREACH(pe, &paths, entry)
7102 free((char *)pe->path);
7103 got_pathlist_free(&paths);
7111 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7112 "arg1 [arg2 ...]\n", getprogname());
7116 static const struct got_error *
7117 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7119 const struct got_error *err;
7120 struct got_blob_object *blob;
7122 err = got_object_open_as_blob(&blob, repo, id, 8192);
7126 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7127 got_object_blob_close(blob);
7131 static const struct got_error *
7132 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7134 const struct got_error *err;
7135 struct got_tree_object *tree;
7138 err = got_object_open_as_tree(&tree, repo, id);
7142 nentries = got_object_tree_get_nentries(tree);
7143 for (i = 0; i < nentries; i++) {
7144 struct got_tree_entry *te;
7146 if (sigint_received || sigpipe_received)
7148 te = got_object_tree_get_entry(tree, i);
7149 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7152 fprintf(outfile, "%s %.7o %s\n", id_str,
7153 got_tree_entry_get_mode(te),
7154 got_tree_entry_get_name(te));
7158 got_object_tree_close(tree);
7162 static const struct got_error *
7163 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7165 const struct got_error *err;
7166 struct got_commit_object *commit;
7167 const struct got_object_id_queue *parent_ids;
7168 struct got_object_qid *pid;
7169 char *id_str = NULL;
7170 const char *logmsg = NULL;
7172 err = got_object_open_as_commit(&commit, repo, id);
7176 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7180 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7181 parent_ids = got_object_commit_get_parent_ids(commit);
7182 fprintf(outfile, "numparents %d\n",
7183 got_object_commit_get_nparents(commit));
7184 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7186 err = got_object_id_str(&pid_str, pid->id);
7189 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7192 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7193 got_object_commit_get_author(commit),
7194 got_object_commit_get_author_time(commit));
7196 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7197 got_object_commit_get_author(commit),
7198 got_object_commit_get_committer_time(commit));
7200 logmsg = got_object_commit_get_logmsg_raw(commit);
7201 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7202 fprintf(outfile, "%s", logmsg);
7205 got_object_commit_close(commit);
7209 static const struct got_error *
7210 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7212 const struct got_error *err;
7213 struct got_tag_object *tag;
7214 char *id_str = NULL;
7215 const char *tagmsg = NULL;
7217 err = got_object_open_as_tag(&tag, repo, id);
7221 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7225 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7227 switch (got_object_tag_get_object_type(tag)) {
7228 case GOT_OBJ_TYPE_BLOB:
7229 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7230 GOT_OBJ_LABEL_BLOB);
7232 case GOT_OBJ_TYPE_TREE:
7233 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7234 GOT_OBJ_LABEL_TREE);
7236 case GOT_OBJ_TYPE_COMMIT:
7237 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7238 GOT_OBJ_LABEL_COMMIT);
7240 case GOT_OBJ_TYPE_TAG:
7241 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7248 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7249 got_object_tag_get_name(tag));
7251 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7252 got_object_tag_get_tagger(tag),
7253 got_object_tag_get_tagger_time(tag));
7255 tagmsg = got_object_tag_get_message(tag);
7256 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7257 fprintf(outfile, "%s", tagmsg);
7260 got_object_tag_close(tag);
7264 static const struct got_error *
7265 cmd_cat(int argc, char *argv[])
7267 const struct got_error *error;
7268 struct got_repository *repo = NULL;
7269 struct got_worktree *worktree = NULL;
7270 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7271 const char *commit_id_str = NULL;
7272 struct got_object_id *id = NULL, *commit_id = NULL;
7273 int ch, obj_type, i, force_path = 0;
7276 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7281 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7284 commit_id_str = optarg;
7287 repo_path = realpath(optarg, NULL);
7288 if (repo_path == NULL)
7289 return got_error_from_errno2("realpath",
7291 got_path_strip_trailing_slashes(repo_path);
7305 cwd = getcwd(NULL, 0);
7307 error = got_error_from_errno("getcwd");
7310 error = got_worktree_open(&worktree, cwd);
7311 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7314 if (repo_path == NULL) {
7316 got_worktree_get_repo_path(worktree));
7317 if (repo_path == NULL) {
7318 error = got_error_from_errno("strdup");
7324 if (repo_path == NULL) {
7325 repo_path = getcwd(NULL, 0);
7326 if (repo_path == NULL)
7327 return got_error_from_errno("getcwd");
7330 error = got_repo_open(&repo, repo_path, NULL);
7335 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7339 if (commit_id_str == NULL)
7340 commit_id_str = GOT_REF_HEAD;
7341 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7345 for (i = 0; i < argc; i++) {
7347 error = got_object_id_by_path(&id, repo, commit_id,
7352 error = match_object_id(&id, &label, argv[i],
7353 GOT_OBJ_TYPE_ANY, 0, repo);
7355 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7356 error->code != GOT_ERR_NOT_REF)
7358 error = got_object_id_by_path(&id, repo,
7359 commit_id, argv[i]);
7365 error = got_object_get_type(&obj_type, repo, id);
7370 case GOT_OBJ_TYPE_BLOB:
7371 error = cat_blob(id, repo, stdout);
7373 case GOT_OBJ_TYPE_TREE:
7374 error = cat_tree(id, repo, stdout);
7376 case GOT_OBJ_TYPE_COMMIT:
7377 error = cat_commit(id, repo, stdout);
7379 case GOT_OBJ_TYPE_TAG:
7380 error = cat_tag(id, repo, stdout);
7383 error = got_error(GOT_ERR_OBJ_TYPE);
7399 got_worktree_close(worktree);
7401 const struct got_error *repo_error;
7402 repo_error = got_repo_close(repo);