2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/queue.h>
20 #include <sys/types.h>
22 #include <sys/param.h>
43 #include "got_version.h"
44 #include "got_error.h"
45 #include "got_object.h"
46 #include "got_reference.h"
47 #include "got_repository.h"
49 #include "got_cancel.h"
50 #include "got_worktree.h"
52 #include "got_commit_graph.h"
53 #include "got_fetch.h"
54 #include "got_blame.h"
55 #include "got_privsep.h"
56 #include "got_opentemp.h"
59 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
62 static volatile sig_atomic_t sigint_received;
63 static volatile sig_atomic_t sigpipe_received;
66 catch_sigint(int signo)
72 catch_sigpipe(int signo)
80 const struct got_error *(*cmd_main)(int, char *[]);
81 void (*cmd_usage)(void);
82 const char *cmd_alias;
85 __dead static void usage(int);
86 __dead static void usage_init(void);
87 __dead static void usage_import(void);
88 __dead static void usage_clone(void);
89 __dead static void usage_fetch(void);
90 __dead static void usage_checkout(void);
91 __dead static void usage_update(void);
92 __dead static void usage_log(void);
93 __dead static void usage_diff(void);
94 __dead static void usage_blame(void);
95 __dead static void usage_tree(void);
96 __dead static void usage_status(void);
97 __dead static void usage_ref(void);
98 __dead static void usage_branch(void);
99 __dead static void usage_tag(void);
100 __dead static void usage_add(void);
101 __dead static void usage_remove(void);
102 __dead static void usage_revert(void);
103 __dead static void usage_commit(void);
104 __dead static void usage_cherrypick(void);
105 __dead static void usage_backout(void);
106 __dead static void usage_rebase(void);
107 __dead static void usage_histedit(void);
108 __dead static void usage_integrate(void);
109 __dead static void usage_stage(void);
110 __dead static void usage_unstage(void);
111 __dead static void usage_cat(void);
113 static const struct got_error* cmd_init(int, char *[]);
114 static const struct got_error* cmd_import(int, char *[]);
115 static const struct got_error* cmd_clone(int, char *[]);
116 static const struct got_error* cmd_fetch(int, char *[]);
117 static const struct got_error* cmd_checkout(int, char *[]);
118 static const struct got_error* cmd_update(int, char *[]);
119 static const struct got_error* cmd_log(int, char *[]);
120 static const struct got_error* cmd_diff(int, char *[]);
121 static const struct got_error* cmd_blame(int, char *[]);
122 static const struct got_error* cmd_tree(int, char *[]);
123 static const struct got_error* cmd_status(int, char *[]);
124 static const struct got_error* cmd_ref(int, char *[]);
125 static const struct got_error* cmd_branch(int, char *[]);
126 static const struct got_error* cmd_tag(int, char *[]);
127 static const struct got_error* cmd_add(int, char *[]);
128 static const struct got_error* cmd_remove(int, char *[]);
129 static const struct got_error* cmd_revert(int, char *[]);
130 static const struct got_error* cmd_commit(int, char *[]);
131 static const struct got_error* cmd_cherrypick(int, char *[]);
132 static const struct got_error* cmd_backout(int, char *[]);
133 static const struct got_error* cmd_rebase(int, char *[]);
134 static const struct got_error* cmd_histedit(int, char *[]);
135 static const struct got_error* cmd_integrate(int, char *[]);
136 static const struct got_error* cmd_stage(int, char *[]);
137 static const struct got_error* cmd_unstage(int, char *[]);
138 static const struct got_error* cmd_cat(int, char *[]);
140 static struct got_cmd got_commands[] = {
141 { "init", cmd_init, usage_init, "in" },
142 { "import", cmd_import, usage_import, "im" },
143 { "clone", cmd_clone, usage_clone, "cl" },
144 { "fetch", cmd_fetch, usage_fetch, "fe" },
145 { "checkout", cmd_checkout, usage_checkout, "co" },
146 { "update", cmd_update, usage_update, "up" },
147 { "log", cmd_log, usage_log, "" },
148 { "diff", cmd_diff, usage_diff, "di" },
149 { "blame", cmd_blame, usage_blame, "bl" },
150 { "tree", cmd_tree, usage_tree, "tr" },
151 { "status", cmd_status, usage_status, "st" },
152 { "ref", cmd_ref, usage_ref, "" },
153 { "branch", cmd_branch, usage_branch, "br" },
154 { "tag", cmd_tag, usage_tag, "" },
155 { "add", cmd_add, usage_add, "" },
156 { "remove", cmd_remove, usage_remove, "rm" },
157 { "revert", cmd_revert, usage_revert, "rv" },
158 { "commit", cmd_commit, usage_commit, "ci" },
159 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 { "backout", cmd_backout, usage_backout, "bo" },
161 { "rebase", cmd_rebase, usage_rebase, "rb" },
162 { "histedit", cmd_histedit, usage_histedit, "he" },
163 { "integrate", cmd_integrate, usage_integrate,"ig" },
164 { "stage", cmd_stage, usage_stage, "sg" },
165 { "unstage", cmd_unstage, usage_unstage, "ug" },
166 { "cat", cmd_cat, usage_cat, "" },
174 fprintf(stderr, "commands:");
175 for (i = 0; i < nitems(got_commands); i++) {
176 struct got_cmd *cmd = &got_commands[i];
177 fprintf(stderr, " %s", cmd->cmd_name);
183 main(int argc, char *argv[])
188 int hflag = 0, Vflag = 0;
189 static struct option longopts[] = {
190 { "version", no_argument, NULL, 'V' },
194 setlocale(LC_CTYPE, "");
196 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
215 got_version_print_str();
222 signal(SIGINT, catch_sigint);
223 signal(SIGPIPE, catch_sigpipe);
225 for (i = 0; i < nitems(got_commands); i++) {
226 const struct got_error *error;
228 cmd = &got_commands[i];
230 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 strcmp(cmd->cmd_alias, argv[0]) != 0)
235 got_commands[i].cmd_usage();
237 error = got_commands[i].cmd_main(argc, argv);
238 if (error && error->code != GOT_ERR_CANCELLED &&
239 error->code != GOT_ERR_PRIVSEP_EXIT &&
240 !(sigpipe_received &&
241 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
243 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
251 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
259 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
266 static const struct got_error *
267 get_editor(char **abspath)
269 const struct got_error *err = NULL;
274 editor = getenv("VISUAL");
276 editor = getenv("EDITOR");
279 err = got_path_find_prog(abspath, editor);
284 if (*abspath == NULL) {
285 *abspath = strdup("/bin/ed");
286 if (*abspath == NULL)
287 return got_error_from_errno("strdup");
293 static const struct got_error *
294 apply_unveil(const char *repo_path, int repo_read_only,
295 const char *worktree_path)
297 const struct got_error *err;
300 if (unveil("gmon.out", "rwc") != 0)
301 return got_error_from_errno2("unveil", "gmon.out");
303 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 return got_error_from_errno2("unveil", repo_path);
306 if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 return got_error_from_errno2("unveil", worktree_path);
309 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
312 err = got_privsep_unveil_exec_helpers();
316 if (unveil(NULL, NULL) != 0)
317 return got_error_from_errno("unveil");
325 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
329 static const struct got_error *
330 cmd_init(int argc, char *argv[])
332 const struct got_error *error = NULL;
333 char *repo_path = NULL;
336 while ((ch = getopt(argc, argv, "")) != -1) {
348 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
354 repo_path = strdup(argv[0]);
355 if (repo_path == NULL)
356 return got_error_from_errno("strdup");
358 got_path_strip_trailing_slashes(repo_path);
360 error = got_path_mkdir(repo_path);
362 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
365 error = apply_unveil(repo_path, 0, NULL);
369 error = got_repo_init(repo_path);
378 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 "[-r repository-path] [-I pattern] path\n", getprogname());
384 spawn_editor(const char *editor, const char *file)
387 sig_t sighup, sigint, sigquit;
390 sighup = signal(SIGHUP, SIG_IGN);
391 sigint = signal(SIGINT, SIG_IGN);
392 sigquit = signal(SIGQUIT, SIG_IGN);
394 switch (pid = fork()) {
398 execl(editor, editor, file, (char *)NULL);
402 while (waitpid(pid, &st, 0) == -1)
407 (void)signal(SIGHUP, sighup);
408 (void)signal(SIGINT, sigint);
409 (void)signal(SIGQUIT, sigquit);
411 if (!WIFEXITED(st)) {
416 return WEXITSTATUS(st);
419 static const struct got_error *
420 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 const char *initial_content)
423 const struct got_error *err = NULL;
427 int content_changed = 0;
432 if (stat(logmsg_path, &st) == -1)
433 return got_error_from_errno2("stat", logmsg_path);
435 if (spawn_editor(editor, logmsg_path) == -1)
436 return got_error_from_errno("failed spawning editor");
438 if (stat(logmsg_path, &st2) == -1)
439 return got_error_from_errno("stat");
441 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 "no changes made to commit message, aborting");
445 *logmsg = malloc(st2.st_size + 1);
447 return got_error_from_errno("malloc");
451 fp = fopen(logmsg_path, "r");
453 err = got_error_from_errno("fopen");
456 while (fgets(buf, sizeof(buf), fp) != NULL) {
457 if (!content_changed && strcmp(buf, initial_content) != 0)
459 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 continue; /* remove comments and leading empty lines */
461 len = strlcat(*logmsg, buf, st2.st_size);
465 while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 (*logmsg)[len - 1] = '\0';
470 if (len == 0 || !content_changed)
471 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 "commit message cannot be empty, aborting");
481 static const struct got_error *
482 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 const char *path_dir, const char *branch_name)
485 char *initial_content = NULL;
486 const struct got_error *err = NULL;
489 if (asprintf(&initial_content,
490 "\n# %s to be imported to branch %s\n", path_dir,
492 return got_error_from_errno("asprintf");
494 err = got_opentemp_named_fd(logmsg_path, &fd,
495 GOT_TMPDIR_STR "/got-importmsg");
499 dprintf(fd, initial_content);
502 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
504 free(initial_content);
508 static const struct got_error *
509 import_progress(void *arg, const char *path)
511 printf("A %s\n", path);
515 static const struct got_error *
516 get_author(char **author, struct got_repository *repo)
518 const struct got_error *err = NULL;
519 const char *got_author, *name, *email;
523 name = got_repo_get_gitconfig_author_name(repo);
524 email = got_repo_get_gitconfig_author_email(repo);
526 if (asprintf(author, "%s <%s>", name, email) == -1)
527 return got_error_from_errno("asprintf");
531 got_author = getenv("GOT_AUTHOR");
532 if (got_author == NULL) {
533 name = got_repo_get_global_gitconfig_author_name(repo);
534 email = got_repo_get_global_gitconfig_author_email(repo);
536 if (asprintf(author, "%s <%s>", name, email) == -1)
537 return got_error_from_errno("asprintf");
540 /* TODO: Look up user in password database? */
541 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 *author = strdup(got_author);
546 return got_error_from_errno("strdup");
549 * Really dumb email address check; we're only doing this to
550 * avoid git's object parser breaking on commits we create.
552 while (*got_author && *got_author != '<')
554 if (*got_author != '<') {
555 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 while (*got_author && *got_author != '@')
560 if (*got_author != '@') {
561 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 while (*got_author && *got_author != '>')
566 if (*got_author != '>')
567 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
576 static const struct got_error *
577 get_gitconfig_path(char **gitconfig_path)
579 const char *homedir = getenv("HOME");
581 *gitconfig_path = NULL;
583 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 return got_error_from_errno("asprintf");
590 static const struct got_error *
591 cmd_import(int argc, char *argv[])
593 const struct got_error *error = NULL;
594 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 const char *branch_name = "main";
597 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 struct got_repository *repo = NULL;
599 struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 struct got_object_id *new_commit_id = NULL;
602 struct got_pathlist_head ignores;
603 struct got_pathlist_entry *pe;
604 int preserve_logmsg = 0;
606 TAILQ_INIT(&ignores);
608 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
611 branch_name = optarg;
614 logmsg = strdup(optarg);
615 if (logmsg == NULL) {
616 error = got_error_from_errno("strdup");
621 repo_path = realpath(optarg, NULL);
622 if (repo_path == NULL) {
623 error = got_error_from_errno2("realpath",
629 if (optarg[0] == '\0')
631 error = got_pathlist_insert(&pe, &ignores, optarg,
646 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
654 if (repo_path == NULL) {
655 repo_path = getcwd(NULL, 0);
656 if (repo_path == NULL)
657 return got_error_from_errno("getcwd");
659 got_path_strip_trailing_slashes(repo_path);
660 error = get_gitconfig_path(&gitconfig_path);
663 error = got_repo_open(&repo, repo_path, gitconfig_path);
667 error = get_author(&author, repo);
672 * Don't let the user create a branch name with a leading '-'.
673 * While technically a valid reference name, this case is usually
674 * an unintended typo.
676 if (branch_name[0] == '-')
677 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
679 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 error = got_error_from_errno("asprintf");
684 error = got_ref_open(&branch_ref, repo, refname, 0);
686 if (error->code != GOT_ERR_NOT_REF)
689 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 "import target branch already exists");
694 path_dir = realpath(argv[0], NULL);
695 if (path_dir == NULL) {
696 error = got_error_from_errno2("realpath", argv[0]);
699 got_path_strip_trailing_slashes(path_dir);
702 * unveil(2) traverses exec(2); if an editor is used we have
703 * to apply unveil after the log message has been written.
705 if (logmsg == NULL || strlen(logmsg) == 0) {
706 error = get_editor(&editor);
710 error = collect_import_msg(&logmsg, &logmsg_path, editor,
713 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
720 if (unveil(path_dir, "r") != 0) {
721 error = got_error_from_errno2("unveil", path_dir);
727 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
734 error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 author, &ignores, repo, import_progress, NULL);
742 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
749 error = got_ref_write(branch_ref, repo);
756 error = got_object_id_str(&id_str, new_commit_id);
763 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
765 if (error->code != GOT_ERR_NOT_REF) {
771 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
779 error = got_ref_write(head_ref, repo);
787 printf("Created branch %s with commit %s\n",
788 got_ref_get_name(branch_ref), id_str);
790 if (preserve_logmsg) {
791 fprintf(stderr, "%s: log message preserved in %s\n",
792 getprogname(), logmsg_path);
793 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 error = got_error_from_errno2("unlink", logmsg_path);
803 free(gitconfig_path);
805 got_ref_close(branch_ref);
807 got_ref_close(head_ref);
814 fprintf(stderr, "usage: %s clone [-q] [-v] repository-url [target-directory]\n",
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);
843 if (packfile_size > 0 || nobj_indexed > 0) {
844 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
845 (a->last_scaled_size[0] == '\0' ||
846 strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 if (strlcpy(a->last_scaled_size, scaled_size,
849 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
850 return got_error(GOT_ERR_NO_SPACE);
852 if (nobj_indexed > 0) {
853 p_indexed = (nobj_indexed * 100) / nobj_total;
854 if (p_indexed != a->last_p_indexed) {
855 a->last_p_indexed = p_indexed;
860 if (nobj_resolved > 0) {
861 p_resolved = (nobj_resolved * 100) /
862 (nobj_total - nobj_loose);
863 if (p_resolved != a->last_p_resolved) {
864 a->last_p_resolved = p_resolved;
872 if (print_size || print_indexed || print_resolved)
875 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 printf("; indexing %d%%", p_indexed);
878 if (a->verbosity > 0)
879 printf(" (%d/%d)", nobj_indexed, nobj_total);
881 if (print_resolved) {
882 printf("; resolving deltas %d%%", p_resolved);
883 if (a->verbosity > 0)
884 printf(" (%d/%d)", nobj_resolved,
885 nobj_total - nobj_loose);
887 if (print_size || print_indexed || print_resolved)
890 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
891 nobj_resolved == nobj_total - nobj_loose)
892 printf("\nWriting pack index...\n");
897 static const struct got_error *
898 cmd_clone(int argc, char *argv[])
900 const struct got_error *error = NULL;
901 const char *uri, *dirname;
902 char *proto, *host, *port, *repo_name, *server_path;
903 char *default_destdir = NULL, *id_str = NULL;
904 const char *repo_path;
905 struct got_repository *repo = NULL;
906 struct got_pathlist_head refs, symrefs;
907 struct got_pathlist_entry *pe;
908 struct got_object_id *pack_hash = NULL;
909 int ch, fetchfd = -1;
910 struct got_fetch_progress_arg fpa;
911 char *git_url = NULL;
912 char *gitconfig_path = NULL;
913 char *gitconfig = NULL;
914 FILE *gitconfig_file = NULL;
919 TAILQ_INIT(&symrefs);
921 while ((ch = getopt(argc, argv, "vq")) != -1) {
926 else if (verbosity < 3)
949 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
950 &repo_name, argv[0]);
954 if (strcmp(proto, "git") == 0) {
956 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
957 "sendfd dns inet unveil", NULL) == -1)
960 git_url = strdup(argv[0]);
961 if (git_url == NULL) {
962 error = got_error_from_errno("strdup");
965 } else if (strcmp(proto, "git+ssh") == 0 ||
966 strcmp(proto, "ssh") == 0) {
968 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
969 "sendfd unveil", NULL) == -1)
972 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
973 error = got_error_from_errno("asprintf");
976 } else if (strcmp(proto, "http") == 0 ||
977 strcmp(proto, "git+http") == 0) {
978 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
981 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
984 if (dirname == NULL) {
985 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
986 error = got_error_from_errno("asprintf");
989 repo_path = default_destdir;
993 error = got_path_mkdir(repo_path);
997 error = got_repo_init(repo_path);
1001 error = got_repo_open(&repo, repo_path, NULL);
1005 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1006 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1007 error = got_error_from_errno2("unveil",
1008 GOT_FETCH_PATH_SSH);
1012 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1016 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1022 printf("Connected to %s:%s\n", host, port);
1024 fpa.last_scaled_size[0] = '\0';
1025 fpa.last_p_indexed = -1;
1026 fpa.last_p_resolved = -1;
1027 fpa.verbosity = verbosity;
1028 error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1029 GOT_FETCH_DEFAULT_REMOTE_NAME, fetchfd, repo,
1030 fetch_progress, &fpa);
1034 error = got_object_id_str(&id_str, pack_hash);
1038 printf("Fetched %s.pack\n", id_str);
1041 /* Set up references provided with the pack file. */
1042 TAILQ_FOREACH(pe, &refs, entry) {
1043 const char *refname = pe->path;
1044 struct got_object_id *id = pe->data;
1045 struct got_reference *ref;
1046 char *remote_refname;
1048 error = got_ref_alloc(&ref, refname, id);
1051 error = got_ref_write(ref, repo);
1056 if (strncmp("refs/heads/", refname, 11) != 0)
1059 if (asprintf(&remote_refname,
1060 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1061 refname + 11) == -1) {
1062 error = got_error_from_errno("asprintf");
1065 error = got_ref_alloc(&ref, remote_refname, id);
1068 error = got_ref_write(ref, repo);
1074 /* Set the HEAD reference if the server provided one. */
1075 TAILQ_FOREACH(pe, &symrefs, entry) {
1076 struct got_reference *symref, *target_ref;
1077 const char *refname = pe->path;
1078 const char *target = pe->data;
1080 if (strcmp(refname, GOT_REF_HEAD) != 0)
1083 error = got_ref_open(&target_ref, repo, target, 0);
1085 if (error->code == GOT_ERR_NOT_REF)
1090 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1091 got_ref_close(target_ref);
1095 if (verbosity > 1) {
1096 printf("Setting %s to %s\n", GOT_REF_HEAD,
1097 got_ref_get_symref_target(symref));
1100 printf("Created cloned repository '%s'\n", repo_path);
1102 error = got_ref_write(symref, repo);
1103 got_ref_close(symref);
1107 /* Create a config file so Git can understand this repository. */
1108 gitconfig_path = got_repo_get_path_gitconfig(repo);
1109 if (gitconfig_path == NULL) {
1110 error = got_error_from_errno("got_repo_get_path_gitconfig");
1113 gitconfig_file = fopen(gitconfig_path, "w");
1114 if (gitconfig_file == NULL) {
1115 error = got_error_from_errno2("fopen", gitconfig_path);
1118 if (asprintf(&gitconfig,
1120 "\trepositoryformatversion = 0\n"
1124 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1125 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1126 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1127 error = got_error_from_errno("asprintf");
1130 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1131 if (n != strlen(gitconfig))
1132 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1134 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1135 error = got_error_from_errno("close");
1136 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1137 error = got_error_from_errno("fclose");
1139 got_repo_close(repo);
1140 TAILQ_FOREACH(pe, &refs, entry) {
1141 free((void *)pe->path);
1144 got_pathlist_free(&refs);
1145 TAILQ_FOREACH(pe, &symrefs, entry) {
1146 free((void *)pe->path);
1149 got_pathlist_free(&symrefs);
1156 free(default_destdir);
1157 free(gitconfig_path);
1162 static const struct got_error *
1163 create_ref(const char *refname, struct got_object_id *id,
1164 const char *id_str, struct got_repository *repo)
1166 const struct got_error *err = NULL;
1167 struct got_reference *ref;
1169 printf("Creating %s: %s\n", refname, id_str);
1171 err = got_ref_alloc(&ref, refname, id);
1175 err = got_ref_write(ref, repo);
1180 static const struct got_error *
1181 update_ref(struct got_reference *ref, struct got_object_id *new_id,
1182 struct got_repository *repo)
1184 const struct got_error *err = NULL;
1185 char *new_id_str = NULL;
1186 struct got_object_id *old_id = NULL;
1188 err = got_object_id_str(&new_id_str, new_id);
1192 if (got_ref_is_symbolic(ref)) {
1193 struct got_reference *new_ref;
1194 err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1197 printf("Deleting symbolic reference %s -> %s\n",
1198 got_ref_get_name(ref), got_ref_get_symref_target(ref));
1199 err = got_ref_delete(ref, repo);
1202 printf("Setting %s to %s\n", got_ref_get_name(ref),
1204 err = got_ref_write(new_ref, repo);
1208 err = got_ref_resolve(&old_id, repo, ref);
1211 if (got_object_id_cmp(old_id, new_id) != 0) {
1212 printf("Setting %s to %s\n",
1213 got_ref_get_name(ref), new_id_str);
1214 err = got_ref_change_ref(ref, new_id);
1217 err = got_ref_write(ref, repo);
1231 fprintf(stderr, "usage: %s fetch [-r repository-path] [-q] [-v] "
1232 "[remote-repository-name]\n", getprogname());
1236 static const struct got_error *
1237 cmd_fetch(int argc, char *argv[])
1239 const struct got_error *error = NULL;
1240 char *cwd = NULL, *repo_path = NULL;
1241 const char *remote_name;
1242 char *proto = NULL, *host = NULL, *port = NULL;
1243 char *repo_name = NULL, *server_path = NULL;
1244 struct got_remote_repo *remotes, *remote = NULL;
1246 char *id_str = NULL;
1247 struct got_repository *repo = NULL;
1248 struct got_worktree *worktree = NULL;
1249 struct got_pathlist_head refs, symrefs;
1250 struct got_pathlist_entry *pe;
1251 struct got_object_id *pack_hash = NULL;
1252 int i, ch, fetchfd = -1;
1253 struct got_fetch_progress_arg fpa;
1257 TAILQ_INIT(&symrefs);
1259 while ((ch = getopt(argc, argv, "r:vq")) != -1) {
1262 repo_path = realpath(optarg, NULL);
1263 if (repo_path == NULL)
1264 return got_error_from_errno2("realpath",
1266 got_path_strip_trailing_slashes(repo_path);
1271 else if (verbosity < 3)
1286 remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1288 remote_name = argv[0];
1292 cwd = getcwd(NULL, 0);
1294 error = got_error_from_errno("getcwd");
1298 if (repo_path == NULL) {
1299 error = got_worktree_open(&worktree, cwd);
1300 if (error && error->code != GOT_ERR_NOT_WORKTREE)
1306 strdup(got_worktree_get_repo_path(worktree));
1307 if (repo_path == NULL)
1308 error = got_error_from_errno("strdup");
1312 repo_path = strdup(cwd);
1313 if (repo_path == NULL) {
1314 error = got_error_from_errno("strdup");
1320 error = got_repo_open(&repo, repo_path, NULL);
1324 got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1325 for (i = 0; i < nremotes; i++) {
1326 remote = &remotes[i];
1327 if (strcmp(remote->name, remote_name) == 0)
1330 if (i == nremotes) {
1331 error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1335 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1336 &repo_name, remote->url);
1340 if (strcmp(proto, "git") == 0) {
1342 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1343 "sendfd dns inet unveil", NULL) == -1)
1346 } else if (strcmp(proto, "git+ssh") == 0 ||
1347 strcmp(proto, "ssh") == 0) {
1349 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1350 "sendfd unveil", NULL) == -1)
1353 } else if (strcmp(proto, "http") == 0 ||
1354 strcmp(proto, "git+http") == 0) {
1355 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1358 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1362 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1363 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1364 error = got_error_from_errno2("unveil",
1365 GOT_FETCH_PATH_SSH);
1369 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1373 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1379 printf("Connected to \"%s\" %s:%s\n", remote->name, host, port);
1381 fpa.last_scaled_size[0] = '\0';
1382 fpa.last_p_indexed = -1;
1383 fpa.last_p_resolved = -1;
1384 fpa.verbosity = verbosity;
1385 error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1386 fetchfd, repo, fetch_progress, &fpa);
1390 if (pack_hash == NULL) {
1392 printf("Already up-to-date\n");
1396 error = got_object_id_str(&id_str, pack_hash);
1400 printf("Fetched %s.pack\n", id_str);
1404 /* Update references provided with the pack file. */
1405 TAILQ_FOREACH(pe, &refs, entry) {
1406 const char *refname = pe->path;
1407 struct got_object_id *id = pe->data;
1408 struct got_reference *ref;
1409 char *remote_refname;
1411 error = got_object_id_str(&id_str, id);
1415 if (strncmp("refs/tags/", refname, 10) == 0) {
1416 error = got_ref_open(&ref, repo, refname, 0);
1418 if (error->code != GOT_ERR_NOT_REF)
1420 error = create_ref(refname, id, id_str, repo);
1424 error = update_ref(ref, id, repo);
1429 } else if (strncmp("refs/heads/", refname, 11) == 0) {
1430 if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1431 remote_name, refname + 11) == -1) {
1432 error = got_error_from_errno("asprintf");
1436 error = got_ref_open(&ref, repo, remote_refname, 0);
1438 if (error->code != GOT_ERR_NOT_REF)
1440 error = create_ref(refname, id, id_str, repo);
1444 error = update_ref(ref, id, repo);
1454 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1455 error = got_error_from_errno("close");
1457 got_repo_close(repo);
1459 got_worktree_close(worktree);
1460 TAILQ_FOREACH(pe, &refs, entry) {
1461 free((void *)pe->path);
1464 got_pathlist_free(&refs);
1465 TAILQ_FOREACH(pe, &symrefs, entry) {
1466 free((void *)pe->path);
1469 got_pathlist_free(&symrefs);
1484 usage_checkout(void)
1486 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1487 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1492 show_worktree_base_ref_warning(void)
1494 fprintf(stderr, "%s: warning: could not create a reference "
1495 "to the work tree's base commit; the commit could be "
1496 "garbage-collected by Git; making the repository "
1497 "writable and running 'got update' will prevent this\n",
1501 struct got_checkout_progress_arg {
1502 const char *worktree_path;
1503 int had_base_commit_ref_error;
1506 static const struct got_error *
1507 checkout_progress(void *arg, unsigned char status, const char *path)
1509 struct got_checkout_progress_arg *a = arg;
1511 /* Base commit bump happens silently. */
1512 if (status == GOT_STATUS_BUMP_BASE)
1515 if (status == GOT_STATUS_BASE_REF_ERR) {
1516 a->had_base_commit_ref_error = 1;
1520 while (path[0] == '/')
1523 printf("%c %s/%s\n", status, a->worktree_path, path);
1527 static const struct got_error *
1528 check_cancelled(void *arg)
1530 if (sigint_received || sigpipe_received)
1531 return got_error(GOT_ERR_CANCELLED);
1535 static const struct got_error *
1536 check_linear_ancestry(struct got_object_id *commit_id,
1537 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1538 struct got_repository *repo)
1540 const struct got_error *err = NULL;
1541 struct got_object_id *yca_id;
1543 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1544 commit_id, base_commit_id, repo, check_cancelled, NULL);
1549 return got_error(GOT_ERR_ANCESTRY);
1552 * Require a straight line of history between the target commit
1553 * and the work tree's base commit.
1555 * Non-linear situations such as this require a rebase:
1557 * (commit) D F (base_commit)
1565 * 'got update' only handles linear cases:
1566 * Update forwards in time: A (base/yca) - B - C - D (commit)
1567 * Update backwards in time: D (base) - C - B - A (commit/yca)
1569 if (allow_forwards_in_time_only) {
1570 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1571 return got_error(GOT_ERR_ANCESTRY);
1572 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1573 got_object_id_cmp(base_commit_id, yca_id) != 0)
1574 return got_error(GOT_ERR_ANCESTRY);
1580 static const struct got_error *
1581 check_same_branch(struct got_object_id *commit_id,
1582 struct got_reference *head_ref, struct got_object_id *yca_id,
1583 struct got_repository *repo)
1585 const struct got_error *err = NULL;
1586 struct got_commit_graph *graph = NULL;
1587 struct got_object_id *head_commit_id = NULL;
1588 int is_same_branch = 0;
1590 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1594 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1598 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1603 err = got_commit_graph_open(&graph, "/", 1);
1607 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1608 check_cancelled, NULL);
1613 struct got_object_id *id;
1614 err = got_commit_graph_iter_next(&id, graph, repo,
1615 check_cancelled, NULL);
1617 if (err->code == GOT_ERR_ITER_COMPLETED)
1623 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1625 if (got_object_id_cmp(id, commit_id) == 0) {
1633 got_commit_graph_close(graph);
1634 free(head_commit_id);
1635 if (!err && !is_same_branch)
1636 err = got_error(GOT_ERR_ANCESTRY);
1640 static const struct got_error *
1641 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1643 static char msg[512];
1644 const char *branch_name;
1646 if (got_ref_is_symbolic(ref))
1647 branch_name = got_ref_get_symref_target(ref);
1649 branch_name = got_ref_get_name(ref);
1651 if (strncmp("refs/heads/", branch_name, 11) == 0)
1654 snprintf(msg, sizeof(msg),
1655 "target commit is not contained in branch '%s'; "
1656 "the branch to use must be specified with -b; "
1657 "if necessary a new branch can be created for "
1658 "this commit with 'got branch -c %s BRANCH_NAME'",
1659 branch_name, commit_id_str);
1661 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1664 static const struct got_error *
1665 cmd_checkout(int argc, char *argv[])
1667 const struct got_error *error = NULL;
1668 struct got_repository *repo = NULL;
1669 struct got_reference *head_ref = NULL;
1670 struct got_worktree *worktree = NULL;
1671 char *repo_path = NULL;
1672 char *worktree_path = NULL;
1673 const char *path_prefix = "";
1674 const char *branch_name = GOT_REF_HEAD;
1675 char *commit_id_str = NULL;
1676 int ch, same_path_prefix, allow_nonempty = 0;
1677 struct got_pathlist_head paths;
1678 struct got_checkout_progress_arg cpa;
1682 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1685 branch_name = optarg;
1688 commit_id_str = strdup(optarg);
1689 if (commit_id_str == NULL)
1690 return got_error_from_errno("strdup");
1696 path_prefix = optarg;
1708 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1709 "unveil", NULL) == -1)
1713 char *cwd, *base, *dotgit;
1714 repo_path = realpath(argv[0], NULL);
1715 if (repo_path == NULL)
1716 return got_error_from_errno2("realpath", argv[0]);
1717 cwd = getcwd(NULL, 0);
1719 error = got_error_from_errno("getcwd");
1722 if (path_prefix[0]) {
1723 base = basename(path_prefix);
1725 error = got_error_from_errno2("basename",
1730 base = basename(repo_path);
1732 error = got_error_from_errno2("basename",
1737 dotgit = strstr(base, ".git");
1740 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1741 error = got_error_from_errno("asprintf");
1746 } else if (argc == 2) {
1747 repo_path = realpath(argv[0], NULL);
1748 if (repo_path == NULL) {
1749 error = got_error_from_errno2("realpath", argv[0]);
1752 worktree_path = realpath(argv[1], NULL);
1753 if (worktree_path == NULL) {
1754 if (errno != ENOENT) {
1755 error = got_error_from_errno2("realpath",
1759 worktree_path = strdup(argv[1]);
1760 if (worktree_path == NULL) {
1761 error = got_error_from_errno("strdup");
1768 got_path_strip_trailing_slashes(repo_path);
1769 got_path_strip_trailing_slashes(worktree_path);
1771 error = got_repo_open(&repo, repo_path, NULL);
1775 /* Pre-create work tree path for unveil(2) */
1776 error = got_path_mkdir(worktree_path);
1778 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1779 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1781 if (!allow_nonempty &&
1782 !got_path_dir_is_empty(worktree_path)) {
1783 error = got_error_path(worktree_path,
1784 GOT_ERR_DIR_NOT_EMPTY);
1789 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1793 error = got_ref_open(&head_ref, repo, branch_name, 0);
1797 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1798 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1801 error = got_worktree_open(&worktree, worktree_path);
1805 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1809 if (!same_path_prefix) {
1810 error = got_error(GOT_ERR_PATH_PREFIX);
1814 if (commit_id_str) {
1815 struct got_object_id *commit_id;
1816 error = got_repo_match_object_id(&commit_id, NULL,
1817 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1820 error = check_linear_ancestry(commit_id,
1821 got_worktree_get_base_commit_id(worktree), 0, repo);
1822 if (error != NULL) {
1824 if (error->code == GOT_ERR_ANCESTRY) {
1825 error = checkout_ancestry_error(
1826 head_ref, commit_id_str);
1830 error = check_same_branch(commit_id, head_ref, NULL, repo);
1832 if (error->code == GOT_ERR_ANCESTRY) {
1833 error = checkout_ancestry_error(
1834 head_ref, commit_id_str);
1838 error = got_worktree_set_base_commit_id(worktree, repo,
1845 error = got_pathlist_append(&paths, "", NULL);
1848 cpa.worktree_path = worktree_path;
1849 cpa.had_base_commit_ref_error = 0;
1850 error = got_worktree_checkout_files(worktree, &paths, repo,
1851 checkout_progress, &cpa, check_cancelled, NULL);
1855 printf("Now shut up and hack\n");
1856 if (cpa.had_base_commit_ref_error)
1857 show_worktree_base_ref_warning();
1859 got_pathlist_free(&paths);
1860 free(commit_id_str);
1862 free(worktree_path);
1869 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1874 static const struct got_error *
1875 update_progress(void *arg, unsigned char status, const char *path)
1877 int *did_something = arg;
1879 if (status == GOT_STATUS_EXISTS ||
1880 status == GOT_STATUS_BASE_REF_ERR)
1885 /* Base commit bump happens silently. */
1886 if (status == GOT_STATUS_BUMP_BASE)
1889 while (path[0] == '/')
1891 printf("%c %s\n", status, path);
1895 static const struct got_error *
1896 switch_head_ref(struct got_reference *head_ref,
1897 struct got_object_id *commit_id, struct got_worktree *worktree,
1898 struct got_repository *repo)
1900 const struct got_error *err = NULL;
1902 int ref_has_moved = 0;
1904 /* Trivial case: switching between two different references. */
1905 if (strcmp(got_ref_get_name(head_ref),
1906 got_worktree_get_head_ref_name(worktree)) != 0) {
1907 printf("Switching work tree from %s to %s\n",
1908 got_worktree_get_head_ref_name(worktree),
1909 got_ref_get_name(head_ref));
1910 return got_worktree_set_head_ref(worktree, head_ref);
1913 err = check_linear_ancestry(commit_id,
1914 got_worktree_get_base_commit_id(worktree), 0, repo);
1916 if (err->code != GOT_ERR_ANCESTRY)
1923 /* Switching to a rebased branch with the same reference name. */
1924 err = got_object_id_str(&base_id_str,
1925 got_worktree_get_base_commit_id(worktree));
1928 printf("Reference %s now points at a different branch\n",
1929 got_worktree_get_head_ref_name(worktree));
1930 printf("Switching work tree from %s to %s\n", base_id_str,
1931 got_worktree_get_head_ref_name(worktree));
1935 static const struct got_error *
1936 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1938 const struct got_error *err;
1941 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1945 return got_error(GOT_ERR_REBASING);
1947 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1951 return got_error(GOT_ERR_HISTEDIT_BUSY);
1956 static const struct got_error *
1957 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1958 char *argv[], struct got_worktree *worktree)
1960 const struct got_error *err = NULL;
1967 return got_error_from_errno("strdup");
1968 return got_pathlist_append(paths, path, NULL);
1971 for (i = 0; i < argc; i++) {
1972 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1975 err = got_pathlist_append(paths, path, NULL);
1985 static const struct got_error *
1986 cmd_update(int argc, char *argv[])
1988 const struct got_error *error = NULL;
1989 struct got_repository *repo = NULL;
1990 struct got_worktree *worktree = NULL;
1991 char *worktree_path = NULL;
1992 struct got_object_id *commit_id = NULL;
1993 char *commit_id_str = NULL;
1994 const char *branch_name = NULL;
1995 struct got_reference *head_ref = NULL;
1996 struct got_pathlist_head paths;
1997 struct got_pathlist_entry *pe;
1998 int ch, did_something = 0;
2002 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2005 branch_name = optarg;
2008 commit_id_str = strdup(optarg);
2009 if (commit_id_str == NULL)
2010 return got_error_from_errno("strdup");
2022 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2023 "unveil", NULL) == -1)
2026 worktree_path = getcwd(NULL, 0);
2027 if (worktree_path == NULL) {
2028 error = got_error_from_errno("getcwd");
2031 error = got_worktree_open(&worktree, worktree_path);
2035 error = check_rebase_or_histedit_in_progress(worktree);
2039 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2044 error = apply_unveil(got_repo_get_path(repo), 0,
2045 got_worktree_get_root_path(worktree));
2049 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2053 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2054 got_worktree_get_head_ref_name(worktree), 0);
2057 if (commit_id_str == NULL) {
2058 error = got_ref_resolve(&commit_id, repo, head_ref);
2061 error = got_object_id_str(&commit_id_str, commit_id);
2065 error = got_repo_match_object_id(&commit_id, NULL,
2066 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2067 free(commit_id_str);
2068 commit_id_str = NULL;
2071 error = got_object_id_str(&commit_id_str, commit_id);
2077 struct got_object_id *head_commit_id;
2078 TAILQ_FOREACH(pe, &paths, entry) {
2079 if (pe->path_len == 0)
2081 error = got_error_msg(GOT_ERR_BAD_PATH,
2082 "switching between branches requires that "
2083 "the entire work tree gets updated");
2086 error = got_ref_resolve(&head_commit_id, repo, head_ref);
2089 error = check_linear_ancestry(commit_id, head_commit_id, 0,
2091 free(head_commit_id);
2094 error = check_same_branch(commit_id, head_ref, NULL, repo);
2097 error = switch_head_ref(head_ref, commit_id, worktree, repo);
2101 error = check_linear_ancestry(commit_id,
2102 got_worktree_get_base_commit_id(worktree), 0, repo);
2103 if (error != NULL) {
2104 if (error->code == GOT_ERR_ANCESTRY)
2105 error = got_error(GOT_ERR_BRANCH_MOVED);
2108 error = check_same_branch(commit_id, head_ref, NULL, repo);
2113 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2115 error = got_worktree_set_base_commit_id(worktree, repo,
2121 error = got_worktree_checkout_files(worktree, &paths, repo,
2122 update_progress, &did_something, check_cancelled, NULL);
2127 printf("Updated to commit %s\n", commit_id_str);
2129 printf("Already up-to-date\n");
2131 free(worktree_path);
2132 TAILQ_FOREACH(pe, &paths, entry)
2133 free((char *)pe->path);
2134 got_pathlist_free(&paths);
2136 free(commit_id_str);
2140 static const struct got_error *
2141 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2142 const char *path, int diff_context, int ignore_whitespace,
2143 struct got_repository *repo)
2145 const struct got_error *err = NULL;
2146 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2149 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2154 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2158 while (path[0] == '/')
2160 err = got_diff_blob(blob1, blob2, path, path, diff_context,
2161 ignore_whitespace, stdout);
2164 got_object_blob_close(blob1);
2165 got_object_blob_close(blob2);
2169 static const struct got_error *
2170 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2171 const char *path, int diff_context, int ignore_whitespace,
2172 struct got_repository *repo)
2174 const struct got_error *err = NULL;
2175 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2176 struct got_diff_blob_output_unidiff_arg arg;
2179 err = got_object_open_as_tree(&tree1, repo, tree_id1);
2184 err = got_object_open_as_tree(&tree2, repo, tree_id2);
2188 arg.diff_context = diff_context;
2189 arg.ignore_whitespace = ignore_whitespace;
2190 arg.outfile = stdout;
2191 while (path[0] == '/')
2193 err = got_diff_tree(tree1, tree2, path, path, repo,
2194 got_diff_blob_output_unidiff, &arg, 1);
2197 got_object_tree_close(tree1);
2199 got_object_tree_close(tree2);
2203 static const struct got_error *
2204 print_patch(struct got_commit_object *commit, struct got_object_id *id,
2205 const char *path, int diff_context, struct got_repository *repo)
2207 const struct got_error *err = NULL;
2208 struct got_commit_object *pcommit = NULL;
2209 char *id_str1 = NULL, *id_str2 = NULL;
2210 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2211 struct got_object_qid *qid;
2213 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2215 err = got_object_open_as_commit(&pcommit, repo,
2221 if (path && path[0] != '\0') {
2223 err = got_object_id_by_path(&obj_id2, repo, id, path);
2226 err = got_object_id_str(&id_str2, obj_id2);
2232 err = got_object_id_by_path(&obj_id1, repo,
2238 err = got_object_id_str(&id_str1, obj_id1);
2244 err = got_object_get_type(&obj_type, repo, obj_id2);
2249 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2251 case GOT_OBJ_TYPE_BLOB:
2252 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2255 case GOT_OBJ_TYPE_TREE:
2256 err = diff_trees(obj_id1, obj_id2, path, diff_context,
2260 err = got_error(GOT_ERR_OBJ_TYPE);
2266 obj_id2 = got_object_commit_get_tree_id(commit);
2267 err = got_object_id_str(&id_str2, obj_id2);
2270 obj_id1 = got_object_commit_get_tree_id(pcommit);
2271 err = got_object_id_str(&id_str1, obj_id1);
2274 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2275 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2281 got_object_commit_close(pcommit);
2286 get_datestr(time_t *time, char *datebuf)
2288 struct tm mytm, *tm;
2291 tm = gmtime_r(time, &mytm);
2294 s = asctime_r(tm, datebuf);
2297 p = strchr(s, '\n');
2303 static const struct got_error *
2304 match_logmsg(int *have_match, struct got_object_id *id,
2305 struct got_commit_object *commit, regex_t *regex)
2307 const struct got_error *err = NULL;
2308 regmatch_t regmatch;
2309 char *id_str = NULL, *logmsg = NULL;
2313 err = got_object_id_str(&id_str, id);
2317 err = got_object_commit_get_logmsg(&logmsg, commit);
2321 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2329 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2331 static const struct got_error *
2332 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2333 struct got_repository *repo, const char *path, int show_patch,
2334 int diff_context, struct got_reflist_head *refs)
2336 const struct got_error *err = NULL;
2337 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2339 time_t committer_time;
2340 const char *author, *committer;
2341 char *refs_str = NULL;
2342 struct got_reflist_entry *re;
2344 SIMPLEQ_FOREACH(re, refs, entry) {
2347 struct got_tag_object *tag = NULL;
2350 name = got_ref_get_name(re->ref);
2351 if (strcmp(name, GOT_REF_HEAD) == 0)
2353 if (strncmp(name, "refs/", 5) == 0)
2355 if (strncmp(name, "got/", 4) == 0)
2357 if (strncmp(name, "heads/", 6) == 0)
2359 if (strncmp(name, "remotes/", 8) == 0)
2361 if (strncmp(name, "tags/", 5) == 0) {
2362 err = got_object_open_as_tag(&tag, repo, re->id);
2364 if (err->code != GOT_ERR_OBJ_TYPE)
2366 /* Ref points at something other than a tag. */
2371 cmp = got_object_id_cmp(tag ?
2372 got_object_tag_get_object_id(tag) : re->id, id);
2374 got_object_tag_close(tag);
2378 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2380 err = got_error_from_errno("asprintf");
2386 err = got_object_id_str(&id_str, id);
2390 printf(GOT_COMMIT_SEP_STR);
2391 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2392 refs_str ? refs_str : "", refs_str ? ")" : "");
2397 printf("from: %s\n", got_object_commit_get_author(commit));
2398 committer_time = got_object_commit_get_committer_time(commit);
2399 datestr = get_datestr(&committer_time, datebuf);
2401 printf("date: %s UTC\n", datestr);
2402 author = got_object_commit_get_author(commit);
2403 committer = got_object_commit_get_committer(commit);
2404 if (strcmp(author, committer) != 0)
2405 printf("via: %s\n", committer);
2406 if (got_object_commit_get_nparents(commit) > 1) {
2407 const struct got_object_id_queue *parent_ids;
2408 struct got_object_qid *qid;
2410 parent_ids = got_object_commit_get_parent_ids(commit);
2411 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2412 err = got_object_id_str(&id_str, qid->id);
2415 printf("parent %d: %s\n", n++, id_str);
2420 err = got_object_commit_get_logmsg(&logmsg0, commit);
2426 line = strsep(&logmsg, "\n");
2428 printf(" %s\n", line);
2433 err = print_patch(commit, id, path, diff_context, repo);
2438 if (fflush(stdout) != 0 && err == NULL)
2439 err = got_error_from_errno("fflush");
2443 static const struct got_error *
2444 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2445 const char *path, int show_patch, const char *search_pattern,
2446 int diff_context, int limit, int log_branches,
2447 struct got_reflist_head *refs)
2449 const struct got_error *err;
2450 struct got_commit_graph *graph;
2454 if (search_pattern &&
2455 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2456 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2458 err = got_commit_graph_open(&graph, path, !log_branches);
2461 err = got_commit_graph_iter_start(graph, root_id, repo,
2462 check_cancelled, NULL);
2466 struct got_commit_object *commit;
2467 struct got_object_id *id;
2469 if (sigint_received || sigpipe_received)
2472 err = got_commit_graph_iter_next(&id, graph, repo,
2473 check_cancelled, NULL);
2475 if (err->code == GOT_ERR_ITER_COMPLETED)
2482 err = got_object_open_as_commit(&commit, repo, id);
2486 if (search_pattern) {
2487 err = match_logmsg(&have_match, id, commit, ®ex);
2489 got_object_commit_close(commit);
2492 if (have_match == 0) {
2493 got_object_commit_close(commit);
2498 err = print_commit(commit, id, repo, path, show_patch,
2499 diff_context, refs);
2500 got_object_commit_close(commit);
2501 if (err || (limit && --limit == 0))
2507 got_commit_graph_close(graph);
2514 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2515 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2520 get_default_log_limit(void)
2522 const char *got_default_log_limit;
2526 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2527 if (got_default_log_limit == NULL)
2529 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2535 static const struct got_error *
2536 cmd_log(int argc, char *argv[])
2538 const struct got_error *error;
2539 struct got_repository *repo = NULL;
2540 struct got_worktree *worktree = NULL;
2541 struct got_commit_object *commit = NULL;
2542 struct got_object_id *id = NULL;
2543 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2544 const char *start_commit = NULL, *search_pattern = NULL;
2545 int diff_context = -1, ch;
2546 int show_patch = 0, limit = 0, log_branches = 0;
2548 struct got_reflist_head refs;
2550 SIMPLEQ_INIT(&refs);
2553 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2559 limit = get_default_log_limit();
2561 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2567 start_commit = optarg;
2570 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2573 err(1, "-C option %s", errstr);
2576 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2578 err(1, "-l option %s", errstr);
2584 repo_path = realpath(optarg, NULL);
2585 if (repo_path == NULL)
2586 return got_error_from_errno2("realpath",
2588 got_path_strip_trailing_slashes(repo_path);
2591 search_pattern = optarg;
2602 if (diff_context == -1)
2604 else if (!show_patch)
2605 errx(1, "-C reguires -p");
2607 cwd = getcwd(NULL, 0);
2609 error = got_error_from_errno("getcwd");
2613 error = got_worktree_open(&worktree, cwd);
2614 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2621 error = got_error_from_errno("strdup");
2624 } else if (argc == 1) {
2626 error = got_worktree_resolve_path(&path, worktree,
2631 path = strdup(argv[0]);
2633 error = got_error_from_errno("strdup");
2640 if (repo_path == NULL) {
2641 repo_path = worktree ?
2642 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2644 if (repo_path == NULL) {
2645 error = got_error_from_errno("strdup");
2649 error = got_repo_open(&repo, repo_path, NULL);
2653 error = apply_unveil(got_repo_get_path(repo), 1,
2654 worktree ? got_worktree_get_root_path(worktree) : NULL);
2658 if (start_commit == NULL) {
2659 struct got_reference *head_ref;
2660 error = got_ref_open(&head_ref, repo,
2661 worktree ? got_worktree_get_head_ref_name(worktree)
2665 error = got_ref_resolve(&id, repo, head_ref);
2666 got_ref_close(head_ref);
2669 error = got_object_open_as_commit(&commit, repo, id);
2671 struct got_reference *ref;
2672 error = got_ref_open(&ref, repo, start_commit, 0);
2673 if (error == NULL) {
2675 error = got_ref_resolve(&id, repo, ref);
2679 error = got_object_get_type(&obj_type, repo, id);
2682 if (obj_type == GOT_OBJ_TYPE_TAG) {
2683 struct got_tag_object *tag;
2684 error = got_object_open_as_tag(&tag, repo, id);
2687 if (got_object_tag_get_object_type(tag) !=
2688 GOT_OBJ_TYPE_COMMIT) {
2689 got_object_tag_close(tag);
2690 error = got_error(GOT_ERR_OBJ_TYPE);
2694 id = got_object_id_dup(
2695 got_object_tag_get_object_id(tag));
2697 error = got_error_from_errno(
2698 "got_object_id_dup");
2699 got_object_tag_close(tag);
2702 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2703 error = got_error(GOT_ERR_OBJ_TYPE);
2706 error = got_object_open_as_commit(&commit, repo, id);
2710 if (commit == NULL) {
2711 error = got_repo_match_object_id_prefix(&id,
2712 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2721 const char *prefix = got_worktree_get_path_prefix(worktree);
2723 if (asprintf(&p, "%s%s%s", prefix,
2724 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2725 error = got_error_from_errno("asprintf");
2728 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2731 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2736 path = in_repo_path;
2739 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2743 error = print_commits(id, repo, path, show_patch, search_pattern,
2744 diff_context, limit, log_branches, &refs);
2751 got_worktree_close(worktree);
2753 const struct got_error *repo_error;
2754 repo_error = got_repo_close(repo);
2758 got_ref_list_free(&refs);
2765 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2766 "[-w] [object1 object2 | path]\n", getprogname());
2770 struct print_diff_arg {
2771 struct got_repository *repo;
2772 struct got_worktree *worktree;
2777 int ignore_whitespace;
2780 static const struct got_error *
2781 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2782 const char *path, struct got_object_id *blob_id,
2783 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2784 int dirfd, const char *de_name)
2786 struct print_diff_arg *a = arg;
2787 const struct got_error *err = NULL;
2788 struct got_blob_object *blob1 = NULL;
2791 char *abspath = NULL, *label1 = NULL;
2794 if (a->diff_staged) {
2795 if (staged_status != GOT_STATUS_MODIFY &&
2796 staged_status != GOT_STATUS_ADD &&
2797 staged_status != GOT_STATUS_DELETE)
2800 if (staged_status == GOT_STATUS_DELETE)
2802 if (status == GOT_STATUS_NONEXISTENT)
2803 return got_error_set_errno(ENOENT, path);
2804 if (status != GOT_STATUS_MODIFY &&
2805 status != GOT_STATUS_ADD &&
2806 status != GOT_STATUS_DELETE &&
2807 status != GOT_STATUS_CONFLICT)
2811 if (!a->header_shown) {
2812 printf("diff %s %s%s\n", a->id_str,
2813 got_worktree_get_root_path(a->worktree),
2814 a->diff_staged ? " (staged changes)" : "");
2815 a->header_shown = 1;
2818 if (a->diff_staged) {
2819 const char *label1 = NULL, *label2 = NULL;
2820 switch (staged_status) {
2821 case GOT_STATUS_MODIFY:
2825 case GOT_STATUS_ADD:
2828 case GOT_STATUS_DELETE:
2832 return got_error(GOT_ERR_FILE_STATUS);
2834 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2835 label1, label2, a->diff_context, a->ignore_whitespace,
2839 if (staged_status == GOT_STATUS_ADD ||
2840 staged_status == GOT_STATUS_MODIFY) {
2842 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2846 err = got_object_id_str(&id_str, staged_blob_id);
2849 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2850 err = got_error_from_errno("asprintf");
2855 } else if (status != GOT_STATUS_ADD) {
2856 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2861 if (status != GOT_STATUS_DELETE) {
2862 if (asprintf(&abspath, "%s/%s",
2863 got_worktree_get_root_path(a->worktree), path) == -1) {
2864 err = got_error_from_errno("asprintf");
2869 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2871 err = got_error_from_errno2("openat", abspath);
2875 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2877 err = got_error_from_errno2("open", abspath);
2881 if (fstat(fd, &sb) == -1) {
2882 err = got_error_from_errno2("fstat", abspath);
2885 f2 = fdopen(fd, "r");
2887 err = got_error_from_errno2("fdopen", abspath);
2894 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2895 a->diff_context, a->ignore_whitespace, stdout);
2898 got_object_blob_close(blob1);
2899 if (f2 && fclose(f2) == EOF && err == NULL)
2900 err = got_error_from_errno("fclose");
2901 if (fd != -1 && close(fd) == -1 && err == NULL)
2902 err = got_error_from_errno("close");
2907 static const struct got_error *
2908 cmd_diff(int argc, char *argv[])
2910 const struct got_error *error;
2911 struct got_repository *repo = NULL;
2912 struct got_worktree *worktree = NULL;
2913 char *cwd = NULL, *repo_path = NULL;
2914 struct got_object_id *id1 = NULL, *id2 = NULL;
2915 const char *id_str1 = NULL, *id_str2 = NULL;
2916 char *label1 = NULL, *label2 = NULL;
2918 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2923 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2928 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2931 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2934 err(1, "-C option %s", errstr);
2937 repo_path = realpath(optarg, NULL);
2938 if (repo_path == NULL)
2939 return got_error_from_errno2("realpath",
2941 got_path_strip_trailing_slashes(repo_path);
2947 ignore_whitespace = 1;
2958 cwd = getcwd(NULL, 0);
2960 error = got_error_from_errno("getcwd");
2963 error = got_worktree_open(&worktree, cwd);
2964 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2967 if (worktree == NULL) {
2968 error = got_error(GOT_ERR_NOT_WORKTREE);
2973 "-r option can't be used when diffing a work tree");
2974 repo_path = strdup(got_worktree_get_repo_path(worktree));
2975 if (repo_path == NULL) {
2976 error = got_error_from_errno("strdup");
2980 error = got_worktree_resolve_path(&path, worktree,
2987 error = got_error_from_errno("strdup");
2991 } else if (argc == 2) {
2993 errx(1, "-s option can't be used when diffing "
2994 "objects in repository");
2997 if (worktree && repo_path == NULL) {
2999 strdup(got_worktree_get_repo_path(worktree));
3000 if (repo_path == NULL) {
3001 error = got_error_from_errno("strdup");
3008 if (repo_path == NULL) {
3009 repo_path = getcwd(NULL, 0);
3010 if (repo_path == NULL)
3011 return got_error_from_errno("getcwd");
3014 error = got_repo_open(&repo, repo_path, NULL);
3019 error = apply_unveil(got_repo_get_path(repo), 1,
3020 worktree ? got_worktree_get_root_path(worktree) : NULL);
3025 struct print_diff_arg arg;
3026 struct got_pathlist_head paths;
3031 error = got_object_id_str(&id_str,
3032 got_worktree_get_base_commit_id(worktree));
3036 arg.worktree = worktree;
3037 arg.diff_context = diff_context;
3038 arg.id_str = id_str;
3039 arg.header_shown = 0;
3040 arg.diff_staged = diff_staged;
3041 arg.ignore_whitespace = ignore_whitespace;
3043 error = got_pathlist_append(&paths, path, NULL);
3047 error = got_worktree_status(worktree, &paths, repo, print_diff,
3048 &arg, check_cancelled, NULL);
3050 got_pathlist_free(&paths);
3054 error = got_repo_match_object_id(&id1, &label1, id_str1,
3055 GOT_OBJ_TYPE_ANY, 1, repo);
3059 error = got_repo_match_object_id(&id2, &label2, id_str2,
3060 GOT_OBJ_TYPE_ANY, 1, repo);
3064 error = got_object_get_type(&type1, repo, id1);
3068 error = got_object_get_type(&type2, repo, id2);
3072 if (type1 != type2) {
3073 error = got_error(GOT_ERR_OBJ_TYPE);
3078 case GOT_OBJ_TYPE_BLOB:
3079 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3080 diff_context, ignore_whitespace, repo, stdout);
3082 case GOT_OBJ_TYPE_TREE:
3083 error = got_diff_objects_as_trees(id1, id2, "", "",
3084 diff_context, ignore_whitespace, repo, stdout);
3086 case GOT_OBJ_TYPE_COMMIT:
3087 printf("diff %s %s\n", label1, label2);
3088 error = got_diff_objects_as_commits(id1, id2, diff_context,
3089 ignore_whitespace, repo, stdout);
3092 error = got_error(GOT_ERR_OBJ_TYPE);
3101 got_worktree_close(worktree);
3103 const struct got_error *repo_error;
3104 repo_error = got_repo_close(repo);
3115 "usage: %s blame [-c commit] [-r repository-path] path\n",
3124 char datebuf[11]; /* YYYY-MM-DD + NUL */
3127 struct blame_cb_args {
3128 struct blame_line *lines;
3132 off_t *line_offsets;
3134 struct got_repository *repo;
3137 static const struct got_error *
3138 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3140 const struct got_error *err = NULL;
3141 struct blame_cb_args *a = arg;
3142 struct blame_line *bline;
3144 size_t linesize = 0;
3145 struct got_commit_object *commit = NULL;
3148 time_t committer_time;
3150 if (nlines != a->nlines ||
3151 (lineno != -1 && lineno < 1) || lineno > a->nlines)
3152 return got_error(GOT_ERR_RANGE);
3154 if (sigint_received)
3155 return got_error(GOT_ERR_ITER_COMPLETED);
3158 return NULL; /* no change in this commit */
3160 /* Annotate this line. */
3161 bline = &a->lines[lineno - 1];
3162 if (bline->annotated)
3164 err = got_object_id_str(&bline->id_str, id);
3168 err = got_object_open_as_commit(&commit, a->repo, id);
3172 bline->committer = strdup(got_object_commit_get_committer(commit));
3173 if (bline->committer == NULL) {
3174 err = got_error_from_errno("strdup");
3178 committer_time = got_object_commit_get_committer_time(commit);
3179 if (localtime_r(&committer_time, &tm) == NULL)
3180 return got_error_from_errno("localtime_r");
3181 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3182 &tm) >= sizeof(bline->datebuf)) {
3183 err = got_error(GOT_ERR_NO_SPACE);
3186 bline->annotated = 1;
3188 /* Print lines annotated so far. */
3189 bline = &a->lines[a->lineno_cur - 1];
3190 if (!bline->annotated)
3193 offset = a->line_offsets[a->lineno_cur - 1];
3194 if (fseeko(a->f, offset, SEEK_SET) == -1) {
3195 err = got_error_from_errno("fseeko");
3199 while (bline->annotated) {
3200 char *smallerthan, *at, *nl, *committer;
3203 if (getline(&line, &linesize, a->f) == -1) {
3205 err = got_error_from_errno("getline");
3209 committer = bline->committer;
3210 smallerthan = strchr(committer, '<');
3211 if (smallerthan && smallerthan[1] != '\0')
3212 committer = smallerthan + 1;
3213 at = strchr(committer, '@');
3216 len = strlen(committer);
3218 committer[8] = '\0';
3220 nl = strchr(line, '\n');
3223 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3224 bline->id_str, bline->datebuf, committer, line);
3227 bline = &a->lines[a->lineno_cur - 1];
3231 got_object_commit_close(commit);
3236 static const struct got_error *
3237 cmd_blame(int argc, char *argv[])
3239 const struct got_error *error;
3240 struct got_repository *repo = NULL;
3241 struct got_worktree *worktree = NULL;
3242 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3243 struct got_object_id *obj_id = NULL;
3244 struct got_object_id *commit_id = NULL;
3245 struct got_blob_object *blob = NULL;
3246 char *commit_id_str = NULL;
3247 struct blame_cb_args bca;
3248 int ch, obj_type, i;
3251 memset(&bca, 0, sizeof(bca));
3254 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3259 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3262 commit_id_str = optarg;
3265 repo_path = realpath(optarg, NULL);
3266 if (repo_path == NULL)
3267 return got_error_from_errno2("realpath",
3269 got_path_strip_trailing_slashes(repo_path);
3285 cwd = getcwd(NULL, 0);
3287 error = got_error_from_errno("getcwd");
3290 if (repo_path == NULL) {
3291 error = got_worktree_open(&worktree, cwd);
3292 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3298 strdup(got_worktree_get_repo_path(worktree));
3299 if (repo_path == NULL)
3300 error = got_error_from_errno("strdup");
3304 repo_path = strdup(cwd);
3305 if (repo_path == NULL) {
3306 error = got_error_from_errno("strdup");
3312 error = got_repo_open(&repo, repo_path, NULL);
3316 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3321 const char *prefix = got_worktree_get_path_prefix(worktree);
3322 char *p, *worktree_subdir = cwd +
3323 strlen(got_worktree_get_root_path(worktree));
3324 if (asprintf(&p, "%s%s%s%s%s",
3325 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3326 worktree_subdir, worktree_subdir[0] ? "/" : "",
3328 error = got_error_from_errno("asprintf");
3331 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3334 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3339 if (commit_id_str == NULL) {
3340 struct got_reference *head_ref;
3341 error = got_ref_open(&head_ref, repo, worktree ?
3342 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3345 error = got_ref_resolve(&commit_id, repo, head_ref);
3346 got_ref_close(head_ref);
3350 error = got_repo_match_object_id(&commit_id, NULL,
3351 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3356 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3360 error = got_object_get_type(&obj_type, repo, obj_id);
3364 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3365 error = got_error(GOT_ERR_OBJ_TYPE);
3369 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3372 bca.f = got_opentemp();
3373 if (bca.f == NULL) {
3374 error = got_error_from_errno("got_opentemp");
3377 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3378 &bca.line_offsets, bca.f, blob);
3379 if (error || bca.nlines == 0)
3382 /* Don't include \n at EOF in the blame line count. */
3383 if (bca.line_offsets[bca.nlines - 1] == filesize)
3386 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3387 if (bca.lines == NULL) {
3388 error = got_error_from_errno("calloc");
3392 bca.nlines_prec = 0;
3400 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3401 check_cancelled, NULL);
3409 got_object_blob_close(blob);
3411 got_worktree_close(worktree);
3413 const struct got_error *repo_error;
3414 repo_error = got_repo_close(repo);
3419 for (i = 0; i < bca.nlines; i++) {
3420 struct blame_line *bline = &bca.lines[i];
3421 free(bline->id_str);
3422 free(bline->committer);
3426 free(bca.line_offsets);
3427 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3428 error = got_error_from_errno("fclose");
3436 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3442 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3443 const char *root_path)
3445 int is_root_path = (strcmp(path, root_path) == 0);
3446 const char *modestr = "";
3447 mode_t mode = got_tree_entry_get_mode(te);
3449 path += strlen(root_path);
3450 while (path[0] == '/')
3453 if (got_object_tree_entry_is_submodule(te))
3455 else if (S_ISLNK(mode))
3457 else if (S_ISDIR(mode))
3459 else if (mode & S_IXUSR)
3462 printf("%s%s%s%s%s\n", id ? id : "", path,
3463 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3466 static const struct got_error *
3467 print_tree(const char *path, struct got_object_id *commit_id,
3468 int show_ids, int recurse, const char *root_path,
3469 struct got_repository *repo)
3471 const struct got_error *err = NULL;
3472 struct got_object_id *tree_id = NULL;
3473 struct got_tree_object *tree = NULL;
3476 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3480 err = got_object_open_as_tree(&tree, repo, tree_id);
3483 nentries = got_object_tree_get_nentries(tree);
3484 for (i = 0; i < nentries; i++) {
3485 struct got_tree_entry *te;
3488 if (sigint_received || sigpipe_received)
3491 te = got_object_tree_get_entry(tree, i);
3494 err = got_object_id_str(&id_str,
3495 got_tree_entry_get_id(te));
3498 if (asprintf(&id, "%s ", id_str) == -1) {
3499 err = got_error_from_errno("asprintf");
3505 print_entry(te, id, path, root_path);
3508 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3510 if (asprintf(&child_path, "%s%s%s", path,
3511 path[0] == '/' && path[1] == '\0' ? "" : "/",
3512 got_tree_entry_get_name(te)) == -1) {
3513 err = got_error_from_errno("asprintf");
3516 err = print_tree(child_path, commit_id, show_ids, 1,
3525 got_object_tree_close(tree);
3530 static const struct got_error *
3531 cmd_tree(int argc, char *argv[])
3533 const struct got_error *error;
3534 struct got_repository *repo = NULL;
3535 struct got_worktree *worktree = NULL;
3537 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3538 struct got_object_id *commit_id = NULL;
3539 char *commit_id_str = NULL;
3540 int show_ids = 0, recurse = 0;
3544 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3549 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3552 commit_id_str = optarg;
3555 repo_path = realpath(optarg, NULL);
3556 if (repo_path == NULL)
3557 return got_error_from_errno2("realpath",
3559 got_path_strip_trailing_slashes(repo_path);
3583 cwd = getcwd(NULL, 0);
3585 error = got_error_from_errno("getcwd");
3588 if (repo_path == NULL) {
3589 error = got_worktree_open(&worktree, cwd);
3590 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3596 strdup(got_worktree_get_repo_path(worktree));
3597 if (repo_path == NULL)
3598 error = got_error_from_errno("strdup");
3602 repo_path = strdup(cwd);
3603 if (repo_path == NULL) {
3604 error = got_error_from_errno("strdup");
3610 error = got_repo_open(&repo, repo_path, NULL);
3614 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3620 char *p, *worktree_subdir = cwd +
3621 strlen(got_worktree_get_root_path(worktree));
3622 if (asprintf(&p, "%s/%s",
3623 got_worktree_get_path_prefix(worktree),
3624 worktree_subdir) == -1) {
3625 error = got_error_from_errno("asprintf");
3628 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3635 if (in_repo_path == NULL) {
3636 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3641 if (commit_id_str == NULL) {
3642 struct got_reference *head_ref;
3643 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3646 error = got_ref_resolve(&commit_id, repo, head_ref);
3647 got_ref_close(head_ref);
3651 error = got_repo_match_object_id(&commit_id, NULL,
3652 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3657 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3658 in_repo_path, repo);
3665 got_worktree_close(worktree);
3667 const struct got_error *repo_error;
3668 repo_error = got_repo_close(repo);
3678 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3682 static const struct got_error *
3683 print_status(void *arg, unsigned char status, unsigned char staged_status,
3684 const char *path, struct got_object_id *blob_id,
3685 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3686 int dirfd, const char *de_name)
3688 if (status == staged_status && (status == GOT_STATUS_DELETE))
3689 status = GOT_STATUS_NO_CHANGE;
3690 printf("%c%c %s\n", status, staged_status, path);
3694 static const struct got_error *
3695 cmd_status(int argc, char *argv[])
3697 const struct got_error *error = NULL;
3698 struct got_repository *repo = NULL;
3699 struct got_worktree *worktree = NULL;
3701 struct got_pathlist_head paths;
3702 struct got_pathlist_entry *pe;
3707 while ((ch = getopt(argc, argv, "")) != -1) {
3719 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3723 cwd = getcwd(NULL, 0);
3725 error = got_error_from_errno("getcwd");
3729 error = got_worktree_open(&worktree, cwd);
3733 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3738 error = apply_unveil(got_repo_get_path(repo), 1,
3739 got_worktree_get_root_path(worktree));
3743 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3747 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3748 check_cancelled, NULL);
3750 TAILQ_FOREACH(pe, &paths, entry)
3751 free((char *)pe->path);
3752 got_pathlist_free(&paths);
3761 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3766 static const struct got_error *
3767 list_refs(struct got_repository *repo)
3769 static const struct got_error *err = NULL;
3770 struct got_reflist_head refs;
3771 struct got_reflist_entry *re;
3773 SIMPLEQ_INIT(&refs);
3774 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3778 SIMPLEQ_FOREACH(re, &refs, entry) {
3780 refstr = got_ref_to_str(re->ref);
3782 return got_error_from_errno("got_ref_to_str");
3783 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3787 got_ref_list_free(&refs);
3791 static const struct got_error *
3792 delete_ref(struct got_repository *repo, const char *refname)
3794 const struct got_error *err = NULL;
3795 struct got_reference *ref;
3797 err = got_ref_open(&ref, repo, refname, 0);
3801 err = got_ref_delete(ref, repo);
3806 static const struct got_error *
3807 add_ref(struct got_repository *repo, const char *refname, const char *target)
3809 const struct got_error *err = NULL;
3810 struct got_object_id *id;
3811 struct got_reference *ref = NULL;
3814 * Don't let the user create a reference name with a leading '-'.
3815 * While technically a valid reference name, this case is usually
3816 * an unintended typo.
3818 if (refname[0] == '-')
3819 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3821 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3824 struct got_reference *target_ref;
3826 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3828 err = got_ref_open(&target_ref, repo, target, 0);
3831 err = got_ref_resolve(&id, repo, target_ref);
3832 got_ref_close(target_ref);
3837 err = got_ref_alloc(&ref, refname, id);
3841 err = got_ref_write(ref, repo);
3849 static const struct got_error *
3850 add_symref(struct got_repository *repo, const char *refname, const char *target)
3852 const struct got_error *err = NULL;
3853 struct got_reference *ref = NULL;
3854 struct got_reference *target_ref = NULL;
3857 * Don't let the user create a reference name with a leading '-'.
3858 * While technically a valid reference name, this case is usually
3859 * an unintended typo.
3861 if (refname[0] == '-')
3862 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3864 err = got_ref_open(&target_ref, repo, target, 0);
3868 err = got_ref_alloc_symref(&ref, refname, target_ref);
3872 err = got_ref_write(ref, repo);
3875 got_ref_close(target_ref);
3881 static const struct got_error *
3882 cmd_ref(int argc, char *argv[])
3884 const struct got_error *error = NULL;
3885 struct got_repository *repo = NULL;
3886 struct got_worktree *worktree = NULL;
3887 char *cwd = NULL, *repo_path = NULL;
3888 int ch, do_list = 0, create_symref = 0;
3889 const char *delref = NULL;
3891 /* TODO: Add -s option for adding symbolic references. */
3892 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3898 repo_path = realpath(optarg, NULL);
3899 if (repo_path == NULL)
3900 return got_error_from_errno2("realpath",
3902 got_path_strip_trailing_slashes(repo_path);
3916 if (do_list && delref)
3917 errx(1, "-l and -d options are mutually exclusive\n");
3922 if (do_list || delref) {
3924 errx(1, "-s option cannot be used together with the "
3925 "-l or -d options");
3928 } else if (argc != 2)
3933 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3937 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3938 "sendfd unveil", NULL) == -1)
3942 cwd = getcwd(NULL, 0);
3944 error = got_error_from_errno("getcwd");
3948 if (repo_path == NULL) {
3949 error = got_worktree_open(&worktree, cwd);
3950 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3956 strdup(got_worktree_get_repo_path(worktree));
3957 if (repo_path == NULL)
3958 error = got_error_from_errno("strdup");
3962 repo_path = strdup(cwd);
3963 if (repo_path == NULL) {
3964 error = got_error_from_errno("strdup");
3970 error = got_repo_open(&repo, repo_path, NULL);
3974 error = apply_unveil(got_repo_get_path(repo), do_list,
3975 worktree ? got_worktree_get_root_path(worktree) : NULL);
3980 error = list_refs(repo);
3982 error = delete_ref(repo, delref);
3983 else if (create_symref)
3984 error = add_symref(repo, argv[0], argv[1]);
3986 error = add_ref(repo, argv[0], argv[1]);
3989 got_repo_close(repo);
3991 got_worktree_close(worktree);
4001 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4002 "[name]\n", getprogname());
4006 static const struct got_error *
4007 list_branch(struct got_repository *repo, struct got_worktree *worktree,
4008 struct got_reference *ref)
4010 const struct got_error *err = NULL;
4011 const char *refname, *marker = " ";
4014 refname = got_ref_get_name(ref);
4015 if (worktree && strcmp(refname,
4016 got_worktree_get_head_ref_name(worktree)) == 0) {
4017 struct got_object_id *id = NULL;
4019 err = got_ref_resolve(&id, repo, ref);
4022 if (got_object_id_cmp(id,
4023 got_worktree_get_base_commit_id(worktree)) == 0)
4030 if (strncmp(refname, "refs/heads/", 11) == 0)
4032 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4035 refstr = got_ref_to_str(ref);
4037 return got_error_from_errno("got_ref_to_str");
4039 printf("%s%s: %s\n", marker, refname, refstr);
4044 static const struct got_error *
4045 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4047 const char *refname;
4049 if (worktree == NULL)
4050 return got_error(GOT_ERR_NOT_WORKTREE);
4052 refname = got_worktree_get_head_ref_name(worktree);
4054 if (strncmp(refname, "refs/heads/", 11) == 0)
4056 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4059 printf("%s\n", refname);
4064 static const struct got_error *
4065 list_branches(struct got_repository *repo, struct got_worktree *worktree)
4067 static const struct got_error *err = NULL;
4068 struct got_reflist_head refs;
4069 struct got_reflist_entry *re;
4070 struct got_reference *temp_ref = NULL;
4071 int rebase_in_progress, histedit_in_progress;
4073 SIMPLEQ_INIT(&refs);
4076 err = got_worktree_rebase_in_progress(&rebase_in_progress,
4081 err = got_worktree_histedit_in_progress(&histedit_in_progress,
4086 if (rebase_in_progress || histedit_in_progress) {
4087 err = got_ref_open(&temp_ref, repo,
4088 got_worktree_get_head_ref_name(worktree), 0);
4091 list_branch(repo, worktree, temp_ref);
4092 got_ref_close(temp_ref);
4096 err = got_ref_list(&refs, repo, "refs/heads",
4097 got_ref_cmp_by_name, NULL);
4101 SIMPLEQ_FOREACH(re, &refs, entry)
4102 list_branch(repo, worktree, re->ref);
4104 got_ref_list_free(&refs);
4108 static const struct got_error *
4109 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4110 const char *branch_name)
4112 const struct got_error *err = NULL;
4113 struct got_reference *ref = NULL;
4116 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4117 return got_error_from_errno("asprintf");
4119 err = got_ref_open(&ref, repo, refname, 0);
4124 strcmp(got_worktree_get_head_ref_name(worktree),
4125 got_ref_get_name(ref)) == 0) {
4126 err = got_error_msg(GOT_ERR_SAME_BRANCH,
4127 "will not delete this work tree's current branch");
4131 err = got_ref_delete(ref, repo);
4139 static const struct got_error *
4140 add_branch(struct got_repository *repo, const char *branch_name,
4141 struct got_object_id *base_commit_id)
4143 const struct got_error *err = NULL;
4144 struct got_reference *ref = NULL;
4145 char *base_refname = NULL, *refname = NULL;
4148 * Don't let the user create a branch name with a leading '-'.
4149 * While technically a valid reference name, this case is usually
4150 * an unintended typo.
4152 if (branch_name[0] == '-')
4153 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4155 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4156 err = got_error_from_errno("asprintf");
4160 err = got_ref_open(&ref, repo, refname, 0);
4162 err = got_error(GOT_ERR_BRANCH_EXISTS);
4164 } else if (err->code != GOT_ERR_NOT_REF)
4167 err = got_ref_alloc(&ref, refname, base_commit_id);
4171 err = got_ref_write(ref, repo);
4180 static const struct got_error *
4181 cmd_branch(int argc, char *argv[])
4183 const struct got_error *error = NULL;
4184 struct got_repository *repo = NULL;
4185 struct got_worktree *worktree = NULL;
4186 char *cwd = NULL, *repo_path = NULL;
4187 int ch, do_list = 0, do_show = 0, do_update = 1;
4188 const char *delref = NULL, *commit_id_arg = NULL;
4189 struct got_reference *ref = NULL;
4190 struct got_pathlist_head paths;
4191 struct got_pathlist_entry *pe;
4192 struct got_object_id *commit_id = NULL;
4193 char *commit_id_str = NULL;
4197 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4200 commit_id_arg = optarg;
4206 repo_path = realpath(optarg, NULL);
4207 if (repo_path == NULL)
4208 return got_error_from_errno2("realpath",
4210 got_path_strip_trailing_slashes(repo_path);
4224 if (do_list && delref)
4225 errx(1, "-l and -d options are mutually exclusive\n");
4230 if (!do_list && !delref && argc == 0)
4233 if ((do_list || delref || do_show) && commit_id_arg != NULL)
4234 errx(1, "-c option can only be used when creating a branch");
4236 if (do_list || delref) {
4239 } else if (!do_show && argc != 1)
4243 if (do_list || do_show) {
4244 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4248 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4249 "sendfd unveil", NULL) == -1)
4253 cwd = getcwd(NULL, 0);
4255 error = got_error_from_errno("getcwd");
4259 if (repo_path == NULL) {
4260 error = got_worktree_open(&worktree, cwd);
4261 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4267 strdup(got_worktree_get_repo_path(worktree));
4268 if (repo_path == NULL)
4269 error = got_error_from_errno("strdup");
4273 repo_path = strdup(cwd);
4274 if (repo_path == NULL) {
4275 error = got_error_from_errno("strdup");
4281 error = got_repo_open(&repo, repo_path, NULL);
4285 error = apply_unveil(got_repo_get_path(repo), do_list,
4286 worktree ? got_worktree_get_root_path(worktree) : NULL);
4291 error = show_current_branch(repo, worktree);
4293 error = list_branches(repo, worktree);
4295 error = delete_branch(repo, worktree, delref);
4297 if (commit_id_arg == NULL)
4298 commit_id_arg = worktree ?
4299 got_worktree_get_head_ref_name(worktree) :
4301 error = got_repo_match_object_id(&commit_id, NULL,
4302 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4305 error = add_branch(repo, argv[0], commit_id);
4308 if (worktree && do_update) {
4309 int did_something = 0;
4310 char *branch_refname = NULL;
4312 error = got_object_id_str(&commit_id_str, commit_id);
4315 error = get_worktree_paths_from_argv(&paths, 0, NULL,
4319 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4321 error = got_error_from_errno("asprintf");
4324 error = got_ref_open(&ref, repo, branch_refname, 0);
4325 free(branch_refname);
4328 error = switch_head_ref(ref, commit_id, worktree,
4332 error = got_worktree_set_base_commit_id(worktree, repo,
4336 error = got_worktree_checkout_files(worktree, &paths,
4337 repo, update_progress, &did_something,
4338 check_cancelled, NULL);
4342 printf("Updated to commit %s\n", commit_id_str);
4349 got_repo_close(repo);
4351 got_worktree_close(worktree);
4355 free(commit_id_str);
4356 TAILQ_FOREACH(pe, &paths, entry)
4357 free((char *)pe->path);
4358 got_pathlist_free(&paths);
4367 "usage: %s tag [-c commit] [-r repository] [-l] "
4368 "[-m message] name\n", getprogname());
4373 static const struct got_error *
4374 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4376 const struct got_error *err = NULL;
4377 struct got_reflist_entry *re, *se, *new;
4378 struct got_object_id *re_id, *se_id;
4379 struct got_tag_object *re_tag, *se_tag;
4380 time_t re_time, se_time;
4382 SIMPLEQ_FOREACH(re, tags, entry) {
4383 se = SIMPLEQ_FIRST(sorted);
4385 err = got_reflist_entry_dup(&new, re);
4388 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4391 err = got_ref_resolve(&re_id, repo, re->ref);
4394 err = got_object_open_as_tag(&re_tag, repo, re_id);
4398 re_time = got_object_tag_get_tagger_time(re_tag);
4399 got_object_tag_close(re_tag);
4403 err = got_ref_resolve(&se_id, repo, re->ref);
4406 err = got_object_open_as_tag(&se_tag, repo, se_id);
4410 se_time = got_object_tag_get_tagger_time(se_tag);
4411 got_object_tag_close(se_tag);
4413 if (se_time > re_time) {
4414 err = got_reflist_entry_dup(&new, re);
4417 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4420 se = SIMPLEQ_NEXT(se, entry);
4429 static const struct got_error *
4430 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4432 static const struct got_error *err = NULL;
4433 struct got_reflist_head refs;
4434 struct got_reflist_entry *re;
4436 SIMPLEQ_INIT(&refs);
4438 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4442 SIMPLEQ_FOREACH(re, &refs, entry) {
4443 const char *refname;
4444 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4448 struct got_object_id *id;
4449 struct got_tag_object *tag;
4450 struct got_commit_object *commit = NULL;
4452 refname = got_ref_get_name(re->ref);
4453 if (strncmp(refname, "refs/tags/", 10) != 0)
4456 refstr = got_ref_to_str(re->ref);
4457 if (refstr == NULL) {
4458 err = got_error_from_errno("got_ref_to_str");
4461 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4464 err = got_ref_resolve(&id, repo, re->ref);
4467 err = got_object_open_as_tag(&tag, repo, id);
4469 if (err->code != GOT_ERR_OBJ_TYPE) {
4473 /* "lightweight" tag */
4474 err = got_object_open_as_commit(&commit, repo, id);
4479 tagger = got_object_commit_get_committer(commit);
4481 got_object_commit_get_committer_time(commit);
4482 err = got_object_id_str(&id_str, id);
4488 tagger = got_object_tag_get_tagger(tag);
4489 tagger_time = got_object_tag_get_tagger_time(tag);
4490 err = got_object_id_str(&id_str,
4491 got_object_tag_get_object_id(tag));
4495 printf("from: %s\n", tagger);
4496 datestr = get_datestr(&tagger_time, datebuf);
4498 printf("date: %s UTC\n", datestr);
4500 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4502 switch (got_object_tag_get_object_type(tag)) {
4503 case GOT_OBJ_TYPE_BLOB:
4504 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4507 case GOT_OBJ_TYPE_TREE:
4508 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4511 case GOT_OBJ_TYPE_COMMIT:
4512 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4515 case GOT_OBJ_TYPE_TAG:
4516 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4525 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4528 got_object_commit_close(commit);
4530 tagmsg0 = strdup(got_object_tag_get_message(tag));
4531 got_object_tag_close(tag);
4532 if (tagmsg0 == NULL) {
4533 err = got_error_from_errno("strdup");
4540 line = strsep(&tagmsg, "\n");
4542 printf(" %s\n", line);
4547 got_ref_list_free(&refs);
4551 static const struct got_error *
4552 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4553 const char *tag_name, const char *repo_path)
4555 const struct got_error *err = NULL;
4556 char *template = NULL, *initial_content = NULL;
4557 char *editor = NULL;
4560 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4561 err = got_error_from_errno("asprintf");
4565 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4566 commit_id_str, tag_name) == -1) {
4567 err = got_error_from_errno("asprintf");
4571 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4575 dprintf(fd, initial_content);
4578 err = get_editor(&editor);
4581 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4583 free(initial_content);
4587 /* Editor is done; we can now apply unveil(2) */
4589 err = apply_unveil(repo_path, 0, NULL);
4598 static const struct got_error *
4599 add_tag(struct got_repository *repo, const char *tag_name,
4600 const char *commit_arg, const char *tagmsg_arg)
4602 const struct got_error *err = NULL;
4603 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4604 char *label = NULL, *commit_id_str = NULL;
4605 struct got_reference *ref = NULL;
4606 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4607 char *tagmsg_path = NULL, *tag_id_str = NULL;
4608 int preserve_tagmsg = 0;
4611 * Don't let the user create a tag name with a leading '-'.
4612 * While technically a valid reference name, this case is usually
4613 * an unintended typo.
4615 if (tag_name[0] == '-')
4616 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4618 err = get_author(&tagger, repo);
4622 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4623 GOT_OBJ_TYPE_COMMIT, 1, repo);
4627 err = got_object_id_str(&commit_id_str, commit_id);
4631 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4632 refname = strdup(tag_name);
4633 if (refname == NULL) {
4634 err = got_error_from_errno("strdup");
4638 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4639 err = got_error_from_errno("asprintf");
4643 err = got_ref_open(&ref, repo, refname, 0);
4645 err = got_error(GOT_ERR_TAG_EXISTS);
4647 } else if (err->code != GOT_ERR_NOT_REF)
4650 if (tagmsg_arg == NULL) {
4651 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4652 tag_name, got_repo_get_path(repo));
4654 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4655 tagmsg_path != NULL)
4656 preserve_tagmsg = 1;
4661 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4662 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4665 preserve_tagmsg = 1;
4669 err = got_ref_alloc(&ref, refname, tag_id);
4672 preserve_tagmsg = 1;
4676 err = got_ref_write(ref, repo);
4679 preserve_tagmsg = 1;
4683 err = got_object_id_str(&tag_id_str, tag_id);
4686 preserve_tagmsg = 1;
4689 printf("Created tag %s\n", tag_id_str);
4691 if (preserve_tagmsg) {
4692 fprintf(stderr, "%s: tag message preserved in %s\n",
4693 getprogname(), tagmsg_path);
4694 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4695 err = got_error_from_errno2("unlink", tagmsg_path);
4700 free(commit_id_str);
4708 static const struct got_error *
4709 cmd_tag(int argc, char *argv[])
4711 const struct got_error *error = NULL;
4712 struct got_repository *repo = NULL;
4713 struct got_worktree *worktree = NULL;
4714 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4715 char *gitconfig_path = NULL;
4716 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4717 int ch, do_list = 0;
4719 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4722 commit_id_arg = optarg;
4728 repo_path = realpath(optarg, NULL);
4729 if (repo_path == NULL)
4730 return got_error_from_errno2("realpath",
4732 got_path_strip_trailing_slashes(repo_path);
4747 if (commit_id_arg != NULL)
4748 errx(1, "-c option can only be used when creating a tag");
4750 errx(1, "-l and -m options are mutually exclusive");
4753 } else if (argc != 1)
4760 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4764 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4765 "sendfd unveil", NULL) == -1)
4769 cwd = getcwd(NULL, 0);
4771 error = got_error_from_errno("getcwd");
4775 if (repo_path == NULL) {
4776 error = got_worktree_open(&worktree, cwd);
4777 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4783 strdup(got_worktree_get_repo_path(worktree));
4784 if (repo_path == NULL)
4785 error = got_error_from_errno("strdup");
4789 repo_path = strdup(cwd);
4790 if (repo_path == NULL) {
4791 error = got_error_from_errno("strdup");
4798 error = got_repo_open(&repo, repo_path, NULL);
4801 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4804 error = list_tags(repo, worktree);
4806 error = get_gitconfig_path(&gitconfig_path);
4809 error = got_repo_open(&repo, repo_path, gitconfig_path);
4814 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4819 if (commit_id_arg == NULL) {
4820 struct got_reference *head_ref;
4821 struct got_object_id *commit_id;
4822 error = got_ref_open(&head_ref, repo,
4823 worktree ? got_worktree_get_head_ref_name(worktree)
4827 error = got_ref_resolve(&commit_id, repo, head_ref);
4828 got_ref_close(head_ref);
4831 error = got_object_id_str(&commit_id_str, commit_id);
4837 error = add_tag(repo, tag_name,
4838 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4842 got_repo_close(repo);
4844 got_worktree_close(worktree);
4847 free(gitconfig_path);
4848 free(commit_id_str);
4855 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4860 static const struct got_error *
4861 add_progress(void *arg, unsigned char status, const char *path)
4863 while (path[0] == '/')
4865 printf("%c %s\n", status, path);
4869 static const struct got_error *
4870 cmd_add(int argc, char *argv[])
4872 const struct got_error *error = NULL;
4873 struct got_repository *repo = NULL;
4874 struct got_worktree *worktree = NULL;
4876 struct got_pathlist_head paths;
4877 struct got_pathlist_entry *pe;
4878 int ch, can_recurse = 0, no_ignores = 0;
4882 while ((ch = getopt(argc, argv, "IR")) != -1) {
4900 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4907 cwd = getcwd(NULL, 0);
4909 error = got_error_from_errno("getcwd");
4913 error = got_worktree_open(&worktree, cwd);
4917 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4922 error = apply_unveil(got_repo_get_path(repo), 1,
4923 got_worktree_get_root_path(worktree));
4927 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4931 if (!can_recurse && no_ignores) {
4932 error = got_error_msg(GOT_ERR_BAD_PATH,
4933 "disregarding ignores requires -R option");
4941 TAILQ_FOREACH(pe, &paths, entry) {
4942 if (asprintf(&ondisk_path, "%s/%s",
4943 got_worktree_get_root_path(worktree),
4945 error = got_error_from_errno("asprintf");
4948 if (lstat(ondisk_path, &sb) == -1) {
4949 if (errno == ENOENT) {
4953 error = got_error_from_errno2("lstat",
4959 if (S_ISDIR(sb.st_mode)) {
4960 error = got_error_msg(GOT_ERR_BAD_PATH,
4961 "adding directories requires -R option");
4967 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4968 NULL, repo, no_ignores);
4971 got_repo_close(repo);
4973 got_worktree_close(worktree);
4974 TAILQ_FOREACH(pe, &paths, entry)
4975 free((char *)pe->path);
4976 got_pathlist_free(&paths);
4984 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4989 static const struct got_error *
4990 print_remove_status(void *arg, unsigned char status,
4991 unsigned char staged_status, const char *path)
4993 while (path[0] == '/')
4995 if (status == GOT_STATUS_NONEXISTENT)
4997 if (status == staged_status && (status == GOT_STATUS_DELETE))
4998 status = GOT_STATUS_NO_CHANGE;
4999 printf("%c%c %s\n", status, staged_status, path);
5003 static const struct got_error *
5004 cmd_remove(int argc, char *argv[])
5006 const struct got_error *error = NULL;
5007 struct got_worktree *worktree = NULL;
5008 struct got_repository *repo = NULL;
5010 struct got_pathlist_head paths;
5011 struct got_pathlist_entry *pe;
5012 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5016 while ((ch = getopt(argc, argv, "fkR")) != -1) {
5019 delete_local_mods = 1;
5037 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5044 cwd = getcwd(NULL, 0);
5046 error = got_error_from_errno("getcwd");
5049 error = got_worktree_open(&worktree, cwd);
5053 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5058 error = apply_unveil(got_repo_get_path(repo), 1,
5059 got_worktree_get_root_path(worktree));
5063 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5070 TAILQ_FOREACH(pe, &paths, entry) {
5071 if (asprintf(&ondisk_path, "%s/%s",
5072 got_worktree_get_root_path(worktree),
5074 error = got_error_from_errno("asprintf");
5077 if (lstat(ondisk_path, &sb) == -1) {
5078 if (errno == ENOENT) {
5082 error = got_error_from_errno2("lstat",
5088 if (S_ISDIR(sb.st_mode)) {
5089 error = got_error_msg(GOT_ERR_BAD_PATH,
5090 "removing directories requires -R option");
5096 error = got_worktree_schedule_delete(worktree, &paths,
5097 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5100 got_repo_close(repo);
5102 got_worktree_close(worktree);
5103 TAILQ_FOREACH(pe, &paths, entry)
5104 free((char *)pe->path);
5105 got_pathlist_free(&paths);
5113 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5114 "path ...\n", getprogname());
5118 static const struct got_error *
5119 revert_progress(void *arg, unsigned char status, const char *path)
5121 if (status == GOT_STATUS_UNVERSIONED)
5124 while (path[0] == '/')
5126 printf("%c %s\n", status, path);
5130 struct choose_patch_arg {
5131 FILE *patch_script_file;
5135 static const struct got_error *
5136 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5137 int nchanges, const char *action)
5140 size_t linesize = 0;
5144 case GOT_STATUS_ADD:
5145 printf("A %s\n%s this addition? [y/n] ", path, action);
5147 case GOT_STATUS_DELETE:
5148 printf("D %s\n%s this deletion? [y/n] ", path, action);
5150 case GOT_STATUS_MODIFY:
5151 if (fseek(patch_file, 0L, SEEK_SET) == -1)
5152 return got_error_from_errno("fseek");
5153 printf(GOT_COMMIT_SEP_STR);
5154 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5156 if (ferror(patch_file))
5157 return got_error_from_errno("getline");
5158 printf(GOT_COMMIT_SEP_STR);
5159 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5160 path, n, nchanges, action);
5163 return got_error_path(path, GOT_ERR_FILE_STATUS);
5169 static const struct got_error *
5170 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5171 FILE *patch_file, int n, int nchanges)
5173 const struct got_error *err = NULL;
5175 size_t linesize = 0;
5178 struct choose_patch_arg *a = arg;
5180 *choice = GOT_PATCH_CHOICE_NONE;
5182 if (a->patch_script_file) {
5184 err = show_change(status, path, patch_file, n, nchanges,
5188 linelen = getline(&line, &linesize, a->patch_script_file);
5189 if (linelen == -1) {
5190 if (ferror(a->patch_script_file))
5191 return got_error_from_errno("getline");
5194 nl = strchr(line, '\n');
5197 if (strcmp(line, "y") == 0) {
5198 *choice = GOT_PATCH_CHOICE_YES;
5200 } else if (strcmp(line, "n") == 0) {
5201 *choice = GOT_PATCH_CHOICE_NO;
5203 } else if (strcmp(line, "q") == 0 &&
5204 status == GOT_STATUS_MODIFY) {
5205 *choice = GOT_PATCH_CHOICE_QUIT;
5208 printf("invalid response '%s'\n", line);
5213 while (resp != 'y' && resp != 'n' && resp != 'q') {
5214 err = show_change(status, path, patch_file, n, nchanges,
5221 if (status == GOT_STATUS_MODIFY) {
5222 if (resp != 'y' && resp != 'n' && resp != 'q') {
5223 printf("invalid response '%c'\n", resp);
5226 } else if (resp != 'y' && resp != 'n') {
5227 printf("invalid response '%c'\n", resp);
5233 *choice = GOT_PATCH_CHOICE_YES;
5234 else if (resp == 'n')
5235 *choice = GOT_PATCH_CHOICE_NO;
5236 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5237 *choice = GOT_PATCH_CHOICE_QUIT;
5243 static const struct got_error *
5244 cmd_revert(int argc, char *argv[])
5246 const struct got_error *error = NULL;
5247 struct got_worktree *worktree = NULL;
5248 struct got_repository *repo = NULL;
5249 char *cwd = NULL, *path = NULL;
5250 struct got_pathlist_head paths;
5251 struct got_pathlist_entry *pe;
5252 int ch, can_recurse = 0, pflag = 0;
5253 FILE *patch_script_file = NULL;
5254 const char *patch_script_path = NULL;
5255 struct choose_patch_arg cpa;
5259 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5265 patch_script_path = optarg;
5280 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5281 "unveil", NULL) == -1)
5286 if (patch_script_path && !pflag)
5287 errx(1, "-F option can only be used together with -p option");
5289 cwd = getcwd(NULL, 0);
5291 error = got_error_from_errno("getcwd");
5294 error = got_worktree_open(&worktree, cwd);
5298 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5303 if (patch_script_path) {
5304 patch_script_file = fopen(patch_script_path, "r");
5305 if (patch_script_file == NULL) {
5306 error = got_error_from_errno2("fopen",
5311 error = apply_unveil(got_repo_get_path(repo), 1,
5312 got_worktree_get_root_path(worktree));
5316 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5323 TAILQ_FOREACH(pe, &paths, entry) {
5324 if (asprintf(&ondisk_path, "%s/%s",
5325 got_worktree_get_root_path(worktree),
5327 error = got_error_from_errno("asprintf");
5330 if (lstat(ondisk_path, &sb) == -1) {
5331 if (errno == ENOENT) {
5335 error = got_error_from_errno2("lstat",
5341 if (S_ISDIR(sb.st_mode)) {
5342 error = got_error_msg(GOT_ERR_BAD_PATH,
5343 "reverting directories requires -R option");
5349 cpa.patch_script_file = patch_script_file;
5350 cpa.action = "revert";
5351 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5352 pflag ? choose_patch : NULL, &cpa, repo);
5354 if (patch_script_file && fclose(patch_script_file) == EOF &&
5356 error = got_error_from_errno2("fclose", patch_script_path);
5358 got_repo_close(repo);
5360 got_worktree_close(worktree);
5369 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5374 struct collect_commit_logmsg_arg {
5375 const char *cmdline_log;
5377 const char *worktree_path;
5378 const char *branch_name;
5379 const char *repo_path;
5384 static const struct got_error *
5385 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5388 char *initial_content = NULL;
5389 struct got_pathlist_entry *pe;
5390 const struct got_error *err = NULL;
5391 char *template = NULL;
5392 struct collect_commit_logmsg_arg *a = arg;
5396 /* if a message was specified on the command line, just use it */
5397 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5398 len = strlen(a->cmdline_log) + 1;
5399 *logmsg = malloc(len + 1);
5400 if (*logmsg == NULL)
5401 return got_error_from_errno("malloc");
5402 strlcpy(*logmsg, a->cmdline_log, len);
5406 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5407 return got_error_from_errno("asprintf");
5409 if (asprintf(&initial_content,
5410 "\n# changes to be committed on branch %s:\n",
5411 a->branch_name) == -1)
5412 return got_error_from_errno("asprintf");
5414 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5418 dprintf(fd, initial_content);
5420 TAILQ_FOREACH(pe, commitable_paths, entry) {
5421 struct got_commitable *ct = pe->data;
5422 dprintf(fd, "# %c %s\n",
5423 got_commitable_get_status(ct),
5424 got_commitable_get_path(ct));
5428 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5430 free(initial_content);
5433 /* Editor is done; we can now apply unveil(2) */
5435 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5444 static const struct got_error *
5445 cmd_commit(int argc, char *argv[])
5447 const struct got_error *error = NULL;
5448 struct got_worktree *worktree = NULL;
5449 struct got_repository *repo = NULL;
5450 char *cwd = NULL, *id_str = NULL;
5451 struct got_object_id *id = NULL;
5452 const char *logmsg = NULL;
5453 struct collect_commit_logmsg_arg cl_arg;
5454 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5455 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5456 struct got_pathlist_head paths;
5459 cl_arg.logmsg_path = NULL;
5461 while ((ch = getopt(argc, argv, "m:")) != -1) {
5476 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5477 "unveil", NULL) == -1)
5480 cwd = getcwd(NULL, 0);
5482 error = got_error_from_errno("getcwd");
5485 error = got_worktree_open(&worktree, cwd);
5489 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5492 if (rebase_in_progress) {
5493 error = got_error(GOT_ERR_REBASING);
5497 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5502 error = get_gitconfig_path(&gitconfig_path);
5505 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5510 error = get_author(&author, repo);
5515 * unveil(2) traverses exec(2); if an editor is used we have
5516 * to apply unveil after the log message has been written.
5518 if (logmsg == NULL || strlen(logmsg) == 0)
5519 error = get_editor(&editor);
5521 error = apply_unveil(got_repo_get_path(repo), 0,
5522 got_worktree_get_root_path(worktree));
5526 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5530 cl_arg.editor = editor;
5531 cl_arg.cmdline_log = logmsg;
5532 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5533 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5534 if (!histedit_in_progress) {
5535 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5536 error = got_error(GOT_ERR_COMMIT_BRANCH);
5539 cl_arg.branch_name += 11;
5541 cl_arg.repo_path = got_repo_get_path(repo);
5542 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5543 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5545 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5546 cl_arg.logmsg_path != NULL)
5547 preserve_logmsg = 1;
5551 error = got_object_id_str(&id_str, id);
5554 printf("Created commit %s\n", id_str);
5556 if (preserve_logmsg) {
5557 fprintf(stderr, "%s: log message preserved in %s\n",
5558 getprogname(), cl_arg.logmsg_path);
5559 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5561 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5562 free(cl_arg.logmsg_path);
5564 got_repo_close(repo);
5566 got_worktree_close(worktree);
5569 free(gitconfig_path);
5576 usage_cherrypick(void)
5578 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5582 static const struct got_error *
5583 cmd_cherrypick(int argc, char *argv[])
5585 const struct got_error *error = NULL;
5586 struct got_worktree *worktree = NULL;
5587 struct got_repository *repo = NULL;
5588 char *cwd = NULL, *commit_id_str = NULL;
5589 struct got_object_id *commit_id = NULL;
5590 struct got_commit_object *commit = NULL;
5591 struct got_object_qid *pid;
5592 struct got_reference *head_ref = NULL;
5593 int ch, did_something = 0;
5595 while ((ch = getopt(argc, argv, "")) != -1) {
5607 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5608 "unveil", NULL) == -1)
5614 cwd = getcwd(NULL, 0);
5616 error = got_error_from_errno("getcwd");
5619 error = got_worktree_open(&worktree, cwd);
5623 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5628 error = apply_unveil(got_repo_get_path(repo), 0,
5629 got_worktree_get_root_path(worktree));
5633 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5634 GOT_OBJ_TYPE_COMMIT, repo);
5635 if (error != NULL) {
5636 struct got_reference *ref;
5637 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5639 error = got_ref_open(&ref, repo, argv[0], 0);
5642 error = got_ref_resolve(&commit_id, repo, ref);
5647 error = got_object_id_str(&commit_id_str, commit_id);
5651 error = got_ref_open(&head_ref, repo,
5652 got_worktree_get_head_ref_name(worktree), 0);
5656 error = check_same_branch(commit_id, head_ref, NULL, repo);
5658 if (error->code != GOT_ERR_ANCESTRY)
5662 error = got_error(GOT_ERR_SAME_BRANCH);
5666 error = got_object_open_as_commit(&commit, repo, commit_id);
5669 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5670 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5671 commit_id, repo, update_progress, &did_something, check_cancelled,
5677 printf("Merged commit %s\n", commit_id_str);
5680 got_object_commit_close(commit);
5681 free(commit_id_str);
5683 got_ref_close(head_ref);
5685 got_worktree_close(worktree);
5687 got_repo_close(repo);
5694 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5698 static const struct got_error *
5699 cmd_backout(int argc, char *argv[])
5701 const struct got_error *error = NULL;
5702 struct got_worktree *worktree = NULL;
5703 struct got_repository *repo = NULL;
5704 char *cwd = NULL, *commit_id_str = NULL;
5705 struct got_object_id *commit_id = NULL;
5706 struct got_commit_object *commit = NULL;
5707 struct got_object_qid *pid;
5708 struct got_reference *head_ref = NULL;
5709 int ch, did_something = 0;
5711 while ((ch = getopt(argc, argv, "")) != -1) {
5723 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5724 "unveil", NULL) == -1)
5730 cwd = getcwd(NULL, 0);
5732 error = got_error_from_errno("getcwd");
5735 error = got_worktree_open(&worktree, cwd);
5739 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5744 error = apply_unveil(got_repo_get_path(repo), 0,
5745 got_worktree_get_root_path(worktree));
5749 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5750 GOT_OBJ_TYPE_COMMIT, repo);
5751 if (error != NULL) {
5752 struct got_reference *ref;
5753 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5755 error = got_ref_open(&ref, repo, argv[0], 0);
5758 error = got_ref_resolve(&commit_id, repo, ref);
5763 error = got_object_id_str(&commit_id_str, commit_id);
5767 error = got_ref_open(&head_ref, repo,
5768 got_worktree_get_head_ref_name(worktree), 0);
5772 error = check_same_branch(commit_id, head_ref, NULL, repo);
5776 error = got_object_open_as_commit(&commit, repo, commit_id);
5779 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5781 error = got_error(GOT_ERR_ROOT_COMMIT);
5785 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5786 update_progress, &did_something, check_cancelled, NULL);
5791 printf("Backed out commit %s\n", commit_id_str);
5794 got_object_commit_close(commit);
5795 free(commit_id_str);
5797 got_ref_close(head_ref);
5799 got_worktree_close(worktree);
5801 got_repo_close(repo);
5808 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5814 trim_logmsg(char *logmsg, int limit)
5819 len = strlen(logmsg);
5823 nl = strchr(logmsg, '\n');
5828 static const struct got_error *
5829 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5831 const struct got_error *err;
5832 char *logmsg0 = NULL;
5835 err = got_object_commit_get_logmsg(&logmsg0, commit);
5840 while (isspace((unsigned char)s[0]))
5843 *logmsg = strdup(s);
5844 if (*logmsg == NULL) {
5845 err = got_error_from_errno("strdup");
5849 trim_logmsg(*logmsg, limit);
5855 static const struct got_error *
5856 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5858 const struct got_error *err;
5859 struct got_commit_object *commit = NULL;
5860 char *id_str = NULL, *logmsg = NULL;
5862 err = got_object_open_as_commit(&commit, repo, id);
5866 err = got_object_id_str(&id_str, id);
5872 err = get_short_logmsg(&logmsg, 42, commit);
5876 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5879 got_object_commit_close(commit);
5884 static const struct got_error *
5885 show_rebase_progress(struct got_commit_object *commit,
5886 struct got_object_id *old_id, struct got_object_id *new_id)
5888 const struct got_error *err;
5889 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5891 err = got_object_id_str(&old_id_str, old_id);
5896 err = got_object_id_str(&new_id_str, new_id);
5901 old_id_str[12] = '\0';
5903 new_id_str[12] = '\0';
5905 err = get_short_logmsg(&logmsg, 42, commit);
5909 printf("%s -> %s: %s\n", old_id_str,
5910 new_id_str ? new_id_str : "no-op change", logmsg);
5918 static const struct got_error *
5919 rebase_progress(void *arg, unsigned char status, const char *path)
5921 unsigned char *rebase_status = arg;
5923 while (path[0] == '/')
5925 printf("%c %s\n", status, path);
5927 if (*rebase_status == GOT_STATUS_CONFLICT)
5929 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5930 *rebase_status = status;
5934 static const struct got_error *
5935 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5936 struct got_reference *branch, struct got_reference *new_base_branch,
5937 struct got_reference *tmp_branch, struct got_repository *repo)
5939 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5940 return got_worktree_rebase_complete(worktree, fileindex,
5941 new_base_branch, tmp_branch, branch, repo);
5944 static const struct got_error *
5945 rebase_commit(struct got_pathlist_head *merged_paths,
5946 struct got_worktree *worktree, struct got_fileindex *fileindex,
5947 struct got_reference *tmp_branch,
5948 struct got_object_id *commit_id, struct got_repository *repo)
5950 const struct got_error *error;
5951 struct got_commit_object *commit;
5952 struct got_object_id *new_commit_id;
5954 error = got_object_open_as_commit(&commit, repo, commit_id);
5958 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5959 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5961 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5963 error = show_rebase_progress(commit, commit_id, NULL);
5965 error = show_rebase_progress(commit, commit_id, new_commit_id);
5966 free(new_commit_id);
5969 got_object_commit_close(commit);
5973 struct check_path_prefix_arg {
5974 const char *path_prefix;
5979 static const struct got_error *
5980 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5981 struct got_blob_object *blob2, struct got_object_id *id1,
5982 struct got_object_id *id2, const char *path1, const char *path2,
5983 mode_t mode1, mode_t mode2, struct got_repository *repo)
5985 struct check_path_prefix_arg *a = arg;
5987 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5988 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5989 return got_error(a->errcode);
5994 static const struct got_error *
5995 check_path_prefix(struct got_object_id *parent_id,
5996 struct got_object_id *commit_id, const char *path_prefix,
5997 int errcode, struct got_repository *repo)
5999 const struct got_error *err;
6000 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6001 struct got_commit_object *commit = NULL, *parent_commit = NULL;
6002 struct check_path_prefix_arg cpp_arg;
6004 if (got_path_is_root_dir(path_prefix))
6007 err = got_object_open_as_commit(&commit, repo, commit_id);
6011 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6015 err = got_object_open_as_tree(&tree1, repo,
6016 got_object_commit_get_tree_id(parent_commit));
6020 err = got_object_open_as_tree(&tree2, repo,
6021 got_object_commit_get_tree_id(commit));
6025 cpp_arg.path_prefix = path_prefix;
6026 while (cpp_arg.path_prefix[0] == '/')
6027 cpp_arg.path_prefix++;
6028 cpp_arg.len = strlen(cpp_arg.path_prefix);
6029 cpp_arg.errcode = errcode;
6030 err = got_diff_tree(tree1, tree2, "", "", repo,
6031 check_path_prefix_in_diff, &cpp_arg, 0);
6034 got_object_tree_close(tree1);
6036 got_object_tree_close(tree2);
6038 got_object_commit_close(commit);
6040 got_object_commit_close(parent_commit);
6044 static const struct got_error *
6045 collect_commits(struct got_object_id_queue *commits,
6046 struct got_object_id *initial_commit_id,
6047 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6048 const char *path_prefix, int path_prefix_errcode,
6049 struct got_repository *repo)
6051 const struct got_error *err = NULL;
6052 struct got_commit_graph *graph = NULL;
6053 struct got_object_id *parent_id = NULL;
6054 struct got_object_qid *qid;
6055 struct got_object_id *commit_id = initial_commit_id;
6057 err = got_commit_graph_open(&graph, "/", 1);
6061 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6062 check_cancelled, NULL);
6065 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6066 err = got_commit_graph_iter_next(&parent_id, graph, repo,
6067 check_cancelled, NULL);
6069 if (err->code == GOT_ERR_ITER_COMPLETED) {
6070 err = got_error_msg(GOT_ERR_ANCESTRY,
6071 "ran out of commits to rebase before "
6072 "youngest common ancestor commit has "
6077 err = check_path_prefix(parent_id, commit_id,
6078 path_prefix, path_prefix_errcode, repo);
6082 err = got_object_qid_alloc(&qid, commit_id);
6085 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6086 commit_id = parent_id;
6090 got_commit_graph_close(graph);
6094 static const struct got_error *
6095 cmd_rebase(int argc, char *argv[])
6097 const struct got_error *error = NULL;
6098 struct got_worktree *worktree = NULL;
6099 struct got_repository *repo = NULL;
6100 struct got_fileindex *fileindex = NULL;
6102 struct got_reference *branch = NULL;
6103 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6104 struct got_object_id *commit_id = NULL, *parent_id = NULL;
6105 struct got_object_id *resume_commit_id = NULL;
6106 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6107 struct got_commit_object *commit = NULL;
6108 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6109 int histedit_in_progress = 0;
6110 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6111 struct got_object_id_queue commits;
6112 struct got_pathlist_head merged_paths;
6113 const struct got_object_id_queue *parent_ids;
6114 struct got_object_qid *qid, *pid;
6116 SIMPLEQ_INIT(&commits);
6117 TAILQ_INIT(&merged_paths);
6119 while ((ch = getopt(argc, argv, "ac")) != -1) {
6125 continue_rebase = 1;
6137 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6138 "unveil", NULL) == -1)
6141 if (abort_rebase && continue_rebase)
6143 else if (abort_rebase || continue_rebase) {
6146 } else if (argc != 1)
6149 cwd = getcwd(NULL, 0);
6151 error = got_error_from_errno("getcwd");
6154 error = got_worktree_open(&worktree, cwd);
6158 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6163 error = apply_unveil(got_repo_get_path(repo), 0,
6164 got_worktree_get_root_path(worktree));
6168 error = got_worktree_histedit_in_progress(&histedit_in_progress,
6172 if (histedit_in_progress) {
6173 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6177 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6183 if (!rebase_in_progress) {
6184 error = got_error(GOT_ERR_NOT_REBASING);
6187 error = got_worktree_rebase_continue(&resume_commit_id,
6188 &new_base_branch, &tmp_branch, &branch, &fileindex,
6192 printf("Switching work tree to %s\n",
6193 got_ref_get_symref_target(new_base_branch));
6194 error = got_worktree_rebase_abort(worktree, fileindex, repo,
6195 new_base_branch, update_progress, &did_something);
6198 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6199 goto done; /* nothing else to do */
6202 if (continue_rebase) {
6203 if (!rebase_in_progress) {
6204 error = got_error(GOT_ERR_NOT_REBASING);
6207 error = got_worktree_rebase_continue(&resume_commit_id,
6208 &new_base_branch, &tmp_branch, &branch, &fileindex,
6213 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6214 resume_commit_id, repo);
6218 yca_id = got_object_id_dup(resume_commit_id);
6219 if (yca_id == NULL) {
6220 error = got_error_from_errno("got_object_id_dup");
6224 error = got_ref_open(&branch, repo, argv[0], 0);
6229 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6233 if (!continue_rebase) {
6234 struct got_object_id *base_commit_id;
6236 base_commit_id = got_worktree_get_base_commit_id(worktree);
6237 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6238 base_commit_id, branch_head_commit_id, repo,
6239 check_cancelled, NULL);
6242 if (yca_id == NULL) {
6243 error = got_error_msg(GOT_ERR_ANCESTRY,
6244 "specified branch shares no common ancestry "
6245 "with work tree's branch");
6249 error = check_same_branch(base_commit_id, branch, yca_id, repo);
6251 if (error->code != GOT_ERR_ANCESTRY)
6255 error = got_error_msg(GOT_ERR_SAME_BRANCH,
6256 "specified branch resolves to a commit which "
6257 "is already contained in work tree's branch");
6260 error = got_worktree_rebase_prepare(&new_base_branch,
6261 &tmp_branch, &fileindex, worktree, branch, repo);
6266 commit_id = branch_head_commit_id;
6267 error = got_object_open_as_commit(&commit, repo, commit_id);
6271 parent_ids = got_object_commit_get_parent_ids(commit);
6272 pid = SIMPLEQ_FIRST(parent_ids);
6274 if (!continue_rebase) {
6276 error = got_worktree_rebase_abort(worktree, fileindex,
6277 repo, new_base_branch, update_progress,
6281 printf("Rebase of %s aborted\n",
6282 got_ref_get_name(branch));
6284 error = got_error(GOT_ERR_EMPTY_REBASE);
6287 error = collect_commits(&commits, commit_id, pid->id,
6288 yca_id, got_worktree_get_path_prefix(worktree),
6289 GOT_ERR_REBASE_PATH, repo);
6290 got_object_commit_close(commit);
6295 if (SIMPLEQ_EMPTY(&commits)) {
6296 if (continue_rebase) {
6297 error = rebase_complete(worktree, fileindex,
6298 branch, new_base_branch, tmp_branch, repo);
6301 /* Fast-forward the reference of the branch. */
6302 struct got_object_id *new_head_commit_id;
6304 error = got_ref_resolve(&new_head_commit_id, repo,
6308 error = got_object_id_str(&id_str, new_head_commit_id);
6309 printf("Forwarding %s to commit %s\n",
6310 got_ref_get_name(branch), id_str);
6312 error = got_ref_change_ref(branch,
6313 new_head_commit_id);
6320 SIMPLEQ_FOREACH(qid, &commits, entry) {
6321 commit_id = qid->id;
6322 parent_id = pid ? pid->id : yca_id;
6325 error = got_worktree_rebase_merge_files(&merged_paths,
6326 worktree, fileindex, parent_id, commit_id, repo,
6327 rebase_progress, &rebase_status, check_cancelled, NULL);
6331 if (rebase_status == GOT_STATUS_CONFLICT) {
6332 error = show_rebase_merge_conflict(qid->id, repo);
6335 got_worktree_rebase_pathlist_free(&merged_paths);
6339 error = rebase_commit(&merged_paths, worktree, fileindex,
6340 tmp_branch, commit_id, repo);
6341 got_worktree_rebase_pathlist_free(&merged_paths);
6346 if (rebase_status == GOT_STATUS_CONFLICT) {
6347 error = got_worktree_rebase_postpone(worktree, fileindex);
6350 error = got_error_msg(GOT_ERR_CONFLICTS,
6351 "conflicts must be resolved before rebasing can continue");
6353 error = rebase_complete(worktree, fileindex, branch,
6354 new_base_branch, tmp_branch, repo);
6356 got_object_id_queue_free(&commits);
6357 free(branch_head_commit_id);
6358 free(resume_commit_id);
6361 got_object_commit_close(commit);
6363 got_ref_close(branch);
6364 if (new_base_branch)
6365 got_ref_close(new_base_branch);
6367 got_ref_close(tmp_branch);
6369 got_worktree_close(worktree);
6371 got_repo_close(repo);
6376 usage_histedit(void)
6378 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6383 #define GOT_HISTEDIT_PICK 'p'
6384 #define GOT_HISTEDIT_EDIT 'e'
6385 #define GOT_HISTEDIT_FOLD 'f'
6386 #define GOT_HISTEDIT_DROP 'd'
6387 #define GOT_HISTEDIT_MESG 'm'
6389 static struct got_histedit_cmd {
6393 } got_histedit_cmds[] = {
6394 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6395 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6396 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6398 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6399 { GOT_HISTEDIT_MESG, "mesg",
6400 "single-line log message for commit above (open editor if empty)" },
6403 struct got_histedit_list_entry {
6404 TAILQ_ENTRY(got_histedit_list_entry) entry;
6405 struct got_object_id *commit_id;
6406 const struct got_histedit_cmd *cmd;
6409 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6411 static const struct got_error *
6412 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6413 FILE *f, struct got_repository *repo)
6415 const struct got_error *err = NULL;
6416 char *logmsg = NULL, *id_str = NULL;
6417 struct got_commit_object *commit = NULL;
6420 err = got_object_open_as_commit(&commit, repo, commit_id);
6424 err = get_short_logmsg(&logmsg, 34, commit);
6428 err = got_object_id_str(&id_str, commit_id);
6432 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6434 err = got_ferror(f, GOT_ERR_IO);
6437 got_object_commit_close(commit);
6443 static const struct got_error *
6444 histedit_write_commit_list(struct got_object_id_queue *commits,
6445 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6447 const struct got_error *err = NULL;
6448 struct got_object_qid *qid;
6450 if (SIMPLEQ_EMPTY(commits))
6451 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6453 SIMPLEQ_FOREACH(qid, commits, entry) {
6454 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6458 if (edit_logmsg_only) {
6459 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6461 err = got_ferror(f, GOT_ERR_IO);
6470 static const struct got_error *
6471 write_cmd_list(FILE *f, const char *branch_name,
6472 struct got_object_id_queue *commits)
6474 const struct got_error *err = NULL;
6477 struct got_object_qid *qid;
6479 qid = SIMPLEQ_FIRST(commits);
6480 err = got_object_id_str(&id_str, qid->id);
6485 "# Editing the history of branch '%s' starting at\n"
6487 "# Commits will be processed in order from top to "
6488 "bottom of this file.\n", branch_name, id_str);
6490 err = got_ferror(f, GOT_ERR_IO);
6494 n = fprintf(f, "# Available histedit commands:\n");
6496 err = got_ferror(f, GOT_ERR_IO);
6500 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6501 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6502 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6505 err = got_ferror(f, GOT_ERR_IO);
6514 static const struct got_error *
6515 histedit_syntax_error(int lineno)
6517 static char msg[42];
6520 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6522 if (ret == -1 || ret >= sizeof(msg))
6523 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6525 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6528 static const struct got_error *
6529 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6530 char *logmsg, struct got_repository *repo)
6532 const struct got_error *err;
6533 struct got_commit_object *folded_commit = NULL;
6534 char *id_str, *folded_logmsg = NULL;
6536 err = got_object_id_str(&id_str, hle->commit_id);
6540 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6544 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6547 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6548 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6549 folded_logmsg) == -1) {
6550 err = got_error_from_errno("asprintf");
6554 got_object_commit_close(folded_commit);
6556 free(folded_logmsg);
6560 static struct got_histedit_list_entry *
6561 get_folded_commits(struct got_histedit_list_entry *hle)
6563 struct got_histedit_list_entry *prev, *folded = NULL;
6565 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6566 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6567 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6568 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6570 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6576 static const struct got_error *
6577 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6578 struct got_repository *repo)
6580 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6581 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6582 const struct got_error *err = NULL;
6583 struct got_commit_object *commit = NULL;
6585 struct got_histedit_list_entry *folded = NULL;
6587 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6591 folded = get_folded_commits(hle);
6593 while (folded != hle) {
6594 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6595 folded = TAILQ_NEXT(folded, entry);
6598 err = append_folded_commit_msg(&new_msg, folded,
6604 folded = TAILQ_NEXT(folded, entry);
6608 err = got_object_id_str(&id_str, hle->commit_id);
6611 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6614 if (asprintf(&new_msg,
6615 "%s\n# original log message of commit %s: %s",
6616 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6617 err = got_error_from_errno("asprintf");
6623 err = got_object_id_str(&id_str, hle->commit_id);
6627 err = got_opentemp_named_fd(&logmsg_path, &fd,
6628 GOT_TMPDIR_STR "/got-logmsg");
6632 dprintf(fd, logmsg);
6635 err = get_editor(&editor);
6639 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6641 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6643 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6646 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6647 err = got_error_from_errno2("unlink", logmsg_path);
6653 got_object_commit_close(commit);
6657 static const struct got_error *
6658 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6659 FILE *f, struct got_repository *repo)
6661 const struct got_error *err = NULL;
6662 char *line = NULL, *p, *end;
6666 const struct got_histedit_cmd *cmd;
6667 struct got_object_id *commit_id = NULL;
6668 struct got_histedit_list_entry *hle = NULL;
6671 len = getline(&line, &size, f);
6673 const struct got_error *getline_err;
6676 getline_err = got_error_from_errno("getline");
6677 err = got_ferror(f, getline_err->code);
6682 while (isspace((unsigned char)p[0]))
6684 if (p[0] == '#' || p[0] == '\0') {
6690 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6691 cmd = &got_histedit_cmds[i];
6692 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6693 isspace((unsigned char)p[strlen(cmd->name)])) {
6694 p += strlen(cmd->name);
6697 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6702 if (i == nitems(got_histedit_cmds)) {
6703 err = histedit_syntax_error(lineno);
6706 while (isspace((unsigned char)p[0]))
6708 if (cmd->code == GOT_HISTEDIT_MESG) {
6709 if (hle == NULL || hle->logmsg != NULL) {
6710 err = got_error(GOT_ERR_HISTEDIT_CMD);
6714 err = histedit_edit_logmsg(hle, repo);
6718 hle->logmsg = strdup(p);
6719 if (hle->logmsg == NULL) {
6720 err = got_error_from_errno("strdup");
6729 while (end[0] && !isspace((unsigned char)end[0]))
6733 err = got_object_resolve_id_str(&commit_id, repo, p);
6735 /* override error code */
6736 err = histedit_syntax_error(lineno);
6740 hle = malloc(sizeof(*hle));
6742 err = got_error_from_errno("malloc");
6746 hle->commit_id = commit_id;
6751 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6759 static const struct got_error *
6760 histedit_check_script(struct got_histedit_list *histedit_cmds,
6761 struct got_object_id_queue *commits, struct got_repository *repo)
6763 const struct got_error *err = NULL;
6764 struct got_object_qid *qid;
6765 struct got_histedit_list_entry *hle;
6766 static char msg[92];
6769 if (TAILQ_EMPTY(histedit_cmds))
6770 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6771 "histedit script contains no commands");
6772 if (SIMPLEQ_EMPTY(commits))
6773 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6775 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6776 struct got_histedit_list_entry *hle2;
6777 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6780 if (got_object_id_cmp(hle->commit_id,
6781 hle2->commit_id) != 0)
6783 err = got_object_id_str(&id_str, hle->commit_id);
6786 snprintf(msg, sizeof(msg), "commit %s is listed "
6787 "more than once in histedit script", id_str);
6789 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6793 SIMPLEQ_FOREACH(qid, commits, entry) {
6794 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6795 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6799 err = got_object_id_str(&id_str, qid->id);
6802 snprintf(msg, sizeof(msg),
6803 "commit %s missing from histedit script", id_str);
6805 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6809 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6810 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6811 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6812 "last commit in histedit script cannot be folded");
6817 static const struct got_error *
6818 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6819 const char *path, struct got_object_id_queue *commits,
6820 struct got_repository *repo)
6822 const struct got_error *err = NULL;
6826 err = get_editor(&editor);
6830 if (spawn_editor(editor, path) == -1) {
6831 err = got_error_from_errno("failed spawning editor");
6835 f = fopen(path, "r");
6837 err = got_error_from_errno("fopen");
6840 err = histedit_parse_list(histedit_cmds, f, repo);
6844 err = histedit_check_script(histedit_cmds, commits, repo);
6846 if (f && fclose(f) != 0 && err == NULL)
6847 err = got_error_from_errno("fclose");
6852 static const struct got_error *
6853 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6854 struct got_object_id_queue *, const char *, const char *,
6855 struct got_repository *);
6857 static const struct got_error *
6858 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6859 struct got_object_id_queue *commits, const char *branch_name,
6860 int edit_logmsg_only, struct got_repository *repo)
6862 const struct got_error *err;
6866 err = got_opentemp_named(&path, &f, "got-histedit");
6870 err = write_cmd_list(f, branch_name, commits);
6874 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6878 if (edit_logmsg_only) {
6880 err = histedit_parse_list(histedit_cmds, f, repo);
6882 if (fclose(f) != 0) {
6883 err = got_error_from_errno("fclose");
6887 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6889 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6890 err->code != GOT_ERR_HISTEDIT_CMD)
6892 err = histedit_edit_list_retry(histedit_cmds, err,
6893 commits, path, branch_name, repo);
6897 if (f && fclose(f) != 0 && err == NULL)
6898 err = got_error_from_errno("fclose");
6899 if (path && unlink(path) != 0 && err == NULL)
6900 err = got_error_from_errno2("unlink", path);
6905 static const struct got_error *
6906 histedit_save_list(struct got_histedit_list *histedit_cmds,
6907 struct got_worktree *worktree, struct got_repository *repo)
6909 const struct got_error *err = NULL;
6912 struct got_histedit_list_entry *hle;
6913 struct got_commit_object *commit = NULL;
6915 err = got_worktree_get_histedit_script_path(&path, worktree);
6919 f = fopen(path, "w");
6921 err = got_error_from_errno2("fopen", path);
6924 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6925 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6931 int n = fprintf(f, "%c %s\n",
6932 GOT_HISTEDIT_MESG, hle->logmsg);
6934 err = got_ferror(f, GOT_ERR_IO);
6940 if (f && fclose(f) != 0 && err == NULL)
6941 err = got_error_from_errno("fclose");
6944 got_object_commit_close(commit);
6949 histedit_free_list(struct got_histedit_list *histedit_cmds)
6951 struct got_histedit_list_entry *hle;
6953 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6954 TAILQ_REMOVE(histedit_cmds, hle, entry);
6959 static const struct got_error *
6960 histedit_load_list(struct got_histedit_list *histedit_cmds,
6961 const char *path, struct got_repository *repo)
6963 const struct got_error *err = NULL;
6966 f = fopen(path, "r");
6968 err = got_error_from_errno2("fopen", path);
6972 err = histedit_parse_list(histedit_cmds, f, repo);
6974 if (f && fclose(f) != 0 && err == NULL)
6975 err = got_error_from_errno("fclose");
6979 static const struct got_error *
6980 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6981 const struct got_error *edit_err, struct got_object_id_queue *commits,
6982 const char *path, const char *branch_name, struct got_repository *repo)
6984 const struct got_error *err = NULL, *prev_err = edit_err;
6987 while (resp != 'c' && resp != 'r' && resp != 'a') {
6988 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6989 "or (a)bort: ", getprogname(), prev_err->msg);
6994 histedit_free_list(histedit_cmds);
6995 err = histedit_run_editor(histedit_cmds, path, commits,
6998 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6999 err->code != GOT_ERR_HISTEDIT_CMD)
7006 } else if (resp == 'r') {
7007 histedit_free_list(histedit_cmds);
7008 err = histedit_edit_script(histedit_cmds,
7009 commits, branch_name, 0, repo);
7011 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7012 err->code != GOT_ERR_HISTEDIT_CMD)
7019 } else if (resp == 'a') {
7020 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7023 printf("invalid response '%c'\n", resp);
7029 static const struct got_error *
7030 histedit_complete(struct got_worktree *worktree,
7031 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7032 struct got_reference *branch, struct got_repository *repo)
7034 printf("Switching work tree to %s\n",
7035 got_ref_get_symref_target(branch));
7036 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7040 static const struct got_error *
7041 show_histedit_progress(struct got_commit_object *commit,
7042 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7044 const struct got_error *err;
7045 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7047 err = got_object_id_str(&old_id_str, hle->commit_id);
7052 err = got_object_id_str(&new_id_str, new_id);
7057 old_id_str[12] = '\0';
7059 new_id_str[12] = '\0';
7062 logmsg = strdup(hle->logmsg);
7063 if (logmsg == NULL) {
7064 err = got_error_from_errno("strdup");
7067 trim_logmsg(logmsg, 42);
7069 err = get_short_logmsg(&logmsg, 42, commit);
7074 switch (hle->cmd->code) {
7075 case GOT_HISTEDIT_PICK:
7076 case GOT_HISTEDIT_EDIT:
7077 printf("%s -> %s: %s\n", old_id_str,
7078 new_id_str ? new_id_str : "no-op change", logmsg);
7080 case GOT_HISTEDIT_DROP:
7081 case GOT_HISTEDIT_FOLD:
7082 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7094 static const struct got_error *
7095 histedit_commit(struct got_pathlist_head *merged_paths,
7096 struct got_worktree *worktree, struct got_fileindex *fileindex,
7097 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7098 struct got_repository *repo)
7100 const struct got_error *err;
7101 struct got_commit_object *commit;
7102 struct got_object_id *new_commit_id;
7104 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7105 && hle->logmsg == NULL) {
7106 err = histedit_edit_logmsg(hle, repo);
7111 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7115 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7116 worktree, fileindex, tmp_branch, commit, hle->commit_id,
7119 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7121 err = show_histedit_progress(commit, hle, NULL);
7123 err = show_histedit_progress(commit, hle, new_commit_id);
7124 free(new_commit_id);
7127 got_object_commit_close(commit);
7131 static const struct got_error *
7132 histedit_skip_commit(struct got_histedit_list_entry *hle,
7133 struct got_worktree *worktree, struct got_repository *repo)
7135 const struct got_error *error;
7136 struct got_commit_object *commit;
7138 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7143 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7147 error = show_histedit_progress(commit, hle, NULL);
7148 got_object_commit_close(commit);
7152 static const struct got_error *
7153 check_local_changes(void *arg, unsigned char status,
7154 unsigned char staged_status, const char *path,
7155 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7156 struct got_object_id *commit_id, int dirfd, const char *de_name)
7158 int *have_local_changes = arg;
7161 case GOT_STATUS_ADD:
7162 case GOT_STATUS_DELETE:
7163 case GOT_STATUS_MODIFY:
7164 case GOT_STATUS_CONFLICT:
7165 *have_local_changes = 1;
7166 return got_error(GOT_ERR_CANCELLED);
7171 switch (staged_status) {
7172 case GOT_STATUS_ADD:
7173 case GOT_STATUS_DELETE:
7174 case GOT_STATUS_MODIFY:
7175 *have_local_changes = 1;
7176 return got_error(GOT_ERR_CANCELLED);
7184 static const struct got_error *
7185 cmd_histedit(int argc, char *argv[])
7187 const struct got_error *error = NULL;
7188 struct got_worktree *worktree = NULL;
7189 struct got_fileindex *fileindex = NULL;
7190 struct got_repository *repo = NULL;
7192 struct got_reference *branch = NULL;
7193 struct got_reference *tmp_branch = NULL;
7194 struct got_object_id *resume_commit_id = NULL;
7195 struct got_object_id *base_commit_id = NULL;
7196 struct got_object_id *head_commit_id = NULL;
7197 struct got_commit_object *commit = NULL;
7198 int ch, rebase_in_progress = 0, did_something;
7199 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7200 int edit_logmsg_only = 0;
7201 const char *edit_script_path = NULL;
7202 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7203 struct got_object_id_queue commits;
7204 struct got_pathlist_head merged_paths;
7205 const struct got_object_id_queue *parent_ids;
7206 struct got_object_qid *pid;
7207 struct got_histedit_list histedit_cmds;
7208 struct got_histedit_list_entry *hle;
7210 SIMPLEQ_INIT(&commits);
7211 TAILQ_INIT(&histedit_cmds);
7212 TAILQ_INIT(&merged_paths);
7214 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7223 edit_script_path = optarg;
7226 edit_logmsg_only = 1;
7238 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7239 "unveil", NULL) == -1)
7242 if (abort_edit && continue_edit)
7243 errx(1, "histedit's -a and -c options are mutually exclusive");
7244 if (edit_script_path && edit_logmsg_only)
7245 errx(1, "histedit's -F and -m options are mutually exclusive");
7246 if (abort_edit && edit_logmsg_only)
7247 errx(1, "histedit's -a and -m options are mutually exclusive");
7248 if (continue_edit && edit_logmsg_only)
7249 errx(1, "histedit's -c and -m options are mutually exclusive");
7254 * This command cannot apply unveil(2) in all cases because the
7255 * user may choose to run an editor to edit the histedit script
7256 * and to edit individual commit log messages.
7257 * unveil(2) traverses exec(2); if an editor is used we have to
7258 * apply unveil after edit script and log messages have been written.
7259 * XXX TODO: Make use of unveil(2) where possible.
7262 cwd = getcwd(NULL, 0);
7264 error = got_error_from_errno("getcwd");
7267 error = got_worktree_open(&worktree, cwd);
7271 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7276 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7279 if (rebase_in_progress) {
7280 error = got_error(GOT_ERR_REBASING);
7284 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7288 if (edit_in_progress && edit_logmsg_only) {
7289 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7290 "histedit operation is in progress in this "
7291 "work tree and must be continued or aborted "
7292 "before the -m option can be used");
7296 if (edit_in_progress && abort_edit) {
7297 error = got_worktree_histedit_continue(&resume_commit_id,
7298 &tmp_branch, &branch, &base_commit_id, &fileindex,
7302 printf("Switching work tree to %s\n",
7303 got_ref_get_symref_target(branch));
7304 error = got_worktree_histedit_abort(worktree, fileindex, repo,
7305 branch, base_commit_id, update_progress, &did_something);
7308 printf("Histedit of %s aborted\n",
7309 got_ref_get_symref_target(branch));
7310 goto done; /* nothing else to do */
7311 } else if (abort_edit) {
7312 error = got_error(GOT_ERR_NOT_HISTEDIT);
7316 if (continue_edit) {
7319 if (!edit_in_progress) {
7320 error = got_error(GOT_ERR_NOT_HISTEDIT);
7324 error = got_worktree_get_histedit_script_path(&path, worktree);
7328 error = histedit_load_list(&histedit_cmds, path, repo);
7333 error = got_worktree_histedit_continue(&resume_commit_id,
7334 &tmp_branch, &branch, &base_commit_id, &fileindex,
7339 error = got_ref_resolve(&head_commit_id, repo, branch);
7343 error = got_object_open_as_commit(&commit, repo,
7347 parent_ids = got_object_commit_get_parent_ids(commit);
7348 pid = SIMPLEQ_FIRST(parent_ids);
7350 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7353 error = collect_commits(&commits, head_commit_id, pid->id,
7354 base_commit_id, got_worktree_get_path_prefix(worktree),
7355 GOT_ERR_HISTEDIT_PATH, repo);
7356 got_object_commit_close(commit);
7361 if (edit_in_progress) {
7362 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7366 error = got_ref_open(&branch, repo,
7367 got_worktree_get_head_ref_name(worktree), 0);
7371 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7372 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7373 "will not edit commit history of a branch outside "
7374 "the \"refs/heads/\" reference namespace");
7378 error = got_ref_resolve(&head_commit_id, repo, branch);
7379 got_ref_close(branch);
7384 error = got_object_open_as_commit(&commit, repo,
7388 parent_ids = got_object_commit_get_parent_ids(commit);
7389 pid = SIMPLEQ_FIRST(parent_ids);
7391 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7394 error = collect_commits(&commits, head_commit_id, pid->id,
7395 got_worktree_get_base_commit_id(worktree),
7396 got_worktree_get_path_prefix(worktree),
7397 GOT_ERR_HISTEDIT_PATH, repo);
7398 got_object_commit_close(commit);
7403 if (SIMPLEQ_EMPTY(&commits)) {
7404 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7408 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7409 &base_commit_id, &fileindex, worktree, repo);
7413 if (edit_script_path) {
7414 error = histedit_load_list(&histedit_cmds,
7415 edit_script_path, repo);
7417 got_worktree_histedit_abort(worktree, fileindex,
7418 repo, branch, base_commit_id,
7419 update_progress, &did_something);
7423 const char *branch_name;
7424 branch_name = got_ref_get_symref_target(branch);
7425 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7427 error = histedit_edit_script(&histedit_cmds, &commits,
7428 branch_name, edit_logmsg_only, repo);
7430 got_worktree_histedit_abort(worktree, fileindex,
7431 repo, branch, base_commit_id,
7432 update_progress, &did_something);
7438 error = histedit_save_list(&histedit_cmds, worktree,
7441 got_worktree_histedit_abort(worktree, fileindex,
7442 repo, branch, base_commit_id,
7443 update_progress, &did_something);
7449 error = histedit_check_script(&histedit_cmds, &commits, repo);
7453 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7454 if (resume_commit_id) {
7455 if (got_object_id_cmp(hle->commit_id,
7456 resume_commit_id) != 0)
7459 resume_commit_id = NULL;
7460 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7461 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7462 error = histedit_skip_commit(hle, worktree,
7467 struct got_pathlist_head paths;
7468 int have_changes = 0;
7471 error = got_pathlist_append(&paths, "", NULL);
7474 error = got_worktree_status(worktree, &paths,
7475 repo, check_local_changes, &have_changes,
7476 check_cancelled, NULL);
7477 got_pathlist_free(&paths);
7479 if (error->code != GOT_ERR_CANCELLED)
7481 if (sigint_received || sigpipe_received)
7485 error = histedit_commit(NULL, worktree,
7486 fileindex, tmp_branch, hle, repo);
7490 error = got_object_open_as_commit(
7491 &commit, repo, hle->commit_id);
7494 error = show_histedit_progress(commit,
7496 got_object_commit_close(commit);
7505 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7506 error = histedit_skip_commit(hle, worktree, repo);
7512 error = got_object_open_as_commit(&commit, repo,
7516 parent_ids = got_object_commit_get_parent_ids(commit);
7517 pid = SIMPLEQ_FIRST(parent_ids);
7519 error = got_worktree_histedit_merge_files(&merged_paths,
7520 worktree, fileindex, pid->id, hle->commit_id, repo,
7521 rebase_progress, &rebase_status, check_cancelled, NULL);
7524 got_object_commit_close(commit);
7527 if (rebase_status == GOT_STATUS_CONFLICT) {
7528 error = show_rebase_merge_conflict(hle->commit_id,
7532 got_worktree_rebase_pathlist_free(&merged_paths);
7536 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7538 error = got_object_id_str(&id_str, hle->commit_id);
7541 printf("Stopping histedit for amending commit %s\n",
7544 got_worktree_rebase_pathlist_free(&merged_paths);
7545 error = got_worktree_histedit_postpone(worktree,
7550 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7551 error = histedit_skip_commit(hle, worktree, repo);
7557 error = histedit_commit(&merged_paths, worktree, fileindex,
7558 tmp_branch, hle, repo);
7559 got_worktree_rebase_pathlist_free(&merged_paths);
7564 if (rebase_status == GOT_STATUS_CONFLICT) {
7565 error = got_worktree_histedit_postpone(worktree, fileindex);
7568 error = got_error_msg(GOT_ERR_CONFLICTS,
7569 "conflicts must be resolved before histedit can continue");
7571 error = histedit_complete(worktree, fileindex, tmp_branch,
7574 got_object_id_queue_free(&commits);
7575 histedit_free_list(&histedit_cmds);
7576 free(head_commit_id);
7577 free(base_commit_id);
7578 free(resume_commit_id);
7580 got_object_commit_close(commit);
7582 got_ref_close(branch);
7584 got_ref_close(tmp_branch);
7586 got_worktree_close(worktree);
7588 got_repo_close(repo);
7593 usage_integrate(void)
7595 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7599 static const struct got_error *
7600 cmd_integrate(int argc, char *argv[])
7602 const struct got_error *error = NULL;
7603 struct got_repository *repo = NULL;
7604 struct got_worktree *worktree = NULL;
7605 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7606 const char *branch_arg = NULL;
7607 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7608 struct got_fileindex *fileindex = NULL;
7609 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7610 int ch, did_something = 0;
7612 while ((ch = getopt(argc, argv, "")) != -1) {
7625 branch_arg = argv[0];
7627 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7628 "unveil", NULL) == -1)
7631 cwd = getcwd(NULL, 0);
7633 error = got_error_from_errno("getcwd");
7637 error = got_worktree_open(&worktree, cwd);
7641 error = check_rebase_or_histedit_in_progress(worktree);
7645 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7650 error = apply_unveil(got_repo_get_path(repo), 0,
7651 got_worktree_get_root_path(worktree));
7655 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7656 error = got_error_from_errno("asprintf");
7660 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7661 &base_branch_ref, worktree, refname, repo);
7665 refname = strdup(got_ref_get_name(branch_ref));
7666 if (refname == NULL) {
7667 error = got_error_from_errno("strdup");
7668 got_worktree_integrate_abort(worktree, fileindex, repo,
7669 branch_ref, base_branch_ref);
7672 base_refname = strdup(got_ref_get_name(base_branch_ref));
7673 if (base_refname == NULL) {
7674 error = got_error_from_errno("strdup");
7675 got_worktree_integrate_abort(worktree, fileindex, repo,
7676 branch_ref, base_branch_ref);
7680 error = got_ref_resolve(&commit_id, repo, branch_ref);
7684 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7688 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7689 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7690 "specified branch has already been integrated");
7691 got_worktree_integrate_abort(worktree, fileindex, repo,
7692 branch_ref, base_branch_ref);
7696 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7698 if (error->code == GOT_ERR_ANCESTRY)
7699 error = got_error(GOT_ERR_REBASE_REQUIRED);
7700 got_worktree_integrate_abort(worktree, fileindex, repo,
7701 branch_ref, base_branch_ref);
7705 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7706 branch_ref, base_branch_ref, update_progress, &did_something,
7707 check_cancelled, NULL);
7711 printf("Integrated %s into %s\n", refname, base_refname);
7714 got_repo_close(repo);
7716 got_worktree_close(worktree);
7718 free(base_commit_id);
7728 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7729 "[file-path ...]\n",
7734 static const struct got_error *
7735 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7736 const char *path, struct got_object_id *blob_id,
7737 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7738 int dirfd, const char *de_name)
7740 const struct got_error *err = NULL;
7741 char *id_str = NULL;
7743 if (staged_status != GOT_STATUS_ADD &&
7744 staged_status != GOT_STATUS_MODIFY &&
7745 staged_status != GOT_STATUS_DELETE)
7748 if (staged_status == GOT_STATUS_ADD ||
7749 staged_status == GOT_STATUS_MODIFY)
7750 err = got_object_id_str(&id_str, staged_blob_id);
7752 err = got_object_id_str(&id_str, blob_id);
7756 printf("%s %c %s\n", id_str, staged_status, path);
7761 static const struct got_error *
7762 cmd_stage(int argc, char *argv[])
7764 const struct got_error *error = NULL;
7765 struct got_repository *repo = NULL;
7766 struct got_worktree *worktree = NULL;
7768 struct got_pathlist_head paths;
7769 struct got_pathlist_entry *pe;
7770 int ch, list_stage = 0, pflag = 0;
7771 FILE *patch_script_file = NULL;
7772 const char *patch_script_path = NULL;
7773 struct choose_patch_arg cpa;
7777 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7786 patch_script_path = optarg;
7798 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7799 "unveil", NULL) == -1)
7802 if (list_stage && (pflag || patch_script_path))
7803 errx(1, "-l option cannot be used with other options");
7804 if (patch_script_path && !pflag)
7805 errx(1, "-F option can only be used together with -p option");
7807 cwd = getcwd(NULL, 0);
7809 error = got_error_from_errno("getcwd");
7813 error = got_worktree_open(&worktree, cwd);
7817 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7822 if (patch_script_path) {
7823 patch_script_file = fopen(patch_script_path, "r");
7824 if (patch_script_file == NULL) {
7825 error = got_error_from_errno2("fopen",
7830 error = apply_unveil(got_repo_get_path(repo), 0,
7831 got_worktree_get_root_path(worktree));
7835 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7840 error = got_worktree_status(worktree, &paths, repo,
7841 print_stage, NULL, check_cancelled, NULL);
7843 cpa.patch_script_file = patch_script_file;
7844 cpa.action = "stage";
7845 error = got_worktree_stage(worktree, &paths,
7846 pflag ? NULL : print_status, NULL,
7847 pflag ? choose_patch : NULL, &cpa, repo);
7850 if (patch_script_file && fclose(patch_script_file) == EOF &&
7852 error = got_error_from_errno2("fclose", patch_script_path);
7854 got_repo_close(repo);
7856 got_worktree_close(worktree);
7857 TAILQ_FOREACH(pe, &paths, entry)
7858 free((char *)pe->path);
7859 got_pathlist_free(&paths);
7867 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7868 "[file-path ...]\n",
7874 static const struct got_error *
7875 cmd_unstage(int argc, char *argv[])
7877 const struct got_error *error = NULL;
7878 struct got_repository *repo = NULL;
7879 struct got_worktree *worktree = NULL;
7881 struct got_pathlist_head paths;
7882 struct got_pathlist_entry *pe;
7883 int ch, did_something = 0, pflag = 0;
7884 FILE *patch_script_file = NULL;
7885 const char *patch_script_path = NULL;
7886 struct choose_patch_arg cpa;
7890 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7896 patch_script_path = optarg;
7908 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7909 "unveil", NULL) == -1)
7912 if (patch_script_path && !pflag)
7913 errx(1, "-F option can only be used together with -p option");
7915 cwd = getcwd(NULL, 0);
7917 error = got_error_from_errno("getcwd");
7921 error = got_worktree_open(&worktree, cwd);
7925 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7930 if (patch_script_path) {
7931 patch_script_file = fopen(patch_script_path, "r");
7932 if (patch_script_file == NULL) {
7933 error = got_error_from_errno2("fopen",
7939 error = apply_unveil(got_repo_get_path(repo), 0,
7940 got_worktree_get_root_path(worktree));
7944 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7948 cpa.patch_script_file = patch_script_file;
7949 cpa.action = "unstage";
7950 error = got_worktree_unstage(worktree, &paths, update_progress,
7951 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7953 if (patch_script_file && fclose(patch_script_file) == EOF &&
7955 error = got_error_from_errno2("fclose", patch_script_path);
7957 got_repo_close(repo);
7959 got_worktree_close(worktree);
7960 TAILQ_FOREACH(pe, &paths, entry)
7961 free((char *)pe->path);
7962 got_pathlist_free(&paths);
7970 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7971 "arg1 [arg2 ...]\n", getprogname());
7975 static const struct got_error *
7976 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7978 const struct got_error *err;
7979 struct got_blob_object *blob;
7981 err = got_object_open_as_blob(&blob, repo, id, 8192);
7985 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7986 got_object_blob_close(blob);
7990 static const struct got_error *
7991 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7993 const struct got_error *err;
7994 struct got_tree_object *tree;
7997 err = got_object_open_as_tree(&tree, repo, id);
8001 nentries = got_object_tree_get_nentries(tree);
8002 for (i = 0; i < nentries; i++) {
8003 struct got_tree_entry *te;
8005 if (sigint_received || sigpipe_received)
8007 te = got_object_tree_get_entry(tree, i);
8008 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8011 fprintf(outfile, "%s %.7o %s\n", id_str,
8012 got_tree_entry_get_mode(te),
8013 got_tree_entry_get_name(te));
8017 got_object_tree_close(tree);
8021 static const struct got_error *
8022 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8024 const struct got_error *err;
8025 struct got_commit_object *commit;
8026 const struct got_object_id_queue *parent_ids;
8027 struct got_object_qid *pid;
8028 char *id_str = NULL;
8029 const char *logmsg = NULL;
8031 err = got_object_open_as_commit(&commit, repo, id);
8035 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8039 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8040 parent_ids = got_object_commit_get_parent_ids(commit);
8041 fprintf(outfile, "numparents %d\n",
8042 got_object_commit_get_nparents(commit));
8043 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8045 err = got_object_id_str(&pid_str, pid->id);
8048 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8051 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8052 got_object_commit_get_author(commit),
8053 got_object_commit_get_author_time(commit));
8055 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8056 got_object_commit_get_author(commit),
8057 got_object_commit_get_committer_time(commit));
8059 logmsg = got_object_commit_get_logmsg_raw(commit);
8060 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8061 fprintf(outfile, "%s", logmsg);
8064 got_object_commit_close(commit);
8068 static const struct got_error *
8069 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8071 const struct got_error *err;
8072 struct got_tag_object *tag;
8073 char *id_str = NULL;
8074 const char *tagmsg = NULL;
8076 err = got_object_open_as_tag(&tag, repo, id);
8080 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8084 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8086 switch (got_object_tag_get_object_type(tag)) {
8087 case GOT_OBJ_TYPE_BLOB:
8088 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8089 GOT_OBJ_LABEL_BLOB);
8091 case GOT_OBJ_TYPE_TREE:
8092 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8093 GOT_OBJ_LABEL_TREE);
8095 case GOT_OBJ_TYPE_COMMIT:
8096 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8097 GOT_OBJ_LABEL_COMMIT);
8099 case GOT_OBJ_TYPE_TAG:
8100 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8107 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8108 got_object_tag_get_name(tag));
8110 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8111 got_object_tag_get_tagger(tag),
8112 got_object_tag_get_tagger_time(tag));
8114 tagmsg = got_object_tag_get_message(tag);
8115 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8116 fprintf(outfile, "%s", tagmsg);
8119 got_object_tag_close(tag);
8123 static const struct got_error *
8124 cmd_cat(int argc, char *argv[])
8126 const struct got_error *error;
8127 struct got_repository *repo = NULL;
8128 struct got_worktree *worktree = NULL;
8129 char *cwd = NULL, *repo_path = NULL, *label = NULL;
8130 const char *commit_id_str = NULL;
8131 struct got_object_id *id = NULL, *commit_id = NULL;
8132 int ch, obj_type, i, force_path = 0;
8135 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8140 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8143 commit_id_str = optarg;
8146 repo_path = realpath(optarg, NULL);
8147 if (repo_path == NULL)
8148 return got_error_from_errno2("realpath",
8150 got_path_strip_trailing_slashes(repo_path);
8164 cwd = getcwd(NULL, 0);
8166 error = got_error_from_errno("getcwd");
8169 error = got_worktree_open(&worktree, cwd);
8170 if (error && error->code != GOT_ERR_NOT_WORKTREE)
8173 if (repo_path == NULL) {
8175 got_worktree_get_repo_path(worktree));
8176 if (repo_path == NULL) {
8177 error = got_error_from_errno("strdup");
8183 if (repo_path == NULL) {
8184 repo_path = getcwd(NULL, 0);
8185 if (repo_path == NULL)
8186 return got_error_from_errno("getcwd");
8189 error = got_repo_open(&repo, repo_path, NULL);
8194 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8198 if (commit_id_str == NULL)
8199 commit_id_str = GOT_REF_HEAD;
8200 error = got_repo_match_object_id(&commit_id, NULL,
8201 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8205 for (i = 0; i < argc; i++) {
8207 error = got_object_id_by_path(&id, repo, commit_id,
8212 error = got_repo_match_object_id(&id, &label, argv[i],
8213 GOT_OBJ_TYPE_ANY, 0, repo);
8215 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8216 error->code != GOT_ERR_NOT_REF)
8218 error = got_object_id_by_path(&id, repo,
8219 commit_id, argv[i]);
8225 error = got_object_get_type(&obj_type, repo, id);
8230 case GOT_OBJ_TYPE_BLOB:
8231 error = cat_blob(id, repo, stdout);
8233 case GOT_OBJ_TYPE_TREE:
8234 error = cat_tree(id, repo, stdout);
8236 case GOT_OBJ_TYPE_COMMIT:
8237 error = cat_commit(id, repo, stdout);
8239 case GOT_OBJ_TYPE_TAG:
8240 error = cat_tag(id, repo, stdout);
8243 error = got_error(GOT_ERR_OBJ_TYPE);
8258 got_worktree_close(worktree);
8260 const struct got_error *repo_error;
8261 repo_error = got_repo_close(repo);