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 [-m] [-q] [-v] repository-url "
815 "[target-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 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
906 int verbosity = 0, mirror_references = 0;
909 TAILQ_INIT(&symrefs);
911 while ((ch = getopt(argc, argv, "mvq")) != -1) {
914 mirror_references = 1;
919 else if (verbosity < 3)
942 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
943 &repo_name, argv[0]);
947 if (strcmp(proto, "git") == 0) {
949 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
950 "sendfd dns inet unveil", NULL) == -1)
953 git_url = strdup(argv[0]);
954 if (git_url == NULL) {
955 error = got_error_from_errno("strdup");
958 } else if (strcmp(proto, "git+ssh") == 0 ||
959 strcmp(proto, "ssh") == 0) {
961 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
962 "sendfd unveil", NULL) == -1)
965 if (asprintf(&git_url, "ssh://%s:%s/%s", host, port,
966 server_path) == -1) {
967 error = got_error_from_errno("asprintf");
970 } else if (strcmp(proto, "http") == 0 ||
971 strcmp(proto, "git+http") == 0) {
972 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
975 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
978 if (dirname == NULL) {
979 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
980 error = got_error_from_errno("asprintf");
983 repo_path = default_destdir;
987 error = got_path_mkdir(repo_path);
991 error = got_repo_init(repo_path);
995 error = got_repo_open(&repo, repo_path, NULL);
999 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1000 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1001 error = got_error_from_errno2("unveil",
1002 GOT_FETCH_PATH_SSH);
1006 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1010 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1016 printf("Connected to %s:%s\n", host, port);
1018 /* Create a config file git-fetch(1) can understand. */
1019 gitconfig_path = got_repo_get_path_gitconfig(repo);
1020 if (gitconfig_path == NULL) {
1021 error = got_error_from_errno("got_repo_get_path_gitconfig");
1024 gitconfig_file = fopen(gitconfig_path, "a");
1025 if (gitconfig_file == NULL) {
1026 error = got_error_from_errno2("fopen", gitconfig_path);
1029 if (mirror_references) {
1030 if (asprintf(&gitconfig,
1033 "\tmirror = true\n",
1034 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1035 error = got_error_from_errno("asprintf");
1039 if (asprintf(&gitconfig,
1042 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1043 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1044 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1045 error = got_error_from_errno("asprintf");
1049 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1050 if (n != strlen(gitconfig)) {
1051 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1055 fpa.last_scaled_size[0] = '\0';
1056 fpa.last_p_indexed = -1;
1057 fpa.last_p_resolved = -1;
1058 fpa.verbosity = verbosity;
1059 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1060 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1061 fetchfd, repo, fetch_progress, &fpa);
1065 error = got_object_id_str(&id_str, pack_hash);
1069 printf("\nFetched %s.pack\n", id_str);
1072 /* Set up references provided with the pack file. */
1073 TAILQ_FOREACH(pe, &refs, entry) {
1074 const char *refname = pe->path;
1075 struct got_object_id *id = pe->data;
1076 struct got_reference *ref;
1077 char *remote_refname;
1079 error = got_ref_alloc(&ref, refname, id);
1082 error = got_ref_write(ref, repo);
1087 if (mirror_references)
1090 if (strncmp("refs/heads/", refname, 11) != 0)
1093 if (asprintf(&remote_refname,
1094 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1095 refname + 11) == -1) {
1096 error = got_error_from_errno("asprintf");
1099 error = got_ref_alloc(&ref, remote_refname, id);
1102 error = got_ref_write(ref, repo);
1108 /* Set the HEAD reference if the server provided one. */
1109 TAILQ_FOREACH(pe, &symrefs, entry) {
1110 struct got_reference *symref, *target_ref;
1111 const char *refname = pe->path;
1112 const char *target = pe->data;
1114 if (strcmp(refname, GOT_REF_HEAD) != 0)
1117 error = got_ref_open(&target_ref, repo, target, 0);
1119 if (error->code == GOT_ERR_NOT_REF)
1124 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1125 got_ref_close(target_ref);
1130 printf("Setting %s to %s\n", GOT_REF_HEAD,
1131 got_ref_get_symref_target(symref));
1133 error = got_ref_write(symref, repo);
1134 got_ref_close(symref);
1139 printf("Created %s repository '%s'\n",
1140 mirror_references ? "mirrored" : "cloned", repo_path);
1142 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1143 error = got_error_from_errno("close");
1144 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1145 error = got_error_from_errno("fclose");
1147 got_repo_close(repo);
1148 TAILQ_FOREACH(pe, &refs, entry) {
1149 free((void *)pe->path);
1152 got_pathlist_free(&refs);
1153 TAILQ_FOREACH(pe, &symrefs, entry) {
1154 free((void *)pe->path);
1157 got_pathlist_free(&symrefs);
1164 free(default_destdir);
1165 free(gitconfig_path);
1170 static const struct got_error *
1171 create_ref(const char *refname, struct got_object_id *id,
1172 const char *id_str, struct got_repository *repo)
1174 const struct got_error *err = NULL;
1175 struct got_reference *ref;
1177 printf("Creating %s: %s\n", refname, id_str);
1179 err = got_ref_alloc(&ref, refname, id);
1183 err = got_ref_write(ref, repo);
1188 static const struct got_error *
1189 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1190 struct got_repository *repo)
1192 const struct got_error *err = NULL;
1193 char *new_id_str = NULL;
1194 struct got_object_id *old_id = NULL;
1196 err = got_object_id_str(&new_id_str, new_id);
1200 if (got_ref_is_symbolic(ref)) {
1201 struct got_reference *new_ref;
1202 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1205 printf("Deleting symbolic reference %s -> %s\n",
1206 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1207 err = got_ref_delete(ref, repo);
1210 printf("Setting %s to %s\n", got_ref_get_name(ref),
1212 err = got_ref_write(new_ref, repo);
1216 err = got_ref_resolve(&old_id, repo, ref);
1219 if (got_object_id_cmp(old_id, new_id) != 0) {
1220 printf("Setting %s to %s\n",
1221 got_ref_get_name(ref), new_id_str);
1222 err = got_ref_change_ref(ref, new_id);
1225 err = got_ref_write(ref, repo);
1239 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1240 "[remote-repository-name]\n", getprogname());
1244 static const struct got_error *
1245 cmd_fetch(int argc, char *argv[])
1247 const struct got_error *error = NULL;
1248 char *cwd = NULL, *repo_path = NULL;
1249 const char *remote_name;
1250 char *proto = NULL, *host = NULL, *port = NULL;
1251 char *repo_name = NULL, *server_path = NULL;
1252 struct got_remote_repo *remotes, *remote = NULL;
1254 char *id_str = NULL;
1255 struct got_repository *repo = NULL;
1256 struct got_worktree *worktree = NULL;
1257 struct got_pathlist_head refs, symrefs;
1258 struct got_pathlist_entry *pe;
1259 struct got_object_id *pack_hash = NULL;
1260 int i, ch, fetchfd = -1;
1261 struct got_fetch_progress_arg fpa;
1265 TAILQ_INIT(&symrefs);
1267 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1270 repo_path = realpath(optarg, NULL);
1271 if (repo_path == NULL)
1272 return got_error_from_errno2("realpath",
1274 got_path_strip_trailing_slashes(repo_path);
1279 else if (verbosity < 3)
1294 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1296 remote_name = argv[0];
1300 cwd = getcwd(NULL, 0);
1302 error = got_error_from_errno("getcwd");
1306 if (repo_path == NULL) {
1307 error = got_worktree_open(&worktree, cwd);
1308 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1314 strdup(got_worktree_get_repo_path(worktree));
1315 if (repo_path == NULL)
1316 error = got_error_from_errno("strdup");
1320 repo_path = strdup(cwd);
1321 if (repo_path == NULL) {
1322 error = got_error_from_errno("strdup");
1328 error = got_repo_open(&repo, repo_path, NULL);
1332 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1333 for (i = 0; i < nremotes; i++) {
1334 remote = &remotes[i];
1335 if (strcmp(remote->name, remote_name) == 0)
1338 if (i == nremotes) {
1339 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1343 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1344 &repo_name, remote->url);
1348 if (strcmp(proto, "git") == 0) {
1350 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1351 "sendfd dns inet unveil", NULL) == -1)
1354 } else if (strcmp(proto, "git+ssh") == 0 ||
1355 strcmp(proto, "ssh") == 0) {
1357 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1358 "sendfd unveil", NULL) == -1)
1361 } else if (strcmp(proto, "http") == 0 ||
1362 strcmp(proto, "git+http") == 0) {
1363 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1366 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1370 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1371 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1372 error = got_error_from_errno2("unveil",
1373 GOT_FETCH_PATH_SSH);
1377 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1381 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1387 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1389 fpa.last_scaled_size[0] = '\0';
1390 fpa.last_p_indexed = -1;
1391 fpa.last_p_resolved = -1;
1392 fpa.verbosity = verbosity;
1393 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1394 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1398 if (pack_hash == NULL) {
1400 printf("Already up-to-date\n");
1404 if (verbosity >= 0) {
1405 error = got_object_id_str(&id_str, pack_hash);
1408 printf("\nFetched %s.pack\n", id_str);
1413 /* Update references provided with the pack file. */
1414 TAILQ_FOREACH(pe, &refs, entry) {
1415 const char *refname = pe->path;
1416 struct got_object_id *id = pe->data;
1417 struct got_reference *ref;
1418 char *remote_refname;
1420 error = got_object_id_str(&id_str, id);
1424 if (strncmp("refs/tags/", refname, 10) == 0) {
1425 error = got_ref_open(&ref, repo, refname, 0);
1427 if (error->code != GOT_ERR_NOT_REF)
1429 error = create_ref(refname, id, id_str, repo);
1433 error = update_ref(ref, id, repo);
1438 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1439 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1440 remote_name, refname + 11) == -1) {
1441 error = got_error_from_errno("asprintf");
1445 error = got_ref_open(&ref, repo, remote_refname, 0);
1447 if (error->code != GOT_ERR_NOT_REF)
1449 error = create_ref(remote_refname, id, id_str,
1454 error = update_ref(ref, id, repo);
1464 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1465 error = got_error_from_errno("close");
1467 got_repo_close(repo);
1469 got_worktree_close(worktree);
1470 TAILQ_FOREACH(pe, &refs, entry) {
1471 free((void *)pe->path);
1474 got_pathlist_free(&refs);
1475 TAILQ_FOREACH(pe, &symrefs, entry) {
1476 free((void *)pe->path);
1479 got_pathlist_free(&symrefs);
1494 usage_checkout(void)
1496 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1497 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1502 show_worktree_base_ref_warning(void)
1504 fprintf(stderr, "%s: warning: could not create a reference "
1505 "to the work tree's base commit; the commit could be "
1506 "garbage-collected by Git; making the repository "
1507 "writable and running 'got update' will prevent this\n",
1511 struct got_checkout_progress_arg {
1512 const char *worktree_path;
1513 int had_base_commit_ref_error;
1516 static const struct got_error *
1517 checkout_progress(void *arg, unsigned char status, const char *path)
1519 struct got_checkout_progress_arg *a = arg;
1521 /* Base commit bump happens silently. */
1522 if (status == GOT_STATUS_BUMP_BASE)
1525 if (status == GOT_STATUS_BASE_REF_ERR) {
1526 a->had_base_commit_ref_error = 1;
1530 while (path[0] == '/')
1533 printf("%c %s/%s\n", status, a->worktree_path, path);
1537 static const struct got_error *
1538 check_cancelled(void *arg)
1540 if (sigint_received || sigpipe_received)
1541 return got_error(GOT_ERR_CANCELLED);
1545 static const struct got_error *
1546 check_linear_ancestry(struct got_object_id *commit_id,
1547 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1548 struct got_repository *repo)
1550 const struct got_error *err = NULL;
1551 struct got_object_id *yca_id;
1553 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1554 commit_id, base_commit_id, repo, check_cancelled, NULL);
1559 return got_error(GOT_ERR_ANCESTRY);
1562 * Require a straight line of history between the target commit
1563 * and the work tree's base commit.
1565 * Non-linear situations such as this require a rebase:
1567 * (commit) D F (base_commit)
1575 * 'got update' only handles linear cases:
1576 * Update forwards in time: A (base/yca) - B - C - D (commit)
1577 * Update backwards in time: D (base) - C - B - A (commit/yca)
1579 if (allow_forwards_in_time_only) {
1580 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1581 return got_error(GOT_ERR_ANCESTRY);
1582 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1583 got_object_id_cmp(base_commit_id, yca_id) != 0)
1584 return got_error(GOT_ERR_ANCESTRY);
1590 static const struct got_error *
1591 check_same_branch(struct got_object_id *commit_id,
1592 struct got_reference *head_ref, struct got_object_id *yca_id,
1593 struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_commit_graph *graph = NULL;
1597 struct got_object_id *head_commit_id = NULL;
1598 int is_same_branch = 0;
1600 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1604 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1608 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1613 err = got_commit_graph_open(&graph, "/", 1);
1617 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1618 check_cancelled, NULL);
1623 struct got_object_id *id;
1624 err = got_commit_graph_iter_next(&id, graph, repo,
1625 check_cancelled, NULL);
1627 if (err->code == GOT_ERR_ITER_COMPLETED)
1633 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1635 if (got_object_id_cmp(id, commit_id) == 0) {
1643 got_commit_graph_close(graph);
1644 free(head_commit_id);
1645 if (!err && !is_same_branch)
1646 err = got_error(GOT_ERR_ANCESTRY);
1650 static const struct got_error *
1651 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1653 static char msg[512];
1654 const char *branch_name;
1656 if (got_ref_is_symbolic(ref))
1657 branch_name = got_ref_get_symref_target(ref);
1659 branch_name = got_ref_get_name(ref);
1661 if (strncmp("refs/heads/", branch_name, 11) == 0)
1664 snprintf(msg, sizeof(msg),
1665 "target commit is not contained in branch '%s'; "
1666 "the branch to use must be specified with -b; "
1667 "if necessary a new branch can be created for "
1668 "this commit with 'got branch -c %s BRANCH_NAME'",
1669 branch_name, commit_id_str);
1671 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1674 static const struct got_error *
1675 cmd_checkout(int argc, char *argv[])
1677 const struct got_error *error = NULL;
1678 struct got_repository *repo = NULL;
1679 struct got_reference *head_ref = NULL;
1680 struct got_worktree *worktree = NULL;
1681 char *repo_path = NULL;
1682 char *worktree_path = NULL;
1683 const char *path_prefix = "";
1684 const char *branch_name = GOT_REF_HEAD;
1685 char *commit_id_str = NULL;
1686 int ch, same_path_prefix, allow_nonempty = 0;
1687 struct got_pathlist_head paths;
1688 struct got_checkout_progress_arg cpa;
1692 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1695 branch_name = optarg;
1698 commit_id_str = strdup(optarg);
1699 if (commit_id_str == NULL)
1700 return got_error_from_errno("strdup");
1706 path_prefix = optarg;
1718 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1719 "unveil", NULL) == -1)
1723 char *cwd, *base, *dotgit;
1724 repo_path = realpath(argv[0], NULL);
1725 if (repo_path == NULL)
1726 return got_error_from_errno2("realpath", argv[0]);
1727 cwd = getcwd(NULL, 0);
1729 error = got_error_from_errno("getcwd");
1732 if (path_prefix[0]) {
1733 base = basename(path_prefix);
1735 error = got_error_from_errno2("basename",
1740 base = basename(repo_path);
1742 error = got_error_from_errno2("basename",
1747 dotgit = strstr(base, ".git");
1750 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1751 error = got_error_from_errno("asprintf");
1756 } else if (argc == 2) {
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL) {
1759 error = got_error_from_errno2("realpath", argv[0]);
1762 worktree_path = realpath(argv[1], NULL);
1763 if (worktree_path == NULL) {
1764 if (errno != ENOENT) {
1765 error = got_error_from_errno2("realpath",
1769 worktree_path = strdup(argv[1]);
1770 if (worktree_path == NULL) {
1771 error = got_error_from_errno("strdup");
1778 got_path_strip_trailing_slashes(repo_path);
1779 got_path_strip_trailing_slashes(worktree_path);
1781 error = got_repo_open(&repo, repo_path, NULL);
1785 /* Pre-create work tree path for unveil(2) */
1786 error = got_path_mkdir(worktree_path);
1788 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1789 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1791 if (!allow_nonempty &&
1792 !got_path_dir_is_empty(worktree_path)) {
1793 error = got_error_path(worktree_path,
1794 GOT_ERR_DIR_NOT_EMPTY);
1799 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1803 error = got_ref_open(&head_ref, repo, branch_name, 0);
1807 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1808 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1811 error = got_worktree_open(&worktree, worktree_path);
1815 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1819 if (!same_path_prefix) {
1820 error = got_error(GOT_ERR_PATH_PREFIX);
1824 if (commit_id_str) {
1825 struct got_object_id *commit_id;
1826 error = got_repo_match_object_id(&commit_id, NULL,
1827 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1830 error = check_linear_ancestry(commit_id,
1831 got_worktree_get_base_commit_id(worktree), 0, repo);
1832 if (error != NULL) {
1834 if (error->code == GOT_ERR_ANCESTRY) {
1835 error = checkout_ancestry_error(
1836 head_ref, commit_id_str);
1840 error = check_same_branch(commit_id, head_ref, NULL, repo);
1842 if (error->code == GOT_ERR_ANCESTRY) {
1843 error = checkout_ancestry_error(
1844 head_ref, commit_id_str);
1848 error = got_worktree_set_base_commit_id(worktree, repo,
1855 error = got_pathlist_append(&paths, "", NULL);
1858 cpa.worktree_path = worktree_path;
1859 cpa.had_base_commit_ref_error = 0;
1860 error = got_worktree_checkout_files(worktree, &paths, repo,
1861 checkout_progress, &cpa, check_cancelled, NULL);
1865 printf("Now shut up and hack\n");
1866 if (cpa.had_base_commit_ref_error)
1867 show_worktree_base_ref_warning();
1869 got_pathlist_free(&paths);
1870 free(commit_id_str);
1872 free(worktree_path);
1879 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1884 static const struct got_error *
1885 update_progress(void *arg, unsigned char status, const char *path)
1887 int *did_something = arg;
1889 if (status == GOT_STATUS_EXISTS ||
1890 status == GOT_STATUS_BASE_REF_ERR)
1895 /* Base commit bump happens silently. */
1896 if (status == GOT_STATUS_BUMP_BASE)
1899 while (path[0] == '/')
1901 printf("%c %s\n", status, path);
1905 static const struct got_error *
1906 switch_head_ref(struct got_reference *head_ref,
1907 struct got_object_id *commit_id, struct got_worktree *worktree,
1908 struct got_repository *repo)
1910 const struct got_error *err = NULL;
1912 int ref_has_moved = 0;
1914 /* Trivial case: switching between two different references. */
1915 if (strcmp(got_ref_get_name(head_ref),
1916 got_worktree_get_head_ref_name(worktree)) != 0) {
1917 printf("Switching work tree from %s to %s\n",
1918 got_worktree_get_head_ref_name(worktree),
1919 got_ref_get_name(head_ref));
1920 return got_worktree_set_head_ref(worktree, head_ref);
1923 err = check_linear_ancestry(commit_id,
1924 got_worktree_get_base_commit_id(worktree), 0, repo);
1926 if (err->code != GOT_ERR_ANCESTRY)
1933 /* Switching to a rebased branch with the same reference name. */
1934 err = got_object_id_str(&base_id_str,
1935 got_worktree_get_base_commit_id(worktree));
1938 printf("Reference %s now points at a different branch\n",
1939 got_worktree_get_head_ref_name(worktree));
1940 printf("Switching work tree from %s to %s\n", base_id_str,
1941 got_worktree_get_head_ref_name(worktree));
1945 static const struct got_error *
1946 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1948 const struct got_error *err;
1951 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1955 return got_error(GOT_ERR_REBASING);
1957 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1961 return got_error(GOT_ERR_HISTEDIT_BUSY);
1966 static const struct got_error *
1967 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1968 char *argv[], struct got_worktree *worktree)
1970 const struct got_error *err = NULL;
1977 return got_error_from_errno("strdup");
1978 return got_pathlist_append(paths, path, NULL);
1981 for (i = 0; i < argc; i++) {
1982 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1985 err = got_pathlist_append(paths, path, NULL);
1995 static const struct got_error *
1996 cmd_update(int argc, char *argv[])
1998 const struct got_error *error = NULL;
1999 struct got_repository *repo = NULL;
2000 struct got_worktree *worktree = NULL;
2001 char *worktree_path = NULL;
2002 struct got_object_id *commit_id = NULL;
2003 char *commit_id_str = NULL;
2004 const char *branch_name = NULL;
2005 struct got_reference *head_ref = NULL;
2006 struct got_pathlist_head paths;
2007 struct got_pathlist_entry *pe;
2008 int ch, did_something = 0;
2012 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2015 branch_name = optarg;
2018 commit_id_str = strdup(optarg);
2019 if (commit_id_str == NULL)
2020 return got_error_from_errno("strdup");
2032 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2033 "unveil", NULL) == -1)
2036 worktree_path = getcwd(NULL, 0);
2037 if (worktree_path == NULL) {
2038 error = got_error_from_errno("getcwd");
2041 error = got_worktree_open(&worktree, worktree_path);
2045 error = check_rebase_or_histedit_in_progress(worktree);
2049 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2054 error = apply_unveil(got_repo_get_path(repo), 0,
2055 got_worktree_get_root_path(worktree));
2059 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2063 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2064 got_worktree_get_head_ref_name(worktree), 0);
2067 if (commit_id_str == NULL) {
2068 error = got_ref_resolve(&commit_id, repo, head_ref);
2071 error = got_object_id_str(&commit_id_str, commit_id);
2075 error = got_repo_match_object_id(&commit_id, NULL,
2076 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2077 free(commit_id_str);
2078 commit_id_str = NULL;
2081 error = got_object_id_str(&commit_id_str, commit_id);
2087 struct got_object_id *head_commit_id;
2088 TAILQ_FOREACH(pe, &paths, entry) {
2089 if (pe->path_len == 0)
2091 error = got_error_msg(GOT_ERR_BAD_PATH,
2092 "switching between branches requires that "
2093 "the entire work tree gets updated");
2096 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2099 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2101 free(head_commit_id);
2104 error = check_same_branch(commit_id, head_ref, NULL, repo);
2107 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2111 error = check_linear_ancestry(commit_id,
2112 got_worktree_get_base_commit_id(worktree), 0, repo);
2113 if (error != NULL) {
2114 if (error->code == GOT_ERR_ANCESTRY)
2115 error = got_error(GOT_ERR_BRANCH_MOVED);
2118 error = check_same_branch(commit_id, head_ref, NULL, repo);
2123 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2125 error = got_worktree_set_base_commit_id(worktree, repo,
2131 error = got_worktree_checkout_files(worktree, &paths, repo,
2132 update_progress, &did_something, check_cancelled, NULL);
2137 printf("Updated to commit %s\n", commit_id_str);
2139 printf("Already up-to-date\n");
2141 free(worktree_path);
2142 TAILQ_FOREACH(pe, &paths, entry)
2143 free((char *)pe->path);
2144 got_pathlist_free(&paths);
2146 free(commit_id_str);
2150 static const struct got_error *
2151 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2152 const char *path, int diff_context, int ignore_whitespace,
2153 struct got_repository *repo)
2155 const struct got_error *err = NULL;
2156 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2159 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2164 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2168 while (path[0] == '/')
2170 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2171 ignore_whitespace, stdout);
2174 got_object_blob_close(blob1);
2175 got_object_blob_close(blob2);
2179 static const struct got_error *
2180 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2181 const char *path, int diff_context, int ignore_whitespace,
2182 struct got_repository *repo)
2184 const struct got_error *err = NULL;
2185 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2186 struct got_diff_blob_output_unidiff_arg arg;
2189 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2194 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2198 arg.diff_context = diff_context;
2199 arg.ignore_whitespace = ignore_whitespace;
2200 arg.outfile = stdout;
2201 while (path[0] == '/')
2203 err = got_diff_tree(tree1, tree2, path, path, repo,
2204 got_diff_blob_output_unidiff, &arg, 1);
2207 got_object_tree_close(tree1);
2209 got_object_tree_close(tree2);
2213 static const struct got_error *
2214 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2215 const char *path, int diff_context, struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_commit_object *pcommit = NULL;
2219 char *id_str1 = NULL, *id_str2 = NULL;
2220 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2221 struct got_object_qid *qid;
2223 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2225 err = got_object_open_as_commit(&pcommit, repo,
2231 if (path && path[0] != '\0') {
2233 err = got_object_id_by_path(&obj_id2, repo, id, path);
2236 err = got_object_id_str(&id_str2, obj_id2);
2242 err = got_object_id_by_path(&obj_id1, repo,
2248 err = got_object_id_str(&id_str1, obj_id1);
2254 err = got_object_get_type(&obj_type, repo, obj_id2);
2259 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2261 case GOT_OBJ_TYPE_BLOB:
2262 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2265 case GOT_OBJ_TYPE_TREE:
2266 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2270 err = got_error(GOT_ERR_OBJ_TYPE);
2276 obj_id2 = got_object_commit_get_tree_id(commit);
2277 err = got_object_id_str(&id_str2, obj_id2);
2280 obj_id1 = got_object_commit_get_tree_id(pcommit);
2281 err = got_object_id_str(&id_str1, obj_id1);
2284 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2285 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2291 got_object_commit_close(pcommit);
2296 get_datestr(time_t *time, char *datebuf)
2298 struct tm mytm, *tm;
2301 tm = gmtime_r(time, &mytm);
2304 s = asctime_r(tm, datebuf);
2307 p = strchr(s, '\n');
2313 static const struct got_error *
2314 match_logmsg(int *have_match, struct got_object_id *id,
2315 struct got_commit_object *commit, regex_t *regex)
2317 const struct got_error *err = NULL;
2318 regmatch_t regmatch;
2319 char *id_str = NULL, *logmsg = NULL;
2323 err = got_object_id_str(&id_str, id);
2327 err = got_object_commit_get_logmsg(&logmsg, commit);
2331 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2339 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2341 static const struct got_error *
2342 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2343 struct got_repository *repo, const char *path, int show_patch,
2344 int diff_context, struct got_reflist_head *refs)
2346 const struct got_error *err = NULL;
2347 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2349 time_t committer_time;
2350 const char *author, *committer;
2351 char *refs_str = NULL;
2352 struct got_reflist_entry *re;
2354 SIMPLEQ_FOREACH(re, refs, entry) {
2357 struct got_tag_object *tag = NULL;
2360 name = got_ref_get_name(re->ref);
2361 if (strcmp(name, GOT_REF_HEAD) == 0)
2363 if (strncmp(name, "refs/", 5) == 0)
2365 if (strncmp(name, "got/", 4) == 0)
2367 if (strncmp(name, "heads/", 6) == 0)
2369 if (strncmp(name, "remotes/", 8) == 0)
2371 if (strncmp(name, "tags/", 5) == 0) {
2372 err = got_object_open_as_tag(&tag, repo, re->id);
2374 if (err->code != GOT_ERR_OBJ_TYPE)
2376 /* Ref points at something other than a tag. */
2381 cmp = got_object_id_cmp(tag ?
2382 got_object_tag_get_object_id(tag) : re->id, id);
2384 got_object_tag_close(tag);
2388 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2390 err = got_error_from_errno("asprintf");
2396 err = got_object_id_str(&id_str, id);
2400 printf(GOT_COMMIT_SEP_STR);
2401 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2402 refs_str ? refs_str : "", refs_str ? ")" : "");
2407 printf("from: %s\n", got_object_commit_get_author(commit));
2408 committer_time = got_object_commit_get_committer_time(commit);
2409 datestr = get_datestr(&committer_time, datebuf);
2411 printf("date: %s UTC\n", datestr);
2412 author = got_object_commit_get_author(commit);
2413 committer = got_object_commit_get_committer(commit);
2414 if (strcmp(author, committer) != 0)
2415 printf("via: %s\n", committer);
2416 if (got_object_commit_get_nparents(commit) > 1) {
2417 const struct got_object_id_queue *parent_ids;
2418 struct got_object_qid *qid;
2420 parent_ids = got_object_commit_get_parent_ids(commit);
2421 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2422 err = got_object_id_str(&id_str, qid->id);
2425 printf("parent %d: %s\n", n++, id_str);
2430 err = got_object_commit_get_logmsg(&logmsg0, commit);
2436 line = strsep(&logmsg, "\n");
2438 printf(" %s\n", line);
2443 err = print_patch(commit, id, path, diff_context, repo);
2448 if (fflush(stdout) != 0 && err == NULL)
2449 err = got_error_from_errno("fflush");
2453 static const struct got_error *
2454 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2455 const char *path, int show_patch, const char *search_pattern,
2456 int diff_context, int limit, int log_branches,
2457 struct got_reflist_head *refs)
2459 const struct got_error *err;
2460 struct got_commit_graph *graph;
2464 if (search_pattern &&
2465 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2466 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2468 err = got_commit_graph_open(&graph, path, !log_branches);
2471 err = got_commit_graph_iter_start(graph, root_id, repo,
2472 check_cancelled, NULL);
2476 struct got_commit_object *commit;
2477 struct got_object_id *id;
2479 if (sigint_received || sigpipe_received)
2482 err = got_commit_graph_iter_next(&id, graph, repo,
2483 check_cancelled, NULL);
2485 if (err->code == GOT_ERR_ITER_COMPLETED)
2492 err = got_object_open_as_commit(&commit, repo, id);
2496 if (search_pattern) {
2497 err = match_logmsg(&have_match, id, commit, ®ex);
2499 got_object_commit_close(commit);
2502 if (have_match == 0) {
2503 got_object_commit_close(commit);
2508 err = print_commit(commit, id, repo, path, show_patch,
2509 diff_context, refs);
2510 got_object_commit_close(commit);
2511 if (err || (limit && --limit == 0))
2517 got_commit_graph_close(graph);
2524 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2525 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2530 get_default_log_limit(void)
2532 const char *got_default_log_limit;
2536 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2537 if (got_default_log_limit == NULL)
2539 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2545 static const struct got_error *
2546 cmd_log(int argc, char *argv[])
2548 const struct got_error *error;
2549 struct got_repository *repo = NULL;
2550 struct got_worktree *worktree = NULL;
2551 struct got_commit_object *commit = NULL;
2552 struct got_object_id *id = NULL;
2553 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2554 const char *start_commit = NULL, *search_pattern = NULL;
2555 int diff_context = -1, ch;
2556 int show_patch = 0, limit = 0, log_branches = 0;
2558 struct got_reflist_head refs;
2560 SIMPLEQ_INIT(&refs);
2563 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2569 limit = get_default_log_limit();
2571 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2577 start_commit = optarg;
2580 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2583 err(1, "-C option %s", errstr);
2586 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2588 err(1, "-l option %s", errstr);
2594 repo_path = realpath(optarg, NULL);
2595 if (repo_path == NULL)
2596 return got_error_from_errno2("realpath",
2598 got_path_strip_trailing_slashes(repo_path);
2601 search_pattern = optarg;
2612 if (diff_context == -1)
2614 else if (!show_patch)
2615 errx(1, "-C reguires -p");
2617 cwd = getcwd(NULL, 0);
2619 error = got_error_from_errno("getcwd");
2623 error = got_worktree_open(&worktree, cwd);
2624 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2631 error = got_error_from_errno("strdup");
2634 } else if (argc == 1) {
2636 error = got_worktree_resolve_path(&path, worktree,
2641 path = strdup(argv[0]);
2643 error = got_error_from_errno("strdup");
2650 if (repo_path == NULL) {
2651 repo_path = worktree ?
2652 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2654 if (repo_path == NULL) {
2655 error = got_error_from_errno("strdup");
2659 error = got_repo_open(&repo, repo_path, NULL);
2663 error = apply_unveil(got_repo_get_path(repo), 1,
2664 worktree ? got_worktree_get_root_path(worktree) : NULL);
2668 if (start_commit == NULL) {
2669 struct got_reference *head_ref;
2670 error = got_ref_open(&head_ref, repo,
2671 worktree ? got_worktree_get_head_ref_name(worktree)
2675 error = got_ref_resolve(&id, repo, head_ref);
2676 got_ref_close(head_ref);
2679 error = got_object_open_as_commit(&commit, repo, id);
2681 struct got_reference *ref;
2682 error = got_ref_open(&ref, repo, start_commit, 0);
2683 if (error == NULL) {
2685 error = got_ref_resolve(&id, repo, ref);
2689 error = got_object_get_type(&obj_type, repo, id);
2692 if (obj_type == GOT_OBJ_TYPE_TAG) {
2693 struct got_tag_object *tag;
2694 error = got_object_open_as_tag(&tag, repo, id);
2697 if (got_object_tag_get_object_type(tag) !=
2698 GOT_OBJ_TYPE_COMMIT) {
2699 got_object_tag_close(tag);
2700 error = got_error(GOT_ERR_OBJ_TYPE);
2704 id = got_object_id_dup(
2705 got_object_tag_get_object_id(tag));
2707 error = got_error_from_errno(
2708 "got_object_id_dup");
2709 got_object_tag_close(tag);
2712 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2713 error = got_error(GOT_ERR_OBJ_TYPE);
2716 error = got_object_open_as_commit(&commit, repo, id);
2720 if (commit == NULL) {
2721 error = got_repo_match_object_id_prefix(&id,
2722 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2731 const char *prefix = got_worktree_get_path_prefix(worktree);
2733 if (asprintf(&p, "%s%s%s", prefix,
2734 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2735 error = got_error_from_errno("asprintf");
2738 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2741 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2746 path = in_repo_path;
2749 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2753 error = print_commits(id, repo, path, show_patch, search_pattern,
2754 diff_context, limit, log_branches, &refs);
2761 got_worktree_close(worktree);
2763 const struct got_error *repo_error;
2764 repo_error = got_repo_close(repo);
2768 got_ref_list_free(&refs);
2775 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2776 "[-w] [object1 object2 | path]\n", getprogname());
2780 struct print_diff_arg {
2781 struct got_repository *repo;
2782 struct got_worktree *worktree;
2787 int ignore_whitespace;
2790 static const struct got_error *
2791 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2792 const char *path, struct got_object_id *blob_id,
2793 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2794 int dirfd, const char *de_name)
2796 struct print_diff_arg *a = arg;
2797 const struct got_error *err = NULL;
2798 struct got_blob_object *blob1 = NULL;
2801 char *abspath = NULL, *label1 = NULL;
2804 if (a->diff_staged) {
2805 if (staged_status != GOT_STATUS_MODIFY &&
2806 staged_status != GOT_STATUS_ADD &&
2807 staged_status != GOT_STATUS_DELETE)
2810 if (staged_status == GOT_STATUS_DELETE)
2812 if (status == GOT_STATUS_NONEXISTENT)
2813 return got_error_set_errno(ENOENT, path);
2814 if (status != GOT_STATUS_MODIFY &&
2815 status != GOT_STATUS_ADD &&
2816 status != GOT_STATUS_DELETE &&
2817 status != GOT_STATUS_CONFLICT)
2821 if (!a->header_shown) {
2822 printf("diff %s %s%s\n", a->id_str,
2823 got_worktree_get_root_path(a->worktree),
2824 a->diff_staged ? " (staged changes)" : "");
2825 a->header_shown = 1;
2828 if (a->diff_staged) {
2829 const char *label1 = NULL, *label2 = NULL;
2830 switch (staged_status) {
2831 case GOT_STATUS_MODIFY:
2835 case GOT_STATUS_ADD:
2838 case GOT_STATUS_DELETE:
2842 return got_error(GOT_ERR_FILE_STATUS);
2844 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2845 label1, label2, a->diff_context, a->ignore_whitespace,
2849 if (staged_status == GOT_STATUS_ADD ||
2850 staged_status == GOT_STATUS_MODIFY) {
2852 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2856 err = got_object_id_str(&id_str, staged_blob_id);
2859 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2860 err = got_error_from_errno("asprintf");
2865 } else if (status != GOT_STATUS_ADD) {
2866 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2871 if (status != GOT_STATUS_DELETE) {
2872 if (asprintf(&abspath, "%s/%s",
2873 got_worktree_get_root_path(a->worktree), path) == -1) {
2874 err = got_error_from_errno("asprintf");
2879 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2881 err = got_error_from_errno2("openat", abspath);
2885 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2887 err = got_error_from_errno2("open", abspath);
2891 if (fstat(fd, &sb) == -1) {
2892 err = got_error_from_errno2("fstat", abspath);
2895 f2 = fdopen(fd, "r");
2897 err = got_error_from_errno2("fdopen", abspath);
2904 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2905 a->diff_context, a->ignore_whitespace, stdout);
2908 got_object_blob_close(blob1);
2909 if (f2 && fclose(f2) == EOF && err == NULL)
2910 err = got_error_from_errno("fclose");
2911 if (fd != -1 && close(fd) == -1 && err == NULL)
2912 err = got_error_from_errno("close");
2917 static const struct got_error *
2918 cmd_diff(int argc, char *argv[])
2920 const struct got_error *error;
2921 struct got_repository *repo = NULL;
2922 struct got_worktree *worktree = NULL;
2923 char *cwd = NULL, *repo_path = NULL;
2924 struct got_object_id *id1 = NULL, *id2 = NULL;
2925 const char *id_str1 = NULL, *id_str2 = NULL;
2926 char *label1 = NULL, *label2 = NULL;
2928 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2933 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2938 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2941 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2944 err(1, "-C option %s", errstr);
2947 repo_path = realpath(optarg, NULL);
2948 if (repo_path == NULL)
2949 return got_error_from_errno2("realpath",
2951 got_path_strip_trailing_slashes(repo_path);
2957 ignore_whitespace = 1;
2968 cwd = getcwd(NULL, 0);
2970 error = got_error_from_errno("getcwd");
2973 error = got_worktree_open(&worktree, cwd);
2974 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2977 if (worktree == NULL) {
2978 error = got_error(GOT_ERR_NOT_WORKTREE);
2983 "-r option can't be used when diffing a work tree");
2984 repo_path = strdup(got_worktree_get_repo_path(worktree));
2985 if (repo_path == NULL) {
2986 error = got_error_from_errno("strdup");
2990 error = got_worktree_resolve_path(&path, worktree,
2997 error = got_error_from_errno("strdup");
3001 } else if (argc == 2) {
3003 errx(1, "-s option can't be used when diffing "
3004 "objects in repository");
3007 if (worktree && repo_path == NULL) {
3009 strdup(got_worktree_get_repo_path(worktree));
3010 if (repo_path == NULL) {
3011 error = got_error_from_errno("strdup");
3018 if (repo_path == NULL) {
3019 repo_path = getcwd(NULL, 0);
3020 if (repo_path == NULL)
3021 return got_error_from_errno("getcwd");
3024 error = got_repo_open(&repo, repo_path, NULL);
3029 error = apply_unveil(got_repo_get_path(repo), 1,
3030 worktree ? got_worktree_get_root_path(worktree) : NULL);
3035 struct print_diff_arg arg;
3036 struct got_pathlist_head paths;
3041 error = got_object_id_str(&id_str,
3042 got_worktree_get_base_commit_id(worktree));
3046 arg.worktree = worktree;
3047 arg.diff_context = diff_context;
3048 arg.id_str = id_str;
3049 arg.header_shown = 0;
3050 arg.diff_staged = diff_staged;
3051 arg.ignore_whitespace = ignore_whitespace;
3053 error = got_pathlist_append(&paths, path, NULL);
3057 error = got_worktree_status(worktree, &paths, repo, print_diff,
3058 &arg, check_cancelled, NULL);
3060 got_pathlist_free(&paths);
3064 error = got_repo_match_object_id(&id1, &label1, id_str1,
3065 GOT_OBJ_TYPE_ANY, 1, repo);
3069 error = got_repo_match_object_id(&id2, &label2, id_str2,
3070 GOT_OBJ_TYPE_ANY, 1, repo);
3074 error = got_object_get_type(&type1, repo, id1);
3078 error = got_object_get_type(&type2, repo, id2);
3082 if (type1 != type2) {
3083 error = got_error(GOT_ERR_OBJ_TYPE);
3088 case GOT_OBJ_TYPE_BLOB:
3089 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3090 diff_context, ignore_whitespace, repo, stdout);
3092 case GOT_OBJ_TYPE_TREE:
3093 error = got_diff_objects_as_trees(id1, id2, "", "",
3094 diff_context, ignore_whitespace, repo, stdout);
3096 case GOT_OBJ_TYPE_COMMIT:
3097 printf("diff %s %s\n", label1, label2);
3098 error = got_diff_objects_as_commits(id1, id2, diff_context,
3099 ignore_whitespace, repo, stdout);
3102 error = got_error(GOT_ERR_OBJ_TYPE);
3111 got_worktree_close(worktree);
3113 const struct got_error *repo_error;
3114 repo_error = got_repo_close(repo);
3125 "usage: %s blame [-c commit] [-r repository-path] path\n",
3134 char datebuf[11]; /* YYYY-MM-DD + NUL */
3137 struct blame_cb_args {
3138 struct blame_line *lines;
3142 off_t *line_offsets;
3144 struct got_repository *repo;
3147 static const struct got_error *
3148 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3150 const struct got_error *err = NULL;
3151 struct blame_cb_args *a = arg;
3152 struct blame_line *bline;
3154 size_t linesize = 0;
3155 struct got_commit_object *commit = NULL;
3158 time_t committer_time;
3160 if (nlines != a->nlines ||
3161 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3162 return got_error(GOT_ERR_RANGE);
3164 if (sigint_received)
3165 return got_error(GOT_ERR_ITER_COMPLETED);
3168 return NULL; /* no change in this commit */
3170 /* Annotate this line. */
3171 bline = &a->lines[lineno - 1];
3172 if (bline->annotated)
3174 err = got_object_id_str(&bline->id_str, id);
3178 err = got_object_open_as_commit(&commit, a->repo, id);
3182 bline->committer = strdup(got_object_commit_get_committer(commit));
3183 if (bline->committer == NULL) {
3184 err = got_error_from_errno("strdup");
3188 committer_time = got_object_commit_get_committer_time(commit);
3189 if (localtime_r(&committer_time, &tm) == NULL)
3190 return got_error_from_errno("localtime_r");
3191 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3192 &tm) >= sizeof(bline->datebuf)) {
3193 err = got_error(GOT_ERR_NO_SPACE);
3196 bline->annotated = 1;
3198 /* Print lines annotated so far. */
3199 bline = &a->lines[a->lineno_cur - 1];
3200 if (!bline->annotated)
3203 offset = a->line_offsets[a->lineno_cur - 1];
3204 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3205 err = got_error_from_errno("fseeko");
3209 while (bline->annotated) {
3210 char *smallerthan, *at, *nl, *committer;
3213 if (getline(&line, &linesize, a->f) == -1) {
3215 err = got_error_from_errno("getline");
3219 committer = bline->committer;
3220 smallerthan = strchr(committer, '<');
3221 if (smallerthan && smallerthan[1] != '\0')
3222 committer = smallerthan + 1;
3223 at = strchr(committer, '@');
3226 len = strlen(committer);
3228 committer[8] = '\0';
3230 nl = strchr(line, '\n');
3233 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3234 bline->id_str, bline->datebuf, committer, line);
3237 bline = &a->lines[a->lineno_cur - 1];
3241 got_object_commit_close(commit);
3246 static const struct got_error *
3247 cmd_blame(int argc, char *argv[])
3249 const struct got_error *error;
3250 struct got_repository *repo = NULL;
3251 struct got_worktree *worktree = NULL;
3252 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3253 struct got_object_id *obj_id = NULL;
3254 struct got_object_id *commit_id = NULL;
3255 struct got_blob_object *blob = NULL;
3256 char *commit_id_str = NULL;
3257 struct blame_cb_args bca;
3258 int ch, obj_type, i;
3261 memset(&bca, 0, sizeof(bca));
3264 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3269 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3272 commit_id_str = optarg;
3275 repo_path = realpath(optarg, NULL);
3276 if (repo_path == NULL)
3277 return got_error_from_errno2("realpath",
3279 got_path_strip_trailing_slashes(repo_path);
3295 cwd = getcwd(NULL, 0);
3297 error = got_error_from_errno("getcwd");
3300 if (repo_path == NULL) {
3301 error = got_worktree_open(&worktree, cwd);
3302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3308 strdup(got_worktree_get_repo_path(worktree));
3309 if (repo_path == NULL)
3310 error = got_error_from_errno("strdup");
3314 repo_path = strdup(cwd);
3315 if (repo_path == NULL) {
3316 error = got_error_from_errno("strdup");
3322 error = got_repo_open(&repo, repo_path, NULL);
3326 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3331 const char *prefix = got_worktree_get_path_prefix(worktree);
3332 char *p, *worktree_subdir = cwd +
3333 strlen(got_worktree_get_root_path(worktree));
3334 if (asprintf(&p, "%s%s%s%s%s",
3335 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3336 worktree_subdir, worktree_subdir[0] ? "/" : "",
3338 error = got_error_from_errno("asprintf");
3341 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3344 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3349 if (commit_id_str == NULL) {
3350 struct got_reference *head_ref;
3351 error = got_ref_open(&head_ref, repo, worktree ?
3352 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3355 error = got_ref_resolve(&commit_id, repo, head_ref);
3356 got_ref_close(head_ref);
3360 error = got_repo_match_object_id(&commit_id, NULL,
3361 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3366 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3370 error = got_object_get_type(&obj_type, repo, obj_id);
3374 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3375 error = got_error(GOT_ERR_OBJ_TYPE);
3379 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3382 bca.f = got_opentemp();
3383 if (bca.f == NULL) {
3384 error = got_error_from_errno("got_opentemp");
3387 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3388 &bca.line_offsets, bca.f, blob);
3389 if (error || bca.nlines == 0)
3392 /* Don't include \n at EOF in the blame line count. */
3393 if (bca.line_offsets[bca.nlines - 1] == filesize)
3396 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3397 if (bca.lines == NULL) {
3398 error = got_error_from_errno("calloc");
3402 bca.nlines_prec = 0;
3410 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3411 check_cancelled, NULL);
3419 got_object_blob_close(blob);
3421 got_worktree_close(worktree);
3423 const struct got_error *repo_error;
3424 repo_error = got_repo_close(repo);
3429 for (i = 0; i < bca.nlines; i++) {
3430 struct blame_line *bline = &bca.lines[i];
3431 free(bline->id_str);
3432 free(bline->committer);
3436 free(bca.line_offsets);
3437 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3438 error = got_error_from_errno("fclose");
3446 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3452 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3453 const char *root_path)
3455 int is_root_path = (strcmp(path, root_path) == 0);
3456 const char *modestr = "";
3457 mode_t mode = got_tree_entry_get_mode(te);
3459 path += strlen(root_path);
3460 while (path[0] == '/')
3463 if (got_object_tree_entry_is_submodule(te))
3465 else if (S_ISLNK(mode))
3467 else if (S_ISDIR(mode))
3469 else if (mode & S_IXUSR)
3472 printf("%s%s%s%s%s\n", id ? id : "", path,
3473 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3476 static const struct got_error *
3477 print_tree(const char *path, struct got_object_id *commit_id,
3478 int show_ids, int recurse, const char *root_path,
3479 struct got_repository *repo)
3481 const struct got_error *err = NULL;
3482 struct got_object_id *tree_id = NULL;
3483 struct got_tree_object *tree = NULL;
3486 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3490 err = got_object_open_as_tree(&tree, repo, tree_id);
3493 nentries = got_object_tree_get_nentries(tree);
3494 for (i = 0; i < nentries; i++) {
3495 struct got_tree_entry *te;
3498 if (sigint_received || sigpipe_received)
3501 te = got_object_tree_get_entry(tree, i);
3504 err = got_object_id_str(&id_str,
3505 got_tree_entry_get_id(te));
3508 if (asprintf(&id, "%s ", id_str) == -1) {
3509 err = got_error_from_errno("asprintf");
3515 print_entry(te, id, path, root_path);
3518 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3520 if (asprintf(&child_path, "%s%s%s", path,
3521 path[0] == '/' && path[1] == '\0' ? "" : "/",
3522 got_tree_entry_get_name(te)) == -1) {
3523 err = got_error_from_errno("asprintf");
3526 err = print_tree(child_path, commit_id, show_ids, 1,
3535 got_object_tree_close(tree);
3540 static const struct got_error *
3541 cmd_tree(int argc, char *argv[])
3543 const struct got_error *error;
3544 struct got_repository *repo = NULL;
3545 struct got_worktree *worktree = NULL;
3547 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3548 struct got_object_id *commit_id = NULL;
3549 char *commit_id_str = NULL;
3550 int show_ids = 0, recurse = 0;
3554 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3559 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3562 commit_id_str = optarg;
3565 repo_path = realpath(optarg, NULL);
3566 if (repo_path == NULL)
3567 return got_error_from_errno2("realpath",
3569 got_path_strip_trailing_slashes(repo_path);
3593 cwd = getcwd(NULL, 0);
3595 error = got_error_from_errno("getcwd");
3598 if (repo_path == NULL) {
3599 error = got_worktree_open(&worktree, cwd);
3600 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3606 strdup(got_worktree_get_repo_path(worktree));
3607 if (repo_path == NULL)
3608 error = got_error_from_errno("strdup");
3612 repo_path = strdup(cwd);
3613 if (repo_path == NULL) {
3614 error = got_error_from_errno("strdup");
3620 error = got_repo_open(&repo, repo_path, NULL);
3624 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3630 char *p, *worktree_subdir = cwd +
3631 strlen(got_worktree_get_root_path(worktree));
3632 if (asprintf(&p, "%s/%s",
3633 got_worktree_get_path_prefix(worktree),
3634 worktree_subdir) == -1) {
3635 error = got_error_from_errno("asprintf");
3638 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3645 if (in_repo_path == NULL) {
3646 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3651 if (commit_id_str == NULL) {
3652 struct got_reference *head_ref;
3653 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3656 error = got_ref_resolve(&commit_id, repo, head_ref);
3657 got_ref_close(head_ref);
3661 error = got_repo_match_object_id(&commit_id, NULL,
3662 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3667 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3668 in_repo_path, repo);
3675 got_worktree_close(worktree);
3677 const struct got_error *repo_error;
3678 repo_error = got_repo_close(repo);
3688 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3692 static const struct got_error *
3693 print_status(void *arg, unsigned char status, unsigned char staged_status,
3694 const char *path, struct got_object_id *blob_id,
3695 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3696 int dirfd, const char *de_name)
3698 if (status == staged_status && (status == GOT_STATUS_DELETE))
3699 status = GOT_STATUS_NO_CHANGE;
3700 printf("%c%c %s\n", status, staged_status, path);
3704 static const struct got_error *
3705 cmd_status(int argc, char *argv[])
3707 const struct got_error *error = NULL;
3708 struct got_repository *repo = NULL;
3709 struct got_worktree *worktree = NULL;
3711 struct got_pathlist_head paths;
3712 struct got_pathlist_entry *pe;
3717 while ((ch = getopt(argc, argv, "")) != -1) {
3729 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3733 cwd = getcwd(NULL, 0);
3735 error = got_error_from_errno("getcwd");
3739 error = got_worktree_open(&worktree, cwd);
3743 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3748 error = apply_unveil(got_repo_get_path(repo), 1,
3749 got_worktree_get_root_path(worktree));
3753 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3757 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3758 check_cancelled, NULL);
3760 TAILQ_FOREACH(pe, &paths, entry)
3761 free((char *)pe->path);
3762 got_pathlist_free(&paths);
3771 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3776 static const struct got_error *
3777 list_refs(struct got_repository *repo)
3779 static const struct got_error *err = NULL;
3780 struct got_reflist_head refs;
3781 struct got_reflist_entry *re;
3783 SIMPLEQ_INIT(&refs);
3784 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3788 SIMPLEQ_FOREACH(re, &refs, entry) {
3790 refstr = got_ref_to_str(re->ref);
3792 return got_error_from_errno("got_ref_to_str");
3793 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3797 got_ref_list_free(&refs);
3801 static const struct got_error *
3802 delete_ref(struct got_repository *repo, const char *refname)
3804 const struct got_error *err = NULL;
3805 struct got_reference *ref;
3807 err = got_ref_open(&ref, repo, refname, 0);
3811 err = got_ref_delete(ref, repo);
3816 static const struct got_error *
3817 add_ref(struct got_repository *repo, const char *refname, const char *target)
3819 const struct got_error *err = NULL;
3820 struct got_object_id *id;
3821 struct got_reference *ref = NULL;
3824 * Don't let the user create a reference name with a leading '-'.
3825 * While technically a valid reference name, this case is usually
3826 * an unintended typo.
3828 if (refname[0] == '-')
3829 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3831 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3834 struct got_reference *target_ref;
3836 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3838 err = got_ref_open(&target_ref, repo, target, 0);
3841 err = got_ref_resolve(&id, repo, target_ref);
3842 got_ref_close(target_ref);
3847 err = got_ref_alloc(&ref, refname, id);
3851 err = got_ref_write(ref, repo);
3859 static const struct got_error *
3860 add_symref(struct got_repository *repo, const char *refname, const char *target)
3862 const struct got_error *err = NULL;
3863 struct got_reference *ref = NULL;
3864 struct got_reference *target_ref = NULL;
3867 * Don't let the user create a reference name with a leading '-'.
3868 * While technically a valid reference name, this case is usually
3869 * an unintended typo.
3871 if (refname[0] == '-')
3872 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3874 err = got_ref_open(&target_ref, repo, target, 0);
3878 err = got_ref_alloc_symref(&ref, refname, target_ref);
3882 err = got_ref_write(ref, repo);
3885 got_ref_close(target_ref);
3891 static const struct got_error *
3892 cmd_ref(int argc, char *argv[])
3894 const struct got_error *error = NULL;
3895 struct got_repository *repo = NULL;
3896 struct got_worktree *worktree = NULL;
3897 char *cwd = NULL, *repo_path = NULL;
3898 int ch, do_list = 0, create_symref = 0;
3899 const char *delref = NULL;
3901 /* TODO: Add -s option for adding symbolic references. */
3902 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3908 repo_path = realpath(optarg, NULL);
3909 if (repo_path == NULL)
3910 return got_error_from_errno2("realpath",
3912 got_path_strip_trailing_slashes(repo_path);
3926 if (do_list && delref)
3927 errx(1, "-l and -d options are mutually exclusive\n");
3932 if (do_list || delref) {
3934 errx(1, "-s option cannot be used together with the "
3935 "-l or -d options");
3938 } else if (argc != 2)
3943 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3947 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3948 "sendfd unveil", NULL) == -1)
3952 cwd = getcwd(NULL, 0);
3954 error = got_error_from_errno("getcwd");
3958 if (repo_path == NULL) {
3959 error = got_worktree_open(&worktree, cwd);
3960 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3966 strdup(got_worktree_get_repo_path(worktree));
3967 if (repo_path == NULL)
3968 error = got_error_from_errno("strdup");
3972 repo_path = strdup(cwd);
3973 if (repo_path == NULL) {
3974 error = got_error_from_errno("strdup");
3980 error = got_repo_open(&repo, repo_path, NULL);
3984 error = apply_unveil(got_repo_get_path(repo), do_list,
3985 worktree ? got_worktree_get_root_path(worktree) : NULL);
3990 error = list_refs(repo);
3992 error = delete_ref(repo, delref);
3993 else if (create_symref)
3994 error = add_symref(repo, argv[0], argv[1]);
3996 error = add_ref(repo, argv[0], argv[1]);
3999 got_repo_close(repo);
4001 got_worktree_close(worktree);
4011 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4012 "[name]\n", getprogname());
4016 static const struct got_error *
4017 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4018 struct got_reference *ref)
4020 const struct got_error *err = NULL;
4021 const char *refname, *marker = " ";
4024 refname = got_ref_get_name(ref);
4025 if (worktree && strcmp(refname,
4026 got_worktree_get_head_ref_name(worktree)) == 0) {
4027 struct got_object_id *id = NULL;
4029 err = got_ref_resolve(&id, repo, ref);
4032 if (got_object_id_cmp(id,
4033 got_worktree_get_base_commit_id(worktree)) == 0)
4040 if (strncmp(refname, "refs/heads/", 11) == 0)
4042 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4045 refstr = got_ref_to_str(ref);
4047 return got_error_from_errno("got_ref_to_str");
4049 printf("%s%s: %s\n", marker, refname, refstr);
4054 static const struct got_error *
4055 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4057 const char *refname;
4059 if (worktree == NULL)
4060 return got_error(GOT_ERR_NOT_WORKTREE);
4062 refname = got_worktree_get_head_ref_name(worktree);
4064 if (strncmp(refname, "refs/heads/", 11) == 0)
4066 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4069 printf("%s\n", refname);
4074 static const struct got_error *
4075 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4077 static const struct got_error *err = NULL;
4078 struct got_reflist_head refs;
4079 struct got_reflist_entry *re;
4080 struct got_reference *temp_ref = NULL;
4081 int rebase_in_progress, histedit_in_progress;
4083 SIMPLEQ_INIT(&refs);
4086 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4091 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4096 if (rebase_in_progress || histedit_in_progress) {
4097 err = got_ref_open(&temp_ref, repo,
4098 got_worktree_get_head_ref_name(worktree), 0);
4101 list_branch(repo, worktree, temp_ref);
4102 got_ref_close(temp_ref);
4106 err = got_ref_list(&refs, repo, "refs/heads",
4107 got_ref_cmp_by_name, NULL);
4111 SIMPLEQ_FOREACH(re, &refs, entry)
4112 list_branch(repo, worktree, re->ref);
4114 got_ref_list_free(&refs);
4118 static const struct got_error *
4119 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4120 const char *branch_name)
4122 const struct got_error *err = NULL;
4123 struct got_reference *ref = NULL;
4126 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4127 return got_error_from_errno("asprintf");
4129 err = got_ref_open(&ref, repo, refname, 0);
4134 strcmp(got_worktree_get_head_ref_name(worktree),
4135 got_ref_get_name(ref)) == 0) {
4136 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4137 "will not delete this work tree's current branch");
4141 err = got_ref_delete(ref, repo);
4149 static const struct got_error *
4150 add_branch(struct got_repository *repo, const char *branch_name,
4151 struct got_object_id *base_commit_id)
4153 const struct got_error *err = NULL;
4154 struct got_reference *ref = NULL;
4155 char *base_refname = NULL, *refname = NULL;
4158 * Don't let the user create a branch name with a leading '-'.
4159 * While technically a valid reference name, this case is usually
4160 * an unintended typo.
4162 if (branch_name[0] == '-')
4163 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4165 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4166 err = got_error_from_errno("asprintf");
4170 err = got_ref_open(&ref, repo, refname, 0);
4172 err = got_error(GOT_ERR_BRANCH_EXISTS);
4174 } else if (err->code != GOT_ERR_NOT_REF)
4177 err = got_ref_alloc(&ref, refname, base_commit_id);
4181 err = got_ref_write(ref, repo);
4190 static const struct got_error *
4191 cmd_branch(int argc, char *argv[])
4193 const struct got_error *error = NULL;
4194 struct got_repository *repo = NULL;
4195 struct got_worktree *worktree = NULL;
4196 char *cwd = NULL, *repo_path = NULL;
4197 int ch, do_list = 0, do_show = 0, do_update = 1;
4198 const char *delref = NULL, *commit_id_arg = NULL;
4199 struct got_reference *ref = NULL;
4200 struct got_pathlist_head paths;
4201 struct got_pathlist_entry *pe;
4202 struct got_object_id *commit_id = NULL;
4203 char *commit_id_str = NULL;
4207 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4210 commit_id_arg = optarg;
4216 repo_path = realpath(optarg, NULL);
4217 if (repo_path == NULL)
4218 return got_error_from_errno2("realpath",
4220 got_path_strip_trailing_slashes(repo_path);
4234 if (do_list && delref)
4235 errx(1, "-l and -d options are mutually exclusive\n");
4240 if (!do_list && !delref && argc == 0)
4243 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4244 errx(1, "-c option can only be used when creating a branch");
4246 if (do_list || delref) {
4249 } else if (!do_show && argc != 1)
4253 if (do_list || do_show) {
4254 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4258 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4259 "sendfd unveil", NULL) == -1)
4263 cwd = getcwd(NULL, 0);
4265 error = got_error_from_errno("getcwd");
4269 if (repo_path == NULL) {
4270 error = got_worktree_open(&worktree, cwd);
4271 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4277 strdup(got_worktree_get_repo_path(worktree));
4278 if (repo_path == NULL)
4279 error = got_error_from_errno("strdup");
4283 repo_path = strdup(cwd);
4284 if (repo_path == NULL) {
4285 error = got_error_from_errno("strdup");
4291 error = got_repo_open(&repo, repo_path, NULL);
4295 error = apply_unveil(got_repo_get_path(repo), do_list,
4296 worktree ? got_worktree_get_root_path(worktree) : NULL);
4301 error = show_current_branch(repo, worktree);
4303 error = list_branches(repo, worktree);
4305 error = delete_branch(repo, worktree, delref);
4307 if (commit_id_arg == NULL)
4308 commit_id_arg = worktree ?
4309 got_worktree_get_head_ref_name(worktree) :
4311 error = got_repo_match_object_id(&commit_id, NULL,
4312 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4315 error = add_branch(repo, argv[0], commit_id);
4318 if (worktree && do_update) {
4319 int did_something = 0;
4320 char *branch_refname = NULL;
4322 error = got_object_id_str(&commit_id_str, commit_id);
4325 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4329 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4331 error = got_error_from_errno("asprintf");
4334 error = got_ref_open(&ref, repo, branch_refname, 0);
4335 free(branch_refname);
4338 error = switch_head_ref(ref, commit_id, worktree,
4342 error = got_worktree_set_base_commit_id(worktree, repo,
4346 error = got_worktree_checkout_files(worktree, &paths,
4347 repo, update_progress, &did_something,
4348 check_cancelled, NULL);
4352 printf("Updated to commit %s\n", commit_id_str);
4359 got_repo_close(repo);
4361 got_worktree_close(worktree);
4365 free(commit_id_str);
4366 TAILQ_FOREACH(pe, &paths, entry)
4367 free((char *)pe->path);
4368 got_pathlist_free(&paths);
4377 "usage: %s tag [-c commit] [-r repository] [-l] "
4378 "[-m message] name\n", getprogname());
4383 static const struct got_error *
4384 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4386 const struct got_error *err = NULL;
4387 struct got_reflist_entry *re, *se, *new;
4388 struct got_object_id *re_id, *se_id;
4389 struct got_tag_object *re_tag, *se_tag;
4390 time_t re_time, se_time;
4392 SIMPLEQ_FOREACH(re, tags, entry) {
4393 se = SIMPLEQ_FIRST(sorted);
4395 err = got_reflist_entry_dup(&new, re);
4398 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4401 err = got_ref_resolve(&re_id, repo, re->ref);
4404 err = got_object_open_as_tag(&re_tag, repo, re_id);
4408 re_time = got_object_tag_get_tagger_time(re_tag);
4409 got_object_tag_close(re_tag);
4413 err = got_ref_resolve(&se_id, repo, re->ref);
4416 err = got_object_open_as_tag(&se_tag, repo, se_id);
4420 se_time = got_object_tag_get_tagger_time(se_tag);
4421 got_object_tag_close(se_tag);
4423 if (se_time > re_time) {
4424 err = got_reflist_entry_dup(&new, re);
4427 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4430 se = SIMPLEQ_NEXT(se, entry);
4439 static const struct got_error *
4440 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4442 static const struct got_error *err = NULL;
4443 struct got_reflist_head refs;
4444 struct got_reflist_entry *re;
4446 SIMPLEQ_INIT(&refs);
4448 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4452 SIMPLEQ_FOREACH(re, &refs, entry) {
4453 const char *refname;
4454 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4458 struct got_object_id *id;
4459 struct got_tag_object *tag;
4460 struct got_commit_object *commit = NULL;
4462 refname = got_ref_get_name(re->ref);
4463 if (strncmp(refname, "refs/tags/", 10) != 0)
4466 refstr = got_ref_to_str(re->ref);
4467 if (refstr == NULL) {
4468 err = got_error_from_errno("got_ref_to_str");
4471 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4474 err = got_ref_resolve(&id, repo, re->ref);
4477 err = got_object_open_as_tag(&tag, repo, id);
4479 if (err->code != GOT_ERR_OBJ_TYPE) {
4483 /* "lightweight" tag */
4484 err = got_object_open_as_commit(&commit, repo, id);
4489 tagger = got_object_commit_get_committer(commit);
4491 got_object_commit_get_committer_time(commit);
4492 err = got_object_id_str(&id_str, id);
4498 tagger = got_object_tag_get_tagger(tag);
4499 tagger_time = got_object_tag_get_tagger_time(tag);
4500 err = got_object_id_str(&id_str,
4501 got_object_tag_get_object_id(tag));
4505 printf("from: %s\n", tagger);
4506 datestr = get_datestr(&tagger_time, datebuf);
4508 printf("date: %s UTC\n", datestr);
4510 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4512 switch (got_object_tag_get_object_type(tag)) {
4513 case GOT_OBJ_TYPE_BLOB:
4514 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4517 case GOT_OBJ_TYPE_TREE:
4518 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4521 case GOT_OBJ_TYPE_COMMIT:
4522 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4525 case GOT_OBJ_TYPE_TAG:
4526 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4535 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4538 got_object_commit_close(commit);
4540 tagmsg0 = strdup(got_object_tag_get_message(tag));
4541 got_object_tag_close(tag);
4542 if (tagmsg0 == NULL) {
4543 err = got_error_from_errno("strdup");
4550 line = strsep(&tagmsg, "\n");
4552 printf(" %s\n", line);
4557 got_ref_list_free(&refs);
4561 static const struct got_error *
4562 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4563 const char *tag_name, const char *repo_path)
4565 const struct got_error *err = NULL;
4566 char *template = NULL, *initial_content = NULL;
4567 char *editor = NULL;
4570 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4571 err = got_error_from_errno("asprintf");
4575 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4576 commit_id_str, tag_name) == -1) {
4577 err = got_error_from_errno("asprintf");
4581 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4585 dprintf(fd, initial_content);
4588 err = get_editor(&editor);
4591 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4593 free(initial_content);
4597 /* Editor is done; we can now apply unveil(2) */
4599 err = apply_unveil(repo_path, 0, NULL);
4608 static const struct got_error *
4609 add_tag(struct got_repository *repo, const char *tag_name,
4610 const char *commit_arg, const char *tagmsg_arg)
4612 const struct got_error *err = NULL;
4613 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4614 char *label = NULL, *commit_id_str = NULL;
4615 struct got_reference *ref = NULL;
4616 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4617 char *tagmsg_path = NULL, *tag_id_str = NULL;
4618 int preserve_tagmsg = 0;
4621 * Don't let the user create a tag name with a leading '-'.
4622 * While technically a valid reference name, this case is usually
4623 * an unintended typo.
4625 if (tag_name[0] == '-')
4626 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4628 err = get_author(&tagger, repo);
4632 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4633 GOT_OBJ_TYPE_COMMIT, 1, repo);
4637 err = got_object_id_str(&commit_id_str, commit_id);
4641 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4642 refname = strdup(tag_name);
4643 if (refname == NULL) {
4644 err = got_error_from_errno("strdup");
4648 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4649 err = got_error_from_errno("asprintf");
4653 err = got_ref_open(&ref, repo, refname, 0);
4655 err = got_error(GOT_ERR_TAG_EXISTS);
4657 } else if (err->code != GOT_ERR_NOT_REF)
4660 if (tagmsg_arg == NULL) {
4661 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4662 tag_name, got_repo_get_path(repo));
4664 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4665 tagmsg_path != NULL)
4666 preserve_tagmsg = 1;
4671 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4672 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4675 preserve_tagmsg = 1;
4679 err = got_ref_alloc(&ref, refname, tag_id);
4682 preserve_tagmsg = 1;
4686 err = got_ref_write(ref, repo);
4689 preserve_tagmsg = 1;
4693 err = got_object_id_str(&tag_id_str, tag_id);
4696 preserve_tagmsg = 1;
4699 printf("Created tag %s\n", tag_id_str);
4701 if (preserve_tagmsg) {
4702 fprintf(stderr, "%s: tag message preserved in %s\n",
4703 getprogname(), tagmsg_path);
4704 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4705 err = got_error_from_errno2("unlink", tagmsg_path);
4710 free(commit_id_str);
4718 static const struct got_error *
4719 cmd_tag(int argc, char *argv[])
4721 const struct got_error *error = NULL;
4722 struct got_repository *repo = NULL;
4723 struct got_worktree *worktree = NULL;
4724 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4725 char *gitconfig_path = NULL;
4726 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4727 int ch, do_list = 0;
4729 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4732 commit_id_arg = optarg;
4738 repo_path = realpath(optarg, NULL);
4739 if (repo_path == NULL)
4740 return got_error_from_errno2("realpath",
4742 got_path_strip_trailing_slashes(repo_path);
4757 if (commit_id_arg != NULL)
4758 errx(1, "-c option can only be used when creating a tag");
4760 errx(1, "-l and -m options are mutually exclusive");
4763 } else if (argc != 1)
4770 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4774 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4775 "sendfd unveil", NULL) == -1)
4779 cwd = getcwd(NULL, 0);
4781 error = got_error_from_errno("getcwd");
4785 if (repo_path == NULL) {
4786 error = got_worktree_open(&worktree, cwd);
4787 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4793 strdup(got_worktree_get_repo_path(worktree));
4794 if (repo_path == NULL)
4795 error = got_error_from_errno("strdup");
4799 repo_path = strdup(cwd);
4800 if (repo_path == NULL) {
4801 error = got_error_from_errno("strdup");
4808 error = got_repo_open(&repo, repo_path, NULL);
4811 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4814 error = list_tags(repo, worktree);
4816 error = get_gitconfig_path(&gitconfig_path);
4819 error = got_repo_open(&repo, repo_path, gitconfig_path);
4824 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4829 if (commit_id_arg == NULL) {
4830 struct got_reference *head_ref;
4831 struct got_object_id *commit_id;
4832 error = got_ref_open(&head_ref, repo,
4833 worktree ? got_worktree_get_head_ref_name(worktree)
4837 error = got_ref_resolve(&commit_id, repo, head_ref);
4838 got_ref_close(head_ref);
4841 error = got_object_id_str(&commit_id_str, commit_id);
4847 error = add_tag(repo, tag_name,
4848 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4852 got_repo_close(repo);
4854 got_worktree_close(worktree);
4857 free(gitconfig_path);
4858 free(commit_id_str);
4865 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4870 static const struct got_error *
4871 add_progress(void *arg, unsigned char status, const char *path)
4873 while (path[0] == '/')
4875 printf("%c %s\n", status, path);
4879 static const struct got_error *
4880 cmd_add(int argc, char *argv[])
4882 const struct got_error *error = NULL;
4883 struct got_repository *repo = NULL;
4884 struct got_worktree *worktree = NULL;
4886 struct got_pathlist_head paths;
4887 struct got_pathlist_entry *pe;
4888 int ch, can_recurse = 0, no_ignores = 0;
4892 while ((ch = getopt(argc, argv, "IR")) != -1) {
4910 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4917 cwd = getcwd(NULL, 0);
4919 error = got_error_from_errno("getcwd");
4923 error = got_worktree_open(&worktree, cwd);
4927 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4932 error = apply_unveil(got_repo_get_path(repo), 1,
4933 got_worktree_get_root_path(worktree));
4937 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4941 if (!can_recurse && no_ignores) {
4942 error = got_error_msg(GOT_ERR_BAD_PATH,
4943 "disregarding ignores requires -R option");
4951 TAILQ_FOREACH(pe, &paths, entry) {
4952 if (asprintf(&ondisk_path, "%s/%s",
4953 got_worktree_get_root_path(worktree),
4955 error = got_error_from_errno("asprintf");
4958 if (lstat(ondisk_path, &sb) == -1) {
4959 if (errno == ENOENT) {
4963 error = got_error_from_errno2("lstat",
4969 if (S_ISDIR(sb.st_mode)) {
4970 error = got_error_msg(GOT_ERR_BAD_PATH,
4971 "adding directories requires -R option");
4977 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4978 NULL, repo, no_ignores);
4981 got_repo_close(repo);
4983 got_worktree_close(worktree);
4984 TAILQ_FOREACH(pe, &paths, entry)
4985 free((char *)pe->path);
4986 got_pathlist_free(&paths);
4994 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4999 static const struct got_error *
5000 print_remove_status(void *arg, unsigned char status,
5001 unsigned char staged_status, const char *path)
5003 while (path[0] == '/')
5005 if (status == GOT_STATUS_NONEXISTENT)
5007 if (status == staged_status && (status == GOT_STATUS_DELETE))
5008 status = GOT_STATUS_NO_CHANGE;
5009 printf("%c%c %s\n", status, staged_status, path);
5013 static const struct got_error *
5014 cmd_remove(int argc, char *argv[])
5016 const struct got_error *error = NULL;
5017 struct got_worktree *worktree = NULL;
5018 struct got_repository *repo = NULL;
5020 struct got_pathlist_head paths;
5021 struct got_pathlist_entry *pe;
5022 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5026 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5029 delete_local_mods = 1;
5047 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5054 cwd = getcwd(NULL, 0);
5056 error = got_error_from_errno("getcwd");
5059 error = got_worktree_open(&worktree, cwd);
5063 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5068 error = apply_unveil(got_repo_get_path(repo), 1,
5069 got_worktree_get_root_path(worktree));
5073 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5080 TAILQ_FOREACH(pe, &paths, entry) {
5081 if (asprintf(&ondisk_path, "%s/%s",
5082 got_worktree_get_root_path(worktree),
5084 error = got_error_from_errno("asprintf");
5087 if (lstat(ondisk_path, &sb) == -1) {
5088 if (errno == ENOENT) {
5092 error = got_error_from_errno2("lstat",
5098 if (S_ISDIR(sb.st_mode)) {
5099 error = got_error_msg(GOT_ERR_BAD_PATH,
5100 "removing directories requires -R option");
5106 error = got_worktree_schedule_delete(worktree, &paths,
5107 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5110 got_repo_close(repo);
5112 got_worktree_close(worktree);
5113 TAILQ_FOREACH(pe, &paths, entry)
5114 free((char *)pe->path);
5115 got_pathlist_free(&paths);
5123 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5124 "path ...\n", getprogname());
5128 static const struct got_error *
5129 revert_progress(void *arg, unsigned char status, const char *path)
5131 if (status == GOT_STATUS_UNVERSIONED)
5134 while (path[0] == '/')
5136 printf("%c %s\n", status, path);
5140 struct choose_patch_arg {
5141 FILE *patch_script_file;
5145 static const struct got_error *
5146 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5147 int nchanges, const char *action)
5150 size_t linesize = 0;
5154 case GOT_STATUS_ADD:
5155 printf("A %s\n%s this addition? [y/n] ", path, action);
5157 case GOT_STATUS_DELETE:
5158 printf("D %s\n%s this deletion? [y/n] ", path, action);
5160 case GOT_STATUS_MODIFY:
5161 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5162 return got_error_from_errno("fseek");
5163 printf(GOT_COMMIT_SEP_STR);
5164 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5166 if (ferror(patch_file))
5167 return got_error_from_errno("getline");
5168 printf(GOT_COMMIT_SEP_STR);
5169 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5170 path, n, nchanges, action);
5173 return got_error_path(path, GOT_ERR_FILE_STATUS);
5179 static const struct got_error *
5180 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5181 FILE *patch_file, int n, int nchanges)
5183 const struct got_error *err = NULL;
5185 size_t linesize = 0;
5188 struct choose_patch_arg *a = arg;
5190 *choice = GOT_PATCH_CHOICE_NONE;
5192 if (a->patch_script_file) {
5194 err = show_change(status, path, patch_file, n, nchanges,
5198 linelen = getline(&line, &linesize, a->patch_script_file);
5199 if (linelen == -1) {
5200 if (ferror(a->patch_script_file))
5201 return got_error_from_errno("getline");
5204 nl = strchr(line, '\n');
5207 if (strcmp(line, "y") == 0) {
5208 *choice = GOT_PATCH_CHOICE_YES;
5210 } else if (strcmp(line, "n") == 0) {
5211 *choice = GOT_PATCH_CHOICE_NO;
5213 } else if (strcmp(line, "q") == 0 &&
5214 status == GOT_STATUS_MODIFY) {
5215 *choice = GOT_PATCH_CHOICE_QUIT;
5218 printf("invalid response '%s'\n", line);
5223 while (resp != 'y' && resp != 'n' && resp != 'q') {
5224 err = show_change(status, path, patch_file, n, nchanges,
5231 if (status == GOT_STATUS_MODIFY) {
5232 if (resp != 'y' && resp != 'n' && resp != 'q') {
5233 printf("invalid response '%c'\n", resp);
5236 } else if (resp != 'y' && resp != 'n') {
5237 printf("invalid response '%c'\n", resp);
5243 *choice = GOT_PATCH_CHOICE_YES;
5244 else if (resp == 'n')
5245 *choice = GOT_PATCH_CHOICE_NO;
5246 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5247 *choice = GOT_PATCH_CHOICE_QUIT;
5253 static const struct got_error *
5254 cmd_revert(int argc, char *argv[])
5256 const struct got_error *error = NULL;
5257 struct got_worktree *worktree = NULL;
5258 struct got_repository *repo = NULL;
5259 char *cwd = NULL, *path = NULL;
5260 struct got_pathlist_head paths;
5261 struct got_pathlist_entry *pe;
5262 int ch, can_recurse = 0, pflag = 0;
5263 FILE *patch_script_file = NULL;
5264 const char *patch_script_path = NULL;
5265 struct choose_patch_arg cpa;
5269 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5275 patch_script_path = optarg;
5290 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5291 "unveil", NULL) == -1)
5296 if (patch_script_path && !pflag)
5297 errx(1, "-F option can only be used together with -p option");
5299 cwd = getcwd(NULL, 0);
5301 error = got_error_from_errno("getcwd");
5304 error = got_worktree_open(&worktree, cwd);
5308 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5313 if (patch_script_path) {
5314 patch_script_file = fopen(patch_script_path, "r");
5315 if (patch_script_file == NULL) {
5316 error = got_error_from_errno2("fopen",
5321 error = apply_unveil(got_repo_get_path(repo), 1,
5322 got_worktree_get_root_path(worktree));
5326 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5333 TAILQ_FOREACH(pe, &paths, entry) {
5334 if (asprintf(&ondisk_path, "%s/%s",
5335 got_worktree_get_root_path(worktree),
5337 error = got_error_from_errno("asprintf");
5340 if (lstat(ondisk_path, &sb) == -1) {
5341 if (errno == ENOENT) {
5345 error = got_error_from_errno2("lstat",
5351 if (S_ISDIR(sb.st_mode)) {
5352 error = got_error_msg(GOT_ERR_BAD_PATH,
5353 "reverting directories requires -R option");
5359 cpa.patch_script_file = patch_script_file;
5360 cpa.action = "revert";
5361 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5362 pflag ? choose_patch : NULL, &cpa, repo);
5364 if (patch_script_file && fclose(patch_script_file) == EOF &&
5366 error = got_error_from_errno2("fclose", patch_script_path);
5368 got_repo_close(repo);
5370 got_worktree_close(worktree);
5379 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5384 struct collect_commit_logmsg_arg {
5385 const char *cmdline_log;
5387 const char *worktree_path;
5388 const char *branch_name;
5389 const char *repo_path;
5394 static const struct got_error *
5395 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5398 char *initial_content = NULL;
5399 struct got_pathlist_entry *pe;
5400 const struct got_error *err = NULL;
5401 char *template = NULL;
5402 struct collect_commit_logmsg_arg *a = arg;
5406 /* if a message was specified on the command line, just use it */
5407 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5408 len = strlen(a->cmdline_log) + 1;
5409 *logmsg = malloc(len + 1);
5410 if (*logmsg == NULL)
5411 return got_error_from_errno("malloc");
5412 strlcpy(*logmsg, a->cmdline_log, len);
5416 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5417 return got_error_from_errno("asprintf");
5419 if (asprintf(&initial_content,
5420 "\n# changes to be committed on branch %s:\n",
5421 a->branch_name) == -1)
5422 return got_error_from_errno("asprintf");
5424 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5428 dprintf(fd, initial_content);
5430 TAILQ_FOREACH(pe, commitable_paths, entry) {
5431 struct got_commitable *ct = pe->data;
5432 dprintf(fd, "# %c %s\n",
5433 got_commitable_get_status(ct),
5434 got_commitable_get_path(ct));
5438 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5440 free(initial_content);
5443 /* Editor is done; we can now apply unveil(2) */
5445 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5454 static const struct got_error *
5455 cmd_commit(int argc, char *argv[])
5457 const struct got_error *error = NULL;
5458 struct got_worktree *worktree = NULL;
5459 struct got_repository *repo = NULL;
5460 char *cwd = NULL, *id_str = NULL;
5461 struct got_object_id *id = NULL;
5462 const char *logmsg = NULL;
5463 struct collect_commit_logmsg_arg cl_arg;
5464 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5465 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5466 struct got_pathlist_head paths;
5469 cl_arg.logmsg_path = NULL;
5471 while ((ch = getopt(argc, argv, "m:")) != -1) {
5486 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5487 "unveil", NULL) == -1)
5490 cwd = getcwd(NULL, 0);
5492 error = got_error_from_errno("getcwd");
5495 error = got_worktree_open(&worktree, cwd);
5499 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5502 if (rebase_in_progress) {
5503 error = got_error(GOT_ERR_REBASING);
5507 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5512 error = get_gitconfig_path(&gitconfig_path);
5515 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5520 error = get_author(&author, repo);
5525 * unveil(2) traverses exec(2); if an editor is used we have
5526 * to apply unveil after the log message has been written.
5528 if (logmsg == NULL || strlen(logmsg) == 0)
5529 error = get_editor(&editor);
5531 error = apply_unveil(got_repo_get_path(repo), 0,
5532 got_worktree_get_root_path(worktree));
5536 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5540 cl_arg.editor = editor;
5541 cl_arg.cmdline_log = logmsg;
5542 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5543 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5544 if (!histedit_in_progress) {
5545 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5546 error = got_error(GOT_ERR_COMMIT_BRANCH);
5549 cl_arg.branch_name += 11;
5551 cl_arg.repo_path = got_repo_get_path(repo);
5552 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5553 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5555 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5556 cl_arg.logmsg_path != NULL)
5557 preserve_logmsg = 1;
5561 error = got_object_id_str(&id_str, id);
5564 printf("Created commit %s\n", id_str);
5566 if (preserve_logmsg) {
5567 fprintf(stderr, "%s: log message preserved in %s\n",
5568 getprogname(), cl_arg.logmsg_path);
5569 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5571 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5572 free(cl_arg.logmsg_path);
5574 got_repo_close(repo);
5576 got_worktree_close(worktree);
5579 free(gitconfig_path);
5586 usage_cherrypick(void)
5588 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5592 static const struct got_error *
5593 cmd_cherrypick(int argc, char *argv[])
5595 const struct got_error *error = NULL;
5596 struct got_worktree *worktree = NULL;
5597 struct got_repository *repo = NULL;
5598 char *cwd = NULL, *commit_id_str = NULL;
5599 struct got_object_id *commit_id = NULL;
5600 struct got_commit_object *commit = NULL;
5601 struct got_object_qid *pid;
5602 struct got_reference *head_ref = NULL;
5603 int ch, did_something = 0;
5605 while ((ch = getopt(argc, argv, "")) != -1) {
5617 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5618 "unveil", NULL) == -1)
5624 cwd = getcwd(NULL, 0);
5626 error = got_error_from_errno("getcwd");
5629 error = got_worktree_open(&worktree, cwd);
5633 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5638 error = apply_unveil(got_repo_get_path(repo), 0,
5639 got_worktree_get_root_path(worktree));
5643 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5644 GOT_OBJ_TYPE_COMMIT, repo);
5645 if (error != NULL) {
5646 struct got_reference *ref;
5647 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5649 error = got_ref_open(&ref, repo, argv[0], 0);
5652 error = got_ref_resolve(&commit_id, repo, ref);
5657 error = got_object_id_str(&commit_id_str, commit_id);
5661 error = got_ref_open(&head_ref, repo,
5662 got_worktree_get_head_ref_name(worktree), 0);
5666 error = check_same_branch(commit_id, head_ref, NULL, repo);
5668 if (error->code != GOT_ERR_ANCESTRY)
5672 error = got_error(GOT_ERR_SAME_BRANCH);
5676 error = got_object_open_as_commit(&commit, repo, commit_id);
5679 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5680 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5681 commit_id, repo, update_progress, &did_something, check_cancelled,
5687 printf("Merged commit %s\n", commit_id_str);
5690 got_object_commit_close(commit);
5691 free(commit_id_str);
5693 got_ref_close(head_ref);
5695 got_worktree_close(worktree);
5697 got_repo_close(repo);
5704 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5708 static const struct got_error *
5709 cmd_backout(int argc, char *argv[])
5711 const struct got_error *error = NULL;
5712 struct got_worktree *worktree = NULL;
5713 struct got_repository *repo = NULL;
5714 char *cwd = NULL, *commit_id_str = NULL;
5715 struct got_object_id *commit_id = NULL;
5716 struct got_commit_object *commit = NULL;
5717 struct got_object_qid *pid;
5718 struct got_reference *head_ref = NULL;
5719 int ch, did_something = 0;
5721 while ((ch = getopt(argc, argv, "")) != -1) {
5733 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5734 "unveil", NULL) == -1)
5740 cwd = getcwd(NULL, 0);
5742 error = got_error_from_errno("getcwd");
5745 error = got_worktree_open(&worktree, cwd);
5749 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5754 error = apply_unveil(got_repo_get_path(repo), 0,
5755 got_worktree_get_root_path(worktree));
5759 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5760 GOT_OBJ_TYPE_COMMIT, repo);
5761 if (error != NULL) {
5762 struct got_reference *ref;
5763 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5765 error = got_ref_open(&ref, repo, argv[0], 0);
5768 error = got_ref_resolve(&commit_id, repo, ref);
5773 error = got_object_id_str(&commit_id_str, commit_id);
5777 error = got_ref_open(&head_ref, repo,
5778 got_worktree_get_head_ref_name(worktree), 0);
5782 error = check_same_branch(commit_id, head_ref, NULL, repo);
5786 error = got_object_open_as_commit(&commit, repo, commit_id);
5789 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5791 error = got_error(GOT_ERR_ROOT_COMMIT);
5795 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5796 update_progress, &did_something, check_cancelled, NULL);
5801 printf("Backed out commit %s\n", commit_id_str);
5804 got_object_commit_close(commit);
5805 free(commit_id_str);
5807 got_ref_close(head_ref);
5809 got_worktree_close(worktree);
5811 got_repo_close(repo);
5818 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5824 trim_logmsg(char *logmsg, int limit)
5829 len = strlen(logmsg);
5833 nl = strchr(logmsg, '\n');
5838 static const struct got_error *
5839 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5841 const struct got_error *err;
5842 char *logmsg0 = NULL;
5845 err = got_object_commit_get_logmsg(&logmsg0, commit);
5850 while (isspace((unsigned char)s[0]))
5853 *logmsg = strdup(s);
5854 if (*logmsg == NULL) {
5855 err = got_error_from_errno("strdup");
5859 trim_logmsg(*logmsg, limit);
5865 static const struct got_error *
5866 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5868 const struct got_error *err;
5869 struct got_commit_object *commit = NULL;
5870 char *id_str = NULL, *logmsg = NULL;
5872 err = got_object_open_as_commit(&commit, repo, id);
5876 err = got_object_id_str(&id_str, id);
5882 err = get_short_logmsg(&logmsg, 42, commit);
5886 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5889 got_object_commit_close(commit);
5894 static const struct got_error *
5895 show_rebase_progress(struct got_commit_object *commit,
5896 struct got_object_id *old_id, struct got_object_id *new_id)
5898 const struct got_error *err;
5899 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5901 err = got_object_id_str(&old_id_str, old_id);
5906 err = got_object_id_str(&new_id_str, new_id);
5911 old_id_str[12] = '\0';
5913 new_id_str[12] = '\0';
5915 err = get_short_logmsg(&logmsg, 42, commit);
5919 printf("%s -> %s: %s\n", old_id_str,
5920 new_id_str ? new_id_str : "no-op change", logmsg);
5928 static const struct got_error *
5929 rebase_progress(void *arg, unsigned char status, const char *path)
5931 unsigned char *rebase_status = arg;
5933 while (path[0] == '/')
5935 printf("%c %s\n", status, path);
5937 if (*rebase_status == GOT_STATUS_CONFLICT)
5939 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5940 *rebase_status = status;
5944 static const struct got_error *
5945 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5946 struct got_reference *branch, struct got_reference *new_base_branch,
5947 struct got_reference *tmp_branch, struct got_repository *repo)
5949 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5950 return got_worktree_rebase_complete(worktree, fileindex,
5951 new_base_branch, tmp_branch, branch, repo);
5954 static const struct got_error *
5955 rebase_commit(struct got_pathlist_head *merged_paths,
5956 struct got_worktree *worktree, struct got_fileindex *fileindex,
5957 struct got_reference *tmp_branch,
5958 struct got_object_id *commit_id, struct got_repository *repo)
5960 const struct got_error *error;
5961 struct got_commit_object *commit;
5962 struct got_object_id *new_commit_id;
5964 error = got_object_open_as_commit(&commit, repo, commit_id);
5968 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5969 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5971 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5973 error = show_rebase_progress(commit, commit_id, NULL);
5975 error = show_rebase_progress(commit, commit_id, new_commit_id);
5976 free(new_commit_id);
5979 got_object_commit_close(commit);
5983 struct check_path_prefix_arg {
5984 const char *path_prefix;
5989 static const struct got_error *
5990 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5991 struct got_blob_object *blob2, struct got_object_id *id1,
5992 struct got_object_id *id2, const char *path1, const char *path2,
5993 mode_t mode1, mode_t mode2, struct got_repository *repo)
5995 struct check_path_prefix_arg *a = arg;
5997 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5998 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5999 return got_error(a->errcode);
6004 static const struct got_error *
6005 check_path_prefix(struct got_object_id *parent_id,
6006 struct got_object_id *commit_id, const char *path_prefix,
6007 int errcode, struct got_repository *repo)
6009 const struct got_error *err;
6010 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6011 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6012 struct check_path_prefix_arg cpp_arg;
6014 if (got_path_is_root_dir(path_prefix))
6017 err = got_object_open_as_commit(&commit, repo, commit_id);
6021 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6025 err = got_object_open_as_tree(&tree1, repo,
6026 got_object_commit_get_tree_id(parent_commit));
6030 err = got_object_open_as_tree(&tree2, repo,
6031 got_object_commit_get_tree_id(commit));
6035 cpp_arg.path_prefix = path_prefix;
6036 while (cpp_arg.path_prefix[0] == '/')
6037 cpp_arg.path_prefix++;
6038 cpp_arg.len = strlen(cpp_arg.path_prefix);
6039 cpp_arg.errcode = errcode;
6040 err = got_diff_tree(tree1, tree2, "", "", repo,
6041 check_path_prefix_in_diff, &cpp_arg, 0);
6044 got_object_tree_close(tree1);
6046 got_object_tree_close(tree2);
6048 got_object_commit_close(commit);
6050 got_object_commit_close(parent_commit);
6054 static const struct got_error *
6055 collect_commits(struct got_object_id_queue *commits,
6056 struct got_object_id *initial_commit_id,
6057 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6058 const char *path_prefix, int path_prefix_errcode,
6059 struct got_repository *repo)
6061 const struct got_error *err = NULL;
6062 struct got_commit_graph *graph = NULL;
6063 struct got_object_id *parent_id = NULL;
6064 struct got_object_qid *qid;
6065 struct got_object_id *commit_id = initial_commit_id;
6067 err = got_commit_graph_open(&graph, "/", 1);
6071 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6072 check_cancelled, NULL);
6075 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6076 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6077 check_cancelled, NULL);
6079 if (err->code == GOT_ERR_ITER_COMPLETED) {
6080 err = got_error_msg(GOT_ERR_ANCESTRY,
6081 "ran out of commits to rebase before "
6082 "youngest common ancestor commit has "
6087 err = check_path_prefix(parent_id, commit_id,
6088 path_prefix, path_prefix_errcode, repo);
6092 err = got_object_qid_alloc(&qid, commit_id);
6095 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6096 commit_id = parent_id;
6100 got_commit_graph_close(graph);
6104 static const struct got_error *
6105 cmd_rebase(int argc, char *argv[])
6107 const struct got_error *error = NULL;
6108 struct got_worktree *worktree = NULL;
6109 struct got_repository *repo = NULL;
6110 struct got_fileindex *fileindex = NULL;
6112 struct got_reference *branch = NULL;
6113 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6114 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6115 struct got_object_id *resume_commit_id = NULL;
6116 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6117 struct got_commit_object *commit = NULL;
6118 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6119 int histedit_in_progress = 0;
6120 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6121 struct got_object_id_queue commits;
6122 struct got_pathlist_head merged_paths;
6123 const struct got_object_id_queue *parent_ids;
6124 struct got_object_qid *qid, *pid;
6126 SIMPLEQ_INIT(&commits);
6127 TAILQ_INIT(&merged_paths);
6129 while ((ch = getopt(argc, argv, "ac")) != -1) {
6135 continue_rebase = 1;
6147 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6148 "unveil", NULL) == -1)
6151 if (abort_rebase && continue_rebase)
6153 else if (abort_rebase || continue_rebase) {
6156 } else if (argc != 1)
6159 cwd = getcwd(NULL, 0);
6161 error = got_error_from_errno("getcwd");
6164 error = got_worktree_open(&worktree, cwd);
6168 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6173 error = apply_unveil(got_repo_get_path(repo), 0,
6174 got_worktree_get_root_path(worktree));
6178 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6182 if (histedit_in_progress) {
6183 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6187 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6193 if (!rebase_in_progress) {
6194 error = got_error(GOT_ERR_NOT_REBASING);
6197 error = got_worktree_rebase_continue(&resume_commit_id,
6198 &new_base_branch, &tmp_branch, &branch, &fileindex,
6202 printf("Switching work tree to %s\n",
6203 got_ref_get_symref_target(new_base_branch));
6204 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6205 new_base_branch, update_progress, &did_something);
6208 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6209 goto done; /* nothing else to do */
6212 if (continue_rebase) {
6213 if (!rebase_in_progress) {
6214 error = got_error(GOT_ERR_NOT_REBASING);
6217 error = got_worktree_rebase_continue(&resume_commit_id,
6218 &new_base_branch, &tmp_branch, &branch, &fileindex,
6223 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6224 resume_commit_id, repo);
6228 yca_id = got_object_id_dup(resume_commit_id);
6229 if (yca_id == NULL) {
6230 error = got_error_from_errno("got_object_id_dup");
6234 error = got_ref_open(&branch, repo, argv[0], 0);
6239 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6243 if (!continue_rebase) {
6244 struct got_object_id *base_commit_id;
6246 base_commit_id = got_worktree_get_base_commit_id(worktree);
6247 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6248 base_commit_id, branch_head_commit_id, repo,
6249 check_cancelled, NULL);
6252 if (yca_id == NULL) {
6253 error = got_error_msg(GOT_ERR_ANCESTRY,
6254 "specified branch shares no common ancestry "
6255 "with work tree's branch");
6259 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6261 if (error->code != GOT_ERR_ANCESTRY)
6265 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6266 "specified branch resolves to a commit which "
6267 "is already contained in work tree's branch");
6270 error = got_worktree_rebase_prepare(&new_base_branch,
6271 &tmp_branch, &fileindex, worktree, branch, repo);
6276 commit_id = branch_head_commit_id;
6277 error = got_object_open_as_commit(&commit, repo, commit_id);
6281 parent_ids = got_object_commit_get_parent_ids(commit);
6282 pid = SIMPLEQ_FIRST(parent_ids);
6284 if (!continue_rebase) {
6286 error = got_worktree_rebase_abort(worktree, fileindex,
6287 repo, new_base_branch, update_progress,
6291 printf("Rebase of %s aborted\n",
6292 got_ref_get_name(branch));
6294 error = got_error(GOT_ERR_EMPTY_REBASE);
6297 error = collect_commits(&commits, commit_id, pid->id,
6298 yca_id, got_worktree_get_path_prefix(worktree),
6299 GOT_ERR_REBASE_PATH, repo);
6300 got_object_commit_close(commit);
6305 if (SIMPLEQ_EMPTY(&commits)) {
6306 if (continue_rebase) {
6307 error = rebase_complete(worktree, fileindex,
6308 branch, new_base_branch, tmp_branch, repo);
6311 /* Fast-forward the reference of the branch. */
6312 struct got_object_id *new_head_commit_id;
6314 error = got_ref_resolve(&new_head_commit_id, repo,
6318 error = got_object_id_str(&id_str, new_head_commit_id);
6319 printf("Forwarding %s to commit %s\n",
6320 got_ref_get_name(branch), id_str);
6322 error = got_ref_change_ref(branch,
6323 new_head_commit_id);
6330 SIMPLEQ_FOREACH(qid, &commits, entry) {
6331 commit_id = qid->id;
6332 parent_id = pid ? pid->id : yca_id;
6335 error = got_worktree_rebase_merge_files(&merged_paths,
6336 worktree, fileindex, parent_id, commit_id, repo,
6337 rebase_progress, &rebase_status, check_cancelled, NULL);
6341 if (rebase_status == GOT_STATUS_CONFLICT) {
6342 error = show_rebase_merge_conflict(qid->id, repo);
6345 got_worktree_rebase_pathlist_free(&merged_paths);
6349 error = rebase_commit(&merged_paths, worktree, fileindex,
6350 tmp_branch, commit_id, repo);
6351 got_worktree_rebase_pathlist_free(&merged_paths);
6356 if (rebase_status == GOT_STATUS_CONFLICT) {
6357 error = got_worktree_rebase_postpone(worktree, fileindex);
6360 error = got_error_msg(GOT_ERR_CONFLICTS,
6361 "conflicts must be resolved before rebasing can continue");
6363 error = rebase_complete(worktree, fileindex, branch,
6364 new_base_branch, tmp_branch, repo);
6366 got_object_id_queue_free(&commits);
6367 free(branch_head_commit_id);
6368 free(resume_commit_id);
6371 got_object_commit_close(commit);
6373 got_ref_close(branch);
6374 if (new_base_branch)
6375 got_ref_close(new_base_branch);
6377 got_ref_close(tmp_branch);
6379 got_worktree_close(worktree);
6381 got_repo_close(repo);
6386 usage_histedit(void)
6388 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6393 #define GOT_HISTEDIT_PICK 'p'
6394 #define GOT_HISTEDIT_EDIT 'e'
6395 #define GOT_HISTEDIT_FOLD 'f'
6396 #define GOT_HISTEDIT_DROP 'd'
6397 #define GOT_HISTEDIT_MESG 'm'
6399 static struct got_histedit_cmd {
6403 } got_histedit_cmds[] = {
6404 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6405 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6406 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6408 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6409 { GOT_HISTEDIT_MESG, "mesg",
6410 "single-line log message for commit above (open editor if empty)" },
6413 struct got_histedit_list_entry {
6414 TAILQ_ENTRY(got_histedit_list_entry) entry;
6415 struct got_object_id *commit_id;
6416 const struct got_histedit_cmd *cmd;
6419 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6421 static const struct got_error *
6422 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6423 FILE *f, struct got_repository *repo)
6425 const struct got_error *err = NULL;
6426 char *logmsg = NULL, *id_str = NULL;
6427 struct got_commit_object *commit = NULL;
6430 err = got_object_open_as_commit(&commit, repo, commit_id);
6434 err = get_short_logmsg(&logmsg, 34, commit);
6438 err = got_object_id_str(&id_str, commit_id);
6442 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6444 err = got_ferror(f, GOT_ERR_IO);
6447 got_object_commit_close(commit);
6453 static const struct got_error *
6454 histedit_write_commit_list(struct got_object_id_queue *commits,
6455 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6457 const struct got_error *err = NULL;
6458 struct got_object_qid *qid;
6460 if (SIMPLEQ_EMPTY(commits))
6461 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6463 SIMPLEQ_FOREACH(qid, commits, entry) {
6464 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6468 if (edit_logmsg_only) {
6469 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6471 err = got_ferror(f, GOT_ERR_IO);
6480 static const struct got_error *
6481 write_cmd_list(FILE *f, const char *branch_name,
6482 struct got_object_id_queue *commits)
6484 const struct got_error *err = NULL;
6487 struct got_object_qid *qid;
6489 qid = SIMPLEQ_FIRST(commits);
6490 err = got_object_id_str(&id_str, qid->id);
6495 "# Editing the history of branch '%s' starting at\n"
6497 "# Commits will be processed in order from top to "
6498 "bottom of this file.\n", branch_name, id_str);
6500 err = got_ferror(f, GOT_ERR_IO);
6504 n = fprintf(f, "# Available histedit commands:\n");
6506 err = got_ferror(f, GOT_ERR_IO);
6510 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6511 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6512 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6515 err = got_ferror(f, GOT_ERR_IO);
6524 static const struct got_error *
6525 histedit_syntax_error(int lineno)
6527 static char msg[42];
6530 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6532 if (ret == -1 || ret >= sizeof(msg))
6533 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6535 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6538 static const struct got_error *
6539 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6540 char *logmsg, struct got_repository *repo)
6542 const struct got_error *err;
6543 struct got_commit_object *folded_commit = NULL;
6544 char *id_str, *folded_logmsg = NULL;
6546 err = got_object_id_str(&id_str, hle->commit_id);
6550 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6554 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6557 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6558 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6559 folded_logmsg) == -1) {
6560 err = got_error_from_errno("asprintf");
6564 got_object_commit_close(folded_commit);
6566 free(folded_logmsg);
6570 static struct got_histedit_list_entry *
6571 get_folded_commits(struct got_histedit_list_entry *hle)
6573 struct got_histedit_list_entry *prev, *folded = NULL;
6575 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6576 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6577 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6578 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6580 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6586 static const struct got_error *
6587 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6588 struct got_repository *repo)
6590 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6591 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6592 const struct got_error *err = NULL;
6593 struct got_commit_object *commit = NULL;
6595 struct got_histedit_list_entry *folded = NULL;
6597 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6601 folded = get_folded_commits(hle);
6603 while (folded != hle) {
6604 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6605 folded = TAILQ_NEXT(folded, entry);
6608 err = append_folded_commit_msg(&new_msg, folded,
6614 folded = TAILQ_NEXT(folded, entry);
6618 err = got_object_id_str(&id_str, hle->commit_id);
6621 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6624 if (asprintf(&new_msg,
6625 "%s\n# original log message of commit %s: %s",
6626 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6627 err = got_error_from_errno("asprintf");
6633 err = got_object_id_str(&id_str, hle->commit_id);
6637 err = got_opentemp_named_fd(&logmsg_path, &fd,
6638 GOT_TMPDIR_STR "/got-logmsg");
6642 dprintf(fd, logmsg);
6645 err = get_editor(&editor);
6649 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6651 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6653 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6656 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6657 err = got_error_from_errno2("unlink", logmsg_path);
6663 got_object_commit_close(commit);
6667 static const struct got_error *
6668 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6669 FILE *f, struct got_repository *repo)
6671 const struct got_error *err = NULL;
6672 char *line = NULL, *p, *end;
6676 const struct got_histedit_cmd *cmd;
6677 struct got_object_id *commit_id = NULL;
6678 struct got_histedit_list_entry *hle = NULL;
6681 len = getline(&line, &size, f);
6683 const struct got_error *getline_err;
6686 getline_err = got_error_from_errno("getline");
6687 err = got_ferror(f, getline_err->code);
6692 while (isspace((unsigned char)p[0]))
6694 if (p[0] == '#' || p[0] == '\0') {
6700 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6701 cmd = &got_histedit_cmds[i];
6702 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6703 isspace((unsigned char)p[strlen(cmd->name)])) {
6704 p += strlen(cmd->name);
6707 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6712 if (i == nitems(got_histedit_cmds)) {
6713 err = histedit_syntax_error(lineno);
6716 while (isspace((unsigned char)p[0]))
6718 if (cmd->code == GOT_HISTEDIT_MESG) {
6719 if (hle == NULL || hle->logmsg != NULL) {
6720 err = got_error(GOT_ERR_HISTEDIT_CMD);
6724 err = histedit_edit_logmsg(hle, repo);
6728 hle->logmsg = strdup(p);
6729 if (hle->logmsg == NULL) {
6730 err = got_error_from_errno("strdup");
6739 while (end[0] && !isspace((unsigned char)end[0]))
6743 err = got_object_resolve_id_str(&commit_id, repo, p);
6745 /* override error code */
6746 err = histedit_syntax_error(lineno);
6750 hle = malloc(sizeof(*hle));
6752 err = got_error_from_errno("malloc");
6756 hle->commit_id = commit_id;
6761 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6769 static const struct got_error *
6770 histedit_check_script(struct got_histedit_list *histedit_cmds,
6771 struct got_object_id_queue *commits, struct got_repository *repo)
6773 const struct got_error *err = NULL;
6774 struct got_object_qid *qid;
6775 struct got_histedit_list_entry *hle;
6776 static char msg[92];
6779 if (TAILQ_EMPTY(histedit_cmds))
6780 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6781 "histedit script contains no commands");
6782 if (SIMPLEQ_EMPTY(commits))
6783 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6785 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6786 struct got_histedit_list_entry *hle2;
6787 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6790 if (got_object_id_cmp(hle->commit_id,
6791 hle2->commit_id) != 0)
6793 err = got_object_id_str(&id_str, hle->commit_id);
6796 snprintf(msg, sizeof(msg), "commit %s is listed "
6797 "more than once in histedit script", id_str);
6799 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6803 SIMPLEQ_FOREACH(qid, commits, entry) {
6804 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6805 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6809 err = got_object_id_str(&id_str, qid->id);
6812 snprintf(msg, sizeof(msg),
6813 "commit %s missing from histedit script", id_str);
6815 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6819 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6820 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6821 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6822 "last commit in histedit script cannot be folded");
6827 static const struct got_error *
6828 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6829 const char *path, struct got_object_id_queue *commits,
6830 struct got_repository *repo)
6832 const struct got_error *err = NULL;
6836 err = get_editor(&editor);
6840 if (spawn_editor(editor, path) == -1) {
6841 err = got_error_from_errno("failed spawning editor");
6845 f = fopen(path, "r");
6847 err = got_error_from_errno("fopen");
6850 err = histedit_parse_list(histedit_cmds, f, repo);
6854 err = histedit_check_script(histedit_cmds, commits, repo);
6856 if (f && fclose(f) != 0 && err == NULL)
6857 err = got_error_from_errno("fclose");
6862 static const struct got_error *
6863 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6864 struct got_object_id_queue *, const char *, const char *,
6865 struct got_repository *);
6867 static const struct got_error *
6868 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6869 struct got_object_id_queue *commits, const char *branch_name,
6870 int edit_logmsg_only, struct got_repository *repo)
6872 const struct got_error *err;
6876 err = got_opentemp_named(&path, &f, "got-histedit");
6880 err = write_cmd_list(f, branch_name, commits);
6884 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6888 if (edit_logmsg_only) {
6890 err = histedit_parse_list(histedit_cmds, f, repo);
6892 if (fclose(f) != 0) {
6893 err = got_error_from_errno("fclose");
6897 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6899 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6900 err->code != GOT_ERR_HISTEDIT_CMD)
6902 err = histedit_edit_list_retry(histedit_cmds, err,
6903 commits, path, branch_name, repo);
6907 if (f && fclose(f) != 0 && err == NULL)
6908 err = got_error_from_errno("fclose");
6909 if (path && unlink(path) != 0 && err == NULL)
6910 err = got_error_from_errno2("unlink", path);
6915 static const struct got_error *
6916 histedit_save_list(struct got_histedit_list *histedit_cmds,
6917 struct got_worktree *worktree, struct got_repository *repo)
6919 const struct got_error *err = NULL;
6922 struct got_histedit_list_entry *hle;
6923 struct got_commit_object *commit = NULL;
6925 err = got_worktree_get_histedit_script_path(&path, worktree);
6929 f = fopen(path, "w");
6931 err = got_error_from_errno2("fopen", path);
6934 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6935 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6941 int n = fprintf(f, "%c %s\n",
6942 GOT_HISTEDIT_MESG, hle->logmsg);
6944 err = got_ferror(f, GOT_ERR_IO);
6950 if (f && fclose(f) != 0 && err == NULL)
6951 err = got_error_from_errno("fclose");
6954 got_object_commit_close(commit);
6959 histedit_free_list(struct got_histedit_list *histedit_cmds)
6961 struct got_histedit_list_entry *hle;
6963 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6964 TAILQ_REMOVE(histedit_cmds, hle, entry);
6969 static const struct got_error *
6970 histedit_load_list(struct got_histedit_list *histedit_cmds,
6971 const char *path, struct got_repository *repo)
6973 const struct got_error *err = NULL;
6976 f = fopen(path, "r");
6978 err = got_error_from_errno2("fopen", path);
6982 err = histedit_parse_list(histedit_cmds, f, repo);
6984 if (f && fclose(f) != 0 && err == NULL)
6985 err = got_error_from_errno("fclose");
6989 static const struct got_error *
6990 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6991 const struct got_error *edit_err, struct got_object_id_queue *commits,
6992 const char *path, const char *branch_name, struct got_repository *repo)
6994 const struct got_error *err = NULL, *prev_err = edit_err;
6997 while (resp != 'c' && resp != 'r' && resp != 'a') {
6998 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6999 "or (a)bort: ", getprogname(), prev_err->msg);
7004 histedit_free_list(histedit_cmds);
7005 err = histedit_run_editor(histedit_cmds, path, commits,
7008 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7009 err->code != GOT_ERR_HISTEDIT_CMD)
7016 } else if (resp == 'r') {
7017 histedit_free_list(histedit_cmds);
7018 err = histedit_edit_script(histedit_cmds,
7019 commits, branch_name, 0, repo);
7021 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7022 err->code != GOT_ERR_HISTEDIT_CMD)
7029 } else if (resp == 'a') {
7030 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7033 printf("invalid response '%c'\n", resp);
7039 static const struct got_error *
7040 histedit_complete(struct got_worktree *worktree,
7041 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7042 struct got_reference *branch, struct got_repository *repo)
7044 printf("Switching work tree to %s\n",
7045 got_ref_get_symref_target(branch));
7046 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7050 static const struct got_error *
7051 show_histedit_progress(struct got_commit_object *commit,
7052 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7054 const struct got_error *err;
7055 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7057 err = got_object_id_str(&old_id_str, hle->commit_id);
7062 err = got_object_id_str(&new_id_str, new_id);
7067 old_id_str[12] = '\0';
7069 new_id_str[12] = '\0';
7072 logmsg = strdup(hle->logmsg);
7073 if (logmsg == NULL) {
7074 err = got_error_from_errno("strdup");
7077 trim_logmsg(logmsg, 42);
7079 err = get_short_logmsg(&logmsg, 42, commit);
7084 switch (hle->cmd->code) {
7085 case GOT_HISTEDIT_PICK:
7086 case GOT_HISTEDIT_EDIT:
7087 printf("%s -> %s: %s\n", old_id_str,
7088 new_id_str ? new_id_str : "no-op change", logmsg);
7090 case GOT_HISTEDIT_DROP:
7091 case GOT_HISTEDIT_FOLD:
7092 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7104 static const struct got_error *
7105 histedit_commit(struct got_pathlist_head *merged_paths,
7106 struct got_worktree *worktree, struct got_fileindex *fileindex,
7107 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7108 struct got_repository *repo)
7110 const struct got_error *err;
7111 struct got_commit_object *commit;
7112 struct got_object_id *new_commit_id;
7114 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7115 && hle->logmsg == NULL) {
7116 err = histedit_edit_logmsg(hle, repo);
7121 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7125 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7126 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7129 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7131 err = show_histedit_progress(commit, hle, NULL);
7133 err = show_histedit_progress(commit, hle, new_commit_id);
7134 free(new_commit_id);
7137 got_object_commit_close(commit);
7141 static const struct got_error *
7142 histedit_skip_commit(struct got_histedit_list_entry *hle,
7143 struct got_worktree *worktree, struct got_repository *repo)
7145 const struct got_error *error;
7146 struct got_commit_object *commit;
7148 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7153 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7157 error = show_histedit_progress(commit, hle, NULL);
7158 got_object_commit_close(commit);
7162 static const struct got_error *
7163 check_local_changes(void *arg, unsigned char status,
7164 unsigned char staged_status, const char *path,
7165 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7166 struct got_object_id *commit_id, int dirfd, const char *de_name)
7168 int *have_local_changes = arg;
7171 case GOT_STATUS_ADD:
7172 case GOT_STATUS_DELETE:
7173 case GOT_STATUS_MODIFY:
7174 case GOT_STATUS_CONFLICT:
7175 *have_local_changes = 1;
7176 return got_error(GOT_ERR_CANCELLED);
7181 switch (staged_status) {
7182 case GOT_STATUS_ADD:
7183 case GOT_STATUS_DELETE:
7184 case GOT_STATUS_MODIFY:
7185 *have_local_changes = 1;
7186 return got_error(GOT_ERR_CANCELLED);
7194 static const struct got_error *
7195 cmd_histedit(int argc, char *argv[])
7197 const struct got_error *error = NULL;
7198 struct got_worktree *worktree = NULL;
7199 struct got_fileindex *fileindex = NULL;
7200 struct got_repository *repo = NULL;
7202 struct got_reference *branch = NULL;
7203 struct got_reference *tmp_branch = NULL;
7204 struct got_object_id *resume_commit_id = NULL;
7205 struct got_object_id *base_commit_id = NULL;
7206 struct got_object_id *head_commit_id = NULL;
7207 struct got_commit_object *commit = NULL;
7208 int ch, rebase_in_progress = 0, did_something;
7209 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7210 int edit_logmsg_only = 0;
7211 const char *edit_script_path = NULL;
7212 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7213 struct got_object_id_queue commits;
7214 struct got_pathlist_head merged_paths;
7215 const struct got_object_id_queue *parent_ids;
7216 struct got_object_qid *pid;
7217 struct got_histedit_list histedit_cmds;
7218 struct got_histedit_list_entry *hle;
7220 SIMPLEQ_INIT(&commits);
7221 TAILQ_INIT(&histedit_cmds);
7222 TAILQ_INIT(&merged_paths);
7224 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7233 edit_script_path = optarg;
7236 edit_logmsg_only = 1;
7248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7249 "unveil", NULL) == -1)
7252 if (abort_edit && continue_edit)
7253 errx(1, "histedit's -a and -c options are mutually exclusive");
7254 if (edit_script_path && edit_logmsg_only)
7255 errx(1, "histedit's -F and -m options are mutually exclusive");
7256 if (abort_edit && edit_logmsg_only)
7257 errx(1, "histedit's -a and -m options are mutually exclusive");
7258 if (continue_edit && edit_logmsg_only)
7259 errx(1, "histedit's -c and -m options are mutually exclusive");
7264 * This command cannot apply unveil(2) in all cases because the
7265 * user may choose to run an editor to edit the histedit script
7266 * and to edit individual commit log messages.
7267 * unveil(2) traverses exec(2); if an editor is used we have to
7268 * apply unveil after edit script and log messages have been written.
7269 * XXX TODO: Make use of unveil(2) where possible.
7272 cwd = getcwd(NULL, 0);
7274 error = got_error_from_errno("getcwd");
7277 error = got_worktree_open(&worktree, cwd);
7281 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7286 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7289 if (rebase_in_progress) {
7290 error = got_error(GOT_ERR_REBASING);
7294 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7298 if (edit_in_progress && edit_logmsg_only) {
7299 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7300 "histedit operation is in progress in this "
7301 "work tree and must be continued or aborted "
7302 "before the -m option can be used");
7306 if (edit_in_progress && abort_edit) {
7307 error = got_worktree_histedit_continue(&resume_commit_id,
7308 &tmp_branch, &branch, &base_commit_id, &fileindex,
7312 printf("Switching work tree to %s\n",
7313 got_ref_get_symref_target(branch));
7314 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7315 branch, base_commit_id, update_progress, &did_something);
7318 printf("Histedit of %s aborted\n",
7319 got_ref_get_symref_target(branch));
7320 goto done; /* nothing else to do */
7321 } else if (abort_edit) {
7322 error = got_error(GOT_ERR_NOT_HISTEDIT);
7326 if (continue_edit) {
7329 if (!edit_in_progress) {
7330 error = got_error(GOT_ERR_NOT_HISTEDIT);
7334 error = got_worktree_get_histedit_script_path(&path, worktree);
7338 error = histedit_load_list(&histedit_cmds, path, repo);
7343 error = got_worktree_histedit_continue(&resume_commit_id,
7344 &tmp_branch, &branch, &base_commit_id, &fileindex,
7349 error = got_ref_resolve(&head_commit_id, repo, branch);
7353 error = got_object_open_as_commit(&commit, repo,
7357 parent_ids = got_object_commit_get_parent_ids(commit);
7358 pid = SIMPLEQ_FIRST(parent_ids);
7360 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7363 error = collect_commits(&commits, head_commit_id, pid->id,
7364 base_commit_id, got_worktree_get_path_prefix(worktree),
7365 GOT_ERR_HISTEDIT_PATH, repo);
7366 got_object_commit_close(commit);
7371 if (edit_in_progress) {
7372 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7376 error = got_ref_open(&branch, repo,
7377 got_worktree_get_head_ref_name(worktree), 0);
7381 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7382 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7383 "will not edit commit history of a branch outside "
7384 "the \"refs/heads/\" reference namespace");
7388 error = got_ref_resolve(&head_commit_id, repo, branch);
7389 got_ref_close(branch);
7394 error = got_object_open_as_commit(&commit, repo,
7398 parent_ids = got_object_commit_get_parent_ids(commit);
7399 pid = SIMPLEQ_FIRST(parent_ids);
7401 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7404 error = collect_commits(&commits, head_commit_id, pid->id,
7405 got_worktree_get_base_commit_id(worktree),
7406 got_worktree_get_path_prefix(worktree),
7407 GOT_ERR_HISTEDIT_PATH, repo);
7408 got_object_commit_close(commit);
7413 if (SIMPLEQ_EMPTY(&commits)) {
7414 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7418 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7419 &base_commit_id, &fileindex, worktree, repo);
7423 if (edit_script_path) {
7424 error = histedit_load_list(&histedit_cmds,
7425 edit_script_path, repo);
7427 got_worktree_histedit_abort(worktree, fileindex,
7428 repo, branch, base_commit_id,
7429 update_progress, &did_something);
7433 const char *branch_name;
7434 branch_name = got_ref_get_symref_target(branch);
7435 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7437 error = histedit_edit_script(&histedit_cmds, &commits,
7438 branch_name, edit_logmsg_only, repo);
7440 got_worktree_histedit_abort(worktree, fileindex,
7441 repo, branch, base_commit_id,
7442 update_progress, &did_something);
7448 error = histedit_save_list(&histedit_cmds, worktree,
7451 got_worktree_histedit_abort(worktree, fileindex,
7452 repo, branch, base_commit_id,
7453 update_progress, &did_something);
7459 error = histedit_check_script(&histedit_cmds, &commits, repo);
7463 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7464 if (resume_commit_id) {
7465 if (got_object_id_cmp(hle->commit_id,
7466 resume_commit_id) != 0)
7469 resume_commit_id = NULL;
7470 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7471 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7472 error = histedit_skip_commit(hle, worktree,
7477 struct got_pathlist_head paths;
7478 int have_changes = 0;
7481 error = got_pathlist_append(&paths, "", NULL);
7484 error = got_worktree_status(worktree, &paths,
7485 repo, check_local_changes, &have_changes,
7486 check_cancelled, NULL);
7487 got_pathlist_free(&paths);
7489 if (error->code != GOT_ERR_CANCELLED)
7491 if (sigint_received || sigpipe_received)
7495 error = histedit_commit(NULL, worktree,
7496 fileindex, tmp_branch, hle, repo);
7500 error = got_object_open_as_commit(
7501 &commit, repo, hle->commit_id);
7504 error = show_histedit_progress(commit,
7506 got_object_commit_close(commit);
7515 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7516 error = histedit_skip_commit(hle, worktree, repo);
7522 error = got_object_open_as_commit(&commit, repo,
7526 parent_ids = got_object_commit_get_parent_ids(commit);
7527 pid = SIMPLEQ_FIRST(parent_ids);
7529 error = got_worktree_histedit_merge_files(&merged_paths,
7530 worktree, fileindex, pid->id, hle->commit_id, repo,
7531 rebase_progress, &rebase_status, check_cancelled, NULL);
7534 got_object_commit_close(commit);
7537 if (rebase_status == GOT_STATUS_CONFLICT) {
7538 error = show_rebase_merge_conflict(hle->commit_id,
7542 got_worktree_rebase_pathlist_free(&merged_paths);
7546 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7548 error = got_object_id_str(&id_str, hle->commit_id);
7551 printf("Stopping histedit for amending commit %s\n",
7554 got_worktree_rebase_pathlist_free(&merged_paths);
7555 error = got_worktree_histedit_postpone(worktree,
7560 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7561 error = histedit_skip_commit(hle, worktree, repo);
7567 error = histedit_commit(&merged_paths, worktree, fileindex,
7568 tmp_branch, hle, repo);
7569 got_worktree_rebase_pathlist_free(&merged_paths);
7574 if (rebase_status == GOT_STATUS_CONFLICT) {
7575 error = got_worktree_histedit_postpone(worktree, fileindex);
7578 error = got_error_msg(GOT_ERR_CONFLICTS,
7579 "conflicts must be resolved before histedit can continue");
7581 error = histedit_complete(worktree, fileindex, tmp_branch,
7584 got_object_id_queue_free(&commits);
7585 histedit_free_list(&histedit_cmds);
7586 free(head_commit_id);
7587 free(base_commit_id);
7588 free(resume_commit_id);
7590 got_object_commit_close(commit);
7592 got_ref_close(branch);
7594 got_ref_close(tmp_branch);
7596 got_worktree_close(worktree);
7598 got_repo_close(repo);
7603 usage_integrate(void)
7605 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7609 static const struct got_error *
7610 cmd_integrate(int argc, char *argv[])
7612 const struct got_error *error = NULL;
7613 struct got_repository *repo = NULL;
7614 struct got_worktree *worktree = NULL;
7615 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7616 const char *branch_arg = NULL;
7617 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7618 struct got_fileindex *fileindex = NULL;
7619 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7620 int ch, did_something = 0;
7622 while ((ch = getopt(argc, argv, "")) != -1) {
7635 branch_arg = argv[0];
7637 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7638 "unveil", NULL) == -1)
7641 cwd = getcwd(NULL, 0);
7643 error = got_error_from_errno("getcwd");
7647 error = got_worktree_open(&worktree, cwd);
7651 error = check_rebase_or_histedit_in_progress(worktree);
7655 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7660 error = apply_unveil(got_repo_get_path(repo), 0,
7661 got_worktree_get_root_path(worktree));
7665 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7666 error = got_error_from_errno("asprintf");
7670 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7671 &base_branch_ref, worktree, refname, repo);
7675 refname = strdup(got_ref_get_name(branch_ref));
7676 if (refname == NULL) {
7677 error = got_error_from_errno("strdup");
7678 got_worktree_integrate_abort(worktree, fileindex, repo,
7679 branch_ref, base_branch_ref);
7682 base_refname = strdup(got_ref_get_name(base_branch_ref));
7683 if (base_refname == NULL) {
7684 error = got_error_from_errno("strdup");
7685 got_worktree_integrate_abort(worktree, fileindex, repo,
7686 branch_ref, base_branch_ref);
7690 error = got_ref_resolve(&commit_id, repo, branch_ref);
7694 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7698 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7699 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7700 "specified branch has already been integrated");
7701 got_worktree_integrate_abort(worktree, fileindex, repo,
7702 branch_ref, base_branch_ref);
7706 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7708 if (error->code == GOT_ERR_ANCESTRY)
7709 error = got_error(GOT_ERR_REBASE_REQUIRED);
7710 got_worktree_integrate_abort(worktree, fileindex, repo,
7711 branch_ref, base_branch_ref);
7715 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7716 branch_ref, base_branch_ref, update_progress, &did_something,
7717 check_cancelled, NULL);
7721 printf("Integrated %s into %s\n", refname, base_refname);
7724 got_repo_close(repo);
7726 got_worktree_close(worktree);
7728 free(base_commit_id);
7738 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7739 "[file-path ...]\n",
7744 static const struct got_error *
7745 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7746 const char *path, struct got_object_id *blob_id,
7747 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7748 int dirfd, const char *de_name)
7750 const struct got_error *err = NULL;
7751 char *id_str = NULL;
7753 if (staged_status != GOT_STATUS_ADD &&
7754 staged_status != GOT_STATUS_MODIFY &&
7755 staged_status != GOT_STATUS_DELETE)
7758 if (staged_status == GOT_STATUS_ADD ||
7759 staged_status == GOT_STATUS_MODIFY)
7760 err = got_object_id_str(&id_str, staged_blob_id);
7762 err = got_object_id_str(&id_str, blob_id);
7766 printf("%s %c %s\n", id_str, staged_status, path);
7771 static const struct got_error *
7772 cmd_stage(int argc, char *argv[])
7774 const struct got_error *error = NULL;
7775 struct got_repository *repo = NULL;
7776 struct got_worktree *worktree = NULL;
7778 struct got_pathlist_head paths;
7779 struct got_pathlist_entry *pe;
7780 int ch, list_stage = 0, pflag = 0;
7781 FILE *patch_script_file = NULL;
7782 const char *patch_script_path = NULL;
7783 struct choose_patch_arg cpa;
7787 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7796 patch_script_path = optarg;
7808 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7809 "unveil", NULL) == -1)
7812 if (list_stage && (pflag || patch_script_path))
7813 errx(1, "-l option cannot be used with other options");
7814 if (patch_script_path && !pflag)
7815 errx(1, "-F option can only be used together with -p option");
7817 cwd = getcwd(NULL, 0);
7819 error = got_error_from_errno("getcwd");
7823 error = got_worktree_open(&worktree, cwd);
7827 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7832 if (patch_script_path) {
7833 patch_script_file = fopen(patch_script_path, "r");
7834 if (patch_script_file == NULL) {
7835 error = got_error_from_errno2("fopen",
7840 error = apply_unveil(got_repo_get_path(repo), 0,
7841 got_worktree_get_root_path(worktree));
7845 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7850 error = got_worktree_status(worktree, &paths, repo,
7851 print_stage, NULL, check_cancelled, NULL);
7853 cpa.patch_script_file = patch_script_file;
7854 cpa.action = "stage";
7855 error = got_worktree_stage(worktree, &paths,
7856 pflag ? NULL : print_status, NULL,
7857 pflag ? choose_patch : NULL, &cpa, repo);
7860 if (patch_script_file && fclose(patch_script_file) == EOF &&
7862 error = got_error_from_errno2("fclose", patch_script_path);
7864 got_repo_close(repo);
7866 got_worktree_close(worktree);
7867 TAILQ_FOREACH(pe, &paths, entry)
7868 free((char *)pe->path);
7869 got_pathlist_free(&paths);
7877 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7878 "[file-path ...]\n",
7884 static const struct got_error *
7885 cmd_unstage(int argc, char *argv[])
7887 const struct got_error *error = NULL;
7888 struct got_repository *repo = NULL;
7889 struct got_worktree *worktree = NULL;
7891 struct got_pathlist_head paths;
7892 struct got_pathlist_entry *pe;
7893 int ch, did_something = 0, pflag = 0;
7894 FILE *patch_script_file = NULL;
7895 const char *patch_script_path = NULL;
7896 struct choose_patch_arg cpa;
7900 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7906 patch_script_path = optarg;
7918 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7919 "unveil", NULL) == -1)
7922 if (patch_script_path && !pflag)
7923 errx(1, "-F option can only be used together with -p option");
7925 cwd = getcwd(NULL, 0);
7927 error = got_error_from_errno("getcwd");
7931 error = got_worktree_open(&worktree, cwd);
7935 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7940 if (patch_script_path) {
7941 patch_script_file = fopen(patch_script_path, "r");
7942 if (patch_script_file == NULL) {
7943 error = got_error_from_errno2("fopen",
7949 error = apply_unveil(got_repo_get_path(repo), 0,
7950 got_worktree_get_root_path(worktree));
7954 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7958 cpa.patch_script_file = patch_script_file;
7959 cpa.action = "unstage";
7960 error = got_worktree_unstage(worktree, &paths, update_progress,
7961 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7963 if (patch_script_file && fclose(patch_script_file) == EOF &&
7965 error = got_error_from_errno2("fclose", patch_script_path);
7967 got_repo_close(repo);
7969 got_worktree_close(worktree);
7970 TAILQ_FOREACH(pe, &paths, entry)
7971 free((char *)pe->path);
7972 got_pathlist_free(&paths);
7980 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7981 "arg1 [arg2 ...]\n", getprogname());
7985 static const struct got_error *
7986 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7988 const struct got_error *err;
7989 struct got_blob_object *blob;
7991 err = got_object_open_as_blob(&blob, repo, id, 8192);
7995 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7996 got_object_blob_close(blob);
8000 static const struct got_error *
8001 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8003 const struct got_error *err;
8004 struct got_tree_object *tree;
8007 err = got_object_open_as_tree(&tree, repo, id);
8011 nentries = got_object_tree_get_nentries(tree);
8012 for (i = 0; i < nentries; i++) {
8013 struct got_tree_entry *te;
8015 if (sigint_received || sigpipe_received)
8017 te = got_object_tree_get_entry(tree, i);
8018 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8021 fprintf(outfile, "%s %.7o %s\n", id_str,
8022 got_tree_entry_get_mode(te),
8023 got_tree_entry_get_name(te));
8027 got_object_tree_close(tree);
8031 static const struct got_error *
8032 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8034 const struct got_error *err;
8035 struct got_commit_object *commit;
8036 const struct got_object_id_queue *parent_ids;
8037 struct got_object_qid *pid;
8038 char *id_str = NULL;
8039 const char *logmsg = NULL;
8041 err = got_object_open_as_commit(&commit, repo, id);
8045 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8049 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8050 parent_ids = got_object_commit_get_parent_ids(commit);
8051 fprintf(outfile, "numparents %d\n",
8052 got_object_commit_get_nparents(commit));
8053 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8055 err = got_object_id_str(&pid_str, pid->id);
8058 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8061 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8062 got_object_commit_get_author(commit),
8063 got_object_commit_get_author_time(commit));
8065 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8066 got_object_commit_get_author(commit),
8067 got_object_commit_get_committer_time(commit));
8069 logmsg = got_object_commit_get_logmsg_raw(commit);
8070 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8071 fprintf(outfile, "%s", logmsg);
8074 got_object_commit_close(commit);
8078 static const struct got_error *
8079 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8081 const struct got_error *err;
8082 struct got_tag_object *tag;
8083 char *id_str = NULL;
8084 const char *tagmsg = NULL;
8086 err = got_object_open_as_tag(&tag, repo, id);
8090 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8094 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8096 switch (got_object_tag_get_object_type(tag)) {
8097 case GOT_OBJ_TYPE_BLOB:
8098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8099 GOT_OBJ_LABEL_BLOB);
8101 case GOT_OBJ_TYPE_TREE:
8102 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8103 GOT_OBJ_LABEL_TREE);
8105 case GOT_OBJ_TYPE_COMMIT:
8106 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8107 GOT_OBJ_LABEL_COMMIT);
8109 case GOT_OBJ_TYPE_TAG:
8110 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8117 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8118 got_object_tag_get_name(tag));
8120 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8121 got_object_tag_get_tagger(tag),
8122 got_object_tag_get_tagger_time(tag));
8124 tagmsg = got_object_tag_get_message(tag);
8125 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8126 fprintf(outfile, "%s", tagmsg);
8129 got_object_tag_close(tag);
8133 static const struct got_error *
8134 cmd_cat(int argc, char *argv[])
8136 const struct got_error *error;
8137 struct got_repository *repo = NULL;
8138 struct got_worktree *worktree = NULL;
8139 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8140 const char *commit_id_str = NULL;
8141 struct got_object_id *id = NULL, *commit_id = NULL;
8142 int ch, obj_type, i, force_path = 0;
8145 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8150 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8153 commit_id_str = optarg;
8156 repo_path = realpath(optarg, NULL);
8157 if (repo_path == NULL)
8158 return got_error_from_errno2("realpath",
8160 got_path_strip_trailing_slashes(repo_path);
8174 cwd = getcwd(NULL, 0);
8176 error = got_error_from_errno("getcwd");
8179 error = got_worktree_open(&worktree, cwd);
8180 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8183 if (repo_path == NULL) {
8185 got_worktree_get_repo_path(worktree));
8186 if (repo_path == NULL) {
8187 error = got_error_from_errno("strdup");
8193 if (repo_path == NULL) {
8194 repo_path = getcwd(NULL, 0);
8195 if (repo_path == NULL)
8196 return got_error_from_errno("getcwd");
8199 error = got_repo_open(&repo, repo_path, NULL);
8204 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8208 if (commit_id_str == NULL)
8209 commit_id_str = GOT_REF_HEAD;
8210 error = got_repo_match_object_id(&commit_id, NULL,
8211 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8215 for (i = 0; i < argc; i++) {
8217 error = got_object_id_by_path(&id, repo, commit_id,
8222 error = got_repo_match_object_id(&id, &label, argv[i],
8223 GOT_OBJ_TYPE_ANY, 0, repo);
8225 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8226 error->code != GOT_ERR_NOT_REF)
8228 error = got_object_id_by_path(&id, repo,
8229 commit_id, argv[i]);
8235 error = got_object_get_type(&obj_type, repo, id);
8240 case GOT_OBJ_TYPE_BLOB:
8241 error = cat_blob(id, repo, stdout);
8243 case GOT_OBJ_TYPE_TREE:
8244 error = cat_tree(id, repo, stdout);
8246 case GOT_OBJ_TYPE_COMMIT:
8247 error = cat_commit(id, repo, stdout);
8249 case GOT_OBJ_TYPE_TAG:
8250 error = cat_tag(id, repo, stdout);
8253 error = got_error(GOT_ERR_OBJ_TYPE);
8268 got_worktree_close(worktree);
8270 const struct got_error *repo_error;
8271 repo_error = got_repo_close(repo);