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 "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_head_ref(struct got_reference *target_ref, int verbosity,
889 struct got_repository *repo)
891 const struct got_error *err;
892 struct got_reference *head_symref;
894 err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, 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);
962 static const struct got_error *
963 cmd_clone(int argc, char *argv[])
965 const struct got_error *error = NULL;
966 const char *uri, *dirname;
967 char *proto, *host, *port, *repo_name, *server_path;
968 char *default_destdir = NULL, *id_str = NULL;
969 const char *repo_path;
970 struct got_repository *repo = NULL;
971 struct got_pathlist_head refs, symrefs, wanted_branches;
972 struct got_pathlist_entry *pe;
973 struct got_object_id *pack_hash = NULL;
974 int ch, fetchfd = -1, fetchstatus;
976 struct got_fetch_progress_arg fpa;
977 char *git_url = NULL;
978 char *gitconfig_path = NULL;
979 char *gitconfig = NULL;
980 FILE *gitconfig_file = NULL;
982 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
983 int list_refs_only = 0;
984 struct got_reference *head_symref = NULL;
987 TAILQ_INIT(&symrefs);
988 TAILQ_INIT(&wanted_branches);
990 while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
993 fetch_all_branches = 1;
996 error = got_pathlist_append(&wanted_branches,
1005 mirror_references = 1;
1010 else if (verbosity < 3)
1024 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1025 errx(1, "-a and -b options are mutually exclusive");
1026 if (list_refs_only) {
1027 if (!TAILQ_EMPTY(&wanted_branches))
1028 errx(1, "-l and -b options are mutually exclusive");
1029 if (fetch_all_branches)
1030 errx(1, "-l and -a options are mutually exclusive");
1031 if (mirror_references)
1032 errx(1, "-l and -m options are mutually exclusive");
1033 if (verbosity == -1)
1034 errx(1, "-l and -q options are mutually exclusive");
1046 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1047 &repo_name, argv[0]);
1051 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1052 host, port ? ":" : "", port ? port : "",
1053 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1054 error = got_error_from_errno("asprintf");
1058 if (strcmp(proto, "git") == 0) {
1060 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1061 "sendfd dns inet unveil", NULL) == -1)
1064 } else if (strcmp(proto, "git+ssh") == 0 ||
1065 strcmp(proto, "ssh") == 0) {
1067 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1068 "sendfd unveil", NULL) == -1)
1071 } else if (strcmp(proto, "http") == 0 ||
1072 strcmp(proto, "git+http") == 0) {
1073 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1076 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1079 if (dirname == NULL) {
1080 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1081 error = got_error_from_errno("asprintf");
1084 repo_path = default_destdir;
1086 repo_path = dirname;
1088 if (!list_refs_only) {
1089 error = got_path_mkdir(repo_path);
1093 error = got_repo_init(repo_path);
1096 error = got_repo_open(&repo, repo_path, NULL);
1101 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1102 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1103 error = got_error_from_errno2("unveil",
1104 GOT_FETCH_PATH_SSH);
1108 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1112 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1113 server_path, verbosity);
1118 printf("Connected to %s%s%s\n", host,
1119 port ? ":" : "", port ? port : "");
1121 fpa.last_scaled_size[0] = '\0';
1122 fpa.last_p_indexed = -1;
1123 fpa.last_p_resolved = -1;
1124 fpa.verbosity = verbosity;
1125 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1126 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1127 fetch_all_branches, &wanted_branches, list_refs_only,
1128 verbosity, fetchfd, repo, fetch_progress, &fpa);
1132 if (list_refs_only) {
1133 error = list_remote_refs(&symrefs, &refs);
1137 error = got_object_id_str(&id_str, pack_hash);
1141 printf("\nFetched %s.pack\n", id_str);
1144 /* Set up references provided with the pack file. */
1145 TAILQ_FOREACH(pe, &refs, entry) {
1146 const char *refname = pe->path;
1147 struct got_object_id *id = pe->data;
1148 char *remote_refname;
1150 error = create_ref(refname, id, verbosity - 1, repo);
1154 if (mirror_references)
1157 if (strncmp("refs/heads/", refname, 11) != 0)
1160 if (asprintf(&remote_refname,
1161 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1162 refname + 11) == -1) {
1163 error = got_error_from_errno("asprintf");
1166 error = create_ref(remote_refname, id, verbosity - 1, repo);
1171 /* Set the HEAD reference if the server provided one. */
1172 TAILQ_FOREACH(pe, &symrefs, entry) {
1173 struct got_reference *target_ref;
1174 const char *refname = pe->path;
1175 const char *target = pe->data;
1177 if (strcmp(refname, GOT_REF_HEAD) != 0)
1180 error = got_ref_open(&target_ref, repo, target, 0);
1182 if (error->code == GOT_ERR_NOT_REF) {
1189 error = create_head_ref(target_ref, verbosity, repo);
1190 got_ref_close(target_ref);
1196 * We failed to set the HEAD reference. If we asked for
1197 * a set of wanted branches use the first of one of those
1198 * which could be fetched instead.
1200 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1201 const char *target = pe->path;
1202 struct got_reference *target_ref;
1204 error = got_ref_open(&target_ref, repo, target, 0);
1206 if (error->code == GOT_ERR_NOT_REF) {
1213 error = create_head_ref(target_ref, verbosity, repo);
1214 got_ref_close(target_ref);
1221 /* Create a config file git-fetch(1) can understand. */
1222 gitconfig_path = got_repo_get_path_gitconfig(repo);
1223 if (gitconfig_path == NULL) {
1224 error = got_error_from_errno("got_repo_get_path_gitconfig");
1227 gitconfig_file = fopen(gitconfig_path, "a");
1228 if (gitconfig_file == NULL) {
1229 error = got_error_from_errno2("fopen", gitconfig_path);
1232 if (mirror_references) {
1233 if (asprintf(&gitconfig,
1236 "\tfetch = +refs/*:refs/*\n"
1237 "\tmirror = true\n",
1238 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1239 error = got_error_from_errno("asprintf");
1242 } else if (fetch_all_branches) {
1243 if (asprintf(&gitconfig,
1246 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1247 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1248 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1249 error = got_error_from_errno("asprintf");
1253 const char *branchname;
1256 * If the server specified a default branch, use just that one.
1257 * Otherwise fall back to fetching all branches on next fetch.
1260 branchname = got_ref_get_symref_target(head_symref);
1261 if (strncmp(branchname, "refs/heads/", 11) == 0)
1264 branchname = "*"; /* fall back to all branches */
1265 if (asprintf(&gitconfig,
1268 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1269 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1270 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1271 branchname) == -1) {
1272 error = got_error_from_errno("asprintf");
1276 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1277 if (n != strlen(gitconfig)) {
1278 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1283 printf("Created %s repository '%s'\n",
1284 mirror_references ? "mirrored" : "cloned", repo_path);
1287 if (kill(fetchpid, SIGTERM) == -1)
1288 error = got_error_from_errno("kill");
1289 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1290 error = got_error_from_errno("waitpid");
1292 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1293 error = got_error_from_errno("close");
1294 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1295 error = got_error_from_errno("fclose");
1297 got_repo_close(repo);
1299 got_ref_close(head_symref);
1300 TAILQ_FOREACH(pe, &refs, entry) {
1301 free((void *)pe->path);
1304 got_pathlist_free(&refs);
1305 TAILQ_FOREACH(pe, &symrefs, entry) {
1306 free((void *)pe->path);
1309 got_pathlist_free(&symrefs);
1310 got_pathlist_free(&wanted_branches);
1317 free(default_destdir);
1318 free(gitconfig_path);
1323 static const struct got_error *
1324 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1325 int replace_tags, int verbosity, struct got_repository *repo)
1327 const struct got_error *err = NULL;
1328 char *new_id_str = NULL;
1329 struct got_object_id *old_id = NULL;
1331 err = got_object_id_str(&new_id_str, new_id);
1335 if (!replace_tags &&
1336 strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1337 if (verbosity >= 0) {
1338 printf("Rejecting update of existing tag %s: %s\n",
1339 got_ref_get_name(ref), new_id_str);
1344 if (got_ref_is_symbolic(ref)) {
1345 struct got_reference *new_ref;
1346 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1349 err = got_ref_delete(ref, repo);
1352 if (verbosity >= 0) {
1353 printf("Deleted reference %s: %s\n",
1354 got_ref_get_name(ref),
1355 got_ref_get_symref_target(ref));
1357 err = got_ref_write(new_ref, repo);
1361 err = got_ref_resolve(&old_id, repo, ref);
1364 if (got_object_id_cmp(old_id, new_id) == 0)
1367 err = got_ref_change_ref(ref, new_id);
1370 err = got_ref_write(ref, repo);
1376 printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1387 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1388 "[-r repository-path] [-t] [-q] [-v] [remote-repository-name]\n",
1393 static const struct got_error *
1394 delete_missing_refs(struct got_pathlist_head *their_refs,
1395 int verbosity, struct got_repository *repo)
1397 const struct got_error *err = NULL;
1398 struct got_reflist_head my_refs;
1399 struct got_reflist_entry *re;
1400 struct got_pathlist_entry *pe;
1401 struct got_object_id *id;
1404 SIMPLEQ_INIT(&my_refs);
1406 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1410 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1411 const char *refname = got_ref_get_name(re->ref);
1413 if (strncmp(refname, "refs/heads/", 11) != 0 &&
1414 strncmp(refname, "refs/tags/", 10) != 0)
1417 TAILQ_FOREACH(pe, their_refs, entry) {
1418 if (strcmp(refname, pe->path) == 0)
1424 err = got_ref_resolve(&id, repo, re->ref);
1427 err = got_object_id_str(&id_str, id);
1433 err = got_ref_delete(re->ref, repo);
1436 if (verbosity >= 0) {
1437 printf("Deleted reference %s: %s\n",
1438 got_ref_get_name(re->ref), id_str);
1445 static const struct got_error *
1446 cmd_fetch(int argc, char *argv[])
1448 const struct got_error *error = NULL;
1449 char *cwd = NULL, *repo_path = NULL;
1450 const char *remote_name;
1451 char *proto = NULL, *host = NULL, *port = NULL;
1452 char *repo_name = NULL, *server_path = NULL;
1453 struct got_remote_repo *remotes, *remote = NULL;
1455 char *id_str = NULL;
1456 struct got_repository *repo = NULL;
1457 struct got_worktree *worktree = NULL;
1458 struct got_pathlist_head refs, symrefs, wanted_branches;
1459 struct got_pathlist_entry *pe;
1460 struct got_object_id *pack_hash = NULL;
1461 int i, ch, fetchfd = -1, fetchstatus;
1462 pid_t fetchpid = -1;
1463 struct got_fetch_progress_arg fpa;
1464 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1465 int delete_refs = 0, replace_tags = 0;
1468 TAILQ_INIT(&symrefs);
1469 TAILQ_INIT(&wanted_branches);
1471 while ((ch = getopt(argc, argv, "ab:dlr:tvq")) != -1) {
1474 fetch_all_branches = 1;
1477 error = got_pathlist_append(&wanted_branches,
1489 repo_path = realpath(optarg, NULL);
1490 if (repo_path == NULL)
1491 return got_error_from_errno2("realpath",
1493 got_path_strip_trailing_slashes(repo_path);
1501 else if (verbosity < 3)
1515 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1516 errx(1, "-a and -b options are mutually exclusive");
1517 if (list_refs_only) {
1518 if (!TAILQ_EMPTY(&wanted_branches))
1519 errx(1, "-l and -b options are mutually exclusive");
1520 if (fetch_all_branches)
1521 errx(1, "-l and -a options are mutually exclusive");
1523 errx(1, "-l and -d options are mutually exclusive");
1524 if (verbosity == -1)
1525 errx(1, "-l and -q options are mutually exclusive");
1529 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1531 remote_name = argv[0];
1535 cwd = getcwd(NULL, 0);
1537 error = got_error_from_errno("getcwd");
1541 if (repo_path == NULL) {
1542 error = got_worktree_open(&worktree, cwd);
1543 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1549 strdup(got_worktree_get_repo_path(worktree));
1550 if (repo_path == NULL)
1551 error = got_error_from_errno("strdup");
1555 repo_path = strdup(cwd);
1556 if (repo_path == NULL) {
1557 error = got_error_from_errno("strdup");
1563 error = got_repo_open(&repo, repo_path, NULL);
1567 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1568 for (i = 0; i < nremotes; i++) {
1569 remote = &remotes[i];
1570 if (strcmp(remote->name, remote_name) == 0)
1573 if (i == nremotes) {
1574 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1578 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1579 &repo_name, remote->url);
1583 if (strcmp(proto, "git") == 0) {
1585 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1586 "sendfd dns inet unveil", NULL) == -1)
1589 } else if (strcmp(proto, "git+ssh") == 0 ||
1590 strcmp(proto, "ssh") == 0) {
1592 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1593 "sendfd unveil", NULL) == -1)
1596 } else if (strcmp(proto, "http") == 0 ||
1597 strcmp(proto, "git+http") == 0) {
1598 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1601 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1605 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1606 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1607 error = got_error_from_errno2("unveil",
1608 GOT_FETCH_PATH_SSH);
1612 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1616 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1617 server_path, verbosity);
1622 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1623 port ? ":" : "", port ? port : "");
1625 fpa.last_scaled_size[0] = '\0';
1626 fpa.last_p_indexed = -1;
1627 fpa.last_p_resolved = -1;
1628 fpa.verbosity = verbosity;
1629 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1630 remote->mirror_references, fetch_all_branches, &wanted_branches,
1631 list_refs_only, verbosity, fetchfd, repo, fetch_progress, &fpa);
1635 if (list_refs_only) {
1636 error = list_remote_refs(&symrefs, &refs);
1640 if (pack_hash == NULL) {
1642 printf("Already up-to-date\n");
1644 error = delete_missing_refs(&refs, verbosity, repo);
1648 if (verbosity >= 0) {
1649 error = got_object_id_str(&id_str, pack_hash);
1652 printf("\nFetched %s.pack\n", id_str);
1657 /* Update references provided with the pack file. */
1658 TAILQ_FOREACH(pe, &refs, entry) {
1659 const char *refname = pe->path;
1660 struct got_object_id *id = pe->data;
1661 struct got_reference *ref;
1662 char *remote_refname;
1664 if (remote->mirror_references ||
1665 strncmp("refs/tags/", refname, 10) == 0) {
1666 error = got_ref_open(&ref, repo, refname, 0);
1668 if (error->code != GOT_ERR_NOT_REF)
1670 error = create_ref(refname, id, verbosity,
1675 error = update_ref(ref, id, replace_tags,
1681 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1682 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1683 remote_name, refname + 11) == -1) {
1684 error = got_error_from_errno("asprintf");
1688 error = got_ref_open(&ref, repo, remote_refname, 0);
1690 if (error->code != GOT_ERR_NOT_REF)
1692 error = create_ref(remote_refname, id,
1697 error = update_ref(ref, id, replace_tags,
1704 /* Also create a local branch if none exists yet. */
1705 error = got_ref_open(&ref, repo, refname, 0);
1707 if (error->code != GOT_ERR_NOT_REF)
1709 error = create_ref(refname, id, verbosity,
1718 error = delete_missing_refs(&refs, verbosity, repo);
1721 if (kill(fetchpid, SIGTERM) == -1)
1722 error = got_error_from_errno("kill");
1723 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1724 error = got_error_from_errno("waitpid");
1726 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1727 error = got_error_from_errno("close");
1729 got_repo_close(repo);
1731 got_worktree_close(worktree);
1732 TAILQ_FOREACH(pe, &refs, entry) {
1733 free((void *)pe->path);
1736 got_pathlist_free(&refs);
1737 TAILQ_FOREACH(pe, &symrefs, entry) {
1738 free((void *)pe->path);
1741 got_pathlist_free(&symrefs);
1742 got_pathlist_free(&wanted_branches);
1757 usage_checkout(void)
1759 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1760 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1765 show_worktree_base_ref_warning(void)
1767 fprintf(stderr, "%s: warning: could not create a reference "
1768 "to the work tree's base commit; the commit could be "
1769 "garbage-collected by Git; making the repository "
1770 "writable and running 'got update' will prevent this\n",
1774 struct got_checkout_progress_arg {
1775 const char *worktree_path;
1776 int had_base_commit_ref_error;
1779 static const struct got_error *
1780 checkout_progress(void *arg, unsigned char status, const char *path)
1782 struct got_checkout_progress_arg *a = arg;
1784 /* Base commit bump happens silently. */
1785 if (status == GOT_STATUS_BUMP_BASE)
1788 if (status == GOT_STATUS_BASE_REF_ERR) {
1789 a->had_base_commit_ref_error = 1;
1793 while (path[0] == '/')
1796 printf("%c %s/%s\n", status, a->worktree_path, path);
1800 static const struct got_error *
1801 check_cancelled(void *arg)
1803 if (sigint_received || sigpipe_received)
1804 return got_error(GOT_ERR_CANCELLED);
1808 static const struct got_error *
1809 check_linear_ancestry(struct got_object_id *commit_id,
1810 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1811 struct got_repository *repo)
1813 const struct got_error *err = NULL;
1814 struct got_object_id *yca_id;
1816 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1817 commit_id, base_commit_id, repo, check_cancelled, NULL);
1822 return got_error(GOT_ERR_ANCESTRY);
1825 * Require a straight line of history between the target commit
1826 * and the work tree's base commit.
1828 * Non-linear situations such as this require a rebase:
1830 * (commit) D F (base_commit)
1838 * 'got update' only handles linear cases:
1839 * Update forwards in time: A (base/yca) - B - C - D (commit)
1840 * Update backwards in time: D (base) - C - B - A (commit/yca)
1842 if (allow_forwards_in_time_only) {
1843 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1844 return got_error(GOT_ERR_ANCESTRY);
1845 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1846 got_object_id_cmp(base_commit_id, yca_id) != 0)
1847 return got_error(GOT_ERR_ANCESTRY);
1853 static const struct got_error *
1854 check_same_branch(struct got_object_id *commit_id,
1855 struct got_reference *head_ref, struct got_object_id *yca_id,
1856 struct got_repository *repo)
1858 const struct got_error *err = NULL;
1859 struct got_commit_graph *graph = NULL;
1860 struct got_object_id *head_commit_id = NULL;
1861 int is_same_branch = 0;
1863 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1867 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1871 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1876 err = got_commit_graph_open(&graph, "/", 1);
1880 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1881 check_cancelled, NULL);
1886 struct got_object_id *id;
1887 err = got_commit_graph_iter_next(&id, graph, repo,
1888 check_cancelled, NULL);
1890 if (err->code == GOT_ERR_ITER_COMPLETED)
1896 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1898 if (got_object_id_cmp(id, commit_id) == 0) {
1906 got_commit_graph_close(graph);
1907 free(head_commit_id);
1908 if (!err && !is_same_branch)
1909 err = got_error(GOT_ERR_ANCESTRY);
1913 static const struct got_error *
1914 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1916 static char msg[512];
1917 const char *branch_name;
1919 if (got_ref_is_symbolic(ref))
1920 branch_name = got_ref_get_symref_target(ref);
1922 branch_name = got_ref_get_name(ref);
1924 if (strncmp("refs/heads/", branch_name, 11) == 0)
1927 snprintf(msg, sizeof(msg),
1928 "target commit is not contained in branch '%s'; "
1929 "the branch to use must be specified with -b; "
1930 "if necessary a new branch can be created for "
1931 "this commit with 'got branch -c %s BRANCH_NAME'",
1932 branch_name, commit_id_str);
1934 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1937 static const struct got_error *
1938 cmd_checkout(int argc, char *argv[])
1940 const struct got_error *error = NULL;
1941 struct got_repository *repo = NULL;
1942 struct got_reference *head_ref = NULL;
1943 struct got_worktree *worktree = NULL;
1944 char *repo_path = NULL;
1945 char *worktree_path = NULL;
1946 const char *path_prefix = "";
1947 const char *branch_name = GOT_REF_HEAD;
1948 char *commit_id_str = NULL;
1949 int ch, same_path_prefix, allow_nonempty = 0;
1950 struct got_pathlist_head paths;
1951 struct got_checkout_progress_arg cpa;
1955 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1958 branch_name = optarg;
1961 commit_id_str = strdup(optarg);
1962 if (commit_id_str == NULL)
1963 return got_error_from_errno("strdup");
1969 path_prefix = optarg;
1981 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1982 "unveil", NULL) == -1)
1986 char *cwd, *base, *dotgit;
1987 repo_path = realpath(argv[0], NULL);
1988 if (repo_path == NULL)
1989 return got_error_from_errno2("realpath", argv[0]);
1990 cwd = getcwd(NULL, 0);
1992 error = got_error_from_errno("getcwd");
1995 if (path_prefix[0]) {
1996 base = basename(path_prefix);
1998 error = got_error_from_errno2("basename",
2003 base = basename(repo_path);
2005 error = got_error_from_errno2("basename",
2010 dotgit = strstr(base, ".git");
2013 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2014 error = got_error_from_errno("asprintf");
2019 } else if (argc == 2) {
2020 repo_path = realpath(argv[0], NULL);
2021 if (repo_path == NULL) {
2022 error = got_error_from_errno2("realpath", argv[0]);
2025 worktree_path = realpath(argv[1], NULL);
2026 if (worktree_path == NULL) {
2027 if (errno != ENOENT) {
2028 error = got_error_from_errno2("realpath",
2032 worktree_path = strdup(argv[1]);
2033 if (worktree_path == NULL) {
2034 error = got_error_from_errno("strdup");
2041 got_path_strip_trailing_slashes(repo_path);
2042 got_path_strip_trailing_slashes(worktree_path);
2044 error = got_repo_open(&repo, repo_path, NULL);
2048 /* Pre-create work tree path for unveil(2) */
2049 error = got_path_mkdir(worktree_path);
2051 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2052 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2054 if (!allow_nonempty &&
2055 !got_path_dir_is_empty(worktree_path)) {
2056 error = got_error_path(worktree_path,
2057 GOT_ERR_DIR_NOT_EMPTY);
2062 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2066 error = got_ref_open(&head_ref, repo, branch_name, 0);
2070 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2071 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2074 error = got_worktree_open(&worktree, worktree_path);
2078 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2082 if (!same_path_prefix) {
2083 error = got_error(GOT_ERR_PATH_PREFIX);
2087 if (commit_id_str) {
2088 struct got_object_id *commit_id;
2089 error = got_repo_match_object_id(&commit_id, NULL,
2090 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2093 error = check_linear_ancestry(commit_id,
2094 got_worktree_get_base_commit_id(worktree), 0, repo);
2095 if (error != NULL) {
2097 if (error->code == GOT_ERR_ANCESTRY) {
2098 error = checkout_ancestry_error(
2099 head_ref, commit_id_str);
2103 error = check_same_branch(commit_id, head_ref, NULL, repo);
2105 if (error->code == GOT_ERR_ANCESTRY) {
2106 error = checkout_ancestry_error(
2107 head_ref, commit_id_str);
2111 error = got_worktree_set_base_commit_id(worktree, repo,
2118 error = got_pathlist_append(&paths, "", NULL);
2121 cpa.worktree_path = worktree_path;
2122 cpa.had_base_commit_ref_error = 0;
2123 error = got_worktree_checkout_files(worktree, &paths, repo,
2124 checkout_progress, &cpa, check_cancelled, NULL);
2128 printf("Now shut up and hack\n");
2129 if (cpa.had_base_commit_ref_error)
2130 show_worktree_base_ref_warning();
2132 got_pathlist_free(&paths);
2133 free(commit_id_str);
2135 free(worktree_path);
2142 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2147 static const struct got_error *
2148 update_progress(void *arg, unsigned char status, const char *path)
2150 int *did_something = arg;
2152 if (status == GOT_STATUS_EXISTS ||
2153 status == GOT_STATUS_BASE_REF_ERR)
2158 /* Base commit bump happens silently. */
2159 if (status == GOT_STATUS_BUMP_BASE)
2162 while (path[0] == '/')
2164 printf("%c %s\n", status, path);
2168 static const struct got_error *
2169 switch_head_ref(struct got_reference *head_ref,
2170 struct got_object_id *commit_id, struct got_worktree *worktree,
2171 struct got_repository *repo)
2173 const struct got_error *err = NULL;
2175 int ref_has_moved = 0;
2177 /* Trivial case: switching between two different references. */
2178 if (strcmp(got_ref_get_name(head_ref),
2179 got_worktree_get_head_ref_name(worktree)) != 0) {
2180 printf("Switching work tree from %s to %s\n",
2181 got_worktree_get_head_ref_name(worktree),
2182 got_ref_get_name(head_ref));
2183 return got_worktree_set_head_ref(worktree, head_ref);
2186 err = check_linear_ancestry(commit_id,
2187 got_worktree_get_base_commit_id(worktree), 0, repo);
2189 if (err->code != GOT_ERR_ANCESTRY)
2196 /* Switching to a rebased branch with the same reference name. */
2197 err = got_object_id_str(&base_id_str,
2198 got_worktree_get_base_commit_id(worktree));
2201 printf("Reference %s now points at a different branch\n",
2202 got_worktree_get_head_ref_name(worktree));
2203 printf("Switching work tree from %s to %s\n", base_id_str,
2204 got_worktree_get_head_ref_name(worktree));
2208 static const struct got_error *
2209 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2211 const struct got_error *err;
2214 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2218 return got_error(GOT_ERR_REBASING);
2220 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2224 return got_error(GOT_ERR_HISTEDIT_BUSY);
2229 static const struct got_error *
2230 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2231 char *argv[], struct got_worktree *worktree)
2233 const struct got_error *err = NULL;
2240 return got_error_from_errno("strdup");
2241 return got_pathlist_append(paths, path, NULL);
2244 for (i = 0; i < argc; i++) {
2245 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2248 err = got_pathlist_append(paths, path, NULL);
2258 static const struct got_error *
2259 cmd_update(int argc, char *argv[])
2261 const struct got_error *error = NULL;
2262 struct got_repository *repo = NULL;
2263 struct got_worktree *worktree = NULL;
2264 char *worktree_path = NULL;
2265 struct got_object_id *commit_id = NULL;
2266 char *commit_id_str = NULL;
2267 const char *branch_name = NULL;
2268 struct got_reference *head_ref = NULL;
2269 struct got_pathlist_head paths;
2270 struct got_pathlist_entry *pe;
2271 int ch, did_something = 0;
2275 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2278 branch_name = optarg;
2281 commit_id_str = strdup(optarg);
2282 if (commit_id_str == NULL)
2283 return got_error_from_errno("strdup");
2295 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2296 "unveil", NULL) == -1)
2299 worktree_path = getcwd(NULL, 0);
2300 if (worktree_path == NULL) {
2301 error = got_error_from_errno("getcwd");
2304 error = got_worktree_open(&worktree, worktree_path);
2308 error = check_rebase_or_histedit_in_progress(worktree);
2312 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2317 error = apply_unveil(got_repo_get_path(repo), 0,
2318 got_worktree_get_root_path(worktree));
2322 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2326 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2327 got_worktree_get_head_ref_name(worktree), 0);
2330 if (commit_id_str == NULL) {
2331 error = got_ref_resolve(&commit_id, repo, head_ref);
2334 error = got_object_id_str(&commit_id_str, commit_id);
2338 error = got_repo_match_object_id(&commit_id, NULL,
2339 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2340 free(commit_id_str);
2341 commit_id_str = NULL;
2344 error = got_object_id_str(&commit_id_str, commit_id);
2350 struct got_object_id *head_commit_id;
2351 TAILQ_FOREACH(pe, &paths, entry) {
2352 if (pe->path_len == 0)
2354 error = got_error_msg(GOT_ERR_BAD_PATH,
2355 "switching between branches requires that "
2356 "the entire work tree gets updated");
2359 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2362 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2364 free(head_commit_id);
2367 error = check_same_branch(commit_id, head_ref, NULL, repo);
2370 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2374 error = check_linear_ancestry(commit_id,
2375 got_worktree_get_base_commit_id(worktree), 0, repo);
2376 if (error != NULL) {
2377 if (error->code == GOT_ERR_ANCESTRY)
2378 error = got_error(GOT_ERR_BRANCH_MOVED);
2381 error = check_same_branch(commit_id, head_ref, NULL, repo);
2386 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2388 error = got_worktree_set_base_commit_id(worktree, repo,
2394 error = got_worktree_checkout_files(worktree, &paths, repo,
2395 update_progress, &did_something, check_cancelled, NULL);
2400 printf("Updated to commit %s\n", commit_id_str);
2402 printf("Already up-to-date\n");
2404 free(worktree_path);
2405 TAILQ_FOREACH(pe, &paths, entry)
2406 free((char *)pe->path);
2407 got_pathlist_free(&paths);
2409 free(commit_id_str);
2413 static const struct got_error *
2414 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2415 const char *path, int diff_context, int ignore_whitespace,
2416 struct got_repository *repo)
2418 const struct got_error *err = NULL;
2419 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2422 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2427 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2431 while (path[0] == '/')
2433 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2434 ignore_whitespace, stdout);
2437 got_object_blob_close(blob1);
2438 got_object_blob_close(blob2);
2442 static const struct got_error *
2443 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2444 const char *path, int diff_context, int ignore_whitespace,
2445 struct got_repository *repo)
2447 const struct got_error *err = NULL;
2448 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2449 struct got_diff_blob_output_unidiff_arg arg;
2452 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2457 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2461 arg.diff_context = diff_context;
2462 arg.ignore_whitespace = ignore_whitespace;
2463 arg.outfile = stdout;
2464 while (path[0] == '/')
2466 err = got_diff_tree(tree1, tree2, path, path, repo,
2467 got_diff_blob_output_unidiff, &arg, 1);
2470 got_object_tree_close(tree1);
2472 got_object_tree_close(tree2);
2476 static const struct got_error *
2477 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2478 const char *path, int diff_context, struct got_repository *repo)
2480 const struct got_error *err = NULL;
2481 struct got_commit_object *pcommit = NULL;
2482 char *id_str1 = NULL, *id_str2 = NULL;
2483 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2484 struct got_object_qid *qid;
2486 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2488 err = got_object_open_as_commit(&pcommit, repo,
2494 if (path && path[0] != '\0') {
2496 err = got_object_id_by_path(&obj_id2, repo, id, path);
2499 err = got_object_id_str(&id_str2, obj_id2);
2505 err = got_object_id_by_path(&obj_id1, repo,
2511 err = got_object_id_str(&id_str1, obj_id1);
2517 err = got_object_get_type(&obj_type, repo, obj_id2);
2522 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2524 case GOT_OBJ_TYPE_BLOB:
2525 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2528 case GOT_OBJ_TYPE_TREE:
2529 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2533 err = got_error(GOT_ERR_OBJ_TYPE);
2539 obj_id2 = got_object_commit_get_tree_id(commit);
2540 err = got_object_id_str(&id_str2, obj_id2);
2543 obj_id1 = got_object_commit_get_tree_id(pcommit);
2544 err = got_object_id_str(&id_str1, obj_id1);
2547 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2548 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2554 got_object_commit_close(pcommit);
2559 get_datestr(time_t *time, char *datebuf)
2561 struct tm mytm, *tm;
2564 tm = gmtime_r(time, &mytm);
2567 s = asctime_r(tm, datebuf);
2570 p = strchr(s, '\n');
2576 static const struct got_error *
2577 match_logmsg(int *have_match, struct got_object_id *id,
2578 struct got_commit_object *commit, regex_t *regex)
2580 const struct got_error *err = NULL;
2581 regmatch_t regmatch;
2582 char *id_str = NULL, *logmsg = NULL;
2586 err = got_object_id_str(&id_str, id);
2590 err = got_object_commit_get_logmsg(&logmsg, commit);
2594 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2602 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2604 static const struct got_error *
2605 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2606 struct got_repository *repo, const char *path, int show_patch,
2607 int diff_context, struct got_reflist_head *refs)
2609 const struct got_error *err = NULL;
2610 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2612 time_t committer_time;
2613 const char *author, *committer;
2614 char *refs_str = NULL;
2615 struct got_reflist_entry *re;
2617 SIMPLEQ_FOREACH(re, refs, entry) {
2620 struct got_tag_object *tag = NULL;
2623 name = got_ref_get_name(re->ref);
2624 if (strcmp(name, GOT_REF_HEAD) == 0)
2626 if (strncmp(name, "refs/", 5) == 0)
2628 if (strncmp(name, "got/", 4) == 0)
2630 if (strncmp(name, "heads/", 6) == 0)
2632 if (strncmp(name, "remotes/", 8) == 0)
2634 if (strncmp(name, "tags/", 5) == 0) {
2635 err = got_object_open_as_tag(&tag, repo, re->id);
2637 if (err->code != GOT_ERR_OBJ_TYPE)
2639 /* Ref points at something other than a tag. */
2644 cmp = got_object_id_cmp(tag ?
2645 got_object_tag_get_object_id(tag) : re->id, id);
2647 got_object_tag_close(tag);
2651 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2653 err = got_error_from_errno("asprintf");
2659 err = got_object_id_str(&id_str, id);
2663 printf(GOT_COMMIT_SEP_STR);
2664 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2665 refs_str ? refs_str : "", refs_str ? ")" : "");
2670 printf("from: %s\n", got_object_commit_get_author(commit));
2671 committer_time = got_object_commit_get_committer_time(commit);
2672 datestr = get_datestr(&committer_time, datebuf);
2674 printf("date: %s UTC\n", datestr);
2675 author = got_object_commit_get_author(commit);
2676 committer = got_object_commit_get_committer(commit);
2677 if (strcmp(author, committer) != 0)
2678 printf("via: %s\n", committer);
2679 if (got_object_commit_get_nparents(commit) > 1) {
2680 const struct got_object_id_queue *parent_ids;
2681 struct got_object_qid *qid;
2683 parent_ids = got_object_commit_get_parent_ids(commit);
2684 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2685 err = got_object_id_str(&id_str, qid->id);
2688 printf("parent %d: %s\n", n++, id_str);
2693 err = got_object_commit_get_logmsg(&logmsg0, commit);
2699 line = strsep(&logmsg, "\n");
2701 printf(" %s\n", line);
2706 err = print_patch(commit, id, path, diff_context, repo);
2711 if (fflush(stdout) != 0 && err == NULL)
2712 err = got_error_from_errno("fflush");
2716 static const struct got_error *
2717 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2718 const char *path, int show_patch, const char *search_pattern,
2719 int diff_context, int limit, int log_branches,
2720 struct got_reflist_head *refs)
2722 const struct got_error *err;
2723 struct got_commit_graph *graph;
2727 if (search_pattern &&
2728 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2729 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2731 err = got_commit_graph_open(&graph, path, !log_branches);
2734 err = got_commit_graph_iter_start(graph, root_id, repo,
2735 check_cancelled, NULL);
2739 struct got_commit_object *commit;
2740 struct got_object_id *id;
2742 if (sigint_received || sigpipe_received)
2745 err = got_commit_graph_iter_next(&id, graph, repo,
2746 check_cancelled, NULL);
2748 if (err->code == GOT_ERR_ITER_COMPLETED)
2755 err = got_object_open_as_commit(&commit, repo, id);
2759 if (search_pattern) {
2760 err = match_logmsg(&have_match, id, commit, ®ex);
2762 got_object_commit_close(commit);
2765 if (have_match == 0) {
2766 got_object_commit_close(commit);
2771 err = print_commit(commit, id, repo, path, show_patch,
2772 diff_context, refs);
2773 got_object_commit_close(commit);
2774 if (err || (limit && --limit == 0))
2780 got_commit_graph_close(graph);
2787 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2788 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2793 get_default_log_limit(void)
2795 const char *got_default_log_limit;
2799 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2800 if (got_default_log_limit == NULL)
2802 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2808 static const struct got_error *
2809 cmd_log(int argc, char *argv[])
2811 const struct got_error *error;
2812 struct got_repository *repo = NULL;
2813 struct got_worktree *worktree = NULL;
2814 struct got_commit_object *commit = NULL;
2815 struct got_object_id *id = NULL;
2816 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2817 const char *start_commit = NULL, *search_pattern = NULL;
2818 int diff_context = -1, ch;
2819 int show_patch = 0, limit = 0, log_branches = 0;
2821 struct got_reflist_head refs;
2823 SIMPLEQ_INIT(&refs);
2826 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2832 limit = get_default_log_limit();
2834 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2840 start_commit = optarg;
2843 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2846 err(1, "-C option %s", errstr);
2849 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2851 err(1, "-l option %s", errstr);
2857 repo_path = realpath(optarg, NULL);
2858 if (repo_path == NULL)
2859 return got_error_from_errno2("realpath",
2861 got_path_strip_trailing_slashes(repo_path);
2864 search_pattern = optarg;
2875 if (diff_context == -1)
2877 else if (!show_patch)
2878 errx(1, "-C reguires -p");
2880 cwd = getcwd(NULL, 0);
2882 error = got_error_from_errno("getcwd");
2886 error = got_worktree_open(&worktree, cwd);
2887 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2894 error = got_error_from_errno("strdup");
2897 } else if (argc == 1) {
2899 error = got_worktree_resolve_path(&path, worktree,
2904 path = strdup(argv[0]);
2906 error = got_error_from_errno("strdup");
2913 if (repo_path == NULL) {
2914 repo_path = worktree ?
2915 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2917 if (repo_path == NULL) {
2918 error = got_error_from_errno("strdup");
2922 error = got_repo_open(&repo, repo_path, NULL);
2926 error = apply_unveil(got_repo_get_path(repo), 1,
2927 worktree ? got_worktree_get_root_path(worktree) : NULL);
2931 if (start_commit == NULL) {
2932 struct got_reference *head_ref;
2933 error = got_ref_open(&head_ref, repo,
2934 worktree ? got_worktree_get_head_ref_name(worktree)
2938 error = got_ref_resolve(&id, repo, head_ref);
2939 got_ref_close(head_ref);
2942 error = got_object_open_as_commit(&commit, repo, id);
2944 struct got_reference *ref;
2945 error = got_ref_open(&ref, repo, start_commit, 0);
2946 if (error == NULL) {
2948 error = got_ref_resolve(&id, repo, ref);
2952 error = got_object_get_type(&obj_type, repo, id);
2955 if (obj_type == GOT_OBJ_TYPE_TAG) {
2956 struct got_tag_object *tag;
2957 error = got_object_open_as_tag(&tag, repo, id);
2960 if (got_object_tag_get_object_type(tag) !=
2961 GOT_OBJ_TYPE_COMMIT) {
2962 got_object_tag_close(tag);
2963 error = got_error(GOT_ERR_OBJ_TYPE);
2967 id = got_object_id_dup(
2968 got_object_tag_get_object_id(tag));
2970 error = got_error_from_errno(
2971 "got_object_id_dup");
2972 got_object_tag_close(tag);
2975 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2976 error = got_error(GOT_ERR_OBJ_TYPE);
2979 error = got_object_open_as_commit(&commit, repo, id);
2983 if (commit == NULL) {
2984 error = got_repo_match_object_id_prefix(&id,
2985 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2994 const char *prefix = got_worktree_get_path_prefix(worktree);
2996 if (asprintf(&p, "%s%s%s", prefix,
2997 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2998 error = got_error_from_errno("asprintf");
3001 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3004 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3009 path = in_repo_path;
3012 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3016 error = print_commits(id, repo, path, show_patch, search_pattern,
3017 diff_context, limit, log_branches, &refs);
3024 got_worktree_close(worktree);
3026 const struct got_error *repo_error;
3027 repo_error = got_repo_close(repo);
3031 got_ref_list_free(&refs);
3038 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3039 "[-w] [object1 object2 | path]\n", getprogname());
3043 struct print_diff_arg {
3044 struct got_repository *repo;
3045 struct got_worktree *worktree;
3050 int ignore_whitespace;
3053 static const struct got_error *
3054 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3055 const char *path, struct got_object_id *blob_id,
3056 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3057 int dirfd, const char *de_name)
3059 struct print_diff_arg *a = arg;
3060 const struct got_error *err = NULL;
3061 struct got_blob_object *blob1 = NULL;
3064 char *abspath = NULL, *label1 = NULL;
3067 if (a->diff_staged) {
3068 if (staged_status != GOT_STATUS_MODIFY &&
3069 staged_status != GOT_STATUS_ADD &&
3070 staged_status != GOT_STATUS_DELETE)
3073 if (staged_status == GOT_STATUS_DELETE)
3075 if (status == GOT_STATUS_NONEXISTENT)
3076 return got_error_set_errno(ENOENT, path);
3077 if (status != GOT_STATUS_MODIFY &&
3078 status != GOT_STATUS_ADD &&
3079 status != GOT_STATUS_DELETE &&
3080 status != GOT_STATUS_CONFLICT)
3084 if (!a->header_shown) {
3085 printf("diff %s %s%s\n", a->id_str,
3086 got_worktree_get_root_path(a->worktree),
3087 a->diff_staged ? " (staged changes)" : "");
3088 a->header_shown = 1;
3091 if (a->diff_staged) {
3092 const char *label1 = NULL, *label2 = NULL;
3093 switch (staged_status) {
3094 case GOT_STATUS_MODIFY:
3098 case GOT_STATUS_ADD:
3101 case GOT_STATUS_DELETE:
3105 return got_error(GOT_ERR_FILE_STATUS);
3107 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3108 label1, label2, a->diff_context, a->ignore_whitespace,
3112 if (staged_status == GOT_STATUS_ADD ||
3113 staged_status == GOT_STATUS_MODIFY) {
3115 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3119 err = got_object_id_str(&id_str, staged_blob_id);
3122 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3123 err = got_error_from_errno("asprintf");
3128 } else if (status != GOT_STATUS_ADD) {
3129 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3134 if (status != GOT_STATUS_DELETE) {
3135 if (asprintf(&abspath, "%s/%s",
3136 got_worktree_get_root_path(a->worktree), path) == -1) {
3137 err = got_error_from_errno("asprintf");
3142 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3144 err = got_error_from_errno2("openat", abspath);
3148 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3150 err = got_error_from_errno2("open", abspath);
3154 if (fstat(fd, &sb) == -1) {
3155 err = got_error_from_errno2("fstat", abspath);
3158 f2 = fdopen(fd, "r");
3160 err = got_error_from_errno2("fdopen", abspath);
3167 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3168 a->diff_context, a->ignore_whitespace, stdout);
3171 got_object_blob_close(blob1);
3172 if (f2 && fclose(f2) == EOF && err == NULL)
3173 err = got_error_from_errno("fclose");
3174 if (fd != -1 && close(fd) == -1 && err == NULL)
3175 err = got_error_from_errno("close");
3180 static const struct got_error *
3181 cmd_diff(int argc, char *argv[])
3183 const struct got_error *error;
3184 struct got_repository *repo = NULL;
3185 struct got_worktree *worktree = NULL;
3186 char *cwd = NULL, *repo_path = NULL;
3187 struct got_object_id *id1 = NULL, *id2 = NULL;
3188 const char *id_str1 = NULL, *id_str2 = NULL;
3189 char *label1 = NULL, *label2 = NULL;
3191 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3196 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3201 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3204 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3207 err(1, "-C option %s", errstr);
3210 repo_path = realpath(optarg, NULL);
3211 if (repo_path == NULL)
3212 return got_error_from_errno2("realpath",
3214 got_path_strip_trailing_slashes(repo_path);
3220 ignore_whitespace = 1;
3231 cwd = getcwd(NULL, 0);
3233 error = got_error_from_errno("getcwd");
3239 "-r option can't be used when diffing a work tree");
3240 error = got_worktree_open(&worktree, cwd);
3243 repo_path = strdup(got_worktree_get_repo_path(worktree));
3244 if (repo_path == NULL) {
3245 error = got_error_from_errno("strdup");
3249 error = got_worktree_resolve_path(&path, worktree,
3256 error = got_error_from_errno("strdup");
3260 } else if (argc == 2) {
3262 errx(1, "-s option can't be used when diffing "
3263 "objects in repository");
3266 if (repo_path == NULL) {
3267 error = got_worktree_open(&worktree, cwd);
3268 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3272 got_worktree_get_repo_path(worktree));
3273 if (repo_path == NULL) {
3274 error = got_error_from_errno("strdup");
3278 repo_path = strdup(cwd);
3279 if (repo_path == NULL) {
3280 error = got_error_from_errno("strdup");
3288 error = got_repo_open(&repo, repo_path, NULL);
3293 error = apply_unveil(got_repo_get_path(repo), 1,
3294 worktree ? got_worktree_get_root_path(worktree) : NULL);
3299 struct print_diff_arg arg;
3300 struct got_pathlist_head paths;
3305 error = got_object_id_str(&id_str,
3306 got_worktree_get_base_commit_id(worktree));
3310 arg.worktree = worktree;
3311 arg.diff_context = diff_context;
3312 arg.id_str = id_str;
3313 arg.header_shown = 0;
3314 arg.diff_staged = diff_staged;
3315 arg.ignore_whitespace = ignore_whitespace;
3317 error = got_pathlist_append(&paths, path, NULL);
3321 error = got_worktree_status(worktree, &paths, repo, print_diff,
3322 &arg, check_cancelled, NULL);
3324 got_pathlist_free(&paths);
3328 error = got_repo_match_object_id(&id1, &label1, id_str1,
3329 GOT_OBJ_TYPE_ANY, 1, repo);
3333 error = got_repo_match_object_id(&id2, &label2, id_str2,
3334 GOT_OBJ_TYPE_ANY, 1, repo);
3338 error = got_object_get_type(&type1, repo, id1);
3342 error = got_object_get_type(&type2, repo, id2);
3346 if (type1 != type2) {
3347 error = got_error(GOT_ERR_OBJ_TYPE);
3352 case GOT_OBJ_TYPE_BLOB:
3353 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3354 diff_context, ignore_whitespace, repo, stdout);
3356 case GOT_OBJ_TYPE_TREE:
3357 error = got_diff_objects_as_trees(id1, id2, "", "",
3358 diff_context, ignore_whitespace, repo, stdout);
3360 case GOT_OBJ_TYPE_COMMIT:
3361 printf("diff %s %s\n", label1, label2);
3362 error = got_diff_objects_as_commits(id1, id2, diff_context,
3363 ignore_whitespace, repo, stdout);
3366 error = got_error(GOT_ERR_OBJ_TYPE);
3375 got_worktree_close(worktree);
3377 const struct got_error *repo_error;
3378 repo_error = got_repo_close(repo);
3389 "usage: %s blame [-c commit] [-r repository-path] path\n",
3398 char datebuf[11]; /* YYYY-MM-DD + NUL */
3401 struct blame_cb_args {
3402 struct blame_line *lines;
3406 off_t *line_offsets;
3408 struct got_repository *repo;
3411 static const struct got_error *
3412 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3414 const struct got_error *err = NULL;
3415 struct blame_cb_args *a = arg;
3416 struct blame_line *bline;
3418 size_t linesize = 0;
3419 struct got_commit_object *commit = NULL;
3422 time_t committer_time;
3424 if (nlines != a->nlines ||
3425 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3426 return got_error(GOT_ERR_RANGE);
3428 if (sigint_received)
3429 return got_error(GOT_ERR_ITER_COMPLETED);
3432 return NULL; /* no change in this commit */
3434 /* Annotate this line. */
3435 bline = &a->lines[lineno - 1];
3436 if (bline->annotated)
3438 err = got_object_id_str(&bline->id_str, id);
3442 err = got_object_open_as_commit(&commit, a->repo, id);
3446 bline->committer = strdup(got_object_commit_get_committer(commit));
3447 if (bline->committer == NULL) {
3448 err = got_error_from_errno("strdup");
3452 committer_time = got_object_commit_get_committer_time(commit);
3453 if (localtime_r(&committer_time, &tm) == NULL)
3454 return got_error_from_errno("localtime_r");
3455 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3456 &tm) >= sizeof(bline->datebuf)) {
3457 err = got_error(GOT_ERR_NO_SPACE);
3460 bline->annotated = 1;
3462 /* Print lines annotated so far. */
3463 bline = &a->lines[a->lineno_cur - 1];
3464 if (!bline->annotated)
3467 offset = a->line_offsets[a->lineno_cur - 1];
3468 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3469 err = got_error_from_errno("fseeko");
3473 while (bline->annotated) {
3474 char *smallerthan, *at, *nl, *committer;
3477 if (getline(&line, &linesize, a->f) == -1) {
3479 err = got_error_from_errno("getline");
3483 committer = bline->committer;
3484 smallerthan = strchr(committer, '<');
3485 if (smallerthan && smallerthan[1] != '\0')
3486 committer = smallerthan + 1;
3487 at = strchr(committer, '@');
3490 len = strlen(committer);
3492 committer[8] = '\0';
3494 nl = strchr(line, '\n');
3497 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3498 bline->id_str, bline->datebuf, committer, line);
3501 bline = &a->lines[a->lineno_cur - 1];
3505 got_object_commit_close(commit);
3510 static const struct got_error *
3511 cmd_blame(int argc, char *argv[])
3513 const struct got_error *error;
3514 struct got_repository *repo = NULL;
3515 struct got_worktree *worktree = NULL;
3516 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3517 struct got_object_id *obj_id = NULL;
3518 struct got_object_id *commit_id = NULL;
3519 struct got_blob_object *blob = NULL;
3520 char *commit_id_str = NULL;
3521 struct blame_cb_args bca;
3522 int ch, obj_type, i;
3525 memset(&bca, 0, sizeof(bca));
3528 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3533 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3536 commit_id_str = optarg;
3539 repo_path = realpath(optarg, NULL);
3540 if (repo_path == NULL)
3541 return got_error_from_errno2("realpath",
3543 got_path_strip_trailing_slashes(repo_path);
3559 cwd = getcwd(NULL, 0);
3561 error = got_error_from_errno("getcwd");
3564 if (repo_path == NULL) {
3565 error = got_worktree_open(&worktree, cwd);
3566 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3572 strdup(got_worktree_get_repo_path(worktree));
3573 if (repo_path == NULL) {
3574 error = got_error_from_errno("strdup");
3579 repo_path = strdup(cwd);
3580 if (repo_path == NULL) {
3581 error = got_error_from_errno("strdup");
3587 error = got_repo_open(&repo, repo_path, NULL);
3591 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3596 const char *prefix = got_worktree_get_path_prefix(worktree);
3597 char *p, *worktree_subdir = cwd +
3598 strlen(got_worktree_get_root_path(worktree));
3599 if (asprintf(&p, "%s%s%s%s%s",
3600 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3601 worktree_subdir, worktree_subdir[0] ? "/" : "",
3603 error = got_error_from_errno("asprintf");
3606 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3609 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3614 if (commit_id_str == NULL) {
3615 struct got_reference *head_ref;
3616 error = got_ref_open(&head_ref, repo, worktree ?
3617 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3620 error = got_ref_resolve(&commit_id, repo, head_ref);
3621 got_ref_close(head_ref);
3625 error = got_repo_match_object_id(&commit_id, NULL,
3626 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3631 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3635 error = got_object_get_type(&obj_type, repo, obj_id);
3639 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3640 error = got_error(GOT_ERR_OBJ_TYPE);
3644 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3647 bca.f = got_opentemp();
3648 if (bca.f == NULL) {
3649 error = got_error_from_errno("got_opentemp");
3652 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3653 &bca.line_offsets, bca.f, blob);
3654 if (error || bca.nlines == 0)
3657 /* Don't include \n at EOF in the blame line count. */
3658 if (bca.line_offsets[bca.nlines - 1] == filesize)
3661 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3662 if (bca.lines == NULL) {
3663 error = got_error_from_errno("calloc");
3667 bca.nlines_prec = 0;
3675 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3676 check_cancelled, NULL);
3684 got_object_blob_close(blob);
3686 got_worktree_close(worktree);
3688 const struct got_error *repo_error;
3689 repo_error = got_repo_close(repo);
3694 for (i = 0; i < bca.nlines; i++) {
3695 struct blame_line *bline = &bca.lines[i];
3696 free(bline->id_str);
3697 free(bline->committer);
3701 free(bca.line_offsets);
3702 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3703 error = got_error_from_errno("fclose");
3711 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3717 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3718 const char *root_path)
3720 int is_root_path = (strcmp(path, root_path) == 0);
3721 const char *modestr = "";
3722 mode_t mode = got_tree_entry_get_mode(te);
3724 path += strlen(root_path);
3725 while (path[0] == '/')
3728 if (got_object_tree_entry_is_submodule(te))
3730 else if (S_ISLNK(mode))
3732 else if (S_ISDIR(mode))
3734 else if (mode & S_IXUSR)
3737 printf("%s%s%s%s%s\n", id ? id : "", path,
3738 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3741 static const struct got_error *
3742 print_tree(const char *path, struct got_object_id *commit_id,
3743 int show_ids, int recurse, const char *root_path,
3744 struct got_repository *repo)
3746 const struct got_error *err = NULL;
3747 struct got_object_id *tree_id = NULL;
3748 struct got_tree_object *tree = NULL;
3751 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3755 err = got_object_open_as_tree(&tree, repo, tree_id);
3758 nentries = got_object_tree_get_nentries(tree);
3759 for (i = 0; i < nentries; i++) {
3760 struct got_tree_entry *te;
3763 if (sigint_received || sigpipe_received)
3766 te = got_object_tree_get_entry(tree, i);
3769 err = got_object_id_str(&id_str,
3770 got_tree_entry_get_id(te));
3773 if (asprintf(&id, "%s ", id_str) == -1) {
3774 err = got_error_from_errno("asprintf");
3780 print_entry(te, id, path, root_path);
3783 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3785 if (asprintf(&child_path, "%s%s%s", path,
3786 path[0] == '/' && path[1] == '\0' ? "" : "/",
3787 got_tree_entry_get_name(te)) == -1) {
3788 err = got_error_from_errno("asprintf");
3791 err = print_tree(child_path, commit_id, show_ids, 1,
3800 got_object_tree_close(tree);
3805 static const struct got_error *
3806 cmd_tree(int argc, char *argv[])
3808 const struct got_error *error;
3809 struct got_repository *repo = NULL;
3810 struct got_worktree *worktree = NULL;
3812 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3813 struct got_object_id *commit_id = NULL;
3814 char *commit_id_str = NULL;
3815 int show_ids = 0, recurse = 0;
3819 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3824 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3827 commit_id_str = optarg;
3830 repo_path = realpath(optarg, NULL);
3831 if (repo_path == NULL)
3832 return got_error_from_errno2("realpath",
3834 got_path_strip_trailing_slashes(repo_path);
3858 cwd = getcwd(NULL, 0);
3860 error = got_error_from_errno("getcwd");
3863 if (repo_path == NULL) {
3864 error = got_worktree_open(&worktree, cwd);
3865 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3871 strdup(got_worktree_get_repo_path(worktree));
3872 if (repo_path == NULL)
3873 error = got_error_from_errno("strdup");
3877 repo_path = strdup(cwd);
3878 if (repo_path == NULL) {
3879 error = got_error_from_errno("strdup");
3885 error = got_repo_open(&repo, repo_path, NULL);
3889 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3895 char *p, *worktree_subdir = cwd +
3896 strlen(got_worktree_get_root_path(worktree));
3897 if (asprintf(&p, "%s/%s",
3898 got_worktree_get_path_prefix(worktree),
3899 worktree_subdir) == -1) {
3900 error = got_error_from_errno("asprintf");
3903 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3910 if (in_repo_path == NULL) {
3911 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3916 if (commit_id_str == NULL) {
3917 struct got_reference *head_ref;
3918 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3921 error = got_ref_resolve(&commit_id, repo, head_ref);
3922 got_ref_close(head_ref);
3926 error = got_repo_match_object_id(&commit_id, NULL,
3927 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3932 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3933 in_repo_path, repo);
3940 got_worktree_close(worktree);
3942 const struct got_error *repo_error;
3943 repo_error = got_repo_close(repo);
3953 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3957 static const struct got_error *
3958 print_status(void *arg, unsigned char status, unsigned char staged_status,
3959 const char *path, struct got_object_id *blob_id,
3960 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3961 int dirfd, const char *de_name)
3963 if (status == staged_status && (status == GOT_STATUS_DELETE))
3964 status = GOT_STATUS_NO_CHANGE;
3965 printf("%c%c %s\n", status, staged_status, path);
3969 static const struct got_error *
3970 cmd_status(int argc, char *argv[])
3972 const struct got_error *error = NULL;
3973 struct got_repository *repo = NULL;
3974 struct got_worktree *worktree = NULL;
3976 struct got_pathlist_head paths;
3977 struct got_pathlist_entry *pe;
3982 while ((ch = getopt(argc, argv, "")) != -1) {
3994 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3998 cwd = getcwd(NULL, 0);
4000 error = got_error_from_errno("getcwd");
4004 error = got_worktree_open(&worktree, cwd);
4008 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4013 error = apply_unveil(got_repo_get_path(repo), 1,
4014 got_worktree_get_root_path(worktree));
4018 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4022 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4023 check_cancelled, NULL);
4025 TAILQ_FOREACH(pe, &paths, entry)
4026 free((char *)pe->path);
4027 got_pathlist_free(&paths);
4036 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4041 static const struct got_error *
4042 list_refs(struct got_repository *repo)
4044 static const struct got_error *err = NULL;
4045 struct got_reflist_head refs;
4046 struct got_reflist_entry *re;
4048 SIMPLEQ_INIT(&refs);
4049 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4053 SIMPLEQ_FOREACH(re, &refs, entry) {
4055 refstr = got_ref_to_str(re->ref);
4057 return got_error_from_errno("got_ref_to_str");
4058 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4062 got_ref_list_free(&refs);
4066 static const struct got_error *
4067 delete_ref(struct got_repository *repo, const char *refname)
4069 const struct got_error *err = NULL;
4070 struct got_reference *ref;
4072 err = got_ref_open(&ref, repo, refname, 0);
4076 err = got_ref_delete(ref, repo);
4081 static const struct got_error *
4082 add_ref(struct got_repository *repo, const char *refname, const char *target)
4084 const struct got_error *err = NULL;
4085 struct got_object_id *id;
4086 struct got_reference *ref = NULL;
4089 * Don't let the user create a reference name with a leading '-'.
4090 * While technically a valid reference name, this case is usually
4091 * an unintended typo.
4093 if (refname[0] == '-')
4094 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4096 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4099 struct got_reference *target_ref;
4101 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4103 err = got_ref_open(&target_ref, repo, target, 0);
4106 err = got_ref_resolve(&id, repo, target_ref);
4107 got_ref_close(target_ref);
4112 err = got_ref_alloc(&ref, refname, id);
4116 err = got_ref_write(ref, repo);
4124 static const struct got_error *
4125 add_symref(struct got_repository *repo, const char *refname, const char *target)
4127 const struct got_error *err = NULL;
4128 struct got_reference *ref = NULL;
4129 struct got_reference *target_ref = NULL;
4132 * Don't let the user create a reference name with a leading '-'.
4133 * While technically a valid reference name, this case is usually
4134 * an unintended typo.
4136 if (refname[0] == '-')
4137 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4139 err = got_ref_open(&target_ref, repo, target, 0);
4143 err = got_ref_alloc_symref(&ref, refname, target_ref);
4147 err = got_ref_write(ref, repo);
4150 got_ref_close(target_ref);
4156 static const struct got_error *
4157 cmd_ref(int argc, char *argv[])
4159 const struct got_error *error = NULL;
4160 struct got_repository *repo = NULL;
4161 struct got_worktree *worktree = NULL;
4162 char *cwd = NULL, *repo_path = NULL;
4163 int ch, do_list = 0, create_symref = 0;
4164 const char *delref = NULL;
4166 /* TODO: Add -s option for adding symbolic references. */
4167 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4173 repo_path = realpath(optarg, NULL);
4174 if (repo_path == NULL)
4175 return got_error_from_errno2("realpath",
4177 got_path_strip_trailing_slashes(repo_path);
4191 if (do_list && delref)
4192 errx(1, "-l and -d options are mutually exclusive\n");
4197 if (do_list || delref) {
4199 errx(1, "-s option cannot be used together with the "
4200 "-l or -d options");
4203 } else if (argc != 2)
4208 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4212 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4213 "sendfd unveil", NULL) == -1)
4217 cwd = getcwd(NULL, 0);
4219 error = got_error_from_errno("getcwd");
4223 if (repo_path == NULL) {
4224 error = got_worktree_open(&worktree, cwd);
4225 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4231 strdup(got_worktree_get_repo_path(worktree));
4232 if (repo_path == NULL)
4233 error = got_error_from_errno("strdup");
4237 repo_path = strdup(cwd);
4238 if (repo_path == NULL) {
4239 error = got_error_from_errno("strdup");
4245 error = got_repo_open(&repo, repo_path, NULL);
4249 error = apply_unveil(got_repo_get_path(repo), do_list,
4250 worktree ? got_worktree_get_root_path(worktree) : NULL);
4255 error = list_refs(repo);
4257 error = delete_ref(repo, delref);
4258 else if (create_symref)
4259 error = add_symref(repo, argv[0], argv[1]);
4261 error = add_ref(repo, argv[0], argv[1]);
4264 got_repo_close(repo);
4266 got_worktree_close(worktree);
4276 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4277 "[name]\n", getprogname());
4281 static const struct got_error *
4282 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4283 struct got_reference *ref)
4285 const struct got_error *err = NULL;
4286 const char *refname, *marker = " ";
4289 refname = got_ref_get_name(ref);
4290 if (worktree && strcmp(refname,
4291 got_worktree_get_head_ref_name(worktree)) == 0) {
4292 struct got_object_id *id = NULL;
4294 err = got_ref_resolve(&id, repo, ref);
4297 if (got_object_id_cmp(id,
4298 got_worktree_get_base_commit_id(worktree)) == 0)
4305 if (strncmp(refname, "refs/heads/", 11) == 0)
4307 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4310 refstr = got_ref_to_str(ref);
4312 return got_error_from_errno("got_ref_to_str");
4314 printf("%s%s: %s\n", marker, refname, refstr);
4319 static const struct got_error *
4320 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4322 const char *refname;
4324 if (worktree == NULL)
4325 return got_error(GOT_ERR_NOT_WORKTREE);
4327 refname = got_worktree_get_head_ref_name(worktree);
4329 if (strncmp(refname, "refs/heads/", 11) == 0)
4331 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4334 printf("%s\n", refname);
4339 static const struct got_error *
4340 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4342 static const struct got_error *err = NULL;
4343 struct got_reflist_head refs;
4344 struct got_reflist_entry *re;
4345 struct got_reference *temp_ref = NULL;
4346 int rebase_in_progress, histedit_in_progress;
4348 SIMPLEQ_INIT(&refs);
4351 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4356 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4361 if (rebase_in_progress || histedit_in_progress) {
4362 err = got_ref_open(&temp_ref, repo,
4363 got_worktree_get_head_ref_name(worktree), 0);
4366 list_branch(repo, worktree, temp_ref);
4367 got_ref_close(temp_ref);
4371 err = got_ref_list(&refs, repo, "refs/heads",
4372 got_ref_cmp_by_name, NULL);
4376 SIMPLEQ_FOREACH(re, &refs, entry)
4377 list_branch(repo, worktree, re->ref);
4379 got_ref_list_free(&refs);
4383 static const struct got_error *
4384 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4385 const char *branch_name)
4387 const struct got_error *err = NULL;
4388 struct got_reference *ref = NULL;
4391 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4392 return got_error_from_errno("asprintf");
4394 err = got_ref_open(&ref, repo, refname, 0);
4399 strcmp(got_worktree_get_head_ref_name(worktree),
4400 got_ref_get_name(ref)) == 0) {
4401 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4402 "will not delete this work tree's current branch");
4406 err = got_ref_delete(ref, repo);
4414 static const struct got_error *
4415 add_branch(struct got_repository *repo, const char *branch_name,
4416 struct got_object_id *base_commit_id)
4418 const struct got_error *err = NULL;
4419 struct got_reference *ref = NULL;
4420 char *base_refname = NULL, *refname = NULL;
4423 * Don't let the user create a branch name with a leading '-'.
4424 * While technically a valid reference name, this case is usually
4425 * an unintended typo.
4427 if (branch_name[0] == '-')
4428 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4430 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4431 err = got_error_from_errno("asprintf");
4435 err = got_ref_open(&ref, repo, refname, 0);
4437 err = got_error(GOT_ERR_BRANCH_EXISTS);
4439 } else if (err->code != GOT_ERR_NOT_REF)
4442 err = got_ref_alloc(&ref, refname, base_commit_id);
4446 err = got_ref_write(ref, repo);
4455 static const struct got_error *
4456 cmd_branch(int argc, char *argv[])
4458 const struct got_error *error = NULL;
4459 struct got_repository *repo = NULL;
4460 struct got_worktree *worktree = NULL;
4461 char *cwd = NULL, *repo_path = NULL;
4462 int ch, do_list = 0, do_show = 0, do_update = 1;
4463 const char *delref = NULL, *commit_id_arg = NULL;
4464 struct got_reference *ref = NULL;
4465 struct got_pathlist_head paths;
4466 struct got_pathlist_entry *pe;
4467 struct got_object_id *commit_id = NULL;
4468 char *commit_id_str = NULL;
4472 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4475 commit_id_arg = optarg;
4481 repo_path = realpath(optarg, NULL);
4482 if (repo_path == NULL)
4483 return got_error_from_errno2("realpath",
4485 got_path_strip_trailing_slashes(repo_path);
4499 if (do_list && delref)
4500 errx(1, "-l and -d options are mutually exclusive\n");
4505 if (!do_list && !delref && argc == 0)
4508 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4509 errx(1, "-c option can only be used when creating a branch");
4511 if (do_list || delref) {
4514 } else if (!do_show && argc != 1)
4518 if (do_list || do_show) {
4519 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4523 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4524 "sendfd unveil", NULL) == -1)
4528 cwd = getcwd(NULL, 0);
4530 error = got_error_from_errno("getcwd");
4534 if (repo_path == NULL) {
4535 error = got_worktree_open(&worktree, cwd);
4536 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4542 strdup(got_worktree_get_repo_path(worktree));
4543 if (repo_path == NULL)
4544 error = got_error_from_errno("strdup");
4548 repo_path = strdup(cwd);
4549 if (repo_path == NULL) {
4550 error = got_error_from_errno("strdup");
4556 error = got_repo_open(&repo, repo_path, NULL);
4560 error = apply_unveil(got_repo_get_path(repo), do_list,
4561 worktree ? got_worktree_get_root_path(worktree) : NULL);
4566 error = show_current_branch(repo, worktree);
4568 error = list_branches(repo, worktree);
4570 error = delete_branch(repo, worktree, delref);
4572 if (commit_id_arg == NULL)
4573 commit_id_arg = worktree ?
4574 got_worktree_get_head_ref_name(worktree) :
4576 error = got_repo_match_object_id(&commit_id, NULL,
4577 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4580 error = add_branch(repo, argv[0], commit_id);
4583 if (worktree && do_update) {
4584 int did_something = 0;
4585 char *branch_refname = NULL;
4587 error = got_object_id_str(&commit_id_str, commit_id);
4590 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4594 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4596 error = got_error_from_errno("asprintf");
4599 error = got_ref_open(&ref, repo, branch_refname, 0);
4600 free(branch_refname);
4603 error = switch_head_ref(ref, commit_id, worktree,
4607 error = got_worktree_set_base_commit_id(worktree, repo,
4611 error = got_worktree_checkout_files(worktree, &paths,
4612 repo, update_progress, &did_something,
4613 check_cancelled, NULL);
4617 printf("Updated to commit %s\n", commit_id_str);
4624 got_repo_close(repo);
4626 got_worktree_close(worktree);
4630 free(commit_id_str);
4631 TAILQ_FOREACH(pe, &paths, entry)
4632 free((char *)pe->path);
4633 got_pathlist_free(&paths);
4642 "usage: %s tag [-c commit] [-r repository] [-l] "
4643 "[-m message] name\n", getprogname());
4648 static const struct got_error *
4649 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4651 const struct got_error *err = NULL;
4652 struct got_reflist_entry *re, *se, *new;
4653 struct got_object_id *re_id, *se_id;
4654 struct got_tag_object *re_tag, *se_tag;
4655 time_t re_time, se_time;
4657 SIMPLEQ_FOREACH(re, tags, entry) {
4658 se = SIMPLEQ_FIRST(sorted);
4660 err = got_reflist_entry_dup(&new, re);
4663 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4666 err = got_ref_resolve(&re_id, repo, re->ref);
4669 err = got_object_open_as_tag(&re_tag, repo, re_id);
4673 re_time = got_object_tag_get_tagger_time(re_tag);
4674 got_object_tag_close(re_tag);
4678 err = got_ref_resolve(&se_id, repo, re->ref);
4681 err = got_object_open_as_tag(&se_tag, repo, se_id);
4685 se_time = got_object_tag_get_tagger_time(se_tag);
4686 got_object_tag_close(se_tag);
4688 if (se_time > re_time) {
4689 err = got_reflist_entry_dup(&new, re);
4692 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4695 se = SIMPLEQ_NEXT(se, entry);
4704 static const struct got_error *
4705 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4707 static const struct got_error *err = NULL;
4708 struct got_reflist_head refs;
4709 struct got_reflist_entry *re;
4711 SIMPLEQ_INIT(&refs);
4713 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4717 SIMPLEQ_FOREACH(re, &refs, entry) {
4718 const char *refname;
4719 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4723 struct got_object_id *id;
4724 struct got_tag_object *tag;
4725 struct got_commit_object *commit = NULL;
4727 refname = got_ref_get_name(re->ref);
4728 if (strncmp(refname, "refs/tags/", 10) != 0)
4731 refstr = got_ref_to_str(re->ref);
4732 if (refstr == NULL) {
4733 err = got_error_from_errno("got_ref_to_str");
4736 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4739 err = got_ref_resolve(&id, repo, re->ref);
4742 err = got_object_open_as_tag(&tag, repo, id);
4744 if (err->code != GOT_ERR_OBJ_TYPE) {
4748 /* "lightweight" tag */
4749 err = got_object_open_as_commit(&commit, repo, id);
4754 tagger = got_object_commit_get_committer(commit);
4756 got_object_commit_get_committer_time(commit);
4757 err = got_object_id_str(&id_str, id);
4763 tagger = got_object_tag_get_tagger(tag);
4764 tagger_time = got_object_tag_get_tagger_time(tag);
4765 err = got_object_id_str(&id_str,
4766 got_object_tag_get_object_id(tag));
4770 printf("from: %s\n", tagger);
4771 datestr = get_datestr(&tagger_time, datebuf);
4773 printf("date: %s UTC\n", datestr);
4775 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4777 switch (got_object_tag_get_object_type(tag)) {
4778 case GOT_OBJ_TYPE_BLOB:
4779 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4782 case GOT_OBJ_TYPE_TREE:
4783 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4786 case GOT_OBJ_TYPE_COMMIT:
4787 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4790 case GOT_OBJ_TYPE_TAG:
4791 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4800 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4803 got_object_commit_close(commit);
4805 tagmsg0 = strdup(got_object_tag_get_message(tag));
4806 got_object_tag_close(tag);
4807 if (tagmsg0 == NULL) {
4808 err = got_error_from_errno("strdup");
4815 line = strsep(&tagmsg, "\n");
4817 printf(" %s\n", line);
4822 got_ref_list_free(&refs);
4826 static const struct got_error *
4827 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4828 const char *tag_name, const char *repo_path)
4830 const struct got_error *err = NULL;
4831 char *template = NULL, *initial_content = NULL;
4832 char *editor = NULL;
4835 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4836 err = got_error_from_errno("asprintf");
4840 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4841 commit_id_str, tag_name) == -1) {
4842 err = got_error_from_errno("asprintf");
4846 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4850 dprintf(fd, initial_content);
4853 err = get_editor(&editor);
4856 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4858 free(initial_content);
4862 /* Editor is done; we can now apply unveil(2) */
4864 err = apply_unveil(repo_path, 0, NULL);
4873 static const struct got_error *
4874 add_tag(struct got_repository *repo, const char *tag_name,
4875 const char *commit_arg, const char *tagmsg_arg)
4877 const struct got_error *err = NULL;
4878 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4879 char *label = NULL, *commit_id_str = NULL;
4880 struct got_reference *ref = NULL;
4881 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4882 char *tagmsg_path = NULL, *tag_id_str = NULL;
4883 int preserve_tagmsg = 0;
4886 * Don't let the user create a tag name with a leading '-'.
4887 * While technically a valid reference name, this case is usually
4888 * an unintended typo.
4890 if (tag_name[0] == '-')
4891 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4893 err = get_author(&tagger, repo);
4897 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4898 GOT_OBJ_TYPE_COMMIT, 1, repo);
4902 err = got_object_id_str(&commit_id_str, commit_id);
4906 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4907 refname = strdup(tag_name);
4908 if (refname == NULL) {
4909 err = got_error_from_errno("strdup");
4913 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4914 err = got_error_from_errno("asprintf");
4918 err = got_ref_open(&ref, repo, refname, 0);
4920 err = got_error(GOT_ERR_TAG_EXISTS);
4922 } else if (err->code != GOT_ERR_NOT_REF)
4925 if (tagmsg_arg == NULL) {
4926 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4927 tag_name, got_repo_get_path(repo));
4929 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4930 tagmsg_path != NULL)
4931 preserve_tagmsg = 1;
4936 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4937 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4940 preserve_tagmsg = 1;
4944 err = got_ref_alloc(&ref, refname, tag_id);
4947 preserve_tagmsg = 1;
4951 err = got_ref_write(ref, repo);
4954 preserve_tagmsg = 1;
4958 err = got_object_id_str(&tag_id_str, tag_id);
4961 preserve_tagmsg = 1;
4964 printf("Created tag %s\n", tag_id_str);
4966 if (preserve_tagmsg) {
4967 fprintf(stderr, "%s: tag message preserved in %s\n",
4968 getprogname(), tagmsg_path);
4969 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4970 err = got_error_from_errno2("unlink", tagmsg_path);
4975 free(commit_id_str);
4983 static const struct got_error *
4984 cmd_tag(int argc, char *argv[])
4986 const struct got_error *error = NULL;
4987 struct got_repository *repo = NULL;
4988 struct got_worktree *worktree = NULL;
4989 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4990 char *gitconfig_path = NULL;
4991 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4992 int ch, do_list = 0;
4994 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4997 commit_id_arg = optarg;
5003 repo_path = realpath(optarg, NULL);
5004 if (repo_path == NULL)
5005 return got_error_from_errno2("realpath",
5007 got_path_strip_trailing_slashes(repo_path);
5022 if (commit_id_arg != NULL)
5023 errx(1, "-c option can only be used when creating a tag");
5025 errx(1, "-l and -m options are mutually exclusive");
5028 } else if (argc != 1)
5035 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5039 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5040 "sendfd unveil", NULL) == -1)
5044 cwd = getcwd(NULL, 0);
5046 error = got_error_from_errno("getcwd");
5050 if (repo_path == NULL) {
5051 error = got_worktree_open(&worktree, cwd);
5052 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5058 strdup(got_worktree_get_repo_path(worktree));
5059 if (repo_path == NULL)
5060 error = got_error_from_errno("strdup");
5064 repo_path = strdup(cwd);
5065 if (repo_path == NULL) {
5066 error = got_error_from_errno("strdup");
5073 error = got_repo_open(&repo, repo_path, NULL);
5076 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5079 error = list_tags(repo, worktree);
5081 error = get_gitconfig_path(&gitconfig_path);
5084 error = got_repo_open(&repo, repo_path, gitconfig_path);
5089 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5094 if (commit_id_arg == NULL) {
5095 struct got_reference *head_ref;
5096 struct got_object_id *commit_id;
5097 error = got_ref_open(&head_ref, repo,
5098 worktree ? got_worktree_get_head_ref_name(worktree)
5102 error = got_ref_resolve(&commit_id, repo, head_ref);
5103 got_ref_close(head_ref);
5106 error = got_object_id_str(&commit_id_str, commit_id);
5112 error = add_tag(repo, tag_name,
5113 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5117 got_repo_close(repo);
5119 got_worktree_close(worktree);
5122 free(gitconfig_path);
5123 free(commit_id_str);
5130 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5135 static const struct got_error *
5136 add_progress(void *arg, unsigned char status, const char *path)
5138 while (path[0] == '/')
5140 printf("%c %s\n", status, path);
5144 static const struct got_error *
5145 cmd_add(int argc, char *argv[])
5147 const struct got_error *error = NULL;
5148 struct got_repository *repo = NULL;
5149 struct got_worktree *worktree = NULL;
5151 struct got_pathlist_head paths;
5152 struct got_pathlist_entry *pe;
5153 int ch, can_recurse = 0, no_ignores = 0;
5157 while ((ch = getopt(argc, argv, "IR")) != -1) {
5175 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5182 cwd = getcwd(NULL, 0);
5184 error = got_error_from_errno("getcwd");
5188 error = got_worktree_open(&worktree, cwd);
5192 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5197 error = apply_unveil(got_repo_get_path(repo), 1,
5198 got_worktree_get_root_path(worktree));
5202 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5206 if (!can_recurse && no_ignores) {
5207 error = got_error_msg(GOT_ERR_BAD_PATH,
5208 "disregarding ignores requires -R option");
5216 TAILQ_FOREACH(pe, &paths, entry) {
5217 if (asprintf(&ondisk_path, "%s/%s",
5218 got_worktree_get_root_path(worktree),
5220 error = got_error_from_errno("asprintf");
5223 if (lstat(ondisk_path, &sb) == -1) {
5224 if (errno == ENOENT) {
5228 error = got_error_from_errno2("lstat",
5234 if (S_ISDIR(sb.st_mode)) {
5235 error = got_error_msg(GOT_ERR_BAD_PATH,
5236 "adding directories requires -R option");
5242 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5243 NULL, repo, no_ignores);
5246 got_repo_close(repo);
5248 got_worktree_close(worktree);
5249 TAILQ_FOREACH(pe, &paths, entry)
5250 free((char *)pe->path);
5251 got_pathlist_free(&paths);
5259 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5264 static const struct got_error *
5265 print_remove_status(void *arg, unsigned char status,
5266 unsigned char staged_status, const char *path)
5268 while (path[0] == '/')
5270 if (status == GOT_STATUS_NONEXISTENT)
5272 if (status == staged_status && (status == GOT_STATUS_DELETE))
5273 status = GOT_STATUS_NO_CHANGE;
5274 printf("%c%c %s\n", status, staged_status, path);
5278 static const struct got_error *
5279 cmd_remove(int argc, char *argv[])
5281 const struct got_error *error = NULL;
5282 struct got_worktree *worktree = NULL;
5283 struct got_repository *repo = NULL;
5285 struct got_pathlist_head paths;
5286 struct got_pathlist_entry *pe;
5287 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5291 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5294 delete_local_mods = 1;
5312 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5319 cwd = getcwd(NULL, 0);
5321 error = got_error_from_errno("getcwd");
5324 error = got_worktree_open(&worktree, cwd);
5328 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5333 error = apply_unveil(got_repo_get_path(repo), 1,
5334 got_worktree_get_root_path(worktree));
5338 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5345 TAILQ_FOREACH(pe, &paths, entry) {
5346 if (asprintf(&ondisk_path, "%s/%s",
5347 got_worktree_get_root_path(worktree),
5349 error = got_error_from_errno("asprintf");
5352 if (lstat(ondisk_path, &sb) == -1) {
5353 if (errno == ENOENT) {
5357 error = got_error_from_errno2("lstat",
5363 if (S_ISDIR(sb.st_mode)) {
5364 error = got_error_msg(GOT_ERR_BAD_PATH,
5365 "removing directories requires -R option");
5371 error = got_worktree_schedule_delete(worktree, &paths,
5372 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5375 got_repo_close(repo);
5377 got_worktree_close(worktree);
5378 TAILQ_FOREACH(pe, &paths, entry)
5379 free((char *)pe->path);
5380 got_pathlist_free(&paths);
5388 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5389 "path ...\n", getprogname());
5393 static const struct got_error *
5394 revert_progress(void *arg, unsigned char status, const char *path)
5396 if (status == GOT_STATUS_UNVERSIONED)
5399 while (path[0] == '/')
5401 printf("%c %s\n", status, path);
5405 struct choose_patch_arg {
5406 FILE *patch_script_file;
5410 static const struct got_error *
5411 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5412 int nchanges, const char *action)
5415 size_t linesize = 0;
5419 case GOT_STATUS_ADD:
5420 printf("A %s\n%s this addition? [y/n] ", path, action);
5422 case GOT_STATUS_DELETE:
5423 printf("D %s\n%s this deletion? [y/n] ", path, action);
5425 case GOT_STATUS_MODIFY:
5426 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5427 return got_error_from_errno("fseek");
5428 printf(GOT_COMMIT_SEP_STR);
5429 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5431 if (ferror(patch_file))
5432 return got_error_from_errno("getline");
5433 printf(GOT_COMMIT_SEP_STR);
5434 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5435 path, n, nchanges, action);
5438 return got_error_path(path, GOT_ERR_FILE_STATUS);
5444 static const struct got_error *
5445 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5446 FILE *patch_file, int n, int nchanges)
5448 const struct got_error *err = NULL;
5450 size_t linesize = 0;
5453 struct choose_patch_arg *a = arg;
5455 *choice = GOT_PATCH_CHOICE_NONE;
5457 if (a->patch_script_file) {
5459 err = show_change(status, path, patch_file, n, nchanges,
5463 linelen = getline(&line, &linesize, a->patch_script_file);
5464 if (linelen == -1) {
5465 if (ferror(a->patch_script_file))
5466 return got_error_from_errno("getline");
5469 nl = strchr(line, '\n');
5472 if (strcmp(line, "y") == 0) {
5473 *choice = GOT_PATCH_CHOICE_YES;
5475 } else if (strcmp(line, "n") == 0) {
5476 *choice = GOT_PATCH_CHOICE_NO;
5478 } else if (strcmp(line, "q") == 0 &&
5479 status == GOT_STATUS_MODIFY) {
5480 *choice = GOT_PATCH_CHOICE_QUIT;
5483 printf("invalid response '%s'\n", line);
5488 while (resp != 'y' && resp != 'n' && resp != 'q') {
5489 err = show_change(status, path, patch_file, n, nchanges,
5496 if (status == GOT_STATUS_MODIFY) {
5497 if (resp != 'y' && resp != 'n' && resp != 'q') {
5498 printf("invalid response '%c'\n", resp);
5501 } else if (resp != 'y' && resp != 'n') {
5502 printf("invalid response '%c'\n", resp);
5508 *choice = GOT_PATCH_CHOICE_YES;
5509 else if (resp == 'n')
5510 *choice = GOT_PATCH_CHOICE_NO;
5511 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5512 *choice = GOT_PATCH_CHOICE_QUIT;
5518 static const struct got_error *
5519 cmd_revert(int argc, char *argv[])
5521 const struct got_error *error = NULL;
5522 struct got_worktree *worktree = NULL;
5523 struct got_repository *repo = NULL;
5524 char *cwd = NULL, *path = NULL;
5525 struct got_pathlist_head paths;
5526 struct got_pathlist_entry *pe;
5527 int ch, can_recurse = 0, pflag = 0;
5528 FILE *patch_script_file = NULL;
5529 const char *patch_script_path = NULL;
5530 struct choose_patch_arg cpa;
5534 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5540 patch_script_path = optarg;
5555 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5556 "unveil", NULL) == -1)
5561 if (patch_script_path && !pflag)
5562 errx(1, "-F option can only be used together with -p option");
5564 cwd = getcwd(NULL, 0);
5566 error = got_error_from_errno("getcwd");
5569 error = got_worktree_open(&worktree, cwd);
5573 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5578 if (patch_script_path) {
5579 patch_script_file = fopen(patch_script_path, "r");
5580 if (patch_script_file == NULL) {
5581 error = got_error_from_errno2("fopen",
5586 error = apply_unveil(got_repo_get_path(repo), 1,
5587 got_worktree_get_root_path(worktree));
5591 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5598 TAILQ_FOREACH(pe, &paths, entry) {
5599 if (asprintf(&ondisk_path, "%s/%s",
5600 got_worktree_get_root_path(worktree),
5602 error = got_error_from_errno("asprintf");
5605 if (lstat(ondisk_path, &sb) == -1) {
5606 if (errno == ENOENT) {
5610 error = got_error_from_errno2("lstat",
5616 if (S_ISDIR(sb.st_mode)) {
5617 error = got_error_msg(GOT_ERR_BAD_PATH,
5618 "reverting directories requires -R option");
5624 cpa.patch_script_file = patch_script_file;
5625 cpa.action = "revert";
5626 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5627 pflag ? choose_patch : NULL, &cpa, repo);
5629 if (patch_script_file && fclose(patch_script_file) == EOF &&
5631 error = got_error_from_errno2("fclose", patch_script_path);
5633 got_repo_close(repo);
5635 got_worktree_close(worktree);
5644 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5649 struct collect_commit_logmsg_arg {
5650 const char *cmdline_log;
5652 const char *worktree_path;
5653 const char *branch_name;
5654 const char *repo_path;
5659 static const struct got_error *
5660 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5663 char *initial_content = NULL;
5664 struct got_pathlist_entry *pe;
5665 const struct got_error *err = NULL;
5666 char *template = NULL;
5667 struct collect_commit_logmsg_arg *a = arg;
5671 /* if a message was specified on the command line, just use it */
5672 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5673 len = strlen(a->cmdline_log) + 1;
5674 *logmsg = malloc(len + 1);
5675 if (*logmsg == NULL)
5676 return got_error_from_errno("malloc");
5677 strlcpy(*logmsg, a->cmdline_log, len);
5681 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5682 return got_error_from_errno("asprintf");
5684 if (asprintf(&initial_content,
5685 "\n# changes to be committed on branch %s:\n",
5686 a->branch_name) == -1)
5687 return got_error_from_errno("asprintf");
5689 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5693 dprintf(fd, initial_content);
5695 TAILQ_FOREACH(pe, commitable_paths, entry) {
5696 struct got_commitable *ct = pe->data;
5697 dprintf(fd, "# %c %s\n",
5698 got_commitable_get_status(ct),
5699 got_commitable_get_path(ct));
5703 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5705 free(initial_content);
5708 /* Editor is done; we can now apply unveil(2) */
5710 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5719 static const struct got_error *
5720 cmd_commit(int argc, char *argv[])
5722 const struct got_error *error = NULL;
5723 struct got_worktree *worktree = NULL;
5724 struct got_repository *repo = NULL;
5725 char *cwd = NULL, *id_str = NULL;
5726 struct got_object_id *id = NULL;
5727 const char *logmsg = NULL;
5728 struct collect_commit_logmsg_arg cl_arg;
5729 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5730 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5731 struct got_pathlist_head paths;
5734 cl_arg.logmsg_path = NULL;
5736 while ((ch = getopt(argc, argv, "m:")) != -1) {
5751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5752 "unveil", NULL) == -1)
5755 cwd = getcwd(NULL, 0);
5757 error = got_error_from_errno("getcwd");
5760 error = got_worktree_open(&worktree, cwd);
5764 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5767 if (rebase_in_progress) {
5768 error = got_error(GOT_ERR_REBASING);
5772 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5777 error = get_gitconfig_path(&gitconfig_path);
5780 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5785 error = get_author(&author, repo);
5790 * unveil(2) traverses exec(2); if an editor is used we have
5791 * to apply unveil after the log message has been written.
5793 if (logmsg == NULL || strlen(logmsg) == 0)
5794 error = get_editor(&editor);
5796 error = apply_unveil(got_repo_get_path(repo), 0,
5797 got_worktree_get_root_path(worktree));
5801 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5805 cl_arg.editor = editor;
5806 cl_arg.cmdline_log = logmsg;
5807 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5808 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5809 if (!histedit_in_progress) {
5810 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5811 error = got_error(GOT_ERR_COMMIT_BRANCH);
5814 cl_arg.branch_name += 11;
5816 cl_arg.repo_path = got_repo_get_path(repo);
5817 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5818 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5820 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5821 cl_arg.logmsg_path != NULL)
5822 preserve_logmsg = 1;
5826 error = got_object_id_str(&id_str, id);
5829 printf("Created commit %s\n", id_str);
5831 if (preserve_logmsg) {
5832 fprintf(stderr, "%s: log message preserved in %s\n",
5833 getprogname(), cl_arg.logmsg_path);
5834 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5836 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5837 free(cl_arg.logmsg_path);
5839 got_repo_close(repo);
5841 got_worktree_close(worktree);
5844 free(gitconfig_path);
5851 usage_cherrypick(void)
5853 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5857 static const struct got_error *
5858 cmd_cherrypick(int argc, char *argv[])
5860 const struct got_error *error = NULL;
5861 struct got_worktree *worktree = NULL;
5862 struct got_repository *repo = NULL;
5863 char *cwd = NULL, *commit_id_str = NULL;
5864 struct got_object_id *commit_id = NULL;
5865 struct got_commit_object *commit = NULL;
5866 struct got_object_qid *pid;
5867 struct got_reference *head_ref = NULL;
5868 int ch, did_something = 0;
5870 while ((ch = getopt(argc, argv, "")) != -1) {
5882 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5883 "unveil", NULL) == -1)
5889 cwd = getcwd(NULL, 0);
5891 error = got_error_from_errno("getcwd");
5894 error = got_worktree_open(&worktree, cwd);
5898 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5903 error = apply_unveil(got_repo_get_path(repo), 0,
5904 got_worktree_get_root_path(worktree));
5908 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5909 GOT_OBJ_TYPE_COMMIT, repo);
5910 if (error != NULL) {
5911 struct got_reference *ref;
5912 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5914 error = got_ref_open(&ref, repo, argv[0], 0);
5917 error = got_ref_resolve(&commit_id, repo, ref);
5922 error = got_object_id_str(&commit_id_str, commit_id);
5926 error = got_ref_open(&head_ref, repo,
5927 got_worktree_get_head_ref_name(worktree), 0);
5931 error = check_same_branch(commit_id, head_ref, NULL, repo);
5933 if (error->code != GOT_ERR_ANCESTRY)
5937 error = got_error(GOT_ERR_SAME_BRANCH);
5941 error = got_object_open_as_commit(&commit, repo, commit_id);
5944 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5945 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5946 commit_id, repo, update_progress, &did_something, check_cancelled,
5952 printf("Merged commit %s\n", commit_id_str);
5955 got_object_commit_close(commit);
5956 free(commit_id_str);
5958 got_ref_close(head_ref);
5960 got_worktree_close(worktree);
5962 got_repo_close(repo);
5969 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5973 static const struct got_error *
5974 cmd_backout(int argc, char *argv[])
5976 const struct got_error *error = NULL;
5977 struct got_worktree *worktree = NULL;
5978 struct got_repository *repo = NULL;
5979 char *cwd = NULL, *commit_id_str = NULL;
5980 struct got_object_id *commit_id = NULL;
5981 struct got_commit_object *commit = NULL;
5982 struct got_object_qid *pid;
5983 struct got_reference *head_ref = NULL;
5984 int ch, did_something = 0;
5986 while ((ch = getopt(argc, argv, "")) != -1) {
5998 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5999 "unveil", NULL) == -1)
6005 cwd = getcwd(NULL, 0);
6007 error = got_error_from_errno("getcwd");
6010 error = got_worktree_open(&worktree, cwd);
6014 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6019 error = apply_unveil(got_repo_get_path(repo), 0,
6020 got_worktree_get_root_path(worktree));
6024 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6025 GOT_OBJ_TYPE_COMMIT, repo);
6026 if (error != NULL) {
6027 struct got_reference *ref;
6028 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6030 error = got_ref_open(&ref, repo, argv[0], 0);
6033 error = got_ref_resolve(&commit_id, repo, ref);
6038 error = got_object_id_str(&commit_id_str, commit_id);
6042 error = got_ref_open(&head_ref, repo,
6043 got_worktree_get_head_ref_name(worktree), 0);
6047 error = check_same_branch(commit_id, head_ref, NULL, repo);
6051 error = got_object_open_as_commit(&commit, repo, commit_id);
6054 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6056 error = got_error(GOT_ERR_ROOT_COMMIT);
6060 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6061 update_progress, &did_something, check_cancelled, NULL);
6066 printf("Backed out commit %s\n", commit_id_str);
6069 got_object_commit_close(commit);
6070 free(commit_id_str);
6072 got_ref_close(head_ref);
6074 got_worktree_close(worktree);
6076 got_repo_close(repo);
6083 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6089 trim_logmsg(char *logmsg, int limit)
6094 len = strlen(logmsg);
6098 nl = strchr(logmsg, '\n');
6103 static const struct got_error *
6104 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6106 const struct got_error *err;
6107 char *logmsg0 = NULL;
6110 err = got_object_commit_get_logmsg(&logmsg0, commit);
6115 while (isspace((unsigned char)s[0]))
6118 *logmsg = strdup(s);
6119 if (*logmsg == NULL) {
6120 err = got_error_from_errno("strdup");
6124 trim_logmsg(*logmsg, limit);
6130 static const struct got_error *
6131 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6133 const struct got_error *err;
6134 struct got_commit_object *commit = NULL;
6135 char *id_str = NULL, *logmsg = NULL;
6137 err = got_object_open_as_commit(&commit, repo, id);
6141 err = got_object_id_str(&id_str, id);
6147 err = get_short_logmsg(&logmsg, 42, commit);
6151 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6154 got_object_commit_close(commit);
6159 static const struct got_error *
6160 show_rebase_progress(struct got_commit_object *commit,
6161 struct got_object_id *old_id, struct got_object_id *new_id)
6163 const struct got_error *err;
6164 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6166 err = got_object_id_str(&old_id_str, old_id);
6171 err = got_object_id_str(&new_id_str, new_id);
6176 old_id_str[12] = '\0';
6178 new_id_str[12] = '\0';
6180 err = get_short_logmsg(&logmsg, 42, commit);
6184 printf("%s -> %s: %s\n", old_id_str,
6185 new_id_str ? new_id_str : "no-op change", logmsg);
6193 static const struct got_error *
6194 rebase_progress(void *arg, unsigned char status, const char *path)
6196 unsigned char *rebase_status = arg;
6198 while (path[0] == '/')
6200 printf("%c %s\n", status, path);
6202 if (*rebase_status == GOT_STATUS_CONFLICT)
6204 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6205 *rebase_status = status;
6209 static const struct got_error *
6210 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6211 struct got_reference *branch, struct got_reference *new_base_branch,
6212 struct got_reference *tmp_branch, struct got_repository *repo)
6214 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6215 return got_worktree_rebase_complete(worktree, fileindex,
6216 new_base_branch, tmp_branch, branch, repo);
6219 static const struct got_error *
6220 rebase_commit(struct got_pathlist_head *merged_paths,
6221 struct got_worktree *worktree, struct got_fileindex *fileindex,
6222 struct got_reference *tmp_branch,
6223 struct got_object_id *commit_id, struct got_repository *repo)
6225 const struct got_error *error;
6226 struct got_commit_object *commit;
6227 struct got_object_id *new_commit_id;
6229 error = got_object_open_as_commit(&commit, repo, commit_id);
6233 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6234 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6236 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6238 error = show_rebase_progress(commit, commit_id, NULL);
6240 error = show_rebase_progress(commit, commit_id, new_commit_id);
6241 free(new_commit_id);
6244 got_object_commit_close(commit);
6248 struct check_path_prefix_arg {
6249 const char *path_prefix;
6254 static const struct got_error *
6255 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6256 struct got_blob_object *blob2, struct got_object_id *id1,
6257 struct got_object_id *id2, const char *path1, const char *path2,
6258 mode_t mode1, mode_t mode2, struct got_repository *repo)
6260 struct check_path_prefix_arg *a = arg;
6262 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6263 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6264 return got_error(a->errcode);
6269 static const struct got_error *
6270 check_path_prefix(struct got_object_id *parent_id,
6271 struct got_object_id *commit_id, const char *path_prefix,
6272 int errcode, struct got_repository *repo)
6274 const struct got_error *err;
6275 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6276 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6277 struct check_path_prefix_arg cpp_arg;
6279 if (got_path_is_root_dir(path_prefix))
6282 err = got_object_open_as_commit(&commit, repo, commit_id);
6286 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6290 err = got_object_open_as_tree(&tree1, repo,
6291 got_object_commit_get_tree_id(parent_commit));
6295 err = got_object_open_as_tree(&tree2, repo,
6296 got_object_commit_get_tree_id(commit));
6300 cpp_arg.path_prefix = path_prefix;
6301 while (cpp_arg.path_prefix[0] == '/')
6302 cpp_arg.path_prefix++;
6303 cpp_arg.len = strlen(cpp_arg.path_prefix);
6304 cpp_arg.errcode = errcode;
6305 err = got_diff_tree(tree1, tree2, "", "", repo,
6306 check_path_prefix_in_diff, &cpp_arg, 0);
6309 got_object_tree_close(tree1);
6311 got_object_tree_close(tree2);
6313 got_object_commit_close(commit);
6315 got_object_commit_close(parent_commit);
6319 static const struct got_error *
6320 collect_commits(struct got_object_id_queue *commits,
6321 struct got_object_id *initial_commit_id,
6322 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6323 const char *path_prefix, int path_prefix_errcode,
6324 struct got_repository *repo)
6326 const struct got_error *err = NULL;
6327 struct got_commit_graph *graph = NULL;
6328 struct got_object_id *parent_id = NULL;
6329 struct got_object_qid *qid;
6330 struct got_object_id *commit_id = initial_commit_id;
6332 err = got_commit_graph_open(&graph, "/", 1);
6336 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6337 check_cancelled, NULL);
6340 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6341 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6342 check_cancelled, NULL);
6344 if (err->code == GOT_ERR_ITER_COMPLETED) {
6345 err = got_error_msg(GOT_ERR_ANCESTRY,
6346 "ran out of commits to rebase before "
6347 "youngest common ancestor commit has "
6352 err = check_path_prefix(parent_id, commit_id,
6353 path_prefix, path_prefix_errcode, repo);
6357 err = got_object_qid_alloc(&qid, commit_id);
6360 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6361 commit_id = parent_id;
6365 got_commit_graph_close(graph);
6369 static const struct got_error *
6370 cmd_rebase(int argc, char *argv[])
6372 const struct got_error *error = NULL;
6373 struct got_worktree *worktree = NULL;
6374 struct got_repository *repo = NULL;
6375 struct got_fileindex *fileindex = NULL;
6377 struct got_reference *branch = NULL;
6378 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6379 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6380 struct got_object_id *resume_commit_id = NULL;
6381 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6382 struct got_commit_object *commit = NULL;
6383 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6384 int histedit_in_progress = 0;
6385 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6386 struct got_object_id_queue commits;
6387 struct got_pathlist_head merged_paths;
6388 const struct got_object_id_queue *parent_ids;
6389 struct got_object_qid *qid, *pid;
6391 SIMPLEQ_INIT(&commits);
6392 TAILQ_INIT(&merged_paths);
6394 while ((ch = getopt(argc, argv, "ac")) != -1) {
6400 continue_rebase = 1;
6412 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6413 "unveil", NULL) == -1)
6416 if (abort_rebase && continue_rebase)
6418 else if (abort_rebase || continue_rebase) {
6421 } else if (argc != 1)
6424 cwd = getcwd(NULL, 0);
6426 error = got_error_from_errno("getcwd");
6429 error = got_worktree_open(&worktree, cwd);
6433 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6438 error = apply_unveil(got_repo_get_path(repo), 0,
6439 got_worktree_get_root_path(worktree));
6443 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6447 if (histedit_in_progress) {
6448 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6452 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6458 if (!rebase_in_progress) {
6459 error = got_error(GOT_ERR_NOT_REBASING);
6462 error = got_worktree_rebase_continue(&resume_commit_id,
6463 &new_base_branch, &tmp_branch, &branch, &fileindex,
6467 printf("Switching work tree to %s\n",
6468 got_ref_get_symref_target(new_base_branch));
6469 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6470 new_base_branch, update_progress, &did_something);
6473 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6474 goto done; /* nothing else to do */
6477 if (continue_rebase) {
6478 if (!rebase_in_progress) {
6479 error = got_error(GOT_ERR_NOT_REBASING);
6482 error = got_worktree_rebase_continue(&resume_commit_id,
6483 &new_base_branch, &tmp_branch, &branch, &fileindex,
6488 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6489 resume_commit_id, repo);
6493 yca_id = got_object_id_dup(resume_commit_id);
6494 if (yca_id == NULL) {
6495 error = got_error_from_errno("got_object_id_dup");
6499 error = got_ref_open(&branch, repo, argv[0], 0);
6504 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6508 if (!continue_rebase) {
6509 struct got_object_id *base_commit_id;
6511 base_commit_id = got_worktree_get_base_commit_id(worktree);
6512 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6513 base_commit_id, branch_head_commit_id, repo,
6514 check_cancelled, NULL);
6517 if (yca_id == NULL) {
6518 error = got_error_msg(GOT_ERR_ANCESTRY,
6519 "specified branch shares no common ancestry "
6520 "with work tree's branch");
6524 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6526 if (error->code != GOT_ERR_ANCESTRY)
6530 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6531 "specified branch resolves to a commit which "
6532 "is already contained in work tree's branch");
6535 error = got_worktree_rebase_prepare(&new_base_branch,
6536 &tmp_branch, &fileindex, worktree, branch, repo);
6541 commit_id = branch_head_commit_id;
6542 error = got_object_open_as_commit(&commit, repo, commit_id);
6546 parent_ids = got_object_commit_get_parent_ids(commit);
6547 pid = SIMPLEQ_FIRST(parent_ids);
6549 if (!continue_rebase) {
6551 error = got_worktree_rebase_abort(worktree, fileindex,
6552 repo, new_base_branch, update_progress,
6556 printf("Rebase of %s aborted\n",
6557 got_ref_get_name(branch));
6559 error = got_error(GOT_ERR_EMPTY_REBASE);
6562 error = collect_commits(&commits, commit_id, pid->id,
6563 yca_id, got_worktree_get_path_prefix(worktree),
6564 GOT_ERR_REBASE_PATH, repo);
6565 got_object_commit_close(commit);
6570 if (SIMPLEQ_EMPTY(&commits)) {
6571 if (continue_rebase) {
6572 error = rebase_complete(worktree, fileindex,
6573 branch, new_base_branch, tmp_branch, repo);
6576 /* Fast-forward the reference of the branch. */
6577 struct got_object_id *new_head_commit_id;
6579 error = got_ref_resolve(&new_head_commit_id, repo,
6583 error = got_object_id_str(&id_str, new_head_commit_id);
6584 printf("Forwarding %s to commit %s\n",
6585 got_ref_get_name(branch), id_str);
6587 error = got_ref_change_ref(branch,
6588 new_head_commit_id);
6595 SIMPLEQ_FOREACH(qid, &commits, entry) {
6596 commit_id = qid->id;
6597 parent_id = pid ? pid->id : yca_id;
6600 error = got_worktree_rebase_merge_files(&merged_paths,
6601 worktree, fileindex, parent_id, commit_id, repo,
6602 rebase_progress, &rebase_status, check_cancelled, NULL);
6606 if (rebase_status == GOT_STATUS_CONFLICT) {
6607 error = show_rebase_merge_conflict(qid->id, repo);
6610 got_worktree_rebase_pathlist_free(&merged_paths);
6614 error = rebase_commit(&merged_paths, worktree, fileindex,
6615 tmp_branch, commit_id, repo);
6616 got_worktree_rebase_pathlist_free(&merged_paths);
6621 if (rebase_status == GOT_STATUS_CONFLICT) {
6622 error = got_worktree_rebase_postpone(worktree, fileindex);
6625 error = got_error_msg(GOT_ERR_CONFLICTS,
6626 "conflicts must be resolved before rebasing can continue");
6628 error = rebase_complete(worktree, fileindex, branch,
6629 new_base_branch, tmp_branch, repo);
6631 got_object_id_queue_free(&commits);
6632 free(branch_head_commit_id);
6633 free(resume_commit_id);
6636 got_object_commit_close(commit);
6638 got_ref_close(branch);
6639 if (new_base_branch)
6640 got_ref_close(new_base_branch);
6642 got_ref_close(tmp_branch);
6644 got_worktree_close(worktree);
6646 got_repo_close(repo);
6651 usage_histedit(void)
6653 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6658 #define GOT_HISTEDIT_PICK 'p'
6659 #define GOT_HISTEDIT_EDIT 'e'
6660 #define GOT_HISTEDIT_FOLD 'f'
6661 #define GOT_HISTEDIT_DROP 'd'
6662 #define GOT_HISTEDIT_MESG 'm'
6664 static struct got_histedit_cmd {
6668 } got_histedit_cmds[] = {
6669 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6670 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6671 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6673 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6674 { GOT_HISTEDIT_MESG, "mesg",
6675 "single-line log message for commit above (open editor if empty)" },
6678 struct got_histedit_list_entry {
6679 TAILQ_ENTRY(got_histedit_list_entry) entry;
6680 struct got_object_id *commit_id;
6681 const struct got_histedit_cmd *cmd;
6684 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6686 static const struct got_error *
6687 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6688 FILE *f, struct got_repository *repo)
6690 const struct got_error *err = NULL;
6691 char *logmsg = NULL, *id_str = NULL;
6692 struct got_commit_object *commit = NULL;
6695 err = got_object_open_as_commit(&commit, repo, commit_id);
6699 err = get_short_logmsg(&logmsg, 34, commit);
6703 err = got_object_id_str(&id_str, commit_id);
6707 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6709 err = got_ferror(f, GOT_ERR_IO);
6712 got_object_commit_close(commit);
6718 static const struct got_error *
6719 histedit_write_commit_list(struct got_object_id_queue *commits,
6720 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6722 const struct got_error *err = NULL;
6723 struct got_object_qid *qid;
6725 if (SIMPLEQ_EMPTY(commits))
6726 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6728 SIMPLEQ_FOREACH(qid, commits, entry) {
6729 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6733 if (edit_logmsg_only) {
6734 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6736 err = got_ferror(f, GOT_ERR_IO);
6745 static const struct got_error *
6746 write_cmd_list(FILE *f, const char *branch_name,
6747 struct got_object_id_queue *commits)
6749 const struct got_error *err = NULL;
6752 struct got_object_qid *qid;
6754 qid = SIMPLEQ_FIRST(commits);
6755 err = got_object_id_str(&id_str, qid->id);
6760 "# Editing the history of branch '%s' starting at\n"
6762 "# Commits will be processed in order from top to "
6763 "bottom of this file.\n", branch_name, id_str);
6765 err = got_ferror(f, GOT_ERR_IO);
6769 n = fprintf(f, "# Available histedit commands:\n");
6771 err = got_ferror(f, GOT_ERR_IO);
6775 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6776 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6777 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6780 err = got_ferror(f, GOT_ERR_IO);
6789 static const struct got_error *
6790 histedit_syntax_error(int lineno)
6792 static char msg[42];
6795 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6797 if (ret == -1 || ret >= sizeof(msg))
6798 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6800 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6803 static const struct got_error *
6804 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6805 char *logmsg, struct got_repository *repo)
6807 const struct got_error *err;
6808 struct got_commit_object *folded_commit = NULL;
6809 char *id_str, *folded_logmsg = NULL;
6811 err = got_object_id_str(&id_str, hle->commit_id);
6815 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6819 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6822 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6823 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6824 folded_logmsg) == -1) {
6825 err = got_error_from_errno("asprintf");
6829 got_object_commit_close(folded_commit);
6831 free(folded_logmsg);
6835 static struct got_histedit_list_entry *
6836 get_folded_commits(struct got_histedit_list_entry *hle)
6838 struct got_histedit_list_entry *prev, *folded = NULL;
6840 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6841 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6842 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6843 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6845 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6851 static const struct got_error *
6852 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6853 struct got_repository *repo)
6855 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6856 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6857 const struct got_error *err = NULL;
6858 struct got_commit_object *commit = NULL;
6860 struct got_histedit_list_entry *folded = NULL;
6862 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6866 folded = get_folded_commits(hle);
6868 while (folded != hle) {
6869 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6870 folded = TAILQ_NEXT(folded, entry);
6873 err = append_folded_commit_msg(&new_msg, folded,
6879 folded = TAILQ_NEXT(folded, entry);
6883 err = got_object_id_str(&id_str, hle->commit_id);
6886 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6889 if (asprintf(&new_msg,
6890 "%s\n# original log message of commit %s: %s",
6891 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6892 err = got_error_from_errno("asprintf");
6898 err = got_object_id_str(&id_str, hle->commit_id);
6902 err = got_opentemp_named_fd(&logmsg_path, &fd,
6903 GOT_TMPDIR_STR "/got-logmsg");
6907 dprintf(fd, logmsg);
6910 err = get_editor(&editor);
6914 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6916 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6918 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6921 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6922 err = got_error_from_errno2("unlink", logmsg_path);
6928 got_object_commit_close(commit);
6932 static const struct got_error *
6933 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6934 FILE *f, struct got_repository *repo)
6936 const struct got_error *err = NULL;
6937 char *line = NULL, *p, *end;
6941 const struct got_histedit_cmd *cmd;
6942 struct got_object_id *commit_id = NULL;
6943 struct got_histedit_list_entry *hle = NULL;
6946 len = getline(&line, &size, f);
6948 const struct got_error *getline_err;
6951 getline_err = got_error_from_errno("getline");
6952 err = got_ferror(f, getline_err->code);
6957 while (isspace((unsigned char)p[0]))
6959 if (p[0] == '#' || p[0] == '\0') {
6965 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6966 cmd = &got_histedit_cmds[i];
6967 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6968 isspace((unsigned char)p[strlen(cmd->name)])) {
6969 p += strlen(cmd->name);
6972 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6977 if (i == nitems(got_histedit_cmds)) {
6978 err = histedit_syntax_error(lineno);
6981 while (isspace((unsigned char)p[0]))
6983 if (cmd->code == GOT_HISTEDIT_MESG) {
6984 if (hle == NULL || hle->logmsg != NULL) {
6985 err = got_error(GOT_ERR_HISTEDIT_CMD);
6989 err = histedit_edit_logmsg(hle, repo);
6993 hle->logmsg = strdup(p);
6994 if (hle->logmsg == NULL) {
6995 err = got_error_from_errno("strdup");
7004 while (end[0] && !isspace((unsigned char)end[0]))
7008 err = got_object_resolve_id_str(&commit_id, repo, p);
7010 /* override error code */
7011 err = histedit_syntax_error(lineno);
7015 hle = malloc(sizeof(*hle));
7017 err = got_error_from_errno("malloc");
7021 hle->commit_id = commit_id;
7026 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7034 static const struct got_error *
7035 histedit_check_script(struct got_histedit_list *histedit_cmds,
7036 struct got_object_id_queue *commits, struct got_repository *repo)
7038 const struct got_error *err = NULL;
7039 struct got_object_qid *qid;
7040 struct got_histedit_list_entry *hle;
7041 static char msg[92];
7044 if (TAILQ_EMPTY(histedit_cmds))
7045 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7046 "histedit script contains no commands");
7047 if (SIMPLEQ_EMPTY(commits))
7048 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7050 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7051 struct got_histedit_list_entry *hle2;
7052 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7055 if (got_object_id_cmp(hle->commit_id,
7056 hle2->commit_id) != 0)
7058 err = got_object_id_str(&id_str, hle->commit_id);
7061 snprintf(msg, sizeof(msg), "commit %s is listed "
7062 "more than once in histedit script", id_str);
7064 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7068 SIMPLEQ_FOREACH(qid, commits, entry) {
7069 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7070 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7074 err = got_object_id_str(&id_str, qid->id);
7077 snprintf(msg, sizeof(msg),
7078 "commit %s missing from histedit script", id_str);
7080 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7084 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7085 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7086 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7087 "last commit in histedit script cannot be folded");
7092 static const struct got_error *
7093 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7094 const char *path, struct got_object_id_queue *commits,
7095 struct got_repository *repo)
7097 const struct got_error *err = NULL;
7101 err = get_editor(&editor);
7105 if (spawn_editor(editor, path) == -1) {
7106 err = got_error_from_errno("failed spawning editor");
7110 f = fopen(path, "r");
7112 err = got_error_from_errno("fopen");
7115 err = histedit_parse_list(histedit_cmds, f, repo);
7119 err = histedit_check_script(histedit_cmds, commits, repo);
7121 if (f && fclose(f) != 0 && err == NULL)
7122 err = got_error_from_errno("fclose");
7127 static const struct got_error *
7128 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7129 struct got_object_id_queue *, const char *, const char *,
7130 struct got_repository *);
7132 static const struct got_error *
7133 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7134 struct got_object_id_queue *commits, const char *branch_name,
7135 int edit_logmsg_only, struct got_repository *repo)
7137 const struct got_error *err;
7141 err = got_opentemp_named(&path, &f, "got-histedit");
7145 err = write_cmd_list(f, branch_name, commits);
7149 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7153 if (edit_logmsg_only) {
7155 err = histedit_parse_list(histedit_cmds, f, repo);
7157 if (fclose(f) != 0) {
7158 err = got_error_from_errno("fclose");
7162 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7164 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7165 err->code != GOT_ERR_HISTEDIT_CMD)
7167 err = histedit_edit_list_retry(histedit_cmds, err,
7168 commits, path, branch_name, repo);
7172 if (f && fclose(f) != 0 && err == NULL)
7173 err = got_error_from_errno("fclose");
7174 if (path && unlink(path) != 0 && err == NULL)
7175 err = got_error_from_errno2("unlink", path);
7180 static const struct got_error *
7181 histedit_save_list(struct got_histedit_list *histedit_cmds,
7182 struct got_worktree *worktree, struct got_repository *repo)
7184 const struct got_error *err = NULL;
7187 struct got_histedit_list_entry *hle;
7188 struct got_commit_object *commit = NULL;
7190 err = got_worktree_get_histedit_script_path(&path, worktree);
7194 f = fopen(path, "w");
7196 err = got_error_from_errno2("fopen", path);
7199 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7200 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7206 int n = fprintf(f, "%c %s\n",
7207 GOT_HISTEDIT_MESG, hle->logmsg);
7209 err = got_ferror(f, GOT_ERR_IO);
7215 if (f && fclose(f) != 0 && err == NULL)
7216 err = got_error_from_errno("fclose");
7219 got_object_commit_close(commit);
7224 histedit_free_list(struct got_histedit_list *histedit_cmds)
7226 struct got_histedit_list_entry *hle;
7228 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7229 TAILQ_REMOVE(histedit_cmds, hle, entry);
7234 static const struct got_error *
7235 histedit_load_list(struct got_histedit_list *histedit_cmds,
7236 const char *path, struct got_repository *repo)
7238 const struct got_error *err = NULL;
7241 f = fopen(path, "r");
7243 err = got_error_from_errno2("fopen", path);
7247 err = histedit_parse_list(histedit_cmds, f, repo);
7249 if (f && fclose(f) != 0 && err == NULL)
7250 err = got_error_from_errno("fclose");
7254 static const struct got_error *
7255 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7256 const struct got_error *edit_err, struct got_object_id_queue *commits,
7257 const char *path, const char *branch_name, struct got_repository *repo)
7259 const struct got_error *err = NULL, *prev_err = edit_err;
7262 while (resp != 'c' && resp != 'r' && resp != 'a') {
7263 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7264 "or (a)bort: ", getprogname(), prev_err->msg);
7269 histedit_free_list(histedit_cmds);
7270 err = histedit_run_editor(histedit_cmds, path, commits,
7273 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7274 err->code != GOT_ERR_HISTEDIT_CMD)
7281 } else if (resp == 'r') {
7282 histedit_free_list(histedit_cmds);
7283 err = histedit_edit_script(histedit_cmds,
7284 commits, branch_name, 0, repo);
7286 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7287 err->code != GOT_ERR_HISTEDIT_CMD)
7294 } else if (resp == 'a') {
7295 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7298 printf("invalid response '%c'\n", resp);
7304 static const struct got_error *
7305 histedit_complete(struct got_worktree *worktree,
7306 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7307 struct got_reference *branch, struct got_repository *repo)
7309 printf("Switching work tree to %s\n",
7310 got_ref_get_symref_target(branch));
7311 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7315 static const struct got_error *
7316 show_histedit_progress(struct got_commit_object *commit,
7317 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7319 const struct got_error *err;
7320 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7322 err = got_object_id_str(&old_id_str, hle->commit_id);
7327 err = got_object_id_str(&new_id_str, new_id);
7332 old_id_str[12] = '\0';
7334 new_id_str[12] = '\0';
7337 logmsg = strdup(hle->logmsg);
7338 if (logmsg == NULL) {
7339 err = got_error_from_errno("strdup");
7342 trim_logmsg(logmsg, 42);
7344 err = get_short_logmsg(&logmsg, 42, commit);
7349 switch (hle->cmd->code) {
7350 case GOT_HISTEDIT_PICK:
7351 case GOT_HISTEDIT_EDIT:
7352 printf("%s -> %s: %s\n", old_id_str,
7353 new_id_str ? new_id_str : "no-op change", logmsg);
7355 case GOT_HISTEDIT_DROP:
7356 case GOT_HISTEDIT_FOLD:
7357 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7369 static const struct got_error *
7370 histedit_commit(struct got_pathlist_head *merged_paths,
7371 struct got_worktree *worktree, struct got_fileindex *fileindex,
7372 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7373 struct got_repository *repo)
7375 const struct got_error *err;
7376 struct got_commit_object *commit;
7377 struct got_object_id *new_commit_id;
7379 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7380 && hle->logmsg == NULL) {
7381 err = histedit_edit_logmsg(hle, repo);
7386 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7390 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7391 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7394 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7396 err = show_histedit_progress(commit, hle, NULL);
7398 err = show_histedit_progress(commit, hle, new_commit_id);
7399 free(new_commit_id);
7402 got_object_commit_close(commit);
7406 static const struct got_error *
7407 histedit_skip_commit(struct got_histedit_list_entry *hle,
7408 struct got_worktree *worktree, struct got_repository *repo)
7410 const struct got_error *error;
7411 struct got_commit_object *commit;
7413 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7418 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7422 error = show_histedit_progress(commit, hle, NULL);
7423 got_object_commit_close(commit);
7427 static const struct got_error *
7428 check_local_changes(void *arg, unsigned char status,
7429 unsigned char staged_status, const char *path,
7430 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7431 struct got_object_id *commit_id, int dirfd, const char *de_name)
7433 int *have_local_changes = arg;
7436 case GOT_STATUS_ADD:
7437 case GOT_STATUS_DELETE:
7438 case GOT_STATUS_MODIFY:
7439 case GOT_STATUS_CONFLICT:
7440 *have_local_changes = 1;
7441 return got_error(GOT_ERR_CANCELLED);
7446 switch (staged_status) {
7447 case GOT_STATUS_ADD:
7448 case GOT_STATUS_DELETE:
7449 case GOT_STATUS_MODIFY:
7450 *have_local_changes = 1;
7451 return got_error(GOT_ERR_CANCELLED);
7459 static const struct got_error *
7460 cmd_histedit(int argc, char *argv[])
7462 const struct got_error *error = NULL;
7463 struct got_worktree *worktree = NULL;
7464 struct got_fileindex *fileindex = NULL;
7465 struct got_repository *repo = NULL;
7467 struct got_reference *branch = NULL;
7468 struct got_reference *tmp_branch = NULL;
7469 struct got_object_id *resume_commit_id = NULL;
7470 struct got_object_id *base_commit_id = NULL;
7471 struct got_object_id *head_commit_id = NULL;
7472 struct got_commit_object *commit = NULL;
7473 int ch, rebase_in_progress = 0, did_something;
7474 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7475 int edit_logmsg_only = 0;
7476 const char *edit_script_path = NULL;
7477 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7478 struct got_object_id_queue commits;
7479 struct got_pathlist_head merged_paths;
7480 const struct got_object_id_queue *parent_ids;
7481 struct got_object_qid *pid;
7482 struct got_histedit_list histedit_cmds;
7483 struct got_histedit_list_entry *hle;
7485 SIMPLEQ_INIT(&commits);
7486 TAILQ_INIT(&histedit_cmds);
7487 TAILQ_INIT(&merged_paths);
7489 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7498 edit_script_path = optarg;
7501 edit_logmsg_only = 1;
7513 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7514 "unveil", NULL) == -1)
7517 if (abort_edit && continue_edit)
7518 errx(1, "histedit's -a and -c options are mutually exclusive");
7519 if (edit_script_path && edit_logmsg_only)
7520 errx(1, "histedit's -F and -m options are mutually exclusive");
7521 if (abort_edit && edit_logmsg_only)
7522 errx(1, "histedit's -a and -m options are mutually exclusive");
7523 if (continue_edit && edit_logmsg_only)
7524 errx(1, "histedit's -c and -m options are mutually exclusive");
7529 * This command cannot apply unveil(2) in all cases because the
7530 * user may choose to run an editor to edit the histedit script
7531 * and to edit individual commit log messages.
7532 * unveil(2) traverses exec(2); if an editor is used we have to
7533 * apply unveil after edit script and log messages have been written.
7534 * XXX TODO: Make use of unveil(2) where possible.
7537 cwd = getcwd(NULL, 0);
7539 error = got_error_from_errno("getcwd");
7542 error = got_worktree_open(&worktree, cwd);
7546 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7551 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7554 if (rebase_in_progress) {
7555 error = got_error(GOT_ERR_REBASING);
7559 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7563 if (edit_in_progress && edit_logmsg_only) {
7564 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7565 "histedit operation is in progress in this "
7566 "work tree and must be continued or aborted "
7567 "before the -m option can be used");
7571 if (edit_in_progress && abort_edit) {
7572 error = got_worktree_histedit_continue(&resume_commit_id,
7573 &tmp_branch, &branch, &base_commit_id, &fileindex,
7577 printf("Switching work tree to %s\n",
7578 got_ref_get_symref_target(branch));
7579 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7580 branch, base_commit_id, update_progress, &did_something);
7583 printf("Histedit of %s aborted\n",
7584 got_ref_get_symref_target(branch));
7585 goto done; /* nothing else to do */
7586 } else if (abort_edit) {
7587 error = got_error(GOT_ERR_NOT_HISTEDIT);
7591 if (continue_edit) {
7594 if (!edit_in_progress) {
7595 error = got_error(GOT_ERR_NOT_HISTEDIT);
7599 error = got_worktree_get_histedit_script_path(&path, worktree);
7603 error = histedit_load_list(&histedit_cmds, path, repo);
7608 error = got_worktree_histedit_continue(&resume_commit_id,
7609 &tmp_branch, &branch, &base_commit_id, &fileindex,
7614 error = got_ref_resolve(&head_commit_id, repo, branch);
7618 error = got_object_open_as_commit(&commit, repo,
7622 parent_ids = got_object_commit_get_parent_ids(commit);
7623 pid = SIMPLEQ_FIRST(parent_ids);
7625 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7628 error = collect_commits(&commits, head_commit_id, pid->id,
7629 base_commit_id, got_worktree_get_path_prefix(worktree),
7630 GOT_ERR_HISTEDIT_PATH, repo);
7631 got_object_commit_close(commit);
7636 if (edit_in_progress) {
7637 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7641 error = got_ref_open(&branch, repo,
7642 got_worktree_get_head_ref_name(worktree), 0);
7646 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7647 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7648 "will not edit commit history of a branch outside "
7649 "the \"refs/heads/\" reference namespace");
7653 error = got_ref_resolve(&head_commit_id, repo, branch);
7654 got_ref_close(branch);
7659 error = got_object_open_as_commit(&commit, repo,
7663 parent_ids = got_object_commit_get_parent_ids(commit);
7664 pid = SIMPLEQ_FIRST(parent_ids);
7666 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7669 error = collect_commits(&commits, head_commit_id, pid->id,
7670 got_worktree_get_base_commit_id(worktree),
7671 got_worktree_get_path_prefix(worktree),
7672 GOT_ERR_HISTEDIT_PATH, repo);
7673 got_object_commit_close(commit);
7678 if (SIMPLEQ_EMPTY(&commits)) {
7679 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7683 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7684 &base_commit_id, &fileindex, worktree, repo);
7688 if (edit_script_path) {
7689 error = histedit_load_list(&histedit_cmds,
7690 edit_script_path, repo);
7692 got_worktree_histedit_abort(worktree, fileindex,
7693 repo, branch, base_commit_id,
7694 update_progress, &did_something);
7698 const char *branch_name;
7699 branch_name = got_ref_get_symref_target(branch);
7700 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7702 error = histedit_edit_script(&histedit_cmds, &commits,
7703 branch_name, edit_logmsg_only, repo);
7705 got_worktree_histedit_abort(worktree, fileindex,
7706 repo, branch, base_commit_id,
7707 update_progress, &did_something);
7713 error = histedit_save_list(&histedit_cmds, worktree,
7716 got_worktree_histedit_abort(worktree, fileindex,
7717 repo, branch, base_commit_id,
7718 update_progress, &did_something);
7724 error = histedit_check_script(&histedit_cmds, &commits, repo);
7728 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7729 if (resume_commit_id) {
7730 if (got_object_id_cmp(hle->commit_id,
7731 resume_commit_id) != 0)
7734 resume_commit_id = NULL;
7735 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7736 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7737 error = histedit_skip_commit(hle, worktree,
7742 struct got_pathlist_head paths;
7743 int have_changes = 0;
7746 error = got_pathlist_append(&paths, "", NULL);
7749 error = got_worktree_status(worktree, &paths,
7750 repo, check_local_changes, &have_changes,
7751 check_cancelled, NULL);
7752 got_pathlist_free(&paths);
7754 if (error->code != GOT_ERR_CANCELLED)
7756 if (sigint_received || sigpipe_received)
7760 error = histedit_commit(NULL, worktree,
7761 fileindex, tmp_branch, hle, repo);
7765 error = got_object_open_as_commit(
7766 &commit, repo, hle->commit_id);
7769 error = show_histedit_progress(commit,
7771 got_object_commit_close(commit);
7780 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7781 error = histedit_skip_commit(hle, worktree, repo);
7787 error = got_object_open_as_commit(&commit, repo,
7791 parent_ids = got_object_commit_get_parent_ids(commit);
7792 pid = SIMPLEQ_FIRST(parent_ids);
7794 error = got_worktree_histedit_merge_files(&merged_paths,
7795 worktree, fileindex, pid->id, hle->commit_id, repo,
7796 rebase_progress, &rebase_status, check_cancelled, NULL);
7799 got_object_commit_close(commit);
7802 if (rebase_status == GOT_STATUS_CONFLICT) {
7803 error = show_rebase_merge_conflict(hle->commit_id,
7807 got_worktree_rebase_pathlist_free(&merged_paths);
7811 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7813 error = got_object_id_str(&id_str, hle->commit_id);
7816 printf("Stopping histedit for amending commit %s\n",
7819 got_worktree_rebase_pathlist_free(&merged_paths);
7820 error = got_worktree_histedit_postpone(worktree,
7825 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7826 error = histedit_skip_commit(hle, worktree, repo);
7832 error = histedit_commit(&merged_paths, worktree, fileindex,
7833 tmp_branch, hle, repo);
7834 got_worktree_rebase_pathlist_free(&merged_paths);
7839 if (rebase_status == GOT_STATUS_CONFLICT) {
7840 error = got_worktree_histedit_postpone(worktree, fileindex);
7843 error = got_error_msg(GOT_ERR_CONFLICTS,
7844 "conflicts must be resolved before histedit can continue");
7846 error = histedit_complete(worktree, fileindex, tmp_branch,
7849 got_object_id_queue_free(&commits);
7850 histedit_free_list(&histedit_cmds);
7851 free(head_commit_id);
7852 free(base_commit_id);
7853 free(resume_commit_id);
7855 got_object_commit_close(commit);
7857 got_ref_close(branch);
7859 got_ref_close(tmp_branch);
7861 got_worktree_close(worktree);
7863 got_repo_close(repo);
7868 usage_integrate(void)
7870 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7874 static const struct got_error *
7875 cmd_integrate(int argc, char *argv[])
7877 const struct got_error *error = NULL;
7878 struct got_repository *repo = NULL;
7879 struct got_worktree *worktree = NULL;
7880 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7881 const char *branch_arg = NULL;
7882 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7883 struct got_fileindex *fileindex = NULL;
7884 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7885 int ch, did_something = 0;
7887 while ((ch = getopt(argc, argv, "")) != -1) {
7900 branch_arg = argv[0];
7902 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7903 "unveil", NULL) == -1)
7906 cwd = getcwd(NULL, 0);
7908 error = got_error_from_errno("getcwd");
7912 error = got_worktree_open(&worktree, cwd);
7916 error = check_rebase_or_histedit_in_progress(worktree);
7920 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7925 error = apply_unveil(got_repo_get_path(repo), 0,
7926 got_worktree_get_root_path(worktree));
7930 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7931 error = got_error_from_errno("asprintf");
7935 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7936 &base_branch_ref, worktree, refname, repo);
7940 refname = strdup(got_ref_get_name(branch_ref));
7941 if (refname == NULL) {
7942 error = got_error_from_errno("strdup");
7943 got_worktree_integrate_abort(worktree, fileindex, repo,
7944 branch_ref, base_branch_ref);
7947 base_refname = strdup(got_ref_get_name(base_branch_ref));
7948 if (base_refname == NULL) {
7949 error = got_error_from_errno("strdup");
7950 got_worktree_integrate_abort(worktree, fileindex, repo,
7951 branch_ref, base_branch_ref);
7955 error = got_ref_resolve(&commit_id, repo, branch_ref);
7959 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7963 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7964 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7965 "specified branch has already been integrated");
7966 got_worktree_integrate_abort(worktree, fileindex, repo,
7967 branch_ref, base_branch_ref);
7971 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7973 if (error->code == GOT_ERR_ANCESTRY)
7974 error = got_error(GOT_ERR_REBASE_REQUIRED);
7975 got_worktree_integrate_abort(worktree, fileindex, repo,
7976 branch_ref, base_branch_ref);
7980 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7981 branch_ref, base_branch_ref, update_progress, &did_something,
7982 check_cancelled, NULL);
7986 printf("Integrated %s into %s\n", refname, base_refname);
7989 got_repo_close(repo);
7991 got_worktree_close(worktree);
7993 free(base_commit_id);
8003 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8004 "[file-path ...]\n",
8009 static const struct got_error *
8010 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8011 const char *path, struct got_object_id *blob_id,
8012 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8013 int dirfd, const char *de_name)
8015 const struct got_error *err = NULL;
8016 char *id_str = NULL;
8018 if (staged_status != GOT_STATUS_ADD &&
8019 staged_status != GOT_STATUS_MODIFY &&
8020 staged_status != GOT_STATUS_DELETE)
8023 if (staged_status == GOT_STATUS_ADD ||
8024 staged_status == GOT_STATUS_MODIFY)
8025 err = got_object_id_str(&id_str, staged_blob_id);
8027 err = got_object_id_str(&id_str, blob_id);
8031 printf("%s %c %s\n", id_str, staged_status, path);
8036 static const struct got_error *
8037 cmd_stage(int argc, char *argv[])
8039 const struct got_error *error = NULL;
8040 struct got_repository *repo = NULL;
8041 struct got_worktree *worktree = NULL;
8043 struct got_pathlist_head paths;
8044 struct got_pathlist_entry *pe;
8045 int ch, list_stage = 0, pflag = 0;
8046 FILE *patch_script_file = NULL;
8047 const char *patch_script_path = NULL;
8048 struct choose_patch_arg cpa;
8052 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8061 patch_script_path = optarg;
8073 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8074 "unveil", NULL) == -1)
8077 if (list_stage && (pflag || patch_script_path))
8078 errx(1, "-l option cannot be used with other options");
8079 if (patch_script_path && !pflag)
8080 errx(1, "-F option can only be used together with -p option");
8082 cwd = getcwd(NULL, 0);
8084 error = got_error_from_errno("getcwd");
8088 error = got_worktree_open(&worktree, cwd);
8092 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8097 if (patch_script_path) {
8098 patch_script_file = fopen(patch_script_path, "r");
8099 if (patch_script_file == NULL) {
8100 error = got_error_from_errno2("fopen",
8105 error = apply_unveil(got_repo_get_path(repo), 0,
8106 got_worktree_get_root_path(worktree));
8110 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8115 error = got_worktree_status(worktree, &paths, repo,
8116 print_stage, NULL, check_cancelled, NULL);
8118 cpa.patch_script_file = patch_script_file;
8119 cpa.action = "stage";
8120 error = got_worktree_stage(worktree, &paths,
8121 pflag ? NULL : print_status, NULL,
8122 pflag ? choose_patch : NULL, &cpa, repo);
8125 if (patch_script_file && fclose(patch_script_file) == EOF &&
8127 error = got_error_from_errno2("fclose", patch_script_path);
8129 got_repo_close(repo);
8131 got_worktree_close(worktree);
8132 TAILQ_FOREACH(pe, &paths, entry)
8133 free((char *)pe->path);
8134 got_pathlist_free(&paths);
8142 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8143 "[file-path ...]\n",
8149 static const struct got_error *
8150 cmd_unstage(int argc, char *argv[])
8152 const struct got_error *error = NULL;
8153 struct got_repository *repo = NULL;
8154 struct got_worktree *worktree = NULL;
8156 struct got_pathlist_head paths;
8157 struct got_pathlist_entry *pe;
8158 int ch, did_something = 0, pflag = 0;
8159 FILE *patch_script_file = NULL;
8160 const char *patch_script_path = NULL;
8161 struct choose_patch_arg cpa;
8165 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8171 patch_script_path = optarg;
8183 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8184 "unveil", NULL) == -1)
8187 if (patch_script_path && !pflag)
8188 errx(1, "-F option can only be used together with -p option");
8190 cwd = getcwd(NULL, 0);
8192 error = got_error_from_errno("getcwd");
8196 error = got_worktree_open(&worktree, cwd);
8200 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8205 if (patch_script_path) {
8206 patch_script_file = fopen(patch_script_path, "r");
8207 if (patch_script_file == NULL) {
8208 error = got_error_from_errno2("fopen",
8214 error = apply_unveil(got_repo_get_path(repo), 0,
8215 got_worktree_get_root_path(worktree));
8219 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8223 cpa.patch_script_file = patch_script_file;
8224 cpa.action = "unstage";
8225 error = got_worktree_unstage(worktree, &paths, update_progress,
8226 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8228 if (patch_script_file && fclose(patch_script_file) == EOF &&
8230 error = got_error_from_errno2("fclose", patch_script_path);
8232 got_repo_close(repo);
8234 got_worktree_close(worktree);
8235 TAILQ_FOREACH(pe, &paths, entry)
8236 free((char *)pe->path);
8237 got_pathlist_free(&paths);
8245 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8246 "arg1 [arg2 ...]\n", getprogname());
8250 static const struct got_error *
8251 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8253 const struct got_error *err;
8254 struct got_blob_object *blob;
8256 err = got_object_open_as_blob(&blob, repo, id, 8192);
8260 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8261 got_object_blob_close(blob);
8265 static const struct got_error *
8266 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8268 const struct got_error *err;
8269 struct got_tree_object *tree;
8272 err = got_object_open_as_tree(&tree, repo, id);
8276 nentries = got_object_tree_get_nentries(tree);
8277 for (i = 0; i < nentries; i++) {
8278 struct got_tree_entry *te;
8280 if (sigint_received || sigpipe_received)
8282 te = got_object_tree_get_entry(tree, i);
8283 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8286 fprintf(outfile, "%s %.7o %s\n", id_str,
8287 got_tree_entry_get_mode(te),
8288 got_tree_entry_get_name(te));
8292 got_object_tree_close(tree);
8296 static const struct got_error *
8297 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8299 const struct got_error *err;
8300 struct got_commit_object *commit;
8301 const struct got_object_id_queue *parent_ids;
8302 struct got_object_qid *pid;
8303 char *id_str = NULL;
8304 const char *logmsg = NULL;
8306 err = got_object_open_as_commit(&commit, repo, id);
8310 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8314 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8315 parent_ids = got_object_commit_get_parent_ids(commit);
8316 fprintf(outfile, "numparents %d\n",
8317 got_object_commit_get_nparents(commit));
8318 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8320 err = got_object_id_str(&pid_str, pid->id);
8323 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8326 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8327 got_object_commit_get_author(commit),
8328 got_object_commit_get_author_time(commit));
8330 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8331 got_object_commit_get_author(commit),
8332 got_object_commit_get_committer_time(commit));
8334 logmsg = got_object_commit_get_logmsg_raw(commit);
8335 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8336 fprintf(outfile, "%s", logmsg);
8339 got_object_commit_close(commit);
8343 static const struct got_error *
8344 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8346 const struct got_error *err;
8347 struct got_tag_object *tag;
8348 char *id_str = NULL;
8349 const char *tagmsg = NULL;
8351 err = got_object_open_as_tag(&tag, repo, id);
8355 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8359 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8361 switch (got_object_tag_get_object_type(tag)) {
8362 case GOT_OBJ_TYPE_BLOB:
8363 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8364 GOT_OBJ_LABEL_BLOB);
8366 case GOT_OBJ_TYPE_TREE:
8367 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8368 GOT_OBJ_LABEL_TREE);
8370 case GOT_OBJ_TYPE_COMMIT:
8371 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8372 GOT_OBJ_LABEL_COMMIT);
8374 case GOT_OBJ_TYPE_TAG:
8375 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8382 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8383 got_object_tag_get_name(tag));
8385 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8386 got_object_tag_get_tagger(tag),
8387 got_object_tag_get_tagger_time(tag));
8389 tagmsg = got_object_tag_get_message(tag);
8390 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8391 fprintf(outfile, "%s", tagmsg);
8394 got_object_tag_close(tag);
8398 static const struct got_error *
8399 cmd_cat(int argc, char *argv[])
8401 const struct got_error *error;
8402 struct got_repository *repo = NULL;
8403 struct got_worktree *worktree = NULL;
8404 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8405 const char *commit_id_str = NULL;
8406 struct got_object_id *id = NULL, *commit_id = NULL;
8407 int ch, obj_type, i, force_path = 0;
8410 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8415 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8418 commit_id_str = optarg;
8421 repo_path = realpath(optarg, NULL);
8422 if (repo_path == NULL)
8423 return got_error_from_errno2("realpath",
8425 got_path_strip_trailing_slashes(repo_path);
8439 cwd = getcwd(NULL, 0);
8441 error = got_error_from_errno("getcwd");
8444 error = got_worktree_open(&worktree, cwd);
8445 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8448 if (repo_path == NULL) {
8450 got_worktree_get_repo_path(worktree));
8451 if (repo_path == NULL) {
8452 error = got_error_from_errno("strdup");
8458 if (repo_path == NULL) {
8459 repo_path = getcwd(NULL, 0);
8460 if (repo_path == NULL)
8461 return got_error_from_errno("getcwd");
8464 error = got_repo_open(&repo, repo_path, NULL);
8469 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8473 if (commit_id_str == NULL)
8474 commit_id_str = GOT_REF_HEAD;
8475 error = got_repo_match_object_id(&commit_id, NULL,
8476 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8480 for (i = 0; i < argc; i++) {
8482 error = got_object_id_by_path(&id, repo, commit_id,
8487 error = got_repo_match_object_id(&id, &label, argv[i],
8488 GOT_OBJ_TYPE_ANY, 0, repo);
8490 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8491 error->code != GOT_ERR_NOT_REF)
8493 error = got_object_id_by_path(&id, repo,
8494 commit_id, argv[i]);
8500 error = got_object_get_type(&obj_type, repo, id);
8505 case GOT_OBJ_TYPE_BLOB:
8506 error = cat_blob(id, repo, stdout);
8508 case GOT_OBJ_TYPE_TREE:
8509 error = cat_tree(id, repo, stdout);
8511 case GOT_OBJ_TYPE_COMMIT:
8512 error = cat_commit(id, repo, stdout);
8514 case GOT_OBJ_TYPE_TAG:
8515 error = cat_tag(id, repo, stdout);
8518 error = got_error(GOT_ERR_OBJ_TYPE);
8533 got_worktree_close(worktree);
8535 const struct got_error *repo_error;
8536 repo_error = got_repo_close(repo);