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 fetch_progress(void *arg, const char *message)
974 char *servername = arg;
975 printf("\rserver %s: %s", servername, message);
980 static const struct got_error *
981 cmd_clone(int argc, char *argv[])
983 const struct got_error *err = NULL;
984 const char *uri, *branch_filter, *dirname;
985 char *proto, *host, *port, *repo_name, *server_path;
986 char *default_destdir = NULL, *id_str = NULL;
987 const char *repo_path;
988 struct got_repository *repo = NULL;
989 struct got_pathlist_head refs, symrefs;
990 struct got_pathlist_entry *pe;
991 struct got_object_id *pack_hash = NULL;
992 int ch, fetchfd = -1;
995 TAILQ_INIT(&symrefs);
997 while ((ch = getopt(argc, argv, "b:")) != -1) {
1000 branch_filter = optarg;
1017 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1018 &repo_name, argv[0]);
1022 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1026 if (dirname == NULL) {
1027 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1028 err = got_error_from_errno("asprintf");
1031 repo_path = default_destdir;
1033 repo_path = dirname;
1035 err = got_path_mkdir(repo_path);
1039 err = got_repo_init(repo_path);
1043 err = got_repo_open(&repo, repo_path, NULL);
1047 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1048 repo, fetch_progress, host);
1052 err = got_object_id_str(&id_str, pack_hash);
1055 printf("Fetched %s.pack\n", id_str);
1058 /* Set up references provided with the pack file. */
1059 TAILQ_FOREACH(pe, &refs, entry) {
1060 const char *refname = pe->path;
1061 struct got_object_id *id = pe->data;
1062 struct got_reference *ref;
1064 err = got_object_id_str(&id_str, id);
1068 err = got_ref_alloc(&ref, refname, id);
1074 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1076 err = got_ref_write(ref, repo);
1082 /* Set the HEAD reference if the server provided one. */
1083 TAILQ_FOREACH(pe, &symrefs, entry) {
1084 struct got_reference *symref, *target_ref;
1085 const char *refname = pe->path;
1086 const char *target = pe->data;
1088 if (strcmp(refname, GOT_REF_HEAD) != 0)
1091 err = got_ref_open(&target_ref, repo, target, 0);
1093 if (err->code == GOT_ERR_NOT_REF)
1098 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1099 got_ref_close(target_ref);
1103 printf("Setting %s to %s\n", GOT_REF_HEAD,
1104 got_ref_get_symref_target(symref));
1106 err = got_ref_write(symref, repo);
1107 got_ref_close(symref);
1112 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1113 err = got_error_from_errno("close");
1115 got_repo_close(repo);
1116 TAILQ_FOREACH(pe, &refs, entry) {
1117 free((void *)pe->path);
1120 got_pathlist_free(&refs);
1121 TAILQ_FOREACH(pe, &symrefs, entry) {
1122 free((void *)pe->path);
1125 got_pathlist_free(&symrefs);
1132 free(default_destdir);
1136 static const struct got_error *
1137 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1139 static char msg[512];
1140 const char *branch_name;
1142 if (got_ref_is_symbolic(ref))
1143 branch_name = got_ref_get_symref_target(ref);
1145 branch_name = got_ref_get_name(ref);
1147 if (strncmp("refs/heads/", branch_name, 11) == 0)
1150 snprintf(msg, sizeof(msg),
1151 "target commit is not contained in branch '%s'; "
1152 "the branch to use must be specified with -b; "
1153 "if necessary a new branch can be created for "
1154 "this commit with 'got branch -c %s BRANCH_NAME'",
1155 branch_name, commit_id_str);
1157 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1160 static const struct got_error *
1161 cmd_checkout(int argc, char *argv[])
1163 const struct got_error *error = NULL;
1164 struct got_repository *repo = NULL;
1165 struct got_reference *head_ref = NULL;
1166 struct got_worktree *worktree = NULL;
1167 char *repo_path = NULL;
1168 char *worktree_path = NULL;
1169 const char *path_prefix = "";
1170 const char *branch_name = GOT_REF_HEAD;
1171 char *commit_id_str = NULL;
1172 int ch, same_path_prefix, allow_nonempty = 0;
1173 struct got_pathlist_head paths;
1174 struct got_checkout_progress_arg cpa;
1178 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1181 branch_name = optarg;
1184 commit_id_str = strdup(optarg);
1185 if (commit_id_str == NULL)
1186 return got_error_from_errno("strdup");
1192 path_prefix = optarg;
1204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 "unveil", NULL) == -1)
1209 char *cwd, *base, *dotgit;
1210 repo_path = realpath(argv[0], NULL);
1211 if (repo_path == NULL)
1212 return got_error_from_errno2("realpath", argv[0]);
1213 cwd = getcwd(NULL, 0);
1215 error = got_error_from_errno("getcwd");
1218 if (path_prefix[0]) {
1219 base = basename(path_prefix);
1221 error = got_error_from_errno2("basename",
1226 base = basename(repo_path);
1228 error = got_error_from_errno2("basename",
1233 dotgit = strstr(base, ".git");
1236 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1237 error = got_error_from_errno("asprintf");
1242 } else if (argc == 2) {
1243 repo_path = realpath(argv[0], NULL);
1244 if (repo_path == NULL) {
1245 error = got_error_from_errno2("realpath", argv[0]);
1248 worktree_path = realpath(argv[1], NULL);
1249 if (worktree_path == NULL) {
1250 if (errno != ENOENT) {
1251 error = got_error_from_errno2("realpath",
1255 worktree_path = strdup(argv[1]);
1256 if (worktree_path == NULL) {
1257 error = got_error_from_errno("strdup");
1264 got_path_strip_trailing_slashes(repo_path);
1265 got_path_strip_trailing_slashes(worktree_path);
1267 error = got_repo_open(&repo, repo_path, NULL);
1271 /* Pre-create work tree path for unveil(2) */
1272 error = got_path_mkdir(worktree_path);
1274 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1275 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1277 if (!allow_nonempty &&
1278 !got_path_dir_is_empty(worktree_path)) {
1279 error = got_error_path(worktree_path,
1280 GOT_ERR_DIR_NOT_EMPTY);
1285 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1289 error = got_ref_open(&head_ref, repo, branch_name, 0);
1293 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1294 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1297 error = got_worktree_open(&worktree, worktree_path);
1301 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1305 if (!same_path_prefix) {
1306 error = got_error(GOT_ERR_PATH_PREFIX);
1310 if (commit_id_str) {
1311 struct got_object_id *commit_id;
1312 error = got_repo_match_object_id(&commit_id, NULL,
1313 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1316 error = check_linear_ancestry(commit_id,
1317 got_worktree_get_base_commit_id(worktree), 0, repo);
1318 if (error != NULL) {
1320 if (error->code == GOT_ERR_ANCESTRY) {
1321 error = checkout_ancestry_error(
1322 head_ref, commit_id_str);
1326 error = check_same_branch(commit_id, head_ref, NULL, repo);
1328 if (error->code == GOT_ERR_ANCESTRY) {
1329 error = checkout_ancestry_error(
1330 head_ref, commit_id_str);
1334 error = got_worktree_set_base_commit_id(worktree, repo,
1341 error = got_pathlist_append(&paths, "", NULL);
1344 cpa.worktree_path = worktree_path;
1345 cpa.had_base_commit_ref_error = 0;
1346 error = got_worktree_checkout_files(worktree, &paths, repo,
1347 checkout_progress, &cpa, check_cancelled, NULL);
1351 printf("Now shut up and hack\n");
1352 if (cpa.had_base_commit_ref_error)
1353 show_worktree_base_ref_warning();
1355 got_pathlist_free(&paths);
1356 free(commit_id_str);
1358 free(worktree_path);
1365 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1370 static const struct got_error *
1371 update_progress(void *arg, unsigned char status, const char *path)
1373 int *did_something = arg;
1375 if (status == GOT_STATUS_EXISTS ||
1376 status == GOT_STATUS_BASE_REF_ERR)
1381 /* Base commit bump happens silently. */
1382 if (status == GOT_STATUS_BUMP_BASE)
1385 while (path[0] == '/')
1387 printf("%c %s\n", status, path);
1391 static const struct got_error *
1392 switch_head_ref(struct got_reference *head_ref,
1393 struct got_object_id *commit_id, struct got_worktree *worktree,
1394 struct got_repository *repo)
1396 const struct got_error *err = NULL;
1398 int ref_has_moved = 0;
1400 /* Trivial case: switching between two different references. */
1401 if (strcmp(got_ref_get_name(head_ref),
1402 got_worktree_get_head_ref_name(worktree)) != 0) {
1403 printf("Switching work tree from %s to %s\n",
1404 got_worktree_get_head_ref_name(worktree),
1405 got_ref_get_name(head_ref));
1406 return got_worktree_set_head_ref(worktree, head_ref);
1409 err = check_linear_ancestry(commit_id,
1410 got_worktree_get_base_commit_id(worktree), 0, repo);
1412 if (err->code != GOT_ERR_ANCESTRY)
1419 /* Switching to a rebased branch with the same reference name. */
1420 err = got_object_id_str(&base_id_str,
1421 got_worktree_get_base_commit_id(worktree));
1424 printf("Reference %s now points at a different branch\n",
1425 got_worktree_get_head_ref_name(worktree));
1426 printf("Switching work tree from %s to %s\n", base_id_str,
1427 got_worktree_get_head_ref_name(worktree));
1431 static const struct got_error *
1432 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1434 const struct got_error *err;
1437 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1441 return got_error(GOT_ERR_REBASING);
1443 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1447 return got_error(GOT_ERR_HISTEDIT_BUSY);
1452 static const struct got_error *
1453 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1454 char *argv[], struct got_worktree *worktree)
1456 const struct got_error *err = NULL;
1463 return got_error_from_errno("strdup");
1464 return got_pathlist_append(paths, path, NULL);
1467 for (i = 0; i < argc; i++) {
1468 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1471 err = got_pathlist_append(paths, path, NULL);
1481 static const struct got_error *
1482 cmd_update(int argc, char *argv[])
1484 const struct got_error *error = NULL;
1485 struct got_repository *repo = NULL;
1486 struct got_worktree *worktree = NULL;
1487 char *worktree_path = NULL;
1488 struct got_object_id *commit_id = NULL;
1489 char *commit_id_str = NULL;
1490 const char *branch_name = NULL;
1491 struct got_reference *head_ref = NULL;
1492 struct got_pathlist_head paths;
1493 struct got_pathlist_entry *pe;
1494 int ch, did_something = 0;
1498 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1501 branch_name = optarg;
1504 commit_id_str = strdup(optarg);
1505 if (commit_id_str == NULL)
1506 return got_error_from_errno("strdup");
1518 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1519 "unveil", NULL) == -1)
1522 worktree_path = getcwd(NULL, 0);
1523 if (worktree_path == NULL) {
1524 error = got_error_from_errno("getcwd");
1527 error = got_worktree_open(&worktree, worktree_path);
1531 error = check_rebase_or_histedit_in_progress(worktree);
1535 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1540 error = apply_unveil(got_repo_get_path(repo), 0,
1541 got_worktree_get_root_path(worktree));
1545 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1549 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1550 got_worktree_get_head_ref_name(worktree), 0);
1553 if (commit_id_str == NULL) {
1554 error = got_ref_resolve(&commit_id, repo, head_ref);
1557 error = got_object_id_str(&commit_id_str, commit_id);
1561 error = got_repo_match_object_id(&commit_id, NULL,
1562 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1563 free(commit_id_str);
1564 commit_id_str = NULL;
1567 error = got_object_id_str(&commit_id_str, commit_id);
1573 struct got_object_id *head_commit_id;
1574 TAILQ_FOREACH(pe, &paths, entry) {
1575 if (pe->path_len == 0)
1577 error = got_error_msg(GOT_ERR_BAD_PATH,
1578 "switching between branches requires that "
1579 "the entire work tree gets updated");
1582 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1585 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1587 free(head_commit_id);
1590 error = check_same_branch(commit_id, head_ref, NULL, repo);
1593 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1597 error = check_linear_ancestry(commit_id,
1598 got_worktree_get_base_commit_id(worktree), 0, repo);
1599 if (error != NULL) {
1600 if (error->code == GOT_ERR_ANCESTRY)
1601 error = got_error(GOT_ERR_BRANCH_MOVED);
1604 error = check_same_branch(commit_id, head_ref, NULL, repo);
1609 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1611 error = got_worktree_set_base_commit_id(worktree, repo,
1617 error = got_worktree_checkout_files(worktree, &paths, repo,
1618 update_progress, &did_something, check_cancelled, NULL);
1623 printf("Updated to commit %s\n", commit_id_str);
1625 printf("Already up-to-date\n");
1627 free(worktree_path);
1628 TAILQ_FOREACH(pe, &paths, entry)
1629 free((char *)pe->path);
1630 got_pathlist_free(&paths);
1632 free(commit_id_str);
1636 static const struct got_error *
1637 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1638 const char *path, int diff_context, int ignore_whitespace,
1639 struct got_repository *repo)
1641 const struct got_error *err = NULL;
1642 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1645 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1650 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1654 while (path[0] == '/')
1656 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1657 ignore_whitespace, stdout);
1660 got_object_blob_close(blob1);
1661 got_object_blob_close(blob2);
1665 static const struct got_error *
1666 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1667 const char *path, int diff_context, int ignore_whitespace,
1668 struct got_repository *repo)
1670 const struct got_error *err = NULL;
1671 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1672 struct got_diff_blob_output_unidiff_arg arg;
1675 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1680 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1684 arg.diff_context = diff_context;
1685 arg.ignore_whitespace = ignore_whitespace;
1686 arg.outfile = stdout;
1687 while (path[0] == '/')
1689 err = got_diff_tree(tree1, tree2, path, path, repo,
1690 got_diff_blob_output_unidiff, &arg, 1);
1693 got_object_tree_close(tree1);
1695 got_object_tree_close(tree2);
1699 static const struct got_error *
1700 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1701 const char *path, int diff_context, struct got_repository *repo)
1703 const struct got_error *err = NULL;
1704 struct got_commit_object *pcommit = NULL;
1705 char *id_str1 = NULL, *id_str2 = NULL;
1706 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1707 struct got_object_qid *qid;
1709 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1711 err = got_object_open_as_commit(&pcommit, repo,
1717 if (path && path[0] != '\0') {
1719 err = got_object_id_by_path(&obj_id2, repo, id, path);
1722 err = got_object_id_str(&id_str2, obj_id2);
1728 err = got_object_id_by_path(&obj_id1, repo,
1734 err = got_object_id_str(&id_str1, obj_id1);
1740 err = got_object_get_type(&obj_type, repo, obj_id2);
1745 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1747 case GOT_OBJ_TYPE_BLOB:
1748 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1751 case GOT_OBJ_TYPE_TREE:
1752 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1756 err = got_error(GOT_ERR_OBJ_TYPE);
1762 obj_id2 = got_object_commit_get_tree_id(commit);
1763 err = got_object_id_str(&id_str2, obj_id2);
1766 obj_id1 = got_object_commit_get_tree_id(pcommit);
1767 err = got_object_id_str(&id_str1, obj_id1);
1770 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1771 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1777 got_object_commit_close(pcommit);
1782 get_datestr(time_t *time, char *datebuf)
1784 struct tm mytm, *tm;
1787 tm = gmtime_r(time, &mytm);
1790 s = asctime_r(tm, datebuf);
1793 p = strchr(s, '\n');
1799 static const struct got_error *
1800 match_logmsg(int *have_match, struct got_object_id *id,
1801 struct got_commit_object *commit, regex_t *regex)
1803 const struct got_error *err = NULL;
1804 regmatch_t regmatch;
1805 char *id_str = NULL, *logmsg = NULL;
1809 err = got_object_id_str(&id_str, id);
1813 err = got_object_commit_get_logmsg(&logmsg, commit);
1817 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1825 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1827 static const struct got_error *
1828 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1829 struct got_repository *repo, const char *path, int show_patch,
1830 int diff_context, struct got_reflist_head *refs)
1832 const struct got_error *err = NULL;
1833 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1835 time_t committer_time;
1836 const char *author, *committer;
1837 char *refs_str = NULL;
1838 struct got_reflist_entry *re;
1840 SIMPLEQ_FOREACH(re, refs, entry) {
1843 struct got_tag_object *tag = NULL;
1846 name = got_ref_get_name(re->ref);
1847 if (strcmp(name, GOT_REF_HEAD) == 0)
1849 if (strncmp(name, "refs/", 5) == 0)
1851 if (strncmp(name, "got/", 4) == 0)
1853 if (strncmp(name, "heads/", 6) == 0)
1855 if (strncmp(name, "remotes/", 8) == 0)
1857 if (strncmp(name, "tags/", 5) == 0) {
1858 err = got_object_open_as_tag(&tag, repo, re->id);
1860 if (err->code != GOT_ERR_OBJ_TYPE)
1862 /* Ref points at something other than a tag. */
1867 cmp = got_object_id_cmp(tag ?
1868 got_object_tag_get_object_id(tag) : re->id, id);
1870 got_object_tag_close(tag);
1874 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1876 err = got_error_from_errno("asprintf");
1882 err = got_object_id_str(&id_str, id);
1886 printf(GOT_COMMIT_SEP_STR);
1887 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1888 refs_str ? refs_str : "", refs_str ? ")" : "");
1893 printf("from: %s\n", got_object_commit_get_author(commit));
1894 committer_time = got_object_commit_get_committer_time(commit);
1895 datestr = get_datestr(&committer_time, datebuf);
1897 printf("date: %s UTC\n", datestr);
1898 author = got_object_commit_get_author(commit);
1899 committer = got_object_commit_get_committer(commit);
1900 if (strcmp(author, committer) != 0)
1901 printf("via: %s\n", committer);
1902 if (got_object_commit_get_nparents(commit) > 1) {
1903 const struct got_object_id_queue *parent_ids;
1904 struct got_object_qid *qid;
1906 parent_ids = got_object_commit_get_parent_ids(commit);
1907 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1908 err = got_object_id_str(&id_str, qid->id);
1911 printf("parent %d: %s\n", n++, id_str);
1916 err = got_object_commit_get_logmsg(&logmsg0, commit);
1922 line = strsep(&logmsg, "\n");
1924 printf(" %s\n", line);
1929 err = print_patch(commit, id, path, diff_context, repo);
1934 if (fflush(stdout) != 0 && err == NULL)
1935 err = got_error_from_errno("fflush");
1939 static const struct got_error *
1940 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1941 const char *path, int show_patch, const char *search_pattern,
1942 int diff_context, int limit, int log_branches,
1943 struct got_reflist_head *refs)
1945 const struct got_error *err;
1946 struct got_commit_graph *graph;
1950 if (search_pattern &&
1951 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1952 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1954 err = got_commit_graph_open(&graph, path, !log_branches);
1957 err = got_commit_graph_iter_start(graph, root_id, repo,
1958 check_cancelled, NULL);
1962 struct got_commit_object *commit;
1963 struct got_object_id *id;
1965 if (sigint_received || sigpipe_received)
1968 err = got_commit_graph_iter_next(&id, graph, repo,
1969 check_cancelled, NULL);
1971 if (err->code == GOT_ERR_ITER_COMPLETED)
1978 err = got_object_open_as_commit(&commit, repo, id);
1982 if (search_pattern) {
1983 err = match_logmsg(&have_match, id, commit, ®ex);
1985 got_object_commit_close(commit);
1988 if (have_match == 0) {
1989 got_object_commit_close(commit);
1994 err = print_commit(commit, id, repo, path, show_patch,
1995 diff_context, refs);
1996 got_object_commit_close(commit);
1997 if (err || (limit && --limit == 0))
2003 got_commit_graph_close(graph);
2010 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2011 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2016 get_default_log_limit(void)
2018 const char *got_default_log_limit;
2022 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2023 if (got_default_log_limit == NULL)
2025 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2031 static const struct got_error *
2032 cmd_log(int argc, char *argv[])
2034 const struct got_error *error;
2035 struct got_repository *repo = NULL;
2036 struct got_worktree *worktree = NULL;
2037 struct got_commit_object *commit = NULL;
2038 struct got_object_id *id = NULL;
2039 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2040 const char *start_commit = NULL, *search_pattern = NULL;
2041 int diff_context = -1, ch;
2042 int show_patch = 0, limit = 0, log_branches = 0;
2044 struct got_reflist_head refs;
2046 SIMPLEQ_INIT(&refs);
2049 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2055 limit = get_default_log_limit();
2057 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2063 start_commit = optarg;
2066 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2069 err(1, "-C option %s", errstr);
2072 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2074 err(1, "-l option %s", errstr);
2080 repo_path = realpath(optarg, NULL);
2081 if (repo_path == NULL)
2082 return got_error_from_errno2("realpath",
2084 got_path_strip_trailing_slashes(repo_path);
2087 search_pattern = optarg;
2098 if (diff_context == -1)
2100 else if (!show_patch)
2101 errx(1, "-C reguires -p");
2103 cwd = getcwd(NULL, 0);
2105 error = got_error_from_errno("getcwd");
2109 error = got_worktree_open(&worktree, cwd);
2110 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2117 error = got_error_from_errno("strdup");
2120 } else if (argc == 1) {
2122 error = got_worktree_resolve_path(&path, worktree,
2127 path = strdup(argv[0]);
2129 error = got_error_from_errno("strdup");
2136 if (repo_path == NULL) {
2137 repo_path = worktree ?
2138 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2140 if (repo_path == NULL) {
2141 error = got_error_from_errno("strdup");
2145 error = got_repo_open(&repo, repo_path, NULL);
2149 error = apply_unveil(got_repo_get_path(repo), 1,
2150 worktree ? got_worktree_get_root_path(worktree) : NULL);
2154 if (start_commit == NULL) {
2155 struct got_reference *head_ref;
2156 error = got_ref_open(&head_ref, repo,
2157 worktree ? got_worktree_get_head_ref_name(worktree)
2161 error = got_ref_resolve(&id, repo, head_ref);
2162 got_ref_close(head_ref);
2165 error = got_object_open_as_commit(&commit, repo, id);
2167 struct got_reference *ref;
2168 error = got_ref_open(&ref, repo, start_commit, 0);
2169 if (error == NULL) {
2171 error = got_ref_resolve(&id, repo, ref);
2175 error = got_object_get_type(&obj_type, repo, id);
2178 if (obj_type == GOT_OBJ_TYPE_TAG) {
2179 struct got_tag_object *tag;
2180 error = got_object_open_as_tag(&tag, repo, id);
2183 if (got_object_tag_get_object_type(tag) !=
2184 GOT_OBJ_TYPE_COMMIT) {
2185 got_object_tag_close(tag);
2186 error = got_error(GOT_ERR_OBJ_TYPE);
2190 id = got_object_id_dup(
2191 got_object_tag_get_object_id(tag));
2193 error = got_error_from_errno(
2194 "got_object_id_dup");
2195 got_object_tag_close(tag);
2198 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2199 error = got_error(GOT_ERR_OBJ_TYPE);
2202 error = got_object_open_as_commit(&commit, repo, id);
2206 if (commit == NULL) {
2207 error = got_repo_match_object_id_prefix(&id,
2208 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2217 const char *prefix = got_worktree_get_path_prefix(worktree);
2219 if (asprintf(&p, "%s%s%s", prefix,
2220 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2221 error = got_error_from_errno("asprintf");
2224 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2227 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2232 path = in_repo_path;
2235 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2239 error = print_commits(id, repo, path, show_patch, search_pattern,
2240 diff_context, limit, log_branches, &refs);
2247 got_worktree_close(worktree);
2249 const struct got_error *repo_error;
2250 repo_error = got_repo_close(repo);
2254 got_ref_list_free(&refs);
2261 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2262 "[-w] [object1 object2 | path]\n", getprogname());
2266 struct print_diff_arg {
2267 struct got_repository *repo;
2268 struct got_worktree *worktree;
2273 int ignore_whitespace;
2276 static const struct got_error *
2277 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2278 const char *path, struct got_object_id *blob_id,
2279 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2280 int dirfd, const char *de_name)
2282 struct print_diff_arg *a = arg;
2283 const struct got_error *err = NULL;
2284 struct got_blob_object *blob1 = NULL;
2287 char *abspath = NULL, *label1 = NULL;
2290 if (a->diff_staged) {
2291 if (staged_status != GOT_STATUS_MODIFY &&
2292 staged_status != GOT_STATUS_ADD &&
2293 staged_status != GOT_STATUS_DELETE)
2296 if (staged_status == GOT_STATUS_DELETE)
2298 if (status == GOT_STATUS_NONEXISTENT)
2299 return got_error_set_errno(ENOENT, path);
2300 if (status != GOT_STATUS_MODIFY &&
2301 status != GOT_STATUS_ADD &&
2302 status != GOT_STATUS_DELETE &&
2303 status != GOT_STATUS_CONFLICT)
2307 if (!a->header_shown) {
2308 printf("diff %s %s%s\n", a->id_str,
2309 got_worktree_get_root_path(a->worktree),
2310 a->diff_staged ? " (staged changes)" : "");
2311 a->header_shown = 1;
2314 if (a->diff_staged) {
2315 const char *label1 = NULL, *label2 = NULL;
2316 switch (staged_status) {
2317 case GOT_STATUS_MODIFY:
2321 case GOT_STATUS_ADD:
2324 case GOT_STATUS_DELETE:
2328 return got_error(GOT_ERR_FILE_STATUS);
2330 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2331 label1, label2, a->diff_context, a->ignore_whitespace,
2335 if (staged_status == GOT_STATUS_ADD ||
2336 staged_status == GOT_STATUS_MODIFY) {
2338 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2342 err = got_object_id_str(&id_str, staged_blob_id);
2345 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2346 err = got_error_from_errno("asprintf");
2351 } else if (status != GOT_STATUS_ADD) {
2352 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2357 if (status != GOT_STATUS_DELETE) {
2358 if (asprintf(&abspath, "%s/%s",
2359 got_worktree_get_root_path(a->worktree), path) == -1) {
2360 err = got_error_from_errno("asprintf");
2365 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2367 err = got_error_from_errno2("openat", abspath);
2371 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2373 err = got_error_from_errno2("open", abspath);
2377 if (fstat(fd, &sb) == -1) {
2378 err = got_error_from_errno2("fstat", abspath);
2381 f2 = fdopen(fd, "r");
2383 err = got_error_from_errno2("fdopen", abspath);
2390 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2391 a->diff_context, a->ignore_whitespace, stdout);
2394 got_object_blob_close(blob1);
2395 if (f2 && fclose(f2) == EOF && err == NULL)
2396 err = got_error_from_errno("fclose");
2397 if (fd != -1 && close(fd) == -1 && err == NULL)
2398 err = got_error_from_errno("close");
2403 static const struct got_error *
2404 cmd_diff(int argc, char *argv[])
2406 const struct got_error *error;
2407 struct got_repository *repo = NULL;
2408 struct got_worktree *worktree = NULL;
2409 char *cwd = NULL, *repo_path = NULL;
2410 struct got_object_id *id1 = NULL, *id2 = NULL;
2411 const char *id_str1 = NULL, *id_str2 = NULL;
2412 char *label1 = NULL, *label2 = NULL;
2414 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2419 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2424 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2427 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2430 err(1, "-C option %s", errstr);
2433 repo_path = realpath(optarg, NULL);
2434 if (repo_path == NULL)
2435 return got_error_from_errno2("realpath",
2437 got_path_strip_trailing_slashes(repo_path);
2443 ignore_whitespace = 1;
2454 cwd = getcwd(NULL, 0);
2456 error = got_error_from_errno("getcwd");
2459 error = got_worktree_open(&worktree, cwd);
2460 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2463 if (worktree == NULL) {
2464 error = got_error(GOT_ERR_NOT_WORKTREE);
2469 "-r option can't be used when diffing a work tree");
2470 repo_path = strdup(got_worktree_get_repo_path(worktree));
2471 if (repo_path == NULL) {
2472 error = got_error_from_errno("strdup");
2476 error = got_worktree_resolve_path(&path, worktree,
2483 error = got_error_from_errno("strdup");
2487 } else if (argc == 2) {
2489 errx(1, "-s option can't be used when diffing "
2490 "objects in repository");
2493 if (worktree && repo_path == NULL) {
2495 strdup(got_worktree_get_repo_path(worktree));
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2504 if (repo_path == NULL) {
2505 repo_path = getcwd(NULL, 0);
2506 if (repo_path == NULL)
2507 return got_error_from_errno("getcwd");
2510 error = got_repo_open(&repo, repo_path, NULL);
2515 error = apply_unveil(got_repo_get_path(repo), 1,
2516 worktree ? got_worktree_get_root_path(worktree) : NULL);
2521 struct print_diff_arg arg;
2522 struct got_pathlist_head paths;
2527 error = got_object_id_str(&id_str,
2528 got_worktree_get_base_commit_id(worktree));
2532 arg.worktree = worktree;
2533 arg.diff_context = diff_context;
2534 arg.id_str = id_str;
2535 arg.header_shown = 0;
2536 arg.diff_staged = diff_staged;
2537 arg.ignore_whitespace = ignore_whitespace;
2539 error = got_pathlist_append(&paths, path, NULL);
2543 error = got_worktree_status(worktree, &paths, repo, print_diff,
2544 &arg, check_cancelled, NULL);
2546 got_pathlist_free(&paths);
2550 error = got_repo_match_object_id(&id1, &label1, id_str1,
2551 GOT_OBJ_TYPE_ANY, 1, repo);
2555 error = got_repo_match_object_id(&id2, &label2, id_str2,
2556 GOT_OBJ_TYPE_ANY, 1, repo);
2560 error = got_object_get_type(&type1, repo, id1);
2564 error = got_object_get_type(&type2, repo, id2);
2568 if (type1 != type2) {
2569 error = got_error(GOT_ERR_OBJ_TYPE);
2574 case GOT_OBJ_TYPE_BLOB:
2575 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2576 diff_context, ignore_whitespace, repo, stdout);
2578 case GOT_OBJ_TYPE_TREE:
2579 error = got_diff_objects_as_trees(id1, id2, "", "",
2580 diff_context, ignore_whitespace, repo, stdout);
2582 case GOT_OBJ_TYPE_COMMIT:
2583 printf("diff %s %s\n", label1, label2);
2584 error = got_diff_objects_as_commits(id1, id2, diff_context,
2585 ignore_whitespace, repo, stdout);
2588 error = got_error(GOT_ERR_OBJ_TYPE);
2597 got_worktree_close(worktree);
2599 const struct got_error *repo_error;
2600 repo_error = got_repo_close(repo);
2611 "usage: %s blame [-c commit] [-r repository-path] path\n",
2620 char datebuf[11]; /* YYYY-MM-DD + NUL */
2623 struct blame_cb_args {
2624 struct blame_line *lines;
2628 off_t *line_offsets;
2630 struct got_repository *repo;
2633 static const struct got_error *
2634 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2636 const struct got_error *err = NULL;
2637 struct blame_cb_args *a = arg;
2638 struct blame_line *bline;
2640 size_t linesize = 0;
2641 struct got_commit_object *commit = NULL;
2644 time_t committer_time;
2646 if (nlines != a->nlines ||
2647 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2648 return got_error(GOT_ERR_RANGE);
2650 if (sigint_received)
2651 return got_error(GOT_ERR_ITER_COMPLETED);
2654 return NULL; /* no change in this commit */
2656 /* Annotate this line. */
2657 bline = &a->lines[lineno - 1];
2658 if (bline->annotated)
2660 err = got_object_id_str(&bline->id_str, id);
2664 err = got_object_open_as_commit(&commit, a->repo, id);
2668 bline->committer = strdup(got_object_commit_get_committer(commit));
2669 if (bline->committer == NULL) {
2670 err = got_error_from_errno("strdup");
2674 committer_time = got_object_commit_get_committer_time(commit);
2675 if (localtime_r(&committer_time, &tm) == NULL)
2676 return got_error_from_errno("localtime_r");
2677 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2678 &tm) >= sizeof(bline->datebuf)) {
2679 err = got_error(GOT_ERR_NO_SPACE);
2682 bline->annotated = 1;
2684 /* Print lines annotated so far. */
2685 bline = &a->lines[a->lineno_cur - 1];
2686 if (!bline->annotated)
2689 offset = a->line_offsets[a->lineno_cur - 1];
2690 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2691 err = got_error_from_errno("fseeko");
2695 while (bline->annotated) {
2696 char *smallerthan, *at, *nl, *committer;
2699 if (getline(&line, &linesize, a->f) == -1) {
2701 err = got_error_from_errno("getline");
2705 committer = bline->committer;
2706 smallerthan = strchr(committer, '<');
2707 if (smallerthan && smallerthan[1] != '\0')
2708 committer = smallerthan + 1;
2709 at = strchr(committer, '@');
2712 len = strlen(committer);
2714 committer[8] = '\0';
2716 nl = strchr(line, '\n');
2719 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2720 bline->id_str, bline->datebuf, committer, line);
2723 bline = &a->lines[a->lineno_cur - 1];
2727 got_object_commit_close(commit);
2732 static const struct got_error *
2733 cmd_blame(int argc, char *argv[])
2735 const struct got_error *error;
2736 struct got_repository *repo = NULL;
2737 struct got_worktree *worktree = NULL;
2738 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2739 struct got_object_id *obj_id = NULL;
2740 struct got_object_id *commit_id = NULL;
2741 struct got_blob_object *blob = NULL;
2742 char *commit_id_str = NULL;
2743 struct blame_cb_args bca;
2744 int ch, obj_type, i;
2747 memset(&bca, 0, sizeof(bca));
2750 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2755 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2758 commit_id_str = optarg;
2761 repo_path = realpath(optarg, NULL);
2762 if (repo_path == NULL)
2763 return got_error_from_errno2("realpath",
2765 got_path_strip_trailing_slashes(repo_path);
2781 cwd = getcwd(NULL, 0);
2783 error = got_error_from_errno("getcwd");
2786 if (repo_path == NULL) {
2787 error = got_worktree_open(&worktree, cwd);
2788 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2794 strdup(got_worktree_get_repo_path(worktree));
2795 if (repo_path == NULL)
2796 error = got_error_from_errno("strdup");
2800 repo_path = strdup(cwd);
2801 if (repo_path == NULL) {
2802 error = got_error_from_errno("strdup");
2808 error = got_repo_open(&repo, repo_path, NULL);
2812 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2817 const char *prefix = got_worktree_get_path_prefix(worktree);
2818 char *p, *worktree_subdir = cwd +
2819 strlen(got_worktree_get_root_path(worktree));
2820 if (asprintf(&p, "%s%s%s%s%s",
2821 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2822 worktree_subdir, worktree_subdir[0] ? "/" : "",
2824 error = got_error_from_errno("asprintf");
2827 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2830 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2835 if (commit_id_str == NULL) {
2836 struct got_reference *head_ref;
2837 error = got_ref_open(&head_ref, repo, worktree ?
2838 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2841 error = got_ref_resolve(&commit_id, repo, head_ref);
2842 got_ref_close(head_ref);
2846 error = got_repo_match_object_id(&commit_id, NULL,
2847 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2852 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2856 error = got_object_get_type(&obj_type, repo, obj_id);
2860 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2861 error = got_error(GOT_ERR_OBJ_TYPE);
2865 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2868 bca.f = got_opentemp();
2869 if (bca.f == NULL) {
2870 error = got_error_from_errno("got_opentemp");
2873 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2874 &bca.line_offsets, bca.f, blob);
2875 if (error || bca.nlines == 0)
2878 /* Don't include \n at EOF in the blame line count. */
2879 if (bca.line_offsets[bca.nlines - 1] == filesize)
2882 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2883 if (bca.lines == NULL) {
2884 error = got_error_from_errno("calloc");
2888 bca.nlines_prec = 0;
2896 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2897 check_cancelled, NULL);
2905 got_object_blob_close(blob);
2907 got_worktree_close(worktree);
2909 const struct got_error *repo_error;
2910 repo_error = got_repo_close(repo);
2915 for (i = 0; i < bca.nlines; i++) {
2916 struct blame_line *bline = &bca.lines[i];
2917 free(bline->id_str);
2918 free(bline->committer);
2922 free(bca.line_offsets);
2923 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2924 error = got_error_from_errno("fclose");
2932 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2938 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2939 const char *root_path)
2941 int is_root_path = (strcmp(path, root_path) == 0);
2942 const char *modestr = "";
2943 mode_t mode = got_tree_entry_get_mode(te);
2945 path += strlen(root_path);
2946 while (path[0] == '/')
2949 if (got_object_tree_entry_is_submodule(te))
2951 else if (S_ISLNK(mode))
2953 else if (S_ISDIR(mode))
2955 else if (mode & S_IXUSR)
2958 printf("%s%s%s%s%s\n", id ? id : "", path,
2959 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2962 static const struct got_error *
2963 print_tree(const char *path, struct got_object_id *commit_id,
2964 int show_ids, int recurse, const char *root_path,
2965 struct got_repository *repo)
2967 const struct got_error *err = NULL;
2968 struct got_object_id *tree_id = NULL;
2969 struct got_tree_object *tree = NULL;
2972 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2976 err = got_object_open_as_tree(&tree, repo, tree_id);
2979 nentries = got_object_tree_get_nentries(tree);
2980 for (i = 0; i < nentries; i++) {
2981 struct got_tree_entry *te;
2984 if (sigint_received || sigpipe_received)
2987 te = got_object_tree_get_entry(tree, i);
2990 err = got_object_id_str(&id_str,
2991 got_tree_entry_get_id(te));
2994 if (asprintf(&id, "%s ", id_str) == -1) {
2995 err = got_error_from_errno("asprintf");
3001 print_entry(te, id, path, root_path);
3004 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3006 if (asprintf(&child_path, "%s%s%s", path,
3007 path[0] == '/' && path[1] == '\0' ? "" : "/",
3008 got_tree_entry_get_name(te)) == -1) {
3009 err = got_error_from_errno("asprintf");
3012 err = print_tree(child_path, commit_id, show_ids, 1,
3021 got_object_tree_close(tree);
3026 static const struct got_error *
3027 cmd_tree(int argc, char *argv[])
3029 const struct got_error *error;
3030 struct got_repository *repo = NULL;
3031 struct got_worktree *worktree = NULL;
3033 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3034 struct got_object_id *commit_id = NULL;
3035 char *commit_id_str = NULL;
3036 int show_ids = 0, recurse = 0;
3040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3045 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3048 commit_id_str = optarg;
3051 repo_path = realpath(optarg, NULL);
3052 if (repo_path == NULL)
3053 return got_error_from_errno2("realpath",
3055 got_path_strip_trailing_slashes(repo_path);
3079 cwd = getcwd(NULL, 0);
3081 error = got_error_from_errno("getcwd");
3084 if (repo_path == NULL) {
3085 error = got_worktree_open(&worktree, cwd);
3086 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3092 strdup(got_worktree_get_repo_path(worktree));
3093 if (repo_path == NULL)
3094 error = got_error_from_errno("strdup");
3098 repo_path = strdup(cwd);
3099 if (repo_path == NULL) {
3100 error = got_error_from_errno("strdup");
3106 error = got_repo_open(&repo, repo_path, NULL);
3110 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3116 char *p, *worktree_subdir = cwd +
3117 strlen(got_worktree_get_root_path(worktree));
3118 if (asprintf(&p, "%s/%s",
3119 got_worktree_get_path_prefix(worktree),
3120 worktree_subdir) == -1) {
3121 error = got_error_from_errno("asprintf");
3124 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3131 if (in_repo_path == NULL) {
3132 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3137 if (commit_id_str == NULL) {
3138 struct got_reference *head_ref;
3139 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3142 error = got_ref_resolve(&commit_id, repo, head_ref);
3143 got_ref_close(head_ref);
3147 error = got_repo_match_object_id(&commit_id, NULL,
3148 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3153 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3154 in_repo_path, repo);
3161 got_worktree_close(worktree);
3163 const struct got_error *repo_error;
3164 repo_error = got_repo_close(repo);
3174 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3178 static const struct got_error *
3179 print_status(void *arg, unsigned char status, unsigned char staged_status,
3180 const char *path, struct got_object_id *blob_id,
3181 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3182 int dirfd, const char *de_name)
3184 if (status == staged_status && (status == GOT_STATUS_DELETE))
3185 status = GOT_STATUS_NO_CHANGE;
3186 printf("%c%c %s\n", status, staged_status, path);
3190 static const struct got_error *
3191 cmd_status(int argc, char *argv[])
3193 const struct got_error *error = NULL;
3194 struct got_repository *repo = NULL;
3195 struct got_worktree *worktree = NULL;
3197 struct got_pathlist_head paths;
3198 struct got_pathlist_entry *pe;
3203 while ((ch = getopt(argc, argv, "")) != -1) {
3215 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3219 cwd = getcwd(NULL, 0);
3221 error = got_error_from_errno("getcwd");
3225 error = got_worktree_open(&worktree, cwd);
3229 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3234 error = apply_unveil(got_repo_get_path(repo), 1,
3235 got_worktree_get_root_path(worktree));
3239 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3243 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3244 check_cancelled, NULL);
3246 TAILQ_FOREACH(pe, &paths, entry)
3247 free((char *)pe->path);
3248 got_pathlist_free(&paths);
3257 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3262 static const struct got_error *
3263 list_refs(struct got_repository *repo)
3265 static const struct got_error *err = NULL;
3266 struct got_reflist_head refs;
3267 struct got_reflist_entry *re;
3269 SIMPLEQ_INIT(&refs);
3270 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3274 SIMPLEQ_FOREACH(re, &refs, entry) {
3276 refstr = got_ref_to_str(re->ref);
3278 return got_error_from_errno("got_ref_to_str");
3279 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3283 got_ref_list_free(&refs);
3287 static const struct got_error *
3288 delete_ref(struct got_repository *repo, const char *refname)
3290 const struct got_error *err = NULL;
3291 struct got_reference *ref;
3293 err = got_ref_open(&ref, repo, refname, 0);
3297 err = got_ref_delete(ref, repo);
3302 static const struct got_error *
3303 add_ref(struct got_repository *repo, const char *refname, const char *target)
3305 const struct got_error *err = NULL;
3306 struct got_object_id *id;
3307 struct got_reference *ref = NULL;
3310 * Don't let the user create a reference name with a leading '-'.
3311 * While technically a valid reference name, this case is usually
3312 * an unintended typo.
3314 if (refname[0] == '-')
3315 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3317 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3320 struct got_reference *target_ref;
3322 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3324 err = got_ref_open(&target_ref, repo, target, 0);
3327 err = got_ref_resolve(&id, repo, target_ref);
3328 got_ref_close(target_ref);
3333 err = got_ref_alloc(&ref, refname, id);
3337 err = got_ref_write(ref, repo);
3345 static const struct got_error *
3346 add_symref(struct got_repository *repo, const char *refname, const char *target)
3348 const struct got_error *err = NULL;
3349 struct got_reference *ref = NULL;
3350 struct got_reference *target_ref = NULL;
3353 * Don't let the user create a reference name with a leading '-'.
3354 * While technically a valid reference name, this case is usually
3355 * an unintended typo.
3357 if (refname[0] == '-')
3358 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3360 err = got_ref_open(&target_ref, repo, target, 0);
3364 err = got_ref_alloc_symref(&ref, refname, target_ref);
3368 err = got_ref_write(ref, repo);
3371 got_ref_close(target_ref);
3377 static const struct got_error *
3378 cmd_ref(int argc, char *argv[])
3380 const struct got_error *error = NULL;
3381 struct got_repository *repo = NULL;
3382 struct got_worktree *worktree = NULL;
3383 char *cwd = NULL, *repo_path = NULL;
3384 int ch, do_list = 0, create_symref = 0;
3385 const char *delref = NULL;
3387 /* TODO: Add -s option for adding symbolic references. */
3388 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3394 repo_path = realpath(optarg, NULL);
3395 if (repo_path == NULL)
3396 return got_error_from_errno2("realpath",
3398 got_path_strip_trailing_slashes(repo_path);
3412 if (do_list && delref)
3413 errx(1, "-l and -d options are mutually exclusive\n");
3418 if (do_list || delref) {
3420 errx(1, "-s option cannot be used together with the "
3421 "-l or -d options");
3424 } else if (argc != 2)
3429 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3433 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3434 "sendfd unveil", NULL) == -1)
3438 cwd = getcwd(NULL, 0);
3440 error = got_error_from_errno("getcwd");
3444 if (repo_path == NULL) {
3445 error = got_worktree_open(&worktree, cwd);
3446 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3452 strdup(got_worktree_get_repo_path(worktree));
3453 if (repo_path == NULL)
3454 error = got_error_from_errno("strdup");
3458 repo_path = strdup(cwd);
3459 if (repo_path == NULL) {
3460 error = got_error_from_errno("strdup");
3466 error = got_repo_open(&repo, repo_path, NULL);
3470 error = apply_unveil(got_repo_get_path(repo), do_list,
3471 worktree ? got_worktree_get_root_path(worktree) : NULL);
3476 error = list_refs(repo);
3478 error = delete_ref(repo, delref);
3479 else if (create_symref)
3480 error = add_symref(repo, argv[0], argv[1]);
3482 error = add_ref(repo, argv[0], argv[1]);
3485 got_repo_close(repo);
3487 got_worktree_close(worktree);
3497 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3498 "[name]\n", getprogname());
3502 static const struct got_error *
3503 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3504 struct got_reference *ref)
3506 const struct got_error *err = NULL;
3507 const char *refname, *marker = " ";
3510 refname = got_ref_get_name(ref);
3511 if (worktree && strcmp(refname,
3512 got_worktree_get_head_ref_name(worktree)) == 0) {
3513 struct got_object_id *id = NULL;
3515 err = got_ref_resolve(&id, repo, ref);
3518 if (got_object_id_cmp(id,
3519 got_worktree_get_base_commit_id(worktree)) == 0)
3526 if (strncmp(refname, "refs/heads/", 11) == 0)
3528 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3531 refstr = got_ref_to_str(ref);
3533 return got_error_from_errno("got_ref_to_str");
3535 printf("%s%s: %s\n", marker, refname, refstr);
3540 static const struct got_error *
3541 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3543 const char *refname;
3545 if (worktree == NULL)
3546 return got_error(GOT_ERR_NOT_WORKTREE);
3548 refname = got_worktree_get_head_ref_name(worktree);
3550 if (strncmp(refname, "refs/heads/", 11) == 0)
3552 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3555 printf("%s\n", refname);
3560 static const struct got_error *
3561 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3563 static const struct got_error *err = NULL;
3564 struct got_reflist_head refs;
3565 struct got_reflist_entry *re;
3566 struct got_reference *temp_ref = NULL;
3567 int rebase_in_progress, histedit_in_progress;
3569 SIMPLEQ_INIT(&refs);
3572 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3577 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3582 if (rebase_in_progress || histedit_in_progress) {
3583 err = got_ref_open(&temp_ref, repo,
3584 got_worktree_get_head_ref_name(worktree), 0);
3587 list_branch(repo, worktree, temp_ref);
3588 got_ref_close(temp_ref);
3592 err = got_ref_list(&refs, repo, "refs/heads",
3593 got_ref_cmp_by_name, NULL);
3597 SIMPLEQ_FOREACH(re, &refs, entry)
3598 list_branch(repo, worktree, re->ref);
3600 got_ref_list_free(&refs);
3604 static const struct got_error *
3605 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3606 const char *branch_name)
3608 const struct got_error *err = NULL;
3609 struct got_reference *ref = NULL;
3612 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3613 return got_error_from_errno("asprintf");
3615 err = got_ref_open(&ref, repo, refname, 0);
3620 strcmp(got_worktree_get_head_ref_name(worktree),
3621 got_ref_get_name(ref)) == 0) {
3622 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3623 "will not delete this work tree's current branch");
3627 err = got_ref_delete(ref, repo);
3635 static const struct got_error *
3636 add_branch(struct got_repository *repo, const char *branch_name,
3637 struct got_object_id *base_commit_id)
3639 const struct got_error *err = NULL;
3640 struct got_reference *ref = NULL;
3641 char *base_refname = NULL, *refname = NULL;
3644 * Don't let the user create a branch name with a leading '-'.
3645 * While technically a valid reference name, this case is usually
3646 * an unintended typo.
3648 if (branch_name[0] == '-')
3649 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3651 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3652 err = got_error_from_errno("asprintf");
3656 err = got_ref_open(&ref, repo, refname, 0);
3658 err = got_error(GOT_ERR_BRANCH_EXISTS);
3660 } else if (err->code != GOT_ERR_NOT_REF)
3663 err = got_ref_alloc(&ref, refname, base_commit_id);
3667 err = got_ref_write(ref, repo);
3676 static const struct got_error *
3677 cmd_branch(int argc, char *argv[])
3679 const struct got_error *error = NULL;
3680 struct got_repository *repo = NULL;
3681 struct got_worktree *worktree = NULL;
3682 char *cwd = NULL, *repo_path = NULL;
3683 int ch, do_list = 0, do_show = 0, do_update = 1;
3684 const char *delref = NULL, *commit_id_arg = NULL;
3685 struct got_reference *ref = NULL;
3686 struct got_pathlist_head paths;
3687 struct got_pathlist_entry *pe;
3688 struct got_object_id *commit_id = NULL;
3689 char *commit_id_str = NULL;
3693 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3696 commit_id_arg = optarg;
3702 repo_path = realpath(optarg, NULL);
3703 if (repo_path == NULL)
3704 return got_error_from_errno2("realpath",
3706 got_path_strip_trailing_slashes(repo_path);
3720 if (do_list && delref)
3721 errx(1, "-l and -d options are mutually exclusive\n");
3726 if (!do_list && !delref && argc == 0)
3729 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3730 errx(1, "-c option can only be used when creating a branch");
3732 if (do_list || delref) {
3735 } else if (!do_show && argc != 1)
3739 if (do_list || do_show) {
3740 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3744 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3745 "sendfd unveil", NULL) == -1)
3749 cwd = getcwd(NULL, 0);
3751 error = got_error_from_errno("getcwd");
3755 if (repo_path == NULL) {
3756 error = got_worktree_open(&worktree, cwd);
3757 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3763 strdup(got_worktree_get_repo_path(worktree));
3764 if (repo_path == NULL)
3765 error = got_error_from_errno("strdup");
3769 repo_path = strdup(cwd);
3770 if (repo_path == NULL) {
3771 error = got_error_from_errno("strdup");
3777 error = got_repo_open(&repo, repo_path, NULL);
3781 error = apply_unveil(got_repo_get_path(repo), do_list,
3782 worktree ? got_worktree_get_root_path(worktree) : NULL);
3787 error = show_current_branch(repo, worktree);
3789 error = list_branches(repo, worktree);
3791 error = delete_branch(repo, worktree, delref);
3793 if (commit_id_arg == NULL)
3794 commit_id_arg = worktree ?
3795 got_worktree_get_head_ref_name(worktree) :
3797 error = got_repo_match_object_id(&commit_id, NULL,
3798 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3801 error = add_branch(repo, argv[0], commit_id);
3804 if (worktree && do_update) {
3805 int did_something = 0;
3806 char *branch_refname = NULL;
3808 error = got_object_id_str(&commit_id_str, commit_id);
3811 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3815 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3817 error = got_error_from_errno("asprintf");
3820 error = got_ref_open(&ref, repo, branch_refname, 0);
3821 free(branch_refname);
3824 error = switch_head_ref(ref, commit_id, worktree,
3828 error = got_worktree_set_base_commit_id(worktree, repo,
3832 error = got_worktree_checkout_files(worktree, &paths,
3833 repo, update_progress, &did_something,
3834 check_cancelled, NULL);
3838 printf("Updated to commit %s\n", commit_id_str);
3845 got_repo_close(repo);
3847 got_worktree_close(worktree);
3851 free(commit_id_str);
3852 TAILQ_FOREACH(pe, &paths, entry)
3853 free((char *)pe->path);
3854 got_pathlist_free(&paths);
3863 "usage: %s tag [-c commit] [-r repository] [-l] "
3864 "[-m message] name\n", getprogname());
3869 static const struct got_error *
3870 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3872 const struct got_error *err = NULL;
3873 struct got_reflist_entry *re, *se, *new;
3874 struct got_object_id *re_id, *se_id;
3875 struct got_tag_object *re_tag, *se_tag;
3876 time_t re_time, se_time;
3878 SIMPLEQ_FOREACH(re, tags, entry) {
3879 se = SIMPLEQ_FIRST(sorted);
3881 err = got_reflist_entry_dup(&new, re);
3884 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3887 err = got_ref_resolve(&re_id, repo, re->ref);
3890 err = got_object_open_as_tag(&re_tag, repo, re_id);
3894 re_time = got_object_tag_get_tagger_time(re_tag);
3895 got_object_tag_close(re_tag);
3899 err = got_ref_resolve(&se_id, repo, re->ref);
3902 err = got_object_open_as_tag(&se_tag, repo, se_id);
3906 se_time = got_object_tag_get_tagger_time(se_tag);
3907 got_object_tag_close(se_tag);
3909 if (se_time > re_time) {
3910 err = got_reflist_entry_dup(&new, re);
3913 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3916 se = SIMPLEQ_NEXT(se, entry);
3925 static const struct got_error *
3926 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3928 static const struct got_error *err = NULL;
3929 struct got_reflist_head refs;
3930 struct got_reflist_entry *re;
3932 SIMPLEQ_INIT(&refs);
3934 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3938 SIMPLEQ_FOREACH(re, &refs, entry) {
3939 const char *refname;
3940 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3944 struct got_object_id *id;
3945 struct got_tag_object *tag;
3946 struct got_commit_object *commit = NULL;
3948 refname = got_ref_get_name(re->ref);
3949 if (strncmp(refname, "refs/tags/", 10) != 0)
3952 refstr = got_ref_to_str(re->ref);
3953 if (refstr == NULL) {
3954 err = got_error_from_errno("got_ref_to_str");
3957 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3960 err = got_ref_resolve(&id, repo, re->ref);
3963 err = got_object_open_as_tag(&tag, repo, id);
3965 if (err->code != GOT_ERR_OBJ_TYPE) {
3969 /* "lightweight" tag */
3970 err = got_object_open_as_commit(&commit, repo, id);
3975 tagger = got_object_commit_get_committer(commit);
3977 got_object_commit_get_committer_time(commit);
3978 err = got_object_id_str(&id_str, id);
3984 tagger = got_object_tag_get_tagger(tag);
3985 tagger_time = got_object_tag_get_tagger_time(tag);
3986 err = got_object_id_str(&id_str,
3987 got_object_tag_get_object_id(tag));
3991 printf("from: %s\n", tagger);
3992 datestr = get_datestr(&tagger_time, datebuf);
3994 printf("date: %s UTC\n", datestr);
3996 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3998 switch (got_object_tag_get_object_type(tag)) {
3999 case GOT_OBJ_TYPE_BLOB:
4000 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4003 case GOT_OBJ_TYPE_TREE:
4004 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4007 case GOT_OBJ_TYPE_COMMIT:
4008 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4011 case GOT_OBJ_TYPE_TAG:
4012 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4021 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4024 got_object_commit_close(commit);
4026 tagmsg0 = strdup(got_object_tag_get_message(tag));
4027 got_object_tag_close(tag);
4028 if (tagmsg0 == NULL) {
4029 err = got_error_from_errno("strdup");
4036 line = strsep(&tagmsg, "\n");
4038 printf(" %s\n", line);
4043 got_ref_list_free(&refs);
4047 static const struct got_error *
4048 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4049 const char *tag_name, const char *repo_path)
4051 const struct got_error *err = NULL;
4052 char *template = NULL, *initial_content = NULL;
4053 char *editor = NULL;
4056 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4057 err = got_error_from_errno("asprintf");
4061 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4062 commit_id_str, tag_name) == -1) {
4063 err = got_error_from_errno("asprintf");
4067 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4071 dprintf(fd, initial_content);
4074 err = get_editor(&editor);
4077 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4079 free(initial_content);
4083 /* Editor is done; we can now apply unveil(2) */
4085 err = apply_unveil(repo_path, 0, NULL);
4094 static const struct got_error *
4095 add_tag(struct got_repository *repo, const char *tag_name,
4096 const char *commit_arg, const char *tagmsg_arg)
4098 const struct got_error *err = NULL;
4099 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4100 char *label = NULL, *commit_id_str = NULL;
4101 struct got_reference *ref = NULL;
4102 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4103 char *tagmsg_path = NULL, *tag_id_str = NULL;
4104 int preserve_tagmsg = 0;
4107 * Don't let the user create a tag name with a leading '-'.
4108 * While technically a valid reference name, this case is usually
4109 * an unintended typo.
4111 if (tag_name[0] == '-')
4112 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4114 err = get_author(&tagger, repo);
4118 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4119 GOT_OBJ_TYPE_COMMIT, 1, repo);
4123 err = got_object_id_str(&commit_id_str, commit_id);
4127 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4128 refname = strdup(tag_name);
4129 if (refname == NULL) {
4130 err = got_error_from_errno("strdup");
4134 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4135 err = got_error_from_errno("asprintf");
4139 err = got_ref_open(&ref, repo, refname, 0);
4141 err = got_error(GOT_ERR_TAG_EXISTS);
4143 } else if (err->code != GOT_ERR_NOT_REF)
4146 if (tagmsg_arg == NULL) {
4147 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4148 tag_name, got_repo_get_path(repo));
4150 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4151 tagmsg_path != NULL)
4152 preserve_tagmsg = 1;
4157 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4158 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4161 preserve_tagmsg = 1;
4165 err = got_ref_alloc(&ref, refname, tag_id);
4168 preserve_tagmsg = 1;
4172 err = got_ref_write(ref, repo);
4175 preserve_tagmsg = 1;
4179 err = got_object_id_str(&tag_id_str, tag_id);
4182 preserve_tagmsg = 1;
4185 printf("Created tag %s\n", tag_id_str);
4187 if (preserve_tagmsg) {
4188 fprintf(stderr, "%s: tag message preserved in %s\n",
4189 getprogname(), tagmsg_path);
4190 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4191 err = got_error_from_errno2("unlink", tagmsg_path);
4196 free(commit_id_str);
4204 static const struct got_error *
4205 cmd_tag(int argc, char *argv[])
4207 const struct got_error *error = NULL;
4208 struct got_repository *repo = NULL;
4209 struct got_worktree *worktree = NULL;
4210 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4211 char *gitconfig_path = NULL;
4212 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4213 int ch, do_list = 0;
4215 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4218 commit_id_arg = optarg;
4224 repo_path = realpath(optarg, NULL);
4225 if (repo_path == NULL)
4226 return got_error_from_errno2("realpath",
4228 got_path_strip_trailing_slashes(repo_path);
4243 if (commit_id_arg != NULL)
4244 errx(1, "-c option can only be used when creating a tag");
4246 errx(1, "-l and -m options are mutually exclusive");
4249 } else if (argc != 1)
4256 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4260 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 "sendfd unveil", NULL) == -1)
4265 cwd = getcwd(NULL, 0);
4267 error = got_error_from_errno("getcwd");
4271 if (repo_path == NULL) {
4272 error = got_worktree_open(&worktree, cwd);
4273 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4279 strdup(got_worktree_get_repo_path(worktree));
4280 if (repo_path == NULL)
4281 error = got_error_from_errno("strdup");
4285 repo_path = strdup(cwd);
4286 if (repo_path == NULL) {
4287 error = got_error_from_errno("strdup");
4294 error = got_repo_open(&repo, repo_path, NULL);
4297 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4300 error = list_tags(repo, worktree);
4302 error = get_gitconfig_path(&gitconfig_path);
4305 error = got_repo_open(&repo, repo_path, gitconfig_path);
4310 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4315 if (commit_id_arg == NULL) {
4316 struct got_reference *head_ref;
4317 struct got_object_id *commit_id;
4318 error = got_ref_open(&head_ref, repo,
4319 worktree ? got_worktree_get_head_ref_name(worktree)
4323 error = got_ref_resolve(&commit_id, repo, head_ref);
4324 got_ref_close(head_ref);
4327 error = got_object_id_str(&commit_id_str, commit_id);
4333 error = add_tag(repo, tag_name,
4334 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4338 got_repo_close(repo);
4340 got_worktree_close(worktree);
4343 free(gitconfig_path);
4344 free(commit_id_str);
4351 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4356 static const struct got_error *
4357 add_progress(void *arg, unsigned char status, const char *path)
4359 while (path[0] == '/')
4361 printf("%c %s\n", status, path);
4365 static const struct got_error *
4366 cmd_add(int argc, char *argv[])
4368 const struct got_error *error = NULL;
4369 struct got_repository *repo = NULL;
4370 struct got_worktree *worktree = NULL;
4372 struct got_pathlist_head paths;
4373 struct got_pathlist_entry *pe;
4374 int ch, can_recurse = 0, no_ignores = 0;
4378 while ((ch = getopt(argc, argv, "IR")) != -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");
4409 error = got_worktree_open(&worktree, cwd);
4413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4418 error = apply_unveil(got_repo_get_path(repo), 1,
4419 got_worktree_get_root_path(worktree));
4423 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4427 if (!can_recurse && no_ignores) {
4428 error = got_error_msg(GOT_ERR_BAD_PATH,
4429 "disregarding ignores requires -R option");
4437 TAILQ_FOREACH(pe, &paths, entry) {
4438 if (asprintf(&ondisk_path, "%s/%s",
4439 got_worktree_get_root_path(worktree),
4441 error = got_error_from_errno("asprintf");
4444 if (lstat(ondisk_path, &sb) == -1) {
4445 if (errno == ENOENT) {
4449 error = got_error_from_errno2("lstat",
4455 if (S_ISDIR(sb.st_mode)) {
4456 error = got_error_msg(GOT_ERR_BAD_PATH,
4457 "adding directories requires -R option");
4463 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4464 NULL, repo, no_ignores);
4467 got_repo_close(repo);
4469 got_worktree_close(worktree);
4470 TAILQ_FOREACH(pe, &paths, entry)
4471 free((char *)pe->path);
4472 got_pathlist_free(&paths);
4480 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4485 static const struct got_error *
4486 print_remove_status(void *arg, unsigned char status,
4487 unsigned char staged_status, const char *path)
4489 while (path[0] == '/')
4491 if (status == GOT_STATUS_NONEXISTENT)
4493 if (status == staged_status && (status == GOT_STATUS_DELETE))
4494 status = GOT_STATUS_NO_CHANGE;
4495 printf("%c%c %s\n", status, staged_status, path);
4499 static const struct got_error *
4500 cmd_remove(int argc, char *argv[])
4502 const struct got_error *error = NULL;
4503 struct got_worktree *worktree = NULL;
4504 struct got_repository *repo = NULL;
4506 struct got_pathlist_head paths;
4507 struct got_pathlist_entry *pe;
4508 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4512 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4515 delete_local_mods = 1;
4533 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4540 cwd = getcwd(NULL, 0);
4542 error = got_error_from_errno("getcwd");
4545 error = got_worktree_open(&worktree, cwd);
4549 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4554 error = apply_unveil(got_repo_get_path(repo), 1,
4555 got_worktree_get_root_path(worktree));
4559 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4566 TAILQ_FOREACH(pe, &paths, entry) {
4567 if (asprintf(&ondisk_path, "%s/%s",
4568 got_worktree_get_root_path(worktree),
4570 error = got_error_from_errno("asprintf");
4573 if (lstat(ondisk_path, &sb) == -1) {
4574 if (errno == ENOENT) {
4578 error = got_error_from_errno2("lstat",
4584 if (S_ISDIR(sb.st_mode)) {
4585 error = got_error_msg(GOT_ERR_BAD_PATH,
4586 "removing directories requires -R option");
4592 error = got_worktree_schedule_delete(worktree, &paths,
4593 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4596 got_repo_close(repo);
4598 got_worktree_close(worktree);
4599 TAILQ_FOREACH(pe, &paths, entry)
4600 free((char *)pe->path);
4601 got_pathlist_free(&paths);
4609 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4610 "path ...\n", getprogname());
4614 static const struct got_error *
4615 revert_progress(void *arg, unsigned char status, const char *path)
4617 if (status == GOT_STATUS_UNVERSIONED)
4620 while (path[0] == '/')
4622 printf("%c %s\n", status, path);
4626 struct choose_patch_arg {
4627 FILE *patch_script_file;
4631 static const struct got_error *
4632 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4633 int nchanges, const char *action)
4636 size_t linesize = 0;
4640 case GOT_STATUS_ADD:
4641 printf("A %s\n%s this addition? [y/n] ", path, action);
4643 case GOT_STATUS_DELETE:
4644 printf("D %s\n%s this deletion? [y/n] ", path, action);
4646 case GOT_STATUS_MODIFY:
4647 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4648 return got_error_from_errno("fseek");
4649 printf(GOT_COMMIT_SEP_STR);
4650 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4652 if (ferror(patch_file))
4653 return got_error_from_errno("getline");
4654 printf(GOT_COMMIT_SEP_STR);
4655 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4656 path, n, nchanges, action);
4659 return got_error_path(path, GOT_ERR_FILE_STATUS);
4665 static const struct got_error *
4666 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4667 FILE *patch_file, int n, int nchanges)
4669 const struct got_error *err = NULL;
4671 size_t linesize = 0;
4674 struct choose_patch_arg *a = arg;
4676 *choice = GOT_PATCH_CHOICE_NONE;
4678 if (a->patch_script_file) {
4680 err = show_change(status, path, patch_file, n, nchanges,
4684 linelen = getline(&line, &linesize, a->patch_script_file);
4685 if (linelen == -1) {
4686 if (ferror(a->patch_script_file))
4687 return got_error_from_errno("getline");
4690 nl = strchr(line, '\n');
4693 if (strcmp(line, "y") == 0) {
4694 *choice = GOT_PATCH_CHOICE_YES;
4696 } else if (strcmp(line, "n") == 0) {
4697 *choice = GOT_PATCH_CHOICE_NO;
4699 } else if (strcmp(line, "q") == 0 &&
4700 status == GOT_STATUS_MODIFY) {
4701 *choice = GOT_PATCH_CHOICE_QUIT;
4704 printf("invalid response '%s'\n", line);
4709 while (resp != 'y' && resp != 'n' && resp != 'q') {
4710 err = show_change(status, path, patch_file, n, nchanges,
4717 if (status == GOT_STATUS_MODIFY) {
4718 if (resp != 'y' && resp != 'n' && resp != 'q') {
4719 printf("invalid response '%c'\n", resp);
4722 } else if (resp != 'y' && resp != 'n') {
4723 printf("invalid response '%c'\n", resp);
4729 *choice = GOT_PATCH_CHOICE_YES;
4730 else if (resp == 'n')
4731 *choice = GOT_PATCH_CHOICE_NO;
4732 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4733 *choice = GOT_PATCH_CHOICE_QUIT;
4739 static const struct got_error *
4740 cmd_revert(int argc, char *argv[])
4742 const struct got_error *error = NULL;
4743 struct got_worktree *worktree = NULL;
4744 struct got_repository *repo = NULL;
4745 char *cwd = NULL, *path = NULL;
4746 struct got_pathlist_head paths;
4747 struct got_pathlist_entry *pe;
4748 int ch, can_recurse = 0, pflag = 0;
4749 FILE *patch_script_file = NULL;
4750 const char *patch_script_path = NULL;
4751 struct choose_patch_arg cpa;
4755 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4761 patch_script_path = optarg;
4776 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4777 "unveil", NULL) == -1)
4782 if (patch_script_path && !pflag)
4783 errx(1, "-F option can only be used together with -p option");
4785 cwd = getcwd(NULL, 0);
4787 error = got_error_from_errno("getcwd");
4790 error = got_worktree_open(&worktree, cwd);
4794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4799 if (patch_script_path) {
4800 patch_script_file = fopen(patch_script_path, "r");
4801 if (patch_script_file == NULL) {
4802 error = got_error_from_errno2("fopen",
4807 error = apply_unveil(got_repo_get_path(repo), 1,
4808 got_worktree_get_root_path(worktree));
4812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4819 TAILQ_FOREACH(pe, &paths, entry) {
4820 if (asprintf(&ondisk_path, "%s/%s",
4821 got_worktree_get_root_path(worktree),
4823 error = got_error_from_errno("asprintf");
4826 if (lstat(ondisk_path, &sb) == -1) {
4827 if (errno == ENOENT) {
4831 error = got_error_from_errno2("lstat",
4837 if (S_ISDIR(sb.st_mode)) {
4838 error = got_error_msg(GOT_ERR_BAD_PATH,
4839 "reverting directories requires -R option");
4845 cpa.patch_script_file = patch_script_file;
4846 cpa.action = "revert";
4847 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4848 pflag ? choose_patch : NULL, &cpa, repo);
4850 if (patch_script_file && fclose(patch_script_file) == EOF &&
4852 error = got_error_from_errno2("fclose", patch_script_path);
4854 got_repo_close(repo);
4856 got_worktree_close(worktree);
4865 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4870 struct collect_commit_logmsg_arg {
4871 const char *cmdline_log;
4873 const char *worktree_path;
4874 const char *branch_name;
4875 const char *repo_path;
4880 static const struct got_error *
4881 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4884 char *initial_content = NULL;
4885 struct got_pathlist_entry *pe;
4886 const struct got_error *err = NULL;
4887 char *template = NULL;
4888 struct collect_commit_logmsg_arg *a = arg;
4892 /* if a message was specified on the command line, just use it */
4893 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4894 len = strlen(a->cmdline_log) + 1;
4895 *logmsg = malloc(len + 1);
4896 if (*logmsg == NULL)
4897 return got_error_from_errno("malloc");
4898 strlcpy(*logmsg, a->cmdline_log, len);
4902 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4903 return got_error_from_errno("asprintf");
4905 if (asprintf(&initial_content,
4906 "\n# changes to be committed on branch %s:\n",
4907 a->branch_name) == -1)
4908 return got_error_from_errno("asprintf");
4910 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4914 dprintf(fd, initial_content);
4916 TAILQ_FOREACH(pe, commitable_paths, entry) {
4917 struct got_commitable *ct = pe->data;
4918 dprintf(fd, "# %c %s\n",
4919 got_commitable_get_status(ct),
4920 got_commitable_get_path(ct));
4924 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4926 free(initial_content);
4929 /* Editor is done; we can now apply unveil(2) */
4931 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4940 static const struct got_error *
4941 cmd_commit(int argc, char *argv[])
4943 const struct got_error *error = NULL;
4944 struct got_worktree *worktree = NULL;
4945 struct got_repository *repo = NULL;
4946 char *cwd = NULL, *id_str = NULL;
4947 struct got_object_id *id = NULL;
4948 const char *logmsg = NULL;
4949 struct collect_commit_logmsg_arg cl_arg;
4950 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4951 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4952 struct got_pathlist_head paths;
4955 cl_arg.logmsg_path = NULL;
4957 while ((ch = getopt(argc, argv, "m:")) != -1) {
4972 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4973 "unveil", NULL) == -1)
4976 cwd = getcwd(NULL, 0);
4978 error = got_error_from_errno("getcwd");
4981 error = got_worktree_open(&worktree, cwd);
4985 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4988 if (rebase_in_progress) {
4989 error = got_error(GOT_ERR_REBASING);
4993 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4998 error = get_gitconfig_path(&gitconfig_path);
5001 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5006 error = get_author(&author, repo);
5011 * unveil(2) traverses exec(2); if an editor is used we have
5012 * to apply unveil after the log message has been written.
5014 if (logmsg == NULL || strlen(logmsg) == 0)
5015 error = get_editor(&editor);
5017 error = apply_unveil(got_repo_get_path(repo), 0,
5018 got_worktree_get_root_path(worktree));
5022 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5026 cl_arg.editor = editor;
5027 cl_arg.cmdline_log = logmsg;
5028 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5029 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5030 if (!histedit_in_progress) {
5031 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5032 error = got_error(GOT_ERR_COMMIT_BRANCH);
5035 cl_arg.branch_name += 11;
5037 cl_arg.repo_path = got_repo_get_path(repo);
5038 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5039 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5041 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5042 cl_arg.logmsg_path != NULL)
5043 preserve_logmsg = 1;
5047 error = got_object_id_str(&id_str, id);
5050 printf("Created commit %s\n", id_str);
5052 if (preserve_logmsg) {
5053 fprintf(stderr, "%s: log message preserved in %s\n",
5054 getprogname(), cl_arg.logmsg_path);
5055 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5057 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5058 free(cl_arg.logmsg_path);
5060 got_repo_close(repo);
5062 got_worktree_close(worktree);
5065 free(gitconfig_path);
5072 usage_cherrypick(void)
5074 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5078 static const struct got_error *
5079 cmd_cherrypick(int argc, char *argv[])
5081 const struct got_error *error = NULL;
5082 struct got_worktree *worktree = NULL;
5083 struct got_repository *repo = NULL;
5084 char *cwd = NULL, *commit_id_str = NULL;
5085 struct got_object_id *commit_id = NULL;
5086 struct got_commit_object *commit = NULL;
5087 struct got_object_qid *pid;
5088 struct got_reference *head_ref = NULL;
5089 int ch, did_something = 0;
5091 while ((ch = getopt(argc, argv, "")) != -1) {
5103 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 "unveil", NULL) == -1)
5110 cwd = getcwd(NULL, 0);
5112 error = got_error_from_errno("getcwd");
5115 error = got_worktree_open(&worktree, cwd);
5119 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5124 error = apply_unveil(got_repo_get_path(repo), 0,
5125 got_worktree_get_root_path(worktree));
5129 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 GOT_OBJ_TYPE_COMMIT, repo);
5131 if (error != NULL) {
5132 struct got_reference *ref;
5133 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5135 error = got_ref_open(&ref, repo, argv[0], 0);
5138 error = got_ref_resolve(&commit_id, repo, ref);
5143 error = got_object_id_str(&commit_id_str, commit_id);
5147 error = got_ref_open(&head_ref, repo,
5148 got_worktree_get_head_ref_name(worktree), 0);
5152 error = check_same_branch(commit_id, head_ref, NULL, repo);
5154 if (error->code != GOT_ERR_ANCESTRY)
5158 error = got_error(GOT_ERR_SAME_BRANCH);
5162 error = got_object_open_as_commit(&commit, repo, commit_id);
5165 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5166 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5167 commit_id, repo, update_progress, &did_something, check_cancelled,
5173 printf("Merged commit %s\n", commit_id_str);
5176 got_object_commit_close(commit);
5177 free(commit_id_str);
5179 got_ref_close(head_ref);
5181 got_worktree_close(worktree);
5183 got_repo_close(repo);
5190 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5194 static const struct got_error *
5195 cmd_backout(int argc, char *argv[])
5197 const struct got_error *error = NULL;
5198 struct got_worktree *worktree = NULL;
5199 struct got_repository *repo = NULL;
5200 char *cwd = NULL, *commit_id_str = NULL;
5201 struct got_object_id *commit_id = NULL;
5202 struct got_commit_object *commit = NULL;
5203 struct got_object_qid *pid;
5204 struct got_reference *head_ref = NULL;
5205 int ch, did_something = 0;
5207 while ((ch = getopt(argc, argv, "")) != -1) {
5219 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5220 "unveil", NULL) == -1)
5226 cwd = getcwd(NULL, 0);
5228 error = got_error_from_errno("getcwd");
5231 error = got_worktree_open(&worktree, cwd);
5235 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5240 error = apply_unveil(got_repo_get_path(repo), 0,
5241 got_worktree_get_root_path(worktree));
5245 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5246 GOT_OBJ_TYPE_COMMIT, repo);
5247 if (error != NULL) {
5248 struct got_reference *ref;
5249 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5251 error = got_ref_open(&ref, repo, argv[0], 0);
5254 error = got_ref_resolve(&commit_id, repo, ref);
5259 error = got_object_id_str(&commit_id_str, commit_id);
5263 error = got_ref_open(&head_ref, repo,
5264 got_worktree_get_head_ref_name(worktree), 0);
5268 error = check_same_branch(commit_id, head_ref, NULL, repo);
5272 error = got_object_open_as_commit(&commit, repo, commit_id);
5275 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5277 error = got_error(GOT_ERR_ROOT_COMMIT);
5281 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5282 update_progress, &did_something, check_cancelled, NULL);
5287 printf("Backed out commit %s\n", commit_id_str);
5290 got_object_commit_close(commit);
5291 free(commit_id_str);
5293 got_ref_close(head_ref);
5295 got_worktree_close(worktree);
5297 got_repo_close(repo);
5304 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5310 trim_logmsg(char *logmsg, int limit)
5315 len = strlen(logmsg);
5319 nl = strchr(logmsg, '\n');
5324 static const struct got_error *
5325 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5327 const struct got_error *err;
5328 char *logmsg0 = NULL;
5331 err = got_object_commit_get_logmsg(&logmsg0, commit);
5336 while (isspace((unsigned char)s[0]))
5339 *logmsg = strdup(s);
5340 if (*logmsg == NULL) {
5341 err = got_error_from_errno("strdup");
5345 trim_logmsg(*logmsg, limit);
5351 static const struct got_error *
5352 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5354 const struct got_error *err;
5355 struct got_commit_object *commit = NULL;
5356 char *id_str = NULL, *logmsg = NULL;
5358 err = got_object_open_as_commit(&commit, repo, id);
5362 err = got_object_id_str(&id_str, id);
5368 err = get_short_logmsg(&logmsg, 42, commit);
5372 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5375 got_object_commit_close(commit);
5380 static const struct got_error *
5381 show_rebase_progress(struct got_commit_object *commit,
5382 struct got_object_id *old_id, struct got_object_id *new_id)
5384 const struct got_error *err;
5385 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5387 err = got_object_id_str(&old_id_str, old_id);
5392 err = got_object_id_str(&new_id_str, new_id);
5397 old_id_str[12] = '\0';
5399 new_id_str[12] = '\0';
5401 err = get_short_logmsg(&logmsg, 42, commit);
5405 printf("%s -> %s: %s\n", old_id_str,
5406 new_id_str ? new_id_str : "no-op change", logmsg);
5414 static const struct got_error *
5415 rebase_progress(void *arg, unsigned char status, const char *path)
5417 unsigned char *rebase_status = arg;
5419 while (path[0] == '/')
5421 printf("%c %s\n", status, path);
5423 if (*rebase_status == GOT_STATUS_CONFLICT)
5425 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5426 *rebase_status = status;
5430 static const struct got_error *
5431 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5432 struct got_reference *branch, struct got_reference *new_base_branch,
5433 struct got_reference *tmp_branch, struct got_repository *repo)
5435 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5436 return got_worktree_rebase_complete(worktree, fileindex,
5437 new_base_branch, tmp_branch, branch, repo);
5440 static const struct got_error *
5441 rebase_commit(struct got_pathlist_head *merged_paths,
5442 struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 struct got_reference *tmp_branch,
5444 struct got_object_id *commit_id, struct got_repository *repo)
5446 const struct got_error *error;
5447 struct got_commit_object *commit;
5448 struct got_object_id *new_commit_id;
5450 error = got_object_open_as_commit(&commit, repo, commit_id);
5454 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5455 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5457 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5459 error = show_rebase_progress(commit, commit_id, NULL);
5461 error = show_rebase_progress(commit, commit_id, new_commit_id);
5462 free(new_commit_id);
5465 got_object_commit_close(commit);
5469 struct check_path_prefix_arg {
5470 const char *path_prefix;
5475 static const struct got_error *
5476 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5477 struct got_blob_object *blob2, struct got_object_id *id1,
5478 struct got_object_id *id2, const char *path1, const char *path2,
5479 mode_t mode1, mode_t mode2, struct got_repository *repo)
5481 struct check_path_prefix_arg *a = arg;
5483 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5484 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5485 return got_error(a->errcode);
5490 static const struct got_error *
5491 check_path_prefix(struct got_object_id *parent_id,
5492 struct got_object_id *commit_id, const char *path_prefix,
5493 int errcode, struct got_repository *repo)
5495 const struct got_error *err;
5496 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5497 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5498 struct check_path_prefix_arg cpp_arg;
5500 if (got_path_is_root_dir(path_prefix))
5503 err = got_object_open_as_commit(&commit, repo, commit_id);
5507 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5511 err = got_object_open_as_tree(&tree1, repo,
5512 got_object_commit_get_tree_id(parent_commit));
5516 err = got_object_open_as_tree(&tree2, repo,
5517 got_object_commit_get_tree_id(commit));
5521 cpp_arg.path_prefix = path_prefix;
5522 while (cpp_arg.path_prefix[0] == '/')
5523 cpp_arg.path_prefix++;
5524 cpp_arg.len = strlen(cpp_arg.path_prefix);
5525 cpp_arg.errcode = errcode;
5526 err = got_diff_tree(tree1, tree2, "", "", repo,
5527 check_path_prefix_in_diff, &cpp_arg, 0);
5530 got_object_tree_close(tree1);
5532 got_object_tree_close(tree2);
5534 got_object_commit_close(commit);
5536 got_object_commit_close(parent_commit);
5540 static const struct got_error *
5541 collect_commits(struct got_object_id_queue *commits,
5542 struct got_object_id *initial_commit_id,
5543 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5544 const char *path_prefix, int path_prefix_errcode,
5545 struct got_repository *repo)
5547 const struct got_error *err = NULL;
5548 struct got_commit_graph *graph = NULL;
5549 struct got_object_id *parent_id = NULL;
5550 struct got_object_qid *qid;
5551 struct got_object_id *commit_id = initial_commit_id;
5553 err = got_commit_graph_open(&graph, "/", 1);
5557 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5558 check_cancelled, NULL);
5561 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5562 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5563 check_cancelled, NULL);
5565 if (err->code == GOT_ERR_ITER_COMPLETED) {
5566 err = got_error_msg(GOT_ERR_ANCESTRY,
5567 "ran out of commits to rebase before "
5568 "youngest common ancestor commit has "
5573 err = check_path_prefix(parent_id, commit_id,
5574 path_prefix, path_prefix_errcode, repo);
5578 err = got_object_qid_alloc(&qid, commit_id);
5581 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5582 commit_id = parent_id;
5586 got_commit_graph_close(graph);
5590 static const struct got_error *
5591 cmd_rebase(int argc, char *argv[])
5593 const struct got_error *error = NULL;
5594 struct got_worktree *worktree = NULL;
5595 struct got_repository *repo = NULL;
5596 struct got_fileindex *fileindex = NULL;
5598 struct got_reference *branch = NULL;
5599 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5600 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5601 struct got_object_id *resume_commit_id = NULL;
5602 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5603 struct got_commit_object *commit = NULL;
5604 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5605 int histedit_in_progress = 0;
5606 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5607 struct got_object_id_queue commits;
5608 struct got_pathlist_head merged_paths;
5609 const struct got_object_id_queue *parent_ids;
5610 struct got_object_qid *qid, *pid;
5612 SIMPLEQ_INIT(&commits);
5613 TAILQ_INIT(&merged_paths);
5615 while ((ch = getopt(argc, argv, "ac")) != -1) {
5621 continue_rebase = 1;
5633 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5634 "unveil", NULL) == -1)
5637 if (abort_rebase && continue_rebase)
5639 else if (abort_rebase || continue_rebase) {
5642 } else if (argc != 1)
5645 cwd = getcwd(NULL, 0);
5647 error = got_error_from_errno("getcwd");
5650 error = got_worktree_open(&worktree, cwd);
5654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5659 error = apply_unveil(got_repo_get_path(repo), 0,
5660 got_worktree_get_root_path(worktree));
5664 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5668 if (histedit_in_progress) {
5669 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5673 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5679 if (!rebase_in_progress) {
5680 error = got_error(GOT_ERR_NOT_REBASING);
5683 error = got_worktree_rebase_continue(&resume_commit_id,
5684 &new_base_branch, &tmp_branch, &branch, &fileindex,
5688 printf("Switching work tree to %s\n",
5689 got_ref_get_symref_target(new_base_branch));
5690 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5691 new_base_branch, update_progress, &did_something);
5694 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5695 goto done; /* nothing else to do */
5698 if (continue_rebase) {
5699 if (!rebase_in_progress) {
5700 error = got_error(GOT_ERR_NOT_REBASING);
5703 error = got_worktree_rebase_continue(&resume_commit_id,
5704 &new_base_branch, &tmp_branch, &branch, &fileindex,
5709 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5710 resume_commit_id, repo);
5714 yca_id = got_object_id_dup(resume_commit_id);
5715 if (yca_id == NULL) {
5716 error = got_error_from_errno("got_object_id_dup");
5720 error = got_ref_open(&branch, repo, argv[0], 0);
5725 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5729 if (!continue_rebase) {
5730 struct got_object_id *base_commit_id;
5732 base_commit_id = got_worktree_get_base_commit_id(worktree);
5733 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5734 base_commit_id, branch_head_commit_id, repo,
5735 check_cancelled, NULL);
5738 if (yca_id == NULL) {
5739 error = got_error_msg(GOT_ERR_ANCESTRY,
5740 "specified branch shares no common ancestry "
5741 "with work tree's branch");
5745 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5747 if (error->code != GOT_ERR_ANCESTRY)
5751 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5752 "specified branch resolves to a commit which "
5753 "is already contained in work tree's branch");
5756 error = got_worktree_rebase_prepare(&new_base_branch,
5757 &tmp_branch, &fileindex, worktree, branch, repo);
5762 commit_id = branch_head_commit_id;
5763 error = got_object_open_as_commit(&commit, repo, commit_id);
5767 parent_ids = got_object_commit_get_parent_ids(commit);
5768 pid = SIMPLEQ_FIRST(parent_ids);
5770 if (!continue_rebase) {
5772 error = got_worktree_rebase_abort(worktree, fileindex,
5773 repo, new_base_branch, update_progress,
5777 printf("Rebase of %s aborted\n",
5778 got_ref_get_name(branch));
5780 error = got_error(GOT_ERR_EMPTY_REBASE);
5783 error = collect_commits(&commits, commit_id, pid->id,
5784 yca_id, got_worktree_get_path_prefix(worktree),
5785 GOT_ERR_REBASE_PATH, repo);
5786 got_object_commit_close(commit);
5791 if (SIMPLEQ_EMPTY(&commits)) {
5792 if (continue_rebase) {
5793 error = rebase_complete(worktree, fileindex,
5794 branch, new_base_branch, tmp_branch, repo);
5797 /* Fast-forward the reference of the branch. */
5798 struct got_object_id *new_head_commit_id;
5800 error = got_ref_resolve(&new_head_commit_id, repo,
5804 error = got_object_id_str(&id_str, new_head_commit_id);
5805 printf("Forwarding %s to commit %s\n",
5806 got_ref_get_name(branch), id_str);
5808 error = got_ref_change_ref(branch,
5809 new_head_commit_id);
5816 SIMPLEQ_FOREACH(qid, &commits, entry) {
5817 commit_id = qid->id;
5818 parent_id = pid ? pid->id : yca_id;
5821 error = got_worktree_rebase_merge_files(&merged_paths,
5822 worktree, fileindex, parent_id, commit_id, repo,
5823 rebase_progress, &rebase_status, check_cancelled, NULL);
5827 if (rebase_status == GOT_STATUS_CONFLICT) {
5828 error = show_rebase_merge_conflict(qid->id, repo);
5831 got_worktree_rebase_pathlist_free(&merged_paths);
5835 error = rebase_commit(&merged_paths, worktree, fileindex,
5836 tmp_branch, commit_id, repo);
5837 got_worktree_rebase_pathlist_free(&merged_paths);
5842 if (rebase_status == GOT_STATUS_CONFLICT) {
5843 error = got_worktree_rebase_postpone(worktree, fileindex);
5846 error = got_error_msg(GOT_ERR_CONFLICTS,
5847 "conflicts must be resolved before rebasing can continue");
5849 error = rebase_complete(worktree, fileindex, branch,
5850 new_base_branch, tmp_branch, repo);
5852 got_object_id_queue_free(&commits);
5853 free(branch_head_commit_id);
5854 free(resume_commit_id);
5857 got_object_commit_close(commit);
5859 got_ref_close(branch);
5860 if (new_base_branch)
5861 got_ref_close(new_base_branch);
5863 got_ref_close(tmp_branch);
5865 got_worktree_close(worktree);
5867 got_repo_close(repo);
5872 usage_histedit(void)
5874 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5879 #define GOT_HISTEDIT_PICK 'p'
5880 #define GOT_HISTEDIT_EDIT 'e'
5881 #define GOT_HISTEDIT_FOLD 'f'
5882 #define GOT_HISTEDIT_DROP 'd'
5883 #define GOT_HISTEDIT_MESG 'm'
5885 static struct got_histedit_cmd {
5889 } got_histedit_cmds[] = {
5890 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5891 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5892 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5894 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5895 { GOT_HISTEDIT_MESG, "mesg",
5896 "single-line log message for commit above (open editor if empty)" },
5899 struct got_histedit_list_entry {
5900 TAILQ_ENTRY(got_histedit_list_entry) entry;
5901 struct got_object_id *commit_id;
5902 const struct got_histedit_cmd *cmd;
5905 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5907 static const struct got_error *
5908 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5909 FILE *f, struct got_repository *repo)
5911 const struct got_error *err = NULL;
5912 char *logmsg = NULL, *id_str = NULL;
5913 struct got_commit_object *commit = NULL;
5916 err = got_object_open_as_commit(&commit, repo, commit_id);
5920 err = get_short_logmsg(&logmsg, 34, commit);
5924 err = got_object_id_str(&id_str, commit_id);
5928 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5930 err = got_ferror(f, GOT_ERR_IO);
5933 got_object_commit_close(commit);
5939 static const struct got_error *
5940 histedit_write_commit_list(struct got_object_id_queue *commits,
5941 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5943 const struct got_error *err = NULL;
5944 struct got_object_qid *qid;
5946 if (SIMPLEQ_EMPTY(commits))
5947 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5949 SIMPLEQ_FOREACH(qid, commits, entry) {
5950 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5954 if (edit_logmsg_only) {
5955 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5957 err = got_ferror(f, GOT_ERR_IO);
5966 static const struct got_error *
5967 write_cmd_list(FILE *f, const char *branch_name,
5968 struct got_object_id_queue *commits)
5970 const struct got_error *err = NULL;
5973 struct got_object_qid *qid;
5975 qid = SIMPLEQ_FIRST(commits);
5976 err = got_object_id_str(&id_str, qid->id);
5981 "# Editing the history of branch '%s' starting at\n"
5983 "# Commits will be processed in order from top to "
5984 "bottom of this file.\n", branch_name, id_str);
5986 err = got_ferror(f, GOT_ERR_IO);
5990 n = fprintf(f, "# Available histedit commands:\n");
5992 err = got_ferror(f, GOT_ERR_IO);
5996 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5997 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5998 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6001 err = got_ferror(f, GOT_ERR_IO);
6010 static const struct got_error *
6011 histedit_syntax_error(int lineno)
6013 static char msg[42];
6016 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6018 if (ret == -1 || ret >= sizeof(msg))
6019 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6021 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6024 static const struct got_error *
6025 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6026 char *logmsg, struct got_repository *repo)
6028 const struct got_error *err;
6029 struct got_commit_object *folded_commit = NULL;
6030 char *id_str, *folded_logmsg = NULL;
6032 err = got_object_id_str(&id_str, hle->commit_id);
6036 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6040 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6043 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6044 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6045 folded_logmsg) == -1) {
6046 err = got_error_from_errno("asprintf");
6050 got_object_commit_close(folded_commit);
6052 free(folded_logmsg);
6056 static struct got_histedit_list_entry *
6057 get_folded_commits(struct got_histedit_list_entry *hle)
6059 struct got_histedit_list_entry *prev, *folded = NULL;
6061 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6062 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6063 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6064 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6066 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6072 static const struct got_error *
6073 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6074 struct got_repository *repo)
6076 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6077 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6078 const struct got_error *err = NULL;
6079 struct got_commit_object *commit = NULL;
6081 struct got_histedit_list_entry *folded = NULL;
6083 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6087 folded = get_folded_commits(hle);
6089 while (folded != hle) {
6090 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6091 folded = TAILQ_NEXT(folded, entry);
6094 err = append_folded_commit_msg(&new_msg, folded,
6100 folded = TAILQ_NEXT(folded, entry);
6104 err = got_object_id_str(&id_str, hle->commit_id);
6107 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6110 if (asprintf(&new_msg,
6111 "%s\n# original log message of commit %s: %s",
6112 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6113 err = got_error_from_errno("asprintf");
6119 err = got_object_id_str(&id_str, hle->commit_id);
6123 err = got_opentemp_named_fd(&logmsg_path, &fd,
6124 GOT_TMPDIR_STR "/got-logmsg");
6128 dprintf(fd, logmsg);
6131 err = get_editor(&editor);
6135 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6137 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6139 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6142 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6143 err = got_error_from_errno2("unlink", logmsg_path);
6149 got_object_commit_close(commit);
6153 static const struct got_error *
6154 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6155 FILE *f, struct got_repository *repo)
6157 const struct got_error *err = NULL;
6158 char *line = NULL, *p, *end;
6162 const struct got_histedit_cmd *cmd;
6163 struct got_object_id *commit_id = NULL;
6164 struct got_histedit_list_entry *hle = NULL;
6167 len = getline(&line, &size, f);
6169 const struct got_error *getline_err;
6172 getline_err = got_error_from_errno("getline");
6173 err = got_ferror(f, getline_err->code);
6178 while (isspace((unsigned char)p[0]))
6180 if (p[0] == '#' || p[0] == '\0') {
6186 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6187 cmd = &got_histedit_cmds[i];
6188 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6189 isspace((unsigned char)p[strlen(cmd->name)])) {
6190 p += strlen(cmd->name);
6193 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6198 if (i == nitems(got_histedit_cmds)) {
6199 err = histedit_syntax_error(lineno);
6202 while (isspace((unsigned char)p[0]))
6204 if (cmd->code == GOT_HISTEDIT_MESG) {
6205 if (hle == NULL || hle->logmsg != NULL) {
6206 err = got_error(GOT_ERR_HISTEDIT_CMD);
6210 err = histedit_edit_logmsg(hle, repo);
6214 hle->logmsg = strdup(p);
6215 if (hle->logmsg == NULL) {
6216 err = got_error_from_errno("strdup");
6225 while (end[0] && !isspace((unsigned char)end[0]))
6229 err = got_object_resolve_id_str(&commit_id, repo, p);
6231 /* override error code */
6232 err = histedit_syntax_error(lineno);
6236 hle = malloc(sizeof(*hle));
6238 err = got_error_from_errno("malloc");
6242 hle->commit_id = commit_id;
6247 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6255 static const struct got_error *
6256 histedit_check_script(struct got_histedit_list *histedit_cmds,
6257 struct got_object_id_queue *commits, struct got_repository *repo)
6259 const struct got_error *err = NULL;
6260 struct got_object_qid *qid;
6261 struct got_histedit_list_entry *hle;
6262 static char msg[92];
6265 if (TAILQ_EMPTY(histedit_cmds))
6266 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6267 "histedit script contains no commands");
6268 if (SIMPLEQ_EMPTY(commits))
6269 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6271 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6272 struct got_histedit_list_entry *hle2;
6273 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6276 if (got_object_id_cmp(hle->commit_id,
6277 hle2->commit_id) != 0)
6279 err = got_object_id_str(&id_str, hle->commit_id);
6282 snprintf(msg, sizeof(msg), "commit %s is listed "
6283 "more than once in histedit script", id_str);
6285 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6289 SIMPLEQ_FOREACH(qid, commits, entry) {
6290 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6291 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6295 err = got_object_id_str(&id_str, qid->id);
6298 snprintf(msg, sizeof(msg),
6299 "commit %s missing from histedit script", id_str);
6301 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6305 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6306 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6307 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6308 "last commit in histedit script cannot be folded");
6313 static const struct got_error *
6314 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6315 const char *path, struct got_object_id_queue *commits,
6316 struct got_repository *repo)
6318 const struct got_error *err = NULL;
6322 err = get_editor(&editor);
6326 if (spawn_editor(editor, path) == -1) {
6327 err = got_error_from_errno("failed spawning editor");
6331 f = fopen(path, "r");
6333 err = got_error_from_errno("fopen");
6336 err = histedit_parse_list(histedit_cmds, f, repo);
6340 err = histedit_check_script(histedit_cmds, commits, repo);
6342 if (f && fclose(f) != 0 && err == NULL)
6343 err = got_error_from_errno("fclose");
6348 static const struct got_error *
6349 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6350 struct got_object_id_queue *, const char *, const char *,
6351 struct got_repository *);
6353 static const struct got_error *
6354 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6355 struct got_object_id_queue *commits, const char *branch_name,
6356 int edit_logmsg_only, struct got_repository *repo)
6358 const struct got_error *err;
6362 err = got_opentemp_named(&path, &f, "got-histedit");
6366 err = write_cmd_list(f, branch_name, commits);
6370 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6374 if (edit_logmsg_only) {
6376 err = histedit_parse_list(histedit_cmds, f, repo);
6378 if (fclose(f) != 0) {
6379 err = got_error_from_errno("fclose");
6383 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6385 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6386 err->code != GOT_ERR_HISTEDIT_CMD)
6388 err = histedit_edit_list_retry(histedit_cmds, err,
6389 commits, path, branch_name, repo);
6393 if (f && fclose(f) != 0 && err == NULL)
6394 err = got_error_from_errno("fclose");
6395 if (path && unlink(path) != 0 && err == NULL)
6396 err = got_error_from_errno2("unlink", path);
6401 static const struct got_error *
6402 histedit_save_list(struct got_histedit_list *histedit_cmds,
6403 struct got_worktree *worktree, struct got_repository *repo)
6405 const struct got_error *err = NULL;
6408 struct got_histedit_list_entry *hle;
6409 struct got_commit_object *commit = NULL;
6411 err = got_worktree_get_histedit_script_path(&path, worktree);
6415 f = fopen(path, "w");
6417 err = got_error_from_errno2("fopen", path);
6420 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6421 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6427 int n = fprintf(f, "%c %s\n",
6428 GOT_HISTEDIT_MESG, hle->logmsg);
6430 err = got_ferror(f, GOT_ERR_IO);
6436 if (f && fclose(f) != 0 && err == NULL)
6437 err = got_error_from_errno("fclose");
6440 got_object_commit_close(commit);
6445 histedit_free_list(struct got_histedit_list *histedit_cmds)
6447 struct got_histedit_list_entry *hle;
6449 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6450 TAILQ_REMOVE(histedit_cmds, hle, entry);
6455 static const struct got_error *
6456 histedit_load_list(struct got_histedit_list *histedit_cmds,
6457 const char *path, struct got_repository *repo)
6459 const struct got_error *err = NULL;
6462 f = fopen(path, "r");
6464 err = got_error_from_errno2("fopen", path);
6468 err = histedit_parse_list(histedit_cmds, f, repo);
6470 if (f && fclose(f) != 0 && err == NULL)
6471 err = got_error_from_errno("fclose");
6475 static const struct got_error *
6476 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6477 const struct got_error *edit_err, struct got_object_id_queue *commits,
6478 const char *path, const char *branch_name, struct got_repository *repo)
6480 const struct got_error *err = NULL, *prev_err = edit_err;
6483 while (resp != 'c' && resp != 'r' && resp != 'a') {
6484 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6485 "or (a)bort: ", getprogname(), prev_err->msg);
6490 histedit_free_list(histedit_cmds);
6491 err = histedit_run_editor(histedit_cmds, path, commits,
6494 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6495 err->code != GOT_ERR_HISTEDIT_CMD)
6502 } else if (resp == 'r') {
6503 histedit_free_list(histedit_cmds);
6504 err = histedit_edit_script(histedit_cmds,
6505 commits, branch_name, 0, repo);
6507 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6508 err->code != GOT_ERR_HISTEDIT_CMD)
6515 } else if (resp == 'a') {
6516 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6519 printf("invalid response '%c'\n", resp);
6525 static const struct got_error *
6526 histedit_complete(struct got_worktree *worktree,
6527 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6528 struct got_reference *branch, struct got_repository *repo)
6530 printf("Switching work tree to %s\n",
6531 got_ref_get_symref_target(branch));
6532 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6536 static const struct got_error *
6537 show_histedit_progress(struct got_commit_object *commit,
6538 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6540 const struct got_error *err;
6541 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6543 err = got_object_id_str(&old_id_str, hle->commit_id);
6548 err = got_object_id_str(&new_id_str, new_id);
6553 old_id_str[12] = '\0';
6555 new_id_str[12] = '\0';
6558 logmsg = strdup(hle->logmsg);
6559 if (logmsg == NULL) {
6560 err = got_error_from_errno("strdup");
6563 trim_logmsg(logmsg, 42);
6565 err = get_short_logmsg(&logmsg, 42, commit);
6570 switch (hle->cmd->code) {
6571 case GOT_HISTEDIT_PICK:
6572 case GOT_HISTEDIT_EDIT:
6573 printf("%s -> %s: %s\n", old_id_str,
6574 new_id_str ? new_id_str : "no-op change", logmsg);
6576 case GOT_HISTEDIT_DROP:
6577 case GOT_HISTEDIT_FOLD:
6578 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6590 static const struct got_error *
6591 histedit_commit(struct got_pathlist_head *merged_paths,
6592 struct got_worktree *worktree, struct got_fileindex *fileindex,
6593 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6594 struct got_repository *repo)
6596 const struct got_error *err;
6597 struct got_commit_object *commit;
6598 struct got_object_id *new_commit_id;
6600 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6601 && hle->logmsg == NULL) {
6602 err = histedit_edit_logmsg(hle, repo);
6607 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6611 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6612 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6615 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6617 err = show_histedit_progress(commit, hle, NULL);
6619 err = show_histedit_progress(commit, hle, new_commit_id);
6620 free(new_commit_id);
6623 got_object_commit_close(commit);
6627 static const struct got_error *
6628 histedit_skip_commit(struct got_histedit_list_entry *hle,
6629 struct got_worktree *worktree, struct got_repository *repo)
6631 const struct got_error *error;
6632 struct got_commit_object *commit;
6634 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6639 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6643 error = show_histedit_progress(commit, hle, NULL);
6644 got_object_commit_close(commit);
6648 static const struct got_error *
6649 check_local_changes(void *arg, unsigned char status,
6650 unsigned char staged_status, const char *path,
6651 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6652 struct got_object_id *commit_id, int dirfd, const char *de_name)
6654 int *have_local_changes = arg;
6657 case GOT_STATUS_ADD:
6658 case GOT_STATUS_DELETE:
6659 case GOT_STATUS_MODIFY:
6660 case GOT_STATUS_CONFLICT:
6661 *have_local_changes = 1;
6662 return got_error(GOT_ERR_CANCELLED);
6667 switch (staged_status) {
6668 case GOT_STATUS_ADD:
6669 case GOT_STATUS_DELETE:
6670 case GOT_STATUS_MODIFY:
6671 *have_local_changes = 1;
6672 return got_error(GOT_ERR_CANCELLED);
6680 static const struct got_error *
6681 cmd_histedit(int argc, char *argv[])
6683 const struct got_error *error = NULL;
6684 struct got_worktree *worktree = NULL;
6685 struct got_fileindex *fileindex = NULL;
6686 struct got_repository *repo = NULL;
6688 struct got_reference *branch = NULL;
6689 struct got_reference *tmp_branch = NULL;
6690 struct got_object_id *resume_commit_id = NULL;
6691 struct got_object_id *base_commit_id = NULL;
6692 struct got_object_id *head_commit_id = NULL;
6693 struct got_commit_object *commit = NULL;
6694 int ch, rebase_in_progress = 0, did_something;
6695 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6696 int edit_logmsg_only = 0;
6697 const char *edit_script_path = NULL;
6698 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6699 struct got_object_id_queue commits;
6700 struct got_pathlist_head merged_paths;
6701 const struct got_object_id_queue *parent_ids;
6702 struct got_object_qid *pid;
6703 struct got_histedit_list histedit_cmds;
6704 struct got_histedit_list_entry *hle;
6706 SIMPLEQ_INIT(&commits);
6707 TAILQ_INIT(&histedit_cmds);
6708 TAILQ_INIT(&merged_paths);
6710 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6719 edit_script_path = optarg;
6722 edit_logmsg_only = 1;
6734 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6735 "unveil", NULL) == -1)
6738 if (abort_edit && continue_edit)
6739 errx(1, "histedit's -a and -c options are mutually exclusive");
6740 if (edit_script_path && edit_logmsg_only)
6741 errx(1, "histedit's -F and -m options are mutually exclusive");
6742 if (abort_edit && edit_logmsg_only)
6743 errx(1, "histedit's -a and -m options are mutually exclusive");
6744 if (continue_edit && edit_logmsg_only)
6745 errx(1, "histedit's -c and -m options are mutually exclusive");
6750 * This command cannot apply unveil(2) in all cases because the
6751 * user may choose to run an editor to edit the histedit script
6752 * and to edit individual commit log messages.
6753 * unveil(2) traverses exec(2); if an editor is used we have to
6754 * apply unveil after edit script and log messages have been written.
6755 * XXX TODO: Make use of unveil(2) where possible.
6758 cwd = getcwd(NULL, 0);
6760 error = got_error_from_errno("getcwd");
6763 error = got_worktree_open(&worktree, cwd);
6767 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6772 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6775 if (rebase_in_progress) {
6776 error = got_error(GOT_ERR_REBASING);
6780 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6784 if (edit_in_progress && edit_logmsg_only) {
6785 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6786 "histedit operation is in progress in this "
6787 "work tree and must be continued or aborted "
6788 "before the -m option can be used");
6792 if (edit_in_progress && abort_edit) {
6793 error = got_worktree_histedit_continue(&resume_commit_id,
6794 &tmp_branch, &branch, &base_commit_id, &fileindex,
6798 printf("Switching work tree to %s\n",
6799 got_ref_get_symref_target(branch));
6800 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6801 branch, base_commit_id, update_progress, &did_something);
6804 printf("Histedit of %s aborted\n",
6805 got_ref_get_symref_target(branch));
6806 goto done; /* nothing else to do */
6807 } else if (abort_edit) {
6808 error = got_error(GOT_ERR_NOT_HISTEDIT);
6812 if (continue_edit) {
6815 if (!edit_in_progress) {
6816 error = got_error(GOT_ERR_NOT_HISTEDIT);
6820 error = got_worktree_get_histedit_script_path(&path, worktree);
6824 error = histedit_load_list(&histedit_cmds, path, repo);
6829 error = got_worktree_histedit_continue(&resume_commit_id,
6830 &tmp_branch, &branch, &base_commit_id, &fileindex,
6835 error = got_ref_resolve(&head_commit_id, repo, branch);
6839 error = got_object_open_as_commit(&commit, repo,
6843 parent_ids = got_object_commit_get_parent_ids(commit);
6844 pid = SIMPLEQ_FIRST(parent_ids);
6846 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6849 error = collect_commits(&commits, head_commit_id, pid->id,
6850 base_commit_id, got_worktree_get_path_prefix(worktree),
6851 GOT_ERR_HISTEDIT_PATH, repo);
6852 got_object_commit_close(commit);
6857 if (edit_in_progress) {
6858 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6862 error = got_ref_open(&branch, repo,
6863 got_worktree_get_head_ref_name(worktree), 0);
6867 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6868 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6869 "will not edit commit history of a branch outside "
6870 "the \"refs/heads/\" reference namespace");
6874 error = got_ref_resolve(&head_commit_id, repo, branch);
6875 got_ref_close(branch);
6880 error = got_object_open_as_commit(&commit, repo,
6884 parent_ids = got_object_commit_get_parent_ids(commit);
6885 pid = SIMPLEQ_FIRST(parent_ids);
6887 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6890 error = collect_commits(&commits, head_commit_id, pid->id,
6891 got_worktree_get_base_commit_id(worktree),
6892 got_worktree_get_path_prefix(worktree),
6893 GOT_ERR_HISTEDIT_PATH, repo);
6894 got_object_commit_close(commit);
6899 if (SIMPLEQ_EMPTY(&commits)) {
6900 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6904 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6905 &base_commit_id, &fileindex, worktree, repo);
6909 if (edit_script_path) {
6910 error = histedit_load_list(&histedit_cmds,
6911 edit_script_path, repo);
6913 got_worktree_histedit_abort(worktree, fileindex,
6914 repo, branch, base_commit_id,
6915 update_progress, &did_something);
6919 const char *branch_name;
6920 branch_name = got_ref_get_symref_target(branch);
6921 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6923 error = histedit_edit_script(&histedit_cmds, &commits,
6924 branch_name, edit_logmsg_only, repo);
6926 got_worktree_histedit_abort(worktree, fileindex,
6927 repo, branch, base_commit_id,
6928 update_progress, &did_something);
6934 error = histedit_save_list(&histedit_cmds, worktree,
6937 got_worktree_histedit_abort(worktree, fileindex,
6938 repo, branch, base_commit_id,
6939 update_progress, &did_something);
6945 error = histedit_check_script(&histedit_cmds, &commits, repo);
6949 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6950 if (resume_commit_id) {
6951 if (got_object_id_cmp(hle->commit_id,
6952 resume_commit_id) != 0)
6955 resume_commit_id = NULL;
6956 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6957 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6958 error = histedit_skip_commit(hle, worktree,
6963 struct got_pathlist_head paths;
6964 int have_changes = 0;
6967 error = got_pathlist_append(&paths, "", NULL);
6970 error = got_worktree_status(worktree, &paths,
6971 repo, check_local_changes, &have_changes,
6972 check_cancelled, NULL);
6973 got_pathlist_free(&paths);
6975 if (error->code != GOT_ERR_CANCELLED)
6977 if (sigint_received || sigpipe_received)
6981 error = histedit_commit(NULL, worktree,
6982 fileindex, tmp_branch, hle, repo);
6986 error = got_object_open_as_commit(
6987 &commit, repo, hle->commit_id);
6990 error = show_histedit_progress(commit,
6992 got_object_commit_close(commit);
7001 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7002 error = histedit_skip_commit(hle, worktree, repo);
7008 error = got_object_open_as_commit(&commit, repo,
7012 parent_ids = got_object_commit_get_parent_ids(commit);
7013 pid = SIMPLEQ_FIRST(parent_ids);
7015 error = got_worktree_histedit_merge_files(&merged_paths,
7016 worktree, fileindex, pid->id, hle->commit_id, repo,
7017 rebase_progress, &rebase_status, check_cancelled, NULL);
7020 got_object_commit_close(commit);
7023 if (rebase_status == GOT_STATUS_CONFLICT) {
7024 error = show_rebase_merge_conflict(hle->commit_id,
7028 got_worktree_rebase_pathlist_free(&merged_paths);
7032 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7034 error = got_object_id_str(&id_str, hle->commit_id);
7037 printf("Stopping histedit for amending commit %s\n",
7040 got_worktree_rebase_pathlist_free(&merged_paths);
7041 error = got_worktree_histedit_postpone(worktree,
7046 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7047 error = histedit_skip_commit(hle, worktree, repo);
7053 error = histedit_commit(&merged_paths, worktree, fileindex,
7054 tmp_branch, hle, repo);
7055 got_worktree_rebase_pathlist_free(&merged_paths);
7060 if (rebase_status == GOT_STATUS_CONFLICT) {
7061 error = got_worktree_histedit_postpone(worktree, fileindex);
7064 error = got_error_msg(GOT_ERR_CONFLICTS,
7065 "conflicts must be resolved before histedit can continue");
7067 error = histedit_complete(worktree, fileindex, tmp_branch,
7070 got_object_id_queue_free(&commits);
7071 histedit_free_list(&histedit_cmds);
7072 free(head_commit_id);
7073 free(base_commit_id);
7074 free(resume_commit_id);
7076 got_object_commit_close(commit);
7078 got_ref_close(branch);
7080 got_ref_close(tmp_branch);
7082 got_worktree_close(worktree);
7084 got_repo_close(repo);
7089 usage_integrate(void)
7091 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7095 static const struct got_error *
7096 cmd_integrate(int argc, char *argv[])
7098 const struct got_error *error = NULL;
7099 struct got_repository *repo = NULL;
7100 struct got_worktree *worktree = NULL;
7101 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7102 const char *branch_arg = NULL;
7103 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7104 struct got_fileindex *fileindex = NULL;
7105 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7106 int ch, did_something = 0;
7108 while ((ch = getopt(argc, argv, "")) != -1) {
7121 branch_arg = argv[0];
7123 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 "unveil", NULL) == -1)
7127 cwd = getcwd(NULL, 0);
7129 error = got_error_from_errno("getcwd");
7133 error = got_worktree_open(&worktree, cwd);
7137 error = check_rebase_or_histedit_in_progress(worktree);
7141 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7146 error = apply_unveil(got_repo_get_path(repo), 0,
7147 got_worktree_get_root_path(worktree));
7151 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7152 error = got_error_from_errno("asprintf");
7156 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7157 &base_branch_ref, worktree, refname, repo);
7161 refname = strdup(got_ref_get_name(branch_ref));
7162 if (refname == NULL) {
7163 error = got_error_from_errno("strdup");
7164 got_worktree_integrate_abort(worktree, fileindex, repo,
7165 branch_ref, base_branch_ref);
7168 base_refname = strdup(got_ref_get_name(base_branch_ref));
7169 if (base_refname == NULL) {
7170 error = got_error_from_errno("strdup");
7171 got_worktree_integrate_abort(worktree, fileindex, repo,
7172 branch_ref, base_branch_ref);
7176 error = got_ref_resolve(&commit_id, repo, branch_ref);
7180 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7184 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7185 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7186 "specified branch has already been integrated");
7187 got_worktree_integrate_abort(worktree, fileindex, repo,
7188 branch_ref, base_branch_ref);
7192 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7194 if (error->code == GOT_ERR_ANCESTRY)
7195 error = got_error(GOT_ERR_REBASE_REQUIRED);
7196 got_worktree_integrate_abort(worktree, fileindex, repo,
7197 branch_ref, base_branch_ref);
7201 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7202 branch_ref, base_branch_ref, update_progress, &did_something,
7203 check_cancelled, NULL);
7207 printf("Integrated %s into %s\n", refname, base_refname);
7210 got_repo_close(repo);
7212 got_worktree_close(worktree);
7214 free(base_commit_id);
7224 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7225 "[file-path ...]\n",
7230 static const struct got_error *
7231 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7232 const char *path, struct got_object_id *blob_id,
7233 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7234 int dirfd, const char *de_name)
7236 const struct got_error *err = NULL;
7237 char *id_str = NULL;
7239 if (staged_status != GOT_STATUS_ADD &&
7240 staged_status != GOT_STATUS_MODIFY &&
7241 staged_status != GOT_STATUS_DELETE)
7244 if (staged_status == GOT_STATUS_ADD ||
7245 staged_status == GOT_STATUS_MODIFY)
7246 err = got_object_id_str(&id_str, staged_blob_id);
7248 err = got_object_id_str(&id_str, blob_id);
7252 printf("%s %c %s\n", id_str, staged_status, path);
7257 static const struct got_error *
7258 cmd_stage(int argc, char *argv[])
7260 const struct got_error *error = NULL;
7261 struct got_repository *repo = NULL;
7262 struct got_worktree *worktree = NULL;
7264 struct got_pathlist_head paths;
7265 struct got_pathlist_entry *pe;
7266 int ch, list_stage = 0, pflag = 0;
7267 FILE *patch_script_file = NULL;
7268 const char *patch_script_path = NULL;
7269 struct choose_patch_arg cpa;
7273 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7282 patch_script_path = optarg;
7294 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7295 "unveil", NULL) == -1)
7298 if (list_stage && (pflag || patch_script_path))
7299 errx(1, "-l option cannot be used with other options");
7300 if (patch_script_path && !pflag)
7301 errx(1, "-F option can only be used together with -p option");
7303 cwd = getcwd(NULL, 0);
7305 error = got_error_from_errno("getcwd");
7309 error = got_worktree_open(&worktree, cwd);
7313 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7318 if (patch_script_path) {
7319 patch_script_file = fopen(patch_script_path, "r");
7320 if (patch_script_file == NULL) {
7321 error = got_error_from_errno2("fopen",
7326 error = apply_unveil(got_repo_get_path(repo), 0,
7327 got_worktree_get_root_path(worktree));
7331 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7336 error = got_worktree_status(worktree, &paths, repo,
7337 print_stage, NULL, check_cancelled, NULL);
7339 cpa.patch_script_file = patch_script_file;
7340 cpa.action = "stage";
7341 error = got_worktree_stage(worktree, &paths,
7342 pflag ? NULL : print_status, NULL,
7343 pflag ? choose_patch : NULL, &cpa, repo);
7346 if (patch_script_file && fclose(patch_script_file) == EOF &&
7348 error = got_error_from_errno2("fclose", patch_script_path);
7350 got_repo_close(repo);
7352 got_worktree_close(worktree);
7353 TAILQ_FOREACH(pe, &paths, entry)
7354 free((char *)pe->path);
7355 got_pathlist_free(&paths);
7363 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7364 "[file-path ...]\n",
7370 static const struct got_error *
7371 cmd_unstage(int argc, char *argv[])
7373 const struct got_error *error = NULL;
7374 struct got_repository *repo = NULL;
7375 struct got_worktree *worktree = NULL;
7377 struct got_pathlist_head paths;
7378 struct got_pathlist_entry *pe;
7379 int ch, did_something = 0, pflag = 0;
7380 FILE *patch_script_file = NULL;
7381 const char *patch_script_path = NULL;
7382 struct choose_patch_arg cpa;
7386 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7392 patch_script_path = optarg;
7404 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7405 "unveil", NULL) == -1)
7408 if (patch_script_path && !pflag)
7409 errx(1, "-F option can only be used together with -p option");
7411 cwd = getcwd(NULL, 0);
7413 error = got_error_from_errno("getcwd");
7417 error = got_worktree_open(&worktree, cwd);
7421 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7426 if (patch_script_path) {
7427 patch_script_file = fopen(patch_script_path, "r");
7428 if (patch_script_file == NULL) {
7429 error = got_error_from_errno2("fopen",
7435 error = apply_unveil(got_repo_get_path(repo), 0,
7436 got_worktree_get_root_path(worktree));
7440 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7444 cpa.patch_script_file = patch_script_file;
7445 cpa.action = "unstage";
7446 error = got_worktree_unstage(worktree, &paths, update_progress,
7447 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7449 if (patch_script_file && fclose(patch_script_file) == EOF &&
7451 error = got_error_from_errno2("fclose", patch_script_path);
7453 got_repo_close(repo);
7455 got_worktree_close(worktree);
7456 TAILQ_FOREACH(pe, &paths, entry)
7457 free((char *)pe->path);
7458 got_pathlist_free(&paths);
7466 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7467 "arg1 [arg2 ...]\n", getprogname());
7471 static const struct got_error *
7472 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7474 const struct got_error *err;
7475 struct got_blob_object *blob;
7477 err = got_object_open_as_blob(&blob, repo, id, 8192);
7481 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7482 got_object_blob_close(blob);
7486 static const struct got_error *
7487 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7489 const struct got_error *err;
7490 struct got_tree_object *tree;
7493 err = got_object_open_as_tree(&tree, repo, id);
7497 nentries = got_object_tree_get_nentries(tree);
7498 for (i = 0; i < nentries; i++) {
7499 struct got_tree_entry *te;
7501 if (sigint_received || sigpipe_received)
7503 te = got_object_tree_get_entry(tree, i);
7504 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7507 fprintf(outfile, "%s %.7o %s\n", id_str,
7508 got_tree_entry_get_mode(te),
7509 got_tree_entry_get_name(te));
7513 got_object_tree_close(tree);
7517 static const struct got_error *
7518 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7520 const struct got_error *err;
7521 struct got_commit_object *commit;
7522 const struct got_object_id_queue *parent_ids;
7523 struct got_object_qid *pid;
7524 char *id_str = NULL;
7525 const char *logmsg = NULL;
7527 err = got_object_open_as_commit(&commit, repo, id);
7531 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7535 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7536 parent_ids = got_object_commit_get_parent_ids(commit);
7537 fprintf(outfile, "numparents %d\n",
7538 got_object_commit_get_nparents(commit));
7539 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7541 err = got_object_id_str(&pid_str, pid->id);
7544 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7547 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7548 got_object_commit_get_author(commit),
7549 got_object_commit_get_author_time(commit));
7551 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7552 got_object_commit_get_author(commit),
7553 got_object_commit_get_committer_time(commit));
7555 logmsg = got_object_commit_get_logmsg_raw(commit);
7556 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7557 fprintf(outfile, "%s", logmsg);
7560 got_object_commit_close(commit);
7564 static const struct got_error *
7565 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7567 const struct got_error *err;
7568 struct got_tag_object *tag;
7569 char *id_str = NULL;
7570 const char *tagmsg = NULL;
7572 err = got_object_open_as_tag(&tag, repo, id);
7576 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7580 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7582 switch (got_object_tag_get_object_type(tag)) {
7583 case GOT_OBJ_TYPE_BLOB:
7584 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7585 GOT_OBJ_LABEL_BLOB);
7587 case GOT_OBJ_TYPE_TREE:
7588 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7589 GOT_OBJ_LABEL_TREE);
7591 case GOT_OBJ_TYPE_COMMIT:
7592 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7593 GOT_OBJ_LABEL_COMMIT);
7595 case GOT_OBJ_TYPE_TAG:
7596 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7603 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7604 got_object_tag_get_name(tag));
7606 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7607 got_object_tag_get_tagger(tag),
7608 got_object_tag_get_tagger_time(tag));
7610 tagmsg = got_object_tag_get_message(tag);
7611 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7612 fprintf(outfile, "%s", tagmsg);
7615 got_object_tag_close(tag);
7619 static const struct got_error *
7620 cmd_cat(int argc, char *argv[])
7622 const struct got_error *error;
7623 struct got_repository *repo = NULL;
7624 struct got_worktree *worktree = NULL;
7625 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7626 const char *commit_id_str = NULL;
7627 struct got_object_id *id = NULL, *commit_id = NULL;
7628 int ch, obj_type, i, force_path = 0;
7631 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7636 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7639 commit_id_str = optarg;
7642 repo_path = realpath(optarg, NULL);
7643 if (repo_path == NULL)
7644 return got_error_from_errno2("realpath",
7646 got_path_strip_trailing_slashes(repo_path);
7660 cwd = getcwd(NULL, 0);
7662 error = got_error_from_errno("getcwd");
7665 error = got_worktree_open(&worktree, cwd);
7666 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7669 if (repo_path == NULL) {
7671 got_worktree_get_repo_path(worktree));
7672 if (repo_path == NULL) {
7673 error = got_error_from_errno("strdup");
7679 if (repo_path == NULL) {
7680 repo_path = getcwd(NULL, 0);
7681 if (repo_path == NULL)
7682 return got_error_from_errno("getcwd");
7685 error = got_repo_open(&repo, repo_path, NULL);
7690 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7694 if (commit_id_str == NULL)
7695 commit_id_str = GOT_REF_HEAD;
7696 error = got_repo_match_object_id(&commit_id, NULL,
7697 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7701 for (i = 0; i < argc; i++) {
7703 error = got_object_id_by_path(&id, repo, commit_id,
7708 error = got_repo_match_object_id(&id, &label, argv[i],
7709 GOT_OBJ_TYPE_ANY, 0, repo);
7711 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7712 error->code != GOT_ERR_NOT_REF)
7714 error = got_object_id_by_path(&id, repo,
7715 commit_id, argv[i]);
7721 error = got_object_get_type(&obj_type, repo, id);
7726 case GOT_OBJ_TYPE_BLOB:
7727 error = cat_blob(id, repo, stdout);
7729 case GOT_OBJ_TYPE_TREE:
7730 error = cat_tree(id, repo, stdout);
7732 case GOT_OBJ_TYPE_COMMIT:
7733 error = cat_commit(id, repo, stdout);
7735 case GOT_OBJ_TYPE_TAG:
7736 error = cat_tag(id, repo, stdout);
7739 error = got_error(GOT_ERR_OBJ_TYPE);
7754 got_worktree_close(worktree);
7756 const struct got_error *repo_error;
7757 repo_error = got_repo_close(repo);