2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
42 #include "got_version.h"
43 #include "got_error.h"
44 #include "got_object.h"
45 #include "got_reference.h"
46 #include "got_repository.h"
48 #include "got_cancel.h"
49 #include "got_worktree.h"
51 #include "got_commit_graph.h"
52 #include "got_fetch.h"
53 #include "got_blame.h"
54 #include "got_privsep.h"
55 #include "got_opentemp.h"
58 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
61 static volatile sig_atomic_t sigint_received;
62 static volatile sig_atomic_t sigpipe_received;
65 catch_sigint(int signo)
71 catch_sigpipe(int signo)
79 const struct got_error *(*cmd_main)(int, char *[]);
80 void (*cmd_usage)(void);
81 const char *cmd_alias;
84 __dead static void usage(int);
85 __dead static void usage_init(void);
86 __dead static void usage_import(void);
87 __dead static void usage_checkout(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_update(void);
90 __dead static void usage_log(void);
91 __dead static void usage_diff(void);
92 __dead static void usage_blame(void);
93 __dead static void usage_tree(void);
94 __dead static void usage_status(void);
95 __dead static void usage_ref(void);
96 __dead static void usage_branch(void);
97 __dead static void usage_tag(void);
98 __dead static void usage_add(void);
99 __dead static void usage_remove(void);
100 __dead static void usage_revert(void);
101 __dead static void usage_commit(void);
102 __dead static void usage_cherrypick(void);
103 __dead static void usage_backout(void);
104 __dead static void usage_rebase(void);
105 __dead static void usage_histedit(void);
106 __dead static void usage_integrate(void);
107 __dead static void usage_stage(void);
108 __dead static void usage_unstage(void);
109 __dead static void usage_cat(void);
111 static const struct got_error* cmd_init(int, char *[]);
112 static const struct got_error* cmd_import(int, char *[]);
113 static const struct got_error* cmd_clone(int, char *[]);
114 static const struct got_error* cmd_checkout(int, char *[]);
115 static const struct got_error* cmd_update(int, char *[]);
116 static const struct got_error* cmd_log(int, char *[]);
117 static const struct got_error* cmd_diff(int, char *[]);
118 static const struct got_error* cmd_blame(int, char *[]);
119 static const struct got_error* cmd_tree(int, char *[]);
120 static const struct got_error* cmd_status(int, char *[]);
121 static const struct got_error* cmd_ref(int, char *[]);
122 static const struct got_error* cmd_branch(int, char *[]);
123 static const struct got_error* cmd_tag(int, char *[]);
124 static const struct got_error* cmd_add(int, char *[]);
125 static const struct got_error* cmd_remove(int, char *[]);
126 static const struct got_error* cmd_revert(int, char *[]);
127 static const struct got_error* cmd_commit(int, char *[]);
128 static const struct got_error* cmd_cherrypick(int, char *[]);
129 static const struct got_error* cmd_backout(int, char *[]);
130 static const struct got_error* cmd_rebase(int, char *[]);
131 static const struct got_error* cmd_histedit(int, char *[]);
132 static const struct got_error* cmd_integrate(int, char *[]);
133 static const struct got_error* cmd_stage(int, char *[]);
134 static const struct got_error* cmd_unstage(int, char *[]);
135 static const struct got_error* cmd_cat(int, char *[]);
137 static struct got_cmd got_commands[] = {
138 { "init", cmd_init, usage_init, "in" },
139 { "import", cmd_import, usage_import, "im" },
140 { "checkout", cmd_checkout, usage_checkout, "co" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "update", cmd_update, usage_update, "up" },
143 { "log", cmd_log, usage_log, "" },
144 { "diff", cmd_diff, usage_diff, "di" },
145 { "blame", cmd_blame, usage_blame, "bl" },
146 { "tree", cmd_tree, usage_tree, "tr" },
147 { "status", cmd_status, usage_status, "st" },
148 { "ref", cmd_ref, usage_ref, "" },
149 { "branch", cmd_branch, usage_branch, "br" },
150 { "tag", cmd_tag, usage_tag, "" },
151 { "add", cmd_add, usage_add, "" },
152 { "remove", cmd_remove, usage_remove, "rm" },
153 { "revert", cmd_revert, usage_revert, "rv" },
154 { "commit", cmd_commit, usage_commit, "ci" },
155 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 { "backout", cmd_backout, usage_backout, "bo" },
157 { "rebase", cmd_rebase, usage_rebase, "rb" },
158 { "histedit", cmd_histedit, usage_histedit, "he" },
159 { "integrate", cmd_integrate, usage_integrate,"ig" },
160 { "stage", cmd_stage, usage_stage, "sg" },
161 { "unstage", cmd_unstage, usage_unstage, "ug" },
162 { "cat", cmd_cat, usage_cat, "" },
170 fprintf(stderr, "commands:");
171 for (i = 0; i < nitems(got_commands); i++) {
172 struct got_cmd *cmd = &got_commands[i];
173 fprintf(stderr, " %s", cmd->cmd_name);
179 main(int argc, char *argv[])
184 int hflag = 0, Vflag = 0;
185 static struct option longopts[] = {
186 { "version", no_argument, NULL, 'V' },
190 setlocale(LC_CTYPE, "");
192 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
211 got_version_print_str();
218 signal(SIGINT, catch_sigint);
219 signal(SIGPIPE, catch_sigpipe);
221 for (i = 0; i < nitems(got_commands); i++) {
222 const struct got_error *error;
224 cmd = &got_commands[i];
226 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 strcmp(cmd->cmd_alias, argv[0]) != 0)
231 got_commands[i].cmd_usage();
233 error = got_commands[i].cmd_main(argc, argv);
234 if (error && error->code != GOT_ERR_CANCELLED &&
235 error->code != GOT_ERR_PRIVSEP_EXIT &&
236 !(sigpipe_received &&
237 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
239 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
247 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
255 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
262 static const struct got_error *
263 get_editor(char **abspath)
265 const struct got_error *err = NULL;
270 editor = getenv("VISUAL");
272 editor = getenv("EDITOR");
275 err = got_path_find_prog(abspath, editor);
280 if (*abspath == NULL) {
281 *abspath = strdup("/bin/ed");
282 if (*abspath == NULL)
283 return got_error_from_errno("strdup");
289 static const struct got_error *
290 apply_unveil(const char *repo_path, int repo_read_only,
291 const char *worktree_path)
293 const struct got_error *err;
296 if (unveil("gmon.out", "rwc") != 0)
297 return got_error_from_errno2("unveil", "gmon.out");
299 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 return got_error_from_errno2("unveil", repo_path);
302 if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 return got_error_from_errno2("unveil", worktree_path);
305 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
308 err = got_privsep_unveil_exec_helpers();
312 if (unveil(NULL, NULL) != 0)
313 return got_error_from_errno("unveil");
321 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
325 static const struct got_error *
326 cmd_init(int argc, char *argv[])
328 const struct got_error *error = NULL;
329 char *repo_path = NULL;
332 while ((ch = getopt(argc, argv, "")) != -1) {
344 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
350 repo_path = strdup(argv[0]);
351 if (repo_path == NULL)
352 return got_error_from_errno("strdup");
354 got_path_strip_trailing_slashes(repo_path);
356 error = got_path_mkdir(repo_path);
358 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
361 error = apply_unveil(repo_path, 0, NULL);
365 error = got_repo_init(repo_path);
374 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 "[-r repository-path] [-I pattern] path\n", getprogname());
380 spawn_editor(const char *editor, const char *file)
383 sig_t sighup, sigint, sigquit;
386 sighup = signal(SIGHUP, SIG_IGN);
387 sigint = signal(SIGINT, SIG_IGN);
388 sigquit = signal(SIGQUIT, SIG_IGN);
390 switch (pid = fork()) {
394 execl(editor, editor, file, (char *)NULL);
398 while (waitpid(pid, &st, 0) == -1)
403 (void)signal(SIGHUP, sighup);
404 (void)signal(SIGINT, sigint);
405 (void)signal(SIGQUIT, sigquit);
407 if (!WIFEXITED(st)) {
412 return WEXITSTATUS(st);
415 static const struct got_error *
416 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 const char *initial_content)
419 const struct got_error *err = NULL;
423 int content_changed = 0;
428 if (stat(logmsg_path, &st) == -1)
429 return got_error_from_errno2("stat", logmsg_path);
431 if (spawn_editor(editor, logmsg_path) == -1)
432 return got_error_from_errno("failed spawning editor");
434 if (stat(logmsg_path, &st2) == -1)
435 return got_error_from_errno("stat");
437 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 "no changes made to commit message, aborting");
441 *logmsg = malloc(st2.st_size + 1);
443 return got_error_from_errno("malloc");
447 fp = fopen(logmsg_path, "r");
449 err = got_error_from_errno("fopen");
452 while (fgets(buf, sizeof(buf), fp) != NULL) {
453 if (!content_changed && strcmp(buf, initial_content) != 0)
455 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 continue; /* remove comments and leading empty lines */
457 len = strlcat(*logmsg, buf, st2.st_size);
461 while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 (*logmsg)[len - 1] = '\0';
466 if (len == 0 || !content_changed)
467 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 "commit message cannot be empty, aborting");
477 static const struct got_error *
478 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 const char *path_dir, const char *branch_name)
481 char *initial_content = NULL;
482 const struct got_error *err = NULL;
485 if (asprintf(&initial_content,
486 "\n# %s to be imported to branch %s\n", path_dir,
488 return got_error_from_errno("asprintf");
490 err = got_opentemp_named_fd(logmsg_path, &fd,
491 GOT_TMPDIR_STR "/got-importmsg");
495 dprintf(fd, initial_content);
498 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
500 free(initial_content);
504 static const struct got_error *
505 import_progress(void *arg, const char *path)
507 printf("A %s\n", path);
511 static const struct got_error *
512 get_author(char **author, struct got_repository *repo)
514 const struct got_error *err = NULL;
515 const char *got_author, *name, *email;
519 name = got_repo_get_gitconfig_author_name(repo);
520 email = got_repo_get_gitconfig_author_email(repo);
522 if (asprintf(author, "%s <%s>", name, email) == -1)
523 return got_error_from_errno("asprintf");
527 got_author = getenv("GOT_AUTHOR");
528 if (got_author == NULL) {
529 name = got_repo_get_global_gitconfig_author_name(repo);
530 email = got_repo_get_global_gitconfig_author_email(repo);
532 if (asprintf(author, "%s <%s>", name, email) == -1)
533 return got_error_from_errno("asprintf");
536 /* TODO: Look up user in password database? */
537 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
540 *author = strdup(got_author);
542 return got_error_from_errno("strdup");
545 * Really dumb email address check; we're only doing this to
546 * avoid git's object parser breaking on commits we create.
548 while (*got_author && *got_author != '<')
550 if (*got_author != '<') {
551 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 while (*got_author && *got_author != '@')
556 if (*got_author != '@') {
557 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
560 while (*got_author && *got_author != '>')
562 if (*got_author != '>')
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
572 static const struct got_error *
573 get_gitconfig_path(char **gitconfig_path)
575 const char *homedir = getenv("HOME");
577 *gitconfig_path = NULL;
579 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 return got_error_from_errno("asprintf");
586 static const struct got_error *
587 cmd_import(int argc, char *argv[])
589 const struct got_error *error = NULL;
590 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 const char *branch_name = "main";
593 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 struct got_repository *repo = NULL;
595 struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 struct got_object_id *new_commit_id = NULL;
598 struct got_pathlist_head ignores;
599 struct got_pathlist_entry *pe;
600 int preserve_logmsg = 0;
602 TAILQ_INIT(&ignores);
604 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
607 branch_name = optarg;
610 logmsg = strdup(optarg);
611 if (logmsg == NULL) {
612 error = got_error_from_errno("strdup");
617 repo_path = realpath(optarg, NULL);
618 if (repo_path == NULL) {
619 error = got_error_from_errno2("realpath",
625 if (optarg[0] == '\0')
627 error = got_pathlist_insert(&pe, &ignores, optarg,
642 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
650 if (repo_path == NULL) {
651 repo_path = getcwd(NULL, 0);
652 if (repo_path == NULL)
653 return got_error_from_errno("getcwd");
655 got_path_strip_trailing_slashes(repo_path);
656 error = get_gitconfig_path(&gitconfig_path);
659 error = got_repo_open(&repo, repo_path, gitconfig_path);
663 error = get_author(&author, repo);
668 * Don't let the user create a branch name with a leading '-'.
669 * While technically a valid reference name, this case is usually
670 * an unintended typo.
672 if (branch_name[0] == '-')
673 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
675 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 error = got_error_from_errno("asprintf");
680 error = got_ref_open(&branch_ref, repo, refname, 0);
682 if (error->code != GOT_ERR_NOT_REF)
685 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 "import target branch already exists");
690 path_dir = realpath(argv[0], NULL);
691 if (path_dir == NULL) {
692 error = got_error_from_errno2("realpath", argv[0]);
695 got_path_strip_trailing_slashes(path_dir);
698 * unveil(2) traverses exec(2); if an editor is used we have
699 * to apply unveil after the log message has been written.
701 if (logmsg == NULL || strlen(logmsg) == 0) {
702 error = get_editor(&editor);
706 error = collect_import_msg(&logmsg, &logmsg_path, editor,
709 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
716 if (unveil(path_dir, "r") != 0) {
717 error = got_error_from_errno2("unveil", path_dir);
723 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
730 error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 author, &ignores, repo, import_progress, NULL);
738 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
745 error = got_ref_write(branch_ref, repo);
752 error = got_object_id_str(&id_str, new_commit_id);
759 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
761 if (error->code != GOT_ERR_NOT_REF) {
767 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
775 error = got_ref_write(head_ref, repo);
783 printf("Created branch %s with commit %s\n",
784 got_ref_get_name(branch_ref), id_str);
786 if (preserve_logmsg) {
787 fprintf(stderr, "%s: log message preserved in %s\n",
788 getprogname(), logmsg_path);
789 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 error = got_error_from_errno2("unlink", logmsg_path);
799 free(gitconfig_path);
801 got_ref_close(branch_ref);
803 got_ref_close(head_ref);
810 fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
817 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 "[-p prefix] repository-path [worktree-path]\n", getprogname());
823 show_worktree_base_ref_warning(void)
825 fprintf(stderr, "%s: warning: could not create a reference "
826 "to the work tree's base commit; the commit could be "
827 "garbage-collected by Git; making the repository "
828 "writable and running 'got update' will prevent this\n",
832 struct got_checkout_progress_arg {
833 const char *worktree_path;
834 int had_base_commit_ref_error;
837 static const struct got_error *
838 checkout_progress(void *arg, unsigned char status, const char *path)
840 struct got_checkout_progress_arg *a = arg;
842 /* Base commit bump happens silently. */
843 if (status == GOT_STATUS_BUMP_BASE)
846 if (status == GOT_STATUS_BASE_REF_ERR) {
847 a->had_base_commit_ref_error = 1;
851 while (path[0] == '/')
854 printf("%c %s/%s\n", status, a->worktree_path, path);
858 static const struct got_error *
859 check_cancelled(void *arg)
861 if (sigint_received || sigpipe_received)
862 return got_error(GOT_ERR_CANCELLED);
866 static const struct got_error *
867 check_linear_ancestry(struct got_object_id *commit_id,
868 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 struct got_repository *repo)
871 const struct got_error *err = NULL;
872 struct got_object_id *yca_id;
874 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 commit_id, base_commit_id, repo, check_cancelled, NULL);
880 return got_error(GOT_ERR_ANCESTRY);
883 * Require a straight line of history between the target commit
884 * and the work tree's base commit.
886 * Non-linear situations such as this require a rebase:
888 * (commit) D F (base_commit)
896 * 'got update' only handles linear cases:
897 * Update forwards in time: A (base/yca) - B - C - D (commit)
898 * Update backwards in time: D (base) - C - B - A (commit/yca)
900 if (allow_forwards_in_time_only) {
901 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 return got_error(GOT_ERR_ANCESTRY);
903 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 got_object_id_cmp(base_commit_id, yca_id) != 0)
905 return got_error(GOT_ERR_ANCESTRY);
911 static const struct got_error *
912 check_same_branch(struct got_object_id *commit_id,
913 struct got_reference *head_ref, struct got_object_id *yca_id,
914 struct got_repository *repo)
916 const struct got_error *err = NULL;
917 struct got_commit_graph *graph = NULL;
918 struct got_object_id *head_commit_id = NULL;
919 int is_same_branch = 0;
921 err = got_ref_resolve(&head_commit_id, repo, head_ref);
925 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
929 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
934 err = got_commit_graph_open(&graph, "/", 1);
938 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 check_cancelled, NULL);
944 struct got_object_id *id;
945 err = got_commit_graph_iter_next(&id, graph, repo,
946 check_cancelled, NULL);
948 if (err->code == GOT_ERR_ITER_COMPLETED)
954 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
956 if (got_object_id_cmp(id, commit_id) == 0) {
964 got_commit_graph_close(graph);
965 free(head_commit_id);
966 if (!err && !is_same_branch)
967 err = got_error(GOT_ERR_ANCESTRY);
971 static const struct got_error *
972 cmd_clone(int argc, char *argv[])
974 const struct got_error *err = NULL;
975 const char *uri, *branch_filter, *dirname;
976 char *proto, *host, *port, *repo_name, *server_path;
977 char *default_destdir = NULL, *id_str = NULL;
978 const char *repo_path;
979 struct got_repository *repo = NULL;
980 struct got_pathlist_head refs, symrefs;
981 struct got_pathlist_entry *pe;
982 struct got_object_id *pack_hash = NULL;
983 int ch, fetchfd = -1;
986 TAILQ_INIT(&symrefs);
988 while ((ch = getopt(argc, argv, "b:")) != -1) {
991 branch_filter = optarg;
1008 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1009 &repo_name, argv[0]);
1013 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1017 if (dirname == NULL) {
1018 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1019 err = got_error_from_errno("asprintf");
1022 repo_path = default_destdir;
1024 repo_path = dirname;
1026 err = got_path_mkdir(repo_path);
1030 err = got_repo_init(repo_path);
1034 err = got_repo_open(&repo, repo_path, NULL);
1038 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1043 err = got_object_id_str(&id_str, pack_hash);
1046 printf("Fetched %s.pack\n", id_str);
1049 /* Set up references provided with the pack file. */
1050 TAILQ_FOREACH(pe, &refs, entry) {
1051 const char *refname = pe->path;
1052 struct got_object_id *id = pe->data;
1053 struct got_reference *ref;
1055 err = got_object_id_str(&id_str, id);
1059 err = got_ref_alloc(&ref, refname, id);
1065 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1067 err = got_ref_write(ref, repo);
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *symref, *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 err = got_ref_open(&target_ref, repo, target, 0);
1084 if (err->code == GOT_ERR_NOT_REF)
1089 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1090 got_ref_close(target_ref);
1094 printf("Setting %s to %s\n", GOT_REF_HEAD,
1095 got_ref_get_symref_target(symref));
1097 err = got_ref_write(symref, repo);
1098 got_ref_close(symref);
1103 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1104 err = got_error_from_errno("close");
1106 got_repo_close(repo);
1107 TAILQ_FOREACH(pe, &refs, entry) {
1108 free((void *)pe->path);
1111 got_pathlist_free(&refs);
1112 TAILQ_FOREACH(pe, &symrefs, entry) {
1113 free((void *)pe->path);
1116 got_pathlist_free(&symrefs);
1123 free(default_destdir);
1127 static const struct got_error *
1128 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1130 static char msg[512];
1131 const char *branch_name;
1133 if (got_ref_is_symbolic(ref))
1134 branch_name = got_ref_get_symref_target(ref);
1136 branch_name = got_ref_get_name(ref);
1138 if (strncmp("refs/heads/", branch_name, 11) == 0)
1141 snprintf(msg, sizeof(msg),
1142 "target commit is not contained in branch '%s'; "
1143 "the branch to use must be specified with -b; "
1144 "if necessary a new branch can be created for "
1145 "this commit with 'got branch -c %s BRANCH_NAME'",
1146 branch_name, commit_id_str);
1148 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1151 static const struct got_error *
1152 cmd_checkout(int argc, char *argv[])
1154 const struct got_error *error = NULL;
1155 struct got_repository *repo = NULL;
1156 struct got_reference *head_ref = NULL;
1157 struct got_worktree *worktree = NULL;
1158 char *repo_path = NULL;
1159 char *worktree_path = NULL;
1160 const char *path_prefix = "";
1161 const char *branch_name = GOT_REF_HEAD;
1162 char *commit_id_str = NULL;
1163 int ch, same_path_prefix, allow_nonempty = 0;
1164 struct got_pathlist_head paths;
1165 struct got_checkout_progress_arg cpa;
1169 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1172 branch_name = optarg;
1175 commit_id_str = strdup(optarg);
1176 if (commit_id_str == NULL)
1177 return got_error_from_errno("strdup");
1183 path_prefix = optarg;
1195 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1196 "unveil", NULL) == -1)
1200 char *cwd, *base, *dotgit;
1201 repo_path = realpath(argv[0], NULL);
1202 if (repo_path == NULL)
1203 return got_error_from_errno2("realpath", argv[0]);
1204 cwd = getcwd(NULL, 0);
1206 error = got_error_from_errno("getcwd");
1209 if (path_prefix[0]) {
1210 base = basename(path_prefix);
1212 error = got_error_from_errno2("basename",
1217 base = basename(repo_path);
1219 error = got_error_from_errno2("basename",
1224 dotgit = strstr(base, ".git");
1227 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1228 error = got_error_from_errno("asprintf");
1233 } else if (argc == 2) {
1234 repo_path = realpath(argv[0], NULL);
1235 if (repo_path == NULL) {
1236 error = got_error_from_errno2("realpath", argv[0]);
1239 worktree_path = realpath(argv[1], NULL);
1240 if (worktree_path == NULL) {
1241 if (errno != ENOENT) {
1242 error = got_error_from_errno2("realpath",
1246 worktree_path = strdup(argv[1]);
1247 if (worktree_path == NULL) {
1248 error = got_error_from_errno("strdup");
1255 got_path_strip_trailing_slashes(repo_path);
1256 got_path_strip_trailing_slashes(worktree_path);
1258 error = got_repo_open(&repo, repo_path, NULL);
1262 /* Pre-create work tree path for unveil(2) */
1263 error = got_path_mkdir(worktree_path);
1265 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1266 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1268 if (!allow_nonempty &&
1269 !got_path_dir_is_empty(worktree_path)) {
1270 error = got_error_path(worktree_path,
1271 GOT_ERR_DIR_NOT_EMPTY);
1276 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1280 error = got_ref_open(&head_ref, repo, branch_name, 0);
1284 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1285 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1288 error = got_worktree_open(&worktree, worktree_path);
1292 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1296 if (!same_path_prefix) {
1297 error = got_error(GOT_ERR_PATH_PREFIX);
1301 if (commit_id_str) {
1302 struct got_object_id *commit_id;
1303 error = got_repo_match_object_id(&commit_id, NULL,
1304 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1307 error = check_linear_ancestry(commit_id,
1308 got_worktree_get_base_commit_id(worktree), 0, repo);
1309 if (error != NULL) {
1311 if (error->code == GOT_ERR_ANCESTRY) {
1312 error = checkout_ancestry_error(
1313 head_ref, commit_id_str);
1317 error = check_same_branch(commit_id, head_ref, NULL, repo);
1319 if (error->code == GOT_ERR_ANCESTRY) {
1320 error = checkout_ancestry_error(
1321 head_ref, commit_id_str);
1325 error = got_worktree_set_base_commit_id(worktree, repo,
1332 error = got_pathlist_append(&paths, "", NULL);
1335 cpa.worktree_path = worktree_path;
1336 cpa.had_base_commit_ref_error = 0;
1337 error = got_worktree_checkout_files(worktree, &paths, repo,
1338 checkout_progress, &cpa, check_cancelled, NULL);
1342 printf("Now shut up and hack\n");
1343 if (cpa.had_base_commit_ref_error)
1344 show_worktree_base_ref_warning();
1346 got_pathlist_free(&paths);
1347 free(commit_id_str);
1349 free(worktree_path);
1356 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1361 static const struct got_error *
1362 update_progress(void *arg, unsigned char status, const char *path)
1364 int *did_something = arg;
1366 if (status == GOT_STATUS_EXISTS ||
1367 status == GOT_STATUS_BASE_REF_ERR)
1372 /* Base commit bump happens silently. */
1373 if (status == GOT_STATUS_BUMP_BASE)
1376 while (path[0] == '/')
1378 printf("%c %s\n", status, path);
1382 static const struct got_error *
1383 switch_head_ref(struct got_reference *head_ref,
1384 struct got_object_id *commit_id, struct got_worktree *worktree,
1385 struct got_repository *repo)
1387 const struct got_error *err = NULL;
1389 int ref_has_moved = 0;
1391 /* Trivial case: switching between two different references. */
1392 if (strcmp(got_ref_get_name(head_ref),
1393 got_worktree_get_head_ref_name(worktree)) != 0) {
1394 printf("Switching work tree from %s to %s\n",
1395 got_worktree_get_head_ref_name(worktree),
1396 got_ref_get_name(head_ref));
1397 return got_worktree_set_head_ref(worktree, head_ref);
1400 err = check_linear_ancestry(commit_id,
1401 got_worktree_get_base_commit_id(worktree), 0, repo);
1403 if (err->code != GOT_ERR_ANCESTRY)
1410 /* Switching to a rebased branch with the same reference name. */
1411 err = got_object_id_str(&base_id_str,
1412 got_worktree_get_base_commit_id(worktree));
1415 printf("Reference %s now points at a different branch\n",
1416 got_worktree_get_head_ref_name(worktree));
1417 printf("Switching work tree from %s to %s\n", base_id_str,
1418 got_worktree_get_head_ref_name(worktree));
1422 static const struct got_error *
1423 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1425 const struct got_error *err;
1428 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1432 return got_error(GOT_ERR_REBASING);
1434 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1438 return got_error(GOT_ERR_HISTEDIT_BUSY);
1443 static const struct got_error *
1444 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1445 char *argv[], struct got_worktree *worktree)
1447 const struct got_error *err = NULL;
1454 return got_error_from_errno("strdup");
1455 return got_pathlist_append(paths, path, NULL);
1458 for (i = 0; i < argc; i++) {
1459 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1462 err = got_pathlist_append(paths, path, NULL);
1472 static const struct got_error *
1473 cmd_update(int argc, char *argv[])
1475 const struct got_error *error = NULL;
1476 struct got_repository *repo = NULL;
1477 struct got_worktree *worktree = NULL;
1478 char *worktree_path = NULL;
1479 struct got_object_id *commit_id = NULL;
1480 char *commit_id_str = NULL;
1481 const char *branch_name = NULL;
1482 struct got_reference *head_ref = NULL;
1483 struct got_pathlist_head paths;
1484 struct got_pathlist_entry *pe;
1485 int ch, did_something = 0;
1489 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1492 branch_name = optarg;
1495 commit_id_str = strdup(optarg);
1496 if (commit_id_str == NULL)
1497 return got_error_from_errno("strdup");
1509 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1510 "unveil", NULL) == -1)
1513 worktree_path = getcwd(NULL, 0);
1514 if (worktree_path == NULL) {
1515 error = got_error_from_errno("getcwd");
1518 error = got_worktree_open(&worktree, worktree_path);
1522 error = check_rebase_or_histedit_in_progress(worktree);
1526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1531 error = apply_unveil(got_repo_get_path(repo), 0,
1532 got_worktree_get_root_path(worktree));
1536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1540 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1541 got_worktree_get_head_ref_name(worktree), 0);
1544 if (commit_id_str == NULL) {
1545 error = got_ref_resolve(&commit_id, repo, head_ref);
1548 error = got_object_id_str(&commit_id_str, commit_id);
1552 error = got_repo_match_object_id(&commit_id, NULL,
1553 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1554 free(commit_id_str);
1555 commit_id_str = NULL;
1558 error = got_object_id_str(&commit_id_str, commit_id);
1564 struct got_object_id *head_commit_id;
1565 TAILQ_FOREACH(pe, &paths, entry) {
1566 if (pe->path_len == 0)
1568 error = got_error_msg(GOT_ERR_BAD_PATH,
1569 "switching between branches requires that "
1570 "the entire work tree gets updated");
1573 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1576 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1578 free(head_commit_id);
1581 error = check_same_branch(commit_id, head_ref, NULL, repo);
1584 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1588 error = check_linear_ancestry(commit_id,
1589 got_worktree_get_base_commit_id(worktree), 0, repo);
1590 if (error != NULL) {
1591 if (error->code == GOT_ERR_ANCESTRY)
1592 error = got_error(GOT_ERR_BRANCH_MOVED);
1595 error = check_same_branch(commit_id, head_ref, NULL, repo);
1600 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1602 error = got_worktree_set_base_commit_id(worktree, repo,
1608 error = got_worktree_checkout_files(worktree, &paths, repo,
1609 update_progress, &did_something, check_cancelled, NULL);
1614 printf("Updated to commit %s\n", commit_id_str);
1616 printf("Already up-to-date\n");
1618 free(worktree_path);
1619 TAILQ_FOREACH(pe, &paths, entry)
1620 free((char *)pe->path);
1621 got_pathlist_free(&paths);
1623 free(commit_id_str);
1627 static const struct got_error *
1628 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1629 const char *path, int diff_context, int ignore_whitespace,
1630 struct got_repository *repo)
1632 const struct got_error *err = NULL;
1633 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1636 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1641 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1645 while (path[0] == '/')
1647 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1648 ignore_whitespace, stdout);
1651 got_object_blob_close(blob1);
1652 got_object_blob_close(blob2);
1656 static const struct got_error *
1657 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1658 const char *path, int diff_context, int ignore_whitespace,
1659 struct got_repository *repo)
1661 const struct got_error *err = NULL;
1662 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1663 struct got_diff_blob_output_unidiff_arg arg;
1666 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1671 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1675 arg.diff_context = diff_context;
1676 arg.ignore_whitespace = ignore_whitespace;
1677 arg.outfile = stdout;
1678 while (path[0] == '/')
1680 err = got_diff_tree(tree1, tree2, path, path, repo,
1681 got_diff_blob_output_unidiff, &arg, 1);
1684 got_object_tree_close(tree1);
1686 got_object_tree_close(tree2);
1690 static const struct got_error *
1691 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1692 const char *path, int diff_context, struct got_repository *repo)
1694 const struct got_error *err = NULL;
1695 struct got_commit_object *pcommit = NULL;
1696 char *id_str1 = NULL, *id_str2 = NULL;
1697 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1698 struct got_object_qid *qid;
1700 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1702 err = got_object_open_as_commit(&pcommit, repo,
1708 if (path && path[0] != '\0') {
1710 err = got_object_id_by_path(&obj_id2, repo, id, path);
1713 err = got_object_id_str(&id_str2, obj_id2);
1719 err = got_object_id_by_path(&obj_id1, repo,
1725 err = got_object_id_str(&id_str1, obj_id1);
1731 err = got_object_get_type(&obj_type, repo, obj_id2);
1736 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1738 case GOT_OBJ_TYPE_BLOB:
1739 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1742 case GOT_OBJ_TYPE_TREE:
1743 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1747 err = got_error(GOT_ERR_OBJ_TYPE);
1753 obj_id2 = got_object_commit_get_tree_id(commit);
1754 err = got_object_id_str(&id_str2, obj_id2);
1757 obj_id1 = got_object_commit_get_tree_id(pcommit);
1758 err = got_object_id_str(&id_str1, obj_id1);
1761 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1762 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1768 got_object_commit_close(pcommit);
1773 get_datestr(time_t *time, char *datebuf)
1775 struct tm mytm, *tm;
1778 tm = gmtime_r(time, &mytm);
1781 s = asctime_r(tm, datebuf);
1784 p = strchr(s, '\n');
1790 static const struct got_error *
1791 match_logmsg(int *have_match, struct got_object_id *id,
1792 struct got_commit_object *commit, regex_t *regex)
1794 const struct got_error *err = NULL;
1795 regmatch_t regmatch;
1796 char *id_str = NULL, *logmsg = NULL;
1800 err = got_object_id_str(&id_str, id);
1804 err = got_object_commit_get_logmsg(&logmsg, commit);
1808 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1816 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1818 static const struct got_error *
1819 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1820 struct got_repository *repo, const char *path, int show_patch,
1821 int diff_context, struct got_reflist_head *refs)
1823 const struct got_error *err = NULL;
1824 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1826 time_t committer_time;
1827 const char *author, *committer;
1828 char *refs_str = NULL;
1829 struct got_reflist_entry *re;
1831 SIMPLEQ_FOREACH(re, refs, entry) {
1834 struct got_tag_object *tag = NULL;
1837 name = got_ref_get_name(re->ref);
1838 if (strcmp(name, GOT_REF_HEAD) == 0)
1840 if (strncmp(name, "refs/", 5) == 0)
1842 if (strncmp(name, "got/", 4) == 0)
1844 if (strncmp(name, "heads/", 6) == 0)
1846 if (strncmp(name, "remotes/", 8) == 0)
1848 if (strncmp(name, "tags/", 5) == 0) {
1849 err = got_object_open_as_tag(&tag, repo, re->id);
1851 if (err->code != GOT_ERR_OBJ_TYPE)
1853 /* Ref points at something other than a tag. */
1858 cmp = got_object_id_cmp(tag ?
1859 got_object_tag_get_object_id(tag) : re->id, id);
1861 got_object_tag_close(tag);
1865 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1867 err = got_error_from_errno("asprintf");
1873 err = got_object_id_str(&id_str, id);
1877 printf(GOT_COMMIT_SEP_STR);
1878 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1879 refs_str ? refs_str : "", refs_str ? ")" : "");
1884 printf("from: %s\n", got_object_commit_get_author(commit));
1885 committer_time = got_object_commit_get_committer_time(commit);
1886 datestr = get_datestr(&committer_time, datebuf);
1888 printf("date: %s UTC\n", datestr);
1889 author = got_object_commit_get_author(commit);
1890 committer = got_object_commit_get_committer(commit);
1891 if (strcmp(author, committer) != 0)
1892 printf("via: %s\n", committer);
1893 if (got_object_commit_get_nparents(commit) > 1) {
1894 const struct got_object_id_queue *parent_ids;
1895 struct got_object_qid *qid;
1897 parent_ids = got_object_commit_get_parent_ids(commit);
1898 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1899 err = got_object_id_str(&id_str, qid->id);
1902 printf("parent %d: %s\n", n++, id_str);
1907 err = got_object_commit_get_logmsg(&logmsg0, commit);
1913 line = strsep(&logmsg, "\n");
1915 printf(" %s\n", line);
1920 err = print_patch(commit, id, path, diff_context, repo);
1925 if (fflush(stdout) != 0 && err == NULL)
1926 err = got_error_from_errno("fflush");
1930 static const struct got_error *
1931 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1932 const char *path, int show_patch, const char *search_pattern,
1933 int diff_context, int limit, int log_branches,
1934 struct got_reflist_head *refs)
1936 const struct got_error *err;
1937 struct got_commit_graph *graph;
1941 if (search_pattern &&
1942 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1943 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1945 err = got_commit_graph_open(&graph, path, !log_branches);
1948 err = got_commit_graph_iter_start(graph, root_id, repo,
1949 check_cancelled, NULL);
1953 struct got_commit_object *commit;
1954 struct got_object_id *id;
1956 if (sigint_received || sigpipe_received)
1959 err = got_commit_graph_iter_next(&id, graph, repo,
1960 check_cancelled, NULL);
1962 if (err->code == GOT_ERR_ITER_COMPLETED)
1969 err = got_object_open_as_commit(&commit, repo, id);
1973 if (search_pattern) {
1974 err = match_logmsg(&have_match, id, commit, ®ex);
1976 got_object_commit_close(commit);
1979 if (have_match == 0) {
1980 got_object_commit_close(commit);
1985 err = print_commit(commit, id, repo, path, show_patch,
1986 diff_context, refs);
1987 got_object_commit_close(commit);
1988 if (err || (limit && --limit == 0))
1994 got_commit_graph_close(graph);
2001 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2002 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2007 get_default_log_limit(void)
2009 const char *got_default_log_limit;
2013 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2014 if (got_default_log_limit == NULL)
2016 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2022 static const struct got_error *
2023 cmd_log(int argc, char *argv[])
2025 const struct got_error *error;
2026 struct got_repository *repo = NULL;
2027 struct got_worktree *worktree = NULL;
2028 struct got_commit_object *commit = NULL;
2029 struct got_object_id *id = NULL;
2030 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2031 const char *start_commit = NULL, *search_pattern = NULL;
2032 int diff_context = -1, ch;
2033 int show_patch = 0, limit = 0, log_branches = 0;
2035 struct got_reflist_head refs;
2037 SIMPLEQ_INIT(&refs);
2040 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2046 limit = get_default_log_limit();
2048 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2054 start_commit = optarg;
2057 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2060 err(1, "-C option %s", errstr);
2063 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2065 err(1, "-l option %s", errstr);
2071 repo_path = realpath(optarg, NULL);
2072 if (repo_path == NULL)
2073 return got_error_from_errno2("realpath",
2075 got_path_strip_trailing_slashes(repo_path);
2078 search_pattern = optarg;
2089 if (diff_context == -1)
2091 else if (!show_patch)
2092 errx(1, "-C reguires -p");
2094 cwd = getcwd(NULL, 0);
2096 error = got_error_from_errno("getcwd");
2100 error = got_worktree_open(&worktree, cwd);
2101 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2108 error = got_error_from_errno("strdup");
2111 } else if (argc == 1) {
2113 error = got_worktree_resolve_path(&path, worktree,
2118 path = strdup(argv[0]);
2120 error = got_error_from_errno("strdup");
2127 if (repo_path == NULL) {
2128 repo_path = worktree ?
2129 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2131 if (repo_path == NULL) {
2132 error = got_error_from_errno("strdup");
2136 error = got_repo_open(&repo, repo_path, NULL);
2140 error = apply_unveil(got_repo_get_path(repo), 1,
2141 worktree ? got_worktree_get_root_path(worktree) : NULL);
2145 if (start_commit == NULL) {
2146 struct got_reference *head_ref;
2147 error = got_ref_open(&head_ref, repo,
2148 worktree ? got_worktree_get_head_ref_name(worktree)
2152 error = got_ref_resolve(&id, repo, head_ref);
2153 got_ref_close(head_ref);
2156 error = got_object_open_as_commit(&commit, repo, id);
2158 struct got_reference *ref;
2159 error = got_ref_open(&ref, repo, start_commit, 0);
2160 if (error == NULL) {
2162 error = got_ref_resolve(&id, repo, ref);
2166 error = got_object_get_type(&obj_type, repo, id);
2169 if (obj_type == GOT_OBJ_TYPE_TAG) {
2170 struct got_tag_object *tag;
2171 error = got_object_open_as_tag(&tag, repo, id);
2174 if (got_object_tag_get_object_type(tag) !=
2175 GOT_OBJ_TYPE_COMMIT) {
2176 got_object_tag_close(tag);
2177 error = got_error(GOT_ERR_OBJ_TYPE);
2181 id = got_object_id_dup(
2182 got_object_tag_get_object_id(tag));
2184 error = got_error_from_errno(
2185 "got_object_id_dup");
2186 got_object_tag_close(tag);
2189 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2190 error = got_error(GOT_ERR_OBJ_TYPE);
2193 error = got_object_open_as_commit(&commit, repo, id);
2197 if (commit == NULL) {
2198 error = got_repo_match_object_id_prefix(&id,
2199 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2208 const char *prefix = got_worktree_get_path_prefix(worktree);
2210 if (asprintf(&p, "%s%s%s", prefix,
2211 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2212 error = got_error_from_errno("asprintf");
2215 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2218 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2223 path = in_repo_path;
2226 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2230 error = print_commits(id, repo, path, show_patch, search_pattern,
2231 diff_context, limit, log_branches, &refs);
2238 got_worktree_close(worktree);
2240 const struct got_error *repo_error;
2241 repo_error = got_repo_close(repo);
2245 got_ref_list_free(&refs);
2252 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2253 "[-w] [object1 object2 | path]\n", getprogname());
2257 struct print_diff_arg {
2258 struct got_repository *repo;
2259 struct got_worktree *worktree;
2264 int ignore_whitespace;
2267 static const struct got_error *
2268 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2269 const char *path, struct got_object_id *blob_id,
2270 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2271 int dirfd, const char *de_name)
2273 struct print_diff_arg *a = arg;
2274 const struct got_error *err = NULL;
2275 struct got_blob_object *blob1 = NULL;
2278 char *abspath = NULL, *label1 = NULL;
2281 if (a->diff_staged) {
2282 if (staged_status != GOT_STATUS_MODIFY &&
2283 staged_status != GOT_STATUS_ADD &&
2284 staged_status != GOT_STATUS_DELETE)
2287 if (staged_status == GOT_STATUS_DELETE)
2289 if (status == GOT_STATUS_NONEXISTENT)
2290 return got_error_set_errno(ENOENT, path);
2291 if (status != GOT_STATUS_MODIFY &&
2292 status != GOT_STATUS_ADD &&
2293 status != GOT_STATUS_DELETE &&
2294 status != GOT_STATUS_CONFLICT)
2298 if (!a->header_shown) {
2299 printf("diff %s %s%s\n", a->id_str,
2300 got_worktree_get_root_path(a->worktree),
2301 a->diff_staged ? " (staged changes)" : "");
2302 a->header_shown = 1;
2305 if (a->diff_staged) {
2306 const char *label1 = NULL, *label2 = NULL;
2307 switch (staged_status) {
2308 case GOT_STATUS_MODIFY:
2312 case GOT_STATUS_ADD:
2315 case GOT_STATUS_DELETE:
2319 return got_error(GOT_ERR_FILE_STATUS);
2321 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2322 label1, label2, a->diff_context, a->ignore_whitespace,
2326 if (staged_status == GOT_STATUS_ADD ||
2327 staged_status == GOT_STATUS_MODIFY) {
2329 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2333 err = got_object_id_str(&id_str, staged_blob_id);
2336 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2337 err = got_error_from_errno("asprintf");
2342 } else if (status != GOT_STATUS_ADD) {
2343 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2348 if (status != GOT_STATUS_DELETE) {
2349 if (asprintf(&abspath, "%s/%s",
2350 got_worktree_get_root_path(a->worktree), path) == -1) {
2351 err = got_error_from_errno("asprintf");
2356 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2358 err = got_error_from_errno2("openat", abspath);
2362 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2364 err = got_error_from_errno2("open", abspath);
2368 if (fstat(fd, &sb) == -1) {
2369 err = got_error_from_errno2("fstat", abspath);
2372 f2 = fdopen(fd, "r");
2374 err = got_error_from_errno2("fdopen", abspath);
2381 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2382 a->diff_context, a->ignore_whitespace, stdout);
2385 got_object_blob_close(blob1);
2386 if (f2 && fclose(f2) == EOF && err == NULL)
2387 err = got_error_from_errno("fclose");
2388 if (fd != -1 && close(fd) == -1 && err == NULL)
2389 err = got_error_from_errno("close");
2394 static const struct got_error *
2395 cmd_diff(int argc, char *argv[])
2397 const struct got_error *error;
2398 struct got_repository *repo = NULL;
2399 struct got_worktree *worktree = NULL;
2400 char *cwd = NULL, *repo_path = NULL;
2401 struct got_object_id *id1 = NULL, *id2 = NULL;
2402 const char *id_str1 = NULL, *id_str2 = NULL;
2403 char *label1 = NULL, *label2 = NULL;
2405 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2410 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2415 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2418 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2421 err(1, "-C option %s", errstr);
2424 repo_path = realpath(optarg, NULL);
2425 if (repo_path == NULL)
2426 return got_error_from_errno2("realpath",
2428 got_path_strip_trailing_slashes(repo_path);
2434 ignore_whitespace = 1;
2445 cwd = getcwd(NULL, 0);
2447 error = got_error_from_errno("getcwd");
2450 error = got_worktree_open(&worktree, cwd);
2451 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2454 if (worktree == NULL) {
2455 error = got_error(GOT_ERR_NOT_WORKTREE);
2460 "-r option can't be used when diffing a work tree");
2461 repo_path = strdup(got_worktree_get_repo_path(worktree));
2462 if (repo_path == NULL) {
2463 error = got_error_from_errno("strdup");
2467 error = got_worktree_resolve_path(&path, worktree,
2474 error = got_error_from_errno("strdup");
2478 } else if (argc == 2) {
2480 errx(1, "-s option can't be used when diffing "
2481 "objects in repository");
2484 if (worktree && repo_path == NULL) {
2486 strdup(got_worktree_get_repo_path(worktree));
2487 if (repo_path == NULL) {
2488 error = got_error_from_errno("strdup");
2495 if (repo_path == NULL) {
2496 repo_path = getcwd(NULL, 0);
2497 if (repo_path == NULL)
2498 return got_error_from_errno("getcwd");
2501 error = got_repo_open(&repo, repo_path, NULL);
2506 error = apply_unveil(got_repo_get_path(repo), 1,
2507 worktree ? got_worktree_get_root_path(worktree) : NULL);
2512 struct print_diff_arg arg;
2513 struct got_pathlist_head paths;
2518 error = got_object_id_str(&id_str,
2519 got_worktree_get_base_commit_id(worktree));
2523 arg.worktree = worktree;
2524 arg.diff_context = diff_context;
2525 arg.id_str = id_str;
2526 arg.header_shown = 0;
2527 arg.diff_staged = diff_staged;
2528 arg.ignore_whitespace = ignore_whitespace;
2530 error = got_pathlist_append(&paths, path, NULL);
2534 error = got_worktree_status(worktree, &paths, repo, print_diff,
2535 &arg, check_cancelled, NULL);
2537 got_pathlist_free(&paths);
2541 error = got_repo_match_object_id(&id1, &label1, id_str1,
2542 GOT_OBJ_TYPE_ANY, 1, repo);
2546 error = got_repo_match_object_id(&id2, &label2, id_str2,
2547 GOT_OBJ_TYPE_ANY, 1, repo);
2551 error = got_object_get_type(&type1, repo, id1);
2555 error = got_object_get_type(&type2, repo, id2);
2559 if (type1 != type2) {
2560 error = got_error(GOT_ERR_OBJ_TYPE);
2565 case GOT_OBJ_TYPE_BLOB:
2566 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2567 diff_context, ignore_whitespace, repo, stdout);
2569 case GOT_OBJ_TYPE_TREE:
2570 error = got_diff_objects_as_trees(id1, id2, "", "",
2571 diff_context, ignore_whitespace, repo, stdout);
2573 case GOT_OBJ_TYPE_COMMIT:
2574 printf("diff %s %s\n", label1, label2);
2575 error = got_diff_objects_as_commits(id1, id2, diff_context,
2576 ignore_whitespace, repo, stdout);
2579 error = got_error(GOT_ERR_OBJ_TYPE);
2588 got_worktree_close(worktree);
2590 const struct got_error *repo_error;
2591 repo_error = got_repo_close(repo);
2602 "usage: %s blame [-c commit] [-r repository-path] path\n",
2611 char datebuf[11]; /* YYYY-MM-DD + NUL */
2614 struct blame_cb_args {
2615 struct blame_line *lines;
2619 off_t *line_offsets;
2621 struct got_repository *repo;
2624 static const struct got_error *
2625 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2627 const struct got_error *err = NULL;
2628 struct blame_cb_args *a = arg;
2629 struct blame_line *bline;
2631 size_t linesize = 0;
2632 struct got_commit_object *commit = NULL;
2635 time_t committer_time;
2637 if (nlines != a->nlines ||
2638 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2639 return got_error(GOT_ERR_RANGE);
2641 if (sigint_received)
2642 return got_error(GOT_ERR_ITER_COMPLETED);
2645 return NULL; /* no change in this commit */
2647 /* Annotate this line. */
2648 bline = &a->lines[lineno - 1];
2649 if (bline->annotated)
2651 err = got_object_id_str(&bline->id_str, id);
2655 err = got_object_open_as_commit(&commit, a->repo, id);
2659 bline->committer = strdup(got_object_commit_get_committer(commit));
2660 if (bline->committer == NULL) {
2661 err = got_error_from_errno("strdup");
2665 committer_time = got_object_commit_get_committer_time(commit);
2666 if (localtime_r(&committer_time, &tm) == NULL)
2667 return got_error_from_errno("localtime_r");
2668 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2669 &tm) >= sizeof(bline->datebuf)) {
2670 err = got_error(GOT_ERR_NO_SPACE);
2673 bline->annotated = 1;
2675 /* Print lines annotated so far. */
2676 bline = &a->lines[a->lineno_cur - 1];
2677 if (!bline->annotated)
2680 offset = a->line_offsets[a->lineno_cur - 1];
2681 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2682 err = got_error_from_errno("fseeko");
2686 while (bline->annotated) {
2687 char *smallerthan, *at, *nl, *committer;
2690 if (getline(&line, &linesize, a->f) == -1) {
2692 err = got_error_from_errno("getline");
2696 committer = bline->committer;
2697 smallerthan = strchr(committer, '<');
2698 if (smallerthan && smallerthan[1] != '\0')
2699 committer = smallerthan + 1;
2700 at = strchr(committer, '@');
2703 len = strlen(committer);
2705 committer[8] = '\0';
2707 nl = strchr(line, '\n');
2710 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2711 bline->id_str, bline->datebuf, committer, line);
2714 bline = &a->lines[a->lineno_cur - 1];
2718 got_object_commit_close(commit);
2723 static const struct got_error *
2724 cmd_blame(int argc, char *argv[])
2726 const struct got_error *error;
2727 struct got_repository *repo = NULL;
2728 struct got_worktree *worktree = NULL;
2729 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2730 struct got_object_id *obj_id = NULL;
2731 struct got_object_id *commit_id = NULL;
2732 struct got_blob_object *blob = NULL;
2733 char *commit_id_str = NULL;
2734 struct blame_cb_args bca;
2735 int ch, obj_type, i;
2738 memset(&bca, 0, sizeof(bca));
2741 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2746 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2749 commit_id_str = optarg;
2752 repo_path = realpath(optarg, NULL);
2753 if (repo_path == NULL)
2754 return got_error_from_errno2("realpath",
2756 got_path_strip_trailing_slashes(repo_path);
2772 cwd = getcwd(NULL, 0);
2774 error = got_error_from_errno("getcwd");
2777 if (repo_path == NULL) {
2778 error = got_worktree_open(&worktree, cwd);
2779 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2785 strdup(got_worktree_get_repo_path(worktree));
2786 if (repo_path == NULL)
2787 error = got_error_from_errno("strdup");
2791 repo_path = strdup(cwd);
2792 if (repo_path == NULL) {
2793 error = got_error_from_errno("strdup");
2799 error = got_repo_open(&repo, repo_path, NULL);
2803 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2808 const char *prefix = got_worktree_get_path_prefix(worktree);
2809 char *p, *worktree_subdir = cwd +
2810 strlen(got_worktree_get_root_path(worktree));
2811 if (asprintf(&p, "%s%s%s%s%s",
2812 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2813 worktree_subdir, worktree_subdir[0] ? "/" : "",
2815 error = got_error_from_errno("asprintf");
2818 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2821 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2826 if (commit_id_str == NULL) {
2827 struct got_reference *head_ref;
2828 error = got_ref_open(&head_ref, repo, worktree ?
2829 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2832 error = got_ref_resolve(&commit_id, repo, head_ref);
2833 got_ref_close(head_ref);
2837 error = got_repo_match_object_id(&commit_id, NULL,
2838 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2843 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2847 error = got_object_get_type(&obj_type, repo, obj_id);
2851 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2852 error = got_error(GOT_ERR_OBJ_TYPE);
2856 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2859 bca.f = got_opentemp();
2860 if (bca.f == NULL) {
2861 error = got_error_from_errno("got_opentemp");
2864 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2865 &bca.line_offsets, bca.f, blob);
2866 if (error || bca.nlines == 0)
2869 /* Don't include \n at EOF in the blame line count. */
2870 if (bca.line_offsets[bca.nlines - 1] == filesize)
2873 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2874 if (bca.lines == NULL) {
2875 error = got_error_from_errno("calloc");
2879 bca.nlines_prec = 0;
2887 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2888 check_cancelled, NULL);
2896 got_object_blob_close(blob);
2898 got_worktree_close(worktree);
2900 const struct got_error *repo_error;
2901 repo_error = got_repo_close(repo);
2906 for (i = 0; i < bca.nlines; i++) {
2907 struct blame_line *bline = &bca.lines[i];
2908 free(bline->id_str);
2909 free(bline->committer);
2913 free(bca.line_offsets);
2914 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2915 error = got_error_from_errno("fclose");
2923 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2929 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2930 const char *root_path)
2932 int is_root_path = (strcmp(path, root_path) == 0);
2933 const char *modestr = "";
2934 mode_t mode = got_tree_entry_get_mode(te);
2936 path += strlen(root_path);
2937 while (path[0] == '/')
2940 if (got_object_tree_entry_is_submodule(te))
2942 else if (S_ISLNK(mode))
2944 else if (S_ISDIR(mode))
2946 else if (mode & S_IXUSR)
2949 printf("%s%s%s%s%s\n", id ? id : "", path,
2950 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2953 static const struct got_error *
2954 print_tree(const char *path, struct got_object_id *commit_id,
2955 int show_ids, int recurse, const char *root_path,
2956 struct got_repository *repo)
2958 const struct got_error *err = NULL;
2959 struct got_object_id *tree_id = NULL;
2960 struct got_tree_object *tree = NULL;
2963 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2967 err = got_object_open_as_tree(&tree, repo, tree_id);
2970 nentries = got_object_tree_get_nentries(tree);
2971 for (i = 0; i < nentries; i++) {
2972 struct got_tree_entry *te;
2975 if (sigint_received || sigpipe_received)
2978 te = got_object_tree_get_entry(tree, i);
2981 err = got_object_id_str(&id_str,
2982 got_tree_entry_get_id(te));
2985 if (asprintf(&id, "%s ", id_str) == -1) {
2986 err = got_error_from_errno("asprintf");
2992 print_entry(te, id, path, root_path);
2995 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2997 if (asprintf(&child_path, "%s%s%s", path,
2998 path[0] == '/' && path[1] == '\0' ? "" : "/",
2999 got_tree_entry_get_name(te)) == -1) {
3000 err = got_error_from_errno("asprintf");
3003 err = print_tree(child_path, commit_id, show_ids, 1,
3012 got_object_tree_close(tree);
3017 static const struct got_error *
3018 cmd_tree(int argc, char *argv[])
3020 const struct got_error *error;
3021 struct got_repository *repo = NULL;
3022 struct got_worktree *worktree = NULL;
3024 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3025 struct got_object_id *commit_id = NULL;
3026 char *commit_id_str = NULL;
3027 int show_ids = 0, recurse = 0;
3031 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3036 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3039 commit_id_str = optarg;
3042 repo_path = realpath(optarg, NULL);
3043 if (repo_path == NULL)
3044 return got_error_from_errno2("realpath",
3046 got_path_strip_trailing_slashes(repo_path);
3070 cwd = getcwd(NULL, 0);
3072 error = got_error_from_errno("getcwd");
3075 if (repo_path == NULL) {
3076 error = got_worktree_open(&worktree, cwd);
3077 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3083 strdup(got_worktree_get_repo_path(worktree));
3084 if (repo_path == NULL)
3085 error = got_error_from_errno("strdup");
3089 repo_path = strdup(cwd);
3090 if (repo_path == NULL) {
3091 error = got_error_from_errno("strdup");
3097 error = got_repo_open(&repo, repo_path, NULL);
3101 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3107 char *p, *worktree_subdir = cwd +
3108 strlen(got_worktree_get_root_path(worktree));
3109 if (asprintf(&p, "%s/%s",
3110 got_worktree_get_path_prefix(worktree),
3111 worktree_subdir) == -1) {
3112 error = got_error_from_errno("asprintf");
3115 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3122 if (in_repo_path == NULL) {
3123 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3128 if (commit_id_str == NULL) {
3129 struct got_reference *head_ref;
3130 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3133 error = got_ref_resolve(&commit_id, repo, head_ref);
3134 got_ref_close(head_ref);
3138 error = got_repo_match_object_id(&commit_id, NULL,
3139 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3144 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3145 in_repo_path, repo);
3152 got_worktree_close(worktree);
3154 const struct got_error *repo_error;
3155 repo_error = got_repo_close(repo);
3165 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3169 static const struct got_error *
3170 print_status(void *arg, unsigned char status, unsigned char staged_status,
3171 const char *path, struct got_object_id *blob_id,
3172 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3173 int dirfd, const char *de_name)
3175 if (status == staged_status && (status == GOT_STATUS_DELETE))
3176 status = GOT_STATUS_NO_CHANGE;
3177 printf("%c%c %s\n", status, staged_status, path);
3181 static const struct got_error *
3182 cmd_status(int argc, char *argv[])
3184 const struct got_error *error = NULL;
3185 struct got_repository *repo = NULL;
3186 struct got_worktree *worktree = NULL;
3188 struct got_pathlist_head paths;
3189 struct got_pathlist_entry *pe;
3194 while ((ch = getopt(argc, argv, "")) != -1) {
3206 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3210 cwd = getcwd(NULL, 0);
3212 error = got_error_from_errno("getcwd");
3216 error = got_worktree_open(&worktree, cwd);
3220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3225 error = apply_unveil(got_repo_get_path(repo), 1,
3226 got_worktree_get_root_path(worktree));
3230 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3234 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3235 check_cancelled, NULL);
3237 TAILQ_FOREACH(pe, &paths, entry)
3238 free((char *)pe->path);
3239 got_pathlist_free(&paths);
3248 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3253 static const struct got_error *
3254 list_refs(struct got_repository *repo)
3256 static const struct got_error *err = NULL;
3257 struct got_reflist_head refs;
3258 struct got_reflist_entry *re;
3260 SIMPLEQ_INIT(&refs);
3261 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3265 SIMPLEQ_FOREACH(re, &refs, entry) {
3267 refstr = got_ref_to_str(re->ref);
3269 return got_error_from_errno("got_ref_to_str");
3270 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3274 got_ref_list_free(&refs);
3278 static const struct got_error *
3279 delete_ref(struct got_repository *repo, const char *refname)
3281 const struct got_error *err = NULL;
3282 struct got_reference *ref;
3284 err = got_ref_open(&ref, repo, refname, 0);
3288 err = got_ref_delete(ref, repo);
3293 static const struct got_error *
3294 add_ref(struct got_repository *repo, const char *refname, const char *target)
3296 const struct got_error *err = NULL;
3297 struct got_object_id *id;
3298 struct got_reference *ref = NULL;
3301 * Don't let the user create a reference name with a leading '-'.
3302 * While technically a valid reference name, this case is usually
3303 * an unintended typo.
3305 if (refname[0] == '-')
3306 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3308 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3311 struct got_reference *target_ref;
3313 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3315 err = got_ref_open(&target_ref, repo, target, 0);
3318 err = got_ref_resolve(&id, repo, target_ref);
3319 got_ref_close(target_ref);
3324 err = got_ref_alloc(&ref, refname, id);
3328 err = got_ref_write(ref, repo);
3336 static const struct got_error *
3337 add_symref(struct got_repository *repo, const char *refname, const char *target)
3339 const struct got_error *err = NULL;
3340 struct got_reference *ref = NULL;
3341 struct got_reference *target_ref = NULL;
3344 * Don't let the user create a reference name with a leading '-'.
3345 * While technically a valid reference name, this case is usually
3346 * an unintended typo.
3348 if (refname[0] == '-')
3349 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3351 err = got_ref_open(&target_ref, repo, target, 0);
3355 err = got_ref_alloc_symref(&ref, refname, target_ref);
3359 err = got_ref_write(ref, repo);
3362 got_ref_close(target_ref);
3368 static const struct got_error *
3369 cmd_ref(int argc, char *argv[])
3371 const struct got_error *error = NULL;
3372 struct got_repository *repo = NULL;
3373 struct got_worktree *worktree = NULL;
3374 char *cwd = NULL, *repo_path = NULL;
3375 int ch, do_list = 0, create_symref = 0;
3376 const char *delref = NULL;
3378 /* TODO: Add -s option for adding symbolic references. */
3379 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3385 repo_path = realpath(optarg, NULL);
3386 if (repo_path == NULL)
3387 return got_error_from_errno2("realpath",
3389 got_path_strip_trailing_slashes(repo_path);
3403 if (do_list && delref)
3404 errx(1, "-l and -d options are mutually exclusive\n");
3409 if (do_list || delref) {
3411 errx(1, "-s option cannot be used together with the "
3412 "-l or -d options");
3415 } else if (argc != 2)
3420 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3424 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3425 "sendfd unveil", NULL) == -1)
3429 cwd = getcwd(NULL, 0);
3431 error = got_error_from_errno("getcwd");
3435 if (repo_path == NULL) {
3436 error = got_worktree_open(&worktree, cwd);
3437 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3443 strdup(got_worktree_get_repo_path(worktree));
3444 if (repo_path == NULL)
3445 error = got_error_from_errno("strdup");
3449 repo_path = strdup(cwd);
3450 if (repo_path == NULL) {
3451 error = got_error_from_errno("strdup");
3457 error = got_repo_open(&repo, repo_path, NULL);
3461 error = apply_unveil(got_repo_get_path(repo), do_list,
3462 worktree ? got_worktree_get_root_path(worktree) : NULL);
3467 error = list_refs(repo);
3469 error = delete_ref(repo, delref);
3470 else if (create_symref)
3471 error = add_symref(repo, argv[0], argv[1]);
3473 error = add_ref(repo, argv[0], argv[1]);
3476 got_repo_close(repo);
3478 got_worktree_close(worktree);
3488 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3489 "[name]\n", getprogname());
3493 static const struct got_error *
3494 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3495 struct got_reference *ref)
3497 const struct got_error *err = NULL;
3498 const char *refname, *marker = " ";
3501 refname = got_ref_get_name(ref);
3502 if (worktree && strcmp(refname,
3503 got_worktree_get_head_ref_name(worktree)) == 0) {
3504 struct got_object_id *id = NULL;
3506 err = got_ref_resolve(&id, repo, ref);
3509 if (got_object_id_cmp(id,
3510 got_worktree_get_base_commit_id(worktree)) == 0)
3517 if (strncmp(refname, "refs/heads/", 11) == 0)
3519 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3522 refstr = got_ref_to_str(ref);
3524 return got_error_from_errno("got_ref_to_str");
3526 printf("%s%s: %s\n", marker, refname, refstr);
3531 static const struct got_error *
3532 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3534 const char *refname;
3536 if (worktree == NULL)
3537 return got_error(GOT_ERR_NOT_WORKTREE);
3539 refname = got_worktree_get_head_ref_name(worktree);
3541 if (strncmp(refname, "refs/heads/", 11) == 0)
3543 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3546 printf("%s\n", refname);
3551 static const struct got_error *
3552 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3554 static const struct got_error *err = NULL;
3555 struct got_reflist_head refs;
3556 struct got_reflist_entry *re;
3557 struct got_reference *temp_ref = NULL;
3558 int rebase_in_progress, histedit_in_progress;
3560 SIMPLEQ_INIT(&refs);
3563 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3568 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3573 if (rebase_in_progress || histedit_in_progress) {
3574 err = got_ref_open(&temp_ref, repo,
3575 got_worktree_get_head_ref_name(worktree), 0);
3578 list_branch(repo, worktree, temp_ref);
3579 got_ref_close(temp_ref);
3583 err = got_ref_list(&refs, repo, "refs/heads",
3584 got_ref_cmp_by_name, NULL);
3588 SIMPLEQ_FOREACH(re, &refs, entry)
3589 list_branch(repo, worktree, re->ref);
3591 got_ref_list_free(&refs);
3595 static const struct got_error *
3596 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3597 const char *branch_name)
3599 const struct got_error *err = NULL;
3600 struct got_reference *ref = NULL;
3603 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3604 return got_error_from_errno("asprintf");
3606 err = got_ref_open(&ref, repo, refname, 0);
3611 strcmp(got_worktree_get_head_ref_name(worktree),
3612 got_ref_get_name(ref)) == 0) {
3613 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3614 "will not delete this work tree's current branch");
3618 err = got_ref_delete(ref, repo);
3626 static const struct got_error *
3627 add_branch(struct got_repository *repo, const char *branch_name,
3628 struct got_object_id *base_commit_id)
3630 const struct got_error *err = NULL;
3631 struct got_reference *ref = NULL;
3632 char *base_refname = NULL, *refname = NULL;
3635 * Don't let the user create a branch name with a leading '-'.
3636 * While technically a valid reference name, this case is usually
3637 * an unintended typo.
3639 if (branch_name[0] == '-')
3640 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3642 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3643 err = got_error_from_errno("asprintf");
3647 err = got_ref_open(&ref, repo, refname, 0);
3649 err = got_error(GOT_ERR_BRANCH_EXISTS);
3651 } else if (err->code != GOT_ERR_NOT_REF)
3654 err = got_ref_alloc(&ref, refname, base_commit_id);
3658 err = got_ref_write(ref, repo);
3667 static const struct got_error *
3668 cmd_branch(int argc, char *argv[])
3670 const struct got_error *error = NULL;
3671 struct got_repository *repo = NULL;
3672 struct got_worktree *worktree = NULL;
3673 char *cwd = NULL, *repo_path = NULL;
3674 int ch, do_list = 0, do_show = 0, do_update = 1;
3675 const char *delref = NULL, *commit_id_arg = NULL;
3676 struct got_reference *ref = NULL;
3677 struct got_pathlist_head paths;
3678 struct got_pathlist_entry *pe;
3679 struct got_object_id *commit_id = NULL;
3680 char *commit_id_str = NULL;
3684 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3687 commit_id_arg = optarg;
3693 repo_path = realpath(optarg, NULL);
3694 if (repo_path == NULL)
3695 return got_error_from_errno2("realpath",
3697 got_path_strip_trailing_slashes(repo_path);
3711 if (do_list && delref)
3712 errx(1, "-l and -d options are mutually exclusive\n");
3717 if (!do_list && !delref && argc == 0)
3720 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3721 errx(1, "-c option can only be used when creating a branch");
3723 if (do_list || delref) {
3726 } else if (!do_show && argc != 1)
3730 if (do_list || do_show) {
3731 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3735 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3736 "sendfd unveil", NULL) == -1)
3740 cwd = getcwd(NULL, 0);
3742 error = got_error_from_errno("getcwd");
3746 if (repo_path == NULL) {
3747 error = got_worktree_open(&worktree, cwd);
3748 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3754 strdup(got_worktree_get_repo_path(worktree));
3755 if (repo_path == NULL)
3756 error = got_error_from_errno("strdup");
3760 repo_path = strdup(cwd);
3761 if (repo_path == NULL) {
3762 error = got_error_from_errno("strdup");
3768 error = got_repo_open(&repo, repo_path, NULL);
3772 error = apply_unveil(got_repo_get_path(repo), do_list,
3773 worktree ? got_worktree_get_root_path(worktree) : NULL);
3778 error = show_current_branch(repo, worktree);
3780 error = list_branches(repo, worktree);
3782 error = delete_branch(repo, worktree, delref);
3784 if (commit_id_arg == NULL)
3785 commit_id_arg = worktree ?
3786 got_worktree_get_head_ref_name(worktree) :
3788 error = got_repo_match_object_id(&commit_id, NULL,
3789 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3792 error = add_branch(repo, argv[0], commit_id);
3795 if (worktree && do_update) {
3796 int did_something = 0;
3797 char *branch_refname = NULL;
3799 error = got_object_id_str(&commit_id_str, commit_id);
3802 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3806 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3808 error = got_error_from_errno("asprintf");
3811 error = got_ref_open(&ref, repo, branch_refname, 0);
3812 free(branch_refname);
3815 error = switch_head_ref(ref, commit_id, worktree,
3819 error = got_worktree_set_base_commit_id(worktree, repo,
3823 error = got_worktree_checkout_files(worktree, &paths,
3824 repo, update_progress, &did_something,
3825 check_cancelled, NULL);
3829 printf("Updated to commit %s\n", commit_id_str);
3836 got_repo_close(repo);
3838 got_worktree_close(worktree);
3842 free(commit_id_str);
3843 TAILQ_FOREACH(pe, &paths, entry)
3844 free((char *)pe->path);
3845 got_pathlist_free(&paths);
3854 "usage: %s tag [-c commit] [-r repository] [-l] "
3855 "[-m message] name\n", getprogname());
3860 static const struct got_error *
3861 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3863 const struct got_error *err = NULL;
3864 struct got_reflist_entry *re, *se, *new;
3865 struct got_object_id *re_id, *se_id;
3866 struct got_tag_object *re_tag, *se_tag;
3867 time_t re_time, se_time;
3869 SIMPLEQ_FOREACH(re, tags, entry) {
3870 se = SIMPLEQ_FIRST(sorted);
3872 err = got_reflist_entry_dup(&new, re);
3875 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3878 err = got_ref_resolve(&re_id, repo, re->ref);
3881 err = got_object_open_as_tag(&re_tag, repo, re_id);
3885 re_time = got_object_tag_get_tagger_time(re_tag);
3886 got_object_tag_close(re_tag);
3890 err = got_ref_resolve(&se_id, repo, re->ref);
3893 err = got_object_open_as_tag(&se_tag, repo, se_id);
3897 se_time = got_object_tag_get_tagger_time(se_tag);
3898 got_object_tag_close(se_tag);
3900 if (se_time > re_time) {
3901 err = got_reflist_entry_dup(&new, re);
3904 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3907 se = SIMPLEQ_NEXT(se, entry);
3916 static const struct got_error *
3917 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3919 static const struct got_error *err = NULL;
3920 struct got_reflist_head refs;
3921 struct got_reflist_entry *re;
3923 SIMPLEQ_INIT(&refs);
3925 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3929 SIMPLEQ_FOREACH(re, &refs, entry) {
3930 const char *refname;
3931 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3935 struct got_object_id *id;
3936 struct got_tag_object *tag;
3937 struct got_commit_object *commit = NULL;
3939 refname = got_ref_get_name(re->ref);
3940 if (strncmp(refname, "refs/tags/", 10) != 0)
3943 refstr = got_ref_to_str(re->ref);
3944 if (refstr == NULL) {
3945 err = got_error_from_errno("got_ref_to_str");
3948 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3951 err = got_ref_resolve(&id, repo, re->ref);
3954 err = got_object_open_as_tag(&tag, repo, id);
3956 if (err->code != GOT_ERR_OBJ_TYPE) {
3960 /* "lightweight" tag */
3961 err = got_object_open_as_commit(&commit, repo, id);
3966 tagger = got_object_commit_get_committer(commit);
3968 got_object_commit_get_committer_time(commit);
3969 err = got_object_id_str(&id_str, id);
3975 tagger = got_object_tag_get_tagger(tag);
3976 tagger_time = got_object_tag_get_tagger_time(tag);
3977 err = got_object_id_str(&id_str,
3978 got_object_tag_get_object_id(tag));
3982 printf("from: %s\n", tagger);
3983 datestr = get_datestr(&tagger_time, datebuf);
3985 printf("date: %s UTC\n", datestr);
3987 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3989 switch (got_object_tag_get_object_type(tag)) {
3990 case GOT_OBJ_TYPE_BLOB:
3991 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3994 case GOT_OBJ_TYPE_TREE:
3995 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3998 case GOT_OBJ_TYPE_COMMIT:
3999 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4002 case GOT_OBJ_TYPE_TAG:
4003 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4012 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4015 got_object_commit_close(commit);
4017 tagmsg0 = strdup(got_object_tag_get_message(tag));
4018 got_object_tag_close(tag);
4019 if (tagmsg0 == NULL) {
4020 err = got_error_from_errno("strdup");
4027 line = strsep(&tagmsg, "\n");
4029 printf(" %s\n", line);
4034 got_ref_list_free(&refs);
4038 static const struct got_error *
4039 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4040 const char *tag_name, const char *repo_path)
4042 const struct got_error *err = NULL;
4043 char *template = NULL, *initial_content = NULL;
4044 char *editor = NULL;
4047 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4048 err = got_error_from_errno("asprintf");
4052 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4053 commit_id_str, tag_name) == -1) {
4054 err = got_error_from_errno("asprintf");
4058 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4062 dprintf(fd, initial_content);
4065 err = get_editor(&editor);
4068 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4070 free(initial_content);
4074 /* Editor is done; we can now apply unveil(2) */
4076 err = apply_unveil(repo_path, 0, NULL);
4085 static const struct got_error *
4086 add_tag(struct got_repository *repo, const char *tag_name,
4087 const char *commit_arg, const char *tagmsg_arg)
4089 const struct got_error *err = NULL;
4090 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4091 char *label = NULL, *commit_id_str = NULL;
4092 struct got_reference *ref = NULL;
4093 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4094 char *tagmsg_path = NULL, *tag_id_str = NULL;
4095 int preserve_tagmsg = 0;
4098 * Don't let the user create a tag name with a leading '-'.
4099 * While technically a valid reference name, this case is usually
4100 * an unintended typo.
4102 if (tag_name[0] == '-')
4103 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4105 err = get_author(&tagger, repo);
4109 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4110 GOT_OBJ_TYPE_COMMIT, 1, repo);
4114 err = got_object_id_str(&commit_id_str, commit_id);
4118 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4119 refname = strdup(tag_name);
4120 if (refname == NULL) {
4121 err = got_error_from_errno("strdup");
4125 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4126 err = got_error_from_errno("asprintf");
4130 err = got_ref_open(&ref, repo, refname, 0);
4132 err = got_error(GOT_ERR_TAG_EXISTS);
4134 } else if (err->code != GOT_ERR_NOT_REF)
4137 if (tagmsg_arg == NULL) {
4138 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4139 tag_name, got_repo_get_path(repo));
4141 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4142 tagmsg_path != NULL)
4143 preserve_tagmsg = 1;
4148 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4149 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4152 preserve_tagmsg = 1;
4156 err = got_ref_alloc(&ref, refname, tag_id);
4159 preserve_tagmsg = 1;
4163 err = got_ref_write(ref, repo);
4166 preserve_tagmsg = 1;
4170 err = got_object_id_str(&tag_id_str, tag_id);
4173 preserve_tagmsg = 1;
4176 printf("Created tag %s\n", tag_id_str);
4178 if (preserve_tagmsg) {
4179 fprintf(stderr, "%s: tag message preserved in %s\n",
4180 getprogname(), tagmsg_path);
4181 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4182 err = got_error_from_errno2("unlink", tagmsg_path);
4187 free(commit_id_str);
4195 static const struct got_error *
4196 cmd_tag(int argc, char *argv[])
4198 const struct got_error *error = NULL;
4199 struct got_repository *repo = NULL;
4200 struct got_worktree *worktree = NULL;
4201 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4202 char *gitconfig_path = NULL;
4203 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4204 int ch, do_list = 0;
4206 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4209 commit_id_arg = optarg;
4215 repo_path = realpath(optarg, NULL);
4216 if (repo_path == NULL)
4217 return got_error_from_errno2("realpath",
4219 got_path_strip_trailing_slashes(repo_path);
4234 if (commit_id_arg != NULL)
4235 errx(1, "-c option can only be used when creating a tag");
4237 errx(1, "-l and -m options are mutually exclusive");
4240 } else if (argc != 1)
4247 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4251 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4252 "sendfd unveil", NULL) == -1)
4256 cwd = getcwd(NULL, 0);
4258 error = got_error_from_errno("getcwd");
4262 if (repo_path == NULL) {
4263 error = got_worktree_open(&worktree, cwd);
4264 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4270 strdup(got_worktree_get_repo_path(worktree));
4271 if (repo_path == NULL)
4272 error = got_error_from_errno("strdup");
4276 repo_path = strdup(cwd);
4277 if (repo_path == NULL) {
4278 error = got_error_from_errno("strdup");
4285 error = got_repo_open(&repo, repo_path, NULL);
4288 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4291 error = list_tags(repo, worktree);
4293 error = get_gitconfig_path(&gitconfig_path);
4296 error = got_repo_open(&repo, repo_path, gitconfig_path);
4301 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4306 if (commit_id_arg == NULL) {
4307 struct got_reference *head_ref;
4308 struct got_object_id *commit_id;
4309 error = got_ref_open(&head_ref, repo,
4310 worktree ? got_worktree_get_head_ref_name(worktree)
4314 error = got_ref_resolve(&commit_id, repo, head_ref);
4315 got_ref_close(head_ref);
4318 error = got_object_id_str(&commit_id_str, commit_id);
4324 error = add_tag(repo, tag_name,
4325 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4329 got_repo_close(repo);
4331 got_worktree_close(worktree);
4334 free(gitconfig_path);
4335 free(commit_id_str);
4342 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4347 static const struct got_error *
4348 add_progress(void *arg, unsigned char status, const char *path)
4350 while (path[0] == '/')
4352 printf("%c %s\n", status, path);
4356 static const struct got_error *
4357 cmd_add(int argc, char *argv[])
4359 const struct got_error *error = NULL;
4360 struct got_repository *repo = NULL;
4361 struct got_worktree *worktree = NULL;
4363 struct got_pathlist_head paths;
4364 struct got_pathlist_entry *pe;
4365 int ch, can_recurse = 0, no_ignores = 0;
4369 while ((ch = getopt(argc, argv, "IR")) != -1) {
4387 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4394 cwd = getcwd(NULL, 0);
4396 error = got_error_from_errno("getcwd");
4400 error = got_worktree_open(&worktree, cwd);
4404 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4409 error = apply_unveil(got_repo_get_path(repo), 1,
4410 got_worktree_get_root_path(worktree));
4414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4418 if (!can_recurse && no_ignores) {
4419 error = got_error_msg(GOT_ERR_BAD_PATH,
4420 "disregarding ignores requires -R option");
4428 TAILQ_FOREACH(pe, &paths, entry) {
4429 if (asprintf(&ondisk_path, "%s/%s",
4430 got_worktree_get_root_path(worktree),
4432 error = got_error_from_errno("asprintf");
4435 if (lstat(ondisk_path, &sb) == -1) {
4436 if (errno == ENOENT) {
4440 error = got_error_from_errno2("lstat",
4446 if (S_ISDIR(sb.st_mode)) {
4447 error = got_error_msg(GOT_ERR_BAD_PATH,
4448 "adding directories requires -R option");
4454 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4455 NULL, repo, no_ignores);
4458 got_repo_close(repo);
4460 got_worktree_close(worktree);
4461 TAILQ_FOREACH(pe, &paths, entry)
4462 free((char *)pe->path);
4463 got_pathlist_free(&paths);
4471 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4476 static const struct got_error *
4477 print_remove_status(void *arg, unsigned char status,
4478 unsigned char staged_status, const char *path)
4480 while (path[0] == '/')
4482 if (status == GOT_STATUS_NONEXISTENT)
4484 if (status == staged_status && (status == GOT_STATUS_DELETE))
4485 status = GOT_STATUS_NO_CHANGE;
4486 printf("%c%c %s\n", status, staged_status, path);
4490 static const struct got_error *
4491 cmd_remove(int argc, char *argv[])
4493 const struct got_error *error = NULL;
4494 struct got_worktree *worktree = NULL;
4495 struct got_repository *repo = NULL;
4497 struct got_pathlist_head paths;
4498 struct got_pathlist_entry *pe;
4499 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4503 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4506 delete_local_mods = 1;
4524 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4531 cwd = getcwd(NULL, 0);
4533 error = got_error_from_errno("getcwd");
4536 error = got_worktree_open(&worktree, cwd);
4540 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4545 error = apply_unveil(got_repo_get_path(repo), 1,
4546 got_worktree_get_root_path(worktree));
4550 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4557 TAILQ_FOREACH(pe, &paths, entry) {
4558 if (asprintf(&ondisk_path, "%s/%s",
4559 got_worktree_get_root_path(worktree),
4561 error = got_error_from_errno("asprintf");
4564 if (lstat(ondisk_path, &sb) == -1) {
4565 if (errno == ENOENT) {
4569 error = got_error_from_errno2("lstat",
4575 if (S_ISDIR(sb.st_mode)) {
4576 error = got_error_msg(GOT_ERR_BAD_PATH,
4577 "removing directories requires -R option");
4583 error = got_worktree_schedule_delete(worktree, &paths,
4584 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4587 got_repo_close(repo);
4589 got_worktree_close(worktree);
4590 TAILQ_FOREACH(pe, &paths, entry)
4591 free((char *)pe->path);
4592 got_pathlist_free(&paths);
4600 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4601 "path ...\n", getprogname());
4605 static const struct got_error *
4606 revert_progress(void *arg, unsigned char status, const char *path)
4608 if (status == GOT_STATUS_UNVERSIONED)
4611 while (path[0] == '/')
4613 printf("%c %s\n", status, path);
4617 struct choose_patch_arg {
4618 FILE *patch_script_file;
4622 static const struct got_error *
4623 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4624 int nchanges, const char *action)
4627 size_t linesize = 0;
4631 case GOT_STATUS_ADD:
4632 printf("A %s\n%s this addition? [y/n] ", path, action);
4634 case GOT_STATUS_DELETE:
4635 printf("D %s\n%s this deletion? [y/n] ", path, action);
4637 case GOT_STATUS_MODIFY:
4638 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4639 return got_error_from_errno("fseek");
4640 printf(GOT_COMMIT_SEP_STR);
4641 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4643 if (ferror(patch_file))
4644 return got_error_from_errno("getline");
4645 printf(GOT_COMMIT_SEP_STR);
4646 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4647 path, n, nchanges, action);
4650 return got_error_path(path, GOT_ERR_FILE_STATUS);
4656 static const struct got_error *
4657 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4658 FILE *patch_file, int n, int nchanges)
4660 const struct got_error *err = NULL;
4662 size_t linesize = 0;
4665 struct choose_patch_arg *a = arg;
4667 *choice = GOT_PATCH_CHOICE_NONE;
4669 if (a->patch_script_file) {
4671 err = show_change(status, path, patch_file, n, nchanges,
4675 linelen = getline(&line, &linesize, a->patch_script_file);
4676 if (linelen == -1) {
4677 if (ferror(a->patch_script_file))
4678 return got_error_from_errno("getline");
4681 nl = strchr(line, '\n');
4684 if (strcmp(line, "y") == 0) {
4685 *choice = GOT_PATCH_CHOICE_YES;
4687 } else if (strcmp(line, "n") == 0) {
4688 *choice = GOT_PATCH_CHOICE_NO;
4690 } else if (strcmp(line, "q") == 0 &&
4691 status == GOT_STATUS_MODIFY) {
4692 *choice = GOT_PATCH_CHOICE_QUIT;
4695 printf("invalid response '%s'\n", line);
4700 while (resp != 'y' && resp != 'n' && resp != 'q') {
4701 err = show_change(status, path, patch_file, n, nchanges,
4708 if (status == GOT_STATUS_MODIFY) {
4709 if (resp != 'y' && resp != 'n' && resp != 'q') {
4710 printf("invalid response '%c'\n", resp);
4713 } else if (resp != 'y' && resp != 'n') {
4714 printf("invalid response '%c'\n", resp);
4720 *choice = GOT_PATCH_CHOICE_YES;
4721 else if (resp == 'n')
4722 *choice = GOT_PATCH_CHOICE_NO;
4723 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4724 *choice = GOT_PATCH_CHOICE_QUIT;
4730 static const struct got_error *
4731 cmd_revert(int argc, char *argv[])
4733 const struct got_error *error = NULL;
4734 struct got_worktree *worktree = NULL;
4735 struct got_repository *repo = NULL;
4736 char *cwd = NULL, *path = NULL;
4737 struct got_pathlist_head paths;
4738 struct got_pathlist_entry *pe;
4739 int ch, can_recurse = 0, pflag = 0;
4740 FILE *patch_script_file = NULL;
4741 const char *patch_script_path = NULL;
4742 struct choose_patch_arg cpa;
4746 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4752 patch_script_path = optarg;
4767 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4768 "unveil", NULL) == -1)
4773 if (patch_script_path && !pflag)
4774 errx(1, "-F option can only be used together with -p option");
4776 cwd = getcwd(NULL, 0);
4778 error = got_error_from_errno("getcwd");
4781 error = got_worktree_open(&worktree, cwd);
4785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4790 if (patch_script_path) {
4791 patch_script_file = fopen(patch_script_path, "r");
4792 if (patch_script_file == NULL) {
4793 error = got_error_from_errno2("fopen",
4798 error = apply_unveil(got_repo_get_path(repo), 1,
4799 got_worktree_get_root_path(worktree));
4803 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4810 TAILQ_FOREACH(pe, &paths, entry) {
4811 if (asprintf(&ondisk_path, "%s/%s",
4812 got_worktree_get_root_path(worktree),
4814 error = got_error_from_errno("asprintf");
4817 if (lstat(ondisk_path, &sb) == -1) {
4818 if (errno == ENOENT) {
4822 error = got_error_from_errno2("lstat",
4828 if (S_ISDIR(sb.st_mode)) {
4829 error = got_error_msg(GOT_ERR_BAD_PATH,
4830 "reverting directories requires -R option");
4836 cpa.patch_script_file = patch_script_file;
4837 cpa.action = "revert";
4838 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4839 pflag ? choose_patch : NULL, &cpa, repo);
4841 if (patch_script_file && fclose(patch_script_file) == EOF &&
4843 error = got_error_from_errno2("fclose", patch_script_path);
4845 got_repo_close(repo);
4847 got_worktree_close(worktree);
4856 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4861 struct collect_commit_logmsg_arg {
4862 const char *cmdline_log;
4864 const char *worktree_path;
4865 const char *branch_name;
4866 const char *repo_path;
4871 static const struct got_error *
4872 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4875 char *initial_content = NULL;
4876 struct got_pathlist_entry *pe;
4877 const struct got_error *err = NULL;
4878 char *template = NULL;
4879 struct collect_commit_logmsg_arg *a = arg;
4883 /* if a message was specified on the command line, just use it */
4884 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4885 len = strlen(a->cmdline_log) + 1;
4886 *logmsg = malloc(len + 1);
4887 if (*logmsg == NULL)
4888 return got_error_from_errno("malloc");
4889 strlcpy(*logmsg, a->cmdline_log, len);
4893 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4894 return got_error_from_errno("asprintf");
4896 if (asprintf(&initial_content,
4897 "\n# changes to be committed on branch %s:\n",
4898 a->branch_name) == -1)
4899 return got_error_from_errno("asprintf");
4901 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4905 dprintf(fd, initial_content);
4907 TAILQ_FOREACH(pe, commitable_paths, entry) {
4908 struct got_commitable *ct = pe->data;
4909 dprintf(fd, "# %c %s\n",
4910 got_commitable_get_status(ct),
4911 got_commitable_get_path(ct));
4915 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4917 free(initial_content);
4920 /* Editor is done; we can now apply unveil(2) */
4922 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4931 static const struct got_error *
4932 cmd_commit(int argc, char *argv[])
4934 const struct got_error *error = NULL;
4935 struct got_worktree *worktree = NULL;
4936 struct got_repository *repo = NULL;
4937 char *cwd = NULL, *id_str = NULL;
4938 struct got_object_id *id = NULL;
4939 const char *logmsg = NULL;
4940 struct collect_commit_logmsg_arg cl_arg;
4941 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4942 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4943 struct got_pathlist_head paths;
4946 cl_arg.logmsg_path = NULL;
4948 while ((ch = getopt(argc, argv, "m:")) != -1) {
4963 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4964 "unveil", NULL) == -1)
4967 cwd = getcwd(NULL, 0);
4969 error = got_error_from_errno("getcwd");
4972 error = got_worktree_open(&worktree, cwd);
4976 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4979 if (rebase_in_progress) {
4980 error = got_error(GOT_ERR_REBASING);
4984 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4989 error = get_gitconfig_path(&gitconfig_path);
4992 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4997 error = get_author(&author, repo);
5002 * unveil(2) traverses exec(2); if an editor is used we have
5003 * to apply unveil after the log message has been written.
5005 if (logmsg == NULL || strlen(logmsg) == 0)
5006 error = get_editor(&editor);
5008 error = apply_unveil(got_repo_get_path(repo), 0,
5009 got_worktree_get_root_path(worktree));
5013 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5017 cl_arg.editor = editor;
5018 cl_arg.cmdline_log = logmsg;
5019 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5020 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5021 if (!histedit_in_progress) {
5022 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5023 error = got_error(GOT_ERR_COMMIT_BRANCH);
5026 cl_arg.branch_name += 11;
5028 cl_arg.repo_path = got_repo_get_path(repo);
5029 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5030 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5032 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5033 cl_arg.logmsg_path != NULL)
5034 preserve_logmsg = 1;
5038 error = got_object_id_str(&id_str, id);
5041 printf("Created commit %s\n", id_str);
5043 if (preserve_logmsg) {
5044 fprintf(stderr, "%s: log message preserved in %s\n",
5045 getprogname(), cl_arg.logmsg_path);
5046 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5048 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5049 free(cl_arg.logmsg_path);
5051 got_repo_close(repo);
5053 got_worktree_close(worktree);
5056 free(gitconfig_path);
5063 usage_cherrypick(void)
5065 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5069 static const struct got_error *
5070 cmd_cherrypick(int argc, char *argv[])
5072 const struct got_error *error = NULL;
5073 struct got_worktree *worktree = NULL;
5074 struct got_repository *repo = NULL;
5075 char *cwd = NULL, *commit_id_str = NULL;
5076 struct got_object_id *commit_id = NULL;
5077 struct got_commit_object *commit = NULL;
5078 struct got_object_qid *pid;
5079 struct got_reference *head_ref = NULL;
5080 int ch, did_something = 0;
5082 while ((ch = getopt(argc, argv, "")) != -1) {
5094 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5095 "unveil", NULL) == -1)
5101 cwd = getcwd(NULL, 0);
5103 error = got_error_from_errno("getcwd");
5106 error = got_worktree_open(&worktree, cwd);
5110 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5115 error = apply_unveil(got_repo_get_path(repo), 0,
5116 got_worktree_get_root_path(worktree));
5120 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5121 GOT_OBJ_TYPE_COMMIT, repo);
5122 if (error != NULL) {
5123 struct got_reference *ref;
5124 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5126 error = got_ref_open(&ref, repo, argv[0], 0);
5129 error = got_ref_resolve(&commit_id, repo, ref);
5134 error = got_object_id_str(&commit_id_str, commit_id);
5138 error = got_ref_open(&head_ref, repo,
5139 got_worktree_get_head_ref_name(worktree), 0);
5143 error = check_same_branch(commit_id, head_ref, NULL, repo);
5145 if (error->code != GOT_ERR_ANCESTRY)
5149 error = got_error(GOT_ERR_SAME_BRANCH);
5153 error = got_object_open_as_commit(&commit, repo, commit_id);
5156 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5157 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5158 commit_id, repo, update_progress, &did_something, check_cancelled,
5164 printf("Merged commit %s\n", commit_id_str);
5167 got_object_commit_close(commit);
5168 free(commit_id_str);
5170 got_ref_close(head_ref);
5172 got_worktree_close(worktree);
5174 got_repo_close(repo);
5181 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5185 static const struct got_error *
5186 cmd_backout(int argc, char *argv[])
5188 const struct got_error *error = NULL;
5189 struct got_worktree *worktree = NULL;
5190 struct got_repository *repo = NULL;
5191 char *cwd = NULL, *commit_id_str = NULL;
5192 struct got_object_id *commit_id = NULL;
5193 struct got_commit_object *commit = NULL;
5194 struct got_object_qid *pid;
5195 struct got_reference *head_ref = NULL;
5196 int ch, did_something = 0;
5198 while ((ch = getopt(argc, argv, "")) != -1) {
5210 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5211 "unveil", NULL) == -1)
5217 cwd = getcwd(NULL, 0);
5219 error = got_error_from_errno("getcwd");
5222 error = got_worktree_open(&worktree, cwd);
5226 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5231 error = apply_unveil(got_repo_get_path(repo), 0,
5232 got_worktree_get_root_path(worktree));
5236 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5237 GOT_OBJ_TYPE_COMMIT, repo);
5238 if (error != NULL) {
5239 struct got_reference *ref;
5240 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5242 error = got_ref_open(&ref, repo, argv[0], 0);
5245 error = got_ref_resolve(&commit_id, repo, ref);
5250 error = got_object_id_str(&commit_id_str, commit_id);
5254 error = got_ref_open(&head_ref, repo,
5255 got_worktree_get_head_ref_name(worktree), 0);
5259 error = check_same_branch(commit_id, head_ref, NULL, repo);
5263 error = got_object_open_as_commit(&commit, repo, commit_id);
5266 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5268 error = got_error(GOT_ERR_ROOT_COMMIT);
5272 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5273 update_progress, &did_something, check_cancelled, NULL);
5278 printf("Backed out commit %s\n", commit_id_str);
5281 got_object_commit_close(commit);
5282 free(commit_id_str);
5284 got_ref_close(head_ref);
5286 got_worktree_close(worktree);
5288 got_repo_close(repo);
5295 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5301 trim_logmsg(char *logmsg, int limit)
5306 len = strlen(logmsg);
5310 nl = strchr(logmsg, '\n');
5315 static const struct got_error *
5316 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5318 const struct got_error *err;
5319 char *logmsg0 = NULL;
5322 err = got_object_commit_get_logmsg(&logmsg0, commit);
5327 while (isspace((unsigned char)s[0]))
5330 *logmsg = strdup(s);
5331 if (*logmsg == NULL) {
5332 err = got_error_from_errno("strdup");
5336 trim_logmsg(*logmsg, limit);
5342 static const struct got_error *
5343 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5345 const struct got_error *err;
5346 struct got_commit_object *commit = NULL;
5347 char *id_str = NULL, *logmsg = NULL;
5349 err = got_object_open_as_commit(&commit, repo, id);
5353 err = got_object_id_str(&id_str, id);
5359 err = get_short_logmsg(&logmsg, 42, commit);
5363 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5366 got_object_commit_close(commit);
5371 static const struct got_error *
5372 show_rebase_progress(struct got_commit_object *commit,
5373 struct got_object_id *old_id, struct got_object_id *new_id)
5375 const struct got_error *err;
5376 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5378 err = got_object_id_str(&old_id_str, old_id);
5383 err = got_object_id_str(&new_id_str, new_id);
5388 old_id_str[12] = '\0';
5390 new_id_str[12] = '\0';
5392 err = get_short_logmsg(&logmsg, 42, commit);
5396 printf("%s -> %s: %s\n", old_id_str,
5397 new_id_str ? new_id_str : "no-op change", logmsg);
5405 static const struct got_error *
5406 rebase_progress(void *arg, unsigned char status, const char *path)
5408 unsigned char *rebase_status = arg;
5410 while (path[0] == '/')
5412 printf("%c %s\n", status, path);
5414 if (*rebase_status == GOT_STATUS_CONFLICT)
5416 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5417 *rebase_status = status;
5421 static const struct got_error *
5422 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5423 struct got_reference *branch, struct got_reference *new_base_branch,
5424 struct got_reference *tmp_branch, struct got_repository *repo)
5426 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5427 return got_worktree_rebase_complete(worktree, fileindex,
5428 new_base_branch, tmp_branch, branch, repo);
5431 static const struct got_error *
5432 rebase_commit(struct got_pathlist_head *merged_paths,
5433 struct got_worktree *worktree, struct got_fileindex *fileindex,
5434 struct got_reference *tmp_branch,
5435 struct got_object_id *commit_id, struct got_repository *repo)
5437 const struct got_error *error;
5438 struct got_commit_object *commit;
5439 struct got_object_id *new_commit_id;
5441 error = got_object_open_as_commit(&commit, repo, commit_id);
5445 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5446 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5448 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5450 error = show_rebase_progress(commit, commit_id, NULL);
5452 error = show_rebase_progress(commit, commit_id, new_commit_id);
5453 free(new_commit_id);
5456 got_object_commit_close(commit);
5460 struct check_path_prefix_arg {
5461 const char *path_prefix;
5466 static const struct got_error *
5467 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5468 struct got_blob_object *blob2, struct got_object_id *id1,
5469 struct got_object_id *id2, const char *path1, const char *path2,
5470 mode_t mode1, mode_t mode2, struct got_repository *repo)
5472 struct check_path_prefix_arg *a = arg;
5474 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5475 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5476 return got_error(a->errcode);
5481 static const struct got_error *
5482 check_path_prefix(struct got_object_id *parent_id,
5483 struct got_object_id *commit_id, const char *path_prefix,
5484 int errcode, struct got_repository *repo)
5486 const struct got_error *err;
5487 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5488 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5489 struct check_path_prefix_arg cpp_arg;
5491 if (got_path_is_root_dir(path_prefix))
5494 err = got_object_open_as_commit(&commit, repo, commit_id);
5498 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5502 err = got_object_open_as_tree(&tree1, repo,
5503 got_object_commit_get_tree_id(parent_commit));
5507 err = got_object_open_as_tree(&tree2, repo,
5508 got_object_commit_get_tree_id(commit));
5512 cpp_arg.path_prefix = path_prefix;
5513 while (cpp_arg.path_prefix[0] == '/')
5514 cpp_arg.path_prefix++;
5515 cpp_arg.len = strlen(cpp_arg.path_prefix);
5516 cpp_arg.errcode = errcode;
5517 err = got_diff_tree(tree1, tree2, "", "", repo,
5518 check_path_prefix_in_diff, &cpp_arg, 0);
5521 got_object_tree_close(tree1);
5523 got_object_tree_close(tree2);
5525 got_object_commit_close(commit);
5527 got_object_commit_close(parent_commit);
5531 static const struct got_error *
5532 collect_commits(struct got_object_id_queue *commits,
5533 struct got_object_id *initial_commit_id,
5534 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5535 const char *path_prefix, int path_prefix_errcode,
5536 struct got_repository *repo)
5538 const struct got_error *err = NULL;
5539 struct got_commit_graph *graph = NULL;
5540 struct got_object_id *parent_id = NULL;
5541 struct got_object_qid *qid;
5542 struct got_object_id *commit_id = initial_commit_id;
5544 err = got_commit_graph_open(&graph, "/", 1);
5548 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5549 check_cancelled, NULL);
5552 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5553 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5554 check_cancelled, NULL);
5556 if (err->code == GOT_ERR_ITER_COMPLETED) {
5557 err = got_error_msg(GOT_ERR_ANCESTRY,
5558 "ran out of commits to rebase before "
5559 "youngest common ancestor commit has "
5564 err = check_path_prefix(parent_id, commit_id,
5565 path_prefix, path_prefix_errcode, repo);
5569 err = got_object_qid_alloc(&qid, commit_id);
5572 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5573 commit_id = parent_id;
5577 got_commit_graph_close(graph);
5581 static const struct got_error *
5582 cmd_rebase(int argc, char *argv[])
5584 const struct got_error *error = NULL;
5585 struct got_worktree *worktree = NULL;
5586 struct got_repository *repo = NULL;
5587 struct got_fileindex *fileindex = NULL;
5589 struct got_reference *branch = NULL;
5590 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5591 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5592 struct got_object_id *resume_commit_id = NULL;
5593 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5594 struct got_commit_object *commit = NULL;
5595 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5596 int histedit_in_progress = 0;
5597 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5598 struct got_object_id_queue commits;
5599 struct got_pathlist_head merged_paths;
5600 const struct got_object_id_queue *parent_ids;
5601 struct got_object_qid *qid, *pid;
5603 SIMPLEQ_INIT(&commits);
5604 TAILQ_INIT(&merged_paths);
5606 while ((ch = getopt(argc, argv, "ac")) != -1) {
5612 continue_rebase = 1;
5624 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5625 "unveil", NULL) == -1)
5628 if (abort_rebase && continue_rebase)
5630 else if (abort_rebase || continue_rebase) {
5633 } else if (argc != 1)
5636 cwd = getcwd(NULL, 0);
5638 error = got_error_from_errno("getcwd");
5641 error = got_worktree_open(&worktree, cwd);
5645 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5650 error = apply_unveil(got_repo_get_path(repo), 0,
5651 got_worktree_get_root_path(worktree));
5655 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5659 if (histedit_in_progress) {
5660 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5664 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5670 if (!rebase_in_progress) {
5671 error = got_error(GOT_ERR_NOT_REBASING);
5674 error = got_worktree_rebase_continue(&resume_commit_id,
5675 &new_base_branch, &tmp_branch, &branch, &fileindex,
5679 printf("Switching work tree to %s\n",
5680 got_ref_get_symref_target(new_base_branch));
5681 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5682 new_base_branch, update_progress, &did_something);
5685 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5686 goto done; /* nothing else to do */
5689 if (continue_rebase) {
5690 if (!rebase_in_progress) {
5691 error = got_error(GOT_ERR_NOT_REBASING);
5694 error = got_worktree_rebase_continue(&resume_commit_id,
5695 &new_base_branch, &tmp_branch, &branch, &fileindex,
5700 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5701 resume_commit_id, repo);
5705 yca_id = got_object_id_dup(resume_commit_id);
5706 if (yca_id == NULL) {
5707 error = got_error_from_errno("got_object_id_dup");
5711 error = got_ref_open(&branch, repo, argv[0], 0);
5716 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5720 if (!continue_rebase) {
5721 struct got_object_id *base_commit_id;
5723 base_commit_id = got_worktree_get_base_commit_id(worktree);
5724 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5725 base_commit_id, branch_head_commit_id, repo,
5726 check_cancelled, NULL);
5729 if (yca_id == NULL) {
5730 error = got_error_msg(GOT_ERR_ANCESTRY,
5731 "specified branch shares no common ancestry "
5732 "with work tree's branch");
5736 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5738 if (error->code != GOT_ERR_ANCESTRY)
5742 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5743 "specified branch resolves to a commit which "
5744 "is already contained in work tree's branch");
5747 error = got_worktree_rebase_prepare(&new_base_branch,
5748 &tmp_branch, &fileindex, worktree, branch, repo);
5753 commit_id = branch_head_commit_id;
5754 error = got_object_open_as_commit(&commit, repo, commit_id);
5758 parent_ids = got_object_commit_get_parent_ids(commit);
5759 pid = SIMPLEQ_FIRST(parent_ids);
5761 if (!continue_rebase) {
5763 error = got_worktree_rebase_abort(worktree, fileindex,
5764 repo, new_base_branch, update_progress,
5768 printf("Rebase of %s aborted\n",
5769 got_ref_get_name(branch));
5771 error = got_error(GOT_ERR_EMPTY_REBASE);
5774 error = collect_commits(&commits, commit_id, pid->id,
5775 yca_id, got_worktree_get_path_prefix(worktree),
5776 GOT_ERR_REBASE_PATH, repo);
5777 got_object_commit_close(commit);
5782 if (SIMPLEQ_EMPTY(&commits)) {
5783 if (continue_rebase) {
5784 error = rebase_complete(worktree, fileindex,
5785 branch, new_base_branch, tmp_branch, repo);
5788 /* Fast-forward the reference of the branch. */
5789 struct got_object_id *new_head_commit_id;
5791 error = got_ref_resolve(&new_head_commit_id, repo,
5795 error = got_object_id_str(&id_str, new_head_commit_id);
5796 printf("Forwarding %s to commit %s\n",
5797 got_ref_get_name(branch), id_str);
5799 error = got_ref_change_ref(branch,
5800 new_head_commit_id);
5807 SIMPLEQ_FOREACH(qid, &commits, entry) {
5808 commit_id = qid->id;
5809 parent_id = pid ? pid->id : yca_id;
5812 error = got_worktree_rebase_merge_files(&merged_paths,
5813 worktree, fileindex, parent_id, commit_id, repo,
5814 rebase_progress, &rebase_status, check_cancelled, NULL);
5818 if (rebase_status == GOT_STATUS_CONFLICT) {
5819 error = show_rebase_merge_conflict(qid->id, repo);
5822 got_worktree_rebase_pathlist_free(&merged_paths);
5826 error = rebase_commit(&merged_paths, worktree, fileindex,
5827 tmp_branch, commit_id, repo);
5828 got_worktree_rebase_pathlist_free(&merged_paths);
5833 if (rebase_status == GOT_STATUS_CONFLICT) {
5834 error = got_worktree_rebase_postpone(worktree, fileindex);
5837 error = got_error_msg(GOT_ERR_CONFLICTS,
5838 "conflicts must be resolved before rebasing can continue");
5840 error = rebase_complete(worktree, fileindex, branch,
5841 new_base_branch, tmp_branch, repo);
5843 got_object_id_queue_free(&commits);
5844 free(branch_head_commit_id);
5845 free(resume_commit_id);
5848 got_object_commit_close(commit);
5850 got_ref_close(branch);
5851 if (new_base_branch)
5852 got_ref_close(new_base_branch);
5854 got_ref_close(tmp_branch);
5856 got_worktree_close(worktree);
5858 got_repo_close(repo);
5863 usage_histedit(void)
5865 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5870 #define GOT_HISTEDIT_PICK 'p'
5871 #define GOT_HISTEDIT_EDIT 'e'
5872 #define GOT_HISTEDIT_FOLD 'f'
5873 #define GOT_HISTEDIT_DROP 'd'
5874 #define GOT_HISTEDIT_MESG 'm'
5876 static struct got_histedit_cmd {
5880 } got_histedit_cmds[] = {
5881 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5882 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5883 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5885 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5886 { GOT_HISTEDIT_MESG, "mesg",
5887 "single-line log message for commit above (open editor if empty)" },
5890 struct got_histedit_list_entry {
5891 TAILQ_ENTRY(got_histedit_list_entry) entry;
5892 struct got_object_id *commit_id;
5893 const struct got_histedit_cmd *cmd;
5896 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5898 static const struct got_error *
5899 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5900 FILE *f, struct got_repository *repo)
5902 const struct got_error *err = NULL;
5903 char *logmsg = NULL, *id_str = NULL;
5904 struct got_commit_object *commit = NULL;
5907 err = got_object_open_as_commit(&commit, repo, commit_id);
5911 err = get_short_logmsg(&logmsg, 34, commit);
5915 err = got_object_id_str(&id_str, commit_id);
5919 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5921 err = got_ferror(f, GOT_ERR_IO);
5924 got_object_commit_close(commit);
5930 static const struct got_error *
5931 histedit_write_commit_list(struct got_object_id_queue *commits,
5932 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5934 const struct got_error *err = NULL;
5935 struct got_object_qid *qid;
5937 if (SIMPLEQ_EMPTY(commits))
5938 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5940 SIMPLEQ_FOREACH(qid, commits, entry) {
5941 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5945 if (edit_logmsg_only) {
5946 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5948 err = got_ferror(f, GOT_ERR_IO);
5957 static const struct got_error *
5958 write_cmd_list(FILE *f, const char *branch_name,
5959 struct got_object_id_queue *commits)
5961 const struct got_error *err = NULL;
5964 struct got_object_qid *qid;
5966 qid = SIMPLEQ_FIRST(commits);
5967 err = got_object_id_str(&id_str, qid->id);
5972 "# Editing the history of branch '%s' starting at\n"
5974 "# Commits will be processed in order from top to "
5975 "bottom of this file.\n", branch_name, id_str);
5977 err = got_ferror(f, GOT_ERR_IO);
5981 n = fprintf(f, "# Available histedit commands:\n");
5983 err = got_ferror(f, GOT_ERR_IO);
5987 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5988 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5989 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5992 err = got_ferror(f, GOT_ERR_IO);
6001 static const struct got_error *
6002 histedit_syntax_error(int lineno)
6004 static char msg[42];
6007 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6009 if (ret == -1 || ret >= sizeof(msg))
6010 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6012 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6015 static const struct got_error *
6016 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6017 char *logmsg, struct got_repository *repo)
6019 const struct got_error *err;
6020 struct got_commit_object *folded_commit = NULL;
6021 char *id_str, *folded_logmsg = NULL;
6023 err = got_object_id_str(&id_str, hle->commit_id);
6027 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6031 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6034 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6035 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6036 folded_logmsg) == -1) {
6037 err = got_error_from_errno("asprintf");
6041 got_object_commit_close(folded_commit);
6043 free(folded_logmsg);
6047 static struct got_histedit_list_entry *
6048 get_folded_commits(struct got_histedit_list_entry *hle)
6050 struct got_histedit_list_entry *prev, *folded = NULL;
6052 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6053 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6054 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6055 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6057 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6063 static const struct got_error *
6064 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6065 struct got_repository *repo)
6067 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6068 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6069 const struct got_error *err = NULL;
6070 struct got_commit_object *commit = NULL;
6072 struct got_histedit_list_entry *folded = NULL;
6074 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6078 folded = get_folded_commits(hle);
6080 while (folded != hle) {
6081 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6082 folded = TAILQ_NEXT(folded, entry);
6085 err = append_folded_commit_msg(&new_msg, folded,
6091 folded = TAILQ_NEXT(folded, entry);
6095 err = got_object_id_str(&id_str, hle->commit_id);
6098 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6101 if (asprintf(&new_msg,
6102 "%s\n# original log message of commit %s: %s",
6103 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6104 err = got_error_from_errno("asprintf");
6110 err = got_object_id_str(&id_str, hle->commit_id);
6114 err = got_opentemp_named_fd(&logmsg_path, &fd,
6115 GOT_TMPDIR_STR "/got-logmsg");
6119 dprintf(fd, logmsg);
6122 err = get_editor(&editor);
6126 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6128 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6130 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6133 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6134 err = got_error_from_errno2("unlink", logmsg_path);
6140 got_object_commit_close(commit);
6144 static const struct got_error *
6145 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6146 FILE *f, struct got_repository *repo)
6148 const struct got_error *err = NULL;
6149 char *line = NULL, *p, *end;
6153 const struct got_histedit_cmd *cmd;
6154 struct got_object_id *commit_id = NULL;
6155 struct got_histedit_list_entry *hle = NULL;
6158 len = getline(&line, &size, f);
6160 const struct got_error *getline_err;
6163 getline_err = got_error_from_errno("getline");
6164 err = got_ferror(f, getline_err->code);
6169 while (isspace((unsigned char)p[0]))
6171 if (p[0] == '#' || p[0] == '\0') {
6177 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6178 cmd = &got_histedit_cmds[i];
6179 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6180 isspace((unsigned char)p[strlen(cmd->name)])) {
6181 p += strlen(cmd->name);
6184 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6189 if (i == nitems(got_histedit_cmds)) {
6190 err = histedit_syntax_error(lineno);
6193 while (isspace((unsigned char)p[0]))
6195 if (cmd->code == GOT_HISTEDIT_MESG) {
6196 if (hle == NULL || hle->logmsg != NULL) {
6197 err = got_error(GOT_ERR_HISTEDIT_CMD);
6201 err = histedit_edit_logmsg(hle, repo);
6205 hle->logmsg = strdup(p);
6206 if (hle->logmsg == NULL) {
6207 err = got_error_from_errno("strdup");
6216 while (end[0] && !isspace((unsigned char)end[0]))
6220 err = got_object_resolve_id_str(&commit_id, repo, p);
6222 /* override error code */
6223 err = histedit_syntax_error(lineno);
6227 hle = malloc(sizeof(*hle));
6229 err = got_error_from_errno("malloc");
6233 hle->commit_id = commit_id;
6238 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6246 static const struct got_error *
6247 histedit_check_script(struct got_histedit_list *histedit_cmds,
6248 struct got_object_id_queue *commits, struct got_repository *repo)
6250 const struct got_error *err = NULL;
6251 struct got_object_qid *qid;
6252 struct got_histedit_list_entry *hle;
6253 static char msg[92];
6256 if (TAILQ_EMPTY(histedit_cmds))
6257 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6258 "histedit script contains no commands");
6259 if (SIMPLEQ_EMPTY(commits))
6260 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6262 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6263 struct got_histedit_list_entry *hle2;
6264 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6267 if (got_object_id_cmp(hle->commit_id,
6268 hle2->commit_id) != 0)
6270 err = got_object_id_str(&id_str, hle->commit_id);
6273 snprintf(msg, sizeof(msg), "commit %s is listed "
6274 "more than once in histedit script", id_str);
6276 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6280 SIMPLEQ_FOREACH(qid, commits, entry) {
6281 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6282 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6286 err = got_object_id_str(&id_str, qid->id);
6289 snprintf(msg, sizeof(msg),
6290 "commit %s missing from histedit script", id_str);
6292 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6296 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6297 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6298 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6299 "last commit in histedit script cannot be folded");
6304 static const struct got_error *
6305 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6306 const char *path, struct got_object_id_queue *commits,
6307 struct got_repository *repo)
6309 const struct got_error *err = NULL;
6313 err = get_editor(&editor);
6317 if (spawn_editor(editor, path) == -1) {
6318 err = got_error_from_errno("failed spawning editor");
6322 f = fopen(path, "r");
6324 err = got_error_from_errno("fopen");
6327 err = histedit_parse_list(histedit_cmds, f, repo);
6331 err = histedit_check_script(histedit_cmds, commits, repo);
6333 if (f && fclose(f) != 0 && err == NULL)
6334 err = got_error_from_errno("fclose");
6339 static const struct got_error *
6340 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6341 struct got_object_id_queue *, const char *, const char *,
6342 struct got_repository *);
6344 static const struct got_error *
6345 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6346 struct got_object_id_queue *commits, const char *branch_name,
6347 int edit_logmsg_only, struct got_repository *repo)
6349 const struct got_error *err;
6353 err = got_opentemp_named(&path, &f, "got-histedit");
6357 err = write_cmd_list(f, branch_name, commits);
6361 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6365 if (edit_logmsg_only) {
6367 err = histedit_parse_list(histedit_cmds, f, repo);
6369 if (fclose(f) != 0) {
6370 err = got_error_from_errno("fclose");
6374 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6376 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6377 err->code != GOT_ERR_HISTEDIT_CMD)
6379 err = histedit_edit_list_retry(histedit_cmds, err,
6380 commits, path, branch_name, repo);
6384 if (f && fclose(f) != 0 && err == NULL)
6385 err = got_error_from_errno("fclose");
6386 if (path && unlink(path) != 0 && err == NULL)
6387 err = got_error_from_errno2("unlink", path);
6392 static const struct got_error *
6393 histedit_save_list(struct got_histedit_list *histedit_cmds,
6394 struct got_worktree *worktree, struct got_repository *repo)
6396 const struct got_error *err = NULL;
6399 struct got_histedit_list_entry *hle;
6400 struct got_commit_object *commit = NULL;
6402 err = got_worktree_get_histedit_script_path(&path, worktree);
6406 f = fopen(path, "w");
6408 err = got_error_from_errno2("fopen", path);
6411 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6412 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6418 int n = fprintf(f, "%c %s\n",
6419 GOT_HISTEDIT_MESG, hle->logmsg);
6421 err = got_ferror(f, GOT_ERR_IO);
6427 if (f && fclose(f) != 0 && err == NULL)
6428 err = got_error_from_errno("fclose");
6431 got_object_commit_close(commit);
6436 histedit_free_list(struct got_histedit_list *histedit_cmds)
6438 struct got_histedit_list_entry *hle;
6440 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6441 TAILQ_REMOVE(histedit_cmds, hle, entry);
6446 static const struct got_error *
6447 histedit_load_list(struct got_histedit_list *histedit_cmds,
6448 const char *path, struct got_repository *repo)
6450 const struct got_error *err = NULL;
6453 f = fopen(path, "r");
6455 err = got_error_from_errno2("fopen", path);
6459 err = histedit_parse_list(histedit_cmds, f, repo);
6461 if (f && fclose(f) != 0 && err == NULL)
6462 err = got_error_from_errno("fclose");
6466 static const struct got_error *
6467 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6468 const struct got_error *edit_err, struct got_object_id_queue *commits,
6469 const char *path, const char *branch_name, struct got_repository *repo)
6471 const struct got_error *err = NULL, *prev_err = edit_err;
6474 while (resp != 'c' && resp != 'r' && resp != 'a') {
6475 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6476 "or (a)bort: ", getprogname(), prev_err->msg);
6481 histedit_free_list(histedit_cmds);
6482 err = histedit_run_editor(histedit_cmds, path, commits,
6485 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6486 err->code != GOT_ERR_HISTEDIT_CMD)
6493 } else if (resp == 'r') {
6494 histedit_free_list(histedit_cmds);
6495 err = histedit_edit_script(histedit_cmds,
6496 commits, branch_name, 0, repo);
6498 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6499 err->code != GOT_ERR_HISTEDIT_CMD)
6506 } else if (resp == 'a') {
6507 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6510 printf("invalid response '%c'\n", resp);
6516 static const struct got_error *
6517 histedit_complete(struct got_worktree *worktree,
6518 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6519 struct got_reference *branch, struct got_repository *repo)
6521 printf("Switching work tree to %s\n",
6522 got_ref_get_symref_target(branch));
6523 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6527 static const struct got_error *
6528 show_histedit_progress(struct got_commit_object *commit,
6529 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6531 const struct got_error *err;
6532 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6534 err = got_object_id_str(&old_id_str, hle->commit_id);
6539 err = got_object_id_str(&new_id_str, new_id);
6544 old_id_str[12] = '\0';
6546 new_id_str[12] = '\0';
6549 logmsg = strdup(hle->logmsg);
6550 if (logmsg == NULL) {
6551 err = got_error_from_errno("strdup");
6554 trim_logmsg(logmsg, 42);
6556 err = get_short_logmsg(&logmsg, 42, commit);
6561 switch (hle->cmd->code) {
6562 case GOT_HISTEDIT_PICK:
6563 case GOT_HISTEDIT_EDIT:
6564 printf("%s -> %s: %s\n", old_id_str,
6565 new_id_str ? new_id_str : "no-op change", logmsg);
6567 case GOT_HISTEDIT_DROP:
6568 case GOT_HISTEDIT_FOLD:
6569 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6581 static const struct got_error *
6582 histedit_commit(struct got_pathlist_head *merged_paths,
6583 struct got_worktree *worktree, struct got_fileindex *fileindex,
6584 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6585 struct got_repository *repo)
6587 const struct got_error *err;
6588 struct got_commit_object *commit;
6589 struct got_object_id *new_commit_id;
6591 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6592 && hle->logmsg == NULL) {
6593 err = histedit_edit_logmsg(hle, repo);
6598 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6602 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6603 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6606 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6608 err = show_histedit_progress(commit, hle, NULL);
6610 err = show_histedit_progress(commit, hle, new_commit_id);
6611 free(new_commit_id);
6614 got_object_commit_close(commit);
6618 static const struct got_error *
6619 histedit_skip_commit(struct got_histedit_list_entry *hle,
6620 struct got_worktree *worktree, struct got_repository *repo)
6622 const struct got_error *error;
6623 struct got_commit_object *commit;
6625 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6630 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6634 error = show_histedit_progress(commit, hle, NULL);
6635 got_object_commit_close(commit);
6639 static const struct got_error *
6640 check_local_changes(void *arg, unsigned char status,
6641 unsigned char staged_status, const char *path,
6642 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6643 struct got_object_id *commit_id, int dirfd, const char *de_name)
6645 int *have_local_changes = arg;
6648 case GOT_STATUS_ADD:
6649 case GOT_STATUS_DELETE:
6650 case GOT_STATUS_MODIFY:
6651 case GOT_STATUS_CONFLICT:
6652 *have_local_changes = 1;
6653 return got_error(GOT_ERR_CANCELLED);
6658 switch (staged_status) {
6659 case GOT_STATUS_ADD:
6660 case GOT_STATUS_DELETE:
6661 case GOT_STATUS_MODIFY:
6662 *have_local_changes = 1;
6663 return got_error(GOT_ERR_CANCELLED);
6671 static const struct got_error *
6672 cmd_histedit(int argc, char *argv[])
6674 const struct got_error *error = NULL;
6675 struct got_worktree *worktree = NULL;
6676 struct got_fileindex *fileindex = NULL;
6677 struct got_repository *repo = NULL;
6679 struct got_reference *branch = NULL;
6680 struct got_reference *tmp_branch = NULL;
6681 struct got_object_id *resume_commit_id = NULL;
6682 struct got_object_id *base_commit_id = NULL;
6683 struct got_object_id *head_commit_id = NULL;
6684 struct got_commit_object *commit = NULL;
6685 int ch, rebase_in_progress = 0, did_something;
6686 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6687 int edit_logmsg_only = 0;
6688 const char *edit_script_path = NULL;
6689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 struct got_object_id_queue commits;
6691 struct got_pathlist_head merged_paths;
6692 const struct got_object_id_queue *parent_ids;
6693 struct got_object_qid *pid;
6694 struct got_histedit_list histedit_cmds;
6695 struct got_histedit_list_entry *hle;
6697 SIMPLEQ_INIT(&commits);
6698 TAILQ_INIT(&histedit_cmds);
6699 TAILQ_INIT(&merged_paths);
6701 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6710 edit_script_path = optarg;
6713 edit_logmsg_only = 1;
6725 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6726 "unveil", NULL) == -1)
6729 if (abort_edit && continue_edit)
6730 errx(1, "histedit's -a and -c options are mutually exclusive");
6731 if (edit_script_path && edit_logmsg_only)
6732 errx(1, "histedit's -F and -m options are mutually exclusive");
6733 if (abort_edit && edit_logmsg_only)
6734 errx(1, "histedit's -a and -m options are mutually exclusive");
6735 if (continue_edit && edit_logmsg_only)
6736 errx(1, "histedit's -c and -m options are mutually exclusive");
6741 * This command cannot apply unveil(2) in all cases because the
6742 * user may choose to run an editor to edit the histedit script
6743 * and to edit individual commit log messages.
6744 * unveil(2) traverses exec(2); if an editor is used we have to
6745 * apply unveil after edit script and log messages have been written.
6746 * XXX TODO: Make use of unveil(2) where possible.
6749 cwd = getcwd(NULL, 0);
6751 error = got_error_from_errno("getcwd");
6754 error = got_worktree_open(&worktree, cwd);
6758 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6763 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6766 if (rebase_in_progress) {
6767 error = got_error(GOT_ERR_REBASING);
6771 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6775 if (edit_in_progress && edit_logmsg_only) {
6776 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6777 "histedit operation is in progress in this "
6778 "work tree and must be continued or aborted "
6779 "before the -m option can be used");
6783 if (edit_in_progress && abort_edit) {
6784 error = got_worktree_histedit_continue(&resume_commit_id,
6785 &tmp_branch, &branch, &base_commit_id, &fileindex,
6789 printf("Switching work tree to %s\n",
6790 got_ref_get_symref_target(branch));
6791 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6792 branch, base_commit_id, update_progress, &did_something);
6795 printf("Histedit of %s aborted\n",
6796 got_ref_get_symref_target(branch));
6797 goto done; /* nothing else to do */
6798 } else if (abort_edit) {
6799 error = got_error(GOT_ERR_NOT_HISTEDIT);
6803 if (continue_edit) {
6806 if (!edit_in_progress) {
6807 error = got_error(GOT_ERR_NOT_HISTEDIT);
6811 error = got_worktree_get_histedit_script_path(&path, worktree);
6815 error = histedit_load_list(&histedit_cmds, path, repo);
6820 error = got_worktree_histedit_continue(&resume_commit_id,
6821 &tmp_branch, &branch, &base_commit_id, &fileindex,
6826 error = got_ref_resolve(&head_commit_id, repo, branch);
6830 error = got_object_open_as_commit(&commit, repo,
6834 parent_ids = got_object_commit_get_parent_ids(commit);
6835 pid = SIMPLEQ_FIRST(parent_ids);
6837 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6840 error = collect_commits(&commits, head_commit_id, pid->id,
6841 base_commit_id, got_worktree_get_path_prefix(worktree),
6842 GOT_ERR_HISTEDIT_PATH, repo);
6843 got_object_commit_close(commit);
6848 if (edit_in_progress) {
6849 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6853 error = got_ref_open(&branch, repo,
6854 got_worktree_get_head_ref_name(worktree), 0);
6858 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6859 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6860 "will not edit commit history of a branch outside "
6861 "the \"refs/heads/\" reference namespace");
6865 error = got_ref_resolve(&head_commit_id, repo, branch);
6866 got_ref_close(branch);
6871 error = got_object_open_as_commit(&commit, repo,
6875 parent_ids = got_object_commit_get_parent_ids(commit);
6876 pid = SIMPLEQ_FIRST(parent_ids);
6878 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6881 error = collect_commits(&commits, head_commit_id, pid->id,
6882 got_worktree_get_base_commit_id(worktree),
6883 got_worktree_get_path_prefix(worktree),
6884 GOT_ERR_HISTEDIT_PATH, repo);
6885 got_object_commit_close(commit);
6890 if (SIMPLEQ_EMPTY(&commits)) {
6891 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6895 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6896 &base_commit_id, &fileindex, worktree, repo);
6900 if (edit_script_path) {
6901 error = histedit_load_list(&histedit_cmds,
6902 edit_script_path, repo);
6904 got_worktree_histedit_abort(worktree, fileindex,
6905 repo, branch, base_commit_id,
6906 update_progress, &did_something);
6910 const char *branch_name;
6911 branch_name = got_ref_get_symref_target(branch);
6912 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6914 error = histedit_edit_script(&histedit_cmds, &commits,
6915 branch_name, edit_logmsg_only, repo);
6917 got_worktree_histedit_abort(worktree, fileindex,
6918 repo, branch, base_commit_id,
6919 update_progress, &did_something);
6925 error = histedit_save_list(&histedit_cmds, worktree,
6928 got_worktree_histedit_abort(worktree, fileindex,
6929 repo, branch, base_commit_id,
6930 update_progress, &did_something);
6936 error = histedit_check_script(&histedit_cmds, &commits, repo);
6940 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6941 if (resume_commit_id) {
6942 if (got_object_id_cmp(hle->commit_id,
6943 resume_commit_id) != 0)
6946 resume_commit_id = NULL;
6947 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6948 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6949 error = histedit_skip_commit(hle, worktree,
6954 struct got_pathlist_head paths;
6955 int have_changes = 0;
6958 error = got_pathlist_append(&paths, "", NULL);
6961 error = got_worktree_status(worktree, &paths,
6962 repo, check_local_changes, &have_changes,
6963 check_cancelled, NULL);
6964 got_pathlist_free(&paths);
6966 if (error->code != GOT_ERR_CANCELLED)
6968 if (sigint_received || sigpipe_received)
6972 error = histedit_commit(NULL, worktree,
6973 fileindex, tmp_branch, hle, repo);
6977 error = got_object_open_as_commit(
6978 &commit, repo, hle->commit_id);
6981 error = show_histedit_progress(commit,
6983 got_object_commit_close(commit);
6992 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6993 error = histedit_skip_commit(hle, worktree, repo);
6999 error = got_object_open_as_commit(&commit, repo,
7003 parent_ids = got_object_commit_get_parent_ids(commit);
7004 pid = SIMPLEQ_FIRST(parent_ids);
7006 error = got_worktree_histedit_merge_files(&merged_paths,
7007 worktree, fileindex, pid->id, hle->commit_id, repo,
7008 rebase_progress, &rebase_status, check_cancelled, NULL);
7011 got_object_commit_close(commit);
7014 if (rebase_status == GOT_STATUS_CONFLICT) {
7015 error = show_rebase_merge_conflict(hle->commit_id,
7019 got_worktree_rebase_pathlist_free(&merged_paths);
7023 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7025 error = got_object_id_str(&id_str, hle->commit_id);
7028 printf("Stopping histedit for amending commit %s\n",
7031 got_worktree_rebase_pathlist_free(&merged_paths);
7032 error = got_worktree_histedit_postpone(worktree,
7037 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7038 error = histedit_skip_commit(hle, worktree, repo);
7044 error = histedit_commit(&merged_paths, worktree, fileindex,
7045 tmp_branch, hle, repo);
7046 got_worktree_rebase_pathlist_free(&merged_paths);
7051 if (rebase_status == GOT_STATUS_CONFLICT) {
7052 error = got_worktree_histedit_postpone(worktree, fileindex);
7055 error = got_error_msg(GOT_ERR_CONFLICTS,
7056 "conflicts must be resolved before histedit can continue");
7058 error = histedit_complete(worktree, fileindex, tmp_branch,
7061 got_object_id_queue_free(&commits);
7062 histedit_free_list(&histedit_cmds);
7063 free(head_commit_id);
7064 free(base_commit_id);
7065 free(resume_commit_id);
7067 got_object_commit_close(commit);
7069 got_ref_close(branch);
7071 got_ref_close(tmp_branch);
7073 got_worktree_close(worktree);
7075 got_repo_close(repo);
7080 usage_integrate(void)
7082 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7086 static const struct got_error *
7087 cmd_integrate(int argc, char *argv[])
7089 const struct got_error *error = NULL;
7090 struct got_repository *repo = NULL;
7091 struct got_worktree *worktree = NULL;
7092 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7093 const char *branch_arg = NULL;
7094 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7095 struct got_fileindex *fileindex = NULL;
7096 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7097 int ch, did_something = 0;
7099 while ((ch = getopt(argc, argv, "")) != -1) {
7112 branch_arg = argv[0];
7114 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7115 "unveil", NULL) == -1)
7118 cwd = getcwd(NULL, 0);
7120 error = got_error_from_errno("getcwd");
7124 error = got_worktree_open(&worktree, cwd);
7128 error = check_rebase_or_histedit_in_progress(worktree);
7132 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7137 error = apply_unveil(got_repo_get_path(repo), 0,
7138 got_worktree_get_root_path(worktree));
7142 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7143 error = got_error_from_errno("asprintf");
7147 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7148 &base_branch_ref, worktree, refname, repo);
7152 refname = strdup(got_ref_get_name(branch_ref));
7153 if (refname == NULL) {
7154 error = got_error_from_errno("strdup");
7155 got_worktree_integrate_abort(worktree, fileindex, repo,
7156 branch_ref, base_branch_ref);
7159 base_refname = strdup(got_ref_get_name(base_branch_ref));
7160 if (base_refname == NULL) {
7161 error = got_error_from_errno("strdup");
7162 got_worktree_integrate_abort(worktree, fileindex, repo,
7163 branch_ref, base_branch_ref);
7167 error = got_ref_resolve(&commit_id, repo, branch_ref);
7171 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7175 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7176 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7177 "specified branch has already been integrated");
7178 got_worktree_integrate_abort(worktree, fileindex, repo,
7179 branch_ref, base_branch_ref);
7183 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7185 if (error->code == GOT_ERR_ANCESTRY)
7186 error = got_error(GOT_ERR_REBASE_REQUIRED);
7187 got_worktree_integrate_abort(worktree, fileindex, repo,
7188 branch_ref, base_branch_ref);
7192 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7193 branch_ref, base_branch_ref, update_progress, &did_something,
7194 check_cancelled, NULL);
7198 printf("Integrated %s into %s\n", refname, base_refname);
7201 got_repo_close(repo);
7203 got_worktree_close(worktree);
7205 free(base_commit_id);
7215 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7216 "[file-path ...]\n",
7221 static const struct got_error *
7222 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7223 const char *path, struct got_object_id *blob_id,
7224 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7225 int dirfd, const char *de_name)
7227 const struct got_error *err = NULL;
7228 char *id_str = NULL;
7230 if (staged_status != GOT_STATUS_ADD &&
7231 staged_status != GOT_STATUS_MODIFY &&
7232 staged_status != GOT_STATUS_DELETE)
7235 if (staged_status == GOT_STATUS_ADD ||
7236 staged_status == GOT_STATUS_MODIFY)
7237 err = got_object_id_str(&id_str, staged_blob_id);
7239 err = got_object_id_str(&id_str, blob_id);
7243 printf("%s %c %s\n", id_str, staged_status, path);
7248 static const struct got_error *
7249 cmd_stage(int argc, char *argv[])
7251 const struct got_error *error = NULL;
7252 struct got_repository *repo = NULL;
7253 struct got_worktree *worktree = NULL;
7255 struct got_pathlist_head paths;
7256 struct got_pathlist_entry *pe;
7257 int ch, list_stage = 0, pflag = 0;
7258 FILE *patch_script_file = NULL;
7259 const char *patch_script_path = NULL;
7260 struct choose_patch_arg cpa;
7264 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7273 patch_script_path = optarg;
7285 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7286 "unveil", NULL) == -1)
7289 if (list_stage && (pflag || patch_script_path))
7290 errx(1, "-l option cannot be used with other options");
7291 if (patch_script_path && !pflag)
7292 errx(1, "-F option can only be used together with -p option");
7294 cwd = getcwd(NULL, 0);
7296 error = got_error_from_errno("getcwd");
7300 error = got_worktree_open(&worktree, cwd);
7304 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7309 if (patch_script_path) {
7310 patch_script_file = fopen(patch_script_path, "r");
7311 if (patch_script_file == NULL) {
7312 error = got_error_from_errno2("fopen",
7317 error = apply_unveil(got_repo_get_path(repo), 0,
7318 got_worktree_get_root_path(worktree));
7322 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7327 error = got_worktree_status(worktree, &paths, repo,
7328 print_stage, NULL, check_cancelled, NULL);
7330 cpa.patch_script_file = patch_script_file;
7331 cpa.action = "stage";
7332 error = got_worktree_stage(worktree, &paths,
7333 pflag ? NULL : print_status, NULL,
7334 pflag ? choose_patch : NULL, &cpa, repo);
7337 if (patch_script_file && fclose(patch_script_file) == EOF &&
7339 error = got_error_from_errno2("fclose", patch_script_path);
7341 got_repo_close(repo);
7343 got_worktree_close(worktree);
7344 TAILQ_FOREACH(pe, &paths, entry)
7345 free((char *)pe->path);
7346 got_pathlist_free(&paths);
7354 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7355 "[file-path ...]\n",
7361 static const struct got_error *
7362 cmd_unstage(int argc, char *argv[])
7364 const struct got_error *error = NULL;
7365 struct got_repository *repo = NULL;
7366 struct got_worktree *worktree = NULL;
7368 struct got_pathlist_head paths;
7369 struct got_pathlist_entry *pe;
7370 int ch, did_something = 0, pflag = 0;
7371 FILE *patch_script_file = NULL;
7372 const char *patch_script_path = NULL;
7373 struct choose_patch_arg cpa;
7377 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7383 patch_script_path = optarg;
7395 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7396 "unveil", NULL) == -1)
7399 if (patch_script_path && !pflag)
7400 errx(1, "-F option can only be used together with -p option");
7402 cwd = getcwd(NULL, 0);
7404 error = got_error_from_errno("getcwd");
7408 error = got_worktree_open(&worktree, cwd);
7412 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7417 if (patch_script_path) {
7418 patch_script_file = fopen(patch_script_path, "r");
7419 if (patch_script_file == NULL) {
7420 error = got_error_from_errno2("fopen",
7426 error = apply_unveil(got_repo_get_path(repo), 0,
7427 got_worktree_get_root_path(worktree));
7431 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7435 cpa.patch_script_file = patch_script_file;
7436 cpa.action = "unstage";
7437 error = got_worktree_unstage(worktree, &paths, update_progress,
7438 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7440 if (patch_script_file && fclose(patch_script_file) == EOF &&
7442 error = got_error_from_errno2("fclose", patch_script_path);
7444 got_repo_close(repo);
7446 got_worktree_close(worktree);
7447 TAILQ_FOREACH(pe, &paths, entry)
7448 free((char *)pe->path);
7449 got_pathlist_free(&paths);
7457 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7458 "arg1 [arg2 ...]\n", getprogname());
7462 static const struct got_error *
7463 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7465 const struct got_error *err;
7466 struct got_blob_object *blob;
7468 err = got_object_open_as_blob(&blob, repo, id, 8192);
7472 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7473 got_object_blob_close(blob);
7477 static const struct got_error *
7478 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7480 const struct got_error *err;
7481 struct got_tree_object *tree;
7484 err = got_object_open_as_tree(&tree, repo, id);
7488 nentries = got_object_tree_get_nentries(tree);
7489 for (i = 0; i < nentries; i++) {
7490 struct got_tree_entry *te;
7492 if (sigint_received || sigpipe_received)
7494 te = got_object_tree_get_entry(tree, i);
7495 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7498 fprintf(outfile, "%s %.7o %s\n", id_str,
7499 got_tree_entry_get_mode(te),
7500 got_tree_entry_get_name(te));
7504 got_object_tree_close(tree);
7508 static const struct got_error *
7509 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7511 const struct got_error *err;
7512 struct got_commit_object *commit;
7513 const struct got_object_id_queue *parent_ids;
7514 struct got_object_qid *pid;
7515 char *id_str = NULL;
7516 const char *logmsg = NULL;
7518 err = got_object_open_as_commit(&commit, repo, id);
7522 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7526 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7527 parent_ids = got_object_commit_get_parent_ids(commit);
7528 fprintf(outfile, "numparents %d\n",
7529 got_object_commit_get_nparents(commit));
7530 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7532 err = got_object_id_str(&pid_str, pid->id);
7535 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7538 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7539 got_object_commit_get_author(commit),
7540 got_object_commit_get_author_time(commit));
7542 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7543 got_object_commit_get_author(commit),
7544 got_object_commit_get_committer_time(commit));
7546 logmsg = got_object_commit_get_logmsg_raw(commit);
7547 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7548 fprintf(outfile, "%s", logmsg);
7551 got_object_commit_close(commit);
7555 static const struct got_error *
7556 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7558 const struct got_error *err;
7559 struct got_tag_object *tag;
7560 char *id_str = NULL;
7561 const char *tagmsg = NULL;
7563 err = got_object_open_as_tag(&tag, repo, id);
7567 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7571 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7573 switch (got_object_tag_get_object_type(tag)) {
7574 case GOT_OBJ_TYPE_BLOB:
7575 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7576 GOT_OBJ_LABEL_BLOB);
7578 case GOT_OBJ_TYPE_TREE:
7579 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7580 GOT_OBJ_LABEL_TREE);
7582 case GOT_OBJ_TYPE_COMMIT:
7583 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7584 GOT_OBJ_LABEL_COMMIT);
7586 case GOT_OBJ_TYPE_TAG:
7587 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7594 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7595 got_object_tag_get_name(tag));
7597 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7598 got_object_tag_get_tagger(tag),
7599 got_object_tag_get_tagger_time(tag));
7601 tagmsg = got_object_tag_get_message(tag);
7602 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7603 fprintf(outfile, "%s", tagmsg);
7606 got_object_tag_close(tag);
7610 static const struct got_error *
7611 cmd_cat(int argc, char *argv[])
7613 const struct got_error *error;
7614 struct got_repository *repo = NULL;
7615 struct got_worktree *worktree = NULL;
7616 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7617 const char *commit_id_str = NULL;
7618 struct got_object_id *id = NULL, *commit_id = NULL;
7619 int ch, obj_type, i, force_path = 0;
7622 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7627 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7630 commit_id_str = optarg;
7633 repo_path = realpath(optarg, NULL);
7634 if (repo_path == NULL)
7635 return got_error_from_errno2("realpath",
7637 got_path_strip_trailing_slashes(repo_path);
7651 cwd = getcwd(NULL, 0);
7653 error = got_error_from_errno("getcwd");
7656 error = got_worktree_open(&worktree, cwd);
7657 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7660 if (repo_path == NULL) {
7662 got_worktree_get_repo_path(worktree));
7663 if (repo_path == NULL) {
7664 error = got_error_from_errno("strdup");
7670 if (repo_path == NULL) {
7671 repo_path = getcwd(NULL, 0);
7672 if (repo_path == NULL)
7673 return got_error_from_errno("getcwd");
7676 error = got_repo_open(&repo, repo_path, NULL);
7681 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7685 if (commit_id_str == NULL)
7686 commit_id_str = GOT_REF_HEAD;
7687 error = got_repo_match_object_id(&commit_id, NULL,
7688 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7692 for (i = 0; i < argc; i++) {
7694 error = got_object_id_by_path(&id, repo, commit_id,
7699 error = got_repo_match_object_id(&id, &label, argv[i],
7700 GOT_OBJ_TYPE_ANY, 0, repo);
7702 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7703 error->code != GOT_ERR_NOT_REF)
7705 error = got_object_id_by_path(&id, repo,
7706 commit_id, argv[i]);
7712 error = got_object_get_type(&obj_type, repo, id);
7717 case GOT_OBJ_TYPE_BLOB:
7718 error = cat_blob(id, repo, stdout);
7720 case GOT_OBJ_TYPE_TREE:
7721 error = cat_tree(id, repo, stdout);
7723 case GOT_OBJ_TYPE_COMMIT:
7724 error = cat_commit(id, repo, stdout);
7726 case GOT_OBJ_TYPE_TAG:
7727 error = cat_tag(id, repo, stdout);
7730 error = got_error(GOT_ERR_OBJ_TYPE);
7745 got_worktree_close(worktree);
7747 const struct got_error *repo_error;
7748 repo_error = got_repo_close(repo);