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);
1180 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 server_path, verbosity);
1186 printf("Connected to %s%s%s\n", host,
1187 port ? ":" : "", port ? port : "");
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 reference %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;
1512 err = got_ref_open(&symref, repo, refname, 1);
1516 if (strcmp(got_ref_get_symref_target(symref),
1517 got_ref_get_name(target_ref)) == 0)
1520 err = got_ref_change_symref(symref, got_ref_get_name(target_ref));
1524 err = got_ref_write(symref, repo);
1529 printf("Updated reference %s: %s\n", got_ref_get_name(symref),
1530 got_ref_get_symref_target(symref));
1532 unlock_err = got_ref_unlock(symref);
1533 if (unlock_err && err == NULL)
1535 got_ref_close(symref);
1543 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1544 "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1545 "[remote-repository-name]\n",
1550 static const struct got_error *
1551 delete_missing_refs(struct got_pathlist_head *their_refs,
1552 int verbosity, struct got_repository *repo)
1554 const struct got_error *err = NULL;
1555 struct got_reflist_head my_refs;
1556 struct got_reflist_entry *re;
1557 struct got_pathlist_entry *pe;
1558 struct got_object_id *id;
1561 SIMPLEQ_INIT(&my_refs);
1563 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1567 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1568 const char *refname = got_ref_get_name(re->ref);
1570 if (strncmp(refname, "refs/heads/", 11) != 0 &&
1571 strncmp(refname, "refs/tags/", 10) != 0)
1574 TAILQ_FOREACH(pe, their_refs, entry) {
1575 if (strcmp(refname, pe->path) == 0)
1581 err = got_ref_resolve(&id, repo, re->ref);
1584 err = got_object_id_str(&id_str, id);
1590 err = got_ref_delete(re->ref, repo);
1593 if (verbosity >= 0) {
1594 printf("Deleted reference %s: %s\n",
1595 got_ref_get_name(re->ref), id_str);
1602 static const struct got_error *
1603 update_wanted_ref(const char *refname, struct got_object_id *id,
1604 const char *remote_repo_name, int verbosity, struct got_repository *repo)
1606 const struct got_error *err, *unlock_err;
1607 char *remote_refname;
1608 struct got_reference *ref;
1610 if (strncmp("refs/", refname, 5) == 0)
1613 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1614 remote_repo_name, refname) == -1)
1615 return got_error_from_errno("asprintf");
1617 err = got_ref_open(&ref, repo, remote_refname, 1);
1619 if (err->code != GOT_ERR_NOT_REF)
1621 err = create_ref(remote_refname, id, verbosity, repo);
1623 err = update_ref(ref, id, 0, verbosity, repo);
1624 unlock_err = got_ref_unlock(ref);
1625 if (unlock_err && err == NULL)
1630 free(remote_refname);
1634 static const struct got_error *
1635 cmd_fetch(int argc, char *argv[])
1637 const struct got_error *error = NULL, *unlock_err;
1638 char *cwd = NULL, *repo_path = NULL;
1639 const char *remote_name;
1640 char *proto = NULL, *host = NULL, *port = NULL;
1641 char *repo_name = NULL, *server_path = NULL;
1642 struct got_remote_repo *remotes, *remote = NULL;
1644 char *id_str = NULL;
1645 struct got_repository *repo = NULL;
1646 struct got_worktree *worktree = NULL;
1647 struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1648 struct got_pathlist_entry *pe;
1649 struct got_object_id *pack_hash = NULL;
1650 int i, ch, fetchfd = -1, fetchstatus;
1651 pid_t fetchpid = -1;
1652 struct got_fetch_progress_arg fpa;
1653 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1654 int delete_refs = 0, replace_tags = 0;
1657 TAILQ_INIT(&symrefs);
1658 TAILQ_INIT(&wanted_branches);
1659 TAILQ_INIT(&wanted_refs);
1661 while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1664 fetch_all_branches = 1;
1667 error = got_pathlist_append(&wanted_branches,
1679 repo_path = realpath(optarg, NULL);
1680 if (repo_path == NULL)
1681 return got_error_from_errno2("realpath",
1683 got_path_strip_trailing_slashes(repo_path);
1691 else if (verbosity < 3)
1698 error = got_pathlist_append(&wanted_refs,
1711 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1712 errx(1, "-a and -b options are mutually exclusive");
1713 if (list_refs_only) {
1714 if (!TAILQ_EMPTY(&wanted_branches))
1715 errx(1, "-l and -b options are mutually exclusive");
1716 if (fetch_all_branches)
1717 errx(1, "-l and -a options are mutually exclusive");
1719 errx(1, "-l and -d options are mutually exclusive");
1720 if (verbosity == -1)
1721 errx(1, "-l and -q options are mutually exclusive");
1725 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1727 remote_name = argv[0];
1731 cwd = getcwd(NULL, 0);
1733 error = got_error_from_errno("getcwd");
1737 if (repo_path == NULL) {
1738 error = got_worktree_open(&worktree, cwd);
1739 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1745 strdup(got_worktree_get_repo_path(worktree));
1746 if (repo_path == NULL)
1747 error = got_error_from_errno("strdup");
1751 repo_path = strdup(cwd);
1752 if (repo_path == NULL) {
1753 error = got_error_from_errno("strdup");
1759 error = got_repo_open(&repo, repo_path, NULL);
1763 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1764 for (i = 0; i < nremotes; i++) {
1765 remote = &remotes[i];
1766 if (strcmp(remote->name, remote_name) == 0)
1769 if (i == nremotes) {
1770 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1774 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1775 &repo_name, remote->url);
1779 if (strcmp(proto, "git") == 0) {
1781 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1782 "sendfd dns inet unveil", NULL) == -1)
1785 } else if (strcmp(proto, "git+ssh") == 0 ||
1786 strcmp(proto, "ssh") == 0) {
1788 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1789 "sendfd unveil", NULL) == -1)
1792 } else if (strcmp(proto, "http") == 0 ||
1793 strcmp(proto, "git+http") == 0) {
1794 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1797 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1801 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1802 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1803 error = got_error_from_errno2("unveil",
1804 GOT_FETCH_PATH_SSH);
1808 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1812 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1813 server_path, verbosity);
1818 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1819 port ? ":" : "", port ? port : "");
1821 fpa.last_scaled_size[0] = '\0';
1822 fpa.last_p_indexed = -1;
1823 fpa.last_p_resolved = -1;
1824 fpa.verbosity = verbosity;
1825 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1826 remote->mirror_references, fetch_all_branches, &wanted_branches,
1827 &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1828 fetch_progress, &fpa);
1832 if (list_refs_only) {
1833 error = list_remote_refs(&symrefs, &refs);
1837 if (pack_hash == NULL) {
1839 printf("Already up-to-date\n");
1841 error = delete_missing_refs(&refs, verbosity, repo);
1845 if (verbosity >= 0) {
1846 error = got_object_id_str(&id_str, pack_hash);
1849 printf("\nFetched %s.pack\n", id_str);
1854 /* Update references provided with the pack file. */
1855 TAILQ_FOREACH(pe, &refs, entry) {
1856 const char *refname = pe->path;
1857 struct got_object_id *id = pe->data;
1858 struct got_reference *ref;
1859 char *remote_refname;
1861 if (is_wanted_ref(&wanted_refs, refname) &&
1862 !remote->mirror_references) {
1863 error = update_wanted_ref(refname, id,
1864 remote->name, verbosity, repo);
1870 if (remote->mirror_references ||
1871 strncmp("refs/tags/", refname, 10) == 0) {
1872 error = got_ref_open(&ref, repo, refname, 1);
1874 if (error->code != GOT_ERR_NOT_REF)
1876 error = create_ref(refname, id, verbosity,
1881 error = update_ref(ref, id, replace_tags,
1883 unlock_err = got_ref_unlock(ref);
1884 if (unlock_err && error == NULL)
1890 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1891 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1892 remote_name, refname + 11) == -1) {
1893 error = got_error_from_errno("asprintf");
1897 error = got_ref_open(&ref, repo, remote_refname, 1);
1899 if (error->code != GOT_ERR_NOT_REF)
1901 error = create_ref(remote_refname, id,
1906 error = update_ref(ref, id, replace_tags,
1908 unlock_err = got_ref_unlock(ref);
1909 if (unlock_err && error == NULL)
1916 /* Also create a local branch if none exists yet. */
1917 error = got_ref_open(&ref, repo, refname, 1);
1919 if (error->code != GOT_ERR_NOT_REF)
1921 error = create_ref(refname, id, verbosity,
1926 unlock_err = got_ref_unlock(ref);
1927 if (unlock_err && error == NULL)
1934 error = delete_missing_refs(&refs, verbosity, repo);
1939 if (!remote->mirror_references) {
1940 /* Update remote HEAD reference if the server provided one. */
1941 TAILQ_FOREACH(pe, &symrefs, entry) {
1942 struct got_reference *target_ref;
1943 const char *refname = pe->path;
1944 const char *target = pe->data;
1945 char *remote_refname = NULL, *remote_target = NULL;
1947 if (strcmp(refname, GOT_REF_HEAD) != 0)
1950 if (strncmp("refs/heads/", target, 11) != 0)
1953 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1954 remote->name, refname) == -1) {
1955 error = got_error_from_errno("asprintf");
1958 if (asprintf(&remote_target, "refs/remotes/%s/%s",
1959 remote->name, target + 11) == -1) {
1960 error = got_error_from_errno("asprintf");
1961 free(remote_refname);
1965 error = got_ref_open(&target_ref, repo, remote_target,
1968 free(remote_refname);
1969 free(remote_target);
1970 if (error->code == GOT_ERR_NOT_REF) {
1976 error = update_symref(remote_refname, target_ref,
1978 free(remote_refname);
1979 free(remote_target);
1980 got_ref_close(target_ref);
1987 if (kill(fetchpid, SIGTERM) == -1)
1988 error = got_error_from_errno("kill");
1989 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1990 error = got_error_from_errno("waitpid");
1992 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1993 error = got_error_from_errno("close");
1995 got_repo_close(repo);
1997 got_worktree_close(worktree);
1998 TAILQ_FOREACH(pe, &refs, entry) {
1999 free((void *)pe->path);
2002 got_pathlist_free(&refs);
2003 TAILQ_FOREACH(pe, &symrefs, entry) {
2004 free((void *)pe->path);
2007 got_pathlist_free(&symrefs);
2008 got_pathlist_free(&wanted_branches);
2009 got_pathlist_free(&wanted_refs);
2024 usage_checkout(void)
2026 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2027 "[-p prefix] repository-path [worktree-path]\n", getprogname());
2032 show_worktree_base_ref_warning(void)
2034 fprintf(stderr, "%s: warning: could not create a reference "
2035 "to the work tree's base commit; the commit could be "
2036 "garbage-collected by Git; making the repository "
2037 "writable and running 'got update' will prevent this\n",
2041 struct got_checkout_progress_arg {
2042 const char *worktree_path;
2043 int had_base_commit_ref_error;
2046 static const struct got_error *
2047 checkout_progress(void *arg, unsigned char status, const char *path)
2049 struct got_checkout_progress_arg *a = arg;
2051 /* Base commit bump happens silently. */
2052 if (status == GOT_STATUS_BUMP_BASE)
2055 if (status == GOT_STATUS_BASE_REF_ERR) {
2056 a->had_base_commit_ref_error = 1;
2060 while (path[0] == '/')
2063 printf("%c %s/%s\n", status, a->worktree_path, path);
2067 static const struct got_error *
2068 check_cancelled(void *arg)
2070 if (sigint_received || sigpipe_received)
2071 return got_error(GOT_ERR_CANCELLED);
2075 static const struct got_error *
2076 check_linear_ancestry(struct got_object_id *commit_id,
2077 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2078 struct got_repository *repo)
2080 const struct got_error *err = NULL;
2081 struct got_object_id *yca_id;
2083 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2084 commit_id, base_commit_id, repo, check_cancelled, NULL);
2089 return got_error(GOT_ERR_ANCESTRY);
2092 * Require a straight line of history between the target commit
2093 * and the work tree's base commit.
2095 * Non-linear situations such as this require a rebase:
2097 * (commit) D F (base_commit)
2105 * 'got update' only handles linear cases:
2106 * Update forwards in time: A (base/yca) - B - C - D (commit)
2107 * Update backwards in time: D (base) - C - B - A (commit/yca)
2109 if (allow_forwards_in_time_only) {
2110 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2111 return got_error(GOT_ERR_ANCESTRY);
2112 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2113 got_object_id_cmp(base_commit_id, yca_id) != 0)
2114 return got_error(GOT_ERR_ANCESTRY);
2120 static const struct got_error *
2121 check_same_branch(struct got_object_id *commit_id,
2122 struct got_reference *head_ref, struct got_object_id *yca_id,
2123 struct got_repository *repo)
2125 const struct got_error *err = NULL;
2126 struct got_commit_graph *graph = NULL;
2127 struct got_object_id *head_commit_id = NULL;
2128 int is_same_branch = 0;
2130 err = got_ref_resolve(&head_commit_id, repo, head_ref);
2134 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2138 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2143 err = got_commit_graph_open(&graph, "/", 1);
2147 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2148 check_cancelled, NULL);
2153 struct got_object_id *id;
2154 err = got_commit_graph_iter_next(&id, graph, repo,
2155 check_cancelled, NULL);
2157 if (err->code == GOT_ERR_ITER_COMPLETED)
2163 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2165 if (got_object_id_cmp(id, commit_id) == 0) {
2173 got_commit_graph_close(graph);
2174 free(head_commit_id);
2175 if (!err && !is_same_branch)
2176 err = got_error(GOT_ERR_ANCESTRY);
2180 static const struct got_error *
2181 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2183 static char msg[512];
2184 const char *branch_name;
2186 if (got_ref_is_symbolic(ref))
2187 branch_name = got_ref_get_symref_target(ref);
2189 branch_name = got_ref_get_name(ref);
2191 if (strncmp("refs/heads/", branch_name, 11) == 0)
2194 snprintf(msg, sizeof(msg),
2195 "target commit is not contained in branch '%s'; "
2196 "the branch to use must be specified with -b; "
2197 "if necessary a new branch can be created for "
2198 "this commit with 'got branch -c %s BRANCH_NAME'",
2199 branch_name, commit_id_str);
2201 return got_error_msg(GOT_ERR_ANCESTRY, msg);
2204 static const struct got_error *
2205 cmd_checkout(int argc, char *argv[])
2207 const struct got_error *error = NULL;
2208 struct got_repository *repo = NULL;
2209 struct got_reference *head_ref = NULL;
2210 struct got_worktree *worktree = NULL;
2211 char *repo_path = NULL;
2212 char *worktree_path = NULL;
2213 const char *path_prefix = "";
2214 const char *branch_name = GOT_REF_HEAD;
2215 char *commit_id_str = NULL;
2216 int ch, same_path_prefix, allow_nonempty = 0;
2217 struct got_pathlist_head paths;
2218 struct got_checkout_progress_arg cpa;
2222 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2225 branch_name = optarg;
2228 commit_id_str = strdup(optarg);
2229 if (commit_id_str == NULL)
2230 return got_error_from_errno("strdup");
2236 path_prefix = optarg;
2248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2249 "unveil", NULL) == -1)
2253 char *cwd, *base, *dotgit;
2254 repo_path = realpath(argv[0], NULL);
2255 if (repo_path == NULL)
2256 return got_error_from_errno2("realpath", argv[0]);
2257 cwd = getcwd(NULL, 0);
2259 error = got_error_from_errno("getcwd");
2262 if (path_prefix[0]) {
2263 base = basename(path_prefix);
2265 error = got_error_from_errno2("basename",
2270 base = basename(repo_path);
2272 error = got_error_from_errno2("basename",
2277 dotgit = strstr(base, ".git");
2280 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2281 error = got_error_from_errno("asprintf");
2286 } else if (argc == 2) {
2287 repo_path = realpath(argv[0], NULL);
2288 if (repo_path == NULL) {
2289 error = got_error_from_errno2("realpath", argv[0]);
2292 worktree_path = realpath(argv[1], NULL);
2293 if (worktree_path == NULL) {
2294 if (errno != ENOENT) {
2295 error = got_error_from_errno2("realpath",
2299 worktree_path = strdup(argv[1]);
2300 if (worktree_path == NULL) {
2301 error = got_error_from_errno("strdup");
2308 got_path_strip_trailing_slashes(repo_path);
2309 got_path_strip_trailing_slashes(worktree_path);
2311 error = got_repo_open(&repo, repo_path, NULL);
2315 /* Pre-create work tree path for unveil(2) */
2316 error = got_path_mkdir(worktree_path);
2318 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2319 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2321 if (!allow_nonempty &&
2322 !got_path_dir_is_empty(worktree_path)) {
2323 error = got_error_path(worktree_path,
2324 GOT_ERR_DIR_NOT_EMPTY);
2329 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2333 error = got_ref_open(&head_ref, repo, branch_name, 0);
2337 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2338 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2341 error = got_worktree_open(&worktree, worktree_path);
2345 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2349 if (!same_path_prefix) {
2350 error = got_error(GOT_ERR_PATH_PREFIX);
2354 if (commit_id_str) {
2355 struct got_object_id *commit_id;
2356 error = got_repo_match_object_id(&commit_id, NULL,
2357 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2360 error = check_linear_ancestry(commit_id,
2361 got_worktree_get_base_commit_id(worktree), 0, repo);
2362 if (error != NULL) {
2364 if (error->code == GOT_ERR_ANCESTRY) {
2365 error = checkout_ancestry_error(
2366 head_ref, commit_id_str);
2370 error = check_same_branch(commit_id, head_ref, NULL, repo);
2372 if (error->code == GOT_ERR_ANCESTRY) {
2373 error = checkout_ancestry_error(
2374 head_ref, commit_id_str);
2378 error = got_worktree_set_base_commit_id(worktree, repo,
2385 error = got_pathlist_append(&paths, "", NULL);
2388 cpa.worktree_path = worktree_path;
2389 cpa.had_base_commit_ref_error = 0;
2390 error = got_worktree_checkout_files(worktree, &paths, repo,
2391 checkout_progress, &cpa, check_cancelled, NULL);
2395 printf("Now shut up and hack\n");
2396 if (cpa.had_base_commit_ref_error)
2397 show_worktree_base_ref_warning();
2399 got_pathlist_free(&paths);
2400 free(commit_id_str);
2402 free(worktree_path);
2409 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2414 static const struct got_error *
2415 update_progress(void *arg, unsigned char status, const char *path)
2417 int *did_something = arg;
2419 if (status == GOT_STATUS_EXISTS ||
2420 status == GOT_STATUS_BASE_REF_ERR)
2425 /* Base commit bump happens silently. */
2426 if (status == GOT_STATUS_BUMP_BASE)
2429 while (path[0] == '/')
2431 printf("%c %s\n", status, path);
2435 static const struct got_error *
2436 switch_head_ref(struct got_reference *head_ref,
2437 struct got_object_id *commit_id, struct got_worktree *worktree,
2438 struct got_repository *repo)
2440 const struct got_error *err = NULL;
2442 int ref_has_moved = 0;
2444 /* Trivial case: switching between two different references. */
2445 if (strcmp(got_ref_get_name(head_ref),
2446 got_worktree_get_head_ref_name(worktree)) != 0) {
2447 printf("Switching work tree from %s to %s\n",
2448 got_worktree_get_head_ref_name(worktree),
2449 got_ref_get_name(head_ref));
2450 return got_worktree_set_head_ref(worktree, head_ref);
2453 err = check_linear_ancestry(commit_id,
2454 got_worktree_get_base_commit_id(worktree), 0, repo);
2456 if (err->code != GOT_ERR_ANCESTRY)
2463 /* Switching to a rebased branch with the same reference name. */
2464 err = got_object_id_str(&base_id_str,
2465 got_worktree_get_base_commit_id(worktree));
2468 printf("Reference %s now points at a different branch\n",
2469 got_worktree_get_head_ref_name(worktree));
2470 printf("Switching work tree from %s to %s\n", base_id_str,
2471 got_worktree_get_head_ref_name(worktree));
2475 static const struct got_error *
2476 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2478 const struct got_error *err;
2481 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2485 return got_error(GOT_ERR_REBASING);
2487 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2491 return got_error(GOT_ERR_HISTEDIT_BUSY);
2496 static const struct got_error *
2497 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2498 char *argv[], struct got_worktree *worktree)
2500 const struct got_error *err = NULL;
2507 return got_error_from_errno("strdup");
2508 return got_pathlist_append(paths, path, NULL);
2511 for (i = 0; i < argc; i++) {
2512 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2515 err = got_pathlist_append(paths, path, NULL);
2525 static const struct got_error *
2526 cmd_update(int argc, char *argv[])
2528 const struct got_error *error = NULL;
2529 struct got_repository *repo = NULL;
2530 struct got_worktree *worktree = NULL;
2531 char *worktree_path = NULL;
2532 struct got_object_id *commit_id = NULL;
2533 char *commit_id_str = NULL;
2534 const char *branch_name = NULL;
2535 struct got_reference *head_ref = NULL;
2536 struct got_pathlist_head paths;
2537 struct got_pathlist_entry *pe;
2538 int ch, did_something = 0;
2542 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2545 branch_name = optarg;
2548 commit_id_str = strdup(optarg);
2549 if (commit_id_str == NULL)
2550 return got_error_from_errno("strdup");
2562 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2563 "unveil", NULL) == -1)
2566 worktree_path = getcwd(NULL, 0);
2567 if (worktree_path == NULL) {
2568 error = got_error_from_errno("getcwd");
2571 error = got_worktree_open(&worktree, worktree_path);
2575 error = check_rebase_or_histedit_in_progress(worktree);
2579 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2584 error = apply_unveil(got_repo_get_path(repo), 0,
2585 got_worktree_get_root_path(worktree));
2589 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2593 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2594 got_worktree_get_head_ref_name(worktree), 0);
2597 if (commit_id_str == NULL) {
2598 error = got_ref_resolve(&commit_id, repo, head_ref);
2601 error = got_object_id_str(&commit_id_str, commit_id);
2605 error = got_repo_match_object_id(&commit_id, NULL,
2606 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2607 free(commit_id_str);
2608 commit_id_str = NULL;
2611 error = got_object_id_str(&commit_id_str, commit_id);
2617 struct got_object_id *head_commit_id;
2618 TAILQ_FOREACH(pe, &paths, entry) {
2619 if (pe->path_len == 0)
2621 error = got_error_msg(GOT_ERR_BAD_PATH,
2622 "switching between branches requires that "
2623 "the entire work tree gets updated");
2626 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2629 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2631 free(head_commit_id);
2634 error = check_same_branch(commit_id, head_ref, NULL, repo);
2637 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2641 error = check_linear_ancestry(commit_id,
2642 got_worktree_get_base_commit_id(worktree), 0, repo);
2643 if (error != NULL) {
2644 if (error->code == GOT_ERR_ANCESTRY)
2645 error = got_error(GOT_ERR_BRANCH_MOVED);
2648 error = check_same_branch(commit_id, head_ref, NULL, repo);
2653 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2655 error = got_worktree_set_base_commit_id(worktree, repo,
2661 error = got_worktree_checkout_files(worktree, &paths, repo,
2662 update_progress, &did_something, check_cancelled, NULL);
2667 printf("Updated to commit %s\n", commit_id_str);
2669 printf("Already up-to-date\n");
2671 free(worktree_path);
2672 TAILQ_FOREACH(pe, &paths, entry)
2673 free((char *)pe->path);
2674 got_pathlist_free(&paths);
2676 free(commit_id_str);
2680 static const struct got_error *
2681 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2682 const char *path, int diff_context, int ignore_whitespace,
2683 struct got_repository *repo)
2685 const struct got_error *err = NULL;
2686 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2689 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2694 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2698 while (path[0] == '/')
2700 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2701 ignore_whitespace, stdout);
2704 got_object_blob_close(blob1);
2705 got_object_blob_close(blob2);
2709 static const struct got_error *
2710 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2711 const char *path, int diff_context, int ignore_whitespace,
2712 struct got_repository *repo)
2714 const struct got_error *err = NULL;
2715 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2716 struct got_diff_blob_output_unidiff_arg arg;
2719 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2724 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2728 arg.diff_context = diff_context;
2729 arg.ignore_whitespace = ignore_whitespace;
2730 arg.outfile = stdout;
2731 while (path[0] == '/')
2733 err = got_diff_tree(tree1, tree2, path, path, repo,
2734 got_diff_blob_output_unidiff, &arg, 1);
2737 got_object_tree_close(tree1);
2739 got_object_tree_close(tree2);
2743 static const struct got_error *
2744 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2745 const char *path, int diff_context, struct got_repository *repo)
2747 const struct got_error *err = NULL;
2748 struct got_commit_object *pcommit = NULL;
2749 char *id_str1 = NULL, *id_str2 = NULL;
2750 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2751 struct got_object_qid *qid;
2753 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2755 err = got_object_open_as_commit(&pcommit, repo,
2761 if (path && path[0] != '\0') {
2763 err = got_object_id_by_path(&obj_id2, repo, id, path);
2766 err = got_object_id_str(&id_str2, obj_id2);
2772 err = got_object_id_by_path(&obj_id1, repo,
2778 err = got_object_id_str(&id_str1, obj_id1);
2784 err = got_object_get_type(&obj_type, repo, obj_id2);
2789 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2791 case GOT_OBJ_TYPE_BLOB:
2792 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2795 case GOT_OBJ_TYPE_TREE:
2796 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2800 err = got_error(GOT_ERR_OBJ_TYPE);
2806 obj_id2 = got_object_commit_get_tree_id(commit);
2807 err = got_object_id_str(&id_str2, obj_id2);
2810 obj_id1 = got_object_commit_get_tree_id(pcommit);
2811 err = got_object_id_str(&id_str1, obj_id1);
2814 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2815 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2821 got_object_commit_close(pcommit);
2826 get_datestr(time_t *time, char *datebuf)
2828 struct tm mytm, *tm;
2831 tm = gmtime_r(time, &mytm);
2834 s = asctime_r(tm, datebuf);
2837 p = strchr(s, '\n');
2843 static const struct got_error *
2844 match_logmsg(int *have_match, struct got_object_id *id,
2845 struct got_commit_object *commit, regex_t *regex)
2847 const struct got_error *err = NULL;
2848 regmatch_t regmatch;
2849 char *id_str = NULL, *logmsg = NULL;
2853 err = got_object_id_str(&id_str, id);
2857 err = got_object_commit_get_logmsg(&logmsg, commit);
2861 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2869 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2871 static const struct got_error *
2872 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2873 struct got_repository *repo, const char *path, int show_patch,
2874 int diff_context, struct got_reflist_head *refs)
2876 const struct got_error *err = NULL;
2877 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2879 time_t committer_time;
2880 const char *author, *committer;
2881 char *refs_str = NULL;
2882 struct got_reflist_entry *re;
2884 SIMPLEQ_FOREACH(re, refs, entry) {
2887 struct got_tag_object *tag = NULL;
2890 name = got_ref_get_name(re->ref);
2891 if (strcmp(name, GOT_REF_HEAD) == 0)
2893 if (strncmp(name, "refs/", 5) == 0)
2895 if (strncmp(name, "got/", 4) == 0)
2897 if (strncmp(name, "heads/", 6) == 0)
2899 if (strncmp(name, "remotes/", 8) == 0)
2901 if (strncmp(name, "tags/", 5) == 0) {
2902 err = got_object_open_as_tag(&tag, repo, re->id);
2904 if (err->code != GOT_ERR_OBJ_TYPE)
2906 /* Ref points at something other than a tag. */
2911 cmp = got_object_id_cmp(tag ?
2912 got_object_tag_get_object_id(tag) : re->id, id);
2914 got_object_tag_close(tag);
2918 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2920 err = got_error_from_errno("asprintf");
2926 err = got_object_id_str(&id_str, id);
2930 printf(GOT_COMMIT_SEP_STR);
2931 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2932 refs_str ? refs_str : "", refs_str ? ")" : "");
2937 printf("from: %s\n", got_object_commit_get_author(commit));
2938 committer_time = got_object_commit_get_committer_time(commit);
2939 datestr = get_datestr(&committer_time, datebuf);
2941 printf("date: %s UTC\n", datestr);
2942 author = got_object_commit_get_author(commit);
2943 committer = got_object_commit_get_committer(commit);
2944 if (strcmp(author, committer) != 0)
2945 printf("via: %s\n", committer);
2946 if (got_object_commit_get_nparents(commit) > 1) {
2947 const struct got_object_id_queue *parent_ids;
2948 struct got_object_qid *qid;
2950 parent_ids = got_object_commit_get_parent_ids(commit);
2951 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2952 err = got_object_id_str(&id_str, qid->id);
2955 printf("parent %d: %s\n", n++, id_str);
2960 err = got_object_commit_get_logmsg(&logmsg0, commit);
2966 line = strsep(&logmsg, "\n");
2968 printf(" %s\n", line);
2973 err = print_patch(commit, id, path, diff_context, repo);
2978 if (fflush(stdout) != 0 && err == NULL)
2979 err = got_error_from_errno("fflush");
2983 static const struct got_error *
2984 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2985 const char *path, int show_patch, const char *search_pattern,
2986 int diff_context, int limit, int log_branches,
2987 struct got_reflist_head *refs)
2989 const struct got_error *err;
2990 struct got_commit_graph *graph;
2994 if (search_pattern &&
2995 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2996 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2998 err = got_commit_graph_open(&graph, path, !log_branches);
3001 err = got_commit_graph_iter_start(graph, root_id, repo,
3002 check_cancelled, NULL);
3006 struct got_commit_object *commit;
3007 struct got_object_id *id;
3009 if (sigint_received || sigpipe_received)
3012 err = got_commit_graph_iter_next(&id, graph, repo,
3013 check_cancelled, NULL);
3015 if (err->code == GOT_ERR_ITER_COMPLETED)
3022 err = got_object_open_as_commit(&commit, repo, id);
3026 if (search_pattern) {
3027 err = match_logmsg(&have_match, id, commit, ®ex);
3029 got_object_commit_close(commit);
3032 if (have_match == 0) {
3033 got_object_commit_close(commit);
3038 err = print_commit(commit, id, repo, path, show_patch,
3039 diff_context, refs);
3040 got_object_commit_close(commit);
3041 if (err || (limit && --limit == 0))
3047 got_commit_graph_close(graph);
3054 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
3055 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
3060 get_default_log_limit(void)
3062 const char *got_default_log_limit;
3066 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3067 if (got_default_log_limit == NULL)
3069 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3075 static const struct got_error *
3076 cmd_log(int argc, char *argv[])
3078 const struct got_error *error;
3079 struct got_repository *repo = NULL;
3080 struct got_worktree *worktree = NULL;
3081 struct got_commit_object *commit = NULL;
3082 struct got_object_id *id = NULL;
3083 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3084 const char *start_commit = NULL, *search_pattern = NULL;
3085 int diff_context = -1, ch;
3086 int show_patch = 0, limit = 0, log_branches = 0;
3088 struct got_reflist_head refs;
3090 SIMPLEQ_INIT(&refs);
3093 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3099 limit = get_default_log_limit();
3101 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3107 start_commit = optarg;
3110 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3113 err(1, "-C option %s", errstr);
3116 limit = strtonum(optarg, 0, INT_MAX, &errstr);
3118 err(1, "-l option %s", errstr);
3124 repo_path = realpath(optarg, NULL);
3125 if (repo_path == NULL)
3126 return got_error_from_errno2("realpath",
3128 got_path_strip_trailing_slashes(repo_path);
3131 search_pattern = optarg;
3142 if (diff_context == -1)
3144 else if (!show_patch)
3145 errx(1, "-C reguires -p");
3147 cwd = getcwd(NULL, 0);
3149 error = got_error_from_errno("getcwd");
3153 error = got_worktree_open(&worktree, cwd);
3154 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3161 error = got_error_from_errno("strdup");
3164 } else if (argc == 1) {
3166 error = got_worktree_resolve_path(&path, worktree,
3171 path = strdup(argv[0]);
3173 error = got_error_from_errno("strdup");
3180 if (repo_path == NULL) {
3181 repo_path = worktree ?
3182 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3184 if (repo_path == NULL) {
3185 error = got_error_from_errno("strdup");
3189 error = got_repo_open(&repo, repo_path, NULL);
3193 error = apply_unveil(got_repo_get_path(repo), 1,
3194 worktree ? got_worktree_get_root_path(worktree) : NULL);
3198 if (start_commit == NULL) {
3199 struct got_reference *head_ref;
3200 error = got_ref_open(&head_ref, repo,
3201 worktree ? got_worktree_get_head_ref_name(worktree)
3205 error = got_ref_resolve(&id, repo, head_ref);
3206 got_ref_close(head_ref);
3209 error = got_object_open_as_commit(&commit, repo, id);
3211 struct got_reference *ref;
3212 error = got_ref_open(&ref, repo, start_commit, 0);
3213 if (error == NULL) {
3215 error = got_ref_resolve(&id, repo, ref);
3219 error = got_object_get_type(&obj_type, repo, id);
3222 if (obj_type == GOT_OBJ_TYPE_TAG) {
3223 struct got_tag_object *tag;
3224 error = got_object_open_as_tag(&tag, repo, id);
3227 if (got_object_tag_get_object_type(tag) !=
3228 GOT_OBJ_TYPE_COMMIT) {
3229 got_object_tag_close(tag);
3230 error = got_error(GOT_ERR_OBJ_TYPE);
3234 id = got_object_id_dup(
3235 got_object_tag_get_object_id(tag));
3237 error = got_error_from_errno(
3238 "got_object_id_dup");
3239 got_object_tag_close(tag);
3242 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3243 error = got_error(GOT_ERR_OBJ_TYPE);
3246 error = got_object_open_as_commit(&commit, repo, id);
3250 if (commit == NULL) {
3251 error = got_repo_match_object_id_prefix(&id,
3252 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3261 const char *prefix = got_worktree_get_path_prefix(worktree);
3263 if (asprintf(&p, "%s%s%s", prefix,
3264 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3265 error = got_error_from_errno("asprintf");
3268 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3271 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3276 path = in_repo_path;
3279 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3283 error = print_commits(id, repo, path, show_patch, search_pattern,
3284 diff_context, limit, log_branches, &refs);
3291 got_worktree_close(worktree);
3293 const struct got_error *repo_error;
3294 repo_error = got_repo_close(repo);
3298 got_ref_list_free(&refs);
3305 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3306 "[-w] [object1 object2 | path]\n", getprogname());
3310 struct print_diff_arg {
3311 struct got_repository *repo;
3312 struct got_worktree *worktree;
3317 int ignore_whitespace;
3320 static const struct got_error *
3321 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3322 const char *path, struct got_object_id *blob_id,
3323 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3324 int dirfd, const char *de_name)
3326 struct print_diff_arg *a = arg;
3327 const struct got_error *err = NULL;
3328 struct got_blob_object *blob1 = NULL;
3331 char *abspath = NULL, *label1 = NULL;
3334 if (a->diff_staged) {
3335 if (staged_status != GOT_STATUS_MODIFY &&
3336 staged_status != GOT_STATUS_ADD &&
3337 staged_status != GOT_STATUS_DELETE)
3340 if (staged_status == GOT_STATUS_DELETE)
3342 if (status == GOT_STATUS_NONEXISTENT)
3343 return got_error_set_errno(ENOENT, path);
3344 if (status != GOT_STATUS_MODIFY &&
3345 status != GOT_STATUS_ADD &&
3346 status != GOT_STATUS_DELETE &&
3347 status != GOT_STATUS_CONFLICT)
3351 if (!a->header_shown) {
3352 printf("diff %s %s%s\n", a->id_str,
3353 got_worktree_get_root_path(a->worktree),
3354 a->diff_staged ? " (staged changes)" : "");
3355 a->header_shown = 1;
3358 if (a->diff_staged) {
3359 const char *label1 = NULL, *label2 = NULL;
3360 switch (staged_status) {
3361 case GOT_STATUS_MODIFY:
3365 case GOT_STATUS_ADD:
3368 case GOT_STATUS_DELETE:
3372 return got_error(GOT_ERR_FILE_STATUS);
3374 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3375 label1, label2, a->diff_context, a->ignore_whitespace,
3379 if (staged_status == GOT_STATUS_ADD ||
3380 staged_status == GOT_STATUS_MODIFY) {
3382 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3386 err = got_object_id_str(&id_str, staged_blob_id);
3389 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3390 err = got_error_from_errno("asprintf");
3395 } else if (status != GOT_STATUS_ADD) {
3396 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3401 if (status != GOT_STATUS_DELETE) {
3402 if (asprintf(&abspath, "%s/%s",
3403 got_worktree_get_root_path(a->worktree), path) == -1) {
3404 err = got_error_from_errno("asprintf");
3409 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3411 err = got_error_from_errno2("openat", abspath);
3415 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3417 err = got_error_from_errno2("open", abspath);
3421 if (fstat(fd, &sb) == -1) {
3422 err = got_error_from_errno2("fstat", abspath);
3425 f2 = fdopen(fd, "r");
3427 err = got_error_from_errno2("fdopen", abspath);
3434 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3435 a->diff_context, a->ignore_whitespace, stdout);
3438 got_object_blob_close(blob1);
3439 if (f2 && fclose(f2) == EOF && err == NULL)
3440 err = got_error_from_errno("fclose");
3441 if (fd != -1 && close(fd) == -1 && err == NULL)
3442 err = got_error_from_errno("close");
3447 static const struct got_error *
3448 cmd_diff(int argc, char *argv[])
3450 const struct got_error *error;
3451 struct got_repository *repo = NULL;
3452 struct got_worktree *worktree = NULL;
3453 char *cwd = NULL, *repo_path = NULL;
3454 struct got_object_id *id1 = NULL, *id2 = NULL;
3455 const char *id_str1 = NULL, *id_str2 = NULL;
3456 char *label1 = NULL, *label2 = NULL;
3458 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3463 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3468 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3471 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3474 err(1, "-C option %s", errstr);
3477 repo_path = realpath(optarg, NULL);
3478 if (repo_path == NULL)
3479 return got_error_from_errno2("realpath",
3481 got_path_strip_trailing_slashes(repo_path);
3487 ignore_whitespace = 1;
3498 cwd = getcwd(NULL, 0);
3500 error = got_error_from_errno("getcwd");
3506 "-r option can't be used when diffing a work tree");
3507 error = got_worktree_open(&worktree, cwd);
3510 repo_path = strdup(got_worktree_get_repo_path(worktree));
3511 if (repo_path == NULL) {
3512 error = got_error_from_errno("strdup");
3516 error = got_worktree_resolve_path(&path, worktree,
3523 error = got_error_from_errno("strdup");
3527 } else if (argc == 2) {
3529 errx(1, "-s option can't be used when diffing "
3530 "objects in repository");
3533 if (repo_path == NULL) {
3534 error = got_worktree_open(&worktree, cwd);
3535 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3539 got_worktree_get_repo_path(worktree));
3540 if (repo_path == NULL) {
3541 error = got_error_from_errno("strdup");
3545 repo_path = strdup(cwd);
3546 if (repo_path == NULL) {
3547 error = got_error_from_errno("strdup");
3555 error = got_repo_open(&repo, repo_path, NULL);
3560 error = apply_unveil(got_repo_get_path(repo), 1,
3561 worktree ? got_worktree_get_root_path(worktree) : NULL);
3566 struct print_diff_arg arg;
3567 struct got_pathlist_head paths;
3572 error = got_object_id_str(&id_str,
3573 got_worktree_get_base_commit_id(worktree));
3577 arg.worktree = worktree;
3578 arg.diff_context = diff_context;
3579 arg.id_str = id_str;
3580 arg.header_shown = 0;
3581 arg.diff_staged = diff_staged;
3582 arg.ignore_whitespace = ignore_whitespace;
3584 error = got_pathlist_append(&paths, path, NULL);
3588 error = got_worktree_status(worktree, &paths, repo, print_diff,
3589 &arg, check_cancelled, NULL);
3591 got_pathlist_free(&paths);
3595 error = got_repo_match_object_id(&id1, &label1, id_str1,
3596 GOT_OBJ_TYPE_ANY, 1, repo);
3600 error = got_repo_match_object_id(&id2, &label2, id_str2,
3601 GOT_OBJ_TYPE_ANY, 1, repo);
3605 error = got_object_get_type(&type1, repo, id1);
3609 error = got_object_get_type(&type2, repo, id2);
3613 if (type1 != type2) {
3614 error = got_error(GOT_ERR_OBJ_TYPE);
3619 case GOT_OBJ_TYPE_BLOB:
3620 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3621 diff_context, ignore_whitespace, repo, stdout);
3623 case GOT_OBJ_TYPE_TREE:
3624 error = got_diff_objects_as_trees(id1, id2, "", "",
3625 diff_context, ignore_whitespace, repo, stdout);
3627 case GOT_OBJ_TYPE_COMMIT:
3628 printf("diff %s %s\n", label1, label2);
3629 error = got_diff_objects_as_commits(id1, id2, diff_context,
3630 ignore_whitespace, repo, stdout);
3633 error = got_error(GOT_ERR_OBJ_TYPE);
3642 got_worktree_close(worktree);
3644 const struct got_error *repo_error;
3645 repo_error = got_repo_close(repo);
3656 "usage: %s blame [-c commit] [-r repository-path] path\n",
3665 char datebuf[11]; /* YYYY-MM-DD + NUL */
3668 struct blame_cb_args {
3669 struct blame_line *lines;
3673 off_t *line_offsets;
3675 struct got_repository *repo;
3678 static const struct got_error *
3679 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3681 const struct got_error *err = NULL;
3682 struct blame_cb_args *a = arg;
3683 struct blame_line *bline;
3685 size_t linesize = 0;
3686 struct got_commit_object *commit = NULL;
3689 time_t committer_time;
3691 if (nlines != a->nlines ||
3692 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3693 return got_error(GOT_ERR_RANGE);
3695 if (sigint_received)
3696 return got_error(GOT_ERR_ITER_COMPLETED);
3699 return NULL; /* no change in this commit */
3701 /* Annotate this line. */
3702 bline = &a->lines[lineno - 1];
3703 if (bline->annotated)
3705 err = got_object_id_str(&bline->id_str, id);
3709 err = got_object_open_as_commit(&commit, a->repo, id);
3713 bline->committer = strdup(got_object_commit_get_committer(commit));
3714 if (bline->committer == NULL) {
3715 err = got_error_from_errno("strdup");
3719 committer_time = got_object_commit_get_committer_time(commit);
3720 if (localtime_r(&committer_time, &tm) == NULL)
3721 return got_error_from_errno("localtime_r");
3722 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3723 &tm) >= sizeof(bline->datebuf)) {
3724 err = got_error(GOT_ERR_NO_SPACE);
3727 bline->annotated = 1;
3729 /* Print lines annotated so far. */
3730 bline = &a->lines[a->lineno_cur - 1];
3731 if (!bline->annotated)
3734 offset = a->line_offsets[a->lineno_cur - 1];
3735 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3736 err = got_error_from_errno("fseeko");
3740 while (bline->annotated) {
3741 char *smallerthan, *at, *nl, *committer;
3744 if (getline(&line, &linesize, a->f) == -1) {
3746 err = got_error_from_errno("getline");
3750 committer = bline->committer;
3751 smallerthan = strchr(committer, '<');
3752 if (smallerthan && smallerthan[1] != '\0')
3753 committer = smallerthan + 1;
3754 at = strchr(committer, '@');
3757 len = strlen(committer);
3759 committer[8] = '\0';
3761 nl = strchr(line, '\n');
3764 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3765 bline->id_str, bline->datebuf, committer, line);
3768 bline = &a->lines[a->lineno_cur - 1];
3772 got_object_commit_close(commit);
3777 static const struct got_error *
3778 cmd_blame(int argc, char *argv[])
3780 const struct got_error *error;
3781 struct got_repository *repo = NULL;
3782 struct got_worktree *worktree = NULL;
3783 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3784 struct got_object_id *obj_id = NULL;
3785 struct got_object_id *commit_id = NULL;
3786 struct got_blob_object *blob = NULL;
3787 char *commit_id_str = NULL;
3788 struct blame_cb_args bca;
3789 int ch, obj_type, i;
3792 memset(&bca, 0, sizeof(bca));
3795 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3800 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3803 commit_id_str = optarg;
3806 repo_path = realpath(optarg, NULL);
3807 if (repo_path == NULL)
3808 return got_error_from_errno2("realpath",
3810 got_path_strip_trailing_slashes(repo_path);
3826 cwd = getcwd(NULL, 0);
3828 error = got_error_from_errno("getcwd");
3831 if (repo_path == NULL) {
3832 error = got_worktree_open(&worktree, cwd);
3833 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3839 strdup(got_worktree_get_repo_path(worktree));
3840 if (repo_path == NULL) {
3841 error = got_error_from_errno("strdup");
3846 repo_path = strdup(cwd);
3847 if (repo_path == NULL) {
3848 error = got_error_from_errno("strdup");
3854 error = got_repo_open(&repo, repo_path, NULL);
3858 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3863 const char *prefix = got_worktree_get_path_prefix(worktree);
3864 char *p, *worktree_subdir = cwd +
3865 strlen(got_worktree_get_root_path(worktree));
3866 if (asprintf(&p, "%s%s%s%s%s",
3867 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3868 worktree_subdir, worktree_subdir[0] ? "/" : "",
3870 error = got_error_from_errno("asprintf");
3873 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3876 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3881 if (commit_id_str == NULL) {
3882 struct got_reference *head_ref;
3883 error = got_ref_open(&head_ref, repo, worktree ?
3884 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3887 error = got_ref_resolve(&commit_id, repo, head_ref);
3888 got_ref_close(head_ref);
3892 error = got_repo_match_object_id(&commit_id, NULL,
3893 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3898 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3902 error = got_object_get_type(&obj_type, repo, obj_id);
3906 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3907 error = got_error(GOT_ERR_OBJ_TYPE);
3911 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3914 bca.f = got_opentemp();
3915 if (bca.f == NULL) {
3916 error = got_error_from_errno("got_opentemp");
3919 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3920 &bca.line_offsets, bca.f, blob);
3921 if (error || bca.nlines == 0)
3924 /* Don't include \n at EOF in the blame line count. */
3925 if (bca.line_offsets[bca.nlines - 1] == filesize)
3928 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3929 if (bca.lines == NULL) {
3930 error = got_error_from_errno("calloc");
3934 bca.nlines_prec = 0;
3942 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3943 check_cancelled, NULL);
3951 got_object_blob_close(blob);
3953 got_worktree_close(worktree);
3955 const struct got_error *repo_error;
3956 repo_error = got_repo_close(repo);
3961 for (i = 0; i < bca.nlines; i++) {
3962 struct blame_line *bline = &bca.lines[i];
3963 free(bline->id_str);
3964 free(bline->committer);
3968 free(bca.line_offsets);
3969 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3970 error = got_error_from_errno("fclose");
3978 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3984 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3985 const char *root_path)
3987 int is_root_path = (strcmp(path, root_path) == 0);
3988 const char *modestr = "";
3989 mode_t mode = got_tree_entry_get_mode(te);
3991 path += strlen(root_path);
3992 while (path[0] == '/')
3995 if (got_object_tree_entry_is_submodule(te))
3997 else if (S_ISLNK(mode))
3999 else if (S_ISDIR(mode))
4001 else if (mode & S_IXUSR)
4004 printf("%s%s%s%s%s\n", id ? id : "", path,
4005 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4008 static const struct got_error *
4009 print_tree(const char *path, struct got_object_id *commit_id,
4010 int show_ids, int recurse, const char *root_path,
4011 struct got_repository *repo)
4013 const struct got_error *err = NULL;
4014 struct got_object_id *tree_id = NULL;
4015 struct got_tree_object *tree = NULL;
4018 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4022 err = got_object_open_as_tree(&tree, repo, tree_id);
4025 nentries = got_object_tree_get_nentries(tree);
4026 for (i = 0; i < nentries; i++) {
4027 struct got_tree_entry *te;
4030 if (sigint_received || sigpipe_received)
4033 te = got_object_tree_get_entry(tree, i);
4036 err = got_object_id_str(&id_str,
4037 got_tree_entry_get_id(te));
4040 if (asprintf(&id, "%s ", id_str) == -1) {
4041 err = got_error_from_errno("asprintf");
4047 print_entry(te, id, path, root_path);
4050 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4052 if (asprintf(&child_path, "%s%s%s", path,
4053 path[0] == '/' && path[1] == '\0' ? "" : "/",
4054 got_tree_entry_get_name(te)) == -1) {
4055 err = got_error_from_errno("asprintf");
4058 err = print_tree(child_path, commit_id, show_ids, 1,
4067 got_object_tree_close(tree);
4072 static const struct got_error *
4073 cmd_tree(int argc, char *argv[])
4075 const struct got_error *error;
4076 struct got_repository *repo = NULL;
4077 struct got_worktree *worktree = NULL;
4078 const char *path, *refname = NULL;
4079 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4080 struct got_object_id *commit_id = NULL;
4081 char *commit_id_str = NULL;
4082 int show_ids = 0, recurse = 0;
4086 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4091 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4094 commit_id_str = optarg;
4097 repo_path = realpath(optarg, NULL);
4098 if (repo_path == NULL)
4099 return got_error_from_errno2("realpath",
4101 got_path_strip_trailing_slashes(repo_path);
4125 cwd = getcwd(NULL, 0);
4127 error = got_error_from_errno("getcwd");
4130 if (repo_path == NULL) {
4131 error = got_worktree_open(&worktree, cwd);
4132 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4138 strdup(got_worktree_get_repo_path(worktree));
4139 if (repo_path == NULL)
4140 error = got_error_from_errno("strdup");
4144 repo_path = strdup(cwd);
4145 if (repo_path == NULL) {
4146 error = got_error_from_errno("strdup");
4152 error = got_repo_open(&repo, repo_path, NULL);
4156 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4162 char *p, *worktree_subdir = cwd +
4163 strlen(got_worktree_get_root_path(worktree));
4164 if (asprintf(&p, "%s/%s",
4165 got_worktree_get_path_prefix(worktree),
4166 worktree_subdir) == -1) {
4167 error = got_error_from_errno("asprintf");
4170 error = got_repo_map_path(&in_repo_path, repo, p, 0);
4177 if (in_repo_path == NULL) {
4178 error = got_repo_map_path(&in_repo_path, repo, path, 1);
4183 if (commit_id_str == NULL) {
4184 struct got_reference *head_ref;
4186 refname = got_worktree_get_head_ref_name(worktree);
4188 refname = GOT_REF_HEAD;
4189 error = got_ref_open(&head_ref, repo, refname, 0);
4192 error = got_ref_resolve(&commit_id, repo, head_ref);
4193 got_ref_close(head_ref);
4197 error = got_repo_match_object_id(&commit_id, NULL,
4198 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4203 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4204 in_repo_path, repo);
4211 got_worktree_close(worktree);
4213 const struct got_error *repo_error;
4214 repo_error = got_repo_close(repo);
4224 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4228 static const struct got_error *
4229 print_status(void *arg, unsigned char status, unsigned char staged_status,
4230 const char *path, struct got_object_id *blob_id,
4231 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4232 int dirfd, const char *de_name)
4234 if (status == staged_status && (status == GOT_STATUS_DELETE))
4235 status = GOT_STATUS_NO_CHANGE;
4236 printf("%c%c %s\n", status, staged_status, path);
4240 static const struct got_error *
4241 cmd_status(int argc, char *argv[])
4243 const struct got_error *error = NULL;
4244 struct got_repository *repo = NULL;
4245 struct got_worktree *worktree = NULL;
4247 struct got_pathlist_head paths;
4248 struct got_pathlist_entry *pe;
4253 while ((ch = getopt(argc, argv, "")) != -1) {
4265 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4269 cwd = getcwd(NULL, 0);
4271 error = got_error_from_errno("getcwd");
4275 error = got_worktree_open(&worktree, cwd);
4279 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4284 error = apply_unveil(got_repo_get_path(repo), 1,
4285 got_worktree_get_root_path(worktree));
4289 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4293 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4294 check_cancelled, NULL);
4296 TAILQ_FOREACH(pe, &paths, entry)
4297 free((char *)pe->path);
4298 got_pathlist_free(&paths);
4307 "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4313 static const struct got_error *
4314 list_refs(struct got_repository *repo, const char *refname)
4316 static const struct got_error *err = NULL;
4317 struct got_reflist_head refs;
4318 struct got_reflist_entry *re;
4320 SIMPLEQ_INIT(&refs);
4321 err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4325 SIMPLEQ_FOREACH(re, &refs, entry) {
4327 refstr = got_ref_to_str(re->ref);
4329 return got_error_from_errno("got_ref_to_str");
4330 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4334 got_ref_list_free(&refs);
4338 static const struct got_error *
4339 delete_ref(struct got_repository *repo, const char *refname)
4341 const struct got_error *err = NULL;
4342 struct got_reference *ref;
4344 err = got_ref_open(&ref, repo, refname, 0);
4348 err = got_ref_delete(ref, repo);
4353 static const struct got_error *
4354 add_ref(struct got_repository *repo, const char *refname, const char *target)
4356 const struct got_error *err = NULL;
4357 struct got_object_id *id;
4358 struct got_reference *ref = NULL;
4361 * Don't let the user create a reference name with a leading '-'.
4362 * While technically a valid reference name, this case is usually
4363 * an unintended typo.
4365 if (refname[0] == '-')
4366 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4368 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4371 struct got_reference *target_ref;
4373 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4375 err = got_ref_open(&target_ref, repo, target, 0);
4378 err = got_ref_resolve(&id, repo, target_ref);
4379 got_ref_close(target_ref);
4384 err = got_ref_alloc(&ref, refname, id);
4388 err = got_ref_write(ref, repo);
4396 static const struct got_error *
4397 add_symref(struct got_repository *repo, const char *refname, const char *target)
4399 const struct got_error *err = NULL;
4400 struct got_reference *ref = NULL;
4401 struct got_reference *target_ref = NULL;
4404 * Don't let the user create a reference name with a leading '-'.
4405 * While technically a valid reference name, this case is usually
4406 * an unintended typo.
4408 if (refname[0] == '-')
4409 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4411 err = got_ref_open(&target_ref, repo, target, 0);
4415 err = got_ref_alloc_symref(&ref, refname, target_ref);
4419 err = got_ref_write(ref, repo);
4422 got_ref_close(target_ref);
4428 static const struct got_error *
4429 cmd_ref(int argc, char *argv[])
4431 const struct got_error *error = NULL;
4432 struct got_repository *repo = NULL;
4433 struct got_worktree *worktree = NULL;
4434 char *cwd = NULL, *repo_path = NULL;
4435 int ch, do_list = 0, do_delete = 0;
4436 const char *obj_arg = NULL, *symref_target= NULL;
4437 char *refname = NULL;
4439 while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4448 repo_path = realpath(optarg, NULL);
4449 if (repo_path == NULL)
4450 return got_error_from_errno2("realpath",
4452 got_path_strip_trailing_slashes(repo_path);
4458 symref_target = optarg;
4466 if (obj_arg && do_list)
4467 errx(1, "-c and -l options are mutually exclusive");
4468 if (obj_arg && do_delete)
4469 errx(1, "-c and -d options are mutually exclusive");
4470 if (obj_arg && symref_target)
4471 errx(1, "-c and -s options are mutually exclusive");
4472 if (symref_target && do_delete)
4473 errx(1, "-s and -d options are mutually exclusive");
4474 if (symref_target && do_list)
4475 errx(1, "-s and -l options are mutually exclusive");
4476 if (do_delete && do_list)
4477 errx(1, "-d and -l options are mutually exclusive");
4483 if (argc != 0 && argc != 1)
4486 refname = strdup(argv[0]);
4487 if (refname == NULL) {
4488 error = got_error_from_errno("strdup");
4495 refname = strdup(argv[0]);
4496 if (refname == NULL) {
4497 error = got_error_from_errno("strdup");
4503 got_path_strip_trailing_slashes(refname);
4507 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4511 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4512 "sendfd unveil", NULL) == -1)
4516 cwd = getcwd(NULL, 0);
4518 error = got_error_from_errno("getcwd");
4522 if (repo_path == NULL) {
4523 error = got_worktree_open(&worktree, cwd);
4524 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4530 strdup(got_worktree_get_repo_path(worktree));
4531 if (repo_path == NULL)
4532 error = got_error_from_errno("strdup");
4536 repo_path = strdup(cwd);
4537 if (repo_path == NULL) {
4538 error = got_error_from_errno("strdup");
4544 error = got_repo_open(&repo, repo_path, NULL);
4548 error = apply_unveil(got_repo_get_path(repo), do_list,
4549 worktree ? got_worktree_get_root_path(worktree) : NULL);
4554 error = list_refs(repo, refname);
4556 error = delete_ref(repo, refname);
4557 else if (symref_target)
4558 error = add_symref(repo, refname, symref_target);
4560 if (obj_arg == NULL)
4562 error = add_ref(repo, refname, obj_arg);
4567 got_repo_close(repo);
4569 got_worktree_close(worktree);
4579 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4580 "[name]\n", getprogname());
4584 static const struct got_error *
4585 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4586 struct got_reference *ref)
4588 const struct got_error *err = NULL;
4589 const char *refname, *marker = " ";
4592 refname = got_ref_get_name(ref);
4593 if (worktree && strcmp(refname,
4594 got_worktree_get_head_ref_name(worktree)) == 0) {
4595 struct got_object_id *id = NULL;
4597 err = got_ref_resolve(&id, repo, ref);
4600 if (got_object_id_cmp(id,
4601 got_worktree_get_base_commit_id(worktree)) == 0)
4608 if (strncmp(refname, "refs/heads/", 11) == 0)
4610 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4613 refstr = got_ref_to_str(ref);
4615 return got_error_from_errno("got_ref_to_str");
4617 printf("%s%s: %s\n", marker, refname, refstr);
4622 static const struct got_error *
4623 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4625 const char *refname;
4627 if (worktree == NULL)
4628 return got_error(GOT_ERR_NOT_WORKTREE);
4630 refname = got_worktree_get_head_ref_name(worktree);
4632 if (strncmp(refname, "refs/heads/", 11) == 0)
4634 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4637 printf("%s\n", refname);
4642 static const struct got_error *
4643 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4645 static const struct got_error *err = NULL;
4646 struct got_reflist_head refs;
4647 struct got_reflist_entry *re;
4648 struct got_reference *temp_ref = NULL;
4649 int rebase_in_progress, histedit_in_progress;
4651 SIMPLEQ_INIT(&refs);
4654 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4659 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4664 if (rebase_in_progress || histedit_in_progress) {
4665 err = got_ref_open(&temp_ref, repo,
4666 got_worktree_get_head_ref_name(worktree), 0);
4669 list_branch(repo, worktree, temp_ref);
4670 got_ref_close(temp_ref);
4674 err = got_ref_list(&refs, repo, "refs/heads",
4675 got_ref_cmp_by_name, NULL);
4679 SIMPLEQ_FOREACH(re, &refs, entry)
4680 list_branch(repo, worktree, re->ref);
4682 got_ref_list_free(&refs);
4686 static const struct got_error *
4687 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4688 const char *branch_name)
4690 const struct got_error *err = NULL;
4691 struct got_reference *ref = NULL;
4694 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4695 return got_error_from_errno("asprintf");
4697 err = got_ref_open(&ref, repo, refname, 0);
4702 strcmp(got_worktree_get_head_ref_name(worktree),
4703 got_ref_get_name(ref)) == 0) {
4704 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4705 "will not delete this work tree's current branch");
4709 err = got_ref_delete(ref, repo);
4717 static const struct got_error *
4718 add_branch(struct got_repository *repo, const char *branch_name,
4719 struct got_object_id *base_commit_id)
4721 const struct got_error *err = NULL;
4722 struct got_reference *ref = NULL;
4723 char *base_refname = NULL, *refname = NULL;
4726 * Don't let the user create a branch name with a leading '-'.
4727 * While technically a valid reference name, this case is usually
4728 * an unintended typo.
4730 if (branch_name[0] == '-')
4731 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4733 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4734 err = got_error_from_errno("asprintf");
4738 err = got_ref_open(&ref, repo, refname, 0);
4740 err = got_error(GOT_ERR_BRANCH_EXISTS);
4742 } else if (err->code != GOT_ERR_NOT_REF)
4745 err = got_ref_alloc(&ref, refname, base_commit_id);
4749 err = got_ref_write(ref, repo);
4758 static const struct got_error *
4759 cmd_branch(int argc, char *argv[])
4761 const struct got_error *error = NULL;
4762 struct got_repository *repo = NULL;
4763 struct got_worktree *worktree = NULL;
4764 char *cwd = NULL, *repo_path = NULL;
4765 int ch, do_list = 0, do_show = 0, do_update = 1;
4766 const char *delref = NULL, *commit_id_arg = NULL;
4767 struct got_reference *ref = NULL;
4768 struct got_pathlist_head paths;
4769 struct got_pathlist_entry *pe;
4770 struct got_object_id *commit_id = NULL;
4771 char *commit_id_str = NULL;
4775 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4778 commit_id_arg = optarg;
4784 repo_path = realpath(optarg, NULL);
4785 if (repo_path == NULL)
4786 return got_error_from_errno2("realpath",
4788 got_path_strip_trailing_slashes(repo_path);
4802 if (do_list && delref)
4803 errx(1, "-l and -d options are mutually exclusive");
4808 if (!do_list && !delref && argc == 0)
4811 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4812 errx(1, "-c option can only be used when creating a branch");
4814 if (do_list || delref) {
4817 } else if (!do_show && argc != 1)
4821 if (do_list || do_show) {
4822 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4826 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4827 "sendfd unveil", NULL) == -1)
4831 cwd = getcwd(NULL, 0);
4833 error = got_error_from_errno("getcwd");
4837 if (repo_path == NULL) {
4838 error = got_worktree_open(&worktree, cwd);
4839 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4845 strdup(got_worktree_get_repo_path(worktree));
4846 if (repo_path == NULL)
4847 error = got_error_from_errno("strdup");
4851 repo_path = strdup(cwd);
4852 if (repo_path == NULL) {
4853 error = got_error_from_errno("strdup");
4859 error = got_repo_open(&repo, repo_path, NULL);
4863 error = apply_unveil(got_repo_get_path(repo), do_list,
4864 worktree ? got_worktree_get_root_path(worktree) : NULL);
4869 error = show_current_branch(repo, worktree);
4871 error = list_branches(repo, worktree);
4873 error = delete_branch(repo, worktree, delref);
4875 if (commit_id_arg == NULL)
4876 commit_id_arg = worktree ?
4877 got_worktree_get_head_ref_name(worktree) :
4879 error = got_repo_match_object_id(&commit_id, NULL,
4880 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4883 error = add_branch(repo, argv[0], commit_id);
4886 if (worktree && do_update) {
4887 int did_something = 0;
4888 char *branch_refname = NULL;
4890 error = got_object_id_str(&commit_id_str, commit_id);
4893 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4897 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4899 error = got_error_from_errno("asprintf");
4902 error = got_ref_open(&ref, repo, branch_refname, 0);
4903 free(branch_refname);
4906 error = switch_head_ref(ref, commit_id, worktree,
4910 error = got_worktree_set_base_commit_id(worktree, repo,
4914 error = got_worktree_checkout_files(worktree, &paths,
4915 repo, update_progress, &did_something,
4916 check_cancelled, NULL);
4920 printf("Updated to commit %s\n", commit_id_str);
4927 got_repo_close(repo);
4929 got_worktree_close(worktree);
4933 free(commit_id_str);
4934 TAILQ_FOREACH(pe, &paths, entry)
4935 free((char *)pe->path);
4936 got_pathlist_free(&paths);
4945 "usage: %s tag [-c commit] [-r repository] [-l] "
4946 "[-m message] name\n", getprogname());
4951 static const struct got_error *
4952 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4954 const struct got_error *err = NULL;
4955 struct got_reflist_entry *re, *se, *new;
4956 struct got_object_id *re_id, *se_id;
4957 struct got_tag_object *re_tag, *se_tag;
4958 time_t re_time, se_time;
4960 SIMPLEQ_FOREACH(re, tags, entry) {
4961 se = SIMPLEQ_FIRST(sorted);
4963 err = got_reflist_entry_dup(&new, re);
4966 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4969 err = got_ref_resolve(&re_id, repo, re->ref);
4972 err = got_object_open_as_tag(&re_tag, repo, re_id);
4976 re_time = got_object_tag_get_tagger_time(re_tag);
4977 got_object_tag_close(re_tag);
4981 err = got_ref_resolve(&se_id, repo, re->ref);
4984 err = got_object_open_as_tag(&se_tag, repo, se_id);
4988 se_time = got_object_tag_get_tagger_time(se_tag);
4989 got_object_tag_close(se_tag);
4991 if (se_time > re_time) {
4992 err = got_reflist_entry_dup(&new, re);
4995 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4998 se = SIMPLEQ_NEXT(se, entry);
5007 static const struct got_error *
5008 list_tags(struct got_repository *repo, struct got_worktree *worktree)
5010 static const struct got_error *err = NULL;
5011 struct got_reflist_head refs;
5012 struct got_reflist_entry *re;
5014 SIMPLEQ_INIT(&refs);
5016 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5020 SIMPLEQ_FOREACH(re, &refs, entry) {
5021 const char *refname;
5022 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5026 struct got_object_id *id;
5027 struct got_tag_object *tag;
5028 struct got_commit_object *commit = NULL;
5030 refname = got_ref_get_name(re->ref);
5031 if (strncmp(refname, "refs/tags/", 10) != 0)
5034 refstr = got_ref_to_str(re->ref);
5035 if (refstr == NULL) {
5036 err = got_error_from_errno("got_ref_to_str");
5039 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5042 err = got_ref_resolve(&id, repo, re->ref);
5045 err = got_object_open_as_tag(&tag, repo, id);
5047 if (err->code != GOT_ERR_OBJ_TYPE) {
5051 /* "lightweight" tag */
5052 err = got_object_open_as_commit(&commit, repo, id);
5057 tagger = got_object_commit_get_committer(commit);
5059 got_object_commit_get_committer_time(commit);
5060 err = got_object_id_str(&id_str, id);
5066 tagger = got_object_tag_get_tagger(tag);
5067 tagger_time = got_object_tag_get_tagger_time(tag);
5068 err = got_object_id_str(&id_str,
5069 got_object_tag_get_object_id(tag));
5073 printf("from: %s\n", tagger);
5074 datestr = get_datestr(&tagger_time, datebuf);
5076 printf("date: %s UTC\n", datestr);
5078 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5080 switch (got_object_tag_get_object_type(tag)) {
5081 case GOT_OBJ_TYPE_BLOB:
5082 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5085 case GOT_OBJ_TYPE_TREE:
5086 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5089 case GOT_OBJ_TYPE_COMMIT:
5090 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5093 case GOT_OBJ_TYPE_TAG:
5094 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5103 err = got_object_commit_get_logmsg(&tagmsg0, commit);
5106 got_object_commit_close(commit);
5108 tagmsg0 = strdup(got_object_tag_get_message(tag));
5109 got_object_tag_close(tag);
5110 if (tagmsg0 == NULL) {
5111 err = got_error_from_errno("strdup");
5118 line = strsep(&tagmsg, "\n");
5120 printf(" %s\n", line);
5125 got_ref_list_free(&refs);
5129 static const struct got_error *
5130 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5131 const char *tag_name, const char *repo_path)
5133 const struct got_error *err = NULL;
5134 char *template = NULL, *initial_content = NULL;
5135 char *editor = NULL;
5138 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5139 err = got_error_from_errno("asprintf");
5143 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5144 commit_id_str, tag_name) == -1) {
5145 err = got_error_from_errno("asprintf");
5149 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5153 dprintf(fd, initial_content);
5156 err = get_editor(&editor);
5159 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5161 free(initial_content);
5165 /* Editor is done; we can now apply unveil(2) */
5167 err = apply_unveil(repo_path, 0, NULL);
5176 static const struct got_error *
5177 add_tag(struct got_repository *repo, const char *tag_name,
5178 const char *commit_arg, const char *tagmsg_arg)
5180 const struct got_error *err = NULL;
5181 struct got_object_id *commit_id = NULL, *tag_id = NULL;
5182 char *label = NULL, *commit_id_str = NULL;
5183 struct got_reference *ref = NULL;
5184 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5185 char *tagmsg_path = NULL, *tag_id_str = NULL;
5186 int preserve_tagmsg = 0;
5189 * Don't let the user create a tag name with a leading '-'.
5190 * While technically a valid reference name, this case is usually
5191 * an unintended typo.
5193 if (tag_name[0] == '-')
5194 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5196 err = get_author(&tagger, repo);
5200 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5201 GOT_OBJ_TYPE_COMMIT, 1, repo);
5205 err = got_object_id_str(&commit_id_str, commit_id);
5209 if (strncmp("refs/tags/", tag_name, 10) == 0) {
5210 refname = strdup(tag_name);
5211 if (refname == NULL) {
5212 err = got_error_from_errno("strdup");
5216 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5217 err = got_error_from_errno("asprintf");
5221 err = got_ref_open(&ref, repo, refname, 0);
5223 err = got_error(GOT_ERR_TAG_EXISTS);
5225 } else if (err->code != GOT_ERR_NOT_REF)
5228 if (tagmsg_arg == NULL) {
5229 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5230 tag_name, got_repo_get_path(repo));
5232 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5233 tagmsg_path != NULL)
5234 preserve_tagmsg = 1;
5239 err = got_object_tag_create(&tag_id, tag_name, commit_id,
5240 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5243 preserve_tagmsg = 1;
5247 err = got_ref_alloc(&ref, refname, tag_id);
5250 preserve_tagmsg = 1;
5254 err = got_ref_write(ref, repo);
5257 preserve_tagmsg = 1;
5261 err = got_object_id_str(&tag_id_str, tag_id);
5264 preserve_tagmsg = 1;
5267 printf("Created tag %s\n", tag_id_str);
5269 if (preserve_tagmsg) {
5270 fprintf(stderr, "%s: tag message preserved in %s\n",
5271 getprogname(), tagmsg_path);
5272 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5273 err = got_error_from_errno2("unlink", tagmsg_path);
5278 free(commit_id_str);
5286 static const struct got_error *
5287 cmd_tag(int argc, char *argv[])
5289 const struct got_error *error = NULL;
5290 struct got_repository *repo = NULL;
5291 struct got_worktree *worktree = NULL;
5292 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5293 char *gitconfig_path = NULL;
5294 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5295 int ch, do_list = 0;
5297 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5300 commit_id_arg = optarg;
5306 repo_path = realpath(optarg, NULL);
5307 if (repo_path == NULL)
5308 return got_error_from_errno2("realpath",
5310 got_path_strip_trailing_slashes(repo_path);
5325 if (commit_id_arg != NULL)
5327 "-c option can only be used when creating a tag");
5329 errx(1, "-l and -m options are mutually exclusive");
5332 } else if (argc != 1)
5339 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5343 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5344 "sendfd unveil", NULL) == -1)
5348 cwd = getcwd(NULL, 0);
5350 error = got_error_from_errno("getcwd");
5354 if (repo_path == NULL) {
5355 error = got_worktree_open(&worktree, cwd);
5356 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5362 strdup(got_worktree_get_repo_path(worktree));
5363 if (repo_path == NULL)
5364 error = got_error_from_errno("strdup");
5368 repo_path = strdup(cwd);
5369 if (repo_path == NULL) {
5370 error = got_error_from_errno("strdup");
5377 error = got_repo_open(&repo, repo_path, NULL);
5380 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5383 error = list_tags(repo, worktree);
5385 error = get_gitconfig_path(&gitconfig_path);
5388 error = got_repo_open(&repo, repo_path, gitconfig_path);
5393 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5398 if (commit_id_arg == NULL) {
5399 struct got_reference *head_ref;
5400 struct got_object_id *commit_id;
5401 error = got_ref_open(&head_ref, repo,
5402 worktree ? got_worktree_get_head_ref_name(worktree)
5406 error = got_ref_resolve(&commit_id, repo, head_ref);
5407 got_ref_close(head_ref);
5410 error = got_object_id_str(&commit_id_str, commit_id);
5416 error = add_tag(repo, tag_name,
5417 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5421 got_repo_close(repo);
5423 got_worktree_close(worktree);
5426 free(gitconfig_path);
5427 free(commit_id_str);
5434 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5439 static const struct got_error *
5440 add_progress(void *arg, unsigned char status, const char *path)
5442 while (path[0] == '/')
5444 printf("%c %s\n", status, path);
5448 static const struct got_error *
5449 cmd_add(int argc, char *argv[])
5451 const struct got_error *error = NULL;
5452 struct got_repository *repo = NULL;
5453 struct got_worktree *worktree = NULL;
5455 struct got_pathlist_head paths;
5456 struct got_pathlist_entry *pe;
5457 int ch, can_recurse = 0, no_ignores = 0;
5461 while ((ch = getopt(argc, argv, "IR")) != -1) {
5479 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5486 cwd = getcwd(NULL, 0);
5488 error = got_error_from_errno("getcwd");
5492 error = got_worktree_open(&worktree, cwd);
5496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5501 error = apply_unveil(got_repo_get_path(repo), 1,
5502 got_worktree_get_root_path(worktree));
5506 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5510 if (!can_recurse && no_ignores) {
5511 error = got_error_msg(GOT_ERR_BAD_PATH,
5512 "disregarding ignores requires -R option");
5520 TAILQ_FOREACH(pe, &paths, entry) {
5521 if (asprintf(&ondisk_path, "%s/%s",
5522 got_worktree_get_root_path(worktree),
5524 error = got_error_from_errno("asprintf");
5527 if (lstat(ondisk_path, &sb) == -1) {
5528 if (errno == ENOENT) {
5532 error = got_error_from_errno2("lstat",
5538 if (S_ISDIR(sb.st_mode)) {
5539 error = got_error_msg(GOT_ERR_BAD_PATH,
5540 "adding directories requires -R option");
5546 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5547 NULL, repo, no_ignores);
5550 got_repo_close(repo);
5552 got_worktree_close(worktree);
5553 TAILQ_FOREACH(pe, &paths, entry)
5554 free((char *)pe->path);
5555 got_pathlist_free(&paths);
5563 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5568 static const struct got_error *
5569 print_remove_status(void *arg, unsigned char status,
5570 unsigned char staged_status, const char *path)
5572 while (path[0] == '/')
5574 if (status == GOT_STATUS_NONEXISTENT)
5576 if (status == staged_status && (status == GOT_STATUS_DELETE))
5577 status = GOT_STATUS_NO_CHANGE;
5578 printf("%c%c %s\n", status, staged_status, path);
5582 static const struct got_error *
5583 cmd_remove(int argc, char *argv[])
5585 const struct got_error *error = NULL;
5586 struct got_worktree *worktree = NULL;
5587 struct got_repository *repo = NULL;
5589 struct got_pathlist_head paths;
5590 struct got_pathlist_entry *pe;
5591 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5595 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5598 delete_local_mods = 1;
5616 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5623 cwd = getcwd(NULL, 0);
5625 error = got_error_from_errno("getcwd");
5628 error = got_worktree_open(&worktree, cwd);
5632 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5637 error = apply_unveil(got_repo_get_path(repo), 1,
5638 got_worktree_get_root_path(worktree));
5642 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5649 TAILQ_FOREACH(pe, &paths, entry) {
5650 if (asprintf(&ondisk_path, "%s/%s",
5651 got_worktree_get_root_path(worktree),
5653 error = got_error_from_errno("asprintf");
5656 if (lstat(ondisk_path, &sb) == -1) {
5657 if (errno == ENOENT) {
5661 error = got_error_from_errno2("lstat",
5667 if (S_ISDIR(sb.st_mode)) {
5668 error = got_error_msg(GOT_ERR_BAD_PATH,
5669 "removing directories requires -R option");
5675 error = got_worktree_schedule_delete(worktree, &paths,
5676 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5679 got_repo_close(repo);
5681 got_worktree_close(worktree);
5682 TAILQ_FOREACH(pe, &paths, entry)
5683 free((char *)pe->path);
5684 got_pathlist_free(&paths);
5692 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5693 "path ...\n", getprogname());
5697 static const struct got_error *
5698 revert_progress(void *arg, unsigned char status, const char *path)
5700 if (status == GOT_STATUS_UNVERSIONED)
5703 while (path[0] == '/')
5705 printf("%c %s\n", status, path);
5709 struct choose_patch_arg {
5710 FILE *patch_script_file;
5714 static const struct got_error *
5715 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5716 int nchanges, const char *action)
5719 size_t linesize = 0;
5723 case GOT_STATUS_ADD:
5724 printf("A %s\n%s this addition? [y/n] ", path, action);
5726 case GOT_STATUS_DELETE:
5727 printf("D %s\n%s this deletion? [y/n] ", path, action);
5729 case GOT_STATUS_MODIFY:
5730 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5731 return got_error_from_errno("fseek");
5732 printf(GOT_COMMIT_SEP_STR);
5733 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5735 if (ferror(patch_file))
5736 return got_error_from_errno("getline");
5737 printf(GOT_COMMIT_SEP_STR);
5738 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5739 path, n, nchanges, action);
5742 return got_error_path(path, GOT_ERR_FILE_STATUS);
5748 static const struct got_error *
5749 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5750 FILE *patch_file, int n, int nchanges)
5752 const struct got_error *err = NULL;
5754 size_t linesize = 0;
5757 struct choose_patch_arg *a = arg;
5759 *choice = GOT_PATCH_CHOICE_NONE;
5761 if (a->patch_script_file) {
5763 err = show_change(status, path, patch_file, n, nchanges,
5767 linelen = getline(&line, &linesize, a->patch_script_file);
5768 if (linelen == -1) {
5769 if (ferror(a->patch_script_file))
5770 return got_error_from_errno("getline");
5773 nl = strchr(line, '\n');
5776 if (strcmp(line, "y") == 0) {
5777 *choice = GOT_PATCH_CHOICE_YES;
5779 } else if (strcmp(line, "n") == 0) {
5780 *choice = GOT_PATCH_CHOICE_NO;
5782 } else if (strcmp(line, "q") == 0 &&
5783 status == GOT_STATUS_MODIFY) {
5784 *choice = GOT_PATCH_CHOICE_QUIT;
5787 printf("invalid response '%s'\n", line);
5792 while (resp != 'y' && resp != 'n' && resp != 'q') {
5793 err = show_change(status, path, patch_file, n, nchanges,
5800 if (status == GOT_STATUS_MODIFY) {
5801 if (resp != 'y' && resp != 'n' && resp != 'q') {
5802 printf("invalid response '%c'\n", resp);
5805 } else if (resp != 'y' && resp != 'n') {
5806 printf("invalid response '%c'\n", resp);
5812 *choice = GOT_PATCH_CHOICE_YES;
5813 else if (resp == 'n')
5814 *choice = GOT_PATCH_CHOICE_NO;
5815 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5816 *choice = GOT_PATCH_CHOICE_QUIT;
5822 static const struct got_error *
5823 cmd_revert(int argc, char *argv[])
5825 const struct got_error *error = NULL;
5826 struct got_worktree *worktree = NULL;
5827 struct got_repository *repo = NULL;
5828 char *cwd = NULL, *path = NULL;
5829 struct got_pathlist_head paths;
5830 struct got_pathlist_entry *pe;
5831 int ch, can_recurse = 0, pflag = 0;
5832 FILE *patch_script_file = NULL;
5833 const char *patch_script_path = NULL;
5834 struct choose_patch_arg cpa;
5838 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5844 patch_script_path = optarg;
5859 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5860 "unveil", NULL) == -1)
5865 if (patch_script_path && !pflag)
5866 errx(1, "-F option can only be used together with -p option");
5868 cwd = getcwd(NULL, 0);
5870 error = got_error_from_errno("getcwd");
5873 error = got_worktree_open(&worktree, cwd);
5877 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5882 if (patch_script_path) {
5883 patch_script_file = fopen(patch_script_path, "r");
5884 if (patch_script_file == NULL) {
5885 error = got_error_from_errno2("fopen",
5890 error = apply_unveil(got_repo_get_path(repo), 1,
5891 got_worktree_get_root_path(worktree));
5895 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5902 TAILQ_FOREACH(pe, &paths, entry) {
5903 if (asprintf(&ondisk_path, "%s/%s",
5904 got_worktree_get_root_path(worktree),
5906 error = got_error_from_errno("asprintf");
5909 if (lstat(ondisk_path, &sb) == -1) {
5910 if (errno == ENOENT) {
5914 error = got_error_from_errno2("lstat",
5920 if (S_ISDIR(sb.st_mode)) {
5921 error = got_error_msg(GOT_ERR_BAD_PATH,
5922 "reverting directories requires -R option");
5928 cpa.patch_script_file = patch_script_file;
5929 cpa.action = "revert";
5930 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5931 pflag ? choose_patch : NULL, &cpa, repo);
5933 if (patch_script_file && fclose(patch_script_file) == EOF &&
5935 error = got_error_from_errno2("fclose", patch_script_path);
5937 got_repo_close(repo);
5939 got_worktree_close(worktree);
5948 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5953 struct collect_commit_logmsg_arg {
5954 const char *cmdline_log;
5956 const char *worktree_path;
5957 const char *branch_name;
5958 const char *repo_path;
5963 static const struct got_error *
5964 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5967 char *initial_content = NULL;
5968 struct got_pathlist_entry *pe;
5969 const struct got_error *err = NULL;
5970 char *template = NULL;
5971 struct collect_commit_logmsg_arg *a = arg;
5975 /* if a message was specified on the command line, just use it */
5976 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5977 len = strlen(a->cmdline_log) + 1;
5978 *logmsg = malloc(len + 1);
5979 if (*logmsg == NULL)
5980 return got_error_from_errno("malloc");
5981 strlcpy(*logmsg, a->cmdline_log, len);
5985 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5986 return got_error_from_errno("asprintf");
5988 if (asprintf(&initial_content,
5989 "\n# changes to be committed on branch %s:\n",
5990 a->branch_name) == -1)
5991 return got_error_from_errno("asprintf");
5993 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5997 dprintf(fd, initial_content);
5999 TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 struct got_commitable *ct = pe->data;
6001 dprintf(fd, "# %c %s\n",
6002 got_commitable_get_status(ct),
6003 got_commitable_get_path(ct));
6007 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6009 free(initial_content);
6012 /* Editor is done; we can now apply unveil(2) */
6014 err = apply_unveil(a->repo_path, 0, a->worktree_path);
6023 static const struct got_error *
6024 cmd_commit(int argc, char *argv[])
6026 const struct got_error *error = NULL;
6027 struct got_worktree *worktree = NULL;
6028 struct got_repository *repo = NULL;
6029 char *cwd = NULL, *id_str = NULL;
6030 struct got_object_id *id = NULL;
6031 const char *logmsg = NULL;
6032 struct collect_commit_logmsg_arg cl_arg;
6033 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6034 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6035 struct got_pathlist_head paths;
6038 cl_arg.logmsg_path = NULL;
6040 while ((ch = getopt(argc, argv, "m:")) != -1) {
6055 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6056 "unveil", NULL) == -1)
6059 cwd = getcwd(NULL, 0);
6061 error = got_error_from_errno("getcwd");
6064 error = got_worktree_open(&worktree, cwd);
6068 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6071 if (rebase_in_progress) {
6072 error = got_error(GOT_ERR_REBASING);
6076 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6081 error = get_gitconfig_path(&gitconfig_path);
6084 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6089 error = get_author(&author, repo);
6094 * unveil(2) traverses exec(2); if an editor is used we have
6095 * to apply unveil after the log message has been written.
6097 if (logmsg == NULL || strlen(logmsg) == 0)
6098 error = get_editor(&editor);
6100 error = apply_unveil(got_repo_get_path(repo), 0,
6101 got_worktree_get_root_path(worktree));
6105 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6109 cl_arg.editor = editor;
6110 cl_arg.cmdline_log = logmsg;
6111 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6112 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6113 if (!histedit_in_progress) {
6114 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6115 error = got_error(GOT_ERR_COMMIT_BRANCH);
6118 cl_arg.branch_name += 11;
6120 cl_arg.repo_path = got_repo_get_path(repo);
6121 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6122 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6124 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6125 cl_arg.logmsg_path != NULL)
6126 preserve_logmsg = 1;
6130 error = got_object_id_str(&id_str, id);
6133 printf("Created commit %s\n", id_str);
6135 if (preserve_logmsg) {
6136 fprintf(stderr, "%s: log message preserved in %s\n",
6137 getprogname(), cl_arg.logmsg_path);
6138 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6140 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6141 free(cl_arg.logmsg_path);
6143 got_repo_close(repo);
6145 got_worktree_close(worktree);
6148 free(gitconfig_path);
6155 usage_cherrypick(void)
6157 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6161 static const struct got_error *
6162 cmd_cherrypick(int argc, char *argv[])
6164 const struct got_error *error = NULL;
6165 struct got_worktree *worktree = NULL;
6166 struct got_repository *repo = NULL;
6167 char *cwd = NULL, *commit_id_str = NULL;
6168 struct got_object_id *commit_id = NULL;
6169 struct got_commit_object *commit = NULL;
6170 struct got_object_qid *pid;
6171 struct got_reference *head_ref = NULL;
6172 int ch, did_something = 0;
6174 while ((ch = getopt(argc, argv, "")) != -1) {
6186 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6187 "unveil", NULL) == -1)
6193 cwd = getcwd(NULL, 0);
6195 error = got_error_from_errno("getcwd");
6198 error = got_worktree_open(&worktree, cwd);
6202 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6207 error = apply_unveil(got_repo_get_path(repo), 0,
6208 got_worktree_get_root_path(worktree));
6212 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6213 GOT_OBJ_TYPE_COMMIT, repo);
6214 if (error != NULL) {
6215 struct got_reference *ref;
6216 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6218 error = got_ref_open(&ref, repo, argv[0], 0);
6221 error = got_ref_resolve(&commit_id, repo, ref);
6226 error = got_object_id_str(&commit_id_str, commit_id);
6230 error = got_ref_open(&head_ref, repo,
6231 got_worktree_get_head_ref_name(worktree), 0);
6235 error = check_same_branch(commit_id, head_ref, NULL, repo);
6237 if (error->code != GOT_ERR_ANCESTRY)
6241 error = got_error(GOT_ERR_SAME_BRANCH);
6245 error = got_object_open_as_commit(&commit, repo, commit_id);
6248 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6249 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6250 commit_id, repo, update_progress, &did_something, check_cancelled,
6256 printf("Merged commit %s\n", commit_id_str);
6259 got_object_commit_close(commit);
6260 free(commit_id_str);
6262 got_ref_close(head_ref);
6264 got_worktree_close(worktree);
6266 got_repo_close(repo);
6273 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6277 static const struct got_error *
6278 cmd_backout(int argc, char *argv[])
6280 const struct got_error *error = NULL;
6281 struct got_worktree *worktree = NULL;
6282 struct got_repository *repo = NULL;
6283 char *cwd = NULL, *commit_id_str = NULL;
6284 struct got_object_id *commit_id = NULL;
6285 struct got_commit_object *commit = NULL;
6286 struct got_object_qid *pid;
6287 struct got_reference *head_ref = NULL;
6288 int ch, did_something = 0;
6290 while ((ch = getopt(argc, argv, "")) != -1) {
6302 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6303 "unveil", NULL) == -1)
6309 cwd = getcwd(NULL, 0);
6311 error = got_error_from_errno("getcwd");
6314 error = got_worktree_open(&worktree, cwd);
6318 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6323 error = apply_unveil(got_repo_get_path(repo), 0,
6324 got_worktree_get_root_path(worktree));
6328 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6329 GOT_OBJ_TYPE_COMMIT, repo);
6330 if (error != NULL) {
6331 struct got_reference *ref;
6332 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6334 error = got_ref_open(&ref, repo, argv[0], 0);
6337 error = got_ref_resolve(&commit_id, repo, ref);
6342 error = got_object_id_str(&commit_id_str, commit_id);
6346 error = got_ref_open(&head_ref, repo,
6347 got_worktree_get_head_ref_name(worktree), 0);
6351 error = check_same_branch(commit_id, head_ref, NULL, repo);
6355 error = got_object_open_as_commit(&commit, repo, commit_id);
6358 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6360 error = got_error(GOT_ERR_ROOT_COMMIT);
6364 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6365 update_progress, &did_something, check_cancelled, NULL);
6370 printf("Backed out commit %s\n", commit_id_str);
6373 got_object_commit_close(commit);
6374 free(commit_id_str);
6376 got_ref_close(head_ref);
6378 got_worktree_close(worktree);
6380 got_repo_close(repo);
6387 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6393 trim_logmsg(char *logmsg, int limit)
6398 len = strlen(logmsg);
6402 nl = strchr(logmsg, '\n');
6407 static const struct got_error *
6408 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6410 const struct got_error *err;
6411 char *logmsg0 = NULL;
6414 err = got_object_commit_get_logmsg(&logmsg0, commit);
6419 while (isspace((unsigned char)s[0]))
6422 *logmsg = strdup(s);
6423 if (*logmsg == NULL) {
6424 err = got_error_from_errno("strdup");
6428 trim_logmsg(*logmsg, limit);
6434 static const struct got_error *
6435 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6437 const struct got_error *err;
6438 struct got_commit_object *commit = NULL;
6439 char *id_str = NULL, *logmsg = NULL;
6441 err = got_object_open_as_commit(&commit, repo, id);
6445 err = got_object_id_str(&id_str, id);
6451 err = get_short_logmsg(&logmsg, 42, commit);
6455 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6458 got_object_commit_close(commit);
6463 static const struct got_error *
6464 show_rebase_progress(struct got_commit_object *commit,
6465 struct got_object_id *old_id, struct got_object_id *new_id)
6467 const struct got_error *err;
6468 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6470 err = got_object_id_str(&old_id_str, old_id);
6475 err = got_object_id_str(&new_id_str, new_id);
6480 old_id_str[12] = '\0';
6482 new_id_str[12] = '\0';
6484 err = get_short_logmsg(&logmsg, 42, commit);
6488 printf("%s -> %s: %s\n", old_id_str,
6489 new_id_str ? new_id_str : "no-op change", logmsg);
6497 static const struct got_error *
6498 rebase_progress(void *arg, unsigned char status, const char *path)
6500 unsigned char *rebase_status = arg;
6502 while (path[0] == '/')
6504 printf("%c %s\n", status, path);
6506 if (*rebase_status == GOT_STATUS_CONFLICT)
6508 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6509 *rebase_status = status;
6513 static const struct got_error *
6514 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6515 struct got_reference *branch, struct got_reference *new_base_branch,
6516 struct got_reference *tmp_branch, struct got_repository *repo)
6518 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6519 return got_worktree_rebase_complete(worktree, fileindex,
6520 new_base_branch, tmp_branch, branch, repo);
6523 static const struct got_error *
6524 rebase_commit(struct got_pathlist_head *merged_paths,
6525 struct got_worktree *worktree, struct got_fileindex *fileindex,
6526 struct got_reference *tmp_branch,
6527 struct got_object_id *commit_id, struct got_repository *repo)
6529 const struct got_error *error;
6530 struct got_commit_object *commit;
6531 struct got_object_id *new_commit_id;
6533 error = got_object_open_as_commit(&commit, repo, commit_id);
6537 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6538 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6540 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6542 error = show_rebase_progress(commit, commit_id, NULL);
6544 error = show_rebase_progress(commit, commit_id, new_commit_id);
6545 free(new_commit_id);
6548 got_object_commit_close(commit);
6552 struct check_path_prefix_arg {
6553 const char *path_prefix;
6558 static const struct got_error *
6559 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6560 struct got_blob_object *blob2, struct got_object_id *id1,
6561 struct got_object_id *id2, const char *path1, const char *path2,
6562 mode_t mode1, mode_t mode2, struct got_repository *repo)
6564 struct check_path_prefix_arg *a = arg;
6566 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6567 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6568 return got_error(a->errcode);
6573 static const struct got_error *
6574 check_path_prefix(struct got_object_id *parent_id,
6575 struct got_object_id *commit_id, const char *path_prefix,
6576 int errcode, struct got_repository *repo)
6578 const struct got_error *err;
6579 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6580 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6581 struct check_path_prefix_arg cpp_arg;
6583 if (got_path_is_root_dir(path_prefix))
6586 err = got_object_open_as_commit(&commit, repo, commit_id);
6590 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6594 err = got_object_open_as_tree(&tree1, repo,
6595 got_object_commit_get_tree_id(parent_commit));
6599 err = got_object_open_as_tree(&tree2, repo,
6600 got_object_commit_get_tree_id(commit));
6604 cpp_arg.path_prefix = path_prefix;
6605 while (cpp_arg.path_prefix[0] == '/')
6606 cpp_arg.path_prefix++;
6607 cpp_arg.len = strlen(cpp_arg.path_prefix);
6608 cpp_arg.errcode = errcode;
6609 err = got_diff_tree(tree1, tree2, "", "", repo,
6610 check_path_prefix_in_diff, &cpp_arg, 0);
6613 got_object_tree_close(tree1);
6615 got_object_tree_close(tree2);
6617 got_object_commit_close(commit);
6619 got_object_commit_close(parent_commit);
6623 static const struct got_error *
6624 collect_commits(struct got_object_id_queue *commits,
6625 struct got_object_id *initial_commit_id,
6626 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6627 const char *path_prefix, int path_prefix_errcode,
6628 struct got_repository *repo)
6630 const struct got_error *err = NULL;
6631 struct got_commit_graph *graph = NULL;
6632 struct got_object_id *parent_id = NULL;
6633 struct got_object_qid *qid;
6634 struct got_object_id *commit_id = initial_commit_id;
6636 err = got_commit_graph_open(&graph, "/", 1);
6640 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6641 check_cancelled, NULL);
6644 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6645 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6646 check_cancelled, NULL);
6648 if (err->code == GOT_ERR_ITER_COMPLETED) {
6649 err = got_error_msg(GOT_ERR_ANCESTRY,
6650 "ran out of commits to rebase before "
6651 "youngest common ancestor commit has "
6656 err = check_path_prefix(parent_id, commit_id,
6657 path_prefix, path_prefix_errcode, repo);
6661 err = got_object_qid_alloc(&qid, commit_id);
6664 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6665 commit_id = parent_id;
6669 got_commit_graph_close(graph);
6673 static const struct got_error *
6674 cmd_rebase(int argc, char *argv[])
6676 const struct got_error *error = NULL;
6677 struct got_worktree *worktree = NULL;
6678 struct got_repository *repo = NULL;
6679 struct got_fileindex *fileindex = NULL;
6681 struct got_reference *branch = NULL;
6682 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6683 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6684 struct got_object_id *resume_commit_id = NULL;
6685 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6686 struct got_commit_object *commit = NULL;
6687 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6688 int histedit_in_progress = 0;
6689 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 struct got_object_id_queue commits;
6691 struct got_pathlist_head merged_paths;
6692 const struct got_object_id_queue *parent_ids;
6693 struct got_object_qid *qid, *pid;
6695 SIMPLEQ_INIT(&commits);
6696 TAILQ_INIT(&merged_paths);
6698 while ((ch = getopt(argc, argv, "ac")) != -1) {
6704 continue_rebase = 1;
6716 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6717 "unveil", NULL) == -1)
6720 if (abort_rebase && continue_rebase)
6722 else if (abort_rebase || continue_rebase) {
6725 } else if (argc != 1)
6728 cwd = getcwd(NULL, 0);
6730 error = got_error_from_errno("getcwd");
6733 error = got_worktree_open(&worktree, cwd);
6737 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6742 error = apply_unveil(got_repo_get_path(repo), 0,
6743 got_worktree_get_root_path(worktree));
6747 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6751 if (histedit_in_progress) {
6752 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6756 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6762 if (!rebase_in_progress) {
6763 error = got_error(GOT_ERR_NOT_REBASING);
6766 error = got_worktree_rebase_continue(&resume_commit_id,
6767 &new_base_branch, &tmp_branch, &branch, &fileindex,
6771 printf("Switching work tree to %s\n",
6772 got_ref_get_symref_target(new_base_branch));
6773 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6774 new_base_branch, update_progress, &did_something);
6777 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6778 goto done; /* nothing else to do */
6781 if (continue_rebase) {
6782 if (!rebase_in_progress) {
6783 error = got_error(GOT_ERR_NOT_REBASING);
6786 error = got_worktree_rebase_continue(&resume_commit_id,
6787 &new_base_branch, &tmp_branch, &branch, &fileindex,
6792 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6793 resume_commit_id, repo);
6797 yca_id = got_object_id_dup(resume_commit_id);
6798 if (yca_id == NULL) {
6799 error = got_error_from_errno("got_object_id_dup");
6803 error = got_ref_open(&branch, repo, argv[0], 0);
6808 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6812 if (!continue_rebase) {
6813 struct got_object_id *base_commit_id;
6815 base_commit_id = got_worktree_get_base_commit_id(worktree);
6816 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6817 base_commit_id, branch_head_commit_id, repo,
6818 check_cancelled, NULL);
6821 if (yca_id == NULL) {
6822 error = got_error_msg(GOT_ERR_ANCESTRY,
6823 "specified branch shares no common ancestry "
6824 "with work tree's branch");
6828 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6830 if (error->code != GOT_ERR_ANCESTRY)
6834 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6835 "specified branch resolves to a commit which "
6836 "is already contained in work tree's branch");
6839 error = got_worktree_rebase_prepare(&new_base_branch,
6840 &tmp_branch, &fileindex, worktree, branch, repo);
6845 commit_id = branch_head_commit_id;
6846 error = got_object_open_as_commit(&commit, repo, commit_id);
6850 parent_ids = got_object_commit_get_parent_ids(commit);
6851 pid = SIMPLEQ_FIRST(parent_ids);
6853 if (!continue_rebase) {
6855 error = got_worktree_rebase_abort(worktree, fileindex,
6856 repo, new_base_branch, update_progress,
6860 printf("Rebase of %s aborted\n",
6861 got_ref_get_name(branch));
6863 error = got_error(GOT_ERR_EMPTY_REBASE);
6866 error = collect_commits(&commits, commit_id, pid->id,
6867 yca_id, got_worktree_get_path_prefix(worktree),
6868 GOT_ERR_REBASE_PATH, repo);
6869 got_object_commit_close(commit);
6874 if (SIMPLEQ_EMPTY(&commits)) {
6875 if (continue_rebase) {
6876 error = rebase_complete(worktree, fileindex,
6877 branch, new_base_branch, tmp_branch, repo);
6880 /* Fast-forward the reference of the branch. */
6881 struct got_object_id *new_head_commit_id;
6883 error = got_ref_resolve(&new_head_commit_id, repo,
6887 error = got_object_id_str(&id_str, new_head_commit_id);
6888 printf("Forwarding %s to commit %s\n",
6889 got_ref_get_name(branch), id_str);
6891 error = got_ref_change_ref(branch,
6892 new_head_commit_id);
6899 SIMPLEQ_FOREACH(qid, &commits, entry) {
6900 commit_id = qid->id;
6901 parent_id = pid ? pid->id : yca_id;
6904 error = got_worktree_rebase_merge_files(&merged_paths,
6905 worktree, fileindex, parent_id, commit_id, repo,
6906 rebase_progress, &rebase_status, check_cancelled, NULL);
6910 if (rebase_status == GOT_STATUS_CONFLICT) {
6911 error = show_rebase_merge_conflict(qid->id, repo);
6914 got_worktree_rebase_pathlist_free(&merged_paths);
6918 error = rebase_commit(&merged_paths, worktree, fileindex,
6919 tmp_branch, commit_id, repo);
6920 got_worktree_rebase_pathlist_free(&merged_paths);
6925 if (rebase_status == GOT_STATUS_CONFLICT) {
6926 error = got_worktree_rebase_postpone(worktree, fileindex);
6929 error = got_error_msg(GOT_ERR_CONFLICTS,
6930 "conflicts must be resolved before rebasing can continue");
6932 error = rebase_complete(worktree, fileindex, branch,
6933 new_base_branch, tmp_branch, repo);
6935 got_object_id_queue_free(&commits);
6936 free(branch_head_commit_id);
6937 free(resume_commit_id);
6940 got_object_commit_close(commit);
6942 got_ref_close(branch);
6943 if (new_base_branch)
6944 got_ref_close(new_base_branch);
6946 got_ref_close(tmp_branch);
6948 got_worktree_close(worktree);
6950 got_repo_close(repo);
6955 usage_histedit(void)
6957 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6962 #define GOT_HISTEDIT_PICK 'p'
6963 #define GOT_HISTEDIT_EDIT 'e'
6964 #define GOT_HISTEDIT_FOLD 'f'
6965 #define GOT_HISTEDIT_DROP 'd'
6966 #define GOT_HISTEDIT_MESG 'm'
6968 static struct got_histedit_cmd {
6972 } got_histedit_cmds[] = {
6973 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6974 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6975 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6977 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6978 { GOT_HISTEDIT_MESG, "mesg",
6979 "single-line log message for commit above (open editor if empty)" },
6982 struct got_histedit_list_entry {
6983 TAILQ_ENTRY(got_histedit_list_entry) entry;
6984 struct got_object_id *commit_id;
6985 const struct got_histedit_cmd *cmd;
6988 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6990 static const struct got_error *
6991 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6992 FILE *f, struct got_repository *repo)
6994 const struct got_error *err = NULL;
6995 char *logmsg = NULL, *id_str = NULL;
6996 struct got_commit_object *commit = NULL;
6999 err = got_object_open_as_commit(&commit, repo, commit_id);
7003 err = get_short_logmsg(&logmsg, 34, commit);
7007 err = got_object_id_str(&id_str, commit_id);
7011 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7013 err = got_ferror(f, GOT_ERR_IO);
7016 got_object_commit_close(commit);
7022 static const struct got_error *
7023 histedit_write_commit_list(struct got_object_id_queue *commits,
7024 FILE *f, int edit_logmsg_only, struct got_repository *repo)
7026 const struct got_error *err = NULL;
7027 struct got_object_qid *qid;
7029 if (SIMPLEQ_EMPTY(commits))
7030 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7032 SIMPLEQ_FOREACH(qid, commits, entry) {
7033 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7037 if (edit_logmsg_only) {
7038 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7040 err = got_ferror(f, GOT_ERR_IO);
7049 static const struct got_error *
7050 write_cmd_list(FILE *f, const char *branch_name,
7051 struct got_object_id_queue *commits)
7053 const struct got_error *err = NULL;
7056 struct got_object_qid *qid;
7058 qid = SIMPLEQ_FIRST(commits);
7059 err = got_object_id_str(&id_str, qid->id);
7064 "# Editing the history of branch '%s' starting at\n"
7066 "# Commits will be processed in order from top to "
7067 "bottom of this file.\n", branch_name, id_str);
7069 err = got_ferror(f, GOT_ERR_IO);
7073 n = fprintf(f, "# Available histedit commands:\n");
7075 err = got_ferror(f, GOT_ERR_IO);
7079 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7080 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7081 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7084 err = got_ferror(f, GOT_ERR_IO);
7093 static const struct got_error *
7094 histedit_syntax_error(int lineno)
7096 static char msg[42];
7099 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7101 if (ret == -1 || ret >= sizeof(msg))
7102 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7104 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7107 static const struct got_error *
7108 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7109 char *logmsg, struct got_repository *repo)
7111 const struct got_error *err;
7112 struct got_commit_object *folded_commit = NULL;
7113 char *id_str, *folded_logmsg = NULL;
7115 err = got_object_id_str(&id_str, hle->commit_id);
7119 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7123 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7126 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7127 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7128 folded_logmsg) == -1) {
7129 err = got_error_from_errno("asprintf");
7133 got_object_commit_close(folded_commit);
7135 free(folded_logmsg);
7139 static struct got_histedit_list_entry *
7140 get_folded_commits(struct got_histedit_list_entry *hle)
7142 struct got_histedit_list_entry *prev, *folded = NULL;
7144 prev = TAILQ_PREV(hle, got_histedit_list, entry);
7145 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7146 prev->cmd->code == GOT_HISTEDIT_DROP)) {
7147 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7149 prev = TAILQ_PREV(prev, got_histedit_list, entry);
7155 static const struct got_error *
7156 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7157 struct got_repository *repo)
7159 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7160 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7161 const struct got_error *err = NULL;
7162 struct got_commit_object *commit = NULL;
7164 struct got_histedit_list_entry *folded = NULL;
7166 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7170 folded = get_folded_commits(hle);
7172 while (folded != hle) {
7173 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7174 folded = TAILQ_NEXT(folded, entry);
7177 err = append_folded_commit_msg(&new_msg, folded,
7183 folded = TAILQ_NEXT(folded, entry);
7187 err = got_object_id_str(&id_str, hle->commit_id);
7190 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7193 if (asprintf(&new_msg,
7194 "%s\n# original log message of commit %s: %s",
7195 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7196 err = got_error_from_errno("asprintf");
7202 err = got_object_id_str(&id_str, hle->commit_id);
7206 err = got_opentemp_named_fd(&logmsg_path, &fd,
7207 GOT_TMPDIR_STR "/got-logmsg");
7211 dprintf(fd, logmsg);
7214 err = get_editor(&editor);
7218 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7220 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7222 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7225 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7226 err = got_error_from_errno2("unlink", logmsg_path);
7232 got_object_commit_close(commit);
7236 static const struct got_error *
7237 histedit_parse_list(struct got_histedit_list *histedit_cmds,
7238 FILE *f, struct got_repository *repo)
7240 const struct got_error *err = NULL;
7241 char *line = NULL, *p, *end;
7245 const struct got_histedit_cmd *cmd;
7246 struct got_object_id *commit_id = NULL;
7247 struct got_histedit_list_entry *hle = NULL;
7250 len = getline(&line, &size, f);
7252 const struct got_error *getline_err;
7255 getline_err = got_error_from_errno("getline");
7256 err = got_ferror(f, getline_err->code);
7261 while (isspace((unsigned char)p[0]))
7263 if (p[0] == '#' || p[0] == '\0') {
7269 for (i = 0; i < nitems(got_histedit_cmds); i++) {
7270 cmd = &got_histedit_cmds[i];
7271 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7272 isspace((unsigned char)p[strlen(cmd->name)])) {
7273 p += strlen(cmd->name);
7276 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7281 if (i == nitems(got_histedit_cmds)) {
7282 err = histedit_syntax_error(lineno);
7285 while (isspace((unsigned char)p[0]))
7287 if (cmd->code == GOT_HISTEDIT_MESG) {
7288 if (hle == NULL || hle->logmsg != NULL) {
7289 err = got_error(GOT_ERR_HISTEDIT_CMD);
7293 err = histedit_edit_logmsg(hle, repo);
7297 hle->logmsg = strdup(p);
7298 if (hle->logmsg == NULL) {
7299 err = got_error_from_errno("strdup");
7308 while (end[0] && !isspace((unsigned char)end[0]))
7312 err = got_object_resolve_id_str(&commit_id, repo, p);
7314 /* override error code */
7315 err = histedit_syntax_error(lineno);
7319 hle = malloc(sizeof(*hle));
7321 err = got_error_from_errno("malloc");
7325 hle->commit_id = commit_id;
7330 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7338 static const struct got_error *
7339 histedit_check_script(struct got_histedit_list *histedit_cmds,
7340 struct got_object_id_queue *commits, struct got_repository *repo)
7342 const struct got_error *err = NULL;
7343 struct got_object_qid *qid;
7344 struct got_histedit_list_entry *hle;
7345 static char msg[92];
7348 if (TAILQ_EMPTY(histedit_cmds))
7349 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7350 "histedit script contains no commands");
7351 if (SIMPLEQ_EMPTY(commits))
7352 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7354 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7355 struct got_histedit_list_entry *hle2;
7356 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7359 if (got_object_id_cmp(hle->commit_id,
7360 hle2->commit_id) != 0)
7362 err = got_object_id_str(&id_str, hle->commit_id);
7365 snprintf(msg, sizeof(msg), "commit %s is listed "
7366 "more than once in histedit script", id_str);
7368 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7372 SIMPLEQ_FOREACH(qid, commits, entry) {
7373 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7374 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7378 err = got_object_id_str(&id_str, qid->id);
7381 snprintf(msg, sizeof(msg),
7382 "commit %s missing from histedit script", id_str);
7384 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7388 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7389 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7390 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7391 "last commit in histedit script cannot be folded");
7396 static const struct got_error *
7397 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7398 const char *path, struct got_object_id_queue *commits,
7399 struct got_repository *repo)
7401 const struct got_error *err = NULL;
7405 err = get_editor(&editor);
7409 if (spawn_editor(editor, path) == -1) {
7410 err = got_error_from_errno("failed spawning editor");
7414 f = fopen(path, "r");
7416 err = got_error_from_errno("fopen");
7419 err = histedit_parse_list(histedit_cmds, f, repo);
7423 err = histedit_check_script(histedit_cmds, commits, repo);
7425 if (f && fclose(f) != 0 && err == NULL)
7426 err = got_error_from_errno("fclose");
7431 static const struct got_error *
7432 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7433 struct got_object_id_queue *, const char *, const char *,
7434 struct got_repository *);
7436 static const struct got_error *
7437 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7438 struct got_object_id_queue *commits, const char *branch_name,
7439 int edit_logmsg_only, struct got_repository *repo)
7441 const struct got_error *err;
7445 err = got_opentemp_named(&path, &f, "got-histedit");
7449 err = write_cmd_list(f, branch_name, commits);
7453 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7457 if (edit_logmsg_only) {
7459 err = histedit_parse_list(histedit_cmds, f, repo);
7461 if (fclose(f) != 0) {
7462 err = got_error_from_errno("fclose");
7466 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7468 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7469 err->code != GOT_ERR_HISTEDIT_CMD)
7471 err = histedit_edit_list_retry(histedit_cmds, err,
7472 commits, path, branch_name, repo);
7476 if (f && fclose(f) != 0 && err == NULL)
7477 err = got_error_from_errno("fclose");
7478 if (path && unlink(path) != 0 && err == NULL)
7479 err = got_error_from_errno2("unlink", path);
7484 static const struct got_error *
7485 histedit_save_list(struct got_histedit_list *histedit_cmds,
7486 struct got_worktree *worktree, struct got_repository *repo)
7488 const struct got_error *err = NULL;
7491 struct got_histedit_list_entry *hle;
7492 struct got_commit_object *commit = NULL;
7494 err = got_worktree_get_histedit_script_path(&path, worktree);
7498 f = fopen(path, "w");
7500 err = got_error_from_errno2("fopen", path);
7503 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7504 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7510 int n = fprintf(f, "%c %s\n",
7511 GOT_HISTEDIT_MESG, hle->logmsg);
7513 err = got_ferror(f, GOT_ERR_IO);
7519 if (f && fclose(f) != 0 && err == NULL)
7520 err = got_error_from_errno("fclose");
7523 got_object_commit_close(commit);
7528 histedit_free_list(struct got_histedit_list *histedit_cmds)
7530 struct got_histedit_list_entry *hle;
7532 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7533 TAILQ_REMOVE(histedit_cmds, hle, entry);
7538 static const struct got_error *
7539 histedit_load_list(struct got_histedit_list *histedit_cmds,
7540 const char *path, struct got_repository *repo)
7542 const struct got_error *err = NULL;
7545 f = fopen(path, "r");
7547 err = got_error_from_errno2("fopen", path);
7551 err = histedit_parse_list(histedit_cmds, f, repo);
7553 if (f && fclose(f) != 0 && err == NULL)
7554 err = got_error_from_errno("fclose");
7558 static const struct got_error *
7559 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7560 const struct got_error *edit_err, struct got_object_id_queue *commits,
7561 const char *path, const char *branch_name, struct got_repository *repo)
7563 const struct got_error *err = NULL, *prev_err = edit_err;
7566 while (resp != 'c' && resp != 'r' && resp != 'a') {
7567 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7568 "or (a)bort: ", getprogname(), prev_err->msg);
7573 histedit_free_list(histedit_cmds);
7574 err = histedit_run_editor(histedit_cmds, path, commits,
7577 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7578 err->code != GOT_ERR_HISTEDIT_CMD)
7585 } else if (resp == 'r') {
7586 histedit_free_list(histedit_cmds);
7587 err = histedit_edit_script(histedit_cmds,
7588 commits, branch_name, 0, repo);
7590 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7591 err->code != GOT_ERR_HISTEDIT_CMD)
7598 } else if (resp == 'a') {
7599 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7602 printf("invalid response '%c'\n", resp);
7608 static const struct got_error *
7609 histedit_complete(struct got_worktree *worktree,
7610 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7611 struct got_reference *branch, struct got_repository *repo)
7613 printf("Switching work tree to %s\n",
7614 got_ref_get_symref_target(branch));
7615 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7619 static const struct got_error *
7620 show_histedit_progress(struct got_commit_object *commit,
7621 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7623 const struct got_error *err;
7624 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7626 err = got_object_id_str(&old_id_str, hle->commit_id);
7631 err = got_object_id_str(&new_id_str, new_id);
7636 old_id_str[12] = '\0';
7638 new_id_str[12] = '\0';
7641 logmsg = strdup(hle->logmsg);
7642 if (logmsg == NULL) {
7643 err = got_error_from_errno("strdup");
7646 trim_logmsg(logmsg, 42);
7648 err = get_short_logmsg(&logmsg, 42, commit);
7653 switch (hle->cmd->code) {
7654 case GOT_HISTEDIT_PICK:
7655 case GOT_HISTEDIT_EDIT:
7656 printf("%s -> %s: %s\n", old_id_str,
7657 new_id_str ? new_id_str : "no-op change", logmsg);
7659 case GOT_HISTEDIT_DROP:
7660 case GOT_HISTEDIT_FOLD:
7661 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7673 static const struct got_error *
7674 histedit_commit(struct got_pathlist_head *merged_paths,
7675 struct got_worktree *worktree, struct got_fileindex *fileindex,
7676 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7677 struct got_repository *repo)
7679 const struct got_error *err;
7680 struct got_commit_object *commit;
7681 struct got_object_id *new_commit_id;
7683 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7684 && hle->logmsg == NULL) {
7685 err = histedit_edit_logmsg(hle, repo);
7690 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7694 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7695 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7698 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7700 err = show_histedit_progress(commit, hle, NULL);
7702 err = show_histedit_progress(commit, hle, new_commit_id);
7703 free(new_commit_id);
7706 got_object_commit_close(commit);
7710 static const struct got_error *
7711 histedit_skip_commit(struct got_histedit_list_entry *hle,
7712 struct got_worktree *worktree, struct got_repository *repo)
7714 const struct got_error *error;
7715 struct got_commit_object *commit;
7717 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7722 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7726 error = show_histedit_progress(commit, hle, NULL);
7727 got_object_commit_close(commit);
7731 static const struct got_error *
7732 check_local_changes(void *arg, unsigned char status,
7733 unsigned char staged_status, const char *path,
7734 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7735 struct got_object_id *commit_id, int dirfd, const char *de_name)
7737 int *have_local_changes = arg;
7740 case GOT_STATUS_ADD:
7741 case GOT_STATUS_DELETE:
7742 case GOT_STATUS_MODIFY:
7743 case GOT_STATUS_CONFLICT:
7744 *have_local_changes = 1;
7745 return got_error(GOT_ERR_CANCELLED);
7750 switch (staged_status) {
7751 case GOT_STATUS_ADD:
7752 case GOT_STATUS_DELETE:
7753 case GOT_STATUS_MODIFY:
7754 *have_local_changes = 1;
7755 return got_error(GOT_ERR_CANCELLED);
7763 static const struct got_error *
7764 cmd_histedit(int argc, char *argv[])
7766 const struct got_error *error = NULL;
7767 struct got_worktree *worktree = NULL;
7768 struct got_fileindex *fileindex = NULL;
7769 struct got_repository *repo = NULL;
7771 struct got_reference *branch = NULL;
7772 struct got_reference *tmp_branch = NULL;
7773 struct got_object_id *resume_commit_id = NULL;
7774 struct got_object_id *base_commit_id = NULL;
7775 struct got_object_id *head_commit_id = NULL;
7776 struct got_commit_object *commit = NULL;
7777 int ch, rebase_in_progress = 0, did_something;
7778 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7779 int edit_logmsg_only = 0;
7780 const char *edit_script_path = NULL;
7781 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7782 struct got_object_id_queue commits;
7783 struct got_pathlist_head merged_paths;
7784 const struct got_object_id_queue *parent_ids;
7785 struct got_object_qid *pid;
7786 struct got_histedit_list histedit_cmds;
7787 struct got_histedit_list_entry *hle;
7789 SIMPLEQ_INIT(&commits);
7790 TAILQ_INIT(&histedit_cmds);
7791 TAILQ_INIT(&merged_paths);
7793 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7802 edit_script_path = optarg;
7805 edit_logmsg_only = 1;
7817 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7818 "unveil", NULL) == -1)
7821 if (abort_edit && continue_edit)
7822 errx(1, "histedit's -a and -c options are mutually exclusive");
7823 if (edit_script_path && edit_logmsg_only)
7824 errx(1, "histedit's -F and -m options are mutually exclusive");
7825 if (abort_edit && edit_logmsg_only)
7826 errx(1, "histedit's -a and -m options are mutually exclusive");
7827 if (continue_edit && edit_logmsg_only)
7828 errx(1, "histedit's -c and -m options are mutually exclusive");
7833 * This command cannot apply unveil(2) in all cases because the
7834 * user may choose to run an editor to edit the histedit script
7835 * and to edit individual commit log messages.
7836 * unveil(2) traverses exec(2); if an editor is used we have to
7837 * apply unveil after edit script and log messages have been written.
7838 * XXX TODO: Make use of unveil(2) where possible.
7841 cwd = getcwd(NULL, 0);
7843 error = got_error_from_errno("getcwd");
7846 error = got_worktree_open(&worktree, cwd);
7850 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7855 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7858 if (rebase_in_progress) {
7859 error = got_error(GOT_ERR_REBASING);
7863 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7867 if (edit_in_progress && edit_logmsg_only) {
7868 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7869 "histedit operation is in progress in this "
7870 "work tree and must be continued or aborted "
7871 "before the -m option can be used");
7875 if (edit_in_progress && abort_edit) {
7876 error = got_worktree_histedit_continue(&resume_commit_id,
7877 &tmp_branch, &branch, &base_commit_id, &fileindex,
7881 printf("Switching work tree to %s\n",
7882 got_ref_get_symref_target(branch));
7883 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7884 branch, base_commit_id, update_progress, &did_something);
7887 printf("Histedit of %s aborted\n",
7888 got_ref_get_symref_target(branch));
7889 goto done; /* nothing else to do */
7890 } else if (abort_edit) {
7891 error = got_error(GOT_ERR_NOT_HISTEDIT);
7895 if (continue_edit) {
7898 if (!edit_in_progress) {
7899 error = got_error(GOT_ERR_NOT_HISTEDIT);
7903 error = got_worktree_get_histedit_script_path(&path, worktree);
7907 error = histedit_load_list(&histedit_cmds, path, repo);
7912 error = got_worktree_histedit_continue(&resume_commit_id,
7913 &tmp_branch, &branch, &base_commit_id, &fileindex,
7918 error = got_ref_resolve(&head_commit_id, repo, branch);
7922 error = got_object_open_as_commit(&commit, repo,
7926 parent_ids = got_object_commit_get_parent_ids(commit);
7927 pid = SIMPLEQ_FIRST(parent_ids);
7929 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7932 error = collect_commits(&commits, head_commit_id, pid->id,
7933 base_commit_id, got_worktree_get_path_prefix(worktree),
7934 GOT_ERR_HISTEDIT_PATH, repo);
7935 got_object_commit_close(commit);
7940 if (edit_in_progress) {
7941 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7945 error = got_ref_open(&branch, repo,
7946 got_worktree_get_head_ref_name(worktree), 0);
7950 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7951 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7952 "will not edit commit history of a branch outside "
7953 "the \"refs/heads/\" reference namespace");
7957 error = got_ref_resolve(&head_commit_id, repo, branch);
7958 got_ref_close(branch);
7963 error = got_object_open_as_commit(&commit, repo,
7967 parent_ids = got_object_commit_get_parent_ids(commit);
7968 pid = SIMPLEQ_FIRST(parent_ids);
7970 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7973 error = collect_commits(&commits, head_commit_id, pid->id,
7974 got_worktree_get_base_commit_id(worktree),
7975 got_worktree_get_path_prefix(worktree),
7976 GOT_ERR_HISTEDIT_PATH, repo);
7977 got_object_commit_close(commit);
7982 if (SIMPLEQ_EMPTY(&commits)) {
7983 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7987 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7988 &base_commit_id, &fileindex, worktree, repo);
7992 if (edit_script_path) {
7993 error = histedit_load_list(&histedit_cmds,
7994 edit_script_path, repo);
7996 got_worktree_histedit_abort(worktree, fileindex,
7997 repo, branch, base_commit_id,
7998 update_progress, &did_something);
8002 const char *branch_name;
8003 branch_name = got_ref_get_symref_target(branch);
8004 if (strncmp(branch_name, "refs/heads/", 11) == 0)
8006 error = histedit_edit_script(&histedit_cmds, &commits,
8007 branch_name, edit_logmsg_only, repo);
8009 got_worktree_histedit_abort(worktree, fileindex,
8010 repo, branch, base_commit_id,
8011 update_progress, &did_something);
8017 error = histedit_save_list(&histedit_cmds, worktree,
8020 got_worktree_histedit_abort(worktree, fileindex,
8021 repo, branch, base_commit_id,
8022 update_progress, &did_something);
8028 error = histedit_check_script(&histedit_cmds, &commits, repo);
8032 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8033 if (resume_commit_id) {
8034 if (got_object_id_cmp(hle->commit_id,
8035 resume_commit_id) != 0)
8038 resume_commit_id = NULL;
8039 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8040 hle->cmd->code == GOT_HISTEDIT_FOLD) {
8041 error = histedit_skip_commit(hle, worktree,
8046 struct got_pathlist_head paths;
8047 int have_changes = 0;
8050 error = got_pathlist_append(&paths, "", NULL);
8053 error = got_worktree_status(worktree, &paths,
8054 repo, check_local_changes, &have_changes,
8055 check_cancelled, NULL);
8056 got_pathlist_free(&paths);
8058 if (error->code != GOT_ERR_CANCELLED)
8060 if (sigint_received || sigpipe_received)
8064 error = histedit_commit(NULL, worktree,
8065 fileindex, tmp_branch, hle, repo);
8069 error = got_object_open_as_commit(
8070 &commit, repo, hle->commit_id);
8073 error = show_histedit_progress(commit,
8075 got_object_commit_close(commit);
8084 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8085 error = histedit_skip_commit(hle, worktree, repo);
8091 error = got_object_open_as_commit(&commit, repo,
8095 parent_ids = got_object_commit_get_parent_ids(commit);
8096 pid = SIMPLEQ_FIRST(parent_ids);
8098 error = got_worktree_histedit_merge_files(&merged_paths,
8099 worktree, fileindex, pid->id, hle->commit_id, repo,
8100 rebase_progress, &rebase_status, check_cancelled, NULL);
8103 got_object_commit_close(commit);
8106 if (rebase_status == GOT_STATUS_CONFLICT) {
8107 error = show_rebase_merge_conflict(hle->commit_id,
8111 got_worktree_rebase_pathlist_free(&merged_paths);
8115 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8117 error = got_object_id_str(&id_str, hle->commit_id);
8120 printf("Stopping histedit for amending commit %s\n",
8123 got_worktree_rebase_pathlist_free(&merged_paths);
8124 error = got_worktree_histedit_postpone(worktree,
8129 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8130 error = histedit_skip_commit(hle, worktree, repo);
8136 error = histedit_commit(&merged_paths, worktree, fileindex,
8137 tmp_branch, hle, repo);
8138 got_worktree_rebase_pathlist_free(&merged_paths);
8143 if (rebase_status == GOT_STATUS_CONFLICT) {
8144 error = got_worktree_histedit_postpone(worktree, fileindex);
8147 error = got_error_msg(GOT_ERR_CONFLICTS,
8148 "conflicts must be resolved before histedit can continue");
8150 error = histedit_complete(worktree, fileindex, tmp_branch,
8153 got_object_id_queue_free(&commits);
8154 histedit_free_list(&histedit_cmds);
8155 free(head_commit_id);
8156 free(base_commit_id);
8157 free(resume_commit_id);
8159 got_object_commit_close(commit);
8161 got_ref_close(branch);
8163 got_ref_close(tmp_branch);
8165 got_worktree_close(worktree);
8167 got_repo_close(repo);
8172 usage_integrate(void)
8174 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8178 static const struct got_error *
8179 cmd_integrate(int argc, char *argv[])
8181 const struct got_error *error = NULL;
8182 struct got_repository *repo = NULL;
8183 struct got_worktree *worktree = NULL;
8184 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8185 const char *branch_arg = NULL;
8186 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8187 struct got_fileindex *fileindex = NULL;
8188 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8189 int ch, did_something = 0;
8191 while ((ch = getopt(argc, argv, "")) != -1) {
8204 branch_arg = argv[0];
8206 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8207 "unveil", NULL) == -1)
8210 cwd = getcwd(NULL, 0);
8212 error = got_error_from_errno("getcwd");
8216 error = got_worktree_open(&worktree, cwd);
8220 error = check_rebase_or_histedit_in_progress(worktree);
8224 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8229 error = apply_unveil(got_repo_get_path(repo), 0,
8230 got_worktree_get_root_path(worktree));
8234 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8235 error = got_error_from_errno("asprintf");
8239 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8240 &base_branch_ref, worktree, refname, repo);
8244 refname = strdup(got_ref_get_name(branch_ref));
8245 if (refname == NULL) {
8246 error = got_error_from_errno("strdup");
8247 got_worktree_integrate_abort(worktree, fileindex, repo,
8248 branch_ref, base_branch_ref);
8251 base_refname = strdup(got_ref_get_name(base_branch_ref));
8252 if (base_refname == NULL) {
8253 error = got_error_from_errno("strdup");
8254 got_worktree_integrate_abort(worktree, fileindex, repo,
8255 branch_ref, base_branch_ref);
8259 error = got_ref_resolve(&commit_id, repo, branch_ref);
8263 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8267 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8268 error = got_error_msg(GOT_ERR_SAME_BRANCH,
8269 "specified branch has already been integrated");
8270 got_worktree_integrate_abort(worktree, fileindex, repo,
8271 branch_ref, base_branch_ref);
8275 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8277 if (error->code == GOT_ERR_ANCESTRY)
8278 error = got_error(GOT_ERR_REBASE_REQUIRED);
8279 got_worktree_integrate_abort(worktree, fileindex, repo,
8280 branch_ref, base_branch_ref);
8284 error = got_worktree_integrate_continue(worktree, fileindex, repo,
8285 branch_ref, base_branch_ref, update_progress, &did_something,
8286 check_cancelled, NULL);
8290 printf("Integrated %s into %s\n", refname, base_refname);
8293 got_repo_close(repo);
8295 got_worktree_close(worktree);
8297 free(base_commit_id);
8307 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8308 "[file-path ...]\n",
8313 static const struct got_error *
8314 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8315 const char *path, struct got_object_id *blob_id,
8316 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8317 int dirfd, const char *de_name)
8319 const struct got_error *err = NULL;
8320 char *id_str = NULL;
8322 if (staged_status != GOT_STATUS_ADD &&
8323 staged_status != GOT_STATUS_MODIFY &&
8324 staged_status != GOT_STATUS_DELETE)
8327 if (staged_status == GOT_STATUS_ADD ||
8328 staged_status == GOT_STATUS_MODIFY)
8329 err = got_object_id_str(&id_str, staged_blob_id);
8331 err = got_object_id_str(&id_str, blob_id);
8335 printf("%s %c %s\n", id_str, staged_status, path);
8340 static const struct got_error *
8341 cmd_stage(int argc, char *argv[])
8343 const struct got_error *error = NULL;
8344 struct got_repository *repo = NULL;
8345 struct got_worktree *worktree = NULL;
8347 struct got_pathlist_head paths;
8348 struct got_pathlist_entry *pe;
8349 int ch, list_stage = 0, pflag = 0;
8350 FILE *patch_script_file = NULL;
8351 const char *patch_script_path = NULL;
8352 struct choose_patch_arg cpa;
8356 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8365 patch_script_path = optarg;
8377 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8378 "unveil", NULL) == -1)
8381 if (list_stage && (pflag || patch_script_path))
8382 errx(1, "-l option cannot be used with other options");
8383 if (patch_script_path && !pflag)
8384 errx(1, "-F option can only be used together with -p option");
8386 cwd = getcwd(NULL, 0);
8388 error = got_error_from_errno("getcwd");
8392 error = got_worktree_open(&worktree, cwd);
8396 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8401 if (patch_script_path) {
8402 patch_script_file = fopen(patch_script_path, "r");
8403 if (patch_script_file == NULL) {
8404 error = got_error_from_errno2("fopen",
8409 error = apply_unveil(got_repo_get_path(repo), 0,
8410 got_worktree_get_root_path(worktree));
8414 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8419 error = got_worktree_status(worktree, &paths, repo,
8420 print_stage, NULL, check_cancelled, NULL);
8422 cpa.patch_script_file = patch_script_file;
8423 cpa.action = "stage";
8424 error = got_worktree_stage(worktree, &paths,
8425 pflag ? NULL : print_status, NULL,
8426 pflag ? choose_patch : NULL, &cpa, repo);
8429 if (patch_script_file && fclose(patch_script_file) == EOF &&
8431 error = got_error_from_errno2("fclose", patch_script_path);
8433 got_repo_close(repo);
8435 got_worktree_close(worktree);
8436 TAILQ_FOREACH(pe, &paths, entry)
8437 free((char *)pe->path);
8438 got_pathlist_free(&paths);
8446 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8447 "[file-path ...]\n",
8453 static const struct got_error *
8454 cmd_unstage(int argc, char *argv[])
8456 const struct got_error *error = NULL;
8457 struct got_repository *repo = NULL;
8458 struct got_worktree *worktree = NULL;
8460 struct got_pathlist_head paths;
8461 struct got_pathlist_entry *pe;
8462 int ch, did_something = 0, pflag = 0;
8463 FILE *patch_script_file = NULL;
8464 const char *patch_script_path = NULL;
8465 struct choose_patch_arg cpa;
8469 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8475 patch_script_path = optarg;
8487 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8488 "unveil", NULL) == -1)
8491 if (patch_script_path && !pflag)
8492 errx(1, "-F option can only be used together with -p option");
8494 cwd = getcwd(NULL, 0);
8496 error = got_error_from_errno("getcwd");
8500 error = got_worktree_open(&worktree, cwd);
8504 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8509 if (patch_script_path) {
8510 patch_script_file = fopen(patch_script_path, "r");
8511 if (patch_script_file == NULL) {
8512 error = got_error_from_errno2("fopen",
8518 error = apply_unveil(got_repo_get_path(repo), 0,
8519 got_worktree_get_root_path(worktree));
8523 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8527 cpa.patch_script_file = patch_script_file;
8528 cpa.action = "unstage";
8529 error = got_worktree_unstage(worktree, &paths, update_progress,
8530 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8532 if (patch_script_file && fclose(patch_script_file) == EOF &&
8534 error = got_error_from_errno2("fclose", patch_script_path);
8536 got_repo_close(repo);
8538 got_worktree_close(worktree);
8539 TAILQ_FOREACH(pe, &paths, entry)
8540 free((char *)pe->path);
8541 got_pathlist_free(&paths);
8549 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8550 "arg1 [arg2 ...]\n", getprogname());
8554 static const struct got_error *
8555 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8557 const struct got_error *err;
8558 struct got_blob_object *blob;
8560 err = got_object_open_as_blob(&blob, repo, id, 8192);
8564 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8565 got_object_blob_close(blob);
8569 static const struct got_error *
8570 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8572 const struct got_error *err;
8573 struct got_tree_object *tree;
8576 err = got_object_open_as_tree(&tree, repo, id);
8580 nentries = got_object_tree_get_nentries(tree);
8581 for (i = 0; i < nentries; i++) {
8582 struct got_tree_entry *te;
8584 if (sigint_received || sigpipe_received)
8586 te = got_object_tree_get_entry(tree, i);
8587 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8590 fprintf(outfile, "%s %.7o %s\n", id_str,
8591 got_tree_entry_get_mode(te),
8592 got_tree_entry_get_name(te));
8596 got_object_tree_close(tree);
8600 static const struct got_error *
8601 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8603 const struct got_error *err;
8604 struct got_commit_object *commit;
8605 const struct got_object_id_queue *parent_ids;
8606 struct got_object_qid *pid;
8607 char *id_str = NULL;
8608 const char *logmsg = NULL;
8610 err = got_object_open_as_commit(&commit, repo, id);
8614 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8618 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8619 parent_ids = got_object_commit_get_parent_ids(commit);
8620 fprintf(outfile, "numparents %d\n",
8621 got_object_commit_get_nparents(commit));
8622 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8624 err = got_object_id_str(&pid_str, pid->id);
8627 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8630 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8631 got_object_commit_get_author(commit),
8632 got_object_commit_get_author_time(commit));
8634 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8635 got_object_commit_get_author(commit),
8636 got_object_commit_get_committer_time(commit));
8638 logmsg = got_object_commit_get_logmsg_raw(commit);
8639 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8640 fprintf(outfile, "%s", logmsg);
8643 got_object_commit_close(commit);
8647 static const struct got_error *
8648 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8650 const struct got_error *err;
8651 struct got_tag_object *tag;
8652 char *id_str = NULL;
8653 const char *tagmsg = NULL;
8655 err = got_object_open_as_tag(&tag, repo, id);
8659 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8663 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8665 switch (got_object_tag_get_object_type(tag)) {
8666 case GOT_OBJ_TYPE_BLOB:
8667 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8668 GOT_OBJ_LABEL_BLOB);
8670 case GOT_OBJ_TYPE_TREE:
8671 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8672 GOT_OBJ_LABEL_TREE);
8674 case GOT_OBJ_TYPE_COMMIT:
8675 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8676 GOT_OBJ_LABEL_COMMIT);
8678 case GOT_OBJ_TYPE_TAG:
8679 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8686 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8687 got_object_tag_get_name(tag));
8689 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8690 got_object_tag_get_tagger(tag),
8691 got_object_tag_get_tagger_time(tag));
8693 tagmsg = got_object_tag_get_message(tag);
8694 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8695 fprintf(outfile, "%s", tagmsg);
8698 got_object_tag_close(tag);
8702 static const struct got_error *
8703 cmd_cat(int argc, char *argv[])
8705 const struct got_error *error;
8706 struct got_repository *repo = NULL;
8707 struct got_worktree *worktree = NULL;
8708 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8709 const char *commit_id_str = NULL;
8710 struct got_object_id *id = NULL, *commit_id = NULL;
8711 int ch, obj_type, i, force_path = 0;
8714 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8719 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8722 commit_id_str = optarg;
8725 repo_path = realpath(optarg, NULL);
8726 if (repo_path == NULL)
8727 return got_error_from_errno2("realpath",
8729 got_path_strip_trailing_slashes(repo_path);
8743 cwd = getcwd(NULL, 0);
8745 error = got_error_from_errno("getcwd");
8748 error = got_worktree_open(&worktree, cwd);
8749 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8752 if (repo_path == NULL) {
8754 got_worktree_get_repo_path(worktree));
8755 if (repo_path == NULL) {
8756 error = got_error_from_errno("strdup");
8762 if (repo_path == NULL) {
8763 repo_path = getcwd(NULL, 0);
8764 if (repo_path == NULL)
8765 return got_error_from_errno("getcwd");
8768 error = got_repo_open(&repo, repo_path, NULL);
8773 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8777 if (commit_id_str == NULL)
8778 commit_id_str = GOT_REF_HEAD;
8779 error = got_repo_match_object_id(&commit_id, NULL,
8780 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8784 for (i = 0; i < argc; i++) {
8786 error = got_object_id_by_path(&id, repo, commit_id,
8791 error = got_repo_match_object_id(&id, &label, argv[i],
8792 GOT_OBJ_TYPE_ANY, 0, repo);
8794 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8795 error->code != GOT_ERR_NOT_REF)
8797 error = got_object_id_by_path(&id, repo,
8798 commit_id, argv[i]);
8804 error = got_object_get_type(&obj_type, repo, id);
8809 case GOT_OBJ_TYPE_BLOB:
8810 error = cat_blob(id, repo, stdout);
8812 case GOT_OBJ_TYPE_TREE:
8813 error = cat_tree(id, repo, stdout);
8815 case GOT_OBJ_TYPE_COMMIT:
8816 error = cat_commit(id, repo, stdout);
8818 case GOT_OBJ_TYPE_TAG:
8819 error = cat_tag(id, repo, stdout);
8822 error = got_error(GOT_ERR_OBJ_TYPE);
8837 got_worktree_close(worktree);
8839 const struct got_error *repo_error;
8840 repo_error = got_repo_close(repo);