2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
19 #include <sys/types.h>
21 #include <sys/param.h>
41 #include "got_version.h"
42 #include "got_error.h"
43 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository.h"
47 #include "got_cancel.h"
48 #include "got_worktree.h"
50 #include "got_commit_graph.h"
51 #include "got_blame.h"
52 #include "got_privsep.h"
53 #include "got_opentemp.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 static volatile sig_atomic_t sigint_received;
60 static volatile sig_atomic_t sigpipe_received;
63 catch_sigint(int signo)
69 catch_sigpipe(int signo)
77 const struct got_error *(*cmd_main)(int, char *[]);
78 void (*cmd_usage)(void);
79 const char *cmd_alias;
82 __dead static void usage(int);
83 __dead static void usage_init(void);
84 __dead static void usage_import(void);
85 __dead static void usage_checkout(void);
86 __dead static void usage_update(void);
87 __dead static void usage_log(void);
88 __dead static void usage_diff(void);
89 __dead static void usage_blame(void);
90 __dead static void usage_tree(void);
91 __dead static void usage_status(void);
92 __dead static void usage_ref(void);
93 __dead static void usage_branch(void);
94 __dead static void usage_tag(void);
95 __dead static void usage_add(void);
96 __dead static void usage_remove(void);
97 __dead static void usage_revert(void);
98 __dead static void usage_commit(void);
99 __dead static void usage_cherrypick(void);
100 __dead static void usage_backout(void);
101 __dead static void usage_rebase(void);
102 __dead static void usage_histedit(void);
103 __dead static void usage_integrate(void);
104 __dead static void usage_stage(void);
105 __dead static void usage_unstage(void);
106 __dead static void usage_cat(void);
108 static const struct got_error* cmd_init(int, char *[]);
109 static const struct got_error* cmd_import(int, char *[]);
110 static const struct got_error* cmd_checkout(int, char *[]);
111 static const struct got_error* cmd_update(int, char *[]);
112 static const struct got_error* cmd_log(int, char *[]);
113 static const struct got_error* cmd_diff(int, char *[]);
114 static const struct got_error* cmd_blame(int, char *[]);
115 static const struct got_error* cmd_tree(int, char *[]);
116 static const struct got_error* cmd_status(int, char *[]);
117 static const struct got_error* cmd_ref(int, char *[]);
118 static const struct got_error* cmd_branch(int, char *[]);
119 static const struct got_error* cmd_tag(int, char *[]);
120 static const struct got_error* cmd_add(int, char *[]);
121 static const struct got_error* cmd_remove(int, char *[]);
122 static const struct got_error* cmd_revert(int, char *[]);
123 static const struct got_error* cmd_commit(int, char *[]);
124 static const struct got_error* cmd_cherrypick(int, char *[]);
125 static const struct got_error* cmd_backout(int, char *[]);
126 static const struct got_error* cmd_rebase(int, char *[]);
127 static const struct got_error* cmd_histedit(int, char *[]);
128 static const struct got_error* cmd_integrate(int, char *[]);
129 static const struct got_error* cmd_stage(int, char *[]);
130 static const struct got_error* cmd_unstage(int, char *[]);
131 static const struct got_error* cmd_cat(int, char *[]);
133 static struct got_cmd got_commands[] = {
134 { "init", cmd_init, usage_init, "in" },
135 { "import", cmd_import, usage_import, "im" },
136 { "checkout", cmd_checkout, usage_checkout, "co" },
137 { "update", cmd_update, usage_update, "up" },
138 { "log", cmd_log, usage_log, "" },
139 { "diff", cmd_diff, usage_diff, "di" },
140 { "blame", cmd_blame, usage_blame, "bl" },
141 { "tree", cmd_tree, usage_tree, "tr" },
142 { "status", cmd_status, usage_status, "st" },
143 { "ref", cmd_ref, usage_ref, "" },
144 { "branch", cmd_branch, usage_branch, "br" },
145 { "tag", cmd_tag, usage_tag, "" },
146 { "add", cmd_add, usage_add, "" },
147 { "remove", cmd_remove, usage_remove, "rm" },
148 { "revert", cmd_revert, usage_revert, "rv" },
149 { "commit", cmd_commit, usage_commit, "ci" },
150 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 { "backout", cmd_backout, usage_backout, "bo" },
152 { "rebase", cmd_rebase, usage_rebase, "rb" },
153 { "histedit", cmd_histedit, usage_histedit, "he" },
154 { "integrate", cmd_integrate, usage_integrate,"ig" },
155 { "stage", cmd_stage, usage_stage, "sg" },
156 { "unstage", cmd_unstage, usage_unstage, "ug" },
157 { "cat", cmd_cat, usage_cat, "" },
165 fprintf(stderr, "commands:");
166 for (i = 0; i < nitems(got_commands); i++) {
167 struct got_cmd *cmd = &got_commands[i];
168 fprintf(stderr, " %s", cmd->cmd_name);
174 main(int argc, char *argv[])
179 int hflag = 0, Vflag = 0;
180 static struct option longopts[] = {
181 { "version", no_argument, NULL, 'V' },
185 setlocale(LC_CTYPE, "");
187 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
206 got_version_print_str();
213 signal(SIGINT, catch_sigint);
214 signal(SIGPIPE, catch_sigpipe);
216 for (i = 0; i < nitems(got_commands); i++) {
217 const struct got_error *error;
219 cmd = &got_commands[i];
221 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 strcmp(cmd->cmd_alias, argv[0]) != 0)
226 got_commands[i].cmd_usage();
228 error = got_commands[i].cmd_main(argc, argv);
229 if (error && error->code != GOT_ERR_CANCELLED &&
230 error->code != GOT_ERR_PRIVSEP_EXIT &&
231 !(sigpipe_received &&
232 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
234 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
242 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
250 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
257 static const struct got_error *
258 get_editor(char **abspath)
260 const struct got_error *err = NULL;
265 editor = getenv("VISUAL");
267 editor = getenv("EDITOR");
270 err = got_path_find_prog(abspath, editor);
275 if (*abspath == NULL) {
276 *abspath = strdup("/bin/ed");
277 if (*abspath == NULL)
278 return got_error_from_errno("strdup");
284 static const struct got_error *
285 apply_unveil(const char *repo_path, int repo_read_only,
286 const char *worktree_path)
288 const struct got_error *err;
291 if (unveil("gmon.out", "rwc") != 0)
292 return got_error_from_errno2("unveil", "gmon.out");
294 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 return got_error_from_errno2("unveil", repo_path);
297 if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 return got_error_from_errno2("unveil", worktree_path);
300 if (unveil("/tmp", "rwc") != 0)
301 return got_error_from_errno2("unveil", "/tmp");
303 err = got_privsep_unveil_exec_helpers();
307 if (unveil(NULL, NULL) != 0)
308 return got_error_from_errno("unveil");
316 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
320 static const struct got_error *
321 cmd_init(int argc, char *argv[])
323 const struct got_error *error = NULL;
324 char *repo_path = NULL;
327 while ((ch = getopt(argc, argv, "")) != -1) {
339 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 repo_path = strdup(argv[0]);
346 if (repo_path == NULL)
347 return got_error_from_errno("strdup");
349 got_path_strip_trailing_slashes(repo_path);
351 error = got_path_mkdir(repo_path);
353 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
356 error = apply_unveil(repo_path, 0, NULL);
360 error = got_repo_init(repo_path);
369 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 "[-r repository-path] [-I pattern] path\n", getprogname());
375 spawn_editor(const char *editor, const char *file)
378 sig_t sighup, sigint, sigquit;
381 sighup = signal(SIGHUP, SIG_IGN);
382 sigint = signal(SIGINT, SIG_IGN);
383 sigquit = signal(SIGQUIT, SIG_IGN);
385 switch (pid = fork()) {
389 execl(editor, editor, file, (char *)NULL);
393 while (waitpid(pid, &st, 0) == -1)
398 (void)signal(SIGHUP, sighup);
399 (void)signal(SIGINT, sigint);
400 (void)signal(SIGQUIT, sigquit);
402 if (!WIFEXITED(st)) {
407 return WEXITSTATUS(st);
410 static const struct got_error *
411 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 const char *initial_content)
414 const struct got_error *err = NULL;
418 int content_changed = 0;
423 if (stat(logmsg_path, &st) == -1)
424 return got_error_from_errno2("stat", logmsg_path);
426 if (spawn_editor(editor, logmsg_path) == -1)
427 return got_error_from_errno("failed spawning editor");
429 if (stat(logmsg_path, &st2) == -1)
430 return got_error_from_errno("stat");
432 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 "no changes made to commit message, aborting");
436 *logmsg = malloc(st2.st_size + 1);
438 return got_error_from_errno("malloc");
442 fp = fopen(logmsg_path, "r");
444 err = got_error_from_errno("fopen");
447 while (fgets(buf, sizeof(buf), fp) != NULL) {
448 if (!content_changed && strcmp(buf, initial_content) != 0)
450 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 continue; /* remove comments and leading empty lines */
452 len = strlcat(*logmsg, buf, st2.st_size);
456 while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 (*logmsg)[len - 1] = '\0';
461 if (len == 0 || !content_changed)
462 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 "commit message cannot be empty, aborting");
472 static const struct got_error *
473 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 const char *path_dir, const char *branch_name)
476 char *initial_content = NULL;
477 const struct got_error *err = NULL;
480 if (asprintf(&initial_content,
481 "\n# %s to be imported to branch %s\n", path_dir,
483 return got_error_from_errno("asprintf");
485 err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
489 dprintf(fd, initial_content);
492 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 free(initial_content);
498 static const struct got_error *
499 import_progress(void *arg, const char *path)
501 printf("A %s\n", path);
505 static const struct got_error *
506 get_author(char **author, struct got_repository *repo)
508 const struct got_error *err = NULL;
509 const char *got_author, *name, *email;
513 name = got_repo_get_gitconfig_author_name(repo);
514 email = got_repo_get_gitconfig_author_email(repo);
516 if (asprintf(author, "%s <%s>", name, email) == -1)
517 return got_error_from_errno("asprintf");
521 got_author = getenv("GOT_AUTHOR");
522 if (got_author == NULL) {
523 name = got_repo_get_global_gitconfig_author_name(repo);
524 email = got_repo_get_global_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
530 /* TODO: Look up user in password database? */
531 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
534 *author = strdup(got_author);
536 return got_error_from_errno("strdup");
539 * Really dumb email address check; we're only doing this to
540 * avoid git's object parser breaking on commits we create.
542 while (*got_author && *got_author != '<')
544 if (*got_author != '<') {
545 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
548 while (*got_author && *got_author != '@')
550 if (*got_author != '@') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 while (*got_author && *got_author != '>')
556 if (*got_author != '>')
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
566 static const struct got_error *
567 get_gitconfig_path(char **gitconfig_path)
569 const char *homedir = getenv("HOME");
571 *gitconfig_path = NULL;
573 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 return got_error_from_errno("asprintf");
580 static const struct got_error *
581 cmd_import(int argc, char *argv[])
583 const struct got_error *error = NULL;
584 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 const char *branch_name = "main";
587 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 struct got_repository *repo = NULL;
589 struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 struct got_object_id *new_commit_id = NULL;
592 struct got_pathlist_head ignores;
593 struct got_pathlist_entry *pe;
594 int preserve_logmsg = 0;
596 TAILQ_INIT(&ignores);
598 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
601 branch_name = optarg;
604 logmsg = strdup(optarg);
605 if (logmsg == NULL) {
606 error = got_error_from_errno("strdup");
611 repo_path = realpath(optarg, NULL);
612 if (repo_path == NULL) {
613 error = got_error_from_errno2("realpath",
619 if (optarg[0] == '\0')
621 error = got_pathlist_insert(&pe, &ignores, optarg,
636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
644 if (repo_path == NULL) {
645 repo_path = getcwd(NULL, 0);
646 if (repo_path == NULL)
647 return got_error_from_errno("getcwd");
649 got_path_strip_trailing_slashes(repo_path);
650 error = get_gitconfig_path(&gitconfig_path);
653 error = got_repo_open(&repo, repo_path, gitconfig_path);
657 error = get_author(&author, repo);
662 * Don't let the user create a branch name with a leading '-'.
663 * While technically a valid reference name, this case is usually
664 * an unintended typo.
666 if (branch_name[0] == '-')
667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 error = got_error_from_errno("asprintf");
674 error = got_ref_open(&branch_ref, repo, refname, 0);
676 if (error->code != GOT_ERR_NOT_REF)
679 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 "import target branch already exists");
684 path_dir = realpath(argv[0], NULL);
685 if (path_dir == NULL) {
686 error = got_error_from_errno2("realpath", argv[0]);
689 got_path_strip_trailing_slashes(path_dir);
692 * unveil(2) traverses exec(2); if an editor is used we have
693 * to apply unveil after the log message has been written.
695 if (logmsg == NULL || strlen(logmsg) == 0) {
696 error = get_editor(&editor);
700 error = collect_import_msg(&logmsg, &logmsg_path, editor,
703 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 if (unveil(path_dir, "r") != 0) {
711 error = got_error_from_errno2("unveil", path_dir);
717 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 author, &ignores, repo, import_progress, NULL);
732 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 error = got_ref_write(branch_ref, repo);
746 error = got_object_id_str(&id_str, new_commit_id);
753 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 if (error->code != GOT_ERR_NOT_REF) {
761 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
769 error = got_ref_write(head_ref, repo);
777 printf("Created branch %s with commit %s\n",
778 got_ref_get_name(branch_ref), id_str);
780 if (preserve_logmsg) {
781 fprintf(stderr, "%s: log message preserved in %s\n",
782 getprogname(), logmsg_path);
783 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 error = got_error_from_errno2("unlink", logmsg_path);
793 free(gitconfig_path);
795 got_ref_close(branch_ref);
797 got_ref_close(head_ref);
804 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 "[-p prefix] repository-path [worktree-path]\n", getprogname());
810 show_worktree_base_ref_warning(void)
812 fprintf(stderr, "%s: warning: could not create a reference "
813 "to the work tree's base commit; the commit could be "
814 "garbage-collected by Git; making the repository "
815 "writable and running 'got update' will prevent this\n",
819 struct got_checkout_progress_arg {
820 const char *worktree_path;
821 int had_base_commit_ref_error;
824 static const struct got_error *
825 checkout_progress(void *arg, unsigned char status, const char *path)
827 struct got_checkout_progress_arg *a = arg;
829 /* Base commit bump happens silently. */
830 if (status == GOT_STATUS_BUMP_BASE)
833 if (status == GOT_STATUS_BASE_REF_ERR) {
834 a->had_base_commit_ref_error = 1;
838 while (path[0] == '/')
841 printf("%c %s/%s\n", status, a->worktree_path, path);
845 static const struct got_error *
846 check_cancelled(void *arg)
848 if (sigint_received || sigpipe_received)
849 return got_error(GOT_ERR_CANCELLED);
853 static const struct got_error *
854 check_linear_ancestry(struct got_object_id *commit_id,
855 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 struct got_repository *repo)
858 const struct got_error *err = NULL;
859 struct got_object_id *yca_id;
861 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 commit_id, base_commit_id, repo, check_cancelled, NULL);
867 return got_error(GOT_ERR_ANCESTRY);
870 * Require a straight line of history between the target commit
871 * and the work tree's base commit.
873 * Non-linear situations such as this require a rebase:
875 * (commit) D F (base_commit)
883 * 'got update' only handles linear cases:
884 * Update forwards in time: A (base/yca) - B - C - D (commit)
885 * Update backwards in time: D (base) - C - B - A (commit/yca)
887 if (allow_forwards_in_time_only) {
888 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 return got_error(GOT_ERR_ANCESTRY);
890 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 got_object_id_cmp(base_commit_id, yca_id) != 0)
892 return got_error(GOT_ERR_ANCESTRY);
898 static const struct got_error *
899 check_same_branch(struct got_object_id *commit_id,
900 struct got_reference *head_ref, struct got_object_id *yca_id,
901 struct got_repository *repo)
903 const struct got_error *err = NULL;
904 struct got_commit_graph *graph = NULL;
905 struct got_object_id *head_commit_id = NULL;
906 int is_same_branch = 0;
908 err = got_ref_resolve(&head_commit_id, repo, head_ref);
912 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
916 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
921 err = got_commit_graph_open(&graph, "/", 1);
925 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 check_cancelled, NULL);
931 struct got_object_id *id;
932 err = got_commit_graph_iter_next(&id, graph, repo,
933 check_cancelled, NULL);
935 if (err->code == GOT_ERR_ITER_COMPLETED)
941 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 if (got_object_id_cmp(id, commit_id) == 0) {
951 got_commit_graph_close(graph);
952 free(head_commit_id);
953 if (!err && !is_same_branch)
954 err = got_error(GOT_ERR_ANCESTRY);
958 static const struct got_error *
959 cmd_checkout(int argc, char *argv[])
961 const struct got_error *error = NULL;
962 struct got_repository *repo = NULL;
963 struct got_reference *head_ref = NULL;
964 struct got_worktree *worktree = NULL;
965 char *repo_path = NULL;
966 char *worktree_path = NULL;
967 const char *path_prefix = "";
968 const char *branch_name = GOT_REF_HEAD;
969 char *commit_id_str = NULL;
970 int ch, same_path_prefix, allow_nonempty = 0;
971 struct got_pathlist_head paths;
972 struct got_checkout_progress_arg cpa;
976 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
979 branch_name = optarg;
982 commit_id_str = strdup(optarg);
983 if (commit_id_str == NULL)
984 return got_error_from_errno("strdup");
990 path_prefix = optarg;
1002 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1003 "unveil", NULL) == -1)
1007 char *cwd, *base, *dotgit;
1008 repo_path = realpath(argv[0], NULL);
1009 if (repo_path == NULL)
1010 return got_error_from_errno2("realpath", argv[0]);
1011 cwd = getcwd(NULL, 0);
1013 error = got_error_from_errno("getcwd");
1016 if (path_prefix[0]) {
1017 base = basename(path_prefix);
1019 error = got_error_from_errno2("basename",
1024 base = basename(repo_path);
1026 error = got_error_from_errno2("basename",
1031 dotgit = strstr(base, ".git");
1034 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1035 error = got_error_from_errno("asprintf");
1040 } else if (argc == 2) {
1041 repo_path = realpath(argv[0], NULL);
1042 if (repo_path == NULL) {
1043 error = got_error_from_errno2("realpath", argv[0]);
1046 worktree_path = realpath(argv[1], NULL);
1047 if (worktree_path == NULL) {
1048 if (errno != ENOENT) {
1049 error = got_error_from_errno2("realpath",
1053 worktree_path = strdup(argv[1]);
1054 if (worktree_path == NULL) {
1055 error = got_error_from_errno("strdup");
1062 got_path_strip_trailing_slashes(repo_path);
1063 got_path_strip_trailing_slashes(worktree_path);
1065 error = got_repo_open(&repo, repo_path, NULL);
1069 /* Pre-create work tree path for unveil(2) */
1070 error = got_path_mkdir(worktree_path);
1072 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1073 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1075 if (!allow_nonempty &&
1076 !got_path_dir_is_empty(worktree_path)) {
1077 error = got_error_path(worktree_path,
1078 GOT_ERR_DIR_NOT_EMPTY);
1083 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1087 error = got_ref_open(&head_ref, repo, branch_name, 0);
1091 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1092 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1095 error = got_worktree_open(&worktree, worktree_path);
1099 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1103 if (!same_path_prefix) {
1104 error = got_error(GOT_ERR_PATH_PREFIX);
1108 if (commit_id_str) {
1109 struct got_object_id *commit_id;
1110 error = got_repo_match_object_id(&commit_id, NULL,
1111 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1114 error = check_linear_ancestry(commit_id,
1115 got_worktree_get_base_commit_id(worktree), 0, repo);
1116 if (error != NULL) {
1120 error = check_same_branch(commit_id, head_ref, NULL, repo);
1123 error = got_worktree_set_base_commit_id(worktree, repo,
1130 error = got_pathlist_append(&paths, "", NULL);
1133 cpa.worktree_path = worktree_path;
1134 cpa.had_base_commit_ref_error = 0;
1135 error = got_worktree_checkout_files(worktree, &paths, repo,
1136 checkout_progress, &cpa, check_cancelled, NULL);
1140 printf("Now shut up and hack\n");
1141 if (cpa.had_base_commit_ref_error)
1142 show_worktree_base_ref_warning();
1144 got_pathlist_free(&paths);
1145 free(commit_id_str);
1147 free(worktree_path);
1154 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1159 static const struct got_error *
1160 update_progress(void *arg, unsigned char status, const char *path)
1162 int *did_something = arg;
1164 if (status == GOT_STATUS_EXISTS ||
1165 status == GOT_STATUS_BASE_REF_ERR)
1170 /* Base commit bump happens silently. */
1171 if (status == GOT_STATUS_BUMP_BASE)
1174 while (path[0] == '/')
1176 printf("%c %s\n", status, path);
1180 static const struct got_error *
1181 switch_head_ref(struct got_reference *head_ref,
1182 struct got_object_id *commit_id, struct got_worktree *worktree,
1183 struct got_repository *repo)
1185 const struct got_error *err = NULL;
1187 int ref_has_moved = 0;
1189 /* Trivial case: switching between two different references. */
1190 if (strcmp(got_ref_get_name(head_ref),
1191 got_worktree_get_head_ref_name(worktree)) != 0) {
1192 printf("Switching work tree from %s to %s\n",
1193 got_worktree_get_head_ref_name(worktree),
1194 got_ref_get_name(head_ref));
1195 return got_worktree_set_head_ref(worktree, head_ref);
1198 err = check_linear_ancestry(commit_id,
1199 got_worktree_get_base_commit_id(worktree), 0, repo);
1201 if (err->code != GOT_ERR_ANCESTRY)
1208 /* Switching to a rebased branch with the same reference name. */
1209 err = got_object_id_str(&base_id_str,
1210 got_worktree_get_base_commit_id(worktree));
1213 printf("Reference %s now points at a different branch\n",
1214 got_worktree_get_head_ref_name(worktree));
1215 printf("Switching work tree from %s to %s\n", base_id_str,
1216 got_worktree_get_head_ref_name(worktree));
1220 static const struct got_error *
1221 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1223 const struct got_error *err;
1226 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1230 return got_error(GOT_ERR_REBASING);
1232 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1236 return got_error(GOT_ERR_HISTEDIT_BUSY);
1241 static const struct got_error *
1242 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1243 char *argv[], struct got_worktree *worktree)
1245 const struct got_error *err = NULL;
1252 return got_error_from_errno("strdup");
1253 return got_pathlist_append(paths, path, NULL);
1256 for (i = 0; i < argc; i++) {
1257 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1260 err = got_pathlist_append(paths, path, NULL);
1270 static const struct got_error *
1271 cmd_update(int argc, char *argv[])
1273 const struct got_error *error = NULL;
1274 struct got_repository *repo = NULL;
1275 struct got_worktree *worktree = NULL;
1276 char *worktree_path = NULL;
1277 struct got_object_id *commit_id = NULL;
1278 char *commit_id_str = NULL;
1279 const char *branch_name = NULL;
1280 struct got_reference *head_ref = NULL;
1281 struct got_pathlist_head paths;
1282 struct got_pathlist_entry *pe;
1283 int ch, did_something = 0;
1287 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1290 branch_name = optarg;
1293 commit_id_str = strdup(optarg);
1294 if (commit_id_str == NULL)
1295 return got_error_from_errno("strdup");
1307 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1308 "unveil", NULL) == -1)
1311 worktree_path = getcwd(NULL, 0);
1312 if (worktree_path == NULL) {
1313 error = got_error_from_errno("getcwd");
1316 error = got_worktree_open(&worktree, worktree_path);
1320 error = check_rebase_or_histedit_in_progress(worktree);
1324 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1329 error = apply_unveil(got_repo_get_path(repo), 0,
1330 got_worktree_get_root_path(worktree));
1334 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1338 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1339 got_worktree_get_head_ref_name(worktree), 0);
1342 if (commit_id_str == NULL) {
1343 error = got_ref_resolve(&commit_id, repo, head_ref);
1346 error = got_object_id_str(&commit_id_str, commit_id);
1350 error = got_repo_match_object_id(&commit_id, NULL,
1351 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1352 free(commit_id_str);
1353 commit_id_str = NULL;
1356 error = got_object_id_str(&commit_id_str, commit_id);
1362 struct got_object_id *head_commit_id;
1363 TAILQ_FOREACH(pe, &paths, entry) {
1364 if (pe->path_len == 0)
1366 error = got_error_msg(GOT_ERR_BAD_PATH,
1367 "switching between branches requires that "
1368 "the entire work tree gets updated");
1371 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1374 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1376 free(head_commit_id);
1379 error = check_same_branch(commit_id, head_ref, NULL, repo);
1382 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1386 error = check_linear_ancestry(commit_id,
1387 got_worktree_get_base_commit_id(worktree), 0, repo);
1388 if (error != NULL) {
1389 if (error->code == GOT_ERR_ANCESTRY)
1390 error = got_error(GOT_ERR_BRANCH_MOVED);
1393 error = check_same_branch(commit_id, head_ref, NULL, repo);
1398 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1400 error = got_worktree_set_base_commit_id(worktree, repo,
1406 error = got_worktree_checkout_files(worktree, &paths, repo,
1407 update_progress, &did_something, check_cancelled, NULL);
1412 printf("Updated to commit %s\n", commit_id_str);
1414 printf("Already up-to-date\n");
1416 free(worktree_path);
1417 TAILQ_FOREACH(pe, &paths, entry)
1418 free((char *)pe->path);
1419 got_pathlist_free(&paths);
1421 free(commit_id_str);
1425 static const struct got_error *
1426 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1427 const char *path, int diff_context, int ignore_whitespace,
1428 struct got_repository *repo)
1430 const struct got_error *err = NULL;
1431 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1434 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1439 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1443 while (path[0] == '/')
1445 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1446 ignore_whitespace, stdout);
1449 got_object_blob_close(blob1);
1450 got_object_blob_close(blob2);
1454 static const struct got_error *
1455 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1456 const char *path, int diff_context, int ignore_whitespace,
1457 struct got_repository *repo)
1459 const struct got_error *err = NULL;
1460 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1461 struct got_diff_blob_output_unidiff_arg arg;
1464 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1469 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1473 arg.diff_context = diff_context;
1474 arg.ignore_whitespace = ignore_whitespace;
1475 arg.outfile = stdout;
1476 while (path[0] == '/')
1478 err = got_diff_tree(tree1, tree2, path, path, repo,
1479 got_diff_blob_output_unidiff, &arg, 1);
1482 got_object_tree_close(tree1);
1484 got_object_tree_close(tree2);
1488 static const struct got_error *
1489 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1490 const char *path, int diff_context, struct got_repository *repo)
1492 const struct got_error *err = NULL;
1493 struct got_commit_object *pcommit = NULL;
1494 char *id_str1 = NULL, *id_str2 = NULL;
1495 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1496 struct got_object_qid *qid;
1498 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1500 err = got_object_open_as_commit(&pcommit, repo,
1506 if (path && path[0] != '\0') {
1508 err = got_object_id_by_path(&obj_id2, repo, id, path);
1511 err = got_object_id_str(&id_str2, obj_id2);
1517 err = got_object_id_by_path(&obj_id1, repo,
1523 err = got_object_id_str(&id_str1, obj_id1);
1529 err = got_object_get_type(&obj_type, repo, obj_id2);
1534 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1536 case GOT_OBJ_TYPE_BLOB:
1537 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1540 case GOT_OBJ_TYPE_TREE:
1541 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1545 err = got_error(GOT_ERR_OBJ_TYPE);
1551 obj_id2 = got_object_commit_get_tree_id(commit);
1552 err = got_object_id_str(&id_str2, obj_id2);
1555 obj_id1 = got_object_commit_get_tree_id(pcommit);
1556 err = got_object_id_str(&id_str1, obj_id1);
1559 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1560 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1566 got_object_commit_close(pcommit);
1571 get_datestr(time_t *time, char *datebuf)
1573 struct tm mytm, *tm;
1576 tm = gmtime_r(time, &mytm);
1579 s = asctime_r(tm, datebuf);
1582 p = strchr(s, '\n');
1588 static const struct got_error *
1589 match_logmsg(int *have_match, struct got_object_id *id,
1590 struct got_commit_object *commit, regex_t *regex)
1592 const struct got_error *err = NULL;
1593 regmatch_t regmatch;
1594 char *id_str = NULL, *logmsg = NULL;
1598 err = got_object_id_str(&id_str, id);
1602 err = got_object_commit_get_logmsg(&logmsg, commit);
1606 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1614 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1616 static const struct got_error *
1617 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1618 struct got_repository *repo, const char *path, int show_patch,
1619 int diff_context, struct got_reflist_head *refs)
1621 const struct got_error *err = NULL;
1622 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1624 time_t committer_time;
1625 const char *author, *committer;
1626 char *refs_str = NULL;
1627 struct got_reflist_entry *re;
1629 SIMPLEQ_FOREACH(re, refs, entry) {
1632 struct got_tag_object *tag = NULL;
1635 name = got_ref_get_name(re->ref);
1636 if (strcmp(name, GOT_REF_HEAD) == 0)
1638 if (strncmp(name, "refs/", 5) == 0)
1640 if (strncmp(name, "got/", 4) == 0)
1642 if (strncmp(name, "heads/", 6) == 0)
1644 if (strncmp(name, "remotes/", 8) == 0)
1646 if (strncmp(name, "tags/", 5) == 0) {
1647 err = got_object_open_as_tag(&tag, repo, re->id);
1649 if (err->code != GOT_ERR_OBJ_TYPE)
1651 /* Ref points at something other than a tag. */
1656 cmp = got_object_id_cmp(tag ?
1657 got_object_tag_get_object_id(tag) : re->id, id);
1659 got_object_tag_close(tag);
1663 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1665 err = got_error_from_errno("asprintf");
1671 err = got_object_id_str(&id_str, id);
1675 printf(GOT_COMMIT_SEP_STR);
1676 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1677 refs_str ? refs_str : "", refs_str ? ")" : "");
1682 printf("from: %s\n", got_object_commit_get_author(commit));
1683 committer_time = got_object_commit_get_committer_time(commit);
1684 datestr = get_datestr(&committer_time, datebuf);
1686 printf("date: %s UTC\n", datestr);
1687 author = got_object_commit_get_author(commit);
1688 committer = got_object_commit_get_committer(commit);
1689 if (strcmp(author, committer) != 0)
1690 printf("via: %s\n", committer);
1691 if (got_object_commit_get_nparents(commit) > 1) {
1692 const struct got_object_id_queue *parent_ids;
1693 struct got_object_qid *qid;
1695 parent_ids = got_object_commit_get_parent_ids(commit);
1696 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1697 err = got_object_id_str(&id_str, qid->id);
1700 printf("parent %d: %s\n", n++, id_str);
1705 err = got_object_commit_get_logmsg(&logmsg0, commit);
1711 line = strsep(&logmsg, "\n");
1713 printf(" %s\n", line);
1718 err = print_patch(commit, id, path, diff_context, repo);
1723 if (fflush(stdout) != 0 && err == NULL)
1724 err = got_error_from_errno("fflush");
1728 static const struct got_error *
1729 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1730 const char *path, int show_patch, const char *search_pattern,
1731 int diff_context, int limit, int first_parent_traversal,
1732 struct got_reflist_head *refs)
1734 const struct got_error *err;
1735 struct got_commit_graph *graph;
1739 if (search_pattern &&
1740 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1741 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1743 err = got_commit_graph_open(&graph, path, first_parent_traversal);
1746 err = got_commit_graph_iter_start(graph, root_id, repo,
1747 check_cancelled, NULL);
1751 struct got_commit_object *commit;
1752 struct got_object_id *id;
1754 if (sigint_received || sigpipe_received)
1757 err = got_commit_graph_iter_next(&id, graph, repo,
1758 check_cancelled, NULL);
1760 if (err->code == GOT_ERR_ITER_COMPLETED)
1767 err = got_object_open_as_commit(&commit, repo, id);
1771 if (search_pattern) {
1772 err = match_logmsg(&have_match, id, commit, ®ex);
1774 got_object_commit_close(commit);
1777 if (have_match == 0) {
1778 got_object_commit_close(commit);
1783 err = print_commit(commit, id, repo, path, show_patch,
1784 diff_context, refs);
1785 got_object_commit_close(commit);
1786 if (err || (limit && --limit == 0))
1792 got_commit_graph_close(graph);
1799 fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1800 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1805 get_default_log_limit(void)
1807 const char *got_default_log_limit;
1811 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1812 if (got_default_log_limit == NULL)
1814 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1820 static const struct got_error *
1821 cmd_log(int argc, char *argv[])
1823 const struct got_error *error;
1824 struct got_repository *repo = NULL;
1825 struct got_worktree *worktree = NULL;
1826 struct got_commit_object *commit = NULL;
1827 struct got_object_id *id = NULL;
1828 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1829 const char *start_commit = NULL, *search_pattern = NULL;
1830 int diff_context = -1, ch;
1831 int show_patch = 0, limit = 0, first_parent_traversal = 0;
1833 struct got_reflist_head refs;
1835 SIMPLEQ_INIT(&refs);
1838 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1844 limit = get_default_log_limit();
1846 while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1852 start_commit = optarg;
1855 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1858 err(1, "-C option %s", errstr);
1861 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1863 err(1, "-l option %s", errstr);
1866 first_parent_traversal = 1;
1869 repo_path = realpath(optarg, NULL);
1870 if (repo_path == NULL)
1871 return got_error_from_errno2("realpath",
1873 got_path_strip_trailing_slashes(repo_path);
1876 search_pattern = optarg;
1887 if (diff_context == -1)
1889 else if (!show_patch)
1890 errx(1, "-C reguires -p");
1892 cwd = getcwd(NULL, 0);
1894 error = got_error_from_errno("getcwd");
1898 error = got_worktree_open(&worktree, cwd);
1899 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1906 error = got_error_from_errno("strdup");
1909 } else if (argc == 1) {
1911 error = got_worktree_resolve_path(&path, worktree,
1916 path = strdup(argv[0]);
1918 error = got_error_from_errno("strdup");
1925 if (repo_path == NULL) {
1926 repo_path = worktree ?
1927 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1929 if (repo_path == NULL) {
1930 error = got_error_from_errno("strdup");
1934 error = got_repo_open(&repo, repo_path, NULL);
1938 error = apply_unveil(got_repo_get_path(repo), 1,
1939 worktree ? got_worktree_get_root_path(worktree) : NULL);
1943 if (start_commit == NULL) {
1944 struct got_reference *head_ref;
1945 error = got_ref_open(&head_ref, repo,
1946 worktree ? got_worktree_get_head_ref_name(worktree)
1950 error = got_ref_resolve(&id, repo, head_ref);
1951 got_ref_close(head_ref);
1954 error = got_object_open_as_commit(&commit, repo, id);
1956 struct got_reference *ref;
1957 error = got_ref_open(&ref, repo, start_commit, 0);
1958 if (error == NULL) {
1960 error = got_ref_resolve(&id, repo, ref);
1964 error = got_object_get_type(&obj_type, repo, id);
1967 if (obj_type == GOT_OBJ_TYPE_TAG) {
1968 struct got_tag_object *tag;
1969 error = got_object_open_as_tag(&tag, repo, id);
1972 if (got_object_tag_get_object_type(tag) !=
1973 GOT_OBJ_TYPE_COMMIT) {
1974 got_object_tag_close(tag);
1975 error = got_error(GOT_ERR_OBJ_TYPE);
1979 id = got_object_id_dup(
1980 got_object_tag_get_object_id(tag));
1982 error = got_error_from_errno(
1983 "got_object_id_dup");
1984 got_object_tag_close(tag);
1987 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 error = got_error(GOT_ERR_OBJ_TYPE);
1991 error = got_object_open_as_commit(&commit, repo, id);
1995 if (commit == NULL) {
1996 error = got_repo_match_object_id_prefix(&id,
1997 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2006 const char *prefix = got_worktree_get_path_prefix(worktree);
2008 if (asprintf(&p, "%s%s%s", prefix,
2009 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2010 error = got_error_from_errno("asprintf");
2013 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2016 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2021 path = in_repo_path;
2024 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2028 error = print_commits(id, repo, path, show_patch, search_pattern,
2029 diff_context, limit, first_parent_traversal, &refs);
2036 got_worktree_close(worktree);
2038 const struct got_error *repo_error;
2039 repo_error = got_repo_close(repo);
2043 got_ref_list_free(&refs);
2050 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2051 "[-w] [object1 object2 | path]\n", getprogname());
2055 struct print_diff_arg {
2056 struct got_repository *repo;
2057 struct got_worktree *worktree;
2062 int ignore_whitespace;
2065 static const struct got_error *
2066 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2067 const char *path, struct got_object_id *blob_id,
2068 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2069 int dirfd, const char *de_name)
2071 struct print_diff_arg *a = arg;
2072 const struct got_error *err = NULL;
2073 struct got_blob_object *blob1 = NULL;
2076 char *abspath = NULL, *label1 = NULL;
2079 if (a->diff_staged) {
2080 if (staged_status != GOT_STATUS_MODIFY &&
2081 staged_status != GOT_STATUS_ADD &&
2082 staged_status != GOT_STATUS_DELETE)
2085 if (staged_status == GOT_STATUS_DELETE)
2087 if (status == GOT_STATUS_NONEXISTENT)
2088 return got_error_set_errno(ENOENT, path);
2089 if (status != GOT_STATUS_MODIFY &&
2090 status != GOT_STATUS_ADD &&
2091 status != GOT_STATUS_DELETE &&
2092 status != GOT_STATUS_CONFLICT)
2096 if (!a->header_shown) {
2097 printf("diff %s %s%s\n", a->id_str,
2098 got_worktree_get_root_path(a->worktree),
2099 a->diff_staged ? " (staged changes)" : "");
2100 a->header_shown = 1;
2103 if (a->diff_staged) {
2104 const char *label1 = NULL, *label2 = NULL;
2105 switch (staged_status) {
2106 case GOT_STATUS_MODIFY:
2110 case GOT_STATUS_ADD:
2113 case GOT_STATUS_DELETE:
2117 return got_error(GOT_ERR_FILE_STATUS);
2119 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2120 label1, label2, a->diff_context, a->ignore_whitespace,
2124 if (staged_status == GOT_STATUS_ADD ||
2125 staged_status == GOT_STATUS_MODIFY) {
2127 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2131 err = got_object_id_str(&id_str, staged_blob_id);
2134 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2135 err = got_error_from_errno("asprintf");
2140 } else if (status != GOT_STATUS_ADD) {
2141 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2146 if (status != GOT_STATUS_DELETE) {
2147 if (asprintf(&abspath, "%s/%s",
2148 got_worktree_get_root_path(a->worktree), path) == -1) {
2149 err = got_error_from_errno("asprintf");
2154 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2156 err = got_error_from_errno2("openat", abspath);
2160 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2162 err = got_error_from_errno2("open", abspath);
2166 if (fstat(fd, &sb) == -1) {
2167 err = got_error_from_errno2("fstat", abspath);
2170 f2 = fdopen(fd, "r");
2172 err = got_error_from_errno2("fdopen", abspath);
2179 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2180 a->diff_context, a->ignore_whitespace, stdout);
2183 got_object_blob_close(blob1);
2184 if (f2 && fclose(f2) == EOF && err == NULL)
2185 err = got_error_from_errno("fclose");
2186 if (fd != -1 && close(fd) == -1 && err == NULL)
2187 err = got_error_from_errno("close");
2192 static const struct got_error *
2193 cmd_diff(int argc, char *argv[])
2195 const struct got_error *error;
2196 struct got_repository *repo = NULL;
2197 struct got_worktree *worktree = NULL;
2198 char *cwd = NULL, *repo_path = NULL;
2199 struct got_object_id *id1 = NULL, *id2 = NULL;
2200 const char *id_str1 = NULL, *id_str2 = NULL;
2201 char *label1 = NULL, *label2 = NULL;
2203 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2208 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2213 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2216 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2219 err(1, "-C option %s", errstr);
2222 repo_path = realpath(optarg, NULL);
2223 if (repo_path == NULL)
2224 return got_error_from_errno2("realpath",
2226 got_path_strip_trailing_slashes(repo_path);
2232 ignore_whitespace = 1;
2243 cwd = getcwd(NULL, 0);
2245 error = got_error_from_errno("getcwd");
2248 error = got_worktree_open(&worktree, cwd);
2249 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2252 if (worktree == NULL) {
2253 error = got_error(GOT_ERR_NOT_WORKTREE);
2258 "-r option can't be used when diffing a work tree");
2259 repo_path = strdup(got_worktree_get_repo_path(worktree));
2260 if (repo_path == NULL) {
2261 error = got_error_from_errno("strdup");
2265 error = got_worktree_resolve_path(&path, worktree,
2272 error = got_error_from_errno("strdup");
2276 } else if (argc == 2) {
2278 errx(1, "-s option can't be used when diffing "
2279 "objects in repository");
2282 if (worktree && repo_path == NULL) {
2284 strdup(got_worktree_get_repo_path(worktree));
2285 if (repo_path == NULL) {
2286 error = got_error_from_errno("strdup");
2293 if (repo_path == NULL) {
2294 repo_path = getcwd(NULL, 0);
2295 if (repo_path == NULL)
2296 return got_error_from_errno("getcwd");
2299 error = got_repo_open(&repo, repo_path, NULL);
2304 error = apply_unveil(got_repo_get_path(repo), 1,
2305 worktree ? got_worktree_get_root_path(worktree) : NULL);
2310 struct print_diff_arg arg;
2311 struct got_pathlist_head paths;
2316 error = got_object_id_str(&id_str,
2317 got_worktree_get_base_commit_id(worktree));
2321 arg.worktree = worktree;
2322 arg.diff_context = diff_context;
2323 arg.id_str = id_str;
2324 arg.header_shown = 0;
2325 arg.diff_staged = diff_staged;
2326 arg.ignore_whitespace = ignore_whitespace;
2328 error = got_pathlist_append(&paths, path, NULL);
2332 error = got_worktree_status(worktree, &paths, repo, print_diff,
2333 &arg, check_cancelled, NULL);
2335 got_pathlist_free(&paths);
2339 error = got_repo_match_object_id(&id1, &label1, id_str1,
2340 GOT_OBJ_TYPE_ANY, 1, repo);
2344 error = got_repo_match_object_id(&id2, &label2, id_str2,
2345 GOT_OBJ_TYPE_ANY, 1, repo);
2349 error = got_object_get_type(&type1, repo, id1);
2353 error = got_object_get_type(&type2, repo, id2);
2357 if (type1 != type2) {
2358 error = got_error(GOT_ERR_OBJ_TYPE);
2363 case GOT_OBJ_TYPE_BLOB:
2364 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2365 diff_context, ignore_whitespace, repo, stdout);
2367 case GOT_OBJ_TYPE_TREE:
2368 error = got_diff_objects_as_trees(id1, id2, "", "",
2369 diff_context, ignore_whitespace, repo, stdout);
2371 case GOT_OBJ_TYPE_COMMIT:
2372 printf("diff %s %s\n", label1, label2);
2373 error = got_diff_objects_as_commits(id1, id2, diff_context,
2374 ignore_whitespace, repo, stdout);
2377 error = got_error(GOT_ERR_OBJ_TYPE);
2386 got_worktree_close(worktree);
2388 const struct got_error *repo_error;
2389 repo_error = got_repo_close(repo);
2400 "usage: %s blame [-c commit] [-r repository-path] path\n",
2409 char datebuf[11]; /* YYYY-MM-DD + NUL */
2412 struct blame_cb_args {
2413 struct blame_line *lines;
2417 off_t *line_offsets;
2419 struct got_repository *repo;
2422 static const struct got_error *
2423 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2425 const struct got_error *err = NULL;
2426 struct blame_cb_args *a = arg;
2427 struct blame_line *bline;
2429 size_t linesize = 0;
2430 struct got_commit_object *commit = NULL;
2433 time_t committer_time;
2435 if (nlines != a->nlines ||
2436 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2437 return got_error(GOT_ERR_RANGE);
2439 if (sigint_received)
2440 return got_error(GOT_ERR_ITER_COMPLETED);
2443 return NULL; /* no change in this commit */
2445 /* Annotate this line. */
2446 bline = &a->lines[lineno - 1];
2447 if (bline->annotated)
2449 err = got_object_id_str(&bline->id_str, id);
2453 err = got_object_open_as_commit(&commit, a->repo, id);
2457 bline->committer = strdup(got_object_commit_get_committer(commit));
2458 if (bline->committer == NULL) {
2459 err = got_error_from_errno("strdup");
2463 committer_time = got_object_commit_get_committer_time(commit);
2464 if (localtime_r(&committer_time, &tm) == NULL)
2465 return got_error_from_errno("localtime_r");
2466 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2467 &tm) >= sizeof(bline->datebuf)) {
2468 err = got_error(GOT_ERR_NO_SPACE);
2471 bline->annotated = 1;
2473 /* Print lines annotated so far. */
2474 bline = &a->lines[a->lineno_cur - 1];
2475 if (!bline->annotated)
2478 offset = a->line_offsets[a->lineno_cur - 1];
2479 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2480 err = got_error_from_errno("fseeko");
2484 while (bline->annotated) {
2485 char *smallerthan, *at, *nl, *committer;
2488 if (getline(&line, &linesize, a->f) == -1) {
2490 err = got_error_from_errno("getline");
2494 committer = bline->committer;
2495 smallerthan = strchr(committer, '<');
2496 if (smallerthan && smallerthan[1] != '\0')
2497 committer = smallerthan + 1;
2498 at = strchr(committer, '@');
2501 len = strlen(committer);
2503 committer[8] = '\0';
2505 nl = strchr(line, '\n');
2508 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2509 bline->id_str, bline->datebuf, committer, line);
2512 bline = &a->lines[a->lineno_cur - 1];
2516 got_object_commit_close(commit);
2521 static const struct got_error *
2522 cmd_blame(int argc, char *argv[])
2524 const struct got_error *error;
2525 struct got_repository *repo = NULL;
2526 struct got_worktree *worktree = NULL;
2527 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2528 struct got_object_id *obj_id = NULL;
2529 struct got_object_id *commit_id = NULL;
2530 struct got_blob_object *blob = NULL;
2531 char *commit_id_str = NULL;
2532 struct blame_cb_args bca;
2533 int ch, obj_type, i;
2536 memset(&bca, 0, sizeof(bca));
2539 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2544 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2547 commit_id_str = optarg;
2550 repo_path = realpath(optarg, NULL);
2551 if (repo_path == NULL)
2552 return got_error_from_errno2("realpath",
2554 got_path_strip_trailing_slashes(repo_path);
2570 cwd = getcwd(NULL, 0);
2572 error = got_error_from_errno("getcwd");
2575 if (repo_path == NULL) {
2576 error = got_worktree_open(&worktree, cwd);
2577 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2583 strdup(got_worktree_get_repo_path(worktree));
2584 if (repo_path == NULL)
2585 error = got_error_from_errno("strdup");
2589 repo_path = strdup(cwd);
2590 if (repo_path == NULL) {
2591 error = got_error_from_errno("strdup");
2597 error = got_repo_open(&repo, repo_path, NULL);
2601 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2606 const char *prefix = got_worktree_get_path_prefix(worktree);
2607 char *p, *worktree_subdir = cwd +
2608 strlen(got_worktree_get_root_path(worktree));
2609 if (asprintf(&p, "%s%s%s%s%s",
2610 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2611 worktree_subdir, worktree_subdir[0] ? "/" : "",
2613 error = got_error_from_errno("asprintf");
2616 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2619 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2624 if (commit_id_str == NULL) {
2625 struct got_reference *head_ref;
2626 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2629 error = got_ref_resolve(&commit_id, repo, head_ref);
2630 got_ref_close(head_ref);
2634 error = got_repo_match_object_id(&commit_id, NULL,
2635 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2640 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2643 if (obj_id == NULL) {
2644 error = got_error(GOT_ERR_NO_OBJ);
2648 error = got_object_get_type(&obj_type, repo, obj_id);
2652 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2653 error = got_error(GOT_ERR_OBJ_TYPE);
2657 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2660 bca.f = got_opentemp();
2661 if (bca.f == NULL) {
2662 error = got_error_from_errno("got_opentemp");
2665 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2666 &bca.line_offsets, bca.f, blob);
2667 if (error || bca.nlines == 0)
2670 /* Don't include \n at EOF in the blame line count. */
2671 if (bca.line_offsets[bca.nlines - 1] == filesize)
2674 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2675 if (bca.lines == NULL) {
2676 error = got_error_from_errno("calloc");
2680 bca.nlines_prec = 0;
2688 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2689 check_cancelled, NULL);
2697 got_object_blob_close(blob);
2699 got_worktree_close(worktree);
2701 const struct got_error *repo_error;
2702 repo_error = got_repo_close(repo);
2707 for (i = 0; i < bca.nlines; i++) {
2708 struct blame_line *bline = &bca.lines[i];
2709 free(bline->id_str);
2710 free(bline->committer);
2714 free(bca.line_offsets);
2715 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2716 error = got_error_from_errno("fclose");
2724 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2730 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2731 const char *root_path)
2733 int is_root_path = (strcmp(path, root_path) == 0);
2734 const char *modestr = "";
2735 mode_t mode = got_tree_entry_get_mode(te);
2737 path += strlen(root_path);
2738 while (path[0] == '/')
2741 if (got_object_tree_entry_is_submodule(te))
2743 else if (S_ISLNK(mode))
2745 else if (S_ISDIR(mode))
2747 else if (mode & S_IXUSR)
2750 printf("%s%s%s%s%s\n", id ? id : "", path,
2751 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2754 static const struct got_error *
2755 print_tree(const char *path, struct got_object_id *commit_id,
2756 int show_ids, int recurse, const char *root_path,
2757 struct got_repository *repo)
2759 const struct got_error *err = NULL;
2760 struct got_object_id *tree_id = NULL;
2761 struct got_tree_object *tree = NULL;
2764 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2768 err = got_object_open_as_tree(&tree, repo, tree_id);
2771 nentries = got_object_tree_get_nentries(tree);
2772 for (i = 0; i < nentries; i++) {
2773 struct got_tree_entry *te;
2776 if (sigint_received || sigpipe_received)
2779 te = got_object_tree_get_entry(tree, i);
2782 err = got_object_id_str(&id_str,
2783 got_tree_entry_get_id(te));
2786 if (asprintf(&id, "%s ", id_str) == -1) {
2787 err = got_error_from_errno("asprintf");
2793 print_entry(te, id, path, root_path);
2796 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2798 if (asprintf(&child_path, "%s%s%s", path,
2799 path[0] == '/' && path[1] == '\0' ? "" : "/",
2800 got_tree_entry_get_name(te)) == -1) {
2801 err = got_error_from_errno("asprintf");
2804 err = print_tree(child_path, commit_id, show_ids, 1,
2813 got_object_tree_close(tree);
2818 static const struct got_error *
2819 cmd_tree(int argc, char *argv[])
2821 const struct got_error *error;
2822 struct got_repository *repo = NULL;
2823 struct got_worktree *worktree = NULL;
2825 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2826 struct got_object_id *commit_id = NULL;
2827 char *commit_id_str = NULL;
2828 int show_ids = 0, recurse = 0;
2832 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2837 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2840 commit_id_str = optarg;
2843 repo_path = realpath(optarg, NULL);
2844 if (repo_path == NULL)
2845 return got_error_from_errno2("realpath",
2847 got_path_strip_trailing_slashes(repo_path);
2871 cwd = getcwd(NULL, 0);
2873 error = got_error_from_errno("getcwd");
2876 if (repo_path == NULL) {
2877 error = got_worktree_open(&worktree, cwd);
2878 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2884 strdup(got_worktree_get_repo_path(worktree));
2885 if (repo_path == NULL)
2886 error = got_error_from_errno("strdup");
2890 repo_path = strdup(cwd);
2891 if (repo_path == NULL) {
2892 error = got_error_from_errno("strdup");
2898 error = got_repo_open(&repo, repo_path, NULL);
2902 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2908 char *p, *worktree_subdir = cwd +
2909 strlen(got_worktree_get_root_path(worktree));
2910 if (asprintf(&p, "%s/%s",
2911 got_worktree_get_path_prefix(worktree),
2912 worktree_subdir) == -1) {
2913 error = got_error_from_errno("asprintf");
2916 error = got_repo_map_path(&in_repo_path, repo, p, 1);
2923 if (in_repo_path == NULL) {
2924 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2929 if (commit_id_str == NULL) {
2930 struct got_reference *head_ref;
2931 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2934 error = got_ref_resolve(&commit_id, repo, head_ref);
2935 got_ref_close(head_ref);
2939 error = got_repo_match_object_id(&commit_id, NULL,
2940 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2945 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2946 in_repo_path, repo);
2953 got_worktree_close(worktree);
2955 const struct got_error *repo_error;
2956 repo_error = got_repo_close(repo);
2966 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2970 static const struct got_error *
2971 print_status(void *arg, unsigned char status, unsigned char staged_status,
2972 const char *path, struct got_object_id *blob_id,
2973 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2974 int dirfd, const char *de_name)
2976 if (status == staged_status && (status == GOT_STATUS_DELETE))
2977 status = GOT_STATUS_NO_CHANGE;
2978 printf("%c%c %s\n", status, staged_status, path);
2982 static const struct got_error *
2983 cmd_status(int argc, char *argv[])
2985 const struct got_error *error = NULL;
2986 struct got_repository *repo = NULL;
2987 struct got_worktree *worktree = NULL;
2989 struct got_pathlist_head paths;
2990 struct got_pathlist_entry *pe;
2995 while ((ch = getopt(argc, argv, "")) != -1) {
3007 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3011 cwd = getcwd(NULL, 0);
3013 error = got_error_from_errno("getcwd");
3017 error = got_worktree_open(&worktree, cwd);
3021 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3026 error = apply_unveil(got_repo_get_path(repo), 1,
3027 got_worktree_get_root_path(worktree));
3031 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3035 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3036 check_cancelled, NULL);
3038 TAILQ_FOREACH(pe, &paths, entry)
3039 free((char *)pe->path);
3040 got_pathlist_free(&paths);
3049 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3054 static const struct got_error *
3055 list_refs(struct got_repository *repo)
3057 static const struct got_error *err = NULL;
3058 struct got_reflist_head refs;
3059 struct got_reflist_entry *re;
3061 SIMPLEQ_INIT(&refs);
3062 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3066 SIMPLEQ_FOREACH(re, &refs, entry) {
3068 refstr = got_ref_to_str(re->ref);
3070 return got_error_from_errno("got_ref_to_str");
3071 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3075 got_ref_list_free(&refs);
3079 static const struct got_error *
3080 delete_ref(struct got_repository *repo, const char *refname)
3082 const struct got_error *err = NULL;
3083 struct got_reference *ref;
3085 err = got_ref_open(&ref, repo, refname, 0);
3089 err = got_ref_delete(ref, repo);
3094 static const struct got_error *
3095 add_ref(struct got_repository *repo, const char *refname, const char *target)
3097 const struct got_error *err = NULL;
3098 struct got_object_id *id;
3099 struct got_reference *ref = NULL;
3102 * Don't let the user create a reference name with a leading '-'.
3103 * While technically a valid reference name, this case is usually
3104 * an unintended typo.
3106 if (refname[0] == '-')
3107 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3109 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3112 struct got_reference *target_ref;
3114 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3116 err = got_ref_open(&target_ref, repo, target, 0);
3119 err = got_ref_resolve(&id, repo, target_ref);
3120 got_ref_close(target_ref);
3125 err = got_ref_alloc(&ref, refname, id);
3129 err = got_ref_write(ref, repo);
3137 static const struct got_error *
3138 add_symref(struct got_repository *repo, const char *refname, const char *target)
3140 const struct got_error *err = NULL;
3141 struct got_reference *ref = NULL;
3142 struct got_reference *target_ref = NULL;
3145 * Don't let the user create a reference name with a leading '-'.
3146 * While technically a valid reference name, this case is usually
3147 * an unintended typo.
3149 if (refname[0] == '-')
3150 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3152 err = got_ref_open(&target_ref, repo, target, 0);
3156 err = got_ref_alloc_symref(&ref, refname, target_ref);
3160 err = got_ref_write(ref, repo);
3163 got_ref_close(target_ref);
3169 static const struct got_error *
3170 cmd_ref(int argc, char *argv[])
3172 const struct got_error *error = NULL;
3173 struct got_repository *repo = NULL;
3174 struct got_worktree *worktree = NULL;
3175 char *cwd = NULL, *repo_path = NULL;
3176 int ch, do_list = 0, create_symref = 0;
3177 const char *delref = NULL;
3179 /* TODO: Add -s option for adding symbolic references. */
3180 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3186 repo_path = realpath(optarg, NULL);
3187 if (repo_path == NULL)
3188 return got_error_from_errno2("realpath",
3190 got_path_strip_trailing_slashes(repo_path);
3204 if (do_list && delref)
3205 errx(1, "-l and -d options are mutually exclusive\n");
3210 if (do_list || delref) {
3212 errx(1, "-s option cannot be used together with the "
3213 "-l or -d options");
3216 } else if (argc != 2)
3221 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3225 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3226 "sendfd unveil", NULL) == -1)
3230 cwd = getcwd(NULL, 0);
3232 error = got_error_from_errno("getcwd");
3236 if (repo_path == NULL) {
3237 error = got_worktree_open(&worktree, cwd);
3238 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3244 strdup(got_worktree_get_repo_path(worktree));
3245 if (repo_path == NULL)
3246 error = got_error_from_errno("strdup");
3250 repo_path = strdup(cwd);
3251 if (repo_path == NULL) {
3252 error = got_error_from_errno("strdup");
3258 error = got_repo_open(&repo, repo_path, NULL);
3262 error = apply_unveil(got_repo_get_path(repo), do_list,
3263 worktree ? got_worktree_get_root_path(worktree) : NULL);
3268 error = list_refs(repo);
3270 error = delete_ref(repo, delref);
3271 else if (create_symref)
3272 error = add_symref(repo, argv[0], argv[1]);
3274 error = add_ref(repo, argv[0], argv[1]);
3277 got_repo_close(repo);
3279 got_worktree_close(worktree);
3289 "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3290 "[name]\n", getprogname());
3294 static const struct got_error *
3295 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3296 struct got_reference *ref)
3298 const struct got_error *err = NULL;
3299 const char *refname, *marker = " ";
3302 refname = got_ref_get_name(ref);
3303 if (worktree && strcmp(refname,
3304 got_worktree_get_head_ref_name(worktree)) == 0) {
3305 struct got_object_id *id = NULL;
3307 err = got_ref_resolve(&id, repo, ref);
3310 if (got_object_id_cmp(id,
3311 got_worktree_get_base_commit_id(worktree)) == 0)
3318 if (strncmp(refname, "refs/heads/", 11) == 0)
3320 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3323 refstr = got_ref_to_str(ref);
3325 return got_error_from_errno("got_ref_to_str");
3327 printf("%s%s: %s\n", marker, refname, refstr);
3332 static const struct got_error *
3333 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3335 const char *refname;
3337 if (worktree == NULL)
3338 return got_error(GOT_ERR_NOT_WORKTREE);
3340 refname = got_worktree_get_head_ref_name(worktree);
3342 if (strncmp(refname, "refs/heads/", 11) == 0)
3344 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3347 printf("%s\n", refname);
3352 static const struct got_error *
3353 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3355 static const struct got_error *err = NULL;
3356 struct got_reflist_head refs;
3357 struct got_reflist_entry *re;
3358 struct got_reference *temp_ref = NULL;
3359 int rebase_in_progress, histedit_in_progress;
3361 SIMPLEQ_INIT(&refs);
3364 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3369 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3374 if (rebase_in_progress || histedit_in_progress) {
3375 err = got_ref_open(&temp_ref, repo,
3376 got_worktree_get_head_ref_name(worktree), 0);
3379 list_branch(repo, worktree, temp_ref);
3380 got_ref_close(temp_ref);
3384 err = got_ref_list(&refs, repo, "refs/heads",
3385 got_ref_cmp_by_name, NULL);
3389 SIMPLEQ_FOREACH(re, &refs, entry)
3390 list_branch(repo, worktree, re->ref);
3392 got_ref_list_free(&refs);
3396 static const struct got_error *
3397 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3398 const char *branch_name)
3400 const struct got_error *err = NULL;
3401 struct got_reference *ref = NULL;
3404 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3405 return got_error_from_errno("asprintf");
3407 err = got_ref_open(&ref, repo, refname, 0);
3412 strcmp(got_worktree_get_head_ref_name(worktree),
3413 got_ref_get_name(ref)) == 0) {
3414 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3415 "will not delete this work tree's current branch");
3419 err = got_ref_delete(ref, repo);
3427 static const struct got_error *
3428 add_branch(struct got_repository *repo, const char *branch_name,
3429 struct got_object_id *base_commit_id)
3431 const struct got_error *err = NULL;
3432 struct got_reference *ref = NULL;
3433 char *base_refname = NULL, *refname = NULL;
3436 * Don't let the user create a branch name with a leading '-'.
3437 * While technically a valid reference name, this case is usually
3438 * an unintended typo.
3440 if (branch_name[0] == '-')
3441 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3443 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3444 err = got_error_from_errno("asprintf");
3448 err = got_ref_open(&ref, repo, refname, 0);
3450 err = got_error(GOT_ERR_BRANCH_EXISTS);
3452 } else if (err->code != GOT_ERR_NOT_REF)
3455 err = got_ref_alloc(&ref, refname, base_commit_id);
3459 err = got_ref_write(ref, repo);
3468 static const struct got_error *
3469 cmd_branch(int argc, char *argv[])
3471 const struct got_error *error = NULL;
3472 struct got_repository *repo = NULL;
3473 struct got_worktree *worktree = NULL;
3474 char *cwd = NULL, *repo_path = NULL;
3475 int ch, do_list = 0, do_show = 0;
3476 const char *delref = NULL, *commit_id_arg = NULL;
3478 while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3481 commit_id_arg = optarg;
3487 repo_path = realpath(optarg, NULL);
3488 if (repo_path == NULL)
3489 return got_error_from_errno2("realpath",
3491 got_path_strip_trailing_slashes(repo_path);
3502 if (do_list && delref)
3503 errx(1, "-l and -d options are mutually exclusive\n");
3508 if (!do_list && !delref && argc == 0)
3511 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3512 errx(1, "-c option can only be used when creating a branch");
3514 if (do_list || delref) {
3517 } else if (!do_show && argc != 1)
3521 if (do_list || do_show) {
3522 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3526 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3527 "sendfd unveil", NULL) == -1)
3531 cwd = getcwd(NULL, 0);
3533 error = got_error_from_errno("getcwd");
3537 if (repo_path == NULL) {
3538 error = got_worktree_open(&worktree, cwd);
3539 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3545 strdup(got_worktree_get_repo_path(worktree));
3546 if (repo_path == NULL)
3547 error = got_error_from_errno("strdup");
3551 repo_path = strdup(cwd);
3552 if (repo_path == NULL) {
3553 error = got_error_from_errno("strdup");
3559 error = got_repo_open(&repo, repo_path, NULL);
3563 error = apply_unveil(got_repo_get_path(repo), do_list,
3564 worktree ? got_worktree_get_root_path(worktree) : NULL);
3569 error = show_current_branch(repo, worktree);
3571 error = list_branches(repo, worktree);
3573 error = delete_branch(repo, worktree, delref);
3575 struct got_object_id *commit_id;
3576 if (commit_id_arg == NULL)
3577 commit_id_arg = worktree ?
3578 got_worktree_get_head_ref_name(worktree) :
3580 error = got_repo_match_object_id(&commit_id, NULL,
3581 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3584 error = add_branch(repo, argv[0], commit_id);
3589 got_repo_close(repo);
3591 got_worktree_close(worktree);
3602 "usage: %s tag [-r repository] | -l | "
3603 "[-m message] name [commit]\n", getprogname());
3608 static const struct got_error *
3609 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3611 const struct got_error *err = NULL;
3612 struct got_reflist_entry *re, *se, *new;
3613 struct got_object_id *re_id, *se_id;
3614 struct got_tag_object *re_tag, *se_tag;
3615 time_t re_time, se_time;
3617 SIMPLEQ_FOREACH(re, tags, entry) {
3618 se = SIMPLEQ_FIRST(sorted);
3620 err = got_reflist_entry_dup(&new, re);
3623 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3626 err = got_ref_resolve(&re_id, repo, re->ref);
3629 err = got_object_open_as_tag(&re_tag, repo, re_id);
3633 re_time = got_object_tag_get_tagger_time(re_tag);
3634 got_object_tag_close(re_tag);
3638 err = got_ref_resolve(&se_id, repo, re->ref);
3641 err = got_object_open_as_tag(&se_tag, repo, se_id);
3645 se_time = got_object_tag_get_tagger_time(se_tag);
3646 got_object_tag_close(se_tag);
3648 if (se_time > re_time) {
3649 err = got_reflist_entry_dup(&new, re);
3652 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3655 se = SIMPLEQ_NEXT(se, entry);
3664 static const struct got_error *
3665 cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3666 struct got_reference *ref2)
3668 const struct got_error *err = NULL;
3669 struct got_repository *repo = arg;
3670 struct got_object_id *id1, *id2 = NULL;
3671 struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3672 struct got_commit_object *commit1 = NULL, *commit2 = NULL;
3673 time_t time1, time2;
3677 err = got_ref_resolve(&id1, repo, ref1);
3680 err = got_object_open_as_tag(&tag1, repo, id1);
3682 if (err->code != GOT_ERR_OBJ_TYPE)
3684 /* "lightweight" tag */
3685 err = got_object_open_as_commit(&commit1, repo, id1);
3688 time1 = got_object_commit_get_committer_time(commit1);
3690 time1 = got_object_tag_get_tagger_time(tag1);
3692 err = got_ref_resolve(&id2, repo, ref2);
3695 err = got_object_open_as_tag(&tag2, repo, id2);
3697 if (err->code != GOT_ERR_OBJ_TYPE)
3699 /* "lightweight" tag */
3700 err = got_object_open_as_commit(&commit2, repo, id2);
3703 time2 = got_object_commit_get_committer_time(commit2);
3705 time2 = got_object_tag_get_tagger_time(tag2);
3707 /* Put latest tags first. */
3710 else if (time1 > time2)
3713 err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3718 got_object_tag_close(tag1);
3720 got_object_tag_close(tag2);
3722 got_object_commit_close(commit1);
3724 got_object_commit_close(commit2);
3728 static const struct got_error *
3729 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3731 static const struct got_error *err = NULL;
3732 struct got_reflist_head refs;
3733 struct got_reflist_entry *re;
3735 SIMPLEQ_INIT(&refs);
3737 err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3741 SIMPLEQ_FOREACH(re, &refs, entry) {
3742 const char *refname;
3743 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3747 struct got_object_id *id;
3748 struct got_tag_object *tag;
3749 struct got_commit_object *commit = NULL;
3751 refname = got_ref_get_name(re->ref);
3752 if (strncmp(refname, "refs/tags/", 10) != 0)
3755 refstr = got_ref_to_str(re->ref);
3756 if (refstr == NULL) {
3757 err = got_error_from_errno("got_ref_to_str");
3760 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3763 err = got_ref_resolve(&id, repo, re->ref);
3766 err = got_object_open_as_tag(&tag, repo, id);
3768 if (err->code != GOT_ERR_OBJ_TYPE) {
3772 /* "lightweight" tag */
3773 err = got_object_open_as_commit(&commit, repo, id);
3778 tagger = got_object_commit_get_committer(commit);
3780 got_object_commit_get_committer_time(commit);
3781 err = got_object_id_str(&id_str, id);
3787 tagger = got_object_tag_get_tagger(tag);
3788 tagger_time = got_object_tag_get_tagger_time(tag);
3789 err = got_object_id_str(&id_str,
3790 got_object_tag_get_object_id(tag));
3794 printf("from: %s\n", tagger);
3795 datestr = get_datestr(&tagger_time, datebuf);
3797 printf("date: %s UTC\n", datestr);
3799 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3801 switch (got_object_tag_get_object_type(tag)) {
3802 case GOT_OBJ_TYPE_BLOB:
3803 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3806 case GOT_OBJ_TYPE_TREE:
3807 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3810 case GOT_OBJ_TYPE_COMMIT:
3811 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3814 case GOT_OBJ_TYPE_TAG:
3815 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3824 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3827 got_object_commit_close(commit);
3829 tagmsg0 = strdup(got_object_tag_get_message(tag));
3830 got_object_tag_close(tag);
3831 if (tagmsg0 == NULL) {
3832 err = got_error_from_errno("strdup");
3839 line = strsep(&tagmsg, "\n");
3841 printf(" %s\n", line);
3846 got_ref_list_free(&refs);
3850 static const struct got_error *
3851 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3852 const char *tag_name, const char *repo_path)
3854 const struct got_error *err = NULL;
3855 char *template = NULL, *initial_content = NULL;
3856 char *editor = NULL;
3859 if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3860 err = got_error_from_errno("asprintf");
3864 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3865 commit_id_str, tag_name) == -1) {
3866 err = got_error_from_errno("asprintf");
3870 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3874 dprintf(fd, initial_content);
3877 err = get_editor(&editor);
3880 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3882 free(initial_content);
3886 /* Editor is done; we can now apply unveil(2) */
3888 err = apply_unveil(repo_path, 0, NULL);
3897 static const struct got_error *
3898 add_tag(struct got_repository *repo, const char *tag_name,
3899 const char *commit_arg, const char *tagmsg_arg)
3901 const struct got_error *err = NULL;
3902 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3903 char *label = NULL, *commit_id_str = NULL;
3904 struct got_reference *ref = NULL;
3905 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3906 char *tagmsg_path = NULL, *tag_id_str = NULL;
3907 int preserve_tagmsg = 0;
3910 * Don't let the user create a tag name with a leading '-'.
3911 * While technically a valid reference name, this case is usually
3912 * an unintended typo.
3914 if (tag_name[0] == '-')
3915 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3917 err = get_author(&tagger, repo);
3921 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3922 GOT_OBJ_TYPE_COMMIT, 1, repo);
3926 err = got_object_id_str(&commit_id_str, commit_id);
3930 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3931 refname = strdup(tag_name);
3932 if (refname == NULL) {
3933 err = got_error_from_errno("strdup");
3937 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3938 err = got_error_from_errno("asprintf");
3942 err = got_ref_open(&ref, repo, refname, 0);
3944 err = got_error(GOT_ERR_TAG_EXISTS);
3946 } else if (err->code != GOT_ERR_NOT_REF)
3949 if (tagmsg_arg == NULL) {
3950 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3951 tag_name, got_repo_get_path(repo));
3953 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3954 tagmsg_path != NULL)
3955 preserve_tagmsg = 1;
3960 err = got_object_tag_create(&tag_id, tag_name, commit_id,
3961 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3964 preserve_tagmsg = 1;
3968 err = got_ref_alloc(&ref, refname, tag_id);
3971 preserve_tagmsg = 1;
3975 err = got_ref_write(ref, repo);
3978 preserve_tagmsg = 1;
3982 err = got_object_id_str(&tag_id_str, tag_id);
3985 preserve_tagmsg = 1;
3988 printf("Created tag %s\n", tag_id_str);
3990 if (preserve_tagmsg) {
3991 fprintf(stderr, "%s: tag message preserved in %s\n",
3992 getprogname(), tagmsg_path);
3993 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3994 err = got_error_from_errno2("unlink", tagmsg_path);
3999 free(commit_id_str);
4007 static const struct got_error *
4008 cmd_tag(int argc, char *argv[])
4010 const struct got_error *error = NULL;
4011 struct got_repository *repo = NULL;
4012 struct got_worktree *worktree = NULL;
4013 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4014 char *gitconfig_path = NULL;
4015 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4016 int ch, do_list = 0;
4018 while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4024 repo_path = realpath(optarg, NULL);
4025 if (repo_path == NULL)
4026 return got_error_from_errno2("realpath",
4028 got_path_strip_trailing_slashes(repo_path);
4044 errx(1, "-l and -m options are mutually exclusive\n");
4047 } else if (argc < 1 || argc > 2)
4050 commit_id_arg = argv[1];
4055 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4059 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4060 "sendfd unveil", NULL) == -1)
4064 cwd = getcwd(NULL, 0);
4066 error = got_error_from_errno("getcwd");
4070 if (repo_path == NULL) {
4071 error = got_worktree_open(&worktree, cwd);
4072 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4078 strdup(got_worktree_get_repo_path(worktree));
4079 if (repo_path == NULL)
4080 error = got_error_from_errno("strdup");
4084 repo_path = strdup(cwd);
4085 if (repo_path == NULL) {
4086 error = got_error_from_errno("strdup");
4093 error = got_repo_open(&repo, repo_path, NULL);
4096 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4099 error = list_tags(repo, worktree);
4101 error = get_gitconfig_path(&gitconfig_path);
4104 error = got_repo_open(&repo, repo_path, gitconfig_path);
4109 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4114 if (commit_id_arg == NULL) {
4115 struct got_reference *head_ref;
4116 struct got_object_id *commit_id;
4117 error = got_ref_open(&head_ref, repo,
4118 worktree ? got_worktree_get_head_ref_name(worktree)
4122 error = got_ref_resolve(&commit_id, repo, head_ref);
4123 got_ref_close(head_ref);
4126 error = got_object_id_str(&commit_id_str, commit_id);
4132 error = add_tag(repo, tag_name,
4133 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4137 got_repo_close(repo);
4139 got_worktree_close(worktree);
4142 free(gitconfig_path);
4143 free(commit_id_str);
4150 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4155 static const struct got_error *
4156 add_progress(void *arg, unsigned char status, const char *path)
4158 while (path[0] == '/')
4160 printf("%c %s\n", status, path);
4164 static const struct got_error *
4165 cmd_add(int argc, char *argv[])
4167 const struct got_error *error = NULL;
4168 struct got_repository *repo = NULL;
4169 struct got_worktree *worktree = NULL;
4171 struct got_pathlist_head paths;
4172 struct got_pathlist_entry *pe;
4173 int ch, can_recurse = 0, no_ignores = 0;
4177 while ((ch = getopt(argc, argv, "IR")) != -1) {
4195 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4202 cwd = getcwd(NULL, 0);
4204 error = got_error_from_errno("getcwd");
4208 error = got_worktree_open(&worktree, cwd);
4212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4217 error = apply_unveil(got_repo_get_path(repo), 1,
4218 got_worktree_get_root_path(worktree));
4222 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4226 if (!can_recurse && no_ignores) {
4227 error = got_error_msg(GOT_ERR_BAD_PATH,
4228 "disregarding ignores requires -R option");
4236 TAILQ_FOREACH(pe, &paths, entry) {
4237 if (asprintf(&ondisk_path, "%s/%s",
4238 got_worktree_get_root_path(worktree),
4240 error = got_error_from_errno("asprintf");
4243 if (lstat(ondisk_path, &sb) == -1) {
4244 if (errno == ENOENT) {
4248 error = got_error_from_errno2("lstat",
4254 if (S_ISDIR(sb.st_mode)) {
4255 error = got_error_msg(GOT_ERR_BAD_PATH,
4256 "adding directories requires -R option");
4262 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4263 NULL, repo, no_ignores);
4266 got_repo_close(repo);
4268 got_worktree_close(worktree);
4269 TAILQ_FOREACH(pe, &paths, entry)
4270 free((char *)pe->path);
4271 got_pathlist_free(&paths);
4279 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4284 static const struct got_error *
4285 print_remove_status(void *arg, unsigned char status,
4286 unsigned char staged_status, const char *path)
4288 while (path[0] == '/')
4290 if (status == GOT_STATUS_NONEXISTENT)
4292 if (status == staged_status && (status == GOT_STATUS_DELETE))
4293 status = GOT_STATUS_NO_CHANGE;
4294 printf("%c%c %s\n", status, staged_status, path);
4298 static const struct got_error *
4299 cmd_remove(int argc, char *argv[])
4301 const struct got_error *error = NULL;
4302 struct got_worktree *worktree = NULL;
4303 struct got_repository *repo = NULL;
4305 struct got_pathlist_head paths;
4306 struct got_pathlist_entry *pe;
4307 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4311 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4314 delete_local_mods = 1;
4332 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4339 cwd = getcwd(NULL, 0);
4341 error = got_error_from_errno("getcwd");
4344 error = got_worktree_open(&worktree, cwd);
4348 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4353 error = apply_unveil(got_repo_get_path(repo), 1,
4354 got_worktree_get_root_path(worktree));
4358 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4365 TAILQ_FOREACH(pe, &paths, entry) {
4366 if (asprintf(&ondisk_path, "%s/%s",
4367 got_worktree_get_root_path(worktree),
4369 error = got_error_from_errno("asprintf");
4372 if (lstat(ondisk_path, &sb) == -1) {
4373 if (errno == ENOENT) {
4377 error = got_error_from_errno2("lstat",
4383 if (S_ISDIR(sb.st_mode)) {
4384 error = got_error_msg(GOT_ERR_BAD_PATH,
4385 "removing directories requires -R option");
4391 error = got_worktree_schedule_delete(worktree, &paths,
4392 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4395 got_repo_close(repo);
4397 got_worktree_close(worktree);
4398 TAILQ_FOREACH(pe, &paths, entry)
4399 free((char *)pe->path);
4400 got_pathlist_free(&paths);
4408 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4409 "path ...\n", getprogname());
4413 static const struct got_error *
4414 revert_progress(void *arg, unsigned char status, const char *path)
4416 while (path[0] == '/')
4418 printf("%c %s\n", status, path);
4422 struct choose_patch_arg {
4423 FILE *patch_script_file;
4427 static const struct got_error *
4428 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4429 int nchanges, const char *action)
4432 size_t linesize = 0;
4436 case GOT_STATUS_ADD:
4437 printf("A %s\n%s this addition? [y/n] ", path, action);
4439 case GOT_STATUS_DELETE:
4440 printf("D %s\n%s this deletion? [y/n] ", path, action);
4442 case GOT_STATUS_MODIFY:
4443 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4444 return got_error_from_errno("fseek");
4445 printf(GOT_COMMIT_SEP_STR);
4446 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4448 if (ferror(patch_file))
4449 return got_error_from_errno("getline");
4450 printf(GOT_COMMIT_SEP_STR);
4451 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4452 path, n, nchanges, action);
4455 return got_error_path(path, GOT_ERR_FILE_STATUS);
4461 static const struct got_error *
4462 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4463 FILE *patch_file, int n, int nchanges)
4465 const struct got_error *err = NULL;
4467 size_t linesize = 0;
4470 struct choose_patch_arg *a = arg;
4472 *choice = GOT_PATCH_CHOICE_NONE;
4474 if (a->patch_script_file) {
4476 err = show_change(status, path, patch_file, n, nchanges,
4480 linelen = getline(&line, &linesize, a->patch_script_file);
4481 if (linelen == -1) {
4482 if (ferror(a->patch_script_file))
4483 return got_error_from_errno("getline");
4486 nl = strchr(line, '\n');
4489 if (strcmp(line, "y") == 0) {
4490 *choice = GOT_PATCH_CHOICE_YES;
4492 } else if (strcmp(line, "n") == 0) {
4493 *choice = GOT_PATCH_CHOICE_NO;
4495 } else if (strcmp(line, "q") == 0 &&
4496 status == GOT_STATUS_MODIFY) {
4497 *choice = GOT_PATCH_CHOICE_QUIT;
4500 printf("invalid response '%s'\n", line);
4505 while (resp != 'y' && resp != 'n' && resp != 'q') {
4506 err = show_change(status, path, patch_file, n, nchanges,
4513 if (status == GOT_STATUS_MODIFY) {
4514 if (resp != 'y' && resp != 'n' && resp != 'q') {
4515 printf("invalid response '%c'\n", resp);
4518 } else if (resp != 'y' && resp != 'n') {
4519 printf("invalid response '%c'\n", resp);
4525 *choice = GOT_PATCH_CHOICE_YES;
4526 else if (resp == 'n')
4527 *choice = GOT_PATCH_CHOICE_NO;
4528 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4529 *choice = GOT_PATCH_CHOICE_QUIT;
4535 static const struct got_error *
4536 cmd_revert(int argc, char *argv[])
4538 const struct got_error *error = NULL;
4539 struct got_worktree *worktree = NULL;
4540 struct got_repository *repo = NULL;
4541 char *cwd = NULL, *path = NULL;
4542 struct got_pathlist_head paths;
4543 struct got_pathlist_entry *pe;
4544 int ch, can_recurse = 0, pflag = 0;
4545 FILE *patch_script_file = NULL;
4546 const char *patch_script_path = NULL;
4547 struct choose_patch_arg cpa;
4551 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4557 patch_script_path = optarg;
4572 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4573 "unveil", NULL) == -1)
4578 if (patch_script_path && !pflag)
4579 errx(1, "-F option can only be used together with -p option");
4581 cwd = getcwd(NULL, 0);
4583 error = got_error_from_errno("getcwd");
4586 error = got_worktree_open(&worktree, cwd);
4590 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4595 if (patch_script_path) {
4596 patch_script_file = fopen(patch_script_path, "r");
4597 if (patch_script_file == NULL) {
4598 error = got_error_from_errno2("fopen",
4603 error = apply_unveil(got_repo_get_path(repo), 1,
4604 got_worktree_get_root_path(worktree));
4608 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4615 TAILQ_FOREACH(pe, &paths, entry) {
4616 if (asprintf(&ondisk_path, "%s/%s",
4617 got_worktree_get_root_path(worktree),
4619 error = got_error_from_errno("asprintf");
4622 if (lstat(ondisk_path, &sb) == -1) {
4623 if (errno == ENOENT) {
4627 error = got_error_from_errno2("lstat",
4633 if (S_ISDIR(sb.st_mode)) {
4634 error = got_error_msg(GOT_ERR_BAD_PATH,
4635 "reverting directories requires -R option");
4641 cpa.patch_script_file = patch_script_file;
4642 cpa.action = "revert";
4643 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4644 pflag ? choose_patch : NULL, &cpa, repo);
4646 if (patch_script_file && fclose(patch_script_file) == EOF &&
4648 error = got_error_from_errno2("fclose", patch_script_path);
4650 got_repo_close(repo);
4652 got_worktree_close(worktree);
4661 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4666 struct collect_commit_logmsg_arg {
4667 const char *cmdline_log;
4669 const char *worktree_path;
4670 const char *branch_name;
4671 const char *repo_path;
4676 static const struct got_error *
4677 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4680 char *initial_content = NULL;
4681 struct got_pathlist_entry *pe;
4682 const struct got_error *err = NULL;
4683 char *template = NULL;
4684 struct collect_commit_logmsg_arg *a = arg;
4688 /* if a message was specified on the command line, just use it */
4689 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4690 len = strlen(a->cmdline_log) + 1;
4691 *logmsg = malloc(len + 1);
4692 if (*logmsg == NULL)
4693 return got_error_from_errno("malloc");
4694 strlcpy(*logmsg, a->cmdline_log, len);
4698 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4699 return got_error_from_errno("asprintf");
4701 if (asprintf(&initial_content,
4702 "\n# changes to be committed on branch %s:\n",
4703 a->branch_name) == -1)
4704 return got_error_from_errno("asprintf");
4706 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4710 dprintf(fd, initial_content);
4712 TAILQ_FOREACH(pe, commitable_paths, entry) {
4713 struct got_commitable *ct = pe->data;
4714 dprintf(fd, "# %c %s\n",
4715 got_commitable_get_status(ct),
4716 got_commitable_get_path(ct));
4720 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4722 free(initial_content);
4725 /* Editor is done; we can now apply unveil(2) */
4727 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4736 static const struct got_error *
4737 cmd_commit(int argc, char *argv[])
4739 const struct got_error *error = NULL;
4740 struct got_worktree *worktree = NULL;
4741 struct got_repository *repo = NULL;
4742 char *cwd = NULL, *id_str = NULL;
4743 struct got_object_id *id = NULL;
4744 const char *logmsg = NULL;
4745 struct collect_commit_logmsg_arg cl_arg;
4746 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4747 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4748 struct got_pathlist_head paths;
4751 cl_arg.logmsg_path = NULL;
4753 while ((ch = getopt(argc, argv, "m:")) != -1) {
4768 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4769 "unveil", NULL) == -1)
4772 cwd = getcwd(NULL, 0);
4774 error = got_error_from_errno("getcwd");
4777 error = got_worktree_open(&worktree, cwd);
4781 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4784 if (rebase_in_progress) {
4785 error = got_error(GOT_ERR_REBASING);
4789 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4794 error = get_gitconfig_path(&gitconfig_path);
4797 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4802 error = get_author(&author, repo);
4807 * unveil(2) traverses exec(2); if an editor is used we have
4808 * to apply unveil after the log message has been written.
4810 if (logmsg == NULL || strlen(logmsg) == 0)
4811 error = get_editor(&editor);
4813 error = apply_unveil(got_repo_get_path(repo), 0,
4814 got_worktree_get_root_path(worktree));
4818 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4822 cl_arg.editor = editor;
4823 cl_arg.cmdline_log = logmsg;
4824 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4825 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4826 if (!histedit_in_progress) {
4827 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4828 error = got_error(GOT_ERR_COMMIT_BRANCH);
4831 cl_arg.branch_name += 11;
4833 cl_arg.repo_path = got_repo_get_path(repo);
4834 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4835 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4837 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4838 cl_arg.logmsg_path != NULL)
4839 preserve_logmsg = 1;
4843 error = got_object_id_str(&id_str, id);
4846 printf("Created commit %s\n", id_str);
4848 if (preserve_logmsg) {
4849 fprintf(stderr, "%s: log message preserved in %s\n",
4850 getprogname(), cl_arg.logmsg_path);
4851 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4853 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4854 free(cl_arg.logmsg_path);
4856 got_repo_close(repo);
4858 got_worktree_close(worktree);
4861 free(gitconfig_path);
4868 usage_cherrypick(void)
4870 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4874 static const struct got_error *
4875 cmd_cherrypick(int argc, char *argv[])
4877 const struct got_error *error = NULL;
4878 struct got_worktree *worktree = NULL;
4879 struct got_repository *repo = NULL;
4880 char *cwd = NULL, *commit_id_str = NULL;
4881 struct got_object_id *commit_id = NULL;
4882 struct got_commit_object *commit = NULL;
4883 struct got_object_qid *pid;
4884 struct got_reference *head_ref = NULL;
4885 int ch, did_something = 0;
4887 while ((ch = getopt(argc, argv, "")) != -1) {
4899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4900 "unveil", NULL) == -1)
4906 cwd = getcwd(NULL, 0);
4908 error = got_error_from_errno("getcwd");
4911 error = got_worktree_open(&worktree, cwd);
4915 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4920 error = apply_unveil(got_repo_get_path(repo), 0,
4921 got_worktree_get_root_path(worktree));
4925 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4926 GOT_OBJ_TYPE_COMMIT, repo);
4927 if (error != NULL) {
4928 struct got_reference *ref;
4929 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4931 error = got_ref_open(&ref, repo, argv[0], 0);
4934 error = got_ref_resolve(&commit_id, repo, ref);
4939 error = got_object_id_str(&commit_id_str, commit_id);
4943 error = got_ref_open(&head_ref, repo,
4944 got_worktree_get_head_ref_name(worktree), 0);
4948 error = check_same_branch(commit_id, head_ref, NULL, repo);
4950 if (error->code != GOT_ERR_ANCESTRY)
4954 error = got_error(GOT_ERR_SAME_BRANCH);
4958 error = got_object_open_as_commit(&commit, repo, commit_id);
4961 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4962 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4963 commit_id, repo, update_progress, &did_something, check_cancelled,
4969 printf("Merged commit %s\n", commit_id_str);
4972 got_object_commit_close(commit);
4973 free(commit_id_str);
4975 got_ref_close(head_ref);
4977 got_worktree_close(worktree);
4979 got_repo_close(repo);
4986 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4990 static const struct got_error *
4991 cmd_backout(int argc, char *argv[])
4993 const struct got_error *error = NULL;
4994 struct got_worktree *worktree = NULL;
4995 struct got_repository *repo = NULL;
4996 char *cwd = NULL, *commit_id_str = NULL;
4997 struct got_object_id *commit_id = NULL;
4998 struct got_commit_object *commit = NULL;
4999 struct got_object_qid *pid;
5000 struct got_reference *head_ref = NULL;
5001 int ch, did_something = 0;
5003 while ((ch = getopt(argc, argv, "")) != -1) {
5015 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5016 "unveil", NULL) == -1)
5022 cwd = getcwd(NULL, 0);
5024 error = got_error_from_errno("getcwd");
5027 error = got_worktree_open(&worktree, cwd);
5031 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5036 error = apply_unveil(got_repo_get_path(repo), 0,
5037 got_worktree_get_root_path(worktree));
5041 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5042 GOT_OBJ_TYPE_COMMIT, repo);
5043 if (error != NULL) {
5044 struct got_reference *ref;
5045 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5047 error = got_ref_open(&ref, repo, argv[0], 0);
5050 error = got_ref_resolve(&commit_id, repo, ref);
5055 error = got_object_id_str(&commit_id_str, commit_id);
5059 error = got_ref_open(&head_ref, repo,
5060 got_worktree_get_head_ref_name(worktree), 0);
5064 error = check_same_branch(commit_id, head_ref, NULL, repo);
5068 error = got_object_open_as_commit(&commit, repo, commit_id);
5071 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5073 error = got_error(GOT_ERR_ROOT_COMMIT);
5077 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5078 update_progress, &did_something, check_cancelled, NULL);
5083 printf("Backed out commit %s\n", commit_id_str);
5086 got_object_commit_close(commit);
5087 free(commit_id_str);
5089 got_ref_close(head_ref);
5091 got_worktree_close(worktree);
5093 got_repo_close(repo);
5100 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5106 trim_logmsg(char *logmsg, int limit)
5111 len = strlen(logmsg);
5115 nl = strchr(logmsg, '\n');
5120 static const struct got_error *
5121 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5123 const struct got_error *err;
5124 char *logmsg0 = NULL;
5127 err = got_object_commit_get_logmsg(&logmsg0, commit);
5132 while (isspace((unsigned char)s[0]))
5135 *logmsg = strdup(s);
5136 if (*logmsg == NULL) {
5137 err = got_error_from_errno("strdup");
5141 trim_logmsg(*logmsg, limit);
5147 static const struct got_error *
5148 show_rebase_progress(struct got_commit_object *commit,
5149 struct got_object_id *old_id, struct got_object_id *new_id)
5151 const struct got_error *err;
5152 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5154 err = got_object_id_str(&old_id_str, old_id);
5159 err = got_object_id_str(&new_id_str, new_id);
5164 old_id_str[12] = '\0';
5166 new_id_str[12] = '\0';
5168 err = get_short_logmsg(&logmsg, 42, commit);
5172 printf("%s -> %s: %s\n", old_id_str,
5173 new_id_str ? new_id_str : "no-op change", logmsg);
5180 static const struct got_error *
5181 rebase_progress(void *arg, unsigned char status, const char *path)
5183 unsigned char *rebase_status = arg;
5185 while (path[0] == '/')
5187 printf("%c %s\n", status, path);
5189 if (*rebase_status == GOT_STATUS_CONFLICT)
5191 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5192 *rebase_status = status;
5196 static const struct got_error *
5197 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5198 struct got_reference *branch, struct got_reference *new_base_branch,
5199 struct got_reference *tmp_branch, struct got_repository *repo)
5201 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5202 return got_worktree_rebase_complete(worktree, fileindex,
5203 new_base_branch, tmp_branch, branch, repo);
5206 static const struct got_error *
5207 rebase_commit(struct got_pathlist_head *merged_paths,
5208 struct got_worktree *worktree, struct got_fileindex *fileindex,
5209 struct got_reference *tmp_branch,
5210 struct got_object_id *commit_id, struct got_repository *repo)
5212 const struct got_error *error;
5213 struct got_commit_object *commit;
5214 struct got_object_id *new_commit_id;
5216 error = got_object_open_as_commit(&commit, repo, commit_id);
5220 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5221 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5223 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5225 error = show_rebase_progress(commit, commit_id, NULL);
5227 error = show_rebase_progress(commit, commit_id, new_commit_id);
5228 free(new_commit_id);
5231 got_object_commit_close(commit);
5235 struct check_path_prefix_arg {
5236 const char *path_prefix;
5241 static const struct got_error *
5242 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5243 struct got_blob_object *blob2, struct got_object_id *id1,
5244 struct got_object_id *id2, const char *path1, const char *path2,
5245 mode_t mode1, mode_t mode2, struct got_repository *repo)
5247 struct check_path_prefix_arg *a = arg;
5249 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5250 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5251 return got_error(a->errcode);
5256 static const struct got_error *
5257 check_path_prefix(struct got_object_id *parent_id,
5258 struct got_object_id *commit_id, const char *path_prefix,
5259 int errcode, struct got_repository *repo)
5261 const struct got_error *err;
5262 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5263 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5264 struct check_path_prefix_arg cpp_arg;
5266 if (got_path_is_root_dir(path_prefix))
5269 err = got_object_open_as_commit(&commit, repo, commit_id);
5273 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5277 err = got_object_open_as_tree(&tree1, repo,
5278 got_object_commit_get_tree_id(parent_commit));
5282 err = got_object_open_as_tree(&tree2, repo,
5283 got_object_commit_get_tree_id(commit));
5287 cpp_arg.path_prefix = path_prefix;
5288 while (cpp_arg.path_prefix[0] == '/')
5289 cpp_arg.path_prefix++;
5290 cpp_arg.len = strlen(cpp_arg.path_prefix);
5291 cpp_arg.errcode = errcode;
5292 err = got_diff_tree(tree1, tree2, "", "", repo,
5293 check_path_prefix_in_diff, &cpp_arg, 0);
5296 got_object_tree_close(tree1);
5298 got_object_tree_close(tree2);
5300 got_object_commit_close(commit);
5302 got_object_commit_close(parent_commit);
5306 static const struct got_error *
5307 collect_commits(struct got_object_id_queue *commits,
5308 struct got_object_id *initial_commit_id,
5309 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5310 const char *path_prefix, int path_prefix_errcode,
5311 struct got_repository *repo)
5313 const struct got_error *err = NULL;
5314 struct got_commit_graph *graph = NULL;
5315 struct got_object_id *parent_id = NULL;
5316 struct got_object_qid *qid;
5317 struct got_object_id *commit_id = initial_commit_id;
5319 err = got_commit_graph_open(&graph, "/", 1);
5323 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5324 check_cancelled, NULL);
5327 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5328 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5329 check_cancelled, NULL);
5331 if (err->code == GOT_ERR_ITER_COMPLETED) {
5332 err = got_error_msg(GOT_ERR_ANCESTRY,
5333 "ran out of commits to rebase before "
5334 "youngest common ancestor commit has "
5339 err = check_path_prefix(parent_id, commit_id,
5340 path_prefix, path_prefix_errcode, repo);
5344 err = got_object_qid_alloc(&qid, commit_id);
5347 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5348 commit_id = parent_id;
5352 got_commit_graph_close(graph);
5356 static const struct got_error *
5357 cmd_rebase(int argc, char *argv[])
5359 const struct got_error *error = NULL;
5360 struct got_worktree *worktree = NULL;
5361 struct got_repository *repo = NULL;
5362 struct got_fileindex *fileindex = NULL;
5364 struct got_reference *branch = NULL;
5365 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5366 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5367 struct got_object_id *resume_commit_id = NULL;
5368 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5369 struct got_commit_object *commit = NULL;
5370 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5371 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5372 struct got_object_id_queue commits;
5373 struct got_pathlist_head merged_paths;
5374 const struct got_object_id_queue *parent_ids;
5375 struct got_object_qid *qid, *pid;
5377 SIMPLEQ_INIT(&commits);
5378 TAILQ_INIT(&merged_paths);
5380 while ((ch = getopt(argc, argv, "ac")) != -1) {
5386 continue_rebase = 1;
5398 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5399 "unveil", NULL) == -1)
5402 if (abort_rebase && continue_rebase)
5404 else if (abort_rebase || continue_rebase) {
5407 } else if (argc != 1)
5410 cwd = getcwd(NULL, 0);
5412 error = got_error_from_errno("getcwd");
5415 error = got_worktree_open(&worktree, cwd);
5419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5424 error = apply_unveil(got_repo_get_path(repo), 0,
5425 got_worktree_get_root_path(worktree));
5429 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5435 if (!rebase_in_progress) {
5436 error = got_error(GOT_ERR_NOT_REBASING);
5439 error = got_worktree_rebase_continue(&resume_commit_id,
5440 &new_base_branch, &tmp_branch, &branch, &fileindex,
5444 printf("Switching work tree to %s\n",
5445 got_ref_get_symref_target(new_base_branch));
5446 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5447 new_base_branch, update_progress, &did_something);
5450 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5451 goto done; /* nothing else to do */
5454 if (continue_rebase) {
5455 if (!rebase_in_progress) {
5456 error = got_error(GOT_ERR_NOT_REBASING);
5459 error = got_worktree_rebase_continue(&resume_commit_id,
5460 &new_base_branch, &tmp_branch, &branch, &fileindex,
5465 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5466 resume_commit_id, repo);
5470 yca_id = got_object_id_dup(resume_commit_id);
5471 if (yca_id == NULL) {
5472 error = got_error_from_errno("got_object_id_dup");
5476 error = got_ref_open(&branch, repo, argv[0], 0);
5481 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5485 if (!continue_rebase) {
5486 struct got_object_id *base_commit_id;
5488 base_commit_id = got_worktree_get_base_commit_id(worktree);
5489 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5490 base_commit_id, branch_head_commit_id, repo,
5491 check_cancelled, NULL);
5494 if (yca_id == NULL) {
5495 error = got_error_msg(GOT_ERR_ANCESTRY,
5496 "specified branch shares no common ancestry "
5497 "with work tree's branch");
5501 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5503 if (error->code != GOT_ERR_ANCESTRY)
5507 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5508 "specified branch resolves to a commit which "
5509 "is already contained in work tree's branch");
5512 error = got_worktree_rebase_prepare(&new_base_branch,
5513 &tmp_branch, &fileindex, worktree, branch, repo);
5518 commit_id = branch_head_commit_id;
5519 error = got_object_open_as_commit(&commit, repo, commit_id);
5523 parent_ids = got_object_commit_get_parent_ids(commit);
5524 pid = SIMPLEQ_FIRST(parent_ids);
5526 if (!continue_rebase) {
5528 error = got_worktree_rebase_abort(worktree, fileindex,
5529 repo, new_base_branch, update_progress,
5533 printf("Rebase of %s aborted\n",
5534 got_ref_get_name(branch));
5536 error = got_error(GOT_ERR_EMPTY_REBASE);
5539 error = collect_commits(&commits, commit_id, pid->id,
5540 yca_id, got_worktree_get_path_prefix(worktree),
5541 GOT_ERR_REBASE_PATH, repo);
5542 got_object_commit_close(commit);
5547 if (SIMPLEQ_EMPTY(&commits)) {
5548 if (continue_rebase) {
5549 error = rebase_complete(worktree, fileindex,
5550 branch, new_base_branch, tmp_branch, repo);
5553 /* Fast-forward the reference of the branch. */
5554 struct got_object_id *new_head_commit_id;
5556 error = got_ref_resolve(&new_head_commit_id, repo,
5560 error = got_object_id_str(&id_str, new_head_commit_id);
5561 printf("Forwarding %s to commit %s\n",
5562 got_ref_get_name(branch), id_str);
5564 error = got_ref_change_ref(branch,
5565 new_head_commit_id);
5572 SIMPLEQ_FOREACH(qid, &commits, entry) {
5573 commit_id = qid->id;
5574 parent_id = pid ? pid->id : yca_id;
5577 error = got_worktree_rebase_merge_files(&merged_paths,
5578 worktree, fileindex, parent_id, commit_id, repo,
5579 rebase_progress, &rebase_status, check_cancelled, NULL);
5583 if (rebase_status == GOT_STATUS_CONFLICT) {
5584 got_worktree_rebase_pathlist_free(&merged_paths);
5588 error = rebase_commit(&merged_paths, worktree, fileindex,
5589 tmp_branch, commit_id, repo);
5590 got_worktree_rebase_pathlist_free(&merged_paths);
5595 if (rebase_status == GOT_STATUS_CONFLICT) {
5596 error = got_worktree_rebase_postpone(worktree, fileindex);
5599 error = got_error_msg(GOT_ERR_CONFLICTS,
5600 "conflicts must be resolved before rebasing can continue");
5602 error = rebase_complete(worktree, fileindex, branch,
5603 new_base_branch, tmp_branch, repo);
5605 got_object_id_queue_free(&commits);
5606 free(branch_head_commit_id);
5607 free(resume_commit_id);
5610 got_object_commit_close(commit);
5612 got_ref_close(branch);
5613 if (new_base_branch)
5614 got_ref_close(new_base_branch);
5616 got_ref_close(tmp_branch);
5618 got_worktree_close(worktree);
5620 got_repo_close(repo);
5625 usage_histedit(void)
5627 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5632 #define GOT_HISTEDIT_PICK 'p'
5633 #define GOT_HISTEDIT_EDIT 'e'
5634 #define GOT_HISTEDIT_FOLD 'f'
5635 #define GOT_HISTEDIT_DROP 'd'
5636 #define GOT_HISTEDIT_MESG 'm'
5638 static struct got_histedit_cmd {
5642 } got_histedit_cmds[] = {
5643 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5644 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5645 { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5646 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5647 { GOT_HISTEDIT_MESG, "mesg",
5648 "single-line log message for commit above (open editor if empty)" },
5651 struct got_histedit_list_entry {
5652 TAILQ_ENTRY(got_histedit_list_entry) entry;
5653 struct got_object_id *commit_id;
5654 const struct got_histedit_cmd *cmd;
5657 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5659 static const struct got_error *
5660 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5661 FILE *f, struct got_repository *repo)
5663 const struct got_error *err = NULL;
5664 char *logmsg = NULL, *id_str = NULL;
5665 struct got_commit_object *commit = NULL;
5668 err = got_object_open_as_commit(&commit, repo, commit_id);
5672 err = get_short_logmsg(&logmsg, 34, commit);
5676 err = got_object_id_str(&id_str, commit_id);
5680 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5682 err = got_ferror(f, GOT_ERR_IO);
5685 got_object_commit_close(commit);
5691 static const struct got_error *
5692 histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5693 struct got_repository *repo)
5695 const struct got_error *err = NULL;
5696 struct got_object_qid *qid;
5698 if (SIMPLEQ_EMPTY(commits))
5699 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5701 SIMPLEQ_FOREACH(qid, commits, entry) {
5702 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5711 static const struct got_error *
5712 write_cmd_list(FILE *f)
5714 const struct got_error *err = NULL;
5717 n = fprintf(f, "# Available histedit commands:\n");
5719 return got_ferror(f, GOT_ERR_IO);
5721 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5722 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5723 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5726 err = got_ferror(f, GOT_ERR_IO);
5730 n = fprintf(f, "# Commits will be processed in order from top to "
5731 "bottom of this file.\n");
5733 return got_ferror(f, GOT_ERR_IO);
5737 static const struct got_error *
5738 histedit_syntax_error(int lineno)
5740 static char msg[42];
5743 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5745 if (ret == -1 || ret >= sizeof(msg))
5746 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5748 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5751 static const struct got_error *
5752 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5753 char *logmsg, struct got_repository *repo)
5755 const struct got_error *err;
5756 struct got_commit_object *folded_commit = NULL;
5757 char *id_str, *folded_logmsg = NULL;
5759 err = got_object_id_str(&id_str, hle->commit_id);
5763 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5767 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5770 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5771 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5772 folded_logmsg) == -1) {
5773 err = got_error_from_errno("asprintf");
5777 got_object_commit_close(folded_commit);
5779 free(folded_logmsg);
5783 static struct got_histedit_list_entry *
5784 get_folded_commits(struct got_histedit_list_entry *hle)
5786 struct got_histedit_list_entry *prev, *folded = NULL;
5788 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5789 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5790 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5791 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5793 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5799 static const struct got_error *
5800 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5801 struct got_repository *repo)
5803 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5804 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5805 const struct got_error *err = NULL;
5806 struct got_commit_object *commit = NULL;
5808 struct got_histedit_list_entry *folded = NULL;
5810 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5814 folded = get_folded_commits(hle);
5816 while (folded != hle) {
5817 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5818 folded = TAILQ_NEXT(folded, entry);
5821 err = append_folded_commit_msg(&new_msg, folded,
5827 folded = TAILQ_NEXT(folded, entry);
5831 err = got_object_id_str(&id_str, hle->commit_id);
5834 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5837 if (asprintf(&new_msg,
5838 "%s\n# original log message of commit %s: %s",
5839 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5840 err = got_error_from_errno("asprintf");
5846 err = got_object_id_str(&id_str, hle->commit_id);
5850 err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5854 dprintf(fd, logmsg);
5857 err = get_editor(&editor);
5861 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5863 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5865 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5868 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5869 err = got_error_from_errno2("unlink", logmsg_path);
5875 got_object_commit_close(commit);
5879 static const struct got_error *
5880 histedit_parse_list(struct got_histedit_list *histedit_cmds,
5881 FILE *f, struct got_repository *repo)
5883 const struct got_error *err = NULL;
5884 char *line = NULL, *p, *end;
5888 const struct got_histedit_cmd *cmd;
5889 struct got_object_id *commit_id = NULL;
5890 struct got_histedit_list_entry *hle = NULL;
5893 len = getline(&line, &size, f);
5895 const struct got_error *getline_err;
5898 getline_err = got_error_from_errno("getline");
5899 err = got_ferror(f, getline_err->code);
5904 while (isspace((unsigned char)p[0]))
5906 if (p[0] == '#' || p[0] == '\0') {
5912 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5913 cmd = &got_histedit_cmds[i];
5914 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5915 isspace((unsigned char)p[strlen(cmd->name)])) {
5916 p += strlen(cmd->name);
5919 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5924 if (i == nitems(got_histedit_cmds)) {
5925 err = histedit_syntax_error(lineno);
5928 while (isspace((unsigned char)p[0]))
5930 if (cmd->code == GOT_HISTEDIT_MESG) {
5931 if (hle == NULL || hle->logmsg != NULL) {
5932 err = got_error(GOT_ERR_HISTEDIT_CMD);
5936 err = histedit_edit_logmsg(hle, repo);
5940 hle->logmsg = strdup(p);
5941 if (hle->logmsg == NULL) {
5942 err = got_error_from_errno("strdup");
5951 while (end[0] && !isspace((unsigned char)end[0]))
5955 err = got_object_resolve_id_str(&commit_id, repo, p);
5957 /* override error code */
5958 err = histedit_syntax_error(lineno);
5962 hle = malloc(sizeof(*hle));
5964 err = got_error_from_errno("malloc");
5968 hle->commit_id = commit_id;
5973 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5981 static const struct got_error *
5982 histedit_check_script(struct got_histedit_list *histedit_cmds,
5983 struct got_object_id_queue *commits, struct got_repository *repo)
5985 const struct got_error *err = NULL;
5986 struct got_object_qid *qid;
5987 struct got_histedit_list_entry *hle;
5988 static char msg[80];
5991 if (TAILQ_EMPTY(histedit_cmds))
5992 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5993 "histedit script contains no commands");
5994 if (SIMPLEQ_EMPTY(commits))
5995 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5997 SIMPLEQ_FOREACH(qid, commits, entry) {
5998 TAILQ_FOREACH(hle, histedit_cmds, entry) {
5999 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6003 err = got_object_id_str(&id_str, qid->id);
6006 snprintf(msg, sizeof(msg),
6007 "commit %s missing from histedit script", id_str);
6009 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6013 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6014 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6015 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6016 "last commit in histedit script cannot be folded");
6021 static const struct got_error *
6022 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6023 const char *path, struct got_object_id_queue *commits,
6024 struct got_repository *repo)
6026 const struct got_error *err = NULL;
6030 err = get_editor(&editor);
6034 if (spawn_editor(editor, path) == -1) {
6035 err = got_error_from_errno("failed spawning editor");
6039 f = fopen(path, "r");
6041 err = got_error_from_errno("fopen");
6044 err = histedit_parse_list(histedit_cmds, f, repo);
6048 err = histedit_check_script(histedit_cmds, commits, repo);
6050 if (f && fclose(f) != 0 && err == NULL)
6051 err = got_error_from_errno("fclose");
6056 static const struct got_error *
6057 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6058 struct got_object_id_queue *, const char *, struct got_repository *);
6060 static const struct got_error *
6061 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6062 struct got_object_id_queue *commits, struct got_repository *repo)
6064 const struct got_error *err;
6068 err = got_opentemp_named(&path, &f, "got-histedit");
6072 err = write_cmd_list(f);
6076 err = histedit_write_commit_list(commits, f, repo);
6080 if (fclose(f) != 0) {
6081 err = got_error_from_errno("fclose");
6086 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6088 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6089 err->code != GOT_ERR_HISTEDIT_CMD)
6091 err = histedit_edit_list_retry(histedit_cmds, err,
6092 commits, path, repo);
6095 if (f && fclose(f) != 0 && err == NULL)
6096 err = got_error_from_errno("fclose");
6097 if (path && unlink(path) != 0 && err == NULL)
6098 err = got_error_from_errno2("unlink", path);
6103 static const struct got_error *
6104 histedit_save_list(struct got_histedit_list *histedit_cmds,
6105 struct got_worktree *worktree, struct got_repository *repo)
6107 const struct got_error *err = NULL;
6110 struct got_histedit_list_entry *hle;
6111 struct got_commit_object *commit = NULL;
6113 err = got_worktree_get_histedit_script_path(&path, worktree);
6117 f = fopen(path, "w");
6119 err = got_error_from_errno2("fopen", path);
6122 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6123 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6129 int n = fprintf(f, "%c %s\n",
6130 GOT_HISTEDIT_MESG, hle->logmsg);
6132 err = got_ferror(f, GOT_ERR_IO);
6138 if (f && fclose(f) != 0 && err == NULL)
6139 err = got_error_from_errno("fclose");
6142 got_object_commit_close(commit);
6147 histedit_free_list(struct got_histedit_list *histedit_cmds)
6149 struct got_histedit_list_entry *hle;
6151 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6152 TAILQ_REMOVE(histedit_cmds, hle, entry);
6157 static const struct got_error *
6158 histedit_load_list(struct got_histedit_list *histedit_cmds,
6159 const char *path, struct got_repository *repo)
6161 const struct got_error *err = NULL;
6164 f = fopen(path, "r");
6166 err = got_error_from_errno2("fopen", path);
6170 err = histedit_parse_list(histedit_cmds, f, repo);
6172 if (f && fclose(f) != 0 && err == NULL)
6173 err = got_error_from_errno("fclose");
6177 static const struct got_error *
6178 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6179 const struct got_error *edit_err, struct got_object_id_queue *commits,
6180 const char *path, struct got_repository *repo)
6182 const struct got_error *err = NULL, *prev_err = edit_err;
6185 while (resp != 'c' && resp != 'r' && resp != 'a') {
6186 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6187 "or (a)bort: ", getprogname(), prev_err->msg);
6192 histedit_free_list(histedit_cmds);
6193 err = histedit_run_editor(histedit_cmds, path, commits,
6196 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6197 err->code != GOT_ERR_HISTEDIT_CMD)
6204 } else if (resp == 'r') {
6205 histedit_free_list(histedit_cmds);
6206 err = histedit_edit_script(histedit_cmds,
6209 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6210 err->code != GOT_ERR_HISTEDIT_CMD)
6217 } else if (resp == 'a') {
6218 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6221 printf("invalid response '%c'\n", resp);
6227 static const struct got_error *
6228 histedit_complete(struct got_worktree *worktree,
6229 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6230 struct got_reference *branch, struct got_repository *repo)
6232 printf("Switching work tree to %s\n",
6233 got_ref_get_symref_target(branch));
6234 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6238 static const struct got_error *
6239 show_histedit_progress(struct got_commit_object *commit,
6240 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6242 const struct got_error *err;
6243 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6245 err = got_object_id_str(&old_id_str, hle->commit_id);
6250 err = got_object_id_str(&new_id_str, new_id);
6255 old_id_str[12] = '\0';
6257 new_id_str[12] = '\0';
6260 logmsg = strdup(hle->logmsg);
6261 if (logmsg == NULL) {
6262 err = got_error_from_errno("strdup");
6265 trim_logmsg(logmsg, 42);
6267 err = get_short_logmsg(&logmsg, 42, commit);
6272 switch (hle->cmd->code) {
6273 case GOT_HISTEDIT_PICK:
6274 case GOT_HISTEDIT_EDIT:
6275 printf("%s -> %s: %s\n", old_id_str,
6276 new_id_str ? new_id_str : "no-op change", logmsg);
6278 case GOT_HISTEDIT_DROP:
6279 case GOT_HISTEDIT_FOLD:
6280 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6292 static const struct got_error *
6293 histedit_commit(struct got_pathlist_head *merged_paths,
6294 struct got_worktree *worktree, struct got_fileindex *fileindex,
6295 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6296 struct got_repository *repo)
6298 const struct got_error *err;
6299 struct got_commit_object *commit;
6300 struct got_object_id *new_commit_id;
6302 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6303 && hle->logmsg == NULL) {
6304 err = histedit_edit_logmsg(hle, repo);
6309 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6313 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6314 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6317 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6319 err = show_histedit_progress(commit, hle, NULL);
6321 err = show_histedit_progress(commit, hle, new_commit_id);
6322 free(new_commit_id);
6325 got_object_commit_close(commit);
6329 static const struct got_error *
6330 histedit_skip_commit(struct got_histedit_list_entry *hle,
6331 struct got_worktree *worktree, struct got_repository *repo)
6333 const struct got_error *error;
6334 struct got_commit_object *commit;
6336 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6341 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6345 error = show_histedit_progress(commit, hle, NULL);
6346 got_object_commit_close(commit);
6350 static const struct got_error *
6351 cmd_histedit(int argc, char *argv[])
6353 const struct got_error *error = NULL;
6354 struct got_worktree *worktree = NULL;
6355 struct got_fileindex *fileindex = NULL;
6356 struct got_repository *repo = NULL;
6358 struct got_reference *branch = NULL;
6359 struct got_reference *tmp_branch = NULL;
6360 struct got_object_id *resume_commit_id = NULL;
6361 struct got_object_id *base_commit_id = NULL;
6362 struct got_object_id *head_commit_id = NULL;
6363 struct got_commit_object *commit = NULL;
6364 int ch, rebase_in_progress = 0, did_something;
6365 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6366 const char *edit_script_path = NULL;
6367 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6368 struct got_object_id_queue commits;
6369 struct got_pathlist_head merged_paths;
6370 const struct got_object_id_queue *parent_ids;
6371 struct got_object_qid *pid;
6372 struct got_histedit_list histedit_cmds;
6373 struct got_histedit_list_entry *hle;
6375 SIMPLEQ_INIT(&commits);
6376 TAILQ_INIT(&histedit_cmds);
6377 TAILQ_INIT(&merged_paths);
6379 while ((ch = getopt(argc, argv, "acF:")) != -1) {
6388 edit_script_path = optarg;
6400 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6401 "unveil", NULL) == -1)
6404 if (abort_edit && continue_edit)
6410 * This command cannot apply unveil(2) in all cases because the
6411 * user may choose to run an editor to edit the histedit script
6412 * and to edit individual commit log messages.
6413 * unveil(2) traverses exec(2); if an editor is used we have to
6414 * apply unveil after edit script and log messages have been written.
6415 * XXX TODO: Make use of unveil(2) where possible.
6418 cwd = getcwd(NULL, 0);
6420 error = got_error_from_errno("getcwd");
6423 error = got_worktree_open(&worktree, cwd);
6427 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6432 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6435 if (rebase_in_progress) {
6436 error = got_error(GOT_ERR_REBASING);
6440 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6444 if (edit_in_progress && abort_edit) {
6445 error = got_worktree_histedit_continue(&resume_commit_id,
6446 &tmp_branch, &branch, &base_commit_id, &fileindex,
6450 printf("Switching work tree to %s\n",
6451 got_ref_get_symref_target(branch));
6452 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6453 branch, base_commit_id, update_progress, &did_something);
6456 printf("Histedit of %s aborted\n",
6457 got_ref_get_symref_target(branch));
6458 goto done; /* nothing else to do */
6459 } else if (abort_edit) {
6460 error = got_error(GOT_ERR_NOT_HISTEDIT);
6464 if (continue_edit) {
6467 if (!edit_in_progress) {
6468 error = got_error(GOT_ERR_NOT_HISTEDIT);
6472 error = got_worktree_get_histedit_script_path(&path, worktree);
6476 error = histedit_load_list(&histedit_cmds, path, repo);
6481 error = got_worktree_histedit_continue(&resume_commit_id,
6482 &tmp_branch, &branch, &base_commit_id, &fileindex,
6487 error = got_ref_resolve(&head_commit_id, repo, branch);
6491 error = got_object_open_as_commit(&commit, repo,
6495 parent_ids = got_object_commit_get_parent_ids(commit);
6496 pid = SIMPLEQ_FIRST(parent_ids);
6498 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6501 error = collect_commits(&commits, head_commit_id, pid->id,
6502 base_commit_id, got_worktree_get_path_prefix(worktree),
6503 GOT_ERR_HISTEDIT_PATH, repo);
6504 got_object_commit_close(commit);
6509 if (edit_in_progress) {
6510 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6514 error = got_ref_open(&branch, repo,
6515 got_worktree_get_head_ref_name(worktree), 0);
6519 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6520 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6521 "will not edit commit history of a branch outside "
6522 "the \"refs/heads/\" reference namespace");
6526 error = got_ref_resolve(&head_commit_id, repo, branch);
6527 got_ref_close(branch);
6532 error = got_object_open_as_commit(&commit, repo,
6536 parent_ids = got_object_commit_get_parent_ids(commit);
6537 pid = SIMPLEQ_FIRST(parent_ids);
6539 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6542 error = collect_commits(&commits, head_commit_id, pid->id,
6543 got_worktree_get_base_commit_id(worktree),
6544 got_worktree_get_path_prefix(worktree),
6545 GOT_ERR_HISTEDIT_PATH, repo);
6546 got_object_commit_close(commit);
6551 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6552 &base_commit_id, &fileindex, worktree, repo);
6556 if (edit_script_path) {
6557 error = histedit_load_list(&histedit_cmds,
6558 edit_script_path, repo);
6560 got_worktree_histedit_abort(worktree, fileindex,
6561 repo, branch, base_commit_id,
6562 update_progress, &did_something);
6566 error = histedit_edit_script(&histedit_cmds, &commits,
6569 got_worktree_histedit_abort(worktree, fileindex,
6570 repo, branch, base_commit_id,
6571 update_progress, &did_something);
6577 error = histedit_save_list(&histedit_cmds, worktree,
6580 got_worktree_histedit_abort(worktree, fileindex,
6581 repo, branch, base_commit_id,
6582 update_progress, &did_something);
6588 error = histedit_check_script(&histedit_cmds, &commits, repo);
6592 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6593 if (resume_commit_id) {
6594 if (got_object_id_cmp(hle->commit_id,
6595 resume_commit_id) != 0)
6598 resume_commit_id = NULL;
6599 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6600 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6601 error = histedit_skip_commit(hle, worktree,
6604 error = histedit_commit(NULL, worktree,
6605 fileindex, tmp_branch, hle, repo);
6612 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6613 error = histedit_skip_commit(hle, worktree, repo);
6619 error = got_object_open_as_commit(&commit, repo,
6623 parent_ids = got_object_commit_get_parent_ids(commit);
6624 pid = SIMPLEQ_FIRST(parent_ids);
6626 error = got_worktree_histedit_merge_files(&merged_paths,
6627 worktree, fileindex, pid->id, hle->commit_id, repo,
6628 rebase_progress, &rebase_status, check_cancelled, NULL);
6631 got_object_commit_close(commit);
6634 if (rebase_status == GOT_STATUS_CONFLICT) {
6635 got_worktree_rebase_pathlist_free(&merged_paths);
6639 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6641 error = got_object_id_str(&id_str, hle->commit_id);
6644 printf("Stopping histedit for amending commit %s\n",
6647 got_worktree_rebase_pathlist_free(&merged_paths);
6648 error = got_worktree_histedit_postpone(worktree,
6653 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6654 error = histedit_skip_commit(hle, worktree, repo);
6660 error = histedit_commit(&merged_paths, worktree, fileindex,
6661 tmp_branch, hle, repo);
6662 got_worktree_rebase_pathlist_free(&merged_paths);
6667 if (rebase_status == GOT_STATUS_CONFLICT) {
6668 error = got_worktree_histedit_postpone(worktree, fileindex);
6671 error = got_error_msg(GOT_ERR_CONFLICTS,
6672 "conflicts must be resolved before rebasing can continue");
6674 error = histedit_complete(worktree, fileindex, tmp_branch,
6677 got_object_id_queue_free(&commits);
6678 histedit_free_list(&histedit_cmds);
6679 free(head_commit_id);
6680 free(base_commit_id);
6681 free(resume_commit_id);
6683 got_object_commit_close(commit);
6685 got_ref_close(branch);
6687 got_ref_close(tmp_branch);
6689 got_worktree_close(worktree);
6691 got_repo_close(repo);
6696 usage_integrate(void)
6698 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6702 static const struct got_error *
6703 cmd_integrate(int argc, char *argv[])
6705 const struct got_error *error = NULL;
6706 struct got_repository *repo = NULL;
6707 struct got_worktree *worktree = NULL;
6708 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6709 const char *branch_arg = NULL;
6710 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6711 struct got_fileindex *fileindex = NULL;
6712 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6713 int ch, did_something = 0;
6715 while ((ch = getopt(argc, argv, "")) != -1) {
6728 branch_arg = argv[0];
6730 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6731 "unveil", NULL) == -1)
6734 cwd = getcwd(NULL, 0);
6736 error = got_error_from_errno("getcwd");
6740 error = got_worktree_open(&worktree, cwd);
6744 error = check_rebase_or_histedit_in_progress(worktree);
6748 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6753 error = apply_unveil(got_repo_get_path(repo), 0,
6754 got_worktree_get_root_path(worktree));
6758 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6759 error = got_error_from_errno("asprintf");
6763 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6764 &base_branch_ref, worktree, refname, repo);
6768 refname = strdup(got_ref_get_name(branch_ref));
6769 if (refname == NULL) {
6770 error = got_error_from_errno("strdup");
6771 got_worktree_integrate_abort(worktree, fileindex, repo,
6772 branch_ref, base_branch_ref);
6775 base_refname = strdup(got_ref_get_name(base_branch_ref));
6776 if (base_refname == NULL) {
6777 error = got_error_from_errno("strdup");
6778 got_worktree_integrate_abort(worktree, fileindex, repo,
6779 branch_ref, base_branch_ref);
6783 error = got_ref_resolve(&commit_id, repo, branch_ref);
6787 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6791 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6792 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6793 "specified branch has already been integrated");
6794 got_worktree_integrate_abort(worktree, fileindex, repo,
6795 branch_ref, base_branch_ref);
6799 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6801 if (error->code == GOT_ERR_ANCESTRY)
6802 error = got_error(GOT_ERR_REBASE_REQUIRED);
6803 got_worktree_integrate_abort(worktree, fileindex, repo,
6804 branch_ref, base_branch_ref);
6808 error = got_worktree_integrate_continue(worktree, fileindex, repo,
6809 branch_ref, base_branch_ref, update_progress, &did_something,
6810 check_cancelled, NULL);
6814 printf("Integrated %s into %s\n", refname, base_refname);
6817 got_repo_close(repo);
6819 got_worktree_close(worktree);
6821 free(base_commit_id);
6831 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6832 "[file-path ...]\n",
6837 static const struct got_error *
6838 print_stage(void *arg, unsigned char status, unsigned char staged_status,
6839 const char *path, struct got_object_id *blob_id,
6840 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6841 int dirfd, const char *de_name)
6843 const struct got_error *err = NULL;
6844 char *id_str = NULL;
6846 if (staged_status != GOT_STATUS_ADD &&
6847 staged_status != GOT_STATUS_MODIFY &&
6848 staged_status != GOT_STATUS_DELETE)
6851 if (staged_status == GOT_STATUS_ADD ||
6852 staged_status == GOT_STATUS_MODIFY)
6853 err = got_object_id_str(&id_str, staged_blob_id);
6855 err = got_object_id_str(&id_str, blob_id);
6859 printf("%s %c %s\n", id_str, staged_status, path);
6864 static const struct got_error *
6865 cmd_stage(int argc, char *argv[])
6867 const struct got_error *error = NULL;
6868 struct got_repository *repo = NULL;
6869 struct got_worktree *worktree = NULL;
6871 struct got_pathlist_head paths;
6872 struct got_pathlist_entry *pe;
6873 int ch, list_stage = 0, pflag = 0;
6874 FILE *patch_script_file = NULL;
6875 const char *patch_script_path = NULL;
6876 struct choose_patch_arg cpa;
6880 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6889 patch_script_path = optarg;
6901 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6902 "unveil", NULL) == -1)
6905 if (list_stage && (pflag || patch_script_path))
6906 errx(1, "-l option cannot be used with other options");
6907 if (patch_script_path && !pflag)
6908 errx(1, "-F option can only be used together with -p option");
6910 cwd = getcwd(NULL, 0);
6912 error = got_error_from_errno("getcwd");
6916 error = got_worktree_open(&worktree, cwd);
6920 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6925 if (patch_script_path) {
6926 patch_script_file = fopen(patch_script_path, "r");
6927 if (patch_script_file == NULL) {
6928 error = got_error_from_errno2("fopen",
6933 error = apply_unveil(got_repo_get_path(repo), 0,
6934 got_worktree_get_root_path(worktree));
6938 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6943 error = got_worktree_status(worktree, &paths, repo,
6944 print_stage, NULL, check_cancelled, NULL);
6946 cpa.patch_script_file = patch_script_file;
6947 cpa.action = "stage";
6948 error = got_worktree_stage(worktree, &paths,
6949 pflag ? NULL : print_status, NULL,
6950 pflag ? choose_patch : NULL, &cpa, repo);
6953 if (patch_script_file && fclose(patch_script_file) == EOF &&
6955 error = got_error_from_errno2("fclose", patch_script_path);
6957 got_repo_close(repo);
6959 got_worktree_close(worktree);
6960 TAILQ_FOREACH(pe, &paths, entry)
6961 free((char *)pe->path);
6962 got_pathlist_free(&paths);
6970 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6971 "[file-path ...]\n",
6977 static const struct got_error *
6978 cmd_unstage(int argc, char *argv[])
6980 const struct got_error *error = NULL;
6981 struct got_repository *repo = NULL;
6982 struct got_worktree *worktree = NULL;
6984 struct got_pathlist_head paths;
6985 struct got_pathlist_entry *pe;
6986 int ch, did_something = 0, pflag = 0;
6987 FILE *patch_script_file = NULL;
6988 const char *patch_script_path = NULL;
6989 struct choose_patch_arg cpa;
6993 while ((ch = getopt(argc, argv, "pF:")) != -1) {
6999 patch_script_path = optarg;
7011 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7012 "unveil", NULL) == -1)
7015 if (patch_script_path && !pflag)
7016 errx(1, "-F option can only be used together with -p option");
7018 cwd = getcwd(NULL, 0);
7020 error = got_error_from_errno("getcwd");
7024 error = got_worktree_open(&worktree, cwd);
7028 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7033 if (patch_script_path) {
7034 patch_script_file = fopen(patch_script_path, "r");
7035 if (patch_script_file == NULL) {
7036 error = got_error_from_errno2("fopen",
7042 error = apply_unveil(got_repo_get_path(repo), 0,
7043 got_worktree_get_root_path(worktree));
7047 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7051 cpa.patch_script_file = patch_script_file;
7052 cpa.action = "unstage";
7053 error = got_worktree_unstage(worktree, &paths, update_progress,
7054 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7056 if (patch_script_file && fclose(patch_script_file) == EOF &&
7058 error = got_error_from_errno2("fclose", patch_script_path);
7060 got_repo_close(repo);
7062 got_worktree_close(worktree);
7063 TAILQ_FOREACH(pe, &paths, entry)
7064 free((char *)pe->path);
7065 got_pathlist_free(&paths);
7073 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7074 "arg1 [arg2 ...]\n", getprogname());
7078 static const struct got_error *
7079 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7081 const struct got_error *err;
7082 struct got_blob_object *blob;
7084 err = got_object_open_as_blob(&blob, repo, id, 8192);
7088 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7089 got_object_blob_close(blob);
7093 static const struct got_error *
7094 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7096 const struct got_error *err;
7097 struct got_tree_object *tree;
7100 err = got_object_open_as_tree(&tree, repo, id);
7104 nentries = got_object_tree_get_nentries(tree);
7105 for (i = 0; i < nentries; i++) {
7106 struct got_tree_entry *te;
7108 if (sigint_received || sigpipe_received)
7110 te = got_object_tree_get_entry(tree, i);
7111 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7114 fprintf(outfile, "%s %.7o %s\n", id_str,
7115 got_tree_entry_get_mode(te),
7116 got_tree_entry_get_name(te));
7120 got_object_tree_close(tree);
7124 static const struct got_error *
7125 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7127 const struct got_error *err;
7128 struct got_commit_object *commit;
7129 const struct got_object_id_queue *parent_ids;
7130 struct got_object_qid *pid;
7131 char *id_str = NULL;
7132 const char *logmsg = NULL;
7134 err = got_object_open_as_commit(&commit, repo, id);
7138 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7142 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7143 parent_ids = got_object_commit_get_parent_ids(commit);
7144 fprintf(outfile, "numparents %d\n",
7145 got_object_commit_get_nparents(commit));
7146 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7148 err = got_object_id_str(&pid_str, pid->id);
7151 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7154 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7155 got_object_commit_get_author(commit),
7156 got_object_commit_get_author_time(commit));
7158 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7159 got_object_commit_get_author(commit),
7160 got_object_commit_get_committer_time(commit));
7162 logmsg = got_object_commit_get_logmsg_raw(commit);
7163 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7164 fprintf(outfile, "%s", logmsg);
7167 got_object_commit_close(commit);
7171 static const struct got_error *
7172 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7174 const struct got_error *err;
7175 struct got_tag_object *tag;
7176 char *id_str = NULL;
7177 const char *tagmsg = NULL;
7179 err = got_object_open_as_tag(&tag, repo, id);
7183 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7187 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7189 switch (got_object_tag_get_object_type(tag)) {
7190 case GOT_OBJ_TYPE_BLOB:
7191 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7192 GOT_OBJ_LABEL_BLOB);
7194 case GOT_OBJ_TYPE_TREE:
7195 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7196 GOT_OBJ_LABEL_TREE);
7198 case GOT_OBJ_TYPE_COMMIT:
7199 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7200 GOT_OBJ_LABEL_COMMIT);
7202 case GOT_OBJ_TYPE_TAG:
7203 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7210 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7211 got_object_tag_get_name(tag));
7213 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7214 got_object_tag_get_tagger(tag),
7215 got_object_tag_get_tagger_time(tag));
7217 tagmsg = got_object_tag_get_message(tag);
7218 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7219 fprintf(outfile, "%s", tagmsg);
7222 got_object_tag_close(tag);
7226 static const struct got_error *
7227 cmd_cat(int argc, char *argv[])
7229 const struct got_error *error;
7230 struct got_repository *repo = NULL;
7231 struct got_worktree *worktree = NULL;
7232 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7233 const char *commit_id_str = NULL;
7234 struct got_object_id *id = NULL, *commit_id = NULL;
7235 int ch, obj_type, i, force_path = 0;
7238 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7243 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7246 commit_id_str = optarg;
7249 repo_path = realpath(optarg, NULL);
7250 if (repo_path == NULL)
7251 return got_error_from_errno2("realpath",
7253 got_path_strip_trailing_slashes(repo_path);
7267 cwd = getcwd(NULL, 0);
7269 error = got_error_from_errno("getcwd");
7272 error = got_worktree_open(&worktree, cwd);
7273 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7276 if (repo_path == NULL) {
7278 got_worktree_get_repo_path(worktree));
7279 if (repo_path == NULL) {
7280 error = got_error_from_errno("strdup");
7286 if (repo_path == NULL) {
7287 repo_path = getcwd(NULL, 0);
7288 if (repo_path == NULL)
7289 return got_error_from_errno("getcwd");
7292 error = got_repo_open(&repo, repo_path, NULL);
7297 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7301 if (commit_id_str == NULL)
7302 commit_id_str = GOT_REF_HEAD;
7303 error = got_repo_match_object_id(&commit_id, NULL,
7304 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7308 for (i = 0; i < argc; i++) {
7310 error = got_object_id_by_path(&id, repo, commit_id,
7315 error = got_repo_match_object_id(&id, &label, argv[i],
7316 GOT_OBJ_TYPE_ANY, 0, repo);
7318 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7319 error->code != GOT_ERR_NOT_REF)
7321 error = got_object_id_by_path(&id, repo,
7322 commit_id, argv[i]);
7328 error = got_object_get_type(&obj_type, repo, id);
7333 case GOT_OBJ_TYPE_BLOB:
7334 error = cat_blob(id, repo, stdout);
7336 case GOT_OBJ_TYPE_TREE:
7337 error = cat_tree(id, repo, stdout);
7339 case GOT_OBJ_TYPE_COMMIT:
7340 error = cat_commit(id, repo, stdout);
7342 case GOT_OBJ_TYPE_TAG:
7343 error = cat_tag(id, repo, stdout);
7346 error = got_error(GOT_ERR_OBJ_TYPE);
7361 got_worktree_close(worktree);
7363 const struct got_error *repo_error;
7364 repo_error = got_repo_close(repo);