2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-a] [-m] [-q] [-v] repository-url "
815 "[directory]\n", getprogname());
819 struct got_fetch_progress_arg {
820 char last_scaled_size[FMT_SCALED_STRSIZE];
826 static const struct got_error *
827 fetch_progress(void *arg, const char *message, off_t packfile_size,
828 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
830 struct got_fetch_progress_arg *a = arg;
831 char scaled_size[FMT_SCALED_STRSIZE];
832 int p_indexed, p_resolved;
833 int print_size = 0, print_indexed = 0, print_resolved = 0;
835 if (a->verbosity < 0)
838 if (message && message[0] != '\0') {
839 printf("\rserver: %s", message);
844 if (packfile_size > 0 || nobj_indexed > 0) {
845 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 (a->last_scaled_size[0] == '\0' ||
847 strcmp(scaled_size, a->last_scaled_size)) != 0) {
849 if (strlcpy(a->last_scaled_size, scaled_size,
850 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 return got_error(GOT_ERR_NO_SPACE);
853 if (nobj_indexed > 0) {
854 p_indexed = (nobj_indexed * 100) / nobj_total;
855 if (p_indexed != a->last_p_indexed) {
856 a->last_p_indexed = p_indexed;
861 if (nobj_resolved > 0) {
862 p_resolved = (nobj_resolved * 100) /
863 (nobj_total - nobj_loose);
864 if (p_resolved != a->last_p_resolved) {
865 a->last_p_resolved = p_resolved;
873 if (print_size || print_indexed || print_resolved)
876 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
878 printf("; indexing %d%%", p_indexed);
880 printf("; resolving deltas %d%%", p_resolved);
881 if (print_size || print_indexed || print_resolved)
887 static const struct got_error *
888 cmd_clone(int argc, char *argv[])
890 const struct got_error *error = NULL;
891 const char *uri, *dirname;
892 char *proto, *host, *port, *repo_name, *server_path;
893 char *default_destdir = NULL, *id_str = NULL;
894 const char *repo_path;
895 struct got_repository *repo = NULL;
896 struct got_pathlist_head refs, symrefs;
897 struct got_pathlist_entry *pe;
898 struct got_object_id *pack_hash = NULL;
899 int ch, fetchfd = -1;
900 struct got_fetch_progress_arg fpa;
901 char *git_url = NULL;
902 char *gitconfig_path = NULL;
903 char *gitconfig = NULL;
904 FILE *gitconfig_file = NULL;
906 int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
907 struct got_reference *head_symref = NULL;
910 TAILQ_INIT(&symrefs);
912 while ((ch = getopt(argc, argv, "amvq")) != -1) {
915 fetch_all_branches = 1;
918 mirror_references = 1;
923 else if (verbosity < 3)
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
951 if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
952 host, port ? ":" : "", port ? port : "",
953 server_path[0] != '/' ? "/" : "", server_path) == -1) {
954 error = got_error_from_errno("asprintf");
958 if (strcmp(proto, "git") == 0) {
960 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
961 "sendfd dns inet unveil", NULL) == -1)
964 } else if (strcmp(proto, "git+ssh") == 0 ||
965 strcmp(proto, "ssh") == 0) {
967 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
968 "sendfd unveil", NULL) == -1)
971 } else if (strcmp(proto, "http") == 0 ||
972 strcmp(proto, "git+http") == 0) {
973 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
976 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
979 if (dirname == NULL) {
980 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
981 error = got_error_from_errno("asprintf");
984 repo_path = default_destdir;
988 error = got_path_mkdir(repo_path);
992 error = got_repo_init(repo_path);
996 error = got_repo_open(&repo, repo_path, NULL);
1000 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1001 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1002 error = got_error_from_errno2("unveil",
1003 GOT_FETCH_PATH_SSH);
1007 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1011 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1017 printf("Connected to %s%s%s\n", host,
1018 port ? ":" : "", port ? port : "");
1020 fpa.last_scaled_size[0] = '\0';
1021 fpa.last_p_indexed = -1;
1022 fpa.last_p_resolved = -1;
1023 fpa.verbosity = verbosity;
1024 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1025 GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1026 fetch_all_branches, fetchfd, repo, fetch_progress, &fpa);
1030 error = got_object_id_str(&id_str, pack_hash);
1034 printf("\nFetched %s.pack\n", id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1047 error = got_ref_write(ref, repo);
1052 if (mirror_references)
1055 if (strncmp("refs/heads/", refname, 11) != 0)
1058 if (asprintf(&remote_refname,
1059 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1060 refname + 11) == -1) {
1061 error = got_error_from_errno("asprintf");
1064 error = got_ref_alloc(&ref, remote_refname, id);
1067 error = got_ref_write(ref, repo);
1073 /* Set the HEAD reference if the server provided one. */
1074 TAILQ_FOREACH(pe, &symrefs, entry) {
1075 struct got_reference *target_ref;
1076 const char *refname = pe->path;
1077 const char *target = pe->data;
1079 if (strcmp(refname, GOT_REF_HEAD) != 0)
1082 error = got_ref_open(&target_ref, repo, target, 0);
1084 if (error->code == GOT_ERR_NOT_REF) {
1091 error = got_ref_alloc_symref(&head_symref,
1092 GOT_REF_HEAD, target_ref);
1093 got_ref_close(target_ref);
1098 printf("Setting %s to %s\n", GOT_REF_HEAD,
1099 got_ref_get_symref_target(head_symref));
1101 error = got_ref_write(head_symref, repo);
1106 /* Create a config file git-fetch(1) can understand. */
1107 gitconfig_path = got_repo_get_path_gitconfig(repo);
1108 if (gitconfig_path == NULL) {
1109 error = got_error_from_errno("got_repo_get_path_gitconfig");
1112 gitconfig_file = fopen(gitconfig_path, "a");
1113 if (gitconfig_file == NULL) {
1114 error = got_error_from_errno2("fopen", gitconfig_path);
1117 if (mirror_references) {
1118 if (asprintf(&gitconfig,
1121 "\tfetch = +refs/*:refs/*\n"
1122 "\tmirror = true\n",
1123 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1124 error = got_error_from_errno("asprintf");
1127 } else if (fetch_all_branches) {
1128 if (asprintf(&gitconfig,
1131 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1132 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1133 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1134 error = got_error_from_errno("asprintf");
1138 const char *branchname;
1141 * If the server specified a default branch, use just that one.
1142 * Otherwise fall back to fetching all branches on next fetch.
1145 branchname = got_ref_get_symref_target(head_symref);
1146 if (strncmp(branchname, "refs/heads/", 11) == 0)
1149 branchname = "*"; /* fall back to all branches */
1150 if (asprintf(&gitconfig,
1153 "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1154 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1155 branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1156 branchname) == -1) {
1157 error = got_error_from_errno("asprintf");
1161 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1162 if (n != strlen(gitconfig)) {
1163 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1169 printf("Created %s repository '%s'\n",
1170 mirror_references ? "mirrored" : "cloned", repo_path);
1172 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1173 error = got_error_from_errno("close");
1174 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1175 error = got_error_from_errno("fclose");
1177 got_repo_close(repo);
1179 got_ref_close(head_symref);
1180 TAILQ_FOREACH(pe, &refs, entry) {
1181 free((void *)pe->path);
1184 got_pathlist_free(&refs);
1185 TAILQ_FOREACH(pe, &symrefs, entry) {
1186 free((void *)pe->path);
1189 got_pathlist_free(&symrefs);
1196 free(default_destdir);
1197 free(gitconfig_path);
1202 static const struct got_error *
1203 create_ref(const char *refname, struct got_object_id *id,
1204 const char *id_str, struct got_repository *repo)
1206 const struct got_error *err = NULL;
1207 struct got_reference *ref;
1209 printf("Creating %s: %s\n", refname, id_str);
1211 err = got_ref_alloc(&ref, refname, id);
1215 err = got_ref_write(ref, repo);
1220 static const struct got_error *
1221 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1222 struct got_repository *repo)
1224 const struct got_error *err = NULL;
1225 char *new_id_str = NULL;
1226 struct got_object_id *old_id = NULL;
1228 err = got_object_id_str(&new_id_str, new_id);
1232 if (got_ref_is_symbolic(ref)) {
1233 struct got_reference *new_ref;
1234 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1237 printf("Deleting symbolic reference %s -> %s\n",
1238 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1239 err = got_ref_delete(ref, repo);
1242 printf("Setting %s to %s\n", got_ref_get_name(ref),
1244 err = got_ref_write(new_ref, repo);
1248 err = got_ref_resolve(&old_id, repo, ref);
1251 if (got_object_id_cmp(old_id, new_id) != 0) {
1252 printf("Setting %s to %s\n",
1253 got_ref_get_name(ref), new_id_str);
1254 err = got_ref_change_ref(ref, new_id);
1257 err = got_ref_write(ref, repo);
1271 fprintf(stderr, "usage: %s fetch [-a] [-r repository-path] [-q] [-v] "
1272 "[remote-repository-name]\n", getprogname());
1276 static const struct got_error *
1277 cmd_fetch(int argc, char *argv[])
1279 const struct got_error *error = NULL;
1280 char *cwd = NULL, *repo_path = NULL;
1281 const char *remote_name;
1282 char *proto = NULL, *host = NULL, *port = NULL;
1283 char *repo_name = NULL, *server_path = NULL;
1284 struct got_remote_repo *remotes, *remote = NULL;
1286 char *id_str = NULL;
1287 struct got_repository *repo = NULL;
1288 struct got_worktree *worktree = NULL;
1289 struct got_pathlist_head refs, symrefs;
1290 struct got_pathlist_entry *pe;
1291 struct got_object_id *pack_hash = NULL;
1292 int i, ch, fetchfd = -1;
1293 struct got_fetch_progress_arg fpa;
1294 int verbosity = 0, fetch_all_branches = 0;
1297 TAILQ_INIT(&symrefs);
1299 while ((ch = getopt(argc, argv, "ar:vq")) != -1) {
1302 fetch_all_branches = 1;
1305 repo_path = realpath(optarg, NULL);
1306 if (repo_path == NULL)
1307 return got_error_from_errno2("realpath",
1309 got_path_strip_trailing_slashes(repo_path);
1314 else if (verbosity < 3)
1329 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1331 remote_name = argv[0];
1335 cwd = getcwd(NULL, 0);
1337 error = got_error_from_errno("getcwd");
1341 if (repo_path == NULL) {
1342 error = got_worktree_open(&worktree, cwd);
1343 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1349 strdup(got_worktree_get_repo_path(worktree));
1350 if (repo_path == NULL)
1351 error = got_error_from_errno("strdup");
1355 repo_path = strdup(cwd);
1356 if (repo_path == NULL) {
1357 error = got_error_from_errno("strdup");
1363 error = got_repo_open(&repo, repo_path, NULL);
1367 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1368 for (i = 0; i < nremotes; i++) {
1369 remote = &remotes[i];
1370 if (strcmp(remote->name, remote_name) == 0)
1373 if (i == nremotes) {
1374 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1378 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1379 &repo_name, remote->url);
1383 if (strcmp(proto, "git") == 0) {
1385 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1386 "sendfd dns inet unveil", NULL) == -1)
1389 } else if (strcmp(proto, "git+ssh") == 0 ||
1390 strcmp(proto, "ssh") == 0) {
1392 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1393 "sendfd unveil", NULL) == -1)
1396 } else if (strcmp(proto, "http") == 0 ||
1397 strcmp(proto, "git+http") == 0) {
1398 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1401 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1405 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1406 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1407 error = got_error_from_errno2("unveil",
1408 GOT_FETCH_PATH_SSH);
1412 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1416 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1422 printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1423 port ? ":" : "", port ? port : "");
1425 fpa.last_scaled_size[0] = '\0';
1426 fpa.last_p_indexed = -1;
1427 fpa.last_p_resolved = -1;
1428 fpa.verbosity = verbosity;
1429 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1430 remote->mirror_references, fetch_all_branches, fetchfd, repo,
1431 fetch_progress, &fpa);
1435 if (pack_hash == NULL) {
1437 printf("Already up-to-date\n");
1441 if (verbosity >= 0) {
1442 error = got_object_id_str(&id_str, pack_hash);
1445 printf("\nFetched %s.pack\n", id_str);
1450 /* Update references provided with the pack file. */
1451 TAILQ_FOREACH(pe, &refs, entry) {
1452 const char *refname = pe->path;
1453 struct got_object_id *id = pe->data;
1454 struct got_reference *ref;
1455 char *remote_refname;
1457 error = got_object_id_str(&id_str, id);
1461 if (remote->mirror_references ||
1462 strncmp("refs/tags/", refname, 10) == 0) {
1463 error = got_ref_open(&ref, repo, refname, 0);
1465 if (error->code != GOT_ERR_NOT_REF)
1467 error = create_ref(refname, id, id_str, repo);
1471 error = update_ref(ref, id, repo);
1476 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1477 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1478 remote_name, refname + 11) == -1) {
1479 error = got_error_from_errno("asprintf");
1483 error = got_ref_open(&ref, repo, remote_refname, 0);
1485 if (error->code != GOT_ERR_NOT_REF)
1487 error = create_ref(remote_refname, id, id_str,
1492 error = update_ref(ref, id, repo);
1502 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1503 error = got_error_from_errno("close");
1505 got_repo_close(repo);
1507 got_worktree_close(worktree);
1508 TAILQ_FOREACH(pe, &refs, entry) {
1509 free((void *)pe->path);
1512 got_pathlist_free(&refs);
1513 TAILQ_FOREACH(pe, &symrefs, entry) {
1514 free((void *)pe->path);
1517 got_pathlist_free(&symrefs);
1532 usage_checkout(void)
1534 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1535 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1540 show_worktree_base_ref_warning(void)
1542 fprintf(stderr, "%s: warning: could not create a reference "
1543 "to the work tree's base commit; the commit could be "
1544 "garbage-collected by Git; making the repository "
1545 "writable and running 'got update' will prevent this\n",
1549 struct got_checkout_progress_arg {
1550 const char *worktree_path;
1551 int had_base_commit_ref_error;
1554 static const struct got_error *
1555 checkout_progress(void *arg, unsigned char status, const char *path)
1557 struct got_checkout_progress_arg *a = arg;
1559 /* Base commit bump happens silently. */
1560 if (status == GOT_STATUS_BUMP_BASE)
1563 if (status == GOT_STATUS_BASE_REF_ERR) {
1564 a->had_base_commit_ref_error = 1;
1568 while (path[0] == '/')
1571 printf("%c %s/%s\n", status, a->worktree_path, path);
1575 static const struct got_error *
1576 check_cancelled(void *arg)
1578 if (sigint_received || sigpipe_received)
1579 return got_error(GOT_ERR_CANCELLED);
1583 static const struct got_error *
1584 check_linear_ancestry(struct got_object_id *commit_id,
1585 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1586 struct got_repository *repo)
1588 const struct got_error *err = NULL;
1589 struct got_object_id *yca_id;
1591 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1592 commit_id, base_commit_id, repo, check_cancelled, NULL);
1597 return got_error(GOT_ERR_ANCESTRY);
1600 * Require a straight line of history between the target commit
1601 * and the work tree's base commit.
1603 * Non-linear situations such as this require a rebase:
1605 * (commit) D F (base_commit)
1613 * 'got update' only handles linear cases:
1614 * Update forwards in time: A (base/yca) - B - C - D (commit)
1615 * Update backwards in time: D (base) - C - B - A (commit/yca)
1617 if (allow_forwards_in_time_only) {
1618 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1619 return got_error(GOT_ERR_ANCESTRY);
1620 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1621 got_object_id_cmp(base_commit_id, yca_id) != 0)
1622 return got_error(GOT_ERR_ANCESTRY);
1628 static const struct got_error *
1629 check_same_branch(struct got_object_id *commit_id,
1630 struct got_reference *head_ref, struct got_object_id *yca_id,
1631 struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_commit_graph *graph = NULL;
1635 struct got_object_id *head_commit_id = NULL;
1636 int is_same_branch = 0;
1638 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1642 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1646 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1651 err = got_commit_graph_open(&graph, "/", 1);
1655 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1656 check_cancelled, NULL);
1661 struct got_object_id *id;
1662 err = got_commit_graph_iter_next(&id, graph, repo,
1663 check_cancelled, NULL);
1665 if (err->code == GOT_ERR_ITER_COMPLETED)
1671 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1673 if (got_object_id_cmp(id, commit_id) == 0) {
1681 got_commit_graph_close(graph);
1682 free(head_commit_id);
1683 if (!err && !is_same_branch)
1684 err = got_error(GOT_ERR_ANCESTRY);
1688 static const struct got_error *
1689 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1691 static char msg[512];
1692 const char *branch_name;
1694 if (got_ref_is_symbolic(ref))
1695 branch_name = got_ref_get_symref_target(ref);
1697 branch_name = got_ref_get_name(ref);
1699 if (strncmp("refs/heads/", branch_name, 11) == 0)
1702 snprintf(msg, sizeof(msg),
1703 "target commit is not contained in branch '%s'; "
1704 "the branch to use must be specified with -b; "
1705 "if necessary a new branch can be created for "
1706 "this commit with 'got branch -c %s BRANCH_NAME'",
1707 branch_name, commit_id_str);
1709 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1712 static const struct got_error *
1713 cmd_checkout(int argc, char *argv[])
1715 const struct got_error *error = NULL;
1716 struct got_repository *repo = NULL;
1717 struct got_reference *head_ref = NULL;
1718 struct got_worktree *worktree = NULL;
1719 char *repo_path = NULL;
1720 char *worktree_path = NULL;
1721 const char *path_prefix = "";
1722 const char *branch_name = GOT_REF_HEAD;
1723 char *commit_id_str = NULL;
1724 int ch, same_path_prefix, allow_nonempty = 0;
1725 struct got_pathlist_head paths;
1726 struct got_checkout_progress_arg cpa;
1730 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1733 branch_name = optarg;
1736 commit_id_str = strdup(optarg);
1737 if (commit_id_str == NULL)
1738 return got_error_from_errno("strdup");
1744 path_prefix = optarg;
1756 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1757 "unveil", NULL) == -1)
1761 char *cwd, *base, *dotgit;
1762 repo_path = realpath(argv[0], NULL);
1763 if (repo_path == NULL)
1764 return got_error_from_errno2("realpath", argv[0]);
1765 cwd = getcwd(NULL, 0);
1767 error = got_error_from_errno("getcwd");
1770 if (path_prefix[0]) {
1771 base = basename(path_prefix);
1773 error = got_error_from_errno2("basename",
1778 base = basename(repo_path);
1780 error = got_error_from_errno2("basename",
1785 dotgit = strstr(base, ".git");
1788 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1789 error = got_error_from_errno("asprintf");
1794 } else if (argc == 2) {
1795 repo_path = realpath(argv[0], NULL);
1796 if (repo_path == NULL) {
1797 error = got_error_from_errno2("realpath", argv[0]);
1800 worktree_path = realpath(argv[1], NULL);
1801 if (worktree_path == NULL) {
1802 if (errno != ENOENT) {
1803 error = got_error_from_errno2("realpath",
1807 worktree_path = strdup(argv[1]);
1808 if (worktree_path == NULL) {
1809 error = got_error_from_errno("strdup");
1816 got_path_strip_trailing_slashes(repo_path);
1817 got_path_strip_trailing_slashes(worktree_path);
1819 error = got_repo_open(&repo, repo_path, NULL);
1823 /* Pre-create work tree path for unveil(2) */
1824 error = got_path_mkdir(worktree_path);
1826 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1827 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1829 if (!allow_nonempty &&
1830 !got_path_dir_is_empty(worktree_path)) {
1831 error = got_error_path(worktree_path,
1832 GOT_ERR_DIR_NOT_EMPTY);
1837 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1841 error = got_ref_open(&head_ref, repo, branch_name, 0);
1845 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1846 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1849 error = got_worktree_open(&worktree, worktree_path);
1853 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1857 if (!same_path_prefix) {
1858 error = got_error(GOT_ERR_PATH_PREFIX);
1862 if (commit_id_str) {
1863 struct got_object_id *commit_id;
1864 error = got_repo_match_object_id(&commit_id, NULL,
1865 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1868 error = check_linear_ancestry(commit_id,
1869 got_worktree_get_base_commit_id(worktree), 0, repo);
1870 if (error != NULL) {
1872 if (error->code == GOT_ERR_ANCESTRY) {
1873 error = checkout_ancestry_error(
1874 head_ref, commit_id_str);
1878 error = check_same_branch(commit_id, head_ref, NULL, repo);
1880 if (error->code == GOT_ERR_ANCESTRY) {
1881 error = checkout_ancestry_error(
1882 head_ref, commit_id_str);
1886 error = got_worktree_set_base_commit_id(worktree, repo,
1893 error = got_pathlist_append(&paths, "", NULL);
1896 cpa.worktree_path = worktree_path;
1897 cpa.had_base_commit_ref_error = 0;
1898 error = got_worktree_checkout_files(worktree, &paths, repo,
1899 checkout_progress, &cpa, check_cancelled, NULL);
1903 printf("Now shut up and hack\n");
1904 if (cpa.had_base_commit_ref_error)
1905 show_worktree_base_ref_warning();
1907 got_pathlist_free(&paths);
1908 free(commit_id_str);
1910 free(worktree_path);
1917 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1922 static const struct got_error *
1923 update_progress(void *arg, unsigned char status, const char *path)
1925 int *did_something = arg;
1927 if (status == GOT_STATUS_EXISTS ||
1928 status == GOT_STATUS_BASE_REF_ERR)
1933 /* Base commit bump happens silently. */
1934 if (status == GOT_STATUS_BUMP_BASE)
1937 while (path[0] == '/')
1939 printf("%c %s\n", status, path);
1943 static const struct got_error *
1944 switch_head_ref(struct got_reference *head_ref,
1945 struct got_object_id *commit_id, struct got_worktree *worktree,
1946 struct got_repository *repo)
1948 const struct got_error *err = NULL;
1950 int ref_has_moved = 0;
1952 /* Trivial case: switching between two different references. */
1953 if (strcmp(got_ref_get_name(head_ref),
1954 got_worktree_get_head_ref_name(worktree)) != 0) {
1955 printf("Switching work tree from %s to %s\n",
1956 got_worktree_get_head_ref_name(worktree),
1957 got_ref_get_name(head_ref));
1958 return got_worktree_set_head_ref(worktree, head_ref);
1961 err = check_linear_ancestry(commit_id,
1962 got_worktree_get_base_commit_id(worktree), 0, repo);
1964 if (err->code != GOT_ERR_ANCESTRY)
1971 /* Switching to a rebased branch with the same reference name. */
1972 err = got_object_id_str(&base_id_str,
1973 got_worktree_get_base_commit_id(worktree));
1976 printf("Reference %s now points at a different branch\n",
1977 got_worktree_get_head_ref_name(worktree));
1978 printf("Switching work tree from %s to %s\n", base_id_str,
1979 got_worktree_get_head_ref_name(worktree));
1983 static const struct got_error *
1984 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1986 const struct got_error *err;
1989 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1993 return got_error(GOT_ERR_REBASING);
1995 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1999 return got_error(GOT_ERR_HISTEDIT_BUSY);
2004 static const struct got_error *
2005 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2006 char *argv[], struct got_worktree *worktree)
2008 const struct got_error *err = NULL;
2015 return got_error_from_errno("strdup");
2016 return got_pathlist_append(paths, path, NULL);
2019 for (i = 0; i < argc; i++) {
2020 err = got_worktree_resolve_path(&path, worktree, argv[i]);
2023 err = got_pathlist_append(paths, path, NULL);
2033 static const struct got_error *
2034 cmd_update(int argc, char *argv[])
2036 const struct got_error *error = NULL;
2037 struct got_repository *repo = NULL;
2038 struct got_worktree *worktree = NULL;
2039 char *worktree_path = NULL;
2040 struct got_object_id *commit_id = NULL;
2041 char *commit_id_str = NULL;
2042 const char *branch_name = NULL;
2043 struct got_reference *head_ref = NULL;
2044 struct got_pathlist_head paths;
2045 struct got_pathlist_entry *pe;
2046 int ch, did_something = 0;
2050 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2053 branch_name = optarg;
2056 commit_id_str = strdup(optarg);
2057 if (commit_id_str == NULL)
2058 return got_error_from_errno("strdup");
2070 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2071 "unveil", NULL) == -1)
2074 worktree_path = getcwd(NULL, 0);
2075 if (worktree_path == NULL) {
2076 error = got_error_from_errno("getcwd");
2079 error = got_worktree_open(&worktree, worktree_path);
2083 error = check_rebase_or_histedit_in_progress(worktree);
2087 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2092 error = apply_unveil(got_repo_get_path(repo), 0,
2093 got_worktree_get_root_path(worktree));
2097 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2101 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2102 got_worktree_get_head_ref_name(worktree), 0);
2105 if (commit_id_str == NULL) {
2106 error = got_ref_resolve(&commit_id, repo, head_ref);
2109 error = got_object_id_str(&commit_id_str, commit_id);
2113 error = got_repo_match_object_id(&commit_id, NULL,
2114 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2115 free(commit_id_str);
2116 commit_id_str = NULL;
2119 error = got_object_id_str(&commit_id_str, commit_id);
2125 struct got_object_id *head_commit_id;
2126 TAILQ_FOREACH(pe, &paths, entry) {
2127 if (pe->path_len == 0)
2129 error = got_error_msg(GOT_ERR_BAD_PATH,
2130 "switching between branches requires that "
2131 "the entire work tree gets updated");
2134 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2137 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2139 free(head_commit_id);
2142 error = check_same_branch(commit_id, head_ref, NULL, repo);
2145 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2149 error = check_linear_ancestry(commit_id,
2150 got_worktree_get_base_commit_id(worktree), 0, repo);
2151 if (error != NULL) {
2152 if (error->code == GOT_ERR_ANCESTRY)
2153 error = got_error(GOT_ERR_BRANCH_MOVED);
2156 error = check_same_branch(commit_id, head_ref, NULL, repo);
2161 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2163 error = got_worktree_set_base_commit_id(worktree, repo,
2169 error = got_worktree_checkout_files(worktree, &paths, repo,
2170 update_progress, &did_something, check_cancelled, NULL);
2175 printf("Updated to commit %s\n", commit_id_str);
2177 printf("Already up-to-date\n");
2179 free(worktree_path);
2180 TAILQ_FOREACH(pe, &paths, entry)
2181 free((char *)pe->path);
2182 got_pathlist_free(&paths);
2184 free(commit_id_str);
2188 static const struct got_error *
2189 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2190 const char *path, int diff_context, int ignore_whitespace,
2191 struct got_repository *repo)
2193 const struct got_error *err = NULL;
2194 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2197 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2202 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2206 while (path[0] == '/')
2208 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2209 ignore_whitespace, stdout);
2212 got_object_blob_close(blob1);
2213 got_object_blob_close(blob2);
2217 static const struct got_error *
2218 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2219 const char *path, int diff_context, int ignore_whitespace,
2220 struct got_repository *repo)
2222 const struct got_error *err = NULL;
2223 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2224 struct got_diff_blob_output_unidiff_arg arg;
2227 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2232 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2236 arg.diff_context = diff_context;
2237 arg.ignore_whitespace = ignore_whitespace;
2238 arg.outfile = stdout;
2239 while (path[0] == '/')
2241 err = got_diff_tree(tree1, tree2, path, path, repo,
2242 got_diff_blob_output_unidiff, &arg, 1);
2245 got_object_tree_close(tree1);
2247 got_object_tree_close(tree2);
2251 static const struct got_error *
2252 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2253 const char *path, int diff_context, struct got_repository *repo)
2255 const struct got_error *err = NULL;
2256 struct got_commit_object *pcommit = NULL;
2257 char *id_str1 = NULL, *id_str2 = NULL;
2258 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2259 struct got_object_qid *qid;
2261 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2263 err = got_object_open_as_commit(&pcommit, repo,
2269 if (path && path[0] != '\0') {
2271 err = got_object_id_by_path(&obj_id2, repo, id, path);
2274 err = got_object_id_str(&id_str2, obj_id2);
2280 err = got_object_id_by_path(&obj_id1, repo,
2286 err = got_object_id_str(&id_str1, obj_id1);
2292 err = got_object_get_type(&obj_type, repo, obj_id2);
2297 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2299 case GOT_OBJ_TYPE_BLOB:
2300 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2303 case GOT_OBJ_TYPE_TREE:
2304 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2308 err = got_error(GOT_ERR_OBJ_TYPE);
2314 obj_id2 = got_object_commit_get_tree_id(commit);
2315 err = got_object_id_str(&id_str2, obj_id2);
2318 obj_id1 = got_object_commit_get_tree_id(pcommit);
2319 err = got_object_id_str(&id_str1, obj_id1);
2322 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2323 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2329 got_object_commit_close(pcommit);
2334 get_datestr(time_t *time, char *datebuf)
2336 struct tm mytm, *tm;
2339 tm = gmtime_r(time, &mytm);
2342 s = asctime_r(tm, datebuf);
2345 p = strchr(s, '\n');
2351 static const struct got_error *
2352 match_logmsg(int *have_match, struct got_object_id *id,
2353 struct got_commit_object *commit, regex_t *regex)
2355 const struct got_error *err = NULL;
2356 regmatch_t regmatch;
2357 char *id_str = NULL, *logmsg = NULL;
2361 err = got_object_id_str(&id_str, id);
2365 err = got_object_commit_get_logmsg(&logmsg, commit);
2369 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2377 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2379 static const struct got_error *
2380 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2381 struct got_repository *repo, const char *path, int show_patch,
2382 int diff_context, struct got_reflist_head *refs)
2384 const struct got_error *err = NULL;
2385 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2387 time_t committer_time;
2388 const char *author, *committer;
2389 char *refs_str = NULL;
2390 struct got_reflist_entry *re;
2392 SIMPLEQ_FOREACH(re, refs, entry) {
2395 struct got_tag_object *tag = NULL;
2398 name = got_ref_get_name(re->ref);
2399 if (strcmp(name, GOT_REF_HEAD) == 0)
2401 if (strncmp(name, "refs/", 5) == 0)
2403 if (strncmp(name, "got/", 4) == 0)
2405 if (strncmp(name, "heads/", 6) == 0)
2407 if (strncmp(name, "remotes/", 8) == 0)
2409 if (strncmp(name, "tags/", 5) == 0) {
2410 err = got_object_open_as_tag(&tag, repo, re->id);
2412 if (err->code != GOT_ERR_OBJ_TYPE)
2414 /* Ref points at something other than a tag. */
2419 cmp = got_object_id_cmp(tag ?
2420 got_object_tag_get_object_id(tag) : re->id, id);
2422 got_object_tag_close(tag);
2426 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2428 err = got_error_from_errno("asprintf");
2434 err = got_object_id_str(&id_str, id);
2438 printf(GOT_COMMIT_SEP_STR);
2439 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2440 refs_str ? refs_str : "", refs_str ? ")" : "");
2445 printf("from: %s\n", got_object_commit_get_author(commit));
2446 committer_time = got_object_commit_get_committer_time(commit);
2447 datestr = get_datestr(&committer_time, datebuf);
2449 printf("date: %s UTC\n", datestr);
2450 author = got_object_commit_get_author(commit);
2451 committer = got_object_commit_get_committer(commit);
2452 if (strcmp(author, committer) != 0)
2453 printf("via: %s\n", committer);
2454 if (got_object_commit_get_nparents(commit) > 1) {
2455 const struct got_object_id_queue *parent_ids;
2456 struct got_object_qid *qid;
2458 parent_ids = got_object_commit_get_parent_ids(commit);
2459 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2460 err = got_object_id_str(&id_str, qid->id);
2463 printf("parent %d: %s\n", n++, id_str);
2468 err = got_object_commit_get_logmsg(&logmsg0, commit);
2474 line = strsep(&logmsg, "\n");
2476 printf(" %s\n", line);
2481 err = print_patch(commit, id, path, diff_context, repo);
2486 if (fflush(stdout) != 0 && err == NULL)
2487 err = got_error_from_errno("fflush");
2491 static const struct got_error *
2492 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2493 const char *path, int show_patch, const char *search_pattern,
2494 int diff_context, int limit, int log_branches,
2495 struct got_reflist_head *refs)
2497 const struct got_error *err;
2498 struct got_commit_graph *graph;
2502 if (search_pattern &&
2503 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2504 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2506 err = got_commit_graph_open(&graph, path, !log_branches);
2509 err = got_commit_graph_iter_start(graph, root_id, repo,
2510 check_cancelled, NULL);
2514 struct got_commit_object *commit;
2515 struct got_object_id *id;
2517 if (sigint_received || sigpipe_received)
2520 err = got_commit_graph_iter_next(&id, graph, repo,
2521 check_cancelled, NULL);
2523 if (err->code == GOT_ERR_ITER_COMPLETED)
2530 err = got_object_open_as_commit(&commit, repo, id);
2534 if (search_pattern) {
2535 err = match_logmsg(&have_match, id, commit, ®ex);
2537 got_object_commit_close(commit);
2540 if (have_match == 0) {
2541 got_object_commit_close(commit);
2546 err = print_commit(commit, id, repo, path, show_patch,
2547 diff_context, refs);
2548 got_object_commit_close(commit);
2549 if (err || (limit && --limit == 0))
2555 got_commit_graph_close(graph);
2562 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2563 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2568 get_default_log_limit(void)
2570 const char *got_default_log_limit;
2574 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2575 if (got_default_log_limit == NULL)
2577 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2583 static const struct got_error *
2584 cmd_log(int argc, char *argv[])
2586 const struct got_error *error;
2587 struct got_repository *repo = NULL;
2588 struct got_worktree *worktree = NULL;
2589 struct got_commit_object *commit = NULL;
2590 struct got_object_id *id = NULL;
2591 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2592 const char *start_commit = NULL, *search_pattern = NULL;
2593 int diff_context = -1, ch;
2594 int show_patch = 0, limit = 0, log_branches = 0;
2596 struct got_reflist_head refs;
2598 SIMPLEQ_INIT(&refs);
2601 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2607 limit = get_default_log_limit();
2609 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2615 start_commit = optarg;
2618 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2621 err(1, "-C option %s", errstr);
2624 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2626 err(1, "-l option %s", errstr);
2632 repo_path = realpath(optarg, NULL);
2633 if (repo_path == NULL)
2634 return got_error_from_errno2("realpath",
2636 got_path_strip_trailing_slashes(repo_path);
2639 search_pattern = optarg;
2650 if (diff_context == -1)
2652 else if (!show_patch)
2653 errx(1, "-C reguires -p");
2655 cwd = getcwd(NULL, 0);
2657 error = got_error_from_errno("getcwd");
2661 error = got_worktree_open(&worktree, cwd);
2662 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2669 error = got_error_from_errno("strdup");
2672 } else if (argc == 1) {
2674 error = got_worktree_resolve_path(&path, worktree,
2679 path = strdup(argv[0]);
2681 error = got_error_from_errno("strdup");
2688 if (repo_path == NULL) {
2689 repo_path = worktree ?
2690 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2692 if (repo_path == NULL) {
2693 error = got_error_from_errno("strdup");
2697 error = got_repo_open(&repo, repo_path, NULL);
2701 error = apply_unveil(got_repo_get_path(repo), 1,
2702 worktree ? got_worktree_get_root_path(worktree) : NULL);
2706 if (start_commit == NULL) {
2707 struct got_reference *head_ref;
2708 error = got_ref_open(&head_ref, repo,
2709 worktree ? got_worktree_get_head_ref_name(worktree)
2713 error = got_ref_resolve(&id, repo, head_ref);
2714 got_ref_close(head_ref);
2717 error = got_object_open_as_commit(&commit, repo, id);
2719 struct got_reference *ref;
2720 error = got_ref_open(&ref, repo, start_commit, 0);
2721 if (error == NULL) {
2723 error = got_ref_resolve(&id, repo, ref);
2727 error = got_object_get_type(&obj_type, repo, id);
2730 if (obj_type == GOT_OBJ_TYPE_TAG) {
2731 struct got_tag_object *tag;
2732 error = got_object_open_as_tag(&tag, repo, id);
2735 if (got_object_tag_get_object_type(tag) !=
2736 GOT_OBJ_TYPE_COMMIT) {
2737 got_object_tag_close(tag);
2738 error = got_error(GOT_ERR_OBJ_TYPE);
2742 id = got_object_id_dup(
2743 got_object_tag_get_object_id(tag));
2745 error = got_error_from_errno(
2746 "got_object_id_dup");
2747 got_object_tag_close(tag);
2750 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2751 error = got_error(GOT_ERR_OBJ_TYPE);
2754 error = got_object_open_as_commit(&commit, repo, id);
2758 if (commit == NULL) {
2759 error = got_repo_match_object_id_prefix(&id,
2760 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2769 const char *prefix = got_worktree_get_path_prefix(worktree);
2771 if (asprintf(&p, "%s%s%s", prefix,
2772 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2773 error = got_error_from_errno("asprintf");
2776 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2779 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2784 path = in_repo_path;
2787 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2791 error = print_commits(id, repo, path, show_patch, search_pattern,
2792 diff_context, limit, log_branches, &refs);
2799 got_worktree_close(worktree);
2801 const struct got_error *repo_error;
2802 repo_error = got_repo_close(repo);
2806 got_ref_list_free(&refs);
2813 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2814 "[-w] [object1 object2 | path]\n", getprogname());
2818 struct print_diff_arg {
2819 struct got_repository *repo;
2820 struct got_worktree *worktree;
2825 int ignore_whitespace;
2828 static const struct got_error *
2829 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2830 const char *path, struct got_object_id *blob_id,
2831 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2832 int dirfd, const char *de_name)
2834 struct print_diff_arg *a = arg;
2835 const struct got_error *err = NULL;
2836 struct got_blob_object *blob1 = NULL;
2839 char *abspath = NULL, *label1 = NULL;
2842 if (a->diff_staged) {
2843 if (staged_status != GOT_STATUS_MODIFY &&
2844 staged_status != GOT_STATUS_ADD &&
2845 staged_status != GOT_STATUS_DELETE)
2848 if (staged_status == GOT_STATUS_DELETE)
2850 if (status == GOT_STATUS_NONEXISTENT)
2851 return got_error_set_errno(ENOENT, path);
2852 if (status != GOT_STATUS_MODIFY &&
2853 status != GOT_STATUS_ADD &&
2854 status != GOT_STATUS_DELETE &&
2855 status != GOT_STATUS_CONFLICT)
2859 if (!a->header_shown) {
2860 printf("diff %s %s%s\n", a->id_str,
2861 got_worktree_get_root_path(a->worktree),
2862 a->diff_staged ? " (staged changes)" : "");
2863 a->header_shown = 1;
2866 if (a->diff_staged) {
2867 const char *label1 = NULL, *label2 = NULL;
2868 switch (staged_status) {
2869 case GOT_STATUS_MODIFY:
2873 case GOT_STATUS_ADD:
2876 case GOT_STATUS_DELETE:
2880 return got_error(GOT_ERR_FILE_STATUS);
2882 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2883 label1, label2, a->diff_context, a->ignore_whitespace,
2887 if (staged_status == GOT_STATUS_ADD ||
2888 staged_status == GOT_STATUS_MODIFY) {
2890 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2894 err = got_object_id_str(&id_str, staged_blob_id);
2897 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2898 err = got_error_from_errno("asprintf");
2903 } else if (status != GOT_STATUS_ADD) {
2904 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2909 if (status != GOT_STATUS_DELETE) {
2910 if (asprintf(&abspath, "%s/%s",
2911 got_worktree_get_root_path(a->worktree), path) == -1) {
2912 err = got_error_from_errno("asprintf");
2917 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2919 err = got_error_from_errno2("openat", abspath);
2923 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2925 err = got_error_from_errno2("open", abspath);
2929 if (fstat(fd, &sb) == -1) {
2930 err = got_error_from_errno2("fstat", abspath);
2933 f2 = fdopen(fd, "r");
2935 err = got_error_from_errno2("fdopen", abspath);
2942 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2943 a->diff_context, a->ignore_whitespace, stdout);
2946 got_object_blob_close(blob1);
2947 if (f2 && fclose(f2) == EOF && err == NULL)
2948 err = got_error_from_errno("fclose");
2949 if (fd != -1 && close(fd) == -1 && err == NULL)
2950 err = got_error_from_errno("close");
2955 static const struct got_error *
2956 cmd_diff(int argc, char *argv[])
2958 const struct got_error *error;
2959 struct got_repository *repo = NULL;
2960 struct got_worktree *worktree = NULL;
2961 char *cwd = NULL, *repo_path = NULL;
2962 struct got_object_id *id1 = NULL, *id2 = NULL;
2963 const char *id_str1 = NULL, *id_str2 = NULL;
2964 char *label1 = NULL, *label2 = NULL;
2966 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2971 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2976 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2979 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2982 err(1, "-C option %s", errstr);
2985 repo_path = realpath(optarg, NULL);
2986 if (repo_path == NULL)
2987 return got_error_from_errno2("realpath",
2989 got_path_strip_trailing_slashes(repo_path);
2995 ignore_whitespace = 1;
3006 cwd = getcwd(NULL, 0);
3008 error = got_error_from_errno("getcwd");
3011 error = got_worktree_open(&worktree, cwd);
3012 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3015 if (worktree == NULL) {
3016 error = got_error(GOT_ERR_NOT_WORKTREE);
3021 "-r option can't be used when diffing a work tree");
3022 repo_path = strdup(got_worktree_get_repo_path(worktree));
3023 if (repo_path == NULL) {
3024 error = got_error_from_errno("strdup");
3028 error = got_worktree_resolve_path(&path, worktree,
3035 error = got_error_from_errno("strdup");
3039 } else if (argc == 2) {
3041 errx(1, "-s option can't be used when diffing "
3042 "objects in repository");
3045 if (worktree && repo_path == NULL) {
3047 strdup(got_worktree_get_repo_path(worktree));
3048 if (repo_path == NULL) {
3049 error = got_error_from_errno("strdup");
3056 if (repo_path == NULL) {
3057 repo_path = getcwd(NULL, 0);
3058 if (repo_path == NULL)
3059 return got_error_from_errno("getcwd");
3062 error = got_repo_open(&repo, repo_path, NULL);
3067 error = apply_unveil(got_repo_get_path(repo), 1,
3068 worktree ? got_worktree_get_root_path(worktree) : NULL);
3073 struct print_diff_arg arg;
3074 struct got_pathlist_head paths;
3079 error = got_object_id_str(&id_str,
3080 got_worktree_get_base_commit_id(worktree));
3084 arg.worktree = worktree;
3085 arg.diff_context = diff_context;
3086 arg.id_str = id_str;
3087 arg.header_shown = 0;
3088 arg.diff_staged = diff_staged;
3089 arg.ignore_whitespace = ignore_whitespace;
3091 error = got_pathlist_append(&paths, path, NULL);
3095 error = got_worktree_status(worktree, &paths, repo, print_diff,
3096 &arg, check_cancelled, NULL);
3098 got_pathlist_free(&paths);
3102 error = got_repo_match_object_id(&id1, &label1, id_str1,
3103 GOT_OBJ_TYPE_ANY, 1, repo);
3107 error = got_repo_match_object_id(&id2, &label2, id_str2,
3108 GOT_OBJ_TYPE_ANY, 1, repo);
3112 error = got_object_get_type(&type1, repo, id1);
3116 error = got_object_get_type(&type2, repo, id2);
3120 if (type1 != type2) {
3121 error = got_error(GOT_ERR_OBJ_TYPE);
3126 case GOT_OBJ_TYPE_BLOB:
3127 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3128 diff_context, ignore_whitespace, repo, stdout);
3130 case GOT_OBJ_TYPE_TREE:
3131 error = got_diff_objects_as_trees(id1, id2, "", "",
3132 diff_context, ignore_whitespace, repo, stdout);
3134 case GOT_OBJ_TYPE_COMMIT:
3135 printf("diff %s %s\n", label1, label2);
3136 error = got_diff_objects_as_commits(id1, id2, diff_context,
3137 ignore_whitespace, repo, stdout);
3140 error = got_error(GOT_ERR_OBJ_TYPE);
3149 got_worktree_close(worktree);
3151 const struct got_error *repo_error;
3152 repo_error = got_repo_close(repo);
3163 "usage: %s blame [-c commit] [-r repository-path] path\n",
3172 char datebuf[11]; /* YYYY-MM-DD + NUL */
3175 struct blame_cb_args {
3176 struct blame_line *lines;
3180 off_t *line_offsets;
3182 struct got_repository *repo;
3185 static const struct got_error *
3186 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3188 const struct got_error *err = NULL;
3189 struct blame_cb_args *a = arg;
3190 struct blame_line *bline;
3192 size_t linesize = 0;
3193 struct got_commit_object *commit = NULL;
3196 time_t committer_time;
3198 if (nlines != a->nlines ||
3199 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3200 return got_error(GOT_ERR_RANGE);
3202 if (sigint_received)
3203 return got_error(GOT_ERR_ITER_COMPLETED);
3206 return NULL; /* no change in this commit */
3208 /* Annotate this line. */
3209 bline = &a->lines[lineno - 1];
3210 if (bline->annotated)
3212 err = got_object_id_str(&bline->id_str, id);
3216 err = got_object_open_as_commit(&commit, a->repo, id);
3220 bline->committer = strdup(got_object_commit_get_committer(commit));
3221 if (bline->committer == NULL) {
3222 err = got_error_from_errno("strdup");
3226 committer_time = got_object_commit_get_committer_time(commit);
3227 if (localtime_r(&committer_time, &tm) == NULL)
3228 return got_error_from_errno("localtime_r");
3229 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3230 &tm) >= sizeof(bline->datebuf)) {
3231 err = got_error(GOT_ERR_NO_SPACE);
3234 bline->annotated = 1;
3236 /* Print lines annotated so far. */
3237 bline = &a->lines[a->lineno_cur - 1];
3238 if (!bline->annotated)
3241 offset = a->line_offsets[a->lineno_cur - 1];
3242 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3243 err = got_error_from_errno("fseeko");
3247 while (bline->annotated) {
3248 char *smallerthan, *at, *nl, *committer;
3251 if (getline(&line, &linesize, a->f) == -1) {
3253 err = got_error_from_errno("getline");
3257 committer = bline->committer;
3258 smallerthan = strchr(committer, '<');
3259 if (smallerthan && smallerthan[1] != '\0')
3260 committer = smallerthan + 1;
3261 at = strchr(committer, '@');
3264 len = strlen(committer);
3266 committer[8] = '\0';
3268 nl = strchr(line, '\n');
3271 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3272 bline->id_str, bline->datebuf, committer, line);
3275 bline = &a->lines[a->lineno_cur - 1];
3279 got_object_commit_close(commit);
3284 static const struct got_error *
3285 cmd_blame(int argc, char *argv[])
3287 const struct got_error *error;
3288 struct got_repository *repo = NULL;
3289 struct got_worktree *worktree = NULL;
3290 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3291 struct got_object_id *obj_id = NULL;
3292 struct got_object_id *commit_id = NULL;
3293 struct got_blob_object *blob = NULL;
3294 char *commit_id_str = NULL;
3295 struct blame_cb_args bca;
3296 int ch, obj_type, i;
3299 memset(&bca, 0, sizeof(bca));
3302 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3307 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3310 commit_id_str = optarg;
3313 repo_path = realpath(optarg, NULL);
3314 if (repo_path == NULL)
3315 return got_error_from_errno2("realpath",
3317 got_path_strip_trailing_slashes(repo_path);
3333 cwd = getcwd(NULL, 0);
3335 error = got_error_from_errno("getcwd");
3338 if (repo_path == NULL) {
3339 error = got_worktree_open(&worktree, cwd);
3340 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3346 strdup(got_worktree_get_repo_path(worktree));
3347 if (repo_path == NULL)
3348 error = got_error_from_errno("strdup");
3352 repo_path = strdup(cwd);
3353 if (repo_path == NULL) {
3354 error = got_error_from_errno("strdup");
3360 error = got_repo_open(&repo, repo_path, NULL);
3364 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3369 const char *prefix = got_worktree_get_path_prefix(worktree);
3370 char *p, *worktree_subdir = cwd +
3371 strlen(got_worktree_get_root_path(worktree));
3372 if (asprintf(&p, "%s%s%s%s%s",
3373 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3374 worktree_subdir, worktree_subdir[0] ? "/" : "",
3376 error = got_error_from_errno("asprintf");
3379 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3382 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3387 if (commit_id_str == NULL) {
3388 struct got_reference *head_ref;
3389 error = got_ref_open(&head_ref, repo, worktree ?
3390 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3393 error = got_ref_resolve(&commit_id, repo, head_ref);
3394 got_ref_close(head_ref);
3398 error = got_repo_match_object_id(&commit_id, NULL,
3399 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3404 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3408 error = got_object_get_type(&obj_type, repo, obj_id);
3412 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3413 error = got_error(GOT_ERR_OBJ_TYPE);
3417 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3420 bca.f = got_opentemp();
3421 if (bca.f == NULL) {
3422 error = got_error_from_errno("got_opentemp");
3425 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3426 &bca.line_offsets, bca.f, blob);
3427 if (error || bca.nlines == 0)
3430 /* Don't include \n at EOF in the blame line count. */
3431 if (bca.line_offsets[bca.nlines - 1] == filesize)
3434 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3435 if (bca.lines == NULL) {
3436 error = got_error_from_errno("calloc");
3440 bca.nlines_prec = 0;
3448 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3449 check_cancelled, NULL);
3457 got_object_blob_close(blob);
3459 got_worktree_close(worktree);
3461 const struct got_error *repo_error;
3462 repo_error = got_repo_close(repo);
3467 for (i = 0; i < bca.nlines; i++) {
3468 struct blame_line *bline = &bca.lines[i];
3469 free(bline->id_str);
3470 free(bline->committer);
3474 free(bca.line_offsets);
3475 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3476 error = got_error_from_errno("fclose");
3484 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3490 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3491 const char *root_path)
3493 int is_root_path = (strcmp(path, root_path) == 0);
3494 const char *modestr = "";
3495 mode_t mode = got_tree_entry_get_mode(te);
3497 path += strlen(root_path);
3498 while (path[0] == '/')
3501 if (got_object_tree_entry_is_submodule(te))
3503 else if (S_ISLNK(mode))
3505 else if (S_ISDIR(mode))
3507 else if (mode & S_IXUSR)
3510 printf("%s%s%s%s%s\n", id ? id : "", path,
3511 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3514 static const struct got_error *
3515 print_tree(const char *path, struct got_object_id *commit_id,
3516 int show_ids, int recurse, const char *root_path,
3517 struct got_repository *repo)
3519 const struct got_error *err = NULL;
3520 struct got_object_id *tree_id = NULL;
3521 struct got_tree_object *tree = NULL;
3524 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3528 err = got_object_open_as_tree(&tree, repo, tree_id);
3531 nentries = got_object_tree_get_nentries(tree);
3532 for (i = 0; i < nentries; i++) {
3533 struct got_tree_entry *te;
3536 if (sigint_received || sigpipe_received)
3539 te = got_object_tree_get_entry(tree, i);
3542 err = got_object_id_str(&id_str,
3543 got_tree_entry_get_id(te));
3546 if (asprintf(&id, "%s ", id_str) == -1) {
3547 err = got_error_from_errno("asprintf");
3553 print_entry(te, id, path, root_path);
3556 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3558 if (asprintf(&child_path, "%s%s%s", path,
3559 path[0] == '/' && path[1] == '\0' ? "" : "/",
3560 got_tree_entry_get_name(te)) == -1) {
3561 err = got_error_from_errno("asprintf");
3564 err = print_tree(child_path, commit_id, show_ids, 1,
3573 got_object_tree_close(tree);
3578 static const struct got_error *
3579 cmd_tree(int argc, char *argv[])
3581 const struct got_error *error;
3582 struct got_repository *repo = NULL;
3583 struct got_worktree *worktree = NULL;
3585 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3586 struct got_object_id *commit_id = NULL;
3587 char *commit_id_str = NULL;
3588 int show_ids = 0, recurse = 0;
3592 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3597 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3600 commit_id_str = optarg;
3603 repo_path = realpath(optarg, NULL);
3604 if (repo_path == NULL)
3605 return got_error_from_errno2("realpath",
3607 got_path_strip_trailing_slashes(repo_path);
3631 cwd = getcwd(NULL, 0);
3633 error = got_error_from_errno("getcwd");
3636 if (repo_path == NULL) {
3637 error = got_worktree_open(&worktree, cwd);
3638 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3644 strdup(got_worktree_get_repo_path(worktree));
3645 if (repo_path == NULL)
3646 error = got_error_from_errno("strdup");
3650 repo_path = strdup(cwd);
3651 if (repo_path == NULL) {
3652 error = got_error_from_errno("strdup");
3658 error = got_repo_open(&repo, repo_path, NULL);
3662 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3668 char *p, *worktree_subdir = cwd +
3669 strlen(got_worktree_get_root_path(worktree));
3670 if (asprintf(&p, "%s/%s",
3671 got_worktree_get_path_prefix(worktree),
3672 worktree_subdir) == -1) {
3673 error = got_error_from_errno("asprintf");
3676 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3683 if (in_repo_path == NULL) {
3684 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3689 if (commit_id_str == NULL) {
3690 struct got_reference *head_ref;
3691 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3694 error = got_ref_resolve(&commit_id, repo, head_ref);
3695 got_ref_close(head_ref);
3699 error = got_repo_match_object_id(&commit_id, NULL,
3700 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3705 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3706 in_repo_path, repo);
3713 got_worktree_close(worktree);
3715 const struct got_error *repo_error;
3716 repo_error = got_repo_close(repo);
3726 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3730 static const struct got_error *
3731 print_status(void *arg, unsigned char status, unsigned char staged_status,
3732 const char *path, struct got_object_id *blob_id,
3733 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3734 int dirfd, const char *de_name)
3736 if (status == staged_status && (status == GOT_STATUS_DELETE))
3737 status = GOT_STATUS_NO_CHANGE;
3738 printf("%c%c %s\n", status, staged_status, path);
3742 static const struct got_error *
3743 cmd_status(int argc, char *argv[])
3745 const struct got_error *error = NULL;
3746 struct got_repository *repo = NULL;
3747 struct got_worktree *worktree = NULL;
3749 struct got_pathlist_head paths;
3750 struct got_pathlist_entry *pe;
3755 while ((ch = getopt(argc, argv, "")) != -1) {
3767 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3771 cwd = getcwd(NULL, 0);
3773 error = got_error_from_errno("getcwd");
3777 error = got_worktree_open(&worktree, cwd);
3781 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3786 error = apply_unveil(got_repo_get_path(repo), 1,
3787 got_worktree_get_root_path(worktree));
3791 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3795 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3796 check_cancelled, NULL);
3798 TAILQ_FOREACH(pe, &paths, entry)
3799 free((char *)pe->path);
3800 got_pathlist_free(&paths);
3809 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3814 static const struct got_error *
3815 list_refs(struct got_repository *repo)
3817 static const struct got_error *err = NULL;
3818 struct got_reflist_head refs;
3819 struct got_reflist_entry *re;
3821 SIMPLEQ_INIT(&refs);
3822 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3826 SIMPLEQ_FOREACH(re, &refs, entry) {
3828 refstr = got_ref_to_str(re->ref);
3830 return got_error_from_errno("got_ref_to_str");
3831 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3835 got_ref_list_free(&refs);
3839 static const struct got_error *
3840 delete_ref(struct got_repository *repo, const char *refname)
3842 const struct got_error *err = NULL;
3843 struct got_reference *ref;
3845 err = got_ref_open(&ref, repo, refname, 0);
3849 err = got_ref_delete(ref, repo);
3854 static const struct got_error *
3855 add_ref(struct got_repository *repo, const char *refname, const char *target)
3857 const struct got_error *err = NULL;
3858 struct got_object_id *id;
3859 struct got_reference *ref = NULL;
3862 * Don't let the user create a reference name with a leading '-'.
3863 * While technically a valid reference name, this case is usually
3864 * an unintended typo.
3866 if (refname[0] == '-')
3867 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3869 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3872 struct got_reference *target_ref;
3874 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3876 err = got_ref_open(&target_ref, repo, target, 0);
3879 err = got_ref_resolve(&id, repo, target_ref);
3880 got_ref_close(target_ref);
3885 err = got_ref_alloc(&ref, refname, id);
3889 err = got_ref_write(ref, repo);
3897 static const struct got_error *
3898 add_symref(struct got_repository *repo, const char *refname, const char *target)
3900 const struct got_error *err = NULL;
3901 struct got_reference *ref = NULL;
3902 struct got_reference *target_ref = NULL;
3905 * Don't let the user create a reference name with a leading '-'.
3906 * While technically a valid reference name, this case is usually
3907 * an unintended typo.
3909 if (refname[0] == '-')
3910 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3912 err = got_ref_open(&target_ref, repo, target, 0);
3916 err = got_ref_alloc_symref(&ref, refname, target_ref);
3920 err = got_ref_write(ref, repo);
3923 got_ref_close(target_ref);
3929 static const struct got_error *
3930 cmd_ref(int argc, char *argv[])
3932 const struct got_error *error = NULL;
3933 struct got_repository *repo = NULL;
3934 struct got_worktree *worktree = NULL;
3935 char *cwd = NULL, *repo_path = NULL;
3936 int ch, do_list = 0, create_symref = 0;
3937 const char *delref = NULL;
3939 /* TODO: Add -s option for adding symbolic references. */
3940 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3946 repo_path = realpath(optarg, NULL);
3947 if (repo_path == NULL)
3948 return got_error_from_errno2("realpath",
3950 got_path_strip_trailing_slashes(repo_path);
3964 if (do_list && delref)
3965 errx(1, "-l and -d options are mutually exclusive\n");
3970 if (do_list || delref) {
3972 errx(1, "-s option cannot be used together with the "
3973 "-l or -d options");
3976 } else if (argc != 2)
3981 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3985 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3986 "sendfd unveil", NULL) == -1)
3990 cwd = getcwd(NULL, 0);
3992 error = got_error_from_errno("getcwd");
3996 if (repo_path == NULL) {
3997 error = got_worktree_open(&worktree, cwd);
3998 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4004 strdup(got_worktree_get_repo_path(worktree));
4005 if (repo_path == NULL)
4006 error = got_error_from_errno("strdup");
4010 repo_path = strdup(cwd);
4011 if (repo_path == NULL) {
4012 error = got_error_from_errno("strdup");
4018 error = got_repo_open(&repo, repo_path, NULL);
4022 error = apply_unveil(got_repo_get_path(repo), do_list,
4023 worktree ? got_worktree_get_root_path(worktree) : NULL);
4028 error = list_refs(repo);
4030 error = delete_ref(repo, delref);
4031 else if (create_symref)
4032 error = add_symref(repo, argv[0], argv[1]);
4034 error = add_ref(repo, argv[0], argv[1]);
4037 got_repo_close(repo);
4039 got_worktree_close(worktree);
4049 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4050 "[name]\n", getprogname());
4054 static const struct got_error *
4055 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4056 struct got_reference *ref)
4058 const struct got_error *err = NULL;
4059 const char *refname, *marker = " ";
4062 refname = got_ref_get_name(ref);
4063 if (worktree && strcmp(refname,
4064 got_worktree_get_head_ref_name(worktree)) == 0) {
4065 struct got_object_id *id = NULL;
4067 err = got_ref_resolve(&id, repo, ref);
4070 if (got_object_id_cmp(id,
4071 got_worktree_get_base_commit_id(worktree)) == 0)
4078 if (strncmp(refname, "refs/heads/", 11) == 0)
4080 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4083 refstr = got_ref_to_str(ref);
4085 return got_error_from_errno("got_ref_to_str");
4087 printf("%s%s: %s\n", marker, refname, refstr);
4092 static const struct got_error *
4093 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4095 const char *refname;
4097 if (worktree == NULL)
4098 return got_error(GOT_ERR_NOT_WORKTREE);
4100 refname = got_worktree_get_head_ref_name(worktree);
4102 if (strncmp(refname, "refs/heads/", 11) == 0)
4104 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4107 printf("%s\n", refname);
4112 static const struct got_error *
4113 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4115 static const struct got_error *err = NULL;
4116 struct got_reflist_head refs;
4117 struct got_reflist_entry *re;
4118 struct got_reference *temp_ref = NULL;
4119 int rebase_in_progress, histedit_in_progress;
4121 SIMPLEQ_INIT(&refs);
4124 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4129 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4134 if (rebase_in_progress || histedit_in_progress) {
4135 err = got_ref_open(&temp_ref, repo,
4136 got_worktree_get_head_ref_name(worktree), 0);
4139 list_branch(repo, worktree, temp_ref);
4140 got_ref_close(temp_ref);
4144 err = got_ref_list(&refs, repo, "refs/heads",
4145 got_ref_cmp_by_name, NULL);
4149 SIMPLEQ_FOREACH(re, &refs, entry)
4150 list_branch(repo, worktree, re->ref);
4152 got_ref_list_free(&refs);
4156 static const struct got_error *
4157 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4158 const char *branch_name)
4160 const struct got_error *err = NULL;
4161 struct got_reference *ref = NULL;
4164 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4165 return got_error_from_errno("asprintf");
4167 err = got_ref_open(&ref, repo, refname, 0);
4172 strcmp(got_worktree_get_head_ref_name(worktree),
4173 got_ref_get_name(ref)) == 0) {
4174 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4175 "will not delete this work tree's current branch");
4179 err = got_ref_delete(ref, repo);
4187 static const struct got_error *
4188 add_branch(struct got_repository *repo, const char *branch_name,
4189 struct got_object_id *base_commit_id)
4191 const struct got_error *err = NULL;
4192 struct got_reference *ref = NULL;
4193 char *base_refname = NULL, *refname = NULL;
4196 * Don't let the user create a branch name with a leading '-'.
4197 * While technically a valid reference name, this case is usually
4198 * an unintended typo.
4200 if (branch_name[0] == '-')
4201 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4203 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4204 err = got_error_from_errno("asprintf");
4208 err = got_ref_open(&ref, repo, refname, 0);
4210 err = got_error(GOT_ERR_BRANCH_EXISTS);
4212 } else if (err->code != GOT_ERR_NOT_REF)
4215 err = got_ref_alloc(&ref, refname, base_commit_id);
4219 err = got_ref_write(ref, repo);
4228 static const struct got_error *
4229 cmd_branch(int argc, char *argv[])
4231 const struct got_error *error = NULL;
4232 struct got_repository *repo = NULL;
4233 struct got_worktree *worktree = NULL;
4234 char *cwd = NULL, *repo_path = NULL;
4235 int ch, do_list = 0, do_show = 0, do_update = 1;
4236 const char *delref = NULL, *commit_id_arg = NULL;
4237 struct got_reference *ref = NULL;
4238 struct got_pathlist_head paths;
4239 struct got_pathlist_entry *pe;
4240 struct got_object_id *commit_id = NULL;
4241 char *commit_id_str = NULL;
4245 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4248 commit_id_arg = optarg;
4254 repo_path = realpath(optarg, NULL);
4255 if (repo_path == NULL)
4256 return got_error_from_errno2("realpath",
4258 got_path_strip_trailing_slashes(repo_path);
4272 if (do_list && delref)
4273 errx(1, "-l and -d options are mutually exclusive\n");
4278 if (!do_list && !delref && argc == 0)
4281 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4282 errx(1, "-c option can only be used when creating a branch");
4284 if (do_list || delref) {
4287 } else if (!do_show && argc != 1)
4291 if (do_list || do_show) {
4292 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4296 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4297 "sendfd unveil", NULL) == -1)
4301 cwd = getcwd(NULL, 0);
4303 error = got_error_from_errno("getcwd");
4307 if (repo_path == NULL) {
4308 error = got_worktree_open(&worktree, cwd);
4309 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4315 strdup(got_worktree_get_repo_path(worktree));
4316 if (repo_path == NULL)
4317 error = got_error_from_errno("strdup");
4321 repo_path = strdup(cwd);
4322 if (repo_path == NULL) {
4323 error = got_error_from_errno("strdup");
4329 error = got_repo_open(&repo, repo_path, NULL);
4333 error = apply_unveil(got_repo_get_path(repo), do_list,
4334 worktree ? got_worktree_get_root_path(worktree) : NULL);
4339 error = show_current_branch(repo, worktree);
4341 error = list_branches(repo, worktree);
4343 error = delete_branch(repo, worktree, delref);
4345 if (commit_id_arg == NULL)
4346 commit_id_arg = worktree ?
4347 got_worktree_get_head_ref_name(worktree) :
4349 error = got_repo_match_object_id(&commit_id, NULL,
4350 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4353 error = add_branch(repo, argv[0], commit_id);
4356 if (worktree && do_update) {
4357 int did_something = 0;
4358 char *branch_refname = NULL;
4360 error = got_object_id_str(&commit_id_str, commit_id);
4363 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4367 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4369 error = got_error_from_errno("asprintf");
4372 error = got_ref_open(&ref, repo, branch_refname, 0);
4373 free(branch_refname);
4376 error = switch_head_ref(ref, commit_id, worktree,
4380 error = got_worktree_set_base_commit_id(worktree, repo,
4384 error = got_worktree_checkout_files(worktree, &paths,
4385 repo, update_progress, &did_something,
4386 check_cancelled, NULL);
4390 printf("Updated to commit %s\n", commit_id_str);
4397 got_repo_close(repo);
4399 got_worktree_close(worktree);
4403 free(commit_id_str);
4404 TAILQ_FOREACH(pe, &paths, entry)
4405 free((char *)pe->path);
4406 got_pathlist_free(&paths);
4415 "usage: %s tag [-c commit] [-r repository] [-l] "
4416 "[-m message] name\n", getprogname());
4421 static const struct got_error *
4422 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4424 const struct got_error *err = NULL;
4425 struct got_reflist_entry *re, *se, *new;
4426 struct got_object_id *re_id, *se_id;
4427 struct got_tag_object *re_tag, *se_tag;
4428 time_t re_time, se_time;
4430 SIMPLEQ_FOREACH(re, tags, entry) {
4431 se = SIMPLEQ_FIRST(sorted);
4433 err = got_reflist_entry_dup(&new, re);
4436 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4439 err = got_ref_resolve(&re_id, repo, re->ref);
4442 err = got_object_open_as_tag(&re_tag, repo, re_id);
4446 re_time = got_object_tag_get_tagger_time(re_tag);
4447 got_object_tag_close(re_tag);
4451 err = got_ref_resolve(&se_id, repo, re->ref);
4454 err = got_object_open_as_tag(&se_tag, repo, se_id);
4458 se_time = got_object_tag_get_tagger_time(se_tag);
4459 got_object_tag_close(se_tag);
4461 if (se_time > re_time) {
4462 err = got_reflist_entry_dup(&new, re);
4465 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4468 se = SIMPLEQ_NEXT(se, entry);
4477 static const struct got_error *
4478 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4480 static const struct got_error *err = NULL;
4481 struct got_reflist_head refs;
4482 struct got_reflist_entry *re;
4484 SIMPLEQ_INIT(&refs);
4486 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4490 SIMPLEQ_FOREACH(re, &refs, entry) {
4491 const char *refname;
4492 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4496 struct got_object_id *id;
4497 struct got_tag_object *tag;
4498 struct got_commit_object *commit = NULL;
4500 refname = got_ref_get_name(re->ref);
4501 if (strncmp(refname, "refs/tags/", 10) != 0)
4504 refstr = got_ref_to_str(re->ref);
4505 if (refstr == NULL) {
4506 err = got_error_from_errno("got_ref_to_str");
4509 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4512 err = got_ref_resolve(&id, repo, re->ref);
4515 err = got_object_open_as_tag(&tag, repo, id);
4517 if (err->code != GOT_ERR_OBJ_TYPE) {
4521 /* "lightweight" tag */
4522 err = got_object_open_as_commit(&commit, repo, id);
4527 tagger = got_object_commit_get_committer(commit);
4529 got_object_commit_get_committer_time(commit);
4530 err = got_object_id_str(&id_str, id);
4536 tagger = got_object_tag_get_tagger(tag);
4537 tagger_time = got_object_tag_get_tagger_time(tag);
4538 err = got_object_id_str(&id_str,
4539 got_object_tag_get_object_id(tag));
4543 printf("from: %s\n", tagger);
4544 datestr = get_datestr(&tagger_time, datebuf);
4546 printf("date: %s UTC\n", datestr);
4548 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4550 switch (got_object_tag_get_object_type(tag)) {
4551 case GOT_OBJ_TYPE_BLOB:
4552 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4555 case GOT_OBJ_TYPE_TREE:
4556 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4559 case GOT_OBJ_TYPE_COMMIT:
4560 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4563 case GOT_OBJ_TYPE_TAG:
4564 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4573 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4576 got_object_commit_close(commit);
4578 tagmsg0 = strdup(got_object_tag_get_message(tag));
4579 got_object_tag_close(tag);
4580 if (tagmsg0 == NULL) {
4581 err = got_error_from_errno("strdup");
4588 line = strsep(&tagmsg, "\n");
4590 printf(" %s\n", line);
4595 got_ref_list_free(&refs);
4599 static const struct got_error *
4600 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4601 const char *tag_name, const char *repo_path)
4603 const struct got_error *err = NULL;
4604 char *template = NULL, *initial_content = NULL;
4605 char *editor = NULL;
4608 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4609 err = got_error_from_errno("asprintf");
4613 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4614 commit_id_str, tag_name) == -1) {
4615 err = got_error_from_errno("asprintf");
4619 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4623 dprintf(fd, initial_content);
4626 err = get_editor(&editor);
4629 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4631 free(initial_content);
4635 /* Editor is done; we can now apply unveil(2) */
4637 err = apply_unveil(repo_path, 0, NULL);
4646 static const struct got_error *
4647 add_tag(struct got_repository *repo, const char *tag_name,
4648 const char *commit_arg, const char *tagmsg_arg)
4650 const struct got_error *err = NULL;
4651 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4652 char *label = NULL, *commit_id_str = NULL;
4653 struct got_reference *ref = NULL;
4654 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4655 char *tagmsg_path = NULL, *tag_id_str = NULL;
4656 int preserve_tagmsg = 0;
4659 * Don't let the user create a tag name with a leading '-'.
4660 * While technically a valid reference name, this case is usually
4661 * an unintended typo.
4663 if (tag_name[0] == '-')
4664 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4666 err = get_author(&tagger, repo);
4670 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4671 GOT_OBJ_TYPE_COMMIT, 1, repo);
4675 err = got_object_id_str(&commit_id_str, commit_id);
4679 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4680 refname = strdup(tag_name);
4681 if (refname == NULL) {
4682 err = got_error_from_errno("strdup");
4686 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4687 err = got_error_from_errno("asprintf");
4691 err = got_ref_open(&ref, repo, refname, 0);
4693 err = got_error(GOT_ERR_TAG_EXISTS);
4695 } else if (err->code != GOT_ERR_NOT_REF)
4698 if (tagmsg_arg == NULL) {
4699 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4700 tag_name, got_repo_get_path(repo));
4702 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4703 tagmsg_path != NULL)
4704 preserve_tagmsg = 1;
4709 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4710 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4713 preserve_tagmsg = 1;
4717 err = got_ref_alloc(&ref, refname, tag_id);
4720 preserve_tagmsg = 1;
4724 err = got_ref_write(ref, repo);
4727 preserve_tagmsg = 1;
4731 err = got_object_id_str(&tag_id_str, tag_id);
4734 preserve_tagmsg = 1;
4737 printf("Created tag %s\n", tag_id_str);
4739 if (preserve_tagmsg) {
4740 fprintf(stderr, "%s: tag message preserved in %s\n",
4741 getprogname(), tagmsg_path);
4742 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4743 err = got_error_from_errno2("unlink", tagmsg_path);
4748 free(commit_id_str);
4756 static const struct got_error *
4757 cmd_tag(int argc, char *argv[])
4759 const struct got_error *error = NULL;
4760 struct got_repository *repo = NULL;
4761 struct got_worktree *worktree = NULL;
4762 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4763 char *gitconfig_path = NULL;
4764 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4765 int ch, do_list = 0;
4767 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4770 commit_id_arg = optarg;
4776 repo_path = realpath(optarg, NULL);
4777 if (repo_path == NULL)
4778 return got_error_from_errno2("realpath",
4780 got_path_strip_trailing_slashes(repo_path);
4795 if (commit_id_arg != NULL)
4796 errx(1, "-c option can only be used when creating a tag");
4798 errx(1, "-l and -m options are mutually exclusive");
4801 } else if (argc != 1)
4808 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4812 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4813 "sendfd unveil", NULL) == -1)
4817 cwd = getcwd(NULL, 0);
4819 error = got_error_from_errno("getcwd");
4823 if (repo_path == NULL) {
4824 error = got_worktree_open(&worktree, cwd);
4825 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4831 strdup(got_worktree_get_repo_path(worktree));
4832 if (repo_path == NULL)
4833 error = got_error_from_errno("strdup");
4837 repo_path = strdup(cwd);
4838 if (repo_path == NULL) {
4839 error = got_error_from_errno("strdup");
4846 error = got_repo_open(&repo, repo_path, NULL);
4849 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4852 error = list_tags(repo, worktree);
4854 error = get_gitconfig_path(&gitconfig_path);
4857 error = got_repo_open(&repo, repo_path, gitconfig_path);
4862 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4867 if (commit_id_arg == NULL) {
4868 struct got_reference *head_ref;
4869 struct got_object_id *commit_id;
4870 error = got_ref_open(&head_ref, repo,
4871 worktree ? got_worktree_get_head_ref_name(worktree)
4875 error = got_ref_resolve(&commit_id, repo, head_ref);
4876 got_ref_close(head_ref);
4879 error = got_object_id_str(&commit_id_str, commit_id);
4885 error = add_tag(repo, tag_name,
4886 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4890 got_repo_close(repo);
4892 got_worktree_close(worktree);
4895 free(gitconfig_path);
4896 free(commit_id_str);
4903 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4908 static const struct got_error *
4909 add_progress(void *arg, unsigned char status, const char *path)
4911 while (path[0] == '/')
4913 printf("%c %s\n", status, path);
4917 static const struct got_error *
4918 cmd_add(int argc, char *argv[])
4920 const struct got_error *error = NULL;
4921 struct got_repository *repo = NULL;
4922 struct got_worktree *worktree = NULL;
4924 struct got_pathlist_head paths;
4925 struct got_pathlist_entry *pe;
4926 int ch, can_recurse = 0, no_ignores = 0;
4930 while ((ch = getopt(argc, argv, "IR")) != -1) {
4948 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4955 cwd = getcwd(NULL, 0);
4957 error = got_error_from_errno("getcwd");
4961 error = got_worktree_open(&worktree, cwd);
4965 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4970 error = apply_unveil(got_repo_get_path(repo), 1,
4971 got_worktree_get_root_path(worktree));
4975 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4979 if (!can_recurse && no_ignores) {
4980 error = got_error_msg(GOT_ERR_BAD_PATH,
4981 "disregarding ignores requires -R option");
4989 TAILQ_FOREACH(pe, &paths, entry) {
4990 if (asprintf(&ondisk_path, "%s/%s",
4991 got_worktree_get_root_path(worktree),
4993 error = got_error_from_errno("asprintf");
4996 if (lstat(ondisk_path, &sb) == -1) {
4997 if (errno == ENOENT) {
5001 error = got_error_from_errno2("lstat",
5007 if (S_ISDIR(sb.st_mode)) {
5008 error = got_error_msg(GOT_ERR_BAD_PATH,
5009 "adding directories requires -R option");
5015 error = got_worktree_schedule_add(worktree, &paths, add_progress,
5016 NULL, repo, no_ignores);
5019 got_repo_close(repo);
5021 got_worktree_close(worktree);
5022 TAILQ_FOREACH(pe, &paths, entry)
5023 free((char *)pe->path);
5024 got_pathlist_free(&paths);
5032 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5037 static const struct got_error *
5038 print_remove_status(void *arg, unsigned char status,
5039 unsigned char staged_status, const char *path)
5041 while (path[0] == '/')
5043 if (status == GOT_STATUS_NONEXISTENT)
5045 if (status == staged_status && (status == GOT_STATUS_DELETE))
5046 status = GOT_STATUS_NO_CHANGE;
5047 printf("%c%c %s\n", status, staged_status, path);
5051 static const struct got_error *
5052 cmd_remove(int argc, char *argv[])
5054 const struct got_error *error = NULL;
5055 struct got_worktree *worktree = NULL;
5056 struct got_repository *repo = NULL;
5058 struct got_pathlist_head paths;
5059 struct got_pathlist_entry *pe;
5060 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5064 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5067 delete_local_mods = 1;
5085 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5092 cwd = getcwd(NULL, 0);
5094 error = got_error_from_errno("getcwd");
5097 error = got_worktree_open(&worktree, cwd);
5101 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5106 error = apply_unveil(got_repo_get_path(repo), 1,
5107 got_worktree_get_root_path(worktree));
5111 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5118 TAILQ_FOREACH(pe, &paths, entry) {
5119 if (asprintf(&ondisk_path, "%s/%s",
5120 got_worktree_get_root_path(worktree),
5122 error = got_error_from_errno("asprintf");
5125 if (lstat(ondisk_path, &sb) == -1) {
5126 if (errno == ENOENT) {
5130 error = got_error_from_errno2("lstat",
5136 if (S_ISDIR(sb.st_mode)) {
5137 error = got_error_msg(GOT_ERR_BAD_PATH,
5138 "removing directories requires -R option");
5144 error = got_worktree_schedule_delete(worktree, &paths,
5145 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5148 got_repo_close(repo);
5150 got_worktree_close(worktree);
5151 TAILQ_FOREACH(pe, &paths, entry)
5152 free((char *)pe->path);
5153 got_pathlist_free(&paths);
5161 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5162 "path ...\n", getprogname());
5166 static const struct got_error *
5167 revert_progress(void *arg, unsigned char status, const char *path)
5169 if (status == GOT_STATUS_UNVERSIONED)
5172 while (path[0] == '/')
5174 printf("%c %s\n", status, path);
5178 struct choose_patch_arg {
5179 FILE *patch_script_file;
5183 static const struct got_error *
5184 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5185 int nchanges, const char *action)
5188 size_t linesize = 0;
5192 case GOT_STATUS_ADD:
5193 printf("A %s\n%s this addition? [y/n] ", path, action);
5195 case GOT_STATUS_DELETE:
5196 printf("D %s\n%s this deletion? [y/n] ", path, action);
5198 case GOT_STATUS_MODIFY:
5199 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5200 return got_error_from_errno("fseek");
5201 printf(GOT_COMMIT_SEP_STR);
5202 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5204 if (ferror(patch_file))
5205 return got_error_from_errno("getline");
5206 printf(GOT_COMMIT_SEP_STR);
5207 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5208 path, n, nchanges, action);
5211 return got_error_path(path, GOT_ERR_FILE_STATUS);
5217 static const struct got_error *
5218 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5219 FILE *patch_file, int n, int nchanges)
5221 const struct got_error *err = NULL;
5223 size_t linesize = 0;
5226 struct choose_patch_arg *a = arg;
5228 *choice = GOT_PATCH_CHOICE_NONE;
5230 if (a->patch_script_file) {
5232 err = show_change(status, path, patch_file, n, nchanges,
5236 linelen = getline(&line, &linesize, a->patch_script_file);
5237 if (linelen == -1) {
5238 if (ferror(a->patch_script_file))
5239 return got_error_from_errno("getline");
5242 nl = strchr(line, '\n');
5245 if (strcmp(line, "y") == 0) {
5246 *choice = GOT_PATCH_CHOICE_YES;
5248 } else if (strcmp(line, "n") == 0) {
5249 *choice = GOT_PATCH_CHOICE_NO;
5251 } else if (strcmp(line, "q") == 0 &&
5252 status == GOT_STATUS_MODIFY) {
5253 *choice = GOT_PATCH_CHOICE_QUIT;
5256 printf("invalid response '%s'\n", line);
5261 while (resp != 'y' && resp != 'n' && resp != 'q') {
5262 err = show_change(status, path, patch_file, n, nchanges,
5269 if (status == GOT_STATUS_MODIFY) {
5270 if (resp != 'y' && resp != 'n' && resp != 'q') {
5271 printf("invalid response '%c'\n", resp);
5274 } else if (resp != 'y' && resp != 'n') {
5275 printf("invalid response '%c'\n", resp);
5281 *choice = GOT_PATCH_CHOICE_YES;
5282 else if (resp == 'n')
5283 *choice = GOT_PATCH_CHOICE_NO;
5284 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5285 *choice = GOT_PATCH_CHOICE_QUIT;
5291 static const struct got_error *
5292 cmd_revert(int argc, char *argv[])
5294 const struct got_error *error = NULL;
5295 struct got_worktree *worktree = NULL;
5296 struct got_repository *repo = NULL;
5297 char *cwd = NULL, *path = NULL;
5298 struct got_pathlist_head paths;
5299 struct got_pathlist_entry *pe;
5300 int ch, can_recurse = 0, pflag = 0;
5301 FILE *patch_script_file = NULL;
5302 const char *patch_script_path = NULL;
5303 struct choose_patch_arg cpa;
5307 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5313 patch_script_path = optarg;
5328 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5329 "unveil", NULL) == -1)
5334 if (patch_script_path && !pflag)
5335 errx(1, "-F option can only be used together with -p option");
5337 cwd = getcwd(NULL, 0);
5339 error = got_error_from_errno("getcwd");
5342 error = got_worktree_open(&worktree, cwd);
5346 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5351 if (patch_script_path) {
5352 patch_script_file = fopen(patch_script_path, "r");
5353 if (patch_script_file == NULL) {
5354 error = got_error_from_errno2("fopen",
5359 error = apply_unveil(got_repo_get_path(repo), 1,
5360 got_worktree_get_root_path(worktree));
5364 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5371 TAILQ_FOREACH(pe, &paths, entry) {
5372 if (asprintf(&ondisk_path, "%s/%s",
5373 got_worktree_get_root_path(worktree),
5375 error = got_error_from_errno("asprintf");
5378 if (lstat(ondisk_path, &sb) == -1) {
5379 if (errno == ENOENT) {
5383 error = got_error_from_errno2("lstat",
5389 if (S_ISDIR(sb.st_mode)) {
5390 error = got_error_msg(GOT_ERR_BAD_PATH,
5391 "reverting directories requires -R option");
5397 cpa.patch_script_file = patch_script_file;
5398 cpa.action = "revert";
5399 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5400 pflag ? choose_patch : NULL, &cpa, repo);
5402 if (patch_script_file && fclose(patch_script_file) == EOF &&
5404 error = got_error_from_errno2("fclose", patch_script_path);
5406 got_repo_close(repo);
5408 got_worktree_close(worktree);
5417 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5422 struct collect_commit_logmsg_arg {
5423 const char *cmdline_log;
5425 const char *worktree_path;
5426 const char *branch_name;
5427 const char *repo_path;
5432 static const struct got_error *
5433 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5436 char *initial_content = NULL;
5437 struct got_pathlist_entry *pe;
5438 const struct got_error *err = NULL;
5439 char *template = NULL;
5440 struct collect_commit_logmsg_arg *a = arg;
5444 /* if a message was specified on the command line, just use it */
5445 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5446 len = strlen(a->cmdline_log) + 1;
5447 *logmsg = malloc(len + 1);
5448 if (*logmsg == NULL)
5449 return got_error_from_errno("malloc");
5450 strlcpy(*logmsg, a->cmdline_log, len);
5454 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5455 return got_error_from_errno("asprintf");
5457 if (asprintf(&initial_content,
5458 "\n# changes to be committed on branch %s:\n",
5459 a->branch_name) == -1)
5460 return got_error_from_errno("asprintf");
5462 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5466 dprintf(fd, initial_content);
5468 TAILQ_FOREACH(pe, commitable_paths, entry) {
5469 struct got_commitable *ct = pe->data;
5470 dprintf(fd, "# %c %s\n",
5471 got_commitable_get_status(ct),
5472 got_commitable_get_path(ct));
5476 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5478 free(initial_content);
5481 /* Editor is done; we can now apply unveil(2) */
5483 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5492 static const struct got_error *
5493 cmd_commit(int argc, char *argv[])
5495 const struct got_error *error = NULL;
5496 struct got_worktree *worktree = NULL;
5497 struct got_repository *repo = NULL;
5498 char *cwd = NULL, *id_str = NULL;
5499 struct got_object_id *id = NULL;
5500 const char *logmsg = NULL;
5501 struct collect_commit_logmsg_arg cl_arg;
5502 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5503 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5504 struct got_pathlist_head paths;
5507 cl_arg.logmsg_path = NULL;
5509 while ((ch = getopt(argc, argv, "m:")) != -1) {
5524 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5525 "unveil", NULL) == -1)
5528 cwd = getcwd(NULL, 0);
5530 error = got_error_from_errno("getcwd");
5533 error = got_worktree_open(&worktree, cwd);
5537 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5540 if (rebase_in_progress) {
5541 error = got_error(GOT_ERR_REBASING);
5545 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5550 error = get_gitconfig_path(&gitconfig_path);
5553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5558 error = get_author(&author, repo);
5563 * unveil(2) traverses exec(2); if an editor is used we have
5564 * to apply unveil after the log message has been written.
5566 if (logmsg == NULL || strlen(logmsg) == 0)
5567 error = get_editor(&editor);
5569 error = apply_unveil(got_repo_get_path(repo), 0,
5570 got_worktree_get_root_path(worktree));
5574 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5578 cl_arg.editor = editor;
5579 cl_arg.cmdline_log = logmsg;
5580 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5581 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5582 if (!histedit_in_progress) {
5583 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5584 error = got_error(GOT_ERR_COMMIT_BRANCH);
5587 cl_arg.branch_name += 11;
5589 cl_arg.repo_path = got_repo_get_path(repo);
5590 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5591 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5593 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5594 cl_arg.logmsg_path != NULL)
5595 preserve_logmsg = 1;
5599 error = got_object_id_str(&id_str, id);
5602 printf("Created commit %s\n", id_str);
5604 if (preserve_logmsg) {
5605 fprintf(stderr, "%s: log message preserved in %s\n",
5606 getprogname(), cl_arg.logmsg_path);
5607 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5609 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5610 free(cl_arg.logmsg_path);
5612 got_repo_close(repo);
5614 got_worktree_close(worktree);
5617 free(gitconfig_path);
5624 usage_cherrypick(void)
5626 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5630 static const struct got_error *
5631 cmd_cherrypick(int argc, char *argv[])
5633 const struct got_error *error = NULL;
5634 struct got_worktree *worktree = NULL;
5635 struct got_repository *repo = NULL;
5636 char *cwd = NULL, *commit_id_str = NULL;
5637 struct got_object_id *commit_id = NULL;
5638 struct got_commit_object *commit = NULL;
5639 struct got_object_qid *pid;
5640 struct got_reference *head_ref = NULL;
5641 int ch, did_something = 0;
5643 while ((ch = getopt(argc, argv, "")) != -1) {
5655 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5656 "unveil", NULL) == -1)
5662 cwd = getcwd(NULL, 0);
5664 error = got_error_from_errno("getcwd");
5667 error = got_worktree_open(&worktree, cwd);
5671 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5676 error = apply_unveil(got_repo_get_path(repo), 0,
5677 got_worktree_get_root_path(worktree));
5681 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5682 GOT_OBJ_TYPE_COMMIT, repo);
5683 if (error != NULL) {
5684 struct got_reference *ref;
5685 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5687 error = got_ref_open(&ref, repo, argv[0], 0);
5690 error = got_ref_resolve(&commit_id, repo, ref);
5695 error = got_object_id_str(&commit_id_str, commit_id);
5699 error = got_ref_open(&head_ref, repo,
5700 got_worktree_get_head_ref_name(worktree), 0);
5704 error = check_same_branch(commit_id, head_ref, NULL, repo);
5706 if (error->code != GOT_ERR_ANCESTRY)
5710 error = got_error(GOT_ERR_SAME_BRANCH);
5714 error = got_object_open_as_commit(&commit, repo, commit_id);
5717 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5718 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5719 commit_id, repo, update_progress, &did_something, check_cancelled,
5725 printf("Merged commit %s\n", commit_id_str);
5728 got_object_commit_close(commit);
5729 free(commit_id_str);
5731 got_ref_close(head_ref);
5733 got_worktree_close(worktree);
5735 got_repo_close(repo);
5742 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5746 static const struct got_error *
5747 cmd_backout(int argc, char *argv[])
5749 const struct got_error *error = NULL;
5750 struct got_worktree *worktree = NULL;
5751 struct got_repository *repo = NULL;
5752 char *cwd = NULL, *commit_id_str = NULL;
5753 struct got_object_id *commit_id = NULL;
5754 struct got_commit_object *commit = NULL;
5755 struct got_object_qid *pid;
5756 struct got_reference *head_ref = NULL;
5757 int ch, did_something = 0;
5759 while ((ch = getopt(argc, argv, "")) != -1) {
5771 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5772 "unveil", NULL) == -1)
5778 cwd = getcwd(NULL, 0);
5780 error = got_error_from_errno("getcwd");
5783 error = got_worktree_open(&worktree, cwd);
5787 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5792 error = apply_unveil(got_repo_get_path(repo), 0,
5793 got_worktree_get_root_path(worktree));
5797 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5798 GOT_OBJ_TYPE_COMMIT, repo);
5799 if (error != NULL) {
5800 struct got_reference *ref;
5801 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5803 error = got_ref_open(&ref, repo, argv[0], 0);
5806 error = got_ref_resolve(&commit_id, repo, ref);
5811 error = got_object_id_str(&commit_id_str, commit_id);
5815 error = got_ref_open(&head_ref, repo,
5816 got_worktree_get_head_ref_name(worktree), 0);
5820 error = check_same_branch(commit_id, head_ref, NULL, repo);
5824 error = got_object_open_as_commit(&commit, repo, commit_id);
5827 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5829 error = got_error(GOT_ERR_ROOT_COMMIT);
5833 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5834 update_progress, &did_something, check_cancelled, NULL);
5839 printf("Backed out commit %s\n", commit_id_str);
5842 got_object_commit_close(commit);
5843 free(commit_id_str);
5845 got_ref_close(head_ref);
5847 got_worktree_close(worktree);
5849 got_repo_close(repo);
5856 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5862 trim_logmsg(char *logmsg, int limit)
5867 len = strlen(logmsg);
5871 nl = strchr(logmsg, '\n');
5876 static const struct got_error *
5877 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5879 const struct got_error *err;
5880 char *logmsg0 = NULL;
5883 err = got_object_commit_get_logmsg(&logmsg0, commit);
5888 while (isspace((unsigned char)s[0]))
5891 *logmsg = strdup(s);
5892 if (*logmsg == NULL) {
5893 err = got_error_from_errno("strdup");
5897 trim_logmsg(*logmsg, limit);
5903 static const struct got_error *
5904 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5906 const struct got_error *err;
5907 struct got_commit_object *commit = NULL;
5908 char *id_str = NULL, *logmsg = NULL;
5910 err = got_object_open_as_commit(&commit, repo, id);
5914 err = got_object_id_str(&id_str, id);
5920 err = get_short_logmsg(&logmsg, 42, commit);
5924 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5927 got_object_commit_close(commit);
5932 static const struct got_error *
5933 show_rebase_progress(struct got_commit_object *commit,
5934 struct got_object_id *old_id, struct got_object_id *new_id)
5936 const struct got_error *err;
5937 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5939 err = got_object_id_str(&old_id_str, old_id);
5944 err = got_object_id_str(&new_id_str, new_id);
5949 old_id_str[12] = '\0';
5951 new_id_str[12] = '\0';
5953 err = get_short_logmsg(&logmsg, 42, commit);
5957 printf("%s -> %s: %s\n", old_id_str,
5958 new_id_str ? new_id_str : "no-op change", logmsg);
5966 static const struct got_error *
5967 rebase_progress(void *arg, unsigned char status, const char *path)
5969 unsigned char *rebase_status = arg;
5971 while (path[0] == '/')
5973 printf("%c %s\n", status, path);
5975 if (*rebase_status == GOT_STATUS_CONFLICT)
5977 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5978 *rebase_status = status;
5982 static const struct got_error *
5983 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5984 struct got_reference *branch, struct got_reference *new_base_branch,
5985 struct got_reference *tmp_branch, struct got_repository *repo)
5987 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5988 return got_worktree_rebase_complete(worktree, fileindex,
5989 new_base_branch, tmp_branch, branch, repo);
5992 static const struct got_error *
5993 rebase_commit(struct got_pathlist_head *merged_paths,
5994 struct got_worktree *worktree, struct got_fileindex *fileindex,
5995 struct got_reference *tmp_branch,
5996 struct got_object_id *commit_id, struct got_repository *repo)
5998 const struct got_error *error;
5999 struct got_commit_object *commit;
6000 struct got_object_id *new_commit_id;
6002 error = got_object_open_as_commit(&commit, repo, commit_id);
6006 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6007 worktree, fileindex, tmp_branch, commit, commit_id, repo);
6009 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6011 error = show_rebase_progress(commit, commit_id, NULL);
6013 error = show_rebase_progress(commit, commit_id, new_commit_id);
6014 free(new_commit_id);
6017 got_object_commit_close(commit);
6021 struct check_path_prefix_arg {
6022 const char *path_prefix;
6027 static const struct got_error *
6028 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6029 struct got_blob_object *blob2, struct got_object_id *id1,
6030 struct got_object_id *id2, const char *path1, const char *path2,
6031 mode_t mode1, mode_t mode2, struct got_repository *repo)
6033 struct check_path_prefix_arg *a = arg;
6035 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6036 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6037 return got_error(a->errcode);
6042 static const struct got_error *
6043 check_path_prefix(struct got_object_id *parent_id,
6044 struct got_object_id *commit_id, const char *path_prefix,
6045 int errcode, struct got_repository *repo)
6047 const struct got_error *err;
6048 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6049 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6050 struct check_path_prefix_arg cpp_arg;
6052 if (got_path_is_root_dir(path_prefix))
6055 err = got_object_open_as_commit(&commit, repo, commit_id);
6059 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6063 err = got_object_open_as_tree(&tree1, repo,
6064 got_object_commit_get_tree_id(parent_commit));
6068 err = got_object_open_as_tree(&tree2, repo,
6069 got_object_commit_get_tree_id(commit));
6073 cpp_arg.path_prefix = path_prefix;
6074 while (cpp_arg.path_prefix[0] == '/')
6075 cpp_arg.path_prefix++;
6076 cpp_arg.len = strlen(cpp_arg.path_prefix);
6077 cpp_arg.errcode = errcode;
6078 err = got_diff_tree(tree1, tree2, "", "", repo,
6079 check_path_prefix_in_diff, &cpp_arg, 0);
6082 got_object_tree_close(tree1);
6084 got_object_tree_close(tree2);
6086 got_object_commit_close(commit);
6088 got_object_commit_close(parent_commit);
6092 static const struct got_error *
6093 collect_commits(struct got_object_id_queue *commits,
6094 struct got_object_id *initial_commit_id,
6095 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6096 const char *path_prefix, int path_prefix_errcode,
6097 struct got_repository *repo)
6099 const struct got_error *err = NULL;
6100 struct got_commit_graph *graph = NULL;
6101 struct got_object_id *parent_id = NULL;
6102 struct got_object_qid *qid;
6103 struct got_object_id *commit_id = initial_commit_id;
6105 err = got_commit_graph_open(&graph, "/", 1);
6109 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6110 check_cancelled, NULL);
6113 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6114 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6115 check_cancelled, NULL);
6117 if (err->code == GOT_ERR_ITER_COMPLETED) {
6118 err = got_error_msg(GOT_ERR_ANCESTRY,
6119 "ran out of commits to rebase before "
6120 "youngest common ancestor commit has "
6125 err = check_path_prefix(parent_id, commit_id,
6126 path_prefix, path_prefix_errcode, repo);
6130 err = got_object_qid_alloc(&qid, commit_id);
6133 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6134 commit_id = parent_id;
6138 got_commit_graph_close(graph);
6142 static const struct got_error *
6143 cmd_rebase(int argc, char *argv[])
6145 const struct got_error *error = NULL;
6146 struct got_worktree *worktree = NULL;
6147 struct got_repository *repo = NULL;
6148 struct got_fileindex *fileindex = NULL;
6150 struct got_reference *branch = NULL;
6151 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6152 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6153 struct got_object_id *resume_commit_id = NULL;
6154 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6155 struct got_commit_object *commit = NULL;
6156 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6157 int histedit_in_progress = 0;
6158 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6159 struct got_object_id_queue commits;
6160 struct got_pathlist_head merged_paths;
6161 const struct got_object_id_queue *parent_ids;
6162 struct got_object_qid *qid, *pid;
6164 SIMPLEQ_INIT(&commits);
6165 TAILQ_INIT(&merged_paths);
6167 while ((ch = getopt(argc, argv, "ac")) != -1) {
6173 continue_rebase = 1;
6185 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6186 "unveil", NULL) == -1)
6189 if (abort_rebase && continue_rebase)
6191 else if (abort_rebase || continue_rebase) {
6194 } else if (argc != 1)
6197 cwd = getcwd(NULL, 0);
6199 error = got_error_from_errno("getcwd");
6202 error = got_worktree_open(&worktree, cwd);
6206 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6211 error = apply_unveil(got_repo_get_path(repo), 0,
6212 got_worktree_get_root_path(worktree));
6216 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6220 if (histedit_in_progress) {
6221 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6225 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6231 if (!rebase_in_progress) {
6232 error = got_error(GOT_ERR_NOT_REBASING);
6235 error = got_worktree_rebase_continue(&resume_commit_id,
6236 &new_base_branch, &tmp_branch, &branch, &fileindex,
6240 printf("Switching work tree to %s\n",
6241 got_ref_get_symref_target(new_base_branch));
6242 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6243 new_base_branch, update_progress, &did_something);
6246 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6247 goto done; /* nothing else to do */
6250 if (continue_rebase) {
6251 if (!rebase_in_progress) {
6252 error = got_error(GOT_ERR_NOT_REBASING);
6255 error = got_worktree_rebase_continue(&resume_commit_id,
6256 &new_base_branch, &tmp_branch, &branch, &fileindex,
6261 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6262 resume_commit_id, repo);
6266 yca_id = got_object_id_dup(resume_commit_id);
6267 if (yca_id == NULL) {
6268 error = got_error_from_errno("got_object_id_dup");
6272 error = got_ref_open(&branch, repo, argv[0], 0);
6277 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6281 if (!continue_rebase) {
6282 struct got_object_id *base_commit_id;
6284 base_commit_id = got_worktree_get_base_commit_id(worktree);
6285 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6286 base_commit_id, branch_head_commit_id, repo,
6287 check_cancelled, NULL);
6290 if (yca_id == NULL) {
6291 error = got_error_msg(GOT_ERR_ANCESTRY,
6292 "specified branch shares no common ancestry "
6293 "with work tree's branch");
6297 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6299 if (error->code != GOT_ERR_ANCESTRY)
6303 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6304 "specified branch resolves to a commit which "
6305 "is already contained in work tree's branch");
6308 error = got_worktree_rebase_prepare(&new_base_branch,
6309 &tmp_branch, &fileindex, worktree, branch, repo);
6314 commit_id = branch_head_commit_id;
6315 error = got_object_open_as_commit(&commit, repo, commit_id);
6319 parent_ids = got_object_commit_get_parent_ids(commit);
6320 pid = SIMPLEQ_FIRST(parent_ids);
6322 if (!continue_rebase) {
6324 error = got_worktree_rebase_abort(worktree, fileindex,
6325 repo, new_base_branch, update_progress,
6329 printf("Rebase of %s aborted\n",
6330 got_ref_get_name(branch));
6332 error = got_error(GOT_ERR_EMPTY_REBASE);
6335 error = collect_commits(&commits, commit_id, pid->id,
6336 yca_id, got_worktree_get_path_prefix(worktree),
6337 GOT_ERR_REBASE_PATH, repo);
6338 got_object_commit_close(commit);
6343 if (SIMPLEQ_EMPTY(&commits)) {
6344 if (continue_rebase) {
6345 error = rebase_complete(worktree, fileindex,
6346 branch, new_base_branch, tmp_branch, repo);
6349 /* Fast-forward the reference of the branch. */
6350 struct got_object_id *new_head_commit_id;
6352 error = got_ref_resolve(&new_head_commit_id, repo,
6356 error = got_object_id_str(&id_str, new_head_commit_id);
6357 printf("Forwarding %s to commit %s\n",
6358 got_ref_get_name(branch), id_str);
6360 error = got_ref_change_ref(branch,
6361 new_head_commit_id);
6368 SIMPLEQ_FOREACH(qid, &commits, entry) {
6369 commit_id = qid->id;
6370 parent_id = pid ? pid->id : yca_id;
6373 error = got_worktree_rebase_merge_files(&merged_paths,
6374 worktree, fileindex, parent_id, commit_id, repo,
6375 rebase_progress, &rebase_status, check_cancelled, NULL);
6379 if (rebase_status == GOT_STATUS_CONFLICT) {
6380 error = show_rebase_merge_conflict(qid->id, repo);
6383 got_worktree_rebase_pathlist_free(&merged_paths);
6387 error = rebase_commit(&merged_paths, worktree, fileindex,
6388 tmp_branch, commit_id, repo);
6389 got_worktree_rebase_pathlist_free(&merged_paths);
6394 if (rebase_status == GOT_STATUS_CONFLICT) {
6395 error = got_worktree_rebase_postpone(worktree, fileindex);
6398 error = got_error_msg(GOT_ERR_CONFLICTS,
6399 "conflicts must be resolved before rebasing can continue");
6401 error = rebase_complete(worktree, fileindex, branch,
6402 new_base_branch, tmp_branch, repo);
6404 got_object_id_queue_free(&commits);
6405 free(branch_head_commit_id);
6406 free(resume_commit_id);
6409 got_object_commit_close(commit);
6411 got_ref_close(branch);
6412 if (new_base_branch)
6413 got_ref_close(new_base_branch);
6415 got_ref_close(tmp_branch);
6417 got_worktree_close(worktree);
6419 got_repo_close(repo);
6424 usage_histedit(void)
6426 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6431 #define GOT_HISTEDIT_PICK 'p'
6432 #define GOT_HISTEDIT_EDIT 'e'
6433 #define GOT_HISTEDIT_FOLD 'f'
6434 #define GOT_HISTEDIT_DROP 'd'
6435 #define GOT_HISTEDIT_MESG 'm'
6437 static struct got_histedit_cmd {
6441 } got_histedit_cmds[] = {
6442 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6443 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6444 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6446 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6447 { GOT_HISTEDIT_MESG, "mesg",
6448 "single-line log message for commit above (open editor if empty)" },
6451 struct got_histedit_list_entry {
6452 TAILQ_ENTRY(got_histedit_list_entry) entry;
6453 struct got_object_id *commit_id;
6454 const struct got_histedit_cmd *cmd;
6457 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6459 static const struct got_error *
6460 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6461 FILE *f, struct got_repository *repo)
6463 const struct got_error *err = NULL;
6464 char *logmsg = NULL, *id_str = NULL;
6465 struct got_commit_object *commit = NULL;
6468 err = got_object_open_as_commit(&commit, repo, commit_id);
6472 err = get_short_logmsg(&logmsg, 34, commit);
6476 err = got_object_id_str(&id_str, commit_id);
6480 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6482 err = got_ferror(f, GOT_ERR_IO);
6485 got_object_commit_close(commit);
6491 static const struct got_error *
6492 histedit_write_commit_list(struct got_object_id_queue *commits,
6493 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6495 const struct got_error *err = NULL;
6496 struct got_object_qid *qid;
6498 if (SIMPLEQ_EMPTY(commits))
6499 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6501 SIMPLEQ_FOREACH(qid, commits, entry) {
6502 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6506 if (edit_logmsg_only) {
6507 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6509 err = got_ferror(f, GOT_ERR_IO);
6518 static const struct got_error *
6519 write_cmd_list(FILE *f, const char *branch_name,
6520 struct got_object_id_queue *commits)
6522 const struct got_error *err = NULL;
6525 struct got_object_qid *qid;
6527 qid = SIMPLEQ_FIRST(commits);
6528 err = got_object_id_str(&id_str, qid->id);
6533 "# Editing the history of branch '%s' starting at\n"
6535 "# Commits will be processed in order from top to "
6536 "bottom of this file.\n", branch_name, id_str);
6538 err = got_ferror(f, GOT_ERR_IO);
6542 n = fprintf(f, "# Available histedit commands:\n");
6544 err = got_ferror(f, GOT_ERR_IO);
6548 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6549 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6550 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6553 err = got_ferror(f, GOT_ERR_IO);
6562 static const struct got_error *
6563 histedit_syntax_error(int lineno)
6565 static char msg[42];
6568 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6570 if (ret == -1 || ret >= sizeof(msg))
6571 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6573 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6576 static const struct got_error *
6577 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6578 char *logmsg, struct got_repository *repo)
6580 const struct got_error *err;
6581 struct got_commit_object *folded_commit = NULL;
6582 char *id_str, *folded_logmsg = NULL;
6584 err = got_object_id_str(&id_str, hle->commit_id);
6588 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6592 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6595 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6596 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6597 folded_logmsg) == -1) {
6598 err = got_error_from_errno("asprintf");
6602 got_object_commit_close(folded_commit);
6604 free(folded_logmsg);
6608 static struct got_histedit_list_entry *
6609 get_folded_commits(struct got_histedit_list_entry *hle)
6611 struct got_histedit_list_entry *prev, *folded = NULL;
6613 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6614 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6615 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6616 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6618 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6624 static const struct got_error *
6625 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6626 struct got_repository *repo)
6628 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6629 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6630 const struct got_error *err = NULL;
6631 struct got_commit_object *commit = NULL;
6633 struct got_histedit_list_entry *folded = NULL;
6635 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6639 folded = get_folded_commits(hle);
6641 while (folded != hle) {
6642 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6643 folded = TAILQ_NEXT(folded, entry);
6646 err = append_folded_commit_msg(&new_msg, folded,
6652 folded = TAILQ_NEXT(folded, entry);
6656 err = got_object_id_str(&id_str, hle->commit_id);
6659 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6662 if (asprintf(&new_msg,
6663 "%s\n# original log message of commit %s: %s",
6664 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6665 err = got_error_from_errno("asprintf");
6671 err = got_object_id_str(&id_str, hle->commit_id);
6675 err = got_opentemp_named_fd(&logmsg_path, &fd,
6676 GOT_TMPDIR_STR "/got-logmsg");
6680 dprintf(fd, logmsg);
6683 err = get_editor(&editor);
6687 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6689 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6691 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6694 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6695 err = got_error_from_errno2("unlink", logmsg_path);
6701 got_object_commit_close(commit);
6705 static const struct got_error *
6706 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6707 FILE *f, struct got_repository *repo)
6709 const struct got_error *err = NULL;
6710 char *line = NULL, *p, *end;
6714 const struct got_histedit_cmd *cmd;
6715 struct got_object_id *commit_id = NULL;
6716 struct got_histedit_list_entry *hle = NULL;
6719 len = getline(&line, &size, f);
6721 const struct got_error *getline_err;
6724 getline_err = got_error_from_errno("getline");
6725 err = got_ferror(f, getline_err->code);
6730 while (isspace((unsigned char)p[0]))
6732 if (p[0] == '#' || p[0] == '\0') {
6738 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6739 cmd = &got_histedit_cmds[i];
6740 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6741 isspace((unsigned char)p[strlen(cmd->name)])) {
6742 p += strlen(cmd->name);
6745 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6750 if (i == nitems(got_histedit_cmds)) {
6751 err = histedit_syntax_error(lineno);
6754 while (isspace((unsigned char)p[0]))
6756 if (cmd->code == GOT_HISTEDIT_MESG) {
6757 if (hle == NULL || hle->logmsg != NULL) {
6758 err = got_error(GOT_ERR_HISTEDIT_CMD);
6762 err = histedit_edit_logmsg(hle, repo);
6766 hle->logmsg = strdup(p);
6767 if (hle->logmsg == NULL) {
6768 err = got_error_from_errno("strdup");
6777 while (end[0] && !isspace((unsigned char)end[0]))
6781 err = got_object_resolve_id_str(&commit_id, repo, p);
6783 /* override error code */
6784 err = histedit_syntax_error(lineno);
6788 hle = malloc(sizeof(*hle));
6790 err = got_error_from_errno("malloc");
6794 hle->commit_id = commit_id;
6799 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6807 static const struct got_error *
6808 histedit_check_script(struct got_histedit_list *histedit_cmds,
6809 struct got_object_id_queue *commits, struct got_repository *repo)
6811 const struct got_error *err = NULL;
6812 struct got_object_qid *qid;
6813 struct got_histedit_list_entry *hle;
6814 static char msg[92];
6817 if (TAILQ_EMPTY(histedit_cmds))
6818 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6819 "histedit script contains no commands");
6820 if (SIMPLEQ_EMPTY(commits))
6821 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6823 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6824 struct got_histedit_list_entry *hle2;
6825 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6828 if (got_object_id_cmp(hle->commit_id,
6829 hle2->commit_id) != 0)
6831 err = got_object_id_str(&id_str, hle->commit_id);
6834 snprintf(msg, sizeof(msg), "commit %s is listed "
6835 "more than once in histedit script", id_str);
6837 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6841 SIMPLEQ_FOREACH(qid, commits, entry) {
6842 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6843 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6847 err = got_object_id_str(&id_str, qid->id);
6850 snprintf(msg, sizeof(msg),
6851 "commit %s missing from histedit script", id_str);
6853 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6857 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6858 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6859 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6860 "last commit in histedit script cannot be folded");
6865 static const struct got_error *
6866 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6867 const char *path, struct got_object_id_queue *commits,
6868 struct got_repository *repo)
6870 const struct got_error *err = NULL;
6874 err = get_editor(&editor);
6878 if (spawn_editor(editor, path) == -1) {
6879 err = got_error_from_errno("failed spawning editor");
6883 f = fopen(path, "r");
6885 err = got_error_from_errno("fopen");
6888 err = histedit_parse_list(histedit_cmds, f, repo);
6892 err = histedit_check_script(histedit_cmds, commits, repo);
6894 if (f && fclose(f) != 0 && err == NULL)
6895 err = got_error_from_errno("fclose");
6900 static const struct got_error *
6901 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6902 struct got_object_id_queue *, const char *, const char *,
6903 struct got_repository *);
6905 static const struct got_error *
6906 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6907 struct got_object_id_queue *commits, const char *branch_name,
6908 int edit_logmsg_only, struct got_repository *repo)
6910 const struct got_error *err;
6914 err = got_opentemp_named(&path, &f, "got-histedit");
6918 err = write_cmd_list(f, branch_name, commits);
6922 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6926 if (edit_logmsg_only) {
6928 err = histedit_parse_list(histedit_cmds, f, repo);
6930 if (fclose(f) != 0) {
6931 err = got_error_from_errno("fclose");
6935 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6937 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6938 err->code != GOT_ERR_HISTEDIT_CMD)
6940 err = histedit_edit_list_retry(histedit_cmds, err,
6941 commits, path, branch_name, repo);
6945 if (f && fclose(f) != 0 && err == NULL)
6946 err = got_error_from_errno("fclose");
6947 if (path && unlink(path) != 0 && err == NULL)
6948 err = got_error_from_errno2("unlink", path);
6953 static const struct got_error *
6954 histedit_save_list(struct got_histedit_list *histedit_cmds,
6955 struct got_worktree *worktree, struct got_repository *repo)
6957 const struct got_error *err = NULL;
6960 struct got_histedit_list_entry *hle;
6961 struct got_commit_object *commit = NULL;
6963 err = got_worktree_get_histedit_script_path(&path, worktree);
6967 f = fopen(path, "w");
6969 err = got_error_from_errno2("fopen", path);
6972 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6973 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6979 int n = fprintf(f, "%c %s\n",
6980 GOT_HISTEDIT_MESG, hle->logmsg);
6982 err = got_ferror(f, GOT_ERR_IO);
6988 if (f && fclose(f) != 0 && err == NULL)
6989 err = got_error_from_errno("fclose");
6992 got_object_commit_close(commit);
6997 histedit_free_list(struct got_histedit_list *histedit_cmds)
6999 struct got_histedit_list_entry *hle;
7001 while ((hle = TAILQ_FIRST(histedit_cmds))) {
7002 TAILQ_REMOVE(histedit_cmds, hle, entry);
7007 static const struct got_error *
7008 histedit_load_list(struct got_histedit_list *histedit_cmds,
7009 const char *path, struct got_repository *repo)
7011 const struct got_error *err = NULL;
7014 f = fopen(path, "r");
7016 err = got_error_from_errno2("fopen", path);
7020 err = histedit_parse_list(histedit_cmds, f, repo);
7022 if (f && fclose(f) != 0 && err == NULL)
7023 err = got_error_from_errno("fclose");
7027 static const struct got_error *
7028 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7029 const struct got_error *edit_err, struct got_object_id_queue *commits,
7030 const char *path, const char *branch_name, struct got_repository *repo)
7032 const struct got_error *err = NULL, *prev_err = edit_err;
7035 while (resp != 'c' && resp != 'r' && resp != 'a') {
7036 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7037 "or (a)bort: ", getprogname(), prev_err->msg);
7042 histedit_free_list(histedit_cmds);
7043 err = histedit_run_editor(histedit_cmds, path, commits,
7046 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7047 err->code != GOT_ERR_HISTEDIT_CMD)
7054 } else if (resp == 'r') {
7055 histedit_free_list(histedit_cmds);
7056 err = histedit_edit_script(histedit_cmds,
7057 commits, branch_name, 0, repo);
7059 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7060 err->code != GOT_ERR_HISTEDIT_CMD)
7067 } else if (resp == 'a') {
7068 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7071 printf("invalid response '%c'\n", resp);
7077 static const struct got_error *
7078 histedit_complete(struct got_worktree *worktree,
7079 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7080 struct got_reference *branch, struct got_repository *repo)
7082 printf("Switching work tree to %s\n",
7083 got_ref_get_symref_target(branch));
7084 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7088 static const struct got_error *
7089 show_histedit_progress(struct got_commit_object *commit,
7090 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7092 const struct got_error *err;
7093 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7095 err = got_object_id_str(&old_id_str, hle->commit_id);
7100 err = got_object_id_str(&new_id_str, new_id);
7105 old_id_str[12] = '\0';
7107 new_id_str[12] = '\0';
7110 logmsg = strdup(hle->logmsg);
7111 if (logmsg == NULL) {
7112 err = got_error_from_errno("strdup");
7115 trim_logmsg(logmsg, 42);
7117 err = get_short_logmsg(&logmsg, 42, commit);
7122 switch (hle->cmd->code) {
7123 case GOT_HISTEDIT_PICK:
7124 case GOT_HISTEDIT_EDIT:
7125 printf("%s -> %s: %s\n", old_id_str,
7126 new_id_str ? new_id_str : "no-op change", logmsg);
7128 case GOT_HISTEDIT_DROP:
7129 case GOT_HISTEDIT_FOLD:
7130 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7142 static const struct got_error *
7143 histedit_commit(struct got_pathlist_head *merged_paths,
7144 struct got_worktree *worktree, struct got_fileindex *fileindex,
7145 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7146 struct got_repository *repo)
7148 const struct got_error *err;
7149 struct got_commit_object *commit;
7150 struct got_object_id *new_commit_id;
7152 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7153 && hle->logmsg == NULL) {
7154 err = histedit_edit_logmsg(hle, repo);
7159 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7163 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7164 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7167 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7169 err = show_histedit_progress(commit, hle, NULL);
7171 err = show_histedit_progress(commit, hle, new_commit_id);
7172 free(new_commit_id);
7175 got_object_commit_close(commit);
7179 static const struct got_error *
7180 histedit_skip_commit(struct got_histedit_list_entry *hle,
7181 struct got_worktree *worktree, struct got_repository *repo)
7183 const struct got_error *error;
7184 struct got_commit_object *commit;
7186 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7191 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7195 error = show_histedit_progress(commit, hle, NULL);
7196 got_object_commit_close(commit);
7200 static const struct got_error *
7201 check_local_changes(void *arg, unsigned char status,
7202 unsigned char staged_status, const char *path,
7203 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7204 struct got_object_id *commit_id, int dirfd, const char *de_name)
7206 int *have_local_changes = arg;
7209 case GOT_STATUS_ADD:
7210 case GOT_STATUS_DELETE:
7211 case GOT_STATUS_MODIFY:
7212 case GOT_STATUS_CONFLICT:
7213 *have_local_changes = 1;
7214 return got_error(GOT_ERR_CANCELLED);
7219 switch (staged_status) {
7220 case GOT_STATUS_ADD:
7221 case GOT_STATUS_DELETE:
7222 case GOT_STATUS_MODIFY:
7223 *have_local_changes = 1;
7224 return got_error(GOT_ERR_CANCELLED);
7232 static const struct got_error *
7233 cmd_histedit(int argc, char *argv[])
7235 const struct got_error *error = NULL;
7236 struct got_worktree *worktree = NULL;
7237 struct got_fileindex *fileindex = NULL;
7238 struct got_repository *repo = NULL;
7240 struct got_reference *branch = NULL;
7241 struct got_reference *tmp_branch = NULL;
7242 struct got_object_id *resume_commit_id = NULL;
7243 struct got_object_id *base_commit_id = NULL;
7244 struct got_object_id *head_commit_id = NULL;
7245 struct got_commit_object *commit = NULL;
7246 int ch, rebase_in_progress = 0, did_something;
7247 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7248 int edit_logmsg_only = 0;
7249 const char *edit_script_path = NULL;
7250 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7251 struct got_object_id_queue commits;
7252 struct got_pathlist_head merged_paths;
7253 const struct got_object_id_queue *parent_ids;
7254 struct got_object_qid *pid;
7255 struct got_histedit_list histedit_cmds;
7256 struct got_histedit_list_entry *hle;
7258 SIMPLEQ_INIT(&commits);
7259 TAILQ_INIT(&histedit_cmds);
7260 TAILQ_INIT(&merged_paths);
7262 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7271 edit_script_path = optarg;
7274 edit_logmsg_only = 1;
7286 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7287 "unveil", NULL) == -1)
7290 if (abort_edit && continue_edit)
7291 errx(1, "histedit's -a and -c options are mutually exclusive");
7292 if (edit_script_path && edit_logmsg_only)
7293 errx(1, "histedit's -F and -m options are mutually exclusive");
7294 if (abort_edit && edit_logmsg_only)
7295 errx(1, "histedit's -a and -m options are mutually exclusive");
7296 if (continue_edit && edit_logmsg_only)
7297 errx(1, "histedit's -c and -m options are mutually exclusive");
7302 * This command cannot apply unveil(2) in all cases because the
7303 * user may choose to run an editor to edit the histedit script
7304 * and to edit individual commit log messages.
7305 * unveil(2) traverses exec(2); if an editor is used we have to
7306 * apply unveil after edit script and log messages have been written.
7307 * XXX TODO: Make use of unveil(2) where possible.
7310 cwd = getcwd(NULL, 0);
7312 error = got_error_from_errno("getcwd");
7315 error = got_worktree_open(&worktree, cwd);
7319 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7324 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7327 if (rebase_in_progress) {
7328 error = got_error(GOT_ERR_REBASING);
7332 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7336 if (edit_in_progress && edit_logmsg_only) {
7337 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7338 "histedit operation is in progress in this "
7339 "work tree and must be continued or aborted "
7340 "before the -m option can be used");
7344 if (edit_in_progress && abort_edit) {
7345 error = got_worktree_histedit_continue(&resume_commit_id,
7346 &tmp_branch, &branch, &base_commit_id, &fileindex,
7350 printf("Switching work tree to %s\n",
7351 got_ref_get_symref_target(branch));
7352 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7353 branch, base_commit_id, update_progress, &did_something);
7356 printf("Histedit of %s aborted\n",
7357 got_ref_get_symref_target(branch));
7358 goto done; /* nothing else to do */
7359 } else if (abort_edit) {
7360 error = got_error(GOT_ERR_NOT_HISTEDIT);
7364 if (continue_edit) {
7367 if (!edit_in_progress) {
7368 error = got_error(GOT_ERR_NOT_HISTEDIT);
7372 error = got_worktree_get_histedit_script_path(&path, worktree);
7376 error = histedit_load_list(&histedit_cmds, path, repo);
7381 error = got_worktree_histedit_continue(&resume_commit_id,
7382 &tmp_branch, &branch, &base_commit_id, &fileindex,
7387 error = got_ref_resolve(&head_commit_id, repo, branch);
7391 error = got_object_open_as_commit(&commit, repo,
7395 parent_ids = got_object_commit_get_parent_ids(commit);
7396 pid = SIMPLEQ_FIRST(parent_ids);
7398 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7401 error = collect_commits(&commits, head_commit_id, pid->id,
7402 base_commit_id, got_worktree_get_path_prefix(worktree),
7403 GOT_ERR_HISTEDIT_PATH, repo);
7404 got_object_commit_close(commit);
7409 if (edit_in_progress) {
7410 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7414 error = got_ref_open(&branch, repo,
7415 got_worktree_get_head_ref_name(worktree), 0);
7419 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7420 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7421 "will not edit commit history of a branch outside "
7422 "the \"refs/heads/\" reference namespace");
7426 error = got_ref_resolve(&head_commit_id, repo, branch);
7427 got_ref_close(branch);
7432 error = got_object_open_as_commit(&commit, repo,
7436 parent_ids = got_object_commit_get_parent_ids(commit);
7437 pid = SIMPLEQ_FIRST(parent_ids);
7439 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7442 error = collect_commits(&commits, head_commit_id, pid->id,
7443 got_worktree_get_base_commit_id(worktree),
7444 got_worktree_get_path_prefix(worktree),
7445 GOT_ERR_HISTEDIT_PATH, repo);
7446 got_object_commit_close(commit);
7451 if (SIMPLEQ_EMPTY(&commits)) {
7452 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7456 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7457 &base_commit_id, &fileindex, worktree, repo);
7461 if (edit_script_path) {
7462 error = histedit_load_list(&histedit_cmds,
7463 edit_script_path, repo);
7465 got_worktree_histedit_abort(worktree, fileindex,
7466 repo, branch, base_commit_id,
7467 update_progress, &did_something);
7471 const char *branch_name;
7472 branch_name = got_ref_get_symref_target(branch);
7473 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7475 error = histedit_edit_script(&histedit_cmds, &commits,
7476 branch_name, edit_logmsg_only, repo);
7478 got_worktree_histedit_abort(worktree, fileindex,
7479 repo, branch, base_commit_id,
7480 update_progress, &did_something);
7486 error = histedit_save_list(&histedit_cmds, worktree,
7489 got_worktree_histedit_abort(worktree, fileindex,
7490 repo, branch, base_commit_id,
7491 update_progress, &did_something);
7497 error = histedit_check_script(&histedit_cmds, &commits, repo);
7501 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7502 if (resume_commit_id) {
7503 if (got_object_id_cmp(hle->commit_id,
7504 resume_commit_id) != 0)
7507 resume_commit_id = NULL;
7508 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7509 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7510 error = histedit_skip_commit(hle, worktree,
7515 struct got_pathlist_head paths;
7516 int have_changes = 0;
7519 error = got_pathlist_append(&paths, "", NULL);
7522 error = got_worktree_status(worktree, &paths,
7523 repo, check_local_changes, &have_changes,
7524 check_cancelled, NULL);
7525 got_pathlist_free(&paths);
7527 if (error->code != GOT_ERR_CANCELLED)
7529 if (sigint_received || sigpipe_received)
7533 error = histedit_commit(NULL, worktree,
7534 fileindex, tmp_branch, hle, repo);
7538 error = got_object_open_as_commit(
7539 &commit, repo, hle->commit_id);
7542 error = show_histedit_progress(commit,
7544 got_object_commit_close(commit);
7553 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7554 error = histedit_skip_commit(hle, worktree, repo);
7560 error = got_object_open_as_commit(&commit, repo,
7564 parent_ids = got_object_commit_get_parent_ids(commit);
7565 pid = SIMPLEQ_FIRST(parent_ids);
7567 error = got_worktree_histedit_merge_files(&merged_paths,
7568 worktree, fileindex, pid->id, hle->commit_id, repo,
7569 rebase_progress, &rebase_status, check_cancelled, NULL);
7572 got_object_commit_close(commit);
7575 if (rebase_status == GOT_STATUS_CONFLICT) {
7576 error = show_rebase_merge_conflict(hle->commit_id,
7580 got_worktree_rebase_pathlist_free(&merged_paths);
7584 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7586 error = got_object_id_str(&id_str, hle->commit_id);
7589 printf("Stopping histedit for amending commit %s\n",
7592 got_worktree_rebase_pathlist_free(&merged_paths);
7593 error = got_worktree_histedit_postpone(worktree,
7598 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7599 error = histedit_skip_commit(hle, worktree, repo);
7605 error = histedit_commit(&merged_paths, worktree, fileindex,
7606 tmp_branch, hle, repo);
7607 got_worktree_rebase_pathlist_free(&merged_paths);
7612 if (rebase_status == GOT_STATUS_CONFLICT) {
7613 error = got_worktree_histedit_postpone(worktree, fileindex);
7616 error = got_error_msg(GOT_ERR_CONFLICTS,
7617 "conflicts must be resolved before histedit can continue");
7619 error = histedit_complete(worktree, fileindex, tmp_branch,
7622 got_object_id_queue_free(&commits);
7623 histedit_free_list(&histedit_cmds);
7624 free(head_commit_id);
7625 free(base_commit_id);
7626 free(resume_commit_id);
7628 got_object_commit_close(commit);
7630 got_ref_close(branch);
7632 got_ref_close(tmp_branch);
7634 got_worktree_close(worktree);
7636 got_repo_close(repo);
7641 usage_integrate(void)
7643 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7647 static const struct got_error *
7648 cmd_integrate(int argc, char *argv[])
7650 const struct got_error *error = NULL;
7651 struct got_repository *repo = NULL;
7652 struct got_worktree *worktree = NULL;
7653 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7654 const char *branch_arg = NULL;
7655 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7656 struct got_fileindex *fileindex = NULL;
7657 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7658 int ch, did_something = 0;
7660 while ((ch = getopt(argc, argv, "")) != -1) {
7673 branch_arg = argv[0];
7675 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7676 "unveil", NULL) == -1)
7679 cwd = getcwd(NULL, 0);
7681 error = got_error_from_errno("getcwd");
7685 error = got_worktree_open(&worktree, cwd);
7689 error = check_rebase_or_histedit_in_progress(worktree);
7693 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7698 error = apply_unveil(got_repo_get_path(repo), 0,
7699 got_worktree_get_root_path(worktree));
7703 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7704 error = got_error_from_errno("asprintf");
7708 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7709 &base_branch_ref, worktree, refname, repo);
7713 refname = strdup(got_ref_get_name(branch_ref));
7714 if (refname == NULL) {
7715 error = got_error_from_errno("strdup");
7716 got_worktree_integrate_abort(worktree, fileindex, repo,
7717 branch_ref, base_branch_ref);
7720 base_refname = strdup(got_ref_get_name(base_branch_ref));
7721 if (base_refname == NULL) {
7722 error = got_error_from_errno("strdup");
7723 got_worktree_integrate_abort(worktree, fileindex, repo,
7724 branch_ref, base_branch_ref);
7728 error = got_ref_resolve(&commit_id, repo, branch_ref);
7732 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7736 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7737 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7738 "specified branch has already been integrated");
7739 got_worktree_integrate_abort(worktree, fileindex, repo,
7740 branch_ref, base_branch_ref);
7744 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7746 if (error->code == GOT_ERR_ANCESTRY)
7747 error = got_error(GOT_ERR_REBASE_REQUIRED);
7748 got_worktree_integrate_abort(worktree, fileindex, repo,
7749 branch_ref, base_branch_ref);
7753 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7754 branch_ref, base_branch_ref, update_progress, &did_something,
7755 check_cancelled, NULL);
7759 printf("Integrated %s into %s\n", refname, base_refname);
7762 got_repo_close(repo);
7764 got_worktree_close(worktree);
7766 free(base_commit_id);
7776 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7777 "[file-path ...]\n",
7782 static const struct got_error *
7783 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7784 const char *path, struct got_object_id *blob_id,
7785 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7786 int dirfd, const char *de_name)
7788 const struct got_error *err = NULL;
7789 char *id_str = NULL;
7791 if (staged_status != GOT_STATUS_ADD &&
7792 staged_status != GOT_STATUS_MODIFY &&
7793 staged_status != GOT_STATUS_DELETE)
7796 if (staged_status == GOT_STATUS_ADD ||
7797 staged_status == GOT_STATUS_MODIFY)
7798 err = got_object_id_str(&id_str, staged_blob_id);
7800 err = got_object_id_str(&id_str, blob_id);
7804 printf("%s %c %s\n", id_str, staged_status, path);
7809 static const struct got_error *
7810 cmd_stage(int argc, char *argv[])
7812 const struct got_error *error = NULL;
7813 struct got_repository *repo = NULL;
7814 struct got_worktree *worktree = NULL;
7816 struct got_pathlist_head paths;
7817 struct got_pathlist_entry *pe;
7818 int ch, list_stage = 0, pflag = 0;
7819 FILE *patch_script_file = NULL;
7820 const char *patch_script_path = NULL;
7821 struct choose_patch_arg cpa;
7825 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7834 patch_script_path = optarg;
7846 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7847 "unveil", NULL) == -1)
7850 if (list_stage && (pflag || patch_script_path))
7851 errx(1, "-l option cannot be used with other options");
7852 if (patch_script_path && !pflag)
7853 errx(1, "-F option can only be used together with -p option");
7855 cwd = getcwd(NULL, 0);
7857 error = got_error_from_errno("getcwd");
7861 error = got_worktree_open(&worktree, cwd);
7865 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7870 if (patch_script_path) {
7871 patch_script_file = fopen(patch_script_path, "r");
7872 if (patch_script_file == NULL) {
7873 error = got_error_from_errno2("fopen",
7878 error = apply_unveil(got_repo_get_path(repo), 0,
7879 got_worktree_get_root_path(worktree));
7883 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7888 error = got_worktree_status(worktree, &paths, repo,
7889 print_stage, NULL, check_cancelled, NULL);
7891 cpa.patch_script_file = patch_script_file;
7892 cpa.action = "stage";
7893 error = got_worktree_stage(worktree, &paths,
7894 pflag ? NULL : print_status, NULL,
7895 pflag ? choose_patch : NULL, &cpa, repo);
7898 if (patch_script_file && fclose(patch_script_file) == EOF &&
7900 error = got_error_from_errno2("fclose", patch_script_path);
7902 got_repo_close(repo);
7904 got_worktree_close(worktree);
7905 TAILQ_FOREACH(pe, &paths, entry)
7906 free((char *)pe->path);
7907 got_pathlist_free(&paths);
7915 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7916 "[file-path ...]\n",
7922 static const struct got_error *
7923 cmd_unstage(int argc, char *argv[])
7925 const struct got_error *error = NULL;
7926 struct got_repository *repo = NULL;
7927 struct got_worktree *worktree = NULL;
7929 struct got_pathlist_head paths;
7930 struct got_pathlist_entry *pe;
7931 int ch, did_something = 0, pflag = 0;
7932 FILE *patch_script_file = NULL;
7933 const char *patch_script_path = NULL;
7934 struct choose_patch_arg cpa;
7938 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7944 patch_script_path = optarg;
7956 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7957 "unveil", NULL) == -1)
7960 if (patch_script_path && !pflag)
7961 errx(1, "-F option can only be used together with -p option");
7963 cwd = getcwd(NULL, 0);
7965 error = got_error_from_errno("getcwd");
7969 error = got_worktree_open(&worktree, cwd);
7973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7978 if (patch_script_path) {
7979 patch_script_file = fopen(patch_script_path, "r");
7980 if (patch_script_file == NULL) {
7981 error = got_error_from_errno2("fopen",
7987 error = apply_unveil(got_repo_get_path(repo), 0,
7988 got_worktree_get_root_path(worktree));
7992 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7996 cpa.patch_script_file = patch_script_file;
7997 cpa.action = "unstage";
7998 error = got_worktree_unstage(worktree, &paths, update_progress,
7999 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8001 if (patch_script_file && fclose(patch_script_file) == EOF &&
8003 error = got_error_from_errno2("fclose", patch_script_path);
8005 got_repo_close(repo);
8007 got_worktree_close(worktree);
8008 TAILQ_FOREACH(pe, &paths, entry)
8009 free((char *)pe->path);
8010 got_pathlist_free(&paths);
8018 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8019 "arg1 [arg2 ...]\n", getprogname());
8023 static const struct got_error *
8024 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8026 const struct got_error *err;
8027 struct got_blob_object *blob;
8029 err = got_object_open_as_blob(&blob, repo, id, 8192);
8033 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8034 got_object_blob_close(blob);
8038 static const struct got_error *
8039 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8041 const struct got_error *err;
8042 struct got_tree_object *tree;
8045 err = got_object_open_as_tree(&tree, repo, id);
8049 nentries = got_object_tree_get_nentries(tree);
8050 for (i = 0; i < nentries; i++) {
8051 struct got_tree_entry *te;
8053 if (sigint_received || sigpipe_received)
8055 te = got_object_tree_get_entry(tree, i);
8056 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8059 fprintf(outfile, "%s %.7o %s\n", id_str,
8060 got_tree_entry_get_mode(te),
8061 got_tree_entry_get_name(te));
8065 got_object_tree_close(tree);
8069 static const struct got_error *
8070 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8072 const struct got_error *err;
8073 struct got_commit_object *commit;
8074 const struct got_object_id_queue *parent_ids;
8075 struct got_object_qid *pid;
8076 char *id_str = NULL;
8077 const char *logmsg = NULL;
8079 err = got_object_open_as_commit(&commit, repo, id);
8083 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8087 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8088 parent_ids = got_object_commit_get_parent_ids(commit);
8089 fprintf(outfile, "numparents %d\n",
8090 got_object_commit_get_nparents(commit));
8091 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8093 err = got_object_id_str(&pid_str, pid->id);
8096 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8099 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8100 got_object_commit_get_author(commit),
8101 got_object_commit_get_author_time(commit));
8103 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8104 got_object_commit_get_author(commit),
8105 got_object_commit_get_committer_time(commit));
8107 logmsg = got_object_commit_get_logmsg_raw(commit);
8108 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8109 fprintf(outfile, "%s", logmsg);
8112 got_object_commit_close(commit);
8116 static const struct got_error *
8117 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8119 const struct got_error *err;
8120 struct got_tag_object *tag;
8121 char *id_str = NULL;
8122 const char *tagmsg = NULL;
8124 err = got_object_open_as_tag(&tag, repo, id);
8128 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8132 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8134 switch (got_object_tag_get_object_type(tag)) {
8135 case GOT_OBJ_TYPE_BLOB:
8136 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8137 GOT_OBJ_LABEL_BLOB);
8139 case GOT_OBJ_TYPE_TREE:
8140 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8141 GOT_OBJ_LABEL_TREE);
8143 case GOT_OBJ_TYPE_COMMIT:
8144 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8145 GOT_OBJ_LABEL_COMMIT);
8147 case GOT_OBJ_TYPE_TAG:
8148 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8155 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8156 got_object_tag_get_name(tag));
8158 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8159 got_object_tag_get_tagger(tag),
8160 got_object_tag_get_tagger_time(tag));
8162 tagmsg = got_object_tag_get_message(tag);
8163 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8164 fprintf(outfile, "%s", tagmsg);
8167 got_object_tag_close(tag);
8171 static const struct got_error *
8172 cmd_cat(int argc, char *argv[])
8174 const struct got_error *error;
8175 struct got_repository *repo = NULL;
8176 struct got_worktree *worktree = NULL;
8177 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8178 const char *commit_id_str = NULL;
8179 struct got_object_id *id = NULL, *commit_id = NULL;
8180 int ch, obj_type, i, force_path = 0;
8183 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8188 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8191 commit_id_str = optarg;
8194 repo_path = realpath(optarg, NULL);
8195 if (repo_path == NULL)
8196 return got_error_from_errno2("realpath",
8198 got_path_strip_trailing_slashes(repo_path);
8212 cwd = getcwd(NULL, 0);
8214 error = got_error_from_errno("getcwd");
8217 error = got_worktree_open(&worktree, cwd);
8218 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8221 if (repo_path == NULL) {
8223 got_worktree_get_repo_path(worktree));
8224 if (repo_path == NULL) {
8225 error = got_error_from_errno("strdup");
8231 if (repo_path == NULL) {
8232 repo_path = getcwd(NULL, 0);
8233 if (repo_path == NULL)
8234 return got_error_from_errno("getcwd");
8237 error = got_repo_open(&repo, repo_path, NULL);
8242 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8246 if (commit_id_str == NULL)
8247 commit_id_str = GOT_REF_HEAD;
8248 error = got_repo_match_object_id(&commit_id, NULL,
8249 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8253 for (i = 0; i < argc; i++) {
8255 error = got_object_id_by_path(&id, repo, commit_id,
8260 error = got_repo_match_object_id(&id, &label, argv[i],
8261 GOT_OBJ_TYPE_ANY, 0, repo);
8263 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8264 error->code != GOT_ERR_NOT_REF)
8266 error = got_object_id_by_path(&id, repo,
8267 commit_id, argv[i]);
8273 error = got_object_get_type(&obj_type, repo, id);
8278 case GOT_OBJ_TYPE_BLOB:
8279 error = cat_blob(id, repo, stdout);
8281 case GOT_OBJ_TYPE_TREE:
8282 error = cat_tree(id, repo, stdout);
8284 case GOT_OBJ_TYPE_COMMIT:
8285 error = cat_commit(id, repo, stdout);
8287 case GOT_OBJ_TYPE_TAG:
8288 error = cat_tag(id, repo, stdout);
8291 error = got_error(GOT_ERR_OBJ_TYPE);
8306 got_worktree_close(worktree);
8308 const struct got_error *repo_error;
8309 repo_error = got_repo_close(repo);