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);
112 __dead static void usage_info(void);
114 static const struct got_error* cmd_init(int, char *[]);
115 static const struct got_error* cmd_import(int, char *[]);
116 static const struct got_error* cmd_clone(int, char *[]);
117 static const struct got_error* cmd_fetch(int, char *[]);
118 static const struct got_error* cmd_checkout(int, char *[]);
119 static const struct got_error* cmd_update(int, char *[]);
120 static const struct got_error* cmd_log(int, char *[]);
121 static const struct got_error* cmd_diff(int, char *[]);
122 static const struct got_error* cmd_blame(int, char *[]);
123 static const struct got_error* cmd_tree(int, char *[]);
124 static const struct got_error* cmd_status(int, char *[]);
125 static const struct got_error* cmd_ref(int, char *[]);
126 static const struct got_error* cmd_branch(int, char *[]);
127 static const struct got_error* cmd_tag(int, char *[]);
128 static const struct got_error* cmd_add(int, char *[]);
129 static const struct got_error* cmd_remove(int, char *[]);
130 static const struct got_error* cmd_revert(int, char *[]);
131 static const struct got_error* cmd_commit(int, char *[]);
132 static const struct got_error* cmd_cherrypick(int, char *[]);
133 static const struct got_error* cmd_backout(int, char *[]);
134 static const struct got_error* cmd_rebase(int, char *[]);
135 static const struct got_error* cmd_histedit(int, char *[]);
136 static const struct got_error* cmd_integrate(int, char *[]);
137 static const struct got_error* cmd_stage(int, char *[]);
138 static const struct got_error* cmd_unstage(int, char *[]);
139 static const struct got_error* cmd_cat(int, char *[]);
140 static const struct got_error* cmd_info(int, char *[]);
142 static struct got_cmd got_commands[] = {
143 { "init", cmd_init, usage_init, "" },
144 { "import", cmd_import, usage_import, "im" },
145 { "clone", cmd_clone, usage_clone, "cl" },
146 { "fetch", cmd_fetch, usage_fetch, "fe" },
147 { "checkout", cmd_checkout, usage_checkout, "co" },
148 { "update", cmd_update, usage_update, "up" },
149 { "log", cmd_log, usage_log, "" },
150 { "diff", cmd_diff, usage_diff, "di" },
151 { "blame", cmd_blame, usage_blame, "bl" },
152 { "tree", cmd_tree, usage_tree, "tr" },
153 { "status", cmd_status, usage_status, "st" },
154 { "ref", cmd_ref, usage_ref, "" },
155 { "branch", cmd_branch, usage_branch, "br" },
156 { "tag", cmd_tag, usage_tag, "" },
157 { "add", cmd_add, usage_add, "" },
158 { "remove", cmd_remove, usage_remove, "rm" },
159 { "revert", cmd_revert, usage_revert, "rv" },
160 { "commit", cmd_commit, usage_commit, "ci" },
161 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
162 { "backout", cmd_backout, usage_backout, "bo" },
163 { "rebase", cmd_rebase, usage_rebase, "rb" },
164 { "histedit", cmd_histedit, usage_histedit, "he" },
165 { "integrate", cmd_integrate, usage_integrate,"ig" },
166 { "stage", cmd_stage, usage_stage, "sg" },
167 { "unstage", cmd_unstage, usage_unstage, "ug" },
168 { "cat", cmd_cat, usage_cat, "" },
169 { "info", cmd_info, usage_info, "" },
177 fprintf(stderr, "commands:");
178 for (i = 0; i < nitems(got_commands); i++) {
179 struct got_cmd *cmd = &got_commands[i];
180 fprintf(stderr, " %s", cmd->cmd_name);
186 main(int argc, char *argv[])
191 int hflag = 0, Vflag = 0;
192 static struct option longopts[] = {
193 { "version", no_argument, NULL, 'V' },
197 setlocale(LC_CTYPE, "");
199 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
218 got_version_print_str();
225 signal(SIGINT, catch_sigint);
226 signal(SIGPIPE, catch_sigpipe);
228 for (i = 0; i < nitems(got_commands); i++) {
229 const struct got_error *error;
231 cmd = &got_commands[i];
233 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
234 strcmp(cmd->cmd_alias, argv[0]) != 0)
238 got_commands[i].cmd_usage();
240 error = got_commands[i].cmd_main(argc, argv);
241 if (error && error->code != GOT_ERR_CANCELLED &&
242 error->code != GOT_ERR_PRIVSEP_EXIT &&
243 !(sigpipe_received &&
244 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
246 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
247 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
254 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
262 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
269 static const struct got_error *
270 get_editor(char **abspath)
272 const struct got_error *err = NULL;
277 editor = getenv("VISUAL");
279 editor = getenv("EDITOR");
282 err = got_path_find_prog(abspath, editor);
287 if (*abspath == NULL) {
288 *abspath = strdup("/bin/ed");
289 if (*abspath == NULL)
290 return got_error_from_errno("strdup");
296 static const struct got_error *
297 apply_unveil(const char *repo_path, int repo_read_only,
298 const char *worktree_path)
300 const struct got_error *err;
303 if (unveil("gmon.out", "rwc") != 0)
304 return got_error_from_errno2("unveil", "gmon.out");
306 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
307 return got_error_from_errno2("unveil", repo_path);
309 if (worktree_path && unveil(worktree_path, "rwc") != 0)
310 return got_error_from_errno2("unveil", worktree_path);
312 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
313 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
315 err = got_privsep_unveil_exec_helpers();
319 if (unveil(NULL, NULL) != 0)
320 return got_error_from_errno("unveil");
328 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
332 static const struct got_error *
333 cmd_init(int argc, char *argv[])
335 const struct got_error *error = NULL;
336 char *repo_path = NULL;
339 while ((ch = getopt(argc, argv, "")) != -1) {
351 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
357 repo_path = strdup(argv[0]);
358 if (repo_path == NULL)
359 return got_error_from_errno("strdup");
361 got_path_strip_trailing_slashes(repo_path);
363 error = got_path_mkdir(repo_path);
365 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
368 error = apply_unveil(repo_path, 0, NULL);
372 error = got_repo_init(repo_path);
381 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
382 "[-r repository-path] [-I pattern] path\n", getprogname());
387 spawn_editor(const char *editor, const char *file)
390 sig_t sighup, sigint, sigquit;
393 sighup = signal(SIGHUP, SIG_IGN);
394 sigint = signal(SIGINT, SIG_IGN);
395 sigquit = signal(SIGQUIT, SIG_IGN);
397 switch (pid = fork()) {
401 execl(editor, editor, file, (char *)NULL);
405 while (waitpid(pid, &st, 0) == -1)
410 (void)signal(SIGHUP, sighup);
411 (void)signal(SIGINT, sigint);
412 (void)signal(SIGQUIT, sigquit);
414 if (!WIFEXITED(st)) {
419 return WEXITSTATUS(st);
422 static const struct got_error *
423 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
424 const char *initial_content)
426 const struct got_error *err = NULL;
430 int content_changed = 0;
435 if (stat(logmsg_path, &st) == -1)
436 return got_error_from_errno2("stat", logmsg_path);
438 if (spawn_editor(editor, logmsg_path) == -1)
439 return got_error_from_errno("failed spawning editor");
441 if (stat(logmsg_path, &st2) == -1)
442 return got_error_from_errno("stat");
444 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
445 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
446 "no changes made to commit message, aborting");
448 *logmsg = malloc(st2.st_size + 1);
450 return got_error_from_errno("malloc");
454 fp = fopen(logmsg_path, "r");
456 err = got_error_from_errno("fopen");
459 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 if (!content_changed && strcmp(buf, initial_content) != 0)
462 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
463 continue; /* remove comments and leading empty lines */
464 len = strlcat(*logmsg, buf, st2.st_size);
468 while (len > 0 && (*logmsg)[len - 1] == '\n') {
469 (*logmsg)[len - 1] = '\0';
473 if (len == 0 || !content_changed)
474 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
475 "commit message cannot be empty, aborting");
484 static const struct got_error *
485 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
486 const char *path_dir, const char *branch_name)
488 char *initial_content = NULL;
489 const struct got_error *err = NULL;
492 if (asprintf(&initial_content,
493 "\n# %s to be imported to branch %s\n", path_dir,
495 return got_error_from_errno("asprintf");
497 err = got_opentemp_named_fd(logmsg_path, &fd,
498 GOT_TMPDIR_STR "/got-importmsg");
502 dprintf(fd, initial_content);
505 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
507 free(initial_content);
511 static const struct got_error *
512 import_progress(void *arg, const char *path)
514 printf("A %s\n", path);
518 static const struct got_error *
519 get_author(char **author, struct got_repository *repo)
521 const struct got_error *err = NULL;
522 const char *got_author, *name, *email;
526 name = got_repo_get_gitconfig_author_name(repo);
527 email = got_repo_get_gitconfig_author_email(repo);
529 if (asprintf(author, "%s <%s>", name, email) == -1)
530 return got_error_from_errno("asprintf");
534 got_author = getenv("GOT_AUTHOR");
535 if (got_author == NULL) {
536 name = got_repo_get_global_gitconfig_author_name(repo);
537 email = got_repo_get_global_gitconfig_author_email(repo);
539 if (asprintf(author, "%s <%s>", name, email) == -1)
540 return got_error_from_errno("asprintf");
543 /* TODO: Look up user in password database? */
544 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
547 *author = strdup(got_author);
549 return got_error_from_errno("strdup");
552 * Really dumb email address check; we're only doing this to
553 * avoid git's object parser breaking on commits we create.
555 while (*got_author && *got_author != '<')
557 if (*got_author != '<') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 while (*got_author && *got_author != '@')
563 if (*got_author != '@') {
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
567 while (*got_author && *got_author != '>')
569 if (*got_author != '>')
570 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
579 static const struct got_error *
580 get_gitconfig_path(char **gitconfig_path)
582 const char *homedir = getenv("HOME");
584 *gitconfig_path = NULL;
586 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
587 return got_error_from_errno("asprintf");
593 static const struct got_error *
594 cmd_import(int argc, char *argv[])
596 const struct got_error *error = NULL;
597 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
598 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
599 const char *branch_name = "main";
600 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
601 struct got_repository *repo = NULL;
602 struct got_reference *branch_ref = NULL, *head_ref = NULL;
603 struct got_object_id *new_commit_id = NULL;
605 struct got_pathlist_head ignores;
606 struct got_pathlist_entry *pe;
607 int preserve_logmsg = 0;
609 TAILQ_INIT(&ignores);
611 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
614 branch_name = optarg;
617 logmsg = strdup(optarg);
618 if (logmsg == NULL) {
619 error = got_error_from_errno("strdup");
624 repo_path = realpath(optarg, NULL);
625 if (repo_path == NULL) {
626 error = got_error_from_errno2("realpath",
632 if (optarg[0] == '\0')
634 error = got_pathlist_insert(&pe, &ignores, optarg,
649 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
657 if (repo_path == NULL) {
658 repo_path = getcwd(NULL, 0);
659 if (repo_path == NULL)
660 return got_error_from_errno("getcwd");
662 got_path_strip_trailing_slashes(repo_path);
663 error = get_gitconfig_path(&gitconfig_path);
666 error = got_repo_open(&repo, repo_path, gitconfig_path);
670 error = get_author(&author, repo);
675 * Don't let the user create a branch name with a leading '-'.
676 * While technically a valid reference name, this case is usually
677 * an unintended typo.
679 if (branch_name[0] == '-')
680 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
682 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
683 error = got_error_from_errno("asprintf");
687 error = got_ref_open(&branch_ref, repo, refname, 0);
689 if (error->code != GOT_ERR_NOT_REF)
692 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
693 "import target branch already exists");
697 path_dir = realpath(argv[0], NULL);
698 if (path_dir == NULL) {
699 error = got_error_from_errno2("realpath", argv[0]);
702 got_path_strip_trailing_slashes(path_dir);
705 * unveil(2) traverses exec(2); if an editor is used we have
706 * to apply unveil after the log message has been written.
708 if (logmsg == NULL || strlen(logmsg) == 0) {
709 error = get_editor(&editor);
713 error = collect_import_msg(&logmsg, &logmsg_path, editor,
716 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
723 if (unveil(path_dir, "r") != 0) {
724 error = got_error_from_errno2("unveil", path_dir);
730 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
737 error = got_repo_import(&new_commit_id, path_dir, logmsg,
738 author, &ignores, repo, import_progress, NULL);
745 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
752 error = got_ref_write(branch_ref, repo);
759 error = got_object_id_str(&id_str, new_commit_id);
766 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
768 if (error->code != GOT_ERR_NOT_REF) {
774 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
782 error = got_ref_write(head_ref, repo);
790 printf("Created branch %s with commit %s\n",
791 got_ref_get_name(branch_ref), id_str);
793 if (preserve_logmsg) {
794 fprintf(stderr, "%s: log message preserved in %s\n",
795 getprogname(), logmsg_path);
796 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
797 error = got_error_from_errno2("unlink", logmsg_path);
806 free(gitconfig_path);
808 got_ref_close(branch_ref);
810 got_ref_close(head_ref);
817 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
818 "[-R reference] repository-url [directory]\n", getprogname());
822 struct got_fetch_progress_arg {
823 char last_scaled_size[FMT_SCALED_STRSIZE];
829 static const struct got_error *
830 fetch_progress(void *arg, const char *message, off_t packfile_size,
831 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
833 struct got_fetch_progress_arg *a = arg;
834 char scaled_size[FMT_SCALED_STRSIZE];
835 int p_indexed, p_resolved;
836 int print_size = 0, print_indexed = 0, print_resolved = 0;
838 if (a->verbosity < 0)
841 if (message && message[0] != '\0') {
842 printf("\rserver: %s", message);
847 if (packfile_size > 0 || nobj_indexed > 0) {
848 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
849 (a->last_scaled_size[0] == '\0' ||
850 strcmp(scaled_size, a->last_scaled_size)) != 0) {
852 if (strlcpy(a->last_scaled_size, scaled_size,
853 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
854 return got_error(GOT_ERR_NO_SPACE);
856 if (nobj_indexed > 0) {
857 p_indexed = (nobj_indexed * 100) / nobj_total;
858 if (p_indexed != a->last_p_indexed) {
859 a->last_p_indexed = p_indexed;
864 if (nobj_resolved > 0) {
865 p_resolved = (nobj_resolved * 100) /
866 (nobj_total - nobj_loose);
867 if (p_resolved != a->last_p_resolved) {
868 a->last_p_resolved = p_resolved;
876 if (print_size || print_indexed || print_resolved)
879 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
881 printf("; indexing %d%%", p_indexed);
883 printf("; resolving deltas %d%%", p_resolved);
884 if (print_size || print_indexed || print_resolved)
890 static const struct got_error *
891 create_symref(const char *refname, struct got_reference *target_ref,
892 int verbosity, struct got_repository *repo)
894 const struct got_error *err;
895 struct got_reference *head_symref;
897 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
901 err = got_ref_write(head_symref, repo);
902 got_ref_close(head_symref);
903 if (err == NULL && verbosity > 0) {
904 printf("Created reference %s: %s\n", GOT_REF_HEAD,
905 got_ref_get_name(target_ref));
910 static const struct got_error *
911 list_remote_refs(struct got_pathlist_head *symrefs,
912 struct got_pathlist_head *refs)
914 const struct got_error *err;
915 struct got_pathlist_entry *pe;
917 TAILQ_FOREACH(pe, symrefs, entry) {
918 const char *refname = pe->path;
919 const char *targetref = pe->data;
921 printf("%s: %s\n", refname, targetref);
924 TAILQ_FOREACH(pe, refs, entry) {
925 const char *refname = pe->path;
926 struct got_object_id *id = pe->data;
929 err = got_object_id_str(&id_str, id);
932 printf("%s: %s\n", refname, id_str);
939 static const struct got_error *
940 create_ref(const char *refname, struct got_object_id *id,
941 int verbosity, struct got_repository *repo)
943 const struct got_error *err = NULL;
944 struct got_reference *ref;
947 err = got_object_id_str(&id_str, id);
951 err = got_ref_alloc(&ref, refname, id);
955 err = got_ref_write(ref, repo);
958 if (err == NULL && verbosity >= 0)
959 printf("Created reference %s: %s\n", refname, id_str);
966 match_wanted_ref(const char *refname, const char *wanted_ref)
968 if (strncmp(refname, "refs/", 5) != 0)
973 * Prevent fetching of references that won't make any
974 * sense outside of the remote repository's context.
976 if (strncmp(refname, "got/", 4) == 0)
978 if (strncmp(refname, "remotes/", 8) == 0)
981 if (strncmp(wanted_ref, "refs/", 5) == 0)
984 /* Allow prefix match. */
985 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
988 /* Allow exact match. */
989 return (strcmp(refname, wanted_ref) == 0);
993 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
995 struct got_pathlist_entry *pe;
997 TAILQ_FOREACH(pe, wanted_refs, entry) {
998 if (match_wanted_ref(refname, pe->path))
1005 static const struct got_error *
1006 create_wanted_ref(const char *refname, struct got_object_id *id,
1007 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1009 const struct got_error *err;
1010 char *remote_refname;
1012 if (strncmp("refs/", refname, 5) == 0)
1015 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1016 remote_repo_name, refname) == -1)
1017 return got_error_from_errno("asprintf");
1019 err = create_ref(remote_refname, id, verbosity, repo);
1020 free(remote_refname);
1024 static const struct got_error *
1025 cmd_clone(int argc, char *argv[])
1027 const struct got_error *error = NULL;
1028 const char *uri, *dirname;
1029 char *proto, *host, *port, *repo_name, *server_path;
1030 char *default_destdir = NULL, *id_str = NULL;
1031 const char *repo_path;
1032 struct got_repository *repo = NULL;
1033 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1034 struct got_pathlist_entry *pe;
1035 struct got_object_id *pack_hash = NULL;
1036 int ch, fetchfd = -1, fetchstatus;
1037 pid_t fetchpid = -1;
1038 struct got_fetch_progress_arg fpa;
1039 char *git_url = NULL;
1040 char *gitconfig_path = NULL;
1041 char *gitconfig = NULL;
1042 FILE *gitconfig_file = NULL;
1044 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1045 int list_refs_only = 0;
1046 struct got_reference *head_symref = NULL;
1049 TAILQ_INIT(&symrefs);
1050 TAILQ_INIT(&wanted_branches);
1051 TAILQ_INIT(&wanted_refs);
1053 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1056 fetch_all_branches = 1;
1059 error = got_pathlist_append(&wanted_branches,
1068 mirror_references = 1;
1073 else if (verbosity < 3)
1080 error = got_pathlist_append(&wanted_refs,
1093 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1094 errx(1, "-a and -b options are mutually exclusive");
1095 if (list_refs_only) {
1096 if (!TAILQ_EMPTY(&wanted_branches))
1097 errx(1, "-l and -b options are mutually exclusive");
1098 if (fetch_all_branches)
1099 errx(1, "-l and -a options are mutually exclusive");
1100 if (mirror_references)
1101 errx(1, "-l and -m options are mutually exclusive");
1102 if (verbosity == -1)
1103 errx(1, "-l and -q options are mutually exclusive");
1104 if (!TAILQ_EMPTY(&wanted_refs))
1105 errx(1, "-l and -R options are mutually exclusive");
1117 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1118 &repo_name, argv[0]);
1122 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1123 host, port ? ":" : "", port ? port : "",
1124 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1125 error = got_error_from_errno("asprintf");
1129 if (strcmp(proto, "git") == 0) {
1131 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1132 "sendfd dns inet unveil", NULL) == -1)
1135 } else if (strcmp(proto, "git+ssh") == 0 ||
1136 strcmp(proto, "ssh") == 0) {
1138 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1139 "sendfd unveil", NULL) == -1)
1142 } else if (strcmp(proto, "http") == 0 ||
1143 strcmp(proto, "git+http") == 0) {
1144 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1147 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1150 if (dirname == NULL) {
1151 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1152 error = got_error_from_errno("asprintf");
1155 repo_path = default_destdir;
1157 repo_path = dirname;
1159 if (!list_refs_only) {
1160 error = got_path_mkdir(repo_path);
1164 error = got_repo_init(repo_path);
1167 error = got_repo_open(&repo, repo_path, NULL);
1172 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1173 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1174 error = got_error_from_errno2("unveil",
1175 GOT_FETCH_PATH_SSH);
1179 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1184 printf("Connecting to %s%s%s\n", host,
1185 port ? ":" : "", port ? port : "");
1187 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1188 server_path, verbosity);
1192 fpa.last_scaled_size[0] = '\0';
1193 fpa.last_p_indexed = -1;
1194 fpa.last_p_resolved = -1;
1195 fpa.verbosity = verbosity;
1196 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1197 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1198 fetch_all_branches, &wanted_branches, &wanted_refs,
1199 list_refs_only, verbosity, fetchfd, repo,
1200 fetch_progress, &fpa);
1204 if (list_refs_only) {
1205 error = list_remote_refs(&symrefs, &refs);
1209 error = got_object_id_str(&id_str, pack_hash);
1213 printf("\nFetched %s.pack\n", id_str);
1216 /* Set up references provided with the pack file. */
1217 TAILQ_FOREACH(pe, &refs, entry) {
1218 const char *refname = pe->path;
1219 struct got_object_id *id = pe->data;
1220 char *remote_refname;
1222 if (is_wanted_ref(&wanted_refs, refname) &&
1223 !mirror_references) {
1224 error = create_wanted_ref(refname, id,
1225 GOT_FETCH_DEFAULT_REMOTE_NAME,
1226 verbosity - 1, repo);
1232 error = create_ref(refname, id, verbosity - 1, repo);
1236 if (mirror_references)
1239 if (strncmp("refs/heads/", refname, 11) != 0)
1242 if (asprintf(&remote_refname,
1243 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1244 refname + 11) == -1) {
1245 error = got_error_from_errno("asprintf");
1248 error = create_ref(remote_refname, id, verbosity - 1, repo);
1249 free(remote_refname);
1254 /* Set the HEAD reference if the server provided one. */
1255 TAILQ_FOREACH(pe, &symrefs, entry) {
1256 struct got_reference *target_ref;
1257 const char *refname = pe->path;
1258 const char *target = pe->data;
1259 char *remote_refname = NULL, *remote_target = NULL;
1261 if (strcmp(refname, GOT_REF_HEAD) != 0)
1264 error = got_ref_open(&target_ref, repo, target, 0);
1266 if (error->code == GOT_ERR_NOT_REF) {
1273 error = create_symref(refname, target_ref, verbosity, repo);
1274 got_ref_close(target_ref);
1278 if (mirror_references)
1281 if (strncmp("refs/heads/", target, 11) != 0)
1284 if (asprintf(&remote_refname,
1285 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1287 error = got_error_from_errno("asprintf");
1290 if (asprintf(&remote_target,
1291 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1292 target + 11) == -1) {
1293 error = got_error_from_errno("asprintf");
1294 free(remote_refname);
1297 error = got_ref_open(&target_ref, repo, remote_target, 0);
1299 free(remote_refname);
1300 free(remote_target);
1301 if (error->code == GOT_ERR_NOT_REF) {
1307 error = create_symref(remote_refname, target_ref,
1308 verbosity - 1, repo);
1309 free(remote_refname);
1310 free(remote_target);
1311 got_ref_close(target_ref);
1317 * We failed to set the HEAD reference. If we asked for
1318 * a set of wanted branches use the first of one of those
1319 * which could be fetched instead.
1321 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1322 const char *target = pe->path;
1323 struct got_reference *target_ref;
1325 error = got_ref_open(&target_ref, repo, target, 0);
1327 if (error->code == GOT_ERR_NOT_REF) {
1334 error = create_symref(GOT_REF_HEAD, target_ref,
1336 got_ref_close(target_ref);
1343 /* Create a config file git-fetch(1) can understand. */
1344 gitconfig_path = got_repo_get_path_gitconfig(repo);
1345 if (gitconfig_path == NULL) {
1346 error = got_error_from_errno("got_repo_get_path_gitconfig");
1349 gitconfig_file = fopen(gitconfig_path, "a");
1350 if (gitconfig_file == NULL) {
1351 error = got_error_from_errno2("fopen", gitconfig_path);
1354 if (mirror_references) {
1355 if (asprintf(&gitconfig,
1358 "\tfetch = +refs/*:refs/*\n"
1359 "\tmirror = true\n",
1360 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1361 error = got_error_from_errno("asprintf");
1364 } else if (fetch_all_branches) {
1365 if (asprintf(&gitconfig,
1368 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1369 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1370 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1371 error = got_error_from_errno("asprintf");
1375 const char *branchname;
1378 * If the server specified a default branch, use just that one.
1379 * Otherwise fall back to fetching all branches on next fetch.
1382 branchname = got_ref_get_symref_target(head_symref);
1383 if (strncmp(branchname, "refs/heads/", 11) == 0)
1386 branchname = "*"; /* fall back to all branches */
1387 if (asprintf(&gitconfig,
1390 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1391 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1392 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1393 branchname) == -1) {
1394 error = got_error_from_errno("asprintf");
1398 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1399 if (n != strlen(gitconfig)) {
1400 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1405 printf("Created %s repository '%s'\n",
1406 mirror_references ? "mirrored" : "cloned", repo_path);
1409 if (kill(fetchpid, SIGTERM) == -1)
1410 error = got_error_from_errno("kill");
1411 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1412 error = got_error_from_errno("waitpid");
1414 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1415 error = got_error_from_errno("close");
1416 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1417 error = got_error_from_errno("fclose");
1419 got_repo_close(repo);
1421 got_ref_close(head_symref);
1422 TAILQ_FOREACH(pe, &refs, entry) {
1423 free((void *)pe->path);
1426 got_pathlist_free(&refs);
1427 TAILQ_FOREACH(pe, &symrefs, entry) {
1428 free((void *)pe->path);
1431 got_pathlist_free(&symrefs);
1432 got_pathlist_free(&wanted_branches);
1433 got_pathlist_free(&wanted_refs);
1440 free(default_destdir);
1441 free(gitconfig_path);
1446 static const struct got_error *
1447 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1448 int replace_tags, int verbosity, struct got_repository *repo)
1450 const struct got_error *err = NULL;
1451 char *new_id_str = NULL;
1452 struct got_object_id *old_id = NULL;
1454 err = got_object_id_str(&new_id_str, new_id);
1458 if (!replace_tags &&
1459 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1460 err = got_ref_resolve(&old_id, repo, ref);
1463 if (got_object_id_cmp(old_id, new_id) == 0)
1465 if (verbosity >= 0) {
1466 printf("Rejecting update of existing tag %s: %s\n",
1467 got_ref_get_name(ref), new_id_str);
1472 if (got_ref_is_symbolic(ref)) {
1473 if (verbosity >= 0) {
1474 printf("Replacing reference %s: %s\n",
1475 got_ref_get_name(ref),
1476 got_ref_get_symref_target(ref));
1478 err = got_ref_change_symref_to_ref(ref, new_id);
1481 err = got_ref_write(ref, repo);
1485 err = got_ref_resolve(&old_id, repo, ref);
1488 if (got_object_id_cmp(old_id, new_id) == 0)
1491 err = got_ref_change_ref(ref, new_id);
1494 err = got_ref_write(ref, repo);
1500 printf("Updated %s: %s\n", got_ref_get_name(ref),
1508 static const struct got_error *
1509 update_symref(const char *refname, struct got_reference *target_ref,
1510 int verbosity, struct got_repository *repo)
1512 const struct got_error *err = NULL, *unlock_err;
1513 struct got_reference *symref;
1514 int symref_is_locked = 0;
1516 err = got_ref_open(&symref, repo, refname, 1);
1518 if (err->code != GOT_ERR_NOT_REF)
1520 err = got_ref_alloc_symref(&symref, refname, target_ref);
1524 err = got_ref_write(symref, repo);
1529 printf("Created reference %s: %s\n",
1530 got_ref_get_name(symref),
1531 got_ref_get_symref_target(symref));
1533 symref_is_locked = 1;
1535 if (strcmp(got_ref_get_symref_target(symref),
1536 got_ref_get_name(target_ref)) == 0)
1539 err = got_ref_change_symref(symref,
1540 got_ref_get_name(target_ref));
1544 err = got_ref_write(symref, repo);
1549 printf("Updated %s: %s\n", got_ref_get_name(symref),
1550 got_ref_get_symref_target(symref));
1554 if (symref_is_locked) {
1555 unlock_err = got_ref_unlock(symref);
1556 if (unlock_err && err == NULL)
1559 got_ref_close(symref);
1566 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1567 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1568 "[remote-repository-name]\n",
1573 static const struct got_error *
1574 delete_missing_ref(struct got_reference *ref,
1575 int verbosity, struct got_repository *repo)
1577 const struct got_error *err = NULL;
1578 struct got_object_id *id = NULL;
1579 char *id_str = NULL;
1581 if (got_ref_is_symbolic(ref)) {
1582 err = got_ref_delete(ref, repo);
1585 if (verbosity >= 0) {
1586 printf("Deleted reference %s: %s\n",
1587 got_ref_get_name(ref),
1588 got_ref_get_symref_target(ref));
1591 err = got_ref_resolve(&id, repo, ref);
1594 err = got_object_id_str(&id_str, id);
1598 err = got_ref_delete(ref, repo);
1601 if (verbosity >= 0) {
1602 printf("Deleted reference %s: %s\n",
1603 got_ref_get_name(ref), id_str);
1612 static const struct got_error *
1613 delete_missing_refs(struct got_pathlist_head *their_refs,
1614 struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1615 int verbosity, struct got_repository *repo)
1617 const struct got_error *err = NULL, *unlock_err;
1618 struct got_reflist_head my_refs;
1619 struct got_reflist_entry *re;
1620 struct got_pathlist_entry *pe;
1621 char *remote_namespace = NULL;
1622 char *local_refname = NULL;
1624 SIMPLEQ_INIT(&my_refs);
1626 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1628 return got_error_from_errno("asprintf");
1630 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1634 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1635 const char *refname = got_ref_get_name(re->ref);
1637 if (!remote->mirror_references) {
1638 if (strncmp(refname, remote_namespace,
1639 strlen(remote_namespace)) == 0) {
1640 if (strcmp(refname + strlen(remote_namespace),
1643 if (asprintf(&local_refname, "refs/heads/%s",
1644 refname + strlen(remote_namespace)) == -1) {
1645 err = got_error_from_errno("asprintf");
1648 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1652 TAILQ_FOREACH(pe, their_refs, entry) {
1653 if (strcmp(local_refname, pe->path) == 0)
1659 TAILQ_FOREACH(pe, their_symrefs, entry) {
1660 if (strcmp(local_refname, pe->path) == 0)
1666 err = delete_missing_ref(re->ref, verbosity, repo);
1670 if (local_refname) {
1671 struct got_reference *ref;
1672 err = got_ref_open(&ref, repo, local_refname, 1);
1674 if (err->code != GOT_ERR_NOT_REF)
1676 free(local_refname);
1677 local_refname = NULL;
1680 err = delete_missing_ref(ref, verbosity, repo);
1683 unlock_err = got_ref_unlock(ref);
1685 if (unlock_err && err == NULL) {
1690 free(local_refname);
1691 local_refname = NULL;
1695 free(remote_namespace);
1696 free(local_refname);
1700 static const struct got_error *
1701 update_wanted_ref(const char *refname, struct got_object_id *id,
1702 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1704 const struct got_error *err, *unlock_err;
1705 char *remote_refname;
1706 struct got_reference *ref;
1708 if (strncmp("refs/", refname, 5) == 0)
1711 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1712 remote_repo_name, refname) == -1)
1713 return got_error_from_errno("asprintf");
1715 err = got_ref_open(&ref, repo, remote_refname, 1);
1717 if (err->code != GOT_ERR_NOT_REF)
1719 err = create_ref(remote_refname, id, verbosity, repo);
1721 err = update_ref(ref, id, 0, verbosity, repo);
1722 unlock_err = got_ref_unlock(ref);
1723 if (unlock_err && err == NULL)
1728 free(remote_refname);
1732 static const struct got_error *
1733 cmd_fetch(int argc, char *argv[])
1735 const struct got_error *error = NULL, *unlock_err;
1736 char *cwd = NULL, *repo_path = NULL;
1737 const char *remote_name;
1738 char *proto = NULL, *host = NULL, *port = NULL;
1739 char *repo_name = NULL, *server_path = NULL;
1740 struct got_remote_repo *remotes, *remote = NULL;
1742 char *id_str = NULL;
1743 struct got_repository *repo = NULL;
1744 struct got_worktree *worktree = NULL;
1745 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1746 struct got_pathlist_entry *pe;
1747 struct got_object_id *pack_hash = NULL;
1748 int i, ch, fetchfd = -1, fetchstatus;
1749 pid_t fetchpid = -1;
1750 struct got_fetch_progress_arg fpa;
1751 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1752 int delete_refs = 0, replace_tags = 0;
1755 TAILQ_INIT(&symrefs);
1756 TAILQ_INIT(&wanted_branches);
1757 TAILQ_INIT(&wanted_refs);
1759 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1762 fetch_all_branches = 1;
1765 error = got_pathlist_append(&wanted_branches,
1777 repo_path = realpath(optarg, NULL);
1778 if (repo_path == NULL)
1779 return got_error_from_errno2("realpath",
1781 got_path_strip_trailing_slashes(repo_path);
1789 else if (verbosity < 3)
1796 error = got_pathlist_append(&wanted_refs,
1809 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1810 errx(1, "-a and -b options are mutually exclusive");
1811 if (list_refs_only) {
1812 if (!TAILQ_EMPTY(&wanted_branches))
1813 errx(1, "-l and -b options are mutually exclusive");
1814 if (fetch_all_branches)
1815 errx(1, "-l and -a options are mutually exclusive");
1817 errx(1, "-l and -d options are mutually exclusive");
1818 if (verbosity == -1)
1819 errx(1, "-l and -q options are mutually exclusive");
1823 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1825 remote_name = argv[0];
1829 cwd = getcwd(NULL, 0);
1831 error = got_error_from_errno("getcwd");
1835 if (repo_path == NULL) {
1836 error = got_worktree_open(&worktree, cwd);
1837 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1843 strdup(got_worktree_get_repo_path(worktree));
1844 if (repo_path == NULL)
1845 error = got_error_from_errno("strdup");
1849 repo_path = strdup(cwd);
1850 if (repo_path == NULL) {
1851 error = got_error_from_errno("strdup");
1857 error = got_repo_open(&repo, repo_path, NULL);
1861 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1862 for (i = 0; i < nremotes; i++) {
1863 remote = &remotes[i];
1864 if (strcmp(remote->name, remote_name) == 0)
1867 if (i == nremotes) {
1868 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1872 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1873 &repo_name, remote->url);
1877 if (strcmp(proto, "git") == 0) {
1879 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1880 "sendfd dns inet unveil", NULL) == -1)
1883 } else if (strcmp(proto, "git+ssh") == 0 ||
1884 strcmp(proto, "ssh") == 0) {
1886 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1887 "sendfd unveil", NULL) == -1)
1890 } else if (strcmp(proto, "http") == 0 ||
1891 strcmp(proto, "git+http") == 0) {
1892 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1895 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1899 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1900 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1901 error = got_error_from_errno2("unveil",
1902 GOT_FETCH_PATH_SSH);
1906 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1911 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1912 port ? ":" : "", port ? port : "");
1914 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1915 server_path, verbosity);
1919 fpa.last_scaled_size[0] = '\0';
1920 fpa.last_p_indexed = -1;
1921 fpa.last_p_resolved = -1;
1922 fpa.verbosity = verbosity;
1923 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1924 remote->mirror_references, fetch_all_branches, &wanted_branches,
1925 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1926 fetch_progress, &fpa);
1930 if (list_refs_only) {
1931 error = list_remote_refs(&symrefs, &refs);
1935 if (pack_hash == NULL) {
1937 printf("Already up-to-date\n");
1938 } else if (verbosity >= 0) {
1939 error = got_object_id_str(&id_str, pack_hash);
1942 printf("\nFetched %s.pack\n", id_str);
1947 /* Update references provided with the pack file. */
1948 TAILQ_FOREACH(pe, &refs, entry) {
1949 const char *refname = pe->path;
1950 struct got_object_id *id = pe->data;
1951 struct got_reference *ref;
1952 char *remote_refname;
1954 if (is_wanted_ref(&wanted_refs, refname) &&
1955 !remote->mirror_references) {
1956 error = update_wanted_ref(refname, id,
1957 remote->name, verbosity, repo);
1963 if (remote->mirror_references ||
1964 strncmp("refs/tags/", refname, 10) == 0) {
1965 error = got_ref_open(&ref, repo, refname, 1);
1967 if (error->code != GOT_ERR_NOT_REF)
1969 error = create_ref(refname, id, verbosity,
1974 error = update_ref(ref, id, replace_tags,
1976 unlock_err = got_ref_unlock(ref);
1977 if (unlock_err && error == NULL)
1983 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1984 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1985 remote_name, refname + 11) == -1) {
1986 error = got_error_from_errno("asprintf");
1990 error = got_ref_open(&ref, repo, remote_refname, 1);
1992 if (error->code != GOT_ERR_NOT_REF)
1994 error = create_ref(remote_refname, id,
1999 error = update_ref(ref, id, replace_tags,
2001 unlock_err = got_ref_unlock(ref);
2002 if (unlock_err && error == NULL)
2009 /* Also create a local branch if none exists yet. */
2010 error = got_ref_open(&ref, repo, refname, 1);
2012 if (error->code != GOT_ERR_NOT_REF)
2014 error = create_ref(refname, id, verbosity,
2019 unlock_err = got_ref_unlock(ref);
2020 if (unlock_err && error == NULL)
2027 error = delete_missing_refs(&refs, &symrefs, remote,
2033 if (!remote->mirror_references) {
2034 /* Update remote HEAD reference if the server provided one. */
2035 TAILQ_FOREACH(pe, &symrefs, entry) {
2036 struct got_reference *target_ref;
2037 const char *refname = pe->path;
2038 const char *target = pe->data;
2039 char *remote_refname = NULL, *remote_target = NULL;
2041 if (strcmp(refname, GOT_REF_HEAD) != 0)
2044 if (strncmp("refs/heads/", target, 11) != 0)
2047 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2048 remote->name, refname) == -1) {
2049 error = got_error_from_errno("asprintf");
2052 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2053 remote->name, target + 11) == -1) {
2054 error = got_error_from_errno("asprintf");
2055 free(remote_refname);
2059 error = got_ref_open(&target_ref, repo, remote_target,
2062 free(remote_refname);
2063 free(remote_target);
2064 if (error->code == GOT_ERR_NOT_REF) {
2070 error = update_symref(remote_refname, target_ref,
2072 free(remote_refname);
2073 free(remote_target);
2074 got_ref_close(target_ref);
2081 if (kill(fetchpid, SIGTERM) == -1)
2082 error = got_error_from_errno("kill");
2083 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2084 error = got_error_from_errno("waitpid");
2086 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2087 error = got_error_from_errno("close");
2089 got_repo_close(repo);
2091 got_worktree_close(worktree);
2092 TAILQ_FOREACH(pe, &refs, entry) {
2093 free((void *)pe->path);
2096 got_pathlist_free(&refs);
2097 TAILQ_FOREACH(pe, &symrefs, entry) {
2098 free((void *)pe->path);
2101 got_pathlist_free(&symrefs);
2102 got_pathlist_free(&wanted_branches);
2103 got_pathlist_free(&wanted_refs);
2118 usage_checkout(void)
2120 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2121 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2126 show_worktree_base_ref_warning(void)
2128 fprintf(stderr, "%s: warning: could not create a reference "
2129 "to the work tree's base commit; the commit could be "
2130 "garbage-collected by Git; making the repository "
2131 "writable and running 'got update' will prevent this\n",
2135 struct got_checkout_progress_arg {
2136 const char *worktree_path;
2137 int had_base_commit_ref_error;
2140 static const struct got_error *
2141 checkout_progress(void *arg, unsigned char status, const char *path)
2143 struct got_checkout_progress_arg *a = arg;
2145 /* Base commit bump happens silently. */
2146 if (status == GOT_STATUS_BUMP_BASE)
2149 if (status == GOT_STATUS_BASE_REF_ERR) {
2150 a->had_base_commit_ref_error = 1;
2154 while (path[0] == '/')
2157 printf("%c %s/%s\n", status, a->worktree_path, path);
2161 static const struct got_error *
2162 check_cancelled(void *arg)
2164 if (sigint_received || sigpipe_received)
2165 return got_error(GOT_ERR_CANCELLED);
2169 static const struct got_error *
2170 check_linear_ancestry(struct got_object_id *commit_id,
2171 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 struct got_object_id *yca_id;
2177 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2178 commit_id, base_commit_id, repo, check_cancelled, NULL);
2183 return got_error(GOT_ERR_ANCESTRY);
2186 * Require a straight line of history between the target commit
2187 * and the work tree's base commit.
2189 * Non-linear situations such as this require a rebase:
2191 * (commit) D F (base_commit)
2199 * 'got update' only handles linear cases:
2200 * Update forwards in time: A (base/yca) - B - C - D (commit)
2201 * Update backwards in time: D (base) - C - B - A (commit/yca)
2203 if (allow_forwards_in_time_only) {
2204 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2205 return got_error(GOT_ERR_ANCESTRY);
2206 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2207 got_object_id_cmp(base_commit_id, yca_id) != 0)
2208 return got_error(GOT_ERR_ANCESTRY);
2214 static const struct got_error *
2215 check_same_branch(struct got_object_id *commit_id,
2216 struct got_reference *head_ref, struct got_object_id *yca_id,
2217 struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_commit_graph *graph = NULL;
2221 struct got_object_id *head_commit_id = NULL;
2222 int is_same_branch = 0;
2224 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2228 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2232 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2237 err = got_commit_graph_open(&graph, "/", 1);
2241 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2242 check_cancelled, NULL);
2247 struct got_object_id *id;
2248 err = got_commit_graph_iter_next(&id, graph, repo,
2249 check_cancelled, NULL);
2251 if (err->code == GOT_ERR_ITER_COMPLETED)
2257 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2259 if (got_object_id_cmp(id, commit_id) == 0) {
2267 got_commit_graph_close(graph);
2268 free(head_commit_id);
2269 if (!err && !is_same_branch)
2270 err = got_error(GOT_ERR_ANCESTRY);
2274 static const struct got_error *
2275 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2277 static char msg[512];
2278 const char *branch_name;
2280 if (got_ref_is_symbolic(ref))
2281 branch_name = got_ref_get_symref_target(ref);
2283 branch_name = got_ref_get_name(ref);
2285 if (strncmp("refs/heads/", branch_name, 11) == 0)
2288 snprintf(msg, sizeof(msg),
2289 "target commit is not contained in branch '%s'; "
2290 "the branch to use must be specified with -b; "
2291 "if necessary a new branch can be created for "
2292 "this commit with 'got branch -c %s BRANCH_NAME'",
2293 branch_name, commit_id_str);
2295 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2298 static const struct got_error *
2299 cmd_checkout(int argc, char *argv[])
2301 const struct got_error *error = NULL;
2302 struct got_repository *repo = NULL;
2303 struct got_reference *head_ref = NULL;
2304 struct got_worktree *worktree = NULL;
2305 char *repo_path = NULL;
2306 char *worktree_path = NULL;
2307 const char *path_prefix = "";
2308 const char *branch_name = GOT_REF_HEAD;
2309 char *commit_id_str = NULL;
2310 int ch, same_path_prefix, allow_nonempty = 0;
2311 struct got_pathlist_head paths;
2312 struct got_checkout_progress_arg cpa;
2316 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2319 branch_name = optarg;
2322 commit_id_str = strdup(optarg);
2323 if (commit_id_str == NULL)
2324 return got_error_from_errno("strdup");
2330 path_prefix = optarg;
2342 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2343 "unveil", NULL) == -1)
2347 char *cwd, *base, *dotgit;
2348 repo_path = realpath(argv[0], NULL);
2349 if (repo_path == NULL)
2350 return got_error_from_errno2("realpath", argv[0]);
2351 cwd = getcwd(NULL, 0);
2353 error = got_error_from_errno("getcwd");
2356 if (path_prefix[0]) {
2357 base = basename(path_prefix);
2359 error = got_error_from_errno2("basename",
2364 base = basename(repo_path);
2366 error = got_error_from_errno2("basename",
2371 dotgit = strstr(base, ".git");
2374 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2375 error = got_error_from_errno("asprintf");
2380 } else if (argc == 2) {
2381 repo_path = realpath(argv[0], NULL);
2382 if (repo_path == NULL) {
2383 error = got_error_from_errno2("realpath", argv[0]);
2386 worktree_path = realpath(argv[1], NULL);
2387 if (worktree_path == NULL) {
2388 if (errno != ENOENT) {
2389 error = got_error_from_errno2("realpath",
2393 worktree_path = strdup(argv[1]);
2394 if (worktree_path == NULL) {
2395 error = got_error_from_errno("strdup");
2402 got_path_strip_trailing_slashes(repo_path);
2403 got_path_strip_trailing_slashes(worktree_path);
2405 error = got_repo_open(&repo, repo_path, NULL);
2409 /* Pre-create work tree path for unveil(2) */
2410 error = got_path_mkdir(worktree_path);
2412 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2413 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2415 if (!allow_nonempty &&
2416 !got_path_dir_is_empty(worktree_path)) {
2417 error = got_error_path(worktree_path,
2418 GOT_ERR_DIR_NOT_EMPTY);
2423 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2427 error = got_ref_open(&head_ref, repo, branch_name, 0);
2431 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2432 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2435 error = got_worktree_open(&worktree, worktree_path);
2439 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2443 if (!same_path_prefix) {
2444 error = got_error(GOT_ERR_PATH_PREFIX);
2448 if (commit_id_str) {
2449 struct got_object_id *commit_id;
2450 error = got_repo_match_object_id(&commit_id, NULL,
2451 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2454 error = check_linear_ancestry(commit_id,
2455 got_worktree_get_base_commit_id(worktree), 0, repo);
2456 if (error != NULL) {
2458 if (error->code == GOT_ERR_ANCESTRY) {
2459 error = checkout_ancestry_error(
2460 head_ref, commit_id_str);
2464 error = check_same_branch(commit_id, head_ref, NULL, repo);
2466 if (error->code == GOT_ERR_ANCESTRY) {
2467 error = checkout_ancestry_error(
2468 head_ref, commit_id_str);
2472 error = got_worktree_set_base_commit_id(worktree, repo,
2479 error = got_pathlist_append(&paths, "", NULL);
2482 cpa.worktree_path = worktree_path;
2483 cpa.had_base_commit_ref_error = 0;
2484 error = got_worktree_checkout_files(worktree, &paths, repo,
2485 checkout_progress, &cpa, check_cancelled, NULL);
2489 printf("Now shut up and hack\n");
2490 if (cpa.had_base_commit_ref_error)
2491 show_worktree_base_ref_warning();
2493 got_pathlist_free(&paths);
2494 free(commit_id_str);
2496 free(worktree_path);
2500 struct got_update_progress_arg {
2508 print_update_progress_stats(struct got_update_progress_arg *upa)
2510 if (!upa->did_something)
2513 if (upa->conflicts > 0)
2514 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2515 if (upa->obstructed > 0)
2516 printf("File paths obstructed by a non-regular file: %d\n",
2518 if (upa->not_updated > 0)
2519 printf("Files not updated because of existing merge "
2520 "conflicts: %d\n", upa->not_updated);
2526 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2531 static const struct got_error *
2532 update_progress(void *arg, unsigned char status, const char *path)
2534 struct got_update_progress_arg *upa = arg;
2536 if (status == GOT_STATUS_EXISTS ||
2537 status == GOT_STATUS_BASE_REF_ERR)
2540 upa->did_something = 1;
2542 /* Base commit bump happens silently. */
2543 if (status == GOT_STATUS_BUMP_BASE)
2546 if (status == GOT_STATUS_CONFLICT)
2548 if (status == GOT_STATUS_OBSTRUCTED)
2550 if (status == GOT_STATUS_CANNOT_UPDATE)
2553 while (path[0] == '/')
2555 printf("%c %s\n", status, path);
2559 static const struct got_error *
2560 switch_head_ref(struct got_reference *head_ref,
2561 struct got_object_id *commit_id, struct got_worktree *worktree,
2562 struct got_repository *repo)
2564 const struct got_error *err = NULL;
2566 int ref_has_moved = 0;
2568 /* Trivial case: switching between two different references. */
2569 if (strcmp(got_ref_get_name(head_ref),
2570 got_worktree_get_head_ref_name(worktree)) != 0) {
2571 printf("Switching work tree from %s to %s\n",
2572 got_worktree_get_head_ref_name(worktree),
2573 got_ref_get_name(head_ref));
2574 return got_worktree_set_head_ref(worktree, head_ref);
2577 err = check_linear_ancestry(commit_id,
2578 got_worktree_get_base_commit_id(worktree), 0, repo);
2580 if (err->code != GOT_ERR_ANCESTRY)
2587 /* Switching to a rebased branch with the same reference name. */
2588 err = got_object_id_str(&base_id_str,
2589 got_worktree_get_base_commit_id(worktree));
2592 printf("Reference %s now points at a different branch\n",
2593 got_worktree_get_head_ref_name(worktree));
2594 printf("Switching work tree from %s to %s\n", base_id_str,
2595 got_worktree_get_head_ref_name(worktree));
2599 static const struct got_error *
2600 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2602 const struct got_error *err;
2605 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2609 return got_error(GOT_ERR_REBASING);
2611 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2615 return got_error(GOT_ERR_HISTEDIT_BUSY);
2620 static const struct got_error *
2621 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2622 char *argv[], struct got_worktree *worktree)
2624 const struct got_error *err = NULL;
2631 return got_error_from_errno("strdup");
2632 return got_pathlist_append(paths, path, NULL);
2635 for (i = 0; i < argc; i++) {
2636 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2639 err = got_pathlist_append(paths, path, NULL);
2649 static const struct got_error *
2650 wrap_not_worktree_error(const struct got_error *orig_err,
2651 const char *cmdname, const char *path)
2653 const struct got_error *err;
2654 struct got_repository *repo;
2655 static char msg[512];
2657 err = got_repo_open(&repo, path, NULL);
2661 snprintf(msg, sizeof(msg),
2662 "'got %s' needs a work tree in addition to a git repository\n"
2663 "Work trees can be checked out from this Git repository with "
2665 "The got(1) manual page contains more information.", cmdname);
2666 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2667 got_repo_close(repo);
2671 static const struct got_error *
2672 cmd_update(int argc, char *argv[])
2674 const struct got_error *error = NULL;
2675 struct got_repository *repo = NULL;
2676 struct got_worktree *worktree = NULL;
2677 char *worktree_path = NULL;
2678 struct got_object_id *commit_id = NULL;
2679 char *commit_id_str = NULL;
2680 const char *branch_name = NULL;
2681 struct got_reference *head_ref = NULL;
2682 struct got_pathlist_head paths;
2683 struct got_pathlist_entry *pe;
2685 struct got_update_progress_arg upa;
2689 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2692 branch_name = optarg;
2695 commit_id_str = strdup(optarg);
2696 if (commit_id_str == NULL)
2697 return got_error_from_errno("strdup");
2709 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2710 "unveil", NULL) == -1)
2713 worktree_path = getcwd(NULL, 0);
2714 if (worktree_path == NULL) {
2715 error = got_error_from_errno("getcwd");
2718 error = got_worktree_open(&worktree, worktree_path);
2720 if (error->code == GOT_ERR_NOT_WORKTREE)
2721 error = wrap_not_worktree_error(error, "update",
2726 error = check_rebase_or_histedit_in_progress(worktree);
2730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2735 error = apply_unveil(got_repo_get_path(repo), 0,
2736 got_worktree_get_root_path(worktree));
2740 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2744 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2745 got_worktree_get_head_ref_name(worktree), 0);
2748 if (commit_id_str == NULL) {
2749 error = got_ref_resolve(&commit_id, repo, head_ref);
2752 error = got_object_id_str(&commit_id_str, commit_id);
2756 error = got_repo_match_object_id(&commit_id, NULL,
2757 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2758 free(commit_id_str);
2759 commit_id_str = NULL;
2762 error = got_object_id_str(&commit_id_str, commit_id);
2768 struct got_object_id *head_commit_id;
2769 TAILQ_FOREACH(pe, &paths, entry) {
2770 if (pe->path_len == 0)
2772 error = got_error_msg(GOT_ERR_BAD_PATH,
2773 "switching between branches requires that "
2774 "the entire work tree gets updated");
2777 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2780 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2782 free(head_commit_id);
2785 error = check_same_branch(commit_id, head_ref, NULL, repo);
2788 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2792 error = check_linear_ancestry(commit_id,
2793 got_worktree_get_base_commit_id(worktree), 0, repo);
2794 if (error != NULL) {
2795 if (error->code == GOT_ERR_ANCESTRY)
2796 error = got_error(GOT_ERR_BRANCH_MOVED);
2799 error = check_same_branch(commit_id, head_ref, NULL, repo);
2804 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2806 error = got_worktree_set_base_commit_id(worktree, repo,
2812 memset(&upa, 0, sizeof(upa));
2813 error = got_worktree_checkout_files(worktree, &paths, repo,
2814 update_progress, &upa, check_cancelled, NULL);
2818 if (upa.did_something)
2819 printf("Updated to commit %s\n", commit_id_str);
2821 printf("Already up-to-date\n");
2822 print_update_progress_stats(&upa);
2824 free(worktree_path);
2825 TAILQ_FOREACH(pe, &paths, entry)
2826 free((char *)pe->path);
2827 got_pathlist_free(&paths);
2829 free(commit_id_str);
2833 static const struct got_error *
2834 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2835 const char *path, int diff_context, int ignore_whitespace,
2836 struct got_repository *repo)
2838 const struct got_error *err = NULL;
2839 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2842 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2847 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2851 while (path[0] == '/')
2853 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2854 ignore_whitespace, stdout);
2857 got_object_blob_close(blob1);
2858 got_object_blob_close(blob2);
2862 static const struct got_error *
2863 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2864 const char *path, int diff_context, int ignore_whitespace,
2865 struct got_repository *repo)
2867 const struct got_error *err = NULL;
2868 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2869 struct got_diff_blob_output_unidiff_arg arg;
2872 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2877 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2881 arg.diff_context = diff_context;
2882 arg.ignore_whitespace = ignore_whitespace;
2883 arg.outfile = stdout;
2884 while (path[0] == '/')
2886 err = got_diff_tree(tree1, tree2, path, path, repo,
2887 got_diff_blob_output_unidiff, &arg, 1);
2890 got_object_tree_close(tree1);
2892 got_object_tree_close(tree2);
2896 static const struct got_error *
2897 get_changed_paths(struct got_pathlist_head *paths,
2898 struct got_commit_object *commit, struct got_repository *repo)
2900 const struct got_error *err = NULL;
2901 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2902 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2903 struct got_object_qid *qid;
2905 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2907 struct got_commit_object *pcommit;
2908 err = got_object_open_as_commit(&pcommit, repo,
2913 tree_id1 = got_object_commit_get_tree_id(pcommit);
2914 got_object_commit_close(pcommit);
2919 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2924 tree_id2 = got_object_commit_get_tree_id(commit);
2925 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2929 err = got_diff_tree(tree1, tree2, "", "", repo,
2930 got_diff_tree_collect_changed_paths, paths, 0);
2933 got_object_tree_close(tree1);
2935 got_object_tree_close(tree2);
2939 static const struct got_error *
2940 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2941 const char *path, int diff_context, struct got_repository *repo)
2943 const struct got_error *err = NULL;
2944 struct got_commit_object *pcommit = NULL;
2945 char *id_str1 = NULL, *id_str2 = NULL;
2946 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2947 struct got_object_qid *qid;
2949 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2951 err = got_object_open_as_commit(&pcommit, repo,
2957 if (path && path[0] != '\0') {
2959 err = got_object_id_by_path(&obj_id2, repo, id, path);
2962 err = got_object_id_str(&id_str2, obj_id2);
2968 err = got_object_id_by_path(&obj_id1, repo,
2971 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
2976 err = got_object_id_str(&id_str1, obj_id1);
2983 err = got_object_get_type(&obj_type, repo, obj_id2);
2988 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2990 case GOT_OBJ_TYPE_BLOB:
2991 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2994 case GOT_OBJ_TYPE_TREE:
2995 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2999 err = got_error(GOT_ERR_OBJ_TYPE);
3005 obj_id2 = got_object_commit_get_tree_id(commit);
3006 err = got_object_id_str(&id_str2, obj_id2);
3009 obj_id1 = got_object_commit_get_tree_id(pcommit);
3010 err = got_object_id_str(&id_str1, obj_id1);
3013 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3014 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3020 got_object_commit_close(pcommit);
3025 get_datestr(time_t *time, char *datebuf)
3027 struct tm mytm, *tm;
3030 tm = gmtime_r(time, &mytm);
3033 s = asctime_r(tm, datebuf);
3036 p = strchr(s, '\n');
3042 static const struct got_error *
3043 match_logmsg(int *have_match, struct got_object_id *id,
3044 struct got_commit_object *commit, regex_t *regex)
3046 const struct got_error *err = NULL;
3047 regmatch_t regmatch;
3048 char *id_str = NULL, *logmsg = NULL;
3052 err = got_object_id_str(&id_str, id);
3056 err = got_object_commit_get_logmsg(&logmsg, commit);
3060 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3069 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3072 regmatch_t regmatch;
3073 struct got_pathlist_entry *pe;
3077 TAILQ_FOREACH(pe, changed_paths, entry) {
3078 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3085 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3087 static const struct got_error *
3088 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3089 struct got_repository *repo, const char *path,
3090 struct got_pathlist_head *changed_paths, int show_patch,
3091 int diff_context, struct got_reflist_head *refs)
3093 const struct got_error *err = NULL;
3094 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3096 time_t committer_time;
3097 const char *author, *committer;
3098 char *refs_str = NULL;
3099 struct got_reflist_entry *re;
3101 SIMPLEQ_FOREACH(re, refs, entry) {
3104 struct got_tag_object *tag = NULL;
3107 name = got_ref_get_name(re->ref);
3108 if (strcmp(name, GOT_REF_HEAD) == 0)
3110 if (strncmp(name, "refs/", 5) == 0)
3112 if (strncmp(name, "got/", 4) == 0)
3114 if (strncmp(name, "heads/", 6) == 0)
3116 if (strncmp(name, "remotes/", 8) == 0) {
3118 s = strstr(name, "/" GOT_REF_HEAD);
3119 if (s != NULL && s[strlen(s)] == '\0')
3122 if (strncmp(name, "tags/", 5) == 0) {
3123 err = got_object_open_as_tag(&tag, repo, re->id);
3125 if (err->code != GOT_ERR_OBJ_TYPE)
3127 /* Ref points at something other than a tag. */
3132 cmp = got_object_id_cmp(tag ?
3133 got_object_tag_get_object_id(tag) : re->id, id);
3135 got_object_tag_close(tag);
3139 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3141 err = got_error_from_errno("asprintf");
3147 err = got_object_id_str(&id_str, id);
3151 printf(GOT_COMMIT_SEP_STR);
3152 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3153 refs_str ? refs_str : "", refs_str ? ")" : "");
3158 printf("from: %s\n", got_object_commit_get_author(commit));
3159 committer_time = got_object_commit_get_committer_time(commit);
3160 datestr = get_datestr(&committer_time, datebuf);
3162 printf("date: %s UTC\n", datestr);
3163 author = got_object_commit_get_author(commit);
3164 committer = got_object_commit_get_committer(commit);
3165 if (strcmp(author, committer) != 0)
3166 printf("via: %s\n", committer);
3167 if (got_object_commit_get_nparents(commit) > 1) {
3168 const struct got_object_id_queue *parent_ids;
3169 struct got_object_qid *qid;
3171 parent_ids = got_object_commit_get_parent_ids(commit);
3172 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3173 err = got_object_id_str(&id_str, qid->id);
3176 printf("parent %d: %s\n", n++, id_str);
3181 err = got_object_commit_get_logmsg(&logmsg0, commit);
3187 line = strsep(&logmsg, "\n");
3189 printf(" %s\n", line);
3193 if (changed_paths) {
3194 struct got_pathlist_entry *pe;
3195 TAILQ_FOREACH(pe, changed_paths, entry) {
3196 struct got_diff_changed_path *cp = pe->data;
3197 printf(" %c %s\n", cp->status, pe->path);
3202 err = print_patch(commit, id, path, diff_context, repo);
3207 if (fflush(stdout) != 0 && err == NULL)
3208 err = got_error_from_errno("fflush");
3212 static const struct got_error *
3213 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3214 struct got_repository *repo, const char *path, int show_changed_paths,
3215 int show_patch, const char *search_pattern, int diff_context, int limit,
3216 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3218 const struct got_error *err;
3219 struct got_commit_graph *graph;
3222 struct got_object_id_queue reversed_commits;
3223 struct got_object_qid *qid;
3224 struct got_commit_object *commit;
3225 struct got_pathlist_head changed_paths;
3226 struct got_pathlist_entry *pe;
3228 SIMPLEQ_INIT(&reversed_commits);
3229 TAILQ_INIT(&changed_paths);
3231 if (search_pattern && regcomp(®ex, search_pattern,
3232 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3233 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3235 err = got_commit_graph_open(&graph, path, !log_branches);
3238 err = got_commit_graph_iter_start(graph, root_id, repo,
3239 check_cancelled, NULL);
3243 struct got_object_id *id;
3245 if (sigint_received || sigpipe_received)
3248 err = got_commit_graph_iter_next(&id, graph, repo,
3249 check_cancelled, NULL);
3251 if (err->code == GOT_ERR_ITER_COMPLETED)
3258 err = got_object_open_as_commit(&commit, repo, id);
3262 if (show_changed_paths) {
3263 err = get_changed_paths(&changed_paths, commit, repo);
3268 if (search_pattern) {
3269 err = match_logmsg(&have_match, id, commit, ®ex);
3271 got_object_commit_close(commit);
3274 if (have_match == 0 && show_changed_paths)
3275 match_changed_paths(&have_match,
3276 &changed_paths, ®ex);
3277 if (have_match == 0) {
3278 got_object_commit_close(commit);
3279 TAILQ_FOREACH(pe, &changed_paths, entry) {
3280 free((char *)pe->path);
3283 got_pathlist_free(&changed_paths);
3288 if (reverse_display_order) {
3289 err = got_object_qid_alloc(&qid, id);
3292 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3293 got_object_commit_close(commit);
3295 err = print_commit(commit, id, repo, path,
3296 show_changed_paths ? &changed_paths : NULL,
3297 show_patch, diff_context, refs);
3298 got_object_commit_close(commit);
3302 if ((limit && --limit == 0) ||
3303 (end_id && got_object_id_cmp(id, end_id) == 0))
3306 TAILQ_FOREACH(pe, &changed_paths, entry) {
3307 free((char *)pe->path);
3310 got_pathlist_free(&changed_paths);
3312 if (reverse_display_order) {
3313 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3314 err = got_object_open_as_commit(&commit, repo, qid->id);
3317 err = print_commit(commit, qid->id, repo, path,
3318 show_changed_paths ? &changed_paths : NULL,
3319 show_patch, diff_context, refs);
3320 got_object_commit_close(commit);
3326 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3327 qid = SIMPLEQ_FIRST(&reversed_commits);
3328 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3329 got_object_qid_free(qid);
3331 TAILQ_FOREACH(pe, &changed_paths, entry) {
3332 free((char *)pe->path);
3335 got_pathlist_free(&changed_paths);
3338 got_commit_graph_close(graph);
3345 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3346 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3347 "[-R] [path]\n", getprogname());
3352 get_default_log_limit(void)
3354 const char *got_default_log_limit;
3358 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3359 if (got_default_log_limit == NULL)
3361 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3367 static const struct got_error *
3368 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3369 struct got_repository *repo)
3371 const struct got_error *err = NULL;
3372 struct got_reference *ref;
3376 err = got_ref_open(&ref, repo, commit_arg, 0);
3379 err = got_ref_resolve(id, repo, ref);
3383 err = got_object_get_type(&obj_type, repo, *id);
3386 if (obj_type == GOT_OBJ_TYPE_TAG) {
3387 struct got_tag_object *tag;
3388 err = got_object_open_as_tag(&tag, repo, *id);
3391 if (got_object_tag_get_object_type(tag) !=
3392 GOT_OBJ_TYPE_COMMIT) {
3393 got_object_tag_close(tag);
3394 return got_error(GOT_ERR_OBJ_TYPE);
3397 *id = got_object_id_dup(
3398 got_object_tag_get_object_id(tag));
3400 err = got_error_from_errno(
3401 "got_object_id_dup");
3402 got_object_tag_close(tag);
3405 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3406 return got_error(GOT_ERR_OBJ_TYPE);
3408 err = got_repo_match_object_id_prefix(id, commit_arg,
3409 GOT_OBJ_TYPE_COMMIT, repo);
3415 static const struct got_error *
3416 cmd_log(int argc, char *argv[])
3418 const struct got_error *error;
3419 struct got_repository *repo = NULL;
3420 struct got_worktree *worktree = NULL;
3421 struct got_object_id *start_id = NULL, *end_id = NULL;
3422 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3423 const char *start_commit = NULL, *end_commit = NULL;
3424 const char *search_pattern = NULL;
3425 int diff_context = -1, ch;
3426 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3427 int reverse_display_order = 0;
3429 struct got_reflist_head refs;
3431 SIMPLEQ_INIT(&refs);
3434 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3440 limit = get_default_log_limit();
3442 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3448 show_changed_paths = 1;
3451 start_commit = optarg;
3454 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3457 err(1, "-C option %s", errstr);
3460 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3462 err(1, "-l option %s", errstr);
3468 repo_path = realpath(optarg, NULL);
3469 if (repo_path == NULL)
3470 return got_error_from_errno2("realpath",
3472 got_path_strip_trailing_slashes(repo_path);
3475 reverse_display_order = 1;
3478 search_pattern = optarg;
3481 end_commit = optarg;
3492 if (diff_context == -1)
3494 else if (!show_patch)
3495 errx(1, "-C reguires -p");
3497 cwd = getcwd(NULL, 0);
3499 error = got_error_from_errno("getcwd");
3503 if (repo_path == NULL) {
3504 error = got_worktree_open(&worktree, cwd);
3505 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3513 error = got_error_from_errno("strdup");
3516 } else if (argc == 1) {
3518 error = got_worktree_resolve_path(&path, worktree,
3523 path = strdup(argv[0]);
3525 error = got_error_from_errno("strdup");
3532 if (repo_path == NULL) {
3533 repo_path = worktree ?
3534 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3536 if (repo_path == NULL) {
3537 error = got_error_from_errno("strdup");
3541 error = got_repo_open(&repo, repo_path, NULL);
3545 error = apply_unveil(got_repo_get_path(repo), 1,
3546 worktree ? got_worktree_get_root_path(worktree) : NULL);
3550 if (start_commit == NULL) {
3551 struct got_reference *head_ref;
3552 struct got_commit_object *commit = NULL;
3553 error = got_ref_open(&head_ref, repo,
3554 worktree ? got_worktree_get_head_ref_name(worktree)
3558 error = got_ref_resolve(&start_id, repo, head_ref);
3559 got_ref_close(head_ref);
3562 error = got_object_open_as_commit(&commit, repo,
3566 got_object_commit_close(commit);
3568 error = resolve_commit_arg(&start_id, start_commit, repo);
3572 if (end_commit != NULL) {
3573 error = resolve_commit_arg(&end_id, end_commit, repo);
3579 const char *prefix = got_worktree_get_path_prefix(worktree);
3581 if (asprintf(&p, "%s%s%s", prefix,
3582 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3583 error = got_error_from_errno("asprintf");
3586 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3589 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3594 path = in_repo_path;
3597 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3601 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3602 show_patch, search_pattern, diff_context, limit, log_branches,
3603 reverse_display_order, &refs);
3609 got_worktree_close(worktree);
3611 const struct got_error *repo_error;
3612 repo_error = got_repo_close(repo);
3616 got_ref_list_free(&refs);
3623 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3624 "[-w] [object1 object2 | path]\n", getprogname());
3628 struct print_diff_arg {
3629 struct got_repository *repo;
3630 struct got_worktree *worktree;
3635 int ignore_whitespace;
3639 * Create a file which contains the target path of a symlink so we can feed
3640 * it as content to the diff engine.
3642 static const struct got_error *
3643 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3644 const char *abspath)
3646 const struct got_error *err = NULL;
3647 char target_path[PATH_MAX];
3648 ssize_t target_len, outlen;
3653 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3654 if (target_len == -1)
3655 return got_error_from_errno2("readlinkat", abspath);
3657 target_len = readlink(abspath, target_path, PATH_MAX);
3658 if (target_len == -1)
3659 return got_error_from_errno2("readlink", abspath);
3662 *fd = got_opentempfd();
3664 return got_error_from_errno("got_opentempfd");
3666 outlen = write(*fd, target_path, target_len);
3668 err = got_error_from_errno("got_opentempfd");
3672 if (lseek(*fd, 0, SEEK_SET) == -1) {
3673 err = got_error_from_errno2("lseek", abspath);
3684 static const struct got_error *
3685 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3686 const char *path, struct got_object_id *blob_id,
3687 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3688 int dirfd, const char *de_name)
3690 struct print_diff_arg *a = arg;
3691 const struct got_error *err = NULL;
3692 struct got_blob_object *blob1 = NULL;
3695 char *abspath = NULL, *label1 = NULL;
3698 if (a->diff_staged) {
3699 if (staged_status != GOT_STATUS_MODIFY &&
3700 staged_status != GOT_STATUS_ADD &&
3701 staged_status != GOT_STATUS_DELETE)
3704 if (staged_status == GOT_STATUS_DELETE)
3706 if (status == GOT_STATUS_NONEXISTENT)
3707 return got_error_set_errno(ENOENT, path);
3708 if (status != GOT_STATUS_MODIFY &&
3709 status != GOT_STATUS_ADD &&
3710 status != GOT_STATUS_DELETE &&
3711 status != GOT_STATUS_CONFLICT)
3715 if (!a->header_shown) {
3716 printf("diff %s %s%s\n", a->id_str,
3717 got_worktree_get_root_path(a->worktree),
3718 a->diff_staged ? " (staged changes)" : "");
3719 a->header_shown = 1;
3722 if (a->diff_staged) {
3723 const char *label1 = NULL, *label2 = NULL;
3724 switch (staged_status) {
3725 case GOT_STATUS_MODIFY:
3729 case GOT_STATUS_ADD:
3732 case GOT_STATUS_DELETE:
3736 return got_error(GOT_ERR_FILE_STATUS);
3738 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3739 label1, label2, a->diff_context, a->ignore_whitespace,
3743 if (staged_status == GOT_STATUS_ADD ||
3744 staged_status == GOT_STATUS_MODIFY) {
3746 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3750 err = got_object_id_str(&id_str, staged_blob_id);
3753 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3754 err = got_error_from_errno("asprintf");
3759 } else if (status != GOT_STATUS_ADD) {
3760 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3765 if (status != GOT_STATUS_DELETE) {
3766 if (asprintf(&abspath, "%s/%s",
3767 got_worktree_get_root_path(a->worktree), path) == -1) {
3768 err = got_error_from_errno("asprintf");
3773 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3775 if (errno != ELOOP) {
3776 err = got_error_from_errno2("openat",
3780 err = get_symlink_target_file(&fd, dirfd,
3786 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3788 if (errno != ELOOP) {
3789 err = got_error_from_errno2("open",
3793 err = get_symlink_target_file(&fd, dirfd,
3799 if (fstat(fd, &sb) == -1) {
3800 err = got_error_from_errno2("fstat", abspath);
3803 f2 = fdopen(fd, "r");
3805 err = got_error_from_errno2("fdopen", abspath);
3812 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3813 a->diff_context, a->ignore_whitespace, stdout);
3816 got_object_blob_close(blob1);
3817 if (f2 && fclose(f2) == EOF && err == NULL)
3818 err = got_error_from_errno("fclose");
3819 if (fd != -1 && close(fd) == -1 && err == NULL)
3820 err = got_error_from_errno("close");
3825 static const struct got_error *
3826 cmd_diff(int argc, char *argv[])
3828 const struct got_error *error;
3829 struct got_repository *repo = NULL;
3830 struct got_worktree *worktree = NULL;
3831 char *cwd = NULL, *repo_path = NULL;
3832 struct got_object_id *id1 = NULL, *id2 = NULL;
3833 const char *id_str1 = NULL, *id_str2 = NULL;
3834 char *label1 = NULL, *label2 = NULL;
3836 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3841 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3846 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3849 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3852 err(1, "-C option %s", errstr);
3855 repo_path = realpath(optarg, NULL);
3856 if (repo_path == NULL)
3857 return got_error_from_errno2("realpath",
3859 got_path_strip_trailing_slashes(repo_path);
3865 ignore_whitespace = 1;
3876 cwd = getcwd(NULL, 0);
3878 error = got_error_from_errno("getcwd");
3884 "-r option can't be used when diffing a work tree");
3885 error = got_worktree_open(&worktree, cwd);
3887 if (error->code == GOT_ERR_NOT_WORKTREE)
3888 error = wrap_not_worktree_error(error, "diff",
3892 repo_path = strdup(got_worktree_get_repo_path(worktree));
3893 if (repo_path == NULL) {
3894 error = got_error_from_errno("strdup");
3898 error = got_worktree_resolve_path(&path, worktree,
3905 error = got_error_from_errno("strdup");
3909 } else if (argc == 2) {
3911 errx(1, "-s option can't be used when diffing "
3912 "objects in repository");
3915 if (repo_path == NULL) {
3916 error = got_worktree_open(&worktree, cwd);
3917 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3921 got_worktree_get_repo_path(worktree));
3922 if (repo_path == NULL) {
3923 error = got_error_from_errno("strdup");
3927 repo_path = strdup(cwd);
3928 if (repo_path == NULL) {
3929 error = got_error_from_errno("strdup");
3937 error = got_repo_open(&repo, repo_path, NULL);
3942 error = apply_unveil(got_repo_get_path(repo), 1,
3943 worktree ? got_worktree_get_root_path(worktree) : NULL);
3948 struct print_diff_arg arg;
3949 struct got_pathlist_head paths;
3954 error = got_object_id_str(&id_str,
3955 got_worktree_get_base_commit_id(worktree));
3959 arg.worktree = worktree;
3960 arg.diff_context = diff_context;
3961 arg.id_str = id_str;
3962 arg.header_shown = 0;
3963 arg.diff_staged = diff_staged;
3964 arg.ignore_whitespace = ignore_whitespace;
3966 error = got_pathlist_append(&paths, path, NULL);
3970 error = got_worktree_status(worktree, &paths, repo, print_diff,
3971 &arg, check_cancelled, NULL);
3973 got_pathlist_free(&paths);
3977 error = got_repo_match_object_id(&id1, &label1, id_str1,
3978 GOT_OBJ_TYPE_ANY, 1, repo);
3982 error = got_repo_match_object_id(&id2, &label2, id_str2,
3983 GOT_OBJ_TYPE_ANY, 1, repo);
3987 error = got_object_get_type(&type1, repo, id1);
3991 error = got_object_get_type(&type2, repo, id2);
3995 if (type1 != type2) {
3996 error = got_error(GOT_ERR_OBJ_TYPE);
4001 case GOT_OBJ_TYPE_BLOB:
4002 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4003 diff_context, ignore_whitespace, repo, stdout);
4005 case GOT_OBJ_TYPE_TREE:
4006 error = got_diff_objects_as_trees(id1, id2, "", "",
4007 diff_context, ignore_whitespace, repo, stdout);
4009 case GOT_OBJ_TYPE_COMMIT:
4010 printf("diff %s %s\n", label1, label2);
4011 error = got_diff_objects_as_commits(id1, id2, diff_context,
4012 ignore_whitespace, repo, stdout);
4015 error = got_error(GOT_ERR_OBJ_TYPE);
4024 got_worktree_close(worktree);
4026 const struct got_error *repo_error;
4027 repo_error = got_repo_close(repo);
4038 "usage: %s blame [-c commit] [-r repository-path] path\n",
4047 char datebuf[11]; /* YYYY-MM-DD + NUL */
4050 struct blame_cb_args {
4051 struct blame_line *lines;
4055 off_t *line_offsets;
4057 struct got_repository *repo;
4060 static const struct got_error *
4061 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4063 const struct got_error *err = NULL;
4064 struct blame_cb_args *a = arg;
4065 struct blame_line *bline;
4067 size_t linesize = 0;
4068 struct got_commit_object *commit = NULL;
4071 time_t committer_time;
4073 if (nlines != a->nlines ||
4074 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4075 return got_error(GOT_ERR_RANGE);
4077 if (sigint_received)
4078 return got_error(GOT_ERR_ITER_COMPLETED);
4081 return NULL; /* no change in this commit */
4083 /* Annotate this line. */
4084 bline = &a->lines[lineno - 1];
4085 if (bline->annotated)
4087 err = got_object_id_str(&bline->id_str, id);
4091 err = got_object_open_as_commit(&commit, a->repo, id);
4095 bline->committer = strdup(got_object_commit_get_committer(commit));
4096 if (bline->committer == NULL) {
4097 err = got_error_from_errno("strdup");
4101 committer_time = got_object_commit_get_committer_time(commit);
4102 if (localtime_r(&committer_time, &tm) == NULL)
4103 return got_error_from_errno("localtime_r");
4104 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4105 &tm) >= sizeof(bline->datebuf)) {
4106 err = got_error(GOT_ERR_NO_SPACE);
4109 bline->annotated = 1;
4111 /* Print lines annotated so far. */
4112 bline = &a->lines[a->lineno_cur - 1];
4113 if (!bline->annotated)
4116 offset = a->line_offsets[a->lineno_cur - 1];
4117 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4118 err = got_error_from_errno("fseeko");
4122 while (bline->annotated) {
4123 char *smallerthan, *at, *nl, *committer;
4126 if (getline(&line, &linesize, a->f) == -1) {
4128 err = got_error_from_errno("getline");
4132 committer = bline->committer;
4133 smallerthan = strchr(committer, '<');
4134 if (smallerthan && smallerthan[1] != '\0')
4135 committer = smallerthan + 1;
4136 at = strchr(committer, '@');
4139 len = strlen(committer);
4141 committer[8] = '\0';
4143 nl = strchr(line, '\n');
4146 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4147 bline->id_str, bline->datebuf, committer, line);
4150 bline = &a->lines[a->lineno_cur - 1];
4154 got_object_commit_close(commit);
4159 static const struct got_error *
4160 cmd_blame(int argc, char *argv[])
4162 const struct got_error *error;
4163 struct got_repository *repo = NULL;
4164 struct got_worktree *worktree = NULL;
4165 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4166 char *link_target = NULL;
4167 struct got_object_id *obj_id = NULL;
4168 struct got_object_id *commit_id = NULL;
4169 struct got_blob_object *blob = NULL;
4170 char *commit_id_str = NULL;
4171 struct blame_cb_args bca;
4172 int ch, obj_type, i;
4175 memset(&bca, 0, sizeof(bca));
4178 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4183 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4186 commit_id_str = optarg;
4189 repo_path = realpath(optarg, NULL);
4190 if (repo_path == NULL)
4191 return got_error_from_errno2("realpath",
4193 got_path_strip_trailing_slashes(repo_path);
4209 cwd = getcwd(NULL, 0);
4211 error = got_error_from_errno("getcwd");
4214 if (repo_path == NULL) {
4215 error = got_worktree_open(&worktree, cwd);
4216 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4222 strdup(got_worktree_get_repo_path(worktree));
4223 if (repo_path == NULL) {
4224 error = got_error_from_errno("strdup");
4229 repo_path = strdup(cwd);
4230 if (repo_path == NULL) {
4231 error = got_error_from_errno("strdup");
4237 error = got_repo_open(&repo, repo_path, NULL);
4241 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4246 const char *prefix = got_worktree_get_path_prefix(worktree);
4247 char *p, *worktree_subdir = cwd +
4248 strlen(got_worktree_get_root_path(worktree));
4249 if (asprintf(&p, "%s%s%s%s%s",
4250 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4251 worktree_subdir, worktree_subdir[0] ? "/" : "",
4253 error = got_error_from_errno("asprintf");
4256 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4259 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4264 if (commit_id_str == NULL) {
4265 struct got_reference *head_ref;
4266 error = got_ref_open(&head_ref, repo, worktree ?
4267 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4270 error = got_ref_resolve(&commit_id, repo, head_ref);
4271 got_ref_close(head_ref);
4275 error = got_repo_match_object_id(&commit_id, NULL,
4276 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4281 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4286 error = got_object_id_by_path(&obj_id, repo, commit_id,
4287 link_target ? link_target : in_repo_path);
4291 error = got_object_get_type(&obj_type, repo, obj_id);
4295 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4296 error = got_error_path(link_target ? link_target : in_repo_path,
4301 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4304 bca.f = got_opentemp();
4305 if (bca.f == NULL) {
4306 error = got_error_from_errno("got_opentemp");
4309 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4310 &bca.line_offsets, bca.f, blob);
4311 if (error || bca.nlines == 0)
4314 /* Don't include \n at EOF in the blame line count. */
4315 if (bca.line_offsets[bca.nlines - 1] == filesize)
4318 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4319 if (bca.lines == NULL) {
4320 error = got_error_from_errno("calloc");
4324 bca.nlines_prec = 0;
4332 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4333 repo, blame_cb, &bca, check_cancelled, NULL);
4342 got_object_blob_close(blob);
4344 got_worktree_close(worktree);
4346 const struct got_error *repo_error;
4347 repo_error = got_repo_close(repo);
4352 for (i = 0; i < bca.nlines; i++) {
4353 struct blame_line *bline = &bca.lines[i];
4354 free(bline->id_str);
4355 free(bline->committer);
4359 free(bca.line_offsets);
4360 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4361 error = got_error_from_errno("fclose");
4369 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4374 static const struct got_error *
4375 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4376 const char *root_path, struct got_repository *repo)
4378 const struct got_error *err = NULL;
4379 int is_root_path = (strcmp(path, root_path) == 0);
4380 const char *modestr = "";
4381 mode_t mode = got_tree_entry_get_mode(te);
4382 char *link_target = NULL;
4384 path += strlen(root_path);
4385 while (path[0] == '/')
4388 if (got_object_tree_entry_is_submodule(te))
4390 else if (S_ISLNK(mode)) {
4393 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4396 for (i = 0; i < strlen(link_target); i++) {
4397 if (!isprint((unsigned char)link_target[i]))
4398 link_target[i] = '?';
4403 else if (S_ISDIR(mode))
4405 else if (mode & S_IXUSR)
4408 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4409 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4410 link_target ? " -> ": "", link_target ? link_target : "");
4416 static const struct got_error *
4417 print_tree(const char *path, struct got_object_id *commit_id,
4418 int show_ids, int recurse, const char *root_path,
4419 struct got_repository *repo)
4421 const struct got_error *err = NULL;
4422 struct got_object_id *tree_id = NULL;
4423 struct got_tree_object *tree = NULL;
4426 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4430 err = got_object_open_as_tree(&tree, repo, tree_id);
4433 nentries = got_object_tree_get_nentries(tree);
4434 for (i = 0; i < nentries; i++) {
4435 struct got_tree_entry *te;
4438 if (sigint_received || sigpipe_received)
4441 te = got_object_tree_get_entry(tree, i);
4444 err = got_object_id_str(&id_str,
4445 got_tree_entry_get_id(te));
4448 if (asprintf(&id, "%s ", id_str) == -1) {
4449 err = got_error_from_errno("asprintf");
4455 err = print_entry(te, id, path, root_path, repo);
4460 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4462 if (asprintf(&child_path, "%s%s%s", path,
4463 path[0] == '/' && path[1] == '\0' ? "" : "/",
4464 got_tree_entry_get_name(te)) == -1) {
4465 err = got_error_from_errno("asprintf");
4468 err = print_tree(child_path, commit_id, show_ids, 1,
4477 got_object_tree_close(tree);
4482 static const struct got_error *
4483 cmd_tree(int argc, char *argv[])
4485 const struct got_error *error;
4486 struct got_repository *repo = NULL;
4487 struct got_worktree *worktree = NULL;
4488 const char *path, *refname = NULL;
4489 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4490 struct got_object_id *commit_id = NULL;
4491 char *commit_id_str = NULL;
4492 int show_ids = 0, recurse = 0;
4496 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4501 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4504 commit_id_str = optarg;
4507 repo_path = realpath(optarg, NULL);
4508 if (repo_path == NULL)
4509 return got_error_from_errno2("realpath",
4511 got_path_strip_trailing_slashes(repo_path);
4535 cwd = getcwd(NULL, 0);
4537 error = got_error_from_errno("getcwd");
4540 if (repo_path == NULL) {
4541 error = got_worktree_open(&worktree, cwd);
4542 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4548 strdup(got_worktree_get_repo_path(worktree));
4549 if (repo_path == NULL)
4550 error = got_error_from_errno("strdup");
4554 repo_path = strdup(cwd);
4555 if (repo_path == NULL) {
4556 error = got_error_from_errno("strdup");
4562 error = got_repo_open(&repo, repo_path, NULL);
4566 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4572 char *p, *worktree_subdir = cwd +
4573 strlen(got_worktree_get_root_path(worktree));
4574 if (asprintf(&p, "%s/%s",
4575 got_worktree_get_path_prefix(worktree),
4576 worktree_subdir) == -1) {
4577 error = got_error_from_errno("asprintf");
4580 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4587 if (in_repo_path == NULL) {
4588 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4593 if (commit_id_str == NULL) {
4594 struct got_reference *head_ref;
4596 refname = got_worktree_get_head_ref_name(worktree);
4598 refname = GOT_REF_HEAD;
4599 error = got_ref_open(&head_ref, repo, refname, 0);
4602 error = got_ref_resolve(&commit_id, repo, head_ref);
4603 got_ref_close(head_ref);
4607 error = got_repo_match_object_id(&commit_id, NULL,
4608 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4613 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4614 in_repo_path, repo);
4621 got_worktree_close(worktree);
4623 const struct got_error *repo_error;
4624 repo_error = got_repo_close(repo);
4634 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4638 static const struct got_error *
4639 print_status(void *arg, unsigned char status, unsigned char staged_status,
4640 const char *path, struct got_object_id *blob_id,
4641 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4642 int dirfd, const char *de_name)
4644 if (status == staged_status && (status == GOT_STATUS_DELETE))
4645 status = GOT_STATUS_NO_CHANGE;
4646 printf("%c%c %s\n", status, staged_status, path);
4650 static const struct got_error *
4651 cmd_status(int argc, char *argv[])
4653 const struct got_error *error = NULL;
4654 struct got_repository *repo = NULL;
4655 struct got_worktree *worktree = NULL;
4657 struct got_pathlist_head paths;
4658 struct got_pathlist_entry *pe;
4663 while ((ch = getopt(argc, argv, "")) != -1) {
4675 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4679 cwd = getcwd(NULL, 0);
4681 error = got_error_from_errno("getcwd");
4685 error = got_worktree_open(&worktree, cwd);
4687 if (error->code == GOT_ERR_NOT_WORKTREE)
4688 error = wrap_not_worktree_error(error, "status", cwd);
4692 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4697 error = apply_unveil(got_repo_get_path(repo), 1,
4698 got_worktree_get_root_path(worktree));
4702 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4706 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4707 check_cancelled, NULL);
4709 TAILQ_FOREACH(pe, &paths, entry)
4710 free((char *)pe->path);
4711 got_pathlist_free(&paths);
4720 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4726 static const struct got_error *
4727 list_refs(struct got_repository *repo, const char *refname)
4729 static const struct got_error *err = NULL;
4730 struct got_reflist_head refs;
4731 struct got_reflist_entry *re;
4733 SIMPLEQ_INIT(&refs);
4734 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4738 SIMPLEQ_FOREACH(re, &refs, entry) {
4740 refstr = got_ref_to_str(re->ref);
4742 return got_error_from_errno("got_ref_to_str");
4743 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4747 got_ref_list_free(&refs);
4751 static const struct got_error *
4752 delete_ref(struct got_repository *repo, const char *refname)
4754 const struct got_error *err = NULL;
4755 struct got_reference *ref;
4757 err = got_ref_open(&ref, repo, refname, 0);
4761 err = got_ref_delete(ref, repo);
4766 static const struct got_error *
4767 add_ref(struct got_repository *repo, const char *refname, const char *target)
4769 const struct got_error *err = NULL;
4770 struct got_object_id *id;
4771 struct got_reference *ref = NULL;
4774 * Don't let the user create a reference name with a leading '-'.
4775 * While technically a valid reference name, this case is usually
4776 * an unintended typo.
4778 if (refname[0] == '-')
4779 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4781 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4784 struct got_reference *target_ref;
4786 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4788 err = got_ref_open(&target_ref, repo, target, 0);
4791 err = got_ref_resolve(&id, repo, target_ref);
4792 got_ref_close(target_ref);
4797 err = got_ref_alloc(&ref, refname, id);
4801 err = got_ref_write(ref, repo);
4809 static const struct got_error *
4810 add_symref(struct got_repository *repo, const char *refname, const char *target)
4812 const struct got_error *err = NULL;
4813 struct got_reference *ref = NULL;
4814 struct got_reference *target_ref = NULL;
4817 * Don't let the user create a reference name with a leading '-'.
4818 * While technically a valid reference name, this case is usually
4819 * an unintended typo.
4821 if (refname[0] == '-')
4822 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4824 err = got_ref_open(&target_ref, repo, target, 0);
4828 err = got_ref_alloc_symref(&ref, refname, target_ref);
4832 err = got_ref_write(ref, repo);
4835 got_ref_close(target_ref);
4841 static const struct got_error *
4842 cmd_ref(int argc, char *argv[])
4844 const struct got_error *error = NULL;
4845 struct got_repository *repo = NULL;
4846 struct got_worktree *worktree = NULL;
4847 char *cwd = NULL, *repo_path = NULL;
4848 int ch, do_list = 0, do_delete = 0;
4849 const char *obj_arg = NULL, *symref_target= NULL;
4850 char *refname = NULL;
4852 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4861 repo_path = realpath(optarg, NULL);
4862 if (repo_path == NULL)
4863 return got_error_from_errno2("realpath",
4865 got_path_strip_trailing_slashes(repo_path);
4871 symref_target = optarg;
4879 if (obj_arg && do_list)
4880 errx(1, "-c and -l options are mutually exclusive");
4881 if (obj_arg && do_delete)
4882 errx(1, "-c and -d options are mutually exclusive");
4883 if (obj_arg && symref_target)
4884 errx(1, "-c and -s options are mutually exclusive");
4885 if (symref_target && do_delete)
4886 errx(1, "-s and -d options are mutually exclusive");
4887 if (symref_target && do_list)
4888 errx(1, "-s and -l options are mutually exclusive");
4889 if (do_delete && do_list)
4890 errx(1, "-d and -l options are mutually exclusive");
4896 if (argc != 0 && argc != 1)
4899 refname = strdup(argv[0]);
4900 if (refname == NULL) {
4901 error = got_error_from_errno("strdup");
4908 refname = strdup(argv[0]);
4909 if (refname == NULL) {
4910 error = got_error_from_errno("strdup");
4916 got_path_strip_trailing_slashes(refname);
4920 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4924 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4925 "sendfd unveil", NULL) == -1)
4929 cwd = getcwd(NULL, 0);
4931 error = got_error_from_errno("getcwd");
4935 if (repo_path == NULL) {
4936 error = got_worktree_open(&worktree, cwd);
4937 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4943 strdup(got_worktree_get_repo_path(worktree));
4944 if (repo_path == NULL)
4945 error = got_error_from_errno("strdup");
4949 repo_path = strdup(cwd);
4950 if (repo_path == NULL) {
4951 error = got_error_from_errno("strdup");
4957 error = got_repo_open(&repo, repo_path, NULL);
4961 error = apply_unveil(got_repo_get_path(repo), do_list,
4962 worktree ? got_worktree_get_root_path(worktree) : NULL);
4967 error = list_refs(repo, refname);
4969 error = delete_ref(repo, refname);
4970 else if (symref_target)
4971 error = add_symref(repo, refname, symref_target);
4973 if (obj_arg == NULL)
4975 error = add_ref(repo, refname, obj_arg);
4980 got_repo_close(repo);
4982 got_worktree_close(worktree);
4992 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4993 "[name]\n", getprogname());
4997 static const struct got_error *
4998 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4999 struct got_reference *ref)
5001 const struct got_error *err = NULL;
5002 const char *refname, *marker = " ";
5005 refname = got_ref_get_name(ref);
5006 if (worktree && strcmp(refname,
5007 got_worktree_get_head_ref_name(worktree)) == 0) {
5008 struct got_object_id *id = NULL;
5010 err = got_ref_resolve(&id, repo, ref);
5013 if (got_object_id_cmp(id,
5014 got_worktree_get_base_commit_id(worktree)) == 0)
5021 if (strncmp(refname, "refs/heads/", 11) == 0)
5023 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5026 refstr = got_ref_to_str(ref);
5028 return got_error_from_errno("got_ref_to_str");
5030 printf("%s%s: %s\n", marker, refname, refstr);
5035 static const struct got_error *
5036 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5038 const char *refname;
5040 if (worktree == NULL)
5041 return got_error(GOT_ERR_NOT_WORKTREE);
5043 refname = got_worktree_get_head_ref_name(worktree);
5045 if (strncmp(refname, "refs/heads/", 11) == 0)
5047 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5050 printf("%s\n", refname);
5055 static const struct got_error *
5056 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5058 static const struct got_error *err = NULL;
5059 struct got_reflist_head refs;
5060 struct got_reflist_entry *re;
5061 struct got_reference *temp_ref = NULL;
5062 int rebase_in_progress, histedit_in_progress;
5064 SIMPLEQ_INIT(&refs);
5067 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5072 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5077 if (rebase_in_progress || histedit_in_progress) {
5078 err = got_ref_open(&temp_ref, repo,
5079 got_worktree_get_head_ref_name(worktree), 0);
5082 list_branch(repo, worktree, temp_ref);
5083 got_ref_close(temp_ref);
5087 err = got_ref_list(&refs, repo, "refs/heads",
5088 got_ref_cmp_by_name, NULL);
5092 SIMPLEQ_FOREACH(re, &refs, entry)
5093 list_branch(repo, worktree, re->ref);
5095 got_ref_list_free(&refs);
5099 static const struct got_error *
5100 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5101 const char *branch_name)
5103 const struct got_error *err = NULL;
5104 struct got_reference *ref = NULL;
5107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5108 return got_error_from_errno("asprintf");
5110 err = got_ref_open(&ref, repo, refname, 0);
5115 strcmp(got_worktree_get_head_ref_name(worktree),
5116 got_ref_get_name(ref)) == 0) {
5117 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5118 "will not delete this work tree's current branch");
5122 err = got_ref_delete(ref, repo);
5130 static const struct got_error *
5131 add_branch(struct got_repository *repo, const char *branch_name,
5132 struct got_object_id *base_commit_id)
5134 const struct got_error *err = NULL;
5135 struct got_reference *ref = NULL;
5136 char *base_refname = NULL, *refname = NULL;
5139 * Don't let the user create a branch name with a leading '-'.
5140 * While technically a valid reference name, this case is usually
5141 * an unintended typo.
5143 if (branch_name[0] == '-')
5144 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5146 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5147 err = got_error_from_errno("asprintf");
5151 err = got_ref_open(&ref, repo, refname, 0);
5153 err = got_error(GOT_ERR_BRANCH_EXISTS);
5155 } else if (err->code != GOT_ERR_NOT_REF)
5158 err = got_ref_alloc(&ref, refname, base_commit_id);
5162 err = got_ref_write(ref, repo);
5171 static const struct got_error *
5172 cmd_branch(int argc, char *argv[])
5174 const struct got_error *error = NULL;
5175 struct got_repository *repo = NULL;
5176 struct got_worktree *worktree = NULL;
5177 char *cwd = NULL, *repo_path = NULL;
5178 int ch, do_list = 0, do_show = 0, do_update = 1;
5179 const char *delref = NULL, *commit_id_arg = NULL;
5180 struct got_reference *ref = NULL;
5181 struct got_pathlist_head paths;
5182 struct got_pathlist_entry *pe;
5183 struct got_object_id *commit_id = NULL;
5184 char *commit_id_str = NULL;
5188 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5191 commit_id_arg = optarg;
5197 repo_path = realpath(optarg, NULL);
5198 if (repo_path == NULL)
5199 return got_error_from_errno2("realpath",
5201 got_path_strip_trailing_slashes(repo_path);
5215 if (do_list && delref)
5216 errx(1, "-l and -d options are mutually exclusive");
5221 if (!do_list && !delref && argc == 0)
5224 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5225 errx(1, "-c option can only be used when creating a branch");
5227 if (do_list || delref) {
5230 } else if (!do_show && argc != 1)
5234 if (do_list || do_show) {
5235 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5239 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5240 "sendfd unveil", NULL) == -1)
5244 cwd = getcwd(NULL, 0);
5246 error = got_error_from_errno("getcwd");
5250 if (repo_path == NULL) {
5251 error = got_worktree_open(&worktree, cwd);
5252 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5258 strdup(got_worktree_get_repo_path(worktree));
5259 if (repo_path == NULL)
5260 error = got_error_from_errno("strdup");
5264 repo_path = strdup(cwd);
5265 if (repo_path == NULL) {
5266 error = got_error_from_errno("strdup");
5272 error = got_repo_open(&repo, repo_path, NULL);
5276 error = apply_unveil(got_repo_get_path(repo), do_list,
5277 worktree ? got_worktree_get_root_path(worktree) : NULL);
5282 error = show_current_branch(repo, worktree);
5284 error = list_branches(repo, worktree);
5286 error = delete_branch(repo, worktree, delref);
5288 if (commit_id_arg == NULL)
5289 commit_id_arg = worktree ?
5290 got_worktree_get_head_ref_name(worktree) :
5292 error = got_repo_match_object_id(&commit_id, NULL,
5293 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5296 error = add_branch(repo, argv[0], commit_id);
5299 if (worktree && do_update) {
5300 struct got_update_progress_arg upa;
5301 char *branch_refname = NULL;
5303 error = got_object_id_str(&commit_id_str, commit_id);
5306 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5310 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5312 error = got_error_from_errno("asprintf");
5315 error = got_ref_open(&ref, repo, branch_refname, 0);
5316 free(branch_refname);
5319 error = switch_head_ref(ref, commit_id, worktree,
5323 error = got_worktree_set_base_commit_id(worktree, repo,
5327 memset(&upa, 0, sizeof(upa));
5328 error = got_worktree_checkout_files(worktree, &paths,
5329 repo, update_progress, &upa, check_cancelled,
5333 if (upa.did_something)
5334 printf("Updated to commit %s\n", commit_id_str);
5335 print_update_progress_stats(&upa);
5342 got_repo_close(repo);
5344 got_worktree_close(worktree);
5348 free(commit_id_str);
5349 TAILQ_FOREACH(pe, &paths, entry)
5350 free((char *)pe->path);
5351 got_pathlist_free(&paths);
5360 "usage: %s tag [-c commit] [-r repository] [-l] "
5361 "[-m message] name\n", getprogname());
5366 static const struct got_error *
5367 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5369 const struct got_error *err = NULL;
5370 struct got_reflist_entry *re, *se, *new;
5371 struct got_object_id *re_id, *se_id;
5372 struct got_tag_object *re_tag, *se_tag;
5373 time_t re_time, se_time;
5375 SIMPLEQ_FOREACH(re, tags, entry) {
5376 se = SIMPLEQ_FIRST(sorted);
5378 err = got_reflist_entry_dup(&new, re);
5381 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5384 err = got_ref_resolve(&re_id, repo, re->ref);
5387 err = got_object_open_as_tag(&re_tag, repo, re_id);
5391 re_time = got_object_tag_get_tagger_time(re_tag);
5392 got_object_tag_close(re_tag);
5396 err = got_ref_resolve(&se_id, repo, re->ref);
5399 err = got_object_open_as_tag(&se_tag, repo, se_id);
5403 se_time = got_object_tag_get_tagger_time(se_tag);
5404 got_object_tag_close(se_tag);
5406 if (se_time > re_time) {
5407 err = got_reflist_entry_dup(&new, re);
5410 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5413 se = SIMPLEQ_NEXT(se, entry);
5422 static const struct got_error *
5423 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5425 static const struct got_error *err = NULL;
5426 struct got_reflist_head refs;
5427 struct got_reflist_entry *re;
5429 SIMPLEQ_INIT(&refs);
5431 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5435 SIMPLEQ_FOREACH(re, &refs, entry) {
5436 const char *refname;
5437 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5441 struct got_object_id *id;
5442 struct got_tag_object *tag;
5443 struct got_commit_object *commit = NULL;
5445 refname = got_ref_get_name(re->ref);
5446 if (strncmp(refname, "refs/tags/", 10) != 0)
5449 refstr = got_ref_to_str(re->ref);
5450 if (refstr == NULL) {
5451 err = got_error_from_errno("got_ref_to_str");
5454 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5457 err = got_ref_resolve(&id, repo, re->ref);
5460 err = got_object_open_as_tag(&tag, repo, id);
5462 if (err->code != GOT_ERR_OBJ_TYPE) {
5466 /* "lightweight" tag */
5467 err = got_object_open_as_commit(&commit, repo, id);
5472 tagger = got_object_commit_get_committer(commit);
5474 got_object_commit_get_committer_time(commit);
5475 err = got_object_id_str(&id_str, id);
5481 tagger = got_object_tag_get_tagger(tag);
5482 tagger_time = got_object_tag_get_tagger_time(tag);
5483 err = got_object_id_str(&id_str,
5484 got_object_tag_get_object_id(tag));
5488 printf("from: %s\n", tagger);
5489 datestr = get_datestr(&tagger_time, datebuf);
5491 printf("date: %s UTC\n", datestr);
5493 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5495 switch (got_object_tag_get_object_type(tag)) {
5496 case GOT_OBJ_TYPE_BLOB:
5497 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5500 case GOT_OBJ_TYPE_TREE:
5501 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5504 case GOT_OBJ_TYPE_COMMIT:
5505 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5508 case GOT_OBJ_TYPE_TAG:
5509 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5518 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5521 got_object_commit_close(commit);
5523 tagmsg0 = strdup(got_object_tag_get_message(tag));
5524 got_object_tag_close(tag);
5525 if (tagmsg0 == NULL) {
5526 err = got_error_from_errno("strdup");
5533 line = strsep(&tagmsg, "\n");
5535 printf(" %s\n", line);
5540 got_ref_list_free(&refs);
5544 static const struct got_error *
5545 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5546 const char *tag_name, const char *repo_path)
5548 const struct got_error *err = NULL;
5549 char *template = NULL, *initial_content = NULL;
5550 char *editor = NULL;
5553 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5554 err = got_error_from_errno("asprintf");
5558 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5559 commit_id_str, tag_name) == -1) {
5560 err = got_error_from_errno("asprintf");
5564 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5568 dprintf(fd, initial_content);
5571 err = get_editor(&editor);
5574 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5576 free(initial_content);
5580 /* Editor is done; we can now apply unveil(2) */
5582 err = apply_unveil(repo_path, 0, NULL);
5591 static const struct got_error *
5592 add_tag(struct got_repository *repo, const char *tag_name,
5593 const char *commit_arg, const char *tagmsg_arg)
5595 const struct got_error *err = NULL;
5596 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5597 char *label = NULL, *commit_id_str = NULL;
5598 struct got_reference *ref = NULL;
5599 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5600 char *tagmsg_path = NULL, *tag_id_str = NULL;
5601 int preserve_tagmsg = 0;
5604 * Don't let the user create a tag name with a leading '-'.
5605 * While technically a valid reference name, this case is usually
5606 * an unintended typo.
5608 if (tag_name[0] == '-')
5609 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5611 err = get_author(&tagger, repo);
5615 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5616 GOT_OBJ_TYPE_COMMIT, 1, repo);
5620 err = got_object_id_str(&commit_id_str, commit_id);
5624 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5625 refname = strdup(tag_name);
5626 if (refname == NULL) {
5627 err = got_error_from_errno("strdup");
5631 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5632 err = got_error_from_errno("asprintf");
5636 err = got_ref_open(&ref, repo, refname, 0);
5638 err = got_error(GOT_ERR_TAG_EXISTS);
5640 } else if (err->code != GOT_ERR_NOT_REF)
5643 if (tagmsg_arg == NULL) {
5644 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5645 tag_name, got_repo_get_path(repo));
5647 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5648 tagmsg_path != NULL)
5649 preserve_tagmsg = 1;
5654 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5655 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5658 preserve_tagmsg = 1;
5662 err = got_ref_alloc(&ref, refname, tag_id);
5665 preserve_tagmsg = 1;
5669 err = got_ref_write(ref, repo);
5672 preserve_tagmsg = 1;
5676 err = got_object_id_str(&tag_id_str, tag_id);
5679 preserve_tagmsg = 1;
5682 printf("Created tag %s\n", tag_id_str);
5684 if (preserve_tagmsg) {
5685 fprintf(stderr, "%s: tag message preserved in %s\n",
5686 getprogname(), tagmsg_path);
5687 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5688 err = got_error_from_errno2("unlink", tagmsg_path);
5693 free(commit_id_str);
5701 static const struct got_error *
5702 cmd_tag(int argc, char *argv[])
5704 const struct got_error *error = NULL;
5705 struct got_repository *repo = NULL;
5706 struct got_worktree *worktree = NULL;
5707 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5708 char *gitconfig_path = NULL;
5709 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5710 int ch, do_list = 0;
5712 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5715 commit_id_arg = optarg;
5721 repo_path = realpath(optarg, NULL);
5722 if (repo_path == NULL)
5723 return got_error_from_errno2("realpath",
5725 got_path_strip_trailing_slashes(repo_path);
5740 if (commit_id_arg != NULL)
5742 "-c option can only be used when creating a tag");
5744 errx(1, "-l and -m options are mutually exclusive");
5747 } else if (argc != 1)
5754 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5758 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5759 "sendfd unveil", NULL) == -1)
5763 cwd = getcwd(NULL, 0);
5765 error = got_error_from_errno("getcwd");
5769 if (repo_path == NULL) {
5770 error = got_worktree_open(&worktree, cwd);
5771 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5777 strdup(got_worktree_get_repo_path(worktree));
5778 if (repo_path == NULL)
5779 error = got_error_from_errno("strdup");
5783 repo_path = strdup(cwd);
5784 if (repo_path == NULL) {
5785 error = got_error_from_errno("strdup");
5792 error = got_repo_open(&repo, repo_path, NULL);
5795 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5798 error = list_tags(repo, worktree);
5800 error = get_gitconfig_path(&gitconfig_path);
5803 error = got_repo_open(&repo, repo_path, gitconfig_path);
5808 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5813 if (commit_id_arg == NULL) {
5814 struct got_reference *head_ref;
5815 struct got_object_id *commit_id;
5816 error = got_ref_open(&head_ref, repo,
5817 worktree ? got_worktree_get_head_ref_name(worktree)
5821 error = got_ref_resolve(&commit_id, repo, head_ref);
5822 got_ref_close(head_ref);
5825 error = got_object_id_str(&commit_id_str, commit_id);
5831 error = add_tag(repo, tag_name,
5832 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5836 got_repo_close(repo);
5838 got_worktree_close(worktree);
5841 free(gitconfig_path);
5842 free(commit_id_str);
5849 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5854 static const struct got_error *
5855 add_progress(void *arg, unsigned char status, const char *path)
5857 while (path[0] == '/')
5859 printf("%c %s\n", status, path);
5863 static const struct got_error *
5864 cmd_add(int argc, char *argv[])
5866 const struct got_error *error = NULL;
5867 struct got_repository *repo = NULL;
5868 struct got_worktree *worktree = NULL;
5870 struct got_pathlist_head paths;
5871 struct got_pathlist_entry *pe;
5872 int ch, can_recurse = 0, no_ignores = 0;
5876 while ((ch = getopt(argc, argv, "IR")) != -1) {
5894 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5901 cwd = getcwd(NULL, 0);
5903 error = got_error_from_errno("getcwd");
5907 error = got_worktree_open(&worktree, cwd);
5909 if (error->code == GOT_ERR_NOT_WORKTREE)
5910 error = wrap_not_worktree_error(error, "add", cwd);
5914 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5919 error = apply_unveil(got_repo_get_path(repo), 1,
5920 got_worktree_get_root_path(worktree));
5924 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5928 if (!can_recurse && no_ignores) {
5929 error = got_error_msg(GOT_ERR_BAD_PATH,
5930 "disregarding ignores requires -R option");
5938 TAILQ_FOREACH(pe, &paths, entry) {
5939 if (asprintf(&ondisk_path, "%s/%s",
5940 got_worktree_get_root_path(worktree),
5942 error = got_error_from_errno("asprintf");
5945 if (lstat(ondisk_path, &sb) == -1) {
5946 if (errno == ENOENT) {
5950 error = got_error_from_errno2("lstat",
5956 if (S_ISDIR(sb.st_mode)) {
5957 error = got_error_msg(GOT_ERR_BAD_PATH,
5958 "adding directories requires -R option");
5964 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5965 NULL, repo, no_ignores);
5968 got_repo_close(repo);
5970 got_worktree_close(worktree);
5971 TAILQ_FOREACH(pe, &paths, entry)
5972 free((char *)pe->path);
5973 got_pathlist_free(&paths);
5981 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5986 static const struct got_error *
5987 print_remove_status(void *arg, unsigned char status,
5988 unsigned char staged_status, const char *path)
5990 while (path[0] == '/')
5992 if (status == GOT_STATUS_NONEXISTENT)
5994 if (status == staged_status && (status == GOT_STATUS_DELETE))
5995 status = GOT_STATUS_NO_CHANGE;
5996 printf("%c%c %s\n", status, staged_status, path);
6000 static const struct got_error *
6001 cmd_remove(int argc, char *argv[])
6003 const struct got_error *error = NULL;
6004 struct got_worktree *worktree = NULL;
6005 struct got_repository *repo = NULL;
6007 struct got_pathlist_head paths;
6008 struct got_pathlist_entry *pe;
6009 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
6013 while ((ch = getopt(argc, argv, "fkR")) != -1) {
6016 delete_local_mods = 1;
6034 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6041 cwd = getcwd(NULL, 0);
6043 error = got_error_from_errno("getcwd");
6046 error = got_worktree_open(&worktree, cwd);
6048 if (error->code == GOT_ERR_NOT_WORKTREE)
6049 error = wrap_not_worktree_error(error, "remove", cwd);
6053 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6058 error = apply_unveil(got_repo_get_path(repo), 1,
6059 got_worktree_get_root_path(worktree));
6063 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6070 TAILQ_FOREACH(pe, &paths, entry) {
6071 if (asprintf(&ondisk_path, "%s/%s",
6072 got_worktree_get_root_path(worktree),
6074 error = got_error_from_errno("asprintf");
6077 if (lstat(ondisk_path, &sb) == -1) {
6078 if (errno == ENOENT) {
6082 error = got_error_from_errno2("lstat",
6088 if (S_ISDIR(sb.st_mode)) {
6089 error = got_error_msg(GOT_ERR_BAD_PATH,
6090 "removing directories requires -R option");
6096 error = got_worktree_schedule_delete(worktree, &paths,
6097 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6100 got_repo_close(repo);
6102 got_worktree_close(worktree);
6103 TAILQ_FOREACH(pe, &paths, entry)
6104 free((char *)pe->path);
6105 got_pathlist_free(&paths);
6113 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6114 "path ...\n", getprogname());
6118 static const struct got_error *
6119 revert_progress(void *arg, unsigned char status, const char *path)
6121 if (status == GOT_STATUS_UNVERSIONED)
6124 while (path[0] == '/')
6126 printf("%c %s\n", status, path);
6130 struct choose_patch_arg {
6131 FILE *patch_script_file;
6135 static const struct got_error *
6136 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6137 int nchanges, const char *action)
6140 size_t linesize = 0;
6144 case GOT_STATUS_ADD:
6145 printf("A %s\n%s this addition? [y/n] ", path, action);
6147 case GOT_STATUS_DELETE:
6148 printf("D %s\n%s this deletion? [y/n] ", path, action);
6150 case GOT_STATUS_MODIFY:
6151 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6152 return got_error_from_errno("fseek");
6153 printf(GOT_COMMIT_SEP_STR);
6154 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6156 if (ferror(patch_file))
6157 return got_error_from_errno("getline");
6158 printf(GOT_COMMIT_SEP_STR);
6159 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6160 path, n, nchanges, action);
6163 return got_error_path(path, GOT_ERR_FILE_STATUS);
6169 static const struct got_error *
6170 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6171 FILE *patch_file, int n, int nchanges)
6173 const struct got_error *err = NULL;
6175 size_t linesize = 0;
6178 struct choose_patch_arg *a = arg;
6180 *choice = GOT_PATCH_CHOICE_NONE;
6182 if (a->patch_script_file) {
6184 err = show_change(status, path, patch_file, n, nchanges,
6188 linelen = getline(&line, &linesize, a->patch_script_file);
6189 if (linelen == -1) {
6190 if (ferror(a->patch_script_file))
6191 return got_error_from_errno("getline");
6194 nl = strchr(line, '\n');
6197 if (strcmp(line, "y") == 0) {
6198 *choice = GOT_PATCH_CHOICE_YES;
6200 } else if (strcmp(line, "n") == 0) {
6201 *choice = GOT_PATCH_CHOICE_NO;
6203 } else if (strcmp(line, "q") == 0 &&
6204 status == GOT_STATUS_MODIFY) {
6205 *choice = GOT_PATCH_CHOICE_QUIT;
6208 printf("invalid response '%s'\n", line);
6213 while (resp != 'y' && resp != 'n' && resp != 'q') {
6214 err = show_change(status, path, patch_file, n, nchanges,
6221 if (status == GOT_STATUS_MODIFY) {
6222 if (resp != 'y' && resp != 'n' && resp != 'q') {
6223 printf("invalid response '%c'\n", resp);
6226 } else if (resp != 'y' && resp != 'n') {
6227 printf("invalid response '%c'\n", resp);
6233 *choice = GOT_PATCH_CHOICE_YES;
6234 else if (resp == 'n')
6235 *choice = GOT_PATCH_CHOICE_NO;
6236 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6237 *choice = GOT_PATCH_CHOICE_QUIT;
6243 static const struct got_error *
6244 cmd_revert(int argc, char *argv[])
6246 const struct got_error *error = NULL;
6247 struct got_worktree *worktree = NULL;
6248 struct got_repository *repo = NULL;
6249 char *cwd = NULL, *path = NULL;
6250 struct got_pathlist_head paths;
6251 struct got_pathlist_entry *pe;
6252 int ch, can_recurse = 0, pflag = 0;
6253 FILE *patch_script_file = NULL;
6254 const char *patch_script_path = NULL;
6255 struct choose_patch_arg cpa;
6259 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6265 patch_script_path = optarg;
6280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6281 "unveil", NULL) == -1)
6286 if (patch_script_path && !pflag)
6287 errx(1, "-F option can only be used together with -p option");
6289 cwd = getcwd(NULL, 0);
6291 error = got_error_from_errno("getcwd");
6294 error = got_worktree_open(&worktree, cwd);
6296 if (error->code == GOT_ERR_NOT_WORKTREE)
6297 error = wrap_not_worktree_error(error, "revert", cwd);
6301 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6306 if (patch_script_path) {
6307 patch_script_file = fopen(patch_script_path, "r");
6308 if (patch_script_file == NULL) {
6309 error = got_error_from_errno2("fopen",
6314 error = apply_unveil(got_repo_get_path(repo), 1,
6315 got_worktree_get_root_path(worktree));
6319 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6326 TAILQ_FOREACH(pe, &paths, entry) {
6327 if (asprintf(&ondisk_path, "%s/%s",
6328 got_worktree_get_root_path(worktree),
6330 error = got_error_from_errno("asprintf");
6333 if (lstat(ondisk_path, &sb) == -1) {
6334 if (errno == ENOENT) {
6338 error = got_error_from_errno2("lstat",
6344 if (S_ISDIR(sb.st_mode)) {
6345 error = got_error_msg(GOT_ERR_BAD_PATH,
6346 "reverting directories requires -R option");
6352 cpa.patch_script_file = patch_script_file;
6353 cpa.action = "revert";
6354 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6355 pflag ? choose_patch : NULL, &cpa, repo);
6357 if (patch_script_file && fclose(patch_script_file) == EOF &&
6359 error = got_error_from_errno2("fclose", patch_script_path);
6361 got_repo_close(repo);
6363 got_worktree_close(worktree);
6372 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6377 struct collect_commit_logmsg_arg {
6378 const char *cmdline_log;
6380 const char *worktree_path;
6381 const char *branch_name;
6382 const char *repo_path;
6387 static const struct got_error *
6388 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6391 char *initial_content = NULL;
6392 struct got_pathlist_entry *pe;
6393 const struct got_error *err = NULL;
6394 char *template = NULL;
6395 struct collect_commit_logmsg_arg *a = arg;
6399 /* if a message was specified on the command line, just use it */
6400 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6401 len = strlen(a->cmdline_log) + 1;
6402 *logmsg = malloc(len + 1);
6403 if (*logmsg == NULL)
6404 return got_error_from_errno("malloc");
6405 strlcpy(*logmsg, a->cmdline_log, len);
6409 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6410 return got_error_from_errno("asprintf");
6412 if (asprintf(&initial_content,
6413 "\n# changes to be committed on branch %s:\n",
6414 a->branch_name) == -1)
6415 return got_error_from_errno("asprintf");
6417 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6421 dprintf(fd, initial_content);
6423 TAILQ_FOREACH(pe, commitable_paths, entry) {
6424 struct got_commitable *ct = pe->data;
6425 dprintf(fd, "# %c %s\n",
6426 got_commitable_get_status(ct),
6427 got_commitable_get_path(ct));
6431 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6433 free(initial_content);
6436 /* Editor is done; we can now apply unveil(2) */
6438 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6447 static const struct got_error *
6448 cmd_commit(int argc, char *argv[])
6450 const struct got_error *error = NULL;
6451 struct got_worktree *worktree = NULL;
6452 struct got_repository *repo = NULL;
6453 char *cwd = NULL, *id_str = NULL;
6454 struct got_object_id *id = NULL;
6455 const char *logmsg = NULL;
6456 struct collect_commit_logmsg_arg cl_arg;
6457 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6458 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6459 int allow_bad_symlinks = 0;
6460 struct got_pathlist_head paths;
6463 cl_arg.logmsg_path = NULL;
6465 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6471 allow_bad_symlinks = 1;
6483 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6484 "unveil", NULL) == -1)
6487 cwd = getcwd(NULL, 0);
6489 error = got_error_from_errno("getcwd");
6492 error = got_worktree_open(&worktree, cwd);
6494 if (error->code == GOT_ERR_NOT_WORKTREE)
6495 error = wrap_not_worktree_error(error, "commit", cwd);
6499 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6502 if (rebase_in_progress) {
6503 error = got_error(GOT_ERR_REBASING);
6507 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6512 error = get_gitconfig_path(&gitconfig_path);
6515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6520 error = get_author(&author, repo);
6525 * unveil(2) traverses exec(2); if an editor is used we have
6526 * to apply unveil after the log message has been written.
6528 if (logmsg == NULL || strlen(logmsg) == 0)
6529 error = get_editor(&editor);
6531 error = apply_unveil(got_repo_get_path(repo), 0,
6532 got_worktree_get_root_path(worktree));
6536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6540 cl_arg.editor = editor;
6541 cl_arg.cmdline_log = logmsg;
6542 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6543 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6544 if (!histedit_in_progress) {
6545 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6546 error = got_error(GOT_ERR_COMMIT_BRANCH);
6549 cl_arg.branch_name += 11;
6551 cl_arg.repo_path = got_repo_get_path(repo);
6552 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6553 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6554 print_status, NULL, repo);
6556 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6557 cl_arg.logmsg_path != NULL)
6558 preserve_logmsg = 1;
6562 error = got_object_id_str(&id_str, id);
6565 printf("Created commit %s\n", id_str);
6567 if (preserve_logmsg) {
6568 fprintf(stderr, "%s: log message preserved in %s\n",
6569 getprogname(), cl_arg.logmsg_path);
6570 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6572 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6573 free(cl_arg.logmsg_path);
6575 got_repo_close(repo);
6577 got_worktree_close(worktree);
6580 free(gitconfig_path);
6587 usage_cherrypick(void)
6589 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6593 static const struct got_error *
6594 cmd_cherrypick(int argc, char *argv[])
6596 const struct got_error *error = NULL;
6597 struct got_worktree *worktree = NULL;
6598 struct got_repository *repo = NULL;
6599 char *cwd = NULL, *commit_id_str = NULL;
6600 struct got_object_id *commit_id = NULL;
6601 struct got_commit_object *commit = NULL;
6602 struct got_object_qid *pid;
6603 struct got_reference *head_ref = NULL;
6605 struct got_update_progress_arg upa;
6607 while ((ch = getopt(argc, argv, "")) != -1) {
6619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6620 "unveil", NULL) == -1)
6626 cwd = getcwd(NULL, 0);
6628 error = got_error_from_errno("getcwd");
6631 error = got_worktree_open(&worktree, cwd);
6633 if (error->code == GOT_ERR_NOT_WORKTREE)
6634 error = wrap_not_worktree_error(error, "cherrypick",
6639 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6644 error = apply_unveil(got_repo_get_path(repo), 0,
6645 got_worktree_get_root_path(worktree));
6649 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6650 GOT_OBJ_TYPE_COMMIT, repo);
6651 if (error != NULL) {
6652 struct got_reference *ref;
6653 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6655 error = got_ref_open(&ref, repo, argv[0], 0);
6658 error = got_ref_resolve(&commit_id, repo, ref);
6663 error = got_object_id_str(&commit_id_str, commit_id);
6667 error = got_ref_open(&head_ref, repo,
6668 got_worktree_get_head_ref_name(worktree), 0);
6672 error = check_same_branch(commit_id, head_ref, NULL, repo);
6674 if (error->code != GOT_ERR_ANCESTRY)
6678 error = got_error(GOT_ERR_SAME_BRANCH);
6682 error = got_object_open_as_commit(&commit, repo, commit_id);
6685 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6686 memset(&upa, 0, sizeof(upa));
6687 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6688 commit_id, repo, update_progress, &upa, check_cancelled,
6693 if (upa.did_something)
6694 printf("Merged commit %s\n", commit_id_str);
6695 print_update_progress_stats(&upa);
6698 got_object_commit_close(commit);
6699 free(commit_id_str);
6701 got_ref_close(head_ref);
6703 got_worktree_close(worktree);
6705 got_repo_close(repo);
6712 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6716 static const struct got_error *
6717 cmd_backout(int argc, char *argv[])
6719 const struct got_error *error = NULL;
6720 struct got_worktree *worktree = NULL;
6721 struct got_repository *repo = NULL;
6722 char *cwd = NULL, *commit_id_str = NULL;
6723 struct got_object_id *commit_id = NULL;
6724 struct got_commit_object *commit = NULL;
6725 struct got_object_qid *pid;
6726 struct got_reference *head_ref = NULL;
6728 struct got_update_progress_arg upa;
6730 while ((ch = getopt(argc, argv, "")) != -1) {
6742 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6743 "unveil", NULL) == -1)
6749 cwd = getcwd(NULL, 0);
6751 error = got_error_from_errno("getcwd");
6754 error = got_worktree_open(&worktree, cwd);
6756 if (error->code == GOT_ERR_NOT_WORKTREE)
6757 error = wrap_not_worktree_error(error, "backout", cwd);
6761 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6766 error = apply_unveil(got_repo_get_path(repo), 0,
6767 got_worktree_get_root_path(worktree));
6771 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6772 GOT_OBJ_TYPE_COMMIT, repo);
6773 if (error != NULL) {
6774 struct got_reference *ref;
6775 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6777 error = got_ref_open(&ref, repo, argv[0], 0);
6780 error = got_ref_resolve(&commit_id, repo, ref);
6785 error = got_object_id_str(&commit_id_str, commit_id);
6789 error = got_ref_open(&head_ref, repo,
6790 got_worktree_get_head_ref_name(worktree), 0);
6794 error = check_same_branch(commit_id, head_ref, NULL, repo);
6798 error = got_object_open_as_commit(&commit, repo, commit_id);
6801 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6803 error = got_error(GOT_ERR_ROOT_COMMIT);
6807 memset(&upa, 0, sizeof(upa));
6808 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6809 update_progress, &upa, check_cancelled, NULL);
6813 if (upa.did_something)
6814 printf("Backed out commit %s\n", commit_id_str);
6815 print_update_progress_stats(&upa);
6818 got_object_commit_close(commit);
6819 free(commit_id_str);
6821 got_ref_close(head_ref);
6823 got_worktree_close(worktree);
6825 got_repo_close(repo);
6832 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6838 trim_logmsg(char *logmsg, int limit)
6843 len = strlen(logmsg);
6847 nl = strchr(logmsg, '\n');
6852 static const struct got_error *
6853 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6855 const struct got_error *err;
6856 char *logmsg0 = NULL;
6859 err = got_object_commit_get_logmsg(&logmsg0, commit);
6864 while (isspace((unsigned char)s[0]))
6867 *logmsg = strdup(s);
6868 if (*logmsg == NULL) {
6869 err = got_error_from_errno("strdup");
6873 trim_logmsg(*logmsg, limit);
6879 static const struct got_error *
6880 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6882 const struct got_error *err;
6883 struct got_commit_object *commit = NULL;
6884 char *id_str = NULL, *logmsg = NULL;
6886 err = got_object_open_as_commit(&commit, repo, id);
6890 err = got_object_id_str(&id_str, id);
6896 err = get_short_logmsg(&logmsg, 42, commit);
6900 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6903 got_object_commit_close(commit);
6908 static const struct got_error *
6909 show_rebase_progress(struct got_commit_object *commit,
6910 struct got_object_id *old_id, struct got_object_id *new_id)
6912 const struct got_error *err;
6913 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6915 err = got_object_id_str(&old_id_str, old_id);
6920 err = got_object_id_str(&new_id_str, new_id);
6925 old_id_str[12] = '\0';
6927 new_id_str[12] = '\0';
6929 err = get_short_logmsg(&logmsg, 42, commit);
6933 printf("%s -> %s: %s\n", old_id_str,
6934 new_id_str ? new_id_str : "no-op change", logmsg);
6942 static const struct got_error *
6943 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6944 struct got_reference *branch, struct got_reference *new_base_branch,
6945 struct got_reference *tmp_branch, struct got_repository *repo)
6947 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6948 return got_worktree_rebase_complete(worktree, fileindex,
6949 new_base_branch, tmp_branch, branch, repo);
6952 static const struct got_error *
6953 rebase_commit(struct got_pathlist_head *merged_paths,
6954 struct got_worktree *worktree, struct got_fileindex *fileindex,
6955 struct got_reference *tmp_branch,
6956 struct got_object_id *commit_id, struct got_repository *repo)
6958 const struct got_error *error;
6959 struct got_commit_object *commit;
6960 struct got_object_id *new_commit_id;
6962 error = got_object_open_as_commit(&commit, repo, commit_id);
6966 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6967 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6969 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6971 error = show_rebase_progress(commit, commit_id, NULL);
6973 error = show_rebase_progress(commit, commit_id, new_commit_id);
6974 free(new_commit_id);
6977 got_object_commit_close(commit);
6981 struct check_path_prefix_arg {
6982 const char *path_prefix;
6987 static const struct got_error *
6988 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6989 struct got_blob_object *blob2, struct got_object_id *id1,
6990 struct got_object_id *id2, const char *path1, const char *path2,
6991 mode_t mode1, mode_t mode2, struct got_repository *repo)
6993 struct check_path_prefix_arg *a = arg;
6995 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6996 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6997 return got_error(a->errcode);
7002 static const struct got_error *
7003 check_path_prefix(struct got_object_id *parent_id,
7004 struct got_object_id *commit_id, const char *path_prefix,
7005 int errcode, struct got_repository *repo)
7007 const struct got_error *err;
7008 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7009 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7010 struct check_path_prefix_arg cpp_arg;
7012 if (got_path_is_root_dir(path_prefix))
7015 err = got_object_open_as_commit(&commit, repo, commit_id);
7019 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7023 err = got_object_open_as_tree(&tree1, repo,
7024 got_object_commit_get_tree_id(parent_commit));
7028 err = got_object_open_as_tree(&tree2, repo,
7029 got_object_commit_get_tree_id(commit));
7033 cpp_arg.path_prefix = path_prefix;
7034 while (cpp_arg.path_prefix[0] == '/')
7035 cpp_arg.path_prefix++;
7036 cpp_arg.len = strlen(cpp_arg.path_prefix);
7037 cpp_arg.errcode = errcode;
7038 err = got_diff_tree(tree1, tree2, "", "", repo,
7039 check_path_prefix_in_diff, &cpp_arg, 0);
7042 got_object_tree_close(tree1);
7044 got_object_tree_close(tree2);
7046 got_object_commit_close(commit);
7048 got_object_commit_close(parent_commit);
7052 static const struct got_error *
7053 collect_commits(struct got_object_id_queue *commits,
7054 struct got_object_id *initial_commit_id,
7055 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7056 const char *path_prefix, int path_prefix_errcode,
7057 struct got_repository *repo)
7059 const struct got_error *err = NULL;
7060 struct got_commit_graph *graph = NULL;
7061 struct got_object_id *parent_id = NULL;
7062 struct got_object_qid *qid;
7063 struct got_object_id *commit_id = initial_commit_id;
7065 err = got_commit_graph_open(&graph, "/", 1);
7069 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7070 check_cancelled, NULL);
7073 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7074 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7075 check_cancelled, NULL);
7077 if (err->code == GOT_ERR_ITER_COMPLETED) {
7078 err = got_error_msg(GOT_ERR_ANCESTRY,
7079 "ran out of commits to rebase before "
7080 "youngest common ancestor commit has "
7085 err = check_path_prefix(parent_id, commit_id,
7086 path_prefix, path_prefix_errcode, repo);
7090 err = got_object_qid_alloc(&qid, commit_id);
7093 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7094 commit_id = parent_id;
7098 got_commit_graph_close(graph);
7102 static const struct got_error *
7103 cmd_rebase(int argc, char *argv[])
7105 const struct got_error *error = NULL;
7106 struct got_worktree *worktree = NULL;
7107 struct got_repository *repo = NULL;
7108 struct got_fileindex *fileindex = NULL;
7110 struct got_reference *branch = NULL;
7111 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7112 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7113 struct got_object_id *resume_commit_id = NULL;
7114 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7115 struct got_commit_object *commit = NULL;
7116 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7117 int histedit_in_progress = 0;
7118 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7119 struct got_object_id_queue commits;
7120 struct got_pathlist_head merged_paths;
7121 const struct got_object_id_queue *parent_ids;
7122 struct got_object_qid *qid, *pid;
7124 SIMPLEQ_INIT(&commits);
7125 TAILQ_INIT(&merged_paths);
7127 while ((ch = getopt(argc, argv, "ac")) != -1) {
7133 continue_rebase = 1;
7145 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7146 "unveil", NULL) == -1)
7149 if (abort_rebase && continue_rebase)
7151 else if (abort_rebase || continue_rebase) {
7154 } else if (argc != 1)
7157 cwd = getcwd(NULL, 0);
7159 error = got_error_from_errno("getcwd");
7162 error = got_worktree_open(&worktree, cwd);
7164 if (error->code == GOT_ERR_NOT_WORKTREE)
7165 error = wrap_not_worktree_error(error, "rebase", cwd);
7169 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7174 error = apply_unveil(got_repo_get_path(repo), 0,
7175 got_worktree_get_root_path(worktree));
7179 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7183 if (histedit_in_progress) {
7184 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7188 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7193 struct got_update_progress_arg upa;
7194 if (!rebase_in_progress) {
7195 error = got_error(GOT_ERR_NOT_REBASING);
7198 error = got_worktree_rebase_continue(&resume_commit_id,
7199 &new_base_branch, &tmp_branch, &branch, &fileindex,
7203 printf("Switching work tree to %s\n",
7204 got_ref_get_symref_target(new_base_branch));
7205 memset(&upa, 0, sizeof(upa));
7206 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7207 new_base_branch, update_progress, &upa);
7210 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7211 print_update_progress_stats(&upa);
7212 goto done; /* nothing else to do */
7215 if (continue_rebase) {
7216 if (!rebase_in_progress) {
7217 error = got_error(GOT_ERR_NOT_REBASING);
7220 error = got_worktree_rebase_continue(&resume_commit_id,
7221 &new_base_branch, &tmp_branch, &branch, &fileindex,
7226 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7227 resume_commit_id, repo);
7231 yca_id = got_object_id_dup(resume_commit_id);
7232 if (yca_id == NULL) {
7233 error = got_error_from_errno("got_object_id_dup");
7237 error = got_ref_open(&branch, repo, argv[0], 0);
7242 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7246 if (!continue_rebase) {
7247 struct got_object_id *base_commit_id;
7249 base_commit_id = got_worktree_get_base_commit_id(worktree);
7250 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7251 base_commit_id, branch_head_commit_id, repo,
7252 check_cancelled, NULL);
7255 if (yca_id == NULL) {
7256 error = got_error_msg(GOT_ERR_ANCESTRY,
7257 "specified branch shares no common ancestry "
7258 "with work tree's branch");
7262 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7264 if (error->code != GOT_ERR_ANCESTRY)
7268 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7269 "specified branch resolves to a commit which "
7270 "is already contained in work tree's branch");
7273 error = got_worktree_rebase_prepare(&new_base_branch,
7274 &tmp_branch, &fileindex, worktree, branch, repo);
7279 commit_id = branch_head_commit_id;
7280 error = got_object_open_as_commit(&commit, repo, commit_id);
7284 parent_ids = got_object_commit_get_parent_ids(commit);
7285 pid = SIMPLEQ_FIRST(parent_ids);
7287 if (!continue_rebase) {
7288 struct got_update_progress_arg upa;
7289 memset(&upa, 0, sizeof(upa));
7290 error = got_worktree_rebase_abort(worktree, fileindex,
7291 repo, new_base_branch, update_progress, &upa);
7294 printf("Rebase of %s aborted\n",
7295 got_ref_get_name(branch));
7296 print_update_progress_stats(&upa);
7299 error = got_error(GOT_ERR_EMPTY_REBASE);
7302 error = collect_commits(&commits, commit_id, pid->id,
7303 yca_id, got_worktree_get_path_prefix(worktree),
7304 GOT_ERR_REBASE_PATH, repo);
7305 got_object_commit_close(commit);
7310 if (SIMPLEQ_EMPTY(&commits)) {
7311 if (continue_rebase) {
7312 error = rebase_complete(worktree, fileindex,
7313 branch, new_base_branch, tmp_branch, repo);
7316 /* Fast-forward the reference of the branch. */
7317 struct got_object_id *new_head_commit_id;
7319 error = got_ref_resolve(&new_head_commit_id, repo,
7323 error = got_object_id_str(&id_str, new_head_commit_id);
7324 printf("Forwarding %s to commit %s\n",
7325 got_ref_get_name(branch), id_str);
7327 error = got_ref_change_ref(branch,
7328 new_head_commit_id);
7335 SIMPLEQ_FOREACH(qid, &commits, entry) {
7336 struct got_update_progress_arg upa;
7338 commit_id = qid->id;
7339 parent_id = pid ? pid->id : yca_id;
7342 memset(&upa, 0, sizeof(upa));
7343 error = got_worktree_rebase_merge_files(&merged_paths,
7344 worktree, fileindex, parent_id, commit_id, repo,
7345 update_progress, &upa, check_cancelled, NULL);
7349 print_update_progress_stats(&upa);
7350 if (upa.conflicts > 0)
7351 rebase_status = GOT_STATUS_CONFLICT;
7353 if (rebase_status == GOT_STATUS_CONFLICT) {
7354 error = show_rebase_merge_conflict(qid->id, repo);
7357 got_worktree_rebase_pathlist_free(&merged_paths);
7361 error = rebase_commit(&merged_paths, worktree, fileindex,
7362 tmp_branch, commit_id, repo);
7363 got_worktree_rebase_pathlist_free(&merged_paths);
7368 if (rebase_status == GOT_STATUS_CONFLICT) {
7369 error = got_worktree_rebase_postpone(worktree, fileindex);
7372 error = got_error_msg(GOT_ERR_CONFLICTS,
7373 "conflicts must be resolved before rebasing can continue");
7375 error = rebase_complete(worktree, fileindex, branch,
7376 new_base_branch, tmp_branch, repo);
7378 got_object_id_queue_free(&commits);
7379 free(branch_head_commit_id);
7380 free(resume_commit_id);
7383 got_object_commit_close(commit);
7385 got_ref_close(branch);
7386 if (new_base_branch)
7387 got_ref_close(new_base_branch);
7389 got_ref_close(tmp_branch);
7391 got_worktree_close(worktree);
7393 got_repo_close(repo);
7398 usage_histedit(void)
7400 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7405 #define GOT_HISTEDIT_PICK 'p'
7406 #define GOT_HISTEDIT_EDIT 'e'
7407 #define GOT_HISTEDIT_FOLD 'f'
7408 #define GOT_HISTEDIT_DROP 'd'
7409 #define GOT_HISTEDIT_MESG 'm'
7411 static struct got_histedit_cmd {
7415 } got_histedit_cmds[] = {
7416 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7417 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7418 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7420 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7421 { GOT_HISTEDIT_MESG, "mesg",
7422 "single-line log message for commit above (open editor if empty)" },
7425 struct got_histedit_list_entry {
7426 TAILQ_ENTRY(got_histedit_list_entry) entry;
7427 struct got_object_id *commit_id;
7428 const struct got_histedit_cmd *cmd;
7431 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7433 static const struct got_error *
7434 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7435 FILE *f, struct got_repository *repo)
7437 const struct got_error *err = NULL;
7438 char *logmsg = NULL, *id_str = NULL;
7439 struct got_commit_object *commit = NULL;
7442 err = got_object_open_as_commit(&commit, repo, commit_id);
7446 err = get_short_logmsg(&logmsg, 34, commit);
7450 err = got_object_id_str(&id_str, commit_id);
7454 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7456 err = got_ferror(f, GOT_ERR_IO);
7459 got_object_commit_close(commit);
7465 static const struct got_error *
7466 histedit_write_commit_list(struct got_object_id_queue *commits,
7467 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7469 const struct got_error *err = NULL;
7470 struct got_object_qid *qid;
7472 if (SIMPLEQ_EMPTY(commits))
7473 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7475 SIMPLEQ_FOREACH(qid, commits, entry) {
7476 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7480 if (edit_logmsg_only) {
7481 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7483 err = got_ferror(f, GOT_ERR_IO);
7492 static const struct got_error *
7493 write_cmd_list(FILE *f, const char *branch_name,
7494 struct got_object_id_queue *commits)
7496 const struct got_error *err = NULL;
7499 struct got_object_qid *qid;
7501 qid = SIMPLEQ_FIRST(commits);
7502 err = got_object_id_str(&id_str, qid->id);
7507 "# Editing the history of branch '%s' starting at\n"
7509 "# Commits will be processed in order from top to "
7510 "bottom of this file.\n", branch_name, id_str);
7512 err = got_ferror(f, GOT_ERR_IO);
7516 n = fprintf(f, "# Available histedit commands:\n");
7518 err = got_ferror(f, GOT_ERR_IO);
7522 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7523 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7524 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7527 err = got_ferror(f, GOT_ERR_IO);
7536 static const struct got_error *
7537 histedit_syntax_error(int lineno)
7539 static char msg[42];
7542 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7544 if (ret == -1 || ret >= sizeof(msg))
7545 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7547 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7550 static const struct got_error *
7551 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7552 char *logmsg, struct got_repository *repo)
7554 const struct got_error *err;
7555 struct got_commit_object *folded_commit = NULL;
7556 char *id_str, *folded_logmsg = NULL;
7558 err = got_object_id_str(&id_str, hle->commit_id);
7562 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7566 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7569 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7570 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7571 folded_logmsg) == -1) {
7572 err = got_error_from_errno("asprintf");
7576 got_object_commit_close(folded_commit);
7578 free(folded_logmsg);
7582 static struct got_histedit_list_entry *
7583 get_folded_commits(struct got_histedit_list_entry *hle)
7585 struct got_histedit_list_entry *prev, *folded = NULL;
7587 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7588 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7589 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7590 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7592 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7598 static const struct got_error *
7599 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7600 struct got_repository *repo)
7602 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7603 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7604 const struct got_error *err = NULL;
7605 struct got_commit_object *commit = NULL;
7607 struct got_histedit_list_entry *folded = NULL;
7609 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7613 folded = get_folded_commits(hle);
7615 while (folded != hle) {
7616 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7617 folded = TAILQ_NEXT(folded, entry);
7620 err = append_folded_commit_msg(&new_msg, folded,
7626 folded = TAILQ_NEXT(folded, entry);
7630 err = got_object_id_str(&id_str, hle->commit_id);
7633 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7636 if (asprintf(&new_msg,
7637 "%s\n# original log message of commit %s: %s",
7638 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7639 err = got_error_from_errno("asprintf");
7645 err = got_object_id_str(&id_str, hle->commit_id);
7649 err = got_opentemp_named_fd(&logmsg_path, &fd,
7650 GOT_TMPDIR_STR "/got-logmsg");
7654 dprintf(fd, logmsg);
7657 err = get_editor(&editor);
7661 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7663 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7665 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7668 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7669 err = got_error_from_errno2("unlink", logmsg_path);
7675 got_object_commit_close(commit);
7679 static const struct got_error *
7680 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7681 FILE *f, struct got_repository *repo)
7683 const struct got_error *err = NULL;
7684 char *line = NULL, *p, *end;
7688 const struct got_histedit_cmd *cmd;
7689 struct got_object_id *commit_id = NULL;
7690 struct got_histedit_list_entry *hle = NULL;
7693 len = getline(&line, &size, f);
7695 const struct got_error *getline_err;
7698 getline_err = got_error_from_errno("getline");
7699 err = got_ferror(f, getline_err->code);
7704 while (isspace((unsigned char)p[0]))
7706 if (p[0] == '#' || p[0] == '\0') {
7712 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7713 cmd = &got_histedit_cmds[i];
7714 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7715 isspace((unsigned char)p[strlen(cmd->name)])) {
7716 p += strlen(cmd->name);
7719 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7724 if (i == nitems(got_histedit_cmds)) {
7725 err = histedit_syntax_error(lineno);
7728 while (isspace((unsigned char)p[0]))
7730 if (cmd->code == GOT_HISTEDIT_MESG) {
7731 if (hle == NULL || hle->logmsg != NULL) {
7732 err = got_error(GOT_ERR_HISTEDIT_CMD);
7736 err = histedit_edit_logmsg(hle, repo);
7740 hle->logmsg = strdup(p);
7741 if (hle->logmsg == NULL) {
7742 err = got_error_from_errno("strdup");
7751 while (end[0] && !isspace((unsigned char)end[0]))
7755 err = got_object_resolve_id_str(&commit_id, repo, p);
7757 /* override error code */
7758 err = histedit_syntax_error(lineno);
7762 hle = malloc(sizeof(*hle));
7764 err = got_error_from_errno("malloc");
7768 hle->commit_id = commit_id;
7773 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7781 static const struct got_error *
7782 histedit_check_script(struct got_histedit_list *histedit_cmds,
7783 struct got_object_id_queue *commits, struct got_repository *repo)
7785 const struct got_error *err = NULL;
7786 struct got_object_qid *qid;
7787 struct got_histedit_list_entry *hle;
7788 static char msg[92];
7791 if (TAILQ_EMPTY(histedit_cmds))
7792 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7793 "histedit script contains no commands");
7794 if (SIMPLEQ_EMPTY(commits))
7795 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7797 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7798 struct got_histedit_list_entry *hle2;
7799 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7802 if (got_object_id_cmp(hle->commit_id,
7803 hle2->commit_id) != 0)
7805 err = got_object_id_str(&id_str, hle->commit_id);
7808 snprintf(msg, sizeof(msg), "commit %s is listed "
7809 "more than once in histedit script", id_str);
7811 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7815 SIMPLEQ_FOREACH(qid, commits, entry) {
7816 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7817 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7821 err = got_object_id_str(&id_str, qid->id);
7824 snprintf(msg, sizeof(msg),
7825 "commit %s missing from histedit script", id_str);
7827 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7831 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7832 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7833 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7834 "last commit in histedit script cannot be folded");
7839 static const struct got_error *
7840 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7841 const char *path, struct got_object_id_queue *commits,
7842 struct got_repository *repo)
7844 const struct got_error *err = NULL;
7848 err = get_editor(&editor);
7852 if (spawn_editor(editor, path) == -1) {
7853 err = got_error_from_errno("failed spawning editor");
7857 f = fopen(path, "r");
7859 err = got_error_from_errno("fopen");
7862 err = histedit_parse_list(histedit_cmds, f, repo);
7866 err = histedit_check_script(histedit_cmds, commits, repo);
7868 if (f && fclose(f) != 0 && err == NULL)
7869 err = got_error_from_errno("fclose");
7874 static const struct got_error *
7875 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7876 struct got_object_id_queue *, const char *, const char *,
7877 struct got_repository *);
7879 static const struct got_error *
7880 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7881 struct got_object_id_queue *commits, const char *branch_name,
7882 int edit_logmsg_only, struct got_repository *repo)
7884 const struct got_error *err;
7888 err = got_opentemp_named(&path, &f, "got-histedit");
7892 err = write_cmd_list(f, branch_name, commits);
7896 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7900 if (edit_logmsg_only) {
7902 err = histedit_parse_list(histedit_cmds, f, repo);
7904 if (fclose(f) != 0) {
7905 err = got_error_from_errno("fclose");
7909 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7911 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7912 err->code != GOT_ERR_HISTEDIT_CMD)
7914 err = histedit_edit_list_retry(histedit_cmds, err,
7915 commits, path, branch_name, repo);
7919 if (f && fclose(f) != 0 && err == NULL)
7920 err = got_error_from_errno("fclose");
7921 if (path && unlink(path) != 0 && err == NULL)
7922 err = got_error_from_errno2("unlink", path);
7927 static const struct got_error *
7928 histedit_save_list(struct got_histedit_list *histedit_cmds,
7929 struct got_worktree *worktree, struct got_repository *repo)
7931 const struct got_error *err = NULL;
7934 struct got_histedit_list_entry *hle;
7935 struct got_commit_object *commit = NULL;
7937 err = got_worktree_get_histedit_script_path(&path, worktree);
7941 f = fopen(path, "w");
7943 err = got_error_from_errno2("fopen", path);
7946 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7947 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7953 int n = fprintf(f, "%c %s\n",
7954 GOT_HISTEDIT_MESG, hle->logmsg);
7956 err = got_ferror(f, GOT_ERR_IO);
7962 if (f && fclose(f) != 0 && err == NULL)
7963 err = got_error_from_errno("fclose");
7966 got_object_commit_close(commit);
7971 histedit_free_list(struct got_histedit_list *histedit_cmds)
7973 struct got_histedit_list_entry *hle;
7975 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7976 TAILQ_REMOVE(histedit_cmds, hle, entry);
7981 static const struct got_error *
7982 histedit_load_list(struct got_histedit_list *histedit_cmds,
7983 const char *path, struct got_repository *repo)
7985 const struct got_error *err = NULL;
7988 f = fopen(path, "r");
7990 err = got_error_from_errno2("fopen", path);
7994 err = histedit_parse_list(histedit_cmds, f, repo);
7996 if (f && fclose(f) != 0 && err == NULL)
7997 err = got_error_from_errno("fclose");
8001 static const struct got_error *
8002 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8003 const struct got_error *edit_err, struct got_object_id_queue *commits,
8004 const char *path, const char *branch_name, struct got_repository *repo)
8006 const struct got_error *err = NULL, *prev_err = edit_err;
8009 while (resp != 'c' && resp != 'r' && resp != 'a') {
8010 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8011 "or (a)bort: ", getprogname(), prev_err->msg);
8016 histedit_free_list(histedit_cmds);
8017 err = histedit_run_editor(histedit_cmds, path, commits,
8020 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8021 err->code != GOT_ERR_HISTEDIT_CMD)
8028 } else if (resp == 'r') {
8029 histedit_free_list(histedit_cmds);
8030 err = histedit_edit_script(histedit_cmds,
8031 commits, branch_name, 0, repo);
8033 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8034 err->code != GOT_ERR_HISTEDIT_CMD)
8041 } else if (resp == 'a') {
8042 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8045 printf("invalid response '%c'\n", resp);
8051 static const struct got_error *
8052 histedit_complete(struct got_worktree *worktree,
8053 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8054 struct got_reference *branch, struct got_repository *repo)
8056 printf("Switching work tree to %s\n",
8057 got_ref_get_symref_target(branch));
8058 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8062 static const struct got_error *
8063 show_histedit_progress(struct got_commit_object *commit,
8064 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8066 const struct got_error *err;
8067 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8069 err = got_object_id_str(&old_id_str, hle->commit_id);
8074 err = got_object_id_str(&new_id_str, new_id);
8079 old_id_str[12] = '\0';
8081 new_id_str[12] = '\0';
8084 logmsg = strdup(hle->logmsg);
8085 if (logmsg == NULL) {
8086 err = got_error_from_errno("strdup");
8089 trim_logmsg(logmsg, 42);
8091 err = get_short_logmsg(&logmsg, 42, commit);
8096 switch (hle->cmd->code) {
8097 case GOT_HISTEDIT_PICK:
8098 case GOT_HISTEDIT_EDIT:
8099 printf("%s -> %s: %s\n", old_id_str,
8100 new_id_str ? new_id_str : "no-op change", logmsg);
8102 case GOT_HISTEDIT_DROP:
8103 case GOT_HISTEDIT_FOLD:
8104 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8116 static const struct got_error *
8117 histedit_commit(struct got_pathlist_head *merged_paths,
8118 struct got_worktree *worktree, struct got_fileindex *fileindex,
8119 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8120 struct got_repository *repo)
8122 const struct got_error *err;
8123 struct got_commit_object *commit;
8124 struct got_object_id *new_commit_id;
8126 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8127 && hle->logmsg == NULL) {
8128 err = histedit_edit_logmsg(hle, repo);
8133 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8137 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8138 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8141 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8143 err = show_histedit_progress(commit, hle, NULL);
8145 err = show_histedit_progress(commit, hle, new_commit_id);
8146 free(new_commit_id);
8149 got_object_commit_close(commit);
8153 static const struct got_error *
8154 histedit_skip_commit(struct got_histedit_list_entry *hle,
8155 struct got_worktree *worktree, struct got_repository *repo)
8157 const struct got_error *error;
8158 struct got_commit_object *commit;
8160 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8165 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8169 error = show_histedit_progress(commit, hle, NULL);
8170 got_object_commit_close(commit);
8174 static const struct got_error *
8175 check_local_changes(void *arg, unsigned char status,
8176 unsigned char staged_status, const char *path,
8177 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8178 struct got_object_id *commit_id, int dirfd, const char *de_name)
8180 int *have_local_changes = arg;
8183 case GOT_STATUS_ADD:
8184 case GOT_STATUS_DELETE:
8185 case GOT_STATUS_MODIFY:
8186 case GOT_STATUS_CONFLICT:
8187 *have_local_changes = 1;
8188 return got_error(GOT_ERR_CANCELLED);
8193 switch (staged_status) {
8194 case GOT_STATUS_ADD:
8195 case GOT_STATUS_DELETE:
8196 case GOT_STATUS_MODIFY:
8197 *have_local_changes = 1;
8198 return got_error(GOT_ERR_CANCELLED);
8206 static const struct got_error *
8207 cmd_histedit(int argc, char *argv[])
8209 const struct got_error *error = NULL;
8210 struct got_worktree *worktree = NULL;
8211 struct got_fileindex *fileindex = NULL;
8212 struct got_repository *repo = NULL;
8214 struct got_reference *branch = NULL;
8215 struct got_reference *tmp_branch = NULL;
8216 struct got_object_id *resume_commit_id = NULL;
8217 struct got_object_id *base_commit_id = NULL;
8218 struct got_object_id *head_commit_id = NULL;
8219 struct got_commit_object *commit = NULL;
8220 int ch, rebase_in_progress = 0;
8221 struct got_update_progress_arg upa;
8222 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8223 int edit_logmsg_only = 0;
8224 const char *edit_script_path = NULL;
8225 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8226 struct got_object_id_queue commits;
8227 struct got_pathlist_head merged_paths;
8228 const struct got_object_id_queue *parent_ids;
8229 struct got_object_qid *pid;
8230 struct got_histedit_list histedit_cmds;
8231 struct got_histedit_list_entry *hle;
8233 SIMPLEQ_INIT(&commits);
8234 TAILQ_INIT(&histedit_cmds);
8235 TAILQ_INIT(&merged_paths);
8236 memset(&upa, 0, sizeof(upa));
8238 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8247 edit_script_path = optarg;
8250 edit_logmsg_only = 1;
8262 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8263 "unveil", NULL) == -1)
8266 if (abort_edit && continue_edit)
8267 errx(1, "histedit's -a and -c options are mutually exclusive");
8268 if (edit_script_path && edit_logmsg_only)
8269 errx(1, "histedit's -F and -m options are mutually exclusive");
8270 if (abort_edit && edit_logmsg_only)
8271 errx(1, "histedit's -a and -m options are mutually exclusive");
8272 if (continue_edit && edit_logmsg_only)
8273 errx(1, "histedit's -c and -m options are mutually exclusive");
8278 * This command cannot apply unveil(2) in all cases because the
8279 * user may choose to run an editor to edit the histedit script
8280 * and to edit individual commit log messages.
8281 * unveil(2) traverses exec(2); if an editor is used we have to
8282 * apply unveil after edit script and log messages have been written.
8283 * XXX TODO: Make use of unveil(2) where possible.
8286 cwd = getcwd(NULL, 0);
8288 error = got_error_from_errno("getcwd");
8291 error = got_worktree_open(&worktree, cwd);
8293 if (error->code == GOT_ERR_NOT_WORKTREE)
8294 error = wrap_not_worktree_error(error, "histedit", cwd);
8298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8303 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8306 if (rebase_in_progress) {
8307 error = got_error(GOT_ERR_REBASING);
8311 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8315 if (edit_in_progress && edit_logmsg_only) {
8316 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8317 "histedit operation is in progress in this "
8318 "work tree and must be continued or aborted "
8319 "before the -m option can be used");
8323 if (edit_in_progress && abort_edit) {
8324 error = got_worktree_histedit_continue(&resume_commit_id,
8325 &tmp_branch, &branch, &base_commit_id, &fileindex,
8329 printf("Switching work tree to %s\n",
8330 got_ref_get_symref_target(branch));
8331 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8332 branch, base_commit_id, update_progress, &upa);
8335 printf("Histedit of %s aborted\n",
8336 got_ref_get_symref_target(branch));
8337 print_update_progress_stats(&upa);
8338 goto done; /* nothing else to do */
8339 } else if (abort_edit) {
8340 error = got_error(GOT_ERR_NOT_HISTEDIT);
8344 if (continue_edit) {
8347 if (!edit_in_progress) {
8348 error = got_error(GOT_ERR_NOT_HISTEDIT);
8352 error = got_worktree_get_histedit_script_path(&path, worktree);
8356 error = histedit_load_list(&histedit_cmds, path, repo);
8361 error = got_worktree_histedit_continue(&resume_commit_id,
8362 &tmp_branch, &branch, &base_commit_id, &fileindex,
8367 error = got_ref_resolve(&head_commit_id, repo, branch);
8371 error = got_object_open_as_commit(&commit, repo,
8375 parent_ids = got_object_commit_get_parent_ids(commit);
8376 pid = SIMPLEQ_FIRST(parent_ids);
8378 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8381 error = collect_commits(&commits, head_commit_id, pid->id,
8382 base_commit_id, got_worktree_get_path_prefix(worktree),
8383 GOT_ERR_HISTEDIT_PATH, repo);
8384 got_object_commit_close(commit);
8389 if (edit_in_progress) {
8390 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8394 error = got_ref_open(&branch, repo,
8395 got_worktree_get_head_ref_name(worktree), 0);
8399 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8400 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8401 "will not edit commit history of a branch outside "
8402 "the \"refs/heads/\" reference namespace");
8406 error = got_ref_resolve(&head_commit_id, repo, branch);
8407 got_ref_close(branch);
8412 error = got_object_open_as_commit(&commit, repo,
8416 parent_ids = got_object_commit_get_parent_ids(commit);
8417 pid = SIMPLEQ_FIRST(parent_ids);
8419 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8422 error = collect_commits(&commits, head_commit_id, pid->id,
8423 got_worktree_get_base_commit_id(worktree),
8424 got_worktree_get_path_prefix(worktree),
8425 GOT_ERR_HISTEDIT_PATH, repo);
8426 got_object_commit_close(commit);
8431 if (SIMPLEQ_EMPTY(&commits)) {
8432 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8436 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8437 &base_commit_id, &fileindex, worktree, repo);
8441 if (edit_script_path) {
8442 error = histedit_load_list(&histedit_cmds,
8443 edit_script_path, repo);
8445 got_worktree_histedit_abort(worktree, fileindex,
8446 repo, branch, base_commit_id,
8447 update_progress, &upa);
8448 print_update_progress_stats(&upa);
8452 const char *branch_name;
8453 branch_name = got_ref_get_symref_target(branch);
8454 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8456 error = histedit_edit_script(&histedit_cmds, &commits,
8457 branch_name, edit_logmsg_only, repo);
8459 got_worktree_histedit_abort(worktree, fileindex,
8460 repo, branch, base_commit_id,
8461 update_progress, &upa);
8462 print_update_progress_stats(&upa);
8468 error = histedit_save_list(&histedit_cmds, worktree,
8471 got_worktree_histedit_abort(worktree, fileindex,
8472 repo, branch, base_commit_id,
8473 update_progress, &upa);
8474 print_update_progress_stats(&upa);
8480 error = histedit_check_script(&histedit_cmds, &commits, repo);
8484 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8485 if (resume_commit_id) {
8486 if (got_object_id_cmp(hle->commit_id,
8487 resume_commit_id) != 0)
8490 resume_commit_id = NULL;
8491 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8492 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8493 error = histedit_skip_commit(hle, worktree,
8498 struct got_pathlist_head paths;
8499 int have_changes = 0;
8502 error = got_pathlist_append(&paths, "", NULL);
8505 error = got_worktree_status(worktree, &paths,
8506 repo, check_local_changes, &have_changes,
8507 check_cancelled, NULL);
8508 got_pathlist_free(&paths);
8510 if (error->code != GOT_ERR_CANCELLED)
8512 if (sigint_received || sigpipe_received)
8516 error = histedit_commit(NULL, worktree,
8517 fileindex, tmp_branch, hle, repo);
8521 error = got_object_open_as_commit(
8522 &commit, repo, hle->commit_id);
8525 error = show_histedit_progress(commit,
8527 got_object_commit_close(commit);
8536 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8537 error = histedit_skip_commit(hle, worktree, repo);
8543 error = got_object_open_as_commit(&commit, repo,
8547 parent_ids = got_object_commit_get_parent_ids(commit);
8548 pid = SIMPLEQ_FIRST(parent_ids);
8550 error = got_worktree_histedit_merge_files(&merged_paths,
8551 worktree, fileindex, pid->id, hle->commit_id, repo,
8552 update_progress, &upa, check_cancelled, NULL);
8555 got_object_commit_close(commit);
8558 print_update_progress_stats(&upa);
8559 if (upa.conflicts > 0)
8560 rebase_status = GOT_STATUS_CONFLICT;
8562 if (rebase_status == GOT_STATUS_CONFLICT) {
8563 error = show_rebase_merge_conflict(hle->commit_id,
8567 got_worktree_rebase_pathlist_free(&merged_paths);
8571 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8573 error = got_object_id_str(&id_str, hle->commit_id);
8576 printf("Stopping histedit for amending commit %s\n",
8579 got_worktree_rebase_pathlist_free(&merged_paths);
8580 error = got_worktree_histedit_postpone(worktree,
8585 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8586 error = histedit_skip_commit(hle, worktree, repo);
8592 error = histedit_commit(&merged_paths, worktree, fileindex,
8593 tmp_branch, hle, repo);
8594 got_worktree_rebase_pathlist_free(&merged_paths);
8599 if (rebase_status == GOT_STATUS_CONFLICT) {
8600 error = got_worktree_histedit_postpone(worktree, fileindex);
8603 error = got_error_msg(GOT_ERR_CONFLICTS,
8604 "conflicts must be resolved before histedit can continue");
8606 error = histedit_complete(worktree, fileindex, tmp_branch,
8609 got_object_id_queue_free(&commits);
8610 histedit_free_list(&histedit_cmds);
8611 free(head_commit_id);
8612 free(base_commit_id);
8613 free(resume_commit_id);
8615 got_object_commit_close(commit);
8617 got_ref_close(branch);
8619 got_ref_close(tmp_branch);
8621 got_worktree_close(worktree);
8623 got_repo_close(repo);
8628 usage_integrate(void)
8630 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8634 static const struct got_error *
8635 cmd_integrate(int argc, char *argv[])
8637 const struct got_error *error = NULL;
8638 struct got_repository *repo = NULL;
8639 struct got_worktree *worktree = NULL;
8640 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8641 const char *branch_arg = NULL;
8642 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8643 struct got_fileindex *fileindex = NULL;
8644 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8646 struct got_update_progress_arg upa;
8648 while ((ch = getopt(argc, argv, "")) != -1) {
8661 branch_arg = argv[0];
8663 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8664 "unveil", NULL) == -1)
8667 cwd = getcwd(NULL, 0);
8669 error = got_error_from_errno("getcwd");
8673 error = got_worktree_open(&worktree, cwd);
8675 if (error->code == GOT_ERR_NOT_WORKTREE)
8676 error = wrap_not_worktree_error(error, "integrate",
8681 error = check_rebase_or_histedit_in_progress(worktree);
8685 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8690 error = apply_unveil(got_repo_get_path(repo), 0,
8691 got_worktree_get_root_path(worktree));
8695 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8696 error = got_error_from_errno("asprintf");
8700 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8701 &base_branch_ref, worktree, refname, repo);
8705 refname = strdup(got_ref_get_name(branch_ref));
8706 if (refname == NULL) {
8707 error = got_error_from_errno("strdup");
8708 got_worktree_integrate_abort(worktree, fileindex, repo,
8709 branch_ref, base_branch_ref);
8712 base_refname = strdup(got_ref_get_name(base_branch_ref));
8713 if (base_refname == NULL) {
8714 error = got_error_from_errno("strdup");
8715 got_worktree_integrate_abort(worktree, fileindex, repo,
8716 branch_ref, base_branch_ref);
8720 error = got_ref_resolve(&commit_id, repo, branch_ref);
8724 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8728 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8729 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8730 "specified branch has already been integrated");
8731 got_worktree_integrate_abort(worktree, fileindex, repo,
8732 branch_ref, base_branch_ref);
8736 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8738 if (error->code == GOT_ERR_ANCESTRY)
8739 error = got_error(GOT_ERR_REBASE_REQUIRED);
8740 got_worktree_integrate_abort(worktree, fileindex, repo,
8741 branch_ref, base_branch_ref);
8745 memset(&upa, 0, sizeof(upa));
8746 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8747 branch_ref, base_branch_ref, update_progress, &upa,
8748 check_cancelled, NULL);
8752 printf("Integrated %s into %s\n", refname, base_refname);
8753 print_update_progress_stats(&upa);
8756 got_repo_close(repo);
8758 got_worktree_close(worktree);
8760 free(base_commit_id);
8770 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8771 "[-S] [file-path ...]\n",
8776 static const struct got_error *
8777 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8778 const char *path, struct got_object_id *blob_id,
8779 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8780 int dirfd, const char *de_name)
8782 const struct got_error *err = NULL;
8783 char *id_str = NULL;
8785 if (staged_status != GOT_STATUS_ADD &&
8786 staged_status != GOT_STATUS_MODIFY &&
8787 staged_status != GOT_STATUS_DELETE)
8790 if (staged_status == GOT_STATUS_ADD ||
8791 staged_status == GOT_STATUS_MODIFY)
8792 err = got_object_id_str(&id_str, staged_blob_id);
8794 err = got_object_id_str(&id_str, blob_id);
8798 printf("%s %c %s\n", id_str, staged_status, path);
8803 static const struct got_error *
8804 cmd_stage(int argc, char *argv[])
8806 const struct got_error *error = NULL;
8807 struct got_repository *repo = NULL;
8808 struct got_worktree *worktree = NULL;
8810 struct got_pathlist_head paths;
8811 struct got_pathlist_entry *pe;
8812 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8813 FILE *patch_script_file = NULL;
8814 const char *patch_script_path = NULL;
8815 struct choose_patch_arg cpa;
8819 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8828 patch_script_path = optarg;
8831 allow_bad_symlinks = 1;
8843 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8844 "unveil", NULL) == -1)
8847 if (list_stage && (pflag || patch_script_path))
8848 errx(1, "-l option cannot be used with other options");
8849 if (patch_script_path && !pflag)
8850 errx(1, "-F option can only be used together with -p option");
8852 cwd = getcwd(NULL, 0);
8854 error = got_error_from_errno("getcwd");
8858 error = got_worktree_open(&worktree, cwd);
8860 if (error->code == GOT_ERR_NOT_WORKTREE)
8861 error = wrap_not_worktree_error(error, "stage", cwd);
8865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8870 if (patch_script_path) {
8871 patch_script_file = fopen(patch_script_path, "r");
8872 if (patch_script_file == NULL) {
8873 error = got_error_from_errno2("fopen",
8878 error = apply_unveil(got_repo_get_path(repo), 0,
8879 got_worktree_get_root_path(worktree));
8883 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8888 error = got_worktree_status(worktree, &paths, repo,
8889 print_stage, NULL, check_cancelled, NULL);
8891 cpa.patch_script_file = patch_script_file;
8892 cpa.action = "stage";
8893 error = got_worktree_stage(worktree, &paths,
8894 pflag ? NULL : print_status, NULL,
8895 pflag ? choose_patch : NULL, &cpa,
8896 allow_bad_symlinks, repo);
8899 if (patch_script_file && fclose(patch_script_file) == EOF &&
8901 error = got_error_from_errno2("fclose", patch_script_path);
8903 got_repo_close(repo);
8905 got_worktree_close(worktree);
8906 TAILQ_FOREACH(pe, &paths, entry)
8907 free((char *)pe->path);
8908 got_pathlist_free(&paths);
8916 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8917 "[file-path ...]\n",
8923 static const struct got_error *
8924 cmd_unstage(int argc, char *argv[])
8926 const struct got_error *error = NULL;
8927 struct got_repository *repo = NULL;
8928 struct got_worktree *worktree = NULL;
8930 struct got_pathlist_head paths;
8931 struct got_pathlist_entry *pe;
8933 struct got_update_progress_arg upa;
8934 FILE *patch_script_file = NULL;
8935 const char *patch_script_path = NULL;
8936 struct choose_patch_arg cpa;
8940 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8946 patch_script_path = optarg;
8958 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8959 "unveil", NULL) == -1)
8962 if (patch_script_path && !pflag)
8963 errx(1, "-F option can only be used together with -p option");
8965 cwd = getcwd(NULL, 0);
8967 error = got_error_from_errno("getcwd");
8971 error = got_worktree_open(&worktree, cwd);
8973 if (error->code == GOT_ERR_NOT_WORKTREE)
8974 error = wrap_not_worktree_error(error, "unstage", cwd);
8978 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8983 if (patch_script_path) {
8984 patch_script_file = fopen(patch_script_path, "r");
8985 if (patch_script_file == NULL) {
8986 error = got_error_from_errno2("fopen",
8992 error = apply_unveil(got_repo_get_path(repo), 0,
8993 got_worktree_get_root_path(worktree));
8997 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9001 cpa.patch_script_file = patch_script_file;
9002 cpa.action = "unstage";
9003 memset(&upa, 0, sizeof(upa));
9004 error = got_worktree_unstage(worktree, &paths, update_progress,
9005 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9007 print_update_progress_stats(&upa);
9009 if (patch_script_file && fclose(patch_script_file) == EOF &&
9011 error = got_error_from_errno2("fclose", patch_script_path);
9013 got_repo_close(repo);
9015 got_worktree_close(worktree);
9016 TAILQ_FOREACH(pe, &paths, entry)
9017 free((char *)pe->path);
9018 got_pathlist_free(&paths);
9026 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9027 "arg1 [arg2 ...]\n", getprogname());
9031 static const struct got_error *
9032 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9034 const struct got_error *err;
9035 struct got_blob_object *blob;
9037 err = got_object_open_as_blob(&blob, repo, id, 8192);
9041 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9042 got_object_blob_close(blob);
9046 static const struct got_error *
9047 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9049 const struct got_error *err;
9050 struct got_tree_object *tree;
9053 err = got_object_open_as_tree(&tree, repo, id);
9057 nentries = got_object_tree_get_nentries(tree);
9058 for (i = 0; i < nentries; i++) {
9059 struct got_tree_entry *te;
9061 if (sigint_received || sigpipe_received)
9063 te = got_object_tree_get_entry(tree, i);
9064 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9067 fprintf(outfile, "%s %.7o %s\n", id_str,
9068 got_tree_entry_get_mode(te),
9069 got_tree_entry_get_name(te));
9073 got_object_tree_close(tree);
9077 static const struct got_error *
9078 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9080 const struct got_error *err;
9081 struct got_commit_object *commit;
9082 const struct got_object_id_queue *parent_ids;
9083 struct got_object_qid *pid;
9084 char *id_str = NULL;
9085 const char *logmsg = NULL;
9087 err = got_object_open_as_commit(&commit, repo, id);
9091 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9095 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9096 parent_ids = got_object_commit_get_parent_ids(commit);
9097 fprintf(outfile, "numparents %d\n",
9098 got_object_commit_get_nparents(commit));
9099 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9101 err = got_object_id_str(&pid_str, pid->id);
9104 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9107 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9108 got_object_commit_get_author(commit),
9109 got_object_commit_get_author_time(commit));
9111 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9112 got_object_commit_get_author(commit),
9113 got_object_commit_get_committer_time(commit));
9115 logmsg = got_object_commit_get_logmsg_raw(commit);
9116 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9117 fprintf(outfile, "%s", logmsg);
9120 got_object_commit_close(commit);
9124 static const struct got_error *
9125 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9127 const struct got_error *err;
9128 struct got_tag_object *tag;
9129 char *id_str = NULL;
9130 const char *tagmsg = NULL;
9132 err = got_object_open_as_tag(&tag, repo, id);
9136 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9140 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9142 switch (got_object_tag_get_object_type(tag)) {
9143 case GOT_OBJ_TYPE_BLOB:
9144 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9145 GOT_OBJ_LABEL_BLOB);
9147 case GOT_OBJ_TYPE_TREE:
9148 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9149 GOT_OBJ_LABEL_TREE);
9151 case GOT_OBJ_TYPE_COMMIT:
9152 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9153 GOT_OBJ_LABEL_COMMIT);
9155 case GOT_OBJ_TYPE_TAG:
9156 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9164 got_object_tag_get_name(tag));
9166 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9167 got_object_tag_get_tagger(tag),
9168 got_object_tag_get_tagger_time(tag));
9170 tagmsg = got_object_tag_get_message(tag);
9171 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9172 fprintf(outfile, "%s", tagmsg);
9175 got_object_tag_close(tag);
9179 static const struct got_error *
9180 cmd_cat(int argc, char *argv[])
9182 const struct got_error *error;
9183 struct got_repository *repo = NULL;
9184 struct got_worktree *worktree = NULL;
9185 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9186 const char *commit_id_str = NULL;
9187 struct got_object_id *id = NULL, *commit_id = NULL;
9188 int ch, obj_type, i, force_path = 0;
9191 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9196 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9199 commit_id_str = optarg;
9202 repo_path = realpath(optarg, NULL);
9203 if (repo_path == NULL)
9204 return got_error_from_errno2("realpath",
9206 got_path_strip_trailing_slashes(repo_path);
9220 cwd = getcwd(NULL, 0);
9222 error = got_error_from_errno("getcwd");
9225 error = got_worktree_open(&worktree, cwd);
9226 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9229 if (repo_path == NULL) {
9231 got_worktree_get_repo_path(worktree));
9232 if (repo_path == NULL) {
9233 error = got_error_from_errno("strdup");
9239 if (repo_path == NULL) {
9240 repo_path = getcwd(NULL, 0);
9241 if (repo_path == NULL)
9242 return got_error_from_errno("getcwd");
9245 error = got_repo_open(&repo, repo_path, NULL);
9250 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9254 if (commit_id_str == NULL)
9255 commit_id_str = GOT_REF_HEAD;
9256 error = got_repo_match_object_id(&commit_id, NULL,
9257 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9261 for (i = 0; i < argc; i++) {
9263 error = got_object_id_by_path(&id, repo, commit_id,
9268 error = got_repo_match_object_id(&id, &label, argv[i],
9269 GOT_OBJ_TYPE_ANY, 0, repo);
9271 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9272 error->code != GOT_ERR_NOT_REF)
9274 error = got_object_id_by_path(&id, repo,
9275 commit_id, argv[i]);
9281 error = got_object_get_type(&obj_type, repo, id);
9286 case GOT_OBJ_TYPE_BLOB:
9287 error = cat_blob(id, repo, stdout);
9289 case GOT_OBJ_TYPE_TREE:
9290 error = cat_tree(id, repo, stdout);
9292 case GOT_OBJ_TYPE_COMMIT:
9293 error = cat_commit(id, repo, stdout);
9295 case GOT_OBJ_TYPE_TAG:
9296 error = cat_tag(id, repo, stdout);
9299 error = got_error(GOT_ERR_OBJ_TYPE);
9314 got_worktree_close(worktree);
9316 const struct got_error *repo_error;
9317 repo_error = got_repo_close(repo);
9327 fprintf(stderr, "usage: %s info [path ...]\n",
9332 static const struct got_error *
9333 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9334 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9335 struct got_object_id *commit_id)
9337 const struct got_error *err = NULL;
9338 char *id_str = NULL;
9340 struct tm mytm, *tm;
9341 struct got_pathlist_head *paths = arg;
9342 struct got_pathlist_entry *pe;
9345 * Clear error indication from any of the path arguments which
9346 * would cause this file index entry to be displayed.
9348 TAILQ_FOREACH(pe, paths, entry) {
9349 if (got_path_cmp(path, pe->path, strlen(path),
9350 pe->path_len) == 0 ||
9351 got_path_is_child(path, pe->path, pe->path_len))
9352 pe->data = NULL; /* no error */
9355 printf(GOT_COMMIT_SEP_STR);
9357 printf("symlink: %s\n", path);
9358 else if (S_ISREG(mode)) {
9359 printf("file: %s\n", path);
9360 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9361 } else if (S_ISDIR(mode))
9362 printf("directory: %s\n", path);
9364 printf("something: %s\n", path);
9366 tm = localtime_r(&mtime, &mytm);
9369 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9370 return got_error(GOT_ERR_NO_SPACE);
9371 printf("timestamp: %s\n", datebuf);
9374 err = got_object_id_str(&id_str, blob_id);
9377 printf("based on blob: %s\n", id_str);
9381 if (staged_blob_id) {
9382 err = got_object_id_str(&id_str, staged_blob_id);
9385 printf("based on staged blob: %s\n", id_str);
9390 err = got_object_id_str(&id_str, commit_id);
9393 printf("based on commit: %s\n", id_str);
9400 static const struct got_error *
9401 cmd_info(int argc, char *argv[])
9403 const struct got_error *error = NULL;
9404 struct got_worktree *worktree = NULL;
9405 char *cwd = NULL, *id_str = NULL;
9406 struct got_pathlist_head paths;
9407 struct got_pathlist_entry *pe;
9408 char *uuidstr = NULL;
9409 int ch, show_files = 0;
9413 while ((ch = getopt(argc, argv, "")) != -1) {
9425 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9429 cwd = getcwd(NULL, 0);
9431 error = got_error_from_errno("getcwd");
9435 error = got_worktree_open(&worktree, cwd);
9437 if (error->code == GOT_ERR_NOT_WORKTREE)
9438 error = wrap_not_worktree_error(error, "status", cwd);
9442 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9447 error = get_worktree_paths_from_argv(&paths, argc, argv,
9454 error = got_object_id_str(&id_str,
9455 got_worktree_get_base_commit_id(worktree));
9459 error = got_worktree_get_uuid(&uuidstr, worktree);
9463 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9464 printf("work tree base commit: %s\n", id_str);
9465 printf("work tree path prefix: %s\n",
9466 got_worktree_get_path_prefix(worktree));
9467 printf("work tree branch reference: %s\n",
9468 got_worktree_get_head_ref_name(worktree));
9469 printf("work tree UUID: %s\n", uuidstr);
9470 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9473 struct got_pathlist_entry *pe;
9474 TAILQ_FOREACH(pe, &paths, entry) {
9475 if (pe->path_len == 0)
9478 * Assume this path will fail. This will be corrected
9479 * in print_path_info() in case the path does suceeed.
9481 pe->data = (void *)got_error_path(pe->path,
9484 error = got_worktree_path_info(worktree, &paths,
9485 print_path_info, &paths, check_cancelled, NULL);
9488 TAILQ_FOREACH(pe, &paths, entry) {
9489 if (pe->data != NULL) {
9490 error = pe->data; /* bad path */
9496 TAILQ_FOREACH(pe, &paths, entry)
9497 free((char *)pe->path);
9498 got_pathlist_free(&paths);