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_checkout(void);
89 __dead static void usage_clone(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 { "checkout", cmd_checkout, usage_checkout, "co" },
142 { "clone", cmd_clone, usage_clone, "cl" },
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 repo-url\n", getprogname());
818 fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
819 "[-p prefix] repository-path [worktree-path]\n", getprogname());
824 show_worktree_base_ref_warning(void)
826 fprintf(stderr, "%s: warning: could not create a reference "
827 "to the work tree's base commit; the commit could be "
828 "garbage-collected by Git; making the repository "
829 "writable and running 'got update' will prevent this\n",
833 struct got_checkout_progress_arg {
834 const char *worktree_path;
835 int had_base_commit_ref_error;
838 static const struct got_error *
839 checkout_progress(void *arg, unsigned char status, const char *path)
841 struct got_checkout_progress_arg *a = arg;
843 /* Base commit bump happens silently. */
844 if (status == GOT_STATUS_BUMP_BASE)
847 if (status == GOT_STATUS_BASE_REF_ERR) {
848 a->had_base_commit_ref_error = 1;
852 while (path[0] == '/')
855 printf("%c %s/%s\n", status, a->worktree_path, path);
859 static const struct got_error *
860 check_cancelled(void *arg)
862 if (sigint_received || sigpipe_received)
863 return got_error(GOT_ERR_CANCELLED);
867 static const struct got_error *
868 check_linear_ancestry(struct got_object_id *commit_id,
869 struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
870 struct got_repository *repo)
872 const struct got_error *err = NULL;
873 struct got_object_id *yca_id;
875 err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
876 commit_id, base_commit_id, repo, check_cancelled, NULL);
881 return got_error(GOT_ERR_ANCESTRY);
884 * Require a straight line of history between the target commit
885 * and the work tree's base commit.
887 * Non-linear situations such as this require a rebase:
889 * (commit) D F (base_commit)
897 * 'got update' only handles linear cases:
898 * Update forwards in time: A (base/yca) - B - C - D (commit)
899 * Update backwards in time: D (base) - C - B - A (commit/yca)
901 if (allow_forwards_in_time_only) {
902 if (got_object_id_cmp(base_commit_id, yca_id) != 0)
903 return got_error(GOT_ERR_ANCESTRY);
904 } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
905 got_object_id_cmp(base_commit_id, yca_id) != 0)
906 return got_error(GOT_ERR_ANCESTRY);
912 static const struct got_error *
913 check_same_branch(struct got_object_id *commit_id,
914 struct got_reference *head_ref, struct got_object_id *yca_id,
915 struct got_repository *repo)
917 const struct got_error *err = NULL;
918 struct got_commit_graph *graph = NULL;
919 struct got_object_id *head_commit_id = NULL;
920 int is_same_branch = 0;
922 err = got_ref_resolve(&head_commit_id, repo, head_ref);
926 if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
930 if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
935 err = got_commit_graph_open(&graph, "/", 1);
939 err = got_commit_graph_iter_start(graph, head_commit_id, repo,
940 check_cancelled, NULL);
945 struct got_object_id *id;
946 err = got_commit_graph_iter_next(&id, graph, repo,
947 check_cancelled, NULL);
949 if (err->code == GOT_ERR_ITER_COMPLETED)
955 if (yca_id && got_object_id_cmp(id, yca_id) == 0)
957 if (got_object_id_cmp(id, commit_id) == 0) {
965 got_commit_graph_close(graph);
966 free(head_commit_id);
967 if (!err && !is_same_branch)
968 err = got_error(GOT_ERR_ANCESTRY);
972 static const struct got_error *
973 fetch_progress(void *arg, const char *message, off_t packfile_size,
974 int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
977 char scaled[FMT_SCALED_STRSIZE];
979 if (message && message[0] != '\0' && message[0] != '\n') {
980 printf("\rserver: %s", message);
981 if (strchr(message, '\n') == 0)
985 if (packfile_size > 0 || nobj_indexed > 0) {
987 if (fmt_scaled(packfile_size, scaled) == 0)
988 printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
989 if (nobj_indexed > 0) {
990 p = (nobj_indexed * 100) / nobj_total;
991 printf("; indexing %d%% (%d)", p, nobj_indexed);
993 if (nobj_resolved > 0) {
994 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
995 printf("; resolving deltas %d%% (%d)",
998 if (nobj_indexed > 0 && nobj_indexed == nobj_total &&
999 nobj_resolved == nobj_total - nobj_loose)
1000 printf("\nWriting pack index...\n");
1007 static const struct got_error *
1008 cmd_clone(int argc, char *argv[])
1010 const struct got_error *err = NULL;
1011 const char *uri, *branch_filter, *dirname;
1012 char *proto, *host, *port, *repo_name, *server_path;
1013 char *default_destdir = NULL, *id_str = NULL;
1014 const char *repo_path;
1015 struct got_repository *repo = NULL;
1016 struct got_pathlist_head refs, symrefs;
1017 struct got_pathlist_entry *pe;
1018 struct got_object_id *pack_hash = NULL;
1019 int ch, fetchfd = -1;
1022 TAILQ_INIT(&symrefs);
1024 while ((ch = getopt(argc, argv, "b:")) != -1) {
1027 branch_filter = optarg;
1044 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1045 &repo_name, argv[0]);
1049 if (dirname == NULL) {
1050 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1051 err = got_error_from_errno("asprintf");
1054 repo_path = default_destdir;
1056 repo_path = dirname;
1058 err = got_path_mkdir(repo_path);
1062 err = got_repo_init(repo_path);
1066 err = got_repo_open(&repo, repo_path, NULL);
1070 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1074 printf("Connected to %s:%s\n", host, port);
1076 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1077 repo, fetch_progress, NULL);
1081 err = got_object_id_str(&id_str, pack_hash);
1084 printf("Fetched %s.pack\n", id_str);
1087 /* Set up references provided with the pack file. */
1088 TAILQ_FOREACH(pe, &refs, entry) {
1089 const char *refname = pe->path;
1090 struct got_object_id *id = pe->data;
1091 struct got_reference *ref;
1094 err = got_ref_alloc(&ref, refname, id);
1099 err = got_object_id_str(&id_str, id);
1102 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1105 err = got_ref_write(ref, repo);
1111 /* Set the HEAD reference if the server provided one. */
1112 TAILQ_FOREACH(pe, &symrefs, entry) {
1113 struct got_reference *symref, *target_ref;
1114 const char *refname = pe->path;
1115 const char *target = pe->data;
1117 if (strcmp(refname, GOT_REF_HEAD) != 0)
1120 err = got_ref_open(&target_ref, repo, target, 0);
1122 if (err->code == GOT_ERR_NOT_REF)
1127 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1128 got_ref_close(target_ref);
1132 printf("Setting %s to %s\n", GOT_REF_HEAD,
1133 got_ref_get_symref_target(symref));
1135 err = got_ref_write(symref, repo);
1136 got_ref_close(symref);
1141 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1142 err = got_error_from_errno("close");
1144 got_repo_close(repo);
1145 TAILQ_FOREACH(pe, &refs, entry) {
1146 free((void *)pe->path);
1149 got_pathlist_free(&refs);
1150 TAILQ_FOREACH(pe, &symrefs, entry) {
1151 free((void *)pe->path);
1154 got_pathlist_free(&symrefs);
1161 free(default_destdir);
1165 static const struct got_error *
1166 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1168 static char msg[512];
1169 const char *branch_name;
1171 if (got_ref_is_symbolic(ref))
1172 branch_name = got_ref_get_symref_target(ref);
1174 branch_name = got_ref_get_name(ref);
1176 if (strncmp("refs/heads/", branch_name, 11) == 0)
1179 snprintf(msg, sizeof(msg),
1180 "target commit is not contained in branch '%s'; "
1181 "the branch to use must be specified with -b; "
1182 "if necessary a new branch can be created for "
1183 "this commit with 'got branch -c %s BRANCH_NAME'",
1184 branch_name, commit_id_str);
1186 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1189 static const struct got_error *
1190 cmd_checkout(int argc, char *argv[])
1192 const struct got_error *error = NULL;
1193 struct got_repository *repo = NULL;
1194 struct got_reference *head_ref = NULL;
1195 struct got_worktree *worktree = NULL;
1196 char *repo_path = NULL;
1197 char *worktree_path = NULL;
1198 const char *path_prefix = "";
1199 const char *branch_name = GOT_REF_HEAD;
1200 char *commit_id_str = NULL;
1201 int ch, same_path_prefix, allow_nonempty = 0;
1202 struct got_pathlist_head paths;
1203 struct got_checkout_progress_arg cpa;
1207 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1210 branch_name = optarg;
1213 commit_id_str = strdup(optarg);
1214 if (commit_id_str == NULL)
1215 return got_error_from_errno("strdup");
1221 path_prefix = optarg;
1233 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1234 "unveil", NULL) == -1)
1238 char *cwd, *base, *dotgit;
1239 repo_path = realpath(argv[0], NULL);
1240 if (repo_path == NULL)
1241 return got_error_from_errno2("realpath", argv[0]);
1242 cwd = getcwd(NULL, 0);
1244 error = got_error_from_errno("getcwd");
1247 if (path_prefix[0]) {
1248 base = basename(path_prefix);
1250 error = got_error_from_errno2("basename",
1255 base = basename(repo_path);
1257 error = got_error_from_errno2("basename",
1262 dotgit = strstr(base, ".git");
1265 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1266 error = got_error_from_errno("asprintf");
1271 } else if (argc == 2) {
1272 repo_path = realpath(argv[0], NULL);
1273 if (repo_path == NULL) {
1274 error = got_error_from_errno2("realpath", argv[0]);
1277 worktree_path = realpath(argv[1], NULL);
1278 if (worktree_path == NULL) {
1279 if (errno != ENOENT) {
1280 error = got_error_from_errno2("realpath",
1284 worktree_path = strdup(argv[1]);
1285 if (worktree_path == NULL) {
1286 error = got_error_from_errno("strdup");
1293 got_path_strip_trailing_slashes(repo_path);
1294 got_path_strip_trailing_slashes(worktree_path);
1296 error = got_repo_open(&repo, repo_path, NULL);
1300 /* Pre-create work tree path for unveil(2) */
1301 error = got_path_mkdir(worktree_path);
1303 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1304 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1306 if (!allow_nonempty &&
1307 !got_path_dir_is_empty(worktree_path)) {
1308 error = got_error_path(worktree_path,
1309 GOT_ERR_DIR_NOT_EMPTY);
1314 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1318 error = got_ref_open(&head_ref, repo, branch_name, 0);
1322 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1323 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1326 error = got_worktree_open(&worktree, worktree_path);
1330 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1334 if (!same_path_prefix) {
1335 error = got_error(GOT_ERR_PATH_PREFIX);
1339 if (commit_id_str) {
1340 struct got_object_id *commit_id;
1341 error = got_repo_match_object_id(&commit_id, NULL,
1342 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1345 error = check_linear_ancestry(commit_id,
1346 got_worktree_get_base_commit_id(worktree), 0, repo);
1347 if (error != NULL) {
1349 if (error->code == GOT_ERR_ANCESTRY) {
1350 error = checkout_ancestry_error(
1351 head_ref, commit_id_str);
1355 error = check_same_branch(commit_id, head_ref, NULL, repo);
1357 if (error->code == GOT_ERR_ANCESTRY) {
1358 error = checkout_ancestry_error(
1359 head_ref, commit_id_str);
1363 error = got_worktree_set_base_commit_id(worktree, repo,
1370 error = got_pathlist_append(&paths, "", NULL);
1373 cpa.worktree_path = worktree_path;
1374 cpa.had_base_commit_ref_error = 0;
1375 error = got_worktree_checkout_files(worktree, &paths, repo,
1376 checkout_progress, &cpa, check_cancelled, NULL);
1380 printf("Now shut up and hack\n");
1381 if (cpa.had_base_commit_ref_error)
1382 show_worktree_base_ref_warning();
1384 got_pathlist_free(&paths);
1385 free(commit_id_str);
1387 free(worktree_path);
1394 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1399 static const struct got_error *
1400 update_progress(void *arg, unsigned char status, const char *path)
1402 int *did_something = arg;
1404 if (status == GOT_STATUS_EXISTS ||
1405 status == GOT_STATUS_BASE_REF_ERR)
1410 /* Base commit bump happens silently. */
1411 if (status == GOT_STATUS_BUMP_BASE)
1414 while (path[0] == '/')
1416 printf("%c %s\n", status, path);
1420 static const struct got_error *
1421 switch_head_ref(struct got_reference *head_ref,
1422 struct got_object_id *commit_id, struct got_worktree *worktree,
1423 struct got_repository *repo)
1425 const struct got_error *err = NULL;
1427 int ref_has_moved = 0;
1429 /* Trivial case: switching between two different references. */
1430 if (strcmp(got_ref_get_name(head_ref),
1431 got_worktree_get_head_ref_name(worktree)) != 0) {
1432 printf("Switching work tree from %s to %s\n",
1433 got_worktree_get_head_ref_name(worktree),
1434 got_ref_get_name(head_ref));
1435 return got_worktree_set_head_ref(worktree, head_ref);
1438 err = check_linear_ancestry(commit_id,
1439 got_worktree_get_base_commit_id(worktree), 0, repo);
1441 if (err->code != GOT_ERR_ANCESTRY)
1448 /* Switching to a rebased branch with the same reference name. */
1449 err = got_object_id_str(&base_id_str,
1450 got_worktree_get_base_commit_id(worktree));
1453 printf("Reference %s now points at a different branch\n",
1454 got_worktree_get_head_ref_name(worktree));
1455 printf("Switching work tree from %s to %s\n", base_id_str,
1456 got_worktree_get_head_ref_name(worktree));
1460 static const struct got_error *
1461 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1463 const struct got_error *err;
1466 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1470 return got_error(GOT_ERR_REBASING);
1472 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1476 return got_error(GOT_ERR_HISTEDIT_BUSY);
1481 static const struct got_error *
1482 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1483 char *argv[], struct got_worktree *worktree)
1485 const struct got_error *err = NULL;
1492 return got_error_from_errno("strdup");
1493 return got_pathlist_append(paths, path, NULL);
1496 for (i = 0; i < argc; i++) {
1497 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1500 err = got_pathlist_append(paths, path, NULL);
1510 static const struct got_error *
1511 cmd_update(int argc, char *argv[])
1513 const struct got_error *error = NULL;
1514 struct got_repository *repo = NULL;
1515 struct got_worktree *worktree = NULL;
1516 char *worktree_path = NULL;
1517 struct got_object_id *commit_id = NULL;
1518 char *commit_id_str = NULL;
1519 const char *branch_name = NULL;
1520 struct got_reference *head_ref = NULL;
1521 struct got_pathlist_head paths;
1522 struct got_pathlist_entry *pe;
1523 int ch, did_something = 0;
1527 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1530 branch_name = optarg;
1533 commit_id_str = strdup(optarg);
1534 if (commit_id_str == NULL)
1535 return got_error_from_errno("strdup");
1547 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1548 "unveil", NULL) == -1)
1551 worktree_path = getcwd(NULL, 0);
1552 if (worktree_path == NULL) {
1553 error = got_error_from_errno("getcwd");
1556 error = got_worktree_open(&worktree, worktree_path);
1560 error = check_rebase_or_histedit_in_progress(worktree);
1564 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1569 error = apply_unveil(got_repo_get_path(repo), 0,
1570 got_worktree_get_root_path(worktree));
1574 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1578 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1579 got_worktree_get_head_ref_name(worktree), 0);
1582 if (commit_id_str == NULL) {
1583 error = got_ref_resolve(&commit_id, repo, head_ref);
1586 error = got_object_id_str(&commit_id_str, commit_id);
1590 error = got_repo_match_object_id(&commit_id, NULL,
1591 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1592 free(commit_id_str);
1593 commit_id_str = NULL;
1596 error = got_object_id_str(&commit_id_str, commit_id);
1602 struct got_object_id *head_commit_id;
1603 TAILQ_FOREACH(pe, &paths, entry) {
1604 if (pe->path_len == 0)
1606 error = got_error_msg(GOT_ERR_BAD_PATH,
1607 "switching between branches requires that "
1608 "the entire work tree gets updated");
1611 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1614 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1616 free(head_commit_id);
1619 error = check_same_branch(commit_id, head_ref, NULL, repo);
1622 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1626 error = check_linear_ancestry(commit_id,
1627 got_worktree_get_base_commit_id(worktree), 0, repo);
1628 if (error != NULL) {
1629 if (error->code == GOT_ERR_ANCESTRY)
1630 error = got_error(GOT_ERR_BRANCH_MOVED);
1633 error = check_same_branch(commit_id, head_ref, NULL, repo);
1638 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1640 error = got_worktree_set_base_commit_id(worktree, repo,
1646 error = got_worktree_checkout_files(worktree, &paths, repo,
1647 update_progress, &did_something, check_cancelled, NULL);
1652 printf("Updated to commit %s\n", commit_id_str);
1654 printf("Already up-to-date\n");
1656 free(worktree_path);
1657 TAILQ_FOREACH(pe, &paths, entry)
1658 free((char *)pe->path);
1659 got_pathlist_free(&paths);
1661 free(commit_id_str);
1665 static const struct got_error *
1666 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1667 const char *path, int diff_context, int ignore_whitespace,
1668 struct got_repository *repo)
1670 const struct got_error *err = NULL;
1671 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1674 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1679 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1683 while (path[0] == '/')
1685 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1686 ignore_whitespace, stdout);
1689 got_object_blob_close(blob1);
1690 got_object_blob_close(blob2);
1694 static const struct got_error *
1695 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1696 const char *path, int diff_context, int ignore_whitespace,
1697 struct got_repository *repo)
1699 const struct got_error *err = NULL;
1700 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1701 struct got_diff_blob_output_unidiff_arg arg;
1704 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1709 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1713 arg.diff_context = diff_context;
1714 arg.ignore_whitespace = ignore_whitespace;
1715 arg.outfile = stdout;
1716 while (path[0] == '/')
1718 err = got_diff_tree(tree1, tree2, path, path, repo,
1719 got_diff_blob_output_unidiff, &arg, 1);
1722 got_object_tree_close(tree1);
1724 got_object_tree_close(tree2);
1728 static const struct got_error *
1729 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1730 const char *path, int diff_context, struct got_repository *repo)
1732 const struct got_error *err = NULL;
1733 struct got_commit_object *pcommit = NULL;
1734 char *id_str1 = NULL, *id_str2 = NULL;
1735 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1736 struct got_object_qid *qid;
1738 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1740 err = got_object_open_as_commit(&pcommit, repo,
1746 if (path && path[0] != '\0') {
1748 err = got_object_id_by_path(&obj_id2, repo, id, path);
1751 err = got_object_id_str(&id_str2, obj_id2);
1757 err = got_object_id_by_path(&obj_id1, repo,
1763 err = got_object_id_str(&id_str1, obj_id1);
1769 err = got_object_get_type(&obj_type, repo, obj_id2);
1774 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1776 case GOT_OBJ_TYPE_BLOB:
1777 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1780 case GOT_OBJ_TYPE_TREE:
1781 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1785 err = got_error(GOT_ERR_OBJ_TYPE);
1791 obj_id2 = got_object_commit_get_tree_id(commit);
1792 err = got_object_id_str(&id_str2, obj_id2);
1795 obj_id1 = got_object_commit_get_tree_id(pcommit);
1796 err = got_object_id_str(&id_str1, obj_id1);
1799 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1800 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1806 got_object_commit_close(pcommit);
1811 get_datestr(time_t *time, char *datebuf)
1813 struct tm mytm, *tm;
1816 tm = gmtime_r(time, &mytm);
1819 s = asctime_r(tm, datebuf);
1822 p = strchr(s, '\n');
1828 static const struct got_error *
1829 match_logmsg(int *have_match, struct got_object_id *id,
1830 struct got_commit_object *commit, regex_t *regex)
1832 const struct got_error *err = NULL;
1833 regmatch_t regmatch;
1834 char *id_str = NULL, *logmsg = NULL;
1838 err = got_object_id_str(&id_str, id);
1842 err = got_object_commit_get_logmsg(&logmsg, commit);
1846 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1854 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1856 static const struct got_error *
1857 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1858 struct got_repository *repo, const char *path, int show_patch,
1859 int diff_context, struct got_reflist_head *refs)
1861 const struct got_error *err = NULL;
1862 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1864 time_t committer_time;
1865 const char *author, *committer;
1866 char *refs_str = NULL;
1867 struct got_reflist_entry *re;
1869 SIMPLEQ_FOREACH(re, refs, entry) {
1872 struct got_tag_object *tag = NULL;
1875 name = got_ref_get_name(re->ref);
1876 if (strcmp(name, GOT_REF_HEAD) == 0)
1878 if (strncmp(name, "refs/", 5) == 0)
1880 if (strncmp(name, "got/", 4) == 0)
1882 if (strncmp(name, "heads/", 6) == 0)
1884 if (strncmp(name, "remotes/", 8) == 0)
1886 if (strncmp(name, "tags/", 5) == 0) {
1887 err = got_object_open_as_tag(&tag, repo, re->id);
1889 if (err->code != GOT_ERR_OBJ_TYPE)
1891 /* Ref points at something other than a tag. */
1896 cmp = got_object_id_cmp(tag ?
1897 got_object_tag_get_object_id(tag) : re->id, id);
1899 got_object_tag_close(tag);
1903 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1905 err = got_error_from_errno("asprintf");
1911 err = got_object_id_str(&id_str, id);
1915 printf(GOT_COMMIT_SEP_STR);
1916 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1917 refs_str ? refs_str : "", refs_str ? ")" : "");
1922 printf("from: %s\n", got_object_commit_get_author(commit));
1923 committer_time = got_object_commit_get_committer_time(commit);
1924 datestr = get_datestr(&committer_time, datebuf);
1926 printf("date: %s UTC\n", datestr);
1927 author = got_object_commit_get_author(commit);
1928 committer = got_object_commit_get_committer(commit);
1929 if (strcmp(author, committer) != 0)
1930 printf("via: %s\n", committer);
1931 if (got_object_commit_get_nparents(commit) > 1) {
1932 const struct got_object_id_queue *parent_ids;
1933 struct got_object_qid *qid;
1935 parent_ids = got_object_commit_get_parent_ids(commit);
1936 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1937 err = got_object_id_str(&id_str, qid->id);
1940 printf("parent %d: %s\n", n++, id_str);
1945 err = got_object_commit_get_logmsg(&logmsg0, commit);
1951 line = strsep(&logmsg, "\n");
1953 printf(" %s\n", line);
1958 err = print_patch(commit, id, path, diff_context, repo);
1963 if (fflush(stdout) != 0 && err == NULL)
1964 err = got_error_from_errno("fflush");
1968 static const struct got_error *
1969 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1970 const char *path, int show_patch, const char *search_pattern,
1971 int diff_context, int limit, int log_branches,
1972 struct got_reflist_head *refs)
1974 const struct got_error *err;
1975 struct got_commit_graph *graph;
1979 if (search_pattern &&
1980 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1981 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1983 err = got_commit_graph_open(&graph, path, !log_branches);
1986 err = got_commit_graph_iter_start(graph, root_id, repo,
1987 check_cancelled, NULL);
1991 struct got_commit_object *commit;
1992 struct got_object_id *id;
1994 if (sigint_received || sigpipe_received)
1997 err = got_commit_graph_iter_next(&id, graph, repo,
1998 check_cancelled, NULL);
2000 if (err->code == GOT_ERR_ITER_COMPLETED)
2007 err = got_object_open_as_commit(&commit, repo, id);
2011 if (search_pattern) {
2012 err = match_logmsg(&have_match, id, commit, ®ex);
2014 got_object_commit_close(commit);
2017 if (have_match == 0) {
2018 got_object_commit_close(commit);
2023 err = print_commit(commit, id, repo, path, show_patch,
2024 diff_context, refs);
2025 got_object_commit_close(commit);
2026 if (err || (limit && --limit == 0))
2032 got_commit_graph_close(graph);
2039 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2040 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2045 get_default_log_limit(void)
2047 const char *got_default_log_limit;
2051 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2052 if (got_default_log_limit == NULL)
2054 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2060 static const struct got_error *
2061 cmd_log(int argc, char *argv[])
2063 const struct got_error *error;
2064 struct got_repository *repo = NULL;
2065 struct got_worktree *worktree = NULL;
2066 struct got_commit_object *commit = NULL;
2067 struct got_object_id *id = NULL;
2068 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2069 const char *start_commit = NULL, *search_pattern = NULL;
2070 int diff_context = -1, ch;
2071 int show_patch = 0, limit = 0, log_branches = 0;
2073 struct got_reflist_head refs;
2075 SIMPLEQ_INIT(&refs);
2078 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2084 limit = get_default_log_limit();
2086 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2092 start_commit = optarg;
2095 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2098 err(1, "-C option %s", errstr);
2101 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2103 err(1, "-l option %s", errstr);
2109 repo_path = realpath(optarg, NULL);
2110 if (repo_path == NULL)
2111 return got_error_from_errno2("realpath",
2113 got_path_strip_trailing_slashes(repo_path);
2116 search_pattern = optarg;
2127 if (diff_context == -1)
2129 else if (!show_patch)
2130 errx(1, "-C reguires -p");
2132 cwd = getcwd(NULL, 0);
2134 error = got_error_from_errno("getcwd");
2138 error = got_worktree_open(&worktree, cwd);
2139 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2146 error = got_error_from_errno("strdup");
2149 } else if (argc == 1) {
2151 error = got_worktree_resolve_path(&path, worktree,
2156 path = strdup(argv[0]);
2158 error = got_error_from_errno("strdup");
2165 if (repo_path == NULL) {
2166 repo_path = worktree ?
2167 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2169 if (repo_path == NULL) {
2170 error = got_error_from_errno("strdup");
2174 error = got_repo_open(&repo, repo_path, NULL);
2178 error = apply_unveil(got_repo_get_path(repo), 1,
2179 worktree ? got_worktree_get_root_path(worktree) : NULL);
2183 if (start_commit == NULL) {
2184 struct got_reference *head_ref;
2185 error = got_ref_open(&head_ref, repo,
2186 worktree ? got_worktree_get_head_ref_name(worktree)
2190 error = got_ref_resolve(&id, repo, head_ref);
2191 got_ref_close(head_ref);
2194 error = got_object_open_as_commit(&commit, repo, id);
2196 struct got_reference *ref;
2197 error = got_ref_open(&ref, repo, start_commit, 0);
2198 if (error == NULL) {
2200 error = got_ref_resolve(&id, repo, ref);
2204 error = got_object_get_type(&obj_type, repo, id);
2207 if (obj_type == GOT_OBJ_TYPE_TAG) {
2208 struct got_tag_object *tag;
2209 error = got_object_open_as_tag(&tag, repo, id);
2212 if (got_object_tag_get_object_type(tag) !=
2213 GOT_OBJ_TYPE_COMMIT) {
2214 got_object_tag_close(tag);
2215 error = got_error(GOT_ERR_OBJ_TYPE);
2219 id = got_object_id_dup(
2220 got_object_tag_get_object_id(tag));
2222 error = got_error_from_errno(
2223 "got_object_id_dup");
2224 got_object_tag_close(tag);
2227 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2228 error = got_error(GOT_ERR_OBJ_TYPE);
2231 error = got_object_open_as_commit(&commit, repo, id);
2235 if (commit == NULL) {
2236 error = got_repo_match_object_id_prefix(&id,
2237 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2246 const char *prefix = got_worktree_get_path_prefix(worktree);
2248 if (asprintf(&p, "%s%s%s", prefix,
2249 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2250 error = got_error_from_errno("asprintf");
2253 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2256 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2261 path = in_repo_path;
2264 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2268 error = print_commits(id, repo, path, show_patch, search_pattern,
2269 diff_context, limit, log_branches, &refs);
2276 got_worktree_close(worktree);
2278 const struct got_error *repo_error;
2279 repo_error = got_repo_close(repo);
2283 got_ref_list_free(&refs);
2290 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2291 "[-w] [object1 object2 | path]\n", getprogname());
2295 struct print_diff_arg {
2296 struct got_repository *repo;
2297 struct got_worktree *worktree;
2302 int ignore_whitespace;
2305 static const struct got_error *
2306 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2307 const char *path, struct got_object_id *blob_id,
2308 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2309 int dirfd, const char *de_name)
2311 struct print_diff_arg *a = arg;
2312 const struct got_error *err = NULL;
2313 struct got_blob_object *blob1 = NULL;
2316 char *abspath = NULL, *label1 = NULL;
2319 if (a->diff_staged) {
2320 if (staged_status != GOT_STATUS_MODIFY &&
2321 staged_status != GOT_STATUS_ADD &&
2322 staged_status != GOT_STATUS_DELETE)
2325 if (staged_status == GOT_STATUS_DELETE)
2327 if (status == GOT_STATUS_NONEXISTENT)
2328 return got_error_set_errno(ENOENT, path);
2329 if (status != GOT_STATUS_MODIFY &&
2330 status != GOT_STATUS_ADD &&
2331 status != GOT_STATUS_DELETE &&
2332 status != GOT_STATUS_CONFLICT)
2336 if (!a->header_shown) {
2337 printf("diff %s %s%s\n", a->id_str,
2338 got_worktree_get_root_path(a->worktree),
2339 a->diff_staged ? " (staged changes)" : "");
2340 a->header_shown = 1;
2343 if (a->diff_staged) {
2344 const char *label1 = NULL, *label2 = NULL;
2345 switch (staged_status) {
2346 case GOT_STATUS_MODIFY:
2350 case GOT_STATUS_ADD:
2353 case GOT_STATUS_DELETE:
2357 return got_error(GOT_ERR_FILE_STATUS);
2359 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2360 label1, label2, a->diff_context, a->ignore_whitespace,
2364 if (staged_status == GOT_STATUS_ADD ||
2365 staged_status == GOT_STATUS_MODIFY) {
2367 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2371 err = got_object_id_str(&id_str, staged_blob_id);
2374 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2375 err = got_error_from_errno("asprintf");
2380 } else if (status != GOT_STATUS_ADD) {
2381 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2386 if (status != GOT_STATUS_DELETE) {
2387 if (asprintf(&abspath, "%s/%s",
2388 got_worktree_get_root_path(a->worktree), path) == -1) {
2389 err = got_error_from_errno("asprintf");
2394 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2396 err = got_error_from_errno2("openat", abspath);
2400 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2402 err = got_error_from_errno2("open", abspath);
2406 if (fstat(fd, &sb) == -1) {
2407 err = got_error_from_errno2("fstat", abspath);
2410 f2 = fdopen(fd, "r");
2412 err = got_error_from_errno2("fdopen", abspath);
2419 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2420 a->diff_context, a->ignore_whitespace, stdout);
2423 got_object_blob_close(blob1);
2424 if (f2 && fclose(f2) == EOF && err == NULL)
2425 err = got_error_from_errno("fclose");
2426 if (fd != -1 && close(fd) == -1 && err == NULL)
2427 err = got_error_from_errno("close");
2432 static const struct got_error *
2433 cmd_diff(int argc, char *argv[])
2435 const struct got_error *error;
2436 struct got_repository *repo = NULL;
2437 struct got_worktree *worktree = NULL;
2438 char *cwd = NULL, *repo_path = NULL;
2439 struct got_object_id *id1 = NULL, *id2 = NULL;
2440 const char *id_str1 = NULL, *id_str2 = NULL;
2441 char *label1 = NULL, *label2 = NULL;
2443 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2448 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2453 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2456 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2459 err(1, "-C option %s", errstr);
2462 repo_path = realpath(optarg, NULL);
2463 if (repo_path == NULL)
2464 return got_error_from_errno2("realpath",
2466 got_path_strip_trailing_slashes(repo_path);
2472 ignore_whitespace = 1;
2483 cwd = getcwd(NULL, 0);
2485 error = got_error_from_errno("getcwd");
2488 error = got_worktree_open(&worktree, cwd);
2489 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2492 if (worktree == NULL) {
2493 error = got_error(GOT_ERR_NOT_WORKTREE);
2498 "-r option can't be used when diffing a work tree");
2499 repo_path = strdup(got_worktree_get_repo_path(worktree));
2500 if (repo_path == NULL) {
2501 error = got_error_from_errno("strdup");
2505 error = got_worktree_resolve_path(&path, worktree,
2512 error = got_error_from_errno("strdup");
2516 } else if (argc == 2) {
2518 errx(1, "-s option can't be used when diffing "
2519 "objects in repository");
2522 if (worktree && repo_path == NULL) {
2524 strdup(got_worktree_get_repo_path(worktree));
2525 if (repo_path == NULL) {
2526 error = got_error_from_errno("strdup");
2533 if (repo_path == NULL) {
2534 repo_path = getcwd(NULL, 0);
2535 if (repo_path == NULL)
2536 return got_error_from_errno("getcwd");
2539 error = got_repo_open(&repo, repo_path, NULL);
2544 error = apply_unveil(got_repo_get_path(repo), 1,
2545 worktree ? got_worktree_get_root_path(worktree) : NULL);
2550 struct print_diff_arg arg;
2551 struct got_pathlist_head paths;
2556 error = got_object_id_str(&id_str,
2557 got_worktree_get_base_commit_id(worktree));
2561 arg.worktree = worktree;
2562 arg.diff_context = diff_context;
2563 arg.id_str = id_str;
2564 arg.header_shown = 0;
2565 arg.diff_staged = diff_staged;
2566 arg.ignore_whitespace = ignore_whitespace;
2568 error = got_pathlist_append(&paths, path, NULL);
2572 error = got_worktree_status(worktree, &paths, repo, print_diff,
2573 &arg, check_cancelled, NULL);
2575 got_pathlist_free(&paths);
2579 error = got_repo_match_object_id(&id1, &label1, id_str1,
2580 GOT_OBJ_TYPE_ANY, 1, repo);
2584 error = got_repo_match_object_id(&id2, &label2, id_str2,
2585 GOT_OBJ_TYPE_ANY, 1, repo);
2589 error = got_object_get_type(&type1, repo, id1);
2593 error = got_object_get_type(&type2, repo, id2);
2597 if (type1 != type2) {
2598 error = got_error(GOT_ERR_OBJ_TYPE);
2603 case GOT_OBJ_TYPE_BLOB:
2604 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2605 diff_context, ignore_whitespace, repo, stdout);
2607 case GOT_OBJ_TYPE_TREE:
2608 error = got_diff_objects_as_trees(id1, id2, "", "",
2609 diff_context, ignore_whitespace, repo, stdout);
2611 case GOT_OBJ_TYPE_COMMIT:
2612 printf("diff %s %s\n", label1, label2);
2613 error = got_diff_objects_as_commits(id1, id2, diff_context,
2614 ignore_whitespace, repo, stdout);
2617 error = got_error(GOT_ERR_OBJ_TYPE);
2626 got_worktree_close(worktree);
2628 const struct got_error *repo_error;
2629 repo_error = got_repo_close(repo);
2640 "usage: %s blame [-c commit] [-r repository-path] path\n",
2649 char datebuf[11]; /* YYYY-MM-DD + NUL */
2652 struct blame_cb_args {
2653 struct blame_line *lines;
2657 off_t *line_offsets;
2659 struct got_repository *repo;
2662 static const struct got_error *
2663 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2665 const struct got_error *err = NULL;
2666 struct blame_cb_args *a = arg;
2667 struct blame_line *bline;
2669 size_t linesize = 0;
2670 struct got_commit_object *commit = NULL;
2673 time_t committer_time;
2675 if (nlines != a->nlines ||
2676 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2677 return got_error(GOT_ERR_RANGE);
2679 if (sigint_received)
2680 return got_error(GOT_ERR_ITER_COMPLETED);
2683 return NULL; /* no change in this commit */
2685 /* Annotate this line. */
2686 bline = &a->lines[lineno - 1];
2687 if (bline->annotated)
2689 err = got_object_id_str(&bline->id_str, id);
2693 err = got_object_open_as_commit(&commit, a->repo, id);
2697 bline->committer = strdup(got_object_commit_get_committer(commit));
2698 if (bline->committer == NULL) {
2699 err = got_error_from_errno("strdup");
2703 committer_time = got_object_commit_get_committer_time(commit);
2704 if (localtime_r(&committer_time, &tm) == NULL)
2705 return got_error_from_errno("localtime_r");
2706 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2707 &tm) >= sizeof(bline->datebuf)) {
2708 err = got_error(GOT_ERR_NO_SPACE);
2711 bline->annotated = 1;
2713 /* Print lines annotated so far. */
2714 bline = &a->lines[a->lineno_cur - 1];
2715 if (!bline->annotated)
2718 offset = a->line_offsets[a->lineno_cur - 1];
2719 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2720 err = got_error_from_errno("fseeko");
2724 while (bline->annotated) {
2725 char *smallerthan, *at, *nl, *committer;
2728 if (getline(&line, &linesize, a->f) == -1) {
2730 err = got_error_from_errno("getline");
2734 committer = bline->committer;
2735 smallerthan = strchr(committer, '<');
2736 if (smallerthan && smallerthan[1] != '\0')
2737 committer = smallerthan + 1;
2738 at = strchr(committer, '@');
2741 len = strlen(committer);
2743 committer[8] = '\0';
2745 nl = strchr(line, '\n');
2748 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2749 bline->id_str, bline->datebuf, committer, line);
2752 bline = &a->lines[a->lineno_cur - 1];
2756 got_object_commit_close(commit);
2761 static const struct got_error *
2762 cmd_blame(int argc, char *argv[])
2764 const struct got_error *error;
2765 struct got_repository *repo = NULL;
2766 struct got_worktree *worktree = NULL;
2767 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2768 struct got_object_id *obj_id = NULL;
2769 struct got_object_id *commit_id = NULL;
2770 struct got_blob_object *blob = NULL;
2771 char *commit_id_str = NULL;
2772 struct blame_cb_args bca;
2773 int ch, obj_type, i;
2776 memset(&bca, 0, sizeof(bca));
2779 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2784 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2787 commit_id_str = optarg;
2790 repo_path = realpath(optarg, NULL);
2791 if (repo_path == NULL)
2792 return got_error_from_errno2("realpath",
2794 got_path_strip_trailing_slashes(repo_path);
2810 cwd = getcwd(NULL, 0);
2812 error = got_error_from_errno("getcwd");
2815 if (repo_path == NULL) {
2816 error = got_worktree_open(&worktree, cwd);
2817 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2823 strdup(got_worktree_get_repo_path(worktree));
2824 if (repo_path == NULL)
2825 error = got_error_from_errno("strdup");
2829 repo_path = strdup(cwd);
2830 if (repo_path == NULL) {
2831 error = got_error_from_errno("strdup");
2837 error = got_repo_open(&repo, repo_path, NULL);
2841 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2846 const char *prefix = got_worktree_get_path_prefix(worktree);
2847 char *p, *worktree_subdir = cwd +
2848 strlen(got_worktree_get_root_path(worktree));
2849 if (asprintf(&p, "%s%s%s%s%s",
2850 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2851 worktree_subdir, worktree_subdir[0] ? "/" : "",
2853 error = got_error_from_errno("asprintf");
2856 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2859 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2864 if (commit_id_str == NULL) {
2865 struct got_reference *head_ref;
2866 error = got_ref_open(&head_ref, repo, worktree ?
2867 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2870 error = got_ref_resolve(&commit_id, repo, head_ref);
2871 got_ref_close(head_ref);
2875 error = got_repo_match_object_id(&commit_id, NULL,
2876 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2881 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2885 error = got_object_get_type(&obj_type, repo, obj_id);
2889 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2890 error = got_error(GOT_ERR_OBJ_TYPE);
2894 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2897 bca.f = got_opentemp();
2898 if (bca.f == NULL) {
2899 error = got_error_from_errno("got_opentemp");
2902 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2903 &bca.line_offsets, bca.f, blob);
2904 if (error || bca.nlines == 0)
2907 /* Don't include \n at EOF in the blame line count. */
2908 if (bca.line_offsets[bca.nlines - 1] == filesize)
2911 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2912 if (bca.lines == NULL) {
2913 error = got_error_from_errno("calloc");
2917 bca.nlines_prec = 0;
2925 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2926 check_cancelled, NULL);
2934 got_object_blob_close(blob);
2936 got_worktree_close(worktree);
2938 const struct got_error *repo_error;
2939 repo_error = got_repo_close(repo);
2944 for (i = 0; i < bca.nlines; i++) {
2945 struct blame_line *bline = &bca.lines[i];
2946 free(bline->id_str);
2947 free(bline->committer);
2951 free(bca.line_offsets);
2952 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2953 error = got_error_from_errno("fclose");
2961 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2967 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2968 const char *root_path)
2970 int is_root_path = (strcmp(path, root_path) == 0);
2971 const char *modestr = "";
2972 mode_t mode = got_tree_entry_get_mode(te);
2974 path += strlen(root_path);
2975 while (path[0] == '/')
2978 if (got_object_tree_entry_is_submodule(te))
2980 else if (S_ISLNK(mode))
2982 else if (S_ISDIR(mode))
2984 else if (mode & S_IXUSR)
2987 printf("%s%s%s%s%s\n", id ? id : "", path,
2988 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2991 static const struct got_error *
2992 print_tree(const char *path, struct got_object_id *commit_id,
2993 int show_ids, int recurse, const char *root_path,
2994 struct got_repository *repo)
2996 const struct got_error *err = NULL;
2997 struct got_object_id *tree_id = NULL;
2998 struct got_tree_object *tree = NULL;
3001 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3005 err = got_object_open_as_tree(&tree, repo, tree_id);
3008 nentries = got_object_tree_get_nentries(tree);
3009 for (i = 0; i < nentries; i++) {
3010 struct got_tree_entry *te;
3013 if (sigint_received || sigpipe_received)
3016 te = got_object_tree_get_entry(tree, i);
3019 err = got_object_id_str(&id_str,
3020 got_tree_entry_get_id(te));
3023 if (asprintf(&id, "%s ", id_str) == -1) {
3024 err = got_error_from_errno("asprintf");
3030 print_entry(te, id, path, root_path);
3033 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3035 if (asprintf(&child_path, "%s%s%s", path,
3036 path[0] == '/' && path[1] == '\0' ? "" : "/",
3037 got_tree_entry_get_name(te)) == -1) {
3038 err = got_error_from_errno("asprintf");
3041 err = print_tree(child_path, commit_id, show_ids, 1,
3050 got_object_tree_close(tree);
3055 static const struct got_error *
3056 cmd_tree(int argc, char *argv[])
3058 const struct got_error *error;
3059 struct got_repository *repo = NULL;
3060 struct got_worktree *worktree = NULL;
3062 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3063 struct got_object_id *commit_id = NULL;
3064 char *commit_id_str = NULL;
3065 int show_ids = 0, recurse = 0;
3069 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3074 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3077 commit_id_str = optarg;
3080 repo_path = realpath(optarg, NULL);
3081 if (repo_path == NULL)
3082 return got_error_from_errno2("realpath",
3084 got_path_strip_trailing_slashes(repo_path);
3108 cwd = getcwd(NULL, 0);
3110 error = got_error_from_errno("getcwd");
3113 if (repo_path == NULL) {
3114 error = got_worktree_open(&worktree, cwd);
3115 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3121 strdup(got_worktree_get_repo_path(worktree));
3122 if (repo_path == NULL)
3123 error = got_error_from_errno("strdup");
3127 repo_path = strdup(cwd);
3128 if (repo_path == NULL) {
3129 error = got_error_from_errno("strdup");
3135 error = got_repo_open(&repo, repo_path, NULL);
3139 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3145 char *p, *worktree_subdir = cwd +
3146 strlen(got_worktree_get_root_path(worktree));
3147 if (asprintf(&p, "%s/%s",
3148 got_worktree_get_path_prefix(worktree),
3149 worktree_subdir) == -1) {
3150 error = got_error_from_errno("asprintf");
3153 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3160 if (in_repo_path == NULL) {
3161 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3166 if (commit_id_str == NULL) {
3167 struct got_reference *head_ref;
3168 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3171 error = got_ref_resolve(&commit_id, repo, head_ref);
3172 got_ref_close(head_ref);
3176 error = got_repo_match_object_id(&commit_id, NULL,
3177 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3182 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3183 in_repo_path, repo);
3190 got_worktree_close(worktree);
3192 const struct got_error *repo_error;
3193 repo_error = got_repo_close(repo);
3203 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3207 static const struct got_error *
3208 print_status(void *arg, unsigned char status, unsigned char staged_status,
3209 const char *path, struct got_object_id *blob_id,
3210 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3211 int dirfd, const char *de_name)
3213 if (status == staged_status && (status == GOT_STATUS_DELETE))
3214 status = GOT_STATUS_NO_CHANGE;
3215 printf("%c%c %s\n", status, staged_status, path);
3219 static const struct got_error *
3220 cmd_status(int argc, char *argv[])
3222 const struct got_error *error = NULL;
3223 struct got_repository *repo = NULL;
3224 struct got_worktree *worktree = NULL;
3226 struct got_pathlist_head paths;
3227 struct got_pathlist_entry *pe;
3232 while ((ch = getopt(argc, argv, "")) != -1) {
3244 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3248 cwd = getcwd(NULL, 0);
3250 error = got_error_from_errno("getcwd");
3254 error = got_worktree_open(&worktree, cwd);
3258 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3263 error = apply_unveil(got_repo_get_path(repo), 1,
3264 got_worktree_get_root_path(worktree));
3268 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3272 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3273 check_cancelled, NULL);
3275 TAILQ_FOREACH(pe, &paths, entry)
3276 free((char *)pe->path);
3277 got_pathlist_free(&paths);
3286 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3291 static const struct got_error *
3292 list_refs(struct got_repository *repo)
3294 static const struct got_error *err = NULL;
3295 struct got_reflist_head refs;
3296 struct got_reflist_entry *re;
3298 SIMPLEQ_INIT(&refs);
3299 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3303 SIMPLEQ_FOREACH(re, &refs, entry) {
3305 refstr = got_ref_to_str(re->ref);
3307 return got_error_from_errno("got_ref_to_str");
3308 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3312 got_ref_list_free(&refs);
3316 static const struct got_error *
3317 delete_ref(struct got_repository *repo, const char *refname)
3319 const struct got_error *err = NULL;
3320 struct got_reference *ref;
3322 err = got_ref_open(&ref, repo, refname, 0);
3326 err = got_ref_delete(ref, repo);
3331 static const struct got_error *
3332 add_ref(struct got_repository *repo, const char *refname, const char *target)
3334 const struct got_error *err = NULL;
3335 struct got_object_id *id;
3336 struct got_reference *ref = NULL;
3339 * Don't let the user create a reference name with a leading '-'.
3340 * While technically a valid reference name, this case is usually
3341 * an unintended typo.
3343 if (refname[0] == '-')
3344 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3346 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3349 struct got_reference *target_ref;
3351 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3353 err = got_ref_open(&target_ref, repo, target, 0);
3356 err = got_ref_resolve(&id, repo, target_ref);
3357 got_ref_close(target_ref);
3362 err = got_ref_alloc(&ref, refname, id);
3366 err = got_ref_write(ref, repo);
3374 static const struct got_error *
3375 add_symref(struct got_repository *repo, const char *refname, const char *target)
3377 const struct got_error *err = NULL;
3378 struct got_reference *ref = NULL;
3379 struct got_reference *target_ref = NULL;
3382 * Don't let the user create a reference name with a leading '-'.
3383 * While technically a valid reference name, this case is usually
3384 * an unintended typo.
3386 if (refname[0] == '-')
3387 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3389 err = got_ref_open(&target_ref, repo, target, 0);
3393 err = got_ref_alloc_symref(&ref, refname, target_ref);
3397 err = got_ref_write(ref, repo);
3400 got_ref_close(target_ref);
3406 static const struct got_error *
3407 cmd_ref(int argc, char *argv[])
3409 const struct got_error *error = NULL;
3410 struct got_repository *repo = NULL;
3411 struct got_worktree *worktree = NULL;
3412 char *cwd = NULL, *repo_path = NULL;
3413 int ch, do_list = 0, create_symref = 0;
3414 const char *delref = NULL;
3416 /* TODO: Add -s option for adding symbolic references. */
3417 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3423 repo_path = realpath(optarg, NULL);
3424 if (repo_path == NULL)
3425 return got_error_from_errno2("realpath",
3427 got_path_strip_trailing_slashes(repo_path);
3441 if (do_list && delref)
3442 errx(1, "-l and -d options are mutually exclusive\n");
3447 if (do_list || delref) {
3449 errx(1, "-s option cannot be used together with the "
3450 "-l or -d options");
3453 } else if (argc != 2)
3458 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3462 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3463 "sendfd unveil", NULL) == -1)
3467 cwd = getcwd(NULL, 0);
3469 error = got_error_from_errno("getcwd");
3473 if (repo_path == NULL) {
3474 error = got_worktree_open(&worktree, cwd);
3475 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3481 strdup(got_worktree_get_repo_path(worktree));
3482 if (repo_path == NULL)
3483 error = got_error_from_errno("strdup");
3487 repo_path = strdup(cwd);
3488 if (repo_path == NULL) {
3489 error = got_error_from_errno("strdup");
3495 error = got_repo_open(&repo, repo_path, NULL);
3499 error = apply_unveil(got_repo_get_path(repo), do_list,
3500 worktree ? got_worktree_get_root_path(worktree) : NULL);
3505 error = list_refs(repo);
3507 error = delete_ref(repo, delref);
3508 else if (create_symref)
3509 error = add_symref(repo, argv[0], argv[1]);
3511 error = add_ref(repo, argv[0], argv[1]);
3514 got_repo_close(repo);
3516 got_worktree_close(worktree);
3526 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3527 "[name]\n", getprogname());
3531 static const struct got_error *
3532 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3533 struct got_reference *ref)
3535 const struct got_error *err = NULL;
3536 const char *refname, *marker = " ";
3539 refname = got_ref_get_name(ref);
3540 if (worktree && strcmp(refname,
3541 got_worktree_get_head_ref_name(worktree)) == 0) {
3542 struct got_object_id *id = NULL;
3544 err = got_ref_resolve(&id, repo, ref);
3547 if (got_object_id_cmp(id,
3548 got_worktree_get_base_commit_id(worktree)) == 0)
3555 if (strncmp(refname, "refs/heads/", 11) == 0)
3557 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3560 refstr = got_ref_to_str(ref);
3562 return got_error_from_errno("got_ref_to_str");
3564 printf("%s%s: %s\n", marker, refname, refstr);
3569 static const struct got_error *
3570 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3572 const char *refname;
3574 if (worktree == NULL)
3575 return got_error(GOT_ERR_NOT_WORKTREE);
3577 refname = got_worktree_get_head_ref_name(worktree);
3579 if (strncmp(refname, "refs/heads/", 11) == 0)
3581 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3584 printf("%s\n", refname);
3589 static const struct got_error *
3590 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3592 static const struct got_error *err = NULL;
3593 struct got_reflist_head refs;
3594 struct got_reflist_entry *re;
3595 struct got_reference *temp_ref = NULL;
3596 int rebase_in_progress, histedit_in_progress;
3598 SIMPLEQ_INIT(&refs);
3601 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3606 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3611 if (rebase_in_progress || histedit_in_progress) {
3612 err = got_ref_open(&temp_ref, repo,
3613 got_worktree_get_head_ref_name(worktree), 0);
3616 list_branch(repo, worktree, temp_ref);
3617 got_ref_close(temp_ref);
3621 err = got_ref_list(&refs, repo, "refs/heads",
3622 got_ref_cmp_by_name, NULL);
3626 SIMPLEQ_FOREACH(re, &refs, entry)
3627 list_branch(repo, worktree, re->ref);
3629 got_ref_list_free(&refs);
3633 static const struct got_error *
3634 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3635 const char *branch_name)
3637 const struct got_error *err = NULL;
3638 struct got_reference *ref = NULL;
3641 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3642 return got_error_from_errno("asprintf");
3644 err = got_ref_open(&ref, repo, refname, 0);
3649 strcmp(got_worktree_get_head_ref_name(worktree),
3650 got_ref_get_name(ref)) == 0) {
3651 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3652 "will not delete this work tree's current branch");
3656 err = got_ref_delete(ref, repo);
3664 static const struct got_error *
3665 add_branch(struct got_repository *repo, const char *branch_name,
3666 struct got_object_id *base_commit_id)
3668 const struct got_error *err = NULL;
3669 struct got_reference *ref = NULL;
3670 char *base_refname = NULL, *refname = NULL;
3673 * Don't let the user create a branch name with a leading '-'.
3674 * While technically a valid reference name, this case is usually
3675 * an unintended typo.
3677 if (branch_name[0] == '-')
3678 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3680 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3681 err = got_error_from_errno("asprintf");
3685 err = got_ref_open(&ref, repo, refname, 0);
3687 err = got_error(GOT_ERR_BRANCH_EXISTS);
3689 } else if (err->code != GOT_ERR_NOT_REF)
3692 err = got_ref_alloc(&ref, refname, base_commit_id);
3696 err = got_ref_write(ref, repo);
3705 static const struct got_error *
3706 cmd_branch(int argc, char *argv[])
3708 const struct got_error *error = NULL;
3709 struct got_repository *repo = NULL;
3710 struct got_worktree *worktree = NULL;
3711 char *cwd = NULL, *repo_path = NULL;
3712 int ch, do_list = 0, do_show = 0, do_update = 1;
3713 const char *delref = NULL, *commit_id_arg = NULL;
3714 struct got_reference *ref = NULL;
3715 struct got_pathlist_head paths;
3716 struct got_pathlist_entry *pe;
3717 struct got_object_id *commit_id = NULL;
3718 char *commit_id_str = NULL;
3722 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3725 commit_id_arg = optarg;
3731 repo_path = realpath(optarg, NULL);
3732 if (repo_path == NULL)
3733 return got_error_from_errno2("realpath",
3735 got_path_strip_trailing_slashes(repo_path);
3749 if (do_list && delref)
3750 errx(1, "-l and -d options are mutually exclusive\n");
3755 if (!do_list && !delref && argc == 0)
3758 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3759 errx(1, "-c option can only be used when creating a branch");
3761 if (do_list || delref) {
3764 } else if (!do_show && argc != 1)
3768 if (do_list || do_show) {
3769 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3773 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3774 "sendfd unveil", NULL) == -1)
3778 cwd = getcwd(NULL, 0);
3780 error = got_error_from_errno("getcwd");
3784 if (repo_path == NULL) {
3785 error = got_worktree_open(&worktree, cwd);
3786 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3792 strdup(got_worktree_get_repo_path(worktree));
3793 if (repo_path == NULL)
3794 error = got_error_from_errno("strdup");
3798 repo_path = strdup(cwd);
3799 if (repo_path == NULL) {
3800 error = got_error_from_errno("strdup");
3806 error = got_repo_open(&repo, repo_path, NULL);
3810 error = apply_unveil(got_repo_get_path(repo), do_list,
3811 worktree ? got_worktree_get_root_path(worktree) : NULL);
3816 error = show_current_branch(repo, worktree);
3818 error = list_branches(repo, worktree);
3820 error = delete_branch(repo, worktree, delref);
3822 if (commit_id_arg == NULL)
3823 commit_id_arg = worktree ?
3824 got_worktree_get_head_ref_name(worktree) :
3826 error = got_repo_match_object_id(&commit_id, NULL,
3827 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3830 error = add_branch(repo, argv[0], commit_id);
3833 if (worktree && do_update) {
3834 int did_something = 0;
3835 char *branch_refname = NULL;
3837 error = got_object_id_str(&commit_id_str, commit_id);
3840 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3844 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3846 error = got_error_from_errno("asprintf");
3849 error = got_ref_open(&ref, repo, branch_refname, 0);
3850 free(branch_refname);
3853 error = switch_head_ref(ref, commit_id, worktree,
3857 error = got_worktree_set_base_commit_id(worktree, repo,
3861 error = got_worktree_checkout_files(worktree, &paths,
3862 repo, update_progress, &did_something,
3863 check_cancelled, NULL);
3867 printf("Updated to commit %s\n", commit_id_str);
3874 got_repo_close(repo);
3876 got_worktree_close(worktree);
3880 free(commit_id_str);
3881 TAILQ_FOREACH(pe, &paths, entry)
3882 free((char *)pe->path);
3883 got_pathlist_free(&paths);
3892 "usage: %s tag [-c commit] [-r repository] [-l] "
3893 "[-m message] name\n", getprogname());
3898 static const struct got_error *
3899 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3901 const struct got_error *err = NULL;
3902 struct got_reflist_entry *re, *se, *new;
3903 struct got_object_id *re_id, *se_id;
3904 struct got_tag_object *re_tag, *se_tag;
3905 time_t re_time, se_time;
3907 SIMPLEQ_FOREACH(re, tags, entry) {
3908 se = SIMPLEQ_FIRST(sorted);
3910 err = got_reflist_entry_dup(&new, re);
3913 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3916 err = got_ref_resolve(&re_id, repo, re->ref);
3919 err = got_object_open_as_tag(&re_tag, repo, re_id);
3923 re_time = got_object_tag_get_tagger_time(re_tag);
3924 got_object_tag_close(re_tag);
3928 err = got_ref_resolve(&se_id, repo, re->ref);
3931 err = got_object_open_as_tag(&se_tag, repo, se_id);
3935 se_time = got_object_tag_get_tagger_time(se_tag);
3936 got_object_tag_close(se_tag);
3938 if (se_time > re_time) {
3939 err = got_reflist_entry_dup(&new, re);
3942 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3945 se = SIMPLEQ_NEXT(se, entry);
3954 static const struct got_error *
3955 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3957 static const struct got_error *err = NULL;
3958 struct got_reflist_head refs;
3959 struct got_reflist_entry *re;
3961 SIMPLEQ_INIT(&refs);
3963 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3967 SIMPLEQ_FOREACH(re, &refs, entry) {
3968 const char *refname;
3969 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3973 struct got_object_id *id;
3974 struct got_tag_object *tag;
3975 struct got_commit_object *commit = NULL;
3977 refname = got_ref_get_name(re->ref);
3978 if (strncmp(refname, "refs/tags/", 10) != 0)
3981 refstr = got_ref_to_str(re->ref);
3982 if (refstr == NULL) {
3983 err = got_error_from_errno("got_ref_to_str");
3986 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3989 err = got_ref_resolve(&id, repo, re->ref);
3992 err = got_object_open_as_tag(&tag, repo, id);
3994 if (err->code != GOT_ERR_OBJ_TYPE) {
3998 /* "lightweight" tag */
3999 err = got_object_open_as_commit(&commit, repo, id);
4004 tagger = got_object_commit_get_committer(commit);
4006 got_object_commit_get_committer_time(commit);
4007 err = got_object_id_str(&id_str, id);
4013 tagger = got_object_tag_get_tagger(tag);
4014 tagger_time = got_object_tag_get_tagger_time(tag);
4015 err = got_object_id_str(&id_str,
4016 got_object_tag_get_object_id(tag));
4020 printf("from: %s\n", tagger);
4021 datestr = get_datestr(&tagger_time, datebuf);
4023 printf("date: %s UTC\n", datestr);
4025 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4027 switch (got_object_tag_get_object_type(tag)) {
4028 case GOT_OBJ_TYPE_BLOB:
4029 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4032 case GOT_OBJ_TYPE_TREE:
4033 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4036 case GOT_OBJ_TYPE_COMMIT:
4037 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4040 case GOT_OBJ_TYPE_TAG:
4041 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4050 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4053 got_object_commit_close(commit);
4055 tagmsg0 = strdup(got_object_tag_get_message(tag));
4056 got_object_tag_close(tag);
4057 if (tagmsg0 == NULL) {
4058 err = got_error_from_errno("strdup");
4065 line = strsep(&tagmsg, "\n");
4067 printf(" %s\n", line);
4072 got_ref_list_free(&refs);
4076 static const struct got_error *
4077 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4078 const char *tag_name, const char *repo_path)
4080 const struct got_error *err = NULL;
4081 char *template = NULL, *initial_content = NULL;
4082 char *editor = NULL;
4085 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4086 err = got_error_from_errno("asprintf");
4090 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4091 commit_id_str, tag_name) == -1) {
4092 err = got_error_from_errno("asprintf");
4096 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4100 dprintf(fd, initial_content);
4103 err = get_editor(&editor);
4106 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4108 free(initial_content);
4112 /* Editor is done; we can now apply unveil(2) */
4114 err = apply_unveil(repo_path, 0, NULL);
4123 static const struct got_error *
4124 add_tag(struct got_repository *repo, const char *tag_name,
4125 const char *commit_arg, const char *tagmsg_arg)
4127 const struct got_error *err = NULL;
4128 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4129 char *label = NULL, *commit_id_str = NULL;
4130 struct got_reference *ref = NULL;
4131 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4132 char *tagmsg_path = NULL, *tag_id_str = NULL;
4133 int preserve_tagmsg = 0;
4136 * Don't let the user create a tag name with a leading '-'.
4137 * While technically a valid reference name, this case is usually
4138 * an unintended typo.
4140 if (tag_name[0] == '-')
4141 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4143 err = get_author(&tagger, repo);
4147 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4148 GOT_OBJ_TYPE_COMMIT, 1, repo);
4152 err = got_object_id_str(&commit_id_str, commit_id);
4156 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4157 refname = strdup(tag_name);
4158 if (refname == NULL) {
4159 err = got_error_from_errno("strdup");
4163 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4164 err = got_error_from_errno("asprintf");
4168 err = got_ref_open(&ref, repo, refname, 0);
4170 err = got_error(GOT_ERR_TAG_EXISTS);
4172 } else if (err->code != GOT_ERR_NOT_REF)
4175 if (tagmsg_arg == NULL) {
4176 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4177 tag_name, got_repo_get_path(repo));
4179 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4180 tagmsg_path != NULL)
4181 preserve_tagmsg = 1;
4186 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4187 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4190 preserve_tagmsg = 1;
4194 err = got_ref_alloc(&ref, refname, tag_id);
4197 preserve_tagmsg = 1;
4201 err = got_ref_write(ref, repo);
4204 preserve_tagmsg = 1;
4208 err = got_object_id_str(&tag_id_str, tag_id);
4211 preserve_tagmsg = 1;
4214 printf("Created tag %s\n", tag_id_str);
4216 if (preserve_tagmsg) {
4217 fprintf(stderr, "%s: tag message preserved in %s\n",
4218 getprogname(), tagmsg_path);
4219 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4220 err = got_error_from_errno2("unlink", tagmsg_path);
4225 free(commit_id_str);
4233 static const struct got_error *
4234 cmd_tag(int argc, char *argv[])
4236 const struct got_error *error = NULL;
4237 struct got_repository *repo = NULL;
4238 struct got_worktree *worktree = NULL;
4239 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4240 char *gitconfig_path = NULL;
4241 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4242 int ch, do_list = 0;
4244 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4247 commit_id_arg = optarg;
4253 repo_path = realpath(optarg, NULL);
4254 if (repo_path == NULL)
4255 return got_error_from_errno2("realpath",
4257 got_path_strip_trailing_slashes(repo_path);
4272 if (commit_id_arg != NULL)
4273 errx(1, "-c option can only be used when creating a tag");
4275 errx(1, "-l and -m options are mutually exclusive");
4278 } else if (argc != 1)
4285 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4289 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4290 "sendfd unveil", NULL) == -1)
4294 cwd = getcwd(NULL, 0);
4296 error = got_error_from_errno("getcwd");
4300 if (repo_path == NULL) {
4301 error = got_worktree_open(&worktree, cwd);
4302 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4308 strdup(got_worktree_get_repo_path(worktree));
4309 if (repo_path == NULL)
4310 error = got_error_from_errno("strdup");
4314 repo_path = strdup(cwd);
4315 if (repo_path == NULL) {
4316 error = got_error_from_errno("strdup");
4323 error = got_repo_open(&repo, repo_path, NULL);
4326 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4329 error = list_tags(repo, worktree);
4331 error = get_gitconfig_path(&gitconfig_path);
4334 error = got_repo_open(&repo, repo_path, gitconfig_path);
4339 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4344 if (commit_id_arg == NULL) {
4345 struct got_reference *head_ref;
4346 struct got_object_id *commit_id;
4347 error = got_ref_open(&head_ref, repo,
4348 worktree ? got_worktree_get_head_ref_name(worktree)
4352 error = got_ref_resolve(&commit_id, repo, head_ref);
4353 got_ref_close(head_ref);
4356 error = got_object_id_str(&commit_id_str, commit_id);
4362 error = add_tag(repo, tag_name,
4363 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4367 got_repo_close(repo);
4369 got_worktree_close(worktree);
4372 free(gitconfig_path);
4373 free(commit_id_str);
4380 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4385 static const struct got_error *
4386 add_progress(void *arg, unsigned char status, const char *path)
4388 while (path[0] == '/')
4390 printf("%c %s\n", status, path);
4394 static const struct got_error *
4395 cmd_add(int argc, char *argv[])
4397 const struct got_error *error = NULL;
4398 struct got_repository *repo = NULL;
4399 struct got_worktree *worktree = NULL;
4401 struct got_pathlist_head paths;
4402 struct got_pathlist_entry *pe;
4403 int ch, can_recurse = 0, no_ignores = 0;
4407 while ((ch = getopt(argc, argv, "IR")) != -1) {
4425 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4432 cwd = getcwd(NULL, 0);
4434 error = got_error_from_errno("getcwd");
4438 error = got_worktree_open(&worktree, cwd);
4442 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4447 error = apply_unveil(got_repo_get_path(repo), 1,
4448 got_worktree_get_root_path(worktree));
4452 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4456 if (!can_recurse && no_ignores) {
4457 error = got_error_msg(GOT_ERR_BAD_PATH,
4458 "disregarding ignores requires -R option");
4466 TAILQ_FOREACH(pe, &paths, entry) {
4467 if (asprintf(&ondisk_path, "%s/%s",
4468 got_worktree_get_root_path(worktree),
4470 error = got_error_from_errno("asprintf");
4473 if (lstat(ondisk_path, &sb) == -1) {
4474 if (errno == ENOENT) {
4478 error = got_error_from_errno2("lstat",
4484 if (S_ISDIR(sb.st_mode)) {
4485 error = got_error_msg(GOT_ERR_BAD_PATH,
4486 "adding directories requires -R option");
4492 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4493 NULL, repo, no_ignores);
4496 got_repo_close(repo);
4498 got_worktree_close(worktree);
4499 TAILQ_FOREACH(pe, &paths, entry)
4500 free((char *)pe->path);
4501 got_pathlist_free(&paths);
4509 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4514 static const struct got_error *
4515 print_remove_status(void *arg, unsigned char status,
4516 unsigned char staged_status, const char *path)
4518 while (path[0] == '/')
4520 if (status == GOT_STATUS_NONEXISTENT)
4522 if (status == staged_status && (status == GOT_STATUS_DELETE))
4523 status = GOT_STATUS_NO_CHANGE;
4524 printf("%c%c %s\n", status, staged_status, path);
4528 static const struct got_error *
4529 cmd_remove(int argc, char *argv[])
4531 const struct got_error *error = NULL;
4532 struct got_worktree *worktree = NULL;
4533 struct got_repository *repo = NULL;
4535 struct got_pathlist_head paths;
4536 struct got_pathlist_entry *pe;
4537 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4541 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4544 delete_local_mods = 1;
4562 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4569 cwd = getcwd(NULL, 0);
4571 error = got_error_from_errno("getcwd");
4574 error = got_worktree_open(&worktree, cwd);
4578 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4583 error = apply_unveil(got_repo_get_path(repo), 1,
4584 got_worktree_get_root_path(worktree));
4588 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4595 TAILQ_FOREACH(pe, &paths, entry) {
4596 if (asprintf(&ondisk_path, "%s/%s",
4597 got_worktree_get_root_path(worktree),
4599 error = got_error_from_errno("asprintf");
4602 if (lstat(ondisk_path, &sb) == -1) {
4603 if (errno == ENOENT) {
4607 error = got_error_from_errno2("lstat",
4613 if (S_ISDIR(sb.st_mode)) {
4614 error = got_error_msg(GOT_ERR_BAD_PATH,
4615 "removing directories requires -R option");
4621 error = got_worktree_schedule_delete(worktree, &paths,
4622 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4625 got_repo_close(repo);
4627 got_worktree_close(worktree);
4628 TAILQ_FOREACH(pe, &paths, entry)
4629 free((char *)pe->path);
4630 got_pathlist_free(&paths);
4638 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4639 "path ...\n", getprogname());
4643 static const struct got_error *
4644 revert_progress(void *arg, unsigned char status, const char *path)
4646 if (status == GOT_STATUS_UNVERSIONED)
4649 while (path[0] == '/')
4651 printf("%c %s\n", status, path);
4655 struct choose_patch_arg {
4656 FILE *patch_script_file;
4660 static const struct got_error *
4661 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4662 int nchanges, const char *action)
4665 size_t linesize = 0;
4669 case GOT_STATUS_ADD:
4670 printf("A %s\n%s this addition? [y/n] ", path, action);
4672 case GOT_STATUS_DELETE:
4673 printf("D %s\n%s this deletion? [y/n] ", path, action);
4675 case GOT_STATUS_MODIFY:
4676 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4677 return got_error_from_errno("fseek");
4678 printf(GOT_COMMIT_SEP_STR);
4679 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4681 if (ferror(patch_file))
4682 return got_error_from_errno("getline");
4683 printf(GOT_COMMIT_SEP_STR);
4684 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4685 path, n, nchanges, action);
4688 return got_error_path(path, GOT_ERR_FILE_STATUS);
4694 static const struct got_error *
4695 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4696 FILE *patch_file, int n, int nchanges)
4698 const struct got_error *err = NULL;
4700 size_t linesize = 0;
4703 struct choose_patch_arg *a = arg;
4705 *choice = GOT_PATCH_CHOICE_NONE;
4707 if (a->patch_script_file) {
4709 err = show_change(status, path, patch_file, n, nchanges,
4713 linelen = getline(&line, &linesize, a->patch_script_file);
4714 if (linelen == -1) {
4715 if (ferror(a->patch_script_file))
4716 return got_error_from_errno("getline");
4719 nl = strchr(line, '\n');
4722 if (strcmp(line, "y") == 0) {
4723 *choice = GOT_PATCH_CHOICE_YES;
4725 } else if (strcmp(line, "n") == 0) {
4726 *choice = GOT_PATCH_CHOICE_NO;
4728 } else if (strcmp(line, "q") == 0 &&
4729 status == GOT_STATUS_MODIFY) {
4730 *choice = GOT_PATCH_CHOICE_QUIT;
4733 printf("invalid response '%s'\n", line);
4738 while (resp != 'y' && resp != 'n' && resp != 'q') {
4739 err = show_change(status, path, patch_file, n, nchanges,
4746 if (status == GOT_STATUS_MODIFY) {
4747 if (resp != 'y' && resp != 'n' && resp != 'q') {
4748 printf("invalid response '%c'\n", resp);
4751 } else if (resp != 'y' && resp != 'n') {
4752 printf("invalid response '%c'\n", resp);
4758 *choice = GOT_PATCH_CHOICE_YES;
4759 else if (resp == 'n')
4760 *choice = GOT_PATCH_CHOICE_NO;
4761 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4762 *choice = GOT_PATCH_CHOICE_QUIT;
4768 static const struct got_error *
4769 cmd_revert(int argc, char *argv[])
4771 const struct got_error *error = NULL;
4772 struct got_worktree *worktree = NULL;
4773 struct got_repository *repo = NULL;
4774 char *cwd = NULL, *path = NULL;
4775 struct got_pathlist_head paths;
4776 struct got_pathlist_entry *pe;
4777 int ch, can_recurse = 0, pflag = 0;
4778 FILE *patch_script_file = NULL;
4779 const char *patch_script_path = NULL;
4780 struct choose_patch_arg cpa;
4784 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4790 patch_script_path = optarg;
4805 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 "unveil", NULL) == -1)
4811 if (patch_script_path && !pflag)
4812 errx(1, "-F option can only be used together with -p option");
4814 cwd = getcwd(NULL, 0);
4816 error = got_error_from_errno("getcwd");
4819 error = got_worktree_open(&worktree, cwd);
4823 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4828 if (patch_script_path) {
4829 patch_script_file = fopen(patch_script_path, "r");
4830 if (patch_script_file == NULL) {
4831 error = got_error_from_errno2("fopen",
4836 error = apply_unveil(got_repo_get_path(repo), 1,
4837 got_worktree_get_root_path(worktree));
4841 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4848 TAILQ_FOREACH(pe, &paths, entry) {
4849 if (asprintf(&ondisk_path, "%s/%s",
4850 got_worktree_get_root_path(worktree),
4852 error = got_error_from_errno("asprintf");
4855 if (lstat(ondisk_path, &sb) == -1) {
4856 if (errno == ENOENT) {
4860 error = got_error_from_errno2("lstat",
4866 if (S_ISDIR(sb.st_mode)) {
4867 error = got_error_msg(GOT_ERR_BAD_PATH,
4868 "reverting directories requires -R option");
4874 cpa.patch_script_file = patch_script_file;
4875 cpa.action = "revert";
4876 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4877 pflag ? choose_patch : NULL, &cpa, repo);
4879 if (patch_script_file && fclose(patch_script_file) == EOF &&
4881 error = got_error_from_errno2("fclose", patch_script_path);
4883 got_repo_close(repo);
4885 got_worktree_close(worktree);
4894 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4899 struct collect_commit_logmsg_arg {
4900 const char *cmdline_log;
4902 const char *worktree_path;
4903 const char *branch_name;
4904 const char *repo_path;
4909 static const struct got_error *
4910 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4913 char *initial_content = NULL;
4914 struct got_pathlist_entry *pe;
4915 const struct got_error *err = NULL;
4916 char *template = NULL;
4917 struct collect_commit_logmsg_arg *a = arg;
4921 /* if a message was specified on the command line, just use it */
4922 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4923 len = strlen(a->cmdline_log) + 1;
4924 *logmsg = malloc(len + 1);
4925 if (*logmsg == NULL)
4926 return got_error_from_errno("malloc");
4927 strlcpy(*logmsg, a->cmdline_log, len);
4931 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4932 return got_error_from_errno("asprintf");
4934 if (asprintf(&initial_content,
4935 "\n# changes to be committed on branch %s:\n",
4936 a->branch_name) == -1)
4937 return got_error_from_errno("asprintf");
4939 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4943 dprintf(fd, initial_content);
4945 TAILQ_FOREACH(pe, commitable_paths, entry) {
4946 struct got_commitable *ct = pe->data;
4947 dprintf(fd, "# %c %s\n",
4948 got_commitable_get_status(ct),
4949 got_commitable_get_path(ct));
4953 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4955 free(initial_content);
4958 /* Editor is done; we can now apply unveil(2) */
4960 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4969 static const struct got_error *
4970 cmd_commit(int argc, char *argv[])
4972 const struct got_error *error = NULL;
4973 struct got_worktree *worktree = NULL;
4974 struct got_repository *repo = NULL;
4975 char *cwd = NULL, *id_str = NULL;
4976 struct got_object_id *id = NULL;
4977 const char *logmsg = NULL;
4978 struct collect_commit_logmsg_arg cl_arg;
4979 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4980 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4981 struct got_pathlist_head paths;
4984 cl_arg.logmsg_path = NULL;
4986 while ((ch = getopt(argc, argv, "m:")) != -1) {
5001 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5002 "unveil", NULL) == -1)
5005 cwd = getcwd(NULL, 0);
5007 error = got_error_from_errno("getcwd");
5010 error = got_worktree_open(&worktree, cwd);
5014 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5017 if (rebase_in_progress) {
5018 error = got_error(GOT_ERR_REBASING);
5022 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5027 error = get_gitconfig_path(&gitconfig_path);
5030 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5035 error = get_author(&author, repo);
5040 * unveil(2) traverses exec(2); if an editor is used we have
5041 * to apply unveil after the log message has been written.
5043 if (logmsg == NULL || strlen(logmsg) == 0)
5044 error = get_editor(&editor);
5046 error = apply_unveil(got_repo_get_path(repo), 0,
5047 got_worktree_get_root_path(worktree));
5051 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5055 cl_arg.editor = editor;
5056 cl_arg.cmdline_log = logmsg;
5057 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5058 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5059 if (!histedit_in_progress) {
5060 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5061 error = got_error(GOT_ERR_COMMIT_BRANCH);
5064 cl_arg.branch_name += 11;
5066 cl_arg.repo_path = got_repo_get_path(repo);
5067 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5068 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5070 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5071 cl_arg.logmsg_path != NULL)
5072 preserve_logmsg = 1;
5076 error = got_object_id_str(&id_str, id);
5079 printf("Created commit %s\n", id_str);
5081 if (preserve_logmsg) {
5082 fprintf(stderr, "%s: log message preserved in %s\n",
5083 getprogname(), cl_arg.logmsg_path);
5084 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5086 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5087 free(cl_arg.logmsg_path);
5089 got_repo_close(repo);
5091 got_worktree_close(worktree);
5094 free(gitconfig_path);
5101 usage_cherrypick(void)
5103 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5107 static const struct got_error *
5108 cmd_cherrypick(int argc, char *argv[])
5110 const struct got_error *error = NULL;
5111 struct got_worktree *worktree = NULL;
5112 struct got_repository *repo = NULL;
5113 char *cwd = NULL, *commit_id_str = NULL;
5114 struct got_object_id *commit_id = NULL;
5115 struct got_commit_object *commit = NULL;
5116 struct got_object_qid *pid;
5117 struct got_reference *head_ref = NULL;
5118 int ch, did_something = 0;
5120 while ((ch = getopt(argc, argv, "")) != -1) {
5132 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5133 "unveil", NULL) == -1)
5139 cwd = getcwd(NULL, 0);
5141 error = got_error_from_errno("getcwd");
5144 error = got_worktree_open(&worktree, cwd);
5148 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5153 error = apply_unveil(got_repo_get_path(repo), 0,
5154 got_worktree_get_root_path(worktree));
5158 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5159 GOT_OBJ_TYPE_COMMIT, repo);
5160 if (error != NULL) {
5161 struct got_reference *ref;
5162 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5164 error = got_ref_open(&ref, repo, argv[0], 0);
5167 error = got_ref_resolve(&commit_id, repo, ref);
5172 error = got_object_id_str(&commit_id_str, commit_id);
5176 error = got_ref_open(&head_ref, repo,
5177 got_worktree_get_head_ref_name(worktree), 0);
5181 error = check_same_branch(commit_id, head_ref, NULL, repo);
5183 if (error->code != GOT_ERR_ANCESTRY)
5187 error = got_error(GOT_ERR_SAME_BRANCH);
5191 error = got_object_open_as_commit(&commit, repo, commit_id);
5194 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5195 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5196 commit_id, repo, update_progress, &did_something, check_cancelled,
5202 printf("Merged commit %s\n", commit_id_str);
5205 got_object_commit_close(commit);
5206 free(commit_id_str);
5208 got_ref_close(head_ref);
5210 got_worktree_close(worktree);
5212 got_repo_close(repo);
5219 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5223 static const struct got_error *
5224 cmd_backout(int argc, char *argv[])
5226 const struct got_error *error = NULL;
5227 struct got_worktree *worktree = NULL;
5228 struct got_repository *repo = NULL;
5229 char *cwd = NULL, *commit_id_str = NULL;
5230 struct got_object_id *commit_id = NULL;
5231 struct got_commit_object *commit = NULL;
5232 struct got_object_qid *pid;
5233 struct got_reference *head_ref = NULL;
5234 int ch, did_something = 0;
5236 while ((ch = getopt(argc, argv, "")) != -1) {
5248 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5249 "unveil", NULL) == -1)
5255 cwd = getcwd(NULL, 0);
5257 error = got_error_from_errno("getcwd");
5260 error = got_worktree_open(&worktree, cwd);
5264 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5269 error = apply_unveil(got_repo_get_path(repo), 0,
5270 got_worktree_get_root_path(worktree));
5274 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5275 GOT_OBJ_TYPE_COMMIT, repo);
5276 if (error != NULL) {
5277 struct got_reference *ref;
5278 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5280 error = got_ref_open(&ref, repo, argv[0], 0);
5283 error = got_ref_resolve(&commit_id, repo, ref);
5288 error = got_object_id_str(&commit_id_str, commit_id);
5292 error = got_ref_open(&head_ref, repo,
5293 got_worktree_get_head_ref_name(worktree), 0);
5297 error = check_same_branch(commit_id, head_ref, NULL, repo);
5301 error = got_object_open_as_commit(&commit, repo, commit_id);
5304 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5306 error = got_error(GOT_ERR_ROOT_COMMIT);
5310 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5311 update_progress, &did_something, check_cancelled, NULL);
5316 printf("Backed out commit %s\n", commit_id_str);
5319 got_object_commit_close(commit);
5320 free(commit_id_str);
5322 got_ref_close(head_ref);
5324 got_worktree_close(worktree);
5326 got_repo_close(repo);
5333 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5339 trim_logmsg(char *logmsg, int limit)
5344 len = strlen(logmsg);
5348 nl = strchr(logmsg, '\n');
5353 static const struct got_error *
5354 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5356 const struct got_error *err;
5357 char *logmsg0 = NULL;
5360 err = got_object_commit_get_logmsg(&logmsg0, commit);
5365 while (isspace((unsigned char)s[0]))
5368 *logmsg = strdup(s);
5369 if (*logmsg == NULL) {
5370 err = got_error_from_errno("strdup");
5374 trim_logmsg(*logmsg, limit);
5380 static const struct got_error *
5381 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5383 const struct got_error *err;
5384 struct got_commit_object *commit = NULL;
5385 char *id_str = NULL, *logmsg = NULL;
5387 err = got_object_open_as_commit(&commit, repo, id);
5391 err = got_object_id_str(&id_str, id);
5397 err = get_short_logmsg(&logmsg, 42, commit);
5401 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5404 got_object_commit_close(commit);
5409 static const struct got_error *
5410 show_rebase_progress(struct got_commit_object *commit,
5411 struct got_object_id *old_id, struct got_object_id *new_id)
5413 const struct got_error *err;
5414 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5416 err = got_object_id_str(&old_id_str, old_id);
5421 err = got_object_id_str(&new_id_str, new_id);
5426 old_id_str[12] = '\0';
5428 new_id_str[12] = '\0';
5430 err = get_short_logmsg(&logmsg, 42, commit);
5434 printf("%s -> %s: %s\n", old_id_str,
5435 new_id_str ? new_id_str : "no-op change", logmsg);
5443 static const struct got_error *
5444 rebase_progress(void *arg, unsigned char status, const char *path)
5446 unsigned char *rebase_status = arg;
5448 while (path[0] == '/')
5450 printf("%c %s\n", status, path);
5452 if (*rebase_status == GOT_STATUS_CONFLICT)
5454 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5455 *rebase_status = status;
5459 static const struct got_error *
5460 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5461 struct got_reference *branch, struct got_reference *new_base_branch,
5462 struct got_reference *tmp_branch, struct got_repository *repo)
5464 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5465 return got_worktree_rebase_complete(worktree, fileindex,
5466 new_base_branch, tmp_branch, branch, repo);
5469 static const struct got_error *
5470 rebase_commit(struct got_pathlist_head *merged_paths,
5471 struct got_worktree *worktree, struct got_fileindex *fileindex,
5472 struct got_reference *tmp_branch,
5473 struct got_object_id *commit_id, struct got_repository *repo)
5475 const struct got_error *error;
5476 struct got_commit_object *commit;
5477 struct got_object_id *new_commit_id;
5479 error = got_object_open_as_commit(&commit, repo, commit_id);
5483 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5484 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5486 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5488 error = show_rebase_progress(commit, commit_id, NULL);
5490 error = show_rebase_progress(commit, commit_id, new_commit_id);
5491 free(new_commit_id);
5494 got_object_commit_close(commit);
5498 struct check_path_prefix_arg {
5499 const char *path_prefix;
5504 static const struct got_error *
5505 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5506 struct got_blob_object *blob2, struct got_object_id *id1,
5507 struct got_object_id *id2, const char *path1, const char *path2,
5508 mode_t mode1, mode_t mode2, struct got_repository *repo)
5510 struct check_path_prefix_arg *a = arg;
5512 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5513 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5514 return got_error(a->errcode);
5519 static const struct got_error *
5520 check_path_prefix(struct got_object_id *parent_id,
5521 struct got_object_id *commit_id, const char *path_prefix,
5522 int errcode, struct got_repository *repo)
5524 const struct got_error *err;
5525 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5526 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5527 struct check_path_prefix_arg cpp_arg;
5529 if (got_path_is_root_dir(path_prefix))
5532 err = got_object_open_as_commit(&commit, repo, commit_id);
5536 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5540 err = got_object_open_as_tree(&tree1, repo,
5541 got_object_commit_get_tree_id(parent_commit));
5545 err = got_object_open_as_tree(&tree2, repo,
5546 got_object_commit_get_tree_id(commit));
5550 cpp_arg.path_prefix = path_prefix;
5551 while (cpp_arg.path_prefix[0] == '/')
5552 cpp_arg.path_prefix++;
5553 cpp_arg.len = strlen(cpp_arg.path_prefix);
5554 cpp_arg.errcode = errcode;
5555 err = got_diff_tree(tree1, tree2, "", "", repo,
5556 check_path_prefix_in_diff, &cpp_arg, 0);
5559 got_object_tree_close(tree1);
5561 got_object_tree_close(tree2);
5563 got_object_commit_close(commit);
5565 got_object_commit_close(parent_commit);
5569 static const struct got_error *
5570 collect_commits(struct got_object_id_queue *commits,
5571 struct got_object_id *initial_commit_id,
5572 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5573 const char *path_prefix, int path_prefix_errcode,
5574 struct got_repository *repo)
5576 const struct got_error *err = NULL;
5577 struct got_commit_graph *graph = NULL;
5578 struct got_object_id *parent_id = NULL;
5579 struct got_object_qid *qid;
5580 struct got_object_id *commit_id = initial_commit_id;
5582 err = got_commit_graph_open(&graph, "/", 1);
5586 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5587 check_cancelled, NULL);
5590 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5591 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5592 check_cancelled, NULL);
5594 if (err->code == GOT_ERR_ITER_COMPLETED) {
5595 err = got_error_msg(GOT_ERR_ANCESTRY,
5596 "ran out of commits to rebase before "
5597 "youngest common ancestor commit has "
5602 err = check_path_prefix(parent_id, commit_id,
5603 path_prefix, path_prefix_errcode, repo);
5607 err = got_object_qid_alloc(&qid, commit_id);
5610 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5611 commit_id = parent_id;
5615 got_commit_graph_close(graph);
5619 static const struct got_error *
5620 cmd_rebase(int argc, char *argv[])
5622 const struct got_error *error = NULL;
5623 struct got_worktree *worktree = NULL;
5624 struct got_repository *repo = NULL;
5625 struct got_fileindex *fileindex = NULL;
5627 struct got_reference *branch = NULL;
5628 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5629 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5630 struct got_object_id *resume_commit_id = NULL;
5631 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5632 struct got_commit_object *commit = NULL;
5633 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5634 int histedit_in_progress = 0;
5635 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5636 struct got_object_id_queue commits;
5637 struct got_pathlist_head merged_paths;
5638 const struct got_object_id_queue *parent_ids;
5639 struct got_object_qid *qid, *pid;
5641 SIMPLEQ_INIT(&commits);
5642 TAILQ_INIT(&merged_paths);
5644 while ((ch = getopt(argc, argv, "ac")) != -1) {
5650 continue_rebase = 1;
5662 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5663 "unveil", NULL) == -1)
5666 if (abort_rebase && continue_rebase)
5668 else if (abort_rebase || continue_rebase) {
5671 } else if (argc != 1)
5674 cwd = getcwd(NULL, 0);
5676 error = got_error_from_errno("getcwd");
5679 error = got_worktree_open(&worktree, cwd);
5683 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5688 error = apply_unveil(got_repo_get_path(repo), 0,
5689 got_worktree_get_root_path(worktree));
5693 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5697 if (histedit_in_progress) {
5698 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5702 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5708 if (!rebase_in_progress) {
5709 error = got_error(GOT_ERR_NOT_REBASING);
5712 error = got_worktree_rebase_continue(&resume_commit_id,
5713 &new_base_branch, &tmp_branch, &branch, &fileindex,
5717 printf("Switching work tree to %s\n",
5718 got_ref_get_symref_target(new_base_branch));
5719 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5720 new_base_branch, update_progress, &did_something);
5723 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5724 goto done; /* nothing else to do */
5727 if (continue_rebase) {
5728 if (!rebase_in_progress) {
5729 error = got_error(GOT_ERR_NOT_REBASING);
5732 error = got_worktree_rebase_continue(&resume_commit_id,
5733 &new_base_branch, &tmp_branch, &branch, &fileindex,
5738 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5739 resume_commit_id, repo);
5743 yca_id = got_object_id_dup(resume_commit_id);
5744 if (yca_id == NULL) {
5745 error = got_error_from_errno("got_object_id_dup");
5749 error = got_ref_open(&branch, repo, argv[0], 0);
5754 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5758 if (!continue_rebase) {
5759 struct got_object_id *base_commit_id;
5761 base_commit_id = got_worktree_get_base_commit_id(worktree);
5762 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5763 base_commit_id, branch_head_commit_id, repo,
5764 check_cancelled, NULL);
5767 if (yca_id == NULL) {
5768 error = got_error_msg(GOT_ERR_ANCESTRY,
5769 "specified branch shares no common ancestry "
5770 "with work tree's branch");
5774 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5776 if (error->code != GOT_ERR_ANCESTRY)
5780 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5781 "specified branch resolves to a commit which "
5782 "is already contained in work tree's branch");
5785 error = got_worktree_rebase_prepare(&new_base_branch,
5786 &tmp_branch, &fileindex, worktree, branch, repo);
5791 commit_id = branch_head_commit_id;
5792 error = got_object_open_as_commit(&commit, repo, commit_id);
5796 parent_ids = got_object_commit_get_parent_ids(commit);
5797 pid = SIMPLEQ_FIRST(parent_ids);
5799 if (!continue_rebase) {
5801 error = got_worktree_rebase_abort(worktree, fileindex,
5802 repo, new_base_branch, update_progress,
5806 printf("Rebase of %s aborted\n",
5807 got_ref_get_name(branch));
5809 error = got_error(GOT_ERR_EMPTY_REBASE);
5812 error = collect_commits(&commits, commit_id, pid->id,
5813 yca_id, got_worktree_get_path_prefix(worktree),
5814 GOT_ERR_REBASE_PATH, repo);
5815 got_object_commit_close(commit);
5820 if (SIMPLEQ_EMPTY(&commits)) {
5821 if (continue_rebase) {
5822 error = rebase_complete(worktree, fileindex,
5823 branch, new_base_branch, tmp_branch, repo);
5826 /* Fast-forward the reference of the branch. */
5827 struct got_object_id *new_head_commit_id;
5829 error = got_ref_resolve(&new_head_commit_id, repo,
5833 error = got_object_id_str(&id_str, new_head_commit_id);
5834 printf("Forwarding %s to commit %s\n",
5835 got_ref_get_name(branch), id_str);
5837 error = got_ref_change_ref(branch,
5838 new_head_commit_id);
5845 SIMPLEQ_FOREACH(qid, &commits, entry) {
5846 commit_id = qid->id;
5847 parent_id = pid ? pid->id : yca_id;
5850 error = got_worktree_rebase_merge_files(&merged_paths,
5851 worktree, fileindex, parent_id, commit_id, repo,
5852 rebase_progress, &rebase_status, check_cancelled, NULL);
5856 if (rebase_status == GOT_STATUS_CONFLICT) {
5857 error = show_rebase_merge_conflict(qid->id, repo);
5860 got_worktree_rebase_pathlist_free(&merged_paths);
5864 error = rebase_commit(&merged_paths, worktree, fileindex,
5865 tmp_branch, commit_id, repo);
5866 got_worktree_rebase_pathlist_free(&merged_paths);
5871 if (rebase_status == GOT_STATUS_CONFLICT) {
5872 error = got_worktree_rebase_postpone(worktree, fileindex);
5875 error = got_error_msg(GOT_ERR_CONFLICTS,
5876 "conflicts must be resolved before rebasing can continue");
5878 error = rebase_complete(worktree, fileindex, branch,
5879 new_base_branch, tmp_branch, repo);
5881 got_object_id_queue_free(&commits);
5882 free(branch_head_commit_id);
5883 free(resume_commit_id);
5886 got_object_commit_close(commit);
5888 got_ref_close(branch);
5889 if (new_base_branch)
5890 got_ref_close(new_base_branch);
5892 got_ref_close(tmp_branch);
5894 got_worktree_close(worktree);
5896 got_repo_close(repo);
5901 usage_histedit(void)
5903 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5908 #define GOT_HISTEDIT_PICK 'p'
5909 #define GOT_HISTEDIT_EDIT 'e'
5910 #define GOT_HISTEDIT_FOLD 'f'
5911 #define GOT_HISTEDIT_DROP 'd'
5912 #define GOT_HISTEDIT_MESG 'm'
5914 static struct got_histedit_cmd {
5918 } got_histedit_cmds[] = {
5919 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5920 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5921 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5923 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5924 { GOT_HISTEDIT_MESG, "mesg",
5925 "single-line log message for commit above (open editor if empty)" },
5928 struct got_histedit_list_entry {
5929 TAILQ_ENTRY(got_histedit_list_entry) entry;
5930 struct got_object_id *commit_id;
5931 const struct got_histedit_cmd *cmd;
5934 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5936 static const struct got_error *
5937 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5938 FILE *f, struct got_repository *repo)
5940 const struct got_error *err = NULL;
5941 char *logmsg = NULL, *id_str = NULL;
5942 struct got_commit_object *commit = NULL;
5945 err = got_object_open_as_commit(&commit, repo, commit_id);
5949 err = get_short_logmsg(&logmsg, 34, commit);
5953 err = got_object_id_str(&id_str, commit_id);
5957 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5959 err = got_ferror(f, GOT_ERR_IO);
5962 got_object_commit_close(commit);
5968 static const struct got_error *
5969 histedit_write_commit_list(struct got_object_id_queue *commits,
5970 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5972 const struct got_error *err = NULL;
5973 struct got_object_qid *qid;
5975 if (SIMPLEQ_EMPTY(commits))
5976 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5978 SIMPLEQ_FOREACH(qid, commits, entry) {
5979 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5983 if (edit_logmsg_only) {
5984 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5986 err = got_ferror(f, GOT_ERR_IO);
5995 static const struct got_error *
5996 write_cmd_list(FILE *f, const char *branch_name,
5997 struct got_object_id_queue *commits)
5999 const struct got_error *err = NULL;
6002 struct got_object_qid *qid;
6004 qid = SIMPLEQ_FIRST(commits);
6005 err = got_object_id_str(&id_str, qid->id);
6010 "# Editing the history of branch '%s' starting at\n"
6012 "# Commits will be processed in order from top to "
6013 "bottom of this file.\n", branch_name, id_str);
6015 err = got_ferror(f, GOT_ERR_IO);
6019 n = fprintf(f, "# Available histedit commands:\n");
6021 err = got_ferror(f, GOT_ERR_IO);
6025 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6026 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6027 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6030 err = got_ferror(f, GOT_ERR_IO);
6039 static const struct got_error *
6040 histedit_syntax_error(int lineno)
6042 static char msg[42];
6045 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6047 if (ret == -1 || ret >= sizeof(msg))
6048 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6050 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6053 static const struct got_error *
6054 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6055 char *logmsg, struct got_repository *repo)
6057 const struct got_error *err;
6058 struct got_commit_object *folded_commit = NULL;
6059 char *id_str, *folded_logmsg = NULL;
6061 err = got_object_id_str(&id_str, hle->commit_id);
6065 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6069 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6072 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6073 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6074 folded_logmsg) == -1) {
6075 err = got_error_from_errno("asprintf");
6079 got_object_commit_close(folded_commit);
6081 free(folded_logmsg);
6085 static struct got_histedit_list_entry *
6086 get_folded_commits(struct got_histedit_list_entry *hle)
6088 struct got_histedit_list_entry *prev, *folded = NULL;
6090 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6091 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6092 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6093 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6095 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6101 static const struct got_error *
6102 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6103 struct got_repository *repo)
6105 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6106 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6107 const struct got_error *err = NULL;
6108 struct got_commit_object *commit = NULL;
6110 struct got_histedit_list_entry *folded = NULL;
6112 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6116 folded = get_folded_commits(hle);
6118 while (folded != hle) {
6119 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6120 folded = TAILQ_NEXT(folded, entry);
6123 err = append_folded_commit_msg(&new_msg, folded,
6129 folded = TAILQ_NEXT(folded, entry);
6133 err = got_object_id_str(&id_str, hle->commit_id);
6136 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6139 if (asprintf(&new_msg,
6140 "%s\n# original log message of commit %s: %s",
6141 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6142 err = got_error_from_errno("asprintf");
6148 err = got_object_id_str(&id_str, hle->commit_id);
6152 err = got_opentemp_named_fd(&logmsg_path, &fd,
6153 GOT_TMPDIR_STR "/got-logmsg");
6157 dprintf(fd, logmsg);
6160 err = get_editor(&editor);
6164 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6166 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6168 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6171 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6172 err = got_error_from_errno2("unlink", logmsg_path);
6178 got_object_commit_close(commit);
6182 static const struct got_error *
6183 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6184 FILE *f, struct got_repository *repo)
6186 const struct got_error *err = NULL;
6187 char *line = NULL, *p, *end;
6191 const struct got_histedit_cmd *cmd;
6192 struct got_object_id *commit_id = NULL;
6193 struct got_histedit_list_entry *hle = NULL;
6196 len = getline(&line, &size, f);
6198 const struct got_error *getline_err;
6201 getline_err = got_error_from_errno("getline");
6202 err = got_ferror(f, getline_err->code);
6207 while (isspace((unsigned char)p[0]))
6209 if (p[0] == '#' || p[0] == '\0') {
6215 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6216 cmd = &got_histedit_cmds[i];
6217 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6218 isspace((unsigned char)p[strlen(cmd->name)])) {
6219 p += strlen(cmd->name);
6222 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6227 if (i == nitems(got_histedit_cmds)) {
6228 err = histedit_syntax_error(lineno);
6231 while (isspace((unsigned char)p[0]))
6233 if (cmd->code == GOT_HISTEDIT_MESG) {
6234 if (hle == NULL || hle->logmsg != NULL) {
6235 err = got_error(GOT_ERR_HISTEDIT_CMD);
6239 err = histedit_edit_logmsg(hle, repo);
6243 hle->logmsg = strdup(p);
6244 if (hle->logmsg == NULL) {
6245 err = got_error_from_errno("strdup");
6254 while (end[0] && !isspace((unsigned char)end[0]))
6258 err = got_object_resolve_id_str(&commit_id, repo, p);
6260 /* override error code */
6261 err = histedit_syntax_error(lineno);
6265 hle = malloc(sizeof(*hle));
6267 err = got_error_from_errno("malloc");
6271 hle->commit_id = commit_id;
6276 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6284 static const struct got_error *
6285 histedit_check_script(struct got_histedit_list *histedit_cmds,
6286 struct got_object_id_queue *commits, struct got_repository *repo)
6288 const struct got_error *err = NULL;
6289 struct got_object_qid *qid;
6290 struct got_histedit_list_entry *hle;
6291 static char msg[92];
6294 if (TAILQ_EMPTY(histedit_cmds))
6295 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6296 "histedit script contains no commands");
6297 if (SIMPLEQ_EMPTY(commits))
6298 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6300 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6301 struct got_histedit_list_entry *hle2;
6302 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6305 if (got_object_id_cmp(hle->commit_id,
6306 hle2->commit_id) != 0)
6308 err = got_object_id_str(&id_str, hle->commit_id);
6311 snprintf(msg, sizeof(msg), "commit %s is listed "
6312 "more than once in histedit script", id_str);
6314 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6318 SIMPLEQ_FOREACH(qid, commits, entry) {
6319 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6320 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6324 err = got_object_id_str(&id_str, qid->id);
6327 snprintf(msg, sizeof(msg),
6328 "commit %s missing from histedit script", id_str);
6330 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6334 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6335 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6336 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6337 "last commit in histedit script cannot be folded");
6342 static const struct got_error *
6343 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6344 const char *path, struct got_object_id_queue *commits,
6345 struct got_repository *repo)
6347 const struct got_error *err = NULL;
6351 err = get_editor(&editor);
6355 if (spawn_editor(editor, path) == -1) {
6356 err = got_error_from_errno("failed spawning editor");
6360 f = fopen(path, "r");
6362 err = got_error_from_errno("fopen");
6365 err = histedit_parse_list(histedit_cmds, f, repo);
6369 err = histedit_check_script(histedit_cmds, commits, repo);
6371 if (f && fclose(f) != 0 && err == NULL)
6372 err = got_error_from_errno("fclose");
6377 static const struct got_error *
6378 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6379 struct got_object_id_queue *, const char *, const char *,
6380 struct got_repository *);
6382 static const struct got_error *
6383 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6384 struct got_object_id_queue *commits, const char *branch_name,
6385 int edit_logmsg_only, struct got_repository *repo)
6387 const struct got_error *err;
6391 err = got_opentemp_named(&path, &f, "got-histedit");
6395 err = write_cmd_list(f, branch_name, commits);
6399 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6403 if (edit_logmsg_only) {
6405 err = histedit_parse_list(histedit_cmds, f, repo);
6407 if (fclose(f) != 0) {
6408 err = got_error_from_errno("fclose");
6412 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6414 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6415 err->code != GOT_ERR_HISTEDIT_CMD)
6417 err = histedit_edit_list_retry(histedit_cmds, err,
6418 commits, path, branch_name, repo);
6422 if (f && fclose(f) != 0 && err == NULL)
6423 err = got_error_from_errno("fclose");
6424 if (path && unlink(path) != 0 && err == NULL)
6425 err = got_error_from_errno2("unlink", path);
6430 static const struct got_error *
6431 histedit_save_list(struct got_histedit_list *histedit_cmds,
6432 struct got_worktree *worktree, struct got_repository *repo)
6434 const struct got_error *err = NULL;
6437 struct got_histedit_list_entry *hle;
6438 struct got_commit_object *commit = NULL;
6440 err = got_worktree_get_histedit_script_path(&path, worktree);
6444 f = fopen(path, "w");
6446 err = got_error_from_errno2("fopen", path);
6449 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6450 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6456 int n = fprintf(f, "%c %s\n",
6457 GOT_HISTEDIT_MESG, hle->logmsg);
6459 err = got_ferror(f, GOT_ERR_IO);
6465 if (f && fclose(f) != 0 && err == NULL)
6466 err = got_error_from_errno("fclose");
6469 got_object_commit_close(commit);
6474 histedit_free_list(struct got_histedit_list *histedit_cmds)
6476 struct got_histedit_list_entry *hle;
6478 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6479 TAILQ_REMOVE(histedit_cmds, hle, entry);
6484 static const struct got_error *
6485 histedit_load_list(struct got_histedit_list *histedit_cmds,
6486 const char *path, struct got_repository *repo)
6488 const struct got_error *err = NULL;
6491 f = fopen(path, "r");
6493 err = got_error_from_errno2("fopen", path);
6497 err = histedit_parse_list(histedit_cmds, f, repo);
6499 if (f && fclose(f) != 0 && err == NULL)
6500 err = got_error_from_errno("fclose");
6504 static const struct got_error *
6505 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6506 const struct got_error *edit_err, struct got_object_id_queue *commits,
6507 const char *path, const char *branch_name, struct got_repository *repo)
6509 const struct got_error *err = NULL, *prev_err = edit_err;
6512 while (resp != 'c' && resp != 'r' && resp != 'a') {
6513 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6514 "or (a)bort: ", getprogname(), prev_err->msg);
6519 histedit_free_list(histedit_cmds);
6520 err = histedit_run_editor(histedit_cmds, path, commits,
6523 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6524 err->code != GOT_ERR_HISTEDIT_CMD)
6531 } else if (resp == 'r') {
6532 histedit_free_list(histedit_cmds);
6533 err = histedit_edit_script(histedit_cmds,
6534 commits, branch_name, 0, repo);
6536 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6537 err->code != GOT_ERR_HISTEDIT_CMD)
6544 } else if (resp == 'a') {
6545 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6548 printf("invalid response '%c'\n", resp);
6554 static const struct got_error *
6555 histedit_complete(struct got_worktree *worktree,
6556 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6557 struct got_reference *branch, struct got_repository *repo)
6559 printf("Switching work tree to %s\n",
6560 got_ref_get_symref_target(branch));
6561 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6565 static const struct got_error *
6566 show_histedit_progress(struct got_commit_object *commit,
6567 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6569 const struct got_error *err;
6570 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6572 err = got_object_id_str(&old_id_str, hle->commit_id);
6577 err = got_object_id_str(&new_id_str, new_id);
6582 old_id_str[12] = '\0';
6584 new_id_str[12] = '\0';
6587 logmsg = strdup(hle->logmsg);
6588 if (logmsg == NULL) {
6589 err = got_error_from_errno("strdup");
6592 trim_logmsg(logmsg, 42);
6594 err = get_short_logmsg(&logmsg, 42, commit);
6599 switch (hle->cmd->code) {
6600 case GOT_HISTEDIT_PICK:
6601 case GOT_HISTEDIT_EDIT:
6602 printf("%s -> %s: %s\n", old_id_str,
6603 new_id_str ? new_id_str : "no-op change", logmsg);
6605 case GOT_HISTEDIT_DROP:
6606 case GOT_HISTEDIT_FOLD:
6607 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6619 static const struct got_error *
6620 histedit_commit(struct got_pathlist_head *merged_paths,
6621 struct got_worktree *worktree, struct got_fileindex *fileindex,
6622 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6623 struct got_repository *repo)
6625 const struct got_error *err;
6626 struct got_commit_object *commit;
6627 struct got_object_id *new_commit_id;
6629 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6630 && hle->logmsg == NULL) {
6631 err = histedit_edit_logmsg(hle, repo);
6636 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6640 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6641 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6644 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6646 err = show_histedit_progress(commit, hle, NULL);
6648 err = show_histedit_progress(commit, hle, new_commit_id);
6649 free(new_commit_id);
6652 got_object_commit_close(commit);
6656 static const struct got_error *
6657 histedit_skip_commit(struct got_histedit_list_entry *hle,
6658 struct got_worktree *worktree, struct got_repository *repo)
6660 const struct got_error *error;
6661 struct got_commit_object *commit;
6663 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6668 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6672 error = show_histedit_progress(commit, hle, NULL);
6673 got_object_commit_close(commit);
6677 static const struct got_error *
6678 check_local_changes(void *arg, unsigned char status,
6679 unsigned char staged_status, const char *path,
6680 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6681 struct got_object_id *commit_id, int dirfd, const char *de_name)
6683 int *have_local_changes = arg;
6686 case GOT_STATUS_ADD:
6687 case GOT_STATUS_DELETE:
6688 case GOT_STATUS_MODIFY:
6689 case GOT_STATUS_CONFLICT:
6690 *have_local_changes = 1;
6691 return got_error(GOT_ERR_CANCELLED);
6696 switch (staged_status) {
6697 case GOT_STATUS_ADD:
6698 case GOT_STATUS_DELETE:
6699 case GOT_STATUS_MODIFY:
6700 *have_local_changes = 1;
6701 return got_error(GOT_ERR_CANCELLED);
6709 static const struct got_error *
6710 cmd_histedit(int argc, char *argv[])
6712 const struct got_error *error = NULL;
6713 struct got_worktree *worktree = NULL;
6714 struct got_fileindex *fileindex = NULL;
6715 struct got_repository *repo = NULL;
6717 struct got_reference *branch = NULL;
6718 struct got_reference *tmp_branch = NULL;
6719 struct got_object_id *resume_commit_id = NULL;
6720 struct got_object_id *base_commit_id = NULL;
6721 struct got_object_id *head_commit_id = NULL;
6722 struct got_commit_object *commit = NULL;
6723 int ch, rebase_in_progress = 0, did_something;
6724 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6725 int edit_logmsg_only = 0;
6726 const char *edit_script_path = NULL;
6727 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6728 struct got_object_id_queue commits;
6729 struct got_pathlist_head merged_paths;
6730 const struct got_object_id_queue *parent_ids;
6731 struct got_object_qid *pid;
6732 struct got_histedit_list histedit_cmds;
6733 struct got_histedit_list_entry *hle;
6735 SIMPLEQ_INIT(&commits);
6736 TAILQ_INIT(&histedit_cmds);
6737 TAILQ_INIT(&merged_paths);
6739 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6748 edit_script_path = optarg;
6751 edit_logmsg_only = 1;
6763 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6764 "unveil", NULL) == -1)
6767 if (abort_edit && continue_edit)
6768 errx(1, "histedit's -a and -c options are mutually exclusive");
6769 if (edit_script_path && edit_logmsg_only)
6770 errx(1, "histedit's -F and -m options are mutually exclusive");
6771 if (abort_edit && edit_logmsg_only)
6772 errx(1, "histedit's -a and -m options are mutually exclusive");
6773 if (continue_edit && edit_logmsg_only)
6774 errx(1, "histedit's -c and -m options are mutually exclusive");
6779 * This command cannot apply unveil(2) in all cases because the
6780 * user may choose to run an editor to edit the histedit script
6781 * and to edit individual commit log messages.
6782 * unveil(2) traverses exec(2); if an editor is used we have to
6783 * apply unveil after edit script and log messages have been written.
6784 * XXX TODO: Make use of unveil(2) where possible.
6787 cwd = getcwd(NULL, 0);
6789 error = got_error_from_errno("getcwd");
6792 error = got_worktree_open(&worktree, cwd);
6796 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6801 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6804 if (rebase_in_progress) {
6805 error = got_error(GOT_ERR_REBASING);
6809 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6813 if (edit_in_progress && edit_logmsg_only) {
6814 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6815 "histedit operation is in progress in this "
6816 "work tree and must be continued or aborted "
6817 "before the -m option can be used");
6821 if (edit_in_progress && abort_edit) {
6822 error = got_worktree_histedit_continue(&resume_commit_id,
6823 &tmp_branch, &branch, &base_commit_id, &fileindex,
6827 printf("Switching work tree to %s\n",
6828 got_ref_get_symref_target(branch));
6829 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6830 branch, base_commit_id, update_progress, &did_something);
6833 printf("Histedit of %s aborted\n",
6834 got_ref_get_symref_target(branch));
6835 goto done; /* nothing else to do */
6836 } else if (abort_edit) {
6837 error = got_error(GOT_ERR_NOT_HISTEDIT);
6841 if (continue_edit) {
6844 if (!edit_in_progress) {
6845 error = got_error(GOT_ERR_NOT_HISTEDIT);
6849 error = got_worktree_get_histedit_script_path(&path, worktree);
6853 error = histedit_load_list(&histedit_cmds, path, repo);
6858 error = got_worktree_histedit_continue(&resume_commit_id,
6859 &tmp_branch, &branch, &base_commit_id, &fileindex,
6864 error = got_ref_resolve(&head_commit_id, repo, branch);
6868 error = got_object_open_as_commit(&commit, repo,
6872 parent_ids = got_object_commit_get_parent_ids(commit);
6873 pid = SIMPLEQ_FIRST(parent_ids);
6875 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6878 error = collect_commits(&commits, head_commit_id, pid->id,
6879 base_commit_id, got_worktree_get_path_prefix(worktree),
6880 GOT_ERR_HISTEDIT_PATH, repo);
6881 got_object_commit_close(commit);
6886 if (edit_in_progress) {
6887 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6891 error = got_ref_open(&branch, repo,
6892 got_worktree_get_head_ref_name(worktree), 0);
6896 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6897 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6898 "will not edit commit history of a branch outside "
6899 "the \"refs/heads/\" reference namespace");
6903 error = got_ref_resolve(&head_commit_id, repo, branch);
6904 got_ref_close(branch);
6909 error = got_object_open_as_commit(&commit, repo,
6913 parent_ids = got_object_commit_get_parent_ids(commit);
6914 pid = SIMPLEQ_FIRST(parent_ids);
6916 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6919 error = collect_commits(&commits, head_commit_id, pid->id,
6920 got_worktree_get_base_commit_id(worktree),
6921 got_worktree_get_path_prefix(worktree),
6922 GOT_ERR_HISTEDIT_PATH, repo);
6923 got_object_commit_close(commit);
6928 if (SIMPLEQ_EMPTY(&commits)) {
6929 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6933 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6934 &base_commit_id, &fileindex, worktree, repo);
6938 if (edit_script_path) {
6939 error = histedit_load_list(&histedit_cmds,
6940 edit_script_path, repo);
6942 got_worktree_histedit_abort(worktree, fileindex,
6943 repo, branch, base_commit_id,
6944 update_progress, &did_something);
6948 const char *branch_name;
6949 branch_name = got_ref_get_symref_target(branch);
6950 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6952 error = histedit_edit_script(&histedit_cmds, &commits,
6953 branch_name, edit_logmsg_only, repo);
6955 got_worktree_histedit_abort(worktree, fileindex,
6956 repo, branch, base_commit_id,
6957 update_progress, &did_something);
6963 error = histedit_save_list(&histedit_cmds, worktree,
6966 got_worktree_histedit_abort(worktree, fileindex,
6967 repo, branch, base_commit_id,
6968 update_progress, &did_something);
6974 error = histedit_check_script(&histedit_cmds, &commits, repo);
6978 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6979 if (resume_commit_id) {
6980 if (got_object_id_cmp(hle->commit_id,
6981 resume_commit_id) != 0)
6984 resume_commit_id = NULL;
6985 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6986 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6987 error = histedit_skip_commit(hle, worktree,
6992 struct got_pathlist_head paths;
6993 int have_changes = 0;
6996 error = got_pathlist_append(&paths, "", NULL);
6999 error = got_worktree_status(worktree, &paths,
7000 repo, check_local_changes, &have_changes,
7001 check_cancelled, NULL);
7002 got_pathlist_free(&paths);
7004 if (error->code != GOT_ERR_CANCELLED)
7006 if (sigint_received || sigpipe_received)
7010 error = histedit_commit(NULL, worktree,
7011 fileindex, tmp_branch, hle, repo);
7015 error = got_object_open_as_commit(
7016 &commit, repo, hle->commit_id);
7019 error = show_histedit_progress(commit,
7021 got_object_commit_close(commit);
7030 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7031 error = histedit_skip_commit(hle, worktree, repo);
7037 error = got_object_open_as_commit(&commit, repo,
7041 parent_ids = got_object_commit_get_parent_ids(commit);
7042 pid = SIMPLEQ_FIRST(parent_ids);
7044 error = got_worktree_histedit_merge_files(&merged_paths,
7045 worktree, fileindex, pid->id, hle->commit_id, repo,
7046 rebase_progress, &rebase_status, check_cancelled, NULL);
7049 got_object_commit_close(commit);
7052 if (rebase_status == GOT_STATUS_CONFLICT) {
7053 error = show_rebase_merge_conflict(hle->commit_id,
7057 got_worktree_rebase_pathlist_free(&merged_paths);
7061 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7063 error = got_object_id_str(&id_str, hle->commit_id);
7066 printf("Stopping histedit for amending commit %s\n",
7069 got_worktree_rebase_pathlist_free(&merged_paths);
7070 error = got_worktree_histedit_postpone(worktree,
7075 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7076 error = histedit_skip_commit(hle, worktree, repo);
7082 error = histedit_commit(&merged_paths, worktree, fileindex,
7083 tmp_branch, hle, repo);
7084 got_worktree_rebase_pathlist_free(&merged_paths);
7089 if (rebase_status == GOT_STATUS_CONFLICT) {
7090 error = got_worktree_histedit_postpone(worktree, fileindex);
7093 error = got_error_msg(GOT_ERR_CONFLICTS,
7094 "conflicts must be resolved before histedit can continue");
7096 error = histedit_complete(worktree, fileindex, tmp_branch,
7099 got_object_id_queue_free(&commits);
7100 histedit_free_list(&histedit_cmds);
7101 free(head_commit_id);
7102 free(base_commit_id);
7103 free(resume_commit_id);
7105 got_object_commit_close(commit);
7107 got_ref_close(branch);
7109 got_ref_close(tmp_branch);
7111 got_worktree_close(worktree);
7113 got_repo_close(repo);
7118 usage_integrate(void)
7120 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7124 static const struct got_error *
7125 cmd_integrate(int argc, char *argv[])
7127 const struct got_error *error = NULL;
7128 struct got_repository *repo = NULL;
7129 struct got_worktree *worktree = NULL;
7130 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7131 const char *branch_arg = NULL;
7132 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7133 struct got_fileindex *fileindex = NULL;
7134 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7135 int ch, did_something = 0;
7137 while ((ch = getopt(argc, argv, "")) != -1) {
7150 branch_arg = argv[0];
7152 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7153 "unveil", NULL) == -1)
7156 cwd = getcwd(NULL, 0);
7158 error = got_error_from_errno("getcwd");
7162 error = got_worktree_open(&worktree, cwd);
7166 error = check_rebase_or_histedit_in_progress(worktree);
7170 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7175 error = apply_unveil(got_repo_get_path(repo), 0,
7176 got_worktree_get_root_path(worktree));
7180 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7181 error = got_error_from_errno("asprintf");
7185 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7186 &base_branch_ref, worktree, refname, repo);
7190 refname = strdup(got_ref_get_name(branch_ref));
7191 if (refname == NULL) {
7192 error = got_error_from_errno("strdup");
7193 got_worktree_integrate_abort(worktree, fileindex, repo,
7194 branch_ref, base_branch_ref);
7197 base_refname = strdup(got_ref_get_name(base_branch_ref));
7198 if (base_refname == NULL) {
7199 error = got_error_from_errno("strdup");
7200 got_worktree_integrate_abort(worktree, fileindex, repo,
7201 branch_ref, base_branch_ref);
7205 error = got_ref_resolve(&commit_id, repo, branch_ref);
7209 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7213 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7214 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7215 "specified branch has already been integrated");
7216 got_worktree_integrate_abort(worktree, fileindex, repo,
7217 branch_ref, base_branch_ref);
7221 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7223 if (error->code == GOT_ERR_ANCESTRY)
7224 error = got_error(GOT_ERR_REBASE_REQUIRED);
7225 got_worktree_integrate_abort(worktree, fileindex, repo,
7226 branch_ref, base_branch_ref);
7230 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7231 branch_ref, base_branch_ref, update_progress, &did_something,
7232 check_cancelled, NULL);
7236 printf("Integrated %s into %s\n", refname, base_refname);
7239 got_repo_close(repo);
7241 got_worktree_close(worktree);
7243 free(base_commit_id);
7253 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7254 "[file-path ...]\n",
7259 static const struct got_error *
7260 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7261 const char *path, struct got_object_id *blob_id,
7262 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7263 int dirfd, const char *de_name)
7265 const struct got_error *err = NULL;
7266 char *id_str = NULL;
7268 if (staged_status != GOT_STATUS_ADD &&
7269 staged_status != GOT_STATUS_MODIFY &&
7270 staged_status != GOT_STATUS_DELETE)
7273 if (staged_status == GOT_STATUS_ADD ||
7274 staged_status == GOT_STATUS_MODIFY)
7275 err = got_object_id_str(&id_str, staged_blob_id);
7277 err = got_object_id_str(&id_str, blob_id);
7281 printf("%s %c %s\n", id_str, staged_status, path);
7286 static const struct got_error *
7287 cmd_stage(int argc, char *argv[])
7289 const struct got_error *error = NULL;
7290 struct got_repository *repo = NULL;
7291 struct got_worktree *worktree = NULL;
7293 struct got_pathlist_head paths;
7294 struct got_pathlist_entry *pe;
7295 int ch, list_stage = 0, pflag = 0;
7296 FILE *patch_script_file = NULL;
7297 const char *patch_script_path = NULL;
7298 struct choose_patch_arg cpa;
7302 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7311 patch_script_path = optarg;
7323 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7324 "unveil", NULL) == -1)
7327 if (list_stage && (pflag || patch_script_path))
7328 errx(1, "-l option cannot be used with other options");
7329 if (patch_script_path && !pflag)
7330 errx(1, "-F option can only be used together with -p option");
7332 cwd = getcwd(NULL, 0);
7334 error = got_error_from_errno("getcwd");
7338 error = got_worktree_open(&worktree, cwd);
7342 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7347 if (patch_script_path) {
7348 patch_script_file = fopen(patch_script_path, "r");
7349 if (patch_script_file == NULL) {
7350 error = got_error_from_errno2("fopen",
7355 error = apply_unveil(got_repo_get_path(repo), 0,
7356 got_worktree_get_root_path(worktree));
7360 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7365 error = got_worktree_status(worktree, &paths, repo,
7366 print_stage, NULL, check_cancelled, NULL);
7368 cpa.patch_script_file = patch_script_file;
7369 cpa.action = "stage";
7370 error = got_worktree_stage(worktree, &paths,
7371 pflag ? NULL : print_status, NULL,
7372 pflag ? choose_patch : NULL, &cpa, repo);
7375 if (patch_script_file && fclose(patch_script_file) == EOF &&
7377 error = got_error_from_errno2("fclose", patch_script_path);
7379 got_repo_close(repo);
7381 got_worktree_close(worktree);
7382 TAILQ_FOREACH(pe, &paths, entry)
7383 free((char *)pe->path);
7384 got_pathlist_free(&paths);
7392 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7393 "[file-path ...]\n",
7399 static const struct got_error *
7400 cmd_unstage(int argc, char *argv[])
7402 const struct got_error *error = NULL;
7403 struct got_repository *repo = NULL;
7404 struct got_worktree *worktree = NULL;
7406 struct got_pathlist_head paths;
7407 struct got_pathlist_entry *pe;
7408 int ch, did_something = 0, pflag = 0;
7409 FILE *patch_script_file = NULL;
7410 const char *patch_script_path = NULL;
7411 struct choose_patch_arg cpa;
7415 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7421 patch_script_path = optarg;
7433 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7434 "unveil", NULL) == -1)
7437 if (patch_script_path && !pflag)
7438 errx(1, "-F option can only be used together with -p option");
7440 cwd = getcwd(NULL, 0);
7442 error = got_error_from_errno("getcwd");
7446 error = got_worktree_open(&worktree, cwd);
7450 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7455 if (patch_script_path) {
7456 patch_script_file = fopen(patch_script_path, "r");
7457 if (patch_script_file == NULL) {
7458 error = got_error_from_errno2("fopen",
7464 error = apply_unveil(got_repo_get_path(repo), 0,
7465 got_worktree_get_root_path(worktree));
7469 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7473 cpa.patch_script_file = patch_script_file;
7474 cpa.action = "unstage";
7475 error = got_worktree_unstage(worktree, &paths, update_progress,
7476 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7478 if (patch_script_file && fclose(patch_script_file) == EOF &&
7480 error = got_error_from_errno2("fclose", patch_script_path);
7482 got_repo_close(repo);
7484 got_worktree_close(worktree);
7485 TAILQ_FOREACH(pe, &paths, entry)
7486 free((char *)pe->path);
7487 got_pathlist_free(&paths);
7495 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7496 "arg1 [arg2 ...]\n", getprogname());
7500 static const struct got_error *
7501 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7503 const struct got_error *err;
7504 struct got_blob_object *blob;
7506 err = got_object_open_as_blob(&blob, repo, id, 8192);
7510 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7511 got_object_blob_close(blob);
7515 static const struct got_error *
7516 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7518 const struct got_error *err;
7519 struct got_tree_object *tree;
7522 err = got_object_open_as_tree(&tree, repo, id);
7526 nentries = got_object_tree_get_nentries(tree);
7527 for (i = 0; i < nentries; i++) {
7528 struct got_tree_entry *te;
7530 if (sigint_received || sigpipe_received)
7532 te = got_object_tree_get_entry(tree, i);
7533 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7536 fprintf(outfile, "%s %.7o %s\n", id_str,
7537 got_tree_entry_get_mode(te),
7538 got_tree_entry_get_name(te));
7542 got_object_tree_close(tree);
7546 static const struct got_error *
7547 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7549 const struct got_error *err;
7550 struct got_commit_object *commit;
7551 const struct got_object_id_queue *parent_ids;
7552 struct got_object_qid *pid;
7553 char *id_str = NULL;
7554 const char *logmsg = NULL;
7556 err = got_object_open_as_commit(&commit, repo, id);
7560 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7564 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7565 parent_ids = got_object_commit_get_parent_ids(commit);
7566 fprintf(outfile, "numparents %d\n",
7567 got_object_commit_get_nparents(commit));
7568 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7570 err = got_object_id_str(&pid_str, pid->id);
7573 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7576 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7577 got_object_commit_get_author(commit),
7578 got_object_commit_get_author_time(commit));
7580 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7581 got_object_commit_get_author(commit),
7582 got_object_commit_get_committer_time(commit));
7584 logmsg = got_object_commit_get_logmsg_raw(commit);
7585 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7586 fprintf(outfile, "%s", logmsg);
7589 got_object_commit_close(commit);
7593 static const struct got_error *
7594 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7596 const struct got_error *err;
7597 struct got_tag_object *tag;
7598 char *id_str = NULL;
7599 const char *tagmsg = NULL;
7601 err = got_object_open_as_tag(&tag, repo, id);
7605 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7609 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7611 switch (got_object_tag_get_object_type(tag)) {
7612 case GOT_OBJ_TYPE_BLOB:
7613 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7614 GOT_OBJ_LABEL_BLOB);
7616 case GOT_OBJ_TYPE_TREE:
7617 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7618 GOT_OBJ_LABEL_TREE);
7620 case GOT_OBJ_TYPE_COMMIT:
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7622 GOT_OBJ_LABEL_COMMIT);
7624 case GOT_OBJ_TYPE_TAG:
7625 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7632 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7633 got_object_tag_get_name(tag));
7635 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7636 got_object_tag_get_tagger(tag),
7637 got_object_tag_get_tagger_time(tag));
7639 tagmsg = got_object_tag_get_message(tag);
7640 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7641 fprintf(outfile, "%s", tagmsg);
7644 got_object_tag_close(tag);
7648 static const struct got_error *
7649 cmd_cat(int argc, char *argv[])
7651 const struct got_error *error;
7652 struct got_repository *repo = NULL;
7653 struct got_worktree *worktree = NULL;
7654 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7655 const char *commit_id_str = NULL;
7656 struct got_object_id *id = NULL, *commit_id = NULL;
7657 int ch, obj_type, i, force_path = 0;
7660 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7665 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7668 commit_id_str = optarg;
7671 repo_path = realpath(optarg, NULL);
7672 if (repo_path == NULL)
7673 return got_error_from_errno2("realpath",
7675 got_path_strip_trailing_slashes(repo_path);
7689 cwd = getcwd(NULL, 0);
7691 error = got_error_from_errno("getcwd");
7694 error = got_worktree_open(&worktree, cwd);
7695 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7698 if (repo_path == NULL) {
7700 got_worktree_get_repo_path(worktree));
7701 if (repo_path == NULL) {
7702 error = got_error_from_errno("strdup");
7708 if (repo_path == NULL) {
7709 repo_path = getcwd(NULL, 0);
7710 if (repo_path == NULL)
7711 return got_error_from_errno("getcwd");
7714 error = got_repo_open(&repo, repo_path, NULL);
7719 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7723 if (commit_id_str == NULL)
7724 commit_id_str = GOT_REF_HEAD;
7725 error = got_repo_match_object_id(&commit_id, NULL,
7726 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7730 for (i = 0; i < argc; i++) {
7732 error = got_object_id_by_path(&id, repo, commit_id,
7737 error = got_repo_match_object_id(&id, &label, argv[i],
7738 GOT_OBJ_TYPE_ANY, 0, repo);
7740 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7741 error->code != GOT_ERR_NOT_REF)
7743 error = got_object_id_by_path(&id, repo,
7744 commit_id, argv[i]);
7750 error = got_object_get_type(&obj_type, repo, id);
7755 case GOT_OBJ_TYPE_BLOB:
7756 error = cat_blob(id, repo, stdout);
7758 case GOT_OBJ_TYPE_TREE:
7759 error = cat_tree(id, repo, stdout);
7761 case GOT_OBJ_TYPE_COMMIT:
7762 error = cat_commit(id, repo, stdout);
7764 case GOT_OBJ_TYPE_TAG:
7765 error = cat_tag(id, repo, stdout);
7768 error = got_error(GOT_ERR_OBJ_TYPE);
7783 got_worktree_close(worktree);
7785 const struct got_error *repo_error;
7786 repo_error = got_repo_close(repo);