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 "[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) {
1091 error = got_ref_alloc_symref(&head_symref,
1092 GOT_REF_HEAD, target_ref);
1093 got_ref_close(target_ref);
1098 printf("Setting %s to %s\n", GOT_REF_HEAD,
1099 got_ref_get_symref_target(head_symref));
1101 error = got_ref_write(head_symref, repo);
1106 /* Create a config file git-fetch(1) can understand. */
1107 gitconfig_path = got_repo_get_path_gitconfig(repo);
1108 if (gitconfig_path == NULL) {
1109 error = got_error_from_errno("got_repo_get_path_gitconfig");
1112 gitconfig_file = fopen(gitconfig_path, "a");
1113 if (gitconfig_file == NULL) {
1114 error = got_error_from_errno2("fopen", gitconfig_path);
1117 if (mirror_references) {
1118 if (asprintf(&gitconfig,
1121 "\tfetch = +refs/*:refs/*\n"
1122 "\tmirror = true\n",
1123 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1124 error = got_error_from_errno("asprintf");
1127 } else if (fetch_all_branches) {
1128 if (asprintf(&gitconfig,
1131 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1132 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1133 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1134 error = got_error_from_errno("asprintf");
1138 const char *branchname;
1141 * If the server specified a default branch, use just that one.
1142 * Otherwise fall back to fetching all branches on next fetch.
1145 branchname = got_ref_get_symref_target(head_symref);
1146 if (strncmp(branchname, "refs/heads/", 11) == 0)
1149 branchname = "*"; /* fall back to all branches */
1150 if (asprintf(&gitconfig,
1153 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1154 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1155 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1156 branchname) == -1) {
1157 error = got_error_from_errno("asprintf");
1161 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1162 if (n != strlen(gitconfig)) {
1163 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1169 printf("Created %s repository '%s'\n",
1170 mirror_references ? "mirrored" : "cloned", repo_path);
1172 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1173 error = got_error_from_errno("close");
1174 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1175 error = got_error_from_errno("fclose");
1177 got_repo_close(repo);
1179 got_ref_close(head_symref);
1180 TAILQ_FOREACH(pe, &refs, entry) {
1181 free((void *)pe->path);
1184 got_pathlist_free(&refs);
1185 TAILQ_FOREACH(pe, &symrefs, entry) {
1186 free((void *)pe->path);
1189 got_pathlist_free(&symrefs);
1196 free(default_destdir);
1197 free(gitconfig_path);
1202 static const struct got_error *
1203 create_ref(const char *refname, struct got_object_id *id,
1204 const char *id_str, struct got_repository *repo)
1206 const struct got_error *err = NULL;
1207 struct got_reference *ref;
1209 printf("Creating %s: %s\n", refname, id_str);
1211 err = got_ref_alloc(&ref, refname, id);
1215 err = got_ref_write(ref, repo);
1220 static const struct got_error *
1221 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 char *new_id_str = NULL;
1226 struct got_object_id *old_id = NULL;
1228 err = got_object_id_str(&new_id_str, new_id);
1232 if (got_ref_is_symbolic(ref)) {
1233 struct got_reference *new_ref;
1234 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1237 printf("Deleting symbolic reference %s -> %s\n",
1238 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1239 err = got_ref_delete(ref, repo);
1242 printf("Setting %s to %s\n", got_ref_get_name(ref),
1244 err = got_ref_write(new_ref, repo);
1248 err = got_ref_resolve(&old_id, repo, ref);
1251 if (got_object_id_cmp(old_id, new_id) != 0) {
1252 printf("Setting %s to %s\n",
1253 got_ref_get_name(ref), new_id_str);
1254 err = got_ref_change_ref(ref, new_id);
1257 err = got_ref_write(ref, repo);
1271 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1272 "[remote-repository-name]\n", getprogname());
1276 static const struct got_error *
1277 cmd_fetch(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 char *cwd = NULL, *repo_path = NULL;
1281 const char *remote_name;
1282 char *proto = NULL, *host = NULL, *port = NULL;
1283 char *repo_name = NULL, *server_path = NULL;
1284 struct got_remote_repo *remotes, *remote = NULL;
1286 char *id_str = NULL;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 struct got_pathlist_head refs, symrefs;
1290 struct got_pathlist_entry *pe;
1291 struct got_object_id *pack_hash = NULL;
1292 int i, ch, fetchfd = -1;
1293 struct got_fetch_progress_arg fpa;
1294 int verbosity = 0, fetch_all_branches = 0;
1297 TAILQ_INIT(&symrefs);
1299 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1302 fetch_all_branches = 1;
1305 repo_path = realpath(optarg, NULL);
1306 if (repo_path == NULL)
1307 return got_error_from_errno2("realpath",
1309 got_path_strip_trailing_slashes(repo_path);
1314 else if (verbosity < 3)
1329 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1331 remote_name = argv[0];
1335 cwd = getcwd(NULL, 0);
1337 error = got_error_from_errno("getcwd");
1341 if (repo_path == NULL) {
1342 error = got_worktree_open(&worktree, cwd);
1343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1349 strdup(got_worktree_get_repo_path(worktree));
1350 if (repo_path == NULL)
1351 error = got_error_from_errno("strdup");
1355 repo_path = strdup(cwd);
1356 if (repo_path == NULL) {
1357 error = got_error_from_errno("strdup");
1363 error = got_repo_open(&repo, repo_path, NULL);
1367 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1368 for (i = 0; i < nremotes; i++) {
1369 remote = &remotes[i];
1370 if (strcmp(remote->name, remote_name) == 0)
1373 if (i == nremotes) {
1374 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1378 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1379 &repo_name, remote->url);
1383 if (strcmp(proto, "git") == 0) {
1385 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1386 "sendfd dns inet unveil", NULL) == -1)
1389 } else if (strcmp(proto, "git+ssh") == 0 ||
1390 strcmp(proto, "ssh") == 0) {
1392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1393 "sendfd unveil", NULL) == -1)
1396 } else if (strcmp(proto, "http") == 0 ||
1397 strcmp(proto, "git+http") == 0) {
1398 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1401 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1405 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1406 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1407 error = got_error_from_errno2("unveil",
1408 GOT_FETCH_PATH_SSH);
1412 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1416 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1422 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1423 port ? ":" : "", port ? port : "");
1425 fpa.last_scaled_size[0] = '\0';
1426 fpa.last_p_indexed = -1;
1427 fpa.last_p_resolved = -1;
1428 fpa.verbosity = verbosity;
1429 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1430 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1431 fetch_progress, &fpa);
1435 if (pack_hash == NULL) {
1437 printf("Already up-to-date\n");
1441 if (verbosity >= 0) {
1442 error = got_object_id_str(&id_str, pack_hash);
1445 printf("\nFetched %s.pack\n", id_str);
1450 /* Update references provided with the pack file. */
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 const char *refname = pe->path;
1453 struct got_object_id *id = pe->data;
1454 struct got_reference *ref;
1455 char *remote_refname;
1457 error = got_object_id_str(&id_str, id);
1461 if (remote->mirror_references ||
1462 strncmp("refs/tags/", refname, 10) == 0) {
1463 error = got_ref_open(&ref, repo, refname, 0);
1465 if (error->code != GOT_ERR_NOT_REF)
1467 error = create_ref(refname, id, id_str, repo);
1471 error = update_ref(ref, id, repo);
1476 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1477 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1478 remote_name, refname + 11) == -1) {
1479 error = got_error_from_errno("asprintf");
1483 error = got_ref_open(&ref, repo, remote_refname, 0);
1485 if (error->code != GOT_ERR_NOT_REF)
1487 error = create_ref(remote_refname, id, id_str,
1492 error = update_ref(ref, id, repo);
1498 /* Also create a local branch if none exists yet. */
1499 error = got_ref_open(&ref, repo, refname, 0);
1501 if (error->code != GOT_ERR_NOT_REF)
1503 error = create_ref(refname, id, id_str, repo);
1513 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1514 error = got_error_from_errno("close");
1516 got_repo_close(repo);
1518 got_worktree_close(worktree);
1519 TAILQ_FOREACH(pe, &refs, entry) {
1520 free((void *)pe->path);
1523 got_pathlist_free(&refs);
1524 TAILQ_FOREACH(pe, &symrefs, entry) {
1525 free((void *)pe->path);
1528 got_pathlist_free(&symrefs);
1543 usage_checkout(void)
1545 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1546 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1551 show_worktree_base_ref_warning(void)
1553 fprintf(stderr, "%s: warning: could not create a reference "
1554 "to the work tree's base commit; the commit could be "
1555 "garbage-collected by Git; making the repository "
1556 "writable and running 'got update' will prevent this\n",
1560 struct got_checkout_progress_arg {
1561 const char *worktree_path;
1562 int had_base_commit_ref_error;
1565 static const struct got_error *
1566 checkout_progress(void *arg, unsigned char status, const char *path)
1568 struct got_checkout_progress_arg *a = arg;
1570 /* Base commit bump happens silently. */
1571 if (status == GOT_STATUS_BUMP_BASE)
1574 if (status == GOT_STATUS_BASE_REF_ERR) {
1575 a->had_base_commit_ref_error = 1;
1579 while (path[0] == '/')
1582 printf("%c %s/%s\n", status, a->worktree_path, path);
1586 static const struct got_error *
1587 check_cancelled(void *arg)
1589 if (sigint_received || sigpipe_received)
1590 return got_error(GOT_ERR_CANCELLED);
1594 static const struct got_error *
1595 check_linear_ancestry(struct got_object_id *commit_id,
1596 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1597 struct got_repository *repo)
1599 const struct got_error *err = NULL;
1600 struct got_object_id *yca_id;
1602 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1603 commit_id, base_commit_id, repo, check_cancelled, NULL);
1608 return got_error(GOT_ERR_ANCESTRY);
1611 * Require a straight line of history between the target commit
1612 * and the work tree's base commit.
1614 * Non-linear situations such as this require a rebase:
1616 * (commit) D F (base_commit)
1624 * 'got update' only handles linear cases:
1625 * Update forwards in time: A (base/yca) - B - C - D (commit)
1626 * Update backwards in time: D (base) - C - B - A (commit/yca)
1628 if (allow_forwards_in_time_only) {
1629 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1630 return got_error(GOT_ERR_ANCESTRY);
1631 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1632 got_object_id_cmp(base_commit_id, yca_id) != 0)
1633 return got_error(GOT_ERR_ANCESTRY);
1639 static const struct got_error *
1640 check_same_branch(struct got_object_id *commit_id,
1641 struct got_reference *head_ref, struct got_object_id *yca_id,
1642 struct got_repository *repo)
1644 const struct got_error *err = NULL;
1645 struct got_commit_graph *graph = NULL;
1646 struct got_object_id *head_commit_id = NULL;
1647 int is_same_branch = 0;
1649 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1653 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1657 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1662 err = got_commit_graph_open(&graph, "/", 1);
1666 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1667 check_cancelled, NULL);
1672 struct got_object_id *id;
1673 err = got_commit_graph_iter_next(&id, graph, repo,
1674 check_cancelled, NULL);
1676 if (err->code == GOT_ERR_ITER_COMPLETED)
1682 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1684 if (got_object_id_cmp(id, commit_id) == 0) {
1692 got_commit_graph_close(graph);
1693 free(head_commit_id);
1694 if (!err && !is_same_branch)
1695 err = got_error(GOT_ERR_ANCESTRY);
1699 static const struct got_error *
1700 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1702 static char msg[512];
1703 const char *branch_name;
1705 if (got_ref_is_symbolic(ref))
1706 branch_name = got_ref_get_symref_target(ref);
1708 branch_name = got_ref_get_name(ref);
1710 if (strncmp("refs/heads/", branch_name, 11) == 0)
1713 snprintf(msg, sizeof(msg),
1714 "target commit is not contained in branch '%s'; "
1715 "the branch to use must be specified with -b; "
1716 "if necessary a new branch can be created for "
1717 "this commit with 'got branch -c %s BRANCH_NAME'",
1718 branch_name, commit_id_str);
1720 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1723 static const struct got_error *
1724 cmd_checkout(int argc, char *argv[])
1726 const struct got_error *error = NULL;
1727 struct got_repository *repo = NULL;
1728 struct got_reference *head_ref = NULL;
1729 struct got_worktree *worktree = NULL;
1730 char *repo_path = NULL;
1731 char *worktree_path = NULL;
1732 const char *path_prefix = "";
1733 const char *branch_name = GOT_REF_HEAD;
1734 char *commit_id_str = NULL;
1735 int ch, same_path_prefix, allow_nonempty = 0;
1736 struct got_pathlist_head paths;
1737 struct got_checkout_progress_arg cpa;
1741 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1744 branch_name = optarg;
1747 commit_id_str = strdup(optarg);
1748 if (commit_id_str == NULL)
1749 return got_error_from_errno("strdup");
1755 path_prefix = optarg;
1767 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1768 "unveil", NULL) == -1)
1772 char *cwd, *base, *dotgit;
1773 repo_path = realpath(argv[0], NULL);
1774 if (repo_path == NULL)
1775 return got_error_from_errno2("realpath", argv[0]);
1776 cwd = getcwd(NULL, 0);
1778 error = got_error_from_errno("getcwd");
1781 if (path_prefix[0]) {
1782 base = basename(path_prefix);
1784 error = got_error_from_errno2("basename",
1789 base = basename(repo_path);
1791 error = got_error_from_errno2("basename",
1796 dotgit = strstr(base, ".git");
1799 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1800 error = got_error_from_errno("asprintf");
1805 } else if (argc == 2) {
1806 repo_path = realpath(argv[0], NULL);
1807 if (repo_path == NULL) {
1808 error = got_error_from_errno2("realpath", argv[0]);
1811 worktree_path = realpath(argv[1], NULL);
1812 if (worktree_path == NULL) {
1813 if (errno != ENOENT) {
1814 error = got_error_from_errno2("realpath",
1818 worktree_path = strdup(argv[1]);
1819 if (worktree_path == NULL) {
1820 error = got_error_from_errno("strdup");
1827 got_path_strip_trailing_slashes(repo_path);
1828 got_path_strip_trailing_slashes(worktree_path);
1830 error = got_repo_open(&repo, repo_path, NULL);
1834 /* Pre-create work tree path for unveil(2) */
1835 error = got_path_mkdir(worktree_path);
1837 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1838 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1840 if (!allow_nonempty &&
1841 !got_path_dir_is_empty(worktree_path)) {
1842 error = got_error_path(worktree_path,
1843 GOT_ERR_DIR_NOT_EMPTY);
1848 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1852 error = got_ref_open(&head_ref, repo, branch_name, 0);
1856 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1857 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1860 error = got_worktree_open(&worktree, worktree_path);
1864 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1868 if (!same_path_prefix) {
1869 error = got_error(GOT_ERR_PATH_PREFIX);
1873 if (commit_id_str) {
1874 struct got_object_id *commit_id;
1875 error = got_repo_match_object_id(&commit_id, NULL,
1876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1879 error = check_linear_ancestry(commit_id,
1880 got_worktree_get_base_commit_id(worktree), 0, repo);
1881 if (error != NULL) {
1883 if (error->code == GOT_ERR_ANCESTRY) {
1884 error = checkout_ancestry_error(
1885 head_ref, commit_id_str);
1889 error = check_same_branch(commit_id, head_ref, NULL, repo);
1891 if (error->code == GOT_ERR_ANCESTRY) {
1892 error = checkout_ancestry_error(
1893 head_ref, commit_id_str);
1897 error = got_worktree_set_base_commit_id(worktree, repo,
1904 error = got_pathlist_append(&paths, "", NULL);
1907 cpa.worktree_path = worktree_path;
1908 cpa.had_base_commit_ref_error = 0;
1909 error = got_worktree_checkout_files(worktree, &paths, repo,
1910 checkout_progress, &cpa, check_cancelled, NULL);
1914 printf("Now shut up and hack\n");
1915 if (cpa.had_base_commit_ref_error)
1916 show_worktree_base_ref_warning();
1918 got_pathlist_free(&paths);
1919 free(commit_id_str);
1921 free(worktree_path);
1928 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1933 static const struct got_error *
1934 update_progress(void *arg, unsigned char status, const char *path)
1936 int *did_something = arg;
1938 if (status == GOT_STATUS_EXISTS ||
1939 status == GOT_STATUS_BASE_REF_ERR)
1944 /* Base commit bump happens silently. */
1945 if (status == GOT_STATUS_BUMP_BASE)
1948 while (path[0] == '/')
1950 printf("%c %s\n", status, path);
1954 static const struct got_error *
1955 switch_head_ref(struct got_reference *head_ref,
1956 struct got_object_id *commit_id, struct got_worktree *worktree,
1957 struct got_repository *repo)
1959 const struct got_error *err = NULL;
1961 int ref_has_moved = 0;
1963 /* Trivial case: switching between two different references. */
1964 if (strcmp(got_ref_get_name(head_ref),
1965 got_worktree_get_head_ref_name(worktree)) != 0) {
1966 printf("Switching work tree from %s to %s\n",
1967 got_worktree_get_head_ref_name(worktree),
1968 got_ref_get_name(head_ref));
1969 return got_worktree_set_head_ref(worktree, head_ref);
1972 err = check_linear_ancestry(commit_id,
1973 got_worktree_get_base_commit_id(worktree), 0, repo);
1975 if (err->code != GOT_ERR_ANCESTRY)
1982 /* Switching to a rebased branch with the same reference name. */
1983 err = got_object_id_str(&base_id_str,
1984 got_worktree_get_base_commit_id(worktree));
1987 printf("Reference %s now points at a different branch\n",
1988 got_worktree_get_head_ref_name(worktree));
1989 printf("Switching work tree from %s to %s\n", base_id_str,
1990 got_worktree_get_head_ref_name(worktree));
1994 static const struct got_error *
1995 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1997 const struct got_error *err;
2000 err = got_worktree_rebase_in_progress(&in_progress, worktree);
2004 return got_error(GOT_ERR_REBASING);
2006 err = got_worktree_histedit_in_progress(&in_progress, worktree);
2010 return got_error(GOT_ERR_HISTEDIT_BUSY);
2015 static const struct got_error *
2016 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2017 char *argv[], struct got_worktree *worktree)
2019 const struct got_error *err = NULL;
2026 return got_error_from_errno("strdup");
2027 return got_pathlist_append(paths, path, NULL);
2030 for (i = 0; i < argc; i++) {
2031 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2034 err = got_pathlist_append(paths, path, NULL);
2044 static const struct got_error *
2045 cmd_update(int argc, char *argv[])
2047 const struct got_error *error = NULL;
2048 struct got_repository *repo = NULL;
2049 struct got_worktree *worktree = NULL;
2050 char *worktree_path = NULL;
2051 struct got_object_id *commit_id = NULL;
2052 char *commit_id_str = NULL;
2053 const char *branch_name = NULL;
2054 struct got_reference *head_ref = NULL;
2055 struct got_pathlist_head paths;
2056 struct got_pathlist_entry *pe;
2057 int ch, did_something = 0;
2061 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2064 branch_name = optarg;
2067 commit_id_str = strdup(optarg);
2068 if (commit_id_str == NULL)
2069 return got_error_from_errno("strdup");
2081 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2082 "unveil", NULL) == -1)
2085 worktree_path = getcwd(NULL, 0);
2086 if (worktree_path == NULL) {
2087 error = got_error_from_errno("getcwd");
2090 error = got_worktree_open(&worktree, worktree_path);
2094 error = check_rebase_or_histedit_in_progress(worktree);
2098 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2103 error = apply_unveil(got_repo_get_path(repo), 0,
2104 got_worktree_get_root_path(worktree));
2108 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2112 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2113 got_worktree_get_head_ref_name(worktree), 0);
2116 if (commit_id_str == NULL) {
2117 error = got_ref_resolve(&commit_id, repo, head_ref);
2120 error = got_object_id_str(&commit_id_str, commit_id);
2124 error = got_repo_match_object_id(&commit_id, NULL,
2125 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2126 free(commit_id_str);
2127 commit_id_str = NULL;
2130 error = got_object_id_str(&commit_id_str, commit_id);
2136 struct got_object_id *head_commit_id;
2137 TAILQ_FOREACH(pe, &paths, entry) {
2138 if (pe->path_len == 0)
2140 error = got_error_msg(GOT_ERR_BAD_PATH,
2141 "switching between branches requires that "
2142 "the entire work tree gets updated");
2145 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2148 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2150 free(head_commit_id);
2153 error = check_same_branch(commit_id, head_ref, NULL, repo);
2156 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2160 error = check_linear_ancestry(commit_id,
2161 got_worktree_get_base_commit_id(worktree), 0, repo);
2162 if (error != NULL) {
2163 if (error->code == GOT_ERR_ANCESTRY)
2164 error = got_error(GOT_ERR_BRANCH_MOVED);
2167 error = check_same_branch(commit_id, head_ref, NULL, repo);
2172 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2174 error = got_worktree_set_base_commit_id(worktree, repo,
2180 error = got_worktree_checkout_files(worktree, &paths, repo,
2181 update_progress, &did_something, check_cancelled, NULL);
2186 printf("Updated to commit %s\n", commit_id_str);
2188 printf("Already up-to-date\n");
2190 free(worktree_path);
2191 TAILQ_FOREACH(pe, &paths, entry)
2192 free((char *)pe->path);
2193 got_pathlist_free(&paths);
2195 free(commit_id_str);
2199 static const struct got_error *
2200 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2201 const char *path, int diff_context, int ignore_whitespace,
2202 struct got_repository *repo)
2204 const struct got_error *err = NULL;
2205 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2208 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2213 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2217 while (path[0] == '/')
2219 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2220 ignore_whitespace, stdout);
2223 got_object_blob_close(blob1);
2224 got_object_blob_close(blob2);
2228 static const struct got_error *
2229 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2230 const char *path, int diff_context, int ignore_whitespace,
2231 struct got_repository *repo)
2233 const struct got_error *err = NULL;
2234 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2235 struct got_diff_blob_output_unidiff_arg arg;
2238 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2243 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2247 arg.diff_context = diff_context;
2248 arg.ignore_whitespace = ignore_whitespace;
2249 arg.outfile = stdout;
2250 while (path[0] == '/')
2252 err = got_diff_tree(tree1, tree2, path, path, repo,
2253 got_diff_blob_output_unidiff, &arg, 1);
2256 got_object_tree_close(tree1);
2258 got_object_tree_close(tree2);
2262 static const struct got_error *
2263 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2264 const char *path, int diff_context, struct got_repository *repo)
2266 const struct got_error *err = NULL;
2267 struct got_commit_object *pcommit = NULL;
2268 char *id_str1 = NULL, *id_str2 = NULL;
2269 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2270 struct got_object_qid *qid;
2272 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2274 err = got_object_open_as_commit(&pcommit, repo,
2280 if (path && path[0] != '\0') {
2282 err = got_object_id_by_path(&obj_id2, repo, id, path);
2285 err = got_object_id_str(&id_str2, obj_id2);
2291 err = got_object_id_by_path(&obj_id1, repo,
2297 err = got_object_id_str(&id_str1, obj_id1);
2303 err = got_object_get_type(&obj_type, repo, obj_id2);
2308 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2310 case GOT_OBJ_TYPE_BLOB:
2311 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2314 case GOT_OBJ_TYPE_TREE:
2315 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2319 err = got_error(GOT_ERR_OBJ_TYPE);
2325 obj_id2 = got_object_commit_get_tree_id(commit);
2326 err = got_object_id_str(&id_str2, obj_id2);
2329 obj_id1 = got_object_commit_get_tree_id(pcommit);
2330 err = got_object_id_str(&id_str1, obj_id1);
2333 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2334 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2340 got_object_commit_close(pcommit);
2345 get_datestr(time_t *time, char *datebuf)
2347 struct tm mytm, *tm;
2350 tm = gmtime_r(time, &mytm);
2353 s = asctime_r(tm, datebuf);
2356 p = strchr(s, '\n');
2362 static const struct got_error *
2363 match_logmsg(int *have_match, struct got_object_id *id,
2364 struct got_commit_object *commit, regex_t *regex)
2366 const struct got_error *err = NULL;
2367 regmatch_t regmatch;
2368 char *id_str = NULL, *logmsg = NULL;
2372 err = got_object_id_str(&id_str, id);
2376 err = got_object_commit_get_logmsg(&logmsg, commit);
2380 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2388 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2390 static const struct got_error *
2391 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2392 struct got_repository *repo, const char *path, int show_patch,
2393 int diff_context, struct got_reflist_head *refs)
2395 const struct got_error *err = NULL;
2396 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2398 time_t committer_time;
2399 const char *author, *committer;
2400 char *refs_str = NULL;
2401 struct got_reflist_entry *re;
2403 SIMPLEQ_FOREACH(re, refs, entry) {
2406 struct got_tag_object *tag = NULL;
2409 name = got_ref_get_name(re->ref);
2410 if (strcmp(name, GOT_REF_HEAD) == 0)
2412 if (strncmp(name, "refs/", 5) == 0)
2414 if (strncmp(name, "got/", 4) == 0)
2416 if (strncmp(name, "heads/", 6) == 0)
2418 if (strncmp(name, "remotes/", 8) == 0)
2420 if (strncmp(name, "tags/", 5) == 0) {
2421 err = got_object_open_as_tag(&tag, repo, re->id);
2423 if (err->code != GOT_ERR_OBJ_TYPE)
2425 /* Ref points at something other than a tag. */
2430 cmp = got_object_id_cmp(tag ?
2431 got_object_tag_get_object_id(tag) : re->id, id);
2433 got_object_tag_close(tag);
2437 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2439 err = got_error_from_errno("asprintf");
2445 err = got_object_id_str(&id_str, id);
2449 printf(GOT_COMMIT_SEP_STR);
2450 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2451 refs_str ? refs_str : "", refs_str ? ")" : "");
2456 printf("from: %s\n", got_object_commit_get_author(commit));
2457 committer_time = got_object_commit_get_committer_time(commit);
2458 datestr = get_datestr(&committer_time, datebuf);
2460 printf("date: %s UTC\n", datestr);
2461 author = got_object_commit_get_author(commit);
2462 committer = got_object_commit_get_committer(commit);
2463 if (strcmp(author, committer) != 0)
2464 printf("via: %s\n", committer);
2465 if (got_object_commit_get_nparents(commit) > 1) {
2466 const struct got_object_id_queue *parent_ids;
2467 struct got_object_qid *qid;
2469 parent_ids = got_object_commit_get_parent_ids(commit);
2470 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2471 err = got_object_id_str(&id_str, qid->id);
2474 printf("parent %d: %s\n", n++, id_str);
2479 err = got_object_commit_get_logmsg(&logmsg0, commit);
2485 line = strsep(&logmsg, "\n");
2487 printf(" %s\n", line);
2492 err = print_patch(commit, id, path, diff_context, repo);
2497 if (fflush(stdout) != 0 && err == NULL)
2498 err = got_error_from_errno("fflush");
2502 static const struct got_error *
2503 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2504 const char *path, int show_patch, const char *search_pattern,
2505 int diff_context, int limit, int log_branches,
2506 struct got_reflist_head *refs)
2508 const struct got_error *err;
2509 struct got_commit_graph *graph;
2513 if (search_pattern &&
2514 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2515 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2517 err = got_commit_graph_open(&graph, path, !log_branches);
2520 err = got_commit_graph_iter_start(graph, root_id, repo,
2521 check_cancelled, NULL);
2525 struct got_commit_object *commit;
2526 struct got_object_id *id;
2528 if (sigint_received || sigpipe_received)
2531 err = got_commit_graph_iter_next(&id, graph, repo,
2532 check_cancelled, NULL);
2534 if (err->code == GOT_ERR_ITER_COMPLETED)
2541 err = got_object_open_as_commit(&commit, repo, id);
2545 if (search_pattern) {
2546 err = match_logmsg(&have_match, id, commit, ®ex);
2548 got_object_commit_close(commit);
2551 if (have_match == 0) {
2552 got_object_commit_close(commit);
2557 err = print_commit(commit, id, repo, path, show_patch,
2558 diff_context, refs);
2559 got_object_commit_close(commit);
2560 if (err || (limit && --limit == 0))
2566 got_commit_graph_close(graph);
2573 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2574 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2579 get_default_log_limit(void)
2581 const char *got_default_log_limit;
2585 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2586 if (got_default_log_limit == NULL)
2588 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2594 static const struct got_error *
2595 cmd_log(int argc, char *argv[])
2597 const struct got_error *error;
2598 struct got_repository *repo = NULL;
2599 struct got_worktree *worktree = NULL;
2600 struct got_commit_object *commit = NULL;
2601 struct got_object_id *id = NULL;
2602 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2603 const char *start_commit = NULL, *search_pattern = NULL;
2604 int diff_context = -1, ch;
2605 int show_patch = 0, limit = 0, log_branches = 0;
2607 struct got_reflist_head refs;
2609 SIMPLEQ_INIT(&refs);
2612 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2618 limit = get_default_log_limit();
2620 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2626 start_commit = optarg;
2629 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2632 err(1, "-C option %s", errstr);
2635 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2637 err(1, "-l option %s", errstr);
2643 repo_path = realpath(optarg, NULL);
2644 if (repo_path == NULL)
2645 return got_error_from_errno2("realpath",
2647 got_path_strip_trailing_slashes(repo_path);
2650 search_pattern = optarg;
2661 if (diff_context == -1)
2663 else if (!show_patch)
2664 errx(1, "-C reguires -p");
2666 cwd = getcwd(NULL, 0);
2668 error = got_error_from_errno("getcwd");
2672 error = got_worktree_open(&worktree, cwd);
2673 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2680 error = got_error_from_errno("strdup");
2683 } else if (argc == 1) {
2685 error = got_worktree_resolve_path(&path, worktree,
2690 path = strdup(argv[0]);
2692 error = got_error_from_errno("strdup");
2699 if (repo_path == NULL) {
2700 repo_path = worktree ?
2701 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2703 if (repo_path == NULL) {
2704 error = got_error_from_errno("strdup");
2708 error = got_repo_open(&repo, repo_path, NULL);
2712 error = apply_unveil(got_repo_get_path(repo), 1,
2713 worktree ? got_worktree_get_root_path(worktree) : NULL);
2717 if (start_commit == NULL) {
2718 struct got_reference *head_ref;
2719 error = got_ref_open(&head_ref, repo,
2720 worktree ? got_worktree_get_head_ref_name(worktree)
2724 error = got_ref_resolve(&id, repo, head_ref);
2725 got_ref_close(head_ref);
2728 error = got_object_open_as_commit(&commit, repo, id);
2730 struct got_reference *ref;
2731 error = got_ref_open(&ref, repo, start_commit, 0);
2732 if (error == NULL) {
2734 error = got_ref_resolve(&id, repo, ref);
2738 error = got_object_get_type(&obj_type, repo, id);
2741 if (obj_type == GOT_OBJ_TYPE_TAG) {
2742 struct got_tag_object *tag;
2743 error = got_object_open_as_tag(&tag, repo, id);
2746 if (got_object_tag_get_object_type(tag) !=
2747 GOT_OBJ_TYPE_COMMIT) {
2748 got_object_tag_close(tag);
2749 error = got_error(GOT_ERR_OBJ_TYPE);
2753 id = got_object_id_dup(
2754 got_object_tag_get_object_id(tag));
2756 error = got_error_from_errno(
2757 "got_object_id_dup");
2758 got_object_tag_close(tag);
2761 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2762 error = got_error(GOT_ERR_OBJ_TYPE);
2765 error = got_object_open_as_commit(&commit, repo, id);
2769 if (commit == NULL) {
2770 error = got_repo_match_object_id_prefix(&id,
2771 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2780 const char *prefix = got_worktree_get_path_prefix(worktree);
2782 if (asprintf(&p, "%s%s%s", prefix,
2783 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2784 error = got_error_from_errno("asprintf");
2787 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2790 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2795 path = in_repo_path;
2798 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2802 error = print_commits(id, repo, path, show_patch, search_pattern,
2803 diff_context, limit, log_branches, &refs);
2810 got_worktree_close(worktree);
2812 const struct got_error *repo_error;
2813 repo_error = got_repo_close(repo);
2817 got_ref_list_free(&refs);
2824 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2825 "[-w] [object1 object2 | path]\n", getprogname());
2829 struct print_diff_arg {
2830 struct got_repository *repo;
2831 struct got_worktree *worktree;
2836 int ignore_whitespace;
2839 static const struct got_error *
2840 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2841 const char *path, struct got_object_id *blob_id,
2842 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2843 int dirfd, const char *de_name)
2845 struct print_diff_arg *a = arg;
2846 const struct got_error *err = NULL;
2847 struct got_blob_object *blob1 = NULL;
2850 char *abspath = NULL, *label1 = NULL;
2853 if (a->diff_staged) {
2854 if (staged_status != GOT_STATUS_MODIFY &&
2855 staged_status != GOT_STATUS_ADD &&
2856 staged_status != GOT_STATUS_DELETE)
2859 if (staged_status == GOT_STATUS_DELETE)
2861 if (status == GOT_STATUS_NONEXISTENT)
2862 return got_error_set_errno(ENOENT, path);
2863 if (status != GOT_STATUS_MODIFY &&
2864 status != GOT_STATUS_ADD &&
2865 status != GOT_STATUS_DELETE &&
2866 status != GOT_STATUS_CONFLICT)
2870 if (!a->header_shown) {
2871 printf("diff %s %s%s\n", a->id_str,
2872 got_worktree_get_root_path(a->worktree),
2873 a->diff_staged ? " (staged changes)" : "");
2874 a->header_shown = 1;
2877 if (a->diff_staged) {
2878 const char *label1 = NULL, *label2 = NULL;
2879 switch (staged_status) {
2880 case GOT_STATUS_MODIFY:
2884 case GOT_STATUS_ADD:
2887 case GOT_STATUS_DELETE:
2891 return got_error(GOT_ERR_FILE_STATUS);
2893 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2894 label1, label2, a->diff_context, a->ignore_whitespace,
2898 if (staged_status == GOT_STATUS_ADD ||
2899 staged_status == GOT_STATUS_MODIFY) {
2901 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2905 err = got_object_id_str(&id_str, staged_blob_id);
2908 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2909 err = got_error_from_errno("asprintf");
2914 } else if (status != GOT_STATUS_ADD) {
2915 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2920 if (status != GOT_STATUS_DELETE) {
2921 if (asprintf(&abspath, "%s/%s",
2922 got_worktree_get_root_path(a->worktree), path) == -1) {
2923 err = got_error_from_errno("asprintf");
2928 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2930 err = got_error_from_errno2("openat", abspath);
2934 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2936 err = got_error_from_errno2("open", abspath);
2940 if (fstat(fd, &sb) == -1) {
2941 err = got_error_from_errno2("fstat", abspath);
2944 f2 = fdopen(fd, "r");
2946 err = got_error_from_errno2("fdopen", abspath);
2953 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2954 a->diff_context, a->ignore_whitespace, stdout);
2957 got_object_blob_close(blob1);
2958 if (f2 && fclose(f2) == EOF && err == NULL)
2959 err = got_error_from_errno("fclose");
2960 if (fd != -1 && close(fd) == -1 && err == NULL)
2961 err = got_error_from_errno("close");
2966 static const struct got_error *
2967 cmd_diff(int argc, char *argv[])
2969 const struct got_error *error;
2970 struct got_repository *repo = NULL;
2971 struct got_worktree *worktree = NULL;
2972 char *cwd = NULL, *repo_path = NULL;
2973 struct got_object_id *id1 = NULL, *id2 = NULL;
2974 const char *id_str1 = NULL, *id_str2 = NULL;
2975 char *label1 = NULL, *label2 = NULL;
2977 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2982 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2987 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2990 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2993 err(1, "-C option %s", errstr);
2996 repo_path = realpath(optarg, NULL);
2997 if (repo_path == NULL)
2998 return got_error_from_errno2("realpath",
3000 got_path_strip_trailing_slashes(repo_path);
3006 ignore_whitespace = 1;
3017 cwd = getcwd(NULL, 0);
3019 error = got_error_from_errno("getcwd");
3022 error = got_worktree_open(&worktree, cwd);
3023 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3026 if (worktree == NULL) {
3027 error = got_error(GOT_ERR_NOT_WORKTREE);
3032 "-r option can't be used when diffing a work tree");
3033 repo_path = strdup(got_worktree_get_repo_path(worktree));
3034 if (repo_path == NULL) {
3035 error = got_error_from_errno("strdup");
3039 error = got_worktree_resolve_path(&path, worktree,
3046 error = got_error_from_errno("strdup");
3050 } else if (argc == 2) {
3052 errx(1, "-s option can't be used when diffing "
3053 "objects in repository");
3056 if (worktree && repo_path == NULL) {
3058 strdup(got_worktree_get_repo_path(worktree));
3059 if (repo_path == NULL) {
3060 error = got_error_from_errno("strdup");
3067 if (repo_path == NULL) {
3068 repo_path = getcwd(NULL, 0);
3069 if (repo_path == NULL)
3070 return got_error_from_errno("getcwd");
3073 error = got_repo_open(&repo, repo_path, NULL);
3078 error = apply_unveil(got_repo_get_path(repo), 1,
3079 worktree ? got_worktree_get_root_path(worktree) : NULL);
3084 struct print_diff_arg arg;
3085 struct got_pathlist_head paths;
3090 error = got_object_id_str(&id_str,
3091 got_worktree_get_base_commit_id(worktree));
3095 arg.worktree = worktree;
3096 arg.diff_context = diff_context;
3097 arg.id_str = id_str;
3098 arg.header_shown = 0;
3099 arg.diff_staged = diff_staged;
3100 arg.ignore_whitespace = ignore_whitespace;
3102 error = got_pathlist_append(&paths, path, NULL);
3106 error = got_worktree_status(worktree, &paths, repo, print_diff,
3107 &arg, check_cancelled, NULL);
3109 got_pathlist_free(&paths);
3113 error = got_repo_match_object_id(&id1, &label1, id_str1,
3114 GOT_OBJ_TYPE_ANY, 1, repo);
3118 error = got_repo_match_object_id(&id2, &label2, id_str2,
3119 GOT_OBJ_TYPE_ANY, 1, repo);
3123 error = got_object_get_type(&type1, repo, id1);
3127 error = got_object_get_type(&type2, repo, id2);
3131 if (type1 != type2) {
3132 error = got_error(GOT_ERR_OBJ_TYPE);
3137 case GOT_OBJ_TYPE_BLOB:
3138 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3139 diff_context, ignore_whitespace, repo, stdout);
3141 case GOT_OBJ_TYPE_TREE:
3142 error = got_diff_objects_as_trees(id1, id2, "", "",
3143 diff_context, ignore_whitespace, repo, stdout);
3145 case GOT_OBJ_TYPE_COMMIT:
3146 printf("diff %s %s\n", label1, label2);
3147 error = got_diff_objects_as_commits(id1, id2, diff_context,
3148 ignore_whitespace, repo, stdout);
3151 error = got_error(GOT_ERR_OBJ_TYPE);
3160 got_worktree_close(worktree);
3162 const struct got_error *repo_error;
3163 repo_error = got_repo_close(repo);
3174 "usage: %s blame [-c commit] [-r repository-path] path\n",
3183 char datebuf[11]; /* YYYY-MM-DD + NUL */
3186 struct blame_cb_args {
3187 struct blame_line *lines;
3191 off_t *line_offsets;
3193 struct got_repository *repo;
3196 static const struct got_error *
3197 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3199 const struct got_error *err = NULL;
3200 struct blame_cb_args *a = arg;
3201 struct blame_line *bline;
3203 size_t linesize = 0;
3204 struct got_commit_object *commit = NULL;
3207 time_t committer_time;
3209 if (nlines != a->nlines ||
3210 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3211 return got_error(GOT_ERR_RANGE);
3213 if (sigint_received)
3214 return got_error(GOT_ERR_ITER_COMPLETED);
3217 return NULL; /* no change in this commit */
3219 /* Annotate this line. */
3220 bline = &a->lines[lineno - 1];
3221 if (bline->annotated)
3223 err = got_object_id_str(&bline->id_str, id);
3227 err = got_object_open_as_commit(&commit, a->repo, id);
3231 bline->committer = strdup(got_object_commit_get_committer(commit));
3232 if (bline->committer == NULL) {
3233 err = got_error_from_errno("strdup");
3237 committer_time = got_object_commit_get_committer_time(commit);
3238 if (localtime_r(&committer_time, &tm) == NULL)
3239 return got_error_from_errno("localtime_r");
3240 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3241 &tm) >= sizeof(bline->datebuf)) {
3242 err = got_error(GOT_ERR_NO_SPACE);
3245 bline->annotated = 1;
3247 /* Print lines annotated so far. */
3248 bline = &a->lines[a->lineno_cur - 1];
3249 if (!bline->annotated)
3252 offset = a->line_offsets[a->lineno_cur - 1];
3253 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3254 err = got_error_from_errno("fseeko");
3258 while (bline->annotated) {
3259 char *smallerthan, *at, *nl, *committer;
3262 if (getline(&line, &linesize, a->f) == -1) {
3264 err = got_error_from_errno("getline");
3268 committer = bline->committer;
3269 smallerthan = strchr(committer, '<');
3270 if (smallerthan && smallerthan[1] != '\0')
3271 committer = smallerthan + 1;
3272 at = strchr(committer, '@');
3275 len = strlen(committer);
3277 committer[8] = '\0';
3279 nl = strchr(line, '\n');
3282 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3283 bline->id_str, bline->datebuf, committer, line);
3286 bline = &a->lines[a->lineno_cur - 1];
3290 got_object_commit_close(commit);
3295 static const struct got_error *
3296 cmd_blame(int argc, char *argv[])
3298 const struct got_error *error;
3299 struct got_repository *repo = NULL;
3300 struct got_worktree *worktree = NULL;
3301 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3302 struct got_object_id *obj_id = NULL;
3303 struct got_object_id *commit_id = NULL;
3304 struct got_blob_object *blob = NULL;
3305 char *commit_id_str = NULL;
3306 struct blame_cb_args bca;
3307 int ch, obj_type, i;
3310 memset(&bca, 0, sizeof(bca));
3313 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3318 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3321 commit_id_str = optarg;
3324 repo_path = realpath(optarg, NULL);
3325 if (repo_path == NULL)
3326 return got_error_from_errno2("realpath",
3328 got_path_strip_trailing_slashes(repo_path);
3344 cwd = getcwd(NULL, 0);
3346 error = got_error_from_errno("getcwd");
3349 if (repo_path == NULL) {
3350 error = got_worktree_open(&worktree, cwd);
3351 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3357 strdup(got_worktree_get_repo_path(worktree));
3358 if (repo_path == NULL)
3359 error = got_error_from_errno("strdup");
3363 repo_path = strdup(cwd);
3364 if (repo_path == NULL) {
3365 error = got_error_from_errno("strdup");
3371 error = got_repo_open(&repo, repo_path, NULL);
3375 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3380 const char *prefix = got_worktree_get_path_prefix(worktree);
3381 char *p, *worktree_subdir = cwd +
3382 strlen(got_worktree_get_root_path(worktree));
3383 if (asprintf(&p, "%s%s%s%s%s",
3384 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3385 worktree_subdir, worktree_subdir[0] ? "/" : "",
3387 error = got_error_from_errno("asprintf");
3390 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3393 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3398 if (commit_id_str == NULL) {
3399 struct got_reference *head_ref;
3400 error = got_ref_open(&head_ref, repo, worktree ?
3401 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3404 error = got_ref_resolve(&commit_id, repo, head_ref);
3405 got_ref_close(head_ref);
3409 error = got_repo_match_object_id(&commit_id, NULL,
3410 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3415 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3419 error = got_object_get_type(&obj_type, repo, obj_id);
3423 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3424 error = got_error(GOT_ERR_OBJ_TYPE);
3428 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3431 bca.f = got_opentemp();
3432 if (bca.f == NULL) {
3433 error = got_error_from_errno("got_opentemp");
3436 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3437 &bca.line_offsets, bca.f, blob);
3438 if (error || bca.nlines == 0)
3441 /* Don't include \n at EOF in the blame line count. */
3442 if (bca.line_offsets[bca.nlines - 1] == filesize)
3445 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3446 if (bca.lines == NULL) {
3447 error = got_error_from_errno("calloc");
3451 bca.nlines_prec = 0;
3459 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3460 check_cancelled, NULL);
3468 got_object_blob_close(blob);
3470 got_worktree_close(worktree);
3472 const struct got_error *repo_error;
3473 repo_error = got_repo_close(repo);
3478 for (i = 0; i < bca.nlines; i++) {
3479 struct blame_line *bline = &bca.lines[i];
3480 free(bline->id_str);
3481 free(bline->committer);
3485 free(bca.line_offsets);
3486 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3487 error = got_error_from_errno("fclose");
3495 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3501 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3502 const char *root_path)
3504 int is_root_path = (strcmp(path, root_path) == 0);
3505 const char *modestr = "";
3506 mode_t mode = got_tree_entry_get_mode(te);
3508 path += strlen(root_path);
3509 while (path[0] == '/')
3512 if (got_object_tree_entry_is_submodule(te))
3514 else if (S_ISLNK(mode))
3516 else if (S_ISDIR(mode))
3518 else if (mode & S_IXUSR)
3521 printf("%s%s%s%s%s\n", id ? id : "", path,
3522 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3525 static const struct got_error *
3526 print_tree(const char *path, struct got_object_id *commit_id,
3527 int show_ids, int recurse, const char *root_path,
3528 struct got_repository *repo)
3530 const struct got_error *err = NULL;
3531 struct got_object_id *tree_id = NULL;
3532 struct got_tree_object *tree = NULL;
3535 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3539 err = got_object_open_as_tree(&tree, repo, tree_id);
3542 nentries = got_object_tree_get_nentries(tree);
3543 for (i = 0; i < nentries; i++) {
3544 struct got_tree_entry *te;
3547 if (sigint_received || sigpipe_received)
3550 te = got_object_tree_get_entry(tree, i);
3553 err = got_object_id_str(&id_str,
3554 got_tree_entry_get_id(te));
3557 if (asprintf(&id, "%s ", id_str) == -1) {
3558 err = got_error_from_errno("asprintf");
3564 print_entry(te, id, path, root_path);
3567 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3569 if (asprintf(&child_path, "%s%s%s", path,
3570 path[0] == '/' && path[1] == '\0' ? "" : "/",
3571 got_tree_entry_get_name(te)) == -1) {
3572 err = got_error_from_errno("asprintf");
3575 err = print_tree(child_path, commit_id, show_ids, 1,
3584 got_object_tree_close(tree);
3589 static const struct got_error *
3590 cmd_tree(int argc, char *argv[])
3592 const struct got_error *error;
3593 struct got_repository *repo = NULL;
3594 struct got_worktree *worktree = NULL;
3596 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3597 struct got_object_id *commit_id = NULL;
3598 char *commit_id_str = NULL;
3599 int show_ids = 0, recurse = 0;
3603 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3608 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3611 commit_id_str = optarg;
3614 repo_path = realpath(optarg, NULL);
3615 if (repo_path == NULL)
3616 return got_error_from_errno2("realpath",
3618 got_path_strip_trailing_slashes(repo_path);
3642 cwd = getcwd(NULL, 0);
3644 error = got_error_from_errno("getcwd");
3647 if (repo_path == NULL) {
3648 error = got_worktree_open(&worktree, cwd);
3649 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3655 strdup(got_worktree_get_repo_path(worktree));
3656 if (repo_path == NULL)
3657 error = got_error_from_errno("strdup");
3661 repo_path = strdup(cwd);
3662 if (repo_path == NULL) {
3663 error = got_error_from_errno("strdup");
3669 error = got_repo_open(&repo, repo_path, NULL);
3673 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3679 char *p, *worktree_subdir = cwd +
3680 strlen(got_worktree_get_root_path(worktree));
3681 if (asprintf(&p, "%s/%s",
3682 got_worktree_get_path_prefix(worktree),
3683 worktree_subdir) == -1) {
3684 error = got_error_from_errno("asprintf");
3687 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3694 if (in_repo_path == NULL) {
3695 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3700 if (commit_id_str == NULL) {
3701 struct got_reference *head_ref;
3702 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3705 error = got_ref_resolve(&commit_id, repo, head_ref);
3706 got_ref_close(head_ref);
3710 error = got_repo_match_object_id(&commit_id, NULL,
3711 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3716 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3717 in_repo_path, repo);
3724 got_worktree_close(worktree);
3726 const struct got_error *repo_error;
3727 repo_error = got_repo_close(repo);
3737 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3741 static const struct got_error *
3742 print_status(void *arg, unsigned char status, unsigned char staged_status,
3743 const char *path, struct got_object_id *blob_id,
3744 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3745 int dirfd, const char *de_name)
3747 if (status == staged_status && (status == GOT_STATUS_DELETE))
3748 status = GOT_STATUS_NO_CHANGE;
3749 printf("%c%c %s\n", status, staged_status, path);
3753 static const struct got_error *
3754 cmd_status(int argc, char *argv[])
3756 const struct got_error *error = NULL;
3757 struct got_repository *repo = NULL;
3758 struct got_worktree *worktree = NULL;
3760 struct got_pathlist_head paths;
3761 struct got_pathlist_entry *pe;
3766 while ((ch = getopt(argc, argv, "")) != -1) {
3778 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3782 cwd = getcwd(NULL, 0);
3784 error = got_error_from_errno("getcwd");
3788 error = got_worktree_open(&worktree, cwd);
3792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3797 error = apply_unveil(got_repo_get_path(repo), 1,
3798 got_worktree_get_root_path(worktree));
3802 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3806 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3807 check_cancelled, NULL);
3809 TAILQ_FOREACH(pe, &paths, entry)
3810 free((char *)pe->path);
3811 got_pathlist_free(&paths);
3820 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3825 static const struct got_error *
3826 list_refs(struct got_repository *repo)
3828 static const struct got_error *err = NULL;
3829 struct got_reflist_head refs;
3830 struct got_reflist_entry *re;
3832 SIMPLEQ_INIT(&refs);
3833 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3837 SIMPLEQ_FOREACH(re, &refs, entry) {
3839 refstr = got_ref_to_str(re->ref);
3841 return got_error_from_errno("got_ref_to_str");
3842 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3846 got_ref_list_free(&refs);
3850 static const struct got_error *
3851 delete_ref(struct got_repository *repo, const char *refname)
3853 const struct got_error *err = NULL;
3854 struct got_reference *ref;
3856 err = got_ref_open(&ref, repo, refname, 0);
3860 err = got_ref_delete(ref, repo);
3865 static const struct got_error *
3866 add_ref(struct got_repository *repo, const char *refname, const char *target)
3868 const struct got_error *err = NULL;
3869 struct got_object_id *id;
3870 struct got_reference *ref = NULL;
3873 * Don't let the user create a reference name with a leading '-'.
3874 * While technically a valid reference name, this case is usually
3875 * an unintended typo.
3877 if (refname[0] == '-')
3878 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3880 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3883 struct got_reference *target_ref;
3885 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3887 err = got_ref_open(&target_ref, repo, target, 0);
3890 err = got_ref_resolve(&id, repo, target_ref);
3891 got_ref_close(target_ref);
3896 err = got_ref_alloc(&ref, refname, id);
3900 err = got_ref_write(ref, repo);
3908 static const struct got_error *
3909 add_symref(struct got_repository *repo, const char *refname, const char *target)
3911 const struct got_error *err = NULL;
3912 struct got_reference *ref = NULL;
3913 struct got_reference *target_ref = NULL;
3916 * Don't let the user create a reference name with a leading '-'.
3917 * While technically a valid reference name, this case is usually
3918 * an unintended typo.
3920 if (refname[0] == '-')
3921 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3923 err = got_ref_open(&target_ref, repo, target, 0);
3927 err = got_ref_alloc_symref(&ref, refname, target_ref);
3931 err = got_ref_write(ref, repo);
3934 got_ref_close(target_ref);
3940 static const struct got_error *
3941 cmd_ref(int argc, char *argv[])
3943 const struct got_error *error = NULL;
3944 struct got_repository *repo = NULL;
3945 struct got_worktree *worktree = NULL;
3946 char *cwd = NULL, *repo_path = NULL;
3947 int ch, do_list = 0, create_symref = 0;
3948 const char *delref = NULL;
3950 /* TODO: Add -s option for adding symbolic references. */
3951 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3957 repo_path = realpath(optarg, NULL);
3958 if (repo_path == NULL)
3959 return got_error_from_errno2("realpath",
3961 got_path_strip_trailing_slashes(repo_path);
3975 if (do_list && delref)
3976 errx(1, "-l and -d options are mutually exclusive\n");
3981 if (do_list || delref) {
3983 errx(1, "-s option cannot be used together with the "
3984 "-l or -d options");
3987 } else if (argc != 2)
3992 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3996 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3997 "sendfd unveil", NULL) == -1)
4001 cwd = getcwd(NULL, 0);
4003 error = got_error_from_errno("getcwd");
4007 if (repo_path == NULL) {
4008 error = got_worktree_open(&worktree, cwd);
4009 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4015 strdup(got_worktree_get_repo_path(worktree));
4016 if (repo_path == NULL)
4017 error = got_error_from_errno("strdup");
4021 repo_path = strdup(cwd);
4022 if (repo_path == NULL) {
4023 error = got_error_from_errno("strdup");
4029 error = got_repo_open(&repo, repo_path, NULL);
4033 error = apply_unveil(got_repo_get_path(repo), do_list,
4034 worktree ? got_worktree_get_root_path(worktree) : NULL);
4039 error = list_refs(repo);
4041 error = delete_ref(repo, delref);
4042 else if (create_symref)
4043 error = add_symref(repo, argv[0], argv[1]);
4045 error = add_ref(repo, argv[0], argv[1]);
4048 got_repo_close(repo);
4050 got_worktree_close(worktree);
4060 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4061 "[name]\n", getprogname());
4065 static const struct got_error *
4066 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4067 struct got_reference *ref)
4069 const struct got_error *err = NULL;
4070 const char *refname, *marker = " ";
4073 refname = got_ref_get_name(ref);
4074 if (worktree && strcmp(refname,
4075 got_worktree_get_head_ref_name(worktree)) == 0) {
4076 struct got_object_id *id = NULL;
4078 err = got_ref_resolve(&id, repo, ref);
4081 if (got_object_id_cmp(id,
4082 got_worktree_get_base_commit_id(worktree)) == 0)
4089 if (strncmp(refname, "refs/heads/", 11) == 0)
4091 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4094 refstr = got_ref_to_str(ref);
4096 return got_error_from_errno("got_ref_to_str");
4098 printf("%s%s: %s\n", marker, refname, refstr);
4103 static const struct got_error *
4104 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4106 const char *refname;
4108 if (worktree == NULL)
4109 return got_error(GOT_ERR_NOT_WORKTREE);
4111 refname = got_worktree_get_head_ref_name(worktree);
4113 if (strncmp(refname, "refs/heads/", 11) == 0)
4115 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4118 printf("%s\n", refname);
4123 static const struct got_error *
4124 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4126 static const struct got_error *err = NULL;
4127 struct got_reflist_head refs;
4128 struct got_reflist_entry *re;
4129 struct got_reference *temp_ref = NULL;
4130 int rebase_in_progress, histedit_in_progress;
4132 SIMPLEQ_INIT(&refs);
4135 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4140 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4145 if (rebase_in_progress || histedit_in_progress) {
4146 err = got_ref_open(&temp_ref, repo,
4147 got_worktree_get_head_ref_name(worktree), 0);
4150 list_branch(repo, worktree, temp_ref);
4151 got_ref_close(temp_ref);
4155 err = got_ref_list(&refs, repo, "refs/heads",
4156 got_ref_cmp_by_name, NULL);
4160 SIMPLEQ_FOREACH(re, &refs, entry)
4161 list_branch(repo, worktree, re->ref);
4163 got_ref_list_free(&refs);
4167 static const struct got_error *
4168 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4169 const char *branch_name)
4171 const struct got_error *err = NULL;
4172 struct got_reference *ref = NULL;
4175 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4176 return got_error_from_errno("asprintf");
4178 err = got_ref_open(&ref, repo, refname, 0);
4183 strcmp(got_worktree_get_head_ref_name(worktree),
4184 got_ref_get_name(ref)) == 0) {
4185 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4186 "will not delete this work tree's current branch");
4190 err = got_ref_delete(ref, repo);
4198 static const struct got_error *
4199 add_branch(struct got_repository *repo, const char *branch_name,
4200 struct got_object_id *base_commit_id)
4202 const struct got_error *err = NULL;
4203 struct got_reference *ref = NULL;
4204 char *base_refname = NULL, *refname = NULL;
4207 * Don't let the user create a branch name with a leading '-'.
4208 * While technically a valid reference name, this case is usually
4209 * an unintended typo.
4211 if (branch_name[0] == '-')
4212 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4214 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4215 err = got_error_from_errno("asprintf");
4219 err = got_ref_open(&ref, repo, refname, 0);
4221 err = got_error(GOT_ERR_BRANCH_EXISTS);
4223 } else if (err->code != GOT_ERR_NOT_REF)
4226 err = got_ref_alloc(&ref, refname, base_commit_id);
4230 err = got_ref_write(ref, repo);
4239 static const struct got_error *
4240 cmd_branch(int argc, char *argv[])
4242 const struct got_error *error = NULL;
4243 struct got_repository *repo = NULL;
4244 struct got_worktree *worktree = NULL;
4245 char *cwd = NULL, *repo_path = NULL;
4246 int ch, do_list = 0, do_show = 0, do_update = 1;
4247 const char *delref = NULL, *commit_id_arg = NULL;
4248 struct got_reference *ref = NULL;
4249 struct got_pathlist_head paths;
4250 struct got_pathlist_entry *pe;
4251 struct got_object_id *commit_id = NULL;
4252 char *commit_id_str = NULL;
4256 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4259 commit_id_arg = optarg;
4265 repo_path = realpath(optarg, NULL);
4266 if (repo_path == NULL)
4267 return got_error_from_errno2("realpath",
4269 got_path_strip_trailing_slashes(repo_path);
4283 if (do_list && delref)
4284 errx(1, "-l and -d options are mutually exclusive\n");
4289 if (!do_list && !delref && argc == 0)
4292 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4293 errx(1, "-c option can only be used when creating a branch");
4295 if (do_list || delref) {
4298 } else if (!do_show && argc != 1)
4302 if (do_list || do_show) {
4303 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4307 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4308 "sendfd unveil", NULL) == -1)
4312 cwd = getcwd(NULL, 0);
4314 error = got_error_from_errno("getcwd");
4318 if (repo_path == NULL) {
4319 error = got_worktree_open(&worktree, cwd);
4320 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4326 strdup(got_worktree_get_repo_path(worktree));
4327 if (repo_path == NULL)
4328 error = got_error_from_errno("strdup");
4332 repo_path = strdup(cwd);
4333 if (repo_path == NULL) {
4334 error = got_error_from_errno("strdup");
4340 error = got_repo_open(&repo, repo_path, NULL);
4344 error = apply_unveil(got_repo_get_path(repo), do_list,
4345 worktree ? got_worktree_get_root_path(worktree) : NULL);
4350 error = show_current_branch(repo, worktree);
4352 error = list_branches(repo, worktree);
4354 error = delete_branch(repo, worktree, delref);
4356 if (commit_id_arg == NULL)
4357 commit_id_arg = worktree ?
4358 got_worktree_get_head_ref_name(worktree) :
4360 error = got_repo_match_object_id(&commit_id, NULL,
4361 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4364 error = add_branch(repo, argv[0], commit_id);
4367 if (worktree && do_update) {
4368 int did_something = 0;
4369 char *branch_refname = NULL;
4371 error = got_object_id_str(&commit_id_str, commit_id);
4374 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4378 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4380 error = got_error_from_errno("asprintf");
4383 error = got_ref_open(&ref, repo, branch_refname, 0);
4384 free(branch_refname);
4387 error = switch_head_ref(ref, commit_id, worktree,
4391 error = got_worktree_set_base_commit_id(worktree, repo,
4395 error = got_worktree_checkout_files(worktree, &paths,
4396 repo, update_progress, &did_something,
4397 check_cancelled, NULL);
4401 printf("Updated to commit %s\n", commit_id_str);
4408 got_repo_close(repo);
4410 got_worktree_close(worktree);
4414 free(commit_id_str);
4415 TAILQ_FOREACH(pe, &paths, entry)
4416 free((char *)pe->path);
4417 got_pathlist_free(&paths);
4426 "usage: %s tag [-c commit] [-r repository] [-l] "
4427 "[-m message] name\n", getprogname());
4432 static const struct got_error *
4433 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4435 const struct got_error *err = NULL;
4436 struct got_reflist_entry *re, *se, *new;
4437 struct got_object_id *re_id, *se_id;
4438 struct got_tag_object *re_tag, *se_tag;
4439 time_t re_time, se_time;
4441 SIMPLEQ_FOREACH(re, tags, entry) {
4442 se = SIMPLEQ_FIRST(sorted);
4444 err = got_reflist_entry_dup(&new, re);
4447 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4450 err = got_ref_resolve(&re_id, repo, re->ref);
4453 err = got_object_open_as_tag(&re_tag, repo, re_id);
4457 re_time = got_object_tag_get_tagger_time(re_tag);
4458 got_object_tag_close(re_tag);
4462 err = got_ref_resolve(&se_id, repo, re->ref);
4465 err = got_object_open_as_tag(&se_tag, repo, se_id);
4469 se_time = got_object_tag_get_tagger_time(se_tag);
4470 got_object_tag_close(se_tag);
4472 if (se_time > re_time) {
4473 err = got_reflist_entry_dup(&new, re);
4476 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4479 se = SIMPLEQ_NEXT(se, entry);
4488 static const struct got_error *
4489 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4491 static const struct got_error *err = NULL;
4492 struct got_reflist_head refs;
4493 struct got_reflist_entry *re;
4495 SIMPLEQ_INIT(&refs);
4497 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4501 SIMPLEQ_FOREACH(re, &refs, entry) {
4502 const char *refname;
4503 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4507 struct got_object_id *id;
4508 struct got_tag_object *tag;
4509 struct got_commit_object *commit = NULL;
4511 refname = got_ref_get_name(re->ref);
4512 if (strncmp(refname, "refs/tags/", 10) != 0)
4515 refstr = got_ref_to_str(re->ref);
4516 if (refstr == NULL) {
4517 err = got_error_from_errno("got_ref_to_str");
4520 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4523 err = got_ref_resolve(&id, repo, re->ref);
4526 err = got_object_open_as_tag(&tag, repo, id);
4528 if (err->code != GOT_ERR_OBJ_TYPE) {
4532 /* "lightweight" tag */
4533 err = got_object_open_as_commit(&commit, repo, id);
4538 tagger = got_object_commit_get_committer(commit);
4540 got_object_commit_get_committer_time(commit);
4541 err = got_object_id_str(&id_str, id);
4547 tagger = got_object_tag_get_tagger(tag);
4548 tagger_time = got_object_tag_get_tagger_time(tag);
4549 err = got_object_id_str(&id_str,
4550 got_object_tag_get_object_id(tag));
4554 printf("from: %s\n", tagger);
4555 datestr = get_datestr(&tagger_time, datebuf);
4557 printf("date: %s UTC\n", datestr);
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4561 switch (got_object_tag_get_object_type(tag)) {
4562 case GOT_OBJ_TYPE_BLOB:
4563 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4566 case GOT_OBJ_TYPE_TREE:
4567 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4570 case GOT_OBJ_TYPE_COMMIT:
4571 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4574 case GOT_OBJ_TYPE_TAG:
4575 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4584 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4587 got_object_commit_close(commit);
4589 tagmsg0 = strdup(got_object_tag_get_message(tag));
4590 got_object_tag_close(tag);
4591 if (tagmsg0 == NULL) {
4592 err = got_error_from_errno("strdup");
4599 line = strsep(&tagmsg, "\n");
4601 printf(" %s\n", line);
4606 got_ref_list_free(&refs);
4610 static const struct got_error *
4611 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4612 const char *tag_name, const char *repo_path)
4614 const struct got_error *err = NULL;
4615 char *template = NULL, *initial_content = NULL;
4616 char *editor = NULL;
4619 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4620 err = got_error_from_errno("asprintf");
4624 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4625 commit_id_str, tag_name) == -1) {
4626 err = got_error_from_errno("asprintf");
4630 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4634 dprintf(fd, initial_content);
4637 err = get_editor(&editor);
4640 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4642 free(initial_content);
4646 /* Editor is done; we can now apply unveil(2) */
4648 err = apply_unveil(repo_path, 0, NULL);
4657 static const struct got_error *
4658 add_tag(struct got_repository *repo, const char *tag_name,
4659 const char *commit_arg, const char *tagmsg_arg)
4661 const struct got_error *err = NULL;
4662 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4663 char *label = NULL, *commit_id_str = NULL;
4664 struct got_reference *ref = NULL;
4665 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4666 char *tagmsg_path = NULL, *tag_id_str = NULL;
4667 int preserve_tagmsg = 0;
4670 * Don't let the user create a tag name with a leading '-'.
4671 * While technically a valid reference name, this case is usually
4672 * an unintended typo.
4674 if (tag_name[0] == '-')
4675 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4677 err = get_author(&tagger, repo);
4681 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4682 GOT_OBJ_TYPE_COMMIT, 1, repo);
4686 err = got_object_id_str(&commit_id_str, commit_id);
4690 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4691 refname = strdup(tag_name);
4692 if (refname == NULL) {
4693 err = got_error_from_errno("strdup");
4697 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4698 err = got_error_from_errno("asprintf");
4702 err = got_ref_open(&ref, repo, refname, 0);
4704 err = got_error(GOT_ERR_TAG_EXISTS);
4706 } else if (err->code != GOT_ERR_NOT_REF)
4709 if (tagmsg_arg == NULL) {
4710 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4711 tag_name, got_repo_get_path(repo));
4713 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4714 tagmsg_path != NULL)
4715 preserve_tagmsg = 1;
4720 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4721 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4724 preserve_tagmsg = 1;
4728 err = got_ref_alloc(&ref, refname, tag_id);
4731 preserve_tagmsg = 1;
4735 err = got_ref_write(ref, repo);
4738 preserve_tagmsg = 1;
4742 err = got_object_id_str(&tag_id_str, tag_id);
4745 preserve_tagmsg = 1;
4748 printf("Created tag %s\n", tag_id_str);
4750 if (preserve_tagmsg) {
4751 fprintf(stderr, "%s: tag message preserved in %s\n",
4752 getprogname(), tagmsg_path);
4753 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4754 err = got_error_from_errno2("unlink", tagmsg_path);
4759 free(commit_id_str);
4767 static const struct got_error *
4768 cmd_tag(int argc, char *argv[])
4770 const struct got_error *error = NULL;
4771 struct got_repository *repo = NULL;
4772 struct got_worktree *worktree = NULL;
4773 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4774 char *gitconfig_path = NULL;
4775 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4776 int ch, do_list = 0;
4778 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4781 commit_id_arg = optarg;
4787 repo_path = realpath(optarg, NULL);
4788 if (repo_path == NULL)
4789 return got_error_from_errno2("realpath",
4791 got_path_strip_trailing_slashes(repo_path);
4806 if (commit_id_arg != NULL)
4807 errx(1, "-c option can only be used when creating a tag");
4809 errx(1, "-l and -m options are mutually exclusive");
4812 } else if (argc != 1)
4819 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4823 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4824 "sendfd unveil", NULL) == -1)
4828 cwd = getcwd(NULL, 0);
4830 error = got_error_from_errno("getcwd");
4834 if (repo_path == NULL) {
4835 error = got_worktree_open(&worktree, cwd);
4836 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4842 strdup(got_worktree_get_repo_path(worktree));
4843 if (repo_path == NULL)
4844 error = got_error_from_errno("strdup");
4848 repo_path = strdup(cwd);
4849 if (repo_path == NULL) {
4850 error = got_error_from_errno("strdup");
4857 error = got_repo_open(&repo, repo_path, NULL);
4860 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4863 error = list_tags(repo, worktree);
4865 error = get_gitconfig_path(&gitconfig_path);
4868 error = got_repo_open(&repo, repo_path, gitconfig_path);
4873 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4878 if (commit_id_arg == NULL) {
4879 struct got_reference *head_ref;
4880 struct got_object_id *commit_id;
4881 error = got_ref_open(&head_ref, repo,
4882 worktree ? got_worktree_get_head_ref_name(worktree)
4886 error = got_ref_resolve(&commit_id, repo, head_ref);
4887 got_ref_close(head_ref);
4890 error = got_object_id_str(&commit_id_str, commit_id);
4896 error = add_tag(repo, tag_name,
4897 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4901 got_repo_close(repo);
4903 got_worktree_close(worktree);
4906 free(gitconfig_path);
4907 free(commit_id_str);
4914 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4919 static const struct got_error *
4920 add_progress(void *arg, unsigned char status, const char *path)
4922 while (path[0] == '/')
4924 printf("%c %s\n", status, path);
4928 static const struct got_error *
4929 cmd_add(int argc, char *argv[])
4931 const struct got_error *error = NULL;
4932 struct got_repository *repo = NULL;
4933 struct got_worktree *worktree = NULL;
4935 struct got_pathlist_head paths;
4936 struct got_pathlist_entry *pe;
4937 int ch, can_recurse = 0, no_ignores = 0;
4941 while ((ch = getopt(argc, argv, "IR")) != -1) {
4959 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4966 cwd = getcwd(NULL, 0);
4968 error = got_error_from_errno("getcwd");
4972 error = got_worktree_open(&worktree, cwd);
4976 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4981 error = apply_unveil(got_repo_get_path(repo), 1,
4982 got_worktree_get_root_path(worktree));
4986 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4990 if (!can_recurse && no_ignores) {
4991 error = got_error_msg(GOT_ERR_BAD_PATH,
4992 "disregarding ignores requires -R option");
5000 TAILQ_FOREACH(pe, &paths, entry) {
5001 if (asprintf(&ondisk_path, "%s/%s",
5002 got_worktree_get_root_path(worktree),
5004 error = got_error_from_errno("asprintf");
5007 if (lstat(ondisk_path, &sb) == -1) {
5008 if (errno == ENOENT) {
5012 error = got_error_from_errno2("lstat",
5018 if (S_ISDIR(sb.st_mode)) {
5019 error = got_error_msg(GOT_ERR_BAD_PATH,
5020 "adding directories requires -R option");
5026 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5027 NULL, repo, no_ignores);
5030 got_repo_close(repo);
5032 got_worktree_close(worktree);
5033 TAILQ_FOREACH(pe, &paths, entry)
5034 free((char *)pe->path);
5035 got_pathlist_free(&paths);
5043 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5048 static const struct got_error *
5049 print_remove_status(void *arg, unsigned char status,
5050 unsigned char staged_status, const char *path)
5052 while (path[0] == '/')
5054 if (status == GOT_STATUS_NONEXISTENT)
5056 if (status == staged_status && (status == GOT_STATUS_DELETE))
5057 status = GOT_STATUS_NO_CHANGE;
5058 printf("%c%c %s\n", status, staged_status, path);
5062 static const struct got_error *
5063 cmd_remove(int argc, char *argv[])
5065 const struct got_error *error = NULL;
5066 struct got_worktree *worktree = NULL;
5067 struct got_repository *repo = NULL;
5069 struct got_pathlist_head paths;
5070 struct got_pathlist_entry *pe;
5071 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5075 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5078 delete_local_mods = 1;
5096 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5103 cwd = getcwd(NULL, 0);
5105 error = got_error_from_errno("getcwd");
5108 error = got_worktree_open(&worktree, cwd);
5112 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5117 error = apply_unveil(got_repo_get_path(repo), 1,
5118 got_worktree_get_root_path(worktree));
5122 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5129 TAILQ_FOREACH(pe, &paths, entry) {
5130 if (asprintf(&ondisk_path, "%s/%s",
5131 got_worktree_get_root_path(worktree),
5133 error = got_error_from_errno("asprintf");
5136 if (lstat(ondisk_path, &sb) == -1) {
5137 if (errno == ENOENT) {
5141 error = got_error_from_errno2("lstat",
5147 if (S_ISDIR(sb.st_mode)) {
5148 error = got_error_msg(GOT_ERR_BAD_PATH,
5149 "removing directories requires -R option");
5155 error = got_worktree_schedule_delete(worktree, &paths,
5156 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5159 got_repo_close(repo);
5161 got_worktree_close(worktree);
5162 TAILQ_FOREACH(pe, &paths, entry)
5163 free((char *)pe->path);
5164 got_pathlist_free(&paths);
5172 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5173 "path ...\n", getprogname());
5177 static const struct got_error *
5178 revert_progress(void *arg, unsigned char status, const char *path)
5180 if (status == GOT_STATUS_UNVERSIONED)
5183 while (path[0] == '/')
5185 printf("%c %s\n", status, path);
5189 struct choose_patch_arg {
5190 FILE *patch_script_file;
5194 static const struct got_error *
5195 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5196 int nchanges, const char *action)
5199 size_t linesize = 0;
5203 case GOT_STATUS_ADD:
5204 printf("A %s\n%s this addition? [y/n] ", path, action);
5206 case GOT_STATUS_DELETE:
5207 printf("D %s\n%s this deletion? [y/n] ", path, action);
5209 case GOT_STATUS_MODIFY:
5210 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5211 return got_error_from_errno("fseek");
5212 printf(GOT_COMMIT_SEP_STR);
5213 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5215 if (ferror(patch_file))
5216 return got_error_from_errno("getline");
5217 printf(GOT_COMMIT_SEP_STR);
5218 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5219 path, n, nchanges, action);
5222 return got_error_path(path, GOT_ERR_FILE_STATUS);
5228 static const struct got_error *
5229 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5230 FILE *patch_file, int n, int nchanges)
5232 const struct got_error *err = NULL;
5234 size_t linesize = 0;
5237 struct choose_patch_arg *a = arg;
5239 *choice = GOT_PATCH_CHOICE_NONE;
5241 if (a->patch_script_file) {
5243 err = show_change(status, path, patch_file, n, nchanges,
5247 linelen = getline(&line, &linesize, a->patch_script_file);
5248 if (linelen == -1) {
5249 if (ferror(a->patch_script_file))
5250 return got_error_from_errno("getline");
5253 nl = strchr(line, '\n');
5256 if (strcmp(line, "y") == 0) {
5257 *choice = GOT_PATCH_CHOICE_YES;
5259 } else if (strcmp(line, "n") == 0) {
5260 *choice = GOT_PATCH_CHOICE_NO;
5262 } else if (strcmp(line, "q") == 0 &&
5263 status == GOT_STATUS_MODIFY) {
5264 *choice = GOT_PATCH_CHOICE_QUIT;
5267 printf("invalid response '%s'\n", line);
5272 while (resp != 'y' && resp != 'n' && resp != 'q') {
5273 err = show_change(status, path, patch_file, n, nchanges,
5280 if (status == GOT_STATUS_MODIFY) {
5281 if (resp != 'y' && resp != 'n' && resp != 'q') {
5282 printf("invalid response '%c'\n", resp);
5285 } else if (resp != 'y' && resp != 'n') {
5286 printf("invalid response '%c'\n", resp);
5292 *choice = GOT_PATCH_CHOICE_YES;
5293 else if (resp == 'n')
5294 *choice = GOT_PATCH_CHOICE_NO;
5295 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5296 *choice = GOT_PATCH_CHOICE_QUIT;
5302 static const struct got_error *
5303 cmd_revert(int argc, char *argv[])
5305 const struct got_error *error = NULL;
5306 struct got_worktree *worktree = NULL;
5307 struct got_repository *repo = NULL;
5308 char *cwd = NULL, *path = NULL;
5309 struct got_pathlist_head paths;
5310 struct got_pathlist_entry *pe;
5311 int ch, can_recurse = 0, pflag = 0;
5312 FILE *patch_script_file = NULL;
5313 const char *patch_script_path = NULL;
5314 struct choose_patch_arg cpa;
5318 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5324 patch_script_path = optarg;
5339 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5340 "unveil", NULL) == -1)
5345 if (patch_script_path && !pflag)
5346 errx(1, "-F option can only be used together with -p option");
5348 cwd = getcwd(NULL, 0);
5350 error = got_error_from_errno("getcwd");
5353 error = got_worktree_open(&worktree, cwd);
5357 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5362 if (patch_script_path) {
5363 patch_script_file = fopen(patch_script_path, "r");
5364 if (patch_script_file == NULL) {
5365 error = got_error_from_errno2("fopen",
5370 error = apply_unveil(got_repo_get_path(repo), 1,
5371 got_worktree_get_root_path(worktree));
5375 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5382 TAILQ_FOREACH(pe, &paths, entry) {
5383 if (asprintf(&ondisk_path, "%s/%s",
5384 got_worktree_get_root_path(worktree),
5386 error = got_error_from_errno("asprintf");
5389 if (lstat(ondisk_path, &sb) == -1) {
5390 if (errno == ENOENT) {
5394 error = got_error_from_errno2("lstat",
5400 if (S_ISDIR(sb.st_mode)) {
5401 error = got_error_msg(GOT_ERR_BAD_PATH,
5402 "reverting directories requires -R option");
5408 cpa.patch_script_file = patch_script_file;
5409 cpa.action = "revert";
5410 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5411 pflag ? choose_patch : NULL, &cpa, repo);
5413 if (patch_script_file && fclose(patch_script_file) == EOF &&
5415 error = got_error_from_errno2("fclose", patch_script_path);
5417 got_repo_close(repo);
5419 got_worktree_close(worktree);
5428 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5433 struct collect_commit_logmsg_arg {
5434 const char *cmdline_log;
5436 const char *worktree_path;
5437 const char *branch_name;
5438 const char *repo_path;
5443 static const struct got_error *
5444 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5447 char *initial_content = NULL;
5448 struct got_pathlist_entry *pe;
5449 const struct got_error *err = NULL;
5450 char *template = NULL;
5451 struct collect_commit_logmsg_arg *a = arg;
5455 /* if a message was specified on the command line, just use it */
5456 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5457 len = strlen(a->cmdline_log) + 1;
5458 *logmsg = malloc(len + 1);
5459 if (*logmsg == NULL)
5460 return got_error_from_errno("malloc");
5461 strlcpy(*logmsg, a->cmdline_log, len);
5465 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5466 return got_error_from_errno("asprintf");
5468 if (asprintf(&initial_content,
5469 "\n# changes to be committed on branch %s:\n",
5470 a->branch_name) == -1)
5471 return got_error_from_errno("asprintf");
5473 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5477 dprintf(fd, initial_content);
5479 TAILQ_FOREACH(pe, commitable_paths, entry) {
5480 struct got_commitable *ct = pe->data;
5481 dprintf(fd, "# %c %s\n",
5482 got_commitable_get_status(ct),
5483 got_commitable_get_path(ct));
5487 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5489 free(initial_content);
5492 /* Editor is done; we can now apply unveil(2) */
5494 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5503 static const struct got_error *
5504 cmd_commit(int argc, char *argv[])
5506 const struct got_error *error = NULL;
5507 struct got_worktree *worktree = NULL;
5508 struct got_repository *repo = NULL;
5509 char *cwd = NULL, *id_str = NULL;
5510 struct got_object_id *id = NULL;
5511 const char *logmsg = NULL;
5512 struct collect_commit_logmsg_arg cl_arg;
5513 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5514 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5515 struct got_pathlist_head paths;
5518 cl_arg.logmsg_path = NULL;
5520 while ((ch = getopt(argc, argv, "m:")) != -1) {
5535 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5536 "unveil", NULL) == -1)
5539 cwd = getcwd(NULL, 0);
5541 error = got_error_from_errno("getcwd");
5544 error = got_worktree_open(&worktree, cwd);
5548 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5551 if (rebase_in_progress) {
5552 error = got_error(GOT_ERR_REBASING);
5556 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5561 error = get_gitconfig_path(&gitconfig_path);
5564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5569 error = get_author(&author, repo);
5574 * unveil(2) traverses exec(2); if an editor is used we have
5575 * to apply unveil after the log message has been written.
5577 if (logmsg == NULL || strlen(logmsg) == 0)
5578 error = get_editor(&editor);
5580 error = apply_unveil(got_repo_get_path(repo), 0,
5581 got_worktree_get_root_path(worktree));
5585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5589 cl_arg.editor = editor;
5590 cl_arg.cmdline_log = logmsg;
5591 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5592 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5593 if (!histedit_in_progress) {
5594 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5595 error = got_error(GOT_ERR_COMMIT_BRANCH);
5598 cl_arg.branch_name += 11;
5600 cl_arg.repo_path = got_repo_get_path(repo);
5601 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5602 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5604 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5605 cl_arg.logmsg_path != NULL)
5606 preserve_logmsg = 1;
5610 error = got_object_id_str(&id_str, id);
5613 printf("Created commit %s\n", id_str);
5615 if (preserve_logmsg) {
5616 fprintf(stderr, "%s: log message preserved in %s\n",
5617 getprogname(), cl_arg.logmsg_path);
5618 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5620 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5621 free(cl_arg.logmsg_path);
5623 got_repo_close(repo);
5625 got_worktree_close(worktree);
5628 free(gitconfig_path);
5635 usage_cherrypick(void)
5637 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5641 static const struct got_error *
5642 cmd_cherrypick(int argc, char *argv[])
5644 const struct got_error *error = NULL;
5645 struct got_worktree *worktree = NULL;
5646 struct got_repository *repo = NULL;
5647 char *cwd = NULL, *commit_id_str = NULL;
5648 struct got_object_id *commit_id = NULL;
5649 struct got_commit_object *commit = NULL;
5650 struct got_object_qid *pid;
5651 struct got_reference *head_ref = NULL;
5652 int ch, did_something = 0;
5654 while ((ch = getopt(argc, argv, "")) != -1) {
5666 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5667 "unveil", NULL) == -1)
5673 cwd = getcwd(NULL, 0);
5675 error = got_error_from_errno("getcwd");
5678 error = got_worktree_open(&worktree, cwd);
5682 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5687 error = apply_unveil(got_repo_get_path(repo), 0,
5688 got_worktree_get_root_path(worktree));
5692 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5693 GOT_OBJ_TYPE_COMMIT, repo);
5694 if (error != NULL) {
5695 struct got_reference *ref;
5696 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5698 error = got_ref_open(&ref, repo, argv[0], 0);
5701 error = got_ref_resolve(&commit_id, repo, ref);
5706 error = got_object_id_str(&commit_id_str, commit_id);
5710 error = got_ref_open(&head_ref, repo,
5711 got_worktree_get_head_ref_name(worktree), 0);
5715 error = check_same_branch(commit_id, head_ref, NULL, repo);
5717 if (error->code != GOT_ERR_ANCESTRY)
5721 error = got_error(GOT_ERR_SAME_BRANCH);
5725 error = got_object_open_as_commit(&commit, repo, commit_id);
5728 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5729 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5730 commit_id, repo, update_progress, &did_something, check_cancelled,
5736 printf("Merged commit %s\n", commit_id_str);
5739 got_object_commit_close(commit);
5740 free(commit_id_str);
5742 got_ref_close(head_ref);
5744 got_worktree_close(worktree);
5746 got_repo_close(repo);
5753 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5757 static const struct got_error *
5758 cmd_backout(int argc, char *argv[])
5760 const struct got_error *error = NULL;
5761 struct got_worktree *worktree = NULL;
5762 struct got_repository *repo = NULL;
5763 char *cwd = NULL, *commit_id_str = NULL;
5764 struct got_object_id *commit_id = NULL;
5765 struct got_commit_object *commit = NULL;
5766 struct got_object_qid *pid;
5767 struct got_reference *head_ref = NULL;
5768 int ch, did_something = 0;
5770 while ((ch = getopt(argc, argv, "")) != -1) {
5782 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5783 "unveil", NULL) == -1)
5789 cwd = getcwd(NULL, 0);
5791 error = got_error_from_errno("getcwd");
5794 error = got_worktree_open(&worktree, cwd);
5798 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5803 error = apply_unveil(got_repo_get_path(repo), 0,
5804 got_worktree_get_root_path(worktree));
5808 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5809 GOT_OBJ_TYPE_COMMIT, repo);
5810 if (error != NULL) {
5811 struct got_reference *ref;
5812 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5814 error = got_ref_open(&ref, repo, argv[0], 0);
5817 error = got_ref_resolve(&commit_id, repo, ref);
5822 error = got_object_id_str(&commit_id_str, commit_id);
5826 error = got_ref_open(&head_ref, repo,
5827 got_worktree_get_head_ref_name(worktree), 0);
5831 error = check_same_branch(commit_id, head_ref, NULL, repo);
5835 error = got_object_open_as_commit(&commit, repo, commit_id);
5838 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5840 error = got_error(GOT_ERR_ROOT_COMMIT);
5844 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5845 update_progress, &did_something, check_cancelled, NULL);
5850 printf("Backed out commit %s\n", commit_id_str);
5853 got_object_commit_close(commit);
5854 free(commit_id_str);
5856 got_ref_close(head_ref);
5858 got_worktree_close(worktree);
5860 got_repo_close(repo);
5867 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5873 trim_logmsg(char *logmsg, int limit)
5878 len = strlen(logmsg);
5882 nl = strchr(logmsg, '\n');
5887 static const struct got_error *
5888 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5890 const struct got_error *err;
5891 char *logmsg0 = NULL;
5894 err = got_object_commit_get_logmsg(&logmsg0, commit);
5899 while (isspace((unsigned char)s[0]))
5902 *logmsg = strdup(s);
5903 if (*logmsg == NULL) {
5904 err = got_error_from_errno("strdup");
5908 trim_logmsg(*logmsg, limit);
5914 static const struct got_error *
5915 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5917 const struct got_error *err;
5918 struct got_commit_object *commit = NULL;
5919 char *id_str = NULL, *logmsg = NULL;
5921 err = got_object_open_as_commit(&commit, repo, id);
5925 err = got_object_id_str(&id_str, id);
5931 err = get_short_logmsg(&logmsg, 42, commit);
5935 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5938 got_object_commit_close(commit);
5943 static const struct got_error *
5944 show_rebase_progress(struct got_commit_object *commit,
5945 struct got_object_id *old_id, struct got_object_id *new_id)
5947 const struct got_error *err;
5948 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5950 err = got_object_id_str(&old_id_str, old_id);
5955 err = got_object_id_str(&new_id_str, new_id);
5960 old_id_str[12] = '\0';
5962 new_id_str[12] = '\0';
5964 err = get_short_logmsg(&logmsg, 42, commit);
5968 printf("%s -> %s: %s\n", old_id_str,
5969 new_id_str ? new_id_str : "no-op change", logmsg);
5977 static const struct got_error *
5978 rebase_progress(void *arg, unsigned char status, const char *path)
5980 unsigned char *rebase_status = arg;
5982 while (path[0] == '/')
5984 printf("%c %s\n", status, path);
5986 if (*rebase_status == GOT_STATUS_CONFLICT)
5988 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5989 *rebase_status = status;
5993 static const struct got_error *
5994 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *branch, struct got_reference *new_base_branch,
5996 struct got_reference *tmp_branch, struct got_repository *repo)
5998 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5999 return got_worktree_rebase_complete(worktree, fileindex,
6000 new_base_branch, tmp_branch, branch, repo);
6003 static const struct got_error *
6004 rebase_commit(struct got_pathlist_head *merged_paths,
6005 struct got_worktree *worktree, struct got_fileindex *fileindex,
6006 struct got_reference *tmp_branch,
6007 struct got_object_id *commit_id, struct got_repository *repo)
6009 const struct got_error *error;
6010 struct got_commit_object *commit;
6011 struct got_object_id *new_commit_id;
6013 error = got_object_open_as_commit(&commit, repo, commit_id);
6017 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6018 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6020 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6022 error = show_rebase_progress(commit, commit_id, NULL);
6024 error = show_rebase_progress(commit, commit_id, new_commit_id);
6025 free(new_commit_id);
6028 got_object_commit_close(commit);
6032 struct check_path_prefix_arg {
6033 const char *path_prefix;
6038 static const struct got_error *
6039 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6040 struct got_blob_object *blob2, struct got_object_id *id1,
6041 struct got_object_id *id2, const char *path1, const char *path2,
6042 mode_t mode1, mode_t mode2, struct got_repository *repo)
6044 struct check_path_prefix_arg *a = arg;
6046 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6047 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6048 return got_error(a->errcode);
6053 static const struct got_error *
6054 check_path_prefix(struct got_object_id *parent_id,
6055 struct got_object_id *commit_id, const char *path_prefix,
6056 int errcode, struct got_repository *repo)
6058 const struct got_error *err;
6059 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6060 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6061 struct check_path_prefix_arg cpp_arg;
6063 if (got_path_is_root_dir(path_prefix))
6066 err = got_object_open_as_commit(&commit, repo, commit_id);
6070 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6074 err = got_object_open_as_tree(&tree1, repo,
6075 got_object_commit_get_tree_id(parent_commit));
6079 err = got_object_open_as_tree(&tree2, repo,
6080 got_object_commit_get_tree_id(commit));
6084 cpp_arg.path_prefix = path_prefix;
6085 while (cpp_arg.path_prefix[0] == '/')
6086 cpp_arg.path_prefix++;
6087 cpp_arg.len = strlen(cpp_arg.path_prefix);
6088 cpp_arg.errcode = errcode;
6089 err = got_diff_tree(tree1, tree2, "", "", repo,
6090 check_path_prefix_in_diff, &cpp_arg, 0);
6093 got_object_tree_close(tree1);
6095 got_object_tree_close(tree2);
6097 got_object_commit_close(commit);
6099 got_object_commit_close(parent_commit);
6103 static const struct got_error *
6104 collect_commits(struct got_object_id_queue *commits,
6105 struct got_object_id *initial_commit_id,
6106 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6107 const char *path_prefix, int path_prefix_errcode,
6108 struct got_repository *repo)
6110 const struct got_error *err = NULL;
6111 struct got_commit_graph *graph = NULL;
6112 struct got_object_id *parent_id = NULL;
6113 struct got_object_qid *qid;
6114 struct got_object_id *commit_id = initial_commit_id;
6116 err = got_commit_graph_open(&graph, "/", 1);
6120 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6121 check_cancelled, NULL);
6124 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6125 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6126 check_cancelled, NULL);
6128 if (err->code == GOT_ERR_ITER_COMPLETED) {
6129 err = got_error_msg(GOT_ERR_ANCESTRY,
6130 "ran out of commits to rebase before "
6131 "youngest common ancestor commit has "
6136 err = check_path_prefix(parent_id, commit_id,
6137 path_prefix, path_prefix_errcode, repo);
6141 err = got_object_qid_alloc(&qid, commit_id);
6144 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6145 commit_id = parent_id;
6149 got_commit_graph_close(graph);
6153 static const struct got_error *
6154 cmd_rebase(int argc, char *argv[])
6156 const struct got_error *error = NULL;
6157 struct got_worktree *worktree = NULL;
6158 struct got_repository *repo = NULL;
6159 struct got_fileindex *fileindex = NULL;
6161 struct got_reference *branch = NULL;
6162 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6163 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6164 struct got_object_id *resume_commit_id = NULL;
6165 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6166 struct got_commit_object *commit = NULL;
6167 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6168 int histedit_in_progress = 0;
6169 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6170 struct got_object_id_queue commits;
6171 struct got_pathlist_head merged_paths;
6172 const struct got_object_id_queue *parent_ids;
6173 struct got_object_qid *qid, *pid;
6175 SIMPLEQ_INIT(&commits);
6176 TAILQ_INIT(&merged_paths);
6178 while ((ch = getopt(argc, argv, "ac")) != -1) {
6184 continue_rebase = 1;
6196 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6197 "unveil", NULL) == -1)
6200 if (abort_rebase && continue_rebase)
6202 else if (abort_rebase || continue_rebase) {
6205 } else if (argc != 1)
6208 cwd = getcwd(NULL, 0);
6210 error = got_error_from_errno("getcwd");
6213 error = got_worktree_open(&worktree, cwd);
6217 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6222 error = apply_unveil(got_repo_get_path(repo), 0,
6223 got_worktree_get_root_path(worktree));
6227 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6231 if (histedit_in_progress) {
6232 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6236 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6242 if (!rebase_in_progress) {
6243 error = got_error(GOT_ERR_NOT_REBASING);
6246 error = got_worktree_rebase_continue(&resume_commit_id,
6247 &new_base_branch, &tmp_branch, &branch, &fileindex,
6251 printf("Switching work tree to %s\n",
6252 got_ref_get_symref_target(new_base_branch));
6253 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6254 new_base_branch, update_progress, &did_something);
6257 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6258 goto done; /* nothing else to do */
6261 if (continue_rebase) {
6262 if (!rebase_in_progress) {
6263 error = got_error(GOT_ERR_NOT_REBASING);
6266 error = got_worktree_rebase_continue(&resume_commit_id,
6267 &new_base_branch, &tmp_branch, &branch, &fileindex,
6272 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6273 resume_commit_id, repo);
6277 yca_id = got_object_id_dup(resume_commit_id);
6278 if (yca_id == NULL) {
6279 error = got_error_from_errno("got_object_id_dup");
6283 error = got_ref_open(&branch, repo, argv[0], 0);
6288 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6292 if (!continue_rebase) {
6293 struct got_object_id *base_commit_id;
6295 base_commit_id = got_worktree_get_base_commit_id(worktree);
6296 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6297 base_commit_id, branch_head_commit_id, repo,
6298 check_cancelled, NULL);
6301 if (yca_id == NULL) {
6302 error = got_error_msg(GOT_ERR_ANCESTRY,
6303 "specified branch shares no common ancestry "
6304 "with work tree's branch");
6308 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6310 if (error->code != GOT_ERR_ANCESTRY)
6314 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6315 "specified branch resolves to a commit which "
6316 "is already contained in work tree's branch");
6319 error = got_worktree_rebase_prepare(&new_base_branch,
6320 &tmp_branch, &fileindex, worktree, branch, repo);
6325 commit_id = branch_head_commit_id;
6326 error = got_object_open_as_commit(&commit, repo, commit_id);
6330 parent_ids = got_object_commit_get_parent_ids(commit);
6331 pid = SIMPLEQ_FIRST(parent_ids);
6333 if (!continue_rebase) {
6335 error = got_worktree_rebase_abort(worktree, fileindex,
6336 repo, new_base_branch, update_progress,
6340 printf("Rebase of %s aborted\n",
6341 got_ref_get_name(branch));
6343 error = got_error(GOT_ERR_EMPTY_REBASE);
6346 error = collect_commits(&commits, commit_id, pid->id,
6347 yca_id, got_worktree_get_path_prefix(worktree),
6348 GOT_ERR_REBASE_PATH, repo);
6349 got_object_commit_close(commit);
6354 if (SIMPLEQ_EMPTY(&commits)) {
6355 if (continue_rebase) {
6356 error = rebase_complete(worktree, fileindex,
6357 branch, new_base_branch, tmp_branch, repo);
6360 /* Fast-forward the reference of the branch. */
6361 struct got_object_id *new_head_commit_id;
6363 error = got_ref_resolve(&new_head_commit_id, repo,
6367 error = got_object_id_str(&id_str, new_head_commit_id);
6368 printf("Forwarding %s to commit %s\n",
6369 got_ref_get_name(branch), id_str);
6371 error = got_ref_change_ref(branch,
6372 new_head_commit_id);
6379 SIMPLEQ_FOREACH(qid, &commits, entry) {
6380 commit_id = qid->id;
6381 parent_id = pid ? pid->id : yca_id;
6384 error = got_worktree_rebase_merge_files(&merged_paths,
6385 worktree, fileindex, parent_id, commit_id, repo,
6386 rebase_progress, &rebase_status, check_cancelled, NULL);
6390 if (rebase_status == GOT_STATUS_CONFLICT) {
6391 error = show_rebase_merge_conflict(qid->id, repo);
6394 got_worktree_rebase_pathlist_free(&merged_paths);
6398 error = rebase_commit(&merged_paths, worktree, fileindex,
6399 tmp_branch, commit_id, repo);
6400 got_worktree_rebase_pathlist_free(&merged_paths);
6405 if (rebase_status == GOT_STATUS_CONFLICT) {
6406 error = got_worktree_rebase_postpone(worktree, fileindex);
6409 error = got_error_msg(GOT_ERR_CONFLICTS,
6410 "conflicts must be resolved before rebasing can continue");
6412 error = rebase_complete(worktree, fileindex, branch,
6413 new_base_branch, tmp_branch, repo);
6415 got_object_id_queue_free(&commits);
6416 free(branch_head_commit_id);
6417 free(resume_commit_id);
6420 got_object_commit_close(commit);
6422 got_ref_close(branch);
6423 if (new_base_branch)
6424 got_ref_close(new_base_branch);
6426 got_ref_close(tmp_branch);
6428 got_worktree_close(worktree);
6430 got_repo_close(repo);
6435 usage_histedit(void)
6437 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6442 #define GOT_HISTEDIT_PICK 'p'
6443 #define GOT_HISTEDIT_EDIT 'e'
6444 #define GOT_HISTEDIT_FOLD 'f'
6445 #define GOT_HISTEDIT_DROP 'd'
6446 #define GOT_HISTEDIT_MESG 'm'
6448 static struct got_histedit_cmd {
6452 } got_histedit_cmds[] = {
6453 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6454 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6455 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6457 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6458 { GOT_HISTEDIT_MESG, "mesg",
6459 "single-line log message for commit above (open editor if empty)" },
6462 struct got_histedit_list_entry {
6463 TAILQ_ENTRY(got_histedit_list_entry) entry;
6464 struct got_object_id *commit_id;
6465 const struct got_histedit_cmd *cmd;
6468 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6470 static const struct got_error *
6471 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6472 FILE *f, struct got_repository *repo)
6474 const struct got_error *err = NULL;
6475 char *logmsg = NULL, *id_str = NULL;
6476 struct got_commit_object *commit = NULL;
6479 err = got_object_open_as_commit(&commit, repo, commit_id);
6483 err = get_short_logmsg(&logmsg, 34, commit);
6487 err = got_object_id_str(&id_str, commit_id);
6491 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6493 err = got_ferror(f, GOT_ERR_IO);
6496 got_object_commit_close(commit);
6502 static const struct got_error *
6503 histedit_write_commit_list(struct got_object_id_queue *commits,
6504 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6506 const struct got_error *err = NULL;
6507 struct got_object_qid *qid;
6509 if (SIMPLEQ_EMPTY(commits))
6510 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6512 SIMPLEQ_FOREACH(qid, commits, entry) {
6513 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6517 if (edit_logmsg_only) {
6518 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6520 err = got_ferror(f, GOT_ERR_IO);
6529 static const struct got_error *
6530 write_cmd_list(FILE *f, const char *branch_name,
6531 struct got_object_id_queue *commits)
6533 const struct got_error *err = NULL;
6536 struct got_object_qid *qid;
6538 qid = SIMPLEQ_FIRST(commits);
6539 err = got_object_id_str(&id_str, qid->id);
6544 "# Editing the history of branch '%s' starting at\n"
6546 "# Commits will be processed in order from top to "
6547 "bottom of this file.\n", branch_name, id_str);
6549 err = got_ferror(f, GOT_ERR_IO);
6553 n = fprintf(f, "# Available histedit commands:\n");
6555 err = got_ferror(f, GOT_ERR_IO);
6559 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6560 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6561 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6564 err = got_ferror(f, GOT_ERR_IO);
6573 static const struct got_error *
6574 histedit_syntax_error(int lineno)
6576 static char msg[42];
6579 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6581 if (ret == -1 || ret >= sizeof(msg))
6582 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6584 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6587 static const struct got_error *
6588 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6589 char *logmsg, struct got_repository *repo)
6591 const struct got_error *err;
6592 struct got_commit_object *folded_commit = NULL;
6593 char *id_str, *folded_logmsg = NULL;
6595 err = got_object_id_str(&id_str, hle->commit_id);
6599 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6603 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6606 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6607 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6608 folded_logmsg) == -1) {
6609 err = got_error_from_errno("asprintf");
6613 got_object_commit_close(folded_commit);
6615 free(folded_logmsg);
6619 static struct got_histedit_list_entry *
6620 get_folded_commits(struct got_histedit_list_entry *hle)
6622 struct got_histedit_list_entry *prev, *folded = NULL;
6624 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6625 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6626 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6627 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6629 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6635 static const struct got_error *
6636 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6637 struct got_repository *repo)
6639 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6640 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6641 const struct got_error *err = NULL;
6642 struct got_commit_object *commit = NULL;
6644 struct got_histedit_list_entry *folded = NULL;
6646 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6650 folded = get_folded_commits(hle);
6652 while (folded != hle) {
6653 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6654 folded = TAILQ_NEXT(folded, entry);
6657 err = append_folded_commit_msg(&new_msg, folded,
6663 folded = TAILQ_NEXT(folded, entry);
6667 err = got_object_id_str(&id_str, hle->commit_id);
6670 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6673 if (asprintf(&new_msg,
6674 "%s\n# original log message of commit %s: %s",
6675 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6676 err = got_error_from_errno("asprintf");
6682 err = got_object_id_str(&id_str, hle->commit_id);
6686 err = got_opentemp_named_fd(&logmsg_path, &fd,
6687 GOT_TMPDIR_STR "/got-logmsg");
6691 dprintf(fd, logmsg);
6694 err = get_editor(&editor);
6698 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6700 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6702 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6705 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6706 err = got_error_from_errno2("unlink", logmsg_path);
6712 got_object_commit_close(commit);
6716 static const struct got_error *
6717 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6718 FILE *f, struct got_repository *repo)
6720 const struct got_error *err = NULL;
6721 char *line = NULL, *p, *end;
6725 const struct got_histedit_cmd *cmd;
6726 struct got_object_id *commit_id = NULL;
6727 struct got_histedit_list_entry *hle = NULL;
6730 len = getline(&line, &size, f);
6732 const struct got_error *getline_err;
6735 getline_err = got_error_from_errno("getline");
6736 err = got_ferror(f, getline_err->code);
6741 while (isspace((unsigned char)p[0]))
6743 if (p[0] == '#' || p[0] == '\0') {
6749 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6750 cmd = &got_histedit_cmds[i];
6751 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6752 isspace((unsigned char)p[strlen(cmd->name)])) {
6753 p += strlen(cmd->name);
6756 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6761 if (i == nitems(got_histedit_cmds)) {
6762 err = histedit_syntax_error(lineno);
6765 while (isspace((unsigned char)p[0]))
6767 if (cmd->code == GOT_HISTEDIT_MESG) {
6768 if (hle == NULL || hle->logmsg != NULL) {
6769 err = got_error(GOT_ERR_HISTEDIT_CMD);
6773 err = histedit_edit_logmsg(hle, repo);
6777 hle->logmsg = strdup(p);
6778 if (hle->logmsg == NULL) {
6779 err = got_error_from_errno("strdup");
6788 while (end[0] && !isspace((unsigned char)end[0]))
6792 err = got_object_resolve_id_str(&commit_id, repo, p);
6794 /* override error code */
6795 err = histedit_syntax_error(lineno);
6799 hle = malloc(sizeof(*hle));
6801 err = got_error_from_errno("malloc");
6805 hle->commit_id = commit_id;
6810 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6818 static const struct got_error *
6819 histedit_check_script(struct got_histedit_list *histedit_cmds,
6820 struct got_object_id_queue *commits, struct got_repository *repo)
6822 const struct got_error *err = NULL;
6823 struct got_object_qid *qid;
6824 struct got_histedit_list_entry *hle;
6825 static char msg[92];
6828 if (TAILQ_EMPTY(histedit_cmds))
6829 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6830 "histedit script contains no commands");
6831 if (SIMPLEQ_EMPTY(commits))
6832 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6834 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6835 struct got_histedit_list_entry *hle2;
6836 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6839 if (got_object_id_cmp(hle->commit_id,
6840 hle2->commit_id) != 0)
6842 err = got_object_id_str(&id_str, hle->commit_id);
6845 snprintf(msg, sizeof(msg), "commit %s is listed "
6846 "more than once in histedit script", id_str);
6848 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6852 SIMPLEQ_FOREACH(qid, commits, entry) {
6853 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6854 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6858 err = got_object_id_str(&id_str, qid->id);
6861 snprintf(msg, sizeof(msg),
6862 "commit %s missing from histedit script", id_str);
6864 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6868 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6869 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6870 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6871 "last commit in histedit script cannot be folded");
6876 static const struct got_error *
6877 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6878 const char *path, struct got_object_id_queue *commits,
6879 struct got_repository *repo)
6881 const struct got_error *err = NULL;
6885 err = get_editor(&editor);
6889 if (spawn_editor(editor, path) == -1) {
6890 err = got_error_from_errno("failed spawning editor");
6894 f = fopen(path, "r");
6896 err = got_error_from_errno("fopen");
6899 err = histedit_parse_list(histedit_cmds, f, repo);
6903 err = histedit_check_script(histedit_cmds, commits, repo);
6905 if (f && fclose(f) != 0 && err == NULL)
6906 err = got_error_from_errno("fclose");
6911 static const struct got_error *
6912 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6913 struct got_object_id_queue *, const char *, const char *,
6914 struct got_repository *);
6916 static const struct got_error *
6917 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6918 struct got_object_id_queue *commits, const char *branch_name,
6919 int edit_logmsg_only, struct got_repository *repo)
6921 const struct got_error *err;
6925 err = got_opentemp_named(&path, &f, "got-histedit");
6929 err = write_cmd_list(f, branch_name, commits);
6933 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6937 if (edit_logmsg_only) {
6939 err = histedit_parse_list(histedit_cmds, f, repo);
6941 if (fclose(f) != 0) {
6942 err = got_error_from_errno("fclose");
6946 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6948 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6949 err->code != GOT_ERR_HISTEDIT_CMD)
6951 err = histedit_edit_list_retry(histedit_cmds, err,
6952 commits, path, branch_name, repo);
6956 if (f && fclose(f) != 0 && err == NULL)
6957 err = got_error_from_errno("fclose");
6958 if (path && unlink(path) != 0 && err == NULL)
6959 err = got_error_from_errno2("unlink", path);
6964 static const struct got_error *
6965 histedit_save_list(struct got_histedit_list *histedit_cmds,
6966 struct got_worktree *worktree, struct got_repository *repo)
6968 const struct got_error *err = NULL;
6971 struct got_histedit_list_entry *hle;
6972 struct got_commit_object *commit = NULL;
6974 err = got_worktree_get_histedit_script_path(&path, worktree);
6978 f = fopen(path, "w");
6980 err = got_error_from_errno2("fopen", path);
6983 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6984 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6990 int n = fprintf(f, "%c %s\n",
6991 GOT_HISTEDIT_MESG, hle->logmsg);
6993 err = got_ferror(f, GOT_ERR_IO);
6999 if (f && fclose(f) != 0 && err == NULL)
7000 err = got_error_from_errno("fclose");
7003 got_object_commit_close(commit);
7008 histedit_free_list(struct got_histedit_list *histedit_cmds)
7010 struct got_histedit_list_entry *hle;
7012 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7013 TAILQ_REMOVE(histedit_cmds, hle, entry);
7018 static const struct got_error *
7019 histedit_load_list(struct got_histedit_list *histedit_cmds,
7020 const char *path, struct got_repository *repo)
7022 const struct got_error *err = NULL;
7025 f = fopen(path, "r");
7027 err = got_error_from_errno2("fopen", path);
7031 err = histedit_parse_list(histedit_cmds, f, repo);
7033 if (f && fclose(f) != 0 && err == NULL)
7034 err = got_error_from_errno("fclose");
7038 static const struct got_error *
7039 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7040 const struct got_error *edit_err, struct got_object_id_queue *commits,
7041 const char *path, const char *branch_name, struct got_repository *repo)
7043 const struct got_error *err = NULL, *prev_err = edit_err;
7046 while (resp != 'c' && resp != 'r' && resp != 'a') {
7047 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7048 "or (a)bort: ", getprogname(), prev_err->msg);
7053 histedit_free_list(histedit_cmds);
7054 err = histedit_run_editor(histedit_cmds, path, commits,
7057 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7058 err->code != GOT_ERR_HISTEDIT_CMD)
7065 } else if (resp == 'r') {
7066 histedit_free_list(histedit_cmds);
7067 err = histedit_edit_script(histedit_cmds,
7068 commits, branch_name, 0, repo);
7070 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7071 err->code != GOT_ERR_HISTEDIT_CMD)
7078 } else if (resp == 'a') {
7079 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7082 printf("invalid response '%c'\n", resp);
7088 static const struct got_error *
7089 histedit_complete(struct got_worktree *worktree,
7090 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7091 struct got_reference *branch, struct got_repository *repo)
7093 printf("Switching work tree to %s\n",
7094 got_ref_get_symref_target(branch));
7095 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7099 static const struct got_error *
7100 show_histedit_progress(struct got_commit_object *commit,
7101 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7103 const struct got_error *err;
7104 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7106 err = got_object_id_str(&old_id_str, hle->commit_id);
7111 err = got_object_id_str(&new_id_str, new_id);
7116 old_id_str[12] = '\0';
7118 new_id_str[12] = '\0';
7121 logmsg = strdup(hle->logmsg);
7122 if (logmsg == NULL) {
7123 err = got_error_from_errno("strdup");
7126 trim_logmsg(logmsg, 42);
7128 err = get_short_logmsg(&logmsg, 42, commit);
7133 switch (hle->cmd->code) {
7134 case GOT_HISTEDIT_PICK:
7135 case GOT_HISTEDIT_EDIT:
7136 printf("%s -> %s: %s\n", old_id_str,
7137 new_id_str ? new_id_str : "no-op change", logmsg);
7139 case GOT_HISTEDIT_DROP:
7140 case GOT_HISTEDIT_FOLD:
7141 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7153 static const struct got_error *
7154 histedit_commit(struct got_pathlist_head *merged_paths,
7155 struct got_worktree *worktree, struct got_fileindex *fileindex,
7156 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7157 struct got_repository *repo)
7159 const struct got_error *err;
7160 struct got_commit_object *commit;
7161 struct got_object_id *new_commit_id;
7163 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7164 && hle->logmsg == NULL) {
7165 err = histedit_edit_logmsg(hle, repo);
7170 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7174 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7175 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7178 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7180 err = show_histedit_progress(commit, hle, NULL);
7182 err = show_histedit_progress(commit, hle, new_commit_id);
7183 free(new_commit_id);
7186 got_object_commit_close(commit);
7190 static const struct got_error *
7191 histedit_skip_commit(struct got_histedit_list_entry *hle,
7192 struct got_worktree *worktree, struct got_repository *repo)
7194 const struct got_error *error;
7195 struct got_commit_object *commit;
7197 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7202 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7206 error = show_histedit_progress(commit, hle, NULL);
7207 got_object_commit_close(commit);
7211 static const struct got_error *
7212 check_local_changes(void *arg, unsigned char status,
7213 unsigned char staged_status, const char *path,
7214 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7215 struct got_object_id *commit_id, int dirfd, const char *de_name)
7217 int *have_local_changes = arg;
7220 case GOT_STATUS_ADD:
7221 case GOT_STATUS_DELETE:
7222 case GOT_STATUS_MODIFY:
7223 case GOT_STATUS_CONFLICT:
7224 *have_local_changes = 1;
7225 return got_error(GOT_ERR_CANCELLED);
7230 switch (staged_status) {
7231 case GOT_STATUS_ADD:
7232 case GOT_STATUS_DELETE:
7233 case GOT_STATUS_MODIFY:
7234 *have_local_changes = 1;
7235 return got_error(GOT_ERR_CANCELLED);
7243 static const struct got_error *
7244 cmd_histedit(int argc, char *argv[])
7246 const struct got_error *error = NULL;
7247 struct got_worktree *worktree = NULL;
7248 struct got_fileindex *fileindex = NULL;
7249 struct got_repository *repo = NULL;
7251 struct got_reference *branch = NULL;
7252 struct got_reference *tmp_branch = NULL;
7253 struct got_object_id *resume_commit_id = NULL;
7254 struct got_object_id *base_commit_id = NULL;
7255 struct got_object_id *head_commit_id = NULL;
7256 struct got_commit_object *commit = NULL;
7257 int ch, rebase_in_progress = 0, did_something;
7258 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7259 int edit_logmsg_only = 0;
7260 const char *edit_script_path = NULL;
7261 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7262 struct got_object_id_queue commits;
7263 struct got_pathlist_head merged_paths;
7264 const struct got_object_id_queue *parent_ids;
7265 struct got_object_qid *pid;
7266 struct got_histedit_list histedit_cmds;
7267 struct got_histedit_list_entry *hle;
7269 SIMPLEQ_INIT(&commits);
7270 TAILQ_INIT(&histedit_cmds);
7271 TAILQ_INIT(&merged_paths);
7273 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7282 edit_script_path = optarg;
7285 edit_logmsg_only = 1;
7297 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7298 "unveil", NULL) == -1)
7301 if (abort_edit && continue_edit)
7302 errx(1, "histedit's -a and -c options are mutually exclusive");
7303 if (edit_script_path && edit_logmsg_only)
7304 errx(1, "histedit's -F and -m options are mutually exclusive");
7305 if (abort_edit && edit_logmsg_only)
7306 errx(1, "histedit's -a and -m options are mutually exclusive");
7307 if (continue_edit && edit_logmsg_only)
7308 errx(1, "histedit's -c and -m options are mutually exclusive");
7313 * This command cannot apply unveil(2) in all cases because the
7314 * user may choose to run an editor to edit the histedit script
7315 * and to edit individual commit log messages.
7316 * unveil(2) traverses exec(2); if an editor is used we have to
7317 * apply unveil after edit script and log messages have been written.
7318 * XXX TODO: Make use of unveil(2) where possible.
7321 cwd = getcwd(NULL, 0);
7323 error = got_error_from_errno("getcwd");
7326 error = got_worktree_open(&worktree, cwd);
7330 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7335 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7338 if (rebase_in_progress) {
7339 error = got_error(GOT_ERR_REBASING);
7343 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7347 if (edit_in_progress && edit_logmsg_only) {
7348 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7349 "histedit operation is in progress in this "
7350 "work tree and must be continued or aborted "
7351 "before the -m option can be used");
7355 if (edit_in_progress && abort_edit) {
7356 error = got_worktree_histedit_continue(&resume_commit_id,
7357 &tmp_branch, &branch, &base_commit_id, &fileindex,
7361 printf("Switching work tree to %s\n",
7362 got_ref_get_symref_target(branch));
7363 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7364 branch, base_commit_id, update_progress, &did_something);
7367 printf("Histedit of %s aborted\n",
7368 got_ref_get_symref_target(branch));
7369 goto done; /* nothing else to do */
7370 } else if (abort_edit) {
7371 error = got_error(GOT_ERR_NOT_HISTEDIT);
7375 if (continue_edit) {
7378 if (!edit_in_progress) {
7379 error = got_error(GOT_ERR_NOT_HISTEDIT);
7383 error = got_worktree_get_histedit_script_path(&path, worktree);
7387 error = histedit_load_list(&histedit_cmds, path, repo);
7392 error = got_worktree_histedit_continue(&resume_commit_id,
7393 &tmp_branch, &branch, &base_commit_id, &fileindex,
7398 error = got_ref_resolve(&head_commit_id, repo, branch);
7402 error = got_object_open_as_commit(&commit, repo,
7406 parent_ids = got_object_commit_get_parent_ids(commit);
7407 pid = SIMPLEQ_FIRST(parent_ids);
7409 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7412 error = collect_commits(&commits, head_commit_id, pid->id,
7413 base_commit_id, got_worktree_get_path_prefix(worktree),
7414 GOT_ERR_HISTEDIT_PATH, repo);
7415 got_object_commit_close(commit);
7420 if (edit_in_progress) {
7421 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7425 error = got_ref_open(&branch, repo,
7426 got_worktree_get_head_ref_name(worktree), 0);
7430 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7431 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7432 "will not edit commit history of a branch outside "
7433 "the \"refs/heads/\" reference namespace");
7437 error = got_ref_resolve(&head_commit_id, repo, branch);
7438 got_ref_close(branch);
7443 error = got_object_open_as_commit(&commit, repo,
7447 parent_ids = got_object_commit_get_parent_ids(commit);
7448 pid = SIMPLEQ_FIRST(parent_ids);
7450 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7453 error = collect_commits(&commits, head_commit_id, pid->id,
7454 got_worktree_get_base_commit_id(worktree),
7455 got_worktree_get_path_prefix(worktree),
7456 GOT_ERR_HISTEDIT_PATH, repo);
7457 got_object_commit_close(commit);
7462 if (SIMPLEQ_EMPTY(&commits)) {
7463 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7467 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7468 &base_commit_id, &fileindex, worktree, repo);
7472 if (edit_script_path) {
7473 error = histedit_load_list(&histedit_cmds,
7474 edit_script_path, repo);
7476 got_worktree_histedit_abort(worktree, fileindex,
7477 repo, branch, base_commit_id,
7478 update_progress, &did_something);
7482 const char *branch_name;
7483 branch_name = got_ref_get_symref_target(branch);
7484 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7486 error = histedit_edit_script(&histedit_cmds, &commits,
7487 branch_name, edit_logmsg_only, repo);
7489 got_worktree_histedit_abort(worktree, fileindex,
7490 repo, branch, base_commit_id,
7491 update_progress, &did_something);
7497 error = histedit_save_list(&histedit_cmds, worktree,
7500 got_worktree_histedit_abort(worktree, fileindex,
7501 repo, branch, base_commit_id,
7502 update_progress, &did_something);
7508 error = histedit_check_script(&histedit_cmds, &commits, repo);
7512 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7513 if (resume_commit_id) {
7514 if (got_object_id_cmp(hle->commit_id,
7515 resume_commit_id) != 0)
7518 resume_commit_id = NULL;
7519 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7520 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7521 error = histedit_skip_commit(hle, worktree,
7526 struct got_pathlist_head paths;
7527 int have_changes = 0;
7530 error = got_pathlist_append(&paths, "", NULL);
7533 error = got_worktree_status(worktree, &paths,
7534 repo, check_local_changes, &have_changes,
7535 check_cancelled, NULL);
7536 got_pathlist_free(&paths);
7538 if (error->code != GOT_ERR_CANCELLED)
7540 if (sigint_received || sigpipe_received)
7544 error = histedit_commit(NULL, worktree,
7545 fileindex, tmp_branch, hle, repo);
7549 error = got_object_open_as_commit(
7550 &commit, repo, hle->commit_id);
7553 error = show_histedit_progress(commit,
7555 got_object_commit_close(commit);
7564 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7565 error = histedit_skip_commit(hle, worktree, repo);
7571 error = got_object_open_as_commit(&commit, repo,
7575 parent_ids = got_object_commit_get_parent_ids(commit);
7576 pid = SIMPLEQ_FIRST(parent_ids);
7578 error = got_worktree_histedit_merge_files(&merged_paths,
7579 worktree, fileindex, pid->id, hle->commit_id, repo,
7580 rebase_progress, &rebase_status, check_cancelled, NULL);
7583 got_object_commit_close(commit);
7586 if (rebase_status == GOT_STATUS_CONFLICT) {
7587 error = show_rebase_merge_conflict(hle->commit_id,
7591 got_worktree_rebase_pathlist_free(&merged_paths);
7595 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7597 error = got_object_id_str(&id_str, hle->commit_id);
7600 printf("Stopping histedit for amending commit %s\n",
7603 got_worktree_rebase_pathlist_free(&merged_paths);
7604 error = got_worktree_histedit_postpone(worktree,
7609 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7610 error = histedit_skip_commit(hle, worktree, repo);
7616 error = histedit_commit(&merged_paths, worktree, fileindex,
7617 tmp_branch, hle, repo);
7618 got_worktree_rebase_pathlist_free(&merged_paths);
7623 if (rebase_status == GOT_STATUS_CONFLICT) {
7624 error = got_worktree_histedit_postpone(worktree, fileindex);
7627 error = got_error_msg(GOT_ERR_CONFLICTS,
7628 "conflicts must be resolved before histedit can continue");
7630 error = histedit_complete(worktree, fileindex, tmp_branch,
7633 got_object_id_queue_free(&commits);
7634 histedit_free_list(&histedit_cmds);
7635 free(head_commit_id);
7636 free(base_commit_id);
7637 free(resume_commit_id);
7639 got_object_commit_close(commit);
7641 got_ref_close(branch);
7643 got_ref_close(tmp_branch);
7645 got_worktree_close(worktree);
7647 got_repo_close(repo);
7652 usage_integrate(void)
7654 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7658 static const struct got_error *
7659 cmd_integrate(int argc, char *argv[])
7661 const struct got_error *error = NULL;
7662 struct got_repository *repo = NULL;
7663 struct got_worktree *worktree = NULL;
7664 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7665 const char *branch_arg = NULL;
7666 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7667 struct got_fileindex *fileindex = NULL;
7668 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7669 int ch, did_something = 0;
7671 while ((ch = getopt(argc, argv, "")) != -1) {
7684 branch_arg = argv[0];
7686 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7687 "unveil", NULL) == -1)
7690 cwd = getcwd(NULL, 0);
7692 error = got_error_from_errno("getcwd");
7696 error = got_worktree_open(&worktree, cwd);
7700 error = check_rebase_or_histedit_in_progress(worktree);
7704 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7709 error = apply_unveil(got_repo_get_path(repo), 0,
7710 got_worktree_get_root_path(worktree));
7714 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7715 error = got_error_from_errno("asprintf");
7719 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7720 &base_branch_ref, worktree, refname, repo);
7724 refname = strdup(got_ref_get_name(branch_ref));
7725 if (refname == NULL) {
7726 error = got_error_from_errno("strdup");
7727 got_worktree_integrate_abort(worktree, fileindex, repo,
7728 branch_ref, base_branch_ref);
7731 base_refname = strdup(got_ref_get_name(base_branch_ref));
7732 if (base_refname == NULL) {
7733 error = got_error_from_errno("strdup");
7734 got_worktree_integrate_abort(worktree, fileindex, repo,
7735 branch_ref, base_branch_ref);
7739 error = got_ref_resolve(&commit_id, repo, branch_ref);
7743 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7747 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7748 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7749 "specified branch has already been integrated");
7750 got_worktree_integrate_abort(worktree, fileindex, repo,
7751 branch_ref, base_branch_ref);
7755 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7757 if (error->code == GOT_ERR_ANCESTRY)
7758 error = got_error(GOT_ERR_REBASE_REQUIRED);
7759 got_worktree_integrate_abort(worktree, fileindex, repo,
7760 branch_ref, base_branch_ref);
7764 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7765 branch_ref, base_branch_ref, update_progress, &did_something,
7766 check_cancelled, NULL);
7770 printf("Integrated %s into %s\n", refname, base_refname);
7773 got_repo_close(repo);
7775 got_worktree_close(worktree);
7777 free(base_commit_id);
7787 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7788 "[file-path ...]\n",
7793 static const struct got_error *
7794 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7795 const char *path, struct got_object_id *blob_id,
7796 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7797 int dirfd, const char *de_name)
7799 const struct got_error *err = NULL;
7800 char *id_str = NULL;
7802 if (staged_status != GOT_STATUS_ADD &&
7803 staged_status != GOT_STATUS_MODIFY &&
7804 staged_status != GOT_STATUS_DELETE)
7807 if (staged_status == GOT_STATUS_ADD ||
7808 staged_status == GOT_STATUS_MODIFY)
7809 err = got_object_id_str(&id_str, staged_blob_id);
7811 err = got_object_id_str(&id_str, blob_id);
7815 printf("%s %c %s\n", id_str, staged_status, path);
7820 static const struct got_error *
7821 cmd_stage(int argc, char *argv[])
7823 const struct got_error *error = NULL;
7824 struct got_repository *repo = NULL;
7825 struct got_worktree *worktree = NULL;
7827 struct got_pathlist_head paths;
7828 struct got_pathlist_entry *pe;
7829 int ch, list_stage = 0, pflag = 0;
7830 FILE *patch_script_file = NULL;
7831 const char *patch_script_path = NULL;
7832 struct choose_patch_arg cpa;
7836 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7845 patch_script_path = optarg;
7857 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7858 "unveil", NULL) == -1)
7861 if (list_stage && (pflag || patch_script_path))
7862 errx(1, "-l option cannot be used with other options");
7863 if (patch_script_path && !pflag)
7864 errx(1, "-F option can only be used together with -p option");
7866 cwd = getcwd(NULL, 0);
7868 error = got_error_from_errno("getcwd");
7872 error = got_worktree_open(&worktree, cwd);
7876 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7881 if (patch_script_path) {
7882 patch_script_file = fopen(patch_script_path, "r");
7883 if (patch_script_file == NULL) {
7884 error = got_error_from_errno2("fopen",
7889 error = apply_unveil(got_repo_get_path(repo), 0,
7890 got_worktree_get_root_path(worktree));
7894 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7899 error = got_worktree_status(worktree, &paths, repo,
7900 print_stage, NULL, check_cancelled, NULL);
7902 cpa.patch_script_file = patch_script_file;
7903 cpa.action = "stage";
7904 error = got_worktree_stage(worktree, &paths,
7905 pflag ? NULL : print_status, NULL,
7906 pflag ? choose_patch : NULL, &cpa, repo);
7909 if (patch_script_file && fclose(patch_script_file) == EOF &&
7911 error = got_error_from_errno2("fclose", patch_script_path);
7913 got_repo_close(repo);
7915 got_worktree_close(worktree);
7916 TAILQ_FOREACH(pe, &paths, entry)
7917 free((char *)pe->path);
7918 got_pathlist_free(&paths);
7926 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7927 "[file-path ...]\n",
7933 static const struct got_error *
7934 cmd_unstage(int argc, char *argv[])
7936 const struct got_error *error = NULL;
7937 struct got_repository *repo = NULL;
7938 struct got_worktree *worktree = NULL;
7940 struct got_pathlist_head paths;
7941 struct got_pathlist_entry *pe;
7942 int ch, did_something = 0, pflag = 0;
7943 FILE *patch_script_file = NULL;
7944 const char *patch_script_path = NULL;
7945 struct choose_patch_arg cpa;
7949 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7955 patch_script_path = optarg;
7967 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7968 "unveil", NULL) == -1)
7971 if (patch_script_path && !pflag)
7972 errx(1, "-F option can only be used together with -p option");
7974 cwd = getcwd(NULL, 0);
7976 error = got_error_from_errno("getcwd");
7980 error = got_worktree_open(&worktree, cwd);
7984 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7989 if (patch_script_path) {
7990 patch_script_file = fopen(patch_script_path, "r");
7991 if (patch_script_file == NULL) {
7992 error = got_error_from_errno2("fopen",
7998 error = apply_unveil(got_repo_get_path(repo), 0,
7999 got_worktree_get_root_path(worktree));
8003 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8007 cpa.patch_script_file = patch_script_file;
8008 cpa.action = "unstage";
8009 error = got_worktree_unstage(worktree, &paths, update_progress,
8010 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8012 if (patch_script_file && fclose(patch_script_file) == EOF &&
8014 error = got_error_from_errno2("fclose", patch_script_path);
8016 got_repo_close(repo);
8018 got_worktree_close(worktree);
8019 TAILQ_FOREACH(pe, &paths, entry)
8020 free((char *)pe->path);
8021 got_pathlist_free(&paths);
8029 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8030 "arg1 [arg2 ...]\n", getprogname());
8034 static const struct got_error *
8035 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8037 const struct got_error *err;
8038 struct got_blob_object *blob;
8040 err = got_object_open_as_blob(&blob, repo, id, 8192);
8044 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8045 got_object_blob_close(blob);
8049 static const struct got_error *
8050 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8052 const struct got_error *err;
8053 struct got_tree_object *tree;
8056 err = got_object_open_as_tree(&tree, repo, id);
8060 nentries = got_object_tree_get_nentries(tree);
8061 for (i = 0; i < nentries; i++) {
8062 struct got_tree_entry *te;
8064 if (sigint_received || sigpipe_received)
8066 te = got_object_tree_get_entry(tree, i);
8067 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8070 fprintf(outfile, "%s %.7o %s\n", id_str,
8071 got_tree_entry_get_mode(te),
8072 got_tree_entry_get_name(te));
8076 got_object_tree_close(tree);
8080 static const struct got_error *
8081 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8083 const struct got_error *err;
8084 struct got_commit_object *commit;
8085 const struct got_object_id_queue *parent_ids;
8086 struct got_object_qid *pid;
8087 char *id_str = NULL;
8088 const char *logmsg = NULL;
8090 err = got_object_open_as_commit(&commit, repo, id);
8094 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8098 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8099 parent_ids = got_object_commit_get_parent_ids(commit);
8100 fprintf(outfile, "numparents %d\n",
8101 got_object_commit_get_nparents(commit));
8102 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8104 err = got_object_id_str(&pid_str, pid->id);
8107 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8111 got_object_commit_get_author(commit),
8112 got_object_commit_get_author_time(commit));
8114 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8115 got_object_commit_get_author(commit),
8116 got_object_commit_get_committer_time(commit));
8118 logmsg = got_object_commit_get_logmsg_raw(commit);
8119 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8120 fprintf(outfile, "%s", logmsg);
8123 got_object_commit_close(commit);
8127 static const struct got_error *
8128 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8130 const struct got_error *err;
8131 struct got_tag_object *tag;
8132 char *id_str = NULL;
8133 const char *tagmsg = NULL;
8135 err = got_object_open_as_tag(&tag, repo, id);
8139 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8145 switch (got_object_tag_get_object_type(tag)) {
8146 case GOT_OBJ_TYPE_BLOB:
8147 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8148 GOT_OBJ_LABEL_BLOB);
8150 case GOT_OBJ_TYPE_TREE:
8151 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8152 GOT_OBJ_LABEL_TREE);
8154 case GOT_OBJ_TYPE_COMMIT:
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8156 GOT_OBJ_LABEL_COMMIT);
8158 case GOT_OBJ_TYPE_TAG:
8159 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8166 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8167 got_object_tag_get_name(tag));
8169 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8170 got_object_tag_get_tagger(tag),
8171 got_object_tag_get_tagger_time(tag));
8173 tagmsg = got_object_tag_get_message(tag);
8174 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8175 fprintf(outfile, "%s", tagmsg);
8178 got_object_tag_close(tag);
8182 static const struct got_error *
8183 cmd_cat(int argc, char *argv[])
8185 const struct got_error *error;
8186 struct got_repository *repo = NULL;
8187 struct got_worktree *worktree = NULL;
8188 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8189 const char *commit_id_str = NULL;
8190 struct got_object_id *id = NULL, *commit_id = NULL;
8191 int ch, obj_type, i, force_path = 0;
8194 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8199 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8202 commit_id_str = optarg;
8205 repo_path = realpath(optarg, NULL);
8206 if (repo_path == NULL)
8207 return got_error_from_errno2("realpath",
8209 got_path_strip_trailing_slashes(repo_path);
8223 cwd = getcwd(NULL, 0);
8225 error = got_error_from_errno("getcwd");
8228 error = got_worktree_open(&worktree, cwd);
8229 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8232 if (repo_path == NULL) {
8234 got_worktree_get_repo_path(worktree));
8235 if (repo_path == NULL) {
8236 error = got_error_from_errno("strdup");
8242 if (repo_path == NULL) {
8243 repo_path = getcwd(NULL, 0);
8244 if (repo_path == NULL)
8245 return got_error_from_errno("getcwd");
8248 error = got_repo_open(&repo, repo_path, NULL);
8253 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8257 if (commit_id_str == NULL)
8258 commit_id_str = GOT_REF_HEAD;
8259 error = got_repo_match_object_id(&commit_id, NULL,
8260 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8264 for (i = 0; i < argc; i++) {
8266 error = got_object_id_by_path(&id, repo, commit_id,
8271 error = got_repo_match_object_id(&id, &label, argv[i],
8272 GOT_OBJ_TYPE_ANY, 0, repo);
8274 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8275 error->code != GOT_ERR_NOT_REF)
8277 error = got_object_id_by_path(&id, repo,
8278 commit_id, argv[i]);
8284 error = got_object_get_type(&obj_type, repo, id);
8289 case GOT_OBJ_TYPE_BLOB:
8290 error = cat_blob(id, repo, stdout);
8292 case GOT_OBJ_TYPE_TREE:
8293 error = cat_tree(id, repo, stdout);
8295 case GOT_OBJ_TYPE_COMMIT:
8296 error = cat_commit(id, repo, stdout);
8298 case GOT_OBJ_TYPE_TAG:
8299 error = cat_tag(id, repo, stdout);
8302 error = got_error(GOT_ERR_OBJ_TYPE);
8317 got_worktree_close(worktree);
8319 const struct got_error *repo_error;
8320 repo_error = got_repo_close(repo);