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)
976 int *did_something = arg, p;
977 char scaled[FMT_SCALED_STRSIZE];
980 printf("\rserver: %s", message);
982 } else if (packfile_size > 0 || nobj_indexed > 0) {
984 if (fmt_scaled(packfile_size, scaled) == 0)
985 printf(" %*s fetched", FMT_SCALED_STRSIZE, scaled);
986 if (nobj_indexed > 0) {
987 p = (nobj_indexed * 100) / nobj_total;
988 printf("; indexing %d%% (%d)", p, nobj_indexed);
990 if (nobj_resolved > 0) {
991 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
992 printf("; resolving deltas %d%% (%d)",
1001 static const struct got_error *
1002 cmd_clone(int argc, char *argv[])
1004 const struct got_error *err = NULL;
1005 const char *uri, *branch_filter, *dirname;
1006 char *proto, *host, *port, *repo_name, *server_path;
1007 char *default_destdir = NULL, *id_str = NULL;
1008 const char *repo_path;
1009 struct got_repository *repo = NULL;
1010 struct got_pathlist_head refs, symrefs;
1011 struct got_pathlist_entry *pe;
1012 struct got_object_id *pack_hash = NULL;
1013 int ch, fetchfd = -1;
1014 int did_something = 0;
1017 TAILQ_INIT(&symrefs);
1019 while ((ch = getopt(argc, argv, "b:")) != -1) {
1022 branch_filter = optarg;
1039 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1040 &repo_name, argv[0]);
1044 if (dirname == NULL) {
1045 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1046 err = got_error_from_errno("asprintf");
1049 repo_path = default_destdir;
1051 repo_path = dirname;
1053 err = got_path_mkdir(repo_path);
1057 err = got_repo_init(repo_path);
1061 err = got_repo_open(&repo, repo_path, NULL);
1065 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1069 printf("Connected to %s:%s\n", host, port);
1071 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1072 repo, fetch_progress, &did_something);
1078 err = got_object_id_str(&id_str, pack_hash);
1081 printf("Fetched %s.pack\n", id_str);
1084 /* Set up references provided with the pack file. */
1085 TAILQ_FOREACH(pe, &refs, entry) {
1086 const char *refname = pe->path;
1087 struct got_object_id *id = pe->data;
1088 struct got_reference *ref;
1091 err = got_ref_alloc(&ref, refname, id);
1096 err = got_object_id_str(&id_str, id);
1099 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1102 err = got_ref_write(ref, repo);
1108 /* Set the HEAD reference if the server provided one. */
1109 TAILQ_FOREACH(pe, &symrefs, entry) {
1110 struct got_reference *symref, *target_ref;
1111 const char *refname = pe->path;
1112 const char *target = pe->data;
1114 if (strcmp(refname, GOT_REF_HEAD) != 0)
1117 err = got_ref_open(&target_ref, repo, target, 0);
1119 if (err->code == GOT_ERR_NOT_REF)
1124 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1125 got_ref_close(target_ref);
1129 printf("Setting %s to %s\n", GOT_REF_HEAD,
1130 got_ref_get_symref_target(symref));
1132 err = got_ref_write(symref, repo);
1133 got_ref_close(symref);
1138 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1139 err = got_error_from_errno("close");
1141 got_repo_close(repo);
1142 TAILQ_FOREACH(pe, &refs, entry) {
1143 free((void *)pe->path);
1146 got_pathlist_free(&refs);
1147 TAILQ_FOREACH(pe, &symrefs, entry) {
1148 free((void *)pe->path);
1151 got_pathlist_free(&symrefs);
1158 free(default_destdir);
1162 static const struct got_error *
1163 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1165 static char msg[512];
1166 const char *branch_name;
1168 if (got_ref_is_symbolic(ref))
1169 branch_name = got_ref_get_symref_target(ref);
1171 branch_name = got_ref_get_name(ref);
1173 if (strncmp("refs/heads/", branch_name, 11) == 0)
1176 snprintf(msg, sizeof(msg),
1177 "target commit is not contained in branch '%s'; "
1178 "the branch to use must be specified with -b; "
1179 "if necessary a new branch can be created for "
1180 "this commit with 'got branch -c %s BRANCH_NAME'",
1181 branch_name, commit_id_str);
1183 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1186 static const struct got_error *
1187 cmd_checkout(int argc, char *argv[])
1189 const struct got_error *error = NULL;
1190 struct got_repository *repo = NULL;
1191 struct got_reference *head_ref = NULL;
1192 struct got_worktree *worktree = NULL;
1193 char *repo_path = NULL;
1194 char *worktree_path = NULL;
1195 const char *path_prefix = "";
1196 const char *branch_name = GOT_REF_HEAD;
1197 char *commit_id_str = NULL;
1198 int ch, same_path_prefix, allow_nonempty = 0;
1199 struct got_pathlist_head paths;
1200 struct got_checkout_progress_arg cpa;
1204 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1207 branch_name = optarg;
1210 commit_id_str = strdup(optarg);
1211 if (commit_id_str == NULL)
1212 return got_error_from_errno("strdup");
1218 path_prefix = optarg;
1230 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1231 "unveil", NULL) == -1)
1235 char *cwd, *base, *dotgit;
1236 repo_path = realpath(argv[0], NULL);
1237 if (repo_path == NULL)
1238 return got_error_from_errno2("realpath", argv[0]);
1239 cwd = getcwd(NULL, 0);
1241 error = got_error_from_errno("getcwd");
1244 if (path_prefix[0]) {
1245 base = basename(path_prefix);
1247 error = got_error_from_errno2("basename",
1252 base = basename(repo_path);
1254 error = got_error_from_errno2("basename",
1259 dotgit = strstr(base, ".git");
1262 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1263 error = got_error_from_errno("asprintf");
1268 } else if (argc == 2) {
1269 repo_path = realpath(argv[0], NULL);
1270 if (repo_path == NULL) {
1271 error = got_error_from_errno2("realpath", argv[0]);
1274 worktree_path = realpath(argv[1], NULL);
1275 if (worktree_path == NULL) {
1276 if (errno != ENOENT) {
1277 error = got_error_from_errno2("realpath",
1281 worktree_path = strdup(argv[1]);
1282 if (worktree_path == NULL) {
1283 error = got_error_from_errno("strdup");
1290 got_path_strip_trailing_slashes(repo_path);
1291 got_path_strip_trailing_slashes(worktree_path);
1293 error = got_repo_open(&repo, repo_path, NULL);
1297 /* Pre-create work tree path for unveil(2) */
1298 error = got_path_mkdir(worktree_path);
1300 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1301 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1303 if (!allow_nonempty &&
1304 !got_path_dir_is_empty(worktree_path)) {
1305 error = got_error_path(worktree_path,
1306 GOT_ERR_DIR_NOT_EMPTY);
1311 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1315 error = got_ref_open(&head_ref, repo, branch_name, 0);
1319 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1320 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1323 error = got_worktree_open(&worktree, worktree_path);
1327 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1331 if (!same_path_prefix) {
1332 error = got_error(GOT_ERR_PATH_PREFIX);
1336 if (commit_id_str) {
1337 struct got_object_id *commit_id;
1338 error = got_repo_match_object_id(&commit_id, NULL,
1339 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1342 error = check_linear_ancestry(commit_id,
1343 got_worktree_get_base_commit_id(worktree), 0, repo);
1344 if (error != NULL) {
1346 if (error->code == GOT_ERR_ANCESTRY) {
1347 error = checkout_ancestry_error(
1348 head_ref, commit_id_str);
1352 error = check_same_branch(commit_id, head_ref, NULL, repo);
1354 if (error->code == GOT_ERR_ANCESTRY) {
1355 error = checkout_ancestry_error(
1356 head_ref, commit_id_str);
1360 error = got_worktree_set_base_commit_id(worktree, repo,
1367 error = got_pathlist_append(&paths, "", NULL);
1370 cpa.worktree_path = worktree_path;
1371 cpa.had_base_commit_ref_error = 0;
1372 error = got_worktree_checkout_files(worktree, &paths, repo,
1373 checkout_progress, &cpa, check_cancelled, NULL);
1377 printf("Now shut up and hack\n");
1378 if (cpa.had_base_commit_ref_error)
1379 show_worktree_base_ref_warning();
1381 got_pathlist_free(&paths);
1382 free(commit_id_str);
1384 free(worktree_path);
1391 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1396 static const struct got_error *
1397 update_progress(void *arg, unsigned char status, const char *path)
1399 int *did_something = arg;
1401 if (status == GOT_STATUS_EXISTS ||
1402 status == GOT_STATUS_BASE_REF_ERR)
1407 /* Base commit bump happens silently. */
1408 if (status == GOT_STATUS_BUMP_BASE)
1411 while (path[0] == '/')
1413 printf("%c %s\n", status, path);
1417 static const struct got_error *
1418 switch_head_ref(struct got_reference *head_ref,
1419 struct got_object_id *commit_id, struct got_worktree *worktree,
1420 struct got_repository *repo)
1422 const struct got_error *err = NULL;
1424 int ref_has_moved = 0;
1426 /* Trivial case: switching between two different references. */
1427 if (strcmp(got_ref_get_name(head_ref),
1428 got_worktree_get_head_ref_name(worktree)) != 0) {
1429 printf("Switching work tree from %s to %s\n",
1430 got_worktree_get_head_ref_name(worktree),
1431 got_ref_get_name(head_ref));
1432 return got_worktree_set_head_ref(worktree, head_ref);
1435 err = check_linear_ancestry(commit_id,
1436 got_worktree_get_base_commit_id(worktree), 0, repo);
1438 if (err->code != GOT_ERR_ANCESTRY)
1445 /* Switching to a rebased branch with the same reference name. */
1446 err = got_object_id_str(&base_id_str,
1447 got_worktree_get_base_commit_id(worktree));
1450 printf("Reference %s now points at a different branch\n",
1451 got_worktree_get_head_ref_name(worktree));
1452 printf("Switching work tree from %s to %s\n", base_id_str,
1453 got_worktree_get_head_ref_name(worktree));
1457 static const struct got_error *
1458 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1460 const struct got_error *err;
1463 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1467 return got_error(GOT_ERR_REBASING);
1469 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1473 return got_error(GOT_ERR_HISTEDIT_BUSY);
1478 static const struct got_error *
1479 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1480 char *argv[], struct got_worktree *worktree)
1482 const struct got_error *err = NULL;
1489 return got_error_from_errno("strdup");
1490 return got_pathlist_append(paths, path, NULL);
1493 for (i = 0; i < argc; i++) {
1494 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1497 err = got_pathlist_append(paths, path, NULL);
1507 static const struct got_error *
1508 cmd_update(int argc, char *argv[])
1510 const struct got_error *error = NULL;
1511 struct got_repository *repo = NULL;
1512 struct got_worktree *worktree = NULL;
1513 char *worktree_path = NULL;
1514 struct got_object_id *commit_id = NULL;
1515 char *commit_id_str = NULL;
1516 const char *branch_name = NULL;
1517 struct got_reference *head_ref = NULL;
1518 struct got_pathlist_head paths;
1519 struct got_pathlist_entry *pe;
1520 int ch, did_something = 0;
1524 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1527 branch_name = optarg;
1530 commit_id_str = strdup(optarg);
1531 if (commit_id_str == NULL)
1532 return got_error_from_errno("strdup");
1544 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1545 "unveil", NULL) == -1)
1548 worktree_path = getcwd(NULL, 0);
1549 if (worktree_path == NULL) {
1550 error = got_error_from_errno("getcwd");
1553 error = got_worktree_open(&worktree, worktree_path);
1557 error = check_rebase_or_histedit_in_progress(worktree);
1561 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1566 error = apply_unveil(got_repo_get_path(repo), 0,
1567 got_worktree_get_root_path(worktree));
1571 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1575 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1576 got_worktree_get_head_ref_name(worktree), 0);
1579 if (commit_id_str == NULL) {
1580 error = got_ref_resolve(&commit_id, repo, head_ref);
1583 error = got_object_id_str(&commit_id_str, commit_id);
1587 error = got_repo_match_object_id(&commit_id, NULL,
1588 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1589 free(commit_id_str);
1590 commit_id_str = NULL;
1593 error = got_object_id_str(&commit_id_str, commit_id);
1599 struct got_object_id *head_commit_id;
1600 TAILQ_FOREACH(pe, &paths, entry) {
1601 if (pe->path_len == 0)
1603 error = got_error_msg(GOT_ERR_BAD_PATH,
1604 "switching between branches requires that "
1605 "the entire work tree gets updated");
1608 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1611 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1613 free(head_commit_id);
1616 error = check_same_branch(commit_id, head_ref, NULL, repo);
1619 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1623 error = check_linear_ancestry(commit_id,
1624 got_worktree_get_base_commit_id(worktree), 0, repo);
1625 if (error != NULL) {
1626 if (error->code == GOT_ERR_ANCESTRY)
1627 error = got_error(GOT_ERR_BRANCH_MOVED);
1630 error = check_same_branch(commit_id, head_ref, NULL, repo);
1635 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1637 error = got_worktree_set_base_commit_id(worktree, repo,
1643 error = got_worktree_checkout_files(worktree, &paths, repo,
1644 update_progress, &did_something, check_cancelled, NULL);
1649 printf("Updated to commit %s\n", commit_id_str);
1651 printf("Already up-to-date\n");
1653 free(worktree_path);
1654 TAILQ_FOREACH(pe, &paths, entry)
1655 free((char *)pe->path);
1656 got_pathlist_free(&paths);
1658 free(commit_id_str);
1662 static const struct got_error *
1663 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1664 const char *path, int diff_context, int ignore_whitespace,
1665 struct got_repository *repo)
1667 const struct got_error *err = NULL;
1668 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1671 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1676 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1680 while (path[0] == '/')
1682 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1683 ignore_whitespace, stdout);
1686 got_object_blob_close(blob1);
1687 got_object_blob_close(blob2);
1691 static const struct got_error *
1692 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1693 const char *path, int diff_context, int ignore_whitespace,
1694 struct got_repository *repo)
1696 const struct got_error *err = NULL;
1697 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1698 struct got_diff_blob_output_unidiff_arg arg;
1701 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1706 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1710 arg.diff_context = diff_context;
1711 arg.ignore_whitespace = ignore_whitespace;
1712 arg.outfile = stdout;
1713 while (path[0] == '/')
1715 err = got_diff_tree(tree1, tree2, path, path, repo,
1716 got_diff_blob_output_unidiff, &arg, 1);
1719 got_object_tree_close(tree1);
1721 got_object_tree_close(tree2);
1725 static const struct got_error *
1726 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1727 const char *path, int diff_context, struct got_repository *repo)
1729 const struct got_error *err = NULL;
1730 struct got_commit_object *pcommit = NULL;
1731 char *id_str1 = NULL, *id_str2 = NULL;
1732 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1733 struct got_object_qid *qid;
1735 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1737 err = got_object_open_as_commit(&pcommit, repo,
1743 if (path && path[0] != '\0') {
1745 err = got_object_id_by_path(&obj_id2, repo, id, path);
1748 err = got_object_id_str(&id_str2, obj_id2);
1754 err = got_object_id_by_path(&obj_id1, repo,
1760 err = got_object_id_str(&id_str1, obj_id1);
1766 err = got_object_get_type(&obj_type, repo, obj_id2);
1771 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1773 case GOT_OBJ_TYPE_BLOB:
1774 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1777 case GOT_OBJ_TYPE_TREE:
1778 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1782 err = got_error(GOT_ERR_OBJ_TYPE);
1788 obj_id2 = got_object_commit_get_tree_id(commit);
1789 err = got_object_id_str(&id_str2, obj_id2);
1792 obj_id1 = got_object_commit_get_tree_id(pcommit);
1793 err = got_object_id_str(&id_str1, obj_id1);
1796 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1797 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1803 got_object_commit_close(pcommit);
1808 get_datestr(time_t *time, char *datebuf)
1810 struct tm mytm, *tm;
1813 tm = gmtime_r(time, &mytm);
1816 s = asctime_r(tm, datebuf);
1819 p = strchr(s, '\n');
1825 static const struct got_error *
1826 match_logmsg(int *have_match, struct got_object_id *id,
1827 struct got_commit_object *commit, regex_t *regex)
1829 const struct got_error *err = NULL;
1830 regmatch_t regmatch;
1831 char *id_str = NULL, *logmsg = NULL;
1835 err = got_object_id_str(&id_str, id);
1839 err = got_object_commit_get_logmsg(&logmsg, commit);
1843 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1851 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1853 static const struct got_error *
1854 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1855 struct got_repository *repo, const char *path, int show_patch,
1856 int diff_context, struct got_reflist_head *refs)
1858 const struct got_error *err = NULL;
1859 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1861 time_t committer_time;
1862 const char *author, *committer;
1863 char *refs_str = NULL;
1864 struct got_reflist_entry *re;
1866 SIMPLEQ_FOREACH(re, refs, entry) {
1869 struct got_tag_object *tag = NULL;
1872 name = got_ref_get_name(re->ref);
1873 if (strcmp(name, GOT_REF_HEAD) == 0)
1875 if (strncmp(name, "refs/", 5) == 0)
1877 if (strncmp(name, "got/", 4) == 0)
1879 if (strncmp(name, "heads/", 6) == 0)
1881 if (strncmp(name, "remotes/", 8) == 0)
1883 if (strncmp(name, "tags/", 5) == 0) {
1884 err = got_object_open_as_tag(&tag, repo, re->id);
1886 if (err->code != GOT_ERR_OBJ_TYPE)
1888 /* Ref points at something other than a tag. */
1893 cmp = got_object_id_cmp(tag ?
1894 got_object_tag_get_object_id(tag) : re->id, id);
1896 got_object_tag_close(tag);
1900 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1902 err = got_error_from_errno("asprintf");
1908 err = got_object_id_str(&id_str, id);
1912 printf(GOT_COMMIT_SEP_STR);
1913 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1914 refs_str ? refs_str : "", refs_str ? ")" : "");
1919 printf("from: %s\n", got_object_commit_get_author(commit));
1920 committer_time = got_object_commit_get_committer_time(commit);
1921 datestr = get_datestr(&committer_time, datebuf);
1923 printf("date: %s UTC\n", datestr);
1924 author = got_object_commit_get_author(commit);
1925 committer = got_object_commit_get_committer(commit);
1926 if (strcmp(author, committer) != 0)
1927 printf("via: %s\n", committer);
1928 if (got_object_commit_get_nparents(commit) > 1) {
1929 const struct got_object_id_queue *parent_ids;
1930 struct got_object_qid *qid;
1932 parent_ids = got_object_commit_get_parent_ids(commit);
1933 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1934 err = got_object_id_str(&id_str, qid->id);
1937 printf("parent %d: %s\n", n++, id_str);
1942 err = got_object_commit_get_logmsg(&logmsg0, commit);
1948 line = strsep(&logmsg, "\n");
1950 printf(" %s\n", line);
1955 err = print_patch(commit, id, path, diff_context, repo);
1960 if (fflush(stdout) != 0 && err == NULL)
1961 err = got_error_from_errno("fflush");
1965 static const struct got_error *
1966 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1967 const char *path, int show_patch, const char *search_pattern,
1968 int diff_context, int limit, int log_branches,
1969 struct got_reflist_head *refs)
1971 const struct got_error *err;
1972 struct got_commit_graph *graph;
1976 if (search_pattern &&
1977 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1978 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1980 err = got_commit_graph_open(&graph, path, !log_branches);
1983 err = got_commit_graph_iter_start(graph, root_id, repo,
1984 check_cancelled, NULL);
1988 struct got_commit_object *commit;
1989 struct got_object_id *id;
1991 if (sigint_received || sigpipe_received)
1994 err = got_commit_graph_iter_next(&id, graph, repo,
1995 check_cancelled, NULL);
1997 if (err->code == GOT_ERR_ITER_COMPLETED)
2004 err = got_object_open_as_commit(&commit, repo, id);
2008 if (search_pattern) {
2009 err = match_logmsg(&have_match, id, commit, ®ex);
2011 got_object_commit_close(commit);
2014 if (have_match == 0) {
2015 got_object_commit_close(commit);
2020 err = print_commit(commit, id, repo, path, show_patch,
2021 diff_context, refs);
2022 got_object_commit_close(commit);
2023 if (err || (limit && --limit == 0))
2029 got_commit_graph_close(graph);
2036 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2037 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2042 get_default_log_limit(void)
2044 const char *got_default_log_limit;
2048 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2049 if (got_default_log_limit == NULL)
2051 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2057 static const struct got_error *
2058 cmd_log(int argc, char *argv[])
2060 const struct got_error *error;
2061 struct got_repository *repo = NULL;
2062 struct got_worktree *worktree = NULL;
2063 struct got_commit_object *commit = NULL;
2064 struct got_object_id *id = NULL;
2065 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2066 const char *start_commit = NULL, *search_pattern = NULL;
2067 int diff_context = -1, ch;
2068 int show_patch = 0, limit = 0, log_branches = 0;
2070 struct got_reflist_head refs;
2072 SIMPLEQ_INIT(&refs);
2075 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2081 limit = get_default_log_limit();
2083 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2089 start_commit = optarg;
2092 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2095 err(1, "-C option %s", errstr);
2098 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2100 err(1, "-l option %s", errstr);
2106 repo_path = realpath(optarg, NULL);
2107 if (repo_path == NULL)
2108 return got_error_from_errno2("realpath",
2110 got_path_strip_trailing_slashes(repo_path);
2113 search_pattern = optarg;
2124 if (diff_context == -1)
2126 else if (!show_patch)
2127 errx(1, "-C reguires -p");
2129 cwd = getcwd(NULL, 0);
2131 error = got_error_from_errno("getcwd");
2135 error = got_worktree_open(&worktree, cwd);
2136 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2143 error = got_error_from_errno("strdup");
2146 } else if (argc == 1) {
2148 error = got_worktree_resolve_path(&path, worktree,
2153 path = strdup(argv[0]);
2155 error = got_error_from_errno("strdup");
2162 if (repo_path == NULL) {
2163 repo_path = worktree ?
2164 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2166 if (repo_path == NULL) {
2167 error = got_error_from_errno("strdup");
2171 error = got_repo_open(&repo, repo_path, NULL);
2175 error = apply_unveil(got_repo_get_path(repo), 1,
2176 worktree ? got_worktree_get_root_path(worktree) : NULL);
2180 if (start_commit == NULL) {
2181 struct got_reference *head_ref;
2182 error = got_ref_open(&head_ref, repo,
2183 worktree ? got_worktree_get_head_ref_name(worktree)
2187 error = got_ref_resolve(&id, repo, head_ref);
2188 got_ref_close(head_ref);
2191 error = got_object_open_as_commit(&commit, repo, id);
2193 struct got_reference *ref;
2194 error = got_ref_open(&ref, repo, start_commit, 0);
2195 if (error == NULL) {
2197 error = got_ref_resolve(&id, repo, ref);
2201 error = got_object_get_type(&obj_type, repo, id);
2204 if (obj_type == GOT_OBJ_TYPE_TAG) {
2205 struct got_tag_object *tag;
2206 error = got_object_open_as_tag(&tag, repo, id);
2209 if (got_object_tag_get_object_type(tag) !=
2210 GOT_OBJ_TYPE_COMMIT) {
2211 got_object_tag_close(tag);
2212 error = got_error(GOT_ERR_OBJ_TYPE);
2216 id = got_object_id_dup(
2217 got_object_tag_get_object_id(tag));
2219 error = got_error_from_errno(
2220 "got_object_id_dup");
2221 got_object_tag_close(tag);
2224 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2225 error = got_error(GOT_ERR_OBJ_TYPE);
2228 error = got_object_open_as_commit(&commit, repo, id);
2232 if (commit == NULL) {
2233 error = got_repo_match_object_id_prefix(&id,
2234 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2243 const char *prefix = got_worktree_get_path_prefix(worktree);
2245 if (asprintf(&p, "%s%s%s", prefix,
2246 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2247 error = got_error_from_errno("asprintf");
2250 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2253 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2258 path = in_repo_path;
2261 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2265 error = print_commits(id, repo, path, show_patch, search_pattern,
2266 diff_context, limit, log_branches, &refs);
2273 got_worktree_close(worktree);
2275 const struct got_error *repo_error;
2276 repo_error = got_repo_close(repo);
2280 got_ref_list_free(&refs);
2287 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2288 "[-w] [object1 object2 | path]\n", getprogname());
2292 struct print_diff_arg {
2293 struct got_repository *repo;
2294 struct got_worktree *worktree;
2299 int ignore_whitespace;
2302 static const struct got_error *
2303 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2304 const char *path, struct got_object_id *blob_id,
2305 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2306 int dirfd, const char *de_name)
2308 struct print_diff_arg *a = arg;
2309 const struct got_error *err = NULL;
2310 struct got_blob_object *blob1 = NULL;
2313 char *abspath = NULL, *label1 = NULL;
2316 if (a->diff_staged) {
2317 if (staged_status != GOT_STATUS_MODIFY &&
2318 staged_status != GOT_STATUS_ADD &&
2319 staged_status != GOT_STATUS_DELETE)
2322 if (staged_status == GOT_STATUS_DELETE)
2324 if (status == GOT_STATUS_NONEXISTENT)
2325 return got_error_set_errno(ENOENT, path);
2326 if (status != GOT_STATUS_MODIFY &&
2327 status != GOT_STATUS_ADD &&
2328 status != GOT_STATUS_DELETE &&
2329 status != GOT_STATUS_CONFLICT)
2333 if (!a->header_shown) {
2334 printf("diff %s %s%s\n", a->id_str,
2335 got_worktree_get_root_path(a->worktree),
2336 a->diff_staged ? " (staged changes)" : "");
2337 a->header_shown = 1;
2340 if (a->diff_staged) {
2341 const char *label1 = NULL, *label2 = NULL;
2342 switch (staged_status) {
2343 case GOT_STATUS_MODIFY:
2347 case GOT_STATUS_ADD:
2350 case GOT_STATUS_DELETE:
2354 return got_error(GOT_ERR_FILE_STATUS);
2356 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2357 label1, label2, a->diff_context, a->ignore_whitespace,
2361 if (staged_status == GOT_STATUS_ADD ||
2362 staged_status == GOT_STATUS_MODIFY) {
2364 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2368 err = got_object_id_str(&id_str, staged_blob_id);
2371 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2372 err = got_error_from_errno("asprintf");
2377 } else if (status != GOT_STATUS_ADD) {
2378 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2383 if (status != GOT_STATUS_DELETE) {
2384 if (asprintf(&abspath, "%s/%s",
2385 got_worktree_get_root_path(a->worktree), path) == -1) {
2386 err = got_error_from_errno("asprintf");
2391 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2393 err = got_error_from_errno2("openat", abspath);
2397 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2399 err = got_error_from_errno2("open", abspath);
2403 if (fstat(fd, &sb) == -1) {
2404 err = got_error_from_errno2("fstat", abspath);
2407 f2 = fdopen(fd, "r");
2409 err = got_error_from_errno2("fdopen", abspath);
2416 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2417 a->diff_context, a->ignore_whitespace, stdout);
2420 got_object_blob_close(blob1);
2421 if (f2 && fclose(f2) == EOF && err == NULL)
2422 err = got_error_from_errno("fclose");
2423 if (fd != -1 && close(fd) == -1 && err == NULL)
2424 err = got_error_from_errno("close");
2429 static const struct got_error *
2430 cmd_diff(int argc, char *argv[])
2432 const struct got_error *error;
2433 struct got_repository *repo = NULL;
2434 struct got_worktree *worktree = NULL;
2435 char *cwd = NULL, *repo_path = NULL;
2436 struct got_object_id *id1 = NULL, *id2 = NULL;
2437 const char *id_str1 = NULL, *id_str2 = NULL;
2438 char *label1 = NULL, *label2 = NULL;
2440 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2445 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2450 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2453 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2456 err(1, "-C option %s", errstr);
2459 repo_path = realpath(optarg, NULL);
2460 if (repo_path == NULL)
2461 return got_error_from_errno2("realpath",
2463 got_path_strip_trailing_slashes(repo_path);
2469 ignore_whitespace = 1;
2480 cwd = getcwd(NULL, 0);
2482 error = got_error_from_errno("getcwd");
2485 error = got_worktree_open(&worktree, cwd);
2486 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2489 if (worktree == NULL) {
2490 error = got_error(GOT_ERR_NOT_WORKTREE);
2495 "-r option can't be used when diffing a work tree");
2496 repo_path = strdup(got_worktree_get_repo_path(worktree));
2497 if (repo_path == NULL) {
2498 error = got_error_from_errno("strdup");
2502 error = got_worktree_resolve_path(&path, worktree,
2509 error = got_error_from_errno("strdup");
2513 } else if (argc == 2) {
2515 errx(1, "-s option can't be used when diffing "
2516 "objects in repository");
2519 if (worktree && repo_path == NULL) {
2521 strdup(got_worktree_get_repo_path(worktree));
2522 if (repo_path == NULL) {
2523 error = got_error_from_errno("strdup");
2530 if (repo_path == NULL) {
2531 repo_path = getcwd(NULL, 0);
2532 if (repo_path == NULL)
2533 return got_error_from_errno("getcwd");
2536 error = got_repo_open(&repo, repo_path, NULL);
2541 error = apply_unveil(got_repo_get_path(repo), 1,
2542 worktree ? got_worktree_get_root_path(worktree) : NULL);
2547 struct print_diff_arg arg;
2548 struct got_pathlist_head paths;
2553 error = got_object_id_str(&id_str,
2554 got_worktree_get_base_commit_id(worktree));
2558 arg.worktree = worktree;
2559 arg.diff_context = diff_context;
2560 arg.id_str = id_str;
2561 arg.header_shown = 0;
2562 arg.diff_staged = diff_staged;
2563 arg.ignore_whitespace = ignore_whitespace;
2565 error = got_pathlist_append(&paths, path, NULL);
2569 error = got_worktree_status(worktree, &paths, repo, print_diff,
2570 &arg, check_cancelled, NULL);
2572 got_pathlist_free(&paths);
2576 error = got_repo_match_object_id(&id1, &label1, id_str1,
2577 GOT_OBJ_TYPE_ANY, 1, repo);
2581 error = got_repo_match_object_id(&id2, &label2, id_str2,
2582 GOT_OBJ_TYPE_ANY, 1, repo);
2586 error = got_object_get_type(&type1, repo, id1);
2590 error = got_object_get_type(&type2, repo, id2);
2594 if (type1 != type2) {
2595 error = got_error(GOT_ERR_OBJ_TYPE);
2600 case GOT_OBJ_TYPE_BLOB:
2601 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2602 diff_context, ignore_whitespace, repo, stdout);
2604 case GOT_OBJ_TYPE_TREE:
2605 error = got_diff_objects_as_trees(id1, id2, "", "",
2606 diff_context, ignore_whitespace, repo, stdout);
2608 case GOT_OBJ_TYPE_COMMIT:
2609 printf("diff %s %s\n", label1, label2);
2610 error = got_diff_objects_as_commits(id1, id2, diff_context,
2611 ignore_whitespace, repo, stdout);
2614 error = got_error(GOT_ERR_OBJ_TYPE);
2623 got_worktree_close(worktree);
2625 const struct got_error *repo_error;
2626 repo_error = got_repo_close(repo);
2637 "usage: %s blame [-c commit] [-r repository-path] path\n",
2646 char datebuf[11]; /* YYYY-MM-DD + NUL */
2649 struct blame_cb_args {
2650 struct blame_line *lines;
2654 off_t *line_offsets;
2656 struct got_repository *repo;
2659 static const struct got_error *
2660 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2662 const struct got_error *err = NULL;
2663 struct blame_cb_args *a = arg;
2664 struct blame_line *bline;
2666 size_t linesize = 0;
2667 struct got_commit_object *commit = NULL;
2670 time_t committer_time;
2672 if (nlines != a->nlines ||
2673 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2674 return got_error(GOT_ERR_RANGE);
2676 if (sigint_received)
2677 return got_error(GOT_ERR_ITER_COMPLETED);
2680 return NULL; /* no change in this commit */
2682 /* Annotate this line. */
2683 bline = &a->lines[lineno - 1];
2684 if (bline->annotated)
2686 err = got_object_id_str(&bline->id_str, id);
2690 err = got_object_open_as_commit(&commit, a->repo, id);
2694 bline->committer = strdup(got_object_commit_get_committer(commit));
2695 if (bline->committer == NULL) {
2696 err = got_error_from_errno("strdup");
2700 committer_time = got_object_commit_get_committer_time(commit);
2701 if (localtime_r(&committer_time, &tm) == NULL)
2702 return got_error_from_errno("localtime_r");
2703 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2704 &tm) >= sizeof(bline->datebuf)) {
2705 err = got_error(GOT_ERR_NO_SPACE);
2708 bline->annotated = 1;
2710 /* Print lines annotated so far. */
2711 bline = &a->lines[a->lineno_cur - 1];
2712 if (!bline->annotated)
2715 offset = a->line_offsets[a->lineno_cur - 1];
2716 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2717 err = got_error_from_errno("fseeko");
2721 while (bline->annotated) {
2722 char *smallerthan, *at, *nl, *committer;
2725 if (getline(&line, &linesize, a->f) == -1) {
2727 err = got_error_from_errno("getline");
2731 committer = bline->committer;
2732 smallerthan = strchr(committer, '<');
2733 if (smallerthan && smallerthan[1] != '\0')
2734 committer = smallerthan + 1;
2735 at = strchr(committer, '@');
2738 len = strlen(committer);
2740 committer[8] = '\0';
2742 nl = strchr(line, '\n');
2745 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2746 bline->id_str, bline->datebuf, committer, line);
2749 bline = &a->lines[a->lineno_cur - 1];
2753 got_object_commit_close(commit);
2758 static const struct got_error *
2759 cmd_blame(int argc, char *argv[])
2761 const struct got_error *error;
2762 struct got_repository *repo = NULL;
2763 struct got_worktree *worktree = NULL;
2764 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2765 struct got_object_id *obj_id = NULL;
2766 struct got_object_id *commit_id = NULL;
2767 struct got_blob_object *blob = NULL;
2768 char *commit_id_str = NULL;
2769 struct blame_cb_args bca;
2770 int ch, obj_type, i;
2773 memset(&bca, 0, sizeof(bca));
2776 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2781 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2784 commit_id_str = optarg;
2787 repo_path = realpath(optarg, NULL);
2788 if (repo_path == NULL)
2789 return got_error_from_errno2("realpath",
2791 got_path_strip_trailing_slashes(repo_path);
2807 cwd = getcwd(NULL, 0);
2809 error = got_error_from_errno("getcwd");
2812 if (repo_path == NULL) {
2813 error = got_worktree_open(&worktree, cwd);
2814 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2820 strdup(got_worktree_get_repo_path(worktree));
2821 if (repo_path == NULL)
2822 error = got_error_from_errno("strdup");
2826 repo_path = strdup(cwd);
2827 if (repo_path == NULL) {
2828 error = got_error_from_errno("strdup");
2834 error = got_repo_open(&repo, repo_path, NULL);
2838 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2843 const char *prefix = got_worktree_get_path_prefix(worktree);
2844 char *p, *worktree_subdir = cwd +
2845 strlen(got_worktree_get_root_path(worktree));
2846 if (asprintf(&p, "%s%s%s%s%s",
2847 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2848 worktree_subdir, worktree_subdir[0] ? "/" : "",
2850 error = got_error_from_errno("asprintf");
2853 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2856 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2861 if (commit_id_str == NULL) {
2862 struct got_reference *head_ref;
2863 error = got_ref_open(&head_ref, repo, worktree ?
2864 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2867 error = got_ref_resolve(&commit_id, repo, head_ref);
2868 got_ref_close(head_ref);
2872 error = got_repo_match_object_id(&commit_id, NULL,
2873 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2878 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2882 error = got_object_get_type(&obj_type, repo, obj_id);
2886 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2887 error = got_error(GOT_ERR_OBJ_TYPE);
2891 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2894 bca.f = got_opentemp();
2895 if (bca.f == NULL) {
2896 error = got_error_from_errno("got_opentemp");
2899 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2900 &bca.line_offsets, bca.f, blob);
2901 if (error || bca.nlines == 0)
2904 /* Don't include \n at EOF in the blame line count. */
2905 if (bca.line_offsets[bca.nlines - 1] == filesize)
2908 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2909 if (bca.lines == NULL) {
2910 error = got_error_from_errno("calloc");
2914 bca.nlines_prec = 0;
2922 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2923 check_cancelled, NULL);
2931 got_object_blob_close(blob);
2933 got_worktree_close(worktree);
2935 const struct got_error *repo_error;
2936 repo_error = got_repo_close(repo);
2941 for (i = 0; i < bca.nlines; i++) {
2942 struct blame_line *bline = &bca.lines[i];
2943 free(bline->id_str);
2944 free(bline->committer);
2948 free(bca.line_offsets);
2949 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2950 error = got_error_from_errno("fclose");
2958 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2964 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2965 const char *root_path)
2967 int is_root_path = (strcmp(path, root_path) == 0);
2968 const char *modestr = "";
2969 mode_t mode = got_tree_entry_get_mode(te);
2971 path += strlen(root_path);
2972 while (path[0] == '/')
2975 if (got_object_tree_entry_is_submodule(te))
2977 else if (S_ISLNK(mode))
2979 else if (S_ISDIR(mode))
2981 else if (mode & S_IXUSR)
2984 printf("%s%s%s%s%s\n", id ? id : "", path,
2985 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2988 static const struct got_error *
2989 print_tree(const char *path, struct got_object_id *commit_id,
2990 int show_ids, int recurse, const char *root_path,
2991 struct got_repository *repo)
2993 const struct got_error *err = NULL;
2994 struct got_object_id *tree_id = NULL;
2995 struct got_tree_object *tree = NULL;
2998 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3002 err = got_object_open_as_tree(&tree, repo, tree_id);
3005 nentries = got_object_tree_get_nentries(tree);
3006 for (i = 0; i < nentries; i++) {
3007 struct got_tree_entry *te;
3010 if (sigint_received || sigpipe_received)
3013 te = got_object_tree_get_entry(tree, i);
3016 err = got_object_id_str(&id_str,
3017 got_tree_entry_get_id(te));
3020 if (asprintf(&id, "%s ", id_str) == -1) {
3021 err = got_error_from_errno("asprintf");
3027 print_entry(te, id, path, root_path);
3030 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3032 if (asprintf(&child_path, "%s%s%s", path,
3033 path[0] == '/' && path[1] == '\0' ? "" : "/",
3034 got_tree_entry_get_name(te)) == -1) {
3035 err = got_error_from_errno("asprintf");
3038 err = print_tree(child_path, commit_id, show_ids, 1,
3047 got_object_tree_close(tree);
3052 static const struct got_error *
3053 cmd_tree(int argc, char *argv[])
3055 const struct got_error *error;
3056 struct got_repository *repo = NULL;
3057 struct got_worktree *worktree = NULL;
3059 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3060 struct got_object_id *commit_id = NULL;
3061 char *commit_id_str = NULL;
3062 int show_ids = 0, recurse = 0;
3066 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3071 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3074 commit_id_str = optarg;
3077 repo_path = realpath(optarg, NULL);
3078 if (repo_path == NULL)
3079 return got_error_from_errno2("realpath",
3081 got_path_strip_trailing_slashes(repo_path);
3105 cwd = getcwd(NULL, 0);
3107 error = got_error_from_errno("getcwd");
3110 if (repo_path == NULL) {
3111 error = got_worktree_open(&worktree, cwd);
3112 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3118 strdup(got_worktree_get_repo_path(worktree));
3119 if (repo_path == NULL)
3120 error = got_error_from_errno("strdup");
3124 repo_path = strdup(cwd);
3125 if (repo_path == NULL) {
3126 error = got_error_from_errno("strdup");
3132 error = got_repo_open(&repo, repo_path, NULL);
3136 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3142 char *p, *worktree_subdir = cwd +
3143 strlen(got_worktree_get_root_path(worktree));
3144 if (asprintf(&p, "%s/%s",
3145 got_worktree_get_path_prefix(worktree),
3146 worktree_subdir) == -1) {
3147 error = got_error_from_errno("asprintf");
3150 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3157 if (in_repo_path == NULL) {
3158 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3163 if (commit_id_str == NULL) {
3164 struct got_reference *head_ref;
3165 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3168 error = got_ref_resolve(&commit_id, repo, head_ref);
3169 got_ref_close(head_ref);
3173 error = got_repo_match_object_id(&commit_id, NULL,
3174 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3179 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3180 in_repo_path, repo);
3187 got_worktree_close(worktree);
3189 const struct got_error *repo_error;
3190 repo_error = got_repo_close(repo);
3200 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3204 static const struct got_error *
3205 print_status(void *arg, unsigned char status, unsigned char staged_status,
3206 const char *path, struct got_object_id *blob_id,
3207 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3208 int dirfd, const char *de_name)
3210 if (status == staged_status && (status == GOT_STATUS_DELETE))
3211 status = GOT_STATUS_NO_CHANGE;
3212 printf("%c%c %s\n", status, staged_status, path);
3216 static const struct got_error *
3217 cmd_status(int argc, char *argv[])
3219 const struct got_error *error = NULL;
3220 struct got_repository *repo = NULL;
3221 struct got_worktree *worktree = NULL;
3223 struct got_pathlist_head paths;
3224 struct got_pathlist_entry *pe;
3229 while ((ch = getopt(argc, argv, "")) != -1) {
3241 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3245 cwd = getcwd(NULL, 0);
3247 error = got_error_from_errno("getcwd");
3251 error = got_worktree_open(&worktree, cwd);
3255 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3260 error = apply_unveil(got_repo_get_path(repo), 1,
3261 got_worktree_get_root_path(worktree));
3265 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3269 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3270 check_cancelled, NULL);
3272 TAILQ_FOREACH(pe, &paths, entry)
3273 free((char *)pe->path);
3274 got_pathlist_free(&paths);
3283 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3288 static const struct got_error *
3289 list_refs(struct got_repository *repo)
3291 static const struct got_error *err = NULL;
3292 struct got_reflist_head refs;
3293 struct got_reflist_entry *re;
3295 SIMPLEQ_INIT(&refs);
3296 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3300 SIMPLEQ_FOREACH(re, &refs, entry) {
3302 refstr = got_ref_to_str(re->ref);
3304 return got_error_from_errno("got_ref_to_str");
3305 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3309 got_ref_list_free(&refs);
3313 static const struct got_error *
3314 delete_ref(struct got_repository *repo, const char *refname)
3316 const struct got_error *err = NULL;
3317 struct got_reference *ref;
3319 err = got_ref_open(&ref, repo, refname, 0);
3323 err = got_ref_delete(ref, repo);
3328 static const struct got_error *
3329 add_ref(struct got_repository *repo, const char *refname, const char *target)
3331 const struct got_error *err = NULL;
3332 struct got_object_id *id;
3333 struct got_reference *ref = NULL;
3336 * Don't let the user create a reference name with a leading '-'.
3337 * While technically a valid reference name, this case is usually
3338 * an unintended typo.
3340 if (refname[0] == '-')
3341 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3343 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3346 struct got_reference *target_ref;
3348 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3350 err = got_ref_open(&target_ref, repo, target, 0);
3353 err = got_ref_resolve(&id, repo, target_ref);
3354 got_ref_close(target_ref);
3359 err = got_ref_alloc(&ref, refname, id);
3363 err = got_ref_write(ref, repo);
3371 static const struct got_error *
3372 add_symref(struct got_repository *repo, const char *refname, const char *target)
3374 const struct got_error *err = NULL;
3375 struct got_reference *ref = NULL;
3376 struct got_reference *target_ref = NULL;
3379 * Don't let the user create a reference name with a leading '-'.
3380 * While technically a valid reference name, this case is usually
3381 * an unintended typo.
3383 if (refname[0] == '-')
3384 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3386 err = got_ref_open(&target_ref, repo, target, 0);
3390 err = got_ref_alloc_symref(&ref, refname, target_ref);
3394 err = got_ref_write(ref, repo);
3397 got_ref_close(target_ref);
3403 static const struct got_error *
3404 cmd_ref(int argc, char *argv[])
3406 const struct got_error *error = NULL;
3407 struct got_repository *repo = NULL;
3408 struct got_worktree *worktree = NULL;
3409 char *cwd = NULL, *repo_path = NULL;
3410 int ch, do_list = 0, create_symref = 0;
3411 const char *delref = NULL;
3413 /* TODO: Add -s option for adding symbolic references. */
3414 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3420 repo_path = realpath(optarg, NULL);
3421 if (repo_path == NULL)
3422 return got_error_from_errno2("realpath",
3424 got_path_strip_trailing_slashes(repo_path);
3438 if (do_list && delref)
3439 errx(1, "-l and -d options are mutually exclusive\n");
3444 if (do_list || delref) {
3446 errx(1, "-s option cannot be used together with the "
3447 "-l or -d options");
3450 } else if (argc != 2)
3455 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3459 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3460 "sendfd unveil", NULL) == -1)
3464 cwd = getcwd(NULL, 0);
3466 error = got_error_from_errno("getcwd");
3470 if (repo_path == NULL) {
3471 error = got_worktree_open(&worktree, cwd);
3472 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3478 strdup(got_worktree_get_repo_path(worktree));
3479 if (repo_path == NULL)
3480 error = got_error_from_errno("strdup");
3484 repo_path = strdup(cwd);
3485 if (repo_path == NULL) {
3486 error = got_error_from_errno("strdup");
3492 error = got_repo_open(&repo, repo_path, NULL);
3496 error = apply_unveil(got_repo_get_path(repo), do_list,
3497 worktree ? got_worktree_get_root_path(worktree) : NULL);
3502 error = list_refs(repo);
3504 error = delete_ref(repo, delref);
3505 else if (create_symref)
3506 error = add_symref(repo, argv[0], argv[1]);
3508 error = add_ref(repo, argv[0], argv[1]);
3511 got_repo_close(repo);
3513 got_worktree_close(worktree);
3523 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3524 "[name]\n", getprogname());
3528 static const struct got_error *
3529 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3530 struct got_reference *ref)
3532 const struct got_error *err = NULL;
3533 const char *refname, *marker = " ";
3536 refname = got_ref_get_name(ref);
3537 if (worktree && strcmp(refname,
3538 got_worktree_get_head_ref_name(worktree)) == 0) {
3539 struct got_object_id *id = NULL;
3541 err = got_ref_resolve(&id, repo, ref);
3544 if (got_object_id_cmp(id,
3545 got_worktree_get_base_commit_id(worktree)) == 0)
3552 if (strncmp(refname, "refs/heads/", 11) == 0)
3554 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3557 refstr = got_ref_to_str(ref);
3559 return got_error_from_errno("got_ref_to_str");
3561 printf("%s%s: %s\n", marker, refname, refstr);
3566 static const struct got_error *
3567 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3569 const char *refname;
3571 if (worktree == NULL)
3572 return got_error(GOT_ERR_NOT_WORKTREE);
3574 refname = got_worktree_get_head_ref_name(worktree);
3576 if (strncmp(refname, "refs/heads/", 11) == 0)
3578 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3581 printf("%s\n", refname);
3586 static const struct got_error *
3587 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3589 static const struct got_error *err = NULL;
3590 struct got_reflist_head refs;
3591 struct got_reflist_entry *re;
3592 struct got_reference *temp_ref = NULL;
3593 int rebase_in_progress, histedit_in_progress;
3595 SIMPLEQ_INIT(&refs);
3598 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3603 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3608 if (rebase_in_progress || histedit_in_progress) {
3609 err = got_ref_open(&temp_ref, repo,
3610 got_worktree_get_head_ref_name(worktree), 0);
3613 list_branch(repo, worktree, temp_ref);
3614 got_ref_close(temp_ref);
3618 err = got_ref_list(&refs, repo, "refs/heads",
3619 got_ref_cmp_by_name, NULL);
3623 SIMPLEQ_FOREACH(re, &refs, entry)
3624 list_branch(repo, worktree, re->ref);
3626 got_ref_list_free(&refs);
3630 static const struct got_error *
3631 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3632 const char *branch_name)
3634 const struct got_error *err = NULL;
3635 struct got_reference *ref = NULL;
3638 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3639 return got_error_from_errno("asprintf");
3641 err = got_ref_open(&ref, repo, refname, 0);
3646 strcmp(got_worktree_get_head_ref_name(worktree),
3647 got_ref_get_name(ref)) == 0) {
3648 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3649 "will not delete this work tree's current branch");
3653 err = got_ref_delete(ref, repo);
3661 static const struct got_error *
3662 add_branch(struct got_repository *repo, const char *branch_name,
3663 struct got_object_id *base_commit_id)
3665 const struct got_error *err = NULL;
3666 struct got_reference *ref = NULL;
3667 char *base_refname = NULL, *refname = NULL;
3670 * Don't let the user create a branch name with a leading '-'.
3671 * While technically a valid reference name, this case is usually
3672 * an unintended typo.
3674 if (branch_name[0] == '-')
3675 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3677 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3678 err = got_error_from_errno("asprintf");
3682 err = got_ref_open(&ref, repo, refname, 0);
3684 err = got_error(GOT_ERR_BRANCH_EXISTS);
3686 } else if (err->code != GOT_ERR_NOT_REF)
3689 err = got_ref_alloc(&ref, refname, base_commit_id);
3693 err = got_ref_write(ref, repo);
3702 static const struct got_error *
3703 cmd_branch(int argc, char *argv[])
3705 const struct got_error *error = NULL;
3706 struct got_repository *repo = NULL;
3707 struct got_worktree *worktree = NULL;
3708 char *cwd = NULL, *repo_path = NULL;
3709 int ch, do_list = 0, do_show = 0, do_update = 1;
3710 const char *delref = NULL, *commit_id_arg = NULL;
3711 struct got_reference *ref = NULL;
3712 struct got_pathlist_head paths;
3713 struct got_pathlist_entry *pe;
3714 struct got_object_id *commit_id = NULL;
3715 char *commit_id_str = NULL;
3719 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3722 commit_id_arg = optarg;
3728 repo_path = realpath(optarg, NULL);
3729 if (repo_path == NULL)
3730 return got_error_from_errno2("realpath",
3732 got_path_strip_trailing_slashes(repo_path);
3746 if (do_list && delref)
3747 errx(1, "-l and -d options are mutually exclusive\n");
3752 if (!do_list && !delref && argc == 0)
3755 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3756 errx(1, "-c option can only be used when creating a branch");
3758 if (do_list || delref) {
3761 } else if (!do_show && argc != 1)
3765 if (do_list || do_show) {
3766 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3770 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3771 "sendfd unveil", NULL) == -1)
3775 cwd = getcwd(NULL, 0);
3777 error = got_error_from_errno("getcwd");
3781 if (repo_path == NULL) {
3782 error = got_worktree_open(&worktree, cwd);
3783 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3789 strdup(got_worktree_get_repo_path(worktree));
3790 if (repo_path == NULL)
3791 error = got_error_from_errno("strdup");
3795 repo_path = strdup(cwd);
3796 if (repo_path == NULL) {
3797 error = got_error_from_errno("strdup");
3803 error = got_repo_open(&repo, repo_path, NULL);
3807 error = apply_unveil(got_repo_get_path(repo), do_list,
3808 worktree ? got_worktree_get_root_path(worktree) : NULL);
3813 error = show_current_branch(repo, worktree);
3815 error = list_branches(repo, worktree);
3817 error = delete_branch(repo, worktree, delref);
3819 if (commit_id_arg == NULL)
3820 commit_id_arg = worktree ?
3821 got_worktree_get_head_ref_name(worktree) :
3823 error = got_repo_match_object_id(&commit_id, NULL,
3824 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3827 error = add_branch(repo, argv[0], commit_id);
3830 if (worktree && do_update) {
3831 int did_something = 0;
3832 char *branch_refname = NULL;
3834 error = got_object_id_str(&commit_id_str, commit_id);
3837 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3841 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3843 error = got_error_from_errno("asprintf");
3846 error = got_ref_open(&ref, repo, branch_refname, 0);
3847 free(branch_refname);
3850 error = switch_head_ref(ref, commit_id, worktree,
3854 error = got_worktree_set_base_commit_id(worktree, repo,
3858 error = got_worktree_checkout_files(worktree, &paths,
3859 repo, update_progress, &did_something,
3860 check_cancelled, NULL);
3864 printf("Updated to commit %s\n", commit_id_str);
3871 got_repo_close(repo);
3873 got_worktree_close(worktree);
3877 free(commit_id_str);
3878 TAILQ_FOREACH(pe, &paths, entry)
3879 free((char *)pe->path);
3880 got_pathlist_free(&paths);
3889 "usage: %s tag [-c commit] [-r repository] [-l] "
3890 "[-m message] name\n", getprogname());
3895 static const struct got_error *
3896 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3898 const struct got_error *err = NULL;
3899 struct got_reflist_entry *re, *se, *new;
3900 struct got_object_id *re_id, *se_id;
3901 struct got_tag_object *re_tag, *se_tag;
3902 time_t re_time, se_time;
3904 SIMPLEQ_FOREACH(re, tags, entry) {
3905 se = SIMPLEQ_FIRST(sorted);
3907 err = got_reflist_entry_dup(&new, re);
3910 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3913 err = got_ref_resolve(&re_id, repo, re->ref);
3916 err = got_object_open_as_tag(&re_tag, repo, re_id);
3920 re_time = got_object_tag_get_tagger_time(re_tag);
3921 got_object_tag_close(re_tag);
3925 err = got_ref_resolve(&se_id, repo, re->ref);
3928 err = got_object_open_as_tag(&se_tag, repo, se_id);
3932 se_time = got_object_tag_get_tagger_time(se_tag);
3933 got_object_tag_close(se_tag);
3935 if (se_time > re_time) {
3936 err = got_reflist_entry_dup(&new, re);
3939 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3942 se = SIMPLEQ_NEXT(se, entry);
3951 static const struct got_error *
3952 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3954 static const struct got_error *err = NULL;
3955 struct got_reflist_head refs;
3956 struct got_reflist_entry *re;
3958 SIMPLEQ_INIT(&refs);
3960 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3964 SIMPLEQ_FOREACH(re, &refs, entry) {
3965 const char *refname;
3966 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3970 struct got_object_id *id;
3971 struct got_tag_object *tag;
3972 struct got_commit_object *commit = NULL;
3974 refname = got_ref_get_name(re->ref);
3975 if (strncmp(refname, "refs/tags/", 10) != 0)
3978 refstr = got_ref_to_str(re->ref);
3979 if (refstr == NULL) {
3980 err = got_error_from_errno("got_ref_to_str");
3983 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3986 err = got_ref_resolve(&id, repo, re->ref);
3989 err = got_object_open_as_tag(&tag, repo, id);
3991 if (err->code != GOT_ERR_OBJ_TYPE) {
3995 /* "lightweight" tag */
3996 err = got_object_open_as_commit(&commit, repo, id);
4001 tagger = got_object_commit_get_committer(commit);
4003 got_object_commit_get_committer_time(commit);
4004 err = got_object_id_str(&id_str, id);
4010 tagger = got_object_tag_get_tagger(tag);
4011 tagger_time = got_object_tag_get_tagger_time(tag);
4012 err = got_object_id_str(&id_str,
4013 got_object_tag_get_object_id(tag));
4017 printf("from: %s\n", tagger);
4018 datestr = get_datestr(&tagger_time, datebuf);
4020 printf("date: %s UTC\n", datestr);
4022 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4024 switch (got_object_tag_get_object_type(tag)) {
4025 case GOT_OBJ_TYPE_BLOB:
4026 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4029 case GOT_OBJ_TYPE_TREE:
4030 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4033 case GOT_OBJ_TYPE_COMMIT:
4034 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4037 case GOT_OBJ_TYPE_TAG:
4038 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4047 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4050 got_object_commit_close(commit);
4052 tagmsg0 = strdup(got_object_tag_get_message(tag));
4053 got_object_tag_close(tag);
4054 if (tagmsg0 == NULL) {
4055 err = got_error_from_errno("strdup");
4062 line = strsep(&tagmsg, "\n");
4064 printf(" %s\n", line);
4069 got_ref_list_free(&refs);
4073 static const struct got_error *
4074 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4075 const char *tag_name, const char *repo_path)
4077 const struct got_error *err = NULL;
4078 char *template = NULL, *initial_content = NULL;
4079 char *editor = NULL;
4082 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4083 err = got_error_from_errno("asprintf");
4087 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4088 commit_id_str, tag_name) == -1) {
4089 err = got_error_from_errno("asprintf");
4093 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4097 dprintf(fd, initial_content);
4100 err = get_editor(&editor);
4103 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4105 free(initial_content);
4109 /* Editor is done; we can now apply unveil(2) */
4111 err = apply_unveil(repo_path, 0, NULL);
4120 static const struct got_error *
4121 add_tag(struct got_repository *repo, const char *tag_name,
4122 const char *commit_arg, const char *tagmsg_arg)
4124 const struct got_error *err = NULL;
4125 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4126 char *label = NULL, *commit_id_str = NULL;
4127 struct got_reference *ref = NULL;
4128 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4129 char *tagmsg_path = NULL, *tag_id_str = NULL;
4130 int preserve_tagmsg = 0;
4133 * Don't let the user create a tag name with a leading '-'.
4134 * While technically a valid reference name, this case is usually
4135 * an unintended typo.
4137 if (tag_name[0] == '-')
4138 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4140 err = get_author(&tagger, repo);
4144 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4145 GOT_OBJ_TYPE_COMMIT, 1, repo);
4149 err = got_object_id_str(&commit_id_str, commit_id);
4153 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4154 refname = strdup(tag_name);
4155 if (refname == NULL) {
4156 err = got_error_from_errno("strdup");
4160 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4161 err = got_error_from_errno("asprintf");
4165 err = got_ref_open(&ref, repo, refname, 0);
4167 err = got_error(GOT_ERR_TAG_EXISTS);
4169 } else if (err->code != GOT_ERR_NOT_REF)
4172 if (tagmsg_arg == NULL) {
4173 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4174 tag_name, got_repo_get_path(repo));
4176 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4177 tagmsg_path != NULL)
4178 preserve_tagmsg = 1;
4183 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4184 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4187 preserve_tagmsg = 1;
4191 err = got_ref_alloc(&ref, refname, tag_id);
4194 preserve_tagmsg = 1;
4198 err = got_ref_write(ref, repo);
4201 preserve_tagmsg = 1;
4205 err = got_object_id_str(&tag_id_str, tag_id);
4208 preserve_tagmsg = 1;
4211 printf("Created tag %s\n", tag_id_str);
4213 if (preserve_tagmsg) {
4214 fprintf(stderr, "%s: tag message preserved in %s\n",
4215 getprogname(), tagmsg_path);
4216 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4217 err = got_error_from_errno2("unlink", tagmsg_path);
4222 free(commit_id_str);
4230 static const struct got_error *
4231 cmd_tag(int argc, char *argv[])
4233 const struct got_error *error = NULL;
4234 struct got_repository *repo = NULL;
4235 struct got_worktree *worktree = NULL;
4236 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4237 char *gitconfig_path = NULL;
4238 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4239 int ch, do_list = 0;
4241 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4244 commit_id_arg = optarg;
4250 repo_path = realpath(optarg, NULL);
4251 if (repo_path == NULL)
4252 return got_error_from_errno2("realpath",
4254 got_path_strip_trailing_slashes(repo_path);
4269 if (commit_id_arg != NULL)
4270 errx(1, "-c option can only be used when creating a tag");
4272 errx(1, "-l and -m options are mutually exclusive");
4275 } else if (argc != 1)
4282 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4286 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4287 "sendfd unveil", NULL) == -1)
4291 cwd = getcwd(NULL, 0);
4293 error = got_error_from_errno("getcwd");
4297 if (repo_path == NULL) {
4298 error = got_worktree_open(&worktree, cwd);
4299 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4305 strdup(got_worktree_get_repo_path(worktree));
4306 if (repo_path == NULL)
4307 error = got_error_from_errno("strdup");
4311 repo_path = strdup(cwd);
4312 if (repo_path == NULL) {
4313 error = got_error_from_errno("strdup");
4320 error = got_repo_open(&repo, repo_path, NULL);
4323 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4326 error = list_tags(repo, worktree);
4328 error = get_gitconfig_path(&gitconfig_path);
4331 error = got_repo_open(&repo, repo_path, gitconfig_path);
4336 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4341 if (commit_id_arg == NULL) {
4342 struct got_reference *head_ref;
4343 struct got_object_id *commit_id;
4344 error = got_ref_open(&head_ref, repo,
4345 worktree ? got_worktree_get_head_ref_name(worktree)
4349 error = got_ref_resolve(&commit_id, repo, head_ref);
4350 got_ref_close(head_ref);
4353 error = got_object_id_str(&commit_id_str, commit_id);
4359 error = add_tag(repo, tag_name,
4360 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4364 got_repo_close(repo);
4366 got_worktree_close(worktree);
4369 free(gitconfig_path);
4370 free(commit_id_str);
4377 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4382 static const struct got_error *
4383 add_progress(void *arg, unsigned char status, const char *path)
4385 while (path[0] == '/')
4387 printf("%c %s\n", status, path);
4391 static const struct got_error *
4392 cmd_add(int argc, char *argv[])
4394 const struct got_error *error = NULL;
4395 struct got_repository *repo = NULL;
4396 struct got_worktree *worktree = NULL;
4398 struct got_pathlist_head paths;
4399 struct got_pathlist_entry *pe;
4400 int ch, can_recurse = 0, no_ignores = 0;
4404 while ((ch = getopt(argc, argv, "IR")) != -1) {
4422 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4429 cwd = getcwd(NULL, 0);
4431 error = got_error_from_errno("getcwd");
4435 error = got_worktree_open(&worktree, cwd);
4439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4444 error = apply_unveil(got_repo_get_path(repo), 1,
4445 got_worktree_get_root_path(worktree));
4449 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4453 if (!can_recurse && no_ignores) {
4454 error = got_error_msg(GOT_ERR_BAD_PATH,
4455 "disregarding ignores requires -R option");
4463 TAILQ_FOREACH(pe, &paths, entry) {
4464 if (asprintf(&ondisk_path, "%s/%s",
4465 got_worktree_get_root_path(worktree),
4467 error = got_error_from_errno("asprintf");
4470 if (lstat(ondisk_path, &sb) == -1) {
4471 if (errno == ENOENT) {
4475 error = got_error_from_errno2("lstat",
4481 if (S_ISDIR(sb.st_mode)) {
4482 error = got_error_msg(GOT_ERR_BAD_PATH,
4483 "adding directories requires -R option");
4489 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4490 NULL, repo, no_ignores);
4493 got_repo_close(repo);
4495 got_worktree_close(worktree);
4496 TAILQ_FOREACH(pe, &paths, entry)
4497 free((char *)pe->path);
4498 got_pathlist_free(&paths);
4506 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4511 static const struct got_error *
4512 print_remove_status(void *arg, unsigned char status,
4513 unsigned char staged_status, const char *path)
4515 while (path[0] == '/')
4517 if (status == GOT_STATUS_NONEXISTENT)
4519 if (status == staged_status && (status == GOT_STATUS_DELETE))
4520 status = GOT_STATUS_NO_CHANGE;
4521 printf("%c%c %s\n", status, staged_status, path);
4525 static const struct got_error *
4526 cmd_remove(int argc, char *argv[])
4528 const struct got_error *error = NULL;
4529 struct got_worktree *worktree = NULL;
4530 struct got_repository *repo = NULL;
4532 struct got_pathlist_head paths;
4533 struct got_pathlist_entry *pe;
4534 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4538 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4541 delete_local_mods = 1;
4559 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4566 cwd = getcwd(NULL, 0);
4568 error = got_error_from_errno("getcwd");
4571 error = got_worktree_open(&worktree, cwd);
4575 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4580 error = apply_unveil(got_repo_get_path(repo), 1,
4581 got_worktree_get_root_path(worktree));
4585 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4592 TAILQ_FOREACH(pe, &paths, entry) {
4593 if (asprintf(&ondisk_path, "%s/%s",
4594 got_worktree_get_root_path(worktree),
4596 error = got_error_from_errno("asprintf");
4599 if (lstat(ondisk_path, &sb) == -1) {
4600 if (errno == ENOENT) {
4604 error = got_error_from_errno2("lstat",
4610 if (S_ISDIR(sb.st_mode)) {
4611 error = got_error_msg(GOT_ERR_BAD_PATH,
4612 "removing directories requires -R option");
4618 error = got_worktree_schedule_delete(worktree, &paths,
4619 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4622 got_repo_close(repo);
4624 got_worktree_close(worktree);
4625 TAILQ_FOREACH(pe, &paths, entry)
4626 free((char *)pe->path);
4627 got_pathlist_free(&paths);
4635 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4636 "path ...\n", getprogname());
4640 static const struct got_error *
4641 revert_progress(void *arg, unsigned char status, const char *path)
4643 if (status == GOT_STATUS_UNVERSIONED)
4646 while (path[0] == '/')
4648 printf("%c %s\n", status, path);
4652 struct choose_patch_arg {
4653 FILE *patch_script_file;
4657 static const struct got_error *
4658 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4659 int nchanges, const char *action)
4662 size_t linesize = 0;
4666 case GOT_STATUS_ADD:
4667 printf("A %s\n%s this addition? [y/n] ", path, action);
4669 case GOT_STATUS_DELETE:
4670 printf("D %s\n%s this deletion? [y/n] ", path, action);
4672 case GOT_STATUS_MODIFY:
4673 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4674 return got_error_from_errno("fseek");
4675 printf(GOT_COMMIT_SEP_STR);
4676 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4678 if (ferror(patch_file))
4679 return got_error_from_errno("getline");
4680 printf(GOT_COMMIT_SEP_STR);
4681 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4682 path, n, nchanges, action);
4685 return got_error_path(path, GOT_ERR_FILE_STATUS);
4691 static const struct got_error *
4692 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4693 FILE *patch_file, int n, int nchanges)
4695 const struct got_error *err = NULL;
4697 size_t linesize = 0;
4700 struct choose_patch_arg *a = arg;
4702 *choice = GOT_PATCH_CHOICE_NONE;
4704 if (a->patch_script_file) {
4706 err = show_change(status, path, patch_file, n, nchanges,
4710 linelen = getline(&line, &linesize, a->patch_script_file);
4711 if (linelen == -1) {
4712 if (ferror(a->patch_script_file))
4713 return got_error_from_errno("getline");
4716 nl = strchr(line, '\n');
4719 if (strcmp(line, "y") == 0) {
4720 *choice = GOT_PATCH_CHOICE_YES;
4722 } else if (strcmp(line, "n") == 0) {
4723 *choice = GOT_PATCH_CHOICE_NO;
4725 } else if (strcmp(line, "q") == 0 &&
4726 status == GOT_STATUS_MODIFY) {
4727 *choice = GOT_PATCH_CHOICE_QUIT;
4730 printf("invalid response '%s'\n", line);
4735 while (resp != 'y' && resp != 'n' && resp != 'q') {
4736 err = show_change(status, path, patch_file, n, nchanges,
4743 if (status == GOT_STATUS_MODIFY) {
4744 if (resp != 'y' && resp != 'n' && resp != 'q') {
4745 printf("invalid response '%c'\n", resp);
4748 } else if (resp != 'y' && resp != 'n') {
4749 printf("invalid response '%c'\n", resp);
4755 *choice = GOT_PATCH_CHOICE_YES;
4756 else if (resp == 'n')
4757 *choice = GOT_PATCH_CHOICE_NO;
4758 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4759 *choice = GOT_PATCH_CHOICE_QUIT;
4765 static const struct got_error *
4766 cmd_revert(int argc, char *argv[])
4768 const struct got_error *error = NULL;
4769 struct got_worktree *worktree = NULL;
4770 struct got_repository *repo = NULL;
4771 char *cwd = NULL, *path = NULL;
4772 struct got_pathlist_head paths;
4773 struct got_pathlist_entry *pe;
4774 int ch, can_recurse = 0, pflag = 0;
4775 FILE *patch_script_file = NULL;
4776 const char *patch_script_path = NULL;
4777 struct choose_patch_arg cpa;
4781 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4787 patch_script_path = optarg;
4802 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4803 "unveil", NULL) == -1)
4808 if (patch_script_path && !pflag)
4809 errx(1, "-F option can only be used together with -p option");
4811 cwd = getcwd(NULL, 0);
4813 error = got_error_from_errno("getcwd");
4816 error = got_worktree_open(&worktree, cwd);
4820 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4825 if (patch_script_path) {
4826 patch_script_file = fopen(patch_script_path, "r");
4827 if (patch_script_file == NULL) {
4828 error = got_error_from_errno2("fopen",
4833 error = apply_unveil(got_repo_get_path(repo), 1,
4834 got_worktree_get_root_path(worktree));
4838 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4845 TAILQ_FOREACH(pe, &paths, entry) {
4846 if (asprintf(&ondisk_path, "%s/%s",
4847 got_worktree_get_root_path(worktree),
4849 error = got_error_from_errno("asprintf");
4852 if (lstat(ondisk_path, &sb) == -1) {
4853 if (errno == ENOENT) {
4857 error = got_error_from_errno2("lstat",
4863 if (S_ISDIR(sb.st_mode)) {
4864 error = got_error_msg(GOT_ERR_BAD_PATH,
4865 "reverting directories requires -R option");
4871 cpa.patch_script_file = patch_script_file;
4872 cpa.action = "revert";
4873 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4874 pflag ? choose_patch : NULL, &cpa, repo);
4876 if (patch_script_file && fclose(patch_script_file) == EOF &&
4878 error = got_error_from_errno2("fclose", patch_script_path);
4880 got_repo_close(repo);
4882 got_worktree_close(worktree);
4891 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4896 struct collect_commit_logmsg_arg {
4897 const char *cmdline_log;
4899 const char *worktree_path;
4900 const char *branch_name;
4901 const char *repo_path;
4906 static const struct got_error *
4907 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4910 char *initial_content = NULL;
4911 struct got_pathlist_entry *pe;
4912 const struct got_error *err = NULL;
4913 char *template = NULL;
4914 struct collect_commit_logmsg_arg *a = arg;
4918 /* if a message was specified on the command line, just use it */
4919 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4920 len = strlen(a->cmdline_log) + 1;
4921 *logmsg = malloc(len + 1);
4922 if (*logmsg == NULL)
4923 return got_error_from_errno("malloc");
4924 strlcpy(*logmsg, a->cmdline_log, len);
4928 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4929 return got_error_from_errno("asprintf");
4931 if (asprintf(&initial_content,
4932 "\n# changes to be committed on branch %s:\n",
4933 a->branch_name) == -1)
4934 return got_error_from_errno("asprintf");
4936 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4940 dprintf(fd, initial_content);
4942 TAILQ_FOREACH(pe, commitable_paths, entry) {
4943 struct got_commitable *ct = pe->data;
4944 dprintf(fd, "# %c %s\n",
4945 got_commitable_get_status(ct),
4946 got_commitable_get_path(ct));
4950 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4952 free(initial_content);
4955 /* Editor is done; we can now apply unveil(2) */
4957 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4966 static const struct got_error *
4967 cmd_commit(int argc, char *argv[])
4969 const struct got_error *error = NULL;
4970 struct got_worktree *worktree = NULL;
4971 struct got_repository *repo = NULL;
4972 char *cwd = NULL, *id_str = NULL;
4973 struct got_object_id *id = NULL;
4974 const char *logmsg = NULL;
4975 struct collect_commit_logmsg_arg cl_arg;
4976 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4977 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4978 struct got_pathlist_head paths;
4981 cl_arg.logmsg_path = NULL;
4983 while ((ch = getopt(argc, argv, "m:")) != -1) {
4998 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4999 "unveil", NULL) == -1)
5002 cwd = getcwd(NULL, 0);
5004 error = got_error_from_errno("getcwd");
5007 error = got_worktree_open(&worktree, cwd);
5011 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5014 if (rebase_in_progress) {
5015 error = got_error(GOT_ERR_REBASING);
5019 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5024 error = get_gitconfig_path(&gitconfig_path);
5027 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5032 error = get_author(&author, repo);
5037 * unveil(2) traverses exec(2); if an editor is used we have
5038 * to apply unveil after the log message has been written.
5040 if (logmsg == NULL || strlen(logmsg) == 0)
5041 error = get_editor(&editor);
5043 error = apply_unveil(got_repo_get_path(repo), 0,
5044 got_worktree_get_root_path(worktree));
5048 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5052 cl_arg.editor = editor;
5053 cl_arg.cmdline_log = logmsg;
5054 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5055 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5056 if (!histedit_in_progress) {
5057 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5058 error = got_error(GOT_ERR_COMMIT_BRANCH);
5061 cl_arg.branch_name += 11;
5063 cl_arg.repo_path = got_repo_get_path(repo);
5064 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5065 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5067 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5068 cl_arg.logmsg_path != NULL)
5069 preserve_logmsg = 1;
5073 error = got_object_id_str(&id_str, id);
5076 printf("Created commit %s\n", id_str);
5078 if (preserve_logmsg) {
5079 fprintf(stderr, "%s: log message preserved in %s\n",
5080 getprogname(), cl_arg.logmsg_path);
5081 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5083 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5084 free(cl_arg.logmsg_path);
5086 got_repo_close(repo);
5088 got_worktree_close(worktree);
5091 free(gitconfig_path);
5098 usage_cherrypick(void)
5100 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5104 static const struct got_error *
5105 cmd_cherrypick(int argc, char *argv[])
5107 const struct got_error *error = NULL;
5108 struct got_worktree *worktree = NULL;
5109 struct got_repository *repo = NULL;
5110 char *cwd = NULL, *commit_id_str = NULL;
5111 struct got_object_id *commit_id = NULL;
5112 struct got_commit_object *commit = NULL;
5113 struct got_object_qid *pid;
5114 struct got_reference *head_ref = NULL;
5115 int ch, did_something = 0;
5117 while ((ch = getopt(argc, argv, "")) != -1) {
5129 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5130 "unveil", NULL) == -1)
5136 cwd = getcwd(NULL, 0);
5138 error = got_error_from_errno("getcwd");
5141 error = got_worktree_open(&worktree, cwd);
5145 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5150 error = apply_unveil(got_repo_get_path(repo), 0,
5151 got_worktree_get_root_path(worktree));
5155 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5156 GOT_OBJ_TYPE_COMMIT, repo);
5157 if (error != NULL) {
5158 struct got_reference *ref;
5159 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5161 error = got_ref_open(&ref, repo, argv[0], 0);
5164 error = got_ref_resolve(&commit_id, repo, ref);
5169 error = got_object_id_str(&commit_id_str, commit_id);
5173 error = got_ref_open(&head_ref, repo,
5174 got_worktree_get_head_ref_name(worktree), 0);
5178 error = check_same_branch(commit_id, head_ref, NULL, repo);
5180 if (error->code != GOT_ERR_ANCESTRY)
5184 error = got_error(GOT_ERR_SAME_BRANCH);
5188 error = got_object_open_as_commit(&commit, repo, commit_id);
5191 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5192 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5193 commit_id, repo, update_progress, &did_something, check_cancelled,
5199 printf("Merged commit %s\n", commit_id_str);
5202 got_object_commit_close(commit);
5203 free(commit_id_str);
5205 got_ref_close(head_ref);
5207 got_worktree_close(worktree);
5209 got_repo_close(repo);
5216 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5220 static const struct got_error *
5221 cmd_backout(int argc, char *argv[])
5223 const struct got_error *error = NULL;
5224 struct got_worktree *worktree = NULL;
5225 struct got_repository *repo = NULL;
5226 char *cwd = NULL, *commit_id_str = NULL;
5227 struct got_object_id *commit_id = NULL;
5228 struct got_commit_object *commit = NULL;
5229 struct got_object_qid *pid;
5230 struct got_reference *head_ref = NULL;
5231 int ch, did_something = 0;
5233 while ((ch = getopt(argc, argv, "")) != -1) {
5245 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5246 "unveil", NULL) == -1)
5252 cwd = getcwd(NULL, 0);
5254 error = got_error_from_errno("getcwd");
5257 error = got_worktree_open(&worktree, cwd);
5261 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5266 error = apply_unveil(got_repo_get_path(repo), 0,
5267 got_worktree_get_root_path(worktree));
5271 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5272 GOT_OBJ_TYPE_COMMIT, repo);
5273 if (error != NULL) {
5274 struct got_reference *ref;
5275 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5277 error = got_ref_open(&ref, repo, argv[0], 0);
5280 error = got_ref_resolve(&commit_id, repo, ref);
5285 error = got_object_id_str(&commit_id_str, commit_id);
5289 error = got_ref_open(&head_ref, repo,
5290 got_worktree_get_head_ref_name(worktree), 0);
5294 error = check_same_branch(commit_id, head_ref, NULL, repo);
5298 error = got_object_open_as_commit(&commit, repo, commit_id);
5301 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5303 error = got_error(GOT_ERR_ROOT_COMMIT);
5307 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5308 update_progress, &did_something, check_cancelled, NULL);
5313 printf("Backed out commit %s\n", commit_id_str);
5316 got_object_commit_close(commit);
5317 free(commit_id_str);
5319 got_ref_close(head_ref);
5321 got_worktree_close(worktree);
5323 got_repo_close(repo);
5330 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5336 trim_logmsg(char *logmsg, int limit)
5341 len = strlen(logmsg);
5345 nl = strchr(logmsg, '\n');
5350 static const struct got_error *
5351 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5353 const struct got_error *err;
5354 char *logmsg0 = NULL;
5357 err = got_object_commit_get_logmsg(&logmsg0, commit);
5362 while (isspace((unsigned char)s[0]))
5365 *logmsg = strdup(s);
5366 if (*logmsg == NULL) {
5367 err = got_error_from_errno("strdup");
5371 trim_logmsg(*logmsg, limit);
5377 static const struct got_error *
5378 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5380 const struct got_error *err;
5381 struct got_commit_object *commit = NULL;
5382 char *id_str = NULL, *logmsg = NULL;
5384 err = got_object_open_as_commit(&commit, repo, id);
5388 err = got_object_id_str(&id_str, id);
5394 err = get_short_logmsg(&logmsg, 42, commit);
5398 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5401 got_object_commit_close(commit);
5406 static const struct got_error *
5407 show_rebase_progress(struct got_commit_object *commit,
5408 struct got_object_id *old_id, struct got_object_id *new_id)
5410 const struct got_error *err;
5411 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5413 err = got_object_id_str(&old_id_str, old_id);
5418 err = got_object_id_str(&new_id_str, new_id);
5423 old_id_str[12] = '\0';
5425 new_id_str[12] = '\0';
5427 err = get_short_logmsg(&logmsg, 42, commit);
5431 printf("%s -> %s: %s\n", old_id_str,
5432 new_id_str ? new_id_str : "no-op change", logmsg);
5440 static const struct got_error *
5441 rebase_progress(void *arg, unsigned char status, const char *path)
5443 unsigned char *rebase_status = arg;
5445 while (path[0] == '/')
5447 printf("%c %s\n", status, path);
5449 if (*rebase_status == GOT_STATUS_CONFLICT)
5451 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5452 *rebase_status = status;
5456 static const struct got_error *
5457 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5458 struct got_reference *branch, struct got_reference *new_base_branch,
5459 struct got_reference *tmp_branch, struct got_repository *repo)
5461 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5462 return got_worktree_rebase_complete(worktree, fileindex,
5463 new_base_branch, tmp_branch, branch, repo);
5466 static const struct got_error *
5467 rebase_commit(struct got_pathlist_head *merged_paths,
5468 struct got_worktree *worktree, struct got_fileindex *fileindex,
5469 struct got_reference *tmp_branch,
5470 struct got_object_id *commit_id, struct got_repository *repo)
5472 const struct got_error *error;
5473 struct got_commit_object *commit;
5474 struct got_object_id *new_commit_id;
5476 error = got_object_open_as_commit(&commit, repo, commit_id);
5480 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5481 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5483 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5485 error = show_rebase_progress(commit, commit_id, NULL);
5487 error = show_rebase_progress(commit, commit_id, new_commit_id);
5488 free(new_commit_id);
5491 got_object_commit_close(commit);
5495 struct check_path_prefix_arg {
5496 const char *path_prefix;
5501 static const struct got_error *
5502 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5503 struct got_blob_object *blob2, struct got_object_id *id1,
5504 struct got_object_id *id2, const char *path1, const char *path2,
5505 mode_t mode1, mode_t mode2, struct got_repository *repo)
5507 struct check_path_prefix_arg *a = arg;
5509 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5510 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5511 return got_error(a->errcode);
5516 static const struct got_error *
5517 check_path_prefix(struct got_object_id *parent_id,
5518 struct got_object_id *commit_id, const char *path_prefix,
5519 int errcode, struct got_repository *repo)
5521 const struct got_error *err;
5522 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5523 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5524 struct check_path_prefix_arg cpp_arg;
5526 if (got_path_is_root_dir(path_prefix))
5529 err = got_object_open_as_commit(&commit, repo, commit_id);
5533 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5537 err = got_object_open_as_tree(&tree1, repo,
5538 got_object_commit_get_tree_id(parent_commit));
5542 err = got_object_open_as_tree(&tree2, repo,
5543 got_object_commit_get_tree_id(commit));
5547 cpp_arg.path_prefix = path_prefix;
5548 while (cpp_arg.path_prefix[0] == '/')
5549 cpp_arg.path_prefix++;
5550 cpp_arg.len = strlen(cpp_arg.path_prefix);
5551 cpp_arg.errcode = errcode;
5552 err = got_diff_tree(tree1, tree2, "", "", repo,
5553 check_path_prefix_in_diff, &cpp_arg, 0);
5556 got_object_tree_close(tree1);
5558 got_object_tree_close(tree2);
5560 got_object_commit_close(commit);
5562 got_object_commit_close(parent_commit);
5566 static const struct got_error *
5567 collect_commits(struct got_object_id_queue *commits,
5568 struct got_object_id *initial_commit_id,
5569 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5570 const char *path_prefix, int path_prefix_errcode,
5571 struct got_repository *repo)
5573 const struct got_error *err = NULL;
5574 struct got_commit_graph *graph = NULL;
5575 struct got_object_id *parent_id = NULL;
5576 struct got_object_qid *qid;
5577 struct got_object_id *commit_id = initial_commit_id;
5579 err = got_commit_graph_open(&graph, "/", 1);
5583 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5584 check_cancelled, NULL);
5587 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5588 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5589 check_cancelled, NULL);
5591 if (err->code == GOT_ERR_ITER_COMPLETED) {
5592 err = got_error_msg(GOT_ERR_ANCESTRY,
5593 "ran out of commits to rebase before "
5594 "youngest common ancestor commit has "
5599 err = check_path_prefix(parent_id, commit_id,
5600 path_prefix, path_prefix_errcode, repo);
5604 err = got_object_qid_alloc(&qid, commit_id);
5607 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5608 commit_id = parent_id;
5612 got_commit_graph_close(graph);
5616 static const struct got_error *
5617 cmd_rebase(int argc, char *argv[])
5619 const struct got_error *error = NULL;
5620 struct got_worktree *worktree = NULL;
5621 struct got_repository *repo = NULL;
5622 struct got_fileindex *fileindex = NULL;
5624 struct got_reference *branch = NULL;
5625 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5626 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5627 struct got_object_id *resume_commit_id = NULL;
5628 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5629 struct got_commit_object *commit = NULL;
5630 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5631 int histedit_in_progress = 0;
5632 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5633 struct got_object_id_queue commits;
5634 struct got_pathlist_head merged_paths;
5635 const struct got_object_id_queue *parent_ids;
5636 struct got_object_qid *qid, *pid;
5638 SIMPLEQ_INIT(&commits);
5639 TAILQ_INIT(&merged_paths);
5641 while ((ch = getopt(argc, argv, "ac")) != -1) {
5647 continue_rebase = 1;
5659 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5660 "unveil", NULL) == -1)
5663 if (abort_rebase && continue_rebase)
5665 else if (abort_rebase || continue_rebase) {
5668 } else if (argc != 1)
5671 cwd = getcwd(NULL, 0);
5673 error = got_error_from_errno("getcwd");
5676 error = got_worktree_open(&worktree, cwd);
5680 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5685 error = apply_unveil(got_repo_get_path(repo), 0,
5686 got_worktree_get_root_path(worktree));
5690 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5694 if (histedit_in_progress) {
5695 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5699 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5705 if (!rebase_in_progress) {
5706 error = got_error(GOT_ERR_NOT_REBASING);
5709 error = got_worktree_rebase_continue(&resume_commit_id,
5710 &new_base_branch, &tmp_branch, &branch, &fileindex,
5714 printf("Switching work tree to %s\n",
5715 got_ref_get_symref_target(new_base_branch));
5716 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5717 new_base_branch, update_progress, &did_something);
5720 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5721 goto done; /* nothing else to do */
5724 if (continue_rebase) {
5725 if (!rebase_in_progress) {
5726 error = got_error(GOT_ERR_NOT_REBASING);
5729 error = got_worktree_rebase_continue(&resume_commit_id,
5730 &new_base_branch, &tmp_branch, &branch, &fileindex,
5735 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5736 resume_commit_id, repo);
5740 yca_id = got_object_id_dup(resume_commit_id);
5741 if (yca_id == NULL) {
5742 error = got_error_from_errno("got_object_id_dup");
5746 error = got_ref_open(&branch, repo, argv[0], 0);
5751 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5755 if (!continue_rebase) {
5756 struct got_object_id *base_commit_id;
5758 base_commit_id = got_worktree_get_base_commit_id(worktree);
5759 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5760 base_commit_id, branch_head_commit_id, repo,
5761 check_cancelled, NULL);
5764 if (yca_id == NULL) {
5765 error = got_error_msg(GOT_ERR_ANCESTRY,
5766 "specified branch shares no common ancestry "
5767 "with work tree's branch");
5771 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5773 if (error->code != GOT_ERR_ANCESTRY)
5777 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5778 "specified branch resolves to a commit which "
5779 "is already contained in work tree's branch");
5782 error = got_worktree_rebase_prepare(&new_base_branch,
5783 &tmp_branch, &fileindex, worktree, branch, repo);
5788 commit_id = branch_head_commit_id;
5789 error = got_object_open_as_commit(&commit, repo, commit_id);
5793 parent_ids = got_object_commit_get_parent_ids(commit);
5794 pid = SIMPLEQ_FIRST(parent_ids);
5796 if (!continue_rebase) {
5798 error = got_worktree_rebase_abort(worktree, fileindex,
5799 repo, new_base_branch, update_progress,
5803 printf("Rebase of %s aborted\n",
5804 got_ref_get_name(branch));
5806 error = got_error(GOT_ERR_EMPTY_REBASE);
5809 error = collect_commits(&commits, commit_id, pid->id,
5810 yca_id, got_worktree_get_path_prefix(worktree),
5811 GOT_ERR_REBASE_PATH, repo);
5812 got_object_commit_close(commit);
5817 if (SIMPLEQ_EMPTY(&commits)) {
5818 if (continue_rebase) {
5819 error = rebase_complete(worktree, fileindex,
5820 branch, new_base_branch, tmp_branch, repo);
5823 /* Fast-forward the reference of the branch. */
5824 struct got_object_id *new_head_commit_id;
5826 error = got_ref_resolve(&new_head_commit_id, repo,
5830 error = got_object_id_str(&id_str, new_head_commit_id);
5831 printf("Forwarding %s to commit %s\n",
5832 got_ref_get_name(branch), id_str);
5834 error = got_ref_change_ref(branch,
5835 new_head_commit_id);
5842 SIMPLEQ_FOREACH(qid, &commits, entry) {
5843 commit_id = qid->id;
5844 parent_id = pid ? pid->id : yca_id;
5847 error = got_worktree_rebase_merge_files(&merged_paths,
5848 worktree, fileindex, parent_id, commit_id, repo,
5849 rebase_progress, &rebase_status, check_cancelled, NULL);
5853 if (rebase_status == GOT_STATUS_CONFLICT) {
5854 error = show_rebase_merge_conflict(qid->id, repo);
5857 got_worktree_rebase_pathlist_free(&merged_paths);
5861 error = rebase_commit(&merged_paths, worktree, fileindex,
5862 tmp_branch, commit_id, repo);
5863 got_worktree_rebase_pathlist_free(&merged_paths);
5868 if (rebase_status == GOT_STATUS_CONFLICT) {
5869 error = got_worktree_rebase_postpone(worktree, fileindex);
5872 error = got_error_msg(GOT_ERR_CONFLICTS,
5873 "conflicts must be resolved before rebasing can continue");
5875 error = rebase_complete(worktree, fileindex, branch,
5876 new_base_branch, tmp_branch, repo);
5878 got_object_id_queue_free(&commits);
5879 free(branch_head_commit_id);
5880 free(resume_commit_id);
5883 got_object_commit_close(commit);
5885 got_ref_close(branch);
5886 if (new_base_branch)
5887 got_ref_close(new_base_branch);
5889 got_ref_close(tmp_branch);
5891 got_worktree_close(worktree);
5893 got_repo_close(repo);
5898 usage_histedit(void)
5900 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5905 #define GOT_HISTEDIT_PICK 'p'
5906 #define GOT_HISTEDIT_EDIT 'e'
5907 #define GOT_HISTEDIT_FOLD 'f'
5908 #define GOT_HISTEDIT_DROP 'd'
5909 #define GOT_HISTEDIT_MESG 'm'
5911 static struct got_histedit_cmd {
5915 } got_histedit_cmds[] = {
5916 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5917 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5918 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5920 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5921 { GOT_HISTEDIT_MESG, "mesg",
5922 "single-line log message for commit above (open editor if empty)" },
5925 struct got_histedit_list_entry {
5926 TAILQ_ENTRY(got_histedit_list_entry) entry;
5927 struct got_object_id *commit_id;
5928 const struct got_histedit_cmd *cmd;
5931 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5933 static const struct got_error *
5934 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5935 FILE *f, struct got_repository *repo)
5937 const struct got_error *err = NULL;
5938 char *logmsg = NULL, *id_str = NULL;
5939 struct got_commit_object *commit = NULL;
5942 err = got_object_open_as_commit(&commit, repo, commit_id);
5946 err = get_short_logmsg(&logmsg, 34, commit);
5950 err = got_object_id_str(&id_str, commit_id);
5954 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5956 err = got_ferror(f, GOT_ERR_IO);
5959 got_object_commit_close(commit);
5965 static const struct got_error *
5966 histedit_write_commit_list(struct got_object_id_queue *commits,
5967 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5969 const struct got_error *err = NULL;
5970 struct got_object_qid *qid;
5972 if (SIMPLEQ_EMPTY(commits))
5973 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5975 SIMPLEQ_FOREACH(qid, commits, entry) {
5976 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5980 if (edit_logmsg_only) {
5981 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5983 err = got_ferror(f, GOT_ERR_IO);
5992 static const struct got_error *
5993 write_cmd_list(FILE *f, const char *branch_name,
5994 struct got_object_id_queue *commits)
5996 const struct got_error *err = NULL;
5999 struct got_object_qid *qid;
6001 qid = SIMPLEQ_FIRST(commits);
6002 err = got_object_id_str(&id_str, qid->id);
6007 "# Editing the history of branch '%s' starting at\n"
6009 "# Commits will be processed in order from top to "
6010 "bottom of this file.\n", branch_name, id_str);
6012 err = got_ferror(f, GOT_ERR_IO);
6016 n = fprintf(f, "# Available histedit commands:\n");
6018 err = got_ferror(f, GOT_ERR_IO);
6022 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6023 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6024 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6027 err = got_ferror(f, GOT_ERR_IO);
6036 static const struct got_error *
6037 histedit_syntax_error(int lineno)
6039 static char msg[42];
6042 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6044 if (ret == -1 || ret >= sizeof(msg))
6045 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6047 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6050 static const struct got_error *
6051 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6052 char *logmsg, struct got_repository *repo)
6054 const struct got_error *err;
6055 struct got_commit_object *folded_commit = NULL;
6056 char *id_str, *folded_logmsg = NULL;
6058 err = got_object_id_str(&id_str, hle->commit_id);
6062 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6066 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6069 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6070 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6071 folded_logmsg) == -1) {
6072 err = got_error_from_errno("asprintf");
6076 got_object_commit_close(folded_commit);
6078 free(folded_logmsg);
6082 static struct got_histedit_list_entry *
6083 get_folded_commits(struct got_histedit_list_entry *hle)
6085 struct got_histedit_list_entry *prev, *folded = NULL;
6087 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6088 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6089 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6090 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6092 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6098 static const struct got_error *
6099 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6100 struct got_repository *repo)
6102 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6103 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6104 const struct got_error *err = NULL;
6105 struct got_commit_object *commit = NULL;
6107 struct got_histedit_list_entry *folded = NULL;
6109 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6113 folded = get_folded_commits(hle);
6115 while (folded != hle) {
6116 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6117 folded = TAILQ_NEXT(folded, entry);
6120 err = append_folded_commit_msg(&new_msg, folded,
6126 folded = TAILQ_NEXT(folded, entry);
6130 err = got_object_id_str(&id_str, hle->commit_id);
6133 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6136 if (asprintf(&new_msg,
6137 "%s\n# original log message of commit %s: %s",
6138 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6139 err = got_error_from_errno("asprintf");
6145 err = got_object_id_str(&id_str, hle->commit_id);
6149 err = got_opentemp_named_fd(&logmsg_path, &fd,
6150 GOT_TMPDIR_STR "/got-logmsg");
6154 dprintf(fd, logmsg);
6157 err = get_editor(&editor);
6161 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6163 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6165 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6168 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6169 err = got_error_from_errno2("unlink", logmsg_path);
6175 got_object_commit_close(commit);
6179 static const struct got_error *
6180 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6181 FILE *f, struct got_repository *repo)
6183 const struct got_error *err = NULL;
6184 char *line = NULL, *p, *end;
6188 const struct got_histedit_cmd *cmd;
6189 struct got_object_id *commit_id = NULL;
6190 struct got_histedit_list_entry *hle = NULL;
6193 len = getline(&line, &size, f);
6195 const struct got_error *getline_err;
6198 getline_err = got_error_from_errno("getline");
6199 err = got_ferror(f, getline_err->code);
6204 while (isspace((unsigned char)p[0]))
6206 if (p[0] == '#' || p[0] == '\0') {
6212 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6213 cmd = &got_histedit_cmds[i];
6214 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6215 isspace((unsigned char)p[strlen(cmd->name)])) {
6216 p += strlen(cmd->name);
6219 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6224 if (i == nitems(got_histedit_cmds)) {
6225 err = histedit_syntax_error(lineno);
6228 while (isspace((unsigned char)p[0]))
6230 if (cmd->code == GOT_HISTEDIT_MESG) {
6231 if (hle == NULL || hle->logmsg != NULL) {
6232 err = got_error(GOT_ERR_HISTEDIT_CMD);
6236 err = histedit_edit_logmsg(hle, repo);
6240 hle->logmsg = strdup(p);
6241 if (hle->logmsg == NULL) {
6242 err = got_error_from_errno("strdup");
6251 while (end[0] && !isspace((unsigned char)end[0]))
6255 err = got_object_resolve_id_str(&commit_id, repo, p);
6257 /* override error code */
6258 err = histedit_syntax_error(lineno);
6262 hle = malloc(sizeof(*hle));
6264 err = got_error_from_errno("malloc");
6268 hle->commit_id = commit_id;
6273 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6281 static const struct got_error *
6282 histedit_check_script(struct got_histedit_list *histedit_cmds,
6283 struct got_object_id_queue *commits, struct got_repository *repo)
6285 const struct got_error *err = NULL;
6286 struct got_object_qid *qid;
6287 struct got_histedit_list_entry *hle;
6288 static char msg[92];
6291 if (TAILQ_EMPTY(histedit_cmds))
6292 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6293 "histedit script contains no commands");
6294 if (SIMPLEQ_EMPTY(commits))
6295 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6297 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6298 struct got_histedit_list_entry *hle2;
6299 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6302 if (got_object_id_cmp(hle->commit_id,
6303 hle2->commit_id) != 0)
6305 err = got_object_id_str(&id_str, hle->commit_id);
6308 snprintf(msg, sizeof(msg), "commit %s is listed "
6309 "more than once in histedit script", id_str);
6311 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6315 SIMPLEQ_FOREACH(qid, commits, entry) {
6316 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6317 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6321 err = got_object_id_str(&id_str, qid->id);
6324 snprintf(msg, sizeof(msg),
6325 "commit %s missing from histedit script", id_str);
6327 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6331 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6332 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6333 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6334 "last commit in histedit script cannot be folded");
6339 static const struct got_error *
6340 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6341 const char *path, struct got_object_id_queue *commits,
6342 struct got_repository *repo)
6344 const struct got_error *err = NULL;
6348 err = get_editor(&editor);
6352 if (spawn_editor(editor, path) == -1) {
6353 err = got_error_from_errno("failed spawning editor");
6357 f = fopen(path, "r");
6359 err = got_error_from_errno("fopen");
6362 err = histedit_parse_list(histedit_cmds, f, repo);
6366 err = histedit_check_script(histedit_cmds, commits, repo);
6368 if (f && fclose(f) != 0 && err == NULL)
6369 err = got_error_from_errno("fclose");
6374 static const struct got_error *
6375 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6376 struct got_object_id_queue *, const char *, const char *,
6377 struct got_repository *);
6379 static const struct got_error *
6380 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6381 struct got_object_id_queue *commits, const char *branch_name,
6382 int edit_logmsg_only, struct got_repository *repo)
6384 const struct got_error *err;
6388 err = got_opentemp_named(&path, &f, "got-histedit");
6392 err = write_cmd_list(f, branch_name, commits);
6396 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6400 if (edit_logmsg_only) {
6402 err = histedit_parse_list(histedit_cmds, f, repo);
6404 if (fclose(f) != 0) {
6405 err = got_error_from_errno("fclose");
6409 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6411 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6412 err->code != GOT_ERR_HISTEDIT_CMD)
6414 err = histedit_edit_list_retry(histedit_cmds, err,
6415 commits, path, branch_name, repo);
6419 if (f && fclose(f) != 0 && err == NULL)
6420 err = got_error_from_errno("fclose");
6421 if (path && unlink(path) != 0 && err == NULL)
6422 err = got_error_from_errno2("unlink", path);
6427 static const struct got_error *
6428 histedit_save_list(struct got_histedit_list *histedit_cmds,
6429 struct got_worktree *worktree, struct got_repository *repo)
6431 const struct got_error *err = NULL;
6434 struct got_histedit_list_entry *hle;
6435 struct got_commit_object *commit = NULL;
6437 err = got_worktree_get_histedit_script_path(&path, worktree);
6441 f = fopen(path, "w");
6443 err = got_error_from_errno2("fopen", path);
6446 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6447 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6453 int n = fprintf(f, "%c %s\n",
6454 GOT_HISTEDIT_MESG, hle->logmsg);
6456 err = got_ferror(f, GOT_ERR_IO);
6462 if (f && fclose(f) != 0 && err == NULL)
6463 err = got_error_from_errno("fclose");
6466 got_object_commit_close(commit);
6471 histedit_free_list(struct got_histedit_list *histedit_cmds)
6473 struct got_histedit_list_entry *hle;
6475 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6476 TAILQ_REMOVE(histedit_cmds, hle, entry);
6481 static const struct got_error *
6482 histedit_load_list(struct got_histedit_list *histedit_cmds,
6483 const char *path, struct got_repository *repo)
6485 const struct got_error *err = NULL;
6488 f = fopen(path, "r");
6490 err = got_error_from_errno2("fopen", path);
6494 err = histedit_parse_list(histedit_cmds, f, repo);
6496 if (f && fclose(f) != 0 && err == NULL)
6497 err = got_error_from_errno("fclose");
6501 static const struct got_error *
6502 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6503 const struct got_error *edit_err, struct got_object_id_queue *commits,
6504 const char *path, const char *branch_name, struct got_repository *repo)
6506 const struct got_error *err = NULL, *prev_err = edit_err;
6509 while (resp != 'c' && resp != 'r' && resp != 'a') {
6510 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6511 "or (a)bort: ", getprogname(), prev_err->msg);
6516 histedit_free_list(histedit_cmds);
6517 err = histedit_run_editor(histedit_cmds, path, commits,
6520 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6521 err->code != GOT_ERR_HISTEDIT_CMD)
6528 } else if (resp == 'r') {
6529 histedit_free_list(histedit_cmds);
6530 err = histedit_edit_script(histedit_cmds,
6531 commits, branch_name, 0, repo);
6533 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6534 err->code != GOT_ERR_HISTEDIT_CMD)
6541 } else if (resp == 'a') {
6542 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6545 printf("invalid response '%c'\n", resp);
6551 static const struct got_error *
6552 histedit_complete(struct got_worktree *worktree,
6553 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6554 struct got_reference *branch, struct got_repository *repo)
6556 printf("Switching work tree to %s\n",
6557 got_ref_get_symref_target(branch));
6558 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6562 static const struct got_error *
6563 show_histedit_progress(struct got_commit_object *commit,
6564 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6566 const struct got_error *err;
6567 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6569 err = got_object_id_str(&old_id_str, hle->commit_id);
6574 err = got_object_id_str(&new_id_str, new_id);
6579 old_id_str[12] = '\0';
6581 new_id_str[12] = '\0';
6584 logmsg = strdup(hle->logmsg);
6585 if (logmsg == NULL) {
6586 err = got_error_from_errno("strdup");
6589 trim_logmsg(logmsg, 42);
6591 err = get_short_logmsg(&logmsg, 42, commit);
6596 switch (hle->cmd->code) {
6597 case GOT_HISTEDIT_PICK:
6598 case GOT_HISTEDIT_EDIT:
6599 printf("%s -> %s: %s\n", old_id_str,
6600 new_id_str ? new_id_str : "no-op change", logmsg);
6602 case GOT_HISTEDIT_DROP:
6603 case GOT_HISTEDIT_FOLD:
6604 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6616 static const struct got_error *
6617 histedit_commit(struct got_pathlist_head *merged_paths,
6618 struct got_worktree *worktree, struct got_fileindex *fileindex,
6619 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6620 struct got_repository *repo)
6622 const struct got_error *err;
6623 struct got_commit_object *commit;
6624 struct got_object_id *new_commit_id;
6626 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6627 && hle->logmsg == NULL) {
6628 err = histedit_edit_logmsg(hle, repo);
6633 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6637 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6638 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6641 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6643 err = show_histedit_progress(commit, hle, NULL);
6645 err = show_histedit_progress(commit, hle, new_commit_id);
6646 free(new_commit_id);
6649 got_object_commit_close(commit);
6653 static const struct got_error *
6654 histedit_skip_commit(struct got_histedit_list_entry *hle,
6655 struct got_worktree *worktree, struct got_repository *repo)
6657 const struct got_error *error;
6658 struct got_commit_object *commit;
6660 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6665 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6669 error = show_histedit_progress(commit, hle, NULL);
6670 got_object_commit_close(commit);
6674 static const struct got_error *
6675 check_local_changes(void *arg, unsigned char status,
6676 unsigned char staged_status, const char *path,
6677 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6678 struct got_object_id *commit_id, int dirfd, const char *de_name)
6680 int *have_local_changes = arg;
6683 case GOT_STATUS_ADD:
6684 case GOT_STATUS_DELETE:
6685 case GOT_STATUS_MODIFY:
6686 case GOT_STATUS_CONFLICT:
6687 *have_local_changes = 1;
6688 return got_error(GOT_ERR_CANCELLED);
6693 switch (staged_status) {
6694 case GOT_STATUS_ADD:
6695 case GOT_STATUS_DELETE:
6696 case GOT_STATUS_MODIFY:
6697 *have_local_changes = 1;
6698 return got_error(GOT_ERR_CANCELLED);
6706 static const struct got_error *
6707 cmd_histedit(int argc, char *argv[])
6709 const struct got_error *error = NULL;
6710 struct got_worktree *worktree = NULL;
6711 struct got_fileindex *fileindex = NULL;
6712 struct got_repository *repo = NULL;
6714 struct got_reference *branch = NULL;
6715 struct got_reference *tmp_branch = NULL;
6716 struct got_object_id *resume_commit_id = NULL;
6717 struct got_object_id *base_commit_id = NULL;
6718 struct got_object_id *head_commit_id = NULL;
6719 struct got_commit_object *commit = NULL;
6720 int ch, rebase_in_progress = 0, did_something;
6721 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6722 int edit_logmsg_only = 0;
6723 const char *edit_script_path = NULL;
6724 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6725 struct got_object_id_queue commits;
6726 struct got_pathlist_head merged_paths;
6727 const struct got_object_id_queue *parent_ids;
6728 struct got_object_qid *pid;
6729 struct got_histedit_list histedit_cmds;
6730 struct got_histedit_list_entry *hle;
6732 SIMPLEQ_INIT(&commits);
6733 TAILQ_INIT(&histedit_cmds);
6734 TAILQ_INIT(&merged_paths);
6736 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6745 edit_script_path = optarg;
6748 edit_logmsg_only = 1;
6760 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6761 "unveil", NULL) == -1)
6764 if (abort_edit && continue_edit)
6765 errx(1, "histedit's -a and -c options are mutually exclusive");
6766 if (edit_script_path && edit_logmsg_only)
6767 errx(1, "histedit's -F and -m options are mutually exclusive");
6768 if (abort_edit && edit_logmsg_only)
6769 errx(1, "histedit's -a and -m options are mutually exclusive");
6770 if (continue_edit && edit_logmsg_only)
6771 errx(1, "histedit's -c and -m options are mutually exclusive");
6776 * This command cannot apply unveil(2) in all cases because the
6777 * user may choose to run an editor to edit the histedit script
6778 * and to edit individual commit log messages.
6779 * unveil(2) traverses exec(2); if an editor is used we have to
6780 * apply unveil after edit script and log messages have been written.
6781 * XXX TODO: Make use of unveil(2) where possible.
6784 cwd = getcwd(NULL, 0);
6786 error = got_error_from_errno("getcwd");
6789 error = got_worktree_open(&worktree, cwd);
6793 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6798 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6801 if (rebase_in_progress) {
6802 error = got_error(GOT_ERR_REBASING);
6806 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6810 if (edit_in_progress && edit_logmsg_only) {
6811 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6812 "histedit operation is in progress in this "
6813 "work tree and must be continued or aborted "
6814 "before the -m option can be used");
6818 if (edit_in_progress && abort_edit) {
6819 error = got_worktree_histedit_continue(&resume_commit_id,
6820 &tmp_branch, &branch, &base_commit_id, &fileindex,
6824 printf("Switching work tree to %s\n",
6825 got_ref_get_symref_target(branch));
6826 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6827 branch, base_commit_id, update_progress, &did_something);
6830 printf("Histedit of %s aborted\n",
6831 got_ref_get_symref_target(branch));
6832 goto done; /* nothing else to do */
6833 } else if (abort_edit) {
6834 error = got_error(GOT_ERR_NOT_HISTEDIT);
6838 if (continue_edit) {
6841 if (!edit_in_progress) {
6842 error = got_error(GOT_ERR_NOT_HISTEDIT);
6846 error = got_worktree_get_histedit_script_path(&path, worktree);
6850 error = histedit_load_list(&histedit_cmds, path, repo);
6855 error = got_worktree_histedit_continue(&resume_commit_id,
6856 &tmp_branch, &branch, &base_commit_id, &fileindex,
6861 error = got_ref_resolve(&head_commit_id, repo, branch);
6865 error = got_object_open_as_commit(&commit, repo,
6869 parent_ids = got_object_commit_get_parent_ids(commit);
6870 pid = SIMPLEQ_FIRST(parent_ids);
6872 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6875 error = collect_commits(&commits, head_commit_id, pid->id,
6876 base_commit_id, got_worktree_get_path_prefix(worktree),
6877 GOT_ERR_HISTEDIT_PATH, repo);
6878 got_object_commit_close(commit);
6883 if (edit_in_progress) {
6884 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6888 error = got_ref_open(&branch, repo,
6889 got_worktree_get_head_ref_name(worktree), 0);
6893 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6894 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6895 "will not edit commit history of a branch outside "
6896 "the \"refs/heads/\" reference namespace");
6900 error = got_ref_resolve(&head_commit_id, repo, branch);
6901 got_ref_close(branch);
6906 error = got_object_open_as_commit(&commit, repo,
6910 parent_ids = got_object_commit_get_parent_ids(commit);
6911 pid = SIMPLEQ_FIRST(parent_ids);
6913 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6916 error = collect_commits(&commits, head_commit_id, pid->id,
6917 got_worktree_get_base_commit_id(worktree),
6918 got_worktree_get_path_prefix(worktree),
6919 GOT_ERR_HISTEDIT_PATH, repo);
6920 got_object_commit_close(commit);
6925 if (SIMPLEQ_EMPTY(&commits)) {
6926 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6930 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6931 &base_commit_id, &fileindex, worktree, repo);
6935 if (edit_script_path) {
6936 error = histedit_load_list(&histedit_cmds,
6937 edit_script_path, repo);
6939 got_worktree_histedit_abort(worktree, fileindex,
6940 repo, branch, base_commit_id,
6941 update_progress, &did_something);
6945 const char *branch_name;
6946 branch_name = got_ref_get_symref_target(branch);
6947 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6949 error = histedit_edit_script(&histedit_cmds, &commits,
6950 branch_name, edit_logmsg_only, repo);
6952 got_worktree_histedit_abort(worktree, fileindex,
6953 repo, branch, base_commit_id,
6954 update_progress, &did_something);
6960 error = histedit_save_list(&histedit_cmds, worktree,
6963 got_worktree_histedit_abort(worktree, fileindex,
6964 repo, branch, base_commit_id,
6965 update_progress, &did_something);
6971 error = histedit_check_script(&histedit_cmds, &commits, repo);
6975 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6976 if (resume_commit_id) {
6977 if (got_object_id_cmp(hle->commit_id,
6978 resume_commit_id) != 0)
6981 resume_commit_id = NULL;
6982 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6983 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6984 error = histedit_skip_commit(hle, worktree,
6989 struct got_pathlist_head paths;
6990 int have_changes = 0;
6993 error = got_pathlist_append(&paths, "", NULL);
6996 error = got_worktree_status(worktree, &paths,
6997 repo, check_local_changes, &have_changes,
6998 check_cancelled, NULL);
6999 got_pathlist_free(&paths);
7001 if (error->code != GOT_ERR_CANCELLED)
7003 if (sigint_received || sigpipe_received)
7007 error = histedit_commit(NULL, worktree,
7008 fileindex, tmp_branch, hle, repo);
7012 error = got_object_open_as_commit(
7013 &commit, repo, hle->commit_id);
7016 error = show_histedit_progress(commit,
7018 got_object_commit_close(commit);
7027 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7028 error = histedit_skip_commit(hle, worktree, repo);
7034 error = got_object_open_as_commit(&commit, repo,
7038 parent_ids = got_object_commit_get_parent_ids(commit);
7039 pid = SIMPLEQ_FIRST(parent_ids);
7041 error = got_worktree_histedit_merge_files(&merged_paths,
7042 worktree, fileindex, pid->id, hle->commit_id, repo,
7043 rebase_progress, &rebase_status, check_cancelled, NULL);
7046 got_object_commit_close(commit);
7049 if (rebase_status == GOT_STATUS_CONFLICT) {
7050 error = show_rebase_merge_conflict(hle->commit_id,
7054 got_worktree_rebase_pathlist_free(&merged_paths);
7058 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7060 error = got_object_id_str(&id_str, hle->commit_id);
7063 printf("Stopping histedit for amending commit %s\n",
7066 got_worktree_rebase_pathlist_free(&merged_paths);
7067 error = got_worktree_histedit_postpone(worktree,
7072 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7073 error = histedit_skip_commit(hle, worktree, repo);
7079 error = histedit_commit(&merged_paths, worktree, fileindex,
7080 tmp_branch, hle, repo);
7081 got_worktree_rebase_pathlist_free(&merged_paths);
7086 if (rebase_status == GOT_STATUS_CONFLICT) {
7087 error = got_worktree_histedit_postpone(worktree, fileindex);
7090 error = got_error_msg(GOT_ERR_CONFLICTS,
7091 "conflicts must be resolved before histedit can continue");
7093 error = histedit_complete(worktree, fileindex, tmp_branch,
7096 got_object_id_queue_free(&commits);
7097 histedit_free_list(&histedit_cmds);
7098 free(head_commit_id);
7099 free(base_commit_id);
7100 free(resume_commit_id);
7102 got_object_commit_close(commit);
7104 got_ref_close(branch);
7106 got_ref_close(tmp_branch);
7108 got_worktree_close(worktree);
7110 got_repo_close(repo);
7115 usage_integrate(void)
7117 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7121 static const struct got_error *
7122 cmd_integrate(int argc, char *argv[])
7124 const struct got_error *error = NULL;
7125 struct got_repository *repo = NULL;
7126 struct got_worktree *worktree = NULL;
7127 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7128 const char *branch_arg = NULL;
7129 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7130 struct got_fileindex *fileindex = NULL;
7131 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7132 int ch, did_something = 0;
7134 while ((ch = getopt(argc, argv, "")) != -1) {
7147 branch_arg = argv[0];
7149 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7150 "unveil", NULL) == -1)
7153 cwd = getcwd(NULL, 0);
7155 error = got_error_from_errno("getcwd");
7159 error = got_worktree_open(&worktree, cwd);
7163 error = check_rebase_or_histedit_in_progress(worktree);
7167 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7172 error = apply_unveil(got_repo_get_path(repo), 0,
7173 got_worktree_get_root_path(worktree));
7177 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7178 error = got_error_from_errno("asprintf");
7182 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7183 &base_branch_ref, worktree, refname, repo);
7187 refname = strdup(got_ref_get_name(branch_ref));
7188 if (refname == NULL) {
7189 error = got_error_from_errno("strdup");
7190 got_worktree_integrate_abort(worktree, fileindex, repo,
7191 branch_ref, base_branch_ref);
7194 base_refname = strdup(got_ref_get_name(base_branch_ref));
7195 if (base_refname == NULL) {
7196 error = got_error_from_errno("strdup");
7197 got_worktree_integrate_abort(worktree, fileindex, repo,
7198 branch_ref, base_branch_ref);
7202 error = got_ref_resolve(&commit_id, repo, branch_ref);
7206 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7210 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7211 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7212 "specified branch has already been integrated");
7213 got_worktree_integrate_abort(worktree, fileindex, repo,
7214 branch_ref, base_branch_ref);
7218 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7220 if (error->code == GOT_ERR_ANCESTRY)
7221 error = got_error(GOT_ERR_REBASE_REQUIRED);
7222 got_worktree_integrate_abort(worktree, fileindex, repo,
7223 branch_ref, base_branch_ref);
7227 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7228 branch_ref, base_branch_ref, update_progress, &did_something,
7229 check_cancelled, NULL);
7233 printf("Integrated %s into %s\n", refname, base_refname);
7236 got_repo_close(repo);
7238 got_worktree_close(worktree);
7240 free(base_commit_id);
7250 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7251 "[file-path ...]\n",
7256 static const struct got_error *
7257 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7258 const char *path, struct got_object_id *blob_id,
7259 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7260 int dirfd, const char *de_name)
7262 const struct got_error *err = NULL;
7263 char *id_str = NULL;
7265 if (staged_status != GOT_STATUS_ADD &&
7266 staged_status != GOT_STATUS_MODIFY &&
7267 staged_status != GOT_STATUS_DELETE)
7270 if (staged_status == GOT_STATUS_ADD ||
7271 staged_status == GOT_STATUS_MODIFY)
7272 err = got_object_id_str(&id_str, staged_blob_id);
7274 err = got_object_id_str(&id_str, blob_id);
7278 printf("%s %c %s\n", id_str, staged_status, path);
7283 static const struct got_error *
7284 cmd_stage(int argc, char *argv[])
7286 const struct got_error *error = NULL;
7287 struct got_repository *repo = NULL;
7288 struct got_worktree *worktree = NULL;
7290 struct got_pathlist_head paths;
7291 struct got_pathlist_entry *pe;
7292 int ch, list_stage = 0, pflag = 0;
7293 FILE *patch_script_file = NULL;
7294 const char *patch_script_path = NULL;
7295 struct choose_patch_arg cpa;
7299 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7308 patch_script_path = optarg;
7320 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7321 "unveil", NULL) == -1)
7324 if (list_stage && (pflag || patch_script_path))
7325 errx(1, "-l option cannot be used with other options");
7326 if (patch_script_path && !pflag)
7327 errx(1, "-F option can only be used together with -p option");
7329 cwd = getcwd(NULL, 0);
7331 error = got_error_from_errno("getcwd");
7335 error = got_worktree_open(&worktree, cwd);
7339 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7344 if (patch_script_path) {
7345 patch_script_file = fopen(patch_script_path, "r");
7346 if (patch_script_file == NULL) {
7347 error = got_error_from_errno2("fopen",
7352 error = apply_unveil(got_repo_get_path(repo), 0,
7353 got_worktree_get_root_path(worktree));
7357 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7362 error = got_worktree_status(worktree, &paths, repo,
7363 print_stage, NULL, check_cancelled, NULL);
7365 cpa.patch_script_file = patch_script_file;
7366 cpa.action = "stage";
7367 error = got_worktree_stage(worktree, &paths,
7368 pflag ? NULL : print_status, NULL,
7369 pflag ? choose_patch : NULL, &cpa, repo);
7372 if (patch_script_file && fclose(patch_script_file) == EOF &&
7374 error = got_error_from_errno2("fclose", patch_script_path);
7376 got_repo_close(repo);
7378 got_worktree_close(worktree);
7379 TAILQ_FOREACH(pe, &paths, entry)
7380 free((char *)pe->path);
7381 got_pathlist_free(&paths);
7389 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7390 "[file-path ...]\n",
7396 static const struct got_error *
7397 cmd_unstage(int argc, char *argv[])
7399 const struct got_error *error = NULL;
7400 struct got_repository *repo = NULL;
7401 struct got_worktree *worktree = NULL;
7403 struct got_pathlist_head paths;
7404 struct got_pathlist_entry *pe;
7405 int ch, did_something = 0, pflag = 0;
7406 FILE *patch_script_file = NULL;
7407 const char *patch_script_path = NULL;
7408 struct choose_patch_arg cpa;
7412 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7418 patch_script_path = optarg;
7430 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7431 "unveil", NULL) == -1)
7434 if (patch_script_path && !pflag)
7435 errx(1, "-F option can only be used together with -p option");
7437 cwd = getcwd(NULL, 0);
7439 error = got_error_from_errno("getcwd");
7443 error = got_worktree_open(&worktree, cwd);
7447 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7452 if (patch_script_path) {
7453 patch_script_file = fopen(patch_script_path, "r");
7454 if (patch_script_file == NULL) {
7455 error = got_error_from_errno2("fopen",
7461 error = apply_unveil(got_repo_get_path(repo), 0,
7462 got_worktree_get_root_path(worktree));
7466 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7470 cpa.patch_script_file = patch_script_file;
7471 cpa.action = "unstage";
7472 error = got_worktree_unstage(worktree, &paths, update_progress,
7473 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7475 if (patch_script_file && fclose(patch_script_file) == EOF &&
7477 error = got_error_from_errno2("fclose", patch_script_path);
7479 got_repo_close(repo);
7481 got_worktree_close(worktree);
7482 TAILQ_FOREACH(pe, &paths, entry)
7483 free((char *)pe->path);
7484 got_pathlist_free(&paths);
7492 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7493 "arg1 [arg2 ...]\n", getprogname());
7497 static const struct got_error *
7498 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7500 const struct got_error *err;
7501 struct got_blob_object *blob;
7503 err = got_object_open_as_blob(&blob, repo, id, 8192);
7507 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7508 got_object_blob_close(blob);
7512 static const struct got_error *
7513 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7515 const struct got_error *err;
7516 struct got_tree_object *tree;
7519 err = got_object_open_as_tree(&tree, repo, id);
7523 nentries = got_object_tree_get_nentries(tree);
7524 for (i = 0; i < nentries; i++) {
7525 struct got_tree_entry *te;
7527 if (sigint_received || sigpipe_received)
7529 te = got_object_tree_get_entry(tree, i);
7530 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7533 fprintf(outfile, "%s %.7o %s\n", id_str,
7534 got_tree_entry_get_mode(te),
7535 got_tree_entry_get_name(te));
7539 got_object_tree_close(tree);
7543 static const struct got_error *
7544 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7546 const struct got_error *err;
7547 struct got_commit_object *commit;
7548 const struct got_object_id_queue *parent_ids;
7549 struct got_object_qid *pid;
7550 char *id_str = NULL;
7551 const char *logmsg = NULL;
7553 err = got_object_open_as_commit(&commit, repo, id);
7557 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7561 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7562 parent_ids = got_object_commit_get_parent_ids(commit);
7563 fprintf(outfile, "numparents %d\n",
7564 got_object_commit_get_nparents(commit));
7565 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7567 err = got_object_id_str(&pid_str, pid->id);
7570 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7573 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7574 got_object_commit_get_author(commit),
7575 got_object_commit_get_author_time(commit));
7577 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7578 got_object_commit_get_author(commit),
7579 got_object_commit_get_committer_time(commit));
7581 logmsg = got_object_commit_get_logmsg_raw(commit);
7582 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7583 fprintf(outfile, "%s", logmsg);
7586 got_object_commit_close(commit);
7590 static const struct got_error *
7591 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7593 const struct got_error *err;
7594 struct got_tag_object *tag;
7595 char *id_str = NULL;
7596 const char *tagmsg = NULL;
7598 err = got_object_open_as_tag(&tag, repo, id);
7602 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7606 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7608 switch (got_object_tag_get_object_type(tag)) {
7609 case GOT_OBJ_TYPE_BLOB:
7610 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7611 GOT_OBJ_LABEL_BLOB);
7613 case GOT_OBJ_TYPE_TREE:
7614 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7615 GOT_OBJ_LABEL_TREE);
7617 case GOT_OBJ_TYPE_COMMIT:
7618 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7619 GOT_OBJ_LABEL_COMMIT);
7621 case GOT_OBJ_TYPE_TAG:
7622 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7629 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7630 got_object_tag_get_name(tag));
7632 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7633 got_object_tag_get_tagger(tag),
7634 got_object_tag_get_tagger_time(tag));
7636 tagmsg = got_object_tag_get_message(tag);
7637 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7638 fprintf(outfile, "%s", tagmsg);
7641 got_object_tag_close(tag);
7645 static const struct got_error *
7646 cmd_cat(int argc, char *argv[])
7648 const struct got_error *error;
7649 struct got_repository *repo = NULL;
7650 struct got_worktree *worktree = NULL;
7651 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7652 const char *commit_id_str = NULL;
7653 struct got_object_id *id = NULL, *commit_id = NULL;
7654 int ch, obj_type, i, force_path = 0;
7657 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7662 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7665 commit_id_str = optarg;
7668 repo_path = realpath(optarg, NULL);
7669 if (repo_path == NULL)
7670 return got_error_from_errno2("realpath",
7672 got_path_strip_trailing_slashes(repo_path);
7686 cwd = getcwd(NULL, 0);
7688 error = got_error_from_errno("getcwd");
7691 error = got_worktree_open(&worktree, cwd);
7692 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7695 if (repo_path == NULL) {
7697 got_worktree_get_repo_path(worktree));
7698 if (repo_path == NULL) {
7699 error = got_error_from_errno("strdup");
7705 if (repo_path == NULL) {
7706 repo_path = getcwd(NULL, 0);
7707 if (repo_path == NULL)
7708 return got_error_from_errno("getcwd");
7711 error = got_repo_open(&repo, repo_path, NULL);
7716 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7720 if (commit_id_str == NULL)
7721 commit_id_str = GOT_REF_HEAD;
7722 error = got_repo_match_object_id(&commit_id, NULL,
7723 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7727 for (i = 0; i < argc; i++) {
7729 error = got_object_id_by_path(&id, repo, commit_id,
7734 error = got_repo_match_object_id(&id, &label, argv[i],
7735 GOT_OBJ_TYPE_ANY, 0, repo);
7737 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7738 error->code != GOT_ERR_NOT_REF)
7740 error = got_object_id_by_path(&id, repo,
7741 commit_id, argv[i]);
7747 error = got_object_get_type(&obj_type, repo, id);
7752 case GOT_OBJ_TYPE_BLOB:
7753 error = cat_blob(id, repo, stdout);
7755 case GOT_OBJ_TYPE_TREE:
7756 error = cat_tree(id, repo, stdout);
7758 case GOT_OBJ_TYPE_COMMIT:
7759 error = cat_commit(id, repo, stdout);
7761 case GOT_OBJ_TYPE_TAG:
7762 error = cat_tag(id, repo, stdout);
7765 error = got_error(GOT_ERR_OBJ_TYPE);
7780 got_worktree_close(worktree);
7782 const struct got_error *repo_error;
7783 repo_error = got_repo_close(repo);