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 && !reverse_display_order) {
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 if (show_changed_paths) {
3318 err = get_changed_paths(&changed_paths,
3323 err = print_commit(commit, qid->id, repo, path,
3324 show_changed_paths ? &changed_paths : NULL,
3325 show_patch, diff_context, refs);
3326 got_object_commit_close(commit);
3329 TAILQ_FOREACH(pe, &changed_paths, entry) {
3330 free((char *)pe->path);
3333 got_pathlist_free(&changed_paths);
3337 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3338 qid = SIMPLEQ_FIRST(&reversed_commits);
3339 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3340 got_object_qid_free(qid);
3342 TAILQ_FOREACH(pe, &changed_paths, entry) {
3343 free((char *)pe->path);
3346 got_pathlist_free(&changed_paths);
3349 got_commit_graph_close(graph);
3356 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3357 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3358 "[-R] [path]\n", getprogname());
3363 get_default_log_limit(void)
3365 const char *got_default_log_limit;
3369 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3370 if (got_default_log_limit == NULL)
3372 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3378 static const struct got_error *
3379 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3380 struct got_repository *repo)
3382 const struct got_error *err = NULL;
3383 struct got_reference *ref;
3387 err = got_ref_open(&ref, repo, commit_arg, 0);
3390 err = got_ref_resolve(id, repo, ref);
3394 err = got_object_get_type(&obj_type, repo, *id);
3397 if (obj_type == GOT_OBJ_TYPE_TAG) {
3398 struct got_tag_object *tag;
3399 err = got_object_open_as_tag(&tag, repo, *id);
3402 if (got_object_tag_get_object_type(tag) !=
3403 GOT_OBJ_TYPE_COMMIT) {
3404 got_object_tag_close(tag);
3405 return got_error(GOT_ERR_OBJ_TYPE);
3408 *id = got_object_id_dup(
3409 got_object_tag_get_object_id(tag));
3411 err = got_error_from_errno(
3412 "got_object_id_dup");
3413 got_object_tag_close(tag);
3416 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3417 return got_error(GOT_ERR_OBJ_TYPE);
3419 err = got_repo_match_object_id_prefix(id, commit_arg,
3420 GOT_OBJ_TYPE_COMMIT, repo);
3426 static const struct got_error *
3427 cmd_log(int argc, char *argv[])
3429 const struct got_error *error;
3430 struct got_repository *repo = NULL;
3431 struct got_worktree *worktree = NULL;
3432 struct got_object_id *start_id = NULL, *end_id = NULL;
3433 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3434 const char *start_commit = NULL, *end_commit = NULL;
3435 const char *search_pattern = NULL;
3436 int diff_context = -1, ch;
3437 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3438 int reverse_display_order = 0;
3440 struct got_reflist_head refs;
3442 SIMPLEQ_INIT(&refs);
3445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3451 limit = get_default_log_limit();
3453 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3459 show_changed_paths = 1;
3462 start_commit = optarg;
3465 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3468 err(1, "-C option %s", errstr);
3471 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3473 err(1, "-l option %s", errstr);
3479 repo_path = realpath(optarg, NULL);
3480 if (repo_path == NULL)
3481 return got_error_from_errno2("realpath",
3483 got_path_strip_trailing_slashes(repo_path);
3486 reverse_display_order = 1;
3489 search_pattern = optarg;
3492 end_commit = optarg;
3503 if (diff_context == -1)
3505 else if (!show_patch)
3506 errx(1, "-C reguires -p");
3508 cwd = getcwd(NULL, 0);
3510 error = got_error_from_errno("getcwd");
3514 if (repo_path == NULL) {
3515 error = got_worktree_open(&worktree, cwd);
3516 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3524 error = got_error_from_errno("strdup");
3527 } else if (argc == 1) {
3529 error = got_worktree_resolve_path(&path, worktree,
3534 path = strdup(argv[0]);
3536 error = got_error_from_errno("strdup");
3543 if (repo_path == NULL) {
3544 repo_path = worktree ?
3545 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3547 if (repo_path == NULL) {
3548 error = got_error_from_errno("strdup");
3552 error = got_repo_open(&repo, repo_path, NULL);
3556 error = apply_unveil(got_repo_get_path(repo), 1,
3557 worktree ? got_worktree_get_root_path(worktree) : NULL);
3561 if (start_commit == NULL) {
3562 struct got_reference *head_ref;
3563 struct got_commit_object *commit = NULL;
3564 error = got_ref_open(&head_ref, repo,
3565 worktree ? got_worktree_get_head_ref_name(worktree)
3569 error = got_ref_resolve(&start_id, repo, head_ref);
3570 got_ref_close(head_ref);
3573 error = got_object_open_as_commit(&commit, repo,
3577 got_object_commit_close(commit);
3579 error = resolve_commit_arg(&start_id, start_commit, repo);
3583 if (end_commit != NULL) {
3584 error = resolve_commit_arg(&end_id, end_commit, repo);
3590 const char *prefix = got_worktree_get_path_prefix(worktree);
3592 if (asprintf(&p, "%s%s%s", prefix,
3593 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3594 error = got_error_from_errno("asprintf");
3597 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3600 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3605 path = in_repo_path;
3608 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3612 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3613 show_patch, search_pattern, diff_context, limit, log_branches,
3614 reverse_display_order, &refs);
3620 got_worktree_close(worktree);
3622 const struct got_error *repo_error;
3623 repo_error = got_repo_close(repo);
3627 got_ref_list_free(&refs);
3634 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3635 "[-w] [object1 object2 | path]\n", getprogname());
3639 struct print_diff_arg {
3640 struct got_repository *repo;
3641 struct got_worktree *worktree;
3646 int ignore_whitespace;
3650 * Create a file which contains the target path of a symlink so we can feed
3651 * it as content to the diff engine.
3653 static const struct got_error *
3654 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3655 const char *abspath)
3657 const struct got_error *err = NULL;
3658 char target_path[PATH_MAX];
3659 ssize_t target_len, outlen;
3664 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3665 if (target_len == -1)
3666 return got_error_from_errno2("readlinkat", abspath);
3668 target_len = readlink(abspath, target_path, PATH_MAX);
3669 if (target_len == -1)
3670 return got_error_from_errno2("readlink", abspath);
3673 *fd = got_opentempfd();
3675 return got_error_from_errno("got_opentempfd");
3677 outlen = write(*fd, target_path, target_len);
3679 err = got_error_from_errno("got_opentempfd");
3683 if (lseek(*fd, 0, SEEK_SET) == -1) {
3684 err = got_error_from_errno2("lseek", abspath);
3695 static const struct got_error *
3696 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3697 const char *path, struct got_object_id *blob_id,
3698 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3699 int dirfd, const char *de_name)
3701 struct print_diff_arg *a = arg;
3702 const struct got_error *err = NULL;
3703 struct got_blob_object *blob1 = NULL;
3706 char *abspath = NULL, *label1 = NULL;
3709 if (a->diff_staged) {
3710 if (staged_status != GOT_STATUS_MODIFY &&
3711 staged_status != GOT_STATUS_ADD &&
3712 staged_status != GOT_STATUS_DELETE)
3715 if (staged_status == GOT_STATUS_DELETE)
3717 if (status == GOT_STATUS_NONEXISTENT)
3718 return got_error_set_errno(ENOENT, path);
3719 if (status != GOT_STATUS_MODIFY &&
3720 status != GOT_STATUS_ADD &&
3721 status != GOT_STATUS_DELETE &&
3722 status != GOT_STATUS_CONFLICT)
3726 if (!a->header_shown) {
3727 printf("diff %s %s%s\n", a->id_str,
3728 got_worktree_get_root_path(a->worktree),
3729 a->diff_staged ? " (staged changes)" : "");
3730 a->header_shown = 1;
3733 if (a->diff_staged) {
3734 const char *label1 = NULL, *label2 = NULL;
3735 switch (staged_status) {
3736 case GOT_STATUS_MODIFY:
3740 case GOT_STATUS_ADD:
3743 case GOT_STATUS_DELETE:
3747 return got_error(GOT_ERR_FILE_STATUS);
3749 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3750 label1, label2, a->diff_context, a->ignore_whitespace,
3754 if (staged_status == GOT_STATUS_ADD ||
3755 staged_status == GOT_STATUS_MODIFY) {
3757 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3761 err = got_object_id_str(&id_str, staged_blob_id);
3764 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3765 err = got_error_from_errno("asprintf");
3770 } else if (status != GOT_STATUS_ADD) {
3771 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3776 if (status != GOT_STATUS_DELETE) {
3777 if (asprintf(&abspath, "%s/%s",
3778 got_worktree_get_root_path(a->worktree), path) == -1) {
3779 err = got_error_from_errno("asprintf");
3784 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3786 if (errno != ELOOP) {
3787 err = got_error_from_errno2("openat",
3791 err = get_symlink_target_file(&fd, dirfd,
3797 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3799 if (errno != ELOOP) {
3800 err = got_error_from_errno2("open",
3804 err = get_symlink_target_file(&fd, dirfd,
3810 if (fstat(fd, &sb) == -1) {
3811 err = got_error_from_errno2("fstat", abspath);
3814 f2 = fdopen(fd, "r");
3816 err = got_error_from_errno2("fdopen", abspath);
3823 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3824 a->diff_context, a->ignore_whitespace, stdout);
3827 got_object_blob_close(blob1);
3828 if (f2 && fclose(f2) == EOF && err == NULL)
3829 err = got_error_from_errno("fclose");
3830 if (fd != -1 && close(fd) == -1 && err == NULL)
3831 err = got_error_from_errno("close");
3836 static const struct got_error *
3837 cmd_diff(int argc, char *argv[])
3839 const struct got_error *error;
3840 struct got_repository *repo = NULL;
3841 struct got_worktree *worktree = NULL;
3842 char *cwd = NULL, *repo_path = NULL;
3843 struct got_object_id *id1 = NULL, *id2 = NULL;
3844 const char *id_str1 = NULL, *id_str2 = NULL;
3845 char *label1 = NULL, *label2 = NULL;
3847 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3852 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3857 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3860 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3863 err(1, "-C option %s", errstr);
3866 repo_path = realpath(optarg, NULL);
3867 if (repo_path == NULL)
3868 return got_error_from_errno2("realpath",
3870 got_path_strip_trailing_slashes(repo_path);
3876 ignore_whitespace = 1;
3887 cwd = getcwd(NULL, 0);
3889 error = got_error_from_errno("getcwd");
3895 "-r option can't be used when diffing a work tree");
3896 error = got_worktree_open(&worktree, cwd);
3898 if (error->code == GOT_ERR_NOT_WORKTREE)
3899 error = wrap_not_worktree_error(error, "diff",
3903 repo_path = strdup(got_worktree_get_repo_path(worktree));
3904 if (repo_path == NULL) {
3905 error = got_error_from_errno("strdup");
3909 error = got_worktree_resolve_path(&path, worktree,
3916 error = got_error_from_errno("strdup");
3920 } else if (argc == 2) {
3922 errx(1, "-s option can't be used when diffing "
3923 "objects in repository");
3926 if (repo_path == NULL) {
3927 error = got_worktree_open(&worktree, cwd);
3928 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3932 got_worktree_get_repo_path(worktree));
3933 if (repo_path == NULL) {
3934 error = got_error_from_errno("strdup");
3938 repo_path = strdup(cwd);
3939 if (repo_path == NULL) {
3940 error = got_error_from_errno("strdup");
3948 error = got_repo_open(&repo, repo_path, NULL);
3953 error = apply_unveil(got_repo_get_path(repo), 1,
3954 worktree ? got_worktree_get_root_path(worktree) : NULL);
3959 struct print_diff_arg arg;
3960 struct got_pathlist_head paths;
3965 error = got_object_id_str(&id_str,
3966 got_worktree_get_base_commit_id(worktree));
3970 arg.worktree = worktree;
3971 arg.diff_context = diff_context;
3972 arg.id_str = id_str;
3973 arg.header_shown = 0;
3974 arg.diff_staged = diff_staged;
3975 arg.ignore_whitespace = ignore_whitespace;
3977 error = got_pathlist_append(&paths, path, NULL);
3981 error = got_worktree_status(worktree, &paths, repo, print_diff,
3982 &arg, check_cancelled, NULL);
3984 got_pathlist_free(&paths);
3988 error = got_repo_match_object_id(&id1, &label1, id_str1,
3989 GOT_OBJ_TYPE_ANY, 1, repo);
3993 error = got_repo_match_object_id(&id2, &label2, id_str2,
3994 GOT_OBJ_TYPE_ANY, 1, repo);
3998 error = got_object_get_type(&type1, repo, id1);
4002 error = got_object_get_type(&type2, repo, id2);
4006 if (type1 != type2) {
4007 error = got_error(GOT_ERR_OBJ_TYPE);
4012 case GOT_OBJ_TYPE_BLOB:
4013 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4014 diff_context, ignore_whitespace, repo, stdout);
4016 case GOT_OBJ_TYPE_TREE:
4017 error = got_diff_objects_as_trees(id1, id2, "", "",
4018 diff_context, ignore_whitespace, repo, stdout);
4020 case GOT_OBJ_TYPE_COMMIT:
4021 printf("diff %s %s\n", label1, label2);
4022 error = got_diff_objects_as_commits(id1, id2, diff_context,
4023 ignore_whitespace, repo, stdout);
4026 error = got_error(GOT_ERR_OBJ_TYPE);
4035 got_worktree_close(worktree);
4037 const struct got_error *repo_error;
4038 repo_error = got_repo_close(repo);
4049 "usage: %s blame [-c commit] [-r repository-path] path\n",
4058 char datebuf[11]; /* YYYY-MM-DD + NUL */
4061 struct blame_cb_args {
4062 struct blame_line *lines;
4066 off_t *line_offsets;
4068 struct got_repository *repo;
4071 static const struct got_error *
4072 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4074 const struct got_error *err = NULL;
4075 struct blame_cb_args *a = arg;
4076 struct blame_line *bline;
4078 size_t linesize = 0;
4079 struct got_commit_object *commit = NULL;
4082 time_t committer_time;
4084 if (nlines != a->nlines ||
4085 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4086 return got_error(GOT_ERR_RANGE);
4088 if (sigint_received)
4089 return got_error(GOT_ERR_ITER_COMPLETED);
4092 return NULL; /* no change in this commit */
4094 /* Annotate this line. */
4095 bline = &a->lines[lineno - 1];
4096 if (bline->annotated)
4098 err = got_object_id_str(&bline->id_str, id);
4102 err = got_object_open_as_commit(&commit, a->repo, id);
4106 bline->committer = strdup(got_object_commit_get_committer(commit));
4107 if (bline->committer == NULL) {
4108 err = got_error_from_errno("strdup");
4112 committer_time = got_object_commit_get_committer_time(commit);
4113 if (localtime_r(&committer_time, &tm) == NULL)
4114 return got_error_from_errno("localtime_r");
4115 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4116 &tm) >= sizeof(bline->datebuf)) {
4117 err = got_error(GOT_ERR_NO_SPACE);
4120 bline->annotated = 1;
4122 /* Print lines annotated so far. */
4123 bline = &a->lines[a->lineno_cur - 1];
4124 if (!bline->annotated)
4127 offset = a->line_offsets[a->lineno_cur - 1];
4128 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4129 err = got_error_from_errno("fseeko");
4133 while (bline->annotated) {
4134 char *smallerthan, *at, *nl, *committer;
4137 if (getline(&line, &linesize, a->f) == -1) {
4139 err = got_error_from_errno("getline");
4143 committer = bline->committer;
4144 smallerthan = strchr(committer, '<');
4145 if (smallerthan && smallerthan[1] != '\0')
4146 committer = smallerthan + 1;
4147 at = strchr(committer, '@');
4150 len = strlen(committer);
4152 committer[8] = '\0';
4154 nl = strchr(line, '\n');
4157 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4158 bline->id_str, bline->datebuf, committer, line);
4161 bline = &a->lines[a->lineno_cur - 1];
4165 got_object_commit_close(commit);
4170 static const struct got_error *
4171 cmd_blame(int argc, char *argv[])
4173 const struct got_error *error;
4174 struct got_repository *repo = NULL;
4175 struct got_worktree *worktree = NULL;
4176 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4177 char *link_target = NULL;
4178 struct got_object_id *obj_id = NULL;
4179 struct got_object_id *commit_id = NULL;
4180 struct got_blob_object *blob = NULL;
4181 char *commit_id_str = NULL;
4182 struct blame_cb_args bca;
4183 int ch, obj_type, i;
4186 memset(&bca, 0, sizeof(bca));
4189 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4194 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4197 commit_id_str = optarg;
4200 repo_path = realpath(optarg, NULL);
4201 if (repo_path == NULL)
4202 return got_error_from_errno2("realpath",
4204 got_path_strip_trailing_slashes(repo_path);
4220 cwd = getcwd(NULL, 0);
4222 error = got_error_from_errno("getcwd");
4225 if (repo_path == NULL) {
4226 error = got_worktree_open(&worktree, cwd);
4227 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4233 strdup(got_worktree_get_repo_path(worktree));
4234 if (repo_path == NULL) {
4235 error = got_error_from_errno("strdup");
4240 repo_path = strdup(cwd);
4241 if (repo_path == NULL) {
4242 error = got_error_from_errno("strdup");
4248 error = got_repo_open(&repo, repo_path, NULL);
4252 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4257 const char *prefix = got_worktree_get_path_prefix(worktree);
4258 char *p, *worktree_subdir = cwd +
4259 strlen(got_worktree_get_root_path(worktree));
4260 if (asprintf(&p, "%s%s%s%s%s",
4261 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4262 worktree_subdir, worktree_subdir[0] ? "/" : "",
4264 error = got_error_from_errno("asprintf");
4267 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4270 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4275 if (commit_id_str == NULL) {
4276 struct got_reference *head_ref;
4277 error = got_ref_open(&head_ref, repo, worktree ?
4278 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4281 error = got_ref_resolve(&commit_id, repo, head_ref);
4282 got_ref_close(head_ref);
4286 error = got_repo_match_object_id(&commit_id, NULL,
4287 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4292 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4297 error = got_object_id_by_path(&obj_id, repo, commit_id,
4298 link_target ? link_target : in_repo_path);
4302 error = got_object_get_type(&obj_type, repo, obj_id);
4306 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4307 error = got_error_path(link_target ? link_target : in_repo_path,
4312 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4315 bca.f = got_opentemp();
4316 if (bca.f == NULL) {
4317 error = got_error_from_errno("got_opentemp");
4320 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4321 &bca.line_offsets, bca.f, blob);
4322 if (error || bca.nlines == 0)
4325 /* Don't include \n at EOF in the blame line count. */
4326 if (bca.line_offsets[bca.nlines - 1] == filesize)
4329 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4330 if (bca.lines == NULL) {
4331 error = got_error_from_errno("calloc");
4335 bca.nlines_prec = 0;
4343 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4344 repo, blame_cb, &bca, check_cancelled, NULL);
4353 got_object_blob_close(blob);
4355 got_worktree_close(worktree);
4357 const struct got_error *repo_error;
4358 repo_error = got_repo_close(repo);
4363 for (i = 0; i < bca.nlines; i++) {
4364 struct blame_line *bline = &bca.lines[i];
4365 free(bline->id_str);
4366 free(bline->committer);
4370 free(bca.line_offsets);
4371 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4372 error = got_error_from_errno("fclose");
4380 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4385 static const struct got_error *
4386 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4387 const char *root_path, struct got_repository *repo)
4389 const struct got_error *err = NULL;
4390 int is_root_path = (strcmp(path, root_path) == 0);
4391 const char *modestr = "";
4392 mode_t mode = got_tree_entry_get_mode(te);
4393 char *link_target = NULL;
4395 path += strlen(root_path);
4396 while (path[0] == '/')
4399 if (got_object_tree_entry_is_submodule(te))
4401 else if (S_ISLNK(mode)) {
4404 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4407 for (i = 0; i < strlen(link_target); i++) {
4408 if (!isprint((unsigned char)link_target[i]))
4409 link_target[i] = '?';
4414 else if (S_ISDIR(mode))
4416 else if (mode & S_IXUSR)
4419 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4420 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4421 link_target ? " -> ": "", link_target ? link_target : "");
4427 static const struct got_error *
4428 print_tree(const char *path, struct got_object_id *commit_id,
4429 int show_ids, int recurse, const char *root_path,
4430 struct got_repository *repo)
4432 const struct got_error *err = NULL;
4433 struct got_object_id *tree_id = NULL;
4434 struct got_tree_object *tree = NULL;
4437 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4441 err = got_object_open_as_tree(&tree, repo, tree_id);
4444 nentries = got_object_tree_get_nentries(tree);
4445 for (i = 0; i < nentries; i++) {
4446 struct got_tree_entry *te;
4449 if (sigint_received || sigpipe_received)
4452 te = got_object_tree_get_entry(tree, i);
4455 err = got_object_id_str(&id_str,
4456 got_tree_entry_get_id(te));
4459 if (asprintf(&id, "%s ", id_str) == -1) {
4460 err = got_error_from_errno("asprintf");
4466 err = print_entry(te, id, path, root_path, repo);
4471 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4473 if (asprintf(&child_path, "%s%s%s", path,
4474 path[0] == '/' && path[1] == '\0' ? "" : "/",
4475 got_tree_entry_get_name(te)) == -1) {
4476 err = got_error_from_errno("asprintf");
4479 err = print_tree(child_path, commit_id, show_ids, 1,
4488 got_object_tree_close(tree);
4493 static const struct got_error *
4494 cmd_tree(int argc, char *argv[])
4496 const struct got_error *error;
4497 struct got_repository *repo = NULL;
4498 struct got_worktree *worktree = NULL;
4499 const char *path, *refname = NULL;
4500 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4501 struct got_object_id *commit_id = NULL;
4502 char *commit_id_str = NULL;
4503 int show_ids = 0, recurse = 0;
4507 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4512 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4515 commit_id_str = optarg;
4518 repo_path = realpath(optarg, NULL);
4519 if (repo_path == NULL)
4520 return got_error_from_errno2("realpath",
4522 got_path_strip_trailing_slashes(repo_path);
4546 cwd = getcwd(NULL, 0);
4548 error = got_error_from_errno("getcwd");
4551 if (repo_path == NULL) {
4552 error = got_worktree_open(&worktree, cwd);
4553 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4559 strdup(got_worktree_get_repo_path(worktree));
4560 if (repo_path == NULL)
4561 error = got_error_from_errno("strdup");
4565 repo_path = strdup(cwd);
4566 if (repo_path == NULL) {
4567 error = got_error_from_errno("strdup");
4573 error = got_repo_open(&repo, repo_path, NULL);
4577 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4583 char *p, *worktree_subdir = cwd +
4584 strlen(got_worktree_get_root_path(worktree));
4585 if (asprintf(&p, "%s/%s",
4586 got_worktree_get_path_prefix(worktree),
4587 worktree_subdir) == -1) {
4588 error = got_error_from_errno("asprintf");
4591 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4598 if (in_repo_path == NULL) {
4599 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4604 if (commit_id_str == NULL) {
4605 struct got_reference *head_ref;
4607 refname = got_worktree_get_head_ref_name(worktree);
4609 refname = GOT_REF_HEAD;
4610 error = got_ref_open(&head_ref, repo, refname, 0);
4613 error = got_ref_resolve(&commit_id, repo, head_ref);
4614 got_ref_close(head_ref);
4618 error = got_repo_match_object_id(&commit_id, NULL,
4619 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4624 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4625 in_repo_path, repo);
4632 got_worktree_close(worktree);
4634 const struct got_error *repo_error;
4635 repo_error = got_repo_close(repo);
4645 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4649 static const struct got_error *
4650 print_status(void *arg, unsigned char status, unsigned char staged_status,
4651 const char *path, struct got_object_id *blob_id,
4652 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4653 int dirfd, const char *de_name)
4655 if (status == staged_status && (status == GOT_STATUS_DELETE))
4656 status = GOT_STATUS_NO_CHANGE;
4657 printf("%c%c %s\n", status, staged_status, path);
4661 static const struct got_error *
4662 cmd_status(int argc, char *argv[])
4664 const struct got_error *error = NULL;
4665 struct got_repository *repo = NULL;
4666 struct got_worktree *worktree = NULL;
4668 struct got_pathlist_head paths;
4669 struct got_pathlist_entry *pe;
4674 while ((ch = getopt(argc, argv, "")) != -1) {
4686 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4690 cwd = getcwd(NULL, 0);
4692 error = got_error_from_errno("getcwd");
4696 error = got_worktree_open(&worktree, cwd);
4698 if (error->code == GOT_ERR_NOT_WORKTREE)
4699 error = wrap_not_worktree_error(error, "status", cwd);
4703 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4708 error = apply_unveil(got_repo_get_path(repo), 1,
4709 got_worktree_get_root_path(worktree));
4713 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4717 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4718 check_cancelled, NULL);
4720 TAILQ_FOREACH(pe, &paths, entry)
4721 free((char *)pe->path);
4722 got_pathlist_free(&paths);
4731 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4737 static const struct got_error *
4738 list_refs(struct got_repository *repo, const char *refname)
4740 static const struct got_error *err = NULL;
4741 struct got_reflist_head refs;
4742 struct got_reflist_entry *re;
4744 SIMPLEQ_INIT(&refs);
4745 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4749 SIMPLEQ_FOREACH(re, &refs, entry) {
4751 refstr = got_ref_to_str(re->ref);
4753 return got_error_from_errno("got_ref_to_str");
4754 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4758 got_ref_list_free(&refs);
4762 static const struct got_error *
4763 delete_ref(struct got_repository *repo, const char *refname)
4765 const struct got_error *err = NULL;
4766 struct got_reference *ref;
4768 err = got_ref_open(&ref, repo, refname, 0);
4772 err = got_ref_delete(ref, repo);
4777 static const struct got_error *
4778 add_ref(struct got_repository *repo, const char *refname, const char *target)
4780 const struct got_error *err = NULL;
4781 struct got_object_id *id;
4782 struct got_reference *ref = NULL;
4785 * Don't let the user create a reference name with a leading '-'.
4786 * While technically a valid reference name, this case is usually
4787 * an unintended typo.
4789 if (refname[0] == '-')
4790 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4792 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4795 struct got_reference *target_ref;
4797 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4799 err = got_ref_open(&target_ref, repo, target, 0);
4802 err = got_ref_resolve(&id, repo, target_ref);
4803 got_ref_close(target_ref);
4808 err = got_ref_alloc(&ref, refname, id);
4812 err = got_ref_write(ref, repo);
4820 static const struct got_error *
4821 add_symref(struct got_repository *repo, const char *refname, const char *target)
4823 const struct got_error *err = NULL;
4824 struct got_reference *ref = NULL;
4825 struct got_reference *target_ref = NULL;
4828 * Don't let the user create a reference name with a leading '-'.
4829 * While technically a valid reference name, this case is usually
4830 * an unintended typo.
4832 if (refname[0] == '-')
4833 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4835 err = got_ref_open(&target_ref, repo, target, 0);
4839 err = got_ref_alloc_symref(&ref, refname, target_ref);
4843 err = got_ref_write(ref, repo);
4846 got_ref_close(target_ref);
4852 static const struct got_error *
4853 cmd_ref(int argc, char *argv[])
4855 const struct got_error *error = NULL;
4856 struct got_repository *repo = NULL;
4857 struct got_worktree *worktree = NULL;
4858 char *cwd = NULL, *repo_path = NULL;
4859 int ch, do_list = 0, do_delete = 0;
4860 const char *obj_arg = NULL, *symref_target= NULL;
4861 char *refname = NULL;
4863 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4872 repo_path = realpath(optarg, NULL);
4873 if (repo_path == NULL)
4874 return got_error_from_errno2("realpath",
4876 got_path_strip_trailing_slashes(repo_path);
4882 symref_target = optarg;
4890 if (obj_arg && do_list)
4891 errx(1, "-c and -l options are mutually exclusive");
4892 if (obj_arg && do_delete)
4893 errx(1, "-c and -d options are mutually exclusive");
4894 if (obj_arg && symref_target)
4895 errx(1, "-c and -s options are mutually exclusive");
4896 if (symref_target && do_delete)
4897 errx(1, "-s and -d options are mutually exclusive");
4898 if (symref_target && do_list)
4899 errx(1, "-s and -l options are mutually exclusive");
4900 if (do_delete && do_list)
4901 errx(1, "-d and -l options are mutually exclusive");
4907 if (argc != 0 && argc != 1)
4910 refname = strdup(argv[0]);
4911 if (refname == NULL) {
4912 error = got_error_from_errno("strdup");
4919 refname = strdup(argv[0]);
4920 if (refname == NULL) {
4921 error = got_error_from_errno("strdup");
4927 got_path_strip_trailing_slashes(refname);
4931 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4935 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4936 "sendfd unveil", NULL) == -1)
4940 cwd = getcwd(NULL, 0);
4942 error = got_error_from_errno("getcwd");
4946 if (repo_path == NULL) {
4947 error = got_worktree_open(&worktree, cwd);
4948 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4954 strdup(got_worktree_get_repo_path(worktree));
4955 if (repo_path == NULL)
4956 error = got_error_from_errno("strdup");
4960 repo_path = strdup(cwd);
4961 if (repo_path == NULL) {
4962 error = got_error_from_errno("strdup");
4968 error = got_repo_open(&repo, repo_path, NULL);
4972 error = apply_unveil(got_repo_get_path(repo), do_list,
4973 worktree ? got_worktree_get_root_path(worktree) : NULL);
4978 error = list_refs(repo, refname);
4980 error = delete_ref(repo, refname);
4981 else if (symref_target)
4982 error = add_symref(repo, refname, symref_target);
4984 if (obj_arg == NULL)
4986 error = add_ref(repo, refname, obj_arg);
4991 got_repo_close(repo);
4993 got_worktree_close(worktree);
5003 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5004 "[name]\n", getprogname());
5008 static const struct got_error *
5009 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5010 struct got_reference *ref)
5012 const struct got_error *err = NULL;
5013 const char *refname, *marker = " ";
5016 refname = got_ref_get_name(ref);
5017 if (worktree && strcmp(refname,
5018 got_worktree_get_head_ref_name(worktree)) == 0) {
5019 struct got_object_id *id = NULL;
5021 err = got_ref_resolve(&id, repo, ref);
5024 if (got_object_id_cmp(id,
5025 got_worktree_get_base_commit_id(worktree)) == 0)
5032 if (strncmp(refname, "refs/heads/", 11) == 0)
5034 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5037 refstr = got_ref_to_str(ref);
5039 return got_error_from_errno("got_ref_to_str");
5041 printf("%s%s: %s\n", marker, refname, refstr);
5046 static const struct got_error *
5047 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5049 const char *refname;
5051 if (worktree == NULL)
5052 return got_error(GOT_ERR_NOT_WORKTREE);
5054 refname = got_worktree_get_head_ref_name(worktree);
5056 if (strncmp(refname, "refs/heads/", 11) == 0)
5058 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5061 printf("%s\n", refname);
5066 static const struct got_error *
5067 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5069 static const struct got_error *err = NULL;
5070 struct got_reflist_head refs;
5071 struct got_reflist_entry *re;
5072 struct got_reference *temp_ref = NULL;
5073 int rebase_in_progress, histedit_in_progress;
5075 SIMPLEQ_INIT(&refs);
5078 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5083 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5088 if (rebase_in_progress || histedit_in_progress) {
5089 err = got_ref_open(&temp_ref, repo,
5090 got_worktree_get_head_ref_name(worktree), 0);
5093 list_branch(repo, worktree, temp_ref);
5094 got_ref_close(temp_ref);
5098 err = got_ref_list(&refs, repo, "refs/heads",
5099 got_ref_cmp_by_name, NULL);
5103 SIMPLEQ_FOREACH(re, &refs, entry)
5104 list_branch(repo, worktree, re->ref);
5106 got_ref_list_free(&refs);
5110 static const struct got_error *
5111 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5112 const char *branch_name)
5114 const struct got_error *err = NULL;
5115 struct got_reference *ref = NULL;
5118 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5119 return got_error_from_errno("asprintf");
5121 err = got_ref_open(&ref, repo, refname, 0);
5126 strcmp(got_worktree_get_head_ref_name(worktree),
5127 got_ref_get_name(ref)) == 0) {
5128 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5129 "will not delete this work tree's current branch");
5133 err = got_ref_delete(ref, repo);
5141 static const struct got_error *
5142 add_branch(struct got_repository *repo, const char *branch_name,
5143 struct got_object_id *base_commit_id)
5145 const struct got_error *err = NULL;
5146 struct got_reference *ref = NULL;
5147 char *base_refname = NULL, *refname = NULL;
5150 * Don't let the user create a branch name with a leading '-'.
5151 * While technically a valid reference name, this case is usually
5152 * an unintended typo.
5154 if (branch_name[0] == '-')
5155 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5157 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5158 err = got_error_from_errno("asprintf");
5162 err = got_ref_open(&ref, repo, refname, 0);
5164 err = got_error(GOT_ERR_BRANCH_EXISTS);
5166 } else if (err->code != GOT_ERR_NOT_REF)
5169 err = got_ref_alloc(&ref, refname, base_commit_id);
5173 err = got_ref_write(ref, repo);
5182 static const struct got_error *
5183 cmd_branch(int argc, char *argv[])
5185 const struct got_error *error = NULL;
5186 struct got_repository *repo = NULL;
5187 struct got_worktree *worktree = NULL;
5188 char *cwd = NULL, *repo_path = NULL;
5189 int ch, do_list = 0, do_show = 0, do_update = 1;
5190 const char *delref = NULL, *commit_id_arg = NULL;
5191 struct got_reference *ref = NULL;
5192 struct got_pathlist_head paths;
5193 struct got_pathlist_entry *pe;
5194 struct got_object_id *commit_id = NULL;
5195 char *commit_id_str = NULL;
5199 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5202 commit_id_arg = optarg;
5208 repo_path = realpath(optarg, NULL);
5209 if (repo_path == NULL)
5210 return got_error_from_errno2("realpath",
5212 got_path_strip_trailing_slashes(repo_path);
5226 if (do_list && delref)
5227 errx(1, "-l and -d options are mutually exclusive");
5232 if (!do_list && !delref && argc == 0)
5235 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5236 errx(1, "-c option can only be used when creating a branch");
5238 if (do_list || delref) {
5241 } else if (!do_show && argc != 1)
5245 if (do_list || do_show) {
5246 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5250 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5251 "sendfd unveil", NULL) == -1)
5255 cwd = getcwd(NULL, 0);
5257 error = got_error_from_errno("getcwd");
5261 if (repo_path == NULL) {
5262 error = got_worktree_open(&worktree, cwd);
5263 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5269 strdup(got_worktree_get_repo_path(worktree));
5270 if (repo_path == NULL)
5271 error = got_error_from_errno("strdup");
5275 repo_path = strdup(cwd);
5276 if (repo_path == NULL) {
5277 error = got_error_from_errno("strdup");
5283 error = got_repo_open(&repo, repo_path, NULL);
5287 error = apply_unveil(got_repo_get_path(repo), do_list,
5288 worktree ? got_worktree_get_root_path(worktree) : NULL);
5293 error = show_current_branch(repo, worktree);
5295 error = list_branches(repo, worktree);
5297 error = delete_branch(repo, worktree, delref);
5299 if (commit_id_arg == NULL)
5300 commit_id_arg = worktree ?
5301 got_worktree_get_head_ref_name(worktree) :
5303 error = got_repo_match_object_id(&commit_id, NULL,
5304 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5307 error = add_branch(repo, argv[0], commit_id);
5310 if (worktree && do_update) {
5311 struct got_update_progress_arg upa;
5312 char *branch_refname = NULL;
5314 error = got_object_id_str(&commit_id_str, commit_id);
5317 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5321 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5323 error = got_error_from_errno("asprintf");
5326 error = got_ref_open(&ref, repo, branch_refname, 0);
5327 free(branch_refname);
5330 error = switch_head_ref(ref, commit_id, worktree,
5334 error = got_worktree_set_base_commit_id(worktree, repo,
5338 memset(&upa, 0, sizeof(upa));
5339 error = got_worktree_checkout_files(worktree, &paths,
5340 repo, update_progress, &upa, check_cancelled,
5344 if (upa.did_something)
5345 printf("Updated to commit %s\n", commit_id_str);
5346 print_update_progress_stats(&upa);
5353 got_repo_close(repo);
5355 got_worktree_close(worktree);
5359 free(commit_id_str);
5360 TAILQ_FOREACH(pe, &paths, entry)
5361 free((char *)pe->path);
5362 got_pathlist_free(&paths);
5371 "usage: %s tag [-c commit] [-r repository] [-l] "
5372 "[-m message] name\n", getprogname());
5377 static const struct got_error *
5378 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5380 const struct got_error *err = NULL;
5381 struct got_reflist_entry *re, *se, *new;
5382 struct got_object_id *re_id, *se_id;
5383 struct got_tag_object *re_tag, *se_tag;
5384 time_t re_time, se_time;
5386 SIMPLEQ_FOREACH(re, tags, entry) {
5387 se = SIMPLEQ_FIRST(sorted);
5389 err = got_reflist_entry_dup(&new, re);
5392 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5395 err = got_ref_resolve(&re_id, repo, re->ref);
5398 err = got_object_open_as_tag(&re_tag, repo, re_id);
5402 re_time = got_object_tag_get_tagger_time(re_tag);
5403 got_object_tag_close(re_tag);
5407 err = got_ref_resolve(&se_id, repo, re->ref);
5410 err = got_object_open_as_tag(&se_tag, repo, se_id);
5414 se_time = got_object_tag_get_tagger_time(se_tag);
5415 got_object_tag_close(se_tag);
5417 if (se_time > re_time) {
5418 err = got_reflist_entry_dup(&new, re);
5421 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5424 se = SIMPLEQ_NEXT(se, entry);
5433 static const struct got_error *
5434 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5436 static const struct got_error *err = NULL;
5437 struct got_reflist_head refs;
5438 struct got_reflist_entry *re;
5440 SIMPLEQ_INIT(&refs);
5442 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5446 SIMPLEQ_FOREACH(re, &refs, entry) {
5447 const char *refname;
5448 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5452 struct got_object_id *id;
5453 struct got_tag_object *tag;
5454 struct got_commit_object *commit = NULL;
5456 refname = got_ref_get_name(re->ref);
5457 if (strncmp(refname, "refs/tags/", 10) != 0)
5460 refstr = got_ref_to_str(re->ref);
5461 if (refstr == NULL) {
5462 err = got_error_from_errno("got_ref_to_str");
5465 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5468 err = got_ref_resolve(&id, repo, re->ref);
5471 err = got_object_open_as_tag(&tag, repo, id);
5473 if (err->code != GOT_ERR_OBJ_TYPE) {
5477 /* "lightweight" tag */
5478 err = got_object_open_as_commit(&commit, repo, id);
5483 tagger = got_object_commit_get_committer(commit);
5485 got_object_commit_get_committer_time(commit);
5486 err = got_object_id_str(&id_str, id);
5492 tagger = got_object_tag_get_tagger(tag);
5493 tagger_time = got_object_tag_get_tagger_time(tag);
5494 err = got_object_id_str(&id_str,
5495 got_object_tag_get_object_id(tag));
5499 printf("from: %s\n", tagger);
5500 datestr = get_datestr(&tagger_time, datebuf);
5502 printf("date: %s UTC\n", datestr);
5504 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5506 switch (got_object_tag_get_object_type(tag)) {
5507 case GOT_OBJ_TYPE_BLOB:
5508 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5511 case GOT_OBJ_TYPE_TREE:
5512 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5515 case GOT_OBJ_TYPE_COMMIT:
5516 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5519 case GOT_OBJ_TYPE_TAG:
5520 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5529 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5532 got_object_commit_close(commit);
5534 tagmsg0 = strdup(got_object_tag_get_message(tag));
5535 got_object_tag_close(tag);
5536 if (tagmsg0 == NULL) {
5537 err = got_error_from_errno("strdup");
5544 line = strsep(&tagmsg, "\n");
5546 printf(" %s\n", line);
5551 got_ref_list_free(&refs);
5555 static const struct got_error *
5556 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5557 const char *tag_name, const char *repo_path)
5559 const struct got_error *err = NULL;
5560 char *template = NULL, *initial_content = NULL;
5561 char *editor = NULL;
5564 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5565 err = got_error_from_errno("asprintf");
5569 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5570 commit_id_str, tag_name) == -1) {
5571 err = got_error_from_errno("asprintf");
5575 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5579 dprintf(fd, initial_content);
5582 err = get_editor(&editor);
5585 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5587 free(initial_content);
5591 /* Editor is done; we can now apply unveil(2) */
5593 err = apply_unveil(repo_path, 0, NULL);
5602 static const struct got_error *
5603 add_tag(struct got_repository *repo, const char *tag_name,
5604 const char *commit_arg, const char *tagmsg_arg)
5606 const struct got_error *err = NULL;
5607 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5608 char *label = NULL, *commit_id_str = NULL;
5609 struct got_reference *ref = NULL;
5610 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5611 char *tagmsg_path = NULL, *tag_id_str = NULL;
5612 int preserve_tagmsg = 0;
5615 * Don't let the user create a tag name with a leading '-'.
5616 * While technically a valid reference name, this case is usually
5617 * an unintended typo.
5619 if (tag_name[0] == '-')
5620 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5622 err = get_author(&tagger, repo);
5626 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5627 GOT_OBJ_TYPE_COMMIT, 1, repo);
5631 err = got_object_id_str(&commit_id_str, commit_id);
5635 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5636 refname = strdup(tag_name);
5637 if (refname == NULL) {
5638 err = got_error_from_errno("strdup");
5642 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5643 err = got_error_from_errno("asprintf");
5647 err = got_ref_open(&ref, repo, refname, 0);
5649 err = got_error(GOT_ERR_TAG_EXISTS);
5651 } else if (err->code != GOT_ERR_NOT_REF)
5654 if (tagmsg_arg == NULL) {
5655 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5656 tag_name, got_repo_get_path(repo));
5658 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5659 tagmsg_path != NULL)
5660 preserve_tagmsg = 1;
5665 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5666 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5669 preserve_tagmsg = 1;
5673 err = got_ref_alloc(&ref, refname, tag_id);
5676 preserve_tagmsg = 1;
5680 err = got_ref_write(ref, repo);
5683 preserve_tagmsg = 1;
5687 err = got_object_id_str(&tag_id_str, tag_id);
5690 preserve_tagmsg = 1;
5693 printf("Created tag %s\n", tag_id_str);
5695 if (preserve_tagmsg) {
5696 fprintf(stderr, "%s: tag message preserved in %s\n",
5697 getprogname(), tagmsg_path);
5698 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5699 err = got_error_from_errno2("unlink", tagmsg_path);
5704 free(commit_id_str);
5712 static const struct got_error *
5713 cmd_tag(int argc, char *argv[])
5715 const struct got_error *error = NULL;
5716 struct got_repository *repo = NULL;
5717 struct got_worktree *worktree = NULL;
5718 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5719 char *gitconfig_path = NULL;
5720 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5721 int ch, do_list = 0;
5723 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5726 commit_id_arg = optarg;
5732 repo_path = realpath(optarg, NULL);
5733 if (repo_path == NULL)
5734 return got_error_from_errno2("realpath",
5736 got_path_strip_trailing_slashes(repo_path);
5751 if (commit_id_arg != NULL)
5753 "-c option can only be used when creating a tag");
5755 errx(1, "-l and -m options are mutually exclusive");
5758 } else if (argc != 1)
5765 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5769 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5770 "sendfd unveil", NULL) == -1)
5774 cwd = getcwd(NULL, 0);
5776 error = got_error_from_errno("getcwd");
5780 if (repo_path == NULL) {
5781 error = got_worktree_open(&worktree, cwd);
5782 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5788 strdup(got_worktree_get_repo_path(worktree));
5789 if (repo_path == NULL)
5790 error = got_error_from_errno("strdup");
5794 repo_path = strdup(cwd);
5795 if (repo_path == NULL) {
5796 error = got_error_from_errno("strdup");
5803 error = got_repo_open(&repo, repo_path, NULL);
5806 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5809 error = list_tags(repo, worktree);
5811 error = get_gitconfig_path(&gitconfig_path);
5814 error = got_repo_open(&repo, repo_path, gitconfig_path);
5819 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5824 if (commit_id_arg == NULL) {
5825 struct got_reference *head_ref;
5826 struct got_object_id *commit_id;
5827 error = got_ref_open(&head_ref, repo,
5828 worktree ? got_worktree_get_head_ref_name(worktree)
5832 error = got_ref_resolve(&commit_id, repo, head_ref);
5833 got_ref_close(head_ref);
5836 error = got_object_id_str(&commit_id_str, commit_id);
5842 error = add_tag(repo, tag_name,
5843 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5847 got_repo_close(repo);
5849 got_worktree_close(worktree);
5852 free(gitconfig_path);
5853 free(commit_id_str);
5860 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5865 static const struct got_error *
5866 add_progress(void *arg, unsigned char status, const char *path)
5868 while (path[0] == '/')
5870 printf("%c %s\n", status, path);
5874 static const struct got_error *
5875 cmd_add(int argc, char *argv[])
5877 const struct got_error *error = NULL;
5878 struct got_repository *repo = NULL;
5879 struct got_worktree *worktree = NULL;
5881 struct got_pathlist_head paths;
5882 struct got_pathlist_entry *pe;
5883 int ch, can_recurse = 0, no_ignores = 0;
5887 while ((ch = getopt(argc, argv, "IR")) != -1) {
5905 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5912 cwd = getcwd(NULL, 0);
5914 error = got_error_from_errno("getcwd");
5918 error = got_worktree_open(&worktree, cwd);
5920 if (error->code == GOT_ERR_NOT_WORKTREE)
5921 error = wrap_not_worktree_error(error, "add", cwd);
5925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5930 error = apply_unveil(got_repo_get_path(repo), 1,
5931 got_worktree_get_root_path(worktree));
5935 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5939 if (!can_recurse && no_ignores) {
5940 error = got_error_msg(GOT_ERR_BAD_PATH,
5941 "disregarding ignores requires -R option");
5949 TAILQ_FOREACH(pe, &paths, entry) {
5950 if (asprintf(&ondisk_path, "%s/%s",
5951 got_worktree_get_root_path(worktree),
5953 error = got_error_from_errno("asprintf");
5956 if (lstat(ondisk_path, &sb) == -1) {
5957 if (errno == ENOENT) {
5961 error = got_error_from_errno2("lstat",
5967 if (S_ISDIR(sb.st_mode)) {
5968 error = got_error_msg(GOT_ERR_BAD_PATH,
5969 "adding directories requires -R option");
5975 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5976 NULL, repo, no_ignores);
5979 got_repo_close(repo);
5981 got_worktree_close(worktree);
5982 TAILQ_FOREACH(pe, &paths, entry)
5983 free((char *)pe->path);
5984 got_pathlist_free(&paths);
5992 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5997 static const struct got_error *
5998 print_remove_status(void *arg, unsigned char status,
5999 unsigned char staged_status, const char *path)
6001 while (path[0] == '/')
6003 if (status == GOT_STATUS_NONEXISTENT)
6005 if (status == staged_status && (status == GOT_STATUS_DELETE))
6006 status = GOT_STATUS_NO_CHANGE;
6007 printf("%c%c %s\n", status, staged_status, path);
6011 static const struct got_error *
6012 cmd_remove(int argc, char *argv[])
6014 const struct got_error *error = NULL;
6015 struct got_worktree *worktree = NULL;
6016 struct got_repository *repo = NULL;
6018 struct got_pathlist_head paths;
6019 struct got_pathlist_entry *pe;
6020 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
6024 while ((ch = getopt(argc, argv, "fkR")) != -1) {
6027 delete_local_mods = 1;
6045 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6052 cwd = getcwd(NULL, 0);
6054 error = got_error_from_errno("getcwd");
6057 error = got_worktree_open(&worktree, cwd);
6059 if (error->code == GOT_ERR_NOT_WORKTREE)
6060 error = wrap_not_worktree_error(error, "remove", cwd);
6064 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6069 error = apply_unveil(got_repo_get_path(repo), 1,
6070 got_worktree_get_root_path(worktree));
6074 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6081 TAILQ_FOREACH(pe, &paths, entry) {
6082 if (asprintf(&ondisk_path, "%s/%s",
6083 got_worktree_get_root_path(worktree),
6085 error = got_error_from_errno("asprintf");
6088 if (lstat(ondisk_path, &sb) == -1) {
6089 if (errno == ENOENT) {
6093 error = got_error_from_errno2("lstat",
6099 if (S_ISDIR(sb.st_mode)) {
6100 error = got_error_msg(GOT_ERR_BAD_PATH,
6101 "removing directories requires -R option");
6107 error = got_worktree_schedule_delete(worktree, &paths,
6108 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6111 got_repo_close(repo);
6113 got_worktree_close(worktree);
6114 TAILQ_FOREACH(pe, &paths, entry)
6115 free((char *)pe->path);
6116 got_pathlist_free(&paths);
6124 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6125 "path ...\n", getprogname());
6129 static const struct got_error *
6130 revert_progress(void *arg, unsigned char status, const char *path)
6132 if (status == GOT_STATUS_UNVERSIONED)
6135 while (path[0] == '/')
6137 printf("%c %s\n", status, path);
6141 struct choose_patch_arg {
6142 FILE *patch_script_file;
6146 static const struct got_error *
6147 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6148 int nchanges, const char *action)
6151 size_t linesize = 0;
6155 case GOT_STATUS_ADD:
6156 printf("A %s\n%s this addition? [y/n] ", path, action);
6158 case GOT_STATUS_DELETE:
6159 printf("D %s\n%s this deletion? [y/n] ", path, action);
6161 case GOT_STATUS_MODIFY:
6162 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6163 return got_error_from_errno("fseek");
6164 printf(GOT_COMMIT_SEP_STR);
6165 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6167 if (ferror(patch_file))
6168 return got_error_from_errno("getline");
6169 printf(GOT_COMMIT_SEP_STR);
6170 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6171 path, n, nchanges, action);
6174 return got_error_path(path, GOT_ERR_FILE_STATUS);
6180 static const struct got_error *
6181 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6182 FILE *patch_file, int n, int nchanges)
6184 const struct got_error *err = NULL;
6186 size_t linesize = 0;
6189 struct choose_patch_arg *a = arg;
6191 *choice = GOT_PATCH_CHOICE_NONE;
6193 if (a->patch_script_file) {
6195 err = show_change(status, path, patch_file, n, nchanges,
6199 linelen = getline(&line, &linesize, a->patch_script_file);
6200 if (linelen == -1) {
6201 if (ferror(a->patch_script_file))
6202 return got_error_from_errno("getline");
6205 nl = strchr(line, '\n');
6208 if (strcmp(line, "y") == 0) {
6209 *choice = GOT_PATCH_CHOICE_YES;
6211 } else if (strcmp(line, "n") == 0) {
6212 *choice = GOT_PATCH_CHOICE_NO;
6214 } else if (strcmp(line, "q") == 0 &&
6215 status == GOT_STATUS_MODIFY) {
6216 *choice = GOT_PATCH_CHOICE_QUIT;
6219 printf("invalid response '%s'\n", line);
6224 while (resp != 'y' && resp != 'n' && resp != 'q') {
6225 err = show_change(status, path, patch_file, n, nchanges,
6232 if (status == GOT_STATUS_MODIFY) {
6233 if (resp != 'y' && resp != 'n' && resp != 'q') {
6234 printf("invalid response '%c'\n", resp);
6237 } else if (resp != 'y' && resp != 'n') {
6238 printf("invalid response '%c'\n", resp);
6244 *choice = GOT_PATCH_CHOICE_YES;
6245 else if (resp == 'n')
6246 *choice = GOT_PATCH_CHOICE_NO;
6247 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6248 *choice = GOT_PATCH_CHOICE_QUIT;
6254 static const struct got_error *
6255 cmd_revert(int argc, char *argv[])
6257 const struct got_error *error = NULL;
6258 struct got_worktree *worktree = NULL;
6259 struct got_repository *repo = NULL;
6260 char *cwd = NULL, *path = NULL;
6261 struct got_pathlist_head paths;
6262 struct got_pathlist_entry *pe;
6263 int ch, can_recurse = 0, pflag = 0;
6264 FILE *patch_script_file = NULL;
6265 const char *patch_script_path = NULL;
6266 struct choose_patch_arg cpa;
6270 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6276 patch_script_path = optarg;
6291 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6292 "unveil", NULL) == -1)
6297 if (patch_script_path && !pflag)
6298 errx(1, "-F option can only be used together with -p option");
6300 cwd = getcwd(NULL, 0);
6302 error = got_error_from_errno("getcwd");
6305 error = got_worktree_open(&worktree, cwd);
6307 if (error->code == GOT_ERR_NOT_WORKTREE)
6308 error = wrap_not_worktree_error(error, "revert", cwd);
6312 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6317 if (patch_script_path) {
6318 patch_script_file = fopen(patch_script_path, "r");
6319 if (patch_script_file == NULL) {
6320 error = got_error_from_errno2("fopen",
6325 error = apply_unveil(got_repo_get_path(repo), 1,
6326 got_worktree_get_root_path(worktree));
6330 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6337 TAILQ_FOREACH(pe, &paths, entry) {
6338 if (asprintf(&ondisk_path, "%s/%s",
6339 got_worktree_get_root_path(worktree),
6341 error = got_error_from_errno("asprintf");
6344 if (lstat(ondisk_path, &sb) == -1) {
6345 if (errno == ENOENT) {
6349 error = got_error_from_errno2("lstat",
6355 if (S_ISDIR(sb.st_mode)) {
6356 error = got_error_msg(GOT_ERR_BAD_PATH,
6357 "reverting directories requires -R option");
6363 cpa.patch_script_file = patch_script_file;
6364 cpa.action = "revert";
6365 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6366 pflag ? choose_patch : NULL, &cpa, repo);
6368 if (patch_script_file && fclose(patch_script_file) == EOF &&
6370 error = got_error_from_errno2("fclose", patch_script_path);
6372 got_repo_close(repo);
6374 got_worktree_close(worktree);
6383 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6388 struct collect_commit_logmsg_arg {
6389 const char *cmdline_log;
6391 const char *worktree_path;
6392 const char *branch_name;
6393 const char *repo_path;
6398 static const struct got_error *
6399 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6402 char *initial_content = NULL;
6403 struct got_pathlist_entry *pe;
6404 const struct got_error *err = NULL;
6405 char *template = NULL;
6406 struct collect_commit_logmsg_arg *a = arg;
6410 /* if a message was specified on the command line, just use it */
6411 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6412 len = strlen(a->cmdline_log) + 1;
6413 *logmsg = malloc(len + 1);
6414 if (*logmsg == NULL)
6415 return got_error_from_errno("malloc");
6416 strlcpy(*logmsg, a->cmdline_log, len);
6420 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6421 return got_error_from_errno("asprintf");
6423 if (asprintf(&initial_content,
6424 "\n# changes to be committed on branch %s:\n",
6425 a->branch_name) == -1)
6426 return got_error_from_errno("asprintf");
6428 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6432 dprintf(fd, initial_content);
6434 TAILQ_FOREACH(pe, commitable_paths, entry) {
6435 struct got_commitable *ct = pe->data;
6436 dprintf(fd, "# %c %s\n",
6437 got_commitable_get_status(ct),
6438 got_commitable_get_path(ct));
6442 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6444 free(initial_content);
6447 /* Editor is done; we can now apply unveil(2) */
6449 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6458 static const struct got_error *
6459 cmd_commit(int argc, char *argv[])
6461 const struct got_error *error = NULL;
6462 struct got_worktree *worktree = NULL;
6463 struct got_repository *repo = NULL;
6464 char *cwd = NULL, *id_str = NULL;
6465 struct got_object_id *id = NULL;
6466 const char *logmsg = NULL;
6467 struct collect_commit_logmsg_arg cl_arg;
6468 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6469 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6470 int allow_bad_symlinks = 0;
6471 struct got_pathlist_head paths;
6474 cl_arg.logmsg_path = NULL;
6476 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6482 allow_bad_symlinks = 1;
6494 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6495 "unveil", NULL) == -1)
6498 cwd = getcwd(NULL, 0);
6500 error = got_error_from_errno("getcwd");
6503 error = got_worktree_open(&worktree, cwd);
6505 if (error->code == GOT_ERR_NOT_WORKTREE)
6506 error = wrap_not_worktree_error(error, "commit", cwd);
6510 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6513 if (rebase_in_progress) {
6514 error = got_error(GOT_ERR_REBASING);
6518 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6523 error = get_gitconfig_path(&gitconfig_path);
6526 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6531 error = get_author(&author, repo);
6536 * unveil(2) traverses exec(2); if an editor is used we have
6537 * to apply unveil after the log message has been written.
6539 if (logmsg == NULL || strlen(logmsg) == 0)
6540 error = get_editor(&editor);
6542 error = apply_unveil(got_repo_get_path(repo), 0,
6543 got_worktree_get_root_path(worktree));
6547 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6551 cl_arg.editor = editor;
6552 cl_arg.cmdline_log = logmsg;
6553 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6554 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6555 if (!histedit_in_progress) {
6556 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6557 error = got_error(GOT_ERR_COMMIT_BRANCH);
6560 cl_arg.branch_name += 11;
6562 cl_arg.repo_path = got_repo_get_path(repo);
6563 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6564 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6565 print_status, NULL, repo);
6567 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6568 cl_arg.logmsg_path != NULL)
6569 preserve_logmsg = 1;
6573 error = got_object_id_str(&id_str, id);
6576 printf("Created commit %s\n", id_str);
6578 if (preserve_logmsg) {
6579 fprintf(stderr, "%s: log message preserved in %s\n",
6580 getprogname(), cl_arg.logmsg_path);
6581 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6583 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6584 free(cl_arg.logmsg_path);
6586 got_repo_close(repo);
6588 got_worktree_close(worktree);
6591 free(gitconfig_path);
6598 usage_cherrypick(void)
6600 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6604 static const struct got_error *
6605 cmd_cherrypick(int argc, char *argv[])
6607 const struct got_error *error = NULL;
6608 struct got_worktree *worktree = NULL;
6609 struct got_repository *repo = NULL;
6610 char *cwd = NULL, *commit_id_str = NULL;
6611 struct got_object_id *commit_id = NULL;
6612 struct got_commit_object *commit = NULL;
6613 struct got_object_qid *pid;
6614 struct got_reference *head_ref = NULL;
6616 struct got_update_progress_arg upa;
6618 while ((ch = getopt(argc, argv, "")) != -1) {
6630 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6631 "unveil", NULL) == -1)
6637 cwd = getcwd(NULL, 0);
6639 error = got_error_from_errno("getcwd");
6642 error = got_worktree_open(&worktree, cwd);
6644 if (error->code == GOT_ERR_NOT_WORKTREE)
6645 error = wrap_not_worktree_error(error, "cherrypick",
6650 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6655 error = apply_unveil(got_repo_get_path(repo), 0,
6656 got_worktree_get_root_path(worktree));
6660 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6661 GOT_OBJ_TYPE_COMMIT, repo);
6662 if (error != NULL) {
6663 struct got_reference *ref;
6664 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6666 error = got_ref_open(&ref, repo, argv[0], 0);
6669 error = got_ref_resolve(&commit_id, repo, ref);
6674 error = got_object_id_str(&commit_id_str, commit_id);
6678 error = got_ref_open(&head_ref, repo,
6679 got_worktree_get_head_ref_name(worktree), 0);
6683 error = check_same_branch(commit_id, head_ref, NULL, repo);
6685 if (error->code != GOT_ERR_ANCESTRY)
6689 error = got_error(GOT_ERR_SAME_BRANCH);
6693 error = got_object_open_as_commit(&commit, repo, commit_id);
6696 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6697 memset(&upa, 0, sizeof(upa));
6698 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6699 commit_id, repo, update_progress, &upa, check_cancelled,
6704 if (upa.did_something)
6705 printf("Merged commit %s\n", commit_id_str);
6706 print_update_progress_stats(&upa);
6709 got_object_commit_close(commit);
6710 free(commit_id_str);
6712 got_ref_close(head_ref);
6714 got_worktree_close(worktree);
6716 got_repo_close(repo);
6723 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6727 static const struct got_error *
6728 cmd_backout(int argc, char *argv[])
6730 const struct got_error *error = NULL;
6731 struct got_worktree *worktree = NULL;
6732 struct got_repository *repo = NULL;
6733 char *cwd = NULL, *commit_id_str = NULL;
6734 struct got_object_id *commit_id = NULL;
6735 struct got_commit_object *commit = NULL;
6736 struct got_object_qid *pid;
6737 struct got_reference *head_ref = NULL;
6739 struct got_update_progress_arg upa;
6741 while ((ch = getopt(argc, argv, "")) != -1) {
6753 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6754 "unveil", NULL) == -1)
6760 cwd = getcwd(NULL, 0);
6762 error = got_error_from_errno("getcwd");
6765 error = got_worktree_open(&worktree, cwd);
6767 if (error->code == GOT_ERR_NOT_WORKTREE)
6768 error = wrap_not_worktree_error(error, "backout", cwd);
6772 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6777 error = apply_unveil(got_repo_get_path(repo), 0,
6778 got_worktree_get_root_path(worktree));
6782 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6783 GOT_OBJ_TYPE_COMMIT, repo);
6784 if (error != NULL) {
6785 struct got_reference *ref;
6786 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6788 error = got_ref_open(&ref, repo, argv[0], 0);
6791 error = got_ref_resolve(&commit_id, repo, ref);
6796 error = got_object_id_str(&commit_id_str, commit_id);
6800 error = got_ref_open(&head_ref, repo,
6801 got_worktree_get_head_ref_name(worktree), 0);
6805 error = check_same_branch(commit_id, head_ref, NULL, repo);
6809 error = got_object_open_as_commit(&commit, repo, commit_id);
6812 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6814 error = got_error(GOT_ERR_ROOT_COMMIT);
6818 memset(&upa, 0, sizeof(upa));
6819 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6820 update_progress, &upa, check_cancelled, NULL);
6824 if (upa.did_something)
6825 printf("Backed out commit %s\n", commit_id_str);
6826 print_update_progress_stats(&upa);
6829 got_object_commit_close(commit);
6830 free(commit_id_str);
6832 got_ref_close(head_ref);
6834 got_worktree_close(worktree);
6836 got_repo_close(repo);
6843 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6849 trim_logmsg(char *logmsg, int limit)
6854 len = strlen(logmsg);
6858 nl = strchr(logmsg, '\n');
6863 static const struct got_error *
6864 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6866 const struct got_error *err;
6867 char *logmsg0 = NULL;
6870 err = got_object_commit_get_logmsg(&logmsg0, commit);
6875 while (isspace((unsigned char)s[0]))
6878 *logmsg = strdup(s);
6879 if (*logmsg == NULL) {
6880 err = got_error_from_errno("strdup");
6884 trim_logmsg(*logmsg, limit);
6890 static const struct got_error *
6891 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6893 const struct got_error *err;
6894 struct got_commit_object *commit = NULL;
6895 char *id_str = NULL, *logmsg = NULL;
6897 err = got_object_open_as_commit(&commit, repo, id);
6901 err = got_object_id_str(&id_str, id);
6907 err = get_short_logmsg(&logmsg, 42, commit);
6911 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6914 got_object_commit_close(commit);
6919 static const struct got_error *
6920 show_rebase_progress(struct got_commit_object *commit,
6921 struct got_object_id *old_id, struct got_object_id *new_id)
6923 const struct got_error *err;
6924 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6926 err = got_object_id_str(&old_id_str, old_id);
6931 err = got_object_id_str(&new_id_str, new_id);
6936 old_id_str[12] = '\0';
6938 new_id_str[12] = '\0';
6940 err = get_short_logmsg(&logmsg, 42, commit);
6944 printf("%s -> %s: %s\n", old_id_str,
6945 new_id_str ? new_id_str : "no-op change", logmsg);
6953 static const struct got_error *
6954 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6955 struct got_reference *branch, struct got_reference *new_base_branch,
6956 struct got_reference *tmp_branch, struct got_repository *repo)
6958 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6959 return got_worktree_rebase_complete(worktree, fileindex,
6960 new_base_branch, tmp_branch, branch, repo);
6963 static const struct got_error *
6964 rebase_commit(struct got_pathlist_head *merged_paths,
6965 struct got_worktree *worktree, struct got_fileindex *fileindex,
6966 struct got_reference *tmp_branch,
6967 struct got_object_id *commit_id, struct got_repository *repo)
6969 const struct got_error *error;
6970 struct got_commit_object *commit;
6971 struct got_object_id *new_commit_id;
6973 error = got_object_open_as_commit(&commit, repo, commit_id);
6977 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6978 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6980 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6982 error = show_rebase_progress(commit, commit_id, NULL);
6984 error = show_rebase_progress(commit, commit_id, new_commit_id);
6985 free(new_commit_id);
6988 got_object_commit_close(commit);
6992 struct check_path_prefix_arg {
6993 const char *path_prefix;
6998 static const struct got_error *
6999 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7000 struct got_blob_object *blob2, struct got_object_id *id1,
7001 struct got_object_id *id2, const char *path1, const char *path2,
7002 mode_t mode1, mode_t mode2, struct got_repository *repo)
7004 struct check_path_prefix_arg *a = arg;
7006 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7007 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7008 return got_error(a->errcode);
7013 static const struct got_error *
7014 check_path_prefix(struct got_object_id *parent_id,
7015 struct got_object_id *commit_id, const char *path_prefix,
7016 int errcode, struct got_repository *repo)
7018 const struct got_error *err;
7019 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7020 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7021 struct check_path_prefix_arg cpp_arg;
7023 if (got_path_is_root_dir(path_prefix))
7026 err = got_object_open_as_commit(&commit, repo, commit_id);
7030 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7034 err = got_object_open_as_tree(&tree1, repo,
7035 got_object_commit_get_tree_id(parent_commit));
7039 err = got_object_open_as_tree(&tree2, repo,
7040 got_object_commit_get_tree_id(commit));
7044 cpp_arg.path_prefix = path_prefix;
7045 while (cpp_arg.path_prefix[0] == '/')
7046 cpp_arg.path_prefix++;
7047 cpp_arg.len = strlen(cpp_arg.path_prefix);
7048 cpp_arg.errcode = errcode;
7049 err = got_diff_tree(tree1, tree2, "", "", repo,
7050 check_path_prefix_in_diff, &cpp_arg, 0);
7053 got_object_tree_close(tree1);
7055 got_object_tree_close(tree2);
7057 got_object_commit_close(commit);
7059 got_object_commit_close(parent_commit);
7063 static const struct got_error *
7064 collect_commits(struct got_object_id_queue *commits,
7065 struct got_object_id *initial_commit_id,
7066 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7067 const char *path_prefix, int path_prefix_errcode,
7068 struct got_repository *repo)
7070 const struct got_error *err = NULL;
7071 struct got_commit_graph *graph = NULL;
7072 struct got_object_id *parent_id = NULL;
7073 struct got_object_qid *qid;
7074 struct got_object_id *commit_id = initial_commit_id;
7076 err = got_commit_graph_open(&graph, "/", 1);
7080 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7081 check_cancelled, NULL);
7084 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7085 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7086 check_cancelled, NULL);
7088 if (err->code == GOT_ERR_ITER_COMPLETED) {
7089 err = got_error_msg(GOT_ERR_ANCESTRY,
7090 "ran out of commits to rebase before "
7091 "youngest common ancestor commit has "
7096 err = check_path_prefix(parent_id, commit_id,
7097 path_prefix, path_prefix_errcode, repo);
7101 err = got_object_qid_alloc(&qid, commit_id);
7104 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7105 commit_id = parent_id;
7109 got_commit_graph_close(graph);
7113 static const struct got_error *
7114 cmd_rebase(int argc, char *argv[])
7116 const struct got_error *error = NULL;
7117 struct got_worktree *worktree = NULL;
7118 struct got_repository *repo = NULL;
7119 struct got_fileindex *fileindex = NULL;
7121 struct got_reference *branch = NULL;
7122 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7123 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7124 struct got_object_id *resume_commit_id = NULL;
7125 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7126 struct got_commit_object *commit = NULL;
7127 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7128 int histedit_in_progress = 0;
7129 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7130 struct got_object_id_queue commits;
7131 struct got_pathlist_head merged_paths;
7132 const struct got_object_id_queue *parent_ids;
7133 struct got_object_qid *qid, *pid;
7135 SIMPLEQ_INIT(&commits);
7136 TAILQ_INIT(&merged_paths);
7138 while ((ch = getopt(argc, argv, "ac")) != -1) {
7144 continue_rebase = 1;
7156 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7157 "unveil", NULL) == -1)
7160 if (abort_rebase && continue_rebase)
7162 else if (abort_rebase || continue_rebase) {
7165 } else if (argc != 1)
7168 cwd = getcwd(NULL, 0);
7170 error = got_error_from_errno("getcwd");
7173 error = got_worktree_open(&worktree, cwd);
7175 if (error->code == GOT_ERR_NOT_WORKTREE)
7176 error = wrap_not_worktree_error(error, "rebase", cwd);
7180 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7185 error = apply_unveil(got_repo_get_path(repo), 0,
7186 got_worktree_get_root_path(worktree));
7190 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7194 if (histedit_in_progress) {
7195 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7199 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7204 struct got_update_progress_arg upa;
7205 if (!rebase_in_progress) {
7206 error = got_error(GOT_ERR_NOT_REBASING);
7209 error = got_worktree_rebase_continue(&resume_commit_id,
7210 &new_base_branch, &tmp_branch, &branch, &fileindex,
7214 printf("Switching work tree to %s\n",
7215 got_ref_get_symref_target(new_base_branch));
7216 memset(&upa, 0, sizeof(upa));
7217 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7218 new_base_branch, update_progress, &upa);
7221 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7222 print_update_progress_stats(&upa);
7223 goto done; /* nothing else to do */
7226 if (continue_rebase) {
7227 if (!rebase_in_progress) {
7228 error = got_error(GOT_ERR_NOT_REBASING);
7231 error = got_worktree_rebase_continue(&resume_commit_id,
7232 &new_base_branch, &tmp_branch, &branch, &fileindex,
7237 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7238 resume_commit_id, repo);
7242 yca_id = got_object_id_dup(resume_commit_id);
7243 if (yca_id == NULL) {
7244 error = got_error_from_errno("got_object_id_dup");
7248 error = got_ref_open(&branch, repo, argv[0], 0);
7253 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7257 if (!continue_rebase) {
7258 struct got_object_id *base_commit_id;
7260 base_commit_id = got_worktree_get_base_commit_id(worktree);
7261 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7262 base_commit_id, branch_head_commit_id, repo,
7263 check_cancelled, NULL);
7266 if (yca_id == NULL) {
7267 error = got_error_msg(GOT_ERR_ANCESTRY,
7268 "specified branch shares no common ancestry "
7269 "with work tree's branch");
7273 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7275 if (error->code != GOT_ERR_ANCESTRY)
7279 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7280 "specified branch resolves to a commit which "
7281 "is already contained in work tree's branch");
7284 error = got_worktree_rebase_prepare(&new_base_branch,
7285 &tmp_branch, &fileindex, worktree, branch, repo);
7290 commit_id = branch_head_commit_id;
7291 error = got_object_open_as_commit(&commit, repo, commit_id);
7295 parent_ids = got_object_commit_get_parent_ids(commit);
7296 pid = SIMPLEQ_FIRST(parent_ids);
7298 if (!continue_rebase) {
7299 struct got_update_progress_arg upa;
7300 memset(&upa, 0, sizeof(upa));
7301 error = got_worktree_rebase_abort(worktree, fileindex,
7302 repo, new_base_branch, update_progress, &upa);
7305 printf("Rebase of %s aborted\n",
7306 got_ref_get_name(branch));
7307 print_update_progress_stats(&upa);
7310 error = got_error(GOT_ERR_EMPTY_REBASE);
7313 error = collect_commits(&commits, commit_id, pid->id,
7314 yca_id, got_worktree_get_path_prefix(worktree),
7315 GOT_ERR_REBASE_PATH, repo);
7316 got_object_commit_close(commit);
7321 if (SIMPLEQ_EMPTY(&commits)) {
7322 if (continue_rebase) {
7323 error = rebase_complete(worktree, fileindex,
7324 branch, new_base_branch, tmp_branch, repo);
7327 /* Fast-forward the reference of the branch. */
7328 struct got_object_id *new_head_commit_id;
7330 error = got_ref_resolve(&new_head_commit_id, repo,
7334 error = got_object_id_str(&id_str, new_head_commit_id);
7335 printf("Forwarding %s to commit %s\n",
7336 got_ref_get_name(branch), id_str);
7338 error = got_ref_change_ref(branch,
7339 new_head_commit_id);
7346 SIMPLEQ_FOREACH(qid, &commits, entry) {
7347 struct got_update_progress_arg upa;
7349 commit_id = qid->id;
7350 parent_id = pid ? pid->id : yca_id;
7353 memset(&upa, 0, sizeof(upa));
7354 error = got_worktree_rebase_merge_files(&merged_paths,
7355 worktree, fileindex, parent_id, commit_id, repo,
7356 update_progress, &upa, check_cancelled, NULL);
7360 print_update_progress_stats(&upa);
7361 if (upa.conflicts > 0)
7362 rebase_status = GOT_STATUS_CONFLICT;
7364 if (rebase_status == GOT_STATUS_CONFLICT) {
7365 error = show_rebase_merge_conflict(qid->id, repo);
7368 got_worktree_rebase_pathlist_free(&merged_paths);
7372 error = rebase_commit(&merged_paths, worktree, fileindex,
7373 tmp_branch, commit_id, repo);
7374 got_worktree_rebase_pathlist_free(&merged_paths);
7379 if (rebase_status == GOT_STATUS_CONFLICT) {
7380 error = got_worktree_rebase_postpone(worktree, fileindex);
7383 error = got_error_msg(GOT_ERR_CONFLICTS,
7384 "conflicts must be resolved before rebasing can continue");
7386 error = rebase_complete(worktree, fileindex, branch,
7387 new_base_branch, tmp_branch, repo);
7389 got_object_id_queue_free(&commits);
7390 free(branch_head_commit_id);
7391 free(resume_commit_id);
7394 got_object_commit_close(commit);
7396 got_ref_close(branch);
7397 if (new_base_branch)
7398 got_ref_close(new_base_branch);
7400 got_ref_close(tmp_branch);
7402 got_worktree_close(worktree);
7404 got_repo_close(repo);
7409 usage_histedit(void)
7411 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7416 #define GOT_HISTEDIT_PICK 'p'
7417 #define GOT_HISTEDIT_EDIT 'e'
7418 #define GOT_HISTEDIT_FOLD 'f'
7419 #define GOT_HISTEDIT_DROP 'd'
7420 #define GOT_HISTEDIT_MESG 'm'
7422 static struct got_histedit_cmd {
7426 } got_histedit_cmds[] = {
7427 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7428 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7429 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7431 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7432 { GOT_HISTEDIT_MESG, "mesg",
7433 "single-line log message for commit above (open editor if empty)" },
7436 struct got_histedit_list_entry {
7437 TAILQ_ENTRY(got_histedit_list_entry) entry;
7438 struct got_object_id *commit_id;
7439 const struct got_histedit_cmd *cmd;
7442 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7444 static const struct got_error *
7445 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7446 FILE *f, struct got_repository *repo)
7448 const struct got_error *err = NULL;
7449 char *logmsg = NULL, *id_str = NULL;
7450 struct got_commit_object *commit = NULL;
7453 err = got_object_open_as_commit(&commit, repo, commit_id);
7457 err = get_short_logmsg(&logmsg, 34, commit);
7461 err = got_object_id_str(&id_str, commit_id);
7465 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7467 err = got_ferror(f, GOT_ERR_IO);
7470 got_object_commit_close(commit);
7476 static const struct got_error *
7477 histedit_write_commit_list(struct got_object_id_queue *commits,
7478 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7480 const struct got_error *err = NULL;
7481 struct got_object_qid *qid;
7483 if (SIMPLEQ_EMPTY(commits))
7484 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7486 SIMPLEQ_FOREACH(qid, commits, entry) {
7487 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7491 if (edit_logmsg_only) {
7492 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7494 err = got_ferror(f, GOT_ERR_IO);
7503 static const struct got_error *
7504 write_cmd_list(FILE *f, const char *branch_name,
7505 struct got_object_id_queue *commits)
7507 const struct got_error *err = NULL;
7510 struct got_object_qid *qid;
7512 qid = SIMPLEQ_FIRST(commits);
7513 err = got_object_id_str(&id_str, qid->id);
7518 "# Editing the history of branch '%s' starting at\n"
7520 "# Commits will be processed in order from top to "
7521 "bottom of this file.\n", branch_name, id_str);
7523 err = got_ferror(f, GOT_ERR_IO);
7527 n = fprintf(f, "# Available histedit commands:\n");
7529 err = got_ferror(f, GOT_ERR_IO);
7533 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7534 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7535 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7538 err = got_ferror(f, GOT_ERR_IO);
7547 static const struct got_error *
7548 histedit_syntax_error(int lineno)
7550 static char msg[42];
7553 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7555 if (ret == -1 || ret >= sizeof(msg))
7556 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7558 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7561 static const struct got_error *
7562 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7563 char *logmsg, struct got_repository *repo)
7565 const struct got_error *err;
7566 struct got_commit_object *folded_commit = NULL;
7567 char *id_str, *folded_logmsg = NULL;
7569 err = got_object_id_str(&id_str, hle->commit_id);
7573 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7577 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7580 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7581 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7582 folded_logmsg) == -1) {
7583 err = got_error_from_errno("asprintf");
7587 got_object_commit_close(folded_commit);
7589 free(folded_logmsg);
7593 static struct got_histedit_list_entry *
7594 get_folded_commits(struct got_histedit_list_entry *hle)
7596 struct got_histedit_list_entry *prev, *folded = NULL;
7598 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7599 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7600 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7601 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7603 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7609 static const struct got_error *
7610 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7611 struct got_repository *repo)
7613 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7614 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7615 const struct got_error *err = NULL;
7616 struct got_commit_object *commit = NULL;
7618 struct got_histedit_list_entry *folded = NULL;
7620 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7624 folded = get_folded_commits(hle);
7626 while (folded != hle) {
7627 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7628 folded = TAILQ_NEXT(folded, entry);
7631 err = append_folded_commit_msg(&new_msg, folded,
7637 folded = TAILQ_NEXT(folded, entry);
7641 err = got_object_id_str(&id_str, hle->commit_id);
7644 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7647 if (asprintf(&new_msg,
7648 "%s\n# original log message of commit %s: %s",
7649 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7650 err = got_error_from_errno("asprintf");
7656 err = got_object_id_str(&id_str, hle->commit_id);
7660 err = got_opentemp_named_fd(&logmsg_path, &fd,
7661 GOT_TMPDIR_STR "/got-logmsg");
7665 dprintf(fd, logmsg);
7668 err = get_editor(&editor);
7672 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7674 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7676 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7679 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7680 err = got_error_from_errno2("unlink", logmsg_path);
7686 got_object_commit_close(commit);
7690 static const struct got_error *
7691 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7692 FILE *f, struct got_repository *repo)
7694 const struct got_error *err = NULL;
7695 char *line = NULL, *p, *end;
7699 const struct got_histedit_cmd *cmd;
7700 struct got_object_id *commit_id = NULL;
7701 struct got_histedit_list_entry *hle = NULL;
7704 len = getline(&line, &size, f);
7706 const struct got_error *getline_err;
7709 getline_err = got_error_from_errno("getline");
7710 err = got_ferror(f, getline_err->code);
7715 while (isspace((unsigned char)p[0]))
7717 if (p[0] == '#' || p[0] == '\0') {
7723 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7724 cmd = &got_histedit_cmds[i];
7725 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7726 isspace((unsigned char)p[strlen(cmd->name)])) {
7727 p += strlen(cmd->name);
7730 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7735 if (i == nitems(got_histedit_cmds)) {
7736 err = histedit_syntax_error(lineno);
7739 while (isspace((unsigned char)p[0]))
7741 if (cmd->code == GOT_HISTEDIT_MESG) {
7742 if (hle == NULL || hle->logmsg != NULL) {
7743 err = got_error(GOT_ERR_HISTEDIT_CMD);
7747 err = histedit_edit_logmsg(hle, repo);
7751 hle->logmsg = strdup(p);
7752 if (hle->logmsg == NULL) {
7753 err = got_error_from_errno("strdup");
7762 while (end[0] && !isspace((unsigned char)end[0]))
7766 err = got_object_resolve_id_str(&commit_id, repo, p);
7768 /* override error code */
7769 err = histedit_syntax_error(lineno);
7773 hle = malloc(sizeof(*hle));
7775 err = got_error_from_errno("malloc");
7779 hle->commit_id = commit_id;
7784 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7792 static const struct got_error *
7793 histedit_check_script(struct got_histedit_list *histedit_cmds,
7794 struct got_object_id_queue *commits, struct got_repository *repo)
7796 const struct got_error *err = NULL;
7797 struct got_object_qid *qid;
7798 struct got_histedit_list_entry *hle;
7799 static char msg[92];
7802 if (TAILQ_EMPTY(histedit_cmds))
7803 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7804 "histedit script contains no commands");
7805 if (SIMPLEQ_EMPTY(commits))
7806 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7808 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7809 struct got_histedit_list_entry *hle2;
7810 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7813 if (got_object_id_cmp(hle->commit_id,
7814 hle2->commit_id) != 0)
7816 err = got_object_id_str(&id_str, hle->commit_id);
7819 snprintf(msg, sizeof(msg), "commit %s is listed "
7820 "more than once in histedit script", id_str);
7822 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7826 SIMPLEQ_FOREACH(qid, commits, entry) {
7827 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7828 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7832 err = got_object_id_str(&id_str, qid->id);
7835 snprintf(msg, sizeof(msg),
7836 "commit %s missing from histedit script", id_str);
7838 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7842 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7843 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7844 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7845 "last commit in histedit script cannot be folded");
7850 static const struct got_error *
7851 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7852 const char *path, struct got_object_id_queue *commits,
7853 struct got_repository *repo)
7855 const struct got_error *err = NULL;
7859 err = get_editor(&editor);
7863 if (spawn_editor(editor, path) == -1) {
7864 err = got_error_from_errno("failed spawning editor");
7868 f = fopen(path, "r");
7870 err = got_error_from_errno("fopen");
7873 err = histedit_parse_list(histedit_cmds, f, repo);
7877 err = histedit_check_script(histedit_cmds, commits, repo);
7879 if (f && fclose(f) != 0 && err == NULL)
7880 err = got_error_from_errno("fclose");
7885 static const struct got_error *
7886 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7887 struct got_object_id_queue *, const char *, const char *,
7888 struct got_repository *);
7890 static const struct got_error *
7891 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7892 struct got_object_id_queue *commits, const char *branch_name,
7893 int edit_logmsg_only, struct got_repository *repo)
7895 const struct got_error *err;
7899 err = got_opentemp_named(&path, &f, "got-histedit");
7903 err = write_cmd_list(f, branch_name, commits);
7907 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7911 if (edit_logmsg_only) {
7913 err = histedit_parse_list(histedit_cmds, f, repo);
7915 if (fclose(f) != 0) {
7916 err = got_error_from_errno("fclose");
7920 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7922 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7923 err->code != GOT_ERR_HISTEDIT_CMD)
7925 err = histedit_edit_list_retry(histedit_cmds, err,
7926 commits, path, branch_name, repo);
7930 if (f && fclose(f) != 0 && err == NULL)
7931 err = got_error_from_errno("fclose");
7932 if (path && unlink(path) != 0 && err == NULL)
7933 err = got_error_from_errno2("unlink", path);
7938 static const struct got_error *
7939 histedit_save_list(struct got_histedit_list *histedit_cmds,
7940 struct got_worktree *worktree, struct got_repository *repo)
7942 const struct got_error *err = NULL;
7945 struct got_histedit_list_entry *hle;
7946 struct got_commit_object *commit = NULL;
7948 err = got_worktree_get_histedit_script_path(&path, worktree);
7952 f = fopen(path, "w");
7954 err = got_error_from_errno2("fopen", path);
7957 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7958 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7964 int n = fprintf(f, "%c %s\n",
7965 GOT_HISTEDIT_MESG, hle->logmsg);
7967 err = got_ferror(f, GOT_ERR_IO);
7973 if (f && fclose(f) != 0 && err == NULL)
7974 err = got_error_from_errno("fclose");
7977 got_object_commit_close(commit);
7982 histedit_free_list(struct got_histedit_list *histedit_cmds)
7984 struct got_histedit_list_entry *hle;
7986 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7987 TAILQ_REMOVE(histedit_cmds, hle, entry);
7992 static const struct got_error *
7993 histedit_load_list(struct got_histedit_list *histedit_cmds,
7994 const char *path, struct got_repository *repo)
7996 const struct got_error *err = NULL;
7999 f = fopen(path, "r");
8001 err = got_error_from_errno2("fopen", path);
8005 err = histedit_parse_list(histedit_cmds, f, repo);
8007 if (f && fclose(f) != 0 && err == NULL)
8008 err = got_error_from_errno("fclose");
8012 static const struct got_error *
8013 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8014 const struct got_error *edit_err, struct got_object_id_queue *commits,
8015 const char *path, const char *branch_name, struct got_repository *repo)
8017 const struct got_error *err = NULL, *prev_err = edit_err;
8020 while (resp != 'c' && resp != 'r' && resp != 'a') {
8021 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8022 "or (a)bort: ", getprogname(), prev_err->msg);
8027 histedit_free_list(histedit_cmds);
8028 err = histedit_run_editor(histedit_cmds, path, commits,
8031 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8032 err->code != GOT_ERR_HISTEDIT_CMD)
8039 } else if (resp == 'r') {
8040 histedit_free_list(histedit_cmds);
8041 err = histedit_edit_script(histedit_cmds,
8042 commits, branch_name, 0, repo);
8044 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8045 err->code != GOT_ERR_HISTEDIT_CMD)
8052 } else if (resp == 'a') {
8053 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8056 printf("invalid response '%c'\n", resp);
8062 static const struct got_error *
8063 histedit_complete(struct got_worktree *worktree,
8064 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8065 struct got_reference *branch, struct got_repository *repo)
8067 printf("Switching work tree to %s\n",
8068 got_ref_get_symref_target(branch));
8069 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8073 static const struct got_error *
8074 show_histedit_progress(struct got_commit_object *commit,
8075 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8077 const struct got_error *err;
8078 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8080 err = got_object_id_str(&old_id_str, hle->commit_id);
8085 err = got_object_id_str(&new_id_str, new_id);
8090 old_id_str[12] = '\0';
8092 new_id_str[12] = '\0';
8095 logmsg = strdup(hle->logmsg);
8096 if (logmsg == NULL) {
8097 err = got_error_from_errno("strdup");
8100 trim_logmsg(logmsg, 42);
8102 err = get_short_logmsg(&logmsg, 42, commit);
8107 switch (hle->cmd->code) {
8108 case GOT_HISTEDIT_PICK:
8109 case GOT_HISTEDIT_EDIT:
8110 printf("%s -> %s: %s\n", old_id_str,
8111 new_id_str ? new_id_str : "no-op change", logmsg);
8113 case GOT_HISTEDIT_DROP:
8114 case GOT_HISTEDIT_FOLD:
8115 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8127 static const struct got_error *
8128 histedit_commit(struct got_pathlist_head *merged_paths,
8129 struct got_worktree *worktree, struct got_fileindex *fileindex,
8130 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8131 struct got_repository *repo)
8133 const struct got_error *err;
8134 struct got_commit_object *commit;
8135 struct got_object_id *new_commit_id;
8137 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8138 && hle->logmsg == NULL) {
8139 err = histedit_edit_logmsg(hle, repo);
8144 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8148 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8149 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8152 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8154 err = show_histedit_progress(commit, hle, NULL);
8156 err = show_histedit_progress(commit, hle, new_commit_id);
8157 free(new_commit_id);
8160 got_object_commit_close(commit);
8164 static const struct got_error *
8165 histedit_skip_commit(struct got_histedit_list_entry *hle,
8166 struct got_worktree *worktree, struct got_repository *repo)
8168 const struct got_error *error;
8169 struct got_commit_object *commit;
8171 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8176 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8180 error = show_histedit_progress(commit, hle, NULL);
8181 got_object_commit_close(commit);
8185 static const struct got_error *
8186 check_local_changes(void *arg, unsigned char status,
8187 unsigned char staged_status, const char *path,
8188 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8189 struct got_object_id *commit_id, int dirfd, const char *de_name)
8191 int *have_local_changes = arg;
8194 case GOT_STATUS_ADD:
8195 case GOT_STATUS_DELETE:
8196 case GOT_STATUS_MODIFY:
8197 case GOT_STATUS_CONFLICT:
8198 *have_local_changes = 1;
8199 return got_error(GOT_ERR_CANCELLED);
8204 switch (staged_status) {
8205 case GOT_STATUS_ADD:
8206 case GOT_STATUS_DELETE:
8207 case GOT_STATUS_MODIFY:
8208 *have_local_changes = 1;
8209 return got_error(GOT_ERR_CANCELLED);
8217 static const struct got_error *
8218 cmd_histedit(int argc, char *argv[])
8220 const struct got_error *error = NULL;
8221 struct got_worktree *worktree = NULL;
8222 struct got_fileindex *fileindex = NULL;
8223 struct got_repository *repo = NULL;
8225 struct got_reference *branch = NULL;
8226 struct got_reference *tmp_branch = NULL;
8227 struct got_object_id *resume_commit_id = NULL;
8228 struct got_object_id *base_commit_id = NULL;
8229 struct got_object_id *head_commit_id = NULL;
8230 struct got_commit_object *commit = NULL;
8231 int ch, rebase_in_progress = 0;
8232 struct got_update_progress_arg upa;
8233 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8234 int edit_logmsg_only = 0;
8235 const char *edit_script_path = NULL;
8236 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8237 struct got_object_id_queue commits;
8238 struct got_pathlist_head merged_paths;
8239 const struct got_object_id_queue *parent_ids;
8240 struct got_object_qid *pid;
8241 struct got_histedit_list histedit_cmds;
8242 struct got_histedit_list_entry *hle;
8244 SIMPLEQ_INIT(&commits);
8245 TAILQ_INIT(&histedit_cmds);
8246 TAILQ_INIT(&merged_paths);
8247 memset(&upa, 0, sizeof(upa));
8249 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8258 edit_script_path = optarg;
8261 edit_logmsg_only = 1;
8273 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8274 "unveil", NULL) == -1)
8277 if (abort_edit && continue_edit)
8278 errx(1, "histedit's -a and -c options are mutually exclusive");
8279 if (edit_script_path && edit_logmsg_only)
8280 errx(1, "histedit's -F and -m options are mutually exclusive");
8281 if (abort_edit && edit_logmsg_only)
8282 errx(1, "histedit's -a and -m options are mutually exclusive");
8283 if (continue_edit && edit_logmsg_only)
8284 errx(1, "histedit's -c and -m options are mutually exclusive");
8289 * This command cannot apply unveil(2) in all cases because the
8290 * user may choose to run an editor to edit the histedit script
8291 * and to edit individual commit log messages.
8292 * unveil(2) traverses exec(2); if an editor is used we have to
8293 * apply unveil after edit script and log messages have been written.
8294 * XXX TODO: Make use of unveil(2) where possible.
8297 cwd = getcwd(NULL, 0);
8299 error = got_error_from_errno("getcwd");
8302 error = got_worktree_open(&worktree, cwd);
8304 if (error->code == GOT_ERR_NOT_WORKTREE)
8305 error = wrap_not_worktree_error(error, "histedit", cwd);
8309 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8314 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8317 if (rebase_in_progress) {
8318 error = got_error(GOT_ERR_REBASING);
8322 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8326 if (edit_in_progress && edit_logmsg_only) {
8327 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8328 "histedit operation is in progress in this "
8329 "work tree and must be continued or aborted "
8330 "before the -m option can be used");
8334 if (edit_in_progress && abort_edit) {
8335 error = got_worktree_histedit_continue(&resume_commit_id,
8336 &tmp_branch, &branch, &base_commit_id, &fileindex,
8340 printf("Switching work tree to %s\n",
8341 got_ref_get_symref_target(branch));
8342 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8343 branch, base_commit_id, update_progress, &upa);
8346 printf("Histedit of %s aborted\n",
8347 got_ref_get_symref_target(branch));
8348 print_update_progress_stats(&upa);
8349 goto done; /* nothing else to do */
8350 } else if (abort_edit) {
8351 error = got_error(GOT_ERR_NOT_HISTEDIT);
8355 if (continue_edit) {
8358 if (!edit_in_progress) {
8359 error = got_error(GOT_ERR_NOT_HISTEDIT);
8363 error = got_worktree_get_histedit_script_path(&path, worktree);
8367 error = histedit_load_list(&histedit_cmds, path, repo);
8372 error = got_worktree_histedit_continue(&resume_commit_id,
8373 &tmp_branch, &branch, &base_commit_id, &fileindex,
8378 error = got_ref_resolve(&head_commit_id, repo, branch);
8382 error = got_object_open_as_commit(&commit, repo,
8386 parent_ids = got_object_commit_get_parent_ids(commit);
8387 pid = SIMPLEQ_FIRST(parent_ids);
8389 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8392 error = collect_commits(&commits, head_commit_id, pid->id,
8393 base_commit_id, got_worktree_get_path_prefix(worktree),
8394 GOT_ERR_HISTEDIT_PATH, repo);
8395 got_object_commit_close(commit);
8400 if (edit_in_progress) {
8401 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8405 error = got_ref_open(&branch, repo,
8406 got_worktree_get_head_ref_name(worktree), 0);
8410 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8411 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8412 "will not edit commit history of a branch outside "
8413 "the \"refs/heads/\" reference namespace");
8417 error = got_ref_resolve(&head_commit_id, repo, branch);
8418 got_ref_close(branch);
8423 error = got_object_open_as_commit(&commit, repo,
8427 parent_ids = got_object_commit_get_parent_ids(commit);
8428 pid = SIMPLEQ_FIRST(parent_ids);
8430 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8433 error = collect_commits(&commits, head_commit_id, pid->id,
8434 got_worktree_get_base_commit_id(worktree),
8435 got_worktree_get_path_prefix(worktree),
8436 GOT_ERR_HISTEDIT_PATH, repo);
8437 got_object_commit_close(commit);
8442 if (SIMPLEQ_EMPTY(&commits)) {
8443 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8447 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8448 &base_commit_id, &fileindex, worktree, repo);
8452 if (edit_script_path) {
8453 error = histedit_load_list(&histedit_cmds,
8454 edit_script_path, repo);
8456 got_worktree_histedit_abort(worktree, fileindex,
8457 repo, branch, base_commit_id,
8458 update_progress, &upa);
8459 print_update_progress_stats(&upa);
8463 const char *branch_name;
8464 branch_name = got_ref_get_symref_target(branch);
8465 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8467 error = histedit_edit_script(&histedit_cmds, &commits,
8468 branch_name, edit_logmsg_only, repo);
8470 got_worktree_histedit_abort(worktree, fileindex,
8471 repo, branch, base_commit_id,
8472 update_progress, &upa);
8473 print_update_progress_stats(&upa);
8479 error = histedit_save_list(&histedit_cmds, worktree,
8482 got_worktree_histedit_abort(worktree, fileindex,
8483 repo, branch, base_commit_id,
8484 update_progress, &upa);
8485 print_update_progress_stats(&upa);
8491 error = histedit_check_script(&histedit_cmds, &commits, repo);
8495 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8496 if (resume_commit_id) {
8497 if (got_object_id_cmp(hle->commit_id,
8498 resume_commit_id) != 0)
8501 resume_commit_id = NULL;
8502 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8503 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8504 error = histedit_skip_commit(hle, worktree,
8509 struct got_pathlist_head paths;
8510 int have_changes = 0;
8513 error = got_pathlist_append(&paths, "", NULL);
8516 error = got_worktree_status(worktree, &paths,
8517 repo, check_local_changes, &have_changes,
8518 check_cancelled, NULL);
8519 got_pathlist_free(&paths);
8521 if (error->code != GOT_ERR_CANCELLED)
8523 if (sigint_received || sigpipe_received)
8527 error = histedit_commit(NULL, worktree,
8528 fileindex, tmp_branch, hle, repo);
8532 error = got_object_open_as_commit(
8533 &commit, repo, hle->commit_id);
8536 error = show_histedit_progress(commit,
8538 got_object_commit_close(commit);
8547 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8548 error = histedit_skip_commit(hle, worktree, repo);
8554 error = got_object_open_as_commit(&commit, repo,
8558 parent_ids = got_object_commit_get_parent_ids(commit);
8559 pid = SIMPLEQ_FIRST(parent_ids);
8561 error = got_worktree_histedit_merge_files(&merged_paths,
8562 worktree, fileindex, pid->id, hle->commit_id, repo,
8563 update_progress, &upa, check_cancelled, NULL);
8566 got_object_commit_close(commit);
8569 print_update_progress_stats(&upa);
8570 if (upa.conflicts > 0)
8571 rebase_status = GOT_STATUS_CONFLICT;
8573 if (rebase_status == GOT_STATUS_CONFLICT) {
8574 error = show_rebase_merge_conflict(hle->commit_id,
8578 got_worktree_rebase_pathlist_free(&merged_paths);
8582 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8584 error = got_object_id_str(&id_str, hle->commit_id);
8587 printf("Stopping histedit for amending commit %s\n",
8590 got_worktree_rebase_pathlist_free(&merged_paths);
8591 error = got_worktree_histedit_postpone(worktree,
8596 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8597 error = histedit_skip_commit(hle, worktree, repo);
8603 error = histedit_commit(&merged_paths, worktree, fileindex,
8604 tmp_branch, hle, repo);
8605 got_worktree_rebase_pathlist_free(&merged_paths);
8610 if (rebase_status == GOT_STATUS_CONFLICT) {
8611 error = got_worktree_histedit_postpone(worktree, fileindex);
8614 error = got_error_msg(GOT_ERR_CONFLICTS,
8615 "conflicts must be resolved before histedit can continue");
8617 error = histedit_complete(worktree, fileindex, tmp_branch,
8620 got_object_id_queue_free(&commits);
8621 histedit_free_list(&histedit_cmds);
8622 free(head_commit_id);
8623 free(base_commit_id);
8624 free(resume_commit_id);
8626 got_object_commit_close(commit);
8628 got_ref_close(branch);
8630 got_ref_close(tmp_branch);
8632 got_worktree_close(worktree);
8634 got_repo_close(repo);
8639 usage_integrate(void)
8641 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8645 static const struct got_error *
8646 cmd_integrate(int argc, char *argv[])
8648 const struct got_error *error = NULL;
8649 struct got_repository *repo = NULL;
8650 struct got_worktree *worktree = NULL;
8651 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8652 const char *branch_arg = NULL;
8653 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8654 struct got_fileindex *fileindex = NULL;
8655 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8657 struct got_update_progress_arg upa;
8659 while ((ch = getopt(argc, argv, "")) != -1) {
8672 branch_arg = argv[0];
8674 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8675 "unveil", NULL) == -1)
8678 cwd = getcwd(NULL, 0);
8680 error = got_error_from_errno("getcwd");
8684 error = got_worktree_open(&worktree, cwd);
8686 if (error->code == GOT_ERR_NOT_WORKTREE)
8687 error = wrap_not_worktree_error(error, "integrate",
8692 error = check_rebase_or_histedit_in_progress(worktree);
8696 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8701 error = apply_unveil(got_repo_get_path(repo), 0,
8702 got_worktree_get_root_path(worktree));
8706 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8707 error = got_error_from_errno("asprintf");
8711 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8712 &base_branch_ref, worktree, refname, repo);
8716 refname = strdup(got_ref_get_name(branch_ref));
8717 if (refname == NULL) {
8718 error = got_error_from_errno("strdup");
8719 got_worktree_integrate_abort(worktree, fileindex, repo,
8720 branch_ref, base_branch_ref);
8723 base_refname = strdup(got_ref_get_name(base_branch_ref));
8724 if (base_refname == NULL) {
8725 error = got_error_from_errno("strdup");
8726 got_worktree_integrate_abort(worktree, fileindex, repo,
8727 branch_ref, base_branch_ref);
8731 error = got_ref_resolve(&commit_id, repo, branch_ref);
8735 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8739 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8740 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8741 "specified branch has already been integrated");
8742 got_worktree_integrate_abort(worktree, fileindex, repo,
8743 branch_ref, base_branch_ref);
8747 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8749 if (error->code == GOT_ERR_ANCESTRY)
8750 error = got_error(GOT_ERR_REBASE_REQUIRED);
8751 got_worktree_integrate_abort(worktree, fileindex, repo,
8752 branch_ref, base_branch_ref);
8756 memset(&upa, 0, sizeof(upa));
8757 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8758 branch_ref, base_branch_ref, update_progress, &upa,
8759 check_cancelled, NULL);
8763 printf("Integrated %s into %s\n", refname, base_refname);
8764 print_update_progress_stats(&upa);
8767 got_repo_close(repo);
8769 got_worktree_close(worktree);
8771 free(base_commit_id);
8781 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8782 "[-S] [file-path ...]\n",
8787 static const struct got_error *
8788 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8789 const char *path, struct got_object_id *blob_id,
8790 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8791 int dirfd, const char *de_name)
8793 const struct got_error *err = NULL;
8794 char *id_str = NULL;
8796 if (staged_status != GOT_STATUS_ADD &&
8797 staged_status != GOT_STATUS_MODIFY &&
8798 staged_status != GOT_STATUS_DELETE)
8801 if (staged_status == GOT_STATUS_ADD ||
8802 staged_status == GOT_STATUS_MODIFY)
8803 err = got_object_id_str(&id_str, staged_blob_id);
8805 err = got_object_id_str(&id_str, blob_id);
8809 printf("%s %c %s\n", id_str, staged_status, path);
8814 static const struct got_error *
8815 cmd_stage(int argc, char *argv[])
8817 const struct got_error *error = NULL;
8818 struct got_repository *repo = NULL;
8819 struct got_worktree *worktree = NULL;
8821 struct got_pathlist_head paths;
8822 struct got_pathlist_entry *pe;
8823 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8824 FILE *patch_script_file = NULL;
8825 const char *patch_script_path = NULL;
8826 struct choose_patch_arg cpa;
8830 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8839 patch_script_path = optarg;
8842 allow_bad_symlinks = 1;
8854 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8855 "unveil", NULL) == -1)
8858 if (list_stage && (pflag || patch_script_path))
8859 errx(1, "-l option cannot be used with other options");
8860 if (patch_script_path && !pflag)
8861 errx(1, "-F option can only be used together with -p option");
8863 cwd = getcwd(NULL, 0);
8865 error = got_error_from_errno("getcwd");
8869 error = got_worktree_open(&worktree, cwd);
8871 if (error->code == GOT_ERR_NOT_WORKTREE)
8872 error = wrap_not_worktree_error(error, "stage", cwd);
8876 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8881 if (patch_script_path) {
8882 patch_script_file = fopen(patch_script_path, "r");
8883 if (patch_script_file == NULL) {
8884 error = got_error_from_errno2("fopen",
8889 error = apply_unveil(got_repo_get_path(repo), 0,
8890 got_worktree_get_root_path(worktree));
8894 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8899 error = got_worktree_status(worktree, &paths, repo,
8900 print_stage, NULL, check_cancelled, NULL);
8902 cpa.patch_script_file = patch_script_file;
8903 cpa.action = "stage";
8904 error = got_worktree_stage(worktree, &paths,
8905 pflag ? NULL : print_status, NULL,
8906 pflag ? choose_patch : NULL, &cpa,
8907 allow_bad_symlinks, repo);
8910 if (patch_script_file && fclose(patch_script_file) == EOF &&
8912 error = got_error_from_errno2("fclose", patch_script_path);
8914 got_repo_close(repo);
8916 got_worktree_close(worktree);
8917 TAILQ_FOREACH(pe, &paths, entry)
8918 free((char *)pe->path);
8919 got_pathlist_free(&paths);
8927 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8928 "[file-path ...]\n",
8934 static const struct got_error *
8935 cmd_unstage(int argc, char *argv[])
8937 const struct got_error *error = NULL;
8938 struct got_repository *repo = NULL;
8939 struct got_worktree *worktree = NULL;
8941 struct got_pathlist_head paths;
8942 struct got_pathlist_entry *pe;
8944 struct got_update_progress_arg upa;
8945 FILE *patch_script_file = NULL;
8946 const char *patch_script_path = NULL;
8947 struct choose_patch_arg cpa;
8951 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8957 patch_script_path = optarg;
8969 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8970 "unveil", NULL) == -1)
8973 if (patch_script_path && !pflag)
8974 errx(1, "-F option can only be used together with -p option");
8976 cwd = getcwd(NULL, 0);
8978 error = got_error_from_errno("getcwd");
8982 error = got_worktree_open(&worktree, cwd);
8984 if (error->code == GOT_ERR_NOT_WORKTREE)
8985 error = wrap_not_worktree_error(error, "unstage", cwd);
8989 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8994 if (patch_script_path) {
8995 patch_script_file = fopen(patch_script_path, "r");
8996 if (patch_script_file == NULL) {
8997 error = got_error_from_errno2("fopen",
9003 error = apply_unveil(got_repo_get_path(repo), 0,
9004 got_worktree_get_root_path(worktree));
9008 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9012 cpa.patch_script_file = patch_script_file;
9013 cpa.action = "unstage";
9014 memset(&upa, 0, sizeof(upa));
9015 error = got_worktree_unstage(worktree, &paths, update_progress,
9016 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9018 print_update_progress_stats(&upa);
9020 if (patch_script_file && fclose(patch_script_file) == EOF &&
9022 error = got_error_from_errno2("fclose", patch_script_path);
9024 got_repo_close(repo);
9026 got_worktree_close(worktree);
9027 TAILQ_FOREACH(pe, &paths, entry)
9028 free((char *)pe->path);
9029 got_pathlist_free(&paths);
9037 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9038 "arg1 [arg2 ...]\n", getprogname());
9042 static const struct got_error *
9043 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9045 const struct got_error *err;
9046 struct got_blob_object *blob;
9048 err = got_object_open_as_blob(&blob, repo, id, 8192);
9052 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9053 got_object_blob_close(blob);
9057 static const struct got_error *
9058 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9060 const struct got_error *err;
9061 struct got_tree_object *tree;
9064 err = got_object_open_as_tree(&tree, repo, id);
9068 nentries = got_object_tree_get_nentries(tree);
9069 for (i = 0; i < nentries; i++) {
9070 struct got_tree_entry *te;
9072 if (sigint_received || sigpipe_received)
9074 te = got_object_tree_get_entry(tree, i);
9075 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9078 fprintf(outfile, "%s %.7o %s\n", id_str,
9079 got_tree_entry_get_mode(te),
9080 got_tree_entry_get_name(te));
9084 got_object_tree_close(tree);
9088 static const struct got_error *
9089 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9091 const struct got_error *err;
9092 struct got_commit_object *commit;
9093 const struct got_object_id_queue *parent_ids;
9094 struct got_object_qid *pid;
9095 char *id_str = NULL;
9096 const char *logmsg = NULL;
9098 err = got_object_open_as_commit(&commit, repo, id);
9102 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9106 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9107 parent_ids = got_object_commit_get_parent_ids(commit);
9108 fprintf(outfile, "numparents %d\n",
9109 got_object_commit_get_nparents(commit));
9110 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9112 err = got_object_id_str(&pid_str, pid->id);
9115 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9118 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9119 got_object_commit_get_author(commit),
9120 got_object_commit_get_author_time(commit));
9122 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9123 got_object_commit_get_author(commit),
9124 got_object_commit_get_committer_time(commit));
9126 logmsg = got_object_commit_get_logmsg_raw(commit);
9127 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9128 fprintf(outfile, "%s", logmsg);
9131 got_object_commit_close(commit);
9135 static const struct got_error *
9136 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9138 const struct got_error *err;
9139 struct got_tag_object *tag;
9140 char *id_str = NULL;
9141 const char *tagmsg = NULL;
9143 err = got_object_open_as_tag(&tag, repo, id);
9147 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9153 switch (got_object_tag_get_object_type(tag)) {
9154 case GOT_OBJ_TYPE_BLOB:
9155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9156 GOT_OBJ_LABEL_BLOB);
9158 case GOT_OBJ_TYPE_TREE:
9159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9160 GOT_OBJ_LABEL_TREE);
9162 case GOT_OBJ_TYPE_COMMIT:
9163 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9164 GOT_OBJ_LABEL_COMMIT);
9166 case GOT_OBJ_TYPE_TAG:
9167 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9174 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9175 got_object_tag_get_name(tag));
9177 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9178 got_object_tag_get_tagger(tag),
9179 got_object_tag_get_tagger_time(tag));
9181 tagmsg = got_object_tag_get_message(tag);
9182 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9183 fprintf(outfile, "%s", tagmsg);
9186 got_object_tag_close(tag);
9190 static const struct got_error *
9191 cmd_cat(int argc, char *argv[])
9193 const struct got_error *error;
9194 struct got_repository *repo = NULL;
9195 struct got_worktree *worktree = NULL;
9196 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9197 const char *commit_id_str = NULL;
9198 struct got_object_id *id = NULL, *commit_id = NULL;
9199 int ch, obj_type, i, force_path = 0;
9202 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9207 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9210 commit_id_str = optarg;
9213 repo_path = realpath(optarg, NULL);
9214 if (repo_path == NULL)
9215 return got_error_from_errno2("realpath",
9217 got_path_strip_trailing_slashes(repo_path);
9231 cwd = getcwd(NULL, 0);
9233 error = got_error_from_errno("getcwd");
9236 error = got_worktree_open(&worktree, cwd);
9237 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9240 if (repo_path == NULL) {
9242 got_worktree_get_repo_path(worktree));
9243 if (repo_path == NULL) {
9244 error = got_error_from_errno("strdup");
9250 if (repo_path == NULL) {
9251 repo_path = getcwd(NULL, 0);
9252 if (repo_path == NULL)
9253 return got_error_from_errno("getcwd");
9256 error = got_repo_open(&repo, repo_path, NULL);
9261 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9265 if (commit_id_str == NULL)
9266 commit_id_str = GOT_REF_HEAD;
9267 error = got_repo_match_object_id(&commit_id, NULL,
9268 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9272 for (i = 0; i < argc; i++) {
9274 error = got_object_id_by_path(&id, repo, commit_id,
9279 error = got_repo_match_object_id(&id, &label, argv[i],
9280 GOT_OBJ_TYPE_ANY, 0, repo);
9282 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9283 error->code != GOT_ERR_NOT_REF)
9285 error = got_object_id_by_path(&id, repo,
9286 commit_id, argv[i]);
9292 error = got_object_get_type(&obj_type, repo, id);
9297 case GOT_OBJ_TYPE_BLOB:
9298 error = cat_blob(id, repo, stdout);
9300 case GOT_OBJ_TYPE_TREE:
9301 error = cat_tree(id, repo, stdout);
9303 case GOT_OBJ_TYPE_COMMIT:
9304 error = cat_commit(id, repo, stdout);
9306 case GOT_OBJ_TYPE_TAG:
9307 error = cat_tag(id, repo, stdout);
9310 error = got_error(GOT_ERR_OBJ_TYPE);
9325 got_worktree_close(worktree);
9327 const struct got_error *repo_error;
9328 repo_error = got_repo_close(repo);
9338 fprintf(stderr, "usage: %s info [path ...]\n",
9343 static const struct got_error *
9344 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9345 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9346 struct got_object_id *commit_id)
9348 const struct got_error *err = NULL;
9349 char *id_str = NULL;
9351 struct tm mytm, *tm;
9352 struct got_pathlist_head *paths = arg;
9353 struct got_pathlist_entry *pe;
9356 * Clear error indication from any of the path arguments which
9357 * would cause this file index entry to be displayed.
9359 TAILQ_FOREACH(pe, paths, entry) {
9360 if (got_path_cmp(path, pe->path, strlen(path),
9361 pe->path_len) == 0 ||
9362 got_path_is_child(path, pe->path, pe->path_len))
9363 pe->data = NULL; /* no error */
9366 printf(GOT_COMMIT_SEP_STR);
9368 printf("symlink: %s\n", path);
9369 else if (S_ISREG(mode)) {
9370 printf("file: %s\n", path);
9371 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9372 } else if (S_ISDIR(mode))
9373 printf("directory: %s\n", path);
9375 printf("something: %s\n", path);
9377 tm = localtime_r(&mtime, &mytm);
9380 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9381 return got_error(GOT_ERR_NO_SPACE);
9382 printf("timestamp: %s\n", datebuf);
9385 err = got_object_id_str(&id_str, blob_id);
9388 printf("based on blob: %s\n", id_str);
9392 if (staged_blob_id) {
9393 err = got_object_id_str(&id_str, staged_blob_id);
9396 printf("based on staged blob: %s\n", id_str);
9401 err = got_object_id_str(&id_str, commit_id);
9404 printf("based on commit: %s\n", id_str);
9411 static const struct got_error *
9412 cmd_info(int argc, char *argv[])
9414 const struct got_error *error = NULL;
9415 struct got_worktree *worktree = NULL;
9416 char *cwd = NULL, *id_str = NULL;
9417 struct got_pathlist_head paths;
9418 struct got_pathlist_entry *pe;
9419 char *uuidstr = NULL;
9420 int ch, show_files = 0;
9424 while ((ch = getopt(argc, argv, "")) != -1) {
9436 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9440 cwd = getcwd(NULL, 0);
9442 error = got_error_from_errno("getcwd");
9446 error = got_worktree_open(&worktree, cwd);
9448 if (error->code == GOT_ERR_NOT_WORKTREE)
9449 error = wrap_not_worktree_error(error, "status", cwd);
9453 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9458 error = get_worktree_paths_from_argv(&paths, argc, argv,
9465 error = got_object_id_str(&id_str,
9466 got_worktree_get_base_commit_id(worktree));
9470 error = got_worktree_get_uuid(&uuidstr, worktree);
9474 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9475 printf("work tree base commit: %s\n", id_str);
9476 printf("work tree path prefix: %s\n",
9477 got_worktree_get_path_prefix(worktree));
9478 printf("work tree branch reference: %s\n",
9479 got_worktree_get_head_ref_name(worktree));
9480 printf("work tree UUID: %s\n", uuidstr);
9481 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9484 struct got_pathlist_entry *pe;
9485 TAILQ_FOREACH(pe, &paths, entry) {
9486 if (pe->path_len == 0)
9489 * Assume this path will fail. This will be corrected
9490 * in print_path_info() in case the path does suceeed.
9492 pe->data = (void *)got_error_path(pe->path,
9495 error = got_worktree_path_info(worktree, &paths,
9496 print_path_info, &paths, check_cancelled, NULL);
9499 TAILQ_FOREACH(pe, &paths, entry) {
9500 if (pe->data != NULL) {
9501 error = pe->data; /* bad path */
9507 TAILQ_FOREACH(pe, &paths, entry)
9508 free((char *)pe->path);
9509 got_pathlist_free(&paths);