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"
57 #include "got_gotconfig.h"
60 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
63 static volatile sig_atomic_t sigint_received;
64 static volatile sig_atomic_t sigpipe_received;
67 catch_sigint(int signo)
73 catch_sigpipe(int signo)
81 const struct got_error *(*cmd_main)(int, char *[]);
82 void (*cmd_usage)(void);
83 const char *cmd_alias;
86 __dead static void usage(int);
87 __dead static void usage_init(void);
88 __dead static void usage_import(void);
89 __dead static void usage_clone(void);
90 __dead static void usage_fetch(void);
91 __dead static void usage_checkout(void);
92 __dead static void usage_update(void);
93 __dead static void usage_log(void);
94 __dead static void usage_diff(void);
95 __dead static void usage_blame(void);
96 __dead static void usage_tree(void);
97 __dead static void usage_status(void);
98 __dead static void usage_ref(void);
99 __dead static void usage_branch(void);
100 __dead static void usage_tag(void);
101 __dead static void usage_add(void);
102 __dead static void usage_remove(void);
103 __dead static void usage_revert(void);
104 __dead static void usage_commit(void);
105 __dead static void usage_cherrypick(void);
106 __dead static void usage_backout(void);
107 __dead static void usage_rebase(void);
108 __dead static void usage_histedit(void);
109 __dead static void usage_integrate(void);
110 __dead static void usage_stage(void);
111 __dead static void usage_unstage(void);
112 __dead static void usage_cat(void);
113 __dead static void usage_info(void);
115 static const struct got_error* cmd_init(int, char *[]);
116 static const struct got_error* cmd_import(int, char *[]);
117 static const struct got_error* cmd_clone(int, char *[]);
118 static const struct got_error* cmd_fetch(int, char *[]);
119 static const struct got_error* cmd_checkout(int, char *[]);
120 static const struct got_error* cmd_update(int, char *[]);
121 static const struct got_error* cmd_log(int, char *[]);
122 static const struct got_error* cmd_diff(int, char *[]);
123 static const struct got_error* cmd_blame(int, char *[]);
124 static const struct got_error* cmd_tree(int, char *[]);
125 static const struct got_error* cmd_status(int, char *[]);
126 static const struct got_error* cmd_ref(int, char *[]);
127 static const struct got_error* cmd_branch(int, char *[]);
128 static const struct got_error* cmd_tag(int, char *[]);
129 static const struct got_error* cmd_add(int, char *[]);
130 static const struct got_error* cmd_remove(int, char *[]);
131 static const struct got_error* cmd_revert(int, char *[]);
132 static const struct got_error* cmd_commit(int, char *[]);
133 static const struct got_error* cmd_cherrypick(int, char *[]);
134 static const struct got_error* cmd_backout(int, char *[]);
135 static const struct got_error* cmd_rebase(int, char *[]);
136 static const struct got_error* cmd_histedit(int, char *[]);
137 static const struct got_error* cmd_integrate(int, char *[]);
138 static const struct got_error* cmd_stage(int, char *[]);
139 static const struct got_error* cmd_unstage(int, char *[]);
140 static const struct got_error* cmd_cat(int, char *[]);
141 static const struct got_error* cmd_info(int, char *[]);
143 static struct got_cmd got_commands[] = {
144 { "init", cmd_init, usage_init, "" },
145 { "import", cmd_import, usage_import, "im" },
146 { "clone", cmd_clone, usage_clone, "cl" },
147 { "fetch", cmd_fetch, usage_fetch, "fe" },
148 { "checkout", cmd_checkout, usage_checkout, "co" },
149 { "update", cmd_update, usage_update, "up" },
150 { "log", cmd_log, usage_log, "" },
151 { "diff", cmd_diff, usage_diff, "di" },
152 { "blame", cmd_blame, usage_blame, "bl" },
153 { "tree", cmd_tree, usage_tree, "tr" },
154 { "status", cmd_status, usage_status, "st" },
155 { "ref", cmd_ref, usage_ref, "" },
156 { "branch", cmd_branch, usage_branch, "br" },
157 { "tag", cmd_tag, usage_tag, "" },
158 { "add", cmd_add, usage_add, "" },
159 { "remove", cmd_remove, usage_remove, "rm" },
160 { "revert", cmd_revert, usage_revert, "rv" },
161 { "commit", cmd_commit, usage_commit, "ci" },
162 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
163 { "backout", cmd_backout, usage_backout, "bo" },
164 { "rebase", cmd_rebase, usage_rebase, "rb" },
165 { "histedit", cmd_histedit, usage_histedit, "he" },
166 { "integrate", cmd_integrate, usage_integrate,"ig" },
167 { "stage", cmd_stage, usage_stage, "sg" },
168 { "unstage", cmd_unstage, usage_unstage, "ug" },
169 { "cat", cmd_cat, usage_cat, "" },
170 { "info", cmd_info, usage_info, "" },
178 fprintf(stderr, "commands:");
179 for (i = 0; i < nitems(got_commands); i++) {
180 struct got_cmd *cmd = &got_commands[i];
181 fprintf(stderr, " %s", cmd->cmd_name);
187 main(int argc, char *argv[])
192 int hflag = 0, Vflag = 0;
193 static struct option longopts[] = {
194 { "version", no_argument, NULL, 'V' },
198 setlocale(LC_CTYPE, "");
200 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
219 got_version_print_str();
226 signal(SIGINT, catch_sigint);
227 signal(SIGPIPE, catch_sigpipe);
229 for (i = 0; i < nitems(got_commands); i++) {
230 const struct got_error *error;
232 cmd = &got_commands[i];
234 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
235 strcmp(cmd->cmd_alias, argv[0]) != 0)
239 got_commands[i].cmd_usage();
241 error = got_commands[i].cmd_main(argc, argv);
242 if (error && error->code != GOT_ERR_CANCELLED &&
243 error->code != GOT_ERR_PRIVSEP_EXIT &&
244 !(sigpipe_received &&
245 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
247 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
248 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
255 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
263 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
270 static const struct got_error *
271 get_editor(char **abspath)
273 const struct got_error *err = NULL;
278 editor = getenv("VISUAL");
280 editor = getenv("EDITOR");
283 err = got_path_find_prog(abspath, editor);
288 if (*abspath == NULL) {
289 *abspath = strdup("/bin/ed");
290 if (*abspath == NULL)
291 return got_error_from_errno("strdup");
297 static const struct got_error *
298 apply_unveil(const char *repo_path, int repo_read_only,
299 const char *worktree_path)
301 const struct got_error *err;
304 if (unveil("gmon.out", "rwc") != 0)
305 return got_error_from_errno2("unveil", "gmon.out");
307 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
308 return got_error_from_errno2("unveil", repo_path);
310 if (worktree_path && unveil(worktree_path, "rwc") != 0)
311 return got_error_from_errno2("unveil", worktree_path);
313 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
314 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
316 err = got_privsep_unveil_exec_helpers();
320 if (unveil(NULL, NULL) != 0)
321 return got_error_from_errno("unveil");
329 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
333 static const struct got_error *
334 cmd_init(int argc, char *argv[])
336 const struct got_error *error = NULL;
337 char *repo_path = NULL;
340 while ((ch = getopt(argc, argv, "")) != -1) {
352 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
358 repo_path = strdup(argv[0]);
359 if (repo_path == NULL)
360 return got_error_from_errno("strdup");
362 got_path_strip_trailing_slashes(repo_path);
364 error = got_path_mkdir(repo_path);
366 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
369 error = apply_unveil(repo_path, 0, NULL);
373 error = got_repo_init(repo_path);
382 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
383 "[-r repository-path] [-I pattern] path\n", getprogname());
388 spawn_editor(const char *editor, const char *file)
391 sig_t sighup, sigint, sigquit;
394 sighup = signal(SIGHUP, SIG_IGN);
395 sigint = signal(SIGINT, SIG_IGN);
396 sigquit = signal(SIGQUIT, SIG_IGN);
398 switch (pid = fork()) {
402 execl(editor, editor, file, (char *)NULL);
406 while (waitpid(pid, &st, 0) == -1)
411 (void)signal(SIGHUP, sighup);
412 (void)signal(SIGINT, sigint);
413 (void)signal(SIGQUIT, sigquit);
415 if (!WIFEXITED(st)) {
420 return WEXITSTATUS(st);
423 static const struct got_error *
424 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
425 const char *initial_content)
427 const struct got_error *err = NULL;
431 int content_changed = 0;
436 if (stat(logmsg_path, &st) == -1)
437 return got_error_from_errno2("stat", logmsg_path);
439 if (spawn_editor(editor, logmsg_path) == -1)
440 return got_error_from_errno("failed spawning editor");
442 if (stat(logmsg_path, &st2) == -1)
443 return got_error_from_errno("stat");
445 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
446 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
447 "no changes made to commit message, aborting");
449 *logmsg = malloc(st2.st_size + 1);
451 return got_error_from_errno("malloc");
455 fp = fopen(logmsg_path, "r");
457 err = got_error_from_errno("fopen");
460 while (fgets(buf, sizeof(buf), fp) != NULL) {
461 if (!content_changed && strcmp(buf, initial_content) != 0)
463 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
464 continue; /* remove comments and leading empty lines */
465 len = strlcat(*logmsg, buf, st2.st_size);
469 while (len > 0 && (*logmsg)[len - 1] == '\n') {
470 (*logmsg)[len - 1] = '\0';
474 if (len == 0 || !content_changed)
475 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
476 "commit message cannot be empty, aborting");
485 static const struct got_error *
486 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
487 const char *path_dir, const char *branch_name)
489 char *initial_content = NULL;
490 const struct got_error *err = NULL;
491 int initial_content_len;
494 initial_content_len = asprintf(&initial_content,
495 "\n# %s to be imported to branch %s\n", path_dir,
497 if (initial_content_len == -1)
498 return got_error_from_errno("asprintf");
500 err = got_opentemp_named_fd(logmsg_path, &fd,
501 GOT_TMPDIR_STR "/got-importmsg");
505 if (write(fd, initial_content, initial_content_len) == -1) {
506 err = got_error_from_errno2("write", *logmsg_path);
510 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
512 if (fd != -1 && close(fd) == -1 && err == NULL)
513 err = got_error_from_errno2("close", *logmsg_path);
514 free(initial_content);
522 static const struct got_error *
523 import_progress(void *arg, const char *path)
525 printf("A %s\n", path);
529 static const struct got_error *
530 get_author(char **author, struct got_repository *repo,
531 struct got_worktree *worktree)
533 const struct got_error *err = NULL;
534 const char *got_author = NULL, *name, *email;
535 const struct got_gotconfig *worktree_conf = NULL, *repo_conf = NULL;
540 worktree_conf = got_worktree_get_gotconfig(worktree);
541 repo_conf = got_repo_get_gotconfig(repo);
544 * Priority of potential author information sources, from most
545 * significant to least significant:
546 * 1) work tree's .got/got.conf file
547 * 2) repository's got.conf file
548 * 3) repository's git config file
549 * 4) environment variables
550 * 5) global git config files (in user's home directory or /etc)
554 got_author = got_gotconfig_get_author(worktree_conf);
555 if (got_author == NULL)
556 got_author = got_gotconfig_get_author(repo_conf);
557 if (got_author == NULL) {
558 name = got_repo_get_gitconfig_author_name(repo);
559 email = got_repo_get_gitconfig_author_email(repo);
561 if (asprintf(author, "%s <%s>", name, email) == -1)
562 return got_error_from_errno("asprintf");
566 got_author = getenv("GOT_AUTHOR");
567 if (got_author == NULL) {
568 name = got_repo_get_global_gitconfig_author_name(repo);
569 email = got_repo_get_global_gitconfig_author_email(
572 if (asprintf(author, "%s <%s>", name, email)
574 return got_error_from_errno("asprintf");
577 /* TODO: Look up user in password database? */
578 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
582 *author = strdup(got_author);
584 return got_error_from_errno("strdup");
587 * Really dumb email address check; we're only doing this to
588 * avoid git's object parser breaking on commits we create.
590 while (*got_author && *got_author != '<')
592 if (*got_author != '<') {
593 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
596 while (*got_author && *got_author != '@')
598 if (*got_author != '@') {
599 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
602 while (*got_author && *got_author != '>')
604 if (*got_author != '>')
605 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
614 static const struct got_error *
615 get_gitconfig_path(char **gitconfig_path)
617 const char *homedir = getenv("HOME");
619 *gitconfig_path = NULL;
621 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
622 return got_error_from_errno("asprintf");
628 static const struct got_error *
629 cmd_import(int argc, char *argv[])
631 const struct got_error *error = NULL;
632 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
633 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
634 const char *branch_name = "main";
635 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
636 struct got_repository *repo = NULL;
637 struct got_reference *branch_ref = NULL, *head_ref = NULL;
638 struct got_object_id *new_commit_id = NULL;
640 struct got_pathlist_head ignores;
641 struct got_pathlist_entry *pe;
642 int preserve_logmsg = 0;
644 TAILQ_INIT(&ignores);
646 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
649 branch_name = optarg;
652 logmsg = strdup(optarg);
653 if (logmsg == NULL) {
654 error = got_error_from_errno("strdup");
659 repo_path = realpath(optarg, NULL);
660 if (repo_path == NULL) {
661 error = got_error_from_errno2("realpath",
667 if (optarg[0] == '\0')
669 error = got_pathlist_insert(&pe, &ignores, optarg,
684 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
692 if (repo_path == NULL) {
693 repo_path = getcwd(NULL, 0);
694 if (repo_path == NULL)
695 return got_error_from_errno("getcwd");
697 got_path_strip_trailing_slashes(repo_path);
698 error = get_gitconfig_path(&gitconfig_path);
701 error = got_repo_open(&repo, repo_path, gitconfig_path);
705 error = get_author(&author, repo, NULL);
710 * Don't let the user create a branch name with a leading '-'.
711 * While technically a valid reference name, this case is usually
712 * an unintended typo.
714 if (branch_name[0] == '-')
715 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
717 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
718 error = got_error_from_errno("asprintf");
722 error = got_ref_open(&branch_ref, repo, refname, 0);
724 if (error->code != GOT_ERR_NOT_REF)
727 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
728 "import target branch already exists");
732 path_dir = realpath(argv[0], NULL);
733 if (path_dir == NULL) {
734 error = got_error_from_errno2("realpath", argv[0]);
737 got_path_strip_trailing_slashes(path_dir);
740 * unveil(2) traverses exec(2); if an editor is used we have
741 * to apply unveil after the log message has been written.
743 if (logmsg == NULL || strlen(logmsg) == 0) {
744 error = get_editor(&editor);
748 error = collect_import_msg(&logmsg, &logmsg_path, editor,
751 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
758 if (unveil(path_dir, "r") != 0) {
759 error = got_error_from_errno2("unveil", path_dir);
765 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
772 error = got_repo_import(&new_commit_id, path_dir, logmsg,
773 author, &ignores, repo, import_progress, NULL);
780 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
787 error = got_ref_write(branch_ref, repo);
794 error = got_object_id_str(&id_str, new_commit_id);
801 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
803 if (error->code != GOT_ERR_NOT_REF) {
809 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
817 error = got_ref_write(head_ref, repo);
825 printf("Created branch %s with commit %s\n",
826 got_ref_get_name(branch_ref), id_str);
828 if (preserve_logmsg) {
829 fprintf(stderr, "%s: log message preserved in %s\n",
830 getprogname(), logmsg_path);
831 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
832 error = got_error_from_errno2("unlink", logmsg_path);
841 free(gitconfig_path);
843 got_ref_close(branch_ref);
845 got_ref_close(head_ref);
852 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
853 "[-R reference] repository-url [directory]\n", getprogname());
857 struct got_fetch_progress_arg {
858 char last_scaled_size[FMT_SCALED_STRSIZE];
864 static const struct got_error *
865 fetch_progress(void *arg, const char *message, off_t packfile_size,
866 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
868 struct got_fetch_progress_arg *a = arg;
869 char scaled_size[FMT_SCALED_STRSIZE];
870 int p_indexed, p_resolved;
871 int print_size = 0, print_indexed = 0, print_resolved = 0;
873 if (a->verbosity < 0)
876 if (message && message[0] != '\0') {
877 printf("\rserver: %s", message);
882 if (packfile_size > 0 || nobj_indexed > 0) {
883 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
884 (a->last_scaled_size[0] == '\0' ||
885 strcmp(scaled_size, a->last_scaled_size)) != 0) {
887 if (strlcpy(a->last_scaled_size, scaled_size,
888 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
889 return got_error(GOT_ERR_NO_SPACE);
891 if (nobj_indexed > 0) {
892 p_indexed = (nobj_indexed * 100) / nobj_total;
893 if (p_indexed != a->last_p_indexed) {
894 a->last_p_indexed = p_indexed;
899 if (nobj_resolved > 0) {
900 p_resolved = (nobj_resolved * 100) /
901 (nobj_total - nobj_loose);
902 if (p_resolved != a->last_p_resolved) {
903 a->last_p_resolved = p_resolved;
911 if (print_size || print_indexed || print_resolved)
914 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
916 printf("; indexing %d%%", p_indexed);
918 printf("; resolving deltas %d%%", p_resolved);
919 if (print_size || print_indexed || print_resolved)
925 static const struct got_error *
926 create_symref(struct got_reference **head_symref, const char *refname,
927 struct got_reference *target_ref, int verbosity,
928 struct got_repository *repo)
930 const struct got_error *err;
932 err = got_ref_alloc_symref(head_symref, refname, target_ref);
936 err = got_ref_write(*head_symref, repo);
937 if (err == NULL && verbosity > 0) {
938 printf("Created reference %s: %s\n", GOT_REF_HEAD,
939 got_ref_get_name(target_ref));
944 static const struct got_error *
945 list_remote_refs(struct got_pathlist_head *symrefs,
946 struct got_pathlist_head *refs)
948 const struct got_error *err;
949 struct got_pathlist_entry *pe;
951 TAILQ_FOREACH(pe, symrefs, entry) {
952 const char *refname = pe->path;
953 const char *targetref = pe->data;
955 printf("%s: %s\n", refname, targetref);
958 TAILQ_FOREACH(pe, refs, entry) {
959 const char *refname = pe->path;
960 struct got_object_id *id = pe->data;
963 err = got_object_id_str(&id_str, id);
966 printf("%s: %s\n", refname, id_str);
973 static const struct got_error *
974 create_ref(const char *refname, struct got_object_id *id,
975 int verbosity, struct got_repository *repo)
977 const struct got_error *err = NULL;
978 struct got_reference *ref;
981 err = got_object_id_str(&id_str, id);
985 err = got_ref_alloc(&ref, refname, id);
989 err = got_ref_write(ref, repo);
992 if (err == NULL && verbosity >= 0)
993 printf("Created reference %s: %s\n", refname, id_str);
1000 match_wanted_ref(const char *refname, const char *wanted_ref)
1002 if (strncmp(refname, "refs/", 5) != 0)
1007 * Prevent fetching of references that won't make any
1008 * sense outside of the remote repository's context.
1010 if (strncmp(refname, "got/", 4) == 0)
1012 if (strncmp(refname, "remotes/", 8) == 0)
1015 if (strncmp(wanted_ref, "refs/", 5) == 0)
1018 /* Allow prefix match. */
1019 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
1022 /* Allow exact match. */
1023 return (strcmp(refname, wanted_ref) == 0);
1027 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
1029 struct got_pathlist_entry *pe;
1031 TAILQ_FOREACH(pe, wanted_refs, entry) {
1032 if (match_wanted_ref(refname, pe->path))
1039 static const struct got_error *
1040 create_wanted_ref(const char *refname, struct got_object_id *id,
1041 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1043 const struct got_error *err;
1044 char *remote_refname;
1046 if (strncmp("refs/", refname, 5) == 0)
1049 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1050 remote_repo_name, refname) == -1)
1051 return got_error_from_errno("asprintf");
1053 err = create_ref(remote_refname, id, verbosity, repo);
1054 free(remote_refname);
1058 static const struct got_error *
1059 create_gotconfig(const char *proto, const char *host, const char *port,
1060 char *remote_repo_path, int fetch_all_branches, int mirror_references,
1061 struct got_repository *repo)
1063 const struct got_error *err = NULL;
1064 char *gotconfig_path = NULL;
1065 char *gotconfig = NULL;
1066 FILE *gotconfig_file = NULL;
1069 /* Create got.conf(5). */
1070 gotconfig_path = got_repo_get_path_gotconfig(repo);
1071 if (gotconfig_path == NULL) {
1072 err = got_error_from_errno("got_repo_get_path_gotconfig");
1075 gotconfig_file = fopen(gotconfig_path, "a");
1076 if (gotconfig_file == NULL) {
1077 err = got_error_from_errno2("fopen", gotconfig_path);
1080 got_path_strip_trailing_slashes(remote_repo_path);
1081 if (asprintf(&gotconfig,
1086 "\trepository \"%s\"\n"
1089 GOT_FETCH_DEFAULT_REMOTE_NAME, host, proto,
1090 port ? "\tport " : "", port ? port : "", port ? "\n" : "",
1092 mirror_references ? "\tmirror-references yes\n" : "") == -1) {
1093 err = got_error_from_errno("asprintf");
1096 n = fwrite(gotconfig, 1, strlen(gotconfig), gotconfig_file);
1097 if (n != strlen(gotconfig)) {
1098 err = got_ferror(gotconfig_file, GOT_ERR_IO);
1103 if (gotconfig_file && fclose(gotconfig_file) == EOF && err == NULL)
1104 err = got_error_from_errno2("fclose", gotconfig_path);
1105 free(gotconfig_path);
1109 static const struct got_error *
1110 create_gitconfig(const char *git_url, struct got_reference *default_head,
1111 int fetch_all_branches, int mirror_references, struct got_repository *repo)
1113 const struct got_error *err = NULL;
1114 char *gitconfig_path = NULL;
1115 char *gitconfig = NULL;
1116 FILE *gitconfig_file = NULL;
1119 /* Create a config file Git can understand. */
1120 gitconfig_path = got_repo_get_path_gitconfig(repo);
1121 if (gitconfig_path == NULL) {
1122 err = got_error_from_errno("got_repo_get_path_gitconfig");
1125 gitconfig_file = fopen(gitconfig_path, "a");
1126 if (gitconfig_file == NULL) {
1127 err = got_error_from_errno2("fopen", gitconfig_path);
1130 if (mirror_references) {
1131 if (asprintf(&gitconfig,
1134 "\tfetch = +refs/*:refs/*\n"
1135 "\tmirror = true\n",
1136 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1137 err = got_error_from_errno("asprintf");
1140 } else if (fetch_all_branches) {
1141 if (asprintf(&gitconfig,
1144 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1145 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1146 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1147 err = got_error_from_errno("asprintf");
1151 const char *branchname;
1154 * If the server specified a default branch, use just that one.
1155 * Otherwise fall back to fetching all branches on next fetch.
1158 branchname = got_ref_get_symref_target(default_head);
1159 if (strncmp(branchname, "refs/heads/", 11) == 0)
1162 branchname = "*"; /* fall back to all branches */
1163 if (asprintf(&gitconfig,
1166 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1167 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1168 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1169 branchname) == -1) {
1170 err = got_error_from_errno("asprintf");
1174 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1175 if (n != strlen(gitconfig)) {
1176 err = got_ferror(gitconfig_file, GOT_ERR_IO);
1180 if (gitconfig_file && fclose(gitconfig_file) == EOF && err == NULL)
1181 err = got_error_from_errno2("fclose", gitconfig_path);
1182 free(gitconfig_path);
1186 static const struct got_error *
1187 cmd_clone(int argc, char *argv[])
1189 const struct got_error *error = NULL;
1190 const char *uri, *dirname;
1191 char *proto, *host, *port, *repo_name, *server_path;
1192 char *default_destdir = NULL, *id_str = NULL;
1193 const char *repo_path;
1194 struct got_repository *repo = NULL;
1195 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1196 struct got_pathlist_entry *pe;
1197 struct got_object_id *pack_hash = NULL;
1198 int ch, fetchfd = -1, fetchstatus;
1199 pid_t fetchpid = -1;
1200 struct got_fetch_progress_arg fpa;
1201 char *git_url = NULL;
1202 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1203 int list_refs_only = 0;
1204 struct got_reference *default_head = NULL;
1207 TAILQ_INIT(&symrefs);
1208 TAILQ_INIT(&wanted_branches);
1209 TAILQ_INIT(&wanted_refs);
1211 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1214 fetch_all_branches = 1;
1217 error = got_pathlist_append(&wanted_branches,
1226 mirror_references = 1;
1231 else if (verbosity < 3)
1238 error = got_pathlist_append(&wanted_refs,
1251 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1252 errx(1, "-a and -b options are mutually exclusive");
1253 if (list_refs_only) {
1254 if (!TAILQ_EMPTY(&wanted_branches))
1255 errx(1, "-l and -b options are mutually exclusive");
1256 if (fetch_all_branches)
1257 errx(1, "-l and -a options are mutually exclusive");
1258 if (mirror_references)
1259 errx(1, "-l and -m options are mutually exclusive");
1260 if (verbosity == -1)
1261 errx(1, "-l and -q options are mutually exclusive");
1262 if (!TAILQ_EMPTY(&wanted_refs))
1263 errx(1, "-l and -R options are mutually exclusive");
1275 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1280 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1281 host, port ? ":" : "", port ? port : "",
1282 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1283 error = got_error_from_errno("asprintf");
1287 if (strcmp(proto, "git") == 0) {
1289 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1290 "sendfd dns inet unveil", NULL) == -1)
1293 } else if (strcmp(proto, "git+ssh") == 0 ||
1294 strcmp(proto, "ssh") == 0) {
1296 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1297 "sendfd unveil", NULL) == -1)
1300 } else if (strcmp(proto, "http") == 0 ||
1301 strcmp(proto, "git+http") == 0) {
1302 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1305 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1308 if (dirname == NULL) {
1309 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1310 error = got_error_from_errno("asprintf");
1313 repo_path = default_destdir;
1315 repo_path = dirname;
1317 if (!list_refs_only) {
1318 error = got_path_mkdir(repo_path);
1320 (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1321 !(error->code == GOT_ERR_ERRNO && errno == EEXIST)))
1323 if (!got_path_dir_is_empty(repo_path)) {
1324 error = got_error_path(repo_path,
1325 GOT_ERR_DIR_NOT_EMPTY);
1330 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1331 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1332 error = got_error_from_errno2("unveil",
1333 GOT_FETCH_PATH_SSH);
1337 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1342 printf("Connecting to %s%s%s\n", host,
1343 port ? ":" : "", port ? port : "");
1345 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1346 server_path, verbosity);
1350 if (!list_refs_only) {
1351 error = got_repo_init(repo_path);
1354 error = got_repo_open(&repo, repo_path, NULL);
1359 fpa.last_scaled_size[0] = '\0';
1360 fpa.last_p_indexed = -1;
1361 fpa.last_p_resolved = -1;
1362 fpa.verbosity = verbosity;
1363 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1364 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1365 fetch_all_branches, &wanted_branches, &wanted_refs,
1366 list_refs_only, verbosity, fetchfd, repo,
1367 fetch_progress, &fpa);
1371 if (list_refs_only) {
1372 error = list_remote_refs(&symrefs, &refs);
1376 error = got_object_id_str(&id_str, pack_hash);
1380 printf("\nFetched %s.pack\n", id_str);
1383 /* Set up references provided with the pack file. */
1384 TAILQ_FOREACH(pe, &refs, entry) {
1385 const char *refname = pe->path;
1386 struct got_object_id *id = pe->data;
1387 char *remote_refname;
1389 if (is_wanted_ref(&wanted_refs, refname) &&
1390 !mirror_references) {
1391 error = create_wanted_ref(refname, id,
1392 GOT_FETCH_DEFAULT_REMOTE_NAME,
1393 verbosity - 1, repo);
1399 error = create_ref(refname, id, verbosity - 1, repo);
1403 if (mirror_references)
1406 if (strncmp("refs/heads/", refname, 11) != 0)
1409 if (asprintf(&remote_refname,
1410 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1411 refname + 11) == -1) {
1412 error = got_error_from_errno("asprintf");
1415 error = create_ref(remote_refname, id, verbosity - 1, repo);
1416 free(remote_refname);
1421 /* Set the HEAD reference if the server provided one. */
1422 TAILQ_FOREACH(pe, &symrefs, entry) {
1423 struct got_reference *target_ref;
1424 const char *refname = pe->path;
1425 const char *target = pe->data;
1426 char *remote_refname = NULL, *remote_target = NULL;
1427 struct got_reference *head_symref;
1429 if (strcmp(refname, GOT_REF_HEAD) != 0)
1432 error = got_ref_open(&target_ref, repo, target, 0);
1434 if (error->code == GOT_ERR_NOT_REF) {
1441 error = create_symref(&head_symref, refname, target_ref,
1443 got_ref_close(target_ref);
1447 /* First HEAD reference listed is the default branch. */
1448 if (default_head == NULL)
1449 default_head = head_symref;
1451 got_ref_close(head_symref);
1453 if (mirror_references)
1456 if (strncmp("refs/heads/", target, 11) != 0)
1459 if (asprintf(&remote_refname,
1460 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1462 error = got_error_from_errno("asprintf");
1465 if (asprintf(&remote_target,
1466 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1467 target + 11) == -1) {
1468 error = got_error_from_errno("asprintf");
1469 free(remote_refname);
1472 error = got_ref_open(&target_ref, repo, remote_target, 0);
1474 free(remote_refname);
1475 free(remote_target);
1476 if (error->code == GOT_ERR_NOT_REF) {
1482 error = create_symref(&head_symref, remote_refname,
1483 target_ref, verbosity - 1, repo);
1484 free(remote_refname);
1485 free(remote_target);
1486 got_ref_close(target_ref);
1487 got_ref_close(head_symref);
1493 * We failed to set the HEAD reference. If we asked for
1494 * a set of wanted branches use the first of one of those
1495 * which could be fetched instead.
1497 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1498 const char *target = pe->path;
1499 struct got_reference *target_ref;
1501 error = got_ref_open(&target_ref, repo, target, 0);
1503 if (error->code == GOT_ERR_NOT_REF) {
1510 error = create_symref(&default_head, GOT_REF_HEAD,
1511 target_ref, verbosity, repo);
1512 got_ref_close(target_ref);
1519 /* Create got.conf(5). */
1520 error = create_gotconfig(proto, host, port, server_path,
1521 fetch_all_branches, mirror_references, repo);
1525 /* Create a config file Git can understand. */
1526 error = create_gitconfig(git_url, default_head, fetch_all_branches,
1527 mirror_references, repo);
1532 printf("Created %s repository '%s'\n",
1533 mirror_references ? "mirrored" : "cloned", repo_path);
1536 if (kill(fetchpid, SIGTERM) == -1)
1537 error = got_error_from_errno("kill");
1538 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1539 error = got_error_from_errno("waitpid");
1541 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1542 error = got_error_from_errno("close");
1544 got_repo_close(repo);
1546 got_ref_close(default_head);
1547 TAILQ_FOREACH(pe, &refs, entry) {
1548 free((void *)pe->path);
1551 got_pathlist_free(&refs);
1552 TAILQ_FOREACH(pe, &symrefs, entry) {
1553 free((void *)pe->path);
1556 got_pathlist_free(&symrefs);
1557 got_pathlist_free(&wanted_branches);
1558 got_pathlist_free(&wanted_refs);
1565 free(default_destdir);
1570 static const struct got_error *
1571 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1572 int replace_tags, int verbosity, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 char *new_id_str = NULL;
1576 struct got_object_id *old_id = NULL;
1578 err = got_object_id_str(&new_id_str, new_id);
1582 if (!replace_tags &&
1583 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1584 err = got_ref_resolve(&old_id, repo, ref);
1587 if (got_object_id_cmp(old_id, new_id) == 0)
1589 if (verbosity >= 0) {
1590 printf("Rejecting update of existing tag %s: %s\n",
1591 got_ref_get_name(ref), new_id_str);
1596 if (got_ref_is_symbolic(ref)) {
1597 if (verbosity >= 0) {
1598 printf("Replacing reference %s: %s\n",
1599 got_ref_get_name(ref),
1600 got_ref_get_symref_target(ref));
1602 err = got_ref_change_symref_to_ref(ref, new_id);
1605 err = got_ref_write(ref, repo);
1609 err = got_ref_resolve(&old_id, repo, ref);
1612 if (got_object_id_cmp(old_id, new_id) == 0)
1615 err = got_ref_change_ref(ref, new_id);
1618 err = got_ref_write(ref, repo);
1624 printf("Updated %s: %s\n", got_ref_get_name(ref),
1632 static const struct got_error *
1633 update_symref(const char *refname, struct got_reference *target_ref,
1634 int verbosity, struct got_repository *repo)
1636 const struct got_error *err = NULL, *unlock_err;
1637 struct got_reference *symref;
1638 int symref_is_locked = 0;
1640 err = got_ref_open(&symref, repo, refname, 1);
1642 if (err->code != GOT_ERR_NOT_REF)
1644 err = got_ref_alloc_symref(&symref, refname, target_ref);
1648 err = got_ref_write(symref, repo);
1653 printf("Created reference %s: %s\n",
1654 got_ref_get_name(symref),
1655 got_ref_get_symref_target(symref));
1657 symref_is_locked = 1;
1659 if (strcmp(got_ref_get_symref_target(symref),
1660 got_ref_get_name(target_ref)) == 0)
1663 err = got_ref_change_symref(symref,
1664 got_ref_get_name(target_ref));
1668 err = got_ref_write(symref, repo);
1673 printf("Updated %s: %s\n", got_ref_get_name(symref),
1674 got_ref_get_symref_target(symref));
1678 if (symref_is_locked) {
1679 unlock_err = got_ref_unlock(symref);
1680 if (unlock_err && err == NULL)
1683 got_ref_close(symref);
1690 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1691 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1692 "[remote-repository-name]\n",
1697 static const struct got_error *
1698 delete_missing_ref(struct got_reference *ref,
1699 int verbosity, struct got_repository *repo)
1701 const struct got_error *err = NULL;
1702 struct got_object_id *id = NULL;
1703 char *id_str = NULL;
1705 if (got_ref_is_symbolic(ref)) {
1706 err = got_ref_delete(ref, repo);
1709 if (verbosity >= 0) {
1710 printf("Deleted reference %s: %s\n",
1711 got_ref_get_name(ref),
1712 got_ref_get_symref_target(ref));
1715 err = got_ref_resolve(&id, repo, ref);
1718 err = got_object_id_str(&id_str, id);
1722 err = got_ref_delete(ref, repo);
1725 if (verbosity >= 0) {
1726 printf("Deleted reference %s: %s\n",
1727 got_ref_get_name(ref), id_str);
1736 static const struct got_error *
1737 delete_missing_refs(struct got_pathlist_head *their_refs,
1738 struct got_pathlist_head *their_symrefs,
1739 const struct got_remote_repo *remote,
1740 int verbosity, struct got_repository *repo)
1742 const struct got_error *err = NULL, *unlock_err;
1743 struct got_reflist_head my_refs;
1744 struct got_reflist_entry *re;
1745 struct got_pathlist_entry *pe;
1746 char *remote_namespace = NULL;
1747 char *local_refname = NULL;
1749 SIMPLEQ_INIT(&my_refs);
1751 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1753 return got_error_from_errno("asprintf");
1755 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1759 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1760 const char *refname = got_ref_get_name(re->ref);
1762 if (!remote->mirror_references) {
1763 if (strncmp(refname, remote_namespace,
1764 strlen(remote_namespace)) == 0) {
1765 if (strcmp(refname + strlen(remote_namespace),
1768 if (asprintf(&local_refname, "refs/heads/%s",
1769 refname + strlen(remote_namespace)) == -1) {
1770 err = got_error_from_errno("asprintf");
1773 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1777 TAILQ_FOREACH(pe, their_refs, entry) {
1778 if (strcmp(local_refname, pe->path) == 0)
1784 TAILQ_FOREACH(pe, their_symrefs, entry) {
1785 if (strcmp(local_refname, pe->path) == 0)
1791 err = delete_missing_ref(re->ref, verbosity, repo);
1795 if (local_refname) {
1796 struct got_reference *ref;
1797 err = got_ref_open(&ref, repo, local_refname, 1);
1799 if (err->code != GOT_ERR_NOT_REF)
1801 free(local_refname);
1802 local_refname = NULL;
1805 err = delete_missing_ref(ref, verbosity, repo);
1808 unlock_err = got_ref_unlock(ref);
1810 if (unlock_err && err == NULL) {
1815 free(local_refname);
1816 local_refname = NULL;
1820 free(remote_namespace);
1821 free(local_refname);
1825 static const struct got_error *
1826 update_wanted_ref(const char *refname, struct got_object_id *id,
1827 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1829 const struct got_error *err, *unlock_err;
1830 char *remote_refname;
1831 struct got_reference *ref;
1833 if (strncmp("refs/", refname, 5) == 0)
1836 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1837 remote_repo_name, refname) == -1)
1838 return got_error_from_errno("asprintf");
1840 err = got_ref_open(&ref, repo, remote_refname, 1);
1842 if (err->code != GOT_ERR_NOT_REF)
1844 err = create_ref(remote_refname, id, verbosity, repo);
1846 err = update_ref(ref, id, 0, verbosity, repo);
1847 unlock_err = got_ref_unlock(ref);
1848 if (unlock_err && err == NULL)
1853 free(remote_refname);
1857 static const struct got_error *
1858 cmd_fetch(int argc, char *argv[])
1860 const struct got_error *error = NULL, *unlock_err;
1861 char *cwd = NULL, *repo_path = NULL;
1862 const char *remote_name;
1863 char *proto = NULL, *host = NULL, *port = NULL;
1864 char *repo_name = NULL, *server_path = NULL;
1865 const struct got_remote_repo *remotes, *remote = NULL;
1867 char *id_str = NULL;
1868 struct got_repository *repo = NULL;
1869 struct got_worktree *worktree = NULL;
1870 const struct got_gotconfig *repo_conf = NULL, *worktree_conf = NULL;
1871 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1872 struct got_pathlist_entry *pe;
1873 struct got_object_id *pack_hash = NULL;
1874 int i, ch, fetchfd = -1, fetchstatus;
1875 pid_t fetchpid = -1;
1876 struct got_fetch_progress_arg fpa;
1877 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1878 int delete_refs = 0, replace_tags = 0;
1881 TAILQ_INIT(&symrefs);
1882 TAILQ_INIT(&wanted_branches);
1883 TAILQ_INIT(&wanted_refs);
1885 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1888 fetch_all_branches = 1;
1891 error = got_pathlist_append(&wanted_branches,
1903 repo_path = realpath(optarg, NULL);
1904 if (repo_path == NULL)
1905 return got_error_from_errno2("realpath",
1907 got_path_strip_trailing_slashes(repo_path);
1915 else if (verbosity < 3)
1922 error = got_pathlist_append(&wanted_refs,
1935 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1936 errx(1, "-a and -b options are mutually exclusive");
1937 if (list_refs_only) {
1938 if (!TAILQ_EMPTY(&wanted_branches))
1939 errx(1, "-l and -b options are mutually exclusive");
1940 if (fetch_all_branches)
1941 errx(1, "-l and -a options are mutually exclusive");
1943 errx(1, "-l and -d options are mutually exclusive");
1944 if (verbosity == -1)
1945 errx(1, "-l and -q options are mutually exclusive");
1949 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1951 remote_name = argv[0];
1955 cwd = getcwd(NULL, 0);
1957 error = got_error_from_errno("getcwd");
1961 if (repo_path == NULL) {
1962 error = got_worktree_open(&worktree, cwd);
1963 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1969 strdup(got_worktree_get_repo_path(worktree));
1970 if (repo_path == NULL)
1971 error = got_error_from_errno("strdup");
1975 repo_path = strdup(cwd);
1976 if (repo_path == NULL) {
1977 error = got_error_from_errno("strdup");
1983 error = got_repo_open(&repo, repo_path, NULL);
1988 worktree_conf = got_worktree_get_gotconfig(worktree);
1989 if (worktree_conf) {
1990 got_gotconfig_get_remotes(&nremotes, &remotes,
1992 for (i = 0; i < nremotes; i++) {
1993 remote = &remotes[i];
1994 if (strcmp(remote->name, remote_name) == 0)
1999 if (remote == NULL) {
2000 repo_conf = got_repo_get_gotconfig(repo);
2002 got_gotconfig_get_remotes(&nremotes, &remotes,
2004 for (i = 0; i < nremotes; i++) {
2005 remote = &remotes[i];
2006 if (strcmp(remote->name, remote_name) == 0)
2011 if (remote == NULL) {
2012 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
2013 for (i = 0; i < nremotes; i++) {
2014 remote = &remotes[i];
2015 if (strcmp(remote->name, remote_name) == 0)
2019 if (remote == NULL) {
2020 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
2024 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
2025 &repo_name, remote->url);
2029 if (strcmp(proto, "git") == 0) {
2031 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2032 "sendfd dns inet unveil", NULL) == -1)
2035 } else if (strcmp(proto, "git+ssh") == 0 ||
2036 strcmp(proto, "ssh") == 0) {
2038 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2039 "sendfd unveil", NULL) == -1)
2042 } else if (strcmp(proto, "http") == 0 ||
2043 strcmp(proto, "git+http") == 0) {
2044 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
2047 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
2051 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
2052 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
2053 error = got_error_from_errno2("unveil",
2054 GOT_FETCH_PATH_SSH);
2058 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
2063 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
2064 port ? ":" : "", port ? port : "");
2066 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
2067 server_path, verbosity);
2071 fpa.last_scaled_size[0] = '\0';
2072 fpa.last_p_indexed = -1;
2073 fpa.last_p_resolved = -1;
2074 fpa.verbosity = verbosity;
2075 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
2076 remote->mirror_references, fetch_all_branches, &wanted_branches,
2077 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
2078 fetch_progress, &fpa);
2082 if (list_refs_only) {
2083 error = list_remote_refs(&symrefs, &refs);
2087 if (pack_hash == NULL) {
2089 printf("Already up-to-date\n");
2090 } else if (verbosity >= 0) {
2091 error = got_object_id_str(&id_str, pack_hash);
2094 printf("\nFetched %s.pack\n", id_str);
2099 /* Update references provided with the pack file. */
2100 TAILQ_FOREACH(pe, &refs, entry) {
2101 const char *refname = pe->path;
2102 struct got_object_id *id = pe->data;
2103 struct got_reference *ref;
2104 char *remote_refname;
2106 if (is_wanted_ref(&wanted_refs, refname) &&
2107 !remote->mirror_references) {
2108 error = update_wanted_ref(refname, id,
2109 remote->name, verbosity, repo);
2115 if (remote->mirror_references ||
2116 strncmp("refs/tags/", refname, 10) == 0) {
2117 error = got_ref_open(&ref, repo, refname, 1);
2119 if (error->code != GOT_ERR_NOT_REF)
2121 error = create_ref(refname, id, verbosity,
2126 error = update_ref(ref, id, replace_tags,
2128 unlock_err = got_ref_unlock(ref);
2129 if (unlock_err && error == NULL)
2135 } else if (strncmp("refs/heads/", refname, 11) == 0) {
2136 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2137 remote_name, refname + 11) == -1) {
2138 error = got_error_from_errno("asprintf");
2142 error = got_ref_open(&ref, repo, remote_refname, 1);
2144 if (error->code != GOT_ERR_NOT_REF)
2146 error = create_ref(remote_refname, id,
2151 error = update_ref(ref, id, replace_tags,
2153 unlock_err = got_ref_unlock(ref);
2154 if (unlock_err && error == NULL)
2161 /* Also create a local branch if none exists yet. */
2162 error = got_ref_open(&ref, repo, refname, 1);
2164 if (error->code != GOT_ERR_NOT_REF)
2166 error = create_ref(refname, id, verbosity,
2171 unlock_err = got_ref_unlock(ref);
2172 if (unlock_err && error == NULL)
2179 error = delete_missing_refs(&refs, &symrefs, remote,
2185 if (!remote->mirror_references) {
2186 /* Update remote HEAD reference if the server provided one. */
2187 TAILQ_FOREACH(pe, &symrefs, entry) {
2188 struct got_reference *target_ref;
2189 const char *refname = pe->path;
2190 const char *target = pe->data;
2191 char *remote_refname = NULL, *remote_target = NULL;
2193 if (strcmp(refname, GOT_REF_HEAD) != 0)
2196 if (strncmp("refs/heads/", target, 11) != 0)
2199 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2200 remote->name, refname) == -1) {
2201 error = got_error_from_errno("asprintf");
2204 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2205 remote->name, target + 11) == -1) {
2206 error = got_error_from_errno("asprintf");
2207 free(remote_refname);
2211 error = got_ref_open(&target_ref, repo, remote_target,
2214 free(remote_refname);
2215 free(remote_target);
2216 if (error->code == GOT_ERR_NOT_REF) {
2222 error = update_symref(remote_refname, target_ref,
2224 free(remote_refname);
2225 free(remote_target);
2226 got_ref_close(target_ref);
2233 if (kill(fetchpid, SIGTERM) == -1)
2234 error = got_error_from_errno("kill");
2235 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2236 error = got_error_from_errno("waitpid");
2238 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2239 error = got_error_from_errno("close");
2241 got_repo_close(repo);
2243 got_worktree_close(worktree);
2244 TAILQ_FOREACH(pe, &refs, entry) {
2245 free((void *)pe->path);
2248 got_pathlist_free(&refs);
2249 TAILQ_FOREACH(pe, &symrefs, entry) {
2250 free((void *)pe->path);
2253 got_pathlist_free(&symrefs);
2254 got_pathlist_free(&wanted_branches);
2255 got_pathlist_free(&wanted_refs);
2270 usage_checkout(void)
2272 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2273 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2278 show_worktree_base_ref_warning(void)
2280 fprintf(stderr, "%s: warning: could not create a reference "
2281 "to the work tree's base commit; the commit could be "
2282 "garbage-collected by Git; making the repository "
2283 "writable and running 'got update' will prevent this\n",
2287 struct got_checkout_progress_arg {
2288 const char *worktree_path;
2289 int had_base_commit_ref_error;
2292 static const struct got_error *
2293 checkout_progress(void *arg, unsigned char status, const char *path)
2295 struct got_checkout_progress_arg *a = arg;
2297 /* Base commit bump happens silently. */
2298 if (status == GOT_STATUS_BUMP_BASE)
2301 if (status == GOT_STATUS_BASE_REF_ERR) {
2302 a->had_base_commit_ref_error = 1;
2306 while (path[0] == '/')
2309 printf("%c %s/%s\n", status, a->worktree_path, path);
2313 static const struct got_error *
2314 check_cancelled(void *arg)
2316 if (sigint_received || sigpipe_received)
2317 return got_error(GOT_ERR_CANCELLED);
2321 static const struct got_error *
2322 check_linear_ancestry(struct got_object_id *commit_id,
2323 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2324 struct got_repository *repo)
2326 const struct got_error *err = NULL;
2327 struct got_object_id *yca_id;
2329 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2330 commit_id, base_commit_id, repo, check_cancelled, NULL);
2335 return got_error(GOT_ERR_ANCESTRY);
2338 * Require a straight line of history between the target commit
2339 * and the work tree's base commit.
2341 * Non-linear situations such as this require a rebase:
2343 * (commit) D F (base_commit)
2351 * 'got update' only handles linear cases:
2352 * Update forwards in time: A (base/yca) - B - C - D (commit)
2353 * Update backwards in time: D (base) - C - B - A (commit/yca)
2355 if (allow_forwards_in_time_only) {
2356 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2357 return got_error(GOT_ERR_ANCESTRY);
2358 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2359 got_object_id_cmp(base_commit_id, yca_id) != 0)
2360 return got_error(GOT_ERR_ANCESTRY);
2366 static const struct got_error *
2367 check_same_branch(struct got_object_id *commit_id,
2368 struct got_reference *head_ref, struct got_object_id *yca_id,
2369 struct got_repository *repo)
2371 const struct got_error *err = NULL;
2372 struct got_commit_graph *graph = NULL;
2373 struct got_object_id *head_commit_id = NULL;
2374 int is_same_branch = 0;
2376 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2380 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2384 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2389 err = got_commit_graph_open(&graph, "/", 1);
2393 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2394 check_cancelled, NULL);
2399 struct got_object_id *id;
2400 err = got_commit_graph_iter_next(&id, graph, repo,
2401 check_cancelled, NULL);
2403 if (err->code == GOT_ERR_ITER_COMPLETED)
2409 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2411 if (got_object_id_cmp(id, commit_id) == 0) {
2419 got_commit_graph_close(graph);
2420 free(head_commit_id);
2421 if (!err && !is_same_branch)
2422 err = got_error(GOT_ERR_ANCESTRY);
2426 static const struct got_error *
2427 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2429 static char msg[512];
2430 const char *branch_name;
2432 if (got_ref_is_symbolic(ref))
2433 branch_name = got_ref_get_symref_target(ref);
2435 branch_name = got_ref_get_name(ref);
2437 if (strncmp("refs/heads/", branch_name, 11) == 0)
2440 snprintf(msg, sizeof(msg),
2441 "target commit is not contained in branch '%s'; "
2442 "the branch to use must be specified with -b; "
2443 "if necessary a new branch can be created for "
2444 "this commit with 'got branch -c %s BRANCH_NAME'",
2445 branch_name, commit_id_str);
2447 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2450 static const struct got_error *
2451 cmd_checkout(int argc, char *argv[])
2453 const struct got_error *error = NULL;
2454 struct got_repository *repo = NULL;
2455 struct got_reference *head_ref = NULL;
2456 struct got_worktree *worktree = NULL;
2457 char *repo_path = NULL;
2458 char *worktree_path = NULL;
2459 const char *path_prefix = "";
2460 const char *branch_name = GOT_REF_HEAD;
2461 char *commit_id_str = NULL;
2462 char *cwd = NULL, *path = NULL;
2463 int ch, same_path_prefix, allow_nonempty = 0;
2464 struct got_pathlist_head paths;
2465 struct got_checkout_progress_arg cpa;
2469 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2472 branch_name = optarg;
2475 commit_id_str = strdup(optarg);
2476 if (commit_id_str == NULL)
2477 return got_error_from_errno("strdup");
2483 path_prefix = optarg;
2495 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2496 "unveil", NULL) == -1)
2500 char *base, *dotgit;
2501 repo_path = realpath(argv[0], NULL);
2502 if (repo_path == NULL)
2503 return got_error_from_errno2("realpath", argv[0]);
2504 cwd = getcwd(NULL, 0);
2506 error = got_error_from_errno("getcwd");
2510 path = strdup(path_prefix);
2512 path = strdup(repo_path);
2514 error = got_error_from_errno("strdup");
2517 base = basename(path);
2519 error = got_error_from_errno2("basename", path);
2522 dotgit = strstr(base, ".git");
2525 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2526 error = got_error_from_errno("asprintf");
2529 } else if (argc == 2) {
2530 repo_path = realpath(argv[0], NULL);
2531 if (repo_path == NULL) {
2532 error = got_error_from_errno2("realpath", argv[0]);
2535 worktree_path = realpath(argv[1], NULL);
2536 if (worktree_path == NULL) {
2537 if (errno != ENOENT) {
2538 error = got_error_from_errno2("realpath",
2542 worktree_path = strdup(argv[1]);
2543 if (worktree_path == NULL) {
2544 error = got_error_from_errno("strdup");
2551 got_path_strip_trailing_slashes(repo_path);
2552 got_path_strip_trailing_slashes(worktree_path);
2554 error = got_repo_open(&repo, repo_path, NULL);
2558 /* Pre-create work tree path for unveil(2) */
2559 error = got_path_mkdir(worktree_path);
2561 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2562 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2564 if (!allow_nonempty &&
2565 !got_path_dir_is_empty(worktree_path)) {
2566 error = got_error_path(worktree_path,
2567 GOT_ERR_DIR_NOT_EMPTY);
2572 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2576 error = got_ref_open(&head_ref, repo, branch_name, 0);
2580 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2581 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2584 error = got_worktree_open(&worktree, worktree_path);
2588 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2592 if (!same_path_prefix) {
2593 error = got_error(GOT_ERR_PATH_PREFIX);
2597 if (commit_id_str) {
2598 struct got_object_id *commit_id;
2599 error = got_repo_match_object_id(&commit_id, NULL,
2600 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2603 error = check_linear_ancestry(commit_id,
2604 got_worktree_get_base_commit_id(worktree), 0, repo);
2605 if (error != NULL) {
2607 if (error->code == GOT_ERR_ANCESTRY) {
2608 error = checkout_ancestry_error(
2609 head_ref, commit_id_str);
2613 error = check_same_branch(commit_id, head_ref, NULL, repo);
2615 if (error->code == GOT_ERR_ANCESTRY) {
2616 error = checkout_ancestry_error(
2617 head_ref, commit_id_str);
2621 error = got_worktree_set_base_commit_id(worktree, repo,
2628 error = got_pathlist_append(&paths, "", NULL);
2631 cpa.worktree_path = worktree_path;
2632 cpa.had_base_commit_ref_error = 0;
2633 error = got_worktree_checkout_files(worktree, &paths, repo,
2634 checkout_progress, &cpa, check_cancelled, NULL);
2638 printf("Now shut up and hack\n");
2639 if (cpa.had_base_commit_ref_error)
2640 show_worktree_base_ref_warning();
2642 got_pathlist_free(&paths);
2643 free(commit_id_str);
2645 free(worktree_path);
2651 struct got_update_progress_arg {
2659 print_update_progress_stats(struct got_update_progress_arg *upa)
2661 if (!upa->did_something)
2664 if (upa->conflicts > 0)
2665 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2666 if (upa->obstructed > 0)
2667 printf("File paths obstructed by a non-regular file: %d\n",
2669 if (upa->not_updated > 0)
2670 printf("Files not updated because of existing merge "
2671 "conflicts: %d\n", upa->not_updated);
2677 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2682 static const struct got_error *
2683 update_progress(void *arg, unsigned char status, const char *path)
2685 struct got_update_progress_arg *upa = arg;
2687 if (status == GOT_STATUS_EXISTS ||
2688 status == GOT_STATUS_BASE_REF_ERR)
2691 upa->did_something = 1;
2693 /* Base commit bump happens silently. */
2694 if (status == GOT_STATUS_BUMP_BASE)
2697 if (status == GOT_STATUS_CONFLICT)
2699 if (status == GOT_STATUS_OBSTRUCTED)
2701 if (status == GOT_STATUS_CANNOT_UPDATE)
2704 while (path[0] == '/')
2706 printf("%c %s\n", status, path);
2710 static const struct got_error *
2711 switch_head_ref(struct got_reference *head_ref,
2712 struct got_object_id *commit_id, struct got_worktree *worktree,
2713 struct got_repository *repo)
2715 const struct got_error *err = NULL;
2717 int ref_has_moved = 0;
2719 /* Trivial case: switching between two different references. */
2720 if (strcmp(got_ref_get_name(head_ref),
2721 got_worktree_get_head_ref_name(worktree)) != 0) {
2722 printf("Switching work tree from %s to %s\n",
2723 got_worktree_get_head_ref_name(worktree),
2724 got_ref_get_name(head_ref));
2725 return got_worktree_set_head_ref(worktree, head_ref);
2728 err = check_linear_ancestry(commit_id,
2729 got_worktree_get_base_commit_id(worktree), 0, repo);
2731 if (err->code != GOT_ERR_ANCESTRY)
2738 /* Switching to a rebased branch with the same reference name. */
2739 err = got_object_id_str(&base_id_str,
2740 got_worktree_get_base_commit_id(worktree));
2743 printf("Reference %s now points at a different branch\n",
2744 got_worktree_get_head_ref_name(worktree));
2745 printf("Switching work tree from %s to %s\n", base_id_str,
2746 got_worktree_get_head_ref_name(worktree));
2750 static const struct got_error *
2751 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2753 const struct got_error *err;
2756 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2760 return got_error(GOT_ERR_REBASING);
2762 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2766 return got_error(GOT_ERR_HISTEDIT_BUSY);
2771 static const struct got_error *
2772 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2773 char *argv[], struct got_worktree *worktree)
2775 const struct got_error *err = NULL;
2782 return got_error_from_errno("strdup");
2783 return got_pathlist_append(paths, path, NULL);
2786 for (i = 0; i < argc; i++) {
2787 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2790 err = got_pathlist_append(paths, path, NULL);
2800 static const struct got_error *
2801 wrap_not_worktree_error(const struct got_error *orig_err,
2802 const char *cmdname, const char *path)
2804 const struct got_error *err;
2805 struct got_repository *repo;
2806 static char msg[512];
2808 err = got_repo_open(&repo, path, NULL);
2812 snprintf(msg, sizeof(msg),
2813 "'got %s' needs a work tree in addition to a git repository\n"
2814 "Work trees can be checked out from this Git repository with "
2816 "The got(1) manual page contains more information.", cmdname);
2817 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2818 got_repo_close(repo);
2822 static const struct got_error *
2823 cmd_update(int argc, char *argv[])
2825 const struct got_error *error = NULL;
2826 struct got_repository *repo = NULL;
2827 struct got_worktree *worktree = NULL;
2828 char *worktree_path = NULL;
2829 struct got_object_id *commit_id = NULL;
2830 char *commit_id_str = NULL;
2831 const char *branch_name = NULL;
2832 struct got_reference *head_ref = NULL;
2833 struct got_pathlist_head paths;
2834 struct got_pathlist_entry *pe;
2836 struct got_update_progress_arg upa;
2840 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2843 branch_name = optarg;
2846 commit_id_str = strdup(optarg);
2847 if (commit_id_str == NULL)
2848 return got_error_from_errno("strdup");
2860 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2861 "unveil", NULL) == -1)
2864 worktree_path = getcwd(NULL, 0);
2865 if (worktree_path == NULL) {
2866 error = got_error_from_errno("getcwd");
2869 error = got_worktree_open(&worktree, worktree_path);
2871 if (error->code == GOT_ERR_NOT_WORKTREE)
2872 error = wrap_not_worktree_error(error, "update",
2877 error = check_rebase_or_histedit_in_progress(worktree);
2881 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2886 error = apply_unveil(got_repo_get_path(repo), 0,
2887 got_worktree_get_root_path(worktree));
2891 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2895 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2896 got_worktree_get_head_ref_name(worktree), 0);
2899 if (commit_id_str == NULL) {
2900 error = got_ref_resolve(&commit_id, repo, head_ref);
2903 error = got_object_id_str(&commit_id_str, commit_id);
2907 error = got_repo_match_object_id(&commit_id, NULL,
2908 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2909 free(commit_id_str);
2910 commit_id_str = NULL;
2913 error = got_object_id_str(&commit_id_str, commit_id);
2919 struct got_object_id *head_commit_id;
2920 TAILQ_FOREACH(pe, &paths, entry) {
2921 if (pe->path_len == 0)
2923 error = got_error_msg(GOT_ERR_BAD_PATH,
2924 "switching between branches requires that "
2925 "the entire work tree gets updated");
2928 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2931 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2933 free(head_commit_id);
2936 error = check_same_branch(commit_id, head_ref, NULL, repo);
2939 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2943 error = check_linear_ancestry(commit_id,
2944 got_worktree_get_base_commit_id(worktree), 0, repo);
2945 if (error != NULL) {
2946 if (error->code == GOT_ERR_ANCESTRY)
2947 error = got_error(GOT_ERR_BRANCH_MOVED);
2950 error = check_same_branch(commit_id, head_ref, NULL, repo);
2955 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2957 error = got_worktree_set_base_commit_id(worktree, repo,
2963 memset(&upa, 0, sizeof(upa));
2964 error = got_worktree_checkout_files(worktree, &paths, repo,
2965 update_progress, &upa, check_cancelled, NULL);
2969 if (upa.did_something)
2970 printf("Updated to commit %s\n", commit_id_str);
2972 printf("Already up-to-date\n");
2973 print_update_progress_stats(&upa);
2975 free(worktree_path);
2976 TAILQ_FOREACH(pe, &paths, entry)
2977 free((char *)pe->path);
2978 got_pathlist_free(&paths);
2980 free(commit_id_str);
2984 static const struct got_error *
2985 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2986 const char *path, int diff_context, int ignore_whitespace,
2987 struct got_repository *repo)
2989 const struct got_error *err = NULL;
2990 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2993 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2998 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
3002 while (path[0] == '/')
3004 err = got_diff_blob(blob1, blob2, path, path, diff_context,
3005 ignore_whitespace, stdout);
3008 got_object_blob_close(blob1);
3009 got_object_blob_close(blob2);
3013 static const struct got_error *
3014 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
3015 const char *path, int diff_context, int ignore_whitespace,
3016 struct got_repository *repo)
3018 const struct got_error *err = NULL;
3019 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3020 struct got_diff_blob_output_unidiff_arg arg;
3023 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3028 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3032 arg.diff_context = diff_context;
3033 arg.ignore_whitespace = ignore_whitespace;
3034 arg.outfile = stdout;
3035 while (path[0] == '/')
3037 err = got_diff_tree(tree1, tree2, path, path, repo,
3038 got_diff_blob_output_unidiff, &arg, 1);
3041 got_object_tree_close(tree1);
3043 got_object_tree_close(tree2);
3047 static const struct got_error *
3048 get_changed_paths(struct got_pathlist_head *paths,
3049 struct got_commit_object *commit, struct got_repository *repo)
3051 const struct got_error *err = NULL;
3052 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
3053 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3054 struct got_object_qid *qid;
3056 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3058 struct got_commit_object *pcommit;
3059 err = got_object_open_as_commit(&pcommit, repo,
3064 tree_id1 = got_object_commit_get_tree_id(pcommit);
3065 got_object_commit_close(pcommit);
3070 err = got_object_open_as_tree(&tree1, repo, tree_id1);
3075 tree_id2 = got_object_commit_get_tree_id(commit);
3076 err = got_object_open_as_tree(&tree2, repo, tree_id2);
3080 err = got_diff_tree(tree1, tree2, "", "", repo,
3081 got_diff_tree_collect_changed_paths, paths, 0);
3084 got_object_tree_close(tree1);
3086 got_object_tree_close(tree2);
3090 static const struct got_error *
3091 print_patch(struct got_commit_object *commit, struct got_object_id *id,
3092 const char *path, int diff_context, struct got_repository *repo)
3094 const struct got_error *err = NULL;
3095 struct got_commit_object *pcommit = NULL;
3096 char *id_str1 = NULL, *id_str2 = NULL;
3097 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
3098 struct got_object_qid *qid;
3100 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3102 err = got_object_open_as_commit(&pcommit, repo,
3108 if (path && path[0] != '\0') {
3110 err = got_object_id_by_path(&obj_id2, repo, id, path);
3113 err = got_object_id_str(&id_str2, obj_id2);
3119 err = got_object_id_by_path(&obj_id1, repo,
3122 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
3127 err = got_object_id_str(&id_str1, obj_id1);
3134 err = got_object_get_type(&obj_type, repo, obj_id2);
3139 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3141 case GOT_OBJ_TYPE_BLOB:
3142 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
3145 case GOT_OBJ_TYPE_TREE:
3146 err = diff_trees(obj_id1, obj_id2, path, diff_context,
3150 err = got_error(GOT_ERR_OBJ_TYPE);
3156 obj_id2 = got_object_commit_get_tree_id(commit);
3157 err = got_object_id_str(&id_str2, obj_id2);
3160 obj_id1 = got_object_commit_get_tree_id(pcommit);
3161 err = got_object_id_str(&id_str1, obj_id1);
3164 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3165 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3171 got_object_commit_close(pcommit);
3176 get_datestr(time_t *time, char *datebuf)
3178 struct tm mytm, *tm;
3181 tm = gmtime_r(time, &mytm);
3184 s = asctime_r(tm, datebuf);
3187 p = strchr(s, '\n');
3193 static const struct got_error *
3194 match_logmsg(int *have_match, struct got_object_id *id,
3195 struct got_commit_object *commit, regex_t *regex)
3197 const struct got_error *err = NULL;
3198 regmatch_t regmatch;
3199 char *id_str = NULL, *logmsg = NULL;
3203 err = got_object_id_str(&id_str, id);
3207 err = got_object_commit_get_logmsg(&logmsg, commit);
3211 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3220 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3223 regmatch_t regmatch;
3224 struct got_pathlist_entry *pe;
3228 TAILQ_FOREACH(pe, changed_paths, entry) {
3229 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3236 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3238 static const struct got_error *
3239 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3240 struct got_repository *repo, const char *path,
3241 struct got_pathlist_head *changed_paths, int show_patch,
3242 int diff_context, struct got_reflist_head *refs)
3244 const struct got_error *err = NULL;
3245 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3247 time_t committer_time;
3248 const char *author, *committer;
3249 char *refs_str = NULL;
3250 struct got_reflist_entry *re;
3252 SIMPLEQ_FOREACH(re, refs, entry) {
3255 struct got_tag_object *tag = NULL;
3256 struct got_object_id *ref_id;
3259 name = got_ref_get_name(re->ref);
3260 if (strcmp(name, GOT_REF_HEAD) == 0)
3262 if (strncmp(name, "refs/", 5) == 0)
3264 if (strncmp(name, "got/", 4) == 0)
3266 if (strncmp(name, "heads/", 6) == 0)
3268 if (strncmp(name, "remotes/", 8) == 0) {
3270 s = strstr(name, "/" GOT_REF_HEAD);
3271 if (s != NULL && s[strlen(s)] == '\0')
3274 err = got_ref_resolve(&ref_id, repo, re->ref);
3277 if (strncmp(name, "tags/", 5) == 0) {
3278 err = got_object_open_as_tag(&tag, repo, ref_id);
3280 if (err->code != GOT_ERR_OBJ_TYPE) {
3284 /* Ref points at something other than a tag. */
3289 cmp = got_object_id_cmp(tag ?
3290 got_object_tag_get_object_id(tag) : ref_id, id);
3293 got_object_tag_close(tag);
3297 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3299 err = got_error_from_errno("asprintf");
3305 err = got_object_id_str(&id_str, id);
3309 printf(GOT_COMMIT_SEP_STR);
3310 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3311 refs_str ? refs_str : "", refs_str ? ")" : "");
3316 printf("from: %s\n", got_object_commit_get_author(commit));
3317 committer_time = got_object_commit_get_committer_time(commit);
3318 datestr = get_datestr(&committer_time, datebuf);
3320 printf("date: %s UTC\n", datestr);
3321 author = got_object_commit_get_author(commit);
3322 committer = got_object_commit_get_committer(commit);
3323 if (strcmp(author, committer) != 0)
3324 printf("via: %s\n", committer);
3325 if (got_object_commit_get_nparents(commit) > 1) {
3326 const struct got_object_id_queue *parent_ids;
3327 struct got_object_qid *qid;
3329 parent_ids = got_object_commit_get_parent_ids(commit);
3330 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3331 err = got_object_id_str(&id_str, qid->id);
3334 printf("parent %d: %s\n", n++, id_str);
3339 err = got_object_commit_get_logmsg(&logmsg0, commit);
3345 line = strsep(&logmsg, "\n");
3347 printf(" %s\n", line);
3351 if (changed_paths) {
3352 struct got_pathlist_entry *pe;
3353 TAILQ_FOREACH(pe, changed_paths, entry) {
3354 struct got_diff_changed_path *cp = pe->data;
3355 printf(" %c %s\n", cp->status, pe->path);
3360 err = print_patch(commit, id, path, diff_context, repo);
3365 if (fflush(stdout) != 0 && err == NULL)
3366 err = got_error_from_errno("fflush");
3370 static const struct got_error *
3371 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3372 struct got_repository *repo, const char *path, int show_changed_paths,
3373 int show_patch, const char *search_pattern, int diff_context, int limit,
3374 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3376 const struct got_error *err;
3377 struct got_commit_graph *graph;
3380 struct got_object_id_queue reversed_commits;
3381 struct got_object_qid *qid;
3382 struct got_commit_object *commit;
3383 struct got_pathlist_head changed_paths;
3384 struct got_pathlist_entry *pe;
3386 SIMPLEQ_INIT(&reversed_commits);
3387 TAILQ_INIT(&changed_paths);
3389 if (search_pattern && regcomp(®ex, search_pattern,
3390 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3391 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3393 err = got_commit_graph_open(&graph, path, !log_branches);
3396 err = got_commit_graph_iter_start(graph, root_id, repo,
3397 check_cancelled, NULL);
3401 struct got_object_id *id;
3403 if (sigint_received || sigpipe_received)
3406 err = got_commit_graph_iter_next(&id, graph, repo,
3407 check_cancelled, NULL);
3409 if (err->code == GOT_ERR_ITER_COMPLETED)
3416 err = got_object_open_as_commit(&commit, repo, id);
3420 if (show_changed_paths && !reverse_display_order) {
3421 err = get_changed_paths(&changed_paths, commit, repo);
3426 if (search_pattern) {
3427 err = match_logmsg(&have_match, id, commit, ®ex);
3429 got_object_commit_close(commit);
3432 if (have_match == 0 && show_changed_paths)
3433 match_changed_paths(&have_match,
3434 &changed_paths, ®ex);
3435 if (have_match == 0) {
3436 got_object_commit_close(commit);
3437 TAILQ_FOREACH(pe, &changed_paths, entry) {
3438 free((char *)pe->path);
3441 got_pathlist_free(&changed_paths);
3446 if (reverse_display_order) {
3447 err = got_object_qid_alloc(&qid, id);
3450 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3451 got_object_commit_close(commit);
3453 err = print_commit(commit, id, repo, path,
3454 show_changed_paths ? &changed_paths : NULL,
3455 show_patch, diff_context, refs);
3456 got_object_commit_close(commit);
3460 if ((limit && --limit == 0) ||
3461 (end_id && got_object_id_cmp(id, end_id) == 0))
3464 TAILQ_FOREACH(pe, &changed_paths, entry) {
3465 free((char *)pe->path);
3468 got_pathlist_free(&changed_paths);
3470 if (reverse_display_order) {
3471 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3472 err = got_object_open_as_commit(&commit, repo, qid->id);
3475 if (show_changed_paths) {
3476 err = get_changed_paths(&changed_paths,
3481 err = print_commit(commit, qid->id, repo, path,
3482 show_changed_paths ? &changed_paths : NULL,
3483 show_patch, diff_context, refs);
3484 got_object_commit_close(commit);
3487 TAILQ_FOREACH(pe, &changed_paths, entry) {
3488 free((char *)pe->path);
3491 got_pathlist_free(&changed_paths);
3495 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3496 qid = SIMPLEQ_FIRST(&reversed_commits);
3497 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3498 got_object_qid_free(qid);
3500 TAILQ_FOREACH(pe, &changed_paths, entry) {
3501 free((char *)pe->path);
3504 got_pathlist_free(&changed_paths);
3507 got_commit_graph_close(graph);
3514 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3515 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3516 "[-R] [path]\n", getprogname());
3521 get_default_log_limit(void)
3523 const char *got_default_log_limit;
3527 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3528 if (got_default_log_limit == NULL)
3530 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3536 static const struct got_error *
3537 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3538 struct got_repository *repo)
3540 const struct got_error *err = NULL;
3541 struct got_reference *ref;
3545 err = got_ref_open(&ref, repo, commit_arg, 0);
3548 err = got_ref_resolve(id, repo, ref);
3552 err = got_object_get_type(&obj_type, repo, *id);
3555 if (obj_type == GOT_OBJ_TYPE_TAG) {
3556 struct got_tag_object *tag;
3557 err = got_object_open_as_tag(&tag, repo, *id);
3560 if (got_object_tag_get_object_type(tag) !=
3561 GOT_OBJ_TYPE_COMMIT) {
3562 got_object_tag_close(tag);
3563 return got_error(GOT_ERR_OBJ_TYPE);
3566 *id = got_object_id_dup(
3567 got_object_tag_get_object_id(tag));
3569 err = got_error_from_errno(
3570 "got_object_id_dup");
3571 got_object_tag_close(tag);
3574 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3575 return got_error(GOT_ERR_OBJ_TYPE);
3577 err = got_repo_match_object_id_prefix(id, commit_arg,
3578 GOT_OBJ_TYPE_COMMIT, repo);
3584 static const struct got_error *
3585 cmd_log(int argc, char *argv[])
3587 const struct got_error *error;
3588 struct got_repository *repo = NULL;
3589 struct got_worktree *worktree = NULL;
3590 struct got_object_id *start_id = NULL, *end_id = NULL;
3591 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3592 const char *start_commit = NULL, *end_commit = NULL;
3593 const char *search_pattern = NULL;
3594 int diff_context = -1, ch;
3595 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3596 int reverse_display_order = 0;
3598 struct got_reflist_head refs;
3600 SIMPLEQ_INIT(&refs);
3603 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3609 limit = get_default_log_limit();
3611 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3617 show_changed_paths = 1;
3620 start_commit = optarg;
3623 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3626 err(1, "-C option %s", errstr);
3629 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3631 err(1, "-l option %s", errstr);
3637 repo_path = realpath(optarg, NULL);
3638 if (repo_path == NULL)
3639 return got_error_from_errno2("realpath",
3641 got_path_strip_trailing_slashes(repo_path);
3644 reverse_display_order = 1;
3647 search_pattern = optarg;
3650 end_commit = optarg;
3661 if (diff_context == -1)
3663 else if (!show_patch)
3664 errx(1, "-C requires -p");
3666 cwd = getcwd(NULL, 0);
3668 error = got_error_from_errno("getcwd");
3672 if (repo_path == NULL) {
3673 error = got_worktree_open(&worktree, cwd);
3674 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3682 error = got_error_from_errno("strdup");
3685 } else if (argc == 1) {
3687 error = got_worktree_resolve_path(&path, worktree,
3692 path = strdup(argv[0]);
3694 error = got_error_from_errno("strdup");
3701 if (repo_path == NULL) {
3702 repo_path = worktree ?
3703 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3705 if (repo_path == NULL) {
3706 error = got_error_from_errno("strdup");
3710 error = got_repo_open(&repo, repo_path, NULL);
3714 error = apply_unveil(got_repo_get_path(repo), 1,
3715 worktree ? got_worktree_get_root_path(worktree) : NULL);
3719 if (start_commit == NULL) {
3720 struct got_reference *head_ref;
3721 struct got_commit_object *commit = NULL;
3722 error = got_ref_open(&head_ref, repo,
3723 worktree ? got_worktree_get_head_ref_name(worktree)
3727 error = got_ref_resolve(&start_id, repo, head_ref);
3728 got_ref_close(head_ref);
3731 error = got_object_open_as_commit(&commit, repo,
3735 got_object_commit_close(commit);
3737 error = resolve_commit_arg(&start_id, start_commit, repo);
3741 if (end_commit != NULL) {
3742 error = resolve_commit_arg(&end_id, end_commit, repo);
3748 const char *prefix = got_worktree_get_path_prefix(worktree);
3750 if (asprintf(&p, "%s%s%s", prefix,
3751 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3752 error = got_error_from_errno("asprintf");
3755 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3758 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3763 path = in_repo_path;
3766 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3770 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3771 show_patch, search_pattern, diff_context, limit, log_branches,
3772 reverse_display_order, &refs);
3778 got_worktree_close(worktree);
3780 const struct got_error *repo_error;
3781 repo_error = got_repo_close(repo);
3785 got_ref_list_free(&refs);
3792 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3793 "[-w] [object1 object2 | path]\n", getprogname());
3797 struct print_diff_arg {
3798 struct got_repository *repo;
3799 struct got_worktree *worktree;
3804 int ignore_whitespace;
3808 * Create a file which contains the target path of a symlink so we can feed
3809 * it as content to the diff engine.
3811 static const struct got_error *
3812 get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3813 const char *abspath)
3815 const struct got_error *err = NULL;
3816 char target_path[PATH_MAX];
3817 ssize_t target_len, outlen;
3822 target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3823 if (target_len == -1)
3824 return got_error_from_errno2("readlinkat", abspath);
3826 target_len = readlink(abspath, target_path, PATH_MAX);
3827 if (target_len == -1)
3828 return got_error_from_errno2("readlink", abspath);
3831 *fd = got_opentempfd();
3833 return got_error_from_errno("got_opentempfd");
3835 outlen = write(*fd, target_path, target_len);
3837 err = got_error_from_errno("got_opentempfd");
3841 if (lseek(*fd, 0, SEEK_SET) == -1) {
3842 err = got_error_from_errno2("lseek", abspath);
3853 static const struct got_error *
3854 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3855 const char *path, struct got_object_id *blob_id,
3856 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3857 int dirfd, const char *de_name)
3859 struct print_diff_arg *a = arg;
3860 const struct got_error *err = NULL;
3861 struct got_blob_object *blob1 = NULL;
3864 char *abspath = NULL, *label1 = NULL;
3867 if (a->diff_staged) {
3868 if (staged_status != GOT_STATUS_MODIFY &&
3869 staged_status != GOT_STATUS_ADD &&
3870 staged_status != GOT_STATUS_DELETE)
3873 if (staged_status == GOT_STATUS_DELETE)
3875 if (status == GOT_STATUS_NONEXISTENT)
3876 return got_error_set_errno(ENOENT, path);
3877 if (status != GOT_STATUS_MODIFY &&
3878 status != GOT_STATUS_ADD &&
3879 status != GOT_STATUS_DELETE &&
3880 status != GOT_STATUS_CONFLICT)
3884 if (!a->header_shown) {
3885 printf("diff %s %s%s\n", a->id_str,
3886 got_worktree_get_root_path(a->worktree),
3887 a->diff_staged ? " (staged changes)" : "");
3888 a->header_shown = 1;
3891 if (a->diff_staged) {
3892 const char *label1 = NULL, *label2 = NULL;
3893 switch (staged_status) {
3894 case GOT_STATUS_MODIFY:
3898 case GOT_STATUS_ADD:
3901 case GOT_STATUS_DELETE:
3905 return got_error(GOT_ERR_FILE_STATUS);
3907 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3908 label1, label2, a->diff_context, a->ignore_whitespace,
3912 if (staged_status == GOT_STATUS_ADD ||
3913 staged_status == GOT_STATUS_MODIFY) {
3915 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3919 err = got_object_id_str(&id_str, staged_blob_id);
3922 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3923 err = got_error_from_errno("asprintf");
3928 } else if (status != GOT_STATUS_ADD) {
3929 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3934 if (status != GOT_STATUS_DELETE) {
3935 if (asprintf(&abspath, "%s/%s",
3936 got_worktree_get_root_path(a->worktree), path) == -1) {
3937 err = got_error_from_errno("asprintf");
3942 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3944 if (errno != ELOOP) {
3945 err = got_error_from_errno2("openat",
3949 err = get_symlink_target_file(&fd, dirfd,
3955 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3957 if (errno != ELOOP) {
3958 err = got_error_from_errno2("open",
3962 err = get_symlink_target_file(&fd, dirfd,
3968 if (fstat(fd, &sb) == -1) {
3969 err = got_error_from_errno2("fstat", abspath);
3972 f2 = fdopen(fd, "r");
3974 err = got_error_from_errno2("fdopen", abspath);
3981 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3982 a->diff_context, a->ignore_whitespace, stdout);
3985 got_object_blob_close(blob1);
3986 if (f2 && fclose(f2) == EOF && err == NULL)
3987 err = got_error_from_errno("fclose");
3988 if (fd != -1 && close(fd) == -1 && err == NULL)
3989 err = got_error_from_errno("close");
3994 static const struct got_error *
3995 cmd_diff(int argc, char *argv[])
3997 const struct got_error *error;
3998 struct got_repository *repo = NULL;
3999 struct got_worktree *worktree = NULL;
4000 char *cwd = NULL, *repo_path = NULL;
4001 struct got_object_id *id1 = NULL, *id2 = NULL;
4002 const char *id_str1 = NULL, *id_str2 = NULL;
4003 char *label1 = NULL, *label2 = NULL;
4005 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
4010 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4015 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
4018 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
4021 err(1, "-C option %s", errstr);
4024 repo_path = realpath(optarg, NULL);
4025 if (repo_path == NULL)
4026 return got_error_from_errno2("realpath",
4028 got_path_strip_trailing_slashes(repo_path);
4034 ignore_whitespace = 1;
4045 cwd = getcwd(NULL, 0);
4047 error = got_error_from_errno("getcwd");
4053 "-r option can't be used when diffing a work tree");
4054 error = got_worktree_open(&worktree, cwd);
4056 if (error->code == GOT_ERR_NOT_WORKTREE)
4057 error = wrap_not_worktree_error(error, "diff",
4061 repo_path = strdup(got_worktree_get_repo_path(worktree));
4062 if (repo_path == NULL) {
4063 error = got_error_from_errno("strdup");
4067 error = got_worktree_resolve_path(&path, worktree,
4074 error = got_error_from_errno("strdup");
4078 } else if (argc == 2) {
4080 errx(1, "-s option can't be used when diffing "
4081 "objects in repository");
4084 if (repo_path == NULL) {
4085 error = got_worktree_open(&worktree, cwd);
4086 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4090 got_worktree_get_repo_path(worktree));
4091 if (repo_path == NULL) {
4092 error = got_error_from_errno("strdup");
4096 repo_path = strdup(cwd);
4097 if (repo_path == NULL) {
4098 error = got_error_from_errno("strdup");
4106 error = got_repo_open(&repo, repo_path, NULL);
4111 error = apply_unveil(got_repo_get_path(repo), 1,
4112 worktree ? got_worktree_get_root_path(worktree) : NULL);
4117 struct print_diff_arg arg;
4118 struct got_pathlist_head paths;
4123 error = got_object_id_str(&id_str,
4124 got_worktree_get_base_commit_id(worktree));
4128 arg.worktree = worktree;
4129 arg.diff_context = diff_context;
4130 arg.id_str = id_str;
4131 arg.header_shown = 0;
4132 arg.diff_staged = diff_staged;
4133 arg.ignore_whitespace = ignore_whitespace;
4135 error = got_pathlist_append(&paths, path, NULL);
4139 error = got_worktree_status(worktree, &paths, repo, print_diff,
4140 &arg, check_cancelled, NULL);
4142 got_pathlist_free(&paths);
4146 error = got_repo_match_object_id(&id1, &label1, id_str1,
4147 GOT_OBJ_TYPE_ANY, 1, repo);
4151 error = got_repo_match_object_id(&id2, &label2, id_str2,
4152 GOT_OBJ_TYPE_ANY, 1, repo);
4156 error = got_object_get_type(&type1, repo, id1);
4160 error = got_object_get_type(&type2, repo, id2);
4164 if (type1 != type2) {
4165 error = got_error(GOT_ERR_OBJ_TYPE);
4170 case GOT_OBJ_TYPE_BLOB:
4171 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4172 diff_context, ignore_whitespace, repo, stdout);
4174 case GOT_OBJ_TYPE_TREE:
4175 error = got_diff_objects_as_trees(id1, id2, "", "",
4176 diff_context, ignore_whitespace, repo, stdout);
4178 case GOT_OBJ_TYPE_COMMIT:
4179 printf("diff %s %s\n", label1, label2);
4180 error = got_diff_objects_as_commits(id1, id2, diff_context,
4181 ignore_whitespace, repo, stdout);
4184 error = got_error(GOT_ERR_OBJ_TYPE);
4193 got_worktree_close(worktree);
4195 const struct got_error *repo_error;
4196 repo_error = got_repo_close(repo);
4207 "usage: %s blame [-c commit] [-r repository-path] path\n",
4216 char datebuf[11]; /* YYYY-MM-DD + NUL */
4219 struct blame_cb_args {
4220 struct blame_line *lines;
4224 off_t *line_offsets;
4226 struct got_repository *repo;
4229 static const struct got_error *
4230 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4232 const struct got_error *err = NULL;
4233 struct blame_cb_args *a = arg;
4234 struct blame_line *bline;
4236 size_t linesize = 0;
4237 struct got_commit_object *commit = NULL;
4240 time_t committer_time;
4242 if (nlines != a->nlines ||
4243 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4244 return got_error(GOT_ERR_RANGE);
4246 if (sigint_received)
4247 return got_error(GOT_ERR_ITER_COMPLETED);
4250 return NULL; /* no change in this commit */
4252 /* Annotate this line. */
4253 bline = &a->lines[lineno - 1];
4254 if (bline->annotated)
4256 err = got_object_id_str(&bline->id_str, id);
4260 err = got_object_open_as_commit(&commit, a->repo, id);
4264 bline->committer = strdup(got_object_commit_get_committer(commit));
4265 if (bline->committer == NULL) {
4266 err = got_error_from_errno("strdup");
4270 committer_time = got_object_commit_get_committer_time(commit);
4271 if (localtime_r(&committer_time, &tm) == NULL)
4272 return got_error_from_errno("localtime_r");
4273 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4274 &tm) >= sizeof(bline->datebuf)) {
4275 err = got_error(GOT_ERR_NO_SPACE);
4278 bline->annotated = 1;
4280 /* Print lines annotated so far. */
4281 bline = &a->lines[a->lineno_cur - 1];
4282 if (!bline->annotated)
4285 offset = a->line_offsets[a->lineno_cur - 1];
4286 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4287 err = got_error_from_errno("fseeko");
4291 while (bline->annotated) {
4292 char *smallerthan, *at, *nl, *committer;
4295 if (getline(&line, &linesize, a->f) == -1) {
4297 err = got_error_from_errno("getline");
4301 committer = bline->committer;
4302 smallerthan = strchr(committer, '<');
4303 if (smallerthan && smallerthan[1] != '\0')
4304 committer = smallerthan + 1;
4305 at = strchr(committer, '@');
4308 len = strlen(committer);
4310 committer[8] = '\0';
4312 nl = strchr(line, '\n');
4315 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4316 bline->id_str, bline->datebuf, committer, line);
4319 bline = &a->lines[a->lineno_cur - 1];
4323 got_object_commit_close(commit);
4328 static const struct got_error *
4329 cmd_blame(int argc, char *argv[])
4331 const struct got_error *error;
4332 struct got_repository *repo = NULL;
4333 struct got_worktree *worktree = NULL;
4334 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4335 char *link_target = NULL;
4336 struct got_object_id *obj_id = NULL;
4337 struct got_object_id *commit_id = NULL;
4338 struct got_blob_object *blob = NULL;
4339 char *commit_id_str = NULL;
4340 struct blame_cb_args bca;
4341 int ch, obj_type, i;
4344 memset(&bca, 0, sizeof(bca));
4347 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4352 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4355 commit_id_str = optarg;
4358 repo_path = realpath(optarg, NULL);
4359 if (repo_path == NULL)
4360 return got_error_from_errno2("realpath",
4362 got_path_strip_trailing_slashes(repo_path);
4378 cwd = getcwd(NULL, 0);
4380 error = got_error_from_errno("getcwd");
4383 if (repo_path == NULL) {
4384 error = got_worktree_open(&worktree, cwd);
4385 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4391 strdup(got_worktree_get_repo_path(worktree));
4392 if (repo_path == NULL) {
4393 error = got_error_from_errno("strdup");
4398 repo_path = strdup(cwd);
4399 if (repo_path == NULL) {
4400 error = got_error_from_errno("strdup");
4406 error = got_repo_open(&repo, repo_path, NULL);
4410 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4415 const char *prefix = got_worktree_get_path_prefix(worktree);
4416 char *p, *worktree_subdir = cwd +
4417 strlen(got_worktree_get_root_path(worktree));
4418 if (asprintf(&p, "%s%s%s%s%s",
4419 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4420 worktree_subdir, worktree_subdir[0] ? "/" : "",
4422 error = got_error_from_errno("asprintf");
4425 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4428 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4433 if (commit_id_str == NULL) {
4434 struct got_reference *head_ref;
4435 error = got_ref_open(&head_ref, repo, worktree ?
4436 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4439 error = got_ref_resolve(&commit_id, repo, head_ref);
4440 got_ref_close(head_ref);
4444 error = got_repo_match_object_id(&commit_id, NULL,
4445 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4450 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4455 error = got_object_id_by_path(&obj_id, repo, commit_id,
4456 link_target ? link_target : in_repo_path);
4460 error = got_object_get_type(&obj_type, repo, obj_id);
4464 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4465 error = got_error_path(link_target ? link_target : in_repo_path,
4470 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4473 bca.f = got_opentemp();
4474 if (bca.f == NULL) {
4475 error = got_error_from_errno("got_opentemp");
4478 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4479 &bca.line_offsets, bca.f, blob);
4480 if (error || bca.nlines == 0)
4483 /* Don't include \n at EOF in the blame line count. */
4484 if (bca.line_offsets[bca.nlines - 1] == filesize)
4487 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4488 if (bca.lines == NULL) {
4489 error = got_error_from_errno("calloc");
4493 bca.nlines_prec = 0;
4501 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4502 repo, blame_cb, &bca, check_cancelled, NULL);
4511 got_object_blob_close(blob);
4513 got_worktree_close(worktree);
4515 const struct got_error *repo_error;
4516 repo_error = got_repo_close(repo);
4521 for (i = 0; i < bca.nlines; i++) {
4522 struct blame_line *bline = &bca.lines[i];
4523 free(bline->id_str);
4524 free(bline->committer);
4528 free(bca.line_offsets);
4529 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4530 error = got_error_from_errno("fclose");
4538 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4543 static const struct got_error *
4544 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4545 const char *root_path, struct got_repository *repo)
4547 const struct got_error *err = NULL;
4548 int is_root_path = (strcmp(path, root_path) == 0);
4549 const char *modestr = "";
4550 mode_t mode = got_tree_entry_get_mode(te);
4551 char *link_target = NULL;
4553 path += strlen(root_path);
4554 while (path[0] == '/')
4557 if (got_object_tree_entry_is_submodule(te))
4559 else if (S_ISLNK(mode)) {
4562 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4565 for (i = 0; i < strlen(link_target); i++) {
4566 if (!isprint((unsigned char)link_target[i]))
4567 link_target[i] = '?';
4572 else if (S_ISDIR(mode))
4574 else if (mode & S_IXUSR)
4577 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4578 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4579 link_target ? " -> ": "", link_target ? link_target : "");
4585 static const struct got_error *
4586 print_tree(const char *path, struct got_object_id *commit_id,
4587 int show_ids, int recurse, const char *root_path,
4588 struct got_repository *repo)
4590 const struct got_error *err = NULL;
4591 struct got_object_id *tree_id = NULL;
4592 struct got_tree_object *tree = NULL;
4595 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4599 err = got_object_open_as_tree(&tree, repo, tree_id);
4602 nentries = got_object_tree_get_nentries(tree);
4603 for (i = 0; i < nentries; i++) {
4604 struct got_tree_entry *te;
4607 if (sigint_received || sigpipe_received)
4610 te = got_object_tree_get_entry(tree, i);
4613 err = got_object_id_str(&id_str,
4614 got_tree_entry_get_id(te));
4617 if (asprintf(&id, "%s ", id_str) == -1) {
4618 err = got_error_from_errno("asprintf");
4624 err = print_entry(te, id, path, root_path, repo);
4629 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4631 if (asprintf(&child_path, "%s%s%s", path,
4632 path[0] == '/' && path[1] == '\0' ? "" : "/",
4633 got_tree_entry_get_name(te)) == -1) {
4634 err = got_error_from_errno("asprintf");
4637 err = print_tree(child_path, commit_id, show_ids, 1,
4646 got_object_tree_close(tree);
4651 static const struct got_error *
4652 cmd_tree(int argc, char *argv[])
4654 const struct got_error *error;
4655 struct got_repository *repo = NULL;
4656 struct got_worktree *worktree = NULL;
4657 const char *path, *refname = NULL;
4658 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4659 struct got_object_id *commit_id = NULL;
4660 char *commit_id_str = NULL;
4661 int show_ids = 0, recurse = 0;
4665 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4670 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4673 commit_id_str = optarg;
4676 repo_path = realpath(optarg, NULL);
4677 if (repo_path == NULL)
4678 return got_error_from_errno2("realpath",
4680 got_path_strip_trailing_slashes(repo_path);
4704 cwd = getcwd(NULL, 0);
4706 error = got_error_from_errno("getcwd");
4709 if (repo_path == NULL) {
4710 error = got_worktree_open(&worktree, cwd);
4711 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4717 strdup(got_worktree_get_repo_path(worktree));
4718 if (repo_path == NULL)
4719 error = got_error_from_errno("strdup");
4723 repo_path = strdup(cwd);
4724 if (repo_path == NULL) {
4725 error = got_error_from_errno("strdup");
4731 error = got_repo_open(&repo, repo_path, NULL);
4735 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4741 char *p, *worktree_subdir = cwd +
4742 strlen(got_worktree_get_root_path(worktree));
4743 if (asprintf(&p, "%s/%s",
4744 got_worktree_get_path_prefix(worktree),
4745 worktree_subdir) == -1) {
4746 error = got_error_from_errno("asprintf");
4749 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4756 if (in_repo_path == NULL) {
4757 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4762 if (commit_id_str == NULL) {
4763 struct got_reference *head_ref;
4765 refname = got_worktree_get_head_ref_name(worktree);
4767 refname = GOT_REF_HEAD;
4768 error = got_ref_open(&head_ref, repo, refname, 0);
4771 error = got_ref_resolve(&commit_id, repo, head_ref);
4772 got_ref_close(head_ref);
4776 error = got_repo_match_object_id(&commit_id, NULL,
4777 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4782 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4783 in_repo_path, repo);
4790 got_worktree_close(worktree);
4792 const struct got_error *repo_error;
4793 repo_error = got_repo_close(repo);
4803 fprintf(stderr, "usage: %s status [-s status-codes ] [path ...]\n",
4808 static const struct got_error *
4809 print_status(void *arg, unsigned char status, unsigned char staged_status,
4810 const char *path, struct got_object_id *blob_id,
4811 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4812 int dirfd, const char *de_name)
4814 if (status == staged_status && (status == GOT_STATUS_DELETE))
4815 status = GOT_STATUS_NO_CHANGE;
4817 char *status_codes = arg;
4818 size_t ncodes = strlen(status_codes);
4820 for (i = 0; i < ncodes ; i++) {
4821 if (status == status_codes[i] ||
4822 staged_status == status_codes[i])
4828 printf("%c%c %s\n", status, staged_status, path);
4832 static const struct got_error *
4833 cmd_status(int argc, char *argv[])
4835 const struct got_error *error = NULL;
4836 struct got_repository *repo = NULL;
4837 struct got_worktree *worktree = NULL;
4838 char *cwd = NULL, *status_codes = NULL;;
4839 struct got_pathlist_head paths;
4840 struct got_pathlist_entry *pe;
4845 while ((ch = getopt(argc, argv, "s:")) != -1) {
4848 for (i = 0; i < strlen(optarg); i++) {
4849 switch (optarg[i]) {
4850 case GOT_STATUS_MODIFY:
4851 case GOT_STATUS_ADD:
4852 case GOT_STATUS_DELETE:
4853 case GOT_STATUS_CONFLICT:
4854 case GOT_STATUS_MISSING:
4855 case GOT_STATUS_OBSTRUCTED:
4856 case GOT_STATUS_UNVERSIONED:
4857 case GOT_STATUS_MODE_CHANGE:
4858 case GOT_STATUS_NONEXISTENT:
4861 errx(1, "invalid status code '%c'",
4865 status_codes = optarg;
4877 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4881 cwd = getcwd(NULL, 0);
4883 error = got_error_from_errno("getcwd");
4887 error = got_worktree_open(&worktree, cwd);
4889 if (error->code == GOT_ERR_NOT_WORKTREE)
4890 error = wrap_not_worktree_error(error, "status", cwd);
4894 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4899 error = apply_unveil(got_repo_get_path(repo), 1,
4900 got_worktree_get_root_path(worktree));
4904 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4908 error = got_worktree_status(worktree, &paths, repo, print_status,
4909 status_codes, check_cancelled, NULL);
4911 TAILQ_FOREACH(pe, &paths, entry)
4912 free((char *)pe->path);
4913 got_pathlist_free(&paths);
4922 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4928 static const struct got_error *
4929 list_refs(struct got_repository *repo, const char *refname)
4931 static const struct got_error *err = NULL;
4932 struct got_reflist_head refs;
4933 struct got_reflist_entry *re;
4935 SIMPLEQ_INIT(&refs);
4936 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4940 SIMPLEQ_FOREACH(re, &refs, entry) {
4942 refstr = got_ref_to_str(re->ref);
4944 return got_error_from_errno("got_ref_to_str");
4945 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4949 got_ref_list_free(&refs);
4953 static const struct got_error *
4954 delete_ref(struct got_repository *repo, const char *refname)
4956 const struct got_error *err = NULL;
4957 struct got_reference *ref;
4959 err = got_ref_open(&ref, repo, refname, 0);
4963 err = got_ref_delete(ref, repo);
4968 static const struct got_error *
4969 add_ref(struct got_repository *repo, const char *refname, const char *target)
4971 const struct got_error *err = NULL;
4972 struct got_object_id *id;
4973 struct got_reference *ref = NULL;
4976 * Don't let the user create a reference name with a leading '-'.
4977 * While technically a valid reference name, this case is usually
4978 * an unintended typo.
4980 if (refname[0] == '-')
4981 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4983 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4986 struct got_reference *target_ref;
4988 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4990 err = got_ref_open(&target_ref, repo, target, 0);
4993 err = got_ref_resolve(&id, repo, target_ref);
4994 got_ref_close(target_ref);
4999 err = got_ref_alloc(&ref, refname, id);
5003 err = got_ref_write(ref, repo);
5011 static const struct got_error *
5012 add_symref(struct got_repository *repo, const char *refname, const char *target)
5014 const struct got_error *err = NULL;
5015 struct got_reference *ref = NULL;
5016 struct got_reference *target_ref = NULL;
5019 * Don't let the user create a reference name with a leading '-'.
5020 * While technically a valid reference name, this case is usually
5021 * an unintended typo.
5023 if (refname[0] == '-')
5024 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
5026 err = got_ref_open(&target_ref, repo, target, 0);
5030 err = got_ref_alloc_symref(&ref, refname, target_ref);
5034 err = got_ref_write(ref, repo);
5037 got_ref_close(target_ref);
5043 static const struct got_error *
5044 cmd_ref(int argc, char *argv[])
5046 const struct got_error *error = NULL;
5047 struct got_repository *repo = NULL;
5048 struct got_worktree *worktree = NULL;
5049 char *cwd = NULL, *repo_path = NULL;
5050 int ch, do_list = 0, do_delete = 0;
5051 const char *obj_arg = NULL, *symref_target= NULL;
5052 char *refname = NULL;
5054 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
5063 repo_path = realpath(optarg, NULL);
5064 if (repo_path == NULL)
5065 return got_error_from_errno2("realpath",
5067 got_path_strip_trailing_slashes(repo_path);
5073 symref_target = optarg;
5081 if (obj_arg && do_list)
5082 errx(1, "-c and -l options are mutually exclusive");
5083 if (obj_arg && do_delete)
5084 errx(1, "-c and -d options are mutually exclusive");
5085 if (obj_arg && symref_target)
5086 errx(1, "-c and -s options are mutually exclusive");
5087 if (symref_target && do_delete)
5088 errx(1, "-s and -d options are mutually exclusive");
5089 if (symref_target && do_list)
5090 errx(1, "-s and -l options are mutually exclusive");
5091 if (do_delete && do_list)
5092 errx(1, "-d and -l options are mutually exclusive");
5098 if (argc != 0 && argc != 1)
5101 refname = strdup(argv[0]);
5102 if (refname == NULL) {
5103 error = got_error_from_errno("strdup");
5110 refname = strdup(argv[0]);
5111 if (refname == NULL) {
5112 error = got_error_from_errno("strdup");
5118 got_path_strip_trailing_slashes(refname);
5122 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5126 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5127 "sendfd unveil", NULL) == -1)
5131 cwd = getcwd(NULL, 0);
5133 error = got_error_from_errno("getcwd");
5137 if (repo_path == NULL) {
5138 error = got_worktree_open(&worktree, cwd);
5139 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5145 strdup(got_worktree_get_repo_path(worktree));
5146 if (repo_path == NULL)
5147 error = got_error_from_errno("strdup");
5151 repo_path = strdup(cwd);
5152 if (repo_path == NULL) {
5153 error = got_error_from_errno("strdup");
5159 error = got_repo_open(&repo, repo_path, NULL);
5163 error = apply_unveil(got_repo_get_path(repo), do_list,
5164 worktree ? got_worktree_get_root_path(worktree) : NULL);
5169 error = list_refs(repo, refname);
5171 error = delete_ref(repo, refname);
5172 else if (symref_target)
5173 error = add_symref(repo, refname, symref_target);
5175 if (obj_arg == NULL)
5177 error = add_ref(repo, refname, obj_arg);
5182 got_repo_close(repo);
5184 got_worktree_close(worktree);
5194 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
5195 "[name]\n", getprogname());
5199 static const struct got_error *
5200 list_branch(struct got_repository *repo, struct got_worktree *worktree,
5201 struct got_reference *ref)
5203 const struct got_error *err = NULL;
5204 const char *refname, *marker = " ";
5207 refname = got_ref_get_name(ref);
5208 if (worktree && strcmp(refname,
5209 got_worktree_get_head_ref_name(worktree)) == 0) {
5210 struct got_object_id *id = NULL;
5212 err = got_ref_resolve(&id, repo, ref);
5215 if (got_object_id_cmp(id,
5216 got_worktree_get_base_commit_id(worktree)) == 0)
5223 if (strncmp(refname, "refs/heads/", 11) == 0)
5225 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5228 refstr = got_ref_to_str(ref);
5230 return got_error_from_errno("got_ref_to_str");
5232 printf("%s%s: %s\n", marker, refname, refstr);
5237 static const struct got_error *
5238 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5240 const char *refname;
5242 if (worktree == NULL)
5243 return got_error(GOT_ERR_NOT_WORKTREE);
5245 refname = got_worktree_get_head_ref_name(worktree);
5247 if (strncmp(refname, "refs/heads/", 11) == 0)
5249 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5252 printf("%s\n", refname);
5257 static const struct got_error *
5258 list_branches(struct got_repository *repo, struct got_worktree *worktree)
5260 static const struct got_error *err = NULL;
5261 struct got_reflist_head refs;
5262 struct got_reflist_entry *re;
5263 struct got_reference *temp_ref = NULL;
5264 int rebase_in_progress, histedit_in_progress;
5266 SIMPLEQ_INIT(&refs);
5269 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5274 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5279 if (rebase_in_progress || histedit_in_progress) {
5280 err = got_ref_open(&temp_ref, repo,
5281 got_worktree_get_head_ref_name(worktree), 0);
5284 list_branch(repo, worktree, temp_ref);
5285 got_ref_close(temp_ref);
5289 err = got_ref_list(&refs, repo, "refs/heads",
5290 got_ref_cmp_by_name, NULL);
5294 SIMPLEQ_FOREACH(re, &refs, entry)
5295 list_branch(repo, worktree, re->ref);
5297 got_ref_list_free(&refs);
5301 static const struct got_error *
5302 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5303 const char *branch_name)
5305 const struct got_error *err = NULL;
5306 struct got_reference *ref = NULL;
5309 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5310 return got_error_from_errno("asprintf");
5312 err = got_ref_open(&ref, repo, refname, 0);
5317 strcmp(got_worktree_get_head_ref_name(worktree),
5318 got_ref_get_name(ref)) == 0) {
5319 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5320 "will not delete this work tree's current branch");
5324 err = got_ref_delete(ref, repo);
5332 static const struct got_error *
5333 add_branch(struct got_repository *repo, const char *branch_name,
5334 struct got_object_id *base_commit_id)
5336 const struct got_error *err = NULL;
5337 struct got_reference *ref = NULL;
5338 char *base_refname = NULL, *refname = NULL;
5341 * Don't let the user create a branch name with a leading '-'.
5342 * While technically a valid reference name, this case is usually
5343 * an unintended typo.
5345 if (branch_name[0] == '-')
5346 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5348 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5349 err = got_error_from_errno("asprintf");
5353 err = got_ref_open(&ref, repo, refname, 0);
5355 err = got_error(GOT_ERR_BRANCH_EXISTS);
5357 } else if (err->code != GOT_ERR_NOT_REF)
5360 err = got_ref_alloc(&ref, refname, base_commit_id);
5364 err = got_ref_write(ref, repo);
5373 static const struct got_error *
5374 cmd_branch(int argc, char *argv[])
5376 const struct got_error *error = NULL;
5377 struct got_repository *repo = NULL;
5378 struct got_worktree *worktree = NULL;
5379 char *cwd = NULL, *repo_path = NULL;
5380 int ch, do_list = 0, do_show = 0, do_update = 1;
5381 const char *delref = NULL, *commit_id_arg = NULL;
5382 struct got_reference *ref = NULL;
5383 struct got_pathlist_head paths;
5384 struct got_pathlist_entry *pe;
5385 struct got_object_id *commit_id = NULL;
5386 char *commit_id_str = NULL;
5390 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5393 commit_id_arg = optarg;
5399 repo_path = realpath(optarg, NULL);
5400 if (repo_path == NULL)
5401 return got_error_from_errno2("realpath",
5403 got_path_strip_trailing_slashes(repo_path);
5417 if (do_list && delref)
5418 errx(1, "-l and -d options are mutually exclusive");
5423 if (!do_list && !delref && argc == 0)
5426 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5427 errx(1, "-c option can only be used when creating a branch");
5429 if (do_list || delref) {
5432 } else if (!do_show && argc != 1)
5436 if (do_list || do_show) {
5437 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5441 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5442 "sendfd unveil", NULL) == -1)
5446 cwd = getcwd(NULL, 0);
5448 error = got_error_from_errno("getcwd");
5452 if (repo_path == NULL) {
5453 error = got_worktree_open(&worktree, cwd);
5454 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5460 strdup(got_worktree_get_repo_path(worktree));
5461 if (repo_path == NULL)
5462 error = got_error_from_errno("strdup");
5466 repo_path = strdup(cwd);
5467 if (repo_path == NULL) {
5468 error = got_error_from_errno("strdup");
5474 error = got_repo_open(&repo, repo_path, NULL);
5478 error = apply_unveil(got_repo_get_path(repo), do_list,
5479 worktree ? got_worktree_get_root_path(worktree) : NULL);
5484 error = show_current_branch(repo, worktree);
5486 error = list_branches(repo, worktree);
5488 error = delete_branch(repo, worktree, delref);
5490 if (commit_id_arg == NULL)
5491 commit_id_arg = worktree ?
5492 got_worktree_get_head_ref_name(worktree) :
5494 error = got_repo_match_object_id(&commit_id, NULL,
5495 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5498 error = add_branch(repo, argv[0], commit_id);
5501 if (worktree && do_update) {
5502 struct got_update_progress_arg upa;
5503 char *branch_refname = NULL;
5505 error = got_object_id_str(&commit_id_str, commit_id);
5508 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5512 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5514 error = got_error_from_errno("asprintf");
5517 error = got_ref_open(&ref, repo, branch_refname, 0);
5518 free(branch_refname);
5521 error = switch_head_ref(ref, commit_id, worktree,
5525 error = got_worktree_set_base_commit_id(worktree, repo,
5529 memset(&upa, 0, sizeof(upa));
5530 error = got_worktree_checkout_files(worktree, &paths,
5531 repo, update_progress, &upa, check_cancelled,
5535 if (upa.did_something)
5536 printf("Updated to commit %s\n", commit_id_str);
5537 print_update_progress_stats(&upa);
5544 got_repo_close(repo);
5546 got_worktree_close(worktree);
5550 free(commit_id_str);
5551 TAILQ_FOREACH(pe, &paths, entry)
5552 free((char *)pe->path);
5553 got_pathlist_free(&paths);
5562 "usage: %s tag [-c commit] [-r repository] [-l] "
5563 "[-m message] name\n", getprogname());
5568 static const struct got_error *
5569 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5571 const struct got_error *err = NULL;
5572 struct got_reflist_entry *re, *se, *new;
5573 struct got_object_id *re_id, *se_id;
5574 struct got_tag_object *re_tag, *se_tag;
5575 time_t re_time, se_time;
5577 SIMPLEQ_FOREACH(re, tags, entry) {
5578 se = SIMPLEQ_FIRST(sorted);
5580 err = got_reflist_entry_dup(&new, re);
5583 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5586 err = got_ref_resolve(&re_id, repo, re->ref);
5589 err = got_object_open_as_tag(&re_tag, repo, re_id);
5593 re_time = got_object_tag_get_tagger_time(re_tag);
5594 got_object_tag_close(re_tag);
5598 err = got_ref_resolve(&se_id, repo, re->ref);
5601 err = got_object_open_as_tag(&se_tag, repo, se_id);
5605 se_time = got_object_tag_get_tagger_time(se_tag);
5606 got_object_tag_close(se_tag);
5608 if (se_time > re_time) {
5609 err = got_reflist_entry_dup(&new, re);
5612 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5615 se = SIMPLEQ_NEXT(se, entry);
5624 static const struct got_error *
5625 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5627 static const struct got_error *err = NULL;
5628 struct got_reflist_head refs;
5629 struct got_reflist_entry *re;
5631 SIMPLEQ_INIT(&refs);
5633 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5637 SIMPLEQ_FOREACH(re, &refs, entry) {
5638 const char *refname;
5639 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5643 struct got_object_id *id;
5644 struct got_tag_object *tag;
5645 struct got_commit_object *commit = NULL;
5647 refname = got_ref_get_name(re->ref);
5648 if (strncmp(refname, "refs/tags/", 10) != 0)
5651 refstr = got_ref_to_str(re->ref);
5652 if (refstr == NULL) {
5653 err = got_error_from_errno("got_ref_to_str");
5656 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5659 err = got_ref_resolve(&id, repo, re->ref);
5662 err = got_object_open_as_tag(&tag, repo, id);
5664 if (err->code != GOT_ERR_OBJ_TYPE) {
5668 /* "lightweight" tag */
5669 err = got_object_open_as_commit(&commit, repo, id);
5674 tagger = got_object_commit_get_committer(commit);
5676 got_object_commit_get_committer_time(commit);
5677 err = got_object_id_str(&id_str, id);
5683 tagger = got_object_tag_get_tagger(tag);
5684 tagger_time = got_object_tag_get_tagger_time(tag);
5685 err = got_object_id_str(&id_str,
5686 got_object_tag_get_object_id(tag));
5690 printf("from: %s\n", tagger);
5691 datestr = get_datestr(&tagger_time, datebuf);
5693 printf("date: %s UTC\n", datestr);
5695 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5697 switch (got_object_tag_get_object_type(tag)) {
5698 case GOT_OBJ_TYPE_BLOB:
5699 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5702 case GOT_OBJ_TYPE_TREE:
5703 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5706 case GOT_OBJ_TYPE_COMMIT:
5707 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5710 case GOT_OBJ_TYPE_TAG:
5711 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5720 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5723 got_object_commit_close(commit);
5725 tagmsg0 = strdup(got_object_tag_get_message(tag));
5726 got_object_tag_close(tag);
5727 if (tagmsg0 == NULL) {
5728 err = got_error_from_errno("strdup");
5735 line = strsep(&tagmsg, "\n");
5737 printf(" %s\n", line);
5742 got_ref_list_free(&refs);
5746 static const struct got_error *
5747 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5748 const char *tag_name, const char *repo_path)
5750 const struct got_error *err = NULL;
5751 char *template = NULL, *initial_content = NULL;
5752 char *editor = NULL;
5753 int initial_content_len;
5756 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5757 err = got_error_from_errno("asprintf");
5761 initial_content_len = asprintf(&initial_content,
5762 "\n# tagging commit %s as %s\n",
5763 commit_id_str, tag_name);
5764 if (initial_content_len == -1) {
5765 err = got_error_from_errno("asprintf");
5769 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5773 if (write(fd, initial_content, initial_content_len) == -1) {
5774 err = got_error_from_errno2("write", *tagmsg_path);
5778 err = get_editor(&editor);
5781 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5783 free(initial_content);
5787 if (fd != -1 && close(fd) == -1 && err == NULL)
5788 err = got_error_from_errno2("close", *tagmsg_path);
5790 /* Editor is done; we can now apply unveil(2) */
5792 err = apply_unveil(repo_path, 0, NULL);
5800 static const struct got_error *
5801 add_tag(struct got_repository *repo, struct got_worktree *worktree,
5802 const char *tag_name, const char *commit_arg, const char *tagmsg_arg)
5804 const struct got_error *err = NULL;
5805 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5806 char *label = NULL, *commit_id_str = NULL;
5807 struct got_reference *ref = NULL;
5808 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5809 char *tagmsg_path = NULL, *tag_id_str = NULL;
5810 int preserve_tagmsg = 0;
5813 * Don't let the user create a tag name with a leading '-'.
5814 * While technically a valid reference name, this case is usually
5815 * an unintended typo.
5817 if (tag_name[0] == '-')
5818 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5820 err = get_author(&tagger, repo, worktree);
5824 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5825 GOT_OBJ_TYPE_COMMIT, 1, repo);
5829 err = got_object_id_str(&commit_id_str, commit_id);
5833 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5834 refname = strdup(tag_name);
5835 if (refname == NULL) {
5836 err = got_error_from_errno("strdup");
5840 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5841 err = got_error_from_errno("asprintf");
5845 err = got_ref_open(&ref, repo, refname, 0);
5847 err = got_error(GOT_ERR_TAG_EXISTS);
5849 } else if (err->code != GOT_ERR_NOT_REF)
5852 if (tagmsg_arg == NULL) {
5853 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5854 tag_name, got_repo_get_path(repo));
5856 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5857 tagmsg_path != NULL)
5858 preserve_tagmsg = 1;
5863 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5864 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5867 preserve_tagmsg = 1;
5871 err = got_ref_alloc(&ref, refname, tag_id);
5874 preserve_tagmsg = 1;
5878 err = got_ref_write(ref, repo);
5881 preserve_tagmsg = 1;
5885 err = got_object_id_str(&tag_id_str, tag_id);
5888 preserve_tagmsg = 1;
5891 printf("Created tag %s\n", tag_id_str);
5893 if (preserve_tagmsg) {
5894 fprintf(stderr, "%s: tag message preserved in %s\n",
5895 getprogname(), tagmsg_path);
5896 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5897 err = got_error_from_errno2("unlink", tagmsg_path);
5902 free(commit_id_str);
5910 static const struct got_error *
5911 cmd_tag(int argc, char *argv[])
5913 const struct got_error *error = NULL;
5914 struct got_repository *repo = NULL;
5915 struct got_worktree *worktree = NULL;
5916 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5917 char *gitconfig_path = NULL;
5918 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5919 int ch, do_list = 0;
5921 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5924 commit_id_arg = optarg;
5930 repo_path = realpath(optarg, NULL);
5931 if (repo_path == NULL)
5932 return got_error_from_errno2("realpath",
5934 got_path_strip_trailing_slashes(repo_path);
5949 if (commit_id_arg != NULL)
5951 "-c option can only be used when creating a tag");
5953 errx(1, "-l and -m options are mutually exclusive");
5956 } else if (argc != 1)
5963 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5968 "sendfd unveil", NULL) == -1)
5972 cwd = getcwd(NULL, 0);
5974 error = got_error_from_errno("getcwd");
5978 if (repo_path == NULL) {
5979 error = got_worktree_open(&worktree, cwd);
5980 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5986 strdup(got_worktree_get_repo_path(worktree));
5987 if (repo_path == NULL)
5988 error = got_error_from_errno("strdup");
5992 repo_path = strdup(cwd);
5993 if (repo_path == NULL) {
5994 error = got_error_from_errno("strdup");
6001 error = got_repo_open(&repo, repo_path, NULL);
6004 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6007 error = list_tags(repo, worktree);
6009 error = get_gitconfig_path(&gitconfig_path);
6012 error = got_repo_open(&repo, repo_path, gitconfig_path);
6017 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
6022 if (commit_id_arg == NULL) {
6023 struct got_reference *head_ref;
6024 struct got_object_id *commit_id;
6025 error = got_ref_open(&head_ref, repo,
6026 worktree ? got_worktree_get_head_ref_name(worktree)
6030 error = got_ref_resolve(&commit_id, repo, head_ref);
6031 got_ref_close(head_ref);
6034 error = got_object_id_str(&commit_id_str, commit_id);
6040 error = add_tag(repo, worktree, tag_name,
6041 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
6045 got_repo_close(repo);
6047 got_worktree_close(worktree);
6050 free(gitconfig_path);
6051 free(commit_id_str);
6058 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
6063 static const struct got_error *
6064 add_progress(void *arg, unsigned char status, const char *path)
6066 while (path[0] == '/')
6068 printf("%c %s\n", status, path);
6072 static const struct got_error *
6073 cmd_add(int argc, char *argv[])
6075 const struct got_error *error = NULL;
6076 struct got_repository *repo = NULL;
6077 struct got_worktree *worktree = NULL;
6079 struct got_pathlist_head paths;
6080 struct got_pathlist_entry *pe;
6081 int ch, can_recurse = 0, no_ignores = 0;
6085 while ((ch = getopt(argc, argv, "IR")) != -1) {
6103 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6110 cwd = getcwd(NULL, 0);
6112 error = got_error_from_errno("getcwd");
6116 error = got_worktree_open(&worktree, cwd);
6118 if (error->code == GOT_ERR_NOT_WORKTREE)
6119 error = wrap_not_worktree_error(error, "add", cwd);
6123 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6128 error = apply_unveil(got_repo_get_path(repo), 1,
6129 got_worktree_get_root_path(worktree));
6133 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6137 if (!can_recurse && no_ignores) {
6138 error = got_error_msg(GOT_ERR_BAD_PATH,
6139 "disregarding ignores requires -R option");
6147 TAILQ_FOREACH(pe, &paths, entry) {
6148 if (asprintf(&ondisk_path, "%s/%s",
6149 got_worktree_get_root_path(worktree),
6151 error = got_error_from_errno("asprintf");
6154 if (lstat(ondisk_path, &sb) == -1) {
6155 if (errno == ENOENT) {
6159 error = got_error_from_errno2("lstat",
6165 if (S_ISDIR(sb.st_mode)) {
6166 error = got_error_msg(GOT_ERR_BAD_PATH,
6167 "adding directories requires -R option");
6173 error = got_worktree_schedule_add(worktree, &paths, add_progress,
6174 NULL, repo, no_ignores);
6177 got_repo_close(repo);
6179 got_worktree_close(worktree);
6180 TAILQ_FOREACH(pe, &paths, entry)
6181 free((char *)pe->path);
6182 got_pathlist_free(&paths);
6190 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] [-s status-codes] "
6191 "path ...\n", getprogname());
6195 static const struct got_error *
6196 print_remove_status(void *arg, unsigned char status,
6197 unsigned char staged_status, const char *path)
6199 while (path[0] == '/')
6201 if (status == GOT_STATUS_NONEXISTENT)
6203 if (status == staged_status && (status == GOT_STATUS_DELETE))
6204 status = GOT_STATUS_NO_CHANGE;
6205 printf("%c%c %s\n", status, staged_status, path);
6209 static const struct got_error *
6210 cmd_remove(int argc, char *argv[])
6212 const struct got_error *error = NULL;
6213 struct got_worktree *worktree = NULL;
6214 struct got_repository *repo = NULL;
6215 const char *status_codes = NULL;
6217 struct got_pathlist_head paths;
6218 struct got_pathlist_entry *pe;
6219 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0, i;
6223 while ((ch = getopt(argc, argv, "fkRs:")) != -1) {
6226 delete_local_mods = 1;
6235 for (i = 0; i < strlen(optarg); i++) {
6236 switch (optarg[i]) {
6237 case GOT_STATUS_MODIFY:
6238 delete_local_mods = 1;
6240 case GOT_STATUS_MISSING:
6243 errx(1, "invalid status code '%c'",
6247 status_codes = optarg;
6259 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6266 cwd = getcwd(NULL, 0);
6268 error = got_error_from_errno("getcwd");
6271 error = got_worktree_open(&worktree, cwd);
6273 if (error->code == GOT_ERR_NOT_WORKTREE)
6274 error = wrap_not_worktree_error(error, "remove", cwd);
6278 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6283 error = apply_unveil(got_repo_get_path(repo), 1,
6284 got_worktree_get_root_path(worktree));
6288 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6295 TAILQ_FOREACH(pe, &paths, entry) {
6296 if (asprintf(&ondisk_path, "%s/%s",
6297 got_worktree_get_root_path(worktree),
6299 error = got_error_from_errno("asprintf");
6302 if (lstat(ondisk_path, &sb) == -1) {
6303 if (errno == ENOENT) {
6307 error = got_error_from_errno2("lstat",
6313 if (S_ISDIR(sb.st_mode)) {
6314 error = got_error_msg(GOT_ERR_BAD_PATH,
6315 "removing directories requires -R option");
6321 error = got_worktree_schedule_delete(worktree, &paths,
6322 delete_local_mods, status_codes, print_remove_status, NULL,
6323 repo, keep_on_disk);
6326 got_repo_close(repo);
6328 got_worktree_close(worktree);
6329 TAILQ_FOREACH(pe, &paths, entry)
6330 free((char *)pe->path);
6331 got_pathlist_free(&paths);
6339 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6340 "path ...\n", getprogname());
6344 static const struct got_error *
6345 revert_progress(void *arg, unsigned char status, const char *path)
6347 if (status == GOT_STATUS_UNVERSIONED)
6350 while (path[0] == '/')
6352 printf("%c %s\n", status, path);
6356 struct choose_patch_arg {
6357 FILE *patch_script_file;
6361 static const struct got_error *
6362 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6363 int nchanges, const char *action)
6366 size_t linesize = 0;
6370 case GOT_STATUS_ADD:
6371 printf("A %s\n%s this addition? [y/n] ", path, action);
6373 case GOT_STATUS_DELETE:
6374 printf("D %s\n%s this deletion? [y/n] ", path, action);
6376 case GOT_STATUS_MODIFY:
6377 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6378 return got_error_from_errno("fseek");
6379 printf(GOT_COMMIT_SEP_STR);
6380 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6382 if (ferror(patch_file))
6383 return got_error_from_errno("getline");
6384 printf(GOT_COMMIT_SEP_STR);
6385 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6386 path, n, nchanges, action);
6389 return got_error_path(path, GOT_ERR_FILE_STATUS);
6395 static const struct got_error *
6396 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6397 FILE *patch_file, int n, int nchanges)
6399 const struct got_error *err = NULL;
6401 size_t linesize = 0;
6404 struct choose_patch_arg *a = arg;
6406 *choice = GOT_PATCH_CHOICE_NONE;
6408 if (a->patch_script_file) {
6410 err = show_change(status, path, patch_file, n, nchanges,
6414 linelen = getline(&line, &linesize, a->patch_script_file);
6415 if (linelen == -1) {
6416 if (ferror(a->patch_script_file))
6417 return got_error_from_errno("getline");
6420 nl = strchr(line, '\n');
6423 if (strcmp(line, "y") == 0) {
6424 *choice = GOT_PATCH_CHOICE_YES;
6426 } else if (strcmp(line, "n") == 0) {
6427 *choice = GOT_PATCH_CHOICE_NO;
6429 } else if (strcmp(line, "q") == 0 &&
6430 status == GOT_STATUS_MODIFY) {
6431 *choice = GOT_PATCH_CHOICE_QUIT;
6434 printf("invalid response '%s'\n", line);
6439 while (resp != 'y' && resp != 'n' && resp != 'q') {
6440 err = show_change(status, path, patch_file, n, nchanges,
6447 if (status == GOT_STATUS_MODIFY) {
6448 if (resp != 'y' && resp != 'n' && resp != 'q') {
6449 printf("invalid response '%c'\n", resp);
6452 } else if (resp != 'y' && resp != 'n') {
6453 printf("invalid response '%c'\n", resp);
6459 *choice = GOT_PATCH_CHOICE_YES;
6460 else if (resp == 'n')
6461 *choice = GOT_PATCH_CHOICE_NO;
6462 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6463 *choice = GOT_PATCH_CHOICE_QUIT;
6469 static const struct got_error *
6470 cmd_revert(int argc, char *argv[])
6472 const struct got_error *error = NULL;
6473 struct got_worktree *worktree = NULL;
6474 struct got_repository *repo = NULL;
6475 char *cwd = NULL, *path = NULL;
6476 struct got_pathlist_head paths;
6477 struct got_pathlist_entry *pe;
6478 int ch, can_recurse = 0, pflag = 0;
6479 FILE *patch_script_file = NULL;
6480 const char *patch_script_path = NULL;
6481 struct choose_patch_arg cpa;
6485 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6491 patch_script_path = optarg;
6506 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6507 "unveil", NULL) == -1)
6512 if (patch_script_path && !pflag)
6513 errx(1, "-F option can only be used together with -p option");
6515 cwd = getcwd(NULL, 0);
6517 error = got_error_from_errno("getcwd");
6520 error = got_worktree_open(&worktree, cwd);
6522 if (error->code == GOT_ERR_NOT_WORKTREE)
6523 error = wrap_not_worktree_error(error, "revert", cwd);
6527 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6532 if (patch_script_path) {
6533 patch_script_file = fopen(patch_script_path, "r");
6534 if (patch_script_file == NULL) {
6535 error = got_error_from_errno2("fopen",
6540 error = apply_unveil(got_repo_get_path(repo), 1,
6541 got_worktree_get_root_path(worktree));
6545 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6552 TAILQ_FOREACH(pe, &paths, entry) {
6553 if (asprintf(&ondisk_path, "%s/%s",
6554 got_worktree_get_root_path(worktree),
6556 error = got_error_from_errno("asprintf");
6559 if (lstat(ondisk_path, &sb) == -1) {
6560 if (errno == ENOENT) {
6564 error = got_error_from_errno2("lstat",
6570 if (S_ISDIR(sb.st_mode)) {
6571 error = got_error_msg(GOT_ERR_BAD_PATH,
6572 "reverting directories requires -R option");
6578 cpa.patch_script_file = patch_script_file;
6579 cpa.action = "revert";
6580 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6581 pflag ? choose_patch : NULL, &cpa, repo);
6583 if (patch_script_file && fclose(patch_script_file) == EOF &&
6585 error = got_error_from_errno2("fclose", patch_script_path);
6587 got_repo_close(repo);
6589 got_worktree_close(worktree);
6598 fprintf(stderr, "usage: %s commit [-m msg] [-S] [path ...]\n",
6603 struct collect_commit_logmsg_arg {
6604 const char *cmdline_log;
6606 const char *worktree_path;
6607 const char *branch_name;
6608 const char *repo_path;
6613 static const struct got_error *
6614 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6617 char *initial_content = NULL;
6618 struct got_pathlist_entry *pe;
6619 const struct got_error *err = NULL;
6620 char *template = NULL;
6621 struct collect_commit_logmsg_arg *a = arg;
6622 int initial_content_len;
6626 /* if a message was specified on the command line, just use it */
6627 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6628 len = strlen(a->cmdline_log) + 1;
6629 *logmsg = malloc(len + 1);
6630 if (*logmsg == NULL)
6631 return got_error_from_errno("malloc");
6632 strlcpy(*logmsg, a->cmdline_log, len);
6636 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6637 return got_error_from_errno("asprintf");
6639 initial_content_len = asprintf(&initial_content,
6640 "\n# changes to be committed on branch %s:\n",
6642 if (initial_content_len == -1)
6643 return got_error_from_errno("asprintf");
6645 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6649 if (write(fd, initial_content, initial_content_len) == -1) {
6650 err = got_error_from_errno2("write", a->logmsg_path);
6654 TAILQ_FOREACH(pe, commitable_paths, entry) {
6655 struct got_commitable *ct = pe->data;
6656 dprintf(fd, "# %c %s\n",
6657 got_commitable_get_status(ct),
6658 got_commitable_get_path(ct));
6661 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6663 free(initial_content);
6666 if (fd != -1 && close(fd) == -1 && err == NULL)
6667 err = got_error_from_errno2("close", a->logmsg_path);
6669 /* Editor is done; we can now apply unveil(2) */
6671 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6679 static const struct got_error *
6680 cmd_commit(int argc, char *argv[])
6682 const struct got_error *error = NULL;
6683 struct got_worktree *worktree = NULL;
6684 struct got_repository *repo = NULL;
6685 char *cwd = NULL, *id_str = NULL;
6686 struct got_object_id *id = NULL;
6687 const char *logmsg = NULL;
6688 struct collect_commit_logmsg_arg cl_arg;
6689 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6690 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6691 int allow_bad_symlinks = 0;
6692 struct got_pathlist_head paths;
6695 cl_arg.logmsg_path = NULL;
6697 while ((ch = getopt(argc, argv, "m:S")) != -1) {
6703 allow_bad_symlinks = 1;
6715 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6716 "unveil", NULL) == -1)
6719 cwd = getcwd(NULL, 0);
6721 error = got_error_from_errno("getcwd");
6724 error = got_worktree_open(&worktree, cwd);
6726 if (error->code == GOT_ERR_NOT_WORKTREE)
6727 error = wrap_not_worktree_error(error, "commit", cwd);
6731 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6734 if (rebase_in_progress) {
6735 error = got_error(GOT_ERR_REBASING);
6739 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6744 error = get_gitconfig_path(&gitconfig_path);
6747 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6752 error = get_author(&author, repo, worktree);
6757 * unveil(2) traverses exec(2); if an editor is used we have
6758 * to apply unveil after the log message has been written.
6760 if (logmsg == NULL || strlen(logmsg) == 0)
6761 error = get_editor(&editor);
6763 error = apply_unveil(got_repo_get_path(repo), 0,
6764 got_worktree_get_root_path(worktree));
6768 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6772 cl_arg.editor = editor;
6773 cl_arg.cmdline_log = logmsg;
6774 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6775 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6776 if (!histedit_in_progress) {
6777 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6778 error = got_error(GOT_ERR_COMMIT_BRANCH);
6781 cl_arg.branch_name += 11;
6783 cl_arg.repo_path = got_repo_get_path(repo);
6784 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6785 allow_bad_symlinks, collect_commit_logmsg, &cl_arg,
6786 print_status, NULL, repo);
6788 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6789 cl_arg.logmsg_path != NULL)
6790 preserve_logmsg = 1;
6794 error = got_object_id_str(&id_str, id);
6797 printf("Created commit %s\n", id_str);
6799 if (preserve_logmsg) {
6800 fprintf(stderr, "%s: log message preserved in %s\n",
6801 getprogname(), cl_arg.logmsg_path);
6802 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6804 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6805 free(cl_arg.logmsg_path);
6807 got_repo_close(repo);
6809 got_worktree_close(worktree);
6812 free(gitconfig_path);
6819 usage_cherrypick(void)
6821 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6825 static const struct got_error *
6826 cmd_cherrypick(int argc, char *argv[])
6828 const struct got_error *error = NULL;
6829 struct got_worktree *worktree = NULL;
6830 struct got_repository *repo = NULL;
6831 char *cwd = NULL, *commit_id_str = NULL;
6832 struct got_object_id *commit_id = NULL;
6833 struct got_commit_object *commit = NULL;
6834 struct got_object_qid *pid;
6835 struct got_reference *head_ref = NULL;
6837 struct got_update_progress_arg upa;
6839 while ((ch = getopt(argc, argv, "")) != -1) {
6851 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6852 "unveil", NULL) == -1)
6858 cwd = getcwd(NULL, 0);
6860 error = got_error_from_errno("getcwd");
6863 error = got_worktree_open(&worktree, cwd);
6865 if (error->code == GOT_ERR_NOT_WORKTREE)
6866 error = wrap_not_worktree_error(error, "cherrypick",
6871 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6876 error = apply_unveil(got_repo_get_path(repo), 0,
6877 got_worktree_get_root_path(worktree));
6881 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6882 GOT_OBJ_TYPE_COMMIT, repo);
6883 if (error != NULL) {
6884 struct got_reference *ref;
6885 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6887 error = got_ref_open(&ref, repo, argv[0], 0);
6890 error = got_ref_resolve(&commit_id, repo, ref);
6895 error = got_object_id_str(&commit_id_str, commit_id);
6899 error = got_ref_open(&head_ref, repo,
6900 got_worktree_get_head_ref_name(worktree), 0);
6904 error = check_same_branch(commit_id, head_ref, NULL, repo);
6906 if (error->code != GOT_ERR_ANCESTRY)
6910 error = got_error(GOT_ERR_SAME_BRANCH);
6914 error = got_object_open_as_commit(&commit, repo, commit_id);
6917 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6918 memset(&upa, 0, sizeof(upa));
6919 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6920 commit_id, repo, update_progress, &upa, check_cancelled,
6925 if (upa.did_something)
6926 printf("Merged 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 backout commit-id\n", getprogname());
6948 static const struct got_error *
6949 cmd_backout(int argc, char *argv[])
6951 const struct got_error *error = NULL;
6952 struct got_worktree *worktree = NULL;
6953 struct got_repository *repo = NULL;
6954 char *cwd = NULL, *commit_id_str = NULL;
6955 struct got_object_id *commit_id = NULL;
6956 struct got_commit_object *commit = NULL;
6957 struct got_object_qid *pid;
6958 struct got_reference *head_ref = NULL;
6960 struct got_update_progress_arg upa;
6962 while ((ch = getopt(argc, argv, "")) != -1) {
6974 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6975 "unveil", NULL) == -1)
6981 cwd = getcwd(NULL, 0);
6983 error = got_error_from_errno("getcwd");
6986 error = got_worktree_open(&worktree, cwd);
6988 if (error->code == GOT_ERR_NOT_WORKTREE)
6989 error = wrap_not_worktree_error(error, "backout", cwd);
6993 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6998 error = apply_unveil(got_repo_get_path(repo), 0,
6999 got_worktree_get_root_path(worktree));
7003 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
7004 GOT_OBJ_TYPE_COMMIT, repo);
7005 if (error != NULL) {
7006 struct got_reference *ref;
7007 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
7009 error = got_ref_open(&ref, repo, argv[0], 0);
7012 error = got_ref_resolve(&commit_id, repo, ref);
7017 error = got_object_id_str(&commit_id_str, commit_id);
7021 error = got_ref_open(&head_ref, repo,
7022 got_worktree_get_head_ref_name(worktree), 0);
7026 error = check_same_branch(commit_id, head_ref, NULL, repo);
7030 error = got_object_open_as_commit(&commit, repo, commit_id);
7033 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
7035 error = got_error(GOT_ERR_ROOT_COMMIT);
7039 memset(&upa, 0, sizeof(upa));
7040 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
7041 update_progress, &upa, check_cancelled, NULL);
7045 if (upa.did_something)
7046 printf("Backed out commit %s\n", commit_id_str);
7047 print_update_progress_stats(&upa);
7050 got_object_commit_close(commit);
7051 free(commit_id_str);
7053 got_ref_close(head_ref);
7055 got_worktree_close(worktree);
7057 got_repo_close(repo);
7064 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
7070 trim_logmsg(char *logmsg, int limit)
7075 len = strlen(logmsg);
7079 nl = strchr(logmsg, '\n');
7084 static const struct got_error *
7085 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
7087 const struct got_error *err;
7088 char *logmsg0 = NULL;
7091 err = got_object_commit_get_logmsg(&logmsg0, commit);
7096 while (isspace((unsigned char)s[0]))
7099 *logmsg = strdup(s);
7100 if (*logmsg == NULL) {
7101 err = got_error_from_errno("strdup");
7105 trim_logmsg(*logmsg, limit);
7111 static const struct got_error *
7112 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
7114 const struct got_error *err;
7115 struct got_commit_object *commit = NULL;
7116 char *id_str = NULL, *logmsg = NULL;
7118 err = got_object_open_as_commit(&commit, repo, id);
7122 err = got_object_id_str(&id_str, id);
7128 err = get_short_logmsg(&logmsg, 42, commit);
7132 printf("%s -> merge conflict: %s\n", id_str, logmsg);
7135 got_object_commit_close(commit);
7140 static const struct got_error *
7141 show_rebase_progress(struct got_commit_object *commit,
7142 struct got_object_id *old_id, struct got_object_id *new_id)
7144 const struct got_error *err;
7145 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7147 err = got_object_id_str(&old_id_str, old_id);
7152 err = got_object_id_str(&new_id_str, new_id);
7157 old_id_str[12] = '\0';
7159 new_id_str[12] = '\0';
7161 err = get_short_logmsg(&logmsg, 42, commit);
7165 printf("%s -> %s: %s\n", old_id_str,
7166 new_id_str ? new_id_str : "no-op change", logmsg);
7174 static const struct got_error *
7175 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
7176 struct got_reference *branch, struct got_reference *new_base_branch,
7177 struct got_reference *tmp_branch, struct got_repository *repo)
7179 printf("Switching work tree to %s\n", got_ref_get_name(branch));
7180 return got_worktree_rebase_complete(worktree, fileindex,
7181 new_base_branch, tmp_branch, branch, repo);
7184 static const struct got_error *
7185 rebase_commit(struct got_pathlist_head *merged_paths,
7186 struct got_worktree *worktree, struct got_fileindex *fileindex,
7187 struct got_reference *tmp_branch,
7188 struct got_object_id *commit_id, struct got_repository *repo)
7190 const struct got_error *error;
7191 struct got_commit_object *commit;
7192 struct got_object_id *new_commit_id;
7194 error = got_object_open_as_commit(&commit, repo, commit_id);
7198 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
7199 worktree, fileindex, tmp_branch, commit, commit_id, repo);
7201 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
7203 error = show_rebase_progress(commit, commit_id, NULL);
7205 error = show_rebase_progress(commit, commit_id, new_commit_id);
7206 free(new_commit_id);
7209 got_object_commit_close(commit);
7213 struct check_path_prefix_arg {
7214 const char *path_prefix;
7219 static const struct got_error *
7220 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
7221 struct got_blob_object *blob2, struct got_object_id *id1,
7222 struct got_object_id *id2, const char *path1, const char *path2,
7223 mode_t mode1, mode_t mode2, struct got_repository *repo)
7225 struct check_path_prefix_arg *a = arg;
7227 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
7228 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
7229 return got_error(a->errcode);
7234 static const struct got_error *
7235 check_path_prefix(struct got_object_id *parent_id,
7236 struct got_object_id *commit_id, const char *path_prefix,
7237 int errcode, struct got_repository *repo)
7239 const struct got_error *err;
7240 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7241 struct got_commit_object *commit = NULL, *parent_commit = NULL;
7242 struct check_path_prefix_arg cpp_arg;
7244 if (got_path_is_root_dir(path_prefix))
7247 err = got_object_open_as_commit(&commit, repo, commit_id);
7251 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7255 err = got_object_open_as_tree(&tree1, repo,
7256 got_object_commit_get_tree_id(parent_commit));
7260 err = got_object_open_as_tree(&tree2, repo,
7261 got_object_commit_get_tree_id(commit));
7265 cpp_arg.path_prefix = path_prefix;
7266 while (cpp_arg.path_prefix[0] == '/')
7267 cpp_arg.path_prefix++;
7268 cpp_arg.len = strlen(cpp_arg.path_prefix);
7269 cpp_arg.errcode = errcode;
7270 err = got_diff_tree(tree1, tree2, "", "", repo,
7271 check_path_prefix_in_diff, &cpp_arg, 0);
7274 got_object_tree_close(tree1);
7276 got_object_tree_close(tree2);
7278 got_object_commit_close(commit);
7280 got_object_commit_close(parent_commit);
7284 static const struct got_error *
7285 collect_commits(struct got_object_id_queue *commits,
7286 struct got_object_id *initial_commit_id,
7287 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7288 const char *path_prefix, int path_prefix_errcode,
7289 struct got_repository *repo)
7291 const struct got_error *err = NULL;
7292 struct got_commit_graph *graph = NULL;
7293 struct got_object_id *parent_id = NULL;
7294 struct got_object_qid *qid;
7295 struct got_object_id *commit_id = initial_commit_id;
7297 err = got_commit_graph_open(&graph, "/", 1);
7301 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7302 check_cancelled, NULL);
7305 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7306 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7307 check_cancelled, NULL);
7309 if (err->code == GOT_ERR_ITER_COMPLETED) {
7310 err = got_error_msg(GOT_ERR_ANCESTRY,
7311 "ran out of commits to rebase before "
7312 "youngest common ancestor commit has "
7317 err = check_path_prefix(parent_id, commit_id,
7318 path_prefix, path_prefix_errcode, repo);
7322 err = got_object_qid_alloc(&qid, commit_id);
7325 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7326 commit_id = parent_id;
7330 got_commit_graph_close(graph);
7334 static const struct got_error *
7335 cmd_rebase(int argc, char *argv[])
7337 const struct got_error *error = NULL;
7338 struct got_worktree *worktree = NULL;
7339 struct got_repository *repo = NULL;
7340 struct got_fileindex *fileindex = NULL;
7342 struct got_reference *branch = NULL;
7343 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7344 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7345 struct got_object_id *resume_commit_id = NULL;
7346 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7347 struct got_commit_object *commit = NULL;
7348 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7349 int histedit_in_progress = 0;
7350 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7351 struct got_object_id_queue commits;
7352 struct got_pathlist_head merged_paths;
7353 const struct got_object_id_queue *parent_ids;
7354 struct got_object_qid *qid, *pid;
7356 SIMPLEQ_INIT(&commits);
7357 TAILQ_INIT(&merged_paths);
7359 while ((ch = getopt(argc, argv, "ac")) != -1) {
7365 continue_rebase = 1;
7377 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7378 "unveil", NULL) == -1)
7381 if (abort_rebase && continue_rebase)
7383 else if (abort_rebase || continue_rebase) {
7386 } else if (argc != 1)
7389 cwd = getcwd(NULL, 0);
7391 error = got_error_from_errno("getcwd");
7394 error = got_worktree_open(&worktree, cwd);
7396 if (error->code == GOT_ERR_NOT_WORKTREE)
7397 error = wrap_not_worktree_error(error, "rebase", cwd);
7401 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7406 error = apply_unveil(got_repo_get_path(repo), 0,
7407 got_worktree_get_root_path(worktree));
7411 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7415 if (histedit_in_progress) {
7416 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7420 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7425 struct got_update_progress_arg upa;
7426 if (!rebase_in_progress) {
7427 error = got_error(GOT_ERR_NOT_REBASING);
7430 error = got_worktree_rebase_continue(&resume_commit_id,
7431 &new_base_branch, &tmp_branch, &branch, &fileindex,
7435 printf("Switching work tree to %s\n",
7436 got_ref_get_symref_target(new_base_branch));
7437 memset(&upa, 0, sizeof(upa));
7438 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7439 new_base_branch, update_progress, &upa);
7442 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7443 print_update_progress_stats(&upa);
7444 goto done; /* nothing else to do */
7447 if (continue_rebase) {
7448 if (!rebase_in_progress) {
7449 error = got_error(GOT_ERR_NOT_REBASING);
7452 error = got_worktree_rebase_continue(&resume_commit_id,
7453 &new_base_branch, &tmp_branch, &branch, &fileindex,
7458 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7459 resume_commit_id, repo);
7463 yca_id = got_object_id_dup(resume_commit_id);
7464 if (yca_id == NULL) {
7465 error = got_error_from_errno("got_object_id_dup");
7469 error = got_ref_open(&branch, repo, argv[0], 0);
7474 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7478 if (!continue_rebase) {
7479 struct got_object_id *base_commit_id;
7481 base_commit_id = got_worktree_get_base_commit_id(worktree);
7482 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7483 base_commit_id, branch_head_commit_id, repo,
7484 check_cancelled, NULL);
7487 if (yca_id == NULL) {
7488 error = got_error_msg(GOT_ERR_ANCESTRY,
7489 "specified branch shares no common ancestry "
7490 "with work tree's branch");
7494 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7496 if (error->code != GOT_ERR_ANCESTRY)
7500 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7501 "specified branch resolves to a commit which "
7502 "is already contained in work tree's branch");
7505 error = got_worktree_rebase_prepare(&new_base_branch,
7506 &tmp_branch, &fileindex, worktree, branch, repo);
7511 commit_id = branch_head_commit_id;
7512 error = got_object_open_as_commit(&commit, repo, commit_id);
7516 parent_ids = got_object_commit_get_parent_ids(commit);
7517 pid = SIMPLEQ_FIRST(parent_ids);
7519 if (!continue_rebase) {
7520 struct got_update_progress_arg upa;
7521 memset(&upa, 0, sizeof(upa));
7522 error = got_worktree_rebase_abort(worktree, fileindex,
7523 repo, new_base_branch, update_progress, &upa);
7526 printf("Rebase of %s aborted\n",
7527 got_ref_get_name(branch));
7528 print_update_progress_stats(&upa);
7531 error = got_error(GOT_ERR_EMPTY_REBASE);
7534 error = collect_commits(&commits, commit_id, pid->id,
7535 yca_id, got_worktree_get_path_prefix(worktree),
7536 GOT_ERR_REBASE_PATH, repo);
7537 got_object_commit_close(commit);
7542 if (SIMPLEQ_EMPTY(&commits)) {
7543 if (continue_rebase) {
7544 error = rebase_complete(worktree, fileindex,
7545 branch, new_base_branch, tmp_branch, repo);
7548 /* Fast-forward the reference of the branch. */
7549 struct got_object_id *new_head_commit_id;
7551 error = got_ref_resolve(&new_head_commit_id, repo,
7555 error = got_object_id_str(&id_str, new_head_commit_id);
7556 printf("Forwarding %s to commit %s\n",
7557 got_ref_get_name(branch), id_str);
7559 error = got_ref_change_ref(branch,
7560 new_head_commit_id);
7567 SIMPLEQ_FOREACH(qid, &commits, entry) {
7568 struct got_update_progress_arg upa;
7570 commit_id = qid->id;
7571 parent_id = pid ? pid->id : yca_id;
7574 memset(&upa, 0, sizeof(upa));
7575 error = got_worktree_rebase_merge_files(&merged_paths,
7576 worktree, fileindex, parent_id, commit_id, repo,
7577 update_progress, &upa, check_cancelled, NULL);
7581 print_update_progress_stats(&upa);
7582 if (upa.conflicts > 0)
7583 rebase_status = GOT_STATUS_CONFLICT;
7585 if (rebase_status == GOT_STATUS_CONFLICT) {
7586 error = show_rebase_merge_conflict(qid->id, repo);
7589 got_worktree_rebase_pathlist_free(&merged_paths);
7593 error = rebase_commit(&merged_paths, worktree, fileindex,
7594 tmp_branch, commit_id, repo);
7595 got_worktree_rebase_pathlist_free(&merged_paths);
7600 if (rebase_status == GOT_STATUS_CONFLICT) {
7601 error = got_worktree_rebase_postpone(worktree, fileindex);
7604 error = got_error_msg(GOT_ERR_CONFLICTS,
7605 "conflicts must be resolved before rebasing can continue");
7607 error = rebase_complete(worktree, fileindex, branch,
7608 new_base_branch, tmp_branch, repo);
7610 got_object_id_queue_free(&commits);
7611 free(branch_head_commit_id);
7612 free(resume_commit_id);
7615 got_object_commit_close(commit);
7617 got_ref_close(branch);
7618 if (new_base_branch)
7619 got_ref_close(new_base_branch);
7621 got_ref_close(tmp_branch);
7623 got_worktree_close(worktree);
7625 got_repo_close(repo);
7630 usage_histedit(void)
7632 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7637 #define GOT_HISTEDIT_PICK 'p'
7638 #define GOT_HISTEDIT_EDIT 'e'
7639 #define GOT_HISTEDIT_FOLD 'f'
7640 #define GOT_HISTEDIT_DROP 'd'
7641 #define GOT_HISTEDIT_MESG 'm'
7643 static struct got_histedit_cmd {
7647 } got_histedit_cmds[] = {
7648 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7649 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7650 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7652 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7653 { GOT_HISTEDIT_MESG, "mesg",
7654 "single-line log message for commit above (open editor if empty)" },
7657 struct got_histedit_list_entry {
7658 TAILQ_ENTRY(got_histedit_list_entry) entry;
7659 struct got_object_id *commit_id;
7660 const struct got_histedit_cmd *cmd;
7663 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7665 static const struct got_error *
7666 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7667 FILE *f, struct got_repository *repo)
7669 const struct got_error *err = NULL;
7670 char *logmsg = NULL, *id_str = NULL;
7671 struct got_commit_object *commit = NULL;
7674 err = got_object_open_as_commit(&commit, repo, commit_id);
7678 err = get_short_logmsg(&logmsg, 34, commit);
7682 err = got_object_id_str(&id_str, commit_id);
7686 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7688 err = got_ferror(f, GOT_ERR_IO);
7691 got_object_commit_close(commit);
7697 static const struct got_error *
7698 histedit_write_commit_list(struct got_object_id_queue *commits,
7699 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7701 const struct got_error *err = NULL;
7702 struct got_object_qid *qid;
7704 if (SIMPLEQ_EMPTY(commits))
7705 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7707 SIMPLEQ_FOREACH(qid, commits, entry) {
7708 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7712 if (edit_logmsg_only) {
7713 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7715 err = got_ferror(f, GOT_ERR_IO);
7724 static const struct got_error *
7725 write_cmd_list(FILE *f, const char *branch_name,
7726 struct got_object_id_queue *commits)
7728 const struct got_error *err = NULL;
7731 struct got_object_qid *qid;
7733 qid = SIMPLEQ_FIRST(commits);
7734 err = got_object_id_str(&id_str, qid->id);
7739 "# Editing the history of branch '%s' starting at\n"
7741 "# Commits will be processed in order from top to "
7742 "bottom of this file.\n", branch_name, id_str);
7744 err = got_ferror(f, GOT_ERR_IO);
7748 n = fprintf(f, "# Available histedit commands:\n");
7750 err = got_ferror(f, GOT_ERR_IO);
7754 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7755 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7756 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7759 err = got_ferror(f, GOT_ERR_IO);
7768 static const struct got_error *
7769 histedit_syntax_error(int lineno)
7771 static char msg[42];
7774 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7776 if (ret == -1 || ret >= sizeof(msg))
7777 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7779 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7782 static const struct got_error *
7783 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7784 char *logmsg, struct got_repository *repo)
7786 const struct got_error *err;
7787 struct got_commit_object *folded_commit = NULL;
7788 char *id_str, *folded_logmsg = NULL;
7790 err = got_object_id_str(&id_str, hle->commit_id);
7794 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7798 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7801 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7802 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7803 folded_logmsg) == -1) {
7804 err = got_error_from_errno("asprintf");
7808 got_object_commit_close(folded_commit);
7810 free(folded_logmsg);
7814 static struct got_histedit_list_entry *
7815 get_folded_commits(struct got_histedit_list_entry *hle)
7817 struct got_histedit_list_entry *prev, *folded = NULL;
7819 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7820 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7821 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7822 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7824 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7830 static const struct got_error *
7831 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7832 struct got_repository *repo)
7834 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7835 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7836 const struct got_error *err = NULL;
7837 struct got_commit_object *commit = NULL;
7840 struct got_histedit_list_entry *folded = NULL;
7842 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7846 folded = get_folded_commits(hle);
7848 while (folded != hle) {
7849 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7850 folded = TAILQ_NEXT(folded, entry);
7853 err = append_folded_commit_msg(&new_msg, folded,
7859 folded = TAILQ_NEXT(folded, entry);
7863 err = got_object_id_str(&id_str, hle->commit_id);
7866 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7869 logmsg_len = asprintf(&new_msg,
7870 "%s\n# original log message of commit %s: %s",
7871 logmsg ? logmsg : "", id_str, orig_logmsg);
7872 if (logmsg_len == -1) {
7873 err = got_error_from_errno("asprintf");
7879 err = got_object_id_str(&id_str, hle->commit_id);
7883 err = got_opentemp_named_fd(&logmsg_path, &fd,
7884 GOT_TMPDIR_STR "/got-logmsg");
7888 write(fd, logmsg, logmsg_len);
7891 err = get_editor(&editor);
7895 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7897 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7899 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7902 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7903 err = got_error_from_errno2("unlink", logmsg_path);
7909 got_object_commit_close(commit);
7913 static const struct got_error *
7914 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7915 FILE *f, struct got_repository *repo)
7917 const struct got_error *err = NULL;
7918 char *line = NULL, *p, *end;
7922 const struct got_histedit_cmd *cmd;
7923 struct got_object_id *commit_id = NULL;
7924 struct got_histedit_list_entry *hle = NULL;
7927 len = getline(&line, &size, f);
7929 const struct got_error *getline_err;
7932 getline_err = got_error_from_errno("getline");
7933 err = got_ferror(f, getline_err->code);
7938 while (isspace((unsigned char)p[0]))
7940 if (p[0] == '#' || p[0] == '\0') {
7946 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7947 cmd = &got_histedit_cmds[i];
7948 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7949 isspace((unsigned char)p[strlen(cmd->name)])) {
7950 p += strlen(cmd->name);
7953 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7958 if (i == nitems(got_histedit_cmds)) {
7959 err = histedit_syntax_error(lineno);
7962 while (isspace((unsigned char)p[0]))
7964 if (cmd->code == GOT_HISTEDIT_MESG) {
7965 if (hle == NULL || hle->logmsg != NULL) {
7966 err = got_error(GOT_ERR_HISTEDIT_CMD);
7970 err = histedit_edit_logmsg(hle, repo);
7974 hle->logmsg = strdup(p);
7975 if (hle->logmsg == NULL) {
7976 err = got_error_from_errno("strdup");
7985 while (end[0] && !isspace((unsigned char)end[0]))
7989 err = got_object_resolve_id_str(&commit_id, repo, p);
7991 /* override error code */
7992 err = histedit_syntax_error(lineno);
7996 hle = malloc(sizeof(*hle));
7998 err = got_error_from_errno("malloc");
8002 hle->commit_id = commit_id;
8007 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
8015 static const struct got_error *
8016 histedit_check_script(struct got_histedit_list *histedit_cmds,
8017 struct got_object_id_queue *commits, struct got_repository *repo)
8019 const struct got_error *err = NULL;
8020 struct got_object_qid *qid;
8021 struct got_histedit_list_entry *hle;
8022 static char msg[92];
8025 if (TAILQ_EMPTY(histedit_cmds))
8026 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
8027 "histedit script contains no commands");
8028 if (SIMPLEQ_EMPTY(commits))
8029 return got_error(GOT_ERR_EMPTY_HISTEDIT);
8031 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8032 struct got_histedit_list_entry *hle2;
8033 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
8036 if (got_object_id_cmp(hle->commit_id,
8037 hle2->commit_id) != 0)
8039 err = got_object_id_str(&id_str, hle->commit_id);
8042 snprintf(msg, sizeof(msg), "commit %s is listed "
8043 "more than once in histedit script", id_str);
8045 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8049 SIMPLEQ_FOREACH(qid, commits, entry) {
8050 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8051 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
8055 err = got_object_id_str(&id_str, qid->id);
8058 snprintf(msg, sizeof(msg),
8059 "commit %s missing from histedit script", id_str);
8061 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
8065 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
8066 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
8067 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
8068 "last commit in histedit script cannot be folded");
8073 static const struct got_error *
8074 histedit_run_editor(struct got_histedit_list *histedit_cmds,
8075 const char *path, struct got_object_id_queue *commits,
8076 struct got_repository *repo)
8078 const struct got_error *err = NULL;
8082 err = get_editor(&editor);
8086 if (spawn_editor(editor, path) == -1) {
8087 err = got_error_from_errno("failed spawning editor");
8091 f = fopen(path, "r");
8093 err = got_error_from_errno("fopen");
8096 err = histedit_parse_list(histedit_cmds, f, repo);
8100 err = histedit_check_script(histedit_cmds, commits, repo);
8102 if (f && fclose(f) != 0 && err == NULL)
8103 err = got_error_from_errno("fclose");
8108 static const struct got_error *
8109 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
8110 struct got_object_id_queue *, const char *, const char *,
8111 struct got_repository *);
8113 static const struct got_error *
8114 histedit_edit_script(struct got_histedit_list *histedit_cmds,
8115 struct got_object_id_queue *commits, const char *branch_name,
8116 int edit_logmsg_only, struct got_repository *repo)
8118 const struct got_error *err;
8122 err = got_opentemp_named(&path, &f, "got-histedit");
8126 err = write_cmd_list(f, branch_name, commits);
8130 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
8134 if (edit_logmsg_only) {
8136 err = histedit_parse_list(histedit_cmds, f, repo);
8138 if (fclose(f) != 0) {
8139 err = got_error_from_errno("fclose");
8143 err = histedit_run_editor(histedit_cmds, path, commits, repo);
8145 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8146 err->code != GOT_ERR_HISTEDIT_CMD)
8148 err = histedit_edit_list_retry(histedit_cmds, err,
8149 commits, path, branch_name, repo);
8153 if (f && fclose(f) != 0 && err == NULL)
8154 err = got_error_from_errno("fclose");
8155 if (path && unlink(path) != 0 && err == NULL)
8156 err = got_error_from_errno2("unlink", path);
8161 static const struct got_error *
8162 histedit_save_list(struct got_histedit_list *histedit_cmds,
8163 struct got_worktree *worktree, struct got_repository *repo)
8165 const struct got_error *err = NULL;
8168 struct got_histedit_list_entry *hle;
8169 struct got_commit_object *commit = NULL;
8171 err = got_worktree_get_histedit_script_path(&path, worktree);
8175 f = fopen(path, "w");
8177 err = got_error_from_errno2("fopen", path);
8180 TAILQ_FOREACH(hle, histedit_cmds, entry) {
8181 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
8187 int n = fprintf(f, "%c %s\n",
8188 GOT_HISTEDIT_MESG, hle->logmsg);
8190 err = got_ferror(f, GOT_ERR_IO);
8196 if (f && fclose(f) != 0 && err == NULL)
8197 err = got_error_from_errno("fclose");
8200 got_object_commit_close(commit);
8205 histedit_free_list(struct got_histedit_list *histedit_cmds)
8207 struct got_histedit_list_entry *hle;
8209 while ((hle = TAILQ_FIRST(histedit_cmds))) {
8210 TAILQ_REMOVE(histedit_cmds, hle, entry);
8215 static const struct got_error *
8216 histedit_load_list(struct got_histedit_list *histedit_cmds,
8217 const char *path, struct got_repository *repo)
8219 const struct got_error *err = NULL;
8222 f = fopen(path, "r");
8224 err = got_error_from_errno2("fopen", path);
8228 err = histedit_parse_list(histedit_cmds, f, repo);
8230 if (f && fclose(f) != 0 && err == NULL)
8231 err = got_error_from_errno("fclose");
8235 static const struct got_error *
8236 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
8237 const struct got_error *edit_err, struct got_object_id_queue *commits,
8238 const char *path, const char *branch_name, struct got_repository *repo)
8240 const struct got_error *err = NULL, *prev_err = edit_err;
8243 while (resp != 'c' && resp != 'r' && resp != 'a') {
8244 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8245 "or (a)bort: ", getprogname(), prev_err->msg);
8250 histedit_free_list(histedit_cmds);
8251 err = histedit_run_editor(histedit_cmds, path, commits,
8254 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8255 err->code != GOT_ERR_HISTEDIT_CMD)
8262 } else if (resp == 'r') {
8263 histedit_free_list(histedit_cmds);
8264 err = histedit_edit_script(histedit_cmds,
8265 commits, branch_name, 0, repo);
8267 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8268 err->code != GOT_ERR_HISTEDIT_CMD)
8275 } else if (resp == 'a') {
8276 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8279 printf("invalid response '%c'\n", resp);
8285 static const struct got_error *
8286 histedit_complete(struct got_worktree *worktree,
8287 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8288 struct got_reference *branch, struct got_repository *repo)
8290 printf("Switching work tree to %s\n",
8291 got_ref_get_symref_target(branch));
8292 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8296 static const struct got_error *
8297 show_histedit_progress(struct got_commit_object *commit,
8298 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8300 const struct got_error *err;
8301 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8303 err = got_object_id_str(&old_id_str, hle->commit_id);
8308 err = got_object_id_str(&new_id_str, new_id);
8313 old_id_str[12] = '\0';
8315 new_id_str[12] = '\0';
8318 logmsg = strdup(hle->logmsg);
8319 if (logmsg == NULL) {
8320 err = got_error_from_errno("strdup");
8323 trim_logmsg(logmsg, 42);
8325 err = get_short_logmsg(&logmsg, 42, commit);
8330 switch (hle->cmd->code) {
8331 case GOT_HISTEDIT_PICK:
8332 case GOT_HISTEDIT_EDIT:
8333 printf("%s -> %s: %s\n", old_id_str,
8334 new_id_str ? new_id_str : "no-op change", logmsg);
8336 case GOT_HISTEDIT_DROP:
8337 case GOT_HISTEDIT_FOLD:
8338 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8350 static const struct got_error *
8351 histedit_commit(struct got_pathlist_head *merged_paths,
8352 struct got_worktree *worktree, struct got_fileindex *fileindex,
8353 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8354 struct got_repository *repo)
8356 const struct got_error *err;
8357 struct got_commit_object *commit;
8358 struct got_object_id *new_commit_id;
8360 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8361 && hle->logmsg == NULL) {
8362 err = histedit_edit_logmsg(hle, repo);
8367 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8371 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8372 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8375 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8377 err = show_histedit_progress(commit, hle, NULL);
8379 err = show_histedit_progress(commit, hle, new_commit_id);
8380 free(new_commit_id);
8383 got_object_commit_close(commit);
8387 static const struct got_error *
8388 histedit_skip_commit(struct got_histedit_list_entry *hle,
8389 struct got_worktree *worktree, struct got_repository *repo)
8391 const struct got_error *error;
8392 struct got_commit_object *commit;
8394 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8399 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8403 error = show_histedit_progress(commit, hle, NULL);
8404 got_object_commit_close(commit);
8408 static const struct got_error *
8409 check_local_changes(void *arg, unsigned char status,
8410 unsigned char staged_status, const char *path,
8411 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8412 struct got_object_id *commit_id, int dirfd, const char *de_name)
8414 int *have_local_changes = arg;
8417 case GOT_STATUS_ADD:
8418 case GOT_STATUS_DELETE:
8419 case GOT_STATUS_MODIFY:
8420 case GOT_STATUS_CONFLICT:
8421 *have_local_changes = 1;
8422 return got_error(GOT_ERR_CANCELLED);
8427 switch (staged_status) {
8428 case GOT_STATUS_ADD:
8429 case GOT_STATUS_DELETE:
8430 case GOT_STATUS_MODIFY:
8431 *have_local_changes = 1;
8432 return got_error(GOT_ERR_CANCELLED);
8440 static const struct got_error *
8441 cmd_histedit(int argc, char *argv[])
8443 const struct got_error *error = NULL;
8444 struct got_worktree *worktree = NULL;
8445 struct got_fileindex *fileindex = NULL;
8446 struct got_repository *repo = NULL;
8448 struct got_reference *branch = NULL;
8449 struct got_reference *tmp_branch = NULL;
8450 struct got_object_id *resume_commit_id = NULL;
8451 struct got_object_id *base_commit_id = NULL;
8452 struct got_object_id *head_commit_id = NULL;
8453 struct got_commit_object *commit = NULL;
8454 int ch, rebase_in_progress = 0;
8455 struct got_update_progress_arg upa;
8456 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8457 int edit_logmsg_only = 0;
8458 const char *edit_script_path = NULL;
8459 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8460 struct got_object_id_queue commits;
8461 struct got_pathlist_head merged_paths;
8462 const struct got_object_id_queue *parent_ids;
8463 struct got_object_qid *pid;
8464 struct got_histedit_list histedit_cmds;
8465 struct got_histedit_list_entry *hle;
8467 SIMPLEQ_INIT(&commits);
8468 TAILQ_INIT(&histedit_cmds);
8469 TAILQ_INIT(&merged_paths);
8470 memset(&upa, 0, sizeof(upa));
8472 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8481 edit_script_path = optarg;
8484 edit_logmsg_only = 1;
8496 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8497 "unveil", NULL) == -1)
8500 if (abort_edit && continue_edit)
8501 errx(1, "histedit's -a and -c options are mutually exclusive");
8502 if (edit_script_path && edit_logmsg_only)
8503 errx(1, "histedit's -F and -m options are mutually exclusive");
8504 if (abort_edit && edit_logmsg_only)
8505 errx(1, "histedit's -a and -m options are mutually exclusive");
8506 if (continue_edit && edit_logmsg_only)
8507 errx(1, "histedit's -c and -m options are mutually exclusive");
8512 * This command cannot apply unveil(2) in all cases because the
8513 * user may choose to run an editor to edit the histedit script
8514 * and to edit individual commit log messages.
8515 * unveil(2) traverses exec(2); if an editor is used we have to
8516 * apply unveil after edit script and log messages have been written.
8517 * XXX TODO: Make use of unveil(2) where possible.
8520 cwd = getcwd(NULL, 0);
8522 error = got_error_from_errno("getcwd");
8525 error = got_worktree_open(&worktree, cwd);
8527 if (error->code == GOT_ERR_NOT_WORKTREE)
8528 error = wrap_not_worktree_error(error, "histedit", cwd);
8532 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8537 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8540 if (rebase_in_progress) {
8541 error = got_error(GOT_ERR_REBASING);
8545 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8549 if (edit_in_progress && edit_logmsg_only) {
8550 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8551 "histedit operation is in progress in this "
8552 "work tree and must be continued or aborted "
8553 "before the -m option can be used");
8557 if (edit_in_progress && abort_edit) {
8558 error = got_worktree_histedit_continue(&resume_commit_id,
8559 &tmp_branch, &branch, &base_commit_id, &fileindex,
8563 printf("Switching work tree to %s\n",
8564 got_ref_get_symref_target(branch));
8565 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8566 branch, base_commit_id, update_progress, &upa);
8569 printf("Histedit of %s aborted\n",
8570 got_ref_get_symref_target(branch));
8571 print_update_progress_stats(&upa);
8572 goto done; /* nothing else to do */
8573 } else if (abort_edit) {
8574 error = got_error(GOT_ERR_NOT_HISTEDIT);
8578 if (continue_edit) {
8581 if (!edit_in_progress) {
8582 error = got_error(GOT_ERR_NOT_HISTEDIT);
8586 error = got_worktree_get_histedit_script_path(&path, worktree);
8590 error = histedit_load_list(&histedit_cmds, path, repo);
8595 error = got_worktree_histedit_continue(&resume_commit_id,
8596 &tmp_branch, &branch, &base_commit_id, &fileindex,
8601 error = got_ref_resolve(&head_commit_id, repo, branch);
8605 error = got_object_open_as_commit(&commit, repo,
8609 parent_ids = got_object_commit_get_parent_ids(commit);
8610 pid = SIMPLEQ_FIRST(parent_ids);
8612 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8615 error = collect_commits(&commits, head_commit_id, pid->id,
8616 base_commit_id, got_worktree_get_path_prefix(worktree),
8617 GOT_ERR_HISTEDIT_PATH, repo);
8618 got_object_commit_close(commit);
8623 if (edit_in_progress) {
8624 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8628 error = got_ref_open(&branch, repo,
8629 got_worktree_get_head_ref_name(worktree), 0);
8633 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8634 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8635 "will not edit commit history of a branch outside "
8636 "the \"refs/heads/\" reference namespace");
8640 error = got_ref_resolve(&head_commit_id, repo, branch);
8641 got_ref_close(branch);
8646 error = got_object_open_as_commit(&commit, repo,
8650 parent_ids = got_object_commit_get_parent_ids(commit);
8651 pid = SIMPLEQ_FIRST(parent_ids);
8653 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8656 error = collect_commits(&commits, head_commit_id, pid->id,
8657 got_worktree_get_base_commit_id(worktree),
8658 got_worktree_get_path_prefix(worktree),
8659 GOT_ERR_HISTEDIT_PATH, repo);
8660 got_object_commit_close(commit);
8665 if (SIMPLEQ_EMPTY(&commits)) {
8666 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8670 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8671 &base_commit_id, &fileindex, worktree, repo);
8675 if (edit_script_path) {
8676 error = histedit_load_list(&histedit_cmds,
8677 edit_script_path, repo);
8679 got_worktree_histedit_abort(worktree, fileindex,
8680 repo, branch, base_commit_id,
8681 update_progress, &upa);
8682 print_update_progress_stats(&upa);
8686 const char *branch_name;
8687 branch_name = got_ref_get_symref_target(branch);
8688 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8690 error = histedit_edit_script(&histedit_cmds, &commits,
8691 branch_name, edit_logmsg_only, repo);
8693 got_worktree_histedit_abort(worktree, fileindex,
8694 repo, branch, base_commit_id,
8695 update_progress, &upa);
8696 print_update_progress_stats(&upa);
8702 error = histedit_save_list(&histedit_cmds, worktree,
8705 got_worktree_histedit_abort(worktree, fileindex,
8706 repo, branch, base_commit_id,
8707 update_progress, &upa);
8708 print_update_progress_stats(&upa);
8714 error = histedit_check_script(&histedit_cmds, &commits, repo);
8718 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8719 if (resume_commit_id) {
8720 if (got_object_id_cmp(hle->commit_id,
8721 resume_commit_id) != 0)
8724 resume_commit_id = NULL;
8725 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8726 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8727 error = histedit_skip_commit(hle, worktree,
8732 struct got_pathlist_head paths;
8733 int have_changes = 0;
8736 error = got_pathlist_append(&paths, "", NULL);
8739 error = got_worktree_status(worktree, &paths,
8740 repo, check_local_changes, &have_changes,
8741 check_cancelled, NULL);
8742 got_pathlist_free(&paths);
8744 if (error->code != GOT_ERR_CANCELLED)
8746 if (sigint_received || sigpipe_received)
8750 error = histedit_commit(NULL, worktree,
8751 fileindex, tmp_branch, hle, repo);
8755 error = got_object_open_as_commit(
8756 &commit, repo, hle->commit_id);
8759 error = show_histedit_progress(commit,
8761 got_object_commit_close(commit);
8770 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8771 error = histedit_skip_commit(hle, worktree, repo);
8777 error = got_object_open_as_commit(&commit, repo,
8781 parent_ids = got_object_commit_get_parent_ids(commit);
8782 pid = SIMPLEQ_FIRST(parent_ids);
8784 error = got_worktree_histedit_merge_files(&merged_paths,
8785 worktree, fileindex, pid->id, hle->commit_id, repo,
8786 update_progress, &upa, check_cancelled, NULL);
8789 got_object_commit_close(commit);
8792 print_update_progress_stats(&upa);
8793 if (upa.conflicts > 0)
8794 rebase_status = GOT_STATUS_CONFLICT;
8796 if (rebase_status == GOT_STATUS_CONFLICT) {
8797 error = show_rebase_merge_conflict(hle->commit_id,
8801 got_worktree_rebase_pathlist_free(&merged_paths);
8805 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8807 error = got_object_id_str(&id_str, hle->commit_id);
8810 printf("Stopping histedit for amending commit %s\n",
8813 got_worktree_rebase_pathlist_free(&merged_paths);
8814 error = got_worktree_histedit_postpone(worktree,
8819 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8820 error = histedit_skip_commit(hle, worktree, repo);
8826 error = histedit_commit(&merged_paths, worktree, fileindex,
8827 tmp_branch, hle, repo);
8828 got_worktree_rebase_pathlist_free(&merged_paths);
8833 if (rebase_status == GOT_STATUS_CONFLICT) {
8834 error = got_worktree_histedit_postpone(worktree, fileindex);
8837 error = got_error_msg(GOT_ERR_CONFLICTS,
8838 "conflicts must be resolved before histedit can continue");
8840 error = histedit_complete(worktree, fileindex, tmp_branch,
8843 got_object_id_queue_free(&commits);
8844 histedit_free_list(&histedit_cmds);
8845 free(head_commit_id);
8846 free(base_commit_id);
8847 free(resume_commit_id);
8849 got_object_commit_close(commit);
8851 got_ref_close(branch);
8853 got_ref_close(tmp_branch);
8855 got_worktree_close(worktree);
8857 got_repo_close(repo);
8862 usage_integrate(void)
8864 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8868 static const struct got_error *
8869 cmd_integrate(int argc, char *argv[])
8871 const struct got_error *error = NULL;
8872 struct got_repository *repo = NULL;
8873 struct got_worktree *worktree = NULL;
8874 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8875 const char *branch_arg = NULL;
8876 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8877 struct got_fileindex *fileindex = NULL;
8878 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8880 struct got_update_progress_arg upa;
8882 while ((ch = getopt(argc, argv, "")) != -1) {
8895 branch_arg = argv[0];
8897 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8898 "unveil", NULL) == -1)
8901 cwd = getcwd(NULL, 0);
8903 error = got_error_from_errno("getcwd");
8907 error = got_worktree_open(&worktree, cwd);
8909 if (error->code == GOT_ERR_NOT_WORKTREE)
8910 error = wrap_not_worktree_error(error, "integrate",
8915 error = check_rebase_or_histedit_in_progress(worktree);
8919 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8924 error = apply_unveil(got_repo_get_path(repo), 0,
8925 got_worktree_get_root_path(worktree));
8929 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8930 error = got_error_from_errno("asprintf");
8934 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8935 &base_branch_ref, worktree, refname, repo);
8939 refname = strdup(got_ref_get_name(branch_ref));
8940 if (refname == NULL) {
8941 error = got_error_from_errno("strdup");
8942 got_worktree_integrate_abort(worktree, fileindex, repo,
8943 branch_ref, base_branch_ref);
8946 base_refname = strdup(got_ref_get_name(base_branch_ref));
8947 if (base_refname == NULL) {
8948 error = got_error_from_errno("strdup");
8949 got_worktree_integrate_abort(worktree, fileindex, repo,
8950 branch_ref, base_branch_ref);
8954 error = got_ref_resolve(&commit_id, repo, branch_ref);
8958 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8962 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8963 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8964 "specified branch has already been integrated");
8965 got_worktree_integrate_abort(worktree, fileindex, repo,
8966 branch_ref, base_branch_ref);
8970 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8972 if (error->code == GOT_ERR_ANCESTRY)
8973 error = got_error(GOT_ERR_REBASE_REQUIRED);
8974 got_worktree_integrate_abort(worktree, fileindex, repo,
8975 branch_ref, base_branch_ref);
8979 memset(&upa, 0, sizeof(upa));
8980 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8981 branch_ref, base_branch_ref, update_progress, &upa,
8982 check_cancelled, NULL);
8986 printf("Integrated %s into %s\n", refname, base_refname);
8987 print_update_progress_stats(&upa);
8990 got_repo_close(repo);
8992 got_worktree_close(worktree);
8994 free(base_commit_id);
9004 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
9005 "[-S] [file-path ...]\n",
9010 static const struct got_error *
9011 print_stage(void *arg, unsigned char status, unsigned char staged_status,
9012 const char *path, struct got_object_id *blob_id,
9013 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
9014 int dirfd, const char *de_name)
9016 const struct got_error *err = NULL;
9017 char *id_str = NULL;
9019 if (staged_status != GOT_STATUS_ADD &&
9020 staged_status != GOT_STATUS_MODIFY &&
9021 staged_status != GOT_STATUS_DELETE)
9024 if (staged_status == GOT_STATUS_ADD ||
9025 staged_status == GOT_STATUS_MODIFY)
9026 err = got_object_id_str(&id_str, staged_blob_id);
9028 err = got_object_id_str(&id_str, blob_id);
9032 printf("%s %c %s\n", id_str, staged_status, path);
9037 static const struct got_error *
9038 cmd_stage(int argc, char *argv[])
9040 const struct got_error *error = NULL;
9041 struct got_repository *repo = NULL;
9042 struct got_worktree *worktree = NULL;
9044 struct got_pathlist_head paths;
9045 struct got_pathlist_entry *pe;
9046 int ch, list_stage = 0, pflag = 0, allow_bad_symlinks = 0;
9047 FILE *patch_script_file = NULL;
9048 const char *patch_script_path = NULL;
9049 struct choose_patch_arg cpa;
9053 while ((ch = getopt(argc, argv, "lpF:S")) != -1) {
9062 patch_script_path = optarg;
9065 allow_bad_symlinks = 1;
9077 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9078 "unveil", NULL) == -1)
9081 if (list_stage && (pflag || patch_script_path))
9082 errx(1, "-l option cannot be used with other options");
9083 if (patch_script_path && !pflag)
9084 errx(1, "-F option can only be used together with -p option");
9086 cwd = getcwd(NULL, 0);
9088 error = got_error_from_errno("getcwd");
9092 error = got_worktree_open(&worktree, cwd);
9094 if (error->code == GOT_ERR_NOT_WORKTREE)
9095 error = wrap_not_worktree_error(error, "stage", cwd);
9099 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9104 if (patch_script_path) {
9105 patch_script_file = fopen(patch_script_path, "r");
9106 if (patch_script_file == NULL) {
9107 error = got_error_from_errno2("fopen",
9112 error = apply_unveil(got_repo_get_path(repo), 0,
9113 got_worktree_get_root_path(worktree));
9117 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9122 error = got_worktree_status(worktree, &paths, repo,
9123 print_stage, NULL, check_cancelled, NULL);
9125 cpa.patch_script_file = patch_script_file;
9126 cpa.action = "stage";
9127 error = got_worktree_stage(worktree, &paths,
9128 pflag ? NULL : print_status, NULL,
9129 pflag ? choose_patch : NULL, &cpa,
9130 allow_bad_symlinks, repo);
9133 if (patch_script_file && fclose(patch_script_file) == EOF &&
9135 error = got_error_from_errno2("fclose", patch_script_path);
9137 got_repo_close(repo);
9139 got_worktree_close(worktree);
9140 TAILQ_FOREACH(pe, &paths, entry)
9141 free((char *)pe->path);
9142 got_pathlist_free(&paths);
9150 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
9151 "[file-path ...]\n",
9157 static const struct got_error *
9158 cmd_unstage(int argc, char *argv[])
9160 const struct got_error *error = NULL;
9161 struct got_repository *repo = NULL;
9162 struct got_worktree *worktree = NULL;
9164 struct got_pathlist_head paths;
9165 struct got_pathlist_entry *pe;
9167 struct got_update_progress_arg upa;
9168 FILE *patch_script_file = NULL;
9169 const char *patch_script_path = NULL;
9170 struct choose_patch_arg cpa;
9174 while ((ch = getopt(argc, argv, "pF:")) != -1) {
9180 patch_script_path = optarg;
9192 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
9193 "unveil", NULL) == -1)
9196 if (patch_script_path && !pflag)
9197 errx(1, "-F option can only be used together with -p option");
9199 cwd = getcwd(NULL, 0);
9201 error = got_error_from_errno("getcwd");
9205 error = got_worktree_open(&worktree, cwd);
9207 if (error->code == GOT_ERR_NOT_WORKTREE)
9208 error = wrap_not_worktree_error(error, "unstage", cwd);
9212 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
9217 if (patch_script_path) {
9218 patch_script_file = fopen(patch_script_path, "r");
9219 if (patch_script_file == NULL) {
9220 error = got_error_from_errno2("fopen",
9226 error = apply_unveil(got_repo_get_path(repo), 0,
9227 got_worktree_get_root_path(worktree));
9231 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
9235 cpa.patch_script_file = patch_script_file;
9236 cpa.action = "unstage";
9237 memset(&upa, 0, sizeof(upa));
9238 error = got_worktree_unstage(worktree, &paths, update_progress,
9239 &upa, pflag ? choose_patch : NULL, &cpa, repo);
9241 print_update_progress_stats(&upa);
9243 if (patch_script_file && fclose(patch_script_file) == EOF &&
9245 error = got_error_from_errno2("fclose", patch_script_path);
9247 got_repo_close(repo);
9249 got_worktree_close(worktree);
9250 TAILQ_FOREACH(pe, &paths, entry)
9251 free((char *)pe->path);
9252 got_pathlist_free(&paths);
9260 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9261 "arg1 [arg2 ...]\n", getprogname());
9265 static const struct got_error *
9266 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9268 const struct got_error *err;
9269 struct got_blob_object *blob;
9271 err = got_object_open_as_blob(&blob, repo, id, 8192);
9275 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9276 got_object_blob_close(blob);
9280 static const struct got_error *
9281 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9283 const struct got_error *err;
9284 struct got_tree_object *tree;
9287 err = got_object_open_as_tree(&tree, repo, id);
9291 nentries = got_object_tree_get_nentries(tree);
9292 for (i = 0; i < nentries; i++) {
9293 struct got_tree_entry *te;
9295 if (sigint_received || sigpipe_received)
9297 te = got_object_tree_get_entry(tree, i);
9298 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9301 fprintf(outfile, "%s %.7o %s\n", id_str,
9302 got_tree_entry_get_mode(te),
9303 got_tree_entry_get_name(te));
9307 got_object_tree_close(tree);
9311 static const struct got_error *
9312 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9314 const struct got_error *err;
9315 struct got_commit_object *commit;
9316 const struct got_object_id_queue *parent_ids;
9317 struct got_object_qid *pid;
9318 char *id_str = NULL;
9319 const char *logmsg = NULL;
9321 err = got_object_open_as_commit(&commit, repo, id);
9325 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9329 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9330 parent_ids = got_object_commit_get_parent_ids(commit);
9331 fprintf(outfile, "numparents %d\n",
9332 got_object_commit_get_nparents(commit));
9333 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9335 err = got_object_id_str(&pid_str, pid->id);
9338 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9341 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9342 got_object_commit_get_author(commit),
9343 got_object_commit_get_author_time(commit));
9345 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9346 got_object_commit_get_author(commit),
9347 got_object_commit_get_committer_time(commit));
9349 logmsg = got_object_commit_get_logmsg_raw(commit);
9350 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9351 fprintf(outfile, "%s", logmsg);
9354 got_object_commit_close(commit);
9358 static const struct got_error *
9359 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9361 const struct got_error *err;
9362 struct got_tag_object *tag;
9363 char *id_str = NULL;
9364 const char *tagmsg = NULL;
9366 err = got_object_open_as_tag(&tag, repo, id);
9370 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9374 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9376 switch (got_object_tag_get_object_type(tag)) {
9377 case GOT_OBJ_TYPE_BLOB:
9378 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9379 GOT_OBJ_LABEL_BLOB);
9381 case GOT_OBJ_TYPE_TREE:
9382 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9383 GOT_OBJ_LABEL_TREE);
9385 case GOT_OBJ_TYPE_COMMIT:
9386 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9387 GOT_OBJ_LABEL_COMMIT);
9389 case GOT_OBJ_TYPE_TAG:
9390 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9397 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9398 got_object_tag_get_name(tag));
9400 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9401 got_object_tag_get_tagger(tag),
9402 got_object_tag_get_tagger_time(tag));
9404 tagmsg = got_object_tag_get_message(tag);
9405 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9406 fprintf(outfile, "%s", tagmsg);
9409 got_object_tag_close(tag);
9413 static const struct got_error *
9414 cmd_cat(int argc, char *argv[])
9416 const struct got_error *error;
9417 struct got_repository *repo = NULL;
9418 struct got_worktree *worktree = NULL;
9419 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9420 const char *commit_id_str = NULL;
9421 struct got_object_id *id = NULL, *commit_id = NULL;
9422 int ch, obj_type, i, force_path = 0;
9425 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9430 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9433 commit_id_str = optarg;
9436 repo_path = realpath(optarg, NULL);
9437 if (repo_path == NULL)
9438 return got_error_from_errno2("realpath",
9440 got_path_strip_trailing_slashes(repo_path);
9454 cwd = getcwd(NULL, 0);
9456 error = got_error_from_errno("getcwd");
9459 error = got_worktree_open(&worktree, cwd);
9460 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9463 if (repo_path == NULL) {
9465 got_worktree_get_repo_path(worktree));
9466 if (repo_path == NULL) {
9467 error = got_error_from_errno("strdup");
9473 if (repo_path == NULL) {
9474 repo_path = getcwd(NULL, 0);
9475 if (repo_path == NULL)
9476 return got_error_from_errno("getcwd");
9479 error = got_repo_open(&repo, repo_path, NULL);
9484 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9488 if (commit_id_str == NULL)
9489 commit_id_str = GOT_REF_HEAD;
9490 error = got_repo_match_object_id(&commit_id, NULL,
9491 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9495 for (i = 0; i < argc; i++) {
9497 error = got_object_id_by_path(&id, repo, commit_id,
9502 error = got_repo_match_object_id(&id, &label, argv[i],
9503 GOT_OBJ_TYPE_ANY, 0, repo);
9505 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9506 error->code != GOT_ERR_NOT_REF)
9508 error = got_object_id_by_path(&id, repo,
9509 commit_id, argv[i]);
9515 error = got_object_get_type(&obj_type, repo, id);
9520 case GOT_OBJ_TYPE_BLOB:
9521 error = cat_blob(id, repo, stdout);
9523 case GOT_OBJ_TYPE_TREE:
9524 error = cat_tree(id, repo, stdout);
9526 case GOT_OBJ_TYPE_COMMIT:
9527 error = cat_commit(id, repo, stdout);
9529 case GOT_OBJ_TYPE_TAG:
9530 error = cat_tag(id, repo, stdout);
9533 error = got_error(GOT_ERR_OBJ_TYPE);
9548 got_worktree_close(worktree);
9550 const struct got_error *repo_error;
9551 repo_error = got_repo_close(repo);
9561 fprintf(stderr, "usage: %s info [path ...]\n",
9566 static const struct got_error *
9567 print_path_info(void *arg, const char *path, mode_t mode, time_t mtime,
9568 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
9569 struct got_object_id *commit_id)
9571 const struct got_error *err = NULL;
9572 char *id_str = NULL;
9574 struct tm mytm, *tm;
9575 struct got_pathlist_head *paths = arg;
9576 struct got_pathlist_entry *pe;
9579 * Clear error indication from any of the path arguments which
9580 * would cause this file index entry to be displayed.
9582 TAILQ_FOREACH(pe, paths, entry) {
9583 if (got_path_cmp(path, pe->path, strlen(path),
9584 pe->path_len) == 0 ||
9585 got_path_is_child(path, pe->path, pe->path_len))
9586 pe->data = NULL; /* no error */
9589 printf(GOT_COMMIT_SEP_STR);
9591 printf("symlink: %s\n", path);
9592 else if (S_ISREG(mode)) {
9593 printf("file: %s\n", path);
9594 printf("mode: %o\n", mode & (S_IRWXU | S_IRWXG | S_IRWXO));
9595 } else if (S_ISDIR(mode))
9596 printf("directory: %s\n", path);
9598 printf("something: %s\n", path);
9600 tm = localtime_r(&mtime, &mytm);
9603 if (strftime(datebuf, sizeof(datebuf), "%c %Z", tm) >= sizeof(datebuf))
9604 return got_error(GOT_ERR_NO_SPACE);
9605 printf("timestamp: %s\n", datebuf);
9608 err = got_object_id_str(&id_str, blob_id);
9611 printf("based on blob: %s\n", id_str);
9615 if (staged_blob_id) {
9616 err = got_object_id_str(&id_str, staged_blob_id);
9619 printf("based on staged blob: %s\n", id_str);
9624 err = got_object_id_str(&id_str, commit_id);
9627 printf("based on commit: %s\n", id_str);
9634 static const struct got_error *
9635 cmd_info(int argc, char *argv[])
9637 const struct got_error *error = NULL;
9638 struct got_worktree *worktree = NULL;
9639 char *cwd = NULL, *id_str = NULL;
9640 struct got_pathlist_head paths;
9641 struct got_pathlist_entry *pe;
9642 char *uuidstr = NULL;
9643 int ch, show_files = 0;
9647 while ((ch = getopt(argc, argv, "")) != -1) {
9659 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
9663 cwd = getcwd(NULL, 0);
9665 error = got_error_from_errno("getcwd");
9669 error = got_worktree_open(&worktree, cwd);
9671 if (error->code == GOT_ERR_NOT_WORKTREE)
9672 error = wrap_not_worktree_error(error, "status", cwd);
9676 error = apply_unveil(NULL, 0, got_worktree_get_root_path(worktree));
9681 error = get_worktree_paths_from_argv(&paths, argc, argv,
9688 error = got_object_id_str(&id_str,
9689 got_worktree_get_base_commit_id(worktree));
9693 error = got_worktree_get_uuid(&uuidstr, worktree);
9697 printf("work tree: %s\n", got_worktree_get_root_path(worktree));
9698 printf("work tree base commit: %s\n", id_str);
9699 printf("work tree path prefix: %s\n",
9700 got_worktree_get_path_prefix(worktree));
9701 printf("work tree branch reference: %s\n",
9702 got_worktree_get_head_ref_name(worktree));
9703 printf("work tree UUID: %s\n", uuidstr);
9704 printf("repository: %s\n", got_worktree_get_repo_path(worktree));
9707 struct got_pathlist_entry *pe;
9708 TAILQ_FOREACH(pe, &paths, entry) {
9709 if (pe->path_len == 0)
9712 * Assume this path will fail. This will be corrected
9713 * in print_path_info() in case the path does suceeed.
9715 pe->data = (void *)got_error_path(pe->path,
9718 error = got_worktree_path_info(worktree, &paths,
9719 print_path_info, &paths, check_cancelled, NULL);
9722 TAILQ_FOREACH(pe, &paths, entry) {
9723 if (pe->data != NULL) {
9724 error = pe->data; /* bad path */
9730 TAILQ_FOREACH(pe, &paths, entry)
9731 free((char *)pe->path);
9732 got_pathlist_free(&paths);