2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
38 #include "got_version.h"
39 #include "got_error.h"
40 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository.h"
44 #include "got_cancel.h"
45 #include "got_worktree.h"
47 #include "got_commit_graph.h"
48 #include "got_blame.h"
49 #include "got_privsep.h"
50 #include "got_opentemp.h"
53 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 static volatile sig_atomic_t sigint_received;
57 static volatile sig_atomic_t sigpipe_received;
60 catch_sigint(int signo)
66 catch_sigpipe(int signo)
74 const struct got_error *(*cmd_main)(int, char *[]);
75 void (*cmd_usage)(void);
76 const char *cmd_alias;
79 __dead static void usage(int);
80 __dead static void usage_init(void);
81 __dead static void usage_import(void);
82 __dead static void usage_checkout(void);
83 __dead static void usage_update(void);
84 __dead static void usage_log(void);
85 __dead static void usage_diff(void);
86 __dead static void usage_blame(void);
87 __dead static void usage_tree(void);
88 __dead static void usage_status(void);
89 __dead static void usage_ref(void);
90 __dead static void usage_branch(void);
91 __dead static void usage_tag(void);
92 __dead static void usage_add(void);
93 __dead static void usage_remove(void);
94 __dead static void usage_revert(void);
95 __dead static void usage_commit(void);
96 __dead static void usage_cherrypick(void);
97 __dead static void usage_backout(void);
98 __dead static void usage_rebase(void);
99 __dead static void usage_histedit(void);
100 __dead static void usage_integrate(void);
101 __dead static void usage_stage(void);
102 __dead static void usage_unstage(void);
103 __dead static void usage_cat(void);
105 static const struct got_error* cmd_init(int, char *[]);
106 static const struct got_error* cmd_import(int, char *[]);
107 static const struct got_error* cmd_checkout(int, char *[]);
108 static const struct got_error* cmd_update(int, char *[]);
109 static const struct got_error* cmd_log(int, char *[]);
110 static const struct got_error* cmd_diff(int, char *[]);
111 static const struct got_error* cmd_blame(int, char *[]);
112 static const struct got_error* cmd_tree(int, char *[]);
113 static const struct got_error* cmd_status(int, char *[]);
114 static const struct got_error* cmd_ref(int, char *[]);
115 static const struct got_error* cmd_branch(int, char *[]);
116 static const struct got_error* cmd_tag(int, char *[]);
117 static const struct got_error* cmd_add(int, char *[]);
118 static const struct got_error* cmd_remove(int, char *[]);
119 static const struct got_error* cmd_revert(int, char *[]);
120 static const struct got_error* cmd_commit(int, char *[]);
121 static const struct got_error* cmd_cherrypick(int, char *[]);
122 static const struct got_error* cmd_backout(int, char *[]);
123 static const struct got_error* cmd_rebase(int, char *[]);
124 static const struct got_error* cmd_histedit(int, char *[]);
125 static const struct got_error* cmd_integrate(int, char *[]);
126 static const struct got_error* cmd_stage(int, char *[]);
127 static const struct got_error* cmd_unstage(int, char *[]);
128 static const struct got_error* cmd_cat(int, char *[]);
130 static struct got_cmd got_commands[] = {
131 { "init", cmd_init, usage_init, "in" },
132 { "import", cmd_import, usage_import, "im" },
133 { "checkout", cmd_checkout, usage_checkout, "co" },
134 { "update", cmd_update, usage_update, "up" },
135 { "log", cmd_log, usage_log, "" },
136 { "diff", cmd_diff, usage_diff, "di" },
137 { "blame", cmd_blame, usage_blame, "bl" },
138 { "tree", cmd_tree, usage_tree, "tr" },
139 { "status", cmd_status, usage_status, "st" },
140 { "ref", cmd_ref, usage_ref, "" },
141 { "branch", cmd_branch, usage_branch, "br" },
142 { "tag", cmd_tag, usage_tag, "" },
143 { "add", cmd_add, usage_add, "" },
144 { "remove", cmd_remove, usage_remove, "rm" },
145 { "revert", cmd_revert, usage_revert, "rv" },
146 { "commit", cmd_commit, usage_commit, "ci" },
147 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 { "backout", cmd_backout, usage_backout, "bo" },
149 { "rebase", cmd_rebase, usage_rebase, "rb" },
150 { "histedit", cmd_histedit, usage_histedit, "he" },
151 { "integrate", cmd_integrate, usage_integrate,"ig" },
152 { "stage", cmd_stage, usage_stage, "sg" },
153 { "unstage", cmd_unstage, usage_unstage, "ug" },
154 { "cat", cmd_cat, usage_cat, "" },
162 fprintf(stderr, "commands:");
163 for (i = 0; i < nitems(got_commands); i++) {
164 struct got_cmd *cmd = &got_commands[i];
165 fprintf(stderr, " %s", cmd->cmd_name);
171 main(int argc, char *argv[])
176 int hflag = 0, Vflag = 0;
178 setlocale(LC_CTYPE, "");
180 while ((ch = getopt(argc, argv, "hV")) != -1) {
199 got_version_print_str();
206 signal(SIGINT, catch_sigint);
207 signal(SIGPIPE, catch_sigpipe);
209 for (i = 0; i < nitems(got_commands); i++) {
210 const struct got_error *error;
212 cmd = &got_commands[i];
214 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 strcmp(cmd->cmd_alias, argv[0]) != 0)
219 got_commands[i].cmd_usage();
221 error = got_commands[i].cmd_main(argc, argv);
222 if (error && error->code != GOT_ERR_CANCELLED &&
223 error->code != GOT_ERR_PRIVSEP_EXIT &&
224 !(sigpipe_received &&
225 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
227 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
228 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
235 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
250 static const struct got_error *
251 get_editor(char **abspath)
253 const struct got_error *err = NULL;
258 editor = getenv("VISUAL");
260 editor = getenv("EDITOR");
263 err = got_path_find_prog(abspath, editor);
268 if (*abspath == NULL) {
269 *abspath = strdup("/bin/ed");
270 if (*abspath == NULL)
271 return got_error_from_errno("strdup");
277 static const struct got_error *
278 apply_unveil(const char *repo_path, int repo_read_only,
279 const char *worktree_path)
281 const struct got_error *err;
284 if (unveil("gmon.out", "rwc") != 0)
285 return got_error_from_errno2("unveil", "gmon.out");
287 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
288 return got_error_from_errno2("unveil", repo_path);
290 if (worktree_path && unveil(worktree_path, "rwc") != 0)
291 return got_error_from_errno2("unveil", worktree_path);
293 if (unveil("/tmp", "rwc") != 0)
294 return got_error_from_errno2("unveil", "/tmp");
296 err = got_privsep_unveil_exec_helpers();
300 if (unveil(NULL, NULL) != 0)
301 return got_error_from_errno("unveil");
309 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
313 static const struct got_error *
314 cmd_init(int argc, char *argv[])
316 const struct got_error *error = NULL;
317 char *repo_path = NULL;
320 while ((ch = getopt(argc, argv, "")) != -1) {
332 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
338 repo_path = strdup(argv[0]);
339 if (repo_path == NULL)
340 return got_error_from_errno("strdup");
342 got_path_strip_trailing_slashes(repo_path);
344 error = got_path_mkdir(repo_path);
346 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
349 error = apply_unveil(repo_path, 0, NULL);
353 error = got_repo_init(repo_path);
365 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
366 "[-r repository-path] [-I pattern] path\n", getprogname());
371 spawn_editor(const char *editor, const char *file)
374 sig_t sighup, sigint, sigquit;
377 sighup = signal(SIGHUP, SIG_IGN);
378 sigint = signal(SIGINT, SIG_IGN);
379 sigquit = signal(SIGQUIT, SIG_IGN);
381 switch (pid = fork()) {
385 execl(editor, editor, file, (char *)NULL);
389 while (waitpid(pid, &st, 0) == -1)
394 (void)signal(SIGHUP, sighup);
395 (void)signal(SIGINT, sigint);
396 (void)signal(SIGQUIT, sigquit);
398 if (!WIFEXITED(st)) {
403 return WEXITSTATUS(st);
406 static const struct got_error *
407 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
408 const char *initial_content)
410 const struct got_error *err = NULL;
414 int content_changed = 0;
419 if (stat(logmsg_path, &st) == -1)
420 return got_error_from_errno2("stat", logmsg_path);
422 if (spawn_editor(editor, logmsg_path) == -1)
423 return got_error_from_errno("failed spawning editor");
425 if (stat(logmsg_path, &st2) == -1)
426 return got_error_from_errno("stat");
428 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
429 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
430 "no changes made to commit message, aborting");
432 *logmsg = malloc(st2.st_size + 1);
434 return got_error_from_errno("malloc");
438 fp = fopen(logmsg_path, "r");
440 err = got_error_from_errno("fopen");
443 while (fgets(buf, sizeof(buf), fp) != NULL) {
444 if (!content_changed && strcmp(buf, initial_content) != 0)
446 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
447 continue; /* remove comments and leading empty lines */
448 len = strlcat(*logmsg, buf, st2.st_size);
452 while (len > 0 && (*logmsg)[len - 1] == '\n') {
453 (*logmsg)[len - 1] = '\0';
457 if (len == 0 || !content_changed)
458 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 "commit message cannot be empty, aborting");
468 static const struct got_error *
469 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 const char *path_dir, const char *branch_name)
472 char *initial_content = NULL;
473 const struct got_error *err = NULL;
476 if (asprintf(&initial_content,
477 "\n# %s to be imported to branch %s\n", path_dir,
479 return got_error_from_errno("asprintf");
481 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
485 dprintf(fd, initial_content);
488 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
490 free(initial_content);
494 static const struct got_error *
495 import_progress(void *arg, const char *path)
497 printf("A %s\n", path);
501 static const struct got_error *
502 get_author(char **author, struct got_repository *repo)
504 const struct got_error *err = NULL;
505 const char *got_author, *name, *email;
509 name = got_repo_get_gitconfig_author_name(repo);
510 email = got_repo_get_gitconfig_author_email(repo);
512 if (asprintf(author, "%s <%s>", name, email) == -1)
513 return got_error_from_errno("asprintf");
517 got_author = getenv("GOT_AUTHOR");
518 if (got_author == NULL) {
519 name = got_repo_get_global_gitconfig_author_name(repo);
520 email = got_repo_get_global_gitconfig_author_email(repo);
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
526 /* TODO: Look up user in password database? */
527 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
530 *author = strdup(got_author);
532 return got_error_from_errno("strdup");
535 * Really dumb email address check; we're only doing this to
536 * avoid git's object parser breaking on commits we create.
538 while (*got_author && *got_author != '<')
540 if (*got_author != '<') {
541 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 while (*got_author && *got_author != '@')
546 if (*got_author != '@') {
547 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 while (*got_author && *got_author != '>')
552 if (*got_author != '>')
553 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 static const struct got_error *
563 get_gitconfig_path(char **gitconfig_path)
565 const char *homedir = getenv("HOME");
567 *gitconfig_path = NULL;
569 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
570 return got_error_from_errno("asprintf");
576 static const struct got_error *
577 cmd_import(int argc, char *argv[])
579 const struct got_error *error = NULL;
580 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
581 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
582 const char *branch_name = "main";
583 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
584 struct got_repository *repo = NULL;
585 struct got_reference *branch_ref = NULL, *head_ref = NULL;
586 struct got_object_id *new_commit_id = NULL;
588 struct got_pathlist_head ignores;
589 struct got_pathlist_entry *pe;
590 int preserve_logmsg = 0;
592 TAILQ_INIT(&ignores);
594 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
597 branch_name = optarg;
600 logmsg = strdup(optarg);
601 if (logmsg == NULL) {
602 error = got_error_from_errno("strdup");
607 repo_path = realpath(optarg, NULL);
608 if (repo_path == NULL) {
609 error = got_error_from_errno2("realpath",
615 if (optarg[0] == '\0')
617 error = got_pathlist_insert(&pe, &ignores, optarg,
632 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
640 if (repo_path == NULL) {
641 repo_path = getcwd(NULL, 0);
642 if (repo_path == NULL)
643 return got_error_from_errno("getcwd");
645 got_path_strip_trailing_slashes(repo_path);
646 error = get_gitconfig_path(&gitconfig_path);
649 error = got_repo_open(&repo, repo_path, gitconfig_path);
653 error = get_author(&author, repo);
657 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
658 error = got_error_from_errno("asprintf");
662 error = got_ref_open(&branch_ref, repo, refname, 0);
664 if (error->code != GOT_ERR_NOT_REF)
667 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
668 "import target branch already exists");
672 path_dir = realpath(argv[0], NULL);
673 if (path_dir == NULL) {
674 error = got_error_from_errno2("realpath", argv[0]);
677 got_path_strip_trailing_slashes(path_dir);
680 * unveil(2) traverses exec(2); if an editor is used we have
681 * to apply unveil after the log message has been written.
683 if (logmsg == NULL || strlen(logmsg) == 0) {
684 error = get_editor(&editor);
688 error = collect_import_msg(&logmsg, &logmsg_path, editor,
691 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
698 if (unveil(path_dir, "r") != 0) {
699 error = got_error_from_errno2("unveil", path_dir);
705 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
712 error = got_repo_import(&new_commit_id, path_dir, logmsg,
713 author, &ignores, repo, import_progress, NULL);
720 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
727 error = got_ref_write(branch_ref, repo);
734 error = got_object_id_str(&id_str, new_commit_id);
741 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
743 if (error->code != GOT_ERR_NOT_REF) {
749 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
757 error = got_ref_write(head_ref, repo);
765 printf("Created branch %s with commit %s\n",
766 got_ref_get_name(branch_ref), id_str);
768 if (preserve_logmsg) {
769 fprintf(stderr, "%s: log message preserved in %s\n",
770 getprogname(), logmsg_path);
771 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
772 error = got_error_from_errno2("unlink", logmsg_path);
781 free(gitconfig_path);
783 got_ref_close(branch_ref);
785 got_ref_close(head_ref);
792 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
793 "[-p prefix] repository-path [worktree-path]\n", getprogname());
797 static const struct got_error *
798 checkout_progress(void *arg, unsigned char status, const char *path)
800 char *worktree_path = arg;
802 /* Base commit bump happens silently. */
803 if (status == GOT_STATUS_BUMP_BASE)
806 while (path[0] == '/')
809 printf("%c %s/%s\n", status, worktree_path, path);
813 static const struct got_error *
814 check_cancelled(void *arg)
816 if (sigint_received || sigpipe_received)
817 return got_error(GOT_ERR_CANCELLED);
821 static const struct got_error *
822 check_linear_ancestry(struct got_object_id *commit_id,
823 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_object_id *yca_id;
829 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
830 commit_id, base_commit_id, repo, check_cancelled, NULL);
835 return got_error(GOT_ERR_ANCESTRY);
838 * Require a straight line of history between the target commit
839 * and the work tree's base commit.
841 * Non-linear situations such as this require a rebase:
843 * (commit) D F (base_commit)
851 * 'got update' only handles linear cases:
852 * Update forwards in time: A (base/yca) - B - C - D (commit)
853 * Update backwards in time: D (base) - C - B - A (commit/yca)
855 if (allow_forwards_in_time_only) {
856 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
857 return got_error(GOT_ERR_ANCESTRY);
858 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
859 got_object_id_cmp(base_commit_id, yca_id) != 0)
860 return got_error(GOT_ERR_ANCESTRY);
866 static const struct got_error *
867 check_same_branch(struct got_object_id *commit_id,
868 struct got_reference *head_ref, struct got_object_id *yca_id,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_commit_graph *graph = NULL;
873 struct got_object_id *head_commit_id = NULL;
874 int is_same_branch = 0;
876 err = got_ref_resolve(&head_commit_id, repo, head_ref);
880 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
884 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
889 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
893 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
894 check_cancelled, NULL);
899 struct got_object_id *id;
900 err = got_commit_graph_iter_next(&id, graph);
902 if (err->code == GOT_ERR_ITER_COMPLETED) {
905 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
907 err = got_commit_graph_fetch_commits(graph, 1,
908 repo, check_cancelled, NULL);
914 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
916 if (got_object_id_cmp(id, commit_id) == 0) {
924 got_commit_graph_close(graph);
925 free(head_commit_id);
926 if (!err && !is_same_branch)
927 err = got_error(GOT_ERR_ANCESTRY);
931 static const struct got_error *
932 resolve_commit_arg(struct got_object_id **commit_id,
933 const char *commit_id_arg, struct got_repository *repo)
935 const struct got_error *err;
936 struct got_reference *ref;
937 struct got_tag_object *tag;
939 err = got_repo_object_match_tag(&tag, commit_id_arg,
940 GOT_OBJ_TYPE_COMMIT, repo);
942 *commit_id = got_object_id_dup(
943 got_object_tag_get_object_id(tag));
944 if (*commit_id == NULL)
945 err = got_error_from_errno("got_object_id_dup");
946 got_object_tag_close(tag);
948 } else if (err->code != GOT_ERR_NO_OBJ)
951 err = got_ref_open(&ref, repo, commit_id_arg, 0);
953 err = got_ref_resolve(commit_id, repo, ref);
956 if (err->code != GOT_ERR_NOT_REF)
958 err = got_repo_match_object_id_prefix(commit_id,
959 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
964 static const struct got_error *
965 cmd_checkout(int argc, char *argv[])
967 const struct got_error *error = NULL;
968 struct got_repository *repo = NULL;
969 struct got_reference *head_ref = NULL;
970 struct got_worktree *worktree = NULL;
971 char *repo_path = NULL;
972 char *worktree_path = NULL;
973 const char *path_prefix = "";
974 const char *branch_name = GOT_REF_HEAD;
975 char *commit_id_str = NULL;
976 int ch, same_path_prefix;
977 struct got_pathlist_head paths;
981 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
984 branch_name = optarg;
987 commit_id_str = strdup(optarg);
988 if (commit_id_str == NULL)
989 return got_error_from_errno("strdup");
992 path_prefix = optarg;
1004 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1005 "unveil", NULL) == -1)
1009 char *cwd, *base, *dotgit;
1010 repo_path = realpath(argv[0], NULL);
1011 if (repo_path == NULL)
1012 return got_error_from_errno2("realpath", argv[0]);
1013 cwd = getcwd(NULL, 0);
1015 error = got_error_from_errno("getcwd");
1018 if (path_prefix[0]) {
1019 base = basename(path_prefix);
1021 error = got_error_from_errno2("basename",
1026 base = basename(repo_path);
1028 error = got_error_from_errno2("basename",
1033 dotgit = strstr(base, ".git");
1036 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1037 error = got_error_from_errno("asprintf");
1042 } else if (argc == 2) {
1043 repo_path = realpath(argv[0], NULL);
1044 if (repo_path == NULL) {
1045 error = got_error_from_errno2("realpath", argv[0]);
1048 worktree_path = realpath(argv[1], NULL);
1049 if (worktree_path == NULL) {
1050 if (errno != ENOENT) {
1051 error = got_error_from_errno2("realpath",
1055 worktree_path = strdup(argv[1]);
1056 if (worktree_path == NULL) {
1057 error = got_error_from_errno("strdup");
1064 got_path_strip_trailing_slashes(repo_path);
1065 got_path_strip_trailing_slashes(worktree_path);
1067 error = got_repo_open(&repo, repo_path, NULL);
1071 /* Pre-create work tree path for unveil(2) */
1072 error = got_path_mkdir(worktree_path);
1074 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1075 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1077 if (!got_path_dir_is_empty(worktree_path)) {
1078 error = got_error_path(worktree_path,
1079 GOT_ERR_DIR_NOT_EMPTY);
1084 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1088 error = got_ref_open(&head_ref, repo, branch_name, 0);
1092 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1096 error = got_worktree_open(&worktree, worktree_path);
1100 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1104 if (!same_path_prefix) {
1105 error = got_error(GOT_ERR_PATH_PREFIX);
1109 if (commit_id_str) {
1110 struct got_object_id *commit_id;
1111 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1114 error = check_linear_ancestry(commit_id,
1115 got_worktree_get_base_commit_id(worktree), 0, repo);
1116 if (error != NULL) {
1120 error = check_same_branch(commit_id, head_ref, NULL, repo);
1123 error = got_worktree_set_base_commit_id(worktree, repo,
1130 error = got_pathlist_append(&paths, "", NULL);
1133 error = got_worktree_checkout_files(worktree, &paths, repo,
1134 checkout_progress, worktree_path, check_cancelled, NULL);
1138 printf("Now shut up and hack\n");
1141 got_pathlist_free(&paths);
1142 free(commit_id_str);
1144 free(worktree_path);
1151 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1156 static const struct got_error *
1157 update_progress(void *arg, unsigned char status, const char *path)
1159 int *did_something = arg;
1161 if (status == GOT_STATUS_EXISTS)
1166 /* Base commit bump happens silently. */
1167 if (status == GOT_STATUS_BUMP_BASE)
1170 while (path[0] == '/')
1172 printf("%c %s\n", status, path);
1176 static const struct got_error *
1177 switch_head_ref(struct got_reference *head_ref,
1178 struct got_object_id *commit_id, struct got_worktree *worktree,
1179 struct got_repository *repo)
1181 const struct got_error *err = NULL;
1183 int ref_has_moved = 0;
1185 /* Trivial case: switching between two different references. */
1186 if (strcmp(got_ref_get_name(head_ref),
1187 got_worktree_get_head_ref_name(worktree)) != 0) {
1188 printf("Switching work tree from %s to %s\n",
1189 got_worktree_get_head_ref_name(worktree),
1190 got_ref_get_name(head_ref));
1191 return got_worktree_set_head_ref(worktree, head_ref);
1194 err = check_linear_ancestry(commit_id,
1195 got_worktree_get_base_commit_id(worktree), 0, repo);
1197 if (err->code != GOT_ERR_ANCESTRY)
1204 /* Switching to a rebased branch with the same reference name. */
1205 err = got_object_id_str(&base_id_str,
1206 got_worktree_get_base_commit_id(worktree));
1209 printf("Reference %s now points at a different branch\n",
1210 got_worktree_get_head_ref_name(worktree));
1211 printf("Switching work tree from %s to %s\n", base_id_str,
1212 got_worktree_get_head_ref_name(worktree));
1216 static const struct got_error *
1217 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1219 const struct got_error *err;
1222 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1226 return got_error(GOT_ERR_REBASING);
1228 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1232 return got_error(GOT_ERR_HISTEDIT_BUSY);
1237 static const struct got_error *
1238 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1239 char *argv[], struct got_worktree *worktree)
1241 const struct got_error *err = NULL;
1248 return got_error_from_errno("strdup");
1249 return got_pathlist_append(paths, path, NULL);
1252 for (i = 0; i < argc; i++) {
1253 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1256 err = got_pathlist_append(paths, path, NULL);
1266 static const struct got_error *
1267 cmd_update(int argc, char *argv[])
1269 const struct got_error *error = NULL;
1270 struct got_repository *repo = NULL;
1271 struct got_worktree *worktree = NULL;
1272 char *worktree_path = NULL;
1273 struct got_object_id *commit_id = NULL;
1274 char *commit_id_str = NULL;
1275 const char *branch_name = NULL;
1276 struct got_reference *head_ref = NULL;
1277 struct got_pathlist_head paths;
1278 struct got_pathlist_entry *pe;
1279 int ch, did_something = 0;
1283 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1286 branch_name = optarg;
1289 commit_id_str = strdup(optarg);
1290 if (commit_id_str == NULL)
1291 return got_error_from_errno("strdup");
1303 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1304 "unveil", NULL) == -1)
1307 worktree_path = getcwd(NULL, 0);
1308 if (worktree_path == NULL) {
1309 error = got_error_from_errno("getcwd");
1312 error = got_worktree_open(&worktree, worktree_path);
1316 error = check_rebase_or_histedit_in_progress(worktree);
1320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1325 error = apply_unveil(got_repo_get_path(repo), 0,
1326 got_worktree_get_root_path(worktree));
1330 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1334 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1335 got_worktree_get_head_ref_name(worktree), 0);
1338 if (commit_id_str == NULL) {
1339 error = got_ref_resolve(&commit_id, repo, head_ref);
1342 error = got_object_id_str(&commit_id_str, commit_id);
1346 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1347 free(commit_id_str);
1348 commit_id_str = NULL;
1351 error = got_object_id_str(&commit_id_str, commit_id);
1357 struct got_object_id *head_commit_id;
1358 TAILQ_FOREACH(pe, &paths, entry) {
1359 if (pe->path_len == 0)
1361 error = got_error_msg(GOT_ERR_BAD_PATH,
1362 "switching between branches requires that "
1363 "the entire work tree gets updated");
1366 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1369 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1371 free(head_commit_id);
1374 error = check_same_branch(commit_id, head_ref, NULL, repo);
1377 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1381 error = check_linear_ancestry(commit_id,
1382 got_worktree_get_base_commit_id(worktree), 0, repo);
1383 if (error != NULL) {
1384 if (error->code == GOT_ERR_ANCESTRY)
1385 error = got_error(GOT_ERR_BRANCH_MOVED);
1388 error = check_same_branch(commit_id, head_ref, NULL, repo);
1393 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1395 error = got_worktree_set_base_commit_id(worktree, repo,
1401 error = got_worktree_checkout_files(worktree, &paths, repo,
1402 update_progress, &did_something, check_cancelled, NULL);
1407 printf("Updated to commit %s\n", commit_id_str);
1409 printf("Already up-to-date\n");
1411 free(worktree_path);
1412 TAILQ_FOREACH(pe, &paths, entry)
1413 free((char *)pe->path);
1414 got_pathlist_free(&paths);
1416 free(commit_id_str);
1420 static const struct got_error *
1421 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1422 const char *path, int diff_context, int ignore_whitespace,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1426 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1429 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1434 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1438 while (path[0] == '/')
1440 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1441 ignore_whitespace, stdout);
1444 got_object_blob_close(blob1);
1445 got_object_blob_close(blob2);
1449 static const struct got_error *
1450 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1451 const char *path, int diff_context, int ignore_whitespace,
1452 struct got_repository *repo)
1454 const struct got_error *err = NULL;
1455 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1456 struct got_diff_blob_output_unidiff_arg arg;
1459 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1464 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1468 arg.diff_context = diff_context;
1469 arg.ignore_whitespace = ignore_whitespace;
1470 arg.outfile = stdout;
1471 while (path[0] == '/')
1473 err = got_diff_tree(tree1, tree2, path, path, repo,
1474 got_diff_blob_output_unidiff, &arg, 1);
1477 got_object_tree_close(tree1);
1479 got_object_tree_close(tree2);
1483 static const struct got_error *
1484 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1485 const char *path, int diff_context, struct got_repository *repo)
1487 const struct got_error *err = NULL;
1488 struct got_commit_object *pcommit = NULL;
1489 char *id_str1 = NULL, *id_str2 = NULL;
1490 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1491 struct got_object_qid *qid;
1493 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1495 err = got_object_open_as_commit(&pcommit, repo,
1501 if (path && path[0] != '\0') {
1503 err = got_object_id_by_path(&obj_id2, repo, id, path);
1506 err = got_object_id_str(&id_str2, obj_id2);
1512 err = got_object_id_by_path(&obj_id1, repo,
1518 err = got_object_id_str(&id_str1, obj_id1);
1524 err = got_object_get_type(&obj_type, repo, obj_id2);
1529 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1531 case GOT_OBJ_TYPE_BLOB:
1532 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1535 case GOT_OBJ_TYPE_TREE:
1536 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1540 err = got_error(GOT_ERR_OBJ_TYPE);
1546 obj_id2 = got_object_commit_get_tree_id(commit);
1547 err = got_object_id_str(&id_str2, obj_id2);
1550 obj_id1 = got_object_commit_get_tree_id(pcommit);
1551 err = got_object_id_str(&id_str1, obj_id1);
1554 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1555 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1562 got_object_commit_close(pcommit);
1567 get_datestr(time_t *time, char *datebuf)
1569 struct tm mytm, *tm;
1572 tm = gmtime_r(time, &mytm);
1575 s = asctime_r(tm, datebuf);
1578 p = strchr(s, '\n');
1584 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1586 static const struct got_error *
1587 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1588 struct got_repository *repo, const char *path, int show_patch,
1589 int diff_context, struct got_reflist_head *refs)
1591 const struct got_error *err = NULL;
1592 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1594 time_t committer_time;
1595 const char *author, *committer;
1596 char *refs_str = NULL;
1597 struct got_reflist_entry *re;
1599 SIMPLEQ_FOREACH(re, refs, entry) {
1602 struct got_tag_object *tag = NULL;
1605 name = got_ref_get_name(re->ref);
1606 if (strcmp(name, GOT_REF_HEAD) == 0)
1608 if (strncmp(name, "refs/", 5) == 0)
1610 if (strncmp(name, "got/", 4) == 0)
1612 if (strncmp(name, "heads/", 6) == 0)
1614 if (strncmp(name, "remotes/", 8) == 0)
1616 if (strncmp(name, "tags/", 5) == 0) {
1617 err = got_object_open_as_tag(&tag, repo, re->id);
1619 if (err->code != GOT_ERR_OBJ_TYPE)
1621 /* Ref points at something other than a tag. */
1626 cmp = got_object_id_cmp(tag ?
1627 got_object_tag_get_object_id(tag) : re->id, id);
1629 got_object_tag_close(tag);
1633 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1635 err = got_error_from_errno("asprintf");
1641 err = got_object_id_str(&id_str, id);
1645 printf(GOT_COMMIT_SEP_STR);
1646 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1647 refs_str ? refs_str : "", refs_str ? ")" : "");
1652 printf("from: %s\n", got_object_commit_get_author(commit));
1653 committer_time = got_object_commit_get_committer_time(commit);
1654 datestr = get_datestr(&committer_time, datebuf);
1656 printf("date: %s UTC\n", datestr);
1657 author = got_object_commit_get_author(commit);
1658 committer = got_object_commit_get_committer(commit);
1659 if (strcmp(author, committer) != 0)
1660 printf("via: %s\n", committer);
1661 if (got_object_commit_get_nparents(commit) > 1) {
1662 const struct got_object_id_queue *parent_ids;
1663 struct got_object_qid *qid;
1665 parent_ids = got_object_commit_get_parent_ids(commit);
1666 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1667 err = got_object_id_str(&id_str, qid->id);
1670 printf("parent %d: %s\n", n++, id_str);
1675 err = got_object_commit_get_logmsg(&logmsg0, commit);
1681 line = strsep(&logmsg, "\n");
1683 printf(" %s\n", line);
1688 err = print_patch(commit, id, path, diff_context, repo);
1693 if (fflush(stdout) != 0 && err == NULL)
1694 err = got_error_from_errno("fflush");
1698 static const struct got_error *
1699 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1700 char *path, int show_patch, int diff_context, int limit,
1701 int first_parent_traversal, struct got_reflist_head *refs)
1703 const struct got_error *err;
1704 struct got_commit_graph *graph;
1706 err = got_commit_graph_open(&graph, root_id, path,
1707 first_parent_traversal, repo);
1710 err = got_commit_graph_iter_start(graph, root_id, repo,
1711 check_cancelled, NULL);
1715 struct got_commit_object *commit;
1716 struct got_object_id *id;
1718 if (sigint_received || sigpipe_received)
1721 err = got_commit_graph_iter_next(&id, graph);
1723 if (err->code == GOT_ERR_ITER_COMPLETED) {
1727 if (err->code != GOT_ERR_ITER_NEED_MORE)
1729 err = got_commit_graph_fetch_commits(graph, 1, repo,
1730 check_cancelled, NULL);
1739 err = got_object_open_as_commit(&commit, repo, id);
1742 err = print_commit(commit, id, repo, path, show_patch,
1743 diff_context, refs);
1744 got_object_commit_close(commit);
1745 if (err || (limit && --limit == 0))
1749 got_commit_graph_close(graph);
1756 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1757 "[-r repository-path] [path]\n", getprogname());
1762 get_default_log_limit(void)
1764 const char *got_default_log_limit;
1768 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1769 if (got_default_log_limit == NULL)
1771 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1777 static const struct got_error *
1778 cmd_log(int argc, char *argv[])
1780 const struct got_error *error;
1781 struct got_repository *repo = NULL;
1782 struct got_worktree *worktree = NULL;
1783 struct got_commit_object *commit = NULL;
1784 struct got_object_id *id = NULL;
1785 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1786 char *start_commit = NULL;
1787 int diff_context = 3, ch;
1788 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1790 struct got_reflist_head refs;
1792 SIMPLEQ_INIT(&refs);
1795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1801 limit = get_default_log_limit();
1803 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1809 start_commit = optarg;
1812 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1815 err(1, "-C option %s", errstr);
1818 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1820 err(1, "-l option %s", errstr);
1823 first_parent_traversal = 1;
1826 repo_path = realpath(optarg, NULL);
1827 if (repo_path == NULL)
1828 return got_error_from_errno2("realpath",
1830 got_path_strip_trailing_slashes(repo_path);
1841 cwd = getcwd(NULL, 0);
1843 error = got_error_from_errno("getcwd");
1847 error = got_worktree_open(&worktree, cwd);
1848 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1855 error = got_error_from_errno("strdup");
1858 } else if (argc == 1) {
1860 error = got_worktree_resolve_path(&path, worktree,
1865 path = strdup(argv[0]);
1867 error = got_error_from_errno("strdup");
1874 if (repo_path == NULL) {
1875 repo_path = worktree ?
1876 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1878 if (repo_path == NULL) {
1879 error = got_error_from_errno("strdup");
1883 error = got_repo_open(&repo, repo_path, NULL);
1887 error = apply_unveil(got_repo_get_path(repo), 1,
1888 worktree ? got_worktree_get_root_path(worktree) : NULL);
1892 if (start_commit == NULL) {
1893 struct got_reference *head_ref;
1894 error = got_ref_open(&head_ref, repo,
1895 worktree ? got_worktree_get_head_ref_name(worktree)
1899 error = got_ref_resolve(&id, repo, head_ref);
1900 got_ref_close(head_ref);
1903 error = got_object_open_as_commit(&commit, repo, id);
1905 struct got_reference *ref;
1906 error = got_ref_open(&ref, repo, start_commit, 0);
1907 if (error == NULL) {
1909 error = got_ref_resolve(&id, repo, ref);
1913 error = got_object_get_type(&obj_type, repo, id);
1916 if (obj_type == GOT_OBJ_TYPE_TAG) {
1917 struct got_tag_object *tag;
1918 error = got_object_open_as_tag(&tag, repo, id);
1921 if (got_object_tag_get_object_type(tag) !=
1922 GOT_OBJ_TYPE_COMMIT) {
1923 got_object_tag_close(tag);
1924 error = got_error(GOT_ERR_OBJ_TYPE);
1928 id = got_object_id_dup(
1929 got_object_tag_get_object_id(tag));
1931 error = got_error_from_errno(
1932 "got_object_id_dup");
1933 got_object_tag_close(tag);
1936 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1937 error = got_error(GOT_ERR_OBJ_TYPE);
1940 error = got_object_open_as_commit(&commit, repo, id);
1944 if (commit == NULL) {
1945 error = got_repo_match_object_id_prefix(&id,
1946 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1955 const char *prefix = got_worktree_get_path_prefix(worktree);
1957 if (asprintf(&p, "%s%s%s", prefix,
1958 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1959 error = got_error_from_errno("asprintf");
1962 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1965 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1970 path = in_repo_path;
1973 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1977 error = print_commits(id, repo, path, show_patch,
1978 diff_context, limit, first_parent_traversal, &refs);
1985 got_worktree_close(worktree);
1987 const struct got_error *repo_error;
1988 repo_error = got_repo_close(repo);
1992 got_ref_list_free(&refs);
1999 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2000 "[-w] [object1 object2 | path]\n", getprogname());
2004 struct print_diff_arg {
2005 struct got_repository *repo;
2006 struct got_worktree *worktree;
2011 int ignore_whitespace;
2014 static const struct got_error *
2015 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2016 const char *path, struct got_object_id *blob_id,
2017 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2019 struct print_diff_arg *a = arg;
2020 const struct got_error *err = NULL;
2021 struct got_blob_object *blob1 = NULL;
2023 char *abspath = NULL, *label1 = NULL;
2026 if (a->diff_staged) {
2027 if (staged_status != GOT_STATUS_MODIFY &&
2028 staged_status != GOT_STATUS_ADD &&
2029 staged_status != GOT_STATUS_DELETE)
2032 if (staged_status == GOT_STATUS_DELETE)
2034 if (status == GOT_STATUS_NONEXISTENT)
2035 return got_error_set_errno(ENOENT, path);
2036 if (status != GOT_STATUS_MODIFY &&
2037 status != GOT_STATUS_ADD &&
2038 status != GOT_STATUS_DELETE &&
2039 status != GOT_STATUS_CONFLICT)
2043 if (!a->header_shown) {
2044 printf("diff %s %s%s\n", a->id_str,
2045 got_worktree_get_root_path(a->worktree),
2046 a->diff_staged ? " (staged changes)" : "");
2047 a->header_shown = 1;
2050 if (a->diff_staged) {
2051 const char *label1 = NULL, *label2 = NULL;
2052 switch (staged_status) {
2053 case GOT_STATUS_MODIFY:
2057 case GOT_STATUS_ADD:
2060 case GOT_STATUS_DELETE:
2064 return got_error(GOT_ERR_FILE_STATUS);
2066 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2067 label1, label2, a->diff_context, a->ignore_whitespace,
2071 if (staged_status == GOT_STATUS_ADD ||
2072 staged_status == GOT_STATUS_MODIFY) {
2074 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2078 err = got_object_id_str(&id_str, staged_blob_id);
2081 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2082 err = got_error_from_errno("asprintf");
2087 } else if (status != GOT_STATUS_ADD) {
2088 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2093 if (status != GOT_STATUS_DELETE) {
2094 if (asprintf(&abspath, "%s/%s",
2095 got_worktree_get_root_path(a->worktree), path) == -1) {
2096 err = got_error_from_errno("asprintf");
2100 f2 = fopen(abspath, "r");
2102 err = got_error_from_errno2("fopen", abspath);
2105 if (lstat(abspath, &sb) == -1) {
2106 err = got_error_from_errno2("lstat", abspath);
2112 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2113 a->diff_context, a->ignore_whitespace, stdout);
2116 got_object_blob_close(blob1);
2117 if (f2 && fclose(f2) != 0 && err == NULL)
2118 err = got_error_from_errno("fclose");
2123 static const struct got_error *
2124 match_object_id(struct got_object_id **id, char **label,
2125 const char *id_str, int obj_type, int resolve_tags,
2126 struct got_repository *repo)
2128 const struct got_error *err;
2129 struct got_tag_object *tag;
2130 struct got_reference *ref = NULL;
2136 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2139 *id = got_object_id_dup(
2140 got_object_tag_get_object_id(tag));
2142 err = got_error_from_errno("got_object_id_dup");
2143 else if (asprintf(label, "refs/tags/%s",
2144 got_object_tag_get_name(tag)) == -1) {
2145 err = got_error_from_errno("asprintf");
2149 got_object_tag_close(tag);
2151 } else if (err->code != GOT_ERR_NO_OBJ)
2155 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2157 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2159 err = got_ref_open(&ref, repo, id_str, 0);
2162 *label = strdup(got_ref_get_name(ref));
2163 if (*label == NULL) {
2164 err = got_error_from_errno("strdup");
2167 err = got_ref_resolve(id, repo, ref);
2169 err = got_object_id_str(label, *id);
2170 if (*label == NULL) {
2171 err = got_error_from_errno("strdup");
2182 static const struct got_error *
2183 cmd_diff(int argc, char *argv[])
2185 const struct got_error *error;
2186 struct got_repository *repo = NULL;
2187 struct got_worktree *worktree = NULL;
2188 char *cwd = NULL, *repo_path = NULL;
2189 struct got_object_id *id1 = NULL, *id2 = NULL;
2190 const char *id_str1 = NULL, *id_str2 = NULL;
2191 char *label1 = NULL, *label2 = NULL;
2193 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2198 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2203 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2206 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2208 err(1, "-C option %s", errstr);
2211 repo_path = realpath(optarg, NULL);
2212 if (repo_path == NULL)
2213 return got_error_from_errno2("realpath",
2215 got_path_strip_trailing_slashes(repo_path);
2221 ignore_whitespace = 1;
2232 cwd = getcwd(NULL, 0);
2234 error = got_error_from_errno("getcwd");
2237 error = got_worktree_open(&worktree, cwd);
2238 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2241 if (worktree == NULL) {
2242 error = got_error(GOT_ERR_NOT_WORKTREE);
2247 "-r option can't be used when diffing a work tree");
2248 repo_path = strdup(got_worktree_get_repo_path(worktree));
2249 if (repo_path == NULL) {
2250 error = got_error_from_errno("strdup");
2254 error = got_worktree_resolve_path(&path, worktree,
2261 error = got_error_from_errno("strdup");
2265 } else if (argc == 2) {
2267 errx(1, "-s option can't be used when diffing "
2268 "objects in repository");
2271 if (worktree && repo_path == NULL) {
2273 strdup(got_worktree_get_repo_path(worktree));
2274 if (repo_path == NULL) {
2275 error = got_error_from_errno("strdup");
2282 if (repo_path == NULL) {
2283 repo_path = getcwd(NULL, 0);
2284 if (repo_path == NULL)
2285 return got_error_from_errno("getcwd");
2288 error = got_repo_open(&repo, repo_path, NULL);
2293 error = apply_unveil(got_repo_get_path(repo), 1,
2294 worktree ? got_worktree_get_root_path(worktree) : NULL);
2299 struct print_diff_arg arg;
2300 struct got_pathlist_head paths;
2305 error = got_object_id_str(&id_str,
2306 got_worktree_get_base_commit_id(worktree));
2310 arg.worktree = worktree;
2311 arg.diff_context = diff_context;
2312 arg.id_str = id_str;
2313 arg.header_shown = 0;
2314 arg.diff_staged = diff_staged;
2315 arg.ignore_whitespace = ignore_whitespace;
2317 error = got_pathlist_append(&paths, path, NULL);
2321 error = got_worktree_status(worktree, &paths, repo, print_diff,
2322 &arg, check_cancelled, NULL);
2324 got_pathlist_free(&paths);
2328 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2333 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2338 error = got_object_get_type(&type1, repo, id1);
2342 error = got_object_get_type(&type2, repo, id2);
2346 if (type1 != type2) {
2347 error = got_error(GOT_ERR_OBJ_TYPE);
2352 case GOT_OBJ_TYPE_BLOB:
2353 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2354 diff_context, ignore_whitespace, repo, stdout);
2356 case GOT_OBJ_TYPE_TREE:
2357 error = got_diff_objects_as_trees(id1, id2, "", "",
2358 diff_context, ignore_whitespace, repo, stdout);
2360 case GOT_OBJ_TYPE_COMMIT:
2361 printf("diff %s %s\n", label1, label2);
2362 error = got_diff_objects_as_commits(id1, id2, diff_context,
2363 ignore_whitespace, repo, stdout);
2366 error = got_error(GOT_ERR_OBJ_TYPE);
2376 got_worktree_close(worktree);
2378 const struct got_error *repo_error;
2379 repo_error = got_repo_close(repo);
2390 "usage: %s blame [-c commit] [-r repository-path] path\n",
2399 char datebuf[11]; /* YYYY-MM-DD + NUL */
2402 struct blame_cb_args {
2403 struct blame_line *lines;
2407 off_t *line_offsets;
2409 struct got_repository *repo;
2412 static const struct got_error *
2413 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2415 const struct got_error *err = NULL;
2416 struct blame_cb_args *a = arg;
2417 struct blame_line *bline;
2419 size_t linesize = 0;
2420 struct got_commit_object *commit = NULL;
2423 time_t committer_time;
2425 if (nlines != a->nlines ||
2426 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2427 return got_error(GOT_ERR_RANGE);
2429 if (sigint_received)
2430 return got_error(GOT_ERR_ITER_COMPLETED);
2433 return NULL; /* no change in this commit */
2435 /* Annotate this line. */
2436 bline = &a->lines[lineno - 1];
2437 if (bline->annotated)
2439 err = got_object_id_str(&bline->id_str, id);
2443 err = got_object_open_as_commit(&commit, a->repo, id);
2447 bline->committer = strdup(got_object_commit_get_committer(commit));
2448 if (bline->committer == NULL) {
2449 err = got_error_from_errno("strdup");
2453 committer_time = got_object_commit_get_committer_time(commit);
2454 if (localtime_r(&committer_time, &tm) == NULL)
2455 return got_error_from_errno("localtime_r");
2456 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2457 &tm) >= sizeof(bline->datebuf)) {
2458 err = got_error(GOT_ERR_NO_SPACE);
2461 bline->annotated = 1;
2463 /* Print lines annotated so far. */
2464 bline = &a->lines[a->lineno_cur - 1];
2465 if (!bline->annotated)
2468 offset = a->line_offsets[a->lineno_cur - 1];
2469 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2470 err = got_error_from_errno("fseeko");
2474 while (bline->annotated) {
2475 char *smallerthan, *at, *nl, *committer;
2478 if (getline(&line, &linesize, a->f) == -1) {
2480 err = got_error_from_errno("getline");
2484 committer = bline->committer;
2485 smallerthan = strchr(committer, '<');
2486 if (smallerthan && smallerthan[1] != '\0')
2487 committer = smallerthan + 1;
2488 at = strchr(committer, '@');
2491 len = strlen(committer);
2493 committer[8] = '\0';
2495 nl = strchr(line, '\n');
2498 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2499 bline->id_str, bline->datebuf, committer, line);
2502 bline = &a->lines[a->lineno_cur - 1];
2506 got_object_commit_close(commit);
2511 static const struct got_error *
2512 cmd_blame(int argc, char *argv[])
2514 const struct got_error *error;
2515 struct got_repository *repo = NULL;
2516 struct got_worktree *worktree = NULL;
2517 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2518 struct got_object_id *obj_id = NULL;
2519 struct got_object_id *commit_id = NULL;
2520 struct got_blob_object *blob = NULL;
2521 char *commit_id_str = NULL;
2522 struct blame_cb_args bca;
2523 int ch, obj_type, i;
2526 memset(&bca, 0, sizeof(bca));
2529 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2534 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2537 commit_id_str = optarg;
2540 repo_path = realpath(optarg, NULL);
2541 if (repo_path == NULL)
2542 return got_error_from_errno2("realpath",
2544 got_path_strip_trailing_slashes(repo_path);
2560 cwd = getcwd(NULL, 0);
2562 error = got_error_from_errno("getcwd");
2565 if (repo_path == NULL) {
2566 error = got_worktree_open(&worktree, cwd);
2567 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2573 strdup(got_worktree_get_repo_path(worktree));
2574 if (repo_path == NULL)
2575 error = got_error_from_errno("strdup");
2579 repo_path = strdup(cwd);
2580 if (repo_path == NULL) {
2581 error = got_error_from_errno("strdup");
2587 error = got_repo_open(&repo, repo_path, NULL);
2591 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2596 const char *prefix = got_worktree_get_path_prefix(worktree);
2597 char *p, *worktree_subdir = cwd +
2598 strlen(got_worktree_get_root_path(worktree));
2599 if (asprintf(&p, "%s%s%s%s%s",
2600 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2601 worktree_subdir, worktree_subdir[0] ? "/" : "",
2603 error = got_error_from_errno("asprintf");
2606 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2609 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2614 if (commit_id_str == NULL) {
2615 struct got_reference *head_ref;
2616 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2619 error = got_ref_resolve(&commit_id, repo, head_ref);
2620 got_ref_close(head_ref);
2624 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2629 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2632 if (obj_id == NULL) {
2633 error = got_error(GOT_ERR_NO_OBJ);
2637 error = got_object_get_type(&obj_type, repo, obj_id);
2641 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2642 error = got_error(GOT_ERR_OBJ_TYPE);
2646 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2649 bca.f = got_opentemp();
2650 if (bca.f == NULL) {
2651 error = got_error_from_errno("got_opentemp");
2654 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2655 &bca.line_offsets, bca.f, blob);
2656 if (error || bca.nlines == 0)
2659 /* Don't include \n at EOF in the blame line count. */
2660 if (bca.line_offsets[bca.nlines - 1] == filesize)
2663 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2664 if (bca.lines == NULL) {
2665 error = got_error_from_errno("calloc");
2669 bca.nlines_prec = 0;
2677 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2678 check_cancelled, NULL);
2688 got_object_blob_close(blob);
2690 got_worktree_close(worktree);
2692 const struct got_error *repo_error;
2693 repo_error = got_repo_close(repo);
2698 for (i = 0; i < bca.nlines; i++) {
2699 struct blame_line *bline = &bca.lines[i];
2700 free(bline->id_str);
2701 free(bline->committer);
2705 free(bca.line_offsets);
2706 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2707 error = got_error_from_errno("fclose");
2715 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2721 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2722 const char *root_path)
2724 int is_root_path = (strcmp(path, root_path) == 0);
2725 const char *modestr = "";
2727 path += strlen(root_path);
2728 while (path[0] == '/')
2731 if (got_object_tree_entry_is_submodule(te))
2733 else if (S_ISLNK(te->mode))
2735 else if (S_ISDIR(te->mode))
2737 else if (te->mode & S_IXUSR)
2740 printf("%s%s%s%s%s\n", id ? id : "", path,
2741 is_root_path ? "" : "/", te->name, modestr);
2744 static const struct got_error *
2745 print_tree(const char *path, struct got_object_id *commit_id,
2746 int show_ids, int recurse, const char *root_path,
2747 struct got_repository *repo)
2749 const struct got_error *err = NULL;
2750 struct got_object_id *tree_id = NULL;
2751 struct got_tree_object *tree = NULL;
2752 const struct got_tree_entries *entries;
2753 struct got_tree_entry *te;
2755 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2759 err = got_object_open_as_tree(&tree, repo, tree_id);
2762 entries = got_object_tree_get_entries(tree);
2763 te = SIMPLEQ_FIRST(&entries->head);
2767 if (sigint_received || sigpipe_received)
2772 err = got_object_id_str(&id_str, te->id);
2775 if (asprintf(&id, "%s ", id_str) == -1) {
2776 err = got_error_from_errno("asprintf");
2782 print_entry(te, id, path, root_path);
2785 if (recurse && S_ISDIR(te->mode)) {
2787 if (asprintf(&child_path, "%s%s%s", path,
2788 path[0] == '/' && path[1] == '\0' ? "" : "/",
2790 err = got_error_from_errno("asprintf");
2793 err = print_tree(child_path, commit_id, show_ids, 1,
2800 te = SIMPLEQ_NEXT(te, entry);
2804 got_object_tree_close(tree);
2809 static const struct got_error *
2810 cmd_tree(int argc, char *argv[])
2812 const struct got_error *error;
2813 struct got_repository *repo = NULL;
2814 struct got_worktree *worktree = NULL;
2816 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2817 struct got_object_id *commit_id = NULL;
2818 char *commit_id_str = NULL;
2819 int show_ids = 0, recurse = 0;
2823 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2828 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2831 commit_id_str = optarg;
2834 repo_path = realpath(optarg, NULL);
2835 if (repo_path == NULL)
2836 return got_error_from_errno2("realpath",
2838 got_path_strip_trailing_slashes(repo_path);
2862 cwd = getcwd(NULL, 0);
2864 error = got_error_from_errno("getcwd");
2867 if (repo_path == NULL) {
2868 error = got_worktree_open(&worktree, cwd);
2869 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2875 strdup(got_worktree_get_repo_path(worktree));
2876 if (repo_path == NULL)
2877 error = got_error_from_errno("strdup");
2881 repo_path = strdup(cwd);
2882 if (repo_path == NULL) {
2883 error = got_error_from_errno("strdup");
2889 error = got_repo_open(&repo, repo_path, NULL);
2893 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2899 char *p, *worktree_subdir = cwd +
2900 strlen(got_worktree_get_root_path(worktree));
2901 if (asprintf(&p, "%s/%s",
2902 got_worktree_get_path_prefix(worktree),
2903 worktree_subdir) == -1) {
2904 error = got_error_from_errno("asprintf");
2907 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2914 if (in_repo_path == NULL) {
2915 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2920 if (commit_id_str == NULL) {
2921 struct got_reference *head_ref;
2922 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2925 error = got_ref_resolve(&commit_id, repo, head_ref);
2926 got_ref_close(head_ref);
2930 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2935 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2936 in_repo_path, repo);
2943 got_worktree_close(worktree);
2945 const struct got_error *repo_error;
2946 repo_error = got_repo_close(repo);
2956 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2960 static const struct got_error *
2961 print_status(void *arg, unsigned char status, unsigned char staged_status,
2962 const char *path, struct got_object_id *blob_id,
2963 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2965 if (status == staged_status && (status == GOT_STATUS_DELETE))
2966 status = GOT_STATUS_NO_CHANGE;
2967 printf("%c%c %s\n", status, staged_status, path);
2971 static const struct got_error *
2972 cmd_status(int argc, char *argv[])
2974 const struct got_error *error = NULL;
2975 struct got_repository *repo = NULL;
2976 struct got_worktree *worktree = NULL;
2978 struct got_pathlist_head paths;
2979 struct got_pathlist_entry *pe;
2984 while ((ch = getopt(argc, argv, "")) != -1) {
2996 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3000 cwd = getcwd(NULL, 0);
3002 error = got_error_from_errno("getcwd");
3006 error = got_worktree_open(&worktree, cwd);
3010 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3015 error = apply_unveil(got_repo_get_path(repo), 1,
3016 got_worktree_get_root_path(worktree));
3020 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3024 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3025 check_cancelled, NULL);
3027 TAILQ_FOREACH(pe, &paths, entry)
3028 free((char *)pe->path);
3029 got_pathlist_free(&paths);
3038 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3043 static const struct got_error *
3044 list_refs(struct got_repository *repo)
3046 static const struct got_error *err = NULL;
3047 struct got_reflist_head refs;
3048 struct got_reflist_entry *re;
3050 SIMPLEQ_INIT(&refs);
3051 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3055 SIMPLEQ_FOREACH(re, &refs, entry) {
3057 refstr = got_ref_to_str(re->ref);
3059 return got_error_from_errno("got_ref_to_str");
3060 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3064 got_ref_list_free(&refs);
3068 static const struct got_error *
3069 delete_ref(struct got_repository *repo, const char *refname)
3071 const struct got_error *err = NULL;
3072 struct got_reference *ref;
3074 err = got_ref_open(&ref, repo, refname, 0);
3078 err = got_ref_delete(ref, repo);
3083 static const struct got_error *
3084 add_ref(struct got_repository *repo, const char *refname, const char *target)
3086 const struct got_error *err = NULL;
3087 struct got_object_id *id;
3088 struct got_reference *ref = NULL;
3091 * Don't let the user create a reference named '-'.
3092 * While technically a valid reference name, this case is usually
3093 * an unintended typo.
3095 if (refname[0] == '-' && refname[1] == '\0')
3096 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3098 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3101 struct got_reference *target_ref;
3103 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3105 err = got_ref_open(&target_ref, repo, target, 0);
3108 err = got_ref_resolve(&id, repo, target_ref);
3109 got_ref_close(target_ref);
3114 err = got_ref_alloc(&ref, refname, id);
3118 err = got_ref_write(ref, repo);
3126 static const struct got_error *
3127 add_symref(struct got_repository *repo, const char *refname, const char *target)
3129 const struct got_error *err = NULL;
3130 struct got_reference *ref = NULL;
3131 struct got_reference *target_ref = NULL;
3134 * Don't let the user create a reference named '-'.
3135 * While technically a valid reference name, this case is usually
3136 * an unintended typo.
3138 if (refname[0] == '-' && refname[1] == '\0')
3139 return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3141 err = got_ref_open(&target_ref, repo, target, 0);
3145 err = got_ref_alloc_symref(&ref, refname, target_ref);
3149 err = got_ref_write(ref, repo);
3152 got_ref_close(target_ref);
3158 static const struct got_error *
3159 cmd_ref(int argc, char *argv[])
3161 const struct got_error *error = NULL;
3162 struct got_repository *repo = NULL;
3163 struct got_worktree *worktree = NULL;
3164 char *cwd = NULL, *repo_path = NULL;
3165 int ch, do_list = 0, create_symref = 0;
3166 const char *delref = NULL;
3168 /* TODO: Add -s option for adding symbolic references. */
3169 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3175 repo_path = realpath(optarg, NULL);
3176 if (repo_path == NULL)
3177 return got_error_from_errno2("realpath",
3179 got_path_strip_trailing_slashes(repo_path);
3193 if (do_list && delref)
3194 errx(1, "-l and -d options are mutually exclusive\n");
3199 if (do_list || delref) {
3201 errx(1, "-s option cannot be used together with the "
3202 "-l or -d options");
3205 } else if (argc != 2)
3210 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3214 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3215 "sendfd unveil", NULL) == -1)
3219 cwd = getcwd(NULL, 0);
3221 error = got_error_from_errno("getcwd");
3225 if (repo_path == NULL) {
3226 error = got_worktree_open(&worktree, cwd);
3227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3233 strdup(got_worktree_get_repo_path(worktree));
3234 if (repo_path == NULL)
3235 error = got_error_from_errno("strdup");
3239 repo_path = strdup(cwd);
3240 if (repo_path == NULL) {
3241 error = got_error_from_errno("strdup");
3247 error = got_repo_open(&repo, repo_path, NULL);
3251 error = apply_unveil(got_repo_get_path(repo), do_list,
3252 worktree ? got_worktree_get_root_path(worktree) : NULL);
3257 error = list_refs(repo);
3259 error = delete_ref(repo, delref);
3260 else if (create_symref)
3261 error = add_symref(repo, argv[0], argv[1]);
3263 error = add_ref(repo, argv[0], argv[1]);
3266 got_repo_close(repo);
3268 got_worktree_close(worktree);
3278 "usage: %s branch [-r repository] [-l] | -d name | "
3279 "[name [commit]]\n", getprogname());
3283 static const struct got_error *
3284 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3285 struct got_reference *ref)
3287 const struct got_error *err = NULL;
3288 const char *refname, *marker = " ";
3291 refname = got_ref_get_name(ref);
3292 if (worktree && strcmp(refname,
3293 got_worktree_get_head_ref_name(worktree)) == 0) {
3294 struct got_object_id *id = NULL;
3296 err = got_ref_resolve(&id, repo, ref);
3299 if (got_object_id_cmp(id,
3300 got_worktree_get_base_commit_id(worktree)) == 0)
3307 if (strncmp(refname, "refs/heads/", 11) == 0)
3309 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3312 refstr = got_ref_to_str(ref);
3314 return got_error_from_errno("got_ref_to_str");
3316 printf("%s%s: %s\n", marker, refname, refstr);
3321 static const struct got_error *
3322 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3324 const char *refname;
3326 if (worktree == NULL)
3327 return got_error(GOT_ERR_NOT_WORKTREE);
3329 refname = got_worktree_get_head_ref_name(worktree);
3331 if (strncmp(refname, "refs/heads/", 11) == 0)
3333 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3336 printf("%s\n", refname);
3341 static const struct got_error *
3342 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3344 static const struct got_error *err = NULL;
3345 struct got_reflist_head refs;
3346 struct got_reflist_entry *re;
3347 struct got_reference *temp_ref = NULL;
3348 int rebase_in_progress, histedit_in_progress;
3350 SIMPLEQ_INIT(&refs);
3353 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3358 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3363 if (rebase_in_progress || histedit_in_progress) {
3364 err = got_ref_open(&temp_ref, repo,
3365 got_worktree_get_head_ref_name(worktree), 0);
3368 list_branch(repo, worktree, temp_ref);
3369 got_ref_close(temp_ref);
3373 err = got_ref_list(&refs, repo, "refs/heads",
3374 got_ref_cmp_by_name, NULL);
3378 SIMPLEQ_FOREACH(re, &refs, entry)
3379 list_branch(repo, worktree, re->ref);
3381 got_ref_list_free(&refs);
3385 static const struct got_error *
3386 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3387 const char *branch_name)
3389 const struct got_error *err = NULL;
3390 struct got_reference *ref = NULL;
3393 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3394 return got_error_from_errno("asprintf");
3396 err = got_ref_open(&ref, repo, refname, 0);
3401 strcmp(got_worktree_get_head_ref_name(worktree),
3402 got_ref_get_name(ref)) == 0) {
3403 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3404 "will not delete this work tree's current branch");
3408 err = got_ref_delete(ref, repo);
3416 static const struct got_error *
3417 add_branch(struct got_repository *repo, const char *branch_name,
3418 const char *base_branch)
3420 const struct got_error *err = NULL;
3421 struct got_object_id *id = NULL;
3423 struct got_reference *ref = NULL;
3424 char *base_refname = NULL, *refname = NULL;
3427 * Don't let the user create a branch named '-'.
3428 * While technically a valid reference name, this case is usually
3429 * an unintended typo.
3431 if (branch_name[0] == '-' && branch_name[1] == '\0')
3432 return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3434 err = match_object_id(&id, &label, base_branch,
3435 GOT_OBJ_TYPE_COMMIT, 1, repo);
3439 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3440 err = got_error_from_errno("asprintf");
3444 err = got_ref_open(&ref, repo, refname, 0);
3446 err = got_error(GOT_ERR_BRANCH_EXISTS);
3448 } else if (err->code != GOT_ERR_NOT_REF)
3451 err = got_ref_alloc(&ref, refname, id);
3455 err = got_ref_write(ref, repo);
3465 static const struct got_error *
3466 cmd_branch(int argc, char *argv[])
3468 const struct got_error *error = NULL;
3469 struct got_repository *repo = NULL;
3470 struct got_worktree *worktree = NULL;
3471 char *cwd = NULL, *repo_path = NULL;
3472 int ch, do_list = 0, do_show = 0;
3473 const char *delref = NULL;
3475 while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3481 repo_path = realpath(optarg, NULL);
3482 if (repo_path == NULL)
3483 return got_error_from_errno2("realpath",
3485 got_path_strip_trailing_slashes(repo_path);
3496 if (do_list && delref)
3497 errx(1, "-l and -d options are mutually exclusive\n");
3502 if (!do_list && !delref && argc == 0)
3505 if (do_list || delref) {
3508 } else if (!do_show && (argc < 1 || argc > 2))
3512 if (do_list || do_show) {
3513 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3517 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3518 "sendfd unveil", NULL) == -1)
3522 cwd = getcwd(NULL, 0);
3524 error = got_error_from_errno("getcwd");
3528 if (repo_path == NULL) {
3529 error = got_worktree_open(&worktree, cwd);
3530 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3536 strdup(got_worktree_get_repo_path(worktree));
3537 if (repo_path == NULL)
3538 error = got_error_from_errno("strdup");
3542 repo_path = strdup(cwd);
3543 if (repo_path == NULL) {
3544 error = got_error_from_errno("strdup");
3550 error = got_repo_open(&repo, repo_path, NULL);
3554 error = apply_unveil(got_repo_get_path(repo), do_list,
3555 worktree ? got_worktree_get_root_path(worktree) : NULL);
3560 error = show_current_branch(repo, worktree);
3562 error = list_branches(repo, worktree);
3564 error = delete_branch(repo, worktree, delref);
3566 const char *base_branch;
3568 base_branch = worktree ?
3569 got_worktree_get_head_ref_name(worktree) :
3571 if (strncmp(base_branch, "refs/heads/", 11) == 0)
3574 base_branch = argv[1];
3575 error = add_branch(repo, argv[0], base_branch);
3579 got_repo_close(repo);
3581 got_worktree_close(worktree);
3592 "usage: %s tag [-r repository] | -l | "
3593 "[-m message] name [commit]\n", getprogname());
3598 static const struct got_error *
3599 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3601 const struct got_error *err = NULL;
3602 struct got_reflist_entry *re, *se, *new;
3603 struct got_object_id *re_id, *se_id;
3604 struct got_tag_object *re_tag, *se_tag;
3605 time_t re_time, se_time;
3607 SIMPLEQ_FOREACH(re, tags, entry) {
3608 se = SIMPLEQ_FIRST(sorted);
3610 err = got_reflist_entry_dup(&new, re);
3613 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3616 err = got_ref_resolve(&re_id, repo, re->ref);
3619 err = got_object_open_as_tag(&re_tag, repo, re_id);
3623 re_time = got_object_tag_get_tagger_time(re_tag);
3624 got_object_tag_close(re_tag);
3628 err = got_ref_resolve(&se_id, repo, re->ref);
3631 err = got_object_open_as_tag(&se_tag, repo, se_id);
3635 se_time = got_object_tag_get_tagger_time(se_tag);
3636 got_object_tag_close(se_tag);
3638 if (se_time > re_time) {
3639 err = got_reflist_entry_dup(&new, re);
3642 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3645 se = SIMPLEQ_NEXT(se, entry);
3654 static const struct got_error *
3655 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3656 struct got_reference *ref2)
3658 const struct got_error *err = NULL;
3659 struct got_repository *repo = arg;
3660 struct got_object_id *id1, *id2 = NULL;
3661 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3662 time_t time1, time2;
3666 err = got_ref_resolve(&id1, repo, ref1);
3669 err = got_object_open_as_tag(&tag1, repo, id1);
3673 err = got_ref_resolve(&id2, repo, ref2);
3676 err = got_object_open_as_tag(&tag2, repo, id2);
3680 time1 = got_object_tag_get_tagger_time(tag1);
3681 time2 = got_object_tag_get_tagger_time(tag2);
3683 /* Put latest tags first. */
3686 else if (time1 > time2)
3689 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3694 got_object_tag_close(tag1);
3696 got_object_tag_close(tag2);
3700 static const struct got_error *
3701 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3703 static const struct got_error *err = NULL;
3704 struct got_reflist_head refs;
3705 struct got_reflist_entry *re;
3707 SIMPLEQ_INIT(&refs);
3709 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3713 SIMPLEQ_FOREACH(re, &refs, entry) {
3714 const char *refname;
3715 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3718 struct got_object_id *id;
3719 struct got_tag_object *tag;
3721 refname = got_ref_get_name(re->ref);
3722 if (strncmp(refname, "refs/tags/", 10) != 0)
3725 refstr = got_ref_to_str(re->ref);
3726 if (refstr == NULL) {
3727 err = got_error_from_errno("got_ref_to_str");
3730 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3733 err = got_ref_resolve(&id, repo, re->ref);
3736 err = got_object_open_as_tag(&tag, repo, id);
3740 printf("from: %s\n", got_object_tag_get_tagger(tag));
3741 tagger_time = got_object_tag_get_tagger_time(tag);
3742 datestr = get_datestr(&tagger_time, datebuf);
3744 printf("date: %s UTC\n", datestr);
3745 err = got_object_id_str(&id_str,
3746 got_object_tag_get_object_id(tag));
3749 switch (got_object_tag_get_object_type(tag)) {
3750 case GOT_OBJ_TYPE_BLOB:
3751 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3753 case GOT_OBJ_TYPE_TREE:
3754 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3756 case GOT_OBJ_TYPE_COMMIT:
3757 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3759 case GOT_OBJ_TYPE_TAG:
3760 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3766 tagmsg0 = strdup(got_object_tag_get_message(tag));
3767 got_object_tag_close(tag);
3768 if (tagmsg0 == NULL) {
3769 err = got_error_from_errno("strdup");
3775 line = strsep(&tagmsg, "\n");
3777 printf(" %s\n", line);
3782 got_ref_list_free(&refs);
3786 static const struct got_error *
3787 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3788 const char *tag_name, const char *repo_path)
3790 const struct got_error *err = NULL;
3791 char *template = NULL, *initial_content = NULL;
3792 char *editor = NULL;
3795 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3796 err = got_error_from_errno("asprintf");
3800 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3801 commit_id_str, tag_name) == -1) {
3802 err = got_error_from_errno("asprintf");
3806 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3810 dprintf(fd, initial_content);
3813 err = get_editor(&editor);
3816 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3818 free(initial_content);
3822 /* Editor is done; we can now apply unveil(2) */
3824 err = apply_unveil(repo_path, 0, NULL);
3833 static const struct got_error *
3834 add_tag(struct got_repository *repo, const char *tag_name,
3835 const char *commit_arg, const char *tagmsg_arg)
3837 const struct got_error *err = NULL;
3838 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3839 char *label = NULL, *commit_id_str = NULL;
3840 struct got_reference *ref = NULL;
3841 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3842 char *tagmsg_path = NULL, *tag_id_str = NULL;
3843 int preserve_tagmsg = 0;
3846 * Don't let the user create a tag named '-'.
3847 * While technically a valid reference name, this case is usually
3848 * an unintended typo.
3850 if (tag_name[0] == '-' && tag_name[1] == '\0')
3851 return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3853 err = get_author(&tagger, repo);
3857 err = match_object_id(&commit_id, &label, commit_arg,
3858 GOT_OBJ_TYPE_COMMIT, 1, repo);
3862 err = got_object_id_str(&commit_id_str, commit_id);
3866 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3867 refname = strdup(tag_name);
3868 if (refname == NULL) {
3869 err = got_error_from_errno("strdup");
3873 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3874 err = got_error_from_errno("asprintf");
3878 err = got_ref_open(&ref, repo, refname, 0);
3880 err = got_error(GOT_ERR_TAG_EXISTS);
3882 } else if (err->code != GOT_ERR_NOT_REF)
3885 if (tagmsg_arg == NULL) {
3886 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3887 tag_name, got_repo_get_path(repo));
3889 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3890 tagmsg_path != NULL)
3891 preserve_tagmsg = 1;
3896 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3897 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3900 preserve_tagmsg = 1;
3904 err = got_ref_alloc(&ref, refname, tag_id);
3907 preserve_tagmsg = 1;
3911 err = got_ref_write(ref, repo);
3914 preserve_tagmsg = 1;
3918 err = got_object_id_str(&tag_id_str, tag_id);
3921 preserve_tagmsg = 1;
3924 printf("Created tag %s\n", tag_id_str);
3926 if (preserve_tagmsg) {
3927 fprintf(stderr, "%s: tag message preserved in %s\n",
3928 getprogname(), tagmsg_path);
3929 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3930 err = got_error_from_errno2("unlink", tagmsg_path);
3935 free(commit_id_str);
3943 static const struct got_error *
3944 cmd_tag(int argc, char *argv[])
3946 const struct got_error *error = NULL;
3947 struct got_repository *repo = NULL;
3948 struct got_worktree *worktree = NULL;
3949 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3950 char *gitconfig_path = NULL;
3951 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3952 int ch, do_list = 0;
3954 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3960 repo_path = realpath(optarg, NULL);
3961 if (repo_path == NULL)
3962 return got_error_from_errno2("realpath",
3964 got_path_strip_trailing_slashes(repo_path);
3980 errx(1, "-l and -m options are mutually exclusive\n");
3983 } else if (argc < 1 || argc > 2)
3986 commit_id_arg = argv[1];
3991 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3995 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3996 "sendfd unveil", NULL) == -1)
4000 cwd = getcwd(NULL, 0);
4002 error = got_error_from_errno("getcwd");
4006 if (repo_path == NULL) {
4007 error = got_worktree_open(&worktree, cwd);
4008 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4014 strdup(got_worktree_get_repo_path(worktree));
4015 if (repo_path == NULL)
4016 error = got_error_from_errno("strdup");
4020 repo_path = strdup(cwd);
4021 if (repo_path == NULL) {
4022 error = got_error_from_errno("strdup");
4029 error = got_repo_open(&repo, repo_path, NULL);
4032 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4035 error = list_tags(repo, worktree);
4037 error = get_gitconfig_path(&gitconfig_path);
4040 error = got_repo_open(&repo, repo_path, gitconfig_path);
4045 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4050 if (commit_id_arg == NULL) {
4051 struct got_reference *head_ref;
4052 struct got_object_id *commit_id;
4053 error = got_ref_open(&head_ref, repo,
4054 worktree ? got_worktree_get_head_ref_name(worktree)
4058 error = got_ref_resolve(&commit_id, repo, head_ref);
4059 got_ref_close(head_ref);
4062 error = got_object_id_str(&commit_id_str, commit_id);
4068 error = add_tag(repo, tag_name,
4069 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4073 got_repo_close(repo);
4075 got_worktree_close(worktree);
4078 free(gitconfig_path);
4079 free(commit_id_str);
4086 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4090 static const struct got_error *
4091 cmd_add(int argc, char *argv[])
4093 const struct got_error *error = NULL;
4094 struct got_repository *repo = NULL;
4095 struct got_worktree *worktree = NULL;
4097 struct got_pathlist_head paths;
4098 struct got_pathlist_entry *pe;
4103 while ((ch = getopt(argc, argv, "")) != -1) {
4115 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4122 cwd = getcwd(NULL, 0);
4124 error = got_error_from_errno("getcwd");
4128 error = got_worktree_open(&worktree, cwd);
4132 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4137 error = apply_unveil(got_repo_get_path(repo), 1,
4138 got_worktree_get_root_path(worktree));
4142 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4146 error = got_worktree_schedule_add(worktree, &paths, print_status,
4150 got_repo_close(repo);
4152 got_worktree_close(worktree);
4153 TAILQ_FOREACH(pe, &paths, entry)
4154 free((char *)pe->path);
4155 got_pathlist_free(&paths);
4163 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4167 static const struct got_error *
4168 print_remove_status(void *arg, unsigned char status,
4169 unsigned char staged_status, const char *path,
4170 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4171 struct got_object_id *commit_id)
4173 if (status == GOT_STATUS_NONEXISTENT)
4175 if (status == staged_status && (status == GOT_STATUS_DELETE))
4176 status = GOT_STATUS_NO_CHANGE;
4177 printf("%c%c %s\n", status, staged_status, path);
4181 static const struct got_error *
4182 cmd_remove(int argc, char *argv[])
4184 const struct got_error *error = NULL;
4185 struct got_worktree *worktree = NULL;
4186 struct got_repository *repo = NULL;
4188 struct got_pathlist_head paths;
4189 struct got_pathlist_entry *pe;
4190 int ch, delete_local_mods = 0;
4194 while ((ch = getopt(argc, argv, "f")) != -1) {
4197 delete_local_mods = 1;
4209 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4216 cwd = getcwd(NULL, 0);
4218 error = got_error_from_errno("getcwd");
4221 error = got_worktree_open(&worktree, cwd);
4225 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4230 error = apply_unveil(got_repo_get_path(repo), 1,
4231 got_worktree_get_root_path(worktree));
4235 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4239 error = got_worktree_schedule_delete(worktree, &paths,
4240 delete_local_mods, print_remove_status, NULL, repo);
4245 got_repo_close(repo);
4247 got_worktree_close(worktree);
4248 TAILQ_FOREACH(pe, &paths, entry)
4249 free((char *)pe->path);
4250 got_pathlist_free(&paths);
4258 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4259 "path ...\n", getprogname());
4263 static const struct got_error *
4264 revert_progress(void *arg, unsigned char status, const char *path)
4266 while (path[0] == '/')
4268 printf("%c %s\n", status, path);
4272 struct choose_patch_arg {
4273 FILE *patch_script_file;
4277 static const struct got_error *
4278 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4279 int nchanges, const char *action)
4282 size_t linesize = 0;
4286 case GOT_STATUS_ADD:
4287 printf("A %s\n%s this addition? [y/n] ", path, action);
4289 case GOT_STATUS_DELETE:
4290 printf("D %s\n%s this deletion? [y/n] ", path, action);
4292 case GOT_STATUS_MODIFY:
4293 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4294 return got_error_from_errno("fseek");
4295 printf(GOT_COMMIT_SEP_STR);
4296 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4298 if (ferror(patch_file))
4299 return got_error_from_errno("getline");
4300 printf(GOT_COMMIT_SEP_STR);
4301 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4302 path, n, nchanges, action);
4305 return got_error_path(path, GOT_ERR_FILE_STATUS);
4311 static const struct got_error *
4312 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4313 FILE *patch_file, int n, int nchanges)
4315 const struct got_error *err = NULL;
4317 size_t linesize = 0;
4320 struct choose_patch_arg *a = arg;
4322 *choice = GOT_PATCH_CHOICE_NONE;
4324 if (a->patch_script_file) {
4326 err = show_change(status, path, patch_file, n, nchanges,
4330 linelen = getline(&line, &linesize, a->patch_script_file);
4331 if (linelen == -1) {
4332 if (ferror(a->patch_script_file))
4333 return got_error_from_errno("getline");
4336 nl = strchr(line, '\n');
4339 if (strcmp(line, "y") == 0) {
4340 *choice = GOT_PATCH_CHOICE_YES;
4342 } else if (strcmp(line, "n") == 0) {
4343 *choice = GOT_PATCH_CHOICE_NO;
4345 } else if (strcmp(line, "q") == 0 &&
4346 status == GOT_STATUS_MODIFY) {
4347 *choice = GOT_PATCH_CHOICE_QUIT;
4350 printf("invalid response '%s'\n", line);
4355 while (resp != 'y' && resp != 'n' && resp != 'q') {
4356 err = show_change(status, path, patch_file, n, nchanges,
4363 if (status == GOT_STATUS_MODIFY) {
4364 if (resp != 'y' && resp != 'n' && resp != 'q') {
4365 printf("invalid response '%c'\n", resp);
4368 } else if (resp != 'y' && resp != 'n') {
4369 printf("invalid response '%c'\n", resp);
4375 *choice = GOT_PATCH_CHOICE_YES;
4376 else if (resp == 'n')
4377 *choice = GOT_PATCH_CHOICE_NO;
4378 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4379 *choice = GOT_PATCH_CHOICE_QUIT;
4385 static const struct got_error *
4386 cmd_revert(int argc, char *argv[])
4388 const struct got_error *error = NULL;
4389 struct got_worktree *worktree = NULL;
4390 struct got_repository *repo = NULL;
4391 char *cwd = NULL, *path = NULL;
4392 struct got_pathlist_head paths;
4393 struct got_pathlist_entry *pe;
4394 int ch, can_recurse = 0, pflag = 0;
4395 FILE *patch_script_file = NULL;
4396 const char *patch_script_path = NULL;
4397 struct choose_patch_arg cpa;
4401 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4407 patch_script_path = optarg;
4422 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4423 "unveil", NULL) == -1)
4428 if (patch_script_path && !pflag)
4429 errx(1, "-F option can only be used together with -p option");
4431 cwd = getcwd(NULL, 0);
4433 error = got_error_from_errno("getcwd");
4436 error = got_worktree_open(&worktree, cwd);
4440 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4445 if (patch_script_path) {
4446 patch_script_file = fopen(patch_script_path, "r");
4447 if (patch_script_file == NULL) {
4448 error = got_error_from_errno2("fopen",
4453 error = apply_unveil(got_repo_get_path(repo), 1,
4454 got_worktree_get_root_path(worktree));
4458 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4465 TAILQ_FOREACH(pe, &paths, entry) {
4466 if (asprintf(&ondisk_path, "%s/%s",
4467 got_worktree_get_root_path(worktree),
4469 error = got_error_from_errno("asprintf");
4472 if (lstat(ondisk_path, &sb) == -1) {
4473 if (errno == ENOENT) {
4477 error = got_error_from_errno2("lstat",
4483 if (S_ISDIR(sb.st_mode)) {
4484 error = got_error_msg(GOT_ERR_BAD_PATH,
4485 "reverting directories requires -R option");
4491 cpa.patch_script_file = patch_script_file;
4492 cpa.action = "revert";
4493 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4494 pflag ? choose_patch : NULL, &cpa, repo);
4498 if (patch_script_file && fclose(patch_script_file) == EOF &&
4500 error = got_error_from_errno2("fclose", patch_script_path);
4502 got_repo_close(repo);
4504 got_worktree_close(worktree);
4513 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4518 struct collect_commit_logmsg_arg {
4519 const char *cmdline_log;
4521 const char *worktree_path;
4522 const char *branch_name;
4523 const char *repo_path;
4528 static const struct got_error *
4529 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4532 char *initial_content = NULL;
4533 struct got_pathlist_entry *pe;
4534 const struct got_error *err = NULL;
4535 char *template = NULL;
4536 struct collect_commit_logmsg_arg *a = arg;
4540 /* if a message was specified on the command line, just use it */
4541 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4542 len = strlen(a->cmdline_log) + 1;
4543 *logmsg = malloc(len + 1);
4544 if (*logmsg == NULL)
4545 return got_error_from_errno("malloc");
4546 strlcpy(*logmsg, a->cmdline_log, len);
4550 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4551 return got_error_from_errno("asprintf");
4553 if (asprintf(&initial_content,
4554 "\n# changes to be committed on branch %s:\n",
4555 a->branch_name) == -1)
4556 return got_error_from_errno("asprintf");
4558 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4562 dprintf(fd, initial_content);
4564 TAILQ_FOREACH(pe, commitable_paths, entry) {
4565 struct got_commitable *ct = pe->data;
4566 dprintf(fd, "# %c %s\n",
4567 got_commitable_get_status(ct),
4568 got_commitable_get_path(ct));
4572 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4574 free(initial_content);
4577 /* Editor is done; we can now apply unveil(2) */
4579 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4588 static const struct got_error *
4589 cmd_commit(int argc, char *argv[])
4591 const struct got_error *error = NULL;
4592 struct got_worktree *worktree = NULL;
4593 struct got_repository *repo = NULL;
4594 char *cwd = NULL, *id_str = NULL;
4595 struct got_object_id *id = NULL;
4596 const char *logmsg = NULL;
4597 struct collect_commit_logmsg_arg cl_arg;
4598 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4599 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4600 struct got_pathlist_head paths;
4603 cl_arg.logmsg_path = NULL;
4605 while ((ch = getopt(argc, argv, "m:")) != -1) {
4620 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4621 "unveil", NULL) == -1)
4624 cwd = getcwd(NULL, 0);
4626 error = got_error_from_errno("getcwd");
4629 error = got_worktree_open(&worktree, cwd);
4633 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4636 if (rebase_in_progress) {
4637 error = got_error(GOT_ERR_REBASING);
4641 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4646 error = get_gitconfig_path(&gitconfig_path);
4649 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4654 error = get_author(&author, repo);
4659 * unveil(2) traverses exec(2); if an editor is used we have
4660 * to apply unveil after the log message has been written.
4662 if (logmsg == NULL || strlen(logmsg) == 0)
4663 error = get_editor(&editor);
4665 error = apply_unveil(got_repo_get_path(repo), 0,
4666 got_worktree_get_root_path(worktree));
4670 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4674 cl_arg.editor = editor;
4675 cl_arg.cmdline_log = logmsg;
4676 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4677 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4678 if (!histedit_in_progress) {
4679 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4680 error = got_error(GOT_ERR_COMMIT_BRANCH);
4683 cl_arg.branch_name += 11;
4685 cl_arg.repo_path = got_repo_get_path(repo);
4686 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4687 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4689 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4690 cl_arg.logmsg_path != NULL)
4691 preserve_logmsg = 1;
4695 error = got_object_id_str(&id_str, id);
4698 printf("Created commit %s\n", id_str);
4700 if (preserve_logmsg) {
4701 fprintf(stderr, "%s: log message preserved in %s\n",
4702 getprogname(), cl_arg.logmsg_path);
4703 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4705 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4706 free(cl_arg.logmsg_path);
4708 got_repo_close(repo);
4710 got_worktree_close(worktree);
4713 free(gitconfig_path);
4720 usage_cherrypick(void)
4722 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4726 static const struct got_error *
4727 cmd_cherrypick(int argc, char *argv[])
4729 const struct got_error *error = NULL;
4730 struct got_worktree *worktree = NULL;
4731 struct got_repository *repo = NULL;
4732 char *cwd = NULL, *commit_id_str = NULL;
4733 struct got_object_id *commit_id = NULL;
4734 struct got_commit_object *commit = NULL;
4735 struct got_object_qid *pid;
4736 struct got_reference *head_ref = NULL;
4737 int ch, did_something = 0;
4739 while ((ch = getopt(argc, argv, "")) != -1) {
4751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4752 "unveil", NULL) == -1)
4758 cwd = getcwd(NULL, 0);
4760 error = got_error_from_errno("getcwd");
4763 error = got_worktree_open(&worktree, cwd);
4767 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4772 error = apply_unveil(got_repo_get_path(repo), 0,
4773 got_worktree_get_root_path(worktree));
4777 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4778 GOT_OBJ_TYPE_COMMIT, repo);
4779 if (error != NULL) {
4780 struct got_reference *ref;
4781 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4783 error = got_ref_open(&ref, repo, argv[0], 0);
4786 error = got_ref_resolve(&commit_id, repo, ref);
4791 error = got_object_id_str(&commit_id_str, commit_id);
4795 error = got_ref_open(&head_ref, repo,
4796 got_worktree_get_head_ref_name(worktree), 0);
4800 error = check_same_branch(commit_id, head_ref, NULL, repo);
4802 if (error->code != GOT_ERR_ANCESTRY)
4806 error = got_error(GOT_ERR_SAME_BRANCH);
4810 error = got_object_open_as_commit(&commit, repo, commit_id);
4813 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4814 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4815 commit_id, repo, update_progress, &did_something, check_cancelled,
4821 printf("Merged commit %s\n", commit_id_str);
4824 got_object_commit_close(commit);
4825 free(commit_id_str);
4827 got_ref_close(head_ref);
4829 got_worktree_close(worktree);
4831 got_repo_close(repo);
4838 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4842 static const struct got_error *
4843 cmd_backout(int argc, char *argv[])
4845 const struct got_error *error = NULL;
4846 struct got_worktree *worktree = NULL;
4847 struct got_repository *repo = NULL;
4848 char *cwd = NULL, *commit_id_str = NULL;
4849 struct got_object_id *commit_id = NULL;
4850 struct got_commit_object *commit = NULL;
4851 struct got_object_qid *pid;
4852 struct got_reference *head_ref = NULL;
4853 int ch, did_something = 0;
4855 while ((ch = getopt(argc, argv, "")) != -1) {
4867 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4868 "unveil", NULL) == -1)
4874 cwd = getcwd(NULL, 0);
4876 error = got_error_from_errno("getcwd");
4879 error = got_worktree_open(&worktree, cwd);
4883 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4888 error = apply_unveil(got_repo_get_path(repo), 0,
4889 got_worktree_get_root_path(worktree));
4893 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4894 GOT_OBJ_TYPE_COMMIT, repo);
4895 if (error != NULL) {
4896 struct got_reference *ref;
4897 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4899 error = got_ref_open(&ref, repo, argv[0], 0);
4902 error = got_ref_resolve(&commit_id, repo, ref);
4907 error = got_object_id_str(&commit_id_str, commit_id);
4911 error = got_ref_open(&head_ref, repo,
4912 got_worktree_get_head_ref_name(worktree), 0);
4916 error = check_same_branch(commit_id, head_ref, NULL, repo);
4920 error = got_object_open_as_commit(&commit, repo, commit_id);
4923 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4925 error = got_error(GOT_ERR_ROOT_COMMIT);
4929 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4930 update_progress, &did_something, check_cancelled, NULL);
4935 printf("Backed out commit %s\n", commit_id_str);
4938 got_object_commit_close(commit);
4939 free(commit_id_str);
4941 got_ref_close(head_ref);
4943 got_worktree_close(worktree);
4945 got_repo_close(repo);
4952 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4958 trim_logmsg(char *logmsg, int limit)
4963 len = strlen(logmsg);
4967 nl = strchr(logmsg, '\n');
4972 static const struct got_error *
4973 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4975 const struct got_error *err;
4976 char *logmsg0 = NULL;
4979 err = got_object_commit_get_logmsg(&logmsg0, commit);
4984 while (isspace((unsigned char)s[0]))
4987 *logmsg = strdup(s);
4988 if (*logmsg == NULL) {
4989 err = got_error_from_errno("strdup");
4993 trim_logmsg(*logmsg, limit);
4999 static const struct got_error *
5000 show_rebase_progress(struct got_commit_object *commit,
5001 struct got_object_id *old_id, struct got_object_id *new_id)
5003 const struct got_error *err;
5004 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5006 err = got_object_id_str(&old_id_str, old_id);
5011 err = got_object_id_str(&new_id_str, new_id);
5016 old_id_str[12] = '\0';
5018 new_id_str[12] = '\0';
5020 err = get_short_logmsg(&logmsg, 42, commit);
5024 printf("%s -> %s: %s\n", old_id_str,
5025 new_id_str ? new_id_str : "no-op change", logmsg);
5032 static const struct got_error *
5033 rebase_progress(void *arg, unsigned char status, const char *path)
5035 unsigned char *rebase_status = arg;
5037 while (path[0] == '/')
5039 printf("%c %s\n", status, path);
5041 if (*rebase_status == GOT_STATUS_CONFLICT)
5043 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5044 *rebase_status = status;
5048 static const struct got_error *
5049 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5050 struct got_reference *branch, struct got_reference *new_base_branch,
5051 struct got_reference *tmp_branch, struct got_repository *repo)
5053 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5054 return got_worktree_rebase_complete(worktree, fileindex,
5055 new_base_branch, tmp_branch, branch, repo);
5058 static const struct got_error *
5059 rebase_commit(struct got_pathlist_head *merged_paths,
5060 struct got_worktree *worktree, struct got_fileindex *fileindex,
5061 struct got_reference *tmp_branch,
5062 struct got_object_id *commit_id, struct got_repository *repo)
5064 const struct got_error *error;
5065 struct got_commit_object *commit;
5066 struct got_object_id *new_commit_id;
5068 error = got_object_open_as_commit(&commit, repo, commit_id);
5072 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5073 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5075 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5077 error = show_rebase_progress(commit, commit_id, NULL);
5079 error = show_rebase_progress(commit, commit_id, new_commit_id);
5080 free(new_commit_id);
5083 got_object_commit_close(commit);
5087 struct check_path_prefix_arg {
5088 const char *path_prefix;
5093 static const struct got_error *
5094 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5095 struct got_blob_object *blob2, struct got_object_id *id1,
5096 struct got_object_id *id2, const char *path1, const char *path2,
5097 mode_t mode1, mode_t mode2, struct got_repository *repo)
5099 struct check_path_prefix_arg *a = arg;
5101 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5102 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5103 return got_error(a->errcode);
5108 static const struct got_error *
5109 check_path_prefix(struct got_object_id *parent_id,
5110 struct got_object_id *commit_id, const char *path_prefix,
5111 int errcode, struct got_repository *repo)
5113 const struct got_error *err;
5114 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5115 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5116 struct check_path_prefix_arg cpp_arg;
5118 if (got_path_is_root_dir(path_prefix))
5121 err = got_object_open_as_commit(&commit, repo, commit_id);
5125 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5129 err = got_object_open_as_tree(&tree1, repo,
5130 got_object_commit_get_tree_id(parent_commit));
5134 err = got_object_open_as_tree(&tree2, repo,
5135 got_object_commit_get_tree_id(commit));
5139 cpp_arg.path_prefix = path_prefix;
5140 while (cpp_arg.path_prefix[0] == '/')
5141 cpp_arg.path_prefix++;
5142 cpp_arg.len = strlen(cpp_arg.path_prefix);
5143 cpp_arg.errcode = errcode;
5144 err = got_diff_tree(tree1, tree2, "", "", repo,
5145 check_path_prefix_in_diff, &cpp_arg, 0);
5148 got_object_tree_close(tree1);
5150 got_object_tree_close(tree2);
5152 got_object_commit_close(commit);
5154 got_object_commit_close(parent_commit);
5158 static const struct got_error *
5159 collect_commits(struct got_object_id_queue *commits,
5160 struct got_object_id *initial_commit_id,
5161 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5162 const char *path_prefix, int path_prefix_errcode,
5163 struct got_repository *repo)
5165 const struct got_error *err = NULL;
5166 struct got_commit_graph *graph = NULL;
5167 struct got_object_id *parent_id = NULL;
5168 struct got_object_qid *qid;
5169 struct got_object_id *commit_id = initial_commit_id;
5171 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5175 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5176 check_cancelled, NULL);
5179 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5180 err = got_commit_graph_iter_next(&parent_id, graph);
5182 if (err->code == GOT_ERR_ITER_COMPLETED) {
5183 err = got_error_msg(GOT_ERR_ANCESTRY,
5184 "ran out of commits to rebase before "
5185 "youngest common ancestor commit has "
5188 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5190 err = got_commit_graph_fetch_commits(graph, 1, repo,
5191 check_cancelled, NULL);
5195 err = check_path_prefix(parent_id, commit_id,
5196 path_prefix, path_prefix_errcode, repo);
5200 err = got_object_qid_alloc(&qid, commit_id);
5203 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5204 commit_id = parent_id;
5208 got_commit_graph_close(graph);
5212 static const struct got_error *
5213 cmd_rebase(int argc, char *argv[])
5215 const struct got_error *error = NULL;
5216 struct got_worktree *worktree = NULL;
5217 struct got_repository *repo = NULL;
5218 struct got_fileindex *fileindex = NULL;
5220 struct got_reference *branch = NULL;
5221 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5222 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5223 struct got_object_id *resume_commit_id = NULL;
5224 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5225 struct got_commit_object *commit = NULL;
5226 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5227 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5228 struct got_object_id_queue commits;
5229 struct got_pathlist_head merged_paths;
5230 const struct got_object_id_queue *parent_ids;
5231 struct got_object_qid *qid, *pid;
5233 SIMPLEQ_INIT(&commits);
5234 TAILQ_INIT(&merged_paths);
5236 while ((ch = getopt(argc, argv, "ac")) != -1) {
5242 continue_rebase = 1;
5254 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5255 "unveil", NULL) == -1)
5258 if (abort_rebase && continue_rebase)
5260 else if (abort_rebase || continue_rebase) {
5263 } else if (argc != 1)
5266 cwd = getcwd(NULL, 0);
5268 error = got_error_from_errno("getcwd");
5271 error = got_worktree_open(&worktree, cwd);
5275 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5280 error = apply_unveil(got_repo_get_path(repo), 0,
5281 got_worktree_get_root_path(worktree));
5285 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5291 if (!rebase_in_progress) {
5292 error = got_error(GOT_ERR_NOT_REBASING);
5295 error = got_worktree_rebase_continue(&resume_commit_id,
5296 &new_base_branch, &tmp_branch, &branch, &fileindex,
5300 printf("Switching work tree to %s\n",
5301 got_ref_get_symref_target(new_base_branch));
5302 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5303 new_base_branch, update_progress, &did_something);
5306 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5307 goto done; /* nothing else to do */
5310 if (continue_rebase) {
5311 if (!rebase_in_progress) {
5312 error = got_error(GOT_ERR_NOT_REBASING);
5315 error = got_worktree_rebase_continue(&resume_commit_id,
5316 &new_base_branch, &tmp_branch, &branch, &fileindex,
5321 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5322 resume_commit_id, repo);
5326 yca_id = got_object_id_dup(resume_commit_id);
5327 if (yca_id == NULL) {
5328 error = got_error_from_errno("got_object_id_dup");
5332 error = got_ref_open(&branch, repo, argv[0], 0);
5337 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5341 if (!continue_rebase) {
5342 struct got_object_id *base_commit_id;
5344 base_commit_id = got_worktree_get_base_commit_id(worktree);
5345 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5346 base_commit_id, branch_head_commit_id, repo,
5347 check_cancelled, NULL);
5350 if (yca_id == NULL) {
5351 error = got_error_msg(GOT_ERR_ANCESTRY,
5352 "specified branch shares no common ancestry "
5353 "with work tree's branch");
5357 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5359 if (error->code != GOT_ERR_ANCESTRY)
5363 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5364 "specified branch resolves to a commit which "
5365 "is already contained in work tree's branch");
5368 error = got_worktree_rebase_prepare(&new_base_branch,
5369 &tmp_branch, &fileindex, worktree, branch, repo);
5374 commit_id = branch_head_commit_id;
5375 error = got_object_open_as_commit(&commit, repo, commit_id);
5379 parent_ids = got_object_commit_get_parent_ids(commit);
5380 pid = SIMPLEQ_FIRST(parent_ids);
5382 if (!continue_rebase) {
5384 error = got_worktree_rebase_abort(worktree, fileindex,
5385 repo, new_base_branch, update_progress,
5389 printf("Rebase of %s aborted\n",
5390 got_ref_get_name(branch));
5392 error = got_error(GOT_ERR_EMPTY_REBASE);
5395 error = collect_commits(&commits, commit_id, pid->id,
5396 yca_id, got_worktree_get_path_prefix(worktree),
5397 GOT_ERR_REBASE_PATH, repo);
5398 got_object_commit_close(commit);
5403 if (SIMPLEQ_EMPTY(&commits)) {
5404 if (continue_rebase)
5405 error = rebase_complete(worktree, fileindex,
5406 branch, new_base_branch, tmp_branch, repo);
5408 error = got_error(GOT_ERR_EMPTY_REBASE);
5413 SIMPLEQ_FOREACH(qid, &commits, entry) {
5414 commit_id = qid->id;
5415 parent_id = pid ? pid->id : yca_id;
5418 error = got_worktree_rebase_merge_files(&merged_paths,
5419 worktree, fileindex, parent_id, commit_id, repo,
5420 rebase_progress, &rebase_status, check_cancelled, NULL);
5424 if (rebase_status == GOT_STATUS_CONFLICT) {
5425 got_worktree_rebase_pathlist_free(&merged_paths);
5429 error = rebase_commit(&merged_paths, worktree, fileindex,
5430 tmp_branch, commit_id, repo);
5431 got_worktree_rebase_pathlist_free(&merged_paths);
5436 if (rebase_status == GOT_STATUS_CONFLICT) {
5437 error = got_worktree_rebase_postpone(worktree, fileindex);
5440 error = got_error_msg(GOT_ERR_CONFLICTS,
5441 "conflicts must be resolved before rebasing can continue");
5443 error = rebase_complete(worktree, fileindex, branch,
5444 new_base_branch, tmp_branch, repo);
5446 got_object_id_queue_free(&commits);
5447 free(branch_head_commit_id);
5448 free(resume_commit_id);
5451 got_object_commit_close(commit);
5453 got_ref_close(branch);
5454 if (new_base_branch)
5455 got_ref_close(new_base_branch);
5457 got_ref_close(tmp_branch);
5459 got_worktree_close(worktree);
5461 got_repo_close(repo);
5466 usage_histedit(void)
5468 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5473 #define GOT_HISTEDIT_PICK 'p'
5474 #define GOT_HISTEDIT_EDIT 'e'
5475 #define GOT_HISTEDIT_FOLD 'f'
5476 #define GOT_HISTEDIT_DROP 'd'
5477 #define GOT_HISTEDIT_MESG 'm'
5479 static struct got_histedit_cmd {
5483 } got_histedit_cmds[] = {
5484 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5485 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5486 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5487 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5488 { GOT_HISTEDIT_MESG, "mesg",
5489 "single-line log message for commit above (open editor if empty)" },
5492 struct got_histedit_list_entry {
5493 TAILQ_ENTRY(got_histedit_list_entry) entry;
5494 struct got_object_id *commit_id;
5495 const struct got_histedit_cmd *cmd;
5498 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5500 static const struct got_error *
5501 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5502 FILE *f, struct got_repository *repo)
5504 const struct got_error *err = NULL;
5505 char *logmsg = NULL, *id_str = NULL;
5506 struct got_commit_object *commit = NULL;
5509 err = got_object_open_as_commit(&commit, repo, commit_id);
5513 err = get_short_logmsg(&logmsg, 34, commit);
5517 err = got_object_id_str(&id_str, commit_id);
5521 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5523 err = got_ferror(f, GOT_ERR_IO);
5526 got_object_commit_close(commit);
5532 static const struct got_error *
5533 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5534 struct got_repository *repo)
5536 const struct got_error *err = NULL;
5537 struct got_object_qid *qid;
5539 if (SIMPLEQ_EMPTY(commits))
5540 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5542 SIMPLEQ_FOREACH(qid, commits, entry) {
5543 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5552 static const struct got_error *
5553 write_cmd_list(FILE *f)
5555 const struct got_error *err = NULL;
5558 n = fprintf(f, "# Available histedit commands:\n");
5560 return got_ferror(f, GOT_ERR_IO);
5562 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5563 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5564 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5567 err = got_ferror(f, GOT_ERR_IO);
5571 n = fprintf(f, "# Commits will be processed in order from top to "
5572 "bottom of this file.\n");
5574 return got_ferror(f, GOT_ERR_IO);
5578 static const struct got_error *
5579 histedit_syntax_error(int lineno)
5581 static char msg[42];
5584 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5586 if (ret == -1 || ret >= sizeof(msg))
5587 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5589 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5592 static const struct got_error *
5593 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5594 char *logmsg, struct got_repository *repo)
5596 const struct got_error *err;
5597 struct got_commit_object *folded_commit = NULL;
5598 char *id_str, *folded_logmsg = NULL;
5600 err = got_object_id_str(&id_str, hle->commit_id);
5604 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5608 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5611 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5612 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5613 folded_logmsg) == -1) {
5614 err = got_error_from_errno("asprintf");
5619 got_object_commit_close(folded_commit);
5621 free(folded_logmsg);
5625 static struct got_histedit_list_entry *
5626 get_folded_commits(struct got_histedit_list_entry *hle)
5628 struct got_histedit_list_entry *prev, *folded = NULL;
5630 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5631 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5632 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5633 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5635 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5641 static const struct got_error *
5642 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5643 struct got_repository *repo)
5645 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5646 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5647 const struct got_error *err = NULL;
5648 struct got_commit_object *commit = NULL;
5650 struct got_histedit_list_entry *folded = NULL;
5652 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5656 folded = get_folded_commits(hle);
5658 while (folded != hle) {
5659 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5660 folded = TAILQ_NEXT(folded, entry);
5663 err = append_folded_commit_msg(&new_msg, folded,
5669 folded = TAILQ_NEXT(folded, entry);
5673 err = got_object_id_str(&id_str, hle->commit_id);
5676 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5679 if (asprintf(&new_msg,
5680 "%s\n# original log message of commit %s: %s",
5681 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5682 err = got_error_from_errno("asprintf");
5688 err = got_object_id_str(&id_str, hle->commit_id);
5692 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5696 dprintf(fd, logmsg);
5699 err = get_editor(&editor);
5703 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5705 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5707 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5710 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5711 err = got_error_from_errno2("unlink", logmsg_path);
5717 got_object_commit_close(commit);
5721 static const struct got_error *
5722 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5723 FILE *f, struct got_repository *repo)
5725 const struct got_error *err = NULL;
5726 char *line = NULL, *p, *end;
5730 const struct got_histedit_cmd *cmd;
5731 struct got_object_id *commit_id = NULL;
5732 struct got_histedit_list_entry *hle = NULL;
5735 len = getline(&line, &size, f);
5737 const struct got_error *getline_err;
5740 getline_err = got_error_from_errno("getline");
5741 err = got_ferror(f, getline_err->code);
5746 while (isspace((unsigned char)p[0]))
5748 if (p[0] == '#' || p[0] == '\0') {
5754 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5755 cmd = &got_histedit_cmds[i];
5756 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5757 isspace((unsigned char)p[strlen(cmd->name)])) {
5758 p += strlen(cmd->name);
5761 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5766 if (i == nitems(got_histedit_cmds)) {
5767 err = histedit_syntax_error(lineno);
5770 while (isspace((unsigned char)p[0]))
5772 if (cmd->code == GOT_HISTEDIT_MESG) {
5773 if (hle == NULL || hle->logmsg != NULL) {
5774 err = got_error(GOT_ERR_HISTEDIT_CMD);
5778 err = histedit_edit_logmsg(hle, repo);
5782 hle->logmsg = strdup(p);
5783 if (hle->logmsg == NULL) {
5784 err = got_error_from_errno("strdup");
5793 while (end[0] && !isspace((unsigned char)end[0]))
5797 err = got_object_resolve_id_str(&commit_id, repo, p);
5799 /* override error code */
5800 err = histedit_syntax_error(lineno);
5804 hle = malloc(sizeof(*hle));
5806 err = got_error_from_errno("malloc");
5810 hle->commit_id = commit_id;
5815 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5823 static const struct got_error *
5824 histedit_check_script(struct got_histedit_list *histedit_cmds,
5825 struct got_object_id_queue *commits, struct got_repository *repo)
5827 const struct got_error *err = NULL;
5828 struct got_object_qid *qid;
5829 struct got_histedit_list_entry *hle;
5830 static char msg[80];
5833 if (TAILQ_EMPTY(histedit_cmds))
5834 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5835 "histedit script contains no commands");
5836 if (SIMPLEQ_EMPTY(commits))
5837 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5839 SIMPLEQ_FOREACH(qid, commits, entry) {
5840 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5841 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5845 err = got_object_id_str(&id_str, qid->id);
5848 snprintf(msg, sizeof(msg),
5849 "commit %s missing from histedit script", id_str);
5851 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5855 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5856 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5857 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5858 "last commit in histedit script cannot be folded");
5863 static const struct got_error *
5864 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5865 const char *path, struct got_object_id_queue *commits,
5866 struct got_repository *repo)
5868 const struct got_error *err = NULL;
5872 err = get_editor(&editor);
5876 if (spawn_editor(editor, path) == -1) {
5877 err = got_error_from_errno("failed spawning editor");
5881 f = fopen(path, "r");
5883 err = got_error_from_errno("fopen");
5886 err = histedit_parse_list(histedit_cmds, f, repo);
5890 err = histedit_check_script(histedit_cmds, commits, repo);
5892 if (f && fclose(f) != 0 && err == NULL)
5893 err = got_error_from_errno("fclose");
5898 static const struct got_error *
5899 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5900 struct got_object_id_queue *, const char *, struct got_repository *);
5902 static const struct got_error *
5903 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5904 struct got_object_id_queue *commits, struct got_repository *repo)
5906 const struct got_error *err;
5910 err = got_opentemp_named(&path, &f, "got-histedit");
5914 err = write_cmd_list(f);
5918 err = histedit_write_commit_list(commits, f, repo);
5922 if (fclose(f) != 0) {
5923 err = got_error_from_errno("fclose");
5928 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5930 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5931 err->code != GOT_ERR_HISTEDIT_CMD)
5933 err = histedit_edit_list_retry(histedit_cmds, err,
5934 commits, path, repo);
5937 if (f && fclose(f) != 0 && err == NULL)
5938 err = got_error_from_errno("fclose");
5939 if (path && unlink(path) != 0 && err == NULL)
5940 err = got_error_from_errno2("unlink", path);
5945 static const struct got_error *
5946 histedit_save_list(struct got_histedit_list *histedit_cmds,
5947 struct got_worktree *worktree, struct got_repository *repo)
5949 const struct got_error *err = NULL;
5952 struct got_histedit_list_entry *hle;
5953 struct got_commit_object *commit = NULL;
5955 err = got_worktree_get_histedit_script_path(&path, worktree);
5959 f = fopen(path, "w");
5961 err = got_error_from_errno2("fopen", path);
5964 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5965 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5971 int n = fprintf(f, "%c %s\n",
5972 GOT_HISTEDIT_MESG, hle->logmsg);
5974 err = got_ferror(f, GOT_ERR_IO);
5980 if (f && fclose(f) != 0 && err == NULL)
5981 err = got_error_from_errno("fclose");
5984 got_object_commit_close(commit);
5989 histedit_free_list(struct got_histedit_list *histedit_cmds)
5991 struct got_histedit_list_entry *hle;
5993 while ((hle = TAILQ_FIRST(histedit_cmds))) {
5994 TAILQ_REMOVE(histedit_cmds, hle, entry);
5999 static const struct got_error *
6000 histedit_load_list(struct got_histedit_list *histedit_cmds,
6001 const char *path, struct got_repository *repo)
6003 const struct got_error *err = NULL;
6006 f = fopen(path, "r");
6008 err = got_error_from_errno2("fopen", path);
6012 err = histedit_parse_list(histedit_cmds, f, repo);
6014 if (f && fclose(f) != 0 && err == NULL)
6015 err = got_error_from_errno("fclose");
6019 static const struct got_error *
6020 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6021 const struct got_error *edit_err, struct got_object_id_queue *commits,
6022 const char *path, struct got_repository *repo)
6024 const struct got_error *err = NULL, *prev_err = edit_err;
6027 while (resp != 'c' && resp != 'r' && resp != 'a') {
6028 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6029 "or (a)bort: ", getprogname(), prev_err->msg);
6034 histedit_free_list(histedit_cmds);
6035 err = histedit_run_editor(histedit_cmds, path, commits,
6038 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6039 err->code != GOT_ERR_HISTEDIT_CMD)
6046 } else if (resp == 'r') {
6047 histedit_free_list(histedit_cmds);
6048 err = histedit_edit_script(histedit_cmds,
6051 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6052 err->code != GOT_ERR_HISTEDIT_CMD)
6059 } else if (resp == 'a') {
6060 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6063 printf("invalid response '%c'\n", resp);
6069 static const struct got_error *
6070 histedit_complete(struct got_worktree *worktree,
6071 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6072 struct got_reference *branch, struct got_repository *repo)
6074 printf("Switching work tree to %s\n",
6075 got_ref_get_symref_target(branch));
6076 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6080 static const struct got_error *
6081 show_histedit_progress(struct got_commit_object *commit,
6082 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6084 const struct got_error *err;
6085 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6087 err = got_object_id_str(&old_id_str, hle->commit_id);
6092 err = got_object_id_str(&new_id_str, new_id);
6097 old_id_str[12] = '\0';
6099 new_id_str[12] = '\0';
6102 logmsg = strdup(hle->logmsg);
6103 if (logmsg == NULL) {
6104 err = got_error_from_errno("strdup");
6107 trim_logmsg(logmsg, 42);
6109 err = get_short_logmsg(&logmsg, 42, commit);
6114 switch (hle->cmd->code) {
6115 case GOT_HISTEDIT_PICK:
6116 case GOT_HISTEDIT_EDIT:
6117 printf("%s -> %s: %s\n", old_id_str,
6118 new_id_str ? new_id_str : "no-op change", logmsg);
6120 case GOT_HISTEDIT_DROP:
6121 case GOT_HISTEDIT_FOLD:
6122 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6135 static const struct got_error *
6136 histedit_commit(struct got_pathlist_head *merged_paths,
6137 struct got_worktree *worktree, struct got_fileindex *fileindex,
6138 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6139 struct got_repository *repo)
6141 const struct got_error *err;
6142 struct got_commit_object *commit;
6143 struct got_object_id *new_commit_id;
6145 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6146 && hle->logmsg == NULL) {
6147 err = histedit_edit_logmsg(hle, repo);
6152 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6156 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6157 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6160 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6162 err = show_histedit_progress(commit, hle, NULL);
6164 err = show_histedit_progress(commit, hle, new_commit_id);
6165 free(new_commit_id);
6168 got_object_commit_close(commit);
6172 static const struct got_error *
6173 histedit_skip_commit(struct got_histedit_list_entry *hle,
6174 struct got_worktree *worktree, struct got_repository *repo)
6176 const struct got_error *error;
6177 struct got_commit_object *commit;
6179 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6184 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6188 error = show_histedit_progress(commit, hle, NULL);
6189 got_object_commit_close(commit);
6193 static const struct got_error *
6194 cmd_histedit(int argc, char *argv[])
6196 const struct got_error *error = NULL;
6197 struct got_worktree *worktree = NULL;
6198 struct got_fileindex *fileindex = NULL;
6199 struct got_repository *repo = NULL;
6201 struct got_reference *branch = NULL;
6202 struct got_reference *tmp_branch = NULL;
6203 struct got_object_id *resume_commit_id = NULL;
6204 struct got_object_id *base_commit_id = NULL;
6205 struct got_object_id *head_commit_id = NULL;
6206 struct got_commit_object *commit = NULL;
6207 int ch, rebase_in_progress = 0, did_something;
6208 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6209 const char *edit_script_path = NULL;
6210 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6211 struct got_object_id_queue commits;
6212 struct got_pathlist_head merged_paths;
6213 const struct got_object_id_queue *parent_ids;
6214 struct got_object_qid *pid;
6215 struct got_histedit_list histedit_cmds;
6216 struct got_histedit_list_entry *hle;
6218 SIMPLEQ_INIT(&commits);
6219 TAILQ_INIT(&histedit_cmds);
6220 TAILQ_INIT(&merged_paths);
6222 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6231 edit_script_path = optarg;
6243 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6244 "unveil", NULL) == -1)
6247 if (abort_edit && continue_edit)
6253 * This command cannot apply unveil(2) in all cases because the
6254 * user may choose to run an editor to edit the histedit script
6255 * and to edit individual commit log messages.
6256 * unveil(2) traverses exec(2); if an editor is used we have to
6257 * apply unveil after edit script and log messages have been written.
6258 * XXX TODO: Make use of unveil(2) where possible.
6261 cwd = getcwd(NULL, 0);
6263 error = got_error_from_errno("getcwd");
6266 error = got_worktree_open(&worktree, cwd);
6270 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6275 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6278 if (rebase_in_progress) {
6279 error = got_error(GOT_ERR_REBASING);
6283 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6287 if (edit_in_progress && abort_edit) {
6288 error = got_worktree_histedit_continue(&resume_commit_id,
6289 &tmp_branch, &branch, &base_commit_id, &fileindex,
6293 printf("Switching work tree to %s\n",
6294 got_ref_get_symref_target(branch));
6295 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6296 branch, base_commit_id, update_progress, &did_something);
6299 printf("Histedit of %s aborted\n",
6300 got_ref_get_symref_target(branch));
6301 goto done; /* nothing else to do */
6302 } else if (abort_edit) {
6303 error = got_error(GOT_ERR_NOT_HISTEDIT);
6307 if (continue_edit) {
6310 if (!edit_in_progress) {
6311 error = got_error(GOT_ERR_NOT_HISTEDIT);
6315 error = got_worktree_get_histedit_script_path(&path, worktree);
6319 error = histedit_load_list(&histedit_cmds, path, repo);
6324 error = got_worktree_histedit_continue(&resume_commit_id,
6325 &tmp_branch, &branch, &base_commit_id, &fileindex,
6330 error = got_ref_resolve(&head_commit_id, repo, branch);
6334 error = got_object_open_as_commit(&commit, repo,
6338 parent_ids = got_object_commit_get_parent_ids(commit);
6339 pid = SIMPLEQ_FIRST(parent_ids);
6341 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6344 error = collect_commits(&commits, head_commit_id, pid->id,
6345 base_commit_id, got_worktree_get_path_prefix(worktree),
6346 GOT_ERR_HISTEDIT_PATH, repo);
6347 got_object_commit_close(commit);
6352 if (edit_in_progress) {
6353 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6357 error = got_ref_open(&branch, repo,
6358 got_worktree_get_head_ref_name(worktree), 0);
6362 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6363 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6364 "will not edit commit history of a branch outside "
6365 "the \"refs/heads/\" reference namespace");
6369 error = got_ref_resolve(&head_commit_id, repo, branch);
6370 got_ref_close(branch);
6375 error = got_object_open_as_commit(&commit, repo,
6379 parent_ids = got_object_commit_get_parent_ids(commit);
6380 pid = SIMPLEQ_FIRST(parent_ids);
6382 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6385 error = collect_commits(&commits, head_commit_id, pid->id,
6386 got_worktree_get_base_commit_id(worktree),
6387 got_worktree_get_path_prefix(worktree),
6388 GOT_ERR_HISTEDIT_PATH, repo);
6389 got_object_commit_close(commit);
6394 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6395 &base_commit_id, &fileindex, worktree, repo);
6399 if (edit_script_path) {
6400 error = histedit_load_list(&histedit_cmds,
6401 edit_script_path, repo);
6403 got_worktree_histedit_abort(worktree, fileindex,
6404 repo, branch, base_commit_id,
6405 update_progress, &did_something);
6409 error = histedit_edit_script(&histedit_cmds, &commits,
6412 got_worktree_histedit_abort(worktree, fileindex,
6413 repo, branch, base_commit_id,
6414 update_progress, &did_something);
6420 error = histedit_save_list(&histedit_cmds, worktree,
6423 got_worktree_histedit_abort(worktree, fileindex,
6424 repo, branch, base_commit_id,
6425 update_progress, &did_something);
6431 error = histedit_check_script(&histedit_cmds, &commits, repo);
6435 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6436 if (resume_commit_id) {
6437 if (got_object_id_cmp(hle->commit_id,
6438 resume_commit_id) != 0)
6441 resume_commit_id = NULL;
6442 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6443 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6444 error = histedit_skip_commit(hle, worktree,
6447 error = histedit_commit(NULL, worktree,
6448 fileindex, tmp_branch, hle, repo);
6455 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6456 error = histedit_skip_commit(hle, worktree, repo);
6462 error = got_object_open_as_commit(&commit, repo,
6466 parent_ids = got_object_commit_get_parent_ids(commit);
6467 pid = SIMPLEQ_FIRST(parent_ids);
6469 error = got_worktree_histedit_merge_files(&merged_paths,
6470 worktree, fileindex, pid->id, hle->commit_id, repo,
6471 rebase_progress, &rebase_status, check_cancelled, NULL);
6474 got_object_commit_close(commit);
6477 if (rebase_status == GOT_STATUS_CONFLICT) {
6478 got_worktree_rebase_pathlist_free(&merged_paths);
6482 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6484 error = got_object_id_str(&id_str, hle->commit_id);
6487 printf("Stopping histedit for amending commit %s\n",
6490 got_worktree_rebase_pathlist_free(&merged_paths);
6491 error = got_worktree_histedit_postpone(worktree,
6496 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6497 error = histedit_skip_commit(hle, worktree, repo);
6503 error = histedit_commit(&merged_paths, worktree, fileindex,
6504 tmp_branch, hle, repo);
6505 got_worktree_rebase_pathlist_free(&merged_paths);
6510 if (rebase_status == GOT_STATUS_CONFLICT) {
6511 error = got_worktree_histedit_postpone(worktree, fileindex);
6514 error = got_error_msg(GOT_ERR_CONFLICTS,
6515 "conflicts must be resolved before rebasing can continue");
6517 error = histedit_complete(worktree, fileindex, tmp_branch,
6520 got_object_id_queue_free(&commits);
6521 histedit_free_list(&histedit_cmds);
6522 free(head_commit_id);
6523 free(base_commit_id);
6524 free(resume_commit_id);
6526 got_object_commit_close(commit);
6528 got_ref_close(branch);
6530 got_ref_close(tmp_branch);
6532 got_worktree_close(worktree);
6534 got_repo_close(repo);
6539 usage_integrate(void)
6541 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6545 static const struct got_error *
6546 cmd_integrate(int argc, char *argv[])
6548 const struct got_error *error = NULL;
6549 struct got_repository *repo = NULL;
6550 struct got_worktree *worktree = NULL;
6551 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6552 const char *branch_arg = NULL;
6553 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6554 struct got_fileindex *fileindex = NULL;
6555 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6556 int ch, did_something = 0;
6558 while ((ch = getopt(argc, argv, "")) != -1) {
6571 branch_arg = argv[0];
6573 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6574 "unveil", NULL) == -1)
6577 cwd = getcwd(NULL, 0);
6579 error = got_error_from_errno("getcwd");
6583 error = got_worktree_open(&worktree, cwd);
6587 error = check_rebase_or_histedit_in_progress(worktree);
6591 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6596 error = apply_unveil(got_repo_get_path(repo), 0,
6597 got_worktree_get_root_path(worktree));
6601 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6602 error = got_error_from_errno("asprintf");
6606 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6607 &base_branch_ref, worktree, refname, repo);
6611 refname = strdup(got_ref_get_name(branch_ref));
6612 if (refname == NULL) {
6613 error = got_error_from_errno("strdup");
6614 got_worktree_integrate_abort(worktree, fileindex, repo,
6615 branch_ref, base_branch_ref);
6618 base_refname = strdup(got_ref_get_name(base_branch_ref));
6619 if (base_refname == NULL) {
6620 error = got_error_from_errno("strdup");
6621 got_worktree_integrate_abort(worktree, fileindex, repo,
6622 branch_ref, base_branch_ref);
6626 error = got_ref_resolve(&commit_id, repo, branch_ref);
6630 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6634 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6635 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6636 "specified branch has already been integrated");
6637 got_worktree_integrate_abort(worktree, fileindex, repo,
6638 branch_ref, base_branch_ref);
6642 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6644 if (error->code == GOT_ERR_ANCESTRY)
6645 error = got_error(GOT_ERR_REBASE_REQUIRED);
6646 got_worktree_integrate_abort(worktree, fileindex, repo,
6647 branch_ref, base_branch_ref);
6651 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6652 branch_ref, base_branch_ref, update_progress, &did_something,
6653 check_cancelled, NULL);
6657 printf("Integrated %s into %s\n", refname, base_refname);
6660 got_repo_close(repo);
6662 got_worktree_close(worktree);
6664 free(base_commit_id);
6674 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6675 "[file-path ...]\n",
6680 static const struct got_error *
6681 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6682 const char *path, struct got_object_id *blob_id,
6683 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6685 const struct got_error *err = NULL;
6686 char *id_str = NULL;
6688 if (staged_status != GOT_STATUS_ADD &&
6689 staged_status != GOT_STATUS_MODIFY &&
6690 staged_status != GOT_STATUS_DELETE)
6693 if (staged_status == GOT_STATUS_ADD ||
6694 staged_status == GOT_STATUS_MODIFY)
6695 err = got_object_id_str(&id_str, staged_blob_id);
6697 err = got_object_id_str(&id_str, blob_id);
6701 printf("%s %c %s\n", id_str, staged_status, path);
6706 static const struct got_error *
6707 cmd_stage(int argc, char *argv[])
6709 const struct got_error *error = NULL;
6710 struct got_repository *repo = NULL;
6711 struct got_worktree *worktree = NULL;
6713 struct got_pathlist_head paths;
6714 struct got_pathlist_entry *pe;
6715 int ch, list_stage = 0, pflag = 0;
6716 FILE *patch_script_file = NULL;
6717 const char *patch_script_path = NULL;
6718 struct choose_patch_arg cpa;
6722 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6731 patch_script_path = optarg;
6743 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6744 "unveil", NULL) == -1)
6747 if (list_stage && (pflag || patch_script_path))
6748 errx(1, "-l option cannot be used with other options");
6749 if (patch_script_path && !pflag)
6750 errx(1, "-F option can only be used together with -p option");
6752 cwd = getcwd(NULL, 0);
6754 error = got_error_from_errno("getcwd");
6758 error = got_worktree_open(&worktree, cwd);
6762 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6767 if (patch_script_path) {
6768 patch_script_file = fopen(patch_script_path, "r");
6769 if (patch_script_file == NULL) {
6770 error = got_error_from_errno2("fopen",
6775 error = apply_unveil(got_repo_get_path(repo), 0,
6776 got_worktree_get_root_path(worktree));
6780 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6785 error = got_worktree_status(worktree, &paths, repo,
6786 print_stage, NULL, check_cancelled, NULL);
6788 cpa.patch_script_file = patch_script_file;
6789 cpa.action = "stage";
6790 error = got_worktree_stage(worktree, &paths,
6791 pflag ? NULL : print_status, NULL,
6792 pflag ? choose_patch : NULL, &cpa, repo);
6795 if (patch_script_file && fclose(patch_script_file) == EOF &&
6797 error = got_error_from_errno2("fclose", patch_script_path);
6799 got_repo_close(repo);
6801 got_worktree_close(worktree);
6802 TAILQ_FOREACH(pe, &paths, entry)
6803 free((char *)pe->path);
6804 got_pathlist_free(&paths);
6812 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6813 "[file-path ...]\n",
6819 static const struct got_error *
6820 cmd_unstage(int argc, char *argv[])
6822 const struct got_error *error = NULL;
6823 struct got_repository *repo = NULL;
6824 struct got_worktree *worktree = NULL;
6826 struct got_pathlist_head paths;
6827 struct got_pathlist_entry *pe;
6828 int ch, did_something = 0, pflag = 0;
6829 FILE *patch_script_file = NULL;
6830 const char *patch_script_path = NULL;
6831 struct choose_patch_arg cpa;
6835 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6841 patch_script_path = optarg;
6853 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6854 "unveil", NULL) == -1)
6857 if (patch_script_path && !pflag)
6858 errx(1, "-F option can only be used together with -p option");
6860 cwd = getcwd(NULL, 0);
6862 error = got_error_from_errno("getcwd");
6866 error = got_worktree_open(&worktree, cwd);
6870 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6875 if (patch_script_path) {
6876 patch_script_file = fopen(patch_script_path, "r");
6877 if (patch_script_file == NULL) {
6878 error = got_error_from_errno2("fopen",
6884 error = apply_unveil(got_repo_get_path(repo), 0,
6885 got_worktree_get_root_path(worktree));
6889 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6893 cpa.patch_script_file = patch_script_file;
6894 cpa.action = "unstage";
6895 error = got_worktree_unstage(worktree, &paths, update_progress,
6896 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6898 if (patch_script_file && fclose(patch_script_file) == EOF &&
6900 error = got_error_from_errno2("fclose", patch_script_path);
6902 got_repo_close(repo);
6904 got_worktree_close(worktree);
6905 TAILQ_FOREACH(pe, &paths, entry)
6906 free((char *)pe->path);
6907 got_pathlist_free(&paths);
6915 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6916 "arg1 [arg2 ...]\n", getprogname());
6920 static const struct got_error *
6921 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6923 const struct got_error *err;
6924 struct got_blob_object *blob;
6926 err = got_object_open_as_blob(&blob, repo, id, 8192);
6930 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6931 got_object_blob_close(blob);
6935 static const struct got_error *
6936 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6938 const struct got_error *err;
6939 struct got_tree_object *tree;
6940 const struct got_tree_entries *entries;
6941 struct got_tree_entry *te;
6943 err = got_object_open_as_tree(&tree, repo, id);
6947 entries = got_object_tree_get_entries(tree);
6948 te = SIMPLEQ_FIRST(&entries->head);
6951 if (sigint_received || sigpipe_received)
6953 err = got_object_id_str(&id_str, te->id);
6956 fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6958 te = SIMPLEQ_NEXT(te, entry);
6961 got_object_tree_close(tree);
6965 static const struct got_error *
6966 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6968 const struct got_error *err;
6969 struct got_commit_object *commit;
6970 const struct got_object_id_queue *parent_ids;
6971 struct got_object_qid *pid;
6972 char *id_str = NULL;
6973 const char *logmsg = NULL;
6975 err = got_object_open_as_commit(&commit, repo, id);
6979 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6983 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6984 parent_ids = got_object_commit_get_parent_ids(commit);
6985 fprintf(outfile, "numparents %d\n",
6986 got_object_commit_get_nparents(commit));
6987 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6989 err = got_object_id_str(&pid_str, pid->id);
6992 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6995 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6996 got_object_commit_get_author(commit),
6997 got_object_commit_get_author_time(commit));
6999 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7000 got_object_commit_get_author(commit),
7001 got_object_commit_get_committer_time(commit));
7003 logmsg = got_object_commit_get_logmsg_raw(commit);
7004 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7005 fprintf(outfile, "%s", logmsg);
7008 got_object_commit_close(commit);
7012 static const struct got_error *
7013 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7015 const struct got_error *err;
7016 struct got_tag_object *tag;
7017 char *id_str = NULL;
7018 const char *tagmsg = NULL;
7020 err = got_object_open_as_tag(&tag, repo, id);
7024 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7028 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7030 switch (got_object_tag_get_object_type(tag)) {
7031 case GOT_OBJ_TYPE_BLOB:
7032 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7033 GOT_OBJ_LABEL_BLOB);
7035 case GOT_OBJ_TYPE_TREE:
7036 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7037 GOT_OBJ_LABEL_TREE);
7039 case GOT_OBJ_TYPE_COMMIT:
7040 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7041 GOT_OBJ_LABEL_COMMIT);
7043 case GOT_OBJ_TYPE_TAG:
7044 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7051 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7052 got_object_tag_get_name(tag));
7054 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7055 got_object_tag_get_tagger(tag),
7056 got_object_tag_get_tagger_time(tag));
7058 tagmsg = got_object_tag_get_message(tag);
7059 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7060 fprintf(outfile, "%s", tagmsg);
7063 got_object_tag_close(tag);
7067 static const struct got_error *
7068 cmd_cat(int argc, char *argv[])
7070 const struct got_error *error;
7071 struct got_repository *repo = NULL;
7072 struct got_worktree *worktree = NULL;
7073 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7074 const char *commit_id_str = NULL;
7075 struct got_object_id *id = NULL, *commit_id = NULL;
7076 int ch, obj_type, i, force_path = 0;
7079 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7084 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7087 commit_id_str = optarg;
7090 repo_path = realpath(optarg, NULL);
7091 if (repo_path == NULL)
7092 return got_error_from_errno2("realpath",
7094 got_path_strip_trailing_slashes(repo_path);
7108 cwd = getcwd(NULL, 0);
7110 error = got_error_from_errno("getcwd");
7113 error = got_worktree_open(&worktree, cwd);
7114 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7117 if (repo_path == NULL) {
7119 got_worktree_get_repo_path(worktree));
7120 if (repo_path == NULL) {
7121 error = got_error_from_errno("strdup");
7127 if (repo_path == NULL) {
7128 repo_path = getcwd(NULL, 0);
7129 if (repo_path == NULL)
7130 return got_error_from_errno("getcwd");
7133 error = got_repo_open(&repo, repo_path, NULL);
7138 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7142 if (commit_id_str == NULL)
7143 commit_id_str = GOT_REF_HEAD;
7144 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7148 for (i = 0; i < argc; i++) {
7150 error = got_object_id_by_path(&id, repo, commit_id,
7155 error = match_object_id(&id, &label, argv[i],
7156 GOT_OBJ_TYPE_ANY, 0, repo);
7158 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7159 error->code != GOT_ERR_NOT_REF)
7161 error = got_object_id_by_path(&id, repo,
7162 commit_id, argv[i]);
7168 error = got_object_get_type(&obj_type, repo, id);
7173 case GOT_OBJ_TYPE_BLOB:
7174 error = cat_blob(id, repo, stdout);
7176 case GOT_OBJ_TYPE_TREE:
7177 error = cat_tree(id, repo, stdout);
7179 case GOT_OBJ_TYPE_COMMIT:
7180 error = cat_commit(id, repo, stdout);
7182 case GOT_OBJ_TYPE_TAG:
7183 error = cat_tag(id, repo, stdout);
7186 error = got_error(GOT_ERR_OBJ_TYPE);
7202 got_worktree_close(worktree);
7204 const struct got_error *repo_error;
7205 repo_error = got_repo_close(repo);