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_head_ref(struct got_reference *target_ref, int verbosity,
889 struct got_repository *repo)
891 const struct got_error *err;
892 struct got_reference *head_symref;
894 err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, 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);
1250 /* Set the HEAD reference if the server provided one. */
1251 TAILQ_FOREACH(pe, &symrefs, entry) {
1252 struct got_reference *target_ref;
1253 const char *refname = pe->path;
1254 const char *target = pe->data;
1256 if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 error = got_ref_open(&target_ref, repo, target, 0);
1261 if (error->code == GOT_ERR_NOT_REF) {
1268 error = create_head_ref(target_ref, verbosity, repo);
1269 got_ref_close(target_ref);
1275 * We failed to set the HEAD reference. If we asked for
1276 * a set of wanted branches use the first of one of those
1277 * which could be fetched instead.
1279 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1280 const char *target = pe->path;
1281 struct got_reference *target_ref;
1283 error = got_ref_open(&target_ref, repo, target, 0);
1285 if (error->code == GOT_ERR_NOT_REF) {
1292 error = create_head_ref(target_ref, verbosity, repo);
1293 got_ref_close(target_ref);
1300 /* Create a config file git-fetch(1) can understand. */
1301 gitconfig_path = got_repo_get_path_gitconfig(repo);
1302 if (gitconfig_path == NULL) {
1303 error = got_error_from_errno("got_repo_get_path_gitconfig");
1306 gitconfig_file = fopen(gitconfig_path, "a");
1307 if (gitconfig_file == NULL) {
1308 error = got_error_from_errno2("fopen", gitconfig_path);
1311 if (mirror_references) {
1312 if (asprintf(&gitconfig,
1315 "\tfetch = +refs/*:refs/*\n"
1316 "\tmirror = true\n",
1317 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1318 error = got_error_from_errno("asprintf");
1321 } else if (fetch_all_branches) {
1322 if (asprintf(&gitconfig,
1325 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1326 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1327 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1328 error = got_error_from_errno("asprintf");
1332 const char *branchname;
1335 * If the server specified a default branch, use just that one.
1336 * Otherwise fall back to fetching all branches on next fetch.
1339 branchname = got_ref_get_symref_target(head_symref);
1340 if (strncmp(branchname, "refs/heads/", 11) == 0)
1343 branchname = "*"; /* fall back to all branches */
1344 if (asprintf(&gitconfig,
1347 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1348 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1349 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1350 branchname) == -1) {
1351 error = got_error_from_errno("asprintf");
1355 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1356 if (n != strlen(gitconfig)) {
1357 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1362 printf("Created %s repository '%s'\n",
1363 mirror_references ? "mirrored" : "cloned", repo_path);
1366 if (kill(fetchpid, SIGTERM) == -1)
1367 error = got_error_from_errno("kill");
1368 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1369 error = got_error_from_errno("waitpid");
1371 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1372 error = got_error_from_errno("close");
1373 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1374 error = got_error_from_errno("fclose");
1376 got_repo_close(repo);
1378 got_ref_close(head_symref);
1379 TAILQ_FOREACH(pe, &refs, entry) {
1380 free((void *)pe->path);
1383 got_pathlist_free(&refs);
1384 TAILQ_FOREACH(pe, &symrefs, entry) {
1385 free((void *)pe->path);
1388 got_pathlist_free(&symrefs);
1389 got_pathlist_free(&wanted_branches);
1390 got_pathlist_free(&wanted_refs);
1397 free(default_destdir);
1398 free(gitconfig_path);
1403 static const struct got_error *
1404 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1405 int replace_tags, int verbosity, struct got_repository *repo)
1407 const struct got_error *err = NULL;
1408 char *new_id_str = NULL;
1409 struct got_object_id *old_id = NULL;
1411 err = got_object_id_str(&new_id_str, new_id);
1415 if (!replace_tags &&
1416 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1417 err = got_ref_resolve(&old_id, repo, ref);
1420 if (got_object_id_cmp(old_id, new_id) == 0)
1422 if (verbosity >= 0) {
1423 printf("Rejecting update of existing tag %s: %s\n",
1424 got_ref_get_name(ref), new_id_str);
1429 if (got_ref_is_symbolic(ref)) {
1430 if (verbosity >= 0) {
1431 printf("Replacing reference %s: %s\n",
1432 got_ref_get_name(ref),
1433 got_ref_get_symref_target(ref));
1435 err = got_ref_change_symref_to_ref(ref, new_id);
1438 err = got_ref_write(ref, repo);
1442 err = got_ref_resolve(&old_id, repo, ref);
1445 if (got_object_id_cmp(old_id, new_id) == 0)
1448 err = got_ref_change_ref(ref, new_id);
1451 err = got_ref_write(ref, repo);
1457 printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1468 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1469 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1470 "[remote-repository-name]\n",
1475 static const struct got_error *
1476 delete_missing_refs(struct got_pathlist_head *their_refs,
1477 int verbosity, struct got_repository *repo)
1479 const struct got_error *err = NULL;
1480 struct got_reflist_head my_refs;
1481 struct got_reflist_entry *re;
1482 struct got_pathlist_entry *pe;
1483 struct got_object_id *id;
1486 SIMPLEQ_INIT(&my_refs);
1488 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1492 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1493 const char *refname = got_ref_get_name(re->ref);
1495 if (strncmp(refname, "refs/heads/", 11) != 0 &&
1496 strncmp(refname, "refs/tags/", 10) != 0)
1499 TAILQ_FOREACH(pe, their_refs, entry) {
1500 if (strcmp(refname, pe->path) == 0)
1506 err = got_ref_resolve(&id, repo, re->ref);
1509 err = got_object_id_str(&id_str, id);
1515 err = got_ref_delete(re->ref, repo);
1518 if (verbosity >= 0) {
1519 printf("Deleted reference %s: %s\n",
1520 got_ref_get_name(re->ref), id_str);
1527 static const struct got_error *
1528 update_wanted_ref(const char *refname, struct got_object_id *id,
1529 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1531 const struct got_error *err, *unlock_err;
1532 char *remote_refname;
1533 struct got_reference *ref;
1535 if (strncmp("refs/", refname, 5) == 0)
1538 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1539 remote_repo_name, refname) == -1)
1540 return got_error_from_errno("asprintf");
1542 err = got_ref_open(&ref, repo, remote_refname, 1);
1544 if (err->code != GOT_ERR_NOT_REF)
1546 err = create_ref(remote_refname, id, verbosity, repo);
1548 err = update_ref(ref, id, 0, verbosity, repo);
1549 unlock_err = got_ref_unlock(ref);
1550 if (unlock_err && err == NULL)
1555 free(remote_refname);
1559 static const struct got_error *
1560 cmd_fetch(int argc, char *argv[])
1562 const struct got_error *error = NULL, *unlock_err;
1563 char *cwd = NULL, *repo_path = NULL;
1564 const char *remote_name;
1565 char *proto = NULL, *host = NULL, *port = NULL;
1566 char *repo_name = NULL, *server_path = NULL;
1567 struct got_remote_repo *remotes, *remote = NULL;
1569 char *id_str = NULL;
1570 struct got_repository *repo = NULL;
1571 struct got_worktree *worktree = NULL;
1572 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1573 struct got_pathlist_entry *pe;
1574 struct got_object_id *pack_hash = NULL;
1575 int i, ch, fetchfd = -1, fetchstatus;
1576 pid_t fetchpid = -1;
1577 struct got_fetch_progress_arg fpa;
1578 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1579 int delete_refs = 0, replace_tags = 0;
1582 TAILQ_INIT(&symrefs);
1583 TAILQ_INIT(&wanted_branches);
1584 TAILQ_INIT(&wanted_refs);
1586 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1589 fetch_all_branches = 1;
1592 error = got_pathlist_append(&wanted_branches,
1604 repo_path = realpath(optarg, NULL);
1605 if (repo_path == NULL)
1606 return got_error_from_errno2("realpath",
1608 got_path_strip_trailing_slashes(repo_path);
1616 else if (verbosity < 3)
1623 error = got_pathlist_append(&wanted_refs,
1636 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1637 errx(1, "-a and -b options are mutually exclusive");
1638 if (list_refs_only) {
1639 if (!TAILQ_EMPTY(&wanted_branches))
1640 errx(1, "-l and -b options are mutually exclusive");
1641 if (fetch_all_branches)
1642 errx(1, "-l and -a options are mutually exclusive");
1644 errx(1, "-l and -d options are mutually exclusive");
1645 if (verbosity == -1)
1646 errx(1, "-l and -q options are mutually exclusive");
1650 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1652 remote_name = argv[0];
1656 cwd = getcwd(NULL, 0);
1658 error = got_error_from_errno("getcwd");
1662 if (repo_path == NULL) {
1663 error = got_worktree_open(&worktree, cwd);
1664 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1670 strdup(got_worktree_get_repo_path(worktree));
1671 if (repo_path == NULL)
1672 error = got_error_from_errno("strdup");
1676 repo_path = strdup(cwd);
1677 if (repo_path == NULL) {
1678 error = got_error_from_errno("strdup");
1684 error = got_repo_open(&repo, repo_path, NULL);
1688 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1689 for (i = 0; i < nremotes; i++) {
1690 remote = &remotes[i];
1691 if (strcmp(remote->name, remote_name) == 0)
1694 if (i == nremotes) {
1695 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1699 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1700 &repo_name, remote->url);
1704 if (strcmp(proto, "git") == 0) {
1706 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1707 "sendfd dns inet unveil", NULL) == -1)
1710 } else if (strcmp(proto, "git+ssh") == 0 ||
1711 strcmp(proto, "ssh") == 0) {
1713 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1714 "sendfd unveil", NULL) == -1)
1717 } else if (strcmp(proto, "http") == 0 ||
1718 strcmp(proto, "git+http") == 0) {
1719 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1722 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1726 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1727 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1728 error = got_error_from_errno2("unveil",
1729 GOT_FETCH_PATH_SSH);
1733 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1737 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1738 server_path, verbosity);
1743 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1744 port ? ":" : "", port ? port : "");
1746 fpa.last_scaled_size[0] = '\0';
1747 fpa.last_p_indexed = -1;
1748 fpa.last_p_resolved = -1;
1749 fpa.verbosity = verbosity;
1750 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1751 remote->mirror_references, fetch_all_branches, &wanted_branches,
1752 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1753 fetch_progress, &fpa);
1757 if (list_refs_only) {
1758 error = list_remote_refs(&symrefs, &refs);
1762 if (pack_hash == NULL) {
1764 printf("Already up-to-date\n");
1766 error = delete_missing_refs(&refs, verbosity, repo);
1770 if (verbosity >= 0) {
1771 error = got_object_id_str(&id_str, pack_hash);
1774 printf("\nFetched %s.pack\n", id_str);
1779 /* Update references provided with the pack file. */
1780 TAILQ_FOREACH(pe, &refs, entry) {
1781 const char *refname = pe->path;
1782 struct got_object_id *id = pe->data;
1783 struct got_reference *ref;
1784 char *remote_refname;
1786 if (is_wanted_ref(&wanted_refs, refname) &&
1787 !remote->mirror_references) {
1788 error = update_wanted_ref(refname, id,
1789 remote->name, verbosity, repo);
1795 if (remote->mirror_references ||
1796 strncmp("refs/tags/", refname, 10) == 0) {
1797 error = got_ref_open(&ref, repo, refname, 1);
1799 if (error->code != GOT_ERR_NOT_REF)
1801 error = create_ref(refname, id, verbosity,
1806 error = update_ref(ref, id, replace_tags,
1808 unlock_err = got_ref_unlock(ref);
1809 if (unlock_err && error == NULL)
1815 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1816 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1817 remote_name, refname + 11) == -1) {
1818 error = got_error_from_errno("asprintf");
1822 error = got_ref_open(&ref, repo, remote_refname, 1);
1824 if (error->code != GOT_ERR_NOT_REF)
1826 error = create_ref(remote_refname, id,
1831 error = update_ref(ref, id, replace_tags,
1833 unlock_err = got_ref_unlock(ref);
1834 if (unlock_err && error == NULL)
1841 /* Also create a local branch if none exists yet. */
1842 error = got_ref_open(&ref, repo, refname, 1);
1844 if (error->code != GOT_ERR_NOT_REF)
1846 error = create_ref(refname, id, verbosity,
1851 unlock_err = got_ref_unlock(ref);
1852 if (unlock_err && error == NULL)
1859 error = delete_missing_refs(&refs, verbosity, repo);
1862 if (kill(fetchpid, SIGTERM) == -1)
1863 error = got_error_from_errno("kill");
1864 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1865 error = got_error_from_errno("waitpid");
1867 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1868 error = got_error_from_errno("close");
1870 got_repo_close(repo);
1872 got_worktree_close(worktree);
1873 TAILQ_FOREACH(pe, &refs, entry) {
1874 free((void *)pe->path);
1877 got_pathlist_free(&refs);
1878 TAILQ_FOREACH(pe, &symrefs, entry) {
1879 free((void *)pe->path);
1882 got_pathlist_free(&symrefs);
1883 got_pathlist_free(&wanted_branches);
1884 got_pathlist_free(&wanted_refs);
1899 usage_checkout(void)
1901 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1902 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1907 show_worktree_base_ref_warning(void)
1909 fprintf(stderr, "%s: warning: could not create a reference "
1910 "to the work tree's base commit; the commit could be "
1911 "garbage-collected by Git; making the repository "
1912 "writable and running 'got update' will prevent this\n",
1916 struct got_checkout_progress_arg {
1917 const char *worktree_path;
1918 int had_base_commit_ref_error;
1921 static const struct got_error *
1922 checkout_progress(void *arg, unsigned char status, const char *path)
1924 struct got_checkout_progress_arg *a = arg;
1926 /* Base commit bump happens silently. */
1927 if (status == GOT_STATUS_BUMP_BASE)
1930 if (status == GOT_STATUS_BASE_REF_ERR) {
1931 a->had_base_commit_ref_error = 1;
1935 while (path[0] == '/')
1938 printf("%c %s/%s\n", status, a->worktree_path, path);
1942 static const struct got_error *
1943 check_cancelled(void *arg)
1945 if (sigint_received || sigpipe_received)
1946 return got_error(GOT_ERR_CANCELLED);
1950 static const struct got_error *
1951 check_linear_ancestry(struct got_object_id *commit_id,
1952 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1953 struct got_repository *repo)
1955 const struct got_error *err = NULL;
1956 struct got_object_id *yca_id;
1958 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1959 commit_id, base_commit_id, repo, check_cancelled, NULL);
1964 return got_error(GOT_ERR_ANCESTRY);
1967 * Require a straight line of history between the target commit
1968 * and the work tree's base commit.
1970 * Non-linear situations such as this require a rebase:
1972 * (commit) D F (base_commit)
1980 * 'got update' only handles linear cases:
1981 * Update forwards in time: A (base/yca) - B - C - D (commit)
1982 * Update backwards in time: D (base) - C - B - A (commit/yca)
1984 if (allow_forwards_in_time_only) {
1985 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1986 return got_error(GOT_ERR_ANCESTRY);
1987 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1988 got_object_id_cmp(base_commit_id, yca_id) != 0)
1989 return got_error(GOT_ERR_ANCESTRY);
1995 static const struct got_error *
1996 check_same_branch(struct got_object_id *commit_id,
1997 struct got_reference *head_ref, struct got_object_id *yca_id,
1998 struct got_repository *repo)
2000 const struct got_error *err = NULL;
2001 struct got_commit_graph *graph = NULL;
2002 struct got_object_id *head_commit_id = NULL;
2003 int is_same_branch = 0;
2005 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2009 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2013 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2018 err = got_commit_graph_open(&graph, "/", 1);
2022 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2023 check_cancelled, NULL);
2028 struct got_object_id *id;
2029 err = got_commit_graph_iter_next(&id, graph, repo,
2030 check_cancelled, NULL);
2032 if (err->code == GOT_ERR_ITER_COMPLETED)
2038 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2040 if (got_object_id_cmp(id, commit_id) == 0) {
2048 got_commit_graph_close(graph);
2049 free(head_commit_id);
2050 if (!err && !is_same_branch)
2051 err = got_error(GOT_ERR_ANCESTRY);
2055 static const struct got_error *
2056 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2058 static char msg[512];
2059 const char *branch_name;
2061 if (got_ref_is_symbolic(ref))
2062 branch_name = got_ref_get_symref_target(ref);
2064 branch_name = got_ref_get_name(ref);
2066 if (strncmp("refs/heads/", branch_name, 11) == 0)
2069 snprintf(msg, sizeof(msg),
2070 "target commit is not contained in branch '%s'; "
2071 "the branch to use must be specified with -b; "
2072 "if necessary a new branch can be created for "
2073 "this commit with 'got branch -c %s BRANCH_NAME'",
2074 branch_name, commit_id_str);
2076 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2079 static const struct got_error *
2080 cmd_checkout(int argc, char *argv[])
2082 const struct got_error *error = NULL;
2083 struct got_repository *repo = NULL;
2084 struct got_reference *head_ref = NULL;
2085 struct got_worktree *worktree = NULL;
2086 char *repo_path = NULL;
2087 char *worktree_path = NULL;
2088 const char *path_prefix = "";
2089 const char *branch_name = GOT_REF_HEAD;
2090 char *commit_id_str = NULL;
2091 int ch, same_path_prefix, allow_nonempty = 0;
2092 struct got_pathlist_head paths;
2093 struct got_checkout_progress_arg cpa;
2097 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2100 branch_name = optarg;
2103 commit_id_str = strdup(optarg);
2104 if (commit_id_str == NULL)
2105 return got_error_from_errno("strdup");
2111 path_prefix = optarg;
2123 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2124 "unveil", NULL) == -1)
2128 char *cwd, *base, *dotgit;
2129 repo_path = realpath(argv[0], NULL);
2130 if (repo_path == NULL)
2131 return got_error_from_errno2("realpath", argv[0]);
2132 cwd = getcwd(NULL, 0);
2134 error = got_error_from_errno("getcwd");
2137 if (path_prefix[0]) {
2138 base = basename(path_prefix);
2140 error = got_error_from_errno2("basename",
2145 base = basename(repo_path);
2147 error = got_error_from_errno2("basename",
2152 dotgit = strstr(base, ".git");
2155 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2156 error = got_error_from_errno("asprintf");
2161 } else if (argc == 2) {
2162 repo_path = realpath(argv[0], NULL);
2163 if (repo_path == NULL) {
2164 error = got_error_from_errno2("realpath", argv[0]);
2167 worktree_path = realpath(argv[1], NULL);
2168 if (worktree_path == NULL) {
2169 if (errno != ENOENT) {
2170 error = got_error_from_errno2("realpath",
2174 worktree_path = strdup(argv[1]);
2175 if (worktree_path == NULL) {
2176 error = got_error_from_errno("strdup");
2183 got_path_strip_trailing_slashes(repo_path);
2184 got_path_strip_trailing_slashes(worktree_path);
2186 error = got_repo_open(&repo, repo_path, NULL);
2190 /* Pre-create work tree path for unveil(2) */
2191 error = got_path_mkdir(worktree_path);
2193 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2194 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2196 if (!allow_nonempty &&
2197 !got_path_dir_is_empty(worktree_path)) {
2198 error = got_error_path(worktree_path,
2199 GOT_ERR_DIR_NOT_EMPTY);
2204 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2208 error = got_ref_open(&head_ref, repo, branch_name, 0);
2212 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2213 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2216 error = got_worktree_open(&worktree, worktree_path);
2220 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2224 if (!same_path_prefix) {
2225 error = got_error(GOT_ERR_PATH_PREFIX);
2229 if (commit_id_str) {
2230 struct got_object_id *commit_id;
2231 error = got_repo_match_object_id(&commit_id, NULL,
2232 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2235 error = check_linear_ancestry(commit_id,
2236 got_worktree_get_base_commit_id(worktree), 0, repo);
2237 if (error != NULL) {
2239 if (error->code == GOT_ERR_ANCESTRY) {
2240 error = checkout_ancestry_error(
2241 head_ref, commit_id_str);
2245 error = check_same_branch(commit_id, head_ref, NULL, repo);
2247 if (error->code == GOT_ERR_ANCESTRY) {
2248 error = checkout_ancestry_error(
2249 head_ref, commit_id_str);
2253 error = got_worktree_set_base_commit_id(worktree, repo,
2260 error = got_pathlist_append(&paths, "", NULL);
2263 cpa.worktree_path = worktree_path;
2264 cpa.had_base_commit_ref_error = 0;
2265 error = got_worktree_checkout_files(worktree, &paths, repo,
2266 checkout_progress, &cpa, check_cancelled, NULL);
2270 printf("Now shut up and hack\n");
2271 if (cpa.had_base_commit_ref_error)
2272 show_worktree_base_ref_warning();
2274 got_pathlist_free(&paths);
2275 free(commit_id_str);
2277 free(worktree_path);
2284 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2289 static const struct got_error *
2290 update_progress(void *arg, unsigned char status, const char *path)
2292 int *did_something = arg;
2294 if (status == GOT_STATUS_EXISTS ||
2295 status == GOT_STATUS_BASE_REF_ERR)
2300 /* Base commit bump happens silently. */
2301 if (status == GOT_STATUS_BUMP_BASE)
2304 while (path[0] == '/')
2306 printf("%c %s\n", status, path);
2310 static const struct got_error *
2311 switch_head_ref(struct got_reference *head_ref,
2312 struct got_object_id *commit_id, struct got_worktree *worktree,
2313 struct got_repository *repo)
2315 const struct got_error *err = NULL;
2317 int ref_has_moved = 0;
2319 /* Trivial case: switching between two different references. */
2320 if (strcmp(got_ref_get_name(head_ref),
2321 got_worktree_get_head_ref_name(worktree)) != 0) {
2322 printf("Switching work tree from %s to %s\n",
2323 got_worktree_get_head_ref_name(worktree),
2324 got_ref_get_name(head_ref));
2325 return got_worktree_set_head_ref(worktree, head_ref);
2328 err = check_linear_ancestry(commit_id,
2329 got_worktree_get_base_commit_id(worktree), 0, repo);
2331 if (err->code != GOT_ERR_ANCESTRY)
2338 /* Switching to a rebased branch with the same reference name. */
2339 err = got_object_id_str(&base_id_str,
2340 got_worktree_get_base_commit_id(worktree));
2343 printf("Reference %s now points at a different branch\n",
2344 got_worktree_get_head_ref_name(worktree));
2345 printf("Switching work tree from %s to %s\n", base_id_str,
2346 got_worktree_get_head_ref_name(worktree));
2350 static const struct got_error *
2351 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2353 const struct got_error *err;
2356 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2360 return got_error(GOT_ERR_REBASING);
2362 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2366 return got_error(GOT_ERR_HISTEDIT_BUSY);
2371 static const struct got_error *
2372 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2373 char *argv[], struct got_worktree *worktree)
2375 const struct got_error *err = NULL;
2382 return got_error_from_errno("strdup");
2383 return got_pathlist_append(paths, path, NULL);
2386 for (i = 0; i < argc; i++) {
2387 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2390 err = got_pathlist_append(paths, path, NULL);
2400 static const struct got_error *
2401 cmd_update(int argc, char *argv[])
2403 const struct got_error *error = NULL;
2404 struct got_repository *repo = NULL;
2405 struct got_worktree *worktree = NULL;
2406 char *worktree_path = NULL;
2407 struct got_object_id *commit_id = NULL;
2408 char *commit_id_str = NULL;
2409 const char *branch_name = NULL;
2410 struct got_reference *head_ref = NULL;
2411 struct got_pathlist_head paths;
2412 struct got_pathlist_entry *pe;
2413 int ch, did_something = 0;
2417 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2420 branch_name = optarg;
2423 commit_id_str = strdup(optarg);
2424 if (commit_id_str == NULL)
2425 return got_error_from_errno("strdup");
2437 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2438 "unveil", NULL) == -1)
2441 worktree_path = getcwd(NULL, 0);
2442 if (worktree_path == NULL) {
2443 error = got_error_from_errno("getcwd");
2446 error = got_worktree_open(&worktree, worktree_path);
2450 error = check_rebase_or_histedit_in_progress(worktree);
2454 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2459 error = apply_unveil(got_repo_get_path(repo), 0,
2460 got_worktree_get_root_path(worktree));
2464 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2468 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2469 got_worktree_get_head_ref_name(worktree), 0);
2472 if (commit_id_str == NULL) {
2473 error = got_ref_resolve(&commit_id, repo, head_ref);
2476 error = got_object_id_str(&commit_id_str, commit_id);
2480 error = got_repo_match_object_id(&commit_id, NULL,
2481 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2482 free(commit_id_str);
2483 commit_id_str = NULL;
2486 error = got_object_id_str(&commit_id_str, commit_id);
2492 struct got_object_id *head_commit_id;
2493 TAILQ_FOREACH(pe, &paths, entry) {
2494 if (pe->path_len == 0)
2496 error = got_error_msg(GOT_ERR_BAD_PATH,
2497 "switching between branches requires that "
2498 "the entire work tree gets updated");
2501 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2504 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2506 free(head_commit_id);
2509 error = check_same_branch(commit_id, head_ref, NULL, repo);
2512 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2516 error = check_linear_ancestry(commit_id,
2517 got_worktree_get_base_commit_id(worktree), 0, repo);
2518 if (error != NULL) {
2519 if (error->code == GOT_ERR_ANCESTRY)
2520 error = got_error(GOT_ERR_BRANCH_MOVED);
2523 error = check_same_branch(commit_id, head_ref, NULL, repo);
2528 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2530 error = got_worktree_set_base_commit_id(worktree, repo,
2536 error = got_worktree_checkout_files(worktree, &paths, repo,
2537 update_progress, &did_something, check_cancelled, NULL);
2542 printf("Updated to commit %s\n", commit_id_str);
2544 printf("Already up-to-date\n");
2546 free(worktree_path);
2547 TAILQ_FOREACH(pe, &paths, entry)
2548 free((char *)pe->path);
2549 got_pathlist_free(&paths);
2551 free(commit_id_str);
2555 static const struct got_error *
2556 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2557 const char *path, int diff_context, int ignore_whitespace,
2558 struct got_repository *repo)
2560 const struct got_error *err = NULL;
2561 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2564 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2569 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2573 while (path[0] == '/')
2575 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2576 ignore_whitespace, stdout);
2579 got_object_blob_close(blob1);
2580 got_object_blob_close(blob2);
2584 static const struct got_error *
2585 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2586 const char *path, int diff_context, int ignore_whitespace,
2587 struct got_repository *repo)
2589 const struct got_error *err = NULL;
2590 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2591 struct got_diff_blob_output_unidiff_arg arg;
2594 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2599 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2603 arg.diff_context = diff_context;
2604 arg.ignore_whitespace = ignore_whitespace;
2605 arg.outfile = stdout;
2606 while (path[0] == '/')
2608 err = got_diff_tree(tree1, tree2, path, path, repo,
2609 got_diff_blob_output_unidiff, &arg, 1);
2612 got_object_tree_close(tree1);
2614 got_object_tree_close(tree2);
2618 static const struct got_error *
2619 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2620 const char *path, int diff_context, struct got_repository *repo)
2622 const struct got_error *err = NULL;
2623 struct got_commit_object *pcommit = NULL;
2624 char *id_str1 = NULL, *id_str2 = NULL;
2625 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2626 struct got_object_qid *qid;
2628 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2630 err = got_object_open_as_commit(&pcommit, repo,
2636 if (path && path[0] != '\0') {
2638 err = got_object_id_by_path(&obj_id2, repo, id, path);
2641 err = got_object_id_str(&id_str2, obj_id2);
2647 err = got_object_id_by_path(&obj_id1, repo,
2653 err = got_object_id_str(&id_str1, obj_id1);
2659 err = got_object_get_type(&obj_type, repo, obj_id2);
2664 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2666 case GOT_OBJ_TYPE_BLOB:
2667 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2670 case GOT_OBJ_TYPE_TREE:
2671 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2675 err = got_error(GOT_ERR_OBJ_TYPE);
2681 obj_id2 = got_object_commit_get_tree_id(commit);
2682 err = got_object_id_str(&id_str2, obj_id2);
2685 obj_id1 = got_object_commit_get_tree_id(pcommit);
2686 err = got_object_id_str(&id_str1, obj_id1);
2689 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2690 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2696 got_object_commit_close(pcommit);
2701 get_datestr(time_t *time, char *datebuf)
2703 struct tm mytm, *tm;
2706 tm = gmtime_r(time, &mytm);
2709 s = asctime_r(tm, datebuf);
2712 p = strchr(s, '\n');
2718 static const struct got_error *
2719 match_logmsg(int *have_match, struct got_object_id *id,
2720 struct got_commit_object *commit, regex_t *regex)
2722 const struct got_error *err = NULL;
2723 regmatch_t regmatch;
2724 char *id_str = NULL, *logmsg = NULL;
2728 err = got_object_id_str(&id_str, id);
2732 err = got_object_commit_get_logmsg(&logmsg, commit);
2736 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2744 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2746 static const struct got_error *
2747 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2748 struct got_repository *repo, const char *path, int show_patch,
2749 int diff_context, struct got_reflist_head *refs)
2751 const struct got_error *err = NULL;
2752 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2754 time_t committer_time;
2755 const char *author, *committer;
2756 char *refs_str = NULL;
2757 struct got_reflist_entry *re;
2759 SIMPLEQ_FOREACH(re, refs, entry) {
2762 struct got_tag_object *tag = NULL;
2765 name = got_ref_get_name(re->ref);
2766 if (strcmp(name, GOT_REF_HEAD) == 0)
2768 if (strncmp(name, "refs/", 5) == 0)
2770 if (strncmp(name, "got/", 4) == 0)
2772 if (strncmp(name, "heads/", 6) == 0)
2774 if (strncmp(name, "remotes/", 8) == 0)
2776 if (strncmp(name, "tags/", 5) == 0) {
2777 err = got_object_open_as_tag(&tag, repo, re->id);
2779 if (err->code != GOT_ERR_OBJ_TYPE)
2781 /* Ref points at something other than a tag. */
2786 cmp = got_object_id_cmp(tag ?
2787 got_object_tag_get_object_id(tag) : re->id, id);
2789 got_object_tag_close(tag);
2793 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2795 err = got_error_from_errno("asprintf");
2801 err = got_object_id_str(&id_str, id);
2805 printf(GOT_COMMIT_SEP_STR);
2806 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2807 refs_str ? refs_str : "", refs_str ? ")" : "");
2812 printf("from: %s\n", got_object_commit_get_author(commit));
2813 committer_time = got_object_commit_get_committer_time(commit);
2814 datestr = get_datestr(&committer_time, datebuf);
2816 printf("date: %s UTC\n", datestr);
2817 author = got_object_commit_get_author(commit);
2818 committer = got_object_commit_get_committer(commit);
2819 if (strcmp(author, committer) != 0)
2820 printf("via: %s\n", committer);
2821 if (got_object_commit_get_nparents(commit) > 1) {
2822 const struct got_object_id_queue *parent_ids;
2823 struct got_object_qid *qid;
2825 parent_ids = got_object_commit_get_parent_ids(commit);
2826 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2827 err = got_object_id_str(&id_str, qid->id);
2830 printf("parent %d: %s\n", n++, id_str);
2835 err = got_object_commit_get_logmsg(&logmsg0, commit);
2841 line = strsep(&logmsg, "\n");
2843 printf(" %s\n", line);
2848 err = print_patch(commit, id, path, diff_context, repo);
2853 if (fflush(stdout) != 0 && err == NULL)
2854 err = got_error_from_errno("fflush");
2858 static const struct got_error *
2859 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2860 const char *path, int show_patch, const char *search_pattern,
2861 int diff_context, int limit, int log_branches,
2862 struct got_reflist_head *refs)
2864 const struct got_error *err;
2865 struct got_commit_graph *graph;
2869 if (search_pattern &&
2870 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2871 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2873 err = got_commit_graph_open(&graph, path, !log_branches);
2876 err = got_commit_graph_iter_start(graph, root_id, repo,
2877 check_cancelled, NULL);
2881 struct got_commit_object *commit;
2882 struct got_object_id *id;
2884 if (sigint_received || sigpipe_received)
2887 err = got_commit_graph_iter_next(&id, graph, repo,
2888 check_cancelled, NULL);
2890 if (err->code == GOT_ERR_ITER_COMPLETED)
2897 err = got_object_open_as_commit(&commit, repo, id);
2901 if (search_pattern) {
2902 err = match_logmsg(&have_match, id, commit, ®ex);
2904 got_object_commit_close(commit);
2907 if (have_match == 0) {
2908 got_object_commit_close(commit);
2913 err = print_commit(commit, id, repo, path, show_patch,
2914 diff_context, refs);
2915 got_object_commit_close(commit);
2916 if (err || (limit && --limit == 0))
2922 got_commit_graph_close(graph);
2929 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2930 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2935 get_default_log_limit(void)
2937 const char *got_default_log_limit;
2941 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2942 if (got_default_log_limit == NULL)
2944 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2950 static const struct got_error *
2951 cmd_log(int argc, char *argv[])
2953 const struct got_error *error;
2954 struct got_repository *repo = NULL;
2955 struct got_worktree *worktree = NULL;
2956 struct got_commit_object *commit = NULL;
2957 struct got_object_id *id = NULL;
2958 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2959 const char *start_commit = NULL, *search_pattern = NULL;
2960 int diff_context = -1, ch;
2961 int show_patch = 0, limit = 0, log_branches = 0;
2963 struct got_reflist_head refs;
2965 SIMPLEQ_INIT(&refs);
2968 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2974 limit = get_default_log_limit();
2976 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2982 start_commit = optarg;
2985 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2988 err(1, "-C option %s", errstr);
2991 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2993 err(1, "-l option %s", errstr);
2999 repo_path = realpath(optarg, NULL);
3000 if (repo_path == NULL)
3001 return got_error_from_errno2("realpath",
3003 got_path_strip_trailing_slashes(repo_path);
3006 search_pattern = optarg;
3017 if (diff_context == -1)
3019 else if (!show_patch)
3020 errx(1, "-C reguires -p");
3022 cwd = getcwd(NULL, 0);
3024 error = got_error_from_errno("getcwd");
3028 error = got_worktree_open(&worktree, cwd);
3029 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3036 error = got_error_from_errno("strdup");
3039 } else if (argc == 1) {
3041 error = got_worktree_resolve_path(&path, worktree,
3046 path = strdup(argv[0]);
3048 error = got_error_from_errno("strdup");
3055 if (repo_path == NULL) {
3056 repo_path = worktree ?
3057 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3059 if (repo_path == NULL) {
3060 error = got_error_from_errno("strdup");
3064 error = got_repo_open(&repo, repo_path, NULL);
3068 error = apply_unveil(got_repo_get_path(repo), 1,
3069 worktree ? got_worktree_get_root_path(worktree) : NULL);
3073 if (start_commit == NULL) {
3074 struct got_reference *head_ref;
3075 error = got_ref_open(&head_ref, repo,
3076 worktree ? got_worktree_get_head_ref_name(worktree)
3080 error = got_ref_resolve(&id, repo, head_ref);
3081 got_ref_close(head_ref);
3084 error = got_object_open_as_commit(&commit, repo, id);
3086 struct got_reference *ref;
3087 error = got_ref_open(&ref, repo, start_commit, 0);
3088 if (error == NULL) {
3090 error = got_ref_resolve(&id, repo, ref);
3094 error = got_object_get_type(&obj_type, repo, id);
3097 if (obj_type == GOT_OBJ_TYPE_TAG) {
3098 struct got_tag_object *tag;
3099 error = got_object_open_as_tag(&tag, repo, id);
3102 if (got_object_tag_get_object_type(tag) !=
3103 GOT_OBJ_TYPE_COMMIT) {
3104 got_object_tag_close(tag);
3105 error = got_error(GOT_ERR_OBJ_TYPE);
3109 id = got_object_id_dup(
3110 got_object_tag_get_object_id(tag));
3112 error = got_error_from_errno(
3113 "got_object_id_dup");
3114 got_object_tag_close(tag);
3117 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3118 error = got_error(GOT_ERR_OBJ_TYPE);
3121 error = got_object_open_as_commit(&commit, repo, id);
3125 if (commit == NULL) {
3126 error = got_repo_match_object_id_prefix(&id,
3127 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3136 const char *prefix = got_worktree_get_path_prefix(worktree);
3138 if (asprintf(&p, "%s%s%s", prefix,
3139 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3140 error = got_error_from_errno("asprintf");
3143 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3146 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3151 path = in_repo_path;
3154 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3158 error = print_commits(id, repo, path, show_patch, search_pattern,
3159 diff_context, limit, log_branches, &refs);
3166 got_worktree_close(worktree);
3168 const struct got_error *repo_error;
3169 repo_error = got_repo_close(repo);
3173 got_ref_list_free(&refs);
3180 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3181 "[-w] [object1 object2 | path]\n", getprogname());
3185 struct print_diff_arg {
3186 struct got_repository *repo;
3187 struct got_worktree *worktree;
3192 int ignore_whitespace;
3195 static const struct got_error *
3196 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3197 const char *path, struct got_object_id *blob_id,
3198 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3199 int dirfd, const char *de_name)
3201 struct print_diff_arg *a = arg;
3202 const struct got_error *err = NULL;
3203 struct got_blob_object *blob1 = NULL;
3206 char *abspath = NULL, *label1 = NULL;
3209 if (a->diff_staged) {
3210 if (staged_status != GOT_STATUS_MODIFY &&
3211 staged_status != GOT_STATUS_ADD &&
3212 staged_status != GOT_STATUS_DELETE)
3215 if (staged_status == GOT_STATUS_DELETE)
3217 if (status == GOT_STATUS_NONEXISTENT)
3218 return got_error_set_errno(ENOENT, path);
3219 if (status != GOT_STATUS_MODIFY &&
3220 status != GOT_STATUS_ADD &&
3221 status != GOT_STATUS_DELETE &&
3222 status != GOT_STATUS_CONFLICT)
3226 if (!a->header_shown) {
3227 printf("diff %s %s%s\n", a->id_str,
3228 got_worktree_get_root_path(a->worktree),
3229 a->diff_staged ? " (staged changes)" : "");
3230 a->header_shown = 1;
3233 if (a->diff_staged) {
3234 const char *label1 = NULL, *label2 = NULL;
3235 switch (staged_status) {
3236 case GOT_STATUS_MODIFY:
3240 case GOT_STATUS_ADD:
3243 case GOT_STATUS_DELETE:
3247 return got_error(GOT_ERR_FILE_STATUS);
3249 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3250 label1, label2, a->diff_context, a->ignore_whitespace,
3254 if (staged_status == GOT_STATUS_ADD ||
3255 staged_status == GOT_STATUS_MODIFY) {
3257 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3261 err = got_object_id_str(&id_str, staged_blob_id);
3264 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3265 err = got_error_from_errno("asprintf");
3270 } else if (status != GOT_STATUS_ADD) {
3271 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3276 if (status != GOT_STATUS_DELETE) {
3277 if (asprintf(&abspath, "%s/%s",
3278 got_worktree_get_root_path(a->worktree), path) == -1) {
3279 err = got_error_from_errno("asprintf");
3284 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3286 err = got_error_from_errno2("openat", abspath);
3290 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3292 err = got_error_from_errno2("open", abspath);
3296 if (fstat(fd, &sb) == -1) {
3297 err = got_error_from_errno2("fstat", abspath);
3300 f2 = fdopen(fd, "r");
3302 err = got_error_from_errno2("fdopen", abspath);
3309 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3310 a->diff_context, a->ignore_whitespace, stdout);
3313 got_object_blob_close(blob1);
3314 if (f2 && fclose(f2) == EOF && err == NULL)
3315 err = got_error_from_errno("fclose");
3316 if (fd != -1 && close(fd) == -1 && err == NULL)
3317 err = got_error_from_errno("close");
3322 static const struct got_error *
3323 cmd_diff(int argc, char *argv[])
3325 const struct got_error *error;
3326 struct got_repository *repo = NULL;
3327 struct got_worktree *worktree = NULL;
3328 char *cwd = NULL, *repo_path = NULL;
3329 struct got_object_id *id1 = NULL, *id2 = NULL;
3330 const char *id_str1 = NULL, *id_str2 = NULL;
3331 char *label1 = NULL, *label2 = NULL;
3333 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3338 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3343 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3346 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3349 err(1, "-C option %s", errstr);
3352 repo_path = realpath(optarg, NULL);
3353 if (repo_path == NULL)
3354 return got_error_from_errno2("realpath",
3356 got_path_strip_trailing_slashes(repo_path);
3362 ignore_whitespace = 1;
3373 cwd = getcwd(NULL, 0);
3375 error = got_error_from_errno("getcwd");
3381 "-r option can't be used when diffing a work tree");
3382 error = got_worktree_open(&worktree, cwd);
3385 repo_path = strdup(got_worktree_get_repo_path(worktree));
3386 if (repo_path == NULL) {
3387 error = got_error_from_errno("strdup");
3391 error = got_worktree_resolve_path(&path, worktree,
3398 error = got_error_from_errno("strdup");
3402 } else if (argc == 2) {
3404 errx(1, "-s option can't be used when diffing "
3405 "objects in repository");
3408 if (repo_path == NULL) {
3409 error = got_worktree_open(&worktree, cwd);
3410 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3414 got_worktree_get_repo_path(worktree));
3415 if (repo_path == NULL) {
3416 error = got_error_from_errno("strdup");
3420 repo_path = strdup(cwd);
3421 if (repo_path == NULL) {
3422 error = got_error_from_errno("strdup");
3430 error = got_repo_open(&repo, repo_path, NULL);
3435 error = apply_unveil(got_repo_get_path(repo), 1,
3436 worktree ? got_worktree_get_root_path(worktree) : NULL);
3441 struct print_diff_arg arg;
3442 struct got_pathlist_head paths;
3447 error = got_object_id_str(&id_str,
3448 got_worktree_get_base_commit_id(worktree));
3452 arg.worktree = worktree;
3453 arg.diff_context = diff_context;
3454 arg.id_str = id_str;
3455 arg.header_shown = 0;
3456 arg.diff_staged = diff_staged;
3457 arg.ignore_whitespace = ignore_whitespace;
3459 error = got_pathlist_append(&paths, path, NULL);
3463 error = got_worktree_status(worktree, &paths, repo, print_diff,
3464 &arg, check_cancelled, NULL);
3466 got_pathlist_free(&paths);
3470 error = got_repo_match_object_id(&id1, &label1, id_str1,
3471 GOT_OBJ_TYPE_ANY, 1, repo);
3475 error = got_repo_match_object_id(&id2, &label2, id_str2,
3476 GOT_OBJ_TYPE_ANY, 1, repo);
3480 error = got_object_get_type(&type1, repo, id1);
3484 error = got_object_get_type(&type2, repo, id2);
3488 if (type1 != type2) {
3489 error = got_error(GOT_ERR_OBJ_TYPE);
3494 case GOT_OBJ_TYPE_BLOB:
3495 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3496 diff_context, ignore_whitespace, repo, stdout);
3498 case GOT_OBJ_TYPE_TREE:
3499 error = got_diff_objects_as_trees(id1, id2, "", "",
3500 diff_context, ignore_whitespace, repo, stdout);
3502 case GOT_OBJ_TYPE_COMMIT:
3503 printf("diff %s %s\n", label1, label2);
3504 error = got_diff_objects_as_commits(id1, id2, diff_context,
3505 ignore_whitespace, repo, stdout);
3508 error = got_error(GOT_ERR_OBJ_TYPE);
3517 got_worktree_close(worktree);
3519 const struct got_error *repo_error;
3520 repo_error = got_repo_close(repo);
3531 "usage: %s blame [-c commit] [-r repository-path] path\n",
3540 char datebuf[11]; /* YYYY-MM-DD + NUL */
3543 struct blame_cb_args {
3544 struct blame_line *lines;
3548 off_t *line_offsets;
3550 struct got_repository *repo;
3553 static const struct got_error *
3554 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3556 const struct got_error *err = NULL;
3557 struct blame_cb_args *a = arg;
3558 struct blame_line *bline;
3560 size_t linesize = 0;
3561 struct got_commit_object *commit = NULL;
3564 time_t committer_time;
3566 if (nlines != a->nlines ||
3567 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3568 return got_error(GOT_ERR_RANGE);
3570 if (sigint_received)
3571 return got_error(GOT_ERR_ITER_COMPLETED);
3574 return NULL; /* no change in this commit */
3576 /* Annotate this line. */
3577 bline = &a->lines[lineno - 1];
3578 if (bline->annotated)
3580 err = got_object_id_str(&bline->id_str, id);
3584 err = got_object_open_as_commit(&commit, a->repo, id);
3588 bline->committer = strdup(got_object_commit_get_committer(commit));
3589 if (bline->committer == NULL) {
3590 err = got_error_from_errno("strdup");
3594 committer_time = got_object_commit_get_committer_time(commit);
3595 if (localtime_r(&committer_time, &tm) == NULL)
3596 return got_error_from_errno("localtime_r");
3597 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3598 &tm) >= sizeof(bline->datebuf)) {
3599 err = got_error(GOT_ERR_NO_SPACE);
3602 bline->annotated = 1;
3604 /* Print lines annotated so far. */
3605 bline = &a->lines[a->lineno_cur - 1];
3606 if (!bline->annotated)
3609 offset = a->line_offsets[a->lineno_cur - 1];
3610 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3611 err = got_error_from_errno("fseeko");
3615 while (bline->annotated) {
3616 char *smallerthan, *at, *nl, *committer;
3619 if (getline(&line, &linesize, a->f) == -1) {
3621 err = got_error_from_errno("getline");
3625 committer = bline->committer;
3626 smallerthan = strchr(committer, '<');
3627 if (smallerthan && smallerthan[1] != '\0')
3628 committer = smallerthan + 1;
3629 at = strchr(committer, '@');
3632 len = strlen(committer);
3634 committer[8] = '\0';
3636 nl = strchr(line, '\n');
3639 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3640 bline->id_str, bline->datebuf, committer, line);
3643 bline = &a->lines[a->lineno_cur - 1];
3647 got_object_commit_close(commit);
3652 static const struct got_error *
3653 cmd_blame(int argc, char *argv[])
3655 const struct got_error *error;
3656 struct got_repository *repo = NULL;
3657 struct got_worktree *worktree = NULL;
3658 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3659 struct got_object_id *obj_id = NULL;
3660 struct got_object_id *commit_id = NULL;
3661 struct got_blob_object *blob = NULL;
3662 char *commit_id_str = NULL;
3663 struct blame_cb_args bca;
3664 int ch, obj_type, i;
3667 memset(&bca, 0, sizeof(bca));
3670 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3675 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3678 commit_id_str = optarg;
3681 repo_path = realpath(optarg, NULL);
3682 if (repo_path == NULL)
3683 return got_error_from_errno2("realpath",
3685 got_path_strip_trailing_slashes(repo_path);
3701 cwd = getcwd(NULL, 0);
3703 error = got_error_from_errno("getcwd");
3706 if (repo_path == NULL) {
3707 error = got_worktree_open(&worktree, cwd);
3708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3714 strdup(got_worktree_get_repo_path(worktree));
3715 if (repo_path == NULL) {
3716 error = got_error_from_errno("strdup");
3721 repo_path = strdup(cwd);
3722 if (repo_path == NULL) {
3723 error = got_error_from_errno("strdup");
3729 error = got_repo_open(&repo, repo_path, NULL);
3733 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3738 const char *prefix = got_worktree_get_path_prefix(worktree);
3739 char *p, *worktree_subdir = cwd +
3740 strlen(got_worktree_get_root_path(worktree));
3741 if (asprintf(&p, "%s%s%s%s%s",
3742 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3743 worktree_subdir, worktree_subdir[0] ? "/" : "",
3745 error = got_error_from_errno("asprintf");
3748 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3751 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3756 if (commit_id_str == NULL) {
3757 struct got_reference *head_ref;
3758 error = got_ref_open(&head_ref, repo, worktree ?
3759 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3762 error = got_ref_resolve(&commit_id, repo, head_ref);
3763 got_ref_close(head_ref);
3767 error = got_repo_match_object_id(&commit_id, NULL,
3768 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3773 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3777 error = got_object_get_type(&obj_type, repo, obj_id);
3781 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3782 error = got_error(GOT_ERR_OBJ_TYPE);
3786 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3789 bca.f = got_opentemp();
3790 if (bca.f == NULL) {
3791 error = got_error_from_errno("got_opentemp");
3794 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3795 &bca.line_offsets, bca.f, blob);
3796 if (error || bca.nlines == 0)
3799 /* Don't include \n at EOF in the blame line count. */
3800 if (bca.line_offsets[bca.nlines - 1] == filesize)
3803 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3804 if (bca.lines == NULL) {
3805 error = got_error_from_errno("calloc");
3809 bca.nlines_prec = 0;
3817 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3818 check_cancelled, NULL);
3826 got_object_blob_close(blob);
3828 got_worktree_close(worktree);
3830 const struct got_error *repo_error;
3831 repo_error = got_repo_close(repo);
3836 for (i = 0; i < bca.nlines; i++) {
3837 struct blame_line *bline = &bca.lines[i];
3838 free(bline->id_str);
3839 free(bline->committer);
3843 free(bca.line_offsets);
3844 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3845 error = got_error_from_errno("fclose");
3853 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3859 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3860 const char *root_path)
3862 int is_root_path = (strcmp(path, root_path) == 0);
3863 const char *modestr = "";
3864 mode_t mode = got_tree_entry_get_mode(te);
3866 path += strlen(root_path);
3867 while (path[0] == '/')
3870 if (got_object_tree_entry_is_submodule(te))
3872 else if (S_ISLNK(mode))
3874 else if (S_ISDIR(mode))
3876 else if (mode & S_IXUSR)
3879 printf("%s%s%s%s%s\n", id ? id : "", path,
3880 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3883 static const struct got_error *
3884 print_tree(const char *path, struct got_object_id *commit_id,
3885 int show_ids, int recurse, const char *root_path,
3886 struct got_repository *repo)
3888 const struct got_error *err = NULL;
3889 struct got_object_id *tree_id = NULL;
3890 struct got_tree_object *tree = NULL;
3893 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3897 err = got_object_open_as_tree(&tree, repo, tree_id);
3900 nentries = got_object_tree_get_nentries(tree);
3901 for (i = 0; i < nentries; i++) {
3902 struct got_tree_entry *te;
3905 if (sigint_received || sigpipe_received)
3908 te = got_object_tree_get_entry(tree, i);
3911 err = got_object_id_str(&id_str,
3912 got_tree_entry_get_id(te));
3915 if (asprintf(&id, "%s ", id_str) == -1) {
3916 err = got_error_from_errno("asprintf");
3922 print_entry(te, id, path, root_path);
3925 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3927 if (asprintf(&child_path, "%s%s%s", path,
3928 path[0] == '/' && path[1] == '\0' ? "" : "/",
3929 got_tree_entry_get_name(te)) == -1) {
3930 err = got_error_from_errno("asprintf");
3933 err = print_tree(child_path, commit_id, show_ids, 1,
3942 got_object_tree_close(tree);
3947 static const struct got_error *
3948 cmd_tree(int argc, char *argv[])
3950 const struct got_error *error;
3951 struct got_repository *repo = NULL;
3952 struct got_worktree *worktree = NULL;
3954 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3955 struct got_object_id *commit_id = NULL;
3956 char *commit_id_str = NULL;
3957 int show_ids = 0, recurse = 0;
3961 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3966 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3969 commit_id_str = optarg;
3972 repo_path = realpath(optarg, NULL);
3973 if (repo_path == NULL)
3974 return got_error_from_errno2("realpath",
3976 got_path_strip_trailing_slashes(repo_path);
4000 cwd = getcwd(NULL, 0);
4002 error = got_error_from_errno("getcwd");
4005 if (repo_path == NULL) {
4006 error = got_worktree_open(&worktree, cwd);
4007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4013 strdup(got_worktree_get_repo_path(worktree));
4014 if (repo_path == NULL)
4015 error = got_error_from_errno("strdup");
4019 repo_path = strdup(cwd);
4020 if (repo_path == NULL) {
4021 error = got_error_from_errno("strdup");
4027 error = got_repo_open(&repo, repo_path, NULL);
4031 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4037 char *p, *worktree_subdir = cwd +
4038 strlen(got_worktree_get_root_path(worktree));
4039 if (asprintf(&p, "%s/%s",
4040 got_worktree_get_path_prefix(worktree),
4041 worktree_subdir) == -1) {
4042 error = got_error_from_errno("asprintf");
4045 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4052 if (in_repo_path == NULL) {
4053 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4058 if (commit_id_str == NULL) {
4059 struct got_reference *head_ref;
4060 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
4063 error = got_ref_resolve(&commit_id, repo, head_ref);
4064 got_ref_close(head_ref);
4068 error = got_repo_match_object_id(&commit_id, NULL,
4069 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4074 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4075 in_repo_path, repo);
4082 got_worktree_close(worktree);
4084 const struct got_error *repo_error;
4085 repo_error = got_repo_close(repo);
4095 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4099 static const struct got_error *
4100 print_status(void *arg, unsigned char status, unsigned char staged_status,
4101 const char *path, struct got_object_id *blob_id,
4102 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4103 int dirfd, const char *de_name)
4105 if (status == staged_status && (status == GOT_STATUS_DELETE))
4106 status = GOT_STATUS_NO_CHANGE;
4107 printf("%c%c %s\n", status, staged_status, path);
4111 static const struct got_error *
4112 cmd_status(int argc, char *argv[])
4114 const struct got_error *error = NULL;
4115 struct got_repository *repo = NULL;
4116 struct got_worktree *worktree = NULL;
4118 struct got_pathlist_head paths;
4119 struct got_pathlist_entry *pe;
4124 while ((ch = getopt(argc, argv, "")) != -1) {
4136 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4140 cwd = getcwd(NULL, 0);
4142 error = got_error_from_errno("getcwd");
4146 error = got_worktree_open(&worktree, cwd);
4150 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4155 error = apply_unveil(got_repo_get_path(repo), 1,
4156 got_worktree_get_root_path(worktree));
4160 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4164 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4165 check_cancelled, NULL);
4167 TAILQ_FOREACH(pe, &paths, entry)
4168 free((char *)pe->path);
4169 got_pathlist_free(&paths);
4178 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4184 static const struct got_error *
4185 list_refs(struct got_repository *repo, const char *refname)
4187 static const struct got_error *err = NULL;
4188 struct got_reflist_head refs;
4189 struct got_reflist_entry *re;
4191 SIMPLEQ_INIT(&refs);
4192 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4196 SIMPLEQ_FOREACH(re, &refs, entry) {
4198 refstr = got_ref_to_str(re->ref);
4200 return got_error_from_errno("got_ref_to_str");
4201 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4205 got_ref_list_free(&refs);
4209 static const struct got_error *
4210 delete_ref(struct got_repository *repo, const char *refname)
4212 const struct got_error *err = NULL;
4213 struct got_reference *ref;
4215 err = got_ref_open(&ref, repo, refname, 0);
4219 err = got_ref_delete(ref, repo);
4224 static const struct got_error *
4225 add_ref(struct got_repository *repo, const char *refname, const char *target)
4227 const struct got_error *err = NULL;
4228 struct got_object_id *id;
4229 struct got_reference *ref = NULL;
4232 * Don't let the user create a reference name with a leading '-'.
4233 * While technically a valid reference name, this case is usually
4234 * an unintended typo.
4236 if (refname[0] == '-')
4237 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4239 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4242 struct got_reference *target_ref;
4244 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4246 err = got_ref_open(&target_ref, repo, target, 0);
4249 err = got_ref_resolve(&id, repo, target_ref);
4250 got_ref_close(target_ref);
4255 err = got_ref_alloc(&ref, refname, id);
4259 err = got_ref_write(ref, repo);
4267 static const struct got_error *
4268 add_symref(struct got_repository *repo, const char *refname, const char *target)
4270 const struct got_error *err = NULL;
4271 struct got_reference *ref = NULL;
4272 struct got_reference *target_ref = NULL;
4275 * Don't let the user create a reference name with a leading '-'.
4276 * While technically a valid reference name, this case is usually
4277 * an unintended typo.
4279 if (refname[0] == '-')
4280 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4282 err = got_ref_open(&target_ref, repo, target, 0);
4286 err = got_ref_alloc_symref(&ref, refname, target_ref);
4290 err = got_ref_write(ref, repo);
4293 got_ref_close(target_ref);
4299 static const struct got_error *
4300 cmd_ref(int argc, char *argv[])
4302 const struct got_error *error = NULL;
4303 struct got_repository *repo = NULL;
4304 struct got_worktree *worktree = NULL;
4305 char *cwd = NULL, *repo_path = NULL;
4306 int ch, do_list = 0, do_delete = 0;
4307 const char *obj_arg = NULL, *symref_target= NULL;
4308 char *refname = NULL;
4310 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4319 repo_path = realpath(optarg, NULL);
4320 if (repo_path == NULL)
4321 return got_error_from_errno2("realpath",
4323 got_path_strip_trailing_slashes(repo_path);
4329 symref_target = optarg;
4337 if (obj_arg && do_list)
4338 errx(1, "-c and -l options are mutually exclusive");
4339 if (obj_arg && do_delete)
4340 errx(1, "-c and -d options are mutually exclusive");
4341 if (obj_arg && symref_target)
4342 errx(1, "-c and -s options are mutually exclusive");
4343 if (symref_target && do_delete)
4344 errx(1, "-s and -d options are mutually exclusive");
4345 if (symref_target && do_list)
4346 errx(1, "-s and -l options are mutually exclusive");
4347 if (do_delete && do_list)
4348 errx(1, "-d and -l options are mutually exclusive");
4354 if (argc != 0 && argc != 1)
4357 refname = strdup(argv[0]);
4358 if (refname == NULL) {
4359 error = got_error_from_errno("strdup");
4366 refname = strdup(argv[0]);
4367 if (refname == NULL) {
4368 error = got_error_from_errno("strdup");
4374 got_path_strip_trailing_slashes(refname);
4378 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4382 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4383 "sendfd unveil", NULL) == -1)
4387 cwd = getcwd(NULL, 0);
4389 error = got_error_from_errno("getcwd");
4393 if (repo_path == NULL) {
4394 error = got_worktree_open(&worktree, cwd);
4395 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4401 strdup(got_worktree_get_repo_path(worktree));
4402 if (repo_path == NULL)
4403 error = got_error_from_errno("strdup");
4407 repo_path = strdup(cwd);
4408 if (repo_path == NULL) {
4409 error = got_error_from_errno("strdup");
4415 error = got_repo_open(&repo, repo_path, NULL);
4419 error = apply_unveil(got_repo_get_path(repo), do_list,
4420 worktree ? got_worktree_get_root_path(worktree) : NULL);
4425 error = list_refs(repo, refname);
4427 error = delete_ref(repo, refname);
4428 else if (symref_target)
4429 error = add_symref(repo, refname, symref_target);
4431 if (obj_arg == NULL)
4433 error = add_ref(repo, refname, obj_arg);
4438 got_repo_close(repo);
4440 got_worktree_close(worktree);
4450 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4451 "[name]\n", getprogname());
4455 static const struct got_error *
4456 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4457 struct got_reference *ref)
4459 const struct got_error *err = NULL;
4460 const char *refname, *marker = " ";
4463 refname = got_ref_get_name(ref);
4464 if (worktree && strcmp(refname,
4465 got_worktree_get_head_ref_name(worktree)) == 0) {
4466 struct got_object_id *id = NULL;
4468 err = got_ref_resolve(&id, repo, ref);
4471 if (got_object_id_cmp(id,
4472 got_worktree_get_base_commit_id(worktree)) == 0)
4479 if (strncmp(refname, "refs/heads/", 11) == 0)
4481 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4484 refstr = got_ref_to_str(ref);
4486 return got_error_from_errno("got_ref_to_str");
4488 printf("%s%s: %s\n", marker, refname, refstr);
4493 static const struct got_error *
4494 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4496 const char *refname;
4498 if (worktree == NULL)
4499 return got_error(GOT_ERR_NOT_WORKTREE);
4501 refname = got_worktree_get_head_ref_name(worktree);
4503 if (strncmp(refname, "refs/heads/", 11) == 0)
4505 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4508 printf("%s\n", refname);
4513 static const struct got_error *
4514 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4516 static const struct got_error *err = NULL;
4517 struct got_reflist_head refs;
4518 struct got_reflist_entry *re;
4519 struct got_reference *temp_ref = NULL;
4520 int rebase_in_progress, histedit_in_progress;
4522 SIMPLEQ_INIT(&refs);
4525 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4530 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4535 if (rebase_in_progress || histedit_in_progress) {
4536 err = got_ref_open(&temp_ref, repo,
4537 got_worktree_get_head_ref_name(worktree), 0);
4540 list_branch(repo, worktree, temp_ref);
4541 got_ref_close(temp_ref);
4545 err = got_ref_list(&refs, repo, "refs/heads",
4546 got_ref_cmp_by_name, NULL);
4550 SIMPLEQ_FOREACH(re, &refs, entry)
4551 list_branch(repo, worktree, re->ref);
4553 got_ref_list_free(&refs);
4557 static const struct got_error *
4558 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4559 const char *branch_name)
4561 const struct got_error *err = NULL;
4562 struct got_reference *ref = NULL;
4565 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4566 return got_error_from_errno("asprintf");
4568 err = got_ref_open(&ref, repo, refname, 0);
4573 strcmp(got_worktree_get_head_ref_name(worktree),
4574 got_ref_get_name(ref)) == 0) {
4575 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4576 "will not delete this work tree's current branch");
4580 err = got_ref_delete(ref, repo);
4588 static const struct got_error *
4589 add_branch(struct got_repository *repo, const char *branch_name,
4590 struct got_object_id *base_commit_id)
4592 const struct got_error *err = NULL;
4593 struct got_reference *ref = NULL;
4594 char *base_refname = NULL, *refname = NULL;
4597 * Don't let the user create a branch name with a leading '-'.
4598 * While technically a valid reference name, this case is usually
4599 * an unintended typo.
4601 if (branch_name[0] == '-')
4602 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4604 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4605 err = got_error_from_errno("asprintf");
4609 err = got_ref_open(&ref, repo, refname, 0);
4611 err = got_error(GOT_ERR_BRANCH_EXISTS);
4613 } else if (err->code != GOT_ERR_NOT_REF)
4616 err = got_ref_alloc(&ref, refname, base_commit_id);
4620 err = got_ref_write(ref, repo);
4629 static const struct got_error *
4630 cmd_branch(int argc, char *argv[])
4632 const struct got_error *error = NULL;
4633 struct got_repository *repo = NULL;
4634 struct got_worktree *worktree = NULL;
4635 char *cwd = NULL, *repo_path = NULL;
4636 int ch, do_list = 0, do_show = 0, do_update = 1;
4637 const char *delref = NULL, *commit_id_arg = NULL;
4638 struct got_reference *ref = NULL;
4639 struct got_pathlist_head paths;
4640 struct got_pathlist_entry *pe;
4641 struct got_object_id *commit_id = NULL;
4642 char *commit_id_str = NULL;
4646 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4649 commit_id_arg = optarg;
4655 repo_path = realpath(optarg, NULL);
4656 if (repo_path == NULL)
4657 return got_error_from_errno2("realpath",
4659 got_path_strip_trailing_slashes(repo_path);
4673 if (do_list && delref)
4674 errx(1, "-l and -d options are mutually exclusive");
4679 if (!do_list && !delref && argc == 0)
4682 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4683 errx(1, "-c option can only be used when creating a branch");
4685 if (do_list || delref) {
4688 } else if (!do_show && argc != 1)
4692 if (do_list || do_show) {
4693 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4697 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4698 "sendfd unveil", NULL) == -1)
4702 cwd = getcwd(NULL, 0);
4704 error = got_error_from_errno("getcwd");
4708 if (repo_path == NULL) {
4709 error = got_worktree_open(&worktree, cwd);
4710 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4716 strdup(got_worktree_get_repo_path(worktree));
4717 if (repo_path == NULL)
4718 error = got_error_from_errno("strdup");
4722 repo_path = strdup(cwd);
4723 if (repo_path == NULL) {
4724 error = got_error_from_errno("strdup");
4730 error = got_repo_open(&repo, repo_path, NULL);
4734 error = apply_unveil(got_repo_get_path(repo), do_list,
4735 worktree ? got_worktree_get_root_path(worktree) : NULL);
4740 error = show_current_branch(repo, worktree);
4742 error = list_branches(repo, worktree);
4744 error = delete_branch(repo, worktree, delref);
4746 if (commit_id_arg == NULL)
4747 commit_id_arg = worktree ?
4748 got_worktree_get_head_ref_name(worktree) :
4750 error = got_repo_match_object_id(&commit_id, NULL,
4751 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4754 error = add_branch(repo, argv[0], commit_id);
4757 if (worktree && do_update) {
4758 int did_something = 0;
4759 char *branch_refname = NULL;
4761 error = got_object_id_str(&commit_id_str, commit_id);
4764 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4768 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4770 error = got_error_from_errno("asprintf");
4773 error = got_ref_open(&ref, repo, branch_refname, 0);
4774 free(branch_refname);
4777 error = switch_head_ref(ref, commit_id, worktree,
4781 error = got_worktree_set_base_commit_id(worktree, repo,
4785 error = got_worktree_checkout_files(worktree, &paths,
4786 repo, update_progress, &did_something,
4787 check_cancelled, NULL);
4791 printf("Updated to commit %s\n", commit_id_str);
4798 got_repo_close(repo);
4800 got_worktree_close(worktree);
4804 free(commit_id_str);
4805 TAILQ_FOREACH(pe, &paths, entry)
4806 free((char *)pe->path);
4807 got_pathlist_free(&paths);
4816 "usage: %s tag [-c commit] [-r repository] [-l] "
4817 "[-m message] name\n", getprogname());
4822 static const struct got_error *
4823 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4825 const struct got_error *err = NULL;
4826 struct got_reflist_entry *re, *se, *new;
4827 struct got_object_id *re_id, *se_id;
4828 struct got_tag_object *re_tag, *se_tag;
4829 time_t re_time, se_time;
4831 SIMPLEQ_FOREACH(re, tags, entry) {
4832 se = SIMPLEQ_FIRST(sorted);
4834 err = got_reflist_entry_dup(&new, re);
4837 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4840 err = got_ref_resolve(&re_id, repo, re->ref);
4843 err = got_object_open_as_tag(&re_tag, repo, re_id);
4847 re_time = got_object_tag_get_tagger_time(re_tag);
4848 got_object_tag_close(re_tag);
4852 err = got_ref_resolve(&se_id, repo, re->ref);
4855 err = got_object_open_as_tag(&se_tag, repo, se_id);
4859 se_time = got_object_tag_get_tagger_time(se_tag);
4860 got_object_tag_close(se_tag);
4862 if (se_time > re_time) {
4863 err = got_reflist_entry_dup(&new, re);
4866 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4869 se = SIMPLEQ_NEXT(se, entry);
4878 static const struct got_error *
4879 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4881 static const struct got_error *err = NULL;
4882 struct got_reflist_head refs;
4883 struct got_reflist_entry *re;
4885 SIMPLEQ_INIT(&refs);
4887 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4891 SIMPLEQ_FOREACH(re, &refs, entry) {
4892 const char *refname;
4893 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4897 struct got_object_id *id;
4898 struct got_tag_object *tag;
4899 struct got_commit_object *commit = NULL;
4901 refname = got_ref_get_name(re->ref);
4902 if (strncmp(refname, "refs/tags/", 10) != 0)
4905 refstr = got_ref_to_str(re->ref);
4906 if (refstr == NULL) {
4907 err = got_error_from_errno("got_ref_to_str");
4910 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4913 err = got_ref_resolve(&id, repo, re->ref);
4916 err = got_object_open_as_tag(&tag, repo, id);
4918 if (err->code != GOT_ERR_OBJ_TYPE) {
4922 /* "lightweight" tag */
4923 err = got_object_open_as_commit(&commit, repo, id);
4928 tagger = got_object_commit_get_committer(commit);
4930 got_object_commit_get_committer_time(commit);
4931 err = got_object_id_str(&id_str, id);
4937 tagger = got_object_tag_get_tagger(tag);
4938 tagger_time = got_object_tag_get_tagger_time(tag);
4939 err = got_object_id_str(&id_str,
4940 got_object_tag_get_object_id(tag));
4944 printf("from: %s\n", tagger);
4945 datestr = get_datestr(&tagger_time, datebuf);
4947 printf("date: %s UTC\n", datestr);
4949 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4951 switch (got_object_tag_get_object_type(tag)) {
4952 case GOT_OBJ_TYPE_BLOB:
4953 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4956 case GOT_OBJ_TYPE_TREE:
4957 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4960 case GOT_OBJ_TYPE_COMMIT:
4961 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4964 case GOT_OBJ_TYPE_TAG:
4965 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4974 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4977 got_object_commit_close(commit);
4979 tagmsg0 = strdup(got_object_tag_get_message(tag));
4980 got_object_tag_close(tag);
4981 if (tagmsg0 == NULL) {
4982 err = got_error_from_errno("strdup");
4989 line = strsep(&tagmsg, "\n");
4991 printf(" %s\n", line);
4996 got_ref_list_free(&refs);
5000 static const struct got_error *
5001 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5002 const char *tag_name, const char *repo_path)
5004 const struct got_error *err = NULL;
5005 char *template = NULL, *initial_content = NULL;
5006 char *editor = NULL;
5009 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5010 err = got_error_from_errno("asprintf");
5014 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5015 commit_id_str, tag_name) == -1) {
5016 err = got_error_from_errno("asprintf");
5020 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5024 dprintf(fd, initial_content);
5027 err = get_editor(&editor);
5030 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5032 free(initial_content);
5036 /* Editor is done; we can now apply unveil(2) */
5038 err = apply_unveil(repo_path, 0, NULL);
5047 static const struct got_error *
5048 add_tag(struct got_repository *repo, const char *tag_name,
5049 const char *commit_arg, const char *tagmsg_arg)
5051 const struct got_error *err = NULL;
5052 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5053 char *label = NULL, *commit_id_str = NULL;
5054 struct got_reference *ref = NULL;
5055 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5056 char *tagmsg_path = NULL, *tag_id_str = NULL;
5057 int preserve_tagmsg = 0;
5060 * Don't let the user create a tag name with a leading '-'.
5061 * While technically a valid reference name, this case is usually
5062 * an unintended typo.
5064 if (tag_name[0] == '-')
5065 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5067 err = get_author(&tagger, repo);
5071 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5072 GOT_OBJ_TYPE_COMMIT, 1, repo);
5076 err = got_object_id_str(&commit_id_str, commit_id);
5080 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5081 refname = strdup(tag_name);
5082 if (refname == NULL) {
5083 err = got_error_from_errno("strdup");
5087 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5088 err = got_error_from_errno("asprintf");
5092 err = got_ref_open(&ref, repo, refname, 0);
5094 err = got_error(GOT_ERR_TAG_EXISTS);
5096 } else if (err->code != GOT_ERR_NOT_REF)
5099 if (tagmsg_arg == NULL) {
5100 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5101 tag_name, got_repo_get_path(repo));
5103 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5104 tagmsg_path != NULL)
5105 preserve_tagmsg = 1;
5110 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5111 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5114 preserve_tagmsg = 1;
5118 err = got_ref_alloc(&ref, refname, tag_id);
5121 preserve_tagmsg = 1;
5125 err = got_ref_write(ref, repo);
5128 preserve_tagmsg = 1;
5132 err = got_object_id_str(&tag_id_str, tag_id);
5135 preserve_tagmsg = 1;
5138 printf("Created tag %s\n", tag_id_str);
5140 if (preserve_tagmsg) {
5141 fprintf(stderr, "%s: tag message preserved in %s\n",
5142 getprogname(), tagmsg_path);
5143 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5144 err = got_error_from_errno2("unlink", tagmsg_path);
5149 free(commit_id_str);
5157 static const struct got_error *
5158 cmd_tag(int argc, char *argv[])
5160 const struct got_error *error = NULL;
5161 struct got_repository *repo = NULL;
5162 struct got_worktree *worktree = NULL;
5163 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5164 char *gitconfig_path = NULL;
5165 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5166 int ch, do_list = 0;
5168 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5171 commit_id_arg = optarg;
5177 repo_path = realpath(optarg, NULL);
5178 if (repo_path == NULL)
5179 return got_error_from_errno2("realpath",
5181 got_path_strip_trailing_slashes(repo_path);
5196 if (commit_id_arg != NULL)
5197 errx(1, "-c option can only be used when creating a tag");
5199 errx(1, "-l and -m options are mutually exclusive");
5202 } else if (argc != 1)
5209 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5213 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5214 "sendfd unveil", NULL) == -1)
5218 cwd = getcwd(NULL, 0);
5220 error = got_error_from_errno("getcwd");
5224 if (repo_path == NULL) {
5225 error = got_worktree_open(&worktree, cwd);
5226 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5232 strdup(got_worktree_get_repo_path(worktree));
5233 if (repo_path == NULL)
5234 error = got_error_from_errno("strdup");
5238 repo_path = strdup(cwd);
5239 if (repo_path == NULL) {
5240 error = got_error_from_errno("strdup");
5247 error = got_repo_open(&repo, repo_path, NULL);
5250 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5253 error = list_tags(repo, worktree);
5255 error = get_gitconfig_path(&gitconfig_path);
5258 error = got_repo_open(&repo, repo_path, gitconfig_path);
5263 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5268 if (commit_id_arg == NULL) {
5269 struct got_reference *head_ref;
5270 struct got_object_id *commit_id;
5271 error = got_ref_open(&head_ref, repo,
5272 worktree ? got_worktree_get_head_ref_name(worktree)
5276 error = got_ref_resolve(&commit_id, repo, head_ref);
5277 got_ref_close(head_ref);
5280 error = got_object_id_str(&commit_id_str, commit_id);
5286 error = add_tag(repo, tag_name,
5287 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5291 got_repo_close(repo);
5293 got_worktree_close(worktree);
5296 free(gitconfig_path);
5297 free(commit_id_str);
5304 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5309 static const struct got_error *
5310 add_progress(void *arg, unsigned char status, const char *path)
5312 while (path[0] == '/')
5314 printf("%c %s\n", status, path);
5318 static const struct got_error *
5319 cmd_add(int argc, char *argv[])
5321 const struct got_error *error = NULL;
5322 struct got_repository *repo = NULL;
5323 struct got_worktree *worktree = NULL;
5325 struct got_pathlist_head paths;
5326 struct got_pathlist_entry *pe;
5327 int ch, can_recurse = 0, no_ignores = 0;
5331 while ((ch = getopt(argc, argv, "IR")) != -1) {
5349 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5356 cwd = getcwd(NULL, 0);
5358 error = got_error_from_errno("getcwd");
5362 error = got_worktree_open(&worktree, cwd);
5366 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5371 error = apply_unveil(got_repo_get_path(repo), 1,
5372 got_worktree_get_root_path(worktree));
5376 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5380 if (!can_recurse && no_ignores) {
5381 error = got_error_msg(GOT_ERR_BAD_PATH,
5382 "disregarding ignores requires -R option");
5390 TAILQ_FOREACH(pe, &paths, entry) {
5391 if (asprintf(&ondisk_path, "%s/%s",
5392 got_worktree_get_root_path(worktree),
5394 error = got_error_from_errno("asprintf");
5397 if (lstat(ondisk_path, &sb) == -1) {
5398 if (errno == ENOENT) {
5402 error = got_error_from_errno2("lstat",
5408 if (S_ISDIR(sb.st_mode)) {
5409 error = got_error_msg(GOT_ERR_BAD_PATH,
5410 "adding directories requires -R option");
5416 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5417 NULL, repo, no_ignores);
5420 got_repo_close(repo);
5422 got_worktree_close(worktree);
5423 TAILQ_FOREACH(pe, &paths, entry)
5424 free((char *)pe->path);
5425 got_pathlist_free(&paths);
5433 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5438 static const struct got_error *
5439 print_remove_status(void *arg, unsigned char status,
5440 unsigned char staged_status, const char *path)
5442 while (path[0] == '/')
5444 if (status == GOT_STATUS_NONEXISTENT)
5446 if (status == staged_status && (status == GOT_STATUS_DELETE))
5447 status = GOT_STATUS_NO_CHANGE;
5448 printf("%c%c %s\n", status, staged_status, path);
5452 static const struct got_error *
5453 cmd_remove(int argc, char *argv[])
5455 const struct got_error *error = NULL;
5456 struct got_worktree *worktree = NULL;
5457 struct got_repository *repo = NULL;
5459 struct got_pathlist_head paths;
5460 struct got_pathlist_entry *pe;
5461 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5465 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5468 delete_local_mods = 1;
5486 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5493 cwd = getcwd(NULL, 0);
5495 error = got_error_from_errno("getcwd");
5498 error = got_worktree_open(&worktree, cwd);
5502 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5507 error = apply_unveil(got_repo_get_path(repo), 1,
5508 got_worktree_get_root_path(worktree));
5512 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5519 TAILQ_FOREACH(pe, &paths, entry) {
5520 if (asprintf(&ondisk_path, "%s/%s",
5521 got_worktree_get_root_path(worktree),
5523 error = got_error_from_errno("asprintf");
5526 if (lstat(ondisk_path, &sb) == -1) {
5527 if (errno == ENOENT) {
5531 error = got_error_from_errno2("lstat",
5537 if (S_ISDIR(sb.st_mode)) {
5538 error = got_error_msg(GOT_ERR_BAD_PATH,
5539 "removing directories requires -R option");
5545 error = got_worktree_schedule_delete(worktree, &paths,
5546 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5549 got_repo_close(repo);
5551 got_worktree_close(worktree);
5552 TAILQ_FOREACH(pe, &paths, entry)
5553 free((char *)pe->path);
5554 got_pathlist_free(&paths);
5562 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5563 "path ...\n", getprogname());
5567 static const struct got_error *
5568 revert_progress(void *arg, unsigned char status, const char *path)
5570 if (status == GOT_STATUS_UNVERSIONED)
5573 while (path[0] == '/')
5575 printf("%c %s\n", status, path);
5579 struct choose_patch_arg {
5580 FILE *patch_script_file;
5584 static const struct got_error *
5585 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5586 int nchanges, const char *action)
5589 size_t linesize = 0;
5593 case GOT_STATUS_ADD:
5594 printf("A %s\n%s this addition? [y/n] ", path, action);
5596 case GOT_STATUS_DELETE:
5597 printf("D %s\n%s this deletion? [y/n] ", path, action);
5599 case GOT_STATUS_MODIFY:
5600 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5601 return got_error_from_errno("fseek");
5602 printf(GOT_COMMIT_SEP_STR);
5603 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5605 if (ferror(patch_file))
5606 return got_error_from_errno("getline");
5607 printf(GOT_COMMIT_SEP_STR);
5608 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5609 path, n, nchanges, action);
5612 return got_error_path(path, GOT_ERR_FILE_STATUS);
5618 static const struct got_error *
5619 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5620 FILE *patch_file, int n, int nchanges)
5622 const struct got_error *err = NULL;
5624 size_t linesize = 0;
5627 struct choose_patch_arg *a = arg;
5629 *choice = GOT_PATCH_CHOICE_NONE;
5631 if (a->patch_script_file) {
5633 err = show_change(status, path, patch_file, n, nchanges,
5637 linelen = getline(&line, &linesize, a->patch_script_file);
5638 if (linelen == -1) {
5639 if (ferror(a->patch_script_file))
5640 return got_error_from_errno("getline");
5643 nl = strchr(line, '\n');
5646 if (strcmp(line, "y") == 0) {
5647 *choice = GOT_PATCH_CHOICE_YES;
5649 } else if (strcmp(line, "n") == 0) {
5650 *choice = GOT_PATCH_CHOICE_NO;
5652 } else if (strcmp(line, "q") == 0 &&
5653 status == GOT_STATUS_MODIFY) {
5654 *choice = GOT_PATCH_CHOICE_QUIT;
5657 printf("invalid response '%s'\n", line);
5662 while (resp != 'y' && resp != 'n' && resp != 'q') {
5663 err = show_change(status, path, patch_file, n, nchanges,
5670 if (status == GOT_STATUS_MODIFY) {
5671 if (resp != 'y' && resp != 'n' && resp != 'q') {
5672 printf("invalid response '%c'\n", resp);
5675 } else if (resp != 'y' && resp != 'n') {
5676 printf("invalid response '%c'\n", resp);
5682 *choice = GOT_PATCH_CHOICE_YES;
5683 else if (resp == 'n')
5684 *choice = GOT_PATCH_CHOICE_NO;
5685 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5686 *choice = GOT_PATCH_CHOICE_QUIT;
5692 static const struct got_error *
5693 cmd_revert(int argc, char *argv[])
5695 const struct got_error *error = NULL;
5696 struct got_worktree *worktree = NULL;
5697 struct got_repository *repo = NULL;
5698 char *cwd = NULL, *path = NULL;
5699 struct got_pathlist_head paths;
5700 struct got_pathlist_entry *pe;
5701 int ch, can_recurse = 0, pflag = 0;
5702 FILE *patch_script_file = NULL;
5703 const char *patch_script_path = NULL;
5704 struct choose_patch_arg cpa;
5708 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5714 patch_script_path = optarg;
5729 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5730 "unveil", NULL) == -1)
5735 if (patch_script_path && !pflag)
5736 errx(1, "-F option can only be used together with -p option");
5738 cwd = getcwd(NULL, 0);
5740 error = got_error_from_errno("getcwd");
5743 error = got_worktree_open(&worktree, cwd);
5747 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5752 if (patch_script_path) {
5753 patch_script_file = fopen(patch_script_path, "r");
5754 if (patch_script_file == NULL) {
5755 error = got_error_from_errno2("fopen",
5760 error = apply_unveil(got_repo_get_path(repo), 1,
5761 got_worktree_get_root_path(worktree));
5765 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5772 TAILQ_FOREACH(pe, &paths, entry) {
5773 if (asprintf(&ondisk_path, "%s/%s",
5774 got_worktree_get_root_path(worktree),
5776 error = got_error_from_errno("asprintf");
5779 if (lstat(ondisk_path, &sb) == -1) {
5780 if (errno == ENOENT) {
5784 error = got_error_from_errno2("lstat",
5790 if (S_ISDIR(sb.st_mode)) {
5791 error = got_error_msg(GOT_ERR_BAD_PATH,
5792 "reverting directories requires -R option");
5798 cpa.patch_script_file = patch_script_file;
5799 cpa.action = "revert";
5800 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5801 pflag ? choose_patch : NULL, &cpa, repo);
5803 if (patch_script_file && fclose(patch_script_file) == EOF &&
5805 error = got_error_from_errno2("fclose", patch_script_path);
5807 got_repo_close(repo);
5809 got_worktree_close(worktree);
5818 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5823 struct collect_commit_logmsg_arg {
5824 const char *cmdline_log;
5826 const char *worktree_path;
5827 const char *branch_name;
5828 const char *repo_path;
5833 static const struct got_error *
5834 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5837 char *initial_content = NULL;
5838 struct got_pathlist_entry *pe;
5839 const struct got_error *err = NULL;
5840 char *template = NULL;
5841 struct collect_commit_logmsg_arg *a = arg;
5845 /* if a message was specified on the command line, just use it */
5846 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5847 len = strlen(a->cmdline_log) + 1;
5848 *logmsg = malloc(len + 1);
5849 if (*logmsg == NULL)
5850 return got_error_from_errno("malloc");
5851 strlcpy(*logmsg, a->cmdline_log, len);
5855 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5856 return got_error_from_errno("asprintf");
5858 if (asprintf(&initial_content,
5859 "\n# changes to be committed on branch %s:\n",
5860 a->branch_name) == -1)
5861 return got_error_from_errno("asprintf");
5863 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5867 dprintf(fd, initial_content);
5869 TAILQ_FOREACH(pe, commitable_paths, entry) {
5870 struct got_commitable *ct = pe->data;
5871 dprintf(fd, "# %c %s\n",
5872 got_commitable_get_status(ct),
5873 got_commitable_get_path(ct));
5877 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5879 free(initial_content);
5882 /* Editor is done; we can now apply unveil(2) */
5884 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5893 static const struct got_error *
5894 cmd_commit(int argc, char *argv[])
5896 const struct got_error *error = NULL;
5897 struct got_worktree *worktree = NULL;
5898 struct got_repository *repo = NULL;
5899 char *cwd = NULL, *id_str = NULL;
5900 struct got_object_id *id = NULL;
5901 const char *logmsg = NULL;
5902 struct collect_commit_logmsg_arg cl_arg;
5903 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5904 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5905 struct got_pathlist_head paths;
5908 cl_arg.logmsg_path = NULL;
5910 while ((ch = getopt(argc, argv, "m:")) != -1) {
5925 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5926 "unveil", NULL) == -1)
5929 cwd = getcwd(NULL, 0);
5931 error = got_error_from_errno("getcwd");
5934 error = got_worktree_open(&worktree, cwd);
5938 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5941 if (rebase_in_progress) {
5942 error = got_error(GOT_ERR_REBASING);
5946 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5951 error = get_gitconfig_path(&gitconfig_path);
5954 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5959 error = get_author(&author, repo);
5964 * unveil(2) traverses exec(2); if an editor is used we have
5965 * to apply unveil after the log message has been written.
5967 if (logmsg == NULL || strlen(logmsg) == 0)
5968 error = get_editor(&editor);
5970 error = apply_unveil(got_repo_get_path(repo), 0,
5971 got_worktree_get_root_path(worktree));
5975 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5979 cl_arg.editor = editor;
5980 cl_arg.cmdline_log = logmsg;
5981 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5982 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5983 if (!histedit_in_progress) {
5984 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5985 error = got_error(GOT_ERR_COMMIT_BRANCH);
5988 cl_arg.branch_name += 11;
5990 cl_arg.repo_path = got_repo_get_path(repo);
5991 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5992 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5994 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5995 cl_arg.logmsg_path != NULL)
5996 preserve_logmsg = 1;
6000 error = got_object_id_str(&id_str, id);
6003 printf("Created commit %s\n", id_str);
6005 if (preserve_logmsg) {
6006 fprintf(stderr, "%s: log message preserved in %s\n",
6007 getprogname(), cl_arg.logmsg_path);
6008 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6010 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6011 free(cl_arg.logmsg_path);
6013 got_repo_close(repo);
6015 got_worktree_close(worktree);
6018 free(gitconfig_path);
6025 usage_cherrypick(void)
6027 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6031 static const struct got_error *
6032 cmd_cherrypick(int argc, char *argv[])
6034 const struct got_error *error = NULL;
6035 struct got_worktree *worktree = NULL;
6036 struct got_repository *repo = NULL;
6037 char *cwd = NULL, *commit_id_str = NULL;
6038 struct got_object_id *commit_id = NULL;
6039 struct got_commit_object *commit = NULL;
6040 struct got_object_qid *pid;
6041 struct got_reference *head_ref = NULL;
6042 int ch, did_something = 0;
6044 while ((ch = getopt(argc, argv, "")) != -1) {
6056 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6057 "unveil", NULL) == -1)
6063 cwd = getcwd(NULL, 0);
6065 error = got_error_from_errno("getcwd");
6068 error = got_worktree_open(&worktree, cwd);
6072 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6077 error = apply_unveil(got_repo_get_path(repo), 0,
6078 got_worktree_get_root_path(worktree));
6082 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6083 GOT_OBJ_TYPE_COMMIT, repo);
6084 if (error != NULL) {
6085 struct got_reference *ref;
6086 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6088 error = got_ref_open(&ref, repo, argv[0], 0);
6091 error = got_ref_resolve(&commit_id, repo, ref);
6096 error = got_object_id_str(&commit_id_str, commit_id);
6100 error = got_ref_open(&head_ref, repo,
6101 got_worktree_get_head_ref_name(worktree), 0);
6105 error = check_same_branch(commit_id, head_ref, NULL, repo);
6107 if (error->code != GOT_ERR_ANCESTRY)
6111 error = got_error(GOT_ERR_SAME_BRANCH);
6115 error = got_object_open_as_commit(&commit, repo, commit_id);
6118 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6119 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6120 commit_id, repo, update_progress, &did_something, check_cancelled,
6126 printf("Merged commit %s\n", commit_id_str);
6129 got_object_commit_close(commit);
6130 free(commit_id_str);
6132 got_ref_close(head_ref);
6134 got_worktree_close(worktree);
6136 got_repo_close(repo);
6143 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6147 static const struct got_error *
6148 cmd_backout(int argc, char *argv[])
6150 const struct got_error *error = NULL;
6151 struct got_worktree *worktree = NULL;
6152 struct got_repository *repo = NULL;
6153 char *cwd = NULL, *commit_id_str = NULL;
6154 struct got_object_id *commit_id = NULL;
6155 struct got_commit_object *commit = NULL;
6156 struct got_object_qid *pid;
6157 struct got_reference *head_ref = NULL;
6158 int ch, did_something = 0;
6160 while ((ch = getopt(argc, argv, "")) != -1) {
6172 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6173 "unveil", NULL) == -1)
6179 cwd = getcwd(NULL, 0);
6181 error = got_error_from_errno("getcwd");
6184 error = got_worktree_open(&worktree, cwd);
6188 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6193 error = apply_unveil(got_repo_get_path(repo), 0,
6194 got_worktree_get_root_path(worktree));
6198 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6199 GOT_OBJ_TYPE_COMMIT, repo);
6200 if (error != NULL) {
6201 struct got_reference *ref;
6202 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6204 error = got_ref_open(&ref, repo, argv[0], 0);
6207 error = got_ref_resolve(&commit_id, repo, ref);
6212 error = got_object_id_str(&commit_id_str, commit_id);
6216 error = got_ref_open(&head_ref, repo,
6217 got_worktree_get_head_ref_name(worktree), 0);
6221 error = check_same_branch(commit_id, head_ref, NULL, repo);
6225 error = got_object_open_as_commit(&commit, repo, commit_id);
6228 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6230 error = got_error(GOT_ERR_ROOT_COMMIT);
6234 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6235 update_progress, &did_something, check_cancelled, NULL);
6240 printf("Backed out commit %s\n", commit_id_str);
6243 got_object_commit_close(commit);
6244 free(commit_id_str);
6246 got_ref_close(head_ref);
6248 got_worktree_close(worktree);
6250 got_repo_close(repo);
6257 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6263 trim_logmsg(char *logmsg, int limit)
6268 len = strlen(logmsg);
6272 nl = strchr(logmsg, '\n');
6277 static const struct got_error *
6278 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6280 const struct got_error *err;
6281 char *logmsg0 = NULL;
6284 err = got_object_commit_get_logmsg(&logmsg0, commit);
6289 while (isspace((unsigned char)s[0]))
6292 *logmsg = strdup(s);
6293 if (*logmsg == NULL) {
6294 err = got_error_from_errno("strdup");
6298 trim_logmsg(*logmsg, limit);
6304 static const struct got_error *
6305 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6307 const struct got_error *err;
6308 struct got_commit_object *commit = NULL;
6309 char *id_str = NULL, *logmsg = NULL;
6311 err = got_object_open_as_commit(&commit, repo, id);
6315 err = got_object_id_str(&id_str, id);
6321 err = get_short_logmsg(&logmsg, 42, commit);
6325 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6328 got_object_commit_close(commit);
6333 static const struct got_error *
6334 show_rebase_progress(struct got_commit_object *commit,
6335 struct got_object_id *old_id, struct got_object_id *new_id)
6337 const struct got_error *err;
6338 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6340 err = got_object_id_str(&old_id_str, old_id);
6345 err = got_object_id_str(&new_id_str, new_id);
6350 old_id_str[12] = '\0';
6352 new_id_str[12] = '\0';
6354 err = get_short_logmsg(&logmsg, 42, commit);
6358 printf("%s -> %s: %s\n", old_id_str,
6359 new_id_str ? new_id_str : "no-op change", logmsg);
6367 static const struct got_error *
6368 rebase_progress(void *arg, unsigned char status, const char *path)
6370 unsigned char *rebase_status = arg;
6372 while (path[0] == '/')
6374 printf("%c %s\n", status, path);
6376 if (*rebase_status == GOT_STATUS_CONFLICT)
6378 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6379 *rebase_status = status;
6383 static const struct got_error *
6384 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6385 struct got_reference *branch, struct got_reference *new_base_branch,
6386 struct got_reference *tmp_branch, struct got_repository *repo)
6388 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6389 return got_worktree_rebase_complete(worktree, fileindex,
6390 new_base_branch, tmp_branch, branch, repo);
6393 static const struct got_error *
6394 rebase_commit(struct got_pathlist_head *merged_paths,
6395 struct got_worktree *worktree, struct got_fileindex *fileindex,
6396 struct got_reference *tmp_branch,
6397 struct got_object_id *commit_id, struct got_repository *repo)
6399 const struct got_error *error;
6400 struct got_commit_object *commit;
6401 struct got_object_id *new_commit_id;
6403 error = got_object_open_as_commit(&commit, repo, commit_id);
6407 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6408 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6410 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6412 error = show_rebase_progress(commit, commit_id, NULL);
6414 error = show_rebase_progress(commit, commit_id, new_commit_id);
6415 free(new_commit_id);
6418 got_object_commit_close(commit);
6422 struct check_path_prefix_arg {
6423 const char *path_prefix;
6428 static const struct got_error *
6429 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6430 struct got_blob_object *blob2, struct got_object_id *id1,
6431 struct got_object_id *id2, const char *path1, const char *path2,
6432 mode_t mode1, mode_t mode2, struct got_repository *repo)
6434 struct check_path_prefix_arg *a = arg;
6436 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6437 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6438 return got_error(a->errcode);
6443 static const struct got_error *
6444 check_path_prefix(struct got_object_id *parent_id,
6445 struct got_object_id *commit_id, const char *path_prefix,
6446 int errcode, struct got_repository *repo)
6448 const struct got_error *err;
6449 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6450 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6451 struct check_path_prefix_arg cpp_arg;
6453 if (got_path_is_root_dir(path_prefix))
6456 err = got_object_open_as_commit(&commit, repo, commit_id);
6460 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6464 err = got_object_open_as_tree(&tree1, repo,
6465 got_object_commit_get_tree_id(parent_commit));
6469 err = got_object_open_as_tree(&tree2, repo,
6470 got_object_commit_get_tree_id(commit));
6474 cpp_arg.path_prefix = path_prefix;
6475 while (cpp_arg.path_prefix[0] == '/')
6476 cpp_arg.path_prefix++;
6477 cpp_arg.len = strlen(cpp_arg.path_prefix);
6478 cpp_arg.errcode = errcode;
6479 err = got_diff_tree(tree1, tree2, "", "", repo,
6480 check_path_prefix_in_diff, &cpp_arg, 0);
6483 got_object_tree_close(tree1);
6485 got_object_tree_close(tree2);
6487 got_object_commit_close(commit);
6489 got_object_commit_close(parent_commit);
6493 static const struct got_error *
6494 collect_commits(struct got_object_id_queue *commits,
6495 struct got_object_id *initial_commit_id,
6496 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6497 const char *path_prefix, int path_prefix_errcode,
6498 struct got_repository *repo)
6500 const struct got_error *err = NULL;
6501 struct got_commit_graph *graph = NULL;
6502 struct got_object_id *parent_id = NULL;
6503 struct got_object_qid *qid;
6504 struct got_object_id *commit_id = initial_commit_id;
6506 err = got_commit_graph_open(&graph, "/", 1);
6510 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6511 check_cancelled, NULL);
6514 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6515 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6516 check_cancelled, NULL);
6518 if (err->code == GOT_ERR_ITER_COMPLETED) {
6519 err = got_error_msg(GOT_ERR_ANCESTRY,
6520 "ran out of commits to rebase before "
6521 "youngest common ancestor commit has "
6526 err = check_path_prefix(parent_id, commit_id,
6527 path_prefix, path_prefix_errcode, repo);
6531 err = got_object_qid_alloc(&qid, commit_id);
6534 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6535 commit_id = parent_id;
6539 got_commit_graph_close(graph);
6543 static const struct got_error *
6544 cmd_rebase(int argc, char *argv[])
6546 const struct got_error *error = NULL;
6547 struct got_worktree *worktree = NULL;
6548 struct got_repository *repo = NULL;
6549 struct got_fileindex *fileindex = NULL;
6551 struct got_reference *branch = NULL;
6552 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6553 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6554 struct got_object_id *resume_commit_id = NULL;
6555 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6556 struct got_commit_object *commit = NULL;
6557 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6558 int histedit_in_progress = 0;
6559 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6560 struct got_object_id_queue commits;
6561 struct got_pathlist_head merged_paths;
6562 const struct got_object_id_queue *parent_ids;
6563 struct got_object_qid *qid, *pid;
6565 SIMPLEQ_INIT(&commits);
6566 TAILQ_INIT(&merged_paths);
6568 while ((ch = getopt(argc, argv, "ac")) != -1) {
6574 continue_rebase = 1;
6586 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6587 "unveil", NULL) == -1)
6590 if (abort_rebase && continue_rebase)
6592 else if (abort_rebase || continue_rebase) {
6595 } else if (argc != 1)
6598 cwd = getcwd(NULL, 0);
6600 error = got_error_from_errno("getcwd");
6603 error = got_worktree_open(&worktree, cwd);
6607 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6612 error = apply_unveil(got_repo_get_path(repo), 0,
6613 got_worktree_get_root_path(worktree));
6617 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6621 if (histedit_in_progress) {
6622 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6626 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6632 if (!rebase_in_progress) {
6633 error = got_error(GOT_ERR_NOT_REBASING);
6636 error = got_worktree_rebase_continue(&resume_commit_id,
6637 &new_base_branch, &tmp_branch, &branch, &fileindex,
6641 printf("Switching work tree to %s\n",
6642 got_ref_get_symref_target(new_base_branch));
6643 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6644 new_base_branch, update_progress, &did_something);
6647 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6648 goto done; /* nothing else to do */
6651 if (continue_rebase) {
6652 if (!rebase_in_progress) {
6653 error = got_error(GOT_ERR_NOT_REBASING);
6656 error = got_worktree_rebase_continue(&resume_commit_id,
6657 &new_base_branch, &tmp_branch, &branch, &fileindex,
6662 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6663 resume_commit_id, repo);
6667 yca_id = got_object_id_dup(resume_commit_id);
6668 if (yca_id == NULL) {
6669 error = got_error_from_errno("got_object_id_dup");
6673 error = got_ref_open(&branch, repo, argv[0], 0);
6678 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6682 if (!continue_rebase) {
6683 struct got_object_id *base_commit_id;
6685 base_commit_id = got_worktree_get_base_commit_id(worktree);
6686 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6687 base_commit_id, branch_head_commit_id, repo,
6688 check_cancelled, NULL);
6691 if (yca_id == NULL) {
6692 error = got_error_msg(GOT_ERR_ANCESTRY,
6693 "specified branch shares no common ancestry "
6694 "with work tree's branch");
6698 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6700 if (error->code != GOT_ERR_ANCESTRY)
6704 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6705 "specified branch resolves to a commit which "
6706 "is already contained in work tree's branch");
6709 error = got_worktree_rebase_prepare(&new_base_branch,
6710 &tmp_branch, &fileindex, worktree, branch, repo);
6715 commit_id = branch_head_commit_id;
6716 error = got_object_open_as_commit(&commit, repo, commit_id);
6720 parent_ids = got_object_commit_get_parent_ids(commit);
6721 pid = SIMPLEQ_FIRST(parent_ids);
6723 if (!continue_rebase) {
6725 error = got_worktree_rebase_abort(worktree, fileindex,
6726 repo, new_base_branch, update_progress,
6730 printf("Rebase of %s aborted\n",
6731 got_ref_get_name(branch));
6733 error = got_error(GOT_ERR_EMPTY_REBASE);
6736 error = collect_commits(&commits, commit_id, pid->id,
6737 yca_id, got_worktree_get_path_prefix(worktree),
6738 GOT_ERR_REBASE_PATH, repo);
6739 got_object_commit_close(commit);
6744 if (SIMPLEQ_EMPTY(&commits)) {
6745 if (continue_rebase) {
6746 error = rebase_complete(worktree, fileindex,
6747 branch, new_base_branch, tmp_branch, repo);
6750 /* Fast-forward the reference of the branch. */
6751 struct got_object_id *new_head_commit_id;
6753 error = got_ref_resolve(&new_head_commit_id, repo,
6757 error = got_object_id_str(&id_str, new_head_commit_id);
6758 printf("Forwarding %s to commit %s\n",
6759 got_ref_get_name(branch), id_str);
6761 error = got_ref_change_ref(branch,
6762 new_head_commit_id);
6769 SIMPLEQ_FOREACH(qid, &commits, entry) {
6770 commit_id = qid->id;
6771 parent_id = pid ? pid->id : yca_id;
6774 error = got_worktree_rebase_merge_files(&merged_paths,
6775 worktree, fileindex, parent_id, commit_id, repo,
6776 rebase_progress, &rebase_status, check_cancelled, NULL);
6780 if (rebase_status == GOT_STATUS_CONFLICT) {
6781 error = show_rebase_merge_conflict(qid->id, repo);
6784 got_worktree_rebase_pathlist_free(&merged_paths);
6788 error = rebase_commit(&merged_paths, worktree, fileindex,
6789 tmp_branch, commit_id, repo);
6790 got_worktree_rebase_pathlist_free(&merged_paths);
6795 if (rebase_status == GOT_STATUS_CONFLICT) {
6796 error = got_worktree_rebase_postpone(worktree, fileindex);
6799 error = got_error_msg(GOT_ERR_CONFLICTS,
6800 "conflicts must be resolved before rebasing can continue");
6802 error = rebase_complete(worktree, fileindex, branch,
6803 new_base_branch, tmp_branch, repo);
6805 got_object_id_queue_free(&commits);
6806 free(branch_head_commit_id);
6807 free(resume_commit_id);
6810 got_object_commit_close(commit);
6812 got_ref_close(branch);
6813 if (new_base_branch)
6814 got_ref_close(new_base_branch);
6816 got_ref_close(tmp_branch);
6818 got_worktree_close(worktree);
6820 got_repo_close(repo);
6825 usage_histedit(void)
6827 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6832 #define GOT_HISTEDIT_PICK 'p'
6833 #define GOT_HISTEDIT_EDIT 'e'
6834 #define GOT_HISTEDIT_FOLD 'f'
6835 #define GOT_HISTEDIT_DROP 'd'
6836 #define GOT_HISTEDIT_MESG 'm'
6838 static struct got_histedit_cmd {
6842 } got_histedit_cmds[] = {
6843 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6844 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6845 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6847 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6848 { GOT_HISTEDIT_MESG, "mesg",
6849 "single-line log message for commit above (open editor if empty)" },
6852 struct got_histedit_list_entry {
6853 TAILQ_ENTRY(got_histedit_list_entry) entry;
6854 struct got_object_id *commit_id;
6855 const struct got_histedit_cmd *cmd;
6858 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6860 static const struct got_error *
6861 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6862 FILE *f, struct got_repository *repo)
6864 const struct got_error *err = NULL;
6865 char *logmsg = NULL, *id_str = NULL;
6866 struct got_commit_object *commit = NULL;
6869 err = got_object_open_as_commit(&commit, repo, commit_id);
6873 err = get_short_logmsg(&logmsg, 34, commit);
6877 err = got_object_id_str(&id_str, commit_id);
6881 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6883 err = got_ferror(f, GOT_ERR_IO);
6886 got_object_commit_close(commit);
6892 static const struct got_error *
6893 histedit_write_commit_list(struct got_object_id_queue *commits,
6894 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6896 const struct got_error *err = NULL;
6897 struct got_object_qid *qid;
6899 if (SIMPLEQ_EMPTY(commits))
6900 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6902 SIMPLEQ_FOREACH(qid, commits, entry) {
6903 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6907 if (edit_logmsg_only) {
6908 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6910 err = got_ferror(f, GOT_ERR_IO);
6919 static const struct got_error *
6920 write_cmd_list(FILE *f, const char *branch_name,
6921 struct got_object_id_queue *commits)
6923 const struct got_error *err = NULL;
6926 struct got_object_qid *qid;
6928 qid = SIMPLEQ_FIRST(commits);
6929 err = got_object_id_str(&id_str, qid->id);
6934 "# Editing the history of branch '%s' starting at\n"
6936 "# Commits will be processed in order from top to "
6937 "bottom of this file.\n", branch_name, id_str);
6939 err = got_ferror(f, GOT_ERR_IO);
6943 n = fprintf(f, "# Available histedit commands:\n");
6945 err = got_ferror(f, GOT_ERR_IO);
6949 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6950 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6951 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6954 err = got_ferror(f, GOT_ERR_IO);
6963 static const struct got_error *
6964 histedit_syntax_error(int lineno)
6966 static char msg[42];
6969 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6971 if (ret == -1 || ret >= sizeof(msg))
6972 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6974 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6977 static const struct got_error *
6978 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6979 char *logmsg, struct got_repository *repo)
6981 const struct got_error *err;
6982 struct got_commit_object *folded_commit = NULL;
6983 char *id_str, *folded_logmsg = NULL;
6985 err = got_object_id_str(&id_str, hle->commit_id);
6989 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6993 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6996 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6997 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6998 folded_logmsg) == -1) {
6999 err = got_error_from_errno("asprintf");
7003 got_object_commit_close(folded_commit);
7005 free(folded_logmsg);
7009 static struct got_histedit_list_entry *
7010 get_folded_commits(struct got_histedit_list_entry *hle)
7012 struct got_histedit_list_entry *prev, *folded = NULL;
7014 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7015 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7016 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7017 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7019 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7025 static const struct got_error *
7026 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7027 struct got_repository *repo)
7029 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7030 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7031 const struct got_error *err = NULL;
7032 struct got_commit_object *commit = NULL;
7034 struct got_histedit_list_entry *folded = NULL;
7036 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7040 folded = get_folded_commits(hle);
7042 while (folded != hle) {
7043 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7044 folded = TAILQ_NEXT(folded, entry);
7047 err = append_folded_commit_msg(&new_msg, folded,
7053 folded = TAILQ_NEXT(folded, entry);
7057 err = got_object_id_str(&id_str, hle->commit_id);
7060 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7063 if (asprintf(&new_msg,
7064 "%s\n# original log message of commit %s: %s",
7065 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7066 err = got_error_from_errno("asprintf");
7072 err = got_object_id_str(&id_str, hle->commit_id);
7076 err = got_opentemp_named_fd(&logmsg_path, &fd,
7077 GOT_TMPDIR_STR "/got-logmsg");
7081 dprintf(fd, logmsg);
7084 err = get_editor(&editor);
7088 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7090 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7092 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7095 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7096 err = got_error_from_errno2("unlink", logmsg_path);
7102 got_object_commit_close(commit);
7106 static const struct got_error *
7107 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7108 FILE *f, struct got_repository *repo)
7110 const struct got_error *err = NULL;
7111 char *line = NULL, *p, *end;
7115 const struct got_histedit_cmd *cmd;
7116 struct got_object_id *commit_id = NULL;
7117 struct got_histedit_list_entry *hle = NULL;
7120 len = getline(&line, &size, f);
7122 const struct got_error *getline_err;
7125 getline_err = got_error_from_errno("getline");
7126 err = got_ferror(f, getline_err->code);
7131 while (isspace((unsigned char)p[0]))
7133 if (p[0] == '#' || p[0] == '\0') {
7139 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7140 cmd = &got_histedit_cmds[i];
7141 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7142 isspace((unsigned char)p[strlen(cmd->name)])) {
7143 p += strlen(cmd->name);
7146 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7151 if (i == nitems(got_histedit_cmds)) {
7152 err = histedit_syntax_error(lineno);
7155 while (isspace((unsigned char)p[0]))
7157 if (cmd->code == GOT_HISTEDIT_MESG) {
7158 if (hle == NULL || hle->logmsg != NULL) {
7159 err = got_error(GOT_ERR_HISTEDIT_CMD);
7163 err = histedit_edit_logmsg(hle, repo);
7167 hle->logmsg = strdup(p);
7168 if (hle->logmsg == NULL) {
7169 err = got_error_from_errno("strdup");
7178 while (end[0] && !isspace((unsigned char)end[0]))
7182 err = got_object_resolve_id_str(&commit_id, repo, p);
7184 /* override error code */
7185 err = histedit_syntax_error(lineno);
7189 hle = malloc(sizeof(*hle));
7191 err = got_error_from_errno("malloc");
7195 hle->commit_id = commit_id;
7200 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7208 static const struct got_error *
7209 histedit_check_script(struct got_histedit_list *histedit_cmds,
7210 struct got_object_id_queue *commits, struct got_repository *repo)
7212 const struct got_error *err = NULL;
7213 struct got_object_qid *qid;
7214 struct got_histedit_list_entry *hle;
7215 static char msg[92];
7218 if (TAILQ_EMPTY(histedit_cmds))
7219 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7220 "histedit script contains no commands");
7221 if (SIMPLEQ_EMPTY(commits))
7222 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7224 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7225 struct got_histedit_list_entry *hle2;
7226 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7229 if (got_object_id_cmp(hle->commit_id,
7230 hle2->commit_id) != 0)
7232 err = got_object_id_str(&id_str, hle->commit_id);
7235 snprintf(msg, sizeof(msg), "commit %s is listed "
7236 "more than once in histedit script", id_str);
7238 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7242 SIMPLEQ_FOREACH(qid, commits, entry) {
7243 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7244 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7248 err = got_object_id_str(&id_str, qid->id);
7251 snprintf(msg, sizeof(msg),
7252 "commit %s missing from histedit script", id_str);
7254 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7258 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7259 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7260 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7261 "last commit in histedit script cannot be folded");
7266 static const struct got_error *
7267 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7268 const char *path, struct got_object_id_queue *commits,
7269 struct got_repository *repo)
7271 const struct got_error *err = NULL;
7275 err = get_editor(&editor);
7279 if (spawn_editor(editor, path) == -1) {
7280 err = got_error_from_errno("failed spawning editor");
7284 f = fopen(path, "r");
7286 err = got_error_from_errno("fopen");
7289 err = histedit_parse_list(histedit_cmds, f, repo);
7293 err = histedit_check_script(histedit_cmds, commits, repo);
7295 if (f && fclose(f) != 0 && err == NULL)
7296 err = got_error_from_errno("fclose");
7301 static const struct got_error *
7302 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7303 struct got_object_id_queue *, const char *, const char *,
7304 struct got_repository *);
7306 static const struct got_error *
7307 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7308 struct got_object_id_queue *commits, const char *branch_name,
7309 int edit_logmsg_only, struct got_repository *repo)
7311 const struct got_error *err;
7315 err = got_opentemp_named(&path, &f, "got-histedit");
7319 err = write_cmd_list(f, branch_name, commits);
7323 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7327 if (edit_logmsg_only) {
7329 err = histedit_parse_list(histedit_cmds, f, repo);
7331 if (fclose(f) != 0) {
7332 err = got_error_from_errno("fclose");
7336 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7338 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7339 err->code != GOT_ERR_HISTEDIT_CMD)
7341 err = histedit_edit_list_retry(histedit_cmds, err,
7342 commits, path, branch_name, repo);
7346 if (f && fclose(f) != 0 && err == NULL)
7347 err = got_error_from_errno("fclose");
7348 if (path && unlink(path) != 0 && err == NULL)
7349 err = got_error_from_errno2("unlink", path);
7354 static const struct got_error *
7355 histedit_save_list(struct got_histedit_list *histedit_cmds,
7356 struct got_worktree *worktree, struct got_repository *repo)
7358 const struct got_error *err = NULL;
7361 struct got_histedit_list_entry *hle;
7362 struct got_commit_object *commit = NULL;
7364 err = got_worktree_get_histedit_script_path(&path, worktree);
7368 f = fopen(path, "w");
7370 err = got_error_from_errno2("fopen", path);
7373 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7374 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7380 int n = fprintf(f, "%c %s\n",
7381 GOT_HISTEDIT_MESG, hle->logmsg);
7383 err = got_ferror(f, GOT_ERR_IO);
7389 if (f && fclose(f) != 0 && err == NULL)
7390 err = got_error_from_errno("fclose");
7393 got_object_commit_close(commit);
7398 histedit_free_list(struct got_histedit_list *histedit_cmds)
7400 struct got_histedit_list_entry *hle;
7402 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7403 TAILQ_REMOVE(histedit_cmds, hle, entry);
7408 static const struct got_error *
7409 histedit_load_list(struct got_histedit_list *histedit_cmds,
7410 const char *path, struct got_repository *repo)
7412 const struct got_error *err = NULL;
7415 f = fopen(path, "r");
7417 err = got_error_from_errno2("fopen", path);
7421 err = histedit_parse_list(histedit_cmds, f, repo);
7423 if (f && fclose(f) != 0 && err == NULL)
7424 err = got_error_from_errno("fclose");
7428 static const struct got_error *
7429 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7430 const struct got_error *edit_err, struct got_object_id_queue *commits,
7431 const char *path, const char *branch_name, struct got_repository *repo)
7433 const struct got_error *err = NULL, *prev_err = edit_err;
7436 while (resp != 'c' && resp != 'r' && resp != 'a') {
7437 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7438 "or (a)bort: ", getprogname(), prev_err->msg);
7443 histedit_free_list(histedit_cmds);
7444 err = histedit_run_editor(histedit_cmds, path, commits,
7447 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7448 err->code != GOT_ERR_HISTEDIT_CMD)
7455 } else if (resp == 'r') {
7456 histedit_free_list(histedit_cmds);
7457 err = histedit_edit_script(histedit_cmds,
7458 commits, branch_name, 0, repo);
7460 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7461 err->code != GOT_ERR_HISTEDIT_CMD)
7468 } else if (resp == 'a') {
7469 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7472 printf("invalid response '%c'\n", resp);
7478 static const struct got_error *
7479 histedit_complete(struct got_worktree *worktree,
7480 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7481 struct got_reference *branch, struct got_repository *repo)
7483 printf("Switching work tree to %s\n",
7484 got_ref_get_symref_target(branch));
7485 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7489 static const struct got_error *
7490 show_histedit_progress(struct got_commit_object *commit,
7491 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7493 const struct got_error *err;
7494 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7496 err = got_object_id_str(&old_id_str, hle->commit_id);
7501 err = got_object_id_str(&new_id_str, new_id);
7506 old_id_str[12] = '\0';
7508 new_id_str[12] = '\0';
7511 logmsg = strdup(hle->logmsg);
7512 if (logmsg == NULL) {
7513 err = got_error_from_errno("strdup");
7516 trim_logmsg(logmsg, 42);
7518 err = get_short_logmsg(&logmsg, 42, commit);
7523 switch (hle->cmd->code) {
7524 case GOT_HISTEDIT_PICK:
7525 case GOT_HISTEDIT_EDIT:
7526 printf("%s -> %s: %s\n", old_id_str,
7527 new_id_str ? new_id_str : "no-op change", logmsg);
7529 case GOT_HISTEDIT_DROP:
7530 case GOT_HISTEDIT_FOLD:
7531 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7543 static const struct got_error *
7544 histedit_commit(struct got_pathlist_head *merged_paths,
7545 struct got_worktree *worktree, struct got_fileindex *fileindex,
7546 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7547 struct got_repository *repo)
7549 const struct got_error *err;
7550 struct got_commit_object *commit;
7551 struct got_object_id *new_commit_id;
7553 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7554 && hle->logmsg == NULL) {
7555 err = histedit_edit_logmsg(hle, repo);
7560 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7564 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7565 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7568 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7570 err = show_histedit_progress(commit, hle, NULL);
7572 err = show_histedit_progress(commit, hle, new_commit_id);
7573 free(new_commit_id);
7576 got_object_commit_close(commit);
7580 static const struct got_error *
7581 histedit_skip_commit(struct got_histedit_list_entry *hle,
7582 struct got_worktree *worktree, struct got_repository *repo)
7584 const struct got_error *error;
7585 struct got_commit_object *commit;
7587 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7592 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7596 error = show_histedit_progress(commit, hle, NULL);
7597 got_object_commit_close(commit);
7601 static const struct got_error *
7602 check_local_changes(void *arg, unsigned char status,
7603 unsigned char staged_status, const char *path,
7604 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7605 struct got_object_id *commit_id, int dirfd, const char *de_name)
7607 int *have_local_changes = arg;
7610 case GOT_STATUS_ADD:
7611 case GOT_STATUS_DELETE:
7612 case GOT_STATUS_MODIFY:
7613 case GOT_STATUS_CONFLICT:
7614 *have_local_changes = 1;
7615 return got_error(GOT_ERR_CANCELLED);
7620 switch (staged_status) {
7621 case GOT_STATUS_ADD:
7622 case GOT_STATUS_DELETE:
7623 case GOT_STATUS_MODIFY:
7624 *have_local_changes = 1;
7625 return got_error(GOT_ERR_CANCELLED);
7633 static const struct got_error *
7634 cmd_histedit(int argc, char *argv[])
7636 const struct got_error *error = NULL;
7637 struct got_worktree *worktree = NULL;
7638 struct got_fileindex *fileindex = NULL;
7639 struct got_repository *repo = NULL;
7641 struct got_reference *branch = NULL;
7642 struct got_reference *tmp_branch = NULL;
7643 struct got_object_id *resume_commit_id = NULL;
7644 struct got_object_id *base_commit_id = NULL;
7645 struct got_object_id *head_commit_id = NULL;
7646 struct got_commit_object *commit = NULL;
7647 int ch, rebase_in_progress = 0, did_something;
7648 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7649 int edit_logmsg_only = 0;
7650 const char *edit_script_path = NULL;
7651 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7652 struct got_object_id_queue commits;
7653 struct got_pathlist_head merged_paths;
7654 const struct got_object_id_queue *parent_ids;
7655 struct got_object_qid *pid;
7656 struct got_histedit_list histedit_cmds;
7657 struct got_histedit_list_entry *hle;
7659 SIMPLEQ_INIT(&commits);
7660 TAILQ_INIT(&histedit_cmds);
7661 TAILQ_INIT(&merged_paths);
7663 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7672 edit_script_path = optarg;
7675 edit_logmsg_only = 1;
7687 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7688 "unveil", NULL) == -1)
7691 if (abort_edit && continue_edit)
7692 errx(1, "histedit's -a and -c options are mutually exclusive");
7693 if (edit_script_path && edit_logmsg_only)
7694 errx(1, "histedit's -F and -m options are mutually exclusive");
7695 if (abort_edit && edit_logmsg_only)
7696 errx(1, "histedit's -a and -m options are mutually exclusive");
7697 if (continue_edit && edit_logmsg_only)
7698 errx(1, "histedit's -c and -m options are mutually exclusive");
7703 * This command cannot apply unveil(2) in all cases because the
7704 * user may choose to run an editor to edit the histedit script
7705 * and to edit individual commit log messages.
7706 * unveil(2) traverses exec(2); if an editor is used we have to
7707 * apply unveil after edit script and log messages have been written.
7708 * XXX TODO: Make use of unveil(2) where possible.
7711 cwd = getcwd(NULL, 0);
7713 error = got_error_from_errno("getcwd");
7716 error = got_worktree_open(&worktree, cwd);
7720 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7725 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7728 if (rebase_in_progress) {
7729 error = got_error(GOT_ERR_REBASING);
7733 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7737 if (edit_in_progress && edit_logmsg_only) {
7738 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7739 "histedit operation is in progress in this "
7740 "work tree and must be continued or aborted "
7741 "before the -m option can be used");
7745 if (edit_in_progress && abort_edit) {
7746 error = got_worktree_histedit_continue(&resume_commit_id,
7747 &tmp_branch, &branch, &base_commit_id, &fileindex,
7751 printf("Switching work tree to %s\n",
7752 got_ref_get_symref_target(branch));
7753 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7754 branch, base_commit_id, update_progress, &did_something);
7757 printf("Histedit of %s aborted\n",
7758 got_ref_get_symref_target(branch));
7759 goto done; /* nothing else to do */
7760 } else if (abort_edit) {
7761 error = got_error(GOT_ERR_NOT_HISTEDIT);
7765 if (continue_edit) {
7768 if (!edit_in_progress) {
7769 error = got_error(GOT_ERR_NOT_HISTEDIT);
7773 error = got_worktree_get_histedit_script_path(&path, worktree);
7777 error = histedit_load_list(&histedit_cmds, path, repo);
7782 error = got_worktree_histedit_continue(&resume_commit_id,
7783 &tmp_branch, &branch, &base_commit_id, &fileindex,
7788 error = got_ref_resolve(&head_commit_id, repo, branch);
7792 error = got_object_open_as_commit(&commit, repo,
7796 parent_ids = got_object_commit_get_parent_ids(commit);
7797 pid = SIMPLEQ_FIRST(parent_ids);
7799 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7802 error = collect_commits(&commits, head_commit_id, pid->id,
7803 base_commit_id, got_worktree_get_path_prefix(worktree),
7804 GOT_ERR_HISTEDIT_PATH, repo);
7805 got_object_commit_close(commit);
7810 if (edit_in_progress) {
7811 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7815 error = got_ref_open(&branch, repo,
7816 got_worktree_get_head_ref_name(worktree), 0);
7820 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7821 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7822 "will not edit commit history of a branch outside "
7823 "the \"refs/heads/\" reference namespace");
7827 error = got_ref_resolve(&head_commit_id, repo, branch);
7828 got_ref_close(branch);
7833 error = got_object_open_as_commit(&commit, repo,
7837 parent_ids = got_object_commit_get_parent_ids(commit);
7838 pid = SIMPLEQ_FIRST(parent_ids);
7840 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7843 error = collect_commits(&commits, head_commit_id, pid->id,
7844 got_worktree_get_base_commit_id(worktree),
7845 got_worktree_get_path_prefix(worktree),
7846 GOT_ERR_HISTEDIT_PATH, repo);
7847 got_object_commit_close(commit);
7852 if (SIMPLEQ_EMPTY(&commits)) {
7853 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7857 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7858 &base_commit_id, &fileindex, worktree, repo);
7862 if (edit_script_path) {
7863 error = histedit_load_list(&histedit_cmds,
7864 edit_script_path, repo);
7866 got_worktree_histedit_abort(worktree, fileindex,
7867 repo, branch, base_commit_id,
7868 update_progress, &did_something);
7872 const char *branch_name;
7873 branch_name = got_ref_get_symref_target(branch);
7874 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7876 error = histedit_edit_script(&histedit_cmds, &commits,
7877 branch_name, edit_logmsg_only, repo);
7879 got_worktree_histedit_abort(worktree, fileindex,
7880 repo, branch, base_commit_id,
7881 update_progress, &did_something);
7887 error = histedit_save_list(&histedit_cmds, worktree,
7890 got_worktree_histedit_abort(worktree, fileindex,
7891 repo, branch, base_commit_id,
7892 update_progress, &did_something);
7898 error = histedit_check_script(&histedit_cmds, &commits, repo);
7902 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7903 if (resume_commit_id) {
7904 if (got_object_id_cmp(hle->commit_id,
7905 resume_commit_id) != 0)
7908 resume_commit_id = NULL;
7909 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7910 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7911 error = histedit_skip_commit(hle, worktree,
7916 struct got_pathlist_head paths;
7917 int have_changes = 0;
7920 error = got_pathlist_append(&paths, "", NULL);
7923 error = got_worktree_status(worktree, &paths,
7924 repo, check_local_changes, &have_changes,
7925 check_cancelled, NULL);
7926 got_pathlist_free(&paths);
7928 if (error->code != GOT_ERR_CANCELLED)
7930 if (sigint_received || sigpipe_received)
7934 error = histedit_commit(NULL, worktree,
7935 fileindex, tmp_branch, hle, repo);
7939 error = got_object_open_as_commit(
7940 &commit, repo, hle->commit_id);
7943 error = show_histedit_progress(commit,
7945 got_object_commit_close(commit);
7954 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7955 error = histedit_skip_commit(hle, worktree, repo);
7961 error = got_object_open_as_commit(&commit, repo,
7965 parent_ids = got_object_commit_get_parent_ids(commit);
7966 pid = SIMPLEQ_FIRST(parent_ids);
7968 error = got_worktree_histedit_merge_files(&merged_paths,
7969 worktree, fileindex, pid->id, hle->commit_id, repo,
7970 rebase_progress, &rebase_status, check_cancelled, NULL);
7973 got_object_commit_close(commit);
7976 if (rebase_status == GOT_STATUS_CONFLICT) {
7977 error = show_rebase_merge_conflict(hle->commit_id,
7981 got_worktree_rebase_pathlist_free(&merged_paths);
7985 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7987 error = got_object_id_str(&id_str, hle->commit_id);
7990 printf("Stopping histedit for amending commit %s\n",
7993 got_worktree_rebase_pathlist_free(&merged_paths);
7994 error = got_worktree_histedit_postpone(worktree,
7999 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8000 error = histedit_skip_commit(hle, worktree, repo);
8006 error = histedit_commit(&merged_paths, worktree, fileindex,
8007 tmp_branch, hle, repo);
8008 got_worktree_rebase_pathlist_free(&merged_paths);
8013 if (rebase_status == GOT_STATUS_CONFLICT) {
8014 error = got_worktree_histedit_postpone(worktree, fileindex);
8017 error = got_error_msg(GOT_ERR_CONFLICTS,
8018 "conflicts must be resolved before histedit can continue");
8020 error = histedit_complete(worktree, fileindex, tmp_branch,
8023 got_object_id_queue_free(&commits);
8024 histedit_free_list(&histedit_cmds);
8025 free(head_commit_id);
8026 free(base_commit_id);
8027 free(resume_commit_id);
8029 got_object_commit_close(commit);
8031 got_ref_close(branch);
8033 got_ref_close(tmp_branch);
8035 got_worktree_close(worktree);
8037 got_repo_close(repo);
8042 usage_integrate(void)
8044 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8048 static const struct got_error *
8049 cmd_integrate(int argc, char *argv[])
8051 const struct got_error *error = NULL;
8052 struct got_repository *repo = NULL;
8053 struct got_worktree *worktree = NULL;
8054 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8055 const char *branch_arg = NULL;
8056 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8057 struct got_fileindex *fileindex = NULL;
8058 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8059 int ch, did_something = 0;
8061 while ((ch = getopt(argc, argv, "")) != -1) {
8074 branch_arg = argv[0];
8076 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8077 "unveil", NULL) == -1)
8080 cwd = getcwd(NULL, 0);
8082 error = got_error_from_errno("getcwd");
8086 error = got_worktree_open(&worktree, cwd);
8090 error = check_rebase_or_histedit_in_progress(worktree);
8094 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8099 error = apply_unveil(got_repo_get_path(repo), 0,
8100 got_worktree_get_root_path(worktree));
8104 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8105 error = got_error_from_errno("asprintf");
8109 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8110 &base_branch_ref, worktree, refname, repo);
8114 refname = strdup(got_ref_get_name(branch_ref));
8115 if (refname == NULL) {
8116 error = got_error_from_errno("strdup");
8117 got_worktree_integrate_abort(worktree, fileindex, repo,
8118 branch_ref, base_branch_ref);
8121 base_refname = strdup(got_ref_get_name(base_branch_ref));
8122 if (base_refname == NULL) {
8123 error = got_error_from_errno("strdup");
8124 got_worktree_integrate_abort(worktree, fileindex, repo,
8125 branch_ref, base_branch_ref);
8129 error = got_ref_resolve(&commit_id, repo, branch_ref);
8133 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8137 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8138 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8139 "specified branch has already been integrated");
8140 got_worktree_integrate_abort(worktree, fileindex, repo,
8141 branch_ref, base_branch_ref);
8145 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8147 if (error->code == GOT_ERR_ANCESTRY)
8148 error = got_error(GOT_ERR_REBASE_REQUIRED);
8149 got_worktree_integrate_abort(worktree, fileindex, repo,
8150 branch_ref, base_branch_ref);
8154 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8155 branch_ref, base_branch_ref, update_progress, &did_something,
8156 check_cancelled, NULL);
8160 printf("Integrated %s into %s\n", refname, base_refname);
8163 got_repo_close(repo);
8165 got_worktree_close(worktree);
8167 free(base_commit_id);
8177 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8178 "[file-path ...]\n",
8183 static const struct got_error *
8184 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8185 const char *path, struct got_object_id *blob_id,
8186 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8187 int dirfd, const char *de_name)
8189 const struct got_error *err = NULL;
8190 char *id_str = NULL;
8192 if (staged_status != GOT_STATUS_ADD &&
8193 staged_status != GOT_STATUS_MODIFY &&
8194 staged_status != GOT_STATUS_DELETE)
8197 if (staged_status == GOT_STATUS_ADD ||
8198 staged_status == GOT_STATUS_MODIFY)
8199 err = got_object_id_str(&id_str, staged_blob_id);
8201 err = got_object_id_str(&id_str, blob_id);
8205 printf("%s %c %s\n", id_str, staged_status, path);
8210 static const struct got_error *
8211 cmd_stage(int argc, char *argv[])
8213 const struct got_error *error = NULL;
8214 struct got_repository *repo = NULL;
8215 struct got_worktree *worktree = NULL;
8217 struct got_pathlist_head paths;
8218 struct got_pathlist_entry *pe;
8219 int ch, list_stage = 0, pflag = 0;
8220 FILE *patch_script_file = NULL;
8221 const char *patch_script_path = NULL;
8222 struct choose_patch_arg cpa;
8226 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8235 patch_script_path = optarg;
8247 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8248 "unveil", NULL) == -1)
8251 if (list_stage && (pflag || patch_script_path))
8252 errx(1, "-l option cannot be used with other options");
8253 if (patch_script_path && !pflag)
8254 errx(1, "-F option can only be used together with -p option");
8256 cwd = getcwd(NULL, 0);
8258 error = got_error_from_errno("getcwd");
8262 error = got_worktree_open(&worktree, cwd);
8266 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8271 if (patch_script_path) {
8272 patch_script_file = fopen(patch_script_path, "r");
8273 if (patch_script_file == NULL) {
8274 error = got_error_from_errno2("fopen",
8279 error = apply_unveil(got_repo_get_path(repo), 0,
8280 got_worktree_get_root_path(worktree));
8284 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8289 error = got_worktree_status(worktree, &paths, repo,
8290 print_stage, NULL, check_cancelled, NULL);
8292 cpa.patch_script_file = patch_script_file;
8293 cpa.action = "stage";
8294 error = got_worktree_stage(worktree, &paths,
8295 pflag ? NULL : print_status, NULL,
8296 pflag ? choose_patch : NULL, &cpa, repo);
8299 if (patch_script_file && fclose(patch_script_file) == EOF &&
8301 error = got_error_from_errno2("fclose", patch_script_path);
8303 got_repo_close(repo);
8305 got_worktree_close(worktree);
8306 TAILQ_FOREACH(pe, &paths, entry)
8307 free((char *)pe->path);
8308 got_pathlist_free(&paths);
8316 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8317 "[file-path ...]\n",
8323 static const struct got_error *
8324 cmd_unstage(int argc, char *argv[])
8326 const struct got_error *error = NULL;
8327 struct got_repository *repo = NULL;
8328 struct got_worktree *worktree = NULL;
8330 struct got_pathlist_head paths;
8331 struct got_pathlist_entry *pe;
8332 int ch, did_something = 0, pflag = 0;
8333 FILE *patch_script_file = NULL;
8334 const char *patch_script_path = NULL;
8335 struct choose_patch_arg cpa;
8339 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8345 patch_script_path = optarg;
8357 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8358 "unveil", NULL) == -1)
8361 if (patch_script_path && !pflag)
8362 errx(1, "-F option can only be used together with -p option");
8364 cwd = getcwd(NULL, 0);
8366 error = got_error_from_errno("getcwd");
8370 error = got_worktree_open(&worktree, cwd);
8374 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8379 if (patch_script_path) {
8380 patch_script_file = fopen(patch_script_path, "r");
8381 if (patch_script_file == NULL) {
8382 error = got_error_from_errno2("fopen",
8388 error = apply_unveil(got_repo_get_path(repo), 0,
8389 got_worktree_get_root_path(worktree));
8393 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8397 cpa.patch_script_file = patch_script_file;
8398 cpa.action = "unstage";
8399 error = got_worktree_unstage(worktree, &paths, update_progress,
8400 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8402 if (patch_script_file && fclose(patch_script_file) == EOF &&
8404 error = got_error_from_errno2("fclose", patch_script_path);
8406 got_repo_close(repo);
8408 got_worktree_close(worktree);
8409 TAILQ_FOREACH(pe, &paths, entry)
8410 free((char *)pe->path);
8411 got_pathlist_free(&paths);
8419 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8420 "arg1 [arg2 ...]\n", getprogname());
8424 static const struct got_error *
8425 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8427 const struct got_error *err;
8428 struct got_blob_object *blob;
8430 err = got_object_open_as_blob(&blob, repo, id, 8192);
8434 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8435 got_object_blob_close(blob);
8439 static const struct got_error *
8440 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8442 const struct got_error *err;
8443 struct got_tree_object *tree;
8446 err = got_object_open_as_tree(&tree, repo, id);
8450 nentries = got_object_tree_get_nentries(tree);
8451 for (i = 0; i < nentries; i++) {
8452 struct got_tree_entry *te;
8454 if (sigint_received || sigpipe_received)
8456 te = got_object_tree_get_entry(tree, i);
8457 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8460 fprintf(outfile, "%s %.7o %s\n", id_str,
8461 got_tree_entry_get_mode(te),
8462 got_tree_entry_get_name(te));
8466 got_object_tree_close(tree);
8470 static const struct got_error *
8471 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8473 const struct got_error *err;
8474 struct got_commit_object *commit;
8475 const struct got_object_id_queue *parent_ids;
8476 struct got_object_qid *pid;
8477 char *id_str = NULL;
8478 const char *logmsg = NULL;
8480 err = got_object_open_as_commit(&commit, repo, id);
8484 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8488 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8489 parent_ids = got_object_commit_get_parent_ids(commit);
8490 fprintf(outfile, "numparents %d\n",
8491 got_object_commit_get_nparents(commit));
8492 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8494 err = got_object_id_str(&pid_str, pid->id);
8497 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8500 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8501 got_object_commit_get_author(commit),
8502 got_object_commit_get_author_time(commit));
8504 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8505 got_object_commit_get_author(commit),
8506 got_object_commit_get_committer_time(commit));
8508 logmsg = got_object_commit_get_logmsg_raw(commit);
8509 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8510 fprintf(outfile, "%s", logmsg);
8513 got_object_commit_close(commit);
8517 static const struct got_error *
8518 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8520 const struct got_error *err;
8521 struct got_tag_object *tag;
8522 char *id_str = NULL;
8523 const char *tagmsg = NULL;
8525 err = got_object_open_as_tag(&tag, repo, id);
8529 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8533 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8535 switch (got_object_tag_get_object_type(tag)) {
8536 case GOT_OBJ_TYPE_BLOB:
8537 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8538 GOT_OBJ_LABEL_BLOB);
8540 case GOT_OBJ_TYPE_TREE:
8541 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8542 GOT_OBJ_LABEL_TREE);
8544 case GOT_OBJ_TYPE_COMMIT:
8545 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8546 GOT_OBJ_LABEL_COMMIT);
8548 case GOT_OBJ_TYPE_TAG:
8549 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8556 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8557 got_object_tag_get_name(tag));
8559 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8560 got_object_tag_get_tagger(tag),
8561 got_object_tag_get_tagger_time(tag));
8563 tagmsg = got_object_tag_get_message(tag);
8564 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8565 fprintf(outfile, "%s", tagmsg);
8568 got_object_tag_close(tag);
8572 static const struct got_error *
8573 cmd_cat(int argc, char *argv[])
8575 const struct got_error *error;
8576 struct got_repository *repo = NULL;
8577 struct got_worktree *worktree = NULL;
8578 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8579 const char *commit_id_str = NULL;
8580 struct got_object_id *id = NULL, *commit_id = NULL;
8581 int ch, obj_type, i, force_path = 0;
8584 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8589 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8592 commit_id_str = optarg;
8595 repo_path = realpath(optarg, NULL);
8596 if (repo_path == NULL)
8597 return got_error_from_errno2("realpath",
8599 got_path_strip_trailing_slashes(repo_path);
8613 cwd = getcwd(NULL, 0);
8615 error = got_error_from_errno("getcwd");
8618 error = got_worktree_open(&worktree, cwd);
8619 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8622 if (repo_path == NULL) {
8624 got_worktree_get_repo_path(worktree));
8625 if (repo_path == NULL) {
8626 error = got_error_from_errno("strdup");
8632 if (repo_path == NULL) {
8633 repo_path = getcwd(NULL, 0);
8634 if (repo_path == NULL)
8635 return got_error_from_errno("getcwd");
8638 error = got_repo_open(&repo, repo_path, NULL);
8643 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8647 if (commit_id_str == NULL)
8648 commit_id_str = GOT_REF_HEAD;
8649 error = got_repo_match_object_id(&commit_id, NULL,
8650 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8654 for (i = 0; i < argc; i++) {
8656 error = got_object_id_by_path(&id, repo, commit_id,
8661 error = got_repo_match_object_id(&id, &label, argv[i],
8662 GOT_OBJ_TYPE_ANY, 0, repo);
8664 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8665 error->code != GOT_ERR_NOT_REF)
8667 error = got_object_id_by_path(&id, repo,
8668 commit_id, argv[i]);
8674 error = got_object_get_type(&obj_type, repo, id);
8679 case GOT_OBJ_TYPE_BLOB:
8680 error = cat_blob(id, repo, stdout);
8682 case GOT_OBJ_TYPE_TREE:
8683 error = cat_tree(id, repo, stdout);
8685 case GOT_OBJ_TYPE_COMMIT:
8686 error = cat_commit(id, repo, stdout);
8688 case GOT_OBJ_TYPE_TAG:
8689 error = cat_tag(id, repo, stdout);
8692 error = got_error(GOT_ERR_OBJ_TYPE);
8707 got_worktree_close(worktree);
8709 const struct got_error *repo_error;
8710 repo_error = got_repo_close(repo);