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%%", p);
990 if (nobj_resolved > 0) {
991 p = (nobj_resolved * 100) / (nobj_total - nobj_loose);
992 printf("; resolving deltas %d%%", p);
1000 static const struct got_error *
1001 cmd_clone(int argc, char *argv[])
1003 const struct got_error *err = NULL;
1004 const char *uri, *branch_filter, *dirname;
1005 char *proto, *host, *port, *repo_name, *server_path;
1006 char *default_destdir = NULL, *id_str = NULL;
1007 const char *repo_path;
1008 struct got_repository *repo = NULL;
1009 struct got_pathlist_head refs, symrefs;
1010 struct got_pathlist_entry *pe;
1011 struct got_object_id *pack_hash = NULL;
1012 int ch, fetchfd = -1;
1013 int did_something = 0;
1016 TAILQ_INIT(&symrefs);
1018 while ((ch = getopt(argc, argv, "b:")) != -1) {
1021 branch_filter = optarg;
1038 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1039 &repo_name, argv[0]);
1043 if (dirname == NULL) {
1044 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1045 err = got_error_from_errno("asprintf");
1048 repo_path = default_destdir;
1050 repo_path = dirname;
1052 err = got_path_mkdir(repo_path);
1056 err = got_repo_init(repo_path);
1060 err = got_repo_open(&repo, repo_path, NULL);
1064 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1068 printf("Connected to %s:%s\n", host, port);
1070 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1071 repo, fetch_progress, &did_something);
1077 err = got_object_id_str(&id_str, pack_hash);
1080 printf("Fetched %s.pack\n", id_str);
1083 /* Set up references provided with the pack file. */
1084 TAILQ_FOREACH(pe, &refs, entry) {
1085 const char *refname = pe->path;
1086 struct got_object_id *id = pe->data;
1087 struct got_reference *ref;
1090 err = got_ref_alloc(&ref, refname, id);
1095 err = got_object_id_str(&id_str, id);
1098 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1101 err = got_ref_write(ref, repo);
1107 /* Set the HEAD reference if the server provided one. */
1108 TAILQ_FOREACH(pe, &symrefs, entry) {
1109 struct got_reference *symref, *target_ref;
1110 const char *refname = pe->path;
1111 const char *target = pe->data;
1113 if (strcmp(refname, GOT_REF_HEAD) != 0)
1116 err = got_ref_open(&target_ref, repo, target, 0);
1118 if (err->code == GOT_ERR_NOT_REF)
1123 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1124 got_ref_close(target_ref);
1128 printf("Setting %s to %s\n", GOT_REF_HEAD,
1129 got_ref_get_symref_target(symref));
1131 err = got_ref_write(symref, repo);
1132 got_ref_close(symref);
1137 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1138 err = got_error_from_errno("close");
1140 got_repo_close(repo);
1141 TAILQ_FOREACH(pe, &refs, entry) {
1142 free((void *)pe->path);
1145 got_pathlist_free(&refs);
1146 TAILQ_FOREACH(pe, &symrefs, entry) {
1147 free((void *)pe->path);
1150 got_pathlist_free(&symrefs);
1157 free(default_destdir);
1161 static const struct got_error *
1162 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1164 static char msg[512];
1165 const char *branch_name;
1167 if (got_ref_is_symbolic(ref))
1168 branch_name = got_ref_get_symref_target(ref);
1170 branch_name = got_ref_get_name(ref);
1172 if (strncmp("refs/heads/", branch_name, 11) == 0)
1175 snprintf(msg, sizeof(msg),
1176 "target commit is not contained in branch '%s'; "
1177 "the branch to use must be specified with -b; "
1178 "if necessary a new branch can be created for "
1179 "this commit with 'got branch -c %s BRANCH_NAME'",
1180 branch_name, commit_id_str);
1182 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1185 static const struct got_error *
1186 cmd_checkout(int argc, char *argv[])
1188 const struct got_error *error = NULL;
1189 struct got_repository *repo = NULL;
1190 struct got_reference *head_ref = NULL;
1191 struct got_worktree *worktree = NULL;
1192 char *repo_path = NULL;
1193 char *worktree_path = NULL;
1194 const char *path_prefix = "";
1195 const char *branch_name = GOT_REF_HEAD;
1196 char *commit_id_str = NULL;
1197 int ch, same_path_prefix, allow_nonempty = 0;
1198 struct got_pathlist_head paths;
1199 struct got_checkout_progress_arg cpa;
1203 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1206 branch_name = optarg;
1209 commit_id_str = strdup(optarg);
1210 if (commit_id_str == NULL)
1211 return got_error_from_errno("strdup");
1217 path_prefix = optarg;
1229 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1230 "unveil", NULL) == -1)
1234 char *cwd, *base, *dotgit;
1235 repo_path = realpath(argv[0], NULL);
1236 if (repo_path == NULL)
1237 return got_error_from_errno2("realpath", argv[0]);
1238 cwd = getcwd(NULL, 0);
1240 error = got_error_from_errno("getcwd");
1243 if (path_prefix[0]) {
1244 base = basename(path_prefix);
1246 error = got_error_from_errno2("basename",
1251 base = basename(repo_path);
1253 error = got_error_from_errno2("basename",
1258 dotgit = strstr(base, ".git");
1261 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1262 error = got_error_from_errno("asprintf");
1267 } else if (argc == 2) {
1268 repo_path = realpath(argv[0], NULL);
1269 if (repo_path == NULL) {
1270 error = got_error_from_errno2("realpath", argv[0]);
1273 worktree_path = realpath(argv[1], NULL);
1274 if (worktree_path == NULL) {
1275 if (errno != ENOENT) {
1276 error = got_error_from_errno2("realpath",
1280 worktree_path = strdup(argv[1]);
1281 if (worktree_path == NULL) {
1282 error = got_error_from_errno("strdup");
1289 got_path_strip_trailing_slashes(repo_path);
1290 got_path_strip_trailing_slashes(worktree_path);
1292 error = got_repo_open(&repo, repo_path, NULL);
1296 /* Pre-create work tree path for unveil(2) */
1297 error = got_path_mkdir(worktree_path);
1299 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1300 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1302 if (!allow_nonempty &&
1303 !got_path_dir_is_empty(worktree_path)) {
1304 error = got_error_path(worktree_path,
1305 GOT_ERR_DIR_NOT_EMPTY);
1310 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1314 error = got_ref_open(&head_ref, repo, branch_name, 0);
1318 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1319 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1322 error = got_worktree_open(&worktree, worktree_path);
1326 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1330 if (!same_path_prefix) {
1331 error = got_error(GOT_ERR_PATH_PREFIX);
1335 if (commit_id_str) {
1336 struct got_object_id *commit_id;
1337 error = got_repo_match_object_id(&commit_id, NULL,
1338 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1341 error = check_linear_ancestry(commit_id,
1342 got_worktree_get_base_commit_id(worktree), 0, repo);
1343 if (error != NULL) {
1345 if (error->code == GOT_ERR_ANCESTRY) {
1346 error = checkout_ancestry_error(
1347 head_ref, commit_id_str);
1351 error = check_same_branch(commit_id, head_ref, NULL, repo);
1353 if (error->code == GOT_ERR_ANCESTRY) {
1354 error = checkout_ancestry_error(
1355 head_ref, commit_id_str);
1359 error = got_worktree_set_base_commit_id(worktree, repo,
1366 error = got_pathlist_append(&paths, "", NULL);
1369 cpa.worktree_path = worktree_path;
1370 cpa.had_base_commit_ref_error = 0;
1371 error = got_worktree_checkout_files(worktree, &paths, repo,
1372 checkout_progress, &cpa, check_cancelled, NULL);
1376 printf("Now shut up and hack\n");
1377 if (cpa.had_base_commit_ref_error)
1378 show_worktree_base_ref_warning();
1380 got_pathlist_free(&paths);
1381 free(commit_id_str);
1383 free(worktree_path);
1390 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1395 static const struct got_error *
1396 update_progress(void *arg, unsigned char status, const char *path)
1398 int *did_something = arg;
1400 if (status == GOT_STATUS_EXISTS ||
1401 status == GOT_STATUS_BASE_REF_ERR)
1406 /* Base commit bump happens silently. */
1407 if (status == GOT_STATUS_BUMP_BASE)
1410 while (path[0] == '/')
1412 printf("%c %s\n", status, path);
1416 static const struct got_error *
1417 switch_head_ref(struct got_reference *head_ref,
1418 struct got_object_id *commit_id, struct got_worktree *worktree,
1419 struct got_repository *repo)
1421 const struct got_error *err = NULL;
1423 int ref_has_moved = 0;
1425 /* Trivial case: switching between two different references. */
1426 if (strcmp(got_ref_get_name(head_ref),
1427 got_worktree_get_head_ref_name(worktree)) != 0) {
1428 printf("Switching work tree from %s to %s\n",
1429 got_worktree_get_head_ref_name(worktree),
1430 got_ref_get_name(head_ref));
1431 return got_worktree_set_head_ref(worktree, head_ref);
1434 err = check_linear_ancestry(commit_id,
1435 got_worktree_get_base_commit_id(worktree), 0, repo);
1437 if (err->code != GOT_ERR_ANCESTRY)
1444 /* Switching to a rebased branch with the same reference name. */
1445 err = got_object_id_str(&base_id_str,
1446 got_worktree_get_base_commit_id(worktree));
1449 printf("Reference %s now points at a different branch\n",
1450 got_worktree_get_head_ref_name(worktree));
1451 printf("Switching work tree from %s to %s\n", base_id_str,
1452 got_worktree_get_head_ref_name(worktree));
1456 static const struct got_error *
1457 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1459 const struct got_error *err;
1462 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1466 return got_error(GOT_ERR_REBASING);
1468 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1472 return got_error(GOT_ERR_HISTEDIT_BUSY);
1477 static const struct got_error *
1478 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1479 char *argv[], struct got_worktree *worktree)
1481 const struct got_error *err = NULL;
1488 return got_error_from_errno("strdup");
1489 return got_pathlist_append(paths, path, NULL);
1492 for (i = 0; i < argc; i++) {
1493 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1496 err = got_pathlist_append(paths, path, NULL);
1506 static const struct got_error *
1507 cmd_update(int argc, char *argv[])
1509 const struct got_error *error = NULL;
1510 struct got_repository *repo = NULL;
1511 struct got_worktree *worktree = NULL;
1512 char *worktree_path = NULL;
1513 struct got_object_id *commit_id = NULL;
1514 char *commit_id_str = NULL;
1515 const char *branch_name = NULL;
1516 struct got_reference *head_ref = NULL;
1517 struct got_pathlist_head paths;
1518 struct got_pathlist_entry *pe;
1519 int ch, did_something = 0;
1523 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1526 branch_name = optarg;
1529 commit_id_str = strdup(optarg);
1530 if (commit_id_str == NULL)
1531 return got_error_from_errno("strdup");
1543 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1544 "unveil", NULL) == -1)
1547 worktree_path = getcwd(NULL, 0);
1548 if (worktree_path == NULL) {
1549 error = got_error_from_errno("getcwd");
1552 error = got_worktree_open(&worktree, worktree_path);
1556 error = check_rebase_or_histedit_in_progress(worktree);
1560 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1565 error = apply_unveil(got_repo_get_path(repo), 0,
1566 got_worktree_get_root_path(worktree));
1570 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1574 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1575 got_worktree_get_head_ref_name(worktree), 0);
1578 if (commit_id_str == NULL) {
1579 error = got_ref_resolve(&commit_id, repo, head_ref);
1582 error = got_object_id_str(&commit_id_str, commit_id);
1586 error = got_repo_match_object_id(&commit_id, NULL,
1587 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1588 free(commit_id_str);
1589 commit_id_str = NULL;
1592 error = got_object_id_str(&commit_id_str, commit_id);
1598 struct got_object_id *head_commit_id;
1599 TAILQ_FOREACH(pe, &paths, entry) {
1600 if (pe->path_len == 0)
1602 error = got_error_msg(GOT_ERR_BAD_PATH,
1603 "switching between branches requires that "
1604 "the entire work tree gets updated");
1607 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1610 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1612 free(head_commit_id);
1615 error = check_same_branch(commit_id, head_ref, NULL, repo);
1618 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1622 error = check_linear_ancestry(commit_id,
1623 got_worktree_get_base_commit_id(worktree), 0, repo);
1624 if (error != NULL) {
1625 if (error->code == GOT_ERR_ANCESTRY)
1626 error = got_error(GOT_ERR_BRANCH_MOVED);
1629 error = check_same_branch(commit_id, head_ref, NULL, repo);
1634 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1636 error = got_worktree_set_base_commit_id(worktree, repo,
1642 error = got_worktree_checkout_files(worktree, &paths, repo,
1643 update_progress, &did_something, check_cancelled, NULL);
1648 printf("Updated to commit %s\n", commit_id_str);
1650 printf("Already up-to-date\n");
1652 free(worktree_path);
1653 TAILQ_FOREACH(pe, &paths, entry)
1654 free((char *)pe->path);
1655 got_pathlist_free(&paths);
1657 free(commit_id_str);
1661 static const struct got_error *
1662 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1663 const char *path, int diff_context, int ignore_whitespace,
1664 struct got_repository *repo)
1666 const struct got_error *err = NULL;
1667 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1670 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1675 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1679 while (path[0] == '/')
1681 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1682 ignore_whitespace, stdout);
1685 got_object_blob_close(blob1);
1686 got_object_blob_close(blob2);
1690 static const struct got_error *
1691 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1692 const char *path, int diff_context, int ignore_whitespace,
1693 struct got_repository *repo)
1695 const struct got_error *err = NULL;
1696 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1697 struct got_diff_blob_output_unidiff_arg arg;
1700 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1705 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1709 arg.diff_context = diff_context;
1710 arg.ignore_whitespace = ignore_whitespace;
1711 arg.outfile = stdout;
1712 while (path[0] == '/')
1714 err = got_diff_tree(tree1, tree2, path, path, repo,
1715 got_diff_blob_output_unidiff, &arg, 1);
1718 got_object_tree_close(tree1);
1720 got_object_tree_close(tree2);
1724 static const struct got_error *
1725 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1726 const char *path, int diff_context, struct got_repository *repo)
1728 const struct got_error *err = NULL;
1729 struct got_commit_object *pcommit = NULL;
1730 char *id_str1 = NULL, *id_str2 = NULL;
1731 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1732 struct got_object_qid *qid;
1734 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1736 err = got_object_open_as_commit(&pcommit, repo,
1742 if (path && path[0] != '\0') {
1744 err = got_object_id_by_path(&obj_id2, repo, id, path);
1747 err = got_object_id_str(&id_str2, obj_id2);
1753 err = got_object_id_by_path(&obj_id1, repo,
1759 err = got_object_id_str(&id_str1, obj_id1);
1765 err = got_object_get_type(&obj_type, repo, obj_id2);
1770 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1772 case GOT_OBJ_TYPE_BLOB:
1773 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1776 case GOT_OBJ_TYPE_TREE:
1777 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1781 err = got_error(GOT_ERR_OBJ_TYPE);
1787 obj_id2 = got_object_commit_get_tree_id(commit);
1788 err = got_object_id_str(&id_str2, obj_id2);
1791 obj_id1 = got_object_commit_get_tree_id(pcommit);
1792 err = got_object_id_str(&id_str1, obj_id1);
1795 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1796 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1802 got_object_commit_close(pcommit);
1807 get_datestr(time_t *time, char *datebuf)
1809 struct tm mytm, *tm;
1812 tm = gmtime_r(time, &mytm);
1815 s = asctime_r(tm, datebuf);
1818 p = strchr(s, '\n');
1824 static const struct got_error *
1825 match_logmsg(int *have_match, struct got_object_id *id,
1826 struct got_commit_object *commit, regex_t *regex)
1828 const struct got_error *err = NULL;
1829 regmatch_t regmatch;
1830 char *id_str = NULL, *logmsg = NULL;
1834 err = got_object_id_str(&id_str, id);
1838 err = got_object_commit_get_logmsg(&logmsg, commit);
1842 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1850 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1852 static const struct got_error *
1853 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1854 struct got_repository *repo, const char *path, int show_patch,
1855 int diff_context, struct got_reflist_head *refs)
1857 const struct got_error *err = NULL;
1858 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1860 time_t committer_time;
1861 const char *author, *committer;
1862 char *refs_str = NULL;
1863 struct got_reflist_entry *re;
1865 SIMPLEQ_FOREACH(re, refs, entry) {
1868 struct got_tag_object *tag = NULL;
1871 name = got_ref_get_name(re->ref);
1872 if (strcmp(name, GOT_REF_HEAD) == 0)
1874 if (strncmp(name, "refs/", 5) == 0)
1876 if (strncmp(name, "got/", 4) == 0)
1878 if (strncmp(name, "heads/", 6) == 0)
1880 if (strncmp(name, "remotes/", 8) == 0)
1882 if (strncmp(name, "tags/", 5) == 0) {
1883 err = got_object_open_as_tag(&tag, repo, re->id);
1885 if (err->code != GOT_ERR_OBJ_TYPE)
1887 /* Ref points at something other than a tag. */
1892 cmp = got_object_id_cmp(tag ?
1893 got_object_tag_get_object_id(tag) : re->id, id);
1895 got_object_tag_close(tag);
1899 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1901 err = got_error_from_errno("asprintf");
1907 err = got_object_id_str(&id_str, id);
1911 printf(GOT_COMMIT_SEP_STR);
1912 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1913 refs_str ? refs_str : "", refs_str ? ")" : "");
1918 printf("from: %s\n", got_object_commit_get_author(commit));
1919 committer_time = got_object_commit_get_committer_time(commit);
1920 datestr = get_datestr(&committer_time, datebuf);
1922 printf("date: %s UTC\n", datestr);
1923 author = got_object_commit_get_author(commit);
1924 committer = got_object_commit_get_committer(commit);
1925 if (strcmp(author, committer) != 0)
1926 printf("via: %s\n", committer);
1927 if (got_object_commit_get_nparents(commit) > 1) {
1928 const struct got_object_id_queue *parent_ids;
1929 struct got_object_qid *qid;
1931 parent_ids = got_object_commit_get_parent_ids(commit);
1932 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1933 err = got_object_id_str(&id_str, qid->id);
1936 printf("parent %d: %s\n", n++, id_str);
1941 err = got_object_commit_get_logmsg(&logmsg0, commit);
1947 line = strsep(&logmsg, "\n");
1949 printf(" %s\n", line);
1954 err = print_patch(commit, id, path, diff_context, repo);
1959 if (fflush(stdout) != 0 && err == NULL)
1960 err = got_error_from_errno("fflush");
1964 static const struct got_error *
1965 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1966 const char *path, int show_patch, const char *search_pattern,
1967 int diff_context, int limit, int log_branches,
1968 struct got_reflist_head *refs)
1970 const struct got_error *err;
1971 struct got_commit_graph *graph;
1975 if (search_pattern &&
1976 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1977 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1979 err = got_commit_graph_open(&graph, path, !log_branches);
1982 err = got_commit_graph_iter_start(graph, root_id, repo,
1983 check_cancelled, NULL);
1987 struct got_commit_object *commit;
1988 struct got_object_id *id;
1990 if (sigint_received || sigpipe_received)
1993 err = got_commit_graph_iter_next(&id, graph, repo,
1994 check_cancelled, NULL);
1996 if (err->code == GOT_ERR_ITER_COMPLETED)
2003 err = got_object_open_as_commit(&commit, repo, id);
2007 if (search_pattern) {
2008 err = match_logmsg(&have_match, id, commit, ®ex);
2010 got_object_commit_close(commit);
2013 if (have_match == 0) {
2014 got_object_commit_close(commit);
2019 err = print_commit(commit, id, repo, path, show_patch,
2020 diff_context, refs);
2021 got_object_commit_close(commit);
2022 if (err || (limit && --limit == 0))
2028 got_commit_graph_close(graph);
2035 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2036 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2041 get_default_log_limit(void)
2043 const char *got_default_log_limit;
2047 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2048 if (got_default_log_limit == NULL)
2050 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2056 static const struct got_error *
2057 cmd_log(int argc, char *argv[])
2059 const struct got_error *error;
2060 struct got_repository *repo = NULL;
2061 struct got_worktree *worktree = NULL;
2062 struct got_commit_object *commit = NULL;
2063 struct got_object_id *id = NULL;
2064 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2065 const char *start_commit = NULL, *search_pattern = NULL;
2066 int diff_context = -1, ch;
2067 int show_patch = 0, limit = 0, log_branches = 0;
2069 struct got_reflist_head refs;
2071 SIMPLEQ_INIT(&refs);
2074 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2080 limit = get_default_log_limit();
2082 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2088 start_commit = optarg;
2091 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2094 err(1, "-C option %s", errstr);
2097 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2099 err(1, "-l option %s", errstr);
2105 repo_path = realpath(optarg, NULL);
2106 if (repo_path == NULL)
2107 return got_error_from_errno2("realpath",
2109 got_path_strip_trailing_slashes(repo_path);
2112 search_pattern = optarg;
2123 if (diff_context == -1)
2125 else if (!show_patch)
2126 errx(1, "-C reguires -p");
2128 cwd = getcwd(NULL, 0);
2130 error = got_error_from_errno("getcwd");
2134 error = got_worktree_open(&worktree, cwd);
2135 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2142 error = got_error_from_errno("strdup");
2145 } else if (argc == 1) {
2147 error = got_worktree_resolve_path(&path, worktree,
2152 path = strdup(argv[0]);
2154 error = got_error_from_errno("strdup");
2161 if (repo_path == NULL) {
2162 repo_path = worktree ?
2163 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2165 if (repo_path == NULL) {
2166 error = got_error_from_errno("strdup");
2170 error = got_repo_open(&repo, repo_path, NULL);
2174 error = apply_unveil(got_repo_get_path(repo), 1,
2175 worktree ? got_worktree_get_root_path(worktree) : NULL);
2179 if (start_commit == NULL) {
2180 struct got_reference *head_ref;
2181 error = got_ref_open(&head_ref, repo,
2182 worktree ? got_worktree_get_head_ref_name(worktree)
2186 error = got_ref_resolve(&id, repo, head_ref);
2187 got_ref_close(head_ref);
2190 error = got_object_open_as_commit(&commit, repo, id);
2192 struct got_reference *ref;
2193 error = got_ref_open(&ref, repo, start_commit, 0);
2194 if (error == NULL) {
2196 error = got_ref_resolve(&id, repo, ref);
2200 error = got_object_get_type(&obj_type, repo, id);
2203 if (obj_type == GOT_OBJ_TYPE_TAG) {
2204 struct got_tag_object *tag;
2205 error = got_object_open_as_tag(&tag, repo, id);
2208 if (got_object_tag_get_object_type(tag) !=
2209 GOT_OBJ_TYPE_COMMIT) {
2210 got_object_tag_close(tag);
2211 error = got_error(GOT_ERR_OBJ_TYPE);
2215 id = got_object_id_dup(
2216 got_object_tag_get_object_id(tag));
2218 error = got_error_from_errno(
2219 "got_object_id_dup");
2220 got_object_tag_close(tag);
2223 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2224 error = got_error(GOT_ERR_OBJ_TYPE);
2227 error = got_object_open_as_commit(&commit, repo, id);
2231 if (commit == NULL) {
2232 error = got_repo_match_object_id_prefix(&id,
2233 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2242 const char *prefix = got_worktree_get_path_prefix(worktree);
2244 if (asprintf(&p, "%s%s%s", prefix,
2245 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2246 error = got_error_from_errno("asprintf");
2249 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2252 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2257 path = in_repo_path;
2260 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2264 error = print_commits(id, repo, path, show_patch, search_pattern,
2265 diff_context, limit, log_branches, &refs);
2272 got_worktree_close(worktree);
2274 const struct got_error *repo_error;
2275 repo_error = got_repo_close(repo);
2279 got_ref_list_free(&refs);
2286 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2287 "[-w] [object1 object2 | path]\n", getprogname());
2291 struct print_diff_arg {
2292 struct got_repository *repo;
2293 struct got_worktree *worktree;
2298 int ignore_whitespace;
2301 static const struct got_error *
2302 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2303 const char *path, struct got_object_id *blob_id,
2304 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2305 int dirfd, const char *de_name)
2307 struct print_diff_arg *a = arg;
2308 const struct got_error *err = NULL;
2309 struct got_blob_object *blob1 = NULL;
2312 char *abspath = NULL, *label1 = NULL;
2315 if (a->diff_staged) {
2316 if (staged_status != GOT_STATUS_MODIFY &&
2317 staged_status != GOT_STATUS_ADD &&
2318 staged_status != GOT_STATUS_DELETE)
2321 if (staged_status == GOT_STATUS_DELETE)
2323 if (status == GOT_STATUS_NONEXISTENT)
2324 return got_error_set_errno(ENOENT, path);
2325 if (status != GOT_STATUS_MODIFY &&
2326 status != GOT_STATUS_ADD &&
2327 status != GOT_STATUS_DELETE &&
2328 status != GOT_STATUS_CONFLICT)
2332 if (!a->header_shown) {
2333 printf("diff %s %s%s\n", a->id_str,
2334 got_worktree_get_root_path(a->worktree),
2335 a->diff_staged ? " (staged changes)" : "");
2336 a->header_shown = 1;
2339 if (a->diff_staged) {
2340 const char *label1 = NULL, *label2 = NULL;
2341 switch (staged_status) {
2342 case GOT_STATUS_MODIFY:
2346 case GOT_STATUS_ADD:
2349 case GOT_STATUS_DELETE:
2353 return got_error(GOT_ERR_FILE_STATUS);
2355 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2356 label1, label2, a->diff_context, a->ignore_whitespace,
2360 if (staged_status == GOT_STATUS_ADD ||
2361 staged_status == GOT_STATUS_MODIFY) {
2363 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2367 err = got_object_id_str(&id_str, staged_blob_id);
2370 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2371 err = got_error_from_errno("asprintf");
2376 } else if (status != GOT_STATUS_ADD) {
2377 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2382 if (status != GOT_STATUS_DELETE) {
2383 if (asprintf(&abspath, "%s/%s",
2384 got_worktree_get_root_path(a->worktree), path) == -1) {
2385 err = got_error_from_errno("asprintf");
2390 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2392 err = got_error_from_errno2("openat", abspath);
2396 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2398 err = got_error_from_errno2("open", abspath);
2402 if (fstat(fd, &sb) == -1) {
2403 err = got_error_from_errno2("fstat", abspath);
2406 f2 = fdopen(fd, "r");
2408 err = got_error_from_errno2("fdopen", abspath);
2415 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2416 a->diff_context, a->ignore_whitespace, stdout);
2419 got_object_blob_close(blob1);
2420 if (f2 && fclose(f2) == EOF && err == NULL)
2421 err = got_error_from_errno("fclose");
2422 if (fd != -1 && close(fd) == -1 && err == NULL)
2423 err = got_error_from_errno("close");
2428 static const struct got_error *
2429 cmd_diff(int argc, char *argv[])
2431 const struct got_error *error;
2432 struct got_repository *repo = NULL;
2433 struct got_worktree *worktree = NULL;
2434 char *cwd = NULL, *repo_path = NULL;
2435 struct got_object_id *id1 = NULL, *id2 = NULL;
2436 const char *id_str1 = NULL, *id_str2 = NULL;
2437 char *label1 = NULL, *label2 = NULL;
2439 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2444 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2449 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2452 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2455 err(1, "-C option %s", errstr);
2458 repo_path = realpath(optarg, NULL);
2459 if (repo_path == NULL)
2460 return got_error_from_errno2("realpath",
2462 got_path_strip_trailing_slashes(repo_path);
2468 ignore_whitespace = 1;
2479 cwd = getcwd(NULL, 0);
2481 error = got_error_from_errno("getcwd");
2484 error = got_worktree_open(&worktree, cwd);
2485 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2488 if (worktree == NULL) {
2489 error = got_error(GOT_ERR_NOT_WORKTREE);
2494 "-r option can't be used when diffing a work tree");
2495 repo_path = strdup(got_worktree_get_repo_path(worktree));
2496 if (repo_path == NULL) {
2497 error = got_error_from_errno("strdup");
2501 error = got_worktree_resolve_path(&path, worktree,
2508 error = got_error_from_errno("strdup");
2512 } else if (argc == 2) {
2514 errx(1, "-s option can't be used when diffing "
2515 "objects in repository");
2518 if (worktree && repo_path == NULL) {
2520 strdup(got_worktree_get_repo_path(worktree));
2521 if (repo_path == NULL) {
2522 error = got_error_from_errno("strdup");
2529 if (repo_path == NULL) {
2530 repo_path = getcwd(NULL, 0);
2531 if (repo_path == NULL)
2532 return got_error_from_errno("getcwd");
2535 error = got_repo_open(&repo, repo_path, NULL);
2540 error = apply_unveil(got_repo_get_path(repo), 1,
2541 worktree ? got_worktree_get_root_path(worktree) : NULL);
2546 struct print_diff_arg arg;
2547 struct got_pathlist_head paths;
2552 error = got_object_id_str(&id_str,
2553 got_worktree_get_base_commit_id(worktree));
2557 arg.worktree = worktree;
2558 arg.diff_context = diff_context;
2559 arg.id_str = id_str;
2560 arg.header_shown = 0;
2561 arg.diff_staged = diff_staged;
2562 arg.ignore_whitespace = ignore_whitespace;
2564 error = got_pathlist_append(&paths, path, NULL);
2568 error = got_worktree_status(worktree, &paths, repo, print_diff,
2569 &arg, check_cancelled, NULL);
2571 got_pathlist_free(&paths);
2575 error = got_repo_match_object_id(&id1, &label1, id_str1,
2576 GOT_OBJ_TYPE_ANY, 1, repo);
2580 error = got_repo_match_object_id(&id2, &label2, id_str2,
2581 GOT_OBJ_TYPE_ANY, 1, repo);
2585 error = got_object_get_type(&type1, repo, id1);
2589 error = got_object_get_type(&type2, repo, id2);
2593 if (type1 != type2) {
2594 error = got_error(GOT_ERR_OBJ_TYPE);
2599 case GOT_OBJ_TYPE_BLOB:
2600 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2601 diff_context, ignore_whitespace, repo, stdout);
2603 case GOT_OBJ_TYPE_TREE:
2604 error = got_diff_objects_as_trees(id1, id2, "", "",
2605 diff_context, ignore_whitespace, repo, stdout);
2607 case GOT_OBJ_TYPE_COMMIT:
2608 printf("diff %s %s\n", label1, label2);
2609 error = got_diff_objects_as_commits(id1, id2, diff_context,
2610 ignore_whitespace, repo, stdout);
2613 error = got_error(GOT_ERR_OBJ_TYPE);
2622 got_worktree_close(worktree);
2624 const struct got_error *repo_error;
2625 repo_error = got_repo_close(repo);
2636 "usage: %s blame [-c commit] [-r repository-path] path\n",
2645 char datebuf[11]; /* YYYY-MM-DD + NUL */
2648 struct blame_cb_args {
2649 struct blame_line *lines;
2653 off_t *line_offsets;
2655 struct got_repository *repo;
2658 static const struct got_error *
2659 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2661 const struct got_error *err = NULL;
2662 struct blame_cb_args *a = arg;
2663 struct blame_line *bline;
2665 size_t linesize = 0;
2666 struct got_commit_object *commit = NULL;
2669 time_t committer_time;
2671 if (nlines != a->nlines ||
2672 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2673 return got_error(GOT_ERR_RANGE);
2675 if (sigint_received)
2676 return got_error(GOT_ERR_ITER_COMPLETED);
2679 return NULL; /* no change in this commit */
2681 /* Annotate this line. */
2682 bline = &a->lines[lineno - 1];
2683 if (bline->annotated)
2685 err = got_object_id_str(&bline->id_str, id);
2689 err = got_object_open_as_commit(&commit, a->repo, id);
2693 bline->committer = strdup(got_object_commit_get_committer(commit));
2694 if (bline->committer == NULL) {
2695 err = got_error_from_errno("strdup");
2699 committer_time = got_object_commit_get_committer_time(commit);
2700 if (localtime_r(&committer_time, &tm) == NULL)
2701 return got_error_from_errno("localtime_r");
2702 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2703 &tm) >= sizeof(bline->datebuf)) {
2704 err = got_error(GOT_ERR_NO_SPACE);
2707 bline->annotated = 1;
2709 /* Print lines annotated so far. */
2710 bline = &a->lines[a->lineno_cur - 1];
2711 if (!bline->annotated)
2714 offset = a->line_offsets[a->lineno_cur - 1];
2715 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2716 err = got_error_from_errno("fseeko");
2720 while (bline->annotated) {
2721 char *smallerthan, *at, *nl, *committer;
2724 if (getline(&line, &linesize, a->f) == -1) {
2726 err = got_error_from_errno("getline");
2730 committer = bline->committer;
2731 smallerthan = strchr(committer, '<');
2732 if (smallerthan && smallerthan[1] != '\0')
2733 committer = smallerthan + 1;
2734 at = strchr(committer, '@');
2737 len = strlen(committer);
2739 committer[8] = '\0';
2741 nl = strchr(line, '\n');
2744 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2745 bline->id_str, bline->datebuf, committer, line);
2748 bline = &a->lines[a->lineno_cur - 1];
2752 got_object_commit_close(commit);
2757 static const struct got_error *
2758 cmd_blame(int argc, char *argv[])
2760 const struct got_error *error;
2761 struct got_repository *repo = NULL;
2762 struct got_worktree *worktree = NULL;
2763 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2764 struct got_object_id *obj_id = NULL;
2765 struct got_object_id *commit_id = NULL;
2766 struct got_blob_object *blob = NULL;
2767 char *commit_id_str = NULL;
2768 struct blame_cb_args bca;
2769 int ch, obj_type, i;
2772 memset(&bca, 0, sizeof(bca));
2775 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2780 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2783 commit_id_str = optarg;
2786 repo_path = realpath(optarg, NULL);
2787 if (repo_path == NULL)
2788 return got_error_from_errno2("realpath",
2790 got_path_strip_trailing_slashes(repo_path);
2806 cwd = getcwd(NULL, 0);
2808 error = got_error_from_errno("getcwd");
2811 if (repo_path == NULL) {
2812 error = got_worktree_open(&worktree, cwd);
2813 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2819 strdup(got_worktree_get_repo_path(worktree));
2820 if (repo_path == NULL)
2821 error = got_error_from_errno("strdup");
2825 repo_path = strdup(cwd);
2826 if (repo_path == NULL) {
2827 error = got_error_from_errno("strdup");
2833 error = got_repo_open(&repo, repo_path, NULL);
2837 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2842 const char *prefix = got_worktree_get_path_prefix(worktree);
2843 char *p, *worktree_subdir = cwd +
2844 strlen(got_worktree_get_root_path(worktree));
2845 if (asprintf(&p, "%s%s%s%s%s",
2846 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2847 worktree_subdir, worktree_subdir[0] ? "/" : "",
2849 error = got_error_from_errno("asprintf");
2852 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2855 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2860 if (commit_id_str == NULL) {
2861 struct got_reference *head_ref;
2862 error = got_ref_open(&head_ref, repo, worktree ?
2863 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2866 error = got_ref_resolve(&commit_id, repo, head_ref);
2867 got_ref_close(head_ref);
2871 error = got_repo_match_object_id(&commit_id, NULL,
2872 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2877 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2881 error = got_object_get_type(&obj_type, repo, obj_id);
2885 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2886 error = got_error(GOT_ERR_OBJ_TYPE);
2890 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2893 bca.f = got_opentemp();
2894 if (bca.f == NULL) {
2895 error = got_error_from_errno("got_opentemp");
2898 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2899 &bca.line_offsets, bca.f, blob);
2900 if (error || bca.nlines == 0)
2903 /* Don't include \n at EOF in the blame line count. */
2904 if (bca.line_offsets[bca.nlines - 1] == filesize)
2907 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2908 if (bca.lines == NULL) {
2909 error = got_error_from_errno("calloc");
2913 bca.nlines_prec = 0;
2921 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2922 check_cancelled, NULL);
2930 got_object_blob_close(blob);
2932 got_worktree_close(worktree);
2934 const struct got_error *repo_error;
2935 repo_error = got_repo_close(repo);
2940 for (i = 0; i < bca.nlines; i++) {
2941 struct blame_line *bline = &bca.lines[i];
2942 free(bline->id_str);
2943 free(bline->committer);
2947 free(bca.line_offsets);
2948 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2949 error = got_error_from_errno("fclose");
2957 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2963 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2964 const char *root_path)
2966 int is_root_path = (strcmp(path, root_path) == 0);
2967 const char *modestr = "";
2968 mode_t mode = got_tree_entry_get_mode(te);
2970 path += strlen(root_path);
2971 while (path[0] == '/')
2974 if (got_object_tree_entry_is_submodule(te))
2976 else if (S_ISLNK(mode))
2978 else if (S_ISDIR(mode))
2980 else if (mode & S_IXUSR)
2983 printf("%s%s%s%s%s\n", id ? id : "", path,
2984 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2987 static const struct got_error *
2988 print_tree(const char *path, struct got_object_id *commit_id,
2989 int show_ids, int recurse, const char *root_path,
2990 struct got_repository *repo)
2992 const struct got_error *err = NULL;
2993 struct got_object_id *tree_id = NULL;
2994 struct got_tree_object *tree = NULL;
2997 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3001 err = got_object_open_as_tree(&tree, repo, tree_id);
3004 nentries = got_object_tree_get_nentries(tree);
3005 for (i = 0; i < nentries; i++) {
3006 struct got_tree_entry *te;
3009 if (sigint_received || sigpipe_received)
3012 te = got_object_tree_get_entry(tree, i);
3015 err = got_object_id_str(&id_str,
3016 got_tree_entry_get_id(te));
3019 if (asprintf(&id, "%s ", id_str) == -1) {
3020 err = got_error_from_errno("asprintf");
3026 print_entry(te, id, path, root_path);
3029 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3031 if (asprintf(&child_path, "%s%s%s", path,
3032 path[0] == '/' && path[1] == '\0' ? "" : "/",
3033 got_tree_entry_get_name(te)) == -1) {
3034 err = got_error_from_errno("asprintf");
3037 err = print_tree(child_path, commit_id, show_ids, 1,
3046 got_object_tree_close(tree);
3051 static const struct got_error *
3052 cmd_tree(int argc, char *argv[])
3054 const struct got_error *error;
3055 struct got_repository *repo = NULL;
3056 struct got_worktree *worktree = NULL;
3058 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3059 struct got_object_id *commit_id = NULL;
3060 char *commit_id_str = NULL;
3061 int show_ids = 0, recurse = 0;
3065 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3070 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3073 commit_id_str = optarg;
3076 repo_path = realpath(optarg, NULL);
3077 if (repo_path == NULL)
3078 return got_error_from_errno2("realpath",
3080 got_path_strip_trailing_slashes(repo_path);
3104 cwd = getcwd(NULL, 0);
3106 error = got_error_from_errno("getcwd");
3109 if (repo_path == NULL) {
3110 error = got_worktree_open(&worktree, cwd);
3111 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3117 strdup(got_worktree_get_repo_path(worktree));
3118 if (repo_path == NULL)
3119 error = got_error_from_errno("strdup");
3123 repo_path = strdup(cwd);
3124 if (repo_path == NULL) {
3125 error = got_error_from_errno("strdup");
3131 error = got_repo_open(&repo, repo_path, NULL);
3135 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3141 char *p, *worktree_subdir = cwd +
3142 strlen(got_worktree_get_root_path(worktree));
3143 if (asprintf(&p, "%s/%s",
3144 got_worktree_get_path_prefix(worktree),
3145 worktree_subdir) == -1) {
3146 error = got_error_from_errno("asprintf");
3149 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3156 if (in_repo_path == NULL) {
3157 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3162 if (commit_id_str == NULL) {
3163 struct got_reference *head_ref;
3164 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3167 error = got_ref_resolve(&commit_id, repo, head_ref);
3168 got_ref_close(head_ref);
3172 error = got_repo_match_object_id(&commit_id, NULL,
3173 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3178 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3179 in_repo_path, repo);
3186 got_worktree_close(worktree);
3188 const struct got_error *repo_error;
3189 repo_error = got_repo_close(repo);
3199 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3203 static const struct got_error *
3204 print_status(void *arg, unsigned char status, unsigned char staged_status,
3205 const char *path, struct got_object_id *blob_id,
3206 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3207 int dirfd, const char *de_name)
3209 if (status == staged_status && (status == GOT_STATUS_DELETE))
3210 status = GOT_STATUS_NO_CHANGE;
3211 printf("%c%c %s\n", status, staged_status, path);
3215 static const struct got_error *
3216 cmd_status(int argc, char *argv[])
3218 const struct got_error *error = NULL;
3219 struct got_repository *repo = NULL;
3220 struct got_worktree *worktree = NULL;
3222 struct got_pathlist_head paths;
3223 struct got_pathlist_entry *pe;
3228 while ((ch = getopt(argc, argv, "")) != -1) {
3240 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3244 cwd = getcwd(NULL, 0);
3246 error = got_error_from_errno("getcwd");
3250 error = got_worktree_open(&worktree, cwd);
3254 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3259 error = apply_unveil(got_repo_get_path(repo), 1,
3260 got_worktree_get_root_path(worktree));
3264 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3268 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3269 check_cancelled, NULL);
3271 TAILQ_FOREACH(pe, &paths, entry)
3272 free((char *)pe->path);
3273 got_pathlist_free(&paths);
3282 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3287 static const struct got_error *
3288 list_refs(struct got_repository *repo)
3290 static const struct got_error *err = NULL;
3291 struct got_reflist_head refs;
3292 struct got_reflist_entry *re;
3294 SIMPLEQ_INIT(&refs);
3295 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3299 SIMPLEQ_FOREACH(re, &refs, entry) {
3301 refstr = got_ref_to_str(re->ref);
3303 return got_error_from_errno("got_ref_to_str");
3304 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3308 got_ref_list_free(&refs);
3312 static const struct got_error *
3313 delete_ref(struct got_repository *repo, const char *refname)
3315 const struct got_error *err = NULL;
3316 struct got_reference *ref;
3318 err = got_ref_open(&ref, repo, refname, 0);
3322 err = got_ref_delete(ref, repo);
3327 static const struct got_error *
3328 add_ref(struct got_repository *repo, const char *refname, const char *target)
3330 const struct got_error *err = NULL;
3331 struct got_object_id *id;
3332 struct got_reference *ref = NULL;
3335 * Don't let the user create a reference name with a leading '-'.
3336 * While technically a valid reference name, this case is usually
3337 * an unintended typo.
3339 if (refname[0] == '-')
3340 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3342 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3345 struct got_reference *target_ref;
3347 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3349 err = got_ref_open(&target_ref, repo, target, 0);
3352 err = got_ref_resolve(&id, repo, target_ref);
3353 got_ref_close(target_ref);
3358 err = got_ref_alloc(&ref, refname, id);
3362 err = got_ref_write(ref, repo);
3370 static const struct got_error *
3371 add_symref(struct got_repository *repo, const char *refname, const char *target)
3373 const struct got_error *err = NULL;
3374 struct got_reference *ref = NULL;
3375 struct got_reference *target_ref = NULL;
3378 * Don't let the user create a reference name with a leading '-'.
3379 * While technically a valid reference name, this case is usually
3380 * an unintended typo.
3382 if (refname[0] == '-')
3383 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3385 err = got_ref_open(&target_ref, repo, target, 0);
3389 err = got_ref_alloc_symref(&ref, refname, target_ref);
3393 err = got_ref_write(ref, repo);
3396 got_ref_close(target_ref);
3402 static const struct got_error *
3403 cmd_ref(int argc, char *argv[])
3405 const struct got_error *error = NULL;
3406 struct got_repository *repo = NULL;
3407 struct got_worktree *worktree = NULL;
3408 char *cwd = NULL, *repo_path = NULL;
3409 int ch, do_list = 0, create_symref = 0;
3410 const char *delref = NULL;
3412 /* TODO: Add -s option for adding symbolic references. */
3413 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3419 repo_path = realpath(optarg, NULL);
3420 if (repo_path == NULL)
3421 return got_error_from_errno2("realpath",
3423 got_path_strip_trailing_slashes(repo_path);
3437 if (do_list && delref)
3438 errx(1, "-l and -d options are mutually exclusive\n");
3443 if (do_list || delref) {
3445 errx(1, "-s option cannot be used together with the "
3446 "-l or -d options");
3449 } else if (argc != 2)
3454 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3458 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3459 "sendfd unveil", NULL) == -1)
3463 cwd = getcwd(NULL, 0);
3465 error = got_error_from_errno("getcwd");
3469 if (repo_path == NULL) {
3470 error = got_worktree_open(&worktree, cwd);
3471 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3477 strdup(got_worktree_get_repo_path(worktree));
3478 if (repo_path == NULL)
3479 error = got_error_from_errno("strdup");
3483 repo_path = strdup(cwd);
3484 if (repo_path == NULL) {
3485 error = got_error_from_errno("strdup");
3491 error = got_repo_open(&repo, repo_path, NULL);
3495 error = apply_unveil(got_repo_get_path(repo), do_list,
3496 worktree ? got_worktree_get_root_path(worktree) : NULL);
3501 error = list_refs(repo);
3503 error = delete_ref(repo, delref);
3504 else if (create_symref)
3505 error = add_symref(repo, argv[0], argv[1]);
3507 error = add_ref(repo, argv[0], argv[1]);
3510 got_repo_close(repo);
3512 got_worktree_close(worktree);
3522 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3523 "[name]\n", getprogname());
3527 static const struct got_error *
3528 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3529 struct got_reference *ref)
3531 const struct got_error *err = NULL;
3532 const char *refname, *marker = " ";
3535 refname = got_ref_get_name(ref);
3536 if (worktree && strcmp(refname,
3537 got_worktree_get_head_ref_name(worktree)) == 0) {
3538 struct got_object_id *id = NULL;
3540 err = got_ref_resolve(&id, repo, ref);
3543 if (got_object_id_cmp(id,
3544 got_worktree_get_base_commit_id(worktree)) == 0)
3551 if (strncmp(refname, "refs/heads/", 11) == 0)
3553 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3556 refstr = got_ref_to_str(ref);
3558 return got_error_from_errno("got_ref_to_str");
3560 printf("%s%s: %s\n", marker, refname, refstr);
3565 static const struct got_error *
3566 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3568 const char *refname;
3570 if (worktree == NULL)
3571 return got_error(GOT_ERR_NOT_WORKTREE);
3573 refname = got_worktree_get_head_ref_name(worktree);
3575 if (strncmp(refname, "refs/heads/", 11) == 0)
3577 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3580 printf("%s\n", refname);
3585 static const struct got_error *
3586 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3588 static const struct got_error *err = NULL;
3589 struct got_reflist_head refs;
3590 struct got_reflist_entry *re;
3591 struct got_reference *temp_ref = NULL;
3592 int rebase_in_progress, histedit_in_progress;
3594 SIMPLEQ_INIT(&refs);
3597 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3602 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3607 if (rebase_in_progress || histedit_in_progress) {
3608 err = got_ref_open(&temp_ref, repo,
3609 got_worktree_get_head_ref_name(worktree), 0);
3612 list_branch(repo, worktree, temp_ref);
3613 got_ref_close(temp_ref);
3617 err = got_ref_list(&refs, repo, "refs/heads",
3618 got_ref_cmp_by_name, NULL);
3622 SIMPLEQ_FOREACH(re, &refs, entry)
3623 list_branch(repo, worktree, re->ref);
3625 got_ref_list_free(&refs);
3629 static const struct got_error *
3630 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3631 const char *branch_name)
3633 const struct got_error *err = NULL;
3634 struct got_reference *ref = NULL;
3637 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3638 return got_error_from_errno("asprintf");
3640 err = got_ref_open(&ref, repo, refname, 0);
3645 strcmp(got_worktree_get_head_ref_name(worktree),
3646 got_ref_get_name(ref)) == 0) {
3647 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3648 "will not delete this work tree's current branch");
3652 err = got_ref_delete(ref, repo);
3660 static const struct got_error *
3661 add_branch(struct got_repository *repo, const char *branch_name,
3662 struct got_object_id *base_commit_id)
3664 const struct got_error *err = NULL;
3665 struct got_reference *ref = NULL;
3666 char *base_refname = NULL, *refname = NULL;
3669 * Don't let the user create a branch name with a leading '-'.
3670 * While technically a valid reference name, this case is usually
3671 * an unintended typo.
3673 if (branch_name[0] == '-')
3674 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3676 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3677 err = got_error_from_errno("asprintf");
3681 err = got_ref_open(&ref, repo, refname, 0);
3683 err = got_error(GOT_ERR_BRANCH_EXISTS);
3685 } else if (err->code != GOT_ERR_NOT_REF)
3688 err = got_ref_alloc(&ref, refname, base_commit_id);
3692 err = got_ref_write(ref, repo);
3701 static const struct got_error *
3702 cmd_branch(int argc, char *argv[])
3704 const struct got_error *error = NULL;
3705 struct got_repository *repo = NULL;
3706 struct got_worktree *worktree = NULL;
3707 char *cwd = NULL, *repo_path = NULL;
3708 int ch, do_list = 0, do_show = 0, do_update = 1;
3709 const char *delref = NULL, *commit_id_arg = NULL;
3710 struct got_reference *ref = NULL;
3711 struct got_pathlist_head paths;
3712 struct got_pathlist_entry *pe;
3713 struct got_object_id *commit_id = NULL;
3714 char *commit_id_str = NULL;
3718 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3721 commit_id_arg = optarg;
3727 repo_path = realpath(optarg, NULL);
3728 if (repo_path == NULL)
3729 return got_error_from_errno2("realpath",
3731 got_path_strip_trailing_slashes(repo_path);
3745 if (do_list && delref)
3746 errx(1, "-l and -d options are mutually exclusive\n");
3751 if (!do_list && !delref && argc == 0)
3754 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3755 errx(1, "-c option can only be used when creating a branch");
3757 if (do_list || delref) {
3760 } else if (!do_show && argc != 1)
3764 if (do_list || do_show) {
3765 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3769 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3770 "sendfd unveil", NULL) == -1)
3774 cwd = getcwd(NULL, 0);
3776 error = got_error_from_errno("getcwd");
3780 if (repo_path == NULL) {
3781 error = got_worktree_open(&worktree, cwd);
3782 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3788 strdup(got_worktree_get_repo_path(worktree));
3789 if (repo_path == NULL)
3790 error = got_error_from_errno("strdup");
3794 repo_path = strdup(cwd);
3795 if (repo_path == NULL) {
3796 error = got_error_from_errno("strdup");
3802 error = got_repo_open(&repo, repo_path, NULL);
3806 error = apply_unveil(got_repo_get_path(repo), do_list,
3807 worktree ? got_worktree_get_root_path(worktree) : NULL);
3812 error = show_current_branch(repo, worktree);
3814 error = list_branches(repo, worktree);
3816 error = delete_branch(repo, worktree, delref);
3818 if (commit_id_arg == NULL)
3819 commit_id_arg = worktree ?
3820 got_worktree_get_head_ref_name(worktree) :
3822 error = got_repo_match_object_id(&commit_id, NULL,
3823 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3826 error = add_branch(repo, argv[0], commit_id);
3829 if (worktree && do_update) {
3830 int did_something = 0;
3831 char *branch_refname = NULL;
3833 error = got_object_id_str(&commit_id_str, commit_id);
3836 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3840 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3842 error = got_error_from_errno("asprintf");
3845 error = got_ref_open(&ref, repo, branch_refname, 0);
3846 free(branch_refname);
3849 error = switch_head_ref(ref, commit_id, worktree,
3853 error = got_worktree_set_base_commit_id(worktree, repo,
3857 error = got_worktree_checkout_files(worktree, &paths,
3858 repo, update_progress, &did_something,
3859 check_cancelled, NULL);
3863 printf("Updated to commit %s\n", commit_id_str);
3870 got_repo_close(repo);
3872 got_worktree_close(worktree);
3876 free(commit_id_str);
3877 TAILQ_FOREACH(pe, &paths, entry)
3878 free((char *)pe->path);
3879 got_pathlist_free(&paths);
3888 "usage: %s tag [-c commit] [-r repository] [-l] "
3889 "[-m message] name\n", getprogname());
3894 static const struct got_error *
3895 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3897 const struct got_error *err = NULL;
3898 struct got_reflist_entry *re, *se, *new;
3899 struct got_object_id *re_id, *se_id;
3900 struct got_tag_object *re_tag, *se_tag;
3901 time_t re_time, se_time;
3903 SIMPLEQ_FOREACH(re, tags, entry) {
3904 se = SIMPLEQ_FIRST(sorted);
3906 err = got_reflist_entry_dup(&new, re);
3909 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3912 err = got_ref_resolve(&re_id, repo, re->ref);
3915 err = got_object_open_as_tag(&re_tag, repo, re_id);
3919 re_time = got_object_tag_get_tagger_time(re_tag);
3920 got_object_tag_close(re_tag);
3924 err = got_ref_resolve(&se_id, repo, re->ref);
3927 err = got_object_open_as_tag(&se_tag, repo, se_id);
3931 se_time = got_object_tag_get_tagger_time(se_tag);
3932 got_object_tag_close(se_tag);
3934 if (se_time > re_time) {
3935 err = got_reflist_entry_dup(&new, re);
3938 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3941 se = SIMPLEQ_NEXT(se, entry);
3950 static const struct got_error *
3951 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3953 static const struct got_error *err = NULL;
3954 struct got_reflist_head refs;
3955 struct got_reflist_entry *re;
3957 SIMPLEQ_INIT(&refs);
3959 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3963 SIMPLEQ_FOREACH(re, &refs, entry) {
3964 const char *refname;
3965 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3969 struct got_object_id *id;
3970 struct got_tag_object *tag;
3971 struct got_commit_object *commit = NULL;
3973 refname = got_ref_get_name(re->ref);
3974 if (strncmp(refname, "refs/tags/", 10) != 0)
3977 refstr = got_ref_to_str(re->ref);
3978 if (refstr == NULL) {
3979 err = got_error_from_errno("got_ref_to_str");
3982 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3985 err = got_ref_resolve(&id, repo, re->ref);
3988 err = got_object_open_as_tag(&tag, repo, id);
3990 if (err->code != GOT_ERR_OBJ_TYPE) {
3994 /* "lightweight" tag */
3995 err = got_object_open_as_commit(&commit, repo, id);
4000 tagger = got_object_commit_get_committer(commit);
4002 got_object_commit_get_committer_time(commit);
4003 err = got_object_id_str(&id_str, id);
4009 tagger = got_object_tag_get_tagger(tag);
4010 tagger_time = got_object_tag_get_tagger_time(tag);
4011 err = got_object_id_str(&id_str,
4012 got_object_tag_get_object_id(tag));
4016 printf("from: %s\n", tagger);
4017 datestr = get_datestr(&tagger_time, datebuf);
4019 printf("date: %s UTC\n", datestr);
4021 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4023 switch (got_object_tag_get_object_type(tag)) {
4024 case GOT_OBJ_TYPE_BLOB:
4025 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4028 case GOT_OBJ_TYPE_TREE:
4029 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4032 case GOT_OBJ_TYPE_COMMIT:
4033 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4036 case GOT_OBJ_TYPE_TAG:
4037 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4046 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4049 got_object_commit_close(commit);
4051 tagmsg0 = strdup(got_object_tag_get_message(tag));
4052 got_object_tag_close(tag);
4053 if (tagmsg0 == NULL) {
4054 err = got_error_from_errno("strdup");
4061 line = strsep(&tagmsg, "\n");
4063 printf(" %s\n", line);
4068 got_ref_list_free(&refs);
4072 static const struct got_error *
4073 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4074 const char *tag_name, const char *repo_path)
4076 const struct got_error *err = NULL;
4077 char *template = NULL, *initial_content = NULL;
4078 char *editor = NULL;
4081 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4082 err = got_error_from_errno("asprintf");
4086 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4087 commit_id_str, tag_name) == -1) {
4088 err = got_error_from_errno("asprintf");
4092 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4096 dprintf(fd, initial_content);
4099 err = get_editor(&editor);
4102 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4104 free(initial_content);
4108 /* Editor is done; we can now apply unveil(2) */
4110 err = apply_unveil(repo_path, 0, NULL);
4119 static const struct got_error *
4120 add_tag(struct got_repository *repo, const char *tag_name,
4121 const char *commit_arg, const char *tagmsg_arg)
4123 const struct got_error *err = NULL;
4124 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4125 char *label = NULL, *commit_id_str = NULL;
4126 struct got_reference *ref = NULL;
4127 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4128 char *tagmsg_path = NULL, *tag_id_str = NULL;
4129 int preserve_tagmsg = 0;
4132 * Don't let the user create a tag name with a leading '-'.
4133 * While technically a valid reference name, this case is usually
4134 * an unintended typo.
4136 if (tag_name[0] == '-')
4137 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4139 err = get_author(&tagger, repo);
4143 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4144 GOT_OBJ_TYPE_COMMIT, 1, repo);
4148 err = got_object_id_str(&commit_id_str, commit_id);
4152 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4153 refname = strdup(tag_name);
4154 if (refname == NULL) {
4155 err = got_error_from_errno("strdup");
4159 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4160 err = got_error_from_errno("asprintf");
4164 err = got_ref_open(&ref, repo, refname, 0);
4166 err = got_error(GOT_ERR_TAG_EXISTS);
4168 } else if (err->code != GOT_ERR_NOT_REF)
4171 if (tagmsg_arg == NULL) {
4172 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4173 tag_name, got_repo_get_path(repo));
4175 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4176 tagmsg_path != NULL)
4177 preserve_tagmsg = 1;
4182 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4183 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4186 preserve_tagmsg = 1;
4190 err = got_ref_alloc(&ref, refname, tag_id);
4193 preserve_tagmsg = 1;
4197 err = got_ref_write(ref, repo);
4200 preserve_tagmsg = 1;
4204 err = got_object_id_str(&tag_id_str, tag_id);
4207 preserve_tagmsg = 1;
4210 printf("Created tag %s\n", tag_id_str);
4212 if (preserve_tagmsg) {
4213 fprintf(stderr, "%s: tag message preserved in %s\n",
4214 getprogname(), tagmsg_path);
4215 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4216 err = got_error_from_errno2("unlink", tagmsg_path);
4221 free(commit_id_str);
4229 static const struct got_error *
4230 cmd_tag(int argc, char *argv[])
4232 const struct got_error *error = NULL;
4233 struct got_repository *repo = NULL;
4234 struct got_worktree *worktree = NULL;
4235 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4236 char *gitconfig_path = NULL;
4237 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4238 int ch, do_list = 0;
4240 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4243 commit_id_arg = optarg;
4249 repo_path = realpath(optarg, NULL);
4250 if (repo_path == NULL)
4251 return got_error_from_errno2("realpath",
4253 got_path_strip_trailing_slashes(repo_path);
4268 if (commit_id_arg != NULL)
4269 errx(1, "-c option can only be used when creating a tag");
4271 errx(1, "-l and -m options are mutually exclusive");
4274 } else if (argc != 1)
4281 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4285 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4286 "sendfd unveil", NULL) == -1)
4290 cwd = getcwd(NULL, 0);
4292 error = got_error_from_errno("getcwd");
4296 if (repo_path == NULL) {
4297 error = got_worktree_open(&worktree, cwd);
4298 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4304 strdup(got_worktree_get_repo_path(worktree));
4305 if (repo_path == NULL)
4306 error = got_error_from_errno("strdup");
4310 repo_path = strdup(cwd);
4311 if (repo_path == NULL) {
4312 error = got_error_from_errno("strdup");
4319 error = got_repo_open(&repo, repo_path, NULL);
4322 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4325 error = list_tags(repo, worktree);
4327 error = get_gitconfig_path(&gitconfig_path);
4330 error = got_repo_open(&repo, repo_path, gitconfig_path);
4335 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4340 if (commit_id_arg == NULL) {
4341 struct got_reference *head_ref;
4342 struct got_object_id *commit_id;
4343 error = got_ref_open(&head_ref, repo,
4344 worktree ? got_worktree_get_head_ref_name(worktree)
4348 error = got_ref_resolve(&commit_id, repo, head_ref);
4349 got_ref_close(head_ref);
4352 error = got_object_id_str(&commit_id_str, commit_id);
4358 error = add_tag(repo, tag_name,
4359 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4363 got_repo_close(repo);
4365 got_worktree_close(worktree);
4368 free(gitconfig_path);
4369 free(commit_id_str);
4376 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4381 static const struct got_error *
4382 add_progress(void *arg, unsigned char status, const char *path)
4384 while (path[0] == '/')
4386 printf("%c %s\n", status, path);
4390 static const struct got_error *
4391 cmd_add(int argc, char *argv[])
4393 const struct got_error *error = NULL;
4394 struct got_repository *repo = NULL;
4395 struct got_worktree *worktree = NULL;
4397 struct got_pathlist_head paths;
4398 struct got_pathlist_entry *pe;
4399 int ch, can_recurse = 0, no_ignores = 0;
4403 while ((ch = getopt(argc, argv, "IR")) != -1) {
4421 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4428 cwd = getcwd(NULL, 0);
4430 error = got_error_from_errno("getcwd");
4434 error = got_worktree_open(&worktree, cwd);
4438 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4443 error = apply_unveil(got_repo_get_path(repo), 1,
4444 got_worktree_get_root_path(worktree));
4448 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4452 if (!can_recurse && no_ignores) {
4453 error = got_error_msg(GOT_ERR_BAD_PATH,
4454 "disregarding ignores requires -R option");
4462 TAILQ_FOREACH(pe, &paths, entry) {
4463 if (asprintf(&ondisk_path, "%s/%s",
4464 got_worktree_get_root_path(worktree),
4466 error = got_error_from_errno("asprintf");
4469 if (lstat(ondisk_path, &sb) == -1) {
4470 if (errno == ENOENT) {
4474 error = got_error_from_errno2("lstat",
4480 if (S_ISDIR(sb.st_mode)) {
4481 error = got_error_msg(GOT_ERR_BAD_PATH,
4482 "adding directories requires -R option");
4488 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4489 NULL, repo, no_ignores);
4492 got_repo_close(repo);
4494 got_worktree_close(worktree);
4495 TAILQ_FOREACH(pe, &paths, entry)
4496 free((char *)pe->path);
4497 got_pathlist_free(&paths);
4505 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4510 static const struct got_error *
4511 print_remove_status(void *arg, unsigned char status,
4512 unsigned char staged_status, const char *path)
4514 while (path[0] == '/')
4516 if (status == GOT_STATUS_NONEXISTENT)
4518 if (status == staged_status && (status == GOT_STATUS_DELETE))
4519 status = GOT_STATUS_NO_CHANGE;
4520 printf("%c%c %s\n", status, staged_status, path);
4524 static const struct got_error *
4525 cmd_remove(int argc, char *argv[])
4527 const struct got_error *error = NULL;
4528 struct got_worktree *worktree = NULL;
4529 struct got_repository *repo = NULL;
4531 struct got_pathlist_head paths;
4532 struct got_pathlist_entry *pe;
4533 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4537 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4540 delete_local_mods = 1;
4558 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4565 cwd = getcwd(NULL, 0);
4567 error = got_error_from_errno("getcwd");
4570 error = got_worktree_open(&worktree, cwd);
4574 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4579 error = apply_unveil(got_repo_get_path(repo), 1,
4580 got_worktree_get_root_path(worktree));
4584 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4591 TAILQ_FOREACH(pe, &paths, entry) {
4592 if (asprintf(&ondisk_path, "%s/%s",
4593 got_worktree_get_root_path(worktree),
4595 error = got_error_from_errno("asprintf");
4598 if (lstat(ondisk_path, &sb) == -1) {
4599 if (errno == ENOENT) {
4603 error = got_error_from_errno2("lstat",
4609 if (S_ISDIR(sb.st_mode)) {
4610 error = got_error_msg(GOT_ERR_BAD_PATH,
4611 "removing directories requires -R option");
4617 error = got_worktree_schedule_delete(worktree, &paths,
4618 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4621 got_repo_close(repo);
4623 got_worktree_close(worktree);
4624 TAILQ_FOREACH(pe, &paths, entry)
4625 free((char *)pe->path);
4626 got_pathlist_free(&paths);
4634 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4635 "path ...\n", getprogname());
4639 static const struct got_error *
4640 revert_progress(void *arg, unsigned char status, const char *path)
4642 if (status == GOT_STATUS_UNVERSIONED)
4645 while (path[0] == '/')
4647 printf("%c %s\n", status, path);
4651 struct choose_patch_arg {
4652 FILE *patch_script_file;
4656 static const struct got_error *
4657 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4658 int nchanges, const char *action)
4661 size_t linesize = 0;
4665 case GOT_STATUS_ADD:
4666 printf("A %s\n%s this addition? [y/n] ", path, action);
4668 case GOT_STATUS_DELETE:
4669 printf("D %s\n%s this deletion? [y/n] ", path, action);
4671 case GOT_STATUS_MODIFY:
4672 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4673 return got_error_from_errno("fseek");
4674 printf(GOT_COMMIT_SEP_STR);
4675 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4677 if (ferror(patch_file))
4678 return got_error_from_errno("getline");
4679 printf(GOT_COMMIT_SEP_STR);
4680 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4681 path, n, nchanges, action);
4684 return got_error_path(path, GOT_ERR_FILE_STATUS);
4690 static const struct got_error *
4691 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4692 FILE *patch_file, int n, int nchanges)
4694 const struct got_error *err = NULL;
4696 size_t linesize = 0;
4699 struct choose_patch_arg *a = arg;
4701 *choice = GOT_PATCH_CHOICE_NONE;
4703 if (a->patch_script_file) {
4705 err = show_change(status, path, patch_file, n, nchanges,
4709 linelen = getline(&line, &linesize, a->patch_script_file);
4710 if (linelen == -1) {
4711 if (ferror(a->patch_script_file))
4712 return got_error_from_errno("getline");
4715 nl = strchr(line, '\n');
4718 if (strcmp(line, "y") == 0) {
4719 *choice = GOT_PATCH_CHOICE_YES;
4721 } else if (strcmp(line, "n") == 0) {
4722 *choice = GOT_PATCH_CHOICE_NO;
4724 } else if (strcmp(line, "q") == 0 &&
4725 status == GOT_STATUS_MODIFY) {
4726 *choice = GOT_PATCH_CHOICE_QUIT;
4729 printf("invalid response '%s'\n", line);
4734 while (resp != 'y' && resp != 'n' && resp != 'q') {
4735 err = show_change(status, path, patch_file, n, nchanges,
4742 if (status == GOT_STATUS_MODIFY) {
4743 if (resp != 'y' && resp != 'n' && resp != 'q') {
4744 printf("invalid response '%c'\n", resp);
4747 } else if (resp != 'y' && resp != 'n') {
4748 printf("invalid response '%c'\n", resp);
4754 *choice = GOT_PATCH_CHOICE_YES;
4755 else if (resp == 'n')
4756 *choice = GOT_PATCH_CHOICE_NO;
4757 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4758 *choice = GOT_PATCH_CHOICE_QUIT;
4764 static const struct got_error *
4765 cmd_revert(int argc, char *argv[])
4767 const struct got_error *error = NULL;
4768 struct got_worktree *worktree = NULL;
4769 struct got_repository *repo = NULL;
4770 char *cwd = NULL, *path = NULL;
4771 struct got_pathlist_head paths;
4772 struct got_pathlist_entry *pe;
4773 int ch, can_recurse = 0, pflag = 0;
4774 FILE *patch_script_file = NULL;
4775 const char *patch_script_path = NULL;
4776 struct choose_patch_arg cpa;
4780 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4786 patch_script_path = optarg;
4801 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4802 "unveil", NULL) == -1)
4807 if (patch_script_path && !pflag)
4808 errx(1, "-F option can only be used together with -p option");
4810 cwd = getcwd(NULL, 0);
4812 error = got_error_from_errno("getcwd");
4815 error = got_worktree_open(&worktree, cwd);
4819 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4824 if (patch_script_path) {
4825 patch_script_file = fopen(patch_script_path, "r");
4826 if (patch_script_file == NULL) {
4827 error = got_error_from_errno2("fopen",
4832 error = apply_unveil(got_repo_get_path(repo), 1,
4833 got_worktree_get_root_path(worktree));
4837 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4844 TAILQ_FOREACH(pe, &paths, entry) {
4845 if (asprintf(&ondisk_path, "%s/%s",
4846 got_worktree_get_root_path(worktree),
4848 error = got_error_from_errno("asprintf");
4851 if (lstat(ondisk_path, &sb) == -1) {
4852 if (errno == ENOENT) {
4856 error = got_error_from_errno2("lstat",
4862 if (S_ISDIR(sb.st_mode)) {
4863 error = got_error_msg(GOT_ERR_BAD_PATH,
4864 "reverting directories requires -R option");
4870 cpa.patch_script_file = patch_script_file;
4871 cpa.action = "revert";
4872 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4873 pflag ? choose_patch : NULL, &cpa, repo);
4875 if (patch_script_file && fclose(patch_script_file) == EOF &&
4877 error = got_error_from_errno2("fclose", patch_script_path);
4879 got_repo_close(repo);
4881 got_worktree_close(worktree);
4890 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4895 struct collect_commit_logmsg_arg {
4896 const char *cmdline_log;
4898 const char *worktree_path;
4899 const char *branch_name;
4900 const char *repo_path;
4905 static const struct got_error *
4906 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4909 char *initial_content = NULL;
4910 struct got_pathlist_entry *pe;
4911 const struct got_error *err = NULL;
4912 char *template = NULL;
4913 struct collect_commit_logmsg_arg *a = arg;
4917 /* if a message was specified on the command line, just use it */
4918 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4919 len = strlen(a->cmdline_log) + 1;
4920 *logmsg = malloc(len + 1);
4921 if (*logmsg == NULL)
4922 return got_error_from_errno("malloc");
4923 strlcpy(*logmsg, a->cmdline_log, len);
4927 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4928 return got_error_from_errno("asprintf");
4930 if (asprintf(&initial_content,
4931 "\n# changes to be committed on branch %s:\n",
4932 a->branch_name) == -1)
4933 return got_error_from_errno("asprintf");
4935 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4939 dprintf(fd, initial_content);
4941 TAILQ_FOREACH(pe, commitable_paths, entry) {
4942 struct got_commitable *ct = pe->data;
4943 dprintf(fd, "# %c %s\n",
4944 got_commitable_get_status(ct),
4945 got_commitable_get_path(ct));
4949 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4951 free(initial_content);
4954 /* Editor is done; we can now apply unveil(2) */
4956 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4965 static const struct got_error *
4966 cmd_commit(int argc, char *argv[])
4968 const struct got_error *error = NULL;
4969 struct got_worktree *worktree = NULL;
4970 struct got_repository *repo = NULL;
4971 char *cwd = NULL, *id_str = NULL;
4972 struct got_object_id *id = NULL;
4973 const char *logmsg = NULL;
4974 struct collect_commit_logmsg_arg cl_arg;
4975 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4976 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4977 struct got_pathlist_head paths;
4980 cl_arg.logmsg_path = NULL;
4982 while ((ch = getopt(argc, argv, "m:")) != -1) {
4997 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4998 "unveil", NULL) == -1)
5001 cwd = getcwd(NULL, 0);
5003 error = got_error_from_errno("getcwd");
5006 error = got_worktree_open(&worktree, cwd);
5010 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5013 if (rebase_in_progress) {
5014 error = got_error(GOT_ERR_REBASING);
5018 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5023 error = get_gitconfig_path(&gitconfig_path);
5026 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5031 error = get_author(&author, repo);
5036 * unveil(2) traverses exec(2); if an editor is used we have
5037 * to apply unveil after the log message has been written.
5039 if (logmsg == NULL || strlen(logmsg) == 0)
5040 error = get_editor(&editor);
5042 error = apply_unveil(got_repo_get_path(repo), 0,
5043 got_worktree_get_root_path(worktree));
5047 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5051 cl_arg.editor = editor;
5052 cl_arg.cmdline_log = logmsg;
5053 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5054 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5055 if (!histedit_in_progress) {
5056 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5057 error = got_error(GOT_ERR_COMMIT_BRANCH);
5060 cl_arg.branch_name += 11;
5062 cl_arg.repo_path = got_repo_get_path(repo);
5063 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5064 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5066 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5067 cl_arg.logmsg_path != NULL)
5068 preserve_logmsg = 1;
5072 error = got_object_id_str(&id_str, id);
5075 printf("Created commit %s\n", id_str);
5077 if (preserve_logmsg) {
5078 fprintf(stderr, "%s: log message preserved in %s\n",
5079 getprogname(), cl_arg.logmsg_path);
5080 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5082 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5083 free(cl_arg.logmsg_path);
5085 got_repo_close(repo);
5087 got_worktree_close(worktree);
5090 free(gitconfig_path);
5097 usage_cherrypick(void)
5099 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5103 static const struct got_error *
5104 cmd_cherrypick(int argc, char *argv[])
5106 const struct got_error *error = NULL;
5107 struct got_worktree *worktree = NULL;
5108 struct got_repository *repo = NULL;
5109 char *cwd = NULL, *commit_id_str = NULL;
5110 struct got_object_id *commit_id = NULL;
5111 struct got_commit_object *commit = NULL;
5112 struct got_object_qid *pid;
5113 struct got_reference *head_ref = NULL;
5114 int ch, did_something = 0;
5116 while ((ch = getopt(argc, argv, "")) != -1) {
5128 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5129 "unveil", NULL) == -1)
5135 cwd = getcwd(NULL, 0);
5137 error = got_error_from_errno("getcwd");
5140 error = got_worktree_open(&worktree, cwd);
5144 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5149 error = apply_unveil(got_repo_get_path(repo), 0,
5150 got_worktree_get_root_path(worktree));
5154 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5155 GOT_OBJ_TYPE_COMMIT, repo);
5156 if (error != NULL) {
5157 struct got_reference *ref;
5158 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5160 error = got_ref_open(&ref, repo, argv[0], 0);
5163 error = got_ref_resolve(&commit_id, repo, ref);
5168 error = got_object_id_str(&commit_id_str, commit_id);
5172 error = got_ref_open(&head_ref, repo,
5173 got_worktree_get_head_ref_name(worktree), 0);
5177 error = check_same_branch(commit_id, head_ref, NULL, repo);
5179 if (error->code != GOT_ERR_ANCESTRY)
5183 error = got_error(GOT_ERR_SAME_BRANCH);
5187 error = got_object_open_as_commit(&commit, repo, commit_id);
5190 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5191 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5192 commit_id, repo, update_progress, &did_something, check_cancelled,
5198 printf("Merged commit %s\n", commit_id_str);
5201 got_object_commit_close(commit);
5202 free(commit_id_str);
5204 got_ref_close(head_ref);
5206 got_worktree_close(worktree);
5208 got_repo_close(repo);
5215 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5219 static const struct got_error *
5220 cmd_backout(int argc, char *argv[])
5222 const struct got_error *error = NULL;
5223 struct got_worktree *worktree = NULL;
5224 struct got_repository *repo = NULL;
5225 char *cwd = NULL, *commit_id_str = NULL;
5226 struct got_object_id *commit_id = NULL;
5227 struct got_commit_object *commit = NULL;
5228 struct got_object_qid *pid;
5229 struct got_reference *head_ref = NULL;
5230 int ch, did_something = 0;
5232 while ((ch = getopt(argc, argv, "")) != -1) {
5244 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5245 "unveil", NULL) == -1)
5251 cwd = getcwd(NULL, 0);
5253 error = got_error_from_errno("getcwd");
5256 error = got_worktree_open(&worktree, cwd);
5260 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5265 error = apply_unveil(got_repo_get_path(repo), 0,
5266 got_worktree_get_root_path(worktree));
5270 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5271 GOT_OBJ_TYPE_COMMIT, repo);
5272 if (error != NULL) {
5273 struct got_reference *ref;
5274 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5276 error = got_ref_open(&ref, repo, argv[0], 0);
5279 error = got_ref_resolve(&commit_id, repo, ref);
5284 error = got_object_id_str(&commit_id_str, commit_id);
5288 error = got_ref_open(&head_ref, repo,
5289 got_worktree_get_head_ref_name(worktree), 0);
5293 error = check_same_branch(commit_id, head_ref, NULL, repo);
5297 error = got_object_open_as_commit(&commit, repo, commit_id);
5300 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5302 error = got_error(GOT_ERR_ROOT_COMMIT);
5306 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5307 update_progress, &did_something, check_cancelled, NULL);
5312 printf("Backed out commit %s\n", commit_id_str);
5315 got_object_commit_close(commit);
5316 free(commit_id_str);
5318 got_ref_close(head_ref);
5320 got_worktree_close(worktree);
5322 got_repo_close(repo);
5329 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5335 trim_logmsg(char *logmsg, int limit)
5340 len = strlen(logmsg);
5344 nl = strchr(logmsg, '\n');
5349 static const struct got_error *
5350 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5352 const struct got_error *err;
5353 char *logmsg0 = NULL;
5356 err = got_object_commit_get_logmsg(&logmsg0, commit);
5361 while (isspace((unsigned char)s[0]))
5364 *logmsg = strdup(s);
5365 if (*logmsg == NULL) {
5366 err = got_error_from_errno("strdup");
5370 trim_logmsg(*logmsg, limit);
5376 static const struct got_error *
5377 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5379 const struct got_error *err;
5380 struct got_commit_object *commit = NULL;
5381 char *id_str = NULL, *logmsg = NULL;
5383 err = got_object_open_as_commit(&commit, repo, id);
5387 err = got_object_id_str(&id_str, id);
5393 err = get_short_logmsg(&logmsg, 42, commit);
5397 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5400 got_object_commit_close(commit);
5405 static const struct got_error *
5406 show_rebase_progress(struct got_commit_object *commit,
5407 struct got_object_id *old_id, struct got_object_id *new_id)
5409 const struct got_error *err;
5410 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5412 err = got_object_id_str(&old_id_str, old_id);
5417 err = got_object_id_str(&new_id_str, new_id);
5422 old_id_str[12] = '\0';
5424 new_id_str[12] = '\0';
5426 err = get_short_logmsg(&logmsg, 42, commit);
5430 printf("%s -> %s: %s\n", old_id_str,
5431 new_id_str ? new_id_str : "no-op change", logmsg);
5439 static const struct got_error *
5440 rebase_progress(void *arg, unsigned char status, const char *path)
5442 unsigned char *rebase_status = arg;
5444 while (path[0] == '/')
5446 printf("%c %s\n", status, path);
5448 if (*rebase_status == GOT_STATUS_CONFLICT)
5450 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5451 *rebase_status = status;
5455 static const struct got_error *
5456 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5457 struct got_reference *branch, struct got_reference *new_base_branch,
5458 struct got_reference *tmp_branch, struct got_repository *repo)
5460 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5461 return got_worktree_rebase_complete(worktree, fileindex,
5462 new_base_branch, tmp_branch, branch, repo);
5465 static const struct got_error *
5466 rebase_commit(struct got_pathlist_head *merged_paths,
5467 struct got_worktree *worktree, struct got_fileindex *fileindex,
5468 struct got_reference *tmp_branch,
5469 struct got_object_id *commit_id, struct got_repository *repo)
5471 const struct got_error *error;
5472 struct got_commit_object *commit;
5473 struct got_object_id *new_commit_id;
5475 error = got_object_open_as_commit(&commit, repo, commit_id);
5479 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5480 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5482 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5484 error = show_rebase_progress(commit, commit_id, NULL);
5486 error = show_rebase_progress(commit, commit_id, new_commit_id);
5487 free(new_commit_id);
5490 got_object_commit_close(commit);
5494 struct check_path_prefix_arg {
5495 const char *path_prefix;
5500 static const struct got_error *
5501 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5502 struct got_blob_object *blob2, struct got_object_id *id1,
5503 struct got_object_id *id2, const char *path1, const char *path2,
5504 mode_t mode1, mode_t mode2, struct got_repository *repo)
5506 struct check_path_prefix_arg *a = arg;
5508 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5509 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5510 return got_error(a->errcode);
5515 static const struct got_error *
5516 check_path_prefix(struct got_object_id *parent_id,
5517 struct got_object_id *commit_id, const char *path_prefix,
5518 int errcode, struct got_repository *repo)
5520 const struct got_error *err;
5521 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5522 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5523 struct check_path_prefix_arg cpp_arg;
5525 if (got_path_is_root_dir(path_prefix))
5528 err = got_object_open_as_commit(&commit, repo, commit_id);
5532 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5536 err = got_object_open_as_tree(&tree1, repo,
5537 got_object_commit_get_tree_id(parent_commit));
5541 err = got_object_open_as_tree(&tree2, repo,
5542 got_object_commit_get_tree_id(commit));
5546 cpp_arg.path_prefix = path_prefix;
5547 while (cpp_arg.path_prefix[0] == '/')
5548 cpp_arg.path_prefix++;
5549 cpp_arg.len = strlen(cpp_arg.path_prefix);
5550 cpp_arg.errcode = errcode;
5551 err = got_diff_tree(tree1, tree2, "", "", repo,
5552 check_path_prefix_in_diff, &cpp_arg, 0);
5555 got_object_tree_close(tree1);
5557 got_object_tree_close(tree2);
5559 got_object_commit_close(commit);
5561 got_object_commit_close(parent_commit);
5565 static const struct got_error *
5566 collect_commits(struct got_object_id_queue *commits,
5567 struct got_object_id *initial_commit_id,
5568 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5569 const char *path_prefix, int path_prefix_errcode,
5570 struct got_repository *repo)
5572 const struct got_error *err = NULL;
5573 struct got_commit_graph *graph = NULL;
5574 struct got_object_id *parent_id = NULL;
5575 struct got_object_qid *qid;
5576 struct got_object_id *commit_id = initial_commit_id;
5578 err = got_commit_graph_open(&graph, "/", 1);
5582 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5583 check_cancelled, NULL);
5586 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5587 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5588 check_cancelled, NULL);
5590 if (err->code == GOT_ERR_ITER_COMPLETED) {
5591 err = got_error_msg(GOT_ERR_ANCESTRY,
5592 "ran out of commits to rebase before "
5593 "youngest common ancestor commit has "
5598 err = check_path_prefix(parent_id, commit_id,
5599 path_prefix, path_prefix_errcode, repo);
5603 err = got_object_qid_alloc(&qid, commit_id);
5606 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5607 commit_id = parent_id;
5611 got_commit_graph_close(graph);
5615 static const struct got_error *
5616 cmd_rebase(int argc, char *argv[])
5618 const struct got_error *error = NULL;
5619 struct got_worktree *worktree = NULL;
5620 struct got_repository *repo = NULL;
5621 struct got_fileindex *fileindex = NULL;
5623 struct got_reference *branch = NULL;
5624 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5625 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5626 struct got_object_id *resume_commit_id = NULL;
5627 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5628 struct got_commit_object *commit = NULL;
5629 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5630 int histedit_in_progress = 0;
5631 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5632 struct got_object_id_queue commits;
5633 struct got_pathlist_head merged_paths;
5634 const struct got_object_id_queue *parent_ids;
5635 struct got_object_qid *qid, *pid;
5637 SIMPLEQ_INIT(&commits);
5638 TAILQ_INIT(&merged_paths);
5640 while ((ch = getopt(argc, argv, "ac")) != -1) {
5646 continue_rebase = 1;
5658 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5659 "unveil", NULL) == -1)
5662 if (abort_rebase && continue_rebase)
5664 else if (abort_rebase || continue_rebase) {
5667 } else if (argc != 1)
5670 cwd = getcwd(NULL, 0);
5672 error = got_error_from_errno("getcwd");
5675 error = got_worktree_open(&worktree, cwd);
5679 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5684 error = apply_unveil(got_repo_get_path(repo), 0,
5685 got_worktree_get_root_path(worktree));
5689 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5693 if (histedit_in_progress) {
5694 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5698 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5704 if (!rebase_in_progress) {
5705 error = got_error(GOT_ERR_NOT_REBASING);
5708 error = got_worktree_rebase_continue(&resume_commit_id,
5709 &new_base_branch, &tmp_branch, &branch, &fileindex,
5713 printf("Switching work tree to %s\n",
5714 got_ref_get_symref_target(new_base_branch));
5715 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5716 new_base_branch, update_progress, &did_something);
5719 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5720 goto done; /* nothing else to do */
5723 if (continue_rebase) {
5724 if (!rebase_in_progress) {
5725 error = got_error(GOT_ERR_NOT_REBASING);
5728 error = got_worktree_rebase_continue(&resume_commit_id,
5729 &new_base_branch, &tmp_branch, &branch, &fileindex,
5734 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5735 resume_commit_id, repo);
5739 yca_id = got_object_id_dup(resume_commit_id);
5740 if (yca_id == NULL) {
5741 error = got_error_from_errno("got_object_id_dup");
5745 error = got_ref_open(&branch, repo, argv[0], 0);
5750 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5754 if (!continue_rebase) {
5755 struct got_object_id *base_commit_id;
5757 base_commit_id = got_worktree_get_base_commit_id(worktree);
5758 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5759 base_commit_id, branch_head_commit_id, repo,
5760 check_cancelled, NULL);
5763 if (yca_id == NULL) {
5764 error = got_error_msg(GOT_ERR_ANCESTRY,
5765 "specified branch shares no common ancestry "
5766 "with work tree's branch");
5770 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5772 if (error->code != GOT_ERR_ANCESTRY)
5776 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5777 "specified branch resolves to a commit which "
5778 "is already contained in work tree's branch");
5781 error = got_worktree_rebase_prepare(&new_base_branch,
5782 &tmp_branch, &fileindex, worktree, branch, repo);
5787 commit_id = branch_head_commit_id;
5788 error = got_object_open_as_commit(&commit, repo, commit_id);
5792 parent_ids = got_object_commit_get_parent_ids(commit);
5793 pid = SIMPLEQ_FIRST(parent_ids);
5795 if (!continue_rebase) {
5797 error = got_worktree_rebase_abort(worktree, fileindex,
5798 repo, new_base_branch, update_progress,
5802 printf("Rebase of %s aborted\n",
5803 got_ref_get_name(branch));
5805 error = got_error(GOT_ERR_EMPTY_REBASE);
5808 error = collect_commits(&commits, commit_id, pid->id,
5809 yca_id, got_worktree_get_path_prefix(worktree),
5810 GOT_ERR_REBASE_PATH, repo);
5811 got_object_commit_close(commit);
5816 if (SIMPLEQ_EMPTY(&commits)) {
5817 if (continue_rebase) {
5818 error = rebase_complete(worktree, fileindex,
5819 branch, new_base_branch, tmp_branch, repo);
5822 /* Fast-forward the reference of the branch. */
5823 struct got_object_id *new_head_commit_id;
5825 error = got_ref_resolve(&new_head_commit_id, repo,
5829 error = got_object_id_str(&id_str, new_head_commit_id);
5830 printf("Forwarding %s to commit %s\n",
5831 got_ref_get_name(branch), id_str);
5833 error = got_ref_change_ref(branch,
5834 new_head_commit_id);
5841 SIMPLEQ_FOREACH(qid, &commits, entry) {
5842 commit_id = qid->id;
5843 parent_id = pid ? pid->id : yca_id;
5846 error = got_worktree_rebase_merge_files(&merged_paths,
5847 worktree, fileindex, parent_id, commit_id, repo,
5848 rebase_progress, &rebase_status, check_cancelled, NULL);
5852 if (rebase_status == GOT_STATUS_CONFLICT) {
5853 error = show_rebase_merge_conflict(qid->id, repo);
5856 got_worktree_rebase_pathlist_free(&merged_paths);
5860 error = rebase_commit(&merged_paths, worktree, fileindex,
5861 tmp_branch, commit_id, repo);
5862 got_worktree_rebase_pathlist_free(&merged_paths);
5867 if (rebase_status == GOT_STATUS_CONFLICT) {
5868 error = got_worktree_rebase_postpone(worktree, fileindex);
5871 error = got_error_msg(GOT_ERR_CONFLICTS,
5872 "conflicts must be resolved before rebasing can continue");
5874 error = rebase_complete(worktree, fileindex, branch,
5875 new_base_branch, tmp_branch, repo);
5877 got_object_id_queue_free(&commits);
5878 free(branch_head_commit_id);
5879 free(resume_commit_id);
5882 got_object_commit_close(commit);
5884 got_ref_close(branch);
5885 if (new_base_branch)
5886 got_ref_close(new_base_branch);
5888 got_ref_close(tmp_branch);
5890 got_worktree_close(worktree);
5892 got_repo_close(repo);
5897 usage_histedit(void)
5899 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5904 #define GOT_HISTEDIT_PICK 'p'
5905 #define GOT_HISTEDIT_EDIT 'e'
5906 #define GOT_HISTEDIT_FOLD 'f'
5907 #define GOT_HISTEDIT_DROP 'd'
5908 #define GOT_HISTEDIT_MESG 'm'
5910 static struct got_histedit_cmd {
5914 } got_histedit_cmds[] = {
5915 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5916 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5917 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5919 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5920 { GOT_HISTEDIT_MESG, "mesg",
5921 "single-line log message for commit above (open editor if empty)" },
5924 struct got_histedit_list_entry {
5925 TAILQ_ENTRY(got_histedit_list_entry) entry;
5926 struct got_object_id *commit_id;
5927 const struct got_histedit_cmd *cmd;
5930 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5932 static const struct got_error *
5933 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5934 FILE *f, struct got_repository *repo)
5936 const struct got_error *err = NULL;
5937 char *logmsg = NULL, *id_str = NULL;
5938 struct got_commit_object *commit = NULL;
5941 err = got_object_open_as_commit(&commit, repo, commit_id);
5945 err = get_short_logmsg(&logmsg, 34, commit);
5949 err = got_object_id_str(&id_str, commit_id);
5953 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5955 err = got_ferror(f, GOT_ERR_IO);
5958 got_object_commit_close(commit);
5964 static const struct got_error *
5965 histedit_write_commit_list(struct got_object_id_queue *commits,
5966 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5968 const struct got_error *err = NULL;
5969 struct got_object_qid *qid;
5971 if (SIMPLEQ_EMPTY(commits))
5972 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5974 SIMPLEQ_FOREACH(qid, commits, entry) {
5975 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5979 if (edit_logmsg_only) {
5980 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5982 err = got_ferror(f, GOT_ERR_IO);
5991 static const struct got_error *
5992 write_cmd_list(FILE *f, const char *branch_name,
5993 struct got_object_id_queue *commits)
5995 const struct got_error *err = NULL;
5998 struct got_object_qid *qid;
6000 qid = SIMPLEQ_FIRST(commits);
6001 err = got_object_id_str(&id_str, qid->id);
6006 "# Editing the history of branch '%s' starting at\n"
6008 "# Commits will be processed in order from top to "
6009 "bottom of this file.\n", branch_name, id_str);
6011 err = got_ferror(f, GOT_ERR_IO);
6015 n = fprintf(f, "# Available histedit commands:\n");
6017 err = got_ferror(f, GOT_ERR_IO);
6021 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6022 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6023 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6026 err = got_ferror(f, GOT_ERR_IO);
6035 static const struct got_error *
6036 histedit_syntax_error(int lineno)
6038 static char msg[42];
6041 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6043 if (ret == -1 || ret >= sizeof(msg))
6044 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6046 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6049 static const struct got_error *
6050 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6051 char *logmsg, struct got_repository *repo)
6053 const struct got_error *err;
6054 struct got_commit_object *folded_commit = NULL;
6055 char *id_str, *folded_logmsg = NULL;
6057 err = got_object_id_str(&id_str, hle->commit_id);
6061 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6065 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6068 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6069 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6070 folded_logmsg) == -1) {
6071 err = got_error_from_errno("asprintf");
6075 got_object_commit_close(folded_commit);
6077 free(folded_logmsg);
6081 static struct got_histedit_list_entry *
6082 get_folded_commits(struct got_histedit_list_entry *hle)
6084 struct got_histedit_list_entry *prev, *folded = NULL;
6086 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6087 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6088 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6089 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6091 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6097 static const struct got_error *
6098 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6099 struct got_repository *repo)
6101 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6102 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6103 const struct got_error *err = NULL;
6104 struct got_commit_object *commit = NULL;
6106 struct got_histedit_list_entry *folded = NULL;
6108 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6112 folded = get_folded_commits(hle);
6114 while (folded != hle) {
6115 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6116 folded = TAILQ_NEXT(folded, entry);
6119 err = append_folded_commit_msg(&new_msg, folded,
6125 folded = TAILQ_NEXT(folded, entry);
6129 err = got_object_id_str(&id_str, hle->commit_id);
6132 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6135 if (asprintf(&new_msg,
6136 "%s\n# original log message of commit %s: %s",
6137 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6138 err = got_error_from_errno("asprintf");
6144 err = got_object_id_str(&id_str, hle->commit_id);
6148 err = got_opentemp_named_fd(&logmsg_path, &fd,
6149 GOT_TMPDIR_STR "/got-logmsg");
6153 dprintf(fd, logmsg);
6156 err = get_editor(&editor);
6160 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6162 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6164 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6167 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6168 err = got_error_from_errno2("unlink", logmsg_path);
6174 got_object_commit_close(commit);
6178 static const struct got_error *
6179 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6180 FILE *f, struct got_repository *repo)
6182 const struct got_error *err = NULL;
6183 char *line = NULL, *p, *end;
6187 const struct got_histedit_cmd *cmd;
6188 struct got_object_id *commit_id = NULL;
6189 struct got_histedit_list_entry *hle = NULL;
6192 len = getline(&line, &size, f);
6194 const struct got_error *getline_err;
6197 getline_err = got_error_from_errno("getline");
6198 err = got_ferror(f, getline_err->code);
6203 while (isspace((unsigned char)p[0]))
6205 if (p[0] == '#' || p[0] == '\0') {
6211 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6212 cmd = &got_histedit_cmds[i];
6213 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6214 isspace((unsigned char)p[strlen(cmd->name)])) {
6215 p += strlen(cmd->name);
6218 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6223 if (i == nitems(got_histedit_cmds)) {
6224 err = histedit_syntax_error(lineno);
6227 while (isspace((unsigned char)p[0]))
6229 if (cmd->code == GOT_HISTEDIT_MESG) {
6230 if (hle == NULL || hle->logmsg != NULL) {
6231 err = got_error(GOT_ERR_HISTEDIT_CMD);
6235 err = histedit_edit_logmsg(hle, repo);
6239 hle->logmsg = strdup(p);
6240 if (hle->logmsg == NULL) {
6241 err = got_error_from_errno("strdup");
6250 while (end[0] && !isspace((unsigned char)end[0]))
6254 err = got_object_resolve_id_str(&commit_id, repo, p);
6256 /* override error code */
6257 err = histedit_syntax_error(lineno);
6261 hle = malloc(sizeof(*hle));
6263 err = got_error_from_errno("malloc");
6267 hle->commit_id = commit_id;
6272 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6280 static const struct got_error *
6281 histedit_check_script(struct got_histedit_list *histedit_cmds,
6282 struct got_object_id_queue *commits, struct got_repository *repo)
6284 const struct got_error *err = NULL;
6285 struct got_object_qid *qid;
6286 struct got_histedit_list_entry *hle;
6287 static char msg[92];
6290 if (TAILQ_EMPTY(histedit_cmds))
6291 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6292 "histedit script contains no commands");
6293 if (SIMPLEQ_EMPTY(commits))
6294 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6296 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6297 struct got_histedit_list_entry *hle2;
6298 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6301 if (got_object_id_cmp(hle->commit_id,
6302 hle2->commit_id) != 0)
6304 err = got_object_id_str(&id_str, hle->commit_id);
6307 snprintf(msg, sizeof(msg), "commit %s is listed "
6308 "more than once in histedit script", id_str);
6310 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6314 SIMPLEQ_FOREACH(qid, commits, entry) {
6315 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6316 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6320 err = got_object_id_str(&id_str, qid->id);
6323 snprintf(msg, sizeof(msg),
6324 "commit %s missing from histedit script", id_str);
6326 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6330 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6331 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6332 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6333 "last commit in histedit script cannot be folded");
6338 static const struct got_error *
6339 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6340 const char *path, struct got_object_id_queue *commits,
6341 struct got_repository *repo)
6343 const struct got_error *err = NULL;
6347 err = get_editor(&editor);
6351 if (spawn_editor(editor, path) == -1) {
6352 err = got_error_from_errno("failed spawning editor");
6356 f = fopen(path, "r");
6358 err = got_error_from_errno("fopen");
6361 err = histedit_parse_list(histedit_cmds, f, repo);
6365 err = histedit_check_script(histedit_cmds, commits, repo);
6367 if (f && fclose(f) != 0 && err == NULL)
6368 err = got_error_from_errno("fclose");
6373 static const struct got_error *
6374 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6375 struct got_object_id_queue *, const char *, const char *,
6376 struct got_repository *);
6378 static const struct got_error *
6379 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6380 struct got_object_id_queue *commits, const char *branch_name,
6381 int edit_logmsg_only, struct got_repository *repo)
6383 const struct got_error *err;
6387 err = got_opentemp_named(&path, &f, "got-histedit");
6391 err = write_cmd_list(f, branch_name, commits);
6395 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6399 if (edit_logmsg_only) {
6401 err = histedit_parse_list(histedit_cmds, f, repo);
6403 if (fclose(f) != 0) {
6404 err = got_error_from_errno("fclose");
6408 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6410 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6411 err->code != GOT_ERR_HISTEDIT_CMD)
6413 err = histedit_edit_list_retry(histedit_cmds, err,
6414 commits, path, branch_name, repo);
6418 if (f && fclose(f) != 0 && err == NULL)
6419 err = got_error_from_errno("fclose");
6420 if (path && unlink(path) != 0 && err == NULL)
6421 err = got_error_from_errno2("unlink", path);
6426 static const struct got_error *
6427 histedit_save_list(struct got_histedit_list *histedit_cmds,
6428 struct got_worktree *worktree, struct got_repository *repo)
6430 const struct got_error *err = NULL;
6433 struct got_histedit_list_entry *hle;
6434 struct got_commit_object *commit = NULL;
6436 err = got_worktree_get_histedit_script_path(&path, worktree);
6440 f = fopen(path, "w");
6442 err = got_error_from_errno2("fopen", path);
6445 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6446 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6452 int n = fprintf(f, "%c %s\n",
6453 GOT_HISTEDIT_MESG, hle->logmsg);
6455 err = got_ferror(f, GOT_ERR_IO);
6461 if (f && fclose(f) != 0 && err == NULL)
6462 err = got_error_from_errno("fclose");
6465 got_object_commit_close(commit);
6470 histedit_free_list(struct got_histedit_list *histedit_cmds)
6472 struct got_histedit_list_entry *hle;
6474 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6475 TAILQ_REMOVE(histedit_cmds, hle, entry);
6480 static const struct got_error *
6481 histedit_load_list(struct got_histedit_list *histedit_cmds,
6482 const char *path, struct got_repository *repo)
6484 const struct got_error *err = NULL;
6487 f = fopen(path, "r");
6489 err = got_error_from_errno2("fopen", path);
6493 err = histedit_parse_list(histedit_cmds, f, repo);
6495 if (f && fclose(f) != 0 && err == NULL)
6496 err = got_error_from_errno("fclose");
6500 static const struct got_error *
6501 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6502 const struct got_error *edit_err, struct got_object_id_queue *commits,
6503 const char *path, const char *branch_name, struct got_repository *repo)
6505 const struct got_error *err = NULL, *prev_err = edit_err;
6508 while (resp != 'c' && resp != 'r' && resp != 'a') {
6509 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6510 "or (a)bort: ", getprogname(), prev_err->msg);
6515 histedit_free_list(histedit_cmds);
6516 err = histedit_run_editor(histedit_cmds, path, commits,
6519 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6520 err->code != GOT_ERR_HISTEDIT_CMD)
6527 } else if (resp == 'r') {
6528 histedit_free_list(histedit_cmds);
6529 err = histedit_edit_script(histedit_cmds,
6530 commits, branch_name, 0, repo);
6532 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6533 err->code != GOT_ERR_HISTEDIT_CMD)
6540 } else if (resp == 'a') {
6541 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6544 printf("invalid response '%c'\n", resp);
6550 static const struct got_error *
6551 histedit_complete(struct got_worktree *worktree,
6552 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6553 struct got_reference *branch, struct got_repository *repo)
6555 printf("Switching work tree to %s\n",
6556 got_ref_get_symref_target(branch));
6557 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6561 static const struct got_error *
6562 show_histedit_progress(struct got_commit_object *commit,
6563 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6565 const struct got_error *err;
6566 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6568 err = got_object_id_str(&old_id_str, hle->commit_id);
6573 err = got_object_id_str(&new_id_str, new_id);
6578 old_id_str[12] = '\0';
6580 new_id_str[12] = '\0';
6583 logmsg = strdup(hle->logmsg);
6584 if (logmsg == NULL) {
6585 err = got_error_from_errno("strdup");
6588 trim_logmsg(logmsg, 42);
6590 err = get_short_logmsg(&logmsg, 42, commit);
6595 switch (hle->cmd->code) {
6596 case GOT_HISTEDIT_PICK:
6597 case GOT_HISTEDIT_EDIT:
6598 printf("%s -> %s: %s\n", old_id_str,
6599 new_id_str ? new_id_str : "no-op change", logmsg);
6601 case GOT_HISTEDIT_DROP:
6602 case GOT_HISTEDIT_FOLD:
6603 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6615 static const struct got_error *
6616 histedit_commit(struct got_pathlist_head *merged_paths,
6617 struct got_worktree *worktree, struct got_fileindex *fileindex,
6618 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6619 struct got_repository *repo)
6621 const struct got_error *err;
6622 struct got_commit_object *commit;
6623 struct got_object_id *new_commit_id;
6625 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6626 && hle->logmsg == NULL) {
6627 err = histedit_edit_logmsg(hle, repo);
6632 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6636 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6637 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6640 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6642 err = show_histedit_progress(commit, hle, NULL);
6644 err = show_histedit_progress(commit, hle, new_commit_id);
6645 free(new_commit_id);
6648 got_object_commit_close(commit);
6652 static const struct got_error *
6653 histedit_skip_commit(struct got_histedit_list_entry *hle,
6654 struct got_worktree *worktree, struct got_repository *repo)
6656 const struct got_error *error;
6657 struct got_commit_object *commit;
6659 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6664 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6668 error = show_histedit_progress(commit, hle, NULL);
6669 got_object_commit_close(commit);
6673 static const struct got_error *
6674 check_local_changes(void *arg, unsigned char status,
6675 unsigned char staged_status, const char *path,
6676 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6677 struct got_object_id *commit_id, int dirfd, const char *de_name)
6679 int *have_local_changes = arg;
6682 case GOT_STATUS_ADD:
6683 case GOT_STATUS_DELETE:
6684 case GOT_STATUS_MODIFY:
6685 case GOT_STATUS_CONFLICT:
6686 *have_local_changes = 1;
6687 return got_error(GOT_ERR_CANCELLED);
6692 switch (staged_status) {
6693 case GOT_STATUS_ADD:
6694 case GOT_STATUS_DELETE:
6695 case GOT_STATUS_MODIFY:
6696 *have_local_changes = 1;
6697 return got_error(GOT_ERR_CANCELLED);
6705 static const struct got_error *
6706 cmd_histedit(int argc, char *argv[])
6708 const struct got_error *error = NULL;
6709 struct got_worktree *worktree = NULL;
6710 struct got_fileindex *fileindex = NULL;
6711 struct got_repository *repo = NULL;
6713 struct got_reference *branch = NULL;
6714 struct got_reference *tmp_branch = NULL;
6715 struct got_object_id *resume_commit_id = NULL;
6716 struct got_object_id *base_commit_id = NULL;
6717 struct got_object_id *head_commit_id = NULL;
6718 struct got_commit_object *commit = NULL;
6719 int ch, rebase_in_progress = 0, did_something;
6720 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6721 int edit_logmsg_only = 0;
6722 const char *edit_script_path = NULL;
6723 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6724 struct got_object_id_queue commits;
6725 struct got_pathlist_head merged_paths;
6726 const struct got_object_id_queue *parent_ids;
6727 struct got_object_qid *pid;
6728 struct got_histedit_list histedit_cmds;
6729 struct got_histedit_list_entry *hle;
6731 SIMPLEQ_INIT(&commits);
6732 TAILQ_INIT(&histedit_cmds);
6733 TAILQ_INIT(&merged_paths);
6735 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6744 edit_script_path = optarg;
6747 edit_logmsg_only = 1;
6759 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6760 "unveil", NULL) == -1)
6763 if (abort_edit && continue_edit)
6764 errx(1, "histedit's -a and -c options are mutually exclusive");
6765 if (edit_script_path && edit_logmsg_only)
6766 errx(1, "histedit's -F and -m options are mutually exclusive");
6767 if (abort_edit && edit_logmsg_only)
6768 errx(1, "histedit's -a and -m options are mutually exclusive");
6769 if (continue_edit && edit_logmsg_only)
6770 errx(1, "histedit's -c and -m options are mutually exclusive");
6775 * This command cannot apply unveil(2) in all cases because the
6776 * user may choose to run an editor to edit the histedit script
6777 * and to edit individual commit log messages.
6778 * unveil(2) traverses exec(2); if an editor is used we have to
6779 * apply unveil after edit script and log messages have been written.
6780 * XXX TODO: Make use of unveil(2) where possible.
6783 cwd = getcwd(NULL, 0);
6785 error = got_error_from_errno("getcwd");
6788 error = got_worktree_open(&worktree, cwd);
6792 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6797 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6800 if (rebase_in_progress) {
6801 error = got_error(GOT_ERR_REBASING);
6805 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6809 if (edit_in_progress && edit_logmsg_only) {
6810 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6811 "histedit operation is in progress in this "
6812 "work tree and must be continued or aborted "
6813 "before the -m option can be used");
6817 if (edit_in_progress && abort_edit) {
6818 error = got_worktree_histedit_continue(&resume_commit_id,
6819 &tmp_branch, &branch, &base_commit_id, &fileindex,
6823 printf("Switching work tree to %s\n",
6824 got_ref_get_symref_target(branch));
6825 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6826 branch, base_commit_id, update_progress, &did_something);
6829 printf("Histedit of %s aborted\n",
6830 got_ref_get_symref_target(branch));
6831 goto done; /* nothing else to do */
6832 } else if (abort_edit) {
6833 error = got_error(GOT_ERR_NOT_HISTEDIT);
6837 if (continue_edit) {
6840 if (!edit_in_progress) {
6841 error = got_error(GOT_ERR_NOT_HISTEDIT);
6845 error = got_worktree_get_histedit_script_path(&path, worktree);
6849 error = histedit_load_list(&histedit_cmds, path, repo);
6854 error = got_worktree_histedit_continue(&resume_commit_id,
6855 &tmp_branch, &branch, &base_commit_id, &fileindex,
6860 error = got_ref_resolve(&head_commit_id, repo, branch);
6864 error = got_object_open_as_commit(&commit, repo,
6868 parent_ids = got_object_commit_get_parent_ids(commit);
6869 pid = SIMPLEQ_FIRST(parent_ids);
6871 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6874 error = collect_commits(&commits, head_commit_id, pid->id,
6875 base_commit_id, got_worktree_get_path_prefix(worktree),
6876 GOT_ERR_HISTEDIT_PATH, repo);
6877 got_object_commit_close(commit);
6882 if (edit_in_progress) {
6883 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6887 error = got_ref_open(&branch, repo,
6888 got_worktree_get_head_ref_name(worktree), 0);
6892 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6893 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6894 "will not edit commit history of a branch outside "
6895 "the \"refs/heads/\" reference namespace");
6899 error = got_ref_resolve(&head_commit_id, repo, branch);
6900 got_ref_close(branch);
6905 error = got_object_open_as_commit(&commit, repo,
6909 parent_ids = got_object_commit_get_parent_ids(commit);
6910 pid = SIMPLEQ_FIRST(parent_ids);
6912 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6915 error = collect_commits(&commits, head_commit_id, pid->id,
6916 got_worktree_get_base_commit_id(worktree),
6917 got_worktree_get_path_prefix(worktree),
6918 GOT_ERR_HISTEDIT_PATH, repo);
6919 got_object_commit_close(commit);
6924 if (SIMPLEQ_EMPTY(&commits)) {
6925 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6929 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6930 &base_commit_id, &fileindex, worktree, repo);
6934 if (edit_script_path) {
6935 error = histedit_load_list(&histedit_cmds,
6936 edit_script_path, repo);
6938 got_worktree_histedit_abort(worktree, fileindex,
6939 repo, branch, base_commit_id,
6940 update_progress, &did_something);
6944 const char *branch_name;
6945 branch_name = got_ref_get_symref_target(branch);
6946 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6948 error = histedit_edit_script(&histedit_cmds, &commits,
6949 branch_name, edit_logmsg_only, repo);
6951 got_worktree_histedit_abort(worktree, fileindex,
6952 repo, branch, base_commit_id,
6953 update_progress, &did_something);
6959 error = histedit_save_list(&histedit_cmds, worktree,
6962 got_worktree_histedit_abort(worktree, fileindex,
6963 repo, branch, base_commit_id,
6964 update_progress, &did_something);
6970 error = histedit_check_script(&histedit_cmds, &commits, repo);
6974 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6975 if (resume_commit_id) {
6976 if (got_object_id_cmp(hle->commit_id,
6977 resume_commit_id) != 0)
6980 resume_commit_id = NULL;
6981 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6982 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6983 error = histedit_skip_commit(hle, worktree,
6988 struct got_pathlist_head paths;
6989 int have_changes = 0;
6992 error = got_pathlist_append(&paths, "", NULL);
6995 error = got_worktree_status(worktree, &paths,
6996 repo, check_local_changes, &have_changes,
6997 check_cancelled, NULL);
6998 got_pathlist_free(&paths);
7000 if (error->code != GOT_ERR_CANCELLED)
7002 if (sigint_received || sigpipe_received)
7006 error = histedit_commit(NULL, worktree,
7007 fileindex, tmp_branch, hle, repo);
7011 error = got_object_open_as_commit(
7012 &commit, repo, hle->commit_id);
7015 error = show_histedit_progress(commit,
7017 got_object_commit_close(commit);
7026 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7027 error = histedit_skip_commit(hle, worktree, repo);
7033 error = got_object_open_as_commit(&commit, repo,
7037 parent_ids = got_object_commit_get_parent_ids(commit);
7038 pid = SIMPLEQ_FIRST(parent_ids);
7040 error = got_worktree_histedit_merge_files(&merged_paths,
7041 worktree, fileindex, pid->id, hle->commit_id, repo,
7042 rebase_progress, &rebase_status, check_cancelled, NULL);
7045 got_object_commit_close(commit);
7048 if (rebase_status == GOT_STATUS_CONFLICT) {
7049 error = show_rebase_merge_conflict(hle->commit_id,
7053 got_worktree_rebase_pathlist_free(&merged_paths);
7057 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7059 error = got_object_id_str(&id_str, hle->commit_id);
7062 printf("Stopping histedit for amending commit %s\n",
7065 got_worktree_rebase_pathlist_free(&merged_paths);
7066 error = got_worktree_histedit_postpone(worktree,
7071 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7072 error = histedit_skip_commit(hle, worktree, repo);
7078 error = histedit_commit(&merged_paths, worktree, fileindex,
7079 tmp_branch, hle, repo);
7080 got_worktree_rebase_pathlist_free(&merged_paths);
7085 if (rebase_status == GOT_STATUS_CONFLICT) {
7086 error = got_worktree_histedit_postpone(worktree, fileindex);
7089 error = got_error_msg(GOT_ERR_CONFLICTS,
7090 "conflicts must be resolved before histedit can continue");
7092 error = histedit_complete(worktree, fileindex, tmp_branch,
7095 got_object_id_queue_free(&commits);
7096 histedit_free_list(&histedit_cmds);
7097 free(head_commit_id);
7098 free(base_commit_id);
7099 free(resume_commit_id);
7101 got_object_commit_close(commit);
7103 got_ref_close(branch);
7105 got_ref_close(tmp_branch);
7107 got_worktree_close(worktree);
7109 got_repo_close(repo);
7114 usage_integrate(void)
7116 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7120 static const struct got_error *
7121 cmd_integrate(int argc, char *argv[])
7123 const struct got_error *error = NULL;
7124 struct got_repository *repo = NULL;
7125 struct got_worktree *worktree = NULL;
7126 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7127 const char *branch_arg = NULL;
7128 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7129 struct got_fileindex *fileindex = NULL;
7130 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7131 int ch, did_something = 0;
7133 while ((ch = getopt(argc, argv, "")) != -1) {
7146 branch_arg = argv[0];
7148 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7149 "unveil", NULL) == -1)
7152 cwd = getcwd(NULL, 0);
7154 error = got_error_from_errno("getcwd");
7158 error = got_worktree_open(&worktree, cwd);
7162 error = check_rebase_or_histedit_in_progress(worktree);
7166 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7171 error = apply_unveil(got_repo_get_path(repo), 0,
7172 got_worktree_get_root_path(worktree));
7176 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7177 error = got_error_from_errno("asprintf");
7181 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7182 &base_branch_ref, worktree, refname, repo);
7186 refname = strdup(got_ref_get_name(branch_ref));
7187 if (refname == NULL) {
7188 error = got_error_from_errno("strdup");
7189 got_worktree_integrate_abort(worktree, fileindex, repo,
7190 branch_ref, base_branch_ref);
7193 base_refname = strdup(got_ref_get_name(base_branch_ref));
7194 if (base_refname == NULL) {
7195 error = got_error_from_errno("strdup");
7196 got_worktree_integrate_abort(worktree, fileindex, repo,
7197 branch_ref, base_branch_ref);
7201 error = got_ref_resolve(&commit_id, repo, branch_ref);
7205 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7209 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7210 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7211 "specified branch has already been integrated");
7212 got_worktree_integrate_abort(worktree, fileindex, repo,
7213 branch_ref, base_branch_ref);
7217 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7219 if (error->code == GOT_ERR_ANCESTRY)
7220 error = got_error(GOT_ERR_REBASE_REQUIRED);
7221 got_worktree_integrate_abort(worktree, fileindex, repo,
7222 branch_ref, base_branch_ref);
7226 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7227 branch_ref, base_branch_ref, update_progress, &did_something,
7228 check_cancelled, NULL);
7232 printf("Integrated %s into %s\n", refname, base_refname);
7235 got_repo_close(repo);
7237 got_worktree_close(worktree);
7239 free(base_commit_id);
7249 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7250 "[file-path ...]\n",
7255 static const struct got_error *
7256 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7257 const char *path, struct got_object_id *blob_id,
7258 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7259 int dirfd, const char *de_name)
7261 const struct got_error *err = NULL;
7262 char *id_str = NULL;
7264 if (staged_status != GOT_STATUS_ADD &&
7265 staged_status != GOT_STATUS_MODIFY &&
7266 staged_status != GOT_STATUS_DELETE)
7269 if (staged_status == GOT_STATUS_ADD ||
7270 staged_status == GOT_STATUS_MODIFY)
7271 err = got_object_id_str(&id_str, staged_blob_id);
7273 err = got_object_id_str(&id_str, blob_id);
7277 printf("%s %c %s\n", id_str, staged_status, path);
7282 static const struct got_error *
7283 cmd_stage(int argc, char *argv[])
7285 const struct got_error *error = NULL;
7286 struct got_repository *repo = NULL;
7287 struct got_worktree *worktree = NULL;
7289 struct got_pathlist_head paths;
7290 struct got_pathlist_entry *pe;
7291 int ch, list_stage = 0, pflag = 0;
7292 FILE *patch_script_file = NULL;
7293 const char *patch_script_path = NULL;
7294 struct choose_patch_arg cpa;
7298 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7307 patch_script_path = optarg;
7319 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7320 "unveil", NULL) == -1)
7323 if (list_stage && (pflag || patch_script_path))
7324 errx(1, "-l option cannot be used with other options");
7325 if (patch_script_path && !pflag)
7326 errx(1, "-F option can only be used together with -p option");
7328 cwd = getcwd(NULL, 0);
7330 error = got_error_from_errno("getcwd");
7334 error = got_worktree_open(&worktree, cwd);
7338 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7343 if (patch_script_path) {
7344 patch_script_file = fopen(patch_script_path, "r");
7345 if (patch_script_file == NULL) {
7346 error = got_error_from_errno2("fopen",
7351 error = apply_unveil(got_repo_get_path(repo), 0,
7352 got_worktree_get_root_path(worktree));
7356 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7361 error = got_worktree_status(worktree, &paths, repo,
7362 print_stage, NULL, check_cancelled, NULL);
7364 cpa.patch_script_file = patch_script_file;
7365 cpa.action = "stage";
7366 error = got_worktree_stage(worktree, &paths,
7367 pflag ? NULL : print_status, NULL,
7368 pflag ? choose_patch : NULL, &cpa, repo);
7371 if (patch_script_file && fclose(patch_script_file) == EOF &&
7373 error = got_error_from_errno2("fclose", patch_script_path);
7375 got_repo_close(repo);
7377 got_worktree_close(worktree);
7378 TAILQ_FOREACH(pe, &paths, entry)
7379 free((char *)pe->path);
7380 got_pathlist_free(&paths);
7388 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7389 "[file-path ...]\n",
7395 static const struct got_error *
7396 cmd_unstage(int argc, char *argv[])
7398 const struct got_error *error = NULL;
7399 struct got_repository *repo = NULL;
7400 struct got_worktree *worktree = NULL;
7402 struct got_pathlist_head paths;
7403 struct got_pathlist_entry *pe;
7404 int ch, did_something = 0, pflag = 0;
7405 FILE *patch_script_file = NULL;
7406 const char *patch_script_path = NULL;
7407 struct choose_patch_arg cpa;
7411 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7417 patch_script_path = optarg;
7429 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7430 "unveil", NULL) == -1)
7433 if (patch_script_path && !pflag)
7434 errx(1, "-F option can only be used together with -p option");
7436 cwd = getcwd(NULL, 0);
7438 error = got_error_from_errno("getcwd");
7442 error = got_worktree_open(&worktree, cwd);
7446 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7451 if (patch_script_path) {
7452 patch_script_file = fopen(patch_script_path, "r");
7453 if (patch_script_file == NULL) {
7454 error = got_error_from_errno2("fopen",
7460 error = apply_unveil(got_repo_get_path(repo), 0,
7461 got_worktree_get_root_path(worktree));
7465 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7469 cpa.patch_script_file = patch_script_file;
7470 cpa.action = "unstage";
7471 error = got_worktree_unstage(worktree, &paths, update_progress,
7472 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7474 if (patch_script_file && fclose(patch_script_file) == EOF &&
7476 error = got_error_from_errno2("fclose", patch_script_path);
7478 got_repo_close(repo);
7480 got_worktree_close(worktree);
7481 TAILQ_FOREACH(pe, &paths, entry)
7482 free((char *)pe->path);
7483 got_pathlist_free(&paths);
7491 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7492 "arg1 [arg2 ...]\n", getprogname());
7496 static const struct got_error *
7497 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7499 const struct got_error *err;
7500 struct got_blob_object *blob;
7502 err = got_object_open_as_blob(&blob, repo, id, 8192);
7506 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7507 got_object_blob_close(blob);
7511 static const struct got_error *
7512 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7514 const struct got_error *err;
7515 struct got_tree_object *tree;
7518 err = got_object_open_as_tree(&tree, repo, id);
7522 nentries = got_object_tree_get_nentries(tree);
7523 for (i = 0; i < nentries; i++) {
7524 struct got_tree_entry *te;
7526 if (sigint_received || sigpipe_received)
7528 te = got_object_tree_get_entry(tree, i);
7529 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7532 fprintf(outfile, "%s %.7o %s\n", id_str,
7533 got_tree_entry_get_mode(te),
7534 got_tree_entry_get_name(te));
7538 got_object_tree_close(tree);
7542 static const struct got_error *
7543 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7545 const struct got_error *err;
7546 struct got_commit_object *commit;
7547 const struct got_object_id_queue *parent_ids;
7548 struct got_object_qid *pid;
7549 char *id_str = NULL;
7550 const char *logmsg = NULL;
7552 err = got_object_open_as_commit(&commit, repo, id);
7556 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7560 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7561 parent_ids = got_object_commit_get_parent_ids(commit);
7562 fprintf(outfile, "numparents %d\n",
7563 got_object_commit_get_nparents(commit));
7564 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7566 err = got_object_id_str(&pid_str, pid->id);
7569 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7572 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7573 got_object_commit_get_author(commit),
7574 got_object_commit_get_author_time(commit));
7576 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7577 got_object_commit_get_author(commit),
7578 got_object_commit_get_committer_time(commit));
7580 logmsg = got_object_commit_get_logmsg_raw(commit);
7581 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7582 fprintf(outfile, "%s", logmsg);
7585 got_object_commit_close(commit);
7589 static const struct got_error *
7590 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7592 const struct got_error *err;
7593 struct got_tag_object *tag;
7594 char *id_str = NULL;
7595 const char *tagmsg = NULL;
7597 err = got_object_open_as_tag(&tag, repo, id);
7601 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7605 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7607 switch (got_object_tag_get_object_type(tag)) {
7608 case GOT_OBJ_TYPE_BLOB:
7609 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7610 GOT_OBJ_LABEL_BLOB);
7612 case GOT_OBJ_TYPE_TREE:
7613 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7614 GOT_OBJ_LABEL_TREE);
7616 case GOT_OBJ_TYPE_COMMIT:
7617 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7618 GOT_OBJ_LABEL_COMMIT);
7620 case GOT_OBJ_TYPE_TAG:
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7628 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7629 got_object_tag_get_name(tag));
7631 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7632 got_object_tag_get_tagger(tag),
7633 got_object_tag_get_tagger_time(tag));
7635 tagmsg = got_object_tag_get_message(tag);
7636 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7637 fprintf(outfile, "%s", tagmsg);
7640 got_object_tag_close(tag);
7644 static const struct got_error *
7645 cmd_cat(int argc, char *argv[])
7647 const struct got_error *error;
7648 struct got_repository *repo = NULL;
7649 struct got_worktree *worktree = NULL;
7650 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7651 const char *commit_id_str = NULL;
7652 struct got_object_id *id = NULL, *commit_id = NULL;
7653 int ch, obj_type, i, force_path = 0;
7656 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7661 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7664 commit_id_str = optarg;
7667 repo_path = realpath(optarg, NULL);
7668 if (repo_path == NULL)
7669 return got_error_from_errno2("realpath",
7671 got_path_strip_trailing_slashes(repo_path);
7685 cwd = getcwd(NULL, 0);
7687 error = got_error_from_errno("getcwd");
7690 error = got_worktree_open(&worktree, cwd);
7691 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7694 if (repo_path == NULL) {
7696 got_worktree_get_repo_path(worktree));
7697 if (repo_path == NULL) {
7698 error = got_error_from_errno("strdup");
7704 if (repo_path == NULL) {
7705 repo_path = getcwd(NULL, 0);
7706 if (repo_path == NULL)
7707 return got_error_from_errno("getcwd");
7710 error = got_repo_open(&repo, repo_path, NULL);
7715 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7719 if (commit_id_str == NULL)
7720 commit_id_str = GOT_REF_HEAD;
7721 error = got_repo_match_object_id(&commit_id, NULL,
7722 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7726 for (i = 0; i < argc; i++) {
7728 error = got_object_id_by_path(&id, repo, commit_id,
7733 error = got_repo_match_object_id(&id, &label, argv[i],
7734 GOT_OBJ_TYPE_ANY, 0, repo);
7736 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7737 error->code != GOT_ERR_NOT_REF)
7739 error = got_object_id_by_path(&id, repo,
7740 commit_id, argv[i]);
7746 error = got_object_get_type(&obj_type, repo, id);
7751 case GOT_OBJ_TYPE_BLOB:
7752 error = cat_blob(id, repo, stdout);
7754 case GOT_OBJ_TYPE_TREE:
7755 error = cat_tree(id, repo, stdout);
7757 case GOT_OBJ_TYPE_COMMIT:
7758 error = cat_commit(id, repo, stdout);
7760 case GOT_OBJ_TYPE_TAG:
7761 error = cat_tag(id, repo, stdout);
7764 error = got_error(GOT_ERR_OBJ_TYPE);
7779 got_worktree_close(worktree);
7781 const struct got_error *repo_error;
7782 repo_error = got_repo_close(repo);