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%s\n", host,
1017 port ? ":" : "", port ? port : "");
1019 /* Create a config file git-fetch(1) can understand. */
1020 gitconfig_path = got_repo_get_path_gitconfig(repo);
1021 if (gitconfig_path == NULL) {
1022 error = got_error_from_errno("got_repo_get_path_gitconfig");
1025 gitconfig_file = fopen(gitconfig_path, "a");
1026 if (gitconfig_file == NULL) {
1027 error = got_error_from_errno2("fopen", gitconfig_path);
1030 if (mirror_references) {
1031 if (asprintf(&gitconfig,
1034 "\tmirror = true\n",
1035 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1036 error = got_error_from_errno("asprintf");
1040 if (asprintf(&gitconfig,
1043 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1044 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1045 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1046 error = got_error_from_errno("asprintf");
1050 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1051 if (n != strlen(gitconfig)) {
1052 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1056 fpa.last_scaled_size[0] = '\0';
1057 fpa.last_p_indexed = -1;
1058 fpa.last_p_resolved = -1;
1059 fpa.verbosity = verbosity;
1060 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1061 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1062 fetchfd, repo, fetch_progress, &fpa);
1066 error = got_object_id_str(&id_str, pack_hash);
1070 printf("\nFetched %s.pack\n", id_str);
1073 /* Set up references provided with the pack file. */
1074 TAILQ_FOREACH(pe, &refs, entry) {
1075 const char *refname = pe->path;
1076 struct got_object_id *id = pe->data;
1077 struct got_reference *ref;
1078 char *remote_refname;
1080 error = got_ref_alloc(&ref, refname, id);
1083 error = got_ref_write(ref, repo);
1088 if (mirror_references)
1091 if (strncmp("refs/heads/", refname, 11) != 0)
1094 if (asprintf(&remote_refname,
1095 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1096 refname + 11) == -1) {
1097 error = got_error_from_errno("asprintf");
1100 error = got_ref_alloc(&ref, remote_refname, id);
1103 error = got_ref_write(ref, repo);
1109 /* Set the HEAD reference if the server provided one. */
1110 TAILQ_FOREACH(pe, &symrefs, entry) {
1111 struct got_reference *symref, *target_ref;
1112 const char *refname = pe->path;
1113 const char *target = pe->data;
1115 if (strcmp(refname, GOT_REF_HEAD) != 0)
1118 error = got_ref_open(&target_ref, repo, target, 0);
1120 if (error->code == GOT_ERR_NOT_REF)
1125 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1126 got_ref_close(target_ref);
1131 printf("Setting %s to %s\n", GOT_REF_HEAD,
1132 got_ref_get_symref_target(symref));
1134 error = got_ref_write(symref, repo);
1135 got_ref_close(symref);
1140 printf("Created %s repository '%s'\n",
1141 mirror_references ? "mirrored" : "cloned", repo_path);
1143 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1144 error = got_error_from_errno("close");
1145 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1146 error = got_error_from_errno("fclose");
1148 got_repo_close(repo);
1149 TAILQ_FOREACH(pe, &refs, entry) {
1150 free((void *)pe->path);
1153 got_pathlist_free(&refs);
1154 TAILQ_FOREACH(pe, &symrefs, entry) {
1155 free((void *)pe->path);
1158 got_pathlist_free(&symrefs);
1165 free(default_destdir);
1166 free(gitconfig_path);
1171 static const struct got_error *
1172 create_ref(const char *refname, struct got_object_id *id,
1173 const char *id_str, struct got_repository *repo)
1175 const struct got_error *err = NULL;
1176 struct got_reference *ref;
1178 printf("Creating %s: %s\n", refname, id_str);
1180 err = got_ref_alloc(&ref, refname, id);
1184 err = got_ref_write(ref, repo);
1189 static const struct got_error *
1190 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1191 struct got_repository *repo)
1193 const struct got_error *err = NULL;
1194 char *new_id_str = NULL;
1195 struct got_object_id *old_id = NULL;
1197 err = got_object_id_str(&new_id_str, new_id);
1201 if (got_ref_is_symbolic(ref)) {
1202 struct got_reference *new_ref;
1203 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1206 printf("Deleting symbolic reference %s -> %s\n",
1207 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1208 err = got_ref_delete(ref, repo);
1211 printf("Setting %s to %s\n", got_ref_get_name(ref),
1213 err = got_ref_write(new_ref, repo);
1217 err = got_ref_resolve(&old_id, repo, ref);
1220 if (got_object_id_cmp(old_id, new_id) != 0) {
1221 printf("Setting %s to %s\n",
1222 got_ref_get_name(ref), new_id_str);
1223 err = got_ref_change_ref(ref, new_id);
1226 err = got_ref_write(ref, repo);
1240 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1241 "[remote-repository-name]\n", getprogname());
1245 static const struct got_error *
1246 cmd_fetch(int argc, char *argv[])
1248 const struct got_error *error = NULL;
1249 char *cwd = NULL, *repo_path = NULL;
1250 const char *remote_name;
1251 char *proto = NULL, *host = NULL, *port = NULL;
1252 char *repo_name = NULL, *server_path = NULL;
1253 struct got_remote_repo *remotes, *remote = NULL;
1255 char *id_str = NULL;
1256 struct got_repository *repo = NULL;
1257 struct got_worktree *worktree = NULL;
1258 struct got_pathlist_head refs, symrefs;
1259 struct got_pathlist_entry *pe;
1260 struct got_object_id *pack_hash = NULL;
1261 int i, ch, fetchfd = -1;
1262 struct got_fetch_progress_arg fpa;
1266 TAILQ_INIT(&symrefs);
1268 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1271 repo_path = realpath(optarg, NULL);
1272 if (repo_path == NULL)
1273 return got_error_from_errno2("realpath",
1275 got_path_strip_trailing_slashes(repo_path);
1280 else if (verbosity < 3)
1295 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1297 remote_name = argv[0];
1301 cwd = getcwd(NULL, 0);
1303 error = got_error_from_errno("getcwd");
1307 if (repo_path == NULL) {
1308 error = got_worktree_open(&worktree, cwd);
1309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1315 strdup(got_worktree_get_repo_path(worktree));
1316 if (repo_path == NULL)
1317 error = got_error_from_errno("strdup");
1321 repo_path = strdup(cwd);
1322 if (repo_path == NULL) {
1323 error = got_error_from_errno("strdup");
1329 error = got_repo_open(&repo, repo_path, NULL);
1333 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1334 for (i = 0; i < nremotes; i++) {
1335 remote = &remotes[i];
1336 if (strcmp(remote->name, remote_name) == 0)
1339 if (i == nremotes) {
1340 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1344 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1345 &repo_name, remote->url);
1349 if (strcmp(proto, "git") == 0) {
1351 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1352 "sendfd dns inet unveil", NULL) == -1)
1355 } else if (strcmp(proto, "git+ssh") == 0 ||
1356 strcmp(proto, "ssh") == 0) {
1358 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1359 "sendfd unveil", NULL) == -1)
1362 } else if (strcmp(proto, "http") == 0 ||
1363 strcmp(proto, "git+http") == 0) {
1364 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1367 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1371 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1372 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1373 error = got_error_from_errno2("unveil",
1374 GOT_FETCH_PATH_SSH);
1378 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1382 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1388 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1389 port ? ":" : "", port ? port : "");
1391 fpa.last_scaled_size[0] = '\0';
1392 fpa.last_p_indexed = -1;
1393 fpa.last_p_resolved = -1;
1394 fpa.verbosity = verbosity;
1395 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1396 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1400 if (pack_hash == NULL) {
1402 printf("Already up-to-date\n");
1406 if (verbosity >= 0) {
1407 error = got_object_id_str(&id_str, pack_hash);
1410 printf("\nFetched %s.pack\n", id_str);
1415 /* Update references provided with the pack file. */
1416 TAILQ_FOREACH(pe, &refs, entry) {
1417 const char *refname = pe->path;
1418 struct got_object_id *id = pe->data;
1419 struct got_reference *ref;
1420 char *remote_refname;
1422 error = got_object_id_str(&id_str, id);
1426 if (strncmp("refs/tags/", refname, 10) == 0) {
1427 error = got_ref_open(&ref, repo, refname, 0);
1429 if (error->code != GOT_ERR_NOT_REF)
1431 error = create_ref(refname, id, id_str, repo);
1435 error = update_ref(ref, id, repo);
1440 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1441 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1442 remote_name, refname + 11) == -1) {
1443 error = got_error_from_errno("asprintf");
1447 error = got_ref_open(&ref, repo, remote_refname, 0);
1449 if (error->code != GOT_ERR_NOT_REF)
1451 error = create_ref(remote_refname, id, id_str,
1456 error = update_ref(ref, id, repo);
1466 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1467 error = got_error_from_errno("close");
1469 got_repo_close(repo);
1471 got_worktree_close(worktree);
1472 TAILQ_FOREACH(pe, &refs, entry) {
1473 free((void *)pe->path);
1476 got_pathlist_free(&refs);
1477 TAILQ_FOREACH(pe, &symrefs, entry) {
1478 free((void *)pe->path);
1481 got_pathlist_free(&symrefs);
1496 usage_checkout(void)
1498 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1499 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1504 show_worktree_base_ref_warning(void)
1506 fprintf(stderr, "%s: warning: could not create a reference "
1507 "to the work tree's base commit; the commit could be "
1508 "garbage-collected by Git; making the repository "
1509 "writable and running 'got update' will prevent this\n",
1513 struct got_checkout_progress_arg {
1514 const char *worktree_path;
1515 int had_base_commit_ref_error;
1518 static const struct got_error *
1519 checkout_progress(void *arg, unsigned char status, const char *path)
1521 struct got_checkout_progress_arg *a = arg;
1523 /* Base commit bump happens silently. */
1524 if (status == GOT_STATUS_BUMP_BASE)
1527 if (status == GOT_STATUS_BASE_REF_ERR) {
1528 a->had_base_commit_ref_error = 1;
1532 while (path[0] == '/')
1535 printf("%c %s/%s\n", status, a->worktree_path, path);
1539 static const struct got_error *
1540 check_cancelled(void *arg)
1542 if (sigint_received || sigpipe_received)
1543 return got_error(GOT_ERR_CANCELLED);
1547 static const struct got_error *
1548 check_linear_ancestry(struct got_object_id *commit_id,
1549 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1550 struct got_repository *repo)
1552 const struct got_error *err = NULL;
1553 struct got_object_id *yca_id;
1555 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1556 commit_id, base_commit_id, repo, check_cancelled, NULL);
1561 return got_error(GOT_ERR_ANCESTRY);
1564 * Require a straight line of history between the target commit
1565 * and the work tree's base commit.
1567 * Non-linear situations such as this require a rebase:
1569 * (commit) D F (base_commit)
1577 * 'got update' only handles linear cases:
1578 * Update forwards in time: A (base/yca) - B - C - D (commit)
1579 * Update backwards in time: D (base) - C - B - A (commit/yca)
1581 if (allow_forwards_in_time_only) {
1582 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1583 return got_error(GOT_ERR_ANCESTRY);
1584 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1585 got_object_id_cmp(base_commit_id, yca_id) != 0)
1586 return got_error(GOT_ERR_ANCESTRY);
1592 static const struct got_error *
1593 check_same_branch(struct got_object_id *commit_id,
1594 struct got_reference *head_ref, struct got_object_id *yca_id,
1595 struct got_repository *repo)
1597 const struct got_error *err = NULL;
1598 struct got_commit_graph *graph = NULL;
1599 struct got_object_id *head_commit_id = NULL;
1600 int is_same_branch = 0;
1602 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1606 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1610 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1615 err = got_commit_graph_open(&graph, "/", 1);
1619 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1620 check_cancelled, NULL);
1625 struct got_object_id *id;
1626 err = got_commit_graph_iter_next(&id, graph, repo,
1627 check_cancelled, NULL);
1629 if (err->code == GOT_ERR_ITER_COMPLETED)
1635 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1637 if (got_object_id_cmp(id, commit_id) == 0) {
1645 got_commit_graph_close(graph);
1646 free(head_commit_id);
1647 if (!err && !is_same_branch)
1648 err = got_error(GOT_ERR_ANCESTRY);
1652 static const struct got_error *
1653 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1655 static char msg[512];
1656 const char *branch_name;
1658 if (got_ref_is_symbolic(ref))
1659 branch_name = got_ref_get_symref_target(ref);
1661 branch_name = got_ref_get_name(ref);
1663 if (strncmp("refs/heads/", branch_name, 11) == 0)
1666 snprintf(msg, sizeof(msg),
1667 "target commit is not contained in branch '%s'; "
1668 "the branch to use must be specified with -b; "
1669 "if necessary a new branch can be created for "
1670 "this commit with 'got branch -c %s BRANCH_NAME'",
1671 branch_name, commit_id_str);
1673 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1676 static const struct got_error *
1677 cmd_checkout(int argc, char *argv[])
1679 const struct got_error *error = NULL;
1680 struct got_repository *repo = NULL;
1681 struct got_reference *head_ref = NULL;
1682 struct got_worktree *worktree = NULL;
1683 char *repo_path = NULL;
1684 char *worktree_path = NULL;
1685 const char *path_prefix = "";
1686 const char *branch_name = GOT_REF_HEAD;
1687 char *commit_id_str = NULL;
1688 int ch, same_path_prefix, allow_nonempty = 0;
1689 struct got_pathlist_head paths;
1690 struct got_checkout_progress_arg cpa;
1694 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1697 branch_name = optarg;
1700 commit_id_str = strdup(optarg);
1701 if (commit_id_str == NULL)
1702 return got_error_from_errno("strdup");
1708 path_prefix = optarg;
1720 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1721 "unveil", NULL) == -1)
1725 char *cwd, *base, *dotgit;
1726 repo_path = realpath(argv[0], NULL);
1727 if (repo_path == NULL)
1728 return got_error_from_errno2("realpath", argv[0]);
1729 cwd = getcwd(NULL, 0);
1731 error = got_error_from_errno("getcwd");
1734 if (path_prefix[0]) {
1735 base = basename(path_prefix);
1737 error = got_error_from_errno2("basename",
1742 base = basename(repo_path);
1744 error = got_error_from_errno2("basename",
1749 dotgit = strstr(base, ".git");
1752 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1753 error = got_error_from_errno("asprintf");
1758 } else if (argc == 2) {
1759 repo_path = realpath(argv[0], NULL);
1760 if (repo_path == NULL) {
1761 error = got_error_from_errno2("realpath", argv[0]);
1764 worktree_path = realpath(argv[1], NULL);
1765 if (worktree_path == NULL) {
1766 if (errno != ENOENT) {
1767 error = got_error_from_errno2("realpath",
1771 worktree_path = strdup(argv[1]);
1772 if (worktree_path == NULL) {
1773 error = got_error_from_errno("strdup");
1780 got_path_strip_trailing_slashes(repo_path);
1781 got_path_strip_trailing_slashes(worktree_path);
1783 error = got_repo_open(&repo, repo_path, NULL);
1787 /* Pre-create work tree path for unveil(2) */
1788 error = got_path_mkdir(worktree_path);
1790 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1791 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1793 if (!allow_nonempty &&
1794 !got_path_dir_is_empty(worktree_path)) {
1795 error = got_error_path(worktree_path,
1796 GOT_ERR_DIR_NOT_EMPTY);
1801 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1805 error = got_ref_open(&head_ref, repo, branch_name, 0);
1809 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1810 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1813 error = got_worktree_open(&worktree, worktree_path);
1817 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1821 if (!same_path_prefix) {
1822 error = got_error(GOT_ERR_PATH_PREFIX);
1826 if (commit_id_str) {
1827 struct got_object_id *commit_id;
1828 error = got_repo_match_object_id(&commit_id, NULL,
1829 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1832 error = check_linear_ancestry(commit_id,
1833 got_worktree_get_base_commit_id(worktree), 0, repo);
1834 if (error != NULL) {
1836 if (error->code == GOT_ERR_ANCESTRY) {
1837 error = checkout_ancestry_error(
1838 head_ref, commit_id_str);
1842 error = check_same_branch(commit_id, head_ref, NULL, repo);
1844 if (error->code == GOT_ERR_ANCESTRY) {
1845 error = checkout_ancestry_error(
1846 head_ref, commit_id_str);
1850 error = got_worktree_set_base_commit_id(worktree, repo,
1857 error = got_pathlist_append(&paths, "", NULL);
1860 cpa.worktree_path = worktree_path;
1861 cpa.had_base_commit_ref_error = 0;
1862 error = got_worktree_checkout_files(worktree, &paths, repo,
1863 checkout_progress, &cpa, check_cancelled, NULL);
1867 printf("Now shut up and hack\n");
1868 if (cpa.had_base_commit_ref_error)
1869 show_worktree_base_ref_warning();
1871 got_pathlist_free(&paths);
1872 free(commit_id_str);
1874 free(worktree_path);
1881 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1886 static const struct got_error *
1887 update_progress(void *arg, unsigned char status, const char *path)
1889 int *did_something = arg;
1891 if (status == GOT_STATUS_EXISTS ||
1892 status == GOT_STATUS_BASE_REF_ERR)
1897 /* Base commit bump happens silently. */
1898 if (status == GOT_STATUS_BUMP_BASE)
1901 while (path[0] == '/')
1903 printf("%c %s\n", status, path);
1907 static const struct got_error *
1908 switch_head_ref(struct got_reference *head_ref,
1909 struct got_object_id *commit_id, struct got_worktree *worktree,
1910 struct got_repository *repo)
1912 const struct got_error *err = NULL;
1914 int ref_has_moved = 0;
1916 /* Trivial case: switching between two different references. */
1917 if (strcmp(got_ref_get_name(head_ref),
1918 got_worktree_get_head_ref_name(worktree)) != 0) {
1919 printf("Switching work tree from %s to %s\n",
1920 got_worktree_get_head_ref_name(worktree),
1921 got_ref_get_name(head_ref));
1922 return got_worktree_set_head_ref(worktree, head_ref);
1925 err = check_linear_ancestry(commit_id,
1926 got_worktree_get_base_commit_id(worktree), 0, repo);
1928 if (err->code != GOT_ERR_ANCESTRY)
1935 /* Switching to a rebased branch with the same reference name. */
1936 err = got_object_id_str(&base_id_str,
1937 got_worktree_get_base_commit_id(worktree));
1940 printf("Reference %s now points at a different branch\n",
1941 got_worktree_get_head_ref_name(worktree));
1942 printf("Switching work tree from %s to %s\n", base_id_str,
1943 got_worktree_get_head_ref_name(worktree));
1947 static const struct got_error *
1948 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1950 const struct got_error *err;
1953 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1957 return got_error(GOT_ERR_REBASING);
1959 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1963 return got_error(GOT_ERR_HISTEDIT_BUSY);
1968 static const struct got_error *
1969 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1970 char *argv[], struct got_worktree *worktree)
1972 const struct got_error *err = NULL;
1979 return got_error_from_errno("strdup");
1980 return got_pathlist_append(paths, path, NULL);
1983 for (i = 0; i < argc; i++) {
1984 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1987 err = got_pathlist_append(paths, path, NULL);
1997 static const struct got_error *
1998 cmd_update(int argc, char *argv[])
2000 const struct got_error *error = NULL;
2001 struct got_repository *repo = NULL;
2002 struct got_worktree *worktree = NULL;
2003 char *worktree_path = NULL;
2004 struct got_object_id *commit_id = NULL;
2005 char *commit_id_str = NULL;
2006 const char *branch_name = NULL;
2007 struct got_reference *head_ref = NULL;
2008 struct got_pathlist_head paths;
2009 struct got_pathlist_entry *pe;
2010 int ch, did_something = 0;
2014 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2017 branch_name = optarg;
2020 commit_id_str = strdup(optarg);
2021 if (commit_id_str == NULL)
2022 return got_error_from_errno("strdup");
2034 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2035 "unveil", NULL) == -1)
2038 worktree_path = getcwd(NULL, 0);
2039 if (worktree_path == NULL) {
2040 error = got_error_from_errno("getcwd");
2043 error = got_worktree_open(&worktree, worktree_path);
2047 error = check_rebase_or_histedit_in_progress(worktree);
2051 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2056 error = apply_unveil(got_repo_get_path(repo), 0,
2057 got_worktree_get_root_path(worktree));
2061 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2065 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2066 got_worktree_get_head_ref_name(worktree), 0);
2069 if (commit_id_str == NULL) {
2070 error = got_ref_resolve(&commit_id, repo, head_ref);
2073 error = got_object_id_str(&commit_id_str, commit_id);
2077 error = got_repo_match_object_id(&commit_id, NULL,
2078 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2079 free(commit_id_str);
2080 commit_id_str = NULL;
2083 error = got_object_id_str(&commit_id_str, commit_id);
2089 struct got_object_id *head_commit_id;
2090 TAILQ_FOREACH(pe, &paths, entry) {
2091 if (pe->path_len == 0)
2093 error = got_error_msg(GOT_ERR_BAD_PATH,
2094 "switching between branches requires that "
2095 "the entire work tree gets updated");
2098 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2101 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2103 free(head_commit_id);
2106 error = check_same_branch(commit_id, head_ref, NULL, repo);
2109 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2113 error = check_linear_ancestry(commit_id,
2114 got_worktree_get_base_commit_id(worktree), 0, repo);
2115 if (error != NULL) {
2116 if (error->code == GOT_ERR_ANCESTRY)
2117 error = got_error(GOT_ERR_BRANCH_MOVED);
2120 error = check_same_branch(commit_id, head_ref, NULL, repo);
2125 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2127 error = got_worktree_set_base_commit_id(worktree, repo,
2133 error = got_worktree_checkout_files(worktree, &paths, repo,
2134 update_progress, &did_something, check_cancelled, NULL);
2139 printf("Updated to commit %s\n", commit_id_str);
2141 printf("Already up-to-date\n");
2143 free(worktree_path);
2144 TAILQ_FOREACH(pe, &paths, entry)
2145 free((char *)pe->path);
2146 got_pathlist_free(&paths);
2148 free(commit_id_str);
2152 static const struct got_error *
2153 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2154 const char *path, int diff_context, int ignore_whitespace,
2155 struct got_repository *repo)
2157 const struct got_error *err = NULL;
2158 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2161 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2166 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2170 while (path[0] == '/')
2172 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2173 ignore_whitespace, stdout);
2176 got_object_blob_close(blob1);
2177 got_object_blob_close(blob2);
2181 static const struct got_error *
2182 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2183 const char *path, int diff_context, int ignore_whitespace,
2184 struct got_repository *repo)
2186 const struct got_error *err = NULL;
2187 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2188 struct got_diff_blob_output_unidiff_arg arg;
2191 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2196 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2200 arg.diff_context = diff_context;
2201 arg.ignore_whitespace = ignore_whitespace;
2202 arg.outfile = stdout;
2203 while (path[0] == '/')
2205 err = got_diff_tree(tree1, tree2, path, path, repo,
2206 got_diff_blob_output_unidiff, &arg, 1);
2209 got_object_tree_close(tree1);
2211 got_object_tree_close(tree2);
2215 static const struct got_error *
2216 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2217 const char *path, int diff_context, struct got_repository *repo)
2219 const struct got_error *err = NULL;
2220 struct got_commit_object *pcommit = NULL;
2221 char *id_str1 = NULL, *id_str2 = NULL;
2222 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2223 struct got_object_qid *qid;
2225 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2227 err = got_object_open_as_commit(&pcommit, repo,
2233 if (path && path[0] != '\0') {
2235 err = got_object_id_by_path(&obj_id2, repo, id, path);
2238 err = got_object_id_str(&id_str2, obj_id2);
2244 err = got_object_id_by_path(&obj_id1, repo,
2250 err = got_object_id_str(&id_str1, obj_id1);
2256 err = got_object_get_type(&obj_type, repo, obj_id2);
2261 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2263 case GOT_OBJ_TYPE_BLOB:
2264 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2267 case GOT_OBJ_TYPE_TREE:
2268 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2272 err = got_error(GOT_ERR_OBJ_TYPE);
2278 obj_id2 = got_object_commit_get_tree_id(commit);
2279 err = got_object_id_str(&id_str2, obj_id2);
2282 obj_id1 = got_object_commit_get_tree_id(pcommit);
2283 err = got_object_id_str(&id_str1, obj_id1);
2286 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2287 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2293 got_object_commit_close(pcommit);
2298 get_datestr(time_t *time, char *datebuf)
2300 struct tm mytm, *tm;
2303 tm = gmtime_r(time, &mytm);
2306 s = asctime_r(tm, datebuf);
2309 p = strchr(s, '\n');
2315 static const struct got_error *
2316 match_logmsg(int *have_match, struct got_object_id *id,
2317 struct got_commit_object *commit, regex_t *regex)
2319 const struct got_error *err = NULL;
2320 regmatch_t regmatch;
2321 char *id_str = NULL, *logmsg = NULL;
2325 err = got_object_id_str(&id_str, id);
2329 err = got_object_commit_get_logmsg(&logmsg, commit);
2333 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2341 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2343 static const struct got_error *
2344 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2345 struct got_repository *repo, const char *path, int show_patch,
2346 int diff_context, struct got_reflist_head *refs)
2348 const struct got_error *err = NULL;
2349 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2351 time_t committer_time;
2352 const char *author, *committer;
2353 char *refs_str = NULL;
2354 struct got_reflist_entry *re;
2356 SIMPLEQ_FOREACH(re, refs, entry) {
2359 struct got_tag_object *tag = NULL;
2362 name = got_ref_get_name(re->ref);
2363 if (strcmp(name, GOT_REF_HEAD) == 0)
2365 if (strncmp(name, "refs/", 5) == 0)
2367 if (strncmp(name, "got/", 4) == 0)
2369 if (strncmp(name, "heads/", 6) == 0)
2371 if (strncmp(name, "remotes/", 8) == 0)
2373 if (strncmp(name, "tags/", 5) == 0) {
2374 err = got_object_open_as_tag(&tag, repo, re->id);
2376 if (err->code != GOT_ERR_OBJ_TYPE)
2378 /* Ref points at something other than a tag. */
2383 cmp = got_object_id_cmp(tag ?
2384 got_object_tag_get_object_id(tag) : re->id, id);
2386 got_object_tag_close(tag);
2390 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2392 err = got_error_from_errno("asprintf");
2398 err = got_object_id_str(&id_str, id);
2402 printf(GOT_COMMIT_SEP_STR);
2403 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2404 refs_str ? refs_str : "", refs_str ? ")" : "");
2409 printf("from: %s\n", got_object_commit_get_author(commit));
2410 committer_time = got_object_commit_get_committer_time(commit);
2411 datestr = get_datestr(&committer_time, datebuf);
2413 printf("date: %s UTC\n", datestr);
2414 author = got_object_commit_get_author(commit);
2415 committer = got_object_commit_get_committer(commit);
2416 if (strcmp(author, committer) != 0)
2417 printf("via: %s\n", committer);
2418 if (got_object_commit_get_nparents(commit) > 1) {
2419 const struct got_object_id_queue *parent_ids;
2420 struct got_object_qid *qid;
2422 parent_ids = got_object_commit_get_parent_ids(commit);
2423 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2424 err = got_object_id_str(&id_str, qid->id);
2427 printf("parent %d: %s\n", n++, id_str);
2432 err = got_object_commit_get_logmsg(&logmsg0, commit);
2438 line = strsep(&logmsg, "\n");
2440 printf(" %s\n", line);
2445 err = print_patch(commit, id, path, diff_context, repo);
2450 if (fflush(stdout) != 0 && err == NULL)
2451 err = got_error_from_errno("fflush");
2455 static const struct got_error *
2456 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2457 const char *path, int show_patch, const char *search_pattern,
2458 int diff_context, int limit, int log_branches,
2459 struct got_reflist_head *refs)
2461 const struct got_error *err;
2462 struct got_commit_graph *graph;
2466 if (search_pattern &&
2467 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2468 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2470 err = got_commit_graph_open(&graph, path, !log_branches);
2473 err = got_commit_graph_iter_start(graph, root_id, repo,
2474 check_cancelled, NULL);
2478 struct got_commit_object *commit;
2479 struct got_object_id *id;
2481 if (sigint_received || sigpipe_received)
2484 err = got_commit_graph_iter_next(&id, graph, repo,
2485 check_cancelled, NULL);
2487 if (err->code == GOT_ERR_ITER_COMPLETED)
2494 err = got_object_open_as_commit(&commit, repo, id);
2498 if (search_pattern) {
2499 err = match_logmsg(&have_match, id, commit, ®ex);
2501 got_object_commit_close(commit);
2504 if (have_match == 0) {
2505 got_object_commit_close(commit);
2510 err = print_commit(commit, id, repo, path, show_patch,
2511 diff_context, refs);
2512 got_object_commit_close(commit);
2513 if (err || (limit && --limit == 0))
2519 got_commit_graph_close(graph);
2526 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2527 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2532 get_default_log_limit(void)
2534 const char *got_default_log_limit;
2538 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2539 if (got_default_log_limit == NULL)
2541 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2547 static const struct got_error *
2548 cmd_log(int argc, char *argv[])
2550 const struct got_error *error;
2551 struct got_repository *repo = NULL;
2552 struct got_worktree *worktree = NULL;
2553 struct got_commit_object *commit = NULL;
2554 struct got_object_id *id = NULL;
2555 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2556 const char *start_commit = NULL, *search_pattern = NULL;
2557 int diff_context = -1, ch;
2558 int show_patch = 0, limit = 0, log_branches = 0;
2560 struct got_reflist_head refs;
2562 SIMPLEQ_INIT(&refs);
2565 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2571 limit = get_default_log_limit();
2573 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2579 start_commit = optarg;
2582 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2585 err(1, "-C option %s", errstr);
2588 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2590 err(1, "-l option %s", errstr);
2596 repo_path = realpath(optarg, NULL);
2597 if (repo_path == NULL)
2598 return got_error_from_errno2("realpath",
2600 got_path_strip_trailing_slashes(repo_path);
2603 search_pattern = optarg;
2614 if (diff_context == -1)
2616 else if (!show_patch)
2617 errx(1, "-C reguires -p");
2619 cwd = getcwd(NULL, 0);
2621 error = got_error_from_errno("getcwd");
2625 error = got_worktree_open(&worktree, cwd);
2626 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2633 error = got_error_from_errno("strdup");
2636 } else if (argc == 1) {
2638 error = got_worktree_resolve_path(&path, worktree,
2643 path = strdup(argv[0]);
2645 error = got_error_from_errno("strdup");
2652 if (repo_path == NULL) {
2653 repo_path = worktree ?
2654 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2656 if (repo_path == NULL) {
2657 error = got_error_from_errno("strdup");
2661 error = got_repo_open(&repo, repo_path, NULL);
2665 error = apply_unveil(got_repo_get_path(repo), 1,
2666 worktree ? got_worktree_get_root_path(worktree) : NULL);
2670 if (start_commit == NULL) {
2671 struct got_reference *head_ref;
2672 error = got_ref_open(&head_ref, repo,
2673 worktree ? got_worktree_get_head_ref_name(worktree)
2677 error = got_ref_resolve(&id, repo, head_ref);
2678 got_ref_close(head_ref);
2681 error = got_object_open_as_commit(&commit, repo, id);
2683 struct got_reference *ref;
2684 error = got_ref_open(&ref, repo, start_commit, 0);
2685 if (error == NULL) {
2687 error = got_ref_resolve(&id, repo, ref);
2691 error = got_object_get_type(&obj_type, repo, id);
2694 if (obj_type == GOT_OBJ_TYPE_TAG) {
2695 struct got_tag_object *tag;
2696 error = got_object_open_as_tag(&tag, repo, id);
2699 if (got_object_tag_get_object_type(tag) !=
2700 GOT_OBJ_TYPE_COMMIT) {
2701 got_object_tag_close(tag);
2702 error = got_error(GOT_ERR_OBJ_TYPE);
2706 id = got_object_id_dup(
2707 got_object_tag_get_object_id(tag));
2709 error = got_error_from_errno(
2710 "got_object_id_dup");
2711 got_object_tag_close(tag);
2714 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2715 error = got_error(GOT_ERR_OBJ_TYPE);
2718 error = got_object_open_as_commit(&commit, repo, id);
2722 if (commit == NULL) {
2723 error = got_repo_match_object_id_prefix(&id,
2724 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2733 const char *prefix = got_worktree_get_path_prefix(worktree);
2735 if (asprintf(&p, "%s%s%s", prefix,
2736 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2737 error = got_error_from_errno("asprintf");
2740 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2743 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2748 path = in_repo_path;
2751 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2755 error = print_commits(id, repo, path, show_patch, search_pattern,
2756 diff_context, limit, log_branches, &refs);
2763 got_worktree_close(worktree);
2765 const struct got_error *repo_error;
2766 repo_error = got_repo_close(repo);
2770 got_ref_list_free(&refs);
2777 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2778 "[-w] [object1 object2 | path]\n", getprogname());
2782 struct print_diff_arg {
2783 struct got_repository *repo;
2784 struct got_worktree *worktree;
2789 int ignore_whitespace;
2792 static const struct got_error *
2793 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2794 const char *path, struct got_object_id *blob_id,
2795 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2796 int dirfd, const char *de_name)
2798 struct print_diff_arg *a = arg;
2799 const struct got_error *err = NULL;
2800 struct got_blob_object *blob1 = NULL;
2803 char *abspath = NULL, *label1 = NULL;
2806 if (a->diff_staged) {
2807 if (staged_status != GOT_STATUS_MODIFY &&
2808 staged_status != GOT_STATUS_ADD &&
2809 staged_status != GOT_STATUS_DELETE)
2812 if (staged_status == GOT_STATUS_DELETE)
2814 if (status == GOT_STATUS_NONEXISTENT)
2815 return got_error_set_errno(ENOENT, path);
2816 if (status != GOT_STATUS_MODIFY &&
2817 status != GOT_STATUS_ADD &&
2818 status != GOT_STATUS_DELETE &&
2819 status != GOT_STATUS_CONFLICT)
2823 if (!a->header_shown) {
2824 printf("diff %s %s%s\n", a->id_str,
2825 got_worktree_get_root_path(a->worktree),
2826 a->diff_staged ? " (staged changes)" : "");
2827 a->header_shown = 1;
2830 if (a->diff_staged) {
2831 const char *label1 = NULL, *label2 = NULL;
2832 switch (staged_status) {
2833 case GOT_STATUS_MODIFY:
2837 case GOT_STATUS_ADD:
2840 case GOT_STATUS_DELETE:
2844 return got_error(GOT_ERR_FILE_STATUS);
2846 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2847 label1, label2, a->diff_context, a->ignore_whitespace,
2851 if (staged_status == GOT_STATUS_ADD ||
2852 staged_status == GOT_STATUS_MODIFY) {
2854 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2858 err = got_object_id_str(&id_str, staged_blob_id);
2861 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2862 err = got_error_from_errno("asprintf");
2867 } else if (status != GOT_STATUS_ADD) {
2868 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2873 if (status != GOT_STATUS_DELETE) {
2874 if (asprintf(&abspath, "%s/%s",
2875 got_worktree_get_root_path(a->worktree), path) == -1) {
2876 err = got_error_from_errno("asprintf");
2881 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2883 err = got_error_from_errno2("openat", abspath);
2887 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2889 err = got_error_from_errno2("open", abspath);
2893 if (fstat(fd, &sb) == -1) {
2894 err = got_error_from_errno2("fstat", abspath);
2897 f2 = fdopen(fd, "r");
2899 err = got_error_from_errno2("fdopen", abspath);
2906 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2907 a->diff_context, a->ignore_whitespace, stdout);
2910 got_object_blob_close(blob1);
2911 if (f2 && fclose(f2) == EOF && err == NULL)
2912 err = got_error_from_errno("fclose");
2913 if (fd != -1 && close(fd) == -1 && err == NULL)
2914 err = got_error_from_errno("close");
2919 static const struct got_error *
2920 cmd_diff(int argc, char *argv[])
2922 const struct got_error *error;
2923 struct got_repository *repo = NULL;
2924 struct got_worktree *worktree = NULL;
2925 char *cwd = NULL, *repo_path = NULL;
2926 struct got_object_id *id1 = NULL, *id2 = NULL;
2927 const char *id_str1 = NULL, *id_str2 = NULL;
2928 char *label1 = NULL, *label2 = NULL;
2930 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2935 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2940 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2943 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2946 err(1, "-C option %s", errstr);
2949 repo_path = realpath(optarg, NULL);
2950 if (repo_path == NULL)
2951 return got_error_from_errno2("realpath",
2953 got_path_strip_trailing_slashes(repo_path);
2959 ignore_whitespace = 1;
2970 cwd = getcwd(NULL, 0);
2972 error = got_error_from_errno("getcwd");
2975 error = got_worktree_open(&worktree, cwd);
2976 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2979 if (worktree == NULL) {
2980 error = got_error(GOT_ERR_NOT_WORKTREE);
2985 "-r option can't be used when diffing a work tree");
2986 repo_path = strdup(got_worktree_get_repo_path(worktree));
2987 if (repo_path == NULL) {
2988 error = got_error_from_errno("strdup");
2992 error = got_worktree_resolve_path(&path, worktree,
2999 error = got_error_from_errno("strdup");
3003 } else if (argc == 2) {
3005 errx(1, "-s option can't be used when diffing "
3006 "objects in repository");
3009 if (worktree && repo_path == NULL) {
3011 strdup(got_worktree_get_repo_path(worktree));
3012 if (repo_path == NULL) {
3013 error = got_error_from_errno("strdup");
3020 if (repo_path == NULL) {
3021 repo_path = getcwd(NULL, 0);
3022 if (repo_path == NULL)
3023 return got_error_from_errno("getcwd");
3026 error = got_repo_open(&repo, repo_path, NULL);
3031 error = apply_unveil(got_repo_get_path(repo), 1,
3032 worktree ? got_worktree_get_root_path(worktree) : NULL);
3037 struct print_diff_arg arg;
3038 struct got_pathlist_head paths;
3043 error = got_object_id_str(&id_str,
3044 got_worktree_get_base_commit_id(worktree));
3048 arg.worktree = worktree;
3049 arg.diff_context = diff_context;
3050 arg.id_str = id_str;
3051 arg.header_shown = 0;
3052 arg.diff_staged = diff_staged;
3053 arg.ignore_whitespace = ignore_whitespace;
3055 error = got_pathlist_append(&paths, path, NULL);
3059 error = got_worktree_status(worktree, &paths, repo, print_diff,
3060 &arg, check_cancelled, NULL);
3062 got_pathlist_free(&paths);
3066 error = got_repo_match_object_id(&id1, &label1, id_str1,
3067 GOT_OBJ_TYPE_ANY, 1, repo);
3071 error = got_repo_match_object_id(&id2, &label2, id_str2,
3072 GOT_OBJ_TYPE_ANY, 1, repo);
3076 error = got_object_get_type(&type1, repo, id1);
3080 error = got_object_get_type(&type2, repo, id2);
3084 if (type1 != type2) {
3085 error = got_error(GOT_ERR_OBJ_TYPE);
3090 case GOT_OBJ_TYPE_BLOB:
3091 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3092 diff_context, ignore_whitespace, repo, stdout);
3094 case GOT_OBJ_TYPE_TREE:
3095 error = got_diff_objects_as_trees(id1, id2, "", "",
3096 diff_context, ignore_whitespace, repo, stdout);
3098 case GOT_OBJ_TYPE_COMMIT:
3099 printf("diff %s %s\n", label1, label2);
3100 error = got_diff_objects_as_commits(id1, id2, diff_context,
3101 ignore_whitespace, repo, stdout);
3104 error = got_error(GOT_ERR_OBJ_TYPE);
3113 got_worktree_close(worktree);
3115 const struct got_error *repo_error;
3116 repo_error = got_repo_close(repo);
3127 "usage: %s blame [-c commit] [-r repository-path] path\n",
3136 char datebuf[11]; /* YYYY-MM-DD + NUL */
3139 struct blame_cb_args {
3140 struct blame_line *lines;
3144 off_t *line_offsets;
3146 struct got_repository *repo;
3149 static const struct got_error *
3150 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3152 const struct got_error *err = NULL;
3153 struct blame_cb_args *a = arg;
3154 struct blame_line *bline;
3156 size_t linesize = 0;
3157 struct got_commit_object *commit = NULL;
3160 time_t committer_time;
3162 if (nlines != a->nlines ||
3163 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3164 return got_error(GOT_ERR_RANGE);
3166 if (sigint_received)
3167 return got_error(GOT_ERR_ITER_COMPLETED);
3170 return NULL; /* no change in this commit */
3172 /* Annotate this line. */
3173 bline = &a->lines[lineno - 1];
3174 if (bline->annotated)
3176 err = got_object_id_str(&bline->id_str, id);
3180 err = got_object_open_as_commit(&commit, a->repo, id);
3184 bline->committer = strdup(got_object_commit_get_committer(commit));
3185 if (bline->committer == NULL) {
3186 err = got_error_from_errno("strdup");
3190 committer_time = got_object_commit_get_committer_time(commit);
3191 if (localtime_r(&committer_time, &tm) == NULL)
3192 return got_error_from_errno("localtime_r");
3193 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3194 &tm) >= sizeof(bline->datebuf)) {
3195 err = got_error(GOT_ERR_NO_SPACE);
3198 bline->annotated = 1;
3200 /* Print lines annotated so far. */
3201 bline = &a->lines[a->lineno_cur - 1];
3202 if (!bline->annotated)
3205 offset = a->line_offsets[a->lineno_cur - 1];
3206 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3207 err = got_error_from_errno("fseeko");
3211 while (bline->annotated) {
3212 char *smallerthan, *at, *nl, *committer;
3215 if (getline(&line, &linesize, a->f) == -1) {
3217 err = got_error_from_errno("getline");
3221 committer = bline->committer;
3222 smallerthan = strchr(committer, '<');
3223 if (smallerthan && smallerthan[1] != '\0')
3224 committer = smallerthan + 1;
3225 at = strchr(committer, '@');
3228 len = strlen(committer);
3230 committer[8] = '\0';
3232 nl = strchr(line, '\n');
3235 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3236 bline->id_str, bline->datebuf, committer, line);
3239 bline = &a->lines[a->lineno_cur - 1];
3243 got_object_commit_close(commit);
3248 static const struct got_error *
3249 cmd_blame(int argc, char *argv[])
3251 const struct got_error *error;
3252 struct got_repository *repo = NULL;
3253 struct got_worktree *worktree = NULL;
3254 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3255 struct got_object_id *obj_id = NULL;
3256 struct got_object_id *commit_id = NULL;
3257 struct got_blob_object *blob = NULL;
3258 char *commit_id_str = NULL;
3259 struct blame_cb_args bca;
3260 int ch, obj_type, i;
3263 memset(&bca, 0, sizeof(bca));
3266 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3271 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3274 commit_id_str = optarg;
3277 repo_path = realpath(optarg, NULL);
3278 if (repo_path == NULL)
3279 return got_error_from_errno2("realpath",
3281 got_path_strip_trailing_slashes(repo_path);
3297 cwd = getcwd(NULL, 0);
3299 error = got_error_from_errno("getcwd");
3302 if (repo_path == NULL) {
3303 error = got_worktree_open(&worktree, cwd);
3304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3310 strdup(got_worktree_get_repo_path(worktree));
3311 if (repo_path == NULL)
3312 error = got_error_from_errno("strdup");
3316 repo_path = strdup(cwd);
3317 if (repo_path == NULL) {
3318 error = got_error_from_errno("strdup");
3324 error = got_repo_open(&repo, repo_path, NULL);
3328 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3333 const char *prefix = got_worktree_get_path_prefix(worktree);
3334 char *p, *worktree_subdir = cwd +
3335 strlen(got_worktree_get_root_path(worktree));
3336 if (asprintf(&p, "%s%s%s%s%s",
3337 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3338 worktree_subdir, worktree_subdir[0] ? "/" : "",
3340 error = got_error_from_errno("asprintf");
3343 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3346 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3351 if (commit_id_str == NULL) {
3352 struct got_reference *head_ref;
3353 error = got_ref_open(&head_ref, repo, worktree ?
3354 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3357 error = got_ref_resolve(&commit_id, repo, head_ref);
3358 got_ref_close(head_ref);
3362 error = got_repo_match_object_id(&commit_id, NULL,
3363 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3368 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3372 error = got_object_get_type(&obj_type, repo, obj_id);
3376 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3377 error = got_error(GOT_ERR_OBJ_TYPE);
3381 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3384 bca.f = got_opentemp();
3385 if (bca.f == NULL) {
3386 error = got_error_from_errno("got_opentemp");
3389 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3390 &bca.line_offsets, bca.f, blob);
3391 if (error || bca.nlines == 0)
3394 /* Don't include \n at EOF in the blame line count. */
3395 if (bca.line_offsets[bca.nlines - 1] == filesize)
3398 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3399 if (bca.lines == NULL) {
3400 error = got_error_from_errno("calloc");
3404 bca.nlines_prec = 0;
3412 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3413 check_cancelled, NULL);
3421 got_object_blob_close(blob);
3423 got_worktree_close(worktree);
3425 const struct got_error *repo_error;
3426 repo_error = got_repo_close(repo);
3431 for (i = 0; i < bca.nlines; i++) {
3432 struct blame_line *bline = &bca.lines[i];
3433 free(bline->id_str);
3434 free(bline->committer);
3438 free(bca.line_offsets);
3439 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3440 error = got_error_from_errno("fclose");
3448 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3454 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3455 const char *root_path)
3457 int is_root_path = (strcmp(path, root_path) == 0);
3458 const char *modestr = "";
3459 mode_t mode = got_tree_entry_get_mode(te);
3461 path += strlen(root_path);
3462 while (path[0] == '/')
3465 if (got_object_tree_entry_is_submodule(te))
3467 else if (S_ISLNK(mode))
3469 else if (S_ISDIR(mode))
3471 else if (mode & S_IXUSR)
3474 printf("%s%s%s%s%s\n", id ? id : "", path,
3475 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3478 static const struct got_error *
3479 print_tree(const char *path, struct got_object_id *commit_id,
3480 int show_ids, int recurse, const char *root_path,
3481 struct got_repository *repo)
3483 const struct got_error *err = NULL;
3484 struct got_object_id *tree_id = NULL;
3485 struct got_tree_object *tree = NULL;
3488 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3492 err = got_object_open_as_tree(&tree, repo, tree_id);
3495 nentries = got_object_tree_get_nentries(tree);
3496 for (i = 0; i < nentries; i++) {
3497 struct got_tree_entry *te;
3500 if (sigint_received || sigpipe_received)
3503 te = got_object_tree_get_entry(tree, i);
3506 err = got_object_id_str(&id_str,
3507 got_tree_entry_get_id(te));
3510 if (asprintf(&id, "%s ", id_str) == -1) {
3511 err = got_error_from_errno("asprintf");
3517 print_entry(te, id, path, root_path);
3520 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3522 if (asprintf(&child_path, "%s%s%s", path,
3523 path[0] == '/' && path[1] == '\0' ? "" : "/",
3524 got_tree_entry_get_name(te)) == -1) {
3525 err = got_error_from_errno("asprintf");
3528 err = print_tree(child_path, commit_id, show_ids, 1,
3537 got_object_tree_close(tree);
3542 static const struct got_error *
3543 cmd_tree(int argc, char *argv[])
3545 const struct got_error *error;
3546 struct got_repository *repo = NULL;
3547 struct got_worktree *worktree = NULL;
3549 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3550 struct got_object_id *commit_id = NULL;
3551 char *commit_id_str = NULL;
3552 int show_ids = 0, recurse = 0;
3556 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3561 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3564 commit_id_str = optarg;
3567 repo_path = realpath(optarg, NULL);
3568 if (repo_path == NULL)
3569 return got_error_from_errno2("realpath",
3571 got_path_strip_trailing_slashes(repo_path);
3595 cwd = getcwd(NULL, 0);
3597 error = got_error_from_errno("getcwd");
3600 if (repo_path == NULL) {
3601 error = got_worktree_open(&worktree, cwd);
3602 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3608 strdup(got_worktree_get_repo_path(worktree));
3609 if (repo_path == NULL)
3610 error = got_error_from_errno("strdup");
3614 repo_path = strdup(cwd);
3615 if (repo_path == NULL) {
3616 error = got_error_from_errno("strdup");
3622 error = got_repo_open(&repo, repo_path, NULL);
3626 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3632 char *p, *worktree_subdir = cwd +
3633 strlen(got_worktree_get_root_path(worktree));
3634 if (asprintf(&p, "%s/%s",
3635 got_worktree_get_path_prefix(worktree),
3636 worktree_subdir) == -1) {
3637 error = got_error_from_errno("asprintf");
3640 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3647 if (in_repo_path == NULL) {
3648 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3653 if (commit_id_str == NULL) {
3654 struct got_reference *head_ref;
3655 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3658 error = got_ref_resolve(&commit_id, repo, head_ref);
3659 got_ref_close(head_ref);
3663 error = got_repo_match_object_id(&commit_id, NULL,
3664 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3669 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3670 in_repo_path, repo);
3677 got_worktree_close(worktree);
3679 const struct got_error *repo_error;
3680 repo_error = got_repo_close(repo);
3690 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3694 static const struct got_error *
3695 print_status(void *arg, unsigned char status, unsigned char staged_status,
3696 const char *path, struct got_object_id *blob_id,
3697 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3698 int dirfd, const char *de_name)
3700 if (status == staged_status && (status == GOT_STATUS_DELETE))
3701 status = GOT_STATUS_NO_CHANGE;
3702 printf("%c%c %s\n", status, staged_status, path);
3706 static const struct got_error *
3707 cmd_status(int argc, char *argv[])
3709 const struct got_error *error = NULL;
3710 struct got_repository *repo = NULL;
3711 struct got_worktree *worktree = NULL;
3713 struct got_pathlist_head paths;
3714 struct got_pathlist_entry *pe;
3719 while ((ch = getopt(argc, argv, "")) != -1) {
3731 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3735 cwd = getcwd(NULL, 0);
3737 error = got_error_from_errno("getcwd");
3741 error = got_worktree_open(&worktree, cwd);
3745 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3750 error = apply_unveil(got_repo_get_path(repo), 1,
3751 got_worktree_get_root_path(worktree));
3755 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3759 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3760 check_cancelled, NULL);
3762 TAILQ_FOREACH(pe, &paths, entry)
3763 free((char *)pe->path);
3764 got_pathlist_free(&paths);
3773 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3778 static const struct got_error *
3779 list_refs(struct got_repository *repo)
3781 static const struct got_error *err = NULL;
3782 struct got_reflist_head refs;
3783 struct got_reflist_entry *re;
3785 SIMPLEQ_INIT(&refs);
3786 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3790 SIMPLEQ_FOREACH(re, &refs, entry) {
3792 refstr = got_ref_to_str(re->ref);
3794 return got_error_from_errno("got_ref_to_str");
3795 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3799 got_ref_list_free(&refs);
3803 static const struct got_error *
3804 delete_ref(struct got_repository *repo, const char *refname)
3806 const struct got_error *err = NULL;
3807 struct got_reference *ref;
3809 err = got_ref_open(&ref, repo, refname, 0);
3813 err = got_ref_delete(ref, repo);
3818 static const struct got_error *
3819 add_ref(struct got_repository *repo, const char *refname, const char *target)
3821 const struct got_error *err = NULL;
3822 struct got_object_id *id;
3823 struct got_reference *ref = NULL;
3826 * Don't let the user create a reference name with a leading '-'.
3827 * While technically a valid reference name, this case is usually
3828 * an unintended typo.
3830 if (refname[0] == '-')
3831 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3833 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3836 struct got_reference *target_ref;
3838 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3840 err = got_ref_open(&target_ref, repo, target, 0);
3843 err = got_ref_resolve(&id, repo, target_ref);
3844 got_ref_close(target_ref);
3849 err = got_ref_alloc(&ref, refname, id);
3853 err = got_ref_write(ref, repo);
3861 static const struct got_error *
3862 add_symref(struct got_repository *repo, const char *refname, const char *target)
3864 const struct got_error *err = NULL;
3865 struct got_reference *ref = NULL;
3866 struct got_reference *target_ref = NULL;
3869 * Don't let the user create a reference name with a leading '-'.
3870 * While technically a valid reference name, this case is usually
3871 * an unintended typo.
3873 if (refname[0] == '-')
3874 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3876 err = got_ref_open(&target_ref, repo, target, 0);
3880 err = got_ref_alloc_symref(&ref, refname, target_ref);
3884 err = got_ref_write(ref, repo);
3887 got_ref_close(target_ref);
3893 static const struct got_error *
3894 cmd_ref(int argc, char *argv[])
3896 const struct got_error *error = NULL;
3897 struct got_repository *repo = NULL;
3898 struct got_worktree *worktree = NULL;
3899 char *cwd = NULL, *repo_path = NULL;
3900 int ch, do_list = 0, create_symref = 0;
3901 const char *delref = NULL;
3903 /* TODO: Add -s option for adding symbolic references. */
3904 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3910 repo_path = realpath(optarg, NULL);
3911 if (repo_path == NULL)
3912 return got_error_from_errno2("realpath",
3914 got_path_strip_trailing_slashes(repo_path);
3928 if (do_list && delref)
3929 errx(1, "-l and -d options are mutually exclusive\n");
3934 if (do_list || delref) {
3936 errx(1, "-s option cannot be used together with the "
3937 "-l or -d options");
3940 } else if (argc != 2)
3945 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3949 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3950 "sendfd unveil", NULL) == -1)
3954 cwd = getcwd(NULL, 0);
3956 error = got_error_from_errno("getcwd");
3960 if (repo_path == NULL) {
3961 error = got_worktree_open(&worktree, cwd);
3962 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3968 strdup(got_worktree_get_repo_path(worktree));
3969 if (repo_path == NULL)
3970 error = got_error_from_errno("strdup");
3974 repo_path = strdup(cwd);
3975 if (repo_path == NULL) {
3976 error = got_error_from_errno("strdup");
3982 error = got_repo_open(&repo, repo_path, NULL);
3986 error = apply_unveil(got_repo_get_path(repo), do_list,
3987 worktree ? got_worktree_get_root_path(worktree) : NULL);
3992 error = list_refs(repo);
3994 error = delete_ref(repo, delref);
3995 else if (create_symref)
3996 error = add_symref(repo, argv[0], argv[1]);
3998 error = add_ref(repo, argv[0], argv[1]);
4001 got_repo_close(repo);
4003 got_worktree_close(worktree);
4013 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4014 "[name]\n", getprogname());
4018 static const struct got_error *
4019 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4020 struct got_reference *ref)
4022 const struct got_error *err = NULL;
4023 const char *refname, *marker = " ";
4026 refname = got_ref_get_name(ref);
4027 if (worktree && strcmp(refname,
4028 got_worktree_get_head_ref_name(worktree)) == 0) {
4029 struct got_object_id *id = NULL;
4031 err = got_ref_resolve(&id, repo, ref);
4034 if (got_object_id_cmp(id,
4035 got_worktree_get_base_commit_id(worktree)) == 0)
4042 if (strncmp(refname, "refs/heads/", 11) == 0)
4044 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4047 refstr = got_ref_to_str(ref);
4049 return got_error_from_errno("got_ref_to_str");
4051 printf("%s%s: %s\n", marker, refname, refstr);
4056 static const struct got_error *
4057 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4059 const char *refname;
4061 if (worktree == NULL)
4062 return got_error(GOT_ERR_NOT_WORKTREE);
4064 refname = got_worktree_get_head_ref_name(worktree);
4066 if (strncmp(refname, "refs/heads/", 11) == 0)
4068 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4071 printf("%s\n", refname);
4076 static const struct got_error *
4077 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4079 static const struct got_error *err = NULL;
4080 struct got_reflist_head refs;
4081 struct got_reflist_entry *re;
4082 struct got_reference *temp_ref = NULL;
4083 int rebase_in_progress, histedit_in_progress;
4085 SIMPLEQ_INIT(&refs);
4088 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4093 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4098 if (rebase_in_progress || histedit_in_progress) {
4099 err = got_ref_open(&temp_ref, repo,
4100 got_worktree_get_head_ref_name(worktree), 0);
4103 list_branch(repo, worktree, temp_ref);
4104 got_ref_close(temp_ref);
4108 err = got_ref_list(&refs, repo, "refs/heads",
4109 got_ref_cmp_by_name, NULL);
4113 SIMPLEQ_FOREACH(re, &refs, entry)
4114 list_branch(repo, worktree, re->ref);
4116 got_ref_list_free(&refs);
4120 static const struct got_error *
4121 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4122 const char *branch_name)
4124 const struct got_error *err = NULL;
4125 struct got_reference *ref = NULL;
4128 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4129 return got_error_from_errno("asprintf");
4131 err = got_ref_open(&ref, repo, refname, 0);
4136 strcmp(got_worktree_get_head_ref_name(worktree),
4137 got_ref_get_name(ref)) == 0) {
4138 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4139 "will not delete this work tree's current branch");
4143 err = got_ref_delete(ref, repo);
4151 static const struct got_error *
4152 add_branch(struct got_repository *repo, const char *branch_name,
4153 struct got_object_id *base_commit_id)
4155 const struct got_error *err = NULL;
4156 struct got_reference *ref = NULL;
4157 char *base_refname = NULL, *refname = NULL;
4160 * Don't let the user create a branch name with a leading '-'.
4161 * While technically a valid reference name, this case is usually
4162 * an unintended typo.
4164 if (branch_name[0] == '-')
4165 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4167 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4168 err = got_error_from_errno("asprintf");
4172 err = got_ref_open(&ref, repo, refname, 0);
4174 err = got_error(GOT_ERR_BRANCH_EXISTS);
4176 } else if (err->code != GOT_ERR_NOT_REF)
4179 err = got_ref_alloc(&ref, refname, base_commit_id);
4183 err = got_ref_write(ref, repo);
4192 static const struct got_error *
4193 cmd_branch(int argc, char *argv[])
4195 const struct got_error *error = NULL;
4196 struct got_repository *repo = NULL;
4197 struct got_worktree *worktree = NULL;
4198 char *cwd = NULL, *repo_path = NULL;
4199 int ch, do_list = 0, do_show = 0, do_update = 1;
4200 const char *delref = NULL, *commit_id_arg = NULL;
4201 struct got_reference *ref = NULL;
4202 struct got_pathlist_head paths;
4203 struct got_pathlist_entry *pe;
4204 struct got_object_id *commit_id = NULL;
4205 char *commit_id_str = NULL;
4209 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4212 commit_id_arg = optarg;
4218 repo_path = realpath(optarg, NULL);
4219 if (repo_path == NULL)
4220 return got_error_from_errno2("realpath",
4222 got_path_strip_trailing_slashes(repo_path);
4236 if (do_list && delref)
4237 errx(1, "-l and -d options are mutually exclusive\n");
4242 if (!do_list && !delref && argc == 0)
4245 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4246 errx(1, "-c option can only be used when creating a branch");
4248 if (do_list || delref) {
4251 } else if (!do_show && argc != 1)
4255 if (do_list || do_show) {
4256 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4260 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 "sendfd unveil", NULL) == -1)
4265 cwd = getcwd(NULL, 0);
4267 error = got_error_from_errno("getcwd");
4271 if (repo_path == NULL) {
4272 error = got_worktree_open(&worktree, cwd);
4273 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4279 strdup(got_worktree_get_repo_path(worktree));
4280 if (repo_path == NULL)
4281 error = got_error_from_errno("strdup");
4285 repo_path = strdup(cwd);
4286 if (repo_path == NULL) {
4287 error = got_error_from_errno("strdup");
4293 error = got_repo_open(&repo, repo_path, NULL);
4297 error = apply_unveil(got_repo_get_path(repo), do_list,
4298 worktree ? got_worktree_get_root_path(worktree) : NULL);
4303 error = show_current_branch(repo, worktree);
4305 error = list_branches(repo, worktree);
4307 error = delete_branch(repo, worktree, delref);
4309 if (commit_id_arg == NULL)
4310 commit_id_arg = worktree ?
4311 got_worktree_get_head_ref_name(worktree) :
4313 error = got_repo_match_object_id(&commit_id, NULL,
4314 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4317 error = add_branch(repo, argv[0], commit_id);
4320 if (worktree && do_update) {
4321 int did_something = 0;
4322 char *branch_refname = NULL;
4324 error = got_object_id_str(&commit_id_str, commit_id);
4327 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4331 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4333 error = got_error_from_errno("asprintf");
4336 error = got_ref_open(&ref, repo, branch_refname, 0);
4337 free(branch_refname);
4340 error = switch_head_ref(ref, commit_id, worktree,
4344 error = got_worktree_set_base_commit_id(worktree, repo,
4348 error = got_worktree_checkout_files(worktree, &paths,
4349 repo, update_progress, &did_something,
4350 check_cancelled, NULL);
4354 printf("Updated to commit %s\n", commit_id_str);
4361 got_repo_close(repo);
4363 got_worktree_close(worktree);
4367 free(commit_id_str);
4368 TAILQ_FOREACH(pe, &paths, entry)
4369 free((char *)pe->path);
4370 got_pathlist_free(&paths);
4379 "usage: %s tag [-c commit] [-r repository] [-l] "
4380 "[-m message] name\n", getprogname());
4385 static const struct got_error *
4386 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4388 const struct got_error *err = NULL;
4389 struct got_reflist_entry *re, *se, *new;
4390 struct got_object_id *re_id, *se_id;
4391 struct got_tag_object *re_tag, *se_tag;
4392 time_t re_time, se_time;
4394 SIMPLEQ_FOREACH(re, tags, entry) {
4395 se = SIMPLEQ_FIRST(sorted);
4397 err = got_reflist_entry_dup(&new, re);
4400 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4403 err = got_ref_resolve(&re_id, repo, re->ref);
4406 err = got_object_open_as_tag(&re_tag, repo, re_id);
4410 re_time = got_object_tag_get_tagger_time(re_tag);
4411 got_object_tag_close(re_tag);
4415 err = got_ref_resolve(&se_id, repo, re->ref);
4418 err = got_object_open_as_tag(&se_tag, repo, se_id);
4422 se_time = got_object_tag_get_tagger_time(se_tag);
4423 got_object_tag_close(se_tag);
4425 if (se_time > re_time) {
4426 err = got_reflist_entry_dup(&new, re);
4429 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4432 se = SIMPLEQ_NEXT(se, entry);
4441 static const struct got_error *
4442 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4444 static const struct got_error *err = NULL;
4445 struct got_reflist_head refs;
4446 struct got_reflist_entry *re;
4448 SIMPLEQ_INIT(&refs);
4450 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4454 SIMPLEQ_FOREACH(re, &refs, entry) {
4455 const char *refname;
4456 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4460 struct got_object_id *id;
4461 struct got_tag_object *tag;
4462 struct got_commit_object *commit = NULL;
4464 refname = got_ref_get_name(re->ref);
4465 if (strncmp(refname, "refs/tags/", 10) != 0)
4468 refstr = got_ref_to_str(re->ref);
4469 if (refstr == NULL) {
4470 err = got_error_from_errno("got_ref_to_str");
4473 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4476 err = got_ref_resolve(&id, repo, re->ref);
4479 err = got_object_open_as_tag(&tag, repo, id);
4481 if (err->code != GOT_ERR_OBJ_TYPE) {
4485 /* "lightweight" tag */
4486 err = got_object_open_as_commit(&commit, repo, id);
4491 tagger = got_object_commit_get_committer(commit);
4493 got_object_commit_get_committer_time(commit);
4494 err = got_object_id_str(&id_str, id);
4500 tagger = got_object_tag_get_tagger(tag);
4501 tagger_time = got_object_tag_get_tagger_time(tag);
4502 err = got_object_id_str(&id_str,
4503 got_object_tag_get_object_id(tag));
4507 printf("from: %s\n", tagger);
4508 datestr = get_datestr(&tagger_time, datebuf);
4510 printf("date: %s UTC\n", datestr);
4512 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4514 switch (got_object_tag_get_object_type(tag)) {
4515 case GOT_OBJ_TYPE_BLOB:
4516 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4519 case GOT_OBJ_TYPE_TREE:
4520 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4523 case GOT_OBJ_TYPE_COMMIT:
4524 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4527 case GOT_OBJ_TYPE_TAG:
4528 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4537 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4540 got_object_commit_close(commit);
4542 tagmsg0 = strdup(got_object_tag_get_message(tag));
4543 got_object_tag_close(tag);
4544 if (tagmsg0 == NULL) {
4545 err = got_error_from_errno("strdup");
4552 line = strsep(&tagmsg, "\n");
4554 printf(" %s\n", line);
4559 got_ref_list_free(&refs);
4563 static const struct got_error *
4564 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4565 const char *tag_name, const char *repo_path)
4567 const struct got_error *err = NULL;
4568 char *template = NULL, *initial_content = NULL;
4569 char *editor = NULL;
4572 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4573 err = got_error_from_errno("asprintf");
4577 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4578 commit_id_str, tag_name) == -1) {
4579 err = got_error_from_errno("asprintf");
4583 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4587 dprintf(fd, initial_content);
4590 err = get_editor(&editor);
4593 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4595 free(initial_content);
4599 /* Editor is done; we can now apply unveil(2) */
4601 err = apply_unveil(repo_path, 0, NULL);
4610 static const struct got_error *
4611 add_tag(struct got_repository *repo, const char *tag_name,
4612 const char *commit_arg, const char *tagmsg_arg)
4614 const struct got_error *err = NULL;
4615 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4616 char *label = NULL, *commit_id_str = NULL;
4617 struct got_reference *ref = NULL;
4618 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4619 char *tagmsg_path = NULL, *tag_id_str = NULL;
4620 int preserve_tagmsg = 0;
4623 * Don't let the user create a tag name with a leading '-'.
4624 * While technically a valid reference name, this case is usually
4625 * an unintended typo.
4627 if (tag_name[0] == '-')
4628 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4630 err = get_author(&tagger, repo);
4634 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4635 GOT_OBJ_TYPE_COMMIT, 1, repo);
4639 err = got_object_id_str(&commit_id_str, commit_id);
4643 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4644 refname = strdup(tag_name);
4645 if (refname == NULL) {
4646 err = got_error_from_errno("strdup");
4650 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4651 err = got_error_from_errno("asprintf");
4655 err = got_ref_open(&ref, repo, refname, 0);
4657 err = got_error(GOT_ERR_TAG_EXISTS);
4659 } else if (err->code != GOT_ERR_NOT_REF)
4662 if (tagmsg_arg == NULL) {
4663 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4664 tag_name, got_repo_get_path(repo));
4666 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4667 tagmsg_path != NULL)
4668 preserve_tagmsg = 1;
4673 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4674 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4677 preserve_tagmsg = 1;
4681 err = got_ref_alloc(&ref, refname, tag_id);
4684 preserve_tagmsg = 1;
4688 err = got_ref_write(ref, repo);
4691 preserve_tagmsg = 1;
4695 err = got_object_id_str(&tag_id_str, tag_id);
4698 preserve_tagmsg = 1;
4701 printf("Created tag %s\n", tag_id_str);
4703 if (preserve_tagmsg) {
4704 fprintf(stderr, "%s: tag message preserved in %s\n",
4705 getprogname(), tagmsg_path);
4706 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4707 err = got_error_from_errno2("unlink", tagmsg_path);
4712 free(commit_id_str);
4720 static const struct got_error *
4721 cmd_tag(int argc, char *argv[])
4723 const struct got_error *error = NULL;
4724 struct got_repository *repo = NULL;
4725 struct got_worktree *worktree = NULL;
4726 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4727 char *gitconfig_path = NULL;
4728 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4729 int ch, do_list = 0;
4731 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4734 commit_id_arg = optarg;
4740 repo_path = realpath(optarg, NULL);
4741 if (repo_path == NULL)
4742 return got_error_from_errno2("realpath",
4744 got_path_strip_trailing_slashes(repo_path);
4759 if (commit_id_arg != NULL)
4760 errx(1, "-c option can only be used when creating a tag");
4762 errx(1, "-l and -m options are mutually exclusive");
4765 } else if (argc != 1)
4772 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4776 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4777 "sendfd unveil", NULL) == -1)
4781 cwd = getcwd(NULL, 0);
4783 error = got_error_from_errno("getcwd");
4787 if (repo_path == NULL) {
4788 error = got_worktree_open(&worktree, cwd);
4789 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4795 strdup(got_worktree_get_repo_path(worktree));
4796 if (repo_path == NULL)
4797 error = got_error_from_errno("strdup");
4801 repo_path = strdup(cwd);
4802 if (repo_path == NULL) {
4803 error = got_error_from_errno("strdup");
4810 error = got_repo_open(&repo, repo_path, NULL);
4813 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4816 error = list_tags(repo, worktree);
4818 error = get_gitconfig_path(&gitconfig_path);
4821 error = got_repo_open(&repo, repo_path, gitconfig_path);
4826 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4831 if (commit_id_arg == NULL) {
4832 struct got_reference *head_ref;
4833 struct got_object_id *commit_id;
4834 error = got_ref_open(&head_ref, repo,
4835 worktree ? got_worktree_get_head_ref_name(worktree)
4839 error = got_ref_resolve(&commit_id, repo, head_ref);
4840 got_ref_close(head_ref);
4843 error = got_object_id_str(&commit_id_str, commit_id);
4849 error = add_tag(repo, tag_name,
4850 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4854 got_repo_close(repo);
4856 got_worktree_close(worktree);
4859 free(gitconfig_path);
4860 free(commit_id_str);
4867 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4872 static const struct got_error *
4873 add_progress(void *arg, unsigned char status, const char *path)
4875 while (path[0] == '/')
4877 printf("%c %s\n", status, path);
4881 static const struct got_error *
4882 cmd_add(int argc, char *argv[])
4884 const struct got_error *error = NULL;
4885 struct got_repository *repo = NULL;
4886 struct got_worktree *worktree = NULL;
4888 struct got_pathlist_head paths;
4889 struct got_pathlist_entry *pe;
4890 int ch, can_recurse = 0, no_ignores = 0;
4894 while ((ch = getopt(argc, argv, "IR")) != -1) {
4912 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4919 cwd = getcwd(NULL, 0);
4921 error = got_error_from_errno("getcwd");
4925 error = got_worktree_open(&worktree, cwd);
4929 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4934 error = apply_unveil(got_repo_get_path(repo), 1,
4935 got_worktree_get_root_path(worktree));
4939 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4943 if (!can_recurse && no_ignores) {
4944 error = got_error_msg(GOT_ERR_BAD_PATH,
4945 "disregarding ignores requires -R option");
4953 TAILQ_FOREACH(pe, &paths, entry) {
4954 if (asprintf(&ondisk_path, "%s/%s",
4955 got_worktree_get_root_path(worktree),
4957 error = got_error_from_errno("asprintf");
4960 if (lstat(ondisk_path, &sb) == -1) {
4961 if (errno == ENOENT) {
4965 error = got_error_from_errno2("lstat",
4971 if (S_ISDIR(sb.st_mode)) {
4972 error = got_error_msg(GOT_ERR_BAD_PATH,
4973 "adding directories requires -R option");
4979 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4980 NULL, repo, no_ignores);
4983 got_repo_close(repo);
4985 got_worktree_close(worktree);
4986 TAILQ_FOREACH(pe, &paths, entry)
4987 free((char *)pe->path);
4988 got_pathlist_free(&paths);
4996 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5001 static const struct got_error *
5002 print_remove_status(void *arg, unsigned char status,
5003 unsigned char staged_status, const char *path)
5005 while (path[0] == '/')
5007 if (status == GOT_STATUS_NONEXISTENT)
5009 if (status == staged_status && (status == GOT_STATUS_DELETE))
5010 status = GOT_STATUS_NO_CHANGE;
5011 printf("%c%c %s\n", status, staged_status, path);
5015 static const struct got_error *
5016 cmd_remove(int argc, char *argv[])
5018 const struct got_error *error = NULL;
5019 struct got_worktree *worktree = NULL;
5020 struct got_repository *repo = NULL;
5022 struct got_pathlist_head paths;
5023 struct got_pathlist_entry *pe;
5024 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5028 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5031 delete_local_mods = 1;
5049 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5056 cwd = getcwd(NULL, 0);
5058 error = got_error_from_errno("getcwd");
5061 error = got_worktree_open(&worktree, cwd);
5065 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5070 error = apply_unveil(got_repo_get_path(repo), 1,
5071 got_worktree_get_root_path(worktree));
5075 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5082 TAILQ_FOREACH(pe, &paths, entry) {
5083 if (asprintf(&ondisk_path, "%s/%s",
5084 got_worktree_get_root_path(worktree),
5086 error = got_error_from_errno("asprintf");
5089 if (lstat(ondisk_path, &sb) == -1) {
5090 if (errno == ENOENT) {
5094 error = got_error_from_errno2("lstat",
5100 if (S_ISDIR(sb.st_mode)) {
5101 error = got_error_msg(GOT_ERR_BAD_PATH,
5102 "removing directories requires -R option");
5108 error = got_worktree_schedule_delete(worktree, &paths,
5109 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5112 got_repo_close(repo);
5114 got_worktree_close(worktree);
5115 TAILQ_FOREACH(pe, &paths, entry)
5116 free((char *)pe->path);
5117 got_pathlist_free(&paths);
5125 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5126 "path ...\n", getprogname());
5130 static const struct got_error *
5131 revert_progress(void *arg, unsigned char status, const char *path)
5133 if (status == GOT_STATUS_UNVERSIONED)
5136 while (path[0] == '/')
5138 printf("%c %s\n", status, path);
5142 struct choose_patch_arg {
5143 FILE *patch_script_file;
5147 static const struct got_error *
5148 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5149 int nchanges, const char *action)
5152 size_t linesize = 0;
5156 case GOT_STATUS_ADD:
5157 printf("A %s\n%s this addition? [y/n] ", path, action);
5159 case GOT_STATUS_DELETE:
5160 printf("D %s\n%s this deletion? [y/n] ", path, action);
5162 case GOT_STATUS_MODIFY:
5163 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5164 return got_error_from_errno("fseek");
5165 printf(GOT_COMMIT_SEP_STR);
5166 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5168 if (ferror(patch_file))
5169 return got_error_from_errno("getline");
5170 printf(GOT_COMMIT_SEP_STR);
5171 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5172 path, n, nchanges, action);
5175 return got_error_path(path, GOT_ERR_FILE_STATUS);
5181 static const struct got_error *
5182 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5183 FILE *patch_file, int n, int nchanges)
5185 const struct got_error *err = NULL;
5187 size_t linesize = 0;
5190 struct choose_patch_arg *a = arg;
5192 *choice = GOT_PATCH_CHOICE_NONE;
5194 if (a->patch_script_file) {
5196 err = show_change(status, path, patch_file, n, nchanges,
5200 linelen = getline(&line, &linesize, a->patch_script_file);
5201 if (linelen == -1) {
5202 if (ferror(a->patch_script_file))
5203 return got_error_from_errno("getline");
5206 nl = strchr(line, '\n');
5209 if (strcmp(line, "y") == 0) {
5210 *choice = GOT_PATCH_CHOICE_YES;
5212 } else if (strcmp(line, "n") == 0) {
5213 *choice = GOT_PATCH_CHOICE_NO;
5215 } else if (strcmp(line, "q") == 0 &&
5216 status == GOT_STATUS_MODIFY) {
5217 *choice = GOT_PATCH_CHOICE_QUIT;
5220 printf("invalid response '%s'\n", line);
5225 while (resp != 'y' && resp != 'n' && resp != 'q') {
5226 err = show_change(status, path, patch_file, n, nchanges,
5233 if (status == GOT_STATUS_MODIFY) {
5234 if (resp != 'y' && resp != 'n' && resp != 'q') {
5235 printf("invalid response '%c'\n", resp);
5238 } else if (resp != 'y' && resp != 'n') {
5239 printf("invalid response '%c'\n", resp);
5245 *choice = GOT_PATCH_CHOICE_YES;
5246 else if (resp == 'n')
5247 *choice = GOT_PATCH_CHOICE_NO;
5248 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5249 *choice = GOT_PATCH_CHOICE_QUIT;
5255 static const struct got_error *
5256 cmd_revert(int argc, char *argv[])
5258 const struct got_error *error = NULL;
5259 struct got_worktree *worktree = NULL;
5260 struct got_repository *repo = NULL;
5261 char *cwd = NULL, *path = NULL;
5262 struct got_pathlist_head paths;
5263 struct got_pathlist_entry *pe;
5264 int ch, can_recurse = 0, pflag = 0;
5265 FILE *patch_script_file = NULL;
5266 const char *patch_script_path = NULL;
5267 struct choose_patch_arg cpa;
5271 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5277 patch_script_path = optarg;
5292 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5293 "unveil", NULL) == -1)
5298 if (patch_script_path && !pflag)
5299 errx(1, "-F option can only be used together with -p option");
5301 cwd = getcwd(NULL, 0);
5303 error = got_error_from_errno("getcwd");
5306 error = got_worktree_open(&worktree, cwd);
5310 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5315 if (patch_script_path) {
5316 patch_script_file = fopen(patch_script_path, "r");
5317 if (patch_script_file == NULL) {
5318 error = got_error_from_errno2("fopen",
5323 error = apply_unveil(got_repo_get_path(repo), 1,
5324 got_worktree_get_root_path(worktree));
5328 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5335 TAILQ_FOREACH(pe, &paths, entry) {
5336 if (asprintf(&ondisk_path, "%s/%s",
5337 got_worktree_get_root_path(worktree),
5339 error = got_error_from_errno("asprintf");
5342 if (lstat(ondisk_path, &sb) == -1) {
5343 if (errno == ENOENT) {
5347 error = got_error_from_errno2("lstat",
5353 if (S_ISDIR(sb.st_mode)) {
5354 error = got_error_msg(GOT_ERR_BAD_PATH,
5355 "reverting directories requires -R option");
5361 cpa.patch_script_file = patch_script_file;
5362 cpa.action = "revert";
5363 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5364 pflag ? choose_patch : NULL, &cpa, repo);
5366 if (patch_script_file && fclose(patch_script_file) == EOF &&
5368 error = got_error_from_errno2("fclose", patch_script_path);
5370 got_repo_close(repo);
5372 got_worktree_close(worktree);
5381 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5386 struct collect_commit_logmsg_arg {
5387 const char *cmdline_log;
5389 const char *worktree_path;
5390 const char *branch_name;
5391 const char *repo_path;
5396 static const struct got_error *
5397 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5400 char *initial_content = NULL;
5401 struct got_pathlist_entry *pe;
5402 const struct got_error *err = NULL;
5403 char *template = NULL;
5404 struct collect_commit_logmsg_arg *a = arg;
5408 /* if a message was specified on the command line, just use it */
5409 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5410 len = strlen(a->cmdline_log) + 1;
5411 *logmsg = malloc(len + 1);
5412 if (*logmsg == NULL)
5413 return got_error_from_errno("malloc");
5414 strlcpy(*logmsg, a->cmdline_log, len);
5418 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5419 return got_error_from_errno("asprintf");
5421 if (asprintf(&initial_content,
5422 "\n# changes to be committed on branch %s:\n",
5423 a->branch_name) == -1)
5424 return got_error_from_errno("asprintf");
5426 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5430 dprintf(fd, initial_content);
5432 TAILQ_FOREACH(pe, commitable_paths, entry) {
5433 struct got_commitable *ct = pe->data;
5434 dprintf(fd, "# %c %s\n",
5435 got_commitable_get_status(ct),
5436 got_commitable_get_path(ct));
5440 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5442 free(initial_content);
5445 /* Editor is done; we can now apply unveil(2) */
5447 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5456 static const struct got_error *
5457 cmd_commit(int argc, char *argv[])
5459 const struct got_error *error = NULL;
5460 struct got_worktree *worktree = NULL;
5461 struct got_repository *repo = NULL;
5462 char *cwd = NULL, *id_str = NULL;
5463 struct got_object_id *id = NULL;
5464 const char *logmsg = NULL;
5465 struct collect_commit_logmsg_arg cl_arg;
5466 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5467 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5468 struct got_pathlist_head paths;
5471 cl_arg.logmsg_path = NULL;
5473 while ((ch = getopt(argc, argv, "m:")) != -1) {
5488 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5489 "unveil", NULL) == -1)
5492 cwd = getcwd(NULL, 0);
5494 error = got_error_from_errno("getcwd");
5497 error = got_worktree_open(&worktree, cwd);
5501 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5504 if (rebase_in_progress) {
5505 error = got_error(GOT_ERR_REBASING);
5509 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5514 error = get_gitconfig_path(&gitconfig_path);
5517 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5522 error = get_author(&author, repo);
5527 * unveil(2) traverses exec(2); if an editor is used we have
5528 * to apply unveil after the log message has been written.
5530 if (logmsg == NULL || strlen(logmsg) == 0)
5531 error = get_editor(&editor);
5533 error = apply_unveil(got_repo_get_path(repo), 0,
5534 got_worktree_get_root_path(worktree));
5538 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5542 cl_arg.editor = editor;
5543 cl_arg.cmdline_log = logmsg;
5544 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5545 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5546 if (!histedit_in_progress) {
5547 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5548 error = got_error(GOT_ERR_COMMIT_BRANCH);
5551 cl_arg.branch_name += 11;
5553 cl_arg.repo_path = got_repo_get_path(repo);
5554 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5555 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5557 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5558 cl_arg.logmsg_path != NULL)
5559 preserve_logmsg = 1;
5563 error = got_object_id_str(&id_str, id);
5566 printf("Created commit %s\n", id_str);
5568 if (preserve_logmsg) {
5569 fprintf(stderr, "%s: log message preserved in %s\n",
5570 getprogname(), cl_arg.logmsg_path);
5571 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5573 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5574 free(cl_arg.logmsg_path);
5576 got_repo_close(repo);
5578 got_worktree_close(worktree);
5581 free(gitconfig_path);
5588 usage_cherrypick(void)
5590 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5594 static const struct got_error *
5595 cmd_cherrypick(int argc, char *argv[])
5597 const struct got_error *error = NULL;
5598 struct got_worktree *worktree = NULL;
5599 struct got_repository *repo = NULL;
5600 char *cwd = NULL, *commit_id_str = NULL;
5601 struct got_object_id *commit_id = NULL;
5602 struct got_commit_object *commit = NULL;
5603 struct got_object_qid *pid;
5604 struct got_reference *head_ref = NULL;
5605 int ch, did_something = 0;
5607 while ((ch = getopt(argc, argv, "")) != -1) {
5619 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5620 "unveil", NULL) == -1)
5626 cwd = getcwd(NULL, 0);
5628 error = got_error_from_errno("getcwd");
5631 error = got_worktree_open(&worktree, cwd);
5635 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5640 error = apply_unveil(got_repo_get_path(repo), 0,
5641 got_worktree_get_root_path(worktree));
5645 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5646 GOT_OBJ_TYPE_COMMIT, repo);
5647 if (error != NULL) {
5648 struct got_reference *ref;
5649 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5651 error = got_ref_open(&ref, repo, argv[0], 0);
5654 error = got_ref_resolve(&commit_id, repo, ref);
5659 error = got_object_id_str(&commit_id_str, commit_id);
5663 error = got_ref_open(&head_ref, repo,
5664 got_worktree_get_head_ref_name(worktree), 0);
5668 error = check_same_branch(commit_id, head_ref, NULL, repo);
5670 if (error->code != GOT_ERR_ANCESTRY)
5674 error = got_error(GOT_ERR_SAME_BRANCH);
5678 error = got_object_open_as_commit(&commit, repo, commit_id);
5681 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5682 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5683 commit_id, repo, update_progress, &did_something, check_cancelled,
5689 printf("Merged commit %s\n", commit_id_str);
5692 got_object_commit_close(commit);
5693 free(commit_id_str);
5695 got_ref_close(head_ref);
5697 got_worktree_close(worktree);
5699 got_repo_close(repo);
5706 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5710 static const struct got_error *
5711 cmd_backout(int argc, char *argv[])
5713 const struct got_error *error = NULL;
5714 struct got_worktree *worktree = NULL;
5715 struct got_repository *repo = NULL;
5716 char *cwd = NULL, *commit_id_str = NULL;
5717 struct got_object_id *commit_id = NULL;
5718 struct got_commit_object *commit = NULL;
5719 struct got_object_qid *pid;
5720 struct got_reference *head_ref = NULL;
5721 int ch, did_something = 0;
5723 while ((ch = getopt(argc, argv, "")) != -1) {
5735 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5736 "unveil", NULL) == -1)
5742 cwd = getcwd(NULL, 0);
5744 error = got_error_from_errno("getcwd");
5747 error = got_worktree_open(&worktree, cwd);
5751 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5756 error = apply_unveil(got_repo_get_path(repo), 0,
5757 got_worktree_get_root_path(worktree));
5761 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5762 GOT_OBJ_TYPE_COMMIT, repo);
5763 if (error != NULL) {
5764 struct got_reference *ref;
5765 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5767 error = got_ref_open(&ref, repo, argv[0], 0);
5770 error = got_ref_resolve(&commit_id, repo, ref);
5775 error = got_object_id_str(&commit_id_str, commit_id);
5779 error = got_ref_open(&head_ref, repo,
5780 got_worktree_get_head_ref_name(worktree), 0);
5784 error = check_same_branch(commit_id, head_ref, NULL, repo);
5788 error = got_object_open_as_commit(&commit, repo, commit_id);
5791 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5793 error = got_error(GOT_ERR_ROOT_COMMIT);
5797 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5798 update_progress, &did_something, check_cancelled, NULL);
5803 printf("Backed out commit %s\n", commit_id_str);
5806 got_object_commit_close(commit);
5807 free(commit_id_str);
5809 got_ref_close(head_ref);
5811 got_worktree_close(worktree);
5813 got_repo_close(repo);
5820 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5826 trim_logmsg(char *logmsg, int limit)
5831 len = strlen(logmsg);
5835 nl = strchr(logmsg, '\n');
5840 static const struct got_error *
5841 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5843 const struct got_error *err;
5844 char *logmsg0 = NULL;
5847 err = got_object_commit_get_logmsg(&logmsg0, commit);
5852 while (isspace((unsigned char)s[0]))
5855 *logmsg = strdup(s);
5856 if (*logmsg == NULL) {
5857 err = got_error_from_errno("strdup");
5861 trim_logmsg(*logmsg, limit);
5867 static const struct got_error *
5868 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5870 const struct got_error *err;
5871 struct got_commit_object *commit = NULL;
5872 char *id_str = NULL, *logmsg = NULL;
5874 err = got_object_open_as_commit(&commit, repo, id);
5878 err = got_object_id_str(&id_str, id);
5884 err = get_short_logmsg(&logmsg, 42, commit);
5888 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5891 got_object_commit_close(commit);
5896 static const struct got_error *
5897 show_rebase_progress(struct got_commit_object *commit,
5898 struct got_object_id *old_id, struct got_object_id *new_id)
5900 const struct got_error *err;
5901 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5903 err = got_object_id_str(&old_id_str, old_id);
5908 err = got_object_id_str(&new_id_str, new_id);
5913 old_id_str[12] = '\0';
5915 new_id_str[12] = '\0';
5917 err = get_short_logmsg(&logmsg, 42, commit);
5921 printf("%s -> %s: %s\n", old_id_str,
5922 new_id_str ? new_id_str : "no-op change", logmsg);
5930 static const struct got_error *
5931 rebase_progress(void *arg, unsigned char status, const char *path)
5933 unsigned char *rebase_status = arg;
5935 while (path[0] == '/')
5937 printf("%c %s\n", status, path);
5939 if (*rebase_status == GOT_STATUS_CONFLICT)
5941 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5942 *rebase_status = status;
5946 static const struct got_error *
5947 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5948 struct got_reference *branch, struct got_reference *new_base_branch,
5949 struct got_reference *tmp_branch, struct got_repository *repo)
5951 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5952 return got_worktree_rebase_complete(worktree, fileindex,
5953 new_base_branch, tmp_branch, branch, repo);
5956 static const struct got_error *
5957 rebase_commit(struct got_pathlist_head *merged_paths,
5958 struct got_worktree *worktree, struct got_fileindex *fileindex,
5959 struct got_reference *tmp_branch,
5960 struct got_object_id *commit_id, struct got_repository *repo)
5962 const struct got_error *error;
5963 struct got_commit_object *commit;
5964 struct got_object_id *new_commit_id;
5966 error = got_object_open_as_commit(&commit, repo, commit_id);
5970 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5971 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5973 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5975 error = show_rebase_progress(commit, commit_id, NULL);
5977 error = show_rebase_progress(commit, commit_id, new_commit_id);
5978 free(new_commit_id);
5981 got_object_commit_close(commit);
5985 struct check_path_prefix_arg {
5986 const char *path_prefix;
5991 static const struct got_error *
5992 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5993 struct got_blob_object *blob2, struct got_object_id *id1,
5994 struct got_object_id *id2, const char *path1, const char *path2,
5995 mode_t mode1, mode_t mode2, struct got_repository *repo)
5997 struct check_path_prefix_arg *a = arg;
5999 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6000 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6001 return got_error(a->errcode);
6006 static const struct got_error *
6007 check_path_prefix(struct got_object_id *parent_id,
6008 struct got_object_id *commit_id, const char *path_prefix,
6009 int errcode, struct got_repository *repo)
6011 const struct got_error *err;
6012 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6013 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6014 struct check_path_prefix_arg cpp_arg;
6016 if (got_path_is_root_dir(path_prefix))
6019 err = got_object_open_as_commit(&commit, repo, commit_id);
6023 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6027 err = got_object_open_as_tree(&tree1, repo,
6028 got_object_commit_get_tree_id(parent_commit));
6032 err = got_object_open_as_tree(&tree2, repo,
6033 got_object_commit_get_tree_id(commit));
6037 cpp_arg.path_prefix = path_prefix;
6038 while (cpp_arg.path_prefix[0] == '/')
6039 cpp_arg.path_prefix++;
6040 cpp_arg.len = strlen(cpp_arg.path_prefix);
6041 cpp_arg.errcode = errcode;
6042 err = got_diff_tree(tree1, tree2, "", "", repo,
6043 check_path_prefix_in_diff, &cpp_arg, 0);
6046 got_object_tree_close(tree1);
6048 got_object_tree_close(tree2);
6050 got_object_commit_close(commit);
6052 got_object_commit_close(parent_commit);
6056 static const struct got_error *
6057 collect_commits(struct got_object_id_queue *commits,
6058 struct got_object_id *initial_commit_id,
6059 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6060 const char *path_prefix, int path_prefix_errcode,
6061 struct got_repository *repo)
6063 const struct got_error *err = NULL;
6064 struct got_commit_graph *graph = NULL;
6065 struct got_object_id *parent_id = NULL;
6066 struct got_object_qid *qid;
6067 struct got_object_id *commit_id = initial_commit_id;
6069 err = got_commit_graph_open(&graph, "/", 1);
6073 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6074 check_cancelled, NULL);
6077 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6078 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6079 check_cancelled, NULL);
6081 if (err->code == GOT_ERR_ITER_COMPLETED) {
6082 err = got_error_msg(GOT_ERR_ANCESTRY,
6083 "ran out of commits to rebase before "
6084 "youngest common ancestor commit has "
6089 err = check_path_prefix(parent_id, commit_id,
6090 path_prefix, path_prefix_errcode, repo);
6094 err = got_object_qid_alloc(&qid, commit_id);
6097 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6098 commit_id = parent_id;
6102 got_commit_graph_close(graph);
6106 static const struct got_error *
6107 cmd_rebase(int argc, char *argv[])
6109 const struct got_error *error = NULL;
6110 struct got_worktree *worktree = NULL;
6111 struct got_repository *repo = NULL;
6112 struct got_fileindex *fileindex = NULL;
6114 struct got_reference *branch = NULL;
6115 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6116 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6117 struct got_object_id *resume_commit_id = NULL;
6118 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6119 struct got_commit_object *commit = NULL;
6120 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6121 int histedit_in_progress = 0;
6122 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6123 struct got_object_id_queue commits;
6124 struct got_pathlist_head merged_paths;
6125 const struct got_object_id_queue *parent_ids;
6126 struct got_object_qid *qid, *pid;
6128 SIMPLEQ_INIT(&commits);
6129 TAILQ_INIT(&merged_paths);
6131 while ((ch = getopt(argc, argv, "ac")) != -1) {
6137 continue_rebase = 1;
6149 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6150 "unveil", NULL) == -1)
6153 if (abort_rebase && continue_rebase)
6155 else if (abort_rebase || continue_rebase) {
6158 } else if (argc != 1)
6161 cwd = getcwd(NULL, 0);
6163 error = got_error_from_errno("getcwd");
6166 error = got_worktree_open(&worktree, cwd);
6170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6175 error = apply_unveil(got_repo_get_path(repo), 0,
6176 got_worktree_get_root_path(worktree));
6180 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6184 if (histedit_in_progress) {
6185 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6189 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6195 if (!rebase_in_progress) {
6196 error = got_error(GOT_ERR_NOT_REBASING);
6199 error = got_worktree_rebase_continue(&resume_commit_id,
6200 &new_base_branch, &tmp_branch, &branch, &fileindex,
6204 printf("Switching work tree to %s\n",
6205 got_ref_get_symref_target(new_base_branch));
6206 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6207 new_base_branch, update_progress, &did_something);
6210 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6211 goto done; /* nothing else to do */
6214 if (continue_rebase) {
6215 if (!rebase_in_progress) {
6216 error = got_error(GOT_ERR_NOT_REBASING);
6219 error = got_worktree_rebase_continue(&resume_commit_id,
6220 &new_base_branch, &tmp_branch, &branch, &fileindex,
6225 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6226 resume_commit_id, repo);
6230 yca_id = got_object_id_dup(resume_commit_id);
6231 if (yca_id == NULL) {
6232 error = got_error_from_errno("got_object_id_dup");
6236 error = got_ref_open(&branch, repo, argv[0], 0);
6241 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6245 if (!continue_rebase) {
6246 struct got_object_id *base_commit_id;
6248 base_commit_id = got_worktree_get_base_commit_id(worktree);
6249 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6250 base_commit_id, branch_head_commit_id, repo,
6251 check_cancelled, NULL);
6254 if (yca_id == NULL) {
6255 error = got_error_msg(GOT_ERR_ANCESTRY,
6256 "specified branch shares no common ancestry "
6257 "with work tree's branch");
6261 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6263 if (error->code != GOT_ERR_ANCESTRY)
6267 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6268 "specified branch resolves to a commit which "
6269 "is already contained in work tree's branch");
6272 error = got_worktree_rebase_prepare(&new_base_branch,
6273 &tmp_branch, &fileindex, worktree, branch, repo);
6278 commit_id = branch_head_commit_id;
6279 error = got_object_open_as_commit(&commit, repo, commit_id);
6283 parent_ids = got_object_commit_get_parent_ids(commit);
6284 pid = SIMPLEQ_FIRST(parent_ids);
6286 if (!continue_rebase) {
6288 error = got_worktree_rebase_abort(worktree, fileindex,
6289 repo, new_base_branch, update_progress,
6293 printf("Rebase of %s aborted\n",
6294 got_ref_get_name(branch));
6296 error = got_error(GOT_ERR_EMPTY_REBASE);
6299 error = collect_commits(&commits, commit_id, pid->id,
6300 yca_id, got_worktree_get_path_prefix(worktree),
6301 GOT_ERR_REBASE_PATH, repo);
6302 got_object_commit_close(commit);
6307 if (SIMPLEQ_EMPTY(&commits)) {
6308 if (continue_rebase) {
6309 error = rebase_complete(worktree, fileindex,
6310 branch, new_base_branch, tmp_branch, repo);
6313 /* Fast-forward the reference of the branch. */
6314 struct got_object_id *new_head_commit_id;
6316 error = got_ref_resolve(&new_head_commit_id, repo,
6320 error = got_object_id_str(&id_str, new_head_commit_id);
6321 printf("Forwarding %s to commit %s\n",
6322 got_ref_get_name(branch), id_str);
6324 error = got_ref_change_ref(branch,
6325 new_head_commit_id);
6332 SIMPLEQ_FOREACH(qid, &commits, entry) {
6333 commit_id = qid->id;
6334 parent_id = pid ? pid->id : yca_id;
6337 error = got_worktree_rebase_merge_files(&merged_paths,
6338 worktree, fileindex, parent_id, commit_id, repo,
6339 rebase_progress, &rebase_status, check_cancelled, NULL);
6343 if (rebase_status == GOT_STATUS_CONFLICT) {
6344 error = show_rebase_merge_conflict(qid->id, repo);
6347 got_worktree_rebase_pathlist_free(&merged_paths);
6351 error = rebase_commit(&merged_paths, worktree, fileindex,
6352 tmp_branch, commit_id, repo);
6353 got_worktree_rebase_pathlist_free(&merged_paths);
6358 if (rebase_status == GOT_STATUS_CONFLICT) {
6359 error = got_worktree_rebase_postpone(worktree, fileindex);
6362 error = got_error_msg(GOT_ERR_CONFLICTS,
6363 "conflicts must be resolved before rebasing can continue");
6365 error = rebase_complete(worktree, fileindex, branch,
6366 new_base_branch, tmp_branch, repo);
6368 got_object_id_queue_free(&commits);
6369 free(branch_head_commit_id);
6370 free(resume_commit_id);
6373 got_object_commit_close(commit);
6375 got_ref_close(branch);
6376 if (new_base_branch)
6377 got_ref_close(new_base_branch);
6379 got_ref_close(tmp_branch);
6381 got_worktree_close(worktree);
6383 got_repo_close(repo);
6388 usage_histedit(void)
6390 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6395 #define GOT_HISTEDIT_PICK 'p'
6396 #define GOT_HISTEDIT_EDIT 'e'
6397 #define GOT_HISTEDIT_FOLD 'f'
6398 #define GOT_HISTEDIT_DROP 'd'
6399 #define GOT_HISTEDIT_MESG 'm'
6401 static struct got_histedit_cmd {
6405 } got_histedit_cmds[] = {
6406 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6407 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6408 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6410 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6411 { GOT_HISTEDIT_MESG, "mesg",
6412 "single-line log message for commit above (open editor if empty)" },
6415 struct got_histedit_list_entry {
6416 TAILQ_ENTRY(got_histedit_list_entry) entry;
6417 struct got_object_id *commit_id;
6418 const struct got_histedit_cmd *cmd;
6421 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6423 static const struct got_error *
6424 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6425 FILE *f, struct got_repository *repo)
6427 const struct got_error *err = NULL;
6428 char *logmsg = NULL, *id_str = NULL;
6429 struct got_commit_object *commit = NULL;
6432 err = got_object_open_as_commit(&commit, repo, commit_id);
6436 err = get_short_logmsg(&logmsg, 34, commit);
6440 err = got_object_id_str(&id_str, commit_id);
6444 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6446 err = got_ferror(f, GOT_ERR_IO);
6449 got_object_commit_close(commit);
6455 static const struct got_error *
6456 histedit_write_commit_list(struct got_object_id_queue *commits,
6457 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6459 const struct got_error *err = NULL;
6460 struct got_object_qid *qid;
6462 if (SIMPLEQ_EMPTY(commits))
6463 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6465 SIMPLEQ_FOREACH(qid, commits, entry) {
6466 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6470 if (edit_logmsg_only) {
6471 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6473 err = got_ferror(f, GOT_ERR_IO);
6482 static const struct got_error *
6483 write_cmd_list(FILE *f, const char *branch_name,
6484 struct got_object_id_queue *commits)
6486 const struct got_error *err = NULL;
6489 struct got_object_qid *qid;
6491 qid = SIMPLEQ_FIRST(commits);
6492 err = got_object_id_str(&id_str, qid->id);
6497 "# Editing the history of branch '%s' starting at\n"
6499 "# Commits will be processed in order from top to "
6500 "bottom of this file.\n", branch_name, id_str);
6502 err = got_ferror(f, GOT_ERR_IO);
6506 n = fprintf(f, "# Available histedit commands:\n");
6508 err = got_ferror(f, GOT_ERR_IO);
6512 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6513 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6514 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6517 err = got_ferror(f, GOT_ERR_IO);
6526 static const struct got_error *
6527 histedit_syntax_error(int lineno)
6529 static char msg[42];
6532 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6534 if (ret == -1 || ret >= sizeof(msg))
6535 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6537 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6540 static const struct got_error *
6541 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6542 char *logmsg, struct got_repository *repo)
6544 const struct got_error *err;
6545 struct got_commit_object *folded_commit = NULL;
6546 char *id_str, *folded_logmsg = NULL;
6548 err = got_object_id_str(&id_str, hle->commit_id);
6552 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6556 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6559 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6560 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6561 folded_logmsg) == -1) {
6562 err = got_error_from_errno("asprintf");
6566 got_object_commit_close(folded_commit);
6568 free(folded_logmsg);
6572 static struct got_histedit_list_entry *
6573 get_folded_commits(struct got_histedit_list_entry *hle)
6575 struct got_histedit_list_entry *prev, *folded = NULL;
6577 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6578 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6579 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6580 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6582 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6588 static const struct got_error *
6589 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6590 struct got_repository *repo)
6592 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6593 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6594 const struct got_error *err = NULL;
6595 struct got_commit_object *commit = NULL;
6597 struct got_histedit_list_entry *folded = NULL;
6599 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6603 folded = get_folded_commits(hle);
6605 while (folded != hle) {
6606 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6607 folded = TAILQ_NEXT(folded, entry);
6610 err = append_folded_commit_msg(&new_msg, folded,
6616 folded = TAILQ_NEXT(folded, entry);
6620 err = got_object_id_str(&id_str, hle->commit_id);
6623 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6626 if (asprintf(&new_msg,
6627 "%s\n# original log message of commit %s: %s",
6628 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6629 err = got_error_from_errno("asprintf");
6635 err = got_object_id_str(&id_str, hle->commit_id);
6639 err = got_opentemp_named_fd(&logmsg_path, &fd,
6640 GOT_TMPDIR_STR "/got-logmsg");
6644 dprintf(fd, logmsg);
6647 err = get_editor(&editor);
6651 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6653 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6655 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6658 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6659 err = got_error_from_errno2("unlink", logmsg_path);
6665 got_object_commit_close(commit);
6669 static const struct got_error *
6670 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6671 FILE *f, struct got_repository *repo)
6673 const struct got_error *err = NULL;
6674 char *line = NULL, *p, *end;
6678 const struct got_histedit_cmd *cmd;
6679 struct got_object_id *commit_id = NULL;
6680 struct got_histedit_list_entry *hle = NULL;
6683 len = getline(&line, &size, f);
6685 const struct got_error *getline_err;
6688 getline_err = got_error_from_errno("getline");
6689 err = got_ferror(f, getline_err->code);
6694 while (isspace((unsigned char)p[0]))
6696 if (p[0] == '#' || p[0] == '\0') {
6702 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6703 cmd = &got_histedit_cmds[i];
6704 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6705 isspace((unsigned char)p[strlen(cmd->name)])) {
6706 p += strlen(cmd->name);
6709 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6714 if (i == nitems(got_histedit_cmds)) {
6715 err = histedit_syntax_error(lineno);
6718 while (isspace((unsigned char)p[0]))
6720 if (cmd->code == GOT_HISTEDIT_MESG) {
6721 if (hle == NULL || hle->logmsg != NULL) {
6722 err = got_error(GOT_ERR_HISTEDIT_CMD);
6726 err = histedit_edit_logmsg(hle, repo);
6730 hle->logmsg = strdup(p);
6731 if (hle->logmsg == NULL) {
6732 err = got_error_from_errno("strdup");
6741 while (end[0] && !isspace((unsigned char)end[0]))
6745 err = got_object_resolve_id_str(&commit_id, repo, p);
6747 /* override error code */
6748 err = histedit_syntax_error(lineno);
6752 hle = malloc(sizeof(*hle));
6754 err = got_error_from_errno("malloc");
6758 hle->commit_id = commit_id;
6763 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6771 static const struct got_error *
6772 histedit_check_script(struct got_histedit_list *histedit_cmds,
6773 struct got_object_id_queue *commits, struct got_repository *repo)
6775 const struct got_error *err = NULL;
6776 struct got_object_qid *qid;
6777 struct got_histedit_list_entry *hle;
6778 static char msg[92];
6781 if (TAILQ_EMPTY(histedit_cmds))
6782 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6783 "histedit script contains no commands");
6784 if (SIMPLEQ_EMPTY(commits))
6785 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6787 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6788 struct got_histedit_list_entry *hle2;
6789 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6792 if (got_object_id_cmp(hle->commit_id,
6793 hle2->commit_id) != 0)
6795 err = got_object_id_str(&id_str, hle->commit_id);
6798 snprintf(msg, sizeof(msg), "commit %s is listed "
6799 "more than once in histedit script", id_str);
6801 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6805 SIMPLEQ_FOREACH(qid, commits, entry) {
6806 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6807 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6811 err = got_object_id_str(&id_str, qid->id);
6814 snprintf(msg, sizeof(msg),
6815 "commit %s missing from histedit script", id_str);
6817 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6821 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6822 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6823 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6824 "last commit in histedit script cannot be folded");
6829 static const struct got_error *
6830 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6831 const char *path, struct got_object_id_queue *commits,
6832 struct got_repository *repo)
6834 const struct got_error *err = NULL;
6838 err = get_editor(&editor);
6842 if (spawn_editor(editor, path) == -1) {
6843 err = got_error_from_errno("failed spawning editor");
6847 f = fopen(path, "r");
6849 err = got_error_from_errno("fopen");
6852 err = histedit_parse_list(histedit_cmds, f, repo);
6856 err = histedit_check_script(histedit_cmds, commits, repo);
6858 if (f && fclose(f) != 0 && err == NULL)
6859 err = got_error_from_errno("fclose");
6864 static const struct got_error *
6865 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6866 struct got_object_id_queue *, const char *, const char *,
6867 struct got_repository *);
6869 static const struct got_error *
6870 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6871 struct got_object_id_queue *commits, const char *branch_name,
6872 int edit_logmsg_only, struct got_repository *repo)
6874 const struct got_error *err;
6878 err = got_opentemp_named(&path, &f, "got-histedit");
6882 err = write_cmd_list(f, branch_name, commits);
6886 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6890 if (edit_logmsg_only) {
6892 err = histedit_parse_list(histedit_cmds, f, repo);
6894 if (fclose(f) != 0) {
6895 err = got_error_from_errno("fclose");
6899 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6901 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6902 err->code != GOT_ERR_HISTEDIT_CMD)
6904 err = histedit_edit_list_retry(histedit_cmds, err,
6905 commits, path, branch_name, repo);
6909 if (f && fclose(f) != 0 && err == NULL)
6910 err = got_error_from_errno("fclose");
6911 if (path && unlink(path) != 0 && err == NULL)
6912 err = got_error_from_errno2("unlink", path);
6917 static const struct got_error *
6918 histedit_save_list(struct got_histedit_list *histedit_cmds,
6919 struct got_worktree *worktree, struct got_repository *repo)
6921 const struct got_error *err = NULL;
6924 struct got_histedit_list_entry *hle;
6925 struct got_commit_object *commit = NULL;
6927 err = got_worktree_get_histedit_script_path(&path, worktree);
6931 f = fopen(path, "w");
6933 err = got_error_from_errno2("fopen", path);
6936 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6937 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6943 int n = fprintf(f, "%c %s\n",
6944 GOT_HISTEDIT_MESG, hle->logmsg);
6946 err = got_ferror(f, GOT_ERR_IO);
6952 if (f && fclose(f) != 0 && err == NULL)
6953 err = got_error_from_errno("fclose");
6956 got_object_commit_close(commit);
6961 histedit_free_list(struct got_histedit_list *histedit_cmds)
6963 struct got_histedit_list_entry *hle;
6965 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6966 TAILQ_REMOVE(histedit_cmds, hle, entry);
6971 static const struct got_error *
6972 histedit_load_list(struct got_histedit_list *histedit_cmds,
6973 const char *path, struct got_repository *repo)
6975 const struct got_error *err = NULL;
6978 f = fopen(path, "r");
6980 err = got_error_from_errno2("fopen", path);
6984 err = histedit_parse_list(histedit_cmds, f, repo);
6986 if (f && fclose(f) != 0 && err == NULL)
6987 err = got_error_from_errno("fclose");
6991 static const struct got_error *
6992 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6993 const struct got_error *edit_err, struct got_object_id_queue *commits,
6994 const char *path, const char *branch_name, struct got_repository *repo)
6996 const struct got_error *err = NULL, *prev_err = edit_err;
6999 while (resp != 'c' && resp != 'r' && resp != 'a') {
7000 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7001 "or (a)bort: ", getprogname(), prev_err->msg);
7006 histedit_free_list(histedit_cmds);
7007 err = histedit_run_editor(histedit_cmds, path, commits,
7010 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7011 err->code != GOT_ERR_HISTEDIT_CMD)
7018 } else if (resp == 'r') {
7019 histedit_free_list(histedit_cmds);
7020 err = histedit_edit_script(histedit_cmds,
7021 commits, branch_name, 0, repo);
7023 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7024 err->code != GOT_ERR_HISTEDIT_CMD)
7031 } else if (resp == 'a') {
7032 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7035 printf("invalid response '%c'\n", resp);
7041 static const struct got_error *
7042 histedit_complete(struct got_worktree *worktree,
7043 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7044 struct got_reference *branch, struct got_repository *repo)
7046 printf("Switching work tree to %s\n",
7047 got_ref_get_symref_target(branch));
7048 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7052 static const struct got_error *
7053 show_histedit_progress(struct got_commit_object *commit,
7054 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7056 const struct got_error *err;
7057 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7059 err = got_object_id_str(&old_id_str, hle->commit_id);
7064 err = got_object_id_str(&new_id_str, new_id);
7069 old_id_str[12] = '\0';
7071 new_id_str[12] = '\0';
7074 logmsg = strdup(hle->logmsg);
7075 if (logmsg == NULL) {
7076 err = got_error_from_errno("strdup");
7079 trim_logmsg(logmsg, 42);
7081 err = get_short_logmsg(&logmsg, 42, commit);
7086 switch (hle->cmd->code) {
7087 case GOT_HISTEDIT_PICK:
7088 case GOT_HISTEDIT_EDIT:
7089 printf("%s -> %s: %s\n", old_id_str,
7090 new_id_str ? new_id_str : "no-op change", logmsg);
7092 case GOT_HISTEDIT_DROP:
7093 case GOT_HISTEDIT_FOLD:
7094 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7106 static const struct got_error *
7107 histedit_commit(struct got_pathlist_head *merged_paths,
7108 struct got_worktree *worktree, struct got_fileindex *fileindex,
7109 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7110 struct got_repository *repo)
7112 const struct got_error *err;
7113 struct got_commit_object *commit;
7114 struct got_object_id *new_commit_id;
7116 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7117 && hle->logmsg == NULL) {
7118 err = histedit_edit_logmsg(hle, repo);
7123 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7127 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7128 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7131 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7133 err = show_histedit_progress(commit, hle, NULL);
7135 err = show_histedit_progress(commit, hle, new_commit_id);
7136 free(new_commit_id);
7139 got_object_commit_close(commit);
7143 static const struct got_error *
7144 histedit_skip_commit(struct got_histedit_list_entry *hle,
7145 struct got_worktree *worktree, struct got_repository *repo)
7147 const struct got_error *error;
7148 struct got_commit_object *commit;
7150 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7155 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7159 error = show_histedit_progress(commit, hle, NULL);
7160 got_object_commit_close(commit);
7164 static const struct got_error *
7165 check_local_changes(void *arg, unsigned char status,
7166 unsigned char staged_status, const char *path,
7167 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7168 struct got_object_id *commit_id, int dirfd, const char *de_name)
7170 int *have_local_changes = arg;
7173 case GOT_STATUS_ADD:
7174 case GOT_STATUS_DELETE:
7175 case GOT_STATUS_MODIFY:
7176 case GOT_STATUS_CONFLICT:
7177 *have_local_changes = 1;
7178 return got_error(GOT_ERR_CANCELLED);
7183 switch (staged_status) {
7184 case GOT_STATUS_ADD:
7185 case GOT_STATUS_DELETE:
7186 case GOT_STATUS_MODIFY:
7187 *have_local_changes = 1;
7188 return got_error(GOT_ERR_CANCELLED);
7196 static const struct got_error *
7197 cmd_histedit(int argc, char *argv[])
7199 const struct got_error *error = NULL;
7200 struct got_worktree *worktree = NULL;
7201 struct got_fileindex *fileindex = NULL;
7202 struct got_repository *repo = NULL;
7204 struct got_reference *branch = NULL;
7205 struct got_reference *tmp_branch = NULL;
7206 struct got_object_id *resume_commit_id = NULL;
7207 struct got_object_id *base_commit_id = NULL;
7208 struct got_object_id *head_commit_id = NULL;
7209 struct got_commit_object *commit = NULL;
7210 int ch, rebase_in_progress = 0, did_something;
7211 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7212 int edit_logmsg_only = 0;
7213 const char *edit_script_path = NULL;
7214 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7215 struct got_object_id_queue commits;
7216 struct got_pathlist_head merged_paths;
7217 const struct got_object_id_queue *parent_ids;
7218 struct got_object_qid *pid;
7219 struct got_histedit_list histedit_cmds;
7220 struct got_histedit_list_entry *hle;
7222 SIMPLEQ_INIT(&commits);
7223 TAILQ_INIT(&histedit_cmds);
7224 TAILQ_INIT(&merged_paths);
7226 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7235 edit_script_path = optarg;
7238 edit_logmsg_only = 1;
7250 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7251 "unveil", NULL) == -1)
7254 if (abort_edit && continue_edit)
7255 errx(1, "histedit's -a and -c options are mutually exclusive");
7256 if (edit_script_path && edit_logmsg_only)
7257 errx(1, "histedit's -F and -m options are mutually exclusive");
7258 if (abort_edit && edit_logmsg_only)
7259 errx(1, "histedit's -a and -m options are mutually exclusive");
7260 if (continue_edit && edit_logmsg_only)
7261 errx(1, "histedit's -c and -m options are mutually exclusive");
7266 * This command cannot apply unveil(2) in all cases because the
7267 * user may choose to run an editor to edit the histedit script
7268 * and to edit individual commit log messages.
7269 * unveil(2) traverses exec(2); if an editor is used we have to
7270 * apply unveil after edit script and log messages have been written.
7271 * XXX TODO: Make use of unveil(2) where possible.
7274 cwd = getcwd(NULL, 0);
7276 error = got_error_from_errno("getcwd");
7279 error = got_worktree_open(&worktree, cwd);
7283 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7288 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7291 if (rebase_in_progress) {
7292 error = got_error(GOT_ERR_REBASING);
7296 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7300 if (edit_in_progress && edit_logmsg_only) {
7301 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7302 "histedit operation is in progress in this "
7303 "work tree and must be continued or aborted "
7304 "before the -m option can be used");
7308 if (edit_in_progress && abort_edit) {
7309 error = got_worktree_histedit_continue(&resume_commit_id,
7310 &tmp_branch, &branch, &base_commit_id, &fileindex,
7314 printf("Switching work tree to %s\n",
7315 got_ref_get_symref_target(branch));
7316 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7317 branch, base_commit_id, update_progress, &did_something);
7320 printf("Histedit of %s aborted\n",
7321 got_ref_get_symref_target(branch));
7322 goto done; /* nothing else to do */
7323 } else if (abort_edit) {
7324 error = got_error(GOT_ERR_NOT_HISTEDIT);
7328 if (continue_edit) {
7331 if (!edit_in_progress) {
7332 error = got_error(GOT_ERR_NOT_HISTEDIT);
7336 error = got_worktree_get_histedit_script_path(&path, worktree);
7340 error = histedit_load_list(&histedit_cmds, path, repo);
7345 error = got_worktree_histedit_continue(&resume_commit_id,
7346 &tmp_branch, &branch, &base_commit_id, &fileindex,
7351 error = got_ref_resolve(&head_commit_id, repo, branch);
7355 error = got_object_open_as_commit(&commit, repo,
7359 parent_ids = got_object_commit_get_parent_ids(commit);
7360 pid = SIMPLEQ_FIRST(parent_ids);
7362 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7365 error = collect_commits(&commits, head_commit_id, pid->id,
7366 base_commit_id, got_worktree_get_path_prefix(worktree),
7367 GOT_ERR_HISTEDIT_PATH, repo);
7368 got_object_commit_close(commit);
7373 if (edit_in_progress) {
7374 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7378 error = got_ref_open(&branch, repo,
7379 got_worktree_get_head_ref_name(worktree), 0);
7383 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7384 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7385 "will not edit commit history of a branch outside "
7386 "the \"refs/heads/\" reference namespace");
7390 error = got_ref_resolve(&head_commit_id, repo, branch);
7391 got_ref_close(branch);
7396 error = got_object_open_as_commit(&commit, repo,
7400 parent_ids = got_object_commit_get_parent_ids(commit);
7401 pid = SIMPLEQ_FIRST(parent_ids);
7403 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7406 error = collect_commits(&commits, head_commit_id, pid->id,
7407 got_worktree_get_base_commit_id(worktree),
7408 got_worktree_get_path_prefix(worktree),
7409 GOT_ERR_HISTEDIT_PATH, repo);
7410 got_object_commit_close(commit);
7415 if (SIMPLEQ_EMPTY(&commits)) {
7416 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7420 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7421 &base_commit_id, &fileindex, worktree, repo);
7425 if (edit_script_path) {
7426 error = histedit_load_list(&histedit_cmds,
7427 edit_script_path, repo);
7429 got_worktree_histedit_abort(worktree, fileindex,
7430 repo, branch, base_commit_id,
7431 update_progress, &did_something);
7435 const char *branch_name;
7436 branch_name = got_ref_get_symref_target(branch);
7437 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7439 error = histedit_edit_script(&histedit_cmds, &commits,
7440 branch_name, edit_logmsg_only, repo);
7442 got_worktree_histedit_abort(worktree, fileindex,
7443 repo, branch, base_commit_id,
7444 update_progress, &did_something);
7450 error = histedit_save_list(&histedit_cmds, worktree,
7453 got_worktree_histedit_abort(worktree, fileindex,
7454 repo, branch, base_commit_id,
7455 update_progress, &did_something);
7461 error = histedit_check_script(&histedit_cmds, &commits, repo);
7465 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7466 if (resume_commit_id) {
7467 if (got_object_id_cmp(hle->commit_id,
7468 resume_commit_id) != 0)
7471 resume_commit_id = NULL;
7472 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7473 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7474 error = histedit_skip_commit(hle, worktree,
7479 struct got_pathlist_head paths;
7480 int have_changes = 0;
7483 error = got_pathlist_append(&paths, "", NULL);
7486 error = got_worktree_status(worktree, &paths,
7487 repo, check_local_changes, &have_changes,
7488 check_cancelled, NULL);
7489 got_pathlist_free(&paths);
7491 if (error->code != GOT_ERR_CANCELLED)
7493 if (sigint_received || sigpipe_received)
7497 error = histedit_commit(NULL, worktree,
7498 fileindex, tmp_branch, hle, repo);
7502 error = got_object_open_as_commit(
7503 &commit, repo, hle->commit_id);
7506 error = show_histedit_progress(commit,
7508 got_object_commit_close(commit);
7517 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7518 error = histedit_skip_commit(hle, worktree, repo);
7524 error = got_object_open_as_commit(&commit, repo,
7528 parent_ids = got_object_commit_get_parent_ids(commit);
7529 pid = SIMPLEQ_FIRST(parent_ids);
7531 error = got_worktree_histedit_merge_files(&merged_paths,
7532 worktree, fileindex, pid->id, hle->commit_id, repo,
7533 rebase_progress, &rebase_status, check_cancelled, NULL);
7536 got_object_commit_close(commit);
7539 if (rebase_status == GOT_STATUS_CONFLICT) {
7540 error = show_rebase_merge_conflict(hle->commit_id,
7544 got_worktree_rebase_pathlist_free(&merged_paths);
7548 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7550 error = got_object_id_str(&id_str, hle->commit_id);
7553 printf("Stopping histedit for amending commit %s\n",
7556 got_worktree_rebase_pathlist_free(&merged_paths);
7557 error = got_worktree_histedit_postpone(worktree,
7562 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7563 error = histedit_skip_commit(hle, worktree, repo);
7569 error = histedit_commit(&merged_paths, worktree, fileindex,
7570 tmp_branch, hle, repo);
7571 got_worktree_rebase_pathlist_free(&merged_paths);
7576 if (rebase_status == GOT_STATUS_CONFLICT) {
7577 error = got_worktree_histedit_postpone(worktree, fileindex);
7580 error = got_error_msg(GOT_ERR_CONFLICTS,
7581 "conflicts must be resolved before histedit can continue");
7583 error = histedit_complete(worktree, fileindex, tmp_branch,
7586 got_object_id_queue_free(&commits);
7587 histedit_free_list(&histedit_cmds);
7588 free(head_commit_id);
7589 free(base_commit_id);
7590 free(resume_commit_id);
7592 got_object_commit_close(commit);
7594 got_ref_close(branch);
7596 got_ref_close(tmp_branch);
7598 got_worktree_close(worktree);
7600 got_repo_close(repo);
7605 usage_integrate(void)
7607 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7611 static const struct got_error *
7612 cmd_integrate(int argc, char *argv[])
7614 const struct got_error *error = NULL;
7615 struct got_repository *repo = NULL;
7616 struct got_worktree *worktree = NULL;
7617 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7618 const char *branch_arg = NULL;
7619 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7620 struct got_fileindex *fileindex = NULL;
7621 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7622 int ch, did_something = 0;
7624 while ((ch = getopt(argc, argv, "")) != -1) {
7637 branch_arg = argv[0];
7639 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7640 "unveil", NULL) == -1)
7643 cwd = getcwd(NULL, 0);
7645 error = got_error_from_errno("getcwd");
7649 error = got_worktree_open(&worktree, cwd);
7653 error = check_rebase_or_histedit_in_progress(worktree);
7657 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7662 error = apply_unveil(got_repo_get_path(repo), 0,
7663 got_worktree_get_root_path(worktree));
7667 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7668 error = got_error_from_errno("asprintf");
7672 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7673 &base_branch_ref, worktree, refname, repo);
7677 refname = strdup(got_ref_get_name(branch_ref));
7678 if (refname == NULL) {
7679 error = got_error_from_errno("strdup");
7680 got_worktree_integrate_abort(worktree, fileindex, repo,
7681 branch_ref, base_branch_ref);
7684 base_refname = strdup(got_ref_get_name(base_branch_ref));
7685 if (base_refname == NULL) {
7686 error = got_error_from_errno("strdup");
7687 got_worktree_integrate_abort(worktree, fileindex, repo,
7688 branch_ref, base_branch_ref);
7692 error = got_ref_resolve(&commit_id, repo, branch_ref);
7696 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7700 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7701 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7702 "specified branch has already been integrated");
7703 got_worktree_integrate_abort(worktree, fileindex, repo,
7704 branch_ref, base_branch_ref);
7708 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7710 if (error->code == GOT_ERR_ANCESTRY)
7711 error = got_error(GOT_ERR_REBASE_REQUIRED);
7712 got_worktree_integrate_abort(worktree, fileindex, repo,
7713 branch_ref, base_branch_ref);
7717 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7718 branch_ref, base_branch_ref, update_progress, &did_something,
7719 check_cancelled, NULL);
7723 printf("Integrated %s into %s\n", refname, base_refname);
7726 got_repo_close(repo);
7728 got_worktree_close(worktree);
7730 free(base_commit_id);
7740 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7741 "[file-path ...]\n",
7746 static const struct got_error *
7747 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7748 const char *path, struct got_object_id *blob_id,
7749 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7750 int dirfd, const char *de_name)
7752 const struct got_error *err = NULL;
7753 char *id_str = NULL;
7755 if (staged_status != GOT_STATUS_ADD &&
7756 staged_status != GOT_STATUS_MODIFY &&
7757 staged_status != GOT_STATUS_DELETE)
7760 if (staged_status == GOT_STATUS_ADD ||
7761 staged_status == GOT_STATUS_MODIFY)
7762 err = got_object_id_str(&id_str, staged_blob_id);
7764 err = got_object_id_str(&id_str, blob_id);
7768 printf("%s %c %s\n", id_str, staged_status, path);
7773 static const struct got_error *
7774 cmd_stage(int argc, char *argv[])
7776 const struct got_error *error = NULL;
7777 struct got_repository *repo = NULL;
7778 struct got_worktree *worktree = NULL;
7780 struct got_pathlist_head paths;
7781 struct got_pathlist_entry *pe;
7782 int ch, list_stage = 0, pflag = 0;
7783 FILE *patch_script_file = NULL;
7784 const char *patch_script_path = NULL;
7785 struct choose_patch_arg cpa;
7789 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7798 patch_script_path = optarg;
7810 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7811 "unveil", NULL) == -1)
7814 if (list_stage && (pflag || patch_script_path))
7815 errx(1, "-l option cannot be used with other options");
7816 if (patch_script_path && !pflag)
7817 errx(1, "-F option can only be used together with -p option");
7819 cwd = getcwd(NULL, 0);
7821 error = got_error_from_errno("getcwd");
7825 error = got_worktree_open(&worktree, cwd);
7829 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7834 if (patch_script_path) {
7835 patch_script_file = fopen(patch_script_path, "r");
7836 if (patch_script_file == NULL) {
7837 error = got_error_from_errno2("fopen",
7842 error = apply_unveil(got_repo_get_path(repo), 0,
7843 got_worktree_get_root_path(worktree));
7847 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7852 error = got_worktree_status(worktree, &paths, repo,
7853 print_stage, NULL, check_cancelled, NULL);
7855 cpa.patch_script_file = patch_script_file;
7856 cpa.action = "stage";
7857 error = got_worktree_stage(worktree, &paths,
7858 pflag ? NULL : print_status, NULL,
7859 pflag ? choose_patch : NULL, &cpa, repo);
7862 if (patch_script_file && fclose(patch_script_file) == EOF &&
7864 error = got_error_from_errno2("fclose", patch_script_path);
7866 got_repo_close(repo);
7868 got_worktree_close(worktree);
7869 TAILQ_FOREACH(pe, &paths, entry)
7870 free((char *)pe->path);
7871 got_pathlist_free(&paths);
7879 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7880 "[file-path ...]\n",
7886 static const struct got_error *
7887 cmd_unstage(int argc, char *argv[])
7889 const struct got_error *error = NULL;
7890 struct got_repository *repo = NULL;
7891 struct got_worktree *worktree = NULL;
7893 struct got_pathlist_head paths;
7894 struct got_pathlist_entry *pe;
7895 int ch, did_something = 0, pflag = 0;
7896 FILE *patch_script_file = NULL;
7897 const char *patch_script_path = NULL;
7898 struct choose_patch_arg cpa;
7902 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7908 patch_script_path = optarg;
7920 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7921 "unveil", NULL) == -1)
7924 if (patch_script_path && !pflag)
7925 errx(1, "-F option can only be used together with -p option");
7927 cwd = getcwd(NULL, 0);
7929 error = got_error_from_errno("getcwd");
7933 error = got_worktree_open(&worktree, cwd);
7937 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7942 if (patch_script_path) {
7943 patch_script_file = fopen(patch_script_path, "r");
7944 if (patch_script_file == NULL) {
7945 error = got_error_from_errno2("fopen",
7951 error = apply_unveil(got_repo_get_path(repo), 0,
7952 got_worktree_get_root_path(worktree));
7956 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7960 cpa.patch_script_file = patch_script_file;
7961 cpa.action = "unstage";
7962 error = got_worktree_unstage(worktree, &paths, update_progress,
7963 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7965 if (patch_script_file && fclose(patch_script_file) == EOF &&
7967 error = got_error_from_errno2("fclose", patch_script_path);
7969 got_repo_close(repo);
7971 got_worktree_close(worktree);
7972 TAILQ_FOREACH(pe, &paths, entry)
7973 free((char *)pe->path);
7974 got_pathlist_free(&paths);
7982 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7983 "arg1 [arg2 ...]\n", getprogname());
7987 static const struct got_error *
7988 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7990 const struct got_error *err;
7991 struct got_blob_object *blob;
7993 err = got_object_open_as_blob(&blob, repo, id, 8192);
7997 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7998 got_object_blob_close(blob);
8002 static const struct got_error *
8003 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8005 const struct got_error *err;
8006 struct got_tree_object *tree;
8009 err = got_object_open_as_tree(&tree, repo, id);
8013 nentries = got_object_tree_get_nentries(tree);
8014 for (i = 0; i < nentries; i++) {
8015 struct got_tree_entry *te;
8017 if (sigint_received || sigpipe_received)
8019 te = got_object_tree_get_entry(tree, i);
8020 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8023 fprintf(outfile, "%s %.7o %s\n", id_str,
8024 got_tree_entry_get_mode(te),
8025 got_tree_entry_get_name(te));
8029 got_object_tree_close(tree);
8033 static const struct got_error *
8034 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8036 const struct got_error *err;
8037 struct got_commit_object *commit;
8038 const struct got_object_id_queue *parent_ids;
8039 struct got_object_qid *pid;
8040 char *id_str = NULL;
8041 const char *logmsg = NULL;
8043 err = got_object_open_as_commit(&commit, repo, id);
8047 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8051 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8052 parent_ids = got_object_commit_get_parent_ids(commit);
8053 fprintf(outfile, "numparents %d\n",
8054 got_object_commit_get_nparents(commit));
8055 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8057 err = got_object_id_str(&pid_str, pid->id);
8060 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8063 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8064 got_object_commit_get_author(commit),
8065 got_object_commit_get_author_time(commit));
8067 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8068 got_object_commit_get_author(commit),
8069 got_object_commit_get_committer_time(commit));
8071 logmsg = got_object_commit_get_logmsg_raw(commit);
8072 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8073 fprintf(outfile, "%s", logmsg);
8076 got_object_commit_close(commit);
8080 static const struct got_error *
8081 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8083 const struct got_error *err;
8084 struct got_tag_object *tag;
8085 char *id_str = NULL;
8086 const char *tagmsg = NULL;
8088 err = got_object_open_as_tag(&tag, repo, id);
8092 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8096 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8098 switch (got_object_tag_get_object_type(tag)) {
8099 case GOT_OBJ_TYPE_BLOB:
8100 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8101 GOT_OBJ_LABEL_BLOB);
8103 case GOT_OBJ_TYPE_TREE:
8104 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8105 GOT_OBJ_LABEL_TREE);
8107 case GOT_OBJ_TYPE_COMMIT:
8108 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8109 GOT_OBJ_LABEL_COMMIT);
8111 case GOT_OBJ_TYPE_TAG:
8112 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8119 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8120 got_object_tag_get_name(tag));
8122 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8123 got_object_tag_get_tagger(tag),
8124 got_object_tag_get_tagger_time(tag));
8126 tagmsg = got_object_tag_get_message(tag);
8127 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8128 fprintf(outfile, "%s", tagmsg);
8131 got_object_tag_close(tag);
8135 static const struct got_error *
8136 cmd_cat(int argc, char *argv[])
8138 const struct got_error *error;
8139 struct got_repository *repo = NULL;
8140 struct got_worktree *worktree = NULL;
8141 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8142 const char *commit_id_str = NULL;
8143 struct got_object_id *id = NULL, *commit_id = NULL;
8144 int ch, obj_type, i, force_path = 0;
8147 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8152 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8155 commit_id_str = optarg;
8158 repo_path = realpath(optarg, NULL);
8159 if (repo_path == NULL)
8160 return got_error_from_errno2("realpath",
8162 got_path_strip_trailing_slashes(repo_path);
8176 cwd = getcwd(NULL, 0);
8178 error = got_error_from_errno("getcwd");
8181 error = got_worktree_open(&worktree, cwd);
8182 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8185 if (repo_path == NULL) {
8187 got_worktree_get_repo_path(worktree));
8188 if (repo_path == NULL) {
8189 error = got_error_from_errno("strdup");
8195 if (repo_path == NULL) {
8196 repo_path = getcwd(NULL, 0);
8197 if (repo_path == NULL)
8198 return got_error_from_errno("getcwd");
8201 error = got_repo_open(&repo, repo_path, NULL);
8206 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8210 if (commit_id_str == NULL)
8211 commit_id_str = GOT_REF_HEAD;
8212 error = got_repo_match_object_id(&commit_id, NULL,
8213 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8217 for (i = 0; i < argc; i++) {
8219 error = got_object_id_by_path(&id, repo, commit_id,
8224 error = got_repo_match_object_id(&id, &label, argv[i],
8225 GOT_OBJ_TYPE_ANY, 0, repo);
8227 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8228 error->code != GOT_ERR_NOT_REF)
8230 error = got_object_id_by_path(&id, repo,
8231 commit_id, argv[i]);
8237 error = got_object_get_type(&obj_type, repo, id);
8242 case GOT_OBJ_TYPE_BLOB:
8243 error = cat_blob(id, repo, stdout);
8245 case GOT_OBJ_TYPE_TREE:
8246 error = cat_tree(id, repo, stdout);
8248 case GOT_OBJ_TYPE_COMMIT:
8249 error = cat_commit(id, repo, stdout);
8251 case GOT_OBJ_TYPE_TAG:
8252 error = cat_tag(id, repo, stdout);
8255 error = got_error(GOT_ERR_OBJ_TYPE);
8270 got_worktree_close(worktree);
8272 const struct got_error *repo_error;
8273 repo_error = got_repo_close(repo);