2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_opentemp.h"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 static volatile sig_atomic_t sigint_received;
62 static volatile sig_atomic_t sigpipe_received;
65 catch_sigint(int signo)
71 catch_sigpipe(int signo)
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
84 __dead static void usage(int);
85 __dead static void usage_init(void);
86 __dead static void usage_import(void);
87 __dead static void usage_checkout(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_update(void);
90 __dead static void usage_log(void);
91 __dead static void usage_diff(void);
92 __dead static void usage_blame(void);
93 __dead static void usage_tree(void);
94 __dead static void usage_status(void);
95 __dead static void usage_ref(void);
96 __dead static void usage_branch(void);
97 __dead static void usage_tag(void);
98 __dead static void usage_add(void);
99 __dead static void usage_remove(void);
100 __dead static void usage_revert(void);
101 __dead static void usage_commit(void);
102 __dead static void usage_cherrypick(void);
103 __dead static void usage_backout(void);
104 __dead static void usage_rebase(void);
105 __dead static void usage_histedit(void);
106 __dead static void usage_integrate(void);
107 __dead static void usage_stage(void);
108 __dead static void usage_unstage(void);
109 __dead static void usage_cat(void);
111 static const struct got_error* cmd_init(int, char *[]);
112 static const struct got_error* cmd_import(int, char *[]);
113 static const struct got_error* cmd_clone(int, char *[]);
114 static const struct got_error* cmd_checkout(int, char *[]);
115 static const struct got_error* cmd_update(int, char *[]);
116 static const struct got_error* cmd_log(int, char *[]);
117 static const struct got_error* cmd_diff(int, char *[]);
118 static const struct got_error* cmd_blame(int, char *[]);
119 static const struct got_error* cmd_tree(int, char *[]);
120 static const struct got_error* cmd_status(int, char *[]);
121 static const struct got_error* cmd_ref(int, char *[]);
122 static const struct got_error* cmd_branch(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_revert(int, char *[]);
127 static const struct got_error* cmd_commit(int, char *[]);
128 static const struct got_error* cmd_cherrypick(int, char *[]);
129 static const struct got_error* cmd_backout(int, char *[]);
130 static const struct got_error* cmd_rebase(int, char *[]);
131 static const struct got_error* cmd_histedit(int, char *[]);
132 static const struct got_error* cmd_integrate(int, char *[]);
133 static const struct got_error* cmd_stage(int, char *[]);
134 static const struct got_error* cmd_unstage(int, char *[]);
135 static const struct got_error* cmd_cat(int, char *[]);
137 static struct got_cmd got_commands[] = {
138 { "init", cmd_init, usage_init, "in" },
139 { "import", cmd_import, usage_import, "im" },
140 { "checkout", cmd_checkout, usage_checkout, "co" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "update", cmd_update, usage_update, "up" },
143 { "log", cmd_log, usage_log, "" },
144 { "diff", cmd_diff, usage_diff, "di" },
145 { "blame", cmd_blame, usage_blame, "bl" },
146 { "tree", cmd_tree, usage_tree, "tr" },
147 { "status", cmd_status, usage_status, "st" },
148 { "ref", cmd_ref, usage_ref, "" },
149 { "branch", cmd_branch, usage_branch, "br" },
150 { "tag", cmd_tag, usage_tag, "" },
151 { "add", cmd_add, usage_add, "" },
152 { "remove", cmd_remove, usage_remove, "rm" },
153 { "revert", cmd_revert, usage_revert, "rv" },
154 { "commit", cmd_commit, usage_commit, "ci" },
155 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 { "backout", cmd_backout, usage_backout, "bo" },
157 { "rebase", cmd_rebase, usage_rebase, "rb" },
158 { "histedit", cmd_histedit, usage_histedit, "he" },
159 { "integrate", cmd_integrate, usage_integrate,"ig" },
160 { "stage", cmd_stage, usage_stage, "sg" },
161 { "unstage", cmd_unstage, usage_unstage, "ug" },
162 { "cat", cmd_cat, usage_cat, "" },
170 fprintf(stderr, "commands:");
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct got_cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s", cmd->cmd_name);
179 main(int argc, char *argv[])
184 int hflag = 0, Vflag = 0;
185 static struct option longopts[] = {
186 { "version", no_argument, NULL, 'V' },
190 setlocale(LC_CTYPE, "");
192 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
211 got_version_print_str();
218 signal(SIGINT, catch_sigint);
219 signal(SIGPIPE, catch_sigpipe);
221 for (i = 0; i < nitems(got_commands); i++) {
222 const struct got_error *error;
224 cmd = &got_commands[i];
226 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 strcmp(cmd->cmd_alias, argv[0]) != 0)
231 got_commands[i].cmd_usage();
233 error = got_commands[i].cmd_main(argc, argv);
234 if (error && error->code != GOT_ERR_CANCELLED &&
235 error->code != GOT_ERR_PRIVSEP_EXIT &&
236 !(sigpipe_received &&
237 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
247 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
255 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
262 static const struct got_error *
263 get_editor(char **abspath)
265 const struct got_error *err = NULL;
270 editor = getenv("VISUAL");
272 editor = getenv("EDITOR");
275 err = got_path_find_prog(abspath, editor);
280 if (*abspath == NULL) {
281 *abspath = strdup("/bin/ed");
282 if (*abspath == NULL)
283 return got_error_from_errno("strdup");
289 static const struct got_error *
290 apply_unveil(const char *repo_path, int repo_read_only,
291 const char *worktree_path)
293 const struct got_error *err;
296 if (unveil("gmon.out", "rwc") != 0)
297 return got_error_from_errno2("unveil", "gmon.out");
299 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 return got_error_from_errno2("unveil", repo_path);
302 if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 return got_error_from_errno2("unveil", worktree_path);
305 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 err = got_privsep_unveil_exec_helpers();
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
321 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
325 static const struct got_error *
326 cmd_init(int argc, char *argv[])
328 const struct got_error *error = NULL;
329 char *repo_path = NULL;
332 while ((ch = getopt(argc, argv, "")) != -1) {
344 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
350 repo_path = strdup(argv[0]);
351 if (repo_path == NULL)
352 return got_error_from_errno("strdup");
354 got_path_strip_trailing_slashes(repo_path);
356 error = got_path_mkdir(repo_path);
358 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
361 error = apply_unveil(repo_path, 0, NULL);
365 error = got_repo_init(repo_path);
374 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 "[-r repository-path] [-I pattern] path\n", getprogname());
380 spawn_editor(const char *editor, const char *file)
383 sig_t sighup, sigint, sigquit;
386 sighup = signal(SIGHUP, SIG_IGN);
387 sigint = signal(SIGINT, SIG_IGN);
388 sigquit = signal(SIGQUIT, SIG_IGN);
390 switch (pid = fork()) {
394 execl(editor, editor, file, (char *)NULL);
398 while (waitpid(pid, &st, 0) == -1)
403 (void)signal(SIGHUP, sighup);
404 (void)signal(SIGINT, sigint);
405 (void)signal(SIGQUIT, sigquit);
407 if (!WIFEXITED(st)) {
412 return WEXITSTATUS(st);
415 static const struct got_error *
416 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 const char *initial_content)
419 const struct got_error *err = NULL;
423 int content_changed = 0;
428 if (stat(logmsg_path, &st) == -1)
429 return got_error_from_errno2("stat", logmsg_path);
431 if (spawn_editor(editor, logmsg_path) == -1)
432 return got_error_from_errno("failed spawning editor");
434 if (stat(logmsg_path, &st2) == -1)
435 return got_error_from_errno("stat");
437 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "no changes made to commit message, aborting");
441 *logmsg = malloc(st2.st_size + 1);
443 return got_error_from_errno("malloc");
447 fp = fopen(logmsg_path, "r");
449 err = got_error_from_errno("fopen");
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (!content_changed && strcmp(buf, initial_content) != 0)
455 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 continue; /* remove comments and leading empty lines */
457 len = strlcat(*logmsg, buf, st2.st_size);
461 while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 (*logmsg)[len - 1] = '\0';
466 if (len == 0 || !content_changed)
467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 "commit message cannot be empty, aborting");
477 static const struct got_error *
478 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 const char *path_dir, const char *branch_name)
481 char *initial_content = NULL;
482 const struct got_error *err = NULL;
485 if (asprintf(&initial_content,
486 "\n# %s to be imported to branch %s\n", path_dir,
488 return got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(logmsg_path, &fd,
491 GOT_TMPDIR_STR "/got-importmsg");
495 dprintf(fd, initial_content);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 free(initial_content);
504 static const struct got_error *
505 import_progress(void *arg, const char *path)
507 printf("A %s\n", path);
511 static const struct got_error *
512 get_author(char **author, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 const char *got_author, *name, *email;
519 name = got_repo_get_gitconfig_author_name(repo);
520 email = got_repo_get_gitconfig_author_email(repo);
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
527 got_author = getenv("GOT_AUTHOR");
528 if (got_author == NULL) {
529 name = got_repo_get_global_gitconfig_author_name(repo);
530 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (asprintf(author, "%s <%s>", name, email) == -1)
533 return got_error_from_errno("asprintf");
536 /* TODO: Look up user in password database? */
537 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 *author = strdup(got_author);
542 return got_error_from_errno("strdup");
545 * Really dumb email address check; we're only doing this to
546 * avoid git's object parser breaking on commits we create.
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);
560 while (*got_author && *got_author != '>')
562 if (*got_author != '>')
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
572 static const struct got_error *
573 get_gitconfig_path(char **gitconfig_path)
575 const char *homedir = getenv("HOME");
577 *gitconfig_path = NULL;
579 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 return got_error_from_errno("asprintf");
586 static const struct got_error *
587 cmd_import(int argc, char *argv[])
589 const struct got_error *error = NULL;
590 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 const char *branch_name = "main";
593 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 struct got_repository *repo = NULL;
595 struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 struct got_object_id *new_commit_id = NULL;
598 struct got_pathlist_head ignores;
599 struct got_pathlist_entry *pe;
600 int preserve_logmsg = 0;
602 TAILQ_INIT(&ignores);
604 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
607 branch_name = optarg;
610 logmsg = strdup(optarg);
611 if (logmsg == NULL) {
612 error = got_error_from_errno("strdup");
617 repo_path = realpath(optarg, NULL);
618 if (repo_path == NULL) {
619 error = got_error_from_errno2("realpath",
625 if (optarg[0] == '\0')
627 error = got_pathlist_insert(&pe, &ignores, optarg,
642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
650 if (repo_path == NULL) {
651 repo_path = getcwd(NULL, 0);
652 if (repo_path == NULL)
653 return got_error_from_errno("getcwd");
655 got_path_strip_trailing_slashes(repo_path);
656 error = get_gitconfig_path(&gitconfig_path);
659 error = got_repo_open(&repo, repo_path, gitconfig_path);
663 error = get_author(&author, repo);
668 * Don't let the user create a branch name with a leading '-'.
669 * While technically a valid reference name, this case is usually
670 * an unintended typo.
672 if (branch_name[0] == '-')
673 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 error = got_error_from_errno("asprintf");
680 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error->code != GOT_ERR_NOT_REF)
685 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 "import target branch already exists");
690 path_dir = realpath(argv[0], NULL);
691 if (path_dir == NULL) {
692 error = got_error_from_errno2("realpath", argv[0]);
695 got_path_strip_trailing_slashes(path_dir);
698 * unveil(2) traverses exec(2); if an editor is used we have
699 * to apply unveil after the log message has been written.
701 if (logmsg == NULL || strlen(logmsg) == 0) {
702 error = get_editor(&editor);
706 error = collect_import_msg(&logmsg, &logmsg_path, editor,
709 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
716 if (unveil(path_dir, "r") != 0) {
717 error = got_error_from_errno2("unveil", path_dir);
723 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
730 error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 author, &ignores, repo, import_progress, NULL);
738 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
745 error = got_ref_write(branch_ref, repo);
752 error = got_object_id_str(&id_str, new_commit_id);
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error->code != GOT_ERR_NOT_REF) {
767 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
775 error = got_ref_write(head_ref, repo);
783 printf("Created branch %s with commit %s\n",
784 got_ref_get_name(branch_ref), id_str);
786 if (preserve_logmsg) {
787 fprintf(stderr, "%s: log message preserved in %s\n",
788 getprogname(), logmsg_path);
789 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 error = got_error_from_errno2("unlink", logmsg_path);
799 free(gitconfig_path);
801 got_ref_close(branch_ref);
803 got_ref_close(head_ref);
810 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
817 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 "[-p prefix] repository-path [worktree-path]\n", getprogname());
823 show_worktree_base_ref_warning(void)
825 fprintf(stderr, "%s: warning: could not create a reference "
826 "to the work tree's base commit; the commit could be "
827 "garbage-collected by Git; making the repository "
828 "writable and running 'got update' will prevent this\n",
832 struct got_checkout_progress_arg {
833 const char *worktree_path;
834 int had_base_commit_ref_error;
837 static const struct got_error *
838 checkout_progress(void *arg, unsigned char status, const char *path)
840 struct got_checkout_progress_arg *a = arg;
842 /* Base commit bump happens silently. */
843 if (status == GOT_STATUS_BUMP_BASE)
846 if (status == GOT_STATUS_BASE_REF_ERR) {
847 a->had_base_commit_ref_error = 1;
851 while (path[0] == '/')
854 printf("%c %s/%s\n", status, a->worktree_path, path);
858 static const struct got_error *
859 check_cancelled(void *arg)
861 if (sigint_received || sigpipe_received)
862 return got_error(GOT_ERR_CANCELLED);
866 static const struct got_error *
867 check_linear_ancestry(struct got_object_id *commit_id,
868 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_object_id *yca_id;
874 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 commit_id, base_commit_id, repo, check_cancelled, NULL);
880 return got_error(GOT_ERR_ANCESTRY);
883 * Require a straight line of history between the target commit
884 * and the work tree's base commit.
886 * Non-linear situations such as this require a rebase:
888 * (commit) D F (base_commit)
896 * 'got update' only handles linear cases:
897 * Update forwards in time: A (base/yca) - B - C - D (commit)
898 * Update backwards in time: D (base) - C - B - A (commit/yca)
900 if (allow_forwards_in_time_only) {
901 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 return got_error(GOT_ERR_ANCESTRY);
903 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 got_object_id_cmp(base_commit_id, yca_id) != 0)
905 return got_error(GOT_ERR_ANCESTRY);
911 static const struct got_error *
912 check_same_branch(struct got_object_id *commit_id,
913 struct got_reference *head_ref, struct got_object_id *yca_id,
914 struct got_repository *repo)
916 const struct got_error *err = NULL;
917 struct got_commit_graph *graph = NULL;
918 struct got_object_id *head_commit_id = NULL;
919 int is_same_branch = 0;
921 err = got_ref_resolve(&head_commit_id, repo, head_ref);
925 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
929 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
934 err = got_commit_graph_open(&graph, "/", 1);
938 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 check_cancelled, NULL);
944 struct got_object_id *id;
945 err = got_commit_graph_iter_next(&id, graph, repo,
946 check_cancelled, NULL);
948 if (err->code == GOT_ERR_ITER_COMPLETED)
954 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 if (got_object_id_cmp(id, commit_id) == 0) {
964 got_commit_graph_close(graph);
965 free(head_commit_id);
966 if (!err && !is_same_branch)
967 err = got_error(GOT_ERR_ANCESTRY);
971 static const struct got_error *
972 cmd_clone(int argc, char *argv[])
974 char *uri, *branch_filter, *dirname;
977 while ((ch = getopt(argc, argv, "b:")) != -1) {
980 branch_filter = optarg;
996 return got_fetch(argv[0], branch_filter, dirname);
999 static const struct got_error *
1000 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1002 static char msg[512];
1003 const char *branch_name;
1005 if (got_ref_is_symbolic(ref))
1006 branch_name = got_ref_get_symref_target(ref);
1008 branch_name = got_ref_get_name(ref);
1010 if (strncmp("refs/heads/", branch_name, 11) == 0)
1013 snprintf(msg, sizeof(msg),
1014 "target commit is not contained in branch '%s'; "
1015 "the branch to use must be specified with -b; "
1016 "if necessary a new branch can be created for "
1017 "this commit with 'got branch -c %s BRANCH_NAME'",
1018 branch_name, commit_id_str);
1020 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1023 static const struct got_error *
1024 cmd_checkout(int argc, char *argv[])
1026 const struct got_error *error = NULL;
1027 struct got_repository *repo = NULL;
1028 struct got_reference *head_ref = NULL;
1029 struct got_worktree *worktree = NULL;
1030 char *repo_path = NULL;
1031 char *worktree_path = NULL;
1032 const char *path_prefix = "";
1033 const char *branch_name = GOT_REF_HEAD;
1034 char *commit_id_str = NULL;
1035 int ch, same_path_prefix, allow_nonempty = 0;
1036 struct got_pathlist_head paths;
1037 struct got_checkout_progress_arg cpa;
1041 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1044 branch_name = optarg;
1047 commit_id_str = strdup(optarg);
1048 if (commit_id_str == NULL)
1049 return got_error_from_errno("strdup");
1055 path_prefix = optarg;
1067 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1068 "unveil", NULL) == -1)
1072 char *cwd, *base, *dotgit;
1073 repo_path = realpath(argv[0], NULL);
1074 if (repo_path == NULL)
1075 return got_error_from_errno2("realpath", argv[0]);
1076 cwd = getcwd(NULL, 0);
1078 error = got_error_from_errno("getcwd");
1081 if (path_prefix[0]) {
1082 base = basename(path_prefix);
1084 error = got_error_from_errno2("basename",
1089 base = basename(repo_path);
1091 error = got_error_from_errno2("basename",
1096 dotgit = strstr(base, ".git");
1099 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1100 error = got_error_from_errno("asprintf");
1105 } else if (argc == 2) {
1106 repo_path = realpath(argv[0], NULL);
1107 if (repo_path == NULL) {
1108 error = got_error_from_errno2("realpath", argv[0]);
1111 worktree_path = realpath(argv[1], NULL);
1112 if (worktree_path == NULL) {
1113 if (errno != ENOENT) {
1114 error = got_error_from_errno2("realpath",
1118 worktree_path = strdup(argv[1]);
1119 if (worktree_path == NULL) {
1120 error = got_error_from_errno("strdup");
1127 got_path_strip_trailing_slashes(repo_path);
1128 got_path_strip_trailing_slashes(worktree_path);
1130 error = got_repo_open(&repo, repo_path, NULL);
1134 /* Pre-create work tree path for unveil(2) */
1135 error = got_path_mkdir(worktree_path);
1137 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1138 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1140 if (!allow_nonempty &&
1141 !got_path_dir_is_empty(worktree_path)) {
1142 error = got_error_path(worktree_path,
1143 GOT_ERR_DIR_NOT_EMPTY);
1148 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1152 error = got_ref_open(&head_ref, repo, branch_name, 0);
1156 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1157 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1160 error = got_worktree_open(&worktree, worktree_path);
1164 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1168 if (!same_path_prefix) {
1169 error = got_error(GOT_ERR_PATH_PREFIX);
1173 if (commit_id_str) {
1174 struct got_object_id *commit_id;
1175 error = got_repo_match_object_id(&commit_id, NULL,
1176 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1179 error = check_linear_ancestry(commit_id,
1180 got_worktree_get_base_commit_id(worktree), 0, repo);
1181 if (error != NULL) {
1183 if (error->code == GOT_ERR_ANCESTRY) {
1184 error = checkout_ancestry_error(
1185 head_ref, commit_id_str);
1189 error = check_same_branch(commit_id, head_ref, NULL, repo);
1191 if (error->code == GOT_ERR_ANCESTRY) {
1192 error = checkout_ancestry_error(
1193 head_ref, commit_id_str);
1197 error = got_worktree_set_base_commit_id(worktree, repo,
1204 error = got_pathlist_append(&paths, "", NULL);
1207 cpa.worktree_path = worktree_path;
1208 cpa.had_base_commit_ref_error = 0;
1209 error = got_worktree_checkout_files(worktree, &paths, repo,
1210 checkout_progress, &cpa, check_cancelled, NULL);
1214 printf("Now shut up and hack\n");
1215 if (cpa.had_base_commit_ref_error)
1216 show_worktree_base_ref_warning();
1218 got_pathlist_free(&paths);
1219 free(commit_id_str);
1221 free(worktree_path);
1228 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1233 static const struct got_error *
1234 update_progress(void *arg, unsigned char status, const char *path)
1236 int *did_something = arg;
1238 if (status == GOT_STATUS_EXISTS ||
1239 status == GOT_STATUS_BASE_REF_ERR)
1244 /* Base commit bump happens silently. */
1245 if (status == GOT_STATUS_BUMP_BASE)
1248 while (path[0] == '/')
1250 printf("%c %s\n", status, path);
1254 static const struct got_error *
1255 switch_head_ref(struct got_reference *head_ref,
1256 struct got_object_id *commit_id, struct got_worktree *worktree,
1257 struct got_repository *repo)
1259 const struct got_error *err = NULL;
1261 int ref_has_moved = 0;
1263 /* Trivial case: switching between two different references. */
1264 if (strcmp(got_ref_get_name(head_ref),
1265 got_worktree_get_head_ref_name(worktree)) != 0) {
1266 printf("Switching work tree from %s to %s\n",
1267 got_worktree_get_head_ref_name(worktree),
1268 got_ref_get_name(head_ref));
1269 return got_worktree_set_head_ref(worktree, head_ref);
1272 err = check_linear_ancestry(commit_id,
1273 got_worktree_get_base_commit_id(worktree), 0, repo);
1275 if (err->code != GOT_ERR_ANCESTRY)
1282 /* Switching to a rebased branch with the same reference name. */
1283 err = got_object_id_str(&base_id_str,
1284 got_worktree_get_base_commit_id(worktree));
1287 printf("Reference %s now points at a different branch\n",
1288 got_worktree_get_head_ref_name(worktree));
1289 printf("Switching work tree from %s to %s\n", base_id_str,
1290 got_worktree_get_head_ref_name(worktree));
1294 static const struct got_error *
1295 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1297 const struct got_error *err;
1300 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1304 return got_error(GOT_ERR_REBASING);
1306 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1310 return got_error(GOT_ERR_HISTEDIT_BUSY);
1315 static const struct got_error *
1316 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1317 char *argv[], struct got_worktree *worktree)
1319 const struct got_error *err = NULL;
1326 return got_error_from_errno("strdup");
1327 return got_pathlist_append(paths, path, NULL);
1330 for (i = 0; i < argc; i++) {
1331 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1334 err = got_pathlist_append(paths, path, NULL);
1344 static const struct got_error *
1345 cmd_update(int argc, char *argv[])
1347 const struct got_error *error = NULL;
1348 struct got_repository *repo = NULL;
1349 struct got_worktree *worktree = NULL;
1350 char *worktree_path = NULL;
1351 struct got_object_id *commit_id = NULL;
1352 char *commit_id_str = NULL;
1353 const char *branch_name = NULL;
1354 struct got_reference *head_ref = NULL;
1355 struct got_pathlist_head paths;
1356 struct got_pathlist_entry *pe;
1357 int ch, did_something = 0;
1361 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1364 branch_name = optarg;
1367 commit_id_str = strdup(optarg);
1368 if (commit_id_str == NULL)
1369 return got_error_from_errno("strdup");
1381 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1382 "unveil", NULL) == -1)
1385 worktree_path = getcwd(NULL, 0);
1386 if (worktree_path == NULL) {
1387 error = got_error_from_errno("getcwd");
1390 error = got_worktree_open(&worktree, worktree_path);
1394 error = check_rebase_or_histedit_in_progress(worktree);
1398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1403 error = apply_unveil(got_repo_get_path(repo), 0,
1404 got_worktree_get_root_path(worktree));
1408 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1412 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1413 got_worktree_get_head_ref_name(worktree), 0);
1416 if (commit_id_str == NULL) {
1417 error = got_ref_resolve(&commit_id, repo, head_ref);
1420 error = got_object_id_str(&commit_id_str, commit_id);
1424 error = got_repo_match_object_id(&commit_id, NULL,
1425 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1426 free(commit_id_str);
1427 commit_id_str = NULL;
1430 error = got_object_id_str(&commit_id_str, commit_id);
1436 struct got_object_id *head_commit_id;
1437 TAILQ_FOREACH(pe, &paths, entry) {
1438 if (pe->path_len == 0)
1440 error = got_error_msg(GOT_ERR_BAD_PATH,
1441 "switching between branches requires that "
1442 "the entire work tree gets updated");
1445 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1448 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1450 free(head_commit_id);
1453 error = check_same_branch(commit_id, head_ref, NULL, repo);
1456 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1460 error = check_linear_ancestry(commit_id,
1461 got_worktree_get_base_commit_id(worktree), 0, repo);
1462 if (error != NULL) {
1463 if (error->code == GOT_ERR_ANCESTRY)
1464 error = got_error(GOT_ERR_BRANCH_MOVED);
1467 error = check_same_branch(commit_id, head_ref, NULL, repo);
1472 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1474 error = got_worktree_set_base_commit_id(worktree, repo,
1480 error = got_worktree_checkout_files(worktree, &paths, repo,
1481 update_progress, &did_something, check_cancelled, NULL);
1486 printf("Updated to commit %s\n", commit_id_str);
1488 printf("Already up-to-date\n");
1490 free(worktree_path);
1491 TAILQ_FOREACH(pe, &paths, entry)
1492 free((char *)pe->path);
1493 got_pathlist_free(&paths);
1495 free(commit_id_str);
1499 static const struct got_error *
1500 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1501 const char *path, int diff_context, int ignore_whitespace,
1502 struct got_repository *repo)
1504 const struct got_error *err = NULL;
1505 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1508 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1513 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1517 while (path[0] == '/')
1519 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1520 ignore_whitespace, stdout);
1523 got_object_blob_close(blob1);
1524 got_object_blob_close(blob2);
1528 static const struct got_error *
1529 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1530 const char *path, int diff_context, int ignore_whitespace,
1531 struct got_repository *repo)
1533 const struct got_error *err = NULL;
1534 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1535 struct got_diff_blob_output_unidiff_arg arg;
1538 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1543 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1547 arg.diff_context = diff_context;
1548 arg.ignore_whitespace = ignore_whitespace;
1549 arg.outfile = stdout;
1550 while (path[0] == '/')
1552 err = got_diff_tree(tree1, tree2, path, path, repo,
1553 got_diff_blob_output_unidiff, &arg, 1);
1556 got_object_tree_close(tree1);
1558 got_object_tree_close(tree2);
1562 static const struct got_error *
1563 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1564 const char *path, int diff_context, struct got_repository *repo)
1566 const struct got_error *err = NULL;
1567 struct got_commit_object *pcommit = NULL;
1568 char *id_str1 = NULL, *id_str2 = NULL;
1569 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1570 struct got_object_qid *qid;
1572 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1574 err = got_object_open_as_commit(&pcommit, repo,
1580 if (path && path[0] != '\0') {
1582 err = got_object_id_by_path(&obj_id2, repo, id, path);
1585 err = got_object_id_str(&id_str2, obj_id2);
1591 err = got_object_id_by_path(&obj_id1, repo,
1597 err = got_object_id_str(&id_str1, obj_id1);
1603 err = got_object_get_type(&obj_type, repo, obj_id2);
1608 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1610 case GOT_OBJ_TYPE_BLOB:
1611 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1614 case GOT_OBJ_TYPE_TREE:
1615 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1619 err = got_error(GOT_ERR_OBJ_TYPE);
1625 obj_id2 = got_object_commit_get_tree_id(commit);
1626 err = got_object_id_str(&id_str2, obj_id2);
1629 obj_id1 = got_object_commit_get_tree_id(pcommit);
1630 err = got_object_id_str(&id_str1, obj_id1);
1633 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1634 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1640 got_object_commit_close(pcommit);
1645 get_datestr(time_t *time, char *datebuf)
1647 struct tm mytm, *tm;
1650 tm = gmtime_r(time, &mytm);
1653 s = asctime_r(tm, datebuf);
1656 p = strchr(s, '\n');
1662 static const struct got_error *
1663 match_logmsg(int *have_match, struct got_object_id *id,
1664 struct got_commit_object *commit, regex_t *regex)
1666 const struct got_error *err = NULL;
1667 regmatch_t regmatch;
1668 char *id_str = NULL, *logmsg = NULL;
1672 err = got_object_id_str(&id_str, id);
1676 err = got_object_commit_get_logmsg(&logmsg, commit);
1680 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1688 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1690 static const struct got_error *
1691 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1692 struct got_repository *repo, const char *path, int show_patch,
1693 int diff_context, struct got_reflist_head *refs)
1695 const struct got_error *err = NULL;
1696 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1698 time_t committer_time;
1699 const char *author, *committer;
1700 char *refs_str = NULL;
1701 struct got_reflist_entry *re;
1703 SIMPLEQ_FOREACH(re, refs, entry) {
1706 struct got_tag_object *tag = NULL;
1709 name = got_ref_get_name(re->ref);
1710 if (strcmp(name, GOT_REF_HEAD) == 0)
1712 if (strncmp(name, "refs/", 5) == 0)
1714 if (strncmp(name, "got/", 4) == 0)
1716 if (strncmp(name, "heads/", 6) == 0)
1718 if (strncmp(name, "remotes/", 8) == 0)
1720 if (strncmp(name, "tags/", 5) == 0) {
1721 err = got_object_open_as_tag(&tag, repo, re->id);
1723 if (err->code != GOT_ERR_OBJ_TYPE)
1725 /* Ref points at something other than a tag. */
1730 cmp = got_object_id_cmp(tag ?
1731 got_object_tag_get_object_id(tag) : re->id, id);
1733 got_object_tag_close(tag);
1737 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1739 err = got_error_from_errno("asprintf");
1745 err = got_object_id_str(&id_str, id);
1749 printf(GOT_COMMIT_SEP_STR);
1750 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1751 refs_str ? refs_str : "", refs_str ? ")" : "");
1756 printf("from: %s\n", got_object_commit_get_author(commit));
1757 committer_time = got_object_commit_get_committer_time(commit);
1758 datestr = get_datestr(&committer_time, datebuf);
1760 printf("date: %s UTC\n", datestr);
1761 author = got_object_commit_get_author(commit);
1762 committer = got_object_commit_get_committer(commit);
1763 if (strcmp(author, committer) != 0)
1764 printf("via: %s\n", committer);
1765 if (got_object_commit_get_nparents(commit) > 1) {
1766 const struct got_object_id_queue *parent_ids;
1767 struct got_object_qid *qid;
1769 parent_ids = got_object_commit_get_parent_ids(commit);
1770 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1771 err = got_object_id_str(&id_str, qid->id);
1774 printf("parent %d: %s\n", n++, id_str);
1779 err = got_object_commit_get_logmsg(&logmsg0, commit);
1785 line = strsep(&logmsg, "\n");
1787 printf(" %s\n", line);
1792 err = print_patch(commit, id, path, diff_context, repo);
1797 if (fflush(stdout) != 0 && err == NULL)
1798 err = got_error_from_errno("fflush");
1802 static const struct got_error *
1803 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1804 const char *path, int show_patch, const char *search_pattern,
1805 int diff_context, int limit, int log_branches,
1806 struct got_reflist_head *refs)
1808 const struct got_error *err;
1809 struct got_commit_graph *graph;
1813 if (search_pattern &&
1814 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1815 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1817 err = got_commit_graph_open(&graph, path, !log_branches);
1820 err = got_commit_graph_iter_start(graph, root_id, repo,
1821 check_cancelled, NULL);
1825 struct got_commit_object *commit;
1826 struct got_object_id *id;
1828 if (sigint_received || sigpipe_received)
1831 err = got_commit_graph_iter_next(&id, graph, repo,
1832 check_cancelled, NULL);
1834 if (err->code == GOT_ERR_ITER_COMPLETED)
1841 err = got_object_open_as_commit(&commit, repo, id);
1845 if (search_pattern) {
1846 err = match_logmsg(&have_match, id, commit, ®ex);
1848 got_object_commit_close(commit);
1851 if (have_match == 0) {
1852 got_object_commit_close(commit);
1857 err = print_commit(commit, id, repo, path, show_patch,
1858 diff_context, refs);
1859 got_object_commit_close(commit);
1860 if (err || (limit && --limit == 0))
1866 got_commit_graph_close(graph);
1873 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1874 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1879 get_default_log_limit(void)
1881 const char *got_default_log_limit;
1885 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1886 if (got_default_log_limit == NULL)
1888 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1894 static const struct got_error *
1895 cmd_log(int argc, char *argv[])
1897 const struct got_error *error;
1898 struct got_repository *repo = NULL;
1899 struct got_worktree *worktree = NULL;
1900 struct got_commit_object *commit = NULL;
1901 struct got_object_id *id = NULL;
1902 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1903 const char *start_commit = NULL, *search_pattern = NULL;
1904 int diff_context = -1, ch;
1905 int show_patch = 0, limit = 0, log_branches = 0;
1907 struct got_reflist_head refs;
1909 SIMPLEQ_INIT(&refs);
1912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1918 limit = get_default_log_limit();
1920 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1926 start_commit = optarg;
1929 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1932 err(1, "-C option %s", errstr);
1935 limit = strtonum(optarg, 0, INT_MAX, &errstr);
1937 err(1, "-l option %s", errstr);
1943 repo_path = realpath(optarg, NULL);
1944 if (repo_path == NULL)
1945 return got_error_from_errno2("realpath",
1947 got_path_strip_trailing_slashes(repo_path);
1950 search_pattern = optarg;
1961 if (diff_context == -1)
1963 else if (!show_patch)
1964 errx(1, "-C reguires -p");
1966 cwd = getcwd(NULL, 0);
1968 error = got_error_from_errno("getcwd");
1972 error = got_worktree_open(&worktree, cwd);
1973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1980 error = got_error_from_errno("strdup");
1983 } else if (argc == 1) {
1985 error = got_worktree_resolve_path(&path, worktree,
1990 path = strdup(argv[0]);
1992 error = got_error_from_errno("strdup");
1999 if (repo_path == NULL) {
2000 repo_path = worktree ?
2001 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2003 if (repo_path == NULL) {
2004 error = got_error_from_errno("strdup");
2008 error = got_repo_open(&repo, repo_path, NULL);
2012 error = apply_unveil(got_repo_get_path(repo), 1,
2013 worktree ? got_worktree_get_root_path(worktree) : NULL);
2017 if (start_commit == NULL) {
2018 struct got_reference *head_ref;
2019 error = got_ref_open(&head_ref, repo,
2020 worktree ? got_worktree_get_head_ref_name(worktree)
2024 error = got_ref_resolve(&id, repo, head_ref);
2025 got_ref_close(head_ref);
2028 error = got_object_open_as_commit(&commit, repo, id);
2030 struct got_reference *ref;
2031 error = got_ref_open(&ref, repo, start_commit, 0);
2032 if (error == NULL) {
2034 error = got_ref_resolve(&id, repo, ref);
2038 error = got_object_get_type(&obj_type, repo, id);
2041 if (obj_type == GOT_OBJ_TYPE_TAG) {
2042 struct got_tag_object *tag;
2043 error = got_object_open_as_tag(&tag, repo, id);
2046 if (got_object_tag_get_object_type(tag) !=
2047 GOT_OBJ_TYPE_COMMIT) {
2048 got_object_tag_close(tag);
2049 error = got_error(GOT_ERR_OBJ_TYPE);
2053 id = got_object_id_dup(
2054 got_object_tag_get_object_id(tag));
2056 error = got_error_from_errno(
2057 "got_object_id_dup");
2058 got_object_tag_close(tag);
2061 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2062 error = got_error(GOT_ERR_OBJ_TYPE);
2065 error = got_object_open_as_commit(&commit, repo, id);
2069 if (commit == NULL) {
2070 error = got_repo_match_object_id_prefix(&id,
2071 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2080 const char *prefix = got_worktree_get_path_prefix(worktree);
2082 if (asprintf(&p, "%s%s%s", prefix,
2083 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2084 error = got_error_from_errno("asprintf");
2087 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2090 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2095 path = in_repo_path;
2098 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2102 error = print_commits(id, repo, path, show_patch, search_pattern,
2103 diff_context, limit, log_branches, &refs);
2110 got_worktree_close(worktree);
2112 const struct got_error *repo_error;
2113 repo_error = got_repo_close(repo);
2117 got_ref_list_free(&refs);
2124 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2125 "[-w] [object1 object2 | path]\n", getprogname());
2129 struct print_diff_arg {
2130 struct got_repository *repo;
2131 struct got_worktree *worktree;
2136 int ignore_whitespace;
2139 static const struct got_error *
2140 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2141 const char *path, struct got_object_id *blob_id,
2142 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2143 int dirfd, const char *de_name)
2145 struct print_diff_arg *a = arg;
2146 const struct got_error *err = NULL;
2147 struct got_blob_object *blob1 = NULL;
2150 char *abspath = NULL, *label1 = NULL;
2153 if (a->diff_staged) {
2154 if (staged_status != GOT_STATUS_MODIFY &&
2155 staged_status != GOT_STATUS_ADD &&
2156 staged_status != GOT_STATUS_DELETE)
2159 if (staged_status == GOT_STATUS_DELETE)
2161 if (status == GOT_STATUS_NONEXISTENT)
2162 return got_error_set_errno(ENOENT, path);
2163 if (status != GOT_STATUS_MODIFY &&
2164 status != GOT_STATUS_ADD &&
2165 status != GOT_STATUS_DELETE &&
2166 status != GOT_STATUS_CONFLICT)
2170 if (!a->header_shown) {
2171 printf("diff %s %s%s\n", a->id_str,
2172 got_worktree_get_root_path(a->worktree),
2173 a->diff_staged ? " (staged changes)" : "");
2174 a->header_shown = 1;
2177 if (a->diff_staged) {
2178 const char *label1 = NULL, *label2 = NULL;
2179 switch (staged_status) {
2180 case GOT_STATUS_MODIFY:
2184 case GOT_STATUS_ADD:
2187 case GOT_STATUS_DELETE:
2191 return got_error(GOT_ERR_FILE_STATUS);
2193 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2194 label1, label2, a->diff_context, a->ignore_whitespace,
2198 if (staged_status == GOT_STATUS_ADD ||
2199 staged_status == GOT_STATUS_MODIFY) {
2201 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2205 err = got_object_id_str(&id_str, staged_blob_id);
2208 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2209 err = got_error_from_errno("asprintf");
2214 } else if (status != GOT_STATUS_ADD) {
2215 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2220 if (status != GOT_STATUS_DELETE) {
2221 if (asprintf(&abspath, "%s/%s",
2222 got_worktree_get_root_path(a->worktree), path) == -1) {
2223 err = got_error_from_errno("asprintf");
2228 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2230 err = got_error_from_errno2("openat", abspath);
2234 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2236 err = got_error_from_errno2("open", abspath);
2240 if (fstat(fd, &sb) == -1) {
2241 err = got_error_from_errno2("fstat", abspath);
2244 f2 = fdopen(fd, "r");
2246 err = got_error_from_errno2("fdopen", abspath);
2253 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2254 a->diff_context, a->ignore_whitespace, stdout);
2257 got_object_blob_close(blob1);
2258 if (f2 && fclose(f2) == EOF && err == NULL)
2259 err = got_error_from_errno("fclose");
2260 if (fd != -1 && close(fd) == -1 && err == NULL)
2261 err = got_error_from_errno("close");
2266 static const struct got_error *
2267 cmd_diff(int argc, char *argv[])
2269 const struct got_error *error;
2270 struct got_repository *repo = NULL;
2271 struct got_worktree *worktree = NULL;
2272 char *cwd = NULL, *repo_path = NULL;
2273 struct got_object_id *id1 = NULL, *id2 = NULL;
2274 const char *id_str1 = NULL, *id_str2 = NULL;
2275 char *label1 = NULL, *label2 = NULL;
2277 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2282 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2287 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2290 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2293 err(1, "-C option %s", errstr);
2296 repo_path = realpath(optarg, NULL);
2297 if (repo_path == NULL)
2298 return got_error_from_errno2("realpath",
2300 got_path_strip_trailing_slashes(repo_path);
2306 ignore_whitespace = 1;
2317 cwd = getcwd(NULL, 0);
2319 error = got_error_from_errno("getcwd");
2322 error = got_worktree_open(&worktree, cwd);
2323 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2326 if (worktree == NULL) {
2327 error = got_error(GOT_ERR_NOT_WORKTREE);
2332 "-r option can't be used when diffing a work tree");
2333 repo_path = strdup(got_worktree_get_repo_path(worktree));
2334 if (repo_path == NULL) {
2335 error = got_error_from_errno("strdup");
2339 error = got_worktree_resolve_path(&path, worktree,
2346 error = got_error_from_errno("strdup");
2350 } else if (argc == 2) {
2352 errx(1, "-s option can't be used when diffing "
2353 "objects in repository");
2356 if (worktree && repo_path == NULL) {
2358 strdup(got_worktree_get_repo_path(worktree));
2359 if (repo_path == NULL) {
2360 error = got_error_from_errno("strdup");
2367 if (repo_path == NULL) {
2368 repo_path = getcwd(NULL, 0);
2369 if (repo_path == NULL)
2370 return got_error_from_errno("getcwd");
2373 error = got_repo_open(&repo, repo_path, NULL);
2378 error = apply_unveil(got_repo_get_path(repo), 1,
2379 worktree ? got_worktree_get_root_path(worktree) : NULL);
2384 struct print_diff_arg arg;
2385 struct got_pathlist_head paths;
2390 error = got_object_id_str(&id_str,
2391 got_worktree_get_base_commit_id(worktree));
2395 arg.worktree = worktree;
2396 arg.diff_context = diff_context;
2397 arg.id_str = id_str;
2398 arg.header_shown = 0;
2399 arg.diff_staged = diff_staged;
2400 arg.ignore_whitespace = ignore_whitespace;
2402 error = got_pathlist_append(&paths, path, NULL);
2406 error = got_worktree_status(worktree, &paths, repo, print_diff,
2407 &arg, check_cancelled, NULL);
2409 got_pathlist_free(&paths);
2413 error = got_repo_match_object_id(&id1, &label1, id_str1,
2414 GOT_OBJ_TYPE_ANY, 1, repo);
2418 error = got_repo_match_object_id(&id2, &label2, id_str2,
2419 GOT_OBJ_TYPE_ANY, 1, repo);
2423 error = got_object_get_type(&type1, repo, id1);
2427 error = got_object_get_type(&type2, repo, id2);
2431 if (type1 != type2) {
2432 error = got_error(GOT_ERR_OBJ_TYPE);
2437 case GOT_OBJ_TYPE_BLOB:
2438 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2439 diff_context, ignore_whitespace, repo, stdout);
2441 case GOT_OBJ_TYPE_TREE:
2442 error = got_diff_objects_as_trees(id1, id2, "", "",
2443 diff_context, ignore_whitespace, repo, stdout);
2445 case GOT_OBJ_TYPE_COMMIT:
2446 printf("diff %s %s\n", label1, label2);
2447 error = got_diff_objects_as_commits(id1, id2, diff_context,
2448 ignore_whitespace, repo, stdout);
2451 error = got_error(GOT_ERR_OBJ_TYPE);
2460 got_worktree_close(worktree);
2462 const struct got_error *repo_error;
2463 repo_error = got_repo_close(repo);
2474 "usage: %s blame [-c commit] [-r repository-path] path\n",
2483 char datebuf[11]; /* YYYY-MM-DD + NUL */
2486 struct blame_cb_args {
2487 struct blame_line *lines;
2491 off_t *line_offsets;
2493 struct got_repository *repo;
2496 static const struct got_error *
2497 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2499 const struct got_error *err = NULL;
2500 struct blame_cb_args *a = arg;
2501 struct blame_line *bline;
2503 size_t linesize = 0;
2504 struct got_commit_object *commit = NULL;
2507 time_t committer_time;
2509 if (nlines != a->nlines ||
2510 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2511 return got_error(GOT_ERR_RANGE);
2513 if (sigint_received)
2514 return got_error(GOT_ERR_ITER_COMPLETED);
2517 return NULL; /* no change in this commit */
2519 /* Annotate this line. */
2520 bline = &a->lines[lineno - 1];
2521 if (bline->annotated)
2523 err = got_object_id_str(&bline->id_str, id);
2527 err = got_object_open_as_commit(&commit, a->repo, id);
2531 bline->committer = strdup(got_object_commit_get_committer(commit));
2532 if (bline->committer == NULL) {
2533 err = got_error_from_errno("strdup");
2537 committer_time = got_object_commit_get_committer_time(commit);
2538 if (localtime_r(&committer_time, &tm) == NULL)
2539 return got_error_from_errno("localtime_r");
2540 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2541 &tm) >= sizeof(bline->datebuf)) {
2542 err = got_error(GOT_ERR_NO_SPACE);
2545 bline->annotated = 1;
2547 /* Print lines annotated so far. */
2548 bline = &a->lines[a->lineno_cur - 1];
2549 if (!bline->annotated)
2552 offset = a->line_offsets[a->lineno_cur - 1];
2553 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2554 err = got_error_from_errno("fseeko");
2558 while (bline->annotated) {
2559 char *smallerthan, *at, *nl, *committer;
2562 if (getline(&line, &linesize, a->f) == -1) {
2564 err = got_error_from_errno("getline");
2568 committer = bline->committer;
2569 smallerthan = strchr(committer, '<');
2570 if (smallerthan && smallerthan[1] != '\0')
2571 committer = smallerthan + 1;
2572 at = strchr(committer, '@');
2575 len = strlen(committer);
2577 committer[8] = '\0';
2579 nl = strchr(line, '\n');
2582 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2583 bline->id_str, bline->datebuf, committer, line);
2586 bline = &a->lines[a->lineno_cur - 1];
2590 got_object_commit_close(commit);
2595 static const struct got_error *
2596 cmd_blame(int argc, char *argv[])
2598 const struct got_error *error;
2599 struct got_repository *repo = NULL;
2600 struct got_worktree *worktree = NULL;
2601 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2602 struct got_object_id *obj_id = NULL;
2603 struct got_object_id *commit_id = NULL;
2604 struct got_blob_object *blob = NULL;
2605 char *commit_id_str = NULL;
2606 struct blame_cb_args bca;
2607 int ch, obj_type, i;
2610 memset(&bca, 0, sizeof(bca));
2613 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2618 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2621 commit_id_str = optarg;
2624 repo_path = realpath(optarg, NULL);
2625 if (repo_path == NULL)
2626 return got_error_from_errno2("realpath",
2628 got_path_strip_trailing_slashes(repo_path);
2644 cwd = getcwd(NULL, 0);
2646 error = got_error_from_errno("getcwd");
2649 if (repo_path == NULL) {
2650 error = got_worktree_open(&worktree, cwd);
2651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2657 strdup(got_worktree_get_repo_path(worktree));
2658 if (repo_path == NULL)
2659 error = got_error_from_errno("strdup");
2663 repo_path = strdup(cwd);
2664 if (repo_path == NULL) {
2665 error = got_error_from_errno("strdup");
2671 error = got_repo_open(&repo, repo_path, NULL);
2675 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2680 const char *prefix = got_worktree_get_path_prefix(worktree);
2681 char *p, *worktree_subdir = cwd +
2682 strlen(got_worktree_get_root_path(worktree));
2683 if (asprintf(&p, "%s%s%s%s%s",
2684 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2685 worktree_subdir, worktree_subdir[0] ? "/" : "",
2687 error = got_error_from_errno("asprintf");
2690 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2693 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2698 if (commit_id_str == NULL) {
2699 struct got_reference *head_ref;
2700 error = got_ref_open(&head_ref, repo, worktree ?
2701 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2704 error = got_ref_resolve(&commit_id, repo, head_ref);
2705 got_ref_close(head_ref);
2709 error = got_repo_match_object_id(&commit_id, NULL,
2710 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2715 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2719 error = got_object_get_type(&obj_type, repo, obj_id);
2723 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2724 error = got_error(GOT_ERR_OBJ_TYPE);
2728 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2731 bca.f = got_opentemp();
2732 if (bca.f == NULL) {
2733 error = got_error_from_errno("got_opentemp");
2736 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2737 &bca.line_offsets, bca.f, blob);
2738 if (error || bca.nlines == 0)
2741 /* Don't include \n at EOF in the blame line count. */
2742 if (bca.line_offsets[bca.nlines - 1] == filesize)
2745 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2746 if (bca.lines == NULL) {
2747 error = got_error_from_errno("calloc");
2751 bca.nlines_prec = 0;
2759 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2760 check_cancelled, NULL);
2768 got_object_blob_close(blob);
2770 got_worktree_close(worktree);
2772 const struct got_error *repo_error;
2773 repo_error = got_repo_close(repo);
2778 for (i = 0; i < bca.nlines; i++) {
2779 struct blame_line *bline = &bca.lines[i];
2780 free(bline->id_str);
2781 free(bline->committer);
2785 free(bca.line_offsets);
2786 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2787 error = got_error_from_errno("fclose");
2795 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2801 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2802 const char *root_path)
2804 int is_root_path = (strcmp(path, root_path) == 0);
2805 const char *modestr = "";
2806 mode_t mode = got_tree_entry_get_mode(te);
2808 path += strlen(root_path);
2809 while (path[0] == '/')
2812 if (got_object_tree_entry_is_submodule(te))
2814 else if (S_ISLNK(mode))
2816 else if (S_ISDIR(mode))
2818 else if (mode & S_IXUSR)
2821 printf("%s%s%s%s%s\n", id ? id : "", path,
2822 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2825 static const struct got_error *
2826 print_tree(const char *path, struct got_object_id *commit_id,
2827 int show_ids, int recurse, const char *root_path,
2828 struct got_repository *repo)
2830 const struct got_error *err = NULL;
2831 struct got_object_id *tree_id = NULL;
2832 struct got_tree_object *tree = NULL;
2835 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2839 err = got_object_open_as_tree(&tree, repo, tree_id);
2842 nentries = got_object_tree_get_nentries(tree);
2843 for (i = 0; i < nentries; i++) {
2844 struct got_tree_entry *te;
2847 if (sigint_received || sigpipe_received)
2850 te = got_object_tree_get_entry(tree, i);
2853 err = got_object_id_str(&id_str,
2854 got_tree_entry_get_id(te));
2857 if (asprintf(&id, "%s ", id_str) == -1) {
2858 err = got_error_from_errno("asprintf");
2864 print_entry(te, id, path, root_path);
2867 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2869 if (asprintf(&child_path, "%s%s%s", path,
2870 path[0] == '/' && path[1] == '\0' ? "" : "/",
2871 got_tree_entry_get_name(te)) == -1) {
2872 err = got_error_from_errno("asprintf");
2875 err = print_tree(child_path, commit_id, show_ids, 1,
2884 got_object_tree_close(tree);
2889 static const struct got_error *
2890 cmd_tree(int argc, char *argv[])
2892 const struct got_error *error;
2893 struct got_repository *repo = NULL;
2894 struct got_worktree *worktree = NULL;
2896 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2897 struct got_object_id *commit_id = NULL;
2898 char *commit_id_str = NULL;
2899 int show_ids = 0, recurse = 0;
2903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2908 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2911 commit_id_str = optarg;
2914 repo_path = realpath(optarg, NULL);
2915 if (repo_path == NULL)
2916 return got_error_from_errno2("realpath",
2918 got_path_strip_trailing_slashes(repo_path);
2942 cwd = getcwd(NULL, 0);
2944 error = got_error_from_errno("getcwd");
2947 if (repo_path == NULL) {
2948 error = got_worktree_open(&worktree, cwd);
2949 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2955 strdup(got_worktree_get_repo_path(worktree));
2956 if (repo_path == NULL)
2957 error = got_error_from_errno("strdup");
2961 repo_path = strdup(cwd);
2962 if (repo_path == NULL) {
2963 error = got_error_from_errno("strdup");
2969 error = got_repo_open(&repo, repo_path, NULL);
2973 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2979 char *p, *worktree_subdir = cwd +
2980 strlen(got_worktree_get_root_path(worktree));
2981 if (asprintf(&p, "%s/%s",
2982 got_worktree_get_path_prefix(worktree),
2983 worktree_subdir) == -1) {
2984 error = got_error_from_errno("asprintf");
2987 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2994 if (in_repo_path == NULL) {
2995 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3000 if (commit_id_str == NULL) {
3001 struct got_reference *head_ref;
3002 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3005 error = got_ref_resolve(&commit_id, repo, head_ref);
3006 got_ref_close(head_ref);
3010 error = got_repo_match_object_id(&commit_id, NULL,
3011 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3016 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3017 in_repo_path, repo);
3024 got_worktree_close(worktree);
3026 const struct got_error *repo_error;
3027 repo_error = got_repo_close(repo);
3037 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3041 static const struct got_error *
3042 print_status(void *arg, unsigned char status, unsigned char staged_status,
3043 const char *path, struct got_object_id *blob_id,
3044 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3045 int dirfd, const char *de_name)
3047 if (status == staged_status && (status == GOT_STATUS_DELETE))
3048 status = GOT_STATUS_NO_CHANGE;
3049 printf("%c%c %s\n", status, staged_status, path);
3053 static const struct got_error *
3054 cmd_status(int argc, char *argv[])
3056 const struct got_error *error = NULL;
3057 struct got_repository *repo = NULL;
3058 struct got_worktree *worktree = NULL;
3060 struct got_pathlist_head paths;
3061 struct got_pathlist_entry *pe;
3066 while ((ch = getopt(argc, argv, "")) != -1) {
3078 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3082 cwd = getcwd(NULL, 0);
3084 error = got_error_from_errno("getcwd");
3088 error = got_worktree_open(&worktree, cwd);
3092 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3097 error = apply_unveil(got_repo_get_path(repo), 1,
3098 got_worktree_get_root_path(worktree));
3102 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3106 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3107 check_cancelled, NULL);
3109 TAILQ_FOREACH(pe, &paths, entry)
3110 free((char *)pe->path);
3111 got_pathlist_free(&paths);
3120 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3125 static const struct got_error *
3126 list_refs(struct got_repository *repo)
3128 static const struct got_error *err = NULL;
3129 struct got_reflist_head refs;
3130 struct got_reflist_entry *re;
3132 SIMPLEQ_INIT(&refs);
3133 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3137 SIMPLEQ_FOREACH(re, &refs, entry) {
3139 refstr = got_ref_to_str(re->ref);
3141 return got_error_from_errno("got_ref_to_str");
3142 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3146 got_ref_list_free(&refs);
3150 static const struct got_error *
3151 delete_ref(struct got_repository *repo, const char *refname)
3153 const struct got_error *err = NULL;
3154 struct got_reference *ref;
3156 err = got_ref_open(&ref, repo, refname, 0);
3160 err = got_ref_delete(ref, repo);
3165 static const struct got_error *
3166 add_ref(struct got_repository *repo, const char *refname, const char *target)
3168 const struct got_error *err = NULL;
3169 struct got_object_id *id;
3170 struct got_reference *ref = NULL;
3173 * Don't let the user create a reference name with a leading '-'.
3174 * While technically a valid reference name, this case is usually
3175 * an unintended typo.
3177 if (refname[0] == '-')
3178 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3180 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3183 struct got_reference *target_ref;
3185 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3187 err = got_ref_open(&target_ref, repo, target, 0);
3190 err = got_ref_resolve(&id, repo, target_ref);
3191 got_ref_close(target_ref);
3196 err = got_ref_alloc(&ref, refname, id);
3200 err = got_ref_write(ref, repo);
3208 static const struct got_error *
3209 add_symref(struct got_repository *repo, const char *refname, const char *target)
3211 const struct got_error *err = NULL;
3212 struct got_reference *ref = NULL;
3213 struct got_reference *target_ref = NULL;
3216 * Don't let the user create a reference name with a leading '-'.
3217 * While technically a valid reference name, this case is usually
3218 * an unintended typo.
3220 if (refname[0] == '-')
3221 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3223 err = got_ref_open(&target_ref, repo, target, 0);
3227 err = got_ref_alloc_symref(&ref, refname, target_ref);
3231 err = got_ref_write(ref, repo);
3234 got_ref_close(target_ref);
3240 static const struct got_error *
3241 cmd_ref(int argc, char *argv[])
3243 const struct got_error *error = NULL;
3244 struct got_repository *repo = NULL;
3245 struct got_worktree *worktree = NULL;
3246 char *cwd = NULL, *repo_path = NULL;
3247 int ch, do_list = 0, create_symref = 0;
3248 const char *delref = NULL;
3250 /* TODO: Add -s option for adding symbolic references. */
3251 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3257 repo_path = realpath(optarg, NULL);
3258 if (repo_path == NULL)
3259 return got_error_from_errno2("realpath",
3261 got_path_strip_trailing_slashes(repo_path);
3275 if (do_list && delref)
3276 errx(1, "-l and -d options are mutually exclusive\n");
3281 if (do_list || delref) {
3283 errx(1, "-s option cannot be used together with the "
3284 "-l or -d options");
3287 } else if (argc != 2)
3292 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3296 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3297 "sendfd unveil", NULL) == -1)
3301 cwd = getcwd(NULL, 0);
3303 error = got_error_from_errno("getcwd");
3307 if (repo_path == NULL) {
3308 error = got_worktree_open(&worktree, cwd);
3309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3315 strdup(got_worktree_get_repo_path(worktree));
3316 if (repo_path == NULL)
3317 error = got_error_from_errno("strdup");
3321 repo_path = strdup(cwd);
3322 if (repo_path == NULL) {
3323 error = got_error_from_errno("strdup");
3329 error = got_repo_open(&repo, repo_path, NULL);
3333 error = apply_unveil(got_repo_get_path(repo), do_list,
3334 worktree ? got_worktree_get_root_path(worktree) : NULL);
3339 error = list_refs(repo);
3341 error = delete_ref(repo, delref);
3342 else if (create_symref)
3343 error = add_symref(repo, argv[0], argv[1]);
3345 error = add_ref(repo, argv[0], argv[1]);
3348 got_repo_close(repo);
3350 got_worktree_close(worktree);
3360 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3361 "[name]\n", getprogname());
3365 static const struct got_error *
3366 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3367 struct got_reference *ref)
3369 const struct got_error *err = NULL;
3370 const char *refname, *marker = " ";
3373 refname = got_ref_get_name(ref);
3374 if (worktree && strcmp(refname,
3375 got_worktree_get_head_ref_name(worktree)) == 0) {
3376 struct got_object_id *id = NULL;
3378 err = got_ref_resolve(&id, repo, ref);
3381 if (got_object_id_cmp(id,
3382 got_worktree_get_base_commit_id(worktree)) == 0)
3389 if (strncmp(refname, "refs/heads/", 11) == 0)
3391 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3394 refstr = got_ref_to_str(ref);
3396 return got_error_from_errno("got_ref_to_str");
3398 printf("%s%s: %s\n", marker, refname, refstr);
3403 static const struct got_error *
3404 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3406 const char *refname;
3408 if (worktree == NULL)
3409 return got_error(GOT_ERR_NOT_WORKTREE);
3411 refname = got_worktree_get_head_ref_name(worktree);
3413 if (strncmp(refname, "refs/heads/", 11) == 0)
3415 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3418 printf("%s\n", refname);
3423 static const struct got_error *
3424 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3426 static const struct got_error *err = NULL;
3427 struct got_reflist_head refs;
3428 struct got_reflist_entry *re;
3429 struct got_reference *temp_ref = NULL;
3430 int rebase_in_progress, histedit_in_progress;
3432 SIMPLEQ_INIT(&refs);
3435 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3440 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3445 if (rebase_in_progress || histedit_in_progress) {
3446 err = got_ref_open(&temp_ref, repo,
3447 got_worktree_get_head_ref_name(worktree), 0);
3450 list_branch(repo, worktree, temp_ref);
3451 got_ref_close(temp_ref);
3455 err = got_ref_list(&refs, repo, "refs/heads",
3456 got_ref_cmp_by_name, NULL);
3460 SIMPLEQ_FOREACH(re, &refs, entry)
3461 list_branch(repo, worktree, re->ref);
3463 got_ref_list_free(&refs);
3467 static const struct got_error *
3468 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3469 const char *branch_name)
3471 const struct got_error *err = NULL;
3472 struct got_reference *ref = NULL;
3475 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3476 return got_error_from_errno("asprintf");
3478 err = got_ref_open(&ref, repo, refname, 0);
3483 strcmp(got_worktree_get_head_ref_name(worktree),
3484 got_ref_get_name(ref)) == 0) {
3485 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3486 "will not delete this work tree's current branch");
3490 err = got_ref_delete(ref, repo);
3498 static const struct got_error *
3499 add_branch(struct got_repository *repo, const char *branch_name,
3500 struct got_object_id *base_commit_id)
3502 const struct got_error *err = NULL;
3503 struct got_reference *ref = NULL;
3504 char *base_refname = NULL, *refname = NULL;
3507 * Don't let the user create a branch name with a leading '-'.
3508 * While technically a valid reference name, this case is usually
3509 * an unintended typo.
3511 if (branch_name[0] == '-')
3512 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3514 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3515 err = got_error_from_errno("asprintf");
3519 err = got_ref_open(&ref, repo, refname, 0);
3521 err = got_error(GOT_ERR_BRANCH_EXISTS);
3523 } else if (err->code != GOT_ERR_NOT_REF)
3526 err = got_ref_alloc(&ref, refname, base_commit_id);
3530 err = got_ref_write(ref, repo);
3539 static const struct got_error *
3540 cmd_branch(int argc, char *argv[])
3542 const struct got_error *error = NULL;
3543 struct got_repository *repo = NULL;
3544 struct got_worktree *worktree = NULL;
3545 char *cwd = NULL, *repo_path = NULL;
3546 int ch, do_list = 0, do_show = 0, do_update = 1;
3547 const char *delref = NULL, *commit_id_arg = NULL;
3548 struct got_reference *ref = NULL;
3549 struct got_pathlist_head paths;
3550 struct got_pathlist_entry *pe;
3551 struct got_object_id *commit_id = NULL;
3552 char *commit_id_str = NULL;
3556 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3559 commit_id_arg = optarg;
3565 repo_path = realpath(optarg, NULL);
3566 if (repo_path == NULL)
3567 return got_error_from_errno2("realpath",
3569 got_path_strip_trailing_slashes(repo_path);
3583 if (do_list && delref)
3584 errx(1, "-l and -d options are mutually exclusive\n");
3589 if (!do_list && !delref && argc == 0)
3592 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3593 errx(1, "-c option can only be used when creating a branch");
3595 if (do_list || delref) {
3598 } else if (!do_show && argc != 1)
3602 if (do_list || do_show) {
3603 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3607 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3608 "sendfd unveil", NULL) == -1)
3612 cwd = getcwd(NULL, 0);
3614 error = got_error_from_errno("getcwd");
3618 if (repo_path == NULL) {
3619 error = got_worktree_open(&worktree, cwd);
3620 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3626 strdup(got_worktree_get_repo_path(worktree));
3627 if (repo_path == NULL)
3628 error = got_error_from_errno("strdup");
3632 repo_path = strdup(cwd);
3633 if (repo_path == NULL) {
3634 error = got_error_from_errno("strdup");
3640 error = got_repo_open(&repo, repo_path, NULL);
3644 error = apply_unveil(got_repo_get_path(repo), do_list,
3645 worktree ? got_worktree_get_root_path(worktree) : NULL);
3650 error = show_current_branch(repo, worktree);
3652 error = list_branches(repo, worktree);
3654 error = delete_branch(repo, worktree, delref);
3656 if (commit_id_arg == NULL)
3657 commit_id_arg = worktree ?
3658 got_worktree_get_head_ref_name(worktree) :
3660 error = got_repo_match_object_id(&commit_id, NULL,
3661 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3664 error = add_branch(repo, argv[0], commit_id);
3667 if (worktree && do_update) {
3668 int did_something = 0;
3669 char *branch_refname = NULL;
3671 error = got_object_id_str(&commit_id_str, commit_id);
3674 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3678 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3680 error = got_error_from_errno("asprintf");
3683 error = got_ref_open(&ref, repo, branch_refname, 0);
3684 free(branch_refname);
3687 error = switch_head_ref(ref, commit_id, worktree,
3691 error = got_worktree_set_base_commit_id(worktree, repo,
3695 error = got_worktree_checkout_files(worktree, &paths,
3696 repo, update_progress, &did_something,
3697 check_cancelled, NULL);
3701 printf("Updated to commit %s\n", commit_id_str);
3708 got_repo_close(repo);
3710 got_worktree_close(worktree);
3714 free(commit_id_str);
3715 TAILQ_FOREACH(pe, &paths, entry)
3716 free((char *)pe->path);
3717 got_pathlist_free(&paths);
3726 "usage: %s tag [-c commit] [-r repository] [-l] "
3727 "[-m message] name\n", getprogname());
3732 static const struct got_error *
3733 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3735 const struct got_error *err = NULL;
3736 struct got_reflist_entry *re, *se, *new;
3737 struct got_object_id *re_id, *se_id;
3738 struct got_tag_object *re_tag, *se_tag;
3739 time_t re_time, se_time;
3741 SIMPLEQ_FOREACH(re, tags, entry) {
3742 se = SIMPLEQ_FIRST(sorted);
3744 err = got_reflist_entry_dup(&new, re);
3747 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3750 err = got_ref_resolve(&re_id, repo, re->ref);
3753 err = got_object_open_as_tag(&re_tag, repo, re_id);
3757 re_time = got_object_tag_get_tagger_time(re_tag);
3758 got_object_tag_close(re_tag);
3762 err = got_ref_resolve(&se_id, repo, re->ref);
3765 err = got_object_open_as_tag(&se_tag, repo, se_id);
3769 se_time = got_object_tag_get_tagger_time(se_tag);
3770 got_object_tag_close(se_tag);
3772 if (se_time > re_time) {
3773 err = got_reflist_entry_dup(&new, re);
3776 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3779 se = SIMPLEQ_NEXT(se, entry);
3788 static const struct got_error *
3789 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3791 static const struct got_error *err = NULL;
3792 struct got_reflist_head refs;
3793 struct got_reflist_entry *re;
3795 SIMPLEQ_INIT(&refs);
3797 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3801 SIMPLEQ_FOREACH(re, &refs, entry) {
3802 const char *refname;
3803 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3807 struct got_object_id *id;
3808 struct got_tag_object *tag;
3809 struct got_commit_object *commit = NULL;
3811 refname = got_ref_get_name(re->ref);
3812 if (strncmp(refname, "refs/tags/", 10) != 0)
3815 refstr = got_ref_to_str(re->ref);
3816 if (refstr == NULL) {
3817 err = got_error_from_errno("got_ref_to_str");
3820 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3823 err = got_ref_resolve(&id, repo, re->ref);
3826 err = got_object_open_as_tag(&tag, repo, id);
3828 if (err->code != GOT_ERR_OBJ_TYPE) {
3832 /* "lightweight" tag */
3833 err = got_object_open_as_commit(&commit, repo, id);
3838 tagger = got_object_commit_get_committer(commit);
3840 got_object_commit_get_committer_time(commit);
3841 err = got_object_id_str(&id_str, id);
3847 tagger = got_object_tag_get_tagger(tag);
3848 tagger_time = got_object_tag_get_tagger_time(tag);
3849 err = got_object_id_str(&id_str,
3850 got_object_tag_get_object_id(tag));
3854 printf("from: %s\n", tagger);
3855 datestr = get_datestr(&tagger_time, datebuf);
3857 printf("date: %s UTC\n", datestr);
3859 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3861 switch (got_object_tag_get_object_type(tag)) {
3862 case GOT_OBJ_TYPE_BLOB:
3863 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3866 case GOT_OBJ_TYPE_TREE:
3867 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3870 case GOT_OBJ_TYPE_COMMIT:
3871 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3874 case GOT_OBJ_TYPE_TAG:
3875 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3884 err = got_object_commit_get_logmsg(&tagmsg0, commit);
3887 got_object_commit_close(commit);
3889 tagmsg0 = strdup(got_object_tag_get_message(tag));
3890 got_object_tag_close(tag);
3891 if (tagmsg0 == NULL) {
3892 err = got_error_from_errno("strdup");
3899 line = strsep(&tagmsg, "\n");
3901 printf(" %s\n", line);
3906 got_ref_list_free(&refs);
3910 static const struct got_error *
3911 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3912 const char *tag_name, const char *repo_path)
3914 const struct got_error *err = NULL;
3915 char *template = NULL, *initial_content = NULL;
3916 char *editor = NULL;
3919 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3920 err = got_error_from_errno("asprintf");
3924 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3925 commit_id_str, tag_name) == -1) {
3926 err = got_error_from_errno("asprintf");
3930 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3934 dprintf(fd, initial_content);
3937 err = get_editor(&editor);
3940 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3942 free(initial_content);
3946 /* Editor is done; we can now apply unveil(2) */
3948 err = apply_unveil(repo_path, 0, NULL);
3957 static const struct got_error *
3958 add_tag(struct got_repository *repo, const char *tag_name,
3959 const char *commit_arg, const char *tagmsg_arg)
3961 const struct got_error *err = NULL;
3962 struct got_object_id *commit_id = NULL, *tag_id = NULL;
3963 char *label = NULL, *commit_id_str = NULL;
3964 struct got_reference *ref = NULL;
3965 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3966 char *tagmsg_path = NULL, *tag_id_str = NULL;
3967 int preserve_tagmsg = 0;
3970 * Don't let the user create a tag name with a leading '-'.
3971 * While technically a valid reference name, this case is usually
3972 * an unintended typo.
3974 if (tag_name[0] == '-')
3975 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3977 err = get_author(&tagger, repo);
3981 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3982 GOT_OBJ_TYPE_COMMIT, 1, repo);
3986 err = got_object_id_str(&commit_id_str, commit_id);
3990 if (strncmp("refs/tags/", tag_name, 10) == 0) {
3991 refname = strdup(tag_name);
3992 if (refname == NULL) {
3993 err = got_error_from_errno("strdup");
3997 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3998 err = got_error_from_errno("asprintf");
4002 err = got_ref_open(&ref, repo, refname, 0);
4004 err = got_error(GOT_ERR_TAG_EXISTS);
4006 } else if (err->code != GOT_ERR_NOT_REF)
4009 if (tagmsg_arg == NULL) {
4010 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4011 tag_name, got_repo_get_path(repo));
4013 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4014 tagmsg_path != NULL)
4015 preserve_tagmsg = 1;
4020 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4021 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4024 preserve_tagmsg = 1;
4028 err = got_ref_alloc(&ref, refname, tag_id);
4031 preserve_tagmsg = 1;
4035 err = got_ref_write(ref, repo);
4038 preserve_tagmsg = 1;
4042 err = got_object_id_str(&tag_id_str, tag_id);
4045 preserve_tagmsg = 1;
4048 printf("Created tag %s\n", tag_id_str);
4050 if (preserve_tagmsg) {
4051 fprintf(stderr, "%s: tag message preserved in %s\n",
4052 getprogname(), tagmsg_path);
4053 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4054 err = got_error_from_errno2("unlink", tagmsg_path);
4059 free(commit_id_str);
4067 static const struct got_error *
4068 cmd_tag(int argc, char *argv[])
4070 const struct got_error *error = NULL;
4071 struct got_repository *repo = NULL;
4072 struct got_worktree *worktree = NULL;
4073 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4074 char *gitconfig_path = NULL;
4075 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4076 int ch, do_list = 0;
4078 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4081 commit_id_arg = optarg;
4087 repo_path = realpath(optarg, NULL);
4088 if (repo_path == NULL)
4089 return got_error_from_errno2("realpath",
4091 got_path_strip_trailing_slashes(repo_path);
4106 if (commit_id_arg != NULL)
4107 errx(1, "-c option can only be used when creating a tag");
4109 errx(1, "-l and -m options are mutually exclusive");
4112 } else if (argc != 1)
4119 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4123 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4124 "sendfd unveil", NULL) == -1)
4128 cwd = getcwd(NULL, 0);
4130 error = got_error_from_errno("getcwd");
4134 if (repo_path == NULL) {
4135 error = got_worktree_open(&worktree, cwd);
4136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4142 strdup(got_worktree_get_repo_path(worktree));
4143 if (repo_path == NULL)
4144 error = got_error_from_errno("strdup");
4148 repo_path = strdup(cwd);
4149 if (repo_path == NULL) {
4150 error = got_error_from_errno("strdup");
4157 error = got_repo_open(&repo, repo_path, NULL);
4160 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4163 error = list_tags(repo, worktree);
4165 error = get_gitconfig_path(&gitconfig_path);
4168 error = got_repo_open(&repo, repo_path, gitconfig_path);
4173 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4178 if (commit_id_arg == NULL) {
4179 struct got_reference *head_ref;
4180 struct got_object_id *commit_id;
4181 error = got_ref_open(&head_ref, repo,
4182 worktree ? got_worktree_get_head_ref_name(worktree)
4186 error = got_ref_resolve(&commit_id, repo, head_ref);
4187 got_ref_close(head_ref);
4190 error = got_object_id_str(&commit_id_str, commit_id);
4196 error = add_tag(repo, tag_name,
4197 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4201 got_repo_close(repo);
4203 got_worktree_close(worktree);
4206 free(gitconfig_path);
4207 free(commit_id_str);
4214 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4219 static const struct got_error *
4220 add_progress(void *arg, unsigned char status, const char *path)
4222 while (path[0] == '/')
4224 printf("%c %s\n", status, path);
4228 static const struct got_error *
4229 cmd_add(int argc, char *argv[])
4231 const struct got_error *error = NULL;
4232 struct got_repository *repo = NULL;
4233 struct got_worktree *worktree = NULL;
4235 struct got_pathlist_head paths;
4236 struct got_pathlist_entry *pe;
4237 int ch, can_recurse = 0, no_ignores = 0;
4241 while ((ch = getopt(argc, argv, "IR")) != -1) {
4259 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4266 cwd = getcwd(NULL, 0);
4268 error = got_error_from_errno("getcwd");
4272 error = got_worktree_open(&worktree, cwd);
4276 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4281 error = apply_unveil(got_repo_get_path(repo), 1,
4282 got_worktree_get_root_path(worktree));
4286 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4290 if (!can_recurse && no_ignores) {
4291 error = got_error_msg(GOT_ERR_BAD_PATH,
4292 "disregarding ignores requires -R option");
4300 TAILQ_FOREACH(pe, &paths, entry) {
4301 if (asprintf(&ondisk_path, "%s/%s",
4302 got_worktree_get_root_path(worktree),
4304 error = got_error_from_errno("asprintf");
4307 if (lstat(ondisk_path, &sb) == -1) {
4308 if (errno == ENOENT) {
4312 error = got_error_from_errno2("lstat",
4318 if (S_ISDIR(sb.st_mode)) {
4319 error = got_error_msg(GOT_ERR_BAD_PATH,
4320 "adding directories requires -R option");
4326 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4327 NULL, repo, no_ignores);
4330 got_repo_close(repo);
4332 got_worktree_close(worktree);
4333 TAILQ_FOREACH(pe, &paths, entry)
4334 free((char *)pe->path);
4335 got_pathlist_free(&paths);
4343 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4348 static const struct got_error *
4349 print_remove_status(void *arg, unsigned char status,
4350 unsigned char staged_status, const char *path)
4352 while (path[0] == '/')
4354 if (status == GOT_STATUS_NONEXISTENT)
4356 if (status == staged_status && (status == GOT_STATUS_DELETE))
4357 status = GOT_STATUS_NO_CHANGE;
4358 printf("%c%c %s\n", status, staged_status, path);
4362 static const struct got_error *
4363 cmd_remove(int argc, char *argv[])
4365 const struct got_error *error = NULL;
4366 struct got_worktree *worktree = NULL;
4367 struct got_repository *repo = NULL;
4369 struct got_pathlist_head paths;
4370 struct got_pathlist_entry *pe;
4371 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4375 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4378 delete_local_mods = 1;
4396 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4403 cwd = getcwd(NULL, 0);
4405 error = got_error_from_errno("getcwd");
4408 error = got_worktree_open(&worktree, cwd);
4412 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4417 error = apply_unveil(got_repo_get_path(repo), 1,
4418 got_worktree_get_root_path(worktree));
4422 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4429 TAILQ_FOREACH(pe, &paths, entry) {
4430 if (asprintf(&ondisk_path, "%s/%s",
4431 got_worktree_get_root_path(worktree),
4433 error = got_error_from_errno("asprintf");
4436 if (lstat(ondisk_path, &sb) == -1) {
4437 if (errno == ENOENT) {
4441 error = got_error_from_errno2("lstat",
4447 if (S_ISDIR(sb.st_mode)) {
4448 error = got_error_msg(GOT_ERR_BAD_PATH,
4449 "removing directories requires -R option");
4455 error = got_worktree_schedule_delete(worktree, &paths,
4456 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4459 got_repo_close(repo);
4461 got_worktree_close(worktree);
4462 TAILQ_FOREACH(pe, &paths, entry)
4463 free((char *)pe->path);
4464 got_pathlist_free(&paths);
4472 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4473 "path ...\n", getprogname());
4477 static const struct got_error *
4478 revert_progress(void *arg, unsigned char status, const char *path)
4480 if (status == GOT_STATUS_UNVERSIONED)
4483 while (path[0] == '/')
4485 printf("%c %s\n", status, path);
4489 struct choose_patch_arg {
4490 FILE *patch_script_file;
4494 static const struct got_error *
4495 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4496 int nchanges, const char *action)
4499 size_t linesize = 0;
4503 case GOT_STATUS_ADD:
4504 printf("A %s\n%s this addition? [y/n] ", path, action);
4506 case GOT_STATUS_DELETE:
4507 printf("D %s\n%s this deletion? [y/n] ", path, action);
4509 case GOT_STATUS_MODIFY:
4510 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4511 return got_error_from_errno("fseek");
4512 printf(GOT_COMMIT_SEP_STR);
4513 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4515 if (ferror(patch_file))
4516 return got_error_from_errno("getline");
4517 printf(GOT_COMMIT_SEP_STR);
4518 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4519 path, n, nchanges, action);
4522 return got_error_path(path, GOT_ERR_FILE_STATUS);
4528 static const struct got_error *
4529 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4530 FILE *patch_file, int n, int nchanges)
4532 const struct got_error *err = NULL;
4534 size_t linesize = 0;
4537 struct choose_patch_arg *a = arg;
4539 *choice = GOT_PATCH_CHOICE_NONE;
4541 if (a->patch_script_file) {
4543 err = show_change(status, path, patch_file, n, nchanges,
4547 linelen = getline(&line, &linesize, a->patch_script_file);
4548 if (linelen == -1) {
4549 if (ferror(a->patch_script_file))
4550 return got_error_from_errno("getline");
4553 nl = strchr(line, '\n');
4556 if (strcmp(line, "y") == 0) {
4557 *choice = GOT_PATCH_CHOICE_YES;
4559 } else if (strcmp(line, "n") == 0) {
4560 *choice = GOT_PATCH_CHOICE_NO;
4562 } else if (strcmp(line, "q") == 0 &&
4563 status == GOT_STATUS_MODIFY) {
4564 *choice = GOT_PATCH_CHOICE_QUIT;
4567 printf("invalid response '%s'\n", line);
4572 while (resp != 'y' && resp != 'n' && resp != 'q') {
4573 err = show_change(status, path, patch_file, n, nchanges,
4580 if (status == GOT_STATUS_MODIFY) {
4581 if (resp != 'y' && resp != 'n' && resp != 'q') {
4582 printf("invalid response '%c'\n", resp);
4585 } else if (resp != 'y' && resp != 'n') {
4586 printf("invalid response '%c'\n", resp);
4592 *choice = GOT_PATCH_CHOICE_YES;
4593 else if (resp == 'n')
4594 *choice = GOT_PATCH_CHOICE_NO;
4595 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4596 *choice = GOT_PATCH_CHOICE_QUIT;
4602 static const struct got_error *
4603 cmd_revert(int argc, char *argv[])
4605 const struct got_error *error = NULL;
4606 struct got_worktree *worktree = NULL;
4607 struct got_repository *repo = NULL;
4608 char *cwd = NULL, *path = NULL;
4609 struct got_pathlist_head paths;
4610 struct got_pathlist_entry *pe;
4611 int ch, can_recurse = 0, pflag = 0;
4612 FILE *patch_script_file = NULL;
4613 const char *patch_script_path = NULL;
4614 struct choose_patch_arg cpa;
4618 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4624 patch_script_path = optarg;
4639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4640 "unveil", NULL) == -1)
4645 if (patch_script_path && !pflag)
4646 errx(1, "-F option can only be used together with -p option");
4648 cwd = getcwd(NULL, 0);
4650 error = got_error_from_errno("getcwd");
4653 error = got_worktree_open(&worktree, cwd);
4657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4662 if (patch_script_path) {
4663 patch_script_file = fopen(patch_script_path, "r");
4664 if (patch_script_file == NULL) {
4665 error = got_error_from_errno2("fopen",
4670 error = apply_unveil(got_repo_get_path(repo), 1,
4671 got_worktree_get_root_path(worktree));
4675 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4682 TAILQ_FOREACH(pe, &paths, entry) {
4683 if (asprintf(&ondisk_path, "%s/%s",
4684 got_worktree_get_root_path(worktree),
4686 error = got_error_from_errno("asprintf");
4689 if (lstat(ondisk_path, &sb) == -1) {
4690 if (errno == ENOENT) {
4694 error = got_error_from_errno2("lstat",
4700 if (S_ISDIR(sb.st_mode)) {
4701 error = got_error_msg(GOT_ERR_BAD_PATH,
4702 "reverting directories requires -R option");
4708 cpa.patch_script_file = patch_script_file;
4709 cpa.action = "revert";
4710 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4711 pflag ? choose_patch : NULL, &cpa, repo);
4713 if (patch_script_file && fclose(patch_script_file) == EOF &&
4715 error = got_error_from_errno2("fclose", patch_script_path);
4717 got_repo_close(repo);
4719 got_worktree_close(worktree);
4728 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4733 struct collect_commit_logmsg_arg {
4734 const char *cmdline_log;
4736 const char *worktree_path;
4737 const char *branch_name;
4738 const char *repo_path;
4743 static const struct got_error *
4744 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4747 char *initial_content = NULL;
4748 struct got_pathlist_entry *pe;
4749 const struct got_error *err = NULL;
4750 char *template = NULL;
4751 struct collect_commit_logmsg_arg *a = arg;
4755 /* if a message was specified on the command line, just use it */
4756 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4757 len = strlen(a->cmdline_log) + 1;
4758 *logmsg = malloc(len + 1);
4759 if (*logmsg == NULL)
4760 return got_error_from_errno("malloc");
4761 strlcpy(*logmsg, a->cmdline_log, len);
4765 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4766 return got_error_from_errno("asprintf");
4768 if (asprintf(&initial_content,
4769 "\n# changes to be committed on branch %s:\n",
4770 a->branch_name) == -1)
4771 return got_error_from_errno("asprintf");
4773 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4777 dprintf(fd, initial_content);
4779 TAILQ_FOREACH(pe, commitable_paths, entry) {
4780 struct got_commitable *ct = pe->data;
4781 dprintf(fd, "# %c %s\n",
4782 got_commitable_get_status(ct),
4783 got_commitable_get_path(ct));
4787 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4789 free(initial_content);
4792 /* Editor is done; we can now apply unveil(2) */
4794 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4803 static const struct got_error *
4804 cmd_commit(int argc, char *argv[])
4806 const struct got_error *error = NULL;
4807 struct got_worktree *worktree = NULL;
4808 struct got_repository *repo = NULL;
4809 char *cwd = NULL, *id_str = NULL;
4810 struct got_object_id *id = NULL;
4811 const char *logmsg = NULL;
4812 struct collect_commit_logmsg_arg cl_arg;
4813 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4814 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4815 struct got_pathlist_head paths;
4818 cl_arg.logmsg_path = NULL;
4820 while ((ch = getopt(argc, argv, "m:")) != -1) {
4835 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4836 "unveil", NULL) == -1)
4839 cwd = getcwd(NULL, 0);
4841 error = got_error_from_errno("getcwd");
4844 error = got_worktree_open(&worktree, cwd);
4848 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4851 if (rebase_in_progress) {
4852 error = got_error(GOT_ERR_REBASING);
4856 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4861 error = get_gitconfig_path(&gitconfig_path);
4864 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4869 error = get_author(&author, repo);
4874 * unveil(2) traverses exec(2); if an editor is used we have
4875 * to apply unveil after the log message has been written.
4877 if (logmsg == NULL || strlen(logmsg) == 0)
4878 error = get_editor(&editor);
4880 error = apply_unveil(got_repo_get_path(repo), 0,
4881 got_worktree_get_root_path(worktree));
4885 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4889 cl_arg.editor = editor;
4890 cl_arg.cmdline_log = logmsg;
4891 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4892 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4893 if (!histedit_in_progress) {
4894 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4895 error = got_error(GOT_ERR_COMMIT_BRANCH);
4898 cl_arg.branch_name += 11;
4900 cl_arg.repo_path = got_repo_get_path(repo);
4901 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4902 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4904 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4905 cl_arg.logmsg_path != NULL)
4906 preserve_logmsg = 1;
4910 error = got_object_id_str(&id_str, id);
4913 printf("Created commit %s\n", id_str);
4915 if (preserve_logmsg) {
4916 fprintf(stderr, "%s: log message preserved in %s\n",
4917 getprogname(), cl_arg.logmsg_path);
4918 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4920 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4921 free(cl_arg.logmsg_path);
4923 got_repo_close(repo);
4925 got_worktree_close(worktree);
4928 free(gitconfig_path);
4935 usage_cherrypick(void)
4937 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4941 static const struct got_error *
4942 cmd_cherrypick(int argc, char *argv[])
4944 const struct got_error *error = NULL;
4945 struct got_worktree *worktree = NULL;
4946 struct got_repository *repo = NULL;
4947 char *cwd = NULL, *commit_id_str = NULL;
4948 struct got_object_id *commit_id = NULL;
4949 struct got_commit_object *commit = NULL;
4950 struct got_object_qid *pid;
4951 struct got_reference *head_ref = NULL;
4952 int ch, did_something = 0;
4954 while ((ch = getopt(argc, argv, "")) != -1) {
4966 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4967 "unveil", NULL) == -1)
4973 cwd = getcwd(NULL, 0);
4975 error = got_error_from_errno("getcwd");
4978 error = got_worktree_open(&worktree, cwd);
4982 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4987 error = apply_unveil(got_repo_get_path(repo), 0,
4988 got_worktree_get_root_path(worktree));
4992 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4993 GOT_OBJ_TYPE_COMMIT, repo);
4994 if (error != NULL) {
4995 struct got_reference *ref;
4996 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4998 error = got_ref_open(&ref, repo, argv[0], 0);
5001 error = got_ref_resolve(&commit_id, repo, ref);
5006 error = got_object_id_str(&commit_id_str, commit_id);
5010 error = got_ref_open(&head_ref, repo,
5011 got_worktree_get_head_ref_name(worktree), 0);
5015 error = check_same_branch(commit_id, head_ref, NULL, repo);
5017 if (error->code != GOT_ERR_ANCESTRY)
5021 error = got_error(GOT_ERR_SAME_BRANCH);
5025 error = got_object_open_as_commit(&commit, repo, commit_id);
5028 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5029 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5030 commit_id, repo, update_progress, &did_something, check_cancelled,
5036 printf("Merged commit %s\n", commit_id_str);
5039 got_object_commit_close(commit);
5040 free(commit_id_str);
5042 got_ref_close(head_ref);
5044 got_worktree_close(worktree);
5046 got_repo_close(repo);
5053 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5057 static const struct got_error *
5058 cmd_backout(int argc, char *argv[])
5060 const struct got_error *error = NULL;
5061 struct got_worktree *worktree = NULL;
5062 struct got_repository *repo = NULL;
5063 char *cwd = NULL, *commit_id_str = NULL;
5064 struct got_object_id *commit_id = NULL;
5065 struct got_commit_object *commit = NULL;
5066 struct got_object_qid *pid;
5067 struct got_reference *head_ref = NULL;
5068 int ch, did_something = 0;
5070 while ((ch = getopt(argc, argv, "")) != -1) {
5082 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5083 "unveil", NULL) == -1)
5089 cwd = getcwd(NULL, 0);
5091 error = got_error_from_errno("getcwd");
5094 error = got_worktree_open(&worktree, cwd);
5098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5103 error = apply_unveil(got_repo_get_path(repo), 0,
5104 got_worktree_get_root_path(worktree));
5108 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5109 GOT_OBJ_TYPE_COMMIT, repo);
5110 if (error != NULL) {
5111 struct got_reference *ref;
5112 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5114 error = got_ref_open(&ref, repo, argv[0], 0);
5117 error = got_ref_resolve(&commit_id, repo, ref);
5122 error = got_object_id_str(&commit_id_str, commit_id);
5126 error = got_ref_open(&head_ref, repo,
5127 got_worktree_get_head_ref_name(worktree), 0);
5131 error = check_same_branch(commit_id, head_ref, NULL, repo);
5135 error = got_object_open_as_commit(&commit, repo, commit_id);
5138 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5140 error = got_error(GOT_ERR_ROOT_COMMIT);
5144 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5145 update_progress, &did_something, check_cancelled, NULL);
5150 printf("Backed out commit %s\n", commit_id_str);
5153 got_object_commit_close(commit);
5154 free(commit_id_str);
5156 got_ref_close(head_ref);
5158 got_worktree_close(worktree);
5160 got_repo_close(repo);
5167 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5173 trim_logmsg(char *logmsg, int limit)
5178 len = strlen(logmsg);
5182 nl = strchr(logmsg, '\n');
5187 static const struct got_error *
5188 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5190 const struct got_error *err;
5191 char *logmsg0 = NULL;
5194 err = got_object_commit_get_logmsg(&logmsg0, commit);
5199 while (isspace((unsigned char)s[0]))
5202 *logmsg = strdup(s);
5203 if (*logmsg == NULL) {
5204 err = got_error_from_errno("strdup");
5208 trim_logmsg(*logmsg, limit);
5214 static const struct got_error *
5215 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5217 const struct got_error *err;
5218 struct got_commit_object *commit = NULL;
5219 char *id_str = NULL, *logmsg = NULL;
5221 err = got_object_open_as_commit(&commit, repo, id);
5225 err = got_object_id_str(&id_str, id);
5231 err = get_short_logmsg(&logmsg, 42, commit);
5235 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5238 got_object_commit_close(commit);
5243 static const struct got_error *
5244 show_rebase_progress(struct got_commit_object *commit,
5245 struct got_object_id *old_id, struct got_object_id *new_id)
5247 const struct got_error *err;
5248 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5250 err = got_object_id_str(&old_id_str, old_id);
5255 err = got_object_id_str(&new_id_str, new_id);
5260 old_id_str[12] = '\0';
5262 new_id_str[12] = '\0';
5264 err = get_short_logmsg(&logmsg, 42, commit);
5268 printf("%s -> %s: %s\n", old_id_str,
5269 new_id_str ? new_id_str : "no-op change", logmsg);
5277 static const struct got_error *
5278 rebase_progress(void *arg, unsigned char status, const char *path)
5280 unsigned char *rebase_status = arg;
5282 while (path[0] == '/')
5284 printf("%c %s\n", status, path);
5286 if (*rebase_status == GOT_STATUS_CONFLICT)
5288 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5289 *rebase_status = status;
5293 static const struct got_error *
5294 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5295 struct got_reference *branch, struct got_reference *new_base_branch,
5296 struct got_reference *tmp_branch, struct got_repository *repo)
5298 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5299 return got_worktree_rebase_complete(worktree, fileindex,
5300 new_base_branch, tmp_branch, branch, repo);
5303 static const struct got_error *
5304 rebase_commit(struct got_pathlist_head *merged_paths,
5305 struct got_worktree *worktree, struct got_fileindex *fileindex,
5306 struct got_reference *tmp_branch,
5307 struct got_object_id *commit_id, struct got_repository *repo)
5309 const struct got_error *error;
5310 struct got_commit_object *commit;
5311 struct got_object_id *new_commit_id;
5313 error = got_object_open_as_commit(&commit, repo, commit_id);
5317 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5318 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5320 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5322 error = show_rebase_progress(commit, commit_id, NULL);
5324 error = show_rebase_progress(commit, commit_id, new_commit_id);
5325 free(new_commit_id);
5328 got_object_commit_close(commit);
5332 struct check_path_prefix_arg {
5333 const char *path_prefix;
5338 static const struct got_error *
5339 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5340 struct got_blob_object *blob2, struct got_object_id *id1,
5341 struct got_object_id *id2, const char *path1, const char *path2,
5342 mode_t mode1, mode_t mode2, struct got_repository *repo)
5344 struct check_path_prefix_arg *a = arg;
5346 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5347 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5348 return got_error(a->errcode);
5353 static const struct got_error *
5354 check_path_prefix(struct got_object_id *parent_id,
5355 struct got_object_id *commit_id, const char *path_prefix,
5356 int errcode, struct got_repository *repo)
5358 const struct got_error *err;
5359 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5360 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5361 struct check_path_prefix_arg cpp_arg;
5363 if (got_path_is_root_dir(path_prefix))
5366 err = got_object_open_as_commit(&commit, repo, commit_id);
5370 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5374 err = got_object_open_as_tree(&tree1, repo,
5375 got_object_commit_get_tree_id(parent_commit));
5379 err = got_object_open_as_tree(&tree2, repo,
5380 got_object_commit_get_tree_id(commit));
5384 cpp_arg.path_prefix = path_prefix;
5385 while (cpp_arg.path_prefix[0] == '/')
5386 cpp_arg.path_prefix++;
5387 cpp_arg.len = strlen(cpp_arg.path_prefix);
5388 cpp_arg.errcode = errcode;
5389 err = got_diff_tree(tree1, tree2, "", "", repo,
5390 check_path_prefix_in_diff, &cpp_arg, 0);
5393 got_object_tree_close(tree1);
5395 got_object_tree_close(tree2);
5397 got_object_commit_close(commit);
5399 got_object_commit_close(parent_commit);
5403 static const struct got_error *
5404 collect_commits(struct got_object_id_queue *commits,
5405 struct got_object_id *initial_commit_id,
5406 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5407 const char *path_prefix, int path_prefix_errcode,
5408 struct got_repository *repo)
5410 const struct got_error *err = NULL;
5411 struct got_commit_graph *graph = NULL;
5412 struct got_object_id *parent_id = NULL;
5413 struct got_object_qid *qid;
5414 struct got_object_id *commit_id = initial_commit_id;
5416 err = got_commit_graph_open(&graph, "/", 1);
5420 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5421 check_cancelled, NULL);
5424 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5425 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5426 check_cancelled, NULL);
5428 if (err->code == GOT_ERR_ITER_COMPLETED) {
5429 err = got_error_msg(GOT_ERR_ANCESTRY,
5430 "ran out of commits to rebase before "
5431 "youngest common ancestor commit has "
5436 err = check_path_prefix(parent_id, commit_id,
5437 path_prefix, path_prefix_errcode, repo);
5441 err = got_object_qid_alloc(&qid, commit_id);
5444 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5445 commit_id = parent_id;
5449 got_commit_graph_close(graph);
5453 static const struct got_error *
5454 cmd_rebase(int argc, char *argv[])
5456 const struct got_error *error = NULL;
5457 struct got_worktree *worktree = NULL;
5458 struct got_repository *repo = NULL;
5459 struct got_fileindex *fileindex = NULL;
5461 struct got_reference *branch = NULL;
5462 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5463 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5464 struct got_object_id *resume_commit_id = NULL;
5465 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5466 struct got_commit_object *commit = NULL;
5467 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5468 int histedit_in_progress = 0;
5469 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5470 struct got_object_id_queue commits;
5471 struct got_pathlist_head merged_paths;
5472 const struct got_object_id_queue *parent_ids;
5473 struct got_object_qid *qid, *pid;
5475 SIMPLEQ_INIT(&commits);
5476 TAILQ_INIT(&merged_paths);
5478 while ((ch = getopt(argc, argv, "ac")) != -1) {
5484 continue_rebase = 1;
5496 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5497 "unveil", NULL) == -1)
5500 if (abort_rebase && continue_rebase)
5502 else if (abort_rebase || continue_rebase) {
5505 } else if (argc != 1)
5508 cwd = getcwd(NULL, 0);
5510 error = got_error_from_errno("getcwd");
5513 error = got_worktree_open(&worktree, cwd);
5517 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5522 error = apply_unveil(got_repo_get_path(repo), 0,
5523 got_worktree_get_root_path(worktree));
5527 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5531 if (histedit_in_progress) {
5532 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5536 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5542 if (!rebase_in_progress) {
5543 error = got_error(GOT_ERR_NOT_REBASING);
5546 error = got_worktree_rebase_continue(&resume_commit_id,
5547 &new_base_branch, &tmp_branch, &branch, &fileindex,
5551 printf("Switching work tree to %s\n",
5552 got_ref_get_symref_target(new_base_branch));
5553 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5554 new_base_branch, update_progress, &did_something);
5557 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5558 goto done; /* nothing else to do */
5561 if (continue_rebase) {
5562 if (!rebase_in_progress) {
5563 error = got_error(GOT_ERR_NOT_REBASING);
5566 error = got_worktree_rebase_continue(&resume_commit_id,
5567 &new_base_branch, &tmp_branch, &branch, &fileindex,
5572 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5573 resume_commit_id, repo);
5577 yca_id = got_object_id_dup(resume_commit_id);
5578 if (yca_id == NULL) {
5579 error = got_error_from_errno("got_object_id_dup");
5583 error = got_ref_open(&branch, repo, argv[0], 0);
5588 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5592 if (!continue_rebase) {
5593 struct got_object_id *base_commit_id;
5595 base_commit_id = got_worktree_get_base_commit_id(worktree);
5596 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5597 base_commit_id, branch_head_commit_id, repo,
5598 check_cancelled, NULL);
5601 if (yca_id == NULL) {
5602 error = got_error_msg(GOT_ERR_ANCESTRY,
5603 "specified branch shares no common ancestry "
5604 "with work tree's branch");
5608 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5610 if (error->code != GOT_ERR_ANCESTRY)
5614 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5615 "specified branch resolves to a commit which "
5616 "is already contained in work tree's branch");
5619 error = got_worktree_rebase_prepare(&new_base_branch,
5620 &tmp_branch, &fileindex, worktree, branch, repo);
5625 commit_id = branch_head_commit_id;
5626 error = got_object_open_as_commit(&commit, repo, commit_id);
5630 parent_ids = got_object_commit_get_parent_ids(commit);
5631 pid = SIMPLEQ_FIRST(parent_ids);
5633 if (!continue_rebase) {
5635 error = got_worktree_rebase_abort(worktree, fileindex,
5636 repo, new_base_branch, update_progress,
5640 printf("Rebase of %s aborted\n",
5641 got_ref_get_name(branch));
5643 error = got_error(GOT_ERR_EMPTY_REBASE);
5646 error = collect_commits(&commits, commit_id, pid->id,
5647 yca_id, got_worktree_get_path_prefix(worktree),
5648 GOT_ERR_REBASE_PATH, repo);
5649 got_object_commit_close(commit);
5654 if (SIMPLEQ_EMPTY(&commits)) {
5655 if (continue_rebase) {
5656 error = rebase_complete(worktree, fileindex,
5657 branch, new_base_branch, tmp_branch, repo);
5660 /* Fast-forward the reference of the branch. */
5661 struct got_object_id *new_head_commit_id;
5663 error = got_ref_resolve(&new_head_commit_id, repo,
5667 error = got_object_id_str(&id_str, new_head_commit_id);
5668 printf("Forwarding %s to commit %s\n",
5669 got_ref_get_name(branch), id_str);
5671 error = got_ref_change_ref(branch,
5672 new_head_commit_id);
5679 SIMPLEQ_FOREACH(qid, &commits, entry) {
5680 commit_id = qid->id;
5681 parent_id = pid ? pid->id : yca_id;
5684 error = got_worktree_rebase_merge_files(&merged_paths,
5685 worktree, fileindex, parent_id, commit_id, repo,
5686 rebase_progress, &rebase_status, check_cancelled, NULL);
5690 if (rebase_status == GOT_STATUS_CONFLICT) {
5691 error = show_rebase_merge_conflict(qid->id, repo);
5694 got_worktree_rebase_pathlist_free(&merged_paths);
5698 error = rebase_commit(&merged_paths, worktree, fileindex,
5699 tmp_branch, commit_id, repo);
5700 got_worktree_rebase_pathlist_free(&merged_paths);
5705 if (rebase_status == GOT_STATUS_CONFLICT) {
5706 error = got_worktree_rebase_postpone(worktree, fileindex);
5709 error = got_error_msg(GOT_ERR_CONFLICTS,
5710 "conflicts must be resolved before rebasing can continue");
5712 error = rebase_complete(worktree, fileindex, branch,
5713 new_base_branch, tmp_branch, repo);
5715 got_object_id_queue_free(&commits);
5716 free(branch_head_commit_id);
5717 free(resume_commit_id);
5720 got_object_commit_close(commit);
5722 got_ref_close(branch);
5723 if (new_base_branch)
5724 got_ref_close(new_base_branch);
5726 got_ref_close(tmp_branch);
5728 got_worktree_close(worktree);
5730 got_repo_close(repo);
5735 usage_histedit(void)
5737 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5742 #define GOT_HISTEDIT_PICK 'p'
5743 #define GOT_HISTEDIT_EDIT 'e'
5744 #define GOT_HISTEDIT_FOLD 'f'
5745 #define GOT_HISTEDIT_DROP 'd'
5746 #define GOT_HISTEDIT_MESG 'm'
5748 static struct got_histedit_cmd {
5752 } got_histedit_cmds[] = {
5753 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5754 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5755 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5757 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5758 { GOT_HISTEDIT_MESG, "mesg",
5759 "single-line log message for commit above (open editor if empty)" },
5762 struct got_histedit_list_entry {
5763 TAILQ_ENTRY(got_histedit_list_entry) entry;
5764 struct got_object_id *commit_id;
5765 const struct got_histedit_cmd *cmd;
5768 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5770 static const struct got_error *
5771 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5772 FILE *f, struct got_repository *repo)
5774 const struct got_error *err = NULL;
5775 char *logmsg = NULL, *id_str = NULL;
5776 struct got_commit_object *commit = NULL;
5779 err = got_object_open_as_commit(&commit, repo, commit_id);
5783 err = get_short_logmsg(&logmsg, 34, commit);
5787 err = got_object_id_str(&id_str, commit_id);
5791 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5793 err = got_ferror(f, GOT_ERR_IO);
5796 got_object_commit_close(commit);
5802 static const struct got_error *
5803 histedit_write_commit_list(struct got_object_id_queue *commits,
5804 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5806 const struct got_error *err = NULL;
5807 struct got_object_qid *qid;
5809 if (SIMPLEQ_EMPTY(commits))
5810 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5812 SIMPLEQ_FOREACH(qid, commits, entry) {
5813 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5817 if (edit_logmsg_only) {
5818 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5820 err = got_ferror(f, GOT_ERR_IO);
5829 static const struct got_error *
5830 write_cmd_list(FILE *f, const char *branch_name,
5831 struct got_object_id_queue *commits)
5833 const struct got_error *err = NULL;
5836 struct got_object_qid *qid;
5838 qid = SIMPLEQ_FIRST(commits);
5839 err = got_object_id_str(&id_str, qid->id);
5844 "# Editing the history of branch '%s' starting at\n"
5846 "# Commits will be processed in order from top to "
5847 "bottom of this file.\n", branch_name, id_str);
5849 err = got_ferror(f, GOT_ERR_IO);
5853 n = fprintf(f, "# Available histedit commands:\n");
5855 err = got_ferror(f, GOT_ERR_IO);
5859 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5860 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5861 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5864 err = got_ferror(f, GOT_ERR_IO);
5873 static const struct got_error *
5874 histedit_syntax_error(int lineno)
5876 static char msg[42];
5879 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5881 if (ret == -1 || ret >= sizeof(msg))
5882 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5884 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5887 static const struct got_error *
5888 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5889 char *logmsg, struct got_repository *repo)
5891 const struct got_error *err;
5892 struct got_commit_object *folded_commit = NULL;
5893 char *id_str, *folded_logmsg = NULL;
5895 err = got_object_id_str(&id_str, hle->commit_id);
5899 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5903 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5906 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5907 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5908 folded_logmsg) == -1) {
5909 err = got_error_from_errno("asprintf");
5913 got_object_commit_close(folded_commit);
5915 free(folded_logmsg);
5919 static struct got_histedit_list_entry *
5920 get_folded_commits(struct got_histedit_list_entry *hle)
5922 struct got_histedit_list_entry *prev, *folded = NULL;
5924 prev = TAILQ_PREV(hle, got_histedit_list, entry);
5925 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5926 prev->cmd->code == GOT_HISTEDIT_DROP)) {
5927 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5929 prev = TAILQ_PREV(prev, got_histedit_list, entry);
5935 static const struct got_error *
5936 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5937 struct got_repository *repo)
5939 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5940 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5941 const struct got_error *err = NULL;
5942 struct got_commit_object *commit = NULL;
5944 struct got_histedit_list_entry *folded = NULL;
5946 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5950 folded = get_folded_commits(hle);
5952 while (folded != hle) {
5953 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5954 folded = TAILQ_NEXT(folded, entry);
5957 err = append_folded_commit_msg(&new_msg, folded,
5963 folded = TAILQ_NEXT(folded, entry);
5967 err = got_object_id_str(&id_str, hle->commit_id);
5970 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5973 if (asprintf(&new_msg,
5974 "%s\n# original log message of commit %s: %s",
5975 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5976 err = got_error_from_errno("asprintf");
5982 err = got_object_id_str(&id_str, hle->commit_id);
5986 err = got_opentemp_named_fd(&logmsg_path, &fd,
5987 GOT_TMPDIR_STR "/got-logmsg");
5991 dprintf(fd, logmsg);
5994 err = get_editor(&editor);
5998 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6000 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6002 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6005 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6006 err = got_error_from_errno2("unlink", logmsg_path);
6012 got_object_commit_close(commit);
6016 static const struct got_error *
6017 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6018 FILE *f, struct got_repository *repo)
6020 const struct got_error *err = NULL;
6021 char *line = NULL, *p, *end;
6025 const struct got_histedit_cmd *cmd;
6026 struct got_object_id *commit_id = NULL;
6027 struct got_histedit_list_entry *hle = NULL;
6030 len = getline(&line, &size, f);
6032 const struct got_error *getline_err;
6035 getline_err = got_error_from_errno("getline");
6036 err = got_ferror(f, getline_err->code);
6041 while (isspace((unsigned char)p[0]))
6043 if (p[0] == '#' || p[0] == '\0') {
6049 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6050 cmd = &got_histedit_cmds[i];
6051 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6052 isspace((unsigned char)p[strlen(cmd->name)])) {
6053 p += strlen(cmd->name);
6056 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6061 if (i == nitems(got_histedit_cmds)) {
6062 err = histedit_syntax_error(lineno);
6065 while (isspace((unsigned char)p[0]))
6067 if (cmd->code == GOT_HISTEDIT_MESG) {
6068 if (hle == NULL || hle->logmsg != NULL) {
6069 err = got_error(GOT_ERR_HISTEDIT_CMD);
6073 err = histedit_edit_logmsg(hle, repo);
6077 hle->logmsg = strdup(p);
6078 if (hle->logmsg == NULL) {
6079 err = got_error_from_errno("strdup");
6088 while (end[0] && !isspace((unsigned char)end[0]))
6092 err = got_object_resolve_id_str(&commit_id, repo, p);
6094 /* override error code */
6095 err = histedit_syntax_error(lineno);
6099 hle = malloc(sizeof(*hle));
6101 err = got_error_from_errno("malloc");
6105 hle->commit_id = commit_id;
6110 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6118 static const struct got_error *
6119 histedit_check_script(struct got_histedit_list *histedit_cmds,
6120 struct got_object_id_queue *commits, struct got_repository *repo)
6122 const struct got_error *err = NULL;
6123 struct got_object_qid *qid;
6124 struct got_histedit_list_entry *hle;
6125 static char msg[92];
6128 if (TAILQ_EMPTY(histedit_cmds))
6129 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6130 "histedit script contains no commands");
6131 if (SIMPLEQ_EMPTY(commits))
6132 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6134 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6135 struct got_histedit_list_entry *hle2;
6136 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6139 if (got_object_id_cmp(hle->commit_id,
6140 hle2->commit_id) != 0)
6142 err = got_object_id_str(&id_str, hle->commit_id);
6145 snprintf(msg, sizeof(msg), "commit %s is listed "
6146 "more than once in histedit script", id_str);
6148 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6152 SIMPLEQ_FOREACH(qid, commits, entry) {
6153 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6154 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6158 err = got_object_id_str(&id_str, qid->id);
6161 snprintf(msg, sizeof(msg),
6162 "commit %s missing from histedit script", id_str);
6164 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6168 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6169 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6170 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6171 "last commit in histedit script cannot be folded");
6176 static const struct got_error *
6177 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6178 const char *path, struct got_object_id_queue *commits,
6179 struct got_repository *repo)
6181 const struct got_error *err = NULL;
6185 err = get_editor(&editor);
6189 if (spawn_editor(editor, path) == -1) {
6190 err = got_error_from_errno("failed spawning editor");
6194 f = fopen(path, "r");
6196 err = got_error_from_errno("fopen");
6199 err = histedit_parse_list(histedit_cmds, f, repo);
6203 err = histedit_check_script(histedit_cmds, commits, repo);
6205 if (f && fclose(f) != 0 && err == NULL)
6206 err = got_error_from_errno("fclose");
6211 static const struct got_error *
6212 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6213 struct got_object_id_queue *, const char *, const char *,
6214 struct got_repository *);
6216 static const struct got_error *
6217 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6218 struct got_object_id_queue *commits, const char *branch_name,
6219 int edit_logmsg_only, struct got_repository *repo)
6221 const struct got_error *err;
6225 err = got_opentemp_named(&path, &f, "got-histedit");
6229 err = write_cmd_list(f, branch_name, commits);
6233 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6237 if (edit_logmsg_only) {
6239 err = histedit_parse_list(histedit_cmds, f, repo);
6241 if (fclose(f) != 0) {
6242 err = got_error_from_errno("fclose");
6246 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6248 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6249 err->code != GOT_ERR_HISTEDIT_CMD)
6251 err = histedit_edit_list_retry(histedit_cmds, err,
6252 commits, path, branch_name, repo);
6256 if (f && fclose(f) != 0 && err == NULL)
6257 err = got_error_from_errno("fclose");
6258 if (path && unlink(path) != 0 && err == NULL)
6259 err = got_error_from_errno2("unlink", path);
6264 static const struct got_error *
6265 histedit_save_list(struct got_histedit_list *histedit_cmds,
6266 struct got_worktree *worktree, struct got_repository *repo)
6268 const struct got_error *err = NULL;
6271 struct got_histedit_list_entry *hle;
6272 struct got_commit_object *commit = NULL;
6274 err = got_worktree_get_histedit_script_path(&path, worktree);
6278 f = fopen(path, "w");
6280 err = got_error_from_errno2("fopen", path);
6283 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6284 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6290 int n = fprintf(f, "%c %s\n",
6291 GOT_HISTEDIT_MESG, hle->logmsg);
6293 err = got_ferror(f, GOT_ERR_IO);
6299 if (f && fclose(f) != 0 && err == NULL)
6300 err = got_error_from_errno("fclose");
6303 got_object_commit_close(commit);
6308 histedit_free_list(struct got_histedit_list *histedit_cmds)
6310 struct got_histedit_list_entry *hle;
6312 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6313 TAILQ_REMOVE(histedit_cmds, hle, entry);
6318 static const struct got_error *
6319 histedit_load_list(struct got_histedit_list *histedit_cmds,
6320 const char *path, struct got_repository *repo)
6322 const struct got_error *err = NULL;
6325 f = fopen(path, "r");
6327 err = got_error_from_errno2("fopen", path);
6331 err = histedit_parse_list(histedit_cmds, f, repo);
6333 if (f && fclose(f) != 0 && err == NULL)
6334 err = got_error_from_errno("fclose");
6338 static const struct got_error *
6339 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6340 const struct got_error *edit_err, struct got_object_id_queue *commits,
6341 const char *path, const char *branch_name, struct got_repository *repo)
6343 const struct got_error *err = NULL, *prev_err = edit_err;
6346 while (resp != 'c' && resp != 'r' && resp != 'a') {
6347 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6348 "or (a)bort: ", getprogname(), prev_err->msg);
6353 histedit_free_list(histedit_cmds);
6354 err = histedit_run_editor(histedit_cmds, path, commits,
6357 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6358 err->code != GOT_ERR_HISTEDIT_CMD)
6365 } else if (resp == 'r') {
6366 histedit_free_list(histedit_cmds);
6367 err = histedit_edit_script(histedit_cmds,
6368 commits, branch_name, 0, repo);
6370 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6371 err->code != GOT_ERR_HISTEDIT_CMD)
6378 } else if (resp == 'a') {
6379 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6382 printf("invalid response '%c'\n", resp);
6388 static const struct got_error *
6389 histedit_complete(struct got_worktree *worktree,
6390 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6391 struct got_reference *branch, struct got_repository *repo)
6393 printf("Switching work tree to %s\n",
6394 got_ref_get_symref_target(branch));
6395 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6399 static const struct got_error *
6400 show_histedit_progress(struct got_commit_object *commit,
6401 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6403 const struct got_error *err;
6404 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6406 err = got_object_id_str(&old_id_str, hle->commit_id);
6411 err = got_object_id_str(&new_id_str, new_id);
6416 old_id_str[12] = '\0';
6418 new_id_str[12] = '\0';
6421 logmsg = strdup(hle->logmsg);
6422 if (logmsg == NULL) {
6423 err = got_error_from_errno("strdup");
6426 trim_logmsg(logmsg, 42);
6428 err = get_short_logmsg(&logmsg, 42, commit);
6433 switch (hle->cmd->code) {
6434 case GOT_HISTEDIT_PICK:
6435 case GOT_HISTEDIT_EDIT:
6436 printf("%s -> %s: %s\n", old_id_str,
6437 new_id_str ? new_id_str : "no-op change", logmsg);
6439 case GOT_HISTEDIT_DROP:
6440 case GOT_HISTEDIT_FOLD:
6441 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6453 static const struct got_error *
6454 histedit_commit(struct got_pathlist_head *merged_paths,
6455 struct got_worktree *worktree, struct got_fileindex *fileindex,
6456 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6457 struct got_repository *repo)
6459 const struct got_error *err;
6460 struct got_commit_object *commit;
6461 struct got_object_id *new_commit_id;
6463 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6464 && hle->logmsg == NULL) {
6465 err = histedit_edit_logmsg(hle, repo);
6470 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6474 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6475 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6478 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6480 err = show_histedit_progress(commit, hle, NULL);
6482 err = show_histedit_progress(commit, hle, new_commit_id);
6483 free(new_commit_id);
6486 got_object_commit_close(commit);
6490 static const struct got_error *
6491 histedit_skip_commit(struct got_histedit_list_entry *hle,
6492 struct got_worktree *worktree, struct got_repository *repo)
6494 const struct got_error *error;
6495 struct got_commit_object *commit;
6497 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6502 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6506 error = show_histedit_progress(commit, hle, NULL);
6507 got_object_commit_close(commit);
6511 static const struct got_error *
6512 check_local_changes(void *arg, unsigned char status,
6513 unsigned char staged_status, const char *path,
6514 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6515 struct got_object_id *commit_id, int dirfd, const char *de_name)
6517 int *have_local_changes = arg;
6520 case GOT_STATUS_ADD:
6521 case GOT_STATUS_DELETE:
6522 case GOT_STATUS_MODIFY:
6523 case GOT_STATUS_CONFLICT:
6524 *have_local_changes = 1;
6525 return got_error(GOT_ERR_CANCELLED);
6530 switch (staged_status) {
6531 case GOT_STATUS_ADD:
6532 case GOT_STATUS_DELETE:
6533 case GOT_STATUS_MODIFY:
6534 *have_local_changes = 1;
6535 return got_error(GOT_ERR_CANCELLED);
6543 static const struct got_error *
6544 cmd_histedit(int argc, char *argv[])
6546 const struct got_error *error = NULL;
6547 struct got_worktree *worktree = NULL;
6548 struct got_fileindex *fileindex = NULL;
6549 struct got_repository *repo = NULL;
6551 struct got_reference *branch = NULL;
6552 struct got_reference *tmp_branch = NULL;
6553 struct got_object_id *resume_commit_id = NULL;
6554 struct got_object_id *base_commit_id = NULL;
6555 struct got_object_id *head_commit_id = NULL;
6556 struct got_commit_object *commit = NULL;
6557 int ch, rebase_in_progress = 0, did_something;
6558 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6559 int edit_logmsg_only = 0;
6560 const char *edit_script_path = NULL;
6561 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6562 struct got_object_id_queue commits;
6563 struct got_pathlist_head merged_paths;
6564 const struct got_object_id_queue *parent_ids;
6565 struct got_object_qid *pid;
6566 struct got_histedit_list histedit_cmds;
6567 struct got_histedit_list_entry *hle;
6569 SIMPLEQ_INIT(&commits);
6570 TAILQ_INIT(&histedit_cmds);
6571 TAILQ_INIT(&merged_paths);
6573 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6582 edit_script_path = optarg;
6585 edit_logmsg_only = 1;
6597 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6598 "unveil", NULL) == -1)
6601 if (abort_edit && continue_edit)
6602 errx(1, "histedit's -a and -c options are mutually exclusive");
6603 if (edit_script_path && edit_logmsg_only)
6604 errx(1, "histedit's -F and -m options are mutually exclusive");
6605 if (abort_edit && edit_logmsg_only)
6606 errx(1, "histedit's -a and -m options are mutually exclusive");
6607 if (continue_edit && edit_logmsg_only)
6608 errx(1, "histedit's -c and -m options are mutually exclusive");
6613 * This command cannot apply unveil(2) in all cases because the
6614 * user may choose to run an editor to edit the histedit script
6615 * and to edit individual commit log messages.
6616 * unveil(2) traverses exec(2); if an editor is used we have to
6617 * apply unveil after edit script and log messages have been written.
6618 * XXX TODO: Make use of unveil(2) where possible.
6621 cwd = getcwd(NULL, 0);
6623 error = got_error_from_errno("getcwd");
6626 error = got_worktree_open(&worktree, cwd);
6630 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6635 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6638 if (rebase_in_progress) {
6639 error = got_error(GOT_ERR_REBASING);
6643 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6647 if (edit_in_progress && edit_logmsg_only) {
6648 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6649 "histedit operation is in progress in this "
6650 "work tree and must be continued or aborted "
6651 "before the -m option can be used");
6655 if (edit_in_progress && abort_edit) {
6656 error = got_worktree_histedit_continue(&resume_commit_id,
6657 &tmp_branch, &branch, &base_commit_id, &fileindex,
6661 printf("Switching work tree to %s\n",
6662 got_ref_get_symref_target(branch));
6663 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6664 branch, base_commit_id, update_progress, &did_something);
6667 printf("Histedit of %s aborted\n",
6668 got_ref_get_symref_target(branch));
6669 goto done; /* nothing else to do */
6670 } else if (abort_edit) {
6671 error = got_error(GOT_ERR_NOT_HISTEDIT);
6675 if (continue_edit) {
6678 if (!edit_in_progress) {
6679 error = got_error(GOT_ERR_NOT_HISTEDIT);
6683 error = got_worktree_get_histedit_script_path(&path, worktree);
6687 error = histedit_load_list(&histedit_cmds, path, repo);
6692 error = got_worktree_histedit_continue(&resume_commit_id,
6693 &tmp_branch, &branch, &base_commit_id, &fileindex,
6698 error = got_ref_resolve(&head_commit_id, repo, branch);
6702 error = got_object_open_as_commit(&commit, repo,
6706 parent_ids = got_object_commit_get_parent_ids(commit);
6707 pid = SIMPLEQ_FIRST(parent_ids);
6709 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6712 error = collect_commits(&commits, head_commit_id, pid->id,
6713 base_commit_id, got_worktree_get_path_prefix(worktree),
6714 GOT_ERR_HISTEDIT_PATH, repo);
6715 got_object_commit_close(commit);
6720 if (edit_in_progress) {
6721 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6725 error = got_ref_open(&branch, repo,
6726 got_worktree_get_head_ref_name(worktree), 0);
6730 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6731 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6732 "will not edit commit history of a branch outside "
6733 "the \"refs/heads/\" reference namespace");
6737 error = got_ref_resolve(&head_commit_id, repo, branch);
6738 got_ref_close(branch);
6743 error = got_object_open_as_commit(&commit, repo,
6747 parent_ids = got_object_commit_get_parent_ids(commit);
6748 pid = SIMPLEQ_FIRST(parent_ids);
6750 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6753 error = collect_commits(&commits, head_commit_id, pid->id,
6754 got_worktree_get_base_commit_id(worktree),
6755 got_worktree_get_path_prefix(worktree),
6756 GOT_ERR_HISTEDIT_PATH, repo);
6757 got_object_commit_close(commit);
6762 if (SIMPLEQ_EMPTY(&commits)) {
6763 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6767 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6768 &base_commit_id, &fileindex, worktree, repo);
6772 if (edit_script_path) {
6773 error = histedit_load_list(&histedit_cmds,
6774 edit_script_path, repo);
6776 got_worktree_histedit_abort(worktree, fileindex,
6777 repo, branch, base_commit_id,
6778 update_progress, &did_something);
6782 const char *branch_name;
6783 branch_name = got_ref_get_symref_target(branch);
6784 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6786 error = histedit_edit_script(&histedit_cmds, &commits,
6787 branch_name, edit_logmsg_only, repo);
6789 got_worktree_histedit_abort(worktree, fileindex,
6790 repo, branch, base_commit_id,
6791 update_progress, &did_something);
6797 error = histedit_save_list(&histedit_cmds, worktree,
6800 got_worktree_histedit_abort(worktree, fileindex,
6801 repo, branch, base_commit_id,
6802 update_progress, &did_something);
6808 error = histedit_check_script(&histedit_cmds, &commits, repo);
6812 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6813 if (resume_commit_id) {
6814 if (got_object_id_cmp(hle->commit_id,
6815 resume_commit_id) != 0)
6818 resume_commit_id = NULL;
6819 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6820 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6821 error = histedit_skip_commit(hle, worktree,
6826 struct got_pathlist_head paths;
6827 int have_changes = 0;
6830 error = got_pathlist_append(&paths, "", NULL);
6833 error = got_worktree_status(worktree, &paths,
6834 repo, check_local_changes, &have_changes,
6835 check_cancelled, NULL);
6836 got_pathlist_free(&paths);
6838 if (error->code != GOT_ERR_CANCELLED)
6840 if (sigint_received || sigpipe_received)
6844 error = histedit_commit(NULL, worktree,
6845 fileindex, tmp_branch, hle, repo);
6849 error = got_object_open_as_commit(
6850 &commit, repo, hle->commit_id);
6853 error = show_histedit_progress(commit,
6855 got_object_commit_close(commit);
6864 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6865 error = histedit_skip_commit(hle, worktree, repo);
6871 error = got_object_open_as_commit(&commit, repo,
6875 parent_ids = got_object_commit_get_parent_ids(commit);
6876 pid = SIMPLEQ_FIRST(parent_ids);
6878 error = got_worktree_histedit_merge_files(&merged_paths,
6879 worktree, fileindex, pid->id, hle->commit_id, repo,
6880 rebase_progress, &rebase_status, check_cancelled, NULL);
6883 got_object_commit_close(commit);
6886 if (rebase_status == GOT_STATUS_CONFLICT) {
6887 error = show_rebase_merge_conflict(hle->commit_id,
6891 got_worktree_rebase_pathlist_free(&merged_paths);
6895 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6897 error = got_object_id_str(&id_str, hle->commit_id);
6900 printf("Stopping histedit for amending commit %s\n",
6903 got_worktree_rebase_pathlist_free(&merged_paths);
6904 error = got_worktree_histedit_postpone(worktree,
6909 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6910 error = histedit_skip_commit(hle, worktree, repo);
6916 error = histedit_commit(&merged_paths, worktree, fileindex,
6917 tmp_branch, hle, repo);
6918 got_worktree_rebase_pathlist_free(&merged_paths);
6923 if (rebase_status == GOT_STATUS_CONFLICT) {
6924 error = got_worktree_histedit_postpone(worktree, fileindex);
6927 error = got_error_msg(GOT_ERR_CONFLICTS,
6928 "conflicts must be resolved before histedit can continue");
6930 error = histedit_complete(worktree, fileindex, tmp_branch,
6933 got_object_id_queue_free(&commits);
6934 histedit_free_list(&histedit_cmds);
6935 free(head_commit_id);
6936 free(base_commit_id);
6937 free(resume_commit_id);
6939 got_object_commit_close(commit);
6941 got_ref_close(branch);
6943 got_ref_close(tmp_branch);
6945 got_worktree_close(worktree);
6947 got_repo_close(repo);
6952 usage_integrate(void)
6954 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6958 static const struct got_error *
6959 cmd_integrate(int argc, char *argv[])
6961 const struct got_error *error = NULL;
6962 struct got_repository *repo = NULL;
6963 struct got_worktree *worktree = NULL;
6964 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6965 const char *branch_arg = NULL;
6966 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6967 struct got_fileindex *fileindex = NULL;
6968 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6969 int ch, did_something = 0;
6971 while ((ch = getopt(argc, argv, "")) != -1) {
6984 branch_arg = argv[0];
6986 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6987 "unveil", NULL) == -1)
6990 cwd = getcwd(NULL, 0);
6992 error = got_error_from_errno("getcwd");
6996 error = got_worktree_open(&worktree, cwd);
7000 error = check_rebase_or_histedit_in_progress(worktree);
7004 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7009 error = apply_unveil(got_repo_get_path(repo), 0,
7010 got_worktree_get_root_path(worktree));
7014 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7015 error = got_error_from_errno("asprintf");
7019 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7020 &base_branch_ref, worktree, refname, repo);
7024 refname = strdup(got_ref_get_name(branch_ref));
7025 if (refname == NULL) {
7026 error = got_error_from_errno("strdup");
7027 got_worktree_integrate_abort(worktree, fileindex, repo,
7028 branch_ref, base_branch_ref);
7031 base_refname = strdup(got_ref_get_name(base_branch_ref));
7032 if (base_refname == NULL) {
7033 error = got_error_from_errno("strdup");
7034 got_worktree_integrate_abort(worktree, fileindex, repo,
7035 branch_ref, base_branch_ref);
7039 error = got_ref_resolve(&commit_id, repo, branch_ref);
7043 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7047 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7048 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7049 "specified branch has already been integrated");
7050 got_worktree_integrate_abort(worktree, fileindex, repo,
7051 branch_ref, base_branch_ref);
7055 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7057 if (error->code == GOT_ERR_ANCESTRY)
7058 error = got_error(GOT_ERR_REBASE_REQUIRED);
7059 got_worktree_integrate_abort(worktree, fileindex, repo,
7060 branch_ref, base_branch_ref);
7064 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7065 branch_ref, base_branch_ref, update_progress, &did_something,
7066 check_cancelled, NULL);
7070 printf("Integrated %s into %s\n", refname, base_refname);
7073 got_repo_close(repo);
7075 got_worktree_close(worktree);
7077 free(base_commit_id);
7087 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7088 "[file-path ...]\n",
7093 static const struct got_error *
7094 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7095 const char *path, struct got_object_id *blob_id,
7096 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7097 int dirfd, const char *de_name)
7099 const struct got_error *err = NULL;
7100 char *id_str = NULL;
7102 if (staged_status != GOT_STATUS_ADD &&
7103 staged_status != GOT_STATUS_MODIFY &&
7104 staged_status != GOT_STATUS_DELETE)
7107 if (staged_status == GOT_STATUS_ADD ||
7108 staged_status == GOT_STATUS_MODIFY)
7109 err = got_object_id_str(&id_str, staged_blob_id);
7111 err = got_object_id_str(&id_str, blob_id);
7115 printf("%s %c %s\n", id_str, staged_status, path);
7120 static const struct got_error *
7121 cmd_stage(int argc, char *argv[])
7123 const struct got_error *error = NULL;
7124 struct got_repository *repo = NULL;
7125 struct got_worktree *worktree = NULL;
7127 struct got_pathlist_head paths;
7128 struct got_pathlist_entry *pe;
7129 int ch, list_stage = 0, pflag = 0;
7130 FILE *patch_script_file = NULL;
7131 const char *patch_script_path = NULL;
7132 struct choose_patch_arg cpa;
7136 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7145 patch_script_path = optarg;
7157 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7158 "unveil", NULL) == -1)
7161 if (list_stage && (pflag || patch_script_path))
7162 errx(1, "-l option cannot be used with other options");
7163 if (patch_script_path && !pflag)
7164 errx(1, "-F option can only be used together with -p option");
7166 cwd = getcwd(NULL, 0);
7168 error = got_error_from_errno("getcwd");
7172 error = got_worktree_open(&worktree, cwd);
7176 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7181 if (patch_script_path) {
7182 patch_script_file = fopen(patch_script_path, "r");
7183 if (patch_script_file == NULL) {
7184 error = got_error_from_errno2("fopen",
7189 error = apply_unveil(got_repo_get_path(repo), 0,
7190 got_worktree_get_root_path(worktree));
7194 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7199 error = got_worktree_status(worktree, &paths, repo,
7200 print_stage, NULL, check_cancelled, NULL);
7202 cpa.patch_script_file = patch_script_file;
7203 cpa.action = "stage";
7204 error = got_worktree_stage(worktree, &paths,
7205 pflag ? NULL : print_status, NULL,
7206 pflag ? choose_patch : NULL, &cpa, repo);
7209 if (patch_script_file && fclose(patch_script_file) == EOF &&
7211 error = got_error_from_errno2("fclose", patch_script_path);
7213 got_repo_close(repo);
7215 got_worktree_close(worktree);
7216 TAILQ_FOREACH(pe, &paths, entry)
7217 free((char *)pe->path);
7218 got_pathlist_free(&paths);
7226 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7227 "[file-path ...]\n",
7233 static const struct got_error *
7234 cmd_unstage(int argc, char *argv[])
7236 const struct got_error *error = NULL;
7237 struct got_repository *repo = NULL;
7238 struct got_worktree *worktree = NULL;
7240 struct got_pathlist_head paths;
7241 struct got_pathlist_entry *pe;
7242 int ch, did_something = 0, pflag = 0;
7243 FILE *patch_script_file = NULL;
7244 const char *patch_script_path = NULL;
7245 struct choose_patch_arg cpa;
7249 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7255 patch_script_path = optarg;
7267 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7268 "unveil", NULL) == -1)
7271 if (patch_script_path && !pflag)
7272 errx(1, "-F option can only be used together with -p option");
7274 cwd = getcwd(NULL, 0);
7276 error = got_error_from_errno("getcwd");
7280 error = got_worktree_open(&worktree, cwd);
7284 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7289 if (patch_script_path) {
7290 patch_script_file = fopen(patch_script_path, "r");
7291 if (patch_script_file == NULL) {
7292 error = got_error_from_errno2("fopen",
7298 error = apply_unveil(got_repo_get_path(repo), 0,
7299 got_worktree_get_root_path(worktree));
7303 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7307 cpa.patch_script_file = patch_script_file;
7308 cpa.action = "unstage";
7309 error = got_worktree_unstage(worktree, &paths, update_progress,
7310 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7312 if (patch_script_file && fclose(patch_script_file) == EOF &&
7314 error = got_error_from_errno2("fclose", patch_script_path);
7316 got_repo_close(repo);
7318 got_worktree_close(worktree);
7319 TAILQ_FOREACH(pe, &paths, entry)
7320 free((char *)pe->path);
7321 got_pathlist_free(&paths);
7329 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7330 "arg1 [arg2 ...]\n", getprogname());
7334 static const struct got_error *
7335 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7337 const struct got_error *err;
7338 struct got_blob_object *blob;
7340 err = got_object_open_as_blob(&blob, repo, id, 8192);
7344 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7345 got_object_blob_close(blob);
7349 static const struct got_error *
7350 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7352 const struct got_error *err;
7353 struct got_tree_object *tree;
7356 err = got_object_open_as_tree(&tree, repo, id);
7360 nentries = got_object_tree_get_nentries(tree);
7361 for (i = 0; i < nentries; i++) {
7362 struct got_tree_entry *te;
7364 if (sigint_received || sigpipe_received)
7366 te = got_object_tree_get_entry(tree, i);
7367 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7370 fprintf(outfile, "%s %.7o %s\n", id_str,
7371 got_tree_entry_get_mode(te),
7372 got_tree_entry_get_name(te));
7376 got_object_tree_close(tree);
7380 static const struct got_error *
7381 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7383 const struct got_error *err;
7384 struct got_commit_object *commit;
7385 const struct got_object_id_queue *parent_ids;
7386 struct got_object_qid *pid;
7387 char *id_str = NULL;
7388 const char *logmsg = NULL;
7390 err = got_object_open_as_commit(&commit, repo, id);
7394 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7398 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7399 parent_ids = got_object_commit_get_parent_ids(commit);
7400 fprintf(outfile, "numparents %d\n",
7401 got_object_commit_get_nparents(commit));
7402 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7404 err = got_object_id_str(&pid_str, pid->id);
7407 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7410 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7411 got_object_commit_get_author(commit),
7412 got_object_commit_get_author_time(commit));
7414 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7415 got_object_commit_get_author(commit),
7416 got_object_commit_get_committer_time(commit));
7418 logmsg = got_object_commit_get_logmsg_raw(commit);
7419 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7420 fprintf(outfile, "%s", logmsg);
7423 got_object_commit_close(commit);
7427 static const struct got_error *
7428 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7430 const struct got_error *err;
7431 struct got_tag_object *tag;
7432 char *id_str = NULL;
7433 const char *tagmsg = NULL;
7435 err = got_object_open_as_tag(&tag, repo, id);
7439 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7443 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7445 switch (got_object_tag_get_object_type(tag)) {
7446 case GOT_OBJ_TYPE_BLOB:
7447 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7448 GOT_OBJ_LABEL_BLOB);
7450 case GOT_OBJ_TYPE_TREE:
7451 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7452 GOT_OBJ_LABEL_TREE);
7454 case GOT_OBJ_TYPE_COMMIT:
7455 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7456 GOT_OBJ_LABEL_COMMIT);
7458 case GOT_OBJ_TYPE_TAG:
7459 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7466 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7467 got_object_tag_get_name(tag));
7469 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7470 got_object_tag_get_tagger(tag),
7471 got_object_tag_get_tagger_time(tag));
7473 tagmsg = got_object_tag_get_message(tag);
7474 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7475 fprintf(outfile, "%s", tagmsg);
7478 got_object_tag_close(tag);
7482 static const struct got_error *
7483 cmd_cat(int argc, char *argv[])
7485 const struct got_error *error;
7486 struct got_repository *repo = NULL;
7487 struct got_worktree *worktree = NULL;
7488 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7489 const char *commit_id_str = NULL;
7490 struct got_object_id *id = NULL, *commit_id = NULL;
7491 int ch, obj_type, i, force_path = 0;
7494 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7499 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7502 commit_id_str = optarg;
7505 repo_path = realpath(optarg, NULL);
7506 if (repo_path == NULL)
7507 return got_error_from_errno2("realpath",
7509 got_path_strip_trailing_slashes(repo_path);
7523 cwd = getcwd(NULL, 0);
7525 error = got_error_from_errno("getcwd");
7528 error = got_worktree_open(&worktree, cwd);
7529 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7532 if (repo_path == NULL) {
7534 got_worktree_get_repo_path(worktree));
7535 if (repo_path == NULL) {
7536 error = got_error_from_errno("strdup");
7542 if (repo_path == NULL) {
7543 repo_path = getcwd(NULL, 0);
7544 if (repo_path == NULL)
7545 return got_error_from_errno("getcwd");
7548 error = got_repo_open(&repo, repo_path, NULL);
7553 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7557 if (commit_id_str == NULL)
7558 commit_id_str = GOT_REF_HEAD;
7559 error = got_repo_match_object_id(&commit_id, NULL,
7560 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7564 for (i = 0; i < argc; i++) {
7566 error = got_object_id_by_path(&id, repo, commit_id,
7571 error = got_repo_match_object_id(&id, &label, argv[i],
7572 GOT_OBJ_TYPE_ANY, 0, repo);
7574 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7575 error->code != GOT_ERR_NOT_REF)
7577 error = got_object_id_by_path(&id, repo,
7578 commit_id, argv[i]);
7584 error = got_object_get_type(&obj_type, repo, id);
7589 case GOT_OBJ_TYPE_BLOB:
7590 error = cat_blob(id, repo, stdout);
7592 case GOT_OBJ_TYPE_TREE:
7593 error = cat_tree(id, repo, stdout);
7595 case GOT_OBJ_TYPE_COMMIT:
7596 error = cat_commit(id, repo, stdout);
7598 case GOT_OBJ_TYPE_TAG:
7599 error = cat_tag(id, repo, stdout);
7602 error = got_error(GOT_ERR_OBJ_TYPE);
7617 got_worktree_close(worktree);
7619 const struct got_error *repo_error;
7620 repo_error = got_repo_close(repo);