2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
40 #include "got_version.h"
41 #include "got_error.h"
42 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
46 #include "got_cancel.h"
47 #include "got_worktree.h"
49 #include "got_commit_graph.h"
50 #include "got_blame.h"
51 #include "got_privsep.h"
52 #include "got_opentemp.h"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
58 static volatile sig_atomic_t sigint_received;
59 static volatile sig_atomic_t sigpipe_received;
62 catch_sigint(int signo)
68 catch_sigpipe(int signo)
76 const struct got_error *(*cmd_main)(int, char *[]);
77 void (*cmd_usage)(void);
78 const char *cmd_alias;
81 __dead static void usage(int);
82 __dead static void usage_init(void);
83 __dead static void usage_import(void);
84 __dead static void usage_checkout(void);
85 __dead static void usage_update(void);
86 __dead static void usage_log(void);
87 __dead static void usage_diff(void);
88 __dead static void usage_blame(void);
89 __dead static void usage_tree(void);
90 __dead static void usage_status(void);
91 __dead static void usage_ref(void);
92 __dead static void usage_branch(void);
93 __dead static void usage_tag(void);
94 __dead static void usage_add(void);
95 __dead static void usage_remove(void);
96 __dead static void usage_revert(void);
97 __dead static void usage_commit(void);
98 __dead static void usage_cherrypick(void);
99 __dead static void usage_backout(void);
100 __dead static void usage_rebase(void);
101 __dead static void usage_histedit(void);
102 __dead static void usage_integrate(void);
103 __dead static void usage_stage(void);
104 __dead static void usage_unstage(void);
105 __dead static void usage_cat(void);
107 static const struct got_error* cmd_init(int, char *[]);
108 static const struct got_error* cmd_import(int, char *[]);
109 static const struct got_error* cmd_checkout(int, char *[]);
110 static const struct got_error* cmd_update(int, char *[]);
111 static const struct got_error* cmd_log(int, char *[]);
112 static const struct got_error* cmd_diff(int, char *[]);
113 static const struct got_error* cmd_blame(int, char *[]);
114 static const struct got_error* cmd_tree(int, char *[]);
115 static const struct got_error* cmd_status(int, char *[]);
116 static const struct got_error* cmd_ref(int, char *[]);
117 static const struct got_error* cmd_branch(int, char *[]);
118 static const struct got_error* cmd_tag(int, char *[]);
119 static const struct got_error* cmd_add(int, char *[]);
120 static const struct got_error* cmd_remove(int, char *[]);
121 static const struct got_error* cmd_revert(int, char *[]);
122 static const struct got_error* cmd_commit(int, char *[]);
123 static const struct got_error* cmd_cherrypick(int, char *[]);
124 static const struct got_error* cmd_backout(int, char *[]);
125 static const struct got_error* cmd_rebase(int, char *[]);
126 static const struct got_error* cmd_histedit(int, char *[]);
127 static const struct got_error* cmd_integrate(int, char *[]);
128 static const struct got_error* cmd_stage(int, char *[]);
129 static const struct got_error* cmd_unstage(int, char *[]);
130 static const struct got_error* cmd_cat(int, char *[]);
132 static struct got_cmd got_commands[] = {
133 { "init", cmd_init, usage_init, "in" },
134 { "import", cmd_import, usage_import, "im" },
135 { "checkout", cmd_checkout, usage_checkout, "co" },
136 { "update", cmd_update, usage_update, "up" },
137 { "log", cmd_log, usage_log, "" },
138 { "diff", cmd_diff, usage_diff, "di" },
139 { "blame", cmd_blame, usage_blame, "bl" },
140 { "tree", cmd_tree, usage_tree, "tr" },
141 { "status", cmd_status, usage_status, "st" },
142 { "ref", cmd_ref, usage_ref, "" },
143 { "branch", cmd_branch, usage_branch, "br" },
144 { "tag", cmd_tag, usage_tag, "" },
145 { "add", cmd_add, usage_add, "" },
146 { "remove", cmd_remove, usage_remove, "rm" },
147 { "revert", cmd_revert, usage_revert, "rv" },
148 { "commit", cmd_commit, usage_commit, "ci" },
149 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 { "backout", cmd_backout, usage_backout, "bo" },
151 { "rebase", cmd_rebase, usage_rebase, "rb" },
152 { "histedit", cmd_histedit, usage_histedit, "he" },
153 { "integrate", cmd_integrate, usage_integrate,"ig" },
154 { "stage", cmd_stage, usage_stage, "sg" },
155 { "unstage", cmd_unstage, usage_unstage, "ug" },
156 { "cat", cmd_cat, usage_cat, "" },
164 fprintf(stderr, "commands:");
165 for (i = 0; i < nitems(got_commands); i++) {
166 struct got_cmd *cmd = &got_commands[i];
167 fprintf(stderr, " %s", cmd->cmd_name);
173 main(int argc, char *argv[])
178 int hflag = 0, Vflag = 0;
180 setlocale(LC_CTYPE, "");
182 while ((ch = getopt(argc, argv, "hV")) != -1) {
201 got_version_print_str();
208 signal(SIGINT, catch_sigint);
209 signal(SIGPIPE, catch_sigpipe);
211 for (i = 0; i < nitems(got_commands); i++) {
212 const struct got_error *error;
214 cmd = &got_commands[i];
216 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 strcmp(cmd->cmd_alias, argv[0]) != 0)
221 got_commands[i].cmd_usage();
223 error = got_commands[i].cmd_main(argc, argv);
224 if (error && error->code != GOT_ERR_CANCELLED &&
225 error->code != GOT_ERR_PRIVSEP_EXIT &&
226 !(sigpipe_received &&
227 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
229 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
237 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
245 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
252 static const struct got_error *
253 get_editor(char **abspath)
255 const struct got_error *err = NULL;
260 editor = getenv("VISUAL");
262 editor = getenv("EDITOR");
265 err = got_path_find_prog(abspath, editor);
270 if (*abspath == NULL) {
271 *abspath = strdup("/bin/ed");
272 if (*abspath == NULL)
273 return got_error_from_errno("strdup");
279 static const struct got_error *
280 apply_unveil(const char *repo_path, int repo_read_only,
281 const char *worktree_path)
283 const struct got_error *err;
286 if (unveil("gmon.out", "rwc") != 0)
287 return got_error_from_errno2("unveil", "gmon.out");
289 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 return got_error_from_errno2("unveil", repo_path);
292 if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 return got_error_from_errno2("unveil", worktree_path);
295 if (unveil("/tmp", "rwc") != 0)
296 return got_error_from_errno2("unveil", "/tmp");
298 err = got_privsep_unveil_exec_helpers();
302 if (unveil(NULL, NULL) != 0)
303 return got_error_from_errno("unveil");
311 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
315 static const struct got_error *
316 cmd_init(int argc, char *argv[])
318 const struct got_error *error = NULL;
319 char *repo_path = NULL;
322 while ((ch = getopt(argc, argv, "")) != -1) {
334 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 repo_path = strdup(argv[0]);
341 if (repo_path == NULL)
342 return got_error_from_errno("strdup");
344 got_path_strip_trailing_slashes(repo_path);
346 error = got_path_mkdir(repo_path);
348 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
351 error = apply_unveil(repo_path, 0, NULL);
355 error = got_repo_init(repo_path);
367 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 "[-r repository-path] [-I pattern] path\n", getprogname());
373 spawn_editor(const char *editor, const char *file)
376 sig_t sighup, sigint, sigquit;
379 sighup = signal(SIGHUP, SIG_IGN);
380 sigint = signal(SIGINT, SIG_IGN);
381 sigquit = signal(SIGQUIT, SIG_IGN);
383 switch (pid = fork()) {
387 execl(editor, editor, file, (char *)NULL);
391 while (waitpid(pid, &st, 0) == -1)
396 (void)signal(SIGHUP, sighup);
397 (void)signal(SIGINT, sigint);
398 (void)signal(SIGQUIT, sigquit);
400 if (!WIFEXITED(st)) {
405 return WEXITSTATUS(st);
408 static const struct got_error *
409 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 const char *initial_content)
412 const struct got_error *err = NULL;
416 int content_changed = 0;
421 if (stat(logmsg_path, &st) == -1)
422 return got_error_from_errno2("stat", logmsg_path);
424 if (spawn_editor(editor, logmsg_path) == -1)
425 return got_error_from_errno("failed spawning editor");
427 if (stat(logmsg_path, &st2) == -1)
428 return got_error_from_errno("stat");
430 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 "no changes made to commit message, aborting");
434 *logmsg = malloc(st2.st_size + 1);
436 return got_error_from_errno("malloc");
440 fp = fopen(logmsg_path, "r");
442 err = got_error_from_errno("fopen");
445 while (fgets(buf, sizeof(buf), fp) != NULL) {
446 if (!content_changed && strcmp(buf, initial_content) != 0)
448 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 continue; /* remove comments and leading empty lines */
450 len = strlcat(*logmsg, buf, st2.st_size);
454 while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 (*logmsg)[len - 1] = '\0';
459 if (len == 0 || !content_changed)
460 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 "commit message cannot be empty, aborting");
470 static const struct got_error *
471 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 const char *path_dir, const char *branch_name)
474 char *initial_content = NULL;
475 const struct got_error *err = NULL;
478 if (asprintf(&initial_content,
479 "\n# %s to be imported to branch %s\n", path_dir,
481 return got_error_from_errno("asprintf");
483 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
487 dprintf(fd, initial_content);
490 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
492 free(initial_content);
496 static const struct got_error *
497 import_progress(void *arg, const char *path)
499 printf("A %s\n", path);
503 static const struct got_error *
504 get_author(char **author, struct got_repository *repo)
506 const struct got_error *err = NULL;
507 const char *got_author, *name, *email;
511 name = got_repo_get_gitconfig_author_name(repo);
512 email = got_repo_get_gitconfig_author_email(repo);
514 if (asprintf(author, "%s <%s>", name, email) == -1)
515 return got_error_from_errno("asprintf");
519 got_author = getenv("GOT_AUTHOR");
520 if (got_author == NULL) {
521 name = got_repo_get_global_gitconfig_author_name(repo);
522 email = got_repo_get_global_gitconfig_author_email(repo);
524 if (asprintf(author, "%s <%s>", name, email) == -1)
525 return got_error_from_errno("asprintf");
528 /* TODO: Look up user in password database? */
529 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 *author = strdup(got_author);
534 return got_error_from_errno("strdup");
537 * Really dumb email address check; we're only doing this to
538 * avoid git's object parser breaking on commits we create.
540 while (*got_author && *got_author != '<')
542 if (*got_author != '<') {
543 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 while (*got_author && *got_author != '@')
548 if (*got_author != '@') {
549 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 while (*got_author && *got_author != '>')
554 if (*got_author != '>')
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 static const struct got_error *
565 get_gitconfig_path(char **gitconfig_path)
567 const char *homedir = getenv("HOME");
569 *gitconfig_path = NULL;
571 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 return got_error_from_errno("asprintf");
578 static const struct got_error *
579 cmd_import(int argc, char *argv[])
581 const struct got_error *error = NULL;
582 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 const char *branch_name = "main";
585 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 struct got_repository *repo = NULL;
587 struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 struct got_object_id *new_commit_id = NULL;
590 struct got_pathlist_head ignores;
591 struct got_pathlist_entry *pe;
592 int preserve_logmsg = 0;
594 TAILQ_INIT(&ignores);
596 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 branch_name = optarg;
602 logmsg = strdup(optarg);
603 if (logmsg == NULL) {
604 error = got_error_from_errno("strdup");
609 repo_path = realpath(optarg, NULL);
610 if (repo_path == NULL) {
611 error = got_error_from_errno2("realpath",
617 if (optarg[0] == '\0')
619 error = got_pathlist_insert(&pe, &ignores, optarg,
634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
642 if (repo_path == NULL) {
643 repo_path = getcwd(NULL, 0);
644 if (repo_path == NULL)
645 return got_error_from_errno("getcwd");
647 got_path_strip_trailing_slashes(repo_path);
648 error = get_gitconfig_path(&gitconfig_path);
651 error = got_repo_open(&repo, repo_path, gitconfig_path);
655 error = get_author(&author, repo);
660 * Don't let the user create a branch name with a leading '-'.
661 * While technically a valid reference name, this case is usually
662 * an unintended typo.
664 if (branch_name[0] == '-')
665 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
667 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 error = got_error_from_errno("asprintf");
672 error = got_ref_open(&branch_ref, repo, refname, 0);
674 if (error->code != GOT_ERR_NOT_REF)
677 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 "import target branch already exists");
682 path_dir = realpath(argv[0], NULL);
683 if (path_dir == NULL) {
684 error = got_error_from_errno2("realpath", argv[0]);
687 got_path_strip_trailing_slashes(path_dir);
690 * unveil(2) traverses exec(2); if an editor is used we have
691 * to apply unveil after the log message has been written.
693 if (logmsg == NULL || strlen(logmsg) == 0) {
694 error = get_editor(&editor);
698 error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
708 if (unveil(path_dir, "r") != 0) {
709 error = got_error_from_errno2("unveil", path_dir);
715 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
722 error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 author, &ignores, repo, import_progress, NULL);
730 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
737 error = got_ref_write(branch_ref, repo);
744 error = got_object_id_str(&id_str, new_commit_id);
751 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
753 if (error->code != GOT_ERR_NOT_REF) {
759 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
767 error = got_ref_write(head_ref, repo);
775 printf("Created branch %s with commit %s\n",
776 got_ref_get_name(branch_ref), id_str);
778 if (preserve_logmsg) {
779 fprintf(stderr, "%s: log message preserved in %s\n",
780 getprogname(), logmsg_path);
781 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 error = got_error_from_errno2("unlink", logmsg_path);
791 free(gitconfig_path);
793 got_ref_close(branch_ref);
795 got_ref_close(head_ref);
802 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 "[-p prefix] repository-path [worktree-path]\n", getprogname());
808 show_worktree_base_ref_warning(void)
810 fprintf(stderr, "%s: warning: could not create a reference "
811 "to the work tree's base commit; the commit could be "
812 "garbage-collected by Git; making the repository "
813 "writable and running 'got update' will prevent this\n",
817 struct got_checkout_progress_arg {
818 const char *worktree_path;
819 int had_base_commit_ref_error;
822 static const struct got_error *
823 checkout_progress(void *arg, unsigned char status, const char *path)
825 struct got_checkout_progress_arg *a = arg;
827 /* Base commit bump happens silently. */
828 if (status == GOT_STATUS_BUMP_BASE)
831 if (status == GOT_STATUS_BASE_REF_ERR) {
832 a->had_base_commit_ref_error = 1;
836 while (path[0] == '/')
839 printf("%c %s/%s\n", status, a->worktree_path, path);
843 static const struct got_error *
844 check_cancelled(void *arg)
846 if (sigint_received || sigpipe_received)
847 return got_error(GOT_ERR_CANCELLED);
851 static const struct got_error *
852 check_linear_ancestry(struct got_object_id *commit_id,
853 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 struct got_repository *repo)
856 const struct got_error *err = NULL;
857 struct got_object_id *yca_id;
859 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 commit_id, base_commit_id, repo, check_cancelled, NULL);
865 return got_error(GOT_ERR_ANCESTRY);
868 * Require a straight line of history between the target commit
869 * and the work tree's base commit.
871 * Non-linear situations such as this require a rebase:
873 * (commit) D F (base_commit)
881 * 'got update' only handles linear cases:
882 * Update forwards in time: A (base/yca) - B - C - D (commit)
883 * Update backwards in time: D (base) - C - B - A (commit/yca)
885 if (allow_forwards_in_time_only) {
886 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 return got_error(GOT_ERR_ANCESTRY);
888 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 got_object_id_cmp(base_commit_id, yca_id) != 0)
890 return got_error(GOT_ERR_ANCESTRY);
896 static const struct got_error *
897 check_same_branch(struct got_object_id *commit_id,
898 struct got_reference *head_ref, struct got_object_id *yca_id,
899 struct got_repository *repo)
901 const struct got_error *err = NULL;
902 struct got_commit_graph *graph = NULL;
903 struct got_object_id *head_commit_id = NULL;
904 int is_same_branch = 0;
906 err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
919 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
923 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 check_cancelled, NULL);
929 struct got_object_id *id;
930 err = got_commit_graph_iter_next(&id, graph);
932 if (err->code == GOT_ERR_ITER_COMPLETED) {
935 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
937 err = got_commit_graph_fetch_commits(graph, 1,
938 repo, check_cancelled, NULL);
944 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
946 if (got_object_id_cmp(id, commit_id) == 0) {
954 got_commit_graph_close(graph);
955 free(head_commit_id);
956 if (!err && !is_same_branch)
957 err = got_error(GOT_ERR_ANCESTRY);
961 static const struct got_error *
962 resolve_commit_arg(struct got_object_id **commit_id,
963 const char *commit_id_arg, struct got_repository *repo)
965 const struct got_error *err;
966 struct got_reference *ref;
967 struct got_tag_object *tag;
969 err = got_repo_object_match_tag(&tag, commit_id_arg,
970 GOT_OBJ_TYPE_COMMIT, repo);
972 *commit_id = got_object_id_dup(
973 got_object_tag_get_object_id(tag));
974 if (*commit_id == NULL)
975 err = got_error_from_errno("got_object_id_dup");
976 got_object_tag_close(tag);
978 } else if (err->code != GOT_ERR_NO_OBJ)
981 err = got_ref_open(&ref, repo, commit_id_arg, 0);
983 err = got_ref_resolve(commit_id, repo, ref);
986 if (err->code != GOT_ERR_NOT_REF)
988 err = got_repo_match_object_id_prefix(commit_id,
989 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
994 static const struct got_error *
995 cmd_checkout(int argc, char *argv[])
997 const struct got_error *error = NULL;
998 struct got_repository *repo = NULL;
999 struct got_reference *head_ref = NULL;
1000 struct got_worktree *worktree = NULL;
1001 char *repo_path = NULL;
1002 char *worktree_path = NULL;
1003 const char *path_prefix = "";
1004 const char *branch_name = GOT_REF_HEAD;
1005 char *commit_id_str = NULL;
1006 int ch, same_path_prefix;
1007 struct got_pathlist_head paths;
1008 struct got_checkout_progress_arg cpa;
1012 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1015 branch_name = optarg;
1018 commit_id_str = strdup(optarg);
1019 if (commit_id_str == NULL)
1020 return got_error_from_errno("strdup");
1023 path_prefix = optarg;
1035 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1036 "unveil", NULL) == -1)
1040 char *cwd, *base, *dotgit;
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL)
1043 return got_error_from_errno2("realpath", argv[0]);
1044 cwd = getcwd(NULL, 0);
1046 error = got_error_from_errno("getcwd");
1049 if (path_prefix[0]) {
1050 base = basename(path_prefix);
1052 error = got_error_from_errno2("basename",
1057 base = basename(repo_path);
1059 error = got_error_from_errno2("basename",
1064 dotgit = strstr(base, ".git");
1067 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1068 error = got_error_from_errno("asprintf");
1073 } else if (argc == 2) {
1074 repo_path = realpath(argv[0], NULL);
1075 if (repo_path == NULL) {
1076 error = got_error_from_errno2("realpath", argv[0]);
1079 worktree_path = realpath(argv[1], NULL);
1080 if (worktree_path == NULL) {
1081 if (errno != ENOENT) {
1082 error = got_error_from_errno2("realpath",
1086 worktree_path = strdup(argv[1]);
1087 if (worktree_path == NULL) {
1088 error = got_error_from_errno("strdup");
1095 got_path_strip_trailing_slashes(repo_path);
1096 got_path_strip_trailing_slashes(worktree_path);
1098 error = got_repo_open(&repo, repo_path, NULL);
1102 /* Pre-create work tree path for unveil(2) */
1103 error = got_path_mkdir(worktree_path);
1105 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1106 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1108 if (!got_path_dir_is_empty(worktree_path)) {
1109 error = got_error_path(worktree_path,
1110 GOT_ERR_DIR_NOT_EMPTY);
1115 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1119 error = got_ref_open(&head_ref, repo, branch_name, 0);
1123 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1124 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1127 error = got_worktree_open(&worktree, worktree_path);
1131 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1135 if (!same_path_prefix) {
1136 error = got_error(GOT_ERR_PATH_PREFIX);
1140 if (commit_id_str) {
1141 struct got_object_id *commit_id;
1142 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1145 error = check_linear_ancestry(commit_id,
1146 got_worktree_get_base_commit_id(worktree), 0, repo);
1147 if (error != NULL) {
1151 error = check_same_branch(commit_id, head_ref, NULL, repo);
1154 error = got_worktree_set_base_commit_id(worktree, repo,
1161 error = got_pathlist_append(&paths, "", NULL);
1164 cpa.worktree_path = worktree_path;
1165 cpa.had_base_commit_ref_error = 0;
1166 error = got_worktree_checkout_files(worktree, &paths, repo,
1167 checkout_progress, &cpa, check_cancelled, NULL);
1171 printf("Now shut up and hack\n");
1172 if (cpa.had_base_commit_ref_error)
1173 show_worktree_base_ref_warning();
1176 got_pathlist_free(&paths);
1177 free(commit_id_str);
1179 free(worktree_path);
1186 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1191 static const struct got_error *
1192 update_progress(void *arg, unsigned char status, const char *path)
1194 int *did_something = arg;
1196 if (status == GOT_STATUS_EXISTS ||
1197 status == GOT_STATUS_BASE_REF_ERR)
1202 /* Base commit bump happens silently. */
1203 if (status == GOT_STATUS_BUMP_BASE)
1206 while (path[0] == '/')
1208 printf("%c %s\n", status, path);
1212 static const struct got_error *
1213 switch_head_ref(struct got_reference *head_ref,
1214 struct got_object_id *commit_id, struct got_worktree *worktree,
1215 struct got_repository *repo)
1217 const struct got_error *err = NULL;
1219 int ref_has_moved = 0;
1221 /* Trivial case: switching between two different references. */
1222 if (strcmp(got_ref_get_name(head_ref),
1223 got_worktree_get_head_ref_name(worktree)) != 0) {
1224 printf("Switching work tree from %s to %s\n",
1225 got_worktree_get_head_ref_name(worktree),
1226 got_ref_get_name(head_ref));
1227 return got_worktree_set_head_ref(worktree, head_ref);
1230 err = check_linear_ancestry(commit_id,
1231 got_worktree_get_base_commit_id(worktree), 0, repo);
1233 if (err->code != GOT_ERR_ANCESTRY)
1240 /* Switching to a rebased branch with the same reference name. */
1241 err = got_object_id_str(&base_id_str,
1242 got_worktree_get_base_commit_id(worktree));
1245 printf("Reference %s now points at a different branch\n",
1246 got_worktree_get_head_ref_name(worktree));
1247 printf("Switching work tree from %s to %s\n", base_id_str,
1248 got_worktree_get_head_ref_name(worktree));
1252 static const struct got_error *
1253 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1255 const struct got_error *err;
1258 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1262 return got_error(GOT_ERR_REBASING);
1264 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1268 return got_error(GOT_ERR_HISTEDIT_BUSY);
1273 static const struct got_error *
1274 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1275 char *argv[], struct got_worktree *worktree)
1277 const struct got_error *err = NULL;
1284 return got_error_from_errno("strdup");
1285 return got_pathlist_append(paths, path, NULL);
1288 for (i = 0; i < argc; i++) {
1289 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1292 err = got_pathlist_append(paths, path, NULL);
1302 static const struct got_error *
1303 cmd_update(int argc, char *argv[])
1305 const struct got_error *error = NULL;
1306 struct got_repository *repo = NULL;
1307 struct got_worktree *worktree = NULL;
1308 char *worktree_path = NULL;
1309 struct got_object_id *commit_id = NULL;
1310 char *commit_id_str = NULL;
1311 const char *branch_name = NULL;
1312 struct got_reference *head_ref = NULL;
1313 struct got_pathlist_head paths;
1314 struct got_pathlist_entry *pe;
1315 int ch, did_something = 0;
1319 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1322 branch_name = optarg;
1325 commit_id_str = strdup(optarg);
1326 if (commit_id_str == NULL)
1327 return got_error_from_errno("strdup");
1339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1340 "unveil", NULL) == -1)
1343 worktree_path = getcwd(NULL, 0);
1344 if (worktree_path == NULL) {
1345 error = got_error_from_errno("getcwd");
1348 error = got_worktree_open(&worktree, worktree_path);
1352 error = check_rebase_or_histedit_in_progress(worktree);
1356 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1361 error = apply_unveil(got_repo_get_path(repo), 0,
1362 got_worktree_get_root_path(worktree));
1366 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1370 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1371 got_worktree_get_head_ref_name(worktree), 0);
1374 if (commit_id_str == NULL) {
1375 error = got_ref_resolve(&commit_id, repo, head_ref);
1378 error = got_object_id_str(&commit_id_str, commit_id);
1382 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1383 free(commit_id_str);
1384 commit_id_str = NULL;
1387 error = got_object_id_str(&commit_id_str, commit_id);
1393 struct got_object_id *head_commit_id;
1394 TAILQ_FOREACH(pe, &paths, entry) {
1395 if (pe->path_len == 0)
1397 error = got_error_msg(GOT_ERR_BAD_PATH,
1398 "switching between branches requires that "
1399 "the entire work tree gets updated");
1402 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1405 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1407 free(head_commit_id);
1410 error = check_same_branch(commit_id, head_ref, NULL, repo);
1413 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1417 error = check_linear_ancestry(commit_id,
1418 got_worktree_get_base_commit_id(worktree), 0, repo);
1419 if (error != NULL) {
1420 if (error->code == GOT_ERR_ANCESTRY)
1421 error = got_error(GOT_ERR_BRANCH_MOVED);
1424 error = check_same_branch(commit_id, head_ref, NULL, repo);
1429 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1431 error = got_worktree_set_base_commit_id(worktree, repo,
1437 error = got_worktree_checkout_files(worktree, &paths, repo,
1438 update_progress, &did_something, check_cancelled, NULL);
1443 printf("Updated to commit %s\n", commit_id_str);
1445 printf("Already up-to-date\n");
1447 free(worktree_path);
1448 TAILQ_FOREACH(pe, &paths, entry)
1449 free((char *)pe->path);
1450 got_pathlist_free(&paths);
1452 free(commit_id_str);
1456 static const struct got_error *
1457 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1458 const char *path, int diff_context, int ignore_whitespace,
1459 struct got_repository *repo)
1461 const struct got_error *err = NULL;
1462 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1465 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1470 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1474 while (path[0] == '/')
1476 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1477 ignore_whitespace, stdout);
1480 got_object_blob_close(blob1);
1481 got_object_blob_close(blob2);
1485 static const struct got_error *
1486 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1487 const char *path, int diff_context, int ignore_whitespace,
1488 struct got_repository *repo)
1490 const struct got_error *err = NULL;
1491 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1492 struct got_diff_blob_output_unidiff_arg arg;
1495 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1500 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1504 arg.diff_context = diff_context;
1505 arg.ignore_whitespace = ignore_whitespace;
1506 arg.outfile = stdout;
1507 while (path[0] == '/')
1509 err = got_diff_tree(tree1, tree2, path, path, repo,
1510 got_diff_blob_output_unidiff, &arg, 1);
1513 got_object_tree_close(tree1);
1515 got_object_tree_close(tree2);
1519 static const struct got_error *
1520 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1521 const char *path, int diff_context, struct got_repository *repo)
1523 const struct got_error *err = NULL;
1524 struct got_commit_object *pcommit = NULL;
1525 char *id_str1 = NULL, *id_str2 = NULL;
1526 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1527 struct got_object_qid *qid;
1529 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1531 err = got_object_open_as_commit(&pcommit, repo,
1537 if (path && path[0] != '\0') {
1539 err = got_object_id_by_path(&obj_id2, repo, id, path);
1542 err = got_object_id_str(&id_str2, obj_id2);
1548 err = got_object_id_by_path(&obj_id1, repo,
1554 err = got_object_id_str(&id_str1, obj_id1);
1560 err = got_object_get_type(&obj_type, repo, obj_id2);
1565 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1567 case GOT_OBJ_TYPE_BLOB:
1568 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1571 case GOT_OBJ_TYPE_TREE:
1572 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1576 err = got_error(GOT_ERR_OBJ_TYPE);
1582 obj_id2 = got_object_commit_get_tree_id(commit);
1583 err = got_object_id_str(&id_str2, obj_id2);
1586 obj_id1 = got_object_commit_get_tree_id(pcommit);
1587 err = got_object_id_str(&id_str1, obj_id1);
1590 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1591 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1598 got_object_commit_close(pcommit);
1603 get_datestr(time_t *time, char *datebuf)
1605 struct tm mytm, *tm;
1608 tm = gmtime_r(time, &mytm);
1611 s = asctime_r(tm, datebuf);
1614 p = strchr(s, '\n');
1620 static const struct got_error *
1621 match_logmsg(int *have_match, struct got_object_id *id,
1622 struct got_commit_object *commit, regex_t *regex)
1624 const struct got_error *err = NULL;
1625 regmatch_t regmatch;
1626 char *id_str = NULL, *logmsg = NULL;
1630 err = got_object_id_str(&id_str, id);
1634 err = got_object_commit_get_logmsg(&logmsg, commit);
1638 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1646 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1648 static const struct got_error *
1649 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1650 struct got_repository *repo, const char *path, int show_patch,
1651 int diff_context, struct got_reflist_head *refs)
1653 const struct got_error *err = NULL;
1654 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1656 time_t committer_time;
1657 const char *author, *committer;
1658 char *refs_str = NULL;
1659 struct got_reflist_entry *re;
1661 SIMPLEQ_FOREACH(re, refs, entry) {
1664 struct got_tag_object *tag = NULL;
1667 name = got_ref_get_name(re->ref);
1668 if (strcmp(name, GOT_REF_HEAD) == 0)
1670 if (strncmp(name, "refs/", 5) == 0)
1672 if (strncmp(name, "got/", 4) == 0)
1674 if (strncmp(name, "heads/", 6) == 0)
1676 if (strncmp(name, "remotes/", 8) == 0)
1678 if (strncmp(name, "tags/", 5) == 0) {
1679 err = got_object_open_as_tag(&tag, repo, re->id);
1681 if (err->code != GOT_ERR_OBJ_TYPE)
1683 /* Ref points at something other than a tag. */
1688 cmp = got_object_id_cmp(tag ?
1689 got_object_tag_get_object_id(tag) : re->id, id);
1691 got_object_tag_close(tag);
1695 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1697 err = got_error_from_errno("asprintf");
1703 err = got_object_id_str(&id_str, id);
1707 printf(GOT_COMMIT_SEP_STR);
1708 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1709 refs_str ? refs_str : "", refs_str ? ")" : "");
1714 printf("from: %s\n", got_object_commit_get_author(commit));
1715 committer_time = got_object_commit_get_committer_time(commit);
1716 datestr = get_datestr(&committer_time, datebuf);
1718 printf("date: %s UTC\n", datestr);
1719 author = got_object_commit_get_author(commit);
1720 committer = got_object_commit_get_committer(commit);
1721 if (strcmp(author, committer) != 0)
1722 printf("via: %s\n", committer);
1723 if (got_object_commit_get_nparents(commit) > 1) {
1724 const struct got_object_id_queue *parent_ids;
1725 struct got_object_qid *qid;
1727 parent_ids = got_object_commit_get_parent_ids(commit);
1728 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1729 err = got_object_id_str(&id_str, qid->id);
1732 printf("parent %d: %s\n", n++, id_str);
1737 err = got_object_commit_get_logmsg(&logmsg0, commit);
1743 line = strsep(&logmsg, "\n");
1745 printf(" %s\n", line);
1750 err = print_patch(commit, id, path, diff_context, repo);
1755 if (fflush(stdout) != 0 && err == NULL)
1756 err = got_error_from_errno("fflush");
1760 static const struct got_error *
1761 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1762 const char *path, int show_patch, const char *search_pattern,
1763 int diff_context, int limit, int first_parent_traversal,
1764 struct got_reflist_head *refs)
1766 const struct got_error *err;
1767 struct got_commit_graph *graph;
1771 if (search_pattern &&
1772 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1773 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1775 err = got_commit_graph_open(&graph, root_id, path,
1776 first_parent_traversal, repo);
1779 err = got_commit_graph_iter_start(graph, root_id, repo,
1780 check_cancelled, NULL);
1784 struct got_commit_object *commit;
1785 struct got_object_id *id;
1787 if (sigint_received || sigpipe_received)
1790 err = got_commit_graph_iter_next(&id, graph);
1792 if (err->code == GOT_ERR_ITER_COMPLETED) {
1796 if (err->code != GOT_ERR_ITER_NEED_MORE)
1798 err = got_commit_graph_fetch_commits(graph, 1, repo,
1799 check_cancelled, NULL);
1808 err = got_object_open_as_commit(&commit, repo, id);
1812 if (search_pattern) {
1813 err = match_logmsg(&have_match, id, commit, ®ex);
1815 got_object_commit_close(commit);
1818 if (have_match == 0) {
1819 got_object_commit_close(commit);
1824 err = print_commit(commit, id, repo, path, show_patch,
1825 diff_context, refs);
1826 got_object_commit_close(commit);
1827 if (err || (limit && --limit == 0))
1833 got_commit_graph_close(graph);
1840 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1841 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1846 get_default_log_limit(void)
1848 const char *got_default_log_limit;
1852 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1853 if (got_default_log_limit == NULL)
1855 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1861 static const struct got_error *
1862 cmd_log(int argc, char *argv[])
1864 const struct got_error *error;
1865 struct got_repository *repo = NULL;
1866 struct got_worktree *worktree = NULL;
1867 struct got_commit_object *commit = NULL;
1868 struct got_object_id *id = NULL;
1869 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1870 const char *start_commit = NULL, *search_pattern = NULL;
1871 int diff_context = -1, ch;
1872 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1874 struct got_reflist_head refs;
1876 SIMPLEQ_INIT(&refs);
1879 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1885 limit = get_default_log_limit();
1887 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1893 start_commit = optarg;
1896 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1899 err(1, "-C option %s", errstr);
1902 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1904 err(1, "-l option %s", errstr);
1907 first_parent_traversal = 1;
1910 repo_path = realpath(optarg, NULL);
1911 if (repo_path == NULL)
1912 return got_error_from_errno2("realpath",
1914 got_path_strip_trailing_slashes(repo_path);
1917 search_pattern = optarg;
1928 if (diff_context == -1)
1930 else if (!show_patch)
1931 errx(1, "-C reguires -p");
1933 cwd = getcwd(NULL, 0);
1935 error = got_error_from_errno("getcwd");
1939 error = got_worktree_open(&worktree, cwd);
1940 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1947 error = got_error_from_errno("strdup");
1950 } else if (argc == 1) {
1952 error = got_worktree_resolve_path(&path, worktree,
1957 path = strdup(argv[0]);
1959 error = got_error_from_errno("strdup");
1966 if (repo_path == NULL) {
1967 repo_path = worktree ?
1968 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1970 if (repo_path == NULL) {
1971 error = got_error_from_errno("strdup");
1975 error = got_repo_open(&repo, repo_path, NULL);
1979 error = apply_unveil(got_repo_get_path(repo), 1,
1980 worktree ? got_worktree_get_root_path(worktree) : NULL);
1984 if (start_commit == NULL) {
1985 struct got_reference *head_ref;
1986 error = got_ref_open(&head_ref, repo,
1987 worktree ? got_worktree_get_head_ref_name(worktree)
1991 error = got_ref_resolve(&id, repo, head_ref);
1992 got_ref_close(head_ref);
1995 error = got_object_open_as_commit(&commit, repo, id);
1997 struct got_reference *ref;
1998 error = got_ref_open(&ref, repo, start_commit, 0);
1999 if (error == NULL) {
2001 error = got_ref_resolve(&id, repo, ref);
2005 error = got_object_get_type(&obj_type, repo, id);
2008 if (obj_type == GOT_OBJ_TYPE_TAG) {
2009 struct got_tag_object *tag;
2010 error = got_object_open_as_tag(&tag, repo, id);
2013 if (got_object_tag_get_object_type(tag) !=
2014 GOT_OBJ_TYPE_COMMIT) {
2015 got_object_tag_close(tag);
2016 error = got_error(GOT_ERR_OBJ_TYPE);
2020 id = got_object_id_dup(
2021 got_object_tag_get_object_id(tag));
2023 error = got_error_from_errno(
2024 "got_object_id_dup");
2025 got_object_tag_close(tag);
2028 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2029 error = got_error(GOT_ERR_OBJ_TYPE);
2032 error = got_object_open_as_commit(&commit, repo, id);
2036 if (commit == NULL) {
2037 error = got_repo_match_object_id_prefix(&id,
2038 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2047 const char *prefix = got_worktree_get_path_prefix(worktree);
2049 if (asprintf(&p, "%s%s%s", prefix,
2050 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2051 error = got_error_from_errno("asprintf");
2054 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2057 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2062 path = in_repo_path;
2065 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2069 error = print_commits(id, repo, path, show_patch, search_pattern,
2070 diff_context, limit, first_parent_traversal, &refs);
2077 got_worktree_close(worktree);
2079 const struct got_error *repo_error;
2080 repo_error = got_repo_close(repo);
2084 got_ref_list_free(&refs);
2091 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2092 "[-w] [object1 object2 | path]\n", getprogname());
2096 struct print_diff_arg {
2097 struct got_repository *repo;
2098 struct got_worktree *worktree;
2103 int ignore_whitespace;
2106 static const struct got_error *
2107 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2108 const char *path, struct got_object_id *blob_id,
2109 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2110 int dirfd, const char *de_name)
2112 struct print_diff_arg *a = arg;
2113 const struct got_error *err = NULL;
2114 struct got_blob_object *blob1 = NULL;
2117 char *abspath = NULL, *label1 = NULL;
2120 if (a->diff_staged) {
2121 if (staged_status != GOT_STATUS_MODIFY &&
2122 staged_status != GOT_STATUS_ADD &&
2123 staged_status != GOT_STATUS_DELETE)
2126 if (staged_status == GOT_STATUS_DELETE)
2128 if (status == GOT_STATUS_NONEXISTENT)
2129 return got_error_set_errno(ENOENT, path);
2130 if (status != GOT_STATUS_MODIFY &&
2131 status != GOT_STATUS_ADD &&
2132 status != GOT_STATUS_DELETE &&
2133 status != GOT_STATUS_CONFLICT)
2137 if (!a->header_shown) {
2138 printf("diff %s %s%s\n", a->id_str,
2139 got_worktree_get_root_path(a->worktree),
2140 a->diff_staged ? " (staged changes)" : "");
2141 a->header_shown = 1;
2144 if (a->diff_staged) {
2145 const char *label1 = NULL, *label2 = NULL;
2146 switch (staged_status) {
2147 case GOT_STATUS_MODIFY:
2151 case GOT_STATUS_ADD:
2154 case GOT_STATUS_DELETE:
2158 return got_error(GOT_ERR_FILE_STATUS);
2160 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2161 label1, label2, a->diff_context, a->ignore_whitespace,
2165 if (staged_status == GOT_STATUS_ADD ||
2166 staged_status == GOT_STATUS_MODIFY) {
2168 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2172 err = got_object_id_str(&id_str, staged_blob_id);
2175 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2176 err = got_error_from_errno("asprintf");
2181 } else if (status != GOT_STATUS_ADD) {
2182 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2187 if (status != GOT_STATUS_DELETE) {
2188 if (asprintf(&abspath, "%s/%s",
2189 got_worktree_get_root_path(a->worktree), path) == -1) {
2190 err = got_error_from_errno("asprintf");
2195 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2197 err = got_error_from_errno2("openat", abspath);
2201 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2203 err = got_error_from_errno2("open", abspath);
2207 if (fstat(fd, &sb) == -1) {
2208 err = got_error_from_errno2("fstat", abspath);
2211 f2 = fdopen(fd, "r");
2213 err = got_error_from_errno2("fdopen", abspath);
2220 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2221 a->diff_context, a->ignore_whitespace, stdout);
2224 got_object_blob_close(blob1);
2225 if (f2 && fclose(f2) == EOF && err == NULL)
2226 err = got_error_from_errno("fclose");
2227 if (fd != -1 && close(fd) == -1 && err == NULL)
2228 err = got_error_from_errno("close");
2233 static const struct got_error *
2234 match_object_id(struct got_object_id **id, char **label,
2235 const char *id_str, int obj_type, int resolve_tags,
2236 struct got_repository *repo)
2238 const struct got_error *err;
2239 struct got_tag_object *tag;
2240 struct got_reference *ref = NULL;
2246 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2249 *id = got_object_id_dup(
2250 got_object_tag_get_object_id(tag));
2252 err = got_error_from_errno("got_object_id_dup");
2253 else if (asprintf(label, "refs/tags/%s",
2254 got_object_tag_get_name(tag)) == -1) {
2255 err = got_error_from_errno("asprintf");
2259 got_object_tag_close(tag);
2261 } else if (err->code != GOT_ERR_NO_OBJ)
2265 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2267 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2269 err = got_ref_open(&ref, repo, id_str, 0);
2272 *label = strdup(got_ref_get_name(ref));
2273 if (*label == NULL) {
2274 err = got_error_from_errno("strdup");
2277 err = got_ref_resolve(id, repo, ref);
2279 err = got_object_id_str(label, *id);
2280 if (*label == NULL) {
2281 err = got_error_from_errno("strdup");
2292 static const struct got_error *
2293 cmd_diff(int argc, char *argv[])
2295 const struct got_error *error;
2296 struct got_repository *repo = NULL;
2297 struct got_worktree *worktree = NULL;
2298 char *cwd = NULL, *repo_path = NULL;
2299 struct got_object_id *id1 = NULL, *id2 = NULL;
2300 const char *id_str1 = NULL, *id_str2 = NULL;
2301 char *label1 = NULL, *label2 = NULL;
2303 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2308 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2313 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2316 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2319 err(1, "-C option %s", errstr);
2322 repo_path = realpath(optarg, NULL);
2323 if (repo_path == NULL)
2324 return got_error_from_errno2("realpath",
2326 got_path_strip_trailing_slashes(repo_path);
2332 ignore_whitespace = 1;
2343 cwd = getcwd(NULL, 0);
2345 error = got_error_from_errno("getcwd");
2348 error = got_worktree_open(&worktree, cwd);
2349 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2352 if (worktree == NULL) {
2353 error = got_error(GOT_ERR_NOT_WORKTREE);
2358 "-r option can't be used when diffing a work tree");
2359 repo_path = strdup(got_worktree_get_repo_path(worktree));
2360 if (repo_path == NULL) {
2361 error = got_error_from_errno("strdup");
2365 error = got_worktree_resolve_path(&path, worktree,
2372 error = got_error_from_errno("strdup");
2376 } else if (argc == 2) {
2378 errx(1, "-s option can't be used when diffing "
2379 "objects in repository");
2382 if (worktree && repo_path == NULL) {
2384 strdup(got_worktree_get_repo_path(worktree));
2385 if (repo_path == NULL) {
2386 error = got_error_from_errno("strdup");
2393 if (repo_path == NULL) {
2394 repo_path = getcwd(NULL, 0);
2395 if (repo_path == NULL)
2396 return got_error_from_errno("getcwd");
2399 error = got_repo_open(&repo, repo_path, NULL);
2404 error = apply_unveil(got_repo_get_path(repo), 1,
2405 worktree ? got_worktree_get_root_path(worktree) : NULL);
2410 struct print_diff_arg arg;
2411 struct got_pathlist_head paths;
2416 error = got_object_id_str(&id_str,
2417 got_worktree_get_base_commit_id(worktree));
2421 arg.worktree = worktree;
2422 arg.diff_context = diff_context;
2423 arg.id_str = id_str;
2424 arg.header_shown = 0;
2425 arg.diff_staged = diff_staged;
2426 arg.ignore_whitespace = ignore_whitespace;
2428 error = got_pathlist_append(&paths, path, NULL);
2432 error = got_worktree_status(worktree, &paths, repo, print_diff,
2433 &arg, check_cancelled, NULL);
2435 got_pathlist_free(&paths);
2439 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2444 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2449 error = got_object_get_type(&type1, repo, id1);
2453 error = got_object_get_type(&type2, repo, id2);
2457 if (type1 != type2) {
2458 error = got_error(GOT_ERR_OBJ_TYPE);
2463 case GOT_OBJ_TYPE_BLOB:
2464 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2465 diff_context, ignore_whitespace, repo, stdout);
2467 case GOT_OBJ_TYPE_TREE:
2468 error = got_diff_objects_as_trees(id1, id2, "", "",
2469 diff_context, ignore_whitespace, repo, stdout);
2471 case GOT_OBJ_TYPE_COMMIT:
2472 printf("diff %s %s\n", label1, label2);
2473 error = got_diff_objects_as_commits(id1, id2, diff_context,
2474 ignore_whitespace, repo, stdout);
2477 error = got_error(GOT_ERR_OBJ_TYPE);
2487 got_worktree_close(worktree);
2489 const struct got_error *repo_error;
2490 repo_error = got_repo_close(repo);
2501 "usage: %s blame [-c commit] [-r repository-path] path\n",
2510 char datebuf[11]; /* YYYY-MM-DD + NUL */
2513 struct blame_cb_args {
2514 struct blame_line *lines;
2518 off_t *line_offsets;
2520 struct got_repository *repo;
2523 static const struct got_error *
2524 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2526 const struct got_error *err = NULL;
2527 struct blame_cb_args *a = arg;
2528 struct blame_line *bline;
2530 size_t linesize = 0;
2531 struct got_commit_object *commit = NULL;
2534 time_t committer_time;
2536 if (nlines != a->nlines ||
2537 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2538 return got_error(GOT_ERR_RANGE);
2540 if (sigint_received)
2541 return got_error(GOT_ERR_ITER_COMPLETED);
2544 return NULL; /* no change in this commit */
2546 /* Annotate this line. */
2547 bline = &a->lines[lineno - 1];
2548 if (bline->annotated)
2550 err = got_object_id_str(&bline->id_str, id);
2554 err = got_object_open_as_commit(&commit, a->repo, id);
2558 bline->committer = strdup(got_object_commit_get_committer(commit));
2559 if (bline->committer == NULL) {
2560 err = got_error_from_errno("strdup");
2564 committer_time = got_object_commit_get_committer_time(commit);
2565 if (localtime_r(&committer_time, &tm) == NULL)
2566 return got_error_from_errno("localtime_r");
2567 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2568 &tm) >= sizeof(bline->datebuf)) {
2569 err = got_error(GOT_ERR_NO_SPACE);
2572 bline->annotated = 1;
2574 /* Print lines annotated so far. */
2575 bline = &a->lines[a->lineno_cur - 1];
2576 if (!bline->annotated)
2579 offset = a->line_offsets[a->lineno_cur - 1];
2580 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2581 err = got_error_from_errno("fseeko");
2585 while (bline->annotated) {
2586 char *smallerthan, *at, *nl, *committer;
2589 if (getline(&line, &linesize, a->f) == -1) {
2591 err = got_error_from_errno("getline");
2595 committer = bline->committer;
2596 smallerthan = strchr(committer, '<');
2597 if (smallerthan && smallerthan[1] != '\0')
2598 committer = smallerthan + 1;
2599 at = strchr(committer, '@');
2602 len = strlen(committer);
2604 committer[8] = '\0';
2606 nl = strchr(line, '\n');
2609 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2610 bline->id_str, bline->datebuf, committer, line);
2613 bline = &a->lines[a->lineno_cur - 1];
2617 got_object_commit_close(commit);
2622 static const struct got_error *
2623 cmd_blame(int argc, char *argv[])
2625 const struct got_error *error;
2626 struct got_repository *repo = NULL;
2627 struct got_worktree *worktree = NULL;
2628 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2629 struct got_object_id *obj_id = NULL;
2630 struct got_object_id *commit_id = NULL;
2631 struct got_blob_object *blob = NULL;
2632 char *commit_id_str = NULL;
2633 struct blame_cb_args bca;
2634 int ch, obj_type, i;
2637 memset(&bca, 0, sizeof(bca));
2640 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2645 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2648 commit_id_str = optarg;
2651 repo_path = realpath(optarg, NULL);
2652 if (repo_path == NULL)
2653 return got_error_from_errno2("realpath",
2655 got_path_strip_trailing_slashes(repo_path);
2671 cwd = getcwd(NULL, 0);
2673 error = got_error_from_errno("getcwd");
2676 if (repo_path == NULL) {
2677 error = got_worktree_open(&worktree, cwd);
2678 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2684 strdup(got_worktree_get_repo_path(worktree));
2685 if (repo_path == NULL)
2686 error = got_error_from_errno("strdup");
2690 repo_path = strdup(cwd);
2691 if (repo_path == NULL) {
2692 error = got_error_from_errno("strdup");
2698 error = got_repo_open(&repo, repo_path, NULL);
2702 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2707 const char *prefix = got_worktree_get_path_prefix(worktree);
2708 char *p, *worktree_subdir = cwd +
2709 strlen(got_worktree_get_root_path(worktree));
2710 if (asprintf(&p, "%s%s%s%s%s",
2711 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2712 worktree_subdir, worktree_subdir[0] ? "/" : "",
2714 error = got_error_from_errno("asprintf");
2717 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2720 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2725 if (commit_id_str == NULL) {
2726 struct got_reference *head_ref;
2727 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2730 error = got_ref_resolve(&commit_id, repo, head_ref);
2731 got_ref_close(head_ref);
2735 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2740 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2743 if (obj_id == NULL) {
2744 error = got_error(GOT_ERR_NO_OBJ);
2748 error = got_object_get_type(&obj_type, repo, obj_id);
2752 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2753 error = got_error(GOT_ERR_OBJ_TYPE);
2757 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2760 bca.f = got_opentemp();
2761 if (bca.f == NULL) {
2762 error = got_error_from_errno("got_opentemp");
2765 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2766 &bca.line_offsets, bca.f, blob);
2767 if (error || bca.nlines == 0)
2770 /* Don't include \n at EOF in the blame line count. */
2771 if (bca.line_offsets[bca.nlines - 1] == filesize)
2774 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2775 if (bca.lines == NULL) {
2776 error = got_error_from_errno("calloc");
2780 bca.nlines_prec = 0;
2788 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2789 check_cancelled, NULL);
2799 got_object_blob_close(blob);
2801 got_worktree_close(worktree);
2803 const struct got_error *repo_error;
2804 repo_error = got_repo_close(repo);
2809 for (i = 0; i < bca.nlines; i++) {
2810 struct blame_line *bline = &bca.lines[i];
2811 free(bline->id_str);
2812 free(bline->committer);
2816 free(bca.line_offsets);
2817 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2818 error = got_error_from_errno("fclose");
2826 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2832 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2833 const char *root_path)
2835 int is_root_path = (strcmp(path, root_path) == 0);
2836 const char *modestr = "";
2837 mode_t mode = got_tree_entry_get_mode(te);
2839 path += strlen(root_path);
2840 while (path[0] == '/')
2843 if (got_object_tree_entry_is_submodule(te))
2845 else if (S_ISLNK(mode))
2847 else if (S_ISDIR(mode))
2849 else if (mode & S_IXUSR)
2852 printf("%s%s%s%s%s\n", id ? id : "", path,
2853 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2856 static const struct got_error *
2857 print_tree(const char *path, struct got_object_id *commit_id,
2858 int show_ids, int recurse, const char *root_path,
2859 struct got_repository *repo)
2861 const struct got_error *err = NULL;
2862 struct got_object_id *tree_id = NULL;
2863 struct got_tree_object *tree = NULL;
2866 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2870 err = got_object_open_as_tree(&tree, repo, tree_id);
2873 nentries = got_object_tree_get_nentries(tree);
2874 for (i = 0; i < nentries; i++) {
2875 struct got_tree_entry *te;
2878 if (sigint_received || sigpipe_received)
2881 te = got_object_tree_get_entry(tree, i);
2884 err = got_object_id_str(&id_str,
2885 got_tree_entry_get_id(te));
2888 if (asprintf(&id, "%s ", id_str) == -1) {
2889 err = got_error_from_errno("asprintf");
2895 print_entry(te, id, path, root_path);
2898 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2900 if (asprintf(&child_path, "%s%s%s", path,
2901 path[0] == '/' && path[1] == '\0' ? "" : "/",
2902 got_tree_entry_get_name(te)) == -1) {
2903 err = got_error_from_errno("asprintf");
2906 err = print_tree(child_path, commit_id, show_ids, 1,
2915 got_object_tree_close(tree);
2920 static const struct got_error *
2921 cmd_tree(int argc, char *argv[])
2923 const struct got_error *error;
2924 struct got_repository *repo = NULL;
2925 struct got_worktree *worktree = NULL;
2927 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2928 struct got_object_id *commit_id = NULL;
2929 char *commit_id_str = NULL;
2930 int show_ids = 0, recurse = 0;
2934 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2939 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2942 commit_id_str = optarg;
2945 repo_path = realpath(optarg, NULL);
2946 if (repo_path == NULL)
2947 return got_error_from_errno2("realpath",
2949 got_path_strip_trailing_slashes(repo_path);
2973 cwd = getcwd(NULL, 0);
2975 error = got_error_from_errno("getcwd");
2978 if (repo_path == NULL) {
2979 error = got_worktree_open(&worktree, cwd);
2980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2986 strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL)
2988 error = got_error_from_errno("strdup");
2992 repo_path = strdup(cwd);
2993 if (repo_path == NULL) {
2994 error = got_error_from_errno("strdup");
3000 error = got_repo_open(&repo, repo_path, NULL);
3004 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3010 char *p, *worktree_subdir = cwd +
3011 strlen(got_worktree_get_root_path(worktree));
3012 if (asprintf(&p, "%s/%s",
3013 got_worktree_get_path_prefix(worktree),
3014 worktree_subdir) == -1) {
3015 error = got_error_from_errno("asprintf");
3018 error = got_repo_map_path(&in_repo_path, repo, p, 1);
3025 if (in_repo_path == NULL) {
3026 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3031 if (commit_id_str == NULL) {
3032 struct got_reference *head_ref;
3033 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3036 error = got_ref_resolve(&commit_id, repo, head_ref);
3037 got_ref_close(head_ref);
3041 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3046 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3047 in_repo_path, repo);
3054 got_worktree_close(worktree);
3056 const struct got_error *repo_error;
3057 repo_error = got_repo_close(repo);
3067 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3071 static const struct got_error *
3072 print_status(void *arg, unsigned char status, unsigned char staged_status,
3073 const char *path, struct got_object_id *blob_id,
3074 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3075 int dirfd, const char *de_name)
3077 if (status == staged_status && (status == GOT_STATUS_DELETE))
3078 status = GOT_STATUS_NO_CHANGE;
3079 printf("%c%c %s\n", status, staged_status, path);
3083 static const struct got_error *
3084 cmd_status(int argc, char *argv[])
3086 const struct got_error *error = NULL;
3087 struct got_repository *repo = NULL;
3088 struct got_worktree *worktree = NULL;
3090 struct got_pathlist_head paths;
3091 struct got_pathlist_entry *pe;
3096 while ((ch = getopt(argc, argv, "")) != -1) {
3108 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3112 cwd = getcwd(NULL, 0);
3114 error = got_error_from_errno("getcwd");
3118 error = got_worktree_open(&worktree, cwd);
3122 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3127 error = apply_unveil(got_repo_get_path(repo), 1,
3128 got_worktree_get_root_path(worktree));
3132 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3136 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3137 check_cancelled, NULL);
3139 TAILQ_FOREACH(pe, &paths, entry)
3140 free((char *)pe->path);
3141 got_pathlist_free(&paths);
3150 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3155 static const struct got_error *
3156 list_refs(struct got_repository *repo)
3158 static const struct got_error *err = NULL;
3159 struct got_reflist_head refs;
3160 struct got_reflist_entry *re;
3162 SIMPLEQ_INIT(&refs);
3163 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3167 SIMPLEQ_FOREACH(re, &refs, entry) {
3169 refstr = got_ref_to_str(re->ref);
3171 return got_error_from_errno("got_ref_to_str");
3172 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3176 got_ref_list_free(&refs);
3180 static const struct got_error *
3181 delete_ref(struct got_repository *repo, const char *refname)
3183 const struct got_error *err = NULL;
3184 struct got_reference *ref;
3186 err = got_ref_open(&ref, repo, refname, 0);
3190 err = got_ref_delete(ref, repo);
3195 static const struct got_error *
3196 add_ref(struct got_repository *repo, const char *refname, const char *target)
3198 const struct got_error *err = NULL;
3199 struct got_object_id *id;
3200 struct got_reference *ref = NULL;
3203 * Don't let the user create a reference name with a leading '-'.
3204 * While technically a valid reference name, this case is usually
3205 * an unintended typo.
3207 if (refname[0] == '-')
3208 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3210 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3213 struct got_reference *target_ref;
3215 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3217 err = got_ref_open(&target_ref, repo, target, 0);
3220 err = got_ref_resolve(&id, repo, target_ref);
3221 got_ref_close(target_ref);
3226 err = got_ref_alloc(&ref, refname, id);
3230 err = got_ref_write(ref, repo);
3238 static const struct got_error *
3239 add_symref(struct got_repository *repo, const char *refname, const char *target)
3241 const struct got_error *err = NULL;
3242 struct got_reference *ref = NULL;
3243 struct got_reference *target_ref = NULL;
3246 * Don't let the user create a reference name with a leading '-'.
3247 * While technically a valid reference name, this case is usually
3248 * an unintended typo.
3250 if (refname[0] == '-')
3251 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3253 err = got_ref_open(&target_ref, repo, target, 0);
3257 err = got_ref_alloc_symref(&ref, refname, target_ref);
3261 err = got_ref_write(ref, repo);
3264 got_ref_close(target_ref);
3270 static const struct got_error *
3271 cmd_ref(int argc, char *argv[])
3273 const struct got_error *error = NULL;
3274 struct got_repository *repo = NULL;
3275 struct got_worktree *worktree = NULL;
3276 char *cwd = NULL, *repo_path = NULL;
3277 int ch, do_list = 0, create_symref = 0;
3278 const char *delref = NULL;
3280 /* TODO: Add -s option for adding symbolic references. */
3281 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3287 repo_path = realpath(optarg, NULL);
3288 if (repo_path == NULL)
3289 return got_error_from_errno2("realpath",
3291 got_path_strip_trailing_slashes(repo_path);
3305 if (do_list && delref)
3306 errx(1, "-l and -d options are mutually exclusive\n");
3311 if (do_list || delref) {
3313 errx(1, "-s option cannot be used together with the "
3314 "-l or -d options");
3317 } else if (argc != 2)
3322 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3326 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3327 "sendfd unveil", NULL) == -1)
3331 cwd = getcwd(NULL, 0);
3333 error = got_error_from_errno("getcwd");
3337 if (repo_path == NULL) {
3338 error = got_worktree_open(&worktree, cwd);
3339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3345 strdup(got_worktree_get_repo_path(worktree));
3346 if (repo_path == NULL)
3347 error = got_error_from_errno("strdup");
3351 repo_path = strdup(cwd);
3352 if (repo_path == NULL) {
3353 error = got_error_from_errno("strdup");
3359 error = got_repo_open(&repo, repo_path, NULL);
3363 error = apply_unveil(got_repo_get_path(repo), do_list,
3364 worktree ? got_worktree_get_root_path(worktree) : NULL);
3369 error = list_refs(repo);
3371 error = delete_ref(repo, delref);
3372 else if (create_symref)
3373 error = add_symref(repo, argv[0], argv[1]);
3375 error = add_ref(repo, argv[0], argv[1]);
3378 got_repo_close(repo);
3380 got_worktree_close(worktree);
3390 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3391 "[name]\n", getprogname());
3395 static const struct got_error *
3396 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3397 struct got_reference *ref)
3399 const struct got_error *err = NULL;
3400 const char *refname, *marker = " ";
3403 refname = got_ref_get_name(ref);
3404 if (worktree && strcmp(refname,
3405 got_worktree_get_head_ref_name(worktree)) == 0) {
3406 struct got_object_id *id = NULL;
3408 err = got_ref_resolve(&id, repo, ref);
3411 if (got_object_id_cmp(id,
3412 got_worktree_get_base_commit_id(worktree)) == 0)
3419 if (strncmp(refname, "refs/heads/", 11) == 0)
3421 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3424 refstr = got_ref_to_str(ref);
3426 return got_error_from_errno("got_ref_to_str");
3428 printf("%s%s: %s\n", marker, refname, refstr);
3433 static const struct got_error *
3434 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3436 const char *refname;
3438 if (worktree == NULL)
3439 return got_error(GOT_ERR_NOT_WORKTREE);
3441 refname = got_worktree_get_head_ref_name(worktree);
3443 if (strncmp(refname, "refs/heads/", 11) == 0)
3445 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3448 printf("%s\n", refname);
3453 static const struct got_error *
3454 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3456 static const struct got_error *err = NULL;
3457 struct got_reflist_head refs;
3458 struct got_reflist_entry *re;
3459 struct got_reference *temp_ref = NULL;
3460 int rebase_in_progress, histedit_in_progress;
3462 SIMPLEQ_INIT(&refs);
3465 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3470 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3475 if (rebase_in_progress || histedit_in_progress) {
3476 err = got_ref_open(&temp_ref, repo,
3477 got_worktree_get_head_ref_name(worktree), 0);
3480 list_branch(repo, worktree, temp_ref);
3481 got_ref_close(temp_ref);
3485 err = got_ref_list(&refs, repo, "refs/heads",
3486 got_ref_cmp_by_name, NULL);
3490 SIMPLEQ_FOREACH(re, &refs, entry)
3491 list_branch(repo, worktree, re->ref);
3493 got_ref_list_free(&refs);
3497 static const struct got_error *
3498 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3499 const char *branch_name)
3501 const struct got_error *err = NULL;
3502 struct got_reference *ref = NULL;
3505 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3506 return got_error_from_errno("asprintf");
3508 err = got_ref_open(&ref, repo, refname, 0);
3513 strcmp(got_worktree_get_head_ref_name(worktree),
3514 got_ref_get_name(ref)) == 0) {
3515 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3516 "will not delete this work tree's current branch");
3520 err = got_ref_delete(ref, repo);
3528 static const struct got_error *
3529 add_branch(struct got_repository *repo, const char *branch_name,
3530 struct got_object_id *base_commit_id)
3532 const struct got_error *err = NULL;
3533 struct got_reference *ref = NULL;
3534 char *base_refname = NULL, *refname = NULL;
3537 * Don't let the user create a branch name with a leading '-'.
3538 * While technically a valid reference name, this case is usually
3539 * an unintended typo.
3541 if (branch_name[0] == '-')
3542 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3544 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3545 err = got_error_from_errno("asprintf");
3549 err = got_ref_open(&ref, repo, refname, 0);
3551 err = got_error(GOT_ERR_BRANCH_EXISTS);
3553 } else if (err->code != GOT_ERR_NOT_REF)
3556 err = got_ref_alloc(&ref, refname, base_commit_id);
3560 err = got_ref_write(ref, repo);
3569 static const struct got_error *
3570 cmd_branch(int argc, char *argv[])
3572 const struct got_error *error = NULL;
3573 struct got_repository *repo = NULL;
3574 struct got_worktree *worktree = NULL;
3575 char *cwd = NULL, *repo_path = NULL;
3576 int ch, do_list = 0, do_show = 0;
3577 const char *delref = NULL, *commit_id_arg = NULL;
3579 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3582 commit_id_arg = optarg;
3588 repo_path = realpath(optarg, NULL);
3589 if (repo_path == NULL)
3590 return got_error_from_errno2("realpath",
3592 got_path_strip_trailing_slashes(repo_path);
3603 if (do_list && delref)
3604 errx(1, "-l and -d options are mutually exclusive\n");
3609 if (!do_list && !delref && argc == 0)
3612 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3613 errx(1, "-c option can only be used when creating a branch");
3615 if (do_list || delref) {
3618 } else if (!do_show && argc != 1)
3622 if (do_list || do_show) {
3623 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3627 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3628 "sendfd unveil", NULL) == -1)
3632 cwd = getcwd(NULL, 0);
3634 error = got_error_from_errno("getcwd");
3638 if (repo_path == NULL) {
3639 error = got_worktree_open(&worktree, cwd);
3640 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3646 strdup(got_worktree_get_repo_path(worktree));
3647 if (repo_path == NULL)
3648 error = got_error_from_errno("strdup");
3652 repo_path = strdup(cwd);
3653 if (repo_path == NULL) {
3654 error = got_error_from_errno("strdup");
3660 error = got_repo_open(&repo, repo_path, NULL);
3664 error = apply_unveil(got_repo_get_path(repo), do_list,
3665 worktree ? got_worktree_get_root_path(worktree) : NULL);
3670 error = show_current_branch(repo, worktree);
3672 error = list_branches(repo, worktree);
3674 error = delete_branch(repo, worktree, delref);
3676 struct got_object_id *commit_id;
3677 if (commit_id_arg == NULL)
3678 commit_id_arg = worktree ?
3679 got_worktree_get_head_ref_name(worktree) :
3681 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3684 error = add_branch(repo, argv[0], commit_id);
3689 got_repo_close(repo);
3691 got_worktree_close(worktree);
3702 "usage: %s tag [-r repository] | -l | "
3703 "[-m message] name [commit]\n", getprogname());
3708 static const struct got_error *
3709 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3711 const struct got_error *err = NULL;
3712 struct got_reflist_entry *re, *se, *new;
3713 struct got_object_id *re_id, *se_id;
3714 struct got_tag_object *re_tag, *se_tag;
3715 time_t re_time, se_time;
3717 SIMPLEQ_FOREACH(re, tags, entry) {
3718 se = SIMPLEQ_FIRST(sorted);
3720 err = got_reflist_entry_dup(&new, re);
3723 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3726 err = got_ref_resolve(&re_id, repo, re->ref);
3729 err = got_object_open_as_tag(&re_tag, repo, re_id);
3733 re_time = got_object_tag_get_tagger_time(re_tag);
3734 got_object_tag_close(re_tag);
3738 err = got_ref_resolve(&se_id, repo, re->ref);
3741 err = got_object_open_as_tag(&se_tag, repo, se_id);
3745 se_time = got_object_tag_get_tagger_time(se_tag);
3746 got_object_tag_close(se_tag);
3748 if (se_time > re_time) {
3749 err = got_reflist_entry_dup(&new, re);
3752 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3755 se = SIMPLEQ_NEXT(se, entry);
3764 static const struct got_error *
3765 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3766 struct got_reference *ref2)
3768 const struct got_error *err = NULL;
3769 struct got_repository *repo = arg;
3770 struct got_object_id *id1, *id2 = NULL;
3771 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3772 time_t time1, time2;
3776 err = got_ref_resolve(&id1, repo, ref1);
3779 err = got_object_open_as_tag(&tag1, repo, id1);
3783 err = got_ref_resolve(&id2, repo, ref2);
3786 err = got_object_open_as_tag(&tag2, repo, id2);
3790 time1 = got_object_tag_get_tagger_time(tag1);
3791 time2 = got_object_tag_get_tagger_time(tag2);
3793 /* Put latest tags first. */
3796 else if (time1 > time2)
3799 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3804 got_object_tag_close(tag1);
3806 got_object_tag_close(tag2);
3810 static const struct got_error *
3811 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3813 static const struct got_error *err = NULL;
3814 struct got_reflist_head refs;
3815 struct got_reflist_entry *re;
3817 SIMPLEQ_INIT(&refs);
3819 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3823 SIMPLEQ_FOREACH(re, &refs, entry) {
3824 const char *refname;
3825 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3828 struct got_object_id *id;
3829 struct got_tag_object *tag;
3831 refname = got_ref_get_name(re->ref);
3832 if (strncmp(refname, "refs/tags/", 10) != 0)
3835 refstr = got_ref_to_str(re->ref);
3836 if (refstr == NULL) {
3837 err = got_error_from_errno("got_ref_to_str");
3840 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3843 err = got_ref_resolve(&id, repo, re->ref);
3846 err = got_object_open_as_tag(&tag, repo, id);
3850 printf("from: %s\n", got_object_tag_get_tagger(tag));
3851 tagger_time = got_object_tag_get_tagger_time(tag);
3852 datestr = get_datestr(&tagger_time, datebuf);
3854 printf("date: %s UTC\n", datestr);
3855 err = got_object_id_str(&id_str,
3856 got_object_tag_get_object_id(tag));
3859 switch (got_object_tag_get_object_type(tag)) {
3860 case GOT_OBJ_TYPE_BLOB:
3861 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3863 case GOT_OBJ_TYPE_TREE:
3864 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3866 case GOT_OBJ_TYPE_COMMIT:
3867 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3869 case GOT_OBJ_TYPE_TAG:
3870 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3876 tagmsg0 = strdup(got_object_tag_get_message(tag));
3877 got_object_tag_close(tag);
3878 if (tagmsg0 == NULL) {
3879 err = got_error_from_errno("strdup");
3885 line = strsep(&tagmsg, "\n");
3887 printf(" %s\n", line);
3892 got_ref_list_free(&refs);
3896 static const struct got_error *
3897 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3898 const char *tag_name, const char *repo_path)
3900 const struct got_error *err = NULL;
3901 char *template = NULL, *initial_content = NULL;
3902 char *editor = NULL;
3905 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3906 err = got_error_from_errno("asprintf");
3910 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3911 commit_id_str, tag_name) == -1) {
3912 err = got_error_from_errno("asprintf");
3916 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3920 dprintf(fd, initial_content);
3923 err = get_editor(&editor);
3926 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3928 free(initial_content);
3932 /* Editor is done; we can now apply unveil(2) */
3934 err = apply_unveil(repo_path, 0, NULL);
3943 static const struct got_error *
3944 add_tag(struct got_repository *repo, const char *tag_name,
3945 const char *commit_arg, const char *tagmsg_arg)
3947 const struct got_error *err = NULL;
3948 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3949 char *label = NULL, *commit_id_str = NULL;
3950 struct got_reference *ref = NULL;
3951 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3952 char *tagmsg_path = NULL, *tag_id_str = NULL;
3953 int preserve_tagmsg = 0;
3956 * Don't let the user create a tag name with a leading '-'.
3957 * While technically a valid reference name, this case is usually
3958 * an unintended typo.
3960 if (tag_name[0] == '-')
3961 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3963 err = get_author(&tagger, repo);
3967 err = match_object_id(&commit_id, &label, commit_arg,
3968 GOT_OBJ_TYPE_COMMIT, 1, repo);
3972 err = got_object_id_str(&commit_id_str, commit_id);
3976 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3977 refname = strdup(tag_name);
3978 if (refname == NULL) {
3979 err = got_error_from_errno("strdup");
3983 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3984 err = got_error_from_errno("asprintf");
3988 err = got_ref_open(&ref, repo, refname, 0);
3990 err = got_error(GOT_ERR_TAG_EXISTS);
3992 } else if (err->code != GOT_ERR_NOT_REF)
3995 if (tagmsg_arg == NULL) {
3996 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3997 tag_name, got_repo_get_path(repo));
3999 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4000 tagmsg_path != NULL)
4001 preserve_tagmsg = 1;
4006 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4007 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4010 preserve_tagmsg = 1;
4014 err = got_ref_alloc(&ref, refname, tag_id);
4017 preserve_tagmsg = 1;
4021 err = got_ref_write(ref, repo);
4024 preserve_tagmsg = 1;
4028 err = got_object_id_str(&tag_id_str, tag_id);
4031 preserve_tagmsg = 1;
4034 printf("Created tag %s\n", tag_id_str);
4036 if (preserve_tagmsg) {
4037 fprintf(stderr, "%s: tag message preserved in %s\n",
4038 getprogname(), tagmsg_path);
4039 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4040 err = got_error_from_errno2("unlink", tagmsg_path);
4045 free(commit_id_str);
4053 static const struct got_error *
4054 cmd_tag(int argc, char *argv[])
4056 const struct got_error *error = NULL;
4057 struct got_repository *repo = NULL;
4058 struct got_worktree *worktree = NULL;
4059 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4060 char *gitconfig_path = NULL;
4061 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4062 int ch, do_list = 0;
4064 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4070 repo_path = realpath(optarg, NULL);
4071 if (repo_path == NULL)
4072 return got_error_from_errno2("realpath",
4074 got_path_strip_trailing_slashes(repo_path);
4090 errx(1, "-l and -m options are mutually exclusive\n");
4093 } else if (argc < 1 || argc > 2)
4096 commit_id_arg = argv[1];
4101 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4105 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4106 "sendfd unveil", NULL) == -1)
4110 cwd = getcwd(NULL, 0);
4112 error = got_error_from_errno("getcwd");
4116 if (repo_path == NULL) {
4117 error = got_worktree_open(&worktree, cwd);
4118 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4124 strdup(got_worktree_get_repo_path(worktree));
4125 if (repo_path == NULL)
4126 error = got_error_from_errno("strdup");
4130 repo_path = strdup(cwd);
4131 if (repo_path == NULL) {
4132 error = got_error_from_errno("strdup");
4139 error = got_repo_open(&repo, repo_path, NULL);
4142 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4145 error = list_tags(repo, worktree);
4147 error = get_gitconfig_path(&gitconfig_path);
4150 error = got_repo_open(&repo, repo_path, gitconfig_path);
4155 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4160 if (commit_id_arg == NULL) {
4161 struct got_reference *head_ref;
4162 struct got_object_id *commit_id;
4163 error = got_ref_open(&head_ref, repo,
4164 worktree ? got_worktree_get_head_ref_name(worktree)
4168 error = got_ref_resolve(&commit_id, repo, head_ref);
4169 got_ref_close(head_ref);
4172 error = got_object_id_str(&commit_id_str, commit_id);
4178 error = add_tag(repo, tag_name,
4179 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4183 got_repo_close(repo);
4185 got_worktree_close(worktree);
4188 free(gitconfig_path);
4189 free(commit_id_str);
4196 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4201 static const struct got_error *
4202 add_progress(void *arg, unsigned char status, const char *path)
4204 while (path[0] == '/')
4206 printf("%c %s\n", status, path);
4210 static const struct got_error *
4211 cmd_add(int argc, char *argv[])
4213 const struct got_error *error = NULL;
4214 struct got_repository *repo = NULL;
4215 struct got_worktree *worktree = NULL;
4217 struct got_pathlist_head paths;
4218 struct got_pathlist_entry *pe;
4219 int ch, can_recurse = 0, no_ignores = 0;
4223 while ((ch = getopt(argc, argv, "IR")) != -1) {
4241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4248 cwd = getcwd(NULL, 0);
4250 error = got_error_from_errno("getcwd");
4254 error = got_worktree_open(&worktree, cwd);
4258 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4263 error = apply_unveil(got_repo_get_path(repo), 1,
4264 got_worktree_get_root_path(worktree));
4268 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4272 if (!can_recurse && no_ignores) {
4273 error = got_error_msg(GOT_ERR_BAD_PATH,
4274 "disregarding ignores requires -R option");
4282 TAILQ_FOREACH(pe, &paths, entry) {
4283 if (asprintf(&ondisk_path, "%s/%s",
4284 got_worktree_get_root_path(worktree),
4286 error = got_error_from_errno("asprintf");
4289 if (lstat(ondisk_path, &sb) == -1) {
4290 if (errno == ENOENT) {
4294 error = got_error_from_errno2("lstat",
4300 if (S_ISDIR(sb.st_mode)) {
4301 error = got_error_msg(GOT_ERR_BAD_PATH,
4302 "adding directories requires -R option");
4308 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4309 NULL, repo, no_ignores);
4312 got_repo_close(repo);
4314 got_worktree_close(worktree);
4315 TAILQ_FOREACH(pe, &paths, entry)
4316 free((char *)pe->path);
4317 got_pathlist_free(&paths);
4325 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4330 static const struct got_error *
4331 print_remove_status(void *arg, unsigned char status,
4332 unsigned char staged_status, const char *path)
4334 while (path[0] == '/')
4336 if (status == GOT_STATUS_NONEXISTENT)
4338 if (status == staged_status && (status == GOT_STATUS_DELETE))
4339 status = GOT_STATUS_NO_CHANGE;
4340 printf("%c%c %s\n", status, staged_status, path);
4344 static const struct got_error *
4345 cmd_remove(int argc, char *argv[])
4347 const struct got_error *error = NULL;
4348 struct got_worktree *worktree = NULL;
4349 struct got_repository *repo = NULL;
4351 struct got_pathlist_head paths;
4352 struct got_pathlist_entry *pe;
4353 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4357 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4360 delete_local_mods = 1;
4378 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4385 cwd = getcwd(NULL, 0);
4387 error = got_error_from_errno("getcwd");
4390 error = got_worktree_open(&worktree, cwd);
4394 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4399 error = apply_unveil(got_repo_get_path(repo), 1,
4400 got_worktree_get_root_path(worktree));
4404 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4411 TAILQ_FOREACH(pe, &paths, entry) {
4412 if (asprintf(&ondisk_path, "%s/%s",
4413 got_worktree_get_root_path(worktree),
4415 error = got_error_from_errno("asprintf");
4418 if (lstat(ondisk_path, &sb) == -1) {
4419 if (errno == ENOENT) {
4423 error = got_error_from_errno2("lstat",
4429 if (S_ISDIR(sb.st_mode)) {
4430 error = got_error_msg(GOT_ERR_BAD_PATH,
4431 "removing directories requires -R option");
4437 error = got_worktree_schedule_delete(worktree, &paths,
4438 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4443 got_repo_close(repo);
4445 got_worktree_close(worktree);
4446 TAILQ_FOREACH(pe, &paths, entry)
4447 free((char *)pe->path);
4448 got_pathlist_free(&paths);
4456 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4457 "path ...\n", getprogname());
4461 static const struct got_error *
4462 revert_progress(void *arg, unsigned char status, const char *path)
4464 while (path[0] == '/')
4466 printf("%c %s\n", status, path);
4470 struct choose_patch_arg {
4471 FILE *patch_script_file;
4475 static const struct got_error *
4476 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4477 int nchanges, const char *action)
4480 size_t linesize = 0;
4484 case GOT_STATUS_ADD:
4485 printf("A %s\n%s this addition? [y/n] ", path, action);
4487 case GOT_STATUS_DELETE:
4488 printf("D %s\n%s this deletion? [y/n] ", path, action);
4490 case GOT_STATUS_MODIFY:
4491 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4492 return got_error_from_errno("fseek");
4493 printf(GOT_COMMIT_SEP_STR);
4494 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4496 if (ferror(patch_file))
4497 return got_error_from_errno("getline");
4498 printf(GOT_COMMIT_SEP_STR);
4499 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4500 path, n, nchanges, action);
4503 return got_error_path(path, GOT_ERR_FILE_STATUS);
4509 static const struct got_error *
4510 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4511 FILE *patch_file, int n, int nchanges)
4513 const struct got_error *err = NULL;
4515 size_t linesize = 0;
4518 struct choose_patch_arg *a = arg;
4520 *choice = GOT_PATCH_CHOICE_NONE;
4522 if (a->patch_script_file) {
4524 err = show_change(status, path, patch_file, n, nchanges,
4528 linelen = getline(&line, &linesize, a->patch_script_file);
4529 if (linelen == -1) {
4530 if (ferror(a->patch_script_file))
4531 return got_error_from_errno("getline");
4534 nl = strchr(line, '\n');
4537 if (strcmp(line, "y") == 0) {
4538 *choice = GOT_PATCH_CHOICE_YES;
4540 } else if (strcmp(line, "n") == 0) {
4541 *choice = GOT_PATCH_CHOICE_NO;
4543 } else if (strcmp(line, "q") == 0 &&
4544 status == GOT_STATUS_MODIFY) {
4545 *choice = GOT_PATCH_CHOICE_QUIT;
4548 printf("invalid response '%s'\n", line);
4553 while (resp != 'y' && resp != 'n' && resp != 'q') {
4554 err = show_change(status, path, patch_file, n, nchanges,
4561 if (status == GOT_STATUS_MODIFY) {
4562 if (resp != 'y' && resp != 'n' && resp != 'q') {
4563 printf("invalid response '%c'\n", resp);
4566 } else if (resp != 'y' && resp != 'n') {
4567 printf("invalid response '%c'\n", resp);
4573 *choice = GOT_PATCH_CHOICE_YES;
4574 else if (resp == 'n')
4575 *choice = GOT_PATCH_CHOICE_NO;
4576 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4577 *choice = GOT_PATCH_CHOICE_QUIT;
4583 static const struct got_error *
4584 cmd_revert(int argc, char *argv[])
4586 const struct got_error *error = NULL;
4587 struct got_worktree *worktree = NULL;
4588 struct got_repository *repo = NULL;
4589 char *cwd = NULL, *path = NULL;
4590 struct got_pathlist_head paths;
4591 struct got_pathlist_entry *pe;
4592 int ch, can_recurse = 0, pflag = 0;
4593 FILE *patch_script_file = NULL;
4594 const char *patch_script_path = NULL;
4595 struct choose_patch_arg cpa;
4599 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4605 patch_script_path = optarg;
4620 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4621 "unveil", NULL) == -1)
4626 if (patch_script_path && !pflag)
4627 errx(1, "-F option can only be used together with -p option");
4629 cwd = getcwd(NULL, 0);
4631 error = got_error_from_errno("getcwd");
4634 error = got_worktree_open(&worktree, cwd);
4638 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4643 if (patch_script_path) {
4644 patch_script_file = fopen(patch_script_path, "r");
4645 if (patch_script_file == NULL) {
4646 error = got_error_from_errno2("fopen",
4651 error = apply_unveil(got_repo_get_path(repo), 1,
4652 got_worktree_get_root_path(worktree));
4656 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4663 TAILQ_FOREACH(pe, &paths, entry) {
4664 if (asprintf(&ondisk_path, "%s/%s",
4665 got_worktree_get_root_path(worktree),
4667 error = got_error_from_errno("asprintf");
4670 if (lstat(ondisk_path, &sb) == -1) {
4671 if (errno == ENOENT) {
4675 error = got_error_from_errno2("lstat",
4681 if (S_ISDIR(sb.st_mode)) {
4682 error = got_error_msg(GOT_ERR_BAD_PATH,
4683 "reverting directories requires -R option");
4689 cpa.patch_script_file = patch_script_file;
4690 cpa.action = "revert";
4691 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4692 pflag ? choose_patch : NULL, &cpa, repo);
4696 if (patch_script_file && fclose(patch_script_file) == EOF &&
4698 error = got_error_from_errno2("fclose", patch_script_path);
4700 got_repo_close(repo);
4702 got_worktree_close(worktree);
4711 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4716 struct collect_commit_logmsg_arg {
4717 const char *cmdline_log;
4719 const char *worktree_path;
4720 const char *branch_name;
4721 const char *repo_path;
4726 static const struct got_error *
4727 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4730 char *initial_content = NULL;
4731 struct got_pathlist_entry *pe;
4732 const struct got_error *err = NULL;
4733 char *template = NULL;
4734 struct collect_commit_logmsg_arg *a = arg;
4738 /* if a message was specified on the command line, just use it */
4739 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4740 len = strlen(a->cmdline_log) + 1;
4741 *logmsg = malloc(len + 1);
4742 if (*logmsg == NULL)
4743 return got_error_from_errno("malloc");
4744 strlcpy(*logmsg, a->cmdline_log, len);
4748 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4749 return got_error_from_errno("asprintf");
4751 if (asprintf(&initial_content,
4752 "\n# changes to be committed on branch %s:\n",
4753 a->branch_name) == -1)
4754 return got_error_from_errno("asprintf");
4756 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4760 dprintf(fd, initial_content);
4762 TAILQ_FOREACH(pe, commitable_paths, entry) {
4763 struct got_commitable *ct = pe->data;
4764 dprintf(fd, "# %c %s\n",
4765 got_commitable_get_status(ct),
4766 got_commitable_get_path(ct));
4770 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4772 free(initial_content);
4775 /* Editor is done; we can now apply unveil(2) */
4777 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4786 static const struct got_error *
4787 cmd_commit(int argc, char *argv[])
4789 const struct got_error *error = NULL;
4790 struct got_worktree *worktree = NULL;
4791 struct got_repository *repo = NULL;
4792 char *cwd = NULL, *id_str = NULL;
4793 struct got_object_id *id = NULL;
4794 const char *logmsg = NULL;
4795 struct collect_commit_logmsg_arg cl_arg;
4796 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4797 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4798 struct got_pathlist_head paths;
4801 cl_arg.logmsg_path = NULL;
4803 while ((ch = getopt(argc, argv, "m:")) != -1) {
4818 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4819 "unveil", NULL) == -1)
4822 cwd = getcwd(NULL, 0);
4824 error = got_error_from_errno("getcwd");
4827 error = got_worktree_open(&worktree, cwd);
4831 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4834 if (rebase_in_progress) {
4835 error = got_error(GOT_ERR_REBASING);
4839 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4844 error = get_gitconfig_path(&gitconfig_path);
4847 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4852 error = get_author(&author, repo);
4857 * unveil(2) traverses exec(2); if an editor is used we have
4858 * to apply unveil after the log message has been written.
4860 if (logmsg == NULL || strlen(logmsg) == 0)
4861 error = get_editor(&editor);
4863 error = apply_unveil(got_repo_get_path(repo), 0,
4864 got_worktree_get_root_path(worktree));
4868 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4872 cl_arg.editor = editor;
4873 cl_arg.cmdline_log = logmsg;
4874 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4875 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4876 if (!histedit_in_progress) {
4877 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4878 error = got_error(GOT_ERR_COMMIT_BRANCH);
4881 cl_arg.branch_name += 11;
4883 cl_arg.repo_path = got_repo_get_path(repo);
4884 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4885 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4887 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4888 cl_arg.logmsg_path != NULL)
4889 preserve_logmsg = 1;
4893 error = got_object_id_str(&id_str, id);
4896 printf("Created commit %s\n", id_str);
4898 if (preserve_logmsg) {
4899 fprintf(stderr, "%s: log message preserved in %s\n",
4900 getprogname(), cl_arg.logmsg_path);
4901 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4903 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4904 free(cl_arg.logmsg_path);
4906 got_repo_close(repo);
4908 got_worktree_close(worktree);
4911 free(gitconfig_path);
4918 usage_cherrypick(void)
4920 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4924 static const struct got_error *
4925 cmd_cherrypick(int argc, char *argv[])
4927 const struct got_error *error = NULL;
4928 struct got_worktree *worktree = NULL;
4929 struct got_repository *repo = NULL;
4930 char *cwd = NULL, *commit_id_str = NULL;
4931 struct got_object_id *commit_id = NULL;
4932 struct got_commit_object *commit = NULL;
4933 struct got_object_qid *pid;
4934 struct got_reference *head_ref = NULL;
4935 int ch, did_something = 0;
4937 while ((ch = getopt(argc, argv, "")) != -1) {
4949 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4950 "unveil", NULL) == -1)
4956 cwd = getcwd(NULL, 0);
4958 error = got_error_from_errno("getcwd");
4961 error = got_worktree_open(&worktree, cwd);
4965 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4970 error = apply_unveil(got_repo_get_path(repo), 0,
4971 got_worktree_get_root_path(worktree));
4975 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4976 GOT_OBJ_TYPE_COMMIT, repo);
4977 if (error != NULL) {
4978 struct got_reference *ref;
4979 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4981 error = got_ref_open(&ref, repo, argv[0], 0);
4984 error = got_ref_resolve(&commit_id, repo, ref);
4989 error = got_object_id_str(&commit_id_str, commit_id);
4993 error = got_ref_open(&head_ref, repo,
4994 got_worktree_get_head_ref_name(worktree), 0);
4998 error = check_same_branch(commit_id, head_ref, NULL, repo);
5000 if (error->code != GOT_ERR_ANCESTRY)
5004 error = got_error(GOT_ERR_SAME_BRANCH);
5008 error = got_object_open_as_commit(&commit, repo, commit_id);
5011 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5012 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5013 commit_id, repo, update_progress, &did_something, check_cancelled,
5019 printf("Merged commit %s\n", commit_id_str);
5022 got_object_commit_close(commit);
5023 free(commit_id_str);
5025 got_ref_close(head_ref);
5027 got_worktree_close(worktree);
5029 got_repo_close(repo);
5036 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5040 static const struct got_error *
5041 cmd_backout(int argc, char *argv[])
5043 const struct got_error *error = NULL;
5044 struct got_worktree *worktree = NULL;
5045 struct got_repository *repo = NULL;
5046 char *cwd = NULL, *commit_id_str = NULL;
5047 struct got_object_id *commit_id = NULL;
5048 struct got_commit_object *commit = NULL;
5049 struct got_object_qid *pid;
5050 struct got_reference *head_ref = NULL;
5051 int ch, did_something = 0;
5053 while ((ch = getopt(argc, argv, "")) != -1) {
5065 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5066 "unveil", NULL) == -1)
5072 cwd = getcwd(NULL, 0);
5074 error = got_error_from_errno("getcwd");
5077 error = got_worktree_open(&worktree, cwd);
5081 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5086 error = apply_unveil(got_repo_get_path(repo), 0,
5087 got_worktree_get_root_path(worktree));
5091 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5092 GOT_OBJ_TYPE_COMMIT, repo);
5093 if (error != NULL) {
5094 struct got_reference *ref;
5095 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5097 error = got_ref_open(&ref, repo, argv[0], 0);
5100 error = got_ref_resolve(&commit_id, repo, ref);
5105 error = got_object_id_str(&commit_id_str, commit_id);
5109 error = got_ref_open(&head_ref, repo,
5110 got_worktree_get_head_ref_name(worktree), 0);
5114 error = check_same_branch(commit_id, head_ref, NULL, repo);
5118 error = got_object_open_as_commit(&commit, repo, commit_id);
5121 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5123 error = got_error(GOT_ERR_ROOT_COMMIT);
5127 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5128 update_progress, &did_something, check_cancelled, NULL);
5133 printf("Backed out commit %s\n", commit_id_str);
5136 got_object_commit_close(commit);
5137 free(commit_id_str);
5139 got_ref_close(head_ref);
5141 got_worktree_close(worktree);
5143 got_repo_close(repo);
5150 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5156 trim_logmsg(char *logmsg, int limit)
5161 len = strlen(logmsg);
5165 nl = strchr(logmsg, '\n');
5170 static const struct got_error *
5171 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5173 const struct got_error *err;
5174 char *logmsg0 = NULL;
5177 err = got_object_commit_get_logmsg(&logmsg0, commit);
5182 while (isspace((unsigned char)s[0]))
5185 *logmsg = strdup(s);
5186 if (*logmsg == NULL) {
5187 err = got_error_from_errno("strdup");
5191 trim_logmsg(*logmsg, limit);
5197 static const struct got_error *
5198 show_rebase_progress(struct got_commit_object *commit,
5199 struct got_object_id *old_id, struct got_object_id *new_id)
5201 const struct got_error *err;
5202 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5204 err = got_object_id_str(&old_id_str, old_id);
5209 err = got_object_id_str(&new_id_str, new_id);
5214 old_id_str[12] = '\0';
5216 new_id_str[12] = '\0';
5218 err = get_short_logmsg(&logmsg, 42, commit);
5222 printf("%s -> %s: %s\n", old_id_str,
5223 new_id_str ? new_id_str : "no-op change", logmsg);
5230 static const struct got_error *
5231 rebase_progress(void *arg, unsigned char status, const char *path)
5233 unsigned char *rebase_status = arg;
5235 while (path[0] == '/')
5237 printf("%c %s\n", status, path);
5239 if (*rebase_status == GOT_STATUS_CONFLICT)
5241 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5242 *rebase_status = status;
5246 static const struct got_error *
5247 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5248 struct got_reference *branch, struct got_reference *new_base_branch,
5249 struct got_reference *tmp_branch, struct got_repository *repo)
5251 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5252 return got_worktree_rebase_complete(worktree, fileindex,
5253 new_base_branch, tmp_branch, branch, repo);
5256 static const struct got_error *
5257 rebase_commit(struct got_pathlist_head *merged_paths,
5258 struct got_worktree *worktree, struct got_fileindex *fileindex,
5259 struct got_reference *tmp_branch,
5260 struct got_object_id *commit_id, struct got_repository *repo)
5262 const struct got_error *error;
5263 struct got_commit_object *commit;
5264 struct got_object_id *new_commit_id;
5266 error = got_object_open_as_commit(&commit, repo, commit_id);
5270 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5271 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5273 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5275 error = show_rebase_progress(commit, commit_id, NULL);
5277 error = show_rebase_progress(commit, commit_id, new_commit_id);
5278 free(new_commit_id);
5281 got_object_commit_close(commit);
5285 struct check_path_prefix_arg {
5286 const char *path_prefix;
5291 static const struct got_error *
5292 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5293 struct got_blob_object *blob2, struct got_object_id *id1,
5294 struct got_object_id *id2, const char *path1, const char *path2,
5295 mode_t mode1, mode_t mode2, struct got_repository *repo)
5297 struct check_path_prefix_arg *a = arg;
5299 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5300 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5301 return got_error(a->errcode);
5306 static const struct got_error *
5307 check_path_prefix(struct got_object_id *parent_id,
5308 struct got_object_id *commit_id, const char *path_prefix,
5309 int errcode, struct got_repository *repo)
5311 const struct got_error *err;
5312 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5313 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5314 struct check_path_prefix_arg cpp_arg;
5316 if (got_path_is_root_dir(path_prefix))
5319 err = got_object_open_as_commit(&commit, repo, commit_id);
5323 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5327 err = got_object_open_as_tree(&tree1, repo,
5328 got_object_commit_get_tree_id(parent_commit));
5332 err = got_object_open_as_tree(&tree2, repo,
5333 got_object_commit_get_tree_id(commit));
5337 cpp_arg.path_prefix = path_prefix;
5338 while (cpp_arg.path_prefix[0] == '/')
5339 cpp_arg.path_prefix++;
5340 cpp_arg.len = strlen(cpp_arg.path_prefix);
5341 cpp_arg.errcode = errcode;
5342 err = got_diff_tree(tree1, tree2, "", "", repo,
5343 check_path_prefix_in_diff, &cpp_arg, 0);
5346 got_object_tree_close(tree1);
5348 got_object_tree_close(tree2);
5350 got_object_commit_close(commit);
5352 got_object_commit_close(parent_commit);
5356 static const struct got_error *
5357 collect_commits(struct got_object_id_queue *commits,
5358 struct got_object_id *initial_commit_id,
5359 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5360 const char *path_prefix, int path_prefix_errcode,
5361 struct got_repository *repo)
5363 const struct got_error *err = NULL;
5364 struct got_commit_graph *graph = NULL;
5365 struct got_object_id *parent_id = NULL;
5366 struct got_object_qid *qid;
5367 struct got_object_id *commit_id = initial_commit_id;
5369 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5373 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5374 check_cancelled, NULL);
5377 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5378 err = got_commit_graph_iter_next(&parent_id, graph);
5380 if (err->code == GOT_ERR_ITER_COMPLETED) {
5381 err = got_error_msg(GOT_ERR_ANCESTRY,
5382 "ran out of commits to rebase before "
5383 "youngest common ancestor commit has "
5386 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5388 err = got_commit_graph_fetch_commits(graph, 1, repo,
5389 check_cancelled, NULL);
5393 err = check_path_prefix(parent_id, commit_id,
5394 path_prefix, path_prefix_errcode, repo);
5398 err = got_object_qid_alloc(&qid, commit_id);
5401 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5402 commit_id = parent_id;
5406 got_commit_graph_close(graph);
5410 static const struct got_error *
5411 cmd_rebase(int argc, char *argv[])
5413 const struct got_error *error = NULL;
5414 struct got_worktree *worktree = NULL;
5415 struct got_repository *repo = NULL;
5416 struct got_fileindex *fileindex = NULL;
5418 struct got_reference *branch = NULL;
5419 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5420 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5421 struct got_object_id *resume_commit_id = NULL;
5422 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5423 struct got_commit_object *commit = NULL;
5424 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5425 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5426 struct got_object_id_queue commits;
5427 struct got_pathlist_head merged_paths;
5428 const struct got_object_id_queue *parent_ids;
5429 struct got_object_qid *qid, *pid;
5431 SIMPLEQ_INIT(&commits);
5432 TAILQ_INIT(&merged_paths);
5434 while ((ch = getopt(argc, argv, "ac")) != -1) {
5440 continue_rebase = 1;
5452 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5453 "unveil", NULL) == -1)
5456 if (abort_rebase && continue_rebase)
5458 else if (abort_rebase || continue_rebase) {
5461 } else if (argc != 1)
5464 cwd = getcwd(NULL, 0);
5466 error = got_error_from_errno("getcwd");
5469 error = got_worktree_open(&worktree, cwd);
5473 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5478 error = apply_unveil(got_repo_get_path(repo), 0,
5479 got_worktree_get_root_path(worktree));
5483 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5489 if (!rebase_in_progress) {
5490 error = got_error(GOT_ERR_NOT_REBASING);
5493 error = got_worktree_rebase_continue(&resume_commit_id,
5494 &new_base_branch, &tmp_branch, &branch, &fileindex,
5498 printf("Switching work tree to %s\n",
5499 got_ref_get_symref_target(new_base_branch));
5500 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5501 new_base_branch, update_progress, &did_something);
5504 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5505 goto done; /* nothing else to do */
5508 if (continue_rebase) {
5509 if (!rebase_in_progress) {
5510 error = got_error(GOT_ERR_NOT_REBASING);
5513 error = got_worktree_rebase_continue(&resume_commit_id,
5514 &new_base_branch, &tmp_branch, &branch, &fileindex,
5519 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5520 resume_commit_id, repo);
5524 yca_id = got_object_id_dup(resume_commit_id);
5525 if (yca_id == NULL) {
5526 error = got_error_from_errno("got_object_id_dup");
5530 error = got_ref_open(&branch, repo, argv[0], 0);
5535 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5539 if (!continue_rebase) {
5540 struct got_object_id *base_commit_id;
5542 base_commit_id = got_worktree_get_base_commit_id(worktree);
5543 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5544 base_commit_id, branch_head_commit_id, repo,
5545 check_cancelled, NULL);
5548 if (yca_id == NULL) {
5549 error = got_error_msg(GOT_ERR_ANCESTRY,
5550 "specified branch shares no common ancestry "
5551 "with work tree's branch");
5555 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5557 if (error->code != GOT_ERR_ANCESTRY)
5561 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5562 "specified branch resolves to a commit which "
5563 "is already contained in work tree's branch");
5566 error = got_worktree_rebase_prepare(&new_base_branch,
5567 &tmp_branch, &fileindex, worktree, branch, repo);
5572 commit_id = branch_head_commit_id;
5573 error = got_object_open_as_commit(&commit, repo, commit_id);
5577 parent_ids = got_object_commit_get_parent_ids(commit);
5578 pid = SIMPLEQ_FIRST(parent_ids);
5580 if (!continue_rebase) {
5582 error = got_worktree_rebase_abort(worktree, fileindex,
5583 repo, new_base_branch, update_progress,
5587 printf("Rebase of %s aborted\n",
5588 got_ref_get_name(branch));
5590 error = got_error(GOT_ERR_EMPTY_REBASE);
5593 error = collect_commits(&commits, commit_id, pid->id,
5594 yca_id, got_worktree_get_path_prefix(worktree),
5595 GOT_ERR_REBASE_PATH, repo);
5596 got_object_commit_close(commit);
5601 if (SIMPLEQ_EMPTY(&commits)) {
5602 if (continue_rebase) {
5603 error = rebase_complete(worktree, fileindex,
5604 branch, new_base_branch, tmp_branch, repo);
5607 /* Fast-forward the reference of the branch. */
5608 struct got_object_id *new_head_commit_id;
5610 error = got_ref_resolve(&new_head_commit_id, repo,
5614 error = got_object_id_str(&id_str, new_head_commit_id);
5615 printf("Forwarding %s to commit %s\n",
5616 got_ref_get_name(branch), id_str);
5618 error = got_ref_change_ref(branch,
5619 new_head_commit_id);
5626 SIMPLEQ_FOREACH(qid, &commits, entry) {
5627 commit_id = qid->id;
5628 parent_id = pid ? pid->id : yca_id;
5631 error = got_worktree_rebase_merge_files(&merged_paths,
5632 worktree, fileindex, parent_id, commit_id, repo,
5633 rebase_progress, &rebase_status, check_cancelled, NULL);
5637 if (rebase_status == GOT_STATUS_CONFLICT) {
5638 got_worktree_rebase_pathlist_free(&merged_paths);
5642 error = rebase_commit(&merged_paths, worktree, fileindex,
5643 tmp_branch, commit_id, repo);
5644 got_worktree_rebase_pathlist_free(&merged_paths);
5649 if (rebase_status == GOT_STATUS_CONFLICT) {
5650 error = got_worktree_rebase_postpone(worktree, fileindex);
5653 error = got_error_msg(GOT_ERR_CONFLICTS,
5654 "conflicts must be resolved before rebasing can continue");
5656 error = rebase_complete(worktree, fileindex, branch,
5657 new_base_branch, tmp_branch, repo);
5659 got_object_id_queue_free(&commits);
5660 free(branch_head_commit_id);
5661 free(resume_commit_id);
5664 got_object_commit_close(commit);
5666 got_ref_close(branch);
5667 if (new_base_branch)
5668 got_ref_close(new_base_branch);
5670 got_ref_close(tmp_branch);
5672 got_worktree_close(worktree);
5674 got_repo_close(repo);
5679 usage_histedit(void)
5681 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5686 #define GOT_HISTEDIT_PICK 'p'
5687 #define GOT_HISTEDIT_EDIT 'e'
5688 #define GOT_HISTEDIT_FOLD 'f'
5689 #define GOT_HISTEDIT_DROP 'd'
5690 #define GOT_HISTEDIT_MESG 'm'
5692 static struct got_histedit_cmd {
5696 } got_histedit_cmds[] = {
5697 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5698 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5699 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5700 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5701 { GOT_HISTEDIT_MESG, "mesg",
5702 "single-line log message for commit above (open editor if empty)" },
5705 struct got_histedit_list_entry {
5706 TAILQ_ENTRY(got_histedit_list_entry) entry;
5707 struct got_object_id *commit_id;
5708 const struct got_histedit_cmd *cmd;
5711 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5713 static const struct got_error *
5714 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5715 FILE *f, struct got_repository *repo)
5717 const struct got_error *err = NULL;
5718 char *logmsg = NULL, *id_str = NULL;
5719 struct got_commit_object *commit = NULL;
5722 err = got_object_open_as_commit(&commit, repo, commit_id);
5726 err = get_short_logmsg(&logmsg, 34, commit);
5730 err = got_object_id_str(&id_str, commit_id);
5734 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5736 err = got_ferror(f, GOT_ERR_IO);
5739 got_object_commit_close(commit);
5745 static const struct got_error *
5746 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5747 struct got_repository *repo)
5749 const struct got_error *err = NULL;
5750 struct got_object_qid *qid;
5752 if (SIMPLEQ_EMPTY(commits))
5753 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5755 SIMPLEQ_FOREACH(qid, commits, entry) {
5756 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5765 static const struct got_error *
5766 write_cmd_list(FILE *f)
5768 const struct got_error *err = NULL;
5771 n = fprintf(f, "# Available histedit commands:\n");
5773 return got_ferror(f, GOT_ERR_IO);
5775 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5776 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5777 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5780 err = got_ferror(f, GOT_ERR_IO);
5784 n = fprintf(f, "# Commits will be processed in order from top to "
5785 "bottom of this file.\n");
5787 return got_ferror(f, GOT_ERR_IO);
5791 static const struct got_error *
5792 histedit_syntax_error(int lineno)
5794 static char msg[42];
5797 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5799 if (ret == -1 || ret >= sizeof(msg))
5800 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5802 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5805 static const struct got_error *
5806 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5807 char *logmsg, struct got_repository *repo)
5809 const struct got_error *err;
5810 struct got_commit_object *folded_commit = NULL;
5811 char *id_str, *folded_logmsg = NULL;
5813 err = got_object_id_str(&id_str, hle->commit_id);
5817 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5821 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5824 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5825 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5826 folded_logmsg) == -1) {
5827 err = got_error_from_errno("asprintf");
5832 got_object_commit_close(folded_commit);
5834 free(folded_logmsg);
5838 static struct got_histedit_list_entry *
5839 get_folded_commits(struct got_histedit_list_entry *hle)
5841 struct got_histedit_list_entry *prev, *folded = NULL;
5843 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5844 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5845 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5846 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5848 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5854 static const struct got_error *
5855 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5856 struct got_repository *repo)
5858 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5859 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5860 const struct got_error *err = NULL;
5861 struct got_commit_object *commit = NULL;
5863 struct got_histedit_list_entry *folded = NULL;
5865 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5869 folded = get_folded_commits(hle);
5871 while (folded != hle) {
5872 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5873 folded = TAILQ_NEXT(folded, entry);
5876 err = append_folded_commit_msg(&new_msg, folded,
5882 folded = TAILQ_NEXT(folded, entry);
5886 err = got_object_id_str(&id_str, hle->commit_id);
5889 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5892 if (asprintf(&new_msg,
5893 "%s\n# original log message of commit %s: %s",
5894 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5895 err = got_error_from_errno("asprintf");
5901 err = got_object_id_str(&id_str, hle->commit_id);
5905 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5909 dprintf(fd, logmsg);
5912 err = get_editor(&editor);
5916 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5918 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5920 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5923 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5924 err = got_error_from_errno2("unlink", logmsg_path);
5930 got_object_commit_close(commit);
5934 static const struct got_error *
5935 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5936 FILE *f, struct got_repository *repo)
5938 const struct got_error *err = NULL;
5939 char *line = NULL, *p, *end;
5943 const struct got_histedit_cmd *cmd;
5944 struct got_object_id *commit_id = NULL;
5945 struct got_histedit_list_entry *hle = NULL;
5948 len = getline(&line, &size, f);
5950 const struct got_error *getline_err;
5953 getline_err = got_error_from_errno("getline");
5954 err = got_ferror(f, getline_err->code);
5959 while (isspace((unsigned char)p[0]))
5961 if (p[0] == '#' || p[0] == '\0') {
5967 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5968 cmd = &got_histedit_cmds[i];
5969 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5970 isspace((unsigned char)p[strlen(cmd->name)])) {
5971 p += strlen(cmd->name);
5974 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5979 if (i == nitems(got_histedit_cmds)) {
5980 err = histedit_syntax_error(lineno);
5983 while (isspace((unsigned char)p[0]))
5985 if (cmd->code == GOT_HISTEDIT_MESG) {
5986 if (hle == NULL || hle->logmsg != NULL) {
5987 err = got_error(GOT_ERR_HISTEDIT_CMD);
5991 err = histedit_edit_logmsg(hle, repo);
5995 hle->logmsg = strdup(p);
5996 if (hle->logmsg == NULL) {
5997 err = got_error_from_errno("strdup");
6006 while (end[0] && !isspace((unsigned char)end[0]))
6010 err = got_object_resolve_id_str(&commit_id, repo, p);
6012 /* override error code */
6013 err = histedit_syntax_error(lineno);
6017 hle = malloc(sizeof(*hle));
6019 err = got_error_from_errno("malloc");
6023 hle->commit_id = commit_id;
6028 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6036 static const struct got_error *
6037 histedit_check_script(struct got_histedit_list *histedit_cmds,
6038 struct got_object_id_queue *commits, struct got_repository *repo)
6040 const struct got_error *err = NULL;
6041 struct got_object_qid *qid;
6042 struct got_histedit_list_entry *hle;
6043 static char msg[80];
6046 if (TAILQ_EMPTY(histedit_cmds))
6047 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6048 "histedit script contains no commands");
6049 if (SIMPLEQ_EMPTY(commits))
6050 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6052 SIMPLEQ_FOREACH(qid, commits, entry) {
6053 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6054 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6058 err = got_object_id_str(&id_str, qid->id);
6061 snprintf(msg, sizeof(msg),
6062 "commit %s missing from histedit script", id_str);
6064 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6068 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6069 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6070 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6071 "last commit in histedit script cannot be folded");
6076 static const struct got_error *
6077 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6078 const char *path, struct got_object_id_queue *commits,
6079 struct got_repository *repo)
6081 const struct got_error *err = NULL;
6085 err = get_editor(&editor);
6089 if (spawn_editor(editor, path) == -1) {
6090 err = got_error_from_errno("failed spawning editor");
6094 f = fopen(path, "r");
6096 err = got_error_from_errno("fopen");
6099 err = histedit_parse_list(histedit_cmds, f, repo);
6103 err = histedit_check_script(histedit_cmds, commits, repo);
6105 if (f && fclose(f) != 0 && err == NULL)
6106 err = got_error_from_errno("fclose");
6111 static const struct got_error *
6112 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6113 struct got_object_id_queue *, const char *, struct got_repository *);
6115 static const struct got_error *
6116 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6117 struct got_object_id_queue *commits, struct got_repository *repo)
6119 const struct got_error *err;
6123 err = got_opentemp_named(&path, &f, "got-histedit");
6127 err = write_cmd_list(f);
6131 err = histedit_write_commit_list(commits, f, repo);
6135 if (fclose(f) != 0) {
6136 err = got_error_from_errno("fclose");
6141 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6143 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6144 err->code != GOT_ERR_HISTEDIT_CMD)
6146 err = histedit_edit_list_retry(histedit_cmds, err,
6147 commits, path, repo);
6150 if (f && fclose(f) != 0 && err == NULL)
6151 err = got_error_from_errno("fclose");
6152 if (path && unlink(path) != 0 && err == NULL)
6153 err = got_error_from_errno2("unlink", path);
6158 static const struct got_error *
6159 histedit_save_list(struct got_histedit_list *histedit_cmds,
6160 struct got_worktree *worktree, struct got_repository *repo)
6162 const struct got_error *err = NULL;
6165 struct got_histedit_list_entry *hle;
6166 struct got_commit_object *commit = NULL;
6168 err = got_worktree_get_histedit_script_path(&path, worktree);
6172 f = fopen(path, "w");
6174 err = got_error_from_errno2("fopen", path);
6177 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6178 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6184 int n = fprintf(f, "%c %s\n",
6185 GOT_HISTEDIT_MESG, hle->logmsg);
6187 err = got_ferror(f, GOT_ERR_IO);
6193 if (f && fclose(f) != 0 && err == NULL)
6194 err = got_error_from_errno("fclose");
6197 got_object_commit_close(commit);
6202 histedit_free_list(struct got_histedit_list *histedit_cmds)
6204 struct got_histedit_list_entry *hle;
6206 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6207 TAILQ_REMOVE(histedit_cmds, hle, entry);
6212 static const struct got_error *
6213 histedit_load_list(struct got_histedit_list *histedit_cmds,
6214 const char *path, struct got_repository *repo)
6216 const struct got_error *err = NULL;
6219 f = fopen(path, "r");
6221 err = got_error_from_errno2("fopen", path);
6225 err = histedit_parse_list(histedit_cmds, f, repo);
6227 if (f && fclose(f) != 0 && err == NULL)
6228 err = got_error_from_errno("fclose");
6232 static const struct got_error *
6233 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6234 const struct got_error *edit_err, struct got_object_id_queue *commits,
6235 const char *path, struct got_repository *repo)
6237 const struct got_error *err = NULL, *prev_err = edit_err;
6240 while (resp != 'c' && resp != 'r' && resp != 'a') {
6241 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6242 "or (a)bort: ", getprogname(), prev_err->msg);
6247 histedit_free_list(histedit_cmds);
6248 err = histedit_run_editor(histedit_cmds, path, commits,
6251 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6252 err->code != GOT_ERR_HISTEDIT_CMD)
6259 } else if (resp == 'r') {
6260 histedit_free_list(histedit_cmds);
6261 err = histedit_edit_script(histedit_cmds,
6264 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6265 err->code != GOT_ERR_HISTEDIT_CMD)
6272 } else if (resp == 'a') {
6273 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6276 printf("invalid response '%c'\n", resp);
6282 static const struct got_error *
6283 histedit_complete(struct got_worktree *worktree,
6284 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6285 struct got_reference *branch, struct got_repository *repo)
6287 printf("Switching work tree to %s\n",
6288 got_ref_get_symref_target(branch));
6289 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6293 static const struct got_error *
6294 show_histedit_progress(struct got_commit_object *commit,
6295 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6297 const struct got_error *err;
6298 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6300 err = got_object_id_str(&old_id_str, hle->commit_id);
6305 err = got_object_id_str(&new_id_str, new_id);
6310 old_id_str[12] = '\0';
6312 new_id_str[12] = '\0';
6315 logmsg = strdup(hle->logmsg);
6316 if (logmsg == NULL) {
6317 err = got_error_from_errno("strdup");
6320 trim_logmsg(logmsg, 42);
6322 err = get_short_logmsg(&logmsg, 42, commit);
6327 switch (hle->cmd->code) {
6328 case GOT_HISTEDIT_PICK:
6329 case GOT_HISTEDIT_EDIT:
6330 printf("%s -> %s: %s\n", old_id_str,
6331 new_id_str ? new_id_str : "no-op change", logmsg);
6333 case GOT_HISTEDIT_DROP:
6334 case GOT_HISTEDIT_FOLD:
6335 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6348 static const struct got_error *
6349 histedit_commit(struct got_pathlist_head *merged_paths,
6350 struct got_worktree *worktree, struct got_fileindex *fileindex,
6351 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6352 struct got_repository *repo)
6354 const struct got_error *err;
6355 struct got_commit_object *commit;
6356 struct got_object_id *new_commit_id;
6358 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6359 && hle->logmsg == NULL) {
6360 err = histedit_edit_logmsg(hle, repo);
6365 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6369 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6370 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6373 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6375 err = show_histedit_progress(commit, hle, NULL);
6377 err = show_histedit_progress(commit, hle, new_commit_id);
6378 free(new_commit_id);
6381 got_object_commit_close(commit);
6385 static const struct got_error *
6386 histedit_skip_commit(struct got_histedit_list_entry *hle,
6387 struct got_worktree *worktree, struct got_repository *repo)
6389 const struct got_error *error;
6390 struct got_commit_object *commit;
6392 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6397 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6401 error = show_histedit_progress(commit, hle, NULL);
6402 got_object_commit_close(commit);
6406 static const struct got_error *
6407 cmd_histedit(int argc, char *argv[])
6409 const struct got_error *error = NULL;
6410 struct got_worktree *worktree = NULL;
6411 struct got_fileindex *fileindex = NULL;
6412 struct got_repository *repo = NULL;
6414 struct got_reference *branch = NULL;
6415 struct got_reference *tmp_branch = NULL;
6416 struct got_object_id *resume_commit_id = NULL;
6417 struct got_object_id *base_commit_id = NULL;
6418 struct got_object_id *head_commit_id = NULL;
6419 struct got_commit_object *commit = NULL;
6420 int ch, rebase_in_progress = 0, did_something;
6421 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6422 const char *edit_script_path = NULL;
6423 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6424 struct got_object_id_queue commits;
6425 struct got_pathlist_head merged_paths;
6426 const struct got_object_id_queue *parent_ids;
6427 struct got_object_qid *pid;
6428 struct got_histedit_list histedit_cmds;
6429 struct got_histedit_list_entry *hle;
6431 SIMPLEQ_INIT(&commits);
6432 TAILQ_INIT(&histedit_cmds);
6433 TAILQ_INIT(&merged_paths);
6435 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6444 edit_script_path = optarg;
6456 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6457 "unveil", NULL) == -1)
6460 if (abort_edit && continue_edit)
6466 * This command cannot apply unveil(2) in all cases because the
6467 * user may choose to run an editor to edit the histedit script
6468 * and to edit individual commit log messages.
6469 * unveil(2) traverses exec(2); if an editor is used we have to
6470 * apply unveil after edit script and log messages have been written.
6471 * XXX TODO: Make use of unveil(2) where possible.
6474 cwd = getcwd(NULL, 0);
6476 error = got_error_from_errno("getcwd");
6479 error = got_worktree_open(&worktree, cwd);
6483 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6488 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6491 if (rebase_in_progress) {
6492 error = got_error(GOT_ERR_REBASING);
6496 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6500 if (edit_in_progress && abort_edit) {
6501 error = got_worktree_histedit_continue(&resume_commit_id,
6502 &tmp_branch, &branch, &base_commit_id, &fileindex,
6506 printf("Switching work tree to %s\n",
6507 got_ref_get_symref_target(branch));
6508 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6509 branch, base_commit_id, update_progress, &did_something);
6512 printf("Histedit of %s aborted\n",
6513 got_ref_get_symref_target(branch));
6514 goto done; /* nothing else to do */
6515 } else if (abort_edit) {
6516 error = got_error(GOT_ERR_NOT_HISTEDIT);
6520 if (continue_edit) {
6523 if (!edit_in_progress) {
6524 error = got_error(GOT_ERR_NOT_HISTEDIT);
6528 error = got_worktree_get_histedit_script_path(&path, worktree);
6532 error = histedit_load_list(&histedit_cmds, path, repo);
6537 error = got_worktree_histedit_continue(&resume_commit_id,
6538 &tmp_branch, &branch, &base_commit_id, &fileindex,
6543 error = got_ref_resolve(&head_commit_id, repo, branch);
6547 error = got_object_open_as_commit(&commit, repo,
6551 parent_ids = got_object_commit_get_parent_ids(commit);
6552 pid = SIMPLEQ_FIRST(parent_ids);
6554 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6557 error = collect_commits(&commits, head_commit_id, pid->id,
6558 base_commit_id, got_worktree_get_path_prefix(worktree),
6559 GOT_ERR_HISTEDIT_PATH, repo);
6560 got_object_commit_close(commit);
6565 if (edit_in_progress) {
6566 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6570 error = got_ref_open(&branch, repo,
6571 got_worktree_get_head_ref_name(worktree), 0);
6575 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6576 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6577 "will not edit commit history of a branch outside "
6578 "the \"refs/heads/\" reference namespace");
6582 error = got_ref_resolve(&head_commit_id, repo, branch);
6583 got_ref_close(branch);
6588 error = got_object_open_as_commit(&commit, repo,
6592 parent_ids = got_object_commit_get_parent_ids(commit);
6593 pid = SIMPLEQ_FIRST(parent_ids);
6595 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6598 error = collect_commits(&commits, head_commit_id, pid->id,
6599 got_worktree_get_base_commit_id(worktree),
6600 got_worktree_get_path_prefix(worktree),
6601 GOT_ERR_HISTEDIT_PATH, repo);
6602 got_object_commit_close(commit);
6607 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6608 &base_commit_id, &fileindex, worktree, repo);
6612 if (edit_script_path) {
6613 error = histedit_load_list(&histedit_cmds,
6614 edit_script_path, repo);
6616 got_worktree_histedit_abort(worktree, fileindex,
6617 repo, branch, base_commit_id,
6618 update_progress, &did_something);
6622 error = histedit_edit_script(&histedit_cmds, &commits,
6625 got_worktree_histedit_abort(worktree, fileindex,
6626 repo, branch, base_commit_id,
6627 update_progress, &did_something);
6633 error = histedit_save_list(&histedit_cmds, worktree,
6636 got_worktree_histedit_abort(worktree, fileindex,
6637 repo, branch, base_commit_id,
6638 update_progress, &did_something);
6644 error = histedit_check_script(&histedit_cmds, &commits, repo);
6648 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6649 if (resume_commit_id) {
6650 if (got_object_id_cmp(hle->commit_id,
6651 resume_commit_id) != 0)
6654 resume_commit_id = NULL;
6655 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6656 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6657 error = histedit_skip_commit(hle, worktree,
6660 error = histedit_commit(NULL, worktree,
6661 fileindex, tmp_branch, hle, repo);
6668 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6669 error = histedit_skip_commit(hle, worktree, repo);
6675 error = got_object_open_as_commit(&commit, repo,
6679 parent_ids = got_object_commit_get_parent_ids(commit);
6680 pid = SIMPLEQ_FIRST(parent_ids);
6682 error = got_worktree_histedit_merge_files(&merged_paths,
6683 worktree, fileindex, pid->id, hle->commit_id, repo,
6684 rebase_progress, &rebase_status, check_cancelled, NULL);
6687 got_object_commit_close(commit);
6690 if (rebase_status == GOT_STATUS_CONFLICT) {
6691 got_worktree_rebase_pathlist_free(&merged_paths);
6695 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6697 error = got_object_id_str(&id_str, hle->commit_id);
6700 printf("Stopping histedit for amending commit %s\n",
6703 got_worktree_rebase_pathlist_free(&merged_paths);
6704 error = got_worktree_histedit_postpone(worktree,
6709 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6710 error = histedit_skip_commit(hle, worktree, repo);
6716 error = histedit_commit(&merged_paths, worktree, fileindex,
6717 tmp_branch, hle, repo);
6718 got_worktree_rebase_pathlist_free(&merged_paths);
6723 if (rebase_status == GOT_STATUS_CONFLICT) {
6724 error = got_worktree_histedit_postpone(worktree, fileindex);
6727 error = got_error_msg(GOT_ERR_CONFLICTS,
6728 "conflicts must be resolved before rebasing can continue");
6730 error = histedit_complete(worktree, fileindex, tmp_branch,
6733 got_object_id_queue_free(&commits);
6734 histedit_free_list(&histedit_cmds);
6735 free(head_commit_id);
6736 free(base_commit_id);
6737 free(resume_commit_id);
6739 got_object_commit_close(commit);
6741 got_ref_close(branch);
6743 got_ref_close(tmp_branch);
6745 got_worktree_close(worktree);
6747 got_repo_close(repo);
6752 usage_integrate(void)
6754 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6758 static const struct got_error *
6759 cmd_integrate(int argc, char *argv[])
6761 const struct got_error *error = NULL;
6762 struct got_repository *repo = NULL;
6763 struct got_worktree *worktree = NULL;
6764 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6765 const char *branch_arg = NULL;
6766 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6767 struct got_fileindex *fileindex = NULL;
6768 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6769 int ch, did_something = 0;
6771 while ((ch = getopt(argc, argv, "")) != -1) {
6784 branch_arg = argv[0];
6786 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6787 "unveil", NULL) == -1)
6790 cwd = getcwd(NULL, 0);
6792 error = got_error_from_errno("getcwd");
6796 error = got_worktree_open(&worktree, cwd);
6800 error = check_rebase_or_histedit_in_progress(worktree);
6804 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6809 error = apply_unveil(got_repo_get_path(repo), 0,
6810 got_worktree_get_root_path(worktree));
6814 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6815 error = got_error_from_errno("asprintf");
6819 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6820 &base_branch_ref, worktree, refname, repo);
6824 refname = strdup(got_ref_get_name(branch_ref));
6825 if (refname == NULL) {
6826 error = got_error_from_errno("strdup");
6827 got_worktree_integrate_abort(worktree, fileindex, repo,
6828 branch_ref, base_branch_ref);
6831 base_refname = strdup(got_ref_get_name(base_branch_ref));
6832 if (base_refname == NULL) {
6833 error = got_error_from_errno("strdup");
6834 got_worktree_integrate_abort(worktree, fileindex, repo,
6835 branch_ref, base_branch_ref);
6839 error = got_ref_resolve(&commit_id, repo, branch_ref);
6843 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6847 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6848 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6849 "specified branch has already been integrated");
6850 got_worktree_integrate_abort(worktree, fileindex, repo,
6851 branch_ref, base_branch_ref);
6855 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6857 if (error->code == GOT_ERR_ANCESTRY)
6858 error = got_error(GOT_ERR_REBASE_REQUIRED);
6859 got_worktree_integrate_abort(worktree, fileindex, repo,
6860 branch_ref, base_branch_ref);
6864 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6865 branch_ref, base_branch_ref, update_progress, &did_something,
6866 check_cancelled, NULL);
6870 printf("Integrated %s into %s\n", refname, base_refname);
6873 got_repo_close(repo);
6875 got_worktree_close(worktree);
6877 free(base_commit_id);
6887 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6888 "[file-path ...]\n",
6893 static const struct got_error *
6894 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6895 const char *path, struct got_object_id *blob_id,
6896 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6897 int dirfd, const char *de_name)
6899 const struct got_error *err = NULL;
6900 char *id_str = NULL;
6902 if (staged_status != GOT_STATUS_ADD &&
6903 staged_status != GOT_STATUS_MODIFY &&
6904 staged_status != GOT_STATUS_DELETE)
6907 if (staged_status == GOT_STATUS_ADD ||
6908 staged_status == GOT_STATUS_MODIFY)
6909 err = got_object_id_str(&id_str, staged_blob_id);
6911 err = got_object_id_str(&id_str, blob_id);
6915 printf("%s %c %s\n", id_str, staged_status, path);
6920 static const struct got_error *
6921 cmd_stage(int argc, char *argv[])
6923 const struct got_error *error = NULL;
6924 struct got_repository *repo = NULL;
6925 struct got_worktree *worktree = NULL;
6927 struct got_pathlist_head paths;
6928 struct got_pathlist_entry *pe;
6929 int ch, list_stage = 0, pflag = 0;
6930 FILE *patch_script_file = NULL;
6931 const char *patch_script_path = NULL;
6932 struct choose_patch_arg cpa;
6936 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6945 patch_script_path = optarg;
6957 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6958 "unveil", NULL) == -1)
6961 if (list_stage && (pflag || patch_script_path))
6962 errx(1, "-l option cannot be used with other options");
6963 if (patch_script_path && !pflag)
6964 errx(1, "-F option can only be used together with -p option");
6966 cwd = getcwd(NULL, 0);
6968 error = got_error_from_errno("getcwd");
6972 error = got_worktree_open(&worktree, cwd);
6976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6981 if (patch_script_path) {
6982 patch_script_file = fopen(patch_script_path, "r");
6983 if (patch_script_file == NULL) {
6984 error = got_error_from_errno2("fopen",
6989 error = apply_unveil(got_repo_get_path(repo), 0,
6990 got_worktree_get_root_path(worktree));
6994 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6999 error = got_worktree_status(worktree, &paths, repo,
7000 print_stage, NULL, check_cancelled, NULL);
7002 cpa.patch_script_file = patch_script_file;
7003 cpa.action = "stage";
7004 error = got_worktree_stage(worktree, &paths,
7005 pflag ? NULL : print_status, NULL,
7006 pflag ? choose_patch : NULL, &cpa, repo);
7009 if (patch_script_file && fclose(patch_script_file) == EOF &&
7011 error = got_error_from_errno2("fclose", patch_script_path);
7013 got_repo_close(repo);
7015 got_worktree_close(worktree);
7016 TAILQ_FOREACH(pe, &paths, entry)
7017 free((char *)pe->path);
7018 got_pathlist_free(&paths);
7026 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7027 "[file-path ...]\n",
7033 static const struct got_error *
7034 cmd_unstage(int argc, char *argv[])
7036 const struct got_error *error = NULL;
7037 struct got_repository *repo = NULL;
7038 struct got_worktree *worktree = NULL;
7040 struct got_pathlist_head paths;
7041 struct got_pathlist_entry *pe;
7042 int ch, did_something = 0, pflag = 0;
7043 FILE *patch_script_file = NULL;
7044 const char *patch_script_path = NULL;
7045 struct choose_patch_arg cpa;
7049 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7055 patch_script_path = optarg;
7067 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7068 "unveil", NULL) == -1)
7071 if (patch_script_path && !pflag)
7072 errx(1, "-F option can only be used together with -p option");
7074 cwd = getcwd(NULL, 0);
7076 error = got_error_from_errno("getcwd");
7080 error = got_worktree_open(&worktree, cwd);
7084 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7089 if (patch_script_path) {
7090 patch_script_file = fopen(patch_script_path, "r");
7091 if (patch_script_file == NULL) {
7092 error = got_error_from_errno2("fopen",
7098 error = apply_unveil(got_repo_get_path(repo), 0,
7099 got_worktree_get_root_path(worktree));
7103 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7107 cpa.patch_script_file = patch_script_file;
7108 cpa.action = "unstage";
7109 error = got_worktree_unstage(worktree, &paths, update_progress,
7110 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7112 if (patch_script_file && fclose(patch_script_file) == EOF &&
7114 error = got_error_from_errno2("fclose", patch_script_path);
7116 got_repo_close(repo);
7118 got_worktree_close(worktree);
7119 TAILQ_FOREACH(pe, &paths, entry)
7120 free((char *)pe->path);
7121 got_pathlist_free(&paths);
7129 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7130 "arg1 [arg2 ...]\n", getprogname());
7134 static const struct got_error *
7135 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7137 const struct got_error *err;
7138 struct got_blob_object *blob;
7140 err = got_object_open_as_blob(&blob, repo, id, 8192);
7144 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7145 got_object_blob_close(blob);
7149 static const struct got_error *
7150 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7152 const struct got_error *err;
7153 struct got_tree_object *tree;
7156 err = got_object_open_as_tree(&tree, repo, id);
7160 nentries = got_object_tree_get_nentries(tree);
7161 for (i = 0; i < nentries; i++) {
7162 struct got_tree_entry *te;
7164 if (sigint_received || sigpipe_received)
7166 te = got_object_tree_get_entry(tree, i);
7167 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7170 fprintf(outfile, "%s %.7o %s\n", id_str,
7171 got_tree_entry_get_mode(te),
7172 got_tree_entry_get_name(te));
7176 got_object_tree_close(tree);
7180 static const struct got_error *
7181 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7183 const struct got_error *err;
7184 struct got_commit_object *commit;
7185 const struct got_object_id_queue *parent_ids;
7186 struct got_object_qid *pid;
7187 char *id_str = NULL;
7188 const char *logmsg = NULL;
7190 err = got_object_open_as_commit(&commit, repo, id);
7194 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7198 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7199 parent_ids = got_object_commit_get_parent_ids(commit);
7200 fprintf(outfile, "numparents %d\n",
7201 got_object_commit_get_nparents(commit));
7202 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7204 err = got_object_id_str(&pid_str, pid->id);
7207 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7210 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7211 got_object_commit_get_author(commit),
7212 got_object_commit_get_author_time(commit));
7214 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7215 got_object_commit_get_author(commit),
7216 got_object_commit_get_committer_time(commit));
7218 logmsg = got_object_commit_get_logmsg_raw(commit);
7219 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7220 fprintf(outfile, "%s", logmsg);
7223 got_object_commit_close(commit);
7227 static const struct got_error *
7228 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7230 const struct got_error *err;
7231 struct got_tag_object *tag;
7232 char *id_str = NULL;
7233 const char *tagmsg = NULL;
7235 err = got_object_open_as_tag(&tag, repo, id);
7239 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7243 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7245 switch (got_object_tag_get_object_type(tag)) {
7246 case GOT_OBJ_TYPE_BLOB:
7247 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7248 GOT_OBJ_LABEL_BLOB);
7250 case GOT_OBJ_TYPE_TREE:
7251 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7252 GOT_OBJ_LABEL_TREE);
7254 case GOT_OBJ_TYPE_COMMIT:
7255 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7256 GOT_OBJ_LABEL_COMMIT);
7258 case GOT_OBJ_TYPE_TAG:
7259 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7266 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7267 got_object_tag_get_name(tag));
7269 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7270 got_object_tag_get_tagger(tag),
7271 got_object_tag_get_tagger_time(tag));
7273 tagmsg = got_object_tag_get_message(tag);
7274 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7275 fprintf(outfile, "%s", tagmsg);
7278 got_object_tag_close(tag);
7282 static const struct got_error *
7283 cmd_cat(int argc, char *argv[])
7285 const struct got_error *error;
7286 struct got_repository *repo = NULL;
7287 struct got_worktree *worktree = NULL;
7288 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7289 const char *commit_id_str = NULL;
7290 struct got_object_id *id = NULL, *commit_id = NULL;
7291 int ch, obj_type, i, force_path = 0;
7294 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7299 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7302 commit_id_str = optarg;
7305 repo_path = realpath(optarg, NULL);
7306 if (repo_path == NULL)
7307 return got_error_from_errno2("realpath",
7309 got_path_strip_trailing_slashes(repo_path);
7323 cwd = getcwd(NULL, 0);
7325 error = got_error_from_errno("getcwd");
7328 error = got_worktree_open(&worktree, cwd);
7329 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7332 if (repo_path == NULL) {
7334 got_worktree_get_repo_path(worktree));
7335 if (repo_path == NULL) {
7336 error = got_error_from_errno("strdup");
7342 if (repo_path == NULL) {
7343 repo_path = getcwd(NULL, 0);
7344 if (repo_path == NULL)
7345 return got_error_from_errno("getcwd");
7348 error = got_repo_open(&repo, repo_path, NULL);
7353 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7357 if (commit_id_str == NULL)
7358 commit_id_str = GOT_REF_HEAD;
7359 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7363 for (i = 0; i < argc; i++) {
7365 error = got_object_id_by_path(&id, repo, commit_id,
7370 error = match_object_id(&id, &label, argv[i],
7371 GOT_OBJ_TYPE_ANY, 0, repo);
7373 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7374 error->code != GOT_ERR_NOT_REF)
7376 error = got_object_id_by_path(&id, repo,
7377 commit_id, argv[i]);
7383 error = got_object_get_type(&obj_type, repo, id);
7388 case GOT_OBJ_TYPE_BLOB:
7389 error = cat_blob(id, repo, stdout);
7391 case GOT_OBJ_TYPE_TREE:
7392 error = cat_tree(id, repo, stdout);
7394 case GOT_OBJ_TYPE_COMMIT:
7395 error = cat_commit(id, repo, stdout);
7397 case GOT_OBJ_TYPE_TAG:
7398 error = cat_tag(id, repo, stdout);
7401 error = got_error(GOT_ERR_OBJ_TYPE);
7417 got_worktree_close(worktree);
7419 const struct got_error *repo_error;
7420 repo_error = got_repo_close(repo);