2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
873 if (print_size || print_indexed || print_resolved)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
878 printf("; indexing %d%%", p_indexed);
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
915 fetch_all_branches = 1;
918 mirror_references = 1;
923 else if (verbosity < 3)
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
958 if (strcmp(proto, "git") == 0) {
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
984 repo_path = default_destdir;
988 error = got_path_mkdir(repo_path);
992 error = got_repo_init(repo_path);
996 error = got_repo_open(&repo, repo_path, NULL);
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1030 error = got_object_id_str(&id_str, pack_hash);
1034 printf("\nFetched %s.pack\n", id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1047 error = got_ref_write(ref, repo);
1052 if (mirror_references)
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1064 error = got_ref_alloc(&ref, remote_refname, id);
1067 error = got_ref_write(ref, repo);
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 error = got_ref_open(&target_ref, repo, target, 0);
1084 if (error->code == GOT_ERR_NOT_REF)
1089 error = got_ref_alloc_symref(&head_symref,
1090 GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(head_symref));
1099 error = got_ref_write(head_symref, repo);
1103 /* Create a config file git-fetch(1) can understand. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1109 gitconfig_file = fopen(gitconfig_path, "a");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1114 if (mirror_references) {
1115 if (asprintf(&gitconfig,
1118 "\tfetch = +refs/*:refs/*\n"
1119 "\tmirror = true\n",
1120 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1121 error = got_error_from_errno("asprintf");
1124 } else if (fetch_all_branches) {
1125 if (asprintf(&gitconfig,
1128 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1129 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1130 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1131 error = got_error_from_errno("asprintf");
1135 const char *branchname;
1138 * If the server specified a default branch, use just that one.
1139 * Otherwise fall back to fetching all branches on next fetch.
1142 branchname = got_ref_get_symref_target(head_symref);
1143 if (strncmp(branchname, "refs/heads/", 11) == 0)
1146 branchname = "*"; /* fall back to all branches */
1147 if (asprintf(&gitconfig,
1150 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1151 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1152 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1153 branchname) == -1) {
1154 error = got_error_from_errno("asprintf");
1158 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1159 if (n != strlen(gitconfig)) {
1160 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1166 printf("Created %s repository '%s'\n",
1167 mirror_references ? "mirrored" : "cloned", repo_path);
1169 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1170 error = got_error_from_errno("close");
1171 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1172 error = got_error_from_errno("fclose");
1174 got_repo_close(repo);
1176 got_ref_close(head_symref);
1177 TAILQ_FOREACH(pe, &refs, entry) {
1178 free((void *)pe->path);
1181 got_pathlist_free(&refs);
1182 TAILQ_FOREACH(pe, &symrefs, entry) {
1183 free((void *)pe->path);
1186 got_pathlist_free(&symrefs);
1193 free(default_destdir);
1194 free(gitconfig_path);
1199 static const struct got_error *
1200 create_ref(const char *refname, struct got_object_id *id,
1201 const char *id_str, struct got_repository *repo)
1203 const struct got_error *err = NULL;
1204 struct got_reference *ref;
1206 printf("Creating %s: %s\n", refname, id_str);
1208 err = got_ref_alloc(&ref, refname, id);
1212 err = got_ref_write(ref, repo);
1217 static const struct got_error *
1218 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1219 struct got_repository *repo)
1221 const struct got_error *err = NULL;
1222 char *new_id_str = NULL;
1223 struct got_object_id *old_id = NULL;
1225 err = got_object_id_str(&new_id_str, new_id);
1229 if (got_ref_is_symbolic(ref)) {
1230 struct got_reference *new_ref;
1231 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1234 printf("Deleting symbolic reference %s -> %s\n",
1235 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1236 err = got_ref_delete(ref, repo);
1239 printf("Setting %s to %s\n", got_ref_get_name(ref),
1241 err = got_ref_write(new_ref, repo);
1245 err = got_ref_resolve(&old_id, repo, ref);
1248 if (got_object_id_cmp(old_id, new_id) != 0) {
1249 printf("Setting %s to %s\n",
1250 got_ref_get_name(ref), new_id_str);
1251 err = got_ref_change_ref(ref, new_id);
1254 err = got_ref_write(ref, repo);
1268 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1269 "[remote-repository-name]\n", getprogname());
1273 static const struct got_error *
1274 cmd_fetch(int argc, char *argv[])
1276 const struct got_error *error = NULL;
1277 char *cwd = NULL, *repo_path = NULL;
1278 const char *remote_name;
1279 char *proto = NULL, *host = NULL, *port = NULL;
1280 char *repo_name = NULL, *server_path = NULL;
1281 struct got_remote_repo *remotes, *remote = NULL;
1283 char *id_str = NULL;
1284 struct got_repository *repo = NULL;
1285 struct got_worktree *worktree = NULL;
1286 struct got_pathlist_head refs, symrefs;
1287 struct got_pathlist_entry *pe;
1288 struct got_object_id *pack_hash = NULL;
1289 int i, ch, fetchfd = -1;
1290 struct got_fetch_progress_arg fpa;
1291 int verbosity = 0, fetch_all_branches = 0;
1294 TAILQ_INIT(&symrefs);
1296 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1299 fetch_all_branches = 1;
1302 repo_path = realpath(optarg, NULL);
1303 if (repo_path == NULL)
1304 return got_error_from_errno2("realpath",
1306 got_path_strip_trailing_slashes(repo_path);
1311 else if (verbosity < 3)
1326 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1328 remote_name = argv[0];
1332 cwd = getcwd(NULL, 0);
1334 error = got_error_from_errno("getcwd");
1338 if (repo_path == NULL) {
1339 error = got_worktree_open(&worktree, cwd);
1340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1346 strdup(got_worktree_get_repo_path(worktree));
1347 if (repo_path == NULL)
1348 error = got_error_from_errno("strdup");
1352 repo_path = strdup(cwd);
1353 if (repo_path == NULL) {
1354 error = got_error_from_errno("strdup");
1360 error = got_repo_open(&repo, repo_path, NULL);
1364 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1365 for (i = 0; i < nremotes; i++) {
1366 remote = &remotes[i];
1367 if (strcmp(remote->name, remote_name) == 0)
1370 if (i == nremotes) {
1371 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1375 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1376 &repo_name, remote->url);
1380 if (strcmp(proto, "git") == 0) {
1382 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1383 "sendfd dns inet unveil", NULL) == -1)
1386 } else if (strcmp(proto, "git+ssh") == 0 ||
1387 strcmp(proto, "ssh") == 0) {
1389 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1390 "sendfd unveil", NULL) == -1)
1393 } else if (strcmp(proto, "http") == 0 ||
1394 strcmp(proto, "git+http") == 0) {
1395 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1398 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1402 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1403 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1404 error = got_error_from_errno2("unveil",
1405 GOT_FETCH_PATH_SSH);
1409 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1413 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1419 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1420 port ? ":" : "", port ? port : "");
1422 fpa.last_scaled_size[0] = '\0';
1423 fpa.last_p_indexed = -1;
1424 fpa.last_p_resolved = -1;
1425 fpa.verbosity = verbosity;
1426 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1427 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1428 fetch_progress, &fpa);
1432 if (pack_hash == NULL) {
1434 printf("Already up-to-date\n");
1438 if (verbosity >= 0) {
1439 error = got_object_id_str(&id_str, pack_hash);
1442 printf("\nFetched %s.pack\n", id_str);
1447 /* Update references provided with the pack file. */
1448 TAILQ_FOREACH(pe, &refs, entry) {
1449 const char *refname = pe->path;
1450 struct got_object_id *id = pe->data;
1451 struct got_reference *ref;
1452 char *remote_refname;
1454 if (remote->mirror_references) {
1455 error = got_ref_alloc(&ref, refname, id);
1458 error = got_ref_write(ref, repo);
1465 error = got_object_id_str(&id_str, id);
1469 if (strncmp("refs/tags/", refname, 10) == 0) {
1470 error = got_ref_open(&ref, repo, refname, 0);
1472 if (error->code != GOT_ERR_NOT_REF)
1474 error = create_ref(refname, id, id_str, repo);
1478 error = update_ref(ref, id, repo);
1483 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1484 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1485 remote_name, refname + 11) == -1) {
1486 error = got_error_from_errno("asprintf");
1490 error = got_ref_open(&ref, repo, remote_refname, 0);
1492 if (error->code != GOT_ERR_NOT_REF)
1494 error = create_ref(remote_refname, id, id_str,
1499 error = update_ref(ref, id, repo);
1509 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1510 error = got_error_from_errno("close");
1512 got_repo_close(repo);
1514 got_worktree_close(worktree);
1515 TAILQ_FOREACH(pe, &refs, entry) {
1516 free((void *)pe->path);
1519 got_pathlist_free(&refs);
1520 TAILQ_FOREACH(pe, &symrefs, entry) {
1521 free((void *)pe->path);
1524 got_pathlist_free(&symrefs);
1539 usage_checkout(void)
1541 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1542 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1547 show_worktree_base_ref_warning(void)
1549 fprintf(stderr, "%s: warning: could not create a reference "
1550 "to the work tree's base commit; the commit could be "
1551 "garbage-collected by Git; making the repository "
1552 "writable and running 'got update' will prevent this\n",
1556 struct got_checkout_progress_arg {
1557 const char *worktree_path;
1558 int had_base_commit_ref_error;
1561 static const struct got_error *
1562 checkout_progress(void *arg, unsigned char status, const char *path)
1564 struct got_checkout_progress_arg *a = arg;
1566 /* Base commit bump happens silently. */
1567 if (status == GOT_STATUS_BUMP_BASE)
1570 if (status == GOT_STATUS_BASE_REF_ERR) {
1571 a->had_base_commit_ref_error = 1;
1575 while (path[0] == '/')
1578 printf("%c %s/%s\n", status, a->worktree_path, path);
1582 static const struct got_error *
1583 check_cancelled(void *arg)
1585 if (sigint_received || sigpipe_received)
1586 return got_error(GOT_ERR_CANCELLED);
1590 static const struct got_error *
1591 check_linear_ancestry(struct got_object_id *commit_id,
1592 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1593 struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_object_id *yca_id;
1598 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1599 commit_id, base_commit_id, repo, check_cancelled, NULL);
1604 return got_error(GOT_ERR_ANCESTRY);
1607 * Require a straight line of history between the target commit
1608 * and the work tree's base commit.
1610 * Non-linear situations such as this require a rebase:
1612 * (commit) D F (base_commit)
1620 * 'got update' only handles linear cases:
1621 * Update forwards in time: A (base/yca) - B - C - D (commit)
1622 * Update backwards in time: D (base) - C - B - A (commit/yca)
1624 if (allow_forwards_in_time_only) {
1625 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1626 return got_error(GOT_ERR_ANCESTRY);
1627 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1628 got_object_id_cmp(base_commit_id, yca_id) != 0)
1629 return got_error(GOT_ERR_ANCESTRY);
1635 static const struct got_error *
1636 check_same_branch(struct got_object_id *commit_id,
1637 struct got_reference *head_ref, struct got_object_id *yca_id,
1638 struct got_repository *repo)
1640 const struct got_error *err = NULL;
1641 struct got_commit_graph *graph = NULL;
1642 struct got_object_id *head_commit_id = NULL;
1643 int is_same_branch = 0;
1645 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1649 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1653 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1658 err = got_commit_graph_open(&graph, "/", 1);
1662 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1663 check_cancelled, NULL);
1668 struct got_object_id *id;
1669 err = got_commit_graph_iter_next(&id, graph, repo,
1670 check_cancelled, NULL);
1672 if (err->code == GOT_ERR_ITER_COMPLETED)
1678 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1680 if (got_object_id_cmp(id, commit_id) == 0) {
1688 got_commit_graph_close(graph);
1689 free(head_commit_id);
1690 if (!err && !is_same_branch)
1691 err = got_error(GOT_ERR_ANCESTRY);
1695 static const struct got_error *
1696 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1698 static char msg[512];
1699 const char *branch_name;
1701 if (got_ref_is_symbolic(ref))
1702 branch_name = got_ref_get_symref_target(ref);
1704 branch_name = got_ref_get_name(ref);
1706 if (strncmp("refs/heads/", branch_name, 11) == 0)
1709 snprintf(msg, sizeof(msg),
1710 "target commit is not contained in branch '%s'; "
1711 "the branch to use must be specified with -b; "
1712 "if necessary a new branch can be created for "
1713 "this commit with 'got branch -c %s BRANCH_NAME'",
1714 branch_name, commit_id_str);
1716 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1719 static const struct got_error *
1720 cmd_checkout(int argc, char *argv[])
1722 const struct got_error *error = NULL;
1723 struct got_repository *repo = NULL;
1724 struct got_reference *head_ref = NULL;
1725 struct got_worktree *worktree = NULL;
1726 char *repo_path = NULL;
1727 char *worktree_path = NULL;
1728 const char *path_prefix = "";
1729 const char *branch_name = GOT_REF_HEAD;
1730 char *commit_id_str = NULL;
1731 int ch, same_path_prefix, allow_nonempty = 0;
1732 struct got_pathlist_head paths;
1733 struct got_checkout_progress_arg cpa;
1737 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1740 branch_name = optarg;
1743 commit_id_str = strdup(optarg);
1744 if (commit_id_str == NULL)
1745 return got_error_from_errno("strdup");
1751 path_prefix = optarg;
1763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1764 "unveil", NULL) == -1)
1768 char *cwd, *base, *dotgit;
1769 repo_path = realpath(argv[0], NULL);
1770 if (repo_path == NULL)
1771 return got_error_from_errno2("realpath", argv[0]);
1772 cwd = getcwd(NULL, 0);
1774 error = got_error_from_errno("getcwd");
1777 if (path_prefix[0]) {
1778 base = basename(path_prefix);
1780 error = got_error_from_errno2("basename",
1785 base = basename(repo_path);
1787 error = got_error_from_errno2("basename",
1792 dotgit = strstr(base, ".git");
1795 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1796 error = got_error_from_errno("asprintf");
1801 } else if (argc == 2) {
1802 repo_path = realpath(argv[0], NULL);
1803 if (repo_path == NULL) {
1804 error = got_error_from_errno2("realpath", argv[0]);
1807 worktree_path = realpath(argv[1], NULL);
1808 if (worktree_path == NULL) {
1809 if (errno != ENOENT) {
1810 error = got_error_from_errno2("realpath",
1814 worktree_path = strdup(argv[1]);
1815 if (worktree_path == NULL) {
1816 error = got_error_from_errno("strdup");
1823 got_path_strip_trailing_slashes(repo_path);
1824 got_path_strip_trailing_slashes(worktree_path);
1826 error = got_repo_open(&repo, repo_path, NULL);
1830 /* Pre-create work tree path for unveil(2) */
1831 error = got_path_mkdir(worktree_path);
1833 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1834 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1836 if (!allow_nonempty &&
1837 !got_path_dir_is_empty(worktree_path)) {
1838 error = got_error_path(worktree_path,
1839 GOT_ERR_DIR_NOT_EMPTY);
1844 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1848 error = got_ref_open(&head_ref, repo, branch_name, 0);
1852 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1853 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1856 error = got_worktree_open(&worktree, worktree_path);
1860 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1864 if (!same_path_prefix) {
1865 error = got_error(GOT_ERR_PATH_PREFIX);
1869 if (commit_id_str) {
1870 struct got_object_id *commit_id;
1871 error = got_repo_match_object_id(&commit_id, NULL,
1872 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1875 error = check_linear_ancestry(commit_id,
1876 got_worktree_get_base_commit_id(worktree), 0, repo);
1877 if (error != NULL) {
1879 if (error->code == GOT_ERR_ANCESTRY) {
1880 error = checkout_ancestry_error(
1881 head_ref, commit_id_str);
1885 error = check_same_branch(commit_id, head_ref, NULL, repo);
1887 if (error->code == GOT_ERR_ANCESTRY) {
1888 error = checkout_ancestry_error(
1889 head_ref, commit_id_str);
1893 error = got_worktree_set_base_commit_id(worktree, repo,
1900 error = got_pathlist_append(&paths, "", NULL);
1903 cpa.worktree_path = worktree_path;
1904 cpa.had_base_commit_ref_error = 0;
1905 error = got_worktree_checkout_files(worktree, &paths, repo,
1906 checkout_progress, &cpa, check_cancelled, NULL);
1910 printf("Now shut up and hack\n");
1911 if (cpa.had_base_commit_ref_error)
1912 show_worktree_base_ref_warning();
1914 got_pathlist_free(&paths);
1915 free(commit_id_str);
1917 free(worktree_path);
1924 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1929 static const struct got_error *
1930 update_progress(void *arg, unsigned char status, const char *path)
1932 int *did_something = arg;
1934 if (status == GOT_STATUS_EXISTS ||
1935 status == GOT_STATUS_BASE_REF_ERR)
1940 /* Base commit bump happens silently. */
1941 if (status == GOT_STATUS_BUMP_BASE)
1944 while (path[0] == '/')
1946 printf("%c %s\n", status, path);
1950 static const struct got_error *
1951 switch_head_ref(struct got_reference *head_ref,
1952 struct got_object_id *commit_id, struct got_worktree *worktree,
1953 struct got_repository *repo)
1955 const struct got_error *err = NULL;
1957 int ref_has_moved = 0;
1959 /* Trivial case: switching between two different references. */
1960 if (strcmp(got_ref_get_name(head_ref),
1961 got_worktree_get_head_ref_name(worktree)) != 0) {
1962 printf("Switching work tree from %s to %s\n",
1963 got_worktree_get_head_ref_name(worktree),
1964 got_ref_get_name(head_ref));
1965 return got_worktree_set_head_ref(worktree, head_ref);
1968 err = check_linear_ancestry(commit_id,
1969 got_worktree_get_base_commit_id(worktree), 0, repo);
1971 if (err->code != GOT_ERR_ANCESTRY)
1978 /* Switching to a rebased branch with the same reference name. */
1979 err = got_object_id_str(&base_id_str,
1980 got_worktree_get_base_commit_id(worktree));
1983 printf("Reference %s now points at a different branch\n",
1984 got_worktree_get_head_ref_name(worktree));
1985 printf("Switching work tree from %s to %s\n", base_id_str,
1986 got_worktree_get_head_ref_name(worktree));
1990 static const struct got_error *
1991 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1993 const struct got_error *err;
1996 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2000 return got_error(GOT_ERR_REBASING);
2002 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2006 return got_error(GOT_ERR_HISTEDIT_BUSY);
2011 static const struct got_error *
2012 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2013 char *argv[], struct got_worktree *worktree)
2015 const struct got_error *err = NULL;
2022 return got_error_from_errno("strdup");
2023 return got_pathlist_append(paths, path, NULL);
2026 for (i = 0; i < argc; i++) {
2027 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2030 err = got_pathlist_append(paths, path, NULL);
2040 static const struct got_error *
2041 cmd_update(int argc, char *argv[])
2043 const struct got_error *error = NULL;
2044 struct got_repository *repo = NULL;
2045 struct got_worktree *worktree = NULL;
2046 char *worktree_path = NULL;
2047 struct got_object_id *commit_id = NULL;
2048 char *commit_id_str = NULL;
2049 const char *branch_name = NULL;
2050 struct got_reference *head_ref = NULL;
2051 struct got_pathlist_head paths;
2052 struct got_pathlist_entry *pe;
2053 int ch, did_something = 0;
2057 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2060 branch_name = optarg;
2063 commit_id_str = strdup(optarg);
2064 if (commit_id_str == NULL)
2065 return got_error_from_errno("strdup");
2077 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2078 "unveil", NULL) == -1)
2081 worktree_path = getcwd(NULL, 0);
2082 if (worktree_path == NULL) {
2083 error = got_error_from_errno("getcwd");
2086 error = got_worktree_open(&worktree, worktree_path);
2090 error = check_rebase_or_histedit_in_progress(worktree);
2094 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2099 error = apply_unveil(got_repo_get_path(repo), 0,
2100 got_worktree_get_root_path(worktree));
2104 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2108 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2109 got_worktree_get_head_ref_name(worktree), 0);
2112 if (commit_id_str == NULL) {
2113 error = got_ref_resolve(&commit_id, repo, head_ref);
2116 error = got_object_id_str(&commit_id_str, commit_id);
2120 error = got_repo_match_object_id(&commit_id, NULL,
2121 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2122 free(commit_id_str);
2123 commit_id_str = NULL;
2126 error = got_object_id_str(&commit_id_str, commit_id);
2132 struct got_object_id *head_commit_id;
2133 TAILQ_FOREACH(pe, &paths, entry) {
2134 if (pe->path_len == 0)
2136 error = got_error_msg(GOT_ERR_BAD_PATH,
2137 "switching between branches requires that "
2138 "the entire work tree gets updated");
2141 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2144 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2146 free(head_commit_id);
2149 error = check_same_branch(commit_id, head_ref, NULL, repo);
2152 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2156 error = check_linear_ancestry(commit_id,
2157 got_worktree_get_base_commit_id(worktree), 0, repo);
2158 if (error != NULL) {
2159 if (error->code == GOT_ERR_ANCESTRY)
2160 error = got_error(GOT_ERR_BRANCH_MOVED);
2163 error = check_same_branch(commit_id, head_ref, NULL, repo);
2168 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2170 error = got_worktree_set_base_commit_id(worktree, repo,
2176 error = got_worktree_checkout_files(worktree, &paths, repo,
2177 update_progress, &did_something, check_cancelled, NULL);
2182 printf("Updated to commit %s\n", commit_id_str);
2184 printf("Already up-to-date\n");
2186 free(worktree_path);
2187 TAILQ_FOREACH(pe, &paths, entry)
2188 free((char *)pe->path);
2189 got_pathlist_free(&paths);
2191 free(commit_id_str);
2195 static const struct got_error *
2196 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2197 const char *path, int diff_context, int ignore_whitespace,
2198 struct got_repository *repo)
2200 const struct got_error *err = NULL;
2201 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2204 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2209 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2213 while (path[0] == '/')
2215 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2216 ignore_whitespace, stdout);
2219 got_object_blob_close(blob1);
2220 got_object_blob_close(blob2);
2224 static const struct got_error *
2225 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2226 const char *path, int diff_context, int ignore_whitespace,
2227 struct got_repository *repo)
2229 const struct got_error *err = NULL;
2230 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2231 struct got_diff_blob_output_unidiff_arg arg;
2234 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2239 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2243 arg.diff_context = diff_context;
2244 arg.ignore_whitespace = ignore_whitespace;
2245 arg.outfile = stdout;
2246 while (path[0] == '/')
2248 err = got_diff_tree(tree1, tree2, path, path, repo,
2249 got_diff_blob_output_unidiff, &arg, 1);
2252 got_object_tree_close(tree1);
2254 got_object_tree_close(tree2);
2258 static const struct got_error *
2259 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2260 const char *path, int diff_context, struct got_repository *repo)
2262 const struct got_error *err = NULL;
2263 struct got_commit_object *pcommit = NULL;
2264 char *id_str1 = NULL, *id_str2 = NULL;
2265 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2266 struct got_object_qid *qid;
2268 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2270 err = got_object_open_as_commit(&pcommit, repo,
2276 if (path && path[0] != '\0') {
2278 err = got_object_id_by_path(&obj_id2, repo, id, path);
2281 err = got_object_id_str(&id_str2, obj_id2);
2287 err = got_object_id_by_path(&obj_id1, repo,
2293 err = got_object_id_str(&id_str1, obj_id1);
2299 err = got_object_get_type(&obj_type, repo, obj_id2);
2304 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2306 case GOT_OBJ_TYPE_BLOB:
2307 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2310 case GOT_OBJ_TYPE_TREE:
2311 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2315 err = got_error(GOT_ERR_OBJ_TYPE);
2321 obj_id2 = got_object_commit_get_tree_id(commit);
2322 err = got_object_id_str(&id_str2, obj_id2);
2325 obj_id1 = got_object_commit_get_tree_id(pcommit);
2326 err = got_object_id_str(&id_str1, obj_id1);
2329 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2330 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2336 got_object_commit_close(pcommit);
2341 get_datestr(time_t *time, char *datebuf)
2343 struct tm mytm, *tm;
2346 tm = gmtime_r(time, &mytm);
2349 s = asctime_r(tm, datebuf);
2352 p = strchr(s, '\n');
2358 static const struct got_error *
2359 match_logmsg(int *have_match, struct got_object_id *id,
2360 struct got_commit_object *commit, regex_t *regex)
2362 const struct got_error *err = NULL;
2363 regmatch_t regmatch;
2364 char *id_str = NULL, *logmsg = NULL;
2368 err = got_object_id_str(&id_str, id);
2372 err = got_object_commit_get_logmsg(&logmsg, commit);
2376 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2384 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2386 static const struct got_error *
2387 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2388 struct got_repository *repo, const char *path, int show_patch,
2389 int diff_context, struct got_reflist_head *refs)
2391 const struct got_error *err = NULL;
2392 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2394 time_t committer_time;
2395 const char *author, *committer;
2396 char *refs_str = NULL;
2397 struct got_reflist_entry *re;
2399 SIMPLEQ_FOREACH(re, refs, entry) {
2402 struct got_tag_object *tag = NULL;
2405 name = got_ref_get_name(re->ref);
2406 if (strcmp(name, GOT_REF_HEAD) == 0)
2408 if (strncmp(name, "refs/", 5) == 0)
2410 if (strncmp(name, "got/", 4) == 0)
2412 if (strncmp(name, "heads/", 6) == 0)
2414 if (strncmp(name, "remotes/", 8) == 0)
2416 if (strncmp(name, "tags/", 5) == 0) {
2417 err = got_object_open_as_tag(&tag, repo, re->id);
2419 if (err->code != GOT_ERR_OBJ_TYPE)
2421 /* Ref points at something other than a tag. */
2426 cmp = got_object_id_cmp(tag ?
2427 got_object_tag_get_object_id(tag) : re->id, id);
2429 got_object_tag_close(tag);
2433 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2435 err = got_error_from_errno("asprintf");
2441 err = got_object_id_str(&id_str, id);
2445 printf(GOT_COMMIT_SEP_STR);
2446 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2447 refs_str ? refs_str : "", refs_str ? ")" : "");
2452 printf("from: %s\n", got_object_commit_get_author(commit));
2453 committer_time = got_object_commit_get_committer_time(commit);
2454 datestr = get_datestr(&committer_time, datebuf);
2456 printf("date: %s UTC\n", datestr);
2457 author = got_object_commit_get_author(commit);
2458 committer = got_object_commit_get_committer(commit);
2459 if (strcmp(author, committer) != 0)
2460 printf("via: %s\n", committer);
2461 if (got_object_commit_get_nparents(commit) > 1) {
2462 const struct got_object_id_queue *parent_ids;
2463 struct got_object_qid *qid;
2465 parent_ids = got_object_commit_get_parent_ids(commit);
2466 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2467 err = got_object_id_str(&id_str, qid->id);
2470 printf("parent %d: %s\n", n++, id_str);
2475 err = got_object_commit_get_logmsg(&logmsg0, commit);
2481 line = strsep(&logmsg, "\n");
2483 printf(" %s\n", line);
2488 err = print_patch(commit, id, path, diff_context, repo);
2493 if (fflush(stdout) != 0 && err == NULL)
2494 err = got_error_from_errno("fflush");
2498 static const struct got_error *
2499 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2500 const char *path, int show_patch, const char *search_pattern,
2501 int diff_context, int limit, int log_branches,
2502 struct got_reflist_head *refs)
2504 const struct got_error *err;
2505 struct got_commit_graph *graph;
2509 if (search_pattern &&
2510 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2511 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2513 err = got_commit_graph_open(&graph, path, !log_branches);
2516 err = got_commit_graph_iter_start(graph, root_id, repo,
2517 check_cancelled, NULL);
2521 struct got_commit_object *commit;
2522 struct got_object_id *id;
2524 if (sigint_received || sigpipe_received)
2527 err = got_commit_graph_iter_next(&id, graph, repo,
2528 check_cancelled, NULL);
2530 if (err->code == GOT_ERR_ITER_COMPLETED)
2537 err = got_object_open_as_commit(&commit, repo, id);
2541 if (search_pattern) {
2542 err = match_logmsg(&have_match, id, commit, ®ex);
2544 got_object_commit_close(commit);
2547 if (have_match == 0) {
2548 got_object_commit_close(commit);
2553 err = print_commit(commit, id, repo, path, show_patch,
2554 diff_context, refs);
2555 got_object_commit_close(commit);
2556 if (err || (limit && --limit == 0))
2562 got_commit_graph_close(graph);
2569 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2570 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2575 get_default_log_limit(void)
2577 const char *got_default_log_limit;
2581 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2582 if (got_default_log_limit == NULL)
2584 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2590 static const struct got_error *
2591 cmd_log(int argc, char *argv[])
2593 const struct got_error *error;
2594 struct got_repository *repo = NULL;
2595 struct got_worktree *worktree = NULL;
2596 struct got_commit_object *commit = NULL;
2597 struct got_object_id *id = NULL;
2598 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2599 const char *start_commit = NULL, *search_pattern = NULL;
2600 int diff_context = -1, ch;
2601 int show_patch = 0, limit = 0, log_branches = 0;
2603 struct got_reflist_head refs;
2605 SIMPLEQ_INIT(&refs);
2608 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2614 limit = get_default_log_limit();
2616 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2622 start_commit = optarg;
2625 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2628 err(1, "-C option %s", errstr);
2631 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2633 err(1, "-l option %s", errstr);
2639 repo_path = realpath(optarg, NULL);
2640 if (repo_path == NULL)
2641 return got_error_from_errno2("realpath",
2643 got_path_strip_trailing_slashes(repo_path);
2646 search_pattern = optarg;
2657 if (diff_context == -1)
2659 else if (!show_patch)
2660 errx(1, "-C reguires -p");
2662 cwd = getcwd(NULL, 0);
2664 error = got_error_from_errno("getcwd");
2668 error = got_worktree_open(&worktree, cwd);
2669 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2676 error = got_error_from_errno("strdup");
2679 } else if (argc == 1) {
2681 error = got_worktree_resolve_path(&path, worktree,
2686 path = strdup(argv[0]);
2688 error = got_error_from_errno("strdup");
2695 if (repo_path == NULL) {
2696 repo_path = worktree ?
2697 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2699 if (repo_path == NULL) {
2700 error = got_error_from_errno("strdup");
2704 error = got_repo_open(&repo, repo_path, NULL);
2708 error = apply_unveil(got_repo_get_path(repo), 1,
2709 worktree ? got_worktree_get_root_path(worktree) : NULL);
2713 if (start_commit == NULL) {
2714 struct got_reference *head_ref;
2715 error = got_ref_open(&head_ref, repo,
2716 worktree ? got_worktree_get_head_ref_name(worktree)
2720 error = got_ref_resolve(&id, repo, head_ref);
2721 got_ref_close(head_ref);
2724 error = got_object_open_as_commit(&commit, repo, id);
2726 struct got_reference *ref;
2727 error = got_ref_open(&ref, repo, start_commit, 0);
2728 if (error == NULL) {
2730 error = got_ref_resolve(&id, repo, ref);
2734 error = got_object_get_type(&obj_type, repo, id);
2737 if (obj_type == GOT_OBJ_TYPE_TAG) {
2738 struct got_tag_object *tag;
2739 error = got_object_open_as_tag(&tag, repo, id);
2742 if (got_object_tag_get_object_type(tag) !=
2743 GOT_OBJ_TYPE_COMMIT) {
2744 got_object_tag_close(tag);
2745 error = got_error(GOT_ERR_OBJ_TYPE);
2749 id = got_object_id_dup(
2750 got_object_tag_get_object_id(tag));
2752 error = got_error_from_errno(
2753 "got_object_id_dup");
2754 got_object_tag_close(tag);
2757 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2758 error = got_error(GOT_ERR_OBJ_TYPE);
2761 error = got_object_open_as_commit(&commit, repo, id);
2765 if (commit == NULL) {
2766 error = got_repo_match_object_id_prefix(&id,
2767 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2776 const char *prefix = got_worktree_get_path_prefix(worktree);
2778 if (asprintf(&p, "%s%s%s", prefix,
2779 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2780 error = got_error_from_errno("asprintf");
2783 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2786 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2791 path = in_repo_path;
2794 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2798 error = print_commits(id, repo, path, show_patch, search_pattern,
2799 diff_context, limit, log_branches, &refs);
2806 got_worktree_close(worktree);
2808 const struct got_error *repo_error;
2809 repo_error = got_repo_close(repo);
2813 got_ref_list_free(&refs);
2820 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2821 "[-w] [object1 object2 | path]\n", getprogname());
2825 struct print_diff_arg {
2826 struct got_repository *repo;
2827 struct got_worktree *worktree;
2832 int ignore_whitespace;
2835 static const struct got_error *
2836 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2837 const char *path, struct got_object_id *blob_id,
2838 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2839 int dirfd, const char *de_name)
2841 struct print_diff_arg *a = arg;
2842 const struct got_error *err = NULL;
2843 struct got_blob_object *blob1 = NULL;
2846 char *abspath = NULL, *label1 = NULL;
2849 if (a->diff_staged) {
2850 if (staged_status != GOT_STATUS_MODIFY &&
2851 staged_status != GOT_STATUS_ADD &&
2852 staged_status != GOT_STATUS_DELETE)
2855 if (staged_status == GOT_STATUS_DELETE)
2857 if (status == GOT_STATUS_NONEXISTENT)
2858 return got_error_set_errno(ENOENT, path);
2859 if (status != GOT_STATUS_MODIFY &&
2860 status != GOT_STATUS_ADD &&
2861 status != GOT_STATUS_DELETE &&
2862 status != GOT_STATUS_CONFLICT)
2866 if (!a->header_shown) {
2867 printf("diff %s %s%s\n", a->id_str,
2868 got_worktree_get_root_path(a->worktree),
2869 a->diff_staged ? " (staged changes)" : "");
2870 a->header_shown = 1;
2873 if (a->diff_staged) {
2874 const char *label1 = NULL, *label2 = NULL;
2875 switch (staged_status) {
2876 case GOT_STATUS_MODIFY:
2880 case GOT_STATUS_ADD:
2883 case GOT_STATUS_DELETE:
2887 return got_error(GOT_ERR_FILE_STATUS);
2889 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2890 label1, label2, a->diff_context, a->ignore_whitespace,
2894 if (staged_status == GOT_STATUS_ADD ||
2895 staged_status == GOT_STATUS_MODIFY) {
2897 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2901 err = got_object_id_str(&id_str, staged_blob_id);
2904 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2905 err = got_error_from_errno("asprintf");
2910 } else if (status != GOT_STATUS_ADD) {
2911 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2916 if (status != GOT_STATUS_DELETE) {
2917 if (asprintf(&abspath, "%s/%s",
2918 got_worktree_get_root_path(a->worktree), path) == -1) {
2919 err = got_error_from_errno("asprintf");
2924 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2926 err = got_error_from_errno2("openat", abspath);
2930 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2932 err = got_error_from_errno2("open", abspath);
2936 if (fstat(fd, &sb) == -1) {
2937 err = got_error_from_errno2("fstat", abspath);
2940 f2 = fdopen(fd, "r");
2942 err = got_error_from_errno2("fdopen", abspath);
2949 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2950 a->diff_context, a->ignore_whitespace, stdout);
2953 got_object_blob_close(blob1);
2954 if (f2 && fclose(f2) == EOF && err == NULL)
2955 err = got_error_from_errno("fclose");
2956 if (fd != -1 && close(fd) == -1 && err == NULL)
2957 err = got_error_from_errno("close");
2962 static const struct got_error *
2963 cmd_diff(int argc, char *argv[])
2965 const struct got_error *error;
2966 struct got_repository *repo = NULL;
2967 struct got_worktree *worktree = NULL;
2968 char *cwd = NULL, *repo_path = NULL;
2969 struct got_object_id *id1 = NULL, *id2 = NULL;
2970 const char *id_str1 = NULL, *id_str2 = NULL;
2971 char *label1 = NULL, *label2 = NULL;
2973 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2978 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2983 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2986 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2989 err(1, "-C option %s", errstr);
2992 repo_path = realpath(optarg, NULL);
2993 if (repo_path == NULL)
2994 return got_error_from_errno2("realpath",
2996 got_path_strip_trailing_slashes(repo_path);
3002 ignore_whitespace = 1;
3013 cwd = getcwd(NULL, 0);
3015 error = got_error_from_errno("getcwd");
3018 error = got_worktree_open(&worktree, cwd);
3019 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3022 if (worktree == NULL) {
3023 error = got_error(GOT_ERR_NOT_WORKTREE);
3028 "-r option can't be used when diffing a work tree");
3029 repo_path = strdup(got_worktree_get_repo_path(worktree));
3030 if (repo_path == NULL) {
3031 error = got_error_from_errno("strdup");
3035 error = got_worktree_resolve_path(&path, worktree,
3042 error = got_error_from_errno("strdup");
3046 } else if (argc == 2) {
3048 errx(1, "-s option can't be used when diffing "
3049 "objects in repository");
3052 if (worktree && repo_path == NULL) {
3054 strdup(got_worktree_get_repo_path(worktree));
3055 if (repo_path == NULL) {
3056 error = got_error_from_errno("strdup");
3063 if (repo_path == NULL) {
3064 repo_path = getcwd(NULL, 0);
3065 if (repo_path == NULL)
3066 return got_error_from_errno("getcwd");
3069 error = got_repo_open(&repo, repo_path, NULL);
3074 error = apply_unveil(got_repo_get_path(repo), 1,
3075 worktree ? got_worktree_get_root_path(worktree) : NULL);
3080 struct print_diff_arg arg;
3081 struct got_pathlist_head paths;
3086 error = got_object_id_str(&id_str,
3087 got_worktree_get_base_commit_id(worktree));
3091 arg.worktree = worktree;
3092 arg.diff_context = diff_context;
3093 arg.id_str = id_str;
3094 arg.header_shown = 0;
3095 arg.diff_staged = diff_staged;
3096 arg.ignore_whitespace = ignore_whitespace;
3098 error = got_pathlist_append(&paths, path, NULL);
3102 error = got_worktree_status(worktree, &paths, repo, print_diff,
3103 &arg, check_cancelled, NULL);
3105 got_pathlist_free(&paths);
3109 error = got_repo_match_object_id(&id1, &label1, id_str1,
3110 GOT_OBJ_TYPE_ANY, 1, repo);
3114 error = got_repo_match_object_id(&id2, &label2, id_str2,
3115 GOT_OBJ_TYPE_ANY, 1, repo);
3119 error = got_object_get_type(&type1, repo, id1);
3123 error = got_object_get_type(&type2, repo, id2);
3127 if (type1 != type2) {
3128 error = got_error(GOT_ERR_OBJ_TYPE);
3133 case GOT_OBJ_TYPE_BLOB:
3134 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3135 diff_context, ignore_whitespace, repo, stdout);
3137 case GOT_OBJ_TYPE_TREE:
3138 error = got_diff_objects_as_trees(id1, id2, "", "",
3139 diff_context, ignore_whitespace, repo, stdout);
3141 case GOT_OBJ_TYPE_COMMIT:
3142 printf("diff %s %s\n", label1, label2);
3143 error = got_diff_objects_as_commits(id1, id2, diff_context,
3144 ignore_whitespace, repo, stdout);
3147 error = got_error(GOT_ERR_OBJ_TYPE);
3156 got_worktree_close(worktree);
3158 const struct got_error *repo_error;
3159 repo_error = got_repo_close(repo);
3170 "usage: %s blame [-c commit] [-r repository-path] path\n",
3179 char datebuf[11]; /* YYYY-MM-DD + NUL */
3182 struct blame_cb_args {
3183 struct blame_line *lines;
3187 off_t *line_offsets;
3189 struct got_repository *repo;
3192 static const struct got_error *
3193 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3195 const struct got_error *err = NULL;
3196 struct blame_cb_args *a = arg;
3197 struct blame_line *bline;
3199 size_t linesize = 0;
3200 struct got_commit_object *commit = NULL;
3203 time_t committer_time;
3205 if (nlines != a->nlines ||
3206 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3207 return got_error(GOT_ERR_RANGE);
3209 if (sigint_received)
3210 return got_error(GOT_ERR_ITER_COMPLETED);
3213 return NULL; /* no change in this commit */
3215 /* Annotate this line. */
3216 bline = &a->lines[lineno - 1];
3217 if (bline->annotated)
3219 err = got_object_id_str(&bline->id_str, id);
3223 err = got_object_open_as_commit(&commit, a->repo, id);
3227 bline->committer = strdup(got_object_commit_get_committer(commit));
3228 if (bline->committer == NULL) {
3229 err = got_error_from_errno("strdup");
3233 committer_time = got_object_commit_get_committer_time(commit);
3234 if (localtime_r(&committer_time, &tm) == NULL)
3235 return got_error_from_errno("localtime_r");
3236 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3237 &tm) >= sizeof(bline->datebuf)) {
3238 err = got_error(GOT_ERR_NO_SPACE);
3241 bline->annotated = 1;
3243 /* Print lines annotated so far. */
3244 bline = &a->lines[a->lineno_cur - 1];
3245 if (!bline->annotated)
3248 offset = a->line_offsets[a->lineno_cur - 1];
3249 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3250 err = got_error_from_errno("fseeko");
3254 while (bline->annotated) {
3255 char *smallerthan, *at, *nl, *committer;
3258 if (getline(&line, &linesize, a->f) == -1) {
3260 err = got_error_from_errno("getline");
3264 committer = bline->committer;
3265 smallerthan = strchr(committer, '<');
3266 if (smallerthan && smallerthan[1] != '\0')
3267 committer = smallerthan + 1;
3268 at = strchr(committer, '@');
3271 len = strlen(committer);
3273 committer[8] = '\0';
3275 nl = strchr(line, '\n');
3278 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3279 bline->id_str, bline->datebuf, committer, line);
3282 bline = &a->lines[a->lineno_cur - 1];
3286 got_object_commit_close(commit);
3291 static const struct got_error *
3292 cmd_blame(int argc, char *argv[])
3294 const struct got_error *error;
3295 struct got_repository *repo = NULL;
3296 struct got_worktree *worktree = NULL;
3297 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3298 struct got_object_id *obj_id = NULL;
3299 struct got_object_id *commit_id = NULL;
3300 struct got_blob_object *blob = NULL;
3301 char *commit_id_str = NULL;
3302 struct blame_cb_args bca;
3303 int ch, obj_type, i;
3306 memset(&bca, 0, sizeof(bca));
3309 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3314 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3317 commit_id_str = optarg;
3320 repo_path = realpath(optarg, NULL);
3321 if (repo_path == NULL)
3322 return got_error_from_errno2("realpath",
3324 got_path_strip_trailing_slashes(repo_path);
3340 cwd = getcwd(NULL, 0);
3342 error = got_error_from_errno("getcwd");
3345 if (repo_path == NULL) {
3346 error = got_worktree_open(&worktree, cwd);
3347 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3353 strdup(got_worktree_get_repo_path(worktree));
3354 if (repo_path == NULL)
3355 error = got_error_from_errno("strdup");
3359 repo_path = strdup(cwd);
3360 if (repo_path == NULL) {
3361 error = got_error_from_errno("strdup");
3367 error = got_repo_open(&repo, repo_path, NULL);
3371 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3376 const char *prefix = got_worktree_get_path_prefix(worktree);
3377 char *p, *worktree_subdir = cwd +
3378 strlen(got_worktree_get_root_path(worktree));
3379 if (asprintf(&p, "%s%s%s%s%s",
3380 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3381 worktree_subdir, worktree_subdir[0] ? "/" : "",
3383 error = got_error_from_errno("asprintf");
3386 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3389 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3394 if (commit_id_str == NULL) {
3395 struct got_reference *head_ref;
3396 error = got_ref_open(&head_ref, repo, worktree ?
3397 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3400 error = got_ref_resolve(&commit_id, repo, head_ref);
3401 got_ref_close(head_ref);
3405 error = got_repo_match_object_id(&commit_id, NULL,
3406 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3411 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3415 error = got_object_get_type(&obj_type, repo, obj_id);
3419 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3420 error = got_error(GOT_ERR_OBJ_TYPE);
3424 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3427 bca.f = got_opentemp();
3428 if (bca.f == NULL) {
3429 error = got_error_from_errno("got_opentemp");
3432 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3433 &bca.line_offsets, bca.f, blob);
3434 if (error || bca.nlines == 0)
3437 /* Don't include \n at EOF in the blame line count. */
3438 if (bca.line_offsets[bca.nlines - 1] == filesize)
3441 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3442 if (bca.lines == NULL) {
3443 error = got_error_from_errno("calloc");
3447 bca.nlines_prec = 0;
3455 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3456 check_cancelled, NULL);
3464 got_object_blob_close(blob);
3466 got_worktree_close(worktree);
3468 const struct got_error *repo_error;
3469 repo_error = got_repo_close(repo);
3474 for (i = 0; i < bca.nlines; i++) {
3475 struct blame_line *bline = &bca.lines[i];
3476 free(bline->id_str);
3477 free(bline->committer);
3481 free(bca.line_offsets);
3482 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3483 error = got_error_from_errno("fclose");
3491 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3497 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3498 const char *root_path)
3500 int is_root_path = (strcmp(path, root_path) == 0);
3501 const char *modestr = "";
3502 mode_t mode = got_tree_entry_get_mode(te);
3504 path += strlen(root_path);
3505 while (path[0] == '/')
3508 if (got_object_tree_entry_is_submodule(te))
3510 else if (S_ISLNK(mode))
3512 else if (S_ISDIR(mode))
3514 else if (mode & S_IXUSR)
3517 printf("%s%s%s%s%s\n", id ? id : "", path,
3518 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3521 static const struct got_error *
3522 print_tree(const char *path, struct got_object_id *commit_id,
3523 int show_ids, int recurse, const char *root_path,
3524 struct got_repository *repo)
3526 const struct got_error *err = NULL;
3527 struct got_object_id *tree_id = NULL;
3528 struct got_tree_object *tree = NULL;
3531 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3535 err = got_object_open_as_tree(&tree, repo, tree_id);
3538 nentries = got_object_tree_get_nentries(tree);
3539 for (i = 0; i < nentries; i++) {
3540 struct got_tree_entry *te;
3543 if (sigint_received || sigpipe_received)
3546 te = got_object_tree_get_entry(tree, i);
3549 err = got_object_id_str(&id_str,
3550 got_tree_entry_get_id(te));
3553 if (asprintf(&id, "%s ", id_str) == -1) {
3554 err = got_error_from_errno("asprintf");
3560 print_entry(te, id, path, root_path);
3563 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3565 if (asprintf(&child_path, "%s%s%s", path,
3566 path[0] == '/' && path[1] == '\0' ? "" : "/",
3567 got_tree_entry_get_name(te)) == -1) {
3568 err = got_error_from_errno("asprintf");
3571 err = print_tree(child_path, commit_id, show_ids, 1,
3580 got_object_tree_close(tree);
3585 static const struct got_error *
3586 cmd_tree(int argc, char *argv[])
3588 const struct got_error *error;
3589 struct got_repository *repo = NULL;
3590 struct got_worktree *worktree = NULL;
3592 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3593 struct got_object_id *commit_id = NULL;
3594 char *commit_id_str = NULL;
3595 int show_ids = 0, recurse = 0;
3599 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3604 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3607 commit_id_str = optarg;
3610 repo_path = realpath(optarg, NULL);
3611 if (repo_path == NULL)
3612 return got_error_from_errno2("realpath",
3614 got_path_strip_trailing_slashes(repo_path);
3638 cwd = getcwd(NULL, 0);
3640 error = got_error_from_errno("getcwd");
3643 if (repo_path == NULL) {
3644 error = got_worktree_open(&worktree, cwd);
3645 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3651 strdup(got_worktree_get_repo_path(worktree));
3652 if (repo_path == NULL)
3653 error = got_error_from_errno("strdup");
3657 repo_path = strdup(cwd);
3658 if (repo_path == NULL) {
3659 error = got_error_from_errno("strdup");
3665 error = got_repo_open(&repo, repo_path, NULL);
3669 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3675 char *p, *worktree_subdir = cwd +
3676 strlen(got_worktree_get_root_path(worktree));
3677 if (asprintf(&p, "%s/%s",
3678 got_worktree_get_path_prefix(worktree),
3679 worktree_subdir) == -1) {
3680 error = got_error_from_errno("asprintf");
3683 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3690 if (in_repo_path == NULL) {
3691 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3696 if (commit_id_str == NULL) {
3697 struct got_reference *head_ref;
3698 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3701 error = got_ref_resolve(&commit_id, repo, head_ref);
3702 got_ref_close(head_ref);
3706 error = got_repo_match_object_id(&commit_id, NULL,
3707 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3712 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3713 in_repo_path, repo);
3720 got_worktree_close(worktree);
3722 const struct got_error *repo_error;
3723 repo_error = got_repo_close(repo);
3733 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3737 static const struct got_error *
3738 print_status(void *arg, unsigned char status, unsigned char staged_status,
3739 const char *path, struct got_object_id *blob_id,
3740 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3741 int dirfd, const char *de_name)
3743 if (status == staged_status && (status == GOT_STATUS_DELETE))
3744 status = GOT_STATUS_NO_CHANGE;
3745 printf("%c%c %s\n", status, staged_status, path);
3749 static const struct got_error *
3750 cmd_status(int argc, char *argv[])
3752 const struct got_error *error = NULL;
3753 struct got_repository *repo = NULL;
3754 struct got_worktree *worktree = NULL;
3756 struct got_pathlist_head paths;
3757 struct got_pathlist_entry *pe;
3762 while ((ch = getopt(argc, argv, "")) != -1) {
3774 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3778 cwd = getcwd(NULL, 0);
3780 error = got_error_from_errno("getcwd");
3784 error = got_worktree_open(&worktree, cwd);
3788 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3793 error = apply_unveil(got_repo_get_path(repo), 1,
3794 got_worktree_get_root_path(worktree));
3798 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3802 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3803 check_cancelled, NULL);
3805 TAILQ_FOREACH(pe, &paths, entry)
3806 free((char *)pe->path);
3807 got_pathlist_free(&paths);
3816 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3821 static const struct got_error *
3822 list_refs(struct got_repository *repo)
3824 static const struct got_error *err = NULL;
3825 struct got_reflist_head refs;
3826 struct got_reflist_entry *re;
3828 SIMPLEQ_INIT(&refs);
3829 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3833 SIMPLEQ_FOREACH(re, &refs, entry) {
3835 refstr = got_ref_to_str(re->ref);
3837 return got_error_from_errno("got_ref_to_str");
3838 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3842 got_ref_list_free(&refs);
3846 static const struct got_error *
3847 delete_ref(struct got_repository *repo, const char *refname)
3849 const struct got_error *err = NULL;
3850 struct got_reference *ref;
3852 err = got_ref_open(&ref, repo, refname, 0);
3856 err = got_ref_delete(ref, repo);
3861 static const struct got_error *
3862 add_ref(struct got_repository *repo, const char *refname, const char *target)
3864 const struct got_error *err = NULL;
3865 struct got_object_id *id;
3866 struct got_reference *ref = NULL;
3869 * Don't let the user create a reference name with a leading '-'.
3870 * While technically a valid reference name, this case is usually
3871 * an unintended typo.
3873 if (refname[0] == '-')
3874 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3876 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3879 struct got_reference *target_ref;
3881 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3883 err = got_ref_open(&target_ref, repo, target, 0);
3886 err = got_ref_resolve(&id, repo, target_ref);
3887 got_ref_close(target_ref);
3892 err = got_ref_alloc(&ref, refname, id);
3896 err = got_ref_write(ref, repo);
3904 static const struct got_error *
3905 add_symref(struct got_repository *repo, const char *refname, const char *target)
3907 const struct got_error *err = NULL;
3908 struct got_reference *ref = NULL;
3909 struct got_reference *target_ref = NULL;
3912 * Don't let the user create a reference name with a leading '-'.
3913 * While technically a valid reference name, this case is usually
3914 * an unintended typo.
3916 if (refname[0] == '-')
3917 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3919 err = got_ref_open(&target_ref, repo, target, 0);
3923 err = got_ref_alloc_symref(&ref, refname, target_ref);
3927 err = got_ref_write(ref, repo);
3930 got_ref_close(target_ref);
3936 static const struct got_error *
3937 cmd_ref(int argc, char *argv[])
3939 const struct got_error *error = NULL;
3940 struct got_repository *repo = NULL;
3941 struct got_worktree *worktree = NULL;
3942 char *cwd = NULL, *repo_path = NULL;
3943 int ch, do_list = 0, create_symref = 0;
3944 const char *delref = NULL;
3946 /* TODO: Add -s option for adding symbolic references. */
3947 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3953 repo_path = realpath(optarg, NULL);
3954 if (repo_path == NULL)
3955 return got_error_from_errno2("realpath",
3957 got_path_strip_trailing_slashes(repo_path);
3971 if (do_list && delref)
3972 errx(1, "-l and -d options are mutually exclusive\n");
3977 if (do_list || delref) {
3979 errx(1, "-s option cannot be used together with the "
3980 "-l or -d options");
3983 } else if (argc != 2)
3988 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3992 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3993 "sendfd unveil", NULL) == -1)
3997 cwd = getcwd(NULL, 0);
3999 error = got_error_from_errno("getcwd");
4003 if (repo_path == NULL) {
4004 error = got_worktree_open(&worktree, cwd);
4005 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4011 strdup(got_worktree_get_repo_path(worktree));
4012 if (repo_path == NULL)
4013 error = got_error_from_errno("strdup");
4017 repo_path = strdup(cwd);
4018 if (repo_path == NULL) {
4019 error = got_error_from_errno("strdup");
4025 error = got_repo_open(&repo, repo_path, NULL);
4029 error = apply_unveil(got_repo_get_path(repo), do_list,
4030 worktree ? got_worktree_get_root_path(worktree) : NULL);
4035 error = list_refs(repo);
4037 error = delete_ref(repo, delref);
4038 else if (create_symref)
4039 error = add_symref(repo, argv[0], argv[1]);
4041 error = add_ref(repo, argv[0], argv[1]);
4044 got_repo_close(repo);
4046 got_worktree_close(worktree);
4056 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4057 "[name]\n", getprogname());
4061 static const struct got_error *
4062 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4063 struct got_reference *ref)
4065 const struct got_error *err = NULL;
4066 const char *refname, *marker = " ";
4069 refname = got_ref_get_name(ref);
4070 if (worktree && strcmp(refname,
4071 got_worktree_get_head_ref_name(worktree)) == 0) {
4072 struct got_object_id *id = NULL;
4074 err = got_ref_resolve(&id, repo, ref);
4077 if (got_object_id_cmp(id,
4078 got_worktree_get_base_commit_id(worktree)) == 0)
4085 if (strncmp(refname, "refs/heads/", 11) == 0)
4087 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4090 refstr = got_ref_to_str(ref);
4092 return got_error_from_errno("got_ref_to_str");
4094 printf("%s%s: %s\n", marker, refname, refstr);
4099 static const struct got_error *
4100 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4102 const char *refname;
4104 if (worktree == NULL)
4105 return got_error(GOT_ERR_NOT_WORKTREE);
4107 refname = got_worktree_get_head_ref_name(worktree);
4109 if (strncmp(refname, "refs/heads/", 11) == 0)
4111 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4114 printf("%s\n", refname);
4119 static const struct got_error *
4120 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4122 static const struct got_error *err = NULL;
4123 struct got_reflist_head refs;
4124 struct got_reflist_entry *re;
4125 struct got_reference *temp_ref = NULL;
4126 int rebase_in_progress, histedit_in_progress;
4128 SIMPLEQ_INIT(&refs);
4131 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4136 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4141 if (rebase_in_progress || histedit_in_progress) {
4142 err = got_ref_open(&temp_ref, repo,
4143 got_worktree_get_head_ref_name(worktree), 0);
4146 list_branch(repo, worktree, temp_ref);
4147 got_ref_close(temp_ref);
4151 err = got_ref_list(&refs, repo, "refs/heads",
4152 got_ref_cmp_by_name, NULL);
4156 SIMPLEQ_FOREACH(re, &refs, entry)
4157 list_branch(repo, worktree, re->ref);
4159 got_ref_list_free(&refs);
4163 static const struct got_error *
4164 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4165 const char *branch_name)
4167 const struct got_error *err = NULL;
4168 struct got_reference *ref = NULL;
4171 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4172 return got_error_from_errno("asprintf");
4174 err = got_ref_open(&ref, repo, refname, 0);
4179 strcmp(got_worktree_get_head_ref_name(worktree),
4180 got_ref_get_name(ref)) == 0) {
4181 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4182 "will not delete this work tree's current branch");
4186 err = got_ref_delete(ref, repo);
4194 static const struct got_error *
4195 add_branch(struct got_repository *repo, const char *branch_name,
4196 struct got_object_id *base_commit_id)
4198 const struct got_error *err = NULL;
4199 struct got_reference *ref = NULL;
4200 char *base_refname = NULL, *refname = NULL;
4203 * Don't let the user create a branch name with a leading '-'.
4204 * While technically a valid reference name, this case is usually
4205 * an unintended typo.
4207 if (branch_name[0] == '-')
4208 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4210 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4211 err = got_error_from_errno("asprintf");
4215 err = got_ref_open(&ref, repo, refname, 0);
4217 err = got_error(GOT_ERR_BRANCH_EXISTS);
4219 } else if (err->code != GOT_ERR_NOT_REF)
4222 err = got_ref_alloc(&ref, refname, base_commit_id);
4226 err = got_ref_write(ref, repo);
4235 static const struct got_error *
4236 cmd_branch(int argc, char *argv[])
4238 const struct got_error *error = NULL;
4239 struct got_repository *repo = NULL;
4240 struct got_worktree *worktree = NULL;
4241 char *cwd = NULL, *repo_path = NULL;
4242 int ch, do_list = 0, do_show = 0, do_update = 1;
4243 const char *delref = NULL, *commit_id_arg = NULL;
4244 struct got_reference *ref = NULL;
4245 struct got_pathlist_head paths;
4246 struct got_pathlist_entry *pe;
4247 struct got_object_id *commit_id = NULL;
4248 char *commit_id_str = NULL;
4252 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4255 commit_id_arg = optarg;
4261 repo_path = realpath(optarg, NULL);
4262 if (repo_path == NULL)
4263 return got_error_from_errno2("realpath",
4265 got_path_strip_trailing_slashes(repo_path);
4279 if (do_list && delref)
4280 errx(1, "-l and -d options are mutually exclusive\n");
4285 if (!do_list && !delref && argc == 0)
4288 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4289 errx(1, "-c option can only be used when creating a branch");
4291 if (do_list || delref) {
4294 } else if (!do_show && argc != 1)
4298 if (do_list || do_show) {
4299 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4303 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4304 "sendfd unveil", NULL) == -1)
4308 cwd = getcwd(NULL, 0);
4310 error = got_error_from_errno("getcwd");
4314 if (repo_path == NULL) {
4315 error = got_worktree_open(&worktree, cwd);
4316 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4322 strdup(got_worktree_get_repo_path(worktree));
4323 if (repo_path == NULL)
4324 error = got_error_from_errno("strdup");
4328 repo_path = strdup(cwd);
4329 if (repo_path == NULL) {
4330 error = got_error_from_errno("strdup");
4336 error = got_repo_open(&repo, repo_path, NULL);
4340 error = apply_unveil(got_repo_get_path(repo), do_list,
4341 worktree ? got_worktree_get_root_path(worktree) : NULL);
4346 error = show_current_branch(repo, worktree);
4348 error = list_branches(repo, worktree);
4350 error = delete_branch(repo, worktree, delref);
4352 if (commit_id_arg == NULL)
4353 commit_id_arg = worktree ?
4354 got_worktree_get_head_ref_name(worktree) :
4356 error = got_repo_match_object_id(&commit_id, NULL,
4357 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4360 error = add_branch(repo, argv[0], commit_id);
4363 if (worktree && do_update) {
4364 int did_something = 0;
4365 char *branch_refname = NULL;
4367 error = got_object_id_str(&commit_id_str, commit_id);
4370 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4374 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4376 error = got_error_from_errno("asprintf");
4379 error = got_ref_open(&ref, repo, branch_refname, 0);
4380 free(branch_refname);
4383 error = switch_head_ref(ref, commit_id, worktree,
4387 error = got_worktree_set_base_commit_id(worktree, repo,
4391 error = got_worktree_checkout_files(worktree, &paths,
4392 repo, update_progress, &did_something,
4393 check_cancelled, NULL);
4397 printf("Updated to commit %s\n", commit_id_str);
4404 got_repo_close(repo);
4406 got_worktree_close(worktree);
4410 free(commit_id_str);
4411 TAILQ_FOREACH(pe, &paths, entry)
4412 free((char *)pe->path);
4413 got_pathlist_free(&paths);
4422 "usage: %s tag [-c commit] [-r repository] [-l] "
4423 "[-m message] name\n", getprogname());
4428 static const struct got_error *
4429 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4431 const struct got_error *err = NULL;
4432 struct got_reflist_entry *re, *se, *new;
4433 struct got_object_id *re_id, *se_id;
4434 struct got_tag_object *re_tag, *se_tag;
4435 time_t re_time, se_time;
4437 SIMPLEQ_FOREACH(re, tags, entry) {
4438 se = SIMPLEQ_FIRST(sorted);
4440 err = got_reflist_entry_dup(&new, re);
4443 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4446 err = got_ref_resolve(&re_id, repo, re->ref);
4449 err = got_object_open_as_tag(&re_tag, repo, re_id);
4453 re_time = got_object_tag_get_tagger_time(re_tag);
4454 got_object_tag_close(re_tag);
4458 err = got_ref_resolve(&se_id, repo, re->ref);
4461 err = got_object_open_as_tag(&se_tag, repo, se_id);
4465 se_time = got_object_tag_get_tagger_time(se_tag);
4466 got_object_tag_close(se_tag);
4468 if (se_time > re_time) {
4469 err = got_reflist_entry_dup(&new, re);
4472 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4475 se = SIMPLEQ_NEXT(se, entry);
4484 static const struct got_error *
4485 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4487 static const struct got_error *err = NULL;
4488 struct got_reflist_head refs;
4489 struct got_reflist_entry *re;
4491 SIMPLEQ_INIT(&refs);
4493 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4497 SIMPLEQ_FOREACH(re, &refs, entry) {
4498 const char *refname;
4499 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4503 struct got_object_id *id;
4504 struct got_tag_object *tag;
4505 struct got_commit_object *commit = NULL;
4507 refname = got_ref_get_name(re->ref);
4508 if (strncmp(refname, "refs/tags/", 10) != 0)
4511 refstr = got_ref_to_str(re->ref);
4512 if (refstr == NULL) {
4513 err = got_error_from_errno("got_ref_to_str");
4516 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4519 err = got_ref_resolve(&id, repo, re->ref);
4522 err = got_object_open_as_tag(&tag, repo, id);
4524 if (err->code != GOT_ERR_OBJ_TYPE) {
4528 /* "lightweight" tag */
4529 err = got_object_open_as_commit(&commit, repo, id);
4534 tagger = got_object_commit_get_committer(commit);
4536 got_object_commit_get_committer_time(commit);
4537 err = got_object_id_str(&id_str, id);
4543 tagger = got_object_tag_get_tagger(tag);
4544 tagger_time = got_object_tag_get_tagger_time(tag);
4545 err = got_object_id_str(&id_str,
4546 got_object_tag_get_object_id(tag));
4550 printf("from: %s\n", tagger);
4551 datestr = get_datestr(&tagger_time, datebuf);
4553 printf("date: %s UTC\n", datestr);
4555 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4557 switch (got_object_tag_get_object_type(tag)) {
4558 case GOT_OBJ_TYPE_BLOB:
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4562 case GOT_OBJ_TYPE_TREE:
4563 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4566 case GOT_OBJ_TYPE_COMMIT:
4567 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4570 case GOT_OBJ_TYPE_TAG:
4571 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4580 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4583 got_object_commit_close(commit);
4585 tagmsg0 = strdup(got_object_tag_get_message(tag));
4586 got_object_tag_close(tag);
4587 if (tagmsg0 == NULL) {
4588 err = got_error_from_errno("strdup");
4595 line = strsep(&tagmsg, "\n");
4597 printf(" %s\n", line);
4602 got_ref_list_free(&refs);
4606 static const struct got_error *
4607 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4608 const char *tag_name, const char *repo_path)
4610 const struct got_error *err = NULL;
4611 char *template = NULL, *initial_content = NULL;
4612 char *editor = NULL;
4615 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4616 err = got_error_from_errno("asprintf");
4620 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4621 commit_id_str, tag_name) == -1) {
4622 err = got_error_from_errno("asprintf");
4626 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4630 dprintf(fd, initial_content);
4633 err = get_editor(&editor);
4636 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4638 free(initial_content);
4642 /* Editor is done; we can now apply unveil(2) */
4644 err = apply_unveil(repo_path, 0, NULL);
4653 static const struct got_error *
4654 add_tag(struct got_repository *repo, const char *tag_name,
4655 const char *commit_arg, const char *tagmsg_arg)
4657 const struct got_error *err = NULL;
4658 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4659 char *label = NULL, *commit_id_str = NULL;
4660 struct got_reference *ref = NULL;
4661 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4662 char *tagmsg_path = NULL, *tag_id_str = NULL;
4663 int preserve_tagmsg = 0;
4666 * Don't let the user create a tag name with a leading '-'.
4667 * While technically a valid reference name, this case is usually
4668 * an unintended typo.
4670 if (tag_name[0] == '-')
4671 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4673 err = get_author(&tagger, repo);
4677 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4678 GOT_OBJ_TYPE_COMMIT, 1, repo);
4682 err = got_object_id_str(&commit_id_str, commit_id);
4686 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4687 refname = strdup(tag_name);
4688 if (refname == NULL) {
4689 err = got_error_from_errno("strdup");
4693 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4694 err = got_error_from_errno("asprintf");
4698 err = got_ref_open(&ref, repo, refname, 0);
4700 err = got_error(GOT_ERR_TAG_EXISTS);
4702 } else if (err->code != GOT_ERR_NOT_REF)
4705 if (tagmsg_arg == NULL) {
4706 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4707 tag_name, got_repo_get_path(repo));
4709 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4710 tagmsg_path != NULL)
4711 preserve_tagmsg = 1;
4716 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4717 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4720 preserve_tagmsg = 1;
4724 err = got_ref_alloc(&ref, refname, tag_id);
4727 preserve_tagmsg = 1;
4731 err = got_ref_write(ref, repo);
4734 preserve_tagmsg = 1;
4738 err = got_object_id_str(&tag_id_str, tag_id);
4741 preserve_tagmsg = 1;
4744 printf("Created tag %s\n", tag_id_str);
4746 if (preserve_tagmsg) {
4747 fprintf(stderr, "%s: tag message preserved in %s\n",
4748 getprogname(), tagmsg_path);
4749 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4750 err = got_error_from_errno2("unlink", tagmsg_path);
4755 free(commit_id_str);
4763 static const struct got_error *
4764 cmd_tag(int argc, char *argv[])
4766 const struct got_error *error = NULL;
4767 struct got_repository *repo = NULL;
4768 struct got_worktree *worktree = NULL;
4769 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4770 char *gitconfig_path = NULL;
4771 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4772 int ch, do_list = 0;
4774 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4777 commit_id_arg = optarg;
4783 repo_path = realpath(optarg, NULL);
4784 if (repo_path == NULL)
4785 return got_error_from_errno2("realpath",
4787 got_path_strip_trailing_slashes(repo_path);
4802 if (commit_id_arg != NULL)
4803 errx(1, "-c option can only be used when creating a tag");
4805 errx(1, "-l and -m options are mutually exclusive");
4808 } else if (argc != 1)
4815 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4819 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4820 "sendfd unveil", NULL) == -1)
4824 cwd = getcwd(NULL, 0);
4826 error = got_error_from_errno("getcwd");
4830 if (repo_path == NULL) {
4831 error = got_worktree_open(&worktree, cwd);
4832 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4838 strdup(got_worktree_get_repo_path(worktree));
4839 if (repo_path == NULL)
4840 error = got_error_from_errno("strdup");
4844 repo_path = strdup(cwd);
4845 if (repo_path == NULL) {
4846 error = got_error_from_errno("strdup");
4853 error = got_repo_open(&repo, repo_path, NULL);
4856 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4859 error = list_tags(repo, worktree);
4861 error = get_gitconfig_path(&gitconfig_path);
4864 error = got_repo_open(&repo, repo_path, gitconfig_path);
4869 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4874 if (commit_id_arg == NULL) {
4875 struct got_reference *head_ref;
4876 struct got_object_id *commit_id;
4877 error = got_ref_open(&head_ref, repo,
4878 worktree ? got_worktree_get_head_ref_name(worktree)
4882 error = got_ref_resolve(&commit_id, repo, head_ref);
4883 got_ref_close(head_ref);
4886 error = got_object_id_str(&commit_id_str, commit_id);
4892 error = add_tag(repo, tag_name,
4893 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4897 got_repo_close(repo);
4899 got_worktree_close(worktree);
4902 free(gitconfig_path);
4903 free(commit_id_str);
4910 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4915 static const struct got_error *
4916 add_progress(void *arg, unsigned char status, const char *path)
4918 while (path[0] == '/')
4920 printf("%c %s\n", status, path);
4924 static const struct got_error *
4925 cmd_add(int argc, char *argv[])
4927 const struct got_error *error = NULL;
4928 struct got_repository *repo = NULL;
4929 struct got_worktree *worktree = NULL;
4931 struct got_pathlist_head paths;
4932 struct got_pathlist_entry *pe;
4933 int ch, can_recurse = 0, no_ignores = 0;
4937 while ((ch = getopt(argc, argv, "IR")) != -1) {
4955 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4962 cwd = getcwd(NULL, 0);
4964 error = got_error_from_errno("getcwd");
4968 error = got_worktree_open(&worktree, cwd);
4972 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4977 error = apply_unveil(got_repo_get_path(repo), 1,
4978 got_worktree_get_root_path(worktree));
4982 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4986 if (!can_recurse && no_ignores) {
4987 error = got_error_msg(GOT_ERR_BAD_PATH,
4988 "disregarding ignores requires -R option");
4996 TAILQ_FOREACH(pe, &paths, entry) {
4997 if (asprintf(&ondisk_path, "%s/%s",
4998 got_worktree_get_root_path(worktree),
5000 error = got_error_from_errno("asprintf");
5003 if (lstat(ondisk_path, &sb) == -1) {
5004 if (errno == ENOENT) {
5008 error = got_error_from_errno2("lstat",
5014 if (S_ISDIR(sb.st_mode)) {
5015 error = got_error_msg(GOT_ERR_BAD_PATH,
5016 "adding directories requires -R option");
5022 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5023 NULL, repo, no_ignores);
5026 got_repo_close(repo);
5028 got_worktree_close(worktree);
5029 TAILQ_FOREACH(pe, &paths, entry)
5030 free((char *)pe->path);
5031 got_pathlist_free(&paths);
5039 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5044 static const struct got_error *
5045 print_remove_status(void *arg, unsigned char status,
5046 unsigned char staged_status, const char *path)
5048 while (path[0] == '/')
5050 if (status == GOT_STATUS_NONEXISTENT)
5052 if (status == staged_status && (status == GOT_STATUS_DELETE))
5053 status = GOT_STATUS_NO_CHANGE;
5054 printf("%c%c %s\n", status, staged_status, path);
5058 static const struct got_error *
5059 cmd_remove(int argc, char *argv[])
5061 const struct got_error *error = NULL;
5062 struct got_worktree *worktree = NULL;
5063 struct got_repository *repo = NULL;
5065 struct got_pathlist_head paths;
5066 struct got_pathlist_entry *pe;
5067 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5071 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5074 delete_local_mods = 1;
5092 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5099 cwd = getcwd(NULL, 0);
5101 error = got_error_from_errno("getcwd");
5104 error = got_worktree_open(&worktree, cwd);
5108 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5113 error = apply_unveil(got_repo_get_path(repo), 1,
5114 got_worktree_get_root_path(worktree));
5118 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5125 TAILQ_FOREACH(pe, &paths, entry) {
5126 if (asprintf(&ondisk_path, "%s/%s",
5127 got_worktree_get_root_path(worktree),
5129 error = got_error_from_errno("asprintf");
5132 if (lstat(ondisk_path, &sb) == -1) {
5133 if (errno == ENOENT) {
5137 error = got_error_from_errno2("lstat",
5143 if (S_ISDIR(sb.st_mode)) {
5144 error = got_error_msg(GOT_ERR_BAD_PATH,
5145 "removing directories requires -R option");
5151 error = got_worktree_schedule_delete(worktree, &paths,
5152 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5155 got_repo_close(repo);
5157 got_worktree_close(worktree);
5158 TAILQ_FOREACH(pe, &paths, entry)
5159 free((char *)pe->path);
5160 got_pathlist_free(&paths);
5168 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5169 "path ...\n", getprogname());
5173 static const struct got_error *
5174 revert_progress(void *arg, unsigned char status, const char *path)
5176 if (status == GOT_STATUS_UNVERSIONED)
5179 while (path[0] == '/')
5181 printf("%c %s\n", status, path);
5185 struct choose_patch_arg {
5186 FILE *patch_script_file;
5190 static const struct got_error *
5191 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5192 int nchanges, const char *action)
5195 size_t linesize = 0;
5199 case GOT_STATUS_ADD:
5200 printf("A %s\n%s this addition? [y/n] ", path, action);
5202 case GOT_STATUS_DELETE:
5203 printf("D %s\n%s this deletion? [y/n] ", path, action);
5205 case GOT_STATUS_MODIFY:
5206 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5207 return got_error_from_errno("fseek");
5208 printf(GOT_COMMIT_SEP_STR);
5209 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5211 if (ferror(patch_file))
5212 return got_error_from_errno("getline");
5213 printf(GOT_COMMIT_SEP_STR);
5214 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5215 path, n, nchanges, action);
5218 return got_error_path(path, GOT_ERR_FILE_STATUS);
5224 static const struct got_error *
5225 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5226 FILE *patch_file, int n, int nchanges)
5228 const struct got_error *err = NULL;
5230 size_t linesize = 0;
5233 struct choose_patch_arg *a = arg;
5235 *choice = GOT_PATCH_CHOICE_NONE;
5237 if (a->patch_script_file) {
5239 err = show_change(status, path, patch_file, n, nchanges,
5243 linelen = getline(&line, &linesize, a->patch_script_file);
5244 if (linelen == -1) {
5245 if (ferror(a->patch_script_file))
5246 return got_error_from_errno("getline");
5249 nl = strchr(line, '\n');
5252 if (strcmp(line, "y") == 0) {
5253 *choice = GOT_PATCH_CHOICE_YES;
5255 } else if (strcmp(line, "n") == 0) {
5256 *choice = GOT_PATCH_CHOICE_NO;
5258 } else if (strcmp(line, "q") == 0 &&
5259 status == GOT_STATUS_MODIFY) {
5260 *choice = GOT_PATCH_CHOICE_QUIT;
5263 printf("invalid response '%s'\n", line);
5268 while (resp != 'y' && resp != 'n' && resp != 'q') {
5269 err = show_change(status, path, patch_file, n, nchanges,
5276 if (status == GOT_STATUS_MODIFY) {
5277 if (resp != 'y' && resp != 'n' && resp != 'q') {
5278 printf("invalid response '%c'\n", resp);
5281 } else if (resp != 'y' && resp != 'n') {
5282 printf("invalid response '%c'\n", resp);
5288 *choice = GOT_PATCH_CHOICE_YES;
5289 else if (resp == 'n')
5290 *choice = GOT_PATCH_CHOICE_NO;
5291 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5292 *choice = GOT_PATCH_CHOICE_QUIT;
5298 static const struct got_error *
5299 cmd_revert(int argc, char *argv[])
5301 const struct got_error *error = NULL;
5302 struct got_worktree *worktree = NULL;
5303 struct got_repository *repo = NULL;
5304 char *cwd = NULL, *path = NULL;
5305 struct got_pathlist_head paths;
5306 struct got_pathlist_entry *pe;
5307 int ch, can_recurse = 0, pflag = 0;
5308 FILE *patch_script_file = NULL;
5309 const char *patch_script_path = NULL;
5310 struct choose_patch_arg cpa;
5314 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5320 patch_script_path = optarg;
5335 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5336 "unveil", NULL) == -1)
5341 if (patch_script_path && !pflag)
5342 errx(1, "-F option can only be used together with -p option");
5344 cwd = getcwd(NULL, 0);
5346 error = got_error_from_errno("getcwd");
5349 error = got_worktree_open(&worktree, cwd);
5353 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5358 if (patch_script_path) {
5359 patch_script_file = fopen(patch_script_path, "r");
5360 if (patch_script_file == NULL) {
5361 error = got_error_from_errno2("fopen",
5366 error = apply_unveil(got_repo_get_path(repo), 1,
5367 got_worktree_get_root_path(worktree));
5371 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5378 TAILQ_FOREACH(pe, &paths, entry) {
5379 if (asprintf(&ondisk_path, "%s/%s",
5380 got_worktree_get_root_path(worktree),
5382 error = got_error_from_errno("asprintf");
5385 if (lstat(ondisk_path, &sb) == -1) {
5386 if (errno == ENOENT) {
5390 error = got_error_from_errno2("lstat",
5396 if (S_ISDIR(sb.st_mode)) {
5397 error = got_error_msg(GOT_ERR_BAD_PATH,
5398 "reverting directories requires -R option");
5404 cpa.patch_script_file = patch_script_file;
5405 cpa.action = "revert";
5406 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5407 pflag ? choose_patch : NULL, &cpa, repo);
5409 if (patch_script_file && fclose(patch_script_file) == EOF &&
5411 error = got_error_from_errno2("fclose", patch_script_path);
5413 got_repo_close(repo);
5415 got_worktree_close(worktree);
5424 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5429 struct collect_commit_logmsg_arg {
5430 const char *cmdline_log;
5432 const char *worktree_path;
5433 const char *branch_name;
5434 const char *repo_path;
5439 static const struct got_error *
5440 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5443 char *initial_content = NULL;
5444 struct got_pathlist_entry *pe;
5445 const struct got_error *err = NULL;
5446 char *template = NULL;
5447 struct collect_commit_logmsg_arg *a = arg;
5451 /* if a message was specified on the command line, just use it */
5452 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5453 len = strlen(a->cmdline_log) + 1;
5454 *logmsg = malloc(len + 1);
5455 if (*logmsg == NULL)
5456 return got_error_from_errno("malloc");
5457 strlcpy(*logmsg, a->cmdline_log, len);
5461 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5462 return got_error_from_errno("asprintf");
5464 if (asprintf(&initial_content,
5465 "\n# changes to be committed on branch %s:\n",
5466 a->branch_name) == -1)
5467 return got_error_from_errno("asprintf");
5469 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5473 dprintf(fd, initial_content);
5475 TAILQ_FOREACH(pe, commitable_paths, entry) {
5476 struct got_commitable *ct = pe->data;
5477 dprintf(fd, "# %c %s\n",
5478 got_commitable_get_status(ct),
5479 got_commitable_get_path(ct));
5483 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5485 free(initial_content);
5488 /* Editor is done; we can now apply unveil(2) */
5490 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5499 static const struct got_error *
5500 cmd_commit(int argc, char *argv[])
5502 const struct got_error *error = NULL;
5503 struct got_worktree *worktree = NULL;
5504 struct got_repository *repo = NULL;
5505 char *cwd = NULL, *id_str = NULL;
5506 struct got_object_id *id = NULL;
5507 const char *logmsg = NULL;
5508 struct collect_commit_logmsg_arg cl_arg;
5509 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5510 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5511 struct got_pathlist_head paths;
5514 cl_arg.logmsg_path = NULL;
5516 while ((ch = getopt(argc, argv, "m:")) != -1) {
5531 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5532 "unveil", NULL) == -1)
5535 cwd = getcwd(NULL, 0);
5537 error = got_error_from_errno("getcwd");
5540 error = got_worktree_open(&worktree, cwd);
5544 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5547 if (rebase_in_progress) {
5548 error = got_error(GOT_ERR_REBASING);
5552 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5557 error = get_gitconfig_path(&gitconfig_path);
5560 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5565 error = get_author(&author, repo);
5570 * unveil(2) traverses exec(2); if an editor is used we have
5571 * to apply unveil after the log message has been written.
5573 if (logmsg == NULL || strlen(logmsg) == 0)
5574 error = get_editor(&editor);
5576 error = apply_unveil(got_repo_get_path(repo), 0,
5577 got_worktree_get_root_path(worktree));
5581 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5585 cl_arg.editor = editor;
5586 cl_arg.cmdline_log = logmsg;
5587 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5588 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5589 if (!histedit_in_progress) {
5590 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5591 error = got_error(GOT_ERR_COMMIT_BRANCH);
5594 cl_arg.branch_name += 11;
5596 cl_arg.repo_path = got_repo_get_path(repo);
5597 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5598 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5600 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5601 cl_arg.logmsg_path != NULL)
5602 preserve_logmsg = 1;
5606 error = got_object_id_str(&id_str, id);
5609 printf("Created commit %s\n", id_str);
5611 if (preserve_logmsg) {
5612 fprintf(stderr, "%s: log message preserved in %s\n",
5613 getprogname(), cl_arg.logmsg_path);
5614 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5616 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5617 free(cl_arg.logmsg_path);
5619 got_repo_close(repo);
5621 got_worktree_close(worktree);
5624 free(gitconfig_path);
5631 usage_cherrypick(void)
5633 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5637 static const struct got_error *
5638 cmd_cherrypick(int argc, char *argv[])
5640 const struct got_error *error = NULL;
5641 struct got_worktree *worktree = NULL;
5642 struct got_repository *repo = NULL;
5643 char *cwd = NULL, *commit_id_str = NULL;
5644 struct got_object_id *commit_id = NULL;
5645 struct got_commit_object *commit = NULL;
5646 struct got_object_qid *pid;
5647 struct got_reference *head_ref = NULL;
5648 int ch, did_something = 0;
5650 while ((ch = getopt(argc, argv, "")) != -1) {
5662 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 "unveil", NULL) == -1)
5669 cwd = getcwd(NULL, 0);
5671 error = got_error_from_errno("getcwd");
5674 error = got_worktree_open(&worktree, cwd);
5678 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5683 error = apply_unveil(got_repo_get_path(repo), 0,
5684 got_worktree_get_root_path(worktree));
5688 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5689 GOT_OBJ_TYPE_COMMIT, repo);
5690 if (error != NULL) {
5691 struct got_reference *ref;
5692 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5694 error = got_ref_open(&ref, repo, argv[0], 0);
5697 error = got_ref_resolve(&commit_id, repo, ref);
5702 error = got_object_id_str(&commit_id_str, commit_id);
5706 error = got_ref_open(&head_ref, repo,
5707 got_worktree_get_head_ref_name(worktree), 0);
5711 error = check_same_branch(commit_id, head_ref, NULL, repo);
5713 if (error->code != GOT_ERR_ANCESTRY)
5717 error = got_error(GOT_ERR_SAME_BRANCH);
5721 error = got_object_open_as_commit(&commit, repo, commit_id);
5724 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5725 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5726 commit_id, repo, update_progress, &did_something, check_cancelled,
5732 printf("Merged commit %s\n", commit_id_str);
5735 got_object_commit_close(commit);
5736 free(commit_id_str);
5738 got_ref_close(head_ref);
5740 got_worktree_close(worktree);
5742 got_repo_close(repo);
5749 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5753 static const struct got_error *
5754 cmd_backout(int argc, char *argv[])
5756 const struct got_error *error = NULL;
5757 struct got_worktree *worktree = NULL;
5758 struct got_repository *repo = NULL;
5759 char *cwd = NULL, *commit_id_str = NULL;
5760 struct got_object_id *commit_id = NULL;
5761 struct got_commit_object *commit = NULL;
5762 struct got_object_qid *pid;
5763 struct got_reference *head_ref = NULL;
5764 int ch, did_something = 0;
5766 while ((ch = getopt(argc, argv, "")) != -1) {
5778 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5779 "unveil", NULL) == -1)
5785 cwd = getcwd(NULL, 0);
5787 error = got_error_from_errno("getcwd");
5790 error = got_worktree_open(&worktree, cwd);
5794 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5799 error = apply_unveil(got_repo_get_path(repo), 0,
5800 got_worktree_get_root_path(worktree));
5804 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5805 GOT_OBJ_TYPE_COMMIT, repo);
5806 if (error != NULL) {
5807 struct got_reference *ref;
5808 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5810 error = got_ref_open(&ref, repo, argv[0], 0);
5813 error = got_ref_resolve(&commit_id, repo, ref);
5818 error = got_object_id_str(&commit_id_str, commit_id);
5822 error = got_ref_open(&head_ref, repo,
5823 got_worktree_get_head_ref_name(worktree), 0);
5827 error = check_same_branch(commit_id, head_ref, NULL, repo);
5831 error = got_object_open_as_commit(&commit, repo, commit_id);
5834 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5836 error = got_error(GOT_ERR_ROOT_COMMIT);
5840 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5841 update_progress, &did_something, check_cancelled, NULL);
5846 printf("Backed out commit %s\n", commit_id_str);
5849 got_object_commit_close(commit);
5850 free(commit_id_str);
5852 got_ref_close(head_ref);
5854 got_worktree_close(worktree);
5856 got_repo_close(repo);
5863 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5869 trim_logmsg(char *logmsg, int limit)
5874 len = strlen(logmsg);
5878 nl = strchr(logmsg, '\n');
5883 static const struct got_error *
5884 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5886 const struct got_error *err;
5887 char *logmsg0 = NULL;
5890 err = got_object_commit_get_logmsg(&logmsg0, commit);
5895 while (isspace((unsigned char)s[0]))
5898 *logmsg = strdup(s);
5899 if (*logmsg == NULL) {
5900 err = got_error_from_errno("strdup");
5904 trim_logmsg(*logmsg, limit);
5910 static const struct got_error *
5911 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5913 const struct got_error *err;
5914 struct got_commit_object *commit = NULL;
5915 char *id_str = NULL, *logmsg = NULL;
5917 err = got_object_open_as_commit(&commit, repo, id);
5921 err = got_object_id_str(&id_str, id);
5927 err = get_short_logmsg(&logmsg, 42, commit);
5931 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5934 got_object_commit_close(commit);
5939 static const struct got_error *
5940 show_rebase_progress(struct got_commit_object *commit,
5941 struct got_object_id *old_id, struct got_object_id *new_id)
5943 const struct got_error *err;
5944 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5946 err = got_object_id_str(&old_id_str, old_id);
5951 err = got_object_id_str(&new_id_str, new_id);
5956 old_id_str[12] = '\0';
5958 new_id_str[12] = '\0';
5960 err = get_short_logmsg(&logmsg, 42, commit);
5964 printf("%s -> %s: %s\n", old_id_str,
5965 new_id_str ? new_id_str : "no-op change", logmsg);
5973 static const struct got_error *
5974 rebase_progress(void *arg, unsigned char status, const char *path)
5976 unsigned char *rebase_status = arg;
5978 while (path[0] == '/')
5980 printf("%c %s\n", status, path);
5982 if (*rebase_status == GOT_STATUS_CONFLICT)
5984 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5985 *rebase_status = status;
5989 static const struct got_error *
5990 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5991 struct got_reference *branch, struct got_reference *new_base_branch,
5992 struct got_reference *tmp_branch, struct got_repository *repo)
5994 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5995 return got_worktree_rebase_complete(worktree, fileindex,
5996 new_base_branch, tmp_branch, branch, repo);
5999 static const struct got_error *
6000 rebase_commit(struct got_pathlist_head *merged_paths,
6001 struct got_worktree *worktree, struct got_fileindex *fileindex,
6002 struct got_reference *tmp_branch,
6003 struct got_object_id *commit_id, struct got_repository *repo)
6005 const struct got_error *error;
6006 struct got_commit_object *commit;
6007 struct got_object_id *new_commit_id;
6009 error = got_object_open_as_commit(&commit, repo, commit_id);
6013 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6014 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6016 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6018 error = show_rebase_progress(commit, commit_id, NULL);
6020 error = show_rebase_progress(commit, commit_id, new_commit_id);
6021 free(new_commit_id);
6024 got_object_commit_close(commit);
6028 struct check_path_prefix_arg {
6029 const char *path_prefix;
6034 static const struct got_error *
6035 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6036 struct got_blob_object *blob2, struct got_object_id *id1,
6037 struct got_object_id *id2, const char *path1, const char *path2,
6038 mode_t mode1, mode_t mode2, struct got_repository *repo)
6040 struct check_path_prefix_arg *a = arg;
6042 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6043 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6044 return got_error(a->errcode);
6049 static const struct got_error *
6050 check_path_prefix(struct got_object_id *parent_id,
6051 struct got_object_id *commit_id, const char *path_prefix,
6052 int errcode, struct got_repository *repo)
6054 const struct got_error *err;
6055 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6056 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6057 struct check_path_prefix_arg cpp_arg;
6059 if (got_path_is_root_dir(path_prefix))
6062 err = got_object_open_as_commit(&commit, repo, commit_id);
6066 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6070 err = got_object_open_as_tree(&tree1, repo,
6071 got_object_commit_get_tree_id(parent_commit));
6075 err = got_object_open_as_tree(&tree2, repo,
6076 got_object_commit_get_tree_id(commit));
6080 cpp_arg.path_prefix = path_prefix;
6081 while (cpp_arg.path_prefix[0] == '/')
6082 cpp_arg.path_prefix++;
6083 cpp_arg.len = strlen(cpp_arg.path_prefix);
6084 cpp_arg.errcode = errcode;
6085 err = got_diff_tree(tree1, tree2, "", "", repo,
6086 check_path_prefix_in_diff, &cpp_arg, 0);
6089 got_object_tree_close(tree1);
6091 got_object_tree_close(tree2);
6093 got_object_commit_close(commit);
6095 got_object_commit_close(parent_commit);
6099 static const struct got_error *
6100 collect_commits(struct got_object_id_queue *commits,
6101 struct got_object_id *initial_commit_id,
6102 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6103 const char *path_prefix, int path_prefix_errcode,
6104 struct got_repository *repo)
6106 const struct got_error *err = NULL;
6107 struct got_commit_graph *graph = NULL;
6108 struct got_object_id *parent_id = NULL;
6109 struct got_object_qid *qid;
6110 struct got_object_id *commit_id = initial_commit_id;
6112 err = got_commit_graph_open(&graph, "/", 1);
6116 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6117 check_cancelled, NULL);
6120 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6121 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6122 check_cancelled, NULL);
6124 if (err->code == GOT_ERR_ITER_COMPLETED) {
6125 err = got_error_msg(GOT_ERR_ANCESTRY,
6126 "ran out of commits to rebase before "
6127 "youngest common ancestor commit has "
6132 err = check_path_prefix(parent_id, commit_id,
6133 path_prefix, path_prefix_errcode, repo);
6137 err = got_object_qid_alloc(&qid, commit_id);
6140 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6141 commit_id = parent_id;
6145 got_commit_graph_close(graph);
6149 static const struct got_error *
6150 cmd_rebase(int argc, char *argv[])
6152 const struct got_error *error = NULL;
6153 struct got_worktree *worktree = NULL;
6154 struct got_repository *repo = NULL;
6155 struct got_fileindex *fileindex = NULL;
6157 struct got_reference *branch = NULL;
6158 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6159 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6160 struct got_object_id *resume_commit_id = NULL;
6161 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6162 struct got_commit_object *commit = NULL;
6163 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6164 int histedit_in_progress = 0;
6165 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6166 struct got_object_id_queue commits;
6167 struct got_pathlist_head merged_paths;
6168 const struct got_object_id_queue *parent_ids;
6169 struct got_object_qid *qid, *pid;
6171 SIMPLEQ_INIT(&commits);
6172 TAILQ_INIT(&merged_paths);
6174 while ((ch = getopt(argc, argv, "ac")) != -1) {
6180 continue_rebase = 1;
6192 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6193 "unveil", NULL) == -1)
6196 if (abort_rebase && continue_rebase)
6198 else if (abort_rebase || continue_rebase) {
6201 } else if (argc != 1)
6204 cwd = getcwd(NULL, 0);
6206 error = got_error_from_errno("getcwd");
6209 error = got_worktree_open(&worktree, cwd);
6213 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6218 error = apply_unveil(got_repo_get_path(repo), 0,
6219 got_worktree_get_root_path(worktree));
6223 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6227 if (histedit_in_progress) {
6228 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6232 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6238 if (!rebase_in_progress) {
6239 error = got_error(GOT_ERR_NOT_REBASING);
6242 error = got_worktree_rebase_continue(&resume_commit_id,
6243 &new_base_branch, &tmp_branch, &branch, &fileindex,
6247 printf("Switching work tree to %s\n",
6248 got_ref_get_symref_target(new_base_branch));
6249 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6250 new_base_branch, update_progress, &did_something);
6253 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6254 goto done; /* nothing else to do */
6257 if (continue_rebase) {
6258 if (!rebase_in_progress) {
6259 error = got_error(GOT_ERR_NOT_REBASING);
6262 error = got_worktree_rebase_continue(&resume_commit_id,
6263 &new_base_branch, &tmp_branch, &branch, &fileindex,
6268 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6269 resume_commit_id, repo);
6273 yca_id = got_object_id_dup(resume_commit_id);
6274 if (yca_id == NULL) {
6275 error = got_error_from_errno("got_object_id_dup");
6279 error = got_ref_open(&branch, repo, argv[0], 0);
6284 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6288 if (!continue_rebase) {
6289 struct got_object_id *base_commit_id;
6291 base_commit_id = got_worktree_get_base_commit_id(worktree);
6292 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6293 base_commit_id, branch_head_commit_id, repo,
6294 check_cancelled, NULL);
6297 if (yca_id == NULL) {
6298 error = got_error_msg(GOT_ERR_ANCESTRY,
6299 "specified branch shares no common ancestry "
6300 "with work tree's branch");
6304 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6306 if (error->code != GOT_ERR_ANCESTRY)
6310 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6311 "specified branch resolves to a commit which "
6312 "is already contained in work tree's branch");
6315 error = got_worktree_rebase_prepare(&new_base_branch,
6316 &tmp_branch, &fileindex, worktree, branch, repo);
6321 commit_id = branch_head_commit_id;
6322 error = got_object_open_as_commit(&commit, repo, commit_id);
6326 parent_ids = got_object_commit_get_parent_ids(commit);
6327 pid = SIMPLEQ_FIRST(parent_ids);
6329 if (!continue_rebase) {
6331 error = got_worktree_rebase_abort(worktree, fileindex,
6332 repo, new_base_branch, update_progress,
6336 printf("Rebase of %s aborted\n",
6337 got_ref_get_name(branch));
6339 error = got_error(GOT_ERR_EMPTY_REBASE);
6342 error = collect_commits(&commits, commit_id, pid->id,
6343 yca_id, got_worktree_get_path_prefix(worktree),
6344 GOT_ERR_REBASE_PATH, repo);
6345 got_object_commit_close(commit);
6350 if (SIMPLEQ_EMPTY(&commits)) {
6351 if (continue_rebase) {
6352 error = rebase_complete(worktree, fileindex,
6353 branch, new_base_branch, tmp_branch, repo);
6356 /* Fast-forward the reference of the branch. */
6357 struct got_object_id *new_head_commit_id;
6359 error = got_ref_resolve(&new_head_commit_id, repo,
6363 error = got_object_id_str(&id_str, new_head_commit_id);
6364 printf("Forwarding %s to commit %s\n",
6365 got_ref_get_name(branch), id_str);
6367 error = got_ref_change_ref(branch,
6368 new_head_commit_id);
6375 SIMPLEQ_FOREACH(qid, &commits, entry) {
6376 commit_id = qid->id;
6377 parent_id = pid ? pid->id : yca_id;
6380 error = got_worktree_rebase_merge_files(&merged_paths,
6381 worktree, fileindex, parent_id, commit_id, repo,
6382 rebase_progress, &rebase_status, check_cancelled, NULL);
6386 if (rebase_status == GOT_STATUS_CONFLICT) {
6387 error = show_rebase_merge_conflict(qid->id, repo);
6390 got_worktree_rebase_pathlist_free(&merged_paths);
6394 error = rebase_commit(&merged_paths, worktree, fileindex,
6395 tmp_branch, commit_id, repo);
6396 got_worktree_rebase_pathlist_free(&merged_paths);
6401 if (rebase_status == GOT_STATUS_CONFLICT) {
6402 error = got_worktree_rebase_postpone(worktree, fileindex);
6405 error = got_error_msg(GOT_ERR_CONFLICTS,
6406 "conflicts must be resolved before rebasing can continue");
6408 error = rebase_complete(worktree, fileindex, branch,
6409 new_base_branch, tmp_branch, repo);
6411 got_object_id_queue_free(&commits);
6412 free(branch_head_commit_id);
6413 free(resume_commit_id);
6416 got_object_commit_close(commit);
6418 got_ref_close(branch);
6419 if (new_base_branch)
6420 got_ref_close(new_base_branch);
6422 got_ref_close(tmp_branch);
6424 got_worktree_close(worktree);
6426 got_repo_close(repo);
6431 usage_histedit(void)
6433 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6438 #define GOT_HISTEDIT_PICK 'p'
6439 #define GOT_HISTEDIT_EDIT 'e'
6440 #define GOT_HISTEDIT_FOLD 'f'
6441 #define GOT_HISTEDIT_DROP 'd'
6442 #define GOT_HISTEDIT_MESG 'm'
6444 static struct got_histedit_cmd {
6448 } got_histedit_cmds[] = {
6449 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6450 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6451 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6453 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6454 { GOT_HISTEDIT_MESG, "mesg",
6455 "single-line log message for commit above (open editor if empty)" },
6458 struct got_histedit_list_entry {
6459 TAILQ_ENTRY(got_histedit_list_entry) entry;
6460 struct got_object_id *commit_id;
6461 const struct got_histedit_cmd *cmd;
6464 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6466 static const struct got_error *
6467 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6468 FILE *f, struct got_repository *repo)
6470 const struct got_error *err = NULL;
6471 char *logmsg = NULL, *id_str = NULL;
6472 struct got_commit_object *commit = NULL;
6475 err = got_object_open_as_commit(&commit, repo, commit_id);
6479 err = get_short_logmsg(&logmsg, 34, commit);
6483 err = got_object_id_str(&id_str, commit_id);
6487 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6489 err = got_ferror(f, GOT_ERR_IO);
6492 got_object_commit_close(commit);
6498 static const struct got_error *
6499 histedit_write_commit_list(struct got_object_id_queue *commits,
6500 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6502 const struct got_error *err = NULL;
6503 struct got_object_qid *qid;
6505 if (SIMPLEQ_EMPTY(commits))
6506 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6508 SIMPLEQ_FOREACH(qid, commits, entry) {
6509 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6513 if (edit_logmsg_only) {
6514 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6516 err = got_ferror(f, GOT_ERR_IO);
6525 static const struct got_error *
6526 write_cmd_list(FILE *f, const char *branch_name,
6527 struct got_object_id_queue *commits)
6529 const struct got_error *err = NULL;
6532 struct got_object_qid *qid;
6534 qid = SIMPLEQ_FIRST(commits);
6535 err = got_object_id_str(&id_str, qid->id);
6540 "# Editing the history of branch '%s' starting at\n"
6542 "# Commits will be processed in order from top to "
6543 "bottom of this file.\n", branch_name, id_str);
6545 err = got_ferror(f, GOT_ERR_IO);
6549 n = fprintf(f, "# Available histedit commands:\n");
6551 err = got_ferror(f, GOT_ERR_IO);
6555 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6556 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6557 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6560 err = got_ferror(f, GOT_ERR_IO);
6569 static const struct got_error *
6570 histedit_syntax_error(int lineno)
6572 static char msg[42];
6575 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6577 if (ret == -1 || ret >= sizeof(msg))
6578 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6580 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6583 static const struct got_error *
6584 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6585 char *logmsg, struct got_repository *repo)
6587 const struct got_error *err;
6588 struct got_commit_object *folded_commit = NULL;
6589 char *id_str, *folded_logmsg = NULL;
6591 err = got_object_id_str(&id_str, hle->commit_id);
6595 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6599 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6602 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6603 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6604 folded_logmsg) == -1) {
6605 err = got_error_from_errno("asprintf");
6609 got_object_commit_close(folded_commit);
6611 free(folded_logmsg);
6615 static struct got_histedit_list_entry *
6616 get_folded_commits(struct got_histedit_list_entry *hle)
6618 struct got_histedit_list_entry *prev, *folded = NULL;
6620 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6621 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6622 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6623 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6625 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6631 static const struct got_error *
6632 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6633 struct got_repository *repo)
6635 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6636 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6637 const struct got_error *err = NULL;
6638 struct got_commit_object *commit = NULL;
6640 struct got_histedit_list_entry *folded = NULL;
6642 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6646 folded = get_folded_commits(hle);
6648 while (folded != hle) {
6649 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6650 folded = TAILQ_NEXT(folded, entry);
6653 err = append_folded_commit_msg(&new_msg, folded,
6659 folded = TAILQ_NEXT(folded, entry);
6663 err = got_object_id_str(&id_str, hle->commit_id);
6666 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6669 if (asprintf(&new_msg,
6670 "%s\n# original log message of commit %s: %s",
6671 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6672 err = got_error_from_errno("asprintf");
6678 err = got_object_id_str(&id_str, hle->commit_id);
6682 err = got_opentemp_named_fd(&logmsg_path, &fd,
6683 GOT_TMPDIR_STR "/got-logmsg");
6687 dprintf(fd, logmsg);
6690 err = get_editor(&editor);
6694 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6696 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6698 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6701 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6702 err = got_error_from_errno2("unlink", logmsg_path);
6708 got_object_commit_close(commit);
6712 static const struct got_error *
6713 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6714 FILE *f, struct got_repository *repo)
6716 const struct got_error *err = NULL;
6717 char *line = NULL, *p, *end;
6721 const struct got_histedit_cmd *cmd;
6722 struct got_object_id *commit_id = NULL;
6723 struct got_histedit_list_entry *hle = NULL;
6726 len = getline(&line, &size, f);
6728 const struct got_error *getline_err;
6731 getline_err = got_error_from_errno("getline");
6732 err = got_ferror(f, getline_err->code);
6737 while (isspace((unsigned char)p[0]))
6739 if (p[0] == '#' || p[0] == '\0') {
6745 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6746 cmd = &got_histedit_cmds[i];
6747 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6748 isspace((unsigned char)p[strlen(cmd->name)])) {
6749 p += strlen(cmd->name);
6752 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6757 if (i == nitems(got_histedit_cmds)) {
6758 err = histedit_syntax_error(lineno);
6761 while (isspace((unsigned char)p[0]))
6763 if (cmd->code == GOT_HISTEDIT_MESG) {
6764 if (hle == NULL || hle->logmsg != NULL) {
6765 err = got_error(GOT_ERR_HISTEDIT_CMD);
6769 err = histedit_edit_logmsg(hle, repo);
6773 hle->logmsg = strdup(p);
6774 if (hle->logmsg == NULL) {
6775 err = got_error_from_errno("strdup");
6784 while (end[0] && !isspace((unsigned char)end[0]))
6788 err = got_object_resolve_id_str(&commit_id, repo, p);
6790 /* override error code */
6791 err = histedit_syntax_error(lineno);
6795 hle = malloc(sizeof(*hle));
6797 err = got_error_from_errno("malloc");
6801 hle->commit_id = commit_id;
6806 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6814 static const struct got_error *
6815 histedit_check_script(struct got_histedit_list *histedit_cmds,
6816 struct got_object_id_queue *commits, struct got_repository *repo)
6818 const struct got_error *err = NULL;
6819 struct got_object_qid *qid;
6820 struct got_histedit_list_entry *hle;
6821 static char msg[92];
6824 if (TAILQ_EMPTY(histedit_cmds))
6825 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6826 "histedit script contains no commands");
6827 if (SIMPLEQ_EMPTY(commits))
6828 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6830 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6831 struct got_histedit_list_entry *hle2;
6832 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6835 if (got_object_id_cmp(hle->commit_id,
6836 hle2->commit_id) != 0)
6838 err = got_object_id_str(&id_str, hle->commit_id);
6841 snprintf(msg, sizeof(msg), "commit %s is listed "
6842 "more than once in histedit script", id_str);
6844 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6848 SIMPLEQ_FOREACH(qid, commits, entry) {
6849 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6850 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6854 err = got_object_id_str(&id_str, qid->id);
6857 snprintf(msg, sizeof(msg),
6858 "commit %s missing from histedit script", id_str);
6860 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6864 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6865 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6866 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6867 "last commit in histedit script cannot be folded");
6872 static const struct got_error *
6873 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6874 const char *path, struct got_object_id_queue *commits,
6875 struct got_repository *repo)
6877 const struct got_error *err = NULL;
6881 err = get_editor(&editor);
6885 if (spawn_editor(editor, path) == -1) {
6886 err = got_error_from_errno("failed spawning editor");
6890 f = fopen(path, "r");
6892 err = got_error_from_errno("fopen");
6895 err = histedit_parse_list(histedit_cmds, f, repo);
6899 err = histedit_check_script(histedit_cmds, commits, repo);
6901 if (f && fclose(f) != 0 && err == NULL)
6902 err = got_error_from_errno("fclose");
6907 static const struct got_error *
6908 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6909 struct got_object_id_queue *, const char *, const char *,
6910 struct got_repository *);
6912 static const struct got_error *
6913 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6914 struct got_object_id_queue *commits, const char *branch_name,
6915 int edit_logmsg_only, struct got_repository *repo)
6917 const struct got_error *err;
6921 err = got_opentemp_named(&path, &f, "got-histedit");
6925 err = write_cmd_list(f, branch_name, commits);
6929 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6933 if (edit_logmsg_only) {
6935 err = histedit_parse_list(histedit_cmds, f, repo);
6937 if (fclose(f) != 0) {
6938 err = got_error_from_errno("fclose");
6942 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6944 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6945 err->code != GOT_ERR_HISTEDIT_CMD)
6947 err = histedit_edit_list_retry(histedit_cmds, err,
6948 commits, path, branch_name, repo);
6952 if (f && fclose(f) != 0 && err == NULL)
6953 err = got_error_from_errno("fclose");
6954 if (path && unlink(path) != 0 && err == NULL)
6955 err = got_error_from_errno2("unlink", path);
6960 static const struct got_error *
6961 histedit_save_list(struct got_histedit_list *histedit_cmds,
6962 struct got_worktree *worktree, struct got_repository *repo)
6964 const struct got_error *err = NULL;
6967 struct got_histedit_list_entry *hle;
6968 struct got_commit_object *commit = NULL;
6970 err = got_worktree_get_histedit_script_path(&path, worktree);
6974 f = fopen(path, "w");
6976 err = got_error_from_errno2("fopen", path);
6979 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6980 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6986 int n = fprintf(f, "%c %s\n",
6987 GOT_HISTEDIT_MESG, hle->logmsg);
6989 err = got_ferror(f, GOT_ERR_IO);
6995 if (f && fclose(f) != 0 && err == NULL)
6996 err = got_error_from_errno("fclose");
6999 got_object_commit_close(commit);
7004 histedit_free_list(struct got_histedit_list *histedit_cmds)
7006 struct got_histedit_list_entry *hle;
7008 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7009 TAILQ_REMOVE(histedit_cmds, hle, entry);
7014 static const struct got_error *
7015 histedit_load_list(struct got_histedit_list *histedit_cmds,
7016 const char *path, struct got_repository *repo)
7018 const struct got_error *err = NULL;
7021 f = fopen(path, "r");
7023 err = got_error_from_errno2("fopen", path);
7027 err = histedit_parse_list(histedit_cmds, f, repo);
7029 if (f && fclose(f) != 0 && err == NULL)
7030 err = got_error_from_errno("fclose");
7034 static const struct got_error *
7035 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7036 const struct got_error *edit_err, struct got_object_id_queue *commits,
7037 const char *path, const char *branch_name, struct got_repository *repo)
7039 const struct got_error *err = NULL, *prev_err = edit_err;
7042 while (resp != 'c' && resp != 'r' && resp != 'a') {
7043 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7044 "or (a)bort: ", getprogname(), prev_err->msg);
7049 histedit_free_list(histedit_cmds);
7050 err = histedit_run_editor(histedit_cmds, path, commits,
7053 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7054 err->code != GOT_ERR_HISTEDIT_CMD)
7061 } else if (resp == 'r') {
7062 histedit_free_list(histedit_cmds);
7063 err = histedit_edit_script(histedit_cmds,
7064 commits, branch_name, 0, repo);
7066 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7067 err->code != GOT_ERR_HISTEDIT_CMD)
7074 } else if (resp == 'a') {
7075 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7078 printf("invalid response '%c'\n", resp);
7084 static const struct got_error *
7085 histedit_complete(struct got_worktree *worktree,
7086 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7087 struct got_reference *branch, struct got_repository *repo)
7089 printf("Switching work tree to %s\n",
7090 got_ref_get_symref_target(branch));
7091 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7095 static const struct got_error *
7096 show_histedit_progress(struct got_commit_object *commit,
7097 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7099 const struct got_error *err;
7100 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7102 err = got_object_id_str(&old_id_str, hle->commit_id);
7107 err = got_object_id_str(&new_id_str, new_id);
7112 old_id_str[12] = '\0';
7114 new_id_str[12] = '\0';
7117 logmsg = strdup(hle->logmsg);
7118 if (logmsg == NULL) {
7119 err = got_error_from_errno("strdup");
7122 trim_logmsg(logmsg, 42);
7124 err = get_short_logmsg(&logmsg, 42, commit);
7129 switch (hle->cmd->code) {
7130 case GOT_HISTEDIT_PICK:
7131 case GOT_HISTEDIT_EDIT:
7132 printf("%s -> %s: %s\n", old_id_str,
7133 new_id_str ? new_id_str : "no-op change", logmsg);
7135 case GOT_HISTEDIT_DROP:
7136 case GOT_HISTEDIT_FOLD:
7137 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7149 static const struct got_error *
7150 histedit_commit(struct got_pathlist_head *merged_paths,
7151 struct got_worktree *worktree, struct got_fileindex *fileindex,
7152 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7153 struct got_repository *repo)
7155 const struct got_error *err;
7156 struct got_commit_object *commit;
7157 struct got_object_id *new_commit_id;
7159 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7160 && hle->logmsg == NULL) {
7161 err = histedit_edit_logmsg(hle, repo);
7166 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7170 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7171 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7174 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7176 err = show_histedit_progress(commit, hle, NULL);
7178 err = show_histedit_progress(commit, hle, new_commit_id);
7179 free(new_commit_id);
7182 got_object_commit_close(commit);
7186 static const struct got_error *
7187 histedit_skip_commit(struct got_histedit_list_entry *hle,
7188 struct got_worktree *worktree, struct got_repository *repo)
7190 const struct got_error *error;
7191 struct got_commit_object *commit;
7193 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7198 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7202 error = show_histedit_progress(commit, hle, NULL);
7203 got_object_commit_close(commit);
7207 static const struct got_error *
7208 check_local_changes(void *arg, unsigned char status,
7209 unsigned char staged_status, const char *path,
7210 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7211 struct got_object_id *commit_id, int dirfd, const char *de_name)
7213 int *have_local_changes = arg;
7216 case GOT_STATUS_ADD:
7217 case GOT_STATUS_DELETE:
7218 case GOT_STATUS_MODIFY:
7219 case GOT_STATUS_CONFLICT:
7220 *have_local_changes = 1;
7221 return got_error(GOT_ERR_CANCELLED);
7226 switch (staged_status) {
7227 case GOT_STATUS_ADD:
7228 case GOT_STATUS_DELETE:
7229 case GOT_STATUS_MODIFY:
7230 *have_local_changes = 1;
7231 return got_error(GOT_ERR_CANCELLED);
7239 static const struct got_error *
7240 cmd_histedit(int argc, char *argv[])
7242 const struct got_error *error = NULL;
7243 struct got_worktree *worktree = NULL;
7244 struct got_fileindex *fileindex = NULL;
7245 struct got_repository *repo = NULL;
7247 struct got_reference *branch = NULL;
7248 struct got_reference *tmp_branch = NULL;
7249 struct got_object_id *resume_commit_id = NULL;
7250 struct got_object_id *base_commit_id = NULL;
7251 struct got_object_id *head_commit_id = NULL;
7252 struct got_commit_object *commit = NULL;
7253 int ch, rebase_in_progress = 0, did_something;
7254 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7255 int edit_logmsg_only = 0;
7256 const char *edit_script_path = NULL;
7257 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7258 struct got_object_id_queue commits;
7259 struct got_pathlist_head merged_paths;
7260 const struct got_object_id_queue *parent_ids;
7261 struct got_object_qid *pid;
7262 struct got_histedit_list histedit_cmds;
7263 struct got_histedit_list_entry *hle;
7265 SIMPLEQ_INIT(&commits);
7266 TAILQ_INIT(&histedit_cmds);
7267 TAILQ_INIT(&merged_paths);
7269 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7278 edit_script_path = optarg;
7281 edit_logmsg_only = 1;
7293 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7294 "unveil", NULL) == -1)
7297 if (abort_edit && continue_edit)
7298 errx(1, "histedit's -a and -c options are mutually exclusive");
7299 if (edit_script_path && edit_logmsg_only)
7300 errx(1, "histedit's -F and -m options are mutually exclusive");
7301 if (abort_edit && edit_logmsg_only)
7302 errx(1, "histedit's -a and -m options are mutually exclusive");
7303 if (continue_edit && edit_logmsg_only)
7304 errx(1, "histedit's -c and -m options are mutually exclusive");
7309 * This command cannot apply unveil(2) in all cases because the
7310 * user may choose to run an editor to edit the histedit script
7311 * and to edit individual commit log messages.
7312 * unveil(2) traverses exec(2); if an editor is used we have to
7313 * apply unveil after edit script and log messages have been written.
7314 * XXX TODO: Make use of unveil(2) where possible.
7317 cwd = getcwd(NULL, 0);
7319 error = got_error_from_errno("getcwd");
7322 error = got_worktree_open(&worktree, cwd);
7326 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7331 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7334 if (rebase_in_progress) {
7335 error = got_error(GOT_ERR_REBASING);
7339 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7343 if (edit_in_progress && edit_logmsg_only) {
7344 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7345 "histedit operation is in progress in this "
7346 "work tree and must be continued or aborted "
7347 "before the -m option can be used");
7351 if (edit_in_progress && abort_edit) {
7352 error = got_worktree_histedit_continue(&resume_commit_id,
7353 &tmp_branch, &branch, &base_commit_id, &fileindex,
7357 printf("Switching work tree to %s\n",
7358 got_ref_get_symref_target(branch));
7359 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7360 branch, base_commit_id, update_progress, &did_something);
7363 printf("Histedit of %s aborted\n",
7364 got_ref_get_symref_target(branch));
7365 goto done; /* nothing else to do */
7366 } else if (abort_edit) {
7367 error = got_error(GOT_ERR_NOT_HISTEDIT);
7371 if (continue_edit) {
7374 if (!edit_in_progress) {
7375 error = got_error(GOT_ERR_NOT_HISTEDIT);
7379 error = got_worktree_get_histedit_script_path(&path, worktree);
7383 error = histedit_load_list(&histedit_cmds, path, repo);
7388 error = got_worktree_histedit_continue(&resume_commit_id,
7389 &tmp_branch, &branch, &base_commit_id, &fileindex,
7394 error = got_ref_resolve(&head_commit_id, repo, branch);
7398 error = got_object_open_as_commit(&commit, repo,
7402 parent_ids = got_object_commit_get_parent_ids(commit);
7403 pid = SIMPLEQ_FIRST(parent_ids);
7405 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7408 error = collect_commits(&commits, head_commit_id, pid->id,
7409 base_commit_id, got_worktree_get_path_prefix(worktree),
7410 GOT_ERR_HISTEDIT_PATH, repo);
7411 got_object_commit_close(commit);
7416 if (edit_in_progress) {
7417 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7421 error = got_ref_open(&branch, repo,
7422 got_worktree_get_head_ref_name(worktree), 0);
7426 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7427 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7428 "will not edit commit history of a branch outside "
7429 "the \"refs/heads/\" reference namespace");
7433 error = got_ref_resolve(&head_commit_id, repo, branch);
7434 got_ref_close(branch);
7439 error = got_object_open_as_commit(&commit, repo,
7443 parent_ids = got_object_commit_get_parent_ids(commit);
7444 pid = SIMPLEQ_FIRST(parent_ids);
7446 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7449 error = collect_commits(&commits, head_commit_id, pid->id,
7450 got_worktree_get_base_commit_id(worktree),
7451 got_worktree_get_path_prefix(worktree),
7452 GOT_ERR_HISTEDIT_PATH, repo);
7453 got_object_commit_close(commit);
7458 if (SIMPLEQ_EMPTY(&commits)) {
7459 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7463 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7464 &base_commit_id, &fileindex, worktree, repo);
7468 if (edit_script_path) {
7469 error = histedit_load_list(&histedit_cmds,
7470 edit_script_path, repo);
7472 got_worktree_histedit_abort(worktree, fileindex,
7473 repo, branch, base_commit_id,
7474 update_progress, &did_something);
7478 const char *branch_name;
7479 branch_name = got_ref_get_symref_target(branch);
7480 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7482 error = histedit_edit_script(&histedit_cmds, &commits,
7483 branch_name, edit_logmsg_only, repo);
7485 got_worktree_histedit_abort(worktree, fileindex,
7486 repo, branch, base_commit_id,
7487 update_progress, &did_something);
7493 error = histedit_save_list(&histedit_cmds, worktree,
7496 got_worktree_histedit_abort(worktree, fileindex,
7497 repo, branch, base_commit_id,
7498 update_progress, &did_something);
7504 error = histedit_check_script(&histedit_cmds, &commits, repo);
7508 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7509 if (resume_commit_id) {
7510 if (got_object_id_cmp(hle->commit_id,
7511 resume_commit_id) != 0)
7514 resume_commit_id = NULL;
7515 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7516 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7517 error = histedit_skip_commit(hle, worktree,
7522 struct got_pathlist_head paths;
7523 int have_changes = 0;
7526 error = got_pathlist_append(&paths, "", NULL);
7529 error = got_worktree_status(worktree, &paths,
7530 repo, check_local_changes, &have_changes,
7531 check_cancelled, NULL);
7532 got_pathlist_free(&paths);
7534 if (error->code != GOT_ERR_CANCELLED)
7536 if (sigint_received || sigpipe_received)
7540 error = histedit_commit(NULL, worktree,
7541 fileindex, tmp_branch, hle, repo);
7545 error = got_object_open_as_commit(
7546 &commit, repo, hle->commit_id);
7549 error = show_histedit_progress(commit,
7551 got_object_commit_close(commit);
7560 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7561 error = histedit_skip_commit(hle, worktree, repo);
7567 error = got_object_open_as_commit(&commit, repo,
7571 parent_ids = got_object_commit_get_parent_ids(commit);
7572 pid = SIMPLEQ_FIRST(parent_ids);
7574 error = got_worktree_histedit_merge_files(&merged_paths,
7575 worktree, fileindex, pid->id, hle->commit_id, repo,
7576 rebase_progress, &rebase_status, check_cancelled, NULL);
7579 got_object_commit_close(commit);
7582 if (rebase_status == GOT_STATUS_CONFLICT) {
7583 error = show_rebase_merge_conflict(hle->commit_id,
7587 got_worktree_rebase_pathlist_free(&merged_paths);
7591 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7593 error = got_object_id_str(&id_str, hle->commit_id);
7596 printf("Stopping histedit for amending commit %s\n",
7599 got_worktree_rebase_pathlist_free(&merged_paths);
7600 error = got_worktree_histedit_postpone(worktree,
7605 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7606 error = histedit_skip_commit(hle, worktree, repo);
7612 error = histedit_commit(&merged_paths, worktree, fileindex,
7613 tmp_branch, hle, repo);
7614 got_worktree_rebase_pathlist_free(&merged_paths);
7619 if (rebase_status == GOT_STATUS_CONFLICT) {
7620 error = got_worktree_histedit_postpone(worktree, fileindex);
7623 error = got_error_msg(GOT_ERR_CONFLICTS,
7624 "conflicts must be resolved before histedit can continue");
7626 error = histedit_complete(worktree, fileindex, tmp_branch,
7629 got_object_id_queue_free(&commits);
7630 histedit_free_list(&histedit_cmds);
7631 free(head_commit_id);
7632 free(base_commit_id);
7633 free(resume_commit_id);
7635 got_object_commit_close(commit);
7637 got_ref_close(branch);
7639 got_ref_close(tmp_branch);
7641 got_worktree_close(worktree);
7643 got_repo_close(repo);
7648 usage_integrate(void)
7650 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7654 static const struct got_error *
7655 cmd_integrate(int argc, char *argv[])
7657 const struct got_error *error = NULL;
7658 struct got_repository *repo = NULL;
7659 struct got_worktree *worktree = NULL;
7660 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7661 const char *branch_arg = NULL;
7662 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7663 struct got_fileindex *fileindex = NULL;
7664 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7665 int ch, did_something = 0;
7667 while ((ch = getopt(argc, argv, "")) != -1) {
7680 branch_arg = argv[0];
7682 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7683 "unveil", NULL) == -1)
7686 cwd = getcwd(NULL, 0);
7688 error = got_error_from_errno("getcwd");
7692 error = got_worktree_open(&worktree, cwd);
7696 error = check_rebase_or_histedit_in_progress(worktree);
7700 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7705 error = apply_unveil(got_repo_get_path(repo), 0,
7706 got_worktree_get_root_path(worktree));
7710 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7711 error = got_error_from_errno("asprintf");
7715 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7716 &base_branch_ref, worktree, refname, repo);
7720 refname = strdup(got_ref_get_name(branch_ref));
7721 if (refname == NULL) {
7722 error = got_error_from_errno("strdup");
7723 got_worktree_integrate_abort(worktree, fileindex, repo,
7724 branch_ref, base_branch_ref);
7727 base_refname = strdup(got_ref_get_name(base_branch_ref));
7728 if (base_refname == NULL) {
7729 error = got_error_from_errno("strdup");
7730 got_worktree_integrate_abort(worktree, fileindex, repo,
7731 branch_ref, base_branch_ref);
7735 error = got_ref_resolve(&commit_id, repo, branch_ref);
7739 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7743 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7744 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7745 "specified branch has already been integrated");
7746 got_worktree_integrate_abort(worktree, fileindex, repo,
7747 branch_ref, base_branch_ref);
7751 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7753 if (error->code == GOT_ERR_ANCESTRY)
7754 error = got_error(GOT_ERR_REBASE_REQUIRED);
7755 got_worktree_integrate_abort(worktree, fileindex, repo,
7756 branch_ref, base_branch_ref);
7760 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7761 branch_ref, base_branch_ref, update_progress, &did_something,
7762 check_cancelled, NULL);
7766 printf("Integrated %s into %s\n", refname, base_refname);
7769 got_repo_close(repo);
7771 got_worktree_close(worktree);
7773 free(base_commit_id);
7783 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7784 "[file-path ...]\n",
7789 static const struct got_error *
7790 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7791 const char *path, struct got_object_id *blob_id,
7792 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7793 int dirfd, const char *de_name)
7795 const struct got_error *err = NULL;
7796 char *id_str = NULL;
7798 if (staged_status != GOT_STATUS_ADD &&
7799 staged_status != GOT_STATUS_MODIFY &&
7800 staged_status != GOT_STATUS_DELETE)
7803 if (staged_status == GOT_STATUS_ADD ||
7804 staged_status == GOT_STATUS_MODIFY)
7805 err = got_object_id_str(&id_str, staged_blob_id);
7807 err = got_object_id_str(&id_str, blob_id);
7811 printf("%s %c %s\n", id_str, staged_status, path);
7816 static const struct got_error *
7817 cmd_stage(int argc, char *argv[])
7819 const struct got_error *error = NULL;
7820 struct got_repository *repo = NULL;
7821 struct got_worktree *worktree = NULL;
7823 struct got_pathlist_head paths;
7824 struct got_pathlist_entry *pe;
7825 int ch, list_stage = 0, pflag = 0;
7826 FILE *patch_script_file = NULL;
7827 const char *patch_script_path = NULL;
7828 struct choose_patch_arg cpa;
7832 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7841 patch_script_path = optarg;
7853 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7854 "unveil", NULL) == -1)
7857 if (list_stage && (pflag || patch_script_path))
7858 errx(1, "-l option cannot be used with other options");
7859 if (patch_script_path && !pflag)
7860 errx(1, "-F option can only be used together with -p option");
7862 cwd = getcwd(NULL, 0);
7864 error = got_error_from_errno("getcwd");
7868 error = got_worktree_open(&worktree, cwd);
7872 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7877 if (patch_script_path) {
7878 patch_script_file = fopen(patch_script_path, "r");
7879 if (patch_script_file == NULL) {
7880 error = got_error_from_errno2("fopen",
7885 error = apply_unveil(got_repo_get_path(repo), 0,
7886 got_worktree_get_root_path(worktree));
7890 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7895 error = got_worktree_status(worktree, &paths, repo,
7896 print_stage, NULL, check_cancelled, NULL);
7898 cpa.patch_script_file = patch_script_file;
7899 cpa.action = "stage";
7900 error = got_worktree_stage(worktree, &paths,
7901 pflag ? NULL : print_status, NULL,
7902 pflag ? choose_patch : NULL, &cpa, repo);
7905 if (patch_script_file && fclose(patch_script_file) == EOF &&
7907 error = got_error_from_errno2("fclose", patch_script_path);
7909 got_repo_close(repo);
7911 got_worktree_close(worktree);
7912 TAILQ_FOREACH(pe, &paths, entry)
7913 free((char *)pe->path);
7914 got_pathlist_free(&paths);
7922 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7923 "[file-path ...]\n",
7929 static const struct got_error *
7930 cmd_unstage(int argc, char *argv[])
7932 const struct got_error *error = NULL;
7933 struct got_repository *repo = NULL;
7934 struct got_worktree *worktree = NULL;
7936 struct got_pathlist_head paths;
7937 struct got_pathlist_entry *pe;
7938 int ch, did_something = 0, pflag = 0;
7939 FILE *patch_script_file = NULL;
7940 const char *patch_script_path = NULL;
7941 struct choose_patch_arg cpa;
7945 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7951 patch_script_path = optarg;
7963 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7964 "unveil", NULL) == -1)
7967 if (patch_script_path && !pflag)
7968 errx(1, "-F option can only be used together with -p option");
7970 cwd = getcwd(NULL, 0);
7972 error = got_error_from_errno("getcwd");
7976 error = got_worktree_open(&worktree, cwd);
7980 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7985 if (patch_script_path) {
7986 patch_script_file = fopen(patch_script_path, "r");
7987 if (patch_script_file == NULL) {
7988 error = got_error_from_errno2("fopen",
7994 error = apply_unveil(got_repo_get_path(repo), 0,
7995 got_worktree_get_root_path(worktree));
7999 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8003 cpa.patch_script_file = patch_script_file;
8004 cpa.action = "unstage";
8005 error = got_worktree_unstage(worktree, &paths, update_progress,
8006 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8008 if (patch_script_file && fclose(patch_script_file) == EOF &&
8010 error = got_error_from_errno2("fclose", patch_script_path);
8012 got_repo_close(repo);
8014 got_worktree_close(worktree);
8015 TAILQ_FOREACH(pe, &paths, entry)
8016 free((char *)pe->path);
8017 got_pathlist_free(&paths);
8025 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8026 "arg1 [arg2 ...]\n", getprogname());
8030 static const struct got_error *
8031 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8033 const struct got_error *err;
8034 struct got_blob_object *blob;
8036 err = got_object_open_as_blob(&blob, repo, id, 8192);
8040 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8041 got_object_blob_close(blob);
8045 static const struct got_error *
8046 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8048 const struct got_error *err;
8049 struct got_tree_object *tree;
8052 err = got_object_open_as_tree(&tree, repo, id);
8056 nentries = got_object_tree_get_nentries(tree);
8057 for (i = 0; i < nentries; i++) {
8058 struct got_tree_entry *te;
8060 if (sigint_received || sigpipe_received)
8062 te = got_object_tree_get_entry(tree, i);
8063 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8066 fprintf(outfile, "%s %.7o %s\n", id_str,
8067 got_tree_entry_get_mode(te),
8068 got_tree_entry_get_name(te));
8072 got_object_tree_close(tree);
8076 static const struct got_error *
8077 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8079 const struct got_error *err;
8080 struct got_commit_object *commit;
8081 const struct got_object_id_queue *parent_ids;
8082 struct got_object_qid *pid;
8083 char *id_str = NULL;
8084 const char *logmsg = NULL;
8086 err = got_object_open_as_commit(&commit, repo, id);
8090 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8094 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8095 parent_ids = got_object_commit_get_parent_ids(commit);
8096 fprintf(outfile, "numparents %d\n",
8097 got_object_commit_get_nparents(commit));
8098 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8100 err = got_object_id_str(&pid_str, pid->id);
8103 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8106 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8107 got_object_commit_get_author(commit),
8108 got_object_commit_get_author_time(commit));
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8111 got_object_commit_get_author(commit),
8112 got_object_commit_get_committer_time(commit));
8114 logmsg = got_object_commit_get_logmsg_raw(commit);
8115 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8116 fprintf(outfile, "%s", logmsg);
8119 got_object_commit_close(commit);
8123 static const struct got_error *
8124 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8126 const struct got_error *err;
8127 struct got_tag_object *tag;
8128 char *id_str = NULL;
8129 const char *tagmsg = NULL;
8131 err = got_object_open_as_tag(&tag, repo, id);
8135 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8139 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8141 switch (got_object_tag_get_object_type(tag)) {
8142 case GOT_OBJ_TYPE_BLOB:
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8144 GOT_OBJ_LABEL_BLOB);
8146 case GOT_OBJ_TYPE_TREE:
8147 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 GOT_OBJ_LABEL_TREE);
8150 case GOT_OBJ_TYPE_COMMIT:
8151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 GOT_OBJ_LABEL_COMMIT);
8154 case GOT_OBJ_TYPE_TAG:
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8162 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8163 got_object_tag_get_name(tag));
8165 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8166 got_object_tag_get_tagger(tag),
8167 got_object_tag_get_tagger_time(tag));
8169 tagmsg = got_object_tag_get_message(tag);
8170 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8171 fprintf(outfile, "%s", tagmsg);
8174 got_object_tag_close(tag);
8178 static const struct got_error *
8179 cmd_cat(int argc, char *argv[])
8181 const struct got_error *error;
8182 struct got_repository *repo = NULL;
8183 struct got_worktree *worktree = NULL;
8184 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8185 const char *commit_id_str = NULL;
8186 struct got_object_id *id = NULL, *commit_id = NULL;
8187 int ch, obj_type, i, force_path = 0;
8190 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8195 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8198 commit_id_str = optarg;
8201 repo_path = realpath(optarg, NULL);
8202 if (repo_path == NULL)
8203 return got_error_from_errno2("realpath",
8205 got_path_strip_trailing_slashes(repo_path);
8219 cwd = getcwd(NULL, 0);
8221 error = got_error_from_errno("getcwd");
8224 error = got_worktree_open(&worktree, cwd);
8225 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8228 if (repo_path == NULL) {
8230 got_worktree_get_repo_path(worktree));
8231 if (repo_path == NULL) {
8232 error = got_error_from_errno("strdup");
8238 if (repo_path == NULL) {
8239 repo_path = getcwd(NULL, 0);
8240 if (repo_path == NULL)
8241 return got_error_from_errno("getcwd");
8244 error = got_repo_open(&repo, repo_path, NULL);
8249 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8253 if (commit_id_str == NULL)
8254 commit_id_str = GOT_REF_HEAD;
8255 error = got_repo_match_object_id(&commit_id, NULL,
8256 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8260 for (i = 0; i < argc; i++) {
8262 error = got_object_id_by_path(&id, repo, commit_id,
8267 error = got_repo_match_object_id(&id, &label, argv[i],
8268 GOT_OBJ_TYPE_ANY, 0, repo);
8270 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8271 error->code != GOT_ERR_NOT_REF)
8273 error = got_object_id_by_path(&id, repo,
8274 commit_id, argv[i]);
8280 error = got_object_get_type(&obj_type, repo, id);
8285 case GOT_OBJ_TYPE_BLOB:
8286 error = cat_blob(id, repo, stdout);
8288 case GOT_OBJ_TYPE_TREE:
8289 error = cat_tree(id, repo, stdout);
8291 case GOT_OBJ_TYPE_COMMIT:
8292 error = cat_commit(id, repo, stdout);
8294 case GOT_OBJ_TYPE_TAG:
8295 error = cat_tag(id, repo, stdout);
8298 error = got_error(GOT_ERR_OBJ_TYPE);
8313 got_worktree_close(worktree);
8315 const struct got_error *repo_error;
8316 repo_error = got_repo_close(repo);