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 [-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;
909 TAILQ_INIT(&symrefs);
911 while ((ch = getopt(argc, argv, "vq")) != -1) {
916 else if (verbosity < 3)
939 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
940 &repo_name, argv[0]);
944 if (strcmp(proto, "git") == 0) {
946 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
947 "sendfd dns inet unveil", NULL) == -1)
950 git_url = strdup(argv[0]);
951 if (git_url == NULL) {
952 error = got_error_from_errno("strdup");
955 } else if (strcmp(proto, "git+ssh") == 0 ||
956 strcmp(proto, "ssh") == 0) {
958 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
959 "sendfd unveil", NULL) == -1)
962 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
963 error = got_error_from_errno("asprintf");
966 } else if (strcmp(proto, "http") == 0 ||
967 strcmp(proto, "git+http") == 0) {
968 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
971 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
974 if (dirname == NULL) {
975 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
976 error = got_error_from_errno("asprintf");
979 repo_path = default_destdir;
983 error = got_path_mkdir(repo_path);
987 error = got_repo_init(repo_path);
991 error = got_repo_open(&repo, repo_path, NULL);
995 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
996 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
997 error = got_error_from_errno2("unveil",
1002 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1006 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1012 printf("Connected to %s:%s\n", host, port);
1014 /* Create a config file git-fetch(1) can understand. */
1015 gitconfig_path = got_repo_get_path_gitconfig(repo);
1016 if (gitconfig_path == NULL) {
1017 error = got_error_from_errno("got_repo_get_path_gitconfig");
1020 gitconfig_file = fopen(gitconfig_path, "a");
1021 if (gitconfig_file == NULL) {
1022 error = got_error_from_errno2("fopen", gitconfig_path);
1025 if (asprintf(&gitconfig,
1028 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1029 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1030 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1031 error = got_error_from_errno("asprintf");
1034 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1035 if (n != strlen(gitconfig)) {
1036 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1040 fpa.last_scaled_size[0] = '\0';
1041 fpa.last_p_indexed = -1;
1042 fpa.last_p_resolved = -1;
1043 fpa.verbosity = verbosity;
1044 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1045 GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
1046 fetch_progress, &fpa);
1050 error = got_object_id_str(&id_str, pack_hash);
1054 printf("\nFetched %s.pack\n", id_str);
1057 /* Set up references provided with the pack file. */
1058 TAILQ_FOREACH(pe, &refs, entry) {
1059 const char *refname = pe->path;
1060 struct got_object_id *id = pe->data;
1061 struct got_reference *ref;
1062 char *remote_refname;
1064 error = got_ref_alloc(&ref, refname, id);
1067 error = got_ref_write(ref, repo);
1072 if (strncmp("refs/heads/", refname, 11) != 0)
1075 if (asprintf(&remote_refname,
1076 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1077 refname + 11) == -1) {
1078 error = got_error_from_errno("asprintf");
1081 error = got_ref_alloc(&ref, remote_refname, id);
1084 error = got_ref_write(ref, repo);
1090 /* Set the HEAD reference if the server provided one. */
1091 TAILQ_FOREACH(pe, &symrefs, entry) {
1092 struct got_reference *symref, *target_ref;
1093 const char *refname = pe->path;
1094 const char *target = pe->data;
1096 if (strcmp(refname, GOT_REF_HEAD) != 0)
1099 error = got_ref_open(&target_ref, repo, target, 0);
1101 if (error->code == GOT_ERR_NOT_REF)
1106 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1107 got_ref_close(target_ref);
1112 printf("Setting %s to %s\n", GOT_REF_HEAD,
1113 got_ref_get_symref_target(symref));
1115 error = got_ref_write(symref, repo);
1116 got_ref_close(symref);
1121 printf("Created cloned repository '%s'\n", repo_path);
1123 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1124 error = got_error_from_errno("close");
1125 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1126 error = got_error_from_errno("fclose");
1128 got_repo_close(repo);
1129 TAILQ_FOREACH(pe, &refs, entry) {
1130 free((void *)pe->path);
1133 got_pathlist_free(&refs);
1134 TAILQ_FOREACH(pe, &symrefs, entry) {
1135 free((void *)pe->path);
1138 got_pathlist_free(&symrefs);
1145 free(default_destdir);
1146 free(gitconfig_path);
1151 static const struct got_error *
1152 create_ref(const char *refname, struct got_object_id *id,
1153 const char *id_str, struct got_repository *repo)
1155 const struct got_error *err = NULL;
1156 struct got_reference *ref;
1158 printf("Creating %s: %s\n", refname, id_str);
1160 err = got_ref_alloc(&ref, refname, id);
1164 err = got_ref_write(ref, repo);
1169 static const struct got_error *
1170 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1171 struct got_repository *repo)
1173 const struct got_error *err = NULL;
1174 char *new_id_str = NULL;
1175 struct got_object_id *old_id = NULL;
1177 err = got_object_id_str(&new_id_str, new_id);
1181 if (got_ref_is_symbolic(ref)) {
1182 struct got_reference *new_ref;
1183 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1186 printf("Deleting symbolic reference %s -> %s\n",
1187 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1188 err = got_ref_delete(ref, repo);
1191 printf("Setting %s to %s\n", got_ref_get_name(ref),
1193 err = got_ref_write(new_ref, repo);
1197 err = got_ref_resolve(&old_id, repo, ref);
1200 if (got_object_id_cmp(old_id, new_id) != 0) {
1201 printf("Setting %s to %s\n",
1202 got_ref_get_name(ref), new_id_str);
1203 err = got_ref_change_ref(ref, new_id);
1206 err = got_ref_write(ref, repo);
1220 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1221 "[remote-repository-name]\n", getprogname());
1225 static const struct got_error *
1226 cmd_fetch(int argc, char *argv[])
1228 const struct got_error *error = NULL;
1229 char *cwd = NULL, *repo_path = NULL;
1230 const char *remote_name;
1231 char *proto = NULL, *host = NULL, *port = NULL;
1232 char *repo_name = NULL, *server_path = NULL;
1233 struct got_remote_repo *remotes, *remote = NULL;
1235 char *id_str = NULL;
1236 struct got_repository *repo = NULL;
1237 struct got_worktree *worktree = NULL;
1238 struct got_pathlist_head refs, symrefs;
1239 struct got_pathlist_entry *pe;
1240 struct got_object_id *pack_hash = NULL;
1241 int i, ch, fetchfd = -1;
1242 struct got_fetch_progress_arg fpa;
1246 TAILQ_INIT(&symrefs);
1248 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1251 repo_path = realpath(optarg, NULL);
1252 if (repo_path == NULL)
1253 return got_error_from_errno2("realpath",
1255 got_path_strip_trailing_slashes(repo_path);
1260 else if (verbosity < 3)
1275 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1277 remote_name = argv[0];
1281 cwd = getcwd(NULL, 0);
1283 error = got_error_from_errno("getcwd");
1287 if (repo_path == NULL) {
1288 error = got_worktree_open(&worktree, cwd);
1289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1295 strdup(got_worktree_get_repo_path(worktree));
1296 if (repo_path == NULL)
1297 error = got_error_from_errno("strdup");
1301 repo_path = strdup(cwd);
1302 if (repo_path == NULL) {
1303 error = got_error_from_errno("strdup");
1309 error = got_repo_open(&repo, repo_path, NULL);
1313 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1314 for (i = 0; i < nremotes; i++) {
1315 remote = &remotes[i];
1316 if (strcmp(remote->name, remote_name) == 0)
1319 if (i == nremotes) {
1320 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1324 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1325 &repo_name, remote->url);
1329 if (strcmp(proto, "git") == 0) {
1331 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1332 "sendfd dns inet unveil", NULL) == -1)
1335 } else if (strcmp(proto, "git+ssh") == 0 ||
1336 strcmp(proto, "ssh") == 0) {
1338 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1339 "sendfd unveil", NULL) == -1)
1342 } else if (strcmp(proto, "http") == 0 ||
1343 strcmp(proto, "git+http") == 0) {
1344 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1347 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1351 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1352 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1353 error = got_error_from_errno2("unveil",
1354 GOT_FETCH_PATH_SSH);
1358 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1362 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1368 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1370 fpa.last_scaled_size[0] = '\0';
1371 fpa.last_p_indexed = -1;
1372 fpa.last_p_resolved = -1;
1373 fpa.verbosity = verbosity;
1374 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1375 fetchfd, repo, fetch_progress, &fpa);
1379 if (pack_hash == NULL) {
1381 printf("Already up-to-date\n");
1385 if (verbosity >= 0) {
1386 error = got_object_id_str(&id_str, pack_hash);
1389 printf("\nFetched %s.pack\n", id_str);
1394 /* Update references provided with the pack file. */
1395 TAILQ_FOREACH(pe, &refs, entry) {
1396 const char *refname = pe->path;
1397 struct got_object_id *id = pe->data;
1398 struct got_reference *ref;
1399 char *remote_refname;
1401 error = got_object_id_str(&id_str, id);
1405 if (strncmp("refs/tags/", refname, 10) == 0) {
1406 error = got_ref_open(&ref, repo, refname, 0);
1408 if (error->code != GOT_ERR_NOT_REF)
1410 error = create_ref(refname, id, id_str, repo);
1414 error = update_ref(ref, id, repo);
1419 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1420 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1421 remote_name, refname + 11) == -1) {
1422 error = got_error_from_errno("asprintf");
1426 error = got_ref_open(&ref, repo, remote_refname, 0);
1428 if (error->code != GOT_ERR_NOT_REF)
1430 error = create_ref(remote_refname, id, id_str,
1435 error = update_ref(ref, id, repo);
1445 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1446 error = got_error_from_errno("close");
1448 got_repo_close(repo);
1450 got_worktree_close(worktree);
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 free((void *)pe->path);
1455 got_pathlist_free(&refs);
1456 TAILQ_FOREACH(pe, &symrefs, entry) {
1457 free((void *)pe->path);
1460 got_pathlist_free(&symrefs);
1475 usage_checkout(void)
1477 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1478 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1483 show_worktree_base_ref_warning(void)
1485 fprintf(stderr, "%s: warning: could not create a reference "
1486 "to the work tree's base commit; the commit could be "
1487 "garbage-collected by Git; making the repository "
1488 "writable and running 'got update' will prevent this\n",
1492 struct got_checkout_progress_arg {
1493 const char *worktree_path;
1494 int had_base_commit_ref_error;
1497 static const struct got_error *
1498 checkout_progress(void *arg, unsigned char status, const char *path)
1500 struct got_checkout_progress_arg *a = arg;
1502 /* Base commit bump happens silently. */
1503 if (status == GOT_STATUS_BUMP_BASE)
1506 if (status == GOT_STATUS_BASE_REF_ERR) {
1507 a->had_base_commit_ref_error = 1;
1511 while (path[0] == '/')
1514 printf("%c %s/%s\n", status, a->worktree_path, path);
1518 static const struct got_error *
1519 check_cancelled(void *arg)
1521 if (sigint_received || sigpipe_received)
1522 return got_error(GOT_ERR_CANCELLED);
1526 static const struct got_error *
1527 check_linear_ancestry(struct got_object_id *commit_id,
1528 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1529 struct got_repository *repo)
1531 const struct got_error *err = NULL;
1532 struct got_object_id *yca_id;
1534 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1535 commit_id, base_commit_id, repo, check_cancelled, NULL);
1540 return got_error(GOT_ERR_ANCESTRY);
1543 * Require a straight line of history between the target commit
1544 * and the work tree's base commit.
1546 * Non-linear situations such as this require a rebase:
1548 * (commit) D F (base_commit)
1556 * 'got update' only handles linear cases:
1557 * Update forwards in time: A (base/yca) - B - C - D (commit)
1558 * Update backwards in time: D (base) - C - B - A (commit/yca)
1560 if (allow_forwards_in_time_only) {
1561 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1562 return got_error(GOT_ERR_ANCESTRY);
1563 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1564 got_object_id_cmp(base_commit_id, yca_id) != 0)
1565 return got_error(GOT_ERR_ANCESTRY);
1571 static const struct got_error *
1572 check_same_branch(struct got_object_id *commit_id,
1573 struct got_reference *head_ref, struct got_object_id *yca_id,
1574 struct got_repository *repo)
1576 const struct got_error *err = NULL;
1577 struct got_commit_graph *graph = NULL;
1578 struct got_object_id *head_commit_id = NULL;
1579 int is_same_branch = 0;
1581 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1585 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1589 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1594 err = got_commit_graph_open(&graph, "/", 1);
1598 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1599 check_cancelled, NULL);
1604 struct got_object_id *id;
1605 err = got_commit_graph_iter_next(&id, graph, repo,
1606 check_cancelled, NULL);
1608 if (err->code == GOT_ERR_ITER_COMPLETED)
1614 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1616 if (got_object_id_cmp(id, commit_id) == 0) {
1624 got_commit_graph_close(graph);
1625 free(head_commit_id);
1626 if (!err && !is_same_branch)
1627 err = got_error(GOT_ERR_ANCESTRY);
1631 static const struct got_error *
1632 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1634 static char msg[512];
1635 const char *branch_name;
1637 if (got_ref_is_symbolic(ref))
1638 branch_name = got_ref_get_symref_target(ref);
1640 branch_name = got_ref_get_name(ref);
1642 if (strncmp("refs/heads/", branch_name, 11) == 0)
1645 snprintf(msg, sizeof(msg),
1646 "target commit is not contained in branch '%s'; "
1647 "the branch to use must be specified with -b; "
1648 "if necessary a new branch can be created for "
1649 "this commit with 'got branch -c %s BRANCH_NAME'",
1650 branch_name, commit_id_str);
1652 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1655 static const struct got_error *
1656 cmd_checkout(int argc, char *argv[])
1658 const struct got_error *error = NULL;
1659 struct got_repository *repo = NULL;
1660 struct got_reference *head_ref = NULL;
1661 struct got_worktree *worktree = NULL;
1662 char *repo_path = NULL;
1663 char *worktree_path = NULL;
1664 const char *path_prefix = "";
1665 const char *branch_name = GOT_REF_HEAD;
1666 char *commit_id_str = NULL;
1667 int ch, same_path_prefix, allow_nonempty = 0;
1668 struct got_pathlist_head paths;
1669 struct got_checkout_progress_arg cpa;
1673 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1676 branch_name = optarg;
1679 commit_id_str = strdup(optarg);
1680 if (commit_id_str == NULL)
1681 return got_error_from_errno("strdup");
1687 path_prefix = optarg;
1699 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1700 "unveil", NULL) == -1)
1704 char *cwd, *base, *dotgit;
1705 repo_path = realpath(argv[0], NULL);
1706 if (repo_path == NULL)
1707 return got_error_from_errno2("realpath", argv[0]);
1708 cwd = getcwd(NULL, 0);
1710 error = got_error_from_errno("getcwd");
1713 if (path_prefix[0]) {
1714 base = basename(path_prefix);
1716 error = got_error_from_errno2("basename",
1721 base = basename(repo_path);
1723 error = got_error_from_errno2("basename",
1728 dotgit = strstr(base, ".git");
1731 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1732 error = got_error_from_errno("asprintf");
1737 } else if (argc == 2) {
1738 repo_path = realpath(argv[0], NULL);
1739 if (repo_path == NULL) {
1740 error = got_error_from_errno2("realpath", argv[0]);
1743 worktree_path = realpath(argv[1], NULL);
1744 if (worktree_path == NULL) {
1745 if (errno != ENOENT) {
1746 error = got_error_from_errno2("realpath",
1750 worktree_path = strdup(argv[1]);
1751 if (worktree_path == NULL) {
1752 error = got_error_from_errno("strdup");
1759 got_path_strip_trailing_slashes(repo_path);
1760 got_path_strip_trailing_slashes(worktree_path);
1762 error = got_repo_open(&repo, repo_path, NULL);
1766 /* Pre-create work tree path for unveil(2) */
1767 error = got_path_mkdir(worktree_path);
1769 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1770 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1772 if (!allow_nonempty &&
1773 !got_path_dir_is_empty(worktree_path)) {
1774 error = got_error_path(worktree_path,
1775 GOT_ERR_DIR_NOT_EMPTY);
1780 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1784 error = got_ref_open(&head_ref, repo, branch_name, 0);
1788 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1789 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1792 error = got_worktree_open(&worktree, worktree_path);
1796 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1800 if (!same_path_prefix) {
1801 error = got_error(GOT_ERR_PATH_PREFIX);
1805 if (commit_id_str) {
1806 struct got_object_id *commit_id;
1807 error = got_repo_match_object_id(&commit_id, NULL,
1808 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1811 error = check_linear_ancestry(commit_id,
1812 got_worktree_get_base_commit_id(worktree), 0, repo);
1813 if (error != NULL) {
1815 if (error->code == GOT_ERR_ANCESTRY) {
1816 error = checkout_ancestry_error(
1817 head_ref, commit_id_str);
1821 error = check_same_branch(commit_id, head_ref, NULL, repo);
1823 if (error->code == GOT_ERR_ANCESTRY) {
1824 error = checkout_ancestry_error(
1825 head_ref, commit_id_str);
1829 error = got_worktree_set_base_commit_id(worktree, repo,
1836 error = got_pathlist_append(&paths, "", NULL);
1839 cpa.worktree_path = worktree_path;
1840 cpa.had_base_commit_ref_error = 0;
1841 error = got_worktree_checkout_files(worktree, &paths, repo,
1842 checkout_progress, &cpa, check_cancelled, NULL);
1846 printf("Now shut up and hack\n");
1847 if (cpa.had_base_commit_ref_error)
1848 show_worktree_base_ref_warning();
1850 got_pathlist_free(&paths);
1851 free(commit_id_str);
1853 free(worktree_path);
1860 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1865 static const struct got_error *
1866 update_progress(void *arg, unsigned char status, const char *path)
1868 int *did_something = arg;
1870 if (status == GOT_STATUS_EXISTS ||
1871 status == GOT_STATUS_BASE_REF_ERR)
1876 /* Base commit bump happens silently. */
1877 if (status == GOT_STATUS_BUMP_BASE)
1880 while (path[0] == '/')
1882 printf("%c %s\n", status, path);
1886 static const struct got_error *
1887 switch_head_ref(struct got_reference *head_ref,
1888 struct got_object_id *commit_id, struct got_worktree *worktree,
1889 struct got_repository *repo)
1891 const struct got_error *err = NULL;
1893 int ref_has_moved = 0;
1895 /* Trivial case: switching between two different references. */
1896 if (strcmp(got_ref_get_name(head_ref),
1897 got_worktree_get_head_ref_name(worktree)) != 0) {
1898 printf("Switching work tree from %s to %s\n",
1899 got_worktree_get_head_ref_name(worktree),
1900 got_ref_get_name(head_ref));
1901 return got_worktree_set_head_ref(worktree, head_ref);
1904 err = check_linear_ancestry(commit_id,
1905 got_worktree_get_base_commit_id(worktree), 0, repo);
1907 if (err->code != GOT_ERR_ANCESTRY)
1914 /* Switching to a rebased branch with the same reference name. */
1915 err = got_object_id_str(&base_id_str,
1916 got_worktree_get_base_commit_id(worktree));
1919 printf("Reference %s now points at a different branch\n",
1920 got_worktree_get_head_ref_name(worktree));
1921 printf("Switching work tree from %s to %s\n", base_id_str,
1922 got_worktree_get_head_ref_name(worktree));
1926 static const struct got_error *
1927 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1929 const struct got_error *err;
1932 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1936 return got_error(GOT_ERR_REBASING);
1938 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1942 return got_error(GOT_ERR_HISTEDIT_BUSY);
1947 static const struct got_error *
1948 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1949 char *argv[], struct got_worktree *worktree)
1951 const struct got_error *err = NULL;
1958 return got_error_from_errno("strdup");
1959 return got_pathlist_append(paths, path, NULL);
1962 for (i = 0; i < argc; i++) {
1963 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1966 err = got_pathlist_append(paths, path, NULL);
1976 static const struct got_error *
1977 cmd_update(int argc, char *argv[])
1979 const struct got_error *error = NULL;
1980 struct got_repository *repo = NULL;
1981 struct got_worktree *worktree = NULL;
1982 char *worktree_path = NULL;
1983 struct got_object_id *commit_id = NULL;
1984 char *commit_id_str = NULL;
1985 const char *branch_name = NULL;
1986 struct got_reference *head_ref = NULL;
1987 struct got_pathlist_head paths;
1988 struct got_pathlist_entry *pe;
1989 int ch, did_something = 0;
1993 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1996 branch_name = optarg;
1999 commit_id_str = strdup(optarg);
2000 if (commit_id_str == NULL)
2001 return got_error_from_errno("strdup");
2013 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2014 "unveil", NULL) == -1)
2017 worktree_path = getcwd(NULL, 0);
2018 if (worktree_path == NULL) {
2019 error = got_error_from_errno("getcwd");
2022 error = got_worktree_open(&worktree, worktree_path);
2026 error = check_rebase_or_histedit_in_progress(worktree);
2030 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2035 error = apply_unveil(got_repo_get_path(repo), 0,
2036 got_worktree_get_root_path(worktree));
2040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2044 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2045 got_worktree_get_head_ref_name(worktree), 0);
2048 if (commit_id_str == NULL) {
2049 error = got_ref_resolve(&commit_id, repo, head_ref);
2052 error = got_object_id_str(&commit_id_str, commit_id);
2056 error = got_repo_match_object_id(&commit_id, NULL,
2057 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2058 free(commit_id_str);
2059 commit_id_str = NULL;
2062 error = got_object_id_str(&commit_id_str, commit_id);
2068 struct got_object_id *head_commit_id;
2069 TAILQ_FOREACH(pe, &paths, entry) {
2070 if (pe->path_len == 0)
2072 error = got_error_msg(GOT_ERR_BAD_PATH,
2073 "switching between branches requires that "
2074 "the entire work tree gets updated");
2077 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2080 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2082 free(head_commit_id);
2085 error = check_same_branch(commit_id, head_ref, NULL, repo);
2088 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2092 error = check_linear_ancestry(commit_id,
2093 got_worktree_get_base_commit_id(worktree), 0, repo);
2094 if (error != NULL) {
2095 if (error->code == GOT_ERR_ANCESTRY)
2096 error = got_error(GOT_ERR_BRANCH_MOVED);
2099 error = check_same_branch(commit_id, head_ref, NULL, repo);
2104 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2106 error = got_worktree_set_base_commit_id(worktree, repo,
2112 error = got_worktree_checkout_files(worktree, &paths, repo,
2113 update_progress, &did_something, check_cancelled, NULL);
2118 printf("Updated to commit %s\n", commit_id_str);
2120 printf("Already up-to-date\n");
2122 free(worktree_path);
2123 TAILQ_FOREACH(pe, &paths, entry)
2124 free((char *)pe->path);
2125 got_pathlist_free(&paths);
2127 free(commit_id_str);
2131 static const struct got_error *
2132 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2133 const char *path, int diff_context, int ignore_whitespace,
2134 struct got_repository *repo)
2136 const struct got_error *err = NULL;
2137 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2140 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2145 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2149 while (path[0] == '/')
2151 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2152 ignore_whitespace, stdout);
2155 got_object_blob_close(blob1);
2156 got_object_blob_close(blob2);
2160 static const struct got_error *
2161 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2162 const char *path, int diff_context, int ignore_whitespace,
2163 struct got_repository *repo)
2165 const struct got_error *err = NULL;
2166 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2167 struct got_diff_blob_output_unidiff_arg arg;
2170 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2175 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2179 arg.diff_context = diff_context;
2180 arg.ignore_whitespace = ignore_whitespace;
2181 arg.outfile = stdout;
2182 while (path[0] == '/')
2184 err = got_diff_tree(tree1, tree2, path, path, repo,
2185 got_diff_blob_output_unidiff, &arg, 1);
2188 got_object_tree_close(tree1);
2190 got_object_tree_close(tree2);
2194 static const struct got_error *
2195 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2196 const char *path, int diff_context, struct got_repository *repo)
2198 const struct got_error *err = NULL;
2199 struct got_commit_object *pcommit = NULL;
2200 char *id_str1 = NULL, *id_str2 = NULL;
2201 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2202 struct got_object_qid *qid;
2204 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2206 err = got_object_open_as_commit(&pcommit, repo,
2212 if (path && path[0] != '\0') {
2214 err = got_object_id_by_path(&obj_id2, repo, id, path);
2217 err = got_object_id_str(&id_str2, obj_id2);
2223 err = got_object_id_by_path(&obj_id1, repo,
2229 err = got_object_id_str(&id_str1, obj_id1);
2235 err = got_object_get_type(&obj_type, repo, obj_id2);
2240 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2242 case GOT_OBJ_TYPE_BLOB:
2243 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2246 case GOT_OBJ_TYPE_TREE:
2247 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2251 err = got_error(GOT_ERR_OBJ_TYPE);
2257 obj_id2 = got_object_commit_get_tree_id(commit);
2258 err = got_object_id_str(&id_str2, obj_id2);
2261 obj_id1 = got_object_commit_get_tree_id(pcommit);
2262 err = got_object_id_str(&id_str1, obj_id1);
2265 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2266 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2272 got_object_commit_close(pcommit);
2277 get_datestr(time_t *time, char *datebuf)
2279 struct tm mytm, *tm;
2282 tm = gmtime_r(time, &mytm);
2285 s = asctime_r(tm, datebuf);
2288 p = strchr(s, '\n');
2294 static const struct got_error *
2295 match_logmsg(int *have_match, struct got_object_id *id,
2296 struct got_commit_object *commit, regex_t *regex)
2298 const struct got_error *err = NULL;
2299 regmatch_t regmatch;
2300 char *id_str = NULL, *logmsg = NULL;
2304 err = got_object_id_str(&id_str, id);
2308 err = got_object_commit_get_logmsg(&logmsg, commit);
2312 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2320 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2322 static const struct got_error *
2323 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2324 struct got_repository *repo, const char *path, int show_patch,
2325 int diff_context, struct got_reflist_head *refs)
2327 const struct got_error *err = NULL;
2328 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2330 time_t committer_time;
2331 const char *author, *committer;
2332 char *refs_str = NULL;
2333 struct got_reflist_entry *re;
2335 SIMPLEQ_FOREACH(re, refs, entry) {
2338 struct got_tag_object *tag = NULL;
2341 name = got_ref_get_name(re->ref);
2342 if (strcmp(name, GOT_REF_HEAD) == 0)
2344 if (strncmp(name, "refs/", 5) == 0)
2346 if (strncmp(name, "got/", 4) == 0)
2348 if (strncmp(name, "heads/", 6) == 0)
2350 if (strncmp(name, "remotes/", 8) == 0)
2352 if (strncmp(name, "tags/", 5) == 0) {
2353 err = got_object_open_as_tag(&tag, repo, re->id);
2355 if (err->code != GOT_ERR_OBJ_TYPE)
2357 /* Ref points at something other than a tag. */
2362 cmp = got_object_id_cmp(tag ?
2363 got_object_tag_get_object_id(tag) : re->id, id);
2365 got_object_tag_close(tag);
2369 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2371 err = got_error_from_errno("asprintf");
2377 err = got_object_id_str(&id_str, id);
2381 printf(GOT_COMMIT_SEP_STR);
2382 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2383 refs_str ? refs_str : "", refs_str ? ")" : "");
2388 printf("from: %s\n", got_object_commit_get_author(commit));
2389 committer_time = got_object_commit_get_committer_time(commit);
2390 datestr = get_datestr(&committer_time, datebuf);
2392 printf("date: %s UTC\n", datestr);
2393 author = got_object_commit_get_author(commit);
2394 committer = got_object_commit_get_committer(commit);
2395 if (strcmp(author, committer) != 0)
2396 printf("via: %s\n", committer);
2397 if (got_object_commit_get_nparents(commit) > 1) {
2398 const struct got_object_id_queue *parent_ids;
2399 struct got_object_qid *qid;
2401 parent_ids = got_object_commit_get_parent_ids(commit);
2402 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2403 err = got_object_id_str(&id_str, qid->id);
2406 printf("parent %d: %s\n", n++, id_str);
2411 err = got_object_commit_get_logmsg(&logmsg0, commit);
2417 line = strsep(&logmsg, "\n");
2419 printf(" %s\n", line);
2424 err = print_patch(commit, id, path, diff_context, repo);
2429 if (fflush(stdout) != 0 && err == NULL)
2430 err = got_error_from_errno("fflush");
2434 static const struct got_error *
2435 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2436 const char *path, int show_patch, const char *search_pattern,
2437 int diff_context, int limit, int log_branches,
2438 struct got_reflist_head *refs)
2440 const struct got_error *err;
2441 struct got_commit_graph *graph;
2445 if (search_pattern &&
2446 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2447 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2449 err = got_commit_graph_open(&graph, path, !log_branches);
2452 err = got_commit_graph_iter_start(graph, root_id, repo,
2453 check_cancelled, NULL);
2457 struct got_commit_object *commit;
2458 struct got_object_id *id;
2460 if (sigint_received || sigpipe_received)
2463 err = got_commit_graph_iter_next(&id, graph, repo,
2464 check_cancelled, NULL);
2466 if (err->code == GOT_ERR_ITER_COMPLETED)
2473 err = got_object_open_as_commit(&commit, repo, id);
2477 if (search_pattern) {
2478 err = match_logmsg(&have_match, id, commit, ®ex);
2480 got_object_commit_close(commit);
2483 if (have_match == 0) {
2484 got_object_commit_close(commit);
2489 err = print_commit(commit, id, repo, path, show_patch,
2490 diff_context, refs);
2491 got_object_commit_close(commit);
2492 if (err || (limit && --limit == 0))
2498 got_commit_graph_close(graph);
2505 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2506 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2511 get_default_log_limit(void)
2513 const char *got_default_log_limit;
2517 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2518 if (got_default_log_limit == NULL)
2520 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2526 static const struct got_error *
2527 cmd_log(int argc, char *argv[])
2529 const struct got_error *error;
2530 struct got_repository *repo = NULL;
2531 struct got_worktree *worktree = NULL;
2532 struct got_commit_object *commit = NULL;
2533 struct got_object_id *id = NULL;
2534 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2535 const char *start_commit = NULL, *search_pattern = NULL;
2536 int diff_context = -1, ch;
2537 int show_patch = 0, limit = 0, log_branches = 0;
2539 struct got_reflist_head refs;
2541 SIMPLEQ_INIT(&refs);
2544 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2550 limit = get_default_log_limit();
2552 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2558 start_commit = optarg;
2561 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2564 err(1, "-C option %s", errstr);
2567 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2569 err(1, "-l option %s", errstr);
2575 repo_path = realpath(optarg, NULL);
2576 if (repo_path == NULL)
2577 return got_error_from_errno2("realpath",
2579 got_path_strip_trailing_slashes(repo_path);
2582 search_pattern = optarg;
2593 if (diff_context == -1)
2595 else if (!show_patch)
2596 errx(1, "-C reguires -p");
2598 cwd = getcwd(NULL, 0);
2600 error = got_error_from_errno("getcwd");
2604 error = got_worktree_open(&worktree, cwd);
2605 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2612 error = got_error_from_errno("strdup");
2615 } else if (argc == 1) {
2617 error = got_worktree_resolve_path(&path, worktree,
2622 path = strdup(argv[0]);
2624 error = got_error_from_errno("strdup");
2631 if (repo_path == NULL) {
2632 repo_path = worktree ?
2633 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2635 if (repo_path == NULL) {
2636 error = got_error_from_errno("strdup");
2640 error = got_repo_open(&repo, repo_path, NULL);
2644 error = apply_unveil(got_repo_get_path(repo), 1,
2645 worktree ? got_worktree_get_root_path(worktree) : NULL);
2649 if (start_commit == NULL) {
2650 struct got_reference *head_ref;
2651 error = got_ref_open(&head_ref, repo,
2652 worktree ? got_worktree_get_head_ref_name(worktree)
2656 error = got_ref_resolve(&id, repo, head_ref);
2657 got_ref_close(head_ref);
2660 error = got_object_open_as_commit(&commit, repo, id);
2662 struct got_reference *ref;
2663 error = got_ref_open(&ref, repo, start_commit, 0);
2664 if (error == NULL) {
2666 error = got_ref_resolve(&id, repo, ref);
2670 error = got_object_get_type(&obj_type, repo, id);
2673 if (obj_type == GOT_OBJ_TYPE_TAG) {
2674 struct got_tag_object *tag;
2675 error = got_object_open_as_tag(&tag, repo, id);
2678 if (got_object_tag_get_object_type(tag) !=
2679 GOT_OBJ_TYPE_COMMIT) {
2680 got_object_tag_close(tag);
2681 error = got_error(GOT_ERR_OBJ_TYPE);
2685 id = got_object_id_dup(
2686 got_object_tag_get_object_id(tag));
2688 error = got_error_from_errno(
2689 "got_object_id_dup");
2690 got_object_tag_close(tag);
2693 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2694 error = got_error(GOT_ERR_OBJ_TYPE);
2697 error = got_object_open_as_commit(&commit, repo, id);
2701 if (commit == NULL) {
2702 error = got_repo_match_object_id_prefix(&id,
2703 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2712 const char *prefix = got_worktree_get_path_prefix(worktree);
2714 if (asprintf(&p, "%s%s%s", prefix,
2715 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2716 error = got_error_from_errno("asprintf");
2719 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2722 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2727 path = in_repo_path;
2730 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2734 error = print_commits(id, repo, path, show_patch, search_pattern,
2735 diff_context, limit, log_branches, &refs);
2742 got_worktree_close(worktree);
2744 const struct got_error *repo_error;
2745 repo_error = got_repo_close(repo);
2749 got_ref_list_free(&refs);
2756 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2757 "[-w] [object1 object2 | path]\n", getprogname());
2761 struct print_diff_arg {
2762 struct got_repository *repo;
2763 struct got_worktree *worktree;
2768 int ignore_whitespace;
2771 static const struct got_error *
2772 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2773 const char *path, struct got_object_id *blob_id,
2774 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2775 int dirfd, const char *de_name)
2777 struct print_diff_arg *a = arg;
2778 const struct got_error *err = NULL;
2779 struct got_blob_object *blob1 = NULL;
2782 char *abspath = NULL, *label1 = NULL;
2785 if (a->diff_staged) {
2786 if (staged_status != GOT_STATUS_MODIFY &&
2787 staged_status != GOT_STATUS_ADD &&
2788 staged_status != GOT_STATUS_DELETE)
2791 if (staged_status == GOT_STATUS_DELETE)
2793 if (status == GOT_STATUS_NONEXISTENT)
2794 return got_error_set_errno(ENOENT, path);
2795 if (status != GOT_STATUS_MODIFY &&
2796 status != GOT_STATUS_ADD &&
2797 status != GOT_STATUS_DELETE &&
2798 status != GOT_STATUS_CONFLICT)
2802 if (!a->header_shown) {
2803 printf("diff %s %s%s\n", a->id_str,
2804 got_worktree_get_root_path(a->worktree),
2805 a->diff_staged ? " (staged changes)" : "");
2806 a->header_shown = 1;
2809 if (a->diff_staged) {
2810 const char *label1 = NULL, *label2 = NULL;
2811 switch (staged_status) {
2812 case GOT_STATUS_MODIFY:
2816 case GOT_STATUS_ADD:
2819 case GOT_STATUS_DELETE:
2823 return got_error(GOT_ERR_FILE_STATUS);
2825 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2826 label1, label2, a->diff_context, a->ignore_whitespace,
2830 if (staged_status == GOT_STATUS_ADD ||
2831 staged_status == GOT_STATUS_MODIFY) {
2833 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2837 err = got_object_id_str(&id_str, staged_blob_id);
2840 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2841 err = got_error_from_errno("asprintf");
2846 } else if (status != GOT_STATUS_ADD) {
2847 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2852 if (status != GOT_STATUS_DELETE) {
2853 if (asprintf(&abspath, "%s/%s",
2854 got_worktree_get_root_path(a->worktree), path) == -1) {
2855 err = got_error_from_errno("asprintf");
2860 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2862 err = got_error_from_errno2("openat", abspath);
2866 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2868 err = got_error_from_errno2("open", abspath);
2872 if (fstat(fd, &sb) == -1) {
2873 err = got_error_from_errno2("fstat", abspath);
2876 f2 = fdopen(fd, "r");
2878 err = got_error_from_errno2("fdopen", abspath);
2885 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2886 a->diff_context, a->ignore_whitespace, stdout);
2889 got_object_blob_close(blob1);
2890 if (f2 && fclose(f2) == EOF && err == NULL)
2891 err = got_error_from_errno("fclose");
2892 if (fd != -1 && close(fd) == -1 && err == NULL)
2893 err = got_error_from_errno("close");
2898 static const struct got_error *
2899 cmd_diff(int argc, char *argv[])
2901 const struct got_error *error;
2902 struct got_repository *repo = NULL;
2903 struct got_worktree *worktree = NULL;
2904 char *cwd = NULL, *repo_path = NULL;
2905 struct got_object_id *id1 = NULL, *id2 = NULL;
2906 const char *id_str1 = NULL, *id_str2 = NULL;
2907 char *label1 = NULL, *label2 = NULL;
2909 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2914 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2919 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2922 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2925 err(1, "-C option %s", errstr);
2928 repo_path = realpath(optarg, NULL);
2929 if (repo_path == NULL)
2930 return got_error_from_errno2("realpath",
2932 got_path_strip_trailing_slashes(repo_path);
2938 ignore_whitespace = 1;
2949 cwd = getcwd(NULL, 0);
2951 error = got_error_from_errno("getcwd");
2954 error = got_worktree_open(&worktree, cwd);
2955 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2958 if (worktree == NULL) {
2959 error = got_error(GOT_ERR_NOT_WORKTREE);
2964 "-r option can't be used when diffing a work tree");
2965 repo_path = strdup(got_worktree_get_repo_path(worktree));
2966 if (repo_path == NULL) {
2967 error = got_error_from_errno("strdup");
2971 error = got_worktree_resolve_path(&path, worktree,
2978 error = got_error_from_errno("strdup");
2982 } else if (argc == 2) {
2984 errx(1, "-s option can't be used when diffing "
2985 "objects in repository");
2988 if (worktree && repo_path == NULL) {
2990 strdup(got_worktree_get_repo_path(worktree));
2991 if (repo_path == NULL) {
2992 error = got_error_from_errno("strdup");
2999 if (repo_path == NULL) {
3000 repo_path = getcwd(NULL, 0);
3001 if (repo_path == NULL)
3002 return got_error_from_errno("getcwd");
3005 error = got_repo_open(&repo, repo_path, NULL);
3010 error = apply_unveil(got_repo_get_path(repo), 1,
3011 worktree ? got_worktree_get_root_path(worktree) : NULL);
3016 struct print_diff_arg arg;
3017 struct got_pathlist_head paths;
3022 error = got_object_id_str(&id_str,
3023 got_worktree_get_base_commit_id(worktree));
3027 arg.worktree = worktree;
3028 arg.diff_context = diff_context;
3029 arg.id_str = id_str;
3030 arg.header_shown = 0;
3031 arg.diff_staged = diff_staged;
3032 arg.ignore_whitespace = ignore_whitespace;
3034 error = got_pathlist_append(&paths, path, NULL);
3038 error = got_worktree_status(worktree, &paths, repo, print_diff,
3039 &arg, check_cancelled, NULL);
3041 got_pathlist_free(&paths);
3045 error = got_repo_match_object_id(&id1, &label1, id_str1,
3046 GOT_OBJ_TYPE_ANY, 1, repo);
3050 error = got_repo_match_object_id(&id2, &label2, id_str2,
3051 GOT_OBJ_TYPE_ANY, 1, repo);
3055 error = got_object_get_type(&type1, repo, id1);
3059 error = got_object_get_type(&type2, repo, id2);
3063 if (type1 != type2) {
3064 error = got_error(GOT_ERR_OBJ_TYPE);
3069 case GOT_OBJ_TYPE_BLOB:
3070 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3071 diff_context, ignore_whitespace, repo, stdout);
3073 case GOT_OBJ_TYPE_TREE:
3074 error = got_diff_objects_as_trees(id1, id2, "", "",
3075 diff_context, ignore_whitespace, repo, stdout);
3077 case GOT_OBJ_TYPE_COMMIT:
3078 printf("diff %s %s\n", label1, label2);
3079 error = got_diff_objects_as_commits(id1, id2, diff_context,
3080 ignore_whitespace, repo, stdout);
3083 error = got_error(GOT_ERR_OBJ_TYPE);
3092 got_worktree_close(worktree);
3094 const struct got_error *repo_error;
3095 repo_error = got_repo_close(repo);
3106 "usage: %s blame [-c commit] [-r repository-path] path\n",
3115 char datebuf[11]; /* YYYY-MM-DD + NUL */
3118 struct blame_cb_args {
3119 struct blame_line *lines;
3123 off_t *line_offsets;
3125 struct got_repository *repo;
3128 static const struct got_error *
3129 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3131 const struct got_error *err = NULL;
3132 struct blame_cb_args *a = arg;
3133 struct blame_line *bline;
3135 size_t linesize = 0;
3136 struct got_commit_object *commit = NULL;
3139 time_t committer_time;
3141 if (nlines != a->nlines ||
3142 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3143 return got_error(GOT_ERR_RANGE);
3145 if (sigint_received)
3146 return got_error(GOT_ERR_ITER_COMPLETED);
3149 return NULL; /* no change in this commit */
3151 /* Annotate this line. */
3152 bline = &a->lines[lineno - 1];
3153 if (bline->annotated)
3155 err = got_object_id_str(&bline->id_str, id);
3159 err = got_object_open_as_commit(&commit, a->repo, id);
3163 bline->committer = strdup(got_object_commit_get_committer(commit));
3164 if (bline->committer == NULL) {
3165 err = got_error_from_errno("strdup");
3169 committer_time = got_object_commit_get_committer_time(commit);
3170 if (localtime_r(&committer_time, &tm) == NULL)
3171 return got_error_from_errno("localtime_r");
3172 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3173 &tm) >= sizeof(bline->datebuf)) {
3174 err = got_error(GOT_ERR_NO_SPACE);
3177 bline->annotated = 1;
3179 /* Print lines annotated so far. */
3180 bline = &a->lines[a->lineno_cur - 1];
3181 if (!bline->annotated)
3184 offset = a->line_offsets[a->lineno_cur - 1];
3185 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3186 err = got_error_from_errno("fseeko");
3190 while (bline->annotated) {
3191 char *smallerthan, *at, *nl, *committer;
3194 if (getline(&line, &linesize, a->f) == -1) {
3196 err = got_error_from_errno("getline");
3200 committer = bline->committer;
3201 smallerthan = strchr(committer, '<');
3202 if (smallerthan && smallerthan[1] != '\0')
3203 committer = smallerthan + 1;
3204 at = strchr(committer, '@');
3207 len = strlen(committer);
3209 committer[8] = '\0';
3211 nl = strchr(line, '\n');
3214 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3215 bline->id_str, bline->datebuf, committer, line);
3218 bline = &a->lines[a->lineno_cur - 1];
3222 got_object_commit_close(commit);
3227 static const struct got_error *
3228 cmd_blame(int argc, char *argv[])
3230 const struct got_error *error;
3231 struct got_repository *repo = NULL;
3232 struct got_worktree *worktree = NULL;
3233 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3234 struct got_object_id *obj_id = NULL;
3235 struct got_object_id *commit_id = NULL;
3236 struct got_blob_object *blob = NULL;
3237 char *commit_id_str = NULL;
3238 struct blame_cb_args bca;
3239 int ch, obj_type, i;
3242 memset(&bca, 0, sizeof(bca));
3245 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3250 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3253 commit_id_str = optarg;
3256 repo_path = realpath(optarg, NULL);
3257 if (repo_path == NULL)
3258 return got_error_from_errno2("realpath",
3260 got_path_strip_trailing_slashes(repo_path);
3276 cwd = getcwd(NULL, 0);
3278 error = got_error_from_errno("getcwd");
3281 if (repo_path == NULL) {
3282 error = got_worktree_open(&worktree, cwd);
3283 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3289 strdup(got_worktree_get_repo_path(worktree));
3290 if (repo_path == NULL)
3291 error = got_error_from_errno("strdup");
3295 repo_path = strdup(cwd);
3296 if (repo_path == NULL) {
3297 error = got_error_from_errno("strdup");
3303 error = got_repo_open(&repo, repo_path, NULL);
3307 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3312 const char *prefix = got_worktree_get_path_prefix(worktree);
3313 char *p, *worktree_subdir = cwd +
3314 strlen(got_worktree_get_root_path(worktree));
3315 if (asprintf(&p, "%s%s%s%s%s",
3316 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3317 worktree_subdir, worktree_subdir[0] ? "/" : "",
3319 error = got_error_from_errno("asprintf");
3322 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3325 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3330 if (commit_id_str == NULL) {
3331 struct got_reference *head_ref;
3332 error = got_ref_open(&head_ref, repo, worktree ?
3333 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3336 error = got_ref_resolve(&commit_id, repo, head_ref);
3337 got_ref_close(head_ref);
3341 error = got_repo_match_object_id(&commit_id, NULL,
3342 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3347 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3351 error = got_object_get_type(&obj_type, repo, obj_id);
3355 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3356 error = got_error(GOT_ERR_OBJ_TYPE);
3360 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3363 bca.f = got_opentemp();
3364 if (bca.f == NULL) {
3365 error = got_error_from_errno("got_opentemp");
3368 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3369 &bca.line_offsets, bca.f, blob);
3370 if (error || bca.nlines == 0)
3373 /* Don't include \n at EOF in the blame line count. */
3374 if (bca.line_offsets[bca.nlines - 1] == filesize)
3377 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3378 if (bca.lines == NULL) {
3379 error = got_error_from_errno("calloc");
3383 bca.nlines_prec = 0;
3391 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3392 check_cancelled, NULL);
3400 got_object_blob_close(blob);
3402 got_worktree_close(worktree);
3404 const struct got_error *repo_error;
3405 repo_error = got_repo_close(repo);
3410 for (i = 0; i < bca.nlines; i++) {
3411 struct blame_line *bline = &bca.lines[i];
3412 free(bline->id_str);
3413 free(bline->committer);
3417 free(bca.line_offsets);
3418 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3419 error = got_error_from_errno("fclose");
3427 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3433 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3434 const char *root_path)
3436 int is_root_path = (strcmp(path, root_path) == 0);
3437 const char *modestr = "";
3438 mode_t mode = got_tree_entry_get_mode(te);
3440 path += strlen(root_path);
3441 while (path[0] == '/')
3444 if (got_object_tree_entry_is_submodule(te))
3446 else if (S_ISLNK(mode))
3448 else if (S_ISDIR(mode))
3450 else if (mode & S_IXUSR)
3453 printf("%s%s%s%s%s\n", id ? id : "", path,
3454 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3457 static const struct got_error *
3458 print_tree(const char *path, struct got_object_id *commit_id,
3459 int show_ids, int recurse, const char *root_path,
3460 struct got_repository *repo)
3462 const struct got_error *err = NULL;
3463 struct got_object_id *tree_id = NULL;
3464 struct got_tree_object *tree = NULL;
3467 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3471 err = got_object_open_as_tree(&tree, repo, tree_id);
3474 nentries = got_object_tree_get_nentries(tree);
3475 for (i = 0; i < nentries; i++) {
3476 struct got_tree_entry *te;
3479 if (sigint_received || sigpipe_received)
3482 te = got_object_tree_get_entry(tree, i);
3485 err = got_object_id_str(&id_str,
3486 got_tree_entry_get_id(te));
3489 if (asprintf(&id, "%s ", id_str) == -1) {
3490 err = got_error_from_errno("asprintf");
3496 print_entry(te, id, path, root_path);
3499 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3501 if (asprintf(&child_path, "%s%s%s", path,
3502 path[0] == '/' && path[1] == '\0' ? "" : "/",
3503 got_tree_entry_get_name(te)) == -1) {
3504 err = got_error_from_errno("asprintf");
3507 err = print_tree(child_path, commit_id, show_ids, 1,
3516 got_object_tree_close(tree);
3521 static const struct got_error *
3522 cmd_tree(int argc, char *argv[])
3524 const struct got_error *error;
3525 struct got_repository *repo = NULL;
3526 struct got_worktree *worktree = NULL;
3528 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3529 struct got_object_id *commit_id = NULL;
3530 char *commit_id_str = NULL;
3531 int show_ids = 0, recurse = 0;
3535 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3540 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3543 commit_id_str = optarg;
3546 repo_path = realpath(optarg, NULL);
3547 if (repo_path == NULL)
3548 return got_error_from_errno2("realpath",
3550 got_path_strip_trailing_slashes(repo_path);
3574 cwd = getcwd(NULL, 0);
3576 error = got_error_from_errno("getcwd");
3579 if (repo_path == NULL) {
3580 error = got_worktree_open(&worktree, cwd);
3581 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3587 strdup(got_worktree_get_repo_path(worktree));
3588 if (repo_path == NULL)
3589 error = got_error_from_errno("strdup");
3593 repo_path = strdup(cwd);
3594 if (repo_path == NULL) {
3595 error = got_error_from_errno("strdup");
3601 error = got_repo_open(&repo, repo_path, NULL);
3605 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3611 char *p, *worktree_subdir = cwd +
3612 strlen(got_worktree_get_root_path(worktree));
3613 if (asprintf(&p, "%s/%s",
3614 got_worktree_get_path_prefix(worktree),
3615 worktree_subdir) == -1) {
3616 error = got_error_from_errno("asprintf");
3619 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3626 if (in_repo_path == NULL) {
3627 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3632 if (commit_id_str == NULL) {
3633 struct got_reference *head_ref;
3634 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3637 error = got_ref_resolve(&commit_id, repo, head_ref);
3638 got_ref_close(head_ref);
3642 error = got_repo_match_object_id(&commit_id, NULL,
3643 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3648 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3649 in_repo_path, repo);
3656 got_worktree_close(worktree);
3658 const struct got_error *repo_error;
3659 repo_error = got_repo_close(repo);
3669 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3673 static const struct got_error *
3674 print_status(void *arg, unsigned char status, unsigned char staged_status,
3675 const char *path, struct got_object_id *blob_id,
3676 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3677 int dirfd, const char *de_name)
3679 if (status == staged_status && (status == GOT_STATUS_DELETE))
3680 status = GOT_STATUS_NO_CHANGE;
3681 printf("%c%c %s\n", status, staged_status, path);
3685 static const struct got_error *
3686 cmd_status(int argc, char *argv[])
3688 const struct got_error *error = NULL;
3689 struct got_repository *repo = NULL;
3690 struct got_worktree *worktree = NULL;
3692 struct got_pathlist_head paths;
3693 struct got_pathlist_entry *pe;
3698 while ((ch = getopt(argc, argv, "")) != -1) {
3710 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3714 cwd = getcwd(NULL, 0);
3716 error = got_error_from_errno("getcwd");
3720 error = got_worktree_open(&worktree, cwd);
3724 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3729 error = apply_unveil(got_repo_get_path(repo), 1,
3730 got_worktree_get_root_path(worktree));
3734 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3738 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3739 check_cancelled, NULL);
3741 TAILQ_FOREACH(pe, &paths, entry)
3742 free((char *)pe->path);
3743 got_pathlist_free(&paths);
3752 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3757 static const struct got_error *
3758 list_refs(struct got_repository *repo)
3760 static const struct got_error *err = NULL;
3761 struct got_reflist_head refs;
3762 struct got_reflist_entry *re;
3764 SIMPLEQ_INIT(&refs);
3765 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3769 SIMPLEQ_FOREACH(re, &refs, entry) {
3771 refstr = got_ref_to_str(re->ref);
3773 return got_error_from_errno("got_ref_to_str");
3774 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3778 got_ref_list_free(&refs);
3782 static const struct got_error *
3783 delete_ref(struct got_repository *repo, const char *refname)
3785 const struct got_error *err = NULL;
3786 struct got_reference *ref;
3788 err = got_ref_open(&ref, repo, refname, 0);
3792 err = got_ref_delete(ref, repo);
3797 static const struct got_error *
3798 add_ref(struct got_repository *repo, const char *refname, const char *target)
3800 const struct got_error *err = NULL;
3801 struct got_object_id *id;
3802 struct got_reference *ref = NULL;
3805 * Don't let the user create a reference name with a leading '-'.
3806 * While technically a valid reference name, this case is usually
3807 * an unintended typo.
3809 if (refname[0] == '-')
3810 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3812 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3815 struct got_reference *target_ref;
3817 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3819 err = got_ref_open(&target_ref, repo, target, 0);
3822 err = got_ref_resolve(&id, repo, target_ref);
3823 got_ref_close(target_ref);
3828 err = got_ref_alloc(&ref, refname, id);
3832 err = got_ref_write(ref, repo);
3840 static const struct got_error *
3841 add_symref(struct got_repository *repo, const char *refname, const char *target)
3843 const struct got_error *err = NULL;
3844 struct got_reference *ref = NULL;
3845 struct got_reference *target_ref = NULL;
3848 * Don't let the user create a reference name with a leading '-'.
3849 * While technically a valid reference name, this case is usually
3850 * an unintended typo.
3852 if (refname[0] == '-')
3853 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3855 err = got_ref_open(&target_ref, repo, target, 0);
3859 err = got_ref_alloc_symref(&ref, refname, target_ref);
3863 err = got_ref_write(ref, repo);
3866 got_ref_close(target_ref);
3872 static const struct got_error *
3873 cmd_ref(int argc, char *argv[])
3875 const struct got_error *error = NULL;
3876 struct got_repository *repo = NULL;
3877 struct got_worktree *worktree = NULL;
3878 char *cwd = NULL, *repo_path = NULL;
3879 int ch, do_list = 0, create_symref = 0;
3880 const char *delref = NULL;
3882 /* TODO: Add -s option for adding symbolic references. */
3883 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3889 repo_path = realpath(optarg, NULL);
3890 if (repo_path == NULL)
3891 return got_error_from_errno2("realpath",
3893 got_path_strip_trailing_slashes(repo_path);
3907 if (do_list && delref)
3908 errx(1, "-l and -d options are mutually exclusive\n");
3913 if (do_list || delref) {
3915 errx(1, "-s option cannot be used together with the "
3916 "-l or -d options");
3919 } else if (argc != 2)
3924 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3928 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3929 "sendfd unveil", NULL) == -1)
3933 cwd = getcwd(NULL, 0);
3935 error = got_error_from_errno("getcwd");
3939 if (repo_path == NULL) {
3940 error = got_worktree_open(&worktree, cwd);
3941 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3947 strdup(got_worktree_get_repo_path(worktree));
3948 if (repo_path == NULL)
3949 error = got_error_from_errno("strdup");
3953 repo_path = strdup(cwd);
3954 if (repo_path == NULL) {
3955 error = got_error_from_errno("strdup");
3961 error = got_repo_open(&repo, repo_path, NULL);
3965 error = apply_unveil(got_repo_get_path(repo), do_list,
3966 worktree ? got_worktree_get_root_path(worktree) : NULL);
3971 error = list_refs(repo);
3973 error = delete_ref(repo, delref);
3974 else if (create_symref)
3975 error = add_symref(repo, argv[0], argv[1]);
3977 error = add_ref(repo, argv[0], argv[1]);
3980 got_repo_close(repo);
3982 got_worktree_close(worktree);
3992 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3993 "[name]\n", getprogname());
3997 static const struct got_error *
3998 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3999 struct got_reference *ref)
4001 const struct got_error *err = NULL;
4002 const char *refname, *marker = " ";
4005 refname = got_ref_get_name(ref);
4006 if (worktree && strcmp(refname,
4007 got_worktree_get_head_ref_name(worktree)) == 0) {
4008 struct got_object_id *id = NULL;
4010 err = got_ref_resolve(&id, repo, ref);
4013 if (got_object_id_cmp(id,
4014 got_worktree_get_base_commit_id(worktree)) == 0)
4021 if (strncmp(refname, "refs/heads/", 11) == 0)
4023 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4026 refstr = got_ref_to_str(ref);
4028 return got_error_from_errno("got_ref_to_str");
4030 printf("%s%s: %s\n", marker, refname, refstr);
4035 static const struct got_error *
4036 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4038 const char *refname;
4040 if (worktree == NULL)
4041 return got_error(GOT_ERR_NOT_WORKTREE);
4043 refname = got_worktree_get_head_ref_name(worktree);
4045 if (strncmp(refname, "refs/heads/", 11) == 0)
4047 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4050 printf("%s\n", refname);
4055 static const struct got_error *
4056 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4058 static const struct got_error *err = NULL;
4059 struct got_reflist_head refs;
4060 struct got_reflist_entry *re;
4061 struct got_reference *temp_ref = NULL;
4062 int rebase_in_progress, histedit_in_progress;
4064 SIMPLEQ_INIT(&refs);
4067 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4072 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4077 if (rebase_in_progress || histedit_in_progress) {
4078 err = got_ref_open(&temp_ref, repo,
4079 got_worktree_get_head_ref_name(worktree), 0);
4082 list_branch(repo, worktree, temp_ref);
4083 got_ref_close(temp_ref);
4087 err = got_ref_list(&refs, repo, "refs/heads",
4088 got_ref_cmp_by_name, NULL);
4092 SIMPLEQ_FOREACH(re, &refs, entry)
4093 list_branch(repo, worktree, re->ref);
4095 got_ref_list_free(&refs);
4099 static const struct got_error *
4100 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4101 const char *branch_name)
4103 const struct got_error *err = NULL;
4104 struct got_reference *ref = NULL;
4107 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4108 return got_error_from_errno("asprintf");
4110 err = got_ref_open(&ref, repo, refname, 0);
4115 strcmp(got_worktree_get_head_ref_name(worktree),
4116 got_ref_get_name(ref)) == 0) {
4117 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4118 "will not delete this work tree's current branch");
4122 err = got_ref_delete(ref, repo);
4130 static const struct got_error *
4131 add_branch(struct got_repository *repo, const char *branch_name,
4132 struct got_object_id *base_commit_id)
4134 const struct got_error *err = NULL;
4135 struct got_reference *ref = NULL;
4136 char *base_refname = NULL, *refname = NULL;
4139 * Don't let the user create a branch name with a leading '-'.
4140 * While technically a valid reference name, this case is usually
4141 * an unintended typo.
4143 if (branch_name[0] == '-')
4144 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4146 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4147 err = got_error_from_errno("asprintf");
4151 err = got_ref_open(&ref, repo, refname, 0);
4153 err = got_error(GOT_ERR_BRANCH_EXISTS);
4155 } else if (err->code != GOT_ERR_NOT_REF)
4158 err = got_ref_alloc(&ref, refname, base_commit_id);
4162 err = got_ref_write(ref, repo);
4171 static const struct got_error *
4172 cmd_branch(int argc, char *argv[])
4174 const struct got_error *error = NULL;
4175 struct got_repository *repo = NULL;
4176 struct got_worktree *worktree = NULL;
4177 char *cwd = NULL, *repo_path = NULL;
4178 int ch, do_list = 0, do_show = 0, do_update = 1;
4179 const char *delref = NULL, *commit_id_arg = NULL;
4180 struct got_reference *ref = NULL;
4181 struct got_pathlist_head paths;
4182 struct got_pathlist_entry *pe;
4183 struct got_object_id *commit_id = NULL;
4184 char *commit_id_str = NULL;
4188 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4191 commit_id_arg = optarg;
4197 repo_path = realpath(optarg, NULL);
4198 if (repo_path == NULL)
4199 return got_error_from_errno2("realpath",
4201 got_path_strip_trailing_slashes(repo_path);
4215 if (do_list && delref)
4216 errx(1, "-l and -d options are mutually exclusive\n");
4221 if (!do_list && !delref && argc == 0)
4224 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4225 errx(1, "-c option can only be used when creating a branch");
4227 if (do_list || delref) {
4230 } else if (!do_show && argc != 1)
4234 if (do_list || do_show) {
4235 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4239 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4240 "sendfd unveil", NULL) == -1)
4244 cwd = getcwd(NULL, 0);
4246 error = got_error_from_errno("getcwd");
4250 if (repo_path == NULL) {
4251 error = got_worktree_open(&worktree, cwd);
4252 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4258 strdup(got_worktree_get_repo_path(worktree));
4259 if (repo_path == NULL)
4260 error = got_error_from_errno("strdup");
4264 repo_path = strdup(cwd);
4265 if (repo_path == NULL) {
4266 error = got_error_from_errno("strdup");
4272 error = got_repo_open(&repo, repo_path, NULL);
4276 error = apply_unveil(got_repo_get_path(repo), do_list,
4277 worktree ? got_worktree_get_root_path(worktree) : NULL);
4282 error = show_current_branch(repo, worktree);
4284 error = list_branches(repo, worktree);
4286 error = delete_branch(repo, worktree, delref);
4288 if (commit_id_arg == NULL)
4289 commit_id_arg = worktree ?
4290 got_worktree_get_head_ref_name(worktree) :
4292 error = got_repo_match_object_id(&commit_id, NULL,
4293 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4296 error = add_branch(repo, argv[0], commit_id);
4299 if (worktree && do_update) {
4300 int did_something = 0;
4301 char *branch_refname = NULL;
4303 error = got_object_id_str(&commit_id_str, commit_id);
4306 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4310 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4312 error = got_error_from_errno("asprintf");
4315 error = got_ref_open(&ref, repo, branch_refname, 0);
4316 free(branch_refname);
4319 error = switch_head_ref(ref, commit_id, worktree,
4323 error = got_worktree_set_base_commit_id(worktree, repo,
4327 error = got_worktree_checkout_files(worktree, &paths,
4328 repo, update_progress, &did_something,
4329 check_cancelled, NULL);
4333 printf("Updated to commit %s\n", commit_id_str);
4340 got_repo_close(repo);
4342 got_worktree_close(worktree);
4346 free(commit_id_str);
4347 TAILQ_FOREACH(pe, &paths, entry)
4348 free((char *)pe->path);
4349 got_pathlist_free(&paths);
4358 "usage: %s tag [-c commit] [-r repository] [-l] "
4359 "[-m message] name\n", getprogname());
4364 static const struct got_error *
4365 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4367 const struct got_error *err = NULL;
4368 struct got_reflist_entry *re, *se, *new;
4369 struct got_object_id *re_id, *se_id;
4370 struct got_tag_object *re_tag, *se_tag;
4371 time_t re_time, se_time;
4373 SIMPLEQ_FOREACH(re, tags, entry) {
4374 se = SIMPLEQ_FIRST(sorted);
4376 err = got_reflist_entry_dup(&new, re);
4379 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4382 err = got_ref_resolve(&re_id, repo, re->ref);
4385 err = got_object_open_as_tag(&re_tag, repo, re_id);
4389 re_time = got_object_tag_get_tagger_time(re_tag);
4390 got_object_tag_close(re_tag);
4394 err = got_ref_resolve(&se_id, repo, re->ref);
4397 err = got_object_open_as_tag(&se_tag, repo, se_id);
4401 se_time = got_object_tag_get_tagger_time(se_tag);
4402 got_object_tag_close(se_tag);
4404 if (se_time > re_time) {
4405 err = got_reflist_entry_dup(&new, re);
4408 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4411 se = SIMPLEQ_NEXT(se, entry);
4420 static const struct got_error *
4421 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4423 static const struct got_error *err = NULL;
4424 struct got_reflist_head refs;
4425 struct got_reflist_entry *re;
4427 SIMPLEQ_INIT(&refs);
4429 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4433 SIMPLEQ_FOREACH(re, &refs, entry) {
4434 const char *refname;
4435 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4439 struct got_object_id *id;
4440 struct got_tag_object *tag;
4441 struct got_commit_object *commit = NULL;
4443 refname = got_ref_get_name(re->ref);
4444 if (strncmp(refname, "refs/tags/", 10) != 0)
4447 refstr = got_ref_to_str(re->ref);
4448 if (refstr == NULL) {
4449 err = got_error_from_errno("got_ref_to_str");
4452 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4455 err = got_ref_resolve(&id, repo, re->ref);
4458 err = got_object_open_as_tag(&tag, repo, id);
4460 if (err->code != GOT_ERR_OBJ_TYPE) {
4464 /* "lightweight" tag */
4465 err = got_object_open_as_commit(&commit, repo, id);
4470 tagger = got_object_commit_get_committer(commit);
4472 got_object_commit_get_committer_time(commit);
4473 err = got_object_id_str(&id_str, id);
4479 tagger = got_object_tag_get_tagger(tag);
4480 tagger_time = got_object_tag_get_tagger_time(tag);
4481 err = got_object_id_str(&id_str,
4482 got_object_tag_get_object_id(tag));
4486 printf("from: %s\n", tagger);
4487 datestr = get_datestr(&tagger_time, datebuf);
4489 printf("date: %s UTC\n", datestr);
4491 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4493 switch (got_object_tag_get_object_type(tag)) {
4494 case GOT_OBJ_TYPE_BLOB:
4495 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4498 case GOT_OBJ_TYPE_TREE:
4499 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4502 case GOT_OBJ_TYPE_COMMIT:
4503 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4506 case GOT_OBJ_TYPE_TAG:
4507 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4516 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4519 got_object_commit_close(commit);
4521 tagmsg0 = strdup(got_object_tag_get_message(tag));
4522 got_object_tag_close(tag);
4523 if (tagmsg0 == NULL) {
4524 err = got_error_from_errno("strdup");
4531 line = strsep(&tagmsg, "\n");
4533 printf(" %s\n", line);
4538 got_ref_list_free(&refs);
4542 static const struct got_error *
4543 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4544 const char *tag_name, const char *repo_path)
4546 const struct got_error *err = NULL;
4547 char *template = NULL, *initial_content = NULL;
4548 char *editor = NULL;
4551 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4552 err = got_error_from_errno("asprintf");
4556 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4557 commit_id_str, tag_name) == -1) {
4558 err = got_error_from_errno("asprintf");
4562 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4566 dprintf(fd, initial_content);
4569 err = get_editor(&editor);
4572 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4574 free(initial_content);
4578 /* Editor is done; we can now apply unveil(2) */
4580 err = apply_unveil(repo_path, 0, NULL);
4589 static const struct got_error *
4590 add_tag(struct got_repository *repo, const char *tag_name,
4591 const char *commit_arg, const char *tagmsg_arg)
4593 const struct got_error *err = NULL;
4594 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4595 char *label = NULL, *commit_id_str = NULL;
4596 struct got_reference *ref = NULL;
4597 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4598 char *tagmsg_path = NULL, *tag_id_str = NULL;
4599 int preserve_tagmsg = 0;
4602 * Don't let the user create a tag name with a leading '-'.
4603 * While technically a valid reference name, this case is usually
4604 * an unintended typo.
4606 if (tag_name[0] == '-')
4607 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4609 err = get_author(&tagger, repo);
4613 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4614 GOT_OBJ_TYPE_COMMIT, 1, repo);
4618 err = got_object_id_str(&commit_id_str, commit_id);
4622 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4623 refname = strdup(tag_name);
4624 if (refname == NULL) {
4625 err = got_error_from_errno("strdup");
4629 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4630 err = got_error_from_errno("asprintf");
4634 err = got_ref_open(&ref, repo, refname, 0);
4636 err = got_error(GOT_ERR_TAG_EXISTS);
4638 } else if (err->code != GOT_ERR_NOT_REF)
4641 if (tagmsg_arg == NULL) {
4642 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4643 tag_name, got_repo_get_path(repo));
4645 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4646 tagmsg_path != NULL)
4647 preserve_tagmsg = 1;
4652 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4653 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4656 preserve_tagmsg = 1;
4660 err = got_ref_alloc(&ref, refname, tag_id);
4663 preserve_tagmsg = 1;
4667 err = got_ref_write(ref, repo);
4670 preserve_tagmsg = 1;
4674 err = got_object_id_str(&tag_id_str, tag_id);
4677 preserve_tagmsg = 1;
4680 printf("Created tag %s\n", tag_id_str);
4682 if (preserve_tagmsg) {
4683 fprintf(stderr, "%s: tag message preserved in %s\n",
4684 getprogname(), tagmsg_path);
4685 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4686 err = got_error_from_errno2("unlink", tagmsg_path);
4691 free(commit_id_str);
4699 static const struct got_error *
4700 cmd_tag(int argc, char *argv[])
4702 const struct got_error *error = NULL;
4703 struct got_repository *repo = NULL;
4704 struct got_worktree *worktree = NULL;
4705 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4706 char *gitconfig_path = NULL;
4707 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4708 int ch, do_list = 0;
4710 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4713 commit_id_arg = optarg;
4719 repo_path = realpath(optarg, NULL);
4720 if (repo_path == NULL)
4721 return got_error_from_errno2("realpath",
4723 got_path_strip_trailing_slashes(repo_path);
4738 if (commit_id_arg != NULL)
4739 errx(1, "-c option can only be used when creating a tag");
4741 errx(1, "-l and -m options are mutually exclusive");
4744 } else if (argc != 1)
4751 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4755 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4756 "sendfd unveil", NULL) == -1)
4760 cwd = getcwd(NULL, 0);
4762 error = got_error_from_errno("getcwd");
4766 if (repo_path == NULL) {
4767 error = got_worktree_open(&worktree, cwd);
4768 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4774 strdup(got_worktree_get_repo_path(worktree));
4775 if (repo_path == NULL)
4776 error = got_error_from_errno("strdup");
4780 repo_path = strdup(cwd);
4781 if (repo_path == NULL) {
4782 error = got_error_from_errno("strdup");
4789 error = got_repo_open(&repo, repo_path, NULL);
4792 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4795 error = list_tags(repo, worktree);
4797 error = get_gitconfig_path(&gitconfig_path);
4800 error = got_repo_open(&repo, repo_path, gitconfig_path);
4805 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4810 if (commit_id_arg == NULL) {
4811 struct got_reference *head_ref;
4812 struct got_object_id *commit_id;
4813 error = got_ref_open(&head_ref, repo,
4814 worktree ? got_worktree_get_head_ref_name(worktree)
4818 error = got_ref_resolve(&commit_id, repo, head_ref);
4819 got_ref_close(head_ref);
4822 error = got_object_id_str(&commit_id_str, commit_id);
4828 error = add_tag(repo, tag_name,
4829 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4833 got_repo_close(repo);
4835 got_worktree_close(worktree);
4838 free(gitconfig_path);
4839 free(commit_id_str);
4846 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4851 static const struct got_error *
4852 add_progress(void *arg, unsigned char status, const char *path)
4854 while (path[0] == '/')
4856 printf("%c %s\n", status, path);
4860 static const struct got_error *
4861 cmd_add(int argc, char *argv[])
4863 const struct got_error *error = NULL;
4864 struct got_repository *repo = NULL;
4865 struct got_worktree *worktree = NULL;
4867 struct got_pathlist_head paths;
4868 struct got_pathlist_entry *pe;
4869 int ch, can_recurse = 0, no_ignores = 0;
4873 while ((ch = getopt(argc, argv, "IR")) != -1) {
4891 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4898 cwd = getcwd(NULL, 0);
4900 error = got_error_from_errno("getcwd");
4904 error = got_worktree_open(&worktree, cwd);
4908 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4913 error = apply_unveil(got_repo_get_path(repo), 1,
4914 got_worktree_get_root_path(worktree));
4918 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4922 if (!can_recurse && no_ignores) {
4923 error = got_error_msg(GOT_ERR_BAD_PATH,
4924 "disregarding ignores requires -R option");
4932 TAILQ_FOREACH(pe, &paths, entry) {
4933 if (asprintf(&ondisk_path, "%s/%s",
4934 got_worktree_get_root_path(worktree),
4936 error = got_error_from_errno("asprintf");
4939 if (lstat(ondisk_path, &sb) == -1) {
4940 if (errno == ENOENT) {
4944 error = got_error_from_errno2("lstat",
4950 if (S_ISDIR(sb.st_mode)) {
4951 error = got_error_msg(GOT_ERR_BAD_PATH,
4952 "adding directories requires -R option");
4958 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4959 NULL, repo, no_ignores);
4962 got_repo_close(repo);
4964 got_worktree_close(worktree);
4965 TAILQ_FOREACH(pe, &paths, entry)
4966 free((char *)pe->path);
4967 got_pathlist_free(&paths);
4975 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4980 static const struct got_error *
4981 print_remove_status(void *arg, unsigned char status,
4982 unsigned char staged_status, const char *path)
4984 while (path[0] == '/')
4986 if (status == GOT_STATUS_NONEXISTENT)
4988 if (status == staged_status && (status == GOT_STATUS_DELETE))
4989 status = GOT_STATUS_NO_CHANGE;
4990 printf("%c%c %s\n", status, staged_status, path);
4994 static const struct got_error *
4995 cmd_remove(int argc, char *argv[])
4997 const struct got_error *error = NULL;
4998 struct got_worktree *worktree = NULL;
4999 struct got_repository *repo = NULL;
5001 struct got_pathlist_head paths;
5002 struct got_pathlist_entry *pe;
5003 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5007 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5010 delete_local_mods = 1;
5028 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5035 cwd = getcwd(NULL, 0);
5037 error = got_error_from_errno("getcwd");
5040 error = got_worktree_open(&worktree, cwd);
5044 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5049 error = apply_unveil(got_repo_get_path(repo), 1,
5050 got_worktree_get_root_path(worktree));
5054 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5061 TAILQ_FOREACH(pe, &paths, entry) {
5062 if (asprintf(&ondisk_path, "%s/%s",
5063 got_worktree_get_root_path(worktree),
5065 error = got_error_from_errno("asprintf");
5068 if (lstat(ondisk_path, &sb) == -1) {
5069 if (errno == ENOENT) {
5073 error = got_error_from_errno2("lstat",
5079 if (S_ISDIR(sb.st_mode)) {
5080 error = got_error_msg(GOT_ERR_BAD_PATH,
5081 "removing directories requires -R option");
5087 error = got_worktree_schedule_delete(worktree, &paths,
5088 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5091 got_repo_close(repo);
5093 got_worktree_close(worktree);
5094 TAILQ_FOREACH(pe, &paths, entry)
5095 free((char *)pe->path);
5096 got_pathlist_free(&paths);
5104 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5105 "path ...\n", getprogname());
5109 static const struct got_error *
5110 revert_progress(void *arg, unsigned char status, const char *path)
5112 if (status == GOT_STATUS_UNVERSIONED)
5115 while (path[0] == '/')
5117 printf("%c %s\n", status, path);
5121 struct choose_patch_arg {
5122 FILE *patch_script_file;
5126 static const struct got_error *
5127 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5128 int nchanges, const char *action)
5131 size_t linesize = 0;
5135 case GOT_STATUS_ADD:
5136 printf("A %s\n%s this addition? [y/n] ", path, action);
5138 case GOT_STATUS_DELETE:
5139 printf("D %s\n%s this deletion? [y/n] ", path, action);
5141 case GOT_STATUS_MODIFY:
5142 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5143 return got_error_from_errno("fseek");
5144 printf(GOT_COMMIT_SEP_STR);
5145 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5147 if (ferror(patch_file))
5148 return got_error_from_errno("getline");
5149 printf(GOT_COMMIT_SEP_STR);
5150 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5151 path, n, nchanges, action);
5154 return got_error_path(path, GOT_ERR_FILE_STATUS);
5160 static const struct got_error *
5161 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5162 FILE *patch_file, int n, int nchanges)
5164 const struct got_error *err = NULL;
5166 size_t linesize = 0;
5169 struct choose_patch_arg *a = arg;
5171 *choice = GOT_PATCH_CHOICE_NONE;
5173 if (a->patch_script_file) {
5175 err = show_change(status, path, patch_file, n, nchanges,
5179 linelen = getline(&line, &linesize, a->patch_script_file);
5180 if (linelen == -1) {
5181 if (ferror(a->patch_script_file))
5182 return got_error_from_errno("getline");
5185 nl = strchr(line, '\n');
5188 if (strcmp(line, "y") == 0) {
5189 *choice = GOT_PATCH_CHOICE_YES;
5191 } else if (strcmp(line, "n") == 0) {
5192 *choice = GOT_PATCH_CHOICE_NO;
5194 } else if (strcmp(line, "q") == 0 &&
5195 status == GOT_STATUS_MODIFY) {
5196 *choice = GOT_PATCH_CHOICE_QUIT;
5199 printf("invalid response '%s'\n", line);
5204 while (resp != 'y' && resp != 'n' && resp != 'q') {
5205 err = show_change(status, path, patch_file, n, nchanges,
5212 if (status == GOT_STATUS_MODIFY) {
5213 if (resp != 'y' && resp != 'n' && resp != 'q') {
5214 printf("invalid response '%c'\n", resp);
5217 } else if (resp != 'y' && resp != 'n') {
5218 printf("invalid response '%c'\n", resp);
5224 *choice = GOT_PATCH_CHOICE_YES;
5225 else if (resp == 'n')
5226 *choice = GOT_PATCH_CHOICE_NO;
5227 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5228 *choice = GOT_PATCH_CHOICE_QUIT;
5234 static const struct got_error *
5235 cmd_revert(int argc, char *argv[])
5237 const struct got_error *error = NULL;
5238 struct got_worktree *worktree = NULL;
5239 struct got_repository *repo = NULL;
5240 char *cwd = NULL, *path = NULL;
5241 struct got_pathlist_head paths;
5242 struct got_pathlist_entry *pe;
5243 int ch, can_recurse = 0, pflag = 0;
5244 FILE *patch_script_file = NULL;
5245 const char *patch_script_path = NULL;
5246 struct choose_patch_arg cpa;
5250 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5256 patch_script_path = optarg;
5271 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5272 "unveil", NULL) == -1)
5277 if (patch_script_path && !pflag)
5278 errx(1, "-F option can only be used together with -p option");
5280 cwd = getcwd(NULL, 0);
5282 error = got_error_from_errno("getcwd");
5285 error = got_worktree_open(&worktree, cwd);
5289 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5294 if (patch_script_path) {
5295 patch_script_file = fopen(patch_script_path, "r");
5296 if (patch_script_file == NULL) {
5297 error = got_error_from_errno2("fopen",
5302 error = apply_unveil(got_repo_get_path(repo), 1,
5303 got_worktree_get_root_path(worktree));
5307 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5314 TAILQ_FOREACH(pe, &paths, entry) {
5315 if (asprintf(&ondisk_path, "%s/%s",
5316 got_worktree_get_root_path(worktree),
5318 error = got_error_from_errno("asprintf");
5321 if (lstat(ondisk_path, &sb) == -1) {
5322 if (errno == ENOENT) {
5326 error = got_error_from_errno2("lstat",
5332 if (S_ISDIR(sb.st_mode)) {
5333 error = got_error_msg(GOT_ERR_BAD_PATH,
5334 "reverting directories requires -R option");
5340 cpa.patch_script_file = patch_script_file;
5341 cpa.action = "revert";
5342 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5343 pflag ? choose_patch : NULL, &cpa, repo);
5345 if (patch_script_file && fclose(patch_script_file) == EOF &&
5347 error = got_error_from_errno2("fclose", patch_script_path);
5349 got_repo_close(repo);
5351 got_worktree_close(worktree);
5360 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5365 struct collect_commit_logmsg_arg {
5366 const char *cmdline_log;
5368 const char *worktree_path;
5369 const char *branch_name;
5370 const char *repo_path;
5375 static const struct got_error *
5376 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5379 char *initial_content = NULL;
5380 struct got_pathlist_entry *pe;
5381 const struct got_error *err = NULL;
5382 char *template = NULL;
5383 struct collect_commit_logmsg_arg *a = arg;
5387 /* if a message was specified on the command line, just use it */
5388 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5389 len = strlen(a->cmdline_log) + 1;
5390 *logmsg = malloc(len + 1);
5391 if (*logmsg == NULL)
5392 return got_error_from_errno("malloc");
5393 strlcpy(*logmsg, a->cmdline_log, len);
5397 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5398 return got_error_from_errno("asprintf");
5400 if (asprintf(&initial_content,
5401 "\n# changes to be committed on branch %s:\n",
5402 a->branch_name) == -1)
5403 return got_error_from_errno("asprintf");
5405 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5409 dprintf(fd, initial_content);
5411 TAILQ_FOREACH(pe, commitable_paths, entry) {
5412 struct got_commitable *ct = pe->data;
5413 dprintf(fd, "# %c %s\n",
5414 got_commitable_get_status(ct),
5415 got_commitable_get_path(ct));
5419 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5421 free(initial_content);
5424 /* Editor is done; we can now apply unveil(2) */
5426 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5435 static const struct got_error *
5436 cmd_commit(int argc, char *argv[])
5438 const struct got_error *error = NULL;
5439 struct got_worktree *worktree = NULL;
5440 struct got_repository *repo = NULL;
5441 char *cwd = NULL, *id_str = NULL;
5442 struct got_object_id *id = NULL;
5443 const char *logmsg = NULL;
5444 struct collect_commit_logmsg_arg cl_arg;
5445 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5446 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5447 struct got_pathlist_head paths;
5450 cl_arg.logmsg_path = NULL;
5452 while ((ch = getopt(argc, argv, "m:")) != -1) {
5467 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5468 "unveil", NULL) == -1)
5471 cwd = getcwd(NULL, 0);
5473 error = got_error_from_errno("getcwd");
5476 error = got_worktree_open(&worktree, cwd);
5480 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5483 if (rebase_in_progress) {
5484 error = got_error(GOT_ERR_REBASING);
5488 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5493 error = get_gitconfig_path(&gitconfig_path);
5496 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5501 error = get_author(&author, repo);
5506 * unveil(2) traverses exec(2); if an editor is used we have
5507 * to apply unveil after the log message has been written.
5509 if (logmsg == NULL || strlen(logmsg) == 0)
5510 error = get_editor(&editor);
5512 error = apply_unveil(got_repo_get_path(repo), 0,
5513 got_worktree_get_root_path(worktree));
5517 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5521 cl_arg.editor = editor;
5522 cl_arg.cmdline_log = logmsg;
5523 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5524 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5525 if (!histedit_in_progress) {
5526 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5527 error = got_error(GOT_ERR_COMMIT_BRANCH);
5530 cl_arg.branch_name += 11;
5532 cl_arg.repo_path = got_repo_get_path(repo);
5533 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5534 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5536 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5537 cl_arg.logmsg_path != NULL)
5538 preserve_logmsg = 1;
5542 error = got_object_id_str(&id_str, id);
5545 printf("Created commit %s\n", id_str);
5547 if (preserve_logmsg) {
5548 fprintf(stderr, "%s: log message preserved in %s\n",
5549 getprogname(), cl_arg.logmsg_path);
5550 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5552 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5553 free(cl_arg.logmsg_path);
5555 got_repo_close(repo);
5557 got_worktree_close(worktree);
5560 free(gitconfig_path);
5567 usage_cherrypick(void)
5569 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5573 static const struct got_error *
5574 cmd_cherrypick(int argc, char *argv[])
5576 const struct got_error *error = NULL;
5577 struct got_worktree *worktree = NULL;
5578 struct got_repository *repo = NULL;
5579 char *cwd = NULL, *commit_id_str = NULL;
5580 struct got_object_id *commit_id = NULL;
5581 struct got_commit_object *commit = NULL;
5582 struct got_object_qid *pid;
5583 struct got_reference *head_ref = NULL;
5584 int ch, did_something = 0;
5586 while ((ch = getopt(argc, argv, "")) != -1) {
5598 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5599 "unveil", NULL) == -1)
5605 cwd = getcwd(NULL, 0);
5607 error = got_error_from_errno("getcwd");
5610 error = got_worktree_open(&worktree, cwd);
5614 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5619 error = apply_unveil(got_repo_get_path(repo), 0,
5620 got_worktree_get_root_path(worktree));
5624 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5625 GOT_OBJ_TYPE_COMMIT, repo);
5626 if (error != NULL) {
5627 struct got_reference *ref;
5628 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5630 error = got_ref_open(&ref, repo, argv[0], 0);
5633 error = got_ref_resolve(&commit_id, repo, ref);
5638 error = got_object_id_str(&commit_id_str, commit_id);
5642 error = got_ref_open(&head_ref, repo,
5643 got_worktree_get_head_ref_name(worktree), 0);
5647 error = check_same_branch(commit_id, head_ref, NULL, repo);
5649 if (error->code != GOT_ERR_ANCESTRY)
5653 error = got_error(GOT_ERR_SAME_BRANCH);
5657 error = got_object_open_as_commit(&commit, repo, commit_id);
5660 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5661 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5662 commit_id, repo, update_progress, &did_something, check_cancelled,
5668 printf("Merged commit %s\n", commit_id_str);
5671 got_object_commit_close(commit);
5672 free(commit_id_str);
5674 got_ref_close(head_ref);
5676 got_worktree_close(worktree);
5678 got_repo_close(repo);
5685 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5689 static const struct got_error *
5690 cmd_backout(int argc, char *argv[])
5692 const struct got_error *error = NULL;
5693 struct got_worktree *worktree = NULL;
5694 struct got_repository *repo = NULL;
5695 char *cwd = NULL, *commit_id_str = NULL;
5696 struct got_object_id *commit_id = NULL;
5697 struct got_commit_object *commit = NULL;
5698 struct got_object_qid *pid;
5699 struct got_reference *head_ref = NULL;
5700 int ch, did_something = 0;
5702 while ((ch = getopt(argc, argv, "")) != -1) {
5714 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5715 "unveil", NULL) == -1)
5721 cwd = getcwd(NULL, 0);
5723 error = got_error_from_errno("getcwd");
5726 error = got_worktree_open(&worktree, cwd);
5730 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5735 error = apply_unveil(got_repo_get_path(repo), 0,
5736 got_worktree_get_root_path(worktree));
5740 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5741 GOT_OBJ_TYPE_COMMIT, repo);
5742 if (error != NULL) {
5743 struct got_reference *ref;
5744 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5746 error = got_ref_open(&ref, repo, argv[0], 0);
5749 error = got_ref_resolve(&commit_id, repo, ref);
5754 error = got_object_id_str(&commit_id_str, commit_id);
5758 error = got_ref_open(&head_ref, repo,
5759 got_worktree_get_head_ref_name(worktree), 0);
5763 error = check_same_branch(commit_id, head_ref, NULL, repo);
5767 error = got_object_open_as_commit(&commit, repo, commit_id);
5770 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5772 error = got_error(GOT_ERR_ROOT_COMMIT);
5776 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5777 update_progress, &did_something, check_cancelled, NULL);
5782 printf("Backed out commit %s\n", commit_id_str);
5785 got_object_commit_close(commit);
5786 free(commit_id_str);
5788 got_ref_close(head_ref);
5790 got_worktree_close(worktree);
5792 got_repo_close(repo);
5799 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5805 trim_logmsg(char *logmsg, int limit)
5810 len = strlen(logmsg);
5814 nl = strchr(logmsg, '\n');
5819 static const struct got_error *
5820 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5822 const struct got_error *err;
5823 char *logmsg0 = NULL;
5826 err = got_object_commit_get_logmsg(&logmsg0, commit);
5831 while (isspace((unsigned char)s[0]))
5834 *logmsg = strdup(s);
5835 if (*logmsg == NULL) {
5836 err = got_error_from_errno("strdup");
5840 trim_logmsg(*logmsg, limit);
5846 static const struct got_error *
5847 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5849 const struct got_error *err;
5850 struct got_commit_object *commit = NULL;
5851 char *id_str = NULL, *logmsg = NULL;
5853 err = got_object_open_as_commit(&commit, repo, id);
5857 err = got_object_id_str(&id_str, id);
5863 err = get_short_logmsg(&logmsg, 42, commit);
5867 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5870 got_object_commit_close(commit);
5875 static const struct got_error *
5876 show_rebase_progress(struct got_commit_object *commit,
5877 struct got_object_id *old_id, struct got_object_id *new_id)
5879 const struct got_error *err;
5880 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5882 err = got_object_id_str(&old_id_str, old_id);
5887 err = got_object_id_str(&new_id_str, new_id);
5892 old_id_str[12] = '\0';
5894 new_id_str[12] = '\0';
5896 err = get_short_logmsg(&logmsg, 42, commit);
5900 printf("%s -> %s: %s\n", old_id_str,
5901 new_id_str ? new_id_str : "no-op change", logmsg);
5909 static const struct got_error *
5910 rebase_progress(void *arg, unsigned char status, const char *path)
5912 unsigned char *rebase_status = arg;
5914 while (path[0] == '/')
5916 printf("%c %s\n", status, path);
5918 if (*rebase_status == GOT_STATUS_CONFLICT)
5920 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5921 *rebase_status = status;
5925 static const struct got_error *
5926 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5927 struct got_reference *branch, struct got_reference *new_base_branch,
5928 struct got_reference *tmp_branch, struct got_repository *repo)
5930 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5931 return got_worktree_rebase_complete(worktree, fileindex,
5932 new_base_branch, tmp_branch, branch, repo);
5935 static const struct got_error *
5936 rebase_commit(struct got_pathlist_head *merged_paths,
5937 struct got_worktree *worktree, struct got_fileindex *fileindex,
5938 struct got_reference *tmp_branch,
5939 struct got_object_id *commit_id, struct got_repository *repo)
5941 const struct got_error *error;
5942 struct got_commit_object *commit;
5943 struct got_object_id *new_commit_id;
5945 error = got_object_open_as_commit(&commit, repo, commit_id);
5949 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5950 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5952 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5954 error = show_rebase_progress(commit, commit_id, NULL);
5956 error = show_rebase_progress(commit, commit_id, new_commit_id);
5957 free(new_commit_id);
5960 got_object_commit_close(commit);
5964 struct check_path_prefix_arg {
5965 const char *path_prefix;
5970 static const struct got_error *
5971 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5972 struct got_blob_object *blob2, struct got_object_id *id1,
5973 struct got_object_id *id2, const char *path1, const char *path2,
5974 mode_t mode1, mode_t mode2, struct got_repository *repo)
5976 struct check_path_prefix_arg *a = arg;
5978 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5979 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5980 return got_error(a->errcode);
5985 static const struct got_error *
5986 check_path_prefix(struct got_object_id *parent_id,
5987 struct got_object_id *commit_id, const char *path_prefix,
5988 int errcode, struct got_repository *repo)
5990 const struct got_error *err;
5991 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5992 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5993 struct check_path_prefix_arg cpp_arg;
5995 if (got_path_is_root_dir(path_prefix))
5998 err = got_object_open_as_commit(&commit, repo, commit_id);
6002 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6006 err = got_object_open_as_tree(&tree1, repo,
6007 got_object_commit_get_tree_id(parent_commit));
6011 err = got_object_open_as_tree(&tree2, repo,
6012 got_object_commit_get_tree_id(commit));
6016 cpp_arg.path_prefix = path_prefix;
6017 while (cpp_arg.path_prefix[0] == '/')
6018 cpp_arg.path_prefix++;
6019 cpp_arg.len = strlen(cpp_arg.path_prefix);
6020 cpp_arg.errcode = errcode;
6021 err = got_diff_tree(tree1, tree2, "", "", repo,
6022 check_path_prefix_in_diff, &cpp_arg, 0);
6025 got_object_tree_close(tree1);
6027 got_object_tree_close(tree2);
6029 got_object_commit_close(commit);
6031 got_object_commit_close(parent_commit);
6035 static const struct got_error *
6036 collect_commits(struct got_object_id_queue *commits,
6037 struct got_object_id *initial_commit_id,
6038 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6039 const char *path_prefix, int path_prefix_errcode,
6040 struct got_repository *repo)
6042 const struct got_error *err = NULL;
6043 struct got_commit_graph *graph = NULL;
6044 struct got_object_id *parent_id = NULL;
6045 struct got_object_qid *qid;
6046 struct got_object_id *commit_id = initial_commit_id;
6048 err = got_commit_graph_open(&graph, "/", 1);
6052 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6053 check_cancelled, NULL);
6056 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6057 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6058 check_cancelled, NULL);
6060 if (err->code == GOT_ERR_ITER_COMPLETED) {
6061 err = got_error_msg(GOT_ERR_ANCESTRY,
6062 "ran out of commits to rebase before "
6063 "youngest common ancestor commit has "
6068 err = check_path_prefix(parent_id, commit_id,
6069 path_prefix, path_prefix_errcode, repo);
6073 err = got_object_qid_alloc(&qid, commit_id);
6076 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6077 commit_id = parent_id;
6081 got_commit_graph_close(graph);
6085 static const struct got_error *
6086 cmd_rebase(int argc, char *argv[])
6088 const struct got_error *error = NULL;
6089 struct got_worktree *worktree = NULL;
6090 struct got_repository *repo = NULL;
6091 struct got_fileindex *fileindex = NULL;
6093 struct got_reference *branch = NULL;
6094 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6095 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6096 struct got_object_id *resume_commit_id = NULL;
6097 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6098 struct got_commit_object *commit = NULL;
6099 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6100 int histedit_in_progress = 0;
6101 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6102 struct got_object_id_queue commits;
6103 struct got_pathlist_head merged_paths;
6104 const struct got_object_id_queue *parent_ids;
6105 struct got_object_qid *qid, *pid;
6107 SIMPLEQ_INIT(&commits);
6108 TAILQ_INIT(&merged_paths);
6110 while ((ch = getopt(argc, argv, "ac")) != -1) {
6116 continue_rebase = 1;
6128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6129 "unveil", NULL) == -1)
6132 if (abort_rebase && continue_rebase)
6134 else if (abort_rebase || continue_rebase) {
6137 } else if (argc != 1)
6140 cwd = getcwd(NULL, 0);
6142 error = got_error_from_errno("getcwd");
6145 error = got_worktree_open(&worktree, cwd);
6149 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6154 error = apply_unveil(got_repo_get_path(repo), 0,
6155 got_worktree_get_root_path(worktree));
6159 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6163 if (histedit_in_progress) {
6164 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6168 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6174 if (!rebase_in_progress) {
6175 error = got_error(GOT_ERR_NOT_REBASING);
6178 error = got_worktree_rebase_continue(&resume_commit_id,
6179 &new_base_branch, &tmp_branch, &branch, &fileindex,
6183 printf("Switching work tree to %s\n",
6184 got_ref_get_symref_target(new_base_branch));
6185 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6186 new_base_branch, update_progress, &did_something);
6189 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6190 goto done; /* nothing else to do */
6193 if (continue_rebase) {
6194 if (!rebase_in_progress) {
6195 error = got_error(GOT_ERR_NOT_REBASING);
6198 error = got_worktree_rebase_continue(&resume_commit_id,
6199 &new_base_branch, &tmp_branch, &branch, &fileindex,
6204 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6205 resume_commit_id, repo);
6209 yca_id = got_object_id_dup(resume_commit_id);
6210 if (yca_id == NULL) {
6211 error = got_error_from_errno("got_object_id_dup");
6215 error = got_ref_open(&branch, repo, argv[0], 0);
6220 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6224 if (!continue_rebase) {
6225 struct got_object_id *base_commit_id;
6227 base_commit_id = got_worktree_get_base_commit_id(worktree);
6228 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6229 base_commit_id, branch_head_commit_id, repo,
6230 check_cancelled, NULL);
6233 if (yca_id == NULL) {
6234 error = got_error_msg(GOT_ERR_ANCESTRY,
6235 "specified branch shares no common ancestry "
6236 "with work tree's branch");
6240 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6242 if (error->code != GOT_ERR_ANCESTRY)
6246 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6247 "specified branch resolves to a commit which "
6248 "is already contained in work tree's branch");
6251 error = got_worktree_rebase_prepare(&new_base_branch,
6252 &tmp_branch, &fileindex, worktree, branch, repo);
6257 commit_id = branch_head_commit_id;
6258 error = got_object_open_as_commit(&commit, repo, commit_id);
6262 parent_ids = got_object_commit_get_parent_ids(commit);
6263 pid = SIMPLEQ_FIRST(parent_ids);
6265 if (!continue_rebase) {
6267 error = got_worktree_rebase_abort(worktree, fileindex,
6268 repo, new_base_branch, update_progress,
6272 printf("Rebase of %s aborted\n",
6273 got_ref_get_name(branch));
6275 error = got_error(GOT_ERR_EMPTY_REBASE);
6278 error = collect_commits(&commits, commit_id, pid->id,
6279 yca_id, got_worktree_get_path_prefix(worktree),
6280 GOT_ERR_REBASE_PATH, repo);
6281 got_object_commit_close(commit);
6286 if (SIMPLEQ_EMPTY(&commits)) {
6287 if (continue_rebase) {
6288 error = rebase_complete(worktree, fileindex,
6289 branch, new_base_branch, tmp_branch, repo);
6292 /* Fast-forward the reference of the branch. */
6293 struct got_object_id *new_head_commit_id;
6295 error = got_ref_resolve(&new_head_commit_id, repo,
6299 error = got_object_id_str(&id_str, new_head_commit_id);
6300 printf("Forwarding %s to commit %s\n",
6301 got_ref_get_name(branch), id_str);
6303 error = got_ref_change_ref(branch,
6304 new_head_commit_id);
6311 SIMPLEQ_FOREACH(qid, &commits, entry) {
6312 commit_id = qid->id;
6313 parent_id = pid ? pid->id : yca_id;
6316 error = got_worktree_rebase_merge_files(&merged_paths,
6317 worktree, fileindex, parent_id, commit_id, repo,
6318 rebase_progress, &rebase_status, check_cancelled, NULL);
6322 if (rebase_status == GOT_STATUS_CONFLICT) {
6323 error = show_rebase_merge_conflict(qid->id, repo);
6326 got_worktree_rebase_pathlist_free(&merged_paths);
6330 error = rebase_commit(&merged_paths, worktree, fileindex,
6331 tmp_branch, commit_id, repo);
6332 got_worktree_rebase_pathlist_free(&merged_paths);
6337 if (rebase_status == GOT_STATUS_CONFLICT) {
6338 error = got_worktree_rebase_postpone(worktree, fileindex);
6341 error = got_error_msg(GOT_ERR_CONFLICTS,
6342 "conflicts must be resolved before rebasing can continue");
6344 error = rebase_complete(worktree, fileindex, branch,
6345 new_base_branch, tmp_branch, repo);
6347 got_object_id_queue_free(&commits);
6348 free(branch_head_commit_id);
6349 free(resume_commit_id);
6352 got_object_commit_close(commit);
6354 got_ref_close(branch);
6355 if (new_base_branch)
6356 got_ref_close(new_base_branch);
6358 got_ref_close(tmp_branch);
6360 got_worktree_close(worktree);
6362 got_repo_close(repo);
6367 usage_histedit(void)
6369 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6374 #define GOT_HISTEDIT_PICK 'p'
6375 #define GOT_HISTEDIT_EDIT 'e'
6376 #define GOT_HISTEDIT_FOLD 'f'
6377 #define GOT_HISTEDIT_DROP 'd'
6378 #define GOT_HISTEDIT_MESG 'm'
6380 static struct got_histedit_cmd {
6384 } got_histedit_cmds[] = {
6385 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6386 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6387 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6389 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6390 { GOT_HISTEDIT_MESG, "mesg",
6391 "single-line log message for commit above (open editor if empty)" },
6394 struct got_histedit_list_entry {
6395 TAILQ_ENTRY(got_histedit_list_entry) entry;
6396 struct got_object_id *commit_id;
6397 const struct got_histedit_cmd *cmd;
6400 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6402 static const struct got_error *
6403 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6404 FILE *f, struct got_repository *repo)
6406 const struct got_error *err = NULL;
6407 char *logmsg = NULL, *id_str = NULL;
6408 struct got_commit_object *commit = NULL;
6411 err = got_object_open_as_commit(&commit, repo, commit_id);
6415 err = get_short_logmsg(&logmsg, 34, commit);
6419 err = got_object_id_str(&id_str, commit_id);
6423 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6425 err = got_ferror(f, GOT_ERR_IO);
6428 got_object_commit_close(commit);
6434 static const struct got_error *
6435 histedit_write_commit_list(struct got_object_id_queue *commits,
6436 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6439 struct got_object_qid *qid;
6441 if (SIMPLEQ_EMPTY(commits))
6442 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6444 SIMPLEQ_FOREACH(qid, commits, entry) {
6445 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6449 if (edit_logmsg_only) {
6450 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6452 err = got_ferror(f, GOT_ERR_IO);
6461 static const struct got_error *
6462 write_cmd_list(FILE *f, const char *branch_name,
6463 struct got_object_id_queue *commits)
6465 const struct got_error *err = NULL;
6468 struct got_object_qid *qid;
6470 qid = SIMPLEQ_FIRST(commits);
6471 err = got_object_id_str(&id_str, qid->id);
6476 "# Editing the history of branch '%s' starting at\n"
6478 "# Commits will be processed in order from top to "
6479 "bottom of this file.\n", branch_name, id_str);
6481 err = got_ferror(f, GOT_ERR_IO);
6485 n = fprintf(f, "# Available histedit commands:\n");
6487 err = got_ferror(f, GOT_ERR_IO);
6491 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6492 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6493 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6496 err = got_ferror(f, GOT_ERR_IO);
6505 static const struct got_error *
6506 histedit_syntax_error(int lineno)
6508 static char msg[42];
6511 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6513 if (ret == -1 || ret >= sizeof(msg))
6514 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6516 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6519 static const struct got_error *
6520 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6521 char *logmsg, struct got_repository *repo)
6523 const struct got_error *err;
6524 struct got_commit_object *folded_commit = NULL;
6525 char *id_str, *folded_logmsg = NULL;
6527 err = got_object_id_str(&id_str, hle->commit_id);
6531 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6535 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6538 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6539 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6540 folded_logmsg) == -1) {
6541 err = got_error_from_errno("asprintf");
6545 got_object_commit_close(folded_commit);
6547 free(folded_logmsg);
6551 static struct got_histedit_list_entry *
6552 get_folded_commits(struct got_histedit_list_entry *hle)
6554 struct got_histedit_list_entry *prev, *folded = NULL;
6556 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6557 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6558 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6559 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6561 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6567 static const struct got_error *
6568 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6569 struct got_repository *repo)
6571 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6572 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6573 const struct got_error *err = NULL;
6574 struct got_commit_object *commit = NULL;
6576 struct got_histedit_list_entry *folded = NULL;
6578 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6582 folded = get_folded_commits(hle);
6584 while (folded != hle) {
6585 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6586 folded = TAILQ_NEXT(folded, entry);
6589 err = append_folded_commit_msg(&new_msg, folded,
6595 folded = TAILQ_NEXT(folded, entry);
6599 err = got_object_id_str(&id_str, hle->commit_id);
6602 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6605 if (asprintf(&new_msg,
6606 "%s\n# original log message of commit %s: %s",
6607 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6608 err = got_error_from_errno("asprintf");
6614 err = got_object_id_str(&id_str, hle->commit_id);
6618 err = got_opentemp_named_fd(&logmsg_path, &fd,
6619 GOT_TMPDIR_STR "/got-logmsg");
6623 dprintf(fd, logmsg);
6626 err = get_editor(&editor);
6630 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6632 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6634 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6637 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6638 err = got_error_from_errno2("unlink", logmsg_path);
6644 got_object_commit_close(commit);
6648 static const struct got_error *
6649 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6650 FILE *f, struct got_repository *repo)
6652 const struct got_error *err = NULL;
6653 char *line = NULL, *p, *end;
6657 const struct got_histedit_cmd *cmd;
6658 struct got_object_id *commit_id = NULL;
6659 struct got_histedit_list_entry *hle = NULL;
6662 len = getline(&line, &size, f);
6664 const struct got_error *getline_err;
6667 getline_err = got_error_from_errno("getline");
6668 err = got_ferror(f, getline_err->code);
6673 while (isspace((unsigned char)p[0]))
6675 if (p[0] == '#' || p[0] == '\0') {
6681 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6682 cmd = &got_histedit_cmds[i];
6683 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6684 isspace((unsigned char)p[strlen(cmd->name)])) {
6685 p += strlen(cmd->name);
6688 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6693 if (i == nitems(got_histedit_cmds)) {
6694 err = histedit_syntax_error(lineno);
6697 while (isspace((unsigned char)p[0]))
6699 if (cmd->code == GOT_HISTEDIT_MESG) {
6700 if (hle == NULL || hle->logmsg != NULL) {
6701 err = got_error(GOT_ERR_HISTEDIT_CMD);
6705 err = histedit_edit_logmsg(hle, repo);
6709 hle->logmsg = strdup(p);
6710 if (hle->logmsg == NULL) {
6711 err = got_error_from_errno("strdup");
6720 while (end[0] && !isspace((unsigned char)end[0]))
6724 err = got_object_resolve_id_str(&commit_id, repo, p);
6726 /* override error code */
6727 err = histedit_syntax_error(lineno);
6731 hle = malloc(sizeof(*hle));
6733 err = got_error_from_errno("malloc");
6737 hle->commit_id = commit_id;
6742 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6750 static const struct got_error *
6751 histedit_check_script(struct got_histedit_list *histedit_cmds,
6752 struct got_object_id_queue *commits, struct got_repository *repo)
6754 const struct got_error *err = NULL;
6755 struct got_object_qid *qid;
6756 struct got_histedit_list_entry *hle;
6757 static char msg[92];
6760 if (TAILQ_EMPTY(histedit_cmds))
6761 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6762 "histedit script contains no commands");
6763 if (SIMPLEQ_EMPTY(commits))
6764 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6766 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6767 struct got_histedit_list_entry *hle2;
6768 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6771 if (got_object_id_cmp(hle->commit_id,
6772 hle2->commit_id) != 0)
6774 err = got_object_id_str(&id_str, hle->commit_id);
6777 snprintf(msg, sizeof(msg), "commit %s is listed "
6778 "more than once in histedit script", id_str);
6780 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6784 SIMPLEQ_FOREACH(qid, commits, entry) {
6785 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6786 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6790 err = got_object_id_str(&id_str, qid->id);
6793 snprintf(msg, sizeof(msg),
6794 "commit %s missing from histedit script", id_str);
6796 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6800 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6801 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6802 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6803 "last commit in histedit script cannot be folded");
6808 static const struct got_error *
6809 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6810 const char *path, struct got_object_id_queue *commits,
6811 struct got_repository *repo)
6813 const struct got_error *err = NULL;
6817 err = get_editor(&editor);
6821 if (spawn_editor(editor, path) == -1) {
6822 err = got_error_from_errno("failed spawning editor");
6826 f = fopen(path, "r");
6828 err = got_error_from_errno("fopen");
6831 err = histedit_parse_list(histedit_cmds, f, repo);
6835 err = histedit_check_script(histedit_cmds, commits, repo);
6837 if (f && fclose(f) != 0 && err == NULL)
6838 err = got_error_from_errno("fclose");
6843 static const struct got_error *
6844 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6845 struct got_object_id_queue *, const char *, const char *,
6846 struct got_repository *);
6848 static const struct got_error *
6849 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6850 struct got_object_id_queue *commits, const char *branch_name,
6851 int edit_logmsg_only, struct got_repository *repo)
6853 const struct got_error *err;
6857 err = got_opentemp_named(&path, &f, "got-histedit");
6861 err = write_cmd_list(f, branch_name, commits);
6865 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6869 if (edit_logmsg_only) {
6871 err = histedit_parse_list(histedit_cmds, f, repo);
6873 if (fclose(f) != 0) {
6874 err = got_error_from_errno("fclose");
6878 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6880 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6881 err->code != GOT_ERR_HISTEDIT_CMD)
6883 err = histedit_edit_list_retry(histedit_cmds, err,
6884 commits, path, branch_name, repo);
6888 if (f && fclose(f) != 0 && err == NULL)
6889 err = got_error_from_errno("fclose");
6890 if (path && unlink(path) != 0 && err == NULL)
6891 err = got_error_from_errno2("unlink", path);
6896 static const struct got_error *
6897 histedit_save_list(struct got_histedit_list *histedit_cmds,
6898 struct got_worktree *worktree, struct got_repository *repo)
6900 const struct got_error *err = NULL;
6903 struct got_histedit_list_entry *hle;
6904 struct got_commit_object *commit = NULL;
6906 err = got_worktree_get_histedit_script_path(&path, worktree);
6910 f = fopen(path, "w");
6912 err = got_error_from_errno2("fopen", path);
6915 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6916 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6922 int n = fprintf(f, "%c %s\n",
6923 GOT_HISTEDIT_MESG, hle->logmsg);
6925 err = got_ferror(f, GOT_ERR_IO);
6931 if (f && fclose(f) != 0 && err == NULL)
6932 err = got_error_from_errno("fclose");
6935 got_object_commit_close(commit);
6940 histedit_free_list(struct got_histedit_list *histedit_cmds)
6942 struct got_histedit_list_entry *hle;
6944 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6945 TAILQ_REMOVE(histedit_cmds, hle, entry);
6950 static const struct got_error *
6951 histedit_load_list(struct got_histedit_list *histedit_cmds,
6952 const char *path, struct got_repository *repo)
6954 const struct got_error *err = NULL;
6957 f = fopen(path, "r");
6959 err = got_error_from_errno2("fopen", path);
6963 err = histedit_parse_list(histedit_cmds, f, repo);
6965 if (f && fclose(f) != 0 && err == NULL)
6966 err = got_error_from_errno("fclose");
6970 static const struct got_error *
6971 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6972 const struct got_error *edit_err, struct got_object_id_queue *commits,
6973 const char *path, const char *branch_name, struct got_repository *repo)
6975 const struct got_error *err = NULL, *prev_err = edit_err;
6978 while (resp != 'c' && resp != 'r' && resp != 'a') {
6979 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6980 "or (a)bort: ", getprogname(), prev_err->msg);
6985 histedit_free_list(histedit_cmds);
6986 err = histedit_run_editor(histedit_cmds, path, commits,
6989 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6990 err->code != GOT_ERR_HISTEDIT_CMD)
6997 } else if (resp == 'r') {
6998 histedit_free_list(histedit_cmds);
6999 err = histedit_edit_script(histedit_cmds,
7000 commits, branch_name, 0, repo);
7002 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7003 err->code != GOT_ERR_HISTEDIT_CMD)
7010 } else if (resp == 'a') {
7011 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7014 printf("invalid response '%c'\n", resp);
7020 static const struct got_error *
7021 histedit_complete(struct got_worktree *worktree,
7022 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7023 struct got_reference *branch, struct got_repository *repo)
7025 printf("Switching work tree to %s\n",
7026 got_ref_get_symref_target(branch));
7027 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7031 static const struct got_error *
7032 show_histedit_progress(struct got_commit_object *commit,
7033 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7035 const struct got_error *err;
7036 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7038 err = got_object_id_str(&old_id_str, hle->commit_id);
7043 err = got_object_id_str(&new_id_str, new_id);
7048 old_id_str[12] = '\0';
7050 new_id_str[12] = '\0';
7053 logmsg = strdup(hle->logmsg);
7054 if (logmsg == NULL) {
7055 err = got_error_from_errno("strdup");
7058 trim_logmsg(logmsg, 42);
7060 err = get_short_logmsg(&logmsg, 42, commit);
7065 switch (hle->cmd->code) {
7066 case GOT_HISTEDIT_PICK:
7067 case GOT_HISTEDIT_EDIT:
7068 printf("%s -> %s: %s\n", old_id_str,
7069 new_id_str ? new_id_str : "no-op change", logmsg);
7071 case GOT_HISTEDIT_DROP:
7072 case GOT_HISTEDIT_FOLD:
7073 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7085 static const struct got_error *
7086 histedit_commit(struct got_pathlist_head *merged_paths,
7087 struct got_worktree *worktree, struct got_fileindex *fileindex,
7088 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7089 struct got_repository *repo)
7091 const struct got_error *err;
7092 struct got_commit_object *commit;
7093 struct got_object_id *new_commit_id;
7095 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7096 && hle->logmsg == NULL) {
7097 err = histedit_edit_logmsg(hle, repo);
7102 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7106 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7107 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7110 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7112 err = show_histedit_progress(commit, hle, NULL);
7114 err = show_histedit_progress(commit, hle, new_commit_id);
7115 free(new_commit_id);
7118 got_object_commit_close(commit);
7122 static const struct got_error *
7123 histedit_skip_commit(struct got_histedit_list_entry *hle,
7124 struct got_worktree *worktree, struct got_repository *repo)
7126 const struct got_error *error;
7127 struct got_commit_object *commit;
7129 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7134 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7138 error = show_histedit_progress(commit, hle, NULL);
7139 got_object_commit_close(commit);
7143 static const struct got_error *
7144 check_local_changes(void *arg, unsigned char status,
7145 unsigned char staged_status, const char *path,
7146 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7147 struct got_object_id *commit_id, int dirfd, const char *de_name)
7149 int *have_local_changes = arg;
7152 case GOT_STATUS_ADD:
7153 case GOT_STATUS_DELETE:
7154 case GOT_STATUS_MODIFY:
7155 case GOT_STATUS_CONFLICT:
7156 *have_local_changes = 1;
7157 return got_error(GOT_ERR_CANCELLED);
7162 switch (staged_status) {
7163 case GOT_STATUS_ADD:
7164 case GOT_STATUS_DELETE:
7165 case GOT_STATUS_MODIFY:
7166 *have_local_changes = 1;
7167 return got_error(GOT_ERR_CANCELLED);
7175 static const struct got_error *
7176 cmd_histedit(int argc, char *argv[])
7178 const struct got_error *error = NULL;
7179 struct got_worktree *worktree = NULL;
7180 struct got_fileindex *fileindex = NULL;
7181 struct got_repository *repo = NULL;
7183 struct got_reference *branch = NULL;
7184 struct got_reference *tmp_branch = NULL;
7185 struct got_object_id *resume_commit_id = NULL;
7186 struct got_object_id *base_commit_id = NULL;
7187 struct got_object_id *head_commit_id = NULL;
7188 struct got_commit_object *commit = NULL;
7189 int ch, rebase_in_progress = 0, did_something;
7190 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7191 int edit_logmsg_only = 0;
7192 const char *edit_script_path = NULL;
7193 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7194 struct got_object_id_queue commits;
7195 struct got_pathlist_head merged_paths;
7196 const struct got_object_id_queue *parent_ids;
7197 struct got_object_qid *pid;
7198 struct got_histedit_list histedit_cmds;
7199 struct got_histedit_list_entry *hle;
7201 SIMPLEQ_INIT(&commits);
7202 TAILQ_INIT(&histedit_cmds);
7203 TAILQ_INIT(&merged_paths);
7205 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7214 edit_script_path = optarg;
7217 edit_logmsg_only = 1;
7229 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7230 "unveil", NULL) == -1)
7233 if (abort_edit && continue_edit)
7234 errx(1, "histedit's -a and -c options are mutually exclusive");
7235 if (edit_script_path && edit_logmsg_only)
7236 errx(1, "histedit's -F and -m options are mutually exclusive");
7237 if (abort_edit && edit_logmsg_only)
7238 errx(1, "histedit's -a and -m options are mutually exclusive");
7239 if (continue_edit && edit_logmsg_only)
7240 errx(1, "histedit's -c and -m options are mutually exclusive");
7245 * This command cannot apply unveil(2) in all cases because the
7246 * user may choose to run an editor to edit the histedit script
7247 * and to edit individual commit log messages.
7248 * unveil(2) traverses exec(2); if an editor is used we have to
7249 * apply unveil after edit script and log messages have been written.
7250 * XXX TODO: Make use of unveil(2) where possible.
7253 cwd = getcwd(NULL, 0);
7255 error = got_error_from_errno("getcwd");
7258 error = got_worktree_open(&worktree, cwd);
7262 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7267 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7270 if (rebase_in_progress) {
7271 error = got_error(GOT_ERR_REBASING);
7275 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7279 if (edit_in_progress && edit_logmsg_only) {
7280 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7281 "histedit operation is in progress in this "
7282 "work tree and must be continued or aborted "
7283 "before the -m option can be used");
7287 if (edit_in_progress && abort_edit) {
7288 error = got_worktree_histedit_continue(&resume_commit_id,
7289 &tmp_branch, &branch, &base_commit_id, &fileindex,
7293 printf("Switching work tree to %s\n",
7294 got_ref_get_symref_target(branch));
7295 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7296 branch, base_commit_id, update_progress, &did_something);
7299 printf("Histedit of %s aborted\n",
7300 got_ref_get_symref_target(branch));
7301 goto done; /* nothing else to do */
7302 } else if (abort_edit) {
7303 error = got_error(GOT_ERR_NOT_HISTEDIT);
7307 if (continue_edit) {
7310 if (!edit_in_progress) {
7311 error = got_error(GOT_ERR_NOT_HISTEDIT);
7315 error = got_worktree_get_histedit_script_path(&path, worktree);
7319 error = histedit_load_list(&histedit_cmds, path, repo);
7324 error = got_worktree_histedit_continue(&resume_commit_id,
7325 &tmp_branch, &branch, &base_commit_id, &fileindex,
7330 error = got_ref_resolve(&head_commit_id, repo, branch);
7334 error = got_object_open_as_commit(&commit, repo,
7338 parent_ids = got_object_commit_get_parent_ids(commit);
7339 pid = SIMPLEQ_FIRST(parent_ids);
7341 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7344 error = collect_commits(&commits, head_commit_id, pid->id,
7345 base_commit_id, got_worktree_get_path_prefix(worktree),
7346 GOT_ERR_HISTEDIT_PATH, repo);
7347 got_object_commit_close(commit);
7352 if (edit_in_progress) {
7353 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7357 error = got_ref_open(&branch, repo,
7358 got_worktree_get_head_ref_name(worktree), 0);
7362 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7363 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7364 "will not edit commit history of a branch outside "
7365 "the \"refs/heads/\" reference namespace");
7369 error = got_ref_resolve(&head_commit_id, repo, branch);
7370 got_ref_close(branch);
7375 error = got_object_open_as_commit(&commit, repo,
7379 parent_ids = got_object_commit_get_parent_ids(commit);
7380 pid = SIMPLEQ_FIRST(parent_ids);
7382 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7385 error = collect_commits(&commits, head_commit_id, pid->id,
7386 got_worktree_get_base_commit_id(worktree),
7387 got_worktree_get_path_prefix(worktree),
7388 GOT_ERR_HISTEDIT_PATH, repo);
7389 got_object_commit_close(commit);
7394 if (SIMPLEQ_EMPTY(&commits)) {
7395 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7399 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7400 &base_commit_id, &fileindex, worktree, repo);
7404 if (edit_script_path) {
7405 error = histedit_load_list(&histedit_cmds,
7406 edit_script_path, repo);
7408 got_worktree_histedit_abort(worktree, fileindex,
7409 repo, branch, base_commit_id,
7410 update_progress, &did_something);
7414 const char *branch_name;
7415 branch_name = got_ref_get_symref_target(branch);
7416 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7418 error = histedit_edit_script(&histedit_cmds, &commits,
7419 branch_name, edit_logmsg_only, repo);
7421 got_worktree_histedit_abort(worktree, fileindex,
7422 repo, branch, base_commit_id,
7423 update_progress, &did_something);
7429 error = histedit_save_list(&histedit_cmds, worktree,
7432 got_worktree_histedit_abort(worktree, fileindex,
7433 repo, branch, base_commit_id,
7434 update_progress, &did_something);
7440 error = histedit_check_script(&histedit_cmds, &commits, repo);
7444 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7445 if (resume_commit_id) {
7446 if (got_object_id_cmp(hle->commit_id,
7447 resume_commit_id) != 0)
7450 resume_commit_id = NULL;
7451 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7452 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7453 error = histedit_skip_commit(hle, worktree,
7458 struct got_pathlist_head paths;
7459 int have_changes = 0;
7462 error = got_pathlist_append(&paths, "", NULL);
7465 error = got_worktree_status(worktree, &paths,
7466 repo, check_local_changes, &have_changes,
7467 check_cancelled, NULL);
7468 got_pathlist_free(&paths);
7470 if (error->code != GOT_ERR_CANCELLED)
7472 if (sigint_received || sigpipe_received)
7476 error = histedit_commit(NULL, worktree,
7477 fileindex, tmp_branch, hle, repo);
7481 error = got_object_open_as_commit(
7482 &commit, repo, hle->commit_id);
7485 error = show_histedit_progress(commit,
7487 got_object_commit_close(commit);
7496 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7497 error = histedit_skip_commit(hle, worktree, repo);
7503 error = got_object_open_as_commit(&commit, repo,
7507 parent_ids = got_object_commit_get_parent_ids(commit);
7508 pid = SIMPLEQ_FIRST(parent_ids);
7510 error = got_worktree_histedit_merge_files(&merged_paths,
7511 worktree, fileindex, pid->id, hle->commit_id, repo,
7512 rebase_progress, &rebase_status, check_cancelled, NULL);
7515 got_object_commit_close(commit);
7518 if (rebase_status == GOT_STATUS_CONFLICT) {
7519 error = show_rebase_merge_conflict(hle->commit_id,
7523 got_worktree_rebase_pathlist_free(&merged_paths);
7527 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7529 error = got_object_id_str(&id_str, hle->commit_id);
7532 printf("Stopping histedit for amending commit %s\n",
7535 got_worktree_rebase_pathlist_free(&merged_paths);
7536 error = got_worktree_histedit_postpone(worktree,
7541 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7542 error = histedit_skip_commit(hle, worktree, repo);
7548 error = histedit_commit(&merged_paths, worktree, fileindex,
7549 tmp_branch, hle, repo);
7550 got_worktree_rebase_pathlist_free(&merged_paths);
7555 if (rebase_status == GOT_STATUS_CONFLICT) {
7556 error = got_worktree_histedit_postpone(worktree, fileindex);
7559 error = got_error_msg(GOT_ERR_CONFLICTS,
7560 "conflicts must be resolved before histedit can continue");
7562 error = histedit_complete(worktree, fileindex, tmp_branch,
7565 got_object_id_queue_free(&commits);
7566 histedit_free_list(&histedit_cmds);
7567 free(head_commit_id);
7568 free(base_commit_id);
7569 free(resume_commit_id);
7571 got_object_commit_close(commit);
7573 got_ref_close(branch);
7575 got_ref_close(tmp_branch);
7577 got_worktree_close(worktree);
7579 got_repo_close(repo);
7584 usage_integrate(void)
7586 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7590 static const struct got_error *
7591 cmd_integrate(int argc, char *argv[])
7593 const struct got_error *error = NULL;
7594 struct got_repository *repo = NULL;
7595 struct got_worktree *worktree = NULL;
7596 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7597 const char *branch_arg = NULL;
7598 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7599 struct got_fileindex *fileindex = NULL;
7600 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7601 int ch, did_something = 0;
7603 while ((ch = getopt(argc, argv, "")) != -1) {
7616 branch_arg = argv[0];
7618 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7619 "unveil", NULL) == -1)
7622 cwd = getcwd(NULL, 0);
7624 error = got_error_from_errno("getcwd");
7628 error = got_worktree_open(&worktree, cwd);
7632 error = check_rebase_or_histedit_in_progress(worktree);
7636 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7641 error = apply_unveil(got_repo_get_path(repo), 0,
7642 got_worktree_get_root_path(worktree));
7646 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7647 error = got_error_from_errno("asprintf");
7651 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7652 &base_branch_ref, worktree, refname, repo);
7656 refname = strdup(got_ref_get_name(branch_ref));
7657 if (refname == NULL) {
7658 error = got_error_from_errno("strdup");
7659 got_worktree_integrate_abort(worktree, fileindex, repo,
7660 branch_ref, base_branch_ref);
7663 base_refname = strdup(got_ref_get_name(base_branch_ref));
7664 if (base_refname == NULL) {
7665 error = got_error_from_errno("strdup");
7666 got_worktree_integrate_abort(worktree, fileindex, repo,
7667 branch_ref, base_branch_ref);
7671 error = got_ref_resolve(&commit_id, repo, branch_ref);
7675 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7679 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7680 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7681 "specified branch has already been integrated");
7682 got_worktree_integrate_abort(worktree, fileindex, repo,
7683 branch_ref, base_branch_ref);
7687 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7689 if (error->code == GOT_ERR_ANCESTRY)
7690 error = got_error(GOT_ERR_REBASE_REQUIRED);
7691 got_worktree_integrate_abort(worktree, fileindex, repo,
7692 branch_ref, base_branch_ref);
7696 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7697 branch_ref, base_branch_ref, update_progress, &did_something,
7698 check_cancelled, NULL);
7702 printf("Integrated %s into %s\n", refname, base_refname);
7705 got_repo_close(repo);
7707 got_worktree_close(worktree);
7709 free(base_commit_id);
7719 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7720 "[file-path ...]\n",
7725 static const struct got_error *
7726 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7727 const char *path, struct got_object_id *blob_id,
7728 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7729 int dirfd, const char *de_name)
7731 const struct got_error *err = NULL;
7732 char *id_str = NULL;
7734 if (staged_status != GOT_STATUS_ADD &&
7735 staged_status != GOT_STATUS_MODIFY &&
7736 staged_status != GOT_STATUS_DELETE)
7739 if (staged_status == GOT_STATUS_ADD ||
7740 staged_status == GOT_STATUS_MODIFY)
7741 err = got_object_id_str(&id_str, staged_blob_id);
7743 err = got_object_id_str(&id_str, blob_id);
7747 printf("%s %c %s\n", id_str, staged_status, path);
7752 static const struct got_error *
7753 cmd_stage(int argc, char *argv[])
7755 const struct got_error *error = NULL;
7756 struct got_repository *repo = NULL;
7757 struct got_worktree *worktree = NULL;
7759 struct got_pathlist_head paths;
7760 struct got_pathlist_entry *pe;
7761 int ch, list_stage = 0, pflag = 0;
7762 FILE *patch_script_file = NULL;
7763 const char *patch_script_path = NULL;
7764 struct choose_patch_arg cpa;
7768 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7777 patch_script_path = optarg;
7789 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7790 "unveil", NULL) == -1)
7793 if (list_stage && (pflag || patch_script_path))
7794 errx(1, "-l option cannot be used with other options");
7795 if (patch_script_path && !pflag)
7796 errx(1, "-F option can only be used together with -p option");
7798 cwd = getcwd(NULL, 0);
7800 error = got_error_from_errno("getcwd");
7804 error = got_worktree_open(&worktree, cwd);
7808 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7813 if (patch_script_path) {
7814 patch_script_file = fopen(patch_script_path, "r");
7815 if (patch_script_file == NULL) {
7816 error = got_error_from_errno2("fopen",
7821 error = apply_unveil(got_repo_get_path(repo), 0,
7822 got_worktree_get_root_path(worktree));
7826 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7831 error = got_worktree_status(worktree, &paths, repo,
7832 print_stage, NULL, check_cancelled, NULL);
7834 cpa.patch_script_file = patch_script_file;
7835 cpa.action = "stage";
7836 error = got_worktree_stage(worktree, &paths,
7837 pflag ? NULL : print_status, NULL,
7838 pflag ? choose_patch : NULL, &cpa, repo);
7841 if (patch_script_file && fclose(patch_script_file) == EOF &&
7843 error = got_error_from_errno2("fclose", patch_script_path);
7845 got_repo_close(repo);
7847 got_worktree_close(worktree);
7848 TAILQ_FOREACH(pe, &paths, entry)
7849 free((char *)pe->path);
7850 got_pathlist_free(&paths);
7858 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7859 "[file-path ...]\n",
7865 static const struct got_error *
7866 cmd_unstage(int argc, char *argv[])
7868 const struct got_error *error = NULL;
7869 struct got_repository *repo = NULL;
7870 struct got_worktree *worktree = NULL;
7872 struct got_pathlist_head paths;
7873 struct got_pathlist_entry *pe;
7874 int ch, did_something = 0, pflag = 0;
7875 FILE *patch_script_file = NULL;
7876 const char *patch_script_path = NULL;
7877 struct choose_patch_arg cpa;
7881 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7887 patch_script_path = optarg;
7899 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7900 "unveil", NULL) == -1)
7903 if (patch_script_path && !pflag)
7904 errx(1, "-F option can only be used together with -p option");
7906 cwd = getcwd(NULL, 0);
7908 error = got_error_from_errno("getcwd");
7912 error = got_worktree_open(&worktree, cwd);
7916 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7921 if (patch_script_path) {
7922 patch_script_file = fopen(patch_script_path, "r");
7923 if (patch_script_file == NULL) {
7924 error = got_error_from_errno2("fopen",
7930 error = apply_unveil(got_repo_get_path(repo), 0,
7931 got_worktree_get_root_path(worktree));
7935 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7939 cpa.patch_script_file = patch_script_file;
7940 cpa.action = "unstage";
7941 error = got_worktree_unstage(worktree, &paths, update_progress,
7942 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7944 if (patch_script_file && fclose(patch_script_file) == EOF &&
7946 error = got_error_from_errno2("fclose", patch_script_path);
7948 got_repo_close(repo);
7950 got_worktree_close(worktree);
7951 TAILQ_FOREACH(pe, &paths, entry)
7952 free((char *)pe->path);
7953 got_pathlist_free(&paths);
7961 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7962 "arg1 [arg2 ...]\n", getprogname());
7966 static const struct got_error *
7967 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7969 const struct got_error *err;
7970 struct got_blob_object *blob;
7972 err = got_object_open_as_blob(&blob, repo, id, 8192);
7976 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7977 got_object_blob_close(blob);
7981 static const struct got_error *
7982 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7984 const struct got_error *err;
7985 struct got_tree_object *tree;
7988 err = got_object_open_as_tree(&tree, repo, id);
7992 nentries = got_object_tree_get_nentries(tree);
7993 for (i = 0; i < nentries; i++) {
7994 struct got_tree_entry *te;
7996 if (sigint_received || sigpipe_received)
7998 te = got_object_tree_get_entry(tree, i);
7999 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8002 fprintf(outfile, "%s %.7o %s\n", id_str,
8003 got_tree_entry_get_mode(te),
8004 got_tree_entry_get_name(te));
8008 got_object_tree_close(tree);
8012 static const struct got_error *
8013 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8015 const struct got_error *err;
8016 struct got_commit_object *commit;
8017 const struct got_object_id_queue *parent_ids;
8018 struct got_object_qid *pid;
8019 char *id_str = NULL;
8020 const char *logmsg = NULL;
8022 err = got_object_open_as_commit(&commit, repo, id);
8026 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8030 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8031 parent_ids = got_object_commit_get_parent_ids(commit);
8032 fprintf(outfile, "numparents %d\n",
8033 got_object_commit_get_nparents(commit));
8034 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8036 err = got_object_id_str(&pid_str, pid->id);
8039 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8042 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8043 got_object_commit_get_author(commit),
8044 got_object_commit_get_author_time(commit));
8046 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8047 got_object_commit_get_author(commit),
8048 got_object_commit_get_committer_time(commit));
8050 logmsg = got_object_commit_get_logmsg_raw(commit);
8051 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8052 fprintf(outfile, "%s", logmsg);
8055 got_object_commit_close(commit);
8059 static const struct got_error *
8060 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8062 const struct got_error *err;
8063 struct got_tag_object *tag;
8064 char *id_str = NULL;
8065 const char *tagmsg = NULL;
8067 err = got_object_open_as_tag(&tag, repo, id);
8071 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8075 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8077 switch (got_object_tag_get_object_type(tag)) {
8078 case GOT_OBJ_TYPE_BLOB:
8079 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8080 GOT_OBJ_LABEL_BLOB);
8082 case GOT_OBJ_TYPE_TREE:
8083 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8084 GOT_OBJ_LABEL_TREE);
8086 case GOT_OBJ_TYPE_COMMIT:
8087 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8088 GOT_OBJ_LABEL_COMMIT);
8090 case GOT_OBJ_TYPE_TAG:
8091 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8098 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8099 got_object_tag_get_name(tag));
8101 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8102 got_object_tag_get_tagger(tag),
8103 got_object_tag_get_tagger_time(tag));
8105 tagmsg = got_object_tag_get_message(tag);
8106 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8107 fprintf(outfile, "%s", tagmsg);
8110 got_object_tag_close(tag);
8114 static const struct got_error *
8115 cmd_cat(int argc, char *argv[])
8117 const struct got_error *error;
8118 struct got_repository *repo = NULL;
8119 struct got_worktree *worktree = NULL;
8120 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8121 const char *commit_id_str = NULL;
8122 struct got_object_id *id = NULL, *commit_id = NULL;
8123 int ch, obj_type, i, force_path = 0;
8126 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8131 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8134 commit_id_str = optarg;
8137 repo_path = realpath(optarg, NULL);
8138 if (repo_path == NULL)
8139 return got_error_from_errno2("realpath",
8141 got_path_strip_trailing_slashes(repo_path);
8155 cwd = getcwd(NULL, 0);
8157 error = got_error_from_errno("getcwd");
8160 error = got_worktree_open(&worktree, cwd);
8161 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8164 if (repo_path == NULL) {
8166 got_worktree_get_repo_path(worktree));
8167 if (repo_path == NULL) {
8168 error = got_error_from_errno("strdup");
8174 if (repo_path == NULL) {
8175 repo_path = getcwd(NULL, 0);
8176 if (repo_path == NULL)
8177 return got_error_from_errno("getcwd");
8180 error = got_repo_open(&repo, repo_path, NULL);
8185 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8189 if (commit_id_str == NULL)
8190 commit_id_str = GOT_REF_HEAD;
8191 error = got_repo_match_object_id(&commit_id, NULL,
8192 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8196 for (i = 0; i < argc; i++) {
8198 error = got_object_id_by_path(&id, repo, commit_id,
8203 error = got_repo_match_object_id(&id, &label, argv[i],
8204 GOT_OBJ_TYPE_ANY, 0, repo);
8206 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8207 error->code != GOT_ERR_NOT_REF)
8209 error = got_object_id_by_path(&id, repo,
8210 commit_id, argv[i]);
8216 error = got_object_get_type(&obj_type, repo, id);
8221 case GOT_OBJ_TYPE_BLOB:
8222 error = cat_blob(id, repo, stdout);
8224 case GOT_OBJ_TYPE_TREE:
8225 error = cat_tree(id, repo, stdout);
8227 case GOT_OBJ_TYPE_COMMIT:
8228 error = cat_commit(id, repo, stdout);
8230 case GOT_OBJ_TYPE_TAG:
8231 error = cat_tag(id, repo, stdout);
8234 error = got_error(GOT_ERR_OBJ_TYPE);
8249 got_worktree_close(worktree);
8251 const struct got_error *repo_error;
8252 repo_error = got_repo_close(repo);