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 got_author = got_repo_get_gotconfig_author(repo);
527 if (got_author == NULL) {
528 name = got_repo_get_gitconfig_author_name(repo);
529 email = got_repo_get_gitconfig_author_email(repo);
531 if (asprintf(author, "%s <%s>", name, email) == -1)
532 return got_error_from_errno("asprintf");
536 got_author = getenv("GOT_AUTHOR");
537 if (got_author == NULL) {
538 name = got_repo_get_global_gitconfig_author_name(repo);
539 email = got_repo_get_global_gitconfig_author_email(
542 if (asprintf(author, "%s <%s>", name, email)
544 return got_error_from_errno("asprintf");
547 /* TODO: Look up user in password database? */
548 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
552 *author = strdup(got_author);
554 return got_error_from_errno("strdup");
557 * Really dumb email address check; we're only doing this to
558 * avoid git's object parser breaking on commits we create.
560 while (*got_author && *got_author != '<')
562 if (*got_author != '<') {
563 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
566 while (*got_author && *got_author != '@')
568 if (*got_author != '@') {
569 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
572 while (*got_author && *got_author != '>')
574 if (*got_author != '>')
575 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
584 static const struct got_error *
585 get_gitconfig_path(char **gitconfig_path)
587 const char *homedir = getenv("HOME");
589 *gitconfig_path = NULL;
591 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
592 return got_error_from_errno("asprintf");
598 static const struct got_error *
599 cmd_import(int argc, char *argv[])
601 const struct got_error *error = NULL;
602 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
603 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
604 const char *branch_name = "main";
605 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
606 struct got_repository *repo = NULL;
607 struct got_reference *branch_ref = NULL, *head_ref = NULL;
608 struct got_object_id *new_commit_id = NULL;
610 struct got_pathlist_head ignores;
611 struct got_pathlist_entry *pe;
612 int preserve_logmsg = 0;
614 TAILQ_INIT(&ignores);
616 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
619 branch_name = optarg;
622 logmsg = strdup(optarg);
623 if (logmsg == NULL) {
624 error = got_error_from_errno("strdup");
629 repo_path = realpath(optarg, NULL);
630 if (repo_path == NULL) {
631 error = got_error_from_errno2("realpath",
637 if (optarg[0] == '\0')
639 error = got_pathlist_insert(&pe, &ignores, optarg,
654 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
662 if (repo_path == NULL) {
663 repo_path = getcwd(NULL, 0);
664 if (repo_path == NULL)
665 return got_error_from_errno("getcwd");
667 got_path_strip_trailing_slashes(repo_path);
668 error = get_gitconfig_path(&gitconfig_path);
671 error = got_repo_open(&repo, repo_path, gitconfig_path);
675 error = get_author(&author, repo);
680 * Don't let the user create a branch name with a leading '-'.
681 * While technically a valid reference name, this case is usually
682 * an unintended typo.
684 if (branch_name[0] == '-')
685 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
687 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
688 error = got_error_from_errno("asprintf");
692 error = got_ref_open(&branch_ref, repo, refname, 0);
694 if (error->code != GOT_ERR_NOT_REF)
697 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
698 "import target branch already exists");
702 path_dir = realpath(argv[0], NULL);
703 if (path_dir == NULL) {
704 error = got_error_from_errno2("realpath", argv[0]);
707 got_path_strip_trailing_slashes(path_dir);
710 * unveil(2) traverses exec(2); if an editor is used we have
711 * to apply unveil after the log message has been written.
713 if (logmsg == NULL || strlen(logmsg) == 0) {
714 error = get_editor(&editor);
718 error = collect_import_msg(&logmsg, &logmsg_path, editor,
721 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
728 if (unveil(path_dir, "r") != 0) {
729 error = got_error_from_errno2("unveil", path_dir);
735 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
742 error = got_repo_import(&new_commit_id, path_dir, logmsg,
743 author, &ignores, repo, import_progress, NULL);
750 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
757 error = got_ref_write(branch_ref, repo);
764 error = got_object_id_str(&id_str, new_commit_id);
771 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
773 if (error->code != GOT_ERR_NOT_REF) {
779 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
787 error = got_ref_write(head_ref, repo);
795 printf("Created branch %s with commit %s\n",
796 got_ref_get_name(branch_ref), id_str);
798 if (preserve_logmsg) {
799 fprintf(stderr, "%s: log message preserved in %s\n",
800 getprogname(), logmsg_path);
801 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
802 error = got_error_from_errno2("unlink", logmsg_path);
811 free(gitconfig_path);
813 got_ref_close(branch_ref);
815 got_ref_close(head_ref);
822 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
823 "[-R reference] repository-url [directory]\n", getprogname());
827 struct got_fetch_progress_arg {
828 char last_scaled_size[FMT_SCALED_STRSIZE];
834 static const struct got_error *
835 fetch_progress(void *arg, const char *message, off_t packfile_size,
836 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
838 struct got_fetch_progress_arg *a = arg;
839 char scaled_size[FMT_SCALED_STRSIZE];
840 int p_indexed, p_resolved;
841 int print_size = 0, print_indexed = 0, print_resolved = 0;
843 if (a->verbosity < 0)
846 if (message && message[0] != '\0') {
847 printf("\rserver: %s", message);
852 if (packfile_size > 0 || nobj_indexed > 0) {
853 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
854 (a->last_scaled_size[0] == '\0' ||
855 strcmp(scaled_size, a->last_scaled_size)) != 0) {
857 if (strlcpy(a->last_scaled_size, scaled_size,
858 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
859 return got_error(GOT_ERR_NO_SPACE);
861 if (nobj_indexed > 0) {
862 p_indexed = (nobj_indexed * 100) / nobj_total;
863 if (p_indexed != a->last_p_indexed) {
864 a->last_p_indexed = p_indexed;
869 if (nobj_resolved > 0) {
870 p_resolved = (nobj_resolved * 100) /
871 (nobj_total - nobj_loose);
872 if (p_resolved != a->last_p_resolved) {
873 a->last_p_resolved = p_resolved;
881 if (print_size || print_indexed || print_resolved)
884 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
886 printf("; indexing %d%%", p_indexed);
888 printf("; resolving deltas %d%%", p_resolved);
889 if (print_size || print_indexed || print_resolved)
895 static const struct got_error *
896 create_symref(const char *refname, struct got_reference *target_ref,
897 int verbosity, struct got_repository *repo)
899 const struct got_error *err;
900 struct got_reference *head_symref;
902 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
906 err = got_ref_write(head_symref, repo);
907 got_ref_close(head_symref);
908 if (err == NULL && verbosity > 0) {
909 printf("Created reference %s: %s\n", GOT_REF_HEAD,
910 got_ref_get_name(target_ref));
915 static const struct got_error *
916 list_remote_refs(struct got_pathlist_head *symrefs,
917 struct got_pathlist_head *refs)
919 const struct got_error *err;
920 struct got_pathlist_entry *pe;
922 TAILQ_FOREACH(pe, symrefs, entry) {
923 const char *refname = pe->path;
924 const char *targetref = pe->data;
926 printf("%s: %s\n", refname, targetref);
929 TAILQ_FOREACH(pe, refs, entry) {
930 const char *refname = pe->path;
931 struct got_object_id *id = pe->data;
934 err = got_object_id_str(&id_str, id);
937 printf("%s: %s\n", refname, id_str);
944 static const struct got_error *
945 create_ref(const char *refname, struct got_object_id *id,
946 int verbosity, struct got_repository *repo)
948 const struct got_error *err = NULL;
949 struct got_reference *ref;
952 err = got_object_id_str(&id_str, id);
956 err = got_ref_alloc(&ref, refname, id);
960 err = got_ref_write(ref, repo);
963 if (err == NULL && verbosity >= 0)
964 printf("Created reference %s: %s\n", refname, id_str);
971 match_wanted_ref(const char *refname, const char *wanted_ref)
973 if (strncmp(refname, "refs/", 5) != 0)
978 * Prevent fetching of references that won't make any
979 * sense outside of the remote repository's context.
981 if (strncmp(refname, "got/", 4) == 0)
983 if (strncmp(refname, "remotes/", 8) == 0)
986 if (strncmp(wanted_ref, "refs/", 5) == 0)
989 /* Allow prefix match. */
990 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
993 /* Allow exact match. */
994 return (strcmp(refname, wanted_ref) == 0);
998 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1000 struct got_pathlist_entry *pe;
1002 TAILQ_FOREACH(pe, wanted_refs, entry) {
1003 if (match_wanted_ref(refname, pe->path))
1010 static const struct got_error *
1011 create_wanted_ref(const char *refname, struct got_object_id *id,
1012 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1014 const struct got_error *err;
1015 char *remote_refname;
1017 if (strncmp("refs/", refname, 5) == 0)
1020 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1021 remote_repo_name, refname) == -1)
1022 return got_error_from_errno("asprintf");
1024 err = create_ref(remote_refname, id, verbosity, repo);
1025 free(remote_refname);
1029 static const struct got_error *
1030 cmd_clone(int argc, char *argv[])
1032 const struct got_error *error = NULL;
1033 const char *uri, *dirname;
1034 char *proto, *host, *port, *repo_name, *server_path;
1035 char *default_destdir = NULL, *id_str = NULL;
1036 const char *repo_path;
1037 struct got_repository *repo = NULL;
1038 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1039 struct got_pathlist_entry *pe;
1040 struct got_object_id *pack_hash = NULL;
1041 int ch, fetchfd = -1, fetchstatus;
1042 pid_t fetchpid = -1;
1043 struct got_fetch_progress_arg fpa;
1044 char *git_url = NULL;
1045 char *gitconfig_path = NULL, *gotconfig_path = NULL;
1046 char *gitconfig = NULL, *gotconfig = NULL;
1047 FILE *gitconfig_file = NULL, *gotconfig_file = NULL;
1049 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1050 int list_refs_only = 0;
1051 struct got_reference *head_symref = NULL;
1054 TAILQ_INIT(&symrefs);
1055 TAILQ_INIT(&wanted_branches);
1056 TAILQ_INIT(&wanted_refs);
1058 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1061 fetch_all_branches = 1;
1064 error = got_pathlist_append(&wanted_branches,
1073 mirror_references = 1;
1078 else if (verbosity < 3)
1085 error = got_pathlist_append(&wanted_refs,
1098 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1099 errx(1, "-a and -b options are mutually exclusive");
1100 if (list_refs_only) {
1101 if (!TAILQ_EMPTY(&wanted_branches))
1102 errx(1, "-l and -b options are mutually exclusive");
1103 if (fetch_all_branches)
1104 errx(1, "-l and -a options are mutually exclusive");
1105 if (mirror_references)
1106 errx(1, "-l and -m options are mutually exclusive");
1107 if (verbosity == -1)
1108 errx(1, "-l and -q options are mutually exclusive");
1109 if (!TAILQ_EMPTY(&wanted_refs))
1110 errx(1, "-l and -R options are mutually exclusive");
1122 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1127 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1128 host, port ? ":" : "", port ? port : "",
1129 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1130 error = got_error_from_errno("asprintf");
1134 if (strcmp(proto, "git") == 0) {
1136 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1137 "sendfd dns inet unveil", NULL) == -1)
1140 } else if (strcmp(proto, "git+ssh") == 0 ||
1141 strcmp(proto, "ssh") == 0) {
1143 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1144 "sendfd unveil", NULL) == -1)
1147 } else if (strcmp(proto, "http") == 0 ||
1148 strcmp(proto, "git+http") == 0) {
1149 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1152 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1155 if (dirname == NULL) {
1156 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1157 error = got_error_from_errno("asprintf");
1160 repo_path = default_destdir;
1162 repo_path = dirname;
1164 if (!list_refs_only) {
1165 error = got_path_mkdir(repo_path);
1169 error = got_repo_init(repo_path);
1172 error = got_repo_open(&repo, repo_path, NULL);
1177 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1178 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1179 error = got_error_from_errno2("unveil",
1180 GOT_FETCH_PATH_SSH);
1184 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1189 printf("Connecting to %s%s%s\n", host,
1190 port ? ":" : "", port ? port : "");
1192 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1193 server_path, verbosity);
1197 fpa.last_scaled_size[0] = '\0';
1198 fpa.last_p_indexed = -1;
1199 fpa.last_p_resolved = -1;
1200 fpa.verbosity = verbosity;
1201 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1202 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1203 fetch_all_branches, &wanted_branches, &wanted_refs,
1204 list_refs_only, verbosity, fetchfd, repo,
1205 fetch_progress, &fpa);
1209 if (list_refs_only) {
1210 error = list_remote_refs(&symrefs, &refs);
1214 error = got_object_id_str(&id_str, pack_hash);
1218 printf("\nFetched %s.pack\n", id_str);
1221 /* Set up references provided with the pack file. */
1222 TAILQ_FOREACH(pe, &refs, entry) {
1223 const char *refname = pe->path;
1224 struct got_object_id *id = pe->data;
1225 char *remote_refname;
1227 if (is_wanted_ref(&wanted_refs, refname) &&
1228 !mirror_references) {
1229 error = create_wanted_ref(refname, id,
1230 GOT_FETCH_DEFAULT_REMOTE_NAME,
1231 verbosity - 1, repo);
1237 error = create_ref(refname, id, verbosity - 1, repo);
1241 if (mirror_references)
1244 if (strncmp("refs/heads/", refname, 11) != 0)
1247 if (asprintf(&remote_refname,
1248 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1249 refname + 11) == -1) {
1250 error = got_error_from_errno("asprintf");
1253 error = create_ref(remote_refname, id, verbosity - 1, repo);
1254 free(remote_refname);
1259 /* Set the HEAD reference if the server provided one. */
1260 TAILQ_FOREACH(pe, &symrefs, entry) {
1261 struct got_reference *target_ref;
1262 const char *refname = pe->path;
1263 const char *target = pe->data;
1264 char *remote_refname = NULL, *remote_target = NULL;
1266 if (strcmp(refname, GOT_REF_HEAD) != 0)
1269 error = got_ref_open(&target_ref, repo, target, 0);
1271 if (error->code == GOT_ERR_NOT_REF) {
1278 error = create_symref(refname, target_ref, verbosity, repo);
1279 got_ref_close(target_ref);
1283 if (mirror_references)
1286 if (strncmp("refs/heads/", target, 11) != 0)
1289 if (asprintf(&remote_refname,
1290 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1292 error = got_error_from_errno("asprintf");
1295 if (asprintf(&remote_target,
1296 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1297 target + 11) == -1) {
1298 error = got_error_from_errno("asprintf");
1299 free(remote_refname);
1302 error = got_ref_open(&target_ref, repo, remote_target, 0);
1304 free(remote_refname);
1305 free(remote_target);
1306 if (error->code == GOT_ERR_NOT_REF) {
1312 error = create_symref(remote_refname, target_ref,
1313 verbosity - 1, repo);
1314 free(remote_refname);
1315 free(remote_target);
1316 got_ref_close(target_ref);
1322 * We failed to set the HEAD reference. If we asked for
1323 * a set of wanted branches use the first of one of those
1324 * which could be fetched instead.
1326 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1327 const char *target = pe->path;
1328 struct got_reference *target_ref;
1330 error = got_ref_open(&target_ref, repo, target, 0);
1332 if (error->code == GOT_ERR_NOT_REF) {
1339 error = create_symref(GOT_REF_HEAD, target_ref,
1341 got_ref_close(target_ref);
1348 /* Create got.conf(5). */
1349 gotconfig_path = got_repo_get_path_gotconfig(repo);
1350 if (gotconfig_path == NULL) {
1351 error = got_error_from_errno("got_repo_get_path_gotconfig");
1354 gotconfig_file = fopen(gotconfig_path, "a");
1355 if (gotconfig_file == NULL) {
1356 error = got_error_from_errno2("fopen", gotconfig_path);
1359 got_path_strip_trailing_slashes(server_path);
1360 if (asprintf(&gotconfig,
1365 "\trepository \"%s\"\n"
1368 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1369 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1371 mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1372 error = got_error_from_errno("asprintf");
1375 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1376 if (n != strlen(gotconfig)) {
1377 error = got_ferror(gotconfig_file, GOT_ERR_IO);
1381 /* Create a config file Git can understand. */
1382 gitconfig_path = got_repo_get_path_gitconfig(repo);
1383 if (gitconfig_path == NULL) {
1384 error = got_error_from_errno("got_repo_get_path_gitconfig");
1387 gitconfig_file = fopen(gitconfig_path, "a");
1388 if (gitconfig_file == NULL) {
1389 error = got_error_from_errno2("fopen", gitconfig_path);
1392 if (mirror_references) {
1393 if (asprintf(&gitconfig,
1396 "\tfetch = +refs/*:refs/*\n"
1397 "\tmirror = true\n",
1398 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1399 error = got_error_from_errno("asprintf");
1402 } else if (fetch_all_branches) {
1403 if (asprintf(&gitconfig,
1406 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1407 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1408 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1409 error = got_error_from_errno("asprintf");
1413 const char *branchname;
1416 * If the server specified a default branch, use just that one.
1417 * Otherwise fall back to fetching all branches on next fetch.
1420 branchname = got_ref_get_symref_target(head_symref);
1421 if (strncmp(branchname, "refs/heads/", 11) == 0)
1424 branchname = "*"; /* fall back to all branches */
1425 if (asprintf(&gitconfig,
1428 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1429 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1430 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1431 branchname) == -1) {
1432 error = got_error_from_errno("asprintf");
1436 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1437 if (n != strlen(gitconfig)) {
1438 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1443 printf("Created %s repository '%s'\n",
1444 mirror_references ? "mirrored" : "cloned", repo_path);
1447 if (kill(fetchpid, SIGTERM) == -1)
1448 error = got_error_from_errno("kill");
1449 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1450 error = got_error_from_errno("waitpid");
1452 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1453 error = got_error_from_errno("close");
1454 if (gotconfig_file && fclose(gotconfig_file) == EOF && error == NULL)
1455 error = got_error_from_errno("fclose");
1456 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1457 error = got_error_from_errno("fclose");
1459 got_repo_close(repo);
1461 got_ref_close(head_symref);
1462 TAILQ_FOREACH(pe, &refs, entry) {
1463 free((void *)pe->path);
1466 got_pathlist_free(&refs);
1467 TAILQ_FOREACH(pe, &symrefs, entry) {
1468 free((void *)pe->path);
1471 got_pathlist_free(&symrefs);
1472 got_pathlist_free(&wanted_branches);
1473 got_pathlist_free(&wanted_refs);
1480 free(default_destdir);
1483 free(gotconfig_path);
1484 free(gitconfig_path);
1489 static const struct got_error *
1490 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1491 int replace_tags, int verbosity, struct got_repository *repo)
1493 const struct got_error *err = NULL;
1494 char *new_id_str = NULL;
1495 struct got_object_id *old_id = NULL;
1497 err = got_object_id_str(&new_id_str, new_id);
1501 if (!replace_tags &&
1502 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1503 err = got_ref_resolve(&old_id, repo, ref);
1506 if (got_object_id_cmp(old_id, new_id) == 0)
1508 if (verbosity >= 0) {
1509 printf("Rejecting update of existing tag %s: %s\n",
1510 got_ref_get_name(ref), new_id_str);
1515 if (got_ref_is_symbolic(ref)) {
1516 if (verbosity >= 0) {
1517 printf("Replacing reference %s: %s\n",
1518 got_ref_get_name(ref),
1519 got_ref_get_symref_target(ref));
1521 err = got_ref_change_symref_to_ref(ref, new_id);
1524 err = got_ref_write(ref, repo);
1528 err = got_ref_resolve(&old_id, repo, ref);
1531 if (got_object_id_cmp(old_id, new_id) == 0)
1534 err = got_ref_change_ref(ref, new_id);
1537 err = got_ref_write(ref, repo);
1543 printf("Updated %s: %s\n", got_ref_get_name(ref),
1551 static const struct got_error *
1552 update_symref(const char *refname, struct got_reference *target_ref,
1553 int verbosity, struct got_repository *repo)
1555 const struct got_error *err = NULL, *unlock_err;
1556 struct got_reference *symref;
1557 int symref_is_locked = 0;
1559 err = got_ref_open(&symref, repo, refname, 1);
1561 if (err->code != GOT_ERR_NOT_REF)
1563 err = got_ref_alloc_symref(&symref, refname, target_ref);
1567 err = got_ref_write(symref, repo);
1572 printf("Created reference %s: %s\n",
1573 got_ref_get_name(symref),
1574 got_ref_get_symref_target(symref));
1576 symref_is_locked = 1;
1578 if (strcmp(got_ref_get_symref_target(symref),
1579 got_ref_get_name(target_ref)) == 0)
1582 err = got_ref_change_symref(symref,
1583 got_ref_get_name(target_ref));
1587 err = got_ref_write(symref, repo);
1592 printf("Updated %s: %s\n", got_ref_get_name(symref),
1593 got_ref_get_symref_target(symref));
1597 if (symref_is_locked) {
1598 unlock_err = got_ref_unlock(symref);
1599 if (unlock_err && err == NULL)
1602 got_ref_close(symref);
1609 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1610 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1611 "[remote-repository-name]\n",
1616 static const struct got_error *
1617 delete_missing_ref(struct got_reference *ref,
1618 int verbosity, struct got_repository *repo)
1620 const struct got_error *err = NULL;
1621 struct got_object_id *id = NULL;
1622 char *id_str = NULL;
1624 if (got_ref_is_symbolic(ref)) {
1625 err = got_ref_delete(ref, repo);
1628 if (verbosity >= 0) {
1629 printf("Deleted reference %s: %s\n",
1630 got_ref_get_name(ref),
1631 got_ref_get_symref_target(ref));
1634 err = got_ref_resolve(&id, repo, ref);
1637 err = got_object_id_str(&id_str, id);
1641 err = got_ref_delete(ref, repo);
1644 if (verbosity >= 0) {
1645 printf("Deleted reference %s: %s\n",
1646 got_ref_get_name(ref), id_str);
1655 static const struct got_error *
1656 delete_missing_refs(struct got_pathlist_head *their_refs,
1657 struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1658 int verbosity, struct got_repository *repo)
1660 const struct got_error *err = NULL, *unlock_err;
1661 struct got_reflist_head my_refs;
1662 struct got_reflist_entry *re;
1663 struct got_pathlist_entry *pe;
1664 char *remote_namespace = NULL;
1665 char *local_refname = NULL;
1667 SIMPLEQ_INIT(&my_refs);
1669 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1671 return got_error_from_errno("asprintf");
1673 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1677 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1678 const char *refname = got_ref_get_name(re->ref);
1680 if (!remote->mirror_references) {
1681 if (strncmp(refname, remote_namespace,
1682 strlen(remote_namespace)) == 0) {
1683 if (strcmp(refname + strlen(remote_namespace),
1686 if (asprintf(&local_refname, "refs/heads/%s",
1687 refname + strlen(remote_namespace)) == -1) {
1688 err = got_error_from_errno("asprintf");
1691 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1695 TAILQ_FOREACH(pe, their_refs, entry) {
1696 if (strcmp(local_refname, pe->path) == 0)
1702 TAILQ_FOREACH(pe, their_symrefs, entry) {
1703 if (strcmp(local_refname, pe->path) == 0)
1709 err = delete_missing_ref(re->ref, verbosity, repo);
1713 if (local_refname) {
1714 struct got_reference *ref;
1715 err = got_ref_open(&ref, repo, local_refname, 1);
1717 if (err->code != GOT_ERR_NOT_REF)
1719 free(local_refname);
1720 local_refname = NULL;
1723 err = delete_missing_ref(ref, verbosity, repo);
1726 unlock_err = got_ref_unlock(ref);
1728 if (unlock_err && err == NULL) {
1733 free(local_refname);
1734 local_refname = NULL;
1738 free(remote_namespace);
1739 free(local_refname);
1743 static const struct got_error *
1744 update_wanted_ref(const char *refname, struct got_object_id *id,
1745 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1747 const struct got_error *err, *unlock_err;
1748 char *remote_refname;
1749 struct got_reference *ref;
1751 if (strncmp("refs/", refname, 5) == 0)
1754 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1755 remote_repo_name, refname) == -1)
1756 return got_error_from_errno("asprintf");
1758 err = got_ref_open(&ref, repo, remote_refname, 1);
1760 if (err->code != GOT_ERR_NOT_REF)
1762 err = create_ref(remote_refname, id, verbosity, repo);
1764 err = update_ref(ref, id, 0, verbosity, repo);
1765 unlock_err = got_ref_unlock(ref);
1766 if (unlock_err && err == NULL)
1771 free(remote_refname);
1775 static const struct got_error *
1776 cmd_fetch(int argc, char *argv[])
1778 const struct got_error *error = NULL, *unlock_err;
1779 char *cwd = NULL, *repo_path = NULL;
1780 const char *remote_name;
1781 char *proto = NULL, *host = NULL, *port = NULL;
1782 char *repo_name = NULL, *server_path = NULL;
1783 struct got_remote_repo *remotes, *remote = NULL;
1785 char *id_str = NULL;
1786 struct got_repository *repo = NULL;
1787 struct got_worktree *worktree = NULL;
1788 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1789 struct got_pathlist_entry *pe;
1790 struct got_object_id *pack_hash = NULL;
1791 int i, ch, fetchfd = -1, fetchstatus;
1792 pid_t fetchpid = -1;
1793 struct got_fetch_progress_arg fpa;
1794 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1795 int delete_refs = 0, replace_tags = 0;
1798 TAILQ_INIT(&symrefs);
1799 TAILQ_INIT(&wanted_branches);
1800 TAILQ_INIT(&wanted_refs);
1802 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1805 fetch_all_branches = 1;
1808 error = got_pathlist_append(&wanted_branches,
1820 repo_path = realpath(optarg, NULL);
1821 if (repo_path == NULL)
1822 return got_error_from_errno2("realpath",
1824 got_path_strip_trailing_slashes(repo_path);
1832 else if (verbosity < 3)
1839 error = got_pathlist_append(&wanted_refs,
1852 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1853 errx(1, "-a and -b options are mutually exclusive");
1854 if (list_refs_only) {
1855 if (!TAILQ_EMPTY(&wanted_branches))
1856 errx(1, "-l and -b options are mutually exclusive");
1857 if (fetch_all_branches)
1858 errx(1, "-l and -a options are mutually exclusive");
1860 errx(1, "-l and -d options are mutually exclusive");
1861 if (verbosity == -1)
1862 errx(1, "-l and -q options are mutually exclusive");
1866 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1868 remote_name = argv[0];
1872 cwd = getcwd(NULL, 0);
1874 error = got_error_from_errno("getcwd");
1878 if (repo_path == NULL) {
1879 error = got_worktree_open(&worktree, cwd);
1880 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1886 strdup(got_worktree_get_repo_path(worktree));
1887 if (repo_path == NULL)
1888 error = got_error_from_errno("strdup");
1892 repo_path = strdup(cwd);
1893 if (repo_path == NULL) {
1894 error = got_error_from_errno("strdup");
1900 error = got_repo_open(&repo, repo_path, NULL);
1904 got_repo_get_gotconfig_remotes(&nremotes, &remotes, repo);
1905 for (i = 0; i < nremotes; i++) {
1906 remote = &remotes[i];
1907 if (strcmp(remote->name, remote_name) == 0)
1910 if (i == nremotes) {
1911 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1912 for (i = 0; i < nremotes; i++) {
1913 remote = &remotes[i];
1914 if (strcmp(remote->name, remote_name) == 0)
1917 if (i == nremotes) {
1918 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1923 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1924 &repo_name, remote->url);
1928 if (strcmp(proto, "git") == 0) {
1930 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1931 "sendfd dns inet unveil", NULL) == -1)
1934 } else if (strcmp(proto, "git+ssh") == 0 ||
1935 strcmp(proto, "ssh") == 0) {
1937 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1938 "sendfd unveil", NULL) == -1)
1941 } else if (strcmp(proto, "http") == 0 ||
1942 strcmp(proto, "git+http") == 0) {
1943 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1946 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1950 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1951 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1952 error = got_error_from_errno2("unveil",
1953 GOT_FETCH_PATH_SSH);
1957 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1962 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1963 port ? ":" : "", port ? port : "");
1965 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1966 server_path, verbosity);
1970 fpa.last_scaled_size[0] = '\0';
1971 fpa.last_p_indexed = -1;
1972 fpa.last_p_resolved = -1;
1973 fpa.verbosity = verbosity;
1974 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1975 remote->mirror_references, fetch_all_branches, &wanted_branches,
1976 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1977 fetch_progress, &fpa);
1981 if (list_refs_only) {
1982 error = list_remote_refs(&symrefs, &refs);
1986 if (pack_hash == NULL) {
1988 printf("Already up-to-date\n");
1989 } else if (verbosity >= 0) {
1990 error = got_object_id_str(&id_str, pack_hash);
1993 printf("\nFetched %s.pack\n", id_str);
1998 /* Update references provided with the pack file. */
1999 TAILQ_FOREACH(pe, &refs, entry) {
2000 const char *refname = pe->path;
2001 struct got_object_id *id = pe->data;
2002 struct got_reference *ref;
2003 char *remote_refname;
2005 if (is_wanted_ref(&wanted_refs, refname) &&
2006 !remote->mirror_references) {
2007 error = update_wanted_ref(refname, id,
2008 remote->name, verbosity, repo);
2014 if (remote->mirror_references ||
2015 strncmp("refs/tags/", refname, 10) == 0) {
2016 error = got_ref_open(&ref, repo, refname, 1);
2018 if (error->code != GOT_ERR_NOT_REF)
2020 error = create_ref(refname, id, verbosity,
2025 error = update_ref(ref, id, replace_tags,
2027 unlock_err = got_ref_unlock(ref);
2028 if (unlock_err && error == NULL)
2034 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2035 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2036 remote_name, refname + 11) == -1) {
2037 error = got_error_from_errno("asprintf");
2041 error = got_ref_open(&ref, repo, remote_refname, 1);
2043 if (error->code != GOT_ERR_NOT_REF)
2045 error = create_ref(remote_refname, id,
2050 error = update_ref(ref, id, replace_tags,
2052 unlock_err = got_ref_unlock(ref);
2053 if (unlock_err && error == NULL)
2060 /* Also create a local branch if none exists yet. */
2061 error = got_ref_open(&ref, repo, refname, 1);
2063 if (error->code != GOT_ERR_NOT_REF)
2065 error = create_ref(refname, id, verbosity,
2070 unlock_err = got_ref_unlock(ref);
2071 if (unlock_err && error == NULL)
2078 error = delete_missing_refs(&refs, &symrefs, remote,
2084 if (!remote->mirror_references) {
2085 /* Update remote HEAD reference if the server provided one. */
2086 TAILQ_FOREACH(pe, &symrefs, entry) {
2087 struct got_reference *target_ref;
2088 const char *refname = pe->path;
2089 const char *target = pe->data;
2090 char *remote_refname = NULL, *remote_target = NULL;
2092 if (strcmp(refname, GOT_REF_HEAD) != 0)
2095 if (strncmp("refs/heads/", target, 11) != 0)
2098 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2099 remote->name, refname) == -1) {
2100 error = got_error_from_errno("asprintf");
2103 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2104 remote->name, target + 11) == -1) {
2105 error = got_error_from_errno("asprintf");
2106 free(remote_refname);
2110 error = got_ref_open(&target_ref, repo, remote_target,
2113 free(remote_refname);
2114 free(remote_target);
2115 if (error->code == GOT_ERR_NOT_REF) {
2121 error = update_symref(remote_refname, target_ref,
2123 free(remote_refname);
2124 free(remote_target);
2125 got_ref_close(target_ref);
2132 if (kill(fetchpid, SIGTERM) == -1)
2133 error = got_error_from_errno("kill");
2134 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2135 error = got_error_from_errno("waitpid");
2137 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2138 error = got_error_from_errno("close");
2140 got_repo_close(repo);
2142 got_worktree_close(worktree);
2143 TAILQ_FOREACH(pe, &refs, entry) {
2144 free((void *)pe->path);
2147 got_pathlist_free(&refs);
2148 TAILQ_FOREACH(pe, &symrefs, entry) {
2149 free((void *)pe->path);
2152 got_pathlist_free(&symrefs);
2153 got_pathlist_free(&wanted_branches);
2154 got_pathlist_free(&wanted_refs);
2169 usage_checkout(void)
2171 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2172 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2177 show_worktree_base_ref_warning(void)
2179 fprintf(stderr, "%s: warning: could not create a reference "
2180 "to the work tree's base commit; the commit could be "
2181 "garbage-collected by Git; making the repository "
2182 "writable and running 'got update' will prevent this\n",
2186 struct got_checkout_progress_arg {
2187 const char *worktree_path;
2188 int had_base_commit_ref_error;
2191 static const struct got_error *
2192 checkout_progress(void *arg, unsigned char status, const char *path)
2194 struct got_checkout_progress_arg *a = arg;
2196 /* Base commit bump happens silently. */
2197 if (status == GOT_STATUS_BUMP_BASE)
2200 if (status == GOT_STATUS_BASE_REF_ERR) {
2201 a->had_base_commit_ref_error = 1;
2205 while (path[0] == '/')
2208 printf("%c %s/%s\n", status, a->worktree_path, path);
2212 static const struct got_error *
2213 check_cancelled(void *arg)
2215 if (sigint_received || sigpipe_received)
2216 return got_error(GOT_ERR_CANCELLED);
2220 static const struct got_error *
2221 check_linear_ancestry(struct got_object_id *commit_id,
2222 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2223 struct got_repository *repo)
2225 const struct got_error *err = NULL;
2226 struct got_object_id *yca_id;
2228 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2229 commit_id, base_commit_id, repo, check_cancelled, NULL);
2234 return got_error(GOT_ERR_ANCESTRY);
2237 * Require a straight line of history between the target commit
2238 * and the work tree's base commit.
2240 * Non-linear situations such as this require a rebase:
2242 * (commit) D F (base_commit)
2250 * 'got update' only handles linear cases:
2251 * Update forwards in time: A (base/yca) - B - C - D (commit)
2252 * Update backwards in time: D (base) - C - B - A (commit/yca)
2254 if (allow_forwards_in_time_only) {
2255 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2256 return got_error(GOT_ERR_ANCESTRY);
2257 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2258 got_object_id_cmp(base_commit_id, yca_id) != 0)
2259 return got_error(GOT_ERR_ANCESTRY);
2265 static const struct got_error *
2266 check_same_branch(struct got_object_id *commit_id,
2267 struct got_reference *head_ref, struct got_object_id *yca_id,
2268 struct got_repository *repo)
2270 const struct got_error *err = NULL;
2271 struct got_commit_graph *graph = NULL;
2272 struct got_object_id *head_commit_id = NULL;
2273 int is_same_branch = 0;
2275 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2279 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2283 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2288 err = got_commit_graph_open(&graph, "/", 1);
2292 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2293 check_cancelled, NULL);
2298 struct got_object_id *id;
2299 err = got_commit_graph_iter_next(&id, graph, repo,
2300 check_cancelled, NULL);
2302 if (err->code == GOT_ERR_ITER_COMPLETED)
2308 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2310 if (got_object_id_cmp(id, commit_id) == 0) {
2318 got_commit_graph_close(graph);
2319 free(head_commit_id);
2320 if (!err && !is_same_branch)
2321 err = got_error(GOT_ERR_ANCESTRY);
2325 static const struct got_error *
2326 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2328 static char msg[512];
2329 const char *branch_name;
2331 if (got_ref_is_symbolic(ref))
2332 branch_name = got_ref_get_symref_target(ref);
2334 branch_name = got_ref_get_name(ref);
2336 if (strncmp("refs/heads/", branch_name, 11) == 0)
2339 snprintf(msg, sizeof(msg),
2340 "target commit is not contained in branch '%s'; "
2341 "the branch to use must be specified with -b; "
2342 "if necessary a new branch can be created for "
2343 "this commit with 'got branch -c %s BRANCH_NAME'",
2344 branch_name, commit_id_str);
2346 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2349 static const struct got_error *
2350 cmd_checkout(int argc, char *argv[])
2352 const struct got_error *error = NULL;
2353 struct got_repository *repo = NULL;
2354 struct got_reference *head_ref = NULL;
2355 struct got_worktree *worktree = NULL;
2356 char *repo_path = NULL;
2357 char *worktree_path = NULL;
2358 const char *path_prefix = "";
2359 const char *branch_name = GOT_REF_HEAD;
2360 char *commit_id_str = NULL;
2361 int ch, same_path_prefix, allow_nonempty = 0;
2362 struct got_pathlist_head paths;
2363 struct got_checkout_progress_arg cpa;
2367 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2370 branch_name = optarg;
2373 commit_id_str = strdup(optarg);
2374 if (commit_id_str == NULL)
2375 return got_error_from_errno("strdup");
2381 path_prefix = optarg;
2393 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2394 "unveil", NULL) == -1)
2398 char *cwd, *base, *dotgit;
2399 repo_path = realpath(argv[0], NULL);
2400 if (repo_path == NULL)
2401 return got_error_from_errno2("realpath", argv[0]);
2402 cwd = getcwd(NULL, 0);
2404 error = got_error_from_errno("getcwd");
2407 if (path_prefix[0]) {
2408 base = basename(path_prefix);
2410 error = got_error_from_errno2("basename",
2415 base = basename(repo_path);
2417 error = got_error_from_errno2("basename",
2422 dotgit = strstr(base, ".git");
2425 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2426 error = got_error_from_errno("asprintf");
2431 } else if (argc == 2) {
2432 repo_path = realpath(argv[0], NULL);
2433 if (repo_path == NULL) {
2434 error = got_error_from_errno2("realpath", argv[0]);
2437 worktree_path = realpath(argv[1], NULL);
2438 if (worktree_path == NULL) {
2439 if (errno != ENOENT) {
2440 error = got_error_from_errno2("realpath",
2444 worktree_path = strdup(argv[1]);
2445 if (worktree_path == NULL) {
2446 error = got_error_from_errno("strdup");
2453 got_path_strip_trailing_slashes(repo_path);
2454 got_path_strip_trailing_slashes(worktree_path);
2456 error = got_repo_open(&repo, repo_path, NULL);
2460 /* Pre-create work tree path for unveil(2) */
2461 error = got_path_mkdir(worktree_path);
2463 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2464 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2466 if (!allow_nonempty &&
2467 !got_path_dir_is_empty(worktree_path)) {
2468 error = got_error_path(worktree_path,
2469 GOT_ERR_DIR_NOT_EMPTY);
2474 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2478 error = got_ref_open(&head_ref, repo, branch_name, 0);
2482 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2483 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2486 error = got_worktree_open(&worktree, worktree_path);
2490 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2494 if (!same_path_prefix) {
2495 error = got_error(GOT_ERR_PATH_PREFIX);
2499 if (commit_id_str) {
2500 struct got_object_id *commit_id;
2501 error = got_repo_match_object_id(&commit_id, NULL,
2502 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2505 error = check_linear_ancestry(commit_id,
2506 got_worktree_get_base_commit_id(worktree), 0, repo);
2507 if (error != NULL) {
2509 if (error->code == GOT_ERR_ANCESTRY) {
2510 error = checkout_ancestry_error(
2511 head_ref, commit_id_str);
2515 error = check_same_branch(commit_id, head_ref, NULL, repo);
2517 if (error->code == GOT_ERR_ANCESTRY) {
2518 error = checkout_ancestry_error(
2519 head_ref, commit_id_str);
2523 error = got_worktree_set_base_commit_id(worktree, repo,
2530 error = got_pathlist_append(&paths, "", NULL);
2533 cpa.worktree_path = worktree_path;
2534 cpa.had_base_commit_ref_error = 0;
2535 error = got_worktree_checkout_files(worktree, &paths, repo,
2536 checkout_progress, &cpa, check_cancelled, NULL);
2540 printf("Now shut up and hack\n");
2541 if (cpa.had_base_commit_ref_error)
2542 show_worktree_base_ref_warning();
2544 got_pathlist_free(&paths);
2545 free(commit_id_str);
2547 free(worktree_path);
2551 struct got_update_progress_arg {
2559 print_update_progress_stats(struct got_update_progress_arg *upa)
2561 if (!upa->did_something)
2564 if (upa->conflicts > 0)
2565 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2566 if (upa->obstructed > 0)
2567 printf("File paths obstructed by a non-regular file: %d\n",
2569 if (upa->not_updated > 0)
2570 printf("Files not updated because of existing merge "
2571 "conflicts: %d\n", upa->not_updated);
2577 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2582 static const struct got_error *
2583 update_progress(void *arg, unsigned char status, const char *path)
2585 struct got_update_progress_arg *upa = arg;
2587 if (status == GOT_STATUS_EXISTS ||
2588 status == GOT_STATUS_BASE_REF_ERR)
2591 upa->did_something = 1;
2593 /* Base commit bump happens silently. */
2594 if (status == GOT_STATUS_BUMP_BASE)
2597 if (status == GOT_STATUS_CONFLICT)
2599 if (status == GOT_STATUS_OBSTRUCTED)
2601 if (status == GOT_STATUS_CANNOT_UPDATE)
2604 while (path[0] == '/')
2606 printf("%c %s\n", status, path);
2610 static const struct got_error *
2611 switch_head_ref(struct got_reference *head_ref,
2612 struct got_object_id *commit_id, struct got_worktree *worktree,
2613 struct got_repository *repo)
2615 const struct got_error *err = NULL;
2617 int ref_has_moved = 0;
2619 /* Trivial case: switching between two different references. */
2620 if (strcmp(got_ref_get_name(head_ref),
2621 got_worktree_get_head_ref_name(worktree)) != 0) {
2622 printf("Switching work tree from %s to %s\n",
2623 got_worktree_get_head_ref_name(worktree),
2624 got_ref_get_name(head_ref));
2625 return got_worktree_set_head_ref(worktree, head_ref);
2628 err = check_linear_ancestry(commit_id,
2629 got_worktree_get_base_commit_id(worktree), 0, repo);
2631 if (err->code != GOT_ERR_ANCESTRY)
2638 /* Switching to a rebased branch with the same reference name. */
2639 err = got_object_id_str(&base_id_str,
2640 got_worktree_get_base_commit_id(worktree));
2643 printf("Reference %s now points at a different branch\n",
2644 got_worktree_get_head_ref_name(worktree));
2645 printf("Switching work tree from %s to %s\n", base_id_str,
2646 got_worktree_get_head_ref_name(worktree));
2650 static const struct got_error *
2651 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2653 const struct got_error *err;
2656 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2660 return got_error(GOT_ERR_REBASING);
2662 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2666 return got_error(GOT_ERR_HISTEDIT_BUSY);
2671 static const struct got_error *
2672 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2673 char *argv[], struct got_worktree *worktree)
2675 const struct got_error *err = NULL;
2682 return got_error_from_errno("strdup");
2683 return got_pathlist_append(paths, path, NULL);
2686 for (i = 0; i < argc; i++) {
2687 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2690 err = got_pathlist_append(paths, path, NULL);
2700 static const struct got_error *
2701 wrap_not_worktree_error(const struct got_error *orig_err,
2702 const char *cmdname, const char *path)
2704 const struct got_error *err;
2705 struct got_repository *repo;
2706 static char msg[512];
2708 err = got_repo_open(&repo, path, NULL);
2712 snprintf(msg, sizeof(msg),
2713 "'got %s' needs a work tree in addition to a git repository\n"
2714 "Work trees can be checked out from this Git repository with "
2716 "The got(1) manual page contains more information.", cmdname);
2717 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2718 got_repo_close(repo);
2722 static const struct got_error *
2723 cmd_update(int argc, char *argv[])
2725 const struct got_error *error = NULL;
2726 struct got_repository *repo = NULL;
2727 struct got_worktree *worktree = NULL;
2728 char *worktree_path = NULL;
2729 struct got_object_id *commit_id = NULL;
2730 char *commit_id_str = NULL;
2731 const char *branch_name = NULL;
2732 struct got_reference *head_ref = NULL;
2733 struct got_pathlist_head paths;
2734 struct got_pathlist_entry *pe;
2736 struct got_update_progress_arg upa;
2740 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2743 branch_name = optarg;
2746 commit_id_str = strdup(optarg);
2747 if (commit_id_str == NULL)
2748 return got_error_from_errno("strdup");
2760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2761 "unveil", NULL) == -1)
2764 worktree_path = getcwd(NULL, 0);
2765 if (worktree_path == NULL) {
2766 error = got_error_from_errno("getcwd");
2769 error = got_worktree_open(&worktree, worktree_path);
2771 if (error->code == GOT_ERR_NOT_WORKTREE)
2772 error = wrap_not_worktree_error(error, "update",
2777 error = check_rebase_or_histedit_in_progress(worktree);
2781 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2786 error = apply_unveil(got_repo_get_path(repo), 0,
2787 got_worktree_get_root_path(worktree));
2791 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2795 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2796 got_worktree_get_head_ref_name(worktree), 0);
2799 if (commit_id_str == NULL) {
2800 error = got_ref_resolve(&commit_id, repo, head_ref);
2803 error = got_object_id_str(&commit_id_str, commit_id);
2807 error = got_repo_match_object_id(&commit_id, NULL,
2808 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2809 free(commit_id_str);
2810 commit_id_str = NULL;
2813 error = got_object_id_str(&commit_id_str, commit_id);
2819 struct got_object_id *head_commit_id;
2820 TAILQ_FOREACH(pe, &paths, entry) {
2821 if (pe->path_len == 0)
2823 error = got_error_msg(GOT_ERR_BAD_PATH,
2824 "switching between branches requires that "
2825 "the entire work tree gets updated");
2828 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2831 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2833 free(head_commit_id);
2836 error = check_same_branch(commit_id, head_ref, NULL, repo);
2839 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2843 error = check_linear_ancestry(commit_id,
2844 got_worktree_get_base_commit_id(worktree), 0, repo);
2845 if (error != NULL) {
2846 if (error->code == GOT_ERR_ANCESTRY)
2847 error = got_error(GOT_ERR_BRANCH_MOVED);
2850 error = check_same_branch(commit_id, head_ref, NULL, repo);
2855 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2857 error = got_worktree_set_base_commit_id(worktree, repo,
2863 memset(&upa, 0, sizeof(upa));
2864 error = got_worktree_checkout_files(worktree, &paths, repo,
2865 update_progress, &upa, check_cancelled, NULL);
2869 if (upa.did_something)
2870 printf("Updated to commit %s\n", commit_id_str);
2872 printf("Already up-to-date\n");
2873 print_update_progress_stats(&upa);
2875 free(worktree_path);
2876 TAILQ_FOREACH(pe, &paths, entry)
2877 free((char *)pe->path);
2878 got_pathlist_free(&paths);
2880 free(commit_id_str);
2884 static const struct got_error *
2885 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2886 const char *path, int diff_context, int ignore_whitespace,
2887 struct got_repository *repo)
2889 const struct got_error *err = NULL;
2890 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2893 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2898 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2902 while (path[0] == '/')
2904 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2905 ignore_whitespace, stdout);
2908 got_object_blob_close(blob1);
2909 got_object_blob_close(blob2);
2913 static const struct got_error *
2914 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2915 const char *path, int diff_context, int ignore_whitespace,
2916 struct got_repository *repo)
2918 const struct got_error *err = NULL;
2919 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2920 struct got_diff_blob_output_unidiff_arg arg;
2923 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2928 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2932 arg.diff_context = diff_context;
2933 arg.ignore_whitespace = ignore_whitespace;
2934 arg.outfile = stdout;
2935 while (path[0] == '/')
2937 err = got_diff_tree(tree1, tree2, path, path, repo,
2938 got_diff_blob_output_unidiff, &arg, 1);
2941 got_object_tree_close(tree1);
2943 got_object_tree_close(tree2);
2947 static const struct got_error *
2948 get_changed_paths(struct got_pathlist_head *paths,
2949 struct got_commit_object *commit, struct got_repository *repo)
2951 const struct got_error *err = NULL;
2952 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2953 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2954 struct got_object_qid *qid;
2956 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2958 struct got_commit_object *pcommit;
2959 err = got_object_open_as_commit(&pcommit, repo,
2964 tree_id1 = got_object_commit_get_tree_id(pcommit);
2965 got_object_commit_close(pcommit);
2970 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2975 tree_id2 = got_object_commit_get_tree_id(commit);
2976 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2980 err = got_diff_tree(tree1, tree2, "", "", repo,
2981 got_diff_tree_collect_changed_paths, paths, 0);
2984 got_object_tree_close(tree1);
2986 got_object_tree_close(tree2);
2990 static const struct got_error *
2991 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2992 const char *path, int diff_context, struct got_repository *repo)
2994 const struct got_error *err = NULL;
2995 struct got_commit_object *pcommit = NULL;
2996 char *id_str1 = NULL, *id_str2 = NULL;
2997 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2998 struct got_object_qid *qid;
3000 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3002 err = got_object_open_as_commit(&pcommit, repo,
3008 if (path && path[0] != '\0') {
3010 err = got_object_id_by_path(&obj_id2, repo, id, path);
3013 err = got_object_id_str(&id_str2, obj_id2);
3019 err = got_object_id_by_path(&obj_id1, repo,
3022 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3027 err = got_object_id_str(&id_str1, obj_id1);
3034 err = got_object_get_type(&obj_type, repo, obj_id2);
3039 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3041 case GOT_OBJ_TYPE_BLOB:
3042 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3045 case GOT_OBJ_TYPE_TREE:
3046 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3050 err = got_error(GOT_ERR_OBJ_TYPE);
3056 obj_id2 = got_object_commit_get_tree_id(commit);
3057 err = got_object_id_str(&id_str2, obj_id2);
3060 obj_id1 = got_object_commit_get_tree_id(pcommit);
3061 err = got_object_id_str(&id_str1, obj_id1);
3064 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3065 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3071 got_object_commit_close(pcommit);
3076 get_datestr(time_t *time, char *datebuf)
3078 struct tm mytm, *tm;
3081 tm = gmtime_r(time, &mytm);
3084 s = asctime_r(tm, datebuf);
3087 p = strchr(s, '\n');
3093 static const struct got_error *
3094 match_logmsg(int *have_match, struct got_object_id *id,
3095 struct got_commit_object *commit, regex_t *regex)
3097 const struct got_error *err = NULL;
3098 regmatch_t regmatch;
3099 char *id_str = NULL, *logmsg = NULL;
3103 err = got_object_id_str(&id_str, id);
3107 err = got_object_commit_get_logmsg(&logmsg, commit);
3111 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3120 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3123 regmatch_t regmatch;
3124 struct got_pathlist_entry *pe;
3128 TAILQ_FOREACH(pe, changed_paths, entry) {
3129 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3136 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3138 static const struct got_error *
3139 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3140 struct got_repository *repo, const char *path,
3141 struct got_pathlist_head *changed_paths, int show_patch,
3142 int diff_context, struct got_reflist_head *refs)
3144 const struct got_error *err = NULL;
3145 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3147 time_t committer_time;
3148 const char *author, *committer;
3149 char *refs_str = NULL;
3150 struct got_reflist_entry *re;
3152 SIMPLEQ_FOREACH(re, refs, entry) {
3155 struct got_tag_object *tag = NULL;
3158 name = got_ref_get_name(re->ref);
3159 if (strcmp(name, GOT_REF_HEAD) == 0)
3161 if (strncmp(name, "refs/", 5) == 0)
3163 if (strncmp(name, "got/", 4) == 0)
3165 if (strncmp(name, "heads/", 6) == 0)
3167 if (strncmp(name, "remotes/", 8) == 0) {
3169 s = strstr(name, "/" GOT_REF_HEAD);
3170 if (s != NULL && s[strlen(s)] == '\0')
3173 if (strncmp(name, "tags/", 5) == 0) {
3174 err = got_object_open_as_tag(&tag, repo, re->id);
3176 if (err->code != GOT_ERR_OBJ_TYPE)
3178 /* Ref points at something other than a tag. */
3183 cmp = got_object_id_cmp(tag ?
3184 got_object_tag_get_object_id(tag) : re->id, id);
3186 got_object_tag_close(tag);
3190 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3192 err = got_error_from_errno("asprintf");
3198 err = got_object_id_str(&id_str, id);
3202 printf(GOT_COMMIT_SEP_STR);
3203 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3204 refs_str ? refs_str : "", refs_str ? ")" : "");
3209 printf("from: %s\n", got_object_commit_get_author(commit));
3210 committer_time = got_object_commit_get_committer_time(commit);
3211 datestr = get_datestr(&committer_time, datebuf);
3213 printf("date: %s UTC\n", datestr);
3214 author = got_object_commit_get_author(commit);
3215 committer = got_object_commit_get_committer(commit);
3216 if (strcmp(author, committer) != 0)
3217 printf("via: %s\n", committer);
3218 if (got_object_commit_get_nparents(commit) > 1) {
3219 const struct got_object_id_queue *parent_ids;
3220 struct got_object_qid *qid;
3222 parent_ids = got_object_commit_get_parent_ids(commit);
3223 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3224 err = got_object_id_str(&id_str, qid->id);
3227 printf("parent %d: %s\n", n++, id_str);
3232 err = got_object_commit_get_logmsg(&logmsg0, commit);
3238 line = strsep(&logmsg, "\n");
3240 printf(" %s\n", line);
3244 if (changed_paths) {
3245 struct got_pathlist_entry *pe;
3246 TAILQ_FOREACH(pe, changed_paths, entry) {
3247 struct got_diff_changed_path *cp = pe->data;
3248 printf(" %c %s\n", cp->status, pe->path);
3253 err = print_patch(commit, id, path, diff_context, repo);
3258 if (fflush(stdout) != 0 && err == NULL)
3259 err = got_error_from_errno("fflush");
3263 static const struct got_error *
3264 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3265 struct got_repository *repo, const char *path, int show_changed_paths,
3266 int show_patch, const char *search_pattern, int diff_context, int limit,
3267 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3269 const struct got_error *err;
3270 struct got_commit_graph *graph;
3273 struct got_object_id_queue reversed_commits;
3274 struct got_object_qid *qid;
3275 struct got_commit_object *commit;
3276 struct got_pathlist_head changed_paths;
3277 struct got_pathlist_entry *pe;
3279 SIMPLEQ_INIT(&reversed_commits);
3280 TAILQ_INIT(&changed_paths);
3282 if (search_pattern && regcomp(®ex, search_pattern,
3283 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3284 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3286 err = got_commit_graph_open(&graph, path, !log_branches);
3289 err = got_commit_graph_iter_start(graph, root_id, repo,
3290 check_cancelled, NULL);
3294 struct got_object_id *id;
3296 if (sigint_received || sigpipe_received)
3299 err = got_commit_graph_iter_next(&id, graph, repo,
3300 check_cancelled, NULL);
3302 if (err->code == GOT_ERR_ITER_COMPLETED)
3309 err = got_object_open_as_commit(&commit, repo, id);
3313 if (show_changed_paths && !reverse_display_order) {
3314 err = get_changed_paths(&changed_paths, commit, repo);
3319 if (search_pattern) {
3320 err = match_logmsg(&have_match, id, commit, ®ex);
3322 got_object_commit_close(commit);
3325 if (have_match == 0 && show_changed_paths)
3326 match_changed_paths(&have_match,
3327 &changed_paths, ®ex);
3328 if (have_match == 0) {
3329 got_object_commit_close(commit);
3330 TAILQ_FOREACH(pe, &changed_paths, entry) {
3331 free((char *)pe->path);
3334 got_pathlist_free(&changed_paths);
3339 if (reverse_display_order) {
3340 err = got_object_qid_alloc(&qid, id);
3343 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3344 got_object_commit_close(commit);
3346 err = print_commit(commit, id, repo, path,
3347 show_changed_paths ? &changed_paths : NULL,
3348 show_patch, diff_context, refs);
3349 got_object_commit_close(commit);
3353 if ((limit && --limit == 0) ||
3354 (end_id && got_object_id_cmp(id, end_id) == 0))
3357 TAILQ_FOREACH(pe, &changed_paths, entry) {
3358 free((char *)pe->path);
3361 got_pathlist_free(&changed_paths);
3363 if (reverse_display_order) {
3364 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3365 err = got_object_open_as_commit(&commit, repo, qid->id);
3368 if (show_changed_paths) {
3369 err = get_changed_paths(&changed_paths,
3374 err = print_commit(commit, qid->id, repo, path,
3375 show_changed_paths ? &changed_paths : NULL,
3376 show_patch, diff_context, refs);
3377 got_object_commit_close(commit);
3380 TAILQ_FOREACH(pe, &changed_paths, entry) {
3381 free((char *)pe->path);
3384 got_pathlist_free(&changed_paths);
3388 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3389 qid = SIMPLEQ_FIRST(&reversed_commits);
3390 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3391 got_object_qid_free(qid);
3393 TAILQ_FOREACH(pe, &changed_paths, entry) {
3394 free((char *)pe->path);
3397 got_pathlist_free(&changed_paths);
3400 got_commit_graph_close(graph);
3407 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3408 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3409 "[-R] [path]\n", getprogname());
3414 get_default_log_limit(void)
3416 const char *got_default_log_limit;
3420 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3421 if (got_default_log_limit == NULL)
3423 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3429 static const struct got_error *
3430 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3431 struct got_repository *repo)
3433 const struct got_error *err = NULL;
3434 struct got_reference *ref;
3438 err = got_ref_open(&ref, repo, commit_arg, 0);
3441 err = got_ref_resolve(id, repo, ref);
3445 err = got_object_get_type(&obj_type, repo, *id);
3448 if (obj_type == GOT_OBJ_TYPE_TAG) {
3449 struct got_tag_object *tag;
3450 err = got_object_open_as_tag(&tag, repo, *id);
3453 if (got_object_tag_get_object_type(tag) !=
3454 GOT_OBJ_TYPE_COMMIT) {
3455 got_object_tag_close(tag);
3456 return got_error(GOT_ERR_OBJ_TYPE);
3459 *id = got_object_id_dup(
3460 got_object_tag_get_object_id(tag));
3462 err = got_error_from_errno(
3463 "got_object_id_dup");
3464 got_object_tag_close(tag);
3467 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3468 return got_error(GOT_ERR_OBJ_TYPE);
3470 err = got_repo_match_object_id_prefix(id, commit_arg,
3471 GOT_OBJ_TYPE_COMMIT, repo);
3477 static const struct got_error *
3478 cmd_log(int argc, char *argv[])
3480 const struct got_error *error;
3481 struct got_repository *repo = NULL;
3482 struct got_worktree *worktree = NULL;
3483 struct got_object_id *start_id = NULL, *end_id = NULL;
3484 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3485 const char *start_commit = NULL, *end_commit = NULL;
3486 const char *search_pattern = NULL;
3487 int diff_context = -1, ch;
3488 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3489 int reverse_display_order = 0;
3491 struct got_reflist_head refs;
3493 SIMPLEQ_INIT(&refs);
3496 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3502 limit = get_default_log_limit();
3504 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3510 show_changed_paths = 1;
3513 start_commit = optarg;
3516 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3519 err(1, "-C option %s", errstr);
3522 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3524 err(1, "-l option %s", errstr);
3530 repo_path = realpath(optarg, NULL);
3531 if (repo_path == NULL)
3532 return got_error_from_errno2("realpath",
3534 got_path_strip_trailing_slashes(repo_path);
3537 reverse_display_order = 1;
3540 search_pattern = optarg;
3543 end_commit = optarg;
3554 if (diff_context == -1)
3556 else if (!show_patch)
3557 errx(1, "-C reguires -p");
3559 cwd = getcwd(NULL, 0);
3561 error = got_error_from_errno("getcwd");
3565 if (repo_path == NULL) {
3566 error = got_worktree_open(&worktree, cwd);
3567 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3575 error = got_error_from_errno("strdup");
3578 } else if (argc == 1) {
3580 error = got_worktree_resolve_path(&path, worktree,
3585 path = strdup(argv[0]);
3587 error = got_error_from_errno("strdup");
3594 if (repo_path == NULL) {
3595 repo_path = worktree ?
3596 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3598 if (repo_path == NULL) {
3599 error = got_error_from_errno("strdup");
3603 error = got_repo_open(&repo, repo_path, NULL);
3607 error = apply_unveil(got_repo_get_path(repo), 1,
3608 worktree ? got_worktree_get_root_path(worktree) : NULL);
3612 if (start_commit == NULL) {
3613 struct got_reference *head_ref;
3614 struct got_commit_object *commit = NULL;
3615 error = got_ref_open(&head_ref, repo,
3616 worktree ? got_worktree_get_head_ref_name(worktree)
3620 error = got_ref_resolve(&start_id, repo, head_ref);
3621 got_ref_close(head_ref);
3624 error = got_object_open_as_commit(&commit, repo,
3628 got_object_commit_close(commit);
3630 error = resolve_commit_arg(&start_id, start_commit, repo);
3634 if (end_commit != NULL) {
3635 error = resolve_commit_arg(&end_id, end_commit, repo);
3641 const char *prefix = got_worktree_get_path_prefix(worktree);
3643 if (asprintf(&p, "%s%s%s", prefix,
3644 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3645 error = got_error_from_errno("asprintf");
3648 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3651 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3656 path = in_repo_path;
3659 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3663 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3664 show_patch, search_pattern, diff_context, limit, log_branches,
3665 reverse_display_order, &refs);
3671 got_worktree_close(worktree);
3673 const struct got_error *repo_error;
3674 repo_error = got_repo_close(repo);
3678 got_ref_list_free(&refs);
3685 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3686 "[-w] [object1 object2 | path]\n", getprogname());
3690 struct print_diff_arg {
3691 struct got_repository *repo;
3692 struct got_worktree *worktree;
3697 int ignore_whitespace;
3701 * Create a file which contains the target path of a symlink so we can feed
3702 * it as content to the diff engine.
3704 static const struct got_error *
3705 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3706 const char *abspath)
3708 const struct got_error *err = NULL;
3709 char target_path[PATH_MAX];
3710 ssize_t target_len, outlen;
3715 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3716 if (target_len == -1)
3717 return got_error_from_errno2("readlinkat", abspath);
3719 target_len = readlink(abspath, target_path, PATH_MAX);
3720 if (target_len == -1)
3721 return got_error_from_errno2("readlink", abspath);
3724 *fd = got_opentempfd();
3726 return got_error_from_errno("got_opentempfd");
3728 outlen = write(*fd, target_path, target_len);
3730 err = got_error_from_errno("got_opentempfd");
3734 if (lseek(*fd, 0, SEEK_SET) == -1) {
3735 err = got_error_from_errno2("lseek", abspath);
3746 static const struct got_error *
3747 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3748 const char *path, struct got_object_id *blob_id,
3749 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3750 int dirfd, const char *de_name)
3752 struct print_diff_arg *a = arg;
3753 const struct got_error *err = NULL;
3754 struct got_blob_object *blob1 = NULL;
3757 char *abspath = NULL, *label1 = NULL;
3760 if (a->diff_staged) {
3761 if (staged_status != GOT_STATUS_MODIFY &&
3762 staged_status != GOT_STATUS_ADD &&
3763 staged_status != GOT_STATUS_DELETE)
3766 if (staged_status == GOT_STATUS_DELETE)
3768 if (status == GOT_STATUS_NONEXISTENT)
3769 return got_error_set_errno(ENOENT, path);
3770 if (status != GOT_STATUS_MODIFY &&
3771 status != GOT_STATUS_ADD &&
3772 status != GOT_STATUS_DELETE &&
3773 status != GOT_STATUS_CONFLICT)
3777 if (!a->header_shown) {
3778 printf("diff %s %s%s\n", a->id_str,
3779 got_worktree_get_root_path(a->worktree),
3780 a->diff_staged ? " (staged changes)" : "");
3781 a->header_shown = 1;
3784 if (a->diff_staged) {
3785 const char *label1 = NULL, *label2 = NULL;
3786 switch (staged_status) {
3787 case GOT_STATUS_MODIFY:
3791 case GOT_STATUS_ADD:
3794 case GOT_STATUS_DELETE:
3798 return got_error(GOT_ERR_FILE_STATUS);
3800 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3801 label1, label2, a->diff_context, a->ignore_whitespace,
3805 if (staged_status == GOT_STATUS_ADD ||
3806 staged_status == GOT_STATUS_MODIFY) {
3808 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3812 err = got_object_id_str(&id_str, staged_blob_id);
3815 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3816 err = got_error_from_errno("asprintf");
3821 } else if (status != GOT_STATUS_ADD) {
3822 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3827 if (status != GOT_STATUS_DELETE) {
3828 if (asprintf(&abspath, "%s/%s",
3829 got_worktree_get_root_path(a->worktree), path) == -1) {
3830 err = got_error_from_errno("asprintf");
3835 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3837 if (errno != ELOOP) {
3838 err = got_error_from_errno2("openat",
3842 err = get_symlink_target_file(&fd, dirfd,
3848 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3850 if (errno != ELOOP) {
3851 err = got_error_from_errno2("open",
3855 err = get_symlink_target_file(&fd, dirfd,
3861 if (fstat(fd, &sb) == -1) {
3862 err = got_error_from_errno2("fstat", abspath);
3865 f2 = fdopen(fd, "r");
3867 err = got_error_from_errno2("fdopen", abspath);
3874 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3875 a->diff_context, a->ignore_whitespace, stdout);
3878 got_object_blob_close(blob1);
3879 if (f2 && fclose(f2) == EOF && err == NULL)
3880 err = got_error_from_errno("fclose");
3881 if (fd != -1 && close(fd) == -1 && err == NULL)
3882 err = got_error_from_errno("close");
3887 static const struct got_error *
3888 cmd_diff(int argc, char *argv[])
3890 const struct got_error *error;
3891 struct got_repository *repo = NULL;
3892 struct got_worktree *worktree = NULL;
3893 char *cwd = NULL, *repo_path = NULL;
3894 struct got_object_id *id1 = NULL, *id2 = NULL;
3895 const char *id_str1 = NULL, *id_str2 = NULL;
3896 char *label1 = NULL, *label2 = NULL;
3898 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3903 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3908 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3911 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3914 err(1, "-C option %s", errstr);
3917 repo_path = realpath(optarg, NULL);
3918 if (repo_path == NULL)
3919 return got_error_from_errno2("realpath",
3921 got_path_strip_trailing_slashes(repo_path);
3927 ignore_whitespace = 1;
3938 cwd = getcwd(NULL, 0);
3940 error = got_error_from_errno("getcwd");
3946 "-r option can't be used when diffing a work tree");
3947 error = got_worktree_open(&worktree, cwd);
3949 if (error->code == GOT_ERR_NOT_WORKTREE)
3950 error = wrap_not_worktree_error(error, "diff",
3954 repo_path = strdup(got_worktree_get_repo_path(worktree));
3955 if (repo_path == NULL) {
3956 error = got_error_from_errno("strdup");
3960 error = got_worktree_resolve_path(&path, worktree,
3967 error = got_error_from_errno("strdup");
3971 } else if (argc == 2) {
3973 errx(1, "-s option can't be used when diffing "
3974 "objects in repository");
3977 if (repo_path == NULL) {
3978 error = got_worktree_open(&worktree, cwd);
3979 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3983 got_worktree_get_repo_path(worktree));
3984 if (repo_path == NULL) {
3985 error = got_error_from_errno("strdup");
3989 repo_path = strdup(cwd);
3990 if (repo_path == NULL) {
3991 error = got_error_from_errno("strdup");
3999 error = got_repo_open(&repo, repo_path, NULL);
4004 error = apply_unveil(got_repo_get_path(repo), 1,
4005 worktree ? got_worktree_get_root_path(worktree) : NULL);
4010 struct print_diff_arg arg;
4011 struct got_pathlist_head paths;
4016 error = got_object_id_str(&id_str,
4017 got_worktree_get_base_commit_id(worktree));
4021 arg.worktree = worktree;
4022 arg.diff_context = diff_context;
4023 arg.id_str = id_str;
4024 arg.header_shown = 0;
4025 arg.diff_staged = diff_staged;
4026 arg.ignore_whitespace = ignore_whitespace;
4028 error = got_pathlist_append(&paths, path, NULL);
4032 error = got_worktree_status(worktree, &paths, repo, print_diff,
4033 &arg, check_cancelled, NULL);
4035 got_pathlist_free(&paths);
4039 error = got_repo_match_object_id(&id1, &label1, id_str1,
4040 GOT_OBJ_TYPE_ANY, 1, repo);
4044 error = got_repo_match_object_id(&id2, &label2, id_str2,
4045 GOT_OBJ_TYPE_ANY, 1, repo);
4049 error = got_object_get_type(&type1, repo, id1);
4053 error = got_object_get_type(&type2, repo, id2);
4057 if (type1 != type2) {
4058 error = got_error(GOT_ERR_OBJ_TYPE);
4063 case GOT_OBJ_TYPE_BLOB:
4064 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4065 diff_context, ignore_whitespace, repo, stdout);
4067 case GOT_OBJ_TYPE_TREE:
4068 error = got_diff_objects_as_trees(id1, id2, "", "",
4069 diff_context, ignore_whitespace, repo, stdout);
4071 case GOT_OBJ_TYPE_COMMIT:
4072 printf("diff %s %s\n", label1, label2);
4073 error = got_diff_objects_as_commits(id1, id2, diff_context,
4074 ignore_whitespace, repo, stdout);
4077 error = got_error(GOT_ERR_OBJ_TYPE);
4086 got_worktree_close(worktree);
4088 const struct got_error *repo_error;
4089 repo_error = got_repo_close(repo);
4100 "usage: %s blame [-c commit] [-r repository-path] path\n",
4109 char datebuf[11]; /* YYYY-MM-DD + NUL */
4112 struct blame_cb_args {
4113 struct blame_line *lines;
4117 off_t *line_offsets;
4119 struct got_repository *repo;
4122 static const struct got_error *
4123 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4125 const struct got_error *err = NULL;
4126 struct blame_cb_args *a = arg;
4127 struct blame_line *bline;
4129 size_t linesize = 0;
4130 struct got_commit_object *commit = NULL;
4133 time_t committer_time;
4135 if (nlines != a->nlines ||
4136 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4137 return got_error(GOT_ERR_RANGE);
4139 if (sigint_received)
4140 return got_error(GOT_ERR_ITER_COMPLETED);
4143 return NULL; /* no change in this commit */
4145 /* Annotate this line. */
4146 bline = &a->lines[lineno - 1];
4147 if (bline->annotated)
4149 err = got_object_id_str(&bline->id_str, id);
4153 err = got_object_open_as_commit(&commit, a->repo, id);
4157 bline->committer = strdup(got_object_commit_get_committer(commit));
4158 if (bline->committer == NULL) {
4159 err = got_error_from_errno("strdup");
4163 committer_time = got_object_commit_get_committer_time(commit);
4164 if (localtime_r(&committer_time, &tm) == NULL)
4165 return got_error_from_errno("localtime_r");
4166 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4167 &tm) >= sizeof(bline->datebuf)) {
4168 err = got_error(GOT_ERR_NO_SPACE);
4171 bline->annotated = 1;
4173 /* Print lines annotated so far. */
4174 bline = &a->lines[a->lineno_cur - 1];
4175 if (!bline->annotated)
4178 offset = a->line_offsets[a->lineno_cur - 1];
4179 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4180 err = got_error_from_errno("fseeko");
4184 while (bline->annotated) {
4185 char *smallerthan, *at, *nl, *committer;
4188 if (getline(&line, &linesize, a->f) == -1) {
4190 err = got_error_from_errno("getline");
4194 committer = bline->committer;
4195 smallerthan = strchr(committer, '<');
4196 if (smallerthan && smallerthan[1] != '\0')
4197 committer = smallerthan + 1;
4198 at = strchr(committer, '@');
4201 len = strlen(committer);
4203 committer[8] = '\0';
4205 nl = strchr(line, '\n');
4208 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4209 bline->id_str, bline->datebuf, committer, line);
4212 bline = &a->lines[a->lineno_cur - 1];
4216 got_object_commit_close(commit);
4221 static const struct got_error *
4222 cmd_blame(int argc, char *argv[])
4224 const struct got_error *error;
4225 struct got_repository *repo = NULL;
4226 struct got_worktree *worktree = NULL;
4227 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4228 char *link_target = NULL;
4229 struct got_object_id *obj_id = NULL;
4230 struct got_object_id *commit_id = NULL;
4231 struct got_blob_object *blob = NULL;
4232 char *commit_id_str = NULL;
4233 struct blame_cb_args bca;
4234 int ch, obj_type, i;
4237 memset(&bca, 0, sizeof(bca));
4240 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4245 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4248 commit_id_str = optarg;
4251 repo_path = realpath(optarg, NULL);
4252 if (repo_path == NULL)
4253 return got_error_from_errno2("realpath",
4255 got_path_strip_trailing_slashes(repo_path);
4271 cwd = getcwd(NULL, 0);
4273 error = got_error_from_errno("getcwd");
4276 if (repo_path == NULL) {
4277 error = got_worktree_open(&worktree, cwd);
4278 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4284 strdup(got_worktree_get_repo_path(worktree));
4285 if (repo_path == NULL) {
4286 error = got_error_from_errno("strdup");
4291 repo_path = strdup(cwd);
4292 if (repo_path == NULL) {
4293 error = got_error_from_errno("strdup");
4299 error = got_repo_open(&repo, repo_path, NULL);
4303 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4308 const char *prefix = got_worktree_get_path_prefix(worktree);
4309 char *p, *worktree_subdir = cwd +
4310 strlen(got_worktree_get_root_path(worktree));
4311 if (asprintf(&p, "%s%s%s%s%s",
4312 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4313 worktree_subdir, worktree_subdir[0] ? "/" : "",
4315 error = got_error_from_errno("asprintf");
4318 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4321 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4326 if (commit_id_str == NULL) {
4327 struct got_reference *head_ref;
4328 error = got_ref_open(&head_ref, repo, worktree ?
4329 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4332 error = got_ref_resolve(&commit_id, repo, head_ref);
4333 got_ref_close(head_ref);
4337 error = got_repo_match_object_id(&commit_id, NULL,
4338 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4343 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4348 error = got_object_id_by_path(&obj_id, repo, commit_id,
4349 link_target ? link_target : in_repo_path);
4353 error = got_object_get_type(&obj_type, repo, obj_id);
4357 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4358 error = got_error_path(link_target ? link_target : in_repo_path,
4363 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4366 bca.f = got_opentemp();
4367 if (bca.f == NULL) {
4368 error = got_error_from_errno("got_opentemp");
4371 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4372 &bca.line_offsets, bca.f, blob);
4373 if (error || bca.nlines == 0)
4376 /* Don't include \n at EOF in the blame line count. */
4377 if (bca.line_offsets[bca.nlines - 1] == filesize)
4380 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4381 if (bca.lines == NULL) {
4382 error = got_error_from_errno("calloc");
4386 bca.nlines_prec = 0;
4394 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4395 repo, blame_cb, &bca, check_cancelled, NULL);
4404 got_object_blob_close(blob);
4406 got_worktree_close(worktree);
4408 const struct got_error *repo_error;
4409 repo_error = got_repo_close(repo);
4414 for (i = 0; i < bca.nlines; i++) {
4415 struct blame_line *bline = &bca.lines[i];
4416 free(bline->id_str);
4417 free(bline->committer);
4421 free(bca.line_offsets);
4422 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4423 error = got_error_from_errno("fclose");
4431 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4436 static const struct got_error *
4437 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4438 const char *root_path, struct got_repository *repo)
4440 const struct got_error *err = NULL;
4441 int is_root_path = (strcmp(path, root_path) == 0);
4442 const char *modestr = "";
4443 mode_t mode = got_tree_entry_get_mode(te);
4444 char *link_target = NULL;
4446 path += strlen(root_path);
4447 while (path[0] == '/')
4450 if (got_object_tree_entry_is_submodule(te))
4452 else if (S_ISLNK(mode)) {
4455 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4458 for (i = 0; i < strlen(link_target); i++) {
4459 if (!isprint((unsigned char)link_target[i]))
4460 link_target[i] = '?';
4465 else if (S_ISDIR(mode))
4467 else if (mode & S_IXUSR)
4470 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4471 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4472 link_target ? " -> ": "", link_target ? link_target : "");
4478 static const struct got_error *
4479 print_tree(const char *path, struct got_object_id *commit_id,
4480 int show_ids, int recurse, const char *root_path,
4481 struct got_repository *repo)
4483 const struct got_error *err = NULL;
4484 struct got_object_id *tree_id = NULL;
4485 struct got_tree_object *tree = NULL;
4488 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4492 err = got_object_open_as_tree(&tree, repo, tree_id);
4495 nentries = got_object_tree_get_nentries(tree);
4496 for (i = 0; i < nentries; i++) {
4497 struct got_tree_entry *te;
4500 if (sigint_received || sigpipe_received)
4503 te = got_object_tree_get_entry(tree, i);
4506 err = got_object_id_str(&id_str,
4507 got_tree_entry_get_id(te));
4510 if (asprintf(&id, "%s ", id_str) == -1) {
4511 err = got_error_from_errno("asprintf");
4517 err = print_entry(te, id, path, root_path, repo);
4522 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4524 if (asprintf(&child_path, "%s%s%s", path,
4525 path[0] == '/' && path[1] == '\0' ? "" : "/",
4526 got_tree_entry_get_name(te)) == -1) {
4527 err = got_error_from_errno("asprintf");
4530 err = print_tree(child_path, commit_id, show_ids, 1,
4539 got_object_tree_close(tree);
4544 static const struct got_error *
4545 cmd_tree(int argc, char *argv[])
4547 const struct got_error *error;
4548 struct got_repository *repo = NULL;
4549 struct got_worktree *worktree = NULL;
4550 const char *path, *refname = NULL;
4551 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4552 struct got_object_id *commit_id = NULL;
4553 char *commit_id_str = NULL;
4554 int show_ids = 0, recurse = 0;
4558 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4563 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4566 commit_id_str = optarg;
4569 repo_path = realpath(optarg, NULL);
4570 if (repo_path == NULL)
4571 return got_error_from_errno2("realpath",
4573 got_path_strip_trailing_slashes(repo_path);
4597 cwd = getcwd(NULL, 0);
4599 error = got_error_from_errno("getcwd");
4602 if (repo_path == NULL) {
4603 error = got_worktree_open(&worktree, cwd);
4604 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4610 strdup(got_worktree_get_repo_path(worktree));
4611 if (repo_path == NULL)
4612 error = got_error_from_errno("strdup");
4616 repo_path = strdup(cwd);
4617 if (repo_path == NULL) {
4618 error = got_error_from_errno("strdup");
4624 error = got_repo_open(&repo, repo_path, NULL);
4628 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4634 char *p, *worktree_subdir = cwd +
4635 strlen(got_worktree_get_root_path(worktree));
4636 if (asprintf(&p, "%s/%s",
4637 got_worktree_get_path_prefix(worktree),
4638 worktree_subdir) == -1) {
4639 error = got_error_from_errno("asprintf");
4642 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4649 if (in_repo_path == NULL) {
4650 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4655 if (commit_id_str == NULL) {
4656 struct got_reference *head_ref;
4658 refname = got_worktree_get_head_ref_name(worktree);
4660 refname = GOT_REF_HEAD;
4661 error = got_ref_open(&head_ref, repo, refname, 0);
4664 error = got_ref_resolve(&commit_id, repo, head_ref);
4665 got_ref_close(head_ref);
4669 error = got_repo_match_object_id(&commit_id, NULL,
4670 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4675 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4676 in_repo_path, repo);
4683 got_worktree_close(worktree);
4685 const struct got_error *repo_error;
4686 repo_error = got_repo_close(repo);
4696 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4701 static const struct got_error *
4702 print_status(void *arg, unsigned char status, unsigned char staged_status,
4703 const char *path, struct got_object_id *blob_id,
4704 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4705 int dirfd, const char *de_name)
4707 if (status == staged_status && (status == GOT_STATUS_DELETE))
4708 status = GOT_STATUS_NO_CHANGE;
4710 char *status_codes = arg;
4711 size_t ncodes = strlen(status_codes);
4713 for (i = 0; i < ncodes ; i++) {
4714 if (status == status_codes[i] ||
4715 staged_status == status_codes[i])
4721 printf("%c%c %s\n", status, staged_status, path);
4725 static const struct got_error *
4726 cmd_status(int argc, char *argv[])
4728 const struct got_error *error = NULL;
4729 struct got_repository *repo = NULL;
4730 struct got_worktree *worktree = NULL;
4731 char *cwd = NULL, *status_codes = NULL;;
4732 struct got_pathlist_head paths;
4733 struct got_pathlist_entry *pe;
4738 while ((ch = getopt(argc, argv, "s:")) != -1) {
4741 for (i = 0; i < strlen(optarg); i++) {
4742 switch (optarg[i]) {
4743 case GOT_STATUS_MODIFY:
4744 case GOT_STATUS_ADD:
4745 case GOT_STATUS_DELETE:
4746 case GOT_STATUS_CONFLICT:
4747 case GOT_STATUS_MISSING:
4748 case GOT_STATUS_OBSTRUCTED:
4749 case GOT_STATUS_UNVERSIONED:
4750 case GOT_STATUS_MODE_CHANGE:
4751 case GOT_STATUS_NONEXISTENT:
4754 errx(1, "invalid status code '%c'",
4758 status_codes = optarg;
4770 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4774 cwd = getcwd(NULL, 0);
4776 error = got_error_from_errno("getcwd");
4780 error = got_worktree_open(&worktree, cwd);
4782 if (error->code == GOT_ERR_NOT_WORKTREE)
4783 error = wrap_not_worktree_error(error, "status", cwd);
4787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4792 error = apply_unveil(got_repo_get_path(repo), 1,
4793 got_worktree_get_root_path(worktree));
4797 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4801 error = got_worktree_status(worktree, &paths, repo, print_status,
4802 status_codes, check_cancelled, NULL);
4804 TAILQ_FOREACH(pe, &paths, entry)
4805 free((char *)pe->path);
4806 got_pathlist_free(&paths);
4815 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4821 static const struct got_error *
4822 list_refs(struct got_repository *repo, const char *refname)
4824 static const struct got_error *err = NULL;
4825 struct got_reflist_head refs;
4826 struct got_reflist_entry *re;
4828 SIMPLEQ_INIT(&refs);
4829 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4833 SIMPLEQ_FOREACH(re, &refs, entry) {
4835 refstr = got_ref_to_str(re->ref);
4837 return got_error_from_errno("got_ref_to_str");
4838 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4842 got_ref_list_free(&refs);
4846 static const struct got_error *
4847 delete_ref(struct got_repository *repo, const char *refname)
4849 const struct got_error *err = NULL;
4850 struct got_reference *ref;
4852 err = got_ref_open(&ref, repo, refname, 0);
4856 err = got_ref_delete(ref, repo);
4861 static const struct got_error *
4862 add_ref(struct got_repository *repo, const char *refname, const char *target)
4864 const struct got_error *err = NULL;
4865 struct got_object_id *id;
4866 struct got_reference *ref = NULL;
4869 * Don't let the user create a reference name with a leading '-'.
4870 * While technically a valid reference name, this case is usually
4871 * an unintended typo.
4873 if (refname[0] == '-')
4874 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4876 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4879 struct got_reference *target_ref;
4881 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4883 err = got_ref_open(&target_ref, repo, target, 0);
4886 err = got_ref_resolve(&id, repo, target_ref);
4887 got_ref_close(target_ref);
4892 err = got_ref_alloc(&ref, refname, id);
4896 err = got_ref_write(ref, repo);
4904 static const struct got_error *
4905 add_symref(struct got_repository *repo, const char *refname, const char *target)
4907 const struct got_error *err = NULL;
4908 struct got_reference *ref = NULL;
4909 struct got_reference *target_ref = NULL;
4912 * Don't let the user create a reference name with a leading '-'.
4913 * While technically a valid reference name, this case is usually
4914 * an unintended typo.
4916 if (refname[0] == '-')
4917 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4919 err = got_ref_open(&target_ref, repo, target, 0);
4923 err = got_ref_alloc_symref(&ref, refname, target_ref);
4927 err = got_ref_write(ref, repo);
4930 got_ref_close(target_ref);
4936 static const struct got_error *
4937 cmd_ref(int argc, char *argv[])
4939 const struct got_error *error = NULL;
4940 struct got_repository *repo = NULL;
4941 struct got_worktree *worktree = NULL;
4942 char *cwd = NULL, *repo_path = NULL;
4943 int ch, do_list = 0, do_delete = 0;
4944 const char *obj_arg = NULL, *symref_target= NULL;
4945 char *refname = NULL;
4947 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4956 repo_path = realpath(optarg, NULL);
4957 if (repo_path == NULL)
4958 return got_error_from_errno2("realpath",
4960 got_path_strip_trailing_slashes(repo_path);
4966 symref_target = optarg;
4974 if (obj_arg && do_list)
4975 errx(1, "-c and -l options are mutually exclusive");
4976 if (obj_arg && do_delete)
4977 errx(1, "-c and -d options are mutually exclusive");
4978 if (obj_arg && symref_target)
4979 errx(1, "-c and -s options are mutually exclusive");
4980 if (symref_target && do_delete)
4981 errx(1, "-s and -d options are mutually exclusive");
4982 if (symref_target && do_list)
4983 errx(1, "-s and -l options are mutually exclusive");
4984 if (do_delete && do_list)
4985 errx(1, "-d and -l options are mutually exclusive");
4991 if (argc != 0 && argc != 1)
4994 refname = strdup(argv[0]);
4995 if (refname == NULL) {
4996 error = got_error_from_errno("strdup");
5003 refname = strdup(argv[0]);
5004 if (refname == NULL) {
5005 error = got_error_from_errno("strdup");
5011 got_path_strip_trailing_slashes(refname);
5015 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5019 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5020 "sendfd unveil", NULL) == -1)
5024 cwd = getcwd(NULL, 0);
5026 error = got_error_from_errno("getcwd");
5030 if (repo_path == NULL) {
5031 error = got_worktree_open(&worktree, cwd);
5032 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5038 strdup(got_worktree_get_repo_path(worktree));
5039 if (repo_path == NULL)
5040 error = got_error_from_errno("strdup");
5044 repo_path = strdup(cwd);
5045 if (repo_path == NULL) {
5046 error = got_error_from_errno("strdup");
5052 error = got_repo_open(&repo, repo_path, NULL);
5056 error = apply_unveil(got_repo_get_path(repo), do_list,
5057 worktree ? got_worktree_get_root_path(worktree) : NULL);
5062 error = list_refs(repo, refname);
5064 error = delete_ref(repo, refname);
5065 else if (symref_target)
5066 error = add_symref(repo, refname, symref_target);
5068 if (obj_arg == NULL)
5070 error = add_ref(repo, refname, obj_arg);
5075 got_repo_close(repo);
5077 got_worktree_close(worktree);
5087 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5088 "[name]\n", getprogname());
5092 static const struct got_error *
5093 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5094 struct got_reference *ref)
5096 const struct got_error *err = NULL;
5097 const char *refname, *marker = " ";
5100 refname = got_ref_get_name(ref);
5101 if (worktree && strcmp(refname,
5102 got_worktree_get_head_ref_name(worktree)) == 0) {
5103 struct got_object_id *id = NULL;
5105 err = got_ref_resolve(&id, repo, ref);
5108 if (got_object_id_cmp(id,
5109 got_worktree_get_base_commit_id(worktree)) == 0)
5116 if (strncmp(refname, "refs/heads/", 11) == 0)
5118 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5121 refstr = got_ref_to_str(ref);
5123 return got_error_from_errno("got_ref_to_str");
5125 printf("%s%s: %s\n", marker, refname, refstr);
5130 static const struct got_error *
5131 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5133 const char *refname;
5135 if (worktree == NULL)
5136 return got_error(GOT_ERR_NOT_WORKTREE);
5138 refname = got_worktree_get_head_ref_name(worktree);
5140 if (strncmp(refname, "refs/heads/", 11) == 0)
5142 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5145 printf("%s\n", refname);
5150 static const struct got_error *
5151 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5153 static const struct got_error *err = NULL;
5154 struct got_reflist_head refs;
5155 struct got_reflist_entry *re;
5156 struct got_reference *temp_ref = NULL;
5157 int rebase_in_progress, histedit_in_progress;
5159 SIMPLEQ_INIT(&refs);
5162 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5167 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5172 if (rebase_in_progress || histedit_in_progress) {
5173 err = got_ref_open(&temp_ref, repo,
5174 got_worktree_get_head_ref_name(worktree), 0);
5177 list_branch(repo, worktree, temp_ref);
5178 got_ref_close(temp_ref);
5182 err = got_ref_list(&refs, repo, "refs/heads",
5183 got_ref_cmp_by_name, NULL);
5187 SIMPLEQ_FOREACH(re, &refs, entry)
5188 list_branch(repo, worktree, re->ref);
5190 got_ref_list_free(&refs);
5194 static const struct got_error *
5195 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5196 const char *branch_name)
5198 const struct got_error *err = NULL;
5199 struct got_reference *ref = NULL;
5202 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5203 return got_error_from_errno("asprintf");
5205 err = got_ref_open(&ref, repo, refname, 0);
5210 strcmp(got_worktree_get_head_ref_name(worktree),
5211 got_ref_get_name(ref)) == 0) {
5212 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5213 "will not delete this work tree's current branch");
5217 err = got_ref_delete(ref, repo);
5225 static const struct got_error *
5226 add_branch(struct got_repository *repo, const char *branch_name,
5227 struct got_object_id *base_commit_id)
5229 const struct got_error *err = NULL;
5230 struct got_reference *ref = NULL;
5231 char *base_refname = NULL, *refname = NULL;
5234 * Don't let the user create a branch name with a leading '-'.
5235 * While technically a valid reference name, this case is usually
5236 * an unintended typo.
5238 if (branch_name[0] == '-')
5239 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5241 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5242 err = got_error_from_errno("asprintf");
5246 err = got_ref_open(&ref, repo, refname, 0);
5248 err = got_error(GOT_ERR_BRANCH_EXISTS);
5250 } else if (err->code != GOT_ERR_NOT_REF)
5253 err = got_ref_alloc(&ref, refname, base_commit_id);
5257 err = got_ref_write(ref, repo);
5266 static const struct got_error *
5267 cmd_branch(int argc, char *argv[])
5269 const struct got_error *error = NULL;
5270 struct got_repository *repo = NULL;
5271 struct got_worktree *worktree = NULL;
5272 char *cwd = NULL, *repo_path = NULL;
5273 int ch, do_list = 0, do_show = 0, do_update = 1;
5274 const char *delref = NULL, *commit_id_arg = NULL;
5275 struct got_reference *ref = NULL;
5276 struct got_pathlist_head paths;
5277 struct got_pathlist_entry *pe;
5278 struct got_object_id *commit_id = NULL;
5279 char *commit_id_str = NULL;
5283 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5286 commit_id_arg = optarg;
5292 repo_path = realpath(optarg, NULL);
5293 if (repo_path == NULL)
5294 return got_error_from_errno2("realpath",
5296 got_path_strip_trailing_slashes(repo_path);
5310 if (do_list && delref)
5311 errx(1, "-l and -d options are mutually exclusive");
5316 if (!do_list && !delref && argc == 0)
5319 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5320 errx(1, "-c option can only be used when creating a branch");
5322 if (do_list || delref) {
5325 } else if (!do_show && argc != 1)
5329 if (do_list || do_show) {
5330 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5334 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5335 "sendfd unveil", NULL) == -1)
5339 cwd = getcwd(NULL, 0);
5341 error = got_error_from_errno("getcwd");
5345 if (repo_path == NULL) {
5346 error = got_worktree_open(&worktree, cwd);
5347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5353 strdup(got_worktree_get_repo_path(worktree));
5354 if (repo_path == NULL)
5355 error = got_error_from_errno("strdup");
5359 repo_path = strdup(cwd);
5360 if (repo_path == NULL) {
5361 error = got_error_from_errno("strdup");
5367 error = got_repo_open(&repo, repo_path, NULL);
5371 error = apply_unveil(got_repo_get_path(repo), do_list,
5372 worktree ? got_worktree_get_root_path(worktree) : NULL);
5377 error = show_current_branch(repo, worktree);
5379 error = list_branches(repo, worktree);
5381 error = delete_branch(repo, worktree, delref);
5383 if (commit_id_arg == NULL)
5384 commit_id_arg = worktree ?
5385 got_worktree_get_head_ref_name(worktree) :
5387 error = got_repo_match_object_id(&commit_id, NULL,
5388 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5391 error = add_branch(repo, argv[0], commit_id);
5394 if (worktree && do_update) {
5395 struct got_update_progress_arg upa;
5396 char *branch_refname = NULL;
5398 error = got_object_id_str(&commit_id_str, commit_id);
5401 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5405 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5407 error = got_error_from_errno("asprintf");
5410 error = got_ref_open(&ref, repo, branch_refname, 0);
5411 free(branch_refname);
5414 error = switch_head_ref(ref, commit_id, worktree,
5418 error = got_worktree_set_base_commit_id(worktree, repo,
5422 memset(&upa, 0, sizeof(upa));
5423 error = got_worktree_checkout_files(worktree, &paths,
5424 repo, update_progress, &upa, check_cancelled,
5428 if (upa.did_something)
5429 printf("Updated to commit %s\n", commit_id_str);
5430 print_update_progress_stats(&upa);
5437 got_repo_close(repo);
5439 got_worktree_close(worktree);
5443 free(commit_id_str);
5444 TAILQ_FOREACH(pe, &paths, entry)
5445 free((char *)pe->path);
5446 got_pathlist_free(&paths);
5455 "usage: %s tag [-c commit] [-r repository] [-l] "
5456 "[-m message] name\n", getprogname());
5461 static const struct got_error *
5462 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5464 const struct got_error *err = NULL;
5465 struct got_reflist_entry *re, *se, *new;
5466 struct got_object_id *re_id, *se_id;
5467 struct got_tag_object *re_tag, *se_tag;
5468 time_t re_time, se_time;
5470 SIMPLEQ_FOREACH(re, tags, entry) {
5471 se = SIMPLEQ_FIRST(sorted);
5473 err = got_reflist_entry_dup(&new, re);
5476 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5479 err = got_ref_resolve(&re_id, repo, re->ref);
5482 err = got_object_open_as_tag(&re_tag, repo, re_id);
5486 re_time = got_object_tag_get_tagger_time(re_tag);
5487 got_object_tag_close(re_tag);
5491 err = got_ref_resolve(&se_id, repo, re->ref);
5494 err = got_object_open_as_tag(&se_tag, repo, se_id);
5498 se_time = got_object_tag_get_tagger_time(se_tag);
5499 got_object_tag_close(se_tag);
5501 if (se_time > re_time) {
5502 err = got_reflist_entry_dup(&new, re);
5505 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5508 se = SIMPLEQ_NEXT(se, entry);
5517 static const struct got_error *
5518 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5520 static const struct got_error *err = NULL;
5521 struct got_reflist_head refs;
5522 struct got_reflist_entry *re;
5524 SIMPLEQ_INIT(&refs);
5526 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5530 SIMPLEQ_FOREACH(re, &refs, entry) {
5531 const char *refname;
5532 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5536 struct got_object_id *id;
5537 struct got_tag_object *tag;
5538 struct got_commit_object *commit = NULL;
5540 refname = got_ref_get_name(re->ref);
5541 if (strncmp(refname, "refs/tags/", 10) != 0)
5544 refstr = got_ref_to_str(re->ref);
5545 if (refstr == NULL) {
5546 err = got_error_from_errno("got_ref_to_str");
5549 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5552 err = got_ref_resolve(&id, repo, re->ref);
5555 err = got_object_open_as_tag(&tag, repo, id);
5557 if (err->code != GOT_ERR_OBJ_TYPE) {
5561 /* "lightweight" tag */
5562 err = got_object_open_as_commit(&commit, repo, id);
5567 tagger = got_object_commit_get_committer(commit);
5569 got_object_commit_get_committer_time(commit);
5570 err = got_object_id_str(&id_str, id);
5576 tagger = got_object_tag_get_tagger(tag);
5577 tagger_time = got_object_tag_get_tagger_time(tag);
5578 err = got_object_id_str(&id_str,
5579 got_object_tag_get_object_id(tag));
5583 printf("from: %s\n", tagger);
5584 datestr = get_datestr(&tagger_time, datebuf);
5586 printf("date: %s UTC\n", datestr);
5588 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5590 switch (got_object_tag_get_object_type(tag)) {
5591 case GOT_OBJ_TYPE_BLOB:
5592 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5595 case GOT_OBJ_TYPE_TREE:
5596 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5599 case GOT_OBJ_TYPE_COMMIT:
5600 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5603 case GOT_OBJ_TYPE_TAG:
5604 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5613 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5616 got_object_commit_close(commit);
5618 tagmsg0 = strdup(got_object_tag_get_message(tag));
5619 got_object_tag_close(tag);
5620 if (tagmsg0 == NULL) {
5621 err = got_error_from_errno("strdup");
5628 line = strsep(&tagmsg, "\n");
5630 printf(" %s\n", line);
5635 got_ref_list_free(&refs);
5639 static const struct got_error *
5640 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5641 const char *tag_name, const char *repo_path)
5643 const struct got_error *err = NULL;
5644 char *template = NULL, *initial_content = NULL;
5645 char *editor = NULL;
5648 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5649 err = got_error_from_errno("asprintf");
5653 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5654 commit_id_str, tag_name) == -1) {
5655 err = got_error_from_errno("asprintf");
5659 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5663 dprintf(fd, initial_content);
5666 err = get_editor(&editor);
5669 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5671 free(initial_content);
5675 /* Editor is done; we can now apply unveil(2) */
5677 err = apply_unveil(repo_path, 0, NULL);
5686 static const struct got_error *
5687 add_tag(struct got_repository *repo, const char *tag_name,
5688 const char *commit_arg, const char *tagmsg_arg)
5690 const struct got_error *err = NULL;
5691 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5692 char *label = NULL, *commit_id_str = NULL;
5693 struct got_reference *ref = NULL;
5694 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5695 char *tagmsg_path = NULL, *tag_id_str = NULL;
5696 int preserve_tagmsg = 0;
5699 * Don't let the user create a tag name with a leading '-'.
5700 * While technically a valid reference name, this case is usually
5701 * an unintended typo.
5703 if (tag_name[0] == '-')
5704 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5706 err = get_author(&tagger, repo);
5710 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5711 GOT_OBJ_TYPE_COMMIT, 1, repo);
5715 err = got_object_id_str(&commit_id_str, commit_id);
5719 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5720 refname = strdup(tag_name);
5721 if (refname == NULL) {
5722 err = got_error_from_errno("strdup");
5726 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5727 err = got_error_from_errno("asprintf");
5731 err = got_ref_open(&ref, repo, refname, 0);
5733 err = got_error(GOT_ERR_TAG_EXISTS);
5735 } else if (err->code != GOT_ERR_NOT_REF)
5738 if (tagmsg_arg == NULL) {
5739 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5740 tag_name, got_repo_get_path(repo));
5742 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5743 tagmsg_path != NULL)
5744 preserve_tagmsg = 1;
5749 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5750 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5753 preserve_tagmsg = 1;
5757 err = got_ref_alloc(&ref, refname, tag_id);
5760 preserve_tagmsg = 1;
5764 err = got_ref_write(ref, repo);
5767 preserve_tagmsg = 1;
5771 err = got_object_id_str(&tag_id_str, tag_id);
5774 preserve_tagmsg = 1;
5777 printf("Created tag %s\n", tag_id_str);
5779 if (preserve_tagmsg) {
5780 fprintf(stderr, "%s: tag message preserved in %s\n",
5781 getprogname(), tagmsg_path);
5782 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5783 err = got_error_from_errno2("unlink", tagmsg_path);
5788 free(commit_id_str);
5796 static const struct got_error *
5797 cmd_tag(int argc, char *argv[])
5799 const struct got_error *error = NULL;
5800 struct got_repository *repo = NULL;
5801 struct got_worktree *worktree = NULL;
5802 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5803 char *gitconfig_path = NULL;
5804 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5805 int ch, do_list = 0;
5807 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5810 commit_id_arg = optarg;
5816 repo_path = realpath(optarg, NULL);
5817 if (repo_path == NULL)
5818 return got_error_from_errno2("realpath",
5820 got_path_strip_trailing_slashes(repo_path);
5835 if (commit_id_arg != NULL)
5837 "-c option can only be used when creating a tag");
5839 errx(1, "-l and -m options are mutually exclusive");
5842 } else if (argc != 1)
5849 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5853 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5854 "sendfd unveil", NULL) == -1)
5858 cwd = getcwd(NULL, 0);
5860 error = got_error_from_errno("getcwd");
5864 if (repo_path == NULL) {
5865 error = got_worktree_open(&worktree, cwd);
5866 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5872 strdup(got_worktree_get_repo_path(worktree));
5873 if (repo_path == NULL)
5874 error = got_error_from_errno("strdup");
5878 repo_path = strdup(cwd);
5879 if (repo_path == NULL) {
5880 error = got_error_from_errno("strdup");
5887 error = got_repo_open(&repo, repo_path, NULL);
5890 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5893 error = list_tags(repo, worktree);
5895 error = get_gitconfig_path(&gitconfig_path);
5898 error = got_repo_open(&repo, repo_path, gitconfig_path);
5903 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5908 if (commit_id_arg == NULL) {
5909 struct got_reference *head_ref;
5910 struct got_object_id *commit_id;
5911 error = got_ref_open(&head_ref, repo,
5912 worktree ? got_worktree_get_head_ref_name(worktree)
5916 error = got_ref_resolve(&commit_id, repo, head_ref);
5917 got_ref_close(head_ref);
5920 error = got_object_id_str(&commit_id_str, commit_id);
5926 error = add_tag(repo, tag_name,
5927 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5931 got_repo_close(repo);
5933 got_worktree_close(worktree);
5936 free(gitconfig_path);
5937 free(commit_id_str);
5944 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5949 static const struct got_error *
5950 add_progress(void *arg, unsigned char status, const char *path)
5952 while (path[0] == '/')
5954 printf("%c %s\n", status, path);
5958 static const struct got_error *
5959 cmd_add(int argc, char *argv[])
5961 const struct got_error *error = NULL;
5962 struct got_repository *repo = NULL;
5963 struct got_worktree *worktree = NULL;
5965 struct got_pathlist_head paths;
5966 struct got_pathlist_entry *pe;
5967 int ch, can_recurse = 0, no_ignores = 0;
5971 while ((ch = getopt(argc, argv, "IR")) != -1) {
5989 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5996 cwd = getcwd(NULL, 0);
5998 error = got_error_from_errno("getcwd");
6002 error = got_worktree_open(&worktree, cwd);
6004 if (error->code == GOT_ERR_NOT_WORKTREE)
6005 error = wrap_not_worktree_error(error, "add", cwd);
6009 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6014 error = apply_unveil(got_repo_get_path(repo), 1,
6015 got_worktree_get_root_path(worktree));
6019 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6023 if (!can_recurse && no_ignores) {
6024 error = got_error_msg(GOT_ERR_BAD_PATH,
6025 "disregarding ignores requires -R option");
6033 TAILQ_FOREACH(pe, &paths, entry) {
6034 if (asprintf(&ondisk_path, "%s/%s",
6035 got_worktree_get_root_path(worktree),
6037 error = got_error_from_errno("asprintf");
6040 if (lstat(ondisk_path, &sb) == -1) {
6041 if (errno == ENOENT) {
6045 error = got_error_from_errno2("lstat",
6051 if (S_ISDIR(sb.st_mode)) {
6052 error = got_error_msg(GOT_ERR_BAD_PATH,
6053 "adding directories requires -R option");
6059 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6060 NULL, repo, no_ignores);
6063 got_repo_close(repo);
6065 got_worktree_close(worktree);
6066 TAILQ_FOREACH(pe, &paths, entry)
6067 free((char *)pe->path);
6068 got_pathlist_free(&paths);
6076 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6077 "path ...\n", getprogname());
6081 static const struct got_error *
6082 print_remove_status(void *arg, unsigned char status,
6083 unsigned char staged_status, const char *path)
6085 while (path[0] == '/')
6087 if (status == GOT_STATUS_NONEXISTENT)
6089 if (status == staged_status && (status == GOT_STATUS_DELETE))
6090 status = GOT_STATUS_NO_CHANGE;
6091 printf("%c%c %s\n", status, staged_status, path);
6095 static const struct got_error *
6096 cmd_remove(int argc, char *argv[])
6098 const struct got_error *error = NULL;
6099 struct got_worktree *worktree = NULL;
6100 struct got_repository *repo = NULL;
6101 const char *status_codes = NULL;
6103 struct got_pathlist_head paths;
6104 struct got_pathlist_entry *pe;
6105 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6109 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6112 delete_local_mods = 1;
6121 for (i = 0; i < strlen(optarg); i++) {
6122 switch (optarg[i]) {
6123 case GOT_STATUS_MODIFY:
6124 delete_local_mods = 1;
6126 case GOT_STATUS_MISSING:
6129 errx(1, "invalid status code '%c'",
6133 status_codes = optarg;
6145 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6152 cwd = getcwd(NULL, 0);
6154 error = got_error_from_errno("getcwd");
6157 error = got_worktree_open(&worktree, cwd);
6159 if (error->code == GOT_ERR_NOT_WORKTREE)
6160 error = wrap_not_worktree_error(error, "remove", cwd);
6164 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6169 error = apply_unveil(got_repo_get_path(repo), 1,
6170 got_worktree_get_root_path(worktree));
6174 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6181 TAILQ_FOREACH(pe, &paths, entry) {
6182 if (asprintf(&ondisk_path, "%s/%s",
6183 got_worktree_get_root_path(worktree),
6185 error = got_error_from_errno("asprintf");
6188 if (lstat(ondisk_path, &sb) == -1) {
6189 if (errno == ENOENT) {
6193 error = got_error_from_errno2("lstat",
6199 if (S_ISDIR(sb.st_mode)) {
6200 error = got_error_msg(GOT_ERR_BAD_PATH,
6201 "removing directories requires -R option");
6207 error = got_worktree_schedule_delete(worktree, &paths,
6208 delete_local_mods, status_codes, print_remove_status, NULL,
6209 repo, keep_on_disk);
6212 got_repo_close(repo);
6214 got_worktree_close(worktree);
6215 TAILQ_FOREACH(pe, &paths, entry)
6216 free((char *)pe->path);
6217 got_pathlist_free(&paths);
6225 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6226 "path ...\n", getprogname());
6230 static const struct got_error *
6231 revert_progress(void *arg, unsigned char status, const char *path)
6233 if (status == GOT_STATUS_UNVERSIONED)
6236 while (path[0] == '/')
6238 printf("%c %s\n", status, path);
6242 struct choose_patch_arg {
6243 FILE *patch_script_file;
6247 static const struct got_error *
6248 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6249 int nchanges, const char *action)
6252 size_t linesize = 0;
6256 case GOT_STATUS_ADD:
6257 printf("A %s\n%s this addition? [y/n] ", path, action);
6259 case GOT_STATUS_DELETE:
6260 printf("D %s\n%s this deletion? [y/n] ", path, action);
6262 case GOT_STATUS_MODIFY:
6263 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6264 return got_error_from_errno("fseek");
6265 printf(GOT_COMMIT_SEP_STR);
6266 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6268 if (ferror(patch_file))
6269 return got_error_from_errno("getline");
6270 printf(GOT_COMMIT_SEP_STR);
6271 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6272 path, n, nchanges, action);
6275 return got_error_path(path, GOT_ERR_FILE_STATUS);
6281 static const struct got_error *
6282 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6283 FILE *patch_file, int n, int nchanges)
6285 const struct got_error *err = NULL;
6287 size_t linesize = 0;
6290 struct choose_patch_arg *a = arg;
6292 *choice = GOT_PATCH_CHOICE_NONE;
6294 if (a->patch_script_file) {
6296 err = show_change(status, path, patch_file, n, nchanges,
6300 linelen = getline(&line, &linesize, a->patch_script_file);
6301 if (linelen == -1) {
6302 if (ferror(a->patch_script_file))
6303 return got_error_from_errno("getline");
6306 nl = strchr(line, '\n');
6309 if (strcmp(line, "y") == 0) {
6310 *choice = GOT_PATCH_CHOICE_YES;
6312 } else if (strcmp(line, "n") == 0) {
6313 *choice = GOT_PATCH_CHOICE_NO;
6315 } else if (strcmp(line, "q") == 0 &&
6316 status == GOT_STATUS_MODIFY) {
6317 *choice = GOT_PATCH_CHOICE_QUIT;
6320 printf("invalid response '%s'\n", line);
6325 while (resp != 'y' && resp != 'n' && resp != 'q') {
6326 err = show_change(status, path, patch_file, n, nchanges,
6333 if (status == GOT_STATUS_MODIFY) {
6334 if (resp != 'y' && resp != 'n' && resp != 'q') {
6335 printf("invalid response '%c'\n", resp);
6338 } else if (resp != 'y' && resp != 'n') {
6339 printf("invalid response '%c'\n", resp);
6345 *choice = GOT_PATCH_CHOICE_YES;
6346 else if (resp == 'n')
6347 *choice = GOT_PATCH_CHOICE_NO;
6348 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6349 *choice = GOT_PATCH_CHOICE_QUIT;
6355 static const struct got_error *
6356 cmd_revert(int argc, char *argv[])
6358 const struct got_error *error = NULL;
6359 struct got_worktree *worktree = NULL;
6360 struct got_repository *repo = NULL;
6361 char *cwd = NULL, *path = NULL;
6362 struct got_pathlist_head paths;
6363 struct got_pathlist_entry *pe;
6364 int ch, can_recurse = 0, pflag = 0;
6365 FILE *patch_script_file = NULL;
6366 const char *patch_script_path = NULL;
6367 struct choose_patch_arg cpa;
6371 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6377 patch_script_path = optarg;
6392 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6393 "unveil", NULL) == -1)
6398 if (patch_script_path && !pflag)
6399 errx(1, "-F option can only be used together with -p option");
6401 cwd = getcwd(NULL, 0);
6403 error = got_error_from_errno("getcwd");
6406 error = got_worktree_open(&worktree, cwd);
6408 if (error->code == GOT_ERR_NOT_WORKTREE)
6409 error = wrap_not_worktree_error(error, "revert", cwd);
6413 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6418 if (patch_script_path) {
6419 patch_script_file = fopen(patch_script_path, "r");
6420 if (patch_script_file == NULL) {
6421 error = got_error_from_errno2("fopen",
6426 error = apply_unveil(got_repo_get_path(repo), 1,
6427 got_worktree_get_root_path(worktree));
6431 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6438 TAILQ_FOREACH(pe, &paths, entry) {
6439 if (asprintf(&ondisk_path, "%s/%s",
6440 got_worktree_get_root_path(worktree),
6442 error = got_error_from_errno("asprintf");
6445 if (lstat(ondisk_path, &sb) == -1) {
6446 if (errno == ENOENT) {
6450 error = got_error_from_errno2("lstat",
6456 if (S_ISDIR(sb.st_mode)) {
6457 error = got_error_msg(GOT_ERR_BAD_PATH,
6458 "reverting directories requires -R option");
6464 cpa.patch_script_file = patch_script_file;
6465 cpa.action = "revert";
6466 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6467 pflag ? choose_patch : NULL, &cpa, repo);
6469 if (patch_script_file && fclose(patch_script_file) == EOF &&
6471 error = got_error_from_errno2("fclose", patch_script_path);
6473 got_repo_close(repo);
6475 got_worktree_close(worktree);
6484 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6489 struct collect_commit_logmsg_arg {
6490 const char *cmdline_log;
6492 const char *worktree_path;
6493 const char *branch_name;
6494 const char *repo_path;
6499 static const struct got_error *
6500 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6503 char *initial_content = NULL;
6504 struct got_pathlist_entry *pe;
6505 const struct got_error *err = NULL;
6506 char *template = NULL;
6507 struct collect_commit_logmsg_arg *a = arg;
6511 /* if a message was specified on the command line, just use it */
6512 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6513 len = strlen(a->cmdline_log) + 1;
6514 *logmsg = malloc(len + 1);
6515 if (*logmsg == NULL)
6516 return got_error_from_errno("malloc");
6517 strlcpy(*logmsg, a->cmdline_log, len);
6521 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6522 return got_error_from_errno("asprintf");
6524 if (asprintf(&initial_content,
6525 "\n# changes to be committed on branch %s:\n",
6526 a->branch_name) == -1)
6527 return got_error_from_errno("asprintf");
6529 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6533 dprintf(fd, initial_content);
6535 TAILQ_FOREACH(pe, commitable_paths, entry) {
6536 struct got_commitable *ct = pe->data;
6537 dprintf(fd, "# %c %s\n",
6538 got_commitable_get_status(ct),
6539 got_commitable_get_path(ct));
6543 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6545 free(initial_content);
6548 /* Editor is done; we can now apply unveil(2) */
6550 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6559 static const struct got_error *
6560 cmd_commit(int argc, char *argv[])
6562 const struct got_error *error = NULL;
6563 struct got_worktree *worktree = NULL;
6564 struct got_repository *repo = NULL;
6565 char *cwd = NULL, *id_str = NULL;
6566 struct got_object_id *id = NULL;
6567 const char *logmsg = NULL;
6568 struct collect_commit_logmsg_arg cl_arg;
6569 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6570 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6571 int allow_bad_symlinks = 0;
6572 struct got_pathlist_head paths;
6575 cl_arg.logmsg_path = NULL;
6577 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6583 allow_bad_symlinks = 1;
6595 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6596 "unveil", NULL) == -1)
6599 cwd = getcwd(NULL, 0);
6601 error = got_error_from_errno("getcwd");
6604 error = got_worktree_open(&worktree, cwd);
6606 if (error->code == GOT_ERR_NOT_WORKTREE)
6607 error = wrap_not_worktree_error(error, "commit", cwd);
6611 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6614 if (rebase_in_progress) {
6615 error = got_error(GOT_ERR_REBASING);
6619 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6624 error = get_gitconfig_path(&gitconfig_path);
6627 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6632 error = get_author(&author, repo);
6637 * unveil(2) traverses exec(2); if an editor is used we have
6638 * to apply unveil after the log message has been written.
6640 if (logmsg == NULL || strlen(logmsg) == 0)
6641 error = get_editor(&editor);
6643 error = apply_unveil(got_repo_get_path(repo), 0,
6644 got_worktree_get_root_path(worktree));
6648 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6652 cl_arg.editor = editor;
6653 cl_arg.cmdline_log = logmsg;
6654 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6655 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6656 if (!histedit_in_progress) {
6657 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6658 error = got_error(GOT_ERR_COMMIT_BRANCH);
6661 cl_arg.branch_name += 11;
6663 cl_arg.repo_path = got_repo_get_path(repo);
6664 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6665 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6666 print_status, NULL, repo);
6668 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6669 cl_arg.logmsg_path != NULL)
6670 preserve_logmsg = 1;
6674 error = got_object_id_str(&id_str, id);
6677 printf("Created commit %s\n", id_str);
6679 if (preserve_logmsg) {
6680 fprintf(stderr, "%s: log message preserved in %s\n",
6681 getprogname(), cl_arg.logmsg_path);
6682 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6684 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6685 free(cl_arg.logmsg_path);
6687 got_repo_close(repo);
6689 got_worktree_close(worktree);
6692 free(gitconfig_path);
6699 usage_cherrypick(void)
6701 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6705 static const struct got_error *
6706 cmd_cherrypick(int argc, char *argv[])
6708 const struct got_error *error = NULL;
6709 struct got_worktree *worktree = NULL;
6710 struct got_repository *repo = NULL;
6711 char *cwd = NULL, *commit_id_str = NULL;
6712 struct got_object_id *commit_id = NULL;
6713 struct got_commit_object *commit = NULL;
6714 struct got_object_qid *pid;
6715 struct got_reference *head_ref = NULL;
6717 struct got_update_progress_arg upa;
6719 while ((ch = getopt(argc, argv, "")) != -1) {
6731 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6732 "unveil", NULL) == -1)
6738 cwd = getcwd(NULL, 0);
6740 error = got_error_from_errno("getcwd");
6743 error = got_worktree_open(&worktree, cwd);
6745 if (error->code == GOT_ERR_NOT_WORKTREE)
6746 error = wrap_not_worktree_error(error, "cherrypick",
6751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6756 error = apply_unveil(got_repo_get_path(repo), 0,
6757 got_worktree_get_root_path(worktree));
6761 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6762 GOT_OBJ_TYPE_COMMIT, repo);
6763 if (error != NULL) {
6764 struct got_reference *ref;
6765 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6767 error = got_ref_open(&ref, repo, argv[0], 0);
6770 error = got_ref_resolve(&commit_id, repo, ref);
6775 error = got_object_id_str(&commit_id_str, commit_id);
6779 error = got_ref_open(&head_ref, repo,
6780 got_worktree_get_head_ref_name(worktree), 0);
6784 error = check_same_branch(commit_id, head_ref, NULL, repo);
6786 if (error->code != GOT_ERR_ANCESTRY)
6790 error = got_error(GOT_ERR_SAME_BRANCH);
6794 error = got_object_open_as_commit(&commit, repo, commit_id);
6797 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6798 memset(&upa, 0, sizeof(upa));
6799 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6800 commit_id, repo, update_progress, &upa, check_cancelled,
6805 if (upa.did_something)
6806 printf("Merged commit %s\n", commit_id_str);
6807 print_update_progress_stats(&upa);
6810 got_object_commit_close(commit);
6811 free(commit_id_str);
6813 got_ref_close(head_ref);
6815 got_worktree_close(worktree);
6817 got_repo_close(repo);
6824 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6828 static const struct got_error *
6829 cmd_backout(int argc, char *argv[])
6831 const struct got_error *error = NULL;
6832 struct got_worktree *worktree = NULL;
6833 struct got_repository *repo = NULL;
6834 char *cwd = NULL, *commit_id_str = NULL;
6835 struct got_object_id *commit_id = NULL;
6836 struct got_commit_object *commit = NULL;
6837 struct got_object_qid *pid;
6838 struct got_reference *head_ref = NULL;
6840 struct got_update_progress_arg upa;
6842 while ((ch = getopt(argc, argv, "")) != -1) {
6854 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6855 "unveil", NULL) == -1)
6861 cwd = getcwd(NULL, 0);
6863 error = got_error_from_errno("getcwd");
6866 error = got_worktree_open(&worktree, cwd);
6868 if (error->code == GOT_ERR_NOT_WORKTREE)
6869 error = wrap_not_worktree_error(error, "backout", cwd);
6873 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6878 error = apply_unveil(got_repo_get_path(repo), 0,
6879 got_worktree_get_root_path(worktree));
6883 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6884 GOT_OBJ_TYPE_COMMIT, repo);
6885 if (error != NULL) {
6886 struct got_reference *ref;
6887 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6889 error = got_ref_open(&ref, repo, argv[0], 0);
6892 error = got_ref_resolve(&commit_id, repo, ref);
6897 error = got_object_id_str(&commit_id_str, commit_id);
6901 error = got_ref_open(&head_ref, repo,
6902 got_worktree_get_head_ref_name(worktree), 0);
6906 error = check_same_branch(commit_id, head_ref, NULL, repo);
6910 error = got_object_open_as_commit(&commit, repo, commit_id);
6913 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6915 error = got_error(GOT_ERR_ROOT_COMMIT);
6919 memset(&upa, 0, sizeof(upa));
6920 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6921 update_progress, &upa, check_cancelled, NULL);
6925 if (upa.did_something)
6926 printf("Backed out commit %s\n", commit_id_str);
6927 print_update_progress_stats(&upa);
6930 got_object_commit_close(commit);
6931 free(commit_id_str);
6933 got_ref_close(head_ref);
6935 got_worktree_close(worktree);
6937 got_repo_close(repo);
6944 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6950 trim_logmsg(char *logmsg, int limit)
6955 len = strlen(logmsg);
6959 nl = strchr(logmsg, '\n');
6964 static const struct got_error *
6965 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6967 const struct got_error *err;
6968 char *logmsg0 = NULL;
6971 err = got_object_commit_get_logmsg(&logmsg0, commit);
6976 while (isspace((unsigned char)s[0]))
6979 *logmsg = strdup(s);
6980 if (*logmsg == NULL) {
6981 err = got_error_from_errno("strdup");
6985 trim_logmsg(*logmsg, limit);
6991 static const struct got_error *
6992 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6994 const struct got_error *err;
6995 struct got_commit_object *commit = NULL;
6996 char *id_str = NULL, *logmsg = NULL;
6998 err = got_object_open_as_commit(&commit, repo, id);
7002 err = got_object_id_str(&id_str, id);
7008 err = get_short_logmsg(&logmsg, 42, commit);
7012 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7015 got_object_commit_close(commit);
7020 static const struct got_error *
7021 show_rebase_progress(struct got_commit_object *commit,
7022 struct got_object_id *old_id, struct got_object_id *new_id)
7024 const struct got_error *err;
7025 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7027 err = got_object_id_str(&old_id_str, old_id);
7032 err = got_object_id_str(&new_id_str, new_id);
7037 old_id_str[12] = '\0';
7039 new_id_str[12] = '\0';
7041 err = get_short_logmsg(&logmsg, 42, commit);
7045 printf("%s -> %s: %s\n", old_id_str,
7046 new_id_str ? new_id_str : "no-op change", logmsg);
7054 static const struct got_error *
7055 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7056 struct got_reference *branch, struct got_reference *new_base_branch,
7057 struct got_reference *tmp_branch, struct got_repository *repo)
7059 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7060 return got_worktree_rebase_complete(worktree, fileindex,
7061 new_base_branch, tmp_branch, branch, repo);
7064 static const struct got_error *
7065 rebase_commit(struct got_pathlist_head *merged_paths,
7066 struct got_worktree *worktree, struct got_fileindex *fileindex,
7067 struct got_reference *tmp_branch,
7068 struct got_object_id *commit_id, struct got_repository *repo)
7070 const struct got_error *error;
7071 struct got_commit_object *commit;
7072 struct got_object_id *new_commit_id;
7074 error = got_object_open_as_commit(&commit, repo, commit_id);
7078 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7079 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7081 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7083 error = show_rebase_progress(commit, commit_id, NULL);
7085 error = show_rebase_progress(commit, commit_id, new_commit_id);
7086 free(new_commit_id);
7089 got_object_commit_close(commit);
7093 struct check_path_prefix_arg {
7094 const char *path_prefix;
7099 static const struct got_error *
7100 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7101 struct got_blob_object *blob2, struct got_object_id *id1,
7102 struct got_object_id *id2, const char *path1, const char *path2,
7103 mode_t mode1, mode_t mode2, struct got_repository *repo)
7105 struct check_path_prefix_arg *a = arg;
7107 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7108 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7109 return got_error(a->errcode);
7114 static const struct got_error *
7115 check_path_prefix(struct got_object_id *parent_id,
7116 struct got_object_id *commit_id, const char *path_prefix,
7117 int errcode, struct got_repository *repo)
7119 const struct got_error *err;
7120 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7121 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7122 struct check_path_prefix_arg cpp_arg;
7124 if (got_path_is_root_dir(path_prefix))
7127 err = got_object_open_as_commit(&commit, repo, commit_id);
7131 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7135 err = got_object_open_as_tree(&tree1, repo,
7136 got_object_commit_get_tree_id(parent_commit));
7140 err = got_object_open_as_tree(&tree2, repo,
7141 got_object_commit_get_tree_id(commit));
7145 cpp_arg.path_prefix = path_prefix;
7146 while (cpp_arg.path_prefix[0] == '/')
7147 cpp_arg.path_prefix++;
7148 cpp_arg.len = strlen(cpp_arg.path_prefix);
7149 cpp_arg.errcode = errcode;
7150 err = got_diff_tree(tree1, tree2, "", "", repo,
7151 check_path_prefix_in_diff, &cpp_arg, 0);
7154 got_object_tree_close(tree1);
7156 got_object_tree_close(tree2);
7158 got_object_commit_close(commit);
7160 got_object_commit_close(parent_commit);
7164 static const struct got_error *
7165 collect_commits(struct got_object_id_queue *commits,
7166 struct got_object_id *initial_commit_id,
7167 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7168 const char *path_prefix, int path_prefix_errcode,
7169 struct got_repository *repo)
7171 const struct got_error *err = NULL;
7172 struct got_commit_graph *graph = NULL;
7173 struct got_object_id *parent_id = NULL;
7174 struct got_object_qid *qid;
7175 struct got_object_id *commit_id = initial_commit_id;
7177 err = got_commit_graph_open(&graph, "/", 1);
7181 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7182 check_cancelled, NULL);
7185 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7186 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7187 check_cancelled, NULL);
7189 if (err->code == GOT_ERR_ITER_COMPLETED) {
7190 err = got_error_msg(GOT_ERR_ANCESTRY,
7191 "ran out of commits to rebase before "
7192 "youngest common ancestor commit has "
7197 err = check_path_prefix(parent_id, commit_id,
7198 path_prefix, path_prefix_errcode, repo);
7202 err = got_object_qid_alloc(&qid, commit_id);
7205 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7206 commit_id = parent_id;
7210 got_commit_graph_close(graph);
7214 static const struct got_error *
7215 cmd_rebase(int argc, char *argv[])
7217 const struct got_error *error = NULL;
7218 struct got_worktree *worktree = NULL;
7219 struct got_repository *repo = NULL;
7220 struct got_fileindex *fileindex = NULL;
7222 struct got_reference *branch = NULL;
7223 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7224 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7225 struct got_object_id *resume_commit_id = NULL;
7226 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7227 struct got_commit_object *commit = NULL;
7228 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7229 int histedit_in_progress = 0;
7230 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7231 struct got_object_id_queue commits;
7232 struct got_pathlist_head merged_paths;
7233 const struct got_object_id_queue *parent_ids;
7234 struct got_object_qid *qid, *pid;
7236 SIMPLEQ_INIT(&commits);
7237 TAILQ_INIT(&merged_paths);
7239 while ((ch = getopt(argc, argv, "ac")) != -1) {
7245 continue_rebase = 1;
7257 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7258 "unveil", NULL) == -1)
7261 if (abort_rebase && continue_rebase)
7263 else if (abort_rebase || continue_rebase) {
7266 } else if (argc != 1)
7269 cwd = getcwd(NULL, 0);
7271 error = got_error_from_errno("getcwd");
7274 error = got_worktree_open(&worktree, cwd);
7276 if (error->code == GOT_ERR_NOT_WORKTREE)
7277 error = wrap_not_worktree_error(error, "rebase", cwd);
7281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7286 error = apply_unveil(got_repo_get_path(repo), 0,
7287 got_worktree_get_root_path(worktree));
7291 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7295 if (histedit_in_progress) {
7296 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7300 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7305 struct got_update_progress_arg upa;
7306 if (!rebase_in_progress) {
7307 error = got_error(GOT_ERR_NOT_REBASING);
7310 error = got_worktree_rebase_continue(&resume_commit_id,
7311 &new_base_branch, &tmp_branch, &branch, &fileindex,
7315 printf("Switching work tree to %s\n",
7316 got_ref_get_symref_target(new_base_branch));
7317 memset(&upa, 0, sizeof(upa));
7318 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7319 new_base_branch, update_progress, &upa);
7322 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7323 print_update_progress_stats(&upa);
7324 goto done; /* nothing else to do */
7327 if (continue_rebase) {
7328 if (!rebase_in_progress) {
7329 error = got_error(GOT_ERR_NOT_REBASING);
7332 error = got_worktree_rebase_continue(&resume_commit_id,
7333 &new_base_branch, &tmp_branch, &branch, &fileindex,
7338 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7339 resume_commit_id, repo);
7343 yca_id = got_object_id_dup(resume_commit_id);
7344 if (yca_id == NULL) {
7345 error = got_error_from_errno("got_object_id_dup");
7349 error = got_ref_open(&branch, repo, argv[0], 0);
7354 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7358 if (!continue_rebase) {
7359 struct got_object_id *base_commit_id;
7361 base_commit_id = got_worktree_get_base_commit_id(worktree);
7362 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7363 base_commit_id, branch_head_commit_id, repo,
7364 check_cancelled, NULL);
7367 if (yca_id == NULL) {
7368 error = got_error_msg(GOT_ERR_ANCESTRY,
7369 "specified branch shares no common ancestry "
7370 "with work tree's branch");
7374 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7376 if (error->code != GOT_ERR_ANCESTRY)
7380 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7381 "specified branch resolves to a commit which "
7382 "is already contained in work tree's branch");
7385 error = got_worktree_rebase_prepare(&new_base_branch,
7386 &tmp_branch, &fileindex, worktree, branch, repo);
7391 commit_id = branch_head_commit_id;
7392 error = got_object_open_as_commit(&commit, repo, commit_id);
7396 parent_ids = got_object_commit_get_parent_ids(commit);
7397 pid = SIMPLEQ_FIRST(parent_ids);
7399 if (!continue_rebase) {
7400 struct got_update_progress_arg upa;
7401 memset(&upa, 0, sizeof(upa));
7402 error = got_worktree_rebase_abort(worktree, fileindex,
7403 repo, new_base_branch, update_progress, &upa);
7406 printf("Rebase of %s aborted\n",
7407 got_ref_get_name(branch));
7408 print_update_progress_stats(&upa);
7411 error = got_error(GOT_ERR_EMPTY_REBASE);
7414 error = collect_commits(&commits, commit_id, pid->id,
7415 yca_id, got_worktree_get_path_prefix(worktree),
7416 GOT_ERR_REBASE_PATH, repo);
7417 got_object_commit_close(commit);
7422 if (SIMPLEQ_EMPTY(&commits)) {
7423 if (continue_rebase) {
7424 error = rebase_complete(worktree, fileindex,
7425 branch, new_base_branch, tmp_branch, repo);
7428 /* Fast-forward the reference of the branch. */
7429 struct got_object_id *new_head_commit_id;
7431 error = got_ref_resolve(&new_head_commit_id, repo,
7435 error = got_object_id_str(&id_str, new_head_commit_id);
7436 printf("Forwarding %s to commit %s\n",
7437 got_ref_get_name(branch), id_str);
7439 error = got_ref_change_ref(branch,
7440 new_head_commit_id);
7447 SIMPLEQ_FOREACH(qid, &commits, entry) {
7448 struct got_update_progress_arg upa;
7450 commit_id = qid->id;
7451 parent_id = pid ? pid->id : yca_id;
7454 memset(&upa, 0, sizeof(upa));
7455 error = got_worktree_rebase_merge_files(&merged_paths,
7456 worktree, fileindex, parent_id, commit_id, repo,
7457 update_progress, &upa, check_cancelled, NULL);
7461 print_update_progress_stats(&upa);
7462 if (upa.conflicts > 0)
7463 rebase_status = GOT_STATUS_CONFLICT;
7465 if (rebase_status == GOT_STATUS_CONFLICT) {
7466 error = show_rebase_merge_conflict(qid->id, repo);
7469 got_worktree_rebase_pathlist_free(&merged_paths);
7473 error = rebase_commit(&merged_paths, worktree, fileindex,
7474 tmp_branch, commit_id, repo);
7475 got_worktree_rebase_pathlist_free(&merged_paths);
7480 if (rebase_status == GOT_STATUS_CONFLICT) {
7481 error = got_worktree_rebase_postpone(worktree, fileindex);
7484 error = got_error_msg(GOT_ERR_CONFLICTS,
7485 "conflicts must be resolved before rebasing can continue");
7487 error = rebase_complete(worktree, fileindex, branch,
7488 new_base_branch, tmp_branch, repo);
7490 got_object_id_queue_free(&commits);
7491 free(branch_head_commit_id);
7492 free(resume_commit_id);
7495 got_object_commit_close(commit);
7497 got_ref_close(branch);
7498 if (new_base_branch)
7499 got_ref_close(new_base_branch);
7501 got_ref_close(tmp_branch);
7503 got_worktree_close(worktree);
7505 got_repo_close(repo);
7510 usage_histedit(void)
7512 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7517 #define GOT_HISTEDIT_PICK 'p'
7518 #define GOT_HISTEDIT_EDIT 'e'
7519 #define GOT_HISTEDIT_FOLD 'f'
7520 #define GOT_HISTEDIT_DROP 'd'
7521 #define GOT_HISTEDIT_MESG 'm'
7523 static struct got_histedit_cmd {
7527 } got_histedit_cmds[] = {
7528 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7529 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7530 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7532 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7533 { GOT_HISTEDIT_MESG, "mesg",
7534 "single-line log message for commit above (open editor if empty)" },
7537 struct got_histedit_list_entry {
7538 TAILQ_ENTRY(got_histedit_list_entry) entry;
7539 struct got_object_id *commit_id;
7540 const struct got_histedit_cmd *cmd;
7543 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7545 static const struct got_error *
7546 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7547 FILE *f, struct got_repository *repo)
7549 const struct got_error *err = NULL;
7550 char *logmsg = NULL, *id_str = NULL;
7551 struct got_commit_object *commit = NULL;
7554 err = got_object_open_as_commit(&commit, repo, commit_id);
7558 err = get_short_logmsg(&logmsg, 34, commit);
7562 err = got_object_id_str(&id_str, commit_id);
7566 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7568 err = got_ferror(f, GOT_ERR_IO);
7571 got_object_commit_close(commit);
7577 static const struct got_error *
7578 histedit_write_commit_list(struct got_object_id_queue *commits,
7579 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7581 const struct got_error *err = NULL;
7582 struct got_object_qid *qid;
7584 if (SIMPLEQ_EMPTY(commits))
7585 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7587 SIMPLEQ_FOREACH(qid, commits, entry) {
7588 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7592 if (edit_logmsg_only) {
7593 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7595 err = got_ferror(f, GOT_ERR_IO);
7604 static const struct got_error *
7605 write_cmd_list(FILE *f, const char *branch_name,
7606 struct got_object_id_queue *commits)
7608 const struct got_error *err = NULL;
7611 struct got_object_qid *qid;
7613 qid = SIMPLEQ_FIRST(commits);
7614 err = got_object_id_str(&id_str, qid->id);
7619 "# Editing the history of branch '%s' starting at\n"
7621 "# Commits will be processed in order from top to "
7622 "bottom of this file.\n", branch_name, id_str);
7624 err = got_ferror(f, GOT_ERR_IO);
7628 n = fprintf(f, "# Available histedit commands:\n");
7630 err = got_ferror(f, GOT_ERR_IO);
7634 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7635 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7636 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7639 err = got_ferror(f, GOT_ERR_IO);
7648 static const struct got_error *
7649 histedit_syntax_error(int lineno)
7651 static char msg[42];
7654 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7656 if (ret == -1 || ret >= sizeof(msg))
7657 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7659 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7662 static const struct got_error *
7663 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7664 char *logmsg, struct got_repository *repo)
7666 const struct got_error *err;
7667 struct got_commit_object *folded_commit = NULL;
7668 char *id_str, *folded_logmsg = NULL;
7670 err = got_object_id_str(&id_str, hle->commit_id);
7674 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7678 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7681 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7682 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7683 folded_logmsg) == -1) {
7684 err = got_error_from_errno("asprintf");
7688 got_object_commit_close(folded_commit);
7690 free(folded_logmsg);
7694 static struct got_histedit_list_entry *
7695 get_folded_commits(struct got_histedit_list_entry *hle)
7697 struct got_histedit_list_entry *prev, *folded = NULL;
7699 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7700 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7701 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7702 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7704 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7710 static const struct got_error *
7711 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7712 struct got_repository *repo)
7714 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7715 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7716 const struct got_error *err = NULL;
7717 struct got_commit_object *commit = NULL;
7719 struct got_histedit_list_entry *folded = NULL;
7721 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7725 folded = get_folded_commits(hle);
7727 while (folded != hle) {
7728 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7729 folded = TAILQ_NEXT(folded, entry);
7732 err = append_folded_commit_msg(&new_msg, folded,
7738 folded = TAILQ_NEXT(folded, entry);
7742 err = got_object_id_str(&id_str, hle->commit_id);
7745 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7748 if (asprintf(&new_msg,
7749 "%s\n# original log message of commit %s: %s",
7750 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7751 err = got_error_from_errno("asprintf");
7757 err = got_object_id_str(&id_str, hle->commit_id);
7761 err = got_opentemp_named_fd(&logmsg_path, &fd,
7762 GOT_TMPDIR_STR "/got-logmsg");
7766 dprintf(fd, logmsg);
7769 err = get_editor(&editor);
7773 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7775 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7777 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7780 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7781 err = got_error_from_errno2("unlink", logmsg_path);
7787 got_object_commit_close(commit);
7791 static const struct got_error *
7792 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7793 FILE *f, struct got_repository *repo)
7795 const struct got_error *err = NULL;
7796 char *line = NULL, *p, *end;
7800 const struct got_histedit_cmd *cmd;
7801 struct got_object_id *commit_id = NULL;
7802 struct got_histedit_list_entry *hle = NULL;
7805 len = getline(&line, &size, f);
7807 const struct got_error *getline_err;
7810 getline_err = got_error_from_errno("getline");
7811 err = got_ferror(f, getline_err->code);
7816 while (isspace((unsigned char)p[0]))
7818 if (p[0] == '#' || p[0] == '\0') {
7824 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7825 cmd = &got_histedit_cmds[i];
7826 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7827 isspace((unsigned char)p[strlen(cmd->name)])) {
7828 p += strlen(cmd->name);
7831 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7836 if (i == nitems(got_histedit_cmds)) {
7837 err = histedit_syntax_error(lineno);
7840 while (isspace((unsigned char)p[0]))
7842 if (cmd->code == GOT_HISTEDIT_MESG) {
7843 if (hle == NULL || hle->logmsg != NULL) {
7844 err = got_error(GOT_ERR_HISTEDIT_CMD);
7848 err = histedit_edit_logmsg(hle, repo);
7852 hle->logmsg = strdup(p);
7853 if (hle->logmsg == NULL) {
7854 err = got_error_from_errno("strdup");
7863 while (end[0] && !isspace((unsigned char)end[0]))
7867 err = got_object_resolve_id_str(&commit_id, repo, p);
7869 /* override error code */
7870 err = histedit_syntax_error(lineno);
7874 hle = malloc(sizeof(*hle));
7876 err = got_error_from_errno("malloc");
7880 hle->commit_id = commit_id;
7885 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7893 static const struct got_error *
7894 histedit_check_script(struct got_histedit_list *histedit_cmds,
7895 struct got_object_id_queue *commits, struct got_repository *repo)
7897 const struct got_error *err = NULL;
7898 struct got_object_qid *qid;
7899 struct got_histedit_list_entry *hle;
7900 static char msg[92];
7903 if (TAILQ_EMPTY(histedit_cmds))
7904 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7905 "histedit script contains no commands");
7906 if (SIMPLEQ_EMPTY(commits))
7907 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7909 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7910 struct got_histedit_list_entry *hle2;
7911 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7914 if (got_object_id_cmp(hle->commit_id,
7915 hle2->commit_id) != 0)
7917 err = got_object_id_str(&id_str, hle->commit_id);
7920 snprintf(msg, sizeof(msg), "commit %s is listed "
7921 "more than once in histedit script", id_str);
7923 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7927 SIMPLEQ_FOREACH(qid, commits, entry) {
7928 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7929 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7933 err = got_object_id_str(&id_str, qid->id);
7936 snprintf(msg, sizeof(msg),
7937 "commit %s missing from histedit script", id_str);
7939 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7943 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7944 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7945 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7946 "last commit in histedit script cannot be folded");
7951 static const struct got_error *
7952 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7953 const char *path, struct got_object_id_queue *commits,
7954 struct got_repository *repo)
7956 const struct got_error *err = NULL;
7960 err = get_editor(&editor);
7964 if (spawn_editor(editor, path) == -1) {
7965 err = got_error_from_errno("failed spawning editor");
7969 f = fopen(path, "r");
7971 err = got_error_from_errno("fopen");
7974 err = histedit_parse_list(histedit_cmds, f, repo);
7978 err = histedit_check_script(histedit_cmds, commits, repo);
7980 if (f && fclose(f) != 0 && err == NULL)
7981 err = got_error_from_errno("fclose");
7986 static const struct got_error *
7987 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7988 struct got_object_id_queue *, const char *, const char *,
7989 struct got_repository *);
7991 static const struct got_error *
7992 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7993 struct got_object_id_queue *commits, const char *branch_name,
7994 int edit_logmsg_only, struct got_repository *repo)
7996 const struct got_error *err;
8000 err = got_opentemp_named(&path, &f, "got-histedit");
8004 err = write_cmd_list(f, branch_name, commits);
8008 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8012 if (edit_logmsg_only) {
8014 err = histedit_parse_list(histedit_cmds, f, repo);
8016 if (fclose(f) != 0) {
8017 err = got_error_from_errno("fclose");
8021 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8023 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8024 err->code != GOT_ERR_HISTEDIT_CMD)
8026 err = histedit_edit_list_retry(histedit_cmds, err,
8027 commits, path, branch_name, repo);
8031 if (f && fclose(f) != 0 && err == NULL)
8032 err = got_error_from_errno("fclose");
8033 if (path && unlink(path) != 0 && err == NULL)
8034 err = got_error_from_errno2("unlink", path);
8039 static const struct got_error *
8040 histedit_save_list(struct got_histedit_list *histedit_cmds,
8041 struct got_worktree *worktree, struct got_repository *repo)
8043 const struct got_error *err = NULL;
8046 struct got_histedit_list_entry *hle;
8047 struct got_commit_object *commit = NULL;
8049 err = got_worktree_get_histedit_script_path(&path, worktree);
8053 f = fopen(path, "w");
8055 err = got_error_from_errno2("fopen", path);
8058 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8059 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8065 int n = fprintf(f, "%c %s\n",
8066 GOT_HISTEDIT_MESG, hle->logmsg);
8068 err = got_ferror(f, GOT_ERR_IO);
8074 if (f && fclose(f) != 0 && err == NULL)
8075 err = got_error_from_errno("fclose");
8078 got_object_commit_close(commit);
8083 histedit_free_list(struct got_histedit_list *histedit_cmds)
8085 struct got_histedit_list_entry *hle;
8087 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8088 TAILQ_REMOVE(histedit_cmds, hle, entry);
8093 static const struct got_error *
8094 histedit_load_list(struct got_histedit_list *histedit_cmds,
8095 const char *path, struct got_repository *repo)
8097 const struct got_error *err = NULL;
8100 f = fopen(path, "r");
8102 err = got_error_from_errno2("fopen", path);
8106 err = histedit_parse_list(histedit_cmds, f, repo);
8108 if (f && fclose(f) != 0 && err == NULL)
8109 err = got_error_from_errno("fclose");
8113 static const struct got_error *
8114 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8115 const struct got_error *edit_err, struct got_object_id_queue *commits,
8116 const char *path, const char *branch_name, struct got_repository *repo)
8118 const struct got_error *err = NULL, *prev_err = edit_err;
8121 while (resp != 'c' && resp != 'r' && resp != 'a') {
8122 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8123 "or (a)bort: ", getprogname(), prev_err->msg);
8128 histedit_free_list(histedit_cmds);
8129 err = histedit_run_editor(histedit_cmds, path, commits,
8132 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8133 err->code != GOT_ERR_HISTEDIT_CMD)
8140 } else if (resp == 'r') {
8141 histedit_free_list(histedit_cmds);
8142 err = histedit_edit_script(histedit_cmds,
8143 commits, branch_name, 0, repo);
8145 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8146 err->code != GOT_ERR_HISTEDIT_CMD)
8153 } else if (resp == 'a') {
8154 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8157 printf("invalid response '%c'\n", resp);
8163 static const struct got_error *
8164 histedit_complete(struct got_worktree *worktree,
8165 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8166 struct got_reference *branch, struct got_repository *repo)
8168 printf("Switching work tree to %s\n",
8169 got_ref_get_symref_target(branch));
8170 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8174 static const struct got_error *
8175 show_histedit_progress(struct got_commit_object *commit,
8176 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8178 const struct got_error *err;
8179 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8181 err = got_object_id_str(&old_id_str, hle->commit_id);
8186 err = got_object_id_str(&new_id_str, new_id);
8191 old_id_str[12] = '\0';
8193 new_id_str[12] = '\0';
8196 logmsg = strdup(hle->logmsg);
8197 if (logmsg == NULL) {
8198 err = got_error_from_errno("strdup");
8201 trim_logmsg(logmsg, 42);
8203 err = get_short_logmsg(&logmsg, 42, commit);
8208 switch (hle->cmd->code) {
8209 case GOT_HISTEDIT_PICK:
8210 case GOT_HISTEDIT_EDIT:
8211 printf("%s -> %s: %s\n", old_id_str,
8212 new_id_str ? new_id_str : "no-op change", logmsg);
8214 case GOT_HISTEDIT_DROP:
8215 case GOT_HISTEDIT_FOLD:
8216 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8228 static const struct got_error *
8229 histedit_commit(struct got_pathlist_head *merged_paths,
8230 struct got_worktree *worktree, struct got_fileindex *fileindex,
8231 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8232 struct got_repository *repo)
8234 const struct got_error *err;
8235 struct got_commit_object *commit;
8236 struct got_object_id *new_commit_id;
8238 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8239 && hle->logmsg == NULL) {
8240 err = histedit_edit_logmsg(hle, repo);
8245 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8249 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8250 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8253 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8255 err = show_histedit_progress(commit, hle, NULL);
8257 err = show_histedit_progress(commit, hle, new_commit_id);
8258 free(new_commit_id);
8261 got_object_commit_close(commit);
8265 static const struct got_error *
8266 histedit_skip_commit(struct got_histedit_list_entry *hle,
8267 struct got_worktree *worktree, struct got_repository *repo)
8269 const struct got_error *error;
8270 struct got_commit_object *commit;
8272 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8277 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8281 error = show_histedit_progress(commit, hle, NULL);
8282 got_object_commit_close(commit);
8286 static const struct got_error *
8287 check_local_changes(void *arg, unsigned char status,
8288 unsigned char staged_status, const char *path,
8289 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8290 struct got_object_id *commit_id, int dirfd, const char *de_name)
8292 int *have_local_changes = arg;
8295 case GOT_STATUS_ADD:
8296 case GOT_STATUS_DELETE:
8297 case GOT_STATUS_MODIFY:
8298 case GOT_STATUS_CONFLICT:
8299 *have_local_changes = 1;
8300 return got_error(GOT_ERR_CANCELLED);
8305 switch (staged_status) {
8306 case GOT_STATUS_ADD:
8307 case GOT_STATUS_DELETE:
8308 case GOT_STATUS_MODIFY:
8309 *have_local_changes = 1;
8310 return got_error(GOT_ERR_CANCELLED);
8318 static const struct got_error *
8319 cmd_histedit(int argc, char *argv[])
8321 const struct got_error *error = NULL;
8322 struct got_worktree *worktree = NULL;
8323 struct got_fileindex *fileindex = NULL;
8324 struct got_repository *repo = NULL;
8326 struct got_reference *branch = NULL;
8327 struct got_reference *tmp_branch = NULL;
8328 struct got_object_id *resume_commit_id = NULL;
8329 struct got_object_id *base_commit_id = NULL;
8330 struct got_object_id *head_commit_id = NULL;
8331 struct got_commit_object *commit = NULL;
8332 int ch, rebase_in_progress = 0;
8333 struct got_update_progress_arg upa;
8334 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8335 int edit_logmsg_only = 0;
8336 const char *edit_script_path = NULL;
8337 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8338 struct got_object_id_queue commits;
8339 struct got_pathlist_head merged_paths;
8340 const struct got_object_id_queue *parent_ids;
8341 struct got_object_qid *pid;
8342 struct got_histedit_list histedit_cmds;
8343 struct got_histedit_list_entry *hle;
8345 SIMPLEQ_INIT(&commits);
8346 TAILQ_INIT(&histedit_cmds);
8347 TAILQ_INIT(&merged_paths);
8348 memset(&upa, 0, sizeof(upa));
8350 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8359 edit_script_path = optarg;
8362 edit_logmsg_only = 1;
8374 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8375 "unveil", NULL) == -1)
8378 if (abort_edit && continue_edit)
8379 errx(1, "histedit's -a and -c options are mutually exclusive");
8380 if (edit_script_path && edit_logmsg_only)
8381 errx(1, "histedit's -F and -m options are mutually exclusive");
8382 if (abort_edit && edit_logmsg_only)
8383 errx(1, "histedit's -a and -m options are mutually exclusive");
8384 if (continue_edit && edit_logmsg_only)
8385 errx(1, "histedit's -c and -m options are mutually exclusive");
8390 * This command cannot apply unveil(2) in all cases because the
8391 * user may choose to run an editor to edit the histedit script
8392 * and to edit individual commit log messages.
8393 * unveil(2) traverses exec(2); if an editor is used we have to
8394 * apply unveil after edit script and log messages have been written.
8395 * XXX TODO: Make use of unveil(2) where possible.
8398 cwd = getcwd(NULL, 0);
8400 error = got_error_from_errno("getcwd");
8403 error = got_worktree_open(&worktree, cwd);
8405 if (error->code == GOT_ERR_NOT_WORKTREE)
8406 error = wrap_not_worktree_error(error, "histedit", cwd);
8410 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8415 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8418 if (rebase_in_progress) {
8419 error = got_error(GOT_ERR_REBASING);
8423 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8427 if (edit_in_progress && edit_logmsg_only) {
8428 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8429 "histedit operation is in progress in this "
8430 "work tree and must be continued or aborted "
8431 "before the -m option can be used");
8435 if (edit_in_progress && abort_edit) {
8436 error = got_worktree_histedit_continue(&resume_commit_id,
8437 &tmp_branch, &branch, &base_commit_id, &fileindex,
8441 printf("Switching work tree to %s\n",
8442 got_ref_get_symref_target(branch));
8443 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8444 branch, base_commit_id, update_progress, &upa);
8447 printf("Histedit of %s aborted\n",
8448 got_ref_get_symref_target(branch));
8449 print_update_progress_stats(&upa);
8450 goto done; /* nothing else to do */
8451 } else if (abort_edit) {
8452 error = got_error(GOT_ERR_NOT_HISTEDIT);
8456 if (continue_edit) {
8459 if (!edit_in_progress) {
8460 error = got_error(GOT_ERR_NOT_HISTEDIT);
8464 error = got_worktree_get_histedit_script_path(&path, worktree);
8468 error = histedit_load_list(&histedit_cmds, path, repo);
8473 error = got_worktree_histedit_continue(&resume_commit_id,
8474 &tmp_branch, &branch, &base_commit_id, &fileindex,
8479 error = got_ref_resolve(&head_commit_id, repo, branch);
8483 error = got_object_open_as_commit(&commit, repo,
8487 parent_ids = got_object_commit_get_parent_ids(commit);
8488 pid = SIMPLEQ_FIRST(parent_ids);
8490 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8493 error = collect_commits(&commits, head_commit_id, pid->id,
8494 base_commit_id, got_worktree_get_path_prefix(worktree),
8495 GOT_ERR_HISTEDIT_PATH, repo);
8496 got_object_commit_close(commit);
8501 if (edit_in_progress) {
8502 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8506 error = got_ref_open(&branch, repo,
8507 got_worktree_get_head_ref_name(worktree), 0);
8511 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8512 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8513 "will not edit commit history of a branch outside "
8514 "the \"refs/heads/\" reference namespace");
8518 error = got_ref_resolve(&head_commit_id, repo, branch);
8519 got_ref_close(branch);
8524 error = got_object_open_as_commit(&commit, repo,
8528 parent_ids = got_object_commit_get_parent_ids(commit);
8529 pid = SIMPLEQ_FIRST(parent_ids);
8531 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8534 error = collect_commits(&commits, head_commit_id, pid->id,
8535 got_worktree_get_base_commit_id(worktree),
8536 got_worktree_get_path_prefix(worktree),
8537 GOT_ERR_HISTEDIT_PATH, repo);
8538 got_object_commit_close(commit);
8543 if (SIMPLEQ_EMPTY(&commits)) {
8544 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8548 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8549 &base_commit_id, &fileindex, worktree, repo);
8553 if (edit_script_path) {
8554 error = histedit_load_list(&histedit_cmds,
8555 edit_script_path, repo);
8557 got_worktree_histedit_abort(worktree, fileindex,
8558 repo, branch, base_commit_id,
8559 update_progress, &upa);
8560 print_update_progress_stats(&upa);
8564 const char *branch_name;
8565 branch_name = got_ref_get_symref_target(branch);
8566 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8568 error = histedit_edit_script(&histedit_cmds, &commits,
8569 branch_name, edit_logmsg_only, repo);
8571 got_worktree_histedit_abort(worktree, fileindex,
8572 repo, branch, base_commit_id,
8573 update_progress, &upa);
8574 print_update_progress_stats(&upa);
8580 error = histedit_save_list(&histedit_cmds, worktree,
8583 got_worktree_histedit_abort(worktree, fileindex,
8584 repo, branch, base_commit_id,
8585 update_progress, &upa);
8586 print_update_progress_stats(&upa);
8592 error = histedit_check_script(&histedit_cmds, &commits, repo);
8596 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8597 if (resume_commit_id) {
8598 if (got_object_id_cmp(hle->commit_id,
8599 resume_commit_id) != 0)
8602 resume_commit_id = NULL;
8603 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8604 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8605 error = histedit_skip_commit(hle, worktree,
8610 struct got_pathlist_head paths;
8611 int have_changes = 0;
8614 error = got_pathlist_append(&paths, "", NULL);
8617 error = got_worktree_status(worktree, &paths,
8618 repo, check_local_changes, &have_changes,
8619 check_cancelled, NULL);
8620 got_pathlist_free(&paths);
8622 if (error->code != GOT_ERR_CANCELLED)
8624 if (sigint_received || sigpipe_received)
8628 error = histedit_commit(NULL, worktree,
8629 fileindex, tmp_branch, hle, repo);
8633 error = got_object_open_as_commit(
8634 &commit, repo, hle->commit_id);
8637 error = show_histedit_progress(commit,
8639 got_object_commit_close(commit);
8648 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8649 error = histedit_skip_commit(hle, worktree, repo);
8655 error = got_object_open_as_commit(&commit, repo,
8659 parent_ids = got_object_commit_get_parent_ids(commit);
8660 pid = SIMPLEQ_FIRST(parent_ids);
8662 error = got_worktree_histedit_merge_files(&merged_paths,
8663 worktree, fileindex, pid->id, hle->commit_id, repo,
8664 update_progress, &upa, check_cancelled, NULL);
8667 got_object_commit_close(commit);
8670 print_update_progress_stats(&upa);
8671 if (upa.conflicts > 0)
8672 rebase_status = GOT_STATUS_CONFLICT;
8674 if (rebase_status == GOT_STATUS_CONFLICT) {
8675 error = show_rebase_merge_conflict(hle->commit_id,
8679 got_worktree_rebase_pathlist_free(&merged_paths);
8683 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8685 error = got_object_id_str(&id_str, hle->commit_id);
8688 printf("Stopping histedit for amending commit %s\n",
8691 got_worktree_rebase_pathlist_free(&merged_paths);
8692 error = got_worktree_histedit_postpone(worktree,
8697 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8698 error = histedit_skip_commit(hle, worktree, repo);
8704 error = histedit_commit(&merged_paths, worktree, fileindex,
8705 tmp_branch, hle, repo);
8706 got_worktree_rebase_pathlist_free(&merged_paths);
8711 if (rebase_status == GOT_STATUS_CONFLICT) {
8712 error = got_worktree_histedit_postpone(worktree, fileindex);
8715 error = got_error_msg(GOT_ERR_CONFLICTS,
8716 "conflicts must be resolved before histedit can continue");
8718 error = histedit_complete(worktree, fileindex, tmp_branch,
8721 got_object_id_queue_free(&commits);
8722 histedit_free_list(&histedit_cmds);
8723 free(head_commit_id);
8724 free(base_commit_id);
8725 free(resume_commit_id);
8727 got_object_commit_close(commit);
8729 got_ref_close(branch);
8731 got_ref_close(tmp_branch);
8733 got_worktree_close(worktree);
8735 got_repo_close(repo);
8740 usage_integrate(void)
8742 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8746 static const struct got_error *
8747 cmd_integrate(int argc, char *argv[])
8749 const struct got_error *error = NULL;
8750 struct got_repository *repo = NULL;
8751 struct got_worktree *worktree = NULL;
8752 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8753 const char *branch_arg = NULL;
8754 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8755 struct got_fileindex *fileindex = NULL;
8756 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8758 struct got_update_progress_arg upa;
8760 while ((ch = getopt(argc, argv, "")) != -1) {
8773 branch_arg = argv[0];
8775 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8776 "unveil", NULL) == -1)
8779 cwd = getcwd(NULL, 0);
8781 error = got_error_from_errno("getcwd");
8785 error = got_worktree_open(&worktree, cwd);
8787 if (error->code == GOT_ERR_NOT_WORKTREE)
8788 error = wrap_not_worktree_error(error, "integrate",
8793 error = check_rebase_or_histedit_in_progress(worktree);
8797 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8802 error = apply_unveil(got_repo_get_path(repo), 0,
8803 got_worktree_get_root_path(worktree));
8807 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8808 error = got_error_from_errno("asprintf");
8812 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8813 &base_branch_ref, worktree, refname, repo);
8817 refname = strdup(got_ref_get_name(branch_ref));
8818 if (refname == NULL) {
8819 error = got_error_from_errno("strdup");
8820 got_worktree_integrate_abort(worktree, fileindex, repo,
8821 branch_ref, base_branch_ref);
8824 base_refname = strdup(got_ref_get_name(base_branch_ref));
8825 if (base_refname == NULL) {
8826 error = got_error_from_errno("strdup");
8827 got_worktree_integrate_abort(worktree, fileindex, repo,
8828 branch_ref, base_branch_ref);
8832 error = got_ref_resolve(&commit_id, repo, branch_ref);
8836 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8840 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8841 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8842 "specified branch has already been integrated");
8843 got_worktree_integrate_abort(worktree, fileindex, repo,
8844 branch_ref, base_branch_ref);
8848 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8850 if (error->code == GOT_ERR_ANCESTRY)
8851 error = got_error(GOT_ERR_REBASE_REQUIRED);
8852 got_worktree_integrate_abort(worktree, fileindex, repo,
8853 branch_ref, base_branch_ref);
8857 memset(&upa, 0, sizeof(upa));
8858 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8859 branch_ref, base_branch_ref, update_progress, &upa,
8860 check_cancelled, NULL);
8864 printf("Integrated %s into %s\n", refname, base_refname);
8865 print_update_progress_stats(&upa);
8868 got_repo_close(repo);
8870 got_worktree_close(worktree);
8872 free(base_commit_id);
8882 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8883 "[-S] [file-path ...]\n",
8888 static const struct got_error *
8889 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8890 const char *path, struct got_object_id *blob_id,
8891 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8892 int dirfd, const char *de_name)
8894 const struct got_error *err = NULL;
8895 char *id_str = NULL;
8897 if (staged_status != GOT_STATUS_ADD &&
8898 staged_status != GOT_STATUS_MODIFY &&
8899 staged_status != GOT_STATUS_DELETE)
8902 if (staged_status == GOT_STATUS_ADD ||
8903 staged_status == GOT_STATUS_MODIFY)
8904 err = got_object_id_str(&id_str, staged_blob_id);
8906 err = got_object_id_str(&id_str, blob_id);
8910 printf("%s %c %s\n", id_str, staged_status, path);
8915 static const struct got_error *
8916 cmd_stage(int argc, char *argv[])
8918 const struct got_error *error = NULL;
8919 struct got_repository *repo = NULL;
8920 struct got_worktree *worktree = NULL;
8922 struct got_pathlist_head paths;
8923 struct got_pathlist_entry *pe;
8924 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
8925 FILE *patch_script_file = NULL;
8926 const char *patch_script_path = NULL;
8927 struct choose_patch_arg cpa;
8931 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
8940 patch_script_path = optarg;
8943 allow_bad_symlinks = 1;
8955 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8956 "unveil", NULL) == -1)
8959 if (list_stage && (pflag || patch_script_path))
8960 errx(1, "-l option cannot be used with other options");
8961 if (patch_script_path && !pflag)
8962 errx(1, "-F option can only be used together with -p option");
8964 cwd = getcwd(NULL, 0);
8966 error = got_error_from_errno("getcwd");
8970 error = got_worktree_open(&worktree, cwd);
8972 if (error->code == GOT_ERR_NOT_WORKTREE)
8973 error = wrap_not_worktree_error(error, "stage", cwd);
8977 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8982 if (patch_script_path) {
8983 patch_script_file = fopen(patch_script_path, "r");
8984 if (patch_script_file == NULL) {
8985 error = got_error_from_errno2("fopen",
8990 error = apply_unveil(got_repo_get_path(repo), 0,
8991 got_worktree_get_root_path(worktree));
8995 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9000 error = got_worktree_status(worktree, &paths, repo,
9001 print_stage, NULL, check_cancelled, NULL);
9003 cpa.patch_script_file = patch_script_file;
9004 cpa.action = "stage";
9005 error = got_worktree_stage(worktree, &paths,
9006 pflag ? NULL : print_status, NULL,
9007 pflag ? choose_patch : NULL, &cpa,
9008 allow_bad_symlinks, repo);
9011 if (patch_script_file && fclose(patch_script_file) == EOF &&
9013 error = got_error_from_errno2("fclose", patch_script_path);
9015 got_repo_close(repo);
9017 got_worktree_close(worktree);
9018 TAILQ_FOREACH(pe, &paths, entry)
9019 free((char *)pe->path);
9020 got_pathlist_free(&paths);
9028 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9029 "[file-path ...]\n",
9035 static const struct got_error *
9036 cmd_unstage(int argc, char *argv[])
9038 const struct got_error *error = NULL;
9039 struct got_repository *repo = NULL;
9040 struct got_worktree *worktree = NULL;
9042 struct got_pathlist_head paths;
9043 struct got_pathlist_entry *pe;
9045 struct got_update_progress_arg upa;
9046 FILE *patch_script_file = NULL;
9047 const char *patch_script_path = NULL;
9048 struct choose_patch_arg cpa;
9052 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9058 patch_script_path = optarg;
9070 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9071 "unveil", NULL) == -1)
9074 if (patch_script_path && !pflag)
9075 errx(1, "-F option can only be used together with -p option");
9077 cwd = getcwd(NULL, 0);
9079 error = got_error_from_errno("getcwd");
9083 error = got_worktree_open(&worktree, cwd);
9085 if (error->code == GOT_ERR_NOT_WORKTREE)
9086 error = wrap_not_worktree_error(error, "unstage", cwd);
9090 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9095 if (patch_script_path) {
9096 patch_script_file = fopen(patch_script_path, "r");
9097 if (patch_script_file == NULL) {
9098 error = got_error_from_errno2("fopen",
9104 error = apply_unveil(got_repo_get_path(repo), 0,
9105 got_worktree_get_root_path(worktree));
9109 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9113 cpa.patch_script_file = patch_script_file;
9114 cpa.action = "unstage";
9115 memset(&upa, 0, sizeof(upa));
9116 error = got_worktree_unstage(worktree, &paths, update_progress,
9117 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9119 print_update_progress_stats(&upa);
9121 if (patch_script_file && fclose(patch_script_file) == EOF &&
9123 error = got_error_from_errno2("fclose", patch_script_path);
9125 got_repo_close(repo);
9127 got_worktree_close(worktree);
9128 TAILQ_FOREACH(pe, &paths, entry)
9129 free((char *)pe->path);
9130 got_pathlist_free(&paths);
9138 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9139 "arg1 [arg2 ...]\n", getprogname());
9143 static const struct got_error *
9144 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9146 const struct got_error *err;
9147 struct got_blob_object *blob;
9149 err = got_object_open_as_blob(&blob, repo, id, 8192);
9153 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9154 got_object_blob_close(blob);
9158 static const struct got_error *
9159 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9161 const struct got_error *err;
9162 struct got_tree_object *tree;
9165 err = got_object_open_as_tree(&tree, repo, id);
9169 nentries = got_object_tree_get_nentries(tree);
9170 for (i = 0; i < nentries; i++) {
9171 struct got_tree_entry *te;
9173 if (sigint_received || sigpipe_received)
9175 te = got_object_tree_get_entry(tree, i);
9176 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9179 fprintf(outfile, "%s %.7o %s\n", id_str,
9180 got_tree_entry_get_mode(te),
9181 got_tree_entry_get_name(te));
9185 got_object_tree_close(tree);
9189 static const struct got_error *
9190 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9192 const struct got_error *err;
9193 struct got_commit_object *commit;
9194 const struct got_object_id_queue *parent_ids;
9195 struct got_object_qid *pid;
9196 char *id_str = NULL;
9197 const char *logmsg = NULL;
9199 err = got_object_open_as_commit(&commit, repo, id);
9203 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9207 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9208 parent_ids = got_object_commit_get_parent_ids(commit);
9209 fprintf(outfile, "numparents %d\n",
9210 got_object_commit_get_nparents(commit));
9211 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9213 err = got_object_id_str(&pid_str, pid->id);
9216 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9219 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9220 got_object_commit_get_author(commit),
9221 got_object_commit_get_author_time(commit));
9223 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9224 got_object_commit_get_author(commit),
9225 got_object_commit_get_committer_time(commit));
9227 logmsg = got_object_commit_get_logmsg_raw(commit);
9228 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9229 fprintf(outfile, "%s", logmsg);
9232 got_object_commit_close(commit);
9236 static const struct got_error *
9237 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9239 const struct got_error *err;
9240 struct got_tag_object *tag;
9241 char *id_str = NULL;
9242 const char *tagmsg = NULL;
9244 err = got_object_open_as_tag(&tag, repo, id);
9248 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9252 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9254 switch (got_object_tag_get_object_type(tag)) {
9255 case GOT_OBJ_TYPE_BLOB:
9256 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9257 GOT_OBJ_LABEL_BLOB);
9259 case GOT_OBJ_TYPE_TREE:
9260 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9261 GOT_OBJ_LABEL_TREE);
9263 case GOT_OBJ_TYPE_COMMIT:
9264 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9265 GOT_OBJ_LABEL_COMMIT);
9267 case GOT_OBJ_TYPE_TAG:
9268 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9275 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9276 got_object_tag_get_name(tag));
9278 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9279 got_object_tag_get_tagger(tag),
9280 got_object_tag_get_tagger_time(tag));
9282 tagmsg = got_object_tag_get_message(tag);
9283 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9284 fprintf(outfile, "%s", tagmsg);
9287 got_object_tag_close(tag);
9291 static const struct got_error *
9292 cmd_cat(int argc, char *argv[])
9294 const struct got_error *error;
9295 struct got_repository *repo = NULL;
9296 struct got_worktree *worktree = NULL;
9297 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9298 const char *commit_id_str = NULL;
9299 struct got_object_id *id = NULL, *commit_id = NULL;
9300 int ch, obj_type, i, force_path = 0;
9303 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9308 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9311 commit_id_str = optarg;
9314 repo_path = realpath(optarg, NULL);
9315 if (repo_path == NULL)
9316 return got_error_from_errno2("realpath",
9318 got_path_strip_trailing_slashes(repo_path);
9332 cwd = getcwd(NULL, 0);
9334 error = got_error_from_errno("getcwd");
9337 error = got_worktree_open(&worktree, cwd);
9338 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9341 if (repo_path == NULL) {
9343 got_worktree_get_repo_path(worktree));
9344 if (repo_path == NULL) {
9345 error = got_error_from_errno("strdup");
9351 if (repo_path == NULL) {
9352 repo_path = getcwd(NULL, 0);
9353 if (repo_path == NULL)
9354 return got_error_from_errno("getcwd");
9357 error = got_repo_open(&repo, repo_path, NULL);
9362 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9366 if (commit_id_str == NULL)
9367 commit_id_str = GOT_REF_HEAD;
9368 error = got_repo_match_object_id(&commit_id, NULL,
9369 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9373 for (i = 0; i < argc; i++) {
9375 error = got_object_id_by_path(&id, repo, commit_id,
9380 error = got_repo_match_object_id(&id, &label, argv[i],
9381 GOT_OBJ_TYPE_ANY, 0, repo);
9383 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9384 error->code != GOT_ERR_NOT_REF)
9386 error = got_object_id_by_path(&id, repo,
9387 commit_id, argv[i]);
9393 error = got_object_get_type(&obj_type, repo, id);
9398 case GOT_OBJ_TYPE_BLOB:
9399 error = cat_blob(id, repo, stdout);
9401 case GOT_OBJ_TYPE_TREE:
9402 error = cat_tree(id, repo, stdout);
9404 case GOT_OBJ_TYPE_COMMIT:
9405 error = cat_commit(id, repo, stdout);
9407 case GOT_OBJ_TYPE_TAG:
9408 error = cat_tag(id, repo, stdout);
9411 error = got_error(GOT_ERR_OBJ_TYPE);
9426 got_worktree_close(worktree);
9428 const struct got_error *repo_error;
9429 repo_error = got_repo_close(repo);
9439 fprintf(stderr, "usage: %s info [path ...]\n",
9444 static const struct got_error *
9445 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9446 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9447 struct got_object_id *commit_id)
9449 const struct got_error *err = NULL;
9450 char *id_str = NULL;
9452 struct tm mytm, *tm;
9453 struct got_pathlist_head *paths = arg;
9454 struct got_pathlist_entry *pe;
9457 * Clear error indication from any of the path arguments which
9458 * would cause this file index entry to be displayed.
9460 TAILQ_FOREACH(pe, paths, entry) {
9461 if (got_path_cmp(path, pe->path, strlen(path),
9462 pe->path_len) == 0 ||
9463 got_path_is_child(path, pe->path, pe->path_len))
9464 pe->data = NULL; /* no error */
9467 printf(GOT_COMMIT_SEP_STR);
9469 printf("symlink: %s\n", path);
9470 else if (S_ISREG(mode)) {
9471 printf("file: %s\n", path);
9472 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9473 } else if (S_ISDIR(mode))
9474 printf("directory: %s\n", path);
9476 printf("something: %s\n", path);
9478 tm = localtime_r(&mtime, &mytm);
9481 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9482 return got_error(GOT_ERR_NO_SPACE);
9483 printf("timestamp: %s\n", datebuf);
9486 err = got_object_id_str(&id_str, blob_id);
9489 printf("based on blob: %s\n", id_str);
9493 if (staged_blob_id) {
9494 err = got_object_id_str(&id_str, staged_blob_id);
9497 printf("based on staged blob: %s\n", id_str);
9502 err = got_object_id_str(&id_str, commit_id);
9505 printf("based on commit: %s\n", id_str);
9512 static const struct got_error *
9513 cmd_info(int argc, char *argv[])
9515 const struct got_error *error = NULL;
9516 struct got_worktree *worktree = NULL;
9517 char *cwd = NULL, *id_str = NULL;
9518 struct got_pathlist_head paths;
9519 struct got_pathlist_entry *pe;
9520 char *uuidstr = NULL;
9521 int ch, show_files = 0;
9525 while ((ch = getopt(argc, argv, "")) != -1) {
9537 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9541 cwd = getcwd(NULL, 0);
9543 error = got_error_from_errno("getcwd");
9547 error = got_worktree_open(&worktree, cwd);
9549 if (error->code == GOT_ERR_NOT_WORKTREE)
9550 error = wrap_not_worktree_error(error, "status", cwd);
9554 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9559 error = get_worktree_paths_from_argv(&paths, argc, argv,
9566 error = got_object_id_str(&id_str,
9567 got_worktree_get_base_commit_id(worktree));
9571 error = got_worktree_get_uuid(&uuidstr, worktree);
9575 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9576 printf("work tree base commit: %s\n", id_str);
9577 printf("work tree path prefix: %s\n",
9578 got_worktree_get_path_prefix(worktree));
9579 printf("work tree branch reference: %s\n",
9580 got_worktree_get_head_ref_name(worktree));
9581 printf("work tree UUID: %s\n", uuidstr);
9582 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9585 struct got_pathlist_entry *pe;
9586 TAILQ_FOREACH(pe, &paths, entry) {
9587 if (pe->path_len == 0)
9590 * Assume this path will fail. This will be corrected
9591 * in print_path_info() in case the path does suceeed.
9593 pe->data = (void *)got_error_path(pe->path,
9596 error = got_worktree_path_info(worktree, &paths,
9597 print_path_info, &paths, check_cancelled, NULL);
9600 TAILQ_FOREACH(pe, &paths, entry) {
9601 if (pe->data != NULL) {
9602 error = pe->data; /* bad path */
9608 TAILQ_FOREACH(pe, &paths, entry)
9609 free((char *)pe->path);
9610 got_pathlist_free(&paths);