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 nobjects_total, int nobjects_indexed)
976 int *did_something = arg;
977 char scaled[FMT_SCALED_STRSIZE];
980 printf("\rserver: %s", message);
982 } else if (packfile_size > 0 || nobjects_indexed > 0) {
983 printf("\rfetching...");
984 if (fmt_scaled(packfile_size, scaled) == 0)
985 printf(" %*s", FMT_SCALED_STRSIZE, scaled);
986 if (nobjects_indexed > 0)
987 printf(" indexed %d/%d objects", nobjects_indexed,
995 static const struct got_error *
996 cmd_clone(int argc, char *argv[])
998 const struct got_error *err = NULL;
999 const char *uri, *branch_filter, *dirname;
1000 char *proto, *host, *port, *repo_name, *server_path;
1001 char *default_destdir = NULL, *id_str = NULL;
1002 const char *repo_path;
1003 struct got_repository *repo = NULL;
1004 struct got_pathlist_head refs, symrefs;
1005 struct got_pathlist_entry *pe;
1006 struct got_object_id *pack_hash = NULL;
1007 int ch, fetchfd = -1;
1008 int did_something = 0;
1011 TAILQ_INIT(&symrefs);
1013 while ((ch = getopt(argc, argv, "b:")) != -1) {
1016 branch_filter = optarg;
1033 err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1034 &repo_name, argv[0]);
1038 err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1042 if (dirname == NULL) {
1043 if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1044 err = got_error_from_errno("asprintf");
1047 repo_path = default_destdir;
1049 repo_path = dirname;
1051 err = got_path_mkdir(repo_path);
1055 err = got_repo_init(repo_path);
1059 err = got_repo_open(&repo, repo_path, NULL);
1063 err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1064 repo, fetch_progress, &did_something);
1070 err = got_object_id_str(&id_str, pack_hash);
1073 printf("Fetched %s.pack\n", id_str);
1076 /* Set up references provided with the pack file. */
1077 TAILQ_FOREACH(pe, &refs, entry) {
1078 const char *refname = pe->path;
1079 struct got_object_id *id = pe->data;
1080 struct got_reference *ref;
1082 err = got_object_id_str(&id_str, id);
1086 err = got_ref_alloc(&ref, refname, id);
1092 printf("%s: %s\n", got_ref_get_name(ref), id_str);
1094 err = got_ref_write(ref, repo);
1100 /* Set the HEAD reference if the server provided one. */
1101 TAILQ_FOREACH(pe, &symrefs, entry) {
1102 struct got_reference *symref, *target_ref;
1103 const char *refname = pe->path;
1104 const char *target = pe->data;
1106 if (strcmp(refname, GOT_REF_HEAD) != 0)
1109 err = got_ref_open(&target_ref, repo, target, 0);
1111 if (err->code == GOT_ERR_NOT_REF)
1116 err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1117 got_ref_close(target_ref);
1121 printf("Setting %s to %s\n", GOT_REF_HEAD,
1122 got_ref_get_symref_target(symref));
1124 err = got_ref_write(symref, repo);
1125 got_ref_close(symref);
1130 if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1131 err = got_error_from_errno("close");
1133 got_repo_close(repo);
1134 TAILQ_FOREACH(pe, &refs, entry) {
1135 free((void *)pe->path);
1138 got_pathlist_free(&refs);
1139 TAILQ_FOREACH(pe, &symrefs, entry) {
1140 free((void *)pe->path);
1143 got_pathlist_free(&symrefs);
1150 free(default_destdir);
1154 static const struct got_error *
1155 checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1157 static char msg[512];
1158 const char *branch_name;
1160 if (got_ref_is_symbolic(ref))
1161 branch_name = got_ref_get_symref_target(ref);
1163 branch_name = got_ref_get_name(ref);
1165 if (strncmp("refs/heads/", branch_name, 11) == 0)
1168 snprintf(msg, sizeof(msg),
1169 "target commit is not contained in branch '%s'; "
1170 "the branch to use must be specified with -b; "
1171 "if necessary a new branch can be created for "
1172 "this commit with 'got branch -c %s BRANCH_NAME'",
1173 branch_name, commit_id_str);
1175 return got_error_msg(GOT_ERR_ANCESTRY, msg);
1178 static const struct got_error *
1179 cmd_checkout(int argc, char *argv[])
1181 const struct got_error *error = NULL;
1182 struct got_repository *repo = NULL;
1183 struct got_reference *head_ref = NULL;
1184 struct got_worktree *worktree = NULL;
1185 char *repo_path = NULL;
1186 char *worktree_path = NULL;
1187 const char *path_prefix = "";
1188 const char *branch_name = GOT_REF_HEAD;
1189 char *commit_id_str = NULL;
1190 int ch, same_path_prefix, allow_nonempty = 0;
1191 struct got_pathlist_head paths;
1192 struct got_checkout_progress_arg cpa;
1196 while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1199 branch_name = optarg;
1202 commit_id_str = strdup(optarg);
1203 if (commit_id_str == NULL)
1204 return got_error_from_errno("strdup");
1210 path_prefix = optarg;
1222 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1223 "unveil", NULL) == -1)
1227 char *cwd, *base, *dotgit;
1228 repo_path = realpath(argv[0], NULL);
1229 if (repo_path == NULL)
1230 return got_error_from_errno2("realpath", argv[0]);
1231 cwd = getcwd(NULL, 0);
1233 error = got_error_from_errno("getcwd");
1236 if (path_prefix[0]) {
1237 base = basename(path_prefix);
1239 error = got_error_from_errno2("basename",
1244 base = basename(repo_path);
1246 error = got_error_from_errno2("basename",
1251 dotgit = strstr(base, ".git");
1254 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1255 error = got_error_from_errno("asprintf");
1260 } else if (argc == 2) {
1261 repo_path = realpath(argv[0], NULL);
1262 if (repo_path == NULL) {
1263 error = got_error_from_errno2("realpath", argv[0]);
1266 worktree_path = realpath(argv[1], NULL);
1267 if (worktree_path == NULL) {
1268 if (errno != ENOENT) {
1269 error = got_error_from_errno2("realpath",
1273 worktree_path = strdup(argv[1]);
1274 if (worktree_path == NULL) {
1275 error = got_error_from_errno("strdup");
1282 got_path_strip_trailing_slashes(repo_path);
1283 got_path_strip_trailing_slashes(worktree_path);
1285 error = got_repo_open(&repo, repo_path, NULL);
1289 /* Pre-create work tree path for unveil(2) */
1290 error = got_path_mkdir(worktree_path);
1292 if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1293 !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1295 if (!allow_nonempty &&
1296 !got_path_dir_is_empty(worktree_path)) {
1297 error = got_error_path(worktree_path,
1298 GOT_ERR_DIR_NOT_EMPTY);
1303 error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1307 error = got_ref_open(&head_ref, repo, branch_name, 0);
1311 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1312 if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1315 error = got_worktree_open(&worktree, worktree_path);
1319 error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1323 if (!same_path_prefix) {
1324 error = got_error(GOT_ERR_PATH_PREFIX);
1328 if (commit_id_str) {
1329 struct got_object_id *commit_id;
1330 error = got_repo_match_object_id(&commit_id, NULL,
1331 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1334 error = check_linear_ancestry(commit_id,
1335 got_worktree_get_base_commit_id(worktree), 0, repo);
1336 if (error != NULL) {
1338 if (error->code == GOT_ERR_ANCESTRY) {
1339 error = checkout_ancestry_error(
1340 head_ref, commit_id_str);
1344 error = check_same_branch(commit_id, head_ref, NULL, repo);
1346 if (error->code == GOT_ERR_ANCESTRY) {
1347 error = checkout_ancestry_error(
1348 head_ref, commit_id_str);
1352 error = got_worktree_set_base_commit_id(worktree, repo,
1359 error = got_pathlist_append(&paths, "", NULL);
1362 cpa.worktree_path = worktree_path;
1363 cpa.had_base_commit_ref_error = 0;
1364 error = got_worktree_checkout_files(worktree, &paths, repo,
1365 checkout_progress, &cpa, check_cancelled, NULL);
1369 printf("Now shut up and hack\n");
1370 if (cpa.had_base_commit_ref_error)
1371 show_worktree_base_ref_warning();
1373 got_pathlist_free(&paths);
1374 free(commit_id_str);
1376 free(worktree_path);
1383 fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1388 static const struct got_error *
1389 update_progress(void *arg, unsigned char status, const char *path)
1391 int *did_something = arg;
1393 if (status == GOT_STATUS_EXISTS ||
1394 status == GOT_STATUS_BASE_REF_ERR)
1399 /* Base commit bump happens silently. */
1400 if (status == GOT_STATUS_BUMP_BASE)
1403 while (path[0] == '/')
1405 printf("%c %s\n", status, path);
1409 static const struct got_error *
1410 switch_head_ref(struct got_reference *head_ref,
1411 struct got_object_id *commit_id, struct got_worktree *worktree,
1412 struct got_repository *repo)
1414 const struct got_error *err = NULL;
1416 int ref_has_moved = 0;
1418 /* Trivial case: switching between two different references. */
1419 if (strcmp(got_ref_get_name(head_ref),
1420 got_worktree_get_head_ref_name(worktree)) != 0) {
1421 printf("Switching work tree from %s to %s\n",
1422 got_worktree_get_head_ref_name(worktree),
1423 got_ref_get_name(head_ref));
1424 return got_worktree_set_head_ref(worktree, head_ref);
1427 err = check_linear_ancestry(commit_id,
1428 got_worktree_get_base_commit_id(worktree), 0, repo);
1430 if (err->code != GOT_ERR_ANCESTRY)
1437 /* Switching to a rebased branch with the same reference name. */
1438 err = got_object_id_str(&base_id_str,
1439 got_worktree_get_base_commit_id(worktree));
1442 printf("Reference %s now points at a different branch\n",
1443 got_worktree_get_head_ref_name(worktree));
1444 printf("Switching work tree from %s to %s\n", base_id_str,
1445 got_worktree_get_head_ref_name(worktree));
1449 static const struct got_error *
1450 check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1452 const struct got_error *err;
1455 err = got_worktree_rebase_in_progress(&in_progress, worktree);
1459 return got_error(GOT_ERR_REBASING);
1461 err = got_worktree_histedit_in_progress(&in_progress, worktree);
1465 return got_error(GOT_ERR_HISTEDIT_BUSY);
1470 static const struct got_error *
1471 get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1472 char *argv[], struct got_worktree *worktree)
1474 const struct got_error *err = NULL;
1481 return got_error_from_errno("strdup");
1482 return got_pathlist_append(paths, path, NULL);
1485 for (i = 0; i < argc; i++) {
1486 err = got_worktree_resolve_path(&path, worktree, argv[i]);
1489 err = got_pathlist_append(paths, path, NULL);
1499 static const struct got_error *
1500 cmd_update(int argc, char *argv[])
1502 const struct got_error *error = NULL;
1503 struct got_repository *repo = NULL;
1504 struct got_worktree *worktree = NULL;
1505 char *worktree_path = NULL;
1506 struct got_object_id *commit_id = NULL;
1507 char *commit_id_str = NULL;
1508 const char *branch_name = NULL;
1509 struct got_reference *head_ref = NULL;
1510 struct got_pathlist_head paths;
1511 struct got_pathlist_entry *pe;
1512 int ch, did_something = 0;
1516 while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1519 branch_name = optarg;
1522 commit_id_str = strdup(optarg);
1523 if (commit_id_str == NULL)
1524 return got_error_from_errno("strdup");
1536 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1537 "unveil", NULL) == -1)
1540 worktree_path = getcwd(NULL, 0);
1541 if (worktree_path == NULL) {
1542 error = got_error_from_errno("getcwd");
1545 error = got_worktree_open(&worktree, worktree_path);
1549 error = check_rebase_or_histedit_in_progress(worktree);
1553 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1558 error = apply_unveil(got_repo_get_path(repo), 0,
1559 got_worktree_get_root_path(worktree));
1563 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1567 error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1568 got_worktree_get_head_ref_name(worktree), 0);
1571 if (commit_id_str == NULL) {
1572 error = got_ref_resolve(&commit_id, repo, head_ref);
1575 error = got_object_id_str(&commit_id_str, commit_id);
1579 error = got_repo_match_object_id(&commit_id, NULL,
1580 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1581 free(commit_id_str);
1582 commit_id_str = NULL;
1585 error = got_object_id_str(&commit_id_str, commit_id);
1591 struct got_object_id *head_commit_id;
1592 TAILQ_FOREACH(pe, &paths, entry) {
1593 if (pe->path_len == 0)
1595 error = got_error_msg(GOT_ERR_BAD_PATH,
1596 "switching between branches requires that "
1597 "the entire work tree gets updated");
1600 error = got_ref_resolve(&head_commit_id, repo, head_ref);
1603 error = check_linear_ancestry(commit_id, head_commit_id, 0,
1605 free(head_commit_id);
1608 error = check_same_branch(commit_id, head_ref, NULL, repo);
1611 error = switch_head_ref(head_ref, commit_id, worktree, repo);
1615 error = check_linear_ancestry(commit_id,
1616 got_worktree_get_base_commit_id(worktree), 0, repo);
1617 if (error != NULL) {
1618 if (error->code == GOT_ERR_ANCESTRY)
1619 error = got_error(GOT_ERR_BRANCH_MOVED);
1622 error = check_same_branch(commit_id, head_ref, NULL, repo);
1627 if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1629 error = got_worktree_set_base_commit_id(worktree, repo,
1635 error = got_worktree_checkout_files(worktree, &paths, repo,
1636 update_progress, &did_something, check_cancelled, NULL);
1641 printf("Updated to commit %s\n", commit_id_str);
1643 printf("Already up-to-date\n");
1645 free(worktree_path);
1646 TAILQ_FOREACH(pe, &paths, entry)
1647 free((char *)pe->path);
1648 got_pathlist_free(&paths);
1650 free(commit_id_str);
1654 static const struct got_error *
1655 diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1656 const char *path, int diff_context, int ignore_whitespace,
1657 struct got_repository *repo)
1659 const struct got_error *err = NULL;
1660 struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1663 err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1668 err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1672 while (path[0] == '/')
1674 err = got_diff_blob(blob1, blob2, path, path, diff_context,
1675 ignore_whitespace, stdout);
1678 got_object_blob_close(blob1);
1679 got_object_blob_close(blob2);
1683 static const struct got_error *
1684 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1685 const char *path, int diff_context, int ignore_whitespace,
1686 struct got_repository *repo)
1688 const struct got_error *err = NULL;
1689 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1690 struct got_diff_blob_output_unidiff_arg arg;
1693 err = got_object_open_as_tree(&tree1, repo, tree_id1);
1698 err = got_object_open_as_tree(&tree2, repo, tree_id2);
1702 arg.diff_context = diff_context;
1703 arg.ignore_whitespace = ignore_whitespace;
1704 arg.outfile = stdout;
1705 while (path[0] == '/')
1707 err = got_diff_tree(tree1, tree2, path, path, repo,
1708 got_diff_blob_output_unidiff, &arg, 1);
1711 got_object_tree_close(tree1);
1713 got_object_tree_close(tree2);
1717 static const struct got_error *
1718 print_patch(struct got_commit_object *commit, struct got_object_id *id,
1719 const char *path, int diff_context, struct got_repository *repo)
1721 const struct got_error *err = NULL;
1722 struct got_commit_object *pcommit = NULL;
1723 char *id_str1 = NULL, *id_str2 = NULL;
1724 struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1725 struct got_object_qid *qid;
1727 qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1729 err = got_object_open_as_commit(&pcommit, repo,
1735 if (path && path[0] != '\0') {
1737 err = got_object_id_by_path(&obj_id2, repo, id, path);
1740 err = got_object_id_str(&id_str2, obj_id2);
1746 err = got_object_id_by_path(&obj_id1, repo,
1752 err = got_object_id_str(&id_str1, obj_id1);
1758 err = got_object_get_type(&obj_type, repo, obj_id2);
1763 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1765 case GOT_OBJ_TYPE_BLOB:
1766 err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1769 case GOT_OBJ_TYPE_TREE:
1770 err = diff_trees(obj_id1, obj_id2, path, diff_context,
1774 err = got_error(GOT_ERR_OBJ_TYPE);
1780 obj_id2 = got_object_commit_get_tree_id(commit);
1781 err = got_object_id_str(&id_str2, obj_id2);
1784 obj_id1 = got_object_commit_get_tree_id(pcommit);
1785 err = got_object_id_str(&id_str1, obj_id1);
1788 printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1789 err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1795 got_object_commit_close(pcommit);
1800 get_datestr(time_t *time, char *datebuf)
1802 struct tm mytm, *tm;
1805 tm = gmtime_r(time, &mytm);
1808 s = asctime_r(tm, datebuf);
1811 p = strchr(s, '\n');
1817 static const struct got_error *
1818 match_logmsg(int *have_match, struct got_object_id *id,
1819 struct got_commit_object *commit, regex_t *regex)
1821 const struct got_error *err = NULL;
1822 regmatch_t regmatch;
1823 char *id_str = NULL, *logmsg = NULL;
1827 err = got_object_id_str(&id_str, id);
1831 err = got_object_commit_get_logmsg(&logmsg, commit);
1835 if (regexec(regex, logmsg, 1, ®match, 0) == 0)
1843 #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1845 static const struct got_error *
1846 print_commit(struct got_commit_object *commit, struct got_object_id *id,
1847 struct got_repository *repo, const char *path, int show_patch,
1848 int diff_context, struct got_reflist_head *refs)
1850 const struct got_error *err = NULL;
1851 char *id_str, *datestr, *logmsg0, *logmsg, *line;
1853 time_t committer_time;
1854 const char *author, *committer;
1855 char *refs_str = NULL;
1856 struct got_reflist_entry *re;
1858 SIMPLEQ_FOREACH(re, refs, entry) {
1861 struct got_tag_object *tag = NULL;
1864 name = got_ref_get_name(re->ref);
1865 if (strcmp(name, GOT_REF_HEAD) == 0)
1867 if (strncmp(name, "refs/", 5) == 0)
1869 if (strncmp(name, "got/", 4) == 0)
1871 if (strncmp(name, "heads/", 6) == 0)
1873 if (strncmp(name, "remotes/", 8) == 0)
1875 if (strncmp(name, "tags/", 5) == 0) {
1876 err = got_object_open_as_tag(&tag, repo, re->id);
1878 if (err->code != GOT_ERR_OBJ_TYPE)
1880 /* Ref points at something other than a tag. */
1885 cmp = got_object_id_cmp(tag ?
1886 got_object_tag_get_object_id(tag) : re->id, id);
1888 got_object_tag_close(tag);
1892 if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1894 err = got_error_from_errno("asprintf");
1900 err = got_object_id_str(&id_str, id);
1904 printf(GOT_COMMIT_SEP_STR);
1905 printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1906 refs_str ? refs_str : "", refs_str ? ")" : "");
1911 printf("from: %s\n", got_object_commit_get_author(commit));
1912 committer_time = got_object_commit_get_committer_time(commit);
1913 datestr = get_datestr(&committer_time, datebuf);
1915 printf("date: %s UTC\n", datestr);
1916 author = got_object_commit_get_author(commit);
1917 committer = got_object_commit_get_committer(commit);
1918 if (strcmp(author, committer) != 0)
1919 printf("via: %s\n", committer);
1920 if (got_object_commit_get_nparents(commit) > 1) {
1921 const struct got_object_id_queue *parent_ids;
1922 struct got_object_qid *qid;
1924 parent_ids = got_object_commit_get_parent_ids(commit);
1925 SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1926 err = got_object_id_str(&id_str, qid->id);
1929 printf("parent %d: %s\n", n++, id_str);
1934 err = got_object_commit_get_logmsg(&logmsg0, commit);
1940 line = strsep(&logmsg, "\n");
1942 printf(" %s\n", line);
1947 err = print_patch(commit, id, path, diff_context, repo);
1952 if (fflush(stdout) != 0 && err == NULL)
1953 err = got_error_from_errno("fflush");
1957 static const struct got_error *
1958 print_commits(struct got_object_id *root_id, struct got_repository *repo,
1959 const char *path, int show_patch, const char *search_pattern,
1960 int diff_context, int limit, int log_branches,
1961 struct got_reflist_head *refs)
1963 const struct got_error *err;
1964 struct got_commit_graph *graph;
1968 if (search_pattern &&
1969 regcomp(®ex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1970 return got_error_msg(GOT_ERR_REGEX, search_pattern);
1972 err = got_commit_graph_open(&graph, path, !log_branches);
1975 err = got_commit_graph_iter_start(graph, root_id, repo,
1976 check_cancelled, NULL);
1980 struct got_commit_object *commit;
1981 struct got_object_id *id;
1983 if (sigint_received || sigpipe_received)
1986 err = got_commit_graph_iter_next(&id, graph, repo,
1987 check_cancelled, NULL);
1989 if (err->code == GOT_ERR_ITER_COMPLETED)
1996 err = got_object_open_as_commit(&commit, repo, id);
2000 if (search_pattern) {
2001 err = match_logmsg(&have_match, id, commit, ®ex);
2003 got_object_commit_close(commit);
2006 if (have_match == 0) {
2007 got_object_commit_close(commit);
2012 err = print_commit(commit, id, repo, path, show_patch,
2013 diff_context, refs);
2014 got_object_commit_close(commit);
2015 if (err || (limit && --limit == 0))
2021 got_commit_graph_close(graph);
2028 fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2029 "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2034 get_default_log_limit(void)
2036 const char *got_default_log_limit;
2040 got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2041 if (got_default_log_limit == NULL)
2043 n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2049 static const struct got_error *
2050 cmd_log(int argc, char *argv[])
2052 const struct got_error *error;
2053 struct got_repository *repo = NULL;
2054 struct got_worktree *worktree = NULL;
2055 struct got_commit_object *commit = NULL;
2056 struct got_object_id *id = NULL;
2057 char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2058 const char *start_commit = NULL, *search_pattern = NULL;
2059 int diff_context = -1, ch;
2060 int show_patch = 0, limit = 0, log_branches = 0;
2062 struct got_reflist_head refs;
2064 SIMPLEQ_INIT(&refs);
2067 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2073 limit = get_default_log_limit();
2075 while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2081 start_commit = optarg;
2084 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2087 err(1, "-C option %s", errstr);
2090 limit = strtonum(optarg, 0, INT_MAX, &errstr);
2092 err(1, "-l option %s", errstr);
2098 repo_path = realpath(optarg, NULL);
2099 if (repo_path == NULL)
2100 return got_error_from_errno2("realpath",
2102 got_path_strip_trailing_slashes(repo_path);
2105 search_pattern = optarg;
2116 if (diff_context == -1)
2118 else if (!show_patch)
2119 errx(1, "-C reguires -p");
2121 cwd = getcwd(NULL, 0);
2123 error = got_error_from_errno("getcwd");
2127 error = got_worktree_open(&worktree, cwd);
2128 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2135 error = got_error_from_errno("strdup");
2138 } else if (argc == 1) {
2140 error = got_worktree_resolve_path(&path, worktree,
2145 path = strdup(argv[0]);
2147 error = got_error_from_errno("strdup");
2154 if (repo_path == NULL) {
2155 repo_path = worktree ?
2156 strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2158 if (repo_path == NULL) {
2159 error = got_error_from_errno("strdup");
2163 error = got_repo_open(&repo, repo_path, NULL);
2167 error = apply_unveil(got_repo_get_path(repo), 1,
2168 worktree ? got_worktree_get_root_path(worktree) : NULL);
2172 if (start_commit == NULL) {
2173 struct got_reference *head_ref;
2174 error = got_ref_open(&head_ref, repo,
2175 worktree ? got_worktree_get_head_ref_name(worktree)
2179 error = got_ref_resolve(&id, repo, head_ref);
2180 got_ref_close(head_ref);
2183 error = got_object_open_as_commit(&commit, repo, id);
2185 struct got_reference *ref;
2186 error = got_ref_open(&ref, repo, start_commit, 0);
2187 if (error == NULL) {
2189 error = got_ref_resolve(&id, repo, ref);
2193 error = got_object_get_type(&obj_type, repo, id);
2196 if (obj_type == GOT_OBJ_TYPE_TAG) {
2197 struct got_tag_object *tag;
2198 error = got_object_open_as_tag(&tag, repo, id);
2201 if (got_object_tag_get_object_type(tag) !=
2202 GOT_OBJ_TYPE_COMMIT) {
2203 got_object_tag_close(tag);
2204 error = got_error(GOT_ERR_OBJ_TYPE);
2208 id = got_object_id_dup(
2209 got_object_tag_get_object_id(tag));
2211 error = got_error_from_errno(
2212 "got_object_id_dup");
2213 got_object_tag_close(tag);
2216 } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2217 error = got_error(GOT_ERR_OBJ_TYPE);
2220 error = got_object_open_as_commit(&commit, repo, id);
2224 if (commit == NULL) {
2225 error = got_repo_match_object_id_prefix(&id,
2226 start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2235 const char *prefix = got_worktree_get_path_prefix(worktree);
2237 if (asprintf(&p, "%s%s%s", prefix,
2238 (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2239 error = got_error_from_errno("asprintf");
2242 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2245 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2250 path = in_repo_path;
2253 error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2257 error = print_commits(id, repo, path, show_patch, search_pattern,
2258 diff_context, limit, log_branches, &refs);
2265 got_worktree_close(worktree);
2267 const struct got_error *repo_error;
2268 repo_error = got_repo_close(repo);
2272 got_ref_list_free(&refs);
2279 fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2280 "[-w] [object1 object2 | path]\n", getprogname());
2284 struct print_diff_arg {
2285 struct got_repository *repo;
2286 struct got_worktree *worktree;
2291 int ignore_whitespace;
2294 static const struct got_error *
2295 print_diff(void *arg, unsigned char status, unsigned char staged_status,
2296 const char *path, struct got_object_id *blob_id,
2297 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2298 int dirfd, const char *de_name)
2300 struct print_diff_arg *a = arg;
2301 const struct got_error *err = NULL;
2302 struct got_blob_object *blob1 = NULL;
2305 char *abspath = NULL, *label1 = NULL;
2308 if (a->diff_staged) {
2309 if (staged_status != GOT_STATUS_MODIFY &&
2310 staged_status != GOT_STATUS_ADD &&
2311 staged_status != GOT_STATUS_DELETE)
2314 if (staged_status == GOT_STATUS_DELETE)
2316 if (status == GOT_STATUS_NONEXISTENT)
2317 return got_error_set_errno(ENOENT, path);
2318 if (status != GOT_STATUS_MODIFY &&
2319 status != GOT_STATUS_ADD &&
2320 status != GOT_STATUS_DELETE &&
2321 status != GOT_STATUS_CONFLICT)
2325 if (!a->header_shown) {
2326 printf("diff %s %s%s\n", a->id_str,
2327 got_worktree_get_root_path(a->worktree),
2328 a->diff_staged ? " (staged changes)" : "");
2329 a->header_shown = 1;
2332 if (a->diff_staged) {
2333 const char *label1 = NULL, *label2 = NULL;
2334 switch (staged_status) {
2335 case GOT_STATUS_MODIFY:
2339 case GOT_STATUS_ADD:
2342 case GOT_STATUS_DELETE:
2346 return got_error(GOT_ERR_FILE_STATUS);
2348 return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2349 label1, label2, a->diff_context, a->ignore_whitespace,
2353 if (staged_status == GOT_STATUS_ADD ||
2354 staged_status == GOT_STATUS_MODIFY) {
2356 err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2360 err = got_object_id_str(&id_str, staged_blob_id);
2363 if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2364 err = got_error_from_errno("asprintf");
2369 } else if (status != GOT_STATUS_ADD) {
2370 err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2375 if (status != GOT_STATUS_DELETE) {
2376 if (asprintf(&abspath, "%s/%s",
2377 got_worktree_get_root_path(a->worktree), path) == -1) {
2378 err = got_error_from_errno("asprintf");
2383 fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2385 err = got_error_from_errno2("openat", abspath);
2389 fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2391 err = got_error_from_errno2("open", abspath);
2395 if (fstat(fd, &sb) == -1) {
2396 err = got_error_from_errno2("fstat", abspath);
2399 f2 = fdopen(fd, "r");
2401 err = got_error_from_errno2("fdopen", abspath);
2408 err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2409 a->diff_context, a->ignore_whitespace, stdout);
2412 got_object_blob_close(blob1);
2413 if (f2 && fclose(f2) == EOF && err == NULL)
2414 err = got_error_from_errno("fclose");
2415 if (fd != -1 && close(fd) == -1 && err == NULL)
2416 err = got_error_from_errno("close");
2421 static const struct got_error *
2422 cmd_diff(int argc, char *argv[])
2424 const struct got_error *error;
2425 struct got_repository *repo = NULL;
2426 struct got_worktree *worktree = NULL;
2427 char *cwd = NULL, *repo_path = NULL;
2428 struct got_object_id *id1 = NULL, *id2 = NULL;
2429 const char *id_str1 = NULL, *id_str2 = NULL;
2430 char *label1 = NULL, *label2 = NULL;
2432 int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2437 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2442 while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2445 diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2448 err(1, "-C option %s", errstr);
2451 repo_path = realpath(optarg, NULL);
2452 if (repo_path == NULL)
2453 return got_error_from_errno2("realpath",
2455 got_path_strip_trailing_slashes(repo_path);
2461 ignore_whitespace = 1;
2472 cwd = getcwd(NULL, 0);
2474 error = got_error_from_errno("getcwd");
2477 error = got_worktree_open(&worktree, cwd);
2478 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2481 if (worktree == NULL) {
2482 error = got_error(GOT_ERR_NOT_WORKTREE);
2487 "-r option can't be used when diffing a work tree");
2488 repo_path = strdup(got_worktree_get_repo_path(worktree));
2489 if (repo_path == NULL) {
2490 error = got_error_from_errno("strdup");
2494 error = got_worktree_resolve_path(&path, worktree,
2501 error = got_error_from_errno("strdup");
2505 } else if (argc == 2) {
2507 errx(1, "-s option can't be used when diffing "
2508 "objects in repository");
2511 if (worktree && repo_path == NULL) {
2513 strdup(got_worktree_get_repo_path(worktree));
2514 if (repo_path == NULL) {
2515 error = got_error_from_errno("strdup");
2522 if (repo_path == NULL) {
2523 repo_path = getcwd(NULL, 0);
2524 if (repo_path == NULL)
2525 return got_error_from_errno("getcwd");
2528 error = got_repo_open(&repo, repo_path, NULL);
2533 error = apply_unveil(got_repo_get_path(repo), 1,
2534 worktree ? got_worktree_get_root_path(worktree) : NULL);
2539 struct print_diff_arg arg;
2540 struct got_pathlist_head paths;
2545 error = got_object_id_str(&id_str,
2546 got_worktree_get_base_commit_id(worktree));
2550 arg.worktree = worktree;
2551 arg.diff_context = diff_context;
2552 arg.id_str = id_str;
2553 arg.header_shown = 0;
2554 arg.diff_staged = diff_staged;
2555 arg.ignore_whitespace = ignore_whitespace;
2557 error = got_pathlist_append(&paths, path, NULL);
2561 error = got_worktree_status(worktree, &paths, repo, print_diff,
2562 &arg, check_cancelled, NULL);
2564 got_pathlist_free(&paths);
2568 error = got_repo_match_object_id(&id1, &label1, id_str1,
2569 GOT_OBJ_TYPE_ANY, 1, repo);
2573 error = got_repo_match_object_id(&id2, &label2, id_str2,
2574 GOT_OBJ_TYPE_ANY, 1, repo);
2578 error = got_object_get_type(&type1, repo, id1);
2582 error = got_object_get_type(&type2, repo, id2);
2586 if (type1 != type2) {
2587 error = got_error(GOT_ERR_OBJ_TYPE);
2592 case GOT_OBJ_TYPE_BLOB:
2593 error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2594 diff_context, ignore_whitespace, repo, stdout);
2596 case GOT_OBJ_TYPE_TREE:
2597 error = got_diff_objects_as_trees(id1, id2, "", "",
2598 diff_context, ignore_whitespace, repo, stdout);
2600 case GOT_OBJ_TYPE_COMMIT:
2601 printf("diff %s %s\n", label1, label2);
2602 error = got_diff_objects_as_commits(id1, id2, diff_context,
2603 ignore_whitespace, repo, stdout);
2606 error = got_error(GOT_ERR_OBJ_TYPE);
2615 got_worktree_close(worktree);
2617 const struct got_error *repo_error;
2618 repo_error = got_repo_close(repo);
2629 "usage: %s blame [-c commit] [-r repository-path] path\n",
2638 char datebuf[11]; /* YYYY-MM-DD + NUL */
2641 struct blame_cb_args {
2642 struct blame_line *lines;
2646 off_t *line_offsets;
2648 struct got_repository *repo;
2651 static const struct got_error *
2652 blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2654 const struct got_error *err = NULL;
2655 struct blame_cb_args *a = arg;
2656 struct blame_line *bline;
2658 size_t linesize = 0;
2659 struct got_commit_object *commit = NULL;
2662 time_t committer_time;
2664 if (nlines != a->nlines ||
2665 (lineno != -1 && lineno < 1) || lineno > a->nlines)
2666 return got_error(GOT_ERR_RANGE);
2668 if (sigint_received)
2669 return got_error(GOT_ERR_ITER_COMPLETED);
2672 return NULL; /* no change in this commit */
2674 /* Annotate this line. */
2675 bline = &a->lines[lineno - 1];
2676 if (bline->annotated)
2678 err = got_object_id_str(&bline->id_str, id);
2682 err = got_object_open_as_commit(&commit, a->repo, id);
2686 bline->committer = strdup(got_object_commit_get_committer(commit));
2687 if (bline->committer == NULL) {
2688 err = got_error_from_errno("strdup");
2692 committer_time = got_object_commit_get_committer_time(commit);
2693 if (localtime_r(&committer_time, &tm) == NULL)
2694 return got_error_from_errno("localtime_r");
2695 if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2696 &tm) >= sizeof(bline->datebuf)) {
2697 err = got_error(GOT_ERR_NO_SPACE);
2700 bline->annotated = 1;
2702 /* Print lines annotated so far. */
2703 bline = &a->lines[a->lineno_cur - 1];
2704 if (!bline->annotated)
2707 offset = a->line_offsets[a->lineno_cur - 1];
2708 if (fseeko(a->f, offset, SEEK_SET) == -1) {
2709 err = got_error_from_errno("fseeko");
2713 while (bline->annotated) {
2714 char *smallerthan, *at, *nl, *committer;
2717 if (getline(&line, &linesize, a->f) == -1) {
2719 err = got_error_from_errno("getline");
2723 committer = bline->committer;
2724 smallerthan = strchr(committer, '<');
2725 if (smallerthan && smallerthan[1] != '\0')
2726 committer = smallerthan + 1;
2727 at = strchr(committer, '@');
2730 len = strlen(committer);
2732 committer[8] = '\0';
2734 nl = strchr(line, '\n');
2737 printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2738 bline->id_str, bline->datebuf, committer, line);
2741 bline = &a->lines[a->lineno_cur - 1];
2745 got_object_commit_close(commit);
2750 static const struct got_error *
2751 cmd_blame(int argc, char *argv[])
2753 const struct got_error *error;
2754 struct got_repository *repo = NULL;
2755 struct got_worktree *worktree = NULL;
2756 char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2757 struct got_object_id *obj_id = NULL;
2758 struct got_object_id *commit_id = NULL;
2759 struct got_blob_object *blob = NULL;
2760 char *commit_id_str = NULL;
2761 struct blame_cb_args bca;
2762 int ch, obj_type, i;
2765 memset(&bca, 0, sizeof(bca));
2768 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2773 while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2776 commit_id_str = optarg;
2779 repo_path = realpath(optarg, NULL);
2780 if (repo_path == NULL)
2781 return got_error_from_errno2("realpath",
2783 got_path_strip_trailing_slashes(repo_path);
2799 cwd = getcwd(NULL, 0);
2801 error = got_error_from_errno("getcwd");
2804 if (repo_path == NULL) {
2805 error = got_worktree_open(&worktree, cwd);
2806 if (error && error->code != GOT_ERR_NOT_WORKTREE)
2812 strdup(got_worktree_get_repo_path(worktree));
2813 if (repo_path == NULL)
2814 error = got_error_from_errno("strdup");
2818 repo_path = strdup(cwd);
2819 if (repo_path == NULL) {
2820 error = got_error_from_errno("strdup");
2826 error = got_repo_open(&repo, repo_path, NULL);
2830 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2835 const char *prefix = got_worktree_get_path_prefix(worktree);
2836 char *p, *worktree_subdir = cwd +
2837 strlen(got_worktree_get_root_path(worktree));
2838 if (asprintf(&p, "%s%s%s%s%s",
2839 prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2840 worktree_subdir, worktree_subdir[0] ? "/" : "",
2842 error = got_error_from_errno("asprintf");
2845 error = got_repo_map_path(&in_repo_path, repo, p, 0);
2848 error = got_repo_map_path(&in_repo_path, repo, path, 1);
2853 if (commit_id_str == NULL) {
2854 struct got_reference *head_ref;
2855 error = got_ref_open(&head_ref, repo, worktree ?
2856 got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2859 error = got_ref_resolve(&commit_id, repo, head_ref);
2860 got_ref_close(head_ref);
2864 error = got_repo_match_object_id(&commit_id, NULL,
2865 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2870 error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2874 error = got_object_get_type(&obj_type, repo, obj_id);
2878 if (obj_type != GOT_OBJ_TYPE_BLOB) {
2879 error = got_error(GOT_ERR_OBJ_TYPE);
2883 error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2886 bca.f = got_opentemp();
2887 if (bca.f == NULL) {
2888 error = got_error_from_errno("got_opentemp");
2891 error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2892 &bca.line_offsets, bca.f, blob);
2893 if (error || bca.nlines == 0)
2896 /* Don't include \n at EOF in the blame line count. */
2897 if (bca.line_offsets[bca.nlines - 1] == filesize)
2900 bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2901 if (bca.lines == NULL) {
2902 error = got_error_from_errno("calloc");
2906 bca.nlines_prec = 0;
2914 error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2915 check_cancelled, NULL);
2923 got_object_blob_close(blob);
2925 got_worktree_close(worktree);
2927 const struct got_error *repo_error;
2928 repo_error = got_repo_close(repo);
2933 for (i = 0; i < bca.nlines; i++) {
2934 struct blame_line *bline = &bca.lines[i];
2935 free(bline->id_str);
2936 free(bline->committer);
2940 free(bca.line_offsets);
2941 if (bca.f && fclose(bca.f) == EOF && error == NULL)
2942 error = got_error_from_errno("fclose");
2950 "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2956 print_entry(struct got_tree_entry *te, const char *id, const char *path,
2957 const char *root_path)
2959 int is_root_path = (strcmp(path, root_path) == 0);
2960 const char *modestr = "";
2961 mode_t mode = got_tree_entry_get_mode(te);
2963 path += strlen(root_path);
2964 while (path[0] == '/')
2967 if (got_object_tree_entry_is_submodule(te))
2969 else if (S_ISLNK(mode))
2971 else if (S_ISDIR(mode))
2973 else if (mode & S_IXUSR)
2976 printf("%s%s%s%s%s\n", id ? id : "", path,
2977 is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2980 static const struct got_error *
2981 print_tree(const char *path, struct got_object_id *commit_id,
2982 int show_ids, int recurse, const char *root_path,
2983 struct got_repository *repo)
2985 const struct got_error *err = NULL;
2986 struct got_object_id *tree_id = NULL;
2987 struct got_tree_object *tree = NULL;
2990 err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2994 err = got_object_open_as_tree(&tree, repo, tree_id);
2997 nentries = got_object_tree_get_nentries(tree);
2998 for (i = 0; i < nentries; i++) {
2999 struct got_tree_entry *te;
3002 if (sigint_received || sigpipe_received)
3005 te = got_object_tree_get_entry(tree, i);
3008 err = got_object_id_str(&id_str,
3009 got_tree_entry_get_id(te));
3012 if (asprintf(&id, "%s ", id_str) == -1) {
3013 err = got_error_from_errno("asprintf");
3019 print_entry(te, id, path, root_path);
3022 if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3024 if (asprintf(&child_path, "%s%s%s", path,
3025 path[0] == '/' && path[1] == '\0' ? "" : "/",
3026 got_tree_entry_get_name(te)) == -1) {
3027 err = got_error_from_errno("asprintf");
3030 err = print_tree(child_path, commit_id, show_ids, 1,
3039 got_object_tree_close(tree);
3044 static const struct got_error *
3045 cmd_tree(int argc, char *argv[])
3047 const struct got_error *error;
3048 struct got_repository *repo = NULL;
3049 struct got_worktree *worktree = NULL;
3051 char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3052 struct got_object_id *commit_id = NULL;
3053 char *commit_id_str = NULL;
3054 int show_ids = 0, recurse = 0;
3058 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3063 while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3066 commit_id_str = optarg;
3069 repo_path = realpath(optarg, NULL);
3070 if (repo_path == NULL)
3071 return got_error_from_errno2("realpath",
3073 got_path_strip_trailing_slashes(repo_path);
3097 cwd = getcwd(NULL, 0);
3099 error = got_error_from_errno("getcwd");
3102 if (repo_path == NULL) {
3103 error = got_worktree_open(&worktree, cwd);
3104 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3110 strdup(got_worktree_get_repo_path(worktree));
3111 if (repo_path == NULL)
3112 error = got_error_from_errno("strdup");
3116 repo_path = strdup(cwd);
3117 if (repo_path == NULL) {
3118 error = got_error_from_errno("strdup");
3124 error = got_repo_open(&repo, repo_path, NULL);
3128 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3134 char *p, *worktree_subdir = cwd +
3135 strlen(got_worktree_get_root_path(worktree));
3136 if (asprintf(&p, "%s/%s",
3137 got_worktree_get_path_prefix(worktree),
3138 worktree_subdir) == -1) {
3139 error = got_error_from_errno("asprintf");
3142 error = got_repo_map_path(&in_repo_path, repo, p, 0);
3149 if (in_repo_path == NULL) {
3150 error = got_repo_map_path(&in_repo_path, repo, path, 1);
3155 if (commit_id_str == NULL) {
3156 struct got_reference *head_ref;
3157 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3160 error = got_ref_resolve(&commit_id, repo, head_ref);
3161 got_ref_close(head_ref);
3165 error = got_repo_match_object_id(&commit_id, NULL,
3166 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3171 error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3172 in_repo_path, repo);
3179 got_worktree_close(worktree);
3181 const struct got_error *repo_error;
3182 repo_error = got_repo_close(repo);
3192 fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3196 static const struct got_error *
3197 print_status(void *arg, unsigned char status, unsigned char staged_status,
3198 const char *path, struct got_object_id *blob_id,
3199 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3200 int dirfd, const char *de_name)
3202 if (status == staged_status && (status == GOT_STATUS_DELETE))
3203 status = GOT_STATUS_NO_CHANGE;
3204 printf("%c%c %s\n", status, staged_status, path);
3208 static const struct got_error *
3209 cmd_status(int argc, char *argv[])
3211 const struct got_error *error = NULL;
3212 struct got_repository *repo = NULL;
3213 struct got_worktree *worktree = NULL;
3215 struct got_pathlist_head paths;
3216 struct got_pathlist_entry *pe;
3221 while ((ch = getopt(argc, argv, "")) != -1) {
3233 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3237 cwd = getcwd(NULL, 0);
3239 error = got_error_from_errno("getcwd");
3243 error = got_worktree_open(&worktree, cwd);
3247 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3252 error = apply_unveil(got_repo_get_path(repo), 1,
3253 got_worktree_get_root_path(worktree));
3257 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3261 error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3262 check_cancelled, NULL);
3264 TAILQ_FOREACH(pe, &paths, entry)
3265 free((char *)pe->path);
3266 got_pathlist_free(&paths);
3275 "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3280 static const struct got_error *
3281 list_refs(struct got_repository *repo)
3283 static const struct got_error *err = NULL;
3284 struct got_reflist_head refs;
3285 struct got_reflist_entry *re;
3287 SIMPLEQ_INIT(&refs);
3288 err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3292 SIMPLEQ_FOREACH(re, &refs, entry) {
3294 refstr = got_ref_to_str(re->ref);
3296 return got_error_from_errno("got_ref_to_str");
3297 printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3301 got_ref_list_free(&refs);
3305 static const struct got_error *
3306 delete_ref(struct got_repository *repo, const char *refname)
3308 const struct got_error *err = NULL;
3309 struct got_reference *ref;
3311 err = got_ref_open(&ref, repo, refname, 0);
3315 err = got_ref_delete(ref, repo);
3320 static const struct got_error *
3321 add_ref(struct got_repository *repo, const char *refname, const char *target)
3323 const struct got_error *err = NULL;
3324 struct got_object_id *id;
3325 struct got_reference *ref = NULL;
3328 * Don't let the user create a reference name with a leading '-'.
3329 * While technically a valid reference name, this case is usually
3330 * an unintended typo.
3332 if (refname[0] == '-')
3333 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3335 err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3338 struct got_reference *target_ref;
3340 if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3342 err = got_ref_open(&target_ref, repo, target, 0);
3345 err = got_ref_resolve(&id, repo, target_ref);
3346 got_ref_close(target_ref);
3351 err = got_ref_alloc(&ref, refname, id);
3355 err = got_ref_write(ref, repo);
3363 static const struct got_error *
3364 add_symref(struct got_repository *repo, const char *refname, const char *target)
3366 const struct got_error *err = NULL;
3367 struct got_reference *ref = NULL;
3368 struct got_reference *target_ref = NULL;
3371 * Don't let the user create a reference name with a leading '-'.
3372 * While technically a valid reference name, this case is usually
3373 * an unintended typo.
3375 if (refname[0] == '-')
3376 return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3378 err = got_ref_open(&target_ref, repo, target, 0);
3382 err = got_ref_alloc_symref(&ref, refname, target_ref);
3386 err = got_ref_write(ref, repo);
3389 got_ref_close(target_ref);
3395 static const struct got_error *
3396 cmd_ref(int argc, char *argv[])
3398 const struct got_error *error = NULL;
3399 struct got_repository *repo = NULL;
3400 struct got_worktree *worktree = NULL;
3401 char *cwd = NULL, *repo_path = NULL;
3402 int ch, do_list = 0, create_symref = 0;
3403 const char *delref = NULL;
3405 /* TODO: Add -s option for adding symbolic references. */
3406 while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3412 repo_path = realpath(optarg, NULL);
3413 if (repo_path == NULL)
3414 return got_error_from_errno2("realpath",
3416 got_path_strip_trailing_slashes(repo_path);
3430 if (do_list && delref)
3431 errx(1, "-l and -d options are mutually exclusive\n");
3436 if (do_list || delref) {
3438 errx(1, "-s option cannot be used together with the "
3439 "-l or -d options");
3442 } else if (argc != 2)
3447 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3451 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3452 "sendfd unveil", NULL) == -1)
3456 cwd = getcwd(NULL, 0);
3458 error = got_error_from_errno("getcwd");
3462 if (repo_path == NULL) {
3463 error = got_worktree_open(&worktree, cwd);
3464 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3470 strdup(got_worktree_get_repo_path(worktree));
3471 if (repo_path == NULL)
3472 error = got_error_from_errno("strdup");
3476 repo_path = strdup(cwd);
3477 if (repo_path == NULL) {
3478 error = got_error_from_errno("strdup");
3484 error = got_repo_open(&repo, repo_path, NULL);
3488 error = apply_unveil(got_repo_get_path(repo), do_list,
3489 worktree ? got_worktree_get_root_path(worktree) : NULL);
3494 error = list_refs(repo);
3496 error = delete_ref(repo, delref);
3497 else if (create_symref)
3498 error = add_symref(repo, argv[0], argv[1]);
3500 error = add_ref(repo, argv[0], argv[1]);
3503 got_repo_close(repo);
3505 got_worktree_close(worktree);
3515 "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3516 "[name]\n", getprogname());
3520 static const struct got_error *
3521 list_branch(struct got_repository *repo, struct got_worktree *worktree,
3522 struct got_reference *ref)
3524 const struct got_error *err = NULL;
3525 const char *refname, *marker = " ";
3528 refname = got_ref_get_name(ref);
3529 if (worktree && strcmp(refname,
3530 got_worktree_get_head_ref_name(worktree)) == 0) {
3531 struct got_object_id *id = NULL;
3533 err = got_ref_resolve(&id, repo, ref);
3536 if (got_object_id_cmp(id,
3537 got_worktree_get_base_commit_id(worktree)) == 0)
3544 if (strncmp(refname, "refs/heads/", 11) == 0)
3546 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3549 refstr = got_ref_to_str(ref);
3551 return got_error_from_errno("got_ref_to_str");
3553 printf("%s%s: %s\n", marker, refname, refstr);
3558 static const struct got_error *
3559 show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3561 const char *refname;
3563 if (worktree == NULL)
3564 return got_error(GOT_ERR_NOT_WORKTREE);
3566 refname = got_worktree_get_head_ref_name(worktree);
3568 if (strncmp(refname, "refs/heads/", 11) == 0)
3570 if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3573 printf("%s\n", refname);
3578 static const struct got_error *
3579 list_branches(struct got_repository *repo, struct got_worktree *worktree)
3581 static const struct got_error *err = NULL;
3582 struct got_reflist_head refs;
3583 struct got_reflist_entry *re;
3584 struct got_reference *temp_ref = NULL;
3585 int rebase_in_progress, histedit_in_progress;
3587 SIMPLEQ_INIT(&refs);
3590 err = got_worktree_rebase_in_progress(&rebase_in_progress,
3595 err = got_worktree_histedit_in_progress(&histedit_in_progress,
3600 if (rebase_in_progress || histedit_in_progress) {
3601 err = got_ref_open(&temp_ref, repo,
3602 got_worktree_get_head_ref_name(worktree), 0);
3605 list_branch(repo, worktree, temp_ref);
3606 got_ref_close(temp_ref);
3610 err = got_ref_list(&refs, repo, "refs/heads",
3611 got_ref_cmp_by_name, NULL);
3615 SIMPLEQ_FOREACH(re, &refs, entry)
3616 list_branch(repo, worktree, re->ref);
3618 got_ref_list_free(&refs);
3622 static const struct got_error *
3623 delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3624 const char *branch_name)
3626 const struct got_error *err = NULL;
3627 struct got_reference *ref = NULL;
3630 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3631 return got_error_from_errno("asprintf");
3633 err = got_ref_open(&ref, repo, refname, 0);
3638 strcmp(got_worktree_get_head_ref_name(worktree),
3639 got_ref_get_name(ref)) == 0) {
3640 err = got_error_msg(GOT_ERR_SAME_BRANCH,
3641 "will not delete this work tree's current branch");
3645 err = got_ref_delete(ref, repo);
3653 static const struct got_error *
3654 add_branch(struct got_repository *repo, const char *branch_name,
3655 struct got_object_id *base_commit_id)
3657 const struct got_error *err = NULL;
3658 struct got_reference *ref = NULL;
3659 char *base_refname = NULL, *refname = NULL;
3662 * Don't let the user create a branch name with a leading '-'.
3663 * While technically a valid reference name, this case is usually
3664 * an unintended typo.
3666 if (branch_name[0] == '-')
3667 return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3669 if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3670 err = got_error_from_errno("asprintf");
3674 err = got_ref_open(&ref, repo, refname, 0);
3676 err = got_error(GOT_ERR_BRANCH_EXISTS);
3678 } else if (err->code != GOT_ERR_NOT_REF)
3681 err = got_ref_alloc(&ref, refname, base_commit_id);
3685 err = got_ref_write(ref, repo);
3694 static const struct got_error *
3695 cmd_branch(int argc, char *argv[])
3697 const struct got_error *error = NULL;
3698 struct got_repository *repo = NULL;
3699 struct got_worktree *worktree = NULL;
3700 char *cwd = NULL, *repo_path = NULL;
3701 int ch, do_list = 0, do_show = 0, do_update = 1;
3702 const char *delref = NULL, *commit_id_arg = NULL;
3703 struct got_reference *ref = NULL;
3704 struct got_pathlist_head paths;
3705 struct got_pathlist_entry *pe;
3706 struct got_object_id *commit_id = NULL;
3707 char *commit_id_str = NULL;
3711 while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3714 commit_id_arg = optarg;
3720 repo_path = realpath(optarg, NULL);
3721 if (repo_path == NULL)
3722 return got_error_from_errno2("realpath",
3724 got_path_strip_trailing_slashes(repo_path);
3738 if (do_list && delref)
3739 errx(1, "-l and -d options are mutually exclusive\n");
3744 if (!do_list && !delref && argc == 0)
3747 if ((do_list || delref || do_show) && commit_id_arg != NULL)
3748 errx(1, "-c option can only be used when creating a branch");
3750 if (do_list || delref) {
3753 } else if (!do_show && argc != 1)
3757 if (do_list || do_show) {
3758 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3762 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3763 "sendfd unveil", NULL) == -1)
3767 cwd = getcwd(NULL, 0);
3769 error = got_error_from_errno("getcwd");
3773 if (repo_path == NULL) {
3774 error = got_worktree_open(&worktree, cwd);
3775 if (error && error->code != GOT_ERR_NOT_WORKTREE)
3781 strdup(got_worktree_get_repo_path(worktree));
3782 if (repo_path == NULL)
3783 error = got_error_from_errno("strdup");
3787 repo_path = strdup(cwd);
3788 if (repo_path == NULL) {
3789 error = got_error_from_errno("strdup");
3795 error = got_repo_open(&repo, repo_path, NULL);
3799 error = apply_unveil(got_repo_get_path(repo), do_list,
3800 worktree ? got_worktree_get_root_path(worktree) : NULL);
3805 error = show_current_branch(repo, worktree);
3807 error = list_branches(repo, worktree);
3809 error = delete_branch(repo, worktree, delref);
3811 if (commit_id_arg == NULL)
3812 commit_id_arg = worktree ?
3813 got_worktree_get_head_ref_name(worktree) :
3815 error = got_repo_match_object_id(&commit_id, NULL,
3816 commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3819 error = add_branch(repo, argv[0], commit_id);
3822 if (worktree && do_update) {
3823 int did_something = 0;
3824 char *branch_refname = NULL;
3826 error = got_object_id_str(&commit_id_str, commit_id);
3829 error = get_worktree_paths_from_argv(&paths, 0, NULL,
3833 if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3835 error = got_error_from_errno("asprintf");
3838 error = got_ref_open(&ref, repo, branch_refname, 0);
3839 free(branch_refname);
3842 error = switch_head_ref(ref, commit_id, worktree,
3846 error = got_worktree_set_base_commit_id(worktree, repo,
3850 error = got_worktree_checkout_files(worktree, &paths,
3851 repo, update_progress, &did_something,
3852 check_cancelled, NULL);
3856 printf("Updated to commit %s\n", commit_id_str);
3863 got_repo_close(repo);
3865 got_worktree_close(worktree);
3869 free(commit_id_str);
3870 TAILQ_FOREACH(pe, &paths, entry)
3871 free((char *)pe->path);
3872 got_pathlist_free(&paths);
3881 "usage: %s tag [-c commit] [-r repository] [-l] "
3882 "[-m message] name\n", getprogname());
3887 static const struct got_error *
3888 sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3890 const struct got_error *err = NULL;
3891 struct got_reflist_entry *re, *se, *new;
3892 struct got_object_id *re_id, *se_id;
3893 struct got_tag_object *re_tag, *se_tag;
3894 time_t re_time, se_time;
3896 SIMPLEQ_FOREACH(re, tags, entry) {
3897 se = SIMPLEQ_FIRST(sorted);
3899 err = got_reflist_entry_dup(&new, re);
3902 SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3905 err = got_ref_resolve(&re_id, repo, re->ref);
3908 err = got_object_open_as_tag(&re_tag, repo, re_id);
3912 re_time = got_object_tag_get_tagger_time(re_tag);
3913 got_object_tag_close(re_tag);
3917 err = got_ref_resolve(&se_id, repo, re->ref);
3920 err = got_object_open_as_tag(&se_tag, repo, se_id);
3924 se_time = got_object_tag_get_tagger_time(se_tag);
3925 got_object_tag_close(se_tag);
3927 if (se_time > re_time) {
3928 err = got_reflist_entry_dup(&new, re);
3931 SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3934 se = SIMPLEQ_NEXT(se, entry);
3943 static const struct got_error *
3944 list_tags(struct got_repository *repo, struct got_worktree *worktree)
3946 static const struct got_error *err = NULL;
3947 struct got_reflist_head refs;
3948 struct got_reflist_entry *re;
3950 SIMPLEQ_INIT(&refs);
3952 err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3956 SIMPLEQ_FOREACH(re, &refs, entry) {
3957 const char *refname;
3958 char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3962 struct got_object_id *id;
3963 struct got_tag_object *tag;
3964 struct got_commit_object *commit = NULL;
3966 refname = got_ref_get_name(re->ref);
3967 if (strncmp(refname, "refs/tags/", 10) != 0)
3970 refstr = got_ref_to_str(re->ref);
3971 if (refstr == NULL) {
3972 err = got_error_from_errno("got_ref_to_str");
3975 printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3978 err = got_ref_resolve(&id, repo, re->ref);
3981 err = got_object_open_as_tag(&tag, repo, id);
3983 if (err->code != GOT_ERR_OBJ_TYPE) {
3987 /* "lightweight" tag */
3988 err = got_object_open_as_commit(&commit, repo, id);
3993 tagger = got_object_commit_get_committer(commit);
3995 got_object_commit_get_committer_time(commit);
3996 err = got_object_id_str(&id_str, id);
4002 tagger = got_object_tag_get_tagger(tag);
4003 tagger_time = got_object_tag_get_tagger_time(tag);
4004 err = got_object_id_str(&id_str,
4005 got_object_tag_get_object_id(tag));
4009 printf("from: %s\n", tagger);
4010 datestr = get_datestr(&tagger_time, datebuf);
4012 printf("date: %s UTC\n", datestr);
4014 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4016 switch (got_object_tag_get_object_type(tag)) {
4017 case GOT_OBJ_TYPE_BLOB:
4018 printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4021 case GOT_OBJ_TYPE_TREE:
4022 printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4025 case GOT_OBJ_TYPE_COMMIT:
4026 printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4029 case GOT_OBJ_TYPE_TAG:
4030 printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4039 err = got_object_commit_get_logmsg(&tagmsg0, commit);
4042 got_object_commit_close(commit);
4044 tagmsg0 = strdup(got_object_tag_get_message(tag));
4045 got_object_tag_close(tag);
4046 if (tagmsg0 == NULL) {
4047 err = got_error_from_errno("strdup");
4054 line = strsep(&tagmsg, "\n");
4056 printf(" %s\n", line);
4061 got_ref_list_free(&refs);
4065 static const struct got_error *
4066 get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4067 const char *tag_name, const char *repo_path)
4069 const struct got_error *err = NULL;
4070 char *template = NULL, *initial_content = NULL;
4071 char *editor = NULL;
4074 if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4075 err = got_error_from_errno("asprintf");
4079 if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4080 commit_id_str, tag_name) == -1) {
4081 err = got_error_from_errno("asprintf");
4085 err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4089 dprintf(fd, initial_content);
4092 err = get_editor(&editor);
4095 err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4097 free(initial_content);
4101 /* Editor is done; we can now apply unveil(2) */
4103 err = apply_unveil(repo_path, 0, NULL);
4112 static const struct got_error *
4113 add_tag(struct got_repository *repo, const char *tag_name,
4114 const char *commit_arg, const char *tagmsg_arg)
4116 const struct got_error *err = NULL;
4117 struct got_object_id *commit_id = NULL, *tag_id = NULL;
4118 char *label = NULL, *commit_id_str = NULL;
4119 struct got_reference *ref = NULL;
4120 char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4121 char *tagmsg_path = NULL, *tag_id_str = NULL;
4122 int preserve_tagmsg = 0;
4125 * Don't let the user create a tag name with a leading '-'.
4126 * While technically a valid reference name, this case is usually
4127 * an unintended typo.
4129 if (tag_name[0] == '-')
4130 return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4132 err = get_author(&tagger, repo);
4136 err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4137 GOT_OBJ_TYPE_COMMIT, 1, repo);
4141 err = got_object_id_str(&commit_id_str, commit_id);
4145 if (strncmp("refs/tags/", tag_name, 10) == 0) {
4146 refname = strdup(tag_name);
4147 if (refname == NULL) {
4148 err = got_error_from_errno("strdup");
4152 } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4153 err = got_error_from_errno("asprintf");
4157 err = got_ref_open(&ref, repo, refname, 0);
4159 err = got_error(GOT_ERR_TAG_EXISTS);
4161 } else if (err->code != GOT_ERR_NOT_REF)
4164 if (tagmsg_arg == NULL) {
4165 err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4166 tag_name, got_repo_get_path(repo));
4168 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4169 tagmsg_path != NULL)
4170 preserve_tagmsg = 1;
4175 err = got_object_tag_create(&tag_id, tag_name, commit_id,
4176 tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4179 preserve_tagmsg = 1;
4183 err = got_ref_alloc(&ref, refname, tag_id);
4186 preserve_tagmsg = 1;
4190 err = got_ref_write(ref, repo);
4193 preserve_tagmsg = 1;
4197 err = got_object_id_str(&tag_id_str, tag_id);
4200 preserve_tagmsg = 1;
4203 printf("Created tag %s\n", tag_id_str);
4205 if (preserve_tagmsg) {
4206 fprintf(stderr, "%s: tag message preserved in %s\n",
4207 getprogname(), tagmsg_path);
4208 } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4209 err = got_error_from_errno2("unlink", tagmsg_path);
4214 free(commit_id_str);
4222 static const struct got_error *
4223 cmd_tag(int argc, char *argv[])
4225 const struct got_error *error = NULL;
4226 struct got_repository *repo = NULL;
4227 struct got_worktree *worktree = NULL;
4228 char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4229 char *gitconfig_path = NULL;
4230 const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4231 int ch, do_list = 0;
4233 while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4236 commit_id_arg = optarg;
4242 repo_path = realpath(optarg, NULL);
4243 if (repo_path == NULL)
4244 return got_error_from_errno2("realpath",
4246 got_path_strip_trailing_slashes(repo_path);
4261 if (commit_id_arg != NULL)
4262 errx(1, "-c option can only be used when creating a tag");
4264 errx(1, "-l and -m options are mutually exclusive");
4267 } else if (argc != 1)
4274 if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4278 if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4279 "sendfd unveil", NULL) == -1)
4283 cwd = getcwd(NULL, 0);
4285 error = got_error_from_errno("getcwd");
4289 if (repo_path == NULL) {
4290 error = got_worktree_open(&worktree, cwd);
4291 if (error && error->code != GOT_ERR_NOT_WORKTREE)
4297 strdup(got_worktree_get_repo_path(worktree));
4298 if (repo_path == NULL)
4299 error = got_error_from_errno("strdup");
4303 repo_path = strdup(cwd);
4304 if (repo_path == NULL) {
4305 error = got_error_from_errno("strdup");
4312 error = got_repo_open(&repo, repo_path, NULL);
4315 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4318 error = list_tags(repo, worktree);
4320 error = get_gitconfig_path(&gitconfig_path);
4323 error = got_repo_open(&repo, repo_path, gitconfig_path);
4328 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4333 if (commit_id_arg == NULL) {
4334 struct got_reference *head_ref;
4335 struct got_object_id *commit_id;
4336 error = got_ref_open(&head_ref, repo,
4337 worktree ? got_worktree_get_head_ref_name(worktree)
4341 error = got_ref_resolve(&commit_id, repo, head_ref);
4342 got_ref_close(head_ref);
4345 error = got_object_id_str(&commit_id_str, commit_id);
4351 error = add_tag(repo, tag_name,
4352 commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4356 got_repo_close(repo);
4358 got_worktree_close(worktree);
4361 free(gitconfig_path);
4362 free(commit_id_str);
4369 fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4374 static const struct got_error *
4375 add_progress(void *arg, unsigned char status, const char *path)
4377 while (path[0] == '/')
4379 printf("%c %s\n", status, path);
4383 static const struct got_error *
4384 cmd_add(int argc, char *argv[])
4386 const struct got_error *error = NULL;
4387 struct got_repository *repo = NULL;
4388 struct got_worktree *worktree = NULL;
4390 struct got_pathlist_head paths;
4391 struct got_pathlist_entry *pe;
4392 int ch, can_recurse = 0, no_ignores = 0;
4396 while ((ch = getopt(argc, argv, "IR")) != -1) {
4414 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4421 cwd = getcwd(NULL, 0);
4423 error = got_error_from_errno("getcwd");
4427 error = got_worktree_open(&worktree, cwd);
4431 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4436 error = apply_unveil(got_repo_get_path(repo), 1,
4437 got_worktree_get_root_path(worktree));
4441 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4445 if (!can_recurse && no_ignores) {
4446 error = got_error_msg(GOT_ERR_BAD_PATH,
4447 "disregarding ignores requires -R option");
4455 TAILQ_FOREACH(pe, &paths, entry) {
4456 if (asprintf(&ondisk_path, "%s/%s",
4457 got_worktree_get_root_path(worktree),
4459 error = got_error_from_errno("asprintf");
4462 if (lstat(ondisk_path, &sb) == -1) {
4463 if (errno == ENOENT) {
4467 error = got_error_from_errno2("lstat",
4473 if (S_ISDIR(sb.st_mode)) {
4474 error = got_error_msg(GOT_ERR_BAD_PATH,
4475 "adding directories requires -R option");
4481 error = got_worktree_schedule_add(worktree, &paths, add_progress,
4482 NULL, repo, no_ignores);
4485 got_repo_close(repo);
4487 got_worktree_close(worktree);
4488 TAILQ_FOREACH(pe, &paths, entry)
4489 free((char *)pe->path);
4490 got_pathlist_free(&paths);
4498 fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4503 static const struct got_error *
4504 print_remove_status(void *arg, unsigned char status,
4505 unsigned char staged_status, const char *path)
4507 while (path[0] == '/')
4509 if (status == GOT_STATUS_NONEXISTENT)
4511 if (status == staged_status && (status == GOT_STATUS_DELETE))
4512 status = GOT_STATUS_NO_CHANGE;
4513 printf("%c%c %s\n", status, staged_status, path);
4517 static const struct got_error *
4518 cmd_remove(int argc, char *argv[])
4520 const struct got_error *error = NULL;
4521 struct got_worktree *worktree = NULL;
4522 struct got_repository *repo = NULL;
4524 struct got_pathlist_head paths;
4525 struct got_pathlist_entry *pe;
4526 int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4530 while ((ch = getopt(argc, argv, "fkR")) != -1) {
4533 delete_local_mods = 1;
4551 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4558 cwd = getcwd(NULL, 0);
4560 error = got_error_from_errno("getcwd");
4563 error = got_worktree_open(&worktree, cwd);
4567 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4572 error = apply_unveil(got_repo_get_path(repo), 1,
4573 got_worktree_get_root_path(worktree));
4577 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4584 TAILQ_FOREACH(pe, &paths, entry) {
4585 if (asprintf(&ondisk_path, "%s/%s",
4586 got_worktree_get_root_path(worktree),
4588 error = got_error_from_errno("asprintf");
4591 if (lstat(ondisk_path, &sb) == -1) {
4592 if (errno == ENOENT) {
4596 error = got_error_from_errno2("lstat",
4602 if (S_ISDIR(sb.st_mode)) {
4603 error = got_error_msg(GOT_ERR_BAD_PATH,
4604 "removing directories requires -R option");
4610 error = got_worktree_schedule_delete(worktree, &paths,
4611 delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4614 got_repo_close(repo);
4616 got_worktree_close(worktree);
4617 TAILQ_FOREACH(pe, &paths, entry)
4618 free((char *)pe->path);
4619 got_pathlist_free(&paths);
4627 fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4628 "path ...\n", getprogname());
4632 static const struct got_error *
4633 revert_progress(void *arg, unsigned char status, const char *path)
4635 if (status == GOT_STATUS_UNVERSIONED)
4638 while (path[0] == '/')
4640 printf("%c %s\n", status, path);
4644 struct choose_patch_arg {
4645 FILE *patch_script_file;
4649 static const struct got_error *
4650 show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4651 int nchanges, const char *action)
4654 size_t linesize = 0;
4658 case GOT_STATUS_ADD:
4659 printf("A %s\n%s this addition? [y/n] ", path, action);
4661 case GOT_STATUS_DELETE:
4662 printf("D %s\n%s this deletion? [y/n] ", path, action);
4664 case GOT_STATUS_MODIFY:
4665 if (fseek(patch_file, 0L, SEEK_SET) == -1)
4666 return got_error_from_errno("fseek");
4667 printf(GOT_COMMIT_SEP_STR);
4668 while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4670 if (ferror(patch_file))
4671 return got_error_from_errno("getline");
4672 printf(GOT_COMMIT_SEP_STR);
4673 printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4674 path, n, nchanges, action);
4677 return got_error_path(path, GOT_ERR_FILE_STATUS);
4683 static const struct got_error *
4684 choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4685 FILE *patch_file, int n, int nchanges)
4687 const struct got_error *err = NULL;
4689 size_t linesize = 0;
4692 struct choose_patch_arg *a = arg;
4694 *choice = GOT_PATCH_CHOICE_NONE;
4696 if (a->patch_script_file) {
4698 err = show_change(status, path, patch_file, n, nchanges,
4702 linelen = getline(&line, &linesize, a->patch_script_file);
4703 if (linelen == -1) {
4704 if (ferror(a->patch_script_file))
4705 return got_error_from_errno("getline");
4708 nl = strchr(line, '\n');
4711 if (strcmp(line, "y") == 0) {
4712 *choice = GOT_PATCH_CHOICE_YES;
4714 } else if (strcmp(line, "n") == 0) {
4715 *choice = GOT_PATCH_CHOICE_NO;
4717 } else if (strcmp(line, "q") == 0 &&
4718 status == GOT_STATUS_MODIFY) {
4719 *choice = GOT_PATCH_CHOICE_QUIT;
4722 printf("invalid response '%s'\n", line);
4727 while (resp != 'y' && resp != 'n' && resp != 'q') {
4728 err = show_change(status, path, patch_file, n, nchanges,
4735 if (status == GOT_STATUS_MODIFY) {
4736 if (resp != 'y' && resp != 'n' && resp != 'q') {
4737 printf("invalid response '%c'\n", resp);
4740 } else if (resp != 'y' && resp != 'n') {
4741 printf("invalid response '%c'\n", resp);
4747 *choice = GOT_PATCH_CHOICE_YES;
4748 else if (resp == 'n')
4749 *choice = GOT_PATCH_CHOICE_NO;
4750 else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4751 *choice = GOT_PATCH_CHOICE_QUIT;
4757 static const struct got_error *
4758 cmd_revert(int argc, char *argv[])
4760 const struct got_error *error = NULL;
4761 struct got_worktree *worktree = NULL;
4762 struct got_repository *repo = NULL;
4763 char *cwd = NULL, *path = NULL;
4764 struct got_pathlist_head paths;
4765 struct got_pathlist_entry *pe;
4766 int ch, can_recurse = 0, pflag = 0;
4767 FILE *patch_script_file = NULL;
4768 const char *patch_script_path = NULL;
4769 struct choose_patch_arg cpa;
4773 while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4779 patch_script_path = optarg;
4794 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4795 "unveil", NULL) == -1)
4800 if (patch_script_path && !pflag)
4801 errx(1, "-F option can only be used together with -p option");
4803 cwd = getcwd(NULL, 0);
4805 error = got_error_from_errno("getcwd");
4808 error = got_worktree_open(&worktree, cwd);
4812 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4817 if (patch_script_path) {
4818 patch_script_file = fopen(patch_script_path, "r");
4819 if (patch_script_file == NULL) {
4820 error = got_error_from_errno2("fopen",
4825 error = apply_unveil(got_repo_get_path(repo), 1,
4826 got_worktree_get_root_path(worktree));
4830 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4837 TAILQ_FOREACH(pe, &paths, entry) {
4838 if (asprintf(&ondisk_path, "%s/%s",
4839 got_worktree_get_root_path(worktree),
4841 error = got_error_from_errno("asprintf");
4844 if (lstat(ondisk_path, &sb) == -1) {
4845 if (errno == ENOENT) {
4849 error = got_error_from_errno2("lstat",
4855 if (S_ISDIR(sb.st_mode)) {
4856 error = got_error_msg(GOT_ERR_BAD_PATH,
4857 "reverting directories requires -R option");
4863 cpa.patch_script_file = patch_script_file;
4864 cpa.action = "revert";
4865 error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4866 pflag ? choose_patch : NULL, &cpa, repo);
4868 if (patch_script_file && fclose(patch_script_file) == EOF &&
4870 error = got_error_from_errno2("fclose", patch_script_path);
4872 got_repo_close(repo);
4874 got_worktree_close(worktree);
4883 fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4888 struct collect_commit_logmsg_arg {
4889 const char *cmdline_log;
4891 const char *worktree_path;
4892 const char *branch_name;
4893 const char *repo_path;
4898 static const struct got_error *
4899 collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4902 char *initial_content = NULL;
4903 struct got_pathlist_entry *pe;
4904 const struct got_error *err = NULL;
4905 char *template = NULL;
4906 struct collect_commit_logmsg_arg *a = arg;
4910 /* if a message was specified on the command line, just use it */
4911 if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4912 len = strlen(a->cmdline_log) + 1;
4913 *logmsg = malloc(len + 1);
4914 if (*logmsg == NULL)
4915 return got_error_from_errno("malloc");
4916 strlcpy(*logmsg, a->cmdline_log, len);
4920 if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4921 return got_error_from_errno("asprintf");
4923 if (asprintf(&initial_content,
4924 "\n# changes to be committed on branch %s:\n",
4925 a->branch_name) == -1)
4926 return got_error_from_errno("asprintf");
4928 err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4932 dprintf(fd, initial_content);
4934 TAILQ_FOREACH(pe, commitable_paths, entry) {
4935 struct got_commitable *ct = pe->data;
4936 dprintf(fd, "# %c %s\n",
4937 got_commitable_get_status(ct),
4938 got_commitable_get_path(ct));
4942 err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4944 free(initial_content);
4947 /* Editor is done; we can now apply unveil(2) */
4949 err = apply_unveil(a->repo_path, 0, a->worktree_path);
4958 static const struct got_error *
4959 cmd_commit(int argc, char *argv[])
4961 const struct got_error *error = NULL;
4962 struct got_worktree *worktree = NULL;
4963 struct got_repository *repo = NULL;
4964 char *cwd = NULL, *id_str = NULL;
4965 struct got_object_id *id = NULL;
4966 const char *logmsg = NULL;
4967 struct collect_commit_logmsg_arg cl_arg;
4968 char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4969 int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4970 struct got_pathlist_head paths;
4973 cl_arg.logmsg_path = NULL;
4975 while ((ch = getopt(argc, argv, "m:")) != -1) {
4990 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4991 "unveil", NULL) == -1)
4994 cwd = getcwd(NULL, 0);
4996 error = got_error_from_errno("getcwd");
4999 error = got_worktree_open(&worktree, cwd);
5003 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5006 if (rebase_in_progress) {
5007 error = got_error(GOT_ERR_REBASING);
5011 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5016 error = get_gitconfig_path(&gitconfig_path);
5019 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5024 error = get_author(&author, repo);
5029 * unveil(2) traverses exec(2); if an editor is used we have
5030 * to apply unveil after the log message has been written.
5032 if (logmsg == NULL || strlen(logmsg) == 0)
5033 error = get_editor(&editor);
5035 error = apply_unveil(got_repo_get_path(repo), 0,
5036 got_worktree_get_root_path(worktree));
5040 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5044 cl_arg.editor = editor;
5045 cl_arg.cmdline_log = logmsg;
5046 cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5047 cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5048 if (!histedit_in_progress) {
5049 if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5050 error = got_error(GOT_ERR_COMMIT_BRANCH);
5053 cl_arg.branch_name += 11;
5055 cl_arg.repo_path = got_repo_get_path(repo);
5056 error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5057 collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5059 if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5060 cl_arg.logmsg_path != NULL)
5061 preserve_logmsg = 1;
5065 error = got_object_id_str(&id_str, id);
5068 printf("Created commit %s\n", id_str);
5070 if (preserve_logmsg) {
5071 fprintf(stderr, "%s: log message preserved in %s\n",
5072 getprogname(), cl_arg.logmsg_path);
5073 } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5075 error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5076 free(cl_arg.logmsg_path);
5078 got_repo_close(repo);
5080 got_worktree_close(worktree);
5083 free(gitconfig_path);
5090 usage_cherrypick(void)
5092 fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5096 static const struct got_error *
5097 cmd_cherrypick(int argc, char *argv[])
5099 const struct got_error *error = NULL;
5100 struct got_worktree *worktree = NULL;
5101 struct got_repository *repo = NULL;
5102 char *cwd = NULL, *commit_id_str = NULL;
5103 struct got_object_id *commit_id = NULL;
5104 struct got_commit_object *commit = NULL;
5105 struct got_object_qid *pid;
5106 struct got_reference *head_ref = NULL;
5107 int ch, did_something = 0;
5109 while ((ch = getopt(argc, argv, "")) != -1) {
5121 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5122 "unveil", NULL) == -1)
5128 cwd = getcwd(NULL, 0);
5130 error = got_error_from_errno("getcwd");
5133 error = got_worktree_open(&worktree, cwd);
5137 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5142 error = apply_unveil(got_repo_get_path(repo), 0,
5143 got_worktree_get_root_path(worktree));
5147 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5148 GOT_OBJ_TYPE_COMMIT, repo);
5149 if (error != NULL) {
5150 struct got_reference *ref;
5151 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5153 error = got_ref_open(&ref, repo, argv[0], 0);
5156 error = got_ref_resolve(&commit_id, repo, ref);
5161 error = got_object_id_str(&commit_id_str, commit_id);
5165 error = got_ref_open(&head_ref, repo,
5166 got_worktree_get_head_ref_name(worktree), 0);
5170 error = check_same_branch(commit_id, head_ref, NULL, repo);
5172 if (error->code != GOT_ERR_ANCESTRY)
5176 error = got_error(GOT_ERR_SAME_BRANCH);
5180 error = got_object_open_as_commit(&commit, repo, commit_id);
5183 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5184 error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5185 commit_id, repo, update_progress, &did_something, check_cancelled,
5191 printf("Merged commit %s\n", commit_id_str);
5194 got_object_commit_close(commit);
5195 free(commit_id_str);
5197 got_ref_close(head_ref);
5199 got_worktree_close(worktree);
5201 got_repo_close(repo);
5208 fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5212 static const struct got_error *
5213 cmd_backout(int argc, char *argv[])
5215 const struct got_error *error = NULL;
5216 struct got_worktree *worktree = NULL;
5217 struct got_repository *repo = NULL;
5218 char *cwd = NULL, *commit_id_str = NULL;
5219 struct got_object_id *commit_id = NULL;
5220 struct got_commit_object *commit = NULL;
5221 struct got_object_qid *pid;
5222 struct got_reference *head_ref = NULL;
5223 int ch, did_something = 0;
5225 while ((ch = getopt(argc, argv, "")) != -1) {
5237 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5238 "unveil", NULL) == -1)
5244 cwd = getcwd(NULL, 0);
5246 error = got_error_from_errno("getcwd");
5249 error = got_worktree_open(&worktree, cwd);
5253 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5258 error = apply_unveil(got_repo_get_path(repo), 0,
5259 got_worktree_get_root_path(worktree));
5263 error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5264 GOT_OBJ_TYPE_COMMIT, repo);
5265 if (error != NULL) {
5266 struct got_reference *ref;
5267 if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5269 error = got_ref_open(&ref, repo, argv[0], 0);
5272 error = got_ref_resolve(&commit_id, repo, ref);
5277 error = got_object_id_str(&commit_id_str, commit_id);
5281 error = got_ref_open(&head_ref, repo,
5282 got_worktree_get_head_ref_name(worktree), 0);
5286 error = check_same_branch(commit_id, head_ref, NULL, repo);
5290 error = got_object_open_as_commit(&commit, repo, commit_id);
5293 pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5295 error = got_error(GOT_ERR_ROOT_COMMIT);
5299 error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5300 update_progress, &did_something, check_cancelled, NULL);
5305 printf("Backed out commit %s\n", commit_id_str);
5308 got_object_commit_close(commit);
5309 free(commit_id_str);
5311 got_ref_close(head_ref);
5313 got_worktree_close(worktree);
5315 got_repo_close(repo);
5322 fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5328 trim_logmsg(char *logmsg, int limit)
5333 len = strlen(logmsg);
5337 nl = strchr(logmsg, '\n');
5342 static const struct got_error *
5343 get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5345 const struct got_error *err;
5346 char *logmsg0 = NULL;
5349 err = got_object_commit_get_logmsg(&logmsg0, commit);
5354 while (isspace((unsigned char)s[0]))
5357 *logmsg = strdup(s);
5358 if (*logmsg == NULL) {
5359 err = got_error_from_errno("strdup");
5363 trim_logmsg(*logmsg, limit);
5369 static const struct got_error *
5370 show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5372 const struct got_error *err;
5373 struct got_commit_object *commit = NULL;
5374 char *id_str = NULL, *logmsg = NULL;
5376 err = got_object_open_as_commit(&commit, repo, id);
5380 err = got_object_id_str(&id_str, id);
5386 err = get_short_logmsg(&logmsg, 42, commit);
5390 printf("%s -> merge conflict: %s\n", id_str, logmsg);
5393 got_object_commit_close(commit);
5398 static const struct got_error *
5399 show_rebase_progress(struct got_commit_object *commit,
5400 struct got_object_id *old_id, struct got_object_id *new_id)
5402 const struct got_error *err;
5403 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5405 err = got_object_id_str(&old_id_str, old_id);
5410 err = got_object_id_str(&new_id_str, new_id);
5415 old_id_str[12] = '\0';
5417 new_id_str[12] = '\0';
5419 err = get_short_logmsg(&logmsg, 42, commit);
5423 printf("%s -> %s: %s\n", old_id_str,
5424 new_id_str ? new_id_str : "no-op change", logmsg);
5432 static const struct got_error *
5433 rebase_progress(void *arg, unsigned char status, const char *path)
5435 unsigned char *rebase_status = arg;
5437 while (path[0] == '/')
5439 printf("%c %s\n", status, path);
5441 if (*rebase_status == GOT_STATUS_CONFLICT)
5443 if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5444 *rebase_status = status;
5448 static const struct got_error *
5449 rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5450 struct got_reference *branch, struct got_reference *new_base_branch,
5451 struct got_reference *tmp_branch, struct got_repository *repo)
5453 printf("Switching work tree to %s\n", got_ref_get_name(branch));
5454 return got_worktree_rebase_complete(worktree, fileindex,
5455 new_base_branch, tmp_branch, branch, repo);
5458 static const struct got_error *
5459 rebase_commit(struct got_pathlist_head *merged_paths,
5460 struct got_worktree *worktree, struct got_fileindex *fileindex,
5461 struct got_reference *tmp_branch,
5462 struct got_object_id *commit_id, struct got_repository *repo)
5464 const struct got_error *error;
5465 struct got_commit_object *commit;
5466 struct got_object_id *new_commit_id;
5468 error = got_object_open_as_commit(&commit, repo, commit_id);
5472 error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5473 worktree, fileindex, tmp_branch, commit, commit_id, repo);
5475 if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5477 error = show_rebase_progress(commit, commit_id, NULL);
5479 error = show_rebase_progress(commit, commit_id, new_commit_id);
5480 free(new_commit_id);
5483 got_object_commit_close(commit);
5487 struct check_path_prefix_arg {
5488 const char *path_prefix;
5493 static const struct got_error *
5494 check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5495 struct got_blob_object *blob2, struct got_object_id *id1,
5496 struct got_object_id *id2, const char *path1, const char *path2,
5497 mode_t mode1, mode_t mode2, struct got_repository *repo)
5499 struct check_path_prefix_arg *a = arg;
5501 if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5502 (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5503 return got_error(a->errcode);
5508 static const struct got_error *
5509 check_path_prefix(struct got_object_id *parent_id,
5510 struct got_object_id *commit_id, const char *path_prefix,
5511 int errcode, struct got_repository *repo)
5513 const struct got_error *err;
5514 struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5515 struct got_commit_object *commit = NULL, *parent_commit = NULL;
5516 struct check_path_prefix_arg cpp_arg;
5518 if (got_path_is_root_dir(path_prefix))
5521 err = got_object_open_as_commit(&commit, repo, commit_id);
5525 err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5529 err = got_object_open_as_tree(&tree1, repo,
5530 got_object_commit_get_tree_id(parent_commit));
5534 err = got_object_open_as_tree(&tree2, repo,
5535 got_object_commit_get_tree_id(commit));
5539 cpp_arg.path_prefix = path_prefix;
5540 while (cpp_arg.path_prefix[0] == '/')
5541 cpp_arg.path_prefix++;
5542 cpp_arg.len = strlen(cpp_arg.path_prefix);
5543 cpp_arg.errcode = errcode;
5544 err = got_diff_tree(tree1, tree2, "", "", repo,
5545 check_path_prefix_in_diff, &cpp_arg, 0);
5548 got_object_tree_close(tree1);
5550 got_object_tree_close(tree2);
5552 got_object_commit_close(commit);
5554 got_object_commit_close(parent_commit);
5558 static const struct got_error *
5559 collect_commits(struct got_object_id_queue *commits,
5560 struct got_object_id *initial_commit_id,
5561 struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5562 const char *path_prefix, int path_prefix_errcode,
5563 struct got_repository *repo)
5565 const struct got_error *err = NULL;
5566 struct got_commit_graph *graph = NULL;
5567 struct got_object_id *parent_id = NULL;
5568 struct got_object_qid *qid;
5569 struct got_object_id *commit_id = initial_commit_id;
5571 err = got_commit_graph_open(&graph, "/", 1);
5575 err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5576 check_cancelled, NULL);
5579 while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5580 err = got_commit_graph_iter_next(&parent_id, graph, repo,
5581 check_cancelled, NULL);
5583 if (err->code == GOT_ERR_ITER_COMPLETED) {
5584 err = got_error_msg(GOT_ERR_ANCESTRY,
5585 "ran out of commits to rebase before "
5586 "youngest common ancestor commit has "
5591 err = check_path_prefix(parent_id, commit_id,
5592 path_prefix, path_prefix_errcode, repo);
5596 err = got_object_qid_alloc(&qid, commit_id);
5599 SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5600 commit_id = parent_id;
5604 got_commit_graph_close(graph);
5608 static const struct got_error *
5609 cmd_rebase(int argc, char *argv[])
5611 const struct got_error *error = NULL;
5612 struct got_worktree *worktree = NULL;
5613 struct got_repository *repo = NULL;
5614 struct got_fileindex *fileindex = NULL;
5616 struct got_reference *branch = NULL;
5617 struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5618 struct got_object_id *commit_id = NULL, *parent_id = NULL;
5619 struct got_object_id *resume_commit_id = NULL;
5620 struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5621 struct got_commit_object *commit = NULL;
5622 int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5623 int histedit_in_progress = 0;
5624 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5625 struct got_object_id_queue commits;
5626 struct got_pathlist_head merged_paths;
5627 const struct got_object_id_queue *parent_ids;
5628 struct got_object_qid *qid, *pid;
5630 SIMPLEQ_INIT(&commits);
5631 TAILQ_INIT(&merged_paths);
5633 while ((ch = getopt(argc, argv, "ac")) != -1) {
5639 continue_rebase = 1;
5651 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5652 "unveil", NULL) == -1)
5655 if (abort_rebase && continue_rebase)
5657 else if (abort_rebase || continue_rebase) {
5660 } else if (argc != 1)
5663 cwd = getcwd(NULL, 0);
5665 error = got_error_from_errno("getcwd");
5668 error = got_worktree_open(&worktree, cwd);
5672 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5677 error = apply_unveil(got_repo_get_path(repo), 0,
5678 got_worktree_get_root_path(worktree));
5682 error = got_worktree_histedit_in_progress(&histedit_in_progress,
5686 if (histedit_in_progress) {
5687 error = got_error(GOT_ERR_HISTEDIT_BUSY);
5691 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5697 if (!rebase_in_progress) {
5698 error = got_error(GOT_ERR_NOT_REBASING);
5701 error = got_worktree_rebase_continue(&resume_commit_id,
5702 &new_base_branch, &tmp_branch, &branch, &fileindex,
5706 printf("Switching work tree to %s\n",
5707 got_ref_get_symref_target(new_base_branch));
5708 error = got_worktree_rebase_abort(worktree, fileindex, repo,
5709 new_base_branch, update_progress, &did_something);
5712 printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5713 goto done; /* nothing else to do */
5716 if (continue_rebase) {
5717 if (!rebase_in_progress) {
5718 error = got_error(GOT_ERR_NOT_REBASING);
5721 error = got_worktree_rebase_continue(&resume_commit_id,
5722 &new_base_branch, &tmp_branch, &branch, &fileindex,
5727 error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5728 resume_commit_id, repo);
5732 yca_id = got_object_id_dup(resume_commit_id);
5733 if (yca_id == NULL) {
5734 error = got_error_from_errno("got_object_id_dup");
5738 error = got_ref_open(&branch, repo, argv[0], 0);
5743 error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5747 if (!continue_rebase) {
5748 struct got_object_id *base_commit_id;
5750 base_commit_id = got_worktree_get_base_commit_id(worktree);
5751 error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5752 base_commit_id, branch_head_commit_id, repo,
5753 check_cancelled, NULL);
5756 if (yca_id == NULL) {
5757 error = got_error_msg(GOT_ERR_ANCESTRY,
5758 "specified branch shares no common ancestry "
5759 "with work tree's branch");
5763 error = check_same_branch(base_commit_id, branch, yca_id, repo);
5765 if (error->code != GOT_ERR_ANCESTRY)
5769 error = got_error_msg(GOT_ERR_SAME_BRANCH,
5770 "specified branch resolves to a commit which "
5771 "is already contained in work tree's branch");
5774 error = got_worktree_rebase_prepare(&new_base_branch,
5775 &tmp_branch, &fileindex, worktree, branch, repo);
5780 commit_id = branch_head_commit_id;
5781 error = got_object_open_as_commit(&commit, repo, commit_id);
5785 parent_ids = got_object_commit_get_parent_ids(commit);
5786 pid = SIMPLEQ_FIRST(parent_ids);
5788 if (!continue_rebase) {
5790 error = got_worktree_rebase_abort(worktree, fileindex,
5791 repo, new_base_branch, update_progress,
5795 printf("Rebase of %s aborted\n",
5796 got_ref_get_name(branch));
5798 error = got_error(GOT_ERR_EMPTY_REBASE);
5801 error = collect_commits(&commits, commit_id, pid->id,
5802 yca_id, got_worktree_get_path_prefix(worktree),
5803 GOT_ERR_REBASE_PATH, repo);
5804 got_object_commit_close(commit);
5809 if (SIMPLEQ_EMPTY(&commits)) {
5810 if (continue_rebase) {
5811 error = rebase_complete(worktree, fileindex,
5812 branch, new_base_branch, tmp_branch, repo);
5815 /* Fast-forward the reference of the branch. */
5816 struct got_object_id *new_head_commit_id;
5818 error = got_ref_resolve(&new_head_commit_id, repo,
5822 error = got_object_id_str(&id_str, new_head_commit_id);
5823 printf("Forwarding %s to commit %s\n",
5824 got_ref_get_name(branch), id_str);
5826 error = got_ref_change_ref(branch,
5827 new_head_commit_id);
5834 SIMPLEQ_FOREACH(qid, &commits, entry) {
5835 commit_id = qid->id;
5836 parent_id = pid ? pid->id : yca_id;
5839 error = got_worktree_rebase_merge_files(&merged_paths,
5840 worktree, fileindex, parent_id, commit_id, repo,
5841 rebase_progress, &rebase_status, check_cancelled, NULL);
5845 if (rebase_status == GOT_STATUS_CONFLICT) {
5846 error = show_rebase_merge_conflict(qid->id, repo);
5849 got_worktree_rebase_pathlist_free(&merged_paths);
5853 error = rebase_commit(&merged_paths, worktree, fileindex,
5854 tmp_branch, commit_id, repo);
5855 got_worktree_rebase_pathlist_free(&merged_paths);
5860 if (rebase_status == GOT_STATUS_CONFLICT) {
5861 error = got_worktree_rebase_postpone(worktree, fileindex);
5864 error = got_error_msg(GOT_ERR_CONFLICTS,
5865 "conflicts must be resolved before rebasing can continue");
5867 error = rebase_complete(worktree, fileindex, branch,
5868 new_base_branch, tmp_branch, repo);
5870 got_object_id_queue_free(&commits);
5871 free(branch_head_commit_id);
5872 free(resume_commit_id);
5875 got_object_commit_close(commit);
5877 got_ref_close(branch);
5878 if (new_base_branch)
5879 got_ref_close(new_base_branch);
5881 got_ref_close(tmp_branch);
5883 got_worktree_close(worktree);
5885 got_repo_close(repo);
5890 usage_histedit(void)
5892 fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5897 #define GOT_HISTEDIT_PICK 'p'
5898 #define GOT_HISTEDIT_EDIT 'e'
5899 #define GOT_HISTEDIT_FOLD 'f'
5900 #define GOT_HISTEDIT_DROP 'd'
5901 #define GOT_HISTEDIT_MESG 'm'
5903 static struct got_histedit_cmd {
5907 } got_histedit_cmds[] = {
5908 { GOT_HISTEDIT_PICK, "pick", "use commit" },
5909 { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5910 { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5912 { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5913 { GOT_HISTEDIT_MESG, "mesg",
5914 "single-line log message for commit above (open editor if empty)" },
5917 struct got_histedit_list_entry {
5918 TAILQ_ENTRY(got_histedit_list_entry) entry;
5919 struct got_object_id *commit_id;
5920 const struct got_histedit_cmd *cmd;
5923 TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5925 static const struct got_error *
5926 histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5927 FILE *f, struct got_repository *repo)
5929 const struct got_error *err = NULL;
5930 char *logmsg = NULL, *id_str = NULL;
5931 struct got_commit_object *commit = NULL;
5934 err = got_object_open_as_commit(&commit, repo, commit_id);
5938 err = get_short_logmsg(&logmsg, 34, commit);
5942 err = got_object_id_str(&id_str, commit_id);
5946 n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5948 err = got_ferror(f, GOT_ERR_IO);
5951 got_object_commit_close(commit);
5957 static const struct got_error *
5958 histedit_write_commit_list(struct got_object_id_queue *commits,
5959 FILE *f, int edit_logmsg_only, struct got_repository *repo)
5961 const struct got_error *err = NULL;
5962 struct got_object_qid *qid;
5964 if (SIMPLEQ_EMPTY(commits))
5965 return got_error(GOT_ERR_EMPTY_HISTEDIT);
5967 SIMPLEQ_FOREACH(qid, commits, entry) {
5968 err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5972 if (edit_logmsg_only) {
5973 int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5975 err = got_ferror(f, GOT_ERR_IO);
5984 static const struct got_error *
5985 write_cmd_list(FILE *f, const char *branch_name,
5986 struct got_object_id_queue *commits)
5988 const struct got_error *err = NULL;
5991 struct got_object_qid *qid;
5993 qid = SIMPLEQ_FIRST(commits);
5994 err = got_object_id_str(&id_str, qid->id);
5999 "# Editing the history of branch '%s' starting at\n"
6001 "# Commits will be processed in order from top to "
6002 "bottom of this file.\n", branch_name, id_str);
6004 err = got_ferror(f, GOT_ERR_IO);
6008 n = fprintf(f, "# Available histedit commands:\n");
6010 err = got_ferror(f, GOT_ERR_IO);
6014 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6015 struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6016 n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6019 err = got_ferror(f, GOT_ERR_IO);
6028 static const struct got_error *
6029 histedit_syntax_error(int lineno)
6031 static char msg[42];
6034 ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6036 if (ret == -1 || ret >= sizeof(msg))
6037 return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6039 return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6042 static const struct got_error *
6043 append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6044 char *logmsg, struct got_repository *repo)
6046 const struct got_error *err;
6047 struct got_commit_object *folded_commit = NULL;
6048 char *id_str, *folded_logmsg = NULL;
6050 err = got_object_id_str(&id_str, hle->commit_id);
6054 err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6058 err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6061 if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6062 logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6063 folded_logmsg) == -1) {
6064 err = got_error_from_errno("asprintf");
6068 got_object_commit_close(folded_commit);
6070 free(folded_logmsg);
6074 static struct got_histedit_list_entry *
6075 get_folded_commits(struct got_histedit_list_entry *hle)
6077 struct got_histedit_list_entry *prev, *folded = NULL;
6079 prev = TAILQ_PREV(hle, got_histedit_list, entry);
6080 while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6081 prev->cmd->code == GOT_HISTEDIT_DROP)) {
6082 if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6084 prev = TAILQ_PREV(prev, got_histedit_list, entry);
6090 static const struct got_error *
6091 histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6092 struct got_repository *repo)
6094 char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6095 char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6096 const struct got_error *err = NULL;
6097 struct got_commit_object *commit = NULL;
6099 struct got_histedit_list_entry *folded = NULL;
6101 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6105 folded = get_folded_commits(hle);
6107 while (folded != hle) {
6108 if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6109 folded = TAILQ_NEXT(folded, entry);
6112 err = append_folded_commit_msg(&new_msg, folded,
6118 folded = TAILQ_NEXT(folded, entry);
6122 err = got_object_id_str(&id_str, hle->commit_id);
6125 err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6128 if (asprintf(&new_msg,
6129 "%s\n# original log message of commit %s: %s",
6130 logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6131 err = got_error_from_errno("asprintf");
6137 err = got_object_id_str(&id_str, hle->commit_id);
6141 err = got_opentemp_named_fd(&logmsg_path, &fd,
6142 GOT_TMPDIR_STR "/got-logmsg");
6146 dprintf(fd, logmsg);
6149 err = get_editor(&editor);
6153 err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6155 if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6157 err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6160 if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6161 err = got_error_from_errno2("unlink", logmsg_path);
6167 got_object_commit_close(commit);
6171 static const struct got_error *
6172 histedit_parse_list(struct got_histedit_list *histedit_cmds,
6173 FILE *f, struct got_repository *repo)
6175 const struct got_error *err = NULL;
6176 char *line = NULL, *p, *end;
6180 const struct got_histedit_cmd *cmd;
6181 struct got_object_id *commit_id = NULL;
6182 struct got_histedit_list_entry *hle = NULL;
6185 len = getline(&line, &size, f);
6187 const struct got_error *getline_err;
6190 getline_err = got_error_from_errno("getline");
6191 err = got_ferror(f, getline_err->code);
6196 while (isspace((unsigned char)p[0]))
6198 if (p[0] == '#' || p[0] == '\0') {
6204 for (i = 0; i < nitems(got_histedit_cmds); i++) {
6205 cmd = &got_histedit_cmds[i];
6206 if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6207 isspace((unsigned char)p[strlen(cmd->name)])) {
6208 p += strlen(cmd->name);
6211 if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6216 if (i == nitems(got_histedit_cmds)) {
6217 err = histedit_syntax_error(lineno);
6220 while (isspace((unsigned char)p[0]))
6222 if (cmd->code == GOT_HISTEDIT_MESG) {
6223 if (hle == NULL || hle->logmsg != NULL) {
6224 err = got_error(GOT_ERR_HISTEDIT_CMD);
6228 err = histedit_edit_logmsg(hle, repo);
6232 hle->logmsg = strdup(p);
6233 if (hle->logmsg == NULL) {
6234 err = got_error_from_errno("strdup");
6243 while (end[0] && !isspace((unsigned char)end[0]))
6247 err = got_object_resolve_id_str(&commit_id, repo, p);
6249 /* override error code */
6250 err = histedit_syntax_error(lineno);
6254 hle = malloc(sizeof(*hle));
6256 err = got_error_from_errno("malloc");
6260 hle->commit_id = commit_id;
6265 TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6273 static const struct got_error *
6274 histedit_check_script(struct got_histedit_list *histedit_cmds,
6275 struct got_object_id_queue *commits, struct got_repository *repo)
6277 const struct got_error *err = NULL;
6278 struct got_object_qid *qid;
6279 struct got_histedit_list_entry *hle;
6280 static char msg[92];
6283 if (TAILQ_EMPTY(histedit_cmds))
6284 return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6285 "histedit script contains no commands");
6286 if (SIMPLEQ_EMPTY(commits))
6287 return got_error(GOT_ERR_EMPTY_HISTEDIT);
6289 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6290 struct got_histedit_list_entry *hle2;
6291 TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6294 if (got_object_id_cmp(hle->commit_id,
6295 hle2->commit_id) != 0)
6297 err = got_object_id_str(&id_str, hle->commit_id);
6300 snprintf(msg, sizeof(msg), "commit %s is listed "
6301 "more than once in histedit script", id_str);
6303 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6307 SIMPLEQ_FOREACH(qid, commits, entry) {
6308 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6309 if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6313 err = got_object_id_str(&id_str, qid->id);
6316 snprintf(msg, sizeof(msg),
6317 "commit %s missing from histedit script", id_str);
6319 return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6323 hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6324 if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6325 return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6326 "last commit in histedit script cannot be folded");
6331 static const struct got_error *
6332 histedit_run_editor(struct got_histedit_list *histedit_cmds,
6333 const char *path, struct got_object_id_queue *commits,
6334 struct got_repository *repo)
6336 const struct got_error *err = NULL;
6340 err = get_editor(&editor);
6344 if (spawn_editor(editor, path) == -1) {
6345 err = got_error_from_errno("failed spawning editor");
6349 f = fopen(path, "r");
6351 err = got_error_from_errno("fopen");
6354 err = histedit_parse_list(histedit_cmds, f, repo);
6358 err = histedit_check_script(histedit_cmds, commits, repo);
6360 if (f && fclose(f) != 0 && err == NULL)
6361 err = got_error_from_errno("fclose");
6366 static const struct got_error *
6367 histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6368 struct got_object_id_queue *, const char *, const char *,
6369 struct got_repository *);
6371 static const struct got_error *
6372 histedit_edit_script(struct got_histedit_list *histedit_cmds,
6373 struct got_object_id_queue *commits, const char *branch_name,
6374 int edit_logmsg_only, struct got_repository *repo)
6376 const struct got_error *err;
6380 err = got_opentemp_named(&path, &f, "got-histedit");
6384 err = write_cmd_list(f, branch_name, commits);
6388 err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6392 if (edit_logmsg_only) {
6394 err = histedit_parse_list(histedit_cmds, f, repo);
6396 if (fclose(f) != 0) {
6397 err = got_error_from_errno("fclose");
6401 err = histedit_run_editor(histedit_cmds, path, commits, repo);
6403 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6404 err->code != GOT_ERR_HISTEDIT_CMD)
6406 err = histedit_edit_list_retry(histedit_cmds, err,
6407 commits, path, branch_name, repo);
6411 if (f && fclose(f) != 0 && err == NULL)
6412 err = got_error_from_errno("fclose");
6413 if (path && unlink(path) != 0 && err == NULL)
6414 err = got_error_from_errno2("unlink", path);
6419 static const struct got_error *
6420 histedit_save_list(struct got_histedit_list *histedit_cmds,
6421 struct got_worktree *worktree, struct got_repository *repo)
6423 const struct got_error *err = NULL;
6426 struct got_histedit_list_entry *hle;
6427 struct got_commit_object *commit = NULL;
6429 err = got_worktree_get_histedit_script_path(&path, worktree);
6433 f = fopen(path, "w");
6435 err = got_error_from_errno2("fopen", path);
6438 TAILQ_FOREACH(hle, histedit_cmds, entry) {
6439 err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6445 int n = fprintf(f, "%c %s\n",
6446 GOT_HISTEDIT_MESG, hle->logmsg);
6448 err = got_ferror(f, GOT_ERR_IO);
6454 if (f && fclose(f) != 0 && err == NULL)
6455 err = got_error_from_errno("fclose");
6458 got_object_commit_close(commit);
6463 histedit_free_list(struct got_histedit_list *histedit_cmds)
6465 struct got_histedit_list_entry *hle;
6467 while ((hle = TAILQ_FIRST(histedit_cmds))) {
6468 TAILQ_REMOVE(histedit_cmds, hle, entry);
6473 static const struct got_error *
6474 histedit_load_list(struct got_histedit_list *histedit_cmds,
6475 const char *path, struct got_repository *repo)
6477 const struct got_error *err = NULL;
6480 f = fopen(path, "r");
6482 err = got_error_from_errno2("fopen", path);
6486 err = histedit_parse_list(histedit_cmds, f, repo);
6488 if (f && fclose(f) != 0 && err == NULL)
6489 err = got_error_from_errno("fclose");
6493 static const struct got_error *
6494 histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6495 const struct got_error *edit_err, struct got_object_id_queue *commits,
6496 const char *path, const char *branch_name, struct got_repository *repo)
6498 const struct got_error *err = NULL, *prev_err = edit_err;
6501 while (resp != 'c' && resp != 'r' && resp != 'a') {
6502 printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6503 "or (a)bort: ", getprogname(), prev_err->msg);
6508 histedit_free_list(histedit_cmds);
6509 err = histedit_run_editor(histedit_cmds, path, commits,
6512 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6513 err->code != GOT_ERR_HISTEDIT_CMD)
6520 } else if (resp == 'r') {
6521 histedit_free_list(histedit_cmds);
6522 err = histedit_edit_script(histedit_cmds,
6523 commits, branch_name, 0, repo);
6525 if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6526 err->code != GOT_ERR_HISTEDIT_CMD)
6533 } else if (resp == 'a') {
6534 err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6537 printf("invalid response '%c'\n", resp);
6543 static const struct got_error *
6544 histedit_complete(struct got_worktree *worktree,
6545 struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6546 struct got_reference *branch, struct got_repository *repo)
6548 printf("Switching work tree to %s\n",
6549 got_ref_get_symref_target(branch));
6550 return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6554 static const struct got_error *
6555 show_histedit_progress(struct got_commit_object *commit,
6556 struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6558 const struct got_error *err;
6559 char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6561 err = got_object_id_str(&old_id_str, hle->commit_id);
6566 err = got_object_id_str(&new_id_str, new_id);
6571 old_id_str[12] = '\0';
6573 new_id_str[12] = '\0';
6576 logmsg = strdup(hle->logmsg);
6577 if (logmsg == NULL) {
6578 err = got_error_from_errno("strdup");
6581 trim_logmsg(logmsg, 42);
6583 err = get_short_logmsg(&logmsg, 42, commit);
6588 switch (hle->cmd->code) {
6589 case GOT_HISTEDIT_PICK:
6590 case GOT_HISTEDIT_EDIT:
6591 printf("%s -> %s: %s\n", old_id_str,
6592 new_id_str ? new_id_str : "no-op change", logmsg);
6594 case GOT_HISTEDIT_DROP:
6595 case GOT_HISTEDIT_FOLD:
6596 printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6608 static const struct got_error *
6609 histedit_commit(struct got_pathlist_head *merged_paths,
6610 struct got_worktree *worktree, struct got_fileindex *fileindex,
6611 struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6612 struct got_repository *repo)
6614 const struct got_error *err;
6615 struct got_commit_object *commit;
6616 struct got_object_id *new_commit_id;
6618 if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6619 && hle->logmsg == NULL) {
6620 err = histedit_edit_logmsg(hle, repo);
6625 err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6629 err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6630 worktree, fileindex, tmp_branch, commit, hle->commit_id,
6633 if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6635 err = show_histedit_progress(commit, hle, NULL);
6637 err = show_histedit_progress(commit, hle, new_commit_id);
6638 free(new_commit_id);
6641 got_object_commit_close(commit);
6645 static const struct got_error *
6646 histedit_skip_commit(struct got_histedit_list_entry *hle,
6647 struct got_worktree *worktree, struct got_repository *repo)
6649 const struct got_error *error;
6650 struct got_commit_object *commit;
6652 error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6657 error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6661 error = show_histedit_progress(commit, hle, NULL);
6662 got_object_commit_close(commit);
6666 static const struct got_error *
6667 check_local_changes(void *arg, unsigned char status,
6668 unsigned char staged_status, const char *path,
6669 struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6670 struct got_object_id *commit_id, int dirfd, const char *de_name)
6672 int *have_local_changes = arg;
6675 case GOT_STATUS_ADD:
6676 case GOT_STATUS_DELETE:
6677 case GOT_STATUS_MODIFY:
6678 case GOT_STATUS_CONFLICT:
6679 *have_local_changes = 1;
6680 return got_error(GOT_ERR_CANCELLED);
6685 switch (staged_status) {
6686 case GOT_STATUS_ADD:
6687 case GOT_STATUS_DELETE:
6688 case GOT_STATUS_MODIFY:
6689 *have_local_changes = 1;
6690 return got_error(GOT_ERR_CANCELLED);
6698 static const struct got_error *
6699 cmd_histedit(int argc, char *argv[])
6701 const struct got_error *error = NULL;
6702 struct got_worktree *worktree = NULL;
6703 struct got_fileindex *fileindex = NULL;
6704 struct got_repository *repo = NULL;
6706 struct got_reference *branch = NULL;
6707 struct got_reference *tmp_branch = NULL;
6708 struct got_object_id *resume_commit_id = NULL;
6709 struct got_object_id *base_commit_id = NULL;
6710 struct got_object_id *head_commit_id = NULL;
6711 struct got_commit_object *commit = NULL;
6712 int ch, rebase_in_progress = 0, did_something;
6713 int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6714 int edit_logmsg_only = 0;
6715 const char *edit_script_path = NULL;
6716 unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6717 struct got_object_id_queue commits;
6718 struct got_pathlist_head merged_paths;
6719 const struct got_object_id_queue *parent_ids;
6720 struct got_object_qid *pid;
6721 struct got_histedit_list histedit_cmds;
6722 struct got_histedit_list_entry *hle;
6724 SIMPLEQ_INIT(&commits);
6725 TAILQ_INIT(&histedit_cmds);
6726 TAILQ_INIT(&merged_paths);
6728 while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6737 edit_script_path = optarg;
6740 edit_logmsg_only = 1;
6752 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6753 "unveil", NULL) == -1)
6756 if (abort_edit && continue_edit)
6757 errx(1, "histedit's -a and -c options are mutually exclusive");
6758 if (edit_script_path && edit_logmsg_only)
6759 errx(1, "histedit's -F and -m options are mutually exclusive");
6760 if (abort_edit && edit_logmsg_only)
6761 errx(1, "histedit's -a and -m options are mutually exclusive");
6762 if (continue_edit && edit_logmsg_only)
6763 errx(1, "histedit's -c and -m options are mutually exclusive");
6768 * This command cannot apply unveil(2) in all cases because the
6769 * user may choose to run an editor to edit the histedit script
6770 * and to edit individual commit log messages.
6771 * unveil(2) traverses exec(2); if an editor is used we have to
6772 * apply unveil after edit script and log messages have been written.
6773 * XXX TODO: Make use of unveil(2) where possible.
6776 cwd = getcwd(NULL, 0);
6778 error = got_error_from_errno("getcwd");
6781 error = got_worktree_open(&worktree, cwd);
6785 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6790 error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6793 if (rebase_in_progress) {
6794 error = got_error(GOT_ERR_REBASING);
6798 error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6802 if (edit_in_progress && edit_logmsg_only) {
6803 error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6804 "histedit operation is in progress in this "
6805 "work tree and must be continued or aborted "
6806 "before the -m option can be used");
6810 if (edit_in_progress && abort_edit) {
6811 error = got_worktree_histedit_continue(&resume_commit_id,
6812 &tmp_branch, &branch, &base_commit_id, &fileindex,
6816 printf("Switching work tree to %s\n",
6817 got_ref_get_symref_target(branch));
6818 error = got_worktree_histedit_abort(worktree, fileindex, repo,
6819 branch, base_commit_id, update_progress, &did_something);
6822 printf("Histedit of %s aborted\n",
6823 got_ref_get_symref_target(branch));
6824 goto done; /* nothing else to do */
6825 } else if (abort_edit) {
6826 error = got_error(GOT_ERR_NOT_HISTEDIT);
6830 if (continue_edit) {
6833 if (!edit_in_progress) {
6834 error = got_error(GOT_ERR_NOT_HISTEDIT);
6838 error = got_worktree_get_histedit_script_path(&path, worktree);
6842 error = histedit_load_list(&histedit_cmds, path, repo);
6847 error = got_worktree_histedit_continue(&resume_commit_id,
6848 &tmp_branch, &branch, &base_commit_id, &fileindex,
6853 error = got_ref_resolve(&head_commit_id, repo, branch);
6857 error = got_object_open_as_commit(&commit, repo,
6861 parent_ids = got_object_commit_get_parent_ids(commit);
6862 pid = SIMPLEQ_FIRST(parent_ids);
6864 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6867 error = collect_commits(&commits, head_commit_id, pid->id,
6868 base_commit_id, got_worktree_get_path_prefix(worktree),
6869 GOT_ERR_HISTEDIT_PATH, repo);
6870 got_object_commit_close(commit);
6875 if (edit_in_progress) {
6876 error = got_error(GOT_ERR_HISTEDIT_BUSY);
6880 error = got_ref_open(&branch, repo,
6881 got_worktree_get_head_ref_name(worktree), 0);
6885 if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6886 error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6887 "will not edit commit history of a branch outside "
6888 "the \"refs/heads/\" reference namespace");
6892 error = got_ref_resolve(&head_commit_id, repo, branch);
6893 got_ref_close(branch);
6898 error = got_object_open_as_commit(&commit, repo,
6902 parent_ids = got_object_commit_get_parent_ids(commit);
6903 pid = SIMPLEQ_FIRST(parent_ids);
6905 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6908 error = collect_commits(&commits, head_commit_id, pid->id,
6909 got_worktree_get_base_commit_id(worktree),
6910 got_worktree_get_path_prefix(worktree),
6911 GOT_ERR_HISTEDIT_PATH, repo);
6912 got_object_commit_close(commit);
6917 if (SIMPLEQ_EMPTY(&commits)) {
6918 error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6922 error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6923 &base_commit_id, &fileindex, worktree, repo);
6927 if (edit_script_path) {
6928 error = histedit_load_list(&histedit_cmds,
6929 edit_script_path, repo);
6931 got_worktree_histedit_abort(worktree, fileindex,
6932 repo, branch, base_commit_id,
6933 update_progress, &did_something);
6937 const char *branch_name;
6938 branch_name = got_ref_get_symref_target(branch);
6939 if (strncmp(branch_name, "refs/heads/", 11) == 0)
6941 error = histedit_edit_script(&histedit_cmds, &commits,
6942 branch_name, edit_logmsg_only, repo);
6944 got_worktree_histedit_abort(worktree, fileindex,
6945 repo, branch, base_commit_id,
6946 update_progress, &did_something);
6952 error = histedit_save_list(&histedit_cmds, worktree,
6955 got_worktree_histedit_abort(worktree, fileindex,
6956 repo, branch, base_commit_id,
6957 update_progress, &did_something);
6963 error = histedit_check_script(&histedit_cmds, &commits, repo);
6967 TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6968 if (resume_commit_id) {
6969 if (got_object_id_cmp(hle->commit_id,
6970 resume_commit_id) != 0)
6973 resume_commit_id = NULL;
6974 if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6975 hle->cmd->code == GOT_HISTEDIT_FOLD) {
6976 error = histedit_skip_commit(hle, worktree,
6981 struct got_pathlist_head paths;
6982 int have_changes = 0;
6985 error = got_pathlist_append(&paths, "", NULL);
6988 error = got_worktree_status(worktree, &paths,
6989 repo, check_local_changes, &have_changes,
6990 check_cancelled, NULL);
6991 got_pathlist_free(&paths);
6993 if (error->code != GOT_ERR_CANCELLED)
6995 if (sigint_received || sigpipe_received)
6999 error = histedit_commit(NULL, worktree,
7000 fileindex, tmp_branch, hle, repo);
7004 error = got_object_open_as_commit(
7005 &commit, repo, hle->commit_id);
7008 error = show_histedit_progress(commit,
7010 got_object_commit_close(commit);
7019 if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7020 error = histedit_skip_commit(hle, worktree, repo);
7026 error = got_object_open_as_commit(&commit, repo,
7030 parent_ids = got_object_commit_get_parent_ids(commit);
7031 pid = SIMPLEQ_FIRST(parent_ids);
7033 error = got_worktree_histedit_merge_files(&merged_paths,
7034 worktree, fileindex, pid->id, hle->commit_id, repo,
7035 rebase_progress, &rebase_status, check_cancelled, NULL);
7038 got_object_commit_close(commit);
7041 if (rebase_status == GOT_STATUS_CONFLICT) {
7042 error = show_rebase_merge_conflict(hle->commit_id,
7046 got_worktree_rebase_pathlist_free(&merged_paths);
7050 if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7052 error = got_object_id_str(&id_str, hle->commit_id);
7055 printf("Stopping histedit for amending commit %s\n",
7058 got_worktree_rebase_pathlist_free(&merged_paths);
7059 error = got_worktree_histedit_postpone(worktree,
7064 if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7065 error = histedit_skip_commit(hle, worktree, repo);
7071 error = histedit_commit(&merged_paths, worktree, fileindex,
7072 tmp_branch, hle, repo);
7073 got_worktree_rebase_pathlist_free(&merged_paths);
7078 if (rebase_status == GOT_STATUS_CONFLICT) {
7079 error = got_worktree_histedit_postpone(worktree, fileindex);
7082 error = got_error_msg(GOT_ERR_CONFLICTS,
7083 "conflicts must be resolved before histedit can continue");
7085 error = histedit_complete(worktree, fileindex, tmp_branch,
7088 got_object_id_queue_free(&commits);
7089 histedit_free_list(&histedit_cmds);
7090 free(head_commit_id);
7091 free(base_commit_id);
7092 free(resume_commit_id);
7094 got_object_commit_close(commit);
7096 got_ref_close(branch);
7098 got_ref_close(tmp_branch);
7100 got_worktree_close(worktree);
7102 got_repo_close(repo);
7107 usage_integrate(void)
7109 fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7113 static const struct got_error *
7114 cmd_integrate(int argc, char *argv[])
7116 const struct got_error *error = NULL;
7117 struct got_repository *repo = NULL;
7118 struct got_worktree *worktree = NULL;
7119 char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7120 const char *branch_arg = NULL;
7121 struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7122 struct got_fileindex *fileindex = NULL;
7123 struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7124 int ch, did_something = 0;
7126 while ((ch = getopt(argc, argv, "")) != -1) {
7139 branch_arg = argv[0];
7141 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7142 "unveil", NULL) == -1)
7145 cwd = getcwd(NULL, 0);
7147 error = got_error_from_errno("getcwd");
7151 error = got_worktree_open(&worktree, cwd);
7155 error = check_rebase_or_histedit_in_progress(worktree);
7159 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7164 error = apply_unveil(got_repo_get_path(repo), 0,
7165 got_worktree_get_root_path(worktree));
7169 if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7170 error = got_error_from_errno("asprintf");
7174 error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7175 &base_branch_ref, worktree, refname, repo);
7179 refname = strdup(got_ref_get_name(branch_ref));
7180 if (refname == NULL) {
7181 error = got_error_from_errno("strdup");
7182 got_worktree_integrate_abort(worktree, fileindex, repo,
7183 branch_ref, base_branch_ref);
7186 base_refname = strdup(got_ref_get_name(base_branch_ref));
7187 if (base_refname == NULL) {
7188 error = got_error_from_errno("strdup");
7189 got_worktree_integrate_abort(worktree, fileindex, repo,
7190 branch_ref, base_branch_ref);
7194 error = got_ref_resolve(&commit_id, repo, branch_ref);
7198 error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7202 if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7203 error = got_error_msg(GOT_ERR_SAME_BRANCH,
7204 "specified branch has already been integrated");
7205 got_worktree_integrate_abort(worktree, fileindex, repo,
7206 branch_ref, base_branch_ref);
7210 error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7212 if (error->code == GOT_ERR_ANCESTRY)
7213 error = got_error(GOT_ERR_REBASE_REQUIRED);
7214 got_worktree_integrate_abort(worktree, fileindex, repo,
7215 branch_ref, base_branch_ref);
7219 error = got_worktree_integrate_continue(worktree, fileindex, repo,
7220 branch_ref, base_branch_ref, update_progress, &did_something,
7221 check_cancelled, NULL);
7225 printf("Integrated %s into %s\n", refname, base_refname);
7228 got_repo_close(repo);
7230 got_worktree_close(worktree);
7232 free(base_commit_id);
7242 fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7243 "[file-path ...]\n",
7248 static const struct got_error *
7249 print_stage(void *arg, unsigned char status, unsigned char staged_status,
7250 const char *path, struct got_object_id *blob_id,
7251 struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7252 int dirfd, const char *de_name)
7254 const struct got_error *err = NULL;
7255 char *id_str = NULL;
7257 if (staged_status != GOT_STATUS_ADD &&
7258 staged_status != GOT_STATUS_MODIFY &&
7259 staged_status != GOT_STATUS_DELETE)
7262 if (staged_status == GOT_STATUS_ADD ||
7263 staged_status == GOT_STATUS_MODIFY)
7264 err = got_object_id_str(&id_str, staged_blob_id);
7266 err = got_object_id_str(&id_str, blob_id);
7270 printf("%s %c %s\n", id_str, staged_status, path);
7275 static const struct got_error *
7276 cmd_stage(int argc, char *argv[])
7278 const struct got_error *error = NULL;
7279 struct got_repository *repo = NULL;
7280 struct got_worktree *worktree = NULL;
7282 struct got_pathlist_head paths;
7283 struct got_pathlist_entry *pe;
7284 int ch, list_stage = 0, pflag = 0;
7285 FILE *patch_script_file = NULL;
7286 const char *patch_script_path = NULL;
7287 struct choose_patch_arg cpa;
7291 while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7300 patch_script_path = optarg;
7312 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7313 "unveil", NULL) == -1)
7316 if (list_stage && (pflag || patch_script_path))
7317 errx(1, "-l option cannot be used with other options");
7318 if (patch_script_path && !pflag)
7319 errx(1, "-F option can only be used together with -p option");
7321 cwd = getcwd(NULL, 0);
7323 error = got_error_from_errno("getcwd");
7327 error = got_worktree_open(&worktree, cwd);
7331 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7336 if (patch_script_path) {
7337 patch_script_file = fopen(patch_script_path, "r");
7338 if (patch_script_file == NULL) {
7339 error = got_error_from_errno2("fopen",
7344 error = apply_unveil(got_repo_get_path(repo), 0,
7345 got_worktree_get_root_path(worktree));
7349 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7354 error = got_worktree_status(worktree, &paths, repo,
7355 print_stage, NULL, check_cancelled, NULL);
7357 cpa.patch_script_file = patch_script_file;
7358 cpa.action = "stage";
7359 error = got_worktree_stage(worktree, &paths,
7360 pflag ? NULL : print_status, NULL,
7361 pflag ? choose_patch : NULL, &cpa, repo);
7364 if (patch_script_file && fclose(patch_script_file) == EOF &&
7366 error = got_error_from_errno2("fclose", patch_script_path);
7368 got_repo_close(repo);
7370 got_worktree_close(worktree);
7371 TAILQ_FOREACH(pe, &paths, entry)
7372 free((char *)pe->path);
7373 got_pathlist_free(&paths);
7381 fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7382 "[file-path ...]\n",
7388 static const struct got_error *
7389 cmd_unstage(int argc, char *argv[])
7391 const struct got_error *error = NULL;
7392 struct got_repository *repo = NULL;
7393 struct got_worktree *worktree = NULL;
7395 struct got_pathlist_head paths;
7396 struct got_pathlist_entry *pe;
7397 int ch, did_something = 0, pflag = 0;
7398 FILE *patch_script_file = NULL;
7399 const char *patch_script_path = NULL;
7400 struct choose_patch_arg cpa;
7404 while ((ch = getopt(argc, argv, "pF:")) != -1) {
7410 patch_script_path = optarg;
7422 if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7423 "unveil", NULL) == -1)
7426 if (patch_script_path && !pflag)
7427 errx(1, "-F option can only be used together with -p option");
7429 cwd = getcwd(NULL, 0);
7431 error = got_error_from_errno("getcwd");
7435 error = got_worktree_open(&worktree, cwd);
7439 error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7444 if (patch_script_path) {
7445 patch_script_file = fopen(patch_script_path, "r");
7446 if (patch_script_file == NULL) {
7447 error = got_error_from_errno2("fopen",
7453 error = apply_unveil(got_repo_get_path(repo), 0,
7454 got_worktree_get_root_path(worktree));
7458 error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7462 cpa.patch_script_file = patch_script_file;
7463 cpa.action = "unstage";
7464 error = got_worktree_unstage(worktree, &paths, update_progress,
7465 &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7467 if (patch_script_file && fclose(patch_script_file) == EOF &&
7469 error = got_error_from_errno2("fclose", patch_script_path);
7471 got_repo_close(repo);
7473 got_worktree_close(worktree);
7474 TAILQ_FOREACH(pe, &paths, entry)
7475 free((char *)pe->path);
7476 got_pathlist_free(&paths);
7484 fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7485 "arg1 [arg2 ...]\n", getprogname());
7489 static const struct got_error *
7490 cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7492 const struct got_error *err;
7493 struct got_blob_object *blob;
7495 err = got_object_open_as_blob(&blob, repo, id, 8192);
7499 err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7500 got_object_blob_close(blob);
7504 static const struct got_error *
7505 cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7507 const struct got_error *err;
7508 struct got_tree_object *tree;
7511 err = got_object_open_as_tree(&tree, repo, id);
7515 nentries = got_object_tree_get_nentries(tree);
7516 for (i = 0; i < nentries; i++) {
7517 struct got_tree_entry *te;
7519 if (sigint_received || sigpipe_received)
7521 te = got_object_tree_get_entry(tree, i);
7522 err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7525 fprintf(outfile, "%s %.7o %s\n", id_str,
7526 got_tree_entry_get_mode(te),
7527 got_tree_entry_get_name(te));
7531 got_object_tree_close(tree);
7535 static const struct got_error *
7536 cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7538 const struct got_error *err;
7539 struct got_commit_object *commit;
7540 const struct got_object_id_queue *parent_ids;
7541 struct got_object_qid *pid;
7542 char *id_str = NULL;
7543 const char *logmsg = NULL;
7545 err = got_object_open_as_commit(&commit, repo, id);
7549 err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7553 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7554 parent_ids = got_object_commit_get_parent_ids(commit);
7555 fprintf(outfile, "numparents %d\n",
7556 got_object_commit_get_nparents(commit));
7557 SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7559 err = got_object_id_str(&pid_str, pid->id);
7562 fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7565 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7566 got_object_commit_get_author(commit),
7567 got_object_commit_get_author_time(commit));
7569 fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7570 got_object_commit_get_author(commit),
7571 got_object_commit_get_committer_time(commit));
7573 logmsg = got_object_commit_get_logmsg_raw(commit);
7574 fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7575 fprintf(outfile, "%s", logmsg);
7578 got_object_commit_close(commit);
7582 static const struct got_error *
7583 cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7585 const struct got_error *err;
7586 struct got_tag_object *tag;
7587 char *id_str = NULL;
7588 const char *tagmsg = NULL;
7590 err = got_object_open_as_tag(&tag, repo, id);
7594 err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7598 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7600 switch (got_object_tag_get_object_type(tag)) {
7601 case GOT_OBJ_TYPE_BLOB:
7602 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7603 GOT_OBJ_LABEL_BLOB);
7605 case GOT_OBJ_TYPE_TREE:
7606 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7607 GOT_OBJ_LABEL_TREE);
7609 case GOT_OBJ_TYPE_COMMIT:
7610 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7611 GOT_OBJ_LABEL_COMMIT);
7613 case GOT_OBJ_TYPE_TAG:
7614 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7621 fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7622 got_object_tag_get_name(tag));
7624 fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7625 got_object_tag_get_tagger(tag),
7626 got_object_tag_get_tagger_time(tag));
7628 tagmsg = got_object_tag_get_message(tag);
7629 fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7630 fprintf(outfile, "%s", tagmsg);
7633 got_object_tag_close(tag);
7637 static const struct got_error *
7638 cmd_cat(int argc, char *argv[])
7640 const struct got_error *error;
7641 struct got_repository *repo = NULL;
7642 struct got_worktree *worktree = NULL;
7643 char *cwd = NULL, *repo_path = NULL, *label = NULL;
7644 const char *commit_id_str = NULL;
7645 struct got_object_id *id = NULL, *commit_id = NULL;
7646 int ch, obj_type, i, force_path = 0;
7649 if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7654 while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7657 commit_id_str = optarg;
7660 repo_path = realpath(optarg, NULL);
7661 if (repo_path == NULL)
7662 return got_error_from_errno2("realpath",
7664 got_path_strip_trailing_slashes(repo_path);
7678 cwd = getcwd(NULL, 0);
7680 error = got_error_from_errno("getcwd");
7683 error = got_worktree_open(&worktree, cwd);
7684 if (error && error->code != GOT_ERR_NOT_WORKTREE)
7687 if (repo_path == NULL) {
7689 got_worktree_get_repo_path(worktree));
7690 if (repo_path == NULL) {
7691 error = got_error_from_errno("strdup");
7697 if (repo_path == NULL) {
7698 repo_path = getcwd(NULL, 0);
7699 if (repo_path == NULL)
7700 return got_error_from_errno("getcwd");
7703 error = got_repo_open(&repo, repo_path, NULL);
7708 error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7712 if (commit_id_str == NULL)
7713 commit_id_str = GOT_REF_HEAD;
7714 error = got_repo_match_object_id(&commit_id, NULL,
7715 commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7719 for (i = 0; i < argc; i++) {
7721 error = got_object_id_by_path(&id, repo, commit_id,
7726 error = got_repo_match_object_id(&id, &label, argv[i],
7727 GOT_OBJ_TYPE_ANY, 0, repo);
7729 if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7730 error->code != GOT_ERR_NOT_REF)
7732 error = got_object_id_by_path(&id, repo,
7733 commit_id, argv[i]);
7739 error = got_object_get_type(&obj_type, repo, id);
7744 case GOT_OBJ_TYPE_BLOB:
7745 error = cat_blob(id, repo, stdout);
7747 case GOT_OBJ_TYPE_TREE:
7748 error = cat_tree(id, repo, stdout);
7750 case GOT_OBJ_TYPE_COMMIT:
7751 error = cat_commit(id, repo, stdout);
7753 case GOT_OBJ_TYPE_TAG:
7754 error = cat_tag(id, repo, stdout);
7757 error = got_error(GOT_ERR_OBJ_TYPE);
7772 got_worktree_close(worktree);
7774 const struct got_error *repo_error;
7775 repo_error = got_repo_close(repo);