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);
658 * Don't let the user create a branch name with a leading '-'.
659 * While technically a valid reference name, this case is usually
660 * an unintended typo.
662 if (branch_name[0] == '-')
663 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
665 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
666 error = got_error_from_errno("asprintf");
670 error = got_ref_open(&branch_ref, repo, refname, 0);
672 if (error->code != GOT_ERR_NOT_REF)
675 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
676 "import target branch already exists");
680 path_dir = realpath(argv[0], NULL);
681 if (path_dir == NULL) {
682 error = got_error_from_errno2("realpath", argv[0]);
685 got_path_strip_trailing_slashes(path_dir);
688 * unveil(2) traverses exec(2); if an editor is used we have
689 * to apply unveil after the log message has been written.
691 if (logmsg == NULL || strlen(logmsg) == 0) {
692 error = get_editor(&editor);
696 error = collect_import_msg(&logmsg, &logmsg_path, editor,
699 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
706 if (unveil(path_dir, "r") != 0) {
707 error = got_error_from_errno2("unveil", path_dir);
713 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
720 error = got_repo_import(&new_commit_id, path_dir, logmsg,
721 author, &ignores, repo, import_progress, NULL);
728 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
735 error = got_ref_write(branch_ref, repo);
742 error = got_object_id_str(&id_str, new_commit_id);
749 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
751 if (error->code != GOT_ERR_NOT_REF) {
757 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
765 error = got_ref_write(head_ref, repo);
773 printf("Created branch %s with commit %s\n",
774 got_ref_get_name(branch_ref), id_str);
776 if (preserve_logmsg) {
777 fprintf(stderr, "%s: log message preserved in %s\n",
778 getprogname(), logmsg_path);
779 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
780 error = got_error_from_errno2("unlink", logmsg_path);
789 free(gitconfig_path);
791 got_ref_close(branch_ref);
793 got_ref_close(head_ref);
800 fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
801 "[-p prefix] repository-path [worktree-path]\n", getprogname());
805 static const struct got_error *
806 checkout_progress(void *arg, unsigned char status, const char *path)
808 char *worktree_path = arg;
810 /* Base commit bump happens silently. */
811 if (status == GOT_STATUS_BUMP_BASE)
814 while (path[0] == '/')
817 printf("%c %s/%s\n", status, worktree_path, path);
821 static const struct got_error *
822 check_cancelled(void *arg)
824 if (sigint_received || sigpipe_received)
825 return got_error(GOT_ERR_CANCELLED);
829 static const struct got_error *
830 check_linear_ancestry(struct got_object_id *commit_id,
831 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
832 struct got_repository *repo)
834 const struct got_error *err = NULL;
835 struct got_object_id *yca_id;
837 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
838 commit_id, base_commit_id, repo, check_cancelled, NULL);
843 return got_error(GOT_ERR_ANCESTRY);
846 * Require a straight line of history between the target commit
847 * and the work tree's base commit.
849 * Non-linear situations such as this require a rebase:
851 * (commit) D F (base_commit)
859 * 'got update' only handles linear cases:
860 * Update forwards in time: A (base/yca) - B - C - D (commit)
861 * Update backwards in time: D (base) - C - B - A (commit/yca)
863 if (allow_forwards_in_time_only) {
864 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
865 return got_error(GOT_ERR_ANCESTRY);
866 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
867 got_object_id_cmp(base_commit_id, yca_id) != 0)
868 return got_error(GOT_ERR_ANCESTRY);
874 static const struct got_error *
875 check_same_branch(struct got_object_id *commit_id,
876 struct got_reference *head_ref, struct got_object_id *yca_id,
877 struct got_repository *repo)
879 const struct got_error *err = NULL;
880 struct got_commit_graph *graph = NULL;
881 struct got_object_id *head_commit_id = NULL;
882 int is_same_branch = 0;
884 err = got_ref_resolve(&head_commit_id, repo, head_ref);
888 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
892 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
897 err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
901 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
902 check_cancelled, NULL);
907 struct got_object_id *id;
908 err = got_commit_graph_iter_next(&id, graph);
910 if (err->code == GOT_ERR_ITER_COMPLETED) {
913 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
915 err = got_commit_graph_fetch_commits(graph, 1,
916 repo, check_cancelled, NULL);
922 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
924 if (got_object_id_cmp(id, commit_id) == 0) {
932 got_commit_graph_close(graph);
933 free(head_commit_id);
934 if (!err && !is_same_branch)
935 err = got_error(GOT_ERR_ANCESTRY);
939 static const struct got_error *
940 resolve_commit_arg(struct got_object_id **commit_id,
941 const char *commit_id_arg, struct got_repository *repo)
943 const struct got_error *err;
944 struct got_reference *ref;
945 struct got_tag_object *tag;
947 err = got_repo_object_match_tag(&tag, commit_id_arg,
948 GOT_OBJ_TYPE_COMMIT, repo);
950 *commit_id = got_object_id_dup(
951 got_object_tag_get_object_id(tag));
952 if (*commit_id == NULL)
953 err = got_error_from_errno("got_object_id_dup");
954 got_object_tag_close(tag);
956 } else if (err->code != GOT_ERR_NO_OBJ)
959 err = got_ref_open(&ref, repo, commit_id_arg, 0);
961 err = got_ref_resolve(commit_id, repo, ref);
964 if (err->code != GOT_ERR_NOT_REF)
966 err = got_repo_match_object_id_prefix(commit_id,
967 commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
972 static const struct got_error *
973 cmd_checkout(int argc, char *argv[])
975 const struct got_error *error = NULL;
976 struct got_repository *repo = NULL;
977 struct got_reference *head_ref = NULL;
978 struct got_worktree *worktree = NULL;
979 char *repo_path = NULL;
980 char *worktree_path = NULL;
981 const char *path_prefix = "";
982 const char *branch_name = GOT_REF_HEAD;
983 char *commit_id_str = NULL;
984 int ch, same_path_prefix;
985 struct got_pathlist_head paths;
989 while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
992 branch_name = optarg;
995 commit_id_str = strdup(optarg);
996 if (commit_id_str == NULL)
997 return got_error_from_errno("strdup");
1000 path_prefix = optarg;
1012 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1013 "unveil", NULL) == -1)
1017 char *cwd, *base, *dotgit;
1018 repo_path = realpath(argv[0], NULL);
1019 if (repo_path == NULL)
1020 return got_error_from_errno2("realpath", argv[0]);
1021 cwd = getcwd(NULL, 0);
1023 error = got_error_from_errno("getcwd");
1026 if (path_prefix[0]) {
1027 base = basename(path_prefix);
1029 error = got_error_from_errno2("basename",
1034 base = basename(repo_path);
1036 error = got_error_from_errno2("basename",
1041 dotgit = strstr(base, ".git");
1044 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1045 error = got_error_from_errno("asprintf");
1050 } else if (argc == 2) {
1051 repo_path = realpath(argv[0], NULL);
1052 if (repo_path == NULL) {
1053 error = got_error_from_errno2("realpath", argv[0]);
1056 worktree_path = realpath(argv[1], NULL);
1057 if (worktree_path == NULL) {
1058 if (errno != ENOENT) {
1059 error = got_error_from_errno2("realpath",
1063 worktree_path = strdup(argv[1]);
1064 if (worktree_path == NULL) {
1065 error = got_error_from_errno("strdup");
1072 got_path_strip_trailing_slashes(repo_path);
1073 got_path_strip_trailing_slashes(worktree_path);
1075 error = got_repo_open(&repo, repo_path, NULL);
1079 /* Pre-create work tree path for unveil(2) */
1080 error = got_path_mkdir(worktree_path);
1082 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1083 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1085 if (!got_path_dir_is_empty(worktree_path)) {
1086 error = got_error_path(worktree_path,
1087 GOT_ERR_DIR_NOT_EMPTY);
1092 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1096 error = got_ref_open(&head_ref, repo, branch_name, 0);
1100 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1101 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1104 error = got_worktree_open(&worktree, worktree_path);
1108 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1112 if (!same_path_prefix) {
1113 error = got_error(GOT_ERR_PATH_PREFIX);
1117 if (commit_id_str) {
1118 struct got_object_id *commit_id;
1119 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1122 error = check_linear_ancestry(commit_id,
1123 got_worktree_get_base_commit_id(worktree), 0, repo);
1124 if (error != NULL) {
1128 error = check_same_branch(commit_id, head_ref, NULL, repo);
1131 error = got_worktree_set_base_commit_id(worktree, repo,
1138 error = got_pathlist_append(&paths, "", NULL);
1141 error = got_worktree_checkout_files(worktree, &paths, repo,
1142 checkout_progress, worktree_path, check_cancelled, NULL);
1146 printf("Now shut up and hack\n");
1149 got_pathlist_free(&paths);
1150 free(commit_id_str);
1152 free(worktree_path);
1159 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1164 static const struct got_error *
1165 update_progress(void *arg, unsigned char status, const char *path)
1167 int *did_something = arg;
1169 if (status == GOT_STATUS_EXISTS)
1174 /* Base commit bump happens silently. */
1175 if (status == GOT_STATUS_BUMP_BASE)
1178 while (path[0] == '/')
1180 printf("%c %s\n", status, path);
1184 static const struct got_error *
1185 switch_head_ref(struct got_reference *head_ref,
1186 struct got_object_id *commit_id, struct got_worktree *worktree,
1187 struct got_repository *repo)
1189 const struct got_error *err = NULL;
1191 int ref_has_moved = 0;
1193 /* Trivial case: switching between two different references. */
1194 if (strcmp(got_ref_get_name(head_ref),
1195 got_worktree_get_head_ref_name(worktree)) != 0) {
1196 printf("Switching work tree from %s to %s\n",
1197 got_worktree_get_head_ref_name(worktree),
1198 got_ref_get_name(head_ref));
1199 return got_worktree_set_head_ref(worktree, head_ref);
1202 err = check_linear_ancestry(commit_id,
1203 got_worktree_get_base_commit_id(worktree), 0, repo);
1205 if (err->code != GOT_ERR_ANCESTRY)
1212 /* Switching to a rebased branch with the same reference name. */
1213 err = got_object_id_str(&base_id_str,
1214 got_worktree_get_base_commit_id(worktree));
1217 printf("Reference %s now points at a different branch\n",
1218 got_worktree_get_head_ref_name(worktree));
1219 printf("Switching work tree from %s to %s\n", base_id_str,
1220 got_worktree_get_head_ref_name(worktree));
1224 static const struct got_error *
1225 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1227 const struct got_error *err;
1230 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1234 return got_error(GOT_ERR_REBASING);
1236 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1240 return got_error(GOT_ERR_HISTEDIT_BUSY);
1245 static const struct got_error *
1246 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1247 char *argv[], struct got_worktree *worktree)
1249 const struct got_error *err = NULL;
1256 return got_error_from_errno("strdup");
1257 return got_pathlist_append(paths, path, NULL);
1260 for (i = 0; i < argc; i++) {
1261 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1264 err = got_pathlist_append(paths, path, NULL);
1274 static const struct got_error *
1275 cmd_update(int argc, char *argv[])
1277 const struct got_error *error = NULL;
1278 struct got_repository *repo = NULL;
1279 struct got_worktree *worktree = NULL;
1280 char *worktree_path = NULL;
1281 struct got_object_id *commit_id = NULL;
1282 char *commit_id_str = NULL;
1283 const char *branch_name = NULL;
1284 struct got_reference *head_ref = NULL;
1285 struct got_pathlist_head paths;
1286 struct got_pathlist_entry *pe;
1287 int ch, did_something = 0;
1291 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1294 branch_name = optarg;
1297 commit_id_str = strdup(optarg);
1298 if (commit_id_str == NULL)
1299 return got_error_from_errno("strdup");
1311 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1312 "unveil", NULL) == -1)
1315 worktree_path = getcwd(NULL, 0);
1316 if (worktree_path == NULL) {
1317 error = got_error_from_errno("getcwd");
1320 error = got_worktree_open(&worktree, worktree_path);
1324 error = check_rebase_or_histedit_in_progress(worktree);
1328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1333 error = apply_unveil(got_repo_get_path(repo), 0,
1334 got_worktree_get_root_path(worktree));
1338 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1342 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1343 got_worktree_get_head_ref_name(worktree), 0);
1346 if (commit_id_str == NULL) {
1347 error = got_ref_resolve(&commit_id, repo, head_ref);
1350 error = got_object_id_str(&commit_id_str, commit_id);
1354 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1355 free(commit_id_str);
1356 commit_id_str = NULL;
1359 error = got_object_id_str(&commit_id_str, commit_id);
1365 struct got_object_id *head_commit_id;
1366 TAILQ_FOREACH(pe, &paths, entry) {
1367 if (pe->path_len == 0)
1369 error = got_error_msg(GOT_ERR_BAD_PATH,
1370 "switching between branches requires that "
1371 "the entire work tree gets updated");
1374 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1377 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1379 free(head_commit_id);
1382 error = check_same_branch(commit_id, head_ref, NULL, repo);
1385 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1389 error = check_linear_ancestry(commit_id,
1390 got_worktree_get_base_commit_id(worktree), 0, repo);
1391 if (error != NULL) {
1392 if (error->code == GOT_ERR_ANCESTRY)
1393 error = got_error(GOT_ERR_BRANCH_MOVED);
1396 error = check_same_branch(commit_id, head_ref, NULL, repo);
1401 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1403 error = got_worktree_set_base_commit_id(worktree, repo,
1409 error = got_worktree_checkout_files(worktree, &paths, repo,
1410 update_progress, &did_something, check_cancelled, NULL);
1415 printf("Updated to commit %s\n", commit_id_str);
1417 printf("Already up-to-date\n");
1419 free(worktree_path);
1420 TAILQ_FOREACH(pe, &paths, entry)
1421 free((char *)pe->path);
1422 got_pathlist_free(&paths);
1424 free(commit_id_str);
1428 static const struct got_error *
1429 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1430 const char *path, int diff_context, int ignore_whitespace,
1431 struct got_repository *repo)
1433 const struct got_error *err = NULL;
1434 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1437 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1442 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1446 while (path[0] == '/')
1448 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1449 ignore_whitespace, stdout);
1452 got_object_blob_close(blob1);
1453 got_object_blob_close(blob2);
1457 static const struct got_error *
1458 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1459 const char *path, int diff_context, int ignore_whitespace,
1460 struct got_repository *repo)
1462 const struct got_error *err = NULL;
1463 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1464 struct got_diff_blob_output_unidiff_arg arg;
1467 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1472 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1476 arg.diff_context = diff_context;
1477 arg.ignore_whitespace = ignore_whitespace;
1478 arg.outfile = stdout;
1479 while (path[0] == '/')
1481 err = got_diff_tree(tree1, tree2, path, path, repo,
1482 got_diff_blob_output_unidiff, &arg, 1);
1485 got_object_tree_close(tree1);
1487 got_object_tree_close(tree2);
1491 static const struct got_error *
1492 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1493 const char *path, int diff_context, struct got_repository *repo)
1495 const struct got_error *err = NULL;
1496 struct got_commit_object *pcommit = NULL;
1497 char *id_str1 = NULL, *id_str2 = NULL;
1498 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1499 struct got_object_qid *qid;
1501 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1503 err = got_object_open_as_commit(&pcommit, repo,
1509 if (path && path[0] != '\0') {
1511 err = got_object_id_by_path(&obj_id2, repo, id, path);
1514 err = got_object_id_str(&id_str2, obj_id2);
1520 err = got_object_id_by_path(&obj_id1, repo,
1526 err = got_object_id_str(&id_str1, obj_id1);
1532 err = got_object_get_type(&obj_type, repo, obj_id2);
1537 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1539 case GOT_OBJ_TYPE_BLOB:
1540 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1543 case GOT_OBJ_TYPE_TREE:
1544 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1548 err = got_error(GOT_ERR_OBJ_TYPE);
1554 obj_id2 = got_object_commit_get_tree_id(commit);
1555 err = got_object_id_str(&id_str2, obj_id2);
1558 obj_id1 = got_object_commit_get_tree_id(pcommit);
1559 err = got_object_id_str(&id_str1, obj_id1);
1562 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1563 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1570 got_object_commit_close(pcommit);
1575 get_datestr(time_t *time, char *datebuf)
1577 struct tm mytm, *tm;
1580 tm = gmtime_r(time, &mytm);
1583 s = asctime_r(tm, datebuf);
1586 p = strchr(s, '\n');
1592 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1594 static const struct got_error *
1595 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1596 struct got_repository *repo, const char *path, int show_patch,
1597 int diff_context, struct got_reflist_head *refs)
1599 const struct got_error *err = NULL;
1600 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1602 time_t committer_time;
1603 const char *author, *committer;
1604 char *refs_str = NULL;
1605 struct got_reflist_entry *re;
1607 SIMPLEQ_FOREACH(re, refs, entry) {
1610 struct got_tag_object *tag = NULL;
1613 name = got_ref_get_name(re->ref);
1614 if (strcmp(name, GOT_REF_HEAD) == 0)
1616 if (strncmp(name, "refs/", 5) == 0)
1618 if (strncmp(name, "got/", 4) == 0)
1620 if (strncmp(name, "heads/", 6) == 0)
1622 if (strncmp(name, "remotes/", 8) == 0)
1624 if (strncmp(name, "tags/", 5) == 0) {
1625 err = got_object_open_as_tag(&tag, repo, re->id);
1627 if (err->code != GOT_ERR_OBJ_TYPE)
1629 /* Ref points at something other than a tag. */
1634 cmp = got_object_id_cmp(tag ?
1635 got_object_tag_get_object_id(tag) : re->id, id);
1637 got_object_tag_close(tag);
1641 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1643 err = got_error_from_errno("asprintf");
1649 err = got_object_id_str(&id_str, id);
1653 printf(GOT_COMMIT_SEP_STR);
1654 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1655 refs_str ? refs_str : "", refs_str ? ")" : "");
1660 printf("from: %s\n", got_object_commit_get_author(commit));
1661 committer_time = got_object_commit_get_committer_time(commit);
1662 datestr = get_datestr(&committer_time, datebuf);
1664 printf("date: %s UTC\n", datestr);
1665 author = got_object_commit_get_author(commit);
1666 committer = got_object_commit_get_committer(commit);
1667 if (strcmp(author, committer) != 0)
1668 printf("via: %s\n", committer);
1669 if (got_object_commit_get_nparents(commit) > 1) {
1670 const struct got_object_id_queue *parent_ids;
1671 struct got_object_qid *qid;
1673 parent_ids = got_object_commit_get_parent_ids(commit);
1674 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1675 err = got_object_id_str(&id_str, qid->id);
1678 printf("parent %d: %s\n", n++, id_str);
1683 err = got_object_commit_get_logmsg(&logmsg0, commit);
1689 line = strsep(&logmsg, "\n");
1691 printf(" %s\n", line);
1696 err = print_patch(commit, id, path, diff_context, repo);
1701 if (fflush(stdout) != 0 && err == NULL)
1702 err = got_error_from_errno("fflush");
1706 static const struct got_error *
1707 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1708 char *path, int show_patch, int diff_context, int limit,
1709 int first_parent_traversal, struct got_reflist_head *refs)
1711 const struct got_error *err;
1712 struct got_commit_graph *graph;
1714 err = got_commit_graph_open(&graph, root_id, path,
1715 first_parent_traversal, repo);
1718 err = got_commit_graph_iter_start(graph, root_id, repo,
1719 check_cancelled, NULL);
1723 struct got_commit_object *commit;
1724 struct got_object_id *id;
1726 if (sigint_received || sigpipe_received)
1729 err = got_commit_graph_iter_next(&id, graph);
1731 if (err->code == GOT_ERR_ITER_COMPLETED) {
1735 if (err->code != GOT_ERR_ITER_NEED_MORE)
1737 err = got_commit_graph_fetch_commits(graph, 1, repo,
1738 check_cancelled, NULL);
1747 err = got_object_open_as_commit(&commit, repo, id);
1750 err = print_commit(commit, id, repo, path, show_patch,
1751 diff_context, refs);
1752 got_object_commit_close(commit);
1753 if (err || (limit && --limit == 0))
1757 got_commit_graph_close(graph);
1764 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1765 "[-r repository-path] [path]\n", getprogname());
1770 get_default_log_limit(void)
1772 const char *got_default_log_limit;
1776 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1777 if (got_default_log_limit == NULL)
1779 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1785 static const struct got_error *
1786 cmd_log(int argc, char *argv[])
1788 const struct got_error *error;
1789 struct got_repository *repo = NULL;
1790 struct got_worktree *worktree = NULL;
1791 struct got_commit_object *commit = NULL;
1792 struct got_object_id *id = NULL;
1793 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1794 char *start_commit = NULL;
1795 int diff_context = 3, ch;
1796 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1798 struct got_reflist_head refs;
1800 SIMPLEQ_INIT(&refs);
1803 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1809 limit = get_default_log_limit();
1811 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1817 start_commit = optarg;
1820 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1823 err(1, "-C option %s", errstr);
1826 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1828 err(1, "-l option %s", errstr);
1831 first_parent_traversal = 1;
1834 repo_path = realpath(optarg, NULL);
1835 if (repo_path == NULL)
1836 return got_error_from_errno2("realpath",
1838 got_path_strip_trailing_slashes(repo_path);
1849 cwd = getcwd(NULL, 0);
1851 error = got_error_from_errno("getcwd");
1855 error = got_worktree_open(&worktree, cwd);
1856 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1863 error = got_error_from_errno("strdup");
1866 } else if (argc == 1) {
1868 error = got_worktree_resolve_path(&path, worktree,
1873 path = strdup(argv[0]);
1875 error = got_error_from_errno("strdup");
1882 if (repo_path == NULL) {
1883 repo_path = worktree ?
1884 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1886 if (repo_path == NULL) {
1887 error = got_error_from_errno("strdup");
1891 error = got_repo_open(&repo, repo_path, NULL);
1895 error = apply_unveil(got_repo_get_path(repo), 1,
1896 worktree ? got_worktree_get_root_path(worktree) : NULL);
1900 if (start_commit == NULL) {
1901 struct got_reference *head_ref;
1902 error = got_ref_open(&head_ref, repo,
1903 worktree ? got_worktree_get_head_ref_name(worktree)
1907 error = got_ref_resolve(&id, repo, head_ref);
1908 got_ref_close(head_ref);
1911 error = got_object_open_as_commit(&commit, repo, id);
1913 struct got_reference *ref;
1914 error = got_ref_open(&ref, repo, start_commit, 0);
1915 if (error == NULL) {
1917 error = got_ref_resolve(&id, repo, ref);
1921 error = got_object_get_type(&obj_type, repo, id);
1924 if (obj_type == GOT_OBJ_TYPE_TAG) {
1925 struct got_tag_object *tag;
1926 error = got_object_open_as_tag(&tag, repo, id);
1929 if (got_object_tag_get_object_type(tag) !=
1930 GOT_OBJ_TYPE_COMMIT) {
1931 got_object_tag_close(tag);
1932 error = got_error(GOT_ERR_OBJ_TYPE);
1936 id = got_object_id_dup(
1937 got_object_tag_get_object_id(tag));
1939 error = got_error_from_errno(
1940 "got_object_id_dup");
1941 got_object_tag_close(tag);
1944 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1945 error = got_error(GOT_ERR_OBJ_TYPE);
1948 error = got_object_open_as_commit(&commit, repo, id);
1952 if (commit == NULL) {
1953 error = got_repo_match_object_id_prefix(&id,
1954 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1963 const char *prefix = got_worktree_get_path_prefix(worktree);
1965 if (asprintf(&p, "%s%s%s", prefix,
1966 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1967 error = got_error_from_errno("asprintf");
1970 error = got_repo_map_path(&in_repo_path, repo, p, 1);
1973 error = got_repo_map_path(&in_repo_path, repo, path, 1);
1978 path = in_repo_path;
1981 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1985 error = print_commits(id, repo, path, show_patch,
1986 diff_context, limit, first_parent_traversal, &refs);
1993 got_worktree_close(worktree);
1995 const struct got_error *repo_error;
1996 repo_error = got_repo_close(repo);
2000 got_ref_list_free(&refs);
2007 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2008 "[-w] [object1 object2 | path]\n", getprogname());
2012 struct print_diff_arg {
2013 struct got_repository *repo;
2014 struct got_worktree *worktree;
2019 int ignore_whitespace;
2022 static const struct got_error *
2023 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2024 const char *path, struct got_object_id *blob_id,
2025 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2027 struct print_diff_arg *a = arg;
2028 const struct got_error *err = NULL;
2029 struct got_blob_object *blob1 = NULL;
2031 char *abspath = NULL, *label1 = NULL;
2034 if (a->diff_staged) {
2035 if (staged_status != GOT_STATUS_MODIFY &&
2036 staged_status != GOT_STATUS_ADD &&
2037 staged_status != GOT_STATUS_DELETE)
2040 if (staged_status == GOT_STATUS_DELETE)
2042 if (status == GOT_STATUS_NONEXISTENT)
2043 return got_error_set_errno(ENOENT, path);
2044 if (status != GOT_STATUS_MODIFY &&
2045 status != GOT_STATUS_ADD &&
2046 status != GOT_STATUS_DELETE &&
2047 status != GOT_STATUS_CONFLICT)
2051 if (!a->header_shown) {
2052 printf("diff %s %s%s\n", a->id_str,
2053 got_worktree_get_root_path(a->worktree),
2054 a->diff_staged ? " (staged changes)" : "");
2055 a->header_shown = 1;
2058 if (a->diff_staged) {
2059 const char *label1 = NULL, *label2 = NULL;
2060 switch (staged_status) {
2061 case GOT_STATUS_MODIFY:
2065 case GOT_STATUS_ADD:
2068 case GOT_STATUS_DELETE:
2072 return got_error(GOT_ERR_FILE_STATUS);
2074 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2075 label1, label2, a->diff_context, a->ignore_whitespace,
2079 if (staged_status == GOT_STATUS_ADD ||
2080 staged_status == GOT_STATUS_MODIFY) {
2082 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2086 err = got_object_id_str(&id_str, staged_blob_id);
2089 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2090 err = got_error_from_errno("asprintf");
2095 } else if (status != GOT_STATUS_ADD) {
2096 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2101 if (status != GOT_STATUS_DELETE) {
2102 if (asprintf(&abspath, "%s/%s",
2103 got_worktree_get_root_path(a->worktree), path) == -1) {
2104 err = got_error_from_errno("asprintf");
2108 f2 = fopen(abspath, "r");
2110 err = got_error_from_errno2("fopen", abspath);
2113 if (lstat(abspath, &sb) == -1) {
2114 err = got_error_from_errno2("lstat", abspath);
2120 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2121 a->diff_context, a->ignore_whitespace, stdout);
2124 got_object_blob_close(blob1);
2125 if (f2 && fclose(f2) != 0 && err == NULL)
2126 err = got_error_from_errno("fclose");
2131 static const struct got_error *
2132 match_object_id(struct got_object_id **id, char **label,
2133 const char *id_str, int obj_type, int resolve_tags,
2134 struct got_repository *repo)
2136 const struct got_error *err;
2137 struct got_tag_object *tag;
2138 struct got_reference *ref = NULL;
2144 err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2147 *id = got_object_id_dup(
2148 got_object_tag_get_object_id(tag));
2150 err = got_error_from_errno("got_object_id_dup");
2151 else if (asprintf(label, "refs/tags/%s",
2152 got_object_tag_get_name(tag)) == -1) {
2153 err = got_error_from_errno("asprintf");
2157 got_object_tag_close(tag);
2159 } else if (err->code != GOT_ERR_NO_OBJ)
2163 err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2165 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2167 err = got_ref_open(&ref, repo, id_str, 0);
2170 *label = strdup(got_ref_get_name(ref));
2171 if (*label == NULL) {
2172 err = got_error_from_errno("strdup");
2175 err = got_ref_resolve(id, repo, ref);
2177 err = got_object_id_str(label, *id);
2178 if (*label == NULL) {
2179 err = got_error_from_errno("strdup");
2190 static const struct got_error *
2191 cmd_diff(int argc, char *argv[])
2193 const struct got_error *error;
2194 struct got_repository *repo = NULL;
2195 struct got_worktree *worktree = NULL;
2196 char *cwd = NULL, *repo_path = NULL;
2197 struct got_object_id *id1 = NULL, *id2 = NULL;
2198 const char *id_str1 = NULL, *id_str2 = NULL;
2199 char *label1 = NULL, *label2 = NULL;
2201 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2206 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2211 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2214 diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2216 err(1, "-C option %s", errstr);
2219 repo_path = realpath(optarg, NULL);
2220 if (repo_path == NULL)
2221 return got_error_from_errno2("realpath",
2223 got_path_strip_trailing_slashes(repo_path);
2229 ignore_whitespace = 1;
2240 cwd = getcwd(NULL, 0);
2242 error = got_error_from_errno("getcwd");
2245 error = got_worktree_open(&worktree, cwd);
2246 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2249 if (worktree == NULL) {
2250 error = got_error(GOT_ERR_NOT_WORKTREE);
2255 "-r option can't be used when diffing a work tree");
2256 repo_path = strdup(got_worktree_get_repo_path(worktree));
2257 if (repo_path == NULL) {
2258 error = got_error_from_errno("strdup");
2262 error = got_worktree_resolve_path(&path, worktree,
2269 error = got_error_from_errno("strdup");
2273 } else if (argc == 2) {
2275 errx(1, "-s option can't be used when diffing "
2276 "objects in repository");
2279 if (worktree && repo_path == NULL) {
2281 strdup(got_worktree_get_repo_path(worktree));
2282 if (repo_path == NULL) {
2283 error = got_error_from_errno("strdup");
2290 if (repo_path == NULL) {
2291 repo_path = getcwd(NULL, 0);
2292 if (repo_path == NULL)
2293 return got_error_from_errno("getcwd");
2296 error = got_repo_open(&repo, repo_path, NULL);
2301 error = apply_unveil(got_repo_get_path(repo), 1,
2302 worktree ? got_worktree_get_root_path(worktree) : NULL);
2307 struct print_diff_arg arg;
2308 struct got_pathlist_head paths;
2313 error = got_object_id_str(&id_str,
2314 got_worktree_get_base_commit_id(worktree));
2318 arg.worktree = worktree;
2319 arg.diff_context = diff_context;
2320 arg.id_str = id_str;
2321 arg.header_shown = 0;
2322 arg.diff_staged = diff_staged;
2323 arg.ignore_whitespace = ignore_whitespace;
2325 error = got_pathlist_append(&paths, path, NULL);
2329 error = got_worktree_status(worktree, &paths, repo, print_diff,
2330 &arg, check_cancelled, NULL);
2332 got_pathlist_free(&paths);
2336 error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2341 error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2346 error = got_object_get_type(&type1, repo, id1);
2350 error = got_object_get_type(&type2, repo, id2);
2354 if (type1 != type2) {
2355 error = got_error(GOT_ERR_OBJ_TYPE);
2360 case GOT_OBJ_TYPE_BLOB:
2361 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2362 diff_context, ignore_whitespace, repo, stdout);
2364 case GOT_OBJ_TYPE_TREE:
2365 error = got_diff_objects_as_trees(id1, id2, "", "",
2366 diff_context, ignore_whitespace, repo, stdout);
2368 case GOT_OBJ_TYPE_COMMIT:
2369 printf("diff %s %s\n", label1, label2);
2370 error = got_diff_objects_as_commits(id1, id2, diff_context,
2371 ignore_whitespace, repo, stdout);
2374 error = got_error(GOT_ERR_OBJ_TYPE);
2384 got_worktree_close(worktree);
2386 const struct got_error *repo_error;
2387 repo_error = got_repo_close(repo);
2398 "usage: %s blame [-c commit] [-r repository-path] path\n",
2407 char datebuf[11]; /* YYYY-MM-DD + NUL */
2410 struct blame_cb_args {
2411 struct blame_line *lines;
2415 off_t *line_offsets;
2417 struct got_repository *repo;
2420 static const struct got_error *
2421 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2423 const struct got_error *err = NULL;
2424 struct blame_cb_args *a = arg;
2425 struct blame_line *bline;
2427 size_t linesize = 0;
2428 struct got_commit_object *commit = NULL;
2431 time_t committer_time;
2433 if (nlines != a->nlines ||
2434 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2435 return got_error(GOT_ERR_RANGE);
2437 if (sigint_received)
2438 return got_error(GOT_ERR_ITER_COMPLETED);
2441 return NULL; /* no change in this commit */
2443 /* Annotate this line. */
2444 bline = &a->lines[lineno - 1];
2445 if (bline->annotated)
2447 err = got_object_id_str(&bline->id_str, id);
2451 err = got_object_open_as_commit(&commit, a->repo, id);
2455 bline->committer = strdup(got_object_commit_get_committer(commit));
2456 if (bline->committer == NULL) {
2457 err = got_error_from_errno("strdup");
2461 committer_time = got_object_commit_get_committer_time(commit);
2462 if (localtime_r(&committer_time, &tm) == NULL)
2463 return got_error_from_errno("localtime_r");
2464 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2465 &tm) >= sizeof(bline->datebuf)) {
2466 err = got_error(GOT_ERR_NO_SPACE);
2469 bline->annotated = 1;
2471 /* Print lines annotated so far. */
2472 bline = &a->lines[a->lineno_cur - 1];
2473 if (!bline->annotated)
2476 offset = a->line_offsets[a->lineno_cur - 1];
2477 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2478 err = got_error_from_errno("fseeko");
2482 while (bline->annotated) {
2483 char *smallerthan, *at, *nl, *committer;
2486 if (getline(&line, &linesize, a->f) == -1) {
2488 err = got_error_from_errno("getline");
2492 committer = bline->committer;
2493 smallerthan = strchr(committer, '<');
2494 if (smallerthan && smallerthan[1] != '\0')
2495 committer = smallerthan + 1;
2496 at = strchr(committer, '@');
2499 len = strlen(committer);
2501 committer[8] = '\0';
2503 nl = strchr(line, '\n');
2506 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2507 bline->id_str, bline->datebuf, committer, line);
2510 bline = &a->lines[a->lineno_cur - 1];
2514 got_object_commit_close(commit);
2519 static const struct got_error *
2520 cmd_blame(int argc, char *argv[])
2522 const struct got_error *error;
2523 struct got_repository *repo = NULL;
2524 struct got_worktree *worktree = NULL;
2525 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2526 struct got_object_id *obj_id = NULL;
2527 struct got_object_id *commit_id = NULL;
2528 struct got_blob_object *blob = NULL;
2529 char *commit_id_str = NULL;
2530 struct blame_cb_args bca;
2531 int ch, obj_type, i;
2534 memset(&bca, 0, sizeof(bca));
2537 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2542 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2545 commit_id_str = optarg;
2548 repo_path = realpath(optarg, NULL);
2549 if (repo_path == NULL)
2550 return got_error_from_errno2("realpath",
2552 got_path_strip_trailing_slashes(repo_path);
2568 cwd = getcwd(NULL, 0);
2570 error = got_error_from_errno("getcwd");
2573 if (repo_path == NULL) {
2574 error = got_worktree_open(&worktree, cwd);
2575 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2581 strdup(got_worktree_get_repo_path(worktree));
2582 if (repo_path == NULL)
2583 error = got_error_from_errno("strdup");
2587 repo_path = strdup(cwd);
2588 if (repo_path == NULL) {
2589 error = got_error_from_errno("strdup");
2595 error = got_repo_open(&repo, repo_path, NULL);
2599 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2604 const char *prefix = got_worktree_get_path_prefix(worktree);
2605 char *p, *worktree_subdir = cwd +
2606 strlen(got_worktree_get_root_path(worktree));
2607 if (asprintf(&p, "%s%s%s%s%s",
2608 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2609 worktree_subdir, worktree_subdir[0] ? "/" : "",
2611 error = got_error_from_errno("asprintf");
2614 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2617 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2622 if (commit_id_str == NULL) {
2623 struct got_reference *head_ref;
2624 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2627 error = got_ref_resolve(&commit_id, repo, head_ref);
2628 got_ref_close(head_ref);
2632 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2637 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2640 if (obj_id == NULL) {
2641 error = got_error(GOT_ERR_NO_OBJ);
2645 error = got_object_get_type(&obj_type, repo, obj_id);
2649 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2650 error = got_error(GOT_ERR_OBJ_TYPE);
2654 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2657 bca.f = got_opentemp();
2658 if (bca.f == NULL) {
2659 error = got_error_from_errno("got_opentemp");
2662 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2663 &bca.line_offsets, bca.f, blob);
2664 if (error || bca.nlines == 0)
2667 /* Don't include \n at EOF in the blame line count. */
2668 if (bca.line_offsets[bca.nlines - 1] == filesize)
2671 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2672 if (bca.lines == NULL) {
2673 error = got_error_from_errno("calloc");
2677 bca.nlines_prec = 0;
2685 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2686 check_cancelled, NULL);
2696 got_object_blob_close(blob);
2698 got_worktree_close(worktree);
2700 const struct got_error *repo_error;
2701 repo_error = got_repo_close(repo);
2706 for (i = 0; i < bca.nlines; i++) {
2707 struct blame_line *bline = &bca.lines[i];
2708 free(bline->id_str);
2709 free(bline->committer);
2713 free(bca.line_offsets);
2714 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2715 error = got_error_from_errno("fclose");
2723 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2729 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2730 const char *root_path)
2732 int is_root_path = (strcmp(path, root_path) == 0);
2733 const char *modestr = "";
2734 mode_t mode = got_tree_entry_get_mode(te);
2736 path += strlen(root_path);
2737 while (path[0] == '/')
2740 if (got_object_tree_entry_is_submodule(te))
2742 else if (S_ISLNK(mode))
2744 else if (S_ISDIR(mode))
2746 else if (mode & S_IXUSR)
2749 printf("%s%s%s%s%s\n", id ? id : "", path,
2750 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2753 static const struct got_error *
2754 print_tree(const char *path, struct got_object_id *commit_id,
2755 int show_ids, int recurse, const char *root_path,
2756 struct got_repository *repo)
2758 const struct got_error *err = NULL;
2759 struct got_object_id *tree_id = NULL;
2760 struct got_tree_object *tree = NULL;
2763 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2767 err = got_object_open_as_tree(&tree, repo, tree_id);
2770 nentries = got_object_tree_get_nentries(tree);
2771 for (i = 0; i < nentries; i++) {
2772 struct got_tree_entry *te;
2775 if (sigint_received || sigpipe_received)
2778 te = got_object_tree_get_entry(tree, i);
2781 err = got_object_id_str(&id_str,
2782 got_tree_entry_get_id(te));
2785 if (asprintf(&id, "%s ", id_str) == -1) {
2786 err = got_error_from_errno("asprintf");
2792 print_entry(te, id, path, root_path);
2795 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2797 if (asprintf(&child_path, "%s%s%s", path,
2798 path[0] == '/' && path[1] == '\0' ? "" : "/",
2799 got_tree_entry_get_name(te)) == -1) {
2800 err = got_error_from_errno("asprintf");
2803 err = print_tree(child_path, commit_id, show_ids, 1,
2812 got_object_tree_close(tree);
2817 static const struct got_error *
2818 cmd_tree(int argc, char *argv[])
2820 const struct got_error *error;
2821 struct got_repository *repo = NULL;
2822 struct got_worktree *worktree = NULL;
2824 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2825 struct got_object_id *commit_id = NULL;
2826 char *commit_id_str = NULL;
2827 int show_ids = 0, recurse = 0;
2831 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2836 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2839 commit_id_str = optarg;
2842 repo_path = realpath(optarg, NULL);
2843 if (repo_path == NULL)
2844 return got_error_from_errno2("realpath",
2846 got_path_strip_trailing_slashes(repo_path);
2870 cwd = getcwd(NULL, 0);
2872 error = got_error_from_errno("getcwd");
2875 if (repo_path == NULL) {
2876 error = got_worktree_open(&worktree, cwd);
2877 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2883 strdup(got_worktree_get_repo_path(worktree));
2884 if (repo_path == NULL)
2885 error = got_error_from_errno("strdup");
2889 repo_path = strdup(cwd);
2890 if (repo_path == NULL) {
2891 error = got_error_from_errno("strdup");
2897 error = got_repo_open(&repo, repo_path, NULL);
2901 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2907 char *p, *worktree_subdir = cwd +
2908 strlen(got_worktree_get_root_path(worktree));
2909 if (asprintf(&p, "%s/%s",
2910 got_worktree_get_path_prefix(worktree),
2911 worktree_subdir) == -1) {
2912 error = got_error_from_errno("asprintf");
2915 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2922 if (in_repo_path == NULL) {
2923 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2928 if (commit_id_str == NULL) {
2929 struct got_reference *head_ref;
2930 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2933 error = got_ref_resolve(&commit_id, repo, head_ref);
2934 got_ref_close(head_ref);
2938 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2943 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2944 in_repo_path, repo);
2951 got_worktree_close(worktree);
2953 const struct got_error *repo_error;
2954 repo_error = got_repo_close(repo);
2964 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2968 static const struct got_error *
2969 print_status(void *arg, unsigned char status, unsigned char staged_status,
2970 const char *path, struct got_object_id *blob_id,
2971 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2973 if (status == staged_status && (status == GOT_STATUS_DELETE))
2974 status = GOT_STATUS_NO_CHANGE;
2975 printf("%c%c %s\n", status, staged_status, path);
2979 static const struct got_error *
2980 cmd_status(int argc, char *argv[])
2982 const struct got_error *error = NULL;
2983 struct got_repository *repo = NULL;
2984 struct got_worktree *worktree = NULL;
2986 struct got_pathlist_head paths;
2987 struct got_pathlist_entry *pe;
2992 while ((ch = getopt(argc, argv, "")) != -1) {
3004 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3008 cwd = getcwd(NULL, 0);
3010 error = got_error_from_errno("getcwd");
3014 error = got_worktree_open(&worktree, cwd);
3018 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3023 error = apply_unveil(got_repo_get_path(repo), 1,
3024 got_worktree_get_root_path(worktree));
3028 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3032 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3033 check_cancelled, NULL);
3035 TAILQ_FOREACH(pe, &paths, entry)
3036 free((char *)pe->path);
3037 got_pathlist_free(&paths);
3046 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3051 static const struct got_error *
3052 list_refs(struct got_repository *repo)
3054 static const struct got_error *err = NULL;
3055 struct got_reflist_head refs;
3056 struct got_reflist_entry *re;
3058 SIMPLEQ_INIT(&refs);
3059 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3063 SIMPLEQ_FOREACH(re, &refs, entry) {
3065 refstr = got_ref_to_str(re->ref);
3067 return got_error_from_errno("got_ref_to_str");
3068 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3072 got_ref_list_free(&refs);
3076 static const struct got_error *
3077 delete_ref(struct got_repository *repo, const char *refname)
3079 const struct got_error *err = NULL;
3080 struct got_reference *ref;
3082 err = got_ref_open(&ref, repo, refname, 0);
3086 err = got_ref_delete(ref, repo);
3091 static const struct got_error *
3092 add_ref(struct got_repository *repo, const char *refname, const char *target)
3094 const struct got_error *err = NULL;
3095 struct got_object_id *id;
3096 struct got_reference *ref = NULL;
3099 * Don't let the user create a reference name with a leading '-'.
3100 * While technically a valid reference name, this case is usually
3101 * an unintended typo.
3103 if (refname[0] == '-')
3104 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3106 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3109 struct got_reference *target_ref;
3111 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3113 err = got_ref_open(&target_ref, repo, target, 0);
3116 err = got_ref_resolve(&id, repo, target_ref);
3117 got_ref_close(target_ref);
3122 err = got_ref_alloc(&ref, refname, id);
3126 err = got_ref_write(ref, repo);
3134 static const struct got_error *
3135 add_symref(struct got_repository *repo, const char *refname, const char *target)
3137 const struct got_error *err = NULL;
3138 struct got_reference *ref = NULL;
3139 struct got_reference *target_ref = NULL;
3142 * Don't let the user create a reference name with a leading '-'.
3143 * While technically a valid reference name, this case is usually
3144 * an unintended typo.
3146 if (refname[0] == '-')
3147 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3149 err = got_ref_open(&target_ref, repo, target, 0);
3153 err = got_ref_alloc_symref(&ref, refname, target_ref);
3157 err = got_ref_write(ref, repo);
3160 got_ref_close(target_ref);
3166 static const struct got_error *
3167 cmd_ref(int argc, char *argv[])
3169 const struct got_error *error = NULL;
3170 struct got_repository *repo = NULL;
3171 struct got_worktree *worktree = NULL;
3172 char *cwd = NULL, *repo_path = NULL;
3173 int ch, do_list = 0, create_symref = 0;
3174 const char *delref = NULL;
3176 /* TODO: Add -s option for adding symbolic references. */
3177 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3183 repo_path = realpath(optarg, NULL);
3184 if (repo_path == NULL)
3185 return got_error_from_errno2("realpath",
3187 got_path_strip_trailing_slashes(repo_path);
3201 if (do_list && delref)
3202 errx(1, "-l and -d options are mutually exclusive\n");
3207 if (do_list || delref) {
3209 errx(1, "-s option cannot be used together with the "
3210 "-l or -d options");
3213 } else if (argc != 2)
3218 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3222 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3223 "sendfd unveil", NULL) == -1)
3227 cwd = getcwd(NULL, 0);
3229 error = got_error_from_errno("getcwd");
3233 if (repo_path == NULL) {
3234 error = got_worktree_open(&worktree, cwd);
3235 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3241 strdup(got_worktree_get_repo_path(worktree));
3242 if (repo_path == NULL)
3243 error = got_error_from_errno("strdup");
3247 repo_path = strdup(cwd);
3248 if (repo_path == NULL) {
3249 error = got_error_from_errno("strdup");
3255 error = got_repo_open(&repo, repo_path, NULL);
3259 error = apply_unveil(got_repo_get_path(repo), do_list,
3260 worktree ? got_worktree_get_root_path(worktree) : NULL);
3265 error = list_refs(repo);
3267 error = delete_ref(repo, delref);
3268 else if (create_symref)
3269 error = add_symref(repo, argv[0], argv[1]);
3271 error = add_ref(repo, argv[0], argv[1]);
3274 got_repo_close(repo);
3276 got_worktree_close(worktree);
3286 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3287 "[name]\n", getprogname());
3291 static const struct got_error *
3292 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3293 struct got_reference *ref)
3295 const struct got_error *err = NULL;
3296 const char *refname, *marker = " ";
3299 refname = got_ref_get_name(ref);
3300 if (worktree && strcmp(refname,
3301 got_worktree_get_head_ref_name(worktree)) == 0) {
3302 struct got_object_id *id = NULL;
3304 err = got_ref_resolve(&id, repo, ref);
3307 if (got_object_id_cmp(id,
3308 got_worktree_get_base_commit_id(worktree)) == 0)
3315 if (strncmp(refname, "refs/heads/", 11) == 0)
3317 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3320 refstr = got_ref_to_str(ref);
3322 return got_error_from_errno("got_ref_to_str");
3324 printf("%s%s: %s\n", marker, refname, refstr);
3329 static const struct got_error *
3330 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3332 const char *refname;
3334 if (worktree == NULL)
3335 return got_error(GOT_ERR_NOT_WORKTREE);
3337 refname = got_worktree_get_head_ref_name(worktree);
3339 if (strncmp(refname, "refs/heads/", 11) == 0)
3341 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3344 printf("%s\n", refname);
3349 static const struct got_error *
3350 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3352 static const struct got_error *err = NULL;
3353 struct got_reflist_head refs;
3354 struct got_reflist_entry *re;
3355 struct got_reference *temp_ref = NULL;
3356 int rebase_in_progress, histedit_in_progress;
3358 SIMPLEQ_INIT(&refs);
3361 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3366 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3371 if (rebase_in_progress || histedit_in_progress) {
3372 err = got_ref_open(&temp_ref, repo,
3373 got_worktree_get_head_ref_name(worktree), 0);
3376 list_branch(repo, worktree, temp_ref);
3377 got_ref_close(temp_ref);
3381 err = got_ref_list(&refs, repo, "refs/heads",
3382 got_ref_cmp_by_name, NULL);
3386 SIMPLEQ_FOREACH(re, &refs, entry)
3387 list_branch(repo, worktree, re->ref);
3389 got_ref_list_free(&refs);
3393 static const struct got_error *
3394 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3395 const char *branch_name)
3397 const struct got_error *err = NULL;
3398 struct got_reference *ref = NULL;
3401 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3402 return got_error_from_errno("asprintf");
3404 err = got_ref_open(&ref, repo, refname, 0);
3409 strcmp(got_worktree_get_head_ref_name(worktree),
3410 got_ref_get_name(ref)) == 0) {
3411 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3412 "will not delete this work tree's current branch");
3416 err = got_ref_delete(ref, repo);
3424 static const struct got_error *
3425 add_branch(struct got_repository *repo, const char *branch_name,
3426 struct got_object_id *base_commit_id)
3428 const struct got_error *err = NULL;
3429 struct got_reference *ref = NULL;
3430 char *base_refname = NULL, *refname = NULL;
3433 * Don't let the user create a branch name with a leading '-'.
3434 * While technically a valid reference name, this case is usually
3435 * an unintended typo.
3437 if (branch_name[0] == '-')
3438 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3440 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3441 err = got_error_from_errno("asprintf");
3445 err = got_ref_open(&ref, repo, refname, 0);
3447 err = got_error(GOT_ERR_BRANCH_EXISTS);
3449 } else if (err->code != GOT_ERR_NOT_REF)
3452 err = got_ref_alloc(&ref, refname, base_commit_id);
3456 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, *commit_id_arg = NULL;
3475 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3478 commit_id_arg = optarg;
3484 repo_path = realpath(optarg, NULL);
3485 if (repo_path == NULL)
3486 return got_error_from_errno2("realpath",
3488 got_path_strip_trailing_slashes(repo_path);
3499 if (do_list && delref)
3500 errx(1, "-l and -d options are mutually exclusive\n");
3505 if (!do_list && !delref && argc == 0)
3508 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3509 errx(1, "-c option can only be used when creating a branch");
3511 if (do_list || delref) {
3514 } else if (!do_show && argc != 1)
3518 if (do_list || do_show) {
3519 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3523 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3524 "sendfd unveil", NULL) == -1)
3528 cwd = getcwd(NULL, 0);
3530 error = got_error_from_errno("getcwd");
3534 if (repo_path == NULL) {
3535 error = got_worktree_open(&worktree, cwd);
3536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3542 strdup(got_worktree_get_repo_path(worktree));
3543 if (repo_path == NULL)
3544 error = got_error_from_errno("strdup");
3548 repo_path = strdup(cwd);
3549 if (repo_path == NULL) {
3550 error = got_error_from_errno("strdup");
3556 error = got_repo_open(&repo, repo_path, NULL);
3560 error = apply_unveil(got_repo_get_path(repo), do_list,
3561 worktree ? got_worktree_get_root_path(worktree) : NULL);
3566 error = show_current_branch(repo, worktree);
3568 error = list_branches(repo, worktree);
3570 error = delete_branch(repo, worktree, delref);
3572 struct got_object_id *commit_id;
3573 if (commit_id_arg == NULL)
3574 commit_id_arg = worktree ?
3575 got_worktree_get_head_ref_name(worktree) :
3577 error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3580 error = add_branch(repo, argv[0], commit_id);
3585 got_repo_close(repo);
3587 got_worktree_close(worktree);
3598 "usage: %s tag [-r repository] | -l | "
3599 "[-m message] name [commit]\n", getprogname());
3604 static const struct got_error *
3605 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3607 const struct got_error *err = NULL;
3608 struct got_reflist_entry *re, *se, *new;
3609 struct got_object_id *re_id, *se_id;
3610 struct got_tag_object *re_tag, *se_tag;
3611 time_t re_time, se_time;
3613 SIMPLEQ_FOREACH(re, tags, entry) {
3614 se = SIMPLEQ_FIRST(sorted);
3616 err = got_reflist_entry_dup(&new, re);
3619 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3622 err = got_ref_resolve(&re_id, repo, re->ref);
3625 err = got_object_open_as_tag(&re_tag, repo, re_id);
3629 re_time = got_object_tag_get_tagger_time(re_tag);
3630 got_object_tag_close(re_tag);
3634 err = got_ref_resolve(&se_id, repo, re->ref);
3637 err = got_object_open_as_tag(&se_tag, repo, se_id);
3641 se_time = got_object_tag_get_tagger_time(se_tag);
3642 got_object_tag_close(se_tag);
3644 if (se_time > re_time) {
3645 err = got_reflist_entry_dup(&new, re);
3648 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3651 se = SIMPLEQ_NEXT(se, entry);
3660 static const struct got_error *
3661 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3662 struct got_reference *ref2)
3664 const struct got_error *err = NULL;
3665 struct got_repository *repo = arg;
3666 struct got_object_id *id1, *id2 = NULL;
3667 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3668 time_t time1, time2;
3672 err = got_ref_resolve(&id1, repo, ref1);
3675 err = got_object_open_as_tag(&tag1, repo, id1);
3679 err = got_ref_resolve(&id2, repo, ref2);
3682 err = got_object_open_as_tag(&tag2, repo, id2);
3686 time1 = got_object_tag_get_tagger_time(tag1);
3687 time2 = got_object_tag_get_tagger_time(tag2);
3689 /* Put latest tags first. */
3692 else if (time1 > time2)
3695 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3700 got_object_tag_close(tag1);
3702 got_object_tag_close(tag2);
3706 static const struct got_error *
3707 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3709 static const struct got_error *err = NULL;
3710 struct got_reflist_head refs;
3711 struct got_reflist_entry *re;
3713 SIMPLEQ_INIT(&refs);
3715 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3719 SIMPLEQ_FOREACH(re, &refs, entry) {
3720 const char *refname;
3721 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3724 struct got_object_id *id;
3725 struct got_tag_object *tag;
3727 refname = got_ref_get_name(re->ref);
3728 if (strncmp(refname, "refs/tags/", 10) != 0)
3731 refstr = got_ref_to_str(re->ref);
3732 if (refstr == NULL) {
3733 err = got_error_from_errno("got_ref_to_str");
3736 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3739 err = got_ref_resolve(&id, repo, re->ref);
3742 err = got_object_open_as_tag(&tag, repo, id);
3746 printf("from: %s\n", got_object_tag_get_tagger(tag));
3747 tagger_time = got_object_tag_get_tagger_time(tag);
3748 datestr = get_datestr(&tagger_time, datebuf);
3750 printf("date: %s UTC\n", datestr);
3751 err = got_object_id_str(&id_str,
3752 got_object_tag_get_object_id(tag));
3755 switch (got_object_tag_get_object_type(tag)) {
3756 case GOT_OBJ_TYPE_BLOB:
3757 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3759 case GOT_OBJ_TYPE_TREE:
3760 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3762 case GOT_OBJ_TYPE_COMMIT:
3763 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3765 case GOT_OBJ_TYPE_TAG:
3766 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3772 tagmsg0 = strdup(got_object_tag_get_message(tag));
3773 got_object_tag_close(tag);
3774 if (tagmsg0 == NULL) {
3775 err = got_error_from_errno("strdup");
3781 line = strsep(&tagmsg, "\n");
3783 printf(" %s\n", line);
3788 got_ref_list_free(&refs);
3792 static const struct got_error *
3793 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3794 const char *tag_name, const char *repo_path)
3796 const struct got_error *err = NULL;
3797 char *template = NULL, *initial_content = NULL;
3798 char *editor = NULL;
3801 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3802 err = got_error_from_errno("asprintf");
3806 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3807 commit_id_str, tag_name) == -1) {
3808 err = got_error_from_errno("asprintf");
3812 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3816 dprintf(fd, initial_content);
3819 err = get_editor(&editor);
3822 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3824 free(initial_content);
3828 /* Editor is done; we can now apply unveil(2) */
3830 err = apply_unveil(repo_path, 0, NULL);
3839 static const struct got_error *
3840 add_tag(struct got_repository *repo, const char *tag_name,
3841 const char *commit_arg, const char *tagmsg_arg)
3843 const struct got_error *err = NULL;
3844 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3845 char *label = NULL, *commit_id_str = NULL;
3846 struct got_reference *ref = NULL;
3847 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3848 char *tagmsg_path = NULL, *tag_id_str = NULL;
3849 int preserve_tagmsg = 0;
3852 * Don't let the user create a tag name with a leading '-'.
3853 * While technically a valid reference name, this case is usually
3854 * an unintended typo.
3856 if (tag_name[0] == '-')
3857 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3859 err = get_author(&tagger, repo);
3863 err = match_object_id(&commit_id, &label, commit_arg,
3864 GOT_OBJ_TYPE_COMMIT, 1, repo);
3868 err = got_object_id_str(&commit_id_str, commit_id);
3872 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3873 refname = strdup(tag_name);
3874 if (refname == NULL) {
3875 err = got_error_from_errno("strdup");
3879 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3880 err = got_error_from_errno("asprintf");
3884 err = got_ref_open(&ref, repo, refname, 0);
3886 err = got_error(GOT_ERR_TAG_EXISTS);
3888 } else if (err->code != GOT_ERR_NOT_REF)
3891 if (tagmsg_arg == NULL) {
3892 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3893 tag_name, got_repo_get_path(repo));
3895 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3896 tagmsg_path != NULL)
3897 preserve_tagmsg = 1;
3902 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3903 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3906 preserve_tagmsg = 1;
3910 err = got_ref_alloc(&ref, refname, tag_id);
3913 preserve_tagmsg = 1;
3917 err = got_ref_write(ref, repo);
3920 preserve_tagmsg = 1;
3924 err = got_object_id_str(&tag_id_str, tag_id);
3927 preserve_tagmsg = 1;
3930 printf("Created tag %s\n", tag_id_str);
3932 if (preserve_tagmsg) {
3933 fprintf(stderr, "%s: tag message preserved in %s\n",
3934 getprogname(), tagmsg_path);
3935 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3936 err = got_error_from_errno2("unlink", tagmsg_path);
3941 free(commit_id_str);
3949 static const struct got_error *
3950 cmd_tag(int argc, char *argv[])
3952 const struct got_error *error = NULL;
3953 struct got_repository *repo = NULL;
3954 struct got_worktree *worktree = NULL;
3955 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3956 char *gitconfig_path = NULL;
3957 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3958 int ch, do_list = 0;
3960 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3966 repo_path = realpath(optarg, NULL);
3967 if (repo_path == NULL)
3968 return got_error_from_errno2("realpath",
3970 got_path_strip_trailing_slashes(repo_path);
3986 errx(1, "-l and -m options are mutually exclusive\n");
3989 } else if (argc < 1 || argc > 2)
3992 commit_id_arg = argv[1];
3997 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4001 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4002 "sendfd unveil", NULL) == -1)
4006 cwd = getcwd(NULL, 0);
4008 error = got_error_from_errno("getcwd");
4012 if (repo_path == NULL) {
4013 error = got_worktree_open(&worktree, cwd);
4014 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4020 strdup(got_worktree_get_repo_path(worktree));
4021 if (repo_path == NULL)
4022 error = got_error_from_errno("strdup");
4026 repo_path = strdup(cwd);
4027 if (repo_path == NULL) {
4028 error = got_error_from_errno("strdup");
4035 error = got_repo_open(&repo, repo_path, NULL);
4038 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4041 error = list_tags(repo, worktree);
4043 error = get_gitconfig_path(&gitconfig_path);
4046 error = got_repo_open(&repo, repo_path, gitconfig_path);
4051 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4056 if (commit_id_arg == NULL) {
4057 struct got_reference *head_ref;
4058 struct got_object_id *commit_id;
4059 error = got_ref_open(&head_ref, repo,
4060 worktree ? got_worktree_get_head_ref_name(worktree)
4064 error = got_ref_resolve(&commit_id, repo, head_ref);
4065 got_ref_close(head_ref);
4068 error = got_object_id_str(&commit_id_str, commit_id);
4074 error = add_tag(repo, tag_name,
4075 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4079 got_repo_close(repo);
4081 got_worktree_close(worktree);
4084 free(gitconfig_path);
4085 free(commit_id_str);
4092 fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4096 static const struct got_error *
4097 add_progress(void *arg, unsigned char status, const char *path)
4099 while (path[0] == '/')
4101 printf("%c %s\n", status, path);
4105 static const struct got_error *
4106 cmd_add(int argc, char *argv[])
4108 const struct got_error *error = NULL;
4109 struct got_repository *repo = NULL;
4110 struct got_worktree *worktree = NULL;
4112 struct got_pathlist_head paths;
4113 struct got_pathlist_entry *pe;
4114 int ch, can_recurse = 0;
4118 while ((ch = getopt(argc, argv, "R")) != -1) {
4133 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4140 cwd = getcwd(NULL, 0);
4142 error = got_error_from_errno("getcwd");
4146 error = got_worktree_open(&worktree, cwd);
4150 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4155 error = apply_unveil(got_repo_get_path(repo), 1,
4156 got_worktree_get_root_path(worktree));
4160 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4167 TAILQ_FOREACH(pe, &paths, entry) {
4168 if (asprintf(&ondisk_path, "%s/%s",
4169 got_worktree_get_root_path(worktree),
4171 error = got_error_from_errno("asprintf");
4174 if (lstat(ondisk_path, &sb) == -1) {
4175 if (errno == ENOENT) {
4179 error = got_error_from_errno2("lstat",
4185 if (S_ISDIR(sb.st_mode)) {
4186 error = got_error_msg(GOT_ERR_BAD_PATH,
4187 "adding directories requires -R option");
4192 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4196 got_repo_close(repo);
4198 got_worktree_close(worktree);
4199 TAILQ_FOREACH(pe, &paths, entry)
4200 free((char *)pe->path);
4201 got_pathlist_free(&paths);
4209 fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4213 static const struct got_error *
4214 print_remove_status(void *arg, unsigned char status,
4215 unsigned char staged_status, const char *path,
4216 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4217 struct got_object_id *commit_id)
4219 if (status == GOT_STATUS_NONEXISTENT)
4221 if (status == staged_status && (status == GOT_STATUS_DELETE))
4222 status = GOT_STATUS_NO_CHANGE;
4223 printf("%c%c %s\n", status, staged_status, path);
4227 static const struct got_error *
4228 cmd_remove(int argc, char *argv[])
4230 const struct got_error *error = NULL;
4231 struct got_worktree *worktree = NULL;
4232 struct got_repository *repo = NULL;
4234 struct got_pathlist_head paths;
4235 struct got_pathlist_entry *pe;
4236 int ch, delete_local_mods = 0;
4240 while ((ch = getopt(argc, argv, "f")) != -1) {
4243 delete_local_mods = 1;
4255 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4262 cwd = getcwd(NULL, 0);
4264 error = got_error_from_errno("getcwd");
4267 error = got_worktree_open(&worktree, cwd);
4271 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4276 error = apply_unveil(got_repo_get_path(repo), 1,
4277 got_worktree_get_root_path(worktree));
4281 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4285 error = got_worktree_schedule_delete(worktree, &paths,
4286 delete_local_mods, print_remove_status, NULL, repo);
4291 got_repo_close(repo);
4293 got_worktree_close(worktree);
4294 TAILQ_FOREACH(pe, &paths, entry)
4295 free((char *)pe->path);
4296 got_pathlist_free(&paths);
4304 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4305 "path ...\n", getprogname());
4309 static const struct got_error *
4310 revert_progress(void *arg, unsigned char status, const char *path)
4312 while (path[0] == '/')
4314 printf("%c %s\n", status, path);
4318 struct choose_patch_arg {
4319 FILE *patch_script_file;
4323 static const struct got_error *
4324 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4325 int nchanges, const char *action)
4328 size_t linesize = 0;
4332 case GOT_STATUS_ADD:
4333 printf("A %s\n%s this addition? [y/n] ", path, action);
4335 case GOT_STATUS_DELETE:
4336 printf("D %s\n%s this deletion? [y/n] ", path, action);
4338 case GOT_STATUS_MODIFY:
4339 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4340 return got_error_from_errno("fseek");
4341 printf(GOT_COMMIT_SEP_STR);
4342 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4344 if (ferror(patch_file))
4345 return got_error_from_errno("getline");
4346 printf(GOT_COMMIT_SEP_STR);
4347 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4348 path, n, nchanges, action);
4351 return got_error_path(path, GOT_ERR_FILE_STATUS);
4357 static const struct got_error *
4358 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4359 FILE *patch_file, int n, int nchanges)
4361 const struct got_error *err = NULL;
4363 size_t linesize = 0;
4366 struct choose_patch_arg *a = arg;
4368 *choice = GOT_PATCH_CHOICE_NONE;
4370 if (a->patch_script_file) {
4372 err = show_change(status, path, patch_file, n, nchanges,
4376 linelen = getline(&line, &linesize, a->patch_script_file);
4377 if (linelen == -1) {
4378 if (ferror(a->patch_script_file))
4379 return got_error_from_errno("getline");
4382 nl = strchr(line, '\n');
4385 if (strcmp(line, "y") == 0) {
4386 *choice = GOT_PATCH_CHOICE_YES;
4388 } else if (strcmp(line, "n") == 0) {
4389 *choice = GOT_PATCH_CHOICE_NO;
4391 } else if (strcmp(line, "q") == 0 &&
4392 status == GOT_STATUS_MODIFY) {
4393 *choice = GOT_PATCH_CHOICE_QUIT;
4396 printf("invalid response '%s'\n", line);
4401 while (resp != 'y' && resp != 'n' && resp != 'q') {
4402 err = show_change(status, path, patch_file, n, nchanges,
4409 if (status == GOT_STATUS_MODIFY) {
4410 if (resp != 'y' && resp != 'n' && resp != 'q') {
4411 printf("invalid response '%c'\n", resp);
4414 } else if (resp != 'y' && resp != 'n') {
4415 printf("invalid response '%c'\n", resp);
4421 *choice = GOT_PATCH_CHOICE_YES;
4422 else if (resp == 'n')
4423 *choice = GOT_PATCH_CHOICE_NO;
4424 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4425 *choice = GOT_PATCH_CHOICE_QUIT;
4431 static const struct got_error *
4432 cmd_revert(int argc, char *argv[])
4434 const struct got_error *error = NULL;
4435 struct got_worktree *worktree = NULL;
4436 struct got_repository *repo = NULL;
4437 char *cwd = NULL, *path = NULL;
4438 struct got_pathlist_head paths;
4439 struct got_pathlist_entry *pe;
4440 int ch, can_recurse = 0, pflag = 0;
4441 FILE *patch_script_file = NULL;
4442 const char *patch_script_path = NULL;
4443 struct choose_patch_arg cpa;
4447 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4453 patch_script_path = optarg;
4468 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4469 "unveil", NULL) == -1)
4474 if (patch_script_path && !pflag)
4475 errx(1, "-F option can only be used together with -p option");
4477 cwd = getcwd(NULL, 0);
4479 error = got_error_from_errno("getcwd");
4482 error = got_worktree_open(&worktree, cwd);
4486 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4491 if (patch_script_path) {
4492 patch_script_file = fopen(patch_script_path, "r");
4493 if (patch_script_file == NULL) {
4494 error = got_error_from_errno2("fopen",
4499 error = apply_unveil(got_repo_get_path(repo), 1,
4500 got_worktree_get_root_path(worktree));
4504 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4511 TAILQ_FOREACH(pe, &paths, entry) {
4512 if (asprintf(&ondisk_path, "%s/%s",
4513 got_worktree_get_root_path(worktree),
4515 error = got_error_from_errno("asprintf");
4518 if (lstat(ondisk_path, &sb) == -1) {
4519 if (errno == ENOENT) {
4523 error = got_error_from_errno2("lstat",
4529 if (S_ISDIR(sb.st_mode)) {
4530 error = got_error_msg(GOT_ERR_BAD_PATH,
4531 "reverting directories requires -R option");
4537 cpa.patch_script_file = patch_script_file;
4538 cpa.action = "revert";
4539 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4540 pflag ? choose_patch : NULL, &cpa, repo);
4544 if (patch_script_file && fclose(patch_script_file) == EOF &&
4546 error = got_error_from_errno2("fclose", patch_script_path);
4548 got_repo_close(repo);
4550 got_worktree_close(worktree);
4559 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4564 struct collect_commit_logmsg_arg {
4565 const char *cmdline_log;
4567 const char *worktree_path;
4568 const char *branch_name;
4569 const char *repo_path;
4574 static const struct got_error *
4575 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4578 char *initial_content = NULL;
4579 struct got_pathlist_entry *pe;
4580 const struct got_error *err = NULL;
4581 char *template = NULL;
4582 struct collect_commit_logmsg_arg *a = arg;
4586 /* if a message was specified on the command line, just use it */
4587 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4588 len = strlen(a->cmdline_log) + 1;
4589 *logmsg = malloc(len + 1);
4590 if (*logmsg == NULL)
4591 return got_error_from_errno("malloc");
4592 strlcpy(*logmsg, a->cmdline_log, len);
4596 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4597 return got_error_from_errno("asprintf");
4599 if (asprintf(&initial_content,
4600 "\n# changes to be committed on branch %s:\n",
4601 a->branch_name) == -1)
4602 return got_error_from_errno("asprintf");
4604 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4608 dprintf(fd, initial_content);
4610 TAILQ_FOREACH(pe, commitable_paths, entry) {
4611 struct got_commitable *ct = pe->data;
4612 dprintf(fd, "# %c %s\n",
4613 got_commitable_get_status(ct),
4614 got_commitable_get_path(ct));
4618 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4620 free(initial_content);
4623 /* Editor is done; we can now apply unveil(2) */
4625 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4634 static const struct got_error *
4635 cmd_commit(int argc, char *argv[])
4637 const struct got_error *error = NULL;
4638 struct got_worktree *worktree = NULL;
4639 struct got_repository *repo = NULL;
4640 char *cwd = NULL, *id_str = NULL;
4641 struct got_object_id *id = NULL;
4642 const char *logmsg = NULL;
4643 struct collect_commit_logmsg_arg cl_arg;
4644 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4645 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4646 struct got_pathlist_head paths;
4649 cl_arg.logmsg_path = NULL;
4651 while ((ch = getopt(argc, argv, "m:")) != -1) {
4666 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4667 "unveil", NULL) == -1)
4670 cwd = getcwd(NULL, 0);
4672 error = got_error_from_errno("getcwd");
4675 error = got_worktree_open(&worktree, cwd);
4679 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4682 if (rebase_in_progress) {
4683 error = got_error(GOT_ERR_REBASING);
4687 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4692 error = get_gitconfig_path(&gitconfig_path);
4695 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4700 error = get_author(&author, repo);
4705 * unveil(2) traverses exec(2); if an editor is used we have
4706 * to apply unveil after the log message has been written.
4708 if (logmsg == NULL || strlen(logmsg) == 0)
4709 error = get_editor(&editor);
4711 error = apply_unveil(got_repo_get_path(repo), 0,
4712 got_worktree_get_root_path(worktree));
4716 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4720 cl_arg.editor = editor;
4721 cl_arg.cmdline_log = logmsg;
4722 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4723 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4724 if (!histedit_in_progress) {
4725 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4726 error = got_error(GOT_ERR_COMMIT_BRANCH);
4729 cl_arg.branch_name += 11;
4731 cl_arg.repo_path = got_repo_get_path(repo);
4732 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4733 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4735 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4736 cl_arg.logmsg_path != NULL)
4737 preserve_logmsg = 1;
4741 error = got_object_id_str(&id_str, id);
4744 printf("Created commit %s\n", id_str);
4746 if (preserve_logmsg) {
4747 fprintf(stderr, "%s: log message preserved in %s\n",
4748 getprogname(), cl_arg.logmsg_path);
4749 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4751 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4752 free(cl_arg.logmsg_path);
4754 got_repo_close(repo);
4756 got_worktree_close(worktree);
4759 free(gitconfig_path);
4766 usage_cherrypick(void)
4768 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4772 static const struct got_error *
4773 cmd_cherrypick(int argc, char *argv[])
4775 const struct got_error *error = NULL;
4776 struct got_worktree *worktree = NULL;
4777 struct got_repository *repo = NULL;
4778 char *cwd = NULL, *commit_id_str = NULL;
4779 struct got_object_id *commit_id = NULL;
4780 struct got_commit_object *commit = NULL;
4781 struct got_object_qid *pid;
4782 struct got_reference *head_ref = NULL;
4783 int ch, did_something = 0;
4785 while ((ch = getopt(argc, argv, "")) != -1) {
4797 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 "unveil", NULL) == -1)
4804 cwd = getcwd(NULL, 0);
4806 error = got_error_from_errno("getcwd");
4809 error = got_worktree_open(&worktree, cwd);
4813 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4818 error = apply_unveil(got_repo_get_path(repo), 0,
4819 got_worktree_get_root_path(worktree));
4823 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 GOT_OBJ_TYPE_COMMIT, repo);
4825 if (error != NULL) {
4826 struct got_reference *ref;
4827 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4829 error = got_ref_open(&ref, repo, argv[0], 0);
4832 error = got_ref_resolve(&commit_id, repo, ref);
4837 error = got_object_id_str(&commit_id_str, commit_id);
4841 error = got_ref_open(&head_ref, repo,
4842 got_worktree_get_head_ref_name(worktree), 0);
4846 error = check_same_branch(commit_id, head_ref, NULL, repo);
4848 if (error->code != GOT_ERR_ANCESTRY)
4852 error = got_error(GOT_ERR_SAME_BRANCH);
4856 error = got_object_open_as_commit(&commit, repo, commit_id);
4859 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4860 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4861 commit_id, repo, update_progress, &did_something, check_cancelled,
4867 printf("Merged commit %s\n", commit_id_str);
4870 got_object_commit_close(commit);
4871 free(commit_id_str);
4873 got_ref_close(head_ref);
4875 got_worktree_close(worktree);
4877 got_repo_close(repo);
4884 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4888 static const struct got_error *
4889 cmd_backout(int argc, char *argv[])
4891 const struct got_error *error = NULL;
4892 struct got_worktree *worktree = NULL;
4893 struct got_repository *repo = NULL;
4894 char *cwd = NULL, *commit_id_str = NULL;
4895 struct got_object_id *commit_id = NULL;
4896 struct got_commit_object *commit = NULL;
4897 struct got_object_qid *pid;
4898 struct got_reference *head_ref = NULL;
4899 int ch, did_something = 0;
4901 while ((ch = getopt(argc, argv, "")) != -1) {
4913 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4914 "unveil", NULL) == -1)
4920 cwd = getcwd(NULL, 0);
4922 error = got_error_from_errno("getcwd");
4925 error = got_worktree_open(&worktree, cwd);
4929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4934 error = apply_unveil(got_repo_get_path(repo), 0,
4935 got_worktree_get_root_path(worktree));
4939 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4940 GOT_OBJ_TYPE_COMMIT, repo);
4941 if (error != NULL) {
4942 struct got_reference *ref;
4943 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4945 error = got_ref_open(&ref, repo, argv[0], 0);
4948 error = got_ref_resolve(&commit_id, repo, ref);
4953 error = got_object_id_str(&commit_id_str, commit_id);
4957 error = got_ref_open(&head_ref, repo,
4958 got_worktree_get_head_ref_name(worktree), 0);
4962 error = check_same_branch(commit_id, head_ref, NULL, repo);
4966 error = got_object_open_as_commit(&commit, repo, commit_id);
4969 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4971 error = got_error(GOT_ERR_ROOT_COMMIT);
4975 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4976 update_progress, &did_something, check_cancelled, NULL);
4981 printf("Backed out commit %s\n", commit_id_str);
4984 got_object_commit_close(commit);
4985 free(commit_id_str);
4987 got_ref_close(head_ref);
4989 got_worktree_close(worktree);
4991 got_repo_close(repo);
4998 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5004 trim_logmsg(char *logmsg, int limit)
5009 len = strlen(logmsg);
5013 nl = strchr(logmsg, '\n');
5018 static const struct got_error *
5019 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5021 const struct got_error *err;
5022 char *logmsg0 = NULL;
5025 err = got_object_commit_get_logmsg(&logmsg0, commit);
5030 while (isspace((unsigned char)s[0]))
5033 *logmsg = strdup(s);
5034 if (*logmsg == NULL) {
5035 err = got_error_from_errno("strdup");
5039 trim_logmsg(*logmsg, limit);
5045 static const struct got_error *
5046 show_rebase_progress(struct got_commit_object *commit,
5047 struct got_object_id *old_id, struct got_object_id *new_id)
5049 const struct got_error *err;
5050 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5052 err = got_object_id_str(&old_id_str, old_id);
5057 err = got_object_id_str(&new_id_str, new_id);
5062 old_id_str[12] = '\0';
5064 new_id_str[12] = '\0';
5066 err = get_short_logmsg(&logmsg, 42, commit);
5070 printf("%s -> %s: %s\n", old_id_str,
5071 new_id_str ? new_id_str : "no-op change", logmsg);
5078 static const struct got_error *
5079 rebase_progress(void *arg, unsigned char status, const char *path)
5081 unsigned char *rebase_status = arg;
5083 while (path[0] == '/')
5085 printf("%c %s\n", status, path);
5087 if (*rebase_status == GOT_STATUS_CONFLICT)
5089 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5090 *rebase_status = status;
5094 static const struct got_error *
5095 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5096 struct got_reference *branch, struct got_reference *new_base_branch,
5097 struct got_reference *tmp_branch, struct got_repository *repo)
5099 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5100 return got_worktree_rebase_complete(worktree, fileindex,
5101 new_base_branch, tmp_branch, branch, repo);
5104 static const struct got_error *
5105 rebase_commit(struct got_pathlist_head *merged_paths,
5106 struct got_worktree *worktree, struct got_fileindex *fileindex,
5107 struct got_reference *tmp_branch,
5108 struct got_object_id *commit_id, struct got_repository *repo)
5110 const struct got_error *error;
5111 struct got_commit_object *commit;
5112 struct got_object_id *new_commit_id;
5114 error = got_object_open_as_commit(&commit, repo, commit_id);
5118 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5119 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5121 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5123 error = show_rebase_progress(commit, commit_id, NULL);
5125 error = show_rebase_progress(commit, commit_id, new_commit_id);
5126 free(new_commit_id);
5129 got_object_commit_close(commit);
5133 struct check_path_prefix_arg {
5134 const char *path_prefix;
5139 static const struct got_error *
5140 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5141 struct got_blob_object *blob2, struct got_object_id *id1,
5142 struct got_object_id *id2, const char *path1, const char *path2,
5143 mode_t mode1, mode_t mode2, struct got_repository *repo)
5145 struct check_path_prefix_arg *a = arg;
5147 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5148 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5149 return got_error(a->errcode);
5154 static const struct got_error *
5155 check_path_prefix(struct got_object_id *parent_id,
5156 struct got_object_id *commit_id, const char *path_prefix,
5157 int errcode, struct got_repository *repo)
5159 const struct got_error *err;
5160 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5161 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5162 struct check_path_prefix_arg cpp_arg;
5164 if (got_path_is_root_dir(path_prefix))
5167 err = got_object_open_as_commit(&commit, repo, commit_id);
5171 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5175 err = got_object_open_as_tree(&tree1, repo,
5176 got_object_commit_get_tree_id(parent_commit));
5180 err = got_object_open_as_tree(&tree2, repo,
5181 got_object_commit_get_tree_id(commit));
5185 cpp_arg.path_prefix = path_prefix;
5186 while (cpp_arg.path_prefix[0] == '/')
5187 cpp_arg.path_prefix++;
5188 cpp_arg.len = strlen(cpp_arg.path_prefix);
5189 cpp_arg.errcode = errcode;
5190 err = got_diff_tree(tree1, tree2, "", "", repo,
5191 check_path_prefix_in_diff, &cpp_arg, 0);
5194 got_object_tree_close(tree1);
5196 got_object_tree_close(tree2);
5198 got_object_commit_close(commit);
5200 got_object_commit_close(parent_commit);
5204 static const struct got_error *
5205 collect_commits(struct got_object_id_queue *commits,
5206 struct got_object_id *initial_commit_id,
5207 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5208 const char *path_prefix, int path_prefix_errcode,
5209 struct got_repository *repo)
5211 const struct got_error *err = NULL;
5212 struct got_commit_graph *graph = NULL;
5213 struct got_object_id *parent_id = NULL;
5214 struct got_object_qid *qid;
5215 struct got_object_id *commit_id = initial_commit_id;
5217 err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5221 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5222 check_cancelled, NULL);
5225 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5226 err = got_commit_graph_iter_next(&parent_id, graph);
5228 if (err->code == GOT_ERR_ITER_COMPLETED) {
5229 err = got_error_msg(GOT_ERR_ANCESTRY,
5230 "ran out of commits to rebase before "
5231 "youngest common ancestor commit has "
5234 } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5236 err = got_commit_graph_fetch_commits(graph, 1, repo,
5237 check_cancelled, NULL);
5241 err = check_path_prefix(parent_id, commit_id,
5242 path_prefix, path_prefix_errcode, repo);
5246 err = got_object_qid_alloc(&qid, commit_id);
5249 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5250 commit_id = parent_id;
5254 got_commit_graph_close(graph);
5258 static const struct got_error *
5259 cmd_rebase(int argc, char *argv[])
5261 const struct got_error *error = NULL;
5262 struct got_worktree *worktree = NULL;
5263 struct got_repository *repo = NULL;
5264 struct got_fileindex *fileindex = NULL;
5266 struct got_reference *branch = NULL;
5267 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5268 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5269 struct got_object_id *resume_commit_id = NULL;
5270 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5271 struct got_commit_object *commit = NULL;
5272 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5273 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5274 struct got_object_id_queue commits;
5275 struct got_pathlist_head merged_paths;
5276 const struct got_object_id_queue *parent_ids;
5277 struct got_object_qid *qid, *pid;
5279 SIMPLEQ_INIT(&commits);
5280 TAILQ_INIT(&merged_paths);
5282 while ((ch = getopt(argc, argv, "ac")) != -1) {
5288 continue_rebase = 1;
5300 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5301 "unveil", NULL) == -1)
5304 if (abort_rebase && continue_rebase)
5306 else if (abort_rebase || continue_rebase) {
5309 } else if (argc != 1)
5312 cwd = getcwd(NULL, 0);
5314 error = got_error_from_errno("getcwd");
5317 error = got_worktree_open(&worktree, cwd);
5321 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5326 error = apply_unveil(got_repo_get_path(repo), 0,
5327 got_worktree_get_root_path(worktree));
5331 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5337 if (!rebase_in_progress) {
5338 error = got_error(GOT_ERR_NOT_REBASING);
5341 error = got_worktree_rebase_continue(&resume_commit_id,
5342 &new_base_branch, &tmp_branch, &branch, &fileindex,
5346 printf("Switching work tree to %s\n",
5347 got_ref_get_symref_target(new_base_branch));
5348 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5349 new_base_branch, update_progress, &did_something);
5352 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5353 goto done; /* nothing else to do */
5356 if (continue_rebase) {
5357 if (!rebase_in_progress) {
5358 error = got_error(GOT_ERR_NOT_REBASING);
5361 error = got_worktree_rebase_continue(&resume_commit_id,
5362 &new_base_branch, &tmp_branch, &branch, &fileindex,
5367 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5368 resume_commit_id, repo);
5372 yca_id = got_object_id_dup(resume_commit_id);
5373 if (yca_id == NULL) {
5374 error = got_error_from_errno("got_object_id_dup");
5378 error = got_ref_open(&branch, repo, argv[0], 0);
5383 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5387 if (!continue_rebase) {
5388 struct got_object_id *base_commit_id;
5390 base_commit_id = got_worktree_get_base_commit_id(worktree);
5391 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5392 base_commit_id, branch_head_commit_id, repo,
5393 check_cancelled, NULL);
5396 if (yca_id == NULL) {
5397 error = got_error_msg(GOT_ERR_ANCESTRY,
5398 "specified branch shares no common ancestry "
5399 "with work tree's branch");
5403 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5405 if (error->code != GOT_ERR_ANCESTRY)
5409 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5410 "specified branch resolves to a commit which "
5411 "is already contained in work tree's branch");
5414 error = got_worktree_rebase_prepare(&new_base_branch,
5415 &tmp_branch, &fileindex, worktree, branch, repo);
5420 commit_id = branch_head_commit_id;
5421 error = got_object_open_as_commit(&commit, repo, commit_id);
5425 parent_ids = got_object_commit_get_parent_ids(commit);
5426 pid = SIMPLEQ_FIRST(parent_ids);
5428 if (!continue_rebase) {
5430 error = got_worktree_rebase_abort(worktree, fileindex,
5431 repo, new_base_branch, update_progress,
5435 printf("Rebase of %s aborted\n",
5436 got_ref_get_name(branch));
5438 error = got_error(GOT_ERR_EMPTY_REBASE);
5441 error = collect_commits(&commits, commit_id, pid->id,
5442 yca_id, got_worktree_get_path_prefix(worktree),
5443 GOT_ERR_REBASE_PATH, repo);
5444 got_object_commit_close(commit);
5449 if (SIMPLEQ_EMPTY(&commits)) {
5450 if (continue_rebase) {
5451 error = rebase_complete(worktree, fileindex,
5452 branch, new_base_branch, tmp_branch, repo);
5455 /* Fast-forward the reference of the branch. */
5456 struct got_object_id *new_head_commit_id;
5458 error = got_ref_resolve(&new_head_commit_id, repo,
5462 error = got_object_id_str(&id_str, new_head_commit_id);
5463 printf("Forwarding %s to commit %s\n",
5464 got_ref_get_name(branch), id_str);
5466 error = got_ref_change_ref(branch,
5467 new_head_commit_id);
5474 SIMPLEQ_FOREACH(qid, &commits, entry) {
5475 commit_id = qid->id;
5476 parent_id = pid ? pid->id : yca_id;
5479 error = got_worktree_rebase_merge_files(&merged_paths,
5480 worktree, fileindex, parent_id, commit_id, repo,
5481 rebase_progress, &rebase_status, check_cancelled, NULL);
5485 if (rebase_status == GOT_STATUS_CONFLICT) {
5486 got_worktree_rebase_pathlist_free(&merged_paths);
5490 error = rebase_commit(&merged_paths, worktree, fileindex,
5491 tmp_branch, commit_id, repo);
5492 got_worktree_rebase_pathlist_free(&merged_paths);
5497 if (rebase_status == GOT_STATUS_CONFLICT) {
5498 error = got_worktree_rebase_postpone(worktree, fileindex);
5501 error = got_error_msg(GOT_ERR_CONFLICTS,
5502 "conflicts must be resolved before rebasing can continue");
5504 error = rebase_complete(worktree, fileindex, branch,
5505 new_base_branch, tmp_branch, repo);
5507 got_object_id_queue_free(&commits);
5508 free(branch_head_commit_id);
5509 free(resume_commit_id);
5512 got_object_commit_close(commit);
5514 got_ref_close(branch);
5515 if (new_base_branch)
5516 got_ref_close(new_base_branch);
5518 got_ref_close(tmp_branch);
5520 got_worktree_close(worktree);
5522 got_repo_close(repo);
5527 usage_histedit(void)
5529 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5534 #define GOT_HISTEDIT_PICK 'p'
5535 #define GOT_HISTEDIT_EDIT 'e'
5536 #define GOT_HISTEDIT_FOLD 'f'
5537 #define GOT_HISTEDIT_DROP 'd'
5538 #define GOT_HISTEDIT_MESG 'm'
5540 static struct got_histedit_cmd {
5544 } got_histedit_cmds[] = {
5545 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5546 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5547 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5548 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5549 { GOT_HISTEDIT_MESG, "mesg",
5550 "single-line log message for commit above (open editor if empty)" },
5553 struct got_histedit_list_entry {
5554 TAILQ_ENTRY(got_histedit_list_entry) entry;
5555 struct got_object_id *commit_id;
5556 const struct got_histedit_cmd *cmd;
5559 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5561 static const struct got_error *
5562 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5563 FILE *f, struct got_repository *repo)
5565 const struct got_error *err = NULL;
5566 char *logmsg = NULL, *id_str = NULL;
5567 struct got_commit_object *commit = NULL;
5570 err = got_object_open_as_commit(&commit, repo, commit_id);
5574 err = get_short_logmsg(&logmsg, 34, commit);
5578 err = got_object_id_str(&id_str, commit_id);
5582 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5584 err = got_ferror(f, GOT_ERR_IO);
5587 got_object_commit_close(commit);
5593 static const struct got_error *
5594 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5595 struct got_repository *repo)
5597 const struct got_error *err = NULL;
5598 struct got_object_qid *qid;
5600 if (SIMPLEQ_EMPTY(commits))
5601 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5603 SIMPLEQ_FOREACH(qid, commits, entry) {
5604 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5613 static const struct got_error *
5614 write_cmd_list(FILE *f)
5616 const struct got_error *err = NULL;
5619 n = fprintf(f, "# Available histedit commands:\n");
5621 return got_ferror(f, GOT_ERR_IO);
5623 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5624 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5625 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5628 err = got_ferror(f, GOT_ERR_IO);
5632 n = fprintf(f, "# Commits will be processed in order from top to "
5633 "bottom of this file.\n");
5635 return got_ferror(f, GOT_ERR_IO);
5639 static const struct got_error *
5640 histedit_syntax_error(int lineno)
5642 static char msg[42];
5645 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5647 if (ret == -1 || ret >= sizeof(msg))
5648 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5650 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5653 static const struct got_error *
5654 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5655 char *logmsg, struct got_repository *repo)
5657 const struct got_error *err;
5658 struct got_commit_object *folded_commit = NULL;
5659 char *id_str, *folded_logmsg = NULL;
5661 err = got_object_id_str(&id_str, hle->commit_id);
5665 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5669 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5672 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5673 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5674 folded_logmsg) == -1) {
5675 err = got_error_from_errno("asprintf");
5680 got_object_commit_close(folded_commit);
5682 free(folded_logmsg);
5686 static struct got_histedit_list_entry *
5687 get_folded_commits(struct got_histedit_list_entry *hle)
5689 struct got_histedit_list_entry *prev, *folded = NULL;
5691 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5692 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5693 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5694 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5696 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5702 static const struct got_error *
5703 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5704 struct got_repository *repo)
5706 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5707 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5708 const struct got_error *err = NULL;
5709 struct got_commit_object *commit = NULL;
5711 struct got_histedit_list_entry *folded = NULL;
5713 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5717 folded = get_folded_commits(hle);
5719 while (folded != hle) {
5720 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5721 folded = TAILQ_NEXT(folded, entry);
5724 err = append_folded_commit_msg(&new_msg, folded,
5730 folded = TAILQ_NEXT(folded, entry);
5734 err = got_object_id_str(&id_str, hle->commit_id);
5737 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5740 if (asprintf(&new_msg,
5741 "%s\n# original log message of commit %s: %s",
5742 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5743 err = got_error_from_errno("asprintf");
5749 err = got_object_id_str(&id_str, hle->commit_id);
5753 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5757 dprintf(fd, logmsg);
5760 err = get_editor(&editor);
5764 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5766 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5768 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5771 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5772 err = got_error_from_errno2("unlink", logmsg_path);
5778 got_object_commit_close(commit);
5782 static const struct got_error *
5783 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5784 FILE *f, struct got_repository *repo)
5786 const struct got_error *err = NULL;
5787 char *line = NULL, *p, *end;
5791 const struct got_histedit_cmd *cmd;
5792 struct got_object_id *commit_id = NULL;
5793 struct got_histedit_list_entry *hle = NULL;
5796 len = getline(&line, &size, f);
5798 const struct got_error *getline_err;
5801 getline_err = got_error_from_errno("getline");
5802 err = got_ferror(f, getline_err->code);
5807 while (isspace((unsigned char)p[0]))
5809 if (p[0] == '#' || p[0] == '\0') {
5815 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5816 cmd = &got_histedit_cmds[i];
5817 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5818 isspace((unsigned char)p[strlen(cmd->name)])) {
5819 p += strlen(cmd->name);
5822 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5827 if (i == nitems(got_histedit_cmds)) {
5828 err = histedit_syntax_error(lineno);
5831 while (isspace((unsigned char)p[0]))
5833 if (cmd->code == GOT_HISTEDIT_MESG) {
5834 if (hle == NULL || hle->logmsg != NULL) {
5835 err = got_error(GOT_ERR_HISTEDIT_CMD);
5839 err = histedit_edit_logmsg(hle, repo);
5843 hle->logmsg = strdup(p);
5844 if (hle->logmsg == NULL) {
5845 err = got_error_from_errno("strdup");
5854 while (end[0] && !isspace((unsigned char)end[0]))
5858 err = got_object_resolve_id_str(&commit_id, repo, p);
5860 /* override error code */
5861 err = histedit_syntax_error(lineno);
5865 hle = malloc(sizeof(*hle));
5867 err = got_error_from_errno("malloc");
5871 hle->commit_id = commit_id;
5876 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5884 static const struct got_error *
5885 histedit_check_script(struct got_histedit_list *histedit_cmds,
5886 struct got_object_id_queue *commits, struct got_repository *repo)
5888 const struct got_error *err = NULL;
5889 struct got_object_qid *qid;
5890 struct got_histedit_list_entry *hle;
5891 static char msg[80];
5894 if (TAILQ_EMPTY(histedit_cmds))
5895 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5896 "histedit script contains no commands");
5897 if (SIMPLEQ_EMPTY(commits))
5898 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5900 SIMPLEQ_FOREACH(qid, commits, entry) {
5901 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5902 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5906 err = got_object_id_str(&id_str, qid->id);
5909 snprintf(msg, sizeof(msg),
5910 "commit %s missing from histedit script", id_str);
5912 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5916 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5917 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5918 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5919 "last commit in histedit script cannot be folded");
5924 static const struct got_error *
5925 histedit_run_editor(struct got_histedit_list *histedit_cmds,
5926 const char *path, struct got_object_id_queue *commits,
5927 struct got_repository *repo)
5929 const struct got_error *err = NULL;
5933 err = get_editor(&editor);
5937 if (spawn_editor(editor, path) == -1) {
5938 err = got_error_from_errno("failed spawning editor");
5942 f = fopen(path, "r");
5944 err = got_error_from_errno("fopen");
5947 err = histedit_parse_list(histedit_cmds, f, repo);
5951 err = histedit_check_script(histedit_cmds, commits, repo);
5953 if (f && fclose(f) != 0 && err == NULL)
5954 err = got_error_from_errno("fclose");
5959 static const struct got_error *
5960 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5961 struct got_object_id_queue *, const char *, struct got_repository *);
5963 static const struct got_error *
5964 histedit_edit_script(struct got_histedit_list *histedit_cmds,
5965 struct got_object_id_queue *commits, struct got_repository *repo)
5967 const struct got_error *err;
5971 err = got_opentemp_named(&path, &f, "got-histedit");
5975 err = write_cmd_list(f);
5979 err = histedit_write_commit_list(commits, f, repo);
5983 if (fclose(f) != 0) {
5984 err = got_error_from_errno("fclose");
5989 err = histedit_run_editor(histedit_cmds, path, commits, repo);
5991 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5992 err->code != GOT_ERR_HISTEDIT_CMD)
5994 err = histedit_edit_list_retry(histedit_cmds, err,
5995 commits, path, repo);
5998 if (f && fclose(f) != 0 && err == NULL)
5999 err = got_error_from_errno("fclose");
6000 if (path && unlink(path) != 0 && err == NULL)
6001 err = got_error_from_errno2("unlink", path);
6006 static const struct got_error *
6007 histedit_save_list(struct got_histedit_list *histedit_cmds,
6008 struct got_worktree *worktree, struct got_repository *repo)
6010 const struct got_error *err = NULL;
6013 struct got_histedit_list_entry *hle;
6014 struct got_commit_object *commit = NULL;
6016 err = got_worktree_get_histedit_script_path(&path, worktree);
6020 f = fopen(path, "w");
6022 err = got_error_from_errno2("fopen", path);
6025 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6026 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6032 int n = fprintf(f, "%c %s\n",
6033 GOT_HISTEDIT_MESG, hle->logmsg);
6035 err = got_ferror(f, GOT_ERR_IO);
6041 if (f && fclose(f) != 0 && err == NULL)
6042 err = got_error_from_errno("fclose");
6045 got_object_commit_close(commit);
6050 histedit_free_list(struct got_histedit_list *histedit_cmds)
6052 struct got_histedit_list_entry *hle;
6054 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6055 TAILQ_REMOVE(histedit_cmds, hle, entry);
6060 static const struct got_error *
6061 histedit_load_list(struct got_histedit_list *histedit_cmds,
6062 const char *path, struct got_repository *repo)
6064 const struct got_error *err = NULL;
6067 f = fopen(path, "r");
6069 err = got_error_from_errno2("fopen", path);
6073 err = histedit_parse_list(histedit_cmds, f, repo);
6075 if (f && fclose(f) != 0 && err == NULL)
6076 err = got_error_from_errno("fclose");
6080 static const struct got_error *
6081 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6082 const struct got_error *edit_err, struct got_object_id_queue *commits,
6083 const char *path, struct got_repository *repo)
6085 const struct got_error *err = NULL, *prev_err = edit_err;
6088 while (resp != 'c' && resp != 'r' && resp != 'a') {
6089 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6090 "or (a)bort: ", getprogname(), prev_err->msg);
6095 histedit_free_list(histedit_cmds);
6096 err = histedit_run_editor(histedit_cmds, path, commits,
6099 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6100 err->code != GOT_ERR_HISTEDIT_CMD)
6107 } else if (resp == 'r') {
6108 histedit_free_list(histedit_cmds);
6109 err = histedit_edit_script(histedit_cmds,
6112 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6113 err->code != GOT_ERR_HISTEDIT_CMD)
6120 } else if (resp == 'a') {
6121 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6124 printf("invalid response '%c'\n", resp);
6130 static const struct got_error *
6131 histedit_complete(struct got_worktree *worktree,
6132 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6133 struct got_reference *branch, struct got_repository *repo)
6135 printf("Switching work tree to %s\n",
6136 got_ref_get_symref_target(branch));
6137 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6141 static const struct got_error *
6142 show_histedit_progress(struct got_commit_object *commit,
6143 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6145 const struct got_error *err;
6146 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6148 err = got_object_id_str(&old_id_str, hle->commit_id);
6153 err = got_object_id_str(&new_id_str, new_id);
6158 old_id_str[12] = '\0';
6160 new_id_str[12] = '\0';
6163 logmsg = strdup(hle->logmsg);
6164 if (logmsg == NULL) {
6165 err = got_error_from_errno("strdup");
6168 trim_logmsg(logmsg, 42);
6170 err = get_short_logmsg(&logmsg, 42, commit);
6175 switch (hle->cmd->code) {
6176 case GOT_HISTEDIT_PICK:
6177 case GOT_HISTEDIT_EDIT:
6178 printf("%s -> %s: %s\n", old_id_str,
6179 new_id_str ? new_id_str : "no-op change", logmsg);
6181 case GOT_HISTEDIT_DROP:
6182 case GOT_HISTEDIT_FOLD:
6183 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6196 static const struct got_error *
6197 histedit_commit(struct got_pathlist_head *merged_paths,
6198 struct got_worktree *worktree, struct got_fileindex *fileindex,
6199 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6200 struct got_repository *repo)
6202 const struct got_error *err;
6203 struct got_commit_object *commit;
6204 struct got_object_id *new_commit_id;
6206 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6207 && hle->logmsg == NULL) {
6208 err = histedit_edit_logmsg(hle, repo);
6213 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6217 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6218 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6221 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6223 err = show_histedit_progress(commit, hle, NULL);
6225 err = show_histedit_progress(commit, hle, new_commit_id);
6226 free(new_commit_id);
6229 got_object_commit_close(commit);
6233 static const struct got_error *
6234 histedit_skip_commit(struct got_histedit_list_entry *hle,
6235 struct got_worktree *worktree, struct got_repository *repo)
6237 const struct got_error *error;
6238 struct got_commit_object *commit;
6240 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6245 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6249 error = show_histedit_progress(commit, hle, NULL);
6250 got_object_commit_close(commit);
6254 static const struct got_error *
6255 cmd_histedit(int argc, char *argv[])
6257 const struct got_error *error = NULL;
6258 struct got_worktree *worktree = NULL;
6259 struct got_fileindex *fileindex = NULL;
6260 struct got_repository *repo = NULL;
6262 struct got_reference *branch = NULL;
6263 struct got_reference *tmp_branch = NULL;
6264 struct got_object_id *resume_commit_id = NULL;
6265 struct got_object_id *base_commit_id = NULL;
6266 struct got_object_id *head_commit_id = NULL;
6267 struct got_commit_object *commit = NULL;
6268 int ch, rebase_in_progress = 0, did_something;
6269 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6270 const char *edit_script_path = NULL;
6271 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6272 struct got_object_id_queue commits;
6273 struct got_pathlist_head merged_paths;
6274 const struct got_object_id_queue *parent_ids;
6275 struct got_object_qid *pid;
6276 struct got_histedit_list histedit_cmds;
6277 struct got_histedit_list_entry *hle;
6279 SIMPLEQ_INIT(&commits);
6280 TAILQ_INIT(&histedit_cmds);
6281 TAILQ_INIT(&merged_paths);
6283 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6292 edit_script_path = optarg;
6304 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6305 "unveil", NULL) == -1)
6308 if (abort_edit && continue_edit)
6314 * This command cannot apply unveil(2) in all cases because the
6315 * user may choose to run an editor to edit the histedit script
6316 * and to edit individual commit log messages.
6317 * unveil(2) traverses exec(2); if an editor is used we have to
6318 * apply unveil after edit script and log messages have been written.
6319 * XXX TODO: Make use of unveil(2) where possible.
6322 cwd = getcwd(NULL, 0);
6324 error = got_error_from_errno("getcwd");
6327 error = got_worktree_open(&worktree, cwd);
6331 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6336 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6339 if (rebase_in_progress) {
6340 error = got_error(GOT_ERR_REBASING);
6344 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6348 if (edit_in_progress && abort_edit) {
6349 error = got_worktree_histedit_continue(&resume_commit_id,
6350 &tmp_branch, &branch, &base_commit_id, &fileindex,
6354 printf("Switching work tree to %s\n",
6355 got_ref_get_symref_target(branch));
6356 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6357 branch, base_commit_id, update_progress, &did_something);
6360 printf("Histedit of %s aborted\n",
6361 got_ref_get_symref_target(branch));
6362 goto done; /* nothing else to do */
6363 } else if (abort_edit) {
6364 error = got_error(GOT_ERR_NOT_HISTEDIT);
6368 if (continue_edit) {
6371 if (!edit_in_progress) {
6372 error = got_error(GOT_ERR_NOT_HISTEDIT);
6376 error = got_worktree_get_histedit_script_path(&path, worktree);
6380 error = histedit_load_list(&histedit_cmds, path, repo);
6385 error = got_worktree_histedit_continue(&resume_commit_id,
6386 &tmp_branch, &branch, &base_commit_id, &fileindex,
6391 error = got_ref_resolve(&head_commit_id, repo, branch);
6395 error = got_object_open_as_commit(&commit, repo,
6399 parent_ids = got_object_commit_get_parent_ids(commit);
6400 pid = SIMPLEQ_FIRST(parent_ids);
6402 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6405 error = collect_commits(&commits, head_commit_id, pid->id,
6406 base_commit_id, got_worktree_get_path_prefix(worktree),
6407 GOT_ERR_HISTEDIT_PATH, repo);
6408 got_object_commit_close(commit);
6413 if (edit_in_progress) {
6414 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6418 error = got_ref_open(&branch, repo,
6419 got_worktree_get_head_ref_name(worktree), 0);
6423 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6424 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6425 "will not edit commit history of a branch outside "
6426 "the \"refs/heads/\" reference namespace");
6430 error = got_ref_resolve(&head_commit_id, repo, branch);
6431 got_ref_close(branch);
6436 error = got_object_open_as_commit(&commit, repo,
6440 parent_ids = got_object_commit_get_parent_ids(commit);
6441 pid = SIMPLEQ_FIRST(parent_ids);
6443 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6446 error = collect_commits(&commits, head_commit_id, pid->id,
6447 got_worktree_get_base_commit_id(worktree),
6448 got_worktree_get_path_prefix(worktree),
6449 GOT_ERR_HISTEDIT_PATH, repo);
6450 got_object_commit_close(commit);
6455 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6456 &base_commit_id, &fileindex, worktree, repo);
6460 if (edit_script_path) {
6461 error = histedit_load_list(&histedit_cmds,
6462 edit_script_path, repo);
6464 got_worktree_histedit_abort(worktree, fileindex,
6465 repo, branch, base_commit_id,
6466 update_progress, &did_something);
6470 error = histedit_edit_script(&histedit_cmds, &commits,
6473 got_worktree_histedit_abort(worktree, fileindex,
6474 repo, branch, base_commit_id,
6475 update_progress, &did_something);
6481 error = histedit_save_list(&histedit_cmds, worktree,
6484 got_worktree_histedit_abort(worktree, fileindex,
6485 repo, branch, base_commit_id,
6486 update_progress, &did_something);
6492 error = histedit_check_script(&histedit_cmds, &commits, repo);
6496 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6497 if (resume_commit_id) {
6498 if (got_object_id_cmp(hle->commit_id,
6499 resume_commit_id) != 0)
6502 resume_commit_id = NULL;
6503 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6504 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6505 error = histedit_skip_commit(hle, worktree,
6508 error = histedit_commit(NULL, worktree,
6509 fileindex, tmp_branch, hle, repo);
6516 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6517 error = histedit_skip_commit(hle, worktree, repo);
6523 error = got_object_open_as_commit(&commit, repo,
6527 parent_ids = got_object_commit_get_parent_ids(commit);
6528 pid = SIMPLEQ_FIRST(parent_ids);
6530 error = got_worktree_histedit_merge_files(&merged_paths,
6531 worktree, fileindex, pid->id, hle->commit_id, repo,
6532 rebase_progress, &rebase_status, check_cancelled, NULL);
6535 got_object_commit_close(commit);
6538 if (rebase_status == GOT_STATUS_CONFLICT) {
6539 got_worktree_rebase_pathlist_free(&merged_paths);
6543 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6545 error = got_object_id_str(&id_str, hle->commit_id);
6548 printf("Stopping histedit for amending commit %s\n",
6551 got_worktree_rebase_pathlist_free(&merged_paths);
6552 error = got_worktree_histedit_postpone(worktree,
6557 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6558 error = histedit_skip_commit(hle, worktree, repo);
6564 error = histedit_commit(&merged_paths, worktree, fileindex,
6565 tmp_branch, hle, repo);
6566 got_worktree_rebase_pathlist_free(&merged_paths);
6571 if (rebase_status == GOT_STATUS_CONFLICT) {
6572 error = got_worktree_histedit_postpone(worktree, fileindex);
6575 error = got_error_msg(GOT_ERR_CONFLICTS,
6576 "conflicts must be resolved before rebasing can continue");
6578 error = histedit_complete(worktree, fileindex, tmp_branch,
6581 got_object_id_queue_free(&commits);
6582 histedit_free_list(&histedit_cmds);
6583 free(head_commit_id);
6584 free(base_commit_id);
6585 free(resume_commit_id);
6587 got_object_commit_close(commit);
6589 got_ref_close(branch);
6591 got_ref_close(tmp_branch);
6593 got_worktree_close(worktree);
6595 got_repo_close(repo);
6600 usage_integrate(void)
6602 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6606 static const struct got_error *
6607 cmd_integrate(int argc, char *argv[])
6609 const struct got_error *error = NULL;
6610 struct got_repository *repo = NULL;
6611 struct got_worktree *worktree = NULL;
6612 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6613 const char *branch_arg = NULL;
6614 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6615 struct got_fileindex *fileindex = NULL;
6616 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6617 int ch, did_something = 0;
6619 while ((ch = getopt(argc, argv, "")) != -1) {
6632 branch_arg = argv[0];
6634 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6635 "unveil", NULL) == -1)
6638 cwd = getcwd(NULL, 0);
6640 error = got_error_from_errno("getcwd");
6644 error = got_worktree_open(&worktree, cwd);
6648 error = check_rebase_or_histedit_in_progress(worktree);
6652 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6657 error = apply_unveil(got_repo_get_path(repo), 0,
6658 got_worktree_get_root_path(worktree));
6662 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6663 error = got_error_from_errno("asprintf");
6667 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6668 &base_branch_ref, worktree, refname, repo);
6672 refname = strdup(got_ref_get_name(branch_ref));
6673 if (refname == NULL) {
6674 error = got_error_from_errno("strdup");
6675 got_worktree_integrate_abort(worktree, fileindex, repo,
6676 branch_ref, base_branch_ref);
6679 base_refname = strdup(got_ref_get_name(base_branch_ref));
6680 if (base_refname == NULL) {
6681 error = got_error_from_errno("strdup");
6682 got_worktree_integrate_abort(worktree, fileindex, repo,
6683 branch_ref, base_branch_ref);
6687 error = got_ref_resolve(&commit_id, repo, branch_ref);
6691 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6695 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6696 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6697 "specified branch has already been integrated");
6698 got_worktree_integrate_abort(worktree, fileindex, repo,
6699 branch_ref, base_branch_ref);
6703 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6705 if (error->code == GOT_ERR_ANCESTRY)
6706 error = got_error(GOT_ERR_REBASE_REQUIRED);
6707 got_worktree_integrate_abort(worktree, fileindex, repo,
6708 branch_ref, base_branch_ref);
6712 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6713 branch_ref, base_branch_ref, update_progress, &did_something,
6714 check_cancelled, NULL);
6718 printf("Integrated %s into %s\n", refname, base_refname);
6721 got_repo_close(repo);
6723 got_worktree_close(worktree);
6725 free(base_commit_id);
6735 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6736 "[file-path ...]\n",
6741 static const struct got_error *
6742 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6743 const char *path, struct got_object_id *blob_id,
6744 struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6746 const struct got_error *err = NULL;
6747 char *id_str = NULL;
6749 if (staged_status != GOT_STATUS_ADD &&
6750 staged_status != GOT_STATUS_MODIFY &&
6751 staged_status != GOT_STATUS_DELETE)
6754 if (staged_status == GOT_STATUS_ADD ||
6755 staged_status == GOT_STATUS_MODIFY)
6756 err = got_object_id_str(&id_str, staged_blob_id);
6758 err = got_object_id_str(&id_str, blob_id);
6762 printf("%s %c %s\n", id_str, staged_status, path);
6767 static const struct got_error *
6768 cmd_stage(int argc, char *argv[])
6770 const struct got_error *error = NULL;
6771 struct got_repository *repo = NULL;
6772 struct got_worktree *worktree = NULL;
6774 struct got_pathlist_head paths;
6775 struct got_pathlist_entry *pe;
6776 int ch, list_stage = 0, pflag = 0;
6777 FILE *patch_script_file = NULL;
6778 const char *patch_script_path = NULL;
6779 struct choose_patch_arg cpa;
6783 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6792 patch_script_path = optarg;
6804 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6805 "unveil", NULL) == -1)
6808 if (list_stage && (pflag || patch_script_path))
6809 errx(1, "-l option cannot be used with other options");
6810 if (patch_script_path && !pflag)
6811 errx(1, "-F option can only be used together with -p option");
6813 cwd = getcwd(NULL, 0);
6815 error = got_error_from_errno("getcwd");
6819 error = got_worktree_open(&worktree, cwd);
6823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6828 if (patch_script_path) {
6829 patch_script_file = fopen(patch_script_path, "r");
6830 if (patch_script_file == NULL) {
6831 error = got_error_from_errno2("fopen",
6836 error = apply_unveil(got_repo_get_path(repo), 0,
6837 got_worktree_get_root_path(worktree));
6841 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6846 error = got_worktree_status(worktree, &paths, repo,
6847 print_stage, NULL, check_cancelled, NULL);
6849 cpa.patch_script_file = patch_script_file;
6850 cpa.action = "stage";
6851 error = got_worktree_stage(worktree, &paths,
6852 pflag ? NULL : print_status, NULL,
6853 pflag ? choose_patch : NULL, &cpa, repo);
6856 if (patch_script_file && fclose(patch_script_file) == EOF &&
6858 error = got_error_from_errno2("fclose", patch_script_path);
6860 got_repo_close(repo);
6862 got_worktree_close(worktree);
6863 TAILQ_FOREACH(pe, &paths, entry)
6864 free((char *)pe->path);
6865 got_pathlist_free(&paths);
6873 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6874 "[file-path ...]\n",
6880 static const struct got_error *
6881 cmd_unstage(int argc, char *argv[])
6883 const struct got_error *error = NULL;
6884 struct got_repository *repo = NULL;
6885 struct got_worktree *worktree = NULL;
6887 struct got_pathlist_head paths;
6888 struct got_pathlist_entry *pe;
6889 int ch, did_something = 0, pflag = 0;
6890 FILE *patch_script_file = NULL;
6891 const char *patch_script_path = NULL;
6892 struct choose_patch_arg cpa;
6896 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6902 patch_script_path = optarg;
6914 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6915 "unveil", NULL) == -1)
6918 if (patch_script_path && !pflag)
6919 errx(1, "-F option can only be used together with -p option");
6921 cwd = getcwd(NULL, 0);
6923 error = got_error_from_errno("getcwd");
6927 error = got_worktree_open(&worktree, cwd);
6931 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6936 if (patch_script_path) {
6937 patch_script_file = fopen(patch_script_path, "r");
6938 if (patch_script_file == NULL) {
6939 error = got_error_from_errno2("fopen",
6945 error = apply_unveil(got_repo_get_path(repo), 0,
6946 got_worktree_get_root_path(worktree));
6950 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6954 cpa.patch_script_file = patch_script_file;
6955 cpa.action = "unstage";
6956 error = got_worktree_unstage(worktree, &paths, update_progress,
6957 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6959 if (patch_script_file && fclose(patch_script_file) == EOF &&
6961 error = got_error_from_errno2("fclose", patch_script_path);
6963 got_repo_close(repo);
6965 got_worktree_close(worktree);
6966 TAILQ_FOREACH(pe, &paths, entry)
6967 free((char *)pe->path);
6968 got_pathlist_free(&paths);
6976 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6977 "arg1 [arg2 ...]\n", getprogname());
6981 static const struct got_error *
6982 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6984 const struct got_error *err;
6985 struct got_blob_object *blob;
6987 err = got_object_open_as_blob(&blob, repo, id, 8192);
6991 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6992 got_object_blob_close(blob);
6996 static const struct got_error *
6997 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6999 const struct got_error *err;
7000 struct got_tree_object *tree;
7003 err = got_object_open_as_tree(&tree, repo, id);
7007 nentries = got_object_tree_get_nentries(tree);
7008 for (i = 0; i < nentries; i++) {
7009 struct got_tree_entry *te;
7011 if (sigint_received || sigpipe_received)
7013 te = got_object_tree_get_entry(tree, i);
7014 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7017 fprintf(outfile, "%s %.7o %s\n", id_str,
7018 got_tree_entry_get_mode(te),
7019 got_tree_entry_get_name(te));
7023 got_object_tree_close(tree);
7027 static const struct got_error *
7028 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7030 const struct got_error *err;
7031 struct got_commit_object *commit;
7032 const struct got_object_id_queue *parent_ids;
7033 struct got_object_qid *pid;
7034 char *id_str = NULL;
7035 const char *logmsg = NULL;
7037 err = got_object_open_as_commit(&commit, repo, id);
7041 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7045 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7046 parent_ids = got_object_commit_get_parent_ids(commit);
7047 fprintf(outfile, "numparents %d\n",
7048 got_object_commit_get_nparents(commit));
7049 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7051 err = got_object_id_str(&pid_str, pid->id);
7054 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7057 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7058 got_object_commit_get_author(commit),
7059 got_object_commit_get_author_time(commit));
7061 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7062 got_object_commit_get_author(commit),
7063 got_object_commit_get_committer_time(commit));
7065 logmsg = got_object_commit_get_logmsg_raw(commit);
7066 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7067 fprintf(outfile, "%s", logmsg);
7070 got_object_commit_close(commit);
7074 static const struct got_error *
7075 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7077 const struct got_error *err;
7078 struct got_tag_object *tag;
7079 char *id_str = NULL;
7080 const char *tagmsg = NULL;
7082 err = got_object_open_as_tag(&tag, repo, id);
7086 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7090 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7092 switch (got_object_tag_get_object_type(tag)) {
7093 case GOT_OBJ_TYPE_BLOB:
7094 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7095 GOT_OBJ_LABEL_BLOB);
7097 case GOT_OBJ_TYPE_TREE:
7098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7099 GOT_OBJ_LABEL_TREE);
7101 case GOT_OBJ_TYPE_COMMIT:
7102 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7103 GOT_OBJ_LABEL_COMMIT);
7105 case GOT_OBJ_TYPE_TAG:
7106 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7113 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7114 got_object_tag_get_name(tag));
7116 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7117 got_object_tag_get_tagger(tag),
7118 got_object_tag_get_tagger_time(tag));
7120 tagmsg = got_object_tag_get_message(tag);
7121 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7122 fprintf(outfile, "%s", tagmsg);
7125 got_object_tag_close(tag);
7129 static const struct got_error *
7130 cmd_cat(int argc, char *argv[])
7132 const struct got_error *error;
7133 struct got_repository *repo = NULL;
7134 struct got_worktree *worktree = NULL;
7135 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7136 const char *commit_id_str = NULL;
7137 struct got_object_id *id = NULL, *commit_id = NULL;
7138 int ch, obj_type, i, force_path = 0;
7141 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7146 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7149 commit_id_str = optarg;
7152 repo_path = realpath(optarg, NULL);
7153 if (repo_path == NULL)
7154 return got_error_from_errno2("realpath",
7156 got_path_strip_trailing_slashes(repo_path);
7170 cwd = getcwd(NULL, 0);
7172 error = got_error_from_errno("getcwd");
7175 error = got_worktree_open(&worktree, cwd);
7176 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7179 if (repo_path == NULL) {
7181 got_worktree_get_repo_path(worktree));
7182 if (repo_path == NULL) {
7183 error = got_error_from_errno("strdup");
7189 if (repo_path == NULL) {
7190 repo_path = getcwd(NULL, 0);
7191 if (repo_path == NULL)
7192 return got_error_from_errno("getcwd");
7195 error = got_repo_open(&repo, repo_path, NULL);
7200 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7204 if (commit_id_str == NULL)
7205 commit_id_str = GOT_REF_HEAD;
7206 error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7210 for (i = 0; i < argc; i++) {
7212 error = got_object_id_by_path(&id, repo, commit_id,
7217 error = match_object_id(&id, &label, argv[i],
7218 GOT_OBJ_TYPE_ANY, 0, repo);
7220 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7221 error->code != GOT_ERR_NOT_REF)
7223 error = got_object_id_by_path(&id, repo,
7224 commit_id, argv[i]);
7230 error = got_object_get_type(&obj_type, repo, id);
7235 case GOT_OBJ_TYPE_BLOB:
7236 error = cat_blob(id, repo, stdout);
7238 case GOT_OBJ_TYPE_TREE:
7239 error = cat_tree(id, repo, stdout);
7241 case GOT_OBJ_TYPE_COMMIT:
7242 error = cat_commit(id, repo, stdout);
7244 case GOT_OBJ_TYPE_TAG:
7245 error = cat_tag(id, repo, stdout);
7248 error = got_error(GOT_ERR_OBJ_TYPE);
7264 got_worktree_close(worktree);
7266 const struct got_error *repo_error;
7267 repo_error = got_repo_close(repo);