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>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 "[-R reference] repository-url [directory]\n", getprogname());
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
873 if (print_size || print_indexed || print_resolved)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
878 printf("; indexing %d%%", p_indexed);
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
887 static const struct got_error *
888 create_symref(const char *refname, struct got_reference *target_ref,
889 int verbosity, struct got_repository *repo)
891 const struct got_error *err;
892 struct got_reference *head_symref;
894 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
898 err = got_ref_write(head_symref, repo);
899 got_ref_close(head_symref);
900 if (err == NULL && verbosity > 0) {
901 printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 got_ref_get_name(target_ref));
907 static const struct got_error *
908 list_remote_refs(struct got_pathlist_head *symrefs,
909 struct got_pathlist_head *refs)
911 const struct got_error *err;
912 struct got_pathlist_entry *pe;
914 TAILQ_FOREACH(pe, symrefs, entry) {
915 const char *refname = pe->path;
916 const char *targetref = pe->data;
918 printf("%s: %s\n", refname, targetref);
921 TAILQ_FOREACH(pe, refs, entry) {
922 const char *refname = pe->path;
923 struct got_object_id *id = pe->data;
926 err = got_object_id_str(&id_str, id);
929 printf("%s: %s\n", refname, id_str);
936 static const struct got_error *
937 create_ref(const char *refname, struct got_object_id *id,
938 int verbosity, struct got_repository *repo)
940 const struct got_error *err = NULL;
941 struct got_reference *ref;
944 err = got_object_id_str(&id_str, id);
948 err = got_ref_alloc(&ref, refname, id);
952 err = got_ref_write(ref, repo);
955 if (err == NULL && verbosity >= 0)
956 printf("Created reference %s: %s\n", refname, id_str);
963 match_wanted_ref(const char *refname, const char *wanted_ref)
965 if (strncmp(refname, "refs/", 5) != 0)
970 * Prevent fetching of references that won't make any
971 * sense outside of the remote repository's context.
973 if (strncmp(refname, "got/", 4) == 0)
975 if (strncmp(refname, "remotes/", 8) == 0)
978 if (strncmp(wanted_ref, "refs/", 5) == 0)
981 /* Allow prefix match. */
982 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
985 /* Allow exact match. */
986 return (strcmp(refname, wanted_ref) == 0);
990 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
992 struct got_pathlist_entry *pe;
994 TAILQ_FOREACH(pe, wanted_refs, entry) {
995 if (match_wanted_ref(refname, pe->path))
1002 static const struct got_error *
1003 create_wanted_ref(const char *refname, struct got_object_id *id,
1004 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1006 const struct got_error *err;
1007 char *remote_refname;
1009 if (strncmp("refs/", refname, 5) == 0)
1012 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 remote_repo_name, refname) == -1)
1014 return got_error_from_errno("asprintf");
1016 err = create_ref(remote_refname, id, verbosity, repo);
1017 free(remote_refname);
1021 static const struct got_error *
1022 cmd_clone(int argc, char *argv[])
1024 const struct got_error *error = NULL;
1025 const char *uri, *dirname;
1026 char *proto, *host, *port, *repo_name, *server_path;
1027 char *default_destdir = NULL, *id_str = NULL;
1028 const char *repo_path;
1029 struct got_repository *repo = NULL;
1030 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 struct got_pathlist_entry *pe;
1032 struct got_object_id *pack_hash = NULL;
1033 int ch, fetchfd = -1, fetchstatus;
1034 pid_t fetchpid = -1;
1035 struct got_fetch_progress_arg fpa;
1036 char *git_url = NULL;
1037 char *gitconfig_path = NULL;
1038 char *gitconfig = NULL;
1039 FILE *gitconfig_file = NULL;
1041 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 int list_refs_only = 0;
1043 struct got_reference *head_symref = NULL;
1046 TAILQ_INIT(&symrefs);
1047 TAILQ_INIT(&wanted_branches);
1048 TAILQ_INIT(&wanted_refs);
1050 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1053 fetch_all_branches = 1;
1056 error = got_pathlist_append(&wanted_branches,
1065 mirror_references = 1;
1070 else if (verbosity < 3)
1077 error = got_pathlist_append(&wanted_refs,
1090 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 errx(1, "-a and -b options are mutually exclusive");
1092 if (list_refs_only) {
1093 if (!TAILQ_EMPTY(&wanted_branches))
1094 errx(1, "-l and -b options are mutually exclusive");
1095 if (fetch_all_branches)
1096 errx(1, "-l and -a options are mutually exclusive");
1097 if (mirror_references)
1098 errx(1, "-l and -m options are mutually exclusive");
1099 if (verbosity == -1)
1100 errx(1, "-l and -q options are mutually exclusive");
1101 if (!TAILQ_EMPTY(&wanted_refs))
1102 errx(1, "-l and -R options are mutually exclusive");
1114 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 &repo_name, argv[0]);
1119 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 host, port ? ":" : "", port ? port : "",
1121 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 error = got_error_from_errno("asprintf");
1126 if (strcmp(proto, "git") == 0) {
1128 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 "sendfd dns inet unveil", NULL) == -1)
1132 } else if (strcmp(proto, "git+ssh") == 0 ||
1133 strcmp(proto, "ssh") == 0) {
1135 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 "sendfd unveil", NULL) == -1)
1139 } else if (strcmp(proto, "http") == 0 ||
1140 strcmp(proto, "git+http") == 0) {
1141 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1144 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1147 if (dirname == NULL) {
1148 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 error = got_error_from_errno("asprintf");
1152 repo_path = default_destdir;
1154 repo_path = dirname;
1156 if (!list_refs_only) {
1157 error = got_path_mkdir(repo_path);
1161 error = got_repo_init(repo_path);
1164 error = got_repo_open(&repo, repo_path, NULL);
1169 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 error = got_error_from_errno2("unveil",
1172 GOT_FETCH_PATH_SSH);
1176 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1180 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 server_path, verbosity);
1186 printf("Connected to %s%s%s\n", host,
1187 port ? ":" : "", port ? port : "");
1189 fpa.last_scaled_size[0] = '\0';
1190 fpa.last_p_indexed = -1;
1191 fpa.last_p_resolved = -1;
1192 fpa.verbosity = verbosity;
1193 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 fetch_all_branches, &wanted_branches, &wanted_refs,
1196 list_refs_only, verbosity, fetchfd, repo,
1197 fetch_progress, &fpa);
1201 if (list_refs_only) {
1202 error = list_remote_refs(&symrefs, &refs);
1206 error = got_object_id_str(&id_str, pack_hash);
1210 printf("\nFetched %s.pack\n", id_str);
1213 /* Set up references provided with the pack file. */
1214 TAILQ_FOREACH(pe, &refs, entry) {
1215 const char *refname = pe->path;
1216 struct got_object_id *id = pe->data;
1217 char *remote_refname;
1219 if (is_wanted_ref(&wanted_refs, refname) &&
1220 !mirror_references) {
1221 error = create_wanted_ref(refname, id,
1222 GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 verbosity - 1, repo);
1229 error = create_ref(refname, id, verbosity - 1, repo);
1233 if (mirror_references)
1236 if (strncmp("refs/heads/", refname, 11) != 0)
1239 if (asprintf(&remote_refname,
1240 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 refname + 11) == -1) {
1242 error = got_error_from_errno("asprintf");
1245 error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 free(remote_refname);
1251 /* Set the HEAD reference if the server provided one. */
1252 TAILQ_FOREACH(pe, &symrefs, entry) {
1253 struct got_reference *target_ref;
1254 const char *refname = pe->path;
1255 const char *target = pe->data;
1256 char *remote_refname = NULL, *remote_target = NULL;
1258 if (strcmp(refname, GOT_REF_HEAD) != 0)
1261 error = got_ref_open(&target_ref, repo, target, 0);
1263 if (error->code == GOT_ERR_NOT_REF) {
1270 error = create_symref(refname, target_ref, verbosity, repo);
1271 got_ref_close(target_ref);
1275 if (mirror_references)
1278 if (strncmp("refs/heads/", target, 11) != 0)
1281 if (asprintf(&remote_refname,
1282 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1284 error = got_error_from_errno("asprintf");
1287 if (asprintf(&remote_target,
1288 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 target + 11) == -1) {
1290 error = got_error_from_errno("asprintf");
1291 free(remote_refname);
1294 error = got_ref_open(&target_ref, repo, remote_target, 0);
1296 free(remote_refname);
1297 free(remote_target);
1298 if (error->code == GOT_ERR_NOT_REF) {
1304 error = create_symref(remote_refname, target_ref,
1305 verbosity - 1, repo);
1306 free(remote_refname);
1307 free(remote_target);
1308 got_ref_close(target_ref);
1314 * We failed to set the HEAD reference. If we asked for
1315 * a set of wanted branches use the first of one of those
1316 * which could be fetched instead.
1318 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 const char *target = pe->path;
1320 struct got_reference *target_ref;
1322 error = got_ref_open(&target_ref, repo, target, 0);
1324 if (error->code == GOT_ERR_NOT_REF) {
1331 error = create_symref(GOT_REF_HEAD, target_ref,
1333 got_ref_close(target_ref);
1340 /* Create a config file git-fetch(1) can understand. */
1341 gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 if (gitconfig_path == NULL) {
1343 error = got_error_from_errno("got_repo_get_path_gitconfig");
1346 gitconfig_file = fopen(gitconfig_path, "a");
1347 if (gitconfig_file == NULL) {
1348 error = got_error_from_errno2("fopen", gitconfig_path);
1351 if (mirror_references) {
1352 if (asprintf(&gitconfig,
1355 "\tfetch = +refs/*:refs/*\n"
1356 "\tmirror = true\n",
1357 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 error = got_error_from_errno("asprintf");
1361 } else if (fetch_all_branches) {
1362 if (asprintf(&gitconfig,
1365 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 error = got_error_from_errno("asprintf");
1372 const char *branchname;
1375 * If the server specified a default branch, use just that one.
1376 * Otherwise fall back to fetching all branches on next fetch.
1379 branchname = got_ref_get_symref_target(head_symref);
1380 if (strncmp(branchname, "refs/heads/", 11) == 0)
1383 branchname = "*"; /* fall back to all branches */
1384 if (asprintf(&gitconfig,
1387 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 branchname) == -1) {
1391 error = got_error_from_errno("asprintf");
1395 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 if (n != strlen(gitconfig)) {
1397 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1402 printf("Created %s repository '%s'\n",
1403 mirror_references ? "mirrored" : "cloned", repo_path);
1406 if (kill(fetchpid, SIGTERM) == -1)
1407 error = got_error_from_errno("kill");
1408 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 error = got_error_from_errno("waitpid");
1411 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 error = got_error_from_errno("close");
1413 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 error = got_error_from_errno("fclose");
1416 got_repo_close(repo);
1418 got_ref_close(head_symref);
1419 TAILQ_FOREACH(pe, &refs, entry) {
1420 free((void *)pe->path);
1423 got_pathlist_free(&refs);
1424 TAILQ_FOREACH(pe, &symrefs, entry) {
1425 free((void *)pe->path);
1428 got_pathlist_free(&symrefs);
1429 got_pathlist_free(&wanted_branches);
1430 got_pathlist_free(&wanted_refs);
1437 free(default_destdir);
1438 free(gitconfig_path);
1443 static const struct got_error *
1444 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 int replace_tags, int verbosity, struct got_repository *repo)
1447 const struct got_error *err = NULL;
1448 char *new_id_str = NULL;
1449 struct got_object_id *old_id = NULL;
1451 err = got_object_id_str(&new_id_str, new_id);
1455 if (!replace_tags &&
1456 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 err = got_ref_resolve(&old_id, repo, ref);
1460 if (got_object_id_cmp(old_id, new_id) == 0)
1462 if (verbosity >= 0) {
1463 printf("Rejecting update of existing tag %s: %s\n",
1464 got_ref_get_name(ref), new_id_str);
1469 if (got_ref_is_symbolic(ref)) {
1470 if (verbosity >= 0) {
1471 printf("Replacing reference %s: %s\n",
1472 got_ref_get_name(ref),
1473 got_ref_get_symref_target(ref));
1475 err = got_ref_change_symref_to_ref(ref, new_id);
1478 err = got_ref_write(ref, repo);
1482 err = got_ref_resolve(&old_id, repo, ref);
1485 if (got_object_id_cmp(old_id, new_id) == 0)
1488 err = got_ref_change_ref(ref, new_id);
1491 err = got_ref_write(ref, repo);
1497 printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1508 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1509 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1510 "[remote-repository-name]\n",
1515 static const struct got_error *
1516 delete_missing_refs(struct got_pathlist_head *their_refs,
1517 int verbosity, struct got_repository *repo)
1519 const struct got_error *err = NULL;
1520 struct got_reflist_head my_refs;
1521 struct got_reflist_entry *re;
1522 struct got_pathlist_entry *pe;
1523 struct got_object_id *id;
1526 SIMPLEQ_INIT(&my_refs);
1528 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1532 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1533 const char *refname = got_ref_get_name(re->ref);
1535 if (strncmp(refname, "refs/heads/", 11) != 0 &&
1536 strncmp(refname, "refs/tags/", 10) != 0)
1539 TAILQ_FOREACH(pe, their_refs, entry) {
1540 if (strcmp(refname, pe->path) == 0)
1546 err = got_ref_resolve(&id, repo, re->ref);
1549 err = got_object_id_str(&id_str, id);
1555 err = got_ref_delete(re->ref, repo);
1558 if (verbosity >= 0) {
1559 printf("Deleted reference %s: %s\n",
1560 got_ref_get_name(re->ref), id_str);
1567 static const struct got_error *
1568 update_wanted_ref(const char *refname, struct got_object_id *id,
1569 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1571 const struct got_error *err, *unlock_err;
1572 char *remote_refname;
1573 struct got_reference *ref;
1575 if (strncmp("refs/", refname, 5) == 0)
1578 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1579 remote_repo_name, refname) == -1)
1580 return got_error_from_errno("asprintf");
1582 err = got_ref_open(&ref, repo, remote_refname, 1);
1584 if (err->code != GOT_ERR_NOT_REF)
1586 err = create_ref(remote_refname, id, verbosity, repo);
1588 err = update_ref(ref, id, 0, verbosity, repo);
1589 unlock_err = got_ref_unlock(ref);
1590 if (unlock_err && err == NULL)
1595 free(remote_refname);
1599 static const struct got_error *
1600 cmd_fetch(int argc, char *argv[])
1602 const struct got_error *error = NULL, *unlock_err;
1603 char *cwd = NULL, *repo_path = NULL;
1604 const char *remote_name;
1605 char *proto = NULL, *host = NULL, *port = NULL;
1606 char *repo_name = NULL, *server_path = NULL;
1607 struct got_remote_repo *remotes, *remote = NULL;
1609 char *id_str = NULL;
1610 struct got_repository *repo = NULL;
1611 struct got_worktree *worktree = NULL;
1612 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1613 struct got_pathlist_entry *pe;
1614 struct got_object_id *pack_hash = NULL;
1615 int i, ch, fetchfd = -1, fetchstatus;
1616 pid_t fetchpid = -1;
1617 struct got_fetch_progress_arg fpa;
1618 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1619 int delete_refs = 0, replace_tags = 0;
1622 TAILQ_INIT(&symrefs);
1623 TAILQ_INIT(&wanted_branches);
1624 TAILQ_INIT(&wanted_refs);
1626 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1629 fetch_all_branches = 1;
1632 error = got_pathlist_append(&wanted_branches,
1644 repo_path = realpath(optarg, NULL);
1645 if (repo_path == NULL)
1646 return got_error_from_errno2("realpath",
1648 got_path_strip_trailing_slashes(repo_path);
1656 else if (verbosity < 3)
1663 error = got_pathlist_append(&wanted_refs,
1676 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1677 errx(1, "-a and -b options are mutually exclusive");
1678 if (list_refs_only) {
1679 if (!TAILQ_EMPTY(&wanted_branches))
1680 errx(1, "-l and -b options are mutually exclusive");
1681 if (fetch_all_branches)
1682 errx(1, "-l and -a options are mutually exclusive");
1684 errx(1, "-l and -d options are mutually exclusive");
1685 if (verbosity == -1)
1686 errx(1, "-l and -q options are mutually exclusive");
1690 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1692 remote_name = argv[0];
1696 cwd = getcwd(NULL, 0);
1698 error = got_error_from_errno("getcwd");
1702 if (repo_path == NULL) {
1703 error = got_worktree_open(&worktree, cwd);
1704 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1710 strdup(got_worktree_get_repo_path(worktree));
1711 if (repo_path == NULL)
1712 error = got_error_from_errno("strdup");
1716 repo_path = strdup(cwd);
1717 if (repo_path == NULL) {
1718 error = got_error_from_errno("strdup");
1724 error = got_repo_open(&repo, repo_path, NULL);
1728 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1729 for (i = 0; i < nremotes; i++) {
1730 remote = &remotes[i];
1731 if (strcmp(remote->name, remote_name) == 0)
1734 if (i == nremotes) {
1735 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1739 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1740 &repo_name, remote->url);
1744 if (strcmp(proto, "git") == 0) {
1746 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1747 "sendfd dns inet unveil", NULL) == -1)
1750 } else if (strcmp(proto, "git+ssh") == 0 ||
1751 strcmp(proto, "ssh") == 0) {
1753 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1754 "sendfd unveil", NULL) == -1)
1757 } else if (strcmp(proto, "http") == 0 ||
1758 strcmp(proto, "git+http") == 0) {
1759 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1762 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1766 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1767 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1768 error = got_error_from_errno2("unveil",
1769 GOT_FETCH_PATH_SSH);
1773 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1777 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1778 server_path, verbosity);
1783 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1784 port ? ":" : "", port ? port : "");
1786 fpa.last_scaled_size[0] = '\0';
1787 fpa.last_p_indexed = -1;
1788 fpa.last_p_resolved = -1;
1789 fpa.verbosity = verbosity;
1790 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1791 remote->mirror_references, fetch_all_branches, &wanted_branches,
1792 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1793 fetch_progress, &fpa);
1797 if (list_refs_only) {
1798 error = list_remote_refs(&symrefs, &refs);
1802 if (pack_hash == NULL) {
1804 printf("Already up-to-date\n");
1806 error = delete_missing_refs(&refs, verbosity, repo);
1810 if (verbosity >= 0) {
1811 error = got_object_id_str(&id_str, pack_hash);
1814 printf("\nFetched %s.pack\n", id_str);
1819 /* Update references provided with the pack file. */
1820 TAILQ_FOREACH(pe, &refs, entry) {
1821 const char *refname = pe->path;
1822 struct got_object_id *id = pe->data;
1823 struct got_reference *ref;
1824 char *remote_refname;
1826 if (is_wanted_ref(&wanted_refs, refname) &&
1827 !remote->mirror_references) {
1828 error = update_wanted_ref(refname, id,
1829 remote->name, verbosity, repo);
1835 if (remote->mirror_references ||
1836 strncmp("refs/tags/", refname, 10) == 0) {
1837 error = got_ref_open(&ref, repo, refname, 1);
1839 if (error->code != GOT_ERR_NOT_REF)
1841 error = create_ref(refname, id, verbosity,
1846 error = update_ref(ref, id, replace_tags,
1848 unlock_err = got_ref_unlock(ref);
1849 if (unlock_err && error == NULL)
1855 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1856 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1857 remote_name, refname + 11) == -1) {
1858 error = got_error_from_errno("asprintf");
1862 error = got_ref_open(&ref, repo, remote_refname, 1);
1864 if (error->code != GOT_ERR_NOT_REF)
1866 error = create_ref(remote_refname, id,
1871 error = update_ref(ref, id, replace_tags,
1873 unlock_err = got_ref_unlock(ref);
1874 if (unlock_err && error == NULL)
1881 /* Also create a local branch if none exists yet. */
1882 error = got_ref_open(&ref, repo, refname, 1);
1884 if (error->code != GOT_ERR_NOT_REF)
1886 error = create_ref(refname, id, verbosity,
1891 unlock_err = got_ref_unlock(ref);
1892 if (unlock_err && error == NULL)
1899 error = delete_missing_refs(&refs, verbosity, repo);
1902 if (kill(fetchpid, SIGTERM) == -1)
1903 error = got_error_from_errno("kill");
1904 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1905 error = got_error_from_errno("waitpid");
1907 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1908 error = got_error_from_errno("close");
1910 got_repo_close(repo);
1912 got_worktree_close(worktree);
1913 TAILQ_FOREACH(pe, &refs, entry) {
1914 free((void *)pe->path);
1917 got_pathlist_free(&refs);
1918 TAILQ_FOREACH(pe, &symrefs, entry) {
1919 free((void *)pe->path);
1922 got_pathlist_free(&symrefs);
1923 got_pathlist_free(&wanted_branches);
1924 got_pathlist_free(&wanted_refs);
1939 usage_checkout(void)
1941 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1942 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1947 show_worktree_base_ref_warning(void)
1949 fprintf(stderr, "%s: warning: could not create a reference "
1950 "to the work tree's base commit; the commit could be "
1951 "garbage-collected by Git; making the repository "
1952 "writable and running 'got update' will prevent this\n",
1956 struct got_checkout_progress_arg {
1957 const char *worktree_path;
1958 int had_base_commit_ref_error;
1961 static const struct got_error *
1962 checkout_progress(void *arg, unsigned char status, const char *path)
1964 struct got_checkout_progress_arg *a = arg;
1966 /* Base commit bump happens silently. */
1967 if (status == GOT_STATUS_BUMP_BASE)
1970 if (status == GOT_STATUS_BASE_REF_ERR) {
1971 a->had_base_commit_ref_error = 1;
1975 while (path[0] == '/')
1978 printf("%c %s/%s\n", status, a->worktree_path, path);
1982 static const struct got_error *
1983 check_cancelled(void *arg)
1985 if (sigint_received || sigpipe_received)
1986 return got_error(GOT_ERR_CANCELLED);
1990 static const struct got_error *
1991 check_linear_ancestry(struct got_object_id *commit_id,
1992 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1993 struct got_repository *repo)
1995 const struct got_error *err = NULL;
1996 struct got_object_id *yca_id;
1998 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1999 commit_id, base_commit_id, repo, check_cancelled, NULL);
2004 return got_error(GOT_ERR_ANCESTRY);
2007 * Require a straight line of history between the target commit
2008 * and the work tree's base commit.
2010 * Non-linear situations such as this require a rebase:
2012 * (commit) D F (base_commit)
2020 * 'got update' only handles linear cases:
2021 * Update forwards in time: A (base/yca) - B - C - D (commit)
2022 * Update backwards in time: D (base) - C - B - A (commit/yca)
2024 if (allow_forwards_in_time_only) {
2025 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2026 return got_error(GOT_ERR_ANCESTRY);
2027 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2028 got_object_id_cmp(base_commit_id, yca_id) != 0)
2029 return got_error(GOT_ERR_ANCESTRY);
2035 static const struct got_error *
2036 check_same_branch(struct got_object_id *commit_id,
2037 struct got_reference *head_ref, struct got_object_id *yca_id,
2038 struct got_repository *repo)
2040 const struct got_error *err = NULL;
2041 struct got_commit_graph *graph = NULL;
2042 struct got_object_id *head_commit_id = NULL;
2043 int is_same_branch = 0;
2045 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2049 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2053 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2058 err = got_commit_graph_open(&graph, "/", 1);
2062 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2063 check_cancelled, NULL);
2068 struct got_object_id *id;
2069 err = got_commit_graph_iter_next(&id, graph, repo,
2070 check_cancelled, NULL);
2072 if (err->code == GOT_ERR_ITER_COMPLETED)
2078 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2080 if (got_object_id_cmp(id, commit_id) == 0) {
2088 got_commit_graph_close(graph);
2089 free(head_commit_id);
2090 if (!err && !is_same_branch)
2091 err = got_error(GOT_ERR_ANCESTRY);
2095 static const struct got_error *
2096 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2098 static char msg[512];
2099 const char *branch_name;
2101 if (got_ref_is_symbolic(ref))
2102 branch_name = got_ref_get_symref_target(ref);
2104 branch_name = got_ref_get_name(ref);
2106 if (strncmp("refs/heads/", branch_name, 11) == 0)
2109 snprintf(msg, sizeof(msg),
2110 "target commit is not contained in branch '%s'; "
2111 "the branch to use must be specified with -b; "
2112 "if necessary a new branch can be created for "
2113 "this commit with 'got branch -c %s BRANCH_NAME'",
2114 branch_name, commit_id_str);
2116 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2119 static const struct got_error *
2120 cmd_checkout(int argc, char *argv[])
2122 const struct got_error *error = NULL;
2123 struct got_repository *repo = NULL;
2124 struct got_reference *head_ref = NULL;
2125 struct got_worktree *worktree = NULL;
2126 char *repo_path = NULL;
2127 char *worktree_path = NULL;
2128 const char *path_prefix = "";
2129 const char *branch_name = GOT_REF_HEAD;
2130 char *commit_id_str = NULL;
2131 int ch, same_path_prefix, allow_nonempty = 0;
2132 struct got_pathlist_head paths;
2133 struct got_checkout_progress_arg cpa;
2137 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2140 branch_name = optarg;
2143 commit_id_str = strdup(optarg);
2144 if (commit_id_str == NULL)
2145 return got_error_from_errno("strdup");
2151 path_prefix = optarg;
2163 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2164 "unveil", NULL) == -1)
2168 char *cwd, *base, *dotgit;
2169 repo_path = realpath(argv[0], NULL);
2170 if (repo_path == NULL)
2171 return got_error_from_errno2("realpath", argv[0]);
2172 cwd = getcwd(NULL, 0);
2174 error = got_error_from_errno("getcwd");
2177 if (path_prefix[0]) {
2178 base = basename(path_prefix);
2180 error = got_error_from_errno2("basename",
2185 base = basename(repo_path);
2187 error = got_error_from_errno2("basename",
2192 dotgit = strstr(base, ".git");
2195 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2196 error = got_error_from_errno("asprintf");
2201 } else if (argc == 2) {
2202 repo_path = realpath(argv[0], NULL);
2203 if (repo_path == NULL) {
2204 error = got_error_from_errno2("realpath", argv[0]);
2207 worktree_path = realpath(argv[1], NULL);
2208 if (worktree_path == NULL) {
2209 if (errno != ENOENT) {
2210 error = got_error_from_errno2("realpath",
2214 worktree_path = strdup(argv[1]);
2215 if (worktree_path == NULL) {
2216 error = got_error_from_errno("strdup");
2223 got_path_strip_trailing_slashes(repo_path);
2224 got_path_strip_trailing_slashes(worktree_path);
2226 error = got_repo_open(&repo, repo_path, NULL);
2230 /* Pre-create work tree path for unveil(2) */
2231 error = got_path_mkdir(worktree_path);
2233 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2234 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2236 if (!allow_nonempty &&
2237 !got_path_dir_is_empty(worktree_path)) {
2238 error = got_error_path(worktree_path,
2239 GOT_ERR_DIR_NOT_EMPTY);
2244 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2248 error = got_ref_open(&head_ref, repo, branch_name, 0);
2252 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2253 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2256 error = got_worktree_open(&worktree, worktree_path);
2260 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2264 if (!same_path_prefix) {
2265 error = got_error(GOT_ERR_PATH_PREFIX);
2269 if (commit_id_str) {
2270 struct got_object_id *commit_id;
2271 error = got_repo_match_object_id(&commit_id, NULL,
2272 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2275 error = check_linear_ancestry(commit_id,
2276 got_worktree_get_base_commit_id(worktree), 0, repo);
2277 if (error != NULL) {
2279 if (error->code == GOT_ERR_ANCESTRY) {
2280 error = checkout_ancestry_error(
2281 head_ref, commit_id_str);
2285 error = check_same_branch(commit_id, head_ref, NULL, repo);
2287 if (error->code == GOT_ERR_ANCESTRY) {
2288 error = checkout_ancestry_error(
2289 head_ref, commit_id_str);
2293 error = got_worktree_set_base_commit_id(worktree, repo,
2300 error = got_pathlist_append(&paths, "", NULL);
2303 cpa.worktree_path = worktree_path;
2304 cpa.had_base_commit_ref_error = 0;
2305 error = got_worktree_checkout_files(worktree, &paths, repo,
2306 checkout_progress, &cpa, check_cancelled, NULL);
2310 printf("Now shut up and hack\n");
2311 if (cpa.had_base_commit_ref_error)
2312 show_worktree_base_ref_warning();
2314 got_pathlist_free(&paths);
2315 free(commit_id_str);
2317 free(worktree_path);
2324 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2329 static const struct got_error *
2330 update_progress(void *arg, unsigned char status, const char *path)
2332 int *did_something = arg;
2334 if (status == GOT_STATUS_EXISTS ||
2335 status == GOT_STATUS_BASE_REF_ERR)
2340 /* Base commit bump happens silently. */
2341 if (status == GOT_STATUS_BUMP_BASE)
2344 while (path[0] == '/')
2346 printf("%c %s\n", status, path);
2350 static const struct got_error *
2351 switch_head_ref(struct got_reference *head_ref,
2352 struct got_object_id *commit_id, struct got_worktree *worktree,
2353 struct got_repository *repo)
2355 const struct got_error *err = NULL;
2357 int ref_has_moved = 0;
2359 /* Trivial case: switching between two different references. */
2360 if (strcmp(got_ref_get_name(head_ref),
2361 got_worktree_get_head_ref_name(worktree)) != 0) {
2362 printf("Switching work tree from %s to %s\n",
2363 got_worktree_get_head_ref_name(worktree),
2364 got_ref_get_name(head_ref));
2365 return got_worktree_set_head_ref(worktree, head_ref);
2368 err = check_linear_ancestry(commit_id,
2369 got_worktree_get_base_commit_id(worktree), 0, repo);
2371 if (err->code != GOT_ERR_ANCESTRY)
2378 /* Switching to a rebased branch with the same reference name. */
2379 err = got_object_id_str(&base_id_str,
2380 got_worktree_get_base_commit_id(worktree));
2383 printf("Reference %s now points at a different branch\n",
2384 got_worktree_get_head_ref_name(worktree));
2385 printf("Switching work tree from %s to %s\n", base_id_str,
2386 got_worktree_get_head_ref_name(worktree));
2390 static const struct got_error *
2391 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2393 const struct got_error *err;
2396 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2400 return got_error(GOT_ERR_REBASING);
2402 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2406 return got_error(GOT_ERR_HISTEDIT_BUSY);
2411 static const struct got_error *
2412 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2413 char *argv[], struct got_worktree *worktree)
2415 const struct got_error *err = NULL;
2422 return got_error_from_errno("strdup");
2423 return got_pathlist_append(paths, path, NULL);
2426 for (i = 0; i < argc; i++) {
2427 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2430 err = got_pathlist_append(paths, path, NULL);
2440 static const struct got_error *
2441 cmd_update(int argc, char *argv[])
2443 const struct got_error *error = NULL;
2444 struct got_repository *repo = NULL;
2445 struct got_worktree *worktree = NULL;
2446 char *worktree_path = NULL;
2447 struct got_object_id *commit_id = NULL;
2448 char *commit_id_str = NULL;
2449 const char *branch_name = NULL;
2450 struct got_reference *head_ref = NULL;
2451 struct got_pathlist_head paths;
2452 struct got_pathlist_entry *pe;
2453 int ch, did_something = 0;
2457 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2460 branch_name = optarg;
2463 commit_id_str = strdup(optarg);
2464 if (commit_id_str == NULL)
2465 return got_error_from_errno("strdup");
2477 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2478 "unveil", NULL) == -1)
2481 worktree_path = getcwd(NULL, 0);
2482 if (worktree_path == NULL) {
2483 error = got_error_from_errno("getcwd");
2486 error = got_worktree_open(&worktree, worktree_path);
2490 error = check_rebase_or_histedit_in_progress(worktree);
2494 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2499 error = apply_unveil(got_repo_get_path(repo), 0,
2500 got_worktree_get_root_path(worktree));
2504 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2508 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2509 got_worktree_get_head_ref_name(worktree), 0);
2512 if (commit_id_str == NULL) {
2513 error = got_ref_resolve(&commit_id, repo, head_ref);
2516 error = got_object_id_str(&commit_id_str, commit_id);
2520 error = got_repo_match_object_id(&commit_id, NULL,
2521 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2522 free(commit_id_str);
2523 commit_id_str = NULL;
2526 error = got_object_id_str(&commit_id_str, commit_id);
2532 struct got_object_id *head_commit_id;
2533 TAILQ_FOREACH(pe, &paths, entry) {
2534 if (pe->path_len == 0)
2536 error = got_error_msg(GOT_ERR_BAD_PATH,
2537 "switching between branches requires that "
2538 "the entire work tree gets updated");
2541 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2544 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2546 free(head_commit_id);
2549 error = check_same_branch(commit_id, head_ref, NULL, repo);
2552 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2556 error = check_linear_ancestry(commit_id,
2557 got_worktree_get_base_commit_id(worktree), 0, repo);
2558 if (error != NULL) {
2559 if (error->code == GOT_ERR_ANCESTRY)
2560 error = got_error(GOT_ERR_BRANCH_MOVED);
2563 error = check_same_branch(commit_id, head_ref, NULL, repo);
2568 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2570 error = got_worktree_set_base_commit_id(worktree, repo,
2576 error = got_worktree_checkout_files(worktree, &paths, repo,
2577 update_progress, &did_something, check_cancelled, NULL);
2582 printf("Updated to commit %s\n", commit_id_str);
2584 printf("Already up-to-date\n");
2586 free(worktree_path);
2587 TAILQ_FOREACH(pe, &paths, entry)
2588 free((char *)pe->path);
2589 got_pathlist_free(&paths);
2591 free(commit_id_str);
2595 static const struct got_error *
2596 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2597 const char *path, int diff_context, int ignore_whitespace,
2598 struct got_repository *repo)
2600 const struct got_error *err = NULL;
2601 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2604 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2609 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2613 while (path[0] == '/')
2615 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2616 ignore_whitespace, stdout);
2619 got_object_blob_close(blob1);
2620 got_object_blob_close(blob2);
2624 static const struct got_error *
2625 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2626 const char *path, int diff_context, int ignore_whitespace,
2627 struct got_repository *repo)
2629 const struct got_error *err = NULL;
2630 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2631 struct got_diff_blob_output_unidiff_arg arg;
2634 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2639 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2643 arg.diff_context = diff_context;
2644 arg.ignore_whitespace = ignore_whitespace;
2645 arg.outfile = stdout;
2646 while (path[0] == '/')
2648 err = got_diff_tree(tree1, tree2, path, path, repo,
2649 got_diff_blob_output_unidiff, &arg, 1);
2652 got_object_tree_close(tree1);
2654 got_object_tree_close(tree2);
2658 static const struct got_error *
2659 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2660 const char *path, int diff_context, struct got_repository *repo)
2662 const struct got_error *err = NULL;
2663 struct got_commit_object *pcommit = NULL;
2664 char *id_str1 = NULL, *id_str2 = NULL;
2665 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2666 struct got_object_qid *qid;
2668 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2670 err = got_object_open_as_commit(&pcommit, repo,
2676 if (path && path[0] != '\0') {
2678 err = got_object_id_by_path(&obj_id2, repo, id, path);
2681 err = got_object_id_str(&id_str2, obj_id2);
2687 err = got_object_id_by_path(&obj_id1, repo,
2693 err = got_object_id_str(&id_str1, obj_id1);
2699 err = got_object_get_type(&obj_type, repo, obj_id2);
2704 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2706 case GOT_OBJ_TYPE_BLOB:
2707 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2710 case GOT_OBJ_TYPE_TREE:
2711 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2715 err = got_error(GOT_ERR_OBJ_TYPE);
2721 obj_id2 = got_object_commit_get_tree_id(commit);
2722 err = got_object_id_str(&id_str2, obj_id2);
2725 obj_id1 = got_object_commit_get_tree_id(pcommit);
2726 err = got_object_id_str(&id_str1, obj_id1);
2729 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2730 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2736 got_object_commit_close(pcommit);
2741 get_datestr(time_t *time, char *datebuf)
2743 struct tm mytm, *tm;
2746 tm = gmtime_r(time, &mytm);
2749 s = asctime_r(tm, datebuf);
2752 p = strchr(s, '\n');
2758 static const struct got_error *
2759 match_logmsg(int *have_match, struct got_object_id *id,
2760 struct got_commit_object *commit, regex_t *regex)
2762 const struct got_error *err = NULL;
2763 regmatch_t regmatch;
2764 char *id_str = NULL, *logmsg = NULL;
2768 err = got_object_id_str(&id_str, id);
2772 err = got_object_commit_get_logmsg(&logmsg, commit);
2776 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2784 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2786 static const struct got_error *
2787 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2788 struct got_repository *repo, const char *path, int show_patch,
2789 int diff_context, struct got_reflist_head *refs)
2791 const struct got_error *err = NULL;
2792 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2794 time_t committer_time;
2795 const char *author, *committer;
2796 char *refs_str = NULL;
2797 struct got_reflist_entry *re;
2799 SIMPLEQ_FOREACH(re, refs, entry) {
2802 struct got_tag_object *tag = NULL;
2805 name = got_ref_get_name(re->ref);
2806 if (strcmp(name, GOT_REF_HEAD) == 0)
2808 if (strncmp(name, "refs/", 5) == 0)
2810 if (strncmp(name, "got/", 4) == 0)
2812 if (strncmp(name, "heads/", 6) == 0)
2814 if (strncmp(name, "remotes/", 8) == 0)
2816 if (strncmp(name, "tags/", 5) == 0) {
2817 err = got_object_open_as_tag(&tag, repo, re->id);
2819 if (err->code != GOT_ERR_OBJ_TYPE)
2821 /* Ref points at something other than a tag. */
2826 cmp = got_object_id_cmp(tag ?
2827 got_object_tag_get_object_id(tag) : re->id, id);
2829 got_object_tag_close(tag);
2833 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2835 err = got_error_from_errno("asprintf");
2841 err = got_object_id_str(&id_str, id);
2845 printf(GOT_COMMIT_SEP_STR);
2846 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2847 refs_str ? refs_str : "", refs_str ? ")" : "");
2852 printf("from: %s\n", got_object_commit_get_author(commit));
2853 committer_time = got_object_commit_get_committer_time(commit);
2854 datestr = get_datestr(&committer_time, datebuf);
2856 printf("date: %s UTC\n", datestr);
2857 author = got_object_commit_get_author(commit);
2858 committer = got_object_commit_get_committer(commit);
2859 if (strcmp(author, committer) != 0)
2860 printf("via: %s\n", committer);
2861 if (got_object_commit_get_nparents(commit) > 1) {
2862 const struct got_object_id_queue *parent_ids;
2863 struct got_object_qid *qid;
2865 parent_ids = got_object_commit_get_parent_ids(commit);
2866 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2867 err = got_object_id_str(&id_str, qid->id);
2870 printf("parent %d: %s\n", n++, id_str);
2875 err = got_object_commit_get_logmsg(&logmsg0, commit);
2881 line = strsep(&logmsg, "\n");
2883 printf(" %s\n", line);
2888 err = print_patch(commit, id, path, diff_context, repo);
2893 if (fflush(stdout) != 0 && err == NULL)
2894 err = got_error_from_errno("fflush");
2898 static const struct got_error *
2899 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2900 const char *path, int show_patch, const char *search_pattern,
2901 int diff_context, int limit, int log_branches,
2902 struct got_reflist_head *refs)
2904 const struct got_error *err;
2905 struct got_commit_graph *graph;
2909 if (search_pattern &&
2910 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2911 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2913 err = got_commit_graph_open(&graph, path, !log_branches);
2916 err = got_commit_graph_iter_start(graph, root_id, repo,
2917 check_cancelled, NULL);
2921 struct got_commit_object *commit;
2922 struct got_object_id *id;
2924 if (sigint_received || sigpipe_received)
2927 err = got_commit_graph_iter_next(&id, graph, repo,
2928 check_cancelled, NULL);
2930 if (err->code == GOT_ERR_ITER_COMPLETED)
2937 err = got_object_open_as_commit(&commit, repo, id);
2941 if (search_pattern) {
2942 err = match_logmsg(&have_match, id, commit, ®ex);
2944 got_object_commit_close(commit);
2947 if (have_match == 0) {
2948 got_object_commit_close(commit);
2953 err = print_commit(commit, id, repo, path, show_patch,
2954 diff_context, refs);
2955 got_object_commit_close(commit);
2956 if (err || (limit && --limit == 0))
2962 got_commit_graph_close(graph);
2969 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2970 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2975 get_default_log_limit(void)
2977 const char *got_default_log_limit;
2981 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2982 if (got_default_log_limit == NULL)
2984 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2990 static const struct got_error *
2991 cmd_log(int argc, char *argv[])
2993 const struct got_error *error;
2994 struct got_repository *repo = NULL;
2995 struct got_worktree *worktree = NULL;
2996 struct got_commit_object *commit = NULL;
2997 struct got_object_id *id = NULL;
2998 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2999 const char *start_commit = NULL, *search_pattern = NULL;
3000 int diff_context = -1, ch;
3001 int show_patch = 0, limit = 0, log_branches = 0;
3003 struct got_reflist_head refs;
3005 SIMPLEQ_INIT(&refs);
3008 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3014 limit = get_default_log_limit();
3016 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3022 start_commit = optarg;
3025 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3028 err(1, "-C option %s", errstr);
3031 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3033 err(1, "-l option %s", errstr);
3039 repo_path = realpath(optarg, NULL);
3040 if (repo_path == NULL)
3041 return got_error_from_errno2("realpath",
3043 got_path_strip_trailing_slashes(repo_path);
3046 search_pattern = optarg;
3057 if (diff_context == -1)
3059 else if (!show_patch)
3060 errx(1, "-C reguires -p");
3062 cwd = getcwd(NULL, 0);
3064 error = got_error_from_errno("getcwd");
3068 error = got_worktree_open(&worktree, cwd);
3069 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3076 error = got_error_from_errno("strdup");
3079 } else if (argc == 1) {
3081 error = got_worktree_resolve_path(&path, worktree,
3086 path = strdup(argv[0]);
3088 error = got_error_from_errno("strdup");
3095 if (repo_path == NULL) {
3096 repo_path = worktree ?
3097 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3099 if (repo_path == NULL) {
3100 error = got_error_from_errno("strdup");
3104 error = got_repo_open(&repo, repo_path, NULL);
3108 error = apply_unveil(got_repo_get_path(repo), 1,
3109 worktree ? got_worktree_get_root_path(worktree) : NULL);
3113 if (start_commit == NULL) {
3114 struct got_reference *head_ref;
3115 error = got_ref_open(&head_ref, repo,
3116 worktree ? got_worktree_get_head_ref_name(worktree)
3120 error = got_ref_resolve(&id, repo, head_ref);
3121 got_ref_close(head_ref);
3124 error = got_object_open_as_commit(&commit, repo, id);
3126 struct got_reference *ref;
3127 error = got_ref_open(&ref, repo, start_commit, 0);
3128 if (error == NULL) {
3130 error = got_ref_resolve(&id, repo, ref);
3134 error = got_object_get_type(&obj_type, repo, id);
3137 if (obj_type == GOT_OBJ_TYPE_TAG) {
3138 struct got_tag_object *tag;
3139 error = got_object_open_as_tag(&tag, repo, id);
3142 if (got_object_tag_get_object_type(tag) !=
3143 GOT_OBJ_TYPE_COMMIT) {
3144 got_object_tag_close(tag);
3145 error = got_error(GOT_ERR_OBJ_TYPE);
3149 id = got_object_id_dup(
3150 got_object_tag_get_object_id(tag));
3152 error = got_error_from_errno(
3153 "got_object_id_dup");
3154 got_object_tag_close(tag);
3157 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3158 error = got_error(GOT_ERR_OBJ_TYPE);
3161 error = got_object_open_as_commit(&commit, repo, id);
3165 if (commit == NULL) {
3166 error = got_repo_match_object_id_prefix(&id,
3167 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3176 const char *prefix = got_worktree_get_path_prefix(worktree);
3178 if (asprintf(&p, "%s%s%s", prefix,
3179 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3180 error = got_error_from_errno("asprintf");
3183 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3186 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3191 path = in_repo_path;
3194 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3198 error = print_commits(id, repo, path, show_patch, search_pattern,
3199 diff_context, limit, log_branches, &refs);
3206 got_worktree_close(worktree);
3208 const struct got_error *repo_error;
3209 repo_error = got_repo_close(repo);
3213 got_ref_list_free(&refs);
3220 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3221 "[-w] [object1 object2 | path]\n", getprogname());
3225 struct print_diff_arg {
3226 struct got_repository *repo;
3227 struct got_worktree *worktree;
3232 int ignore_whitespace;
3235 static const struct got_error *
3236 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3237 const char *path, struct got_object_id *blob_id,
3238 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3239 int dirfd, const char *de_name)
3241 struct print_diff_arg *a = arg;
3242 const struct got_error *err = NULL;
3243 struct got_blob_object *blob1 = NULL;
3246 char *abspath = NULL, *label1 = NULL;
3249 if (a->diff_staged) {
3250 if (staged_status != GOT_STATUS_MODIFY &&
3251 staged_status != GOT_STATUS_ADD &&
3252 staged_status != GOT_STATUS_DELETE)
3255 if (staged_status == GOT_STATUS_DELETE)
3257 if (status == GOT_STATUS_NONEXISTENT)
3258 return got_error_set_errno(ENOENT, path);
3259 if (status != GOT_STATUS_MODIFY &&
3260 status != GOT_STATUS_ADD &&
3261 status != GOT_STATUS_DELETE &&
3262 status != GOT_STATUS_CONFLICT)
3266 if (!a->header_shown) {
3267 printf("diff %s %s%s\n", a->id_str,
3268 got_worktree_get_root_path(a->worktree),
3269 a->diff_staged ? " (staged changes)" : "");
3270 a->header_shown = 1;
3273 if (a->diff_staged) {
3274 const char *label1 = NULL, *label2 = NULL;
3275 switch (staged_status) {
3276 case GOT_STATUS_MODIFY:
3280 case GOT_STATUS_ADD:
3283 case GOT_STATUS_DELETE:
3287 return got_error(GOT_ERR_FILE_STATUS);
3289 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3290 label1, label2, a->diff_context, a->ignore_whitespace,
3294 if (staged_status == GOT_STATUS_ADD ||
3295 staged_status == GOT_STATUS_MODIFY) {
3297 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3301 err = got_object_id_str(&id_str, staged_blob_id);
3304 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3305 err = got_error_from_errno("asprintf");
3310 } else if (status != GOT_STATUS_ADD) {
3311 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3316 if (status != GOT_STATUS_DELETE) {
3317 if (asprintf(&abspath, "%s/%s",
3318 got_worktree_get_root_path(a->worktree), path) == -1) {
3319 err = got_error_from_errno("asprintf");
3324 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3326 err = got_error_from_errno2("openat", abspath);
3330 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3332 err = got_error_from_errno2("open", abspath);
3336 if (fstat(fd, &sb) == -1) {
3337 err = got_error_from_errno2("fstat", abspath);
3340 f2 = fdopen(fd, "r");
3342 err = got_error_from_errno2("fdopen", abspath);
3349 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3350 a->diff_context, a->ignore_whitespace, stdout);
3353 got_object_blob_close(blob1);
3354 if (f2 && fclose(f2) == EOF && err == NULL)
3355 err = got_error_from_errno("fclose");
3356 if (fd != -1 && close(fd) == -1 && err == NULL)
3357 err = got_error_from_errno("close");
3362 static const struct got_error *
3363 cmd_diff(int argc, char *argv[])
3365 const struct got_error *error;
3366 struct got_repository *repo = NULL;
3367 struct got_worktree *worktree = NULL;
3368 char *cwd = NULL, *repo_path = NULL;
3369 struct got_object_id *id1 = NULL, *id2 = NULL;
3370 const char *id_str1 = NULL, *id_str2 = NULL;
3371 char *label1 = NULL, *label2 = NULL;
3373 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3378 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3383 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3386 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3389 err(1, "-C option %s", errstr);
3392 repo_path = realpath(optarg, NULL);
3393 if (repo_path == NULL)
3394 return got_error_from_errno2("realpath",
3396 got_path_strip_trailing_slashes(repo_path);
3402 ignore_whitespace = 1;
3413 cwd = getcwd(NULL, 0);
3415 error = got_error_from_errno("getcwd");
3421 "-r option can't be used when diffing a work tree");
3422 error = got_worktree_open(&worktree, cwd);
3425 repo_path = strdup(got_worktree_get_repo_path(worktree));
3426 if (repo_path == NULL) {
3427 error = got_error_from_errno("strdup");
3431 error = got_worktree_resolve_path(&path, worktree,
3438 error = got_error_from_errno("strdup");
3442 } else if (argc == 2) {
3444 errx(1, "-s option can't be used when diffing "
3445 "objects in repository");
3448 if (repo_path == NULL) {
3449 error = got_worktree_open(&worktree, cwd);
3450 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3454 got_worktree_get_repo_path(worktree));
3455 if (repo_path == NULL) {
3456 error = got_error_from_errno("strdup");
3460 repo_path = strdup(cwd);
3461 if (repo_path == NULL) {
3462 error = got_error_from_errno("strdup");
3470 error = got_repo_open(&repo, repo_path, NULL);
3475 error = apply_unveil(got_repo_get_path(repo), 1,
3476 worktree ? got_worktree_get_root_path(worktree) : NULL);
3481 struct print_diff_arg arg;
3482 struct got_pathlist_head paths;
3487 error = got_object_id_str(&id_str,
3488 got_worktree_get_base_commit_id(worktree));
3492 arg.worktree = worktree;
3493 arg.diff_context = diff_context;
3494 arg.id_str = id_str;
3495 arg.header_shown = 0;
3496 arg.diff_staged = diff_staged;
3497 arg.ignore_whitespace = ignore_whitespace;
3499 error = got_pathlist_append(&paths, path, NULL);
3503 error = got_worktree_status(worktree, &paths, repo, print_diff,
3504 &arg, check_cancelled, NULL);
3506 got_pathlist_free(&paths);
3510 error = got_repo_match_object_id(&id1, &label1, id_str1,
3511 GOT_OBJ_TYPE_ANY, 1, repo);
3515 error = got_repo_match_object_id(&id2, &label2, id_str2,
3516 GOT_OBJ_TYPE_ANY, 1, repo);
3520 error = got_object_get_type(&type1, repo, id1);
3524 error = got_object_get_type(&type2, repo, id2);
3528 if (type1 != type2) {
3529 error = got_error(GOT_ERR_OBJ_TYPE);
3534 case GOT_OBJ_TYPE_BLOB:
3535 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3536 diff_context, ignore_whitespace, repo, stdout);
3538 case GOT_OBJ_TYPE_TREE:
3539 error = got_diff_objects_as_trees(id1, id2, "", "",
3540 diff_context, ignore_whitespace, repo, stdout);
3542 case GOT_OBJ_TYPE_COMMIT:
3543 printf("diff %s %s\n", label1, label2);
3544 error = got_diff_objects_as_commits(id1, id2, diff_context,
3545 ignore_whitespace, repo, stdout);
3548 error = got_error(GOT_ERR_OBJ_TYPE);
3557 got_worktree_close(worktree);
3559 const struct got_error *repo_error;
3560 repo_error = got_repo_close(repo);
3571 "usage: %s blame [-c commit] [-r repository-path] path\n",
3580 char datebuf[11]; /* YYYY-MM-DD + NUL */
3583 struct blame_cb_args {
3584 struct blame_line *lines;
3588 off_t *line_offsets;
3590 struct got_repository *repo;
3593 static const struct got_error *
3594 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3596 const struct got_error *err = NULL;
3597 struct blame_cb_args *a = arg;
3598 struct blame_line *bline;
3600 size_t linesize = 0;
3601 struct got_commit_object *commit = NULL;
3604 time_t committer_time;
3606 if (nlines != a->nlines ||
3607 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3608 return got_error(GOT_ERR_RANGE);
3610 if (sigint_received)
3611 return got_error(GOT_ERR_ITER_COMPLETED);
3614 return NULL; /* no change in this commit */
3616 /* Annotate this line. */
3617 bline = &a->lines[lineno - 1];
3618 if (bline->annotated)
3620 err = got_object_id_str(&bline->id_str, id);
3624 err = got_object_open_as_commit(&commit, a->repo, id);
3628 bline->committer = strdup(got_object_commit_get_committer(commit));
3629 if (bline->committer == NULL) {
3630 err = got_error_from_errno("strdup");
3634 committer_time = got_object_commit_get_committer_time(commit);
3635 if (localtime_r(&committer_time, &tm) == NULL)
3636 return got_error_from_errno("localtime_r");
3637 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3638 &tm) >= sizeof(bline->datebuf)) {
3639 err = got_error(GOT_ERR_NO_SPACE);
3642 bline->annotated = 1;
3644 /* Print lines annotated so far. */
3645 bline = &a->lines[a->lineno_cur - 1];
3646 if (!bline->annotated)
3649 offset = a->line_offsets[a->lineno_cur - 1];
3650 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3651 err = got_error_from_errno("fseeko");
3655 while (bline->annotated) {
3656 char *smallerthan, *at, *nl, *committer;
3659 if (getline(&line, &linesize, a->f) == -1) {
3661 err = got_error_from_errno("getline");
3665 committer = bline->committer;
3666 smallerthan = strchr(committer, '<');
3667 if (smallerthan && smallerthan[1] != '\0')
3668 committer = smallerthan + 1;
3669 at = strchr(committer, '@');
3672 len = strlen(committer);
3674 committer[8] = '\0';
3676 nl = strchr(line, '\n');
3679 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3680 bline->id_str, bline->datebuf, committer, line);
3683 bline = &a->lines[a->lineno_cur - 1];
3687 got_object_commit_close(commit);
3692 static const struct got_error *
3693 cmd_blame(int argc, char *argv[])
3695 const struct got_error *error;
3696 struct got_repository *repo = NULL;
3697 struct got_worktree *worktree = NULL;
3698 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3699 struct got_object_id *obj_id = NULL;
3700 struct got_object_id *commit_id = NULL;
3701 struct got_blob_object *blob = NULL;
3702 char *commit_id_str = NULL;
3703 struct blame_cb_args bca;
3704 int ch, obj_type, i;
3707 memset(&bca, 0, sizeof(bca));
3710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3715 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3718 commit_id_str = optarg;
3721 repo_path = realpath(optarg, NULL);
3722 if (repo_path == NULL)
3723 return got_error_from_errno2("realpath",
3725 got_path_strip_trailing_slashes(repo_path);
3741 cwd = getcwd(NULL, 0);
3743 error = got_error_from_errno("getcwd");
3746 if (repo_path == NULL) {
3747 error = got_worktree_open(&worktree, cwd);
3748 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3754 strdup(got_worktree_get_repo_path(worktree));
3755 if (repo_path == NULL) {
3756 error = got_error_from_errno("strdup");
3761 repo_path = strdup(cwd);
3762 if (repo_path == NULL) {
3763 error = got_error_from_errno("strdup");
3769 error = got_repo_open(&repo, repo_path, NULL);
3773 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3778 const char *prefix = got_worktree_get_path_prefix(worktree);
3779 char *p, *worktree_subdir = cwd +
3780 strlen(got_worktree_get_root_path(worktree));
3781 if (asprintf(&p, "%s%s%s%s%s",
3782 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3783 worktree_subdir, worktree_subdir[0] ? "/" : "",
3785 error = got_error_from_errno("asprintf");
3788 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3791 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3796 if (commit_id_str == NULL) {
3797 struct got_reference *head_ref;
3798 error = got_ref_open(&head_ref, repo, worktree ?
3799 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3802 error = got_ref_resolve(&commit_id, repo, head_ref);
3803 got_ref_close(head_ref);
3807 error = got_repo_match_object_id(&commit_id, NULL,
3808 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3813 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3817 error = got_object_get_type(&obj_type, repo, obj_id);
3821 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3822 error = got_error(GOT_ERR_OBJ_TYPE);
3826 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3829 bca.f = got_opentemp();
3830 if (bca.f == NULL) {
3831 error = got_error_from_errno("got_opentemp");
3834 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3835 &bca.line_offsets, bca.f, blob);
3836 if (error || bca.nlines == 0)
3839 /* Don't include \n at EOF in the blame line count. */
3840 if (bca.line_offsets[bca.nlines - 1] == filesize)
3843 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3844 if (bca.lines == NULL) {
3845 error = got_error_from_errno("calloc");
3849 bca.nlines_prec = 0;
3857 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3858 check_cancelled, NULL);
3866 got_object_blob_close(blob);
3868 got_worktree_close(worktree);
3870 const struct got_error *repo_error;
3871 repo_error = got_repo_close(repo);
3876 for (i = 0; i < bca.nlines; i++) {
3877 struct blame_line *bline = &bca.lines[i];
3878 free(bline->id_str);
3879 free(bline->committer);
3883 free(bca.line_offsets);
3884 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3885 error = got_error_from_errno("fclose");
3893 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3899 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3900 const char *root_path)
3902 int is_root_path = (strcmp(path, root_path) == 0);
3903 const char *modestr = "";
3904 mode_t mode = got_tree_entry_get_mode(te);
3906 path += strlen(root_path);
3907 while (path[0] == '/')
3910 if (got_object_tree_entry_is_submodule(te))
3912 else if (S_ISLNK(mode))
3914 else if (S_ISDIR(mode))
3916 else if (mode & S_IXUSR)
3919 printf("%s%s%s%s%s\n", id ? id : "", path,
3920 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3923 static const struct got_error *
3924 print_tree(const char *path, struct got_object_id *commit_id,
3925 int show_ids, int recurse, const char *root_path,
3926 struct got_repository *repo)
3928 const struct got_error *err = NULL;
3929 struct got_object_id *tree_id = NULL;
3930 struct got_tree_object *tree = NULL;
3933 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3937 err = got_object_open_as_tree(&tree, repo, tree_id);
3940 nentries = got_object_tree_get_nentries(tree);
3941 for (i = 0; i < nentries; i++) {
3942 struct got_tree_entry *te;
3945 if (sigint_received || sigpipe_received)
3948 te = got_object_tree_get_entry(tree, i);
3951 err = got_object_id_str(&id_str,
3952 got_tree_entry_get_id(te));
3955 if (asprintf(&id, "%s ", id_str) == -1) {
3956 err = got_error_from_errno("asprintf");
3962 print_entry(te, id, path, root_path);
3965 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3967 if (asprintf(&child_path, "%s%s%s", path,
3968 path[0] == '/' && path[1] == '\0' ? "" : "/",
3969 got_tree_entry_get_name(te)) == -1) {
3970 err = got_error_from_errno("asprintf");
3973 err = print_tree(child_path, commit_id, show_ids, 1,
3982 got_object_tree_close(tree);
3987 static const struct got_error *
3988 cmd_tree(int argc, char *argv[])
3990 const struct got_error *error;
3991 struct got_repository *repo = NULL;
3992 struct got_worktree *worktree = NULL;
3993 const char *path, *refname = NULL;
3994 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3995 struct got_object_id *commit_id = NULL;
3996 char *commit_id_str = NULL;
3997 int show_ids = 0, recurse = 0;
4001 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4006 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4009 commit_id_str = optarg;
4012 repo_path = realpath(optarg, NULL);
4013 if (repo_path == NULL)
4014 return got_error_from_errno2("realpath",
4016 got_path_strip_trailing_slashes(repo_path);
4040 cwd = getcwd(NULL, 0);
4042 error = got_error_from_errno("getcwd");
4045 if (repo_path == NULL) {
4046 error = got_worktree_open(&worktree, cwd);
4047 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4053 strdup(got_worktree_get_repo_path(worktree));
4054 if (repo_path == NULL)
4055 error = got_error_from_errno("strdup");
4059 repo_path = strdup(cwd);
4060 if (repo_path == NULL) {
4061 error = got_error_from_errno("strdup");
4067 error = got_repo_open(&repo, repo_path, NULL);
4071 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4077 char *p, *worktree_subdir = cwd +
4078 strlen(got_worktree_get_root_path(worktree));
4079 if (asprintf(&p, "%s/%s",
4080 got_worktree_get_path_prefix(worktree),
4081 worktree_subdir) == -1) {
4082 error = got_error_from_errno("asprintf");
4085 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4092 if (in_repo_path == NULL) {
4093 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4098 if (commit_id_str == NULL) {
4099 struct got_reference *head_ref;
4101 refname = got_worktree_get_head_ref_name(worktree);
4103 refname = GOT_REF_HEAD;
4104 error = got_ref_open(&head_ref, repo, refname, 0);
4107 error = got_ref_resolve(&commit_id, repo, head_ref);
4108 got_ref_close(head_ref);
4112 error = got_repo_match_object_id(&commit_id, NULL,
4113 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4118 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4119 in_repo_path, repo);
4126 got_worktree_close(worktree);
4128 const struct got_error *repo_error;
4129 repo_error = got_repo_close(repo);
4139 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4143 static const struct got_error *
4144 print_status(void *arg, unsigned char status, unsigned char staged_status,
4145 const char *path, struct got_object_id *blob_id,
4146 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4147 int dirfd, const char *de_name)
4149 if (status == staged_status && (status == GOT_STATUS_DELETE))
4150 status = GOT_STATUS_NO_CHANGE;
4151 printf("%c%c %s\n", status, staged_status, path);
4155 static const struct got_error *
4156 cmd_status(int argc, char *argv[])
4158 const struct got_error *error = NULL;
4159 struct got_repository *repo = NULL;
4160 struct got_worktree *worktree = NULL;
4162 struct got_pathlist_head paths;
4163 struct got_pathlist_entry *pe;
4168 while ((ch = getopt(argc, argv, "")) != -1) {
4180 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4184 cwd = getcwd(NULL, 0);
4186 error = got_error_from_errno("getcwd");
4190 error = got_worktree_open(&worktree, cwd);
4194 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4199 error = apply_unveil(got_repo_get_path(repo), 1,
4200 got_worktree_get_root_path(worktree));
4204 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4208 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4209 check_cancelled, NULL);
4211 TAILQ_FOREACH(pe, &paths, entry)
4212 free((char *)pe->path);
4213 got_pathlist_free(&paths);
4222 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4228 static const struct got_error *
4229 list_refs(struct got_repository *repo, const char *refname)
4231 static const struct got_error *err = NULL;
4232 struct got_reflist_head refs;
4233 struct got_reflist_entry *re;
4235 SIMPLEQ_INIT(&refs);
4236 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4240 SIMPLEQ_FOREACH(re, &refs, entry) {
4242 refstr = got_ref_to_str(re->ref);
4244 return got_error_from_errno("got_ref_to_str");
4245 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4249 got_ref_list_free(&refs);
4253 static const struct got_error *
4254 delete_ref(struct got_repository *repo, const char *refname)
4256 const struct got_error *err = NULL;
4257 struct got_reference *ref;
4259 err = got_ref_open(&ref, repo, refname, 0);
4263 err = got_ref_delete(ref, repo);
4268 static const struct got_error *
4269 add_ref(struct got_repository *repo, const char *refname, const char *target)
4271 const struct got_error *err = NULL;
4272 struct got_object_id *id;
4273 struct got_reference *ref = NULL;
4276 * Don't let the user create a reference name with a leading '-'.
4277 * While technically a valid reference name, this case is usually
4278 * an unintended typo.
4280 if (refname[0] == '-')
4281 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4283 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4286 struct got_reference *target_ref;
4288 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4290 err = got_ref_open(&target_ref, repo, target, 0);
4293 err = got_ref_resolve(&id, repo, target_ref);
4294 got_ref_close(target_ref);
4299 err = got_ref_alloc(&ref, refname, id);
4303 err = got_ref_write(ref, repo);
4311 static const struct got_error *
4312 add_symref(struct got_repository *repo, const char *refname, const char *target)
4314 const struct got_error *err = NULL;
4315 struct got_reference *ref = NULL;
4316 struct got_reference *target_ref = NULL;
4319 * Don't let the user create a reference name with a leading '-'.
4320 * While technically a valid reference name, this case is usually
4321 * an unintended typo.
4323 if (refname[0] == '-')
4324 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4326 err = got_ref_open(&target_ref, repo, target, 0);
4330 err = got_ref_alloc_symref(&ref, refname, target_ref);
4334 err = got_ref_write(ref, repo);
4337 got_ref_close(target_ref);
4343 static const struct got_error *
4344 cmd_ref(int argc, char *argv[])
4346 const struct got_error *error = NULL;
4347 struct got_repository *repo = NULL;
4348 struct got_worktree *worktree = NULL;
4349 char *cwd = NULL, *repo_path = NULL;
4350 int ch, do_list = 0, do_delete = 0;
4351 const char *obj_arg = NULL, *symref_target= NULL;
4352 char *refname = NULL;
4354 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4363 repo_path = realpath(optarg, NULL);
4364 if (repo_path == NULL)
4365 return got_error_from_errno2("realpath",
4367 got_path_strip_trailing_slashes(repo_path);
4373 symref_target = optarg;
4381 if (obj_arg && do_list)
4382 errx(1, "-c and -l options are mutually exclusive");
4383 if (obj_arg && do_delete)
4384 errx(1, "-c and -d options are mutually exclusive");
4385 if (obj_arg && symref_target)
4386 errx(1, "-c and -s options are mutually exclusive");
4387 if (symref_target && do_delete)
4388 errx(1, "-s and -d options are mutually exclusive");
4389 if (symref_target && do_list)
4390 errx(1, "-s and -l options are mutually exclusive");
4391 if (do_delete && do_list)
4392 errx(1, "-d and -l options are mutually exclusive");
4398 if (argc != 0 && argc != 1)
4401 refname = strdup(argv[0]);
4402 if (refname == NULL) {
4403 error = got_error_from_errno("strdup");
4410 refname = strdup(argv[0]);
4411 if (refname == NULL) {
4412 error = got_error_from_errno("strdup");
4418 got_path_strip_trailing_slashes(refname);
4422 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4426 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4427 "sendfd unveil", NULL) == -1)
4431 cwd = getcwd(NULL, 0);
4433 error = got_error_from_errno("getcwd");
4437 if (repo_path == NULL) {
4438 error = got_worktree_open(&worktree, cwd);
4439 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4445 strdup(got_worktree_get_repo_path(worktree));
4446 if (repo_path == NULL)
4447 error = got_error_from_errno("strdup");
4451 repo_path = strdup(cwd);
4452 if (repo_path == NULL) {
4453 error = got_error_from_errno("strdup");
4459 error = got_repo_open(&repo, repo_path, NULL);
4463 error = apply_unveil(got_repo_get_path(repo), do_list,
4464 worktree ? got_worktree_get_root_path(worktree) : NULL);
4469 error = list_refs(repo, refname);
4471 error = delete_ref(repo, refname);
4472 else if (symref_target)
4473 error = add_symref(repo, refname, symref_target);
4475 if (obj_arg == NULL)
4477 error = add_ref(repo, refname, obj_arg);
4482 got_repo_close(repo);
4484 got_worktree_close(worktree);
4494 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4495 "[name]\n", getprogname());
4499 static const struct got_error *
4500 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4501 struct got_reference *ref)
4503 const struct got_error *err = NULL;
4504 const char *refname, *marker = " ";
4507 refname = got_ref_get_name(ref);
4508 if (worktree && strcmp(refname,
4509 got_worktree_get_head_ref_name(worktree)) == 0) {
4510 struct got_object_id *id = NULL;
4512 err = got_ref_resolve(&id, repo, ref);
4515 if (got_object_id_cmp(id,
4516 got_worktree_get_base_commit_id(worktree)) == 0)
4523 if (strncmp(refname, "refs/heads/", 11) == 0)
4525 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4528 refstr = got_ref_to_str(ref);
4530 return got_error_from_errno("got_ref_to_str");
4532 printf("%s%s: %s\n", marker, refname, refstr);
4537 static const struct got_error *
4538 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4540 const char *refname;
4542 if (worktree == NULL)
4543 return got_error(GOT_ERR_NOT_WORKTREE);
4545 refname = got_worktree_get_head_ref_name(worktree);
4547 if (strncmp(refname, "refs/heads/", 11) == 0)
4549 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4552 printf("%s\n", refname);
4557 static const struct got_error *
4558 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4560 static const struct got_error *err = NULL;
4561 struct got_reflist_head refs;
4562 struct got_reflist_entry *re;
4563 struct got_reference *temp_ref = NULL;
4564 int rebase_in_progress, histedit_in_progress;
4566 SIMPLEQ_INIT(&refs);
4569 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4574 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4579 if (rebase_in_progress || histedit_in_progress) {
4580 err = got_ref_open(&temp_ref, repo,
4581 got_worktree_get_head_ref_name(worktree), 0);
4584 list_branch(repo, worktree, temp_ref);
4585 got_ref_close(temp_ref);
4589 err = got_ref_list(&refs, repo, "refs/heads",
4590 got_ref_cmp_by_name, NULL);
4594 SIMPLEQ_FOREACH(re, &refs, entry)
4595 list_branch(repo, worktree, re->ref);
4597 got_ref_list_free(&refs);
4601 static const struct got_error *
4602 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4603 const char *branch_name)
4605 const struct got_error *err = NULL;
4606 struct got_reference *ref = NULL;
4609 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4610 return got_error_from_errno("asprintf");
4612 err = got_ref_open(&ref, repo, refname, 0);
4617 strcmp(got_worktree_get_head_ref_name(worktree),
4618 got_ref_get_name(ref)) == 0) {
4619 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4620 "will not delete this work tree's current branch");
4624 err = got_ref_delete(ref, repo);
4632 static const struct got_error *
4633 add_branch(struct got_repository *repo, const char *branch_name,
4634 struct got_object_id *base_commit_id)
4636 const struct got_error *err = NULL;
4637 struct got_reference *ref = NULL;
4638 char *base_refname = NULL, *refname = NULL;
4641 * Don't let the user create a branch name with a leading '-'.
4642 * While technically a valid reference name, this case is usually
4643 * an unintended typo.
4645 if (branch_name[0] == '-')
4646 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4648 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4649 err = got_error_from_errno("asprintf");
4653 err = got_ref_open(&ref, repo, refname, 0);
4655 err = got_error(GOT_ERR_BRANCH_EXISTS);
4657 } else if (err->code != GOT_ERR_NOT_REF)
4660 err = got_ref_alloc(&ref, refname, base_commit_id);
4664 err = got_ref_write(ref, repo);
4673 static const struct got_error *
4674 cmd_branch(int argc, char *argv[])
4676 const struct got_error *error = NULL;
4677 struct got_repository *repo = NULL;
4678 struct got_worktree *worktree = NULL;
4679 char *cwd = NULL, *repo_path = NULL;
4680 int ch, do_list = 0, do_show = 0, do_update = 1;
4681 const char *delref = NULL, *commit_id_arg = NULL;
4682 struct got_reference *ref = NULL;
4683 struct got_pathlist_head paths;
4684 struct got_pathlist_entry *pe;
4685 struct got_object_id *commit_id = NULL;
4686 char *commit_id_str = NULL;
4690 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4693 commit_id_arg = optarg;
4699 repo_path = realpath(optarg, NULL);
4700 if (repo_path == NULL)
4701 return got_error_from_errno2("realpath",
4703 got_path_strip_trailing_slashes(repo_path);
4717 if (do_list && delref)
4718 errx(1, "-l and -d options are mutually exclusive");
4723 if (!do_list && !delref && argc == 0)
4726 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4727 errx(1, "-c option can only be used when creating a branch");
4729 if (do_list || delref) {
4732 } else if (!do_show && argc != 1)
4736 if (do_list || do_show) {
4737 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4741 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4742 "sendfd unveil", NULL) == -1)
4746 cwd = getcwd(NULL, 0);
4748 error = got_error_from_errno("getcwd");
4752 if (repo_path == NULL) {
4753 error = got_worktree_open(&worktree, cwd);
4754 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4760 strdup(got_worktree_get_repo_path(worktree));
4761 if (repo_path == NULL)
4762 error = got_error_from_errno("strdup");
4766 repo_path = strdup(cwd);
4767 if (repo_path == NULL) {
4768 error = got_error_from_errno("strdup");
4774 error = got_repo_open(&repo, repo_path, NULL);
4778 error = apply_unveil(got_repo_get_path(repo), do_list,
4779 worktree ? got_worktree_get_root_path(worktree) : NULL);
4784 error = show_current_branch(repo, worktree);
4786 error = list_branches(repo, worktree);
4788 error = delete_branch(repo, worktree, delref);
4790 if (commit_id_arg == NULL)
4791 commit_id_arg = worktree ?
4792 got_worktree_get_head_ref_name(worktree) :
4794 error = got_repo_match_object_id(&commit_id, NULL,
4795 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4798 error = add_branch(repo, argv[0], commit_id);
4801 if (worktree && do_update) {
4802 int did_something = 0;
4803 char *branch_refname = NULL;
4805 error = got_object_id_str(&commit_id_str, commit_id);
4808 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4812 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4814 error = got_error_from_errno("asprintf");
4817 error = got_ref_open(&ref, repo, branch_refname, 0);
4818 free(branch_refname);
4821 error = switch_head_ref(ref, commit_id, worktree,
4825 error = got_worktree_set_base_commit_id(worktree, repo,
4829 error = got_worktree_checkout_files(worktree, &paths,
4830 repo, update_progress, &did_something,
4831 check_cancelled, NULL);
4835 printf("Updated to commit %s\n", commit_id_str);
4842 got_repo_close(repo);
4844 got_worktree_close(worktree);
4848 free(commit_id_str);
4849 TAILQ_FOREACH(pe, &paths, entry)
4850 free((char *)pe->path);
4851 got_pathlist_free(&paths);
4860 "usage: %s tag [-c commit] [-r repository] [-l] "
4861 "[-m message] name\n", getprogname());
4866 static const struct got_error *
4867 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4869 const struct got_error *err = NULL;
4870 struct got_reflist_entry *re, *se, *new;
4871 struct got_object_id *re_id, *se_id;
4872 struct got_tag_object *re_tag, *se_tag;
4873 time_t re_time, se_time;
4875 SIMPLEQ_FOREACH(re, tags, entry) {
4876 se = SIMPLEQ_FIRST(sorted);
4878 err = got_reflist_entry_dup(&new, re);
4881 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4884 err = got_ref_resolve(&re_id, repo, re->ref);
4887 err = got_object_open_as_tag(&re_tag, repo, re_id);
4891 re_time = got_object_tag_get_tagger_time(re_tag);
4892 got_object_tag_close(re_tag);
4896 err = got_ref_resolve(&se_id, repo, re->ref);
4899 err = got_object_open_as_tag(&se_tag, repo, se_id);
4903 se_time = got_object_tag_get_tagger_time(se_tag);
4904 got_object_tag_close(se_tag);
4906 if (se_time > re_time) {
4907 err = got_reflist_entry_dup(&new, re);
4910 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4913 se = SIMPLEQ_NEXT(se, entry);
4922 static const struct got_error *
4923 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4925 static const struct got_error *err = NULL;
4926 struct got_reflist_head refs;
4927 struct got_reflist_entry *re;
4929 SIMPLEQ_INIT(&refs);
4931 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4935 SIMPLEQ_FOREACH(re, &refs, entry) {
4936 const char *refname;
4937 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4941 struct got_object_id *id;
4942 struct got_tag_object *tag;
4943 struct got_commit_object *commit = NULL;
4945 refname = got_ref_get_name(re->ref);
4946 if (strncmp(refname, "refs/tags/", 10) != 0)
4949 refstr = got_ref_to_str(re->ref);
4950 if (refstr == NULL) {
4951 err = got_error_from_errno("got_ref_to_str");
4954 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4957 err = got_ref_resolve(&id, repo, re->ref);
4960 err = got_object_open_as_tag(&tag, repo, id);
4962 if (err->code != GOT_ERR_OBJ_TYPE) {
4966 /* "lightweight" tag */
4967 err = got_object_open_as_commit(&commit, repo, id);
4972 tagger = got_object_commit_get_committer(commit);
4974 got_object_commit_get_committer_time(commit);
4975 err = got_object_id_str(&id_str, id);
4981 tagger = got_object_tag_get_tagger(tag);
4982 tagger_time = got_object_tag_get_tagger_time(tag);
4983 err = got_object_id_str(&id_str,
4984 got_object_tag_get_object_id(tag));
4988 printf("from: %s\n", tagger);
4989 datestr = get_datestr(&tagger_time, datebuf);
4991 printf("date: %s UTC\n", datestr);
4993 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4995 switch (got_object_tag_get_object_type(tag)) {
4996 case GOT_OBJ_TYPE_BLOB:
4997 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5000 case GOT_OBJ_TYPE_TREE:
5001 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5004 case GOT_OBJ_TYPE_COMMIT:
5005 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5008 case GOT_OBJ_TYPE_TAG:
5009 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5018 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5021 got_object_commit_close(commit);
5023 tagmsg0 = strdup(got_object_tag_get_message(tag));
5024 got_object_tag_close(tag);
5025 if (tagmsg0 == NULL) {
5026 err = got_error_from_errno("strdup");
5033 line = strsep(&tagmsg, "\n");
5035 printf(" %s\n", line);
5040 got_ref_list_free(&refs);
5044 static const struct got_error *
5045 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5046 const char *tag_name, const char *repo_path)
5048 const struct got_error *err = NULL;
5049 char *template = NULL, *initial_content = NULL;
5050 char *editor = NULL;
5053 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5054 err = got_error_from_errno("asprintf");
5058 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5059 commit_id_str, tag_name) == -1) {
5060 err = got_error_from_errno("asprintf");
5064 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5068 dprintf(fd, initial_content);
5071 err = get_editor(&editor);
5074 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5076 free(initial_content);
5080 /* Editor is done; we can now apply unveil(2) */
5082 err = apply_unveil(repo_path, 0, NULL);
5091 static const struct got_error *
5092 add_tag(struct got_repository *repo, const char *tag_name,
5093 const char *commit_arg, const char *tagmsg_arg)
5095 const struct got_error *err = NULL;
5096 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5097 char *label = NULL, *commit_id_str = NULL;
5098 struct got_reference *ref = NULL;
5099 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5100 char *tagmsg_path = NULL, *tag_id_str = NULL;
5101 int preserve_tagmsg = 0;
5104 * Don't let the user create a tag name with a leading '-'.
5105 * While technically a valid reference name, this case is usually
5106 * an unintended typo.
5108 if (tag_name[0] == '-')
5109 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5111 err = get_author(&tagger, repo);
5115 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5116 GOT_OBJ_TYPE_COMMIT, 1, repo);
5120 err = got_object_id_str(&commit_id_str, commit_id);
5124 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5125 refname = strdup(tag_name);
5126 if (refname == NULL) {
5127 err = got_error_from_errno("strdup");
5131 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5132 err = got_error_from_errno("asprintf");
5136 err = got_ref_open(&ref, repo, refname, 0);
5138 err = got_error(GOT_ERR_TAG_EXISTS);
5140 } else if (err->code != GOT_ERR_NOT_REF)
5143 if (tagmsg_arg == NULL) {
5144 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5145 tag_name, got_repo_get_path(repo));
5147 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5148 tagmsg_path != NULL)
5149 preserve_tagmsg = 1;
5154 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5155 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5158 preserve_tagmsg = 1;
5162 err = got_ref_alloc(&ref, refname, tag_id);
5165 preserve_tagmsg = 1;
5169 err = got_ref_write(ref, repo);
5172 preserve_tagmsg = 1;
5176 err = got_object_id_str(&tag_id_str, tag_id);
5179 preserve_tagmsg = 1;
5182 printf("Created tag %s\n", tag_id_str);
5184 if (preserve_tagmsg) {
5185 fprintf(stderr, "%s: tag message preserved in %s\n",
5186 getprogname(), tagmsg_path);
5187 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5188 err = got_error_from_errno2("unlink", tagmsg_path);
5193 free(commit_id_str);
5201 static const struct got_error *
5202 cmd_tag(int argc, char *argv[])
5204 const struct got_error *error = NULL;
5205 struct got_repository *repo = NULL;
5206 struct got_worktree *worktree = NULL;
5207 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5208 char *gitconfig_path = NULL;
5209 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5210 int ch, do_list = 0;
5212 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5215 commit_id_arg = optarg;
5221 repo_path = realpath(optarg, NULL);
5222 if (repo_path == NULL)
5223 return got_error_from_errno2("realpath",
5225 got_path_strip_trailing_slashes(repo_path);
5240 if (commit_id_arg != NULL)
5242 "-c option can only be used when creating a tag");
5244 errx(1, "-l and -m options are mutually exclusive");
5247 } else if (argc != 1)
5254 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5258 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5259 "sendfd unveil", NULL) == -1)
5263 cwd = getcwd(NULL, 0);
5265 error = got_error_from_errno("getcwd");
5269 if (repo_path == NULL) {
5270 error = got_worktree_open(&worktree, cwd);
5271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5277 strdup(got_worktree_get_repo_path(worktree));
5278 if (repo_path == NULL)
5279 error = got_error_from_errno("strdup");
5283 repo_path = strdup(cwd);
5284 if (repo_path == NULL) {
5285 error = got_error_from_errno("strdup");
5292 error = got_repo_open(&repo, repo_path, NULL);
5295 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5298 error = list_tags(repo, worktree);
5300 error = get_gitconfig_path(&gitconfig_path);
5303 error = got_repo_open(&repo, repo_path, gitconfig_path);
5308 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5313 if (commit_id_arg == NULL) {
5314 struct got_reference *head_ref;
5315 struct got_object_id *commit_id;
5316 error = got_ref_open(&head_ref, repo,
5317 worktree ? got_worktree_get_head_ref_name(worktree)
5321 error = got_ref_resolve(&commit_id, repo, head_ref);
5322 got_ref_close(head_ref);
5325 error = got_object_id_str(&commit_id_str, commit_id);
5331 error = add_tag(repo, tag_name,
5332 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5336 got_repo_close(repo);
5338 got_worktree_close(worktree);
5341 free(gitconfig_path);
5342 free(commit_id_str);
5349 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5354 static const struct got_error *
5355 add_progress(void *arg, unsigned char status, const char *path)
5357 while (path[0] == '/')
5359 printf("%c %s\n", status, path);
5363 static const struct got_error *
5364 cmd_add(int argc, char *argv[])
5366 const struct got_error *error = NULL;
5367 struct got_repository *repo = NULL;
5368 struct got_worktree *worktree = NULL;
5370 struct got_pathlist_head paths;
5371 struct got_pathlist_entry *pe;
5372 int ch, can_recurse = 0, no_ignores = 0;
5376 while ((ch = getopt(argc, argv, "IR")) != -1) {
5394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5401 cwd = getcwd(NULL, 0);
5403 error = got_error_from_errno("getcwd");
5407 error = got_worktree_open(&worktree, cwd);
5411 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5416 error = apply_unveil(got_repo_get_path(repo), 1,
5417 got_worktree_get_root_path(worktree));
5421 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5425 if (!can_recurse && no_ignores) {
5426 error = got_error_msg(GOT_ERR_BAD_PATH,
5427 "disregarding ignores requires -R option");
5435 TAILQ_FOREACH(pe, &paths, entry) {
5436 if (asprintf(&ondisk_path, "%s/%s",
5437 got_worktree_get_root_path(worktree),
5439 error = got_error_from_errno("asprintf");
5442 if (lstat(ondisk_path, &sb) == -1) {
5443 if (errno == ENOENT) {
5447 error = got_error_from_errno2("lstat",
5453 if (S_ISDIR(sb.st_mode)) {
5454 error = got_error_msg(GOT_ERR_BAD_PATH,
5455 "adding directories requires -R option");
5461 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5462 NULL, repo, no_ignores);
5465 got_repo_close(repo);
5467 got_worktree_close(worktree);
5468 TAILQ_FOREACH(pe, &paths, entry)
5469 free((char *)pe->path);
5470 got_pathlist_free(&paths);
5478 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5483 static const struct got_error *
5484 print_remove_status(void *arg, unsigned char status,
5485 unsigned char staged_status, const char *path)
5487 while (path[0] == '/')
5489 if (status == GOT_STATUS_NONEXISTENT)
5491 if (status == staged_status && (status == GOT_STATUS_DELETE))
5492 status = GOT_STATUS_NO_CHANGE;
5493 printf("%c%c %s\n", status, staged_status, path);
5497 static const struct got_error *
5498 cmd_remove(int argc, char *argv[])
5500 const struct got_error *error = NULL;
5501 struct got_worktree *worktree = NULL;
5502 struct got_repository *repo = NULL;
5504 struct got_pathlist_head paths;
5505 struct got_pathlist_entry *pe;
5506 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5510 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5513 delete_local_mods = 1;
5531 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5538 cwd = getcwd(NULL, 0);
5540 error = got_error_from_errno("getcwd");
5543 error = got_worktree_open(&worktree, cwd);
5547 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5552 error = apply_unveil(got_repo_get_path(repo), 1,
5553 got_worktree_get_root_path(worktree));
5557 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5564 TAILQ_FOREACH(pe, &paths, entry) {
5565 if (asprintf(&ondisk_path, "%s/%s",
5566 got_worktree_get_root_path(worktree),
5568 error = got_error_from_errno("asprintf");
5571 if (lstat(ondisk_path, &sb) == -1) {
5572 if (errno == ENOENT) {
5576 error = got_error_from_errno2("lstat",
5582 if (S_ISDIR(sb.st_mode)) {
5583 error = got_error_msg(GOT_ERR_BAD_PATH,
5584 "removing directories requires -R option");
5590 error = got_worktree_schedule_delete(worktree, &paths,
5591 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5594 got_repo_close(repo);
5596 got_worktree_close(worktree);
5597 TAILQ_FOREACH(pe, &paths, entry)
5598 free((char *)pe->path);
5599 got_pathlist_free(&paths);
5607 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5608 "path ...\n", getprogname());
5612 static const struct got_error *
5613 revert_progress(void *arg, unsigned char status, const char *path)
5615 if (status == GOT_STATUS_UNVERSIONED)
5618 while (path[0] == '/')
5620 printf("%c %s\n", status, path);
5624 struct choose_patch_arg {
5625 FILE *patch_script_file;
5629 static const struct got_error *
5630 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5631 int nchanges, const char *action)
5634 size_t linesize = 0;
5638 case GOT_STATUS_ADD:
5639 printf("A %s\n%s this addition? [y/n] ", path, action);
5641 case GOT_STATUS_DELETE:
5642 printf("D %s\n%s this deletion? [y/n] ", path, action);
5644 case GOT_STATUS_MODIFY:
5645 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5646 return got_error_from_errno("fseek");
5647 printf(GOT_COMMIT_SEP_STR);
5648 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5650 if (ferror(patch_file))
5651 return got_error_from_errno("getline");
5652 printf(GOT_COMMIT_SEP_STR);
5653 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5654 path, n, nchanges, action);
5657 return got_error_path(path, GOT_ERR_FILE_STATUS);
5663 static const struct got_error *
5664 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5665 FILE *patch_file, int n, int nchanges)
5667 const struct got_error *err = NULL;
5669 size_t linesize = 0;
5672 struct choose_patch_arg *a = arg;
5674 *choice = GOT_PATCH_CHOICE_NONE;
5676 if (a->patch_script_file) {
5678 err = show_change(status, path, patch_file, n, nchanges,
5682 linelen = getline(&line, &linesize, a->patch_script_file);
5683 if (linelen == -1) {
5684 if (ferror(a->patch_script_file))
5685 return got_error_from_errno("getline");
5688 nl = strchr(line, '\n');
5691 if (strcmp(line, "y") == 0) {
5692 *choice = GOT_PATCH_CHOICE_YES;
5694 } else if (strcmp(line, "n") == 0) {
5695 *choice = GOT_PATCH_CHOICE_NO;
5697 } else if (strcmp(line, "q") == 0 &&
5698 status == GOT_STATUS_MODIFY) {
5699 *choice = GOT_PATCH_CHOICE_QUIT;
5702 printf("invalid response '%s'\n", line);
5707 while (resp != 'y' && resp != 'n' && resp != 'q') {
5708 err = show_change(status, path, patch_file, n, nchanges,
5715 if (status == GOT_STATUS_MODIFY) {
5716 if (resp != 'y' && resp != 'n' && resp != 'q') {
5717 printf("invalid response '%c'\n", resp);
5720 } else if (resp != 'y' && resp != 'n') {
5721 printf("invalid response '%c'\n", resp);
5727 *choice = GOT_PATCH_CHOICE_YES;
5728 else if (resp == 'n')
5729 *choice = GOT_PATCH_CHOICE_NO;
5730 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5731 *choice = GOT_PATCH_CHOICE_QUIT;
5737 static const struct got_error *
5738 cmd_revert(int argc, char *argv[])
5740 const struct got_error *error = NULL;
5741 struct got_worktree *worktree = NULL;
5742 struct got_repository *repo = NULL;
5743 char *cwd = NULL, *path = NULL;
5744 struct got_pathlist_head paths;
5745 struct got_pathlist_entry *pe;
5746 int ch, can_recurse = 0, pflag = 0;
5747 FILE *patch_script_file = NULL;
5748 const char *patch_script_path = NULL;
5749 struct choose_patch_arg cpa;
5753 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5759 patch_script_path = optarg;
5774 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5775 "unveil", NULL) == -1)
5780 if (patch_script_path && !pflag)
5781 errx(1, "-F option can only be used together with -p option");
5783 cwd = getcwd(NULL, 0);
5785 error = got_error_from_errno("getcwd");
5788 error = got_worktree_open(&worktree, cwd);
5792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5797 if (patch_script_path) {
5798 patch_script_file = fopen(patch_script_path, "r");
5799 if (patch_script_file == NULL) {
5800 error = got_error_from_errno2("fopen",
5805 error = apply_unveil(got_repo_get_path(repo), 1,
5806 got_worktree_get_root_path(worktree));
5810 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5817 TAILQ_FOREACH(pe, &paths, entry) {
5818 if (asprintf(&ondisk_path, "%s/%s",
5819 got_worktree_get_root_path(worktree),
5821 error = got_error_from_errno("asprintf");
5824 if (lstat(ondisk_path, &sb) == -1) {
5825 if (errno == ENOENT) {
5829 error = got_error_from_errno2("lstat",
5835 if (S_ISDIR(sb.st_mode)) {
5836 error = got_error_msg(GOT_ERR_BAD_PATH,
5837 "reverting directories requires -R option");
5843 cpa.patch_script_file = patch_script_file;
5844 cpa.action = "revert";
5845 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5846 pflag ? choose_patch : NULL, &cpa, repo);
5848 if (patch_script_file && fclose(patch_script_file) == EOF &&
5850 error = got_error_from_errno2("fclose", patch_script_path);
5852 got_repo_close(repo);
5854 got_worktree_close(worktree);
5863 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5868 struct collect_commit_logmsg_arg {
5869 const char *cmdline_log;
5871 const char *worktree_path;
5872 const char *branch_name;
5873 const char *repo_path;
5878 static const struct got_error *
5879 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5882 char *initial_content = NULL;
5883 struct got_pathlist_entry *pe;
5884 const struct got_error *err = NULL;
5885 char *template = NULL;
5886 struct collect_commit_logmsg_arg *a = arg;
5890 /* if a message was specified on the command line, just use it */
5891 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5892 len = strlen(a->cmdline_log) + 1;
5893 *logmsg = malloc(len + 1);
5894 if (*logmsg == NULL)
5895 return got_error_from_errno("malloc");
5896 strlcpy(*logmsg, a->cmdline_log, len);
5900 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5901 return got_error_from_errno("asprintf");
5903 if (asprintf(&initial_content,
5904 "\n# changes to be committed on branch %s:\n",
5905 a->branch_name) == -1)
5906 return got_error_from_errno("asprintf");
5908 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5912 dprintf(fd, initial_content);
5914 TAILQ_FOREACH(pe, commitable_paths, entry) {
5915 struct got_commitable *ct = pe->data;
5916 dprintf(fd, "# %c %s\n",
5917 got_commitable_get_status(ct),
5918 got_commitable_get_path(ct));
5922 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5924 free(initial_content);
5927 /* Editor is done; we can now apply unveil(2) */
5929 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5938 static const struct got_error *
5939 cmd_commit(int argc, char *argv[])
5941 const struct got_error *error = NULL;
5942 struct got_worktree *worktree = NULL;
5943 struct got_repository *repo = NULL;
5944 char *cwd = NULL, *id_str = NULL;
5945 struct got_object_id *id = NULL;
5946 const char *logmsg = NULL;
5947 struct collect_commit_logmsg_arg cl_arg;
5948 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5949 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5950 struct got_pathlist_head paths;
5953 cl_arg.logmsg_path = NULL;
5955 while ((ch = getopt(argc, argv, "m:")) != -1) {
5970 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5971 "unveil", NULL) == -1)
5974 cwd = getcwd(NULL, 0);
5976 error = got_error_from_errno("getcwd");
5979 error = got_worktree_open(&worktree, cwd);
5983 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5986 if (rebase_in_progress) {
5987 error = got_error(GOT_ERR_REBASING);
5991 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5996 error = get_gitconfig_path(&gitconfig_path);
5999 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6004 error = get_author(&author, repo);
6009 * unveil(2) traverses exec(2); if an editor is used we have
6010 * to apply unveil after the log message has been written.
6012 if (logmsg == NULL || strlen(logmsg) == 0)
6013 error = get_editor(&editor);
6015 error = apply_unveil(got_repo_get_path(repo), 0,
6016 got_worktree_get_root_path(worktree));
6020 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6024 cl_arg.editor = editor;
6025 cl_arg.cmdline_log = logmsg;
6026 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6027 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6028 if (!histedit_in_progress) {
6029 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6030 error = got_error(GOT_ERR_COMMIT_BRANCH);
6033 cl_arg.branch_name += 11;
6035 cl_arg.repo_path = got_repo_get_path(repo);
6036 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6037 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6039 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6040 cl_arg.logmsg_path != NULL)
6041 preserve_logmsg = 1;
6045 error = got_object_id_str(&id_str, id);
6048 printf("Created commit %s\n", id_str);
6050 if (preserve_logmsg) {
6051 fprintf(stderr, "%s: log message preserved in %s\n",
6052 getprogname(), cl_arg.logmsg_path);
6053 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6055 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6056 free(cl_arg.logmsg_path);
6058 got_repo_close(repo);
6060 got_worktree_close(worktree);
6063 free(gitconfig_path);
6070 usage_cherrypick(void)
6072 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6076 static const struct got_error *
6077 cmd_cherrypick(int argc, char *argv[])
6079 const struct got_error *error = NULL;
6080 struct got_worktree *worktree = NULL;
6081 struct got_repository *repo = NULL;
6082 char *cwd = NULL, *commit_id_str = NULL;
6083 struct got_object_id *commit_id = NULL;
6084 struct got_commit_object *commit = NULL;
6085 struct got_object_qid *pid;
6086 struct got_reference *head_ref = NULL;
6087 int ch, did_something = 0;
6089 while ((ch = getopt(argc, argv, "")) != -1) {
6101 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6102 "unveil", NULL) == -1)
6108 cwd = getcwd(NULL, 0);
6110 error = got_error_from_errno("getcwd");
6113 error = got_worktree_open(&worktree, cwd);
6117 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6122 error = apply_unveil(got_repo_get_path(repo), 0,
6123 got_worktree_get_root_path(worktree));
6127 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6128 GOT_OBJ_TYPE_COMMIT, repo);
6129 if (error != NULL) {
6130 struct got_reference *ref;
6131 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6133 error = got_ref_open(&ref, repo, argv[0], 0);
6136 error = got_ref_resolve(&commit_id, repo, ref);
6141 error = got_object_id_str(&commit_id_str, commit_id);
6145 error = got_ref_open(&head_ref, repo,
6146 got_worktree_get_head_ref_name(worktree), 0);
6150 error = check_same_branch(commit_id, head_ref, NULL, repo);
6152 if (error->code != GOT_ERR_ANCESTRY)
6156 error = got_error(GOT_ERR_SAME_BRANCH);
6160 error = got_object_open_as_commit(&commit, repo, commit_id);
6163 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6164 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6165 commit_id, repo, update_progress, &did_something, check_cancelled,
6171 printf("Merged commit %s\n", commit_id_str);
6174 got_object_commit_close(commit);
6175 free(commit_id_str);
6177 got_ref_close(head_ref);
6179 got_worktree_close(worktree);
6181 got_repo_close(repo);
6188 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6192 static const struct got_error *
6193 cmd_backout(int argc, char *argv[])
6195 const struct got_error *error = NULL;
6196 struct got_worktree *worktree = NULL;
6197 struct got_repository *repo = NULL;
6198 char *cwd = NULL, *commit_id_str = NULL;
6199 struct got_object_id *commit_id = NULL;
6200 struct got_commit_object *commit = NULL;
6201 struct got_object_qid *pid;
6202 struct got_reference *head_ref = NULL;
6203 int ch, did_something = 0;
6205 while ((ch = getopt(argc, argv, "")) != -1) {
6217 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6218 "unveil", NULL) == -1)
6224 cwd = getcwd(NULL, 0);
6226 error = got_error_from_errno("getcwd");
6229 error = got_worktree_open(&worktree, cwd);
6233 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6238 error = apply_unveil(got_repo_get_path(repo), 0,
6239 got_worktree_get_root_path(worktree));
6243 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6244 GOT_OBJ_TYPE_COMMIT, repo);
6245 if (error != NULL) {
6246 struct got_reference *ref;
6247 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6249 error = got_ref_open(&ref, repo, argv[0], 0);
6252 error = got_ref_resolve(&commit_id, repo, ref);
6257 error = got_object_id_str(&commit_id_str, commit_id);
6261 error = got_ref_open(&head_ref, repo,
6262 got_worktree_get_head_ref_name(worktree), 0);
6266 error = check_same_branch(commit_id, head_ref, NULL, repo);
6270 error = got_object_open_as_commit(&commit, repo, commit_id);
6273 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6275 error = got_error(GOT_ERR_ROOT_COMMIT);
6279 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6280 update_progress, &did_something, check_cancelled, NULL);
6285 printf("Backed out commit %s\n", commit_id_str);
6288 got_object_commit_close(commit);
6289 free(commit_id_str);
6291 got_ref_close(head_ref);
6293 got_worktree_close(worktree);
6295 got_repo_close(repo);
6302 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6308 trim_logmsg(char *logmsg, int limit)
6313 len = strlen(logmsg);
6317 nl = strchr(logmsg, '\n');
6322 static const struct got_error *
6323 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6325 const struct got_error *err;
6326 char *logmsg0 = NULL;
6329 err = got_object_commit_get_logmsg(&logmsg0, commit);
6334 while (isspace((unsigned char)s[0]))
6337 *logmsg = strdup(s);
6338 if (*logmsg == NULL) {
6339 err = got_error_from_errno("strdup");
6343 trim_logmsg(*logmsg, limit);
6349 static const struct got_error *
6350 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6352 const struct got_error *err;
6353 struct got_commit_object *commit = NULL;
6354 char *id_str = NULL, *logmsg = NULL;
6356 err = got_object_open_as_commit(&commit, repo, id);
6360 err = got_object_id_str(&id_str, id);
6366 err = get_short_logmsg(&logmsg, 42, commit);
6370 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6373 got_object_commit_close(commit);
6378 static const struct got_error *
6379 show_rebase_progress(struct got_commit_object *commit,
6380 struct got_object_id *old_id, struct got_object_id *new_id)
6382 const struct got_error *err;
6383 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6385 err = got_object_id_str(&old_id_str, old_id);
6390 err = got_object_id_str(&new_id_str, new_id);
6395 old_id_str[12] = '\0';
6397 new_id_str[12] = '\0';
6399 err = get_short_logmsg(&logmsg, 42, commit);
6403 printf("%s -> %s: %s\n", old_id_str,
6404 new_id_str ? new_id_str : "no-op change", logmsg);
6412 static const struct got_error *
6413 rebase_progress(void *arg, unsigned char status, const char *path)
6415 unsigned char *rebase_status = arg;
6417 while (path[0] == '/')
6419 printf("%c %s\n", status, path);
6421 if (*rebase_status == GOT_STATUS_CONFLICT)
6423 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6424 *rebase_status = status;
6428 static const struct got_error *
6429 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6430 struct got_reference *branch, struct got_reference *new_base_branch,
6431 struct got_reference *tmp_branch, struct got_repository *repo)
6433 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6434 return got_worktree_rebase_complete(worktree, fileindex,
6435 new_base_branch, tmp_branch, branch, repo);
6438 static const struct got_error *
6439 rebase_commit(struct got_pathlist_head *merged_paths,
6440 struct got_worktree *worktree, struct got_fileindex *fileindex,
6441 struct got_reference *tmp_branch,
6442 struct got_object_id *commit_id, struct got_repository *repo)
6444 const struct got_error *error;
6445 struct got_commit_object *commit;
6446 struct got_object_id *new_commit_id;
6448 error = got_object_open_as_commit(&commit, repo, commit_id);
6452 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6453 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6455 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6457 error = show_rebase_progress(commit, commit_id, NULL);
6459 error = show_rebase_progress(commit, commit_id, new_commit_id);
6460 free(new_commit_id);
6463 got_object_commit_close(commit);
6467 struct check_path_prefix_arg {
6468 const char *path_prefix;
6473 static const struct got_error *
6474 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6475 struct got_blob_object *blob2, struct got_object_id *id1,
6476 struct got_object_id *id2, const char *path1, const char *path2,
6477 mode_t mode1, mode_t mode2, struct got_repository *repo)
6479 struct check_path_prefix_arg *a = arg;
6481 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6482 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6483 return got_error(a->errcode);
6488 static const struct got_error *
6489 check_path_prefix(struct got_object_id *parent_id,
6490 struct got_object_id *commit_id, const char *path_prefix,
6491 int errcode, struct got_repository *repo)
6493 const struct got_error *err;
6494 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6495 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6496 struct check_path_prefix_arg cpp_arg;
6498 if (got_path_is_root_dir(path_prefix))
6501 err = got_object_open_as_commit(&commit, repo, commit_id);
6505 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6509 err = got_object_open_as_tree(&tree1, repo,
6510 got_object_commit_get_tree_id(parent_commit));
6514 err = got_object_open_as_tree(&tree2, repo,
6515 got_object_commit_get_tree_id(commit));
6519 cpp_arg.path_prefix = path_prefix;
6520 while (cpp_arg.path_prefix[0] == '/')
6521 cpp_arg.path_prefix++;
6522 cpp_arg.len = strlen(cpp_arg.path_prefix);
6523 cpp_arg.errcode = errcode;
6524 err = got_diff_tree(tree1, tree2, "", "", repo,
6525 check_path_prefix_in_diff, &cpp_arg, 0);
6528 got_object_tree_close(tree1);
6530 got_object_tree_close(tree2);
6532 got_object_commit_close(commit);
6534 got_object_commit_close(parent_commit);
6538 static const struct got_error *
6539 collect_commits(struct got_object_id_queue *commits,
6540 struct got_object_id *initial_commit_id,
6541 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6542 const char *path_prefix, int path_prefix_errcode,
6543 struct got_repository *repo)
6545 const struct got_error *err = NULL;
6546 struct got_commit_graph *graph = NULL;
6547 struct got_object_id *parent_id = NULL;
6548 struct got_object_qid *qid;
6549 struct got_object_id *commit_id = initial_commit_id;
6551 err = got_commit_graph_open(&graph, "/", 1);
6555 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6556 check_cancelled, NULL);
6559 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6560 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6561 check_cancelled, NULL);
6563 if (err->code == GOT_ERR_ITER_COMPLETED) {
6564 err = got_error_msg(GOT_ERR_ANCESTRY,
6565 "ran out of commits to rebase before "
6566 "youngest common ancestor commit has "
6571 err = check_path_prefix(parent_id, commit_id,
6572 path_prefix, path_prefix_errcode, repo);
6576 err = got_object_qid_alloc(&qid, commit_id);
6579 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6580 commit_id = parent_id;
6584 got_commit_graph_close(graph);
6588 static const struct got_error *
6589 cmd_rebase(int argc, char *argv[])
6591 const struct got_error *error = NULL;
6592 struct got_worktree *worktree = NULL;
6593 struct got_repository *repo = NULL;
6594 struct got_fileindex *fileindex = NULL;
6596 struct got_reference *branch = NULL;
6597 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6598 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6599 struct got_object_id *resume_commit_id = NULL;
6600 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6601 struct got_commit_object *commit = NULL;
6602 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6603 int histedit_in_progress = 0;
6604 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6605 struct got_object_id_queue commits;
6606 struct got_pathlist_head merged_paths;
6607 const struct got_object_id_queue *parent_ids;
6608 struct got_object_qid *qid, *pid;
6610 SIMPLEQ_INIT(&commits);
6611 TAILQ_INIT(&merged_paths);
6613 while ((ch = getopt(argc, argv, "ac")) != -1) {
6619 continue_rebase = 1;
6631 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6632 "unveil", NULL) == -1)
6635 if (abort_rebase && continue_rebase)
6637 else if (abort_rebase || continue_rebase) {
6640 } else if (argc != 1)
6643 cwd = getcwd(NULL, 0);
6645 error = got_error_from_errno("getcwd");
6648 error = got_worktree_open(&worktree, cwd);
6652 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6657 error = apply_unveil(got_repo_get_path(repo), 0,
6658 got_worktree_get_root_path(worktree));
6662 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6666 if (histedit_in_progress) {
6667 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6671 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6677 if (!rebase_in_progress) {
6678 error = got_error(GOT_ERR_NOT_REBASING);
6681 error = got_worktree_rebase_continue(&resume_commit_id,
6682 &new_base_branch, &tmp_branch, &branch, &fileindex,
6686 printf("Switching work tree to %s\n",
6687 got_ref_get_symref_target(new_base_branch));
6688 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6689 new_base_branch, update_progress, &did_something);
6692 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6693 goto done; /* nothing else to do */
6696 if (continue_rebase) {
6697 if (!rebase_in_progress) {
6698 error = got_error(GOT_ERR_NOT_REBASING);
6701 error = got_worktree_rebase_continue(&resume_commit_id,
6702 &new_base_branch, &tmp_branch, &branch, &fileindex,
6707 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6708 resume_commit_id, repo);
6712 yca_id = got_object_id_dup(resume_commit_id);
6713 if (yca_id == NULL) {
6714 error = got_error_from_errno("got_object_id_dup");
6718 error = got_ref_open(&branch, repo, argv[0], 0);
6723 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6727 if (!continue_rebase) {
6728 struct got_object_id *base_commit_id;
6730 base_commit_id = got_worktree_get_base_commit_id(worktree);
6731 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6732 base_commit_id, branch_head_commit_id, repo,
6733 check_cancelled, NULL);
6736 if (yca_id == NULL) {
6737 error = got_error_msg(GOT_ERR_ANCESTRY,
6738 "specified branch shares no common ancestry "
6739 "with work tree's branch");
6743 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6745 if (error->code != GOT_ERR_ANCESTRY)
6749 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6750 "specified branch resolves to a commit which "
6751 "is already contained in work tree's branch");
6754 error = got_worktree_rebase_prepare(&new_base_branch,
6755 &tmp_branch, &fileindex, worktree, branch, repo);
6760 commit_id = branch_head_commit_id;
6761 error = got_object_open_as_commit(&commit, repo, commit_id);
6765 parent_ids = got_object_commit_get_parent_ids(commit);
6766 pid = SIMPLEQ_FIRST(parent_ids);
6768 if (!continue_rebase) {
6770 error = got_worktree_rebase_abort(worktree, fileindex,
6771 repo, new_base_branch, update_progress,
6775 printf("Rebase of %s aborted\n",
6776 got_ref_get_name(branch));
6778 error = got_error(GOT_ERR_EMPTY_REBASE);
6781 error = collect_commits(&commits, commit_id, pid->id,
6782 yca_id, got_worktree_get_path_prefix(worktree),
6783 GOT_ERR_REBASE_PATH, repo);
6784 got_object_commit_close(commit);
6789 if (SIMPLEQ_EMPTY(&commits)) {
6790 if (continue_rebase) {
6791 error = rebase_complete(worktree, fileindex,
6792 branch, new_base_branch, tmp_branch, repo);
6795 /* Fast-forward the reference of the branch. */
6796 struct got_object_id *new_head_commit_id;
6798 error = got_ref_resolve(&new_head_commit_id, repo,
6802 error = got_object_id_str(&id_str, new_head_commit_id);
6803 printf("Forwarding %s to commit %s\n",
6804 got_ref_get_name(branch), id_str);
6806 error = got_ref_change_ref(branch,
6807 new_head_commit_id);
6814 SIMPLEQ_FOREACH(qid, &commits, entry) {
6815 commit_id = qid->id;
6816 parent_id = pid ? pid->id : yca_id;
6819 error = got_worktree_rebase_merge_files(&merged_paths,
6820 worktree, fileindex, parent_id, commit_id, repo,
6821 rebase_progress, &rebase_status, check_cancelled, NULL);
6825 if (rebase_status == GOT_STATUS_CONFLICT) {
6826 error = show_rebase_merge_conflict(qid->id, repo);
6829 got_worktree_rebase_pathlist_free(&merged_paths);
6833 error = rebase_commit(&merged_paths, worktree, fileindex,
6834 tmp_branch, commit_id, repo);
6835 got_worktree_rebase_pathlist_free(&merged_paths);
6840 if (rebase_status == GOT_STATUS_CONFLICT) {
6841 error = got_worktree_rebase_postpone(worktree, fileindex);
6844 error = got_error_msg(GOT_ERR_CONFLICTS,
6845 "conflicts must be resolved before rebasing can continue");
6847 error = rebase_complete(worktree, fileindex, branch,
6848 new_base_branch, tmp_branch, repo);
6850 got_object_id_queue_free(&commits);
6851 free(branch_head_commit_id);
6852 free(resume_commit_id);
6855 got_object_commit_close(commit);
6857 got_ref_close(branch);
6858 if (new_base_branch)
6859 got_ref_close(new_base_branch);
6861 got_ref_close(tmp_branch);
6863 got_worktree_close(worktree);
6865 got_repo_close(repo);
6870 usage_histedit(void)
6872 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6877 #define GOT_HISTEDIT_PICK 'p'
6878 #define GOT_HISTEDIT_EDIT 'e'
6879 #define GOT_HISTEDIT_FOLD 'f'
6880 #define GOT_HISTEDIT_DROP 'd'
6881 #define GOT_HISTEDIT_MESG 'm'
6883 static struct got_histedit_cmd {
6887 } got_histedit_cmds[] = {
6888 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6889 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6890 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6892 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6893 { GOT_HISTEDIT_MESG, "mesg",
6894 "single-line log message for commit above (open editor if empty)" },
6897 struct got_histedit_list_entry {
6898 TAILQ_ENTRY(got_histedit_list_entry) entry;
6899 struct got_object_id *commit_id;
6900 const struct got_histedit_cmd *cmd;
6903 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6905 static const struct got_error *
6906 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6907 FILE *f, struct got_repository *repo)
6909 const struct got_error *err = NULL;
6910 char *logmsg = NULL, *id_str = NULL;
6911 struct got_commit_object *commit = NULL;
6914 err = got_object_open_as_commit(&commit, repo, commit_id);
6918 err = get_short_logmsg(&logmsg, 34, commit);
6922 err = got_object_id_str(&id_str, commit_id);
6926 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6928 err = got_ferror(f, GOT_ERR_IO);
6931 got_object_commit_close(commit);
6937 static const struct got_error *
6938 histedit_write_commit_list(struct got_object_id_queue *commits,
6939 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6941 const struct got_error *err = NULL;
6942 struct got_object_qid *qid;
6944 if (SIMPLEQ_EMPTY(commits))
6945 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6947 SIMPLEQ_FOREACH(qid, commits, entry) {
6948 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6952 if (edit_logmsg_only) {
6953 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6955 err = got_ferror(f, GOT_ERR_IO);
6964 static const struct got_error *
6965 write_cmd_list(FILE *f, const char *branch_name,
6966 struct got_object_id_queue *commits)
6968 const struct got_error *err = NULL;
6971 struct got_object_qid *qid;
6973 qid = SIMPLEQ_FIRST(commits);
6974 err = got_object_id_str(&id_str, qid->id);
6979 "# Editing the history of branch '%s' starting at\n"
6981 "# Commits will be processed in order from top to "
6982 "bottom of this file.\n", branch_name, id_str);
6984 err = got_ferror(f, GOT_ERR_IO);
6988 n = fprintf(f, "# Available histedit commands:\n");
6990 err = got_ferror(f, GOT_ERR_IO);
6994 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6995 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6996 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6999 err = got_ferror(f, GOT_ERR_IO);
7008 static const struct got_error *
7009 histedit_syntax_error(int lineno)
7011 static char msg[42];
7014 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7016 if (ret == -1 || ret >= sizeof(msg))
7017 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7019 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7022 static const struct got_error *
7023 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7024 char *logmsg, struct got_repository *repo)
7026 const struct got_error *err;
7027 struct got_commit_object *folded_commit = NULL;
7028 char *id_str, *folded_logmsg = NULL;
7030 err = got_object_id_str(&id_str, hle->commit_id);
7034 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7038 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7041 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7042 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7043 folded_logmsg) == -1) {
7044 err = got_error_from_errno("asprintf");
7048 got_object_commit_close(folded_commit);
7050 free(folded_logmsg);
7054 static struct got_histedit_list_entry *
7055 get_folded_commits(struct got_histedit_list_entry *hle)
7057 struct got_histedit_list_entry *prev, *folded = NULL;
7059 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7060 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7061 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7062 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7064 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7070 static const struct got_error *
7071 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7072 struct got_repository *repo)
7074 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7075 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7076 const struct got_error *err = NULL;
7077 struct got_commit_object *commit = NULL;
7079 struct got_histedit_list_entry *folded = NULL;
7081 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7085 folded = get_folded_commits(hle);
7087 while (folded != hle) {
7088 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7089 folded = TAILQ_NEXT(folded, entry);
7092 err = append_folded_commit_msg(&new_msg, folded,
7098 folded = TAILQ_NEXT(folded, entry);
7102 err = got_object_id_str(&id_str, hle->commit_id);
7105 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7108 if (asprintf(&new_msg,
7109 "%s\n# original log message of commit %s: %s",
7110 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7111 err = got_error_from_errno("asprintf");
7117 err = got_object_id_str(&id_str, hle->commit_id);
7121 err = got_opentemp_named_fd(&logmsg_path, &fd,
7122 GOT_TMPDIR_STR "/got-logmsg");
7126 dprintf(fd, logmsg);
7129 err = get_editor(&editor);
7133 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7135 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7137 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7140 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7141 err = got_error_from_errno2("unlink", logmsg_path);
7147 got_object_commit_close(commit);
7151 static const struct got_error *
7152 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7153 FILE *f, struct got_repository *repo)
7155 const struct got_error *err = NULL;
7156 char *line = NULL, *p, *end;
7160 const struct got_histedit_cmd *cmd;
7161 struct got_object_id *commit_id = NULL;
7162 struct got_histedit_list_entry *hle = NULL;
7165 len = getline(&line, &size, f);
7167 const struct got_error *getline_err;
7170 getline_err = got_error_from_errno("getline");
7171 err = got_ferror(f, getline_err->code);
7176 while (isspace((unsigned char)p[0]))
7178 if (p[0] == '#' || p[0] == '\0') {
7184 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7185 cmd = &got_histedit_cmds[i];
7186 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7187 isspace((unsigned char)p[strlen(cmd->name)])) {
7188 p += strlen(cmd->name);
7191 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7196 if (i == nitems(got_histedit_cmds)) {
7197 err = histedit_syntax_error(lineno);
7200 while (isspace((unsigned char)p[0]))
7202 if (cmd->code == GOT_HISTEDIT_MESG) {
7203 if (hle == NULL || hle->logmsg != NULL) {
7204 err = got_error(GOT_ERR_HISTEDIT_CMD);
7208 err = histedit_edit_logmsg(hle, repo);
7212 hle->logmsg = strdup(p);
7213 if (hle->logmsg == NULL) {
7214 err = got_error_from_errno("strdup");
7223 while (end[0] && !isspace((unsigned char)end[0]))
7227 err = got_object_resolve_id_str(&commit_id, repo, p);
7229 /* override error code */
7230 err = histedit_syntax_error(lineno);
7234 hle = malloc(sizeof(*hle));
7236 err = got_error_from_errno("malloc");
7240 hle->commit_id = commit_id;
7245 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7253 static const struct got_error *
7254 histedit_check_script(struct got_histedit_list *histedit_cmds,
7255 struct got_object_id_queue *commits, struct got_repository *repo)
7257 const struct got_error *err = NULL;
7258 struct got_object_qid *qid;
7259 struct got_histedit_list_entry *hle;
7260 static char msg[92];
7263 if (TAILQ_EMPTY(histedit_cmds))
7264 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7265 "histedit script contains no commands");
7266 if (SIMPLEQ_EMPTY(commits))
7267 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7269 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7270 struct got_histedit_list_entry *hle2;
7271 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7274 if (got_object_id_cmp(hle->commit_id,
7275 hle2->commit_id) != 0)
7277 err = got_object_id_str(&id_str, hle->commit_id);
7280 snprintf(msg, sizeof(msg), "commit %s is listed "
7281 "more than once in histedit script", id_str);
7283 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7287 SIMPLEQ_FOREACH(qid, commits, entry) {
7288 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7289 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7293 err = got_object_id_str(&id_str, qid->id);
7296 snprintf(msg, sizeof(msg),
7297 "commit %s missing from histedit script", id_str);
7299 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7303 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7304 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7305 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7306 "last commit in histedit script cannot be folded");
7311 static const struct got_error *
7312 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7313 const char *path, struct got_object_id_queue *commits,
7314 struct got_repository *repo)
7316 const struct got_error *err = NULL;
7320 err = get_editor(&editor);
7324 if (spawn_editor(editor, path) == -1) {
7325 err = got_error_from_errno("failed spawning editor");
7329 f = fopen(path, "r");
7331 err = got_error_from_errno("fopen");
7334 err = histedit_parse_list(histedit_cmds, f, repo);
7338 err = histedit_check_script(histedit_cmds, commits, repo);
7340 if (f && fclose(f) != 0 && err == NULL)
7341 err = got_error_from_errno("fclose");
7346 static const struct got_error *
7347 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7348 struct got_object_id_queue *, const char *, const char *,
7349 struct got_repository *);
7351 static const struct got_error *
7352 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7353 struct got_object_id_queue *commits, const char *branch_name,
7354 int edit_logmsg_only, struct got_repository *repo)
7356 const struct got_error *err;
7360 err = got_opentemp_named(&path, &f, "got-histedit");
7364 err = write_cmd_list(f, branch_name, commits);
7368 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7372 if (edit_logmsg_only) {
7374 err = histedit_parse_list(histedit_cmds, f, repo);
7376 if (fclose(f) != 0) {
7377 err = got_error_from_errno("fclose");
7381 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7383 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7384 err->code != GOT_ERR_HISTEDIT_CMD)
7386 err = histedit_edit_list_retry(histedit_cmds, err,
7387 commits, path, branch_name, repo);
7391 if (f && fclose(f) != 0 && err == NULL)
7392 err = got_error_from_errno("fclose");
7393 if (path && unlink(path) != 0 && err == NULL)
7394 err = got_error_from_errno2("unlink", path);
7399 static const struct got_error *
7400 histedit_save_list(struct got_histedit_list *histedit_cmds,
7401 struct got_worktree *worktree, struct got_repository *repo)
7403 const struct got_error *err = NULL;
7406 struct got_histedit_list_entry *hle;
7407 struct got_commit_object *commit = NULL;
7409 err = got_worktree_get_histedit_script_path(&path, worktree);
7413 f = fopen(path, "w");
7415 err = got_error_from_errno2("fopen", path);
7418 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7419 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7425 int n = fprintf(f, "%c %s\n",
7426 GOT_HISTEDIT_MESG, hle->logmsg);
7428 err = got_ferror(f, GOT_ERR_IO);
7434 if (f && fclose(f) != 0 && err == NULL)
7435 err = got_error_from_errno("fclose");
7438 got_object_commit_close(commit);
7443 histedit_free_list(struct got_histedit_list *histedit_cmds)
7445 struct got_histedit_list_entry *hle;
7447 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7448 TAILQ_REMOVE(histedit_cmds, hle, entry);
7453 static const struct got_error *
7454 histedit_load_list(struct got_histedit_list *histedit_cmds,
7455 const char *path, struct got_repository *repo)
7457 const struct got_error *err = NULL;
7460 f = fopen(path, "r");
7462 err = got_error_from_errno2("fopen", path);
7466 err = histedit_parse_list(histedit_cmds, f, repo);
7468 if (f && fclose(f) != 0 && err == NULL)
7469 err = got_error_from_errno("fclose");
7473 static const struct got_error *
7474 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7475 const struct got_error *edit_err, struct got_object_id_queue *commits,
7476 const char *path, const char *branch_name, struct got_repository *repo)
7478 const struct got_error *err = NULL, *prev_err = edit_err;
7481 while (resp != 'c' && resp != 'r' && resp != 'a') {
7482 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7483 "or (a)bort: ", getprogname(), prev_err->msg);
7488 histedit_free_list(histedit_cmds);
7489 err = histedit_run_editor(histedit_cmds, path, commits,
7492 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7493 err->code != GOT_ERR_HISTEDIT_CMD)
7500 } else if (resp == 'r') {
7501 histedit_free_list(histedit_cmds);
7502 err = histedit_edit_script(histedit_cmds,
7503 commits, branch_name, 0, repo);
7505 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7506 err->code != GOT_ERR_HISTEDIT_CMD)
7513 } else if (resp == 'a') {
7514 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7517 printf("invalid response '%c'\n", resp);
7523 static const struct got_error *
7524 histedit_complete(struct got_worktree *worktree,
7525 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7526 struct got_reference *branch, struct got_repository *repo)
7528 printf("Switching work tree to %s\n",
7529 got_ref_get_symref_target(branch));
7530 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7534 static const struct got_error *
7535 show_histedit_progress(struct got_commit_object *commit,
7536 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7538 const struct got_error *err;
7539 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7541 err = got_object_id_str(&old_id_str, hle->commit_id);
7546 err = got_object_id_str(&new_id_str, new_id);
7551 old_id_str[12] = '\0';
7553 new_id_str[12] = '\0';
7556 logmsg = strdup(hle->logmsg);
7557 if (logmsg == NULL) {
7558 err = got_error_from_errno("strdup");
7561 trim_logmsg(logmsg, 42);
7563 err = get_short_logmsg(&logmsg, 42, commit);
7568 switch (hle->cmd->code) {
7569 case GOT_HISTEDIT_PICK:
7570 case GOT_HISTEDIT_EDIT:
7571 printf("%s -> %s: %s\n", old_id_str,
7572 new_id_str ? new_id_str : "no-op change", logmsg);
7574 case GOT_HISTEDIT_DROP:
7575 case GOT_HISTEDIT_FOLD:
7576 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7588 static const struct got_error *
7589 histedit_commit(struct got_pathlist_head *merged_paths,
7590 struct got_worktree *worktree, struct got_fileindex *fileindex,
7591 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7592 struct got_repository *repo)
7594 const struct got_error *err;
7595 struct got_commit_object *commit;
7596 struct got_object_id *new_commit_id;
7598 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7599 && hle->logmsg == NULL) {
7600 err = histedit_edit_logmsg(hle, repo);
7605 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7609 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7610 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7613 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7615 err = show_histedit_progress(commit, hle, NULL);
7617 err = show_histedit_progress(commit, hle, new_commit_id);
7618 free(new_commit_id);
7621 got_object_commit_close(commit);
7625 static const struct got_error *
7626 histedit_skip_commit(struct got_histedit_list_entry *hle,
7627 struct got_worktree *worktree, struct got_repository *repo)
7629 const struct got_error *error;
7630 struct got_commit_object *commit;
7632 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7637 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7641 error = show_histedit_progress(commit, hle, NULL);
7642 got_object_commit_close(commit);
7646 static const struct got_error *
7647 check_local_changes(void *arg, unsigned char status,
7648 unsigned char staged_status, const char *path,
7649 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7650 struct got_object_id *commit_id, int dirfd, const char *de_name)
7652 int *have_local_changes = arg;
7655 case GOT_STATUS_ADD:
7656 case GOT_STATUS_DELETE:
7657 case GOT_STATUS_MODIFY:
7658 case GOT_STATUS_CONFLICT:
7659 *have_local_changes = 1;
7660 return got_error(GOT_ERR_CANCELLED);
7665 switch (staged_status) {
7666 case GOT_STATUS_ADD:
7667 case GOT_STATUS_DELETE:
7668 case GOT_STATUS_MODIFY:
7669 *have_local_changes = 1;
7670 return got_error(GOT_ERR_CANCELLED);
7678 static const struct got_error *
7679 cmd_histedit(int argc, char *argv[])
7681 const struct got_error *error = NULL;
7682 struct got_worktree *worktree = NULL;
7683 struct got_fileindex *fileindex = NULL;
7684 struct got_repository *repo = NULL;
7686 struct got_reference *branch = NULL;
7687 struct got_reference *tmp_branch = NULL;
7688 struct got_object_id *resume_commit_id = NULL;
7689 struct got_object_id *base_commit_id = NULL;
7690 struct got_object_id *head_commit_id = NULL;
7691 struct got_commit_object *commit = NULL;
7692 int ch, rebase_in_progress = 0, did_something;
7693 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7694 int edit_logmsg_only = 0;
7695 const char *edit_script_path = NULL;
7696 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7697 struct got_object_id_queue commits;
7698 struct got_pathlist_head merged_paths;
7699 const struct got_object_id_queue *parent_ids;
7700 struct got_object_qid *pid;
7701 struct got_histedit_list histedit_cmds;
7702 struct got_histedit_list_entry *hle;
7704 SIMPLEQ_INIT(&commits);
7705 TAILQ_INIT(&histedit_cmds);
7706 TAILQ_INIT(&merged_paths);
7708 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7717 edit_script_path = optarg;
7720 edit_logmsg_only = 1;
7732 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7733 "unveil", NULL) == -1)
7736 if (abort_edit && continue_edit)
7737 errx(1, "histedit's -a and -c options are mutually exclusive");
7738 if (edit_script_path && edit_logmsg_only)
7739 errx(1, "histedit's -F and -m options are mutually exclusive");
7740 if (abort_edit && edit_logmsg_only)
7741 errx(1, "histedit's -a and -m options are mutually exclusive");
7742 if (continue_edit && edit_logmsg_only)
7743 errx(1, "histedit's -c and -m options are mutually exclusive");
7748 * This command cannot apply unveil(2) in all cases because the
7749 * user may choose to run an editor to edit the histedit script
7750 * and to edit individual commit log messages.
7751 * unveil(2) traverses exec(2); if an editor is used we have to
7752 * apply unveil after edit script and log messages have been written.
7753 * XXX TODO: Make use of unveil(2) where possible.
7756 cwd = getcwd(NULL, 0);
7758 error = got_error_from_errno("getcwd");
7761 error = got_worktree_open(&worktree, cwd);
7765 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7770 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7773 if (rebase_in_progress) {
7774 error = got_error(GOT_ERR_REBASING);
7778 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7782 if (edit_in_progress && edit_logmsg_only) {
7783 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7784 "histedit operation is in progress in this "
7785 "work tree and must be continued or aborted "
7786 "before the -m option can be used");
7790 if (edit_in_progress && abort_edit) {
7791 error = got_worktree_histedit_continue(&resume_commit_id,
7792 &tmp_branch, &branch, &base_commit_id, &fileindex,
7796 printf("Switching work tree to %s\n",
7797 got_ref_get_symref_target(branch));
7798 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7799 branch, base_commit_id, update_progress, &did_something);
7802 printf("Histedit of %s aborted\n",
7803 got_ref_get_symref_target(branch));
7804 goto done; /* nothing else to do */
7805 } else if (abort_edit) {
7806 error = got_error(GOT_ERR_NOT_HISTEDIT);
7810 if (continue_edit) {
7813 if (!edit_in_progress) {
7814 error = got_error(GOT_ERR_NOT_HISTEDIT);
7818 error = got_worktree_get_histedit_script_path(&path, worktree);
7822 error = histedit_load_list(&histedit_cmds, path, repo);
7827 error = got_worktree_histedit_continue(&resume_commit_id,
7828 &tmp_branch, &branch, &base_commit_id, &fileindex,
7833 error = got_ref_resolve(&head_commit_id, repo, branch);
7837 error = got_object_open_as_commit(&commit, repo,
7841 parent_ids = got_object_commit_get_parent_ids(commit);
7842 pid = SIMPLEQ_FIRST(parent_ids);
7844 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7847 error = collect_commits(&commits, head_commit_id, pid->id,
7848 base_commit_id, got_worktree_get_path_prefix(worktree),
7849 GOT_ERR_HISTEDIT_PATH, repo);
7850 got_object_commit_close(commit);
7855 if (edit_in_progress) {
7856 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7860 error = got_ref_open(&branch, repo,
7861 got_worktree_get_head_ref_name(worktree), 0);
7865 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7866 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7867 "will not edit commit history of a branch outside "
7868 "the \"refs/heads/\" reference namespace");
7872 error = got_ref_resolve(&head_commit_id, repo, branch);
7873 got_ref_close(branch);
7878 error = got_object_open_as_commit(&commit, repo,
7882 parent_ids = got_object_commit_get_parent_ids(commit);
7883 pid = SIMPLEQ_FIRST(parent_ids);
7885 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7888 error = collect_commits(&commits, head_commit_id, pid->id,
7889 got_worktree_get_base_commit_id(worktree),
7890 got_worktree_get_path_prefix(worktree),
7891 GOT_ERR_HISTEDIT_PATH, repo);
7892 got_object_commit_close(commit);
7897 if (SIMPLEQ_EMPTY(&commits)) {
7898 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7902 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7903 &base_commit_id, &fileindex, worktree, repo);
7907 if (edit_script_path) {
7908 error = histedit_load_list(&histedit_cmds,
7909 edit_script_path, repo);
7911 got_worktree_histedit_abort(worktree, fileindex,
7912 repo, branch, base_commit_id,
7913 update_progress, &did_something);
7917 const char *branch_name;
7918 branch_name = got_ref_get_symref_target(branch);
7919 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7921 error = histedit_edit_script(&histedit_cmds, &commits,
7922 branch_name, edit_logmsg_only, repo);
7924 got_worktree_histedit_abort(worktree, fileindex,
7925 repo, branch, base_commit_id,
7926 update_progress, &did_something);
7932 error = histedit_save_list(&histedit_cmds, worktree,
7935 got_worktree_histedit_abort(worktree, fileindex,
7936 repo, branch, base_commit_id,
7937 update_progress, &did_something);
7943 error = histedit_check_script(&histedit_cmds, &commits, repo);
7947 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7948 if (resume_commit_id) {
7949 if (got_object_id_cmp(hle->commit_id,
7950 resume_commit_id) != 0)
7953 resume_commit_id = NULL;
7954 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7955 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7956 error = histedit_skip_commit(hle, worktree,
7961 struct got_pathlist_head paths;
7962 int have_changes = 0;
7965 error = got_pathlist_append(&paths, "", NULL);
7968 error = got_worktree_status(worktree, &paths,
7969 repo, check_local_changes, &have_changes,
7970 check_cancelled, NULL);
7971 got_pathlist_free(&paths);
7973 if (error->code != GOT_ERR_CANCELLED)
7975 if (sigint_received || sigpipe_received)
7979 error = histedit_commit(NULL, worktree,
7980 fileindex, tmp_branch, hle, repo);
7984 error = got_object_open_as_commit(
7985 &commit, repo, hle->commit_id);
7988 error = show_histedit_progress(commit,
7990 got_object_commit_close(commit);
7999 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8000 error = histedit_skip_commit(hle, worktree, repo);
8006 error = got_object_open_as_commit(&commit, repo,
8010 parent_ids = got_object_commit_get_parent_ids(commit);
8011 pid = SIMPLEQ_FIRST(parent_ids);
8013 error = got_worktree_histedit_merge_files(&merged_paths,
8014 worktree, fileindex, pid->id, hle->commit_id, repo,
8015 rebase_progress, &rebase_status, check_cancelled, NULL);
8018 got_object_commit_close(commit);
8021 if (rebase_status == GOT_STATUS_CONFLICT) {
8022 error = show_rebase_merge_conflict(hle->commit_id,
8026 got_worktree_rebase_pathlist_free(&merged_paths);
8030 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8032 error = got_object_id_str(&id_str, hle->commit_id);
8035 printf("Stopping histedit for amending commit %s\n",
8038 got_worktree_rebase_pathlist_free(&merged_paths);
8039 error = got_worktree_histedit_postpone(worktree,
8044 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8045 error = histedit_skip_commit(hle, worktree, repo);
8051 error = histedit_commit(&merged_paths, worktree, fileindex,
8052 tmp_branch, hle, repo);
8053 got_worktree_rebase_pathlist_free(&merged_paths);
8058 if (rebase_status == GOT_STATUS_CONFLICT) {
8059 error = got_worktree_histedit_postpone(worktree, fileindex);
8062 error = got_error_msg(GOT_ERR_CONFLICTS,
8063 "conflicts must be resolved before histedit can continue");
8065 error = histedit_complete(worktree, fileindex, tmp_branch,
8068 got_object_id_queue_free(&commits);
8069 histedit_free_list(&histedit_cmds);
8070 free(head_commit_id);
8071 free(base_commit_id);
8072 free(resume_commit_id);
8074 got_object_commit_close(commit);
8076 got_ref_close(branch);
8078 got_ref_close(tmp_branch);
8080 got_worktree_close(worktree);
8082 got_repo_close(repo);
8087 usage_integrate(void)
8089 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8093 static const struct got_error *
8094 cmd_integrate(int argc, char *argv[])
8096 const struct got_error *error = NULL;
8097 struct got_repository *repo = NULL;
8098 struct got_worktree *worktree = NULL;
8099 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8100 const char *branch_arg = NULL;
8101 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8102 struct got_fileindex *fileindex = NULL;
8103 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8104 int ch, did_something = 0;
8106 while ((ch = getopt(argc, argv, "")) != -1) {
8119 branch_arg = argv[0];
8121 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8122 "unveil", NULL) == -1)
8125 cwd = getcwd(NULL, 0);
8127 error = got_error_from_errno("getcwd");
8131 error = got_worktree_open(&worktree, cwd);
8135 error = check_rebase_or_histedit_in_progress(worktree);
8139 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8144 error = apply_unveil(got_repo_get_path(repo), 0,
8145 got_worktree_get_root_path(worktree));
8149 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8150 error = got_error_from_errno("asprintf");
8154 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8155 &base_branch_ref, worktree, refname, repo);
8159 refname = strdup(got_ref_get_name(branch_ref));
8160 if (refname == NULL) {
8161 error = got_error_from_errno("strdup");
8162 got_worktree_integrate_abort(worktree, fileindex, repo,
8163 branch_ref, base_branch_ref);
8166 base_refname = strdup(got_ref_get_name(base_branch_ref));
8167 if (base_refname == NULL) {
8168 error = got_error_from_errno("strdup");
8169 got_worktree_integrate_abort(worktree, fileindex, repo,
8170 branch_ref, base_branch_ref);
8174 error = got_ref_resolve(&commit_id, repo, branch_ref);
8178 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8182 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8183 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8184 "specified branch has already been integrated");
8185 got_worktree_integrate_abort(worktree, fileindex, repo,
8186 branch_ref, base_branch_ref);
8190 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8192 if (error->code == GOT_ERR_ANCESTRY)
8193 error = got_error(GOT_ERR_REBASE_REQUIRED);
8194 got_worktree_integrate_abort(worktree, fileindex, repo,
8195 branch_ref, base_branch_ref);
8199 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8200 branch_ref, base_branch_ref, update_progress, &did_something,
8201 check_cancelled, NULL);
8205 printf("Integrated %s into %s\n", refname, base_refname);
8208 got_repo_close(repo);
8210 got_worktree_close(worktree);
8212 free(base_commit_id);
8222 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8223 "[file-path ...]\n",
8228 static const struct got_error *
8229 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8230 const char *path, struct got_object_id *blob_id,
8231 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8232 int dirfd, const char *de_name)
8234 const struct got_error *err = NULL;
8235 char *id_str = NULL;
8237 if (staged_status != GOT_STATUS_ADD &&
8238 staged_status != GOT_STATUS_MODIFY &&
8239 staged_status != GOT_STATUS_DELETE)
8242 if (staged_status == GOT_STATUS_ADD ||
8243 staged_status == GOT_STATUS_MODIFY)
8244 err = got_object_id_str(&id_str, staged_blob_id);
8246 err = got_object_id_str(&id_str, blob_id);
8250 printf("%s %c %s\n", id_str, staged_status, path);
8255 static const struct got_error *
8256 cmd_stage(int argc, char *argv[])
8258 const struct got_error *error = NULL;
8259 struct got_repository *repo = NULL;
8260 struct got_worktree *worktree = NULL;
8262 struct got_pathlist_head paths;
8263 struct got_pathlist_entry *pe;
8264 int ch, list_stage = 0, pflag = 0;
8265 FILE *patch_script_file = NULL;
8266 const char *patch_script_path = NULL;
8267 struct choose_patch_arg cpa;
8271 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8280 patch_script_path = optarg;
8292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8293 "unveil", NULL) == -1)
8296 if (list_stage && (pflag || patch_script_path))
8297 errx(1, "-l option cannot be used with other options");
8298 if (patch_script_path && !pflag)
8299 errx(1, "-F option can only be used together with -p option");
8301 cwd = getcwd(NULL, 0);
8303 error = got_error_from_errno("getcwd");
8307 error = got_worktree_open(&worktree, cwd);
8311 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8316 if (patch_script_path) {
8317 patch_script_file = fopen(patch_script_path, "r");
8318 if (patch_script_file == NULL) {
8319 error = got_error_from_errno2("fopen",
8324 error = apply_unveil(got_repo_get_path(repo), 0,
8325 got_worktree_get_root_path(worktree));
8329 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8334 error = got_worktree_status(worktree, &paths, repo,
8335 print_stage, NULL, check_cancelled, NULL);
8337 cpa.patch_script_file = patch_script_file;
8338 cpa.action = "stage";
8339 error = got_worktree_stage(worktree, &paths,
8340 pflag ? NULL : print_status, NULL,
8341 pflag ? choose_patch : NULL, &cpa, repo);
8344 if (patch_script_file && fclose(patch_script_file) == EOF &&
8346 error = got_error_from_errno2("fclose", patch_script_path);
8348 got_repo_close(repo);
8350 got_worktree_close(worktree);
8351 TAILQ_FOREACH(pe, &paths, entry)
8352 free((char *)pe->path);
8353 got_pathlist_free(&paths);
8361 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8362 "[file-path ...]\n",
8368 static const struct got_error *
8369 cmd_unstage(int argc, char *argv[])
8371 const struct got_error *error = NULL;
8372 struct got_repository *repo = NULL;
8373 struct got_worktree *worktree = NULL;
8375 struct got_pathlist_head paths;
8376 struct got_pathlist_entry *pe;
8377 int ch, did_something = 0, pflag = 0;
8378 FILE *patch_script_file = NULL;
8379 const char *patch_script_path = NULL;
8380 struct choose_patch_arg cpa;
8384 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8390 patch_script_path = optarg;
8402 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8403 "unveil", NULL) == -1)
8406 if (patch_script_path && !pflag)
8407 errx(1, "-F option can only be used together with -p option");
8409 cwd = getcwd(NULL, 0);
8411 error = got_error_from_errno("getcwd");
8415 error = got_worktree_open(&worktree, cwd);
8419 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8424 if (patch_script_path) {
8425 patch_script_file = fopen(patch_script_path, "r");
8426 if (patch_script_file == NULL) {
8427 error = got_error_from_errno2("fopen",
8433 error = apply_unveil(got_repo_get_path(repo), 0,
8434 got_worktree_get_root_path(worktree));
8438 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8442 cpa.patch_script_file = patch_script_file;
8443 cpa.action = "unstage";
8444 error = got_worktree_unstage(worktree, &paths, update_progress,
8445 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8447 if (patch_script_file && fclose(patch_script_file) == EOF &&
8449 error = got_error_from_errno2("fclose", patch_script_path);
8451 got_repo_close(repo);
8453 got_worktree_close(worktree);
8454 TAILQ_FOREACH(pe, &paths, entry)
8455 free((char *)pe->path);
8456 got_pathlist_free(&paths);
8464 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8465 "arg1 [arg2 ...]\n", getprogname());
8469 static const struct got_error *
8470 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8472 const struct got_error *err;
8473 struct got_blob_object *blob;
8475 err = got_object_open_as_blob(&blob, repo, id, 8192);
8479 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8480 got_object_blob_close(blob);
8484 static const struct got_error *
8485 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8487 const struct got_error *err;
8488 struct got_tree_object *tree;
8491 err = got_object_open_as_tree(&tree, repo, id);
8495 nentries = got_object_tree_get_nentries(tree);
8496 for (i = 0; i < nentries; i++) {
8497 struct got_tree_entry *te;
8499 if (sigint_received || sigpipe_received)
8501 te = got_object_tree_get_entry(tree, i);
8502 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8505 fprintf(outfile, "%s %.7o %s\n", id_str,
8506 got_tree_entry_get_mode(te),
8507 got_tree_entry_get_name(te));
8511 got_object_tree_close(tree);
8515 static const struct got_error *
8516 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8518 const struct got_error *err;
8519 struct got_commit_object *commit;
8520 const struct got_object_id_queue *parent_ids;
8521 struct got_object_qid *pid;
8522 char *id_str = NULL;
8523 const char *logmsg = NULL;
8525 err = got_object_open_as_commit(&commit, repo, id);
8529 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8533 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8534 parent_ids = got_object_commit_get_parent_ids(commit);
8535 fprintf(outfile, "numparents %d\n",
8536 got_object_commit_get_nparents(commit));
8537 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8539 err = got_object_id_str(&pid_str, pid->id);
8542 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8545 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8546 got_object_commit_get_author(commit),
8547 got_object_commit_get_author_time(commit));
8549 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8550 got_object_commit_get_author(commit),
8551 got_object_commit_get_committer_time(commit));
8553 logmsg = got_object_commit_get_logmsg_raw(commit);
8554 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8555 fprintf(outfile, "%s", logmsg);
8558 got_object_commit_close(commit);
8562 static const struct got_error *
8563 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8565 const struct got_error *err;
8566 struct got_tag_object *tag;
8567 char *id_str = NULL;
8568 const char *tagmsg = NULL;
8570 err = got_object_open_as_tag(&tag, repo, id);
8574 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8578 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8580 switch (got_object_tag_get_object_type(tag)) {
8581 case GOT_OBJ_TYPE_BLOB:
8582 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8583 GOT_OBJ_LABEL_BLOB);
8585 case GOT_OBJ_TYPE_TREE:
8586 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8587 GOT_OBJ_LABEL_TREE);
8589 case GOT_OBJ_TYPE_COMMIT:
8590 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8591 GOT_OBJ_LABEL_COMMIT);
8593 case GOT_OBJ_TYPE_TAG:
8594 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8601 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8602 got_object_tag_get_name(tag));
8604 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8605 got_object_tag_get_tagger(tag),
8606 got_object_tag_get_tagger_time(tag));
8608 tagmsg = got_object_tag_get_message(tag);
8609 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8610 fprintf(outfile, "%s", tagmsg);
8613 got_object_tag_close(tag);
8617 static const struct got_error *
8618 cmd_cat(int argc, char *argv[])
8620 const struct got_error *error;
8621 struct got_repository *repo = NULL;
8622 struct got_worktree *worktree = NULL;
8623 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8624 const char *commit_id_str = NULL;
8625 struct got_object_id *id = NULL, *commit_id = NULL;
8626 int ch, obj_type, i, force_path = 0;
8629 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8634 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8637 commit_id_str = optarg;
8640 repo_path = realpath(optarg, NULL);
8641 if (repo_path == NULL)
8642 return got_error_from_errno2("realpath",
8644 got_path_strip_trailing_slashes(repo_path);
8658 cwd = getcwd(NULL, 0);
8660 error = got_error_from_errno("getcwd");
8663 error = got_worktree_open(&worktree, cwd);
8664 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8667 if (repo_path == NULL) {
8669 got_worktree_get_repo_path(worktree));
8670 if (repo_path == NULL) {
8671 error = got_error_from_errno("strdup");
8677 if (repo_path == NULL) {
8678 repo_path = getcwd(NULL, 0);
8679 if (repo_path == NULL)
8680 return got_error_from_errno("getcwd");
8683 error = got_repo_open(&repo, repo_path, NULL);
8688 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8692 if (commit_id_str == NULL)
8693 commit_id_str = GOT_REF_HEAD;
8694 error = got_repo_match_object_id(&commit_id, NULL,
8695 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8699 for (i = 0; i < argc; i++) {
8701 error = got_object_id_by_path(&id, repo, commit_id,
8706 error = got_repo_match_object_id(&id, &label, argv[i],
8707 GOT_OBJ_TYPE_ANY, 0, repo);
8709 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8710 error->code != GOT_ERR_NOT_REF)
8712 error = got_object_id_by_path(&id, repo,
8713 commit_id, argv[i]);
8719 error = got_object_get_type(&obj_type, repo, id);
8724 case GOT_OBJ_TYPE_BLOB:
8725 error = cat_blob(id, repo, stdout);
8727 case GOT_OBJ_TYPE_TREE:
8728 error = cat_tree(id, repo, stdout);
8730 case GOT_OBJ_TYPE_COMMIT:
8731 error = cat_commit(id, repo, stdout);
8733 case GOT_OBJ_TYPE_TAG:
8734 error = cat_tag(id, repo, stdout);
8737 error = got_error(GOT_ERR_OBJ_TYPE);
8752 got_worktree_close(worktree);
8754 const struct got_error *repo_error;
8755 repo_error = got_repo_close(repo);