2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 "[-R reference] repository-url [directory]\n", getprogname());
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
873 if (print_size || print_indexed || print_resolved)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
878 printf("; indexing %d%%", p_indexed);
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
887 static const struct got_error *
888 create_symref(const char *refname, struct got_reference *target_ref,
889 int verbosity, struct got_repository *repo)
891 const struct got_error *err;
892 struct got_reference *head_symref;
894 err = got_ref_alloc_symref(&head_symref, refname, target_ref);
898 err = got_ref_write(head_symref, repo);
899 got_ref_close(head_symref);
900 if (err == NULL && verbosity > 0) {
901 printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 got_ref_get_name(target_ref));
907 static const struct got_error *
908 list_remote_refs(struct got_pathlist_head *symrefs,
909 struct got_pathlist_head *refs)
911 const struct got_error *err;
912 struct got_pathlist_entry *pe;
914 TAILQ_FOREACH(pe, symrefs, entry) {
915 const char *refname = pe->path;
916 const char *targetref = pe->data;
918 printf("%s: %s\n", refname, targetref);
921 TAILQ_FOREACH(pe, refs, entry) {
922 const char *refname = pe->path;
923 struct got_object_id *id = pe->data;
926 err = got_object_id_str(&id_str, id);
929 printf("%s: %s\n", refname, id_str);
936 static const struct got_error *
937 create_ref(const char *refname, struct got_object_id *id,
938 int verbosity, struct got_repository *repo)
940 const struct got_error *err = NULL;
941 struct got_reference *ref;
944 err = got_object_id_str(&id_str, id);
948 err = got_ref_alloc(&ref, refname, id);
952 err = got_ref_write(ref, repo);
955 if (err == NULL && verbosity >= 0)
956 printf("Created reference %s: %s\n", refname, id_str);
963 match_wanted_ref(const char *refname, const char *wanted_ref)
965 if (strncmp(refname, "refs/", 5) != 0)
970 * Prevent fetching of references that won't make any
971 * sense outside of the remote repository's context.
973 if (strncmp(refname, "got/", 4) == 0)
975 if (strncmp(refname, "remotes/", 8) == 0)
978 if (strncmp(wanted_ref, "refs/", 5) == 0)
981 /* Allow prefix match. */
982 if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
985 /* Allow exact match. */
986 return (strcmp(refname, wanted_ref) == 0);
990 is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
992 struct got_pathlist_entry *pe;
994 TAILQ_FOREACH(pe, wanted_refs, entry) {
995 if (match_wanted_ref(refname, pe->path))
1002 static const struct got_error *
1003 create_wanted_ref(const char *refname, struct got_object_id *id,
1004 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1006 const struct got_error *err;
1007 char *remote_refname;
1009 if (strncmp("refs/", refname, 5) == 0)
1012 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 remote_repo_name, refname) == -1)
1014 return got_error_from_errno("asprintf");
1016 err = create_ref(remote_refname, id, verbosity, repo);
1017 free(remote_refname);
1021 static const struct got_error *
1022 cmd_clone(int argc, char *argv[])
1024 const struct got_error *error = NULL;
1025 const char *uri, *dirname;
1026 char *proto, *host, *port, *repo_name, *server_path;
1027 char *default_destdir = NULL, *id_str = NULL;
1028 const char *repo_path;
1029 struct got_repository *repo = NULL;
1030 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 struct got_pathlist_entry *pe;
1032 struct got_object_id *pack_hash = NULL;
1033 int ch, fetchfd = -1, fetchstatus;
1034 pid_t fetchpid = -1;
1035 struct got_fetch_progress_arg fpa;
1036 char *git_url = NULL;
1037 char *gitconfig_path = NULL;
1038 char *gitconfig = NULL;
1039 FILE *gitconfig_file = NULL;
1041 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 int list_refs_only = 0;
1043 struct got_reference *head_symref = NULL;
1046 TAILQ_INIT(&symrefs);
1047 TAILQ_INIT(&wanted_branches);
1048 TAILQ_INIT(&wanted_refs);
1050 while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1053 fetch_all_branches = 1;
1056 error = got_pathlist_append(&wanted_branches,
1065 mirror_references = 1;
1070 else if (verbosity < 3)
1077 error = got_pathlist_append(&wanted_refs,
1090 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 errx(1, "-a and -b options are mutually exclusive");
1092 if (list_refs_only) {
1093 if (!TAILQ_EMPTY(&wanted_branches))
1094 errx(1, "-l and -b options are mutually exclusive");
1095 if (fetch_all_branches)
1096 errx(1, "-l and -a options are mutually exclusive");
1097 if (mirror_references)
1098 errx(1, "-l and -m options are mutually exclusive");
1099 if (verbosity == -1)
1100 errx(1, "-l and -q options are mutually exclusive");
1101 if (!TAILQ_EMPTY(&wanted_refs))
1102 errx(1, "-l and -R options are mutually exclusive");
1114 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 &repo_name, argv[0]);
1119 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 host, port ? ":" : "", port ? port : "",
1121 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 error = got_error_from_errno("asprintf");
1126 if (strcmp(proto, "git") == 0) {
1128 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 "sendfd dns inet unveil", NULL) == -1)
1132 } else if (strcmp(proto, "git+ssh") == 0 ||
1133 strcmp(proto, "ssh") == 0) {
1135 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 "sendfd unveil", NULL) == -1)
1139 } else if (strcmp(proto, "http") == 0 ||
1140 strcmp(proto, "git+http") == 0) {
1141 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1144 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1147 if (dirname == NULL) {
1148 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 error = got_error_from_errno("asprintf");
1152 repo_path = default_destdir;
1154 repo_path = dirname;
1156 if (!list_refs_only) {
1157 error = got_path_mkdir(repo_path);
1161 error = got_repo_init(repo_path);
1164 error = got_repo_open(&repo, repo_path, NULL);
1169 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 error = got_error_from_errno2("unveil",
1172 GOT_FETCH_PATH_SSH);
1176 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1181 printf("Connecting to %s%s%s\n", host,
1182 port ? ":" : "", port ? port : "");
1184 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1185 server_path, verbosity);
1189 fpa.last_scaled_size[0] = '\0';
1190 fpa.last_p_indexed = -1;
1191 fpa.last_p_resolved = -1;
1192 fpa.verbosity = verbosity;
1193 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 fetch_all_branches, &wanted_branches, &wanted_refs,
1196 list_refs_only, verbosity, fetchfd, repo,
1197 fetch_progress, &fpa);
1201 if (list_refs_only) {
1202 error = list_remote_refs(&symrefs, &refs);
1206 error = got_object_id_str(&id_str, pack_hash);
1210 printf("\nFetched %s.pack\n", id_str);
1213 /* Set up references provided with the pack file. */
1214 TAILQ_FOREACH(pe, &refs, entry) {
1215 const char *refname = pe->path;
1216 struct got_object_id *id = pe->data;
1217 char *remote_refname;
1219 if (is_wanted_ref(&wanted_refs, refname) &&
1220 !mirror_references) {
1221 error = create_wanted_ref(refname, id,
1222 GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 verbosity - 1, repo);
1229 error = create_ref(refname, id, verbosity - 1, repo);
1233 if (mirror_references)
1236 if (strncmp("refs/heads/", refname, 11) != 0)
1239 if (asprintf(&remote_refname,
1240 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 refname + 11) == -1) {
1242 error = got_error_from_errno("asprintf");
1245 error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 free(remote_refname);
1251 /* Set the HEAD reference if the server provided one. */
1252 TAILQ_FOREACH(pe, &symrefs, entry) {
1253 struct got_reference *target_ref;
1254 const char *refname = pe->path;
1255 const char *target = pe->data;
1256 char *remote_refname = NULL, *remote_target = NULL;
1258 if (strcmp(refname, GOT_REF_HEAD) != 0)
1261 error = got_ref_open(&target_ref, repo, target, 0);
1263 if (error->code == GOT_ERR_NOT_REF) {
1270 error = create_symref(refname, target_ref, verbosity, repo);
1271 got_ref_close(target_ref);
1275 if (mirror_references)
1278 if (strncmp("refs/heads/", target, 11) != 0)
1281 if (asprintf(&remote_refname,
1282 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1284 error = got_error_from_errno("asprintf");
1287 if (asprintf(&remote_target,
1288 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 target + 11) == -1) {
1290 error = got_error_from_errno("asprintf");
1291 free(remote_refname);
1294 error = got_ref_open(&target_ref, repo, remote_target, 0);
1296 free(remote_refname);
1297 free(remote_target);
1298 if (error->code == GOT_ERR_NOT_REF) {
1304 error = create_symref(remote_refname, target_ref,
1305 verbosity - 1, repo);
1306 free(remote_refname);
1307 free(remote_target);
1308 got_ref_close(target_ref);
1314 * We failed to set the HEAD reference. If we asked for
1315 * a set of wanted branches use the first of one of those
1316 * which could be fetched instead.
1318 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 const char *target = pe->path;
1320 struct got_reference *target_ref;
1322 error = got_ref_open(&target_ref, repo, target, 0);
1324 if (error->code == GOT_ERR_NOT_REF) {
1331 error = create_symref(GOT_REF_HEAD, target_ref,
1333 got_ref_close(target_ref);
1340 /* Create a config file git-fetch(1) can understand. */
1341 gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 if (gitconfig_path == NULL) {
1343 error = got_error_from_errno("got_repo_get_path_gitconfig");
1346 gitconfig_file = fopen(gitconfig_path, "a");
1347 if (gitconfig_file == NULL) {
1348 error = got_error_from_errno2("fopen", gitconfig_path);
1351 if (mirror_references) {
1352 if (asprintf(&gitconfig,
1355 "\tfetch = +refs/*:refs/*\n"
1356 "\tmirror = true\n",
1357 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 error = got_error_from_errno("asprintf");
1361 } else if (fetch_all_branches) {
1362 if (asprintf(&gitconfig,
1365 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 error = got_error_from_errno("asprintf");
1372 const char *branchname;
1375 * If the server specified a default branch, use just that one.
1376 * Otherwise fall back to fetching all branches on next fetch.
1379 branchname = got_ref_get_symref_target(head_symref);
1380 if (strncmp(branchname, "refs/heads/", 11) == 0)
1383 branchname = "*"; /* fall back to all branches */
1384 if (asprintf(&gitconfig,
1387 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 branchname) == -1) {
1391 error = got_error_from_errno("asprintf");
1395 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 if (n != strlen(gitconfig)) {
1397 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1402 printf("Created %s repository '%s'\n",
1403 mirror_references ? "mirrored" : "cloned", repo_path);
1406 if (kill(fetchpid, SIGTERM) == -1)
1407 error = got_error_from_errno("kill");
1408 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 error = got_error_from_errno("waitpid");
1411 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 error = got_error_from_errno("close");
1413 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 error = got_error_from_errno("fclose");
1416 got_repo_close(repo);
1418 got_ref_close(head_symref);
1419 TAILQ_FOREACH(pe, &refs, entry) {
1420 free((void *)pe->path);
1423 got_pathlist_free(&refs);
1424 TAILQ_FOREACH(pe, &symrefs, entry) {
1425 free((void *)pe->path);
1428 got_pathlist_free(&symrefs);
1429 got_pathlist_free(&wanted_branches);
1430 got_pathlist_free(&wanted_refs);
1437 free(default_destdir);
1438 free(gitconfig_path);
1443 static const struct got_error *
1444 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 int replace_tags, int verbosity, struct got_repository *repo)
1447 const struct got_error *err = NULL;
1448 char *new_id_str = NULL;
1449 struct got_object_id *old_id = NULL;
1451 err = got_object_id_str(&new_id_str, new_id);
1455 if (!replace_tags &&
1456 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 err = got_ref_resolve(&old_id, repo, ref);
1460 if (got_object_id_cmp(old_id, new_id) == 0)
1462 if (verbosity >= 0) {
1463 printf("Rejecting update of existing tag %s: %s\n",
1464 got_ref_get_name(ref), new_id_str);
1469 if (got_ref_is_symbolic(ref)) {
1470 if (verbosity >= 0) {
1471 printf("Replacing reference %s: %s\n",
1472 got_ref_get_name(ref),
1473 got_ref_get_symref_target(ref));
1475 err = got_ref_change_symref_to_ref(ref, new_id);
1478 err = got_ref_write(ref, repo);
1482 err = got_ref_resolve(&old_id, repo, ref);
1485 if (got_object_id_cmp(old_id, new_id) == 0)
1488 err = got_ref_change_ref(ref, new_id);
1491 err = got_ref_write(ref, repo);
1497 printf("Updated %s: %s\n", got_ref_get_name(ref),
1505 static const struct got_error *
1506 update_symref(const char *refname, struct got_reference *target_ref,
1507 int verbosity, struct got_repository *repo)
1509 const struct got_error *err = NULL, *unlock_err;
1510 struct got_reference *symref;
1511 int symref_is_locked = 0;
1513 err = got_ref_open(&symref, repo, refname, 1);
1515 if (err->code != GOT_ERR_NOT_REF)
1517 err = got_ref_alloc_symref(&symref, refname, target_ref);
1521 err = got_ref_write(symref, repo);
1526 printf("Created reference %s: %s\n",
1527 got_ref_get_name(symref),
1528 got_ref_get_symref_target(symref));
1530 symref_is_locked = 1;
1532 if (strcmp(got_ref_get_symref_target(symref),
1533 got_ref_get_name(target_ref)) == 0)
1536 err = got_ref_change_symref(symref,
1537 got_ref_get_name(target_ref));
1541 err = got_ref_write(symref, repo);
1546 printf("Updated %s: %s\n", got_ref_get_name(symref),
1547 got_ref_get_symref_target(symref));
1551 if (symref_is_locked) {
1552 unlock_err = got_ref_unlock(symref);
1553 if (unlock_err && err == NULL)
1556 got_ref_close(symref);
1563 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1564 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1565 "[remote-repository-name]\n",
1570 static const struct got_error *
1571 delete_missing_ref(struct got_reference *ref,
1572 int verbosity, struct got_repository *repo)
1574 const struct got_error *err = NULL;
1575 struct got_object_id *id = NULL;
1576 char *id_str = NULL;
1578 if (got_ref_is_symbolic(ref)) {
1579 err = got_ref_delete(ref, repo);
1582 if (verbosity >= 0) {
1583 printf("Deleted reference %s: %s\n",
1584 got_ref_get_name(ref),
1585 got_ref_get_symref_target(ref));
1588 err = got_ref_resolve(&id, repo, ref);
1591 err = got_object_id_str(&id_str, id);
1595 err = got_ref_delete(ref, repo);
1598 if (verbosity >= 0) {
1599 printf("Deleted reference %s: %s\n",
1600 got_ref_get_name(ref), id_str);
1609 static const struct got_error *
1610 delete_missing_refs(struct got_pathlist_head *their_refs,
1611 struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1612 int verbosity, struct got_repository *repo)
1614 const struct got_error *err = NULL, *unlock_err;
1615 struct got_reflist_head my_refs;
1616 struct got_reflist_entry *re;
1617 struct got_pathlist_entry *pe;
1618 char *remote_namespace = NULL;
1619 char *local_refname = NULL;
1621 SIMPLEQ_INIT(&my_refs);
1623 if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1625 return got_error_from_errno("asprintf");
1627 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1631 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1632 const char *refname = got_ref_get_name(re->ref);
1634 if (!remote->mirror_references) {
1635 if (strncmp(refname, remote_namespace,
1636 strlen(remote_namespace)) == 0) {
1637 if (strcmp(refname + strlen(remote_namespace),
1640 if (asprintf(&local_refname, "refs/heads/%s",
1641 refname + strlen(remote_namespace)) == -1) {
1642 err = got_error_from_errno("asprintf");
1645 } else if (strncmp(refname, "refs/tags/", 10) != 0)
1649 TAILQ_FOREACH(pe, their_refs, entry) {
1650 if (strcmp(local_refname, pe->path) == 0)
1656 TAILQ_FOREACH(pe, their_symrefs, entry) {
1657 if (strcmp(local_refname, pe->path) == 0)
1663 err = delete_missing_ref(re->ref, verbosity, repo);
1667 if (local_refname) {
1668 struct got_reference *ref;
1669 err = got_ref_open(&ref, repo, local_refname, 1);
1671 if (err->code != GOT_ERR_NOT_REF)
1673 free(local_refname);
1674 local_refname = NULL;
1677 err = delete_missing_ref(ref, verbosity, repo);
1680 unlock_err = got_ref_unlock(ref);
1682 if (unlock_err && err == NULL) {
1687 free(local_refname);
1688 local_refname = NULL;
1692 free(remote_namespace);
1693 free(local_refname);
1697 static const struct got_error *
1698 update_wanted_ref(const char *refname, struct got_object_id *id,
1699 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1701 const struct got_error *err, *unlock_err;
1702 char *remote_refname;
1703 struct got_reference *ref;
1705 if (strncmp("refs/", refname, 5) == 0)
1708 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1709 remote_repo_name, refname) == -1)
1710 return got_error_from_errno("asprintf");
1712 err = got_ref_open(&ref, repo, remote_refname, 1);
1714 if (err->code != GOT_ERR_NOT_REF)
1716 err = create_ref(remote_refname, id, verbosity, repo);
1718 err = update_ref(ref, id, 0, verbosity, repo);
1719 unlock_err = got_ref_unlock(ref);
1720 if (unlock_err && err == NULL)
1725 free(remote_refname);
1729 static const struct got_error *
1730 cmd_fetch(int argc, char *argv[])
1732 const struct got_error *error = NULL, *unlock_err;
1733 char *cwd = NULL, *repo_path = NULL;
1734 const char *remote_name;
1735 char *proto = NULL, *host = NULL, *port = NULL;
1736 char *repo_name = NULL, *server_path = NULL;
1737 struct got_remote_repo *remotes, *remote = NULL;
1739 char *id_str = NULL;
1740 struct got_repository *repo = NULL;
1741 struct got_worktree *worktree = NULL;
1742 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1743 struct got_pathlist_entry *pe;
1744 struct got_object_id *pack_hash = NULL;
1745 int i, ch, fetchfd = -1, fetchstatus;
1746 pid_t fetchpid = -1;
1747 struct got_fetch_progress_arg fpa;
1748 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1749 int delete_refs = 0, replace_tags = 0;
1752 TAILQ_INIT(&symrefs);
1753 TAILQ_INIT(&wanted_branches);
1754 TAILQ_INIT(&wanted_refs);
1756 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1759 fetch_all_branches = 1;
1762 error = got_pathlist_append(&wanted_branches,
1774 repo_path = realpath(optarg, NULL);
1775 if (repo_path == NULL)
1776 return got_error_from_errno2("realpath",
1778 got_path_strip_trailing_slashes(repo_path);
1786 else if (verbosity < 3)
1793 error = got_pathlist_append(&wanted_refs,
1806 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1807 errx(1, "-a and -b options are mutually exclusive");
1808 if (list_refs_only) {
1809 if (!TAILQ_EMPTY(&wanted_branches))
1810 errx(1, "-l and -b options are mutually exclusive");
1811 if (fetch_all_branches)
1812 errx(1, "-l and -a options are mutually exclusive");
1814 errx(1, "-l and -d options are mutually exclusive");
1815 if (verbosity == -1)
1816 errx(1, "-l and -q options are mutually exclusive");
1820 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1822 remote_name = argv[0];
1826 cwd = getcwd(NULL, 0);
1828 error = got_error_from_errno("getcwd");
1832 if (repo_path == NULL) {
1833 error = got_worktree_open(&worktree, cwd);
1834 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1840 strdup(got_worktree_get_repo_path(worktree));
1841 if (repo_path == NULL)
1842 error = got_error_from_errno("strdup");
1846 repo_path = strdup(cwd);
1847 if (repo_path == NULL) {
1848 error = got_error_from_errno("strdup");
1854 error = got_repo_open(&repo, repo_path, NULL);
1858 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1859 for (i = 0; i < nremotes; i++) {
1860 remote = &remotes[i];
1861 if (strcmp(remote->name, remote_name) == 0)
1864 if (i == nremotes) {
1865 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1869 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1870 &repo_name, remote->url);
1874 if (strcmp(proto, "git") == 0) {
1876 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1877 "sendfd dns inet unveil", NULL) == -1)
1880 } else if (strcmp(proto, "git+ssh") == 0 ||
1881 strcmp(proto, "ssh") == 0) {
1883 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1884 "sendfd unveil", NULL) == -1)
1887 } else if (strcmp(proto, "http") == 0 ||
1888 strcmp(proto, "git+http") == 0) {
1889 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1892 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1896 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1897 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1898 error = got_error_from_errno2("unveil",
1899 GOT_FETCH_PATH_SSH);
1903 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1908 printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1909 port ? ":" : "", port ? port : "");
1911 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1912 server_path, verbosity);
1916 fpa.last_scaled_size[0] = '\0';
1917 fpa.last_p_indexed = -1;
1918 fpa.last_p_resolved = -1;
1919 fpa.verbosity = verbosity;
1920 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1921 remote->mirror_references, fetch_all_branches, &wanted_branches,
1922 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1923 fetch_progress, &fpa);
1927 if (list_refs_only) {
1928 error = list_remote_refs(&symrefs, &refs);
1932 if (pack_hash == NULL) {
1934 printf("Already up-to-date\n");
1935 } else if (verbosity >= 0) {
1936 error = got_object_id_str(&id_str, pack_hash);
1939 printf("\nFetched %s.pack\n", id_str);
1944 /* Update references provided with the pack file. */
1945 TAILQ_FOREACH(pe, &refs, entry) {
1946 const char *refname = pe->path;
1947 struct got_object_id *id = pe->data;
1948 struct got_reference *ref;
1949 char *remote_refname;
1951 if (is_wanted_ref(&wanted_refs, refname) &&
1952 !remote->mirror_references) {
1953 error = update_wanted_ref(refname, id,
1954 remote->name, verbosity, repo);
1960 if (remote->mirror_references ||
1961 strncmp("refs/tags/", refname, 10) == 0) {
1962 error = got_ref_open(&ref, repo, refname, 1);
1964 if (error->code != GOT_ERR_NOT_REF)
1966 error = create_ref(refname, id, verbosity,
1971 error = update_ref(ref, id, replace_tags,
1973 unlock_err = got_ref_unlock(ref);
1974 if (unlock_err && error == NULL)
1980 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1981 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1982 remote_name, refname + 11) == -1) {
1983 error = got_error_from_errno("asprintf");
1987 error = got_ref_open(&ref, repo, remote_refname, 1);
1989 if (error->code != GOT_ERR_NOT_REF)
1991 error = create_ref(remote_refname, id,
1996 error = update_ref(ref, id, replace_tags,
1998 unlock_err = got_ref_unlock(ref);
1999 if (unlock_err && error == NULL)
2006 /* Also create a local branch if none exists yet. */
2007 error = got_ref_open(&ref, repo, refname, 1);
2009 if (error->code != GOT_ERR_NOT_REF)
2011 error = create_ref(refname, id, verbosity,
2016 unlock_err = got_ref_unlock(ref);
2017 if (unlock_err && error == NULL)
2024 error = delete_missing_refs(&refs, &symrefs, remote,
2030 if (!remote->mirror_references) {
2031 /* Update remote HEAD reference if the server provided one. */
2032 TAILQ_FOREACH(pe, &symrefs, entry) {
2033 struct got_reference *target_ref;
2034 const char *refname = pe->path;
2035 const char *target = pe->data;
2036 char *remote_refname = NULL, *remote_target = NULL;
2038 if (strcmp(refname, GOT_REF_HEAD) != 0)
2041 if (strncmp("refs/heads/", target, 11) != 0)
2044 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2045 remote->name, refname) == -1) {
2046 error = got_error_from_errno("asprintf");
2049 if (asprintf(&remote_target, "refs/remotes/%s/%s",
2050 remote->name, target + 11) == -1) {
2051 error = got_error_from_errno("asprintf");
2052 free(remote_refname);
2056 error = got_ref_open(&target_ref, repo, remote_target,
2059 free(remote_refname);
2060 free(remote_target);
2061 if (error->code == GOT_ERR_NOT_REF) {
2067 error = update_symref(remote_refname, target_ref,
2069 free(remote_refname);
2070 free(remote_target);
2071 got_ref_close(target_ref);
2078 if (kill(fetchpid, SIGTERM) == -1)
2079 error = got_error_from_errno("kill");
2080 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2081 error = got_error_from_errno("waitpid");
2083 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2084 error = got_error_from_errno("close");
2086 got_repo_close(repo);
2088 got_worktree_close(worktree);
2089 TAILQ_FOREACH(pe, &refs, entry) {
2090 free((void *)pe->path);
2093 got_pathlist_free(&refs);
2094 TAILQ_FOREACH(pe, &symrefs, entry) {
2095 free((void *)pe->path);
2098 got_pathlist_free(&symrefs);
2099 got_pathlist_free(&wanted_branches);
2100 got_pathlist_free(&wanted_refs);
2115 usage_checkout(void)
2117 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2118 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2123 show_worktree_base_ref_warning(void)
2125 fprintf(stderr, "%s: warning: could not create a reference "
2126 "to the work tree's base commit; the commit could be "
2127 "garbage-collected by Git; making the repository "
2128 "writable and running 'got update' will prevent this\n",
2132 struct got_checkout_progress_arg {
2133 const char *worktree_path;
2134 int had_base_commit_ref_error;
2137 static const struct got_error *
2138 checkout_progress(void *arg, unsigned char status, const char *path)
2140 struct got_checkout_progress_arg *a = arg;
2142 /* Base commit bump happens silently. */
2143 if (status == GOT_STATUS_BUMP_BASE)
2146 if (status == GOT_STATUS_BASE_REF_ERR) {
2147 a->had_base_commit_ref_error = 1;
2151 while (path[0] == '/')
2154 printf("%c %s/%s\n", status, a->worktree_path, path);
2158 static const struct got_error *
2159 check_cancelled(void *arg)
2161 if (sigint_received || sigpipe_received)
2162 return got_error(GOT_ERR_CANCELLED);
2166 static const struct got_error *
2167 check_linear_ancestry(struct got_object_id *commit_id,
2168 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2172 struct got_object_id *yca_id;
2174 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2175 commit_id, base_commit_id, repo, check_cancelled, NULL);
2180 return got_error(GOT_ERR_ANCESTRY);
2183 * Require a straight line of history between the target commit
2184 * and the work tree's base commit.
2186 * Non-linear situations such as this require a rebase:
2188 * (commit) D F (base_commit)
2196 * 'got update' only handles linear cases:
2197 * Update forwards in time: A (base/yca) - B - C - D (commit)
2198 * Update backwards in time: D (base) - C - B - A (commit/yca)
2200 if (allow_forwards_in_time_only) {
2201 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2202 return got_error(GOT_ERR_ANCESTRY);
2203 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2204 got_object_id_cmp(base_commit_id, yca_id) != 0)
2205 return got_error(GOT_ERR_ANCESTRY);
2211 static const struct got_error *
2212 check_same_branch(struct got_object_id *commit_id,
2213 struct got_reference *head_ref, struct got_object_id *yca_id,
2214 struct got_repository *repo)
2216 const struct got_error *err = NULL;
2217 struct got_commit_graph *graph = NULL;
2218 struct got_object_id *head_commit_id = NULL;
2219 int is_same_branch = 0;
2221 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2225 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2229 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2234 err = got_commit_graph_open(&graph, "/", 1);
2238 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2239 check_cancelled, NULL);
2244 struct got_object_id *id;
2245 err = got_commit_graph_iter_next(&id, graph, repo,
2246 check_cancelled, NULL);
2248 if (err->code == GOT_ERR_ITER_COMPLETED)
2254 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2256 if (got_object_id_cmp(id, commit_id) == 0) {
2264 got_commit_graph_close(graph);
2265 free(head_commit_id);
2266 if (!err && !is_same_branch)
2267 err = got_error(GOT_ERR_ANCESTRY);
2271 static const struct got_error *
2272 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2274 static char msg[512];
2275 const char *branch_name;
2277 if (got_ref_is_symbolic(ref))
2278 branch_name = got_ref_get_symref_target(ref);
2280 branch_name = got_ref_get_name(ref);
2282 if (strncmp("refs/heads/", branch_name, 11) == 0)
2285 snprintf(msg, sizeof(msg),
2286 "target commit is not contained in branch '%s'; "
2287 "the branch to use must be specified with -b; "
2288 "if necessary a new branch can be created for "
2289 "this commit with 'got branch -c %s BRANCH_NAME'",
2290 branch_name, commit_id_str);
2292 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2295 static const struct got_error *
2296 cmd_checkout(int argc, char *argv[])
2298 const struct got_error *error = NULL;
2299 struct got_repository *repo = NULL;
2300 struct got_reference *head_ref = NULL;
2301 struct got_worktree *worktree = NULL;
2302 char *repo_path = NULL;
2303 char *worktree_path = NULL;
2304 const char *path_prefix = "";
2305 const char *branch_name = GOT_REF_HEAD;
2306 char *commit_id_str = NULL;
2307 int ch, same_path_prefix, allow_nonempty = 0;
2308 struct got_pathlist_head paths;
2309 struct got_checkout_progress_arg cpa;
2313 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2316 branch_name = optarg;
2319 commit_id_str = strdup(optarg);
2320 if (commit_id_str == NULL)
2321 return got_error_from_errno("strdup");
2327 path_prefix = optarg;
2339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2340 "unveil", NULL) == -1)
2344 char *cwd, *base, *dotgit;
2345 repo_path = realpath(argv[0], NULL);
2346 if (repo_path == NULL)
2347 return got_error_from_errno2("realpath", argv[0]);
2348 cwd = getcwd(NULL, 0);
2350 error = got_error_from_errno("getcwd");
2353 if (path_prefix[0]) {
2354 base = basename(path_prefix);
2356 error = got_error_from_errno2("basename",
2361 base = basename(repo_path);
2363 error = got_error_from_errno2("basename",
2368 dotgit = strstr(base, ".git");
2371 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2372 error = got_error_from_errno("asprintf");
2377 } else if (argc == 2) {
2378 repo_path = realpath(argv[0], NULL);
2379 if (repo_path == NULL) {
2380 error = got_error_from_errno2("realpath", argv[0]);
2383 worktree_path = realpath(argv[1], NULL);
2384 if (worktree_path == NULL) {
2385 if (errno != ENOENT) {
2386 error = got_error_from_errno2("realpath",
2390 worktree_path = strdup(argv[1]);
2391 if (worktree_path == NULL) {
2392 error = got_error_from_errno("strdup");
2399 got_path_strip_trailing_slashes(repo_path);
2400 got_path_strip_trailing_slashes(worktree_path);
2402 error = got_repo_open(&repo, repo_path, NULL);
2406 /* Pre-create work tree path for unveil(2) */
2407 error = got_path_mkdir(worktree_path);
2409 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2410 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2412 if (!allow_nonempty &&
2413 !got_path_dir_is_empty(worktree_path)) {
2414 error = got_error_path(worktree_path,
2415 GOT_ERR_DIR_NOT_EMPTY);
2420 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2424 error = got_ref_open(&head_ref, repo, branch_name, 0);
2428 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2429 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2432 error = got_worktree_open(&worktree, worktree_path);
2436 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2440 if (!same_path_prefix) {
2441 error = got_error(GOT_ERR_PATH_PREFIX);
2445 if (commit_id_str) {
2446 struct got_object_id *commit_id;
2447 error = got_repo_match_object_id(&commit_id, NULL,
2448 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2451 error = check_linear_ancestry(commit_id,
2452 got_worktree_get_base_commit_id(worktree), 0, repo);
2453 if (error != NULL) {
2455 if (error->code == GOT_ERR_ANCESTRY) {
2456 error = checkout_ancestry_error(
2457 head_ref, commit_id_str);
2461 error = check_same_branch(commit_id, head_ref, NULL, repo);
2463 if (error->code == GOT_ERR_ANCESTRY) {
2464 error = checkout_ancestry_error(
2465 head_ref, commit_id_str);
2469 error = got_worktree_set_base_commit_id(worktree, repo,
2476 error = got_pathlist_append(&paths, "", NULL);
2479 cpa.worktree_path = worktree_path;
2480 cpa.had_base_commit_ref_error = 0;
2481 error = got_worktree_checkout_files(worktree, &paths, repo,
2482 checkout_progress, &cpa, check_cancelled, NULL);
2486 printf("Now shut up and hack\n");
2487 if (cpa.had_base_commit_ref_error)
2488 show_worktree_base_ref_warning();
2490 got_pathlist_free(&paths);
2491 free(commit_id_str);
2493 free(worktree_path);
2497 struct got_update_progress_arg {
2505 print_update_progress_stats(struct got_update_progress_arg *upa)
2507 if (!upa->did_something)
2510 if (upa->conflicts > 0)
2511 printf("Files with new merge conflicts: %d\n", upa->conflicts);
2512 if (upa->obstructed > 0)
2513 printf("File paths obstructed by a non-regular file: %d\n",
2515 if (upa->not_updated > 0)
2516 printf("Files not updated because of existing merge "
2517 "conflicts: %d\n", upa->not_updated);
2523 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2528 static const struct got_error *
2529 update_progress(void *arg, unsigned char status, const char *path)
2531 struct got_update_progress_arg *upa = arg;
2533 if (status == GOT_STATUS_EXISTS ||
2534 status == GOT_STATUS_BASE_REF_ERR)
2537 upa->did_something = 1;
2539 /* Base commit bump happens silently. */
2540 if (status == GOT_STATUS_BUMP_BASE)
2543 if (status == GOT_STATUS_CONFLICT)
2545 if (status == GOT_STATUS_OBSTRUCTED)
2547 if (status == GOT_STATUS_CANNOT_UPDATE)
2550 while (path[0] == '/')
2552 printf("%c %s\n", status, path);
2556 static const struct got_error *
2557 switch_head_ref(struct got_reference *head_ref,
2558 struct got_object_id *commit_id, struct got_worktree *worktree,
2559 struct got_repository *repo)
2561 const struct got_error *err = NULL;
2563 int ref_has_moved = 0;
2565 /* Trivial case: switching between two different references. */
2566 if (strcmp(got_ref_get_name(head_ref),
2567 got_worktree_get_head_ref_name(worktree)) != 0) {
2568 printf("Switching work tree from %s to %s\n",
2569 got_worktree_get_head_ref_name(worktree),
2570 got_ref_get_name(head_ref));
2571 return got_worktree_set_head_ref(worktree, head_ref);
2574 err = check_linear_ancestry(commit_id,
2575 got_worktree_get_base_commit_id(worktree), 0, repo);
2577 if (err->code != GOT_ERR_ANCESTRY)
2584 /* Switching to a rebased branch with the same reference name. */
2585 err = got_object_id_str(&base_id_str,
2586 got_worktree_get_base_commit_id(worktree));
2589 printf("Reference %s now points at a different branch\n",
2590 got_worktree_get_head_ref_name(worktree));
2591 printf("Switching work tree from %s to %s\n", base_id_str,
2592 got_worktree_get_head_ref_name(worktree));
2596 static const struct got_error *
2597 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2599 const struct got_error *err;
2602 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2606 return got_error(GOT_ERR_REBASING);
2608 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2612 return got_error(GOT_ERR_HISTEDIT_BUSY);
2617 static const struct got_error *
2618 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2619 char *argv[], struct got_worktree *worktree)
2621 const struct got_error *err = NULL;
2628 return got_error_from_errno("strdup");
2629 return got_pathlist_append(paths, path, NULL);
2632 for (i = 0; i < argc; i++) {
2633 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2636 err = got_pathlist_append(paths, path, NULL);
2646 static const struct got_error *
2647 wrap_not_worktree_error(const struct got_error *orig_err,
2648 const char *cmdname, const char *path)
2650 const struct got_error *err;
2651 struct got_repository *repo;
2652 static char msg[512];
2654 err = got_repo_open(&repo, path, NULL);
2658 snprintf(msg, sizeof(msg),
2659 "'got %s' needs a work tree in addition to a git repository\n"
2660 "Work trees can be checked out from this Git repository with "
2662 "The got(1) manual page contains more information.", cmdname);
2663 err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2664 got_repo_close(repo);
2668 static const struct got_error *
2669 cmd_update(int argc, char *argv[])
2671 const struct got_error *error = NULL;
2672 struct got_repository *repo = NULL;
2673 struct got_worktree *worktree = NULL;
2674 char *worktree_path = NULL;
2675 struct got_object_id *commit_id = NULL;
2676 char *commit_id_str = NULL;
2677 const char *branch_name = NULL;
2678 struct got_reference *head_ref = NULL;
2679 struct got_pathlist_head paths;
2680 struct got_pathlist_entry *pe;
2682 struct got_update_progress_arg upa;
2686 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2689 branch_name = optarg;
2692 commit_id_str = strdup(optarg);
2693 if (commit_id_str == NULL)
2694 return got_error_from_errno("strdup");
2706 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2707 "unveil", NULL) == -1)
2710 worktree_path = getcwd(NULL, 0);
2711 if (worktree_path == NULL) {
2712 error = got_error_from_errno("getcwd");
2715 error = got_worktree_open(&worktree, worktree_path);
2717 if (error->code == GOT_ERR_NOT_WORKTREE)
2718 error = wrap_not_worktree_error(error, "update",
2723 error = check_rebase_or_histedit_in_progress(worktree);
2727 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2732 error = apply_unveil(got_repo_get_path(repo), 0,
2733 got_worktree_get_root_path(worktree));
2737 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2741 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2742 got_worktree_get_head_ref_name(worktree), 0);
2745 if (commit_id_str == NULL) {
2746 error = got_ref_resolve(&commit_id, repo, head_ref);
2749 error = got_object_id_str(&commit_id_str, commit_id);
2753 error = got_repo_match_object_id(&commit_id, NULL,
2754 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2755 free(commit_id_str);
2756 commit_id_str = NULL;
2759 error = got_object_id_str(&commit_id_str, commit_id);
2765 struct got_object_id *head_commit_id;
2766 TAILQ_FOREACH(pe, &paths, entry) {
2767 if (pe->path_len == 0)
2769 error = got_error_msg(GOT_ERR_BAD_PATH,
2770 "switching between branches requires that "
2771 "the entire work tree gets updated");
2774 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2777 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2779 free(head_commit_id);
2782 error = check_same_branch(commit_id, head_ref, NULL, repo);
2785 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2789 error = check_linear_ancestry(commit_id,
2790 got_worktree_get_base_commit_id(worktree), 0, repo);
2791 if (error != NULL) {
2792 if (error->code == GOT_ERR_ANCESTRY)
2793 error = got_error(GOT_ERR_BRANCH_MOVED);
2796 error = check_same_branch(commit_id, head_ref, NULL, repo);
2801 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2803 error = got_worktree_set_base_commit_id(worktree, repo,
2809 memset(&upa, 0, sizeof(upa));
2810 error = got_worktree_checkout_files(worktree, &paths, repo,
2811 update_progress, &upa, check_cancelled, NULL);
2815 if (upa.did_something)
2816 printf("Updated to commit %s\n", commit_id_str);
2818 printf("Already up-to-date\n");
2819 print_update_progress_stats(&upa);
2821 free(worktree_path);
2822 TAILQ_FOREACH(pe, &paths, entry)
2823 free((char *)pe->path);
2824 got_pathlist_free(&paths);
2826 free(commit_id_str);
2830 static const struct got_error *
2831 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2832 const char *path, int diff_context, int ignore_whitespace,
2833 struct got_repository *repo)
2835 const struct got_error *err = NULL;
2836 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2839 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2844 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2848 while (path[0] == '/')
2850 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2851 ignore_whitespace, stdout);
2854 got_object_blob_close(blob1);
2855 got_object_blob_close(blob2);
2859 static const struct got_error *
2860 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2861 const char *path, int diff_context, int ignore_whitespace,
2862 struct got_repository *repo)
2864 const struct got_error *err = NULL;
2865 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2866 struct got_diff_blob_output_unidiff_arg arg;
2869 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2874 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2878 arg.diff_context = diff_context;
2879 arg.ignore_whitespace = ignore_whitespace;
2880 arg.outfile = stdout;
2881 while (path[0] == '/')
2883 err = got_diff_tree(tree1, tree2, path, path, repo,
2884 got_diff_blob_output_unidiff, &arg, 1);
2887 got_object_tree_close(tree1);
2889 got_object_tree_close(tree2);
2893 static const struct got_error *
2894 get_changed_paths(struct got_pathlist_head *paths,
2895 struct got_commit_object *commit, struct got_repository *repo)
2897 const struct got_error *err = NULL;
2898 struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2899 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2900 struct got_object_qid *qid;
2902 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2904 struct got_commit_object *pcommit;
2905 err = got_object_open_as_commit(&pcommit, repo,
2910 tree_id1 = got_object_commit_get_tree_id(pcommit);
2911 got_object_commit_close(pcommit);
2916 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2921 tree_id2 = got_object_commit_get_tree_id(commit);
2922 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2926 err = got_diff_tree(tree1, tree2, "", "", repo,
2927 got_diff_tree_collect_changed_paths, paths, 0);
2930 got_object_tree_close(tree1);
2932 got_object_tree_close(tree2);
2936 static const struct got_error *
2937 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2938 const char *path, int diff_context, struct got_repository *repo)
2940 const struct got_error *err = NULL;
2941 struct got_commit_object *pcommit = NULL;
2942 char *id_str1 = NULL, *id_str2 = NULL;
2943 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2944 struct got_object_qid *qid;
2946 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2948 err = got_object_open_as_commit(&pcommit, repo,
2954 if (path && path[0] != '\0') {
2956 err = got_object_id_by_path(&obj_id2, repo, id, path);
2959 err = got_object_id_str(&id_str2, obj_id2);
2965 err = got_object_id_by_path(&obj_id1, repo,
2968 if (err->code != GOT_ERR_NO_TREE_ENTRY) {
2973 err = got_object_id_str(&id_str1, obj_id1);
2980 err = got_object_get_type(&obj_type, repo, obj_id2);
2985 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2987 case GOT_OBJ_TYPE_BLOB:
2988 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2991 case GOT_OBJ_TYPE_TREE:
2992 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2996 err = got_error(GOT_ERR_OBJ_TYPE);
3002 obj_id2 = got_object_commit_get_tree_id(commit);
3003 err = got_object_id_str(&id_str2, obj_id2);
3006 obj_id1 = got_object_commit_get_tree_id(pcommit);
3007 err = got_object_id_str(&id_str1, obj_id1);
3010 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3011 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3017 got_object_commit_close(pcommit);
3022 get_datestr(time_t *time, char *datebuf)
3024 struct tm mytm, *tm;
3027 tm = gmtime_r(time, &mytm);
3030 s = asctime_r(tm, datebuf);
3033 p = strchr(s, '\n');
3039 static const struct got_error *
3040 match_logmsg(int *have_match, struct got_object_id *id,
3041 struct got_commit_object *commit, regex_t *regex)
3043 const struct got_error *err = NULL;
3044 regmatch_t regmatch;
3045 char *id_str = NULL, *logmsg = NULL;
3049 err = got_object_id_str(&id_str, id);
3053 err = got_object_commit_get_logmsg(&logmsg, commit);
3057 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
3066 match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3069 regmatch_t regmatch;
3070 struct got_pathlist_entry *pe;
3074 TAILQ_FOREACH(pe, changed_paths, entry) {
3075 if (regexec(regex, pe->path, 1, ®match, 0) == 0) {
3082 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3084 static const struct got_error *
3085 print_commit(struct got_commit_object *commit, struct got_object_id *id,
3086 struct got_repository *repo, const char *path,
3087 struct got_pathlist_head *changed_paths, int show_patch,
3088 int diff_context, struct got_reflist_head *refs)
3090 const struct got_error *err = NULL;
3091 char *id_str, *datestr, *logmsg0, *logmsg, *line;
3093 time_t committer_time;
3094 const char *author, *committer;
3095 char *refs_str = NULL;
3096 struct got_reflist_entry *re;
3098 SIMPLEQ_FOREACH(re, refs, entry) {
3101 struct got_tag_object *tag = NULL;
3104 name = got_ref_get_name(re->ref);
3105 if (strcmp(name, GOT_REF_HEAD) == 0)
3107 if (strncmp(name, "refs/", 5) == 0)
3109 if (strncmp(name, "got/", 4) == 0)
3111 if (strncmp(name, "heads/", 6) == 0)
3113 if (strncmp(name, "remotes/", 8) == 0) {
3115 s = strstr(name, "/" GOT_REF_HEAD);
3116 if (s != NULL && s[strlen(s)] == '\0')
3119 if (strncmp(name, "tags/", 5) == 0) {
3120 err = got_object_open_as_tag(&tag, repo, re->id);
3122 if (err->code != GOT_ERR_OBJ_TYPE)
3124 /* Ref points at something other than a tag. */
3129 cmp = got_object_id_cmp(tag ?
3130 got_object_tag_get_object_id(tag) : re->id, id);
3132 got_object_tag_close(tag);
3136 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3138 err = got_error_from_errno("asprintf");
3144 err = got_object_id_str(&id_str, id);
3148 printf(GOT_COMMIT_SEP_STR);
3149 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3150 refs_str ? refs_str : "", refs_str ? ")" : "");
3155 printf("from: %s\n", got_object_commit_get_author(commit));
3156 committer_time = got_object_commit_get_committer_time(commit);
3157 datestr = get_datestr(&committer_time, datebuf);
3159 printf("date: %s UTC\n", datestr);
3160 author = got_object_commit_get_author(commit);
3161 committer = got_object_commit_get_committer(commit);
3162 if (strcmp(author, committer) != 0)
3163 printf("via: %s\n", committer);
3164 if (got_object_commit_get_nparents(commit) > 1) {
3165 const struct got_object_id_queue *parent_ids;
3166 struct got_object_qid *qid;
3168 parent_ids = got_object_commit_get_parent_ids(commit);
3169 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3170 err = got_object_id_str(&id_str, qid->id);
3173 printf("parent %d: %s\n", n++, id_str);
3178 err = got_object_commit_get_logmsg(&logmsg0, commit);
3184 line = strsep(&logmsg, "\n");
3186 printf(" %s\n", line);
3190 if (changed_paths) {
3191 struct got_pathlist_entry *pe;
3192 TAILQ_FOREACH(pe, changed_paths, entry) {
3193 struct got_diff_changed_path *cp = pe->data;
3194 printf(" %c %s\n", cp->status, pe->path);
3199 err = print_patch(commit, id, path, diff_context, repo);
3204 if (fflush(stdout) != 0 && err == NULL)
3205 err = got_error_from_errno("fflush");
3209 static const struct got_error *
3210 print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3211 struct got_repository *repo, const char *path, int show_changed_paths,
3212 int show_patch, const char *search_pattern, int diff_context, int limit,
3213 int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3215 const struct got_error *err;
3216 struct got_commit_graph *graph;
3219 struct got_object_id_queue reversed_commits;
3220 struct got_object_qid *qid;
3221 struct got_commit_object *commit;
3222 struct got_pathlist_head changed_paths;
3223 struct got_pathlist_entry *pe;
3225 SIMPLEQ_INIT(&reversed_commits);
3226 TAILQ_INIT(&changed_paths);
3228 if (search_pattern && regcomp(®ex, search_pattern,
3229 REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3230 return got_error_msg(GOT_ERR_REGEX, search_pattern);
3232 err = got_commit_graph_open(&graph, path, !log_branches);
3235 err = got_commit_graph_iter_start(graph, root_id, repo,
3236 check_cancelled, NULL);
3240 struct got_object_id *id;
3242 if (sigint_received || sigpipe_received)
3245 err = got_commit_graph_iter_next(&id, graph, repo,
3246 check_cancelled, NULL);
3248 if (err->code == GOT_ERR_ITER_COMPLETED)
3255 err = got_object_open_as_commit(&commit, repo, id);
3259 if (show_changed_paths) {
3260 err = get_changed_paths(&changed_paths, commit, repo);
3265 if (search_pattern) {
3266 err = match_logmsg(&have_match, id, commit, ®ex);
3268 got_object_commit_close(commit);
3271 if (have_match == 0 && show_changed_paths)
3272 match_changed_paths(&have_match,
3273 &changed_paths, ®ex);
3274 if (have_match == 0) {
3275 got_object_commit_close(commit);
3276 TAILQ_FOREACH(pe, &changed_paths, entry) {
3277 free((char *)pe->path);
3280 got_pathlist_free(&changed_paths);
3285 if (reverse_display_order) {
3286 err = got_object_qid_alloc(&qid, id);
3289 SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3290 got_object_commit_close(commit);
3292 err = print_commit(commit, id, repo, path,
3293 show_changed_paths ? &changed_paths : NULL,
3294 show_patch, diff_context, refs);
3295 got_object_commit_close(commit);
3299 if ((limit && --limit == 0) ||
3300 (end_id && got_object_id_cmp(id, end_id) == 0))
3303 TAILQ_FOREACH(pe, &changed_paths, entry) {
3304 free((char *)pe->path);
3307 got_pathlist_free(&changed_paths);
3309 if (reverse_display_order) {
3310 SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3311 err = got_object_open_as_commit(&commit, repo, qid->id);
3314 err = print_commit(commit, qid->id, repo, path,
3315 show_changed_paths ? &changed_paths : NULL,
3316 show_patch, diff_context, refs);
3317 got_object_commit_close(commit);
3323 while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3324 qid = SIMPLEQ_FIRST(&reversed_commits);
3325 SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3326 got_object_qid_free(qid);
3328 TAILQ_FOREACH(pe, &changed_paths, entry) {
3329 free((char *)pe->path);
3332 got_pathlist_free(&changed_paths);
3335 got_commit_graph_close(graph);
3342 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3343 "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3344 "[-R] [path]\n", getprogname());
3349 get_default_log_limit(void)
3351 const char *got_default_log_limit;
3355 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3356 if (got_default_log_limit == NULL)
3358 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3364 static const struct got_error *
3365 resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3366 struct got_repository *repo)
3368 const struct got_error *err = NULL;
3369 struct got_reference *ref;
3373 err = got_ref_open(&ref, repo, commit_arg, 0);
3376 err = got_ref_resolve(id, repo, ref);
3380 err = got_object_get_type(&obj_type, repo, *id);
3383 if (obj_type == GOT_OBJ_TYPE_TAG) {
3384 struct got_tag_object *tag;
3385 err = got_object_open_as_tag(&tag, repo, *id);
3388 if (got_object_tag_get_object_type(tag) !=
3389 GOT_OBJ_TYPE_COMMIT) {
3390 got_object_tag_close(tag);
3391 return got_error(GOT_ERR_OBJ_TYPE);
3394 *id = got_object_id_dup(
3395 got_object_tag_get_object_id(tag));
3397 err = got_error_from_errno(
3398 "got_object_id_dup");
3399 got_object_tag_close(tag);
3402 } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3403 return got_error(GOT_ERR_OBJ_TYPE);
3405 err = got_repo_match_object_id_prefix(id, commit_arg,
3406 GOT_OBJ_TYPE_COMMIT, repo);
3412 static const struct got_error *
3413 cmd_log(int argc, char *argv[])
3415 const struct got_error *error;
3416 struct got_repository *repo = NULL;
3417 struct got_worktree *worktree = NULL;
3418 struct got_object_id *start_id = NULL, *end_id = NULL;
3419 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3420 const char *start_commit = NULL, *end_commit = NULL;
3421 const char *search_pattern = NULL;
3422 int diff_context = -1, ch;
3423 int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3424 int reverse_display_order = 0;
3426 struct got_reflist_head refs;
3428 SIMPLEQ_INIT(&refs);
3431 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3437 limit = get_default_log_limit();
3439 while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3445 show_changed_paths = 1;
3448 start_commit = optarg;
3451 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3454 err(1, "-C option %s", errstr);
3457 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3459 err(1, "-l option %s", errstr);
3465 repo_path = realpath(optarg, NULL);
3466 if (repo_path == NULL)
3467 return got_error_from_errno2("realpath",
3469 got_path_strip_trailing_slashes(repo_path);
3472 reverse_display_order = 1;
3475 search_pattern = optarg;
3478 end_commit = optarg;
3489 if (diff_context == -1)
3491 else if (!show_patch)
3492 errx(1, "-C reguires -p");
3494 cwd = getcwd(NULL, 0);
3496 error = got_error_from_errno("getcwd");
3500 if (repo_path == NULL) {
3501 error = got_worktree_open(&worktree, cwd);
3502 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3510 error = got_error_from_errno("strdup");
3513 } else if (argc == 1) {
3515 error = got_worktree_resolve_path(&path, worktree,
3520 path = strdup(argv[0]);
3522 error = got_error_from_errno("strdup");
3529 if (repo_path == NULL) {
3530 repo_path = worktree ?
3531 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3533 if (repo_path == NULL) {
3534 error = got_error_from_errno("strdup");
3538 error = got_repo_open(&repo, repo_path, NULL);
3542 error = apply_unveil(got_repo_get_path(repo), 1,
3543 worktree ? got_worktree_get_root_path(worktree) : NULL);
3547 if (start_commit == NULL) {
3548 struct got_reference *head_ref;
3549 struct got_commit_object *commit = NULL;
3550 error = got_ref_open(&head_ref, repo,
3551 worktree ? got_worktree_get_head_ref_name(worktree)
3555 error = got_ref_resolve(&start_id, repo, head_ref);
3556 got_ref_close(head_ref);
3559 error = got_object_open_as_commit(&commit, repo,
3563 got_object_commit_close(commit);
3565 error = resolve_commit_arg(&start_id, start_commit, repo);
3569 if (end_commit != NULL) {
3570 error = resolve_commit_arg(&end_id, end_commit, repo);
3576 const char *prefix = got_worktree_get_path_prefix(worktree);
3578 if (asprintf(&p, "%s%s%s", prefix,
3579 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3580 error = got_error_from_errno("asprintf");
3583 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3586 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3591 path = in_repo_path;
3594 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3598 error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3599 show_patch, search_pattern, diff_context, limit, log_branches,
3600 reverse_display_order, &refs);
3606 got_worktree_close(worktree);
3608 const struct got_error *repo_error;
3609 repo_error = got_repo_close(repo);
3613 got_ref_list_free(&refs);
3620 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3621 "[-w] [object1 object2 | path]\n", getprogname());
3625 struct print_diff_arg {
3626 struct got_repository *repo;
3627 struct got_worktree *worktree;
3632 int ignore_whitespace;
3635 static const struct got_error *
3636 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3637 const char *path, struct got_object_id *blob_id,
3638 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3639 int dirfd, const char *de_name)
3641 struct print_diff_arg *a = arg;
3642 const struct got_error *err = NULL;
3643 struct got_blob_object *blob1 = NULL;
3646 char *abspath = NULL, *label1 = NULL;
3649 if (a->diff_staged) {
3650 if (staged_status != GOT_STATUS_MODIFY &&
3651 staged_status != GOT_STATUS_ADD &&
3652 staged_status != GOT_STATUS_DELETE)
3655 if (staged_status == GOT_STATUS_DELETE)
3657 if (status == GOT_STATUS_NONEXISTENT)
3658 return got_error_set_errno(ENOENT, path);
3659 if (status != GOT_STATUS_MODIFY &&
3660 status != GOT_STATUS_ADD &&
3661 status != GOT_STATUS_DELETE &&
3662 status != GOT_STATUS_CONFLICT)
3666 if (!a->header_shown) {
3667 printf("diff %s %s%s\n", a->id_str,
3668 got_worktree_get_root_path(a->worktree),
3669 a->diff_staged ? " (staged changes)" : "");
3670 a->header_shown = 1;
3673 if (a->diff_staged) {
3674 const char *label1 = NULL, *label2 = NULL;
3675 switch (staged_status) {
3676 case GOT_STATUS_MODIFY:
3680 case GOT_STATUS_ADD:
3683 case GOT_STATUS_DELETE:
3687 return got_error(GOT_ERR_FILE_STATUS);
3689 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3690 label1, label2, a->diff_context, a->ignore_whitespace,
3694 if (staged_status == GOT_STATUS_ADD ||
3695 staged_status == GOT_STATUS_MODIFY) {
3697 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3701 err = got_object_id_str(&id_str, staged_blob_id);
3704 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3705 err = got_error_from_errno("asprintf");
3710 } else if (status != GOT_STATUS_ADD) {
3711 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3716 if (status != GOT_STATUS_DELETE) {
3717 if (asprintf(&abspath, "%s/%s",
3718 got_worktree_get_root_path(a->worktree), path) == -1) {
3719 err = got_error_from_errno("asprintf");
3724 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3726 err = got_error_from_errno2("openat", abspath);
3730 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3732 err = got_error_from_errno2("open", abspath);
3736 if (fstat(fd, &sb) == -1) {
3737 err = got_error_from_errno2("fstat", abspath);
3740 f2 = fdopen(fd, "r");
3742 err = got_error_from_errno2("fdopen", abspath);
3749 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3750 a->diff_context, a->ignore_whitespace, stdout);
3753 got_object_blob_close(blob1);
3754 if (f2 && fclose(f2) == EOF && err == NULL)
3755 err = got_error_from_errno("fclose");
3756 if (fd != -1 && close(fd) == -1 && err == NULL)
3757 err = got_error_from_errno("close");
3762 static const struct got_error *
3763 cmd_diff(int argc, char *argv[])
3765 const struct got_error *error;
3766 struct got_repository *repo = NULL;
3767 struct got_worktree *worktree = NULL;
3768 char *cwd = NULL, *repo_path = NULL;
3769 struct got_object_id *id1 = NULL, *id2 = NULL;
3770 const char *id_str1 = NULL, *id_str2 = NULL;
3771 char *label1 = NULL, *label2 = NULL;
3773 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3783 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3786 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3789 err(1, "-C option %s", errstr);
3792 repo_path = realpath(optarg, NULL);
3793 if (repo_path == NULL)
3794 return got_error_from_errno2("realpath",
3796 got_path_strip_trailing_slashes(repo_path);
3802 ignore_whitespace = 1;
3813 cwd = getcwd(NULL, 0);
3815 error = got_error_from_errno("getcwd");
3821 "-r option can't be used when diffing a work tree");
3822 error = got_worktree_open(&worktree, cwd);
3824 if (error->code == GOT_ERR_NOT_WORKTREE)
3825 error = wrap_not_worktree_error(error, "diff",
3829 repo_path = strdup(got_worktree_get_repo_path(worktree));
3830 if (repo_path == NULL) {
3831 error = got_error_from_errno("strdup");
3835 error = got_worktree_resolve_path(&path, worktree,
3842 error = got_error_from_errno("strdup");
3846 } else if (argc == 2) {
3848 errx(1, "-s option can't be used when diffing "
3849 "objects in repository");
3852 if (repo_path == NULL) {
3853 error = got_worktree_open(&worktree, cwd);
3854 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3858 got_worktree_get_repo_path(worktree));
3859 if (repo_path == NULL) {
3860 error = got_error_from_errno("strdup");
3864 repo_path = strdup(cwd);
3865 if (repo_path == NULL) {
3866 error = got_error_from_errno("strdup");
3874 error = got_repo_open(&repo, repo_path, NULL);
3879 error = apply_unveil(got_repo_get_path(repo), 1,
3880 worktree ? got_worktree_get_root_path(worktree) : NULL);
3885 struct print_diff_arg arg;
3886 struct got_pathlist_head paths;
3891 error = got_object_id_str(&id_str,
3892 got_worktree_get_base_commit_id(worktree));
3896 arg.worktree = worktree;
3897 arg.diff_context = diff_context;
3898 arg.id_str = id_str;
3899 arg.header_shown = 0;
3900 arg.diff_staged = diff_staged;
3901 arg.ignore_whitespace = ignore_whitespace;
3903 error = got_pathlist_append(&paths, path, NULL);
3907 error = got_worktree_status(worktree, &paths, repo, print_diff,
3908 &arg, check_cancelled, NULL);
3910 got_pathlist_free(&paths);
3914 error = got_repo_match_object_id(&id1, &label1, id_str1,
3915 GOT_OBJ_TYPE_ANY, 1, repo);
3919 error = got_repo_match_object_id(&id2, &label2, id_str2,
3920 GOT_OBJ_TYPE_ANY, 1, repo);
3924 error = got_object_get_type(&type1, repo, id1);
3928 error = got_object_get_type(&type2, repo, id2);
3932 if (type1 != type2) {
3933 error = got_error(GOT_ERR_OBJ_TYPE);
3938 case GOT_OBJ_TYPE_BLOB:
3939 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3940 diff_context, ignore_whitespace, repo, stdout);
3942 case GOT_OBJ_TYPE_TREE:
3943 error = got_diff_objects_as_trees(id1, id2, "", "",
3944 diff_context, ignore_whitespace, repo, stdout);
3946 case GOT_OBJ_TYPE_COMMIT:
3947 printf("diff %s %s\n", label1, label2);
3948 error = got_diff_objects_as_commits(id1, id2, diff_context,
3949 ignore_whitespace, repo, stdout);
3952 error = got_error(GOT_ERR_OBJ_TYPE);
3961 got_worktree_close(worktree);
3963 const struct got_error *repo_error;
3964 repo_error = got_repo_close(repo);
3975 "usage: %s blame [-c commit] [-r repository-path] path\n",
3984 char datebuf[11]; /* YYYY-MM-DD + NUL */
3987 struct blame_cb_args {
3988 struct blame_line *lines;
3992 off_t *line_offsets;
3994 struct got_repository *repo;
3997 static const struct got_error *
3998 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4000 const struct got_error *err = NULL;
4001 struct blame_cb_args *a = arg;
4002 struct blame_line *bline;
4004 size_t linesize = 0;
4005 struct got_commit_object *commit = NULL;
4008 time_t committer_time;
4010 if (nlines != a->nlines ||
4011 (lineno != -1 && lineno < 1) || lineno > a->nlines)
4012 return got_error(GOT_ERR_RANGE);
4014 if (sigint_received)
4015 return got_error(GOT_ERR_ITER_COMPLETED);
4018 return NULL; /* no change in this commit */
4020 /* Annotate this line. */
4021 bline = &a->lines[lineno - 1];
4022 if (bline->annotated)
4024 err = got_object_id_str(&bline->id_str, id);
4028 err = got_object_open_as_commit(&commit, a->repo, id);
4032 bline->committer = strdup(got_object_commit_get_committer(commit));
4033 if (bline->committer == NULL) {
4034 err = got_error_from_errno("strdup");
4038 committer_time = got_object_commit_get_committer_time(commit);
4039 if (localtime_r(&committer_time, &tm) == NULL)
4040 return got_error_from_errno("localtime_r");
4041 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4042 &tm) >= sizeof(bline->datebuf)) {
4043 err = got_error(GOT_ERR_NO_SPACE);
4046 bline->annotated = 1;
4048 /* Print lines annotated so far. */
4049 bline = &a->lines[a->lineno_cur - 1];
4050 if (!bline->annotated)
4053 offset = a->line_offsets[a->lineno_cur - 1];
4054 if (fseeko(a->f, offset, SEEK_SET) == -1) {
4055 err = got_error_from_errno("fseeko");
4059 while (bline->annotated) {
4060 char *smallerthan, *at, *nl, *committer;
4063 if (getline(&line, &linesize, a->f) == -1) {
4065 err = got_error_from_errno("getline");
4069 committer = bline->committer;
4070 smallerthan = strchr(committer, '<');
4071 if (smallerthan && smallerthan[1] != '\0')
4072 committer = smallerthan + 1;
4073 at = strchr(committer, '@');
4076 len = strlen(committer);
4078 committer[8] = '\0';
4080 nl = strchr(line, '\n');
4083 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4084 bline->id_str, bline->datebuf, committer, line);
4087 bline = &a->lines[a->lineno_cur - 1];
4091 got_object_commit_close(commit);
4096 static const struct got_error *
4097 cmd_blame(int argc, char *argv[])
4099 const struct got_error *error;
4100 struct got_repository *repo = NULL;
4101 struct got_worktree *worktree = NULL;
4102 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4103 char *link_target = NULL;
4104 struct got_object_id *obj_id = NULL;
4105 struct got_object_id *commit_id = NULL;
4106 struct got_blob_object *blob = NULL;
4107 char *commit_id_str = NULL;
4108 struct blame_cb_args bca;
4109 int ch, obj_type, i;
4112 memset(&bca, 0, sizeof(bca));
4115 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4120 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4123 commit_id_str = optarg;
4126 repo_path = realpath(optarg, NULL);
4127 if (repo_path == NULL)
4128 return got_error_from_errno2("realpath",
4130 got_path_strip_trailing_slashes(repo_path);
4146 cwd = getcwd(NULL, 0);
4148 error = got_error_from_errno("getcwd");
4151 if (repo_path == NULL) {
4152 error = got_worktree_open(&worktree, cwd);
4153 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4159 strdup(got_worktree_get_repo_path(worktree));
4160 if (repo_path == NULL) {
4161 error = got_error_from_errno("strdup");
4166 repo_path = strdup(cwd);
4167 if (repo_path == NULL) {
4168 error = got_error_from_errno("strdup");
4174 error = got_repo_open(&repo, repo_path, NULL);
4178 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4183 const char *prefix = got_worktree_get_path_prefix(worktree);
4184 char *p, *worktree_subdir = cwd +
4185 strlen(got_worktree_get_root_path(worktree));
4186 if (asprintf(&p, "%s%s%s%s%s",
4187 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4188 worktree_subdir, worktree_subdir[0] ? "/" : "",
4190 error = got_error_from_errno("asprintf");
4193 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4196 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4201 if (commit_id_str == NULL) {
4202 struct got_reference *head_ref;
4203 error = got_ref_open(&head_ref, repo, worktree ?
4204 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4207 error = got_ref_resolve(&commit_id, repo, head_ref);
4208 got_ref_close(head_ref);
4212 error = got_repo_match_object_id(&commit_id, NULL,
4213 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4218 error = got_object_resolve_symlinks(&link_target, in_repo_path,
4223 error = got_object_id_by_path(&obj_id, repo, commit_id,
4224 link_target ? link_target : in_repo_path);
4228 error = got_object_get_type(&obj_type, repo, obj_id);
4232 if (obj_type != GOT_OBJ_TYPE_BLOB) {
4233 error = got_error_path(link_target ? link_target : in_repo_path,
4238 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4241 bca.f = got_opentemp();
4242 if (bca.f == NULL) {
4243 error = got_error_from_errno("got_opentemp");
4246 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4247 &bca.line_offsets, bca.f, blob);
4248 if (error || bca.nlines == 0)
4251 /* Don't include \n at EOF in the blame line count. */
4252 if (bca.line_offsets[bca.nlines - 1] == filesize)
4255 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4256 if (bca.lines == NULL) {
4257 error = got_error_from_errno("calloc");
4261 bca.nlines_prec = 0;
4269 error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4270 repo, blame_cb, &bca, check_cancelled, NULL);
4279 got_object_blob_close(blob);
4281 got_worktree_close(worktree);
4283 const struct got_error *repo_error;
4284 repo_error = got_repo_close(repo);
4289 for (i = 0; i < bca.nlines; i++) {
4290 struct blame_line *bline = &bca.lines[i];
4291 free(bline->id_str);
4292 free(bline->committer);
4296 free(bca.line_offsets);
4297 if (bca.f && fclose(bca.f) == EOF && error == NULL)
4298 error = got_error_from_errno("fclose");
4306 "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4311 static const struct got_error *
4312 print_entry(struct got_tree_entry *te, const char *id, const char *path,
4313 const char *root_path, struct got_repository *repo)
4315 const struct got_error *err = NULL;
4316 int is_root_path = (strcmp(path, root_path) == 0);
4317 const char *modestr = "";
4318 mode_t mode = got_tree_entry_get_mode(te);
4319 char *link_target = NULL;
4321 path += strlen(root_path);
4322 while (path[0] == '/')
4325 if (got_object_tree_entry_is_submodule(te))
4327 else if (S_ISLNK(mode)) {
4330 err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4333 for (i = 0; i < strlen(link_target); i++) {
4334 if (!isprint((unsigned char)link_target[i]))
4335 link_target[i] = '?';
4340 else if (S_ISDIR(mode))
4342 else if (mode & S_IXUSR)
4345 printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4346 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4347 link_target ? " -> ": "", link_target ? link_target : "");
4353 static const struct got_error *
4354 print_tree(const char *path, struct got_object_id *commit_id,
4355 int show_ids, int recurse, const char *root_path,
4356 struct got_repository *repo)
4358 const struct got_error *err = NULL;
4359 struct got_object_id *tree_id = NULL;
4360 struct got_tree_object *tree = NULL;
4363 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4367 err = got_object_open_as_tree(&tree, repo, tree_id);
4370 nentries = got_object_tree_get_nentries(tree);
4371 for (i = 0; i < nentries; i++) {
4372 struct got_tree_entry *te;
4375 if (sigint_received || sigpipe_received)
4378 te = got_object_tree_get_entry(tree, i);
4381 err = got_object_id_str(&id_str,
4382 got_tree_entry_get_id(te));
4385 if (asprintf(&id, "%s ", id_str) == -1) {
4386 err = got_error_from_errno("asprintf");
4392 err = print_entry(te, id, path, root_path, repo);
4397 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4399 if (asprintf(&child_path, "%s%s%s", path,
4400 path[0] == '/' && path[1] == '\0' ? "" : "/",
4401 got_tree_entry_get_name(te)) == -1) {
4402 err = got_error_from_errno("asprintf");
4405 err = print_tree(child_path, commit_id, show_ids, 1,
4414 got_object_tree_close(tree);
4419 static const struct got_error *
4420 cmd_tree(int argc, char *argv[])
4422 const struct got_error *error;
4423 struct got_repository *repo = NULL;
4424 struct got_worktree *worktree = NULL;
4425 const char *path, *refname = NULL;
4426 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4427 struct got_object_id *commit_id = NULL;
4428 char *commit_id_str = NULL;
4429 int show_ids = 0, recurse = 0;
4433 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4438 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4441 commit_id_str = optarg;
4444 repo_path = realpath(optarg, NULL);
4445 if (repo_path == NULL)
4446 return got_error_from_errno2("realpath",
4448 got_path_strip_trailing_slashes(repo_path);
4472 cwd = getcwd(NULL, 0);
4474 error = got_error_from_errno("getcwd");
4477 if (repo_path == NULL) {
4478 error = got_worktree_open(&worktree, cwd);
4479 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4485 strdup(got_worktree_get_repo_path(worktree));
4486 if (repo_path == NULL)
4487 error = got_error_from_errno("strdup");
4491 repo_path = strdup(cwd);
4492 if (repo_path == NULL) {
4493 error = got_error_from_errno("strdup");
4499 error = got_repo_open(&repo, repo_path, NULL);
4503 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4509 char *p, *worktree_subdir = cwd +
4510 strlen(got_worktree_get_root_path(worktree));
4511 if (asprintf(&p, "%s/%s",
4512 got_worktree_get_path_prefix(worktree),
4513 worktree_subdir) == -1) {
4514 error = got_error_from_errno("asprintf");
4517 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4524 if (in_repo_path == NULL) {
4525 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4530 if (commit_id_str == NULL) {
4531 struct got_reference *head_ref;
4533 refname = got_worktree_get_head_ref_name(worktree);
4535 refname = GOT_REF_HEAD;
4536 error = got_ref_open(&head_ref, repo, refname, 0);
4539 error = got_ref_resolve(&commit_id, repo, head_ref);
4540 got_ref_close(head_ref);
4544 error = got_repo_match_object_id(&commit_id, NULL,
4545 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4550 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4551 in_repo_path, repo);
4558 got_worktree_close(worktree);
4560 const struct got_error *repo_error;
4561 repo_error = got_repo_close(repo);
4571 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4575 static const struct got_error *
4576 print_status(void *arg, unsigned char status, unsigned char staged_status,
4577 const char *path, struct got_object_id *blob_id,
4578 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4579 int dirfd, const char *de_name)
4581 if (status == staged_status && (status == GOT_STATUS_DELETE))
4582 status = GOT_STATUS_NO_CHANGE;
4583 printf("%c%c %s\n", status, staged_status, path);
4587 static const struct got_error *
4588 cmd_status(int argc, char *argv[])
4590 const struct got_error *error = NULL;
4591 struct got_repository *repo = NULL;
4592 struct got_worktree *worktree = NULL;
4594 struct got_pathlist_head paths;
4595 struct got_pathlist_entry *pe;
4600 while ((ch = getopt(argc, argv, "")) != -1) {
4612 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4616 cwd = getcwd(NULL, 0);
4618 error = got_error_from_errno("getcwd");
4622 error = got_worktree_open(&worktree, cwd);
4624 if (error->code == GOT_ERR_NOT_WORKTREE)
4625 error = wrap_not_worktree_error(error, "status", cwd);
4629 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4634 error = apply_unveil(got_repo_get_path(repo), 1,
4635 got_worktree_get_root_path(worktree));
4639 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4643 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4644 check_cancelled, NULL);
4646 TAILQ_FOREACH(pe, &paths, entry)
4647 free((char *)pe->path);
4648 got_pathlist_free(&paths);
4657 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4663 static const struct got_error *
4664 list_refs(struct got_repository *repo, const char *refname)
4666 static const struct got_error *err = NULL;
4667 struct got_reflist_head refs;
4668 struct got_reflist_entry *re;
4670 SIMPLEQ_INIT(&refs);
4671 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4675 SIMPLEQ_FOREACH(re, &refs, entry) {
4677 refstr = got_ref_to_str(re->ref);
4679 return got_error_from_errno("got_ref_to_str");
4680 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4684 got_ref_list_free(&refs);
4688 static const struct got_error *
4689 delete_ref(struct got_repository *repo, const char *refname)
4691 const struct got_error *err = NULL;
4692 struct got_reference *ref;
4694 err = got_ref_open(&ref, repo, refname, 0);
4698 err = got_ref_delete(ref, repo);
4703 static const struct got_error *
4704 add_ref(struct got_repository *repo, const char *refname, const char *target)
4706 const struct got_error *err = NULL;
4707 struct got_object_id *id;
4708 struct got_reference *ref = NULL;
4711 * Don't let the user create a reference name with a leading '-'.
4712 * While technically a valid reference name, this case is usually
4713 * an unintended typo.
4715 if (refname[0] == '-')
4716 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4718 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4721 struct got_reference *target_ref;
4723 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4725 err = got_ref_open(&target_ref, repo, target, 0);
4728 err = got_ref_resolve(&id, repo, target_ref);
4729 got_ref_close(target_ref);
4734 err = got_ref_alloc(&ref, refname, id);
4738 err = got_ref_write(ref, repo);
4746 static const struct got_error *
4747 add_symref(struct got_repository *repo, const char *refname, const char *target)
4749 const struct got_error *err = NULL;
4750 struct got_reference *ref = NULL;
4751 struct got_reference *target_ref = NULL;
4754 * Don't let the user create a reference name with a leading '-'.
4755 * While technically a valid reference name, this case is usually
4756 * an unintended typo.
4758 if (refname[0] == '-')
4759 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4761 err = got_ref_open(&target_ref, repo, target, 0);
4765 err = got_ref_alloc_symref(&ref, refname, target_ref);
4769 err = got_ref_write(ref, repo);
4772 got_ref_close(target_ref);
4778 static const struct got_error *
4779 cmd_ref(int argc, char *argv[])
4781 const struct got_error *error = NULL;
4782 struct got_repository *repo = NULL;
4783 struct got_worktree *worktree = NULL;
4784 char *cwd = NULL, *repo_path = NULL;
4785 int ch, do_list = 0, do_delete = 0;
4786 const char *obj_arg = NULL, *symref_target= NULL;
4787 char *refname = NULL;
4789 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4798 repo_path = realpath(optarg, NULL);
4799 if (repo_path == NULL)
4800 return got_error_from_errno2("realpath",
4802 got_path_strip_trailing_slashes(repo_path);
4808 symref_target = optarg;
4816 if (obj_arg && do_list)
4817 errx(1, "-c and -l options are mutually exclusive");
4818 if (obj_arg && do_delete)
4819 errx(1, "-c and -d options are mutually exclusive");
4820 if (obj_arg && symref_target)
4821 errx(1, "-c and -s options are mutually exclusive");
4822 if (symref_target && do_delete)
4823 errx(1, "-s and -d options are mutually exclusive");
4824 if (symref_target && do_list)
4825 errx(1, "-s and -l options are mutually exclusive");
4826 if (do_delete && do_list)
4827 errx(1, "-d and -l options are mutually exclusive");
4833 if (argc != 0 && argc != 1)
4836 refname = strdup(argv[0]);
4837 if (refname == NULL) {
4838 error = got_error_from_errno("strdup");
4845 refname = strdup(argv[0]);
4846 if (refname == NULL) {
4847 error = got_error_from_errno("strdup");
4853 got_path_strip_trailing_slashes(refname);
4857 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4861 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4862 "sendfd unveil", NULL) == -1)
4866 cwd = getcwd(NULL, 0);
4868 error = got_error_from_errno("getcwd");
4872 if (repo_path == NULL) {
4873 error = got_worktree_open(&worktree, cwd);
4874 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4880 strdup(got_worktree_get_repo_path(worktree));
4881 if (repo_path == NULL)
4882 error = got_error_from_errno("strdup");
4886 repo_path = strdup(cwd);
4887 if (repo_path == NULL) {
4888 error = got_error_from_errno("strdup");
4894 error = got_repo_open(&repo, repo_path, NULL);
4898 error = apply_unveil(got_repo_get_path(repo), do_list,
4899 worktree ? got_worktree_get_root_path(worktree) : NULL);
4904 error = list_refs(repo, refname);
4906 error = delete_ref(repo, refname);
4907 else if (symref_target)
4908 error = add_symref(repo, refname, symref_target);
4910 if (obj_arg == NULL)
4912 error = add_ref(repo, refname, obj_arg);
4917 got_repo_close(repo);
4919 got_worktree_close(worktree);
4929 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4930 "[name]\n", getprogname());
4934 static const struct got_error *
4935 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4936 struct got_reference *ref)
4938 const struct got_error *err = NULL;
4939 const char *refname, *marker = " ";
4942 refname = got_ref_get_name(ref);
4943 if (worktree && strcmp(refname,
4944 got_worktree_get_head_ref_name(worktree)) == 0) {
4945 struct got_object_id *id = NULL;
4947 err = got_ref_resolve(&id, repo, ref);
4950 if (got_object_id_cmp(id,
4951 got_worktree_get_base_commit_id(worktree)) == 0)
4958 if (strncmp(refname, "refs/heads/", 11) == 0)
4960 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4963 refstr = got_ref_to_str(ref);
4965 return got_error_from_errno("got_ref_to_str");
4967 printf("%s%s: %s\n", marker, refname, refstr);
4972 static const struct got_error *
4973 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4975 const char *refname;
4977 if (worktree == NULL)
4978 return got_error(GOT_ERR_NOT_WORKTREE);
4980 refname = got_worktree_get_head_ref_name(worktree);
4982 if (strncmp(refname, "refs/heads/", 11) == 0)
4984 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4987 printf("%s\n", refname);
4992 static const struct got_error *
4993 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4995 static const struct got_error *err = NULL;
4996 struct got_reflist_head refs;
4997 struct got_reflist_entry *re;
4998 struct got_reference *temp_ref = NULL;
4999 int rebase_in_progress, histedit_in_progress;
5001 SIMPLEQ_INIT(&refs);
5004 err = got_worktree_rebase_in_progress(&rebase_in_progress,
5009 err = got_worktree_histedit_in_progress(&histedit_in_progress,
5014 if (rebase_in_progress || histedit_in_progress) {
5015 err = got_ref_open(&temp_ref, repo,
5016 got_worktree_get_head_ref_name(worktree), 0);
5019 list_branch(repo, worktree, temp_ref);
5020 got_ref_close(temp_ref);
5024 err = got_ref_list(&refs, repo, "refs/heads",
5025 got_ref_cmp_by_name, NULL);
5029 SIMPLEQ_FOREACH(re, &refs, entry)
5030 list_branch(repo, worktree, re->ref);
5032 got_ref_list_free(&refs);
5036 static const struct got_error *
5037 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5038 const char *branch_name)
5040 const struct got_error *err = NULL;
5041 struct got_reference *ref = NULL;
5044 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5045 return got_error_from_errno("asprintf");
5047 err = got_ref_open(&ref, repo, refname, 0);
5052 strcmp(got_worktree_get_head_ref_name(worktree),
5053 got_ref_get_name(ref)) == 0) {
5054 err = got_error_msg(GOT_ERR_SAME_BRANCH,
5055 "will not delete this work tree's current branch");
5059 err = got_ref_delete(ref, repo);
5067 static const struct got_error *
5068 add_branch(struct got_repository *repo, const char *branch_name,
5069 struct got_object_id *base_commit_id)
5071 const struct got_error *err = NULL;
5072 struct got_reference *ref = NULL;
5073 char *base_refname = NULL, *refname = NULL;
5076 * Don't let the user create a branch name with a leading '-'.
5077 * While technically a valid reference name, this case is usually
5078 * an unintended typo.
5080 if (branch_name[0] == '-')
5081 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5083 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5084 err = got_error_from_errno("asprintf");
5088 err = got_ref_open(&ref, repo, refname, 0);
5090 err = got_error(GOT_ERR_BRANCH_EXISTS);
5092 } else if (err->code != GOT_ERR_NOT_REF)
5095 err = got_ref_alloc(&ref, refname, base_commit_id);
5099 err = got_ref_write(ref, repo);
5108 static const struct got_error *
5109 cmd_branch(int argc, char *argv[])
5111 const struct got_error *error = NULL;
5112 struct got_repository *repo = NULL;
5113 struct got_worktree *worktree = NULL;
5114 char *cwd = NULL, *repo_path = NULL;
5115 int ch, do_list = 0, do_show = 0, do_update = 1;
5116 const char *delref = NULL, *commit_id_arg = NULL;
5117 struct got_reference *ref = NULL;
5118 struct got_pathlist_head paths;
5119 struct got_pathlist_entry *pe;
5120 struct got_object_id *commit_id = NULL;
5121 char *commit_id_str = NULL;
5125 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5128 commit_id_arg = optarg;
5134 repo_path = realpath(optarg, NULL);
5135 if (repo_path == NULL)
5136 return got_error_from_errno2("realpath",
5138 got_path_strip_trailing_slashes(repo_path);
5152 if (do_list && delref)
5153 errx(1, "-l and -d options are mutually exclusive");
5158 if (!do_list && !delref && argc == 0)
5161 if ((do_list || delref || do_show) && commit_id_arg != NULL)
5162 errx(1, "-c option can only be used when creating a branch");
5164 if (do_list || delref) {
5167 } else if (!do_show && argc != 1)
5171 if (do_list || do_show) {
5172 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5176 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5177 "sendfd unveil", NULL) == -1)
5181 cwd = getcwd(NULL, 0);
5183 error = got_error_from_errno("getcwd");
5187 if (repo_path == NULL) {
5188 error = got_worktree_open(&worktree, cwd);
5189 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5195 strdup(got_worktree_get_repo_path(worktree));
5196 if (repo_path == NULL)
5197 error = got_error_from_errno("strdup");
5201 repo_path = strdup(cwd);
5202 if (repo_path == NULL) {
5203 error = got_error_from_errno("strdup");
5209 error = got_repo_open(&repo, repo_path, NULL);
5213 error = apply_unveil(got_repo_get_path(repo), do_list,
5214 worktree ? got_worktree_get_root_path(worktree) : NULL);
5219 error = show_current_branch(repo, worktree);
5221 error = list_branches(repo, worktree);
5223 error = delete_branch(repo, worktree, delref);
5225 if (commit_id_arg == NULL)
5226 commit_id_arg = worktree ?
5227 got_worktree_get_head_ref_name(worktree) :
5229 error = got_repo_match_object_id(&commit_id, NULL,
5230 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5233 error = add_branch(repo, argv[0], commit_id);
5236 if (worktree && do_update) {
5237 struct got_update_progress_arg upa;
5238 char *branch_refname = NULL;
5240 error = got_object_id_str(&commit_id_str, commit_id);
5243 error = get_worktree_paths_from_argv(&paths, 0, NULL,
5247 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5249 error = got_error_from_errno("asprintf");
5252 error = got_ref_open(&ref, repo, branch_refname, 0);
5253 free(branch_refname);
5256 error = switch_head_ref(ref, commit_id, worktree,
5260 error = got_worktree_set_base_commit_id(worktree, repo,
5264 memset(&upa, 0, sizeof(upa));
5265 error = got_worktree_checkout_files(worktree, &paths,
5266 repo, update_progress, &upa, check_cancelled,
5270 if (upa.did_something)
5271 printf("Updated to commit %s\n", commit_id_str);
5272 print_update_progress_stats(&upa);
5279 got_repo_close(repo);
5281 got_worktree_close(worktree);
5285 free(commit_id_str);
5286 TAILQ_FOREACH(pe, &paths, entry)
5287 free((char *)pe->path);
5288 got_pathlist_free(&paths);
5297 "usage: %s tag [-c commit] [-r repository] [-l] "
5298 "[-m message] name\n", getprogname());
5303 static const struct got_error *
5304 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5306 const struct got_error *err = NULL;
5307 struct got_reflist_entry *re, *se, *new;
5308 struct got_object_id *re_id, *se_id;
5309 struct got_tag_object *re_tag, *se_tag;
5310 time_t re_time, se_time;
5312 SIMPLEQ_FOREACH(re, tags, entry) {
5313 se = SIMPLEQ_FIRST(sorted);
5315 err = got_reflist_entry_dup(&new, re);
5318 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5321 err = got_ref_resolve(&re_id, repo, re->ref);
5324 err = got_object_open_as_tag(&re_tag, repo, re_id);
5328 re_time = got_object_tag_get_tagger_time(re_tag);
5329 got_object_tag_close(re_tag);
5333 err = got_ref_resolve(&se_id, repo, re->ref);
5336 err = got_object_open_as_tag(&se_tag, repo, se_id);
5340 se_time = got_object_tag_get_tagger_time(se_tag);
5341 got_object_tag_close(se_tag);
5343 if (se_time > re_time) {
5344 err = got_reflist_entry_dup(&new, re);
5347 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5350 se = SIMPLEQ_NEXT(se, entry);
5359 static const struct got_error *
5360 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5362 static const struct got_error *err = NULL;
5363 struct got_reflist_head refs;
5364 struct got_reflist_entry *re;
5366 SIMPLEQ_INIT(&refs);
5368 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5372 SIMPLEQ_FOREACH(re, &refs, entry) {
5373 const char *refname;
5374 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5378 struct got_object_id *id;
5379 struct got_tag_object *tag;
5380 struct got_commit_object *commit = NULL;
5382 refname = got_ref_get_name(re->ref);
5383 if (strncmp(refname, "refs/tags/", 10) != 0)
5386 refstr = got_ref_to_str(re->ref);
5387 if (refstr == NULL) {
5388 err = got_error_from_errno("got_ref_to_str");
5391 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5394 err = got_ref_resolve(&id, repo, re->ref);
5397 err = got_object_open_as_tag(&tag, repo, id);
5399 if (err->code != GOT_ERR_OBJ_TYPE) {
5403 /* "lightweight" tag */
5404 err = got_object_open_as_commit(&commit, repo, id);
5409 tagger = got_object_commit_get_committer(commit);
5411 got_object_commit_get_committer_time(commit);
5412 err = got_object_id_str(&id_str, id);
5418 tagger = got_object_tag_get_tagger(tag);
5419 tagger_time = got_object_tag_get_tagger_time(tag);
5420 err = got_object_id_str(&id_str,
5421 got_object_tag_get_object_id(tag));
5425 printf("from: %s\n", tagger);
5426 datestr = get_datestr(&tagger_time, datebuf);
5428 printf("date: %s UTC\n", datestr);
5430 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5432 switch (got_object_tag_get_object_type(tag)) {
5433 case GOT_OBJ_TYPE_BLOB:
5434 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5437 case GOT_OBJ_TYPE_TREE:
5438 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5441 case GOT_OBJ_TYPE_COMMIT:
5442 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5445 case GOT_OBJ_TYPE_TAG:
5446 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5455 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5458 got_object_commit_close(commit);
5460 tagmsg0 = strdup(got_object_tag_get_message(tag));
5461 got_object_tag_close(tag);
5462 if (tagmsg0 == NULL) {
5463 err = got_error_from_errno("strdup");
5470 line = strsep(&tagmsg, "\n");
5472 printf(" %s\n", line);
5477 got_ref_list_free(&refs);
5481 static const struct got_error *
5482 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5483 const char *tag_name, const char *repo_path)
5485 const struct got_error *err = NULL;
5486 char *template = NULL, *initial_content = NULL;
5487 char *editor = NULL;
5490 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5491 err = got_error_from_errno("asprintf");
5495 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5496 commit_id_str, tag_name) == -1) {
5497 err = got_error_from_errno("asprintf");
5501 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5505 dprintf(fd, initial_content);
5508 err = get_editor(&editor);
5511 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5513 free(initial_content);
5517 /* Editor is done; we can now apply unveil(2) */
5519 err = apply_unveil(repo_path, 0, NULL);
5528 static const struct got_error *
5529 add_tag(struct got_repository *repo, const char *tag_name,
5530 const char *commit_arg, const char *tagmsg_arg)
5532 const struct got_error *err = NULL;
5533 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5534 char *label = NULL, *commit_id_str = NULL;
5535 struct got_reference *ref = NULL;
5536 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5537 char *tagmsg_path = NULL, *tag_id_str = NULL;
5538 int preserve_tagmsg = 0;
5541 * Don't let the user create a tag name with a leading '-'.
5542 * While technically a valid reference name, this case is usually
5543 * an unintended typo.
5545 if (tag_name[0] == '-')
5546 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5548 err = get_author(&tagger, repo);
5552 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5553 GOT_OBJ_TYPE_COMMIT, 1, repo);
5557 err = got_object_id_str(&commit_id_str, commit_id);
5561 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5562 refname = strdup(tag_name);
5563 if (refname == NULL) {
5564 err = got_error_from_errno("strdup");
5568 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5569 err = got_error_from_errno("asprintf");
5573 err = got_ref_open(&ref, repo, refname, 0);
5575 err = got_error(GOT_ERR_TAG_EXISTS);
5577 } else if (err->code != GOT_ERR_NOT_REF)
5580 if (tagmsg_arg == NULL) {
5581 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5582 tag_name, got_repo_get_path(repo));
5584 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5585 tagmsg_path != NULL)
5586 preserve_tagmsg = 1;
5591 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5592 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5595 preserve_tagmsg = 1;
5599 err = got_ref_alloc(&ref, refname, tag_id);
5602 preserve_tagmsg = 1;
5606 err = got_ref_write(ref, repo);
5609 preserve_tagmsg = 1;
5613 err = got_object_id_str(&tag_id_str, tag_id);
5616 preserve_tagmsg = 1;
5619 printf("Created tag %s\n", tag_id_str);
5621 if (preserve_tagmsg) {
5622 fprintf(stderr, "%s: tag message preserved in %s\n",
5623 getprogname(), tagmsg_path);
5624 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5625 err = got_error_from_errno2("unlink", tagmsg_path);
5630 free(commit_id_str);
5638 static const struct got_error *
5639 cmd_tag(int argc, char *argv[])
5641 const struct got_error *error = NULL;
5642 struct got_repository *repo = NULL;
5643 struct got_worktree *worktree = NULL;
5644 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5645 char *gitconfig_path = NULL;
5646 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5647 int ch, do_list = 0;
5649 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5652 commit_id_arg = optarg;
5658 repo_path = realpath(optarg, NULL);
5659 if (repo_path == NULL)
5660 return got_error_from_errno2("realpath",
5662 got_path_strip_trailing_slashes(repo_path);
5677 if (commit_id_arg != NULL)
5679 "-c option can only be used when creating a tag");
5681 errx(1, "-l and -m options are mutually exclusive");
5684 } else if (argc != 1)
5691 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5695 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5696 "sendfd unveil", NULL) == -1)
5700 cwd = getcwd(NULL, 0);
5702 error = got_error_from_errno("getcwd");
5706 if (repo_path == NULL) {
5707 error = got_worktree_open(&worktree, cwd);
5708 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5714 strdup(got_worktree_get_repo_path(worktree));
5715 if (repo_path == NULL)
5716 error = got_error_from_errno("strdup");
5720 repo_path = strdup(cwd);
5721 if (repo_path == NULL) {
5722 error = got_error_from_errno("strdup");
5729 error = got_repo_open(&repo, repo_path, NULL);
5732 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5735 error = list_tags(repo, worktree);
5737 error = get_gitconfig_path(&gitconfig_path);
5740 error = got_repo_open(&repo, repo_path, gitconfig_path);
5745 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5750 if (commit_id_arg == NULL) {
5751 struct got_reference *head_ref;
5752 struct got_object_id *commit_id;
5753 error = got_ref_open(&head_ref, repo,
5754 worktree ? got_worktree_get_head_ref_name(worktree)
5758 error = got_ref_resolve(&commit_id, repo, head_ref);
5759 got_ref_close(head_ref);
5762 error = got_object_id_str(&commit_id_str, commit_id);
5768 error = add_tag(repo, tag_name,
5769 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5773 got_repo_close(repo);
5775 got_worktree_close(worktree);
5778 free(gitconfig_path);
5779 free(commit_id_str);
5786 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5791 static const struct got_error *
5792 add_progress(void *arg, unsigned char status, const char *path)
5794 while (path[0] == '/')
5796 printf("%c %s\n", status, path);
5800 static const struct got_error *
5801 cmd_add(int argc, char *argv[])
5803 const struct got_error *error = NULL;
5804 struct got_repository *repo = NULL;
5805 struct got_worktree *worktree = NULL;
5807 struct got_pathlist_head paths;
5808 struct got_pathlist_entry *pe;
5809 int ch, can_recurse = 0, no_ignores = 0;
5813 while ((ch = getopt(argc, argv, "IR")) != -1) {
5831 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5838 cwd = getcwd(NULL, 0);
5840 error = got_error_from_errno("getcwd");
5844 error = got_worktree_open(&worktree, cwd);
5846 if (error->code == GOT_ERR_NOT_WORKTREE)
5847 error = wrap_not_worktree_error(error, "add", cwd);
5851 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5856 error = apply_unveil(got_repo_get_path(repo), 1,
5857 got_worktree_get_root_path(worktree));
5861 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5865 if (!can_recurse && no_ignores) {
5866 error = got_error_msg(GOT_ERR_BAD_PATH,
5867 "disregarding ignores requires -R option");
5875 TAILQ_FOREACH(pe, &paths, entry) {
5876 if (asprintf(&ondisk_path, "%s/%s",
5877 got_worktree_get_root_path(worktree),
5879 error = got_error_from_errno("asprintf");
5882 if (lstat(ondisk_path, &sb) == -1) {
5883 if (errno == ENOENT) {
5887 error = got_error_from_errno2("lstat",
5893 if (S_ISDIR(sb.st_mode)) {
5894 error = got_error_msg(GOT_ERR_BAD_PATH,
5895 "adding directories requires -R option");
5901 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5902 NULL, repo, no_ignores);
5905 got_repo_close(repo);
5907 got_worktree_close(worktree);
5908 TAILQ_FOREACH(pe, &paths, entry)
5909 free((char *)pe->path);
5910 got_pathlist_free(&paths);
5918 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5923 static const struct got_error *
5924 print_remove_status(void *arg, unsigned char status,
5925 unsigned char staged_status, const char *path)
5927 while (path[0] == '/')
5929 if (status == GOT_STATUS_NONEXISTENT)
5931 if (status == staged_status && (status == GOT_STATUS_DELETE))
5932 status = GOT_STATUS_NO_CHANGE;
5933 printf("%c%c %s\n", status, staged_status, path);
5937 static const struct got_error *
5938 cmd_remove(int argc, char *argv[])
5940 const struct got_error *error = NULL;
5941 struct got_worktree *worktree = NULL;
5942 struct got_repository *repo = NULL;
5944 struct got_pathlist_head paths;
5945 struct got_pathlist_entry *pe;
5946 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5950 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5953 delete_local_mods = 1;
5971 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5978 cwd = getcwd(NULL, 0);
5980 error = got_error_from_errno("getcwd");
5983 error = got_worktree_open(&worktree, cwd);
5985 if (error->code == GOT_ERR_NOT_WORKTREE)
5986 error = wrap_not_worktree_error(error, "remove", cwd);
5990 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5995 error = apply_unveil(got_repo_get_path(repo), 1,
5996 got_worktree_get_root_path(worktree));
6000 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6007 TAILQ_FOREACH(pe, &paths, entry) {
6008 if (asprintf(&ondisk_path, "%s/%s",
6009 got_worktree_get_root_path(worktree),
6011 error = got_error_from_errno("asprintf");
6014 if (lstat(ondisk_path, &sb) == -1) {
6015 if (errno == ENOENT) {
6019 error = got_error_from_errno2("lstat",
6025 if (S_ISDIR(sb.st_mode)) {
6026 error = got_error_msg(GOT_ERR_BAD_PATH,
6027 "removing directories requires -R option");
6033 error = got_worktree_schedule_delete(worktree, &paths,
6034 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6037 got_repo_close(repo);
6039 got_worktree_close(worktree);
6040 TAILQ_FOREACH(pe, &paths, entry)
6041 free((char *)pe->path);
6042 got_pathlist_free(&paths);
6050 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6051 "path ...\n", getprogname());
6055 static const struct got_error *
6056 revert_progress(void *arg, unsigned char status, const char *path)
6058 if (status == GOT_STATUS_UNVERSIONED)
6061 while (path[0] == '/')
6063 printf("%c %s\n", status, path);
6067 struct choose_patch_arg {
6068 FILE *patch_script_file;
6072 static const struct got_error *
6073 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6074 int nchanges, const char *action)
6077 size_t linesize = 0;
6081 case GOT_STATUS_ADD:
6082 printf("A %s\n%s this addition? [y/n] ", path, action);
6084 case GOT_STATUS_DELETE:
6085 printf("D %s\n%s this deletion? [y/n] ", path, action);
6087 case GOT_STATUS_MODIFY:
6088 if (fseek(patch_file, 0L, SEEK_SET) == -1)
6089 return got_error_from_errno("fseek");
6090 printf(GOT_COMMIT_SEP_STR);
6091 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6093 if (ferror(patch_file))
6094 return got_error_from_errno("getline");
6095 printf(GOT_COMMIT_SEP_STR);
6096 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6097 path, n, nchanges, action);
6100 return got_error_path(path, GOT_ERR_FILE_STATUS);
6106 static const struct got_error *
6107 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6108 FILE *patch_file, int n, int nchanges)
6110 const struct got_error *err = NULL;
6112 size_t linesize = 0;
6115 struct choose_patch_arg *a = arg;
6117 *choice = GOT_PATCH_CHOICE_NONE;
6119 if (a->patch_script_file) {
6121 err = show_change(status, path, patch_file, n, nchanges,
6125 linelen = getline(&line, &linesize, a->patch_script_file);
6126 if (linelen == -1) {
6127 if (ferror(a->patch_script_file))
6128 return got_error_from_errno("getline");
6131 nl = strchr(line, '\n');
6134 if (strcmp(line, "y") == 0) {
6135 *choice = GOT_PATCH_CHOICE_YES;
6137 } else if (strcmp(line, "n") == 0) {
6138 *choice = GOT_PATCH_CHOICE_NO;
6140 } else if (strcmp(line, "q") == 0 &&
6141 status == GOT_STATUS_MODIFY) {
6142 *choice = GOT_PATCH_CHOICE_QUIT;
6145 printf("invalid response '%s'\n", line);
6150 while (resp != 'y' && resp != 'n' && resp != 'q') {
6151 err = show_change(status, path, patch_file, n, nchanges,
6158 if (status == GOT_STATUS_MODIFY) {
6159 if (resp != 'y' && resp != 'n' && resp != 'q') {
6160 printf("invalid response '%c'\n", resp);
6163 } else if (resp != 'y' && resp != 'n') {
6164 printf("invalid response '%c'\n", resp);
6170 *choice = GOT_PATCH_CHOICE_YES;
6171 else if (resp == 'n')
6172 *choice = GOT_PATCH_CHOICE_NO;
6173 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6174 *choice = GOT_PATCH_CHOICE_QUIT;
6180 static const struct got_error *
6181 cmd_revert(int argc, char *argv[])
6183 const struct got_error *error = NULL;
6184 struct got_worktree *worktree = NULL;
6185 struct got_repository *repo = NULL;
6186 char *cwd = NULL, *path = NULL;
6187 struct got_pathlist_head paths;
6188 struct got_pathlist_entry *pe;
6189 int ch, can_recurse = 0, pflag = 0;
6190 FILE *patch_script_file = NULL;
6191 const char *patch_script_path = NULL;
6192 struct choose_patch_arg cpa;
6196 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6202 patch_script_path = optarg;
6217 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6218 "unveil", NULL) == -1)
6223 if (patch_script_path && !pflag)
6224 errx(1, "-F option can only be used together with -p option");
6226 cwd = getcwd(NULL, 0);
6228 error = got_error_from_errno("getcwd");
6231 error = got_worktree_open(&worktree, cwd);
6233 if (error->code == GOT_ERR_NOT_WORKTREE)
6234 error = wrap_not_worktree_error(error, "revert", cwd);
6238 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6243 if (patch_script_path) {
6244 patch_script_file = fopen(patch_script_path, "r");
6245 if (patch_script_file == NULL) {
6246 error = got_error_from_errno2("fopen",
6251 error = apply_unveil(got_repo_get_path(repo), 1,
6252 got_worktree_get_root_path(worktree));
6256 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6263 TAILQ_FOREACH(pe, &paths, entry) {
6264 if (asprintf(&ondisk_path, "%s/%s",
6265 got_worktree_get_root_path(worktree),
6267 error = got_error_from_errno("asprintf");
6270 if (lstat(ondisk_path, &sb) == -1) {
6271 if (errno == ENOENT) {
6275 error = got_error_from_errno2("lstat",
6281 if (S_ISDIR(sb.st_mode)) {
6282 error = got_error_msg(GOT_ERR_BAD_PATH,
6283 "reverting directories requires -R option");
6289 cpa.patch_script_file = patch_script_file;
6290 cpa.action = "revert";
6291 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6292 pflag ? choose_patch : NULL, &cpa, repo);
6294 if (patch_script_file && fclose(patch_script_file) == EOF &&
6296 error = got_error_from_errno2("fclose", patch_script_path);
6298 got_repo_close(repo);
6300 got_worktree_close(worktree);
6309 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
6314 struct collect_commit_logmsg_arg {
6315 const char *cmdline_log;
6317 const char *worktree_path;
6318 const char *branch_name;
6319 const char *repo_path;
6324 static const struct got_error *
6325 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6328 char *initial_content = NULL;
6329 struct got_pathlist_entry *pe;
6330 const struct got_error *err = NULL;
6331 char *template = NULL;
6332 struct collect_commit_logmsg_arg *a = arg;
6336 /* if a message was specified on the command line, just use it */
6337 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6338 len = strlen(a->cmdline_log) + 1;
6339 *logmsg = malloc(len + 1);
6340 if (*logmsg == NULL)
6341 return got_error_from_errno("malloc");
6342 strlcpy(*logmsg, a->cmdline_log, len);
6346 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6347 return got_error_from_errno("asprintf");
6349 if (asprintf(&initial_content,
6350 "\n# changes to be committed on branch %s:\n",
6351 a->branch_name) == -1)
6352 return got_error_from_errno("asprintf");
6354 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6358 dprintf(fd, initial_content);
6360 TAILQ_FOREACH(pe, commitable_paths, entry) {
6361 struct got_commitable *ct = pe->data;
6362 dprintf(fd, "# %c %s\n",
6363 got_commitable_get_status(ct),
6364 got_commitable_get_path(ct));
6368 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6370 free(initial_content);
6373 /* Editor is done; we can now apply unveil(2) */
6375 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6384 static const struct got_error *
6385 cmd_commit(int argc, char *argv[])
6387 const struct got_error *error = NULL;
6388 struct got_worktree *worktree = NULL;
6389 struct got_repository *repo = NULL;
6390 char *cwd = NULL, *id_str = NULL;
6391 struct got_object_id *id = NULL;
6392 const char *logmsg = NULL;
6393 struct collect_commit_logmsg_arg cl_arg;
6394 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6395 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6396 struct got_pathlist_head paths;
6399 cl_arg.logmsg_path = NULL;
6401 while ((ch = getopt(argc, argv, "m:")) != -1) {
6416 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6417 "unveil", NULL) == -1)
6420 cwd = getcwd(NULL, 0);
6422 error = got_error_from_errno("getcwd");
6425 error = got_worktree_open(&worktree, cwd);
6427 if (error->code == GOT_ERR_NOT_WORKTREE)
6428 error = wrap_not_worktree_error(error, "commit", cwd);
6432 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6435 if (rebase_in_progress) {
6436 error = got_error(GOT_ERR_REBASING);
6440 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6445 error = get_gitconfig_path(&gitconfig_path);
6448 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6453 error = get_author(&author, repo);
6458 * unveil(2) traverses exec(2); if an editor is used we have
6459 * to apply unveil after the log message has been written.
6461 if (logmsg == NULL || strlen(logmsg) == 0)
6462 error = get_editor(&editor);
6464 error = apply_unveil(got_repo_get_path(repo), 0,
6465 got_worktree_get_root_path(worktree));
6469 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6473 cl_arg.editor = editor;
6474 cl_arg.cmdline_log = logmsg;
6475 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6476 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6477 if (!histedit_in_progress) {
6478 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6479 error = got_error(GOT_ERR_COMMIT_BRANCH);
6482 cl_arg.branch_name += 11;
6484 cl_arg.repo_path = got_repo_get_path(repo);
6485 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6486 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6488 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6489 cl_arg.logmsg_path != NULL)
6490 preserve_logmsg = 1;
6494 error = got_object_id_str(&id_str, id);
6497 printf("Created commit %s\n", id_str);
6499 if (preserve_logmsg) {
6500 fprintf(stderr, "%s: log message preserved in %s\n",
6501 getprogname(), cl_arg.logmsg_path);
6502 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6504 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6505 free(cl_arg.logmsg_path);
6507 got_repo_close(repo);
6509 got_worktree_close(worktree);
6512 free(gitconfig_path);
6519 usage_cherrypick(void)
6521 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6525 static const struct got_error *
6526 cmd_cherrypick(int argc, char *argv[])
6528 const struct got_error *error = NULL;
6529 struct got_worktree *worktree = NULL;
6530 struct got_repository *repo = NULL;
6531 char *cwd = NULL, *commit_id_str = NULL;
6532 struct got_object_id *commit_id = NULL;
6533 struct got_commit_object *commit = NULL;
6534 struct got_object_qid *pid;
6535 struct got_reference *head_ref = NULL;
6537 struct got_update_progress_arg upa;
6539 while ((ch = getopt(argc, argv, "")) != -1) {
6551 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6552 "unveil", NULL) == -1)
6558 cwd = getcwd(NULL, 0);
6560 error = got_error_from_errno("getcwd");
6563 error = got_worktree_open(&worktree, cwd);
6565 if (error->code == GOT_ERR_NOT_WORKTREE)
6566 error = wrap_not_worktree_error(error, "cherrypick",
6571 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6576 error = apply_unveil(got_repo_get_path(repo), 0,
6577 got_worktree_get_root_path(worktree));
6581 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6582 GOT_OBJ_TYPE_COMMIT, repo);
6583 if (error != NULL) {
6584 struct got_reference *ref;
6585 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6587 error = got_ref_open(&ref, repo, argv[0], 0);
6590 error = got_ref_resolve(&commit_id, repo, ref);
6595 error = got_object_id_str(&commit_id_str, commit_id);
6599 error = got_ref_open(&head_ref, repo,
6600 got_worktree_get_head_ref_name(worktree), 0);
6604 error = check_same_branch(commit_id, head_ref, NULL, repo);
6606 if (error->code != GOT_ERR_ANCESTRY)
6610 error = got_error(GOT_ERR_SAME_BRANCH);
6614 error = got_object_open_as_commit(&commit, repo, commit_id);
6617 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6618 memset(&upa, 0, sizeof(upa));
6619 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6620 commit_id, repo, update_progress, &upa, check_cancelled,
6625 if (upa.did_something)
6626 printf("Merged commit %s\n", commit_id_str);
6627 print_update_progress_stats(&upa);
6630 got_object_commit_close(commit);
6631 free(commit_id_str);
6633 got_ref_close(head_ref);
6635 got_worktree_close(worktree);
6637 got_repo_close(repo);
6644 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6648 static const struct got_error *
6649 cmd_backout(int argc, char *argv[])
6651 const struct got_error *error = NULL;
6652 struct got_worktree *worktree = NULL;
6653 struct got_repository *repo = NULL;
6654 char *cwd = NULL, *commit_id_str = NULL;
6655 struct got_object_id *commit_id = NULL;
6656 struct got_commit_object *commit = NULL;
6657 struct got_object_qid *pid;
6658 struct got_reference *head_ref = NULL;
6660 struct got_update_progress_arg upa;
6662 while ((ch = getopt(argc, argv, "")) != -1) {
6674 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6675 "unveil", NULL) == -1)
6681 cwd = getcwd(NULL, 0);
6683 error = got_error_from_errno("getcwd");
6686 error = got_worktree_open(&worktree, cwd);
6688 if (error->code == GOT_ERR_NOT_WORKTREE)
6689 error = wrap_not_worktree_error(error, "backout", cwd);
6693 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6698 error = apply_unveil(got_repo_get_path(repo), 0,
6699 got_worktree_get_root_path(worktree));
6703 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6704 GOT_OBJ_TYPE_COMMIT, repo);
6705 if (error != NULL) {
6706 struct got_reference *ref;
6707 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6709 error = got_ref_open(&ref, repo, argv[0], 0);
6712 error = got_ref_resolve(&commit_id, repo, ref);
6717 error = got_object_id_str(&commit_id_str, commit_id);
6721 error = got_ref_open(&head_ref, repo,
6722 got_worktree_get_head_ref_name(worktree), 0);
6726 error = check_same_branch(commit_id, head_ref, NULL, repo);
6730 error = got_object_open_as_commit(&commit, repo, commit_id);
6733 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6735 error = got_error(GOT_ERR_ROOT_COMMIT);
6739 memset(&upa, 0, sizeof(upa));
6740 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6741 update_progress, &upa, check_cancelled, NULL);
6745 if (upa.did_something)
6746 printf("Backed out commit %s\n", commit_id_str);
6747 print_update_progress_stats(&upa);
6750 got_object_commit_close(commit);
6751 free(commit_id_str);
6753 got_ref_close(head_ref);
6755 got_worktree_close(worktree);
6757 got_repo_close(repo);
6764 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6770 trim_logmsg(char *logmsg, int limit)
6775 len = strlen(logmsg);
6779 nl = strchr(logmsg, '\n');
6784 static const struct got_error *
6785 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6787 const struct got_error *err;
6788 char *logmsg0 = NULL;
6791 err = got_object_commit_get_logmsg(&logmsg0, commit);
6796 while (isspace((unsigned char)s[0]))
6799 *logmsg = strdup(s);
6800 if (*logmsg == NULL) {
6801 err = got_error_from_errno("strdup");
6805 trim_logmsg(*logmsg, limit);
6811 static const struct got_error *
6812 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6814 const struct got_error *err;
6815 struct got_commit_object *commit = NULL;
6816 char *id_str = NULL, *logmsg = NULL;
6818 err = got_object_open_as_commit(&commit, repo, id);
6822 err = got_object_id_str(&id_str, id);
6828 err = get_short_logmsg(&logmsg, 42, commit);
6832 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6835 got_object_commit_close(commit);
6840 static const struct got_error *
6841 show_rebase_progress(struct got_commit_object *commit,
6842 struct got_object_id *old_id, struct got_object_id *new_id)
6844 const struct got_error *err;
6845 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6847 err = got_object_id_str(&old_id_str, old_id);
6852 err = got_object_id_str(&new_id_str, new_id);
6857 old_id_str[12] = '\0';
6859 new_id_str[12] = '\0';
6861 err = get_short_logmsg(&logmsg, 42, commit);
6865 printf("%s -> %s: %s\n", old_id_str,
6866 new_id_str ? new_id_str : "no-op change", logmsg);
6874 static const struct got_error *
6875 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6876 struct got_reference *branch, struct got_reference *new_base_branch,
6877 struct got_reference *tmp_branch, struct got_repository *repo)
6879 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6880 return got_worktree_rebase_complete(worktree, fileindex,
6881 new_base_branch, tmp_branch, branch, repo);
6884 static const struct got_error *
6885 rebase_commit(struct got_pathlist_head *merged_paths,
6886 struct got_worktree *worktree, struct got_fileindex *fileindex,
6887 struct got_reference *tmp_branch,
6888 struct got_object_id *commit_id, struct got_repository *repo)
6890 const struct got_error *error;
6891 struct got_commit_object *commit;
6892 struct got_object_id *new_commit_id;
6894 error = got_object_open_as_commit(&commit, repo, commit_id);
6898 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6899 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6901 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6903 error = show_rebase_progress(commit, commit_id, NULL);
6905 error = show_rebase_progress(commit, commit_id, new_commit_id);
6906 free(new_commit_id);
6909 got_object_commit_close(commit);
6913 struct check_path_prefix_arg {
6914 const char *path_prefix;
6919 static const struct got_error *
6920 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6921 struct got_blob_object *blob2, struct got_object_id *id1,
6922 struct got_object_id *id2, const char *path1, const char *path2,
6923 mode_t mode1, mode_t mode2, struct got_repository *repo)
6925 struct check_path_prefix_arg *a = arg;
6927 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6928 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6929 return got_error(a->errcode);
6934 static const struct got_error *
6935 check_path_prefix(struct got_object_id *parent_id,
6936 struct got_object_id *commit_id, const char *path_prefix,
6937 int errcode, struct got_repository *repo)
6939 const struct got_error *err;
6940 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6941 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6942 struct check_path_prefix_arg cpp_arg;
6944 if (got_path_is_root_dir(path_prefix))
6947 err = got_object_open_as_commit(&commit, repo, commit_id);
6951 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6955 err = got_object_open_as_tree(&tree1, repo,
6956 got_object_commit_get_tree_id(parent_commit));
6960 err = got_object_open_as_tree(&tree2, repo,
6961 got_object_commit_get_tree_id(commit));
6965 cpp_arg.path_prefix = path_prefix;
6966 while (cpp_arg.path_prefix[0] == '/')
6967 cpp_arg.path_prefix++;
6968 cpp_arg.len = strlen(cpp_arg.path_prefix);
6969 cpp_arg.errcode = errcode;
6970 err = got_diff_tree(tree1, tree2, "", "", repo,
6971 check_path_prefix_in_diff, &cpp_arg, 0);
6974 got_object_tree_close(tree1);
6976 got_object_tree_close(tree2);
6978 got_object_commit_close(commit);
6980 got_object_commit_close(parent_commit);
6984 static const struct got_error *
6985 collect_commits(struct got_object_id_queue *commits,
6986 struct got_object_id *initial_commit_id,
6987 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6988 const char *path_prefix, int path_prefix_errcode,
6989 struct got_repository *repo)
6991 const struct got_error *err = NULL;
6992 struct got_commit_graph *graph = NULL;
6993 struct got_object_id *parent_id = NULL;
6994 struct got_object_qid *qid;
6995 struct got_object_id *commit_id = initial_commit_id;
6997 err = got_commit_graph_open(&graph, "/", 1);
7001 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7002 check_cancelled, NULL);
7005 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7006 err = got_commit_graph_iter_next(&parent_id, graph, repo,
7007 check_cancelled, NULL);
7009 if (err->code == GOT_ERR_ITER_COMPLETED) {
7010 err = got_error_msg(GOT_ERR_ANCESTRY,
7011 "ran out of commits to rebase before "
7012 "youngest common ancestor commit has "
7017 err = check_path_prefix(parent_id, commit_id,
7018 path_prefix, path_prefix_errcode, repo);
7022 err = got_object_qid_alloc(&qid, commit_id);
7025 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7026 commit_id = parent_id;
7030 got_commit_graph_close(graph);
7034 static const struct got_error *
7035 cmd_rebase(int argc, char *argv[])
7037 const struct got_error *error = NULL;
7038 struct got_worktree *worktree = NULL;
7039 struct got_repository *repo = NULL;
7040 struct got_fileindex *fileindex = NULL;
7042 struct got_reference *branch = NULL;
7043 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7044 struct got_object_id *commit_id = NULL, *parent_id = NULL;
7045 struct got_object_id *resume_commit_id = NULL;
7046 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7047 struct got_commit_object *commit = NULL;
7048 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7049 int histedit_in_progress = 0;
7050 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7051 struct got_object_id_queue commits;
7052 struct got_pathlist_head merged_paths;
7053 const struct got_object_id_queue *parent_ids;
7054 struct got_object_qid *qid, *pid;
7056 SIMPLEQ_INIT(&commits);
7057 TAILQ_INIT(&merged_paths);
7059 while ((ch = getopt(argc, argv, "ac")) != -1) {
7065 continue_rebase = 1;
7077 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7078 "unveil", NULL) == -1)
7081 if (abort_rebase && continue_rebase)
7083 else if (abort_rebase || continue_rebase) {
7086 } else if (argc != 1)
7089 cwd = getcwd(NULL, 0);
7091 error = got_error_from_errno("getcwd");
7094 error = got_worktree_open(&worktree, cwd);
7096 if (error->code == GOT_ERR_NOT_WORKTREE)
7097 error = wrap_not_worktree_error(error, "rebase", cwd);
7101 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7106 error = apply_unveil(got_repo_get_path(repo), 0,
7107 got_worktree_get_root_path(worktree));
7111 error = got_worktree_histedit_in_progress(&histedit_in_progress,
7115 if (histedit_in_progress) {
7116 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7120 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7125 struct got_update_progress_arg upa;
7126 if (!rebase_in_progress) {
7127 error = got_error(GOT_ERR_NOT_REBASING);
7130 error = got_worktree_rebase_continue(&resume_commit_id,
7131 &new_base_branch, &tmp_branch, &branch, &fileindex,
7135 printf("Switching work tree to %s\n",
7136 got_ref_get_symref_target(new_base_branch));
7137 memset(&upa, 0, sizeof(upa));
7138 error = got_worktree_rebase_abort(worktree, fileindex, repo,
7139 new_base_branch, update_progress, &upa);
7142 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7143 print_update_progress_stats(&upa);
7144 goto done; /* nothing else to do */
7147 if (continue_rebase) {
7148 if (!rebase_in_progress) {
7149 error = got_error(GOT_ERR_NOT_REBASING);
7152 error = got_worktree_rebase_continue(&resume_commit_id,
7153 &new_base_branch, &tmp_branch, &branch, &fileindex,
7158 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7159 resume_commit_id, repo);
7163 yca_id = got_object_id_dup(resume_commit_id);
7164 if (yca_id == NULL) {
7165 error = got_error_from_errno("got_object_id_dup");
7169 error = got_ref_open(&branch, repo, argv[0], 0);
7174 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7178 if (!continue_rebase) {
7179 struct got_object_id *base_commit_id;
7181 base_commit_id = got_worktree_get_base_commit_id(worktree);
7182 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7183 base_commit_id, branch_head_commit_id, repo,
7184 check_cancelled, NULL);
7187 if (yca_id == NULL) {
7188 error = got_error_msg(GOT_ERR_ANCESTRY,
7189 "specified branch shares no common ancestry "
7190 "with work tree's branch");
7194 error = check_same_branch(base_commit_id, branch, yca_id, repo);
7196 if (error->code != GOT_ERR_ANCESTRY)
7200 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7201 "specified branch resolves to a commit which "
7202 "is already contained in work tree's branch");
7205 error = got_worktree_rebase_prepare(&new_base_branch,
7206 &tmp_branch, &fileindex, worktree, branch, repo);
7211 commit_id = branch_head_commit_id;
7212 error = got_object_open_as_commit(&commit, repo, commit_id);
7216 parent_ids = got_object_commit_get_parent_ids(commit);
7217 pid = SIMPLEQ_FIRST(parent_ids);
7219 if (!continue_rebase) {
7220 struct got_update_progress_arg upa;
7221 memset(&upa, 0, sizeof(upa));
7222 error = got_worktree_rebase_abort(worktree, fileindex,
7223 repo, new_base_branch, update_progress, &upa);
7226 printf("Rebase of %s aborted\n",
7227 got_ref_get_name(branch));
7228 print_update_progress_stats(&upa);
7231 error = got_error(GOT_ERR_EMPTY_REBASE);
7234 error = collect_commits(&commits, commit_id, pid->id,
7235 yca_id, got_worktree_get_path_prefix(worktree),
7236 GOT_ERR_REBASE_PATH, repo);
7237 got_object_commit_close(commit);
7242 if (SIMPLEQ_EMPTY(&commits)) {
7243 if (continue_rebase) {
7244 error = rebase_complete(worktree, fileindex,
7245 branch, new_base_branch, tmp_branch, repo);
7248 /* Fast-forward the reference of the branch. */
7249 struct got_object_id *new_head_commit_id;
7251 error = got_ref_resolve(&new_head_commit_id, repo,
7255 error = got_object_id_str(&id_str, new_head_commit_id);
7256 printf("Forwarding %s to commit %s\n",
7257 got_ref_get_name(branch), id_str);
7259 error = got_ref_change_ref(branch,
7260 new_head_commit_id);
7267 SIMPLEQ_FOREACH(qid, &commits, entry) {
7268 struct got_update_progress_arg upa;
7270 commit_id = qid->id;
7271 parent_id = pid ? pid->id : yca_id;
7274 memset(&upa, 0, sizeof(upa));
7275 error = got_worktree_rebase_merge_files(&merged_paths,
7276 worktree, fileindex, parent_id, commit_id, repo,
7277 update_progress, &upa, check_cancelled, NULL);
7281 print_update_progress_stats(&upa);
7282 if (upa.conflicts > 0)
7283 rebase_status = GOT_STATUS_CONFLICT;
7285 if (rebase_status == GOT_STATUS_CONFLICT) {
7286 error = show_rebase_merge_conflict(qid->id, repo);
7289 got_worktree_rebase_pathlist_free(&merged_paths);
7293 error = rebase_commit(&merged_paths, worktree, fileindex,
7294 tmp_branch, commit_id, repo);
7295 got_worktree_rebase_pathlist_free(&merged_paths);
7300 if (rebase_status == GOT_STATUS_CONFLICT) {
7301 error = got_worktree_rebase_postpone(worktree, fileindex);
7304 error = got_error_msg(GOT_ERR_CONFLICTS,
7305 "conflicts must be resolved before rebasing can continue");
7307 error = rebase_complete(worktree, fileindex, branch,
7308 new_base_branch, tmp_branch, repo);
7310 got_object_id_queue_free(&commits);
7311 free(branch_head_commit_id);
7312 free(resume_commit_id);
7315 got_object_commit_close(commit);
7317 got_ref_close(branch);
7318 if (new_base_branch)
7319 got_ref_close(new_base_branch);
7321 got_ref_close(tmp_branch);
7323 got_worktree_close(worktree);
7325 got_repo_close(repo);
7330 usage_histedit(void)
7332 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7337 #define GOT_HISTEDIT_PICK 'p'
7338 #define GOT_HISTEDIT_EDIT 'e'
7339 #define GOT_HISTEDIT_FOLD 'f'
7340 #define GOT_HISTEDIT_DROP 'd'
7341 #define GOT_HISTEDIT_MESG 'm'
7343 static struct got_histedit_cmd {
7347 } got_histedit_cmds[] = {
7348 { GOT_HISTEDIT_PICK, "pick", "use commit" },
7349 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7350 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7352 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7353 { GOT_HISTEDIT_MESG, "mesg",
7354 "single-line log message for commit above (open editor if empty)" },
7357 struct got_histedit_list_entry {
7358 TAILQ_ENTRY(got_histedit_list_entry) entry;
7359 struct got_object_id *commit_id;
7360 const struct got_histedit_cmd *cmd;
7363 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7365 static const struct got_error *
7366 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7367 FILE *f, struct got_repository *repo)
7369 const struct got_error *err = NULL;
7370 char *logmsg = NULL, *id_str = NULL;
7371 struct got_commit_object *commit = NULL;
7374 err = got_object_open_as_commit(&commit, repo, commit_id);
7378 err = get_short_logmsg(&logmsg, 34, commit);
7382 err = got_object_id_str(&id_str, commit_id);
7386 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7388 err = got_ferror(f, GOT_ERR_IO);
7391 got_object_commit_close(commit);
7397 static const struct got_error *
7398 histedit_write_commit_list(struct got_object_id_queue *commits,
7399 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7401 const struct got_error *err = NULL;
7402 struct got_object_qid *qid;
7404 if (SIMPLEQ_EMPTY(commits))
7405 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7407 SIMPLEQ_FOREACH(qid, commits, entry) {
7408 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7412 if (edit_logmsg_only) {
7413 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7415 err = got_ferror(f, GOT_ERR_IO);
7424 static const struct got_error *
7425 write_cmd_list(FILE *f, const char *branch_name,
7426 struct got_object_id_queue *commits)
7428 const struct got_error *err = NULL;
7431 struct got_object_qid *qid;
7433 qid = SIMPLEQ_FIRST(commits);
7434 err = got_object_id_str(&id_str, qid->id);
7439 "# Editing the history of branch '%s' starting at\n"
7441 "# Commits will be processed in order from top to "
7442 "bottom of this file.\n", branch_name, id_str);
7444 err = got_ferror(f, GOT_ERR_IO);
7448 n = fprintf(f, "# Available histedit commands:\n");
7450 err = got_ferror(f, GOT_ERR_IO);
7454 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7455 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7456 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7459 err = got_ferror(f, GOT_ERR_IO);
7468 static const struct got_error *
7469 histedit_syntax_error(int lineno)
7471 static char msg[42];
7474 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7476 if (ret == -1 || ret >= sizeof(msg))
7477 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7479 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7482 static const struct got_error *
7483 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7484 char *logmsg, struct got_repository *repo)
7486 const struct got_error *err;
7487 struct got_commit_object *folded_commit = NULL;
7488 char *id_str, *folded_logmsg = NULL;
7490 err = got_object_id_str(&id_str, hle->commit_id);
7494 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7498 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7501 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7502 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7503 folded_logmsg) == -1) {
7504 err = got_error_from_errno("asprintf");
7508 got_object_commit_close(folded_commit);
7510 free(folded_logmsg);
7514 static struct got_histedit_list_entry *
7515 get_folded_commits(struct got_histedit_list_entry *hle)
7517 struct got_histedit_list_entry *prev, *folded = NULL;
7519 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7520 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7521 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7522 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7524 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7530 static const struct got_error *
7531 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7532 struct got_repository *repo)
7534 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7535 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7536 const struct got_error *err = NULL;
7537 struct got_commit_object *commit = NULL;
7539 struct got_histedit_list_entry *folded = NULL;
7541 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7545 folded = get_folded_commits(hle);
7547 while (folded != hle) {
7548 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7549 folded = TAILQ_NEXT(folded, entry);
7552 err = append_folded_commit_msg(&new_msg, folded,
7558 folded = TAILQ_NEXT(folded, entry);
7562 err = got_object_id_str(&id_str, hle->commit_id);
7565 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7568 if (asprintf(&new_msg,
7569 "%s\n# original log message of commit %s: %s",
7570 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7571 err = got_error_from_errno("asprintf");
7577 err = got_object_id_str(&id_str, hle->commit_id);
7581 err = got_opentemp_named_fd(&logmsg_path, &fd,
7582 GOT_TMPDIR_STR "/got-logmsg");
7586 dprintf(fd, logmsg);
7589 err = get_editor(&editor);
7593 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7595 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7597 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7600 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7601 err = got_error_from_errno2("unlink", logmsg_path);
7607 got_object_commit_close(commit);
7611 static const struct got_error *
7612 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7613 FILE *f, struct got_repository *repo)
7615 const struct got_error *err = NULL;
7616 char *line = NULL, *p, *end;
7620 const struct got_histedit_cmd *cmd;
7621 struct got_object_id *commit_id = NULL;
7622 struct got_histedit_list_entry *hle = NULL;
7625 len = getline(&line, &size, f);
7627 const struct got_error *getline_err;
7630 getline_err = got_error_from_errno("getline");
7631 err = got_ferror(f, getline_err->code);
7636 while (isspace((unsigned char)p[0]))
7638 if (p[0] == '#' || p[0] == '\0') {
7644 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7645 cmd = &got_histedit_cmds[i];
7646 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7647 isspace((unsigned char)p[strlen(cmd->name)])) {
7648 p += strlen(cmd->name);
7651 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7656 if (i == nitems(got_histedit_cmds)) {
7657 err = histedit_syntax_error(lineno);
7660 while (isspace((unsigned char)p[0]))
7662 if (cmd->code == GOT_HISTEDIT_MESG) {
7663 if (hle == NULL || hle->logmsg != NULL) {
7664 err = got_error(GOT_ERR_HISTEDIT_CMD);
7668 err = histedit_edit_logmsg(hle, repo);
7672 hle->logmsg = strdup(p);
7673 if (hle->logmsg == NULL) {
7674 err = got_error_from_errno("strdup");
7683 while (end[0] && !isspace((unsigned char)end[0]))
7687 err = got_object_resolve_id_str(&commit_id, repo, p);
7689 /* override error code */
7690 err = histedit_syntax_error(lineno);
7694 hle = malloc(sizeof(*hle));
7696 err = got_error_from_errno("malloc");
7700 hle->commit_id = commit_id;
7705 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7713 static const struct got_error *
7714 histedit_check_script(struct got_histedit_list *histedit_cmds,
7715 struct got_object_id_queue *commits, struct got_repository *repo)
7717 const struct got_error *err = NULL;
7718 struct got_object_qid *qid;
7719 struct got_histedit_list_entry *hle;
7720 static char msg[92];
7723 if (TAILQ_EMPTY(histedit_cmds))
7724 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7725 "histedit script contains no commands");
7726 if (SIMPLEQ_EMPTY(commits))
7727 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7729 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7730 struct got_histedit_list_entry *hle2;
7731 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7734 if (got_object_id_cmp(hle->commit_id,
7735 hle2->commit_id) != 0)
7737 err = got_object_id_str(&id_str, hle->commit_id);
7740 snprintf(msg, sizeof(msg), "commit %s is listed "
7741 "more than once in histedit script", id_str);
7743 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7747 SIMPLEQ_FOREACH(qid, commits, entry) {
7748 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7749 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7753 err = got_object_id_str(&id_str, qid->id);
7756 snprintf(msg, sizeof(msg),
7757 "commit %s missing from histedit script", id_str);
7759 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7763 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7764 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7765 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7766 "last commit in histedit script cannot be folded");
7771 static const struct got_error *
7772 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7773 const char *path, struct got_object_id_queue *commits,
7774 struct got_repository *repo)
7776 const struct got_error *err = NULL;
7780 err = get_editor(&editor);
7784 if (spawn_editor(editor, path) == -1) {
7785 err = got_error_from_errno("failed spawning editor");
7789 f = fopen(path, "r");
7791 err = got_error_from_errno("fopen");
7794 err = histedit_parse_list(histedit_cmds, f, repo);
7798 err = histedit_check_script(histedit_cmds, commits, repo);
7800 if (f && fclose(f) != 0 && err == NULL)
7801 err = got_error_from_errno("fclose");
7806 static const struct got_error *
7807 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7808 struct got_object_id_queue *, const char *, const char *,
7809 struct got_repository *);
7811 static const struct got_error *
7812 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7813 struct got_object_id_queue *commits, const char *branch_name,
7814 int edit_logmsg_only, struct got_repository *repo)
7816 const struct got_error *err;
7820 err = got_opentemp_named(&path, &f, "got-histedit");
7824 err = write_cmd_list(f, branch_name, commits);
7828 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7832 if (edit_logmsg_only) {
7834 err = histedit_parse_list(histedit_cmds, f, repo);
7836 if (fclose(f) != 0) {
7837 err = got_error_from_errno("fclose");
7841 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7843 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7844 err->code != GOT_ERR_HISTEDIT_CMD)
7846 err = histedit_edit_list_retry(histedit_cmds, err,
7847 commits, path, branch_name, repo);
7851 if (f && fclose(f) != 0 && err == NULL)
7852 err = got_error_from_errno("fclose");
7853 if (path && unlink(path) != 0 && err == NULL)
7854 err = got_error_from_errno2("unlink", path);
7859 static const struct got_error *
7860 histedit_save_list(struct got_histedit_list *histedit_cmds,
7861 struct got_worktree *worktree, struct got_repository *repo)
7863 const struct got_error *err = NULL;
7866 struct got_histedit_list_entry *hle;
7867 struct got_commit_object *commit = NULL;
7869 err = got_worktree_get_histedit_script_path(&path, worktree);
7873 f = fopen(path, "w");
7875 err = got_error_from_errno2("fopen", path);
7878 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7879 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7885 int n = fprintf(f, "%c %s\n",
7886 GOT_HISTEDIT_MESG, hle->logmsg);
7888 err = got_ferror(f, GOT_ERR_IO);
7894 if (f && fclose(f) != 0 && err == NULL)
7895 err = got_error_from_errno("fclose");
7898 got_object_commit_close(commit);
7903 histedit_free_list(struct got_histedit_list *histedit_cmds)
7905 struct got_histedit_list_entry *hle;
7907 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7908 TAILQ_REMOVE(histedit_cmds, hle, entry);
7913 static const struct got_error *
7914 histedit_load_list(struct got_histedit_list *histedit_cmds,
7915 const char *path, struct got_repository *repo)
7917 const struct got_error *err = NULL;
7920 f = fopen(path, "r");
7922 err = got_error_from_errno2("fopen", path);
7926 err = histedit_parse_list(histedit_cmds, f, repo);
7928 if (f && fclose(f) != 0 && err == NULL)
7929 err = got_error_from_errno("fclose");
7933 static const struct got_error *
7934 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7935 const struct got_error *edit_err, struct got_object_id_queue *commits,
7936 const char *path, const char *branch_name, struct got_repository *repo)
7938 const struct got_error *err = NULL, *prev_err = edit_err;
7941 while (resp != 'c' && resp != 'r' && resp != 'a') {
7942 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7943 "or (a)bort: ", getprogname(), prev_err->msg);
7948 histedit_free_list(histedit_cmds);
7949 err = histedit_run_editor(histedit_cmds, path, commits,
7952 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7953 err->code != GOT_ERR_HISTEDIT_CMD)
7960 } else if (resp == 'r') {
7961 histedit_free_list(histedit_cmds);
7962 err = histedit_edit_script(histedit_cmds,
7963 commits, branch_name, 0, repo);
7965 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7966 err->code != GOT_ERR_HISTEDIT_CMD)
7973 } else if (resp == 'a') {
7974 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7977 printf("invalid response '%c'\n", resp);
7983 static const struct got_error *
7984 histedit_complete(struct got_worktree *worktree,
7985 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7986 struct got_reference *branch, struct got_repository *repo)
7988 printf("Switching work tree to %s\n",
7989 got_ref_get_symref_target(branch));
7990 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7994 static const struct got_error *
7995 show_histedit_progress(struct got_commit_object *commit,
7996 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7998 const struct got_error *err;
7999 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8001 err = got_object_id_str(&old_id_str, hle->commit_id);
8006 err = got_object_id_str(&new_id_str, new_id);
8011 old_id_str[12] = '\0';
8013 new_id_str[12] = '\0';
8016 logmsg = strdup(hle->logmsg);
8017 if (logmsg == NULL) {
8018 err = got_error_from_errno("strdup");
8021 trim_logmsg(logmsg, 42);
8023 err = get_short_logmsg(&logmsg, 42, commit);
8028 switch (hle->cmd->code) {
8029 case GOT_HISTEDIT_PICK:
8030 case GOT_HISTEDIT_EDIT:
8031 printf("%s -> %s: %s\n", old_id_str,
8032 new_id_str ? new_id_str : "no-op change", logmsg);
8034 case GOT_HISTEDIT_DROP:
8035 case GOT_HISTEDIT_FOLD:
8036 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8048 static const struct got_error *
8049 histedit_commit(struct got_pathlist_head *merged_paths,
8050 struct got_worktree *worktree, struct got_fileindex *fileindex,
8051 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8052 struct got_repository *repo)
8054 const struct got_error *err;
8055 struct got_commit_object *commit;
8056 struct got_object_id *new_commit_id;
8058 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8059 && hle->logmsg == NULL) {
8060 err = histedit_edit_logmsg(hle, repo);
8065 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8069 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8070 worktree, fileindex, tmp_branch, commit, hle->commit_id,
8073 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8075 err = show_histedit_progress(commit, hle, NULL);
8077 err = show_histedit_progress(commit, hle, new_commit_id);
8078 free(new_commit_id);
8081 got_object_commit_close(commit);
8085 static const struct got_error *
8086 histedit_skip_commit(struct got_histedit_list_entry *hle,
8087 struct got_worktree *worktree, struct got_repository *repo)
8089 const struct got_error *error;
8090 struct got_commit_object *commit;
8092 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8097 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8101 error = show_histedit_progress(commit, hle, NULL);
8102 got_object_commit_close(commit);
8106 static const struct got_error *
8107 check_local_changes(void *arg, unsigned char status,
8108 unsigned char staged_status, const char *path,
8109 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8110 struct got_object_id *commit_id, int dirfd, const char *de_name)
8112 int *have_local_changes = arg;
8115 case GOT_STATUS_ADD:
8116 case GOT_STATUS_DELETE:
8117 case GOT_STATUS_MODIFY:
8118 case GOT_STATUS_CONFLICT:
8119 *have_local_changes = 1;
8120 return got_error(GOT_ERR_CANCELLED);
8125 switch (staged_status) {
8126 case GOT_STATUS_ADD:
8127 case GOT_STATUS_DELETE:
8128 case GOT_STATUS_MODIFY:
8129 *have_local_changes = 1;
8130 return got_error(GOT_ERR_CANCELLED);
8138 static const struct got_error *
8139 cmd_histedit(int argc, char *argv[])
8141 const struct got_error *error = NULL;
8142 struct got_worktree *worktree = NULL;
8143 struct got_fileindex *fileindex = NULL;
8144 struct got_repository *repo = NULL;
8146 struct got_reference *branch = NULL;
8147 struct got_reference *tmp_branch = NULL;
8148 struct got_object_id *resume_commit_id = NULL;
8149 struct got_object_id *base_commit_id = NULL;
8150 struct got_object_id *head_commit_id = NULL;
8151 struct got_commit_object *commit = NULL;
8152 int ch, rebase_in_progress = 0;
8153 struct got_update_progress_arg upa;
8154 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8155 int edit_logmsg_only = 0;
8156 const char *edit_script_path = NULL;
8157 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8158 struct got_object_id_queue commits;
8159 struct got_pathlist_head merged_paths;
8160 const struct got_object_id_queue *parent_ids;
8161 struct got_object_qid *pid;
8162 struct got_histedit_list histedit_cmds;
8163 struct got_histedit_list_entry *hle;
8165 SIMPLEQ_INIT(&commits);
8166 TAILQ_INIT(&histedit_cmds);
8167 TAILQ_INIT(&merged_paths);
8168 memset(&upa, 0, sizeof(upa));
8170 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8179 edit_script_path = optarg;
8182 edit_logmsg_only = 1;
8194 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8195 "unveil", NULL) == -1)
8198 if (abort_edit && continue_edit)
8199 errx(1, "histedit's -a and -c options are mutually exclusive");
8200 if (edit_script_path && edit_logmsg_only)
8201 errx(1, "histedit's -F and -m options are mutually exclusive");
8202 if (abort_edit && edit_logmsg_only)
8203 errx(1, "histedit's -a and -m options are mutually exclusive");
8204 if (continue_edit && edit_logmsg_only)
8205 errx(1, "histedit's -c and -m options are mutually exclusive");
8210 * This command cannot apply unveil(2) in all cases because the
8211 * user may choose to run an editor to edit the histedit script
8212 * and to edit individual commit log messages.
8213 * unveil(2) traverses exec(2); if an editor is used we have to
8214 * apply unveil after edit script and log messages have been written.
8215 * XXX TODO: Make use of unveil(2) where possible.
8218 cwd = getcwd(NULL, 0);
8220 error = got_error_from_errno("getcwd");
8223 error = got_worktree_open(&worktree, cwd);
8225 if (error->code == GOT_ERR_NOT_WORKTREE)
8226 error = wrap_not_worktree_error(error, "histedit", cwd);
8230 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8235 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8238 if (rebase_in_progress) {
8239 error = got_error(GOT_ERR_REBASING);
8243 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8247 if (edit_in_progress && edit_logmsg_only) {
8248 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8249 "histedit operation is in progress in this "
8250 "work tree and must be continued or aborted "
8251 "before the -m option can be used");
8255 if (edit_in_progress && abort_edit) {
8256 error = got_worktree_histedit_continue(&resume_commit_id,
8257 &tmp_branch, &branch, &base_commit_id, &fileindex,
8261 printf("Switching work tree to %s\n",
8262 got_ref_get_symref_target(branch));
8263 error = got_worktree_histedit_abort(worktree, fileindex, repo,
8264 branch, base_commit_id, update_progress, &upa);
8267 printf("Histedit of %s aborted\n",
8268 got_ref_get_symref_target(branch));
8269 print_update_progress_stats(&upa);
8270 goto done; /* nothing else to do */
8271 } else if (abort_edit) {
8272 error = got_error(GOT_ERR_NOT_HISTEDIT);
8276 if (continue_edit) {
8279 if (!edit_in_progress) {
8280 error = got_error(GOT_ERR_NOT_HISTEDIT);
8284 error = got_worktree_get_histedit_script_path(&path, worktree);
8288 error = histedit_load_list(&histedit_cmds, path, repo);
8293 error = got_worktree_histedit_continue(&resume_commit_id,
8294 &tmp_branch, &branch, &base_commit_id, &fileindex,
8299 error = got_ref_resolve(&head_commit_id, repo, branch);
8303 error = got_object_open_as_commit(&commit, repo,
8307 parent_ids = got_object_commit_get_parent_ids(commit);
8308 pid = SIMPLEQ_FIRST(parent_ids);
8310 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8313 error = collect_commits(&commits, head_commit_id, pid->id,
8314 base_commit_id, got_worktree_get_path_prefix(worktree),
8315 GOT_ERR_HISTEDIT_PATH, repo);
8316 got_object_commit_close(commit);
8321 if (edit_in_progress) {
8322 error = got_error(GOT_ERR_HISTEDIT_BUSY);
8326 error = got_ref_open(&branch, repo,
8327 got_worktree_get_head_ref_name(worktree), 0);
8331 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8332 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8333 "will not edit commit history of a branch outside "
8334 "the \"refs/heads/\" reference namespace");
8338 error = got_ref_resolve(&head_commit_id, repo, branch);
8339 got_ref_close(branch);
8344 error = got_object_open_as_commit(&commit, repo,
8348 parent_ids = got_object_commit_get_parent_ids(commit);
8349 pid = SIMPLEQ_FIRST(parent_ids);
8351 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8354 error = collect_commits(&commits, head_commit_id, pid->id,
8355 got_worktree_get_base_commit_id(worktree),
8356 got_worktree_get_path_prefix(worktree),
8357 GOT_ERR_HISTEDIT_PATH, repo);
8358 got_object_commit_close(commit);
8363 if (SIMPLEQ_EMPTY(&commits)) {
8364 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8368 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8369 &base_commit_id, &fileindex, worktree, repo);
8373 if (edit_script_path) {
8374 error = histedit_load_list(&histedit_cmds,
8375 edit_script_path, repo);
8377 got_worktree_histedit_abort(worktree, fileindex,
8378 repo, branch, base_commit_id,
8379 update_progress, &upa);
8380 print_update_progress_stats(&upa);
8384 const char *branch_name;
8385 branch_name = got_ref_get_symref_target(branch);
8386 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8388 error = histedit_edit_script(&histedit_cmds, &commits,
8389 branch_name, edit_logmsg_only, repo);
8391 got_worktree_histedit_abort(worktree, fileindex,
8392 repo, branch, base_commit_id,
8393 update_progress, &upa);
8394 print_update_progress_stats(&upa);
8400 error = histedit_save_list(&histedit_cmds, worktree,
8403 got_worktree_histedit_abort(worktree, fileindex,
8404 repo, branch, base_commit_id,
8405 update_progress, &upa);
8406 print_update_progress_stats(&upa);
8412 error = histedit_check_script(&histedit_cmds, &commits, repo);
8416 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8417 if (resume_commit_id) {
8418 if (got_object_id_cmp(hle->commit_id,
8419 resume_commit_id) != 0)
8422 resume_commit_id = NULL;
8423 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8424 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8425 error = histedit_skip_commit(hle, worktree,
8430 struct got_pathlist_head paths;
8431 int have_changes = 0;
8434 error = got_pathlist_append(&paths, "", NULL);
8437 error = got_worktree_status(worktree, &paths,
8438 repo, check_local_changes, &have_changes,
8439 check_cancelled, NULL);
8440 got_pathlist_free(&paths);
8442 if (error->code != GOT_ERR_CANCELLED)
8444 if (sigint_received || sigpipe_received)
8448 error = histedit_commit(NULL, worktree,
8449 fileindex, tmp_branch, hle, repo);
8453 error = got_object_open_as_commit(
8454 &commit, repo, hle->commit_id);
8457 error = show_histedit_progress(commit,
8459 got_object_commit_close(commit);
8468 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8469 error = histedit_skip_commit(hle, worktree, repo);
8475 error = got_object_open_as_commit(&commit, repo,
8479 parent_ids = got_object_commit_get_parent_ids(commit);
8480 pid = SIMPLEQ_FIRST(parent_ids);
8482 error = got_worktree_histedit_merge_files(&merged_paths,
8483 worktree, fileindex, pid->id, hle->commit_id, repo,
8484 update_progress, &upa, check_cancelled, NULL);
8487 got_object_commit_close(commit);
8490 print_update_progress_stats(&upa);
8491 if (upa.conflicts > 0)
8492 rebase_status = GOT_STATUS_CONFLICT;
8494 if (rebase_status == GOT_STATUS_CONFLICT) {
8495 error = show_rebase_merge_conflict(hle->commit_id,
8499 got_worktree_rebase_pathlist_free(&merged_paths);
8503 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8505 error = got_object_id_str(&id_str, hle->commit_id);
8508 printf("Stopping histedit for amending commit %s\n",
8511 got_worktree_rebase_pathlist_free(&merged_paths);
8512 error = got_worktree_histedit_postpone(worktree,
8517 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8518 error = histedit_skip_commit(hle, worktree, repo);
8524 error = histedit_commit(&merged_paths, worktree, fileindex,
8525 tmp_branch, hle, repo);
8526 got_worktree_rebase_pathlist_free(&merged_paths);
8531 if (rebase_status == GOT_STATUS_CONFLICT) {
8532 error = got_worktree_histedit_postpone(worktree, fileindex);
8535 error = got_error_msg(GOT_ERR_CONFLICTS,
8536 "conflicts must be resolved before histedit can continue");
8538 error = histedit_complete(worktree, fileindex, tmp_branch,
8541 got_object_id_queue_free(&commits);
8542 histedit_free_list(&histedit_cmds);
8543 free(head_commit_id);
8544 free(base_commit_id);
8545 free(resume_commit_id);
8547 got_object_commit_close(commit);
8549 got_ref_close(branch);
8551 got_ref_close(tmp_branch);
8553 got_worktree_close(worktree);
8555 got_repo_close(repo);
8560 usage_integrate(void)
8562 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8566 static const struct got_error *
8567 cmd_integrate(int argc, char *argv[])
8569 const struct got_error *error = NULL;
8570 struct got_repository *repo = NULL;
8571 struct got_worktree *worktree = NULL;
8572 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8573 const char *branch_arg = NULL;
8574 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8575 struct got_fileindex *fileindex = NULL;
8576 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8578 struct got_update_progress_arg upa;
8580 while ((ch = getopt(argc, argv, "")) != -1) {
8593 branch_arg = argv[0];
8595 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8596 "unveil", NULL) == -1)
8599 cwd = getcwd(NULL, 0);
8601 error = got_error_from_errno("getcwd");
8605 error = got_worktree_open(&worktree, cwd);
8607 if (error->code == GOT_ERR_NOT_WORKTREE)
8608 error = wrap_not_worktree_error(error, "integrate",
8613 error = check_rebase_or_histedit_in_progress(worktree);
8617 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8622 error = apply_unveil(got_repo_get_path(repo), 0,
8623 got_worktree_get_root_path(worktree));
8627 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8628 error = got_error_from_errno("asprintf");
8632 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8633 &base_branch_ref, worktree, refname, repo);
8637 refname = strdup(got_ref_get_name(branch_ref));
8638 if (refname == NULL) {
8639 error = got_error_from_errno("strdup");
8640 got_worktree_integrate_abort(worktree, fileindex, repo,
8641 branch_ref, base_branch_ref);
8644 base_refname = strdup(got_ref_get_name(base_branch_ref));
8645 if (base_refname == NULL) {
8646 error = got_error_from_errno("strdup");
8647 got_worktree_integrate_abort(worktree, fileindex, repo,
8648 branch_ref, base_branch_ref);
8652 error = got_ref_resolve(&commit_id, repo, branch_ref);
8656 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8660 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8661 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8662 "specified branch has already been integrated");
8663 got_worktree_integrate_abort(worktree, fileindex, repo,
8664 branch_ref, base_branch_ref);
8668 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8670 if (error->code == GOT_ERR_ANCESTRY)
8671 error = got_error(GOT_ERR_REBASE_REQUIRED);
8672 got_worktree_integrate_abort(worktree, fileindex, repo,
8673 branch_ref, base_branch_ref);
8677 memset(&upa, 0, sizeof(upa));
8678 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8679 branch_ref, base_branch_ref, update_progress, &upa,
8680 check_cancelled, NULL);
8684 printf("Integrated %s into %s\n", refname, base_refname);
8685 print_update_progress_stats(&upa);
8688 got_repo_close(repo);
8690 got_worktree_close(worktree);
8692 free(base_commit_id);
8702 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8703 "[file-path ...]\n",
8708 static const struct got_error *
8709 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8710 const char *path, struct got_object_id *blob_id,
8711 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8712 int dirfd, const char *de_name)
8714 const struct got_error *err = NULL;
8715 char *id_str = NULL;
8717 if (staged_status != GOT_STATUS_ADD &&
8718 staged_status != GOT_STATUS_MODIFY &&
8719 staged_status != GOT_STATUS_DELETE)
8722 if (staged_status == GOT_STATUS_ADD ||
8723 staged_status == GOT_STATUS_MODIFY)
8724 err = got_object_id_str(&id_str, staged_blob_id);
8726 err = got_object_id_str(&id_str, blob_id);
8730 printf("%s %c %s\n", id_str, staged_status, path);
8735 static const struct got_error *
8736 cmd_stage(int argc, char *argv[])
8738 const struct got_error *error = NULL;
8739 struct got_repository *repo = NULL;
8740 struct got_worktree *worktree = NULL;
8742 struct got_pathlist_head paths;
8743 struct got_pathlist_entry *pe;
8744 int ch, list_stage = 0, pflag = 0;
8745 FILE *patch_script_file = NULL;
8746 const char *patch_script_path = NULL;
8747 struct choose_patch_arg cpa;
8751 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8760 patch_script_path = optarg;
8772 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8773 "unveil", NULL) == -1)
8776 if (list_stage && (pflag || patch_script_path))
8777 errx(1, "-l option cannot be used with other options");
8778 if (patch_script_path && !pflag)
8779 errx(1, "-F option can only be used together with -p option");
8781 cwd = getcwd(NULL, 0);
8783 error = got_error_from_errno("getcwd");
8787 error = got_worktree_open(&worktree, cwd);
8789 if (error->code == GOT_ERR_NOT_WORKTREE)
8790 error = wrap_not_worktree_error(error, "stage", cwd);
8794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8799 if (patch_script_path) {
8800 patch_script_file = fopen(patch_script_path, "r");
8801 if (patch_script_file == NULL) {
8802 error = got_error_from_errno2("fopen",
8807 error = apply_unveil(got_repo_get_path(repo), 0,
8808 got_worktree_get_root_path(worktree));
8812 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8817 error = got_worktree_status(worktree, &paths, repo,
8818 print_stage, NULL, check_cancelled, NULL);
8820 cpa.patch_script_file = patch_script_file;
8821 cpa.action = "stage";
8822 error = got_worktree_stage(worktree, &paths,
8823 pflag ? NULL : print_status, NULL,
8824 pflag ? choose_patch : NULL, &cpa, repo);
8827 if (patch_script_file && fclose(patch_script_file) == EOF &&
8829 error = got_error_from_errno2("fclose", patch_script_path);
8831 got_repo_close(repo);
8833 got_worktree_close(worktree);
8834 TAILQ_FOREACH(pe, &paths, entry)
8835 free((char *)pe->path);
8836 got_pathlist_free(&paths);
8844 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8845 "[file-path ...]\n",
8851 static const struct got_error *
8852 cmd_unstage(int argc, char *argv[])
8854 const struct got_error *error = NULL;
8855 struct got_repository *repo = NULL;
8856 struct got_worktree *worktree = NULL;
8858 struct got_pathlist_head paths;
8859 struct got_pathlist_entry *pe;
8861 struct got_update_progress_arg upa;
8862 FILE *patch_script_file = NULL;
8863 const char *patch_script_path = NULL;
8864 struct choose_patch_arg cpa;
8868 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8874 patch_script_path = optarg;
8886 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8887 "unveil", NULL) == -1)
8890 if (patch_script_path && !pflag)
8891 errx(1, "-F option can only be used together with -p option");
8893 cwd = getcwd(NULL, 0);
8895 error = got_error_from_errno("getcwd");
8899 error = got_worktree_open(&worktree, cwd);
8901 if (error->code == GOT_ERR_NOT_WORKTREE)
8902 error = wrap_not_worktree_error(error, "unstage", cwd);
8906 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8911 if (patch_script_path) {
8912 patch_script_file = fopen(patch_script_path, "r");
8913 if (patch_script_file == NULL) {
8914 error = got_error_from_errno2("fopen",
8920 error = apply_unveil(got_repo_get_path(repo), 0,
8921 got_worktree_get_root_path(worktree));
8925 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8929 cpa.patch_script_file = patch_script_file;
8930 cpa.action = "unstage";
8931 memset(&upa, 0, sizeof(upa));
8932 error = got_worktree_unstage(worktree, &paths, update_progress,
8933 &upa, pflag ? choose_patch : NULL, &cpa, repo);
8935 print_update_progress_stats(&upa);
8937 if (patch_script_file && fclose(patch_script_file) == EOF &&
8939 error = got_error_from_errno2("fclose", patch_script_path);
8941 got_repo_close(repo);
8943 got_worktree_close(worktree);
8944 TAILQ_FOREACH(pe, &paths, entry)
8945 free((char *)pe->path);
8946 got_pathlist_free(&paths);
8954 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8955 "arg1 [arg2 ...]\n", getprogname());
8959 static const struct got_error *
8960 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8962 const struct got_error *err;
8963 struct got_blob_object *blob;
8965 err = got_object_open_as_blob(&blob, repo, id, 8192);
8969 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8970 got_object_blob_close(blob);
8974 static const struct got_error *
8975 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8977 const struct got_error *err;
8978 struct got_tree_object *tree;
8981 err = got_object_open_as_tree(&tree, repo, id);
8985 nentries = got_object_tree_get_nentries(tree);
8986 for (i = 0; i < nentries; i++) {
8987 struct got_tree_entry *te;
8989 if (sigint_received || sigpipe_received)
8991 te = got_object_tree_get_entry(tree, i);
8992 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8995 fprintf(outfile, "%s %.7o %s\n", id_str,
8996 got_tree_entry_get_mode(te),
8997 got_tree_entry_get_name(te));
9001 got_object_tree_close(tree);
9005 static const struct got_error *
9006 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9008 const struct got_error *err;
9009 struct got_commit_object *commit;
9010 const struct got_object_id_queue *parent_ids;
9011 struct got_object_qid *pid;
9012 char *id_str = NULL;
9013 const char *logmsg = NULL;
9015 err = got_object_open_as_commit(&commit, repo, id);
9019 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9023 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9024 parent_ids = got_object_commit_get_parent_ids(commit);
9025 fprintf(outfile, "numparents %d\n",
9026 got_object_commit_get_nparents(commit));
9027 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9029 err = got_object_id_str(&pid_str, pid->id);
9032 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9035 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9036 got_object_commit_get_author(commit),
9037 got_object_commit_get_author_time(commit));
9039 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9040 got_object_commit_get_author(commit),
9041 got_object_commit_get_committer_time(commit));
9043 logmsg = got_object_commit_get_logmsg_raw(commit);
9044 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9045 fprintf(outfile, "%s", logmsg);
9048 got_object_commit_close(commit);
9052 static const struct got_error *
9053 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9055 const struct got_error *err;
9056 struct got_tag_object *tag;
9057 char *id_str = NULL;
9058 const char *tagmsg = NULL;
9060 err = got_object_open_as_tag(&tag, repo, id);
9064 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9068 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9070 switch (got_object_tag_get_object_type(tag)) {
9071 case GOT_OBJ_TYPE_BLOB:
9072 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9073 GOT_OBJ_LABEL_BLOB);
9075 case GOT_OBJ_TYPE_TREE:
9076 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9077 GOT_OBJ_LABEL_TREE);
9079 case GOT_OBJ_TYPE_COMMIT:
9080 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9081 GOT_OBJ_LABEL_COMMIT);
9083 case GOT_OBJ_TYPE_TAG:
9084 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9091 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9092 got_object_tag_get_name(tag));
9094 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9095 got_object_tag_get_tagger(tag),
9096 got_object_tag_get_tagger_time(tag));
9098 tagmsg = got_object_tag_get_message(tag);
9099 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9100 fprintf(outfile, "%s", tagmsg);
9103 got_object_tag_close(tag);
9107 static const struct got_error *
9108 cmd_cat(int argc, char *argv[])
9110 const struct got_error *error;
9111 struct got_repository *repo = NULL;
9112 struct got_worktree *worktree = NULL;
9113 char *cwd = NULL, *repo_path = NULL, *label = NULL;
9114 const char *commit_id_str = NULL;
9115 struct got_object_id *id = NULL, *commit_id = NULL;
9116 int ch, obj_type, i, force_path = 0;
9119 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9124 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9127 commit_id_str = optarg;
9130 repo_path = realpath(optarg, NULL);
9131 if (repo_path == NULL)
9132 return got_error_from_errno2("realpath",
9134 got_path_strip_trailing_slashes(repo_path);
9148 cwd = getcwd(NULL, 0);
9150 error = got_error_from_errno("getcwd");
9153 error = got_worktree_open(&worktree, cwd);
9154 if (error && error->code != GOT_ERR_NOT_WORKTREE)
9157 if (repo_path == NULL) {
9159 got_worktree_get_repo_path(worktree));
9160 if (repo_path == NULL) {
9161 error = got_error_from_errno("strdup");
9167 if (repo_path == NULL) {
9168 repo_path = getcwd(NULL, 0);
9169 if (repo_path == NULL)
9170 return got_error_from_errno("getcwd");
9173 error = got_repo_open(&repo, repo_path, NULL);
9178 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9182 if (commit_id_str == NULL)
9183 commit_id_str = GOT_REF_HEAD;
9184 error = got_repo_match_object_id(&commit_id, NULL,
9185 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9189 for (i = 0; i < argc; i++) {
9191 error = got_object_id_by_path(&id, repo, commit_id,
9196 error = got_repo_match_object_id(&id, &label, argv[i],
9197 GOT_OBJ_TYPE_ANY, 0, repo);
9199 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9200 error->code != GOT_ERR_NOT_REF)
9202 error = got_object_id_by_path(&id, repo,
9203 commit_id, argv[i]);
9209 error = got_object_get_type(&obj_type, repo, id);
9214 case GOT_OBJ_TYPE_BLOB:
9215 error = cat_blob(id, repo, stdout);
9217 case GOT_OBJ_TYPE_TREE:
9218 error = cat_tree(id, repo, stdout);
9220 case GOT_OBJ_TYPE_COMMIT:
9221 error = cat_commit(id, repo, stdout);
9223 case GOT_OBJ_TYPE_TAG:
9224 error = cat_tag(id, repo, stdout);
9227 error = got_error(GOT_ERR_OBJ_TYPE);
9242 got_worktree_close(worktree);
9244 const struct got_error *repo_error;
9245 repo_error = got_repo_close(repo);