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_checkout(void);
90 __dead static void usage_update(void);
91 __dead static void usage_log(void);
92 __dead static void usage_diff(void);
93 __dead static void usage_blame(void);
94 __dead static void usage_tree(void);
95 __dead static void usage_status(void);
96 __dead static void usage_ref(void);
97 __dead static void usage_branch(void);
98 __dead static void usage_tag(void);
99 __dead static void usage_add(void);
100 __dead static void usage_remove(void);
101 __dead static void usage_revert(void);
102 __dead static void usage_commit(void);
103 __dead static void usage_cherrypick(void);
104 __dead static void usage_backout(void);
105 __dead static void usage_rebase(void);
106 __dead static void usage_histedit(void);
107 __dead static void usage_integrate(void);
108 __dead static void usage_stage(void);
109 __dead static void usage_unstage(void);
110 __dead static void usage_cat(void);
112 static const struct got_error* cmd_init(int, char *[]);
113 static const struct got_error* cmd_import(int, char *[]);
114 static const struct got_error* cmd_clone(int, char *[]);
115 static const struct got_error* cmd_checkout(int, char *[]);
116 static const struct got_error* cmd_update(int, char *[]);
117 static const struct got_error* cmd_log(int, char *[]);
118 static const struct got_error* cmd_diff(int, char *[]);
119 static const struct got_error* cmd_blame(int, char *[]);
120 static const struct got_error* cmd_tree(int, char *[]);
121 static const struct got_error* cmd_status(int, char *[]);
122 static const struct got_error* cmd_ref(int, char *[]);
123 static const struct got_error* cmd_branch(int, char *[]);
124 static const struct got_error* cmd_tag(int, char *[]);
125 static const struct got_error* cmd_add(int, char *[]);
126 static const struct got_error* cmd_remove(int, char *[]);
127 static const struct got_error* cmd_revert(int, char *[]);
128 static const struct got_error* cmd_commit(int, char *[]);
129 static const struct got_error* cmd_cherrypick(int, char *[]);
130 static const struct got_error* cmd_backout(int, char *[]);
131 static const struct got_error* cmd_rebase(int, char *[]);
132 static const struct got_error* cmd_histedit(int, char *[]);
133 static const struct got_error* cmd_integrate(int, char *[]);
134 static const struct got_error* cmd_stage(int, char *[]);
135 static const struct got_error* cmd_unstage(int, char *[]);
136 static const struct got_error* cmd_cat(int, char *[]);
138 static struct got_cmd got_commands[] = {
139 { "init", cmd_init, usage_init, "in" },
140 { "import", cmd_import, usage_import, "im" },
141 { "clone", cmd_clone, usage_clone, "cl" },
142 { "checkout", cmd_checkout, usage_checkout, "co" },
143 { "update", cmd_update, usage_update, "up" },
144 { "log", cmd_log, usage_log, "" },
145 { "diff", cmd_diff, usage_diff, "di" },
146 { "blame", cmd_blame, usage_blame, "bl" },
147 { "tree", cmd_tree, usage_tree, "tr" },
148 { "status", cmd_status, usage_status, "st" },
149 { "ref", cmd_ref, usage_ref, "" },
150 { "branch", cmd_branch, usage_branch, "br" },
151 { "tag", cmd_tag, usage_tag, "" },
152 { "add", cmd_add, usage_add, "" },
153 { "remove", cmd_remove, usage_remove, "rm" },
154 { "revert", cmd_revert, usage_revert, "rv" },
155 { "commit", cmd_commit, usage_commit, "ci" },
156 { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
157 { "backout", cmd_backout, usage_backout, "bo" },
158 { "rebase", cmd_rebase, usage_rebase, "rb" },
159 { "histedit", cmd_histedit, usage_histedit, "he" },
160 { "integrate", cmd_integrate, usage_integrate,"ig" },
161 { "stage", cmd_stage, usage_stage, "sg" },
162 { "unstage", cmd_unstage, usage_unstage, "ug" },
163 { "cat", cmd_cat, usage_cat, "" },
171 fprintf(stderr, "commands:");
172 for (i = 0; i < nitems(got_commands); i++) {
173 struct got_cmd *cmd = &got_commands[i];
174 fprintf(stderr, " %s", cmd->cmd_name);
180 main(int argc, char *argv[])
185 int hflag = 0, Vflag = 0;
186 static struct option longopts[] = {
187 { "version", no_argument, NULL, 'V' },
191 setlocale(LC_CTYPE, "");
193 while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
212 got_version_print_str();
219 signal(SIGINT, catch_sigint);
220 signal(SIGPIPE, catch_sigpipe);
222 for (i = 0; i < nitems(got_commands); i++) {
223 const struct got_error *error;
225 cmd = &got_commands[i];
227 if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
228 strcmp(cmd->cmd_alias, argv[0]) != 0)
232 got_commands[i].cmd_usage();
234 error = got_commands[i].cmd_main(argc, argv);
235 if (error && error->code != GOT_ERR_CANCELLED &&
236 error->code != GOT_ERR_PRIVSEP_EXIT &&
237 !(sigpipe_received &&
238 error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
240 error->code == GOT_ERR_ERRNO && errno == EINTR)) {
241 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
248 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
256 fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
263 static const struct got_error *
264 get_editor(char **abspath)
266 const struct got_error *err = NULL;
271 editor = getenv("VISUAL");
273 editor = getenv("EDITOR");
276 err = got_path_find_prog(abspath, editor);
281 if (*abspath == NULL) {
282 *abspath = strdup("/bin/ed");
283 if (*abspath == NULL)
284 return got_error_from_errno("strdup");
290 static const struct got_error *
291 apply_unveil(const char *repo_path, int repo_read_only,
292 const char *worktree_path)
294 const struct got_error *err;
297 if (unveil("gmon.out", "rwc") != 0)
298 return got_error_from_errno2("unveil", "gmon.out");
300 if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
301 return got_error_from_errno2("unveil", repo_path);
303 if (worktree_path && unveil(worktree_path, "rwc") != 0)
304 return got_error_from_errno2("unveil", worktree_path);
306 if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
307 return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
309 err = got_privsep_unveil_exec_helpers();
313 if (unveil(NULL, NULL) != 0)
314 return got_error_from_errno("unveil");
322 fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 static const struct got_error *
327 cmd_init(int argc, char *argv[])
329 const struct got_error *error = NULL;
330 char *repo_path = NULL;
333 while ((ch = getopt(argc, argv, "")) != -1) {
345 if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
351 repo_path = strdup(argv[0]);
352 if (repo_path == NULL)
353 return got_error_from_errno("strdup");
355 got_path_strip_trailing_slashes(repo_path);
357 error = got_path_mkdir(repo_path);
359 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
362 error = apply_unveil(repo_path, 0, NULL);
366 error = got_repo_init(repo_path);
375 fprintf(stderr, "usage: %s import [-b branch] [-m message] "
376 "[-r repository-path] [-I pattern] path\n", getprogname());
381 spawn_editor(const char *editor, const char *file)
384 sig_t sighup, sigint, sigquit;
387 sighup = signal(SIGHUP, SIG_IGN);
388 sigint = signal(SIGINT, SIG_IGN);
389 sigquit = signal(SIGQUIT, SIG_IGN);
391 switch (pid = fork()) {
395 execl(editor, editor, file, (char *)NULL);
399 while (waitpid(pid, &st, 0) == -1)
404 (void)signal(SIGHUP, sighup);
405 (void)signal(SIGINT, sigint);
406 (void)signal(SIGQUIT, sigquit);
408 if (!WIFEXITED(st)) {
413 return WEXITSTATUS(st);
416 static const struct got_error *
417 edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
418 const char *initial_content)
420 const struct got_error *err = NULL;
424 int content_changed = 0;
429 if (stat(logmsg_path, &st) == -1)
430 return got_error_from_errno2("stat", logmsg_path);
432 if (spawn_editor(editor, logmsg_path) == -1)
433 return got_error_from_errno("failed spawning editor");
435 if (stat(logmsg_path, &st2) == -1)
436 return got_error_from_errno("stat");
438 if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
439 return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
440 "no changes made to commit message, aborting");
442 *logmsg = malloc(st2.st_size + 1);
444 return got_error_from_errno("malloc");
448 fp = fopen(logmsg_path, "r");
450 err = got_error_from_errno("fopen");
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
454 if (!content_changed && strcmp(buf, initial_content) != 0)
456 if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
457 continue; /* remove comments and leading empty lines */
458 len = strlcat(*logmsg, buf, st2.st_size);
462 while (len > 0 && (*logmsg)[len - 1] == '\n') {
463 (*logmsg)[len - 1] = '\0';
467 if (len == 0 || !content_changed)
468 err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
469 "commit message cannot be empty, aborting");
478 static const struct got_error *
479 collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
480 const char *path_dir, const char *branch_name)
482 char *initial_content = NULL;
483 const struct got_error *err = NULL;
486 if (asprintf(&initial_content,
487 "\n# %s to be imported to branch %s\n", path_dir,
489 return got_error_from_errno("asprintf");
491 err = got_opentemp_named_fd(logmsg_path, &fd,
492 GOT_TMPDIR_STR "/got-importmsg");
496 dprintf(fd, initial_content);
499 err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
501 free(initial_content);
505 static const struct got_error *
506 import_progress(void *arg, const char *path)
508 printf("A %s\n", path);
512 static const struct got_error *
513 get_author(char **author, struct got_repository *repo)
515 const struct got_error *err = NULL;
516 const char *got_author, *name, *email;
520 name = got_repo_get_gitconfig_author_name(repo);
521 email = got_repo_get_gitconfig_author_email(repo);
523 if (asprintf(author, "%s <%s>", name, email) == -1)
524 return got_error_from_errno("asprintf");
528 got_author = getenv("GOT_AUTHOR");
529 if (got_author == NULL) {
530 name = got_repo_get_global_gitconfig_author_name(repo);
531 email = got_repo_get_global_gitconfig_author_email(repo);
533 if (asprintf(author, "%s <%s>", name, email) == -1)
534 return got_error_from_errno("asprintf");
537 /* TODO: Look up user in password database? */
538 return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
541 *author = strdup(got_author);
543 return got_error_from_errno("strdup");
546 * Really dumb email address check; we're only doing this to
547 * avoid git's object parser breaking on commits we create.
549 while (*got_author && *got_author != '<')
551 if (*got_author != '<') {
552 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
555 while (*got_author && *got_author != '@')
557 if (*got_author != '@') {
558 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
561 while (*got_author && *got_author != '>')
563 if (*got_author != '>')
564 err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
573 static const struct got_error *
574 get_gitconfig_path(char **gitconfig_path)
576 const char *homedir = getenv("HOME");
578 *gitconfig_path = NULL;
580 if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
581 return got_error_from_errno("asprintf");
587 static const struct got_error *
588 cmd_import(int argc, char *argv[])
590 const struct got_error *error = NULL;
591 char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
592 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
593 const char *branch_name = "main";
594 char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
595 struct got_repository *repo = NULL;
596 struct got_reference *branch_ref = NULL, *head_ref = NULL;
597 struct got_object_id *new_commit_id = NULL;
599 struct got_pathlist_head ignores;
600 struct got_pathlist_entry *pe;
601 int preserve_logmsg = 0;
603 TAILQ_INIT(&ignores);
605 while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
608 branch_name = optarg;
611 logmsg = strdup(optarg);
612 if (logmsg == NULL) {
613 error = got_error_from_errno("strdup");
618 repo_path = realpath(optarg, NULL);
619 if (repo_path == NULL) {
620 error = got_error_from_errno2("realpath",
626 if (optarg[0] == '\0')
628 error = got_pathlist_insert(&pe, &ignores, optarg,
643 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
651 if (repo_path == NULL) {
652 repo_path = getcwd(NULL, 0);
653 if (repo_path == NULL)
654 return got_error_from_errno("getcwd");
656 got_path_strip_trailing_slashes(repo_path);
657 error = get_gitconfig_path(&gitconfig_path);
660 error = got_repo_open(&repo, repo_path, gitconfig_path);
664 error = get_author(&author, repo);
669 * Don't let the user create a branch name with a leading '-'.
670 * While technically a valid reference name, this case is usually
671 * an unintended typo.
673 if (branch_name[0] == '-')
674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
677 error = got_error_from_errno("asprintf");
681 error = got_ref_open(&branch_ref, repo, refname, 0);
683 if (error->code != GOT_ERR_NOT_REF)
686 error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
687 "import target branch already exists");
691 path_dir = realpath(argv[0], NULL);
692 if (path_dir == NULL) {
693 error = got_error_from_errno2("realpath", argv[0]);
696 got_path_strip_trailing_slashes(path_dir);
699 * unveil(2) traverses exec(2); if an editor is used we have
700 * to apply unveil after the log message has been written.
702 if (logmsg == NULL || strlen(logmsg) == 0) {
703 error = get_editor(&editor);
707 error = collect_import_msg(&logmsg, &logmsg_path, editor,
710 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
717 if (unveil(path_dir, "r") != 0) {
718 error = got_error_from_errno2("unveil", path_dir);
724 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
731 error = got_repo_import(&new_commit_id, path_dir, logmsg,
732 author, &ignores, repo, import_progress, NULL);
739 error = got_ref_alloc(&branch_ref, refname, new_commit_id);
746 error = got_ref_write(branch_ref, repo);
753 error = got_object_id_str(&id_str, new_commit_id);
760 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
762 if (error->code != GOT_ERR_NOT_REF) {
768 error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
776 error = got_ref_write(head_ref, repo);
784 printf("Created branch %s with commit %s\n",
785 got_ref_get_name(branch_ref), id_str);
787 if (preserve_logmsg) {
788 fprintf(stderr, "%s: log message preserved in %s\n",
789 getprogname(), logmsg_path);
790 } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
791 error = got_error_from_errno2("unlink", logmsg_path);
800 free(gitconfig_path);
802 got_ref_close(branch_ref);
804 got_ref_close(head_ref);
811 fprintf(stderr, "usage: %s clone [-q] [-v] repository-url [target-directory]\n",
816 struct got_fetch_progress_arg {
817 char last_scaled_size[FMT_SCALED_STRSIZE];
823 static const struct got_error *
824 fetch_progress(void *arg, const char *message, off_t packfile_size,
825 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
827 struct got_fetch_progress_arg *a = arg;
828 char scaled_size[FMT_SCALED_STRSIZE];
829 int p_indexed, p_resolved;
830 int print_size = 0, print_indexed = 0, print_resolved = 0;
832 if (a->verbosity < 0)
835 if (message && message[0] != '\0') {
836 printf("\rserver: %s", message);
840 if (packfile_size > 0 || nobj_indexed > 0) {
841 if (fmt_scaled(packfile_size, scaled_size) == 0 &&
842 (a->last_scaled_size[0] == '\0' ||
843 strcmp(scaled_size, a->last_scaled_size)) != 0) {
845 if (strlcpy(a->last_scaled_size, scaled_size,
846 FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
847 return got_error(GOT_ERR_NO_SPACE);
849 if (nobj_indexed > 0) {
850 p_indexed = (nobj_indexed * 100) / nobj_total;
851 if (p_indexed != a->last_p_indexed) {
852 a->last_p_indexed = p_indexed;
857 if (nobj_resolved > 0) {
858 p_resolved = (nobj_resolved * 100) /
859 (nobj_total - nobj_loose);
860 if (p_resolved != a->last_p_resolved) {
861 a->last_p_resolved = p_resolved;
869 if (print_size || print_indexed || print_resolved)
872 printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
874 printf("; indexing %d%%", p_indexed);
875 if (a->verbosity > 0)
876 printf(" (%d/%d)", nobj_indexed, nobj_total);
878 if (print_resolved) {
879 printf("; resolving deltas %d%%", p_resolved);
880 if (a->verbosity > 0)
881 printf(" (%d/%d)", nobj_resolved,
882 nobj_total - nobj_loose);
884 if (print_size || print_indexed || print_resolved)
887 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
888 nobj_resolved == nobj_total - nobj_loose)
889 printf("\nWriting pack index...\n");
894 static const struct got_error *
895 cmd_clone(int argc, char *argv[])
897 const struct got_error *error = NULL;
898 const char *uri, *dirname;
899 char *proto, *host, *port, *repo_name, *server_path;
900 char *default_destdir = NULL, *id_str = NULL;
901 const char *repo_path;
902 struct got_repository *repo = NULL;
903 struct got_pathlist_head refs, symrefs;
904 struct got_pathlist_entry *pe;
905 struct got_object_id *pack_hash = NULL;
906 int ch, fetchfd = -1;
907 struct got_fetch_progress_arg fpa;
908 char *git_url = NULL;
909 char *gitconfig_path = NULL;
910 char *gitconfig = NULL;
911 FILE *gitconfig_file = NULL;
916 TAILQ_INIT(&symrefs);
918 while ((ch = getopt(argc, argv, "vq")) != -1) {
923 else if (verbosity < 3)
946 error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
947 &repo_name, argv[0]);
951 if (strcmp(proto, "git") == 0) {
953 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
954 "sendfd dns inet unveil", NULL) == -1)
957 git_url = strdup(argv[0]);
958 if (git_url == NULL) {
959 error = got_error_from_errno("strdup");
962 } else if (strcmp(proto, "git+ssh") == 0 ||
963 strcmp(proto, "ssh") == 0) {
965 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
966 "sendfd unveil", NULL) == -1)
969 if (asprintf(&git_url, "%s:%s", host, server_path) == -1) {
970 error = got_error_from_errno("asprintf");
973 } else if (strcmp(proto, "http") == 0 ||
974 strcmp(proto, "git+http") == 0) {
975 error = got_error_path(proto, GOT_ERR_NOT_IMPL);
978 error = got_error_path(proto, GOT_ERR_BAD_PROTO);
981 if (dirname == NULL) {
982 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
983 error = got_error_from_errno("asprintf");
986 repo_path = default_destdir;
990 error = got_path_mkdir(repo_path);
994 error = got_repo_init(repo_path);
998 error = got_repo_open(&repo, repo_path, NULL);
1002 if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1003 if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1004 error = got_error_from_errno2("unveil",
1005 GOT_FETCH_PATH_SSH);
1009 error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1013 error = got_fetch_connect(&fetchfd, proto, host, port, server_path,
1019 printf("Connected to %s:%s\n", host, port);
1021 fpa.last_scaled_size[0] = '\0';
1022 fpa.last_p_indexed = -1;
1023 fpa.last_p_resolved = -1;
1024 fpa.verbosity = verbosity;
1025 error = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1026 repo, fetch_progress, &fpa);
1030 error = got_object_id_str(&id_str, pack_hash);
1034 printf("Fetched %s.pack\n", id_str);
1037 /* Set up references provided with the pack file. */
1038 TAILQ_FOREACH(pe, &refs, entry) {
1039 const char *refname = pe->path;
1040 struct got_object_id *id = pe->data;
1041 struct got_reference *ref;
1042 char *remote_refname;
1044 error = got_ref_alloc(&ref, refname, id);
1047 error = got_ref_write(ref, repo);
1052 if (strncmp("refs/heads/", refname, 11) != 0)
1055 if (asprintf(&remote_refname,
1056 "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1057 refname + 11) == -1) {
1058 error = got_error_from_errno("asprintf");
1061 error = got_ref_alloc(&ref, remote_refname, id);
1064 error = got_ref_write(ref, repo);
1070 /* Set the HEAD reference if the server provided one. */
1071 TAILQ_FOREACH(pe, &symrefs, entry) {
1072 struct got_reference *symref, *target_ref;
1073 const char *refname = pe->path;
1074 const char *target = pe->data;
1076 if (strcmp(refname, GOT_REF_HEAD) != 0)
1079 error = got_ref_open(&target_ref, repo, target, 0);
1081 if (error->code == GOT_ERR_NOT_REF)
1086 error = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1087 got_ref_close(target_ref);
1091 if (verbosity > 1) {
1092 printf("Setting %s to %s\n", GOT_REF_HEAD,
1093 got_ref_get_symref_target(symref));
1096 printf("Created cloned repository '%s'\n", repo_path);
1098 error = got_ref_write(symref, repo);
1099 got_ref_close(symref);
1103 /* Create a config file so Git can understand this repository. */
1104 gitconfig_path = got_repo_get_path_gitconfig(repo);
1105 if (gitconfig_path == NULL) {
1106 error = got_error_from_errno("got_repo_get_path_gitconfig");
1109 gitconfig_file = fopen(gitconfig_path, "w");
1110 if (gitconfig_file == NULL) {
1111 error = got_error_from_errno2("fopen", gitconfig_path);
1114 if (asprintf(&gitconfig,
1116 "\trepositoryformatversion = 0\n"
1120 "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1121 GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1122 GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1123 error = got_error_from_errno("asprintf");
1126 n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1127 if (n != strlen(gitconfig))
1128 error = got_ferror(gitconfig_file, GOT_ERR_IO);
1130 if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1131 error = got_error_from_errno("close");
1132 if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1133 error = got_error_from_errno("fclose");
1135 got_repo_close(repo);
1136 TAILQ_FOREACH(pe, &refs, entry) {
1137 free((void *)pe->path);
1140 got_pathlist_free(&refs);
1141 TAILQ_FOREACH(pe, &symrefs, entry) {
1142 free((void *)pe->path);
1145 got_pathlist_free(&symrefs);
1152 free(default_destdir);
1153 free(gitconfig_path);
1159 usage_checkout(void)
1161 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1162 "[-p prefix] repository-path [worktree-path]\n", getprogname());
1167 show_worktree_base_ref_warning(void)
1169 fprintf(stderr, "%s: warning: could not create a reference "
1170 "to the work tree's base commit; the commit could be "
1171 "garbage-collected by Git; making the repository "
1172 "writable and running 'got update' will prevent this\n",
1176 struct got_checkout_progress_arg {
1177 const char *worktree_path;
1178 int had_base_commit_ref_error;
1181 static const struct got_error *
1182 checkout_progress(void *arg, unsigned char status, const char *path)
1184 struct got_checkout_progress_arg *a = arg;
1186 /* Base commit bump happens silently. */
1187 if (status == GOT_STATUS_BUMP_BASE)
1190 if (status == GOT_STATUS_BASE_REF_ERR) {
1191 a->had_base_commit_ref_error = 1;
1195 while (path[0] == '/')
1198 printf("%c %s/%s\n", status, a->worktree_path, path);
1202 static const struct got_error *
1203 check_cancelled(void *arg)
1205 if (sigint_received || sigpipe_received)
1206 return got_error(GOT_ERR_CANCELLED);
1210 static const struct got_error *
1211 check_linear_ancestry(struct got_object_id *commit_id,
1212 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1213 struct got_repository *repo)
1215 const struct got_error *err = NULL;
1216 struct got_object_id *yca_id;
1218 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1219 commit_id, base_commit_id, repo, check_cancelled, NULL);
1224 return got_error(GOT_ERR_ANCESTRY);
1227 * Require a straight line of history between the target commit
1228 * and the work tree's base commit.
1230 * Non-linear situations such as this require a rebase:
1232 * (commit) D F (base_commit)
1240 * 'got update' only handles linear cases:
1241 * Update forwards in time: A (base/yca) - B - C - D (commit)
1242 * Update backwards in time: D (base) - C - B - A (commit/yca)
1244 if (allow_forwards_in_time_only) {
1245 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1246 return got_error(GOT_ERR_ANCESTRY);
1247 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1248 got_object_id_cmp(base_commit_id, yca_id) != 0)
1249 return got_error(GOT_ERR_ANCESTRY);
1255 static const struct got_error *
1256 check_same_branch(struct got_object_id *commit_id,
1257 struct got_reference *head_ref, struct got_object_id *yca_id,
1258 struct got_repository *repo)
1260 const struct got_error *err = NULL;
1261 struct got_commit_graph *graph = NULL;
1262 struct got_object_id *head_commit_id = NULL;
1263 int is_same_branch = 0;
1265 err = got_ref_resolve(&head_commit_id, repo, head_ref);
1269 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1273 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1278 err = got_commit_graph_open(&graph, "/", 1);
1282 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1283 check_cancelled, NULL);
1288 struct got_object_id *id;
1289 err = got_commit_graph_iter_next(&id, graph, repo,
1290 check_cancelled, NULL);
1292 if (err->code == GOT_ERR_ITER_COMPLETED)
1298 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1300 if (got_object_id_cmp(id, commit_id) == 0) {
1308 got_commit_graph_close(graph);
1309 free(head_commit_id);
1310 if (!err && !is_same_branch)
1311 err = got_error(GOT_ERR_ANCESTRY);
1315 static const struct got_error *
1316 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1318 static char msg[512];
1319 const char *branch_name;
1321 if (got_ref_is_symbolic(ref))
1322 branch_name = got_ref_get_symref_target(ref);
1324 branch_name = got_ref_get_name(ref);
1326 if (strncmp("refs/heads/", branch_name, 11) == 0)
1329 snprintf(msg, sizeof(msg),
1330 "target commit is not contained in branch '%s'; "
1331 "the branch to use must be specified with -b; "
1332 "if necessary a new branch can be created for "
1333 "this commit with 'got branch -c %s BRANCH_NAME'",
1334 branch_name, commit_id_str);
1336 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1339 static const struct got_error *
1340 cmd_checkout(int argc, char *argv[])
1342 const struct got_error *error = NULL;
1343 struct got_repository *repo = NULL;
1344 struct got_reference *head_ref = NULL;
1345 struct got_worktree *worktree = NULL;
1346 char *repo_path = NULL;
1347 char *worktree_path = NULL;
1348 const char *path_prefix = "";
1349 const char *branch_name = GOT_REF_HEAD;
1350 char *commit_id_str = NULL;
1351 int ch, same_path_prefix, allow_nonempty = 0;
1352 struct got_pathlist_head paths;
1353 struct got_checkout_progress_arg cpa;
1357 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1360 branch_name = optarg;
1363 commit_id_str = strdup(optarg);
1364 if (commit_id_str == NULL)
1365 return got_error_from_errno("strdup");
1371 path_prefix = optarg;
1383 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1384 "unveil", NULL) == -1)
1388 char *cwd, *base, *dotgit;
1389 repo_path = realpath(argv[0], NULL);
1390 if (repo_path == NULL)
1391 return got_error_from_errno2("realpath", argv[0]);
1392 cwd = getcwd(NULL, 0);
1394 error = got_error_from_errno("getcwd");
1397 if (path_prefix[0]) {
1398 base = basename(path_prefix);
1400 error = got_error_from_errno2("basename",
1405 base = basename(repo_path);
1407 error = got_error_from_errno2("basename",
1412 dotgit = strstr(base, ".git");
1415 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1416 error = got_error_from_errno("asprintf");
1421 } else if (argc == 2) {
1422 repo_path = realpath(argv[0], NULL);
1423 if (repo_path == NULL) {
1424 error = got_error_from_errno2("realpath", argv[0]);
1427 worktree_path = realpath(argv[1], NULL);
1428 if (worktree_path == NULL) {
1429 if (errno != ENOENT) {
1430 error = got_error_from_errno2("realpath",
1434 worktree_path = strdup(argv[1]);
1435 if (worktree_path == NULL) {
1436 error = got_error_from_errno("strdup");
1443 got_path_strip_trailing_slashes(repo_path);
1444 got_path_strip_trailing_slashes(worktree_path);
1446 error = got_repo_open(&repo, repo_path, NULL);
1450 /* Pre-create work tree path for unveil(2) */
1451 error = got_path_mkdir(worktree_path);
1453 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1454 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1456 if (!allow_nonempty &&
1457 !got_path_dir_is_empty(worktree_path)) {
1458 error = got_error_path(worktree_path,
1459 GOT_ERR_DIR_NOT_EMPTY);
1464 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1468 error = got_ref_open(&head_ref, repo, branch_name, 0);
1472 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1473 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1476 error = got_worktree_open(&worktree, worktree_path);
1480 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1484 if (!same_path_prefix) {
1485 error = got_error(GOT_ERR_PATH_PREFIX);
1489 if (commit_id_str) {
1490 struct got_object_id *commit_id;
1491 error = got_repo_match_object_id(&commit_id, NULL,
1492 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1495 error = check_linear_ancestry(commit_id,
1496 got_worktree_get_base_commit_id(worktree), 0, repo);
1497 if (error != NULL) {
1499 if (error->code == GOT_ERR_ANCESTRY) {
1500 error = checkout_ancestry_error(
1501 head_ref, commit_id_str);
1505 error = check_same_branch(commit_id, head_ref, NULL, repo);
1507 if (error->code == GOT_ERR_ANCESTRY) {
1508 error = checkout_ancestry_error(
1509 head_ref, commit_id_str);
1513 error = got_worktree_set_base_commit_id(worktree, repo,
1520 error = got_pathlist_append(&paths, "", NULL);
1523 cpa.worktree_path = worktree_path;
1524 cpa.had_base_commit_ref_error = 0;
1525 error = got_worktree_checkout_files(worktree, &paths, repo,
1526 checkout_progress, &cpa, check_cancelled, NULL);
1530 printf("Now shut up and hack\n");
1531 if (cpa.had_base_commit_ref_error)
1532 show_worktree_base_ref_warning();
1534 got_pathlist_free(&paths);
1535 free(commit_id_str);
1537 free(worktree_path);
1544 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1549 static const struct got_error *
1550 update_progress(void *arg, unsigned char status, const char *path)
1552 int *did_something = arg;
1554 if (status == GOT_STATUS_EXISTS ||
1555 status == GOT_STATUS_BASE_REF_ERR)
1560 /* Base commit bump happens silently. */
1561 if (status == GOT_STATUS_BUMP_BASE)
1564 while (path[0] == '/')
1566 printf("%c %s\n", status, path);
1570 static const struct got_error *
1571 switch_head_ref(struct got_reference *head_ref,
1572 struct got_object_id *commit_id, struct got_worktree *worktree,
1573 struct got_repository *repo)
1575 const struct got_error *err = NULL;
1577 int ref_has_moved = 0;
1579 /* Trivial case: switching between two different references. */
1580 if (strcmp(got_ref_get_name(head_ref),
1581 got_worktree_get_head_ref_name(worktree)) != 0) {
1582 printf("Switching work tree from %s to %s\n",
1583 got_worktree_get_head_ref_name(worktree),
1584 got_ref_get_name(head_ref));
1585 return got_worktree_set_head_ref(worktree, head_ref);
1588 err = check_linear_ancestry(commit_id,
1589 got_worktree_get_base_commit_id(worktree), 0, repo);
1591 if (err->code != GOT_ERR_ANCESTRY)
1598 /* Switching to a rebased branch with the same reference name. */
1599 err = got_object_id_str(&base_id_str,
1600 got_worktree_get_base_commit_id(worktree));
1603 printf("Reference %s now points at a different branch\n",
1604 got_worktree_get_head_ref_name(worktree));
1605 printf("Switching work tree from %s to %s\n", base_id_str,
1606 got_worktree_get_head_ref_name(worktree));
1610 static const struct got_error *
1611 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1613 const struct got_error *err;
1616 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1620 return got_error(GOT_ERR_REBASING);
1622 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1626 return got_error(GOT_ERR_HISTEDIT_BUSY);
1631 static const struct got_error *
1632 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1633 char *argv[], struct got_worktree *worktree)
1635 const struct got_error *err = NULL;
1642 return got_error_from_errno("strdup");
1643 return got_pathlist_append(paths, path, NULL);
1646 for (i = 0; i < argc; i++) {
1647 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1650 err = got_pathlist_append(paths, path, NULL);
1660 static const struct got_error *
1661 cmd_update(int argc, char *argv[])
1663 const struct got_error *error = NULL;
1664 struct got_repository *repo = NULL;
1665 struct got_worktree *worktree = NULL;
1666 char *worktree_path = NULL;
1667 struct got_object_id *commit_id = NULL;
1668 char *commit_id_str = NULL;
1669 const char *branch_name = NULL;
1670 struct got_reference *head_ref = NULL;
1671 struct got_pathlist_head paths;
1672 struct got_pathlist_entry *pe;
1673 int ch, did_something = 0;
1677 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1680 branch_name = optarg;
1683 commit_id_str = strdup(optarg);
1684 if (commit_id_str == NULL)
1685 return got_error_from_errno("strdup");
1697 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1698 "unveil", NULL) == -1)
1701 worktree_path = getcwd(NULL, 0);
1702 if (worktree_path == NULL) {
1703 error = got_error_from_errno("getcwd");
1706 error = got_worktree_open(&worktree, worktree_path);
1710 error = check_rebase_or_histedit_in_progress(worktree);
1714 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1719 error = apply_unveil(got_repo_get_path(repo), 0,
1720 got_worktree_get_root_path(worktree));
1724 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1728 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1729 got_worktree_get_head_ref_name(worktree), 0);
1732 if (commit_id_str == NULL) {
1733 error = got_ref_resolve(&commit_id, repo, head_ref);
1736 error = got_object_id_str(&commit_id_str, commit_id);
1740 error = got_repo_match_object_id(&commit_id, NULL,
1741 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1742 free(commit_id_str);
1743 commit_id_str = NULL;
1746 error = got_object_id_str(&commit_id_str, commit_id);
1752 struct got_object_id *head_commit_id;
1753 TAILQ_FOREACH(pe, &paths, entry) {
1754 if (pe->path_len == 0)
1756 error = got_error_msg(GOT_ERR_BAD_PATH,
1757 "switching between branches requires that "
1758 "the entire work tree gets updated");
1761 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1764 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1766 free(head_commit_id);
1769 error = check_same_branch(commit_id, head_ref, NULL, repo);
1772 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1776 error = check_linear_ancestry(commit_id,
1777 got_worktree_get_base_commit_id(worktree), 0, repo);
1778 if (error != NULL) {
1779 if (error->code == GOT_ERR_ANCESTRY)
1780 error = got_error(GOT_ERR_BRANCH_MOVED);
1783 error = check_same_branch(commit_id, head_ref, NULL, repo);
1788 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1790 error = got_worktree_set_base_commit_id(worktree, repo,
1796 error = got_worktree_checkout_files(worktree, &paths, repo,
1797 update_progress, &did_something, check_cancelled, NULL);
1802 printf("Updated to commit %s\n", commit_id_str);
1804 printf("Already up-to-date\n");
1806 free(worktree_path);
1807 TAILQ_FOREACH(pe, &paths, entry)
1808 free((char *)pe->path);
1809 got_pathlist_free(&paths);
1811 free(commit_id_str);
1815 static const struct got_error *
1816 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1817 const char *path, int diff_context, int ignore_whitespace,
1818 struct got_repository *repo)
1820 const struct got_error *err = NULL;
1821 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1824 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1829 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1833 while (path[0] == '/')
1835 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1836 ignore_whitespace, stdout);
1839 got_object_blob_close(blob1);
1840 got_object_blob_close(blob2);
1844 static const struct got_error *
1845 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1846 const char *path, int diff_context, int ignore_whitespace,
1847 struct got_repository *repo)
1849 const struct got_error *err = NULL;
1850 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1851 struct got_diff_blob_output_unidiff_arg arg;
1854 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1859 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1863 arg.diff_context = diff_context;
1864 arg.ignore_whitespace = ignore_whitespace;
1865 arg.outfile = stdout;
1866 while (path[0] == '/')
1868 err = got_diff_tree(tree1, tree2, path, path, repo,
1869 got_diff_blob_output_unidiff, &arg, 1);
1872 got_object_tree_close(tree1);
1874 got_object_tree_close(tree2);
1878 static const struct got_error *
1879 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1880 const char *path, int diff_context, struct got_repository *repo)
1882 const struct got_error *err = NULL;
1883 struct got_commit_object *pcommit = NULL;
1884 char *id_str1 = NULL, *id_str2 = NULL;
1885 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1886 struct got_object_qid *qid;
1888 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1890 err = got_object_open_as_commit(&pcommit, repo,
1896 if (path && path[0] != '\0') {
1898 err = got_object_id_by_path(&obj_id2, repo, id, path);
1901 err = got_object_id_str(&id_str2, obj_id2);
1907 err = got_object_id_by_path(&obj_id1, repo,
1913 err = got_object_id_str(&id_str1, obj_id1);
1919 err = got_object_get_type(&obj_type, repo, obj_id2);
1924 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1926 case GOT_OBJ_TYPE_BLOB:
1927 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1930 case GOT_OBJ_TYPE_TREE:
1931 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1935 err = got_error(GOT_ERR_OBJ_TYPE);
1941 obj_id2 = got_object_commit_get_tree_id(commit);
1942 err = got_object_id_str(&id_str2, obj_id2);
1945 obj_id1 = got_object_commit_get_tree_id(pcommit);
1946 err = got_object_id_str(&id_str1, obj_id1);
1949 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1950 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1956 got_object_commit_close(pcommit);
1961 get_datestr(time_t *time, char *datebuf)
1963 struct tm mytm, *tm;
1966 tm = gmtime_r(time, &mytm);
1969 s = asctime_r(tm, datebuf);
1972 p = strchr(s, '\n');
1978 static const struct got_error *
1979 match_logmsg(int *have_match, struct got_object_id *id,
1980 struct got_commit_object *commit, regex_t *regex)
1982 const struct got_error *err = NULL;
1983 regmatch_t regmatch;
1984 char *id_str = NULL, *logmsg = NULL;
1988 err = got_object_id_str(&id_str, id);
1992 err = got_object_commit_get_logmsg(&logmsg, commit);
1996 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
2004 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2006 static const struct got_error *
2007 print_commit(struct got_commit_object *commit, struct got_object_id *id,
2008 struct got_repository *repo, const char *path, int show_patch,
2009 int diff_context, struct got_reflist_head *refs)
2011 const struct got_error *err = NULL;
2012 char *id_str, *datestr, *logmsg0, *logmsg, *line;
2014 time_t committer_time;
2015 const char *author, *committer;
2016 char *refs_str = NULL;
2017 struct got_reflist_entry *re;
2019 SIMPLEQ_FOREACH(re, refs, entry) {
2022 struct got_tag_object *tag = NULL;
2025 name = got_ref_get_name(re->ref);
2026 if (strcmp(name, GOT_REF_HEAD) == 0)
2028 if (strncmp(name, "refs/", 5) == 0)
2030 if (strncmp(name, "got/", 4) == 0)
2032 if (strncmp(name, "heads/", 6) == 0)
2034 if (strncmp(name, "remotes/", 8) == 0)
2036 if (strncmp(name, "tags/", 5) == 0) {
2037 err = got_object_open_as_tag(&tag, repo, re->id);
2039 if (err->code != GOT_ERR_OBJ_TYPE)
2041 /* Ref points at something other than a tag. */
2046 cmp = got_object_id_cmp(tag ?
2047 got_object_tag_get_object_id(tag) : re->id, id);
2049 got_object_tag_close(tag);
2053 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2055 err = got_error_from_errno("asprintf");
2061 err = got_object_id_str(&id_str, id);
2065 printf(GOT_COMMIT_SEP_STR);
2066 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2067 refs_str ? refs_str : "", refs_str ? ")" : "");
2072 printf("from: %s\n", got_object_commit_get_author(commit));
2073 committer_time = got_object_commit_get_committer_time(commit);
2074 datestr = get_datestr(&committer_time, datebuf);
2076 printf("date: %s UTC\n", datestr);
2077 author = got_object_commit_get_author(commit);
2078 committer = got_object_commit_get_committer(commit);
2079 if (strcmp(author, committer) != 0)
2080 printf("via: %s\n", committer);
2081 if (got_object_commit_get_nparents(commit) > 1) {
2082 const struct got_object_id_queue *parent_ids;
2083 struct got_object_qid *qid;
2085 parent_ids = got_object_commit_get_parent_ids(commit);
2086 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2087 err = got_object_id_str(&id_str, qid->id);
2090 printf("parent %d: %s\n", n++, id_str);
2095 err = got_object_commit_get_logmsg(&logmsg0, commit);
2101 line = strsep(&logmsg, "\n");
2103 printf(" %s\n", line);
2108 err = print_patch(commit, id, path, diff_context, repo);
2113 if (fflush(stdout) != 0 && err == NULL)
2114 err = got_error_from_errno("fflush");
2118 static const struct got_error *
2119 print_commits(struct got_object_id *root_id, struct got_repository *repo,
2120 const char *path, int show_patch, const char *search_pattern,
2121 int diff_context, int limit, int log_branches,
2122 struct got_reflist_head *refs)
2124 const struct got_error *err;
2125 struct got_commit_graph *graph;
2129 if (search_pattern &&
2130 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2131 return got_error_msg(GOT_ERR_REGEX, search_pattern);
2133 err = got_commit_graph_open(&graph, path, !log_branches);
2136 err = got_commit_graph_iter_start(graph, root_id, repo,
2137 check_cancelled, NULL);
2141 struct got_commit_object *commit;
2142 struct got_object_id *id;
2144 if (sigint_received || sigpipe_received)
2147 err = got_commit_graph_iter_next(&id, graph, repo,
2148 check_cancelled, NULL);
2150 if (err->code == GOT_ERR_ITER_COMPLETED)
2157 err = got_object_open_as_commit(&commit, repo, id);
2161 if (search_pattern) {
2162 err = match_logmsg(&have_match, id, commit, ®ex);
2164 got_object_commit_close(commit);
2167 if (have_match == 0) {
2168 got_object_commit_close(commit);
2173 err = print_commit(commit, id, repo, path, show_patch,
2174 diff_context, refs);
2175 got_object_commit_close(commit);
2176 if (err || (limit && --limit == 0))
2182 got_commit_graph_close(graph);
2189 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2190 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2195 get_default_log_limit(void)
2197 const char *got_default_log_limit;
2201 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2202 if (got_default_log_limit == NULL)
2204 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2210 static const struct got_error *
2211 cmd_log(int argc, char *argv[])
2213 const struct got_error *error;
2214 struct got_repository *repo = NULL;
2215 struct got_worktree *worktree = NULL;
2216 struct got_commit_object *commit = NULL;
2217 struct got_object_id *id = NULL;
2218 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2219 const char *start_commit = NULL, *search_pattern = NULL;
2220 int diff_context = -1, ch;
2221 int show_patch = 0, limit = 0, log_branches = 0;
2223 struct got_reflist_head refs;
2225 SIMPLEQ_INIT(&refs);
2228 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2234 limit = get_default_log_limit();
2236 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2242 start_commit = optarg;
2245 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2248 err(1, "-C option %s", errstr);
2251 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2253 err(1, "-l option %s", errstr);
2259 repo_path = realpath(optarg, NULL);
2260 if (repo_path == NULL)
2261 return got_error_from_errno2("realpath",
2263 got_path_strip_trailing_slashes(repo_path);
2266 search_pattern = optarg;
2277 if (diff_context == -1)
2279 else if (!show_patch)
2280 errx(1, "-C reguires -p");
2282 cwd = getcwd(NULL, 0);
2284 error = got_error_from_errno("getcwd");
2288 error = got_worktree_open(&worktree, cwd);
2289 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2296 error = got_error_from_errno("strdup");
2299 } else if (argc == 1) {
2301 error = got_worktree_resolve_path(&path, worktree,
2306 path = strdup(argv[0]);
2308 error = got_error_from_errno("strdup");
2315 if (repo_path == NULL) {
2316 repo_path = worktree ?
2317 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2319 if (repo_path == NULL) {
2320 error = got_error_from_errno("strdup");
2324 error = got_repo_open(&repo, repo_path, NULL);
2328 error = apply_unveil(got_repo_get_path(repo), 1,
2329 worktree ? got_worktree_get_root_path(worktree) : NULL);
2333 if (start_commit == NULL) {
2334 struct got_reference *head_ref;
2335 error = got_ref_open(&head_ref, repo,
2336 worktree ? got_worktree_get_head_ref_name(worktree)
2340 error = got_ref_resolve(&id, repo, head_ref);
2341 got_ref_close(head_ref);
2344 error = got_object_open_as_commit(&commit, repo, id);
2346 struct got_reference *ref;
2347 error = got_ref_open(&ref, repo, start_commit, 0);
2348 if (error == NULL) {
2350 error = got_ref_resolve(&id, repo, ref);
2354 error = got_object_get_type(&obj_type, repo, id);
2357 if (obj_type == GOT_OBJ_TYPE_TAG) {
2358 struct got_tag_object *tag;
2359 error = got_object_open_as_tag(&tag, repo, id);
2362 if (got_object_tag_get_object_type(tag) !=
2363 GOT_OBJ_TYPE_COMMIT) {
2364 got_object_tag_close(tag);
2365 error = got_error(GOT_ERR_OBJ_TYPE);
2369 id = got_object_id_dup(
2370 got_object_tag_get_object_id(tag));
2372 error = got_error_from_errno(
2373 "got_object_id_dup");
2374 got_object_tag_close(tag);
2377 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2378 error = got_error(GOT_ERR_OBJ_TYPE);
2381 error = got_object_open_as_commit(&commit, repo, id);
2385 if (commit == NULL) {
2386 error = got_repo_match_object_id_prefix(&id,
2387 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2396 const char *prefix = got_worktree_get_path_prefix(worktree);
2398 if (asprintf(&p, "%s%s%s", prefix,
2399 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2400 error = got_error_from_errno("asprintf");
2403 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2406 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2411 path = in_repo_path;
2414 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2418 error = print_commits(id, repo, path, show_patch, search_pattern,
2419 diff_context, limit, log_branches, &refs);
2426 got_worktree_close(worktree);
2428 const struct got_error *repo_error;
2429 repo_error = got_repo_close(repo);
2433 got_ref_list_free(&refs);
2440 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2441 "[-w] [object1 object2 | path]\n", getprogname());
2445 struct print_diff_arg {
2446 struct got_repository *repo;
2447 struct got_worktree *worktree;
2452 int ignore_whitespace;
2455 static const struct got_error *
2456 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2457 const char *path, struct got_object_id *blob_id,
2458 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2459 int dirfd, const char *de_name)
2461 struct print_diff_arg *a = arg;
2462 const struct got_error *err = NULL;
2463 struct got_blob_object *blob1 = NULL;
2466 char *abspath = NULL, *label1 = NULL;
2469 if (a->diff_staged) {
2470 if (staged_status != GOT_STATUS_MODIFY &&
2471 staged_status != GOT_STATUS_ADD &&
2472 staged_status != GOT_STATUS_DELETE)
2475 if (staged_status == GOT_STATUS_DELETE)
2477 if (status == GOT_STATUS_NONEXISTENT)
2478 return got_error_set_errno(ENOENT, path);
2479 if (status != GOT_STATUS_MODIFY &&
2480 status != GOT_STATUS_ADD &&
2481 status != GOT_STATUS_DELETE &&
2482 status != GOT_STATUS_CONFLICT)
2486 if (!a->header_shown) {
2487 printf("diff %s %s%s\n", a->id_str,
2488 got_worktree_get_root_path(a->worktree),
2489 a->diff_staged ? " (staged changes)" : "");
2490 a->header_shown = 1;
2493 if (a->diff_staged) {
2494 const char *label1 = NULL, *label2 = NULL;
2495 switch (staged_status) {
2496 case GOT_STATUS_MODIFY:
2500 case GOT_STATUS_ADD:
2503 case GOT_STATUS_DELETE:
2507 return got_error(GOT_ERR_FILE_STATUS);
2509 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2510 label1, label2, a->diff_context, a->ignore_whitespace,
2514 if (staged_status == GOT_STATUS_ADD ||
2515 staged_status == GOT_STATUS_MODIFY) {
2517 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2521 err = got_object_id_str(&id_str, staged_blob_id);
2524 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2525 err = got_error_from_errno("asprintf");
2530 } else if (status != GOT_STATUS_ADD) {
2531 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2536 if (status != GOT_STATUS_DELETE) {
2537 if (asprintf(&abspath, "%s/%s",
2538 got_worktree_get_root_path(a->worktree), path) == -1) {
2539 err = got_error_from_errno("asprintf");
2544 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2546 err = got_error_from_errno2("openat", abspath);
2550 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2552 err = got_error_from_errno2("open", abspath);
2556 if (fstat(fd, &sb) == -1) {
2557 err = got_error_from_errno2("fstat", abspath);
2560 f2 = fdopen(fd, "r");
2562 err = got_error_from_errno2("fdopen", abspath);
2569 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2570 a->diff_context, a->ignore_whitespace, stdout);
2573 got_object_blob_close(blob1);
2574 if (f2 && fclose(f2) == EOF && err == NULL)
2575 err = got_error_from_errno("fclose");
2576 if (fd != -1 && close(fd) == -1 && err == NULL)
2577 err = got_error_from_errno("close");
2582 static const struct got_error *
2583 cmd_diff(int argc, char *argv[])
2585 const struct got_error *error;
2586 struct got_repository *repo = NULL;
2587 struct got_worktree *worktree = NULL;
2588 char *cwd = NULL, *repo_path = NULL;
2589 struct got_object_id *id1 = NULL, *id2 = NULL;
2590 const char *id_str1 = NULL, *id_str2 = NULL;
2591 char *label1 = NULL, *label2 = NULL;
2593 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2598 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2603 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2606 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2609 err(1, "-C option %s", errstr);
2612 repo_path = realpath(optarg, NULL);
2613 if (repo_path == NULL)
2614 return got_error_from_errno2("realpath",
2616 got_path_strip_trailing_slashes(repo_path);
2622 ignore_whitespace = 1;
2633 cwd = getcwd(NULL, 0);
2635 error = got_error_from_errno("getcwd");
2638 error = got_worktree_open(&worktree, cwd);
2639 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2642 if (worktree == NULL) {
2643 error = got_error(GOT_ERR_NOT_WORKTREE);
2648 "-r option can't be used when diffing a work tree");
2649 repo_path = strdup(got_worktree_get_repo_path(worktree));
2650 if (repo_path == NULL) {
2651 error = got_error_from_errno("strdup");
2655 error = got_worktree_resolve_path(&path, worktree,
2662 error = got_error_from_errno("strdup");
2666 } else if (argc == 2) {
2668 errx(1, "-s option can't be used when diffing "
2669 "objects in repository");
2672 if (worktree && repo_path == NULL) {
2674 strdup(got_worktree_get_repo_path(worktree));
2675 if (repo_path == NULL) {
2676 error = got_error_from_errno("strdup");
2683 if (repo_path == NULL) {
2684 repo_path = getcwd(NULL, 0);
2685 if (repo_path == NULL)
2686 return got_error_from_errno("getcwd");
2689 error = got_repo_open(&repo, repo_path, NULL);
2694 error = apply_unveil(got_repo_get_path(repo), 1,
2695 worktree ? got_worktree_get_root_path(worktree) : NULL);
2700 struct print_diff_arg arg;
2701 struct got_pathlist_head paths;
2706 error = got_object_id_str(&id_str,
2707 got_worktree_get_base_commit_id(worktree));
2711 arg.worktree = worktree;
2712 arg.diff_context = diff_context;
2713 arg.id_str = id_str;
2714 arg.header_shown = 0;
2715 arg.diff_staged = diff_staged;
2716 arg.ignore_whitespace = ignore_whitespace;
2718 error = got_pathlist_append(&paths, path, NULL);
2722 error = got_worktree_status(worktree, &paths, repo, print_diff,
2723 &arg, check_cancelled, NULL);
2725 got_pathlist_free(&paths);
2729 error = got_repo_match_object_id(&id1, &label1, id_str1,
2730 GOT_OBJ_TYPE_ANY, 1, repo);
2734 error = got_repo_match_object_id(&id2, &label2, id_str2,
2735 GOT_OBJ_TYPE_ANY, 1, repo);
2739 error = got_object_get_type(&type1, repo, id1);
2743 error = got_object_get_type(&type2, repo, id2);
2747 if (type1 != type2) {
2748 error = got_error(GOT_ERR_OBJ_TYPE);
2753 case GOT_OBJ_TYPE_BLOB:
2754 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2755 diff_context, ignore_whitespace, repo, stdout);
2757 case GOT_OBJ_TYPE_TREE:
2758 error = got_diff_objects_as_trees(id1, id2, "", "",
2759 diff_context, ignore_whitespace, repo, stdout);
2761 case GOT_OBJ_TYPE_COMMIT:
2762 printf("diff %s %s\n", label1, label2);
2763 error = got_diff_objects_as_commits(id1, id2, diff_context,
2764 ignore_whitespace, repo, stdout);
2767 error = got_error(GOT_ERR_OBJ_TYPE);
2776 got_worktree_close(worktree);
2778 const struct got_error *repo_error;
2779 repo_error = got_repo_close(repo);
2790 "usage: %s blame [-c commit] [-r repository-path] path\n",
2799 char datebuf[11]; /* YYYY-MM-DD + NUL */
2802 struct blame_cb_args {
2803 struct blame_line *lines;
2807 off_t *line_offsets;
2809 struct got_repository *repo;
2812 static const struct got_error *
2813 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2815 const struct got_error *err = NULL;
2816 struct blame_cb_args *a = arg;
2817 struct blame_line *bline;
2819 size_t linesize = 0;
2820 struct got_commit_object *commit = NULL;
2823 time_t committer_time;
2825 if (nlines != a->nlines ||
2826 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2827 return got_error(GOT_ERR_RANGE);
2829 if (sigint_received)
2830 return got_error(GOT_ERR_ITER_COMPLETED);
2833 return NULL; /* no change in this commit */
2835 /* Annotate this line. */
2836 bline = &a->lines[lineno - 1];
2837 if (bline->annotated)
2839 err = got_object_id_str(&bline->id_str, id);
2843 err = got_object_open_as_commit(&commit, a->repo, id);
2847 bline->committer = strdup(got_object_commit_get_committer(commit));
2848 if (bline->committer == NULL) {
2849 err = got_error_from_errno("strdup");
2853 committer_time = got_object_commit_get_committer_time(commit);
2854 if (localtime_r(&committer_time, &tm) == NULL)
2855 return got_error_from_errno("localtime_r");
2856 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2857 &tm) >= sizeof(bline->datebuf)) {
2858 err = got_error(GOT_ERR_NO_SPACE);
2861 bline->annotated = 1;
2863 /* Print lines annotated so far. */
2864 bline = &a->lines[a->lineno_cur - 1];
2865 if (!bline->annotated)
2868 offset = a->line_offsets[a->lineno_cur - 1];
2869 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2870 err = got_error_from_errno("fseeko");
2874 while (bline->annotated) {
2875 char *smallerthan, *at, *nl, *committer;
2878 if (getline(&line, &linesize, a->f) == -1) {
2880 err = got_error_from_errno("getline");
2884 committer = bline->committer;
2885 smallerthan = strchr(committer, '<');
2886 if (smallerthan && smallerthan[1] != '\0')
2887 committer = smallerthan + 1;
2888 at = strchr(committer, '@');
2891 len = strlen(committer);
2893 committer[8] = '\0';
2895 nl = strchr(line, '\n');
2898 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2899 bline->id_str, bline->datebuf, committer, line);
2902 bline = &a->lines[a->lineno_cur - 1];
2906 got_object_commit_close(commit);
2911 static const struct got_error *
2912 cmd_blame(int argc, char *argv[])
2914 const struct got_error *error;
2915 struct got_repository *repo = NULL;
2916 struct got_worktree *worktree = NULL;
2917 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2918 struct got_object_id *obj_id = NULL;
2919 struct got_object_id *commit_id = NULL;
2920 struct got_blob_object *blob = NULL;
2921 char *commit_id_str = NULL;
2922 struct blame_cb_args bca;
2923 int ch, obj_type, i;
2926 memset(&bca, 0, sizeof(bca));
2929 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2934 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2937 commit_id_str = optarg;
2940 repo_path = realpath(optarg, NULL);
2941 if (repo_path == NULL)
2942 return got_error_from_errno2("realpath",
2944 got_path_strip_trailing_slashes(repo_path);
2960 cwd = getcwd(NULL, 0);
2962 error = got_error_from_errno("getcwd");
2965 if (repo_path == NULL) {
2966 error = got_worktree_open(&worktree, cwd);
2967 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2973 strdup(got_worktree_get_repo_path(worktree));
2974 if (repo_path == NULL)
2975 error = got_error_from_errno("strdup");
2979 repo_path = strdup(cwd);
2980 if (repo_path == NULL) {
2981 error = got_error_from_errno("strdup");
2987 error = got_repo_open(&repo, repo_path, NULL);
2991 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2996 const char *prefix = got_worktree_get_path_prefix(worktree);
2997 char *p, *worktree_subdir = cwd +
2998 strlen(got_worktree_get_root_path(worktree));
2999 if (asprintf(&p, "%s%s%s%s%s",
3000 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3001 worktree_subdir, worktree_subdir[0] ? "/" : "",
3003 error = got_error_from_errno("asprintf");
3006 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3009 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3014 if (commit_id_str == NULL) {
3015 struct got_reference *head_ref;
3016 error = got_ref_open(&head_ref, repo, worktree ?
3017 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3020 error = got_ref_resolve(&commit_id, repo, head_ref);
3021 got_ref_close(head_ref);
3025 error = got_repo_match_object_id(&commit_id, NULL,
3026 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3031 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3035 error = got_object_get_type(&obj_type, repo, obj_id);
3039 if (obj_type != GOT_OBJ_TYPE_BLOB) {
3040 error = got_error(GOT_ERR_OBJ_TYPE);
3044 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3047 bca.f = got_opentemp();
3048 if (bca.f == NULL) {
3049 error = got_error_from_errno("got_opentemp");
3052 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3053 &bca.line_offsets, bca.f, blob);
3054 if (error || bca.nlines == 0)
3057 /* Don't include \n at EOF in the blame line count. */
3058 if (bca.line_offsets[bca.nlines - 1] == filesize)
3061 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3062 if (bca.lines == NULL) {
3063 error = got_error_from_errno("calloc");
3067 bca.nlines_prec = 0;
3075 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3076 check_cancelled, NULL);
3084 got_object_blob_close(blob);
3086 got_worktree_close(worktree);
3088 const struct got_error *repo_error;
3089 repo_error = got_repo_close(repo);
3094 for (i = 0; i < bca.nlines; i++) {
3095 struct blame_line *bline = &bca.lines[i];
3096 free(bline->id_str);
3097 free(bline->committer);
3101 free(bca.line_offsets);
3102 if (bca.f && fclose(bca.f) == EOF && error == NULL)
3103 error = got_error_from_errno("fclose");
3111 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3117 print_entry(struct got_tree_entry *te, const char *id, const char *path,
3118 const char *root_path)
3120 int is_root_path = (strcmp(path, root_path) == 0);
3121 const char *modestr = "";
3122 mode_t mode = got_tree_entry_get_mode(te);
3124 path += strlen(root_path);
3125 while (path[0] == '/')
3128 if (got_object_tree_entry_is_submodule(te))
3130 else if (S_ISLNK(mode))
3132 else if (S_ISDIR(mode))
3134 else if (mode & S_IXUSR)
3137 printf("%s%s%s%s%s\n", id ? id : "", path,
3138 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3141 static const struct got_error *
3142 print_tree(const char *path, struct got_object_id *commit_id,
3143 int show_ids, int recurse, const char *root_path,
3144 struct got_repository *repo)
3146 const struct got_error *err = NULL;
3147 struct got_object_id *tree_id = NULL;
3148 struct got_tree_object *tree = NULL;
3151 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3155 err = got_object_open_as_tree(&tree, repo, tree_id);
3158 nentries = got_object_tree_get_nentries(tree);
3159 for (i = 0; i < nentries; i++) {
3160 struct got_tree_entry *te;
3163 if (sigint_received || sigpipe_received)
3166 te = got_object_tree_get_entry(tree, i);
3169 err = got_object_id_str(&id_str,
3170 got_tree_entry_get_id(te));
3173 if (asprintf(&id, "%s ", id_str) == -1) {
3174 err = got_error_from_errno("asprintf");
3180 print_entry(te, id, path, root_path);
3183 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3185 if (asprintf(&child_path, "%s%s%s", path,
3186 path[0] == '/' && path[1] == '\0' ? "" : "/",
3187 got_tree_entry_get_name(te)) == -1) {
3188 err = got_error_from_errno("asprintf");
3191 err = print_tree(child_path, commit_id, show_ids, 1,
3200 got_object_tree_close(tree);
3205 static const struct got_error *
3206 cmd_tree(int argc, char *argv[])
3208 const struct got_error *error;
3209 struct got_repository *repo = NULL;
3210 struct got_worktree *worktree = NULL;
3212 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3213 struct got_object_id *commit_id = NULL;
3214 char *commit_id_str = NULL;
3215 int show_ids = 0, recurse = 0;
3219 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3224 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3227 commit_id_str = optarg;
3230 repo_path = realpath(optarg, NULL);
3231 if (repo_path == NULL)
3232 return got_error_from_errno2("realpath",
3234 got_path_strip_trailing_slashes(repo_path);
3258 cwd = getcwd(NULL, 0);
3260 error = got_error_from_errno("getcwd");
3263 if (repo_path == NULL) {
3264 error = got_worktree_open(&worktree, cwd);
3265 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3271 strdup(got_worktree_get_repo_path(worktree));
3272 if (repo_path == NULL)
3273 error = got_error_from_errno("strdup");
3277 repo_path = strdup(cwd);
3278 if (repo_path == NULL) {
3279 error = got_error_from_errno("strdup");
3285 error = got_repo_open(&repo, repo_path, NULL);
3289 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3295 char *p, *worktree_subdir = cwd +
3296 strlen(got_worktree_get_root_path(worktree));
3297 if (asprintf(&p, "%s/%s",
3298 got_worktree_get_path_prefix(worktree),
3299 worktree_subdir) == -1) {
3300 error = got_error_from_errno("asprintf");
3303 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3310 if (in_repo_path == NULL) {
3311 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3316 if (commit_id_str == NULL) {
3317 struct got_reference *head_ref;
3318 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3321 error = got_ref_resolve(&commit_id, repo, head_ref);
3322 got_ref_close(head_ref);
3326 error = got_repo_match_object_id(&commit_id, NULL,
3327 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3332 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3333 in_repo_path, repo);
3340 got_worktree_close(worktree);
3342 const struct got_error *repo_error;
3343 repo_error = got_repo_close(repo);
3353 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3357 static const struct got_error *
3358 print_status(void *arg, unsigned char status, unsigned char staged_status,
3359 const char *path, struct got_object_id *blob_id,
3360 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3361 int dirfd, const char *de_name)
3363 if (status == staged_status && (status == GOT_STATUS_DELETE))
3364 status = GOT_STATUS_NO_CHANGE;
3365 printf("%c%c %s\n", status, staged_status, path);
3369 static const struct got_error *
3370 cmd_status(int argc, char *argv[])
3372 const struct got_error *error = NULL;
3373 struct got_repository *repo = NULL;
3374 struct got_worktree *worktree = NULL;
3376 struct got_pathlist_head paths;
3377 struct got_pathlist_entry *pe;
3382 while ((ch = getopt(argc, argv, "")) != -1) {
3394 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3398 cwd = getcwd(NULL, 0);
3400 error = got_error_from_errno("getcwd");
3404 error = got_worktree_open(&worktree, cwd);
3408 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3413 error = apply_unveil(got_repo_get_path(repo), 1,
3414 got_worktree_get_root_path(worktree));
3418 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3422 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3423 check_cancelled, NULL);
3425 TAILQ_FOREACH(pe, &paths, entry)
3426 free((char *)pe->path);
3427 got_pathlist_free(&paths);
3436 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3441 static const struct got_error *
3442 list_refs(struct got_repository *repo)
3444 static const struct got_error *err = NULL;
3445 struct got_reflist_head refs;
3446 struct got_reflist_entry *re;
3448 SIMPLEQ_INIT(&refs);
3449 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3453 SIMPLEQ_FOREACH(re, &refs, entry) {
3455 refstr = got_ref_to_str(re->ref);
3457 return got_error_from_errno("got_ref_to_str");
3458 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3462 got_ref_list_free(&refs);
3466 static const struct got_error *
3467 delete_ref(struct got_repository *repo, const char *refname)
3469 const struct got_error *err = NULL;
3470 struct got_reference *ref;
3472 err = got_ref_open(&ref, repo, refname, 0);
3476 err = got_ref_delete(ref, repo);
3481 static const struct got_error *
3482 add_ref(struct got_repository *repo, const char *refname, const char *target)
3484 const struct got_error *err = NULL;
3485 struct got_object_id *id;
3486 struct got_reference *ref = NULL;
3489 * Don't let the user create a reference name with a leading '-'.
3490 * While technically a valid reference name, this case is usually
3491 * an unintended typo.
3493 if (refname[0] == '-')
3494 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3496 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3499 struct got_reference *target_ref;
3501 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3503 err = got_ref_open(&target_ref, repo, target, 0);
3506 err = got_ref_resolve(&id, repo, target_ref);
3507 got_ref_close(target_ref);
3512 err = got_ref_alloc(&ref, refname, id);
3516 err = got_ref_write(ref, repo);
3524 static const struct got_error *
3525 add_symref(struct got_repository *repo, const char *refname, const char *target)
3527 const struct got_error *err = NULL;
3528 struct got_reference *ref = NULL;
3529 struct got_reference *target_ref = NULL;
3532 * Don't let the user create a reference name with a leading '-'.
3533 * While technically a valid reference name, this case is usually
3534 * an unintended typo.
3536 if (refname[0] == '-')
3537 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3539 err = got_ref_open(&target_ref, repo, target, 0);
3543 err = got_ref_alloc_symref(&ref, refname, target_ref);
3547 err = got_ref_write(ref, repo);
3550 got_ref_close(target_ref);
3556 static const struct got_error *
3557 cmd_ref(int argc, char *argv[])
3559 const struct got_error *error = NULL;
3560 struct got_repository *repo = NULL;
3561 struct got_worktree *worktree = NULL;
3562 char *cwd = NULL, *repo_path = NULL;
3563 int ch, do_list = 0, create_symref = 0;
3564 const char *delref = NULL;
3566 /* TODO: Add -s option for adding symbolic references. */
3567 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3573 repo_path = realpath(optarg, NULL);
3574 if (repo_path == NULL)
3575 return got_error_from_errno2("realpath",
3577 got_path_strip_trailing_slashes(repo_path);
3591 if (do_list && delref)
3592 errx(1, "-l and -d options are mutually exclusive\n");
3597 if (do_list || delref) {
3599 errx(1, "-s option cannot be used together with the "
3600 "-l or -d options");
3603 } else if (argc != 2)
3608 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3612 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3613 "sendfd unveil", NULL) == -1)
3617 cwd = getcwd(NULL, 0);
3619 error = got_error_from_errno("getcwd");
3623 if (repo_path == NULL) {
3624 error = got_worktree_open(&worktree, cwd);
3625 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3631 strdup(got_worktree_get_repo_path(worktree));
3632 if (repo_path == NULL)
3633 error = got_error_from_errno("strdup");
3637 repo_path = strdup(cwd);
3638 if (repo_path == NULL) {
3639 error = got_error_from_errno("strdup");
3645 error = got_repo_open(&repo, repo_path, NULL);
3649 error = apply_unveil(got_repo_get_path(repo), do_list,
3650 worktree ? got_worktree_get_root_path(worktree) : NULL);
3655 error = list_refs(repo);
3657 error = delete_ref(repo, delref);
3658 else if (create_symref)
3659 error = add_symref(repo, argv[0], argv[1]);
3661 error = add_ref(repo, argv[0], argv[1]);
3664 got_repo_close(repo);
3666 got_worktree_close(worktree);
3676 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3677 "[name]\n", getprogname());
3681 static const struct got_error *
3682 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3683 struct got_reference *ref)
3685 const struct got_error *err = NULL;
3686 const char *refname, *marker = " ";
3689 refname = got_ref_get_name(ref);
3690 if (worktree && strcmp(refname,
3691 got_worktree_get_head_ref_name(worktree)) == 0) {
3692 struct got_object_id *id = NULL;
3694 err = got_ref_resolve(&id, repo, ref);
3697 if (got_object_id_cmp(id,
3698 got_worktree_get_base_commit_id(worktree)) == 0)
3705 if (strncmp(refname, "refs/heads/", 11) == 0)
3707 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3710 refstr = got_ref_to_str(ref);
3712 return got_error_from_errno("got_ref_to_str");
3714 printf("%s%s: %s\n", marker, refname, refstr);
3719 static const struct got_error *
3720 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3722 const char *refname;
3724 if (worktree == NULL)
3725 return got_error(GOT_ERR_NOT_WORKTREE);
3727 refname = got_worktree_get_head_ref_name(worktree);
3729 if (strncmp(refname, "refs/heads/", 11) == 0)
3731 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3734 printf("%s\n", refname);
3739 static const struct got_error *
3740 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3742 static const struct got_error *err = NULL;
3743 struct got_reflist_head refs;
3744 struct got_reflist_entry *re;
3745 struct got_reference *temp_ref = NULL;
3746 int rebase_in_progress, histedit_in_progress;
3748 SIMPLEQ_INIT(&refs);
3751 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3756 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3761 if (rebase_in_progress || histedit_in_progress) {
3762 err = got_ref_open(&temp_ref, repo,
3763 got_worktree_get_head_ref_name(worktree), 0);
3766 list_branch(repo, worktree, temp_ref);
3767 got_ref_close(temp_ref);
3771 err = got_ref_list(&refs, repo, "refs/heads",
3772 got_ref_cmp_by_name, NULL);
3776 SIMPLEQ_FOREACH(re, &refs, entry)
3777 list_branch(repo, worktree, re->ref);
3779 got_ref_list_free(&refs);
3783 static const struct got_error *
3784 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3785 const char *branch_name)
3787 const struct got_error *err = NULL;
3788 struct got_reference *ref = NULL;
3791 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3792 return got_error_from_errno("asprintf");
3794 err = got_ref_open(&ref, repo, refname, 0);
3799 strcmp(got_worktree_get_head_ref_name(worktree),
3800 got_ref_get_name(ref)) == 0) {
3801 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3802 "will not delete this work tree's current branch");
3806 err = got_ref_delete(ref, repo);
3814 static const struct got_error *
3815 add_branch(struct got_repository *repo, const char *branch_name,
3816 struct got_object_id *base_commit_id)
3818 const struct got_error *err = NULL;
3819 struct got_reference *ref = NULL;
3820 char *base_refname = NULL, *refname = NULL;
3823 * Don't let the user create a branch name with a leading '-'.
3824 * While technically a valid reference name, this case is usually
3825 * an unintended typo.
3827 if (branch_name[0] == '-')
3828 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3830 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3831 err = got_error_from_errno("asprintf");
3835 err = got_ref_open(&ref, repo, refname, 0);
3837 err = got_error(GOT_ERR_BRANCH_EXISTS);
3839 } else if (err->code != GOT_ERR_NOT_REF)
3842 err = got_ref_alloc(&ref, refname, base_commit_id);
3846 err = got_ref_write(ref, repo);
3855 static const struct got_error *
3856 cmd_branch(int argc, char *argv[])
3858 const struct got_error *error = NULL;
3859 struct got_repository *repo = NULL;
3860 struct got_worktree *worktree = NULL;
3861 char *cwd = NULL, *repo_path = NULL;
3862 int ch, do_list = 0, do_show = 0, do_update = 1;
3863 const char *delref = NULL, *commit_id_arg = NULL;
3864 struct got_reference *ref = NULL;
3865 struct got_pathlist_head paths;
3866 struct got_pathlist_entry *pe;
3867 struct got_object_id *commit_id = NULL;
3868 char *commit_id_str = NULL;
3872 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3875 commit_id_arg = optarg;
3881 repo_path = realpath(optarg, NULL);
3882 if (repo_path == NULL)
3883 return got_error_from_errno2("realpath",
3885 got_path_strip_trailing_slashes(repo_path);
3899 if (do_list && delref)
3900 errx(1, "-l and -d options are mutually exclusive\n");
3905 if (!do_list && !delref && argc == 0)
3908 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3909 errx(1, "-c option can only be used when creating a branch");
3911 if (do_list || delref) {
3914 } else if (!do_show && argc != 1)
3918 if (do_list || do_show) {
3919 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3923 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 "sendfd unveil", NULL) == -1)
3928 cwd = getcwd(NULL, 0);
3930 error = got_error_from_errno("getcwd");
3934 if (repo_path == NULL) {
3935 error = got_worktree_open(&worktree, cwd);
3936 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3942 strdup(got_worktree_get_repo_path(worktree));
3943 if (repo_path == NULL)
3944 error = got_error_from_errno("strdup");
3948 repo_path = strdup(cwd);
3949 if (repo_path == NULL) {
3950 error = got_error_from_errno("strdup");
3956 error = got_repo_open(&repo, repo_path, NULL);
3960 error = apply_unveil(got_repo_get_path(repo), do_list,
3961 worktree ? got_worktree_get_root_path(worktree) : NULL);
3966 error = show_current_branch(repo, worktree);
3968 error = list_branches(repo, worktree);
3970 error = delete_branch(repo, worktree, delref);
3972 if (commit_id_arg == NULL)
3973 commit_id_arg = worktree ?
3974 got_worktree_get_head_ref_name(worktree) :
3976 error = got_repo_match_object_id(&commit_id, NULL,
3977 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3980 error = add_branch(repo, argv[0], commit_id);
3983 if (worktree && do_update) {
3984 int did_something = 0;
3985 char *branch_refname = NULL;
3987 error = got_object_id_str(&commit_id_str, commit_id);
3990 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3994 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3996 error = got_error_from_errno("asprintf");
3999 error = got_ref_open(&ref, repo, branch_refname, 0);
4000 free(branch_refname);
4003 error = switch_head_ref(ref, commit_id, worktree,
4007 error = got_worktree_set_base_commit_id(worktree, repo,
4011 error = got_worktree_checkout_files(worktree, &paths,
4012 repo, update_progress, &did_something,
4013 check_cancelled, NULL);
4017 printf("Updated to commit %s\n", commit_id_str);
4024 got_repo_close(repo);
4026 got_worktree_close(worktree);
4030 free(commit_id_str);
4031 TAILQ_FOREACH(pe, &paths, entry)
4032 free((char *)pe->path);
4033 got_pathlist_free(&paths);
4042 "usage: %s tag [-c commit] [-r repository] [-l] "
4043 "[-m message] name\n", getprogname());
4048 static const struct got_error *
4049 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4051 const struct got_error *err = NULL;
4052 struct got_reflist_entry *re, *se, *new;
4053 struct got_object_id *re_id, *se_id;
4054 struct got_tag_object *re_tag, *se_tag;
4055 time_t re_time, se_time;
4057 SIMPLEQ_FOREACH(re, tags, entry) {
4058 se = SIMPLEQ_FIRST(sorted);
4060 err = got_reflist_entry_dup(&new, re);
4063 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4066 err = got_ref_resolve(&re_id, repo, re->ref);
4069 err = got_object_open_as_tag(&re_tag, repo, re_id);
4073 re_time = got_object_tag_get_tagger_time(re_tag);
4074 got_object_tag_close(re_tag);
4078 err = got_ref_resolve(&se_id, repo, re->ref);
4081 err = got_object_open_as_tag(&se_tag, repo, se_id);
4085 se_time = got_object_tag_get_tagger_time(se_tag);
4086 got_object_tag_close(se_tag);
4088 if (se_time > re_time) {
4089 err = got_reflist_entry_dup(&new, re);
4092 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4095 se = SIMPLEQ_NEXT(se, entry);
4104 static const struct got_error *
4105 list_tags(struct got_repository *repo, struct got_worktree *worktree)
4107 static const struct got_error *err = NULL;
4108 struct got_reflist_head refs;
4109 struct got_reflist_entry *re;
4111 SIMPLEQ_INIT(&refs);
4113 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4117 SIMPLEQ_FOREACH(re, &refs, entry) {
4118 const char *refname;
4119 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4123 struct got_object_id *id;
4124 struct got_tag_object *tag;
4125 struct got_commit_object *commit = NULL;
4127 refname = got_ref_get_name(re->ref);
4128 if (strncmp(refname, "refs/tags/", 10) != 0)
4131 refstr = got_ref_to_str(re->ref);
4132 if (refstr == NULL) {
4133 err = got_error_from_errno("got_ref_to_str");
4136 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4139 err = got_ref_resolve(&id, repo, re->ref);
4142 err = got_object_open_as_tag(&tag, repo, id);
4144 if (err->code != GOT_ERR_OBJ_TYPE) {
4148 /* "lightweight" tag */
4149 err = got_object_open_as_commit(&commit, repo, id);
4154 tagger = got_object_commit_get_committer(commit);
4156 got_object_commit_get_committer_time(commit);
4157 err = got_object_id_str(&id_str, id);
4163 tagger = got_object_tag_get_tagger(tag);
4164 tagger_time = got_object_tag_get_tagger_time(tag);
4165 err = got_object_id_str(&id_str,
4166 got_object_tag_get_object_id(tag));
4170 printf("from: %s\n", tagger);
4171 datestr = get_datestr(&tagger_time, datebuf);
4173 printf("date: %s UTC\n", datestr);
4175 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4177 switch (got_object_tag_get_object_type(tag)) {
4178 case GOT_OBJ_TYPE_BLOB:
4179 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4182 case GOT_OBJ_TYPE_TREE:
4183 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4186 case GOT_OBJ_TYPE_COMMIT:
4187 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4190 case GOT_OBJ_TYPE_TAG:
4191 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4200 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4203 got_object_commit_close(commit);
4205 tagmsg0 = strdup(got_object_tag_get_message(tag));
4206 got_object_tag_close(tag);
4207 if (tagmsg0 == NULL) {
4208 err = got_error_from_errno("strdup");
4215 line = strsep(&tagmsg, "\n");
4217 printf(" %s\n", line);
4222 got_ref_list_free(&refs);
4226 static const struct got_error *
4227 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4228 const char *tag_name, const char *repo_path)
4230 const struct got_error *err = NULL;
4231 char *template = NULL, *initial_content = NULL;
4232 char *editor = NULL;
4235 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4236 err = got_error_from_errno("asprintf");
4240 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4241 commit_id_str, tag_name) == -1) {
4242 err = got_error_from_errno("asprintf");
4246 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4250 dprintf(fd, initial_content);
4253 err = get_editor(&editor);
4256 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4258 free(initial_content);
4262 /* Editor is done; we can now apply unveil(2) */
4264 err = apply_unveil(repo_path, 0, NULL);
4273 static const struct got_error *
4274 add_tag(struct got_repository *repo, const char *tag_name,
4275 const char *commit_arg, const char *tagmsg_arg)
4277 const struct got_error *err = NULL;
4278 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4279 char *label = NULL, *commit_id_str = NULL;
4280 struct got_reference *ref = NULL;
4281 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4282 char *tagmsg_path = NULL, *tag_id_str = NULL;
4283 int preserve_tagmsg = 0;
4286 * Don't let the user create a tag name with a leading '-'.
4287 * While technically a valid reference name, this case is usually
4288 * an unintended typo.
4290 if (tag_name[0] == '-')
4291 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4293 err = get_author(&tagger, repo);
4297 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4298 GOT_OBJ_TYPE_COMMIT, 1, repo);
4302 err = got_object_id_str(&commit_id_str, commit_id);
4306 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4307 refname = strdup(tag_name);
4308 if (refname == NULL) {
4309 err = got_error_from_errno("strdup");
4313 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4314 err = got_error_from_errno("asprintf");
4318 err = got_ref_open(&ref, repo, refname, 0);
4320 err = got_error(GOT_ERR_TAG_EXISTS);
4322 } else if (err->code != GOT_ERR_NOT_REF)
4325 if (tagmsg_arg == NULL) {
4326 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4327 tag_name, got_repo_get_path(repo));
4329 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4330 tagmsg_path != NULL)
4331 preserve_tagmsg = 1;
4336 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4337 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4340 preserve_tagmsg = 1;
4344 err = got_ref_alloc(&ref, refname, tag_id);
4347 preserve_tagmsg = 1;
4351 err = got_ref_write(ref, repo);
4354 preserve_tagmsg = 1;
4358 err = got_object_id_str(&tag_id_str, tag_id);
4361 preserve_tagmsg = 1;
4364 printf("Created tag %s\n", tag_id_str);
4366 if (preserve_tagmsg) {
4367 fprintf(stderr, "%s: tag message preserved in %s\n",
4368 getprogname(), tagmsg_path);
4369 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4370 err = got_error_from_errno2("unlink", tagmsg_path);
4375 free(commit_id_str);
4383 static const struct got_error *
4384 cmd_tag(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_repository *repo = NULL;
4388 struct got_worktree *worktree = NULL;
4389 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4390 char *gitconfig_path = NULL;
4391 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4392 int ch, do_list = 0;
4394 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4397 commit_id_arg = optarg;
4403 repo_path = realpath(optarg, NULL);
4404 if (repo_path == NULL)
4405 return got_error_from_errno2("realpath",
4407 got_path_strip_trailing_slashes(repo_path);
4422 if (commit_id_arg != NULL)
4423 errx(1, "-c option can only be used when creating a tag");
4425 errx(1, "-l and -m options are mutually exclusive");
4428 } else if (argc != 1)
4435 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4439 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4440 "sendfd unveil", NULL) == -1)
4444 cwd = getcwd(NULL, 0);
4446 error = got_error_from_errno("getcwd");
4450 if (repo_path == NULL) {
4451 error = got_worktree_open(&worktree, cwd);
4452 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4458 strdup(got_worktree_get_repo_path(worktree));
4459 if (repo_path == NULL)
4460 error = got_error_from_errno("strdup");
4464 repo_path = strdup(cwd);
4465 if (repo_path == NULL) {
4466 error = got_error_from_errno("strdup");
4473 error = got_repo_open(&repo, repo_path, NULL);
4476 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4479 error = list_tags(repo, worktree);
4481 error = get_gitconfig_path(&gitconfig_path);
4484 error = got_repo_open(&repo, repo_path, gitconfig_path);
4489 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4494 if (commit_id_arg == NULL) {
4495 struct got_reference *head_ref;
4496 struct got_object_id *commit_id;
4497 error = got_ref_open(&head_ref, repo,
4498 worktree ? got_worktree_get_head_ref_name(worktree)
4502 error = got_ref_resolve(&commit_id, repo, head_ref);
4503 got_ref_close(head_ref);
4506 error = got_object_id_str(&commit_id_str, commit_id);
4512 error = add_tag(repo, tag_name,
4513 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4517 got_repo_close(repo);
4519 got_worktree_close(worktree);
4522 free(gitconfig_path);
4523 free(commit_id_str);
4530 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4535 static const struct got_error *
4536 add_progress(void *arg, unsigned char status, const char *path)
4538 while (path[0] == '/')
4540 printf("%c %s\n", status, path);
4544 static const struct got_error *
4545 cmd_add(int argc, char *argv[])
4547 const struct got_error *error = NULL;
4548 struct got_repository *repo = NULL;
4549 struct got_worktree *worktree = NULL;
4551 struct got_pathlist_head paths;
4552 struct got_pathlist_entry *pe;
4553 int ch, can_recurse = 0, no_ignores = 0;
4557 while ((ch = getopt(argc, argv, "IR")) != -1) {
4575 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4582 cwd = getcwd(NULL, 0);
4584 error = got_error_from_errno("getcwd");
4588 error = got_worktree_open(&worktree, cwd);
4592 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4597 error = apply_unveil(got_repo_get_path(repo), 1,
4598 got_worktree_get_root_path(worktree));
4602 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4606 if (!can_recurse && no_ignores) {
4607 error = got_error_msg(GOT_ERR_BAD_PATH,
4608 "disregarding ignores requires -R option");
4616 TAILQ_FOREACH(pe, &paths, entry) {
4617 if (asprintf(&ondisk_path, "%s/%s",
4618 got_worktree_get_root_path(worktree),
4620 error = got_error_from_errno("asprintf");
4623 if (lstat(ondisk_path, &sb) == -1) {
4624 if (errno == ENOENT) {
4628 error = got_error_from_errno2("lstat",
4634 if (S_ISDIR(sb.st_mode)) {
4635 error = got_error_msg(GOT_ERR_BAD_PATH,
4636 "adding directories requires -R option");
4642 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4643 NULL, repo, no_ignores);
4646 got_repo_close(repo);
4648 got_worktree_close(worktree);
4649 TAILQ_FOREACH(pe, &paths, entry)
4650 free((char *)pe->path);
4651 got_pathlist_free(&paths);
4659 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4664 static const struct got_error *
4665 print_remove_status(void *arg, unsigned char status,
4666 unsigned char staged_status, const char *path)
4668 while (path[0] == '/')
4670 if (status == GOT_STATUS_NONEXISTENT)
4672 if (status == staged_status && (status == GOT_STATUS_DELETE))
4673 status = GOT_STATUS_NO_CHANGE;
4674 printf("%c%c %s\n", status, staged_status, path);
4678 static const struct got_error *
4679 cmd_remove(int argc, char *argv[])
4681 const struct got_error *error = NULL;
4682 struct got_worktree *worktree = NULL;
4683 struct got_repository *repo = NULL;
4685 struct got_pathlist_head paths;
4686 struct got_pathlist_entry *pe;
4687 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4691 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4694 delete_local_mods = 1;
4712 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4719 cwd = getcwd(NULL, 0);
4721 error = got_error_from_errno("getcwd");
4724 error = got_worktree_open(&worktree, cwd);
4728 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4733 error = apply_unveil(got_repo_get_path(repo), 1,
4734 got_worktree_get_root_path(worktree));
4738 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4745 TAILQ_FOREACH(pe, &paths, entry) {
4746 if (asprintf(&ondisk_path, "%s/%s",
4747 got_worktree_get_root_path(worktree),
4749 error = got_error_from_errno("asprintf");
4752 if (lstat(ondisk_path, &sb) == -1) {
4753 if (errno == ENOENT) {
4757 error = got_error_from_errno2("lstat",
4763 if (S_ISDIR(sb.st_mode)) {
4764 error = got_error_msg(GOT_ERR_BAD_PATH,
4765 "removing directories requires -R option");
4771 error = got_worktree_schedule_delete(worktree, &paths,
4772 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4775 got_repo_close(repo);
4777 got_worktree_close(worktree);
4778 TAILQ_FOREACH(pe, &paths, entry)
4779 free((char *)pe->path);
4780 got_pathlist_free(&paths);
4788 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4789 "path ...\n", getprogname());
4793 static const struct got_error *
4794 revert_progress(void *arg, unsigned char status, const char *path)
4796 if (status == GOT_STATUS_UNVERSIONED)
4799 while (path[0] == '/')
4801 printf("%c %s\n", status, path);
4805 struct choose_patch_arg {
4806 FILE *patch_script_file;
4810 static const struct got_error *
4811 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4812 int nchanges, const char *action)
4815 size_t linesize = 0;
4819 case GOT_STATUS_ADD:
4820 printf("A %s\n%s this addition? [y/n] ", path, action);
4822 case GOT_STATUS_DELETE:
4823 printf("D %s\n%s this deletion? [y/n] ", path, action);
4825 case GOT_STATUS_MODIFY:
4826 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4827 return got_error_from_errno("fseek");
4828 printf(GOT_COMMIT_SEP_STR);
4829 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4831 if (ferror(patch_file))
4832 return got_error_from_errno("getline");
4833 printf(GOT_COMMIT_SEP_STR);
4834 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4835 path, n, nchanges, action);
4838 return got_error_path(path, GOT_ERR_FILE_STATUS);
4844 static const struct got_error *
4845 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4846 FILE *patch_file, int n, int nchanges)
4848 const struct got_error *err = NULL;
4850 size_t linesize = 0;
4853 struct choose_patch_arg *a = arg;
4855 *choice = GOT_PATCH_CHOICE_NONE;
4857 if (a->patch_script_file) {
4859 err = show_change(status, path, patch_file, n, nchanges,
4863 linelen = getline(&line, &linesize, a->patch_script_file);
4864 if (linelen == -1) {
4865 if (ferror(a->patch_script_file))
4866 return got_error_from_errno("getline");
4869 nl = strchr(line, '\n');
4872 if (strcmp(line, "y") == 0) {
4873 *choice = GOT_PATCH_CHOICE_YES;
4875 } else if (strcmp(line, "n") == 0) {
4876 *choice = GOT_PATCH_CHOICE_NO;
4878 } else if (strcmp(line, "q") == 0 &&
4879 status == GOT_STATUS_MODIFY) {
4880 *choice = GOT_PATCH_CHOICE_QUIT;
4883 printf("invalid response '%s'\n", line);
4888 while (resp != 'y' && resp != 'n' && resp != 'q') {
4889 err = show_change(status, path, patch_file, n, nchanges,
4896 if (status == GOT_STATUS_MODIFY) {
4897 if (resp != 'y' && resp != 'n' && resp != 'q') {
4898 printf("invalid response '%c'\n", resp);
4901 } else if (resp != 'y' && resp != 'n') {
4902 printf("invalid response '%c'\n", resp);
4908 *choice = GOT_PATCH_CHOICE_YES;
4909 else if (resp == 'n')
4910 *choice = GOT_PATCH_CHOICE_NO;
4911 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4912 *choice = GOT_PATCH_CHOICE_QUIT;
4918 static const struct got_error *
4919 cmd_revert(int argc, char *argv[])
4921 const struct got_error *error = NULL;
4922 struct got_worktree *worktree = NULL;
4923 struct got_repository *repo = NULL;
4924 char *cwd = NULL, *path = NULL;
4925 struct got_pathlist_head paths;
4926 struct got_pathlist_entry *pe;
4927 int ch, can_recurse = 0, pflag = 0;
4928 FILE *patch_script_file = NULL;
4929 const char *patch_script_path = NULL;
4930 struct choose_patch_arg cpa;
4934 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4940 patch_script_path = optarg;
4955 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4956 "unveil", NULL) == -1)
4961 if (patch_script_path && !pflag)
4962 errx(1, "-F option can only be used together with -p option");
4964 cwd = getcwd(NULL, 0);
4966 error = got_error_from_errno("getcwd");
4969 error = got_worktree_open(&worktree, cwd);
4973 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4978 if (patch_script_path) {
4979 patch_script_file = fopen(patch_script_path, "r");
4980 if (patch_script_file == NULL) {
4981 error = got_error_from_errno2("fopen",
4986 error = apply_unveil(got_repo_get_path(repo), 1,
4987 got_worktree_get_root_path(worktree));
4991 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4998 TAILQ_FOREACH(pe, &paths, entry) {
4999 if (asprintf(&ondisk_path, "%s/%s",
5000 got_worktree_get_root_path(worktree),
5002 error = got_error_from_errno("asprintf");
5005 if (lstat(ondisk_path, &sb) == -1) {
5006 if (errno == ENOENT) {
5010 error = got_error_from_errno2("lstat",
5016 if (S_ISDIR(sb.st_mode)) {
5017 error = got_error_msg(GOT_ERR_BAD_PATH,
5018 "reverting directories requires -R option");
5024 cpa.patch_script_file = patch_script_file;
5025 cpa.action = "revert";
5026 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5027 pflag ? choose_patch : NULL, &cpa, repo);
5029 if (patch_script_file && fclose(patch_script_file) == EOF &&
5031 error = got_error_from_errno2("fclose", patch_script_path);
5033 got_repo_close(repo);
5035 got_worktree_close(worktree);
5044 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5049 struct collect_commit_logmsg_arg {
5050 const char *cmdline_log;
5052 const char *worktree_path;
5053 const char *branch_name;
5054 const char *repo_path;
5059 static const struct got_error *
5060 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5063 char *initial_content = NULL;
5064 struct got_pathlist_entry *pe;
5065 const struct got_error *err = NULL;
5066 char *template = NULL;
5067 struct collect_commit_logmsg_arg *a = arg;
5071 /* if a message was specified on the command line, just use it */
5072 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5073 len = strlen(a->cmdline_log) + 1;
5074 *logmsg = malloc(len + 1);
5075 if (*logmsg == NULL)
5076 return got_error_from_errno("malloc");
5077 strlcpy(*logmsg, a->cmdline_log, len);
5081 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5082 return got_error_from_errno("asprintf");
5084 if (asprintf(&initial_content,
5085 "\n# changes to be committed on branch %s:\n",
5086 a->branch_name) == -1)
5087 return got_error_from_errno("asprintf");
5089 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5093 dprintf(fd, initial_content);
5095 TAILQ_FOREACH(pe, commitable_paths, entry) {
5096 struct got_commitable *ct = pe->data;
5097 dprintf(fd, "# %c %s\n",
5098 got_commitable_get_status(ct),
5099 got_commitable_get_path(ct));
5103 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5105 free(initial_content);
5108 /* Editor is done; we can now apply unveil(2) */
5110 err = apply_unveil(a->repo_path, 0, a->worktree_path);
5119 static const struct got_error *
5120 cmd_commit(int argc, char *argv[])
5122 const struct got_error *error = NULL;
5123 struct got_worktree *worktree = NULL;
5124 struct got_repository *repo = NULL;
5125 char *cwd = NULL, *id_str = NULL;
5126 struct got_object_id *id = NULL;
5127 const char *logmsg = NULL;
5128 struct collect_commit_logmsg_arg cl_arg;
5129 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5130 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5131 struct got_pathlist_head paths;
5134 cl_arg.logmsg_path = NULL;
5136 while ((ch = getopt(argc, argv, "m:")) != -1) {
5151 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5152 "unveil", NULL) == -1)
5155 cwd = getcwd(NULL, 0);
5157 error = got_error_from_errno("getcwd");
5160 error = got_worktree_open(&worktree, cwd);
5164 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5167 if (rebase_in_progress) {
5168 error = got_error(GOT_ERR_REBASING);
5172 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5177 error = get_gitconfig_path(&gitconfig_path);
5180 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5185 error = get_author(&author, repo);
5190 * unveil(2) traverses exec(2); if an editor is used we have
5191 * to apply unveil after the log message has been written.
5193 if (logmsg == NULL || strlen(logmsg) == 0)
5194 error = get_editor(&editor);
5196 error = apply_unveil(got_repo_get_path(repo), 0,
5197 got_worktree_get_root_path(worktree));
5201 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5205 cl_arg.editor = editor;
5206 cl_arg.cmdline_log = logmsg;
5207 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5208 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5209 if (!histedit_in_progress) {
5210 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5211 error = got_error(GOT_ERR_COMMIT_BRANCH);
5214 cl_arg.branch_name += 11;
5216 cl_arg.repo_path = got_repo_get_path(repo);
5217 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5218 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5220 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5221 cl_arg.logmsg_path != NULL)
5222 preserve_logmsg = 1;
5226 error = got_object_id_str(&id_str, id);
5229 printf("Created commit %s\n", id_str);
5231 if (preserve_logmsg) {
5232 fprintf(stderr, "%s: log message preserved in %s\n",
5233 getprogname(), cl_arg.logmsg_path);
5234 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5236 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5237 free(cl_arg.logmsg_path);
5239 got_repo_close(repo);
5241 got_worktree_close(worktree);
5244 free(gitconfig_path);
5251 usage_cherrypick(void)
5253 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5257 static const struct got_error *
5258 cmd_cherrypick(int argc, char *argv[])
5260 const struct got_error *error = NULL;
5261 struct got_worktree *worktree = NULL;
5262 struct got_repository *repo = NULL;
5263 char *cwd = NULL, *commit_id_str = NULL;
5264 struct got_object_id *commit_id = NULL;
5265 struct got_commit_object *commit = NULL;
5266 struct got_object_qid *pid;
5267 struct got_reference *head_ref = NULL;
5268 int ch, did_something = 0;
5270 while ((ch = getopt(argc, argv, "")) != -1) {
5282 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5283 "unveil", NULL) == -1)
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 error = apply_unveil(got_repo_get_path(repo), 0,
5304 got_worktree_get_root_path(worktree));
5308 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5309 GOT_OBJ_TYPE_COMMIT, repo);
5310 if (error != NULL) {
5311 struct got_reference *ref;
5312 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5314 error = got_ref_open(&ref, repo, argv[0], 0);
5317 error = got_ref_resolve(&commit_id, repo, ref);
5322 error = got_object_id_str(&commit_id_str, commit_id);
5326 error = got_ref_open(&head_ref, repo,
5327 got_worktree_get_head_ref_name(worktree), 0);
5331 error = check_same_branch(commit_id, head_ref, NULL, repo);
5333 if (error->code != GOT_ERR_ANCESTRY)
5337 error = got_error(GOT_ERR_SAME_BRANCH);
5341 error = got_object_open_as_commit(&commit, repo, commit_id);
5344 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5345 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5346 commit_id, repo, update_progress, &did_something, check_cancelled,
5352 printf("Merged commit %s\n", commit_id_str);
5355 got_object_commit_close(commit);
5356 free(commit_id_str);
5358 got_ref_close(head_ref);
5360 got_worktree_close(worktree);
5362 got_repo_close(repo);
5369 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5373 static const struct got_error *
5374 cmd_backout(int argc, char *argv[])
5376 const struct got_error *error = NULL;
5377 struct got_worktree *worktree = NULL;
5378 struct got_repository *repo = NULL;
5379 char *cwd = NULL, *commit_id_str = NULL;
5380 struct got_object_id *commit_id = NULL;
5381 struct got_commit_object *commit = NULL;
5382 struct got_object_qid *pid;
5383 struct got_reference *head_ref = NULL;
5384 int ch, did_something = 0;
5386 while ((ch = getopt(argc, argv, "")) != -1) {
5398 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5399 "unveil", NULL) == -1)
5405 cwd = getcwd(NULL, 0);
5407 error = got_error_from_errno("getcwd");
5410 error = got_worktree_open(&worktree, cwd);
5414 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5419 error = apply_unveil(got_repo_get_path(repo), 0,
5420 got_worktree_get_root_path(worktree));
5424 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5425 GOT_OBJ_TYPE_COMMIT, repo);
5426 if (error != NULL) {
5427 struct got_reference *ref;
5428 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5430 error = got_ref_open(&ref, repo, argv[0], 0);
5433 error = got_ref_resolve(&commit_id, repo, ref);
5438 error = got_object_id_str(&commit_id_str, commit_id);
5442 error = got_ref_open(&head_ref, repo,
5443 got_worktree_get_head_ref_name(worktree), 0);
5447 error = check_same_branch(commit_id, head_ref, NULL, repo);
5451 error = got_object_open_as_commit(&commit, repo, commit_id);
5454 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5456 error = got_error(GOT_ERR_ROOT_COMMIT);
5460 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5461 update_progress, &did_something, check_cancelled, NULL);
5466 printf("Backed out commit %s\n", commit_id_str);
5469 got_object_commit_close(commit);
5470 free(commit_id_str);
5472 got_ref_close(head_ref);
5474 got_worktree_close(worktree);
5476 got_repo_close(repo);
5483 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5489 trim_logmsg(char *logmsg, int limit)
5494 len = strlen(logmsg);
5498 nl = strchr(logmsg, '\n');
5503 static const struct got_error *
5504 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5506 const struct got_error *err;
5507 char *logmsg0 = NULL;
5510 err = got_object_commit_get_logmsg(&logmsg0, commit);
5515 while (isspace((unsigned char)s[0]))
5518 *logmsg = strdup(s);
5519 if (*logmsg == NULL) {
5520 err = got_error_from_errno("strdup");
5524 trim_logmsg(*logmsg, limit);
5530 static const struct got_error *
5531 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5533 const struct got_error *err;
5534 struct got_commit_object *commit = NULL;
5535 char *id_str = NULL, *logmsg = NULL;
5537 err = got_object_open_as_commit(&commit, repo, id);
5541 err = got_object_id_str(&id_str, id);
5547 err = get_short_logmsg(&logmsg, 42, commit);
5551 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5554 got_object_commit_close(commit);
5559 static const struct got_error *
5560 show_rebase_progress(struct got_commit_object *commit,
5561 struct got_object_id *old_id, struct got_object_id *new_id)
5563 const struct got_error *err;
5564 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5566 err = got_object_id_str(&old_id_str, old_id);
5571 err = got_object_id_str(&new_id_str, new_id);
5576 old_id_str[12] = '\0';
5578 new_id_str[12] = '\0';
5580 err = get_short_logmsg(&logmsg, 42, commit);
5584 printf("%s -> %s: %s\n", old_id_str,
5585 new_id_str ? new_id_str : "no-op change", logmsg);
5593 static const struct got_error *
5594 rebase_progress(void *arg, unsigned char status, const char *path)
5596 unsigned char *rebase_status = arg;
5598 while (path[0] == '/')
5600 printf("%c %s\n", status, path);
5602 if (*rebase_status == GOT_STATUS_CONFLICT)
5604 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5605 *rebase_status = status;
5609 static const struct got_error *
5610 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5611 struct got_reference *branch, struct got_reference *new_base_branch,
5612 struct got_reference *tmp_branch, struct got_repository *repo)
5614 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5615 return got_worktree_rebase_complete(worktree, fileindex,
5616 new_base_branch, tmp_branch, branch, repo);
5619 static const struct got_error *
5620 rebase_commit(struct got_pathlist_head *merged_paths,
5621 struct got_worktree *worktree, struct got_fileindex *fileindex,
5622 struct got_reference *tmp_branch,
5623 struct got_object_id *commit_id, struct got_repository *repo)
5625 const struct got_error *error;
5626 struct got_commit_object *commit;
5627 struct got_object_id *new_commit_id;
5629 error = got_object_open_as_commit(&commit, repo, commit_id);
5633 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5634 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5636 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5638 error = show_rebase_progress(commit, commit_id, NULL);
5640 error = show_rebase_progress(commit, commit_id, new_commit_id);
5641 free(new_commit_id);
5644 got_object_commit_close(commit);
5648 struct check_path_prefix_arg {
5649 const char *path_prefix;
5654 static const struct got_error *
5655 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5656 struct got_blob_object *blob2, struct got_object_id *id1,
5657 struct got_object_id *id2, const char *path1, const char *path2,
5658 mode_t mode1, mode_t mode2, struct got_repository *repo)
5660 struct check_path_prefix_arg *a = arg;
5662 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5663 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5664 return got_error(a->errcode);
5669 static const struct got_error *
5670 check_path_prefix(struct got_object_id *parent_id,
5671 struct got_object_id *commit_id, const char *path_prefix,
5672 int errcode, struct got_repository *repo)
5674 const struct got_error *err;
5675 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5676 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5677 struct check_path_prefix_arg cpp_arg;
5679 if (got_path_is_root_dir(path_prefix))
5682 err = got_object_open_as_commit(&commit, repo, commit_id);
5686 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5690 err = got_object_open_as_tree(&tree1, repo,
5691 got_object_commit_get_tree_id(parent_commit));
5695 err = got_object_open_as_tree(&tree2, repo,
5696 got_object_commit_get_tree_id(commit));
5700 cpp_arg.path_prefix = path_prefix;
5701 while (cpp_arg.path_prefix[0] == '/')
5702 cpp_arg.path_prefix++;
5703 cpp_arg.len = strlen(cpp_arg.path_prefix);
5704 cpp_arg.errcode = errcode;
5705 err = got_diff_tree(tree1, tree2, "", "", repo,
5706 check_path_prefix_in_diff, &cpp_arg, 0);
5709 got_object_tree_close(tree1);
5711 got_object_tree_close(tree2);
5713 got_object_commit_close(commit);
5715 got_object_commit_close(parent_commit);
5719 static const struct got_error *
5720 collect_commits(struct got_object_id_queue *commits,
5721 struct got_object_id *initial_commit_id,
5722 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5723 const char *path_prefix, int path_prefix_errcode,
5724 struct got_repository *repo)
5726 const struct got_error *err = NULL;
5727 struct got_commit_graph *graph = NULL;
5728 struct got_object_id *parent_id = NULL;
5729 struct got_object_qid *qid;
5730 struct got_object_id *commit_id = initial_commit_id;
5732 err = got_commit_graph_open(&graph, "/", 1);
5736 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5737 check_cancelled, NULL);
5740 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5741 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5742 check_cancelled, NULL);
5744 if (err->code == GOT_ERR_ITER_COMPLETED) {
5745 err = got_error_msg(GOT_ERR_ANCESTRY,
5746 "ran out of commits to rebase before "
5747 "youngest common ancestor commit has "
5752 err = check_path_prefix(parent_id, commit_id,
5753 path_prefix, path_prefix_errcode, repo);
5757 err = got_object_qid_alloc(&qid, commit_id);
5760 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5761 commit_id = parent_id;
5765 got_commit_graph_close(graph);
5769 static const struct got_error *
5770 cmd_rebase(int argc, char *argv[])
5772 const struct got_error *error = NULL;
5773 struct got_worktree *worktree = NULL;
5774 struct got_repository *repo = NULL;
5775 struct got_fileindex *fileindex = NULL;
5777 struct got_reference *branch = NULL;
5778 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5779 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5780 struct got_object_id *resume_commit_id = NULL;
5781 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5782 struct got_commit_object *commit = NULL;
5783 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5784 int histedit_in_progress = 0;
5785 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5786 struct got_object_id_queue commits;
5787 struct got_pathlist_head merged_paths;
5788 const struct got_object_id_queue *parent_ids;
5789 struct got_object_qid *qid, *pid;
5791 SIMPLEQ_INIT(&commits);
5792 TAILQ_INIT(&merged_paths);
5794 while ((ch = getopt(argc, argv, "ac")) != -1) {
5800 continue_rebase = 1;
5812 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5813 "unveil", NULL) == -1)
5816 if (abort_rebase && continue_rebase)
5818 else if (abort_rebase || continue_rebase) {
5821 } else if (argc != 1)
5824 cwd = getcwd(NULL, 0);
5826 error = got_error_from_errno("getcwd");
5829 error = got_worktree_open(&worktree, cwd);
5833 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5838 error = apply_unveil(got_repo_get_path(repo), 0,
5839 got_worktree_get_root_path(worktree));
5843 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5847 if (histedit_in_progress) {
5848 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5852 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5858 if (!rebase_in_progress) {
5859 error = got_error(GOT_ERR_NOT_REBASING);
5862 error = got_worktree_rebase_continue(&resume_commit_id,
5863 &new_base_branch, &tmp_branch, &branch, &fileindex,
5867 printf("Switching work tree to %s\n",
5868 got_ref_get_symref_target(new_base_branch));
5869 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5870 new_base_branch, update_progress, &did_something);
5873 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5874 goto done; /* nothing else to do */
5877 if (continue_rebase) {
5878 if (!rebase_in_progress) {
5879 error = got_error(GOT_ERR_NOT_REBASING);
5882 error = got_worktree_rebase_continue(&resume_commit_id,
5883 &new_base_branch, &tmp_branch, &branch, &fileindex,
5888 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5889 resume_commit_id, repo);
5893 yca_id = got_object_id_dup(resume_commit_id);
5894 if (yca_id == NULL) {
5895 error = got_error_from_errno("got_object_id_dup");
5899 error = got_ref_open(&branch, repo, argv[0], 0);
5904 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5908 if (!continue_rebase) {
5909 struct got_object_id *base_commit_id;
5911 base_commit_id = got_worktree_get_base_commit_id(worktree);
5912 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5913 base_commit_id, branch_head_commit_id, repo,
5914 check_cancelled, NULL);
5917 if (yca_id == NULL) {
5918 error = got_error_msg(GOT_ERR_ANCESTRY,
5919 "specified branch shares no common ancestry "
5920 "with work tree's branch");
5924 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5926 if (error->code != GOT_ERR_ANCESTRY)
5930 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5931 "specified branch resolves to a commit which "
5932 "is already contained in work tree's branch");
5935 error = got_worktree_rebase_prepare(&new_base_branch,
5936 &tmp_branch, &fileindex, worktree, branch, repo);
5941 commit_id = branch_head_commit_id;
5942 error = got_object_open_as_commit(&commit, repo, commit_id);
5946 parent_ids = got_object_commit_get_parent_ids(commit);
5947 pid = SIMPLEQ_FIRST(parent_ids);
5949 if (!continue_rebase) {
5951 error = got_worktree_rebase_abort(worktree, fileindex,
5952 repo, new_base_branch, update_progress,
5956 printf("Rebase of %s aborted\n",
5957 got_ref_get_name(branch));
5959 error = got_error(GOT_ERR_EMPTY_REBASE);
5962 error = collect_commits(&commits, commit_id, pid->id,
5963 yca_id, got_worktree_get_path_prefix(worktree),
5964 GOT_ERR_REBASE_PATH, repo);
5965 got_object_commit_close(commit);
5970 if (SIMPLEQ_EMPTY(&commits)) {
5971 if (continue_rebase) {
5972 error = rebase_complete(worktree, fileindex,
5973 branch, new_base_branch, tmp_branch, repo);
5976 /* Fast-forward the reference of the branch. */
5977 struct got_object_id *new_head_commit_id;
5979 error = got_ref_resolve(&new_head_commit_id, repo,
5983 error = got_object_id_str(&id_str, new_head_commit_id);
5984 printf("Forwarding %s to commit %s\n",
5985 got_ref_get_name(branch), id_str);
5987 error = got_ref_change_ref(branch,
5988 new_head_commit_id);
5995 SIMPLEQ_FOREACH(qid, &commits, entry) {
5996 commit_id = qid->id;
5997 parent_id = pid ? pid->id : yca_id;
6000 error = got_worktree_rebase_merge_files(&merged_paths,
6001 worktree, fileindex, parent_id, commit_id, repo,
6002 rebase_progress, &rebase_status, check_cancelled, NULL);
6006 if (rebase_status == GOT_STATUS_CONFLICT) {
6007 error = show_rebase_merge_conflict(qid->id, repo);
6010 got_worktree_rebase_pathlist_free(&merged_paths);
6014 error = rebase_commit(&merged_paths, worktree, fileindex,
6015 tmp_branch, commit_id, repo);
6016 got_worktree_rebase_pathlist_free(&merged_paths);
6021 if (rebase_status == GOT_STATUS_CONFLICT) {
6022 error = got_worktree_rebase_postpone(worktree, fileindex);
6025 error = got_error_msg(GOT_ERR_CONFLICTS,
6026 "conflicts must be resolved before rebasing can continue");
6028 error = rebase_complete(worktree, fileindex, branch,
6029 new_base_branch, tmp_branch, repo);
6031 got_object_id_queue_free(&commits);
6032 free(branch_head_commit_id);
6033 free(resume_commit_id);
6036 got_object_commit_close(commit);
6038 got_ref_close(branch);
6039 if (new_base_branch)
6040 got_ref_close(new_base_branch);
6042 got_ref_close(tmp_branch);
6044 got_worktree_close(worktree);
6046 got_repo_close(repo);
6051 usage_histedit(void)
6053 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6058 #define GOT_HISTEDIT_PICK 'p'
6059 #define GOT_HISTEDIT_EDIT 'e'
6060 #define GOT_HISTEDIT_FOLD 'f'
6061 #define GOT_HISTEDIT_DROP 'd'
6062 #define GOT_HISTEDIT_MESG 'm'
6064 static struct got_histedit_cmd {
6068 } got_histedit_cmds[] = {
6069 { GOT_HISTEDIT_PICK, "pick", "use commit" },
6070 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6071 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6073 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6074 { GOT_HISTEDIT_MESG, "mesg",
6075 "single-line log message for commit above (open editor if empty)" },
6078 struct got_histedit_list_entry {
6079 TAILQ_ENTRY(got_histedit_list_entry) entry;
6080 struct got_object_id *commit_id;
6081 const struct got_histedit_cmd *cmd;
6084 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6086 static const struct got_error *
6087 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6088 FILE *f, struct got_repository *repo)
6090 const struct got_error *err = NULL;
6091 char *logmsg = NULL, *id_str = NULL;
6092 struct got_commit_object *commit = NULL;
6095 err = got_object_open_as_commit(&commit, repo, commit_id);
6099 err = get_short_logmsg(&logmsg, 34, commit);
6103 err = got_object_id_str(&id_str, commit_id);
6107 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6109 err = got_ferror(f, GOT_ERR_IO);
6112 got_object_commit_close(commit);
6118 static const struct got_error *
6119 histedit_write_commit_list(struct got_object_id_queue *commits,
6120 FILE *f, int edit_logmsg_only, struct got_repository *repo)
6122 const struct got_error *err = NULL;
6123 struct got_object_qid *qid;
6125 if (SIMPLEQ_EMPTY(commits))
6126 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6128 SIMPLEQ_FOREACH(qid, commits, entry) {
6129 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6133 if (edit_logmsg_only) {
6134 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6136 err = got_ferror(f, GOT_ERR_IO);
6145 static const struct got_error *
6146 write_cmd_list(FILE *f, const char *branch_name,
6147 struct got_object_id_queue *commits)
6149 const struct got_error *err = NULL;
6152 struct got_object_qid *qid;
6154 qid = SIMPLEQ_FIRST(commits);
6155 err = got_object_id_str(&id_str, qid->id);
6160 "# Editing the history of branch '%s' starting at\n"
6162 "# Commits will be processed in order from top to "
6163 "bottom of this file.\n", branch_name, id_str);
6165 err = got_ferror(f, GOT_ERR_IO);
6169 n = fprintf(f, "# Available histedit commands:\n");
6171 err = got_ferror(f, GOT_ERR_IO);
6175 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6176 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6177 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6180 err = got_ferror(f, GOT_ERR_IO);
6189 static const struct got_error *
6190 histedit_syntax_error(int lineno)
6192 static char msg[42];
6195 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6197 if (ret == -1 || ret >= sizeof(msg))
6198 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6200 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6203 static const struct got_error *
6204 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6205 char *logmsg, struct got_repository *repo)
6207 const struct got_error *err;
6208 struct got_commit_object *folded_commit = NULL;
6209 char *id_str, *folded_logmsg = NULL;
6211 err = got_object_id_str(&id_str, hle->commit_id);
6215 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6219 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6222 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6223 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6224 folded_logmsg) == -1) {
6225 err = got_error_from_errno("asprintf");
6229 got_object_commit_close(folded_commit);
6231 free(folded_logmsg);
6235 static struct got_histedit_list_entry *
6236 get_folded_commits(struct got_histedit_list_entry *hle)
6238 struct got_histedit_list_entry *prev, *folded = NULL;
6240 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6241 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6242 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6243 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6245 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6251 static const struct got_error *
6252 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6253 struct got_repository *repo)
6255 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6256 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6257 const struct got_error *err = NULL;
6258 struct got_commit_object *commit = NULL;
6260 struct got_histedit_list_entry *folded = NULL;
6262 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6266 folded = get_folded_commits(hle);
6268 while (folded != hle) {
6269 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6270 folded = TAILQ_NEXT(folded, entry);
6273 err = append_folded_commit_msg(&new_msg, folded,
6279 folded = TAILQ_NEXT(folded, entry);
6283 err = got_object_id_str(&id_str, hle->commit_id);
6286 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6289 if (asprintf(&new_msg,
6290 "%s\n# original log message of commit %s: %s",
6291 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6292 err = got_error_from_errno("asprintf");
6298 err = got_object_id_str(&id_str, hle->commit_id);
6302 err = got_opentemp_named_fd(&logmsg_path, &fd,
6303 GOT_TMPDIR_STR "/got-logmsg");
6307 dprintf(fd, logmsg);
6310 err = get_editor(&editor);
6314 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6316 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6318 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6321 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6322 err = got_error_from_errno2("unlink", logmsg_path);
6328 got_object_commit_close(commit);
6332 static const struct got_error *
6333 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6334 FILE *f, struct got_repository *repo)
6336 const struct got_error *err = NULL;
6337 char *line = NULL, *p, *end;
6341 const struct got_histedit_cmd *cmd;
6342 struct got_object_id *commit_id = NULL;
6343 struct got_histedit_list_entry *hle = NULL;
6346 len = getline(&line, &size, f);
6348 const struct got_error *getline_err;
6351 getline_err = got_error_from_errno("getline");
6352 err = got_ferror(f, getline_err->code);
6357 while (isspace((unsigned char)p[0]))
6359 if (p[0] == '#' || p[0] == '\0') {
6365 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6366 cmd = &got_histedit_cmds[i];
6367 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6368 isspace((unsigned char)p[strlen(cmd->name)])) {
6369 p += strlen(cmd->name);
6372 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6377 if (i == nitems(got_histedit_cmds)) {
6378 err = histedit_syntax_error(lineno);
6381 while (isspace((unsigned char)p[0]))
6383 if (cmd->code == GOT_HISTEDIT_MESG) {
6384 if (hle == NULL || hle->logmsg != NULL) {
6385 err = got_error(GOT_ERR_HISTEDIT_CMD);
6389 err = histedit_edit_logmsg(hle, repo);
6393 hle->logmsg = strdup(p);
6394 if (hle->logmsg == NULL) {
6395 err = got_error_from_errno("strdup");
6404 while (end[0] && !isspace((unsigned char)end[0]))
6408 err = got_object_resolve_id_str(&commit_id, repo, p);
6410 /* override error code */
6411 err = histedit_syntax_error(lineno);
6415 hle = malloc(sizeof(*hle));
6417 err = got_error_from_errno("malloc");
6421 hle->commit_id = commit_id;
6426 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6434 static const struct got_error *
6435 histedit_check_script(struct got_histedit_list *histedit_cmds,
6436 struct got_object_id_queue *commits, struct got_repository *repo)
6438 const struct got_error *err = NULL;
6439 struct got_object_qid *qid;
6440 struct got_histedit_list_entry *hle;
6441 static char msg[92];
6444 if (TAILQ_EMPTY(histedit_cmds))
6445 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6446 "histedit script contains no commands");
6447 if (SIMPLEQ_EMPTY(commits))
6448 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6450 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6451 struct got_histedit_list_entry *hle2;
6452 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6455 if (got_object_id_cmp(hle->commit_id,
6456 hle2->commit_id) != 0)
6458 err = got_object_id_str(&id_str, hle->commit_id);
6461 snprintf(msg, sizeof(msg), "commit %s is listed "
6462 "more than once in histedit script", id_str);
6464 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6468 SIMPLEQ_FOREACH(qid, commits, entry) {
6469 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6470 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6474 err = got_object_id_str(&id_str, qid->id);
6477 snprintf(msg, sizeof(msg),
6478 "commit %s missing from histedit script", id_str);
6480 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6484 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6485 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6486 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6487 "last commit in histedit script cannot be folded");
6492 static const struct got_error *
6493 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6494 const char *path, struct got_object_id_queue *commits,
6495 struct got_repository *repo)
6497 const struct got_error *err = NULL;
6501 err = get_editor(&editor);
6505 if (spawn_editor(editor, path) == -1) {
6506 err = got_error_from_errno("failed spawning editor");
6510 f = fopen(path, "r");
6512 err = got_error_from_errno("fopen");
6515 err = histedit_parse_list(histedit_cmds, f, repo);
6519 err = histedit_check_script(histedit_cmds, commits, repo);
6521 if (f && fclose(f) != 0 && err == NULL)
6522 err = got_error_from_errno("fclose");
6527 static const struct got_error *
6528 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6529 struct got_object_id_queue *, const char *, const char *,
6530 struct got_repository *);
6532 static const struct got_error *
6533 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6534 struct got_object_id_queue *commits, const char *branch_name,
6535 int edit_logmsg_only, struct got_repository *repo)
6537 const struct got_error *err;
6541 err = got_opentemp_named(&path, &f, "got-histedit");
6545 err = write_cmd_list(f, branch_name, commits);
6549 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6553 if (edit_logmsg_only) {
6555 err = histedit_parse_list(histedit_cmds, f, repo);
6557 if (fclose(f) != 0) {
6558 err = got_error_from_errno("fclose");
6562 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6564 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6565 err->code != GOT_ERR_HISTEDIT_CMD)
6567 err = histedit_edit_list_retry(histedit_cmds, err,
6568 commits, path, branch_name, repo);
6572 if (f && fclose(f) != 0 && err == NULL)
6573 err = got_error_from_errno("fclose");
6574 if (path && unlink(path) != 0 && err == NULL)
6575 err = got_error_from_errno2("unlink", path);
6580 static const struct got_error *
6581 histedit_save_list(struct got_histedit_list *histedit_cmds,
6582 struct got_worktree *worktree, struct got_repository *repo)
6584 const struct got_error *err = NULL;
6587 struct got_histedit_list_entry *hle;
6588 struct got_commit_object *commit = NULL;
6590 err = got_worktree_get_histedit_script_path(&path, worktree);
6594 f = fopen(path, "w");
6596 err = got_error_from_errno2("fopen", path);
6599 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6600 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6606 int n = fprintf(f, "%c %s\n",
6607 GOT_HISTEDIT_MESG, hle->logmsg);
6609 err = got_ferror(f, GOT_ERR_IO);
6615 if (f && fclose(f) != 0 && err == NULL)
6616 err = got_error_from_errno("fclose");
6619 got_object_commit_close(commit);
6624 histedit_free_list(struct got_histedit_list *histedit_cmds)
6626 struct got_histedit_list_entry *hle;
6628 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6629 TAILQ_REMOVE(histedit_cmds, hle, entry);
6634 static const struct got_error *
6635 histedit_load_list(struct got_histedit_list *histedit_cmds,
6636 const char *path, struct got_repository *repo)
6638 const struct got_error *err = NULL;
6641 f = fopen(path, "r");
6643 err = got_error_from_errno2("fopen", path);
6647 err = histedit_parse_list(histedit_cmds, f, repo);
6649 if (f && fclose(f) != 0 && err == NULL)
6650 err = got_error_from_errno("fclose");
6654 static const struct got_error *
6655 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6656 const struct got_error *edit_err, struct got_object_id_queue *commits,
6657 const char *path, const char *branch_name, struct got_repository *repo)
6659 const struct got_error *err = NULL, *prev_err = edit_err;
6662 while (resp != 'c' && resp != 'r' && resp != 'a') {
6663 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6664 "or (a)bort: ", getprogname(), prev_err->msg);
6669 histedit_free_list(histedit_cmds);
6670 err = histedit_run_editor(histedit_cmds, path, commits,
6673 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6674 err->code != GOT_ERR_HISTEDIT_CMD)
6681 } else if (resp == 'r') {
6682 histedit_free_list(histedit_cmds);
6683 err = histedit_edit_script(histedit_cmds,
6684 commits, branch_name, 0, repo);
6686 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6687 err->code != GOT_ERR_HISTEDIT_CMD)
6694 } else if (resp == 'a') {
6695 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6698 printf("invalid response '%c'\n", resp);
6704 static const struct got_error *
6705 histedit_complete(struct got_worktree *worktree,
6706 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6707 struct got_reference *branch, struct got_repository *repo)
6709 printf("Switching work tree to %s\n",
6710 got_ref_get_symref_target(branch));
6711 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6715 static const struct got_error *
6716 show_histedit_progress(struct got_commit_object *commit,
6717 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6719 const struct got_error *err;
6720 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6722 err = got_object_id_str(&old_id_str, hle->commit_id);
6727 err = got_object_id_str(&new_id_str, new_id);
6732 old_id_str[12] = '\0';
6734 new_id_str[12] = '\0';
6737 logmsg = strdup(hle->logmsg);
6738 if (logmsg == NULL) {
6739 err = got_error_from_errno("strdup");
6742 trim_logmsg(logmsg, 42);
6744 err = get_short_logmsg(&logmsg, 42, commit);
6749 switch (hle->cmd->code) {
6750 case GOT_HISTEDIT_PICK:
6751 case GOT_HISTEDIT_EDIT:
6752 printf("%s -> %s: %s\n", old_id_str,
6753 new_id_str ? new_id_str : "no-op change", logmsg);
6755 case GOT_HISTEDIT_DROP:
6756 case GOT_HISTEDIT_FOLD:
6757 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6769 static const struct got_error *
6770 histedit_commit(struct got_pathlist_head *merged_paths,
6771 struct got_worktree *worktree, struct got_fileindex *fileindex,
6772 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6773 struct got_repository *repo)
6775 const struct got_error *err;
6776 struct got_commit_object *commit;
6777 struct got_object_id *new_commit_id;
6779 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6780 && hle->logmsg == NULL) {
6781 err = histedit_edit_logmsg(hle, repo);
6786 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6790 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6791 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6794 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6796 err = show_histedit_progress(commit, hle, NULL);
6798 err = show_histedit_progress(commit, hle, new_commit_id);
6799 free(new_commit_id);
6802 got_object_commit_close(commit);
6806 static const struct got_error *
6807 histedit_skip_commit(struct got_histedit_list_entry *hle,
6808 struct got_worktree *worktree, struct got_repository *repo)
6810 const struct got_error *error;
6811 struct got_commit_object *commit;
6813 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6818 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6822 error = show_histedit_progress(commit, hle, NULL);
6823 got_object_commit_close(commit);
6827 static const struct got_error *
6828 check_local_changes(void *arg, unsigned char status,
6829 unsigned char staged_status, const char *path,
6830 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6831 struct got_object_id *commit_id, int dirfd, const char *de_name)
6833 int *have_local_changes = arg;
6836 case GOT_STATUS_ADD:
6837 case GOT_STATUS_DELETE:
6838 case GOT_STATUS_MODIFY:
6839 case GOT_STATUS_CONFLICT:
6840 *have_local_changes = 1;
6841 return got_error(GOT_ERR_CANCELLED);
6846 switch (staged_status) {
6847 case GOT_STATUS_ADD:
6848 case GOT_STATUS_DELETE:
6849 case GOT_STATUS_MODIFY:
6850 *have_local_changes = 1;
6851 return got_error(GOT_ERR_CANCELLED);
6859 static const struct got_error *
6860 cmd_histedit(int argc, char *argv[])
6862 const struct got_error *error = NULL;
6863 struct got_worktree *worktree = NULL;
6864 struct got_fileindex *fileindex = NULL;
6865 struct got_repository *repo = NULL;
6867 struct got_reference *branch = NULL;
6868 struct got_reference *tmp_branch = NULL;
6869 struct got_object_id *resume_commit_id = NULL;
6870 struct got_object_id *base_commit_id = NULL;
6871 struct got_object_id *head_commit_id = NULL;
6872 struct got_commit_object *commit = NULL;
6873 int ch, rebase_in_progress = 0, did_something;
6874 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6875 int edit_logmsg_only = 0;
6876 const char *edit_script_path = NULL;
6877 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6878 struct got_object_id_queue commits;
6879 struct got_pathlist_head merged_paths;
6880 const struct got_object_id_queue *parent_ids;
6881 struct got_object_qid *pid;
6882 struct got_histedit_list histedit_cmds;
6883 struct got_histedit_list_entry *hle;
6885 SIMPLEQ_INIT(&commits);
6886 TAILQ_INIT(&histedit_cmds);
6887 TAILQ_INIT(&merged_paths);
6889 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6898 edit_script_path = optarg;
6901 edit_logmsg_only = 1;
6913 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6914 "unveil", NULL) == -1)
6917 if (abort_edit && continue_edit)
6918 errx(1, "histedit's -a and -c options are mutually exclusive");
6919 if (edit_script_path && edit_logmsg_only)
6920 errx(1, "histedit's -F and -m options are mutually exclusive");
6921 if (abort_edit && edit_logmsg_only)
6922 errx(1, "histedit's -a and -m options are mutually exclusive");
6923 if (continue_edit && edit_logmsg_only)
6924 errx(1, "histedit's -c and -m options are mutually exclusive");
6929 * This command cannot apply unveil(2) in all cases because the
6930 * user may choose to run an editor to edit the histedit script
6931 * and to edit individual commit log messages.
6932 * unveil(2) traverses exec(2); if an editor is used we have to
6933 * apply unveil after edit script and log messages have been written.
6934 * XXX TODO: Make use of unveil(2) where possible.
6937 cwd = getcwd(NULL, 0);
6939 error = got_error_from_errno("getcwd");
6942 error = got_worktree_open(&worktree, cwd);
6946 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6951 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6954 if (rebase_in_progress) {
6955 error = got_error(GOT_ERR_REBASING);
6959 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6963 if (edit_in_progress && edit_logmsg_only) {
6964 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6965 "histedit operation is in progress in this "
6966 "work tree and must be continued or aborted "
6967 "before the -m option can be used");
6971 if (edit_in_progress && abort_edit) {
6972 error = got_worktree_histedit_continue(&resume_commit_id,
6973 &tmp_branch, &branch, &base_commit_id, &fileindex,
6977 printf("Switching work tree to %s\n",
6978 got_ref_get_symref_target(branch));
6979 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6980 branch, base_commit_id, update_progress, &did_something);
6983 printf("Histedit of %s aborted\n",
6984 got_ref_get_symref_target(branch));
6985 goto done; /* nothing else to do */
6986 } else if (abort_edit) {
6987 error = got_error(GOT_ERR_NOT_HISTEDIT);
6991 if (continue_edit) {
6994 if (!edit_in_progress) {
6995 error = got_error(GOT_ERR_NOT_HISTEDIT);
6999 error = got_worktree_get_histedit_script_path(&path, worktree);
7003 error = histedit_load_list(&histedit_cmds, path, repo);
7008 error = got_worktree_histedit_continue(&resume_commit_id,
7009 &tmp_branch, &branch, &base_commit_id, &fileindex,
7014 error = got_ref_resolve(&head_commit_id, repo, branch);
7018 error = got_object_open_as_commit(&commit, repo,
7022 parent_ids = got_object_commit_get_parent_ids(commit);
7023 pid = SIMPLEQ_FIRST(parent_ids);
7025 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7028 error = collect_commits(&commits, head_commit_id, pid->id,
7029 base_commit_id, got_worktree_get_path_prefix(worktree),
7030 GOT_ERR_HISTEDIT_PATH, repo);
7031 got_object_commit_close(commit);
7036 if (edit_in_progress) {
7037 error = got_error(GOT_ERR_HISTEDIT_BUSY);
7041 error = got_ref_open(&branch, repo,
7042 got_worktree_get_head_ref_name(worktree), 0);
7046 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7047 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7048 "will not edit commit history of a branch outside "
7049 "the \"refs/heads/\" reference namespace");
7053 error = got_ref_resolve(&head_commit_id, repo, branch);
7054 got_ref_close(branch);
7059 error = got_object_open_as_commit(&commit, repo,
7063 parent_ids = got_object_commit_get_parent_ids(commit);
7064 pid = SIMPLEQ_FIRST(parent_ids);
7066 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7069 error = collect_commits(&commits, head_commit_id, pid->id,
7070 got_worktree_get_base_commit_id(worktree),
7071 got_worktree_get_path_prefix(worktree),
7072 GOT_ERR_HISTEDIT_PATH, repo);
7073 got_object_commit_close(commit);
7078 if (SIMPLEQ_EMPTY(&commits)) {
7079 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7083 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7084 &base_commit_id, &fileindex, worktree, repo);
7088 if (edit_script_path) {
7089 error = histedit_load_list(&histedit_cmds,
7090 edit_script_path, repo);
7092 got_worktree_histedit_abort(worktree, fileindex,
7093 repo, branch, base_commit_id,
7094 update_progress, &did_something);
7098 const char *branch_name;
7099 branch_name = got_ref_get_symref_target(branch);
7100 if (strncmp(branch_name, "refs/heads/", 11) == 0)
7102 error = histedit_edit_script(&histedit_cmds, &commits,
7103 branch_name, edit_logmsg_only, repo);
7105 got_worktree_histedit_abort(worktree, fileindex,
7106 repo, branch, base_commit_id,
7107 update_progress, &did_something);
7113 error = histedit_save_list(&histedit_cmds, worktree,
7116 got_worktree_histedit_abort(worktree, fileindex,
7117 repo, branch, base_commit_id,
7118 update_progress, &did_something);
7124 error = histedit_check_script(&histedit_cmds, &commits, repo);
7128 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7129 if (resume_commit_id) {
7130 if (got_object_id_cmp(hle->commit_id,
7131 resume_commit_id) != 0)
7134 resume_commit_id = NULL;
7135 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7136 hle->cmd->code == GOT_HISTEDIT_FOLD) {
7137 error = histedit_skip_commit(hle, worktree,
7142 struct got_pathlist_head paths;
7143 int have_changes = 0;
7146 error = got_pathlist_append(&paths, "", NULL);
7149 error = got_worktree_status(worktree, &paths,
7150 repo, check_local_changes, &have_changes,
7151 check_cancelled, NULL);
7152 got_pathlist_free(&paths);
7154 if (error->code != GOT_ERR_CANCELLED)
7156 if (sigint_received || sigpipe_received)
7160 error = histedit_commit(NULL, worktree,
7161 fileindex, tmp_branch, hle, repo);
7165 error = got_object_open_as_commit(
7166 &commit, repo, hle->commit_id);
7169 error = show_histedit_progress(commit,
7171 got_object_commit_close(commit);
7180 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7181 error = histedit_skip_commit(hle, worktree, repo);
7187 error = got_object_open_as_commit(&commit, repo,
7191 parent_ids = got_object_commit_get_parent_ids(commit);
7192 pid = SIMPLEQ_FIRST(parent_ids);
7194 error = got_worktree_histedit_merge_files(&merged_paths,
7195 worktree, fileindex, pid->id, hle->commit_id, repo,
7196 rebase_progress, &rebase_status, check_cancelled, NULL);
7199 got_object_commit_close(commit);
7202 if (rebase_status == GOT_STATUS_CONFLICT) {
7203 error = show_rebase_merge_conflict(hle->commit_id,
7207 got_worktree_rebase_pathlist_free(&merged_paths);
7211 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7213 error = got_object_id_str(&id_str, hle->commit_id);
7216 printf("Stopping histedit for amending commit %s\n",
7219 got_worktree_rebase_pathlist_free(&merged_paths);
7220 error = got_worktree_histedit_postpone(worktree,
7225 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7226 error = histedit_skip_commit(hle, worktree, repo);
7232 error = histedit_commit(&merged_paths, worktree, fileindex,
7233 tmp_branch, hle, repo);
7234 got_worktree_rebase_pathlist_free(&merged_paths);
7239 if (rebase_status == GOT_STATUS_CONFLICT) {
7240 error = got_worktree_histedit_postpone(worktree, fileindex);
7243 error = got_error_msg(GOT_ERR_CONFLICTS,
7244 "conflicts must be resolved before histedit can continue");
7246 error = histedit_complete(worktree, fileindex, tmp_branch,
7249 got_object_id_queue_free(&commits);
7250 histedit_free_list(&histedit_cmds);
7251 free(head_commit_id);
7252 free(base_commit_id);
7253 free(resume_commit_id);
7255 got_object_commit_close(commit);
7257 got_ref_close(branch);
7259 got_ref_close(tmp_branch);
7261 got_worktree_close(worktree);
7263 got_repo_close(repo);
7268 usage_integrate(void)
7270 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7274 static const struct got_error *
7275 cmd_integrate(int argc, char *argv[])
7277 const struct got_error *error = NULL;
7278 struct got_repository *repo = NULL;
7279 struct got_worktree *worktree = NULL;
7280 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7281 const char *branch_arg = NULL;
7282 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7283 struct got_fileindex *fileindex = NULL;
7284 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7285 int ch, did_something = 0;
7287 while ((ch = getopt(argc, argv, "")) != -1) {
7300 branch_arg = argv[0];
7302 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7303 "unveil", NULL) == -1)
7306 cwd = getcwd(NULL, 0);
7308 error = got_error_from_errno("getcwd");
7312 error = got_worktree_open(&worktree, cwd);
7316 error = check_rebase_or_histedit_in_progress(worktree);
7320 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7325 error = apply_unveil(got_repo_get_path(repo), 0,
7326 got_worktree_get_root_path(worktree));
7330 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7331 error = got_error_from_errno("asprintf");
7335 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7336 &base_branch_ref, worktree, refname, repo);
7340 refname = strdup(got_ref_get_name(branch_ref));
7341 if (refname == NULL) {
7342 error = got_error_from_errno("strdup");
7343 got_worktree_integrate_abort(worktree, fileindex, repo,
7344 branch_ref, base_branch_ref);
7347 base_refname = strdup(got_ref_get_name(base_branch_ref));
7348 if (base_refname == NULL) {
7349 error = got_error_from_errno("strdup");
7350 got_worktree_integrate_abort(worktree, fileindex, repo,
7351 branch_ref, base_branch_ref);
7355 error = got_ref_resolve(&commit_id, repo, branch_ref);
7359 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7363 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7364 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7365 "specified branch has already been integrated");
7366 got_worktree_integrate_abort(worktree, fileindex, repo,
7367 branch_ref, base_branch_ref);
7371 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7373 if (error->code == GOT_ERR_ANCESTRY)
7374 error = got_error(GOT_ERR_REBASE_REQUIRED);
7375 got_worktree_integrate_abort(worktree, fileindex, repo,
7376 branch_ref, base_branch_ref);
7380 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7381 branch_ref, base_branch_ref, update_progress, &did_something,
7382 check_cancelled, NULL);
7386 printf("Integrated %s into %s\n", refname, base_refname);
7389 got_repo_close(repo);
7391 got_worktree_close(worktree);
7393 free(base_commit_id);
7403 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7404 "[file-path ...]\n",
7409 static const struct got_error *
7410 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7411 const char *path, struct got_object_id *blob_id,
7412 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7413 int dirfd, const char *de_name)
7415 const struct got_error *err = NULL;
7416 char *id_str = NULL;
7418 if (staged_status != GOT_STATUS_ADD &&
7419 staged_status != GOT_STATUS_MODIFY &&
7420 staged_status != GOT_STATUS_DELETE)
7423 if (staged_status == GOT_STATUS_ADD ||
7424 staged_status == GOT_STATUS_MODIFY)
7425 err = got_object_id_str(&id_str, staged_blob_id);
7427 err = got_object_id_str(&id_str, blob_id);
7431 printf("%s %c %s\n", id_str, staged_status, path);
7436 static const struct got_error *
7437 cmd_stage(int argc, char *argv[])
7439 const struct got_error *error = NULL;
7440 struct got_repository *repo = NULL;
7441 struct got_worktree *worktree = NULL;
7443 struct got_pathlist_head paths;
7444 struct got_pathlist_entry *pe;
7445 int ch, list_stage = 0, pflag = 0;
7446 FILE *patch_script_file = NULL;
7447 const char *patch_script_path = NULL;
7448 struct choose_patch_arg cpa;
7452 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7461 patch_script_path = optarg;
7473 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7474 "unveil", NULL) == -1)
7477 if (list_stage && (pflag || patch_script_path))
7478 errx(1, "-l option cannot be used with other options");
7479 if (patch_script_path && !pflag)
7480 errx(1, "-F option can only be used together with -p option");
7482 cwd = getcwd(NULL, 0);
7484 error = got_error_from_errno("getcwd");
7488 error = got_worktree_open(&worktree, cwd);
7492 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7497 if (patch_script_path) {
7498 patch_script_file = fopen(patch_script_path, "r");
7499 if (patch_script_file == NULL) {
7500 error = got_error_from_errno2("fopen",
7505 error = apply_unveil(got_repo_get_path(repo), 0,
7506 got_worktree_get_root_path(worktree));
7510 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7515 error = got_worktree_status(worktree, &paths, repo,
7516 print_stage, NULL, check_cancelled, NULL);
7518 cpa.patch_script_file = patch_script_file;
7519 cpa.action = "stage";
7520 error = got_worktree_stage(worktree, &paths,
7521 pflag ? NULL : print_status, NULL,
7522 pflag ? choose_patch : NULL, &cpa, repo);
7525 if (patch_script_file && fclose(patch_script_file) == EOF &&
7527 error = got_error_from_errno2("fclose", patch_script_path);
7529 got_repo_close(repo);
7531 got_worktree_close(worktree);
7532 TAILQ_FOREACH(pe, &paths, entry)
7533 free((char *)pe->path);
7534 got_pathlist_free(&paths);
7542 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7543 "[file-path ...]\n",
7549 static const struct got_error *
7550 cmd_unstage(int argc, char *argv[])
7552 const struct got_error *error = NULL;
7553 struct got_repository *repo = NULL;
7554 struct got_worktree *worktree = NULL;
7556 struct got_pathlist_head paths;
7557 struct got_pathlist_entry *pe;
7558 int ch, did_something = 0, pflag = 0;
7559 FILE *patch_script_file = NULL;
7560 const char *patch_script_path = NULL;
7561 struct choose_patch_arg cpa;
7565 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7571 patch_script_path = optarg;
7583 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7584 "unveil", NULL) == -1)
7587 if (patch_script_path && !pflag)
7588 errx(1, "-F option can only be used together with -p option");
7590 cwd = getcwd(NULL, 0);
7592 error = got_error_from_errno("getcwd");
7596 error = got_worktree_open(&worktree, cwd);
7600 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7605 if (patch_script_path) {
7606 patch_script_file = fopen(patch_script_path, "r");
7607 if (patch_script_file == NULL) {
7608 error = got_error_from_errno2("fopen",
7614 error = apply_unveil(got_repo_get_path(repo), 0,
7615 got_worktree_get_root_path(worktree));
7619 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7623 cpa.patch_script_file = patch_script_file;
7624 cpa.action = "unstage";
7625 error = got_worktree_unstage(worktree, &paths, update_progress,
7626 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7628 if (patch_script_file && fclose(patch_script_file) == EOF &&
7630 error = got_error_from_errno2("fclose", patch_script_path);
7632 got_repo_close(repo);
7634 got_worktree_close(worktree);
7635 TAILQ_FOREACH(pe, &paths, entry)
7636 free((char *)pe->path);
7637 got_pathlist_free(&paths);
7645 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7646 "arg1 [arg2 ...]\n", getprogname());
7650 static const struct got_error *
7651 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7653 const struct got_error *err;
7654 struct got_blob_object *blob;
7656 err = got_object_open_as_blob(&blob, repo, id, 8192);
7660 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7661 got_object_blob_close(blob);
7665 static const struct got_error *
7666 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7668 const struct got_error *err;
7669 struct got_tree_object *tree;
7672 err = got_object_open_as_tree(&tree, repo, id);
7676 nentries = got_object_tree_get_nentries(tree);
7677 for (i = 0; i < nentries; i++) {
7678 struct got_tree_entry *te;
7680 if (sigint_received || sigpipe_received)
7682 te = got_object_tree_get_entry(tree, i);
7683 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7686 fprintf(outfile, "%s %.7o %s\n", id_str,
7687 got_tree_entry_get_mode(te),
7688 got_tree_entry_get_name(te));
7692 got_object_tree_close(tree);
7696 static const struct got_error *
7697 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7699 const struct got_error *err;
7700 struct got_commit_object *commit;
7701 const struct got_object_id_queue *parent_ids;
7702 struct got_object_qid *pid;
7703 char *id_str = NULL;
7704 const char *logmsg = NULL;
7706 err = got_object_open_as_commit(&commit, repo, id);
7710 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7714 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7715 parent_ids = got_object_commit_get_parent_ids(commit);
7716 fprintf(outfile, "numparents %d\n",
7717 got_object_commit_get_nparents(commit));
7718 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7720 err = got_object_id_str(&pid_str, pid->id);
7723 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7726 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7727 got_object_commit_get_author(commit),
7728 got_object_commit_get_author_time(commit));
7730 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7731 got_object_commit_get_author(commit),
7732 got_object_commit_get_committer_time(commit));
7734 logmsg = got_object_commit_get_logmsg_raw(commit);
7735 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7736 fprintf(outfile, "%s", logmsg);
7739 got_object_commit_close(commit);
7743 static const struct got_error *
7744 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7746 const struct got_error *err;
7747 struct got_tag_object *tag;
7748 char *id_str = NULL;
7749 const char *tagmsg = NULL;
7751 err = got_object_open_as_tag(&tag, repo, id);
7755 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7759 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7761 switch (got_object_tag_get_object_type(tag)) {
7762 case GOT_OBJ_TYPE_BLOB:
7763 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7764 GOT_OBJ_LABEL_BLOB);
7766 case GOT_OBJ_TYPE_TREE:
7767 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7768 GOT_OBJ_LABEL_TREE);
7770 case GOT_OBJ_TYPE_COMMIT:
7771 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7772 GOT_OBJ_LABEL_COMMIT);
7774 case GOT_OBJ_TYPE_TAG:
7775 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7782 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7783 got_object_tag_get_name(tag));
7785 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7786 got_object_tag_get_tagger(tag),
7787 got_object_tag_get_tagger_time(tag));
7789 tagmsg = got_object_tag_get_message(tag);
7790 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7791 fprintf(outfile, "%s", tagmsg);
7794 got_object_tag_close(tag);
7798 static const struct got_error *
7799 cmd_cat(int argc, char *argv[])
7801 const struct got_error *error;
7802 struct got_repository *repo = NULL;
7803 struct got_worktree *worktree = NULL;
7804 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7805 const char *commit_id_str = NULL;
7806 struct got_object_id *id = NULL, *commit_id = NULL;
7807 int ch, obj_type, i, force_path = 0;
7810 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7815 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7818 commit_id_str = optarg;
7821 repo_path = realpath(optarg, NULL);
7822 if (repo_path == NULL)
7823 return got_error_from_errno2("realpath",
7825 got_path_strip_trailing_slashes(repo_path);
7839 cwd = getcwd(NULL, 0);
7841 error = got_error_from_errno("getcwd");
7844 error = got_worktree_open(&worktree, cwd);
7845 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7848 if (repo_path == NULL) {
7850 got_worktree_get_repo_path(worktree));
7851 if (repo_path == NULL) {
7852 error = got_error_from_errno("strdup");
7858 if (repo_path == NULL) {
7859 repo_path = getcwd(NULL, 0);
7860 if (repo_path == NULL)
7861 return got_error_from_errno("getcwd");
7864 error = got_repo_open(&repo, repo_path, NULL);
7869 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7873 if (commit_id_str == NULL)
7874 commit_id_str = GOT_REF_HEAD;
7875 error = got_repo_match_object_id(&commit_id, NULL,
7876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7880 for (i = 0; i < argc; i++) {
7882 error = got_object_id_by_path(&id, repo, commit_id,
7887 error = got_repo_match_object_id(&id, &label, argv[i],
7888 GOT_OBJ_TYPE_ANY, 0, repo);
7890 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7891 error->code != GOT_ERR_NOT_REF)
7893 error = got_object_id_by_path(&id, repo,
7894 commit_id, argv[i]);
7900 error = got_object_get_type(&obj_type, repo, id);
7905 case GOT_OBJ_TYPE_BLOB:
7906 error = cat_blob(id, repo, stdout);
7908 case GOT_OBJ_TYPE_TREE:
7909 error = cat_tree(id, repo, stdout);
7911 case GOT_OBJ_TYPE_COMMIT:
7912 error = cat_commit(id, repo, stdout);
7914 case GOT_OBJ_TYPE_TAG:
7915 error = cat_tag(id, repo, stdout);
7918 error = got_error(GOT_ERR_OBJ_TYPE);
7933 got_worktree_close(worktree);
7935 const struct got_error *repo_error;
7936 repo_error = got_repo_close(repo);