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, struct got_repository *repo)
890 const struct got_error *err;
891 struct got_reference *head_symref;
893 err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, target_ref);
897 err = got_ref_write(head_symref, repo);
898 got_ref_close(head_symref);
902 static const struct got_error *
903 list_remote_refs(struct got_pathlist_head *symrefs,
904 struct got_pathlist_head *refs)
906 const struct got_error *err;
907 struct got_pathlist_entry *pe;
909 TAILQ_FOREACH(pe, symrefs, entry) {
910 const char *refname = pe->path;
911 const char *targetref = pe->data;
913 printf("%s: %s\n", refname, targetref);
916 TAILQ_FOREACH(pe, refs, entry) {
917 const char *refname = pe->path;
918 struct got_object_id *id = pe->data;
921 err = got_object_id_str(&id_str, id);
924 printf("%s: %s\n", refname, id_str);
931 static const struct got_error *
932 cmd_clone(int argc, char *argv[])
934 const struct got_error *error = NULL;
935 const char *uri, *dirname;
936 char *proto, *host, *port, *repo_name, *server_path;
937 char *default_destdir = NULL, *id_str = NULL;
938 const char *repo_path;
939 struct got_repository *repo = NULL;
940 struct got_pathlist_head refs, symrefs, wanted_branches;
941 struct got_pathlist_entry *pe;
942 struct got_object_id *pack_hash = NULL;
943 int ch, fetchfd = -1, fetchstatus;
945 struct got_fetch_progress_arg fpa;
946 char *git_url = NULL;
947 char *gitconfig_path = NULL;
948 char *gitconfig = NULL;
949 FILE *gitconfig_file = NULL;
951 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
952 int list_refs_only = 0;
953 struct got_reference *head_symref = NULL;
956 TAILQ_INIT(&symrefs);
957 TAILQ_INIT(&wanted_branches);
959 while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
962 fetch_all_branches = 1;
965 error = got_pathlist_append(&wanted_branches,
974 mirror_references = 1;
979 else if (verbosity < 3)
993 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
994 errx(1, "-a and -b options are mutually exclusive");
995 if (list_refs_only) {
996 if (!TAILQ_EMPTY(&wanted_branches))
997 errx(1, "-l and -b options are mutually exclusive");
998 if (fetch_all_branches)
999 errx(1, "-l and -a options are mutually exclusive");
1000 if (mirror_references)
1001 errx(1, "-l and -m options are mutually exclusive");
1002 if (verbosity == -1)
1003 errx(1, "-l and -q options are mutually exclusive");
1015 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1016 &repo_name, argv[0]);
1020 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1021 host, port ? ":" : "", port ? port : "",
1022 server_path[0] != '/' ? "/" : "", server_path) == -1) {
1023 error = got_error_from_errno("asprintf");
1027 if (strcmp(proto, "git") == 0) {
1029 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1030 "sendfd dns inet unveil", NULL) == -1)
1033 } else if (strcmp(proto, "git+ssh") == 0 ||
1034 strcmp(proto, "ssh") == 0) {
1036 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1037 "sendfd unveil", NULL) == -1)
1040 } else if (strcmp(proto, "http") == 0 ||
1041 strcmp(proto, "git+http") == 0) {
1042 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1045 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1048 if (dirname == NULL) {
1049 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1050 error = got_error_from_errno("asprintf");
1053 repo_path = default_destdir;
1055 repo_path = dirname;
1057 if (!list_refs_only) {
1058 error = got_path_mkdir(repo_path);
1062 error = got_repo_init(repo_path);
1065 error = got_repo_open(&repo, repo_path, NULL);
1070 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1071 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1072 error = got_error_from_errno2("unveil",
1073 GOT_FETCH_PATH_SSH);
1077 error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1081 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1082 server_path, verbosity);
1087 printf("Connected to %s%s%s\n", host,
1088 port ? ":" : "", port ? port : "");
1090 fpa.last_scaled_size[0] = '\0';
1091 fpa.last_p_indexed = -1;
1092 fpa.last_p_resolved = -1;
1093 fpa.verbosity = verbosity;
1094 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1095 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1096 fetch_all_branches, &wanted_branches, list_refs_only,
1097 fetchfd, repo, fetch_progress, &fpa);
1101 if (list_refs_only) {
1102 error = list_remote_refs(&symrefs, &refs);
1106 error = got_object_id_str(&id_str, pack_hash);
1110 printf("\nFetched %s.pack\n", id_str);
1113 /* Set up references provided with the pack file. */
1114 TAILQ_FOREACH(pe, &refs, entry) {
1115 const char *refname = pe->path;
1116 struct got_object_id *id = pe->data;
1117 struct got_reference *ref;
1118 char *remote_refname;
1120 error = got_ref_alloc(&ref, refname, id);
1123 error = got_ref_write(ref, repo);
1128 if (mirror_references)
1131 if (strncmp("refs/heads/", refname, 11) != 0)
1134 if (asprintf(&remote_refname,
1135 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1136 refname + 11) == -1) {
1137 error = got_error_from_errno("asprintf");
1140 error = got_ref_alloc(&ref, remote_refname, id);
1143 error = got_ref_write(ref, repo);
1149 /* Set the HEAD reference if the server provided one. */
1150 TAILQ_FOREACH(pe, &symrefs, entry) {
1151 struct got_reference *target_ref;
1152 const char *refname = pe->path;
1153 const char *target = pe->data;
1155 if (strcmp(refname, GOT_REF_HEAD) != 0)
1158 error = got_ref_open(&target_ref, repo, target, 0);
1160 if (error->code == GOT_ERR_NOT_REF) {
1168 printf("Setting %s to %s\n", refname, target);
1169 error = create_head_ref(target_ref, repo);
1170 got_ref_close(target_ref);
1176 * We failed to set the HEAD reference. If we asked for
1177 * a set of wanted branches use the first of one of those
1178 * which could be fetched instead.
1180 TAILQ_FOREACH(pe, &wanted_branches, entry) {
1181 const char *target = pe->path;
1182 struct got_reference *target_ref;
1184 error = got_ref_open(&target_ref, repo, target, 0);
1186 if (error->code == GOT_ERR_NOT_REF) {
1194 printf("Setting %s to %s\n", GOT_REF_HEAD,
1195 got_ref_get_name(target_ref));
1196 error = create_head_ref(target_ref, repo);
1197 got_ref_close(target_ref);
1204 /* Create a config file git-fetch(1) can understand. */
1205 gitconfig_path = got_repo_get_path_gitconfig(repo);
1206 if (gitconfig_path == NULL) {
1207 error = got_error_from_errno("got_repo_get_path_gitconfig");
1210 gitconfig_file = fopen(gitconfig_path, "a");
1211 if (gitconfig_file == NULL) {
1212 error = got_error_from_errno2("fopen", gitconfig_path);
1215 if (mirror_references) {
1216 if (asprintf(&gitconfig,
1219 "\tfetch = +refs/*:refs/*\n"
1220 "\tmirror = true\n",
1221 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1222 error = got_error_from_errno("asprintf");
1225 } else if (fetch_all_branches) {
1226 if (asprintf(&gitconfig,
1229 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1230 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1231 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1232 error = got_error_from_errno("asprintf");
1236 const char *branchname;
1239 * If the server specified a default branch, use just that one.
1240 * Otherwise fall back to fetching all branches on next fetch.
1243 branchname = got_ref_get_symref_target(head_symref);
1244 if (strncmp(branchname, "refs/heads/", 11) == 0)
1247 branchname = "*"; /* fall back to all branches */
1248 if (asprintf(&gitconfig,
1251 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1252 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1253 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1254 branchname) == -1) {
1255 error = got_error_from_errno("asprintf");
1259 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1260 if (n != strlen(gitconfig)) {
1261 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1267 printf("Created %s repository '%s'\n",
1268 mirror_references ? "mirrored" : "cloned", repo_path);
1271 if (kill(fetchpid, SIGTERM) == -1)
1272 error = got_error_from_errno("kill");
1273 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1274 error = got_error_from_errno("waitpid");
1276 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1277 error = got_error_from_errno("close");
1278 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1279 error = got_error_from_errno("fclose");
1281 got_repo_close(repo);
1283 got_ref_close(head_symref);
1284 TAILQ_FOREACH(pe, &refs, entry) {
1285 free((void *)pe->path);
1288 got_pathlist_free(&refs);
1289 TAILQ_FOREACH(pe, &symrefs, entry) {
1290 free((void *)pe->path);
1293 got_pathlist_free(&symrefs);
1294 got_pathlist_free(&wanted_branches);
1301 free(default_destdir);
1302 free(gitconfig_path);
1307 static const struct got_error *
1308 create_ref(const char *refname, struct got_object_id *id,
1309 const char *id_str, int verbosity, struct got_repository *repo)
1311 const struct got_error *err = NULL;
1312 struct got_reference *ref;
1315 printf("Creating %s: %s\n", refname, id_str);
1317 err = got_ref_alloc(&ref, refname, id);
1321 err = got_ref_write(ref, repo);
1326 static const struct got_error *
1327 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1328 int verbosity, struct got_repository *repo)
1330 const struct got_error *err = NULL;
1331 char *new_id_str = NULL;
1332 struct got_object_id *old_id = NULL;
1334 err = got_object_id_str(&new_id_str, new_id);
1338 if (got_ref_is_symbolic(ref)) {
1339 struct got_reference *new_ref;
1340 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1343 if (verbosity >= 0) {
1344 printf("Deleting symbolic reference %s -> %s\n",
1345 got_ref_get_name(ref),
1346 got_ref_get_symref_target(ref));
1348 err = got_ref_delete(ref, repo);
1351 if (verbosity >= 0) {
1352 printf("Setting %s to %s\n", got_ref_get_name(ref),
1355 err = got_ref_write(new_ref, repo);
1359 err = got_ref_resolve(&old_id, repo, ref);
1362 if (got_object_id_cmp(old_id, new_id) != 0) {
1363 if (verbosity >= 0) {
1364 printf("Setting %s to %s\n",
1365 got_ref_get_name(ref), new_id_str);
1367 err = got_ref_change_ref(ref, new_id);
1370 err = got_ref_write(ref, repo);
1384 fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1385 "[-r repository-path] [-q] [-v] [remote-repository-name]\n",
1390 static const struct got_error *
1391 delete_missing_refs(struct got_pathlist_head *their_refs,
1392 int verbosity, struct got_repository *repo)
1394 const struct got_error *err = NULL;
1395 struct got_reflist_head my_refs;
1396 struct got_reflist_entry *re;
1397 struct got_pathlist_entry *pe;
1398 struct got_object_id *id;
1401 SIMPLEQ_INIT(&my_refs);
1403 err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1407 SIMPLEQ_FOREACH(re, &my_refs, entry) {
1408 const char *refname = got_ref_get_name(re->ref);
1410 if (strncmp(refname, "refs/heads/", 11) != 0 &&
1411 strncmp(refname, "refs/tags/", 10) != 0)
1414 TAILQ_FOREACH(pe, their_refs, entry) {
1415 if (strcmp(refname, pe->path) == 0)
1421 err = got_ref_resolve(&id, repo, re->ref);
1424 err = got_object_id_str(&id_str, id);
1429 if (verbosity >= 0) {
1430 printf("Deleting %s: %s\n",
1431 got_ref_get_name(re->ref), id_str);
1434 err = got_ref_delete(re->ref, repo);
1442 static const struct got_error *
1443 cmd_fetch(int argc, char *argv[])
1445 const struct got_error *error = NULL;
1446 char *cwd = NULL, *repo_path = NULL;
1447 const char *remote_name;
1448 char *proto = NULL, *host = NULL, *port = NULL;
1449 char *repo_name = NULL, *server_path = NULL;
1450 struct got_remote_repo *remotes, *remote = NULL;
1452 char *id_str = NULL;
1453 struct got_repository *repo = NULL;
1454 struct got_worktree *worktree = NULL;
1455 struct got_pathlist_head refs, symrefs, wanted_branches;
1456 struct got_pathlist_entry *pe;
1457 struct got_object_id *pack_hash = NULL;
1458 int i, ch, fetchfd = -1, fetchstatus;
1459 pid_t fetchpid = -1;
1460 struct got_fetch_progress_arg fpa;
1461 int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1462 int delete_refs = 0;
1465 TAILQ_INIT(&symrefs);
1466 TAILQ_INIT(&wanted_branches);
1468 while ((ch = getopt(argc, argv, "ab:dlr:vq")) != -1) {
1471 fetch_all_branches = 1;
1474 error = got_pathlist_append(&wanted_branches,
1486 repo_path = realpath(optarg, NULL);
1487 if (repo_path == NULL)
1488 return got_error_from_errno2("realpath",
1490 got_path_strip_trailing_slashes(repo_path);
1495 else if (verbosity < 3)
1509 if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1510 errx(1, "-a and -b options are mutually exclusive");
1511 if (list_refs_only) {
1512 if (!TAILQ_EMPTY(&wanted_branches))
1513 errx(1, "-l and -b options are mutually exclusive");
1514 if (fetch_all_branches)
1515 errx(1, "-l and -a options are mutually exclusive");
1517 errx(1, "-l and -d options are mutually exclusive");
1518 if (verbosity == -1)
1519 errx(1, "-l and -q options are mutually exclusive");
1523 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1525 remote_name = argv[0];
1529 cwd = getcwd(NULL, 0);
1531 error = got_error_from_errno("getcwd");
1535 if (repo_path == NULL) {
1536 error = got_worktree_open(&worktree, cwd);
1537 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1543 strdup(got_worktree_get_repo_path(worktree));
1544 if (repo_path == NULL)
1545 error = got_error_from_errno("strdup");
1549 repo_path = strdup(cwd);
1550 if (repo_path == NULL) {
1551 error = got_error_from_errno("strdup");
1557 error = got_repo_open(&repo, repo_path, NULL);
1561 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1562 for (i = 0; i < nremotes; i++) {
1563 remote = &remotes[i];
1564 if (strcmp(remote->name, remote_name) == 0)
1567 if (i == nremotes) {
1568 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1572 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1573 &repo_name, remote->url);
1577 if (strcmp(proto, "git") == 0) {
1579 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1580 "sendfd dns inet unveil", NULL) == -1)
1583 } else if (strcmp(proto, "git+ssh") == 0 ||
1584 strcmp(proto, "ssh") == 0) {
1586 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1587 "sendfd unveil", NULL) == -1)
1590 } else if (strcmp(proto, "http") == 0 ||
1591 strcmp(proto, "git+http") == 0) {
1592 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1595 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1599 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1600 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1601 error = got_error_from_errno2("unveil",
1602 GOT_FETCH_PATH_SSH);
1606 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1610 error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1611 server_path, verbosity);
1616 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1617 port ? ":" : "", port ? port : "");
1619 fpa.last_scaled_size[0] = '\0';
1620 fpa.last_p_indexed = -1;
1621 fpa.last_p_resolved = -1;
1622 fpa.verbosity = verbosity;
1623 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1624 remote->mirror_references, fetch_all_branches, &wanted_branches,
1625 list_refs_only, fetchfd, repo, fetch_progress, &fpa);
1629 if (list_refs_only) {
1630 error = list_remote_refs(&symrefs, &refs);
1634 if (pack_hash == NULL) {
1636 printf("Already up-to-date\n");
1638 error = delete_missing_refs(&refs, verbosity, repo);
1642 if (verbosity >= 0) {
1643 error = got_object_id_str(&id_str, pack_hash);
1646 printf("\nFetched %s.pack\n", id_str);
1651 /* Update references provided with the pack file. */
1652 TAILQ_FOREACH(pe, &refs, entry) {
1653 const char *refname = pe->path;
1654 struct got_object_id *id = pe->data;
1655 struct got_reference *ref;
1656 char *remote_refname;
1658 error = got_object_id_str(&id_str, id);
1662 if (remote->mirror_references ||
1663 strncmp("refs/tags/", refname, 10) == 0) {
1664 error = got_ref_open(&ref, repo, refname, 0);
1666 if (error->code != GOT_ERR_NOT_REF)
1668 error = create_ref(refname, id, id_str,
1673 error = update_ref(ref, id, verbosity, repo);
1678 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1679 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1680 remote_name, refname + 11) == -1) {
1681 error = got_error_from_errno("asprintf");
1685 error = got_ref_open(&ref, repo, remote_refname, 0);
1687 if (error->code != GOT_ERR_NOT_REF)
1689 error = create_ref(remote_refname, id, id_str,
1694 error = update_ref(ref, id, verbosity, repo);
1700 /* Also create a local branch if none exists yet. */
1701 error = got_ref_open(&ref, repo, refname, 0);
1703 if (error->code != GOT_ERR_NOT_REF)
1705 error = create_ref(refname, id, id_str,
1716 error = delete_missing_refs(&refs, verbosity, repo);
1719 if (kill(fetchpid, SIGTERM) == -1)
1720 error = got_error_from_errno("kill");
1721 if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1722 error = got_error_from_errno("waitpid");
1724 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1725 error = got_error_from_errno("close");
1727 got_repo_close(repo);
1729 got_worktree_close(worktree);
1730 TAILQ_FOREACH(pe, &refs, entry) {
1731 free((void *)pe->path);
1734 got_pathlist_free(&refs);
1735 TAILQ_FOREACH(pe, &symrefs, entry) {
1736 free((void *)pe->path);
1739 got_pathlist_free(&symrefs);
1740 got_pathlist_free(&wanted_branches);
1755 usage_checkout(void)
1757 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1758 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1763 show_worktree_base_ref_warning(void)
1765 fprintf(stderr, "%s: warning: could not create a reference "
1766 "to the work tree's base commit; the commit could be "
1767 "garbage-collected by Git; making the repository "
1768 "writable and running 'got update' will prevent this\n",
1772 struct got_checkout_progress_arg {
1773 const char *worktree_path;
1774 int had_base_commit_ref_error;
1777 static const struct got_error *
1778 checkout_progress(void *arg, unsigned char status, const char *path)
1780 struct got_checkout_progress_arg *a = arg;
1782 /* Base commit bump happens silently. */
1783 if (status == GOT_STATUS_BUMP_BASE)
1786 if (status == GOT_STATUS_BASE_REF_ERR) {
1787 a->had_base_commit_ref_error = 1;
1791 while (path[0] == '/')
1794 printf("%c %s/%s\n", status, a->worktree_path, path);
1798 static const struct got_error *
1799 check_cancelled(void *arg)
1801 if (sigint_received || sigpipe_received)
1802 return got_error(GOT_ERR_CANCELLED);
1806 static const struct got_error *
1807 check_linear_ancestry(struct got_object_id *commit_id,
1808 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1809 struct got_repository *repo)
1811 const struct got_error *err = NULL;
1812 struct got_object_id *yca_id;
1814 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1815 commit_id, base_commit_id, repo, check_cancelled, NULL);
1820 return got_error(GOT_ERR_ANCESTRY);
1823 * Require a straight line of history between the target commit
1824 * and the work tree's base commit.
1826 * Non-linear situations such as this require a rebase:
1828 * (commit) D F (base_commit)
1836 * 'got update' only handles linear cases:
1837 * Update forwards in time: A (base/yca) - B - C - D (commit)
1838 * Update backwards in time: D (base) - C - B - A (commit/yca)
1840 if (allow_forwards_in_time_only) {
1841 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1842 return got_error(GOT_ERR_ANCESTRY);
1843 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1844 got_object_id_cmp(base_commit_id, yca_id) != 0)
1845 return got_error(GOT_ERR_ANCESTRY);
1851 static const struct got_error *
1852 check_same_branch(struct got_object_id *commit_id,
1853 struct got_reference *head_ref, struct got_object_id *yca_id,
1854 struct got_repository *repo)
1856 const struct got_error *err = NULL;
1857 struct got_commit_graph *graph = NULL;
1858 struct got_object_id *head_commit_id = NULL;
1859 int is_same_branch = 0;
1861 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1865 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1869 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1874 err = got_commit_graph_open(&graph, "/", 1);
1878 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1879 check_cancelled, NULL);
1884 struct got_object_id *id;
1885 err = got_commit_graph_iter_next(&id, graph, repo,
1886 check_cancelled, NULL);
1888 if (err->code == GOT_ERR_ITER_COMPLETED)
1894 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1896 if (got_object_id_cmp(id, commit_id) == 0) {
1904 got_commit_graph_close(graph);
1905 free(head_commit_id);
1906 if (!err && !is_same_branch)
1907 err = got_error(GOT_ERR_ANCESTRY);
1911 static const struct got_error *
1912 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1914 static char msg[512];
1915 const char *branch_name;
1917 if (got_ref_is_symbolic(ref))
1918 branch_name = got_ref_get_symref_target(ref);
1920 branch_name = got_ref_get_name(ref);
1922 if (strncmp("refs/heads/", branch_name, 11) == 0)
1925 snprintf(msg, sizeof(msg),
1926 "target commit is not contained in branch '%s'; "
1927 "the branch to use must be specified with -b; "
1928 "if necessary a new branch can be created for "
1929 "this commit with 'got branch -c %s BRANCH_NAME'",
1930 branch_name, commit_id_str);
1932 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1935 static const struct got_error *
1936 cmd_checkout(int argc, char *argv[])
1938 const struct got_error *error = NULL;
1939 struct got_repository *repo = NULL;
1940 struct got_reference *head_ref = NULL;
1941 struct got_worktree *worktree = NULL;
1942 char *repo_path = NULL;
1943 char *worktree_path = NULL;
1944 const char *path_prefix = "";
1945 const char *branch_name = GOT_REF_HEAD;
1946 char *commit_id_str = NULL;
1947 int ch, same_path_prefix, allow_nonempty = 0;
1948 struct got_pathlist_head paths;
1949 struct got_checkout_progress_arg cpa;
1953 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1956 branch_name = optarg;
1959 commit_id_str = strdup(optarg);
1960 if (commit_id_str == NULL)
1961 return got_error_from_errno("strdup");
1967 path_prefix = optarg;
1979 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1980 "unveil", NULL) == -1)
1984 char *cwd, *base, *dotgit;
1985 repo_path = realpath(argv[0], NULL);
1986 if (repo_path == NULL)
1987 return got_error_from_errno2("realpath", argv[0]);
1988 cwd = getcwd(NULL, 0);
1990 error = got_error_from_errno("getcwd");
1993 if (path_prefix[0]) {
1994 base = basename(path_prefix);
1996 error = got_error_from_errno2("basename",
2001 base = basename(repo_path);
2003 error = got_error_from_errno2("basename",
2008 dotgit = strstr(base, ".git");
2011 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2012 error = got_error_from_errno("asprintf");
2017 } else if (argc == 2) {
2018 repo_path = realpath(argv[0], NULL);
2019 if (repo_path == NULL) {
2020 error = got_error_from_errno2("realpath", argv[0]);
2023 worktree_path = realpath(argv[1], NULL);
2024 if (worktree_path == NULL) {
2025 if (errno != ENOENT) {
2026 error = got_error_from_errno2("realpath",
2030 worktree_path = strdup(argv[1]);
2031 if (worktree_path == NULL) {
2032 error = got_error_from_errno("strdup");
2039 got_path_strip_trailing_slashes(repo_path);
2040 got_path_strip_trailing_slashes(worktree_path);
2042 error = got_repo_open(&repo, repo_path, NULL);
2046 /* Pre-create work tree path for unveil(2) */
2047 error = got_path_mkdir(worktree_path);
2049 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2050 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2052 if (!allow_nonempty &&
2053 !got_path_dir_is_empty(worktree_path)) {
2054 error = got_error_path(worktree_path,
2055 GOT_ERR_DIR_NOT_EMPTY);
2060 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2064 error = got_ref_open(&head_ref, repo, branch_name, 0);
2068 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2069 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2072 error = got_worktree_open(&worktree, worktree_path);
2076 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2080 if (!same_path_prefix) {
2081 error = got_error(GOT_ERR_PATH_PREFIX);
2085 if (commit_id_str) {
2086 struct got_object_id *commit_id;
2087 error = got_repo_match_object_id(&commit_id, NULL,
2088 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2091 error = check_linear_ancestry(commit_id,
2092 got_worktree_get_base_commit_id(worktree), 0, repo);
2093 if (error != NULL) {
2095 if (error->code == GOT_ERR_ANCESTRY) {
2096 error = checkout_ancestry_error(
2097 head_ref, commit_id_str);
2101 error = check_same_branch(commit_id, head_ref, NULL, repo);
2103 if (error->code == GOT_ERR_ANCESTRY) {
2104 error = checkout_ancestry_error(
2105 head_ref, commit_id_str);
2109 error = got_worktree_set_base_commit_id(worktree, repo,
2116 error = got_pathlist_append(&paths, "", NULL);
2119 cpa.worktree_path = worktree_path;
2120 cpa.had_base_commit_ref_error = 0;
2121 error = got_worktree_checkout_files(worktree, &paths, repo,
2122 checkout_progress, &cpa, check_cancelled, NULL);
2126 printf("Now shut up and hack\n");
2127 if (cpa.had_base_commit_ref_error)
2128 show_worktree_base_ref_warning();
2130 got_pathlist_free(&paths);
2131 free(commit_id_str);
2133 free(worktree_path);
2140 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2145 static const struct got_error *
2146 update_progress(void *arg, unsigned char status, const char *path)
2148 int *did_something = arg;
2150 if (status == GOT_STATUS_EXISTS ||
2151 status == GOT_STATUS_BASE_REF_ERR)
2156 /* Base commit bump happens silently. */
2157 if (status == GOT_STATUS_BUMP_BASE)
2160 while (path[0] == '/')
2162 printf("%c %s\n", status, path);
2166 static const struct got_error *
2167 switch_head_ref(struct got_reference *head_ref,
2168 struct got_object_id *commit_id, struct got_worktree *worktree,
2169 struct got_repository *repo)
2171 const struct got_error *err = NULL;
2173 int ref_has_moved = 0;
2175 /* Trivial case: switching between two different references. */
2176 if (strcmp(got_ref_get_name(head_ref),
2177 got_worktree_get_head_ref_name(worktree)) != 0) {
2178 printf("Switching work tree from %s to %s\n",
2179 got_worktree_get_head_ref_name(worktree),
2180 got_ref_get_name(head_ref));
2181 return got_worktree_set_head_ref(worktree, head_ref);
2184 err = check_linear_ancestry(commit_id,
2185 got_worktree_get_base_commit_id(worktree), 0, repo);
2187 if (err->code != GOT_ERR_ANCESTRY)
2194 /* Switching to a rebased branch with the same reference name. */
2195 err = got_object_id_str(&base_id_str,
2196 got_worktree_get_base_commit_id(worktree));
2199 printf("Reference %s now points at a different branch\n",
2200 got_worktree_get_head_ref_name(worktree));
2201 printf("Switching work tree from %s to %s\n", base_id_str,
2202 got_worktree_get_head_ref_name(worktree));
2206 static const struct got_error *
2207 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2209 const struct got_error *err;
2212 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2216 return got_error(GOT_ERR_REBASING);
2218 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2222 return got_error(GOT_ERR_HISTEDIT_BUSY);
2227 static const struct got_error *
2228 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2229 char *argv[], struct got_worktree *worktree)
2231 const struct got_error *err = NULL;
2238 return got_error_from_errno("strdup");
2239 return got_pathlist_append(paths, path, NULL);
2242 for (i = 0; i < argc; i++) {
2243 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2246 err = got_pathlist_append(paths, path, NULL);
2256 static const struct got_error *
2257 cmd_update(int argc, char *argv[])
2259 const struct got_error *error = NULL;
2260 struct got_repository *repo = NULL;
2261 struct got_worktree *worktree = NULL;
2262 char *worktree_path = NULL;
2263 struct got_object_id *commit_id = NULL;
2264 char *commit_id_str = NULL;
2265 const char *branch_name = NULL;
2266 struct got_reference *head_ref = NULL;
2267 struct got_pathlist_head paths;
2268 struct got_pathlist_entry *pe;
2269 int ch, did_something = 0;
2273 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2276 branch_name = optarg;
2279 commit_id_str = strdup(optarg);
2280 if (commit_id_str == NULL)
2281 return got_error_from_errno("strdup");
2293 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2294 "unveil", NULL) == -1)
2297 worktree_path = getcwd(NULL, 0);
2298 if (worktree_path == NULL) {
2299 error = got_error_from_errno("getcwd");
2302 error = got_worktree_open(&worktree, worktree_path);
2306 error = check_rebase_or_histedit_in_progress(worktree);
2310 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2315 error = apply_unveil(got_repo_get_path(repo), 0,
2316 got_worktree_get_root_path(worktree));
2320 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2324 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2325 got_worktree_get_head_ref_name(worktree), 0);
2328 if (commit_id_str == NULL) {
2329 error = got_ref_resolve(&commit_id, repo, head_ref);
2332 error = got_object_id_str(&commit_id_str, commit_id);
2336 error = got_repo_match_object_id(&commit_id, NULL,
2337 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2338 free(commit_id_str);
2339 commit_id_str = NULL;
2342 error = got_object_id_str(&commit_id_str, commit_id);
2348 struct got_object_id *head_commit_id;
2349 TAILQ_FOREACH(pe, &paths, entry) {
2350 if (pe->path_len == 0)
2352 error = got_error_msg(GOT_ERR_BAD_PATH,
2353 "switching between branches requires that "
2354 "the entire work tree gets updated");
2357 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2360 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2362 free(head_commit_id);
2365 error = check_same_branch(commit_id, head_ref, NULL, repo);
2368 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2372 error = check_linear_ancestry(commit_id,
2373 got_worktree_get_base_commit_id(worktree), 0, repo);
2374 if (error != NULL) {
2375 if (error->code == GOT_ERR_ANCESTRY)
2376 error = got_error(GOT_ERR_BRANCH_MOVED);
2379 error = check_same_branch(commit_id, head_ref, NULL, repo);
2384 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2386 error = got_worktree_set_base_commit_id(worktree, repo,
2392 error = got_worktree_checkout_files(worktree, &paths, repo,
2393 update_progress, &did_something, check_cancelled, NULL);
2398 printf("Updated to commit %s\n", commit_id_str);
2400 printf("Already up-to-date\n");
2402 free(worktree_path);
2403 TAILQ_FOREACH(pe, &paths, entry)
2404 free((char *)pe->path);
2405 got_pathlist_free(&paths);
2407 free(commit_id_str);
2411 static const struct got_error *
2412 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2413 const char *path, int diff_context, int ignore_whitespace,
2414 struct got_repository *repo)
2416 const struct got_error *err = NULL;
2417 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2420 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2425 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2429 while (path[0] == '/')
2431 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2432 ignore_whitespace, stdout);
2435 got_object_blob_close(blob1);
2436 got_object_blob_close(blob2);
2440 static const struct got_error *
2441 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2442 const char *path, int diff_context, int ignore_whitespace,
2443 struct got_repository *repo)
2445 const struct got_error *err = NULL;
2446 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2447 struct got_diff_blob_output_unidiff_arg arg;
2450 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2455 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2459 arg.diff_context = diff_context;
2460 arg.ignore_whitespace = ignore_whitespace;
2461 arg.outfile = stdout;
2462 while (path[0] == '/')
2464 err = got_diff_tree(tree1, tree2, path, path, repo,
2465 got_diff_blob_output_unidiff, &arg, 1);
2468 got_object_tree_close(tree1);
2470 got_object_tree_close(tree2);
2474 static const struct got_error *
2475 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2476 const char *path, int diff_context, struct got_repository *repo)
2478 const struct got_error *err = NULL;
2479 struct got_commit_object *pcommit = NULL;
2480 char *id_str1 = NULL, *id_str2 = NULL;
2481 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2482 struct got_object_qid *qid;
2484 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2486 err = got_object_open_as_commit(&pcommit, repo,
2492 if (path && path[0] != '\0') {
2494 err = got_object_id_by_path(&obj_id2, repo, id, path);
2497 err = got_object_id_str(&id_str2, obj_id2);
2503 err = got_object_id_by_path(&obj_id1, repo,
2509 err = got_object_id_str(&id_str1, obj_id1);
2515 err = got_object_get_type(&obj_type, repo, obj_id2);
2520 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2522 case GOT_OBJ_TYPE_BLOB:
2523 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2526 case GOT_OBJ_TYPE_TREE:
2527 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2531 err = got_error(GOT_ERR_OBJ_TYPE);
2537 obj_id2 = got_object_commit_get_tree_id(commit);
2538 err = got_object_id_str(&id_str2, obj_id2);
2541 obj_id1 = got_object_commit_get_tree_id(pcommit);
2542 err = got_object_id_str(&id_str1, obj_id1);
2545 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2546 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2552 got_object_commit_close(pcommit);
2557 get_datestr(time_t *time, char *datebuf)
2559 struct tm mytm, *tm;
2562 tm = gmtime_r(time, &mytm);
2565 s = asctime_r(tm, datebuf);
2568 p = strchr(s, '\n');
2574 static const struct got_error *
2575 match_logmsg(int *have_match, struct got_object_id *id,
2576 struct got_commit_object *commit, regex_t *regex)
2578 const struct got_error *err = NULL;
2579 regmatch_t regmatch;
2580 char *id_str = NULL, *logmsg = NULL;
2584 err = got_object_id_str(&id_str, id);
2588 err = got_object_commit_get_logmsg(&logmsg, commit);
2592 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2600 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2602 static const struct got_error *
2603 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2604 struct got_repository *repo, const char *path, int show_patch,
2605 int diff_context, struct got_reflist_head *refs)
2607 const struct got_error *err = NULL;
2608 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2610 time_t committer_time;
2611 const char *author, *committer;
2612 char *refs_str = NULL;
2613 struct got_reflist_entry *re;
2615 SIMPLEQ_FOREACH(re, refs, entry) {
2618 struct got_tag_object *tag = NULL;
2621 name = got_ref_get_name(re->ref);
2622 if (strcmp(name, GOT_REF_HEAD) == 0)
2624 if (strncmp(name, "refs/", 5) == 0)
2626 if (strncmp(name, "got/", 4) == 0)
2628 if (strncmp(name, "heads/", 6) == 0)
2630 if (strncmp(name, "remotes/", 8) == 0)
2632 if (strncmp(name, "tags/", 5) == 0) {
2633 err = got_object_open_as_tag(&tag, repo, re->id);
2635 if (err->code != GOT_ERR_OBJ_TYPE)
2637 /* Ref points at something other than a tag. */
2642 cmp = got_object_id_cmp(tag ?
2643 got_object_tag_get_object_id(tag) : re->id, id);
2645 got_object_tag_close(tag);
2649 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2651 err = got_error_from_errno("asprintf");
2657 err = got_object_id_str(&id_str, id);
2661 printf(GOT_COMMIT_SEP_STR);
2662 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2663 refs_str ? refs_str : "", refs_str ? ")" : "");
2668 printf("from: %s\n", got_object_commit_get_author(commit));
2669 committer_time = got_object_commit_get_committer_time(commit);
2670 datestr = get_datestr(&committer_time, datebuf);
2672 printf("date: %s UTC\n", datestr);
2673 author = got_object_commit_get_author(commit);
2674 committer = got_object_commit_get_committer(commit);
2675 if (strcmp(author, committer) != 0)
2676 printf("via: %s\n", committer);
2677 if (got_object_commit_get_nparents(commit) > 1) {
2678 const struct got_object_id_queue *parent_ids;
2679 struct got_object_qid *qid;
2681 parent_ids = got_object_commit_get_parent_ids(commit);
2682 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2683 err = got_object_id_str(&id_str, qid->id);
2686 printf("parent %d: %s\n", n++, id_str);
2691 err = got_object_commit_get_logmsg(&logmsg0, commit);
2697 line = strsep(&logmsg, "\n");
2699 printf(" %s\n", line);
2704 err = print_patch(commit, id, path, diff_context, repo);
2709 if (fflush(stdout) != 0 && err == NULL)
2710 err = got_error_from_errno("fflush");
2714 static const struct got_error *
2715 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2716 const char *path, int show_patch, const char *search_pattern,
2717 int diff_context, int limit, int log_branches,
2718 struct got_reflist_head *refs)
2720 const struct got_error *err;
2721 struct got_commit_graph *graph;
2725 if (search_pattern &&
2726 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2727 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2729 err = got_commit_graph_open(&graph, path, !log_branches);
2732 err = got_commit_graph_iter_start(graph, root_id, repo,
2733 check_cancelled, NULL);
2737 struct got_commit_object *commit;
2738 struct got_object_id *id;
2740 if (sigint_received || sigpipe_received)
2743 err = got_commit_graph_iter_next(&id, graph, repo,
2744 check_cancelled, NULL);
2746 if (err->code == GOT_ERR_ITER_COMPLETED)
2753 err = got_object_open_as_commit(&commit, repo, id);
2757 if (search_pattern) {
2758 err = match_logmsg(&have_match, id, commit, ®ex);
2760 got_object_commit_close(commit);
2763 if (have_match == 0) {
2764 got_object_commit_close(commit);
2769 err = print_commit(commit, id, repo, path, show_patch,
2770 diff_context, refs);
2771 got_object_commit_close(commit);
2772 if (err || (limit && --limit == 0))
2778 got_commit_graph_close(graph);
2785 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2786 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2791 get_default_log_limit(void)
2793 const char *got_default_log_limit;
2797 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2798 if (got_default_log_limit == NULL)
2800 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2806 static const struct got_error *
2807 cmd_log(int argc, char *argv[])
2809 const struct got_error *error;
2810 struct got_repository *repo = NULL;
2811 struct got_worktree *worktree = NULL;
2812 struct got_commit_object *commit = NULL;
2813 struct got_object_id *id = NULL;
2814 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2815 const char *start_commit = NULL, *search_pattern = NULL;
2816 int diff_context = -1, ch;
2817 int show_patch = 0, limit = 0, log_branches = 0;
2819 struct got_reflist_head refs;
2821 SIMPLEQ_INIT(&refs);
2824 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2830 limit = get_default_log_limit();
2832 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2838 start_commit = optarg;
2841 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2844 err(1, "-C option %s", errstr);
2847 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2849 err(1, "-l option %s", errstr);
2855 repo_path = realpath(optarg, NULL);
2856 if (repo_path == NULL)
2857 return got_error_from_errno2("realpath",
2859 got_path_strip_trailing_slashes(repo_path);
2862 search_pattern = optarg;
2873 if (diff_context == -1)
2875 else if (!show_patch)
2876 errx(1, "-C reguires -p");
2878 cwd = getcwd(NULL, 0);
2880 error = got_error_from_errno("getcwd");
2884 error = got_worktree_open(&worktree, cwd);
2885 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2892 error = got_error_from_errno("strdup");
2895 } else if (argc == 1) {
2897 error = got_worktree_resolve_path(&path, worktree,
2902 path = strdup(argv[0]);
2904 error = got_error_from_errno("strdup");
2911 if (repo_path == NULL) {
2912 repo_path = worktree ?
2913 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2915 if (repo_path == NULL) {
2916 error = got_error_from_errno("strdup");
2920 error = got_repo_open(&repo, repo_path, NULL);
2924 error = apply_unveil(got_repo_get_path(repo), 1,
2925 worktree ? got_worktree_get_root_path(worktree) : NULL);
2929 if (start_commit == NULL) {
2930 struct got_reference *head_ref;
2931 error = got_ref_open(&head_ref, repo,
2932 worktree ? got_worktree_get_head_ref_name(worktree)
2936 error = got_ref_resolve(&id, repo, head_ref);
2937 got_ref_close(head_ref);
2940 error = got_object_open_as_commit(&commit, repo, id);
2942 struct got_reference *ref;
2943 error = got_ref_open(&ref, repo, start_commit, 0);
2944 if (error == NULL) {
2946 error = got_ref_resolve(&id, repo, ref);
2950 error = got_object_get_type(&obj_type, repo, id);
2953 if (obj_type == GOT_OBJ_TYPE_TAG) {
2954 struct got_tag_object *tag;
2955 error = got_object_open_as_tag(&tag, repo, id);
2958 if (got_object_tag_get_object_type(tag) !=
2959 GOT_OBJ_TYPE_COMMIT) {
2960 got_object_tag_close(tag);
2961 error = got_error(GOT_ERR_OBJ_TYPE);
2965 id = got_object_id_dup(
2966 got_object_tag_get_object_id(tag));
2968 error = got_error_from_errno(
2969 "got_object_id_dup");
2970 got_object_tag_close(tag);
2973 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2974 error = got_error(GOT_ERR_OBJ_TYPE);
2977 error = got_object_open_as_commit(&commit, repo, id);
2981 if (commit == NULL) {
2982 error = got_repo_match_object_id_prefix(&id,
2983 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2992 const char *prefix = got_worktree_get_path_prefix(worktree);
2994 if (asprintf(&p, "%s%s%s", prefix,
2995 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2996 error = got_error_from_errno("asprintf");
2999 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3002 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3007 path = in_repo_path;
3010 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3014 error = print_commits(id, repo, path, show_patch, search_pattern,
3015 diff_context, limit, log_branches, &refs);
3022 got_worktree_close(worktree);
3024 const struct got_error *repo_error;
3025 repo_error = got_repo_close(repo);
3029 got_ref_list_free(&refs);
3036 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3037 "[-w] [object1 object2 | path]\n", getprogname());
3041 struct print_diff_arg {
3042 struct got_repository *repo;
3043 struct got_worktree *worktree;
3048 int ignore_whitespace;
3051 static const struct got_error *
3052 print_diff(void *arg, unsigned char status, unsigned char staged_status,
3053 const char *path, struct got_object_id *blob_id,
3054 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3055 int dirfd, const char *de_name)
3057 struct print_diff_arg *a = arg;
3058 const struct got_error *err = NULL;
3059 struct got_blob_object *blob1 = NULL;
3062 char *abspath = NULL, *label1 = NULL;
3065 if (a->diff_staged) {
3066 if (staged_status != GOT_STATUS_MODIFY &&
3067 staged_status != GOT_STATUS_ADD &&
3068 staged_status != GOT_STATUS_DELETE)
3071 if (staged_status == GOT_STATUS_DELETE)
3073 if (status == GOT_STATUS_NONEXISTENT)
3074 return got_error_set_errno(ENOENT, path);
3075 if (status != GOT_STATUS_MODIFY &&
3076 status != GOT_STATUS_ADD &&
3077 status != GOT_STATUS_DELETE &&
3078 status != GOT_STATUS_CONFLICT)
3082 if (!a->header_shown) {
3083 printf("diff %s %s%s\n", a->id_str,
3084 got_worktree_get_root_path(a->worktree),
3085 a->diff_staged ? " (staged changes)" : "");
3086 a->header_shown = 1;
3089 if (a->diff_staged) {
3090 const char *label1 = NULL, *label2 = NULL;
3091 switch (staged_status) {
3092 case GOT_STATUS_MODIFY:
3096 case GOT_STATUS_ADD:
3099 case GOT_STATUS_DELETE:
3103 return got_error(GOT_ERR_FILE_STATUS);
3105 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3106 label1, label2, a->diff_context, a->ignore_whitespace,
3110 if (staged_status == GOT_STATUS_ADD ||
3111 staged_status == GOT_STATUS_MODIFY) {
3113 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3117 err = got_object_id_str(&id_str, staged_blob_id);
3120 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3121 err = got_error_from_errno("asprintf");
3126 } else if (status != GOT_STATUS_ADD) {
3127 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3132 if (status != GOT_STATUS_DELETE) {
3133 if (asprintf(&abspath, "%s/%s",
3134 got_worktree_get_root_path(a->worktree), path) == -1) {
3135 err = got_error_from_errno("asprintf");
3140 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3142 err = got_error_from_errno2("openat", abspath);
3146 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3148 err = got_error_from_errno2("open", abspath);
3152 if (fstat(fd, &sb) == -1) {
3153 err = got_error_from_errno2("fstat", abspath);
3156 f2 = fdopen(fd, "r");
3158 err = got_error_from_errno2("fdopen", abspath);
3165 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3166 a->diff_context, a->ignore_whitespace, stdout);
3169 got_object_blob_close(blob1);
3170 if (f2 && fclose(f2) == EOF && err == NULL)
3171 err = got_error_from_errno("fclose");
3172 if (fd != -1 && close(fd) == -1 && err == NULL)
3173 err = got_error_from_errno("close");
3178 static const struct got_error *
3179 cmd_diff(int argc, char *argv[])
3181 const struct got_error *error;
3182 struct got_repository *repo = NULL;
3183 struct got_worktree *worktree = NULL;
3184 char *cwd = NULL, *repo_path = NULL;
3185 struct got_object_id *id1 = NULL, *id2 = NULL;
3186 const char *id_str1 = NULL, *id_str2 = NULL;
3187 char *label1 = NULL, *label2 = NULL;
3189 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3194 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3199 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3202 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3205 err(1, "-C option %s", errstr);
3208 repo_path = realpath(optarg, NULL);
3209 if (repo_path == NULL)
3210 return got_error_from_errno2("realpath",
3212 got_path_strip_trailing_slashes(repo_path);
3218 ignore_whitespace = 1;
3229 cwd = getcwd(NULL, 0);
3231 error = got_error_from_errno("getcwd");
3237 "-r option can't be used when diffing a work tree");
3238 error = got_worktree_open(&worktree, cwd);
3241 repo_path = strdup(got_worktree_get_repo_path(worktree));
3242 if (repo_path == NULL) {
3243 error = got_error_from_errno("strdup");
3247 error = got_worktree_resolve_path(&path, worktree,
3254 error = got_error_from_errno("strdup");
3258 } else if (argc == 2) {
3260 errx(1, "-s option can't be used when diffing "
3261 "objects in repository");
3264 if (repo_path == NULL) {
3265 error = got_worktree_open(&worktree, cwd);
3266 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3270 got_worktree_get_repo_path(worktree));
3271 if (repo_path == NULL) {
3272 error = got_error_from_errno("strdup");
3276 repo_path = strdup(cwd);
3277 if (repo_path == NULL) {
3278 error = got_error_from_errno("strdup");
3286 error = got_repo_open(&repo, repo_path, NULL);
3291 error = apply_unveil(got_repo_get_path(repo), 1,
3292 worktree ? got_worktree_get_root_path(worktree) : NULL);
3297 struct print_diff_arg arg;
3298 struct got_pathlist_head paths;
3303 error = got_object_id_str(&id_str,
3304 got_worktree_get_base_commit_id(worktree));
3308 arg.worktree = worktree;
3309 arg.diff_context = diff_context;
3310 arg.id_str = id_str;
3311 arg.header_shown = 0;
3312 arg.diff_staged = diff_staged;
3313 arg.ignore_whitespace = ignore_whitespace;
3315 error = got_pathlist_append(&paths, path, NULL);
3319 error = got_worktree_status(worktree, &paths, repo, print_diff,
3320 &arg, check_cancelled, NULL);
3322 got_pathlist_free(&paths);
3326 error = got_repo_match_object_id(&id1, &label1, id_str1,
3327 GOT_OBJ_TYPE_ANY, 1, repo);
3331 error = got_repo_match_object_id(&id2, &label2, id_str2,
3332 GOT_OBJ_TYPE_ANY, 1, repo);
3336 error = got_object_get_type(&type1, repo, id1);
3340 error = got_object_get_type(&type2, repo, id2);
3344 if (type1 != type2) {
3345 error = got_error(GOT_ERR_OBJ_TYPE);
3350 case GOT_OBJ_TYPE_BLOB:
3351 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3352 diff_context, ignore_whitespace, repo, stdout);
3354 case GOT_OBJ_TYPE_TREE:
3355 error = got_diff_objects_as_trees(id1, id2, "", "",
3356 diff_context, ignore_whitespace, repo, stdout);
3358 case GOT_OBJ_TYPE_COMMIT:
3359 printf("diff %s %s\n", label1, label2);
3360 error = got_diff_objects_as_commits(id1, id2, diff_context,
3361 ignore_whitespace, repo, stdout);
3364 error = got_error(GOT_ERR_OBJ_TYPE);
3373 got_worktree_close(worktree);
3375 const struct got_error *repo_error;
3376 repo_error = got_repo_close(repo);
3387 "usage: %s blame [-c commit] [-r repository-path] path\n",
3396 char datebuf[11]; /* YYYY-MM-DD + NUL */
3399 struct blame_cb_args {
3400 struct blame_line *lines;
3404 off_t *line_offsets;
3406 struct got_repository *repo;
3409 static const struct got_error *
3410 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3412 const struct got_error *err = NULL;
3413 struct blame_cb_args *a = arg;
3414 struct blame_line *bline;
3416 size_t linesize = 0;
3417 struct got_commit_object *commit = NULL;
3420 time_t committer_time;
3422 if (nlines != a->nlines ||
3423 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3424 return got_error(GOT_ERR_RANGE);
3426 if (sigint_received)
3427 return got_error(GOT_ERR_ITER_COMPLETED);
3430 return NULL; /* no change in this commit */
3432 /* Annotate this line. */
3433 bline = &a->lines[lineno - 1];
3434 if (bline->annotated)
3436 err = got_object_id_str(&bline->id_str, id);
3440 err = got_object_open_as_commit(&commit, a->repo, id);
3444 bline->committer = strdup(got_object_commit_get_committer(commit));
3445 if (bline->committer == NULL) {
3446 err = got_error_from_errno("strdup");
3450 committer_time = got_object_commit_get_committer_time(commit);
3451 if (localtime_r(&committer_time, &tm) == NULL)
3452 return got_error_from_errno("localtime_r");
3453 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3454 &tm) >= sizeof(bline->datebuf)) {
3455 err = got_error(GOT_ERR_NO_SPACE);
3458 bline->annotated = 1;
3460 /* Print lines annotated so far. */
3461 bline = &a->lines[a->lineno_cur - 1];
3462 if (!bline->annotated)
3465 offset = a->line_offsets[a->lineno_cur - 1];
3466 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3467 err = got_error_from_errno("fseeko");
3471 while (bline->annotated) {
3472 char *smallerthan, *at, *nl, *committer;
3475 if (getline(&line, &linesize, a->f) == -1) {
3477 err = got_error_from_errno("getline");
3481 committer = bline->committer;
3482 smallerthan = strchr(committer, '<');
3483 if (smallerthan && smallerthan[1] != '\0')
3484 committer = smallerthan + 1;
3485 at = strchr(committer, '@');
3488 len = strlen(committer);
3490 committer[8] = '\0';
3492 nl = strchr(line, '\n');
3495 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3496 bline->id_str, bline->datebuf, committer, line);
3499 bline = &a->lines[a->lineno_cur - 1];
3503 got_object_commit_close(commit);
3508 static const struct got_error *
3509 cmd_blame(int argc, char *argv[])
3511 const struct got_error *error;
3512 struct got_repository *repo = NULL;
3513 struct got_worktree *worktree = NULL;
3514 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3515 struct got_object_id *obj_id = NULL;
3516 struct got_object_id *commit_id = NULL;
3517 struct got_blob_object *blob = NULL;
3518 char *commit_id_str = NULL;
3519 struct blame_cb_args bca;
3520 int ch, obj_type, i;
3523 memset(&bca, 0, sizeof(bca));
3526 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3531 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3534 commit_id_str = optarg;
3537 repo_path = realpath(optarg, NULL);
3538 if (repo_path == NULL)
3539 return got_error_from_errno2("realpath",
3541 got_path_strip_trailing_slashes(repo_path);
3557 cwd = getcwd(NULL, 0);
3559 error = got_error_from_errno("getcwd");
3562 if (repo_path == NULL) {
3563 error = got_worktree_open(&worktree, cwd);
3564 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3570 strdup(got_worktree_get_repo_path(worktree));
3571 if (repo_path == NULL) {
3572 error = got_error_from_errno("strdup");
3577 repo_path = strdup(cwd);
3578 if (repo_path == NULL) {
3579 error = got_error_from_errno("strdup");
3585 error = got_repo_open(&repo, repo_path, NULL);
3589 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3594 const char *prefix = got_worktree_get_path_prefix(worktree);
3595 char *p, *worktree_subdir = cwd +
3596 strlen(got_worktree_get_root_path(worktree));
3597 if (asprintf(&p, "%s%s%s%s%s",
3598 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3599 worktree_subdir, worktree_subdir[0] ? "/" : "",
3601 error = got_error_from_errno("asprintf");
3604 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3607 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3612 if (commit_id_str == NULL) {
3613 struct got_reference *head_ref;
3614 error = got_ref_open(&head_ref, repo, worktree ?
3615 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3618 error = got_ref_resolve(&commit_id, repo, head_ref);
3619 got_ref_close(head_ref);
3623 error = got_repo_match_object_id(&commit_id, NULL,
3624 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3629 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3633 error = got_object_get_type(&obj_type, repo, obj_id);
3637 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3638 error = got_error(GOT_ERR_OBJ_TYPE);
3642 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3645 bca.f = got_opentemp();
3646 if (bca.f == NULL) {
3647 error = got_error_from_errno("got_opentemp");
3650 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3651 &bca.line_offsets, bca.f, blob);
3652 if (error || bca.nlines == 0)
3655 /* Don't include \n at EOF in the blame line count. */
3656 if (bca.line_offsets[bca.nlines - 1] == filesize)
3659 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3660 if (bca.lines == NULL) {
3661 error = got_error_from_errno("calloc");
3665 bca.nlines_prec = 0;
3673 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3674 check_cancelled, NULL);
3682 got_object_blob_close(blob);
3684 got_worktree_close(worktree);
3686 const struct got_error *repo_error;
3687 repo_error = got_repo_close(repo);
3692 for (i = 0; i < bca.nlines; i++) {
3693 struct blame_line *bline = &bca.lines[i];
3694 free(bline->id_str);
3695 free(bline->committer);
3699 free(bca.line_offsets);
3700 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3701 error = got_error_from_errno("fclose");
3709 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3715 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3716 const char *root_path)
3718 int is_root_path = (strcmp(path, root_path) == 0);
3719 const char *modestr = "";
3720 mode_t mode = got_tree_entry_get_mode(te);
3722 path += strlen(root_path);
3723 while (path[0] == '/')
3726 if (got_object_tree_entry_is_submodule(te))
3728 else if (S_ISLNK(mode))
3730 else if (S_ISDIR(mode))
3732 else if (mode & S_IXUSR)
3735 printf("%s%s%s%s%s\n", id ? id : "", path,
3736 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3739 static const struct got_error *
3740 print_tree(const char *path, struct got_object_id *commit_id,
3741 int show_ids, int recurse, const char *root_path,
3742 struct got_repository *repo)
3744 const struct got_error *err = NULL;
3745 struct got_object_id *tree_id = NULL;
3746 struct got_tree_object *tree = NULL;
3749 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3753 err = got_object_open_as_tree(&tree, repo, tree_id);
3756 nentries = got_object_tree_get_nentries(tree);
3757 for (i = 0; i < nentries; i++) {
3758 struct got_tree_entry *te;
3761 if (sigint_received || sigpipe_received)
3764 te = got_object_tree_get_entry(tree, i);
3767 err = got_object_id_str(&id_str,
3768 got_tree_entry_get_id(te));
3771 if (asprintf(&id, "%s ", id_str) == -1) {
3772 err = got_error_from_errno("asprintf");
3778 print_entry(te, id, path, root_path);
3781 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3783 if (asprintf(&child_path, "%s%s%s", path,
3784 path[0] == '/' && path[1] == '\0' ? "" : "/",
3785 got_tree_entry_get_name(te)) == -1) {
3786 err = got_error_from_errno("asprintf");
3789 err = print_tree(child_path, commit_id, show_ids, 1,
3798 got_object_tree_close(tree);
3803 static const struct got_error *
3804 cmd_tree(int argc, char *argv[])
3806 const struct got_error *error;
3807 struct got_repository *repo = NULL;
3808 struct got_worktree *worktree = NULL;
3810 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3811 struct got_object_id *commit_id = NULL;
3812 char *commit_id_str = NULL;
3813 int show_ids = 0, recurse = 0;
3817 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3822 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3825 commit_id_str = optarg;
3828 repo_path = realpath(optarg, NULL);
3829 if (repo_path == NULL)
3830 return got_error_from_errno2("realpath",
3832 got_path_strip_trailing_slashes(repo_path);
3856 cwd = getcwd(NULL, 0);
3858 error = got_error_from_errno("getcwd");
3861 if (repo_path == NULL) {
3862 error = got_worktree_open(&worktree, cwd);
3863 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3869 strdup(got_worktree_get_repo_path(worktree));
3870 if (repo_path == NULL)
3871 error = got_error_from_errno("strdup");
3875 repo_path = strdup(cwd);
3876 if (repo_path == NULL) {
3877 error = got_error_from_errno("strdup");
3883 error = got_repo_open(&repo, repo_path, NULL);
3887 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3893 char *p, *worktree_subdir = cwd +
3894 strlen(got_worktree_get_root_path(worktree));
3895 if (asprintf(&p, "%s/%s",
3896 got_worktree_get_path_prefix(worktree),
3897 worktree_subdir) == -1) {
3898 error = got_error_from_errno("asprintf");
3901 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3908 if (in_repo_path == NULL) {
3909 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3914 if (commit_id_str == NULL) {
3915 struct got_reference *head_ref;
3916 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3919 error = got_ref_resolve(&commit_id, repo, head_ref);
3920 got_ref_close(head_ref);
3924 error = got_repo_match_object_id(&commit_id, NULL,
3925 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3930 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3931 in_repo_path, repo);
3938 got_worktree_close(worktree);
3940 const struct got_error *repo_error;
3941 repo_error = got_repo_close(repo);
3951 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3955 static const struct got_error *
3956 print_status(void *arg, unsigned char status, unsigned char staged_status,
3957 const char *path, struct got_object_id *blob_id,
3958 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3959 int dirfd, const char *de_name)
3961 if (status == staged_status && (status == GOT_STATUS_DELETE))
3962 status = GOT_STATUS_NO_CHANGE;
3963 printf("%c%c %s\n", status, staged_status, path);
3967 static const struct got_error *
3968 cmd_status(int argc, char *argv[])
3970 const struct got_error *error = NULL;
3971 struct got_repository *repo = NULL;
3972 struct got_worktree *worktree = NULL;
3974 struct got_pathlist_head paths;
3975 struct got_pathlist_entry *pe;
3980 while ((ch = getopt(argc, argv, "")) != -1) {
3992 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3996 cwd = getcwd(NULL, 0);
3998 error = got_error_from_errno("getcwd");
4002 error = got_worktree_open(&worktree, cwd);
4006 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4011 error = apply_unveil(got_repo_get_path(repo), 1,
4012 got_worktree_get_root_path(worktree));
4016 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4020 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4021 check_cancelled, NULL);
4023 TAILQ_FOREACH(pe, &paths, entry)
4024 free((char *)pe->path);
4025 got_pathlist_free(&paths);
4034 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4039 static const struct got_error *
4040 list_refs(struct got_repository *repo)
4042 static const struct got_error *err = NULL;
4043 struct got_reflist_head refs;
4044 struct got_reflist_entry *re;
4046 SIMPLEQ_INIT(&refs);
4047 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4051 SIMPLEQ_FOREACH(re, &refs, entry) {
4053 refstr = got_ref_to_str(re->ref);
4055 return got_error_from_errno("got_ref_to_str");
4056 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4060 got_ref_list_free(&refs);
4064 static const struct got_error *
4065 delete_ref(struct got_repository *repo, const char *refname)
4067 const struct got_error *err = NULL;
4068 struct got_reference *ref;
4070 err = got_ref_open(&ref, repo, refname, 0);
4074 err = got_ref_delete(ref, repo);
4079 static const struct got_error *
4080 add_ref(struct got_repository *repo, const char *refname, const char *target)
4082 const struct got_error *err = NULL;
4083 struct got_object_id *id;
4084 struct got_reference *ref = NULL;
4087 * Don't let the user create a reference name with a leading '-'.
4088 * While technically a valid reference name, this case is usually
4089 * an unintended typo.
4091 if (refname[0] == '-')
4092 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4094 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4097 struct got_reference *target_ref;
4099 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4101 err = got_ref_open(&target_ref, repo, target, 0);
4104 err = got_ref_resolve(&id, repo, target_ref);
4105 got_ref_close(target_ref);
4110 err = got_ref_alloc(&ref, refname, id);
4114 err = got_ref_write(ref, repo);
4122 static const struct got_error *
4123 add_symref(struct got_repository *repo, const char *refname, const char *target)
4125 const struct got_error *err = NULL;
4126 struct got_reference *ref = NULL;
4127 struct got_reference *target_ref = NULL;
4130 * Don't let the user create a reference name with a leading '-'.
4131 * While technically a valid reference name, this case is usually
4132 * an unintended typo.
4134 if (refname[0] == '-')
4135 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4137 err = got_ref_open(&target_ref, repo, target, 0);
4141 err = got_ref_alloc_symref(&ref, refname, target_ref);
4145 err = got_ref_write(ref, repo);
4148 got_ref_close(target_ref);
4154 static const struct got_error *
4155 cmd_ref(int argc, char *argv[])
4157 const struct got_error *error = NULL;
4158 struct got_repository *repo = NULL;
4159 struct got_worktree *worktree = NULL;
4160 char *cwd = NULL, *repo_path = NULL;
4161 int ch, do_list = 0, create_symref = 0;
4162 const char *delref = NULL;
4164 /* TODO: Add -s option for adding symbolic references. */
4165 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4171 repo_path = realpath(optarg, NULL);
4172 if (repo_path == NULL)
4173 return got_error_from_errno2("realpath",
4175 got_path_strip_trailing_slashes(repo_path);
4189 if (do_list && delref)
4190 errx(1, "-l and -d options are mutually exclusive\n");
4195 if (do_list || delref) {
4197 errx(1, "-s option cannot be used together with the "
4198 "-l or -d options");
4201 } else if (argc != 2)
4206 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4210 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4211 "sendfd unveil", NULL) == -1)
4215 cwd = getcwd(NULL, 0);
4217 error = got_error_from_errno("getcwd");
4221 if (repo_path == NULL) {
4222 error = got_worktree_open(&worktree, cwd);
4223 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4229 strdup(got_worktree_get_repo_path(worktree));
4230 if (repo_path == NULL)
4231 error = got_error_from_errno("strdup");
4235 repo_path = strdup(cwd);
4236 if (repo_path == NULL) {
4237 error = got_error_from_errno("strdup");
4243 error = got_repo_open(&repo, repo_path, NULL);
4247 error = apply_unveil(got_repo_get_path(repo), do_list,
4248 worktree ? got_worktree_get_root_path(worktree) : NULL);
4253 error = list_refs(repo);
4255 error = delete_ref(repo, delref);
4256 else if (create_symref)
4257 error = add_symref(repo, argv[0], argv[1]);
4259 error = add_ref(repo, argv[0], argv[1]);
4262 got_repo_close(repo);
4264 got_worktree_close(worktree);
4274 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4275 "[name]\n", getprogname());
4279 static const struct got_error *
4280 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4281 struct got_reference *ref)
4283 const struct got_error *err = NULL;
4284 const char *refname, *marker = " ";
4287 refname = got_ref_get_name(ref);
4288 if (worktree && strcmp(refname,
4289 got_worktree_get_head_ref_name(worktree)) == 0) {
4290 struct got_object_id *id = NULL;
4292 err = got_ref_resolve(&id, repo, ref);
4295 if (got_object_id_cmp(id,
4296 got_worktree_get_base_commit_id(worktree)) == 0)
4303 if (strncmp(refname, "refs/heads/", 11) == 0)
4305 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4308 refstr = got_ref_to_str(ref);
4310 return got_error_from_errno("got_ref_to_str");
4312 printf("%s%s: %s\n", marker, refname, refstr);
4317 static const struct got_error *
4318 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4320 const char *refname;
4322 if (worktree == NULL)
4323 return got_error(GOT_ERR_NOT_WORKTREE);
4325 refname = got_worktree_get_head_ref_name(worktree);
4327 if (strncmp(refname, "refs/heads/", 11) == 0)
4329 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4332 printf("%s\n", refname);
4337 static const struct got_error *
4338 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4340 static const struct got_error *err = NULL;
4341 struct got_reflist_head refs;
4342 struct got_reflist_entry *re;
4343 struct got_reference *temp_ref = NULL;
4344 int rebase_in_progress, histedit_in_progress;
4346 SIMPLEQ_INIT(&refs);
4349 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4354 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4359 if (rebase_in_progress || histedit_in_progress) {
4360 err = got_ref_open(&temp_ref, repo,
4361 got_worktree_get_head_ref_name(worktree), 0);
4364 list_branch(repo, worktree, temp_ref);
4365 got_ref_close(temp_ref);
4369 err = got_ref_list(&refs, repo, "refs/heads",
4370 got_ref_cmp_by_name, NULL);
4374 SIMPLEQ_FOREACH(re, &refs, entry)
4375 list_branch(repo, worktree, re->ref);
4377 got_ref_list_free(&refs);
4381 static const struct got_error *
4382 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4383 const char *branch_name)
4385 const struct got_error *err = NULL;
4386 struct got_reference *ref = NULL;
4389 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4390 return got_error_from_errno("asprintf");
4392 err = got_ref_open(&ref, repo, refname, 0);
4397 strcmp(got_worktree_get_head_ref_name(worktree),
4398 got_ref_get_name(ref)) == 0) {
4399 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4400 "will not delete this work tree's current branch");
4404 err = got_ref_delete(ref, repo);
4412 static const struct got_error *
4413 add_branch(struct got_repository *repo, const char *branch_name,
4414 struct got_object_id *base_commit_id)
4416 const struct got_error *err = NULL;
4417 struct got_reference *ref = NULL;
4418 char *base_refname = NULL, *refname = NULL;
4421 * Don't let the user create a branch name with a leading '-'.
4422 * While technically a valid reference name, this case is usually
4423 * an unintended typo.
4425 if (branch_name[0] == '-')
4426 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4428 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4429 err = got_error_from_errno("asprintf");
4433 err = got_ref_open(&ref, repo, refname, 0);
4435 err = got_error(GOT_ERR_BRANCH_EXISTS);
4437 } else if (err->code != GOT_ERR_NOT_REF)
4440 err = got_ref_alloc(&ref, refname, base_commit_id);
4444 err = got_ref_write(ref, repo);
4453 static const struct got_error *
4454 cmd_branch(int argc, char *argv[])
4456 const struct got_error *error = NULL;
4457 struct got_repository *repo = NULL;
4458 struct got_worktree *worktree = NULL;
4459 char *cwd = NULL, *repo_path = NULL;
4460 int ch, do_list = 0, do_show = 0, do_update = 1;
4461 const char *delref = NULL, *commit_id_arg = NULL;
4462 struct got_reference *ref = NULL;
4463 struct got_pathlist_head paths;
4464 struct got_pathlist_entry *pe;
4465 struct got_object_id *commit_id = NULL;
4466 char *commit_id_str = NULL;
4470 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4473 commit_id_arg = optarg;
4479 repo_path = realpath(optarg, NULL);
4480 if (repo_path == NULL)
4481 return got_error_from_errno2("realpath",
4483 got_path_strip_trailing_slashes(repo_path);
4497 if (do_list && delref)
4498 errx(1, "-l and -d options are mutually exclusive\n");
4503 if (!do_list && !delref && argc == 0)
4506 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4507 errx(1, "-c option can only be used when creating a branch");
4509 if (do_list || delref) {
4512 } else if (!do_show && argc != 1)
4516 if (do_list || do_show) {
4517 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4521 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4522 "sendfd unveil", NULL) == -1)
4526 cwd = getcwd(NULL, 0);
4528 error = got_error_from_errno("getcwd");
4532 if (repo_path == NULL) {
4533 error = got_worktree_open(&worktree, cwd);
4534 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4540 strdup(got_worktree_get_repo_path(worktree));
4541 if (repo_path == NULL)
4542 error = got_error_from_errno("strdup");
4546 repo_path = strdup(cwd);
4547 if (repo_path == NULL) {
4548 error = got_error_from_errno("strdup");
4554 error = got_repo_open(&repo, repo_path, NULL);
4558 error = apply_unveil(got_repo_get_path(repo), do_list,
4559 worktree ? got_worktree_get_root_path(worktree) : NULL);
4564 error = show_current_branch(repo, worktree);
4566 error = list_branches(repo, worktree);
4568 error = delete_branch(repo, worktree, delref);
4570 if (commit_id_arg == NULL)
4571 commit_id_arg = worktree ?
4572 got_worktree_get_head_ref_name(worktree) :
4574 error = got_repo_match_object_id(&commit_id, NULL,
4575 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4578 error = add_branch(repo, argv[0], commit_id);
4581 if (worktree && do_update) {
4582 int did_something = 0;
4583 char *branch_refname = NULL;
4585 error = got_object_id_str(&commit_id_str, commit_id);
4588 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4592 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4594 error = got_error_from_errno("asprintf");
4597 error = got_ref_open(&ref, repo, branch_refname, 0);
4598 free(branch_refname);
4601 error = switch_head_ref(ref, commit_id, worktree,
4605 error = got_worktree_set_base_commit_id(worktree, repo,
4609 error = got_worktree_checkout_files(worktree, &paths,
4610 repo, update_progress, &did_something,
4611 check_cancelled, NULL);
4615 printf("Updated to commit %s\n", commit_id_str);
4622 got_repo_close(repo);
4624 got_worktree_close(worktree);
4628 free(commit_id_str);
4629 TAILQ_FOREACH(pe, &paths, entry)
4630 free((char *)pe->path);
4631 got_pathlist_free(&paths);
4640 "usage: %s tag [-c commit] [-r repository] [-l] "
4641 "[-m message] name\n", getprogname());
4646 static const struct got_error *
4647 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4649 const struct got_error *err = NULL;
4650 struct got_reflist_entry *re, *se, *new;
4651 struct got_object_id *re_id, *se_id;
4652 struct got_tag_object *re_tag, *se_tag;
4653 time_t re_time, se_time;
4655 SIMPLEQ_FOREACH(re, tags, entry) {
4656 se = SIMPLEQ_FIRST(sorted);
4658 err = got_reflist_entry_dup(&new, re);
4661 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4664 err = got_ref_resolve(&re_id, repo, re->ref);
4667 err = got_object_open_as_tag(&re_tag, repo, re_id);
4671 re_time = got_object_tag_get_tagger_time(re_tag);
4672 got_object_tag_close(re_tag);
4676 err = got_ref_resolve(&se_id, repo, re->ref);
4679 err = got_object_open_as_tag(&se_tag, repo, se_id);
4683 se_time = got_object_tag_get_tagger_time(se_tag);
4684 got_object_tag_close(se_tag);
4686 if (se_time > re_time) {
4687 err = got_reflist_entry_dup(&new, re);
4690 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4693 se = SIMPLEQ_NEXT(se, entry);
4702 static const struct got_error *
4703 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4705 static const struct got_error *err = NULL;
4706 struct got_reflist_head refs;
4707 struct got_reflist_entry *re;
4709 SIMPLEQ_INIT(&refs);
4711 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4715 SIMPLEQ_FOREACH(re, &refs, entry) {
4716 const char *refname;
4717 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4721 struct got_object_id *id;
4722 struct got_tag_object *tag;
4723 struct got_commit_object *commit = NULL;
4725 refname = got_ref_get_name(re->ref);
4726 if (strncmp(refname, "refs/tags/", 10) != 0)
4729 refstr = got_ref_to_str(re->ref);
4730 if (refstr == NULL) {
4731 err = got_error_from_errno("got_ref_to_str");
4734 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4737 err = got_ref_resolve(&id, repo, re->ref);
4740 err = got_object_open_as_tag(&tag, repo, id);
4742 if (err->code != GOT_ERR_OBJ_TYPE) {
4746 /* "lightweight" tag */
4747 err = got_object_open_as_commit(&commit, repo, id);
4752 tagger = got_object_commit_get_committer(commit);
4754 got_object_commit_get_committer_time(commit);
4755 err = got_object_id_str(&id_str, id);
4761 tagger = got_object_tag_get_tagger(tag);
4762 tagger_time = got_object_tag_get_tagger_time(tag);
4763 err = got_object_id_str(&id_str,
4764 got_object_tag_get_object_id(tag));
4768 printf("from: %s\n", tagger);
4769 datestr = get_datestr(&tagger_time, datebuf);
4771 printf("date: %s UTC\n", datestr);
4773 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4775 switch (got_object_tag_get_object_type(tag)) {
4776 case GOT_OBJ_TYPE_BLOB:
4777 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4780 case GOT_OBJ_TYPE_TREE:
4781 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4784 case GOT_OBJ_TYPE_COMMIT:
4785 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4788 case GOT_OBJ_TYPE_TAG:
4789 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4798 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4801 got_object_commit_close(commit);
4803 tagmsg0 = strdup(got_object_tag_get_message(tag));
4804 got_object_tag_close(tag);
4805 if (tagmsg0 == NULL) {
4806 err = got_error_from_errno("strdup");
4813 line = strsep(&tagmsg, "\n");
4815 printf(" %s\n", line);
4820 got_ref_list_free(&refs);
4824 static const struct got_error *
4825 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4826 const char *tag_name, const char *repo_path)
4828 const struct got_error *err = NULL;
4829 char *template = NULL, *initial_content = NULL;
4830 char *editor = NULL;
4833 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4834 err = got_error_from_errno("asprintf");
4838 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4839 commit_id_str, tag_name) == -1) {
4840 err = got_error_from_errno("asprintf");
4844 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4848 dprintf(fd, initial_content);
4851 err = get_editor(&editor);
4854 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4856 free(initial_content);
4860 /* Editor is done; we can now apply unveil(2) */
4862 err = apply_unveil(repo_path, 0, NULL);
4871 static const struct got_error *
4872 add_tag(struct got_repository *repo, const char *tag_name,
4873 const char *commit_arg, const char *tagmsg_arg)
4875 const struct got_error *err = NULL;
4876 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4877 char *label = NULL, *commit_id_str = NULL;
4878 struct got_reference *ref = NULL;
4879 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4880 char *tagmsg_path = NULL, *tag_id_str = NULL;
4881 int preserve_tagmsg = 0;
4884 * Don't let the user create a tag name with a leading '-'.
4885 * While technically a valid reference name, this case is usually
4886 * an unintended typo.
4888 if (tag_name[0] == '-')
4889 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4891 err = get_author(&tagger, repo);
4895 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4896 GOT_OBJ_TYPE_COMMIT, 1, repo);
4900 err = got_object_id_str(&commit_id_str, commit_id);
4904 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4905 refname = strdup(tag_name);
4906 if (refname == NULL) {
4907 err = got_error_from_errno("strdup");
4911 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4912 err = got_error_from_errno("asprintf");
4916 err = got_ref_open(&ref, repo, refname, 0);
4918 err = got_error(GOT_ERR_TAG_EXISTS);
4920 } else if (err->code != GOT_ERR_NOT_REF)
4923 if (tagmsg_arg == NULL) {
4924 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4925 tag_name, got_repo_get_path(repo));
4927 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4928 tagmsg_path != NULL)
4929 preserve_tagmsg = 1;
4934 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4935 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4938 preserve_tagmsg = 1;
4942 err = got_ref_alloc(&ref, refname, tag_id);
4945 preserve_tagmsg = 1;
4949 err = got_ref_write(ref, repo);
4952 preserve_tagmsg = 1;
4956 err = got_object_id_str(&tag_id_str, tag_id);
4959 preserve_tagmsg = 1;
4962 printf("Created tag %s\n", tag_id_str);
4964 if (preserve_tagmsg) {
4965 fprintf(stderr, "%s: tag message preserved in %s\n",
4966 getprogname(), tagmsg_path);
4967 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4968 err = got_error_from_errno2("unlink", tagmsg_path);
4973 free(commit_id_str);
4981 static const struct got_error *
4982 cmd_tag(int argc, char *argv[])
4984 const struct got_error *error = NULL;
4985 struct got_repository *repo = NULL;
4986 struct got_worktree *worktree = NULL;
4987 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4988 char *gitconfig_path = NULL;
4989 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4990 int ch, do_list = 0;
4992 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4995 commit_id_arg = optarg;
5001 repo_path = realpath(optarg, NULL);
5002 if (repo_path == NULL)
5003 return got_error_from_errno2("realpath",
5005 got_path_strip_trailing_slashes(repo_path);
5020 if (commit_id_arg != NULL)
5021 errx(1, "-c option can only be used when creating a tag");
5023 errx(1, "-l and -m options are mutually exclusive");
5026 } else if (argc != 1)
5033 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5037 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5038 "sendfd unveil", NULL) == -1)
5042 cwd = getcwd(NULL, 0);
5044 error = got_error_from_errno("getcwd");
5048 if (repo_path == NULL) {
5049 error = got_worktree_open(&worktree, cwd);
5050 if (error && error->code != GOT_ERR_NOT_WORKTREE)
5056 strdup(got_worktree_get_repo_path(worktree));
5057 if (repo_path == NULL)
5058 error = got_error_from_errno("strdup");
5062 repo_path = strdup(cwd);
5063 if (repo_path == NULL) {
5064 error = got_error_from_errno("strdup");
5071 error = got_repo_open(&repo, repo_path, NULL);
5074 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5077 error = list_tags(repo, worktree);
5079 error = get_gitconfig_path(&gitconfig_path);
5082 error = got_repo_open(&repo, repo_path, gitconfig_path);
5087 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5092 if (commit_id_arg == NULL) {
5093 struct got_reference *head_ref;
5094 struct got_object_id *commit_id;
5095 error = got_ref_open(&head_ref, repo,
5096 worktree ? got_worktree_get_head_ref_name(worktree)
5100 error = got_ref_resolve(&commit_id, repo, head_ref);
5101 got_ref_close(head_ref);
5104 error = got_object_id_str(&commit_id_str, commit_id);
5110 error = add_tag(repo, tag_name,
5111 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5115 got_repo_close(repo);
5117 got_worktree_close(worktree);
5120 free(gitconfig_path);
5121 free(commit_id_str);
5128 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5133 static const struct got_error *
5134 add_progress(void *arg, unsigned char status, const char *path)
5136 while (path[0] == '/')
5138 printf("%c %s\n", status, path);
5142 static const struct got_error *
5143 cmd_add(int argc, char *argv[])
5145 const struct got_error *error = NULL;
5146 struct got_repository *repo = NULL;
5147 struct got_worktree *worktree = NULL;
5149 struct got_pathlist_head paths;
5150 struct got_pathlist_entry *pe;
5151 int ch, can_recurse = 0, no_ignores = 0;
5155 while ((ch = getopt(argc, argv, "IR")) != -1) {
5173 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5180 cwd = getcwd(NULL, 0);
5182 error = got_error_from_errno("getcwd");
5186 error = got_worktree_open(&worktree, cwd);
5190 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5195 error = apply_unveil(got_repo_get_path(repo), 1,
5196 got_worktree_get_root_path(worktree));
5200 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5204 if (!can_recurse && no_ignores) {
5205 error = got_error_msg(GOT_ERR_BAD_PATH,
5206 "disregarding ignores requires -R option");
5214 TAILQ_FOREACH(pe, &paths, entry) {
5215 if (asprintf(&ondisk_path, "%s/%s",
5216 got_worktree_get_root_path(worktree),
5218 error = got_error_from_errno("asprintf");
5221 if (lstat(ondisk_path, &sb) == -1) {
5222 if (errno == ENOENT) {
5226 error = got_error_from_errno2("lstat",
5232 if (S_ISDIR(sb.st_mode)) {
5233 error = got_error_msg(GOT_ERR_BAD_PATH,
5234 "adding directories requires -R option");
5240 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5241 NULL, repo, no_ignores);
5244 got_repo_close(repo);
5246 got_worktree_close(worktree);
5247 TAILQ_FOREACH(pe, &paths, entry)
5248 free((char *)pe->path);
5249 got_pathlist_free(&paths);
5257 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5262 static const struct got_error *
5263 print_remove_status(void *arg, unsigned char status,
5264 unsigned char staged_status, const char *path)
5266 while (path[0] == '/')
5268 if (status == GOT_STATUS_NONEXISTENT)
5270 if (status == staged_status && (status == GOT_STATUS_DELETE))
5271 status = GOT_STATUS_NO_CHANGE;
5272 printf("%c%c %s\n", status, staged_status, path);
5276 static const struct got_error *
5277 cmd_remove(int argc, char *argv[])
5279 const struct got_error *error = NULL;
5280 struct got_worktree *worktree = NULL;
5281 struct got_repository *repo = NULL;
5283 struct got_pathlist_head paths;
5284 struct got_pathlist_entry *pe;
5285 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5289 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5292 delete_local_mods = 1;
5310 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5317 cwd = getcwd(NULL, 0);
5319 error = got_error_from_errno("getcwd");
5322 error = got_worktree_open(&worktree, cwd);
5326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5331 error = apply_unveil(got_repo_get_path(repo), 1,
5332 got_worktree_get_root_path(worktree));
5336 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5343 TAILQ_FOREACH(pe, &paths, entry) {
5344 if (asprintf(&ondisk_path, "%s/%s",
5345 got_worktree_get_root_path(worktree),
5347 error = got_error_from_errno("asprintf");
5350 if (lstat(ondisk_path, &sb) == -1) {
5351 if (errno == ENOENT) {
5355 error = got_error_from_errno2("lstat",
5361 if (S_ISDIR(sb.st_mode)) {
5362 error = got_error_msg(GOT_ERR_BAD_PATH,
5363 "removing directories requires -R option");
5369 error = got_worktree_schedule_delete(worktree, &paths,
5370 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5373 got_repo_close(repo);
5375 got_worktree_close(worktree);
5376 TAILQ_FOREACH(pe, &paths, entry)
5377 free((char *)pe->path);
5378 got_pathlist_free(&paths);
5386 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5387 "path ...\n", getprogname());
5391 static const struct got_error *
5392 revert_progress(void *arg, unsigned char status, const char *path)
5394 if (status == GOT_STATUS_UNVERSIONED)
5397 while (path[0] == '/')
5399 printf("%c %s\n", status, path);
5403 struct choose_patch_arg {
5404 FILE *patch_script_file;
5408 static const struct got_error *
5409 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5410 int nchanges, const char *action)
5413 size_t linesize = 0;
5417 case GOT_STATUS_ADD:
5418 printf("A %s\n%s this addition? [y/n] ", path, action);
5420 case GOT_STATUS_DELETE:
5421 printf("D %s\n%s this deletion? [y/n] ", path, action);
5423 case GOT_STATUS_MODIFY:
5424 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5425 return got_error_from_errno("fseek");
5426 printf(GOT_COMMIT_SEP_STR);
5427 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5429 if (ferror(patch_file))
5430 return got_error_from_errno("getline");
5431 printf(GOT_COMMIT_SEP_STR);
5432 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5433 path, n, nchanges, action);
5436 return got_error_path(path, GOT_ERR_FILE_STATUS);
5442 static const struct got_error *
5443 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5444 FILE *patch_file, int n, int nchanges)
5446 const struct got_error *err = NULL;
5448 size_t linesize = 0;
5451 struct choose_patch_arg *a = arg;
5453 *choice = GOT_PATCH_CHOICE_NONE;
5455 if (a->patch_script_file) {
5457 err = show_change(status, path, patch_file, n, nchanges,
5461 linelen = getline(&line, &linesize, a->patch_script_file);
5462 if (linelen == -1) {
5463 if (ferror(a->patch_script_file))
5464 return got_error_from_errno("getline");
5467 nl = strchr(line, '\n');
5470 if (strcmp(line, "y") == 0) {
5471 *choice = GOT_PATCH_CHOICE_YES;
5473 } else if (strcmp(line, "n") == 0) {
5474 *choice = GOT_PATCH_CHOICE_NO;
5476 } else if (strcmp(line, "q") == 0 &&
5477 status == GOT_STATUS_MODIFY) {
5478 *choice = GOT_PATCH_CHOICE_QUIT;
5481 printf("invalid response '%s'\n", line);
5486 while (resp != 'y' && resp != 'n' && resp != 'q') {
5487 err = show_change(status, path, patch_file, n, nchanges,
5494 if (status == GOT_STATUS_MODIFY) {
5495 if (resp != 'y' && resp != 'n' && resp != 'q') {
5496 printf("invalid response '%c'\n", resp);
5499 } else if (resp != 'y' && resp != 'n') {
5500 printf("invalid response '%c'\n", resp);
5506 *choice = GOT_PATCH_CHOICE_YES;
5507 else if (resp == 'n')
5508 *choice = GOT_PATCH_CHOICE_NO;
5509 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5510 *choice = GOT_PATCH_CHOICE_QUIT;
5516 static const struct got_error *
5517 cmd_revert(int argc, char *argv[])
5519 const struct got_error *error = NULL;
5520 struct got_worktree *worktree = NULL;
5521 struct got_repository *repo = NULL;
5522 char *cwd = NULL, *path = NULL;
5523 struct got_pathlist_head paths;
5524 struct got_pathlist_entry *pe;
5525 int ch, can_recurse = 0, pflag = 0;
5526 FILE *patch_script_file = NULL;
5527 const char *patch_script_path = NULL;
5528 struct choose_patch_arg cpa;
5532 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5538 patch_script_path = optarg;
5553 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5554 "unveil", NULL) == -1)
5559 if (patch_script_path && !pflag)
5560 errx(1, "-F option can only be used together with -p option");
5562 cwd = getcwd(NULL, 0);
5564 error = got_error_from_errno("getcwd");
5567 error = got_worktree_open(&worktree, cwd);
5571 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5576 if (patch_script_path) {
5577 patch_script_file = fopen(patch_script_path, "r");
5578 if (patch_script_file == NULL) {
5579 error = got_error_from_errno2("fopen",
5584 error = apply_unveil(got_repo_get_path(repo), 1,
5585 got_worktree_get_root_path(worktree));
5589 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5596 TAILQ_FOREACH(pe, &paths, entry) {
5597 if (asprintf(&ondisk_path, "%s/%s",
5598 got_worktree_get_root_path(worktree),
5600 error = got_error_from_errno("asprintf");
5603 if (lstat(ondisk_path, &sb) == -1) {
5604 if (errno == ENOENT) {
5608 error = got_error_from_errno2("lstat",
5614 if (S_ISDIR(sb.st_mode)) {
5615 error = got_error_msg(GOT_ERR_BAD_PATH,
5616 "reverting directories requires -R option");
5622 cpa.patch_script_file = patch_script_file;
5623 cpa.action = "revert";
5624 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5625 pflag ? choose_patch : NULL, &cpa, repo);
5627 if (patch_script_file && fclose(patch_script_file) == EOF &&
5629 error = got_error_from_errno2("fclose", patch_script_path);
5631 got_repo_close(repo);
5633 got_worktree_close(worktree);
5642 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5647 struct collect_commit_logmsg_arg {
5648 const char *cmdline_log;
5650 const char *worktree_path;
5651 const char *branch_name;
5652 const char *repo_path;
5657 static const struct got_error *
5658 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5661 char *initial_content = NULL;
5662 struct got_pathlist_entry *pe;
5663 const struct got_error *err = NULL;
5664 char *template = NULL;
5665 struct collect_commit_logmsg_arg *a = arg;
5669 /* if a message was specified on the command line, just use it */
5670 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5671 len = strlen(a->cmdline_log) + 1;
5672 *logmsg = malloc(len + 1);
5673 if (*logmsg == NULL)
5674 return got_error_from_errno("malloc");
5675 strlcpy(*logmsg, a->cmdline_log, len);
5679 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5680 return got_error_from_errno("asprintf");
5682 if (asprintf(&initial_content,
5683 "\n# changes to be committed on branch %s:\n",
5684 a->branch_name) == -1)
5685 return got_error_from_errno("asprintf");
5687 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5691 dprintf(fd, initial_content);
5693 TAILQ_FOREACH(pe, commitable_paths, entry) {
5694 struct got_commitable *ct = pe->data;
5695 dprintf(fd, "# %c %s\n",
5696 got_commitable_get_status(ct),
5697 got_commitable_get_path(ct));
5701 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5703 free(initial_content);
5706 /* Editor is done; we can now apply unveil(2) */
5708 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5717 static const struct got_error *
5718 cmd_commit(int argc, char *argv[])
5720 const struct got_error *error = NULL;
5721 struct got_worktree *worktree = NULL;
5722 struct got_repository *repo = NULL;
5723 char *cwd = NULL, *id_str = NULL;
5724 struct got_object_id *id = NULL;
5725 const char *logmsg = NULL;
5726 struct collect_commit_logmsg_arg cl_arg;
5727 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5728 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5729 struct got_pathlist_head paths;
5732 cl_arg.logmsg_path = NULL;
5734 while ((ch = getopt(argc, argv, "m:")) != -1) {
5749 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5750 "unveil", NULL) == -1)
5753 cwd = getcwd(NULL, 0);
5755 error = got_error_from_errno("getcwd");
5758 error = got_worktree_open(&worktree, cwd);
5762 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5765 if (rebase_in_progress) {
5766 error = got_error(GOT_ERR_REBASING);
5770 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5775 error = get_gitconfig_path(&gitconfig_path);
5778 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5783 error = get_author(&author, repo);
5788 * unveil(2) traverses exec(2); if an editor is used we have
5789 * to apply unveil after the log message has been written.
5791 if (logmsg == NULL || strlen(logmsg) == 0)
5792 error = get_editor(&editor);
5794 error = apply_unveil(got_repo_get_path(repo), 0,
5795 got_worktree_get_root_path(worktree));
5799 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5803 cl_arg.editor = editor;
5804 cl_arg.cmdline_log = logmsg;
5805 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5806 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5807 if (!histedit_in_progress) {
5808 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5809 error = got_error(GOT_ERR_COMMIT_BRANCH);
5812 cl_arg.branch_name += 11;
5814 cl_arg.repo_path = got_repo_get_path(repo);
5815 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5816 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5818 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5819 cl_arg.logmsg_path != NULL)
5820 preserve_logmsg = 1;
5824 error = got_object_id_str(&id_str, id);
5827 printf("Created commit %s\n", id_str);
5829 if (preserve_logmsg) {
5830 fprintf(stderr, "%s: log message preserved in %s\n",
5831 getprogname(), cl_arg.logmsg_path);
5832 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5834 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5835 free(cl_arg.logmsg_path);
5837 got_repo_close(repo);
5839 got_worktree_close(worktree);
5842 free(gitconfig_path);
5849 usage_cherrypick(void)
5851 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5855 static const struct got_error *
5856 cmd_cherrypick(int argc, char *argv[])
5858 const struct got_error *error = NULL;
5859 struct got_worktree *worktree = NULL;
5860 struct got_repository *repo = NULL;
5861 char *cwd = NULL, *commit_id_str = NULL;
5862 struct got_object_id *commit_id = NULL;
5863 struct got_commit_object *commit = NULL;
5864 struct got_object_qid *pid;
5865 struct got_reference *head_ref = NULL;
5866 int ch, did_something = 0;
5868 while ((ch = getopt(argc, argv, "")) != -1) {
5880 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5881 "unveil", NULL) == -1)
5887 cwd = getcwd(NULL, 0);
5889 error = got_error_from_errno("getcwd");
5892 error = got_worktree_open(&worktree, cwd);
5896 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5901 error = apply_unveil(got_repo_get_path(repo), 0,
5902 got_worktree_get_root_path(worktree));
5906 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5907 GOT_OBJ_TYPE_COMMIT, repo);
5908 if (error != NULL) {
5909 struct got_reference *ref;
5910 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5912 error = got_ref_open(&ref, repo, argv[0], 0);
5915 error = got_ref_resolve(&commit_id, repo, ref);
5920 error = got_object_id_str(&commit_id_str, commit_id);
5924 error = got_ref_open(&head_ref, repo,
5925 got_worktree_get_head_ref_name(worktree), 0);
5929 error = check_same_branch(commit_id, head_ref, NULL, repo);
5931 if (error->code != GOT_ERR_ANCESTRY)
5935 error = got_error(GOT_ERR_SAME_BRANCH);
5939 error = got_object_open_as_commit(&commit, repo, commit_id);
5942 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5943 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5944 commit_id, repo, update_progress, &did_something, check_cancelled,
5950 printf("Merged commit %s\n", commit_id_str);
5953 got_object_commit_close(commit);
5954 free(commit_id_str);
5956 got_ref_close(head_ref);
5958 got_worktree_close(worktree);
5960 got_repo_close(repo);
5967 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5971 static const struct got_error *
5972 cmd_backout(int argc, char *argv[])
5974 const struct got_error *error = NULL;
5975 struct got_worktree *worktree = NULL;
5976 struct got_repository *repo = NULL;
5977 char *cwd = NULL, *commit_id_str = NULL;
5978 struct got_object_id *commit_id = NULL;
5979 struct got_commit_object *commit = NULL;
5980 struct got_object_qid *pid;
5981 struct got_reference *head_ref = NULL;
5982 int ch, did_something = 0;
5984 while ((ch = getopt(argc, argv, "")) != -1) {
5996 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5997 "unveil", NULL) == -1)
6003 cwd = getcwd(NULL, 0);
6005 error = got_error_from_errno("getcwd");
6008 error = got_worktree_open(&worktree, cwd);
6012 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6017 error = apply_unveil(got_repo_get_path(repo), 0,
6018 got_worktree_get_root_path(worktree));
6022 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6023 GOT_OBJ_TYPE_COMMIT, repo);
6024 if (error != NULL) {
6025 struct got_reference *ref;
6026 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6028 error = got_ref_open(&ref, repo, argv[0], 0);
6031 error = got_ref_resolve(&commit_id, repo, ref);
6036 error = got_object_id_str(&commit_id_str, commit_id);
6040 error = got_ref_open(&head_ref, repo,
6041 got_worktree_get_head_ref_name(worktree), 0);
6045 error = check_same_branch(commit_id, head_ref, NULL, repo);
6049 error = got_object_open_as_commit(&commit, repo, commit_id);
6052 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6054 error = got_error(GOT_ERR_ROOT_COMMIT);
6058 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6059 update_progress, &did_something, check_cancelled, NULL);
6064 printf("Backed out commit %s\n", commit_id_str);
6067 got_object_commit_close(commit);
6068 free(commit_id_str);
6070 got_ref_close(head_ref);
6072 got_worktree_close(worktree);
6074 got_repo_close(repo);
6081 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6087 trim_logmsg(char *logmsg, int limit)
6092 len = strlen(logmsg);
6096 nl = strchr(logmsg, '\n');
6101 static const struct got_error *
6102 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6104 const struct got_error *err;
6105 char *logmsg0 = NULL;
6108 err = got_object_commit_get_logmsg(&logmsg0, commit);
6113 while (isspace((unsigned char)s[0]))
6116 *logmsg = strdup(s);
6117 if (*logmsg == NULL) {
6118 err = got_error_from_errno("strdup");
6122 trim_logmsg(*logmsg, limit);
6128 static const struct got_error *
6129 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6131 const struct got_error *err;
6132 struct got_commit_object *commit = NULL;
6133 char *id_str = NULL, *logmsg = NULL;
6135 err = got_object_open_as_commit(&commit, repo, id);
6139 err = got_object_id_str(&id_str, id);
6145 err = get_short_logmsg(&logmsg, 42, commit);
6149 printf("%s -> merge conflict: %s\n", id_str, logmsg);
6152 got_object_commit_close(commit);
6157 static const struct got_error *
6158 show_rebase_progress(struct got_commit_object *commit,
6159 struct got_object_id *old_id, struct got_object_id *new_id)
6161 const struct got_error *err;
6162 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6164 err = got_object_id_str(&old_id_str, old_id);
6169 err = got_object_id_str(&new_id_str, new_id);
6174 old_id_str[12] = '\0';
6176 new_id_str[12] = '\0';
6178 err = get_short_logmsg(&logmsg, 42, commit);
6182 printf("%s -> %s: %s\n", old_id_str,
6183 new_id_str ? new_id_str : "no-op change", logmsg);
6191 static const struct got_error *
6192 rebase_progress(void *arg, unsigned char status, const char *path)
6194 unsigned char *rebase_status = arg;
6196 while (path[0] == '/')
6198 printf("%c %s\n", status, path);
6200 if (*rebase_status == GOT_STATUS_CONFLICT)
6202 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6203 *rebase_status = status;
6207 static const struct got_error *
6208 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6209 struct got_reference *branch, struct got_reference *new_base_branch,
6210 struct got_reference *tmp_branch, struct got_repository *repo)
6212 printf("Switching work tree to %s\n", got_ref_get_name(branch));
6213 return got_worktree_rebase_complete(worktree, fileindex,
6214 new_base_branch, tmp_branch, branch, repo);
6217 static const struct got_error *
6218 rebase_commit(struct got_pathlist_head *merged_paths,
6219 struct got_worktree *worktree, struct got_fileindex *fileindex,
6220 struct got_reference *tmp_branch,
6221 struct got_object_id *commit_id, struct got_repository *repo)
6223 const struct got_error *error;
6224 struct got_commit_object *commit;
6225 struct got_object_id *new_commit_id;
6227 error = got_object_open_as_commit(&commit, repo, commit_id);
6231 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6232 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6234 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6236 error = show_rebase_progress(commit, commit_id, NULL);
6238 error = show_rebase_progress(commit, commit_id, new_commit_id);
6239 free(new_commit_id);
6242 got_object_commit_close(commit);
6246 struct check_path_prefix_arg {
6247 const char *path_prefix;
6252 static const struct got_error *
6253 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6254 struct got_blob_object *blob2, struct got_object_id *id1,
6255 struct got_object_id *id2, const char *path1, const char *path2,
6256 mode_t mode1, mode_t mode2, struct got_repository *repo)
6258 struct check_path_prefix_arg *a = arg;
6260 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6261 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6262 return got_error(a->errcode);
6267 static const struct got_error *
6268 check_path_prefix(struct got_object_id *parent_id,
6269 struct got_object_id *commit_id, const char *path_prefix,
6270 int errcode, struct got_repository *repo)
6272 const struct got_error *err;
6273 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6274 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6275 struct check_path_prefix_arg cpp_arg;
6277 if (got_path_is_root_dir(path_prefix))
6280 err = got_object_open_as_commit(&commit, repo, commit_id);
6284 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6288 err = got_object_open_as_tree(&tree1, repo,
6289 got_object_commit_get_tree_id(parent_commit));
6293 err = got_object_open_as_tree(&tree2, repo,
6294 got_object_commit_get_tree_id(commit));
6298 cpp_arg.path_prefix = path_prefix;
6299 while (cpp_arg.path_prefix[0] == '/')
6300 cpp_arg.path_prefix++;
6301 cpp_arg.len = strlen(cpp_arg.path_prefix);
6302 cpp_arg.errcode = errcode;
6303 err = got_diff_tree(tree1, tree2, "", "", repo,
6304 check_path_prefix_in_diff, &cpp_arg, 0);
6307 got_object_tree_close(tree1);
6309 got_object_tree_close(tree2);
6311 got_object_commit_close(commit);
6313 got_object_commit_close(parent_commit);
6317 static const struct got_error *
6318 collect_commits(struct got_object_id_queue *commits,
6319 struct got_object_id *initial_commit_id,
6320 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6321 const char *path_prefix, int path_prefix_errcode,
6322 struct got_repository *repo)
6324 const struct got_error *err = NULL;
6325 struct got_commit_graph *graph = NULL;
6326 struct got_object_id *parent_id = NULL;
6327 struct got_object_qid *qid;
6328 struct got_object_id *commit_id = initial_commit_id;
6330 err = got_commit_graph_open(&graph, "/", 1);
6334 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6335 check_cancelled, NULL);
6338 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6339 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6340 check_cancelled, NULL);
6342 if (err->code == GOT_ERR_ITER_COMPLETED) {
6343 err = got_error_msg(GOT_ERR_ANCESTRY,
6344 "ran out of commits to rebase before "
6345 "youngest common ancestor commit has "
6350 err = check_path_prefix(parent_id, commit_id,
6351 path_prefix, path_prefix_errcode, repo);
6355 err = got_object_qid_alloc(&qid, commit_id);
6358 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6359 commit_id = parent_id;
6363 got_commit_graph_close(graph);
6367 static const struct got_error *
6368 cmd_rebase(int argc, char *argv[])
6370 const struct got_error *error = NULL;
6371 struct got_worktree *worktree = NULL;
6372 struct got_repository *repo = NULL;
6373 struct got_fileindex *fileindex = NULL;
6375 struct got_reference *branch = NULL;
6376 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6377 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6378 struct got_object_id *resume_commit_id = NULL;
6379 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6380 struct got_commit_object *commit = NULL;
6381 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6382 int histedit_in_progress = 0;
6383 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6384 struct got_object_id_queue commits;
6385 struct got_pathlist_head merged_paths;
6386 const struct got_object_id_queue *parent_ids;
6387 struct got_object_qid *qid, *pid;
6389 SIMPLEQ_INIT(&commits);
6390 TAILQ_INIT(&merged_paths);
6392 while ((ch = getopt(argc, argv, "ac")) != -1) {
6398 continue_rebase = 1;
6410 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6411 "unveil", NULL) == -1)
6414 if (abort_rebase && continue_rebase)
6416 else if (abort_rebase || continue_rebase) {
6419 } else if (argc != 1)
6422 cwd = getcwd(NULL, 0);
6424 error = got_error_from_errno("getcwd");
6427 error = got_worktree_open(&worktree, cwd);
6431 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6436 error = apply_unveil(got_repo_get_path(repo), 0,
6437 got_worktree_get_root_path(worktree));
6441 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6445 if (histedit_in_progress) {
6446 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6450 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6456 if (!rebase_in_progress) {
6457 error = got_error(GOT_ERR_NOT_REBASING);
6460 error = got_worktree_rebase_continue(&resume_commit_id,
6461 &new_base_branch, &tmp_branch, &branch, &fileindex,
6465 printf("Switching work tree to %s\n",
6466 got_ref_get_symref_target(new_base_branch));
6467 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6468 new_base_branch, update_progress, &did_something);
6471 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6472 goto done; /* nothing else to do */
6475 if (continue_rebase) {
6476 if (!rebase_in_progress) {
6477 error = got_error(GOT_ERR_NOT_REBASING);
6480 error = got_worktree_rebase_continue(&resume_commit_id,
6481 &new_base_branch, &tmp_branch, &branch, &fileindex,
6486 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6487 resume_commit_id, repo);
6491 yca_id = got_object_id_dup(resume_commit_id);
6492 if (yca_id == NULL) {
6493 error = got_error_from_errno("got_object_id_dup");
6497 error = got_ref_open(&branch, repo, argv[0], 0);
6502 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6506 if (!continue_rebase) {
6507 struct got_object_id *base_commit_id;
6509 base_commit_id = got_worktree_get_base_commit_id(worktree);
6510 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6511 base_commit_id, branch_head_commit_id, repo,
6512 check_cancelled, NULL);
6515 if (yca_id == NULL) {
6516 error = got_error_msg(GOT_ERR_ANCESTRY,
6517 "specified branch shares no common ancestry "
6518 "with work tree's branch");
6522 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6524 if (error->code != GOT_ERR_ANCESTRY)
6528 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6529 "specified branch resolves to a commit which "
6530 "is already contained in work tree's branch");
6533 error = got_worktree_rebase_prepare(&new_base_branch,
6534 &tmp_branch, &fileindex, worktree, branch, repo);
6539 commit_id = branch_head_commit_id;
6540 error = got_object_open_as_commit(&commit, repo, commit_id);
6544 parent_ids = got_object_commit_get_parent_ids(commit);
6545 pid = SIMPLEQ_FIRST(parent_ids);
6547 if (!continue_rebase) {
6549 error = got_worktree_rebase_abort(worktree, fileindex,
6550 repo, new_base_branch, update_progress,
6554 printf("Rebase of %s aborted\n",
6555 got_ref_get_name(branch));
6557 error = got_error(GOT_ERR_EMPTY_REBASE);
6560 error = collect_commits(&commits, commit_id, pid->id,
6561 yca_id, got_worktree_get_path_prefix(worktree),
6562 GOT_ERR_REBASE_PATH, repo);
6563 got_object_commit_close(commit);
6568 if (SIMPLEQ_EMPTY(&commits)) {
6569 if (continue_rebase) {
6570 error = rebase_complete(worktree, fileindex,
6571 branch, new_base_branch, tmp_branch, repo);
6574 /* Fast-forward the reference of the branch. */
6575 struct got_object_id *new_head_commit_id;
6577 error = got_ref_resolve(&new_head_commit_id, repo,
6581 error = got_object_id_str(&id_str, new_head_commit_id);
6582 printf("Forwarding %s to commit %s\n",
6583 got_ref_get_name(branch), id_str);
6585 error = got_ref_change_ref(branch,
6586 new_head_commit_id);
6593 SIMPLEQ_FOREACH(qid, &commits, entry) {
6594 commit_id = qid->id;
6595 parent_id = pid ? pid->id : yca_id;
6598 error = got_worktree_rebase_merge_files(&merged_paths,
6599 worktree, fileindex, parent_id, commit_id, repo,
6600 rebase_progress, &rebase_status, check_cancelled, NULL);
6604 if (rebase_status == GOT_STATUS_CONFLICT) {
6605 error = show_rebase_merge_conflict(qid->id, repo);
6608 got_worktree_rebase_pathlist_free(&merged_paths);
6612 error = rebase_commit(&merged_paths, worktree, fileindex,
6613 tmp_branch, commit_id, repo);
6614 got_worktree_rebase_pathlist_free(&merged_paths);
6619 if (rebase_status == GOT_STATUS_CONFLICT) {
6620 error = got_worktree_rebase_postpone(worktree, fileindex);
6623 error = got_error_msg(GOT_ERR_CONFLICTS,
6624 "conflicts must be resolved before rebasing can continue");
6626 error = rebase_complete(worktree, fileindex, branch,
6627 new_base_branch, tmp_branch, repo);
6629 got_object_id_queue_free(&commits);
6630 free(branch_head_commit_id);
6631 free(resume_commit_id);
6634 got_object_commit_close(commit);
6636 got_ref_close(branch);
6637 if (new_base_branch)
6638 got_ref_close(new_base_branch);
6640 got_ref_close(tmp_branch);
6642 got_worktree_close(worktree);
6644 got_repo_close(repo);
6649 usage_histedit(void)
6651 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6656 #define GOT_HISTEDIT_PICK 'p'
6657 #define GOT_HISTEDIT_EDIT 'e'
6658 #define GOT_HISTEDIT_FOLD 'f'
6659 #define GOT_HISTEDIT_DROP 'd'
6660 #define GOT_HISTEDIT_MESG 'm'
6662 static struct got_histedit_cmd {
6666 } got_histedit_cmds[] = {
6667 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6668 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6669 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6671 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6672 { GOT_HISTEDIT_MESG, "mesg",
6673 "single-line log message for commit above (open editor if empty)" },
6676 struct got_histedit_list_entry {
6677 TAILQ_ENTRY(got_histedit_list_entry) entry;
6678 struct got_object_id *commit_id;
6679 const struct got_histedit_cmd *cmd;
6682 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6684 static const struct got_error *
6685 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6686 FILE *f, struct got_repository *repo)
6688 const struct got_error *err = NULL;
6689 char *logmsg = NULL, *id_str = NULL;
6690 struct got_commit_object *commit = NULL;
6693 err = got_object_open_as_commit(&commit, repo, commit_id);
6697 err = get_short_logmsg(&logmsg, 34, commit);
6701 err = got_object_id_str(&id_str, commit_id);
6705 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6707 err = got_ferror(f, GOT_ERR_IO);
6710 got_object_commit_close(commit);
6716 static const struct got_error *
6717 histedit_write_commit_list(struct got_object_id_queue *commits,
6718 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6720 const struct got_error *err = NULL;
6721 struct got_object_qid *qid;
6723 if (SIMPLEQ_EMPTY(commits))
6724 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6726 SIMPLEQ_FOREACH(qid, commits, entry) {
6727 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6731 if (edit_logmsg_only) {
6732 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6734 err = got_ferror(f, GOT_ERR_IO);
6743 static const struct got_error *
6744 write_cmd_list(FILE *f, const char *branch_name,
6745 struct got_object_id_queue *commits)
6747 const struct got_error *err = NULL;
6750 struct got_object_qid *qid;
6752 qid = SIMPLEQ_FIRST(commits);
6753 err = got_object_id_str(&id_str, qid->id);
6758 "# Editing the history of branch '%s' starting at\n"
6760 "# Commits will be processed in order from top to "
6761 "bottom of this file.\n", branch_name, id_str);
6763 err = got_ferror(f, GOT_ERR_IO);
6767 n = fprintf(f, "# Available histedit commands:\n");
6769 err = got_ferror(f, GOT_ERR_IO);
6773 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6774 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6775 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6778 err = got_ferror(f, GOT_ERR_IO);
6787 static const struct got_error *
6788 histedit_syntax_error(int lineno)
6790 static char msg[42];
6793 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6795 if (ret == -1 || ret >= sizeof(msg))
6796 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6798 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6801 static const struct got_error *
6802 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6803 char *logmsg, struct got_repository *repo)
6805 const struct got_error *err;
6806 struct got_commit_object *folded_commit = NULL;
6807 char *id_str, *folded_logmsg = NULL;
6809 err = got_object_id_str(&id_str, hle->commit_id);
6813 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6817 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6820 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6821 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6822 folded_logmsg) == -1) {
6823 err = got_error_from_errno("asprintf");
6827 got_object_commit_close(folded_commit);
6829 free(folded_logmsg);
6833 static struct got_histedit_list_entry *
6834 get_folded_commits(struct got_histedit_list_entry *hle)
6836 struct got_histedit_list_entry *prev, *folded = NULL;
6838 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6839 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6840 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6841 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6843 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6849 static const struct got_error *
6850 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6851 struct got_repository *repo)
6853 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6854 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6855 const struct got_error *err = NULL;
6856 struct got_commit_object *commit = NULL;
6858 struct got_histedit_list_entry *folded = NULL;
6860 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6864 folded = get_folded_commits(hle);
6866 while (folded != hle) {
6867 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6868 folded = TAILQ_NEXT(folded, entry);
6871 err = append_folded_commit_msg(&new_msg, folded,
6877 folded = TAILQ_NEXT(folded, entry);
6881 err = got_object_id_str(&id_str, hle->commit_id);
6884 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6887 if (asprintf(&new_msg,
6888 "%s\n# original log message of commit %s: %s",
6889 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6890 err = got_error_from_errno("asprintf");
6896 err = got_object_id_str(&id_str, hle->commit_id);
6900 err = got_opentemp_named_fd(&logmsg_path, &fd,
6901 GOT_TMPDIR_STR "/got-logmsg");
6905 dprintf(fd, logmsg);
6908 err = get_editor(&editor);
6912 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6914 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6916 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6919 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6920 err = got_error_from_errno2("unlink", logmsg_path);
6926 got_object_commit_close(commit);
6930 static const struct got_error *
6931 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6932 FILE *f, struct got_repository *repo)
6934 const struct got_error *err = NULL;
6935 char *line = NULL, *p, *end;
6939 const struct got_histedit_cmd *cmd;
6940 struct got_object_id *commit_id = NULL;
6941 struct got_histedit_list_entry *hle = NULL;
6944 len = getline(&line, &size, f);
6946 const struct got_error *getline_err;
6949 getline_err = got_error_from_errno("getline");
6950 err = got_ferror(f, getline_err->code);
6955 while (isspace((unsigned char)p[0]))
6957 if (p[0] == '#' || p[0] == '\0') {
6963 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6964 cmd = &got_histedit_cmds[i];
6965 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6966 isspace((unsigned char)p[strlen(cmd->name)])) {
6967 p += strlen(cmd->name);
6970 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6975 if (i == nitems(got_histedit_cmds)) {
6976 err = histedit_syntax_error(lineno);
6979 while (isspace((unsigned char)p[0]))
6981 if (cmd->code == GOT_HISTEDIT_MESG) {
6982 if (hle == NULL || hle->logmsg != NULL) {
6983 err = got_error(GOT_ERR_HISTEDIT_CMD);
6987 err = histedit_edit_logmsg(hle, repo);
6991 hle->logmsg = strdup(p);
6992 if (hle->logmsg == NULL) {
6993 err = got_error_from_errno("strdup");
7002 while (end[0] && !isspace((unsigned char)end[0]))
7006 err = got_object_resolve_id_str(&commit_id, repo, p);
7008 /* override error code */
7009 err = histedit_syntax_error(lineno);
7013 hle = malloc(sizeof(*hle));
7015 err = got_error_from_errno("malloc");
7019 hle->commit_id = commit_id;
7024 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7032 static const struct got_error *
7033 histedit_check_script(struct got_histedit_list *histedit_cmds,
7034 struct got_object_id_queue *commits, struct got_repository *repo)
7036 const struct got_error *err = NULL;
7037 struct got_object_qid *qid;
7038 struct got_histedit_list_entry *hle;
7039 static char msg[92];
7042 if (TAILQ_EMPTY(histedit_cmds))
7043 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7044 "histedit script contains no commands");
7045 if (SIMPLEQ_EMPTY(commits))
7046 return got_error(GOT_ERR_EMPTY_HISTEDIT);
7048 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7049 struct got_histedit_list_entry *hle2;
7050 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7053 if (got_object_id_cmp(hle->commit_id,
7054 hle2->commit_id) != 0)
7056 err = got_object_id_str(&id_str, hle->commit_id);
7059 snprintf(msg, sizeof(msg), "commit %s is listed "
7060 "more than once in histedit script", id_str);
7062 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7066 SIMPLEQ_FOREACH(qid, commits, entry) {
7067 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7068 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7072 err = got_object_id_str(&id_str, qid->id);
7075 snprintf(msg, sizeof(msg),
7076 "commit %s missing from histedit script", id_str);
7078 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7082 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7083 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7084 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7085 "last commit in histedit script cannot be folded");
7090 static const struct got_error *
7091 histedit_run_editor(struct got_histedit_list *histedit_cmds,
7092 const char *path, struct got_object_id_queue *commits,
7093 struct got_repository *repo)
7095 const struct got_error *err = NULL;
7099 err = get_editor(&editor);
7103 if (spawn_editor(editor, path) == -1) {
7104 err = got_error_from_errno("failed spawning editor");
7108 f = fopen(path, "r");
7110 err = got_error_from_errno("fopen");
7113 err = histedit_parse_list(histedit_cmds, f, repo);
7117 err = histedit_check_script(histedit_cmds, commits, repo);
7119 if (f && fclose(f) != 0 && err == NULL)
7120 err = got_error_from_errno("fclose");
7125 static const struct got_error *
7126 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7127 struct got_object_id_queue *, const char *, const char *,
7128 struct got_repository *);
7130 static const struct got_error *
7131 histedit_edit_script(struct got_histedit_list *histedit_cmds,
7132 struct got_object_id_queue *commits, const char *branch_name,
7133 int edit_logmsg_only, struct got_repository *repo)
7135 const struct got_error *err;
7139 err = got_opentemp_named(&path, &f, "got-histedit");
7143 err = write_cmd_list(f, branch_name, commits);
7147 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7151 if (edit_logmsg_only) {
7153 err = histedit_parse_list(histedit_cmds, f, repo);
7155 if (fclose(f) != 0) {
7156 err = got_error_from_errno("fclose");
7160 err = histedit_run_editor(histedit_cmds, path, commits, repo);
7162 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7163 err->code != GOT_ERR_HISTEDIT_CMD)
7165 err = histedit_edit_list_retry(histedit_cmds, err,
7166 commits, path, branch_name, repo);
7170 if (f && fclose(f) != 0 && err == NULL)
7171 err = got_error_from_errno("fclose");
7172 if (path && unlink(path) != 0 && err == NULL)
7173 err = got_error_from_errno2("unlink", path);
7178 static const struct got_error *
7179 histedit_save_list(struct got_histedit_list *histedit_cmds,
7180 struct got_worktree *worktree, struct got_repository *repo)
7182 const struct got_error *err = NULL;
7185 struct got_histedit_list_entry *hle;
7186 struct got_commit_object *commit = NULL;
7188 err = got_worktree_get_histedit_script_path(&path, worktree);
7192 f = fopen(path, "w");
7194 err = got_error_from_errno2("fopen", path);
7197 TAILQ_FOREACH(hle, histedit_cmds, entry) {
7198 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7204 int n = fprintf(f, "%c %s\n",
7205 GOT_HISTEDIT_MESG, hle->logmsg);
7207 err = got_ferror(f, GOT_ERR_IO);
7213 if (f && fclose(f) != 0 && err == NULL)
7214 err = got_error_from_errno("fclose");
7217 got_object_commit_close(commit);
7222 histedit_free_list(struct got_histedit_list *histedit_cmds)
7224 struct got_histedit_list_entry *hle;
7226 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7227 TAILQ_REMOVE(histedit_cmds, hle, entry);
7232 static const struct got_error *
7233 histedit_load_list(struct got_histedit_list *histedit_cmds,
7234 const char *path, struct got_repository *repo)
7236 const struct got_error *err = NULL;
7239 f = fopen(path, "r");
7241 err = got_error_from_errno2("fopen", path);
7245 err = histedit_parse_list(histedit_cmds, f, repo);
7247 if (f && fclose(f) != 0 && err == NULL)
7248 err = got_error_from_errno("fclose");
7252 static const struct got_error *
7253 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7254 const struct got_error *edit_err, struct got_object_id_queue *commits,
7255 const char *path, const char *branch_name, struct got_repository *repo)
7257 const struct got_error *err = NULL, *prev_err = edit_err;
7260 while (resp != 'c' && resp != 'r' && resp != 'a') {
7261 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7262 "or (a)bort: ", getprogname(), prev_err->msg);
7267 histedit_free_list(histedit_cmds);
7268 err = histedit_run_editor(histedit_cmds, path, commits,
7271 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7272 err->code != GOT_ERR_HISTEDIT_CMD)
7279 } else if (resp == 'r') {
7280 histedit_free_list(histedit_cmds);
7281 err = histedit_edit_script(histedit_cmds,
7282 commits, branch_name, 0, repo);
7284 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7285 err->code != GOT_ERR_HISTEDIT_CMD)
7292 } else if (resp == 'a') {
7293 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7296 printf("invalid response '%c'\n", resp);
7302 static const struct got_error *
7303 histedit_complete(struct got_worktree *worktree,
7304 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7305 struct got_reference *branch, struct got_repository *repo)
7307 printf("Switching work tree to %s\n",
7308 got_ref_get_symref_target(branch));
7309 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7313 static const struct got_error *
7314 show_histedit_progress(struct got_commit_object *commit,
7315 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7317 const struct got_error *err;
7318 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7320 err = got_object_id_str(&old_id_str, hle->commit_id);
7325 err = got_object_id_str(&new_id_str, new_id);
7330 old_id_str[12] = '\0';
7332 new_id_str[12] = '\0';
7335 logmsg = strdup(hle->logmsg);
7336 if (logmsg == NULL) {
7337 err = got_error_from_errno("strdup");
7340 trim_logmsg(logmsg, 42);
7342 err = get_short_logmsg(&logmsg, 42, commit);
7347 switch (hle->cmd->code) {
7348 case GOT_HISTEDIT_PICK:
7349 case GOT_HISTEDIT_EDIT:
7350 printf("%s -> %s: %s\n", old_id_str,
7351 new_id_str ? new_id_str : "no-op change", logmsg);
7353 case GOT_HISTEDIT_DROP:
7354 case GOT_HISTEDIT_FOLD:
7355 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7367 static const struct got_error *
7368 histedit_commit(struct got_pathlist_head *merged_paths,
7369 struct got_worktree *worktree, struct got_fileindex *fileindex,
7370 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7371 struct got_repository *repo)
7373 const struct got_error *err;
7374 struct got_commit_object *commit;
7375 struct got_object_id *new_commit_id;
7377 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7378 && hle->logmsg == NULL) {
7379 err = histedit_edit_logmsg(hle, repo);
7384 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7388 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7389 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7392 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7394 err = show_histedit_progress(commit, hle, NULL);
7396 err = show_histedit_progress(commit, hle, new_commit_id);
7397 free(new_commit_id);
7400 got_object_commit_close(commit);
7404 static const struct got_error *
7405 histedit_skip_commit(struct got_histedit_list_entry *hle,
7406 struct got_worktree *worktree, struct got_repository *repo)
7408 const struct got_error *error;
7409 struct got_commit_object *commit;
7411 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7416 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7420 error = show_histedit_progress(commit, hle, NULL);
7421 got_object_commit_close(commit);
7425 static const struct got_error *
7426 check_local_changes(void *arg, unsigned char status,
7427 unsigned char staged_status, const char *path,
7428 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7429 struct got_object_id *commit_id, int dirfd, const char *de_name)
7431 int *have_local_changes = arg;
7434 case GOT_STATUS_ADD:
7435 case GOT_STATUS_DELETE:
7436 case GOT_STATUS_MODIFY:
7437 case GOT_STATUS_CONFLICT:
7438 *have_local_changes = 1;
7439 return got_error(GOT_ERR_CANCELLED);
7444 switch (staged_status) {
7445 case GOT_STATUS_ADD:
7446 case GOT_STATUS_DELETE:
7447 case GOT_STATUS_MODIFY:
7448 *have_local_changes = 1;
7449 return got_error(GOT_ERR_CANCELLED);
7457 static const struct got_error *
7458 cmd_histedit(int argc, char *argv[])
7460 const struct got_error *error = NULL;
7461 struct got_worktree *worktree = NULL;
7462 struct got_fileindex *fileindex = NULL;
7463 struct got_repository *repo = NULL;
7465 struct got_reference *branch = NULL;
7466 struct got_reference *tmp_branch = NULL;
7467 struct got_object_id *resume_commit_id = NULL;
7468 struct got_object_id *base_commit_id = NULL;
7469 struct got_object_id *head_commit_id = NULL;
7470 struct got_commit_object *commit = NULL;
7471 int ch, rebase_in_progress = 0, did_something;
7472 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7473 int edit_logmsg_only = 0;
7474 const char *edit_script_path = NULL;
7475 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7476 struct got_object_id_queue commits;
7477 struct got_pathlist_head merged_paths;
7478 const struct got_object_id_queue *parent_ids;
7479 struct got_object_qid *pid;
7480 struct got_histedit_list histedit_cmds;
7481 struct got_histedit_list_entry *hle;
7483 SIMPLEQ_INIT(&commits);
7484 TAILQ_INIT(&histedit_cmds);
7485 TAILQ_INIT(&merged_paths);
7487 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7496 edit_script_path = optarg;
7499 edit_logmsg_only = 1;
7511 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7512 "unveil", NULL) == -1)
7515 if (abort_edit && continue_edit)
7516 errx(1, "histedit's -a and -c options are mutually exclusive");
7517 if (edit_script_path && edit_logmsg_only)
7518 errx(1, "histedit's -F and -m options are mutually exclusive");
7519 if (abort_edit && edit_logmsg_only)
7520 errx(1, "histedit's -a and -m options are mutually exclusive");
7521 if (continue_edit && edit_logmsg_only)
7522 errx(1, "histedit's -c and -m options are mutually exclusive");
7527 * This command cannot apply unveil(2) in all cases because the
7528 * user may choose to run an editor to edit the histedit script
7529 * and to edit individual commit log messages.
7530 * unveil(2) traverses exec(2); if an editor is used we have to
7531 * apply unveil after edit script and log messages have been written.
7532 * XXX TODO: Make use of unveil(2) where possible.
7535 cwd = getcwd(NULL, 0);
7537 error = got_error_from_errno("getcwd");
7540 error = got_worktree_open(&worktree, cwd);
7544 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7549 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7552 if (rebase_in_progress) {
7553 error = got_error(GOT_ERR_REBASING);
7557 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7561 if (edit_in_progress && edit_logmsg_only) {
7562 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7563 "histedit operation is in progress in this "
7564 "work tree and must be continued or aborted "
7565 "before the -m option can be used");
7569 if (edit_in_progress && abort_edit) {
7570 error = got_worktree_histedit_continue(&resume_commit_id,
7571 &tmp_branch, &branch, &base_commit_id, &fileindex,
7575 printf("Switching work tree to %s\n",
7576 got_ref_get_symref_target(branch));
7577 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7578 branch, base_commit_id, update_progress, &did_something);
7581 printf("Histedit of %s aborted\n",
7582 got_ref_get_symref_target(branch));
7583 goto done; /* nothing else to do */
7584 } else if (abort_edit) {
7585 error = got_error(GOT_ERR_NOT_HISTEDIT);
7589 if (continue_edit) {
7592 if (!edit_in_progress) {
7593 error = got_error(GOT_ERR_NOT_HISTEDIT);
7597 error = got_worktree_get_histedit_script_path(&path, worktree);
7601 error = histedit_load_list(&histedit_cmds, path, repo);
7606 error = got_worktree_histedit_continue(&resume_commit_id,
7607 &tmp_branch, &branch, &base_commit_id, &fileindex,
7612 error = got_ref_resolve(&head_commit_id, repo, branch);
7616 error = got_object_open_as_commit(&commit, repo,
7620 parent_ids = got_object_commit_get_parent_ids(commit);
7621 pid = SIMPLEQ_FIRST(parent_ids);
7623 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7626 error = collect_commits(&commits, head_commit_id, pid->id,
7627 base_commit_id, got_worktree_get_path_prefix(worktree),
7628 GOT_ERR_HISTEDIT_PATH, repo);
7629 got_object_commit_close(commit);
7634 if (edit_in_progress) {
7635 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7639 error = got_ref_open(&branch, repo,
7640 got_worktree_get_head_ref_name(worktree), 0);
7644 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7645 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7646 "will not edit commit history of a branch outside "
7647 "the \"refs/heads/\" reference namespace");
7651 error = got_ref_resolve(&head_commit_id, repo, branch);
7652 got_ref_close(branch);
7657 error = got_object_open_as_commit(&commit, repo,
7661 parent_ids = got_object_commit_get_parent_ids(commit);
7662 pid = SIMPLEQ_FIRST(parent_ids);
7664 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7667 error = collect_commits(&commits, head_commit_id, pid->id,
7668 got_worktree_get_base_commit_id(worktree),
7669 got_worktree_get_path_prefix(worktree),
7670 GOT_ERR_HISTEDIT_PATH, repo);
7671 got_object_commit_close(commit);
7676 if (SIMPLEQ_EMPTY(&commits)) {
7677 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7681 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7682 &base_commit_id, &fileindex, worktree, repo);
7686 if (edit_script_path) {
7687 error = histedit_load_list(&histedit_cmds,
7688 edit_script_path, repo);
7690 got_worktree_histedit_abort(worktree, fileindex,
7691 repo, branch, base_commit_id,
7692 update_progress, &did_something);
7696 const char *branch_name;
7697 branch_name = got_ref_get_symref_target(branch);
7698 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7700 error = histedit_edit_script(&histedit_cmds, &commits,
7701 branch_name, edit_logmsg_only, repo);
7703 got_worktree_histedit_abort(worktree, fileindex,
7704 repo, branch, base_commit_id,
7705 update_progress, &did_something);
7711 error = histedit_save_list(&histedit_cmds, worktree,
7714 got_worktree_histedit_abort(worktree, fileindex,
7715 repo, branch, base_commit_id,
7716 update_progress, &did_something);
7722 error = histedit_check_script(&histedit_cmds, &commits, repo);
7726 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7727 if (resume_commit_id) {
7728 if (got_object_id_cmp(hle->commit_id,
7729 resume_commit_id) != 0)
7732 resume_commit_id = NULL;
7733 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7734 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7735 error = histedit_skip_commit(hle, worktree,
7740 struct got_pathlist_head paths;
7741 int have_changes = 0;
7744 error = got_pathlist_append(&paths, "", NULL);
7747 error = got_worktree_status(worktree, &paths,
7748 repo, check_local_changes, &have_changes,
7749 check_cancelled, NULL);
7750 got_pathlist_free(&paths);
7752 if (error->code != GOT_ERR_CANCELLED)
7754 if (sigint_received || sigpipe_received)
7758 error = histedit_commit(NULL, worktree,
7759 fileindex, tmp_branch, hle, repo);
7763 error = got_object_open_as_commit(
7764 &commit, repo, hle->commit_id);
7767 error = show_histedit_progress(commit,
7769 got_object_commit_close(commit);
7778 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7779 error = histedit_skip_commit(hle, worktree, repo);
7785 error = got_object_open_as_commit(&commit, repo,
7789 parent_ids = got_object_commit_get_parent_ids(commit);
7790 pid = SIMPLEQ_FIRST(parent_ids);
7792 error = got_worktree_histedit_merge_files(&merged_paths,
7793 worktree, fileindex, pid->id, hle->commit_id, repo,
7794 rebase_progress, &rebase_status, check_cancelled, NULL);
7797 got_object_commit_close(commit);
7800 if (rebase_status == GOT_STATUS_CONFLICT) {
7801 error = show_rebase_merge_conflict(hle->commit_id,
7805 got_worktree_rebase_pathlist_free(&merged_paths);
7809 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7811 error = got_object_id_str(&id_str, hle->commit_id);
7814 printf("Stopping histedit for amending commit %s\n",
7817 got_worktree_rebase_pathlist_free(&merged_paths);
7818 error = got_worktree_histedit_postpone(worktree,
7823 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7824 error = histedit_skip_commit(hle, worktree, repo);
7830 error = histedit_commit(&merged_paths, worktree, fileindex,
7831 tmp_branch, hle, repo);
7832 got_worktree_rebase_pathlist_free(&merged_paths);
7837 if (rebase_status == GOT_STATUS_CONFLICT) {
7838 error = got_worktree_histedit_postpone(worktree, fileindex);
7841 error = got_error_msg(GOT_ERR_CONFLICTS,
7842 "conflicts must be resolved before histedit can continue");
7844 error = histedit_complete(worktree, fileindex, tmp_branch,
7847 got_object_id_queue_free(&commits);
7848 histedit_free_list(&histedit_cmds);
7849 free(head_commit_id);
7850 free(base_commit_id);
7851 free(resume_commit_id);
7853 got_object_commit_close(commit);
7855 got_ref_close(branch);
7857 got_ref_close(tmp_branch);
7859 got_worktree_close(worktree);
7861 got_repo_close(repo);
7866 usage_integrate(void)
7868 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7872 static const struct got_error *
7873 cmd_integrate(int argc, char *argv[])
7875 const struct got_error *error = NULL;
7876 struct got_repository *repo = NULL;
7877 struct got_worktree *worktree = NULL;
7878 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7879 const char *branch_arg = NULL;
7880 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7881 struct got_fileindex *fileindex = NULL;
7882 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7883 int ch, did_something = 0;
7885 while ((ch = getopt(argc, argv, "")) != -1) {
7898 branch_arg = argv[0];
7900 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7901 "unveil", NULL) == -1)
7904 cwd = getcwd(NULL, 0);
7906 error = got_error_from_errno("getcwd");
7910 error = got_worktree_open(&worktree, cwd);
7914 error = check_rebase_or_histedit_in_progress(worktree);
7918 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7923 error = apply_unveil(got_repo_get_path(repo), 0,
7924 got_worktree_get_root_path(worktree));
7928 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7929 error = got_error_from_errno("asprintf");
7933 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7934 &base_branch_ref, worktree, refname, repo);
7938 refname = strdup(got_ref_get_name(branch_ref));
7939 if (refname == NULL) {
7940 error = got_error_from_errno("strdup");
7941 got_worktree_integrate_abort(worktree, fileindex, repo,
7942 branch_ref, base_branch_ref);
7945 base_refname = strdup(got_ref_get_name(base_branch_ref));
7946 if (base_refname == NULL) {
7947 error = got_error_from_errno("strdup");
7948 got_worktree_integrate_abort(worktree, fileindex, repo,
7949 branch_ref, base_branch_ref);
7953 error = got_ref_resolve(&commit_id, repo, branch_ref);
7957 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7961 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7962 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7963 "specified branch has already been integrated");
7964 got_worktree_integrate_abort(worktree, fileindex, repo,
7965 branch_ref, base_branch_ref);
7969 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7971 if (error->code == GOT_ERR_ANCESTRY)
7972 error = got_error(GOT_ERR_REBASE_REQUIRED);
7973 got_worktree_integrate_abort(worktree, fileindex, repo,
7974 branch_ref, base_branch_ref);
7978 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7979 branch_ref, base_branch_ref, update_progress, &did_something,
7980 check_cancelled, NULL);
7984 printf("Integrated %s into %s\n", refname, base_refname);
7987 got_repo_close(repo);
7989 got_worktree_close(worktree);
7991 free(base_commit_id);
8001 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8002 "[file-path ...]\n",
8007 static const struct got_error *
8008 print_stage(void *arg, unsigned char status, unsigned char staged_status,
8009 const char *path, struct got_object_id *blob_id,
8010 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8011 int dirfd, const char *de_name)
8013 const struct got_error *err = NULL;
8014 char *id_str = NULL;
8016 if (staged_status != GOT_STATUS_ADD &&
8017 staged_status != GOT_STATUS_MODIFY &&
8018 staged_status != GOT_STATUS_DELETE)
8021 if (staged_status == GOT_STATUS_ADD ||
8022 staged_status == GOT_STATUS_MODIFY)
8023 err = got_object_id_str(&id_str, staged_blob_id);
8025 err = got_object_id_str(&id_str, blob_id);
8029 printf("%s %c %s\n", id_str, staged_status, path);
8034 static const struct got_error *
8035 cmd_stage(int argc, char *argv[])
8037 const struct got_error *error = NULL;
8038 struct got_repository *repo = NULL;
8039 struct got_worktree *worktree = NULL;
8041 struct got_pathlist_head paths;
8042 struct got_pathlist_entry *pe;
8043 int ch, list_stage = 0, pflag = 0;
8044 FILE *patch_script_file = NULL;
8045 const char *patch_script_path = NULL;
8046 struct choose_patch_arg cpa;
8050 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8059 patch_script_path = optarg;
8071 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8072 "unveil", NULL) == -1)
8075 if (list_stage && (pflag || patch_script_path))
8076 errx(1, "-l option cannot be used with other options");
8077 if (patch_script_path && !pflag)
8078 errx(1, "-F option can only be used together with -p option");
8080 cwd = getcwd(NULL, 0);
8082 error = got_error_from_errno("getcwd");
8086 error = got_worktree_open(&worktree, cwd);
8090 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8095 if (patch_script_path) {
8096 patch_script_file = fopen(patch_script_path, "r");
8097 if (patch_script_file == NULL) {
8098 error = got_error_from_errno2("fopen",
8103 error = apply_unveil(got_repo_get_path(repo), 0,
8104 got_worktree_get_root_path(worktree));
8108 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8113 error = got_worktree_status(worktree, &paths, repo,
8114 print_stage, NULL, check_cancelled, NULL);
8116 cpa.patch_script_file = patch_script_file;
8117 cpa.action = "stage";
8118 error = got_worktree_stage(worktree, &paths,
8119 pflag ? NULL : print_status, NULL,
8120 pflag ? choose_patch : NULL, &cpa, repo);
8123 if (patch_script_file && fclose(patch_script_file) == EOF &&
8125 error = got_error_from_errno2("fclose", patch_script_path);
8127 got_repo_close(repo);
8129 got_worktree_close(worktree);
8130 TAILQ_FOREACH(pe, &paths, entry)
8131 free((char *)pe->path);
8132 got_pathlist_free(&paths);
8140 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8141 "[file-path ...]\n",
8147 static const struct got_error *
8148 cmd_unstage(int argc, char *argv[])
8150 const struct got_error *error = NULL;
8151 struct got_repository *repo = NULL;
8152 struct got_worktree *worktree = NULL;
8154 struct got_pathlist_head paths;
8155 struct got_pathlist_entry *pe;
8156 int ch, did_something = 0, pflag = 0;
8157 FILE *patch_script_file = NULL;
8158 const char *patch_script_path = NULL;
8159 struct choose_patch_arg cpa;
8163 while ((ch = getopt(argc, argv, "pF:")) != -1) {
8169 patch_script_path = optarg;
8181 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8182 "unveil", NULL) == -1)
8185 if (patch_script_path && !pflag)
8186 errx(1, "-F option can only be used together with -p option");
8188 cwd = getcwd(NULL, 0);
8190 error = got_error_from_errno("getcwd");
8194 error = got_worktree_open(&worktree, cwd);
8198 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8203 if (patch_script_path) {
8204 patch_script_file = fopen(patch_script_path, "r");
8205 if (patch_script_file == NULL) {
8206 error = got_error_from_errno2("fopen",
8212 error = apply_unveil(got_repo_get_path(repo), 0,
8213 got_worktree_get_root_path(worktree));
8217 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8221 cpa.patch_script_file = patch_script_file;
8222 cpa.action = "unstage";
8223 error = got_worktree_unstage(worktree, &paths, update_progress,
8224 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8226 if (patch_script_file && fclose(patch_script_file) == EOF &&
8228 error = got_error_from_errno2("fclose", patch_script_path);
8230 got_repo_close(repo);
8232 got_worktree_close(worktree);
8233 TAILQ_FOREACH(pe, &paths, entry)
8234 free((char *)pe->path);
8235 got_pathlist_free(&paths);
8243 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8244 "arg1 [arg2 ...]\n", getprogname());
8248 static const struct got_error *
8249 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8251 const struct got_error *err;
8252 struct got_blob_object *blob;
8254 err = got_object_open_as_blob(&blob, repo, id, 8192);
8258 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8259 got_object_blob_close(blob);
8263 static const struct got_error *
8264 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8266 const struct got_error *err;
8267 struct got_tree_object *tree;
8270 err = got_object_open_as_tree(&tree, repo, id);
8274 nentries = got_object_tree_get_nentries(tree);
8275 for (i = 0; i < nentries; i++) {
8276 struct got_tree_entry *te;
8278 if (sigint_received || sigpipe_received)
8280 te = got_object_tree_get_entry(tree, i);
8281 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8284 fprintf(outfile, "%s %.7o %s\n", id_str,
8285 got_tree_entry_get_mode(te),
8286 got_tree_entry_get_name(te));
8290 got_object_tree_close(tree);
8294 static const struct got_error *
8295 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8297 const struct got_error *err;
8298 struct got_commit_object *commit;
8299 const struct got_object_id_queue *parent_ids;
8300 struct got_object_qid *pid;
8301 char *id_str = NULL;
8302 const char *logmsg = NULL;
8304 err = got_object_open_as_commit(&commit, repo, id);
8308 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8312 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8313 parent_ids = got_object_commit_get_parent_ids(commit);
8314 fprintf(outfile, "numparents %d\n",
8315 got_object_commit_get_nparents(commit));
8316 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8318 err = got_object_id_str(&pid_str, pid->id);
8321 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8324 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8325 got_object_commit_get_author(commit),
8326 got_object_commit_get_author_time(commit));
8328 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8329 got_object_commit_get_author(commit),
8330 got_object_commit_get_committer_time(commit));
8332 logmsg = got_object_commit_get_logmsg_raw(commit);
8333 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8334 fprintf(outfile, "%s", logmsg);
8337 got_object_commit_close(commit);
8341 static const struct got_error *
8342 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8344 const struct got_error *err;
8345 struct got_tag_object *tag;
8346 char *id_str = NULL;
8347 const char *tagmsg = NULL;
8349 err = got_object_open_as_tag(&tag, repo, id);
8353 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8357 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8359 switch (got_object_tag_get_object_type(tag)) {
8360 case GOT_OBJ_TYPE_BLOB:
8361 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8362 GOT_OBJ_LABEL_BLOB);
8364 case GOT_OBJ_TYPE_TREE:
8365 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8366 GOT_OBJ_LABEL_TREE);
8368 case GOT_OBJ_TYPE_COMMIT:
8369 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8370 GOT_OBJ_LABEL_COMMIT);
8372 case GOT_OBJ_TYPE_TAG:
8373 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8380 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8381 got_object_tag_get_name(tag));
8383 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8384 got_object_tag_get_tagger(tag),
8385 got_object_tag_get_tagger_time(tag));
8387 tagmsg = got_object_tag_get_message(tag);
8388 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8389 fprintf(outfile, "%s", tagmsg);
8392 got_object_tag_close(tag);
8396 static const struct got_error *
8397 cmd_cat(int argc, char *argv[])
8399 const struct got_error *error;
8400 struct got_repository *repo = NULL;
8401 struct got_worktree *worktree = NULL;
8402 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8403 const char *commit_id_str = NULL;
8404 struct got_object_id *id = NULL, *commit_id = NULL;
8405 int ch, obj_type, i, force_path = 0;
8408 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8413 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8416 commit_id_str = optarg;
8419 repo_path = realpath(optarg, NULL);
8420 if (repo_path == NULL)
8421 return got_error_from_errno2("realpath",
8423 got_path_strip_trailing_slashes(repo_path);
8437 cwd = getcwd(NULL, 0);
8439 error = got_error_from_errno("getcwd");
8442 error = got_worktree_open(&worktree, cwd);
8443 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8446 if (repo_path == NULL) {
8448 got_worktree_get_repo_path(worktree));
8449 if (repo_path == NULL) {
8450 error = got_error_from_errno("strdup");
8456 if (repo_path == NULL) {
8457 repo_path = getcwd(NULL, 0);
8458 if (repo_path == NULL)
8459 return got_error_from_errno("getcwd");
8462 error = got_repo_open(&repo, repo_path, NULL);
8467 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8471 if (commit_id_str == NULL)
8472 commit_id_str = GOT_REF_HEAD;
8473 error = got_repo_match_object_id(&commit_id, NULL,
8474 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8478 for (i = 0; i < argc; i++) {
8480 error = got_object_id_by_path(&id, repo, commit_id,
8485 error = got_repo_match_object_id(&id, &label, argv[i],
8486 GOT_OBJ_TYPE_ANY, 0, repo);
8488 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8489 error->code != GOT_ERR_NOT_REF)
8491 error = got_object_id_by_path(&id, repo,
8492 commit_id, argv[i]);
8498 error = got_object_get_type(&obj_type, repo, id);
8503 case GOT_OBJ_TYPE_BLOB:
8504 error = cat_blob(id, repo, stdout);
8506 case GOT_OBJ_TYPE_TREE:
8507 error = cat_tree(id, repo, stdout);
8509 case GOT_OBJ_TYPE_COMMIT:
8510 error = cat_commit(id, repo, stdout);
8512 case GOT_OBJ_TYPE_TAG:
8513 error = cat_tag(id, repo, stdout);
8516 error = got_error(GOT_ERR_OBJ_TYPE);
8531 got_worktree_close(worktree);
8533 const struct got_error *repo_error;
8534 repo_error = got_repo_close(repo);