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, "%s:%s", host, server_path) == -1) {
966 error = got_error_from_errno("asprintf");
969 } else if (strcmp(proto, "http") == 0 ||
970 strcmp(proto, "git+http") == 0) {
971 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
974 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
977 if (dirname == NULL) {
978 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
979 error = got_error_from_errno("asprintf");
982 repo_path = default_destdir;
986 error = got_path_mkdir(repo_path);
990 error = got_repo_init(repo_path);
994 error = got_repo_open(&repo, repo_path, NULL);
998 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
999 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1000 error = got_error_from_errno2("unveil",
1001 GOT_FETCH_PATH_SSH);
1005 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1009 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1015 printf("Connected to %s:%s\n", host, port);
1017 /* Create a config file git-fetch(1) can understand. */
1018 gitconfig_path = got_repo_get_path_gitconfig(repo);
1019 if (gitconfig_path == NULL) {
1020 error = got_error_from_errno("got_repo_get_path_gitconfig");
1023 gitconfig_file = fopen(gitconfig_path, "a");
1024 if (gitconfig_file == NULL) {
1025 error = got_error_from_errno2("fopen", gitconfig_path);
1028 if (mirror_references) {
1029 if (asprintf(&gitconfig,
1032 "\tmirror = true\n",
1033 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1034 error = got_error_from_errno("asprintf");
1038 if (asprintf(&gitconfig,
1041 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1042 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1043 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1044 error = got_error_from_errno("asprintf");
1048 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1049 if (n != strlen(gitconfig)) {
1050 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1054 fpa.last_scaled_size[0] = '\0';
1055 fpa.last_p_indexed = -1;
1056 fpa.last_p_resolved = -1;
1057 fpa.verbosity = verbosity;
1058 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1059 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1060 fetchfd, repo, fetch_progress, &fpa);
1064 error = got_object_id_str(&id_str, pack_hash);
1068 printf("\nFetched %s.pack\n", id_str);
1071 /* Set up references provided with the pack file. */
1072 TAILQ_FOREACH(pe, &refs, entry) {
1073 const char *refname = pe->path;
1074 struct got_object_id *id = pe->data;
1075 struct got_reference *ref;
1076 char *remote_refname;
1078 error = got_ref_alloc(&ref, refname, id);
1081 error = got_ref_write(ref, repo);
1086 if (mirror_references)
1089 if (strncmp("refs/heads/", refname, 11) != 0)
1092 if (asprintf(&remote_refname,
1093 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1094 refname + 11) == -1) {
1095 error = got_error_from_errno("asprintf");
1098 error = got_ref_alloc(&ref, remote_refname, id);
1101 error = got_ref_write(ref, repo);
1107 /* Set the HEAD reference if the server provided one. */
1108 TAILQ_FOREACH(pe, &symrefs, entry) {
1109 struct got_reference *symref, *target_ref;
1110 const char *refname = pe->path;
1111 const char *target = pe->data;
1113 if (strcmp(refname, GOT_REF_HEAD) != 0)
1116 error = got_ref_open(&target_ref, repo, target, 0);
1118 if (error->code == GOT_ERR_NOT_REF)
1123 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1124 got_ref_close(target_ref);
1129 printf("Setting %s to %s\n", GOT_REF_HEAD,
1130 got_ref_get_symref_target(symref));
1132 error = got_ref_write(symref, repo);
1133 got_ref_close(symref);
1138 printf("Created %s repository '%s'\n",
1139 mirror_references ? "mirrored" : "cloned", repo_path);
1141 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1142 error = got_error_from_errno("close");
1143 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1144 error = got_error_from_errno("fclose");
1146 got_repo_close(repo);
1147 TAILQ_FOREACH(pe, &refs, entry) {
1148 free((void *)pe->path);
1151 got_pathlist_free(&refs);
1152 TAILQ_FOREACH(pe, &symrefs, entry) {
1153 free((void *)pe->path);
1156 got_pathlist_free(&symrefs);
1163 free(default_destdir);
1164 free(gitconfig_path);
1169 static const struct got_error *
1170 create_ref(const char *refname, struct got_object_id *id,
1171 const char *id_str, struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 struct got_reference *ref;
1176 printf("Creating %s: %s\n", refname, id_str);
1178 err = got_ref_alloc(&ref, refname, id);
1182 err = got_ref_write(ref, repo);
1187 static const struct got_error *
1188 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1189 struct got_repository *repo)
1191 const struct got_error *err = NULL;
1192 char *new_id_str = NULL;
1193 struct got_object_id *old_id = NULL;
1195 err = got_object_id_str(&new_id_str, new_id);
1199 if (got_ref_is_symbolic(ref)) {
1200 struct got_reference *new_ref;
1201 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1204 printf("Deleting symbolic reference %s -> %s\n",
1205 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1206 err = got_ref_delete(ref, repo);
1209 printf("Setting %s to %s\n", got_ref_get_name(ref),
1211 err = got_ref_write(new_ref, repo);
1215 err = got_ref_resolve(&old_id, repo, ref);
1218 if (got_object_id_cmp(old_id, new_id) != 0) {
1219 printf("Setting %s to %s\n",
1220 got_ref_get_name(ref), new_id_str);
1221 err = got_ref_change_ref(ref, new_id);
1224 err = got_ref_write(ref, repo);
1238 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1239 "[remote-repository-name]\n", getprogname());
1243 static const struct got_error *
1244 cmd_fetch(int argc, char *argv[])
1246 const struct got_error *error = NULL;
1247 char *cwd = NULL, *repo_path = NULL;
1248 const char *remote_name;
1249 char *proto = NULL, *host = NULL, *port = NULL;
1250 char *repo_name = NULL, *server_path = NULL;
1251 struct got_remote_repo *remotes, *remote = NULL;
1253 char *id_str = NULL;
1254 struct got_repository *repo = NULL;
1255 struct got_worktree *worktree = NULL;
1256 struct got_pathlist_head refs, symrefs;
1257 struct got_pathlist_entry *pe;
1258 struct got_object_id *pack_hash = NULL;
1259 int i, ch, fetchfd = -1;
1260 struct got_fetch_progress_arg fpa;
1264 TAILQ_INIT(&symrefs);
1266 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1269 repo_path = realpath(optarg, NULL);
1270 if (repo_path == NULL)
1271 return got_error_from_errno2("realpath",
1273 got_path_strip_trailing_slashes(repo_path);
1278 else if (verbosity < 3)
1293 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1295 remote_name = argv[0];
1299 cwd = getcwd(NULL, 0);
1301 error = got_error_from_errno("getcwd");
1305 if (repo_path == NULL) {
1306 error = got_worktree_open(&worktree, cwd);
1307 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1313 strdup(got_worktree_get_repo_path(worktree));
1314 if (repo_path == NULL)
1315 error = got_error_from_errno("strdup");
1319 repo_path = strdup(cwd);
1320 if (repo_path == NULL) {
1321 error = got_error_from_errno("strdup");
1327 error = got_repo_open(&repo, repo_path, NULL);
1331 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1332 for (i = 0; i < nremotes; i++) {
1333 remote = &remotes[i];
1334 if (strcmp(remote->name, remote_name) == 0)
1337 if (i == nremotes) {
1338 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1342 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1343 &repo_name, remote->url);
1347 if (strcmp(proto, "git") == 0) {
1349 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1350 "sendfd dns inet unveil", NULL) == -1)
1353 } else if (strcmp(proto, "git+ssh") == 0 ||
1354 strcmp(proto, "ssh") == 0) {
1356 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1357 "sendfd unveil", NULL) == -1)
1360 } else if (strcmp(proto, "http") == 0 ||
1361 strcmp(proto, "git+http") == 0) {
1362 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1365 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1369 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1370 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1371 error = got_error_from_errno2("unveil",
1372 GOT_FETCH_PATH_SSH);
1376 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1380 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1386 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1388 fpa.last_scaled_size[0] = '\0';
1389 fpa.last_p_indexed = -1;
1390 fpa.last_p_resolved = -1;
1391 fpa.verbosity = verbosity;
1392 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1393 remote->mirror_references, fetchfd, repo, fetch_progress, &fpa);
1397 if (pack_hash == NULL) {
1399 printf("Already up-to-date\n");
1403 if (verbosity >= 0) {
1404 error = got_object_id_str(&id_str, pack_hash);
1407 printf("\nFetched %s.pack\n", id_str);
1412 /* Update references provided with the pack file. */
1413 TAILQ_FOREACH(pe, &refs, entry) {
1414 const char *refname = pe->path;
1415 struct got_object_id *id = pe->data;
1416 struct got_reference *ref;
1417 char *remote_refname;
1419 error = got_object_id_str(&id_str, id);
1423 if (strncmp("refs/tags/", refname, 10) == 0) {
1424 error = got_ref_open(&ref, repo, refname, 0);
1426 if (error->code != GOT_ERR_NOT_REF)
1428 error = create_ref(refname, id, id_str, repo);
1432 error = update_ref(ref, id, repo);
1437 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1438 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1439 remote_name, refname + 11) == -1) {
1440 error = got_error_from_errno("asprintf");
1444 error = got_ref_open(&ref, repo, remote_refname, 0);
1446 if (error->code != GOT_ERR_NOT_REF)
1448 error = create_ref(remote_refname, id, id_str,
1453 error = update_ref(ref, id, repo);
1463 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1464 error = got_error_from_errno("close");
1466 got_repo_close(repo);
1468 got_worktree_close(worktree);
1469 TAILQ_FOREACH(pe, &refs, entry) {
1470 free((void *)pe->path);
1473 got_pathlist_free(&refs);
1474 TAILQ_FOREACH(pe, &symrefs, entry) {
1475 free((void *)pe->path);
1478 got_pathlist_free(&symrefs);
1493 usage_checkout(void)
1495 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1496 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1501 show_worktree_base_ref_warning(void)
1503 fprintf(stderr, "%s: warning: could not create a reference "
1504 "to the work tree's base commit; the commit could be "
1505 "garbage-collected by Git; making the repository "
1506 "writable and running 'got update' will prevent this\n",
1510 struct got_checkout_progress_arg {
1511 const char *worktree_path;
1512 int had_base_commit_ref_error;
1515 static const struct got_error *
1516 checkout_progress(void *arg, unsigned char status, const char *path)
1518 struct got_checkout_progress_arg *a = arg;
1520 /* Base commit bump happens silently. */
1521 if (status == GOT_STATUS_BUMP_BASE)
1524 if (status == GOT_STATUS_BASE_REF_ERR) {
1525 a->had_base_commit_ref_error = 1;
1529 while (path[0] == '/')
1532 printf("%c %s/%s\n", status, a->worktree_path, path);
1536 static const struct got_error *
1537 check_cancelled(void *arg)
1539 if (sigint_received || sigpipe_received)
1540 return got_error(GOT_ERR_CANCELLED);
1544 static const struct got_error *
1545 check_linear_ancestry(struct got_object_id *commit_id,
1546 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1547 struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_object_id *yca_id;
1552 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1553 commit_id, base_commit_id, repo, check_cancelled, NULL);
1558 return got_error(GOT_ERR_ANCESTRY);
1561 * Require a straight line of history between the target commit
1562 * and the work tree's base commit.
1564 * Non-linear situations such as this require a rebase:
1566 * (commit) D F (base_commit)
1574 * 'got update' only handles linear cases:
1575 * Update forwards in time: A (base/yca) - B - C - D (commit)
1576 * Update backwards in time: D (base) - C - B - A (commit/yca)
1578 if (allow_forwards_in_time_only) {
1579 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1580 return got_error(GOT_ERR_ANCESTRY);
1581 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1582 got_object_id_cmp(base_commit_id, yca_id) != 0)
1583 return got_error(GOT_ERR_ANCESTRY);
1589 static const struct got_error *
1590 check_same_branch(struct got_object_id *commit_id,
1591 struct got_reference *head_ref, struct got_object_id *yca_id,
1592 struct got_repository *repo)
1594 const struct got_error *err = NULL;
1595 struct got_commit_graph *graph = NULL;
1596 struct got_object_id *head_commit_id = NULL;
1597 int is_same_branch = 0;
1599 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1603 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1607 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1612 err = got_commit_graph_open(&graph, "/", 1);
1616 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1617 check_cancelled, NULL);
1622 struct got_object_id *id;
1623 err = got_commit_graph_iter_next(&id, graph, repo,
1624 check_cancelled, NULL);
1626 if (err->code == GOT_ERR_ITER_COMPLETED)
1632 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1634 if (got_object_id_cmp(id, commit_id) == 0) {
1642 got_commit_graph_close(graph);
1643 free(head_commit_id);
1644 if (!err && !is_same_branch)
1645 err = got_error(GOT_ERR_ANCESTRY);
1649 static const struct got_error *
1650 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1652 static char msg[512];
1653 const char *branch_name;
1655 if (got_ref_is_symbolic(ref))
1656 branch_name = got_ref_get_symref_target(ref);
1658 branch_name = got_ref_get_name(ref);
1660 if (strncmp("refs/heads/", branch_name, 11) == 0)
1663 snprintf(msg, sizeof(msg),
1664 "target commit is not contained in branch '%s'; "
1665 "the branch to use must be specified with -b; "
1666 "if necessary a new branch can be created for "
1667 "this commit with 'got branch -c %s BRANCH_NAME'",
1668 branch_name, commit_id_str);
1670 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1673 static const struct got_error *
1674 cmd_checkout(int argc, char *argv[])
1676 const struct got_error *error = NULL;
1677 struct got_repository *repo = NULL;
1678 struct got_reference *head_ref = NULL;
1679 struct got_worktree *worktree = NULL;
1680 char *repo_path = NULL;
1681 char *worktree_path = NULL;
1682 const char *path_prefix = "";
1683 const char *branch_name = GOT_REF_HEAD;
1684 char *commit_id_str = NULL;
1685 int ch, same_path_prefix, allow_nonempty = 0;
1686 struct got_pathlist_head paths;
1687 struct got_checkout_progress_arg cpa;
1691 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1694 branch_name = optarg;
1697 commit_id_str = strdup(optarg);
1698 if (commit_id_str == NULL)
1699 return got_error_from_errno("strdup");
1705 path_prefix = optarg;
1717 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1718 "unveil", NULL) == -1)
1722 char *cwd, *base, *dotgit;
1723 repo_path = realpath(argv[0], NULL);
1724 if (repo_path == NULL)
1725 return got_error_from_errno2("realpath", argv[0]);
1726 cwd = getcwd(NULL, 0);
1728 error = got_error_from_errno("getcwd");
1731 if (path_prefix[0]) {
1732 base = basename(path_prefix);
1734 error = got_error_from_errno2("basename",
1739 base = basename(repo_path);
1741 error = got_error_from_errno2("basename",
1746 dotgit = strstr(base, ".git");
1749 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1750 error = got_error_from_errno("asprintf");
1755 } else if (argc == 2) {
1756 repo_path = realpath(argv[0], NULL);
1757 if (repo_path == NULL) {
1758 error = got_error_from_errno2("realpath", argv[0]);
1761 worktree_path = realpath(argv[1], NULL);
1762 if (worktree_path == NULL) {
1763 if (errno != ENOENT) {
1764 error = got_error_from_errno2("realpath",
1768 worktree_path = strdup(argv[1]);
1769 if (worktree_path == NULL) {
1770 error = got_error_from_errno("strdup");
1777 got_path_strip_trailing_slashes(repo_path);
1778 got_path_strip_trailing_slashes(worktree_path);
1780 error = got_repo_open(&repo, repo_path, NULL);
1784 /* Pre-create work tree path for unveil(2) */
1785 error = got_path_mkdir(worktree_path);
1787 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1788 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1790 if (!allow_nonempty &&
1791 !got_path_dir_is_empty(worktree_path)) {
1792 error = got_error_path(worktree_path,
1793 GOT_ERR_DIR_NOT_EMPTY);
1798 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1802 error = got_ref_open(&head_ref, repo, branch_name, 0);
1806 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1807 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1810 error = got_worktree_open(&worktree, worktree_path);
1814 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1818 if (!same_path_prefix) {
1819 error = got_error(GOT_ERR_PATH_PREFIX);
1823 if (commit_id_str) {
1824 struct got_object_id *commit_id;
1825 error = got_repo_match_object_id(&commit_id, NULL,
1826 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1829 error = check_linear_ancestry(commit_id,
1830 got_worktree_get_base_commit_id(worktree), 0, repo);
1831 if (error != NULL) {
1833 if (error->code == GOT_ERR_ANCESTRY) {
1834 error = checkout_ancestry_error(
1835 head_ref, commit_id_str);
1839 error = check_same_branch(commit_id, head_ref, NULL, repo);
1841 if (error->code == GOT_ERR_ANCESTRY) {
1842 error = checkout_ancestry_error(
1843 head_ref, commit_id_str);
1847 error = got_worktree_set_base_commit_id(worktree, repo,
1854 error = got_pathlist_append(&paths, "", NULL);
1857 cpa.worktree_path = worktree_path;
1858 cpa.had_base_commit_ref_error = 0;
1859 error = got_worktree_checkout_files(worktree, &paths, repo,
1860 checkout_progress, &cpa, check_cancelled, NULL);
1864 printf("Now shut up and hack\n");
1865 if (cpa.had_base_commit_ref_error)
1866 show_worktree_base_ref_warning();
1868 got_pathlist_free(&paths);
1869 free(commit_id_str);
1871 free(worktree_path);
1878 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1883 static const struct got_error *
1884 update_progress(void *arg, unsigned char status, const char *path)
1886 int *did_something = arg;
1888 if (status == GOT_STATUS_EXISTS ||
1889 status == GOT_STATUS_BASE_REF_ERR)
1894 /* Base commit bump happens silently. */
1895 if (status == GOT_STATUS_BUMP_BASE)
1898 while (path[0] == '/')
1900 printf("%c %s\n", status, path);
1904 static const struct got_error *
1905 switch_head_ref(struct got_reference *head_ref,
1906 struct got_object_id *commit_id, struct got_worktree *worktree,
1907 struct got_repository *repo)
1909 const struct got_error *err = NULL;
1911 int ref_has_moved = 0;
1913 /* Trivial case: switching between two different references. */
1914 if (strcmp(got_ref_get_name(head_ref),
1915 got_worktree_get_head_ref_name(worktree)) != 0) {
1916 printf("Switching work tree from %s to %s\n",
1917 got_worktree_get_head_ref_name(worktree),
1918 got_ref_get_name(head_ref));
1919 return got_worktree_set_head_ref(worktree, head_ref);
1922 err = check_linear_ancestry(commit_id,
1923 got_worktree_get_base_commit_id(worktree), 0, repo);
1925 if (err->code != GOT_ERR_ANCESTRY)
1932 /* Switching to a rebased branch with the same reference name. */
1933 err = got_object_id_str(&base_id_str,
1934 got_worktree_get_base_commit_id(worktree));
1937 printf("Reference %s now points at a different branch\n",
1938 got_worktree_get_head_ref_name(worktree));
1939 printf("Switching work tree from %s to %s\n", base_id_str,
1940 got_worktree_get_head_ref_name(worktree));
1944 static const struct got_error *
1945 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1947 const struct got_error *err;
1950 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1954 return got_error(GOT_ERR_REBASING);
1956 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1960 return got_error(GOT_ERR_HISTEDIT_BUSY);
1965 static const struct got_error *
1966 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1967 char *argv[], struct got_worktree *worktree)
1969 const struct got_error *err = NULL;
1976 return got_error_from_errno("strdup");
1977 return got_pathlist_append(paths, path, NULL);
1980 for (i = 0; i < argc; i++) {
1981 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1984 err = got_pathlist_append(paths, path, NULL);
1994 static const struct got_error *
1995 cmd_update(int argc, char *argv[])
1997 const struct got_error *error = NULL;
1998 struct got_repository *repo = NULL;
1999 struct got_worktree *worktree = NULL;
2000 char *worktree_path = NULL;
2001 struct got_object_id *commit_id = NULL;
2002 char *commit_id_str = NULL;
2003 const char *branch_name = NULL;
2004 struct got_reference *head_ref = NULL;
2005 struct got_pathlist_head paths;
2006 struct got_pathlist_entry *pe;
2007 int ch, did_something = 0;
2011 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2014 branch_name = optarg;
2017 commit_id_str = strdup(optarg);
2018 if (commit_id_str == NULL)
2019 return got_error_from_errno("strdup");
2031 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2032 "unveil", NULL) == -1)
2035 worktree_path = getcwd(NULL, 0);
2036 if (worktree_path == NULL) {
2037 error = got_error_from_errno("getcwd");
2040 error = got_worktree_open(&worktree, worktree_path);
2044 error = check_rebase_or_histedit_in_progress(worktree);
2048 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2053 error = apply_unveil(got_repo_get_path(repo), 0,
2054 got_worktree_get_root_path(worktree));
2058 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2062 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2063 got_worktree_get_head_ref_name(worktree), 0);
2066 if (commit_id_str == NULL) {
2067 error = got_ref_resolve(&commit_id, repo, head_ref);
2070 error = got_object_id_str(&commit_id_str, commit_id);
2074 error = got_repo_match_object_id(&commit_id, NULL,
2075 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2076 free(commit_id_str);
2077 commit_id_str = NULL;
2080 error = got_object_id_str(&commit_id_str, commit_id);
2086 struct got_object_id *head_commit_id;
2087 TAILQ_FOREACH(pe, &paths, entry) {
2088 if (pe->path_len == 0)
2090 error = got_error_msg(GOT_ERR_BAD_PATH,
2091 "switching between branches requires that "
2092 "the entire work tree gets updated");
2095 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2098 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2100 free(head_commit_id);
2103 error = check_same_branch(commit_id, head_ref, NULL, repo);
2106 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2110 error = check_linear_ancestry(commit_id,
2111 got_worktree_get_base_commit_id(worktree), 0, repo);
2112 if (error != NULL) {
2113 if (error->code == GOT_ERR_ANCESTRY)
2114 error = got_error(GOT_ERR_BRANCH_MOVED);
2117 error = check_same_branch(commit_id, head_ref, NULL, repo);
2122 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2124 error = got_worktree_set_base_commit_id(worktree, repo,
2130 error = got_worktree_checkout_files(worktree, &paths, repo,
2131 update_progress, &did_something, check_cancelled, NULL);
2136 printf("Updated to commit %s\n", commit_id_str);
2138 printf("Already up-to-date\n");
2140 free(worktree_path);
2141 TAILQ_FOREACH(pe, &paths, entry)
2142 free((char *)pe->path);
2143 got_pathlist_free(&paths);
2145 free(commit_id_str);
2149 static const struct got_error *
2150 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2151 const char *path, int diff_context, int ignore_whitespace,
2152 struct got_repository *repo)
2154 const struct got_error *err = NULL;
2155 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2158 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2163 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2167 while (path[0] == '/')
2169 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2170 ignore_whitespace, stdout);
2173 got_object_blob_close(blob1);
2174 got_object_blob_close(blob2);
2178 static const struct got_error *
2179 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2180 const char *path, int diff_context, int ignore_whitespace,
2181 struct got_repository *repo)
2183 const struct got_error *err = NULL;
2184 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2185 struct got_diff_blob_output_unidiff_arg arg;
2188 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2193 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2197 arg.diff_context = diff_context;
2198 arg.ignore_whitespace = ignore_whitespace;
2199 arg.outfile = stdout;
2200 while (path[0] == '/')
2202 err = got_diff_tree(tree1, tree2, path, path, repo,
2203 got_diff_blob_output_unidiff, &arg, 1);
2206 got_object_tree_close(tree1);
2208 got_object_tree_close(tree2);
2212 static const struct got_error *
2213 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2214 const char *path, int diff_context, struct got_repository *repo)
2216 const struct got_error *err = NULL;
2217 struct got_commit_object *pcommit = NULL;
2218 char *id_str1 = NULL, *id_str2 = NULL;
2219 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2220 struct got_object_qid *qid;
2222 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2224 err = got_object_open_as_commit(&pcommit, repo,
2230 if (path && path[0] != '\0') {
2232 err = got_object_id_by_path(&obj_id2, repo, id, path);
2235 err = got_object_id_str(&id_str2, obj_id2);
2241 err = got_object_id_by_path(&obj_id1, repo,
2247 err = got_object_id_str(&id_str1, obj_id1);
2253 err = got_object_get_type(&obj_type, repo, obj_id2);
2258 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2260 case GOT_OBJ_TYPE_BLOB:
2261 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2264 case GOT_OBJ_TYPE_TREE:
2265 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2269 err = got_error(GOT_ERR_OBJ_TYPE);
2275 obj_id2 = got_object_commit_get_tree_id(commit);
2276 err = got_object_id_str(&id_str2, obj_id2);
2279 obj_id1 = got_object_commit_get_tree_id(pcommit);
2280 err = got_object_id_str(&id_str1, obj_id1);
2283 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2284 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2290 got_object_commit_close(pcommit);
2295 get_datestr(time_t *time, char *datebuf)
2297 struct tm mytm, *tm;
2300 tm = gmtime_r(time, &mytm);
2303 s = asctime_r(tm, datebuf);
2306 p = strchr(s, '\n');
2312 static const struct got_error *
2313 match_logmsg(int *have_match, struct got_object_id *id,
2314 struct got_commit_object *commit, regex_t *regex)
2316 const struct got_error *err = NULL;
2317 regmatch_t regmatch;
2318 char *id_str = NULL, *logmsg = NULL;
2322 err = got_object_id_str(&id_str, id);
2326 err = got_object_commit_get_logmsg(&logmsg, commit);
2330 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2338 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2340 static const struct got_error *
2341 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2342 struct got_repository *repo, const char *path, int show_patch,
2343 int diff_context, struct got_reflist_head *refs)
2345 const struct got_error *err = NULL;
2346 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2348 time_t committer_time;
2349 const char *author, *committer;
2350 char *refs_str = NULL;
2351 struct got_reflist_entry *re;
2353 SIMPLEQ_FOREACH(re, refs, entry) {
2356 struct got_tag_object *tag = NULL;
2359 name = got_ref_get_name(re->ref);
2360 if (strcmp(name, GOT_REF_HEAD) == 0)
2362 if (strncmp(name, "refs/", 5) == 0)
2364 if (strncmp(name, "got/", 4) == 0)
2366 if (strncmp(name, "heads/", 6) == 0)
2368 if (strncmp(name, "remotes/", 8) == 0)
2370 if (strncmp(name, "tags/", 5) == 0) {
2371 err = got_object_open_as_tag(&tag, repo, re->id);
2373 if (err->code != GOT_ERR_OBJ_TYPE)
2375 /* Ref points at something other than a tag. */
2380 cmp = got_object_id_cmp(tag ?
2381 got_object_tag_get_object_id(tag) : re->id, id);
2383 got_object_tag_close(tag);
2387 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2389 err = got_error_from_errno("asprintf");
2395 err = got_object_id_str(&id_str, id);
2399 printf(GOT_COMMIT_SEP_STR);
2400 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2401 refs_str ? refs_str : "", refs_str ? ")" : "");
2406 printf("from: %s\n", got_object_commit_get_author(commit));
2407 committer_time = got_object_commit_get_committer_time(commit);
2408 datestr = get_datestr(&committer_time, datebuf);
2410 printf("date: %s UTC\n", datestr);
2411 author = got_object_commit_get_author(commit);
2412 committer = got_object_commit_get_committer(commit);
2413 if (strcmp(author, committer) != 0)
2414 printf("via: %s\n", committer);
2415 if (got_object_commit_get_nparents(commit) > 1) {
2416 const struct got_object_id_queue *parent_ids;
2417 struct got_object_qid *qid;
2419 parent_ids = got_object_commit_get_parent_ids(commit);
2420 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2421 err = got_object_id_str(&id_str, qid->id);
2424 printf("parent %d: %s\n", n++, id_str);
2429 err = got_object_commit_get_logmsg(&logmsg0, commit);
2435 line = strsep(&logmsg, "\n");
2437 printf(" %s\n", line);
2442 err = print_patch(commit, id, path, diff_context, repo);
2447 if (fflush(stdout) != 0 && err == NULL)
2448 err = got_error_from_errno("fflush");
2452 static const struct got_error *
2453 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2454 const char *path, int show_patch, const char *search_pattern,
2455 int diff_context, int limit, int log_branches,
2456 struct got_reflist_head *refs)
2458 const struct got_error *err;
2459 struct got_commit_graph *graph;
2463 if (search_pattern &&
2464 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2465 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2467 err = got_commit_graph_open(&graph, path, !log_branches);
2470 err = got_commit_graph_iter_start(graph, root_id, repo,
2471 check_cancelled, NULL);
2475 struct got_commit_object *commit;
2476 struct got_object_id *id;
2478 if (sigint_received || sigpipe_received)
2481 err = got_commit_graph_iter_next(&id, graph, repo,
2482 check_cancelled, NULL);
2484 if (err->code == GOT_ERR_ITER_COMPLETED)
2491 err = got_object_open_as_commit(&commit, repo, id);
2495 if (search_pattern) {
2496 err = match_logmsg(&have_match, id, commit, ®ex);
2498 got_object_commit_close(commit);
2501 if (have_match == 0) {
2502 got_object_commit_close(commit);
2507 err = print_commit(commit, id, repo, path, show_patch,
2508 diff_context, refs);
2509 got_object_commit_close(commit);
2510 if (err || (limit && --limit == 0))
2516 got_commit_graph_close(graph);
2523 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2524 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2529 get_default_log_limit(void)
2531 const char *got_default_log_limit;
2535 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2536 if (got_default_log_limit == NULL)
2538 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2544 static const struct got_error *
2545 cmd_log(int argc, char *argv[])
2547 const struct got_error *error;
2548 struct got_repository *repo = NULL;
2549 struct got_worktree *worktree = NULL;
2550 struct got_commit_object *commit = NULL;
2551 struct got_object_id *id = NULL;
2552 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2553 const char *start_commit = NULL, *search_pattern = NULL;
2554 int diff_context = -1, ch;
2555 int show_patch = 0, limit = 0, log_branches = 0;
2557 struct got_reflist_head refs;
2559 SIMPLEQ_INIT(&refs);
2562 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2568 limit = get_default_log_limit();
2570 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2576 start_commit = optarg;
2579 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2582 err(1, "-C option %s", errstr);
2585 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2587 err(1, "-l option %s", errstr);
2593 repo_path = realpath(optarg, NULL);
2594 if (repo_path == NULL)
2595 return got_error_from_errno2("realpath",
2597 got_path_strip_trailing_slashes(repo_path);
2600 search_pattern = optarg;
2611 if (diff_context == -1)
2613 else if (!show_patch)
2614 errx(1, "-C reguires -p");
2616 cwd = getcwd(NULL, 0);
2618 error = got_error_from_errno("getcwd");
2622 error = got_worktree_open(&worktree, cwd);
2623 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2630 error = got_error_from_errno("strdup");
2633 } else if (argc == 1) {
2635 error = got_worktree_resolve_path(&path, worktree,
2640 path = strdup(argv[0]);
2642 error = got_error_from_errno("strdup");
2649 if (repo_path == NULL) {
2650 repo_path = worktree ?
2651 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2653 if (repo_path == NULL) {
2654 error = got_error_from_errno("strdup");
2658 error = got_repo_open(&repo, repo_path, NULL);
2662 error = apply_unveil(got_repo_get_path(repo), 1,
2663 worktree ? got_worktree_get_root_path(worktree) : NULL);
2667 if (start_commit == NULL) {
2668 struct got_reference *head_ref;
2669 error = got_ref_open(&head_ref, repo,
2670 worktree ? got_worktree_get_head_ref_name(worktree)
2674 error = got_ref_resolve(&id, repo, head_ref);
2675 got_ref_close(head_ref);
2678 error = got_object_open_as_commit(&commit, repo, id);
2680 struct got_reference *ref;
2681 error = got_ref_open(&ref, repo, start_commit, 0);
2682 if (error == NULL) {
2684 error = got_ref_resolve(&id, repo, ref);
2688 error = got_object_get_type(&obj_type, repo, id);
2691 if (obj_type == GOT_OBJ_TYPE_TAG) {
2692 struct got_tag_object *tag;
2693 error = got_object_open_as_tag(&tag, repo, id);
2696 if (got_object_tag_get_object_type(tag) !=
2697 GOT_OBJ_TYPE_COMMIT) {
2698 got_object_tag_close(tag);
2699 error = got_error(GOT_ERR_OBJ_TYPE);
2703 id = got_object_id_dup(
2704 got_object_tag_get_object_id(tag));
2706 error = got_error_from_errno(
2707 "got_object_id_dup");
2708 got_object_tag_close(tag);
2711 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2712 error = got_error(GOT_ERR_OBJ_TYPE);
2715 error = got_object_open_as_commit(&commit, repo, id);
2719 if (commit == NULL) {
2720 error = got_repo_match_object_id_prefix(&id,
2721 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2730 const char *prefix = got_worktree_get_path_prefix(worktree);
2732 if (asprintf(&p, "%s%s%s", prefix,
2733 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2734 error = got_error_from_errno("asprintf");
2737 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2740 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2745 path = in_repo_path;
2748 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2752 error = print_commits(id, repo, path, show_patch, search_pattern,
2753 diff_context, limit, log_branches, &refs);
2760 got_worktree_close(worktree);
2762 const struct got_error *repo_error;
2763 repo_error = got_repo_close(repo);
2767 got_ref_list_free(&refs);
2774 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2775 "[-w] [object1 object2 | path]\n", getprogname());
2779 struct print_diff_arg {
2780 struct got_repository *repo;
2781 struct got_worktree *worktree;
2786 int ignore_whitespace;
2789 static const struct got_error *
2790 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2791 const char *path, struct got_object_id *blob_id,
2792 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2793 int dirfd, const char *de_name)
2795 struct print_diff_arg *a = arg;
2796 const struct got_error *err = NULL;
2797 struct got_blob_object *blob1 = NULL;
2800 char *abspath = NULL, *label1 = NULL;
2803 if (a->diff_staged) {
2804 if (staged_status != GOT_STATUS_MODIFY &&
2805 staged_status != GOT_STATUS_ADD &&
2806 staged_status != GOT_STATUS_DELETE)
2809 if (staged_status == GOT_STATUS_DELETE)
2811 if (status == GOT_STATUS_NONEXISTENT)
2812 return got_error_set_errno(ENOENT, path);
2813 if (status != GOT_STATUS_MODIFY &&
2814 status != GOT_STATUS_ADD &&
2815 status != GOT_STATUS_DELETE &&
2816 status != GOT_STATUS_CONFLICT)
2820 if (!a->header_shown) {
2821 printf("diff %s %s%s\n", a->id_str,
2822 got_worktree_get_root_path(a->worktree),
2823 a->diff_staged ? " (staged changes)" : "");
2824 a->header_shown = 1;
2827 if (a->diff_staged) {
2828 const char *label1 = NULL, *label2 = NULL;
2829 switch (staged_status) {
2830 case GOT_STATUS_MODIFY:
2834 case GOT_STATUS_ADD:
2837 case GOT_STATUS_DELETE:
2841 return got_error(GOT_ERR_FILE_STATUS);
2843 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2844 label1, label2, a->diff_context, a->ignore_whitespace,
2848 if (staged_status == GOT_STATUS_ADD ||
2849 staged_status == GOT_STATUS_MODIFY) {
2851 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2855 err = got_object_id_str(&id_str, staged_blob_id);
2858 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2859 err = got_error_from_errno("asprintf");
2864 } else if (status != GOT_STATUS_ADD) {
2865 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2870 if (status != GOT_STATUS_DELETE) {
2871 if (asprintf(&abspath, "%s/%s",
2872 got_worktree_get_root_path(a->worktree), path) == -1) {
2873 err = got_error_from_errno("asprintf");
2878 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2880 err = got_error_from_errno2("openat", abspath);
2884 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2886 err = got_error_from_errno2("open", abspath);
2890 if (fstat(fd, &sb) == -1) {
2891 err = got_error_from_errno2("fstat", abspath);
2894 f2 = fdopen(fd, "r");
2896 err = got_error_from_errno2("fdopen", abspath);
2903 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2904 a->diff_context, a->ignore_whitespace, stdout);
2907 got_object_blob_close(blob1);
2908 if (f2 && fclose(f2) == EOF && err == NULL)
2909 err = got_error_from_errno("fclose");
2910 if (fd != -1 && close(fd) == -1 && err == NULL)
2911 err = got_error_from_errno("close");
2916 static const struct got_error *
2917 cmd_diff(int argc, char *argv[])
2919 const struct got_error *error;
2920 struct got_repository *repo = NULL;
2921 struct got_worktree *worktree = NULL;
2922 char *cwd = NULL, *repo_path = NULL;
2923 struct got_object_id *id1 = NULL, *id2 = NULL;
2924 const char *id_str1 = NULL, *id_str2 = NULL;
2925 char *label1 = NULL, *label2 = NULL;
2927 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2932 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2937 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2940 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2943 err(1, "-C option %s", errstr);
2946 repo_path = realpath(optarg, NULL);
2947 if (repo_path == NULL)
2948 return got_error_from_errno2("realpath",
2950 got_path_strip_trailing_slashes(repo_path);
2956 ignore_whitespace = 1;
2967 cwd = getcwd(NULL, 0);
2969 error = got_error_from_errno("getcwd");
2972 error = got_worktree_open(&worktree, cwd);
2973 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2976 if (worktree == NULL) {
2977 error = got_error(GOT_ERR_NOT_WORKTREE);
2982 "-r option can't be used when diffing a work tree");
2983 repo_path = strdup(got_worktree_get_repo_path(worktree));
2984 if (repo_path == NULL) {
2985 error = got_error_from_errno("strdup");
2989 error = got_worktree_resolve_path(&path, worktree,
2996 error = got_error_from_errno("strdup");
3000 } else if (argc == 2) {
3002 errx(1, "-s option can't be used when diffing "
3003 "objects in repository");
3006 if (worktree && repo_path == NULL) {
3008 strdup(got_worktree_get_repo_path(worktree));
3009 if (repo_path == NULL) {
3010 error = got_error_from_errno("strdup");
3017 if (repo_path == NULL) {
3018 repo_path = getcwd(NULL, 0);
3019 if (repo_path == NULL)
3020 return got_error_from_errno("getcwd");
3023 error = got_repo_open(&repo, repo_path, NULL);
3028 error = apply_unveil(got_repo_get_path(repo), 1,
3029 worktree ? got_worktree_get_root_path(worktree) : NULL);
3034 struct print_diff_arg arg;
3035 struct got_pathlist_head paths;
3040 error = got_object_id_str(&id_str,
3041 got_worktree_get_base_commit_id(worktree));
3045 arg.worktree = worktree;
3046 arg.diff_context = diff_context;
3047 arg.id_str = id_str;
3048 arg.header_shown = 0;
3049 arg.diff_staged = diff_staged;
3050 arg.ignore_whitespace = ignore_whitespace;
3052 error = got_pathlist_append(&paths, path, NULL);
3056 error = got_worktree_status(worktree, &paths, repo, print_diff,
3057 &arg, check_cancelled, NULL);
3059 got_pathlist_free(&paths);
3063 error = got_repo_match_object_id(&id1, &label1, id_str1,
3064 GOT_OBJ_TYPE_ANY, 1, repo);
3068 error = got_repo_match_object_id(&id2, &label2, id_str2,
3069 GOT_OBJ_TYPE_ANY, 1, repo);
3073 error = got_object_get_type(&type1, repo, id1);
3077 error = got_object_get_type(&type2, repo, id2);
3081 if (type1 != type2) {
3082 error = got_error(GOT_ERR_OBJ_TYPE);
3087 case GOT_OBJ_TYPE_BLOB:
3088 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3089 diff_context, ignore_whitespace, repo, stdout);
3091 case GOT_OBJ_TYPE_TREE:
3092 error = got_diff_objects_as_trees(id1, id2, "", "",
3093 diff_context, ignore_whitespace, repo, stdout);
3095 case GOT_OBJ_TYPE_COMMIT:
3096 printf("diff %s %s\n", label1, label2);
3097 error = got_diff_objects_as_commits(id1, id2, diff_context,
3098 ignore_whitespace, repo, stdout);
3101 error = got_error(GOT_ERR_OBJ_TYPE);
3110 got_worktree_close(worktree);
3112 const struct got_error *repo_error;
3113 repo_error = got_repo_close(repo);
3124 "usage: %s blame [-c commit] [-r repository-path] path\n",
3133 char datebuf[11]; /* YYYY-MM-DD + NUL */
3136 struct blame_cb_args {
3137 struct blame_line *lines;
3141 off_t *line_offsets;
3143 struct got_repository *repo;
3146 static const struct got_error *
3147 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3149 const struct got_error *err = NULL;
3150 struct blame_cb_args *a = arg;
3151 struct blame_line *bline;
3153 size_t linesize = 0;
3154 struct got_commit_object *commit = NULL;
3157 time_t committer_time;
3159 if (nlines != a->nlines ||
3160 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3161 return got_error(GOT_ERR_RANGE);
3163 if (sigint_received)
3164 return got_error(GOT_ERR_ITER_COMPLETED);
3167 return NULL; /* no change in this commit */
3169 /* Annotate this line. */
3170 bline = &a->lines[lineno - 1];
3171 if (bline->annotated)
3173 err = got_object_id_str(&bline->id_str, id);
3177 err = got_object_open_as_commit(&commit, a->repo, id);
3181 bline->committer = strdup(got_object_commit_get_committer(commit));
3182 if (bline->committer == NULL) {
3183 err = got_error_from_errno("strdup");
3187 committer_time = got_object_commit_get_committer_time(commit);
3188 if (localtime_r(&committer_time, &tm) == NULL)
3189 return got_error_from_errno("localtime_r");
3190 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3191 &tm) >= sizeof(bline->datebuf)) {
3192 err = got_error(GOT_ERR_NO_SPACE);
3195 bline->annotated = 1;
3197 /* Print lines annotated so far. */
3198 bline = &a->lines[a->lineno_cur - 1];
3199 if (!bline->annotated)
3202 offset = a->line_offsets[a->lineno_cur - 1];
3203 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3204 err = got_error_from_errno("fseeko");
3208 while (bline->annotated) {
3209 char *smallerthan, *at, *nl, *committer;
3212 if (getline(&line, &linesize, a->f) == -1) {
3214 err = got_error_from_errno("getline");
3218 committer = bline->committer;
3219 smallerthan = strchr(committer, '<');
3220 if (smallerthan && smallerthan[1] != '\0')
3221 committer = smallerthan + 1;
3222 at = strchr(committer, '@');
3225 len = strlen(committer);
3227 committer[8] = '\0';
3229 nl = strchr(line, '\n');
3232 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3233 bline->id_str, bline->datebuf, committer, line);
3236 bline = &a->lines[a->lineno_cur - 1];
3240 got_object_commit_close(commit);
3245 static const struct got_error *
3246 cmd_blame(int argc, char *argv[])
3248 const struct got_error *error;
3249 struct got_repository *repo = NULL;
3250 struct got_worktree *worktree = NULL;
3251 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3252 struct got_object_id *obj_id = NULL;
3253 struct got_object_id *commit_id = NULL;
3254 struct got_blob_object *blob = NULL;
3255 char *commit_id_str = NULL;
3256 struct blame_cb_args bca;
3257 int ch, obj_type, i;
3260 memset(&bca, 0, sizeof(bca));
3263 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3268 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3271 commit_id_str = optarg;
3274 repo_path = realpath(optarg, NULL);
3275 if (repo_path == NULL)
3276 return got_error_from_errno2("realpath",
3278 got_path_strip_trailing_slashes(repo_path);
3294 cwd = getcwd(NULL, 0);
3296 error = got_error_from_errno("getcwd");
3299 if (repo_path == NULL) {
3300 error = got_worktree_open(&worktree, cwd);
3301 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3307 strdup(got_worktree_get_repo_path(worktree));
3308 if (repo_path == NULL)
3309 error = got_error_from_errno("strdup");
3313 repo_path = strdup(cwd);
3314 if (repo_path == NULL) {
3315 error = got_error_from_errno("strdup");
3321 error = got_repo_open(&repo, repo_path, NULL);
3325 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3330 const char *prefix = got_worktree_get_path_prefix(worktree);
3331 char *p, *worktree_subdir = cwd +
3332 strlen(got_worktree_get_root_path(worktree));
3333 if (asprintf(&p, "%s%s%s%s%s",
3334 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3335 worktree_subdir, worktree_subdir[0] ? "/" : "",
3337 error = got_error_from_errno("asprintf");
3340 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3343 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3348 if (commit_id_str == NULL) {
3349 struct got_reference *head_ref;
3350 error = got_ref_open(&head_ref, repo, worktree ?
3351 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3354 error = got_ref_resolve(&commit_id, repo, head_ref);
3355 got_ref_close(head_ref);
3359 error = got_repo_match_object_id(&commit_id, NULL,
3360 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3365 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3369 error = got_object_get_type(&obj_type, repo, obj_id);
3373 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3374 error = got_error(GOT_ERR_OBJ_TYPE);
3378 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3381 bca.f = got_opentemp();
3382 if (bca.f == NULL) {
3383 error = got_error_from_errno("got_opentemp");
3386 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3387 &bca.line_offsets, bca.f, blob);
3388 if (error || bca.nlines == 0)
3391 /* Don't include \n at EOF in the blame line count. */
3392 if (bca.line_offsets[bca.nlines - 1] == filesize)
3395 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3396 if (bca.lines == NULL) {
3397 error = got_error_from_errno("calloc");
3401 bca.nlines_prec = 0;
3409 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3410 check_cancelled, NULL);
3418 got_object_blob_close(blob);
3420 got_worktree_close(worktree);
3422 const struct got_error *repo_error;
3423 repo_error = got_repo_close(repo);
3428 for (i = 0; i < bca.nlines; i++) {
3429 struct blame_line *bline = &bca.lines[i];
3430 free(bline->id_str);
3431 free(bline->committer);
3435 free(bca.line_offsets);
3436 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3437 error = got_error_from_errno("fclose");
3445 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3451 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3452 const char *root_path)
3454 int is_root_path = (strcmp(path, root_path) == 0);
3455 const char *modestr = "";
3456 mode_t mode = got_tree_entry_get_mode(te);
3458 path += strlen(root_path);
3459 while (path[0] == '/')
3462 if (got_object_tree_entry_is_submodule(te))
3464 else if (S_ISLNK(mode))
3466 else if (S_ISDIR(mode))
3468 else if (mode & S_IXUSR)
3471 printf("%s%s%s%s%s\n", id ? id : "", path,
3472 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3475 static const struct got_error *
3476 print_tree(const char *path, struct got_object_id *commit_id,
3477 int show_ids, int recurse, const char *root_path,
3478 struct got_repository *repo)
3480 const struct got_error *err = NULL;
3481 struct got_object_id *tree_id = NULL;
3482 struct got_tree_object *tree = NULL;
3485 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3489 err = got_object_open_as_tree(&tree, repo, tree_id);
3492 nentries = got_object_tree_get_nentries(tree);
3493 for (i = 0; i < nentries; i++) {
3494 struct got_tree_entry *te;
3497 if (sigint_received || sigpipe_received)
3500 te = got_object_tree_get_entry(tree, i);
3503 err = got_object_id_str(&id_str,
3504 got_tree_entry_get_id(te));
3507 if (asprintf(&id, "%s ", id_str) == -1) {
3508 err = got_error_from_errno("asprintf");
3514 print_entry(te, id, path, root_path);
3517 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3519 if (asprintf(&child_path, "%s%s%s", path,
3520 path[0] == '/' && path[1] == '\0' ? "" : "/",
3521 got_tree_entry_get_name(te)) == -1) {
3522 err = got_error_from_errno("asprintf");
3525 err = print_tree(child_path, commit_id, show_ids, 1,
3534 got_object_tree_close(tree);
3539 static const struct got_error *
3540 cmd_tree(int argc, char *argv[])
3542 const struct got_error *error;
3543 struct got_repository *repo = NULL;
3544 struct got_worktree *worktree = NULL;
3546 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3547 struct got_object_id *commit_id = NULL;
3548 char *commit_id_str = NULL;
3549 int show_ids = 0, recurse = 0;
3553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3558 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3561 commit_id_str = optarg;
3564 repo_path = realpath(optarg, NULL);
3565 if (repo_path == NULL)
3566 return got_error_from_errno2("realpath",
3568 got_path_strip_trailing_slashes(repo_path);
3592 cwd = getcwd(NULL, 0);
3594 error = got_error_from_errno("getcwd");
3597 if (repo_path == NULL) {
3598 error = got_worktree_open(&worktree, cwd);
3599 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3605 strdup(got_worktree_get_repo_path(worktree));
3606 if (repo_path == NULL)
3607 error = got_error_from_errno("strdup");
3611 repo_path = strdup(cwd);
3612 if (repo_path == NULL) {
3613 error = got_error_from_errno("strdup");
3619 error = got_repo_open(&repo, repo_path, NULL);
3623 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3629 char *p, *worktree_subdir = cwd +
3630 strlen(got_worktree_get_root_path(worktree));
3631 if (asprintf(&p, "%s/%s",
3632 got_worktree_get_path_prefix(worktree),
3633 worktree_subdir) == -1) {
3634 error = got_error_from_errno("asprintf");
3637 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3644 if (in_repo_path == NULL) {
3645 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3650 if (commit_id_str == NULL) {
3651 struct got_reference *head_ref;
3652 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3655 error = got_ref_resolve(&commit_id, repo, head_ref);
3656 got_ref_close(head_ref);
3660 error = got_repo_match_object_id(&commit_id, NULL,
3661 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3666 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3667 in_repo_path, repo);
3674 got_worktree_close(worktree);
3676 const struct got_error *repo_error;
3677 repo_error = got_repo_close(repo);
3687 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3691 static const struct got_error *
3692 print_status(void *arg, unsigned char status, unsigned char staged_status,
3693 const char *path, struct got_object_id *blob_id,
3694 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3695 int dirfd, const char *de_name)
3697 if (status == staged_status && (status == GOT_STATUS_DELETE))
3698 status = GOT_STATUS_NO_CHANGE;
3699 printf("%c%c %s\n", status, staged_status, path);
3703 static const struct got_error *
3704 cmd_status(int argc, char *argv[])
3706 const struct got_error *error = NULL;
3707 struct got_repository *repo = NULL;
3708 struct got_worktree *worktree = NULL;
3710 struct got_pathlist_head paths;
3711 struct got_pathlist_entry *pe;
3716 while ((ch = getopt(argc, argv, "")) != -1) {
3728 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3732 cwd = getcwd(NULL, 0);
3734 error = got_error_from_errno("getcwd");
3738 error = got_worktree_open(&worktree, cwd);
3742 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3747 error = apply_unveil(got_repo_get_path(repo), 1,
3748 got_worktree_get_root_path(worktree));
3752 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3756 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3757 check_cancelled, NULL);
3759 TAILQ_FOREACH(pe, &paths, entry)
3760 free((char *)pe->path);
3761 got_pathlist_free(&paths);
3770 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3775 static const struct got_error *
3776 list_refs(struct got_repository *repo)
3778 static const struct got_error *err = NULL;
3779 struct got_reflist_head refs;
3780 struct got_reflist_entry *re;
3782 SIMPLEQ_INIT(&refs);
3783 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3787 SIMPLEQ_FOREACH(re, &refs, entry) {
3789 refstr = got_ref_to_str(re->ref);
3791 return got_error_from_errno("got_ref_to_str");
3792 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3796 got_ref_list_free(&refs);
3800 static const struct got_error *
3801 delete_ref(struct got_repository *repo, const char *refname)
3803 const struct got_error *err = NULL;
3804 struct got_reference *ref;
3806 err = got_ref_open(&ref, repo, refname, 0);
3810 err = got_ref_delete(ref, repo);
3815 static const struct got_error *
3816 add_ref(struct got_repository *repo, const char *refname, const char *target)
3818 const struct got_error *err = NULL;
3819 struct got_object_id *id;
3820 struct got_reference *ref = NULL;
3823 * Don't let the user create a reference name with a leading '-'.
3824 * While technically a valid reference name, this case is usually
3825 * an unintended typo.
3827 if (refname[0] == '-')
3828 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3830 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3833 struct got_reference *target_ref;
3835 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3837 err = got_ref_open(&target_ref, repo, target, 0);
3840 err = got_ref_resolve(&id, repo, target_ref);
3841 got_ref_close(target_ref);
3846 err = got_ref_alloc(&ref, refname, id);
3850 err = got_ref_write(ref, repo);
3858 static const struct got_error *
3859 add_symref(struct got_repository *repo, const char *refname, const char *target)
3861 const struct got_error *err = NULL;
3862 struct got_reference *ref = NULL;
3863 struct got_reference *target_ref = NULL;
3866 * Don't let the user create a reference name with a leading '-'.
3867 * While technically a valid reference name, this case is usually
3868 * an unintended typo.
3870 if (refname[0] == '-')
3871 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3873 err = got_ref_open(&target_ref, repo, target, 0);
3877 err = got_ref_alloc_symref(&ref, refname, target_ref);
3881 err = got_ref_write(ref, repo);
3884 got_ref_close(target_ref);
3890 static const struct got_error *
3891 cmd_ref(int argc, char *argv[])
3893 const struct got_error *error = NULL;
3894 struct got_repository *repo = NULL;
3895 struct got_worktree *worktree = NULL;
3896 char *cwd = NULL, *repo_path = NULL;
3897 int ch, do_list = 0, create_symref = 0;
3898 const char *delref = NULL;
3900 /* TODO: Add -s option for adding symbolic references. */
3901 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3907 repo_path = realpath(optarg, NULL);
3908 if (repo_path == NULL)
3909 return got_error_from_errno2("realpath",
3911 got_path_strip_trailing_slashes(repo_path);
3925 if (do_list && delref)
3926 errx(1, "-l and -d options are mutually exclusive\n");
3931 if (do_list || delref) {
3933 errx(1, "-s option cannot be used together with the "
3934 "-l or -d options");
3937 } else if (argc != 2)
3942 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3946 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3947 "sendfd unveil", NULL) == -1)
3951 cwd = getcwd(NULL, 0);
3953 error = got_error_from_errno("getcwd");
3957 if (repo_path == NULL) {
3958 error = got_worktree_open(&worktree, cwd);
3959 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3965 strdup(got_worktree_get_repo_path(worktree));
3966 if (repo_path == NULL)
3967 error = got_error_from_errno("strdup");
3971 repo_path = strdup(cwd);
3972 if (repo_path == NULL) {
3973 error = got_error_from_errno("strdup");
3979 error = got_repo_open(&repo, repo_path, NULL);
3983 error = apply_unveil(got_repo_get_path(repo), do_list,
3984 worktree ? got_worktree_get_root_path(worktree) : NULL);
3989 error = list_refs(repo);
3991 error = delete_ref(repo, delref);
3992 else if (create_symref)
3993 error = add_symref(repo, argv[0], argv[1]);
3995 error = add_ref(repo, argv[0], argv[1]);
3998 got_repo_close(repo);
4000 got_worktree_close(worktree);
4010 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4011 "[name]\n", getprogname());
4015 static const struct got_error *
4016 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4017 struct got_reference *ref)
4019 const struct got_error *err = NULL;
4020 const char *refname, *marker = " ";
4023 refname = got_ref_get_name(ref);
4024 if (worktree && strcmp(refname,
4025 got_worktree_get_head_ref_name(worktree)) == 0) {
4026 struct got_object_id *id = NULL;
4028 err = got_ref_resolve(&id, repo, ref);
4031 if (got_object_id_cmp(id,
4032 got_worktree_get_base_commit_id(worktree)) == 0)
4039 if (strncmp(refname, "refs/heads/", 11) == 0)
4041 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4044 refstr = got_ref_to_str(ref);
4046 return got_error_from_errno("got_ref_to_str");
4048 printf("%s%s: %s\n", marker, refname, refstr);
4053 static const struct got_error *
4054 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4056 const char *refname;
4058 if (worktree == NULL)
4059 return got_error(GOT_ERR_NOT_WORKTREE);
4061 refname = got_worktree_get_head_ref_name(worktree);
4063 if (strncmp(refname, "refs/heads/", 11) == 0)
4065 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4068 printf("%s\n", refname);
4073 static const struct got_error *
4074 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4076 static const struct got_error *err = NULL;
4077 struct got_reflist_head refs;
4078 struct got_reflist_entry *re;
4079 struct got_reference *temp_ref = NULL;
4080 int rebase_in_progress, histedit_in_progress;
4082 SIMPLEQ_INIT(&refs);
4085 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4090 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4095 if (rebase_in_progress || histedit_in_progress) {
4096 err = got_ref_open(&temp_ref, repo,
4097 got_worktree_get_head_ref_name(worktree), 0);
4100 list_branch(repo, worktree, temp_ref);
4101 got_ref_close(temp_ref);
4105 err = got_ref_list(&refs, repo, "refs/heads",
4106 got_ref_cmp_by_name, NULL);
4110 SIMPLEQ_FOREACH(re, &refs, entry)
4111 list_branch(repo, worktree, re->ref);
4113 got_ref_list_free(&refs);
4117 static const struct got_error *
4118 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4119 const char *branch_name)
4121 const struct got_error *err = NULL;
4122 struct got_reference *ref = NULL;
4125 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4126 return got_error_from_errno("asprintf");
4128 err = got_ref_open(&ref, repo, refname, 0);
4133 strcmp(got_worktree_get_head_ref_name(worktree),
4134 got_ref_get_name(ref)) == 0) {
4135 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4136 "will not delete this work tree's current branch");
4140 err = got_ref_delete(ref, repo);
4148 static const struct got_error *
4149 add_branch(struct got_repository *repo, const char *branch_name,
4150 struct got_object_id *base_commit_id)
4152 const struct got_error *err = NULL;
4153 struct got_reference *ref = NULL;
4154 char *base_refname = NULL, *refname = NULL;
4157 * Don't let the user create a branch name with a leading '-'.
4158 * While technically a valid reference name, this case is usually
4159 * an unintended typo.
4161 if (branch_name[0] == '-')
4162 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4164 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4165 err = got_error_from_errno("asprintf");
4169 err = got_ref_open(&ref, repo, refname, 0);
4171 err = got_error(GOT_ERR_BRANCH_EXISTS);
4173 } else if (err->code != GOT_ERR_NOT_REF)
4176 err = got_ref_alloc(&ref, refname, base_commit_id);
4180 err = got_ref_write(ref, repo);
4189 static const struct got_error *
4190 cmd_branch(int argc, char *argv[])
4192 const struct got_error *error = NULL;
4193 struct got_repository *repo = NULL;
4194 struct got_worktree *worktree = NULL;
4195 char *cwd = NULL, *repo_path = NULL;
4196 int ch, do_list = 0, do_show = 0, do_update = 1;
4197 const char *delref = NULL, *commit_id_arg = NULL;
4198 struct got_reference *ref = NULL;
4199 struct got_pathlist_head paths;
4200 struct got_pathlist_entry *pe;
4201 struct got_object_id *commit_id = NULL;
4202 char *commit_id_str = NULL;
4206 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4209 commit_id_arg = optarg;
4215 repo_path = realpath(optarg, NULL);
4216 if (repo_path == NULL)
4217 return got_error_from_errno2("realpath",
4219 got_path_strip_trailing_slashes(repo_path);
4233 if (do_list && delref)
4234 errx(1, "-l and -d options are mutually exclusive\n");
4239 if (!do_list && !delref && argc == 0)
4242 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4243 errx(1, "-c option can only be used when creating a branch");
4245 if (do_list || delref) {
4248 } else if (!do_show && argc != 1)
4252 if (do_list || do_show) {
4253 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4257 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4258 "sendfd unveil", NULL) == -1)
4262 cwd = getcwd(NULL, 0);
4264 error = got_error_from_errno("getcwd");
4268 if (repo_path == NULL) {
4269 error = got_worktree_open(&worktree, cwd);
4270 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4276 strdup(got_worktree_get_repo_path(worktree));
4277 if (repo_path == NULL)
4278 error = got_error_from_errno("strdup");
4282 repo_path = strdup(cwd);
4283 if (repo_path == NULL) {
4284 error = got_error_from_errno("strdup");
4290 error = got_repo_open(&repo, repo_path, NULL);
4294 error = apply_unveil(got_repo_get_path(repo), do_list,
4295 worktree ? got_worktree_get_root_path(worktree) : NULL);
4300 error = show_current_branch(repo, worktree);
4302 error = list_branches(repo, worktree);
4304 error = delete_branch(repo, worktree, delref);
4306 if (commit_id_arg == NULL)
4307 commit_id_arg = worktree ?
4308 got_worktree_get_head_ref_name(worktree) :
4310 error = got_repo_match_object_id(&commit_id, NULL,
4311 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4314 error = add_branch(repo, argv[0], commit_id);
4317 if (worktree && do_update) {
4318 int did_something = 0;
4319 char *branch_refname = NULL;
4321 error = got_object_id_str(&commit_id_str, commit_id);
4324 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4328 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4330 error = got_error_from_errno("asprintf");
4333 error = got_ref_open(&ref, repo, branch_refname, 0);
4334 free(branch_refname);
4337 error = switch_head_ref(ref, commit_id, worktree,
4341 error = got_worktree_set_base_commit_id(worktree, repo,
4345 error = got_worktree_checkout_files(worktree, &paths,
4346 repo, update_progress, &did_something,
4347 check_cancelled, NULL);
4351 printf("Updated to commit %s\n", commit_id_str);
4358 got_repo_close(repo);
4360 got_worktree_close(worktree);
4364 free(commit_id_str);
4365 TAILQ_FOREACH(pe, &paths, entry)
4366 free((char *)pe->path);
4367 got_pathlist_free(&paths);
4376 "usage: %s tag [-c commit] [-r repository] [-l] "
4377 "[-m message] name\n", getprogname());
4382 static const struct got_error *
4383 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4385 const struct got_error *err = NULL;
4386 struct got_reflist_entry *re, *se, *new;
4387 struct got_object_id *re_id, *se_id;
4388 struct got_tag_object *re_tag, *se_tag;
4389 time_t re_time, se_time;
4391 SIMPLEQ_FOREACH(re, tags, entry) {
4392 se = SIMPLEQ_FIRST(sorted);
4394 err = got_reflist_entry_dup(&new, re);
4397 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4400 err = got_ref_resolve(&re_id, repo, re->ref);
4403 err = got_object_open_as_tag(&re_tag, repo, re_id);
4407 re_time = got_object_tag_get_tagger_time(re_tag);
4408 got_object_tag_close(re_tag);
4412 err = got_ref_resolve(&se_id, repo, re->ref);
4415 err = got_object_open_as_tag(&se_tag, repo, se_id);
4419 se_time = got_object_tag_get_tagger_time(se_tag);
4420 got_object_tag_close(se_tag);
4422 if (se_time > re_time) {
4423 err = got_reflist_entry_dup(&new, re);
4426 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4429 se = SIMPLEQ_NEXT(se, entry);
4438 static const struct got_error *
4439 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4441 static const struct got_error *err = NULL;
4442 struct got_reflist_head refs;
4443 struct got_reflist_entry *re;
4445 SIMPLEQ_INIT(&refs);
4447 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4451 SIMPLEQ_FOREACH(re, &refs, entry) {
4452 const char *refname;
4453 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4457 struct got_object_id *id;
4458 struct got_tag_object *tag;
4459 struct got_commit_object *commit = NULL;
4461 refname = got_ref_get_name(re->ref);
4462 if (strncmp(refname, "refs/tags/", 10) != 0)
4465 refstr = got_ref_to_str(re->ref);
4466 if (refstr == NULL) {
4467 err = got_error_from_errno("got_ref_to_str");
4470 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4473 err = got_ref_resolve(&id, repo, re->ref);
4476 err = got_object_open_as_tag(&tag, repo, id);
4478 if (err->code != GOT_ERR_OBJ_TYPE) {
4482 /* "lightweight" tag */
4483 err = got_object_open_as_commit(&commit, repo, id);
4488 tagger = got_object_commit_get_committer(commit);
4490 got_object_commit_get_committer_time(commit);
4491 err = got_object_id_str(&id_str, id);
4497 tagger = got_object_tag_get_tagger(tag);
4498 tagger_time = got_object_tag_get_tagger_time(tag);
4499 err = got_object_id_str(&id_str,
4500 got_object_tag_get_object_id(tag));
4504 printf("from: %s\n", tagger);
4505 datestr = get_datestr(&tagger_time, datebuf);
4507 printf("date: %s UTC\n", datestr);
4509 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4511 switch (got_object_tag_get_object_type(tag)) {
4512 case GOT_OBJ_TYPE_BLOB:
4513 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4516 case GOT_OBJ_TYPE_TREE:
4517 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4520 case GOT_OBJ_TYPE_COMMIT:
4521 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4524 case GOT_OBJ_TYPE_TAG:
4525 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4534 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4537 got_object_commit_close(commit);
4539 tagmsg0 = strdup(got_object_tag_get_message(tag));
4540 got_object_tag_close(tag);
4541 if (tagmsg0 == NULL) {
4542 err = got_error_from_errno("strdup");
4549 line = strsep(&tagmsg, "\n");
4551 printf(" %s\n", line);
4556 got_ref_list_free(&refs);
4560 static const struct got_error *
4561 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4562 const char *tag_name, const char *repo_path)
4564 const struct got_error *err = NULL;
4565 char *template = NULL, *initial_content = NULL;
4566 char *editor = NULL;
4569 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4570 err = got_error_from_errno("asprintf");
4574 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4575 commit_id_str, tag_name) == -1) {
4576 err = got_error_from_errno("asprintf");
4580 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4584 dprintf(fd, initial_content);
4587 err = get_editor(&editor);
4590 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4592 free(initial_content);
4596 /* Editor is done; we can now apply unveil(2) */
4598 err = apply_unveil(repo_path, 0, NULL);
4607 static const struct got_error *
4608 add_tag(struct got_repository *repo, const char *tag_name,
4609 const char *commit_arg, const char *tagmsg_arg)
4611 const struct got_error *err = NULL;
4612 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4613 char *label = NULL, *commit_id_str = NULL;
4614 struct got_reference *ref = NULL;
4615 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4616 char *tagmsg_path = NULL, *tag_id_str = NULL;
4617 int preserve_tagmsg = 0;
4620 * Don't let the user create a tag name with a leading '-'.
4621 * While technically a valid reference name, this case is usually
4622 * an unintended typo.
4624 if (tag_name[0] == '-')
4625 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4627 err = get_author(&tagger, repo);
4631 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4632 GOT_OBJ_TYPE_COMMIT, 1, repo);
4636 err = got_object_id_str(&commit_id_str, commit_id);
4640 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4641 refname = strdup(tag_name);
4642 if (refname == NULL) {
4643 err = got_error_from_errno("strdup");
4647 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4648 err = got_error_from_errno("asprintf");
4652 err = got_ref_open(&ref, repo, refname, 0);
4654 err = got_error(GOT_ERR_TAG_EXISTS);
4656 } else if (err->code != GOT_ERR_NOT_REF)
4659 if (tagmsg_arg == NULL) {
4660 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4661 tag_name, got_repo_get_path(repo));
4663 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4664 tagmsg_path != NULL)
4665 preserve_tagmsg = 1;
4670 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4671 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4674 preserve_tagmsg = 1;
4678 err = got_ref_alloc(&ref, refname, tag_id);
4681 preserve_tagmsg = 1;
4685 err = got_ref_write(ref, repo);
4688 preserve_tagmsg = 1;
4692 err = got_object_id_str(&tag_id_str, tag_id);
4695 preserve_tagmsg = 1;
4698 printf("Created tag %s\n", tag_id_str);
4700 if (preserve_tagmsg) {
4701 fprintf(stderr, "%s: tag message preserved in %s\n",
4702 getprogname(), tagmsg_path);
4703 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4704 err = got_error_from_errno2("unlink", tagmsg_path);
4709 free(commit_id_str);
4717 static const struct got_error *
4718 cmd_tag(int argc, char *argv[])
4720 const struct got_error *error = NULL;
4721 struct got_repository *repo = NULL;
4722 struct got_worktree *worktree = NULL;
4723 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4724 char *gitconfig_path = NULL;
4725 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4726 int ch, do_list = 0;
4728 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4731 commit_id_arg = optarg;
4737 repo_path = realpath(optarg, NULL);
4738 if (repo_path == NULL)
4739 return got_error_from_errno2("realpath",
4741 got_path_strip_trailing_slashes(repo_path);
4756 if (commit_id_arg != NULL)
4757 errx(1, "-c option can only be used when creating a tag");
4759 errx(1, "-l and -m options are mutually exclusive");
4762 } else if (argc != 1)
4769 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4773 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4774 "sendfd unveil", NULL) == -1)
4778 cwd = getcwd(NULL, 0);
4780 error = got_error_from_errno("getcwd");
4784 if (repo_path == NULL) {
4785 error = got_worktree_open(&worktree, cwd);
4786 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4792 strdup(got_worktree_get_repo_path(worktree));
4793 if (repo_path == NULL)
4794 error = got_error_from_errno("strdup");
4798 repo_path = strdup(cwd);
4799 if (repo_path == NULL) {
4800 error = got_error_from_errno("strdup");
4807 error = got_repo_open(&repo, repo_path, NULL);
4810 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4813 error = list_tags(repo, worktree);
4815 error = get_gitconfig_path(&gitconfig_path);
4818 error = got_repo_open(&repo, repo_path, gitconfig_path);
4823 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4828 if (commit_id_arg == NULL) {
4829 struct got_reference *head_ref;
4830 struct got_object_id *commit_id;
4831 error = got_ref_open(&head_ref, repo,
4832 worktree ? got_worktree_get_head_ref_name(worktree)
4836 error = got_ref_resolve(&commit_id, repo, head_ref);
4837 got_ref_close(head_ref);
4840 error = got_object_id_str(&commit_id_str, commit_id);
4846 error = add_tag(repo, tag_name,
4847 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4851 got_repo_close(repo);
4853 got_worktree_close(worktree);
4856 free(gitconfig_path);
4857 free(commit_id_str);
4864 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4869 static const struct got_error *
4870 add_progress(void *arg, unsigned char status, const char *path)
4872 while (path[0] == '/')
4874 printf("%c %s\n", status, path);
4878 static const struct got_error *
4879 cmd_add(int argc, char *argv[])
4881 const struct got_error *error = NULL;
4882 struct got_repository *repo = NULL;
4883 struct got_worktree *worktree = NULL;
4885 struct got_pathlist_head paths;
4886 struct got_pathlist_entry *pe;
4887 int ch, can_recurse = 0, no_ignores = 0;
4891 while ((ch = getopt(argc, argv, "IR")) != -1) {
4909 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4916 cwd = getcwd(NULL, 0);
4918 error = got_error_from_errno("getcwd");
4922 error = got_worktree_open(&worktree, cwd);
4926 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4931 error = apply_unveil(got_repo_get_path(repo), 1,
4932 got_worktree_get_root_path(worktree));
4936 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4940 if (!can_recurse && no_ignores) {
4941 error = got_error_msg(GOT_ERR_BAD_PATH,
4942 "disregarding ignores requires -R option");
4950 TAILQ_FOREACH(pe, &paths, entry) {
4951 if (asprintf(&ondisk_path, "%s/%s",
4952 got_worktree_get_root_path(worktree),
4954 error = got_error_from_errno("asprintf");
4957 if (lstat(ondisk_path, &sb) == -1) {
4958 if (errno == ENOENT) {
4962 error = got_error_from_errno2("lstat",
4968 if (S_ISDIR(sb.st_mode)) {
4969 error = got_error_msg(GOT_ERR_BAD_PATH,
4970 "adding directories requires -R option");
4976 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4977 NULL, repo, no_ignores);
4980 got_repo_close(repo);
4982 got_worktree_close(worktree);
4983 TAILQ_FOREACH(pe, &paths, entry)
4984 free((char *)pe->path);
4985 got_pathlist_free(&paths);
4993 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4998 static const struct got_error *
4999 print_remove_status(void *arg, unsigned char status,
5000 unsigned char staged_status, const char *path)
5002 while (path[0] == '/')
5004 if (status == GOT_STATUS_NONEXISTENT)
5006 if (status == staged_status && (status == GOT_STATUS_DELETE))
5007 status = GOT_STATUS_NO_CHANGE;
5008 printf("%c%c %s\n", status, staged_status, path);
5012 static const struct got_error *
5013 cmd_remove(int argc, char *argv[])
5015 const struct got_error *error = NULL;
5016 struct got_worktree *worktree = NULL;
5017 struct got_repository *repo = NULL;
5019 struct got_pathlist_head paths;
5020 struct got_pathlist_entry *pe;
5021 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5025 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5028 delete_local_mods = 1;
5046 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5053 cwd = getcwd(NULL, 0);
5055 error = got_error_from_errno("getcwd");
5058 error = got_worktree_open(&worktree, cwd);
5062 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5067 error = apply_unveil(got_repo_get_path(repo), 1,
5068 got_worktree_get_root_path(worktree));
5072 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5079 TAILQ_FOREACH(pe, &paths, entry) {
5080 if (asprintf(&ondisk_path, "%s/%s",
5081 got_worktree_get_root_path(worktree),
5083 error = got_error_from_errno("asprintf");
5086 if (lstat(ondisk_path, &sb) == -1) {
5087 if (errno == ENOENT) {
5091 error = got_error_from_errno2("lstat",
5097 if (S_ISDIR(sb.st_mode)) {
5098 error = got_error_msg(GOT_ERR_BAD_PATH,
5099 "removing directories requires -R option");
5105 error = got_worktree_schedule_delete(worktree, &paths,
5106 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5109 got_repo_close(repo);
5111 got_worktree_close(worktree);
5112 TAILQ_FOREACH(pe, &paths, entry)
5113 free((char *)pe->path);
5114 got_pathlist_free(&paths);
5122 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5123 "path ...\n", getprogname());
5127 static const struct got_error *
5128 revert_progress(void *arg, unsigned char status, const char *path)
5130 if (status == GOT_STATUS_UNVERSIONED)
5133 while (path[0] == '/')
5135 printf("%c %s\n", status, path);
5139 struct choose_patch_arg {
5140 FILE *patch_script_file;
5144 static const struct got_error *
5145 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5146 int nchanges, const char *action)
5149 size_t linesize = 0;
5153 case GOT_STATUS_ADD:
5154 printf("A %s\n%s this addition? [y/n] ", path, action);
5156 case GOT_STATUS_DELETE:
5157 printf("D %s\n%s this deletion? [y/n] ", path, action);
5159 case GOT_STATUS_MODIFY:
5160 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5161 return got_error_from_errno("fseek");
5162 printf(GOT_COMMIT_SEP_STR);
5163 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5165 if (ferror(patch_file))
5166 return got_error_from_errno("getline");
5167 printf(GOT_COMMIT_SEP_STR);
5168 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5169 path, n, nchanges, action);
5172 return got_error_path(path, GOT_ERR_FILE_STATUS);
5178 static const struct got_error *
5179 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5180 FILE *patch_file, int n, int nchanges)
5182 const struct got_error *err = NULL;
5184 size_t linesize = 0;
5187 struct choose_patch_arg *a = arg;
5189 *choice = GOT_PATCH_CHOICE_NONE;
5191 if (a->patch_script_file) {
5193 err = show_change(status, path, patch_file, n, nchanges,
5197 linelen = getline(&line, &linesize, a->patch_script_file);
5198 if (linelen == -1) {
5199 if (ferror(a->patch_script_file))
5200 return got_error_from_errno("getline");
5203 nl = strchr(line, '\n');
5206 if (strcmp(line, "y") == 0) {
5207 *choice = GOT_PATCH_CHOICE_YES;
5209 } else if (strcmp(line, "n") == 0) {
5210 *choice = GOT_PATCH_CHOICE_NO;
5212 } else if (strcmp(line, "q") == 0 &&
5213 status == GOT_STATUS_MODIFY) {
5214 *choice = GOT_PATCH_CHOICE_QUIT;
5217 printf("invalid response '%s'\n", line);
5222 while (resp != 'y' && resp != 'n' && resp != 'q') {
5223 err = show_change(status, path, patch_file, n, nchanges,
5230 if (status == GOT_STATUS_MODIFY) {
5231 if (resp != 'y' && resp != 'n' && resp != 'q') {
5232 printf("invalid response '%c'\n", resp);
5235 } else if (resp != 'y' && resp != 'n') {
5236 printf("invalid response '%c'\n", resp);
5242 *choice = GOT_PATCH_CHOICE_YES;
5243 else if (resp == 'n')
5244 *choice = GOT_PATCH_CHOICE_NO;
5245 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5246 *choice = GOT_PATCH_CHOICE_QUIT;
5252 static const struct got_error *
5253 cmd_revert(int argc, char *argv[])
5255 const struct got_error *error = NULL;
5256 struct got_worktree *worktree = NULL;
5257 struct got_repository *repo = NULL;
5258 char *cwd = NULL, *path = NULL;
5259 struct got_pathlist_head paths;
5260 struct got_pathlist_entry *pe;
5261 int ch, can_recurse = 0, pflag = 0;
5262 FILE *patch_script_file = NULL;
5263 const char *patch_script_path = NULL;
5264 struct choose_patch_arg cpa;
5268 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5274 patch_script_path = optarg;
5289 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5290 "unveil", NULL) == -1)
5295 if (patch_script_path && !pflag)
5296 errx(1, "-F option can only be used together with -p option");
5298 cwd = getcwd(NULL, 0);
5300 error = got_error_from_errno("getcwd");
5303 error = got_worktree_open(&worktree, cwd);
5307 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5312 if (patch_script_path) {
5313 patch_script_file = fopen(patch_script_path, "r");
5314 if (patch_script_file == NULL) {
5315 error = got_error_from_errno2("fopen",
5320 error = apply_unveil(got_repo_get_path(repo), 1,
5321 got_worktree_get_root_path(worktree));
5325 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5332 TAILQ_FOREACH(pe, &paths, entry) {
5333 if (asprintf(&ondisk_path, "%s/%s",
5334 got_worktree_get_root_path(worktree),
5336 error = got_error_from_errno("asprintf");
5339 if (lstat(ondisk_path, &sb) == -1) {
5340 if (errno == ENOENT) {
5344 error = got_error_from_errno2("lstat",
5350 if (S_ISDIR(sb.st_mode)) {
5351 error = got_error_msg(GOT_ERR_BAD_PATH,
5352 "reverting directories requires -R option");
5358 cpa.patch_script_file = patch_script_file;
5359 cpa.action = "revert";
5360 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5361 pflag ? choose_patch : NULL, &cpa, repo);
5363 if (patch_script_file && fclose(patch_script_file) == EOF &&
5365 error = got_error_from_errno2("fclose", patch_script_path);
5367 got_repo_close(repo);
5369 got_worktree_close(worktree);
5378 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5383 struct collect_commit_logmsg_arg {
5384 const char *cmdline_log;
5386 const char *worktree_path;
5387 const char *branch_name;
5388 const char *repo_path;
5393 static const struct got_error *
5394 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5397 char *initial_content = NULL;
5398 struct got_pathlist_entry *pe;
5399 const struct got_error *err = NULL;
5400 char *template = NULL;
5401 struct collect_commit_logmsg_arg *a = arg;
5405 /* if a message was specified on the command line, just use it */
5406 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5407 len = strlen(a->cmdline_log) + 1;
5408 *logmsg = malloc(len + 1);
5409 if (*logmsg == NULL)
5410 return got_error_from_errno("malloc");
5411 strlcpy(*logmsg, a->cmdline_log, len);
5415 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5416 return got_error_from_errno("asprintf");
5418 if (asprintf(&initial_content,
5419 "\n# changes to be committed on branch %s:\n",
5420 a->branch_name) == -1)
5421 return got_error_from_errno("asprintf");
5423 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5427 dprintf(fd, initial_content);
5429 TAILQ_FOREACH(pe, commitable_paths, entry) {
5430 struct got_commitable *ct = pe->data;
5431 dprintf(fd, "# %c %s\n",
5432 got_commitable_get_status(ct),
5433 got_commitable_get_path(ct));
5437 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5439 free(initial_content);
5442 /* Editor is done; we can now apply unveil(2) */
5444 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5453 static const struct got_error *
5454 cmd_commit(int argc, char *argv[])
5456 const struct got_error *error = NULL;
5457 struct got_worktree *worktree = NULL;
5458 struct got_repository *repo = NULL;
5459 char *cwd = NULL, *id_str = NULL;
5460 struct got_object_id *id = NULL;
5461 const char *logmsg = NULL;
5462 struct collect_commit_logmsg_arg cl_arg;
5463 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5464 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5465 struct got_pathlist_head paths;
5468 cl_arg.logmsg_path = NULL;
5470 while ((ch = getopt(argc, argv, "m:")) != -1) {
5485 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5486 "unveil", NULL) == -1)
5489 cwd = getcwd(NULL, 0);
5491 error = got_error_from_errno("getcwd");
5494 error = got_worktree_open(&worktree, cwd);
5498 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5501 if (rebase_in_progress) {
5502 error = got_error(GOT_ERR_REBASING);
5506 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5511 error = get_gitconfig_path(&gitconfig_path);
5514 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5519 error = get_author(&author, repo);
5524 * unveil(2) traverses exec(2); if an editor is used we have
5525 * to apply unveil after the log message has been written.
5527 if (logmsg == NULL || strlen(logmsg) == 0)
5528 error = get_editor(&editor);
5530 error = apply_unveil(got_repo_get_path(repo), 0,
5531 got_worktree_get_root_path(worktree));
5535 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5539 cl_arg.editor = editor;
5540 cl_arg.cmdline_log = logmsg;
5541 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5542 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5543 if (!histedit_in_progress) {
5544 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5545 error = got_error(GOT_ERR_COMMIT_BRANCH);
5548 cl_arg.branch_name += 11;
5550 cl_arg.repo_path = got_repo_get_path(repo);
5551 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5552 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5554 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5555 cl_arg.logmsg_path != NULL)
5556 preserve_logmsg = 1;
5560 error = got_object_id_str(&id_str, id);
5563 printf("Created commit %s\n", id_str);
5565 if (preserve_logmsg) {
5566 fprintf(stderr, "%s: log message preserved in %s\n",
5567 getprogname(), cl_arg.logmsg_path);
5568 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5570 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5571 free(cl_arg.logmsg_path);
5573 got_repo_close(repo);
5575 got_worktree_close(worktree);
5578 free(gitconfig_path);
5585 usage_cherrypick(void)
5587 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5591 static const struct got_error *
5592 cmd_cherrypick(int argc, char *argv[])
5594 const struct got_error *error = NULL;
5595 struct got_worktree *worktree = NULL;
5596 struct got_repository *repo = NULL;
5597 char *cwd = NULL, *commit_id_str = NULL;
5598 struct got_object_id *commit_id = NULL;
5599 struct got_commit_object *commit = NULL;
5600 struct got_object_qid *pid;
5601 struct got_reference *head_ref = NULL;
5602 int ch, did_something = 0;
5604 while ((ch = getopt(argc, argv, "")) != -1) {
5616 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5617 "unveil", NULL) == -1)
5623 cwd = getcwd(NULL, 0);
5625 error = got_error_from_errno("getcwd");
5628 error = got_worktree_open(&worktree, cwd);
5632 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5637 error = apply_unveil(got_repo_get_path(repo), 0,
5638 got_worktree_get_root_path(worktree));
5642 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5643 GOT_OBJ_TYPE_COMMIT, repo);
5644 if (error != NULL) {
5645 struct got_reference *ref;
5646 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5648 error = got_ref_open(&ref, repo, argv[0], 0);
5651 error = got_ref_resolve(&commit_id, repo, ref);
5656 error = got_object_id_str(&commit_id_str, commit_id);
5660 error = got_ref_open(&head_ref, repo,
5661 got_worktree_get_head_ref_name(worktree), 0);
5665 error = check_same_branch(commit_id, head_ref, NULL, repo);
5667 if (error->code != GOT_ERR_ANCESTRY)
5671 error = got_error(GOT_ERR_SAME_BRANCH);
5675 error = got_object_open_as_commit(&commit, repo, commit_id);
5678 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5679 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5680 commit_id, repo, update_progress, &did_something, check_cancelled,
5686 printf("Merged commit %s\n", commit_id_str);
5689 got_object_commit_close(commit);
5690 free(commit_id_str);
5692 got_ref_close(head_ref);
5694 got_worktree_close(worktree);
5696 got_repo_close(repo);
5703 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5707 static const struct got_error *
5708 cmd_backout(int argc, char *argv[])
5710 const struct got_error *error = NULL;
5711 struct got_worktree *worktree = NULL;
5712 struct got_repository *repo = NULL;
5713 char *cwd = NULL, *commit_id_str = NULL;
5714 struct got_object_id *commit_id = NULL;
5715 struct got_commit_object *commit = NULL;
5716 struct got_object_qid *pid;
5717 struct got_reference *head_ref = NULL;
5718 int ch, did_something = 0;
5720 while ((ch = getopt(argc, argv, "")) != -1) {
5732 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5733 "unveil", NULL) == -1)
5739 cwd = getcwd(NULL, 0);
5741 error = got_error_from_errno("getcwd");
5744 error = got_worktree_open(&worktree, cwd);
5748 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5753 error = apply_unveil(got_repo_get_path(repo), 0,
5754 got_worktree_get_root_path(worktree));
5758 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5759 GOT_OBJ_TYPE_COMMIT, repo);
5760 if (error != NULL) {
5761 struct got_reference *ref;
5762 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5764 error = got_ref_open(&ref, repo, argv[0], 0);
5767 error = got_ref_resolve(&commit_id, repo, ref);
5772 error = got_object_id_str(&commit_id_str, commit_id);
5776 error = got_ref_open(&head_ref, repo,
5777 got_worktree_get_head_ref_name(worktree), 0);
5781 error = check_same_branch(commit_id, head_ref, NULL, repo);
5785 error = got_object_open_as_commit(&commit, repo, commit_id);
5788 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5790 error = got_error(GOT_ERR_ROOT_COMMIT);
5794 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5795 update_progress, &did_something, check_cancelled, NULL);
5800 printf("Backed out commit %s\n", commit_id_str);
5803 got_object_commit_close(commit);
5804 free(commit_id_str);
5806 got_ref_close(head_ref);
5808 got_worktree_close(worktree);
5810 got_repo_close(repo);
5817 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5823 trim_logmsg(char *logmsg, int limit)
5828 len = strlen(logmsg);
5832 nl = strchr(logmsg, '\n');
5837 static const struct got_error *
5838 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5840 const struct got_error *err;
5841 char *logmsg0 = NULL;
5844 err = got_object_commit_get_logmsg(&logmsg0, commit);
5849 while (isspace((unsigned char)s[0]))
5852 *logmsg = strdup(s);
5853 if (*logmsg == NULL) {
5854 err = got_error_from_errno("strdup");
5858 trim_logmsg(*logmsg, limit);
5864 static const struct got_error *
5865 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5867 const struct got_error *err;
5868 struct got_commit_object *commit = NULL;
5869 char *id_str = NULL, *logmsg = NULL;
5871 err = got_object_open_as_commit(&commit, repo, id);
5875 err = got_object_id_str(&id_str, id);
5881 err = get_short_logmsg(&logmsg, 42, commit);
5885 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5888 got_object_commit_close(commit);
5893 static const struct got_error *
5894 show_rebase_progress(struct got_commit_object *commit,
5895 struct got_object_id *old_id, struct got_object_id *new_id)
5897 const struct got_error *err;
5898 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5900 err = got_object_id_str(&old_id_str, old_id);
5905 err = got_object_id_str(&new_id_str, new_id);
5910 old_id_str[12] = '\0';
5912 new_id_str[12] = '\0';
5914 err = get_short_logmsg(&logmsg, 42, commit);
5918 printf("%s -> %s: %s\n", old_id_str,
5919 new_id_str ? new_id_str : "no-op change", logmsg);
5927 static const struct got_error *
5928 rebase_progress(void *arg, unsigned char status, const char *path)
5930 unsigned char *rebase_status = arg;
5932 while (path[0] == '/')
5934 printf("%c %s\n", status, path);
5936 if (*rebase_status == GOT_STATUS_CONFLICT)
5938 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5939 *rebase_status = status;
5943 static const struct got_error *
5944 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5945 struct got_reference *branch, struct got_reference *new_base_branch,
5946 struct got_reference *tmp_branch, struct got_repository *repo)
5948 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5949 return got_worktree_rebase_complete(worktree, fileindex,
5950 new_base_branch, tmp_branch, branch, repo);
5953 static const struct got_error *
5954 rebase_commit(struct got_pathlist_head *merged_paths,
5955 struct got_worktree *worktree, struct got_fileindex *fileindex,
5956 struct got_reference *tmp_branch,
5957 struct got_object_id *commit_id, struct got_repository *repo)
5959 const struct got_error *error;
5960 struct got_commit_object *commit;
5961 struct got_object_id *new_commit_id;
5963 error = got_object_open_as_commit(&commit, repo, commit_id);
5967 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5968 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5970 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5972 error = show_rebase_progress(commit, commit_id, NULL);
5974 error = show_rebase_progress(commit, commit_id, new_commit_id);
5975 free(new_commit_id);
5978 got_object_commit_close(commit);
5982 struct check_path_prefix_arg {
5983 const char *path_prefix;
5988 static const struct got_error *
5989 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5990 struct got_blob_object *blob2, struct got_object_id *id1,
5991 struct got_object_id *id2, const char *path1, const char *path2,
5992 mode_t mode1, mode_t mode2, struct got_repository *repo)
5994 struct check_path_prefix_arg *a = arg;
5996 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5997 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5998 return got_error(a->errcode);
6003 static const struct got_error *
6004 check_path_prefix(struct got_object_id *parent_id,
6005 struct got_object_id *commit_id, const char *path_prefix,
6006 int errcode, struct got_repository *repo)
6008 const struct got_error *err;
6009 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6010 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6011 struct check_path_prefix_arg cpp_arg;
6013 if (got_path_is_root_dir(path_prefix))
6016 err = got_object_open_as_commit(&commit, repo, commit_id);
6020 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6024 err = got_object_open_as_tree(&tree1, repo,
6025 got_object_commit_get_tree_id(parent_commit));
6029 err = got_object_open_as_tree(&tree2, repo,
6030 got_object_commit_get_tree_id(commit));
6034 cpp_arg.path_prefix = path_prefix;
6035 while (cpp_arg.path_prefix[0] == '/')
6036 cpp_arg.path_prefix++;
6037 cpp_arg.len = strlen(cpp_arg.path_prefix);
6038 cpp_arg.errcode = errcode;
6039 err = got_diff_tree(tree1, tree2, "", "", repo,
6040 check_path_prefix_in_diff, &cpp_arg, 0);
6043 got_object_tree_close(tree1);
6045 got_object_tree_close(tree2);
6047 got_object_commit_close(commit);
6049 got_object_commit_close(parent_commit);
6053 static const struct got_error *
6054 collect_commits(struct got_object_id_queue *commits,
6055 struct got_object_id *initial_commit_id,
6056 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6057 const char *path_prefix, int path_prefix_errcode,
6058 struct got_repository *repo)
6060 const struct got_error *err = NULL;
6061 struct got_commit_graph *graph = NULL;
6062 struct got_object_id *parent_id = NULL;
6063 struct got_object_qid *qid;
6064 struct got_object_id *commit_id = initial_commit_id;
6066 err = got_commit_graph_open(&graph, "/", 1);
6070 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6071 check_cancelled, NULL);
6074 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6075 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6076 check_cancelled, NULL);
6078 if (err->code == GOT_ERR_ITER_COMPLETED) {
6079 err = got_error_msg(GOT_ERR_ANCESTRY,
6080 "ran out of commits to rebase before "
6081 "youngest common ancestor commit has "
6086 err = check_path_prefix(parent_id, commit_id,
6087 path_prefix, path_prefix_errcode, repo);
6091 err = got_object_qid_alloc(&qid, commit_id);
6094 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6095 commit_id = parent_id;
6099 got_commit_graph_close(graph);
6103 static const struct got_error *
6104 cmd_rebase(int argc, char *argv[])
6106 const struct got_error *error = NULL;
6107 struct got_worktree *worktree = NULL;
6108 struct got_repository *repo = NULL;
6109 struct got_fileindex *fileindex = NULL;
6111 struct got_reference *branch = NULL;
6112 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6113 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6114 struct got_object_id *resume_commit_id = NULL;
6115 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6116 struct got_commit_object *commit = NULL;
6117 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6118 int histedit_in_progress = 0;
6119 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6120 struct got_object_id_queue commits;
6121 struct got_pathlist_head merged_paths;
6122 const struct got_object_id_queue *parent_ids;
6123 struct got_object_qid *qid, *pid;
6125 SIMPLEQ_INIT(&commits);
6126 TAILQ_INIT(&merged_paths);
6128 while ((ch = getopt(argc, argv, "ac")) != -1) {
6134 continue_rebase = 1;
6146 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6147 "unveil", NULL) == -1)
6150 if (abort_rebase && continue_rebase)
6152 else if (abort_rebase || continue_rebase) {
6155 } else if (argc != 1)
6158 cwd = getcwd(NULL, 0);
6160 error = got_error_from_errno("getcwd");
6163 error = got_worktree_open(&worktree, cwd);
6167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6172 error = apply_unveil(got_repo_get_path(repo), 0,
6173 got_worktree_get_root_path(worktree));
6177 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6181 if (histedit_in_progress) {
6182 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6186 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6192 if (!rebase_in_progress) {
6193 error = got_error(GOT_ERR_NOT_REBASING);
6196 error = got_worktree_rebase_continue(&resume_commit_id,
6197 &new_base_branch, &tmp_branch, &branch, &fileindex,
6201 printf("Switching work tree to %s\n",
6202 got_ref_get_symref_target(new_base_branch));
6203 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6204 new_base_branch, update_progress, &did_something);
6207 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6208 goto done; /* nothing else to do */
6211 if (continue_rebase) {
6212 if (!rebase_in_progress) {
6213 error = got_error(GOT_ERR_NOT_REBASING);
6216 error = got_worktree_rebase_continue(&resume_commit_id,
6217 &new_base_branch, &tmp_branch, &branch, &fileindex,
6222 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6223 resume_commit_id, repo);
6227 yca_id = got_object_id_dup(resume_commit_id);
6228 if (yca_id == NULL) {
6229 error = got_error_from_errno("got_object_id_dup");
6233 error = got_ref_open(&branch, repo, argv[0], 0);
6238 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6242 if (!continue_rebase) {
6243 struct got_object_id *base_commit_id;
6245 base_commit_id = got_worktree_get_base_commit_id(worktree);
6246 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6247 base_commit_id, branch_head_commit_id, repo,
6248 check_cancelled, NULL);
6251 if (yca_id == NULL) {
6252 error = got_error_msg(GOT_ERR_ANCESTRY,
6253 "specified branch shares no common ancestry "
6254 "with work tree's branch");
6258 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6260 if (error->code != GOT_ERR_ANCESTRY)
6264 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6265 "specified branch resolves to a commit which "
6266 "is already contained in work tree's branch");
6269 error = got_worktree_rebase_prepare(&new_base_branch,
6270 &tmp_branch, &fileindex, worktree, branch, repo);
6275 commit_id = branch_head_commit_id;
6276 error = got_object_open_as_commit(&commit, repo, commit_id);
6280 parent_ids = got_object_commit_get_parent_ids(commit);
6281 pid = SIMPLEQ_FIRST(parent_ids);
6283 if (!continue_rebase) {
6285 error = got_worktree_rebase_abort(worktree, fileindex,
6286 repo, new_base_branch, update_progress,
6290 printf("Rebase of %s aborted\n",
6291 got_ref_get_name(branch));
6293 error = got_error(GOT_ERR_EMPTY_REBASE);
6296 error = collect_commits(&commits, commit_id, pid->id,
6297 yca_id, got_worktree_get_path_prefix(worktree),
6298 GOT_ERR_REBASE_PATH, repo);
6299 got_object_commit_close(commit);
6304 if (SIMPLEQ_EMPTY(&commits)) {
6305 if (continue_rebase) {
6306 error = rebase_complete(worktree, fileindex,
6307 branch, new_base_branch, tmp_branch, repo);
6310 /* Fast-forward the reference of the branch. */
6311 struct got_object_id *new_head_commit_id;
6313 error = got_ref_resolve(&new_head_commit_id, repo,
6317 error = got_object_id_str(&id_str, new_head_commit_id);
6318 printf("Forwarding %s to commit %s\n",
6319 got_ref_get_name(branch), id_str);
6321 error = got_ref_change_ref(branch,
6322 new_head_commit_id);
6329 SIMPLEQ_FOREACH(qid, &commits, entry) {
6330 commit_id = qid->id;
6331 parent_id = pid ? pid->id : yca_id;
6334 error = got_worktree_rebase_merge_files(&merged_paths,
6335 worktree, fileindex, parent_id, commit_id, repo,
6336 rebase_progress, &rebase_status, check_cancelled, NULL);
6340 if (rebase_status == GOT_STATUS_CONFLICT) {
6341 error = show_rebase_merge_conflict(qid->id, repo);
6344 got_worktree_rebase_pathlist_free(&merged_paths);
6348 error = rebase_commit(&merged_paths, worktree, fileindex,
6349 tmp_branch, commit_id, repo);
6350 got_worktree_rebase_pathlist_free(&merged_paths);
6355 if (rebase_status == GOT_STATUS_CONFLICT) {
6356 error = got_worktree_rebase_postpone(worktree, fileindex);
6359 error = got_error_msg(GOT_ERR_CONFLICTS,
6360 "conflicts must be resolved before rebasing can continue");
6362 error = rebase_complete(worktree, fileindex, branch,
6363 new_base_branch, tmp_branch, repo);
6365 got_object_id_queue_free(&commits);
6366 free(branch_head_commit_id);
6367 free(resume_commit_id);
6370 got_object_commit_close(commit);
6372 got_ref_close(branch);
6373 if (new_base_branch)
6374 got_ref_close(new_base_branch);
6376 got_ref_close(tmp_branch);
6378 got_worktree_close(worktree);
6380 got_repo_close(repo);
6385 usage_histedit(void)
6387 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6392 #define GOT_HISTEDIT_PICK 'p'
6393 #define GOT_HISTEDIT_EDIT 'e'
6394 #define GOT_HISTEDIT_FOLD 'f'
6395 #define GOT_HISTEDIT_DROP 'd'
6396 #define GOT_HISTEDIT_MESG 'm'
6398 static struct got_histedit_cmd {
6402 } got_histedit_cmds[] = {
6403 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6404 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6405 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6407 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6408 { GOT_HISTEDIT_MESG, "mesg",
6409 "single-line log message for commit above (open editor if empty)" },
6412 struct got_histedit_list_entry {
6413 TAILQ_ENTRY(got_histedit_list_entry) entry;
6414 struct got_object_id *commit_id;
6415 const struct got_histedit_cmd *cmd;
6418 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6420 static const struct got_error *
6421 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6422 FILE *f, struct got_repository *repo)
6424 const struct got_error *err = NULL;
6425 char *logmsg = NULL, *id_str = NULL;
6426 struct got_commit_object *commit = NULL;
6429 err = got_object_open_as_commit(&commit, repo, commit_id);
6433 err = get_short_logmsg(&logmsg, 34, commit);
6437 err = got_object_id_str(&id_str, commit_id);
6441 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6443 err = got_ferror(f, GOT_ERR_IO);
6446 got_object_commit_close(commit);
6452 static const struct got_error *
6453 histedit_write_commit_list(struct got_object_id_queue *commits,
6454 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6456 const struct got_error *err = NULL;
6457 struct got_object_qid *qid;
6459 if (SIMPLEQ_EMPTY(commits))
6460 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6462 SIMPLEQ_FOREACH(qid, commits, entry) {
6463 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6467 if (edit_logmsg_only) {
6468 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6470 err = got_ferror(f, GOT_ERR_IO);
6479 static const struct got_error *
6480 write_cmd_list(FILE *f, const char *branch_name,
6481 struct got_object_id_queue *commits)
6483 const struct got_error *err = NULL;
6486 struct got_object_qid *qid;
6488 qid = SIMPLEQ_FIRST(commits);
6489 err = got_object_id_str(&id_str, qid->id);
6494 "# Editing the history of branch '%s' starting at\n"
6496 "# Commits will be processed in order from top to "
6497 "bottom of this file.\n", branch_name, id_str);
6499 err = got_ferror(f, GOT_ERR_IO);
6503 n = fprintf(f, "# Available histedit commands:\n");
6505 err = got_ferror(f, GOT_ERR_IO);
6509 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6510 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6511 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6514 err = got_ferror(f, GOT_ERR_IO);
6523 static const struct got_error *
6524 histedit_syntax_error(int lineno)
6526 static char msg[42];
6529 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6531 if (ret == -1 || ret >= sizeof(msg))
6532 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6534 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6537 static const struct got_error *
6538 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6539 char *logmsg, struct got_repository *repo)
6541 const struct got_error *err;
6542 struct got_commit_object *folded_commit = NULL;
6543 char *id_str, *folded_logmsg = NULL;
6545 err = got_object_id_str(&id_str, hle->commit_id);
6549 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6553 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6556 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6557 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6558 folded_logmsg) == -1) {
6559 err = got_error_from_errno("asprintf");
6563 got_object_commit_close(folded_commit);
6565 free(folded_logmsg);
6569 static struct got_histedit_list_entry *
6570 get_folded_commits(struct got_histedit_list_entry *hle)
6572 struct got_histedit_list_entry *prev, *folded = NULL;
6574 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6575 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6576 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6577 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6579 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6585 static const struct got_error *
6586 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6587 struct got_repository *repo)
6589 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6590 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6591 const struct got_error *err = NULL;
6592 struct got_commit_object *commit = NULL;
6594 struct got_histedit_list_entry *folded = NULL;
6596 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6600 folded = get_folded_commits(hle);
6602 while (folded != hle) {
6603 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6604 folded = TAILQ_NEXT(folded, entry);
6607 err = append_folded_commit_msg(&new_msg, folded,
6613 folded = TAILQ_NEXT(folded, entry);
6617 err = got_object_id_str(&id_str, hle->commit_id);
6620 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6623 if (asprintf(&new_msg,
6624 "%s\n# original log message of commit %s: %s",
6625 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6626 err = got_error_from_errno("asprintf");
6632 err = got_object_id_str(&id_str, hle->commit_id);
6636 err = got_opentemp_named_fd(&logmsg_path, &fd,
6637 GOT_TMPDIR_STR "/got-logmsg");
6641 dprintf(fd, logmsg);
6644 err = get_editor(&editor);
6648 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6650 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6652 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6655 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6656 err = got_error_from_errno2("unlink", logmsg_path);
6662 got_object_commit_close(commit);
6666 static const struct got_error *
6667 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6668 FILE *f, struct got_repository *repo)
6670 const struct got_error *err = NULL;
6671 char *line = NULL, *p, *end;
6675 const struct got_histedit_cmd *cmd;
6676 struct got_object_id *commit_id = NULL;
6677 struct got_histedit_list_entry *hle = NULL;
6680 len = getline(&line, &size, f);
6682 const struct got_error *getline_err;
6685 getline_err = got_error_from_errno("getline");
6686 err = got_ferror(f, getline_err->code);
6691 while (isspace((unsigned char)p[0]))
6693 if (p[0] == '#' || p[0] == '\0') {
6699 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6700 cmd = &got_histedit_cmds[i];
6701 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6702 isspace((unsigned char)p[strlen(cmd->name)])) {
6703 p += strlen(cmd->name);
6706 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6711 if (i == nitems(got_histedit_cmds)) {
6712 err = histedit_syntax_error(lineno);
6715 while (isspace((unsigned char)p[0]))
6717 if (cmd->code == GOT_HISTEDIT_MESG) {
6718 if (hle == NULL || hle->logmsg != NULL) {
6719 err = got_error(GOT_ERR_HISTEDIT_CMD);
6723 err = histedit_edit_logmsg(hle, repo);
6727 hle->logmsg = strdup(p);
6728 if (hle->logmsg == NULL) {
6729 err = got_error_from_errno("strdup");
6738 while (end[0] && !isspace((unsigned char)end[0]))
6742 err = got_object_resolve_id_str(&commit_id, repo, p);
6744 /* override error code */
6745 err = histedit_syntax_error(lineno);
6749 hle = malloc(sizeof(*hle));
6751 err = got_error_from_errno("malloc");
6755 hle->commit_id = commit_id;
6760 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6768 static const struct got_error *
6769 histedit_check_script(struct got_histedit_list *histedit_cmds,
6770 struct got_object_id_queue *commits, struct got_repository *repo)
6772 const struct got_error *err = NULL;
6773 struct got_object_qid *qid;
6774 struct got_histedit_list_entry *hle;
6775 static char msg[92];
6778 if (TAILQ_EMPTY(histedit_cmds))
6779 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6780 "histedit script contains no commands");
6781 if (SIMPLEQ_EMPTY(commits))
6782 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6784 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6785 struct got_histedit_list_entry *hle2;
6786 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6789 if (got_object_id_cmp(hle->commit_id,
6790 hle2->commit_id) != 0)
6792 err = got_object_id_str(&id_str, hle->commit_id);
6795 snprintf(msg, sizeof(msg), "commit %s is listed "
6796 "more than once in histedit script", id_str);
6798 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6802 SIMPLEQ_FOREACH(qid, commits, entry) {
6803 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6804 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6808 err = got_object_id_str(&id_str, qid->id);
6811 snprintf(msg, sizeof(msg),
6812 "commit %s missing from histedit script", id_str);
6814 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6818 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6819 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6820 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6821 "last commit in histedit script cannot be folded");
6826 static const struct got_error *
6827 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6828 const char *path, struct got_object_id_queue *commits,
6829 struct got_repository *repo)
6831 const struct got_error *err = NULL;
6835 err = get_editor(&editor);
6839 if (spawn_editor(editor, path) == -1) {
6840 err = got_error_from_errno("failed spawning editor");
6844 f = fopen(path, "r");
6846 err = got_error_from_errno("fopen");
6849 err = histedit_parse_list(histedit_cmds, f, repo);
6853 err = histedit_check_script(histedit_cmds, commits, repo);
6855 if (f && fclose(f) != 0 && err == NULL)
6856 err = got_error_from_errno("fclose");
6861 static const struct got_error *
6862 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6863 struct got_object_id_queue *, const char *, const char *,
6864 struct got_repository *);
6866 static const struct got_error *
6867 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6868 struct got_object_id_queue *commits, const char *branch_name,
6869 int edit_logmsg_only, struct got_repository *repo)
6871 const struct got_error *err;
6875 err = got_opentemp_named(&path, &f, "got-histedit");
6879 err = write_cmd_list(f, branch_name, commits);
6883 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6887 if (edit_logmsg_only) {
6889 err = histedit_parse_list(histedit_cmds, f, repo);
6891 if (fclose(f) != 0) {
6892 err = got_error_from_errno("fclose");
6896 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6898 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6899 err->code != GOT_ERR_HISTEDIT_CMD)
6901 err = histedit_edit_list_retry(histedit_cmds, err,
6902 commits, path, branch_name, repo);
6906 if (f && fclose(f) != 0 && err == NULL)
6907 err = got_error_from_errno("fclose");
6908 if (path && unlink(path) != 0 && err == NULL)
6909 err = got_error_from_errno2("unlink", path);
6914 static const struct got_error *
6915 histedit_save_list(struct got_histedit_list *histedit_cmds,
6916 struct got_worktree *worktree, struct got_repository *repo)
6918 const struct got_error *err = NULL;
6921 struct got_histedit_list_entry *hle;
6922 struct got_commit_object *commit = NULL;
6924 err = got_worktree_get_histedit_script_path(&path, worktree);
6928 f = fopen(path, "w");
6930 err = got_error_from_errno2("fopen", path);
6933 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6934 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6940 int n = fprintf(f, "%c %s\n",
6941 GOT_HISTEDIT_MESG, hle->logmsg);
6943 err = got_ferror(f, GOT_ERR_IO);
6949 if (f && fclose(f) != 0 && err == NULL)
6950 err = got_error_from_errno("fclose");
6953 got_object_commit_close(commit);
6958 histedit_free_list(struct got_histedit_list *histedit_cmds)
6960 struct got_histedit_list_entry *hle;
6962 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6963 TAILQ_REMOVE(histedit_cmds, hle, entry);
6968 static const struct got_error *
6969 histedit_load_list(struct got_histedit_list *histedit_cmds,
6970 const char *path, struct got_repository *repo)
6972 const struct got_error *err = NULL;
6975 f = fopen(path, "r");
6977 err = got_error_from_errno2("fopen", path);
6981 err = histedit_parse_list(histedit_cmds, f, repo);
6983 if (f && fclose(f) != 0 && err == NULL)
6984 err = got_error_from_errno("fclose");
6988 static const struct got_error *
6989 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6990 const struct got_error *edit_err, struct got_object_id_queue *commits,
6991 const char *path, const char *branch_name, struct got_repository *repo)
6993 const struct got_error *err = NULL, *prev_err = edit_err;
6996 while (resp != 'c' && resp != 'r' && resp != 'a') {
6997 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6998 "or (a)bort: ", getprogname(), prev_err->msg);
7003 histedit_free_list(histedit_cmds);
7004 err = histedit_run_editor(histedit_cmds, path, commits,
7007 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7008 err->code != GOT_ERR_HISTEDIT_CMD)
7015 } else if (resp == 'r') {
7016 histedit_free_list(histedit_cmds);
7017 err = histedit_edit_script(histedit_cmds,
7018 commits, branch_name, 0, repo);
7020 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7021 err->code != GOT_ERR_HISTEDIT_CMD)
7028 } else if (resp == 'a') {
7029 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7032 printf("invalid response '%c'\n", resp);
7038 static const struct got_error *
7039 histedit_complete(struct got_worktree *worktree,
7040 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7041 struct got_reference *branch, struct got_repository *repo)
7043 printf("Switching work tree to %s\n",
7044 got_ref_get_symref_target(branch));
7045 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7049 static const struct got_error *
7050 show_histedit_progress(struct got_commit_object *commit,
7051 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7053 const struct got_error *err;
7054 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7056 err = got_object_id_str(&old_id_str, hle->commit_id);
7061 err = got_object_id_str(&new_id_str, new_id);
7066 old_id_str[12] = '\0';
7068 new_id_str[12] = '\0';
7071 logmsg = strdup(hle->logmsg);
7072 if (logmsg == NULL) {
7073 err = got_error_from_errno("strdup");
7076 trim_logmsg(logmsg, 42);
7078 err = get_short_logmsg(&logmsg, 42, commit);
7083 switch (hle->cmd->code) {
7084 case GOT_HISTEDIT_PICK:
7085 case GOT_HISTEDIT_EDIT:
7086 printf("%s -> %s: %s\n", old_id_str,
7087 new_id_str ? new_id_str : "no-op change", logmsg);
7089 case GOT_HISTEDIT_DROP:
7090 case GOT_HISTEDIT_FOLD:
7091 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7103 static const struct got_error *
7104 histedit_commit(struct got_pathlist_head *merged_paths,
7105 struct got_worktree *worktree, struct got_fileindex *fileindex,
7106 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7107 struct got_repository *repo)
7109 const struct got_error *err;
7110 struct got_commit_object *commit;
7111 struct got_object_id *new_commit_id;
7113 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7114 && hle->logmsg == NULL) {
7115 err = histedit_edit_logmsg(hle, repo);
7120 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7124 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7125 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7128 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7130 err = show_histedit_progress(commit, hle, NULL);
7132 err = show_histedit_progress(commit, hle, new_commit_id);
7133 free(new_commit_id);
7136 got_object_commit_close(commit);
7140 static const struct got_error *
7141 histedit_skip_commit(struct got_histedit_list_entry *hle,
7142 struct got_worktree *worktree, struct got_repository *repo)
7144 const struct got_error *error;
7145 struct got_commit_object *commit;
7147 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7152 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7156 error = show_histedit_progress(commit, hle, NULL);
7157 got_object_commit_close(commit);
7161 static const struct got_error *
7162 check_local_changes(void *arg, unsigned char status,
7163 unsigned char staged_status, const char *path,
7164 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7165 struct got_object_id *commit_id, int dirfd, const char *de_name)
7167 int *have_local_changes = arg;
7170 case GOT_STATUS_ADD:
7171 case GOT_STATUS_DELETE:
7172 case GOT_STATUS_MODIFY:
7173 case GOT_STATUS_CONFLICT:
7174 *have_local_changes = 1;
7175 return got_error(GOT_ERR_CANCELLED);
7180 switch (staged_status) {
7181 case GOT_STATUS_ADD:
7182 case GOT_STATUS_DELETE:
7183 case GOT_STATUS_MODIFY:
7184 *have_local_changes = 1;
7185 return got_error(GOT_ERR_CANCELLED);
7193 static const struct got_error *
7194 cmd_histedit(int argc, char *argv[])
7196 const struct got_error *error = NULL;
7197 struct got_worktree *worktree = NULL;
7198 struct got_fileindex *fileindex = NULL;
7199 struct got_repository *repo = NULL;
7201 struct got_reference *branch = NULL;
7202 struct got_reference *tmp_branch = NULL;
7203 struct got_object_id *resume_commit_id = NULL;
7204 struct got_object_id *base_commit_id = NULL;
7205 struct got_object_id *head_commit_id = NULL;
7206 struct got_commit_object *commit = NULL;
7207 int ch, rebase_in_progress = 0, did_something;
7208 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7209 int edit_logmsg_only = 0;
7210 const char *edit_script_path = NULL;
7211 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7212 struct got_object_id_queue commits;
7213 struct got_pathlist_head merged_paths;
7214 const struct got_object_id_queue *parent_ids;
7215 struct got_object_qid *pid;
7216 struct got_histedit_list histedit_cmds;
7217 struct got_histedit_list_entry *hle;
7219 SIMPLEQ_INIT(&commits);
7220 TAILQ_INIT(&histedit_cmds);
7221 TAILQ_INIT(&merged_paths);
7223 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7232 edit_script_path = optarg;
7235 edit_logmsg_only = 1;
7247 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7248 "unveil", NULL) == -1)
7251 if (abort_edit && continue_edit)
7252 errx(1, "histedit's -a and -c options are mutually exclusive");
7253 if (edit_script_path && edit_logmsg_only)
7254 errx(1, "histedit's -F and -m options are mutually exclusive");
7255 if (abort_edit && edit_logmsg_only)
7256 errx(1, "histedit's -a and -m options are mutually exclusive");
7257 if (continue_edit && edit_logmsg_only)
7258 errx(1, "histedit's -c and -m options are mutually exclusive");
7263 * This command cannot apply unveil(2) in all cases because the
7264 * user may choose to run an editor to edit the histedit script
7265 * and to edit individual commit log messages.
7266 * unveil(2) traverses exec(2); if an editor is used we have to
7267 * apply unveil after edit script and log messages have been written.
7268 * XXX TODO: Make use of unveil(2) where possible.
7271 cwd = getcwd(NULL, 0);
7273 error = got_error_from_errno("getcwd");
7276 error = got_worktree_open(&worktree, cwd);
7280 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7285 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7288 if (rebase_in_progress) {
7289 error = got_error(GOT_ERR_REBASING);
7293 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7297 if (edit_in_progress && edit_logmsg_only) {
7298 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7299 "histedit operation is in progress in this "
7300 "work tree and must be continued or aborted "
7301 "before the -m option can be used");
7305 if (edit_in_progress && abort_edit) {
7306 error = got_worktree_histedit_continue(&resume_commit_id,
7307 &tmp_branch, &branch, &base_commit_id, &fileindex,
7311 printf("Switching work tree to %s\n",
7312 got_ref_get_symref_target(branch));
7313 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7314 branch, base_commit_id, update_progress, &did_something);
7317 printf("Histedit of %s aborted\n",
7318 got_ref_get_symref_target(branch));
7319 goto done; /* nothing else to do */
7320 } else if (abort_edit) {
7321 error = got_error(GOT_ERR_NOT_HISTEDIT);
7325 if (continue_edit) {
7328 if (!edit_in_progress) {
7329 error = got_error(GOT_ERR_NOT_HISTEDIT);
7333 error = got_worktree_get_histedit_script_path(&path, worktree);
7337 error = histedit_load_list(&histedit_cmds, path, repo);
7342 error = got_worktree_histedit_continue(&resume_commit_id,
7343 &tmp_branch, &branch, &base_commit_id, &fileindex,
7348 error = got_ref_resolve(&head_commit_id, repo, branch);
7352 error = got_object_open_as_commit(&commit, repo,
7356 parent_ids = got_object_commit_get_parent_ids(commit);
7357 pid = SIMPLEQ_FIRST(parent_ids);
7359 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7362 error = collect_commits(&commits, head_commit_id, pid->id,
7363 base_commit_id, got_worktree_get_path_prefix(worktree),
7364 GOT_ERR_HISTEDIT_PATH, repo);
7365 got_object_commit_close(commit);
7370 if (edit_in_progress) {
7371 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7375 error = got_ref_open(&branch, repo,
7376 got_worktree_get_head_ref_name(worktree), 0);
7380 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7381 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7382 "will not edit commit history of a branch outside "
7383 "the \"refs/heads/\" reference namespace");
7387 error = got_ref_resolve(&head_commit_id, repo, branch);
7388 got_ref_close(branch);
7393 error = got_object_open_as_commit(&commit, repo,
7397 parent_ids = got_object_commit_get_parent_ids(commit);
7398 pid = SIMPLEQ_FIRST(parent_ids);
7400 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7403 error = collect_commits(&commits, head_commit_id, pid->id,
7404 got_worktree_get_base_commit_id(worktree),
7405 got_worktree_get_path_prefix(worktree),
7406 GOT_ERR_HISTEDIT_PATH, repo);
7407 got_object_commit_close(commit);
7412 if (SIMPLEQ_EMPTY(&commits)) {
7413 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7417 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7418 &base_commit_id, &fileindex, worktree, repo);
7422 if (edit_script_path) {
7423 error = histedit_load_list(&histedit_cmds,
7424 edit_script_path, repo);
7426 got_worktree_histedit_abort(worktree, fileindex,
7427 repo, branch, base_commit_id,
7428 update_progress, &did_something);
7432 const char *branch_name;
7433 branch_name = got_ref_get_symref_target(branch);
7434 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7436 error = histedit_edit_script(&histedit_cmds, &commits,
7437 branch_name, edit_logmsg_only, repo);
7439 got_worktree_histedit_abort(worktree, fileindex,
7440 repo, branch, base_commit_id,
7441 update_progress, &did_something);
7447 error = histedit_save_list(&histedit_cmds, worktree,
7450 got_worktree_histedit_abort(worktree, fileindex,
7451 repo, branch, base_commit_id,
7452 update_progress, &did_something);
7458 error = histedit_check_script(&histedit_cmds, &commits, repo);
7462 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7463 if (resume_commit_id) {
7464 if (got_object_id_cmp(hle->commit_id,
7465 resume_commit_id) != 0)
7468 resume_commit_id = NULL;
7469 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7470 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7471 error = histedit_skip_commit(hle, worktree,
7476 struct got_pathlist_head paths;
7477 int have_changes = 0;
7480 error = got_pathlist_append(&paths, "", NULL);
7483 error = got_worktree_status(worktree, &paths,
7484 repo, check_local_changes, &have_changes,
7485 check_cancelled, NULL);
7486 got_pathlist_free(&paths);
7488 if (error->code != GOT_ERR_CANCELLED)
7490 if (sigint_received || sigpipe_received)
7494 error = histedit_commit(NULL, worktree,
7495 fileindex, tmp_branch, hle, repo);
7499 error = got_object_open_as_commit(
7500 &commit, repo, hle->commit_id);
7503 error = show_histedit_progress(commit,
7505 got_object_commit_close(commit);
7514 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7515 error = histedit_skip_commit(hle, worktree, repo);
7521 error = got_object_open_as_commit(&commit, repo,
7525 parent_ids = got_object_commit_get_parent_ids(commit);
7526 pid = SIMPLEQ_FIRST(parent_ids);
7528 error = got_worktree_histedit_merge_files(&merged_paths,
7529 worktree, fileindex, pid->id, hle->commit_id, repo,
7530 rebase_progress, &rebase_status, check_cancelled, NULL);
7533 got_object_commit_close(commit);
7536 if (rebase_status == GOT_STATUS_CONFLICT) {
7537 error = show_rebase_merge_conflict(hle->commit_id,
7541 got_worktree_rebase_pathlist_free(&merged_paths);
7545 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7547 error = got_object_id_str(&id_str, hle->commit_id);
7550 printf("Stopping histedit for amending commit %s\n",
7553 got_worktree_rebase_pathlist_free(&merged_paths);
7554 error = got_worktree_histedit_postpone(worktree,
7559 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7560 error = histedit_skip_commit(hle, worktree, repo);
7566 error = histedit_commit(&merged_paths, worktree, fileindex,
7567 tmp_branch, hle, repo);
7568 got_worktree_rebase_pathlist_free(&merged_paths);
7573 if (rebase_status == GOT_STATUS_CONFLICT) {
7574 error = got_worktree_histedit_postpone(worktree, fileindex);
7577 error = got_error_msg(GOT_ERR_CONFLICTS,
7578 "conflicts must be resolved before histedit can continue");
7580 error = histedit_complete(worktree, fileindex, tmp_branch,
7583 got_object_id_queue_free(&commits);
7584 histedit_free_list(&histedit_cmds);
7585 free(head_commit_id);
7586 free(base_commit_id);
7587 free(resume_commit_id);
7589 got_object_commit_close(commit);
7591 got_ref_close(branch);
7593 got_ref_close(tmp_branch);
7595 got_worktree_close(worktree);
7597 got_repo_close(repo);
7602 usage_integrate(void)
7604 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7608 static const struct got_error *
7609 cmd_integrate(int argc, char *argv[])
7611 const struct got_error *error = NULL;
7612 struct got_repository *repo = NULL;
7613 struct got_worktree *worktree = NULL;
7614 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7615 const char *branch_arg = NULL;
7616 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7617 struct got_fileindex *fileindex = NULL;
7618 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7619 int ch, did_something = 0;
7621 while ((ch = getopt(argc, argv, "")) != -1) {
7634 branch_arg = argv[0];
7636 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7637 "unveil", NULL) == -1)
7640 cwd = getcwd(NULL, 0);
7642 error = got_error_from_errno("getcwd");
7646 error = got_worktree_open(&worktree, cwd);
7650 error = check_rebase_or_histedit_in_progress(worktree);
7654 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7659 error = apply_unveil(got_repo_get_path(repo), 0,
7660 got_worktree_get_root_path(worktree));
7664 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7665 error = got_error_from_errno("asprintf");
7669 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7670 &base_branch_ref, worktree, refname, repo);
7674 refname = strdup(got_ref_get_name(branch_ref));
7675 if (refname == NULL) {
7676 error = got_error_from_errno("strdup");
7677 got_worktree_integrate_abort(worktree, fileindex, repo,
7678 branch_ref, base_branch_ref);
7681 base_refname = strdup(got_ref_get_name(base_branch_ref));
7682 if (base_refname == NULL) {
7683 error = got_error_from_errno("strdup");
7684 got_worktree_integrate_abort(worktree, fileindex, repo,
7685 branch_ref, base_branch_ref);
7689 error = got_ref_resolve(&commit_id, repo, branch_ref);
7693 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7697 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7698 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7699 "specified branch has already been integrated");
7700 got_worktree_integrate_abort(worktree, fileindex, repo,
7701 branch_ref, base_branch_ref);
7705 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7707 if (error->code == GOT_ERR_ANCESTRY)
7708 error = got_error(GOT_ERR_REBASE_REQUIRED);
7709 got_worktree_integrate_abort(worktree, fileindex, repo,
7710 branch_ref, base_branch_ref);
7714 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7715 branch_ref, base_branch_ref, update_progress, &did_something,
7716 check_cancelled, NULL);
7720 printf("Integrated %s into %s\n", refname, base_refname);
7723 got_repo_close(repo);
7725 got_worktree_close(worktree);
7727 free(base_commit_id);
7737 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7738 "[file-path ...]\n",
7743 static const struct got_error *
7744 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7745 const char *path, struct got_object_id *blob_id,
7746 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7747 int dirfd, const char *de_name)
7749 const struct got_error *err = NULL;
7750 char *id_str = NULL;
7752 if (staged_status != GOT_STATUS_ADD &&
7753 staged_status != GOT_STATUS_MODIFY &&
7754 staged_status != GOT_STATUS_DELETE)
7757 if (staged_status == GOT_STATUS_ADD ||
7758 staged_status == GOT_STATUS_MODIFY)
7759 err = got_object_id_str(&id_str, staged_blob_id);
7761 err = got_object_id_str(&id_str, blob_id);
7765 printf("%s %c %s\n", id_str, staged_status, path);
7770 static const struct got_error *
7771 cmd_stage(int argc, char *argv[])
7773 const struct got_error *error = NULL;
7774 struct got_repository *repo = NULL;
7775 struct got_worktree *worktree = NULL;
7777 struct got_pathlist_head paths;
7778 struct got_pathlist_entry *pe;
7779 int ch, list_stage = 0, pflag = 0;
7780 FILE *patch_script_file = NULL;
7781 const char *patch_script_path = NULL;
7782 struct choose_patch_arg cpa;
7786 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7795 patch_script_path = optarg;
7807 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7808 "unveil", NULL) == -1)
7811 if (list_stage && (pflag || patch_script_path))
7812 errx(1, "-l option cannot be used with other options");
7813 if (patch_script_path && !pflag)
7814 errx(1, "-F option can only be used together with -p option");
7816 cwd = getcwd(NULL, 0);
7818 error = got_error_from_errno("getcwd");
7822 error = got_worktree_open(&worktree, cwd);
7826 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7831 if (patch_script_path) {
7832 patch_script_file = fopen(patch_script_path, "r");
7833 if (patch_script_file == NULL) {
7834 error = got_error_from_errno2("fopen",
7839 error = apply_unveil(got_repo_get_path(repo), 0,
7840 got_worktree_get_root_path(worktree));
7844 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7849 error = got_worktree_status(worktree, &paths, repo,
7850 print_stage, NULL, check_cancelled, NULL);
7852 cpa.patch_script_file = patch_script_file;
7853 cpa.action = "stage";
7854 error = got_worktree_stage(worktree, &paths,
7855 pflag ? NULL : print_status, NULL,
7856 pflag ? choose_patch : NULL, &cpa, repo);
7859 if (patch_script_file && fclose(patch_script_file) == EOF &&
7861 error = got_error_from_errno2("fclose", patch_script_path);
7863 got_repo_close(repo);
7865 got_worktree_close(worktree);
7866 TAILQ_FOREACH(pe, &paths, entry)
7867 free((char *)pe->path);
7868 got_pathlist_free(&paths);
7876 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7877 "[file-path ...]\n",
7883 static const struct got_error *
7884 cmd_unstage(int argc, char *argv[])
7886 const struct got_error *error = NULL;
7887 struct got_repository *repo = NULL;
7888 struct got_worktree *worktree = NULL;
7890 struct got_pathlist_head paths;
7891 struct got_pathlist_entry *pe;
7892 int ch, did_something = 0, pflag = 0;
7893 FILE *patch_script_file = NULL;
7894 const char *patch_script_path = NULL;
7895 struct choose_patch_arg cpa;
7899 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7905 patch_script_path = optarg;
7917 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7918 "unveil", NULL) == -1)
7921 if (patch_script_path && !pflag)
7922 errx(1, "-F option can only be used together with -p option");
7924 cwd = getcwd(NULL, 0);
7926 error = got_error_from_errno("getcwd");
7930 error = got_worktree_open(&worktree, cwd);
7934 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7939 if (patch_script_path) {
7940 patch_script_file = fopen(patch_script_path, "r");
7941 if (patch_script_file == NULL) {
7942 error = got_error_from_errno2("fopen",
7948 error = apply_unveil(got_repo_get_path(repo), 0,
7949 got_worktree_get_root_path(worktree));
7953 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7957 cpa.patch_script_file = patch_script_file;
7958 cpa.action = "unstage";
7959 error = got_worktree_unstage(worktree, &paths, update_progress,
7960 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7962 if (patch_script_file && fclose(patch_script_file) == EOF &&
7964 error = got_error_from_errno2("fclose", patch_script_path);
7966 got_repo_close(repo);
7968 got_worktree_close(worktree);
7969 TAILQ_FOREACH(pe, &paths, entry)
7970 free((char *)pe->path);
7971 got_pathlist_free(&paths);
7979 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7980 "arg1 [arg2 ...]\n", getprogname());
7984 static const struct got_error *
7985 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7987 const struct got_error *err;
7988 struct got_blob_object *blob;
7990 err = got_object_open_as_blob(&blob, repo, id, 8192);
7994 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7995 got_object_blob_close(blob);
7999 static const struct got_error *
8000 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8002 const struct got_error *err;
8003 struct got_tree_object *tree;
8006 err = got_object_open_as_tree(&tree, repo, id);
8010 nentries = got_object_tree_get_nentries(tree);
8011 for (i = 0; i < nentries; i++) {
8012 struct got_tree_entry *te;
8014 if (sigint_received || sigpipe_received)
8016 te = got_object_tree_get_entry(tree, i);
8017 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8020 fprintf(outfile, "%s %.7o %s\n", id_str,
8021 got_tree_entry_get_mode(te),
8022 got_tree_entry_get_name(te));
8026 got_object_tree_close(tree);
8030 static const struct got_error *
8031 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8033 const struct got_error *err;
8034 struct got_commit_object *commit;
8035 const struct got_object_id_queue *parent_ids;
8036 struct got_object_qid *pid;
8037 char *id_str = NULL;
8038 const char *logmsg = NULL;
8040 err = got_object_open_as_commit(&commit, repo, id);
8044 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8048 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8049 parent_ids = got_object_commit_get_parent_ids(commit);
8050 fprintf(outfile, "numparents %d\n",
8051 got_object_commit_get_nparents(commit));
8052 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8054 err = got_object_id_str(&pid_str, pid->id);
8057 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8060 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8061 got_object_commit_get_author(commit),
8062 got_object_commit_get_author_time(commit));
8064 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8065 got_object_commit_get_author(commit),
8066 got_object_commit_get_committer_time(commit));
8068 logmsg = got_object_commit_get_logmsg_raw(commit);
8069 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8070 fprintf(outfile, "%s", logmsg);
8073 got_object_commit_close(commit);
8077 static const struct got_error *
8078 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8080 const struct got_error *err;
8081 struct got_tag_object *tag;
8082 char *id_str = NULL;
8083 const char *tagmsg = NULL;
8085 err = got_object_open_as_tag(&tag, repo, id);
8089 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8093 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8095 switch (got_object_tag_get_object_type(tag)) {
8096 case GOT_OBJ_TYPE_BLOB:
8097 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8098 GOT_OBJ_LABEL_BLOB);
8100 case GOT_OBJ_TYPE_TREE:
8101 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8102 GOT_OBJ_LABEL_TREE);
8104 case GOT_OBJ_TYPE_COMMIT:
8105 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8106 GOT_OBJ_LABEL_COMMIT);
8108 case GOT_OBJ_TYPE_TAG:
8109 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8116 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8117 got_object_tag_get_name(tag));
8119 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8120 got_object_tag_get_tagger(tag),
8121 got_object_tag_get_tagger_time(tag));
8123 tagmsg = got_object_tag_get_message(tag);
8124 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8125 fprintf(outfile, "%s", tagmsg);
8128 got_object_tag_close(tag);
8132 static const struct got_error *
8133 cmd_cat(int argc, char *argv[])
8135 const struct got_error *error;
8136 struct got_repository *repo = NULL;
8137 struct got_worktree *worktree = NULL;
8138 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8139 const char *commit_id_str = NULL;
8140 struct got_object_id *id = NULL, *commit_id = NULL;
8141 int ch, obj_type, i, force_path = 0;
8144 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8149 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8152 commit_id_str = optarg;
8155 repo_path = realpath(optarg, NULL);
8156 if (repo_path == NULL)
8157 return got_error_from_errno2("realpath",
8159 got_path_strip_trailing_slashes(repo_path);
8173 cwd = getcwd(NULL, 0);
8175 error = got_error_from_errno("getcwd");
8178 error = got_worktree_open(&worktree, cwd);
8179 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8182 if (repo_path == NULL) {
8184 got_worktree_get_repo_path(worktree));
8185 if (repo_path == NULL) {
8186 error = got_error_from_errno("strdup");
8192 if (repo_path == NULL) {
8193 repo_path = getcwd(NULL, 0);
8194 if (repo_path == NULL)
8195 return got_error_from_errno("getcwd");
8198 error = got_repo_open(&repo, repo_path, NULL);
8203 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8207 if (commit_id_str == NULL)
8208 commit_id_str = GOT_REF_HEAD;
8209 error = got_repo_match_object_id(&commit_id, NULL,
8210 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8214 for (i = 0; i < argc; i++) {
8216 error = got_object_id_by_path(&id, repo, commit_id,
8221 error = got_repo_match_object_id(&id, &label, argv[i],
8222 GOT_OBJ_TYPE_ANY, 0, repo);
8224 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8225 error->code != GOT_ERR_NOT_REF)
8227 error = got_object_id_by_path(&id, repo,
8228 commit_id, argv[i]);
8234 error = got_object_get_type(&obj_type, repo, id);
8239 case GOT_OBJ_TYPE_BLOB:
8240 error = cat_blob(id, repo, stdout);
8242 case GOT_OBJ_TYPE_TREE:
8243 error = cat_tree(id, repo, stdout);
8245 case GOT_OBJ_TYPE_COMMIT:
8246 error = cat_commit(id, repo, stdout);
8248 case GOT_OBJ_TYPE_TAG:
8249 error = cat_tag(id, repo, stdout);
8252 error = got_error(GOT_ERR_OBJ_TYPE);
8267 got_worktree_close(worktree);
8269 const struct got_error *repo_error;
8270 repo_error = got_repo_close(repo);