2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 "[target-directory]\n", getprogname());
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
873 if (print_size || print_indexed || print_resolved)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
878 printf("; indexing %d%%", p_indexed);
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
915 fetch_all_branches = 1;
918 mirror_references = 1;
923 else if (verbosity < 3)
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
958 if (strcmp(proto, "git") == 0) {
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
984 repo_path = default_destdir;
988 error = got_path_mkdir(repo_path);
992 error = got_repo_init(repo_path);
996 error = got_repo_open(&repo, repo_path, NULL);
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1030 error = got_object_id_str(&id_str, pack_hash);
1034 printf("\nFetched %s.pack\n", id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1047 error = got_ref_write(ref, repo);
1052 if (mirror_references)
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1064 error = got_ref_alloc(&ref, remote_refname, id);
1067 error = got_ref_write(ref, repo);
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 error = got_ref_open(&target_ref, repo, target, 0);
1084 if (error->code == GOT_ERR_NOT_REF)
1089 error = got_ref_alloc_symref(&head_symref,
1090 GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(head_symref));
1099 error = got_ref_write(head_symref, repo);
1103 /* Create a config file git-fetch(1) can understand. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1109 gitconfig_file = fopen(gitconfig_path, "a");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1114 if (mirror_references) {
1115 if (asprintf(&gitconfig,
1118 "\tmirror = true\n",
1119 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1120 error = got_error_from_errno("asprintf");
1123 } else if (fetch_all_branches) {
1124 if (asprintf(&gitconfig,
1127 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1128 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1129 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1130 error = got_error_from_errno("asprintf");
1134 const char *branchname;
1137 * If the server specified a default branch, use just that one.
1138 * Otherwise fall back to fetching all branches on next fetch.
1141 branchname = got_ref_get_symref_target(head_symref);
1142 if (strncmp(branchname, "refs/heads/", 11) == 0)
1145 branchname = "*"; /* fall back to all branches */
1146 if (asprintf(&gitconfig,
1149 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1150 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1151 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1152 branchname) == -1) {
1153 error = got_error_from_errno("asprintf");
1157 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1158 if (n != strlen(gitconfig)) {
1159 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1165 printf("Created %s repository '%s'\n",
1166 mirror_references ? "mirrored" : "cloned", repo_path);
1168 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1169 error = got_error_from_errno("close");
1170 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1171 error = got_error_from_errno("fclose");
1173 got_repo_close(repo);
1175 got_ref_close(head_symref);
1176 TAILQ_FOREACH(pe, &refs, entry) {
1177 free((void *)pe->path);
1180 got_pathlist_free(&refs);
1181 TAILQ_FOREACH(pe, &symrefs, entry) {
1182 free((void *)pe->path);
1185 got_pathlist_free(&symrefs);
1192 free(default_destdir);
1193 free(gitconfig_path);
1198 static const struct got_error *
1199 create_ref(const char *refname, struct got_object_id *id,
1200 const char *id_str, struct got_repository *repo)
1202 const struct got_error *err = NULL;
1203 struct got_reference *ref;
1205 printf("Creating %s: %s\n", refname, id_str);
1207 err = got_ref_alloc(&ref, refname, id);
1211 err = got_ref_write(ref, repo);
1216 static const struct got_error *
1217 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1218 struct got_repository *repo)
1220 const struct got_error *err = NULL;
1221 char *new_id_str = NULL;
1222 struct got_object_id *old_id = NULL;
1224 err = got_object_id_str(&new_id_str, new_id);
1228 if (got_ref_is_symbolic(ref)) {
1229 struct got_reference *new_ref;
1230 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1233 printf("Deleting symbolic reference %s -> %s\n",
1234 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1235 err = got_ref_delete(ref, repo);
1238 printf("Setting %s to %s\n", got_ref_get_name(ref),
1240 err = got_ref_write(new_ref, repo);
1244 err = got_ref_resolve(&old_id, repo, ref);
1247 if (got_object_id_cmp(old_id, new_id) != 0) {
1248 printf("Setting %s to %s\n",
1249 got_ref_get_name(ref), new_id_str);
1250 err = got_ref_change_ref(ref, new_id);
1253 err = got_ref_write(ref, repo);
1267 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1268 "[remote-repository-name]\n", getprogname());
1272 static const struct got_error *
1273 cmd_fetch(int argc, char *argv[])
1275 const struct got_error *error = NULL;
1276 char *cwd = NULL, *repo_path = NULL;
1277 const char *remote_name;
1278 char *proto = NULL, *host = NULL, *port = NULL;
1279 char *repo_name = NULL, *server_path = NULL;
1280 struct got_remote_repo *remotes, *remote = NULL;
1282 char *id_str = NULL;
1283 struct got_repository *repo = NULL;
1284 struct got_worktree *worktree = NULL;
1285 struct got_pathlist_head refs, symrefs;
1286 struct got_pathlist_entry *pe;
1287 struct got_object_id *pack_hash = NULL;
1288 int i, ch, fetchfd = -1;
1289 struct got_fetch_progress_arg fpa;
1290 int verbosity = 0, fetch_all_branches = 0;
1293 TAILQ_INIT(&symrefs);
1295 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1298 fetch_all_branches = 1;
1301 repo_path = realpath(optarg, NULL);
1302 if (repo_path == NULL)
1303 return got_error_from_errno2("realpath",
1305 got_path_strip_trailing_slashes(repo_path);
1310 else if (verbosity < 3)
1325 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1327 remote_name = argv[0];
1331 cwd = getcwd(NULL, 0);
1333 error = got_error_from_errno("getcwd");
1337 if (repo_path == NULL) {
1338 error = got_worktree_open(&worktree, cwd);
1339 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1345 strdup(got_worktree_get_repo_path(worktree));
1346 if (repo_path == NULL)
1347 error = got_error_from_errno("strdup");
1351 repo_path = strdup(cwd);
1352 if (repo_path == NULL) {
1353 error = got_error_from_errno("strdup");
1359 error = got_repo_open(&repo, repo_path, NULL);
1363 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1364 for (i = 0; i < nremotes; i++) {
1365 remote = &remotes[i];
1366 if (strcmp(remote->name, remote_name) == 0)
1369 if (i == nremotes) {
1370 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1374 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1375 &repo_name, remote->url);
1379 if (strcmp(proto, "git") == 0) {
1381 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1382 "sendfd dns inet unveil", NULL) == -1)
1385 } else if (strcmp(proto, "git+ssh") == 0 ||
1386 strcmp(proto, "ssh") == 0) {
1388 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1389 "sendfd unveil", NULL) == -1)
1392 } else if (strcmp(proto, "http") == 0 ||
1393 strcmp(proto, "git+http") == 0) {
1394 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1397 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1401 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1402 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1403 error = got_error_from_errno2("unveil",
1404 GOT_FETCH_PATH_SSH);
1408 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1412 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1418 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1419 port ? ":" : "", port ? port : "");
1421 fpa.last_scaled_size[0] = '\0';
1422 fpa.last_p_indexed = -1;
1423 fpa.last_p_resolved = -1;
1424 fpa.verbosity = verbosity;
1425 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1426 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1427 fetch_progress, &fpa);
1431 if (pack_hash == NULL) {
1433 printf("Already up-to-date\n");
1437 if (verbosity >= 0) {
1438 error = got_object_id_str(&id_str, pack_hash);
1441 printf("\nFetched %s.pack\n", id_str);
1446 /* Update references provided with the pack file. */
1447 TAILQ_FOREACH(pe, &refs, entry) {
1448 const char *refname = pe->path;
1449 struct got_object_id *id = pe->data;
1450 struct got_reference *ref;
1451 char *remote_refname;
1453 error = got_object_id_str(&id_str, id);
1457 if (strncmp("refs/tags/", refname, 10) == 0) {
1458 error = got_ref_open(&ref, repo, refname, 0);
1460 if (error->code != GOT_ERR_NOT_REF)
1462 error = create_ref(refname, id, id_str, repo);
1466 error = update_ref(ref, id, repo);
1471 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1472 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1473 remote_name, refname + 11) == -1) {
1474 error = got_error_from_errno("asprintf");
1478 error = got_ref_open(&ref, repo, remote_refname, 0);
1480 if (error->code != GOT_ERR_NOT_REF)
1482 error = create_ref(remote_refname, id, id_str,
1487 error = update_ref(ref, id, repo);
1497 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1498 error = got_error_from_errno("close");
1500 got_repo_close(repo);
1502 got_worktree_close(worktree);
1503 TAILQ_FOREACH(pe, &refs, entry) {
1504 free((void *)pe->path);
1507 got_pathlist_free(&refs);
1508 TAILQ_FOREACH(pe, &symrefs, entry) {
1509 free((void *)pe->path);
1512 got_pathlist_free(&symrefs);
1527 usage_checkout(void)
1529 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1530 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1535 show_worktree_base_ref_warning(void)
1537 fprintf(stderr, "%s: warning: could not create a reference "
1538 "to the work tree's base commit; the commit could be "
1539 "garbage-collected by Git; making the repository "
1540 "writable and running 'got update' will prevent this\n",
1544 struct got_checkout_progress_arg {
1545 const char *worktree_path;
1546 int had_base_commit_ref_error;
1549 static const struct got_error *
1550 checkout_progress(void *arg, unsigned char status, const char *path)
1552 struct got_checkout_progress_arg *a = arg;
1554 /* Base commit bump happens silently. */
1555 if (status == GOT_STATUS_BUMP_BASE)
1558 if (status == GOT_STATUS_BASE_REF_ERR) {
1559 a->had_base_commit_ref_error = 1;
1563 while (path[0] == '/')
1566 printf("%c %s/%s\n", status, a->worktree_path, path);
1570 static const struct got_error *
1571 check_cancelled(void *arg)
1573 if (sigint_received || sigpipe_received)
1574 return got_error(GOT_ERR_CANCELLED);
1578 static const struct got_error *
1579 check_linear_ancestry(struct got_object_id *commit_id,
1580 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1581 struct got_repository *repo)
1583 const struct got_error *err = NULL;
1584 struct got_object_id *yca_id;
1586 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1587 commit_id, base_commit_id, repo, check_cancelled, NULL);
1592 return got_error(GOT_ERR_ANCESTRY);
1595 * Require a straight line of history between the target commit
1596 * and the work tree's base commit.
1598 * Non-linear situations such as this require a rebase:
1600 * (commit) D F (base_commit)
1608 * 'got update' only handles linear cases:
1609 * Update forwards in time: A (base/yca) - B - C - D (commit)
1610 * Update backwards in time: D (base) - C - B - A (commit/yca)
1612 if (allow_forwards_in_time_only) {
1613 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1614 return got_error(GOT_ERR_ANCESTRY);
1615 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1616 got_object_id_cmp(base_commit_id, yca_id) != 0)
1617 return got_error(GOT_ERR_ANCESTRY);
1623 static const struct got_error *
1624 check_same_branch(struct got_object_id *commit_id,
1625 struct got_reference *head_ref, struct got_object_id *yca_id,
1626 struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_commit_graph *graph = NULL;
1630 struct got_object_id *head_commit_id = NULL;
1631 int is_same_branch = 0;
1633 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1637 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1641 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1646 err = got_commit_graph_open(&graph, "/", 1);
1650 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1651 check_cancelled, NULL);
1656 struct got_object_id *id;
1657 err = got_commit_graph_iter_next(&id, graph, repo,
1658 check_cancelled, NULL);
1660 if (err->code == GOT_ERR_ITER_COMPLETED)
1666 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1668 if (got_object_id_cmp(id, commit_id) == 0) {
1676 got_commit_graph_close(graph);
1677 free(head_commit_id);
1678 if (!err && !is_same_branch)
1679 err = got_error(GOT_ERR_ANCESTRY);
1683 static const struct got_error *
1684 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1686 static char msg[512];
1687 const char *branch_name;
1689 if (got_ref_is_symbolic(ref))
1690 branch_name = got_ref_get_symref_target(ref);
1692 branch_name = got_ref_get_name(ref);
1694 if (strncmp("refs/heads/", branch_name, 11) == 0)
1697 snprintf(msg, sizeof(msg),
1698 "target commit is not contained in branch '%s'; "
1699 "the branch to use must be specified with -b; "
1700 "if necessary a new branch can be created for "
1701 "this commit with 'got branch -c %s BRANCH_NAME'",
1702 branch_name, commit_id_str);
1704 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1707 static const struct got_error *
1708 cmd_checkout(int argc, char *argv[])
1710 const struct got_error *error = NULL;
1711 struct got_repository *repo = NULL;
1712 struct got_reference *head_ref = NULL;
1713 struct got_worktree *worktree = NULL;
1714 char *repo_path = NULL;
1715 char *worktree_path = NULL;
1716 const char *path_prefix = "";
1717 const char *branch_name = GOT_REF_HEAD;
1718 char *commit_id_str = NULL;
1719 int ch, same_path_prefix, allow_nonempty = 0;
1720 struct got_pathlist_head paths;
1721 struct got_checkout_progress_arg cpa;
1725 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1728 branch_name = optarg;
1731 commit_id_str = strdup(optarg);
1732 if (commit_id_str == NULL)
1733 return got_error_from_errno("strdup");
1739 path_prefix = optarg;
1751 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1752 "unveil", NULL) == -1)
1756 char *cwd, *base, *dotgit;
1757 repo_path = realpath(argv[0], NULL);
1758 if (repo_path == NULL)
1759 return got_error_from_errno2("realpath", argv[0]);
1760 cwd = getcwd(NULL, 0);
1762 error = got_error_from_errno("getcwd");
1765 if (path_prefix[0]) {
1766 base = basename(path_prefix);
1768 error = got_error_from_errno2("basename",
1773 base = basename(repo_path);
1775 error = got_error_from_errno2("basename",
1780 dotgit = strstr(base, ".git");
1783 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1784 error = got_error_from_errno("asprintf");
1789 } else if (argc == 2) {
1790 repo_path = realpath(argv[0], NULL);
1791 if (repo_path == NULL) {
1792 error = got_error_from_errno2("realpath", argv[0]);
1795 worktree_path = realpath(argv[1], NULL);
1796 if (worktree_path == NULL) {
1797 if (errno != ENOENT) {
1798 error = got_error_from_errno2("realpath",
1802 worktree_path = strdup(argv[1]);
1803 if (worktree_path == NULL) {
1804 error = got_error_from_errno("strdup");
1811 got_path_strip_trailing_slashes(repo_path);
1812 got_path_strip_trailing_slashes(worktree_path);
1814 error = got_repo_open(&repo, repo_path, NULL);
1818 /* Pre-create work tree path for unveil(2) */
1819 error = got_path_mkdir(worktree_path);
1821 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1822 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1824 if (!allow_nonempty &&
1825 !got_path_dir_is_empty(worktree_path)) {
1826 error = got_error_path(worktree_path,
1827 GOT_ERR_DIR_NOT_EMPTY);
1832 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1836 error = got_ref_open(&head_ref, repo, branch_name, 0);
1840 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1841 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1844 error = got_worktree_open(&worktree, worktree_path);
1848 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1852 if (!same_path_prefix) {
1853 error = got_error(GOT_ERR_PATH_PREFIX);
1857 if (commit_id_str) {
1858 struct got_object_id *commit_id;
1859 error = got_repo_match_object_id(&commit_id, NULL,
1860 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1863 error = check_linear_ancestry(commit_id,
1864 got_worktree_get_base_commit_id(worktree), 0, repo);
1865 if (error != NULL) {
1867 if (error->code == GOT_ERR_ANCESTRY) {
1868 error = checkout_ancestry_error(
1869 head_ref, commit_id_str);
1873 error = check_same_branch(commit_id, head_ref, NULL, repo);
1875 if (error->code == GOT_ERR_ANCESTRY) {
1876 error = checkout_ancestry_error(
1877 head_ref, commit_id_str);
1881 error = got_worktree_set_base_commit_id(worktree, repo,
1888 error = got_pathlist_append(&paths, "", NULL);
1891 cpa.worktree_path = worktree_path;
1892 cpa.had_base_commit_ref_error = 0;
1893 error = got_worktree_checkout_files(worktree, &paths, repo,
1894 checkout_progress, &cpa, check_cancelled, NULL);
1898 printf("Now shut up and hack\n");
1899 if (cpa.had_base_commit_ref_error)
1900 show_worktree_base_ref_warning();
1902 got_pathlist_free(&paths);
1903 free(commit_id_str);
1905 free(worktree_path);
1912 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1917 static const struct got_error *
1918 update_progress(void *arg, unsigned char status, const char *path)
1920 int *did_something = arg;
1922 if (status == GOT_STATUS_EXISTS ||
1923 status == GOT_STATUS_BASE_REF_ERR)
1928 /* Base commit bump happens silently. */
1929 if (status == GOT_STATUS_BUMP_BASE)
1932 while (path[0] == '/')
1934 printf("%c %s\n", status, path);
1938 static const struct got_error *
1939 switch_head_ref(struct got_reference *head_ref,
1940 struct got_object_id *commit_id, struct got_worktree *worktree,
1941 struct got_repository *repo)
1943 const struct got_error *err = NULL;
1945 int ref_has_moved = 0;
1947 /* Trivial case: switching between two different references. */
1948 if (strcmp(got_ref_get_name(head_ref),
1949 got_worktree_get_head_ref_name(worktree)) != 0) {
1950 printf("Switching work tree from %s to %s\n",
1951 got_worktree_get_head_ref_name(worktree),
1952 got_ref_get_name(head_ref));
1953 return got_worktree_set_head_ref(worktree, head_ref);
1956 err = check_linear_ancestry(commit_id,
1957 got_worktree_get_base_commit_id(worktree), 0, repo);
1959 if (err->code != GOT_ERR_ANCESTRY)
1966 /* Switching to a rebased branch with the same reference name. */
1967 err = got_object_id_str(&base_id_str,
1968 got_worktree_get_base_commit_id(worktree));
1971 printf("Reference %s now points at a different branch\n",
1972 got_worktree_get_head_ref_name(worktree));
1973 printf("Switching work tree from %s to %s\n", base_id_str,
1974 got_worktree_get_head_ref_name(worktree));
1978 static const struct got_error *
1979 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1981 const struct got_error *err;
1984 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1988 return got_error(GOT_ERR_REBASING);
1990 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1994 return got_error(GOT_ERR_HISTEDIT_BUSY);
1999 static const struct got_error *
2000 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2001 char *argv[], struct got_worktree *worktree)
2003 const struct got_error *err = NULL;
2010 return got_error_from_errno("strdup");
2011 return got_pathlist_append(paths, path, NULL);
2014 for (i = 0; i < argc; i++) {
2015 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2018 err = got_pathlist_append(paths, path, NULL);
2028 static const struct got_error *
2029 cmd_update(int argc, char *argv[])
2031 const struct got_error *error = NULL;
2032 struct got_repository *repo = NULL;
2033 struct got_worktree *worktree = NULL;
2034 char *worktree_path = NULL;
2035 struct got_object_id *commit_id = NULL;
2036 char *commit_id_str = NULL;
2037 const char *branch_name = NULL;
2038 struct got_reference *head_ref = NULL;
2039 struct got_pathlist_head paths;
2040 struct got_pathlist_entry *pe;
2041 int ch, did_something = 0;
2045 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2048 branch_name = optarg;
2051 commit_id_str = strdup(optarg);
2052 if (commit_id_str == NULL)
2053 return got_error_from_errno("strdup");
2065 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2066 "unveil", NULL) == -1)
2069 worktree_path = getcwd(NULL, 0);
2070 if (worktree_path == NULL) {
2071 error = got_error_from_errno("getcwd");
2074 error = got_worktree_open(&worktree, worktree_path);
2078 error = check_rebase_or_histedit_in_progress(worktree);
2082 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2087 error = apply_unveil(got_repo_get_path(repo), 0,
2088 got_worktree_get_root_path(worktree));
2092 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2096 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2097 got_worktree_get_head_ref_name(worktree), 0);
2100 if (commit_id_str == NULL) {
2101 error = got_ref_resolve(&commit_id, repo, head_ref);
2104 error = got_object_id_str(&commit_id_str, commit_id);
2108 error = got_repo_match_object_id(&commit_id, NULL,
2109 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2110 free(commit_id_str);
2111 commit_id_str = NULL;
2114 error = got_object_id_str(&commit_id_str, commit_id);
2120 struct got_object_id *head_commit_id;
2121 TAILQ_FOREACH(pe, &paths, entry) {
2122 if (pe->path_len == 0)
2124 error = got_error_msg(GOT_ERR_BAD_PATH,
2125 "switching between branches requires that "
2126 "the entire work tree gets updated");
2129 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2132 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2134 free(head_commit_id);
2137 error = check_same_branch(commit_id, head_ref, NULL, repo);
2140 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2144 error = check_linear_ancestry(commit_id,
2145 got_worktree_get_base_commit_id(worktree), 0, repo);
2146 if (error != NULL) {
2147 if (error->code == GOT_ERR_ANCESTRY)
2148 error = got_error(GOT_ERR_BRANCH_MOVED);
2151 error = check_same_branch(commit_id, head_ref, NULL, repo);
2156 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2158 error = got_worktree_set_base_commit_id(worktree, repo,
2164 error = got_worktree_checkout_files(worktree, &paths, repo,
2165 update_progress, &did_something, check_cancelled, NULL);
2170 printf("Updated to commit %s\n", commit_id_str);
2172 printf("Already up-to-date\n");
2174 free(worktree_path);
2175 TAILQ_FOREACH(pe, &paths, entry)
2176 free((char *)pe->path);
2177 got_pathlist_free(&paths);
2179 free(commit_id_str);
2183 static const struct got_error *
2184 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2185 const char *path, int diff_context, int ignore_whitespace,
2186 struct got_repository *repo)
2188 const struct got_error *err = NULL;
2189 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2192 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2197 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2201 while (path[0] == '/')
2203 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2204 ignore_whitespace, stdout);
2207 got_object_blob_close(blob1);
2208 got_object_blob_close(blob2);
2212 static const struct got_error *
2213 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2214 const char *path, int diff_context, int ignore_whitespace,
2215 struct got_repository *repo)
2217 const struct got_error *err = NULL;
2218 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2219 struct got_diff_blob_output_unidiff_arg arg;
2222 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2227 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2231 arg.diff_context = diff_context;
2232 arg.ignore_whitespace = ignore_whitespace;
2233 arg.outfile = stdout;
2234 while (path[0] == '/')
2236 err = got_diff_tree(tree1, tree2, path, path, repo,
2237 got_diff_blob_output_unidiff, &arg, 1);
2240 got_object_tree_close(tree1);
2242 got_object_tree_close(tree2);
2246 static const struct got_error *
2247 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2248 const char *path, int diff_context, struct got_repository *repo)
2250 const struct got_error *err = NULL;
2251 struct got_commit_object *pcommit = NULL;
2252 char *id_str1 = NULL, *id_str2 = NULL;
2253 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2254 struct got_object_qid *qid;
2256 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2258 err = got_object_open_as_commit(&pcommit, repo,
2264 if (path && path[0] != '\0') {
2266 err = got_object_id_by_path(&obj_id2, repo, id, path);
2269 err = got_object_id_str(&id_str2, obj_id2);
2275 err = got_object_id_by_path(&obj_id1, repo,
2281 err = got_object_id_str(&id_str1, obj_id1);
2287 err = got_object_get_type(&obj_type, repo, obj_id2);
2292 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2294 case GOT_OBJ_TYPE_BLOB:
2295 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2298 case GOT_OBJ_TYPE_TREE:
2299 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2303 err = got_error(GOT_ERR_OBJ_TYPE);
2309 obj_id2 = got_object_commit_get_tree_id(commit);
2310 err = got_object_id_str(&id_str2, obj_id2);
2313 obj_id1 = got_object_commit_get_tree_id(pcommit);
2314 err = got_object_id_str(&id_str1, obj_id1);
2317 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2318 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2324 got_object_commit_close(pcommit);
2329 get_datestr(time_t *time, char *datebuf)
2331 struct tm mytm, *tm;
2334 tm = gmtime_r(time, &mytm);
2337 s = asctime_r(tm, datebuf);
2340 p = strchr(s, '\n');
2346 static const struct got_error *
2347 match_logmsg(int *have_match, struct got_object_id *id,
2348 struct got_commit_object *commit, regex_t *regex)
2350 const struct got_error *err = NULL;
2351 regmatch_t regmatch;
2352 char *id_str = NULL, *logmsg = NULL;
2356 err = got_object_id_str(&id_str, id);
2360 err = got_object_commit_get_logmsg(&logmsg, commit);
2364 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2372 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2374 static const struct got_error *
2375 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2376 struct got_repository *repo, const char *path, int show_patch,
2377 int diff_context, struct got_reflist_head *refs)
2379 const struct got_error *err = NULL;
2380 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2382 time_t committer_time;
2383 const char *author, *committer;
2384 char *refs_str = NULL;
2385 struct got_reflist_entry *re;
2387 SIMPLEQ_FOREACH(re, refs, entry) {
2390 struct got_tag_object *tag = NULL;
2393 name = got_ref_get_name(re->ref);
2394 if (strcmp(name, GOT_REF_HEAD) == 0)
2396 if (strncmp(name, "refs/", 5) == 0)
2398 if (strncmp(name, "got/", 4) == 0)
2400 if (strncmp(name, "heads/", 6) == 0)
2402 if (strncmp(name, "remotes/", 8) == 0)
2404 if (strncmp(name, "tags/", 5) == 0) {
2405 err = got_object_open_as_tag(&tag, repo, re->id);
2407 if (err->code != GOT_ERR_OBJ_TYPE)
2409 /* Ref points at something other than a tag. */
2414 cmp = got_object_id_cmp(tag ?
2415 got_object_tag_get_object_id(tag) : re->id, id);
2417 got_object_tag_close(tag);
2421 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2423 err = got_error_from_errno("asprintf");
2429 err = got_object_id_str(&id_str, id);
2433 printf(GOT_COMMIT_SEP_STR);
2434 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2435 refs_str ? refs_str : "", refs_str ? ")" : "");
2440 printf("from: %s\n", got_object_commit_get_author(commit));
2441 committer_time = got_object_commit_get_committer_time(commit);
2442 datestr = get_datestr(&committer_time, datebuf);
2444 printf("date: %s UTC\n", datestr);
2445 author = got_object_commit_get_author(commit);
2446 committer = got_object_commit_get_committer(commit);
2447 if (strcmp(author, committer) != 0)
2448 printf("via: %s\n", committer);
2449 if (got_object_commit_get_nparents(commit) > 1) {
2450 const struct got_object_id_queue *parent_ids;
2451 struct got_object_qid *qid;
2453 parent_ids = got_object_commit_get_parent_ids(commit);
2454 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2455 err = got_object_id_str(&id_str, qid->id);
2458 printf("parent %d: %s\n", n++, id_str);
2463 err = got_object_commit_get_logmsg(&logmsg0, commit);
2469 line = strsep(&logmsg, "\n");
2471 printf(" %s\n", line);
2476 err = print_patch(commit, id, path, diff_context, repo);
2481 if (fflush(stdout) != 0 && err == NULL)
2482 err = got_error_from_errno("fflush");
2486 static const struct got_error *
2487 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2488 const char *path, int show_patch, const char *search_pattern,
2489 int diff_context, int limit, int log_branches,
2490 struct got_reflist_head *refs)
2492 const struct got_error *err;
2493 struct got_commit_graph *graph;
2497 if (search_pattern &&
2498 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2499 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2501 err = got_commit_graph_open(&graph, path, !log_branches);
2504 err = got_commit_graph_iter_start(graph, root_id, repo,
2505 check_cancelled, NULL);
2509 struct got_commit_object *commit;
2510 struct got_object_id *id;
2512 if (sigint_received || sigpipe_received)
2515 err = got_commit_graph_iter_next(&id, graph, repo,
2516 check_cancelled, NULL);
2518 if (err->code == GOT_ERR_ITER_COMPLETED)
2525 err = got_object_open_as_commit(&commit, repo, id);
2529 if (search_pattern) {
2530 err = match_logmsg(&have_match, id, commit, ®ex);
2532 got_object_commit_close(commit);
2535 if (have_match == 0) {
2536 got_object_commit_close(commit);
2541 err = print_commit(commit, id, repo, path, show_patch,
2542 diff_context, refs);
2543 got_object_commit_close(commit);
2544 if (err || (limit && --limit == 0))
2550 got_commit_graph_close(graph);
2557 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2558 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2563 get_default_log_limit(void)
2565 const char *got_default_log_limit;
2569 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2570 if (got_default_log_limit == NULL)
2572 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2578 static const struct got_error *
2579 cmd_log(int argc, char *argv[])
2581 const struct got_error *error;
2582 struct got_repository *repo = NULL;
2583 struct got_worktree *worktree = NULL;
2584 struct got_commit_object *commit = NULL;
2585 struct got_object_id *id = NULL;
2586 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2587 const char *start_commit = NULL, *search_pattern = NULL;
2588 int diff_context = -1, ch;
2589 int show_patch = 0, limit = 0, log_branches = 0;
2591 struct got_reflist_head refs;
2593 SIMPLEQ_INIT(&refs);
2596 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2602 limit = get_default_log_limit();
2604 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2610 start_commit = optarg;
2613 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2616 err(1, "-C option %s", errstr);
2619 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2621 err(1, "-l option %s", errstr);
2627 repo_path = realpath(optarg, NULL);
2628 if (repo_path == NULL)
2629 return got_error_from_errno2("realpath",
2631 got_path_strip_trailing_slashes(repo_path);
2634 search_pattern = optarg;
2645 if (diff_context == -1)
2647 else if (!show_patch)
2648 errx(1, "-C reguires -p");
2650 cwd = getcwd(NULL, 0);
2652 error = got_error_from_errno("getcwd");
2656 error = got_worktree_open(&worktree, cwd);
2657 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2664 error = got_error_from_errno("strdup");
2667 } else if (argc == 1) {
2669 error = got_worktree_resolve_path(&path, worktree,
2674 path = strdup(argv[0]);
2676 error = got_error_from_errno("strdup");
2683 if (repo_path == NULL) {
2684 repo_path = worktree ?
2685 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2687 if (repo_path == NULL) {
2688 error = got_error_from_errno("strdup");
2692 error = got_repo_open(&repo, repo_path, NULL);
2696 error = apply_unveil(got_repo_get_path(repo), 1,
2697 worktree ? got_worktree_get_root_path(worktree) : NULL);
2701 if (start_commit == NULL) {
2702 struct got_reference *head_ref;
2703 error = got_ref_open(&head_ref, repo,
2704 worktree ? got_worktree_get_head_ref_name(worktree)
2708 error = got_ref_resolve(&id, repo, head_ref);
2709 got_ref_close(head_ref);
2712 error = got_object_open_as_commit(&commit, repo, id);
2714 struct got_reference *ref;
2715 error = got_ref_open(&ref, repo, start_commit, 0);
2716 if (error == NULL) {
2718 error = got_ref_resolve(&id, repo, ref);
2722 error = got_object_get_type(&obj_type, repo, id);
2725 if (obj_type == GOT_OBJ_TYPE_TAG) {
2726 struct got_tag_object *tag;
2727 error = got_object_open_as_tag(&tag, repo, id);
2730 if (got_object_tag_get_object_type(tag) !=
2731 GOT_OBJ_TYPE_COMMIT) {
2732 got_object_tag_close(tag);
2733 error = got_error(GOT_ERR_OBJ_TYPE);
2737 id = got_object_id_dup(
2738 got_object_tag_get_object_id(tag));
2740 error = got_error_from_errno(
2741 "got_object_id_dup");
2742 got_object_tag_close(tag);
2745 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2746 error = got_error(GOT_ERR_OBJ_TYPE);
2749 error = got_object_open_as_commit(&commit, repo, id);
2753 if (commit == NULL) {
2754 error = got_repo_match_object_id_prefix(&id,
2755 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2764 const char *prefix = got_worktree_get_path_prefix(worktree);
2766 if (asprintf(&p, "%s%s%s", prefix,
2767 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2768 error = got_error_from_errno("asprintf");
2771 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2774 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2779 path = in_repo_path;
2782 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2786 error = print_commits(id, repo, path, show_patch, search_pattern,
2787 diff_context, limit, log_branches, &refs);
2794 got_worktree_close(worktree);
2796 const struct got_error *repo_error;
2797 repo_error = got_repo_close(repo);
2801 got_ref_list_free(&refs);
2808 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2809 "[-w] [object1 object2 | path]\n", getprogname());
2813 struct print_diff_arg {
2814 struct got_repository *repo;
2815 struct got_worktree *worktree;
2820 int ignore_whitespace;
2823 static const struct got_error *
2824 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2825 const char *path, struct got_object_id *blob_id,
2826 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2827 int dirfd, const char *de_name)
2829 struct print_diff_arg *a = arg;
2830 const struct got_error *err = NULL;
2831 struct got_blob_object *blob1 = NULL;
2834 char *abspath = NULL, *label1 = NULL;
2837 if (a->diff_staged) {
2838 if (staged_status != GOT_STATUS_MODIFY &&
2839 staged_status != GOT_STATUS_ADD &&
2840 staged_status != GOT_STATUS_DELETE)
2843 if (staged_status == GOT_STATUS_DELETE)
2845 if (status == GOT_STATUS_NONEXISTENT)
2846 return got_error_set_errno(ENOENT, path);
2847 if (status != GOT_STATUS_MODIFY &&
2848 status != GOT_STATUS_ADD &&
2849 status != GOT_STATUS_DELETE &&
2850 status != GOT_STATUS_CONFLICT)
2854 if (!a->header_shown) {
2855 printf("diff %s %s%s\n", a->id_str,
2856 got_worktree_get_root_path(a->worktree),
2857 a->diff_staged ? " (staged changes)" : "");
2858 a->header_shown = 1;
2861 if (a->diff_staged) {
2862 const char *label1 = NULL, *label2 = NULL;
2863 switch (staged_status) {
2864 case GOT_STATUS_MODIFY:
2868 case GOT_STATUS_ADD:
2871 case GOT_STATUS_DELETE:
2875 return got_error(GOT_ERR_FILE_STATUS);
2877 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2878 label1, label2, a->diff_context, a->ignore_whitespace,
2882 if (staged_status == GOT_STATUS_ADD ||
2883 staged_status == GOT_STATUS_MODIFY) {
2885 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2889 err = got_object_id_str(&id_str, staged_blob_id);
2892 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2893 err = got_error_from_errno("asprintf");
2898 } else if (status != GOT_STATUS_ADD) {
2899 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2904 if (status != GOT_STATUS_DELETE) {
2905 if (asprintf(&abspath, "%s/%s",
2906 got_worktree_get_root_path(a->worktree), path) == -1) {
2907 err = got_error_from_errno("asprintf");
2912 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2914 err = got_error_from_errno2("openat", abspath);
2918 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2920 err = got_error_from_errno2("open", abspath);
2924 if (fstat(fd, &sb) == -1) {
2925 err = got_error_from_errno2("fstat", abspath);
2928 f2 = fdopen(fd, "r");
2930 err = got_error_from_errno2("fdopen", abspath);
2937 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2938 a->diff_context, a->ignore_whitespace, stdout);
2941 got_object_blob_close(blob1);
2942 if (f2 && fclose(f2) == EOF && err == NULL)
2943 err = got_error_from_errno("fclose");
2944 if (fd != -1 && close(fd) == -1 && err == NULL)
2945 err = got_error_from_errno("close");
2950 static const struct got_error *
2951 cmd_diff(int argc, char *argv[])
2953 const struct got_error *error;
2954 struct got_repository *repo = NULL;
2955 struct got_worktree *worktree = NULL;
2956 char *cwd = NULL, *repo_path = NULL;
2957 struct got_object_id *id1 = NULL, *id2 = NULL;
2958 const char *id_str1 = NULL, *id_str2 = NULL;
2959 char *label1 = NULL, *label2 = NULL;
2961 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2966 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2971 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2974 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2977 err(1, "-C option %s", errstr);
2980 repo_path = realpath(optarg, NULL);
2981 if (repo_path == NULL)
2982 return got_error_from_errno2("realpath",
2984 got_path_strip_trailing_slashes(repo_path);
2990 ignore_whitespace = 1;
3001 cwd = getcwd(NULL, 0);
3003 error = got_error_from_errno("getcwd");
3006 error = got_worktree_open(&worktree, cwd);
3007 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3010 if (worktree == NULL) {
3011 error = got_error(GOT_ERR_NOT_WORKTREE);
3016 "-r option can't be used when diffing a work tree");
3017 repo_path = strdup(got_worktree_get_repo_path(worktree));
3018 if (repo_path == NULL) {
3019 error = got_error_from_errno("strdup");
3023 error = got_worktree_resolve_path(&path, worktree,
3030 error = got_error_from_errno("strdup");
3034 } else if (argc == 2) {
3036 errx(1, "-s option can't be used when diffing "
3037 "objects in repository");
3040 if (worktree && repo_path == NULL) {
3042 strdup(got_worktree_get_repo_path(worktree));
3043 if (repo_path == NULL) {
3044 error = got_error_from_errno("strdup");
3051 if (repo_path == NULL) {
3052 repo_path = getcwd(NULL, 0);
3053 if (repo_path == NULL)
3054 return got_error_from_errno("getcwd");
3057 error = got_repo_open(&repo, repo_path, NULL);
3062 error = apply_unveil(got_repo_get_path(repo), 1,
3063 worktree ? got_worktree_get_root_path(worktree) : NULL);
3068 struct print_diff_arg arg;
3069 struct got_pathlist_head paths;
3074 error = got_object_id_str(&id_str,
3075 got_worktree_get_base_commit_id(worktree));
3079 arg.worktree = worktree;
3080 arg.diff_context = diff_context;
3081 arg.id_str = id_str;
3082 arg.header_shown = 0;
3083 arg.diff_staged = diff_staged;
3084 arg.ignore_whitespace = ignore_whitespace;
3086 error = got_pathlist_append(&paths, path, NULL);
3090 error = got_worktree_status(worktree, &paths, repo, print_diff,
3091 &arg, check_cancelled, NULL);
3093 got_pathlist_free(&paths);
3097 error = got_repo_match_object_id(&id1, &label1, id_str1,
3098 GOT_OBJ_TYPE_ANY, 1, repo);
3102 error = got_repo_match_object_id(&id2, &label2, id_str2,
3103 GOT_OBJ_TYPE_ANY, 1, repo);
3107 error = got_object_get_type(&type1, repo, id1);
3111 error = got_object_get_type(&type2, repo, id2);
3115 if (type1 != type2) {
3116 error = got_error(GOT_ERR_OBJ_TYPE);
3121 case GOT_OBJ_TYPE_BLOB:
3122 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3123 diff_context, ignore_whitespace, repo, stdout);
3125 case GOT_OBJ_TYPE_TREE:
3126 error = got_diff_objects_as_trees(id1, id2, "", "",
3127 diff_context, ignore_whitespace, repo, stdout);
3129 case GOT_OBJ_TYPE_COMMIT:
3130 printf("diff %s %s\n", label1, label2);
3131 error = got_diff_objects_as_commits(id1, id2, diff_context,
3132 ignore_whitespace, repo, stdout);
3135 error = got_error(GOT_ERR_OBJ_TYPE);
3144 got_worktree_close(worktree);
3146 const struct got_error *repo_error;
3147 repo_error = got_repo_close(repo);
3158 "usage: %s blame [-c commit] [-r repository-path] path\n",
3167 char datebuf[11]; /* YYYY-MM-DD + NUL */
3170 struct blame_cb_args {
3171 struct blame_line *lines;
3175 off_t *line_offsets;
3177 struct got_repository *repo;
3180 static const struct got_error *
3181 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3183 const struct got_error *err = NULL;
3184 struct blame_cb_args *a = arg;
3185 struct blame_line *bline;
3187 size_t linesize = 0;
3188 struct got_commit_object *commit = NULL;
3191 time_t committer_time;
3193 if (nlines != a->nlines ||
3194 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3195 return got_error(GOT_ERR_RANGE);
3197 if (sigint_received)
3198 return got_error(GOT_ERR_ITER_COMPLETED);
3201 return NULL; /* no change in this commit */
3203 /* Annotate this line. */
3204 bline = &a->lines[lineno - 1];
3205 if (bline->annotated)
3207 err = got_object_id_str(&bline->id_str, id);
3211 err = got_object_open_as_commit(&commit, a->repo, id);
3215 bline->committer = strdup(got_object_commit_get_committer(commit));
3216 if (bline->committer == NULL) {
3217 err = got_error_from_errno("strdup");
3221 committer_time = got_object_commit_get_committer_time(commit);
3222 if (localtime_r(&committer_time, &tm) == NULL)
3223 return got_error_from_errno("localtime_r");
3224 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3225 &tm) >= sizeof(bline->datebuf)) {
3226 err = got_error(GOT_ERR_NO_SPACE);
3229 bline->annotated = 1;
3231 /* Print lines annotated so far. */
3232 bline = &a->lines[a->lineno_cur - 1];
3233 if (!bline->annotated)
3236 offset = a->line_offsets[a->lineno_cur - 1];
3237 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3238 err = got_error_from_errno("fseeko");
3242 while (bline->annotated) {
3243 char *smallerthan, *at, *nl, *committer;
3246 if (getline(&line, &linesize, a->f) == -1) {
3248 err = got_error_from_errno("getline");
3252 committer = bline->committer;
3253 smallerthan = strchr(committer, '<');
3254 if (smallerthan && smallerthan[1] != '\0')
3255 committer = smallerthan + 1;
3256 at = strchr(committer, '@');
3259 len = strlen(committer);
3261 committer[8] = '\0';
3263 nl = strchr(line, '\n');
3266 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3267 bline->id_str, bline->datebuf, committer, line);
3270 bline = &a->lines[a->lineno_cur - 1];
3274 got_object_commit_close(commit);
3279 static const struct got_error *
3280 cmd_blame(int argc, char *argv[])
3282 const struct got_error *error;
3283 struct got_repository *repo = NULL;
3284 struct got_worktree *worktree = NULL;
3285 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3286 struct got_object_id *obj_id = NULL;
3287 struct got_object_id *commit_id = NULL;
3288 struct got_blob_object *blob = NULL;
3289 char *commit_id_str = NULL;
3290 struct blame_cb_args bca;
3291 int ch, obj_type, i;
3294 memset(&bca, 0, sizeof(bca));
3297 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3302 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3305 commit_id_str = optarg;
3308 repo_path = realpath(optarg, NULL);
3309 if (repo_path == NULL)
3310 return got_error_from_errno2("realpath",
3312 got_path_strip_trailing_slashes(repo_path);
3328 cwd = getcwd(NULL, 0);
3330 error = got_error_from_errno("getcwd");
3333 if (repo_path == NULL) {
3334 error = got_worktree_open(&worktree, cwd);
3335 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3341 strdup(got_worktree_get_repo_path(worktree));
3342 if (repo_path == NULL)
3343 error = got_error_from_errno("strdup");
3347 repo_path = strdup(cwd);
3348 if (repo_path == NULL) {
3349 error = got_error_from_errno("strdup");
3355 error = got_repo_open(&repo, repo_path, NULL);
3359 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3364 const char *prefix = got_worktree_get_path_prefix(worktree);
3365 char *p, *worktree_subdir = cwd +
3366 strlen(got_worktree_get_root_path(worktree));
3367 if (asprintf(&p, "%s%s%s%s%s",
3368 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3369 worktree_subdir, worktree_subdir[0] ? "/" : "",
3371 error = got_error_from_errno("asprintf");
3374 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3377 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3382 if (commit_id_str == NULL) {
3383 struct got_reference *head_ref;
3384 error = got_ref_open(&head_ref, repo, worktree ?
3385 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3388 error = got_ref_resolve(&commit_id, repo, head_ref);
3389 got_ref_close(head_ref);
3393 error = got_repo_match_object_id(&commit_id, NULL,
3394 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3399 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3403 error = got_object_get_type(&obj_type, repo, obj_id);
3407 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3408 error = got_error(GOT_ERR_OBJ_TYPE);
3412 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3415 bca.f = got_opentemp();
3416 if (bca.f == NULL) {
3417 error = got_error_from_errno("got_opentemp");
3420 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3421 &bca.line_offsets, bca.f, blob);
3422 if (error || bca.nlines == 0)
3425 /* Don't include \n at EOF in the blame line count. */
3426 if (bca.line_offsets[bca.nlines - 1] == filesize)
3429 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3430 if (bca.lines == NULL) {
3431 error = got_error_from_errno("calloc");
3435 bca.nlines_prec = 0;
3443 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3444 check_cancelled, NULL);
3452 got_object_blob_close(blob);
3454 got_worktree_close(worktree);
3456 const struct got_error *repo_error;
3457 repo_error = got_repo_close(repo);
3462 for (i = 0; i < bca.nlines; i++) {
3463 struct blame_line *bline = &bca.lines[i];
3464 free(bline->id_str);
3465 free(bline->committer);
3469 free(bca.line_offsets);
3470 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3471 error = got_error_from_errno("fclose");
3479 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3485 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3486 const char *root_path)
3488 int is_root_path = (strcmp(path, root_path) == 0);
3489 const char *modestr = "";
3490 mode_t mode = got_tree_entry_get_mode(te);
3492 path += strlen(root_path);
3493 while (path[0] == '/')
3496 if (got_object_tree_entry_is_submodule(te))
3498 else if (S_ISLNK(mode))
3500 else if (S_ISDIR(mode))
3502 else if (mode & S_IXUSR)
3505 printf("%s%s%s%s%s\n", id ? id : "", path,
3506 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3509 static const struct got_error *
3510 print_tree(const char *path, struct got_object_id *commit_id,
3511 int show_ids, int recurse, const char *root_path,
3512 struct got_repository *repo)
3514 const struct got_error *err = NULL;
3515 struct got_object_id *tree_id = NULL;
3516 struct got_tree_object *tree = NULL;
3519 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3523 err = got_object_open_as_tree(&tree, repo, tree_id);
3526 nentries = got_object_tree_get_nentries(tree);
3527 for (i = 0; i < nentries; i++) {
3528 struct got_tree_entry *te;
3531 if (sigint_received || sigpipe_received)
3534 te = got_object_tree_get_entry(tree, i);
3537 err = got_object_id_str(&id_str,
3538 got_tree_entry_get_id(te));
3541 if (asprintf(&id, "%s ", id_str) == -1) {
3542 err = got_error_from_errno("asprintf");
3548 print_entry(te, id, path, root_path);
3551 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3553 if (asprintf(&child_path, "%s%s%s", path,
3554 path[0] == '/' && path[1] == '\0' ? "" : "/",
3555 got_tree_entry_get_name(te)) == -1) {
3556 err = got_error_from_errno("asprintf");
3559 err = print_tree(child_path, commit_id, show_ids, 1,
3568 got_object_tree_close(tree);
3573 static const struct got_error *
3574 cmd_tree(int argc, char *argv[])
3576 const struct got_error *error;
3577 struct got_repository *repo = NULL;
3578 struct got_worktree *worktree = NULL;
3580 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3581 struct got_object_id *commit_id = NULL;
3582 char *commit_id_str = NULL;
3583 int show_ids = 0, recurse = 0;
3587 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3592 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3595 commit_id_str = optarg;
3598 repo_path = realpath(optarg, NULL);
3599 if (repo_path == NULL)
3600 return got_error_from_errno2("realpath",
3602 got_path_strip_trailing_slashes(repo_path);
3626 cwd = getcwd(NULL, 0);
3628 error = got_error_from_errno("getcwd");
3631 if (repo_path == NULL) {
3632 error = got_worktree_open(&worktree, cwd);
3633 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3639 strdup(got_worktree_get_repo_path(worktree));
3640 if (repo_path == NULL)
3641 error = got_error_from_errno("strdup");
3645 repo_path = strdup(cwd);
3646 if (repo_path == NULL) {
3647 error = got_error_from_errno("strdup");
3653 error = got_repo_open(&repo, repo_path, NULL);
3657 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3663 char *p, *worktree_subdir = cwd +
3664 strlen(got_worktree_get_root_path(worktree));
3665 if (asprintf(&p, "%s/%s",
3666 got_worktree_get_path_prefix(worktree),
3667 worktree_subdir) == -1) {
3668 error = got_error_from_errno("asprintf");
3671 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3678 if (in_repo_path == NULL) {
3679 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3684 if (commit_id_str == NULL) {
3685 struct got_reference *head_ref;
3686 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3689 error = got_ref_resolve(&commit_id, repo, head_ref);
3690 got_ref_close(head_ref);
3694 error = got_repo_match_object_id(&commit_id, NULL,
3695 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3700 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3701 in_repo_path, repo);
3708 got_worktree_close(worktree);
3710 const struct got_error *repo_error;
3711 repo_error = got_repo_close(repo);
3721 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3725 static const struct got_error *
3726 print_status(void *arg, unsigned char status, unsigned char staged_status,
3727 const char *path, struct got_object_id *blob_id,
3728 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3729 int dirfd, const char *de_name)
3731 if (status == staged_status && (status == GOT_STATUS_DELETE))
3732 status = GOT_STATUS_NO_CHANGE;
3733 printf("%c%c %s\n", status, staged_status, path);
3737 static const struct got_error *
3738 cmd_status(int argc, char *argv[])
3740 const struct got_error *error = NULL;
3741 struct got_repository *repo = NULL;
3742 struct got_worktree *worktree = NULL;
3744 struct got_pathlist_head paths;
3745 struct got_pathlist_entry *pe;
3750 while ((ch = getopt(argc, argv, "")) != -1) {
3762 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3766 cwd = getcwd(NULL, 0);
3768 error = got_error_from_errno("getcwd");
3772 error = got_worktree_open(&worktree, cwd);
3776 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3781 error = apply_unveil(got_repo_get_path(repo), 1,
3782 got_worktree_get_root_path(worktree));
3786 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3790 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3791 check_cancelled, NULL);
3793 TAILQ_FOREACH(pe, &paths, entry)
3794 free((char *)pe->path);
3795 got_pathlist_free(&paths);
3804 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3809 static const struct got_error *
3810 list_refs(struct got_repository *repo)
3812 static const struct got_error *err = NULL;
3813 struct got_reflist_head refs;
3814 struct got_reflist_entry *re;
3816 SIMPLEQ_INIT(&refs);
3817 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3821 SIMPLEQ_FOREACH(re, &refs, entry) {
3823 refstr = got_ref_to_str(re->ref);
3825 return got_error_from_errno("got_ref_to_str");
3826 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3830 got_ref_list_free(&refs);
3834 static const struct got_error *
3835 delete_ref(struct got_repository *repo, const char *refname)
3837 const struct got_error *err = NULL;
3838 struct got_reference *ref;
3840 err = got_ref_open(&ref, repo, refname, 0);
3844 err = got_ref_delete(ref, repo);
3849 static const struct got_error *
3850 add_ref(struct got_repository *repo, const char *refname, const char *target)
3852 const struct got_error *err = NULL;
3853 struct got_object_id *id;
3854 struct got_reference *ref = NULL;
3857 * Don't let the user create a reference name with a leading '-'.
3858 * While technically a valid reference name, this case is usually
3859 * an unintended typo.
3861 if (refname[0] == '-')
3862 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3864 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3867 struct got_reference *target_ref;
3869 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3871 err = got_ref_open(&target_ref, repo, target, 0);
3874 err = got_ref_resolve(&id, repo, target_ref);
3875 got_ref_close(target_ref);
3880 err = got_ref_alloc(&ref, refname, id);
3884 err = got_ref_write(ref, repo);
3892 static const struct got_error *
3893 add_symref(struct got_repository *repo, const char *refname, const char *target)
3895 const struct got_error *err = NULL;
3896 struct got_reference *ref = NULL;
3897 struct got_reference *target_ref = NULL;
3900 * Don't let the user create a reference name with a leading '-'.
3901 * While technically a valid reference name, this case is usually
3902 * an unintended typo.
3904 if (refname[0] == '-')
3905 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3907 err = got_ref_open(&target_ref, repo, target, 0);
3911 err = got_ref_alloc_symref(&ref, refname, target_ref);
3915 err = got_ref_write(ref, repo);
3918 got_ref_close(target_ref);
3924 static const struct got_error *
3925 cmd_ref(int argc, char *argv[])
3927 const struct got_error *error = NULL;
3928 struct got_repository *repo = NULL;
3929 struct got_worktree *worktree = NULL;
3930 char *cwd = NULL, *repo_path = NULL;
3931 int ch, do_list = 0, create_symref = 0;
3932 const char *delref = NULL;
3934 /* TODO: Add -s option for adding symbolic references. */
3935 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3941 repo_path = realpath(optarg, NULL);
3942 if (repo_path == NULL)
3943 return got_error_from_errno2("realpath",
3945 got_path_strip_trailing_slashes(repo_path);
3959 if (do_list && delref)
3960 errx(1, "-l and -d options are mutually exclusive\n");
3965 if (do_list || delref) {
3967 errx(1, "-s option cannot be used together with the "
3968 "-l or -d options");
3971 } else if (argc != 2)
3976 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3980 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3981 "sendfd unveil", NULL) == -1)
3985 cwd = getcwd(NULL, 0);
3987 error = got_error_from_errno("getcwd");
3991 if (repo_path == NULL) {
3992 error = got_worktree_open(&worktree, cwd);
3993 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3999 strdup(got_worktree_get_repo_path(worktree));
4000 if (repo_path == NULL)
4001 error = got_error_from_errno("strdup");
4005 repo_path = strdup(cwd);
4006 if (repo_path == NULL) {
4007 error = got_error_from_errno("strdup");
4013 error = got_repo_open(&repo, repo_path, NULL);
4017 error = apply_unveil(got_repo_get_path(repo), do_list,
4018 worktree ? got_worktree_get_root_path(worktree) : NULL);
4023 error = list_refs(repo);
4025 error = delete_ref(repo, delref);
4026 else if (create_symref)
4027 error = add_symref(repo, argv[0], argv[1]);
4029 error = add_ref(repo, argv[0], argv[1]);
4032 got_repo_close(repo);
4034 got_worktree_close(worktree);
4044 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4045 "[name]\n", getprogname());
4049 static const struct got_error *
4050 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4051 struct got_reference *ref)
4053 const struct got_error *err = NULL;
4054 const char *refname, *marker = " ";
4057 refname = got_ref_get_name(ref);
4058 if (worktree && strcmp(refname,
4059 got_worktree_get_head_ref_name(worktree)) == 0) {
4060 struct got_object_id *id = NULL;
4062 err = got_ref_resolve(&id, repo, ref);
4065 if (got_object_id_cmp(id,
4066 got_worktree_get_base_commit_id(worktree)) == 0)
4073 if (strncmp(refname, "refs/heads/", 11) == 0)
4075 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4078 refstr = got_ref_to_str(ref);
4080 return got_error_from_errno("got_ref_to_str");
4082 printf("%s%s: %s\n", marker, refname, refstr);
4087 static const struct got_error *
4088 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4090 const char *refname;
4092 if (worktree == NULL)
4093 return got_error(GOT_ERR_NOT_WORKTREE);
4095 refname = got_worktree_get_head_ref_name(worktree);
4097 if (strncmp(refname, "refs/heads/", 11) == 0)
4099 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4102 printf("%s\n", refname);
4107 static const struct got_error *
4108 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4110 static const struct got_error *err = NULL;
4111 struct got_reflist_head refs;
4112 struct got_reflist_entry *re;
4113 struct got_reference *temp_ref = NULL;
4114 int rebase_in_progress, histedit_in_progress;
4116 SIMPLEQ_INIT(&refs);
4119 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4124 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4129 if (rebase_in_progress || histedit_in_progress) {
4130 err = got_ref_open(&temp_ref, repo,
4131 got_worktree_get_head_ref_name(worktree), 0);
4134 list_branch(repo, worktree, temp_ref);
4135 got_ref_close(temp_ref);
4139 err = got_ref_list(&refs, repo, "refs/heads",
4140 got_ref_cmp_by_name, NULL);
4144 SIMPLEQ_FOREACH(re, &refs, entry)
4145 list_branch(repo, worktree, re->ref);
4147 got_ref_list_free(&refs);
4151 static const struct got_error *
4152 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4153 const char *branch_name)
4155 const struct got_error *err = NULL;
4156 struct got_reference *ref = NULL;
4159 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4160 return got_error_from_errno("asprintf");
4162 err = got_ref_open(&ref, repo, refname, 0);
4167 strcmp(got_worktree_get_head_ref_name(worktree),
4168 got_ref_get_name(ref)) == 0) {
4169 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4170 "will not delete this work tree's current branch");
4174 err = got_ref_delete(ref, repo);
4182 static const struct got_error *
4183 add_branch(struct got_repository *repo, const char *branch_name,
4184 struct got_object_id *base_commit_id)
4186 const struct got_error *err = NULL;
4187 struct got_reference *ref = NULL;
4188 char *base_refname = NULL, *refname = NULL;
4191 * Don't let the user create a branch name with a leading '-'.
4192 * While technically a valid reference name, this case is usually
4193 * an unintended typo.
4195 if (branch_name[0] == '-')
4196 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4198 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4199 err = got_error_from_errno("asprintf");
4203 err = got_ref_open(&ref, repo, refname, 0);
4205 err = got_error(GOT_ERR_BRANCH_EXISTS);
4207 } else if (err->code != GOT_ERR_NOT_REF)
4210 err = got_ref_alloc(&ref, refname, base_commit_id);
4214 err = got_ref_write(ref, repo);
4223 static const struct got_error *
4224 cmd_branch(int argc, char *argv[])
4226 const struct got_error *error = NULL;
4227 struct got_repository *repo = NULL;
4228 struct got_worktree *worktree = NULL;
4229 char *cwd = NULL, *repo_path = NULL;
4230 int ch, do_list = 0, do_show = 0, do_update = 1;
4231 const char *delref = NULL, *commit_id_arg = NULL;
4232 struct got_reference *ref = NULL;
4233 struct got_pathlist_head paths;
4234 struct got_pathlist_entry *pe;
4235 struct got_object_id *commit_id = NULL;
4236 char *commit_id_str = NULL;
4240 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4243 commit_id_arg = optarg;
4249 repo_path = realpath(optarg, NULL);
4250 if (repo_path == NULL)
4251 return got_error_from_errno2("realpath",
4253 got_path_strip_trailing_slashes(repo_path);
4267 if (do_list && delref)
4268 errx(1, "-l and -d options are mutually exclusive\n");
4273 if (!do_list && !delref && argc == 0)
4276 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4277 errx(1, "-c option can only be used when creating a branch");
4279 if (do_list || delref) {
4282 } else if (!do_show && argc != 1)
4286 if (do_list || do_show) {
4287 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4291 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4292 "sendfd unveil", NULL) == -1)
4296 cwd = getcwd(NULL, 0);
4298 error = got_error_from_errno("getcwd");
4302 if (repo_path == NULL) {
4303 error = got_worktree_open(&worktree, cwd);
4304 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4310 strdup(got_worktree_get_repo_path(worktree));
4311 if (repo_path == NULL)
4312 error = got_error_from_errno("strdup");
4316 repo_path = strdup(cwd);
4317 if (repo_path == NULL) {
4318 error = got_error_from_errno("strdup");
4324 error = got_repo_open(&repo, repo_path, NULL);
4328 error = apply_unveil(got_repo_get_path(repo), do_list,
4329 worktree ? got_worktree_get_root_path(worktree) : NULL);
4334 error = show_current_branch(repo, worktree);
4336 error = list_branches(repo, worktree);
4338 error = delete_branch(repo, worktree, delref);
4340 if (commit_id_arg == NULL)
4341 commit_id_arg = worktree ?
4342 got_worktree_get_head_ref_name(worktree) :
4344 error = got_repo_match_object_id(&commit_id, NULL,
4345 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4348 error = add_branch(repo, argv[0], commit_id);
4351 if (worktree && do_update) {
4352 int did_something = 0;
4353 char *branch_refname = NULL;
4355 error = got_object_id_str(&commit_id_str, commit_id);
4358 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4362 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4364 error = got_error_from_errno("asprintf");
4367 error = got_ref_open(&ref, repo, branch_refname, 0);
4368 free(branch_refname);
4371 error = switch_head_ref(ref, commit_id, worktree,
4375 error = got_worktree_set_base_commit_id(worktree, repo,
4379 error = got_worktree_checkout_files(worktree, &paths,
4380 repo, update_progress, &did_something,
4381 check_cancelled, NULL);
4385 printf("Updated to commit %s\n", commit_id_str);
4392 got_repo_close(repo);
4394 got_worktree_close(worktree);
4398 free(commit_id_str);
4399 TAILQ_FOREACH(pe, &paths, entry)
4400 free((char *)pe->path);
4401 got_pathlist_free(&paths);
4410 "usage: %s tag [-c commit] [-r repository] [-l] "
4411 "[-m message] name\n", getprogname());
4416 static const struct got_error *
4417 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4419 const struct got_error *err = NULL;
4420 struct got_reflist_entry *re, *se, *new;
4421 struct got_object_id *re_id, *se_id;
4422 struct got_tag_object *re_tag, *se_tag;
4423 time_t re_time, se_time;
4425 SIMPLEQ_FOREACH(re, tags, entry) {
4426 se = SIMPLEQ_FIRST(sorted);
4428 err = got_reflist_entry_dup(&new, re);
4431 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4434 err = got_ref_resolve(&re_id, repo, re->ref);
4437 err = got_object_open_as_tag(&re_tag, repo, re_id);
4441 re_time = got_object_tag_get_tagger_time(re_tag);
4442 got_object_tag_close(re_tag);
4446 err = got_ref_resolve(&se_id, repo, re->ref);
4449 err = got_object_open_as_tag(&se_tag, repo, se_id);
4453 se_time = got_object_tag_get_tagger_time(se_tag);
4454 got_object_tag_close(se_tag);
4456 if (se_time > re_time) {
4457 err = got_reflist_entry_dup(&new, re);
4460 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4463 se = SIMPLEQ_NEXT(se, entry);
4472 static const struct got_error *
4473 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4475 static const struct got_error *err = NULL;
4476 struct got_reflist_head refs;
4477 struct got_reflist_entry *re;
4479 SIMPLEQ_INIT(&refs);
4481 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4485 SIMPLEQ_FOREACH(re, &refs, entry) {
4486 const char *refname;
4487 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4491 struct got_object_id *id;
4492 struct got_tag_object *tag;
4493 struct got_commit_object *commit = NULL;
4495 refname = got_ref_get_name(re->ref);
4496 if (strncmp(refname, "refs/tags/", 10) != 0)
4499 refstr = got_ref_to_str(re->ref);
4500 if (refstr == NULL) {
4501 err = got_error_from_errno("got_ref_to_str");
4504 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4507 err = got_ref_resolve(&id, repo, re->ref);
4510 err = got_object_open_as_tag(&tag, repo, id);
4512 if (err->code != GOT_ERR_OBJ_TYPE) {
4516 /* "lightweight" tag */
4517 err = got_object_open_as_commit(&commit, repo, id);
4522 tagger = got_object_commit_get_committer(commit);
4524 got_object_commit_get_committer_time(commit);
4525 err = got_object_id_str(&id_str, id);
4531 tagger = got_object_tag_get_tagger(tag);
4532 tagger_time = got_object_tag_get_tagger_time(tag);
4533 err = got_object_id_str(&id_str,
4534 got_object_tag_get_object_id(tag));
4538 printf("from: %s\n", tagger);
4539 datestr = get_datestr(&tagger_time, datebuf);
4541 printf("date: %s UTC\n", datestr);
4543 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4545 switch (got_object_tag_get_object_type(tag)) {
4546 case GOT_OBJ_TYPE_BLOB:
4547 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4550 case GOT_OBJ_TYPE_TREE:
4551 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4554 case GOT_OBJ_TYPE_COMMIT:
4555 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4558 case GOT_OBJ_TYPE_TAG:
4559 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4568 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4571 got_object_commit_close(commit);
4573 tagmsg0 = strdup(got_object_tag_get_message(tag));
4574 got_object_tag_close(tag);
4575 if (tagmsg0 == NULL) {
4576 err = got_error_from_errno("strdup");
4583 line = strsep(&tagmsg, "\n");
4585 printf(" %s\n", line);
4590 got_ref_list_free(&refs);
4594 static const struct got_error *
4595 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4596 const char *tag_name, const char *repo_path)
4598 const struct got_error *err = NULL;
4599 char *template = NULL, *initial_content = NULL;
4600 char *editor = NULL;
4603 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4604 err = got_error_from_errno("asprintf");
4608 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4609 commit_id_str, tag_name) == -1) {
4610 err = got_error_from_errno("asprintf");
4614 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4618 dprintf(fd, initial_content);
4621 err = get_editor(&editor);
4624 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4626 free(initial_content);
4630 /* Editor is done; we can now apply unveil(2) */
4632 err = apply_unveil(repo_path, 0, NULL);
4641 static const struct got_error *
4642 add_tag(struct got_repository *repo, const char *tag_name,
4643 const char *commit_arg, const char *tagmsg_arg)
4645 const struct got_error *err = NULL;
4646 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4647 char *label = NULL, *commit_id_str = NULL;
4648 struct got_reference *ref = NULL;
4649 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4650 char *tagmsg_path = NULL, *tag_id_str = NULL;
4651 int preserve_tagmsg = 0;
4654 * Don't let the user create a tag name with a leading '-'.
4655 * While technically a valid reference name, this case is usually
4656 * an unintended typo.
4658 if (tag_name[0] == '-')
4659 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4661 err = get_author(&tagger, repo);
4665 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4666 GOT_OBJ_TYPE_COMMIT, 1, repo);
4670 err = got_object_id_str(&commit_id_str, commit_id);
4674 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4675 refname = strdup(tag_name);
4676 if (refname == NULL) {
4677 err = got_error_from_errno("strdup");
4681 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4682 err = got_error_from_errno("asprintf");
4686 err = got_ref_open(&ref, repo, refname, 0);
4688 err = got_error(GOT_ERR_TAG_EXISTS);
4690 } else if (err->code != GOT_ERR_NOT_REF)
4693 if (tagmsg_arg == NULL) {
4694 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4695 tag_name, got_repo_get_path(repo));
4697 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4698 tagmsg_path != NULL)
4699 preserve_tagmsg = 1;
4704 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4705 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4708 preserve_tagmsg = 1;
4712 err = got_ref_alloc(&ref, refname, tag_id);
4715 preserve_tagmsg = 1;
4719 err = got_ref_write(ref, repo);
4722 preserve_tagmsg = 1;
4726 err = got_object_id_str(&tag_id_str, tag_id);
4729 preserve_tagmsg = 1;
4732 printf("Created tag %s\n", tag_id_str);
4734 if (preserve_tagmsg) {
4735 fprintf(stderr, "%s: tag message preserved in %s\n",
4736 getprogname(), tagmsg_path);
4737 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4738 err = got_error_from_errno2("unlink", tagmsg_path);
4743 free(commit_id_str);
4751 static const struct got_error *
4752 cmd_tag(int argc, char *argv[])
4754 const struct got_error *error = NULL;
4755 struct got_repository *repo = NULL;
4756 struct got_worktree *worktree = NULL;
4757 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4758 char *gitconfig_path = NULL;
4759 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4760 int ch, do_list = 0;
4762 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4765 commit_id_arg = optarg;
4771 repo_path = realpath(optarg, NULL);
4772 if (repo_path == NULL)
4773 return got_error_from_errno2("realpath",
4775 got_path_strip_trailing_slashes(repo_path);
4790 if (commit_id_arg != NULL)
4791 errx(1, "-c option can only be used when creating a tag");
4793 errx(1, "-l and -m options are mutually exclusive");
4796 } else if (argc != 1)
4803 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4807 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4808 "sendfd unveil", NULL) == -1)
4812 cwd = getcwd(NULL, 0);
4814 error = got_error_from_errno("getcwd");
4818 if (repo_path == NULL) {
4819 error = got_worktree_open(&worktree, cwd);
4820 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4826 strdup(got_worktree_get_repo_path(worktree));
4827 if (repo_path == NULL)
4828 error = got_error_from_errno("strdup");
4832 repo_path = strdup(cwd);
4833 if (repo_path == NULL) {
4834 error = got_error_from_errno("strdup");
4841 error = got_repo_open(&repo, repo_path, NULL);
4844 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4847 error = list_tags(repo, worktree);
4849 error = get_gitconfig_path(&gitconfig_path);
4852 error = got_repo_open(&repo, repo_path, gitconfig_path);
4857 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4862 if (commit_id_arg == NULL) {
4863 struct got_reference *head_ref;
4864 struct got_object_id *commit_id;
4865 error = got_ref_open(&head_ref, repo,
4866 worktree ? got_worktree_get_head_ref_name(worktree)
4870 error = got_ref_resolve(&commit_id, repo, head_ref);
4871 got_ref_close(head_ref);
4874 error = got_object_id_str(&commit_id_str, commit_id);
4880 error = add_tag(repo, tag_name,
4881 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4885 got_repo_close(repo);
4887 got_worktree_close(worktree);
4890 free(gitconfig_path);
4891 free(commit_id_str);
4898 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4903 static const struct got_error *
4904 add_progress(void *arg, unsigned char status, const char *path)
4906 while (path[0] == '/')
4908 printf("%c %s\n", status, path);
4912 static const struct got_error *
4913 cmd_add(int argc, char *argv[])
4915 const struct got_error *error = NULL;
4916 struct got_repository *repo = NULL;
4917 struct got_worktree *worktree = NULL;
4919 struct got_pathlist_head paths;
4920 struct got_pathlist_entry *pe;
4921 int ch, can_recurse = 0, no_ignores = 0;
4925 while ((ch = getopt(argc, argv, "IR")) != -1) {
4943 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4950 cwd = getcwd(NULL, 0);
4952 error = got_error_from_errno("getcwd");
4956 error = got_worktree_open(&worktree, cwd);
4960 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4965 error = apply_unveil(got_repo_get_path(repo), 1,
4966 got_worktree_get_root_path(worktree));
4970 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4974 if (!can_recurse && no_ignores) {
4975 error = got_error_msg(GOT_ERR_BAD_PATH,
4976 "disregarding ignores requires -R option");
4984 TAILQ_FOREACH(pe, &paths, entry) {
4985 if (asprintf(&ondisk_path, "%s/%s",
4986 got_worktree_get_root_path(worktree),
4988 error = got_error_from_errno("asprintf");
4991 if (lstat(ondisk_path, &sb) == -1) {
4992 if (errno == ENOENT) {
4996 error = got_error_from_errno2("lstat",
5002 if (S_ISDIR(sb.st_mode)) {
5003 error = got_error_msg(GOT_ERR_BAD_PATH,
5004 "adding directories requires -R option");
5010 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5011 NULL, repo, no_ignores);
5014 got_repo_close(repo);
5016 got_worktree_close(worktree);
5017 TAILQ_FOREACH(pe, &paths, entry)
5018 free((char *)pe->path);
5019 got_pathlist_free(&paths);
5027 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5032 static const struct got_error *
5033 print_remove_status(void *arg, unsigned char status,
5034 unsigned char staged_status, const char *path)
5036 while (path[0] == '/')
5038 if (status == GOT_STATUS_NONEXISTENT)
5040 if (status == staged_status && (status == GOT_STATUS_DELETE))
5041 status = GOT_STATUS_NO_CHANGE;
5042 printf("%c%c %s\n", status, staged_status, path);
5046 static const struct got_error *
5047 cmd_remove(int argc, char *argv[])
5049 const struct got_error *error = NULL;
5050 struct got_worktree *worktree = NULL;
5051 struct got_repository *repo = NULL;
5053 struct got_pathlist_head paths;
5054 struct got_pathlist_entry *pe;
5055 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5059 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5062 delete_local_mods = 1;
5080 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5087 cwd = getcwd(NULL, 0);
5089 error = got_error_from_errno("getcwd");
5092 error = got_worktree_open(&worktree, cwd);
5096 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5101 error = apply_unveil(got_repo_get_path(repo), 1,
5102 got_worktree_get_root_path(worktree));
5106 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5113 TAILQ_FOREACH(pe, &paths, entry) {
5114 if (asprintf(&ondisk_path, "%s/%s",
5115 got_worktree_get_root_path(worktree),
5117 error = got_error_from_errno("asprintf");
5120 if (lstat(ondisk_path, &sb) == -1) {
5121 if (errno == ENOENT) {
5125 error = got_error_from_errno2("lstat",
5131 if (S_ISDIR(sb.st_mode)) {
5132 error = got_error_msg(GOT_ERR_BAD_PATH,
5133 "removing directories requires -R option");
5139 error = got_worktree_schedule_delete(worktree, &paths,
5140 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5143 got_repo_close(repo);
5145 got_worktree_close(worktree);
5146 TAILQ_FOREACH(pe, &paths, entry)
5147 free((char *)pe->path);
5148 got_pathlist_free(&paths);
5156 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5157 "path ...\n", getprogname());
5161 static const struct got_error *
5162 revert_progress(void *arg, unsigned char status, const char *path)
5164 if (status == GOT_STATUS_UNVERSIONED)
5167 while (path[0] == '/')
5169 printf("%c %s\n", status, path);
5173 struct choose_patch_arg {
5174 FILE *patch_script_file;
5178 static const struct got_error *
5179 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5180 int nchanges, const char *action)
5183 size_t linesize = 0;
5187 case GOT_STATUS_ADD:
5188 printf("A %s\n%s this addition? [y/n] ", path, action);
5190 case GOT_STATUS_DELETE:
5191 printf("D %s\n%s this deletion? [y/n] ", path, action);
5193 case GOT_STATUS_MODIFY:
5194 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5195 return got_error_from_errno("fseek");
5196 printf(GOT_COMMIT_SEP_STR);
5197 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5199 if (ferror(patch_file))
5200 return got_error_from_errno("getline");
5201 printf(GOT_COMMIT_SEP_STR);
5202 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5203 path, n, nchanges, action);
5206 return got_error_path(path, GOT_ERR_FILE_STATUS);
5212 static const struct got_error *
5213 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5214 FILE *patch_file, int n, int nchanges)
5216 const struct got_error *err = NULL;
5218 size_t linesize = 0;
5221 struct choose_patch_arg *a = arg;
5223 *choice = GOT_PATCH_CHOICE_NONE;
5225 if (a->patch_script_file) {
5227 err = show_change(status, path, patch_file, n, nchanges,
5231 linelen = getline(&line, &linesize, a->patch_script_file);
5232 if (linelen == -1) {
5233 if (ferror(a->patch_script_file))
5234 return got_error_from_errno("getline");
5237 nl = strchr(line, '\n');
5240 if (strcmp(line, "y") == 0) {
5241 *choice = GOT_PATCH_CHOICE_YES;
5243 } else if (strcmp(line, "n") == 0) {
5244 *choice = GOT_PATCH_CHOICE_NO;
5246 } else if (strcmp(line, "q") == 0 &&
5247 status == GOT_STATUS_MODIFY) {
5248 *choice = GOT_PATCH_CHOICE_QUIT;
5251 printf("invalid response '%s'\n", line);
5256 while (resp != 'y' && resp != 'n' && resp != 'q') {
5257 err = show_change(status, path, patch_file, n, nchanges,
5264 if (status == GOT_STATUS_MODIFY) {
5265 if (resp != 'y' && resp != 'n' && resp != 'q') {
5266 printf("invalid response '%c'\n", resp);
5269 } else if (resp != 'y' && resp != 'n') {
5270 printf("invalid response '%c'\n", resp);
5276 *choice = GOT_PATCH_CHOICE_YES;
5277 else if (resp == 'n')
5278 *choice = GOT_PATCH_CHOICE_NO;
5279 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5280 *choice = GOT_PATCH_CHOICE_QUIT;
5286 static const struct got_error *
5287 cmd_revert(int argc, char *argv[])
5289 const struct got_error *error = NULL;
5290 struct got_worktree *worktree = NULL;
5291 struct got_repository *repo = NULL;
5292 char *cwd = NULL, *path = NULL;
5293 struct got_pathlist_head paths;
5294 struct got_pathlist_entry *pe;
5295 int ch, can_recurse = 0, pflag = 0;
5296 FILE *patch_script_file = NULL;
5297 const char *patch_script_path = NULL;
5298 struct choose_patch_arg cpa;
5302 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5308 patch_script_path = optarg;
5323 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5324 "unveil", NULL) == -1)
5329 if (patch_script_path && !pflag)
5330 errx(1, "-F option can only be used together with -p option");
5332 cwd = getcwd(NULL, 0);
5334 error = got_error_from_errno("getcwd");
5337 error = got_worktree_open(&worktree, cwd);
5341 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5346 if (patch_script_path) {
5347 patch_script_file = fopen(patch_script_path, "r");
5348 if (patch_script_file == NULL) {
5349 error = got_error_from_errno2("fopen",
5354 error = apply_unveil(got_repo_get_path(repo), 1,
5355 got_worktree_get_root_path(worktree));
5359 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5366 TAILQ_FOREACH(pe, &paths, entry) {
5367 if (asprintf(&ondisk_path, "%s/%s",
5368 got_worktree_get_root_path(worktree),
5370 error = got_error_from_errno("asprintf");
5373 if (lstat(ondisk_path, &sb) == -1) {
5374 if (errno == ENOENT) {
5378 error = got_error_from_errno2("lstat",
5384 if (S_ISDIR(sb.st_mode)) {
5385 error = got_error_msg(GOT_ERR_BAD_PATH,
5386 "reverting directories requires -R option");
5392 cpa.patch_script_file = patch_script_file;
5393 cpa.action = "revert";
5394 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5395 pflag ? choose_patch : NULL, &cpa, repo);
5397 if (patch_script_file && fclose(patch_script_file) == EOF &&
5399 error = got_error_from_errno2("fclose", patch_script_path);
5401 got_repo_close(repo);
5403 got_worktree_close(worktree);
5412 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5417 struct collect_commit_logmsg_arg {
5418 const char *cmdline_log;
5420 const char *worktree_path;
5421 const char *branch_name;
5422 const char *repo_path;
5427 static const struct got_error *
5428 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5431 char *initial_content = NULL;
5432 struct got_pathlist_entry *pe;
5433 const struct got_error *err = NULL;
5434 char *template = NULL;
5435 struct collect_commit_logmsg_arg *a = arg;
5439 /* if a message was specified on the command line, just use it */
5440 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5441 len = strlen(a->cmdline_log) + 1;
5442 *logmsg = malloc(len + 1);
5443 if (*logmsg == NULL)
5444 return got_error_from_errno("malloc");
5445 strlcpy(*logmsg, a->cmdline_log, len);
5449 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5450 return got_error_from_errno("asprintf");
5452 if (asprintf(&initial_content,
5453 "\n# changes to be committed on branch %s:\n",
5454 a->branch_name) == -1)
5455 return got_error_from_errno("asprintf");
5457 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5461 dprintf(fd, initial_content);
5463 TAILQ_FOREACH(pe, commitable_paths, entry) {
5464 struct got_commitable *ct = pe->data;
5465 dprintf(fd, "# %c %s\n",
5466 got_commitable_get_status(ct),
5467 got_commitable_get_path(ct));
5471 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5473 free(initial_content);
5476 /* Editor is done; we can now apply unveil(2) */
5478 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5487 static const struct got_error *
5488 cmd_commit(int argc, char *argv[])
5490 const struct got_error *error = NULL;
5491 struct got_worktree *worktree = NULL;
5492 struct got_repository *repo = NULL;
5493 char *cwd = NULL, *id_str = NULL;
5494 struct got_object_id *id = NULL;
5495 const char *logmsg = NULL;
5496 struct collect_commit_logmsg_arg cl_arg;
5497 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5498 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5499 struct got_pathlist_head paths;
5502 cl_arg.logmsg_path = NULL;
5504 while ((ch = getopt(argc, argv, "m:")) != -1) {
5519 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5520 "unveil", NULL) == -1)
5523 cwd = getcwd(NULL, 0);
5525 error = got_error_from_errno("getcwd");
5528 error = got_worktree_open(&worktree, cwd);
5532 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5535 if (rebase_in_progress) {
5536 error = got_error(GOT_ERR_REBASING);
5540 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5545 error = get_gitconfig_path(&gitconfig_path);
5548 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5553 error = get_author(&author, repo);
5558 * unveil(2) traverses exec(2); if an editor is used we have
5559 * to apply unveil after the log message has been written.
5561 if (logmsg == NULL || strlen(logmsg) == 0)
5562 error = get_editor(&editor);
5564 error = apply_unveil(got_repo_get_path(repo), 0,
5565 got_worktree_get_root_path(worktree));
5569 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5573 cl_arg.editor = editor;
5574 cl_arg.cmdline_log = logmsg;
5575 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5576 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5577 if (!histedit_in_progress) {
5578 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5579 error = got_error(GOT_ERR_COMMIT_BRANCH);
5582 cl_arg.branch_name += 11;
5584 cl_arg.repo_path = got_repo_get_path(repo);
5585 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5586 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5588 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5589 cl_arg.logmsg_path != NULL)
5590 preserve_logmsg = 1;
5594 error = got_object_id_str(&id_str, id);
5597 printf("Created commit %s\n", id_str);
5599 if (preserve_logmsg) {
5600 fprintf(stderr, "%s: log message preserved in %s\n",
5601 getprogname(), cl_arg.logmsg_path);
5602 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5604 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5605 free(cl_arg.logmsg_path);
5607 got_repo_close(repo);
5609 got_worktree_close(worktree);
5612 free(gitconfig_path);
5619 usage_cherrypick(void)
5621 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5625 static const struct got_error *
5626 cmd_cherrypick(int argc, char *argv[])
5628 const struct got_error *error = NULL;
5629 struct got_worktree *worktree = NULL;
5630 struct got_repository *repo = NULL;
5631 char *cwd = NULL, *commit_id_str = NULL;
5632 struct got_object_id *commit_id = NULL;
5633 struct got_commit_object *commit = NULL;
5634 struct got_object_qid *pid;
5635 struct got_reference *head_ref = NULL;
5636 int ch, did_something = 0;
5638 while ((ch = getopt(argc, argv, "")) != -1) {
5650 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5651 "unveil", NULL) == -1)
5657 cwd = getcwd(NULL, 0);
5659 error = got_error_from_errno("getcwd");
5662 error = got_worktree_open(&worktree, cwd);
5666 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5671 error = apply_unveil(got_repo_get_path(repo), 0,
5672 got_worktree_get_root_path(worktree));
5676 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5677 GOT_OBJ_TYPE_COMMIT, repo);
5678 if (error != NULL) {
5679 struct got_reference *ref;
5680 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5682 error = got_ref_open(&ref, repo, argv[0], 0);
5685 error = got_ref_resolve(&commit_id, repo, ref);
5690 error = got_object_id_str(&commit_id_str, commit_id);
5694 error = got_ref_open(&head_ref, repo,
5695 got_worktree_get_head_ref_name(worktree), 0);
5699 error = check_same_branch(commit_id, head_ref, NULL, repo);
5701 if (error->code != GOT_ERR_ANCESTRY)
5705 error = got_error(GOT_ERR_SAME_BRANCH);
5709 error = got_object_open_as_commit(&commit, repo, commit_id);
5712 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5713 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5714 commit_id, repo, update_progress, &did_something, check_cancelled,
5720 printf("Merged commit %s\n", commit_id_str);
5723 got_object_commit_close(commit);
5724 free(commit_id_str);
5726 got_ref_close(head_ref);
5728 got_worktree_close(worktree);
5730 got_repo_close(repo);
5737 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5741 static const struct got_error *
5742 cmd_backout(int argc, char *argv[])
5744 const struct got_error *error = NULL;
5745 struct got_worktree *worktree = NULL;
5746 struct got_repository *repo = NULL;
5747 char *cwd = NULL, *commit_id_str = NULL;
5748 struct got_object_id *commit_id = NULL;
5749 struct got_commit_object *commit = NULL;
5750 struct got_object_qid *pid;
5751 struct got_reference *head_ref = NULL;
5752 int ch, did_something = 0;
5754 while ((ch = getopt(argc, argv, "")) != -1) {
5766 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5767 "unveil", NULL) == -1)
5773 cwd = getcwd(NULL, 0);
5775 error = got_error_from_errno("getcwd");
5778 error = got_worktree_open(&worktree, cwd);
5782 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5787 error = apply_unveil(got_repo_get_path(repo), 0,
5788 got_worktree_get_root_path(worktree));
5792 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5793 GOT_OBJ_TYPE_COMMIT, repo);
5794 if (error != NULL) {
5795 struct got_reference *ref;
5796 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5798 error = got_ref_open(&ref, repo, argv[0], 0);
5801 error = got_ref_resolve(&commit_id, repo, ref);
5806 error = got_object_id_str(&commit_id_str, commit_id);
5810 error = got_ref_open(&head_ref, repo,
5811 got_worktree_get_head_ref_name(worktree), 0);
5815 error = check_same_branch(commit_id, head_ref, NULL, repo);
5819 error = got_object_open_as_commit(&commit, repo, commit_id);
5822 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5824 error = got_error(GOT_ERR_ROOT_COMMIT);
5828 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5829 update_progress, &did_something, check_cancelled, NULL);
5834 printf("Backed out commit %s\n", commit_id_str);
5837 got_object_commit_close(commit);
5838 free(commit_id_str);
5840 got_ref_close(head_ref);
5842 got_worktree_close(worktree);
5844 got_repo_close(repo);
5851 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5857 trim_logmsg(char *logmsg, int limit)
5862 len = strlen(logmsg);
5866 nl = strchr(logmsg, '\n');
5871 static const struct got_error *
5872 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5874 const struct got_error *err;
5875 char *logmsg0 = NULL;
5878 err = got_object_commit_get_logmsg(&logmsg0, commit);
5883 while (isspace((unsigned char)s[0]))
5886 *logmsg = strdup(s);
5887 if (*logmsg == NULL) {
5888 err = got_error_from_errno("strdup");
5892 trim_logmsg(*logmsg, limit);
5898 static const struct got_error *
5899 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5901 const struct got_error *err;
5902 struct got_commit_object *commit = NULL;
5903 char *id_str = NULL, *logmsg = NULL;
5905 err = got_object_open_as_commit(&commit, repo, id);
5909 err = got_object_id_str(&id_str, id);
5915 err = get_short_logmsg(&logmsg, 42, commit);
5919 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5922 got_object_commit_close(commit);
5927 static const struct got_error *
5928 show_rebase_progress(struct got_commit_object *commit,
5929 struct got_object_id *old_id, struct got_object_id *new_id)
5931 const struct got_error *err;
5932 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5934 err = got_object_id_str(&old_id_str, old_id);
5939 err = got_object_id_str(&new_id_str, new_id);
5944 old_id_str[12] = '\0';
5946 new_id_str[12] = '\0';
5948 err = get_short_logmsg(&logmsg, 42, commit);
5952 printf("%s -> %s: %s\n", old_id_str,
5953 new_id_str ? new_id_str : "no-op change", logmsg);
5961 static const struct got_error *
5962 rebase_progress(void *arg, unsigned char status, const char *path)
5964 unsigned char *rebase_status = arg;
5966 while (path[0] == '/')
5968 printf("%c %s\n", status, path);
5970 if (*rebase_status == GOT_STATUS_CONFLICT)
5972 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5973 *rebase_status = status;
5977 static const struct got_error *
5978 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5979 struct got_reference *branch, struct got_reference *new_base_branch,
5980 struct got_reference *tmp_branch, struct got_repository *repo)
5982 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5983 return got_worktree_rebase_complete(worktree, fileindex,
5984 new_base_branch, tmp_branch, branch, repo);
5987 static const struct got_error *
5988 rebase_commit(struct got_pathlist_head *merged_paths,
5989 struct got_worktree *worktree, struct got_fileindex *fileindex,
5990 struct got_reference *tmp_branch,
5991 struct got_object_id *commit_id, struct got_repository *repo)
5993 const struct got_error *error;
5994 struct got_commit_object *commit;
5995 struct got_object_id *new_commit_id;
5997 error = got_object_open_as_commit(&commit, repo, commit_id);
6001 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6002 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6004 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6006 error = show_rebase_progress(commit, commit_id, NULL);
6008 error = show_rebase_progress(commit, commit_id, new_commit_id);
6009 free(new_commit_id);
6012 got_object_commit_close(commit);
6016 struct check_path_prefix_arg {
6017 const char *path_prefix;
6022 static const struct got_error *
6023 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6024 struct got_blob_object *blob2, struct got_object_id *id1,
6025 struct got_object_id *id2, const char *path1, const char *path2,
6026 mode_t mode1, mode_t mode2, struct got_repository *repo)
6028 struct check_path_prefix_arg *a = arg;
6030 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6031 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6032 return got_error(a->errcode);
6037 static const struct got_error *
6038 check_path_prefix(struct got_object_id *parent_id,
6039 struct got_object_id *commit_id, const char *path_prefix,
6040 int errcode, struct got_repository *repo)
6042 const struct got_error *err;
6043 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6044 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6045 struct check_path_prefix_arg cpp_arg;
6047 if (got_path_is_root_dir(path_prefix))
6050 err = got_object_open_as_commit(&commit, repo, commit_id);
6054 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6058 err = got_object_open_as_tree(&tree1, repo,
6059 got_object_commit_get_tree_id(parent_commit));
6063 err = got_object_open_as_tree(&tree2, repo,
6064 got_object_commit_get_tree_id(commit));
6068 cpp_arg.path_prefix = path_prefix;
6069 while (cpp_arg.path_prefix[0] == '/')
6070 cpp_arg.path_prefix++;
6071 cpp_arg.len = strlen(cpp_arg.path_prefix);
6072 cpp_arg.errcode = errcode;
6073 err = got_diff_tree(tree1, tree2, "", "", repo,
6074 check_path_prefix_in_diff, &cpp_arg, 0);
6077 got_object_tree_close(tree1);
6079 got_object_tree_close(tree2);
6081 got_object_commit_close(commit);
6083 got_object_commit_close(parent_commit);
6087 static const struct got_error *
6088 collect_commits(struct got_object_id_queue *commits,
6089 struct got_object_id *initial_commit_id,
6090 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6091 const char *path_prefix, int path_prefix_errcode,
6092 struct got_repository *repo)
6094 const struct got_error *err = NULL;
6095 struct got_commit_graph *graph = NULL;
6096 struct got_object_id *parent_id = NULL;
6097 struct got_object_qid *qid;
6098 struct got_object_id *commit_id = initial_commit_id;
6100 err = got_commit_graph_open(&graph, "/", 1);
6104 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6105 check_cancelled, NULL);
6108 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6109 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6110 check_cancelled, NULL);
6112 if (err->code == GOT_ERR_ITER_COMPLETED) {
6113 err = got_error_msg(GOT_ERR_ANCESTRY,
6114 "ran out of commits to rebase before "
6115 "youngest common ancestor commit has "
6120 err = check_path_prefix(parent_id, commit_id,
6121 path_prefix, path_prefix_errcode, repo);
6125 err = got_object_qid_alloc(&qid, commit_id);
6128 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6129 commit_id = parent_id;
6133 got_commit_graph_close(graph);
6137 static const struct got_error *
6138 cmd_rebase(int argc, char *argv[])
6140 const struct got_error *error = NULL;
6141 struct got_worktree *worktree = NULL;
6142 struct got_repository *repo = NULL;
6143 struct got_fileindex *fileindex = NULL;
6145 struct got_reference *branch = NULL;
6146 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6147 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6148 struct got_object_id *resume_commit_id = NULL;
6149 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6150 struct got_commit_object *commit = NULL;
6151 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6152 int histedit_in_progress = 0;
6153 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6154 struct got_object_id_queue commits;
6155 struct got_pathlist_head merged_paths;
6156 const struct got_object_id_queue *parent_ids;
6157 struct got_object_qid *qid, *pid;
6159 SIMPLEQ_INIT(&commits);
6160 TAILQ_INIT(&merged_paths);
6162 while ((ch = getopt(argc, argv, "ac")) != -1) {
6168 continue_rebase = 1;
6180 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6181 "unveil", NULL) == -1)
6184 if (abort_rebase && continue_rebase)
6186 else if (abort_rebase || continue_rebase) {
6189 } else if (argc != 1)
6192 cwd = getcwd(NULL, 0);
6194 error = got_error_from_errno("getcwd");
6197 error = got_worktree_open(&worktree, cwd);
6201 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6206 error = apply_unveil(got_repo_get_path(repo), 0,
6207 got_worktree_get_root_path(worktree));
6211 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6215 if (histedit_in_progress) {
6216 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6220 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6226 if (!rebase_in_progress) {
6227 error = got_error(GOT_ERR_NOT_REBASING);
6230 error = got_worktree_rebase_continue(&resume_commit_id,
6231 &new_base_branch, &tmp_branch, &branch, &fileindex,
6235 printf("Switching work tree to %s\n",
6236 got_ref_get_symref_target(new_base_branch));
6237 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6238 new_base_branch, update_progress, &did_something);
6241 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6242 goto done; /* nothing else to do */
6245 if (continue_rebase) {
6246 if (!rebase_in_progress) {
6247 error = got_error(GOT_ERR_NOT_REBASING);
6250 error = got_worktree_rebase_continue(&resume_commit_id,
6251 &new_base_branch, &tmp_branch, &branch, &fileindex,
6256 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6257 resume_commit_id, repo);
6261 yca_id = got_object_id_dup(resume_commit_id);
6262 if (yca_id == NULL) {
6263 error = got_error_from_errno("got_object_id_dup");
6267 error = got_ref_open(&branch, repo, argv[0], 0);
6272 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6276 if (!continue_rebase) {
6277 struct got_object_id *base_commit_id;
6279 base_commit_id = got_worktree_get_base_commit_id(worktree);
6280 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6281 base_commit_id, branch_head_commit_id, repo,
6282 check_cancelled, NULL);
6285 if (yca_id == NULL) {
6286 error = got_error_msg(GOT_ERR_ANCESTRY,
6287 "specified branch shares no common ancestry "
6288 "with work tree's branch");
6292 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6294 if (error->code != GOT_ERR_ANCESTRY)
6298 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6299 "specified branch resolves to a commit which "
6300 "is already contained in work tree's branch");
6303 error = got_worktree_rebase_prepare(&new_base_branch,
6304 &tmp_branch, &fileindex, worktree, branch, repo);
6309 commit_id = branch_head_commit_id;
6310 error = got_object_open_as_commit(&commit, repo, commit_id);
6314 parent_ids = got_object_commit_get_parent_ids(commit);
6315 pid = SIMPLEQ_FIRST(parent_ids);
6317 if (!continue_rebase) {
6319 error = got_worktree_rebase_abort(worktree, fileindex,
6320 repo, new_base_branch, update_progress,
6324 printf("Rebase of %s aborted\n",
6325 got_ref_get_name(branch));
6327 error = got_error(GOT_ERR_EMPTY_REBASE);
6330 error = collect_commits(&commits, commit_id, pid->id,
6331 yca_id, got_worktree_get_path_prefix(worktree),
6332 GOT_ERR_REBASE_PATH, repo);
6333 got_object_commit_close(commit);
6338 if (SIMPLEQ_EMPTY(&commits)) {
6339 if (continue_rebase) {
6340 error = rebase_complete(worktree, fileindex,
6341 branch, new_base_branch, tmp_branch, repo);
6344 /* Fast-forward the reference of the branch. */
6345 struct got_object_id *new_head_commit_id;
6347 error = got_ref_resolve(&new_head_commit_id, repo,
6351 error = got_object_id_str(&id_str, new_head_commit_id);
6352 printf("Forwarding %s to commit %s\n",
6353 got_ref_get_name(branch), id_str);
6355 error = got_ref_change_ref(branch,
6356 new_head_commit_id);
6363 SIMPLEQ_FOREACH(qid, &commits, entry) {
6364 commit_id = qid->id;
6365 parent_id = pid ? pid->id : yca_id;
6368 error = got_worktree_rebase_merge_files(&merged_paths,
6369 worktree, fileindex, parent_id, commit_id, repo,
6370 rebase_progress, &rebase_status, check_cancelled, NULL);
6374 if (rebase_status == GOT_STATUS_CONFLICT) {
6375 error = show_rebase_merge_conflict(qid->id, repo);
6378 got_worktree_rebase_pathlist_free(&merged_paths);
6382 error = rebase_commit(&merged_paths, worktree, fileindex,
6383 tmp_branch, commit_id, repo);
6384 got_worktree_rebase_pathlist_free(&merged_paths);
6389 if (rebase_status == GOT_STATUS_CONFLICT) {
6390 error = got_worktree_rebase_postpone(worktree, fileindex);
6393 error = got_error_msg(GOT_ERR_CONFLICTS,
6394 "conflicts must be resolved before rebasing can continue");
6396 error = rebase_complete(worktree, fileindex, branch,
6397 new_base_branch, tmp_branch, repo);
6399 got_object_id_queue_free(&commits);
6400 free(branch_head_commit_id);
6401 free(resume_commit_id);
6404 got_object_commit_close(commit);
6406 got_ref_close(branch);
6407 if (new_base_branch)
6408 got_ref_close(new_base_branch);
6410 got_ref_close(tmp_branch);
6412 got_worktree_close(worktree);
6414 got_repo_close(repo);
6419 usage_histedit(void)
6421 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6426 #define GOT_HISTEDIT_PICK 'p'
6427 #define GOT_HISTEDIT_EDIT 'e'
6428 #define GOT_HISTEDIT_FOLD 'f'
6429 #define GOT_HISTEDIT_DROP 'd'
6430 #define GOT_HISTEDIT_MESG 'm'
6432 static struct got_histedit_cmd {
6436 } got_histedit_cmds[] = {
6437 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6438 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6439 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6441 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6442 { GOT_HISTEDIT_MESG, "mesg",
6443 "single-line log message for commit above (open editor if empty)" },
6446 struct got_histedit_list_entry {
6447 TAILQ_ENTRY(got_histedit_list_entry) entry;
6448 struct got_object_id *commit_id;
6449 const struct got_histedit_cmd *cmd;
6452 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6454 static const struct got_error *
6455 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6456 FILE *f, struct got_repository *repo)
6458 const struct got_error *err = NULL;
6459 char *logmsg = NULL, *id_str = NULL;
6460 struct got_commit_object *commit = NULL;
6463 err = got_object_open_as_commit(&commit, repo, commit_id);
6467 err = get_short_logmsg(&logmsg, 34, commit);
6471 err = got_object_id_str(&id_str, commit_id);
6475 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6477 err = got_ferror(f, GOT_ERR_IO);
6480 got_object_commit_close(commit);
6486 static const struct got_error *
6487 histedit_write_commit_list(struct got_object_id_queue *commits,
6488 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6490 const struct got_error *err = NULL;
6491 struct got_object_qid *qid;
6493 if (SIMPLEQ_EMPTY(commits))
6494 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6496 SIMPLEQ_FOREACH(qid, commits, entry) {
6497 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6501 if (edit_logmsg_only) {
6502 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6504 err = got_ferror(f, GOT_ERR_IO);
6513 static const struct got_error *
6514 write_cmd_list(FILE *f, const char *branch_name,
6515 struct got_object_id_queue *commits)
6517 const struct got_error *err = NULL;
6520 struct got_object_qid *qid;
6522 qid = SIMPLEQ_FIRST(commits);
6523 err = got_object_id_str(&id_str, qid->id);
6528 "# Editing the history of branch '%s' starting at\n"
6530 "# Commits will be processed in order from top to "
6531 "bottom of this file.\n", branch_name, id_str);
6533 err = got_ferror(f, GOT_ERR_IO);
6537 n = fprintf(f, "# Available histedit commands:\n");
6539 err = got_ferror(f, GOT_ERR_IO);
6543 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6544 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6545 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6548 err = got_ferror(f, GOT_ERR_IO);
6557 static const struct got_error *
6558 histedit_syntax_error(int lineno)
6560 static char msg[42];
6563 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6565 if (ret == -1 || ret >= sizeof(msg))
6566 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6568 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6571 static const struct got_error *
6572 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6573 char *logmsg, struct got_repository *repo)
6575 const struct got_error *err;
6576 struct got_commit_object *folded_commit = NULL;
6577 char *id_str, *folded_logmsg = NULL;
6579 err = got_object_id_str(&id_str, hle->commit_id);
6583 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6587 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6590 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6591 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6592 folded_logmsg) == -1) {
6593 err = got_error_from_errno("asprintf");
6597 got_object_commit_close(folded_commit);
6599 free(folded_logmsg);
6603 static struct got_histedit_list_entry *
6604 get_folded_commits(struct got_histedit_list_entry *hle)
6606 struct got_histedit_list_entry *prev, *folded = NULL;
6608 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6609 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6610 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6611 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6613 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6619 static const struct got_error *
6620 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6621 struct got_repository *repo)
6623 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6624 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6625 const struct got_error *err = NULL;
6626 struct got_commit_object *commit = NULL;
6628 struct got_histedit_list_entry *folded = NULL;
6630 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6634 folded = get_folded_commits(hle);
6636 while (folded != hle) {
6637 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6638 folded = TAILQ_NEXT(folded, entry);
6641 err = append_folded_commit_msg(&new_msg, folded,
6647 folded = TAILQ_NEXT(folded, entry);
6651 err = got_object_id_str(&id_str, hle->commit_id);
6654 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6657 if (asprintf(&new_msg,
6658 "%s\n# original log message of commit %s: %s",
6659 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6660 err = got_error_from_errno("asprintf");
6666 err = got_object_id_str(&id_str, hle->commit_id);
6670 err = got_opentemp_named_fd(&logmsg_path, &fd,
6671 GOT_TMPDIR_STR "/got-logmsg");
6675 dprintf(fd, logmsg);
6678 err = get_editor(&editor);
6682 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6684 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6686 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6689 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6690 err = got_error_from_errno2("unlink", logmsg_path);
6696 got_object_commit_close(commit);
6700 static const struct got_error *
6701 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6702 FILE *f, struct got_repository *repo)
6704 const struct got_error *err = NULL;
6705 char *line = NULL, *p, *end;
6709 const struct got_histedit_cmd *cmd;
6710 struct got_object_id *commit_id = NULL;
6711 struct got_histedit_list_entry *hle = NULL;
6714 len = getline(&line, &size, f);
6716 const struct got_error *getline_err;
6719 getline_err = got_error_from_errno("getline");
6720 err = got_ferror(f, getline_err->code);
6725 while (isspace((unsigned char)p[0]))
6727 if (p[0] == '#' || p[0] == '\0') {
6733 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6734 cmd = &got_histedit_cmds[i];
6735 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6736 isspace((unsigned char)p[strlen(cmd->name)])) {
6737 p += strlen(cmd->name);
6740 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6745 if (i == nitems(got_histedit_cmds)) {
6746 err = histedit_syntax_error(lineno);
6749 while (isspace((unsigned char)p[0]))
6751 if (cmd->code == GOT_HISTEDIT_MESG) {
6752 if (hle == NULL || hle->logmsg != NULL) {
6753 err = got_error(GOT_ERR_HISTEDIT_CMD);
6757 err = histedit_edit_logmsg(hle, repo);
6761 hle->logmsg = strdup(p);
6762 if (hle->logmsg == NULL) {
6763 err = got_error_from_errno("strdup");
6772 while (end[0] && !isspace((unsigned char)end[0]))
6776 err = got_object_resolve_id_str(&commit_id, repo, p);
6778 /* override error code */
6779 err = histedit_syntax_error(lineno);
6783 hle = malloc(sizeof(*hle));
6785 err = got_error_from_errno("malloc");
6789 hle->commit_id = commit_id;
6794 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6802 static const struct got_error *
6803 histedit_check_script(struct got_histedit_list *histedit_cmds,
6804 struct got_object_id_queue *commits, struct got_repository *repo)
6806 const struct got_error *err = NULL;
6807 struct got_object_qid *qid;
6808 struct got_histedit_list_entry *hle;
6809 static char msg[92];
6812 if (TAILQ_EMPTY(histedit_cmds))
6813 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6814 "histedit script contains no commands");
6815 if (SIMPLEQ_EMPTY(commits))
6816 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6818 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6819 struct got_histedit_list_entry *hle2;
6820 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6823 if (got_object_id_cmp(hle->commit_id,
6824 hle2->commit_id) != 0)
6826 err = got_object_id_str(&id_str, hle->commit_id);
6829 snprintf(msg, sizeof(msg), "commit %s is listed "
6830 "more than once in histedit script", id_str);
6832 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6836 SIMPLEQ_FOREACH(qid, commits, entry) {
6837 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6838 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6842 err = got_object_id_str(&id_str, qid->id);
6845 snprintf(msg, sizeof(msg),
6846 "commit %s missing from histedit script", id_str);
6848 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6852 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6853 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6854 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6855 "last commit in histedit script cannot be folded");
6860 static const struct got_error *
6861 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6862 const char *path, struct got_object_id_queue *commits,
6863 struct got_repository *repo)
6865 const struct got_error *err = NULL;
6869 err = get_editor(&editor);
6873 if (spawn_editor(editor, path) == -1) {
6874 err = got_error_from_errno("failed spawning editor");
6878 f = fopen(path, "r");
6880 err = got_error_from_errno("fopen");
6883 err = histedit_parse_list(histedit_cmds, f, repo);
6887 err = histedit_check_script(histedit_cmds, commits, repo);
6889 if (f && fclose(f) != 0 && err == NULL)
6890 err = got_error_from_errno("fclose");
6895 static const struct got_error *
6896 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6897 struct got_object_id_queue *, const char *, const char *,
6898 struct got_repository *);
6900 static const struct got_error *
6901 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6902 struct got_object_id_queue *commits, const char *branch_name,
6903 int edit_logmsg_only, struct got_repository *repo)
6905 const struct got_error *err;
6909 err = got_opentemp_named(&path, &f, "got-histedit");
6913 err = write_cmd_list(f, branch_name, commits);
6917 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6921 if (edit_logmsg_only) {
6923 err = histedit_parse_list(histedit_cmds, f, repo);
6925 if (fclose(f) != 0) {
6926 err = got_error_from_errno("fclose");
6930 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6932 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6933 err->code != GOT_ERR_HISTEDIT_CMD)
6935 err = histedit_edit_list_retry(histedit_cmds, err,
6936 commits, path, branch_name, repo);
6940 if (f && fclose(f) != 0 && err == NULL)
6941 err = got_error_from_errno("fclose");
6942 if (path && unlink(path) != 0 && err == NULL)
6943 err = got_error_from_errno2("unlink", path);
6948 static const struct got_error *
6949 histedit_save_list(struct got_histedit_list *histedit_cmds,
6950 struct got_worktree *worktree, struct got_repository *repo)
6952 const struct got_error *err = NULL;
6955 struct got_histedit_list_entry *hle;
6956 struct got_commit_object *commit = NULL;
6958 err = got_worktree_get_histedit_script_path(&path, worktree);
6962 f = fopen(path, "w");
6964 err = got_error_from_errno2("fopen", path);
6967 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6968 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6974 int n = fprintf(f, "%c %s\n",
6975 GOT_HISTEDIT_MESG, hle->logmsg);
6977 err = got_ferror(f, GOT_ERR_IO);
6983 if (f && fclose(f) != 0 && err == NULL)
6984 err = got_error_from_errno("fclose");
6987 got_object_commit_close(commit);
6992 histedit_free_list(struct got_histedit_list *histedit_cmds)
6994 struct got_histedit_list_entry *hle;
6996 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6997 TAILQ_REMOVE(histedit_cmds, hle, entry);
7002 static const struct got_error *
7003 histedit_load_list(struct got_histedit_list *histedit_cmds,
7004 const char *path, struct got_repository *repo)
7006 const struct got_error *err = NULL;
7009 f = fopen(path, "r");
7011 err = got_error_from_errno2("fopen", path);
7015 err = histedit_parse_list(histedit_cmds, f, repo);
7017 if (f && fclose(f) != 0 && err == NULL)
7018 err = got_error_from_errno("fclose");
7022 static const struct got_error *
7023 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7024 const struct got_error *edit_err, struct got_object_id_queue *commits,
7025 const char *path, const char *branch_name, struct got_repository *repo)
7027 const struct got_error *err = NULL, *prev_err = edit_err;
7030 while (resp != 'c' && resp != 'r' && resp != 'a') {
7031 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7032 "or (a)bort: ", getprogname(), prev_err->msg);
7037 histedit_free_list(histedit_cmds);
7038 err = histedit_run_editor(histedit_cmds, path, commits,
7041 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7042 err->code != GOT_ERR_HISTEDIT_CMD)
7049 } else if (resp == 'r') {
7050 histedit_free_list(histedit_cmds);
7051 err = histedit_edit_script(histedit_cmds,
7052 commits, branch_name, 0, repo);
7054 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7055 err->code != GOT_ERR_HISTEDIT_CMD)
7062 } else if (resp == 'a') {
7063 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7066 printf("invalid response '%c'\n", resp);
7072 static const struct got_error *
7073 histedit_complete(struct got_worktree *worktree,
7074 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7075 struct got_reference *branch, struct got_repository *repo)
7077 printf("Switching work tree to %s\n",
7078 got_ref_get_symref_target(branch));
7079 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7083 static const struct got_error *
7084 show_histedit_progress(struct got_commit_object *commit,
7085 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7087 const struct got_error *err;
7088 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7090 err = got_object_id_str(&old_id_str, hle->commit_id);
7095 err = got_object_id_str(&new_id_str, new_id);
7100 old_id_str[12] = '\0';
7102 new_id_str[12] = '\0';
7105 logmsg = strdup(hle->logmsg);
7106 if (logmsg == NULL) {
7107 err = got_error_from_errno("strdup");
7110 trim_logmsg(logmsg, 42);
7112 err = get_short_logmsg(&logmsg, 42, commit);
7117 switch (hle->cmd->code) {
7118 case GOT_HISTEDIT_PICK:
7119 case GOT_HISTEDIT_EDIT:
7120 printf("%s -> %s: %s\n", old_id_str,
7121 new_id_str ? new_id_str : "no-op change", logmsg);
7123 case GOT_HISTEDIT_DROP:
7124 case GOT_HISTEDIT_FOLD:
7125 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7137 static const struct got_error *
7138 histedit_commit(struct got_pathlist_head *merged_paths,
7139 struct got_worktree *worktree, struct got_fileindex *fileindex,
7140 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7141 struct got_repository *repo)
7143 const struct got_error *err;
7144 struct got_commit_object *commit;
7145 struct got_object_id *new_commit_id;
7147 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7148 && hle->logmsg == NULL) {
7149 err = histedit_edit_logmsg(hle, repo);
7154 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7158 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7159 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7162 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7164 err = show_histedit_progress(commit, hle, NULL);
7166 err = show_histedit_progress(commit, hle, new_commit_id);
7167 free(new_commit_id);
7170 got_object_commit_close(commit);
7174 static const struct got_error *
7175 histedit_skip_commit(struct got_histedit_list_entry *hle,
7176 struct got_worktree *worktree, struct got_repository *repo)
7178 const struct got_error *error;
7179 struct got_commit_object *commit;
7181 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7186 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7190 error = show_histedit_progress(commit, hle, NULL);
7191 got_object_commit_close(commit);
7195 static const struct got_error *
7196 check_local_changes(void *arg, unsigned char status,
7197 unsigned char staged_status, const char *path,
7198 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7199 struct got_object_id *commit_id, int dirfd, const char *de_name)
7201 int *have_local_changes = arg;
7204 case GOT_STATUS_ADD:
7205 case GOT_STATUS_DELETE:
7206 case GOT_STATUS_MODIFY:
7207 case GOT_STATUS_CONFLICT:
7208 *have_local_changes = 1;
7209 return got_error(GOT_ERR_CANCELLED);
7214 switch (staged_status) {
7215 case GOT_STATUS_ADD:
7216 case GOT_STATUS_DELETE:
7217 case GOT_STATUS_MODIFY:
7218 *have_local_changes = 1;
7219 return got_error(GOT_ERR_CANCELLED);
7227 static const struct got_error *
7228 cmd_histedit(int argc, char *argv[])
7230 const struct got_error *error = NULL;
7231 struct got_worktree *worktree = NULL;
7232 struct got_fileindex *fileindex = NULL;
7233 struct got_repository *repo = NULL;
7235 struct got_reference *branch = NULL;
7236 struct got_reference *tmp_branch = NULL;
7237 struct got_object_id *resume_commit_id = NULL;
7238 struct got_object_id *base_commit_id = NULL;
7239 struct got_object_id *head_commit_id = NULL;
7240 struct got_commit_object *commit = NULL;
7241 int ch, rebase_in_progress = 0, did_something;
7242 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7243 int edit_logmsg_only = 0;
7244 const char *edit_script_path = NULL;
7245 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7246 struct got_object_id_queue commits;
7247 struct got_pathlist_head merged_paths;
7248 const struct got_object_id_queue *parent_ids;
7249 struct got_object_qid *pid;
7250 struct got_histedit_list histedit_cmds;
7251 struct got_histedit_list_entry *hle;
7253 SIMPLEQ_INIT(&commits);
7254 TAILQ_INIT(&histedit_cmds);
7255 TAILQ_INIT(&merged_paths);
7257 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7266 edit_script_path = optarg;
7269 edit_logmsg_only = 1;
7281 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7282 "unveil", NULL) == -1)
7285 if (abort_edit && continue_edit)
7286 errx(1, "histedit's -a and -c options are mutually exclusive");
7287 if (edit_script_path && edit_logmsg_only)
7288 errx(1, "histedit's -F and -m options are mutually exclusive");
7289 if (abort_edit && edit_logmsg_only)
7290 errx(1, "histedit's -a and -m options are mutually exclusive");
7291 if (continue_edit && edit_logmsg_only)
7292 errx(1, "histedit's -c and -m options are mutually exclusive");
7297 * This command cannot apply unveil(2) in all cases because the
7298 * user may choose to run an editor to edit the histedit script
7299 * and to edit individual commit log messages.
7300 * unveil(2) traverses exec(2); if an editor is used we have to
7301 * apply unveil after edit script and log messages have been written.
7302 * XXX TODO: Make use of unveil(2) where possible.
7305 cwd = getcwd(NULL, 0);
7307 error = got_error_from_errno("getcwd");
7310 error = got_worktree_open(&worktree, cwd);
7314 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7319 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7322 if (rebase_in_progress) {
7323 error = got_error(GOT_ERR_REBASING);
7327 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7331 if (edit_in_progress && edit_logmsg_only) {
7332 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7333 "histedit operation is in progress in this "
7334 "work tree and must be continued or aborted "
7335 "before the -m option can be used");
7339 if (edit_in_progress && abort_edit) {
7340 error = got_worktree_histedit_continue(&resume_commit_id,
7341 &tmp_branch, &branch, &base_commit_id, &fileindex,
7345 printf("Switching work tree to %s\n",
7346 got_ref_get_symref_target(branch));
7347 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7348 branch, base_commit_id, update_progress, &did_something);
7351 printf("Histedit of %s aborted\n",
7352 got_ref_get_symref_target(branch));
7353 goto done; /* nothing else to do */
7354 } else if (abort_edit) {
7355 error = got_error(GOT_ERR_NOT_HISTEDIT);
7359 if (continue_edit) {
7362 if (!edit_in_progress) {
7363 error = got_error(GOT_ERR_NOT_HISTEDIT);
7367 error = got_worktree_get_histedit_script_path(&path, worktree);
7371 error = histedit_load_list(&histedit_cmds, path, repo);
7376 error = got_worktree_histedit_continue(&resume_commit_id,
7377 &tmp_branch, &branch, &base_commit_id, &fileindex,
7382 error = got_ref_resolve(&head_commit_id, repo, branch);
7386 error = got_object_open_as_commit(&commit, repo,
7390 parent_ids = got_object_commit_get_parent_ids(commit);
7391 pid = SIMPLEQ_FIRST(parent_ids);
7393 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7396 error = collect_commits(&commits, head_commit_id, pid->id,
7397 base_commit_id, got_worktree_get_path_prefix(worktree),
7398 GOT_ERR_HISTEDIT_PATH, repo);
7399 got_object_commit_close(commit);
7404 if (edit_in_progress) {
7405 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7409 error = got_ref_open(&branch, repo,
7410 got_worktree_get_head_ref_name(worktree), 0);
7414 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7415 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7416 "will not edit commit history of a branch outside "
7417 "the \"refs/heads/\" reference namespace");
7421 error = got_ref_resolve(&head_commit_id, repo, branch);
7422 got_ref_close(branch);
7427 error = got_object_open_as_commit(&commit, repo,
7431 parent_ids = got_object_commit_get_parent_ids(commit);
7432 pid = SIMPLEQ_FIRST(parent_ids);
7434 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7437 error = collect_commits(&commits, head_commit_id, pid->id,
7438 got_worktree_get_base_commit_id(worktree),
7439 got_worktree_get_path_prefix(worktree),
7440 GOT_ERR_HISTEDIT_PATH, repo);
7441 got_object_commit_close(commit);
7446 if (SIMPLEQ_EMPTY(&commits)) {
7447 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7451 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7452 &base_commit_id, &fileindex, worktree, repo);
7456 if (edit_script_path) {
7457 error = histedit_load_list(&histedit_cmds,
7458 edit_script_path, repo);
7460 got_worktree_histedit_abort(worktree, fileindex,
7461 repo, branch, base_commit_id,
7462 update_progress, &did_something);
7466 const char *branch_name;
7467 branch_name = got_ref_get_symref_target(branch);
7468 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7470 error = histedit_edit_script(&histedit_cmds, &commits,
7471 branch_name, edit_logmsg_only, repo);
7473 got_worktree_histedit_abort(worktree, fileindex,
7474 repo, branch, base_commit_id,
7475 update_progress, &did_something);
7481 error = histedit_save_list(&histedit_cmds, worktree,
7484 got_worktree_histedit_abort(worktree, fileindex,
7485 repo, branch, base_commit_id,
7486 update_progress, &did_something);
7492 error = histedit_check_script(&histedit_cmds, &commits, repo);
7496 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7497 if (resume_commit_id) {
7498 if (got_object_id_cmp(hle->commit_id,
7499 resume_commit_id) != 0)
7502 resume_commit_id = NULL;
7503 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7504 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7505 error = histedit_skip_commit(hle, worktree,
7510 struct got_pathlist_head paths;
7511 int have_changes = 0;
7514 error = got_pathlist_append(&paths, "", NULL);
7517 error = got_worktree_status(worktree, &paths,
7518 repo, check_local_changes, &have_changes,
7519 check_cancelled, NULL);
7520 got_pathlist_free(&paths);
7522 if (error->code != GOT_ERR_CANCELLED)
7524 if (sigint_received || sigpipe_received)
7528 error = histedit_commit(NULL, worktree,
7529 fileindex, tmp_branch, hle, repo);
7533 error = got_object_open_as_commit(
7534 &commit, repo, hle->commit_id);
7537 error = show_histedit_progress(commit,
7539 got_object_commit_close(commit);
7548 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7549 error = histedit_skip_commit(hle, worktree, repo);
7555 error = got_object_open_as_commit(&commit, repo,
7559 parent_ids = got_object_commit_get_parent_ids(commit);
7560 pid = SIMPLEQ_FIRST(parent_ids);
7562 error = got_worktree_histedit_merge_files(&merged_paths,
7563 worktree, fileindex, pid->id, hle->commit_id, repo,
7564 rebase_progress, &rebase_status, check_cancelled, NULL);
7567 got_object_commit_close(commit);
7570 if (rebase_status == GOT_STATUS_CONFLICT) {
7571 error = show_rebase_merge_conflict(hle->commit_id,
7575 got_worktree_rebase_pathlist_free(&merged_paths);
7579 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7581 error = got_object_id_str(&id_str, hle->commit_id);
7584 printf("Stopping histedit for amending commit %s\n",
7587 got_worktree_rebase_pathlist_free(&merged_paths);
7588 error = got_worktree_histedit_postpone(worktree,
7593 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7594 error = histedit_skip_commit(hle, worktree, repo);
7600 error = histedit_commit(&merged_paths, worktree, fileindex,
7601 tmp_branch, hle, repo);
7602 got_worktree_rebase_pathlist_free(&merged_paths);
7607 if (rebase_status == GOT_STATUS_CONFLICT) {
7608 error = got_worktree_histedit_postpone(worktree, fileindex);
7611 error = got_error_msg(GOT_ERR_CONFLICTS,
7612 "conflicts must be resolved before histedit can continue");
7614 error = histedit_complete(worktree, fileindex, tmp_branch,
7617 got_object_id_queue_free(&commits);
7618 histedit_free_list(&histedit_cmds);
7619 free(head_commit_id);
7620 free(base_commit_id);
7621 free(resume_commit_id);
7623 got_object_commit_close(commit);
7625 got_ref_close(branch);
7627 got_ref_close(tmp_branch);
7629 got_worktree_close(worktree);
7631 got_repo_close(repo);
7636 usage_integrate(void)
7638 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7642 static const struct got_error *
7643 cmd_integrate(int argc, char *argv[])
7645 const struct got_error *error = NULL;
7646 struct got_repository *repo = NULL;
7647 struct got_worktree *worktree = NULL;
7648 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7649 const char *branch_arg = NULL;
7650 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7651 struct got_fileindex *fileindex = NULL;
7652 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7653 int ch, did_something = 0;
7655 while ((ch = getopt(argc, argv, "")) != -1) {
7668 branch_arg = argv[0];
7670 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7671 "unveil", NULL) == -1)
7674 cwd = getcwd(NULL, 0);
7676 error = got_error_from_errno("getcwd");
7680 error = got_worktree_open(&worktree, cwd);
7684 error = check_rebase_or_histedit_in_progress(worktree);
7688 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7693 error = apply_unveil(got_repo_get_path(repo), 0,
7694 got_worktree_get_root_path(worktree));
7698 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7699 error = got_error_from_errno("asprintf");
7703 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7704 &base_branch_ref, worktree, refname, repo);
7708 refname = strdup(got_ref_get_name(branch_ref));
7709 if (refname == NULL) {
7710 error = got_error_from_errno("strdup");
7711 got_worktree_integrate_abort(worktree, fileindex, repo,
7712 branch_ref, base_branch_ref);
7715 base_refname = strdup(got_ref_get_name(base_branch_ref));
7716 if (base_refname == NULL) {
7717 error = got_error_from_errno("strdup");
7718 got_worktree_integrate_abort(worktree, fileindex, repo,
7719 branch_ref, base_branch_ref);
7723 error = got_ref_resolve(&commit_id, repo, branch_ref);
7727 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7731 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7732 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7733 "specified branch has already been integrated");
7734 got_worktree_integrate_abort(worktree, fileindex, repo,
7735 branch_ref, base_branch_ref);
7739 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7741 if (error->code == GOT_ERR_ANCESTRY)
7742 error = got_error(GOT_ERR_REBASE_REQUIRED);
7743 got_worktree_integrate_abort(worktree, fileindex, repo,
7744 branch_ref, base_branch_ref);
7748 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7749 branch_ref, base_branch_ref, update_progress, &did_something,
7750 check_cancelled, NULL);
7754 printf("Integrated %s into %s\n", refname, base_refname);
7757 got_repo_close(repo);
7759 got_worktree_close(worktree);
7761 free(base_commit_id);
7771 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7772 "[file-path ...]\n",
7777 static const struct got_error *
7778 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7779 const char *path, struct got_object_id *blob_id,
7780 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7781 int dirfd, const char *de_name)
7783 const struct got_error *err = NULL;
7784 char *id_str = NULL;
7786 if (staged_status != GOT_STATUS_ADD &&
7787 staged_status != GOT_STATUS_MODIFY &&
7788 staged_status != GOT_STATUS_DELETE)
7791 if (staged_status == GOT_STATUS_ADD ||
7792 staged_status == GOT_STATUS_MODIFY)
7793 err = got_object_id_str(&id_str, staged_blob_id);
7795 err = got_object_id_str(&id_str, blob_id);
7799 printf("%s %c %s\n", id_str, staged_status, path);
7804 static const struct got_error *
7805 cmd_stage(int argc, char *argv[])
7807 const struct got_error *error = NULL;
7808 struct got_repository *repo = NULL;
7809 struct got_worktree *worktree = NULL;
7811 struct got_pathlist_head paths;
7812 struct got_pathlist_entry *pe;
7813 int ch, list_stage = 0, pflag = 0;
7814 FILE *patch_script_file = NULL;
7815 const char *patch_script_path = NULL;
7816 struct choose_patch_arg cpa;
7820 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7829 patch_script_path = optarg;
7841 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7842 "unveil", NULL) == -1)
7845 if (list_stage && (pflag || patch_script_path))
7846 errx(1, "-l option cannot be used with other options");
7847 if (patch_script_path && !pflag)
7848 errx(1, "-F option can only be used together with -p option");
7850 cwd = getcwd(NULL, 0);
7852 error = got_error_from_errno("getcwd");
7856 error = got_worktree_open(&worktree, cwd);
7860 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7865 if (patch_script_path) {
7866 patch_script_file = fopen(patch_script_path, "r");
7867 if (patch_script_file == NULL) {
7868 error = got_error_from_errno2("fopen",
7873 error = apply_unveil(got_repo_get_path(repo), 0,
7874 got_worktree_get_root_path(worktree));
7878 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7883 error = got_worktree_status(worktree, &paths, repo,
7884 print_stage, NULL, check_cancelled, NULL);
7886 cpa.patch_script_file = patch_script_file;
7887 cpa.action = "stage";
7888 error = got_worktree_stage(worktree, &paths,
7889 pflag ? NULL : print_status, NULL,
7890 pflag ? choose_patch : NULL, &cpa, repo);
7893 if (patch_script_file && fclose(patch_script_file) == EOF &&
7895 error = got_error_from_errno2("fclose", patch_script_path);
7897 got_repo_close(repo);
7899 got_worktree_close(worktree);
7900 TAILQ_FOREACH(pe, &paths, entry)
7901 free((char *)pe->path);
7902 got_pathlist_free(&paths);
7910 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7911 "[file-path ...]\n",
7917 static const struct got_error *
7918 cmd_unstage(int argc, char *argv[])
7920 const struct got_error *error = NULL;
7921 struct got_repository *repo = NULL;
7922 struct got_worktree *worktree = NULL;
7924 struct got_pathlist_head paths;
7925 struct got_pathlist_entry *pe;
7926 int ch, did_something = 0, pflag = 0;
7927 FILE *patch_script_file = NULL;
7928 const char *patch_script_path = NULL;
7929 struct choose_patch_arg cpa;
7933 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7939 patch_script_path = optarg;
7951 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7952 "unveil", NULL) == -1)
7955 if (patch_script_path && !pflag)
7956 errx(1, "-F option can only be used together with -p option");
7958 cwd = getcwd(NULL, 0);
7960 error = got_error_from_errno("getcwd");
7964 error = got_worktree_open(&worktree, cwd);
7968 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7973 if (patch_script_path) {
7974 patch_script_file = fopen(patch_script_path, "r");
7975 if (patch_script_file == NULL) {
7976 error = got_error_from_errno2("fopen",
7982 error = apply_unveil(got_repo_get_path(repo), 0,
7983 got_worktree_get_root_path(worktree));
7987 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7991 cpa.patch_script_file = patch_script_file;
7992 cpa.action = "unstage";
7993 error = got_worktree_unstage(worktree, &paths, update_progress,
7994 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7996 if (patch_script_file && fclose(patch_script_file) == EOF &&
7998 error = got_error_from_errno2("fclose", patch_script_path);
8000 got_repo_close(repo);
8002 got_worktree_close(worktree);
8003 TAILQ_FOREACH(pe, &paths, entry)
8004 free((char *)pe->path);
8005 got_pathlist_free(&paths);
8013 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8014 "arg1 [arg2 ...]\n", getprogname());
8018 static const struct got_error *
8019 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8021 const struct got_error *err;
8022 struct got_blob_object *blob;
8024 err = got_object_open_as_blob(&blob, repo, id, 8192);
8028 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8029 got_object_blob_close(blob);
8033 static const struct got_error *
8034 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8036 const struct got_error *err;
8037 struct got_tree_object *tree;
8040 err = got_object_open_as_tree(&tree, repo, id);
8044 nentries = got_object_tree_get_nentries(tree);
8045 for (i = 0; i < nentries; i++) {
8046 struct got_tree_entry *te;
8048 if (sigint_received || sigpipe_received)
8050 te = got_object_tree_get_entry(tree, i);
8051 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8054 fprintf(outfile, "%s %.7o %s\n", id_str,
8055 got_tree_entry_get_mode(te),
8056 got_tree_entry_get_name(te));
8060 got_object_tree_close(tree);
8064 static const struct got_error *
8065 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8067 const struct got_error *err;
8068 struct got_commit_object *commit;
8069 const struct got_object_id_queue *parent_ids;
8070 struct got_object_qid *pid;
8071 char *id_str = NULL;
8072 const char *logmsg = NULL;
8074 err = got_object_open_as_commit(&commit, repo, id);
8078 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8082 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8083 parent_ids = got_object_commit_get_parent_ids(commit);
8084 fprintf(outfile, "numparents %d\n",
8085 got_object_commit_get_nparents(commit));
8086 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8088 err = got_object_id_str(&pid_str, pid->id);
8091 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8094 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8095 got_object_commit_get_author(commit),
8096 got_object_commit_get_author_time(commit));
8098 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8099 got_object_commit_get_author(commit),
8100 got_object_commit_get_committer_time(commit));
8102 logmsg = got_object_commit_get_logmsg_raw(commit);
8103 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8104 fprintf(outfile, "%s", logmsg);
8107 got_object_commit_close(commit);
8111 static const struct got_error *
8112 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8114 const struct got_error *err;
8115 struct got_tag_object *tag;
8116 char *id_str = NULL;
8117 const char *tagmsg = NULL;
8119 err = got_object_open_as_tag(&tag, repo, id);
8123 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8127 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8129 switch (got_object_tag_get_object_type(tag)) {
8130 case GOT_OBJ_TYPE_BLOB:
8131 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8132 GOT_OBJ_LABEL_BLOB);
8134 case GOT_OBJ_TYPE_TREE:
8135 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8136 GOT_OBJ_LABEL_TREE);
8138 case GOT_OBJ_TYPE_COMMIT:
8139 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8140 GOT_OBJ_LABEL_COMMIT);
8142 case GOT_OBJ_TYPE_TAG:
8143 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8150 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8151 got_object_tag_get_name(tag));
8153 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8154 got_object_tag_get_tagger(tag),
8155 got_object_tag_get_tagger_time(tag));
8157 tagmsg = got_object_tag_get_message(tag);
8158 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8159 fprintf(outfile, "%s", tagmsg);
8162 got_object_tag_close(tag);
8166 static const struct got_error *
8167 cmd_cat(int argc, char *argv[])
8169 const struct got_error *error;
8170 struct got_repository *repo = NULL;
8171 struct got_worktree *worktree = NULL;
8172 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8173 const char *commit_id_str = NULL;
8174 struct got_object_id *id = NULL, *commit_id = NULL;
8175 int ch, obj_type, i, force_path = 0;
8178 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8183 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8186 commit_id_str = optarg;
8189 repo_path = realpath(optarg, NULL);
8190 if (repo_path == NULL)
8191 return got_error_from_errno2("realpath",
8193 got_path_strip_trailing_slashes(repo_path);
8207 cwd = getcwd(NULL, 0);
8209 error = got_error_from_errno("getcwd");
8212 error = got_worktree_open(&worktree, cwd);
8213 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8216 if (repo_path == NULL) {
8218 got_worktree_get_repo_path(worktree));
8219 if (repo_path == NULL) {
8220 error = got_error_from_errno("strdup");
8226 if (repo_path == NULL) {
8227 repo_path = getcwd(NULL, 0);
8228 if (repo_path == NULL)
8229 return got_error_from_errno("getcwd");
8232 error = got_repo_open(&repo, repo_path, NULL);
8237 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8241 if (commit_id_str == NULL)
8242 commit_id_str = GOT_REF_HEAD;
8243 error = got_repo_match_object_id(&commit_id, NULL,
8244 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8248 for (i = 0; i < argc; i++) {
8250 error = got_object_id_by_path(&id, repo, commit_id,
8255 error = got_repo_match_object_id(&id, &label, argv[i],
8256 GOT_OBJ_TYPE_ANY, 0, repo);
8258 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8259 error->code != GOT_ERR_NOT_REF)
8261 error = got_object_id_by_path(&id, repo,
8262 commit_id, argv[i]);
8268 error = got_object_get_type(&obj_type, repo, id);
8273 case GOT_OBJ_TYPE_BLOB:
8274 error = cat_blob(id, repo, stdout);
8276 case GOT_OBJ_TYPE_TREE:
8277 error = cat_tree(id, repo, stdout);
8279 case GOT_OBJ_TYPE_COMMIT:
8280 error = cat_commit(id, repo, stdout);
8282 case GOT_OBJ_TYPE_TAG:
8283 error = cat_tag(id, repo, stdout);
8286 error = got_error(GOT_ERR_OBJ_TYPE);
8301 got_worktree_close(worktree);
8303 const struct got_error *repo_error;
8304 repo_error = got_repo_close(repo);