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;
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 if (dirname == NULL) {
1014 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1015 err = got_error_from_errno("asprintf");
1018 repo_path = default_destdir;
1020 repo_path = dirname;
1022 err = got_path_mkdir(repo_path);
1026 err = got_repo_init(repo_path);
1030 err = got_repo_open(&repo, repo_path, NULL);
1034 err = got_fetch(&pack_hash, &refs, &symrefs,
1035 proto, host, port, server_path, repo_name, branch_filter, repo);
1039 err = got_object_id_str(&id_str, pack_hash);
1042 printf("Fetched %s.pack\n", id_str);
1045 /* Set up references provided with the pack file. */
1046 TAILQ_FOREACH(pe, &refs, entry) {
1047 const char *refname = pe->path;
1048 struct got_object_id *id = pe->data;
1049 struct got_reference *ref;
1051 err = got_object_id_str(&id_str, id);
1055 err = got_ref_alloc(&ref, refname, id);
1061 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1063 err = got_ref_write(ref, repo);
1069 /* Set the HEAD reference if the server provided one. */
1070 TAILQ_FOREACH(pe, &symrefs, entry) {
1071 struct got_reference *symref, *target_ref;
1072 const char *refname = pe->path;
1073 const char *target = pe->data;
1075 if (strcmp(refname, GOT_REF_HEAD) != 0)
1078 err = got_ref_open(&target_ref, repo, target, 0);
1080 if (err->code == GOT_ERR_NOT_REF)
1085 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1086 got_ref_close(target_ref);
1090 printf("Setting %s to %s\n", GOT_REF_HEAD,
1091 got_ref_get_symref_target(symref));
1093 err = got_ref_write(symref, repo);
1094 got_ref_close(symref);
1100 got_repo_close(repo);
1101 TAILQ_FOREACH(pe, &refs, entry) {
1102 free((void *)pe->path);
1105 got_pathlist_free(&refs);
1106 TAILQ_FOREACH(pe, &symrefs, entry) {
1107 free((void *)pe->path);
1110 got_pathlist_free(&symrefs);
1117 free(default_destdir);
1121 static const struct got_error *
1122 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1124 static char msg[512];
1125 const char *branch_name;
1127 if (got_ref_is_symbolic(ref))
1128 branch_name = got_ref_get_symref_target(ref);
1130 branch_name = got_ref_get_name(ref);
1132 if (strncmp("refs/heads/", branch_name, 11) == 0)
1135 snprintf(msg, sizeof(msg),
1136 "target commit is not contained in branch '%s'; "
1137 "the branch to use must be specified with -b; "
1138 "if necessary a new branch can be created for "
1139 "this commit with 'got branch -c %s BRANCH_NAME'",
1140 branch_name, commit_id_str);
1142 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1145 static const struct got_error *
1146 cmd_checkout(int argc, char *argv[])
1148 const struct got_error *error = NULL;
1149 struct got_repository *repo = NULL;
1150 struct got_reference *head_ref = NULL;
1151 struct got_worktree *worktree = NULL;
1152 char *repo_path = NULL;
1153 char *worktree_path = NULL;
1154 const char *path_prefix = "";
1155 const char *branch_name = GOT_REF_HEAD;
1156 char *commit_id_str = NULL;
1157 int ch, same_path_prefix, allow_nonempty = 0;
1158 struct got_pathlist_head paths;
1159 struct got_checkout_progress_arg cpa;
1163 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1166 branch_name = optarg;
1169 commit_id_str = strdup(optarg);
1170 if (commit_id_str == NULL)
1171 return got_error_from_errno("strdup");
1177 path_prefix = optarg;
1189 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1190 "unveil", NULL) == -1)
1194 char *cwd, *base, *dotgit;
1195 repo_path = realpath(argv[0], NULL);
1196 if (repo_path == NULL)
1197 return got_error_from_errno2("realpath", argv[0]);
1198 cwd = getcwd(NULL, 0);
1200 error = got_error_from_errno("getcwd");
1203 if (path_prefix[0]) {
1204 base = basename(path_prefix);
1206 error = got_error_from_errno2("basename",
1211 base = basename(repo_path);
1213 error = got_error_from_errno2("basename",
1218 dotgit = strstr(base, ".git");
1221 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1222 error = got_error_from_errno("asprintf");
1227 } else if (argc == 2) {
1228 repo_path = realpath(argv[0], NULL);
1229 if (repo_path == NULL) {
1230 error = got_error_from_errno2("realpath", argv[0]);
1233 worktree_path = realpath(argv[1], NULL);
1234 if (worktree_path == NULL) {
1235 if (errno != ENOENT) {
1236 error = got_error_from_errno2("realpath",
1240 worktree_path = strdup(argv[1]);
1241 if (worktree_path == NULL) {
1242 error = got_error_from_errno("strdup");
1249 got_path_strip_trailing_slashes(repo_path);
1250 got_path_strip_trailing_slashes(worktree_path);
1252 error = got_repo_open(&repo, repo_path, NULL);
1256 /* Pre-create work tree path for unveil(2) */
1257 error = got_path_mkdir(worktree_path);
1259 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1260 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1262 if (!allow_nonempty &&
1263 !got_path_dir_is_empty(worktree_path)) {
1264 error = got_error_path(worktree_path,
1265 GOT_ERR_DIR_NOT_EMPTY);
1270 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1274 error = got_ref_open(&head_ref, repo, branch_name, 0);
1278 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1279 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1282 error = got_worktree_open(&worktree, worktree_path);
1286 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1290 if (!same_path_prefix) {
1291 error = got_error(GOT_ERR_PATH_PREFIX);
1295 if (commit_id_str) {
1296 struct got_object_id *commit_id;
1297 error = got_repo_match_object_id(&commit_id, NULL,
1298 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1301 error = check_linear_ancestry(commit_id,
1302 got_worktree_get_base_commit_id(worktree), 0, repo);
1303 if (error != NULL) {
1305 if (error->code == GOT_ERR_ANCESTRY) {
1306 error = checkout_ancestry_error(
1307 head_ref, commit_id_str);
1311 error = check_same_branch(commit_id, head_ref, NULL, repo);
1313 if (error->code == GOT_ERR_ANCESTRY) {
1314 error = checkout_ancestry_error(
1315 head_ref, commit_id_str);
1319 error = got_worktree_set_base_commit_id(worktree, repo,
1326 error = got_pathlist_append(&paths, "", NULL);
1329 cpa.worktree_path = worktree_path;
1330 cpa.had_base_commit_ref_error = 0;
1331 error = got_worktree_checkout_files(worktree, &paths, repo,
1332 checkout_progress, &cpa, check_cancelled, NULL);
1336 printf("Now shut up and hack\n");
1337 if (cpa.had_base_commit_ref_error)
1338 show_worktree_base_ref_warning();
1340 got_pathlist_free(&paths);
1341 free(commit_id_str);
1343 free(worktree_path);
1350 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1355 static const struct got_error *
1356 update_progress(void *arg, unsigned char status, const char *path)
1358 int *did_something = arg;
1360 if (status == GOT_STATUS_EXISTS ||
1361 status == GOT_STATUS_BASE_REF_ERR)
1366 /* Base commit bump happens silently. */
1367 if (status == GOT_STATUS_BUMP_BASE)
1370 while (path[0] == '/')
1372 printf("%c %s\n", status, path);
1376 static const struct got_error *
1377 switch_head_ref(struct got_reference *head_ref,
1378 struct got_object_id *commit_id, struct got_worktree *worktree,
1379 struct got_repository *repo)
1381 const struct got_error *err = NULL;
1383 int ref_has_moved = 0;
1385 /* Trivial case: switching between two different references. */
1386 if (strcmp(got_ref_get_name(head_ref),
1387 got_worktree_get_head_ref_name(worktree)) != 0) {
1388 printf("Switching work tree from %s to %s\n",
1389 got_worktree_get_head_ref_name(worktree),
1390 got_ref_get_name(head_ref));
1391 return got_worktree_set_head_ref(worktree, head_ref);
1394 err = check_linear_ancestry(commit_id,
1395 got_worktree_get_base_commit_id(worktree), 0, repo);
1397 if (err->code != GOT_ERR_ANCESTRY)
1404 /* Switching to a rebased branch with the same reference name. */
1405 err = got_object_id_str(&base_id_str,
1406 got_worktree_get_base_commit_id(worktree));
1409 printf("Reference %s now points at a different branch\n",
1410 got_worktree_get_head_ref_name(worktree));
1411 printf("Switching work tree from %s to %s\n", base_id_str,
1412 got_worktree_get_head_ref_name(worktree));
1416 static const struct got_error *
1417 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1419 const struct got_error *err;
1422 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1426 return got_error(GOT_ERR_REBASING);
1428 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1432 return got_error(GOT_ERR_HISTEDIT_BUSY);
1437 static const struct got_error *
1438 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1439 char *argv[], struct got_worktree *worktree)
1441 const struct got_error *err = NULL;
1448 return got_error_from_errno("strdup");
1449 return got_pathlist_append(paths, path, NULL);
1452 for (i = 0; i < argc; i++) {
1453 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1456 err = got_pathlist_append(paths, path, NULL);
1466 static const struct got_error *
1467 cmd_update(int argc, char *argv[])
1469 const struct got_error *error = NULL;
1470 struct got_repository *repo = NULL;
1471 struct got_worktree *worktree = NULL;
1472 char *worktree_path = NULL;
1473 struct got_object_id *commit_id = NULL;
1474 char *commit_id_str = NULL;
1475 const char *branch_name = NULL;
1476 struct got_reference *head_ref = NULL;
1477 struct got_pathlist_head paths;
1478 struct got_pathlist_entry *pe;
1479 int ch, did_something = 0;
1483 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1486 branch_name = optarg;
1489 commit_id_str = strdup(optarg);
1490 if (commit_id_str == NULL)
1491 return got_error_from_errno("strdup");
1503 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1504 "unveil", NULL) == -1)
1507 worktree_path = getcwd(NULL, 0);
1508 if (worktree_path == NULL) {
1509 error = got_error_from_errno("getcwd");
1512 error = got_worktree_open(&worktree, worktree_path);
1516 error = check_rebase_or_histedit_in_progress(worktree);
1520 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1525 error = apply_unveil(got_repo_get_path(repo), 0,
1526 got_worktree_get_root_path(worktree));
1530 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1534 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1535 got_worktree_get_head_ref_name(worktree), 0);
1538 if (commit_id_str == NULL) {
1539 error = got_ref_resolve(&commit_id, repo, head_ref);
1542 error = got_object_id_str(&commit_id_str, commit_id);
1546 error = got_repo_match_object_id(&commit_id, NULL,
1547 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1548 free(commit_id_str);
1549 commit_id_str = NULL;
1552 error = got_object_id_str(&commit_id_str, commit_id);
1558 struct got_object_id *head_commit_id;
1559 TAILQ_FOREACH(pe, &paths, entry) {
1560 if (pe->path_len == 0)
1562 error = got_error_msg(GOT_ERR_BAD_PATH,
1563 "switching between branches requires that "
1564 "the entire work tree gets updated");
1567 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1570 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1572 free(head_commit_id);
1575 error = check_same_branch(commit_id, head_ref, NULL, repo);
1578 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1582 error = check_linear_ancestry(commit_id,
1583 got_worktree_get_base_commit_id(worktree), 0, repo);
1584 if (error != NULL) {
1585 if (error->code == GOT_ERR_ANCESTRY)
1586 error = got_error(GOT_ERR_BRANCH_MOVED);
1589 error = check_same_branch(commit_id, head_ref, NULL, repo);
1594 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1596 error = got_worktree_set_base_commit_id(worktree, repo,
1602 error = got_worktree_checkout_files(worktree, &paths, repo,
1603 update_progress, &did_something, check_cancelled, NULL);
1608 printf("Updated to commit %s\n", commit_id_str);
1610 printf("Already up-to-date\n");
1612 free(worktree_path);
1613 TAILQ_FOREACH(pe, &paths, entry)
1614 free((char *)pe->path);
1615 got_pathlist_free(&paths);
1617 free(commit_id_str);
1621 static const struct got_error *
1622 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1623 const char *path, int diff_context, int ignore_whitespace,
1624 struct got_repository *repo)
1626 const struct got_error *err = NULL;
1627 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1630 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1635 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1639 while (path[0] == '/')
1641 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1642 ignore_whitespace, stdout);
1645 got_object_blob_close(blob1);
1646 got_object_blob_close(blob2);
1650 static const struct got_error *
1651 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1652 const char *path, int diff_context, int ignore_whitespace,
1653 struct got_repository *repo)
1655 const struct got_error *err = NULL;
1656 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1657 struct got_diff_blob_output_unidiff_arg arg;
1660 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1665 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1669 arg.diff_context = diff_context;
1670 arg.ignore_whitespace = ignore_whitespace;
1671 arg.outfile = stdout;
1672 while (path[0] == '/')
1674 err = got_diff_tree(tree1, tree2, path, path, repo,
1675 got_diff_blob_output_unidiff, &arg, 1);
1678 got_object_tree_close(tree1);
1680 got_object_tree_close(tree2);
1684 static const struct got_error *
1685 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1686 const char *path, int diff_context, struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_commit_object *pcommit = NULL;
1690 char *id_str1 = NULL, *id_str2 = NULL;
1691 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1692 struct got_object_qid *qid;
1694 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1696 err = got_object_open_as_commit(&pcommit, repo,
1702 if (path && path[0] != '\0') {
1704 err = got_object_id_by_path(&obj_id2, repo, id, path);
1707 err = got_object_id_str(&id_str2, obj_id2);
1713 err = got_object_id_by_path(&obj_id1, repo,
1719 err = got_object_id_str(&id_str1, obj_id1);
1725 err = got_object_get_type(&obj_type, repo, obj_id2);
1730 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1732 case GOT_OBJ_TYPE_BLOB:
1733 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1736 case GOT_OBJ_TYPE_TREE:
1737 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1741 err = got_error(GOT_ERR_OBJ_TYPE);
1747 obj_id2 = got_object_commit_get_tree_id(commit);
1748 err = got_object_id_str(&id_str2, obj_id2);
1751 obj_id1 = got_object_commit_get_tree_id(pcommit);
1752 err = got_object_id_str(&id_str1, obj_id1);
1755 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1756 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1762 got_object_commit_close(pcommit);
1767 get_datestr(time_t *time, char *datebuf)
1769 struct tm mytm, *tm;
1772 tm = gmtime_r(time, &mytm);
1775 s = asctime_r(tm, datebuf);
1778 p = strchr(s, '\n');
1784 static const struct got_error *
1785 match_logmsg(int *have_match, struct got_object_id *id,
1786 struct got_commit_object *commit, regex_t *regex)
1788 const struct got_error *err = NULL;
1789 regmatch_t regmatch;
1790 char *id_str = NULL, *logmsg = NULL;
1794 err = got_object_id_str(&id_str, id);
1798 err = got_object_commit_get_logmsg(&logmsg, commit);
1802 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1810 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1812 static const struct got_error *
1813 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1814 struct got_repository *repo, const char *path, int show_patch,
1815 int diff_context, struct got_reflist_head *refs)
1817 const struct got_error *err = NULL;
1818 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1820 time_t committer_time;
1821 const char *author, *committer;
1822 char *refs_str = NULL;
1823 struct got_reflist_entry *re;
1825 SIMPLEQ_FOREACH(re, refs, entry) {
1828 struct got_tag_object *tag = NULL;
1831 name = got_ref_get_name(re->ref);
1832 if (strcmp(name, GOT_REF_HEAD) == 0)
1834 if (strncmp(name, "refs/", 5) == 0)
1836 if (strncmp(name, "got/", 4) == 0)
1838 if (strncmp(name, "heads/", 6) == 0)
1840 if (strncmp(name, "remotes/", 8) == 0)
1842 if (strncmp(name, "tags/", 5) == 0) {
1843 err = got_object_open_as_tag(&tag, repo, re->id);
1845 if (err->code != GOT_ERR_OBJ_TYPE)
1847 /* Ref points at something other than a tag. */
1852 cmp = got_object_id_cmp(tag ?
1853 got_object_tag_get_object_id(tag) : re->id, id);
1855 got_object_tag_close(tag);
1859 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1861 err = got_error_from_errno("asprintf");
1867 err = got_object_id_str(&id_str, id);
1871 printf(GOT_COMMIT_SEP_STR);
1872 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1873 refs_str ? refs_str : "", refs_str ? ")" : "");
1878 printf("from: %s\n", got_object_commit_get_author(commit));
1879 committer_time = got_object_commit_get_committer_time(commit);
1880 datestr = get_datestr(&committer_time, datebuf);
1882 printf("date: %s UTC\n", datestr);
1883 author = got_object_commit_get_author(commit);
1884 committer = got_object_commit_get_committer(commit);
1885 if (strcmp(author, committer) != 0)
1886 printf("via: %s\n", committer);
1887 if (got_object_commit_get_nparents(commit) > 1) {
1888 const struct got_object_id_queue *parent_ids;
1889 struct got_object_qid *qid;
1891 parent_ids = got_object_commit_get_parent_ids(commit);
1892 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1893 err = got_object_id_str(&id_str, qid->id);
1896 printf("parent %d: %s\n", n++, id_str);
1901 err = got_object_commit_get_logmsg(&logmsg0, commit);
1907 line = strsep(&logmsg, "\n");
1909 printf(" %s\n", line);
1914 err = print_patch(commit, id, path, diff_context, repo);
1919 if (fflush(stdout) != 0 && err == NULL)
1920 err = got_error_from_errno("fflush");
1924 static const struct got_error *
1925 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1926 const char *path, int show_patch, const char *search_pattern,
1927 int diff_context, int limit, int log_branches,
1928 struct got_reflist_head *refs)
1930 const struct got_error *err;
1931 struct got_commit_graph *graph;
1935 if (search_pattern &&
1936 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1937 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1939 err = got_commit_graph_open(&graph, path, !log_branches);
1942 err = got_commit_graph_iter_start(graph, root_id, repo,
1943 check_cancelled, NULL);
1947 struct got_commit_object *commit;
1948 struct got_object_id *id;
1950 if (sigint_received || sigpipe_received)
1953 err = got_commit_graph_iter_next(&id, graph, repo,
1954 check_cancelled, NULL);
1956 if (err->code == GOT_ERR_ITER_COMPLETED)
1963 err = got_object_open_as_commit(&commit, repo, id);
1967 if (search_pattern) {
1968 err = match_logmsg(&have_match, id, commit, ®ex);
1970 got_object_commit_close(commit);
1973 if (have_match == 0) {
1974 got_object_commit_close(commit);
1979 err = print_commit(commit, id, repo, path, show_patch,
1980 diff_context, refs);
1981 got_object_commit_close(commit);
1982 if (err || (limit && --limit == 0))
1988 got_commit_graph_close(graph);
1995 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1996 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2001 get_default_log_limit(void)
2003 const char *got_default_log_limit;
2007 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2008 if (got_default_log_limit == NULL)
2010 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2016 static const struct got_error *
2017 cmd_log(int argc, char *argv[])
2019 const struct got_error *error;
2020 struct got_repository *repo = NULL;
2021 struct got_worktree *worktree = NULL;
2022 struct got_commit_object *commit = NULL;
2023 struct got_object_id *id = NULL;
2024 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2025 const char *start_commit = NULL, *search_pattern = NULL;
2026 int diff_context = -1, ch;
2027 int show_patch = 0, limit = 0, log_branches = 0;
2029 struct got_reflist_head refs;
2031 SIMPLEQ_INIT(&refs);
2034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2040 limit = get_default_log_limit();
2042 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2048 start_commit = optarg;
2051 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2054 err(1, "-C option %s", errstr);
2057 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2059 err(1, "-l option %s", errstr);
2065 repo_path = realpath(optarg, NULL);
2066 if (repo_path == NULL)
2067 return got_error_from_errno2("realpath",
2069 got_path_strip_trailing_slashes(repo_path);
2072 search_pattern = optarg;
2083 if (diff_context == -1)
2085 else if (!show_patch)
2086 errx(1, "-C reguires -p");
2088 cwd = getcwd(NULL, 0);
2090 error = got_error_from_errno("getcwd");
2094 error = got_worktree_open(&worktree, cwd);
2095 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2102 error = got_error_from_errno("strdup");
2105 } else if (argc == 1) {
2107 error = got_worktree_resolve_path(&path, worktree,
2112 path = strdup(argv[0]);
2114 error = got_error_from_errno("strdup");
2121 if (repo_path == NULL) {
2122 repo_path = worktree ?
2123 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2125 if (repo_path == NULL) {
2126 error = got_error_from_errno("strdup");
2130 error = got_repo_open(&repo, repo_path, NULL);
2134 error = apply_unveil(got_repo_get_path(repo), 1,
2135 worktree ? got_worktree_get_root_path(worktree) : NULL);
2139 if (start_commit == NULL) {
2140 struct got_reference *head_ref;
2141 error = got_ref_open(&head_ref, repo,
2142 worktree ? got_worktree_get_head_ref_name(worktree)
2146 error = got_ref_resolve(&id, repo, head_ref);
2147 got_ref_close(head_ref);
2150 error = got_object_open_as_commit(&commit, repo, id);
2152 struct got_reference *ref;
2153 error = got_ref_open(&ref, repo, start_commit, 0);
2154 if (error == NULL) {
2156 error = got_ref_resolve(&id, repo, ref);
2160 error = got_object_get_type(&obj_type, repo, id);
2163 if (obj_type == GOT_OBJ_TYPE_TAG) {
2164 struct got_tag_object *tag;
2165 error = got_object_open_as_tag(&tag, repo, id);
2168 if (got_object_tag_get_object_type(tag) !=
2169 GOT_OBJ_TYPE_COMMIT) {
2170 got_object_tag_close(tag);
2171 error = got_error(GOT_ERR_OBJ_TYPE);
2175 id = got_object_id_dup(
2176 got_object_tag_get_object_id(tag));
2178 error = got_error_from_errno(
2179 "got_object_id_dup");
2180 got_object_tag_close(tag);
2183 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2184 error = got_error(GOT_ERR_OBJ_TYPE);
2187 error = got_object_open_as_commit(&commit, repo, id);
2191 if (commit == NULL) {
2192 error = got_repo_match_object_id_prefix(&id,
2193 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2202 const char *prefix = got_worktree_get_path_prefix(worktree);
2204 if (asprintf(&p, "%s%s%s", prefix,
2205 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2206 error = got_error_from_errno("asprintf");
2209 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2212 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2217 path = in_repo_path;
2220 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2224 error = print_commits(id, repo, path, show_patch, search_pattern,
2225 diff_context, limit, log_branches, &refs);
2232 got_worktree_close(worktree);
2234 const struct got_error *repo_error;
2235 repo_error = got_repo_close(repo);
2239 got_ref_list_free(&refs);
2246 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2247 "[-w] [object1 object2 | path]\n", getprogname());
2251 struct print_diff_arg {
2252 struct got_repository *repo;
2253 struct got_worktree *worktree;
2258 int ignore_whitespace;
2261 static const struct got_error *
2262 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2263 const char *path, struct got_object_id *blob_id,
2264 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2265 int dirfd, const char *de_name)
2267 struct print_diff_arg *a = arg;
2268 const struct got_error *err = NULL;
2269 struct got_blob_object *blob1 = NULL;
2272 char *abspath = NULL, *label1 = NULL;
2275 if (a->diff_staged) {
2276 if (staged_status != GOT_STATUS_MODIFY &&
2277 staged_status != GOT_STATUS_ADD &&
2278 staged_status != GOT_STATUS_DELETE)
2281 if (staged_status == GOT_STATUS_DELETE)
2283 if (status == GOT_STATUS_NONEXISTENT)
2284 return got_error_set_errno(ENOENT, path);
2285 if (status != GOT_STATUS_MODIFY &&
2286 status != GOT_STATUS_ADD &&
2287 status != GOT_STATUS_DELETE &&
2288 status != GOT_STATUS_CONFLICT)
2292 if (!a->header_shown) {
2293 printf("diff %s %s%s\n", a->id_str,
2294 got_worktree_get_root_path(a->worktree),
2295 a->diff_staged ? " (staged changes)" : "");
2296 a->header_shown = 1;
2299 if (a->diff_staged) {
2300 const char *label1 = NULL, *label2 = NULL;
2301 switch (staged_status) {
2302 case GOT_STATUS_MODIFY:
2306 case GOT_STATUS_ADD:
2309 case GOT_STATUS_DELETE:
2313 return got_error(GOT_ERR_FILE_STATUS);
2315 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2316 label1, label2, a->diff_context, a->ignore_whitespace,
2320 if (staged_status == GOT_STATUS_ADD ||
2321 staged_status == GOT_STATUS_MODIFY) {
2323 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2327 err = got_object_id_str(&id_str, staged_blob_id);
2330 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2331 err = got_error_from_errno("asprintf");
2336 } else if (status != GOT_STATUS_ADD) {
2337 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2342 if (status != GOT_STATUS_DELETE) {
2343 if (asprintf(&abspath, "%s/%s",
2344 got_worktree_get_root_path(a->worktree), path) == -1) {
2345 err = got_error_from_errno("asprintf");
2350 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2352 err = got_error_from_errno2("openat", abspath);
2356 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2358 err = got_error_from_errno2("open", abspath);
2362 if (fstat(fd, &sb) == -1) {
2363 err = got_error_from_errno2("fstat", abspath);
2366 f2 = fdopen(fd, "r");
2368 err = got_error_from_errno2("fdopen", abspath);
2375 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2376 a->diff_context, a->ignore_whitespace, stdout);
2379 got_object_blob_close(blob1);
2380 if (f2 && fclose(f2) == EOF && err == NULL)
2381 err = got_error_from_errno("fclose");
2382 if (fd != -1 && close(fd) == -1 && err == NULL)
2383 err = got_error_from_errno("close");
2388 static const struct got_error *
2389 cmd_diff(int argc, char *argv[])
2391 const struct got_error *error;
2392 struct got_repository *repo = NULL;
2393 struct got_worktree *worktree = NULL;
2394 char *cwd = NULL, *repo_path = NULL;
2395 struct got_object_id *id1 = NULL, *id2 = NULL;
2396 const char *id_str1 = NULL, *id_str2 = NULL;
2397 char *label1 = NULL, *label2 = NULL;
2399 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2404 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2409 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2412 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2415 err(1, "-C option %s", errstr);
2418 repo_path = realpath(optarg, NULL);
2419 if (repo_path == NULL)
2420 return got_error_from_errno2("realpath",
2422 got_path_strip_trailing_slashes(repo_path);
2428 ignore_whitespace = 1;
2439 cwd = getcwd(NULL, 0);
2441 error = got_error_from_errno("getcwd");
2444 error = got_worktree_open(&worktree, cwd);
2445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2448 if (worktree == NULL) {
2449 error = got_error(GOT_ERR_NOT_WORKTREE);
2454 "-r option can't be used when diffing a work tree");
2455 repo_path = strdup(got_worktree_get_repo_path(worktree));
2456 if (repo_path == NULL) {
2457 error = got_error_from_errno("strdup");
2461 error = got_worktree_resolve_path(&path, worktree,
2468 error = got_error_from_errno("strdup");
2472 } else if (argc == 2) {
2474 errx(1, "-s option can't be used when diffing "
2475 "objects in repository");
2478 if (worktree && repo_path == NULL) {
2480 strdup(got_worktree_get_repo_path(worktree));
2481 if (repo_path == NULL) {
2482 error = got_error_from_errno("strdup");
2489 if (repo_path == NULL) {
2490 repo_path = getcwd(NULL, 0);
2491 if (repo_path == NULL)
2492 return got_error_from_errno("getcwd");
2495 error = got_repo_open(&repo, repo_path, NULL);
2500 error = apply_unveil(got_repo_get_path(repo), 1,
2501 worktree ? got_worktree_get_root_path(worktree) : NULL);
2506 struct print_diff_arg arg;
2507 struct got_pathlist_head paths;
2512 error = got_object_id_str(&id_str,
2513 got_worktree_get_base_commit_id(worktree));
2517 arg.worktree = worktree;
2518 arg.diff_context = diff_context;
2519 arg.id_str = id_str;
2520 arg.header_shown = 0;
2521 arg.diff_staged = diff_staged;
2522 arg.ignore_whitespace = ignore_whitespace;
2524 error = got_pathlist_append(&paths, path, NULL);
2528 error = got_worktree_status(worktree, &paths, repo, print_diff,
2529 &arg, check_cancelled, NULL);
2531 got_pathlist_free(&paths);
2535 error = got_repo_match_object_id(&id1, &label1, id_str1,
2536 GOT_OBJ_TYPE_ANY, 1, repo);
2540 error = got_repo_match_object_id(&id2, &label2, id_str2,
2541 GOT_OBJ_TYPE_ANY, 1, repo);
2545 error = got_object_get_type(&type1, repo, id1);
2549 error = got_object_get_type(&type2, repo, id2);
2553 if (type1 != type2) {
2554 error = got_error(GOT_ERR_OBJ_TYPE);
2559 case GOT_OBJ_TYPE_BLOB:
2560 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2561 diff_context, ignore_whitespace, repo, stdout);
2563 case GOT_OBJ_TYPE_TREE:
2564 error = got_diff_objects_as_trees(id1, id2, "", "",
2565 diff_context, ignore_whitespace, repo, stdout);
2567 case GOT_OBJ_TYPE_COMMIT:
2568 printf("diff %s %s\n", label1, label2);
2569 error = got_diff_objects_as_commits(id1, id2, diff_context,
2570 ignore_whitespace, repo, stdout);
2573 error = got_error(GOT_ERR_OBJ_TYPE);
2582 got_worktree_close(worktree);
2584 const struct got_error *repo_error;
2585 repo_error = got_repo_close(repo);
2596 "usage: %s blame [-c commit] [-r repository-path] path\n",
2605 char datebuf[11]; /* YYYY-MM-DD + NUL */
2608 struct blame_cb_args {
2609 struct blame_line *lines;
2613 off_t *line_offsets;
2615 struct got_repository *repo;
2618 static const struct got_error *
2619 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2621 const struct got_error *err = NULL;
2622 struct blame_cb_args *a = arg;
2623 struct blame_line *bline;
2625 size_t linesize = 0;
2626 struct got_commit_object *commit = NULL;
2629 time_t committer_time;
2631 if (nlines != a->nlines ||
2632 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2633 return got_error(GOT_ERR_RANGE);
2635 if (sigint_received)
2636 return got_error(GOT_ERR_ITER_COMPLETED);
2639 return NULL; /* no change in this commit */
2641 /* Annotate this line. */
2642 bline = &a->lines[lineno - 1];
2643 if (bline->annotated)
2645 err = got_object_id_str(&bline->id_str, id);
2649 err = got_object_open_as_commit(&commit, a->repo, id);
2653 bline->committer = strdup(got_object_commit_get_committer(commit));
2654 if (bline->committer == NULL) {
2655 err = got_error_from_errno("strdup");
2659 committer_time = got_object_commit_get_committer_time(commit);
2660 if (localtime_r(&committer_time, &tm) == NULL)
2661 return got_error_from_errno("localtime_r");
2662 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2663 &tm) >= sizeof(bline->datebuf)) {
2664 err = got_error(GOT_ERR_NO_SPACE);
2667 bline->annotated = 1;
2669 /* Print lines annotated so far. */
2670 bline = &a->lines[a->lineno_cur - 1];
2671 if (!bline->annotated)
2674 offset = a->line_offsets[a->lineno_cur - 1];
2675 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2676 err = got_error_from_errno("fseeko");
2680 while (bline->annotated) {
2681 char *smallerthan, *at, *nl, *committer;
2684 if (getline(&line, &linesize, a->f) == -1) {
2686 err = got_error_from_errno("getline");
2690 committer = bline->committer;
2691 smallerthan = strchr(committer, '<');
2692 if (smallerthan && smallerthan[1] != '\0')
2693 committer = smallerthan + 1;
2694 at = strchr(committer, '@');
2697 len = strlen(committer);
2699 committer[8] = '\0';
2701 nl = strchr(line, '\n');
2704 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2705 bline->id_str, bline->datebuf, committer, line);
2708 bline = &a->lines[a->lineno_cur - 1];
2712 got_object_commit_close(commit);
2717 static const struct got_error *
2718 cmd_blame(int argc, char *argv[])
2720 const struct got_error *error;
2721 struct got_repository *repo = NULL;
2722 struct got_worktree *worktree = NULL;
2723 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2724 struct got_object_id *obj_id = NULL;
2725 struct got_object_id *commit_id = NULL;
2726 struct got_blob_object *blob = NULL;
2727 char *commit_id_str = NULL;
2728 struct blame_cb_args bca;
2729 int ch, obj_type, i;
2732 memset(&bca, 0, sizeof(bca));
2735 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2740 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2743 commit_id_str = optarg;
2746 repo_path = realpath(optarg, NULL);
2747 if (repo_path == NULL)
2748 return got_error_from_errno2("realpath",
2750 got_path_strip_trailing_slashes(repo_path);
2766 cwd = getcwd(NULL, 0);
2768 error = got_error_from_errno("getcwd");
2771 if (repo_path == NULL) {
2772 error = got_worktree_open(&worktree, cwd);
2773 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2779 strdup(got_worktree_get_repo_path(worktree));
2780 if (repo_path == NULL)
2781 error = got_error_from_errno("strdup");
2785 repo_path = strdup(cwd);
2786 if (repo_path == NULL) {
2787 error = got_error_from_errno("strdup");
2793 error = got_repo_open(&repo, repo_path, NULL);
2797 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2802 const char *prefix = got_worktree_get_path_prefix(worktree);
2803 char *p, *worktree_subdir = cwd +
2804 strlen(got_worktree_get_root_path(worktree));
2805 if (asprintf(&p, "%s%s%s%s%s",
2806 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2807 worktree_subdir, worktree_subdir[0] ? "/" : "",
2809 error = got_error_from_errno("asprintf");
2812 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2815 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2820 if (commit_id_str == NULL) {
2821 struct got_reference *head_ref;
2822 error = got_ref_open(&head_ref, repo, worktree ?
2823 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2826 error = got_ref_resolve(&commit_id, repo, head_ref);
2827 got_ref_close(head_ref);
2831 error = got_repo_match_object_id(&commit_id, NULL,
2832 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2837 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2841 error = got_object_get_type(&obj_type, repo, obj_id);
2845 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2846 error = got_error(GOT_ERR_OBJ_TYPE);
2850 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2853 bca.f = got_opentemp();
2854 if (bca.f == NULL) {
2855 error = got_error_from_errno("got_opentemp");
2858 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2859 &bca.line_offsets, bca.f, blob);
2860 if (error || bca.nlines == 0)
2863 /* Don't include \n at EOF in the blame line count. */
2864 if (bca.line_offsets[bca.nlines - 1] == filesize)
2867 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2868 if (bca.lines == NULL) {
2869 error = got_error_from_errno("calloc");
2873 bca.nlines_prec = 0;
2881 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2882 check_cancelled, NULL);
2890 got_object_blob_close(blob);
2892 got_worktree_close(worktree);
2894 const struct got_error *repo_error;
2895 repo_error = got_repo_close(repo);
2900 for (i = 0; i < bca.nlines; i++) {
2901 struct blame_line *bline = &bca.lines[i];
2902 free(bline->id_str);
2903 free(bline->committer);
2907 free(bca.line_offsets);
2908 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2909 error = got_error_from_errno("fclose");
2917 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2923 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2924 const char *root_path)
2926 int is_root_path = (strcmp(path, root_path) == 0);
2927 const char *modestr = "";
2928 mode_t mode = got_tree_entry_get_mode(te);
2930 path += strlen(root_path);
2931 while (path[0] == '/')
2934 if (got_object_tree_entry_is_submodule(te))
2936 else if (S_ISLNK(mode))
2938 else if (S_ISDIR(mode))
2940 else if (mode & S_IXUSR)
2943 printf("%s%s%s%s%s\n", id ? id : "", path,
2944 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2947 static const struct got_error *
2948 print_tree(const char *path, struct got_object_id *commit_id,
2949 int show_ids, int recurse, const char *root_path,
2950 struct got_repository *repo)
2952 const struct got_error *err = NULL;
2953 struct got_object_id *tree_id = NULL;
2954 struct got_tree_object *tree = NULL;
2957 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2961 err = got_object_open_as_tree(&tree, repo, tree_id);
2964 nentries = got_object_tree_get_nentries(tree);
2965 for (i = 0; i < nentries; i++) {
2966 struct got_tree_entry *te;
2969 if (sigint_received || sigpipe_received)
2972 te = got_object_tree_get_entry(tree, i);
2975 err = got_object_id_str(&id_str,
2976 got_tree_entry_get_id(te));
2979 if (asprintf(&id, "%s ", id_str) == -1) {
2980 err = got_error_from_errno("asprintf");
2986 print_entry(te, id, path, root_path);
2989 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2991 if (asprintf(&child_path, "%s%s%s", path,
2992 path[0] == '/' && path[1] == '\0' ? "" : "/",
2993 got_tree_entry_get_name(te)) == -1) {
2994 err = got_error_from_errno("asprintf");
2997 err = print_tree(child_path, commit_id, show_ids, 1,
3006 got_object_tree_close(tree);
3011 static const struct got_error *
3012 cmd_tree(int argc, char *argv[])
3014 const struct got_error *error;
3015 struct got_repository *repo = NULL;
3016 struct got_worktree *worktree = NULL;
3018 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3019 struct got_object_id *commit_id = NULL;
3020 char *commit_id_str = NULL;
3021 int show_ids = 0, recurse = 0;
3025 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3030 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3033 commit_id_str = optarg;
3036 repo_path = realpath(optarg, NULL);
3037 if (repo_path == NULL)
3038 return got_error_from_errno2("realpath",
3040 got_path_strip_trailing_slashes(repo_path);
3064 cwd = getcwd(NULL, 0);
3066 error = got_error_from_errno("getcwd");
3069 if (repo_path == NULL) {
3070 error = got_worktree_open(&worktree, cwd);
3071 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3077 strdup(got_worktree_get_repo_path(worktree));
3078 if (repo_path == NULL)
3079 error = got_error_from_errno("strdup");
3083 repo_path = strdup(cwd);
3084 if (repo_path == NULL) {
3085 error = got_error_from_errno("strdup");
3091 error = got_repo_open(&repo, repo_path, NULL);
3095 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3101 char *p, *worktree_subdir = cwd +
3102 strlen(got_worktree_get_root_path(worktree));
3103 if (asprintf(&p, "%s/%s",
3104 got_worktree_get_path_prefix(worktree),
3105 worktree_subdir) == -1) {
3106 error = got_error_from_errno("asprintf");
3109 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3116 if (in_repo_path == NULL) {
3117 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3122 if (commit_id_str == NULL) {
3123 struct got_reference *head_ref;
3124 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3127 error = got_ref_resolve(&commit_id, repo, head_ref);
3128 got_ref_close(head_ref);
3132 error = got_repo_match_object_id(&commit_id, NULL,
3133 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3138 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3139 in_repo_path, repo);
3146 got_worktree_close(worktree);
3148 const struct got_error *repo_error;
3149 repo_error = got_repo_close(repo);
3159 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3163 static const struct got_error *
3164 print_status(void *arg, unsigned char status, unsigned char staged_status,
3165 const char *path, struct got_object_id *blob_id,
3166 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3167 int dirfd, const char *de_name)
3169 if (status == staged_status && (status == GOT_STATUS_DELETE))
3170 status = GOT_STATUS_NO_CHANGE;
3171 printf("%c%c %s\n", status, staged_status, path);
3175 static const struct got_error *
3176 cmd_status(int argc, char *argv[])
3178 const struct got_error *error = NULL;
3179 struct got_repository *repo = NULL;
3180 struct got_worktree *worktree = NULL;
3182 struct got_pathlist_head paths;
3183 struct got_pathlist_entry *pe;
3188 while ((ch = getopt(argc, argv, "")) != -1) {
3200 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3204 cwd = getcwd(NULL, 0);
3206 error = got_error_from_errno("getcwd");
3210 error = got_worktree_open(&worktree, cwd);
3214 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3219 error = apply_unveil(got_repo_get_path(repo), 1,
3220 got_worktree_get_root_path(worktree));
3224 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3228 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3229 check_cancelled, NULL);
3231 TAILQ_FOREACH(pe, &paths, entry)
3232 free((char *)pe->path);
3233 got_pathlist_free(&paths);
3242 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3247 static const struct got_error *
3248 list_refs(struct got_repository *repo)
3250 static const struct got_error *err = NULL;
3251 struct got_reflist_head refs;
3252 struct got_reflist_entry *re;
3254 SIMPLEQ_INIT(&refs);
3255 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3259 SIMPLEQ_FOREACH(re, &refs, entry) {
3261 refstr = got_ref_to_str(re->ref);
3263 return got_error_from_errno("got_ref_to_str");
3264 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3268 got_ref_list_free(&refs);
3272 static const struct got_error *
3273 delete_ref(struct got_repository *repo, const char *refname)
3275 const struct got_error *err = NULL;
3276 struct got_reference *ref;
3278 err = got_ref_open(&ref, repo, refname, 0);
3282 err = got_ref_delete(ref, repo);
3287 static const struct got_error *
3288 add_ref(struct got_repository *repo, const char *refname, const char *target)
3290 const struct got_error *err = NULL;
3291 struct got_object_id *id;
3292 struct got_reference *ref = NULL;
3295 * Don't let the user create a reference name with a leading '-'.
3296 * While technically a valid reference name, this case is usually
3297 * an unintended typo.
3299 if (refname[0] == '-')
3300 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3302 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3305 struct got_reference *target_ref;
3307 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3309 err = got_ref_open(&target_ref, repo, target, 0);
3312 err = got_ref_resolve(&id, repo, target_ref);
3313 got_ref_close(target_ref);
3318 err = got_ref_alloc(&ref, refname, id);
3322 err = got_ref_write(ref, repo);
3330 static const struct got_error *
3331 add_symref(struct got_repository *repo, const char *refname, const char *target)
3333 const struct got_error *err = NULL;
3334 struct got_reference *ref = NULL;
3335 struct got_reference *target_ref = NULL;
3338 * Don't let the user create a reference name with a leading '-'.
3339 * While technically a valid reference name, this case is usually
3340 * an unintended typo.
3342 if (refname[0] == '-')
3343 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3345 err = got_ref_open(&target_ref, repo, target, 0);
3349 err = got_ref_alloc_symref(&ref, refname, target_ref);
3353 err = got_ref_write(ref, repo);
3356 got_ref_close(target_ref);
3362 static const struct got_error *
3363 cmd_ref(int argc, char *argv[])
3365 const struct got_error *error = NULL;
3366 struct got_repository *repo = NULL;
3367 struct got_worktree *worktree = NULL;
3368 char *cwd = NULL, *repo_path = NULL;
3369 int ch, do_list = 0, create_symref = 0;
3370 const char *delref = NULL;
3372 /* TODO: Add -s option for adding symbolic references. */
3373 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3379 repo_path = realpath(optarg, NULL);
3380 if (repo_path == NULL)
3381 return got_error_from_errno2("realpath",
3383 got_path_strip_trailing_slashes(repo_path);
3397 if (do_list && delref)
3398 errx(1, "-l and -d options are mutually exclusive\n");
3403 if (do_list || delref) {
3405 errx(1, "-s option cannot be used together with the "
3406 "-l or -d options");
3409 } else if (argc != 2)
3414 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3418 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3419 "sendfd unveil", NULL) == -1)
3423 cwd = getcwd(NULL, 0);
3425 error = got_error_from_errno("getcwd");
3429 if (repo_path == NULL) {
3430 error = got_worktree_open(&worktree, cwd);
3431 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3437 strdup(got_worktree_get_repo_path(worktree));
3438 if (repo_path == NULL)
3439 error = got_error_from_errno("strdup");
3443 repo_path = strdup(cwd);
3444 if (repo_path == NULL) {
3445 error = got_error_from_errno("strdup");
3451 error = got_repo_open(&repo, repo_path, NULL);
3455 error = apply_unveil(got_repo_get_path(repo), do_list,
3456 worktree ? got_worktree_get_root_path(worktree) : NULL);
3461 error = list_refs(repo);
3463 error = delete_ref(repo, delref);
3464 else if (create_symref)
3465 error = add_symref(repo, argv[0], argv[1]);
3467 error = add_ref(repo, argv[0], argv[1]);
3470 got_repo_close(repo);
3472 got_worktree_close(worktree);
3482 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3483 "[name]\n", getprogname());
3487 static const struct got_error *
3488 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3489 struct got_reference *ref)
3491 const struct got_error *err = NULL;
3492 const char *refname, *marker = " ";
3495 refname = got_ref_get_name(ref);
3496 if (worktree && strcmp(refname,
3497 got_worktree_get_head_ref_name(worktree)) == 0) {
3498 struct got_object_id *id = NULL;
3500 err = got_ref_resolve(&id, repo, ref);
3503 if (got_object_id_cmp(id,
3504 got_worktree_get_base_commit_id(worktree)) == 0)
3511 if (strncmp(refname, "refs/heads/", 11) == 0)
3513 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3516 refstr = got_ref_to_str(ref);
3518 return got_error_from_errno("got_ref_to_str");
3520 printf("%s%s: %s\n", marker, refname, refstr);
3525 static const struct got_error *
3526 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3528 const char *refname;
3530 if (worktree == NULL)
3531 return got_error(GOT_ERR_NOT_WORKTREE);
3533 refname = got_worktree_get_head_ref_name(worktree);
3535 if (strncmp(refname, "refs/heads/", 11) == 0)
3537 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3540 printf("%s\n", refname);
3545 static const struct got_error *
3546 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3548 static const struct got_error *err = NULL;
3549 struct got_reflist_head refs;
3550 struct got_reflist_entry *re;
3551 struct got_reference *temp_ref = NULL;
3552 int rebase_in_progress, histedit_in_progress;
3554 SIMPLEQ_INIT(&refs);
3557 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3562 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3567 if (rebase_in_progress || histedit_in_progress) {
3568 err = got_ref_open(&temp_ref, repo,
3569 got_worktree_get_head_ref_name(worktree), 0);
3572 list_branch(repo, worktree, temp_ref);
3573 got_ref_close(temp_ref);
3577 err = got_ref_list(&refs, repo, "refs/heads",
3578 got_ref_cmp_by_name, NULL);
3582 SIMPLEQ_FOREACH(re, &refs, entry)
3583 list_branch(repo, worktree, re->ref);
3585 got_ref_list_free(&refs);
3589 static const struct got_error *
3590 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3591 const char *branch_name)
3593 const struct got_error *err = NULL;
3594 struct got_reference *ref = NULL;
3597 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3598 return got_error_from_errno("asprintf");
3600 err = got_ref_open(&ref, repo, refname, 0);
3605 strcmp(got_worktree_get_head_ref_name(worktree),
3606 got_ref_get_name(ref)) == 0) {
3607 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3608 "will not delete this work tree's current branch");
3612 err = got_ref_delete(ref, repo);
3620 static const struct got_error *
3621 add_branch(struct got_repository *repo, const char *branch_name,
3622 struct got_object_id *base_commit_id)
3624 const struct got_error *err = NULL;
3625 struct got_reference *ref = NULL;
3626 char *base_refname = NULL, *refname = NULL;
3629 * Don't let the user create a branch name with a leading '-'.
3630 * While technically a valid reference name, this case is usually
3631 * an unintended typo.
3633 if (branch_name[0] == '-')
3634 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3636 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3637 err = got_error_from_errno("asprintf");
3641 err = got_ref_open(&ref, repo, refname, 0);
3643 err = got_error(GOT_ERR_BRANCH_EXISTS);
3645 } else if (err->code != GOT_ERR_NOT_REF)
3648 err = got_ref_alloc(&ref, refname, base_commit_id);
3652 err = got_ref_write(ref, repo);
3661 static const struct got_error *
3662 cmd_branch(int argc, char *argv[])
3664 const struct got_error *error = NULL;
3665 struct got_repository *repo = NULL;
3666 struct got_worktree *worktree = NULL;
3667 char *cwd = NULL, *repo_path = NULL;
3668 int ch, do_list = 0, do_show = 0, do_update = 1;
3669 const char *delref = NULL, *commit_id_arg = NULL;
3670 struct got_reference *ref = NULL;
3671 struct got_pathlist_head paths;
3672 struct got_pathlist_entry *pe;
3673 struct got_object_id *commit_id = NULL;
3674 char *commit_id_str = NULL;
3678 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3681 commit_id_arg = optarg;
3687 repo_path = realpath(optarg, NULL);
3688 if (repo_path == NULL)
3689 return got_error_from_errno2("realpath",
3691 got_path_strip_trailing_slashes(repo_path);
3705 if (do_list && delref)
3706 errx(1, "-l and -d options are mutually exclusive\n");
3711 if (!do_list && !delref && argc == 0)
3714 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3715 errx(1, "-c option can only be used when creating a branch");
3717 if (do_list || delref) {
3720 } else if (!do_show && argc != 1)
3724 if (do_list || do_show) {
3725 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3729 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3730 "sendfd unveil", NULL) == -1)
3734 cwd = getcwd(NULL, 0);
3736 error = got_error_from_errno("getcwd");
3740 if (repo_path == NULL) {
3741 error = got_worktree_open(&worktree, cwd);
3742 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3748 strdup(got_worktree_get_repo_path(worktree));
3749 if (repo_path == NULL)
3750 error = got_error_from_errno("strdup");
3754 repo_path = strdup(cwd);
3755 if (repo_path == NULL) {
3756 error = got_error_from_errno("strdup");
3762 error = got_repo_open(&repo, repo_path, NULL);
3766 error = apply_unveil(got_repo_get_path(repo), do_list,
3767 worktree ? got_worktree_get_root_path(worktree) : NULL);
3772 error = show_current_branch(repo, worktree);
3774 error = list_branches(repo, worktree);
3776 error = delete_branch(repo, worktree, delref);
3778 if (commit_id_arg == NULL)
3779 commit_id_arg = worktree ?
3780 got_worktree_get_head_ref_name(worktree) :
3782 error = got_repo_match_object_id(&commit_id, NULL,
3783 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3786 error = add_branch(repo, argv[0], commit_id);
3789 if (worktree && do_update) {
3790 int did_something = 0;
3791 char *branch_refname = NULL;
3793 error = got_object_id_str(&commit_id_str, commit_id);
3796 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3800 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3802 error = got_error_from_errno("asprintf");
3805 error = got_ref_open(&ref, repo, branch_refname, 0);
3806 free(branch_refname);
3809 error = switch_head_ref(ref, commit_id, worktree,
3813 error = got_worktree_set_base_commit_id(worktree, repo,
3817 error = got_worktree_checkout_files(worktree, &paths,
3818 repo, update_progress, &did_something,
3819 check_cancelled, NULL);
3823 printf("Updated to commit %s\n", commit_id_str);
3830 got_repo_close(repo);
3832 got_worktree_close(worktree);
3836 free(commit_id_str);
3837 TAILQ_FOREACH(pe, &paths, entry)
3838 free((char *)pe->path);
3839 got_pathlist_free(&paths);
3848 "usage: %s tag [-c commit] [-r repository] [-l] "
3849 "[-m message] name\n", getprogname());
3854 static const struct got_error *
3855 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3857 const struct got_error *err = NULL;
3858 struct got_reflist_entry *re, *se, *new;
3859 struct got_object_id *re_id, *se_id;
3860 struct got_tag_object *re_tag, *se_tag;
3861 time_t re_time, se_time;
3863 SIMPLEQ_FOREACH(re, tags, entry) {
3864 se = SIMPLEQ_FIRST(sorted);
3866 err = got_reflist_entry_dup(&new, re);
3869 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3872 err = got_ref_resolve(&re_id, repo, re->ref);
3875 err = got_object_open_as_tag(&re_tag, repo, re_id);
3879 re_time = got_object_tag_get_tagger_time(re_tag);
3880 got_object_tag_close(re_tag);
3884 err = got_ref_resolve(&se_id, repo, re->ref);
3887 err = got_object_open_as_tag(&se_tag, repo, se_id);
3891 se_time = got_object_tag_get_tagger_time(se_tag);
3892 got_object_tag_close(se_tag);
3894 if (se_time > re_time) {
3895 err = got_reflist_entry_dup(&new, re);
3898 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3901 se = SIMPLEQ_NEXT(se, entry);
3910 static const struct got_error *
3911 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3913 static const struct got_error *err = NULL;
3914 struct got_reflist_head refs;
3915 struct got_reflist_entry *re;
3917 SIMPLEQ_INIT(&refs);
3919 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3923 SIMPLEQ_FOREACH(re, &refs, entry) {
3924 const char *refname;
3925 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3929 struct got_object_id *id;
3930 struct got_tag_object *tag;
3931 struct got_commit_object *commit = NULL;
3933 refname = got_ref_get_name(re->ref);
3934 if (strncmp(refname, "refs/tags/", 10) != 0)
3937 refstr = got_ref_to_str(re->ref);
3938 if (refstr == NULL) {
3939 err = got_error_from_errno("got_ref_to_str");
3942 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3945 err = got_ref_resolve(&id, repo, re->ref);
3948 err = got_object_open_as_tag(&tag, repo, id);
3950 if (err->code != GOT_ERR_OBJ_TYPE) {
3954 /* "lightweight" tag */
3955 err = got_object_open_as_commit(&commit, repo, id);
3960 tagger = got_object_commit_get_committer(commit);
3962 got_object_commit_get_committer_time(commit);
3963 err = got_object_id_str(&id_str, id);
3969 tagger = got_object_tag_get_tagger(tag);
3970 tagger_time = got_object_tag_get_tagger_time(tag);
3971 err = got_object_id_str(&id_str,
3972 got_object_tag_get_object_id(tag));
3976 printf("from: %s\n", tagger);
3977 datestr = get_datestr(&tagger_time, datebuf);
3979 printf("date: %s UTC\n", datestr);
3981 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3983 switch (got_object_tag_get_object_type(tag)) {
3984 case GOT_OBJ_TYPE_BLOB:
3985 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3988 case GOT_OBJ_TYPE_TREE:
3989 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3992 case GOT_OBJ_TYPE_COMMIT:
3993 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3996 case GOT_OBJ_TYPE_TAG:
3997 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4006 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4009 got_object_commit_close(commit);
4011 tagmsg0 = strdup(got_object_tag_get_message(tag));
4012 got_object_tag_close(tag);
4013 if (tagmsg0 == NULL) {
4014 err = got_error_from_errno("strdup");
4021 line = strsep(&tagmsg, "\n");
4023 printf(" %s\n", line);
4028 got_ref_list_free(&refs);
4032 static const struct got_error *
4033 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4034 const char *tag_name, const char *repo_path)
4036 const struct got_error *err = NULL;
4037 char *template = NULL, *initial_content = NULL;
4038 char *editor = NULL;
4041 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4042 err = got_error_from_errno("asprintf");
4046 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4047 commit_id_str, tag_name) == -1) {
4048 err = got_error_from_errno("asprintf");
4052 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4056 dprintf(fd, initial_content);
4059 err = get_editor(&editor);
4062 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4064 free(initial_content);
4068 /* Editor is done; we can now apply unveil(2) */
4070 err = apply_unveil(repo_path, 0, NULL);
4079 static const struct got_error *
4080 add_tag(struct got_repository *repo, const char *tag_name,
4081 const char *commit_arg, const char *tagmsg_arg)
4083 const struct got_error *err = NULL;
4084 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4085 char *label = NULL, *commit_id_str = NULL;
4086 struct got_reference *ref = NULL;
4087 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4088 char *tagmsg_path = NULL, *tag_id_str = NULL;
4089 int preserve_tagmsg = 0;
4092 * Don't let the user create a tag name with a leading '-'.
4093 * While technically a valid reference name, this case is usually
4094 * an unintended typo.
4096 if (tag_name[0] == '-')
4097 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4099 err = get_author(&tagger, repo);
4103 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4104 GOT_OBJ_TYPE_COMMIT, 1, repo);
4108 err = got_object_id_str(&commit_id_str, commit_id);
4112 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4113 refname = strdup(tag_name);
4114 if (refname == NULL) {
4115 err = got_error_from_errno("strdup");
4119 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4120 err = got_error_from_errno("asprintf");
4124 err = got_ref_open(&ref, repo, refname, 0);
4126 err = got_error(GOT_ERR_TAG_EXISTS);
4128 } else if (err->code != GOT_ERR_NOT_REF)
4131 if (tagmsg_arg == NULL) {
4132 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4133 tag_name, got_repo_get_path(repo));
4135 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4136 tagmsg_path != NULL)
4137 preserve_tagmsg = 1;
4142 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4143 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4146 preserve_tagmsg = 1;
4150 err = got_ref_alloc(&ref, refname, tag_id);
4153 preserve_tagmsg = 1;
4157 err = got_ref_write(ref, repo);
4160 preserve_tagmsg = 1;
4164 err = got_object_id_str(&tag_id_str, tag_id);
4167 preserve_tagmsg = 1;
4170 printf("Created tag %s\n", tag_id_str);
4172 if (preserve_tagmsg) {
4173 fprintf(stderr, "%s: tag message preserved in %s\n",
4174 getprogname(), tagmsg_path);
4175 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4176 err = got_error_from_errno2("unlink", tagmsg_path);
4181 free(commit_id_str);
4189 static const struct got_error *
4190 cmd_tag(int argc, char *argv[])
4192 const struct got_error *error = NULL;
4193 struct got_repository *repo = NULL;
4194 struct got_worktree *worktree = NULL;
4195 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4196 char *gitconfig_path = NULL;
4197 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4198 int ch, do_list = 0;
4200 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4203 commit_id_arg = optarg;
4209 repo_path = realpath(optarg, NULL);
4210 if (repo_path == NULL)
4211 return got_error_from_errno2("realpath",
4213 got_path_strip_trailing_slashes(repo_path);
4228 if (commit_id_arg != NULL)
4229 errx(1, "-c option can only be used when creating a tag");
4231 errx(1, "-l and -m options are mutually exclusive");
4234 } else if (argc != 1)
4241 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4245 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4246 "sendfd unveil", NULL) == -1)
4250 cwd = getcwd(NULL, 0);
4252 error = got_error_from_errno("getcwd");
4256 if (repo_path == NULL) {
4257 error = got_worktree_open(&worktree, cwd);
4258 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4264 strdup(got_worktree_get_repo_path(worktree));
4265 if (repo_path == NULL)
4266 error = got_error_from_errno("strdup");
4270 repo_path = strdup(cwd);
4271 if (repo_path == NULL) {
4272 error = got_error_from_errno("strdup");
4279 error = got_repo_open(&repo, repo_path, NULL);
4282 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4285 error = list_tags(repo, worktree);
4287 error = get_gitconfig_path(&gitconfig_path);
4290 error = got_repo_open(&repo, repo_path, gitconfig_path);
4295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4300 if (commit_id_arg == NULL) {
4301 struct got_reference *head_ref;
4302 struct got_object_id *commit_id;
4303 error = got_ref_open(&head_ref, repo,
4304 worktree ? got_worktree_get_head_ref_name(worktree)
4308 error = got_ref_resolve(&commit_id, repo, head_ref);
4309 got_ref_close(head_ref);
4312 error = got_object_id_str(&commit_id_str, commit_id);
4318 error = add_tag(repo, tag_name,
4319 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4323 got_repo_close(repo);
4325 got_worktree_close(worktree);
4328 free(gitconfig_path);
4329 free(commit_id_str);
4336 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4341 static const struct got_error *
4342 add_progress(void *arg, unsigned char status, const char *path)
4344 while (path[0] == '/')
4346 printf("%c %s\n", status, path);
4350 static const struct got_error *
4351 cmd_add(int argc, char *argv[])
4353 const struct got_error *error = NULL;
4354 struct got_repository *repo = NULL;
4355 struct got_worktree *worktree = NULL;
4357 struct got_pathlist_head paths;
4358 struct got_pathlist_entry *pe;
4359 int ch, can_recurse = 0, no_ignores = 0;
4363 while ((ch = getopt(argc, argv, "IR")) != -1) {
4381 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4388 cwd = getcwd(NULL, 0);
4390 error = got_error_from_errno("getcwd");
4394 error = got_worktree_open(&worktree, cwd);
4398 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4403 error = apply_unveil(got_repo_get_path(repo), 1,
4404 got_worktree_get_root_path(worktree));
4408 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4412 if (!can_recurse && no_ignores) {
4413 error = got_error_msg(GOT_ERR_BAD_PATH,
4414 "disregarding ignores requires -R option");
4422 TAILQ_FOREACH(pe, &paths, entry) {
4423 if (asprintf(&ondisk_path, "%s/%s",
4424 got_worktree_get_root_path(worktree),
4426 error = got_error_from_errno("asprintf");
4429 if (lstat(ondisk_path, &sb) == -1) {
4430 if (errno == ENOENT) {
4434 error = got_error_from_errno2("lstat",
4440 if (S_ISDIR(sb.st_mode)) {
4441 error = got_error_msg(GOT_ERR_BAD_PATH,
4442 "adding directories requires -R option");
4448 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4449 NULL, repo, no_ignores);
4452 got_repo_close(repo);
4454 got_worktree_close(worktree);
4455 TAILQ_FOREACH(pe, &paths, entry)
4456 free((char *)pe->path);
4457 got_pathlist_free(&paths);
4465 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4470 static const struct got_error *
4471 print_remove_status(void *arg, unsigned char status,
4472 unsigned char staged_status, const char *path)
4474 while (path[0] == '/')
4476 if (status == GOT_STATUS_NONEXISTENT)
4478 if (status == staged_status && (status == GOT_STATUS_DELETE))
4479 status = GOT_STATUS_NO_CHANGE;
4480 printf("%c%c %s\n", status, staged_status, path);
4484 static const struct got_error *
4485 cmd_remove(int argc, char *argv[])
4487 const struct got_error *error = NULL;
4488 struct got_worktree *worktree = NULL;
4489 struct got_repository *repo = NULL;
4491 struct got_pathlist_head paths;
4492 struct got_pathlist_entry *pe;
4493 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4497 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4500 delete_local_mods = 1;
4518 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4525 cwd = getcwd(NULL, 0);
4527 error = got_error_from_errno("getcwd");
4530 error = got_worktree_open(&worktree, cwd);
4534 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4539 error = apply_unveil(got_repo_get_path(repo), 1,
4540 got_worktree_get_root_path(worktree));
4544 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4551 TAILQ_FOREACH(pe, &paths, entry) {
4552 if (asprintf(&ondisk_path, "%s/%s",
4553 got_worktree_get_root_path(worktree),
4555 error = got_error_from_errno("asprintf");
4558 if (lstat(ondisk_path, &sb) == -1) {
4559 if (errno == ENOENT) {
4563 error = got_error_from_errno2("lstat",
4569 if (S_ISDIR(sb.st_mode)) {
4570 error = got_error_msg(GOT_ERR_BAD_PATH,
4571 "removing directories requires -R option");
4577 error = got_worktree_schedule_delete(worktree, &paths,
4578 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4581 got_repo_close(repo);
4583 got_worktree_close(worktree);
4584 TAILQ_FOREACH(pe, &paths, entry)
4585 free((char *)pe->path);
4586 got_pathlist_free(&paths);
4594 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4595 "path ...\n", getprogname());
4599 static const struct got_error *
4600 revert_progress(void *arg, unsigned char status, const char *path)
4602 if (status == GOT_STATUS_UNVERSIONED)
4605 while (path[0] == '/')
4607 printf("%c %s\n", status, path);
4611 struct choose_patch_arg {
4612 FILE *patch_script_file;
4616 static const struct got_error *
4617 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4618 int nchanges, const char *action)
4621 size_t linesize = 0;
4625 case GOT_STATUS_ADD:
4626 printf("A %s\n%s this addition? [y/n] ", path, action);
4628 case GOT_STATUS_DELETE:
4629 printf("D %s\n%s this deletion? [y/n] ", path, action);
4631 case GOT_STATUS_MODIFY:
4632 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4633 return got_error_from_errno("fseek");
4634 printf(GOT_COMMIT_SEP_STR);
4635 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4637 if (ferror(patch_file))
4638 return got_error_from_errno("getline");
4639 printf(GOT_COMMIT_SEP_STR);
4640 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4641 path, n, nchanges, action);
4644 return got_error_path(path, GOT_ERR_FILE_STATUS);
4650 static const struct got_error *
4651 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4652 FILE *patch_file, int n, int nchanges)
4654 const struct got_error *err = NULL;
4656 size_t linesize = 0;
4659 struct choose_patch_arg *a = arg;
4661 *choice = GOT_PATCH_CHOICE_NONE;
4663 if (a->patch_script_file) {
4665 err = show_change(status, path, patch_file, n, nchanges,
4669 linelen = getline(&line, &linesize, a->patch_script_file);
4670 if (linelen == -1) {
4671 if (ferror(a->patch_script_file))
4672 return got_error_from_errno("getline");
4675 nl = strchr(line, '\n');
4678 if (strcmp(line, "y") == 0) {
4679 *choice = GOT_PATCH_CHOICE_YES;
4681 } else if (strcmp(line, "n") == 0) {
4682 *choice = GOT_PATCH_CHOICE_NO;
4684 } else if (strcmp(line, "q") == 0 &&
4685 status == GOT_STATUS_MODIFY) {
4686 *choice = GOT_PATCH_CHOICE_QUIT;
4689 printf("invalid response '%s'\n", line);
4694 while (resp != 'y' && resp != 'n' && resp != 'q') {
4695 err = show_change(status, path, patch_file, n, nchanges,
4702 if (status == GOT_STATUS_MODIFY) {
4703 if (resp != 'y' && resp != 'n' && resp != 'q') {
4704 printf("invalid response '%c'\n", resp);
4707 } else if (resp != 'y' && resp != 'n') {
4708 printf("invalid response '%c'\n", resp);
4714 *choice = GOT_PATCH_CHOICE_YES;
4715 else if (resp == 'n')
4716 *choice = GOT_PATCH_CHOICE_NO;
4717 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4718 *choice = GOT_PATCH_CHOICE_QUIT;
4724 static const struct got_error *
4725 cmd_revert(int argc, char *argv[])
4727 const struct got_error *error = NULL;
4728 struct got_worktree *worktree = NULL;
4729 struct got_repository *repo = NULL;
4730 char *cwd = NULL, *path = NULL;
4731 struct got_pathlist_head paths;
4732 struct got_pathlist_entry *pe;
4733 int ch, can_recurse = 0, pflag = 0;
4734 FILE *patch_script_file = NULL;
4735 const char *patch_script_path = NULL;
4736 struct choose_patch_arg cpa;
4740 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4746 patch_script_path = optarg;
4761 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4762 "unveil", NULL) == -1)
4767 if (patch_script_path && !pflag)
4768 errx(1, "-F option can only be used together with -p option");
4770 cwd = getcwd(NULL, 0);
4772 error = got_error_from_errno("getcwd");
4775 error = got_worktree_open(&worktree, cwd);
4779 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4784 if (patch_script_path) {
4785 patch_script_file = fopen(patch_script_path, "r");
4786 if (patch_script_file == NULL) {
4787 error = got_error_from_errno2("fopen",
4792 error = apply_unveil(got_repo_get_path(repo), 1,
4793 got_worktree_get_root_path(worktree));
4797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4804 TAILQ_FOREACH(pe, &paths, entry) {
4805 if (asprintf(&ondisk_path, "%s/%s",
4806 got_worktree_get_root_path(worktree),
4808 error = got_error_from_errno("asprintf");
4811 if (lstat(ondisk_path, &sb) == -1) {
4812 if (errno == ENOENT) {
4816 error = got_error_from_errno2("lstat",
4822 if (S_ISDIR(sb.st_mode)) {
4823 error = got_error_msg(GOT_ERR_BAD_PATH,
4824 "reverting directories requires -R option");
4830 cpa.patch_script_file = patch_script_file;
4831 cpa.action = "revert";
4832 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4833 pflag ? choose_patch : NULL, &cpa, repo);
4835 if (patch_script_file && fclose(patch_script_file) == EOF &&
4837 error = got_error_from_errno2("fclose", patch_script_path);
4839 got_repo_close(repo);
4841 got_worktree_close(worktree);
4850 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4855 struct collect_commit_logmsg_arg {
4856 const char *cmdline_log;
4858 const char *worktree_path;
4859 const char *branch_name;
4860 const char *repo_path;
4865 static const struct got_error *
4866 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4869 char *initial_content = NULL;
4870 struct got_pathlist_entry *pe;
4871 const struct got_error *err = NULL;
4872 char *template = NULL;
4873 struct collect_commit_logmsg_arg *a = arg;
4877 /* if a message was specified on the command line, just use it */
4878 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4879 len = strlen(a->cmdline_log) + 1;
4880 *logmsg = malloc(len + 1);
4881 if (*logmsg == NULL)
4882 return got_error_from_errno("malloc");
4883 strlcpy(*logmsg, a->cmdline_log, len);
4887 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4888 return got_error_from_errno("asprintf");
4890 if (asprintf(&initial_content,
4891 "\n# changes to be committed on branch %s:\n",
4892 a->branch_name) == -1)
4893 return got_error_from_errno("asprintf");
4895 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4899 dprintf(fd, initial_content);
4901 TAILQ_FOREACH(pe, commitable_paths, entry) {
4902 struct got_commitable *ct = pe->data;
4903 dprintf(fd, "# %c %s\n",
4904 got_commitable_get_status(ct),
4905 got_commitable_get_path(ct));
4909 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4911 free(initial_content);
4914 /* Editor is done; we can now apply unveil(2) */
4916 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4925 static const struct got_error *
4926 cmd_commit(int argc, char *argv[])
4928 const struct got_error *error = NULL;
4929 struct got_worktree *worktree = NULL;
4930 struct got_repository *repo = NULL;
4931 char *cwd = NULL, *id_str = NULL;
4932 struct got_object_id *id = NULL;
4933 const char *logmsg = NULL;
4934 struct collect_commit_logmsg_arg cl_arg;
4935 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4936 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4937 struct got_pathlist_head paths;
4940 cl_arg.logmsg_path = NULL;
4942 while ((ch = getopt(argc, argv, "m:")) != -1) {
4957 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4958 "unveil", NULL) == -1)
4961 cwd = getcwd(NULL, 0);
4963 error = got_error_from_errno("getcwd");
4966 error = got_worktree_open(&worktree, cwd);
4970 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4973 if (rebase_in_progress) {
4974 error = got_error(GOT_ERR_REBASING);
4978 error = got_worktree_histedit_in_progress(&histedit_in_progress,
4983 error = get_gitconfig_path(&gitconfig_path);
4986 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4991 error = get_author(&author, repo);
4996 * unveil(2) traverses exec(2); if an editor is used we have
4997 * to apply unveil after the log message has been written.
4999 if (logmsg == NULL || strlen(logmsg) == 0)
5000 error = get_editor(&editor);
5002 error = apply_unveil(got_repo_get_path(repo), 0,
5003 got_worktree_get_root_path(worktree));
5007 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5011 cl_arg.editor = editor;
5012 cl_arg.cmdline_log = logmsg;
5013 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5014 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5015 if (!histedit_in_progress) {
5016 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5017 error = got_error(GOT_ERR_COMMIT_BRANCH);
5020 cl_arg.branch_name += 11;
5022 cl_arg.repo_path = got_repo_get_path(repo);
5023 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5024 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5026 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5027 cl_arg.logmsg_path != NULL)
5028 preserve_logmsg = 1;
5032 error = got_object_id_str(&id_str, id);
5035 printf("Created commit %s\n", id_str);
5037 if (preserve_logmsg) {
5038 fprintf(stderr, "%s: log message preserved in %s\n",
5039 getprogname(), cl_arg.logmsg_path);
5040 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5042 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5043 free(cl_arg.logmsg_path);
5045 got_repo_close(repo);
5047 got_worktree_close(worktree);
5050 free(gitconfig_path);
5057 usage_cherrypick(void)
5059 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5063 static const struct got_error *
5064 cmd_cherrypick(int argc, char *argv[])
5066 const struct got_error *error = NULL;
5067 struct got_worktree *worktree = NULL;
5068 struct got_repository *repo = NULL;
5069 char *cwd = NULL, *commit_id_str = NULL;
5070 struct got_object_id *commit_id = NULL;
5071 struct got_commit_object *commit = NULL;
5072 struct got_object_qid *pid;
5073 struct got_reference *head_ref = NULL;
5074 int ch, did_something = 0;
5076 while ((ch = getopt(argc, argv, "")) != -1) {
5088 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5089 "unveil", NULL) == -1)
5095 cwd = getcwd(NULL, 0);
5097 error = got_error_from_errno("getcwd");
5100 error = got_worktree_open(&worktree, cwd);
5104 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5109 error = apply_unveil(got_repo_get_path(repo), 0,
5110 got_worktree_get_root_path(worktree));
5114 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5115 GOT_OBJ_TYPE_COMMIT, repo);
5116 if (error != NULL) {
5117 struct got_reference *ref;
5118 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5120 error = got_ref_open(&ref, repo, argv[0], 0);
5123 error = got_ref_resolve(&commit_id, repo, ref);
5128 error = got_object_id_str(&commit_id_str, commit_id);
5132 error = got_ref_open(&head_ref, repo,
5133 got_worktree_get_head_ref_name(worktree), 0);
5137 error = check_same_branch(commit_id, head_ref, NULL, repo);
5139 if (error->code != GOT_ERR_ANCESTRY)
5143 error = got_error(GOT_ERR_SAME_BRANCH);
5147 error = got_object_open_as_commit(&commit, repo, commit_id);
5150 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5151 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5152 commit_id, repo, update_progress, &did_something, check_cancelled,
5158 printf("Merged commit %s\n", commit_id_str);
5161 got_object_commit_close(commit);
5162 free(commit_id_str);
5164 got_ref_close(head_ref);
5166 got_worktree_close(worktree);
5168 got_repo_close(repo);
5175 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5179 static const struct got_error *
5180 cmd_backout(int argc, char *argv[])
5182 const struct got_error *error = NULL;
5183 struct got_worktree *worktree = NULL;
5184 struct got_repository *repo = NULL;
5185 char *cwd = NULL, *commit_id_str = NULL;
5186 struct got_object_id *commit_id = NULL;
5187 struct got_commit_object *commit = NULL;
5188 struct got_object_qid *pid;
5189 struct got_reference *head_ref = NULL;
5190 int ch, did_something = 0;
5192 while ((ch = getopt(argc, argv, "")) != -1) {
5204 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5205 "unveil", NULL) == -1)
5211 cwd = getcwd(NULL, 0);
5213 error = got_error_from_errno("getcwd");
5216 error = got_worktree_open(&worktree, cwd);
5220 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5225 error = apply_unveil(got_repo_get_path(repo), 0,
5226 got_worktree_get_root_path(worktree));
5230 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5231 GOT_OBJ_TYPE_COMMIT, repo);
5232 if (error != NULL) {
5233 struct got_reference *ref;
5234 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5236 error = got_ref_open(&ref, repo, argv[0], 0);
5239 error = got_ref_resolve(&commit_id, repo, ref);
5244 error = got_object_id_str(&commit_id_str, commit_id);
5248 error = got_ref_open(&head_ref, repo,
5249 got_worktree_get_head_ref_name(worktree), 0);
5253 error = check_same_branch(commit_id, head_ref, NULL, repo);
5257 error = got_object_open_as_commit(&commit, repo, commit_id);
5260 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5262 error = got_error(GOT_ERR_ROOT_COMMIT);
5266 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5267 update_progress, &did_something, check_cancelled, NULL);
5272 printf("Backed out commit %s\n", commit_id_str);
5275 got_object_commit_close(commit);
5276 free(commit_id_str);
5278 got_ref_close(head_ref);
5280 got_worktree_close(worktree);
5282 got_repo_close(repo);
5289 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5295 trim_logmsg(char *logmsg, int limit)
5300 len = strlen(logmsg);
5304 nl = strchr(logmsg, '\n');
5309 static const struct got_error *
5310 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5312 const struct got_error *err;
5313 char *logmsg0 = NULL;
5316 err = got_object_commit_get_logmsg(&logmsg0, commit);
5321 while (isspace((unsigned char)s[0]))
5324 *logmsg = strdup(s);
5325 if (*logmsg == NULL) {
5326 err = got_error_from_errno("strdup");
5330 trim_logmsg(*logmsg, limit);
5336 static const struct got_error *
5337 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5339 const struct got_error *err;
5340 struct got_commit_object *commit = NULL;
5341 char *id_str = NULL, *logmsg = NULL;
5343 err = got_object_open_as_commit(&commit, repo, id);
5347 err = got_object_id_str(&id_str, id);
5353 err = get_short_logmsg(&logmsg, 42, commit);
5357 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5360 got_object_commit_close(commit);
5365 static const struct got_error *
5366 show_rebase_progress(struct got_commit_object *commit,
5367 struct got_object_id *old_id, struct got_object_id *new_id)
5369 const struct got_error *err;
5370 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5372 err = got_object_id_str(&old_id_str, old_id);
5377 err = got_object_id_str(&new_id_str, new_id);
5382 old_id_str[12] = '\0';
5384 new_id_str[12] = '\0';
5386 err = get_short_logmsg(&logmsg, 42, commit);
5390 printf("%s -> %s: %s\n", old_id_str,
5391 new_id_str ? new_id_str : "no-op change", logmsg);
5399 static const struct got_error *
5400 rebase_progress(void *arg, unsigned char status, const char *path)
5402 unsigned char *rebase_status = arg;
5404 while (path[0] == '/')
5406 printf("%c %s\n", status, path);
5408 if (*rebase_status == GOT_STATUS_CONFLICT)
5410 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5411 *rebase_status = status;
5415 static const struct got_error *
5416 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5417 struct got_reference *branch, struct got_reference *new_base_branch,
5418 struct got_reference *tmp_branch, struct got_repository *repo)
5420 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5421 return got_worktree_rebase_complete(worktree, fileindex,
5422 new_base_branch, tmp_branch, branch, repo);
5425 static const struct got_error *
5426 rebase_commit(struct got_pathlist_head *merged_paths,
5427 struct got_worktree *worktree, struct got_fileindex *fileindex,
5428 struct got_reference *tmp_branch,
5429 struct got_object_id *commit_id, struct got_repository *repo)
5431 const struct got_error *error;
5432 struct got_commit_object *commit;
5433 struct got_object_id *new_commit_id;
5435 error = got_object_open_as_commit(&commit, repo, commit_id);
5439 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5440 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5442 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5444 error = show_rebase_progress(commit, commit_id, NULL);
5446 error = show_rebase_progress(commit, commit_id, new_commit_id);
5447 free(new_commit_id);
5450 got_object_commit_close(commit);
5454 struct check_path_prefix_arg {
5455 const char *path_prefix;
5460 static const struct got_error *
5461 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5462 struct got_blob_object *blob2, struct got_object_id *id1,
5463 struct got_object_id *id2, const char *path1, const char *path2,
5464 mode_t mode1, mode_t mode2, struct got_repository *repo)
5466 struct check_path_prefix_arg *a = arg;
5468 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5469 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5470 return got_error(a->errcode);
5475 static const struct got_error *
5476 check_path_prefix(struct got_object_id *parent_id,
5477 struct got_object_id *commit_id, const char *path_prefix,
5478 int errcode, struct got_repository *repo)
5480 const struct got_error *err;
5481 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5482 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5483 struct check_path_prefix_arg cpp_arg;
5485 if (got_path_is_root_dir(path_prefix))
5488 err = got_object_open_as_commit(&commit, repo, commit_id);
5492 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5496 err = got_object_open_as_tree(&tree1, repo,
5497 got_object_commit_get_tree_id(parent_commit));
5501 err = got_object_open_as_tree(&tree2, repo,
5502 got_object_commit_get_tree_id(commit));
5506 cpp_arg.path_prefix = path_prefix;
5507 while (cpp_arg.path_prefix[0] == '/')
5508 cpp_arg.path_prefix++;
5509 cpp_arg.len = strlen(cpp_arg.path_prefix);
5510 cpp_arg.errcode = errcode;
5511 err = got_diff_tree(tree1, tree2, "", "", repo,
5512 check_path_prefix_in_diff, &cpp_arg, 0);
5515 got_object_tree_close(tree1);
5517 got_object_tree_close(tree2);
5519 got_object_commit_close(commit);
5521 got_object_commit_close(parent_commit);
5525 static const struct got_error *
5526 collect_commits(struct got_object_id_queue *commits,
5527 struct got_object_id *initial_commit_id,
5528 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5529 const char *path_prefix, int path_prefix_errcode,
5530 struct got_repository *repo)
5532 const struct got_error *err = NULL;
5533 struct got_commit_graph *graph = NULL;
5534 struct got_object_id *parent_id = NULL;
5535 struct got_object_qid *qid;
5536 struct got_object_id *commit_id = initial_commit_id;
5538 err = got_commit_graph_open(&graph, "/", 1);
5542 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5543 check_cancelled, NULL);
5546 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5547 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5548 check_cancelled, NULL);
5550 if (err->code == GOT_ERR_ITER_COMPLETED) {
5551 err = got_error_msg(GOT_ERR_ANCESTRY,
5552 "ran out of commits to rebase before "
5553 "youngest common ancestor commit has "
5558 err = check_path_prefix(parent_id, commit_id,
5559 path_prefix, path_prefix_errcode, repo);
5563 err = got_object_qid_alloc(&qid, commit_id);
5566 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5567 commit_id = parent_id;
5571 got_commit_graph_close(graph);
5575 static const struct got_error *
5576 cmd_rebase(int argc, char *argv[])
5578 const struct got_error *error = NULL;
5579 struct got_worktree *worktree = NULL;
5580 struct got_repository *repo = NULL;
5581 struct got_fileindex *fileindex = NULL;
5583 struct got_reference *branch = NULL;
5584 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5585 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5586 struct got_object_id *resume_commit_id = NULL;
5587 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5588 struct got_commit_object *commit = NULL;
5589 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5590 int histedit_in_progress = 0;
5591 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5592 struct got_object_id_queue commits;
5593 struct got_pathlist_head merged_paths;
5594 const struct got_object_id_queue *parent_ids;
5595 struct got_object_qid *qid, *pid;
5597 SIMPLEQ_INIT(&commits);
5598 TAILQ_INIT(&merged_paths);
5600 while ((ch = getopt(argc, argv, "ac")) != -1) {
5606 continue_rebase = 1;
5618 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5619 "unveil", NULL) == -1)
5622 if (abort_rebase && continue_rebase)
5624 else if (abort_rebase || continue_rebase) {
5627 } else if (argc != 1)
5630 cwd = getcwd(NULL, 0);
5632 error = got_error_from_errno("getcwd");
5635 error = got_worktree_open(&worktree, cwd);
5639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5644 error = apply_unveil(got_repo_get_path(repo), 0,
5645 got_worktree_get_root_path(worktree));
5649 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5653 if (histedit_in_progress) {
5654 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5658 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5664 if (!rebase_in_progress) {
5665 error = got_error(GOT_ERR_NOT_REBASING);
5668 error = got_worktree_rebase_continue(&resume_commit_id,
5669 &new_base_branch, &tmp_branch, &branch, &fileindex,
5673 printf("Switching work tree to %s\n",
5674 got_ref_get_symref_target(new_base_branch));
5675 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5676 new_base_branch, update_progress, &did_something);
5679 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5680 goto done; /* nothing else to do */
5683 if (continue_rebase) {
5684 if (!rebase_in_progress) {
5685 error = got_error(GOT_ERR_NOT_REBASING);
5688 error = got_worktree_rebase_continue(&resume_commit_id,
5689 &new_base_branch, &tmp_branch, &branch, &fileindex,
5694 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5695 resume_commit_id, repo);
5699 yca_id = got_object_id_dup(resume_commit_id);
5700 if (yca_id == NULL) {
5701 error = got_error_from_errno("got_object_id_dup");
5705 error = got_ref_open(&branch, repo, argv[0], 0);
5710 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5714 if (!continue_rebase) {
5715 struct got_object_id *base_commit_id;
5717 base_commit_id = got_worktree_get_base_commit_id(worktree);
5718 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5719 base_commit_id, branch_head_commit_id, repo,
5720 check_cancelled, NULL);
5723 if (yca_id == NULL) {
5724 error = got_error_msg(GOT_ERR_ANCESTRY,
5725 "specified branch shares no common ancestry "
5726 "with work tree's branch");
5730 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5732 if (error->code != GOT_ERR_ANCESTRY)
5736 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5737 "specified branch resolves to a commit which "
5738 "is already contained in work tree's branch");
5741 error = got_worktree_rebase_prepare(&new_base_branch,
5742 &tmp_branch, &fileindex, worktree, branch, repo);
5747 commit_id = branch_head_commit_id;
5748 error = got_object_open_as_commit(&commit, repo, commit_id);
5752 parent_ids = got_object_commit_get_parent_ids(commit);
5753 pid = SIMPLEQ_FIRST(parent_ids);
5755 if (!continue_rebase) {
5757 error = got_worktree_rebase_abort(worktree, fileindex,
5758 repo, new_base_branch, update_progress,
5762 printf("Rebase of %s aborted\n",
5763 got_ref_get_name(branch));
5765 error = got_error(GOT_ERR_EMPTY_REBASE);
5768 error = collect_commits(&commits, commit_id, pid->id,
5769 yca_id, got_worktree_get_path_prefix(worktree),
5770 GOT_ERR_REBASE_PATH, repo);
5771 got_object_commit_close(commit);
5776 if (SIMPLEQ_EMPTY(&commits)) {
5777 if (continue_rebase) {
5778 error = rebase_complete(worktree, fileindex,
5779 branch, new_base_branch, tmp_branch, repo);
5782 /* Fast-forward the reference of the branch. */
5783 struct got_object_id *new_head_commit_id;
5785 error = got_ref_resolve(&new_head_commit_id, repo,
5789 error = got_object_id_str(&id_str, new_head_commit_id);
5790 printf("Forwarding %s to commit %s\n",
5791 got_ref_get_name(branch), id_str);
5793 error = got_ref_change_ref(branch,
5794 new_head_commit_id);
5801 SIMPLEQ_FOREACH(qid, &commits, entry) {
5802 commit_id = qid->id;
5803 parent_id = pid ? pid->id : yca_id;
5806 error = got_worktree_rebase_merge_files(&merged_paths,
5807 worktree, fileindex, parent_id, commit_id, repo,
5808 rebase_progress, &rebase_status, check_cancelled, NULL);
5812 if (rebase_status == GOT_STATUS_CONFLICT) {
5813 error = show_rebase_merge_conflict(qid->id, repo);
5816 got_worktree_rebase_pathlist_free(&merged_paths);
5820 error = rebase_commit(&merged_paths, worktree, fileindex,
5821 tmp_branch, commit_id, repo);
5822 got_worktree_rebase_pathlist_free(&merged_paths);
5827 if (rebase_status == GOT_STATUS_CONFLICT) {
5828 error = got_worktree_rebase_postpone(worktree, fileindex);
5831 error = got_error_msg(GOT_ERR_CONFLICTS,
5832 "conflicts must be resolved before rebasing can continue");
5834 error = rebase_complete(worktree, fileindex, branch,
5835 new_base_branch, tmp_branch, repo);
5837 got_object_id_queue_free(&commits);
5838 free(branch_head_commit_id);
5839 free(resume_commit_id);
5842 got_object_commit_close(commit);
5844 got_ref_close(branch);
5845 if (new_base_branch)
5846 got_ref_close(new_base_branch);
5848 got_ref_close(tmp_branch);
5850 got_worktree_close(worktree);
5852 got_repo_close(repo);
5857 usage_histedit(void)
5859 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5864 #define GOT_HISTEDIT_PICK 'p'
5865 #define GOT_HISTEDIT_EDIT 'e'
5866 #define GOT_HISTEDIT_FOLD 'f'
5867 #define GOT_HISTEDIT_DROP 'd'
5868 #define GOT_HISTEDIT_MESG 'm'
5870 static struct got_histedit_cmd {
5874 } got_histedit_cmds[] = {
5875 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5876 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5877 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5879 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5880 { GOT_HISTEDIT_MESG, "mesg",
5881 "single-line log message for commit above (open editor if empty)" },
5884 struct got_histedit_list_entry {
5885 TAILQ_ENTRY(got_histedit_list_entry) entry;
5886 struct got_object_id *commit_id;
5887 const struct got_histedit_cmd *cmd;
5890 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5892 static const struct got_error *
5893 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5894 FILE *f, struct got_repository *repo)
5896 const struct got_error *err = NULL;
5897 char *logmsg = NULL, *id_str = NULL;
5898 struct got_commit_object *commit = NULL;
5901 err = got_object_open_as_commit(&commit, repo, commit_id);
5905 err = get_short_logmsg(&logmsg, 34, commit);
5909 err = got_object_id_str(&id_str, commit_id);
5913 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5915 err = got_ferror(f, GOT_ERR_IO);
5918 got_object_commit_close(commit);
5924 static const struct got_error *
5925 histedit_write_commit_list(struct got_object_id_queue *commits,
5926 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5928 const struct got_error *err = NULL;
5929 struct got_object_qid *qid;
5931 if (SIMPLEQ_EMPTY(commits))
5932 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5934 SIMPLEQ_FOREACH(qid, commits, entry) {
5935 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5939 if (edit_logmsg_only) {
5940 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5942 err = got_ferror(f, GOT_ERR_IO);
5951 static const struct got_error *
5952 write_cmd_list(FILE *f, const char *branch_name,
5953 struct got_object_id_queue *commits)
5955 const struct got_error *err = NULL;
5958 struct got_object_qid *qid;
5960 qid = SIMPLEQ_FIRST(commits);
5961 err = got_object_id_str(&id_str, qid->id);
5966 "# Editing the history of branch '%s' starting at\n"
5968 "# Commits will be processed in order from top to "
5969 "bottom of this file.\n", branch_name, id_str);
5971 err = got_ferror(f, GOT_ERR_IO);
5975 n = fprintf(f, "# Available histedit commands:\n");
5977 err = got_ferror(f, GOT_ERR_IO);
5981 for (i = 0; i < nitems(got_histedit_cmds); i++) {
5982 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5983 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5986 err = got_ferror(f, GOT_ERR_IO);
5995 static const struct got_error *
5996 histedit_syntax_error(int lineno)
5998 static char msg[42];
6001 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6003 if (ret == -1 || ret >= sizeof(msg))
6004 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6006 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6009 static const struct got_error *
6010 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6011 char *logmsg, struct got_repository *repo)
6013 const struct got_error *err;
6014 struct got_commit_object *folded_commit = NULL;
6015 char *id_str, *folded_logmsg = NULL;
6017 err = got_object_id_str(&id_str, hle->commit_id);
6021 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6025 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6028 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6029 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6030 folded_logmsg) == -1) {
6031 err = got_error_from_errno("asprintf");
6035 got_object_commit_close(folded_commit);
6037 free(folded_logmsg);
6041 static struct got_histedit_list_entry *
6042 get_folded_commits(struct got_histedit_list_entry *hle)
6044 struct got_histedit_list_entry *prev, *folded = NULL;
6046 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6047 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6048 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6049 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6051 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6057 static const struct got_error *
6058 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6059 struct got_repository *repo)
6061 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6062 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6063 const struct got_error *err = NULL;
6064 struct got_commit_object *commit = NULL;
6066 struct got_histedit_list_entry *folded = NULL;
6068 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6072 folded = get_folded_commits(hle);
6074 while (folded != hle) {
6075 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6076 folded = TAILQ_NEXT(folded, entry);
6079 err = append_folded_commit_msg(&new_msg, folded,
6085 folded = TAILQ_NEXT(folded, entry);
6089 err = got_object_id_str(&id_str, hle->commit_id);
6092 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6095 if (asprintf(&new_msg,
6096 "%s\n# original log message of commit %s: %s",
6097 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6098 err = got_error_from_errno("asprintf");
6104 err = got_object_id_str(&id_str, hle->commit_id);
6108 err = got_opentemp_named_fd(&logmsg_path, &fd,
6109 GOT_TMPDIR_STR "/got-logmsg");
6113 dprintf(fd, logmsg);
6116 err = get_editor(&editor);
6120 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6122 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6124 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6127 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6128 err = got_error_from_errno2("unlink", logmsg_path);
6134 got_object_commit_close(commit);
6138 static const struct got_error *
6139 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6140 FILE *f, struct got_repository *repo)
6142 const struct got_error *err = NULL;
6143 char *line = NULL, *p, *end;
6147 const struct got_histedit_cmd *cmd;
6148 struct got_object_id *commit_id = NULL;
6149 struct got_histedit_list_entry *hle = NULL;
6152 len = getline(&line, &size, f);
6154 const struct got_error *getline_err;
6157 getline_err = got_error_from_errno("getline");
6158 err = got_ferror(f, getline_err->code);
6163 while (isspace((unsigned char)p[0]))
6165 if (p[0] == '#' || p[0] == '\0') {
6171 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6172 cmd = &got_histedit_cmds[i];
6173 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6174 isspace((unsigned char)p[strlen(cmd->name)])) {
6175 p += strlen(cmd->name);
6178 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6183 if (i == nitems(got_histedit_cmds)) {
6184 err = histedit_syntax_error(lineno);
6187 while (isspace((unsigned char)p[0]))
6189 if (cmd->code == GOT_HISTEDIT_MESG) {
6190 if (hle == NULL || hle->logmsg != NULL) {
6191 err = got_error(GOT_ERR_HISTEDIT_CMD);
6195 err = histedit_edit_logmsg(hle, repo);
6199 hle->logmsg = strdup(p);
6200 if (hle->logmsg == NULL) {
6201 err = got_error_from_errno("strdup");
6210 while (end[0] && !isspace((unsigned char)end[0]))
6214 err = got_object_resolve_id_str(&commit_id, repo, p);
6216 /* override error code */
6217 err = histedit_syntax_error(lineno);
6221 hle = malloc(sizeof(*hle));
6223 err = got_error_from_errno("malloc");
6227 hle->commit_id = commit_id;
6232 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6240 static const struct got_error *
6241 histedit_check_script(struct got_histedit_list *histedit_cmds,
6242 struct got_object_id_queue *commits, struct got_repository *repo)
6244 const struct got_error *err = NULL;
6245 struct got_object_qid *qid;
6246 struct got_histedit_list_entry *hle;
6247 static char msg[92];
6250 if (TAILQ_EMPTY(histedit_cmds))
6251 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6252 "histedit script contains no commands");
6253 if (SIMPLEQ_EMPTY(commits))
6254 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6256 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6257 struct got_histedit_list_entry *hle2;
6258 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6261 if (got_object_id_cmp(hle->commit_id,
6262 hle2->commit_id) != 0)
6264 err = got_object_id_str(&id_str, hle->commit_id);
6267 snprintf(msg, sizeof(msg), "commit %s is listed "
6268 "more than once in histedit script", id_str);
6270 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6274 SIMPLEQ_FOREACH(qid, commits, entry) {
6275 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6276 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6280 err = got_object_id_str(&id_str, qid->id);
6283 snprintf(msg, sizeof(msg),
6284 "commit %s missing from histedit script", id_str);
6286 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6290 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6291 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6292 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6293 "last commit in histedit script cannot be folded");
6298 static const struct got_error *
6299 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6300 const char *path, struct got_object_id_queue *commits,
6301 struct got_repository *repo)
6303 const struct got_error *err = NULL;
6307 err = get_editor(&editor);
6311 if (spawn_editor(editor, path) == -1) {
6312 err = got_error_from_errno("failed spawning editor");
6316 f = fopen(path, "r");
6318 err = got_error_from_errno("fopen");
6321 err = histedit_parse_list(histedit_cmds, f, repo);
6325 err = histedit_check_script(histedit_cmds, commits, repo);
6327 if (f && fclose(f) != 0 && err == NULL)
6328 err = got_error_from_errno("fclose");
6333 static const struct got_error *
6334 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6335 struct got_object_id_queue *, const char *, const char *,
6336 struct got_repository *);
6338 static const struct got_error *
6339 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6340 struct got_object_id_queue *commits, const char *branch_name,
6341 int edit_logmsg_only, struct got_repository *repo)
6343 const struct got_error *err;
6347 err = got_opentemp_named(&path, &f, "got-histedit");
6351 err = write_cmd_list(f, branch_name, commits);
6355 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6359 if (edit_logmsg_only) {
6361 err = histedit_parse_list(histedit_cmds, f, repo);
6363 if (fclose(f) != 0) {
6364 err = got_error_from_errno("fclose");
6368 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6370 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6371 err->code != GOT_ERR_HISTEDIT_CMD)
6373 err = histedit_edit_list_retry(histedit_cmds, err,
6374 commits, path, branch_name, repo);
6378 if (f && fclose(f) != 0 && err == NULL)
6379 err = got_error_from_errno("fclose");
6380 if (path && unlink(path) != 0 && err == NULL)
6381 err = got_error_from_errno2("unlink", path);
6386 static const struct got_error *
6387 histedit_save_list(struct got_histedit_list *histedit_cmds,
6388 struct got_worktree *worktree, struct got_repository *repo)
6390 const struct got_error *err = NULL;
6393 struct got_histedit_list_entry *hle;
6394 struct got_commit_object *commit = NULL;
6396 err = got_worktree_get_histedit_script_path(&path, worktree);
6400 f = fopen(path, "w");
6402 err = got_error_from_errno2("fopen", path);
6405 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6406 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6412 int n = fprintf(f, "%c %s\n",
6413 GOT_HISTEDIT_MESG, hle->logmsg);
6415 err = got_ferror(f, GOT_ERR_IO);
6421 if (f && fclose(f) != 0 && err == NULL)
6422 err = got_error_from_errno("fclose");
6425 got_object_commit_close(commit);
6430 histedit_free_list(struct got_histedit_list *histedit_cmds)
6432 struct got_histedit_list_entry *hle;
6434 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6435 TAILQ_REMOVE(histedit_cmds, hle, entry);
6440 static const struct got_error *
6441 histedit_load_list(struct got_histedit_list *histedit_cmds,
6442 const char *path, struct got_repository *repo)
6444 const struct got_error *err = NULL;
6447 f = fopen(path, "r");
6449 err = got_error_from_errno2("fopen", path);
6453 err = histedit_parse_list(histedit_cmds, f, repo);
6455 if (f && fclose(f) != 0 && err == NULL)
6456 err = got_error_from_errno("fclose");
6460 static const struct got_error *
6461 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6462 const struct got_error *edit_err, struct got_object_id_queue *commits,
6463 const char *path, const char *branch_name, struct got_repository *repo)
6465 const struct got_error *err = NULL, *prev_err = edit_err;
6468 while (resp != 'c' && resp != 'r' && resp != 'a') {
6469 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6470 "or (a)bort: ", getprogname(), prev_err->msg);
6475 histedit_free_list(histedit_cmds);
6476 err = histedit_run_editor(histedit_cmds, path, commits,
6479 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6480 err->code != GOT_ERR_HISTEDIT_CMD)
6487 } else if (resp == 'r') {
6488 histedit_free_list(histedit_cmds);
6489 err = histedit_edit_script(histedit_cmds,
6490 commits, branch_name, 0, repo);
6492 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6493 err->code != GOT_ERR_HISTEDIT_CMD)
6500 } else if (resp == 'a') {
6501 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6504 printf("invalid response '%c'\n", resp);
6510 static const struct got_error *
6511 histedit_complete(struct got_worktree *worktree,
6512 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6513 struct got_reference *branch, struct got_repository *repo)
6515 printf("Switching work tree to %s\n",
6516 got_ref_get_symref_target(branch));
6517 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6521 static const struct got_error *
6522 show_histedit_progress(struct got_commit_object *commit,
6523 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6525 const struct got_error *err;
6526 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6528 err = got_object_id_str(&old_id_str, hle->commit_id);
6533 err = got_object_id_str(&new_id_str, new_id);
6538 old_id_str[12] = '\0';
6540 new_id_str[12] = '\0';
6543 logmsg = strdup(hle->logmsg);
6544 if (logmsg == NULL) {
6545 err = got_error_from_errno("strdup");
6548 trim_logmsg(logmsg, 42);
6550 err = get_short_logmsg(&logmsg, 42, commit);
6555 switch (hle->cmd->code) {
6556 case GOT_HISTEDIT_PICK:
6557 case GOT_HISTEDIT_EDIT:
6558 printf("%s -> %s: %s\n", old_id_str,
6559 new_id_str ? new_id_str : "no-op change", logmsg);
6561 case GOT_HISTEDIT_DROP:
6562 case GOT_HISTEDIT_FOLD:
6563 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6575 static const struct got_error *
6576 histedit_commit(struct got_pathlist_head *merged_paths,
6577 struct got_worktree *worktree, struct got_fileindex *fileindex,
6578 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6579 struct got_repository *repo)
6581 const struct got_error *err;
6582 struct got_commit_object *commit;
6583 struct got_object_id *new_commit_id;
6585 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6586 && hle->logmsg == NULL) {
6587 err = histedit_edit_logmsg(hle, repo);
6592 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6596 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6597 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6600 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6602 err = show_histedit_progress(commit, hle, NULL);
6604 err = show_histedit_progress(commit, hle, new_commit_id);
6605 free(new_commit_id);
6608 got_object_commit_close(commit);
6612 static const struct got_error *
6613 histedit_skip_commit(struct got_histedit_list_entry *hle,
6614 struct got_worktree *worktree, struct got_repository *repo)
6616 const struct got_error *error;
6617 struct got_commit_object *commit;
6619 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6624 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6628 error = show_histedit_progress(commit, hle, NULL);
6629 got_object_commit_close(commit);
6633 static const struct got_error *
6634 check_local_changes(void *arg, unsigned char status,
6635 unsigned char staged_status, const char *path,
6636 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6637 struct got_object_id *commit_id, int dirfd, const char *de_name)
6639 int *have_local_changes = arg;
6642 case GOT_STATUS_ADD:
6643 case GOT_STATUS_DELETE:
6644 case GOT_STATUS_MODIFY:
6645 case GOT_STATUS_CONFLICT:
6646 *have_local_changes = 1;
6647 return got_error(GOT_ERR_CANCELLED);
6652 switch (staged_status) {
6653 case GOT_STATUS_ADD:
6654 case GOT_STATUS_DELETE:
6655 case GOT_STATUS_MODIFY:
6656 *have_local_changes = 1;
6657 return got_error(GOT_ERR_CANCELLED);
6665 static const struct got_error *
6666 cmd_histedit(int argc, char *argv[])
6668 const struct got_error *error = NULL;
6669 struct got_worktree *worktree = NULL;
6670 struct got_fileindex *fileindex = NULL;
6671 struct got_repository *repo = NULL;
6673 struct got_reference *branch = NULL;
6674 struct got_reference *tmp_branch = NULL;
6675 struct got_object_id *resume_commit_id = NULL;
6676 struct got_object_id *base_commit_id = NULL;
6677 struct got_object_id *head_commit_id = NULL;
6678 struct got_commit_object *commit = NULL;
6679 int ch, rebase_in_progress = 0, did_something;
6680 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6681 int edit_logmsg_only = 0;
6682 const char *edit_script_path = NULL;
6683 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6684 struct got_object_id_queue commits;
6685 struct got_pathlist_head merged_paths;
6686 const struct got_object_id_queue *parent_ids;
6687 struct got_object_qid *pid;
6688 struct got_histedit_list histedit_cmds;
6689 struct got_histedit_list_entry *hle;
6691 SIMPLEQ_INIT(&commits);
6692 TAILQ_INIT(&histedit_cmds);
6693 TAILQ_INIT(&merged_paths);
6695 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6704 edit_script_path = optarg;
6707 edit_logmsg_only = 1;
6719 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6720 "unveil", NULL) == -1)
6723 if (abort_edit && continue_edit)
6724 errx(1, "histedit's -a and -c options are mutually exclusive");
6725 if (edit_script_path && edit_logmsg_only)
6726 errx(1, "histedit's -F and -m options are mutually exclusive");
6727 if (abort_edit && edit_logmsg_only)
6728 errx(1, "histedit's -a and -m options are mutually exclusive");
6729 if (continue_edit && edit_logmsg_only)
6730 errx(1, "histedit's -c and -m options are mutually exclusive");
6735 * This command cannot apply unveil(2) in all cases because the
6736 * user may choose to run an editor to edit the histedit script
6737 * and to edit individual commit log messages.
6738 * unveil(2) traverses exec(2); if an editor is used we have to
6739 * apply unveil after edit script and log messages have been written.
6740 * XXX TODO: Make use of unveil(2) where possible.
6743 cwd = getcwd(NULL, 0);
6745 error = got_error_from_errno("getcwd");
6748 error = got_worktree_open(&worktree, cwd);
6752 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6757 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6760 if (rebase_in_progress) {
6761 error = got_error(GOT_ERR_REBASING);
6765 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6769 if (edit_in_progress && edit_logmsg_only) {
6770 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6771 "histedit operation is in progress in this "
6772 "work tree and must be continued or aborted "
6773 "before the -m option can be used");
6777 if (edit_in_progress && abort_edit) {
6778 error = got_worktree_histedit_continue(&resume_commit_id,
6779 &tmp_branch, &branch, &base_commit_id, &fileindex,
6783 printf("Switching work tree to %s\n",
6784 got_ref_get_symref_target(branch));
6785 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6786 branch, base_commit_id, update_progress, &did_something);
6789 printf("Histedit of %s aborted\n",
6790 got_ref_get_symref_target(branch));
6791 goto done; /* nothing else to do */
6792 } else if (abort_edit) {
6793 error = got_error(GOT_ERR_NOT_HISTEDIT);
6797 if (continue_edit) {
6800 if (!edit_in_progress) {
6801 error = got_error(GOT_ERR_NOT_HISTEDIT);
6805 error = got_worktree_get_histedit_script_path(&path, worktree);
6809 error = histedit_load_list(&histedit_cmds, path, repo);
6814 error = got_worktree_histedit_continue(&resume_commit_id,
6815 &tmp_branch, &branch, &base_commit_id, &fileindex,
6820 error = got_ref_resolve(&head_commit_id, repo, branch);
6824 error = got_object_open_as_commit(&commit, repo,
6828 parent_ids = got_object_commit_get_parent_ids(commit);
6829 pid = SIMPLEQ_FIRST(parent_ids);
6831 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6834 error = collect_commits(&commits, head_commit_id, pid->id,
6835 base_commit_id, got_worktree_get_path_prefix(worktree),
6836 GOT_ERR_HISTEDIT_PATH, repo);
6837 got_object_commit_close(commit);
6842 if (edit_in_progress) {
6843 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6847 error = got_ref_open(&branch, repo,
6848 got_worktree_get_head_ref_name(worktree), 0);
6852 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6853 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6854 "will not edit commit history of a branch outside "
6855 "the \"refs/heads/\" reference namespace");
6859 error = got_ref_resolve(&head_commit_id, repo, branch);
6860 got_ref_close(branch);
6865 error = got_object_open_as_commit(&commit, repo,
6869 parent_ids = got_object_commit_get_parent_ids(commit);
6870 pid = SIMPLEQ_FIRST(parent_ids);
6872 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6875 error = collect_commits(&commits, head_commit_id, pid->id,
6876 got_worktree_get_base_commit_id(worktree),
6877 got_worktree_get_path_prefix(worktree),
6878 GOT_ERR_HISTEDIT_PATH, repo);
6879 got_object_commit_close(commit);
6884 if (SIMPLEQ_EMPTY(&commits)) {
6885 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6889 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6890 &base_commit_id, &fileindex, worktree, repo);
6894 if (edit_script_path) {
6895 error = histedit_load_list(&histedit_cmds,
6896 edit_script_path, repo);
6898 got_worktree_histedit_abort(worktree, fileindex,
6899 repo, branch, base_commit_id,
6900 update_progress, &did_something);
6904 const char *branch_name;
6905 branch_name = got_ref_get_symref_target(branch);
6906 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6908 error = histedit_edit_script(&histedit_cmds, &commits,
6909 branch_name, edit_logmsg_only, repo);
6911 got_worktree_histedit_abort(worktree, fileindex,
6912 repo, branch, base_commit_id,
6913 update_progress, &did_something);
6919 error = histedit_save_list(&histedit_cmds, worktree,
6922 got_worktree_histedit_abort(worktree, fileindex,
6923 repo, branch, base_commit_id,
6924 update_progress, &did_something);
6930 error = histedit_check_script(&histedit_cmds, &commits, repo);
6934 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6935 if (resume_commit_id) {
6936 if (got_object_id_cmp(hle->commit_id,
6937 resume_commit_id) != 0)
6940 resume_commit_id = NULL;
6941 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6942 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6943 error = histedit_skip_commit(hle, worktree,
6948 struct got_pathlist_head paths;
6949 int have_changes = 0;
6952 error = got_pathlist_append(&paths, "", NULL);
6955 error = got_worktree_status(worktree, &paths,
6956 repo, check_local_changes, &have_changes,
6957 check_cancelled, NULL);
6958 got_pathlist_free(&paths);
6960 if (error->code != GOT_ERR_CANCELLED)
6962 if (sigint_received || sigpipe_received)
6966 error = histedit_commit(NULL, worktree,
6967 fileindex, tmp_branch, hle, repo);
6971 error = got_object_open_as_commit(
6972 &commit, repo, hle->commit_id);
6975 error = show_histedit_progress(commit,
6977 got_object_commit_close(commit);
6986 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6987 error = histedit_skip_commit(hle, worktree, repo);
6993 error = got_object_open_as_commit(&commit, repo,
6997 parent_ids = got_object_commit_get_parent_ids(commit);
6998 pid = SIMPLEQ_FIRST(parent_ids);
7000 error = got_worktree_histedit_merge_files(&merged_paths,
7001 worktree, fileindex, pid->id, hle->commit_id, repo,
7002 rebase_progress, &rebase_status, check_cancelled, NULL);
7005 got_object_commit_close(commit);
7008 if (rebase_status == GOT_STATUS_CONFLICT) {
7009 error = show_rebase_merge_conflict(hle->commit_id,
7013 got_worktree_rebase_pathlist_free(&merged_paths);
7017 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7019 error = got_object_id_str(&id_str, hle->commit_id);
7022 printf("Stopping histedit for amending commit %s\n",
7025 got_worktree_rebase_pathlist_free(&merged_paths);
7026 error = got_worktree_histedit_postpone(worktree,
7031 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7032 error = histedit_skip_commit(hle, worktree, repo);
7038 error = histedit_commit(&merged_paths, worktree, fileindex,
7039 tmp_branch, hle, repo);
7040 got_worktree_rebase_pathlist_free(&merged_paths);
7045 if (rebase_status == GOT_STATUS_CONFLICT) {
7046 error = got_worktree_histedit_postpone(worktree, fileindex);
7049 error = got_error_msg(GOT_ERR_CONFLICTS,
7050 "conflicts must be resolved before histedit can continue");
7052 error = histedit_complete(worktree, fileindex, tmp_branch,
7055 got_object_id_queue_free(&commits);
7056 histedit_free_list(&histedit_cmds);
7057 free(head_commit_id);
7058 free(base_commit_id);
7059 free(resume_commit_id);
7061 got_object_commit_close(commit);
7063 got_ref_close(branch);
7065 got_ref_close(tmp_branch);
7067 got_worktree_close(worktree);
7069 got_repo_close(repo);
7074 usage_integrate(void)
7076 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7080 static const struct got_error *
7081 cmd_integrate(int argc, char *argv[])
7083 const struct got_error *error = NULL;
7084 struct got_repository *repo = NULL;
7085 struct got_worktree *worktree = NULL;
7086 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7087 const char *branch_arg = NULL;
7088 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7089 struct got_fileindex *fileindex = NULL;
7090 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7091 int ch, did_something = 0;
7093 while ((ch = getopt(argc, argv, "")) != -1) {
7106 branch_arg = argv[0];
7108 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7109 "unveil", NULL) == -1)
7112 cwd = getcwd(NULL, 0);
7114 error = got_error_from_errno("getcwd");
7118 error = got_worktree_open(&worktree, cwd);
7122 error = check_rebase_or_histedit_in_progress(worktree);
7126 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7131 error = apply_unveil(got_repo_get_path(repo), 0,
7132 got_worktree_get_root_path(worktree));
7136 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7137 error = got_error_from_errno("asprintf");
7141 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7142 &base_branch_ref, worktree, refname, repo);
7146 refname = strdup(got_ref_get_name(branch_ref));
7147 if (refname == NULL) {
7148 error = got_error_from_errno("strdup");
7149 got_worktree_integrate_abort(worktree, fileindex, repo,
7150 branch_ref, base_branch_ref);
7153 base_refname = strdup(got_ref_get_name(base_branch_ref));
7154 if (base_refname == NULL) {
7155 error = got_error_from_errno("strdup");
7156 got_worktree_integrate_abort(worktree, fileindex, repo,
7157 branch_ref, base_branch_ref);
7161 error = got_ref_resolve(&commit_id, repo, branch_ref);
7165 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7169 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7170 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7171 "specified branch has already been integrated");
7172 got_worktree_integrate_abort(worktree, fileindex, repo,
7173 branch_ref, base_branch_ref);
7177 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7179 if (error->code == GOT_ERR_ANCESTRY)
7180 error = got_error(GOT_ERR_REBASE_REQUIRED);
7181 got_worktree_integrate_abort(worktree, fileindex, repo,
7182 branch_ref, base_branch_ref);
7186 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7187 branch_ref, base_branch_ref, update_progress, &did_something,
7188 check_cancelled, NULL);
7192 printf("Integrated %s into %s\n", refname, base_refname);
7195 got_repo_close(repo);
7197 got_worktree_close(worktree);
7199 free(base_commit_id);
7209 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7210 "[file-path ...]\n",
7215 static const struct got_error *
7216 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7217 const char *path, struct got_object_id *blob_id,
7218 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7219 int dirfd, const char *de_name)
7221 const struct got_error *err = NULL;
7222 char *id_str = NULL;
7224 if (staged_status != GOT_STATUS_ADD &&
7225 staged_status != GOT_STATUS_MODIFY &&
7226 staged_status != GOT_STATUS_DELETE)
7229 if (staged_status == GOT_STATUS_ADD ||
7230 staged_status == GOT_STATUS_MODIFY)
7231 err = got_object_id_str(&id_str, staged_blob_id);
7233 err = got_object_id_str(&id_str, blob_id);
7237 printf("%s %c %s\n", id_str, staged_status, path);
7242 static const struct got_error *
7243 cmd_stage(int argc, char *argv[])
7245 const struct got_error *error = NULL;
7246 struct got_repository *repo = NULL;
7247 struct got_worktree *worktree = NULL;
7249 struct got_pathlist_head paths;
7250 struct got_pathlist_entry *pe;
7251 int ch, list_stage = 0, pflag = 0;
7252 FILE *patch_script_file = NULL;
7253 const char *patch_script_path = NULL;
7254 struct choose_patch_arg cpa;
7258 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7267 patch_script_path = optarg;
7279 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7280 "unveil", NULL) == -1)
7283 if (list_stage && (pflag || patch_script_path))
7284 errx(1, "-l option cannot be used with other options");
7285 if (patch_script_path && !pflag)
7286 errx(1, "-F option can only be used together with -p option");
7288 cwd = getcwd(NULL, 0);
7290 error = got_error_from_errno("getcwd");
7294 error = got_worktree_open(&worktree, cwd);
7298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7303 if (patch_script_path) {
7304 patch_script_file = fopen(patch_script_path, "r");
7305 if (patch_script_file == NULL) {
7306 error = got_error_from_errno2("fopen",
7311 error = apply_unveil(got_repo_get_path(repo), 0,
7312 got_worktree_get_root_path(worktree));
7316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7321 error = got_worktree_status(worktree, &paths, repo,
7322 print_stage, NULL, check_cancelled, NULL);
7324 cpa.patch_script_file = patch_script_file;
7325 cpa.action = "stage";
7326 error = got_worktree_stage(worktree, &paths,
7327 pflag ? NULL : print_status, NULL,
7328 pflag ? choose_patch : NULL, &cpa, repo);
7331 if (patch_script_file && fclose(patch_script_file) == EOF &&
7333 error = got_error_from_errno2("fclose", patch_script_path);
7335 got_repo_close(repo);
7337 got_worktree_close(worktree);
7338 TAILQ_FOREACH(pe, &paths, entry)
7339 free((char *)pe->path);
7340 got_pathlist_free(&paths);
7348 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7349 "[file-path ...]\n",
7355 static const struct got_error *
7356 cmd_unstage(int argc, char *argv[])
7358 const struct got_error *error = NULL;
7359 struct got_repository *repo = NULL;
7360 struct got_worktree *worktree = NULL;
7362 struct got_pathlist_head paths;
7363 struct got_pathlist_entry *pe;
7364 int ch, did_something = 0, pflag = 0;
7365 FILE *patch_script_file = NULL;
7366 const char *patch_script_path = NULL;
7367 struct choose_patch_arg cpa;
7371 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7377 patch_script_path = optarg;
7389 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7390 "unveil", NULL) == -1)
7393 if (patch_script_path && !pflag)
7394 errx(1, "-F option can only be used together with -p option");
7396 cwd = getcwd(NULL, 0);
7398 error = got_error_from_errno("getcwd");
7402 error = got_worktree_open(&worktree, cwd);
7406 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7411 if (patch_script_path) {
7412 patch_script_file = fopen(patch_script_path, "r");
7413 if (patch_script_file == NULL) {
7414 error = got_error_from_errno2("fopen",
7420 error = apply_unveil(got_repo_get_path(repo), 0,
7421 got_worktree_get_root_path(worktree));
7425 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7429 cpa.patch_script_file = patch_script_file;
7430 cpa.action = "unstage";
7431 error = got_worktree_unstage(worktree, &paths, update_progress,
7432 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7434 if (patch_script_file && fclose(patch_script_file) == EOF &&
7436 error = got_error_from_errno2("fclose", patch_script_path);
7438 got_repo_close(repo);
7440 got_worktree_close(worktree);
7441 TAILQ_FOREACH(pe, &paths, entry)
7442 free((char *)pe->path);
7443 got_pathlist_free(&paths);
7451 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7452 "arg1 [arg2 ...]\n", getprogname());
7456 static const struct got_error *
7457 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7459 const struct got_error *err;
7460 struct got_blob_object *blob;
7462 err = got_object_open_as_blob(&blob, repo, id, 8192);
7466 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7467 got_object_blob_close(blob);
7471 static const struct got_error *
7472 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7474 const struct got_error *err;
7475 struct got_tree_object *tree;
7478 err = got_object_open_as_tree(&tree, repo, id);
7482 nentries = got_object_tree_get_nentries(tree);
7483 for (i = 0; i < nentries; i++) {
7484 struct got_tree_entry *te;
7486 if (sigint_received || sigpipe_received)
7488 te = got_object_tree_get_entry(tree, i);
7489 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7492 fprintf(outfile, "%s %.7o %s\n", id_str,
7493 got_tree_entry_get_mode(te),
7494 got_tree_entry_get_name(te));
7498 got_object_tree_close(tree);
7502 static const struct got_error *
7503 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7505 const struct got_error *err;
7506 struct got_commit_object *commit;
7507 const struct got_object_id_queue *parent_ids;
7508 struct got_object_qid *pid;
7509 char *id_str = NULL;
7510 const char *logmsg = NULL;
7512 err = got_object_open_as_commit(&commit, repo, id);
7516 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7520 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7521 parent_ids = got_object_commit_get_parent_ids(commit);
7522 fprintf(outfile, "numparents %d\n",
7523 got_object_commit_get_nparents(commit));
7524 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7526 err = got_object_id_str(&pid_str, pid->id);
7529 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7532 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7533 got_object_commit_get_author(commit),
7534 got_object_commit_get_author_time(commit));
7536 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7537 got_object_commit_get_author(commit),
7538 got_object_commit_get_committer_time(commit));
7540 logmsg = got_object_commit_get_logmsg_raw(commit);
7541 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7542 fprintf(outfile, "%s", logmsg);
7545 got_object_commit_close(commit);
7549 static const struct got_error *
7550 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7552 const struct got_error *err;
7553 struct got_tag_object *tag;
7554 char *id_str = NULL;
7555 const char *tagmsg = NULL;
7557 err = got_object_open_as_tag(&tag, repo, id);
7561 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7565 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7567 switch (got_object_tag_get_object_type(tag)) {
7568 case GOT_OBJ_TYPE_BLOB:
7569 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7570 GOT_OBJ_LABEL_BLOB);
7572 case GOT_OBJ_TYPE_TREE:
7573 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7574 GOT_OBJ_LABEL_TREE);
7576 case GOT_OBJ_TYPE_COMMIT:
7577 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7578 GOT_OBJ_LABEL_COMMIT);
7580 case GOT_OBJ_TYPE_TAG:
7581 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7588 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7589 got_object_tag_get_name(tag));
7591 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7592 got_object_tag_get_tagger(tag),
7593 got_object_tag_get_tagger_time(tag));
7595 tagmsg = got_object_tag_get_message(tag);
7596 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7597 fprintf(outfile, "%s", tagmsg);
7600 got_object_tag_close(tag);
7604 static const struct got_error *
7605 cmd_cat(int argc, char *argv[])
7607 const struct got_error *error;
7608 struct got_repository *repo = NULL;
7609 struct got_worktree *worktree = NULL;
7610 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7611 const char *commit_id_str = NULL;
7612 struct got_object_id *id = NULL, *commit_id = NULL;
7613 int ch, obj_type, i, force_path = 0;
7616 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7621 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7624 commit_id_str = optarg;
7627 repo_path = realpath(optarg, NULL);
7628 if (repo_path == NULL)
7629 return got_error_from_errno2("realpath",
7631 got_path_strip_trailing_slashes(repo_path);
7645 cwd = getcwd(NULL, 0);
7647 error = got_error_from_errno("getcwd");
7650 error = got_worktree_open(&worktree, cwd);
7651 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7654 if (repo_path == NULL) {
7656 got_worktree_get_repo_path(worktree));
7657 if (repo_path == NULL) {
7658 error = got_error_from_errno("strdup");
7664 if (repo_path == NULL) {
7665 repo_path = getcwd(NULL, 0);
7666 if (repo_path == NULL)
7667 return got_error_from_errno("getcwd");
7670 error = got_repo_open(&repo, repo_path, NULL);
7675 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7679 if (commit_id_str == NULL)
7680 commit_id_str = GOT_REF_HEAD;
7681 error = got_repo_match_object_id(&commit_id, NULL,
7682 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7686 for (i = 0; i < argc; i++) {
7688 error = got_object_id_by_path(&id, repo, commit_id,
7693 error = got_repo_match_object_id(&id, &label, argv[i],
7694 GOT_OBJ_TYPE_ANY, 0, repo);
7696 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7697 error->code != GOT_ERR_NOT_REF)
7699 error = got_object_id_by_path(&id, repo,
7700 commit_id, argv[i]);
7706 error = got_object_get_type(&obj_type, repo, id);
7711 case GOT_OBJ_TYPE_BLOB:
7712 error = cat_blob(id, repo, stdout);
7714 case GOT_OBJ_TYPE_TREE:
7715 error = cat_tree(id, repo, stdout);
7717 case GOT_OBJ_TYPE_COMMIT:
7718 error = cat_commit(id, repo, stdout);
7720 case GOT_OBJ_TYPE_TAG:
7721 error = cat_tag(id, repo, stdout);
7724 error = got_error(GOT_ERR_OBJ_TYPE);
7739 got_worktree_close(worktree);
7741 const struct got_error *repo_error;
7742 repo_error = got_repo_close(repo);